2014-05-24 11:56:38 -07: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.
|
|
|
|
|
|
|
|
//! Support for inlining external documentation into the current AST.
|
|
|
|
|
2015-04-07 16:18:49 -07:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2014-05-24 11:56:38 -07:00
|
|
|
use syntax::ast;
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::attr::AttrMetaMethods;
|
2015-07-31 00:04:06 -07:00
|
|
|
use rustc_front::hir;
|
2014-05-24 11:56:38 -07:00
|
|
|
|
2015-11-22 21:02:04 +02:00
|
|
|
use rustc::metadata::util::{self as mdutil, CrateStore};
|
2014-05-14 15:31:30 -04:00
|
|
|
use rustc::middle::def;
|
2015-08-16 06:32:28 -04:00
|
|
|
use rustc::middle::def_id::DefId;
|
2014-05-24 11:56:38 -07:00
|
|
|
use rustc::middle::ty;
|
2014-07-25 09:07:20 -07:00
|
|
|
use rustc::middle::subst;
|
2014-06-26 11:37:39 -07:00
|
|
|
use rustc::middle::stability;
|
2015-03-15 19:35:25 -06:00
|
|
|
use rustc::middle::const_eval;
|
2014-05-24 11:56:38 -07:00
|
|
|
|
2014-09-06 19:13:40 +03:00
|
|
|
use core::DocContext;
|
2014-05-24 11:56:38 -07:00
|
|
|
use doctree;
|
|
|
|
use clean;
|
|
|
|
|
2015-03-15 19:35:25 -06:00
|
|
|
use super::{Clean, ToSource};
|
2014-05-24 11:56:38 -07:00
|
|
|
|
|
|
|
/// Attempt to inline the definition of a local node id into this AST.
|
|
|
|
///
|
|
|
|
/// This function will fetch the definition of the id specified, and if it is
|
|
|
|
/// from another crate it will attempt to inline the documentation from the
|
|
|
|
/// other crate into this crate.
|
|
|
|
///
|
|
|
|
/// This is primarily used for `pub use` statements which are, in general,
|
|
|
|
/// implementation details. Inlining the documentation should help provide a
|
|
|
|
/// better experience when reading the documentation in this use case.
|
|
|
|
///
|
|
|
|
/// The returned value is `None` if the `id` could not be inlined, and `Some`
|
|
|
|
/// of a vector of items if it was successfully expanded.
|
2015-09-20 14:51:40 +03:00
|
|
|
pub fn try_inline(cx: &DocContext, id: ast::NodeId, into: Option<ast::Name>)
|
2014-07-25 10:16:41 -07:00
|
|
|
-> Option<Vec<clean::Item>> {
|
2014-09-06 19:13:40 +03:00
|
|
|
let tcx = match cx.tcx_opt() {
|
|
|
|
Some(tcx) => tcx,
|
|
|
|
None => return None,
|
2014-05-24 11:56:38 -07:00
|
|
|
};
|
2014-11-06 12:25:16 -05:00
|
|
|
let def = match tcx.def_map.borrow().get(&id) {
|
2015-02-17 06:44:23 +02:00
|
|
|
Some(d) => d.full_def(),
|
2014-05-24 11:56:38 -07:00
|
|
|
None => return None,
|
|
|
|
};
|
2014-05-14 15:31:30 -04:00
|
|
|
let did = def.def_id();
|
2015-08-16 06:32:28 -04:00
|
|
|
if did.is_local() { return None }
|
2014-09-06 19:13:40 +03:00
|
|
|
try_inline_def(cx, tcx, def).map(|vec| {
|
2014-09-14 20:27:36 -07:00
|
|
|
vec.into_iter().map(|mut item| {
|
2014-07-25 10:16:41 -07:00
|
|
|
match into {
|
|
|
|
Some(into) if item.name.is_some() => {
|
2014-09-06 19:13:40 +03:00
|
|
|
item.name = Some(into.clean(cx));
|
2014-07-25 10:16:41 -07:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
item
|
|
|
|
}).collect()
|
|
|
|
})
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
|
2014-09-06 19:13:40 +03:00
|
|
|
fn try_inline_def(cx: &DocContext, tcx: &ty::ctxt,
|
2014-05-14 15:31:30 -04:00
|
|
|
def: def::Def) -> Option<Vec<clean::Item>> {
|
2014-05-24 11:56:38 -07:00
|
|
|
let mut ret = Vec::new();
|
2014-05-14 15:31:30 -04:00
|
|
|
let did = def.def_id();
|
2014-05-24 11:56:38 -07:00
|
|
|
let inner = match def {
|
2015-02-24 07:45:34 +02:00
|
|
|
def::DefTrait(did) => {
|
2014-05-24 11:56:38 -07:00
|
|
|
record_extern_fqn(cx, did, clean::TypeTrait);
|
2014-09-06 19:13:40 +03:00
|
|
|
clean::TraitItem(build_external_trait(cx, tcx, did))
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
2014-10-31 15:00:35 +13:00
|
|
|
def::DefFn(did, false) => {
|
2014-05-29 15:52:03 -07:00
|
|
|
// If this function is a tuple struct constructor, we just skip it
|
2014-05-24 11:56:38 -07:00
|
|
|
record_extern_fqn(cx, did, clean::TypeFunction);
|
2014-10-31 15:00:35 +13:00
|
|
|
clean::FunctionItem(build_external_function(cx, tcx, did))
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
2014-05-14 15:31:30 -04:00
|
|
|
def::DefStruct(did) => {
|
2014-05-24 11:56:38 -07:00
|
|
|
record_extern_fqn(cx, did, clean::TypeStruct);
|
2015-06-10 17:22:20 +01:00
|
|
|
ret.extend(build_impls(cx, tcx, did));
|
2014-09-06 19:13:40 +03:00
|
|
|
clean::StructItem(build_struct(cx, tcx, did))
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
2014-09-16 09:13:00 +12:00
|
|
|
def::DefTy(did, false) => {
|
|
|
|
record_extern_fqn(cx, did, clean::TypeTypedef);
|
2015-06-10 17:22:20 +01:00
|
|
|
ret.extend(build_impls(cx, tcx, did));
|
2014-09-16 09:13:00 +12:00
|
|
|
build_type(cx, tcx, did)
|
|
|
|
}
|
|
|
|
def::DefTy(did, true) => {
|
2014-05-24 11:56:38 -07:00
|
|
|
record_extern_fqn(cx, did, clean::TypeEnum);
|
2015-06-10 17:22:20 +01:00
|
|
|
ret.extend(build_impls(cx, tcx, did));
|
2014-09-06 19:13:40 +03:00
|
|
|
build_type(cx, tcx, did)
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
// Assume that the enum type is reexported next to the variant, and
|
|
|
|
// variants don't show up in documentation specially.
|
2014-05-14 15:31:30 -04:00
|
|
|
def::DefVariant(..) => return Some(Vec::new()),
|
|
|
|
def::DefMod(did) => {
|
2014-05-24 11:56:38 -07:00
|
|
|
record_extern_fqn(cx, did, clean::TypeModule);
|
|
|
|
clean::ModuleItem(build_module(cx, tcx, did))
|
|
|
|
}
|
2014-06-06 20:17:19 -07:00
|
|
|
def::DefStatic(did, mtbl) => {
|
2014-06-05 17:20:59 -07:00
|
|
|
record_extern_fqn(cx, did, clean::TypeStatic);
|
2014-09-06 19:13:40 +03:00
|
|
|
clean::StaticItem(build_static(cx, tcx, did, mtbl))
|
2014-06-05 17:20:59 -07:00
|
|
|
}
|
2015-08-04 01:16:53 +03:00
|
|
|
def::DefConst(did) | def::DefAssociatedConst(did) => {
|
2014-12-12 04:13:36 -08:00
|
|
|
record_extern_fqn(cx, did, clean::TypeConst);
|
|
|
|
clean::ConstantItem(build_const(cx, tcx, did))
|
|
|
|
}
|
2014-05-24 11:56:38 -07:00
|
|
|
_ => return None,
|
|
|
|
};
|
2014-08-18 17:52:38 -07:00
|
|
|
cx.inlined.borrow_mut().as_mut().unwrap().insert(did);
|
2014-05-24 11:56:38 -07:00
|
|
|
ret.push(clean::Item {
|
|
|
|
source: clean::Span::empty(),
|
2015-08-12 20:22:25 +02:00
|
|
|
name: Some(tcx.item_name(did).to_string()),
|
2014-09-06 19:13:40 +03:00
|
|
|
attrs: load_attrs(cx, tcx, did),
|
2014-05-24 11:56:38 -07:00
|
|
|
inner: inner,
|
2015-07-31 00:04:06 -07:00
|
|
|
visibility: Some(hir::Public),
|
2014-09-06 19:13:40 +03:00
|
|
|
stability: stability::lookup(tcx, did).clean(cx),
|
2014-05-24 11:56:38 -07:00
|
|
|
def_id: did,
|
|
|
|
});
|
|
|
|
Some(ret)
|
|
|
|
}
|
|
|
|
|
2014-09-06 19:13:40 +03:00
|
|
|
pub fn load_attrs(cx: &DocContext, tcx: &ty::ctxt,
|
2015-08-16 06:32:28 -04:00
|
|
|
did: DefId) -> Vec<clean::Attribute> {
|
2015-11-22 21:02:04 +02:00
|
|
|
tcx.get_attrs(did).iter().map(|a| a.clean(cx)).collect()
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Record an external fully qualified name in the external_paths cache.
|
|
|
|
///
|
|
|
|
/// These names are used later on by HTML rendering to generate things like
|
|
|
|
/// source links back to the original item.
|
2015-08-16 06:32:28 -04:00
|
|
|
pub fn record_extern_fqn(cx: &DocContext, did: DefId, kind: clean::TypeKind) {
|
2014-09-06 19:13:40 +03:00
|
|
|
match cx.tcx_opt() {
|
|
|
|
Some(tcx) => {
|
2015-11-22 21:02:04 +02:00
|
|
|
let fqn = tcx.sess.cstore.item_path(did);
|
2014-09-14 20:27:36 -07:00
|
|
|
let fqn = fqn.into_iter().map(|i| i.to_string()).collect();
|
2014-08-18 17:52:38 -07:00
|
|
|
cx.external_paths.borrow_mut().as_mut().unwrap().insert(did, (fqn, kind));
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
2014-09-06 19:13:40 +03:00
|
|
|
None => {}
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 19:13:40 +03:00
|
|
|
pub fn build_external_trait(cx: &DocContext, tcx: &ty::ctxt,
|
2015-08-16 06:32:28 -04:00
|
|
|
did: DefId) -> clean::Trait {
|
2015-06-25 23:42:17 +03:00
|
|
|
let def = tcx.lookup_trait_def(did);
|
|
|
|
let trait_items = tcx.trait_items(did).clean(cx);
|
|
|
|
let predicates = tcx.lookup_predicates(did);
|
2015-04-07 00:16:35 -07:00
|
|
|
let generics = (&def.generics, &predicates, subst::TypeSpace).clean(cx);
|
|
|
|
let generics = filter_non_trait_generics(did, generics);
|
|
|
|
let (generics, supertrait_bounds) = separate_supertrait_bounds(generics);
|
2014-05-24 11:56:38 -07:00
|
|
|
clean::Trait {
|
2014-12-09 19:59:20 -05:00
|
|
|
unsafety: def.unsafety,
|
2015-04-07 00:16:35 -07:00
|
|
|
generics: generics,
|
2015-03-10 12:28:44 +02:00
|
|
|
items: trait_items,
|
2015-04-07 00:16:35 -07:00
|
|
|
bounds: supertrait_bounds,
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 06:32:28 -04:00
|
|
|
fn build_external_function(cx: &DocContext, tcx: &ty::ctxt, did: DefId) -> clean::Function {
|
2015-06-25 23:42:17 +03:00
|
|
|
let t = tcx.lookup_item_type(did);
|
2015-04-07 14:22:55 -07:00
|
|
|
let (decl, style, abi) = match t.ty.sty {
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyBareFn(_, ref f) => ((did, &f.sig).clean(cx), f.unsafety, f.abi),
|
2014-10-31 15:00:35 +13:00
|
|
|
_ => panic!("bad function"),
|
|
|
|
};
|
2015-11-19 16:27:17 +01:00
|
|
|
|
|
|
|
let constness = if csearch::is_const_fn(&tcx.sess.cstore, did) {
|
|
|
|
hir::Constness::Const
|
|
|
|
} else {
|
|
|
|
hir::Constness::NotConst
|
|
|
|
};
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
let predicates = tcx.lookup_predicates(did);
|
2014-05-24 11:56:38 -07:00
|
|
|
clean::Function {
|
2014-10-31 15:00:35 +13:00
|
|
|
decl: decl,
|
2015-02-11 10:28:52 -05:00
|
|
|
generics: (&t.generics, &predicates, subst::FnSpace).clean(cx),
|
2014-12-09 10:36:46 -05:00
|
|
|
unsafety: style,
|
2015-11-19 16:27:17 +01:00
|
|
|
constness: constness,
|
2015-04-07 14:22:55 -07:00
|
|
|
abi: abi,
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 06:32:28 -04:00
|
|
|
fn build_struct(cx: &DocContext, tcx: &ty::ctxt, did: DefId) -> clean::Struct {
|
2014-05-24 11:56:38 -07:00
|
|
|
use syntax::parse::token::special_idents::unnamed_field;
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
let t = tcx.lookup_item_type(did);
|
|
|
|
let predicates = tcx.lookup_predicates(did);
|
2015-08-02 22:52:50 +03:00
|
|
|
let variant = tcx.lookup_adt_def(did).struct_variant();
|
2014-05-24 11:56:38 -07:00
|
|
|
|
|
|
|
clean::Struct {
|
2015-08-02 22:52:50 +03:00
|
|
|
struct_type: match &*variant.fields {
|
2014-05-24 11:56:38 -07:00
|
|
|
[] => doctree::Unit,
|
|
|
|
[ref f] if f.name == unnamed_field.name => doctree::Newtype,
|
|
|
|
[ref f, ..] if f.name == unnamed_field.name => doctree::Tuple,
|
|
|
|
_ => doctree::Plain,
|
|
|
|
},
|
2015-02-11 10:28:52 -05:00
|
|
|
generics: (&t.generics, &predicates, subst::TypeSpace).clean(cx),
|
2015-08-02 22:52:50 +03:00
|
|
|
fields: variant.fields.clean(cx),
|
2014-05-24 11:56:38 -07:00
|
|
|
fields_stripped: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 06:32:28 -04:00
|
|
|
fn build_type(cx: &DocContext, tcx: &ty::ctxt, did: DefId) -> clean::ItemEnum {
|
2015-06-25 23:42:17 +03:00
|
|
|
let t = tcx.lookup_item_type(did);
|
|
|
|
let predicates = tcx.lookup_predicates(did);
|
2014-10-31 10:51:16 +02:00
|
|
|
match t.ty.sty {
|
2015-11-22 21:02:04 +02:00
|
|
|
ty::TyEnum(edef, _) if !tcx.sess.cstore.is_typedef(did) => {
|
2014-05-24 11:56:38 -07:00
|
|
|
return clean::EnumItem(clean::Enum {
|
2015-02-11 10:28:52 -05:00
|
|
|
generics: (&t.generics, &predicates, subst::TypeSpace).clean(cx),
|
2014-05-24 11:56:38 -07:00
|
|
|
variants_stripped: false,
|
2015-08-02 22:52:50 +03:00
|
|
|
variants: edef.variants.clean(cx),
|
2014-05-24 11:56:38 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
clean::TypedefItem(clean::Typedef {
|
2014-09-06 19:13:40 +03:00
|
|
|
type_: t.ty.clean(cx),
|
2015-02-11 10:28:52 -05:00
|
|
|
generics: (&t.generics, &predicates, subst::TypeSpace).clean(cx),
|
2015-05-21 14:17:37 +02:00
|
|
|
}, false)
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
|
2015-04-13 16:23:32 -07:00
|
|
|
pub fn build_impls(cx: &DocContext, tcx: &ty::ctxt,
|
2015-08-16 06:32:28 -04:00
|
|
|
did: DefId) -> Vec<clean::Item> {
|
2015-06-25 23:42:17 +03:00
|
|
|
tcx.populate_inherent_implementations_for_type_if_necessary(did);
|
2014-05-24 11:56:38 -07:00
|
|
|
let mut impls = Vec::new();
|
|
|
|
|
2014-11-06 12:25:16 -05:00
|
|
|
match tcx.inherent_impls.borrow().get(&did) {
|
2014-05-24 11:56:38 -07:00
|
|
|
None => {}
|
|
|
|
Some(i) => {
|
2015-06-11 13:56:07 +01:00
|
|
|
for &did in i.iter() {
|
2015-04-13 16:23:32 -07:00
|
|
|
build_impl(cx, tcx, did, &mut impls);
|
|
|
|
}
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 01:35:44 -07:00
|
|
|
// If this is the first time we've inlined something from this crate, then
|
|
|
|
// we inline *all* impls from the crate into this crate. Note that there's
|
|
|
|
// currently no way for us to filter this based on type, and we likely need
|
|
|
|
// many impls for a variety of reasons.
|
|
|
|
//
|
|
|
|
// Primarily, the impls will be used to populate the documentation for this
|
|
|
|
// type being inlined, but impls can also be used when generating
|
|
|
|
// documentation for primitives (no way to find those specifically).
|
|
|
|
if cx.populated_crate_impls.borrow_mut().insert(did.krate) {
|
2015-11-22 21:02:04 +02:00
|
|
|
for item in tcx.sess.cstore.crate_top_level_items(did.krate) {
|
|
|
|
populate_impls(cx, tcx, item.def, &mut impls);
|
|
|
|
}
|
2014-05-29 01:35:44 -07:00
|
|
|
|
2014-09-06 19:13:40 +03:00
|
|
|
fn populate_impls(cx: &DocContext, tcx: &ty::ctxt,
|
2015-11-22 21:02:04 +02:00
|
|
|
def: mdutil::DefLike,
|
2015-04-13 16:23:32 -07:00
|
|
|
impls: &mut Vec<clean::Item>) {
|
2014-05-29 01:35:44 -07:00
|
|
|
match def {
|
2015-11-22 21:02:04 +02:00
|
|
|
mdutil::DlImpl(did) => build_impl(cx, tcx, did, impls),
|
|
|
|
mdutil::DlDef(def::DefMod(did)) => {
|
|
|
|
for item in tcx.sess.cstore.item_children(did) {
|
|
|
|
populate_impls(cx, tcx, item.def, impls)
|
|
|
|
}
|
2014-05-29 01:35:44 -07:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 16:23:32 -07:00
|
|
|
return impls;
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
|
2015-04-13 16:23:32 -07:00
|
|
|
pub fn build_impl(cx: &DocContext,
|
|
|
|
tcx: &ty::ctxt,
|
2015-08-16 06:32:28 -04:00
|
|
|
did: DefId,
|
2015-04-13 16:23:32 -07:00
|
|
|
ret: &mut Vec<clean::Item>) {
|
2014-08-18 17:52:38 -07:00
|
|
|
if !cx.inlined.borrow_mut().as_mut().unwrap().insert(did) {
|
2015-04-13 16:23:32 -07:00
|
|
|
return
|
2014-06-01 11:16:18 -07:00
|
|
|
}
|
|
|
|
|
2015-03-12 19:15:52 -07:00
|
|
|
let attrs = load_attrs(cx, tcx, did);
|
2015-11-22 21:02:04 +02:00
|
|
|
let associated_trait = tcx.impl_trait_ref(did);
|
2015-03-12 19:15:52 -07:00
|
|
|
if let Some(ref t) = associated_trait {
|
|
|
|
// If this is an impl for a #[doc(hidden)] trait, be sure to not inline
|
|
|
|
let trait_attrs = load_attrs(cx, tcx, t.def_id);
|
|
|
|
if trait_attrs.iter().any(|a| is_doc_hidden(a)) {
|
2015-04-13 16:23:32 -07:00
|
|
|
return
|
2014-07-25 08:26:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 19:15:52 -07:00
|
|
|
// If this is a defaulted impl, then bail out early here
|
2015-11-22 21:02:04 +02:00
|
|
|
if tcx.sess.cstore.is_default_impl(did) {
|
2015-04-13 16:23:32 -07:00
|
|
|
return ret.push(clean::Item {
|
2015-03-12 19:15:52 -07:00
|
|
|
inner: clean::DefaultImplItem(clean::DefaultImpl {
|
|
|
|
// FIXME: this should be decoded
|
2015-07-31 00:04:06 -07:00
|
|
|
unsafety: hir::Unsafety::Normal,
|
2015-03-12 19:15:52 -07:00
|
|
|
trait_: match associated_trait.as_ref().unwrap().clean(cx) {
|
|
|
|
clean::TraitBound(polyt, _) => polyt.trait_,
|
|
|
|
clean::RegionBound(..) => unreachable!(),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
source: clean::Span::empty(),
|
|
|
|
name: None,
|
|
|
|
attrs: attrs,
|
2015-07-31 00:04:06 -07:00
|
|
|
visibility: Some(hir::Inherited),
|
2015-03-12 19:15:52 -07:00
|
|
|
stability: stability::lookup(tcx, did).clean(cx),
|
|
|
|
def_id: did,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
let predicates = tcx.lookup_predicates(did);
|
2015-11-22 21:02:04 +02:00
|
|
|
let trait_items = tcx.sess.cstore.impl_items(did)
|
2014-08-04 13:56:56 -07:00
|
|
|
.iter()
|
|
|
|
.filter_map(|did| {
|
|
|
|
let did = did.def_id();
|
2015-06-25 23:42:17 +03:00
|
|
|
let impl_item = tcx.impl_or_trait_item(did);
|
2014-08-04 13:56:56 -07:00
|
|
|
match impl_item {
|
2015-03-15 19:35:25 -06:00
|
|
|
ty::ConstTraitItem(ref assoc_const) => {
|
|
|
|
let did = assoc_const.def_id;
|
2015-06-25 23:42:17 +03:00
|
|
|
let type_scheme = tcx.lookup_item_type(did);
|
2015-09-30 22:28:27 +03:00
|
|
|
let default = if assoc_const.has_value {
|
|
|
|
Some(const_eval::lookup_const_by_id(tcx, did, None)
|
|
|
|
.unwrap().span.to_src(cx))
|
|
|
|
} else {
|
|
|
|
None
|
2015-03-15 19:35:25 -06:00
|
|
|
};
|
|
|
|
Some(clean::Item {
|
|
|
|
name: Some(assoc_const.name.clean(cx)),
|
|
|
|
inner: clean::AssociatedConstItem(
|
|
|
|
type_scheme.ty.clean(cx),
|
|
|
|
default,
|
|
|
|
),
|
|
|
|
source: clean::Span::empty(),
|
|
|
|
attrs: vec![],
|
|
|
|
visibility: None,
|
|
|
|
stability: stability::lookup(tcx, did).clean(cx),
|
|
|
|
def_id: did
|
|
|
|
})
|
|
|
|
}
|
2014-08-04 13:56:56 -07:00
|
|
|
ty::MethodTraitItem(method) => {
|
2015-07-31 00:04:06 -07:00
|
|
|
if method.vis != hir::Public && associated_trait.is_none() {
|
2014-08-04 13:56:56 -07:00
|
|
|
return None
|
|
|
|
}
|
2014-09-06 19:13:40 +03:00
|
|
|
let mut item = method.clean(cx);
|
2014-08-04 13:56:56 -07:00
|
|
|
item.inner = match item.inner.clone() {
|
|
|
|
clean::TyMethodItem(clean::TyMethod {
|
2015-02-06 00:51:38 -08:00
|
|
|
unsafety, decl, self_, generics, abi
|
2014-08-04 13:56:56 -07:00
|
|
|
}) => {
|
2015-11-19 16:27:17 +01:00
|
|
|
let constness = if csearch::is_const_fn(&tcx.sess.cstore, did) {
|
|
|
|
hir::Constness::Const
|
|
|
|
} else {
|
|
|
|
hir::Constness::NotConst
|
|
|
|
};
|
|
|
|
|
2014-08-04 13:56:56 -07:00
|
|
|
clean::MethodItem(clean::Method {
|
2014-12-09 10:36:46 -05:00
|
|
|
unsafety: unsafety,
|
2015-11-19 16:27:17 +01:00
|
|
|
constness: constness,
|
2014-08-04 13:56:56 -07:00
|
|
|
decl: decl,
|
|
|
|
self_: self_,
|
|
|
|
generics: generics,
|
2015-02-06 00:51:38 -08:00
|
|
|
abi: abi
|
2014-08-04 13:56:56 -07:00
|
|
|
})
|
|
|
|
}
|
2014-10-09 15:17:22 -04:00
|
|
|
_ => panic!("not a tymethod"),
|
2014-08-04 13:56:56 -07:00
|
|
|
};
|
|
|
|
Some(item)
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
2015-01-17 22:39:01 -08:00
|
|
|
ty::TypeTraitItem(ref assoc_ty) => {
|
|
|
|
let did = assoc_ty.def_id;
|
2015-05-26 17:12:39 +03:00
|
|
|
let type_scheme = ty::TypeScheme {
|
|
|
|
ty: assoc_ty.ty.unwrap(),
|
|
|
|
generics: ty::Generics::empty()
|
|
|
|
};
|
2015-04-06 15:10:55 -07:00
|
|
|
// Not sure the choice of ParamSpace actually matters here,
|
|
|
|
// because an associated type won't have generics on the LHS
|
2015-05-26 17:12:39 +03:00
|
|
|
let typedef = (type_scheme, ty::GenericPredicates::empty(),
|
2015-04-06 15:10:55 -07:00
|
|
|
subst::ParamSpace::TypeSpace).clean(cx);
|
2015-01-17 22:39:01 -08:00
|
|
|
Some(clean::Item {
|
|
|
|
name: Some(assoc_ty.name.clean(cx)),
|
2015-05-21 14:17:37 +02:00
|
|
|
inner: clean::TypedefItem(typedef, true),
|
2015-01-17 22:39:01 -08:00
|
|
|
source: clean::Span::empty(),
|
|
|
|
attrs: vec![],
|
|
|
|
visibility: None,
|
|
|
|
stability: stability::lookup(tcx, did).clean(cx),
|
|
|
|
def_id: did
|
|
|
|
})
|
2014-08-05 19:44:21 -07:00
|
|
|
}
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
2015-04-13 16:23:32 -07:00
|
|
|
}).collect::<Vec<_>>();
|
2015-11-22 21:02:04 +02:00
|
|
|
let polarity = tcx.trait_impl_polarity(did);
|
2015-06-25 23:42:17 +03:00
|
|
|
let ty = tcx.lookup_item_type(did);
|
2015-04-13 16:23:32 -07:00
|
|
|
let trait_ = associated_trait.clean(cx).map(|bound| {
|
|
|
|
match bound {
|
|
|
|
clean::TraitBound(polyt, _) => polyt.trait_,
|
|
|
|
clean::RegionBound(..) => unreachable!(),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if let Some(clean::ResolvedPath { did, .. }) = trait_ {
|
|
|
|
if Some(did) == cx.deref_trait_did.get() {
|
|
|
|
super::build_deref_target_impls(cx, &trait_items, ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.push(clean::Item {
|
2014-05-24 11:56:38 -07:00
|
|
|
inner: clean::ImplItem(clean::Impl {
|
2015-07-31 00:04:06 -07:00
|
|
|
unsafety: hir::Unsafety::Normal, // FIXME: this should be decoded
|
2015-02-01 21:53:25 -05:00
|
|
|
derived: clean::detect_derived(&attrs),
|
2015-04-13 16:23:32 -07:00
|
|
|
trait_: trait_,
|
2014-09-06 19:13:40 +03:00
|
|
|
for_: ty.ty.clean(cx),
|
2015-02-11 10:28:52 -05:00
|
|
|
generics: (&ty.generics, &predicates, subst::TypeSpace).clean(cx),
|
2014-08-04 13:56:56 -07:00
|
|
|
items: trait_items,
|
2015-01-21 04:35:57 +00:00
|
|
|
polarity: polarity.map(|p| { p.clean(cx) }),
|
2014-05-24 11:56:38 -07:00
|
|
|
}),
|
|
|
|
source: clean::Span::empty(),
|
|
|
|
name: None,
|
|
|
|
attrs: attrs,
|
2015-07-31 00:04:06 -07:00
|
|
|
visibility: Some(hir::Inherited),
|
2014-09-06 19:13:40 +03:00
|
|
|
stability: stability::lookup(tcx, did).clean(cx),
|
2014-05-24 11:56:38 -07:00
|
|
|
def_id: did,
|
2014-07-25 08:26:17 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
fn is_doc_hidden(a: &clean::Attribute) -> bool {
|
|
|
|
match *a {
|
2014-11-27 14:19:27 -05:00
|
|
|
clean::List(ref name, ref inner) if *name == "doc" => {
|
2014-07-25 08:26:17 -07:00
|
|
|
inner.iter().any(|a| {
|
|
|
|
match *a {
|
2014-11-27 14:19:27 -05:00
|
|
|
clean::Word(ref s) => *s == "hidden",
|
2014-07-25 08:26:17 -07:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
|
2014-09-06 19:13:40 +03:00
|
|
|
fn build_module(cx: &DocContext, tcx: &ty::ctxt,
|
2015-08-16 06:32:28 -04:00
|
|
|
did: DefId) -> clean::Module {
|
2014-05-24 11:56:38 -07:00
|
|
|
let mut items = Vec::new();
|
2014-07-25 10:37:23 -07:00
|
|
|
fill_in(cx, tcx, did, &mut items);
|
|
|
|
return clean::Module {
|
|
|
|
items: items,
|
|
|
|
is_crate: false,
|
|
|
|
};
|
2014-05-24 11:56:38 -07:00
|
|
|
|
2015-08-16 06:32:28 -04:00
|
|
|
fn fill_in(cx: &DocContext, tcx: &ty::ctxt, did: DefId,
|
2014-07-25 10:37:23 -07:00
|
|
|
items: &mut Vec<clean::Item>) {
|
2015-04-07 16:18:49 -07:00
|
|
|
// If we're reexporting a reexport it may actually reexport something in
|
|
|
|
// two namespaces, so the target may be listed twice. Make sure we only
|
|
|
|
// visit each node at most once.
|
|
|
|
let mut visited = HashSet::new();
|
2015-11-22 21:02:04 +02:00
|
|
|
for item in tcx.sess.cstore.item_children(did) {
|
|
|
|
match item.def {
|
|
|
|
mdutil::DlDef(def::DefForeignMod(did)) => {
|
2014-07-25 10:37:23 -07:00
|
|
|
fill_in(cx, tcx, did, items);
|
|
|
|
}
|
2015-11-22 21:02:04 +02:00
|
|
|
mdutil::DlDef(def) if item.vis == hir::Public => {
|
2015-04-07 16:18:49 -07:00
|
|
|
if !visited.insert(def) { return }
|
2014-07-25 10:37:23 -07:00
|
|
|
match try_inline_def(cx, tcx, def) {
|
2015-06-10 17:22:20 +01:00
|
|
|
Some(i) => items.extend(i),
|
2014-07-25 10:37:23 -07:00
|
|
|
None => {}
|
|
|
|
}
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
2015-11-22 21:02:04 +02:00
|
|
|
mdutil::DlDef(..) => {}
|
2014-07-25 10:37:23 -07:00
|
|
|
// All impls were inlined above
|
2015-11-22 21:02:04 +02:00
|
|
|
mdutil::DlImpl(..) => {}
|
|
|
|
mdutil::DlField => panic!("unimplemented field"),
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
2015-11-22 21:02:04 +02:00
|
|
|
}
|
2014-05-24 11:56:38 -07:00
|
|
|
}
|
|
|
|
}
|
2014-06-05 17:20:59 -07:00
|
|
|
|
2014-12-12 04:13:36 -08:00
|
|
|
fn build_const(cx: &DocContext, tcx: &ty::ctxt,
|
2015-08-16 06:32:28 -04:00
|
|
|
did: DefId) -> clean::Constant {
|
2014-12-12 04:13:36 -08:00
|
|
|
use rustc::middle::const_eval;
|
2015-07-31 00:04:06 -07:00
|
|
|
use rustc_front::print::pprust;
|
2014-12-12 04:13:36 -08:00
|
|
|
|
2015-03-15 19:35:25 -06:00
|
|
|
let expr = const_eval::lookup_const_by_id(tcx, did, None).unwrap_or_else(|| {
|
2014-12-20 00:09:35 -08:00
|
|
|
panic!("expected lookup_const_by_id to succeed for {:?}", did);
|
2014-12-12 04:13:36 -08:00
|
|
|
});
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("converting constant expr {:?} to snippet", expr);
|
2014-12-12 04:13:36 -08:00
|
|
|
let sn = pprust::expr_to_string(expr);
|
|
|
|
debug!("got snippet {}", sn);
|
|
|
|
|
|
|
|
clean::Constant {
|
2015-06-25 23:42:17 +03:00
|
|
|
type_: tcx.lookup_item_type(did).ty.clean(cx),
|
2014-12-12 04:13:36 -08:00
|
|
|
expr: sn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 19:13:40 +03:00
|
|
|
fn build_static(cx: &DocContext, tcx: &ty::ctxt,
|
2015-08-16 06:32:28 -04:00
|
|
|
did: DefId,
|
2014-06-05 17:20:59 -07:00
|
|
|
mutable: bool) -> clean::Static {
|
|
|
|
clean::Static {
|
2015-06-25 23:42:17 +03:00
|
|
|
type_: tcx.lookup_item_type(did).ty.clean(cx),
|
2014-06-05 17:20:59 -07:00
|
|
|
mutability: if mutable {clean::Mutable} else {clean::Immutable},
|
|
|
|
expr: "\n\n\n".to_string(), // trigger the "[definition]" links
|
|
|
|
}
|
|
|
|
}
|
2015-04-07 00:16:35 -07:00
|
|
|
|
|
|
|
/// A trait's generics clause actually contains all of the predicates for all of
|
|
|
|
/// its associated types as well. We specifically move these clauses to the
|
|
|
|
/// associated types instead when displaying, so when we're genering the
|
|
|
|
/// generics for the trait itself we need to be sure to remove them.
|
|
|
|
///
|
|
|
|
/// The inverse of this filtering logic can be found in the `Clean`
|
|
|
|
/// implementation for `AssociatedType`
|
2015-08-16 06:32:28 -04:00
|
|
|
fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics)
|
2015-04-07 00:16:35 -07:00
|
|
|
-> clean::Generics {
|
|
|
|
g.where_predicates.retain(|pred| {
|
|
|
|
match *pred {
|
|
|
|
clean::WherePredicate::BoundPredicate {
|
|
|
|
ty: clean::QPath {
|
|
|
|
self_type: box clean::Generic(ref s),
|
|
|
|
trait_: box clean::ResolvedPath { did, .. },
|
|
|
|
name: ref _name,
|
|
|
|
}, ..
|
|
|
|
} => *s != "Self" || did != trait_did,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return g;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Supertrait bounds for a trait are also listed in the generics coming from
|
|
|
|
/// the metadata for a crate, so we want to separate those out and create a new
|
|
|
|
/// list of explicit supertrait bounds to render nicely.
|
|
|
|
fn separate_supertrait_bounds(mut g: clean::Generics)
|
|
|
|
-> (clean::Generics, Vec<clean::TyParamBound>) {
|
|
|
|
let mut ty_bounds = Vec::new();
|
|
|
|
g.where_predicates.retain(|pred| {
|
|
|
|
match *pred {
|
|
|
|
clean::WherePredicate::BoundPredicate {
|
|
|
|
ty: clean::Generic(ref s),
|
|
|
|
ref bounds
|
|
|
|
} if *s == "Self" => {
|
|
|
|
ty_bounds.extend(bounds.iter().cloned());
|
|
|
|
false
|
|
|
|
}
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
(g, ty_bounds)
|
|
|
|
}
|