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 syntax::abi::AbiSet;
18 use syntax::codemap::Span;
23 pub struct RustdocVisitor<'a> {
25 attrs: ~[ast::Attribute],
26 cx: &'a core::DocContext,
27 analysis: Option<&'a core::CrateAnalysis>,
30 impl<'a> RustdocVisitor<'a> {
31 pub fn new<'b>(cx: &'b core::DocContext,
32 analysis: Option<&'b core::CrateAnalysis>) -> RustdocVisitor<'b> {
34 module: Module::new(None),
41 pub fn visit(&mut self, krate: &ast::Crate) {
42 self.attrs = krate.attrs.iter().map(|x| (*x).clone()).collect();
44 self.module = self.visit_mod_contents(krate.span,
53 self.module.is_crate = true;
56 pub fn visit_struct_def(&mut self, item: &ast::Item, sd: @ast::StructDef,
57 generics: &ast::Generics) -> Struct {
58 debug!("Visiting struct");
59 let struct_type = struct_type_from_def(sd);
62 struct_type: struct_type,
65 attrs: item.attrs.iter().map(|x| *x).collect(),
66 generics: generics.clone(),
67 fields: sd.fields.iter().map(|x| (*x).clone()).collect(),
72 pub fn visit_enum_def(&mut self, it: &ast::Item, def: &ast::EnumDef,
73 params: &ast::Generics) -> Enum {
74 debug!("Visiting enum");
75 let mut vars: ~[Variant] = ~[];
76 for x in def.variants.iter() {
79 attrs: x.node.attrs.iter().map(|x| *x).collect(),
82 kind: x.node.kind.clone(),
90 generics: params.clone(),
91 attrs: it.attrs.iter().map(|x| *x).collect(),
97 pub fn visit_fn(&mut self, item: &ast::Item, fd: &ast::FnDecl,
98 purity: &ast::Purity, _abi: &AbiSet,
99 gen: &ast::Generics) -> Function {
100 debug!("Visiting fn");
104 attrs: item.attrs.iter().map(|x| *x).collect(),
108 generics: gen.clone(),
113 pub fn visit_mod_contents(&mut self, span: Span, attrs: ~[ast::Attribute],
114 vis: ast::Visibility, id: ast::NodeId,
116 name: Option<ast::Ident>) -> Module {
117 let mut om = Module::new(name);
118 for item in m.view_items.iter() {
119 self.visit_view_item(item, &mut om);
125 for i in m.items.iter() {
126 self.visit_item(*i, &mut om);
131 pub fn visit_view_item(&mut self, item: &ast::ViewItem, om: &mut Module) {
132 if item.vis != ast::Public {
133 return om.view_items.push(item.clone());
135 let item = match item.node {
136 ast::ViewItemUse(ref paths) => {
137 // rustc no longer supports "use foo, bar;"
138 assert_eq!(paths.len(), 1);
139 match self.visit_view_path(*paths.get(0), om) {
143 node: ast::ViewItemUse(vec!(path)),
149 ast::ViewItemExternCrate(..) => item.clone()
151 om.view_items.push(item);
154 fn visit_view_path(&mut self, path: @ast::ViewPath,
155 om: &mut Module) -> Option<@ast::ViewPath> {
157 ast::ViewPathSimple(_, _, id) => {
158 if self.resolve_id(id, false, om) { return None }
160 ast::ViewPathList(ref p, ref paths, ref b) => {
161 let mut mine = Vec::new();
162 for path in paths.iter() {
163 if !self.resolve_id(path.node.id, false, om) {
164 mine.push(path.clone());
168 if mine.len() == 0 { return None }
169 return Some(@::syntax::codemap::Spanned {
170 node: ast::ViewPathList(p.clone(), mine, b.clone()),
175 // these are feature gated anyway
176 ast::ViewPathGlob(_, id) => {
177 if self.resolve_id(id, true, om) { return None }
183 fn resolve_id(&mut self, id: ast::NodeId, glob: bool,
184 om: &mut Module) -> bool {
185 let tcx = match self.cx.maybe_typed {
186 core::Typed(ref tcx) => tcx,
187 core::NotTyped(_) => return false
189 let def = ast_util::def_id_of_def(*tcx.def_map.borrow().get().get(&id));
190 if !ast_util::is_local(def) { return false }
191 let analysis = match self.analysis {
192 Some(analysis) => analysis, None => return false
194 if analysis.public_items.contains(&def.node) { return false }
196 match tcx.map.get(def.node) {
197 ast_map::NodeItem(it) => {
200 ast::ItemMod(ref m) => {
201 for vi in m.view_items.iter() {
202 self.visit_view_item(vi, om);
204 for i in m.items.iter() {
205 self.visit_item(*i, om);
208 _ => { fail!("glob not mapped to a module"); }
211 self.visit_item(it, om);
219 pub fn visit_item(&mut self, item: &ast::Item, om: &mut Module) {
220 debug!("Visiting item {:?}", item);
222 ast::ItemMod(ref m) => {
223 om.mods.push(self.visit_mod_contents(item.span,
233 ast::ItemEnum(ref ed, ref gen) =>
234 om.enums.push(self.visit_enum_def(item, ed, gen)),
235 ast::ItemStruct(sd, ref gen) =>
236 om.structs.push(self.visit_struct_def(item, sd, gen)),
237 ast::ItemFn(fd, ref pur, ref abi, ref gen, _) =>
238 om.fns.push(self.visit_fn(item, fd, pur, abi, gen)),
239 ast::ItemTy(ty, ref gen) => {
245 attrs: item.attrs.iter().map(|x| *x).collect(),
251 ast::ItemStatic(ty, ref mut_, ref exp) => {
254 mutability: mut_.clone(),
258 attrs: item.attrs.iter().map(|x| *x).collect(),
264 ast::ItemTrait(ref gen, ref tr, ref met) => {
267 methods: met.iter().map(|x| (*x).clone()).collect(),
268 generics: gen.clone(),
269 parents: tr.iter().map(|x| (*x).clone()).collect(),
271 attrs: item.attrs.iter().map(|x| *x).collect(),
277 ast::ItemImpl(ref gen, ref tr, ty, ref meths) => {
279 generics: gen.clone(),
282 methods: meths.iter().map(|x| *x).collect(),
283 attrs: item.attrs.iter().map(|x| *x).collect(),
290 ast::ItemForeignMod(ref fm) => {
291 om.foreigns.push(fm.clone());
293 ast::ItemMac(ref _m) => {
294 om.macros.push(Macro {
296 attrs: item.attrs.iter().map(|x| *x).collect(),