summaryrefslogtreecommitdiff
path: root/src/librustdoc/clean/inline.rs
blob: ef54be720376a7a79f96eb40491fa71d6ef2a8a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// 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.

use std::collections::HashSet;

use syntax::ast;
use syntax::attr::AttrMetaMethods;
use rustc_front::hir;

use rustc::middle::cstore::{self, CrateStore};
use rustc::middle::def::Def;
use rustc::middle::def_id::DefId;
use rustc::middle::ty::{self, TyCtxt};
use rustc::middle::subst;
use rustc::middle::stability;
use rustc::middle::const_eval;

use core::DocContext;
use doctree;
use clean::{self, Attributes};

use super::{Clean, ToSource};

/// 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.
pub fn try_inline(cx: &DocContext, id: ast::NodeId, into: Option<ast::Name>)
                  -> Option<Vec<clean::Item>> {
    let tcx = match cx.tcx_opt() {
        Some(tcx) => tcx,
        None => return None,
    };
    let def = match tcx.def_map.borrow().get(&id) {
        Some(d) => d.full_def(),
        None => return None,
    };
    let did = def.def_id();
    if did.is_local() { return None }
    try_inline_def(cx, tcx, def).map(|vec| {
        vec.into_iter().map(|mut item| {
            match into {
                Some(into) if item.name.is_some() => {
                    item.name = Some(into.clean(cx));
                }
                _ => {}
            }
            item
        }).collect()
    })
}

fn try_inline_def(cx: &DocContext, tcx: &TyCtxt,
                  def: Def) -> Option<Vec<clean::Item>> {
    let mut ret = Vec::new();
    let did = def.def_id();
    let inner = match def {
        Def::Trait(did) => {
            record_extern_fqn(cx, did, clean::TypeTrait);
            clean::TraitItem(build_external_trait(cx, tcx, did))
        }
        Def::Fn(did) => {
            record_extern_fqn(cx, did, clean::TypeFunction);
            clean::FunctionItem(build_external_function(cx, tcx, did))
        }
        Def::Struct(did)
                // If this is a struct constructor, we skip it
                if tcx.sess.cstore.tuple_struct_definition_if_ctor(did).is_none() => {
            record_extern_fqn(cx, did, clean::TypeStruct);
            ret.extend(build_impls(cx, tcx, did));
            clean::StructItem(build_struct(cx, tcx, did))
        }
        Def::TyAlias(did) => {
            record_extern_fqn(cx, did, clean::TypeTypedef);
            ret.extend(build_impls(cx, tcx, did));
            build_type(cx, tcx, did)
        }
        Def::Enum(did) => {
            record_extern_fqn(cx, did, clean::TypeEnum);
            ret.extend(build_impls(cx, tcx, did));
            build_type(cx, tcx, did)
        }
        // Assume that the enum type is reexported next to the variant, and
        // variants don't show up in documentation specially.
        Def::Variant(..) => return Some(Vec::new()),
        Def::Mod(did) => {
            record_extern_fqn(cx, did, clean::TypeModule);
            clean::ModuleItem(build_module(cx, tcx, did))
        }
        Def::Static(did, mtbl) => {
            record_extern_fqn(cx, did, clean::TypeStatic);
            clean::StaticItem(build_static(cx, tcx, did, mtbl))
        }
        Def::Const(did) | Def::AssociatedConst(did) => {
            record_extern_fqn(cx, did, clean::TypeConst);
            clean::ConstantItem(build_const(cx, tcx, did))
        }
        _ => return None,
    };
    cx.inlined.borrow_mut().as_mut().unwrap().insert(did);
    ret.push(clean::Item {
        source: clean::Span::empty(),
        name: Some(tcx.item_name(did).to_string()),
        attrs: load_attrs(cx, tcx, did),
        inner: inner,
        visibility: Some(hir::Public),
        stability: stability::lookup_stability(tcx, did).clean(cx),
        deprecation: stability::lookup_deprecation(tcx, did).clean(cx),
        def_id: did,
    });
    Some(ret)
}

