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 clean::{self, AttributesExt, NestedAttributesExt, def_id_to_path};
21 // Looks to me like the first two of these are actually
22 // output parameters, maybe only mutated once; perhaps
23 // better simply to have the visit method return a tuple
26 // Also, is there some reason that this doesn't use the 'visit'
27 // framework from syntax?.
29 pub struct RustdocVisitor<'a, 'tcx: 'a, 'rcx: 'a> {
31 pub attrs: hir::HirVec<ast::Attribute>,
32 pub cx: &'a core::DocContext<'a, 'tcx, 'rcx>,
33 view_item_stack: FxHashSet<ast::NodeId>,
35 /// Are the current module and all of its parents public?
36 inside_public_path: bool,
37 exact_paths: Option<FxHashMap<DefId, Vec<String>>>,
40 impl<'a, 'tcx, 'rcx> RustdocVisitor<'a, 'tcx, 'rcx> {
42 cx: &'a core::DocContext<'a, 'tcx, 'rcx>
43 ) -> RustdocVisitor<'a, 'tcx, 'rcx> {
44 // If the root is re-exported, terminate all recursion.
45 let mut stack = FxHashSet::default();
46 stack.insert(ast::CRATE_NODE_ID);
48 module: Module::new(None),
49 attrs: hir::HirVec::new(),
51 view_item_stack: stack,
53 inside_public_path: true,
54 exact_paths: Some(FxHashMap::default()),
58 fn store_path(&mut self, did: DefId) {
59 // We can't use the entry API, as that keeps the mutable borrow of `self` active
60 // when we try to use `cx`.
61 let exact_paths = self.exact_paths.as_mut().unwrap();
62 if exact_paths.get(&did).is_none() {
63 let path = def_id_to_path(self.cx, did, self.cx.crate_name.clone());
64 exact_paths.insert(did, path);
68 fn stability(&self, id: ast::NodeId) -> Option<attr::Stability> {
69 self.cx.tcx.hir().opt_local_def_id(id)
70 .and_then(|def_id| self.cx.tcx.lookup_stability(def_id)).cloned()
73 fn deprecation(&self, id: ast::NodeId) -> Option<attr::Deprecation> {
74 self.cx.tcx.hir().opt_local_def_id(id)
75 .and_then(|def_id| self.cx.tcx.lookup_deprecation(def_id))
78 pub fn visit(&mut self, krate: &hir::Crate) {
79 self.attrs = krate.attrs.clone();
81 self.module = self.visit_mod_contents(krate.span,
83 Spanned { span: syntax_pos::DUMMY_SP,
84 node: hir::VisibilityKind::Public },
88 // Attach the crate's exported macros to the top-level module:
89 let macro_exports: Vec<_> =
90 krate.exported_macros.iter().map(|def| self.visit_local_macro(def, None)).collect();
91 self.module.macros.extend(macro_exports);
92 self.module.is_crate = true;
94 self.cx.renderinfo.borrow_mut().exact_paths = self.exact_paths.take().unwrap();
97 pub fn visit_variant_data(&mut self, item: &hir::Item,
98 name: ast::Name, sd: &hir::VariantData,
99 generics: &hir::Generics) -> Struct {
100 debug!("Visiting struct");
101 let struct_type = struct_type_from_def(&*sd);
106 vis: item.vis.clone(),
107 stab: self.stability(item.id),
108 depr: self.deprecation(item.id),
109 attrs: item.attrs.clone(),
110 generics: generics.clone(),
111 fields: sd.fields().iter().cloned().collect(),
116 pub fn visit_union_data(&mut self, item: &hir::Item,
117 name: ast::Name, sd: &hir::VariantData,
118 generics: &hir::Generics) -> Union {
119 debug!("Visiting union");
120 let struct_type = struct_type_from_def(&*sd);
125 vis: item.vis.clone(),
126 stab: self.stability(item.id),
127 depr: self.deprecation(item.id),
128 attrs: item.attrs.clone(),
129 generics: generics.clone(),
130 fields: sd.fields().iter().cloned().collect(),
135 pub fn visit_enum_def(&mut self, it: &hir::Item,
136 name: ast::Name, def: &hir::EnumDef,
137 params: &hir::Generics) -> Enum {
138 debug!("Visiting enum");
141 variants: def.variants.iter().map(|v| Variant {
142 name: v.node.ident.name,
143 attrs: v.node.attrs.clone(),
144 stab: self.stability(v.node.data.id()),
145 depr: self.deprecation(v.node.data.id()),
146 def: v.node.data.clone(),
150 stab: self.stability(it.id),
151 depr: self.deprecation(it.id),
152 generics: params.clone(),
153 attrs: it.attrs.clone(),
159 pub fn visit_fn(&mut self, om: &mut Module, item: &hir::Item,
160 name: ast::Name, fd: &hir::FnDecl,
161 header: hir::FnHeader,
164 debug!("Visiting fn");
165 let macro_kind = item.attrs.iter().filter_map(|a| {
166 if a.check_name("proc_macro") {
167 Some(MacroKind::Bang)
168 } else if a.check_name("proc_macro_derive") {
169 Some(MacroKind::Derive)
170 } else if a.check_name("proc_macro_attribute") {
171 Some(MacroKind::Attr)
178 let name = if kind == MacroKind::Derive {
179 item.attrs.lists("proc_macro_derive")
180 .filter_map(|mi| mi.name())
182 .expect("proc-macro derives require a name")
187 let mut helpers = Vec::new();
188 for mi in item.attrs.lists("proc_macro_derive") {
189 if !mi.check_name("attributes") {
193 if let Some(list) = mi.meta_item_list() {
194 for inner_mi in list {
195 if let Some(name) = inner_mi.name() {
202 om.proc_macros.push(ProcMacro {
207 attrs: item.attrs.clone(),
209 stab: self.stability(item.id),
210 depr: self.deprecation(item.id),
214 om.fns.push(Function {
216 vis: item.vis.clone(),
217 stab: self.stability(item.id),
218 depr: self.deprecation(item.id),
219 attrs: item.attrs.clone(),
223 generics: gen.clone(),
231 pub fn visit_mod_contents(&mut self, span: Span, attrs: hir::HirVec<ast::Attribute>,
232 vis: hir::Visibility, id: ast::NodeId,
234 name: Option<ast::Name>) -> Module {
235 let mut om = Module::new(name);
236 om.where_outer = span;
237 om.where_inner = m.inner;
239 om.vis = vis.clone();
240 om.stab = self.stability(id);
241 om.depr = self.deprecation(id);
243 // Keep track of if there were any private modules in the path.
244 let orig_inside_public_path = self.inside_public_path;
245 self.inside_public_path &= vis.node.is_pub();
246 for i in &m.item_ids {
247 let item = self.cx.tcx.hir().expect_item(i.id);
248 self.visit_item(item, None, &mut om);
250 self.inside_public_path = orig_inside_public_path;
254 /// Tries to resolve the target of a `pub use` statement and inlines the
255 /// target if it is defined locally and would not be documented otherwise,
256 /// or when it is specifically requested with `please_inline`.
257 /// (the latter is the case when the import is marked `doc(inline)`)
259 /// Cross-crate inlining occurs later on during crate cleaning
260 /// and follows different rules.
262 /// Returns true if the target has been inlined.
263 fn maybe_inline_local(&mut self,
266 renamed: Option<ast::Ident>,
269 please_inline: bool) -> bool {
271 fn inherits_doc_hidden(cx: &core::DocContext, mut node: ast::NodeId) -> bool {
272 while let Some(id) = cx.tcx.hir().get_enclosing_scope(node) {
274 if cx.tcx.hir().attrs(node).lists("doc").has_word("hidden") {
277 if node == ast::CRATE_NODE_ID {
284 debug!("maybe_inline_local def: {:?}", def);
286 let tcx = self.cx.tcx;
287 let def_did = if let Some(did) = def.opt_def_id() {
293 let use_attrs = tcx.hir().attrs(id);
294 // Don't inline `doc(hidden)` imports so they can be stripped at a later stage.
295 let is_no_inline = use_attrs.lists("doc").has_word("no_inline") ||
296 use_attrs.lists("doc").has_word("hidden");
298 // For cross-crate impl inlining we need to know whether items are
299 // reachable in documentation -- a previously nonreachable item can be
300 // made reachable by cross-crate inlining which we're checking here.
301 // (this is done here because we need to know this upfront).
302 if !def_did.is_local() && !is_no_inline {
303 let attrs = clean::inline::load_attrs(self.cx, def_did);
304 let self_is_hidden = attrs.lists("doc").has_word("hidden");
310 Def::ForeignTy(did) |
311 Def::TyAlias(did) if !self_is_hidden => {
315 .insert(did, AccessLevel::Public);
317 Def::Mod(did) => if !self_is_hidden {
318 ::visit_lib::LibEmbargoVisitor::new(self.cx).visit_mod(did);
326 let def_node_id = match tcx.hir().as_local_node_id(def_did) {
327 Some(n) => n, None => return false
330 let is_private = !self.cx.renderinfo.borrow().access_levels.is_public(def_did);
331 let is_hidden = inherits_doc_hidden(self.cx, def_node_id);
333 // Only inline if requested or if the item would otherwise be stripped.
334 if (!please_inline && !is_private && !is_hidden) || is_no_inline {
338 if !self.view_item_stack.insert(def_node_id) { return false }
340 let ret = match tcx.hir().get(def_node_id) {
341 Node::Item(&hir::Item { node: hir::ItemKind::Mod(ref m), .. }) if glob => {
342 let prev = mem::replace(&mut self.inlining, true);
343 for i in &m.item_ids {
344 let i = self.cx.tcx.hir().expect_item(i.id);
345 self.visit_item(i, None, om);
347 self.inlining = prev;
350 Node::Item(it) if !glob => {
351 let prev = mem::replace(&mut self.inlining, true);
352 self.visit_item(it, renamed, om);
353 self.inlining = prev;
356 Node::ForeignItem(it) if !glob => {
357 // Generate a fresh `extern {}` block if we want to inline a foreign item.
358 om.foreigns.push(hir::ForeignMod {
359 abi: tcx.hir().get_foreign_abi(it.id),
360 items: vec![hir::ForeignItem {
361 ident: renamed.unwrap_or(it.ident),
367 Node::MacroDef(def) if !glob => {
368 om.macros.push(self.visit_local_macro(def, renamed.map(|i| i.name)));
373 self.view_item_stack.remove(&def_node_id);
377 pub fn visit_item(&mut self, item: &hir::Item,
378 renamed: Option<ast::Ident>, om: &mut Module) {
379 debug!("Visiting item {:?}", item);
380 let ident = renamed.unwrap_or(item.ident);
382 if item.vis.node.is_pub() {
383 let def_id = self.cx.tcx.hir().local_def_id(item.id);
384 self.store_path(def_id);
388 hir::ItemKind::ForeignMod(ref fm) => {
389 // If inlining we only want to include public functions.
390 om.foreigns.push(if self.inlining {
393 items: fm.items.iter().filter(|i| i.vis.node.is_pub()).cloned().collect(),
399 // If we're inlining, skip private items.
400 _ if self.inlining && !item.vis.node.is_pub() => {}
401 hir::ItemKind::GlobalAsm(..) => {}
402 hir::ItemKind::ExternCrate(orig_name) => {
403 let def_id = self.cx.tcx.hir().local_def_id(item.id);
404 om.extern_crates.push(ExternCrate {
405 cnum: self.cx.tcx.extern_mod_stmt_cnum(def_id)
406 .unwrap_or(LOCAL_CRATE),
408 path: orig_name.map(|x|x.to_string()),
409 vis: item.vis.clone(),
410 attrs: item.attrs.clone(),
414 hir::ItemKind::Use(_, hir::UseKind::ListStem) => {}
415 hir::ItemKind::Use(ref path, kind) => {
416 let is_glob = kind == hir::UseKind::Glob;
418 // Struct and variant constructors and proc macro stubs always show up alongside
419 // their definitions, we've already processed them so just discard these.
421 Def::StructCtor(..) | Def::VariantCtor(..) | Def::SelfCtor(..) |
422 Def::Macro(_, MacroKind::ProcMacroStub) => return,
426 // If there was a private module in the current path then don't bother inlining
427 // anything as it will probably be stripped anyway.
428 if item.vis.node.is_pub() && self.inside_public_path {
429 let please_inline = item.attrs.iter().any(|item| {
430 match item.meta_item_list() {
431 Some(ref list) if item.check_name("doc") => {
432 list.iter().any(|i| i.check_name("inline"))
437 let ident = if is_glob { None } else { Some(ident) };
438 if self.maybe_inline_local(item.id,
448 om.imports.push(Import {
451 vis: item.vis.clone(),
452 attrs: item.attrs.clone(),
453 path: (**path).clone(),
458 hir::ItemKind::Mod(ref m) => {
459 om.mods.push(self.visit_mod_contents(item.span,
466 hir::ItemKind::Enum(ref ed, ref gen) =>
467 om.enums.push(self.visit_enum_def(item, ident.name, ed, gen)),
468 hir::ItemKind::Struct(ref sd, ref gen) =>
469 om.structs.push(self.visit_variant_data(item, ident.name, sd, gen)),
470 hir::ItemKind::Union(ref sd, ref gen) =>
471 om.unions.push(self.visit_union_data(item, ident.name, sd, gen)),
472 hir::ItemKind::Fn(ref fd, header, ref gen, body) =>
473 self.visit_fn(om, item, ident.name, &**fd, header, gen, body),
474 hir::ItemKind::Ty(ref ty, ref gen) => {
480 attrs: item.attrs.clone(),
482 vis: item.vis.clone(),
483 stab: self.stability(item.id),
484 depr: self.deprecation(item.id),
488 hir::ItemKind::Existential(ref exist_ty) => {
489 let t = Existential {
490 exist_ty: exist_ty.clone(),
493 attrs: item.attrs.clone(),
495 vis: item.vis.clone(),
496 stab: self.stability(item.id),
497 depr: self.deprecation(item.id),
499 om.existentials.push(t);
501 hir::ItemKind::Static(ref ty, ref mut_, ref exp) => {
504 mutability: mut_.clone(),
508 attrs: item.attrs.clone(),
510 vis: item.vis.clone(),
511 stab: self.stability(item.id),
512 depr: self.deprecation(item.id),
516 hir::ItemKind::Const(ref ty, ref exp) => {
522 attrs: item.attrs.clone(),
524 vis: item.vis.clone(),
525 stab: self.stability(item.id),
526 depr: self.deprecation(item.id),
528 om.constants.push(s);
530 hir::ItemKind::Trait(is_auto, unsafety, ref gen, ref b, ref item_ids) => {
531 let items = item_ids.iter()
532 .map(|ti| self.cx.tcx.hir().trait_item(ti.id).clone())
539 generics: gen.clone(),
540 bounds: b.iter().cloned().collect(),
542 attrs: item.attrs.clone(),
544 vis: item.vis.clone(),
545 stab: self.stability(item.id),
546 depr: self.deprecation(item.id),
550 hir::ItemKind::TraitAlias(..) => {
551 unimplemented!("trait objects are not yet implemented")
554 hir::ItemKind::Impl(unsafety,
561 // Don't duplicate impls when inlining or if it's implementing a trait, we'll pick
562 // them up regardless of where they're located.
563 if !self.inlining && tr.is_none() {
564 let items = item_ids.iter()
565 .map(|ii| self.cx.tcx.hir().impl_item(ii.id).clone())
571 generics: gen.clone(),
575 attrs: item.attrs.clone(),
578 vis: item.vis.clone(),
579 stab: self.stability(item.id),
580 depr: self.deprecation(item.id),
588 // Convert each `exported_macro` into a doc item.
589 fn visit_local_macro(
592 renamed: Option<ast::Name>
594 debug!("visit_local_macro: {}", def.name);
595 let tts = def.body.trees().collect::<Vec<_>>();
596 // Extract the spans of all matchers. They represent the "interface" of the macro.
597 let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect();
600 def_id: self.cx.tcx.hir().local_def_id(def.id),
601 attrs: def.attrs.clone(),
602 name: renamed.unwrap_or(def.name),
605 stab: self.stability(def.id),
606 depr: self.deprecation(def.id),