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;
21 use syntax::attr::AttrMetaMethods;
22 use syntax::codemap::Span;
24 use rustc::middle::stability;
29 // looks to me like the first two of these are actually
30 // output parameters, maybe only mutated once; perhaps
31 // better simply to have the visit method return a tuple
34 // also, is there some reason that this doesn't use the 'visit'
35 // framework from syntax?
37 pub struct RustdocVisitor<'a, 'tcx: 'a> {
39 pub attrs: Vec<ast::Attribute>,
40 pub cx: &'a core::DocContext<'tcx>,
41 pub analysis: Option<&'a core::CrateAnalysis>,
42 view_item_stack: HashSet<ast::NodeId>,
45 impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
46 pub fn new(cx: &'a core::DocContext<'tcx>,
47 analysis: Option<&'a core::CrateAnalysis>) -> RustdocVisitor<'a, 'tcx> {
48 // If the root is reexported, terminate all recursion.
49 let mut stack = HashSet::new();
50 stack.insert(ast::CRATE_NODE_ID);
52 module: Module::new(None),
56 view_item_stack: stack,
60 fn stability(&self, id: ast::NodeId) -> Option<attr::Stability> {
61 self.cx.tcx_opt().and_then(|tcx| stability::lookup(tcx, ast_util::local_def(id)))
64 pub fn visit(&mut self, krate: &ast::Crate) {
65 self.attrs = krate.attrs.clone();
67 self.module = self.visit_mod_contents(krate.span,
73 // attach the crate's exported macros to the top-level module:
74 self.module.macros = krate.exported_macros.iter()
75 .map(|def| self.visit_macro(def)).collect();
76 self.module.is_crate = true;
79 pub fn visit_struct_def(&mut self, item: &ast::Item,
80 name: ast::Ident, sd: &ast::StructDef,
81 generics: &ast::Generics) -> Struct {
82 debug!("Visiting struct");
83 let struct_type = struct_type_from_def(&*sd);
86 struct_type: struct_type,
89 stab: self.stability(item.id),
90 attrs: item.attrs.clone(),
91 generics: generics.clone(),
92 fields: sd.fields.clone(),
97 pub fn visit_enum_def(&mut self, it: &ast::Item,
98 name: ast::Ident, def: &ast::EnumDef,
99 params: &ast::Generics) -> Enum {
100 debug!("Visiting enum");
103 variants: def.variants.iter().map(|v| Variant {
105 attrs: v.node.attrs.clone(),
107 stab: self.stability(v.node.id),
109 kind: v.node.kind.clone(),
113 stab: self.stability(it.id),
114 generics: params.clone(),
115 attrs: it.attrs.clone(),
121 pub fn visit_fn(&mut self, item: &ast::Item,
122 name: ast::Ident, fd: &ast::FnDecl,
123 unsafety: &ast::Unsafety, _abi: &abi::Abi,
124 gen: &ast::Generics) -> Function {
125 debug!("Visiting fn");
129 stab: self.stability(item.id),
130 attrs: item.attrs.clone(),
134 generics: gen.clone(),
139 pub fn visit_mod_contents(&mut self, span: Span, attrs: Vec<ast::Attribute> ,
140 vis: ast::Visibility, id: ast::NodeId,
142 name: Option<ast::Ident>) -> Module {
143 let mut om = Module::new(name);
144 om.where_outer = span;
145 om.where_inner = m.inner;
148 om.stab = self.stability(id);
151 self.visit_item(&**i, None, &mut om);
156 fn visit_view_path(&mut self, path: ast::ViewPath_,
159 please_inline: bool) -> Option<ast::ViewPath_> {
161 ast::ViewPathSimple(dst, base) => {
162 if self.resolve_id(id, Some(dst), false, om, please_inline) {
165 Some(ast::ViewPathSimple(dst, base))
168 ast::ViewPathList(p, paths) => {
169 let mine = paths.into_iter().filter(|path| {
170 !self.resolve_id(path.node.id(), None, false, om,
172 }).collect::<Vec<ast::PathListItem>>();
177 Some(ast::ViewPathList(p, mine))
181 // these are feature gated anyway
182 ast::ViewPathGlob(base) => {
183 if self.resolve_id(id, None, true, om, please_inline) {
186 Some(ast::ViewPathGlob(base))
193 fn resolve_id(&mut self, id: ast::NodeId, renamed: Option<ast::Ident>,
194 glob: bool, om: &mut Module, please_inline: bool) -> bool {
195 let tcx = match self.cx.tcx_opt() {
199 let def = (*tcx.def_map.borrow())[id].def_id();
200 if !ast_util::is_local(def) { return false }
201 let analysis = match self.analysis {
202 Some(analysis) => analysis, None => return false
204 if !please_inline && analysis.public_items.contains(&def.node) {
207 if !self.view_item_stack.insert(def.node) { return false }
209 let ret = match tcx.map.get(def.node) {
210 ast_map::NodeItem(it) => {
213 ast::ItemMod(ref m) => {
215 self.visit_item(&**i, None, om);
218 ast::ItemEnum(..) => {}
219 _ => { panic!("glob not mapped to a module or enum"); }
222 self.visit_item(it, renamed, om);
228 self.view_item_stack.remove(&id);
232 pub fn visit_item(&mut self, item: &ast::Item,
233 renamed: Option<ast::Ident>, om: &mut Module) {
234 debug!("Visiting item {:?}", item);
235 let name = renamed.unwrap_or(item.ident);
237 ast::ItemExternCrate(ref p) => {
238 let path = match *p {
240 Some((ref x, _)) => Some(x.to_string()),
242 om.extern_crates.push(ExternCrate {
246 attrs: item.attrs.clone(),
250 ast::ItemUse(ref vpath) => {
251 let node = vpath.node.clone();
252 let node = if item.vis == ast::Public {
253 let please_inline = item.attrs.iter().any(|item| {
254 match item.meta_item_list() {
256 list.iter().any(|i| &i.name()[] == "inline")
261 match self.visit_view_path(node, om, item.id, please_inline) {
268 om.imports.push(Import {
271 attrs: item.attrs.clone(),
276 ast::ItemMod(ref m) => {
277 om.mods.push(self.visit_mod_contents(item.span,
284 ast::ItemEnum(ref ed, ref gen) =>
285 om.enums.push(self.visit_enum_def(item, name, ed, gen)),
286 ast::ItemStruct(ref sd, ref gen) =>
287 om.structs.push(self.visit_struct_def(item, name, &**sd, gen)),
288 ast::ItemFn(ref fd, ref pur, ref abi, ref gen, _) =>
289 om.fns.push(self.visit_fn(item, name, &**fd, pur, abi, gen)),
290 ast::ItemTy(ref ty, ref gen) => {
296 attrs: item.attrs.clone(),
299 stab: self.stability(item.id),
303 ast::ItemStatic(ref ty, ref mut_, ref exp) => {
306 mutability: mut_.clone(),
310 attrs: item.attrs.clone(),
313 stab: self.stability(item.id),
317 ast::ItemConst(ref ty, ref exp) => {
323 attrs: item.attrs.clone(),
326 stab: self.stability(item.id),
328 om.constants.push(s);
330 ast::ItemTrait(unsafety, ref gen, ref b, ref items) => {
334 items: items.clone(),
335 generics: gen.clone(),
336 bounds: b.iter().cloned().collect(),
338 attrs: item.attrs.clone(),
341 stab: self.stability(item.id),
345 ast::ItemImpl(unsafety, polarity, ref gen, ref tr, ref ty, ref items) => {
349 generics: gen.clone(),
352 items: items.clone(),
353 attrs: item.attrs.clone(),
357 stab: self.stability(item.id),
361 ast::ItemForeignMod(ref fm) => {
362 om.foreigns.push(fm.clone());
365 panic!("rustdoc: macros should be gone, after expansion");
370 // convert each exported_macro into a doc item
371 fn visit_macro(&self, def: &ast::MacroDef) -> Macro {
374 attrs: def.attrs.clone(),
377 stab: self.stability(def.id),