]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/clean/inline.rs
fe93dd16ffda91e3aefd3408c0fbc54b67dbf9ea
[rust.git] / src / librustdoc / clean / inline.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 //! Support for inlining external documentation into the current AST.
12
13 use std::iter::once;
14
15 use syntax::ast;
16 use syntax::ext::base::MacroKind;
17 use syntax_pos::Span;
18
19 use rustc::hir;
20 use rustc::hir::def::{Def, CtorKind};
21 use rustc::hir::def_id::DefId;
22 use rustc::middle::cstore::LoadedMacro;
23 use rustc::ty;
24 use rustc::util::nodemap::FxHashSet;
25
26 use core::{DocContext, DocAccessLevels};
27 use doctree;
28 use clean::{self, GetDefId, ToSource, get_auto_traits_with_def_id};
29
30 use super::Clean;
31
32 /// Attempt to inline a definition into this AST.
33 ///
34 /// This function will fetch the definition specified, and if it is
35 /// from another crate it will attempt to inline the documentation
36 /// from the other crate into this crate.
37 ///
38 /// This is primarily used for `pub use` statements which are, in general,
39 /// implementation details. Inlining the documentation should help provide a
40 /// better experience when reading the documentation in this use case.
41 ///
42 /// The returned value is `None` if the definition could not be inlined,
43 /// and `Some` of a vector of items if it was successfully expanded.
44 pub fn try_inline(cx: &DocContext, def: Def, name: ast::Name, visited: &mut FxHashSet<DefId>)
45                   -> Option<Vec<clean::Item>> {
46     if def == Def::Err { return None }
47     let did = def.def_id();
48     if did.is_local() { return None }
49     let mut ret = Vec::new();
50     let inner = match def {
51         Def::Trait(did) => {
52             record_extern_fqn(cx, did, clean::TypeKind::Trait);
53             ret.extend(build_impls(cx, did, false));
54             clean::TraitItem(build_external_trait(cx, did))
55         }
56         Def::Fn(did) => {
57             record_extern_fqn(cx, did, clean::TypeKind::Function);
58             clean::FunctionItem(build_external_function(cx, did))
59         }
60         Def::Struct(did) => {
61             record_extern_fqn(cx, did, clean::TypeKind::Struct);
62             ret.extend(build_impls(cx, did, true));
63             clean::StructItem(build_struct(cx, did))
64         }
65         Def::Union(did) => {
66             record_extern_fqn(cx, did, clean::TypeKind::Union);
67             ret.extend(build_impls(cx, did, true));
68             clean::UnionItem(build_union(cx, did))
69         }
70         Def::TyAlias(did) => {
71             record_extern_fqn(cx, did, clean::TypeKind::Typedef);
72             ret.extend(build_impls(cx, did, false));
73             clean::TypedefItem(build_type_alias(cx, did), false)
74         }
75         Def::Enum(did) => {
76             record_extern_fqn(cx, did, clean::TypeKind::Enum);
77             ret.extend(build_impls(cx, did, true));
78             clean::EnumItem(build_enum(cx, did))
79         }
80         Def::TyForeign(did) => {
81             record_extern_fqn(cx, did, clean::TypeKind::Foreign);
82             ret.extend(build_impls(cx, did, false));
83             clean::ForeignTypeItem
84         }
85         // Never inline enum variants but leave them shown as re-exports.
86         Def::Variant(..) => return None,
87         // Assume that enum variants and struct types are re-exported next to
88         // their constructors.
89         Def::VariantCtor(..) |
90         Def::StructCtor(..) => return Some(Vec::new()),
91         Def::Mod(did) => {
92             record_extern_fqn(cx, did, clean::TypeKind::Module);
93             clean::ModuleItem(build_module(cx, did, visited))
94         }
95         Def::Static(did, mtbl) => {
96             record_extern_fqn(cx, did, clean::TypeKind::Static);
97             clean::StaticItem(build_static(cx, did, mtbl))
98         }
99         Def::Const(did) => {
100             record_extern_fqn(cx, did, clean::TypeKind::Const);
101             clean::ConstantItem(build_const(cx, did))
102         }
103         // FIXME(misdreavus): if attributes/derives come down here we should probably document them
104         // separately
105         Def::Macro(did, MacroKind::Bang) => {
106             record_extern_fqn(cx, did, clean::TypeKind::Macro);
107             if let Some(mac) = build_macro(cx, did, name) {
108                 clean::MacroItem(mac)
109             } else {
110                 return None;
111             }
112         }
113         _ => return None,
114     };
115     cx.renderinfo.borrow_mut().inlined.insert(did);
116     ret.push(clean::Item {
117         source: cx.tcx.def_span(did).clean(cx),
118         name: Some(name.clean(cx)),
119         attrs: load_attrs(cx, did),
120         inner,
121         visibility: Some(clean::Public),
122         stability: cx.tcx.lookup_stability(did).clean(cx),
123         deprecation: cx.tcx.lookup_deprecation(did).clean(cx),
124         def_id: did,
125     });
126     Some(ret)
127 }
128
129 pub fn try_inline_glob(cx: &DocContext, def: Def, visited: &mut FxHashSet<DefId>)
130     -> Option<Vec<clean::Item>>
131 {
132     if def == Def::Err { return None }
133     let did = def.def_id();
134     if did.is_local() { return None }
135
136     match def {
137         Def::Mod(did) => {
138             let m = build_module(cx, did, visited);
139             Some(m.items)
140         }
141         // glob imports on things like enums aren't inlined even for local exports, so just bail
142         _ => None,
143     }
144 }
145
146 pub fn load_attrs(cx: &DocContext, did: DefId) -> clean::Attributes {
147     cx.tcx.get_attrs(did).clean(cx)
148 }
149
150 /// Record an external fully qualified name in the external_paths cache.
151 ///
152 /// These names are used later on by HTML rendering to generate things like
153 /// source links back to the original item.
154 pub fn record_extern_fqn(cx: &DocContext, did: DefId, kind: clean::TypeKind) {
155     if did.is_local() {
156         debug!("record_extern_fqn(did={:?}, kind+{:?}): def_id is local, aborting", did, kind);
157         return;
158     }
159
160     let crate_name = cx.tcx.crate_name(did.krate).to_string();
161     let relative = cx.tcx.def_path(did).data.into_iter().filter_map(|elem| {
162         // extern blocks have an empty name
163         let s = elem.data.to_string();
164         if !s.is_empty() {
165             Some(s)
166         } else {
167             None
168         }
169     });
170     let fqn = if let clean::TypeKind::Macro = kind {
171         vec![crate_name, relative.last().unwrap()]
172     } else {
173         once(crate_name).chain(relative).collect()
174     };
175     cx.renderinfo.borrow_mut().external_paths.insert(did, (fqn, kind));
176 }
177
178 pub fn build_external_trait(cx: &DocContext, did: DefId) -> clean::Trait {
179     let auto_trait = cx.tcx.trait_def(did).has_auto_impl;
180     let trait_items = cx.tcx.associated_items(did).map(|item| item.clean(cx)).collect();
181     let predicates = cx.tcx.predicates_of(did);
182     let generics = (cx.tcx.generics_of(did), &predicates).clean(cx);
183     let generics = filter_non_trait_generics(did, generics);
184     let (generics, supertrait_bounds) = separate_supertrait_bounds(generics);
185     let is_spotlight = load_attrs(cx, did).has_doc_flag("spotlight");
186     let is_auto = cx.tcx.trait_is_auto(did);
187     clean::Trait {
188         auto: auto_trait,
189         unsafety: cx.tcx.trait_def(did).unsafety,
190         generics,
191         items: trait_items,
192         bounds: supertrait_bounds,
193         is_spotlight,
194         is_auto,
195     }
196 }
197
198 fn build_external_function(cx: &DocContext, did: DefId) -> clean::Function {
199     let sig = cx.tcx.fn_sig(did);
200
201     let constness = if cx.tcx.is_const_fn(did) {
202         hir::Constness::Const
203     } else {
204         hir::Constness::NotConst
205     };
206
207     let predicates = cx.tcx.predicates_of(did);
208     clean::Function {
209         decl: (did, sig).clean(cx),
210         generics: (cx.tcx.generics_of(did), &predicates).clean(cx),
211         header: hir::FnHeader {
212             unsafety: sig.unsafety(),
213             abi: sig.abi(),
214             constness,
215             asyncness: hir::IsAsync::NotAsync,
216         }
217     }
218 }
219
220 fn build_enum(cx: &DocContext, did: DefId) -> clean::Enum {
221     let predicates = cx.tcx.predicates_of(did);
222
223     clean::Enum {
224         generics: (cx.tcx.generics_of(did), &predicates).clean(cx),
225         variants_stripped: false,
226         variants: cx.tcx.adt_def(did).variants.clean(cx),
227     }
228 }
229
230 fn build_struct(cx: &DocContext, did: DefId) -> clean::Struct {
231     let predicates = cx.tcx.predicates_of(did);
232     let variant = cx.tcx.adt_def(did).non_enum_variant();
233
234     clean::Struct {
235         struct_type: match variant.ctor_kind {
236             CtorKind::Fictive => doctree::Plain,
237             CtorKind::Fn => doctree::Tuple,
238             CtorKind::Const => doctree::Unit,
239         },
240         generics: (cx.tcx.generics_of(did), &predicates).clean(cx),
241         fields: variant.fields.clean(cx),
242         fields_stripped: false,
243     }
244 }
245
246 fn build_union(cx: &DocContext, did: DefId) -> clean::Union {
247     let predicates = cx.tcx.predicates_of(did);
248     let variant = cx.tcx.adt_def(did).non_enum_variant();
249
250     clean::Union {
251         struct_type: doctree::Plain,
252         generics: (cx.tcx.generics_of(did), &predicates).clean(cx),
253         fields: variant.fields.clean(cx),
254         fields_stripped: false,
255     }
256 }
257
258 fn build_type_alias(cx: &DocContext, did: DefId) -> clean::Typedef {
259     let predicates = cx.tcx.predicates_of(did);
260
261     clean::Typedef {
262         type_: cx.tcx.type_of(did).clean(cx),
263         generics: (cx.tcx.generics_of(did), &predicates).clean(cx),
264     }
265 }
266
267 pub fn build_impls(cx: &DocContext, did: DefId, auto_traits: bool) -> Vec<clean::Item> {
268     let tcx = cx.tcx;
269     let mut impls = Vec::new();
270
271     for &did in tcx.inherent_impls(did).iter() {
272         build_impl(cx, did, &mut impls);
273     }
274
275     if auto_traits {
276         let auto_impls = get_auto_traits_with_def_id(cx, did);
277         let mut renderinfo = cx.renderinfo.borrow_mut();
278         let new_impls: Vec<clean::Item> = auto_impls.into_iter()
279             .filter(|i| renderinfo.inlined.insert(i.def_id)).collect();
280
281         impls.extend(new_impls);
282     }
283
284     // If this is the first time we've inlined something from another crate, then
285     // we inline *all* impls from all the crates into this crate. Note that there's
286     // currently no way for us to filter this based on type, and we likely need
287     // many impls for a variety of reasons.
288     //
289     // Primarily, the impls will be used to populate the documentation for this
290     // type being inlined, but impls can also be used when generating
291     // documentation for primitives (no way to find those specifically).
292     if cx.populated_all_crate_impls.get() {
293         return impls;
294     }
295
296     cx.populated_all_crate_impls.set(true);
297
298     for &cnum in tcx.crates().iter() {
299         for did in tcx.all_trait_implementations(cnum).iter() {
300             build_impl(cx, *did, &mut impls);
301         }
302     }
303
304     // Also try to inline primitive impls from other crates.
305     let lang_items = tcx.lang_items();
306     let primitive_impls = [
307         lang_items.isize_impl(),
308         lang_items.i8_impl(),
309         lang_items.i16_impl(),
310         lang_items.i32_impl(),
311         lang_items.i64_impl(),
312         lang_items.i128_impl(),
313         lang_items.usize_impl(),
314         lang_items.u8_impl(),
315         lang_items.u16_impl(),
316         lang_items.u32_impl(),
317         lang_items.u64_impl(),
318         lang_items.u128_impl(),
319         lang_items.f32_impl(),
320         lang_items.f64_impl(),
321         lang_items.f32_runtime_impl(),
322         lang_items.f64_runtime_impl(),
323         lang_items.char_impl(),
324         lang_items.str_impl(),
325         lang_items.slice_impl(),
326         lang_items.slice_u8_impl(),
327         lang_items.str_alloc_impl(),
328         lang_items.slice_alloc_impl(),
329         lang_items.slice_u8_alloc_impl(),
330         lang_items.const_ptr_impl(),
331         lang_items.mut_ptr_impl(),
332     ];
333
334     for def_id in primitive_impls.iter().filter_map(|&def_id| def_id) {
335         if !def_id.is_local() {
336             build_impl(cx, def_id, &mut impls);
337
338             let auto_impls = get_auto_traits_with_def_id(cx, def_id);
339             let mut renderinfo = cx.renderinfo.borrow_mut();
340
341             let new_impls: Vec<clean::Item> = auto_impls.into_iter()
342                 .filter(|i| renderinfo.inlined.insert(i.def_id)).collect();
343
344             impls.extend(new_impls);
345         }
346     }
347
348     impls
349 }
350
351 pub fn build_impl(cx: &DocContext, did: DefId, ret: &mut Vec<clean::Item>) {
352     if !cx.renderinfo.borrow_mut().inlined.insert(did) {
353         return
354     }
355
356     let attrs = load_attrs(cx, did);
357     let tcx = cx.tcx;
358     let associated_trait = tcx.impl_trait_ref(did);
359
360     // Only inline impl if the implemented trait is
361     // reachable in rustdoc generated documentation
362     if let Some(traitref) = associated_trait {
363         if !cx.access_levels.borrow().is_doc_reachable(traitref.def_id) {
364             return
365         }
366     }
367
368     let for_ = tcx.type_of(did).clean(cx);
369
370     // Only inline impl if the implementing type is
371     // reachable in rustdoc generated documentation
372     if let Some(did) = for_.def_id() {
373         if !cx.access_levels.borrow().is_doc_reachable(did) {
374             return
375         }
376     }
377
378     let predicates = tcx.predicates_of(did);
379     let trait_items = tcx.associated_items(did).filter_map(|item| {
380         if associated_trait.is_some() || item.vis == ty::Visibility::Public {
381             Some(item.clean(cx))
382         } else {
383             None
384         }
385     }).collect::<Vec<_>>();
386     let polarity = tcx.impl_polarity(did);
387     let trait_ = associated_trait.clean(cx).map(|bound| {
388         match bound {
389             clean::GenericBound::TraitBound(polyt, _) => polyt.trait_,
390             clean::GenericBound::Outlives(..) => unreachable!(),
391         }
392     });
393     if trait_.def_id() == tcx.lang_items().deref_trait() {
394         super::build_deref_target_impls(cx, &trait_items, ret);
395     }
396     if let Some(trait_did) = trait_.def_id() {
397         record_extern_trait(cx, trait_did);
398     }
399
400     let provided = trait_.def_id().map(|did| {
401         tcx.provided_trait_methods(did)
402            .into_iter()
403            .map(|meth| meth.ident.to_string())
404            .collect()
405     }).unwrap_or(FxHashSet());
406
407     ret.push(clean::Item {
408         inner: clean::ImplItem(clean::Impl {
409             unsafety: hir::Unsafety::Normal,
410             generics: (tcx.generics_of(did), &predicates).clean(cx),
411             provided_trait_methods: provided,
412             trait_,
413             for_,
414             items: trait_items,
415             polarity: Some(polarity.clean(cx)),
416             synthetic: false,
417         }),
418         source: tcx.def_span(did).clean(cx),
419         name: None,
420         attrs,
421         visibility: Some(clean::Inherited),
422         stability: tcx.lookup_stability(did).clean(cx),
423         deprecation: tcx.lookup_deprecation(did).clean(cx),
424         def_id: did,
425     });
426 }
427
428 fn build_module(cx: &DocContext, did: DefId, visited: &mut FxHashSet<DefId>) -> clean::Module {
429     let mut items = Vec::new();
430     fill_in(cx, did, &mut items, visited);
431     return clean::Module {
432         items,
433         is_crate: false,
434     };
435
436     fn fill_in(cx: &DocContext, did: DefId, items: &mut Vec<clean::Item>,
437                visited: &mut FxHashSet<DefId>) {
438         // If we're re-exporting a re-export it may actually re-export something in
439         // two namespaces, so the target may be listed twice. Make sure we only
440         // visit each node at most once.
441         for &item in cx.tcx.item_children(did).iter() {
442             let def_id = item.def.def_id();
443             if item.vis == ty::Visibility::Public {
444                 if did == def_id || !visited.insert(def_id) { continue }
445                 if let Some(i) = try_inline(cx, item.def, item.ident.name, visited) {
446                     items.extend(i)
447                 }
448             }
449         }
450     }
451 }
452
453 pub fn print_inlined_const(cx: &DocContext, did: DefId) -> String {
454     cx.tcx.rendered_const(did)
455 }
456
457 fn build_const(cx: &DocContext, did: DefId) -> clean::Constant {
458     clean::Constant {
459         type_: cx.tcx.type_of(did).clean(cx),
460         expr: print_inlined_const(cx, did)
461     }
462 }
463
464 fn build_static(cx: &DocContext, did: DefId, mutable: bool) -> clean::Static {
465     clean::Static {
466         type_: cx.tcx.type_of(did).clean(cx),
467         mutability: if mutable {clean::Mutable} else {clean::Immutable},
468         expr: "\n\n\n".to_string(), // trigger the "[definition]" links
469     }
470 }
471
472 fn build_macro(cx: &DocContext, did: DefId, name: ast::Name) -> Option<clean::Macro> {
473     let imported_from = cx.tcx.original_crate_name(did.krate);
474     let def = match cx.cstore.load_macro_untracked(did, cx.sess()) {
475         LoadedMacro::MacroDef(macro_def) => macro_def,
476         // FIXME(jseyfried): document proc macro re-exports
477         LoadedMacro::ProcMacro(..) => return None,
478     };
479
480     let matchers: hir::HirVec<Span> = if let ast::ItemKind::MacroDef(ref def) = def.node {
481         let tts: Vec<_> = def.stream().into_trees().collect();
482         tts.chunks(4).map(|arm| arm[0].span()).collect()
483     } else {
484         unreachable!()
485     };
486
487     let source = format!("macro_rules! {} {{\n{}}}",
488                          name.clean(cx),
489                          matchers.iter().map(|span| {
490                              format!("    {} => {{ ... }};\n", span.to_src(cx))
491                          }).collect::<String>());
492
493     Some(clean::Macro {
494         source,
495         imported_from: Some(imported_from).clean(cx),
496     })
497 }
498
499 /// A trait's generics clause actually contains all of the predicates for all of
500 /// its associated types as well. We specifically move these clauses to the
501 /// associated types instead when displaying, so when we're generating the
502 /// generics for the trait itself we need to be sure to remove them.
503 /// We also need to remove the implied "recursive" Self: Trait bound.
504 ///
505 /// The inverse of this filtering logic can be found in the `Clean`
506 /// implementation for `AssociatedType`
507 fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean::Generics {
508     for pred in &mut g.where_predicates {
509         match *pred {
510             clean::WherePredicate::BoundPredicate {
511                 ty: clean::Generic(ref s),
512                 ref mut bounds
513             } if *s == "Self" => {
514                 bounds.retain(|bound| {
515                     match *bound {
516                         clean::GenericBound::TraitBound(clean::PolyTrait {
517                             trait_: clean::ResolvedPath { did, .. },
518                             ..
519                         }, _) => did != trait_did,
520                         _ => true
521                     }
522                 });
523             }
524             _ => {}
525         }
526     }
527
528     g.where_predicates.retain(|pred| {
529         match *pred {
530             clean::WherePredicate::BoundPredicate {
531                 ty: clean::QPath {
532                     self_type: box clean::Generic(ref s),
533                     trait_: box clean::ResolvedPath { did, .. },
534                     name: ref _name,
535                 }, ref bounds
536             } => !(*s == "Self" && did == trait_did) && !bounds.is_empty(),
537             _ => true,
538         }
539     });
540     g
541 }
542
543 /// Supertrait bounds for a trait are also listed in the generics coming from
544 /// the metadata for a crate, so we want to separate those out and create a new
545 /// list of explicit supertrait bounds to render nicely.
546 fn separate_supertrait_bounds(mut g: clean::Generics)
547                               -> (clean::Generics, Vec<clean::GenericBound>) {
548     let mut ty_bounds = Vec::new();
549     g.where_predicates.retain(|pred| {
550         match *pred {
551             clean::WherePredicate::BoundPredicate {
552                 ty: clean::Generic(ref s),
553                 ref bounds
554             } if *s == "Self" => {
555                 ty_bounds.extend(bounds.iter().cloned());
556                 false
557             }
558             _ => true,
559         }
560     });
561     (g, ty_bounds)
562 }
563
564 pub fn record_extern_trait(cx: &DocContext, did: DefId) {
565     if cx.external_traits.borrow().contains_key(&did) ||
566         cx.active_extern_traits.borrow().contains(&did)
567     {
568         return;
569     }
570
571     cx.active_extern_traits.borrow_mut().push(did);
572
573     let trait_ = build_external_trait(cx, did);
574
575     cx.external_traits.borrow_mut().insert(did, trait_);
576     cx.active_extern_traits.borrow_mut().remove_item(&did);
577 }