pub fn load_attrs(cx: &DocContext, tcx: &TyCtxt,
                  did: DefId) -> Vec<clean::Attribute> {
    tcx.get_attrs(did).iter().map(|a| a.clean(cx)).collect()
}

/// 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.
pub fn record_extern_fqn(cx: &DocContext, did: DefId, kind: clean::TypeKind) {
    if let Some(tcx) = cx.tcx_opt() {
        let fqn = tcx.sess.cstore.extern_item_path(did);
        let fqn = fqn.into_iter().map(|i| i.to_string()).collect();
        cx.external_paths.borrow_mut().as_mut().unwrap().insert(did, (fqn, kind));
    }
}

pub fn build_external_trait(cx: &DocContext, tcx: &TyCtxt,
                            did: DefId) -> clean::Trait {
    let def = tcx.lookup_trait_def(did);
    let trait_items = tcx.trait_items(did).clean(cx);
    let predicates = tcx.lookup_predicates(did);
    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);
    clean::Trait {
        unsafety: def.unsafety,
        generics: generics,
        items: trait_items,
        bounds: supertrait_bounds,
    }
}

fn build_external_function(cx: &DocContext, tcx: &TyCtxt, did: DefId) -> clean::Function {
    let t = tcx.lookup_item_type(did);
    let (decl, style, abi) = match t.ty.sty {
        ty::TyFnDef(_, _, ref f) => ((did, &f.sig).clean(cx), f.unsafety, f.abi),
        _ => panic!("bad function"),
    };

    let constness = if tcx.sess.cstore.is_const_fn(did) {
        hir::Constness::Const
    } else {
        hir::Constness::NotConst
    };

    let predicates = tcx.lookup_predicates(did);
    clean::Function {
        decl: decl,
        generics: (&t.generics, &predicates, subst::FnSpace).clean(cx),
        unsafety: style,
        constness: constness,
        abi: abi,
    }
}

fn build_struct(cx: &DocContext, tcx: &TyCtxt, did: DefId) -> clean::Struct {
    let t = tcx.lookup_item_type(did);
    let predicates = tcx.lookup_predicates(did);
    let variant = tcx.lookup_adt_def(did).struct_variant();

    clean::Struct {
        struct_type: match &*variant.fields {
            [] => doctree::Unit,
            [_] if variant.kind == ty::VariantKind::Tuple => doctree::Newtype,
            [..] if variant.kind == ty::VariantKind::Tuple => doctree::Tuple,
            _ => doctree::Plain,
        },
        generics: (&t.generics, &predicates, subst::TypeSpace).clean(cx),
        fields: variant.fields.clean(cx),
        fields_stripped: false,
    }
}

fn build_type(cx: &DocContext, tcx: &TyCtxt, did: DefId) -> clean::ItemEnum {
    let t = tcx.lookup_item_type(did);
    let predicates = tcx.lookup_predicates(did);
    match t.ty.sty {
        ty::TyEnum(edef, _) if !tcx.sess.cstore.is_typedef(did) => {
            return clean::EnumItem(clean::Enum {
                generics: (&t.generics, &predicates, subst::TypeSpace).clean(cx),
                variants_stripped: false,
                variants: edef.variants.clean(cx),
            })
        }
        _ => {}
    }

    clean::TypedefItem(clean::Typedef {
        type_: t.ty.clean(cx),
        generics: (&t.generics, &predicates, subst::TypeSpace).clean(cx),
    }, false)
}

