]> git.proxmox.com Git - rustc.git/blob - src/librustdoc/visit_ast.rs
Imported Upstream version 1.6.0+dfsg1
[rustc.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 std::collections::HashSet;
15 use std::mem;
16
17 use syntax::abi;
18 use syntax::ast;
19 use syntax::attr;
20 use syntax::attr::AttrMetaMethods;
21 use syntax::codemap::Span;
22
23 use rustc::front::map as hir_map;
24 use rustc::middle::stability;
25
26 use rustc_front::hir;
27
28 use core;
29 use doctree::*;
30
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
34 // containing them?
35
36 // also, is there some reason that this doesn't use the 'visit'
37 // framework from syntax?
38
39 pub struct RustdocVisitor<'a, 'tcx: 'a> {
40 pub module: Module,
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,
46 }
47
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);
54 RustdocVisitor {
55 module: Module::new(None),
56 attrs: Vec::new(),
57 cx: cx,
58 analysis: analysis,
59 view_item_stack: stack,
60 inlining_from_glob: false,
61 }
62 }
63
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))
68 .cloned()
69 })
70 }
71
72 pub fn visit(&mut self, krate: &hir::Crate) {
73 self.attrs = krate.attrs.clone();
74
75 self.module = self.visit_mod_contents(krate.span,
76 krate.attrs.clone(),
77 hir::Public,
78 ast::CRATE_NODE_ID,
79 &krate.module,
80 None);
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;
85 }
86
87 pub fn visit_variant_data(&mut self, item: &hir::Item,
88 name: ast::Name, sd: &hir::VariantData,
89 generics: &hir::Generics) -> Struct {
90 debug!("Visiting struct");
91 let struct_type = struct_type_from_def(&*sd);
92 Struct {
93 id: item.id,
94 struct_type: struct_type,
95 name: name,
96 vis: item.vis,
97 stab: self.stability(item.id),
98 attrs: item.attrs.clone(),
99 generics: generics.clone(),
100 fields: sd.fields().iter().cloned().collect(),
101 whence: item.span
102 }
103 }
104
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");
109 Enum {
110 name: name,
111 variants: def.variants.iter().map(|v| Variant {
112 name: v.node.name,
113 attrs: v.node.attrs.clone(),
114 stab: self.stability(v.node.data.id()),
115 def: v.node.data.clone(),
116 whence: v.span,
117 }).collect(),
118 vis: it.vis,
119 stab: self.stability(it.id),
120 generics: params.clone(),
121 attrs: it.attrs.clone(),
122 id: it.id,
123 whence: it.span,
124 }
125 }
126
127 pub fn visit_fn(&mut self, item: &hir::Item,
128 name: ast::Name, fd: &hir::FnDecl,
129 unsafety: &hir::Unsafety,
130 constness: hir::Constness,
131 abi: &abi::Abi,
132 gen: &hir::Generics) -> Function {
133 debug!("Visiting fn");
134 Function {
135 id: item.id,
136 vis: item.vis,
137 stab: self.stability(item.id),
138 attrs: item.attrs.clone(),
139 decl: fd.clone(),
140 name: name,
141 whence: item.span,
142 generics: gen.clone(),
143 unsafety: *unsafety,
144 constness: constness,
145 abi: *abi,
146 }
147 }
148
149 pub fn visit_mod_contents(&mut self, span: Span, attrs: Vec<ast::Attribute> ,
150 vis: hir::Visibility, id: ast::NodeId,
151 m: &hir::Mod,
152 name: Option<ast::Name>) -> Module {
153 let mut om = Module::new(name);
154 om.where_outer = span;
155 om.where_inner = m.inner;
156 om.attrs = attrs;
157 om.vis = vis;
158 om.stab = self.stability(id);
159 om.id = id;
160 for i in &m.item_ids {
161 let item = self.cx.map.expect_item(i.id);
162 self.visit_item(item, None, &mut om);
163 }
164 om
165 }
166
167 fn visit_view_path(&mut self, path: hir::ViewPath_,
168 om: &mut Module,
169 id: ast::NodeId,
170 please_inline: bool) -> Option<hir::ViewPath_> {
171 match path {
172 hir::ViewPathSimple(dst, base) => {
173 if self.resolve_id(id, Some(dst), false, om, please_inline) {
174 None
175 } else {
176 Some(hir::ViewPathSimple(dst, base))
177 }
178 }
179 hir::ViewPathList(p, paths) => {
180 let mine = paths.into_iter().filter(|path| {
181 !self.resolve_id(path.node.id(), None, false, om,
182 please_inline)
183 }).collect::<Vec<hir::PathListItem>>();
184
185 if mine.is_empty() {
186 None
187 } else {
188 Some(hir::ViewPathList(p, mine))
189 }
190 }
191
192 // these are feature gated anyway
193 hir::ViewPathGlob(base) => {
194 if self.resolve_id(id, None, true, om, please_inline) {
195 None
196 } else {
197 Some(hir::ViewPathGlob(base))
198 }
199 }
200 }
201
202 }
203
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() {
207 Some(tcx) => tcx,
208 None => return false
209 };
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
213 };
214 let analysis = match self.analysis {
215 Some(analysis) => analysis, None => return false
216 };
217 if !please_inline && analysis.access_levels.is_public(def) {
218 return false
219 }
220 if !self.view_item_stack.insert(def_node_id) { return false }
221
222 let ret = match tcx.map.get(def_node_id) {
223 hir_map::NodeItem(it) => {
224 if glob {
225 let prev = mem::replace(&mut self.inlining_from_glob, true);
226 match it.node {
227 hir::ItemMod(ref m) => {
228 for i in &m.item_ids {
229 let i = self.cx.map.expect_item(i.id);
230 self.visit_item(i, None, om);
231 }
232 }
233 hir::ItemEnum(..) => {}
234 _ => { panic!("glob not mapped to a module or enum"); }
235 }
236 self.inlining_from_glob = prev;
237 } else {
238 self.visit_item(it, renamed, om);
239 }
240 true
241 }
242 _ => false,
243 };
244 self.view_item_stack.remove(&def_node_id);
245 return ret;
246 }
247
248 pub fn visit_item(&mut self, item: &hir::Item,
249 renamed: Option<ast::Name>, om: &mut Module) {
250 debug!("Visiting item {:?}", item);
251 let name = renamed.unwrap_or(item.name);
252 match item.node {
253 hir::ItemExternCrate(ref p) => {
254 let path = match *p {
255 None => None,
256 Some(x) => Some(x.to_string()),
257 };
258 om.extern_crates.push(ExternCrate {
259 name: name,
260 path: path,
261 vis: item.vis,
262 attrs: item.attrs.clone(),
263 whence: item.span,
264 })
265 }
266 hir::ItemUse(ref vpath) => {
267 let node = vpath.node.clone();
268 let node = if item.vis == hir::Public {
269 let please_inline = item.attrs.iter().any(|item| {
270 match item.meta_item_list() {
271 Some(list) => {
272 list.iter().any(|i| &i.name()[..] == "inline")
273 }
274 None => false,
275 }
276 });
277 match self.visit_view_path(node, om, item.id, please_inline) {
278 None => return,
279 Some(p) => p
280 }
281 } else {
282 node
283 };
284 om.imports.push(Import {
285 id: item.id,
286 vis: item.vis,
287 attrs: item.attrs.clone(),
288 node: node,
289 whence: item.span,
290 });
291 }
292 hir::ItemMod(ref m) => {
293 om.mods.push(self.visit_mod_contents(item.span,
294 item.attrs.clone(),
295 item.vis,
296 item.id,
297 m,
298 Some(name)));
299 },
300 hir::ItemEnum(ref ed, ref gen) =>
301 om.enums.push(self.visit_enum_def(item, name, ed, gen)),
302 hir::ItemStruct(ref sd, ref gen) =>
303 om.structs.push(self.visit_variant_data(item, name, sd, gen)),
304 hir::ItemFn(ref fd, ref unsafety, constness, ref abi, ref gen, _) =>
305 om.fns.push(self.visit_fn(item, name, &**fd, unsafety,
306 constness, abi, gen)),
307 hir::ItemTy(ref ty, ref gen) => {
308 let t = Typedef {
309 ty: ty.clone(),
310 gen: gen.clone(),
311 name: name,
312 id: item.id,
313 attrs: item.attrs.clone(),
314 whence: item.span,
315 vis: item.vis,
316 stab: self.stability(item.id),
317 };
318 om.typedefs.push(t);
319 },
320 hir::ItemStatic(ref ty, ref mut_, ref exp) => {
321 let s = Static {
322 type_: ty.clone(),
323 mutability: mut_.clone(),
324 expr: exp.clone(),
325 id: item.id,
326 name: name,
327 attrs: item.attrs.clone(),
328 whence: item.span,
329 vis: item.vis,
330 stab: self.stability(item.id),
331 };
332 om.statics.push(s);
333 },
334 hir::ItemConst(ref ty, ref exp) => {
335 let s = Constant {
336 type_: ty.clone(),
337 expr: exp.clone(),
338 id: item.id,
339 name: name,
340 attrs: item.attrs.clone(),
341 whence: item.span,
342 vis: item.vis,
343 stab: self.stability(item.id),
344 };
345 om.constants.push(s);
346 },
347 hir::ItemTrait(unsafety, ref gen, ref b, ref items) => {
348 let t = Trait {
349 unsafety: unsafety,
350 name: name,
351 items: items.clone(),
352 generics: gen.clone(),
353 bounds: b.iter().cloned().collect(),
354 id: item.id,
355 attrs: item.attrs.clone(),
356 whence: item.span,
357 vis: item.vis,
358 stab: self.stability(item.id),
359 };
360 om.traits.push(t);
361 },
362 hir::ItemImpl(unsafety, polarity, ref gen, ref tr, ref ty, ref items) => {
363 let i = Impl {
364 unsafety: unsafety,
365 polarity: polarity,
366 generics: gen.clone(),
367 trait_: tr.clone(),
368 for_: ty.clone(),
369 items: items.clone(),
370 attrs: item.attrs.clone(),
371 id: item.id,
372 whence: item.span,
373 vis: item.vis,
374 stab: self.stability(item.id),
375 };
376 // Don't duplicate impls when inlining glob imports, we'll pick
377 // them up regardless of where they're located.
378 if !self.inlining_from_glob {
379 om.impls.push(i);
380 }
381 },
382 hir::ItemDefaultImpl(unsafety, ref trait_ref) => {
383 let i = DefaultImpl {
384 unsafety: unsafety,
385 trait_: trait_ref.clone(),
386 id: item.id,
387 attrs: item.attrs.clone(),
388 whence: item.span,
389 };
390 // see comment above about ItemImpl
391 if !self.inlining_from_glob {
392 om.def_traits.push(i);
393 }
394 }
395 hir::ItemForeignMod(ref fm) => {
396 om.foreigns.push(fm.clone());
397 }
398 }
399 }
400
401 // convert each exported_macro into a doc item
402 fn visit_macro(&self, def: &hir::MacroDef) -> Macro {
403 // Extract the spans of all matchers. They represent the "interface" of the macro.
404 let matchers = def.body.chunks(4).map(|arm| arm[0].get_span()).collect();
405
406 Macro {
407 id: def.id,
408 attrs: def.attrs.clone(),
409 name: def.name,
410 whence: def.span,
411 matchers: matchers,
412 stab: self.stability(def.id),
413 imported_from: def.imported_from,
414 }
415 }
416 }