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.
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.
11 //! This module contains the "cleaned" pieces of the AST, and the functions
18 use syntax::attr::{AttributeMethods, AttrMetaMethods};
19 use syntax::codemap::Pos;
20 use syntax::parse::token::InternedString;
21 use syntax::parse::token;
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;
36 use std::gc::{Gc, GC};
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";
48 // load the current DocContext from TLD
49 fn get_cx() -> Gc<core::DocContext> {
50 *super::ctxtkey.get().unwrap()
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())
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()
69 impl<T: Clean<U>, U> Clean<VecPerParamSpace<U>> for VecPerParamSpace<T> {
70 fn clean(&self) -> VecPerParamSpace<U> {
71 self.map(|x| x.clean())
75 impl<T: 'static + Clean<U>, U> Clean<U> for Gc<T> {
76 fn clean(&self) -> U {
81 impl<T: Clean<U>, U> Clean<U> for Rc<T> {
82 fn clean(&self) -> U {
87 impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
88 fn clean(&self) -> Option<U> {
91 &Some(ref v) => Some(v.clean())
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()
102 #[deriving(Clone, Encodable, Decodable)]
105 pub module: Option<Item>,
106 pub externs: Vec<(ast::CrateNum, ExternalCrate)>,
107 pub primitives: Vec<Primitive>,
110 impl<'a> Clean<Crate> for visit_ast::RustdocVisitor<'a> {
111 fn clean(&self) -> Crate {
114 let mut externs = Vec::new();
115 cx.sess().cstore.iter_crate_data(|n, meta| {
116 externs.push((n, meta.clean()));
118 externs.sort_by(|&(a, _), &(b, _)| a.cmp(&b));
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,
125 self.attrs.as_slice(),
127 let id = link::find_crate_id(self.attrs.as_slice(),
128 t_outputs.out_filestem.as_slice());
130 // Clean the crate, translating the entire libsyntax AST to one that is
131 // understood by rustdoc.
132 let mut module = self.module.clean();
134 // Collect all inner modules which are tagged as implementations of
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).
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.
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();
153 let m = match module.inner {
154 ModuleItem(ref mut m) => m,
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,
163 let prim = match Primitive::find(child.attrs.as_slice()) {
167 primitives.push(prim);
169 source: Span::empty(),
170 name: Some(prim.to_url_str().to_string()),
174 def_id: ast_util::local_def(prim.to_node_id()),
175 inner: PrimitiveItem(prim),
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
182 i.visibility = Some(ast::Public);
183 i.attrs = child.attrs.clone();
187 m.items.extend(tmp.move_iter());
191 name: id.name.to_string(),
192 module: Some(module),
194 primitives: primitives,
199 #[deriving(Clone, Encodable, Decodable)]
200 pub struct ExternalCrate {
202 pub attrs: Vec<Attribute>,
203 pub primitives: Vec<Primitive>,
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,
213 let did = match def {
214 decoder::DlDef(def::DefMod(did)) => did,
217 let attrs = inline::load_attrs(tcx, did);
218 Primitive::find(attrs.as_slice()).map(|prim| primitives.push(prim));
222 name: self.name.to_string(),
223 attrs: decoder::get_crate_attributes(self.data()).clean(),
224 primitives: primitives,
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)]
236 /// Not everything has a name. E.g., impls
237 pub name: Option<String>,
238 pub attrs: Vec<Attribute> ,
240 pub visibility: Option<Visibility>,
241 pub def_id: ast::DefId,
242 pub stability: Option<Stability>,
246 /// Finds the `doc` attribute as a List and returns the list of attributes
248 pub fn doc_list<'a>(&'a self) -> Option<&'a [Attribute]> {
249 for attr in self.attrs.iter() {
251 List(ref x, ref list) if "doc" == x.as_slice() => {
252 return Some(list.as_slice());
260 /// Finds the `doc` attribute as a NameValue and returns the corresponding
262 pub fn doc_value<'a>(&'a self) -> Option<&'a str> {
263 for attr in self.attrs.iter() {
265 NameValue(ref x, ref v) if "doc" == x.as_slice() => {
266 return Some(v.as_slice());
274 pub fn is_hidden_from_doc(&self) -> bool {
275 match self.doc_list() {
277 for innerattr in l.iter() {
279 Word(ref s) if "hidden" == s.as_slice() => {
291 pub fn is_mod(&self) -> bool {
292 match self.inner { ModuleItem(..) => true, _ => false }
294 pub fn is_trait(&self) -> bool {
295 match self.inner { TraitItem(..) => true, _ => false }
297 pub fn is_struct(&self) -> bool {
298 match self.inner { StructItem(..) => true, _ => false }
300 pub fn is_enum(&self) -> bool {
301 match self.inner { EnumItem(..) => true, _ => false }
303 pub fn is_fn(&self) -> bool {
304 match self.inner { FunctionItem(..) => true, _ => false }
308 #[deriving(Clone, Encodable, Decodable)]
312 FunctionItem(Function),
314 TypedefItem(Typedef),
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.
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),
332 PrimitiveItem(Primitive),
335 #[deriving(Clone, Encodable, Decodable)]
337 pub items: Vec<Item>,
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()
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)
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(),
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()
369 // determine if we should display the inner contents or
370 // the outer `mod` item for the source code.
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 {
380 // mod foo; (and a separate FileMap for the contents)
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,
395 .flat_map(|x| x.iter().map(|x| (*x).clone()))
402 #[deriving(Clone, Encodable, Decodable)]
405 List(String, Vec<Attribute> ),
406 NameValue(String, String)
409 impl Clean<Attribute> for ast::MetaItem {
410 fn clean(&self) -> Attribute {
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())
416 ast::MetaNameValue(ref s, ref v) => {
417 NameValue(s.get().to_string(), lit_to_str(v))
423 impl Clean<Attribute> for ast::Attribute {
424 fn clean(&self) -> Attribute {
425 self.desugar_doc().node.value.clean()
429 // This is a rough approximation that gets us what we want.
430 impl attr::AttrMetaMethods for Attribute {
431 fn name(&self) -> InternedString {
433 Word(ref n) | List(ref n, _) | NameValue(ref n, _) => {
434 token::intern_and_get_ident(n.as_slice())
439 fn value_str(&self) -> Option<InternedString> {
441 NameValue(_, ref v) => {
442 Some(token::intern_and_get_ident(v.as_slice()))
447 fn meta_item_list<'a>(&'a self) -> Option<&'a [Gc<ast::MetaItem>]> { None }
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 }
455 #[deriving(Clone, Encodable, Decodable)]
459 pub bounds: Vec<TyParamBound>,
460 pub default: Option<Type>
463 impl Clean<TyParam> for ast::TyParam {
464 fn clean(&self) -> 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()
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());
479 name: self.ident.clean(),
481 bounds: self.bounds.clean(),
482 default: self.default.clean()
487 #[deriving(Clone, Encodable, Decodable)]
488 pub enum TyParamBound {
493 impl Clean<TyParamBound> for ast::TyParamBound {
494 fn clean(&self) -> TyParamBound {
496 ast::StaticRegionTyParamBound => RegionBound,
497 ast::OtherRegionTyParamBound(_) => RegionBound,
498 ast::UnboxedFnTyParamBound(_) => {
499 // FIXME(pcwalton): Wrong.
502 ast::TraitTyParamBound(ref t) => TraitBound(t.clean()),
507 fn external_path(name: &str, substs: &subst::Substs) -> Path {
508 let lifetimes = substs.regions().get_vec(subst::TypeSpace)
510 .filter_map(|v| v.clean())
512 let types = substs.types.get_vec(subst::TypeSpace).clean();
515 segments: vec![PathSegment {
516 name: name.to_string(),
517 lifetimes: lifetimes,
523 impl Clean<TyParamBound> for ty::BuiltinBound {
524 fn clean(&self) -> TyParamBound {
526 let tcx = match cx.maybe_typed {
527 core::Typed(ref tcx) => tcx,
528 core::NotTyped(_) => return RegionBound,
530 let empty = subst::Substs::empty();
531 let (did, path) = match *self {
532 ty::BoundStatic => return RegionBound,
534 (tcx.lang_items.send_trait().unwrap(),
535 external_path("Send", &empty)),
537 (tcx.lang_items.sized_trait().unwrap(),
538 external_path("Sized", &empty)),
540 (tcx.lang_items.copy_trait().unwrap(),
541 external_path("Copy", &empty)),
543 (tcx.lang_items.share_trait().unwrap(),
544 external_path("Share", &empty)),
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,
550 TraitBound(ResolvedPath {
558 impl Clean<TyParamBound> for ty::TraitRef {
559 fn clean(&self) -> TyParamBound {
561 let tcx = match cx.maybe_typed {
562 core::Typed(ref tcx) => tcx,
563 core::NotTyped(_) => return RegionBound,
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(),
570 cx.external_paths.borrow_mut().get_mut_ref().insert(self.def_id,
572 TraitBound(ResolvedPath {
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 {
588 for t in self.trait_bounds.iter() {
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}
604 #[deriving(Clone, Encodable, Decodable, PartialEq)]
605 pub struct Lifetime(String);
608 pub fn get_ref<'a>(&'a self) -> &'a str {
609 let Lifetime(ref s) = *self;
610 let s: &'a str = s.as_slice();
615 impl Clean<Lifetime> for ast::Lifetime {
616 fn clean(&self) -> Lifetime {
617 Lifetime(token::get_name(self.name).get().to_string())
621 impl Clean<Lifetime> for ty::RegionParameterDef {
622 fn clean(&self) -> Lifetime {
623 Lifetime(token::get_name(self.name).get().to_string())
627 impl Clean<Option<Lifetime>> for ty::Region {
628 fn clean(&self) -> Option<Lifetime> {
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())),
635 ty::ReLateBound(..) |
639 ty::ReEmpty(..) => None
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>,
651 impl Clean<Generics> for ast::Generics {
652 fn clean(&self) -> Generics {
654 lifetimes: self.lifetimes.clean(),
655 type_params: self.ty_params.clean(),
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
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
677 if !self.types.get_vec(subst::FnSpace).is_empty() ||
678 !self.regions.get_vec(subst::FnSpace).is_empty()
687 type_params: self.types.get_vec(space).clean(),
688 lifetimes: self.regions.get_vec(space).clean(),
693 #[deriving(Clone, Encodable, Decodable)]
695 pub generics: Generics,
697 pub fn_style: ast::FnStyle,
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)
709 values: inputs.iter().map(|x| x.clean()).collect(),
711 output: (self.decl.output.clean()),
712 cf: self.decl.cf.clean(),
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(),
732 #[deriving(Clone, Encodable, Decodable)]
733 pub struct TyMethod {
734 pub fn_style: ast::FnStyle,
736 pub generics: Generics,
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)
748 values: inputs.iter().map(|x| x.clean()).collect(),
750 output: (self.decl.output.clean()),
751 cf: self.decl.cf.clean(),
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),
760 stability: get_stability(ast_util::local_def(self.id)),
761 inner: TyMethodItem(TyMethod {
762 fn_style: self.fn_style.clone(),
764 self_: self.explicit_self.node.clean(),
765 generics: self.generics.clean(),
771 #[deriving(Clone, Encodable, Decodable, PartialEq)]
775 SelfBorrowed(Option<Lifetime>, Mutability),
779 impl Clean<SelfTy> for ast::ExplicitSelf_ {
780 fn clean(&self) -> SelfTy {
782 ast::SelfStatic => SelfStatic,
783 ast::SelfValue => SelfValue,
784 ast::SelfUniq => SelfOwned,
785 ast::SelfRegion(lt, mt) => SelfBorrowed(lt.clean(), mt.clean()),
790 #[deriving(Clone, Encodable, Decodable)]
791 pub struct Function {
793 pub generics: Generics,
794 pub fn_style: ast::FnStyle,
797 impl Clean<Item> for doctree::Function {
798 fn clean(&self) -> 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,
815 #[deriving(Clone, Encodable, Decodable)]
816 pub struct ClosureDecl {
817 pub lifetimes: Vec<Lifetime>,
819 pub onceness: ast::Onceness,
820 pub fn_style: ast::FnStyle,
821 pub bounds: Vec<TyParamBound>,
824 impl Clean<ClosureDecl> for ast::ClosureTy {
825 fn clean(&self) -> 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(),
839 #[deriving(Clone, Encodable, Decodable)]
841 pub inputs: Arguments,
844 pub attrs: Vec<Attribute>,
847 #[deriving(Clone, Encodable, Decodable)]
848 pub struct Arguments {
849 pub values: Vec<Argument>,
852 impl Clean<FnDecl> for ast::FnDecl {
853 fn clean(&self) -> FnDecl {
856 values: self.inputs.iter().map(|x| x.clean()).collect(),
858 output: (self.output.clean()),
865 impl<'a> Clean<FnDecl> for (ast::DefId, &'a ty::FnSig) {
866 fn clean(&self) -> FnDecl {
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()
872 Vec::new().move_iter()
874 if names.peek().map(|s| s.as_slice()) == Some("self") {
875 let _ = names.next();
878 output: sig.output.clean(),
882 values: sig.inputs.iter().map(|t| {
886 name: names.next().unwrap_or("".to_string()),
894 #[deriving(Clone, Encodable, Decodable)]
895 pub struct Argument {
901 impl Clean<Argument> for ast::Arg {
902 fn clean(&self) -> Argument {
904 name: name_from_pat(&*self.pat),
905 type_: (self.ty.clean()),
911 #[deriving(Clone, Encodable, Decodable)]
917 impl Clean<RetStyle> for ast::RetStyle {
918 fn clean(&self) -> RetStyle {
920 ast::Return => Return,
921 ast::NoReturn => NoReturn
926 #[deriving(Clone, Encodable, Decodable)]
928 pub methods: Vec<TraitMethod>,
929 pub generics: Generics,
930 pub parents: Vec<Type>,
933 impl Clean<Item> for doctree::Trait {
934 fn clean(&self) -> 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(),
951 impl Clean<Type> for ast::TraitRef {
952 fn clean(&self) -> Type {
953 resolve_type(self.path.clean(), None, self.ref_id)
957 #[deriving(Clone, Encodable, Decodable)]
958 pub enum TraitMethod {
964 pub fn is_req(&self) -> bool {
966 &Required(..) => true,
970 pub fn is_def(&self) -> bool {
972 &Provided(..) => true,
976 pub fn item<'a>(&'a self) -> &'a Item {
978 Required(ref item) => item,
979 Provided(ref item) => item,
984 impl Clean<TraitMethod> for ast::TraitMethod {
985 fn clean(&self) -> TraitMethod {
987 &ast::Required(ref t) => Required(t.clean()),
988 &ast::Provided(ref t) => Provided(t.clean()),
993 impl Clean<Item> for ty::Method {
994 fn clean(&self) -> Item {
996 let (self_, sig) = match self.explicit_self {
997 ast::SelfStatic => (ast::SelfStatic.clean(), self.fty.sig.clone()),
999 let sig = ty::FnSig {
1000 inputs: Vec::from_slice(self.fty.sig.inputs.slice_from(1)),
1001 ..self.fty.sig.clone()
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())
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(),
1029 decl: (self.def_id, &sig).clean(),
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)]
1040 /// structs/enums/traits (anything that'd be an ast::TyPath)
1043 pub typarams: Option<Vec<TyParamBound>>,
1044 pub did: ast::DefId,
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
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>),
1058 BareFunction(Box<BareFunctionDecl>),
1061 FixedVector(Box<Type>, String),
1066 RawPointer(Mutability, Box<Type>),
1068 pub lifetime: Option<Lifetime>,
1069 pub mutability: Mutability,
1070 pub type_: Box<Type>,
1072 // region, raw, other boxes, mutable
1075 #[deriving(Clone, Encodable, Decodable, PartialEq, Eq, Hash)]
1076 pub enum Primitive {
1077 Int, I8, I16, I32, I64,
1078 Uint, U8, U16, U32, U64,
1088 #[deriving(Clone, Encodable, Decodable)]
1100 fn from_str(s: &str) -> Option<Primitive> {
1101 match s.as_slice() {
1107 "uint" => Some(Uint),
1112 "bool" => Some(Bool),
1114 "char" => Some(Char),
1118 "slice" => Some(Slice),
1119 "tuple" => Some(PrimitiveTuple),
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,
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(),
1136 match Primitive::from_str(value) {
1137 Some(p) => return Some(p),
1145 pub fn to_str(&self) -> &'static str {
1164 PrimitiveTuple => "tuple",
1168 pub fn to_url_str(&self) -> &'static str {
1171 other => other.to_str(),
1175 /// Creates a rustdoc-specific node id for primitive types.
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)
1183 impl Clean<Type> for ast::Ty {
1184 fn clean(&self) -> Type {
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(),
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()),
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(),
1208 ref x => fail!("Unimplemented type {:?}", x),
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(),
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(),
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()
1249 decl: (ast_util::local_def(0), &fty.sig).clean(),
1250 abi: fty.abi.to_str(),
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(),
1261 ty::UniqTraitStore => Proc(decl),
1262 ty::RegionTraitStore(ref r, _) => Closure(decl, r.clean()),
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| {
1272 let kind = match ty::get(*self).sty {
1273 ty::ty_struct(..) => TypeStruct,
1274 ty::ty_trait(..) => TypeTrait,
1277 let path = external_path(fqn.last().unwrap().to_str().as_slice(),
1279 get_cx().external_paths.borrow_mut().get_mut_ref()
1280 .insert(did, (fqn, kind));
1287 ty::ty_tup(ref t) => Tuple(t.iter().map(|t| t.clean()).collect()),
1289 ty::ty_param(ref p) => {
1290 if p.space == subst::SelfSpace {
1297 ty::ty_infer(..) => fail!("ty_infer"),
1298 ty::ty_err => fail!("ty_err"),
1303 #[deriving(Clone, Encodable, Decodable)]
1304 pub enum StructField {
1305 HiddenStructField, // inserted later by strip passes
1306 TypedStructField(Type),
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)
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())),
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 {
1336 let ty = ty::lookup_item_type(cx.tcx(), self.id);
1339 attrs: inline::load_attrs(cx.tcx(), self.id),
1340 source: Span::empty(),
1341 visibility: Some(self.vis),
1342 stability: get_stability(self.id),
1344 inner: StructFieldItem(TypedStructField(ty.ty.clean())),
1349 pub type Visibility = ast::Visibility;
1351 impl Clean<Option<Visibility>> for ast::Visibility {
1352 fn clean(&self) -> Option<Visibility> {
1357 #[deriving(Clone, Encodable, Decodable)]
1359 pub struct_type: doctree::StructType,
1360 pub generics: Generics,
1361 pub fields: Vec<Item>,
1362 pub fields_stripped: bool,
1365 impl Clean<Item> for doctree::Struct {
1366 fn clean(&self) -> 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,
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,
1394 impl Clean<VariantStruct> for syntax::ast::StructDef {
1395 fn clean(&self) -> VariantStruct {
1397 struct_type: doctree::struct_type_from_def(self),
1398 fields: self.fields.clean().move_iter().collect(),
1399 fields_stripped: false,
1404 #[deriving(Clone, Encodable, Decodable)]
1406 pub variants: Vec<Item>,
1407 pub generics: Generics,
1408 pub variants_stripped: bool,
1411 impl Clean<Item> for doctree::Enum {
1412 fn clean(&self) -> 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,
1429 #[deriving(Clone, Encodable, Decodable)]
1430 pub struct Variant {
1431 pub kind: VariantKind,
1434 impl Clean<Item> for doctree::Variant {
1435 fn clean(&self) -> 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(),
1450 impl Clean<Item> for ty::VariantInfo {
1451 fn clean(&self) -> Item {
1452 // use syntax::parse::token::special_idents::unnamed_field;
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())
1460 StructVariant(VariantStruct {
1461 struct_type: doctree::Plain,
1462 fields_stripped: false,
1463 fields: s.iter().zip(self.args.iter()).map(|(name, ty)| {
1465 source: Span::empty(),
1466 name: Some(name.clean()),
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.
1475 inner: StructFieldItem(
1476 TypedStructField(ty.clean())
1484 name: Some(self.name.clean()),
1485 attrs: inline::load_attrs(cx.tcx(), self.id),
1486 source: Span::empty(),
1487 visibility: Some(ast::Public),
1489 inner: VariantItem(Variant { kind: kind }),
1495 #[deriving(Clone, Encodable, Decodable)]
1496 pub enum VariantKind {
1498 TupleVariant(Vec<Type>),
1499 StructVariant(VariantStruct),
1502 impl Clean<VariantKind> for ast::VariantKind {
1503 fn clean(&self) -> VariantKind {
1505 &ast::TupleVariantKind(ref args) => {
1506 if args.len() == 0 {
1509 TupleVariant(args.iter().map(|x| x.ty.clean()).collect())
1512 &ast::StructVariantKind(ref sd) => StructVariant(sd.clean()),
1517 #[deriving(Clone, Encodable, Decodable)]
1519 pub filename: String,
1527 fn empty() -> Span {
1529 filename: "".to_string(),
1530 loline: 0, locol: 0,
1531 hiline: 0, hicol: 0,
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);
1544 filename: filename.to_string(),
1546 locol: lo.col.to_uint(),
1548 hicol: hi.col.to_uint(),
1553 #[deriving(Clone, Encodable, Decodable)]
1556 pub segments: Vec<PathSegment>,
1559 impl Clean<Path> for ast::Path {
1560 fn clean(&self) -> Path {
1562 global: self.global,
1563 segments: self.segments.clean().move_iter().collect(),
1568 #[deriving(Clone, Encodable, Decodable)]
1569 pub struct PathSegment {
1571 pub lifetimes: Vec<Lifetime>,
1572 pub types: Vec<Type>,
1575 impl Clean<PathSegment> for ast::PathSegment {
1576 fn clean(&self) -> PathSegment {
1578 name: self.identifier.clean(),
1579 lifetimes: self.lifetimes.clean().move_iter().collect(),
1580 types: self.types.clean().move_iter().collect()
1585 fn path_to_str(p: &ast::Path) -> String {
1586 use syntax::parse::token;
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 {
1596 s.push_str(i.get());
1601 impl Clean<String> for ast::Ident {
1602 fn clean(&self) -> String {
1603 token::get_ident(*self).get().to_string()
1607 impl Clean<String> for ast::Name {
1608 fn clean(&self) -> String {
1609 token::get_name(*self).get().to_string()
1613 #[deriving(Clone, Encodable, Decodable)]
1614 pub struct Typedef {
1616 pub generics: Generics,
1619 impl Clean<Item> for doctree::Typedef {
1620 fn clean(&self) -> 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(),
1636 #[deriving(Clone, Encodable, Decodable)]
1637 pub struct BareFunctionDecl {
1638 pub fn_style: ast::FnStyle,
1639 pub generics: Generics,
1644 impl Clean<BareFunctionDecl> for ast::BareFnTy {
1645 fn clean(&self) -> BareFunctionDecl {
1647 fn_style: self.fn_style,
1648 generics: Generics {
1649 lifetimes: self.lifetimes.clean().move_iter().collect(),
1650 type_params: Vec::new(),
1652 decl: self.decl.clean(),
1653 abi: self.abi.to_str(),
1658 #[deriving(Clone, Encodable, Decodable)]
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.
1668 impl Clean<Item> for doctree::Static {
1669 fn clean(&self) -> Item {
1670 debug!("claning static {}: {:?}", self.name.clean(), self);
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(),
1687 #[deriving(Show, Clone, Encodable, Decodable, PartialEq)]
1688 pub enum Mutability {
1693 impl Clean<Mutability> for ast::Mutability {
1694 fn clean(&self) -> Mutability {
1696 &ast::MutMutable => Mutable,
1697 &ast::MutImmutable => Immutable,
1702 #[deriving(Clone, Encodable, Decodable)]
1704 pub generics: Generics,
1705 pub trait_: Option<Type>,
1707 pub methods: Vec<Item>,
1711 fn detect_derived<M: AttrMetaMethods>(attrs: &[M]) -> bool {
1712 attr::contains_name(attrs, "automatically_derived")
1715 impl Clean<Item> for doctree::Impl {
1716 fn clean(&self) -> Item {
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()),
1735 #[deriving(Clone, Encodable, Decodable)]
1736 pub struct ViewItem {
1737 pub inner: ViewItemInner,
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"),
1751 let convert = |node: &ast::ViewItem_| {
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(),
1759 inner: ViewItemItem(ViewItem { inner: node.clean() }),
1762 let mut ret = Vec::new();
1764 ast::ViewItemUse(ref path) if !denied => {
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) {
1774 ret.extend(items.move_iter()); false
1778 }).map(|a| a.clone()).collect::<Vec<ast::PathListIdent>>();
1779 if remaining.len() > 0 {
1780 let path = ast::ViewPathList(a.clone(),
1783 let path = syntax::codemap::dummy_spanned(path);
1784 ret.push(convert(&ast::ViewItemUse(box(GC) path)));
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)),
1795 ref n => ret.push(convert(n)),
1801 #[deriving(Clone, Encodable, Decodable)]
1802 pub enum ViewItemInner {
1803 ExternCrate(String, Option<String>, ast::NodeId),
1807 impl Clean<ViewItemInner> for ast::ViewItem_ {
1808 fn clean(&self) -> ViewItemInner {
1810 &ast::ViewItemExternCrate(ref i, ref p, ref id) => {
1811 let string = match *p {
1813 Some((ref x, _)) => Some(x.get().to_string()),
1815 ExternCrate(i.clean(), string, *id)
1817 &ast::ViewItemUse(ref vp) => {
1824 #[deriving(Clone, Encodable, Decodable)]
1826 // use str = source;
1827 SimpleImport(String, ImportSource),
1829 GlobImport(ImportSource),
1830 // use source::{a, b, c};
1831 ImportList(ImportSource, Vec<ViewListIdent>),
1834 #[deriving(Clone, Encodable, Decodable)]
1835 pub struct ImportSource {
1837 pub did: Option<ast::DefId>,
1840 impl Clean<ViewPath> for ast::ViewPath {
1841 fn clean(&self) -> ViewPath {
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())
1855 #[deriving(Clone, Encodable, Decodable)]
1856 pub struct ViewListIdent {
1858 pub source: Option<ast::DefId>,
1861 impl Clean<ViewListIdent> for ast::PathListIdent {
1862 fn clean(&self) -> ViewListIdent {
1864 name: self.node.name.clean(),
1865 source: resolve_def(self.node.id),
1870 impl Clean<Vec<Item>> for ast::ForeignMod {
1871 fn clean(&self) -> Vec<Item> {
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 {
1882 generics: generics.clean(),
1883 fn_style: ast::UnsafeFn,
1886 ast::ForeignItemStatic(ref ty, mutbl) => {
1887 ForeignStaticItem(Static {
1889 mutability: if mutbl {Mutable} else {Immutable},
1890 expr: "".to_string(),
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(),
1909 fn to_src(&self) -> String;
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()
1921 debug!("got snippet {}", sn);
1926 fn lit_to_str(lit: &ast::Lit) -> String {
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| {
1935 res.push_char('\'');
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(),
1949 fn name_from_pat(p: &ast::Pat) -> String {
1951 debug!("Trying to get a name from pattern: {:?}", p);
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),
1964 warn!("tried to get argument name from PatLit, \
1965 which is silly in function arguments");
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"),
1973 warn!("can't document the name of a function argument \
1974 produced by a pattern macro");
1975 "(argument produced by macro)".to_string()
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 {
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),
1989 debug!("searching for {:?} in defmap", id);
1990 let def = match tycx.def_map.borrow().find(&id) {
1992 None => fail!("unresolved id not in defmap")
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),
2014 def::DefTyParam(_, i, _) => return Generic(i),
2015 def::DefTyParamBinder(i) => return TyParamBinder(i),
2018 let did = register_def(&*cx, def);
2019 ResolvedPath { path: path, typarams: tpbs, did: did }
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()
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
2038 inline::record_extern_fqn(cx, did, kind);
2041 let t = inline::build_external_trait(tcx, did);
2042 cx.external_traits.borrow_mut().get_mut_ref().insert(did, t);
2049 fn resolve_use_source(path: Path, id: ast::NodeId) -> ImportSource {
2052 did: resolve_def(id),
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))
2062 #[deriving(Clone, Encodable, Decodable)]
2067 impl Clean<Item> for doctree::Macro {
2068 fn clean(&self) -> 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(),
2083 #[deriving(Clone, Encodable, Decodable)]
2084 pub struct Stability {
2085 pub level: attr::StabilityLevel,
2089 impl Clean<Stability> for attr::Stability {
2090 fn clean(&self) -> Stability {
2093 text: self.text.as_ref().map_or("".to_string(),
2094 |interned| interned.get().to_string()),