pub fn build_impls(cx: &DocContext,
                   tcx: &TyCtxt,
                   did: DefId) -> Vec<clean::Item> {
    tcx.populate_inherent_implementations_for_type_if_necessary(did);
    let mut impls = Vec::new();

    if let Some(i) = tcx.inherent_impls.borrow().get(&did) {
        for &did in i.iter() {
            build_impl(cx, tcx, did, &mut impls);
        }
    }

    // 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.all_crate_impls.borrow_mut().contains_key(&did.krate) {
        let mut impls = Vec::new();
        for item in tcx.sess.cstore.crate_top_level_items(did.krate) {
            populate_impls(cx, tcx, item.def, &mut impls);
        }
        cx.all_crate_impls.borrow_mut().insert(did.krate, impls);

        fn populate_impls(cx: &DocContext, tcx: &TyCtxt,
                          def: cstore::DefLike,
                          impls: &mut Vec<clean::Item>) {
            match def {
                cstore::DlImpl(did) => build_impl(cx, tcx, did, impls),
                cstore::DlDef(Def::Mod(did)) => {
                    // Don't recurse if this is a #[doc(hidden)] module
                    if load_attrs(cx, tcx, did).list_def("doc").has_word("hidden") {
                        return;
                    }

                    for item in tcx.sess.cstore.item_children(did) {
                        populate_impls(cx, tcx, item.def, impls)
                    }
                }
                _ => {}
            }
        }
    }

    let mut candidates = cx.all_crate_impls.borrow_mut();
    let candidates = candidates.get_mut(&did.krate).unwrap();
    for i in (0..candidates.len()).rev() {
        let remove = match candidates[i].inner {
            clean::ImplItem(ref i) => {
                i.for_.def_id() == Some(did) || i.for_.primitive_type().is_some()
            }
            _ => continue,
        };
        if remove {
            impls.push(candidates.swap_remove(i));
        }
    }

    return impls;
}

pub fn build_impl(cx: &DocContext,
                  tcx: &TyCtxt,
                  did: DefId,
                  ret: &mut Vec<clean::Item>) {
    if !cx.inlined.borrow_mut().as_mut().unwrap().insert(did) {
        return
    }

    let attrs = load_attrs(cx, tcx, did);
    let associated_trait = tcx.impl_trait_ref(did);
    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.list_def("doc").has_word("hidden") {
            return
        }
    }

    // If this is a defaulted impl, then bail out early here
    if tcx.sess.cstore.is_default_impl(did) {
        return ret.push(clean::Item {
            inner: clean::DefaultImplItem(clean::DefaultImpl {
                // FIXME: this should be decoded
                unsafety: hir::Unsafety::Normal,
                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,
            visibility: Some(hir::Inherited),
            stability: stability::lookup_stability(tcx, did).clean(cx),
            deprecation: stability::lookup_deprecation(tcx, did).clean(cx),
            def_id: did,
        });
    }

    let predicates = tcx.lookup_predicates(did);
    let trait_items = tcx.sess.cstore.impl_items(did)
            .iter()
            .filter_map(|did| {
        let did = did.def_id();
        let impl_item = tcx.impl_or_trait_item(did);
        match impl_item {
            ty::ConstTraitItem(ref assoc_const) => {
                let did = assoc_const.def_id;
                let type_scheme = tcx.lookup_item_type(did);
                let default = if assoc_const.has_value {
                    Some(const_eval::lookup_const_by_id(tcx, did, None, None)
                         .unwrap().0.span.to_src(cx))
                } else {
                    None
                };
                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_stability(tcx, did).clean(cx),
                    deprecation: stability::lookup_deprecation(tcx, did).clean(cx),
                    def_id: did
                })
            }
            ty::MethodTraitItem(method) => {
                if method.vis != hir::Public && associated_trait.is_none() {
                    return None
                }
                let mut item = method.clean(cx);
                item.inner = match item.inner.clone() {
                    clean::TyMethodItem(clean::TyMethod {
                        unsafety, decl, self_, generics, abi
                    }) => {
                        let constness = if tcx.sess.cstore.is_const_fn(did) {
                            hir::Constness::Const
                        } else {
                            hir::Constness::NotConst
                        };

                        clean::MethodItem(clean::Method {
                            unsafety: unsafety,
                            constness: constness,
                            decl: decl,
                            self_: self_,
                            generics: generics,
                            abi: abi
                        })
                    }
                    _ => panic!("not a tymethod"),
                };
                Some(item)
            }
            ty::TypeTraitItem(ref assoc_ty) => {
                let did = assoc_ty.def_id;
                let type_scheme = ty::TypeScheme {
                    ty: assoc_ty.ty.unwrap(),
                    generics: ty::Generics::empty()
                };
                // Not sure the choice of ParamSpace actually matters here,
                // because an associated type won't have generics on the LHS
                let typedef = (type_scheme, ty::GenericPredicates::empty(),
                               subst::ParamSpace::TypeSpace).clean(cx);
                Some(clean::Item {
                    name: Some(assoc_ty.name.clean(cx)),
                    inner: clean::TypedefItem(typedef, true),
                    source: clean::Span::empty(),
                    attrs: vec![],
                    visibility: None,
                    stability: stability::lookup_stability(tcx, did).clean(cx),
                    deprecation: stability::lookup_deprecation(tcx, did).clean(cx),
                    def_id: did
                })
            }
        }
    }).collect::<Vec<_>>();
    let polarity = tcx.trait_impl_polarity(did);
    let ty = tcx.lookup_item_type(did);
    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 {
        inner: clean::ImplItem(clean::Impl {
            unsafety: hir::Unsafety::Normal, // FIXME: this should be decoded
            derived: clean::detect_derived(&attrs),
            trait_: trait_,
            for_: ty.ty.clean(cx),
            generics: (&ty.generics, &predicates, subst::TypeSpace).clean(cx),
            items: trait_items,
            polarity: polarity.map(|p| { p.clean(cx) }),
        }),
        source: clean::Span::empty(),
        name: None,
        attrs: attrs,
        visibility: Some(hir::Inherited),
        stability: stability::lookup_stability(tcx, did).clean(cx),
        deprecation: stability::lookup_deprecation(tcx, did).clean(cx),
        def_id: did,
    });
}

