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
18 use syntax::attr::AttrMetaMethods;
19 use syntax::codemap::Span;
21 use std::gc::{Gc, GC};
26 pub struct RustdocVisitor<'a> {
28 pub attrs: Vec<ast::Attribute>,
29 pub cx: &'a core::DocContext,
30 pub analysis: Option<&'a core::CrateAnalysis>,
33 impl<'a> RustdocVisitor<'a> {
34 pub fn new<'b>(cx: &'b core::DocContext,
35 analysis: Option<&'b core::CrateAnalysis>) -> RustdocVisitor<'b> {
37 module: Module::new(None),
44 pub fn visit(&mut self, krate: &ast::Crate) {
45 self.attrs = krate.attrs.iter().map(|x| (*x).clone()).collect();
47 self.module = self.visit_mod_contents(krate.span,
56 self.module.is_crate = true;
59 pub fn visit_struct_def(&mut self, item: &ast::Item, sd: Gc<ast::StructDef>,
60 generics: &ast::Generics) -> Struct {
61 debug!("Visiting struct");
62 let struct_type = struct_type_from_def(&*sd);
65 struct_type: struct_type,
68 attrs: item.attrs.iter().map(|x| *x).collect(),
69 generics: generics.clone(),
70 fields: sd.fields.iter().map(|x| (*x).clone()).collect(),
75 pub fn visit_enum_def(&mut self, it: &ast::Item, def: &ast::EnumDef,
76 params: &ast::Generics) -> Enum {
77 debug!("Visiting enum");
78 let mut vars: Vec<Variant> = Vec::new();
79 for x in def.variants.iter() {
82 attrs: x.node.attrs.iter().map(|x| *x).collect(),
85 kind: x.node.kind.clone(),
93 generics: params.clone(),
94 attrs: it.attrs.iter().map(|x| *x).collect(),
100 pub fn visit_fn(&mut self, item: &ast::Item, fd: &ast::FnDecl,
101 fn_style: &ast::FnStyle, _abi: &abi::Abi,
102 gen: &ast::Generics) -> Function {
103 debug!("Visiting fn");
107 attrs: item.attrs.iter().map(|x| *x).collect(),
111 generics: gen.clone(),
116 pub fn visit_mod_contents(&mut self, span: Span, attrs: Vec<ast::Attribute> ,
117 vis: ast::Visibility, id: ast::NodeId,
119 name: Option<ast::Ident>) -> Module {
120 let mut om = Module::new(name);
121 for item in m.view_items.iter() {
122 self.visit_view_item(item, &mut om);
124 om.where_outer = span;
125 om.where_inner = m.inner;
129 for i in m.items.iter() {
130 self.visit_item(&**i, &mut om);
135 pub fn visit_view_item(&mut self, item: &ast::ViewItem, om: &mut Module) {
136 if item.vis != ast::Public {
137 return om.view_items.push(item.clone());
139 let please_inline = item.attrs.iter().any(|item| {
140 match item.meta_item_list() {
142 list.iter().any(|i| i.name().get() == "inline")
147 let item = match item.node {
148 ast::ViewItemUse(ref vpath) => {
149 match self.visit_view_path(*vpath, om, please_inline) {
153 node: ast::ViewItemUse(path),
159 ast::ViewItemExternCrate(..) => item.clone()
161 om.view_items.push(item);
164 fn visit_view_path(&mut self, path: Gc<ast::ViewPath>,
166 please_inline: bool) -> Option<Gc<ast::ViewPath>> {
168 ast::ViewPathSimple(_, _, id) => {
169 if self.resolve_id(id, false, om, please_inline) { return None }
171 ast::ViewPathList(ref p, ref paths, ref b) => {
172 let mut mine = Vec::new();
173 for path in paths.iter() {
174 if !self.resolve_id(path.node.id, false, om, please_inline) {
175 mine.push(path.clone());
179 if mine.len() == 0 { return None }
180 return Some(box(GC) ::syntax::codemap::Spanned {
181 node: ast::ViewPathList(p.clone(), mine, b.clone()),
186 // these are feature gated anyway
187 ast::ViewPathGlob(_, id) => {
188 if self.resolve_id(id, true, om, please_inline) { return None }
194 fn resolve_id(&mut self, id: ast::NodeId, glob: bool,
195 om: &mut Module, please_inline: bool) -> bool {
196 let tcx = match self.cx.maybe_typed {
197 core::Typed(ref tcx) => tcx,
198 core::NotTyped(_) => return false
200 let def = tcx.def_map.borrow().get(&id).def_id();
201 if !ast_util::is_local(def) { return false }
202 let analysis = match self.analysis {
203 Some(analysis) => analysis, None => return false
205 if !please_inline && analysis.public_items.contains(&def.node) {
209 match tcx.map.get(def.node) {
210 ast_map::NodeItem(it) => {
213 ast::ItemMod(ref m) => {
214 for vi in m.view_items.iter() {
215 self.visit_view_item(vi, om);
217 for i in m.items.iter() {
218 self.visit_item(&**i, om);
221 _ => { fail!("glob not mapped to a module"); }
224 self.visit_item(&*it, om);
232 pub fn visit_item(&mut self, item: &ast::Item, om: &mut Module) {
233 debug!("Visiting item {:?}", item);
235 ast::ItemMod(ref m) => {
236 om.mods.push(self.visit_mod_contents(item.span,
246 ast::ItemEnum(ref ed, ref gen) =>
247 om.enums.push(self.visit_enum_def(item, ed, gen)),
248 ast::ItemStruct(sd, ref gen) =>
249 om.structs.push(self.visit_struct_def(item, sd, gen)),
250 ast::ItemFn(ref fd, ref pur, ref abi, ref gen, _) =>
251 om.fns.push(self.visit_fn(item, &**fd, pur, abi, gen)),
252 ast::ItemTy(ty, ref gen) => {
258 attrs: item.attrs.iter().map(|x| *x).collect(),
264 ast::ItemStatic(ty, ref mut_, ref exp) => {
267 mutability: mut_.clone(),
271 attrs: item.attrs.iter().map(|x| *x).collect(),
277 ast::ItemTrait(ref gen, _, ref tr, ref met) => {
280 methods: met.iter().map(|x| (*x).clone()).collect(),
281 generics: gen.clone(),
282 parents: tr.iter().map(|x| (*x).clone()).collect(),
284 attrs: item.attrs.iter().map(|x| *x).collect(),
290 ast::ItemImpl(ref gen, ref tr, ty, ref meths) => {
292 generics: gen.clone(),
295 methods: meths.iter().map(|x| *x).collect(),
296 attrs: item.attrs.iter().map(|x| *x).collect(),
303 ast::ItemForeignMod(ref fm) => {
304 om.foreigns.push(fm.clone());
306 ast::ItemMac(ref _m) => {
307 om.macros.push(Macro {
309 attrs: item.attrs.iter().map(|x| *x).collect(),