1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Rust AST Visitor. Extracts useful information and massages it into a form
15 use syntax::abi::AbiSet;
19 use syntax::codemap::Span;
24 pub struct RustdocVisitor<'a> {
26 attrs: ~[ast::Attribute],
27 cx: &'a core::DocContext,
28 analysis: Option<&'a core::CrateAnalysis>,
31 impl<'a> RustdocVisitor<'a> {
32 pub fn new<'b>(cx: &'b core::DocContext,
33 analysis: Option<&'b core::CrateAnalysis>) -> RustdocVisitor<'b> {
35 module: Module::new(None),
42 pub fn visit(&mut self, krate: &ast::Crate) {
43 self.attrs = krate.attrs.iter().map(|x| (*x).clone()).collect();
45 self.module = self.visit_mod_contents(krate.span,
54 self.module.is_crate = true;
57 pub fn visit_struct_def(&mut self, item: &ast::Item, sd: @ast::StructDef,
58 generics: &ast::Generics) -> Struct {
59 debug!("Visiting struct");
60 let struct_type = struct_type_from_def(sd);
63 struct_type: struct_type,
66 attrs: item.attrs.iter().map(|x| *x).collect(),
67 generics: generics.clone(),
68 fields: sd.fields.iter().map(|x| (*x).clone()).collect(),
73 pub fn visit_enum_def(&mut self, it: &ast::Item, def: &ast::EnumDef,
74 params: &ast::Generics) -> Enum {
75 debug!("Visiting enum");
76 let mut vars: ~[Variant] = ~[];
77 for x in def.variants.iter() {
80 attrs: x.node.attrs.iter().map(|x| *x).collect(),
83 kind: x.node.kind.clone(),
91 generics: params.clone(),
92 attrs: it.attrs.iter().map(|x| *x).collect(),
98 pub fn visit_fn(&mut self, item: &ast::Item, fd: &ast::FnDecl,
99 purity: &ast::Purity, _abi: &AbiSet,
100 gen: &ast::Generics) -> Function {
101 debug!("Visiting fn");
105 attrs: item.attrs.iter().map(|x| *x).collect(),
109 generics: gen.clone(),
114 pub fn visit_mod_contents(&mut self, span: Span, attrs: ~[ast::Attribute],
115 vis: ast::Visibility, id: ast::NodeId,
117 name: Option<ast::Ident>) -> Module {
118 let mut om = Module::new(name);
119 for item in m.view_items.iter() {
120 self.visit_view_item(item, &mut om);
126 for i in m.items.iter() {
127 self.visit_item(*i, &mut om);
132 pub fn visit_view_item(&mut self, item: &ast::ViewItem, om: &mut Module) {
133 if item.vis != ast::Public {
134 return om.view_items.push(item.clone());
136 let item = match item.node {
137 ast::ViewItemUse(ref paths) => {
138 // rustc no longer supports "use foo, bar;"
139 assert_eq!(paths.len(), 1);
140 match self.visit_view_path(*paths.get(0), om) {
144 node: ast::ViewItemUse(vec!(path)),
150 ast::ViewItemExternMod(..) => item.clone()
152 om.view_items.push(item);
155 fn visit_view_path(&mut self, path: @ast::ViewPath,
156 om: &mut Module) -> Option<@ast::ViewPath> {
158 ast::ViewPathSimple(_, _, id) => {
159 if self.resolve_id(id, false, om) { return None }
161 ast::ViewPathList(ref p, ref paths, ref b) => {
162 let mut mine = Vec::new();
163 for path in paths.iter() {
164 if !self.resolve_id(path.node.id, false, om) {
165 mine.push(path.clone());
169 if mine.len() == 0 { return None }
170 return Some(@::syntax::codemap::Spanned {
171 node: ast::ViewPathList(p.clone(), mine, b.clone()),
176 // these are feature gated anyway
177 ast::ViewPathGlob(_, id) => {
178 if self.resolve_id(id, true, om) { return None }
184 fn resolve_id(&mut self, id: ast::NodeId, glob: bool,
185 om: &mut Module) -> bool {
187 let dm = match self.cx.tycx {
188 Some(tcx) => tcx.def_map.borrow(),
189 None => return false,
191 ast_util::def_id_of_def(*dm.get().get(&id))
193 if !ast_util::is_local(def) { return false }
194 let analysis = match self.analysis {
195 Some(analysis) => analysis, None => return false
197 if analysis.public_items.contains(&def.node) { return false }
199 let item = self.cx.tycx.unwrap().map.get(def.node);
201 ast_map::NodeItem(it) => {
204 ast::ItemMod(ref m) => {
205 for vi in m.view_items.iter() {
206 self.visit_view_item(vi, om);
208 for i in m.items.iter() {
209 self.visit_item(*i, om);
212 _ => { fail!("glob not mapped to a module"); }
215 self.visit_item(it, om);
223 pub fn visit_item(&mut self, item: &ast::Item, om: &mut Module) {
224 debug!("Visiting item {:?}", item);
226 ast::ItemMod(ref m) => {
227 om.mods.push(self.visit_mod_contents(item.span,
237 ast::ItemEnum(ref ed, ref gen) =>
238 om.enums.push(self.visit_enum_def(item, ed, gen)),
239 ast::ItemStruct(sd, ref gen) =>
240 om.structs.push(self.visit_struct_def(item, sd, gen)),
241 ast::ItemFn(fd, ref pur, ref abi, ref gen, _) =>
242 om.fns.push(self.visit_fn(item, fd, pur, abi, gen)),
243 ast::ItemTy(ty, ref gen) => {
249 attrs: item.attrs.iter().map(|x| *x).collect(),
255 ast::ItemStatic(ty, ref mut_, ref exp) => {
258 mutability: mut_.clone(),
262 attrs: item.attrs.iter().map(|x| *x).collect(),
268 ast::ItemTrait(ref gen, ref tr, ref met) => {
271 methods: met.iter().map(|x| (*x).clone()).collect(),
272 generics: gen.clone(),
273 parents: tr.iter().map(|x| (*x).clone()).collect(),
275 attrs: item.attrs.iter().map(|x| *x).collect(),
281 ast::ItemImpl(ref gen, ref tr, ty, ref meths) => {
283 generics: gen.clone(),
286 methods: meths.iter().map(|x| *x).collect(),
287 attrs: item.attrs.iter().map(|x| *x).collect(),
294 ast::ItemForeignMod(ref fm) => {
295 om.foreigns.push(fm.clone());
297 ast::ItemMac(ref _m) => {
298 om.macros.push(Macro {
300 attrs: item.attrs.iter().map(|x| *x).collect(),