fn build_module(cx: &DocContext, tcx: &TyCtxt,
                did: DefId) -> clean::Module {
    let mut items = Vec::new();
    fill_in(cx, tcx, did, &mut items);
    return clean::Module {
        items: items,
        is_crate: false,
    };

    fn fill_in(cx: &DocContext, tcx: &TyCtxt, did: DefId,
               items: &mut Vec<clean::Item>) {
        // 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();
        for item in tcx.sess.cstore.item_children(did) {
            match item.def {
                cstore::DlDef(Def::ForeignMod(did)) => {
                    fill_in(cx, tcx, did, items);
                }
                cstore::DlDef(def) if item.vis == hir::Public => {
                    if !visited.insert(def) { continue }
                    if let Some(i) = try_inline_def(cx, tcx, def) {
                        items.extend(i)
                    }
                }
                cstore::DlDef(..) => {}
                // All impls were inlined above
                cstore::DlImpl(..) => {}
                cstore::DlField => panic!("unimplemented field"),
            }
        }
    }
}

fn build_const(cx: &DocContext, tcx: &TyCtxt,
               did: DefId) -> clean::Constant {
    use rustc::middle::const_eval;
    use rustc_front::print::pprust;

    let (expr, ty) = const_eval::lookup_const_by_id(tcx, did, None, None).unwrap_or_else(|| {
        panic!("expected lookup_const_by_id to succeed for {:?}", did);
    });
    debug!("converting constant expr {:?} to snippet", expr);
    let sn = pprust::expr_to_string(expr);
    debug!("got snippet {}", sn);

    clean::Constant {
        type_: ty.map(|t| t.clean(cx)).unwrap_or_else(|| tcx.lookup_item_type(did).ty.clean(cx)),
        expr: sn
    }
}

fn build_static(cx: &DocContext, tcx: &TyCtxt,
                did: DefId,
                mutable: bool) -> clean::Static {
    clean::Static {
        type_: tcx.lookup_item_type(did).ty.clean(cx),
        mutability: if mutable {clean::Mutable} else {clean::Immutable},
        expr: "\n\n\n".to_string(), // trigger the "[definition]" links
    }
}

/// 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`
fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics)
                             -> 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)
}