]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/clean/mod.rs
rustdoc: incorporate stability index throughout
[rust.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 use syntax;
15 use syntax::ast;
16 use syntax::ast_util;
17 use syntax::attr;
18 use syntax::attr::{AttributeMethods, AttrMetaMethods};
19 use syntax::codemap::Pos;
20 use syntax::parse::token::InternedString;
21 use syntax::parse::token;
22
23 use rustc::back::link;
24 use rustc::driver::driver;
25 use rustc::metadata::cstore;
26 use rustc::metadata::csearch;
27 use rustc::metadata::decoder;
28 use rustc::middle::def;
29 use rustc::middle::subst;
30 use rustc::middle::subst::VecPerParamSpace;
31 use rustc::middle::ty;
32 use rustc::middle::stability;
33
34 use std::rc::Rc;
35 use std::u32;
36 use std::gc::{Gc, GC};
37
38 use core;
39 use doctree;
40 use visit_ast;
41
42 /// A stable identifier to the particular version of JSON output.
43 /// Increment this when the `Crate` and related structures change.
44 pub static SCHEMA_VERSION: &'static str = "0.8.3";
45
46 mod inline;
47
48 // load the current DocContext from TLD
49 fn get_cx() -> Gc<core::DocContext> {
50     *super::ctxtkey.get().unwrap()
51 }
52
53 // extract the stability index for a node from TLD, if possible
54 fn get_stability(def_id: ast::DefId) -> Option<Stability> {
55     get_cx().tcx_opt().and_then(|tcx| stability::lookup(tcx, def_id))
56             .map(|stab| stab.clean())
57 }
58
59 pub trait Clean<T> {
60     fn clean(&self) -> T;
61 }
62
63 impl<T: Clean<U>, U> Clean<Vec<U>> for Vec<T> {
64     fn clean(&self) -> Vec<U> {
65         self.iter().map(|x| x.clean()).collect()
66     }
67 }
68
69 impl<T: Clean<U>, U> Clean<VecPerParamSpace<U>> for VecPerParamSpace<T> {
70     fn clean(&self) -> VecPerParamSpace<U> {
71         self.map(|x| x.clean())
72     }
73 }
74
75 impl<T: 'static + Clean<U>, U> Clean<U> for Gc<T> {
76     fn clean(&self) -> U {
77         (**self).clean()
78     }
79 }
80
81 impl<T: Clean<U>, U> Clean<U> for Rc<T> {
82     fn clean(&self) -> U {
83         (**self).clean()
84     }
85 }
86
87 impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
88     fn clean(&self) -> Option<U> {
89         match self {
90             &None => None,
91             &Some(ref v) => Some(v.clean())
92         }
93     }
94 }
95
96 impl<T: Clean<U>, U> Clean<Vec<U>> for syntax::owned_slice::OwnedSlice<T> {
97     fn clean(&self) -> Vec<U> {
98         self.iter().map(|x| x.clean()).collect()
99     }
100 }
101
102 #[deriving(Clone, Encodable, Decodable)]
103 pub struct Crate {
104     pub name: String,
105     pub module: Option<Item>,
106     pub externs: Vec<(ast::CrateNum, ExternalCrate)>,
107     pub primitives: Vec<Primitive>,
108 }
109
110 impl<'a> Clean<Crate> for visit_ast::RustdocVisitor<'a> {
111     fn clean(&self) -> Crate {
112         let cx = get_cx();
113
114         let mut externs = Vec::new();
115         cx.sess().cstore.iter_crate_data(|n, meta| {
116             externs.push((n, meta.clean()));
117         });
118         externs.sort_by(|&(a, _), &(b, _)| a.cmp(&b));
119
120         // Figure out the name of this crate
121         let input = driver::FileInput(cx.src.clone());
122         let t_outputs = driver::build_output_filenames(&input,
123                                                        &None,
124                                                        &None,
125                                                        self.attrs.as_slice(),
126                                                        cx.sess());
127         let id = link::find_crate_id(self.attrs.as_slice(),
128                                      t_outputs.out_filestem.as_slice());
129
130         // Clean the crate, translating the entire libsyntax AST to one that is
131         // understood by rustdoc.
132         let mut module = self.module.clean();
133
134         // Collect all inner modules which are tagged as implementations of
135         // primitives.
136         //
137         // Note that this loop only searches the top-level items of the crate,
138         // and this is intentional. If we were to search the entire crate for an
139         // item tagged with `#[doc(primitive)]` then we we would also have to
140         // search the entirety of external modules for items tagged
141         // `#[doc(primitive)]`, which is a pretty inefficient process (decoding
142         // all that metadata unconditionally).
143         //
144         // In order to keep the metadata load under control, the
145         // `#[doc(primitive)]` feature is explicitly designed to only allow the
146         // primitive tags to show up as the top level items in a crate.
147         //
148         // Also note that this does not attempt to deal with modules tagged
149         // duplicately for the same primitive. This is handled later on when
150         // rendering by delegating everything to a hash map.
151         let mut primitives = Vec::new();
152         {
153             let m = match module.inner {
154                 ModuleItem(ref mut m) => m,
155                 _ => unreachable!(),
156             };
157             let mut tmp = Vec::new();
158             for child in m.items.mut_iter() {
159                 let inner = match child.inner {
160                     ModuleItem(ref mut m) => m,
161                     _ => continue,
162                 };
163                 let prim = match Primitive::find(child.attrs.as_slice()) {
164                     Some(prim) => prim,
165                     None => continue,
166                 };
167                 primitives.push(prim);
168                 let mut i = Item {
169                     source: Span::empty(),
170                     name: Some(prim.to_url_str().to_string()),
171                     attrs: Vec::new(),
172                     visibility: None,
173                     stability: None,
174                     def_id: ast_util::local_def(prim.to_node_id()),
175                     inner: PrimitiveItem(prim),
176                 };
177                 // Push one copy to get indexed for the whole crate, and push a
178                 // another copy in the proper location which will actually get
179                 // documented. The first copy will also serve as a redirect to
180                 // the other copy.
181                 tmp.push(i.clone());
182                 i.visibility = Some(ast::Public);
183                 i.attrs = child.attrs.clone();
184                 inner.items.push(i);
185
186             }
187             m.items.extend(tmp.move_iter());
188         }
189
190         Crate {
191             name: id.name.to_string(),
192             module: Some(module),
193             externs: externs,
194             primitives: primitives,
195         }
196     }
197 }
198
199 #[deriving(Clone, Encodable, Decodable)]
200 pub struct ExternalCrate {
201     pub name: String,
202     pub attrs: Vec<Attribute>,
203     pub primitives: Vec<Primitive>,
204 }
205
206 impl Clean<ExternalCrate> for cstore::crate_metadata {
207     fn clean(&self) -> ExternalCrate {
208         let mut primitives = Vec::new();
209         get_cx().tcx_opt().map(|tcx| {
210             csearch::each_top_level_item_of_crate(&tcx.sess.cstore,
211                                                   self.cnum,
212                                                   |def, _, _| {
213                 let did = match def {
214                     decoder::DlDef(def::DefMod(did)) => did,
215                     _ => return
216                 };
217                 let attrs = inline::load_attrs(tcx, did);
218                 Primitive::find(attrs.as_slice()).map(|prim| primitives.push(prim));
219             })
220         });
221         ExternalCrate {
222             name: self.name.to_string(),
223             attrs: decoder::get_crate_attributes(self.data()).clean(),
224             primitives: primitives,
225         }
226     }
227 }
228
229 /// Anything with a source location and set of attributes and, optionally, a
230 /// name. That is, anything that can be documented. This doesn't correspond
231 /// directly to the AST's concept of an item; it's a strict superset.
232 #[deriving(Clone, Encodable, Decodable)]
233 pub struct Item {
234     /// Stringified span
235     pub source: Span,
236     /// Not everything has a name. E.g., impls
237     pub name: Option<String>,
238     pub attrs: Vec<Attribute> ,
239     pub inner: ItemEnum,
240     pub visibility: Option<Visibility>,
241     pub def_id: ast::DefId,
242     pub stability: Option<Stability>,
243 }
244
245 impl Item {
246     /// Finds the `doc` attribute as a List and returns the list of attributes
247     /// nested inside.
248     pub fn doc_list<'a>(&'a self) -> Option<&'a [Attribute]> {
249         for attr in self.attrs.iter() {
250             match *attr {
251                 List(ref x, ref list) if "doc" == x.as_slice() => {
252                     return Some(list.as_slice());
253                 }
254                 _ => {}
255             }
256         }
257         return None;
258     }
259
260     /// Finds the `doc` attribute as a NameValue and returns the corresponding
261     /// value found.
262     pub fn doc_value<'a>(&'a self) -> Option<&'a str> {
263         for attr in self.attrs.iter() {
264             match *attr {
265                 NameValue(ref x, ref v) if "doc" == x.as_slice() => {
266                     return Some(v.as_slice());
267                 }
268                 _ => {}
269             }
270         }
271         return None;
272     }
273
274     pub fn is_hidden_from_doc(&self) -> bool {
275         match self.doc_list() {
276             Some(ref l) => {
277                 for innerattr in l.iter() {
278                     match *innerattr {
279                         Word(ref s) if "hidden" == s.as_slice() => {
280                             return true
281                         }
282                         _ => (),
283                     }
284                 }
285             },
286             None => ()
287         }
288         return false;
289     }
290
291     pub fn is_mod(&self) -> bool {
292         match self.inner { ModuleItem(..) => true, _ => false }
293     }
294     pub fn is_trait(&self) -> bool {
295         match self.inner { TraitItem(..) => true, _ => false }
296     }
297     pub fn is_struct(&self) -> bool {
298         match self.inner { StructItem(..) => true, _ => false }
299     }
300     pub fn is_enum(&self) -> bool {
301         match self.inner { EnumItem(..) => true, _ => false }
302     }
303     pub fn is_fn(&self) -> bool {
304         match self.inner { FunctionItem(..) => true, _ => false }
305     }
306 }
307
308 #[deriving(Clone, Encodable, Decodable)]
309 pub enum ItemEnum {
310     StructItem(Struct),
311     EnumItem(Enum),
312     FunctionItem(Function),
313     ModuleItem(Module),
314     TypedefItem(Typedef),
315     StaticItem(Static),
316     TraitItem(Trait),
317     ImplItem(Impl),
318     /// `use` and `extern crate`
319     ViewItemItem(ViewItem),
320     /// A method signature only. Used for required methods in traits (ie,
321     /// non-default-methods).
322     TyMethodItem(TyMethod),
323     /// A method with a body.
324     MethodItem(Method),
325     StructFieldItem(StructField),
326     VariantItem(Variant),
327     /// `fn`s from an extern block
328     ForeignFunctionItem(Function),
329     /// `static`s from an extern block
330     ForeignStaticItem(Static),
331     MacroItem(Macro),
332     PrimitiveItem(Primitive),
333 }
334
335 #[deriving(Clone, Encodable, Decodable)]
336 pub struct Module {
337     pub items: Vec<Item>,
338     pub is_crate: bool,
339 }
340
341 impl Clean<Item> for doctree::Module {
342     fn clean(&self) -> Item {
343         let name = if self.name.is_some() {
344             self.name.unwrap().clean()
345         } else {
346             "".to_string()
347         };
348         let mut foreigns = Vec::new();
349         for subforeigns in self.foreigns.clean().move_iter() {
350             for foreign in subforeigns.move_iter() {
351                 foreigns.push(foreign)
352             }
353         }
354         let items: Vec<Vec<Item> > = vec!(
355             self.structs.clean().move_iter().collect(),
356             self.enums.clean().move_iter().collect(),
357             self.fns.clean().move_iter().collect(),
358             foreigns,
359             self.mods.clean().move_iter().collect(),
360             self.typedefs.clean().move_iter().collect(),
361             self.statics.clean().move_iter().collect(),
362             self.traits.clean().move_iter().collect(),
363             self.impls.clean().move_iter().collect(),
364             self.view_items.clean().move_iter()
365                            .flat_map(|s| s.move_iter()).collect(),
366             self.macros.clean().move_iter().collect()
367         );
368
369         // determine if we should display the inner contents or
370         // the outer `mod` item for the source code.
371         let where = {
372             let ctxt = super::ctxtkey.get().unwrap();
373             let cm = ctxt.sess().codemap();
374             let outer = cm.lookup_char_pos(self.where_outer.lo);
375             let inner = cm.lookup_char_pos(self.where_inner.lo);
376             if outer.file.start_pos == inner.file.start_pos {
377                 // mod foo { ... }
378                 self.where_outer
379             } else {
380                 // mod foo; (and a separate FileMap for the contents)
381                 self.where_inner
382             }
383         };
384
385         Item {
386             name: Some(name),
387             attrs: self.attrs.clean(),
388             source: where.clean(),
389             visibility: self.vis.clean(),
390             stability: self.stab.clean(),
391             def_id: ast_util::local_def(self.id),
392             inner: ModuleItem(Module {
393                is_crate: self.is_crate,
394                items: items.iter()
395                            .flat_map(|x| x.iter().map(|x| (*x).clone()))
396                            .collect(),
397             })
398         }
399     }
400 }
401
402 #[deriving(Clone, Encodable, Decodable)]
403 pub enum Attribute {
404     Word(String),
405     List(String, Vec<Attribute> ),
406     NameValue(String, String)
407 }
408
409 impl Clean<Attribute> for ast::MetaItem {
410     fn clean(&self) -> Attribute {
411         match self.node {
412             ast::MetaWord(ref s) => Word(s.get().to_string()),
413             ast::MetaList(ref s, ref l) => {
414                 List(s.get().to_string(), l.clean().move_iter().collect())
415             }
416             ast::MetaNameValue(ref s, ref v) => {
417                 NameValue(s.get().to_string(), lit_to_str(v))
418             }
419         }
420     }
421 }
422
423 impl Clean<Attribute> for ast::Attribute {
424     fn clean(&self) -> Attribute {
425         self.desugar_doc().node.value.clean()
426     }
427 }
428
429 // This is a rough approximation that gets us what we want.
430 impl attr::AttrMetaMethods for Attribute {
431     fn name(&self) -> InternedString {
432         match *self {
433             Word(ref n) | List(ref n, _) | NameValue(ref n, _) => {
434                 token::intern_and_get_ident(n.as_slice())
435             }
436         }
437     }
438
439     fn value_str(&self) -> Option<InternedString> {
440         match *self {
441             NameValue(_, ref v) => {
442                 Some(token::intern_and_get_ident(v.as_slice()))
443             }
444             _ => None,
445         }
446     }
447     fn meta_item_list<'a>(&'a self) -> Option<&'a [Gc<ast::MetaItem>]> { None }
448 }
449 impl<'a> attr::AttrMetaMethods for &'a Attribute {
450     fn name(&self) -> InternedString { (**self).name() }
451     fn value_str(&self) -> Option<InternedString> { (**self).value_str() }
452     fn meta_item_list<'a>(&'a self) -> Option<&'a [Gc<ast::MetaItem>]> { None }
453 }
454
455 #[deriving(Clone, Encodable, Decodable)]
456 pub struct TyParam {
457     pub name: String,
458     pub did: ast::DefId,
459     pub bounds: Vec<TyParamBound>,
460     pub default: Option<Type>
461 }
462
463 impl Clean<TyParam> for ast::TyParam {
464     fn clean(&self) -> TyParam {
465         TyParam {
466             name: self.ident.clean(),
467             did: ast::DefId { krate: ast::LOCAL_CRATE, node: self.id },
468             bounds: self.bounds.clean().move_iter().collect(),
469             default: self.default.clean()
470         }
471     }
472 }
473
474 impl Clean<TyParam> for ty::TypeParameterDef {
475     fn clean(&self) -> TyParam {
476         get_cx().external_typarams.borrow_mut().get_mut_ref()
477                 .insert(self.def_id, self.ident.clean());
478         TyParam {
479             name: self.ident.clean(),
480             did: self.def_id,
481             bounds: self.bounds.clean(),
482             default: self.default.clean()
483         }
484     }
485 }
486
487 #[deriving(Clone, Encodable, Decodable)]
488 pub enum TyParamBound {
489     RegionBound,
490     TraitBound(Type)
491 }
492
493 impl Clean<TyParamBound> for ast::TyParamBound {
494     fn clean(&self) -> TyParamBound {
495         match *self {
496             ast::StaticRegionTyParamBound => RegionBound,
497             ast::OtherRegionTyParamBound(_) => RegionBound,
498             ast::UnboxedFnTyParamBound(_) => {
499                 // FIXME(pcwalton): Wrong.
500                 RegionBound
501             }
502             ast::TraitTyParamBound(ref t) => TraitBound(t.clean()),
503         }
504     }
505 }
506
507 fn external_path(name: &str, substs: &subst::Substs) -> Path {
508     let lifetimes = substs.regions().get_vec(subst::TypeSpace)
509                     .iter()
510                     .filter_map(|v| v.clean())
511                     .collect();
512     let types = substs.types.get_vec(subst::TypeSpace).clean();
513     Path {
514         global: false,
515         segments: vec![PathSegment {
516             name: name.to_string(),
517             lifetimes: lifetimes,
518             types: types,
519         }],
520     }
521 }
522
523 impl Clean<TyParamBound> for ty::BuiltinBound {
524     fn clean(&self) -> TyParamBound {
525         let cx = get_cx();
526         let tcx = match cx.maybe_typed {
527             core::Typed(ref tcx) => tcx,
528             core::NotTyped(_) => return RegionBound,
529         };
530         let empty = subst::Substs::empty();
531         let (did, path) = match *self {
532             ty::BoundStatic => return RegionBound,
533             ty::BoundSend =>
534                 (tcx.lang_items.send_trait().unwrap(),
535                  external_path("Send", &empty)),
536             ty::BoundSized =>
537                 (tcx.lang_items.sized_trait().unwrap(),
538                  external_path("Sized", &empty)),
539             ty::BoundCopy =>
540                 (tcx.lang_items.copy_trait().unwrap(),
541                  external_path("Copy", &empty)),
542             ty::BoundShare =>
543                 (tcx.lang_items.share_trait().unwrap(),
544                  external_path("Share", &empty)),
545         };
546         let fqn = csearch::get_item_path(tcx, did);
547         let fqn = fqn.move_iter().map(|i| i.to_str()).collect();
548         cx.external_paths.borrow_mut().get_mut_ref().insert(did,
549                                                             (fqn, TypeTrait));
550         TraitBound(ResolvedPath {
551             path: path,
552             typarams: None,
553             did: did,
554         })
555     }
556 }
557
558 impl Clean<TyParamBound> for ty::TraitRef {
559     fn clean(&self) -> TyParamBound {
560         let cx = get_cx();
561         let tcx = match cx.maybe_typed {
562             core::Typed(ref tcx) => tcx,
563             core::NotTyped(_) => return RegionBound,
564         };
565         let fqn = csearch::get_item_path(tcx, self.def_id);
566         let fqn = fqn.move_iter().map(|i| i.to_str())
567                      .collect::<Vec<String>>();
568         let path = external_path(fqn.last().unwrap().as_slice(),
569                                  &self.substs);
570         cx.external_paths.borrow_mut().get_mut_ref().insert(self.def_id,
571                                                             (fqn, TypeTrait));
572         TraitBound(ResolvedPath {
573             path: path,
574             typarams: None,
575             did: self.def_id,
576         })
577     }
578 }
579
580 impl Clean<Vec<TyParamBound>> for ty::ParamBounds {
581     fn clean(&self) -> Vec<TyParamBound> {
582         let mut v = Vec::new();
583         for b in self.builtin_bounds.iter() {
584             if b != ty::BoundSized {
585                 v.push(b.clean());
586             }
587         }
588         for t in self.trait_bounds.iter() {
589             v.push(t.clean());
590         }
591         return v;
592     }
593 }
594
595 impl Clean<Option<Vec<TyParamBound>>> for subst::Substs {
596     fn clean(&self) -> Option<Vec<TyParamBound>> {
597         let mut v = Vec::new();
598         v.extend(self.regions().iter().map(|_| RegionBound));
599         v.extend(self.types.iter().map(|t| TraitBound(t.clean())));
600         if v.len() > 0 {Some(v)} else {None}
601     }
602 }
603
604 #[deriving(Clone, Encodable, Decodable, PartialEq)]
605 pub struct Lifetime(String);
606
607 impl Lifetime {
608     pub fn get_ref<'a>(&'a self) -> &'a str {
609         let Lifetime(ref s) = *self;
610         let s: &'a str = s.as_slice();
611         return s;
612     }
613 }
614
615 impl Clean<Lifetime> for ast::Lifetime {
616     fn clean(&self) -> Lifetime {
617         Lifetime(token::get_name(self.name).get().to_string())
618     }
619 }
620
621 impl Clean<Lifetime> for ty::RegionParameterDef {
622     fn clean(&self) -> Lifetime {
623         Lifetime(token::get_name(self.name).get().to_string())
624     }
625 }
626
627 impl Clean<Option<Lifetime>> for ty::Region {
628     fn clean(&self) -> Option<Lifetime> {
629         match *self {
630             ty::ReStatic => Some(Lifetime("'static".to_string())),
631             ty::ReLateBound(_, ty::BrNamed(_, name)) =>
632                 Some(Lifetime(token::get_name(name).get().to_string())),
633             ty::ReEarlyBound(_, _, _, name) => Some(Lifetime(name.clean())),
634
635             ty::ReLateBound(..) |
636             ty::ReFree(..) |
637             ty::ReScope(..) |
638             ty::ReInfer(..) |
639             ty::ReEmpty(..) => None
640         }
641     }
642 }
643
644 // maybe use a Generic enum and use ~[Generic]?
645 #[deriving(Clone, Encodable, Decodable)]
646 pub struct Generics {
647     pub lifetimes: Vec<Lifetime>,
648     pub type_params: Vec<TyParam>,
649 }
650
651 impl Clean<Generics> for ast::Generics {
652     fn clean(&self) -> Generics {
653         Generics {
654             lifetimes: self.lifetimes.clean(),
655             type_params: self.ty_params.clean(),
656         }
657     }
658 }
659
660 impl Clean<Generics> for ty::Generics {
661     fn clean(&self) -> Generics {
662         // In the type space, generics can come in one of multiple
663         // namespaces.  This means that e.g. for fn items the type
664         // parameters will live in FnSpace, but for types the
665         // parameters will live in TypeSpace (trait definitions also
666         // define a parameter in SelfSpace). *Method* definitions are
667         // the one exception: they combine the TypeSpace parameters
668         // from the enclosing impl/trait with their own FnSpace
669         // parameters.
670         //
671         // In general, when we clean, we are trying to produce the
672         // "user-facing" generics. Hence we select the most specific
673         // namespace that is occupied, ignoring SelfSpace because it
674         // is implicit.
675
676         let space = {
677             if !self.types.get_vec(subst::FnSpace).is_empty() ||
678                 !self.regions.get_vec(subst::FnSpace).is_empty()
679             {
680                 subst::FnSpace
681             } else {
682                 subst::TypeSpace
683             }
684         };
685
686         Generics {
687             type_params: self.types.get_vec(space).clean(),
688             lifetimes: self.regions.get_vec(space).clean(),
689         }
690     }
691 }
692
693 #[deriving(Clone, Encodable, Decodable)]
694 pub struct Method {
695     pub generics: Generics,
696     pub self_: SelfTy,
697     pub fn_style: ast::FnStyle,
698     pub decl: FnDecl,
699 }
700
701 impl Clean<Item> for ast::Method {
702     fn clean(&self) -> Item {
703         let inputs = match self.explicit_self.node {
704             ast::SelfStatic => self.decl.inputs.as_slice(),
705             _ => self.decl.inputs.slice_from(1)
706         };
707         let decl = FnDecl {
708             inputs: Arguments {
709                 values: inputs.iter().map(|x| x.clean()).collect(),
710             },
711             output: (self.decl.output.clean()),
712             cf: self.decl.cf.clean(),
713             attrs: Vec::new()
714         };
715         Item {
716             name: Some(self.ident.clean()),
717             attrs: self.attrs.clean().move_iter().collect(),
718             source: self.span.clean(),
719             def_id: ast_util::local_def(self.id),
720             visibility: self.vis.clean(),
721             stability: get_stability(ast_util::local_def(self.id)),
722             inner: MethodItem(Method {
723                 generics: self.generics.clean(),
724                 self_: self.explicit_self.node.clean(),
725                 fn_style: self.fn_style.clone(),
726                 decl: decl,
727             }),
728         }
729     }
730 }
731
732 #[deriving(Clone, Encodable, Decodable)]
733 pub struct TyMethod {
734     pub fn_style: ast::FnStyle,
735     pub decl: FnDecl,
736     pub generics: Generics,
737     pub self_: SelfTy,
738 }
739
740 impl Clean<Item> for ast::TypeMethod {
741     fn clean(&self) -> Item {
742         let inputs = match self.explicit_self.node {
743             ast::SelfStatic => self.decl.inputs.as_slice(),
744             _ => self.decl.inputs.slice_from(1)
745         };
746         let decl = FnDecl {
747             inputs: Arguments {
748                 values: inputs.iter().map(|x| x.clean()).collect(),
749             },
750             output: (self.decl.output.clean()),
751             cf: self.decl.cf.clean(),
752             attrs: Vec::new()
753         };
754         Item {
755             name: Some(self.ident.clean()),
756             attrs: self.attrs.clean().move_iter().collect(),
757             source: self.span.clean(),
758             def_id: ast_util::local_def(self.id),
759             visibility: None,
760             stability: get_stability(ast_util::local_def(self.id)),
761             inner: TyMethodItem(TyMethod {
762                 fn_style: self.fn_style.clone(),
763                 decl: decl,
764                 self_: self.explicit_self.node.clean(),
765                 generics: self.generics.clean(),
766             }),
767         }
768     }
769 }
770
771 #[deriving(Clone, Encodable, Decodable, PartialEq)]
772 pub enum SelfTy {
773     SelfStatic,
774     SelfValue,
775     SelfBorrowed(Option<Lifetime>, Mutability),
776     SelfOwned,
777 }
778
779 impl Clean<SelfTy> for ast::ExplicitSelf_ {
780     fn clean(&self) -> SelfTy {
781         match *self {
782             ast::SelfStatic => SelfStatic,
783             ast::SelfValue => SelfValue,
784             ast::SelfUniq => SelfOwned,
785             ast::SelfRegion(lt, mt) => SelfBorrowed(lt.clean(), mt.clean()),
786         }
787     }
788 }
789
790 #[deriving(Clone, Encodable, Decodable)]
791 pub struct Function {
792     pub decl: FnDecl,
793     pub generics: Generics,
794     pub fn_style: ast::FnStyle,
795 }
796
797 impl Clean<Item> for doctree::Function {
798     fn clean(&self) -> Item {
799         Item {
800             name: Some(self.name.clean()),
801             attrs: self.attrs.clean(),
802             source: self.where.clean(),
803             visibility: self.vis.clean(),
804             stability: self.stab.clean(),
805             def_id: ast_util::local_def(self.id),
806             inner: FunctionItem(Function {
807                 decl: self.decl.clean(),
808                 generics: self.generics.clean(),
809                 fn_style: self.fn_style,
810             }),
811         }
812     }
813 }
814
815 #[deriving(Clone, Encodable, Decodable)]
816 pub struct ClosureDecl {
817     pub lifetimes: Vec<Lifetime>,
818     pub decl: FnDecl,
819     pub onceness: ast::Onceness,
820     pub fn_style: ast::FnStyle,
821     pub bounds: Vec<TyParamBound>,
822 }
823
824 impl Clean<ClosureDecl> for ast::ClosureTy {
825     fn clean(&self) -> ClosureDecl {
826         ClosureDecl {
827             lifetimes: self.lifetimes.clean(),
828             decl: self.decl.clean(),
829             onceness: self.onceness,
830             fn_style: self.fn_style,
831             bounds: match self.bounds {
832                 Some(ref x) => x.clean().move_iter().collect(),
833                 None        => Vec::new()
834             },
835         }
836     }
837 }
838
839 #[deriving(Clone, Encodable, Decodable)]
840 pub struct FnDecl {
841     pub inputs: Arguments,
842     pub output: Type,
843     pub cf: RetStyle,
844     pub attrs: Vec<Attribute>,
845 }
846
847 #[deriving(Clone, Encodable, Decodable)]
848 pub struct Arguments {
849     pub values: Vec<Argument>,
850 }
851
852 impl Clean<FnDecl> for ast::FnDecl {
853     fn clean(&self) -> FnDecl {
854         FnDecl {
855             inputs: Arguments {
856                 values: self.inputs.iter().map(|x| x.clean()).collect(),
857             },
858             output: (self.output.clean()),
859             cf: self.cf.clean(),
860             attrs: Vec::new()
861         }
862     }
863 }
864
865 impl<'a> Clean<FnDecl> for (ast::DefId, &'a ty::FnSig) {
866     fn clean(&self) -> FnDecl {
867         let cx = get_cx();
868         let (did, sig) = *self;
869         let mut names = if did.node != 0 {
870             csearch::get_method_arg_names(&cx.tcx().sess.cstore, did).move_iter()
871         } else {
872             Vec::new().move_iter()
873         }.peekable();
874         if names.peek().map(|s| s.as_slice()) == Some("self") {
875             let _ = names.next();
876         }
877         FnDecl {
878             output: sig.output.clean(),
879             cf: Return,
880             attrs: Vec::new(),
881             inputs: Arguments {
882                 values: sig.inputs.iter().map(|t| {
883                     Argument {
884                         type_: t.clean(),
885                         id: 0,
886                         name: names.next().unwrap_or("".to_string()),
887                     }
888                 }).collect(),
889             },
890         }
891     }
892 }
893
894 #[deriving(Clone, Encodable, Decodable)]
895 pub struct Argument {
896     pub type_: Type,
897     pub name: String,
898     pub id: ast::NodeId,
899 }
900
901 impl Clean<Argument> for ast::Arg {
902     fn clean(&self) -> Argument {
903         Argument {
904             name: name_from_pat(&*self.pat),
905             type_: (self.ty.clean()),
906             id: self.id
907         }
908     }
909 }
910
911 #[deriving(Clone, Encodable, Decodable)]
912 pub enum RetStyle {
913     NoReturn,
914     Return
915 }
916
917 impl Clean<RetStyle> for ast::RetStyle {
918     fn clean(&self) -> RetStyle {
919         match *self {
920             ast::Return => Return,
921             ast::NoReturn => NoReturn
922         }
923     }
924 }
925
926 #[deriving(Clone, Encodable, Decodable)]
927 pub struct Trait {
928     pub methods: Vec<TraitMethod>,
929     pub generics: Generics,
930     pub parents: Vec<Type>,
931 }
932
933 impl Clean<Item> for doctree::Trait {
934     fn clean(&self) -> Item {
935         Item {
936             name: Some(self.name.clean()),
937             attrs: self.attrs.clean(),
938             source: self.where.clean(),
939             def_id: ast_util::local_def(self.id),
940             visibility: self.vis.clean(),
941             stability: self.stab.clean(),
942             inner: TraitItem(Trait {
943                 methods: self.methods.clean(),
944                 generics: self.generics.clean(),
945                 parents: self.parents.clean(),
946             }),
947         }
948     }
949 }
950
951 impl Clean<Type> for ast::TraitRef {
952     fn clean(&self) -> Type {
953         resolve_type(self.path.clean(), None, self.ref_id)
954     }
955 }
956
957 #[deriving(Clone, Encodable, Decodable)]
958 pub enum TraitMethod {
959     Required(Item),
960     Provided(Item),
961 }
962
963 impl TraitMethod {
964     pub fn is_req(&self) -> bool {
965         match self {
966             &Required(..) => true,
967             _ => false,
968         }
969     }
970     pub fn is_def(&self) -> bool {
971         match self {
972             &Provided(..) => true,
973             _ => false,
974         }
975     }
976     pub fn item<'a>(&'a self) -> &'a Item {
977         match *self {
978             Required(ref item) => item,
979             Provided(ref item) => item,
980         }
981     }
982 }
983
984 impl Clean<TraitMethod> for ast::TraitMethod {
985     fn clean(&self) -> TraitMethod {
986         match self {
987             &ast::Required(ref t) => Required(t.clean()),
988             &ast::Provided(ref t) => Provided(t.clean()),
989         }
990     }
991 }
992
993 impl Clean<Item> for ty::Method {
994     fn clean(&self) -> Item {
995         let cx = get_cx();
996         let (self_, sig) = match self.explicit_self {
997             ast::SelfStatic => (ast::SelfStatic.clean(), self.fty.sig.clone()),
998             s => {
999                 let sig = ty::FnSig {
1000                     inputs: Vec::from_slice(self.fty.sig.inputs.slice_from(1)),
1001                     ..self.fty.sig.clone()
1002                 };
1003                 let s = match s {
1004                     ast::SelfRegion(..) => {
1005                         match ty::get(*self.fty.sig.inputs.get(0)).sty {
1006                             ty::ty_rptr(r, mt) => {
1007                                 SelfBorrowed(r.clean(), mt.mutbl.clean())
1008                             }
1009                             _ => s.clean(),
1010                         }
1011                     }
1012                     s => s.clean(),
1013                 };
1014                 (s, sig)
1015             }
1016         };
1017
1018         Item {
1019             name: Some(self.ident.clean()),
1020             visibility: Some(ast::Inherited),
1021             stability: get_stability(self.def_id),
1022             def_id: self.def_id,
1023             attrs: inline::load_attrs(cx.tcx(), self.def_id),
1024             source: Span::empty(),
1025             inner: TyMethodItem(TyMethod {
1026                 fn_style: self.fty.fn_style,
1027                 generics: self.generics.clean(),
1028                 self_: self_,
1029                 decl: (self.def_id, &sig).clean(),
1030             })
1031         }
1032     }
1033 }
1034
1035 /// A representation of a Type suitable for hyperlinking purposes. Ideally one can get the original
1036 /// type out of the AST/ty::ctxt given one of these, if more information is needed. Most importantly
1037 /// it does not preserve mutability or boxes.
1038 #[deriving(Clone, Encodable, Decodable)]
1039 pub enum Type {
1040     /// structs/enums/traits (anything that'd be an ast::TyPath)
1041     ResolvedPath {
1042         pub path: Path,
1043         pub typarams: Option<Vec<TyParamBound>>,
1044         pub did: ast::DefId,
1045     },
1046     // I have no idea how to usefully use this.
1047     TyParamBinder(ast::NodeId),
1048     /// For parameterized types, so the consumer of the JSON don't go looking
1049     /// for types which don't exist anywhere.
1050     Generic(ast::DefId),
1051     /// For references to self
1052     Self(ast::DefId),
1053     /// Primitives are just the fixed-size numeric types (plus int/uint/float), and char.
1054     Primitive(Primitive),
1055     Closure(Box<ClosureDecl>, Option<Lifetime>),
1056     Proc(Box<ClosureDecl>),
1057     /// extern "ABI" fn
1058     BareFunction(Box<BareFunctionDecl>),
1059     Tuple(Vec<Type>),
1060     Vector(Box<Type>),
1061     FixedVector(Box<Type>, String),
1062     /// aka TyBot
1063     Bottom,
1064     Unique(Box<Type>),
1065     Managed(Box<Type>),
1066     RawPointer(Mutability, Box<Type>),
1067     BorrowedRef {
1068         pub lifetime: Option<Lifetime>,
1069         pub mutability: Mutability,
1070         pub type_: Box<Type>,
1071     },
1072     // region, raw, other boxes, mutable
1073 }
1074
1075 #[deriving(Clone, Encodable, Decodable, PartialEq, Eq, Hash)]
1076 pub enum Primitive {
1077     Int, I8, I16, I32, I64,
1078     Uint, U8, U16, U32, U64,
1079     F32, F64,
1080     Char,
1081     Bool,
1082     Nil,
1083     Str,
1084     Slice,
1085     PrimitiveTuple,
1086 }
1087
1088 #[deriving(Clone, Encodable, Decodable)]
1089 pub enum TypeKind {
1090     TypeEnum,
1091     TypeFunction,
1092     TypeModule,
1093     TypeStatic,
1094     TypeStruct,
1095     TypeTrait,
1096     TypeVariant,
1097 }
1098
1099 impl Primitive {
1100     fn from_str(s: &str) -> Option<Primitive> {
1101         match s.as_slice() {
1102             "int" => Some(Int),
1103             "i8" => Some(I8),
1104             "i16" => Some(I16),
1105             "i32" => Some(I32),
1106             "i64" => Some(I64),
1107             "uint" => Some(Uint),
1108             "u8" => Some(U8),
1109             "u16" => Some(U16),
1110             "u32" => Some(U32),
1111             "u64" => Some(U64),
1112             "bool" => Some(Bool),
1113             "nil" => Some(Nil),
1114             "char" => Some(Char),
1115             "str" => Some(Str),
1116             "f32" => Some(F32),
1117             "f64" => Some(F64),
1118             "slice" => Some(Slice),
1119             "tuple" => Some(PrimitiveTuple),
1120             _ => None,
1121         }
1122     }
1123
1124     fn find(attrs: &[Attribute]) -> Option<Primitive> {
1125         for attr in attrs.iter() {
1126             let list = match *attr {
1127                 List(ref k, ref l) if k.as_slice() == "doc" => l,
1128                 _ => continue,
1129             };
1130             for sub_attr in list.iter() {
1131                 let value = match *sub_attr {
1132                     NameValue(ref k, ref v)
1133                         if k.as_slice() == "primitive" => v.as_slice(),
1134                     _ => continue,
1135                 };
1136                 match Primitive::from_str(value) {
1137                     Some(p) => return Some(p),
1138                     None => {}
1139                 }
1140             }
1141         }
1142         return None
1143     }
1144
1145     pub fn to_str(&self) -> &'static str {
1146         match *self {
1147             Int => "int",
1148             I8 => "i8",
1149             I16 => "i16",
1150             I32 => "i32",
1151             I64 => "i64",
1152             Uint => "uint",
1153             U8 => "u8",
1154             U16 => "u16",
1155             U32 => "u32",
1156             U64 => "u64",
1157             F32 => "f32",
1158             F64 => "f64",
1159             Str => "str",
1160             Bool => "bool",
1161             Char => "char",
1162             Nil => "()",
1163             Slice => "slice",
1164             PrimitiveTuple => "tuple",
1165         }
1166     }
1167
1168     pub fn to_url_str(&self) -> &'static str {
1169         match *self {
1170             Nil => "nil",
1171             other => other.to_str(),
1172         }
1173     }
1174
1175     /// Creates a rustdoc-specific node id for primitive types.
1176     ///
1177     /// These node ids are generally never used by the AST itself.
1178     pub fn to_node_id(&self) -> ast::NodeId {
1179         u32::MAX - 1 - (*self as u32)
1180     }
1181 }
1182
1183 impl Clean<Type> for ast::Ty {
1184     fn clean(&self) -> Type {
1185         use syntax::ast::*;
1186         match self.node {
1187             TyNil => Primitive(Nil),
1188             TyPtr(ref m) => RawPointer(m.mutbl.clean(), box m.ty.clean()),
1189             TyRptr(ref l, ref m) =>
1190                 BorrowedRef {lifetime: l.clean(), mutability: m.mutbl.clean(),
1191                              type_: box m.ty.clean()},
1192             TyBox(ty) => Managed(box ty.clean()),
1193             TyUniq(ty) => Unique(box ty.clean()),
1194             TyVec(ty) => Vector(box ty.clean()),
1195             TyFixedLengthVec(ty, ref e) => FixedVector(box ty.clean(),
1196                                                        e.span.to_src()),
1197             TyTup(ref tys) => Tuple(tys.iter().map(|x| x.clean()).collect()),
1198             TyPath(ref p, ref tpbs, id) => {
1199                 resolve_type(p.clean(),
1200                              tpbs.clean().map(|x| x.move_iter().collect()),
1201                              id)
1202             }
1203             TyClosure(ref c, region) => Closure(box c.clean(), region.clean()),
1204             TyProc(ref c) => Proc(box c.clean()),
1205             TyBareFn(ref barefn) => BareFunction(box barefn.clean()),
1206             TyParen(ref ty) => ty.clean(),
1207             TyBot => Bottom,
1208             ref x => fail!("Unimplemented type {:?}", x),
1209         }
1210     }
1211 }
1212
1213 impl Clean<Type> for ty::t {
1214     fn clean(&self) -> Type {
1215         match ty::get(*self).sty {
1216             ty::ty_bot => Bottom,
1217             ty::ty_nil => Primitive(Nil),
1218             ty::ty_bool => Primitive(Bool),
1219             ty::ty_char => Primitive(Char),
1220             ty::ty_int(ast::TyI) => Primitive(Int),
1221             ty::ty_int(ast::TyI8) => Primitive(I8),
1222             ty::ty_int(ast::TyI16) => Primitive(I16),
1223             ty::ty_int(ast::TyI32) => Primitive(I32),
1224             ty::ty_int(ast::TyI64) => Primitive(I64),
1225             ty::ty_uint(ast::TyU) => Primitive(Uint),
1226             ty::ty_uint(ast::TyU8) => Primitive(U8),
1227             ty::ty_uint(ast::TyU16) => Primitive(U16),
1228             ty::ty_uint(ast::TyU32) => Primitive(U32),
1229             ty::ty_uint(ast::TyU64) => Primitive(U64),
1230             ty::ty_float(ast::TyF32) => Primitive(F32),
1231             ty::ty_float(ast::TyF64) => Primitive(F64),
1232             ty::ty_str => Primitive(Str),
1233             ty::ty_box(t) => Managed(box t.clean()),
1234             ty::ty_uniq(t) => Unique(box t.clean()),
1235             ty::ty_vec(mt, None) => Vector(box mt.ty.clean()),
1236             ty::ty_vec(mt, Some(i)) => FixedVector(box mt.ty.clean(),
1237                                                    format!("{}", i)),
1238             ty::ty_ptr(mt) => RawPointer(mt.mutbl.clean(), box mt.ty.clean()),
1239             ty::ty_rptr(r, mt) => BorrowedRef {
1240                 lifetime: r.clean(),
1241                 mutability: mt.mutbl.clean(),
1242                 type_: box mt.ty.clean(),
1243             },
1244             ty::ty_bare_fn(ref fty) => BareFunction(box BareFunctionDecl {
1245                 fn_style: fty.fn_style,
1246                 generics: Generics {
1247                     lifetimes: Vec::new(), type_params: Vec::new()
1248                 },
1249                 decl: (ast_util::local_def(0), &fty.sig).clean(),
1250                 abi: fty.abi.to_str(),
1251             }),
1252             ty::ty_closure(ref fty) => {
1253                 let decl = box ClosureDecl {
1254                     lifetimes: Vec::new(), // FIXME: this looks wrong...
1255                     decl: (ast_util::local_def(0), &fty.sig).clean(),
1256                     onceness: fty.onceness,
1257                     fn_style: fty.fn_style,
1258                     bounds: fty.bounds.iter().map(|i| i.clean()).collect(),
1259                 };
1260                 match fty.store {
1261                     ty::UniqTraitStore => Proc(decl),
1262                     ty::RegionTraitStore(ref r, _) => Closure(decl, r.clean()),
1263                 }
1264             }
1265             ty::ty_struct(did, ref substs) |
1266             ty::ty_enum(did, ref substs) |
1267             ty::ty_trait(box ty::TyTrait { def_id: did, ref substs, .. }) => {
1268                 let fqn = csearch::get_item_path(get_cx().tcx(), did);
1269                 let fqn: Vec<String> = fqn.move_iter().map(|i| {
1270                     i.to_str()
1271                 }).collect();
1272                 let kind = match ty::get(*self).sty {
1273                     ty::ty_struct(..) => TypeStruct,
1274                     ty::ty_trait(..) => TypeTrait,
1275                     _ => TypeEnum,
1276                 };
1277                 let path = external_path(fqn.last().unwrap().to_str().as_slice(),
1278                                          substs);
1279                 get_cx().external_paths.borrow_mut().get_mut_ref()
1280                                        .insert(did, (fqn, kind));
1281                 ResolvedPath {
1282                     path: path,
1283                     typarams: None,
1284                     did: did,
1285                 }
1286             }
1287             ty::ty_tup(ref t) => Tuple(t.iter().map(|t| t.clean()).collect()),
1288
1289             ty::ty_param(ref p) => {
1290                 if p.space == subst::SelfSpace {
1291                     Self(p.def_id)
1292                 } else {
1293                     Generic(p.def_id)
1294                 }
1295             }
1296
1297             ty::ty_infer(..) => fail!("ty_infer"),
1298             ty::ty_err => fail!("ty_err"),
1299         }
1300     }
1301 }
1302
1303 #[deriving(Clone, Encodable, Decodable)]
1304 pub enum StructField {
1305     HiddenStructField, // inserted later by strip passes
1306     TypedStructField(Type),
1307 }
1308
1309 impl Clean<Item> for ast::StructField {
1310     fn clean(&self) -> Item {
1311         let (name, vis) = match self.node.kind {
1312             ast::NamedField(id, vis) => (Some(id), vis),
1313             ast::UnnamedField(vis) => (None, vis)
1314         };
1315         Item {
1316             name: name.clean(),
1317             attrs: self.node.attrs.clean().move_iter().collect(),
1318             source: self.span.clean(),
1319             visibility: Some(vis),
1320             stability: get_stability(ast_util::local_def(self.node.id)),
1321             def_id: ast_util::local_def(self.node.id),
1322             inner: StructFieldItem(TypedStructField(self.node.ty.clean())),
1323         }
1324     }
1325 }
1326
1327 impl Clean<Item> for ty::field_ty {
1328     fn clean(&self) -> Item {
1329         use syntax::parse::token::special_idents::unnamed_field;
1330         let name = if self.name == unnamed_field.name {
1331             None
1332         } else {
1333             Some(self.name)
1334         };
1335         let cx = get_cx();
1336         let ty = ty::lookup_item_type(cx.tcx(), self.id);
1337         Item {
1338             name: name.clean(),
1339             attrs: inline::load_attrs(cx.tcx(), self.id),
1340             source: Span::empty(),
1341             visibility: Some(self.vis),
1342             stability: get_stability(self.id),
1343             def_id: self.id,
1344             inner: StructFieldItem(TypedStructField(ty.ty.clean())),
1345         }
1346     }
1347 }
1348
1349 pub type Visibility = ast::Visibility;
1350
1351 impl Clean<Option<Visibility>> for ast::Visibility {
1352     fn clean(&self) -> Option<Visibility> {
1353         Some(*self)
1354     }
1355 }
1356
1357 #[deriving(Clone, Encodable, Decodable)]
1358 pub struct Struct {
1359     pub struct_type: doctree::StructType,
1360     pub generics: Generics,
1361     pub fields: Vec<Item>,
1362     pub fields_stripped: bool,
1363 }
1364
1365 impl Clean<Item> for doctree::Struct {
1366     fn clean(&self) -> Item {
1367         Item {
1368             name: Some(self.name.clean()),
1369             attrs: self.attrs.clean(),
1370             source: self.where.clean(),
1371             def_id: ast_util::local_def(self.id),
1372             visibility: self.vis.clean(),
1373             stability: self.stab.clean(),
1374             inner: StructItem(Struct {
1375                 struct_type: self.struct_type,
1376                 generics: self.generics.clean(),
1377                 fields: self.fields.clean(),
1378                 fields_stripped: false,
1379             }),
1380         }
1381     }
1382 }
1383
1384 /// This is a more limited form of the standard Struct, different in that
1385 /// it lacks the things most items have (name, id, parameterization). Found
1386 /// only as a variant in an enum.
1387 #[deriving(Clone, Encodable, Decodable)]
1388 pub struct VariantStruct {
1389     pub struct_type: doctree::StructType,
1390     pub fields: Vec<Item>,
1391     pub fields_stripped: bool,
1392 }
1393
1394 impl Clean<VariantStruct> for syntax::ast::StructDef {
1395     fn clean(&self) -> VariantStruct {
1396         VariantStruct {
1397             struct_type: doctree::struct_type_from_def(self),
1398             fields: self.fields.clean().move_iter().collect(),
1399             fields_stripped: false,
1400         }
1401     }
1402 }
1403
1404 #[deriving(Clone, Encodable, Decodable)]
1405 pub struct Enum {
1406     pub variants: Vec<Item>,
1407     pub generics: Generics,
1408     pub variants_stripped: bool,
1409 }
1410
1411 impl Clean<Item> for doctree::Enum {
1412     fn clean(&self) -> Item {
1413         Item {
1414             name: Some(self.name.clean()),
1415             attrs: self.attrs.clean(),
1416             source: self.where.clean(),
1417             def_id: ast_util::local_def(self.id),
1418             visibility: self.vis.clean(),
1419             stability: self.stab.clean(),
1420             inner: EnumItem(Enum {
1421                 variants: self.variants.clean(),
1422                 generics: self.generics.clean(),
1423                 variants_stripped: false,
1424             }),
1425         }
1426     }
1427 }
1428
1429 #[deriving(Clone, Encodable, Decodable)]
1430 pub struct Variant {
1431     pub kind: VariantKind,
1432 }
1433
1434 impl Clean<Item> for doctree::Variant {
1435     fn clean(&self) -> Item {
1436         Item {
1437             name: Some(self.name.clean()),
1438             attrs: self.attrs.clean(),
1439             source: self.where.clean(),
1440             visibility: self.vis.clean(),
1441             stability: self.stab.clean(),
1442             def_id: ast_util::local_def(self.id),
1443             inner: VariantItem(Variant {
1444                 kind: self.kind.clean(),
1445             }),
1446         }
1447     }
1448 }
1449
1450 impl Clean<Item> for ty::VariantInfo {
1451     fn clean(&self) -> Item {
1452         // use syntax::parse::token::special_idents::unnamed_field;
1453         let cx = get_cx();
1454         let kind = match self.arg_names.as_ref().map(|s| s.as_slice()) {
1455             None | Some([]) if self.args.len() == 0 => CLikeVariant,
1456             None | Some([]) => {
1457                 TupleVariant(self.args.iter().map(|t| t.clean()).collect())
1458             }
1459             Some(s) => {
1460                 StructVariant(VariantStruct {
1461                     struct_type: doctree::Plain,
1462                     fields_stripped: false,
1463                     fields: s.iter().zip(self.args.iter()).map(|(name, ty)| {
1464                         Item {
1465                             source: Span::empty(),
1466                             name: Some(name.clean()),
1467                             attrs: Vec::new(),
1468                             visibility: Some(ast::Public),
1469                             stability: get_stability(self.id),
1470                             // FIXME: this is not accurate, we need an id for
1471                             //        the specific field but we're using the id
1472                             //        for the whole variant. Nothing currently
1473                             //        uses this so we should be good for now.
1474                             def_id: self.id,
1475                             inner: StructFieldItem(
1476                                 TypedStructField(ty.clean())
1477                             )
1478                         }
1479                     }).collect()
1480                 })
1481             }
1482         };
1483         Item {
1484             name: Some(self.name.clean()),
1485             attrs: inline::load_attrs(cx.tcx(), self.id),
1486             source: Span::empty(),
1487             visibility: Some(ast::Public),
1488             def_id: self.id,
1489             inner: VariantItem(Variant { kind: kind }),
1490             stability: None,
1491         }
1492     }
1493 }
1494
1495 #[deriving(Clone, Encodable, Decodable)]
1496 pub enum VariantKind {
1497     CLikeVariant,
1498     TupleVariant(Vec<Type>),
1499     StructVariant(VariantStruct),
1500 }
1501
1502 impl Clean<VariantKind> for ast::VariantKind {
1503     fn clean(&self) -> VariantKind {
1504         match self {
1505             &ast::TupleVariantKind(ref args) => {
1506                 if args.len() == 0 {
1507                     CLikeVariant
1508                 } else {
1509                     TupleVariant(args.iter().map(|x| x.ty.clean()).collect())
1510                 }
1511             },
1512             &ast::StructVariantKind(ref sd) => StructVariant(sd.clean()),
1513         }
1514     }
1515 }
1516
1517 #[deriving(Clone, Encodable, Decodable)]
1518 pub struct Span {
1519     pub filename: String,
1520     pub loline: uint,
1521     pub locol: uint,
1522     pub hiline: uint,
1523     pub hicol: uint,
1524 }
1525
1526 impl Span {
1527     fn empty() -> Span {
1528         Span {
1529             filename: "".to_string(),
1530             loline: 0, locol: 0,
1531             hiline: 0, hicol: 0,
1532         }
1533     }
1534 }
1535
1536 impl Clean<Span> for syntax::codemap::Span {
1537     fn clean(&self) -> Span {
1538         let ctxt = super::ctxtkey.get().unwrap();
1539         let cm = ctxt.sess().codemap();
1540         let filename = cm.span_to_filename(*self);
1541         let lo = cm.lookup_char_pos(self.lo);
1542         let hi = cm.lookup_char_pos(self.hi);
1543         Span {
1544             filename: filename.to_string(),
1545             loline: lo.line,
1546             locol: lo.col.to_uint(),
1547             hiline: hi.line,
1548             hicol: hi.col.to_uint(),
1549         }
1550     }
1551 }
1552
1553 #[deriving(Clone, Encodable, Decodable)]
1554 pub struct Path {
1555     pub global: bool,
1556     pub segments: Vec<PathSegment>,
1557 }
1558
1559 impl Clean<Path> for ast::Path {
1560     fn clean(&self) -> Path {
1561         Path {
1562             global: self.global,
1563             segments: self.segments.clean().move_iter().collect(),
1564         }
1565     }
1566 }
1567
1568 #[deriving(Clone, Encodable, Decodable)]
1569 pub struct PathSegment {
1570     pub name: String,
1571     pub lifetimes: Vec<Lifetime>,
1572     pub types: Vec<Type>,
1573 }
1574
1575 impl Clean<PathSegment> for ast::PathSegment {
1576     fn clean(&self) -> PathSegment {
1577         PathSegment {
1578             name: self.identifier.clean(),
1579             lifetimes: self.lifetimes.clean().move_iter().collect(),
1580             types: self.types.clean().move_iter().collect()
1581         }
1582     }
1583 }
1584
1585 fn path_to_str(p: &ast::Path) -> String {
1586     use syntax::parse::token;
1587
1588     let mut s = String::new();
1589     let mut first = true;
1590     for i in p.segments.iter().map(|x| token::get_ident(x.identifier)) {
1591         if !first || p.global {
1592             s.push_str("::");
1593         } else {
1594             first = false;
1595         }
1596         s.push_str(i.get());
1597     }
1598     s
1599 }
1600
1601 impl Clean<String> for ast::Ident {
1602     fn clean(&self) -> String {
1603         token::get_ident(*self).get().to_string()
1604     }
1605 }
1606
1607 impl Clean<String> for ast::Name {
1608     fn clean(&self) -> String {
1609         token::get_name(*self).get().to_string()
1610     }
1611 }
1612
1613 #[deriving(Clone, Encodable, Decodable)]
1614 pub struct Typedef {
1615     pub type_: Type,
1616     pub generics: Generics,
1617 }
1618
1619 impl Clean<Item> for doctree::Typedef {
1620     fn clean(&self) -> Item {
1621         Item {
1622             name: Some(self.name.clean()),
1623             attrs: self.attrs.clean(),
1624             source: self.where.clean(),
1625             def_id: ast_util::local_def(self.id.clone()),
1626             visibility: self.vis.clean(),
1627             stability: self.stab.clean(),
1628             inner: TypedefItem(Typedef {
1629                 type_: self.ty.clean(),
1630                 generics: self.gen.clean(),
1631             }),
1632         }
1633     }
1634 }
1635
1636 #[deriving(Clone, Encodable, Decodable)]
1637 pub struct BareFunctionDecl {
1638     pub fn_style: ast::FnStyle,
1639     pub generics: Generics,
1640     pub decl: FnDecl,
1641     pub abi: String,
1642 }
1643
1644 impl Clean<BareFunctionDecl> for ast::BareFnTy {
1645     fn clean(&self) -> BareFunctionDecl {
1646         BareFunctionDecl {
1647             fn_style: self.fn_style,
1648             generics: Generics {
1649                 lifetimes: self.lifetimes.clean().move_iter().collect(),
1650                 type_params: Vec::new(),
1651             },
1652             decl: self.decl.clean(),
1653             abi: self.abi.to_str(),
1654         }
1655     }
1656 }
1657
1658 #[deriving(Clone, Encodable, Decodable)]
1659 pub struct Static {
1660     pub type_: Type,
1661     pub mutability: Mutability,
1662     /// It's useful to have the value of a static documented, but I have no
1663     /// desire to represent expressions (that'd basically be all of the AST,
1664     /// which is huge!). So, have a string.
1665     pub expr: String,
1666 }
1667
1668 impl Clean<Item> for doctree::Static {
1669     fn clean(&self) -> Item {
1670         debug!("claning static {}: {:?}", self.name.clean(), self);
1671         Item {
1672             name: Some(self.name.clean()),
1673             attrs: self.attrs.clean(),
1674             source: self.where.clean(),
1675             def_id: ast_util::local_def(self.id),
1676             visibility: self.vis.clean(),
1677             stability: self.stab.clean(),
1678             inner: StaticItem(Static {
1679                 type_: self.type_.clean(),
1680                 mutability: self.mutability.clean(),
1681                 expr: self.expr.span.to_src(),
1682             }),
1683         }
1684     }
1685 }
1686
1687 #[deriving(Show, Clone, Encodable, Decodable, PartialEq)]
1688 pub enum Mutability {
1689     Mutable,
1690     Immutable,
1691 }
1692
1693 impl Clean<Mutability> for ast::Mutability {
1694     fn clean(&self) -> Mutability {
1695         match self {
1696             &ast::MutMutable => Mutable,
1697             &ast::MutImmutable => Immutable,
1698         }
1699     }
1700 }
1701
1702 #[deriving(Clone, Encodable, Decodable)]
1703 pub struct Impl {
1704     pub generics: Generics,
1705     pub trait_: Option<Type>,
1706     pub for_: Type,
1707     pub methods: Vec<Item>,
1708     pub derived: bool,
1709 }
1710
1711 fn detect_derived<M: AttrMetaMethods>(attrs: &[M]) -> bool {
1712     attr::contains_name(attrs, "automatically_derived")
1713 }
1714
1715 impl Clean<Item> for doctree::Impl {
1716     fn clean(&self) -> Item {
1717         Item {
1718             name: None,
1719             attrs: self.attrs.clean(),
1720             source: self.where.clean(),
1721             def_id: ast_util::local_def(self.id),
1722             visibility: self.vis.clean(),
1723             stability: self.stab.clean(),
1724             inner: ImplItem(Impl {
1725                 generics: self.generics.clean(),
1726                 trait_: self.trait_.clean(),
1727                 for_: self.for_.clean(),
1728                 methods: self.methods.clean(),
1729                 derived: detect_derived(self.attrs.as_slice()),
1730             }),
1731         }
1732     }
1733 }
1734
1735 #[deriving(Clone, Encodable, Decodable)]
1736 pub struct ViewItem {
1737     pub inner: ViewItemInner,
1738 }
1739
1740 impl Clean<Vec<Item>> for ast::ViewItem {
1741     fn clean(&self) -> Vec<Item> {
1742         // We consider inlining the documentation of `pub use` statments, but we
1743         // forcefully don't inline if this is not public or if the
1744         // #[doc(no_inline)] attribute is present.
1745         let denied = self.vis != ast::Public || self.attrs.iter().any(|a| {
1746             a.name().get() == "doc" && match a.meta_item_list() {
1747                 Some(l) => attr::contains_name(l, "no_inline"),
1748                 None => false,
1749             }
1750         });
1751         let convert = |node: &ast::ViewItem_| {
1752             Item {
1753                 name: None,
1754                 attrs: self.attrs.clean().move_iter().collect(),
1755                 source: self.span.clean(),
1756                 def_id: ast_util::local_def(0),
1757                 visibility: self.vis.clean(),
1758                 stability: None,
1759                 inner: ViewItemItem(ViewItem { inner: node.clean() }),
1760             }
1761         };
1762         let mut ret = Vec::new();
1763         match self.node {
1764             ast::ViewItemUse(ref path) if !denied => {
1765                 match path.node {
1766                     ast::ViewPathGlob(..) => ret.push(convert(&self.node)),
1767                     ast::ViewPathList(ref a, ref list, ref b) => {
1768                         // Attempt to inline all reexported items, but be sure
1769                         // to keep any non-inlineable reexports so they can be
1770                         // listed in the documentation.
1771                         let remaining = list.iter().filter(|path| {
1772                             match inline::try_inline(path.node.id) {
1773                                 Some(items) => {
1774                                     ret.extend(items.move_iter()); false
1775                                 }
1776                                 None => true,
1777                             }
1778                         }).map(|a| a.clone()).collect::<Vec<ast::PathListIdent>>();
1779                         if remaining.len() > 0 {
1780                             let path = ast::ViewPathList(a.clone(),
1781                                                          remaining,
1782                                                          b.clone());
1783                             let path = syntax::codemap::dummy_spanned(path);
1784                             ret.push(convert(&ast::ViewItemUse(box(GC) path)));
1785                         }
1786                     }
1787                     ast::ViewPathSimple(_, _, id) => {
1788                         match inline::try_inline(id) {
1789                             Some(items) => ret.extend(items.move_iter()),
1790                             None => ret.push(convert(&self.node)),
1791                         }
1792                     }
1793                 }
1794             }
1795             ref n => ret.push(convert(n)),
1796         }
1797         return ret;
1798     }
1799 }
1800
1801 #[deriving(Clone, Encodable, Decodable)]
1802 pub enum ViewItemInner {
1803     ExternCrate(String, Option<String>, ast::NodeId),
1804     Import(ViewPath)
1805 }
1806
1807 impl Clean<ViewItemInner> for ast::ViewItem_ {
1808     fn clean(&self) -> ViewItemInner {
1809         match self {
1810             &ast::ViewItemExternCrate(ref i, ref p, ref id) => {
1811                 let string = match *p {
1812                     None => None,
1813                     Some((ref x, _)) => Some(x.get().to_string()),
1814                 };
1815                 ExternCrate(i.clean(), string, *id)
1816             }
1817             &ast::ViewItemUse(ref vp) => {
1818                 Import(vp.clean())
1819             }
1820         }
1821     }
1822 }
1823
1824 #[deriving(Clone, Encodable, Decodable)]
1825 pub enum ViewPath {
1826     // use str = source;
1827     SimpleImport(String, ImportSource),
1828     // use source::*;
1829     GlobImport(ImportSource),
1830     // use source::{a, b, c};
1831     ImportList(ImportSource, Vec<ViewListIdent>),
1832 }
1833
1834 #[deriving(Clone, Encodable, Decodable)]
1835 pub struct ImportSource {
1836     pub path: Path,
1837     pub did: Option<ast::DefId>,
1838 }
1839
1840 impl Clean<ViewPath> for ast::ViewPath {
1841     fn clean(&self) -> ViewPath {
1842         match self.node {
1843             ast::ViewPathSimple(ref i, ref p, id) =>
1844                 SimpleImport(i.clean(), resolve_use_source(p.clean(), id)),
1845             ast::ViewPathGlob(ref p, id) =>
1846                 GlobImport(resolve_use_source(p.clean(), id)),
1847             ast::ViewPathList(ref p, ref pl, id) => {
1848                 ImportList(resolve_use_source(p.clean(), id),
1849                            pl.clean().move_iter().collect())
1850             }
1851         }
1852     }
1853 }
1854
1855 #[deriving(Clone, Encodable, Decodable)]
1856 pub struct ViewListIdent {
1857     pub name: String,
1858     pub source: Option<ast::DefId>,
1859 }
1860
1861 impl Clean<ViewListIdent> for ast::PathListIdent {
1862     fn clean(&self) -> ViewListIdent {
1863         ViewListIdent {
1864             name: self.node.name.clean(),
1865             source: resolve_def(self.node.id),
1866         }
1867     }
1868 }
1869
1870 impl Clean<Vec<Item>> for ast::ForeignMod {
1871     fn clean(&self) -> Vec<Item> {
1872         self.items.clean()
1873     }
1874 }
1875
1876 impl Clean<Item> for ast::ForeignItem {
1877     fn clean(&self) -> Item {
1878         let inner = match self.node {
1879             ast::ForeignItemFn(ref decl, ref generics) => {
1880                 ForeignFunctionItem(Function {
1881                     decl: decl.clean(),
1882                     generics: generics.clean(),
1883                     fn_style: ast::UnsafeFn,
1884                 })
1885             }
1886             ast::ForeignItemStatic(ref ty, mutbl) => {
1887                 ForeignStaticItem(Static {
1888                     type_: ty.clean(),
1889                     mutability: if mutbl {Mutable} else {Immutable},
1890                     expr: "".to_string(),
1891                 })
1892             }
1893         };
1894         Item {
1895             name: Some(self.ident.clean()),
1896             attrs: self.attrs.clean().move_iter().collect(),
1897             source: self.span.clean(),
1898             def_id: ast_util::local_def(self.id),
1899             visibility: self.vis.clean(),
1900             stability: None,
1901             inner: inner,
1902         }
1903     }
1904 }
1905
1906 // Utilities
1907
1908 trait ToSource {
1909     fn to_src(&self) -> String;
1910 }
1911
1912 impl ToSource for syntax::codemap::Span {
1913     fn to_src(&self) -> String {
1914         debug!("converting span {:?} to snippet", self.clean());
1915         let ctxt = super::ctxtkey.get().unwrap();
1916         let cm = ctxt.sess().codemap().clone();
1917         let sn = match cm.span_to_snippet(*self) {
1918             Some(x) => x.to_string(),
1919             None    => "".to_string()
1920         };
1921         debug!("got snippet {}", sn);
1922         sn
1923     }
1924 }
1925
1926 fn lit_to_str(lit: &ast::Lit) -> String {
1927     match lit.node {
1928         ast::LitStr(ref st, _) => st.get().to_string(),
1929         ast::LitBinary(ref data) => format!("{:?}", data.as_slice()),
1930         ast::LitByte(b) => {
1931             let mut res = String::from_str("b'");
1932             (b as char).escape_default(|c| {
1933                 res.push_char(c);
1934             });
1935             res.push_char('\'');
1936             res
1937         },
1938         ast::LitChar(c) => format!("'{}'", c),
1939         ast::LitInt(i, _t) => i.to_str(),
1940         ast::LitUint(u, _t) => u.to_str(),
1941         ast::LitIntUnsuffixed(i) => i.to_str(),
1942         ast::LitFloat(ref f, _t) => f.get().to_string(),
1943         ast::LitFloatUnsuffixed(ref f) => f.get().to_string(),
1944         ast::LitBool(b) => b.to_str(),
1945         ast::LitNil => "".to_string(),
1946     }
1947 }
1948
1949 fn name_from_pat(p: &ast::Pat) -> String {
1950     use syntax::ast::*;
1951     debug!("Trying to get a name from pattern: {:?}", p);
1952
1953     match p.node {
1954         PatWild => "_".to_string(),
1955         PatWildMulti => "..".to_string(),
1956         PatIdent(_, ref p, _) => path_to_str(p),
1957         PatEnum(ref p, _) => path_to_str(p),
1958         PatStruct(..) => fail!("tried to get argument name from pat_struct, \
1959                                 which is not allowed in function arguments"),
1960         PatTup(..) => "(tuple arg NYI)".to_string(),
1961         PatBox(p) => name_from_pat(&*p),
1962         PatRegion(p) => name_from_pat(&*p),
1963         PatLit(..) => {
1964             warn!("tried to get argument name from PatLit, \
1965                   which is silly in function arguments");
1966             "()".to_string()
1967         },
1968         PatRange(..) => fail!("tried to get argument name from PatRange, \
1969                               which is not allowed in function arguments"),
1970         PatVec(..) => fail!("tried to get argument name from pat_vec, \
1971                              which is not allowed in function arguments"),
1972         PatMac(..) => {
1973             warn!("can't document the name of a function argument \
1974                    produced by a pattern macro");
1975             "(argument produced by macro)".to_string()
1976         }
1977     }
1978 }
1979
1980 /// Given a Type, resolve it using the def_map
1981 fn resolve_type(path: Path, tpbs: Option<Vec<TyParamBound>>,
1982                 id: ast::NodeId) -> Type {
1983     let cx = get_cx();
1984     let tycx = match cx.maybe_typed {
1985         core::Typed(ref tycx) => tycx,
1986         // If we're extracting tests, this return value doesn't matter.
1987         core::NotTyped(_) => return Primitive(Bool),
1988     };
1989     debug!("searching for {:?} in defmap", id);
1990     let def = match tycx.def_map.borrow().find(&id) {
1991         Some(&k) => k,
1992         None => fail!("unresolved id not in defmap")
1993     };
1994
1995     match def {
1996         def::DefSelfTy(i) => return Self(ast_util::local_def(i)),
1997         def::DefPrimTy(p) => match p {
1998             ast::TyStr => return Primitive(Str),
1999             ast::TyBool => return Primitive(Bool),
2000             ast::TyChar => return Primitive(Char),
2001             ast::TyInt(ast::TyI) => return Primitive(Int),
2002             ast::TyInt(ast::TyI8) => return Primitive(I8),
2003             ast::TyInt(ast::TyI16) => return Primitive(I16),
2004             ast::TyInt(ast::TyI32) => return Primitive(I32),
2005             ast::TyInt(ast::TyI64) => return Primitive(I64),
2006             ast::TyUint(ast::TyU) => return Primitive(Uint),
2007             ast::TyUint(ast::TyU8) => return Primitive(U8),
2008             ast::TyUint(ast::TyU16) => return Primitive(U16),
2009             ast::TyUint(ast::TyU32) => return Primitive(U32),
2010             ast::TyUint(ast::TyU64) => return Primitive(U64),
2011             ast::TyFloat(ast::TyF32) => return Primitive(F32),
2012             ast::TyFloat(ast::TyF64) => return Primitive(F64),
2013         },
2014         def::DefTyParam(_, i, _) => return Generic(i),
2015         def::DefTyParamBinder(i) => return TyParamBinder(i),
2016         _ => {}
2017     };
2018     let did = register_def(&*cx, def);
2019     ResolvedPath { path: path, typarams: tpbs, did: did }
2020 }
2021
2022 fn register_def(cx: &core::DocContext, def: def::Def) -> ast::DefId {
2023     let (did, kind) = match def {
2024         def::DefFn(i, _) => (i, TypeFunction),
2025         def::DefTy(i) => (i, TypeEnum),
2026         def::DefTrait(i) => (i, TypeTrait),
2027         def::DefStruct(i) => (i, TypeStruct),
2028         def::DefMod(i) => (i, TypeModule),
2029         def::DefStatic(i, _) => (i, TypeStatic),
2030         def::DefVariant(i, _, _) => (i, TypeEnum),
2031         _ => return def.def_id()
2032     };
2033     if ast_util::is_local(did) { return did }
2034     let tcx = match cx.maybe_typed {
2035         core::Typed(ref t) => t,
2036         core::NotTyped(_) => return did
2037     };
2038     inline::record_extern_fqn(cx, did, kind);
2039     match kind {
2040         TypeTrait => {
2041             let t = inline::build_external_trait(tcx, did);
2042             cx.external_traits.borrow_mut().get_mut_ref().insert(did, t);
2043         }
2044         _ => {}
2045     }
2046     return did;
2047 }
2048
2049 fn resolve_use_source(path: Path, id: ast::NodeId) -> ImportSource {
2050     ImportSource {
2051         path: path,
2052         did: resolve_def(id),
2053     }
2054 }
2055
2056 fn resolve_def(id: ast::NodeId) -> Option<ast::DefId> {
2057     get_cx().tcx_opt().and_then(|tcx| {
2058         tcx.def_map.borrow().find(&id).map(|&def| register_def(&*get_cx(), def))
2059     })
2060 }
2061
2062 #[deriving(Clone, Encodable, Decodable)]
2063 pub struct Macro {
2064     pub source: String,
2065 }
2066
2067 impl Clean<Item> for doctree::Macro {
2068     fn clean(&self) -> Item {
2069         Item {
2070             name: Some(format!("{}!", self.name.clean())),
2071             attrs: self.attrs.clean(),
2072             source: self.where.clean(),
2073             visibility: ast::Public.clean(),
2074             stability: self.stab.clean(),
2075             def_id: ast_util::local_def(self.id),
2076             inner: MacroItem(Macro {
2077                 source: self.where.to_src(),
2078             }),
2079         }
2080     }
2081 }
2082
2083 #[deriving(Clone, Encodable, Decodable)]
2084 pub struct Stability {
2085     pub level: attr::StabilityLevel,
2086     pub text: String
2087 }
2088
2089 impl Clean<Stability> for attr::Stability {
2090     fn clean(&self) -> Stability {
2091         Stability {
2092             level: self.level,
2093             text: self.text.as_ref().map_or("".to_string(),
2094                                             |interned| interned.get().to_string()),
2095         }
2096     }
2097 }