1 //! The Rust AST Visitor. Extracts useful information and massages it into a form
2 //! usable for `clean`.
4 use rustc::hir::{self, Node};
5 use rustc::hir::def::Def;
6 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
7 use rustc::middle::privacy::AccessLevel;
8 use rustc::util::nodemap::{FxHashSet, FxHashMap};
11 use syntax::ext::base::MacroKind;
12 use syntax::source_map::Spanned;
13 use syntax_pos::{self, Span};
18 use crate::clean::{self, AttributesExt, NestedAttributesExt, def_id_to_path};
19 use crate::doctree::*;
22 // Looks to me like the first two of these are actually
23 // output parameters, maybe only mutated once; perhaps
24 // better simply to have the visit method return a tuple
27 // Also, is there some reason that this doesn't use the 'visit'
28 // framework from syntax?.
30 pub struct RustdocVisitor<'a, 'tcx> {
32 pub attrs: hir::HirVec<ast::Attribute>,
33 pub cx: &'a core::DocContext<'tcx>,
34 view_item_stack: FxHashSet<ast::NodeId>,
36 /// Are the current module and all of its parents public?
37 inside_public_path: bool,
38 exact_paths: Option<FxHashMap<DefId, Vec<String>>>,
41 impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
43 cx: &'a core::DocContext<'tcx>
44 ) -> RustdocVisitor<'a, 'tcx> {
45 // If the root is re-exported, terminate all recursion.
46 let mut stack = FxHashSet::default();
47 stack.insert(ast::CRATE_NODE_ID);
49 module: Module::new(None),
50 attrs: hir::HirVec::new(),
52 view_item_stack: stack,
54 inside_public_path: true,
55 exact_paths: Some(FxHashMap::default()),
59 fn store_path(&mut self, did: DefId) {
60 // We can't use the entry API, as that keeps the mutable borrow of `self` active
61 // when we try to use `cx`.
62 let exact_paths = self.exact_paths.as_mut().unwrap();
63 if exact_paths.get(&did).is_none() {
64 let path = def_id_to_path(self.cx, did, self.cx.crate_name.clone());
65 exact_paths.insert(did, path);
69 fn stability(&self, id: hir::HirId) -> Option<attr::Stability> {
70 self.cx.tcx.hir().opt_local_def_id_from_hir_id(id)
71 .and_then(|def_id| self.cx.tcx.lookup_stability(def_id)).cloned()
74 fn deprecation(&self, id: hir::HirId) -> Option<attr::Deprecation> {
75 self.cx.tcx.hir().opt_local_def_id_from_hir_id(id)
76 .and_then(|def_id| self.cx.tcx.lookup_deprecation(def_id))
79 pub fn visit(&mut self, krate: &hir::Crate) {
80 self.attrs = krate.attrs.clone();
82 self.module = self.visit_mod_contents(krate.span,
84 Spanned { span: syntax_pos::DUMMY_SP,
85 node: hir::VisibilityKind::Public },
89 // Attach the crate's exported macros to the top-level module:
90 let macro_exports: Vec<_> =
91 krate.exported_macros.iter().map(|def| self.visit_local_macro(def, None)).collect();
92 self.module.macros.extend(macro_exports);
93 self.module.is_crate = true;
95 self.cx.renderinfo.borrow_mut().exact_paths = self.exact_paths.take().unwrap();
98 pub fn visit_variant_data(&mut self, item: &hir::Item,
99 name: ast::Name, sd: &hir::VariantData,
100 generics: &hir::Generics) -> Struct {
101 debug!("Visiting struct");
102 let struct_type = struct_type_from_def(&*sd);
107 vis: item.vis.clone(),
108 stab: self.stability(item.hir_id),
109 depr: self.deprecation(item.hir_id),
110 attrs: item.attrs.clone(),
111 generics: generics.clone(),
112 fields: sd.fields().iter().cloned().collect(),
117 pub fn visit_union_data(&mut self, item: &hir::Item,
118 name: ast::Name, sd: &hir::VariantData,
119 generics: &hir::Generics) -> Union {
120 debug!("Visiting union");
121 let struct_type = struct_type_from_def(&*sd);
126 vis: item.vis.clone(),
127 stab: self.stability(item.hir_id),
128 depr: self.deprecation(item.hir_id),
129 attrs: item.attrs.clone(),
130 generics: generics.clone(),
131 fields: sd.fields().iter().cloned().collect(),
136 pub fn visit_enum_def(&mut self, it: &hir::Item,
137 name: ast::Name, def: &hir::EnumDef,
138 params: &hir::Generics) -> Enum {
139 debug!("Visiting enum");
142 variants: def.variants.iter().map(|v| Variant {
143 name: v.node.ident.name,
144 attrs: v.node.attrs.clone(),
145 stab: self.stability(v.node.data.hir_id()),
146 depr: self.deprecation(v.node.data.hir_id()),
147 def: v.node.data.clone(),
151 stab: self.stability(it.hir_id),
152 depr: self.deprecation(it.hir_id),
153 generics: params.clone(),
154 attrs: it.attrs.clone(),
160 pub fn visit_fn(&mut self, om: &mut Module, item: &hir::Item,
161 name: ast::Name, fd: &hir::FnDecl,
162 header: hir::FnHeader,
165 debug!("Visiting fn");
166 let macro_kind = item.attrs.iter().filter_map(|a| {
167 if a.check_name("proc_macro") {
168 Some(MacroKind::Bang)
169 } else if a.check_name("proc_macro_derive") {
170 Some(MacroKind::Derive)
171 } else if a.check_name("proc_macro_attribute") {
172 Some(MacroKind::Attr)
179 let name = if kind == MacroKind::Derive {
180 item.attrs.lists("proc_macro_derive")
181 .filter_map(|mi| mi.ident())
183 .expect("proc-macro derives require a name")
189 let mut helpers = Vec::new();
190 for mi in item.attrs.lists("proc_macro_derive") {
191 if !mi.check_name("attributes") {
195 if let Some(list) = mi.meta_item_list() {
196 for inner_mi in list {
197 if let Some(ident) = inner_mi.ident() {
198 helpers.push(ident.name);
204 om.proc_macros.push(ProcMacro {
209 attrs: item.attrs.clone(),
211 stab: self.stability(item.hir_id),
212 depr: self.deprecation(item.hir_id),
216 om.fns.push(Function {
218 vis: item.vis.clone(),
219 stab: self.stability(item.hir_id),
220 depr: self.deprecation(item.hir_id),
221 attrs: item.attrs.clone(),
225 generics: gen.clone(),
233 pub fn visit_mod_contents(&mut self, span: Span, attrs: hir::HirVec<ast::Attribute>,
234 vis: hir::Visibility, id: hir::HirId,
236 name: Option<ast::Name>) -> Module {
237 let mut om = Module::new(name);
238 om.where_outer = span;
239 om.where_inner = m.inner;
241 om.vis = vis.clone();
242 om.stab = self.stability(id);
243 om.depr = self.deprecation(id);
244 om.id = self.cx.tcx.hir().hir_to_node_id(id);
245 // Keep track of if there were any private modules in the path.
246 let orig_inside_public_path = self.inside_public_path;
247 self.inside_public_path &= vis.node.is_pub();
248 for i in &m.item_ids {
249 let item = self.cx.tcx.hir().expect_item(i.id);
250 self.visit_item(item, None, &mut om);
252 self.inside_public_path = orig_inside_public_path;
256 /// Tries to resolve the target of a `pub use` statement and inlines the
257 /// target if it is defined locally and would not be documented otherwise,
258 /// or when it is specifically requested with `please_inline`.
259 /// (the latter is the case when the import is marked `doc(inline)`)
261 /// Cross-crate inlining occurs later on during crate cleaning
262 /// and follows different rules.
264 /// Returns `true` if the target has been inlined.
265 fn maybe_inline_local(&mut self,
268 renamed: Option<ast::Ident>,
271 please_inline: bool) -> bool {
273 fn inherits_doc_hidden(cx: &core::DocContext<'_>, mut node: ast::NodeId) -> bool {
274 while let Some(id) = cx.tcx.hir().get_enclosing_scope(node) {
276 if cx.tcx.hir().attrs(node).lists("doc").has_word("hidden") {
279 if node == ast::CRATE_NODE_ID {
286 debug!("maybe_inline_local def: {:?}", def);
288 let tcx = self.cx.tcx;
289 let def_did = if let Some(did) = def.opt_def_id() {
295 let use_attrs = tcx.hir().attrs_by_hir_id(id);
296 // Don't inline `doc(hidden)` imports so they can be stripped at a later stage.
297 let is_no_inline = use_attrs.lists("doc").has_word("no_inline") ||
298 use_attrs.lists("doc").has_word("hidden");
300 // For cross-crate impl inlining we need to know whether items are
301 // reachable in documentation -- a previously nonreachable item can be
302 // made reachable by cross-crate inlining which we're checking here.
303 // (this is done here because we need to know this upfront).
304 if !def_did.is_local() && !is_no_inline {
305 let attrs = clean::inline::load_attrs(self.cx, def_did);
306 let self_is_hidden = attrs.lists("doc").has_word("hidden");
312 Def::ForeignTy(did) |
313 Def::TyAlias(did) if !self_is_hidden => {
317 .insert(did, AccessLevel::Public);
319 Def::Mod(did) => if !self_is_hidden {
320 crate::visit_lib::LibEmbargoVisitor::new(self.cx).visit_mod(did);
328 let def_node_id = match tcx.hir().as_local_node_id(def_did) {
329 Some(n) => n, None => return false
332 let is_private = !self.cx.renderinfo.borrow().access_levels.is_public(def_did);
333 let is_hidden = inherits_doc_hidden(self.cx, def_node_id);
335 // Only inline if requested or if the item would otherwise be stripped.
336 if (!please_inline && !is_private && !is_hidden) || is_no_inline {
340 if !self.view_item_stack.insert(def_node_id) { return false }
342 let ret = match tcx.hir().get(def_node_id) {
343 Node::Item(&hir::Item { node: hir::ItemKind::Mod(ref m), .. }) if glob => {
344 let prev = mem::replace(&mut self.inlining, true);
345 for i in &m.item_ids {
346 let i = self.cx.tcx.hir().expect_item(i.id);
347 self.visit_item(i, None, om);
349 self.inlining = prev;
352 Node::Item(it) if !glob => {
353 let prev = mem::replace(&mut self.inlining, true);
354 self.visit_item(it, renamed, om);
355 self.inlining = prev;
358 Node::ForeignItem(it) if !glob => {
359 // Generate a fresh `extern {}` block if we want to inline a foreign item.
360 om.foreigns.push(hir::ForeignMod {
361 abi: tcx.hir().get_foreign_abi_by_hir_id(it.hir_id),
362 items: vec![hir::ForeignItem {
363 ident: renamed.unwrap_or(it.ident),
369 Node::MacroDef(def) if !glob => {
370 om.macros.push(self.visit_local_macro(def, renamed.map(|i| i.name)));
375 self.view_item_stack.remove(&def_node_id);
379 pub fn visit_item(&mut self, item: &hir::Item,
380 renamed: Option<ast::Ident>, om: &mut Module) {
381 debug!("Visiting item {:?}", item);
382 let ident = renamed.unwrap_or(item.ident);
384 if item.vis.node.is_pub() {
385 let def_id = self.cx.tcx.hir().local_def_id_from_hir_id(item.hir_id);
386 self.store_path(def_id);
390 hir::ItemKind::ForeignMod(ref fm) => {
391 // If inlining we only want to include public functions.
392 om.foreigns.push(if self.inlining {
395 items: fm.items.iter().filter(|i| i.vis.node.is_pub()).cloned().collect(),
401 // If we're inlining, skip private items.
402 _ if self.inlining && !item.vis.node.is_pub() => {}
403 hir::ItemKind::GlobalAsm(..) => {}
404 hir::ItemKind::ExternCrate(orig_name) => {
405 let def_id = self.cx.tcx.hir().local_def_id_from_hir_id(item.hir_id);
406 om.extern_crates.push(ExternCrate {
407 cnum: self.cx.tcx.extern_mod_stmt_cnum(def_id)
408 .unwrap_or(LOCAL_CRATE),
410 path: orig_name.map(|x|x.to_string()),
411 vis: item.vis.clone(),
412 attrs: item.attrs.clone(),
416 hir::ItemKind::Use(_, hir::UseKind::ListStem) => {}
417 hir::ItemKind::Use(ref path, kind) => {
418 let is_glob = kind == hir::UseKind::Glob;
420 // Struct and variant constructors and proc macro stubs always show up alongside
421 // their definitions, we've already processed them so just discard these.
423 Def::StructCtor(..) | Def::VariantCtor(..) | Def::SelfCtor(..) |
424 Def::Macro(_, MacroKind::ProcMacroStub) => return,
428 // If there was a private module in the current path then don't bother inlining
429 // anything as it will probably be stripped anyway.
430 if item.vis.node.is_pub() && self.inside_public_path {
431 let please_inline = item.attrs.iter().any(|item| {
432 match item.meta_item_list() {
433 Some(ref list) if item.check_name("doc") => {
434 list.iter().any(|i| i.check_name("inline"))
439 let ident = if is_glob { None } else { Some(ident) };
440 if self.maybe_inline_local(item.hir_id,
450 om.imports.push(Import {
453 vis: item.vis.clone(),
454 attrs: item.attrs.clone(),
455 path: (**path).clone(),
460 hir::ItemKind::Mod(ref m) => {
461 om.mods.push(self.visit_mod_contents(item.span,
468 hir::ItemKind::Enum(ref ed, ref gen) =>
469 om.enums.push(self.visit_enum_def(item, ident.name, ed, gen)),
470 hir::ItemKind::Struct(ref sd, ref gen) =>
471 om.structs.push(self.visit_variant_data(item, ident.name, sd, gen)),
472 hir::ItemKind::Union(ref sd, ref gen) =>
473 om.unions.push(self.visit_union_data(item, ident.name, sd, gen)),
474 hir::ItemKind::Fn(ref fd, header, ref gen, body) =>
475 self.visit_fn(om, item, ident.name, &**fd, header, gen, body),
476 hir::ItemKind::Ty(ref ty, ref gen) => {
482 attrs: item.attrs.clone(),
484 vis: item.vis.clone(),
485 stab: self.stability(item.hir_id),
486 depr: self.deprecation(item.hir_id),
490 hir::ItemKind::Existential(ref exist_ty) => {
491 let t = Existential {
492 exist_ty: exist_ty.clone(),
495 attrs: item.attrs.clone(),
497 vis: item.vis.clone(),
498 stab: self.stability(item.hir_id),
499 depr: self.deprecation(item.hir_id),
501 om.existentials.push(t);
503 hir::ItemKind::Static(ref ty, ref mut_, ref exp) => {
506 mutability: mut_.clone(),
510 attrs: item.attrs.clone(),
512 vis: item.vis.clone(),
513 stab: self.stability(item.hir_id),
514 depr: self.deprecation(item.hir_id),
518 hir::ItemKind::Const(ref ty, ref exp) => {
524 attrs: item.attrs.clone(),
526 vis: item.vis.clone(),
527 stab: self.stability(item.hir_id),
528 depr: self.deprecation(item.hir_id),
530 om.constants.push(s);
532 hir::ItemKind::Trait(is_auto, unsafety, ref gen, ref b, ref item_ids) => {
533 let items = item_ids.iter()
534 .map(|ti| self.cx.tcx.hir().trait_item(ti.id).clone())
541 generics: gen.clone(),
542 bounds: b.iter().cloned().collect(),
544 attrs: item.attrs.clone(),
546 vis: item.vis.clone(),
547 stab: self.stability(item.hir_id),
548 depr: self.deprecation(item.hir_id),
552 hir::ItemKind::TraitAlias(ref gen, ref b) => {
555 generics: gen.clone(),
556 bounds: b.iter().cloned().collect(),
558 attrs: item.attrs.clone(),
560 vis: item.vis.clone(),
561 stab: self.stability(item.hir_id),
562 depr: self.deprecation(item.hir_id),
564 om.trait_aliases.push(t);
567 hir::ItemKind::Impl(unsafety,
574 // Don't duplicate impls when inlining or if it's implementing a trait, we'll pick
575 // them up regardless of where they're located.
576 if !self.inlining && tr.is_none() {
577 let items = item_ids.iter()
578 .map(|ii| self.cx.tcx.hir().impl_item(ii.id).clone())
584 generics: gen.clone(),
588 attrs: item.attrs.clone(),
591 vis: item.vis.clone(),
592 stab: self.stability(item.hir_id),
593 depr: self.deprecation(item.hir_id),
601 // Convert each `exported_macro` into a doc item.
602 fn visit_local_macro(
605 renamed: Option<ast::Name>
607 debug!("visit_local_macro: {}", def.name);
608 let tts = def.body.trees().collect::<Vec<_>>();
609 // Extract the spans of all matchers. They represent the "interface" of the macro.
610 let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect();
614 def_id: self.cx.tcx.hir().local_def_id_from_hir_id(def.hir_id),
615 attrs: def.attrs.clone(),
616 name: renamed.unwrap_or(def.name),
619 stab: self.stability(def.hir_id),
620 depr: self.deprecation(def.hir_id),