]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/visit_ast.rs
test: Make manual changes to deal with the fallout from removal of
[rust.git] / src / librustdoc / visit_ast.rs
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.
4 //
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.
10
11 //! Rust AST Visitor. Extracts useful information and massages it into a form
12 //! usable for clean
13
14 use syntax::abi::AbiSet;
15 use syntax::ast;
16 use syntax::ast_util;
17 use syntax::ast_map;
18 use syntax::codemap::Span;
19
20 use core;
21 use doctree::*;
22
23 pub struct RustdocVisitor<'a> {
24     module: Module,
25     attrs: Vec<ast::Attribute> ,
26     cx: &'a core::DocContext,
27     analysis: Option<&'a core::CrateAnalysis>,
28 }
29
30 impl<'a> RustdocVisitor<'a> {
31     pub fn new<'b>(cx: &'b core::DocContext,
32                    analysis: Option<&'b core::CrateAnalysis>) -> RustdocVisitor<'b> {
33         RustdocVisitor {
34             module: Module::new(None),
35             attrs: Vec::new(),
36             cx: cx,
37             analysis: analysis,
38         }
39     }
40
41     pub fn visit(&mut self, krate: &ast::Crate) {
42         self.attrs = krate.attrs.iter().map(|x| (*x).clone()).collect();
43
44         self.module = self.visit_mod_contents(krate.span,
45                                               krate.attrs
46                                                    .iter()
47                                                    .map(|x| *x)
48                                                    .collect(),
49                                               ast::Public,
50                                               ast::CRATE_NODE_ID,
51                                               &krate.module,
52                                               None);
53         self.module.is_crate = true;
54     }
55
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);
60         Struct {
61             id: item.id,
62             struct_type: struct_type,
63             name: item.ident,
64             vis: item.vis,
65             attrs: item.attrs.iter().map(|x| *x).collect(),
66             generics: generics.clone(),
67             fields: sd.fields.iter().map(|x| (*x).clone()).collect(),
68             where: item.span
69         }
70     }
71
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: Vec<Variant> = Vec::new();
76         for x in def.variants.iter() {
77             vars.push(Variant {
78                 name: x.node.name,
79                 attrs: x.node.attrs.iter().map(|x| *x).collect(),
80                 vis: x.node.vis,
81                 id: x.node.id,
82                 kind: x.node.kind.clone(),
83                 where: x.span,
84             });
85         }
86         Enum {
87             name: it.ident,
88             variants: vars,
89             vis: it.vis,
90             generics: params.clone(),
91             attrs: it.attrs.iter().map(|x| *x).collect(),
92             id: it.id,
93             where: it.span,
94         }
95     }
96
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");
101         Function {
102             id: item.id,
103             vis: item.vis,
104             attrs: item.attrs.iter().map(|x| *x).collect(),
105             decl: fd.clone(),
106             name: item.ident,
107             where: item.span,
108             generics: gen.clone(),
109             purity: *purity,
110         }
111     }
112
113     pub fn visit_mod_contents(&mut self, span: Span, attrs: Vec<ast::Attribute> ,
114                               vis: ast::Visibility, id: ast::NodeId,
115                               m: &ast::Mod,
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);
120         }
121         om.where = span;
122         om.attrs = attrs;
123         om.vis = vis;
124         om.id = id;
125         for i in m.items.iter() {
126             self.visit_item(*i, &mut om);
127         }
128         om
129     }
130
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());
134         }
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) {
140                     None => return,
141                     Some(path) => {
142                         ast::ViewItem {
143                             node: ast::ViewItemUse(vec!(path)),
144                             .. item.clone()
145                         }
146                     }
147                 }
148             }
149             ast::ViewItemExternCrate(..) => item.clone()
150         };
151         om.view_items.push(item);
152     }
153
154     fn visit_view_path(&mut self, path: @ast::ViewPath,
155                        om: &mut Module) -> Option<@ast::ViewPath> {
156         match path.node {
157             ast::ViewPathSimple(_, _, id) => {
158                 if self.resolve_id(id, false, om) { return None }
159             }
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());
165                     }
166                 }
167
168                 if mine.len() == 0 { return None }
169                 return Some(@::syntax::codemap::Spanned {
170                     node: ast::ViewPathList(p.clone(), mine, b.clone()),
171                     span: path.span,
172                 })
173             }
174
175             // these are feature gated anyway
176             ast::ViewPathGlob(_, id) => {
177                 if self.resolve_id(id, true, om) { return None }
178             }
179         }
180         return Some(path);
181     }
182
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
188         };
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
193         };
194         if analysis.public_items.contains(&def.node) { return false }
195
196         match tcx.map.get(def.node) {
197             ast_map::NodeItem(it) => {
198                 if glob {
199                     match it.node {
200                         ast::ItemMod(ref m) => {
201                             for vi in m.view_items.iter() {
202                                 self.visit_view_item(vi, om);
203                             }
204                             for i in m.items.iter() {
205                                 self.visit_item(*i, om);
206                             }
207                         }
208                         _ => { fail!("glob not mapped to a module"); }
209                     }
210                 } else {
211                     self.visit_item(it, om);
212                 }
213                 true
214             }
215             _ => false,
216         }
217     }
218
219     pub fn visit_item(&mut self, item: &ast::Item, om: &mut Module) {
220         debug!("Visiting item {:?}", item);
221         match item.node {
222             ast::ItemMod(ref m) => {
223                 om.mods.push(self.visit_mod_contents(item.span,
224                                                      item.attrs
225                                                          .iter()
226                                                          .map(|x| *x)
227                                                          .collect(),
228                                                      item.vis,
229                                                      item.id,
230                                                      m,
231                                                      Some(item.ident)));
232             },
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) => {
240                 let t = Typedef {
241                     ty: ty,
242                     gen: gen.clone(),
243                     name: item.ident,
244                     id: item.id,
245                     attrs: item.attrs.iter().map(|x| *x).collect(),
246                     where: item.span,
247                     vis: item.vis,
248                 };
249                 om.typedefs.push(t);
250             },
251             ast::ItemStatic(ty, ref mut_, ref exp) => {
252                 let s = Static {
253                     type_: ty,
254                     mutability: mut_.clone(),
255                     expr: exp.clone(),
256                     id: item.id,
257                     name: item.ident,
258                     attrs: item.attrs.iter().map(|x| *x).collect(),
259                     where: item.span,
260                     vis: item.vis,
261                 };
262                 om.statics.push(s);
263             },
264             ast::ItemTrait(ref gen, ref tr, ref met) => {
265                 let t = Trait {
266                     name: item.ident,
267                     methods: met.iter().map(|x| (*x).clone()).collect(),
268                     generics: gen.clone(),
269                     parents: tr.iter().map(|x| (*x).clone()).collect(),
270                     id: item.id,
271                     attrs: item.attrs.iter().map(|x| *x).collect(),
272                     where: item.span,
273                     vis: item.vis,
274                 };
275                 om.traits.push(t);
276             },
277             ast::ItemImpl(ref gen, ref tr, ty, ref meths) => {
278                 let i = Impl {
279                     generics: gen.clone(),
280                     trait_: tr.clone(),
281                     for_: ty,
282                     methods: meths.iter().map(|x| *x).collect(),
283                     attrs: item.attrs.iter().map(|x| *x).collect(),
284                     id: item.id,
285                     where: item.span,
286                     vis: item.vis,
287                 };
288                 om.impls.push(i);
289             },
290             ast::ItemForeignMod(ref fm) => {
291                 om.foreigns.push(fm.clone());
292             }
293             ast::ItemMac(ref _m) => {
294                 om.macros.push(Macro {
295                     id: item.id,
296                     attrs: item.attrs.iter().map(|x| *x).collect(),
297                     name: item.ident,
298                     where: item.span,
299                 })
300             }
301         }
302     }
303 }