2013-09-12 21:10:51 -04:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-08-15 16:28:54 -04:00
|
|
|
//! Rust AST Visitor. Extracts useful information and massages it into a form
|
|
|
|
//! usable for clean
|
|
|
|
|
2015-04-07 11:08:21 -07:00
|
|
|
use std::mem;
|
2014-09-23 15:13:56 -07:00
|
|
|
|
2014-04-02 01:19:41 -07:00
|
|
|
use syntax::abi;
|
2013-12-22 11:23:04 -08:00
|
|
|
use syntax::ast;
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::attr;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir::map as hir_map;
|
2016-04-15 16:34:48 +02:00
|
|
|
use rustc::hir::def::Def;
|
2016-08-31 14:00:29 +03:00
|
|
|
use rustc::hir::def_id::LOCAL_CRATE;
|
2016-11-05 20:30:40 +00:00
|
|
|
use rustc::middle::cstore::LoadedMacro;
|
2016-04-15 16:34:48 +02:00
|
|
|
use rustc::middle::privacy::AccessLevel;
|
2016-11-08 14:02:55 +11:00
|
|
|
use rustc::util::nodemap::FxHashSet;
|
2014-06-26 11:37:39 -07:00
|
|
|
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2014-01-07 18:46:16 -08:00
|
|
|
use core;
|
2016-04-15 16:34:48 +02:00
|
|
|
use clean::{self, Clean, Attributes};
|
2013-08-15 16:28:54 -04:00
|
|
|
use doctree::*;
|
|
|
|
|
2014-07-17 09:45:31 -07:00
|
|
|
// looks to me like the first two of these are actually
|
|
|
|
// output parameters, maybe only mutated once; perhaps
|
|
|
|
// better simply to have the visit method return a tuple
|
|
|
|
// containing them?
|
|
|
|
|
|
|
|
// also, is there some reason that this doesn't use the 'visit'
|
|
|
|
// framework from syntax?
|
|
|
|
|
2014-09-06 19:13:40 +03:00
|
|
|
pub struct RustdocVisitor<'a, 'tcx: 'a> {
|
2014-03-28 10:27:24 -07:00
|
|
|
pub module: Module,
|
2015-12-17 20:41:28 +03:00
|
|
|
pub attrs: hir::HirVec<ast::Attribute>,
|
2015-06-14 04:50:23 +03:00
|
|
|
pub cx: &'a core::DocContext<'a, 'tcx>,
|
2016-11-08 14:02:55 +11:00
|
|
|
view_item_stack: FxHashSet<ast::NodeId>,
|
2016-11-14 18:24:47 +00:00
|
|
|
inlining: bool,
|
|
|
|
/// Is the current module and all of its parents public?
|
|
|
|
inside_public_path: bool,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
2014-09-06 19:13:40 +03:00
|
|
|
impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
|
2016-04-07 05:59:02 +02:00
|
|
|
pub fn new(cx: &'a core::DocContext<'a, 'tcx>) -> RustdocVisitor<'a, 'tcx> {
|
2014-09-23 15:19:30 -07:00
|
|
|
// If the root is reexported, terminate all recursion.
|
2016-11-08 14:02:55 +11:00
|
|
|
let mut stack = FxHashSet();
|
2014-09-23 15:19:30 -07:00
|
|
|
stack.insert(ast::CRATE_NODE_ID);
|
2013-08-15 16:28:54 -04:00
|
|
|
RustdocVisitor {
|
|
|
|
module: Module::new(None),
|
2015-12-17 20:41:28 +03:00
|
|
|
attrs: hir::HirVec::new(),
|
2014-01-07 18:46:16 -08:00
|
|
|
cx: cx,
|
2014-09-23 15:19:30 -07:00
|
|
|
view_item_stack: stack,
|
2016-11-14 18:24:47 +00:00
|
|
|
inlining: false,
|
|
|
|
inside_public_path: true,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 11:37:39 -07:00
|
|
|
fn stability(&self, id: ast::NodeId) -> Option<attr::Stability> {
|
2015-09-10 15:53:08 -04:00
|
|
|
self.cx.tcx_opt().and_then(|tcx| {
|
|
|
|
self.cx.map.opt_local_def_id(id)
|
2016-03-17 00:15:31 +02:00
|
|
|
.and_then(|def_id| tcx.lookup_stability(def_id))
|
2015-09-10 15:53:08 -04:00
|
|
|
.cloned()
|
|
|
|
})
|
2014-06-26 11:37:39 -07:00
|
|
|
}
|
|
|
|
|
2015-12-12 23:01:27 +03:00
|
|
|
fn deprecation(&self, id: ast::NodeId) -> Option<attr::Deprecation> {
|
|
|
|
self.cx.tcx_opt().and_then(|tcx| {
|
|
|
|
self.cx.map.opt_local_def_id(id)
|
2016-03-17 00:15:31 +02:00
|
|
|
.and_then(|def_id| tcx.lookup_deprecation(def_id))
|
2015-12-12 23:01:27 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn visit(&mut self, krate: &hir::Crate) {
|
2014-05-18 16:56:13 +03:00
|
|
|
self.attrs = krate.attrs.clone();
|
2014-01-07 18:46:16 -08:00
|
|
|
|
2014-02-28 17:46:09 -08:00
|
|
|
self.module = self.visit_mod_contents(krate.span,
|
2014-05-18 16:56:13 +03:00
|
|
|
krate.attrs.clone(),
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::Public,
|
2014-02-28 17:46:09 -08:00
|
|
|
ast::CRATE_NODE_ID,
|
|
|
|
&krate.module,
|
|
|
|
None);
|
2014-07-17 09:45:31 -07:00
|
|
|
// attach the crate's exported macros to the top-level module:
|
2016-10-29 07:31:07 +00:00
|
|
|
let macro_exports: Vec<_> =
|
|
|
|
krate.exported_macros.iter().map(|def| self.visit_macro(def)).collect();
|
|
|
|
self.module.macros.extend(macro_exports);
|
2014-02-28 22:33:45 +01:00
|
|
|
self.module.is_crate = true;
|
2014-01-07 18:46:16 -08:00
|
|
|
}
|
|
|
|
|
2015-10-08 03:20:57 +03:00
|
|
|
pub fn visit_variant_data(&mut self, item: &hir::Item,
|
|
|
|
name: ast::Name, sd: &hir::VariantData,
|
2015-07-31 00:04:06 -07:00
|
|
|
generics: &hir::Generics) -> Struct {
|
2014-01-07 18:46:16 -08:00
|
|
|
debug!("Visiting struct");
|
2014-05-16 10:15:33 -07:00
|
|
|
let struct_type = struct_type_from_def(&*sd);
|
2014-01-07 18:46:16 -08:00
|
|
|
Struct {
|
|
|
|
id: item.id,
|
|
|
|
struct_type: struct_type,
|
2014-05-18 16:56:13 +03:00
|
|
|
name: name,
|
2016-03-25 06:08:11 +00:00
|
|
|
vis: item.vis.clone(),
|
2014-06-26 11:37:39 -07:00
|
|
|
stab: self.stability(item.id),
|
2015-12-12 23:01:27 +03:00
|
|
|
depr: self.deprecation(item.id),
|
2014-05-18 16:56:13 +03:00
|
|
|
attrs: item.attrs.clone(),
|
2014-01-07 18:46:16 -08:00
|
|
|
generics: generics.clone(),
|
2015-10-25 18:33:51 +03:00
|
|
|
fields: sd.fields().iter().cloned().collect(),
|
2014-08-11 09:32:26 -07:00
|
|
|
whence: item.span
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
2014-01-07 18:46:16 -08:00
|
|
|
}
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2016-08-10 21:00:17 +03:00
|
|
|
pub fn visit_union_data(&mut self, item: &hir::Item,
|
|
|
|
name: ast::Name, sd: &hir::VariantData,
|
|
|
|
generics: &hir::Generics) -> Union {
|
|
|
|
debug!("Visiting union");
|
|
|
|
let struct_type = struct_type_from_def(&*sd);
|
|
|
|
Union {
|
|
|
|
id: item.id,
|
|
|
|
struct_type: struct_type,
|
|
|
|
name: name,
|
|
|
|
vis: item.vis.clone(),
|
|
|
|
stab: self.stability(item.id),
|
|
|
|
depr: self.deprecation(item.id),
|
|
|
|
attrs: item.attrs.clone(),
|
|
|
|
generics: generics.clone(),
|
|
|
|
fields: sd.fields().iter().cloned().collect(),
|
|
|
|
whence: item.span
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn visit_enum_def(&mut self, it: &hir::Item,
|
2015-09-20 04:50:30 +03:00
|
|
|
name: ast::Name, def: &hir::EnumDef,
|
2015-07-31 00:04:06 -07:00
|
|
|
params: &hir::Generics) -> Enum {
|
2014-01-07 18:46:16 -08:00
|
|
|
debug!("Visiting enum");
|
|
|
|
Enum {
|
2014-05-18 16:56:13 +03:00
|
|
|
name: name,
|
|
|
|
variants: def.variants.iter().map(|v| Variant {
|
|
|
|
name: v.node.name,
|
|
|
|
attrs: v.node.attrs.clone(),
|
2015-10-10 03:28:40 +03:00
|
|
|
stab: self.stability(v.node.data.id()),
|
2015-12-12 23:01:27 +03:00
|
|
|
depr: self.deprecation(v.node.data.id()),
|
2015-10-08 03:20:57 +03:00
|
|
|
def: v.node.data.clone(),
|
2014-05-18 16:56:13 +03:00
|
|
|
whence: v.span,
|
|
|
|
}).collect(),
|
2016-03-25 06:08:11 +00:00
|
|
|
vis: it.vis.clone(),
|
2014-06-26 11:37:39 -07:00
|
|
|
stab: self.stability(it.id),
|
2015-12-12 23:01:27 +03:00
|
|
|
depr: self.deprecation(it.id),
|
2014-01-07 18:46:16 -08:00
|
|
|
generics: params.clone(),
|
2014-05-18 16:56:13 +03:00
|
|
|
attrs: it.attrs.clone(),
|
2014-01-07 18:46:16 -08:00
|
|
|
id: it.id,
|
2014-08-11 09:32:26 -07:00
|
|
|
whence: it.span,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
2014-01-07 18:46:16 -08:00
|
|
|
}
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn visit_fn(&mut self, item: &hir::Item,
|
2015-09-20 04:50:30 +03:00
|
|
|
name: ast::Name, fd: &hir::FnDecl,
|
2015-07-31 00:04:06 -07:00
|
|
|
unsafety: &hir::Unsafety,
|
|
|
|
constness: hir::Constness,
|
2015-05-05 08:47:04 -04:00
|
|
|
abi: &abi::Abi,
|
2015-07-31 00:04:06 -07:00
|
|
|
gen: &hir::Generics) -> Function {
|
2014-01-07 18:46:16 -08:00
|
|
|
debug!("Visiting fn");
|
|
|
|
Function {
|
|
|
|
id: item.id,
|
2016-03-25 06:08:11 +00:00
|
|
|
vis: item.vis.clone(),
|
2014-06-26 11:37:39 -07:00
|
|
|
stab: self.stability(item.id),
|
2015-12-12 23:01:27 +03:00
|
|
|
depr: self.deprecation(item.id),
|
2014-05-18 16:56:13 +03:00
|
|
|
attrs: item.attrs.clone(),
|
2014-01-07 18:46:16 -08:00
|
|
|
decl: fd.clone(),
|
2014-05-18 16:56:13 +03:00
|
|
|
name: name,
|
2014-08-11 09:32:26 -07:00
|
|
|
whence: item.span,
|
2014-01-07 18:46:16 -08:00
|
|
|
generics: gen.clone(),
|
2014-12-09 10:36:46 -05:00
|
|
|
unsafety: *unsafety,
|
2015-02-25 22:05:07 +02:00
|
|
|
constness: constness,
|
2015-04-07 14:22:55 -07:00
|
|
|
abi: *abi,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
2014-01-07 18:46:16 -08:00
|
|
|
}
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2015-12-17 20:41:28 +03:00
|
|
|
pub fn visit_mod_contents(&mut self, span: Span, attrs: hir::HirVec<ast::Attribute>,
|
2015-07-31 00:04:06 -07:00
|
|
|
vis: hir::Visibility, id: ast::NodeId,
|
|
|
|
m: &hir::Mod,
|
2015-09-20 04:50:30 +03:00
|
|
|
name: Option<ast::Name>) -> Module {
|
2014-01-07 18:46:16 -08:00
|
|
|
let mut om = Module::new(name);
|
2014-04-27 05:08:36 +09:00
|
|
|
om.where_outer = span;
|
|
|
|
om.where_inner = m.inner;
|
2014-01-07 18:46:16 -08:00
|
|
|
om.attrs = attrs;
|
2016-03-25 06:08:11 +00:00
|
|
|
om.vis = vis.clone();
|
2014-06-26 11:37:39 -07:00
|
|
|
om.stab = self.stability(id);
|
2015-12-12 23:01:27 +03:00
|
|
|
om.depr = self.deprecation(id);
|
2014-01-07 18:46:16 -08:00
|
|
|
om.id = id;
|
2016-11-14 18:24:47 +00:00
|
|
|
// Keep track of if there were any private modules in the path.
|
|
|
|
let orig_inside_public_path = self.inside_public_path;
|
|
|
|
self.inside_public_path &= vis == hir::Public;
|
2015-11-17 17:51:44 -05:00
|
|
|
for i in &m.item_ids {
|
|
|
|
let item = self.cx.map.expect_item(i.id);
|
|
|
|
self.visit_item(item, None, &mut om);
|
2014-01-07 18:46:16 -08:00
|
|
|
}
|
2016-11-14 18:24:47 +00:00
|
|
|
self.inside_public_path = orig_inside_public_path;
|
2016-10-29 07:31:07 +00:00
|
|
|
if let Some(exports) = self.cx.export_map.get(&id) {
|
|
|
|
for export in exports {
|
|
|
|
if let Def::Macro(def_id) = export.def {
|
|
|
|
if def_id.krate == LOCAL_CRATE {
|
|
|
|
continue // These are `krate.exported_macros`, handled in `self.visit()`.
|
|
|
|
}
|
2016-11-05 20:30:40 +00:00
|
|
|
let def = match self.cx.sess().cstore.load_macro(def_id, self.cx.sess()) {
|
|
|
|
LoadedMacro::MacroRules(macro_rules) => macro_rules,
|
|
|
|
// FIXME(jseyfried): document proc macro reexports
|
|
|
|
LoadedMacro::ProcMacro(..) => continue,
|
|
|
|
};
|
|
|
|
|
2016-10-29 07:31:07 +00:00
|
|
|
// FIXME(jseyfried) merge with `self.visit_macro()`
|
|
|
|
let matchers = def.body.chunks(4).map(|arm| arm[0].get_span()).collect();
|
|
|
|
om.macros.push(Macro {
|
|
|
|
id: def.id,
|
|
|
|
attrs: def.attrs.clone().into(),
|
|
|
|
name: def.ident.name,
|
|
|
|
whence: def.span,
|
|
|
|
matchers: matchers,
|
|
|
|
stab: self.stability(def.id),
|
|
|
|
depr: self.deprecation(def.id),
|
|
|
|
imported_from: def.imported_from.map(|ident| ident.name),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-07 18:46:16 -08:00
|
|
|
om
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
fn visit_view_path(&mut self, path: hir::ViewPath_,
|
2014-05-02 17:16:30 -07:00
|
|
|
om: &mut Module,
|
2014-12-26 10:55:16 +02:00
|
|
|
id: ast::NodeId,
|
2015-07-31 00:04:06 -07:00
|
|
|
please_inline: bool) -> Option<hir::ViewPath_> {
|
2014-12-26 10:55:16 +02:00
|
|
|
match path {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ViewPathSimple(dst, base) => {
|
2016-03-10 03:29:46 +01:00
|
|
|
if self.maybe_inline_local(id, Some(dst), false, om, please_inline) {
|
2014-12-26 10:55:16 +02:00
|
|
|
None
|
|
|
|
} else {
|
2015-07-31 00:04:06 -07:00
|
|
|
Some(hir::ViewPathSimple(dst, base))
|
2014-07-25 10:16:41 -07:00
|
|
|
}
|
2014-01-07 18:46:16 -08:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ViewPathList(p, paths) => {
|
2014-12-26 10:55:16 +02:00
|
|
|
let mine = paths.into_iter().filter(|path| {
|
2016-08-12 23:09:41 +00:00
|
|
|
!self.maybe_inline_local(path.node.id, path.node.rename,
|
2016-06-26 03:08:10 +01:00
|
|
|
false, om, please_inline)
|
2015-12-17 20:41:28 +03:00
|
|
|
}).collect::<hir::HirVec<hir::PathListItem>>();
|
2014-01-07 18:46:16 -08:00
|
|
|
|
2015-03-24 16:53:34 -07:00
|
|
|
if mine.is_empty() {
|
2014-12-26 10:55:16 +02:00
|
|
|
None
|
|
|
|
} else {
|
2015-07-31 00:04:06 -07:00
|
|
|
Some(hir::ViewPathList(p, mine))
|
2014-12-26 10:55:16 +02:00
|
|
|
}
|
2014-01-07 18:46:16 -08:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ViewPathGlob(base) => {
|
2016-03-10 03:29:46 +01:00
|
|
|
if self.maybe_inline_local(id, None, true, om, please_inline) {
|
2014-12-26 10:55:16 +02:00
|
|
|
None
|
|
|
|
} else {
|
2015-07-31 00:04:06 -07:00
|
|
|
Some(hir::ViewPathGlob(base))
|
2014-07-25 10:16:41 -07:00
|
|
|
}
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
}
|
2014-12-26 10:55:16 +02:00
|
|
|
|
2014-01-07 18:46:16 -08:00
|
|
|
}
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2016-03-10 03:29:46 +01:00
|
|
|
/// Tries to resolve the target of a `pub use` statement and inlines the
|
|
|
|
/// target if it is defined locally and would not be documented otherwise,
|
|
|
|
/// or when it is specifically requested with `please_inline`.
|
|
|
|
/// (the latter is the case when the import is marked `doc(inline)`)
|
|
|
|
///
|
|
|
|
/// Cross-crate inlining occurs later on during crate cleaning
|
|
|
|
/// and follows different rules.
|
|
|
|
///
|
|
|
|
/// Returns true if the target has been inlined.
|
|
|
|
fn maybe_inline_local(&mut self, id: ast::NodeId, renamed: Option<ast::Name>,
|
2014-07-25 10:16:41 -07:00
|
|
|
glob: bool, om: &mut Module, please_inline: bool) -> bool {
|
2016-03-10 03:29:46 +01:00
|
|
|
|
|
|
|
fn inherits_doc_hidden(cx: &core::DocContext, mut node: ast::NodeId) -> bool {
|
|
|
|
while let Some(id) = cx.map.get_enclosing_scope(node) {
|
|
|
|
node = id;
|
|
|
|
let attrs = cx.map.attrs(node).clean(cx);
|
2016-03-22 20:26:33 +01:00
|
|
|
if attrs.list("doc").has_word("hidden") {
|
2016-03-10 03:29:46 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if node == ast::CRATE_NODE_ID {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2014-09-06 19:13:40 +03:00
|
|
|
let tcx = match self.cx.tcx_opt() {
|
|
|
|
Some(tcx) => tcx,
|
|
|
|
None => return false
|
2014-01-07 18:46:16 -08:00
|
|
|
};
|
2016-06-03 23:15:00 +03:00
|
|
|
let def = tcx.expect_def(id);
|
2016-04-15 16:34:48 +02:00
|
|
|
let def_did = def.def_id();
|
2016-03-10 03:29:46 +01:00
|
|
|
|
2016-03-22 20:26:33 +01:00
|
|
|
let use_attrs = tcx.map.attrs(id).clean(self.cx);
|
2016-06-07 01:20:12 +01:00
|
|
|
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
|
|
|
|
let is_no_inline = use_attrs.list("doc").has_word("no_inline") ||
|
|
|
|
use_attrs.list("doc").has_word("hidden");
|
2016-04-15 16:34:48 +02:00
|
|
|
|
|
|
|
// For cross-crate impl inlining we need to know whether items are
|
|
|
|
// reachable in documentation - a previously nonreachable item can be
|
|
|
|
// made reachable by cross-crate inlining which we're checking here.
|
|
|
|
// (this is done here because we need to know this upfront)
|
2016-06-03 23:15:00 +03:00
|
|
|
if !def_did.is_local() && !is_no_inline {
|
2016-04-15 16:34:48 +02:00
|
|
|
let attrs = clean::inline::load_attrs(self.cx, tcx, def_did);
|
|
|
|
let self_is_hidden = attrs.list("doc").has_word("hidden");
|
2016-06-03 23:15:00 +03:00
|
|
|
match def {
|
2016-04-15 16:34:48 +02:00
|
|
|
Def::Trait(did) |
|
|
|
|
Def::Struct(did) |
|
2016-08-10 21:00:17 +03:00
|
|
|
Def::Union(did) |
|
2016-04-15 16:34:48 +02:00
|
|
|
Def::Enum(did) |
|
|
|
|
Def::TyAlias(did) if !self_is_hidden => {
|
|
|
|
self.cx.access_levels.borrow_mut().map.insert(did, AccessLevel::Public);
|
|
|
|
},
|
|
|
|
Def::Mod(did) => if !self_is_hidden {
|
|
|
|
::visit_lib::LibEmbargoVisitor::new(self.cx).visit_mod(did);
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2016-03-22 20:26:33 +01:00
|
|
|
|
2016-04-15 16:34:48 +02:00
|
|
|
let def_node_id = match tcx.map.as_local_node_id(def_did) {
|
|
|
|
Some(n) => n, None => return false
|
|
|
|
};
|
|
|
|
|
|
|
|
let is_private = !self.cx.access_levels.borrow().is_public(def_did);
|
2016-03-10 03:29:46 +01:00
|
|
|
let is_hidden = inherits_doc_hidden(self.cx, def_node_id);
|
|
|
|
|
|
|
|
// Only inline if requested or if the item would otherwise be stripped
|
2016-03-22 20:26:33 +01:00
|
|
|
if (!please_inline && !is_private && !is_hidden) || is_no_inline {
|
2014-05-02 17:16:30 -07:00
|
|
|
return false
|
|
|
|
}
|
2016-03-10 03:29:46 +01:00
|
|
|
|
2015-09-04 13:52:28 -04:00
|
|
|
if !self.view_item_stack.insert(def_node_id) { return false }
|
2014-01-07 18:46:16 -08:00
|
|
|
|
2015-09-04 13:52:28 -04:00
|
|
|
let ret = match tcx.map.get(def_node_id) {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir_map::NodeItem(it) => {
|
2016-11-14 18:24:47 +00:00
|
|
|
let prev = mem::replace(&mut self.inlining, true);
|
2014-01-07 18:46:16 -08:00
|
|
|
if glob {
|
|
|
|
match it.node {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemMod(ref m) => {
|
2015-11-17 17:51:44 -05:00
|
|
|
for i in &m.item_ids {
|
|
|
|
let i = self.cx.map.expect_item(i.id);
|
|
|
|
self.visit_item(i, None, om);
|
2014-01-07 18:46:16 -08:00
|
|
|
}
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemEnum(..) => {}
|
2014-11-06 00:05:53 -08:00
|
|
|
_ => { panic!("glob not mapped to a module or enum"); }
|
2014-01-07 18:46:16 -08:00
|
|
|
}
|
|
|
|
} else {
|
2014-05-18 16:56:13 +03:00
|
|
|
self.visit_item(it, renamed, om);
|
2013-09-26 11:57:25 -07:00
|
|
|
}
|
2016-11-14 18:24:47 +00:00
|
|
|
self.inlining = prev;
|
2014-01-07 18:46:16 -08:00
|
|
|
true
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
2014-01-07 18:46:16 -08:00
|
|
|
_ => false,
|
2014-09-23 15:13:56 -07:00
|
|
|
};
|
2015-09-04 13:52:28 -04:00
|
|
|
self.view_item_stack.remove(&def_node_id);
|
2016-10-01 16:47:43 -04:00
|
|
|
ret
|
2014-01-07 18:46:16 -08:00
|
|
|
}
|
2013-08-15 16:28:54 -04:00
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn visit_item(&mut self, item: &hir::Item,
|
2015-09-20 14:51:40 +03:00
|
|
|
renamed: Option<ast::Name>, om: &mut Module) {
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("Visiting item {:?}", item);
|
2015-09-20 14:51:40 +03:00
|
|
|
let name = renamed.unwrap_or(item.name);
|
2014-01-07 18:46:16 -08:00
|
|
|
match item.node {
|
2016-11-14 18:24:47 +00:00
|
|
|
hir::ItemForeignMod(ref fm) => {
|
|
|
|
// If inlining we only want to include public functions.
|
|
|
|
om.foreigns.push(if self.inlining {
|
|
|
|
hir::ForeignMod {
|
|
|
|
abi: fm.abi,
|
|
|
|
items: fm.items.iter().filter(|i| i.vis == hir::Public).cloned().collect(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fm.clone()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// If we're inlining, skip private items.
|
|
|
|
_ if self.inlining && item.vis != hir::Public => {}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemExternCrate(ref p) => {
|
2016-04-25 08:24:50 +02:00
|
|
|
let cstore = &self.cx.sess().cstore;
|
2014-12-26 10:55:16 +02:00
|
|
|
om.extern_crates.push(ExternCrate {
|
2016-04-25 08:24:50 +02:00
|
|
|
cnum: cstore.extern_mod_stmt_cnum(item.id)
|
2016-08-31 14:00:29 +03:00
|
|
|
.unwrap_or(LOCAL_CRATE),
|
2014-12-26 10:55:16 +02:00
|
|
|
name: name,
|
2016-02-28 12:11:13 +01:00
|
|
|
path: p.map(|x|x.to_string()),
|
2016-03-25 06:08:11 +00:00
|
|
|
vis: item.vis.clone(),
|
2014-12-26 10:55:16 +02:00
|
|
|
attrs: item.attrs.clone(),
|
|
|
|
whence: item.span,
|
|
|
|
})
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemUse(ref vpath) => {
|
2014-12-26 10:55:16 +02:00
|
|
|
let node = vpath.node.clone();
|
2016-11-14 18:24:47 +00:00
|
|
|
// If there was a private module in the current path then don't bother inlining
|
|
|
|
// anything as it will probably be stripped anyway.
|
|
|
|
let node = if item.vis == hir::Public && self.inside_public_path {
|
2014-12-26 10:55:16 +02:00
|
|
|
let please_inline = item.attrs.iter().any(|item| {
|
|
|
|
match item.meta_item_list() {
|
2016-08-19 18:58:14 -07:00
|
|
|
Some(list) if item.check_name("doc") => {
|
|
|
|
list.iter().any(|i| i.check_name("inline"))
|
2014-12-26 10:55:16 +02:00
|
|
|
}
|
2016-03-09 02:05:39 +01:00
|
|
|
_ => false,
|
2014-12-26 10:55:16 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
match self.visit_view_path(node, om, item.id, please_inline) {
|
|
|
|
None => return,
|
|
|
|
Some(p) => p
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
node
|
|
|
|
};
|
|
|
|
om.imports.push(Import {
|
|
|
|
id: item.id,
|
2016-03-25 06:08:11 +00:00
|
|
|
vis: item.vis.clone(),
|
2014-12-26 10:55:16 +02:00
|
|
|
attrs: item.attrs.clone(),
|
|
|
|
node: node,
|
|
|
|
whence: item.span,
|
|
|
|
});
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemMod(ref m) => {
|
2014-02-28 17:46:09 -08:00
|
|
|
om.mods.push(self.visit_mod_contents(item.span,
|
2014-05-18 16:56:13 +03:00
|
|
|
item.attrs.clone(),
|
2016-03-25 06:08:11 +00:00
|
|
|
item.vis.clone(),
|
2014-02-28 17:46:09 -08:00
|
|
|
item.id,
|
|
|
|
m,
|
2014-05-18 16:56:13 +03:00
|
|
|
Some(name)));
|
2014-01-07 18:46:16 -08:00
|
|
|
},
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemEnum(ref ed, ref gen) =>
|
2014-05-18 16:56:13 +03:00
|
|
|
om.enums.push(self.visit_enum_def(item, name, ed, gen)),
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemStruct(ref sd, ref gen) =>
|
2015-10-25 18:33:51 +03:00
|
|
|
om.structs.push(self.visit_variant_data(item, name, sd, gen)),
|
2016-08-10 21:00:17 +03:00
|
|
|
hir::ItemUnion(ref sd, ref gen) =>
|
|
|
|
om.unions.push(self.visit_union_data(item, name, sd, gen)),
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemFn(ref fd, ref unsafety, constness, ref abi, ref gen, _) =>
|
2015-02-25 22:05:07 +02:00
|
|
|
om.fns.push(self.visit_fn(item, name, &**fd, unsafety,
|
|
|
|
constness, abi, gen)),
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemTy(ref ty, ref gen) => {
|
2014-01-07 18:46:16 -08:00
|
|
|
let t = Typedef {
|
2014-05-18 16:56:13 +03:00
|
|
|
ty: ty.clone(),
|
2014-01-07 18:46:16 -08:00
|
|
|
gen: gen.clone(),
|
2014-05-18 16:56:13 +03:00
|
|
|
name: name,
|
2014-01-07 18:46:16 -08:00
|
|
|
id: item.id,
|
2014-05-18 16:56:13 +03:00
|
|
|
attrs: item.attrs.clone(),
|
2014-08-11 09:32:26 -07:00
|
|
|
whence: item.span,
|
2016-03-25 06:08:11 +00:00
|
|
|
vis: item.vis.clone(),
|
2014-06-26 11:37:39 -07:00
|
|
|
stab: self.stability(item.id),
|
2015-12-12 23:01:27 +03:00
|
|
|
depr: self.deprecation(item.id),
|
2014-01-07 18:46:16 -08:00
|
|
|
};
|
|
|
|
om.typedefs.push(t);
|
|
|
|
},
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemStatic(ref ty, ref mut_, ref exp) => {
|
2014-01-07 18:46:16 -08:00
|
|
|
let s = Static {
|
2014-05-18 16:56:13 +03:00
|
|
|
type_: ty.clone(),
|
2014-01-07 18:46:16 -08:00
|
|
|
mutability: mut_.clone(),
|
|
|
|
expr: exp.clone(),
|
|
|
|
id: item.id,
|
2014-05-18 16:56:13 +03:00
|
|
|
name: name,
|
|
|
|
attrs: item.attrs.clone(),
|
2014-08-11 09:32:26 -07:00
|
|
|
whence: item.span,
|
2016-03-25 06:08:11 +00:00
|
|
|
vis: item.vis.clone(),
|
2014-06-26 11:37:39 -07:00
|
|
|
stab: self.stability(item.id),
|
2015-12-12 23:01:27 +03:00
|
|
|
depr: self.deprecation(item.id),
|
2014-01-07 18:46:16 -08:00
|
|
|
};
|
|
|
|
om.statics.push(s);
|
|
|
|
},
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemConst(ref ty, ref exp) => {
|
2014-10-06 17:41:15 -07:00
|
|
|
let s = Constant {
|
|
|
|
type_: ty.clone(),
|
|
|
|
expr: exp.clone(),
|
|
|
|
id: item.id,
|
|
|
|
name: name,
|
|
|
|
attrs: item.attrs.clone(),
|
|
|
|
whence: item.span,
|
2016-03-25 06:08:11 +00:00
|
|
|
vis: item.vis.clone(),
|
2014-10-06 17:41:15 -07:00
|
|
|
stab: self.stability(item.id),
|
2015-12-12 23:01:27 +03:00
|
|
|
depr: self.deprecation(item.id),
|
2014-10-06 17:41:15 -07:00
|
|
|
};
|
|
|
|
om.constants.push(s);
|
|
|
|
},
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemTrait(unsafety, ref gen, ref b, ref items) => {
|
2014-01-07 18:46:16 -08:00
|
|
|
let t = Trait {
|
2014-12-09 19:59:20 -05:00
|
|
|
unsafety: unsafety,
|
2014-05-18 16:56:13 +03:00
|
|
|
name: name,
|
|
|
|
items: items.clone(),
|
2014-01-07 18:46:16 -08:00
|
|
|
generics: gen.clone(),
|
2015-02-13 07:33:44 +00:00
|
|
|
bounds: b.iter().cloned().collect(),
|
2014-01-07 18:46:16 -08:00
|
|
|
id: item.id,
|
2014-05-18 16:56:13 +03:00
|
|
|
attrs: item.attrs.clone(),
|
2014-08-11 09:32:26 -07:00
|
|
|
whence: item.span,
|
2016-03-25 06:08:11 +00:00
|
|
|
vis: item.vis.clone(),
|
2014-06-26 11:37:39 -07:00
|
|
|
stab: self.stability(item.id),
|
2015-12-12 23:01:27 +03:00
|
|
|
depr: self.deprecation(item.id),
|
2014-01-07 18:46:16 -08:00
|
|
|
};
|
|
|
|
om.traits.push(t);
|
|
|
|
},
|
2016-11-14 18:24:47 +00:00
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemImpl(unsafety, polarity, ref gen, ref tr, ref ty, ref items) => {
|
2016-11-14 18:24:47 +00:00
|
|
|
// Don't duplicate impls when inlining, we'll pick them up
|
|
|
|
// regardless of where they're located.
|
|
|
|
if !self.inlining {
|
|
|
|
let i = Impl {
|
|
|
|
unsafety: unsafety,
|
|
|
|
polarity: polarity,
|
|
|
|
generics: gen.clone(),
|
|
|
|
trait_: tr.clone(),
|
|
|
|
for_: ty.clone(),
|
|
|
|
items: items.clone(),
|
|
|
|
attrs: item.attrs.clone(),
|
|
|
|
id: item.id,
|
|
|
|
whence: item.span,
|
|
|
|
vis: item.vis.clone(),
|
|
|
|
stab: self.stability(item.id),
|
|
|
|
depr: self.deprecation(item.id),
|
|
|
|
};
|
2015-04-07 11:08:21 -07:00
|
|
|
om.impls.push(i);
|
|
|
|
}
|
2014-01-07 18:46:16 -08:00
|
|
|
},
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ItemDefaultImpl(unsafety, ref trait_ref) => {
|
2016-11-14 18:24:47 +00:00
|
|
|
// See comment above about ItemImpl.
|
|
|
|
if !self.inlining {
|
|
|
|
let i = DefaultImpl {
|
|
|
|
unsafety: unsafety,
|
|
|
|
trait_: trait_ref.clone(),
|
|
|
|
id: item.id,
|
|
|
|
attrs: item.attrs.clone(),
|
|
|
|
whence: item.span,
|
|
|
|
};
|
2015-04-07 11:08:21 -07:00
|
|
|
om.def_traits.push(i);
|
|
|
|
}
|
2015-01-27 00:35:03 +01:00
|
|
|
}
|
2014-01-07 18:46:16 -08:00
|
|
|
}
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
2014-07-17 09:45:31 -07:00
|
|
|
|
|
|
|
// convert each exported_macro into a doc item
|
2015-07-31 00:04:06 -07:00
|
|
|
fn visit_macro(&self, def: &hir::MacroDef) -> Macro {
|
2015-11-26 19:14:36 +01:00
|
|
|
// Extract the spans of all matchers. They represent the "interface" of the macro.
|
|
|
|
let matchers = def.body.chunks(4).map(|arm| arm[0].get_span()).collect();
|
|
|
|
|
2014-07-17 09:45:31 -07:00
|
|
|
Macro {
|
2014-12-30 19:10:46 -08:00
|
|
|
id: def.id,
|
|
|
|
attrs: def.attrs.clone(),
|
2015-09-20 14:51:40 +03:00
|
|
|
name: def.name,
|
2014-12-30 19:10:46 -08:00
|
|
|
whence: def.span,
|
2015-11-26 19:14:36 +01:00
|
|
|
matchers: matchers,
|
2014-12-30 19:10:46 -08:00
|
|
|
stab: self.stability(def.id),
|
2015-12-12 23:01:27 +03:00
|
|
|
depr: self.deprecation(def.id),
|
2015-04-13 15:25:40 -07:00
|
|
|
imported_from: def.imported_from,
|
2014-07-17 09:45:31 -07:00
|
|
|
}
|
|
|
|
}
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|