]> git.proxmox.com Git - rustc.git/blob - src/librustdoc/clean/mod.rs
Imported Upstream version 1.2.0+dfsg1
[rustc.git] / src / librustdoc / clean / mod.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 //! This module contains the "cleaned" pieces of the AST, and the functions
12 //! that clean them.
13
14 pub use self::Type::*;
15 pub use self::PrimitiveType::*;
16 pub use self::TypeKind::*;
17 pub use self::StructField::*;
18 pub use self::VariantKind::*;
19 pub use self::Mutability::*;
20 pub use self::Import::*;
21 pub use self::ItemEnum::*;
22 pub use self::Attribute::*;
23 pub use self::TyParamBound::*;
24 pub use self::SelfTy::*;
25 pub use self::FunctionRetTy::*;
26
27 use syntax;
28 use syntax::abi;
29 use syntax::ast;
30 use syntax::ast_util;
31 use syntax::attr;
32 use syntax::attr::{AttributeMethods, AttrMetaMethods};
33 use syntax::codemap;
34 use syntax::codemap::{DUMMY_SP, Pos, Spanned};
35 use syntax::parse::token::{self, InternedString, special_idents};
36 use syntax::ptr::P;
37
38 use rustc_trans::back::link;
39 use rustc::metadata::cstore;
40 use rustc::metadata::csearch;
41 use rustc::metadata::decoder;
42 use rustc::middle::def;
43 use rustc::middle::subst::{self, ParamSpace, VecPerParamSpace};
44 use rustc::middle::ty;
45 use rustc::middle::stability;
46
47 use std::collections::HashMap;
48 use std::path::PathBuf;
49 use std::rc::Rc;
50 use std::u32;
51
52 use core::DocContext;
53 use doctree;
54 use visit_ast;
55
56 /// A stable identifier to the particular version of JSON output.
57 /// Increment this when the `Crate` and related structures change.
58 pub const SCHEMA_VERSION: &'static str = "0.8.3";
59
60 mod inline;
61 mod simplify;
62
63 // extract the stability index for a node from tcx, if possible
64 fn get_stability(cx: &DocContext, def_id: ast::DefId) -> Option<Stability> {
65 cx.tcx_opt().and_then(|tcx| stability::lookup(tcx, def_id)).clean(cx)
66 }
67
68 pub trait Clean<T> {
69 fn clean(&self, cx: &DocContext) -> T;
70 }
71
72 impl<T: Clean<U>, U> Clean<Vec<U>> for [T] {
73 fn clean(&self, cx: &DocContext) -> Vec<U> {
74 self.iter().map(|x| x.clean(cx)).collect()
75 }
76 }
77
78 impl<T: Clean<U>, U> Clean<VecPerParamSpace<U>> for VecPerParamSpace<T> {
79 fn clean(&self, cx: &DocContext) -> VecPerParamSpace<U> {
80 self.map(|x| x.clean(cx))
81 }
82 }
83
84 impl<T: Clean<U>, U> Clean<U> for P<T> {
85 fn clean(&self, cx: &DocContext) -> U {
86 (**self).clean(cx)
87 }
88 }
89
90 impl<T: Clean<U>, U> Clean<U> for Rc<T> {
91 fn clean(&self, cx: &DocContext) -> U {
92 (**self).clean(cx)
93 }
94 }
95
96 impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
97 fn clean(&self, cx: &DocContext) -> Option<U> {
98 match self {
99 &None => None,
100 &Some(ref v) => Some(v.clean(cx))
101 }
102 }
103 }
104
105 impl<T, U> Clean<U> for ty::Binder<T> where T: Clean<U> {
106 fn clean(&self, cx: &DocContext) -> U {
107 self.0.clean(cx)
108 }
109 }
110
111 impl<T: Clean<U>, U> Clean<Vec<U>> for syntax::owned_slice::OwnedSlice<T> {
112 fn clean(&self, cx: &DocContext) -> Vec<U> {
113 self.iter().map(|x| x.clean(cx)).collect()
114 }
115 }
116
117 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
118 pub struct Crate {
119 pub name: String,
120 pub src: PathBuf,
121 pub module: Option<Item>,
122 pub externs: Vec<(ast::CrateNum, ExternalCrate)>,
123 pub primitives: Vec<PrimitiveType>,
124 pub external_traits: HashMap<ast::DefId, Trait>,
125 }
126
127 impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
128 fn clean(&self, cx: &DocContext) -> Crate {
129 use rustc::session::config::Input;
130
131 if let Some(t) = cx.tcx_opt() {
132 cx.deref_trait_did.set(t.lang_items.deref_trait());
133 }
134
135 let mut externs = Vec::new();
136 cx.sess().cstore.iter_crate_data(|n, meta| {
137 externs.push((n, meta.clean(cx)));
138 });
139 externs.sort_by(|&(a, _), &(b, _)| a.cmp(&b));
140
141 // Figure out the name of this crate
142 let input = &cx.input;
143 let name = link::find_crate_name(None, &self.attrs, input);
144
145 // Clean the crate, translating the entire libsyntax AST to one that is
146 // understood by rustdoc.
147 let mut module = self.module.clean(cx);
148
149 // Collect all inner modules which are tagged as implementations of
150 // primitives.
151 //
152 // Note that this loop only searches the top-level items of the crate,
153 // and this is intentional. If we were to search the entire crate for an
154 // item tagged with `#[doc(primitive)]` then we we would also have to
155 // search the entirety of external modules for items tagged
156 // `#[doc(primitive)]`, which is a pretty inefficient process (decoding
157 // all that metadata unconditionally).
158 //
159 // In order to keep the metadata load under control, the
160 // `#[doc(primitive)]` feature is explicitly designed to only allow the
161 // primitive tags to show up as the top level items in a crate.
162 //
163 // Also note that this does not attempt to deal with modules tagged
164 // duplicately for the same primitive. This is handled later on when
165 // rendering by delegating everything to a hash map.
166 let mut primitives = Vec::new();
167 {
168 let m = match module.inner {
169 ModuleItem(ref mut m) => m,
170 _ => unreachable!(),
171 };
172 let mut tmp = Vec::new();
173 for child in &mut m.items {
174 match child.inner {
175 ModuleItem(..) => {}
176 _ => continue,
177 }
178 let prim = match PrimitiveType::find(&child.attrs) {
179 Some(prim) => prim,
180 None => continue,
181 };
182 primitives.push(prim);
183 tmp.push(Item {
184 source: Span::empty(),
185 name: Some(prim.to_url_str().to_string()),
186 attrs: child.attrs.clone(),
187 visibility: Some(ast::Public),
188 stability: None,
189 def_id: ast_util::local_def(prim.to_node_id()),
190 inner: PrimitiveItem(prim),
191 });
192 }
193 m.items.extend(tmp);
194 }
195
196 let src = match cx.input {
197 Input::File(ref path) => path.clone(),
198 Input::Str(_) => PathBuf::new() // FIXME: this is wrong
199 };
200
201 Crate {
202 name: name.to_string(),
203 src: src,
204 module: Some(module),
205 externs: externs,
206 primitives: primitives,
207 external_traits: cx.external_traits.borrow_mut().take()
208 .unwrap_or(HashMap::new()),
209 }
210 }
211 }
212
213 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
214 pub struct ExternalCrate {
215 pub name: String,
216 pub attrs: Vec<Attribute>,
217 pub primitives: Vec<PrimitiveType>,
218 }
219
220 impl Clean<ExternalCrate> for cstore::crate_metadata {
221 fn clean(&self, cx: &DocContext) -> ExternalCrate {
222 let mut primitives = Vec::new();
223 cx.tcx_opt().map(|tcx| {
224 csearch::each_top_level_item_of_crate(&tcx.sess.cstore,
225 self.cnum,
226 |def, _, _| {
227 let did = match def {
228 decoder::DlDef(def::DefMod(did)) => did,
229 _ => return
230 };
231 let attrs = inline::load_attrs(cx, tcx, did);
232 PrimitiveType::find(&attrs).map(|prim| primitives.push(prim));
233 })
234 });
235 ExternalCrate {
236 name: self.name.to_string(),
237 attrs: decoder::get_crate_attributes(self.data()).clean(cx),
238 primitives: primitives,
239 }
240 }
241 }
242
243 /// Anything with a source location and set of attributes and, optionally, a
244 /// name. That is, anything that can be documented. This doesn't correspond
245 /// directly to the AST's concept of an item; it's a strict superset.
246 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
247 pub struct Item {
248 /// Stringified span
249 pub source: Span,
250 /// Not everything has a name. E.g., impls
251 pub name: Option<String>,
252 pub attrs: Vec<Attribute> ,
253 pub inner: ItemEnum,
254 pub visibility: Option<Visibility>,
255 pub def_id: ast::DefId,
256 pub stability: Option<Stability>,
257 }
258
259 impl Item {
260 /// Finds the `doc` attribute as a List and returns the list of attributes
261 /// nested inside.
262 pub fn doc_list<'a>(&'a self) -> Option<&'a [Attribute]> {
263 for attr in &self.attrs {
264 match *attr {
265 List(ref x, ref list) if "doc" == *x => {
266 return Some(list);
267 }
268 _ => {}
269 }
270 }
271 return None;
272 }
273
274 /// Finds the `doc` attribute as a NameValue and returns the corresponding
275 /// value found.
276 pub fn doc_value<'a>(&'a self) -> Option<&'a str> {
277 for attr in &self.attrs {
278 match *attr {
279 NameValue(ref x, ref v) if "doc" == *x => {
280 return Some(v);
281 }
282 _ => {}
283 }
284 }
285 return None;
286 }
287
288 pub fn is_hidden_from_doc(&self) -> bool {
289 match self.doc_list() {
290 Some(l) => {
291 for innerattr in l {
292 match *innerattr {
293 Word(ref s) if "hidden" == *s => {
294 return true
295 }
296 _ => (),
297 }
298 }
299 },
300 None => ()
301 }
302 return false;
303 }
304
305 pub fn is_mod(&self) -> bool {
306 match self.inner { ModuleItem(..) => true, _ => false }
307 }
308 pub fn is_trait(&self) -> bool {
309 match self.inner { TraitItem(..) => true, _ => false }
310 }
311 pub fn is_struct(&self) -> bool {
312 match self.inner { StructItem(..) => true, _ => false }
313 }
314 pub fn is_enum(&self) -> bool {
315 match self.inner { EnumItem(..) => true, _ => false }
316 }
317 pub fn is_fn(&self) -> bool {
318 match self.inner { FunctionItem(..) => true, _ => false }
319 }
320
321 pub fn stability_class(&self) -> String {
322 match self.stability {
323 Some(ref s) => {
324 let mut base = match s.level {
325 attr::Unstable => "unstable".to_string(),
326 attr::Stable => String::new(),
327 };
328 if !s.deprecated_since.is_empty() {
329 base.push_str(" deprecated");
330 }
331 base
332 }
333 _ => String::new(),
334 }
335 }
336 }
337
338 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
339 pub enum ItemEnum {
340 ExternCrateItem(String, Option<String>),
341 ImportItem(Import),
342 StructItem(Struct),
343 EnumItem(Enum),
344 FunctionItem(Function),
345 ModuleItem(Module),
346 TypedefItem(Typedef, bool /* is associated type */),
347 StaticItem(Static),
348 ConstantItem(Constant),
349 TraitItem(Trait),
350 ImplItem(Impl),
351 /// A method signature only. Used for required methods in traits (ie,
352 /// non-default-methods).
353 TyMethodItem(TyMethod),
354 /// A method with a body.
355 MethodItem(Method),
356 StructFieldItem(StructField),
357 VariantItem(Variant),
358 /// `fn`s from an extern block
359 ForeignFunctionItem(Function),
360 /// `static`s from an extern block
361 ForeignStaticItem(Static),
362 MacroItem(Macro),
363 PrimitiveItem(PrimitiveType),
364 AssociatedConstItem(Type, Option<String>),
365 AssociatedTypeItem(Vec<TyParamBound>, Option<Type>),
366 DefaultImplItem(DefaultImpl),
367 }
368
369 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
370 pub struct Module {
371 pub items: Vec<Item>,
372 pub is_crate: bool,
373 }
374
375 impl Clean<Item> for doctree::Module {
376 fn clean(&self, cx: &DocContext) -> Item {
377 let name = if self.name.is_some() {
378 self.name.unwrap().clean(cx)
379 } else {
380 "".to_string()
381 };
382
383 let mut items: Vec<Item> = vec![];
384 items.extend(self.extern_crates.iter().map(|x| x.clean(cx)));
385 items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
386 items.extend(self.structs.iter().map(|x| x.clean(cx)));
387 items.extend(self.enums.iter().map(|x| x.clean(cx)));
388 items.extend(self.fns.iter().map(|x| x.clean(cx)));
389 items.extend(self.foreigns.iter().flat_map(|x| x.clean(cx)));
390 items.extend(self.mods.iter().map(|x| x.clean(cx)));
391 items.extend(self.typedefs.iter().map(|x| x.clean(cx)));
392 items.extend(self.statics.iter().map(|x| x.clean(cx)));
393 items.extend(self.constants.iter().map(|x| x.clean(cx)));
394 items.extend(self.traits.iter().map(|x| x.clean(cx)));
395 items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
396 items.extend(self.macros.iter().map(|x| x.clean(cx)));
397 items.extend(self.def_traits.iter().map(|x| x.clean(cx)));
398
399 // determine if we should display the inner contents or
400 // the outer `mod` item for the source code.
401 let whence = {
402 let cm = cx.sess().codemap();
403 let outer = cm.lookup_char_pos(self.where_outer.lo);
404 let inner = cm.lookup_char_pos(self.where_inner.lo);
405 if outer.file.start_pos == inner.file.start_pos {
406 // mod foo { ... }
407 self.where_outer
408 } else {
409 // mod foo; (and a separate FileMap for the contents)
410 self.where_inner
411 }
412 };
413
414 Item {
415 name: Some(name),
416 attrs: self.attrs.clean(cx),
417 source: whence.clean(cx),
418 visibility: self.vis.clean(cx),
419 stability: self.stab.clean(cx),
420 def_id: ast_util::local_def(self.id),
421 inner: ModuleItem(Module {
422 is_crate: self.is_crate,
423 items: items
424 })
425 }
426 }
427 }
428
429 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
430 pub enum Attribute {
431 Word(String),
432 List(String, Vec<Attribute> ),
433 NameValue(String, String)
434 }
435
436 impl Clean<Attribute> for ast::MetaItem {
437 fn clean(&self, cx: &DocContext) -> Attribute {
438 match self.node {
439 ast::MetaWord(ref s) => Word(s.to_string()),
440 ast::MetaList(ref s, ref l) => {
441 List(s.to_string(), l.clean(cx))
442 }
443 ast::MetaNameValue(ref s, ref v) => {
444 NameValue(s.to_string(), lit_to_string(v))
445 }
446 }
447 }
448 }
449
450 impl Clean<Attribute> for ast::Attribute {
451 fn clean(&self, cx: &DocContext) -> Attribute {
452 self.with_desugared_doc(|a| a.node.value.clean(cx))
453 }
454 }
455
456 // This is a rough approximation that gets us what we want.
457 impl attr::AttrMetaMethods for Attribute {
458 fn name(&self) -> InternedString {
459 match *self {
460 Word(ref n) | List(ref n, _) | NameValue(ref n, _) => {
461 token::intern_and_get_ident(n)
462 }
463 }
464 }
465
466 fn value_str(&self) -> Option<InternedString> {
467 match *self {
468 NameValue(_, ref v) => {
469 Some(token::intern_and_get_ident(v))
470 }
471 _ => None,
472 }
473 }
474 fn meta_item_list<'a>(&'a self) -> Option<&'a [P<ast::MetaItem>]> { None }
475 fn span(&self) -> codemap::Span { unimplemented!() }
476 }
477 impl<'a> attr::AttrMetaMethods for &'a Attribute {
478 fn name(&self) -> InternedString { (**self).name() }
479 fn value_str(&self) -> Option<InternedString> { (**self).value_str() }
480 fn meta_item_list(&self) -> Option<&[P<ast::MetaItem>]> { None }
481 fn span(&self) -> codemap::Span { unimplemented!() }
482 }
483
484 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
485 pub struct TyParam {
486 pub name: String,
487 pub did: ast::DefId,
488 pub bounds: Vec<TyParamBound>,
489 pub default: Option<Type>,
490 }
491
492 impl Clean<TyParam> for ast::TyParam {
493 fn clean(&self, cx: &DocContext) -> TyParam {
494 TyParam {
495 name: self.ident.clean(cx),
496 did: ast::DefId { krate: ast::LOCAL_CRATE, node: self.id },
497 bounds: self.bounds.clean(cx),
498 default: self.default.clean(cx),
499 }
500 }
501 }
502
503 impl<'tcx> Clean<TyParam> for ty::TypeParameterDef<'tcx> {
504 fn clean(&self, cx: &DocContext) -> TyParam {
505 cx.external_typarams.borrow_mut().as_mut().unwrap()
506 .insert(self.def_id, self.name.clean(cx));
507 TyParam {
508 name: self.name.clean(cx),
509 did: self.def_id,
510 bounds: vec![], // these are filled in from the where-clauses
511 default: self.default.clean(cx),
512 }
513 }
514 }
515
516 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
517 pub enum TyParamBound {
518 RegionBound(Lifetime),
519 TraitBound(PolyTrait, ast::TraitBoundModifier)
520 }
521
522 impl TyParamBound {
523 fn maybe_sized(cx: &DocContext) -> TyParamBound {
524 use syntax::ast::TraitBoundModifier as TBM;
525 let mut sized_bound = ty::BoundSized.clean(cx);
526 if let TyParamBound::TraitBound(_, ref mut tbm) = sized_bound {
527 *tbm = TBM::Maybe
528 };
529 sized_bound
530 }
531
532 fn is_sized_bound(&self, cx: &DocContext) -> bool {
533 use syntax::ast::TraitBoundModifier as TBM;
534 if let Some(tcx) = cx.tcx_opt() {
535 let sized_did = match tcx.lang_items.sized_trait() {
536 Some(did) => did,
537 None => return false
538 };
539 if let TyParamBound::TraitBound(PolyTrait {
540 trait_: Type::ResolvedPath { did, .. }, ..
541 }, TBM::None) = *self {
542 if did == sized_did {
543 return true
544 }
545 }
546 }
547 false
548 }
549 }
550
551 impl Clean<TyParamBound> for ast::TyParamBound {
552 fn clean(&self, cx: &DocContext) -> TyParamBound {
553 match *self {
554 ast::RegionTyParamBound(lt) => RegionBound(lt.clean(cx)),
555 ast::TraitTyParamBound(ref t, modifier) => TraitBound(t.clean(cx), modifier),
556 }
557 }
558 }
559
560 impl<'tcx> Clean<(Vec<TyParamBound>, Vec<TypeBinding>)> for ty::ExistentialBounds<'tcx> {
561 fn clean(&self, cx: &DocContext) -> (Vec<TyParamBound>, Vec<TypeBinding>) {
562 let mut tp_bounds = vec![];
563 self.region_bound.clean(cx).map(|b| tp_bounds.push(RegionBound(b)));
564 for bb in &self.builtin_bounds {
565 tp_bounds.push(bb.clean(cx));
566 }
567
568 let mut bindings = vec![];
569 for &ty::Binder(ref pb) in &self.projection_bounds {
570 bindings.push(TypeBinding {
571 name: pb.projection_ty.item_name.clean(cx),
572 ty: pb.ty.clean(cx)
573 });
574 }
575
576 (tp_bounds, bindings)
577 }
578 }
579
580 fn external_path_params(cx: &DocContext, trait_did: Option<ast::DefId>,
581 bindings: Vec<TypeBinding>, substs: &subst::Substs) -> PathParameters {
582 let lifetimes = substs.regions().get_slice(subst::TypeSpace)
583 .iter()
584 .filter_map(|v| v.clean(cx))
585 .collect();
586 let types = substs.types.get_slice(subst::TypeSpace).to_vec();
587
588 match (trait_did, cx.tcx_opt()) {
589 // Attempt to sugar an external path like Fn<(A, B,), C> to Fn(A, B) -> C
590 (Some(did), Some(ref tcx)) if tcx.lang_items.fn_trait_kind(did).is_some() => {
591 assert_eq!(types.len(), 1);
592 let inputs = match types[0].sty {
593 ty::TyTuple(ref tys) => tys.iter().map(|t| t.clean(cx)).collect(),
594 _ => {
595 return PathParameters::AngleBracketed {
596 lifetimes: lifetimes,
597 types: types.clean(cx),
598 bindings: bindings
599 }
600 }
601 };
602 let output = None;
603 // FIXME(#20299) return type comes from a projection now
604 // match types[1].sty {
605 // ty::TyTuple(ref v) if v.is_empty() => None, // -> ()
606 // _ => Some(types[1].clean(cx))
607 // };
608 PathParameters::Parenthesized {
609 inputs: inputs,
610 output: output
611 }
612 },
613 (_, _) => {
614 PathParameters::AngleBracketed {
615 lifetimes: lifetimes,
616 types: types.clean(cx),
617 bindings: bindings
618 }
619 }
620 }
621 }
622
623 // trait_did should be set to a trait's DefId if called on a TraitRef, in order to sugar
624 // from Fn<(A, B,), C> to Fn(A, B) -> C
625 fn external_path(cx: &DocContext, name: &str, trait_did: Option<ast::DefId>,
626 bindings: Vec<TypeBinding>, substs: &subst::Substs) -> Path {
627 Path {
628 global: false,
629 segments: vec![PathSegment {
630 name: name.to_string(),
631 params: external_path_params(cx, trait_did, bindings, substs)
632 }],
633 }
634 }
635
636 impl Clean<TyParamBound> for ty::BuiltinBound {
637 fn clean(&self, cx: &DocContext) -> TyParamBound {
638 let tcx = match cx.tcx_opt() {
639 Some(tcx) => tcx,
640 None => return RegionBound(Lifetime::statik())
641 };
642 let empty = subst::Substs::empty();
643 let (did, path) = match *self {
644 ty::BoundSend =>
645 (tcx.lang_items.send_trait().unwrap(),
646 external_path(cx, "Send", None, vec![], &empty)),
647 ty::BoundSized =>
648 (tcx.lang_items.sized_trait().unwrap(),
649 external_path(cx, "Sized", None, vec![], &empty)),
650 ty::BoundCopy =>
651 (tcx.lang_items.copy_trait().unwrap(),
652 external_path(cx, "Copy", None, vec![], &empty)),
653 ty::BoundSync =>
654 (tcx.lang_items.sync_trait().unwrap(),
655 external_path(cx, "Sync", None, vec![], &empty)),
656 };
657 let fqn = csearch::get_item_path(tcx, did);
658 let fqn = fqn.into_iter().map(|i| i.to_string()).collect();
659 cx.external_paths.borrow_mut().as_mut().unwrap().insert(did,
660 (fqn, TypeTrait));
661 TraitBound(PolyTrait {
662 trait_: ResolvedPath {
663 path: path,
664 typarams: None,
665 did: did,
666 is_generic: false,
667 },
668 lifetimes: vec![]
669 }, ast::TraitBoundModifier::None)
670 }
671 }
672
673 impl<'tcx> Clean<TyParamBound> for ty::TraitRef<'tcx> {
674 fn clean(&self, cx: &DocContext) -> TyParamBound {
675 let tcx = match cx.tcx_opt() {
676 Some(tcx) => tcx,
677 None => return RegionBound(Lifetime::statik())
678 };
679 let fqn = csearch::get_item_path(tcx, self.def_id);
680 let fqn = fqn.into_iter().map(|i| i.to_string())
681 .collect::<Vec<String>>();
682 let path = external_path(cx, fqn.last().unwrap(),
683 Some(self.def_id), vec![], self.substs);
684 cx.external_paths.borrow_mut().as_mut().unwrap().insert(self.def_id,
685 (fqn, TypeTrait));
686
687 debug!("ty::TraitRef\n substs.types(TypeSpace): {:?}\n",
688 self.substs.types.get_slice(ParamSpace::TypeSpace));
689
690 // collect any late bound regions
691 let mut late_bounds = vec![];
692 for &ty_s in self.substs.types.get_slice(ParamSpace::TypeSpace) {
693 if let ty::TyTuple(ref ts) = ty_s.sty {
694 for &ty_s in ts {
695 if let ty::TyRef(ref reg, _) = ty_s.sty {
696 if let &ty::Region::ReLateBound(_, _) = *reg {
697 debug!(" hit an ReLateBound {:?}", reg);
698 if let Some(lt) = reg.clean(cx) {
699 late_bounds.push(lt)
700 }
701 }
702 }
703 }
704 }
705 }
706
707 TraitBound(PolyTrait {
708 trait_: ResolvedPath {
709 path: path,
710 typarams: None,
711 did: self.def_id,
712 is_generic: false,
713 },
714 lifetimes: late_bounds
715 }, ast::TraitBoundModifier::None)
716 }
717 }
718
719 impl<'tcx> Clean<Option<Vec<TyParamBound>>> for subst::Substs<'tcx> {
720 fn clean(&self, cx: &DocContext) -> Option<Vec<TyParamBound>> {
721 let mut v = Vec::new();
722 v.extend(self.regions().iter().filter_map(|r| r.clean(cx)).map(RegionBound));
723 v.extend(self.types.iter().map(|t| TraitBound(PolyTrait {
724 trait_: t.clean(cx),
725 lifetimes: vec![]
726 }, ast::TraitBoundModifier::None)));
727 if !v.is_empty() {Some(v)} else {None}
728 }
729 }
730
731 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
732 pub struct Lifetime(String);
733
734 impl Lifetime {
735 pub fn get_ref<'a>(&'a self) -> &'a str {
736 let Lifetime(ref s) = *self;
737 let s: &'a str = s;
738 return s;
739 }
740
741 pub fn statik() -> Lifetime {
742 Lifetime("'static".to_string())
743 }
744 }
745
746 impl Clean<Lifetime> for ast::Lifetime {
747 fn clean(&self, _: &DocContext) -> Lifetime {
748 Lifetime(token::get_name(self.name).to_string())
749 }
750 }
751
752 impl Clean<Lifetime> for ast::LifetimeDef {
753 fn clean(&self, _: &DocContext) -> Lifetime {
754 Lifetime(token::get_name(self.lifetime.name).to_string())
755 }
756 }
757
758 impl Clean<Lifetime> for ty::RegionParameterDef {
759 fn clean(&self, _: &DocContext) -> Lifetime {
760 Lifetime(token::get_name(self.name).to_string())
761 }
762 }
763
764 impl Clean<Option<Lifetime>> for ty::Region {
765 fn clean(&self, cx: &DocContext) -> Option<Lifetime> {
766 match *self {
767 ty::ReStatic => Some(Lifetime::statik()),
768 ty::ReLateBound(_, ty::BrNamed(_, name)) =>
769 Some(Lifetime(token::get_name(name).to_string())),
770 ty::ReEarlyBound(ref data) => Some(Lifetime(data.name.clean(cx))),
771
772 ty::ReLateBound(..) |
773 ty::ReFree(..) |
774 ty::ReScope(..) |
775 ty::ReInfer(..) |
776 ty::ReEmpty(..) => None
777 }
778 }
779 }
780
781 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
782 pub enum WherePredicate {
783 BoundPredicate { ty: Type, bounds: Vec<TyParamBound> },
784 RegionPredicate { lifetime: Lifetime, bounds: Vec<Lifetime>},
785 EqPredicate { lhs: Type, rhs: Type }
786 }
787
788 impl Clean<WherePredicate> for ast::WherePredicate {
789 fn clean(&self, cx: &DocContext) -> WherePredicate {
790 match *self {
791 ast::WherePredicate::BoundPredicate(ref wbp) => {
792 WherePredicate::BoundPredicate {
793 ty: wbp.bounded_ty.clean(cx),
794 bounds: wbp.bounds.clean(cx)
795 }
796 }
797
798 ast::WherePredicate::RegionPredicate(ref wrp) => {
799 WherePredicate::RegionPredicate {
800 lifetime: wrp.lifetime.clean(cx),
801 bounds: wrp.bounds.clean(cx)
802 }
803 }
804
805 ast::WherePredicate::EqPredicate(_) => {
806 unimplemented!() // FIXME(#20041)
807 }
808 }
809 }
810 }
811
812 impl<'a> Clean<WherePredicate> for ty::Predicate<'a> {
813 fn clean(&self, cx: &DocContext) -> WherePredicate {
814 use rustc::middle::ty::Predicate;
815
816 match *self {
817 Predicate::Trait(ref pred) => pred.clean(cx),
818 Predicate::Equate(ref pred) => pred.clean(cx),
819 Predicate::RegionOutlives(ref pred) => pred.clean(cx),
820 Predicate::TypeOutlives(ref pred) => pred.clean(cx),
821 Predicate::Projection(ref pred) => pred.clean(cx)
822 }
823 }
824 }
825
826 impl<'a> Clean<WherePredicate> for ty::TraitPredicate<'a> {
827 fn clean(&self, cx: &DocContext) -> WherePredicate {
828 WherePredicate::BoundPredicate {
829 ty: self.trait_ref.substs.self_ty().clean(cx).unwrap(),
830 bounds: vec![self.trait_ref.clean(cx)]
831 }
832 }
833 }
834
835 impl<'tcx> Clean<WherePredicate> for ty::EquatePredicate<'tcx> {
836 fn clean(&self, cx: &DocContext) -> WherePredicate {
837 let ty::EquatePredicate(ref lhs, ref rhs) = *self;
838 WherePredicate::EqPredicate {
839 lhs: lhs.clean(cx),
840 rhs: rhs.clean(cx)
841 }
842 }
843 }
844
845 impl Clean<WherePredicate> for ty::OutlivesPredicate<ty::Region, ty::Region> {
846 fn clean(&self, cx: &DocContext) -> WherePredicate {
847 let ty::OutlivesPredicate(ref a, ref b) = *self;
848 WherePredicate::RegionPredicate {
849 lifetime: a.clean(cx).unwrap(),
850 bounds: vec![b.clean(cx).unwrap()]
851 }
852 }
853 }
854
855 impl<'tcx> Clean<WherePredicate> for ty::OutlivesPredicate<ty::Ty<'tcx>, ty::Region> {
856 fn clean(&self, cx: &DocContext) -> WherePredicate {
857 let ty::OutlivesPredicate(ref ty, ref lt) = *self;
858
859 WherePredicate::BoundPredicate {
860 ty: ty.clean(cx),
861 bounds: vec![TyParamBound::RegionBound(lt.clean(cx).unwrap())]
862 }
863 }
864 }
865
866 impl<'tcx> Clean<WherePredicate> for ty::ProjectionPredicate<'tcx> {
867 fn clean(&self, cx: &DocContext) -> WherePredicate {
868 WherePredicate::EqPredicate {
869 lhs: self.projection_ty.clean(cx),
870 rhs: self.ty.clean(cx)
871 }
872 }
873 }
874
875 impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
876 fn clean(&self, cx: &DocContext) -> Type {
877 let trait_ = match self.trait_ref.clean(cx) {
878 TyParamBound::TraitBound(t, _) => t.trait_,
879 TyParamBound::RegionBound(_) => {
880 panic!("cleaning a trait got a region")
881 }
882 };
883 Type::QPath {
884 name: self.item_name.clean(cx),
885 self_type: box self.trait_ref.self_ty().clean(cx),
886 trait_: box trait_
887 }
888 }
889 }
890
891 // maybe use a Generic enum and use Vec<Generic>?
892 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
893 pub struct Generics {
894 pub lifetimes: Vec<Lifetime>,
895 pub type_params: Vec<TyParam>,
896 pub where_predicates: Vec<WherePredicate>
897 }
898
899 impl Clean<Generics> for ast::Generics {
900 fn clean(&self, cx: &DocContext) -> Generics {
901 Generics {
902 lifetimes: self.lifetimes.clean(cx),
903 type_params: self.ty_params.clean(cx),
904 where_predicates: self.where_clause.predicates.clean(cx)
905 }
906 }
907 }
908
909 impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics<'tcx>,
910 &'a ty::GenericPredicates<'tcx>,
911 subst::ParamSpace) {
912 fn clean(&self, cx: &DocContext) -> Generics {
913 use std::collections::HashSet;
914 use self::WherePredicate as WP;
915
916 let (gens, preds, space) = *self;
917
918 // Bounds in the type_params and lifetimes fields are repeated in the
919 // predicates field (see rustc_typeck::collect::ty_generics), so remove
920 // them.
921 let stripped_typarams = gens.types.get_slice(space).iter().map(|tp| {
922 tp.clean(cx)
923 }).collect::<Vec<_>>();
924 let stripped_lifetimes = gens.regions.get_slice(space).iter().map(|rp| {
925 let mut srp = rp.clone();
926 srp.bounds = Vec::new();
927 srp.clean(cx)
928 }).collect::<Vec<_>>();
929
930 let mut where_predicates = preds.predicates.get_slice(space)
931 .to_vec().clean(cx);
932
933 // Type parameters and have a Sized bound by default unless removed with
934 // ?Sized. Scan through the predicates and mark any type parameter with
935 // a Sized bound, removing the bounds as we find them.
936 //
937 // Note that associated types also have a sized bound by default, but we
938 // don't actually know the set of associated types right here so that's
939 // handled in cleaning associated types
940 let mut sized_params = HashSet::new();
941 where_predicates.retain(|pred| {
942 match *pred {
943 WP::BoundPredicate { ty: Generic(ref g), ref bounds } => {
944 if bounds.iter().any(|b| b.is_sized_bound(cx)) {
945 sized_params.insert(g.clone());
946 false
947 } else {
948 true
949 }
950 }
951 _ => true,
952 }
953 });
954
955 // Run through the type parameters again and insert a ?Sized
956 // unbound for any we didn't find to be Sized.
957 for tp in &stripped_typarams {
958 if !sized_params.contains(&tp.name) {
959 where_predicates.push(WP::BoundPredicate {
960 ty: Type::Generic(tp.name.clone()),
961 bounds: vec![TyParamBound::maybe_sized(cx)],
962 })
963 }
964 }
965
966 // It would be nice to collect all of the bounds on a type and recombine
967 // them if possible, to avoid e.g. `where T: Foo, T: Bar, T: Sized, T: 'a`
968 // and instead see `where T: Foo + Bar + Sized + 'a`
969
970 Generics {
971 type_params: simplify::ty_params(stripped_typarams),
972 lifetimes: stripped_lifetimes,
973 where_predicates: simplify::where_clauses(cx, where_predicates),
974 }
975 }
976 }
977
978 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
979 pub struct Method {
980 pub generics: Generics,
981 pub self_: SelfTy,
982 pub unsafety: ast::Unsafety,
983 pub constness: ast::Constness,
984 pub decl: FnDecl,
985 pub abi: abi::Abi
986 }
987
988 impl Clean<Method> for ast::MethodSig {
989 fn clean(&self, cx: &DocContext) -> Method {
990 let all_inputs = &self.decl.inputs;
991 let inputs = match self.explicit_self.node {
992 ast::SelfStatic => &**all_inputs,
993 _ => &all_inputs[1..]
994 };
995 let decl = FnDecl {
996 inputs: Arguments {
997 values: inputs.clean(cx),
998 },
999 output: self.decl.output.clean(cx),
1000 attrs: Vec::new()
1001 };
1002 Method {
1003 generics: self.generics.clean(cx),
1004 self_: self.explicit_self.node.clean(cx),
1005 unsafety: self.unsafety,
1006 constness: self.constness,
1007 decl: decl,
1008 abi: self.abi
1009 }
1010 }
1011 }
1012
1013 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1014 pub struct TyMethod {
1015 pub unsafety: ast::Unsafety,
1016 pub decl: FnDecl,
1017 pub generics: Generics,
1018 pub self_: SelfTy,
1019 pub abi: abi::Abi
1020 }
1021
1022 impl Clean<TyMethod> for ast::MethodSig {
1023 fn clean(&self, cx: &DocContext) -> TyMethod {
1024 let inputs = match self.explicit_self.node {
1025 ast::SelfStatic => &*self.decl.inputs,
1026 _ => &self.decl.inputs[1..]
1027 };
1028 let decl = FnDecl {
1029 inputs: Arguments {
1030 values: inputs.clean(cx),
1031 },
1032 output: self.decl.output.clean(cx),
1033 attrs: Vec::new()
1034 };
1035 TyMethod {
1036 unsafety: self.unsafety.clone(),
1037 decl: decl,
1038 self_: self.explicit_self.node.clean(cx),
1039 generics: self.generics.clean(cx),
1040 abi: self.abi
1041 }
1042 }
1043 }
1044
1045 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
1046 pub enum SelfTy {
1047 SelfStatic,
1048 SelfValue,
1049 SelfBorrowed(Option<Lifetime>, Mutability),
1050 SelfExplicit(Type),
1051 }
1052
1053 impl Clean<SelfTy> for ast::ExplicitSelf_ {
1054 fn clean(&self, cx: &DocContext) -> SelfTy {
1055 match *self {
1056 ast::SelfStatic => SelfStatic,
1057 ast::SelfValue(_) => SelfValue,
1058 ast::SelfRegion(ref lt, ref mt, _) => {
1059 SelfBorrowed(lt.clean(cx), mt.clean(cx))
1060 }
1061 ast::SelfExplicit(ref typ, _) => SelfExplicit(typ.clean(cx)),
1062 }
1063 }
1064 }
1065
1066 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1067 pub struct Function {
1068 pub decl: FnDecl,
1069 pub generics: Generics,
1070 pub unsafety: ast::Unsafety,
1071 pub constness: ast::Constness,
1072 pub abi: abi::Abi,
1073 }
1074
1075 impl Clean<Item> for doctree::Function {
1076 fn clean(&self, cx: &DocContext) -> Item {
1077 Item {
1078 name: Some(self.name.clean(cx)),
1079 attrs: self.attrs.clean(cx),
1080 source: self.whence.clean(cx),
1081 visibility: self.vis.clean(cx),
1082 stability: self.stab.clean(cx),
1083 def_id: ast_util::local_def(self.id),
1084 inner: FunctionItem(Function {
1085 decl: self.decl.clean(cx),
1086 generics: self.generics.clean(cx),
1087 unsafety: self.unsafety,
1088 constness: self.constness,
1089 abi: self.abi,
1090 }),
1091 }
1092 }
1093 }
1094
1095 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
1096 pub struct FnDecl {
1097 pub inputs: Arguments,
1098 pub output: FunctionRetTy,
1099 pub attrs: Vec<Attribute>,
1100 }
1101
1102 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
1103 pub struct Arguments {
1104 pub values: Vec<Argument>,
1105 }
1106
1107 impl Clean<FnDecl> for ast::FnDecl {
1108 fn clean(&self, cx: &DocContext) -> FnDecl {
1109 FnDecl {
1110 inputs: Arguments {
1111 values: self.inputs.clean(cx),
1112 },
1113 output: self.output.clean(cx),
1114 attrs: Vec::new()
1115 }
1116 }
1117 }
1118
1119 impl<'tcx> Clean<Type> for ty::FnOutput<'tcx> {
1120 fn clean(&self, cx: &DocContext) -> Type {
1121 match *self {
1122 ty::FnConverging(ty) => ty.clean(cx),
1123 ty::FnDiverging => Bottom
1124 }
1125 }
1126 }
1127
1128 impl<'a, 'tcx> Clean<FnDecl> for (ast::DefId, &'a ty::PolyFnSig<'tcx>) {
1129 fn clean(&self, cx: &DocContext) -> FnDecl {
1130 let (did, sig) = *self;
1131 let mut names = if did.node != 0 {
1132 csearch::get_method_arg_names(&cx.tcx().sess.cstore, did).into_iter()
1133 } else {
1134 Vec::new().into_iter()
1135 }.peekable();
1136 if names.peek().map(|s| &**s) == Some("self") {
1137 let _ = names.next();
1138 }
1139 FnDecl {
1140 output: Return(sig.0.output.clean(cx)),
1141 attrs: Vec::new(),
1142 inputs: Arguments {
1143 values: sig.0.inputs.iter().map(|t| {
1144 Argument {
1145 type_: t.clean(cx),
1146 id: 0,
1147 name: names.next().unwrap_or("".to_string()),
1148 }
1149 }).collect(),
1150 },
1151 }
1152 }
1153 }
1154
1155 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
1156 pub struct Argument {
1157 pub type_: Type,
1158 pub name: String,
1159 pub id: ast::NodeId,
1160 }
1161
1162 impl Clean<Argument> for ast::Arg {
1163 fn clean(&self, cx: &DocContext) -> Argument {
1164 Argument {
1165 name: name_from_pat(&*self.pat),
1166 type_: (self.ty.clean(cx)),
1167 id: self.id
1168 }
1169 }
1170 }
1171
1172 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
1173 pub enum FunctionRetTy {
1174 Return(Type),
1175 DefaultReturn,
1176 NoReturn
1177 }
1178
1179 impl Clean<FunctionRetTy> for ast::FunctionRetTy {
1180 fn clean(&self, cx: &DocContext) -> FunctionRetTy {
1181 match *self {
1182 ast::Return(ref typ) => Return(typ.clean(cx)),
1183 ast::DefaultReturn(..) => DefaultReturn,
1184 ast::NoReturn(..) => NoReturn
1185 }
1186 }
1187 }
1188
1189 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1190 pub struct Trait {
1191 pub unsafety: ast::Unsafety,
1192 pub items: Vec<Item>,
1193 pub generics: Generics,
1194 pub bounds: Vec<TyParamBound>,
1195 }
1196
1197 impl Clean<Item> for doctree::Trait {
1198 fn clean(&self, cx: &DocContext) -> Item {
1199 Item {
1200 name: Some(self.name.clean(cx)),
1201 attrs: self.attrs.clean(cx),
1202 source: self.whence.clean(cx),
1203 def_id: ast_util::local_def(self.id),
1204 visibility: self.vis.clean(cx),
1205 stability: self.stab.clean(cx),
1206 inner: TraitItem(Trait {
1207 unsafety: self.unsafety,
1208 items: self.items.clean(cx),
1209 generics: self.generics.clean(cx),
1210 bounds: self.bounds.clean(cx),
1211 }),
1212 }
1213 }
1214 }
1215
1216 impl Clean<Type> for ast::TraitRef {
1217 fn clean(&self, cx: &DocContext) -> Type {
1218 resolve_type(cx, self.path.clean(cx), self.ref_id)
1219 }
1220 }
1221
1222 impl Clean<PolyTrait> for ast::PolyTraitRef {
1223 fn clean(&self, cx: &DocContext) -> PolyTrait {
1224 PolyTrait {
1225 trait_: self.trait_ref.clean(cx),
1226 lifetimes: self.bound_lifetimes.clean(cx)
1227 }
1228 }
1229 }
1230
1231 impl Clean<Item> for ast::TraitItem {
1232 fn clean(&self, cx: &DocContext) -> Item {
1233 let inner = match self.node {
1234 ast::ConstTraitItem(ref ty, ref default) => {
1235 AssociatedConstItem(ty.clean(cx),
1236 default.as_ref().map(|expr|
1237 expr.span.to_src(cx)))
1238 }
1239 ast::MethodTraitItem(ref sig, Some(_)) => {
1240 MethodItem(sig.clean(cx))
1241 }
1242 ast::MethodTraitItem(ref sig, None) => {
1243 TyMethodItem(sig.clean(cx))
1244 }
1245 ast::TypeTraitItem(ref bounds, ref default) => {
1246 AssociatedTypeItem(bounds.clean(cx), default.clean(cx))
1247 }
1248 };
1249 Item {
1250 name: Some(self.ident.clean(cx)),
1251 attrs: self.attrs.clean(cx),
1252 source: self.span.clean(cx),
1253 def_id: ast_util::local_def(self.id),
1254 visibility: None,
1255 stability: get_stability(cx, ast_util::local_def(self.id)),
1256 inner: inner
1257 }
1258 }
1259 }
1260
1261 impl Clean<Item> for ast::ImplItem {
1262 fn clean(&self, cx: &DocContext) -> Item {
1263 let inner = match self.node {
1264 ast::ConstImplItem(ref ty, ref expr) => {
1265 ConstantItem(Constant{
1266 type_: ty.clean(cx),
1267 expr: expr.span.to_src(cx),
1268 })
1269 }
1270 ast::MethodImplItem(ref sig, _) => {
1271 MethodItem(sig.clean(cx))
1272 }
1273 ast::TypeImplItem(ref ty) => TypedefItem(Typedef {
1274 type_: ty.clean(cx),
1275 generics: Generics {
1276 lifetimes: Vec::new(),
1277 type_params: Vec::new(),
1278 where_predicates: Vec::new()
1279 },
1280 }, true),
1281 ast::MacImplItem(_) => {
1282 MacroItem(Macro {
1283 source: self.span.to_src(cx),
1284 imported_from: None,
1285 })
1286 }
1287 };
1288 Item {
1289 name: Some(self.ident.clean(cx)),
1290 source: self.span.clean(cx),
1291 attrs: self.attrs.clean(cx),
1292 def_id: ast_util::local_def(self.id),
1293 visibility: self.vis.clean(cx),
1294 stability: get_stability(cx, ast_util::local_def(self.id)),
1295 inner: inner
1296 }
1297 }
1298 }
1299
1300 impl<'tcx> Clean<Item> for ty::Method<'tcx> {
1301 fn clean(&self, cx: &DocContext) -> Item {
1302 let (self_, sig) = match self.explicit_self {
1303 ty::StaticExplicitSelfCategory => (ast::SelfStatic.clean(cx),
1304 self.fty.sig.clone()),
1305 s => {
1306 let sig = ty::Binder(ty::FnSig {
1307 inputs: self.fty.sig.0.inputs[1..].to_vec(),
1308 ..self.fty.sig.0.clone()
1309 });
1310 let s = match s {
1311 ty::ByValueExplicitSelfCategory => SelfValue,
1312 ty::ByReferenceExplicitSelfCategory(..) => {
1313 match self.fty.sig.0.inputs[0].sty {
1314 ty::TyRef(r, mt) => {
1315 SelfBorrowed(r.clean(cx), mt.mutbl.clean(cx))
1316 }
1317 _ => unreachable!(),
1318 }
1319 }
1320 ty::ByBoxExplicitSelfCategory => {
1321 SelfExplicit(self.fty.sig.0.inputs[0].clean(cx))
1322 }
1323 ty::StaticExplicitSelfCategory => unreachable!(),
1324 };
1325 (s, sig)
1326 }
1327 };
1328
1329 let generics = (&self.generics, &self.predicates,
1330 subst::FnSpace).clean(cx);
1331 let decl = (self.def_id, &sig).clean(cx);
1332 let provided = match self.container {
1333 ty::ImplContainer(..) => false,
1334 ty::TraitContainer(did) => {
1335 ty::provided_trait_methods(cx.tcx(), did).iter().any(|m| {
1336 m.def_id == self.def_id
1337 })
1338 }
1339 };
1340 let inner = if provided {
1341 MethodItem(Method {
1342 unsafety: self.fty.unsafety,
1343 generics: generics,
1344 self_: self_,
1345 decl: decl,
1346 abi: self.fty.abi,
1347
1348 // trait methods canot (currently, at least) be const
1349 constness: ast::Constness::NotConst,
1350 })
1351 } else {
1352 TyMethodItem(TyMethod {
1353 unsafety: self.fty.unsafety,
1354 generics: generics,
1355 self_: self_,
1356 decl: decl,
1357 abi: self.fty.abi,
1358 })
1359 };
1360
1361 Item {
1362 name: Some(self.name.clean(cx)),
1363 visibility: Some(ast::Inherited),
1364 stability: get_stability(cx, self.def_id),
1365 def_id: self.def_id,
1366 attrs: inline::load_attrs(cx, cx.tcx(), self.def_id),
1367 source: Span::empty(),
1368 inner: inner,
1369 }
1370 }
1371 }
1372
1373 impl<'tcx> Clean<Item> for ty::ImplOrTraitItem<'tcx> {
1374 fn clean(&self, cx: &DocContext) -> Item {
1375 match *self {
1376 ty::ConstTraitItem(ref cti) => cti.clean(cx),
1377 ty::MethodTraitItem(ref mti) => mti.clean(cx),
1378 ty::TypeTraitItem(ref tti) => tti.clean(cx),
1379 }
1380 }
1381 }
1382
1383 /// A trait reference, which may have higher ranked lifetimes.
1384 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
1385 pub struct PolyTrait {
1386 pub trait_: Type,
1387 pub lifetimes: Vec<Lifetime>
1388 }
1389
1390 /// A representation of a Type suitable for hyperlinking purposes. Ideally one can get the original
1391 /// type out of the AST/ty::ctxt given one of these, if more information is needed. Most importantly
1392 /// it does not preserve mutability or boxes.
1393 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
1394 pub enum Type {
1395 /// structs/enums/traits (most that'd be an ast::TyPath)
1396 ResolvedPath {
1397 path: Path,
1398 typarams: Option<Vec<TyParamBound>>,
1399 did: ast::DefId,
1400 /// true if is a `T::Name` path for associated types
1401 is_generic: bool,
1402 },
1403 /// For parameterized types, so the consumer of the JSON don't go
1404 /// looking for types which don't exist anywhere.
1405 Generic(String),
1406 /// Primitives are the fixed-size numeric types (plus int/usize/float), char,
1407 /// arrays, slices, and tuples.
1408 Primitive(PrimitiveType),
1409 /// extern "ABI" fn
1410 BareFunction(Box<BareFunctionDecl>),
1411 Tuple(Vec<Type>),
1412 Vector(Box<Type>),
1413 FixedVector(Box<Type>, String),
1414 /// aka TyBot
1415 Bottom,
1416 Unique(Box<Type>),
1417 RawPointer(Mutability, Box<Type>),
1418 BorrowedRef {
1419 lifetime: Option<Lifetime>,
1420 mutability: Mutability,
1421 type_: Box<Type>,
1422 },
1423
1424 // <Type as Trait>::Name
1425 QPath {
1426 name: String,
1427 self_type: Box<Type>,
1428 trait_: Box<Type>
1429 },
1430
1431 // _
1432 Infer,
1433
1434 // for<'a> Foo(&'a)
1435 PolyTraitRef(Vec<TyParamBound>),
1436 }
1437
1438 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Copy, Debug)]
1439 pub enum PrimitiveType {
1440 Isize, I8, I16, I32, I64,
1441 Usize, U8, U16, U32, U64,
1442 F32, F64,
1443 Char,
1444 Bool,
1445 Str,
1446 Slice,
1447 Array,
1448 PrimitiveTuple,
1449 PrimitiveRawPointer,
1450 }
1451
1452 #[derive(Clone, RustcEncodable, RustcDecodable, Copy, Debug)]
1453 pub enum TypeKind {
1454 TypeEnum,
1455 TypeFunction,
1456 TypeModule,
1457 TypeConst,
1458 TypeStatic,
1459 TypeStruct,
1460 TypeTrait,
1461 TypeVariant,
1462 TypeTypedef,
1463 }
1464
1465 impl Type {
1466 pub fn primitive_type(&self) -> Option<PrimitiveType> {
1467 match *self {
1468 Primitive(p) | BorrowedRef { type_: box Primitive(p), ..} => Some(p),
1469 Vector(..) | BorrowedRef{ type_: box Vector(..), .. } => Some(Slice),
1470 FixedVector(..) | BorrowedRef { type_: box FixedVector(..), .. } => {
1471 Some(Array)
1472 }
1473 Tuple(..) => Some(PrimitiveTuple),
1474 RawPointer(..) => Some(PrimitiveRawPointer),
1475 _ => None,
1476 }
1477 }
1478 }
1479
1480 impl PrimitiveType {
1481 fn from_str(s: &str) -> Option<PrimitiveType> {
1482 match s {
1483 "isize" => Some(Isize),
1484 "i8" => Some(I8),
1485 "i16" => Some(I16),
1486 "i32" => Some(I32),
1487 "i64" => Some(I64),
1488 "usize" => Some(Usize),
1489 "u8" => Some(U8),
1490 "u16" => Some(U16),
1491 "u32" => Some(U32),
1492 "u64" => Some(U64),
1493 "bool" => Some(Bool),
1494 "char" => Some(Char),
1495 "str" => Some(Str),
1496 "f32" => Some(F32),
1497 "f64" => Some(F64),
1498 "array" => Some(Array),
1499 "slice" => Some(Slice),
1500 "tuple" => Some(PrimitiveTuple),
1501 "pointer" => Some(PrimitiveRawPointer),
1502 _ => None,
1503 }
1504 }
1505
1506 fn find(attrs: &[Attribute]) -> Option<PrimitiveType> {
1507 for attr in attrs {
1508 let list = match *attr {
1509 List(ref k, ref l) if *k == "doc" => l,
1510 _ => continue,
1511 };
1512 for sub_attr in list {
1513 let value = match *sub_attr {
1514 NameValue(ref k, ref v)
1515 if *k == "primitive" => v,
1516 _ => continue,
1517 };
1518 match PrimitiveType::from_str(value) {
1519 Some(p) => return Some(p),
1520 None => {}
1521 }
1522 }
1523 }
1524 return None
1525 }
1526
1527 pub fn to_string(&self) -> &'static str {
1528 match *self {
1529 Isize => "isize",
1530 I8 => "i8",
1531 I16 => "i16",
1532 I32 => "i32",
1533 I64 => "i64",
1534 Usize => "usize",
1535 U8 => "u8",
1536 U16 => "u16",
1537 U32 => "u32",
1538 U64 => "u64",
1539 F32 => "f32",
1540 F64 => "f64",
1541 Str => "str",
1542 Bool => "bool",
1543 Char => "char",
1544 Array => "array",
1545 Slice => "slice",
1546 PrimitiveTuple => "tuple",
1547 PrimitiveRawPointer => "pointer",
1548 }
1549 }
1550
1551 pub fn to_url_str(&self) -> &'static str {
1552 self.to_string()
1553 }
1554
1555 /// Creates a rustdoc-specific node id for primitive types.
1556 ///
1557 /// These node ids are generally never used by the AST itself.
1558 pub fn to_node_id(&self) -> ast::NodeId {
1559 u32::MAX - 1 - (*self as u32)
1560 }
1561 }
1562
1563 impl Clean<Type> for ast::Ty {
1564 fn clean(&self, cx: &DocContext) -> Type {
1565 use syntax::ast::*;
1566 match self.node {
1567 TyPtr(ref m) => RawPointer(m.mutbl.clean(cx), box m.ty.clean(cx)),
1568 TyRptr(ref l, ref m) =>
1569 BorrowedRef {lifetime: l.clean(cx), mutability: m.mutbl.clean(cx),
1570 type_: box m.ty.clean(cx)},
1571 TyVec(ref ty) => Vector(box ty.clean(cx)),
1572 TyFixedLengthVec(ref ty, ref e) => FixedVector(box ty.clean(cx),
1573 e.span.to_src(cx)),
1574 TyTup(ref tys) => Tuple(tys.clean(cx)),
1575 TyPath(None, ref p) => {
1576 resolve_type(cx, p.clean(cx), self.id)
1577 }
1578 TyPath(Some(ref qself), ref p) => {
1579 let mut trait_path = p.clone();
1580 trait_path.segments.pop();
1581 Type::QPath {
1582 name: p.segments.last().unwrap().identifier.clean(cx),
1583 self_type: box qself.ty.clean(cx),
1584 trait_: box resolve_type(cx, trait_path.clean(cx), self.id)
1585 }
1586 }
1587 TyObjectSum(ref lhs, ref bounds) => {
1588 let lhs_ty = lhs.clean(cx);
1589 match lhs_ty {
1590 ResolvedPath { path, typarams: None, did, is_generic } => {
1591 ResolvedPath {
1592 path: path,
1593 typarams: Some(bounds.clean(cx)),
1594 did: did,
1595 is_generic: is_generic,
1596 }
1597 }
1598 _ => {
1599 lhs_ty // shouldn't happen
1600 }
1601 }
1602 }
1603 TyBareFn(ref barefn) => BareFunction(box barefn.clean(cx)),
1604 TyParen(ref ty) => ty.clean(cx),
1605 TyPolyTraitRef(ref bounds) => {
1606 PolyTraitRef(bounds.clean(cx))
1607 },
1608 TyInfer(..) => {
1609 Infer
1610 },
1611 TyTypeof(..) => {
1612 panic!("Unimplemented type {:?}", self.node)
1613 },
1614 }
1615 }
1616 }
1617
1618 impl<'tcx> Clean<Type> for ty::Ty<'tcx> {
1619 fn clean(&self, cx: &DocContext) -> Type {
1620 match self.sty {
1621 ty::TyBool => Primitive(Bool),
1622 ty::TyChar => Primitive(Char),
1623 ty::TyInt(ast::TyIs) => Primitive(Isize),
1624 ty::TyInt(ast::TyI8) => Primitive(I8),
1625 ty::TyInt(ast::TyI16) => Primitive(I16),
1626 ty::TyInt(ast::TyI32) => Primitive(I32),
1627 ty::TyInt(ast::TyI64) => Primitive(I64),
1628 ty::TyUint(ast::TyUs) => Primitive(Usize),
1629 ty::TyUint(ast::TyU8) => Primitive(U8),
1630 ty::TyUint(ast::TyU16) => Primitive(U16),
1631 ty::TyUint(ast::TyU32) => Primitive(U32),
1632 ty::TyUint(ast::TyU64) => Primitive(U64),
1633 ty::TyFloat(ast::TyF32) => Primitive(F32),
1634 ty::TyFloat(ast::TyF64) => Primitive(F64),
1635 ty::TyStr => Primitive(Str),
1636 ty::TyBox(t) => {
1637 let box_did = cx.tcx_opt().and_then(|tcx| {
1638 tcx.lang_items.owned_box()
1639 });
1640 lang_struct(cx, box_did, t, "Box", Unique)
1641 }
1642 ty::TySlice(ty) => Vector(box ty.clean(cx)),
1643 ty::TyArray(ty, i) => FixedVector(box ty.clean(cx),
1644 format!("{}", i)),
1645 ty::TyRawPtr(mt) => RawPointer(mt.mutbl.clean(cx), box mt.ty.clean(cx)),
1646 ty::TyRef(r, mt) => BorrowedRef {
1647 lifetime: r.clean(cx),
1648 mutability: mt.mutbl.clean(cx),
1649 type_: box mt.ty.clean(cx),
1650 },
1651 ty::TyBareFn(_, ref fty) => BareFunction(box BareFunctionDecl {
1652 unsafety: fty.unsafety,
1653 generics: Generics {
1654 lifetimes: Vec::new(),
1655 type_params: Vec::new(),
1656 where_predicates: Vec::new()
1657 },
1658 decl: (ast_util::local_def(0), &fty.sig).clean(cx),
1659 abi: fty.abi.to_string(),
1660 }),
1661 ty::TyStruct(did, substs) |
1662 ty::TyEnum(did, substs) => {
1663 let fqn = csearch::get_item_path(cx.tcx(), did);
1664 let fqn: Vec<_> = fqn.into_iter().map(|i| i.to_string()).collect();
1665 let kind = match self.sty {
1666 ty::TyStruct(..) => TypeStruct,
1667 _ => TypeEnum,
1668 };
1669 let path = external_path(cx, &fqn.last().unwrap().to_string(),
1670 None, vec![], substs);
1671 cx.external_paths.borrow_mut().as_mut().unwrap().insert(did, (fqn, kind));
1672 ResolvedPath {
1673 path: path,
1674 typarams: None,
1675 did: did,
1676 is_generic: false,
1677 }
1678 }
1679 ty::TyTrait(box ty::TraitTy { ref principal, ref bounds }) => {
1680 let did = principal.def_id();
1681 let fqn = csearch::get_item_path(cx.tcx(), did);
1682 let fqn: Vec<_> = fqn.into_iter().map(|i| i.to_string()).collect();
1683 let (typarams, bindings) = bounds.clean(cx);
1684 let path = external_path(cx, &fqn.last().unwrap().to_string(),
1685 Some(did), bindings, principal.substs());
1686 cx.external_paths.borrow_mut().as_mut().unwrap().insert(did, (fqn, TypeTrait));
1687 ResolvedPath {
1688 path: path,
1689 typarams: Some(typarams),
1690 did: did,
1691 is_generic: false,
1692 }
1693 }
1694 ty::TyTuple(ref t) => Tuple(t.clean(cx)),
1695
1696 ty::TyProjection(ref data) => data.clean(cx),
1697
1698 ty::TyParam(ref p) => Generic(token::get_name(p.name).to_string()),
1699
1700 ty::TyClosure(..) => Tuple(vec![]), // FIXME(pcwalton)
1701
1702 ty::TyInfer(..) => panic!("TyInfer"),
1703 ty::TyError => panic!("TyError"),
1704 }
1705 }
1706 }
1707
1708 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1709 pub enum StructField {
1710 HiddenStructField, // inserted later by strip passes
1711 TypedStructField(Type),
1712 }
1713
1714 impl Clean<Item> for ast::StructField {
1715 fn clean(&self, cx: &DocContext) -> Item {
1716 let (name, vis) = match self.node.kind {
1717 ast::NamedField(id, vis) => (Some(id), vis),
1718 ast::UnnamedField(vis) => (None, vis)
1719 };
1720 Item {
1721 name: name.clean(cx),
1722 attrs: self.node.attrs.clean(cx),
1723 source: self.span.clean(cx),
1724 visibility: Some(vis),
1725 stability: get_stability(cx, ast_util::local_def(self.node.id)),
1726 def_id: ast_util::local_def(self.node.id),
1727 inner: StructFieldItem(TypedStructField(self.node.ty.clean(cx))),
1728 }
1729 }
1730 }
1731
1732 impl Clean<Item> for ty::field_ty {
1733 fn clean(&self, cx: &DocContext) -> Item {
1734 use syntax::parse::token::special_idents::unnamed_field;
1735 use rustc::metadata::csearch;
1736
1737 let attr_map = csearch::get_struct_field_attrs(&cx.tcx().sess.cstore, self.id);
1738
1739 let (name, attrs) = if self.name == unnamed_field.name {
1740 (None, None)
1741 } else {
1742 (Some(self.name), Some(attr_map.get(&self.id.node).unwrap()))
1743 };
1744
1745 let ty = ty::lookup_item_type(cx.tcx(), self.id);
1746
1747 Item {
1748 name: name.clean(cx),
1749 attrs: attrs.unwrap_or(&Vec::new()).clean(cx),
1750 source: Span::empty(),
1751 visibility: Some(self.vis),
1752 stability: get_stability(cx, self.id),
1753 def_id: self.id,
1754 inner: StructFieldItem(TypedStructField(ty.ty.clean(cx))),
1755 }
1756 }
1757 }
1758
1759 pub type Visibility = ast::Visibility;
1760
1761 impl Clean<Option<Visibility>> for ast::Visibility {
1762 fn clean(&self, _: &DocContext) -> Option<Visibility> {
1763 Some(*self)
1764 }
1765 }
1766
1767 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1768 pub struct Struct {
1769 pub struct_type: doctree::StructType,
1770 pub generics: Generics,
1771 pub fields: Vec<Item>,
1772 pub fields_stripped: bool,
1773 }
1774
1775 impl Clean<Item> for doctree::Struct {
1776 fn clean(&self, cx: &DocContext) -> Item {
1777 Item {
1778 name: Some(self.name.clean(cx)),
1779 attrs: self.attrs.clean(cx),
1780 source: self.whence.clean(cx),
1781 def_id: ast_util::local_def(self.id),
1782 visibility: self.vis.clean(cx),
1783 stability: self.stab.clean(cx),
1784 inner: StructItem(Struct {
1785 struct_type: self.struct_type,
1786 generics: self.generics.clean(cx),
1787 fields: self.fields.clean(cx),
1788 fields_stripped: false,
1789 }),
1790 }
1791 }
1792 }
1793
1794 /// This is a more limited form of the standard Struct, different in that
1795 /// it lacks the things most items have (name, id, parameterization). Found
1796 /// only as a variant in an enum.
1797 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1798 pub struct VariantStruct {
1799 pub struct_type: doctree::StructType,
1800 pub fields: Vec<Item>,
1801 pub fields_stripped: bool,
1802 }
1803
1804 impl Clean<VariantStruct> for syntax::ast::StructDef {
1805 fn clean(&self, cx: &DocContext) -> VariantStruct {
1806 VariantStruct {
1807 struct_type: doctree::struct_type_from_def(self),
1808 fields: self.fields.clean(cx),
1809 fields_stripped: false,
1810 }
1811 }
1812 }
1813
1814 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1815 pub struct Enum {
1816 pub variants: Vec<Item>,
1817 pub generics: Generics,
1818 pub variants_stripped: bool,
1819 }
1820
1821 impl Clean<Item> for doctree::Enum {
1822 fn clean(&self, cx: &DocContext) -> Item {
1823 Item {
1824 name: Some(self.name.clean(cx)),
1825 attrs: self.attrs.clean(cx),
1826 source: self.whence.clean(cx),
1827 def_id: ast_util::local_def(self.id),
1828 visibility: self.vis.clean(cx),
1829 stability: self.stab.clean(cx),
1830 inner: EnumItem(Enum {
1831 variants: self.variants.clean(cx),
1832 generics: self.generics.clean(cx),
1833 variants_stripped: false,
1834 }),
1835 }
1836 }
1837 }
1838
1839 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1840 pub struct Variant {
1841 pub kind: VariantKind,
1842 }
1843
1844 impl Clean<Item> for doctree::Variant {
1845 fn clean(&self, cx: &DocContext) -> Item {
1846 Item {
1847 name: Some(self.name.clean(cx)),
1848 attrs: self.attrs.clean(cx),
1849 source: self.whence.clean(cx),
1850 visibility: self.vis.clean(cx),
1851 stability: self.stab.clean(cx),
1852 def_id: ast_util::local_def(self.id),
1853 inner: VariantItem(Variant {
1854 kind: self.kind.clean(cx),
1855 }),
1856 }
1857 }
1858 }
1859
1860 impl<'tcx> Clean<Item> for ty::VariantInfo<'tcx> {
1861 fn clean(&self, cx: &DocContext) -> Item {
1862 // use syntax::parse::token::special_idents::unnamed_field;
1863 let kind = match self.arg_names.as_ref().map(|s| &**s) {
1864 None | Some([]) if self.args.is_empty() => CLikeVariant,
1865 None | Some([]) => {
1866 TupleVariant(self.args.clean(cx))
1867 }
1868 Some(s) => {
1869 StructVariant(VariantStruct {
1870 struct_type: doctree::Plain,
1871 fields_stripped: false,
1872 fields: s.iter().zip(&self.args).map(|(name, ty)| {
1873 Item {
1874 source: Span::empty(),
1875 name: Some(name.clean(cx)),
1876 attrs: Vec::new(),
1877 visibility: Some(ast::Public),
1878 // FIXME: this is not accurate, we need an id for
1879 // the specific field but we're using the id
1880 // for the whole variant. Thus we read the
1881 // stability from the whole variant as well.
1882 // Struct variants are experimental and need
1883 // more infrastructure work before we can get
1884 // at the needed information here.
1885 def_id: self.id,
1886 stability: get_stability(cx, self.id),
1887 inner: StructFieldItem(
1888 TypedStructField(ty.clean(cx))
1889 )
1890 }
1891 }).collect()
1892 })
1893 }
1894 };
1895 Item {
1896 name: Some(self.name.clean(cx)),
1897 attrs: inline::load_attrs(cx, cx.tcx(), self.id),
1898 source: Span::empty(),
1899 visibility: Some(ast::Public),
1900 def_id: self.id,
1901 inner: VariantItem(Variant { kind: kind }),
1902 stability: get_stability(cx, self.id),
1903 }
1904 }
1905 }
1906
1907 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1908 pub enum VariantKind {
1909 CLikeVariant,
1910 TupleVariant(Vec<Type>),
1911 StructVariant(VariantStruct),
1912 }
1913
1914 impl Clean<VariantKind> for ast::VariantKind {
1915 fn clean(&self, cx: &DocContext) -> VariantKind {
1916 match self {
1917 &ast::TupleVariantKind(ref args) => {
1918 if args.is_empty() {
1919 CLikeVariant
1920 } else {
1921 TupleVariant(args.iter().map(|x| x.ty.clean(cx)).collect())
1922 }
1923 },
1924 &ast::StructVariantKind(ref sd) => StructVariant(sd.clean(cx)),
1925 }
1926 }
1927 }
1928
1929 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1930 pub struct Span {
1931 pub filename: String,
1932 pub loline: usize,
1933 pub locol: usize,
1934 pub hiline: usize,
1935 pub hicol: usize,
1936 }
1937
1938 impl Span {
1939 fn empty() -> Span {
1940 Span {
1941 filename: "".to_string(),
1942 loline: 0, locol: 0,
1943 hiline: 0, hicol: 0,
1944 }
1945 }
1946 }
1947
1948 impl Clean<Span> for syntax::codemap::Span {
1949 fn clean(&self, cx: &DocContext) -> Span {
1950 let cm = cx.sess().codemap();
1951 let filename = cm.span_to_filename(*self);
1952 let lo = cm.lookup_char_pos(self.lo);
1953 let hi = cm.lookup_char_pos(self.hi);
1954 Span {
1955 filename: filename.to_string(),
1956 loline: lo.line,
1957 locol: lo.col.to_usize(),
1958 hiline: hi.line,
1959 hicol: hi.col.to_usize(),
1960 }
1961 }
1962 }
1963
1964 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
1965 pub struct Path {
1966 pub global: bool,
1967 pub segments: Vec<PathSegment>,
1968 }
1969
1970 impl Path {
1971 pub fn singleton(name: String) -> Path {
1972 Path {
1973 global: false,
1974 segments: vec![PathSegment {
1975 name: name,
1976 params: PathParameters::AngleBracketed {
1977 lifetimes: Vec::new(),
1978 types: Vec::new(),
1979 bindings: Vec::new()
1980 }
1981 }]
1982 }
1983 }
1984 }
1985
1986 impl Clean<Path> for ast::Path {
1987 fn clean(&self, cx: &DocContext) -> Path {
1988 Path {
1989 global: self.global,
1990 segments: self.segments.clean(cx),
1991 }
1992 }
1993 }
1994
1995 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
1996 pub enum PathParameters {
1997 AngleBracketed {
1998 lifetimes: Vec<Lifetime>,
1999 types: Vec<Type>,
2000 bindings: Vec<TypeBinding>
2001 },
2002 Parenthesized {
2003 inputs: Vec<Type>,
2004 output: Option<Type>
2005 }
2006 }
2007
2008 impl Clean<PathParameters> for ast::PathParameters {
2009 fn clean(&self, cx: &DocContext) -> PathParameters {
2010 match *self {
2011 ast::AngleBracketedParameters(ref data) => {
2012 PathParameters::AngleBracketed {
2013 lifetimes: data.lifetimes.clean(cx),
2014 types: data.types.clean(cx),
2015 bindings: data.bindings.clean(cx)
2016 }
2017 }
2018
2019 ast::ParenthesizedParameters(ref data) => {
2020 PathParameters::Parenthesized {
2021 inputs: data.inputs.clean(cx),
2022 output: data.output.clean(cx)
2023 }
2024 }
2025 }
2026 }
2027 }
2028
2029 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
2030 pub struct PathSegment {
2031 pub name: String,
2032 pub params: PathParameters
2033 }
2034
2035 impl Clean<PathSegment> for ast::PathSegment {
2036 fn clean(&self, cx: &DocContext) -> PathSegment {
2037 PathSegment {
2038 name: self.identifier.clean(cx),
2039 params: self.parameters.clean(cx)
2040 }
2041 }
2042 }
2043
2044 fn path_to_string(p: &ast::Path) -> String {
2045 let mut s = String::new();
2046 let mut first = true;
2047 for i in p.segments.iter().map(|x| token::get_ident(x.identifier)) {
2048 if !first || p.global {
2049 s.push_str("::");
2050 } else {
2051 first = false;
2052 }
2053 s.push_str(&i);
2054 }
2055 s
2056 }
2057
2058 impl Clean<String> for ast::Ident {
2059 fn clean(&self, _: &DocContext) -> String {
2060 token::get_ident(*self).to_string()
2061 }
2062 }
2063
2064 impl Clean<String> for ast::Name {
2065 fn clean(&self, _: &DocContext) -> String {
2066 token::get_name(*self).to_string()
2067 }
2068 }
2069
2070 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2071 pub struct Typedef {
2072 pub type_: Type,
2073 pub generics: Generics,
2074 }
2075
2076 impl Clean<Item> for doctree::Typedef {
2077 fn clean(&self, cx: &DocContext) -> Item {
2078 Item {
2079 name: Some(self.name.clean(cx)),
2080 attrs: self.attrs.clean(cx),
2081 source: self.whence.clean(cx),
2082 def_id: ast_util::local_def(self.id.clone()),
2083 visibility: self.vis.clean(cx),
2084 stability: self.stab.clean(cx),
2085 inner: TypedefItem(Typedef {
2086 type_: self.ty.clean(cx),
2087 generics: self.gen.clean(cx),
2088 }, false),
2089 }
2090 }
2091 }
2092
2093 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
2094 pub struct BareFunctionDecl {
2095 pub unsafety: ast::Unsafety,
2096 pub generics: Generics,
2097 pub decl: FnDecl,
2098 pub abi: String,
2099 }
2100
2101 impl Clean<BareFunctionDecl> for ast::BareFnTy {
2102 fn clean(&self, cx: &DocContext) -> BareFunctionDecl {
2103 BareFunctionDecl {
2104 unsafety: self.unsafety,
2105 generics: Generics {
2106 lifetimes: self.lifetimes.clean(cx),
2107 type_params: Vec::new(),
2108 where_predicates: Vec::new()
2109 },
2110 decl: self.decl.clean(cx),
2111 abi: self.abi.to_string(),
2112 }
2113 }
2114 }
2115
2116 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2117 pub struct Static {
2118 pub type_: Type,
2119 pub mutability: Mutability,
2120 /// It's useful to have the value of a static documented, but I have no
2121 /// desire to represent expressions (that'd basically be all of the AST,
2122 /// which is huge!). So, have a string.
2123 pub expr: String,
2124 }
2125
2126 impl Clean<Item> for doctree::Static {
2127 fn clean(&self, cx: &DocContext) -> Item {
2128 debug!("cleaning static {}: {:?}", self.name.clean(cx), self);
2129 Item {
2130 name: Some(self.name.clean(cx)),
2131 attrs: self.attrs.clean(cx),
2132 source: self.whence.clean(cx),
2133 def_id: ast_util::local_def(self.id),
2134 visibility: self.vis.clean(cx),
2135 stability: self.stab.clean(cx),
2136 inner: StaticItem(Static {
2137 type_: self.type_.clean(cx),
2138 mutability: self.mutability.clean(cx),
2139 expr: self.expr.span.to_src(cx),
2140 }),
2141 }
2142 }
2143 }
2144
2145 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2146 pub struct Constant {
2147 pub type_: Type,
2148 pub expr: String,
2149 }
2150
2151 impl Clean<Item> for doctree::Constant {
2152 fn clean(&self, cx: &DocContext) -> Item {
2153 Item {
2154 name: Some(self.name.clean(cx)),
2155 attrs: self.attrs.clean(cx),
2156 source: self.whence.clean(cx),
2157 def_id: ast_util::local_def(self.id),
2158 visibility: self.vis.clean(cx),
2159 stability: self.stab.clean(cx),
2160 inner: ConstantItem(Constant {
2161 type_: self.type_.clean(cx),
2162 expr: self.expr.span.to_src(cx),
2163 }),
2164 }
2165 }
2166 }
2167
2168 #[derive(Debug, Clone, RustcEncodable, RustcDecodable, PartialEq, Copy)]
2169 pub enum Mutability {
2170 Mutable,
2171 Immutable,
2172 }
2173
2174 impl Clean<Mutability> for ast::Mutability {
2175 fn clean(&self, _: &DocContext) -> Mutability {
2176 match self {
2177 &ast::MutMutable => Mutable,
2178 &ast::MutImmutable => Immutable,
2179 }
2180 }
2181 }
2182
2183 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Copy, Debug)]
2184 pub enum ImplPolarity {
2185 Positive,
2186 Negative,
2187 }
2188
2189 impl Clean<ImplPolarity> for ast::ImplPolarity {
2190 fn clean(&self, _: &DocContext) -> ImplPolarity {
2191 match self {
2192 &ast::ImplPolarity::Positive => ImplPolarity::Positive,
2193 &ast::ImplPolarity::Negative => ImplPolarity::Negative,
2194 }
2195 }
2196 }
2197
2198 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2199 pub struct Impl {
2200 pub unsafety: ast::Unsafety,
2201 pub generics: Generics,
2202 pub trait_: Option<Type>,
2203 pub for_: Type,
2204 pub items: Vec<Item>,
2205 pub derived: bool,
2206 pub polarity: Option<ImplPolarity>,
2207 }
2208
2209 fn detect_derived<M: AttrMetaMethods>(attrs: &[M]) -> bool {
2210 attr::contains_name(attrs, "automatically_derived")
2211 }
2212
2213 impl Clean<Vec<Item>> for doctree::Impl {
2214 fn clean(&self, cx: &DocContext) -> Vec<Item> {
2215 let mut ret = Vec::new();
2216 let trait_ = self.trait_.clean(cx);
2217 let items = self.items.clean(cx);
2218
2219 // If this impl block is an implementation of the Deref trait, then we
2220 // need to try inlining the target's inherent impl blocks as well.
2221 if let Some(ResolvedPath { did, .. }) = trait_ {
2222 if Some(did) == cx.deref_trait_did.get() {
2223 build_deref_target_impls(cx, &items, &mut ret);
2224 }
2225 }
2226
2227 ret.push(Item {
2228 name: None,
2229 attrs: self.attrs.clean(cx),
2230 source: self.whence.clean(cx),
2231 def_id: ast_util::local_def(self.id),
2232 visibility: self.vis.clean(cx),
2233 stability: self.stab.clean(cx),
2234 inner: ImplItem(Impl {
2235 unsafety: self.unsafety,
2236 generics: self.generics.clean(cx),
2237 trait_: trait_,
2238 for_: self.for_.clean(cx),
2239 items: items,
2240 derived: detect_derived(&self.attrs),
2241 polarity: Some(self.polarity.clean(cx)),
2242 }),
2243 });
2244 return ret;
2245 }
2246 }
2247
2248 fn build_deref_target_impls(cx: &DocContext,
2249 items: &[Item],
2250 ret: &mut Vec<Item>) {
2251 let tcx = match cx.tcx_opt() {
2252 Some(t) => t,
2253 None => return,
2254 };
2255
2256 for item in items {
2257 let target = match item.inner {
2258 TypedefItem(ref t, true) => &t.type_,
2259 _ => continue,
2260 };
2261 let primitive = match *target {
2262 ResolvedPath { did, .. } if ast_util::is_local(did) => continue,
2263 ResolvedPath { did, .. } => {
2264 ret.extend(inline::build_impls(cx, tcx, did));
2265 continue
2266 }
2267 _ => match target.primitive_type() {
2268 Some(prim) => prim,
2269 None => continue,
2270 }
2271 };
2272 let did = match primitive {
2273 Isize => tcx.lang_items.isize_impl(),
2274 I8 => tcx.lang_items.i8_impl(),
2275 I16 => tcx.lang_items.i16_impl(),
2276 I32 => tcx.lang_items.i32_impl(),
2277 I64 => tcx.lang_items.i64_impl(),
2278 Usize => tcx.lang_items.usize_impl(),
2279 U8 => tcx.lang_items.u8_impl(),
2280 U16 => tcx.lang_items.u16_impl(),
2281 U32 => tcx.lang_items.u32_impl(),
2282 U64 => tcx.lang_items.u64_impl(),
2283 F32 => tcx.lang_items.f32_impl(),
2284 F64 => tcx.lang_items.f64_impl(),
2285 Char => tcx.lang_items.char_impl(),
2286 Bool => None,
2287 Str => tcx.lang_items.str_impl(),
2288 Slice => tcx.lang_items.slice_impl(),
2289 Array => tcx.lang_items.slice_impl(),
2290 PrimitiveTuple => None,
2291 PrimitiveRawPointer => tcx.lang_items.const_ptr_impl(),
2292 };
2293 if let Some(did) = did {
2294 if !ast_util::is_local(did) {
2295 inline::build_impl(cx, tcx, did, ret);
2296 }
2297 }
2298 }
2299 }
2300
2301 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2302 pub struct DefaultImpl {
2303 pub unsafety: ast::Unsafety,
2304 pub trait_: Type,
2305 }
2306
2307 impl Clean<Item> for doctree::DefaultImpl {
2308 fn clean(&self, cx: &DocContext) -> Item {
2309 Item {
2310 name: None,
2311 attrs: self.attrs.clean(cx),
2312 source: self.whence.clean(cx),
2313 def_id: ast_util::local_def(self.id),
2314 visibility: Some(ast::Public),
2315 stability: None,
2316 inner: DefaultImplItem(DefaultImpl {
2317 unsafety: self.unsafety,
2318 trait_: self.trait_.clean(cx),
2319 }),
2320 }
2321 }
2322 }
2323
2324 impl Clean<Item> for doctree::ExternCrate {
2325 fn clean(&self, cx: &DocContext) -> Item {
2326 Item {
2327 name: None,
2328 attrs: self.attrs.clean(cx),
2329 source: self.whence.clean(cx),
2330 def_id: ast_util::local_def(0),
2331 visibility: self.vis.clean(cx),
2332 stability: None,
2333 inner: ExternCrateItem(self.name.clean(cx), self.path.clone())
2334 }
2335 }
2336 }
2337
2338 impl Clean<Vec<Item>> for doctree::Import {
2339 fn clean(&self, cx: &DocContext) -> Vec<Item> {
2340 // We consider inlining the documentation of `pub use` statements, but we
2341 // forcefully don't inline if this is not public or if the
2342 // #[doc(no_inline)] attribute is present.
2343 let denied = self.vis != ast::Public || self.attrs.iter().any(|a| {
2344 &a.name()[..] == "doc" && match a.meta_item_list() {
2345 Some(l) => attr::contains_name(l, "no_inline"),
2346 None => false,
2347 }
2348 });
2349 let (mut ret, inner) = match self.node {
2350 ast::ViewPathGlob(ref p) => {
2351 (vec![], GlobImport(resolve_use_source(cx, p.clean(cx), self.id)))
2352 }
2353 ast::ViewPathList(ref p, ref list) => {
2354 // Attempt to inline all reexported items, but be sure
2355 // to keep any non-inlineable reexports so they can be
2356 // listed in the documentation.
2357 let mut ret = vec![];
2358 let remaining = if !denied {
2359 let mut remaining = vec![];
2360 for path in list {
2361 match inline::try_inline(cx, path.node.id(), None) {
2362 Some(items) => {
2363 ret.extend(items);
2364 }
2365 None => {
2366 remaining.push(path.clean(cx));
2367 }
2368 }
2369 }
2370 remaining
2371 } else {
2372 list.clean(cx)
2373 };
2374 if remaining.is_empty() {
2375 return ret;
2376 }
2377 (ret, ImportList(resolve_use_source(cx, p.clean(cx), self.id),
2378 remaining))
2379 }
2380 ast::ViewPathSimple(i, ref p) => {
2381 if !denied {
2382 match inline::try_inline(cx, self.id, Some(i)) {
2383 Some(items) => return items,
2384 None => {}
2385 }
2386 }
2387 (vec![], SimpleImport(i.clean(cx),
2388 resolve_use_source(cx, p.clean(cx), self.id)))
2389 }
2390 };
2391 ret.push(Item {
2392 name: None,
2393 attrs: self.attrs.clean(cx),
2394 source: self.whence.clean(cx),
2395 def_id: ast_util::local_def(0),
2396 visibility: self.vis.clean(cx),
2397 stability: None,
2398 inner: ImportItem(inner)
2399 });
2400 ret
2401 }
2402 }
2403
2404 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2405 pub enum Import {
2406 // use source as str;
2407 SimpleImport(String, ImportSource),
2408 // use source::*;
2409 GlobImport(ImportSource),
2410 // use source::{a, b, c};
2411 ImportList(ImportSource, Vec<ViewListIdent>),
2412 }
2413
2414 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2415 pub struct ImportSource {
2416 pub path: Path,
2417 pub did: Option<ast::DefId>,
2418 }
2419
2420 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2421 pub struct ViewListIdent {
2422 pub name: String,
2423 pub source: Option<ast::DefId>,
2424 }
2425
2426 impl Clean<ViewListIdent> for ast::PathListItem {
2427 fn clean(&self, cx: &DocContext) -> ViewListIdent {
2428 match self.node {
2429 ast::PathListIdent { id, name } => ViewListIdent {
2430 name: name.clean(cx),
2431 source: resolve_def(cx, id)
2432 },
2433 ast::PathListMod { id } => ViewListIdent {
2434 name: "self".to_string(),
2435 source: resolve_def(cx, id)
2436 }
2437 }
2438 }
2439 }
2440
2441 impl Clean<Vec<Item>> for ast::ForeignMod {
2442 fn clean(&self, cx: &DocContext) -> Vec<Item> {
2443 let mut items = self.items.clean(cx);
2444 for item in &mut items {
2445 match item.inner {
2446 ForeignFunctionItem(ref mut f) => f.abi = self.abi,
2447 _ => {}
2448 }
2449 }
2450 items
2451 }
2452 }
2453
2454 impl Clean<Item> for ast::ForeignItem {
2455 fn clean(&self, cx: &DocContext) -> Item {
2456 let inner = match self.node {
2457 ast::ForeignItemFn(ref decl, ref generics) => {
2458 ForeignFunctionItem(Function {
2459 decl: decl.clean(cx),
2460 generics: generics.clean(cx),
2461 unsafety: ast::Unsafety::Unsafe,
2462 abi: abi::Rust,
2463 constness: ast::Constness::NotConst,
2464 })
2465 }
2466 ast::ForeignItemStatic(ref ty, mutbl) => {
2467 ForeignStaticItem(Static {
2468 type_: ty.clean(cx),
2469 mutability: if mutbl {Mutable} else {Immutable},
2470 expr: "".to_string(),
2471 })
2472 }
2473 };
2474 Item {
2475 name: Some(self.ident.clean(cx)),
2476 attrs: self.attrs.clean(cx),
2477 source: self.span.clean(cx),
2478 def_id: ast_util::local_def(self.id),
2479 visibility: self.vis.clean(cx),
2480 stability: get_stability(cx, ast_util::local_def(self.id)),
2481 inner: inner,
2482 }
2483 }
2484 }
2485
2486 // Utilities
2487
2488 trait ToSource {
2489 fn to_src(&self, cx: &DocContext) -> String;
2490 }
2491
2492 impl ToSource for syntax::codemap::Span {
2493 fn to_src(&self, cx: &DocContext) -> String {
2494 debug!("converting span {:?} to snippet", self.clean(cx));
2495 let sn = match cx.sess().codemap().span_to_snippet(*self) {
2496 Ok(x) => x.to_string(),
2497 Err(_) => "".to_string()
2498 };
2499 debug!("got snippet {}", sn);
2500 sn
2501 }
2502 }
2503
2504 fn lit_to_string(lit: &ast::Lit) -> String {
2505 match lit.node {
2506 ast::LitStr(ref st, _) => st.to_string(),
2507 ast::LitBinary(ref data) => format!("{:?}", data),
2508 ast::LitByte(b) => {
2509 let mut res = String::from("b'");
2510 for c in (b as char).escape_default() {
2511 res.push(c);
2512 }
2513 res.push('\'');
2514 res
2515 },
2516 ast::LitChar(c) => format!("'{}'", c),
2517 ast::LitInt(i, _t) => i.to_string(),
2518 ast::LitFloat(ref f, _t) => f.to_string(),
2519 ast::LitFloatUnsuffixed(ref f) => f.to_string(),
2520 ast::LitBool(b) => b.to_string(),
2521 }
2522 }
2523
2524 fn name_from_pat(p: &ast::Pat) -> String {
2525 use syntax::ast::*;
2526 debug!("Trying to get a name from pattern: {:?}", p);
2527
2528 match p.node {
2529 PatWild(PatWildSingle) => "_".to_string(),
2530 PatWild(PatWildMulti) => "..".to_string(),
2531 PatIdent(_, ref p, _) => token::get_ident(p.node).to_string(),
2532 PatEnum(ref p, _) => path_to_string(p),
2533 PatQPath(..) => panic!("tried to get argument name from PatQPath, \
2534 which is not allowed in function arguments"),
2535 PatStruct(ref name, ref fields, etc) => {
2536 format!("{} {{ {}{} }}", path_to_string(name),
2537 fields.iter().map(|&Spanned { node: ref fp, .. }|
2538 format!("{}: {}", fp.ident.as_str(), name_from_pat(&*fp.pat)))
2539 .collect::<Vec<String>>().connect(", "),
2540 if etc { ", ..." } else { "" }
2541 )
2542 },
2543 PatTup(ref elts) => format!("({})", elts.iter().map(|p| name_from_pat(&**p))
2544 .collect::<Vec<String>>().connect(", ")),
2545 PatBox(ref p) => name_from_pat(&**p),
2546 PatRegion(ref p, _) => name_from_pat(&**p),
2547 PatLit(..) => {
2548 warn!("tried to get argument name from PatLit, \
2549 which is silly in function arguments");
2550 "()".to_string()
2551 },
2552 PatRange(..) => panic!("tried to get argument name from PatRange, \
2553 which is not allowed in function arguments"),
2554 PatVec(ref begin, ref mid, ref end) => {
2555 let begin = begin.iter().map(|p| name_from_pat(&**p));
2556 let mid = mid.as_ref().map(|p| format!("..{}", name_from_pat(&**p))).into_iter();
2557 let end = end.iter().map(|p| name_from_pat(&**p));
2558 format!("[{}]", begin.chain(mid).chain(end).collect::<Vec<_>>().connect(", "))
2559 },
2560 PatMac(..) => {
2561 warn!("can't document the name of a function argument \
2562 produced by a pattern macro");
2563 "(argument produced by macro)".to_string()
2564 }
2565 }
2566 }
2567
2568 /// Given a Type, resolve it using the def_map
2569 fn resolve_type(cx: &DocContext,
2570 path: Path,
2571 id: ast::NodeId) -> Type {
2572 let tcx = match cx.tcx_opt() {
2573 Some(tcx) => tcx,
2574 // If we're extracting tests, this return value doesn't matter.
2575 None => return Primitive(Bool),
2576 };
2577 debug!("searching for {} in defmap", id);
2578 let def = match tcx.def_map.borrow().get(&id) {
2579 Some(k) => k.full_def(),
2580 None => panic!("unresolved id not in defmap")
2581 };
2582
2583 let is_generic = match def {
2584 def::DefPrimTy(p) => match p {
2585 ast::TyStr => return Primitive(Str),
2586 ast::TyBool => return Primitive(Bool),
2587 ast::TyChar => return Primitive(Char),
2588 ast::TyInt(ast::TyIs) => return Primitive(Isize),
2589 ast::TyInt(ast::TyI8) => return Primitive(I8),
2590 ast::TyInt(ast::TyI16) => return Primitive(I16),
2591 ast::TyInt(ast::TyI32) => return Primitive(I32),
2592 ast::TyInt(ast::TyI64) => return Primitive(I64),
2593 ast::TyUint(ast::TyUs) => return Primitive(Usize),
2594 ast::TyUint(ast::TyU8) => return Primitive(U8),
2595 ast::TyUint(ast::TyU16) => return Primitive(U16),
2596 ast::TyUint(ast::TyU32) => return Primitive(U32),
2597 ast::TyUint(ast::TyU64) => return Primitive(U64),
2598 ast::TyFloat(ast::TyF32) => return Primitive(F32),
2599 ast::TyFloat(ast::TyF64) => return Primitive(F64),
2600 },
2601 def::DefSelfTy(..) if path.segments.len() == 1 => {
2602 return Generic(token::get_name(special_idents::type_self.name).to_string());
2603 }
2604 def::DefSelfTy(..) | def::DefTyParam(..) => true,
2605 _ => false,
2606 };
2607 let did = register_def(&*cx, def);
2608 ResolvedPath { path: path, typarams: None, did: did, is_generic: is_generic }
2609 }
2610
2611 fn register_def(cx: &DocContext, def: def::Def) -> ast::DefId {
2612 let (did, kind) = match def {
2613 def::DefFn(i, _) => (i, TypeFunction),
2614 def::DefTy(i, false) => (i, TypeTypedef),
2615 def::DefTy(i, true) => (i, TypeEnum),
2616 def::DefTrait(i) => (i, TypeTrait),
2617 def::DefStruct(i) => (i, TypeStruct),
2618 def::DefMod(i) => (i, TypeModule),
2619 def::DefStatic(i, _) => (i, TypeStatic),
2620 def::DefVariant(i, _, _) => (i, TypeEnum),
2621 _ => return def.def_id()
2622 };
2623 if ast_util::is_local(did) { return did }
2624 let tcx = match cx.tcx_opt() {
2625 Some(tcx) => tcx,
2626 None => return did
2627 };
2628 inline::record_extern_fqn(cx, did, kind);
2629 if let TypeTrait = kind {
2630 let t = inline::build_external_trait(cx, tcx, did);
2631 cx.external_traits.borrow_mut().as_mut().unwrap().insert(did, t);
2632 }
2633 return did;
2634 }
2635
2636 fn resolve_use_source(cx: &DocContext, path: Path, id: ast::NodeId) -> ImportSource {
2637 ImportSource {
2638 path: path,
2639 did: resolve_def(cx, id),
2640 }
2641 }
2642
2643 fn resolve_def(cx: &DocContext, id: ast::NodeId) -> Option<ast::DefId> {
2644 cx.tcx_opt().and_then(|tcx| {
2645 tcx.def_map.borrow().get(&id).map(|d| register_def(cx, d.full_def()))
2646 })
2647 }
2648
2649 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2650 pub struct Macro {
2651 pub source: String,
2652 pub imported_from: Option<String>,
2653 }
2654
2655 impl Clean<Item> for doctree::Macro {
2656 fn clean(&self, cx: &DocContext) -> Item {
2657 Item {
2658 name: Some(format!("{}!", self.name.clean(cx))),
2659 attrs: self.attrs.clean(cx),
2660 source: self.whence.clean(cx),
2661 visibility: ast::Public.clean(cx),
2662 stability: self.stab.clean(cx),
2663 def_id: ast_util::local_def(self.id),
2664 inner: MacroItem(Macro {
2665 source: self.whence.to_src(cx),
2666 imported_from: self.imported_from.clean(cx),
2667 }),
2668 }
2669 }
2670 }
2671
2672 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2673 pub struct Stability {
2674 pub level: attr::StabilityLevel,
2675 pub feature: String,
2676 pub since: String,
2677 pub deprecated_since: String,
2678 pub reason: String
2679 }
2680
2681 impl Clean<Stability> for attr::Stability {
2682 fn clean(&self, _: &DocContext) -> Stability {
2683 Stability {
2684 level: self.level,
2685 feature: self.feature.to_string(),
2686 since: self.since.as_ref().map_or("".to_string(),
2687 |interned| interned.to_string()),
2688 deprecated_since: self.deprecated_since.as_ref().map_or("".to_string(),
2689 |istr| istr.to_string()),
2690 reason: self.reason.as_ref().map_or("".to_string(),
2691 |interned| interned.to_string()),
2692 }
2693 }
2694 }
2695
2696 impl<'a> Clean<Stability> for &'a attr::Stability {
2697 fn clean(&self, _: &DocContext) -> Stability {
2698 Stability {
2699 level: self.level,
2700 feature: self.feature.to_string(),
2701 since: self.since.as_ref().map_or("".to_string(),
2702 |interned| interned.to_string()),
2703 deprecated_since: self.deprecated_since.as_ref().map_or("".to_string(),
2704 |istr| istr.to_string()),
2705 reason: self.reason.as_ref().map_or("".to_string(),
2706 |interned| interned.to_string()),
2707 }
2708 }
2709 }
2710
2711 impl<'tcx> Clean<Item> for ty::AssociatedConst<'tcx> {
2712 fn clean(&self, cx: &DocContext) -> Item {
2713 Item {
2714 source: DUMMY_SP.clean(cx),
2715 name: Some(self.name.clean(cx)),
2716 attrs: Vec::new(),
2717 inner: AssociatedConstItem(self.ty.clean(cx), None),
2718 visibility: None,
2719 def_id: self.def_id,
2720 stability: None,
2721 }
2722 }
2723 }
2724
2725 impl<'tcx> Clean<Item> for ty::AssociatedType<'tcx> {
2726 fn clean(&self, cx: &DocContext) -> Item {
2727 let my_name = self.name.clean(cx);
2728
2729 let mut bounds = if let ty::TraitContainer(did) = self.container {
2730 // When loading a cross-crate associated type, the bounds for this type
2731 // are actually located on the trait/impl itself, so we need to load
2732 // all of the generics from there and then look for bounds that are
2733 // applied to this associated type in question.
2734 let def = ty::lookup_trait_def(cx.tcx(), did);
2735 let predicates = ty::lookup_predicates(cx.tcx(), did);
2736 let generics = (&def.generics, &predicates, subst::TypeSpace).clean(cx);
2737 generics.where_predicates.iter().filter_map(|pred| {
2738 let (name, self_type, trait_, bounds) = match *pred {
2739 WherePredicate::BoundPredicate {
2740 ty: QPath { ref name, ref self_type, ref trait_ },
2741 ref bounds
2742 } => (name, self_type, trait_, bounds),
2743 _ => return None,
2744 };
2745 if *name != my_name { return None }
2746 match **trait_ {
2747 ResolvedPath { did, .. } if did == self.container.id() => {}
2748 _ => return None,
2749 }
2750 match **self_type {
2751 Generic(ref s) if *s == "Self" => {}
2752 _ => return None,
2753 }
2754 Some(bounds)
2755 }).flat_map(|i| i.iter().cloned()).collect::<Vec<_>>()
2756 } else {
2757 vec![]
2758 };
2759
2760 // Our Sized/?Sized bound didn't get handled when creating the generics
2761 // because we didn't actually get our whole set of bounds until just now
2762 // (some of them may have come from the trait). If we do have a sized
2763 // bound, we remove it, and if we don't then we add the `?Sized` bound
2764 // at the end.
2765 match bounds.iter().position(|b| b.is_sized_bound(cx)) {
2766 Some(i) => { bounds.remove(i); }
2767 None => bounds.push(TyParamBound::maybe_sized(cx)),
2768 }
2769
2770 Item {
2771 source: DUMMY_SP.clean(cx),
2772 name: Some(self.name.clean(cx)),
2773 attrs: inline::load_attrs(cx, cx.tcx(), self.def_id),
2774 inner: AssociatedTypeItem(bounds, self.ty.clean(cx)),
2775 visibility: self.vis.clean(cx),
2776 def_id: self.def_id,
2777 stability: stability::lookup(cx.tcx(), self.def_id).clean(cx),
2778 }
2779 }
2780 }
2781
2782 impl<'a> Clean<Typedef> for (ty::TypeScheme<'a>, ty::GenericPredicates<'a>,
2783 ParamSpace) {
2784 fn clean(&self, cx: &DocContext) -> Typedef {
2785 let (ref ty_scheme, ref predicates, ps) = *self;
2786 Typedef {
2787 type_: ty_scheme.ty.clean(cx),
2788 generics: (&ty_scheme.generics, predicates, ps).clean(cx)
2789 }
2790 }
2791 }
2792
2793 fn lang_struct(cx: &DocContext, did: Option<ast::DefId>,
2794 t: ty::Ty, name: &str,
2795 fallback: fn(Box<Type>) -> Type) -> Type {
2796 let did = match did {
2797 Some(did) => did,
2798 None => return fallback(box t.clean(cx)),
2799 };
2800 let fqn = csearch::get_item_path(cx.tcx(), did);
2801 let fqn: Vec<String> = fqn.into_iter().map(|i| {
2802 i.to_string()
2803 }).collect();
2804 cx.external_paths.borrow_mut().as_mut().unwrap().insert(did, (fqn, TypeStruct));
2805 ResolvedPath {
2806 typarams: None,
2807 did: did,
2808 path: Path {
2809 global: false,
2810 segments: vec![PathSegment {
2811 name: name.to_string(),
2812 params: PathParameters::AngleBracketed {
2813 lifetimes: vec![],
2814 types: vec![t.clean(cx)],
2815 bindings: vec![]
2816 }
2817 }],
2818 },
2819 is_generic: false,
2820 }
2821 }
2822
2823 /// An equality constraint on an associated type, e.g. `A=Bar` in `Foo<A=Bar>`
2824 #[derive(Clone, PartialEq, RustcDecodable, RustcEncodable, Debug)]
2825 pub struct TypeBinding {
2826 pub name: String,
2827 pub ty: Type
2828 }
2829
2830 impl Clean<TypeBinding> for ast::TypeBinding {
2831 fn clean(&self, cx: &DocContext) -> TypeBinding {
2832 TypeBinding {
2833 name: self.ident.clean(cx),
2834 ty: self.ty.clean(cx)
2835 }
2836 }
2837 }