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
14 use std::collections::HashSet;
20 use syntax::attr::AttrMetaMethods;
21 use syntax::codemap::Span;
23 use rustc::front::map as hir_map;
24 use rustc::middle::stability;
31 // looks to me like the first two of these are actually
32 // output parameters, maybe only mutated once; perhaps
33 // better simply to have the visit method return a tuple
36 // also, is there some reason that this doesn't use the 'visit'
37 // framework from syntax?
39 pub struct RustdocVisitor<'a, 'tcx: 'a> {
41 pub attrs: Vec<ast::Attribute>,
42 pub cx: &'a core::DocContext<'a, 'tcx>,
43 pub analysis: Option<&'a core::CrateAnalysis>,
44 view_item_stack: HashSet<ast::NodeId>,
45 inlining_from_glob: bool,
48 impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
49 pub fn new(cx: &'a core::DocContext<'a, 'tcx>,
50 analysis: Option<&'a core::CrateAnalysis>) -> RustdocVisitor<'a, 'tcx> {
51 // If the root is reexported, terminate all recursion.
52 let mut stack = HashSet::new();
53 stack.insert(ast::CRATE_NODE_ID);
55 module: Module::new(None),
59 view_item_stack: stack,
60 inlining_from_glob: false,
64 fn stability(&self, id: ast::NodeId) -> Option<attr::Stability> {
65 self.cx.tcx_opt().and_then(|tcx| {
66 self.cx.map.opt_local_def_id(id)
67 .and_then(|def_id| stability::lookup(tcx, def_id))
72 pub fn visit(&mut self, krate: &hir::Crate) {
73 self.attrs = krate.attrs.clone();
75 self.module = self.visit_mod_contents(krate.span,
81 // attach the crate's exported macros to the top-level module:
82 self.module.macros = krate.exported_macros.iter()
83 .map(|def| self.visit_macro(def)).collect();
84 self.module.is_crate = true;
87 pub fn visit_struct_def(&mut self, item: &hir::Item,
88 name: ast::Name, sd: &hir::StructDef,
89 generics: &hir::Generics) -> Struct {
90 debug!("Visiting struct");
91 let struct_type = struct_type_from_def(&*sd);
94 struct_type: struct_type,
97 stab: self.stability(item.id),
98 attrs: item.attrs.clone(),
99 generics: generics.clone(),
100 fields: sd.fields.clone(),
105 pub fn visit_enum_def(&mut self, it: &hir::Item,
106 name: ast::Name, def: &hir::EnumDef,
107 params: &hir::Generics) -> Enum {
108 debug!("Visiting enum");
111 variants: def.variants.iter().map(|v| Variant {
113 attrs: v.node.attrs.clone(),
114 stab: self.stability(v.node.id),
116 kind: v.node.kind.clone(),
120 stab: self.stability(it.id),
121 generics: params.clone(),
122 attrs: it.attrs.clone(),
128 pub fn visit_fn(&mut self, item: &hir::Item,
129 name: ast::Name, fd: &hir::FnDecl,
130 unsafety: &hir::Unsafety,
131 constness: hir::Constness,
133 gen: &hir::Generics) -> Function {
134 debug!("Visiting fn");
138 stab: self.stability(item.id),
139 attrs: item.attrs.clone(),
143 generics: gen.clone(),
145 constness: constness,
150 pub fn visit_mod_contents(&mut self, span: Span, attrs: Vec<ast::Attribute> ,
151 vis: hir::Visibility, id: ast::NodeId,
153 name: Option<ast::Name>) -> Module {
154 let mut om = Module::new(name);
155 om.where_outer = span;
156 om.where_inner = m.inner;
159 om.stab = self.stability(id);
162 self.visit_item(&**i, None, &mut om);
167 fn visit_view_path(&mut self, path: hir::ViewPath_,
170 please_inline: bool) -> Option<hir::ViewPath_> {
172 hir::ViewPathSimple(dst, base) => {
173 if self.resolve_id(id, Some(dst), false, om, please_inline) {
176 Some(hir::ViewPathSimple(dst, base))
179 hir::ViewPathList(p, paths) => {
180 let mine = paths.into_iter().filter(|path| {
181 !self.resolve_id(path.node.id(), None, false, om,
183 }).collect::<Vec<hir::PathListItem>>();
188 Some(hir::ViewPathList(p, mine))
192 // these are feature gated anyway
193 hir::ViewPathGlob(base) => {
194 if self.resolve_id(id, None, true, om, please_inline) {
197 Some(hir::ViewPathGlob(base))
204 fn resolve_id(&mut self, id: ast::NodeId, renamed: Option<ast::Name>,
205 glob: bool, om: &mut Module, please_inline: bool) -> bool {
206 let tcx = match self.cx.tcx_opt() {
210 let def = tcx.def_map.borrow()[&id].def_id();
211 let def_node_id = match tcx.map.as_local_node_id(def) {
212 Some(n) => n, None => return false
214 let analysis = match self.analysis {
215 Some(analysis) => analysis, None => return false
217 if !please_inline && analysis.public_items.contains(&def) {
220 if !self.view_item_stack.insert(def_node_id) { return false }
222 let ret = match tcx.map.get(def_node_id) {
223 hir_map::NodeItem(it) => {
225 let prev = mem::replace(&mut self.inlining_from_glob, true);
227 hir::ItemMod(ref m) => {
229 self.visit_item(&**i, None, om);
232 hir::ItemEnum(..) => {}
233 _ => { panic!("glob not mapped to a module or enum"); }
235 self.inlining_from_glob = prev;
237 self.visit_item(it, renamed, om);
243 self.view_item_stack.remove(&def_node_id);
247 pub fn visit_item(&mut self, item: &hir::Item,
248 renamed: Option<ast::Name>, om: &mut Module) {
249 debug!("Visiting item {:?}", item);
250 let name = renamed.unwrap_or(item.name);
252 hir::ItemExternCrate(ref p) => {
253 let path = match *p {
255 Some(x) => Some(x.to_string()),
257 om.extern_crates.push(ExternCrate {
261 attrs: item.attrs.clone(),
265 hir::ItemUse(ref vpath) => {
266 let node = vpath.node.clone();
267 let node = if item.vis == hir::Public {
268 let please_inline = item.attrs.iter().any(|item| {
269 match item.meta_item_list() {
271 list.iter().any(|i| &i.name()[..] == "inline")
276 match self.visit_view_path(node, om, item.id, please_inline) {
283 om.imports.push(Import {
286 attrs: item.attrs.clone(),
291 hir::ItemMod(ref m) => {
292 om.mods.push(self.visit_mod_contents(item.span,
299 hir::ItemEnum(ref ed, ref gen) =>
300 om.enums.push(self.visit_enum_def(item, name, ed, gen)),
301 hir::ItemStruct(ref sd, ref gen) =>
302 om.structs.push(self.visit_struct_def(item, name, &**sd, gen)),
303 hir::ItemFn(ref fd, ref unsafety, constness, ref abi, ref gen, _) =>
304 om.fns.push(self.visit_fn(item, name, &**fd, unsafety,
305 constness, abi, gen)),
306 hir::ItemTy(ref ty, ref gen) => {
312 attrs: item.attrs.clone(),
315 stab: self.stability(item.id),
319 hir::ItemStatic(ref ty, ref mut_, ref exp) => {
322 mutability: mut_.clone(),
326 attrs: item.attrs.clone(),
329 stab: self.stability(item.id),
333 hir::ItemConst(ref ty, ref exp) => {
339 attrs: item.attrs.clone(),
342 stab: self.stability(item.id),
344 om.constants.push(s);
346 hir::ItemTrait(unsafety, ref gen, ref b, ref items) => {
350 items: items.clone(),
351 generics: gen.clone(),
352 bounds: b.iter().cloned().collect(),
354 attrs: item.attrs.clone(),
357 stab: self.stability(item.id),
361 hir::ItemImpl(unsafety, polarity, ref gen, ref tr, ref ty, ref items) => {
365 generics: gen.clone(),
368 items: items.clone(),
369 attrs: item.attrs.clone(),
373 stab: self.stability(item.id),
375 // Don't duplicate impls when inlining glob imports, we'll pick
376 // them up regardless of where they're located.
377 if !self.inlining_from_glob {
381 hir::ItemDefaultImpl(unsafety, ref trait_ref) => {
382 let i = DefaultImpl {
384 trait_: trait_ref.clone(),
386 attrs: item.attrs.clone(),
389 // see comment above about ItemImpl
390 if !self.inlining_from_glob {
391 om.def_traits.push(i);
394 hir::ItemForeignMod(ref fm) => {
395 om.foreigns.push(fm.clone());
400 // convert each exported_macro into a doc item
401 fn visit_macro(&self, def: &hir::MacroDef) -> Macro {
404 attrs: def.attrs.clone(),
407 stab: self.stability(def.id),
408 imported_from: def.imported_from,