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;
19 use syntax::codemap::Pos;
20 use syntax::parse::token::InternedString;
21 use syntax::parse::token;
23 use rustc::metadata::cstore;
24 use rustc::metadata::csearch;
25 use rustc::metadata::decoder;
38 impl<T: Clean<U>, U> Clean<Vec<U>> for Vec<T> {
39 fn clean(&self) -> Vec<U> {
40 self.iter().map(|x| x.clean()).collect()
44 impl<T: Clean<U>, U> Clean<U> for @T {
45 fn clean(&self) -> U {
50 impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
51 fn clean(&self) -> Option<U> {
54 &Some(ref v) => Some(v.clean())
59 impl<T: Clean<U>, U> Clean<Vec<U>> for syntax::owned_slice::OwnedSlice<T> {
60 fn clean(&self) -> Vec<U> {
61 self.iter().map(|x| x.clean()).collect()
65 #[deriving(Clone, Encodable, Decodable)]
69 externs: Vec<(ast::CrateNum, ExternalCrate)> ,
72 impl<'a> Clean<Crate> for visit_ast::RustdocVisitor<'a> {
73 fn clean(&self) -> Crate {
74 use syntax::attr::find_crateid;
75 let cx = local_data::get(super::ctxtkey, |x| *x.unwrap());
77 let mut externs = Vec::new();
78 cx.sess().cstore.iter_crate_data(|n, meta| {
79 externs.push((n, meta.clean()));
83 name: match find_crateid(self.attrs.as_slice()) {
85 None => fail!("rustdoc requires a `crate_id` crate attribute"),
87 module: Some(self.module.clean()),
93 #[deriving(Clone, Encodable, Decodable)]
94 pub struct ExternalCrate {
96 attrs: Vec<Attribute> ,
99 impl Clean<ExternalCrate> for cstore::crate_metadata {
100 fn clean(&self) -> ExternalCrate {
102 name: self.name.to_owned(),
103 attrs: decoder::get_crate_attributes(self.data()).clean()
110 /// Anything with a source location and set of attributes and, optionally, a
111 /// name. That is, anything that can be documented. This doesn't correspond
112 /// directly to the AST's concept of an item; it's a strict superset.
113 #[deriving(Clone, Encodable, Decodable)]
117 /// Not everything has a name. E.g., impls
119 attrs: Vec<Attribute> ,
121 visibility: Option<Visibility>,
126 /// Finds the `doc` attribute as a List and returns the list of attributes
128 pub fn doc_list<'a>(&'a self) -> Option<&'a [Attribute]> {
129 for attr in self.attrs.iter() {
131 List(ref x, ref list) if "doc" == *x => { return Some(list.as_slice()); }
138 /// Finds the `doc` attribute as a NameValue and returns the corresponding
140 pub fn doc_value<'a>(&'a self) -> Option<&'a str> {
141 for attr in self.attrs.iter() {
143 NameValue(ref x, ref v) if "doc" == *x => { return Some(v.as_slice()); }
150 pub fn is_mod(&self) -> bool {
151 match self.inner { ModuleItem(..) => true, _ => false }
153 pub fn is_trait(&self) -> bool {
154 match self.inner { TraitItem(..) => true, _ => false }
156 pub fn is_struct(&self) -> bool {
157 match self.inner { StructItem(..) => true, _ => false }
159 pub fn is_enum(&self) -> bool {
160 match self.inner { EnumItem(..) => true, _ => false }
162 pub fn is_fn(&self) -> bool {
163 match self.inner { FunctionItem(..) => true, _ => false }
167 #[deriving(Clone, Encodable, Decodable)]
171 FunctionItem(Function),
173 TypedefItem(Typedef),
177 /// `use` and `extern crate`
178 ViewItemItem(ViewItem),
179 /// A method signature only. Used for required methods in traits (ie,
180 /// non-default-methods).
181 TyMethodItem(TyMethod),
182 /// A method with a body.
184 StructFieldItem(StructField),
185 VariantItem(Variant),
186 /// `fn`s from an extern block
187 ForeignFunctionItem(Function),
188 /// `static`s from an extern block
189 ForeignStaticItem(Static),
193 #[deriving(Clone, Encodable, Decodable)]
199 impl Clean<Item> for doctree::Module {
200 fn clean(&self) -> Item {
201 let name = if self.name.is_some() {
202 self.name.unwrap().clean()
206 let mut foreigns = Vec::new();
207 for subforeigns in self.foreigns.clean().move_iter() {
208 for foreign in subforeigns.move_iter() {
209 foreigns.push(foreign)
212 let items: Vec<Vec<Item> > = vec!(
213 self.structs.clean().move_iter().collect(),
214 self.enums.clean().move_iter().collect(),
215 self.fns.clean().move_iter().collect(),
217 self.mods.clean().move_iter().collect(),
218 self.typedefs.clean().move_iter().collect(),
219 self.statics.clean().move_iter().collect(),
220 self.traits.clean().move_iter().collect(),
221 self.impls.clean().move_iter().collect(),
222 self.view_items.clean().move_iter().collect(),
223 self.macros.clean().move_iter().collect()
227 attrs: self.attrs.clean(),
228 source: self.where.clean(),
229 visibility: self.vis.clean(),
231 inner: ModuleItem(Module {
232 is_crate: self.is_crate,
234 .flat_map(|x| x.iter().map(|x| (*x).clone()))
241 #[deriving(Clone, Encodable, Decodable)]
244 List(~str, Vec<Attribute> ),
245 NameValue(~str, ~str)
248 impl Clean<Attribute> for ast::MetaItem {
249 fn clean(&self) -> Attribute {
251 ast::MetaWord(ref s) => Word(s.get().to_owned()),
252 ast::MetaList(ref s, ref l) => {
253 List(s.get().to_owned(), l.clean().move_iter().collect())
255 ast::MetaNameValue(ref s, ref v) => {
256 NameValue(s.get().to_owned(), lit_to_str(v))
262 impl Clean<Attribute> for ast::Attribute {
263 fn clean(&self) -> Attribute {
264 self.desugar_doc().node.value.clean()
268 // This is a rough approximation that gets us what we want.
269 impl<'a> attr::AttrMetaMethods for &'a Attribute {
270 fn name(&self) -> InternedString {
272 Word(ref n) | List(ref n, _) | NameValue(ref n, _) => {
273 token::intern_and_get_ident(*n)
278 fn value_str(&self) -> Option<InternedString> {
280 NameValue(_, ref v) => Some(token::intern_and_get_ident(*v)),
284 fn meta_item_list<'a>(&'a self) -> Option<&'a [@ast::MetaItem]> { None }
285 fn name_str_pair(&self) -> Option<(InternedString, InternedString)> {
290 #[deriving(Clone, Encodable, Decodable)]
294 bounds: Vec<TyParamBound> }
296 impl Clean<TyParam> for ast::TyParam {
297 fn clean(&self) -> TyParam {
299 name: self.ident.clean(),
301 bounds: self.bounds.clean().move_iter().collect(),
306 #[deriving(Clone, Encodable, Decodable)]
307 pub enum TyParamBound {
312 impl Clean<TyParamBound> for ast::TyParamBound {
313 fn clean(&self) -> TyParamBound {
315 ast::RegionTyParamBound => RegionBound,
316 ast::TraitTyParamBound(ref t) => TraitBound(t.clean()),
321 #[deriving(Clone, Encodable, Decodable)]
322 pub struct Lifetime(~str);
325 pub fn get_ref<'a>(&'a self) -> &'a str {
326 let Lifetime(ref s) = *self;
332 impl Clean<Lifetime> for ast::Lifetime {
333 fn clean(&self) -> Lifetime {
334 Lifetime(token::get_name(self.name).get().to_owned())
338 // maybe use a Generic enum and use ~[Generic]?
339 #[deriving(Clone, Encodable, Decodable)]
340 pub struct Generics {
341 lifetimes: Vec<Lifetime> ,
342 type_params: Vec<TyParam> }
344 impl Clean<Generics> for ast::Generics {
345 fn clean(&self) -> Generics {
347 lifetimes: self.lifetimes.clean().move_iter().collect(),
348 type_params: self.ty_params.clean().move_iter().collect(),
353 #[deriving(Clone, Encodable, Decodable)]
361 impl Clean<Item> for ast::Method {
362 fn clean(&self) -> Item {
363 let inputs = match self.explicit_self.node {
364 ast::SelfStatic => self.decl.inputs.as_slice(),
365 _ => self.decl.inputs.slice_from(1)
369 values: inputs.iter().map(|x| x.clean()).collect(),
371 output: (self.decl.output.clean()),
372 cf: self.decl.cf.clean(),
376 name: Some(self.ident.clean()),
377 attrs: self.attrs.clean().move_iter().collect(),
378 source: self.span.clean(),
380 visibility: self.vis.clean(),
381 inner: MethodItem(Method {
382 generics: self.generics.clean(),
383 self_: self.explicit_self.clean(),
384 purity: self.purity.clone(),
391 #[deriving(Clone, Encodable, Decodable)]
392 pub struct TyMethod {
399 impl Clean<Item> for ast::TypeMethod {
400 fn clean(&self) -> Item {
401 let inputs = match self.explicit_self.node {
402 ast::SelfStatic => self.decl.inputs.as_slice(),
403 _ => self.decl.inputs.slice_from(1)
407 values: inputs.iter().map(|x| x.clean()).collect(),
409 output: (self.decl.output.clean()),
410 cf: self.decl.cf.clean(),
414 name: Some(self.ident.clean()),
415 attrs: self.attrs.clean().move_iter().collect(),
416 source: self.span.clean(),
419 inner: TyMethodItem(TyMethod {
420 purity: self.purity.clone(),
422 self_: self.explicit_self.clean(),
423 generics: self.generics.clean(),
429 #[deriving(Clone, Encodable, Decodable)]
433 SelfBorrowed(Option<Lifetime>, Mutability),
437 impl Clean<SelfTy> for ast::ExplicitSelf {
438 fn clean(&self) -> SelfTy {
440 ast::SelfStatic => SelfStatic,
441 ast::SelfValue => SelfValue,
442 ast::SelfUniq => SelfOwned,
443 ast::SelfRegion(lt, mt) => SelfBorrowed(lt.clean(), mt.clean()),
448 #[deriving(Clone, Encodable, Decodable)]
449 pub struct Function {
455 impl Clean<Item> for doctree::Function {
456 fn clean(&self) -> Item {
458 name: Some(self.name.clean()),
459 attrs: self.attrs.clean(),
460 source: self.where.clean(),
461 visibility: self.vis.clean(),
463 inner: FunctionItem(Function {
464 decl: self.decl.clean(),
465 generics: self.generics.clean(),
472 #[deriving(Clone, Encodable, Decodable)]
473 pub struct ClosureDecl {
475 region: Option<Lifetime>,
476 lifetimes: Vec<Lifetime> ,
478 onceness: ast::Onceness,
480 bounds: Vec<TyParamBound> }
482 impl Clean<ClosureDecl> for ast::ClosureTy {
483 fn clean(&self) -> ClosureDecl {
486 region: self.region.clean(),
487 lifetimes: self.lifetimes.clean().move_iter().collect(),
488 decl: self.decl.clean(),
489 onceness: self.onceness,
491 bounds: match self.bounds {
492 Some(ref x) => x.clean().move_iter().collect(),
499 #[deriving(Clone, Encodable, Decodable)]
504 attrs: Vec<Attribute> }
506 #[deriving(Clone, Encodable, Decodable)]
507 pub struct Arguments {
508 values: Vec<Argument> ,
511 impl Clean<FnDecl> for ast::FnDecl {
512 fn clean(&self) -> FnDecl {
515 values: self.inputs.iter().map(|x| x.clean()).collect(),
517 output: (self.output.clean()),
524 #[deriving(Clone, Encodable, Decodable)]
525 pub struct Argument {
531 impl Clean<Argument> for ast::Arg {
532 fn clean(&self) -> Argument {
534 name: name_from_pat(self.pat),
535 type_: (self.ty.clean()),
541 #[deriving(Clone, Encodable, Decodable)]
547 impl Clean<RetStyle> for ast::RetStyle {
548 fn clean(&self) -> RetStyle {
550 ast::Return => Return,
551 ast::NoReturn => NoReturn
556 #[deriving(Clone, Encodable, Decodable)]
558 methods: Vec<TraitMethod> ,
563 impl Clean<Item> for doctree::Trait {
564 fn clean(&self) -> Item {
566 name: Some(self.name.clean()),
567 attrs: self.attrs.clean(),
568 source: self.where.clean(),
570 visibility: self.vis.clean(),
571 inner: TraitItem(Trait {
572 methods: self.methods.clean(),
573 generics: self.generics.clean(),
574 parents: self.parents.clean(),
580 impl Clean<Type> for ast::TraitRef {
581 fn clean(&self) -> Type {
582 resolve_type(self.path.clean(), None, self.ref_id)
586 #[deriving(Clone, Encodable, Decodable)]
587 pub enum TraitMethod {
593 pub fn is_req(&self) -> bool {
595 &Required(..) => true,
599 pub fn is_def(&self) -> bool {
601 &Provided(..) => true,
605 pub fn item<'a>(&'a self) -> &'a Item {
607 Required(ref item) => item,
608 Provided(ref item) => item,
613 impl Clean<TraitMethod> for ast::TraitMethod {
614 fn clean(&self) -> TraitMethod {
616 &ast::Required(ref t) => Required(t.clean()),
617 &ast::Provided(ref t) => Provided(t.clean()),
622 /// A representation of a Type suitable for hyperlinking purposes. Ideally one can get the original
623 /// type out of the AST/ty::ctxt given one of these, if more information is needed. Most importantly
624 /// it does not preserve mutability or boxes.
625 #[deriving(Clone, Encodable, Decodable)]
627 /// structs/enums/traits (anything that'd be an ast::TyPath)
630 typarams: Option<Vec<TyParamBound> >,
633 /// Same as above, but only external variants
636 typarams: Option<Vec<TyParamBound> >,
639 krate: ast::CrateNum,
641 // I have no idea how to usefully use this.
642 TyParamBinder(ast::NodeId),
643 /// For parameterized types, so the consumer of the JSON don't go looking
644 /// for types which don't exist anywhere.
645 Generic(ast::NodeId),
646 /// For references to self
648 /// Primitives are just the fixed-size numeric types (plus int/uint/float), and char.
649 Primitive(ast::PrimTy),
650 Closure(~ClosureDecl),
652 BareFunction(~BareFunctionDecl),
655 FixedVector(~Type, ~str),
664 RawPointer(Mutability, ~Type),
665 BorrowedRef { lifetime: Option<Lifetime>, mutability: Mutability, type_: ~Type},
666 // region, raw, other boxes, mutable
669 #[deriving(Clone, Encodable, Decodable)]
677 impl Clean<Type> for ast::Ty {
678 fn clean(&self) -> Type {
680 debug!("cleaning type `{:?}`", self);
681 let codemap = local_data::get(super::ctxtkey, |x| *x.unwrap()).sess().codemap();
682 debug!("span corresponds to `{}`", codemap.span_to_str(self.span));
685 TyPtr(ref m) => RawPointer(m.mutbl.clean(), ~m.ty.clean()),
686 TyRptr(ref l, ref m) =>
687 BorrowedRef {lifetime: l.clean(), mutability: m.mutbl.clean(),
688 type_: ~m.ty.clean()},
689 TyBox(ty) => Managed(~ty.clean()),
690 TyUniq(ty) => Unique(~ty.clean()),
691 TyVec(ty) => Vector(~ty.clean()),
692 TyFixedLengthVec(ty, ref e) => FixedVector(~ty.clean(),
694 TyTup(ref tys) => Tuple(tys.iter().map(|x| x.clean()).collect()),
695 TyPath(ref p, ref tpbs, id) => {
696 resolve_type(p.clean(),
697 tpbs.clean().map(|x| x.move_iter().collect()),
700 TyClosure(ref c) => Closure(~c.clean()),
701 TyBareFn(ref barefn) => BareFunction(~barefn.clean()),
703 ref x => fail!("Unimplemented type {:?}", x),
708 #[deriving(Clone, Encodable, Decodable)]
709 pub struct StructField {
713 impl Clean<Item> for ast::StructField {
714 fn clean(&self) -> Item {
715 let (name, vis) = match self.node.kind {
716 ast::NamedField(id, vis) => (Some(id), Some(vis)),
721 attrs: self.node.attrs.clean().move_iter().collect(),
722 source: self.span.clean(),
725 inner: StructFieldItem(StructField {
726 type_: self.node.ty.clean(),
732 pub type Visibility = ast::Visibility;
734 impl Clean<Option<Visibility>> for ast::Visibility {
735 fn clean(&self) -> Option<Visibility> {
740 #[deriving(Clone, Encodable, Decodable)]
742 struct_type: doctree::StructType,
745 fields_stripped: bool,
748 impl Clean<Item> for doctree::Struct {
749 fn clean(&self) -> Item {
751 name: Some(self.name.clean()),
752 attrs: self.attrs.clean(),
753 source: self.where.clean(),
755 visibility: self.vis.clean(),
756 inner: StructItem(Struct {
757 struct_type: self.struct_type,
758 generics: self.generics.clean(),
759 fields: self.fields.clean(),
760 fields_stripped: false,
766 /// This is a more limited form of the standard Struct, different in that
767 /// it lacks the things most items have (name, id, parameterization). Found
768 /// only as a variant in an enum.
769 #[deriving(Clone, Encodable, Decodable)]
770 pub struct VariantStruct {
771 struct_type: doctree::StructType,
773 fields_stripped: bool,
776 impl Clean<VariantStruct> for syntax::ast::StructDef {
777 fn clean(&self) -> VariantStruct {
779 struct_type: doctree::struct_type_from_def(self),
780 fields: self.fields.clean().move_iter().collect(),
781 fields_stripped: false,
786 #[deriving(Clone, Encodable, Decodable)]
788 variants: Vec<Item> ,
790 variants_stripped: bool,
793 impl Clean<Item> for doctree::Enum {
794 fn clean(&self) -> Item {
796 name: Some(self.name.clean()),
797 attrs: self.attrs.clean(),
798 source: self.where.clean(),
800 visibility: self.vis.clean(),
801 inner: EnumItem(Enum {
802 variants: self.variants.clean(),
803 generics: self.generics.clean(),
804 variants_stripped: false,
810 #[deriving(Clone, Encodable, Decodable)]
815 impl Clean<Item> for doctree::Variant {
816 fn clean(&self) -> Item {
818 name: Some(self.name.clean()),
819 attrs: self.attrs.clean(),
820 source: self.where.clean(),
821 visibility: self.vis.clean(),
823 inner: VariantItem(Variant {
824 kind: self.kind.clean(),
830 #[deriving(Clone, Encodable, Decodable)]
831 pub enum VariantKind {
833 TupleVariant(Vec<Type> ),
834 StructVariant(VariantStruct),
837 impl Clean<VariantKind> for ast::VariantKind {
838 fn clean(&self) -> VariantKind {
840 &ast::TupleVariantKind(ref args) => {
844 TupleVariant(args.iter().map(|x| x.ty.clean()).collect())
847 &ast::StructVariantKind(ref sd) => StructVariant(sd.clean()),
852 #[deriving(Clone, Encodable, Decodable)]
861 impl Clean<Span> for syntax::codemap::Span {
862 fn clean(&self) -> Span {
863 let cm = local_data::get(super::ctxtkey, |x| *x.unwrap()).sess().codemap();
864 let filename = cm.span_to_filename(*self);
865 let lo = cm.lookup_char_pos(self.lo);
866 let hi = cm.lookup_char_pos(self.hi);
868 filename: filename.to_owned(),
870 locol: lo.col.to_uint(),
872 hicol: hi.col.to_uint(),
877 #[deriving(Clone, Encodable, Decodable)]
880 segments: Vec<PathSegment> ,
883 impl Clean<Path> for ast::Path {
884 fn clean(&self) -> Path {
887 segments: self.segments.clean().move_iter().collect(),
892 #[deriving(Clone, Encodable, Decodable)]
893 pub struct PathSegment {
895 lifetimes: Vec<Lifetime> ,
899 impl Clean<PathSegment> for ast::PathSegment {
900 fn clean(&self) -> PathSegment {
902 name: self.identifier.clean(),
903 lifetimes: self.lifetimes.clean().move_iter().collect(),
904 types: self.types.clean().move_iter().collect()
909 fn path_to_str(p: &ast::Path) -> ~str {
910 use syntax::parse::token;
913 let mut first = true;
914 for i in p.segments.iter().map(|x| token::get_ident(x.identifier)) {
915 if !first || p.global {
925 impl Clean<~str> for ast::Ident {
926 fn clean(&self) -> ~str {
927 token::get_ident(*self).get().to_owned()
931 #[deriving(Clone, Encodable, Decodable)]
937 impl Clean<Item> for doctree::Typedef {
938 fn clean(&self) -> Item {
940 name: Some(self.name.clean()),
941 attrs: self.attrs.clean(),
942 source: self.where.clean(),
944 visibility: self.vis.clean(),
945 inner: TypedefItem(Typedef {
946 type_: self.ty.clean(),
947 generics: self.gen.clean(),
953 #[deriving(Clone, Encodable, Decodable)]
954 pub struct BareFunctionDecl {
961 impl Clean<BareFunctionDecl> for ast::BareFnTy {
962 fn clean(&self) -> BareFunctionDecl {
966 lifetimes: self.lifetimes.clean().move_iter().collect(),
967 type_params: Vec::new(),
969 decl: self.decl.clean(),
970 abi: self.abis.to_str(),
975 #[deriving(Clone, Encodable, Decodable)]
978 mutability: Mutability,
979 /// It's useful to have the value of a static documented, but I have no
980 /// desire to represent expressions (that'd basically be all of the AST,
981 /// which is huge!). So, have a string.
985 impl Clean<Item> for doctree::Static {
986 fn clean(&self) -> Item {
987 debug!("claning static {}: {:?}", self.name.clean(), self);
989 name: Some(self.name.clean()),
990 attrs: self.attrs.clean(),
991 source: self.where.clean(),
993 visibility: self.vis.clean(),
994 inner: StaticItem(Static {
995 type_: self.type_.clean(),
996 mutability: self.mutability.clean(),
997 expr: self.expr.span.to_src(),
1003 #[deriving(Show, Clone, Encodable, Decodable)]
1004 pub enum Mutability {
1009 impl Clean<Mutability> for ast::Mutability {
1010 fn clean(&self) -> Mutability {
1012 &ast::MutMutable => Mutable,
1013 &ast::MutImmutable => Immutable,
1018 #[deriving(Clone, Encodable, Decodable)]
1021 trait_: Option<Type>,
1027 impl Clean<Item> for doctree::Impl {
1028 fn clean(&self) -> Item {
1029 let mut derived = false;
1030 for attr in self.attrs.iter() {
1031 match attr.node.value.node {
1032 ast::MetaWord(ref s) => {
1033 if s.get() == "automatically_derived" {
1042 attrs: self.attrs.clean(),
1043 source: self.where.clean(),
1045 visibility: self.vis.clean(),
1046 inner: ImplItem(Impl {
1047 generics: self.generics.clean(),
1048 trait_: self.trait_.clean(),
1049 for_: self.for_.clean(),
1050 methods: self.methods.clean(),
1057 #[deriving(Clone, Encodable, Decodable)]
1058 pub struct ViewItem {
1059 inner: ViewItemInner
1062 impl Clean<Item> for ast::ViewItem {
1063 fn clean(&self) -> Item {
1066 attrs: self.attrs.clean().move_iter().collect(),
1067 source: self.span.clean(),
1069 visibility: self.vis.clean(),
1070 inner: ViewItemItem(ViewItem {
1071 inner: self.node.clean()
1077 #[deriving(Clone, Encodable, Decodable)]
1078 pub enum ViewItemInner {
1079 ExternCrate(~str, Option<~str>, ast::NodeId),
1080 Import(Vec<ViewPath>)
1083 impl Clean<ViewItemInner> for ast::ViewItem_ {
1084 fn clean(&self) -> ViewItemInner {
1086 &ast::ViewItemExternCrate(ref i, ref p, ref id) => {
1087 let string = match *p {
1089 Some((ref x, _)) => Some(x.get().to_owned()),
1091 ExternCrate(i.clean(), string, *id)
1093 &ast::ViewItemUse(ref vp) => {
1094 Import(vp.clean().move_iter().collect())
1100 #[deriving(Clone, Encodable, Decodable)]
1102 // use str = source;
1103 SimpleImport(~str, ImportSource),
1105 GlobImport(ImportSource),
1106 // use source::{a, b, c};
1107 ImportList(ImportSource, Vec<ViewListIdent> ),
1110 #[deriving(Clone, Encodable, Decodable)]
1111 pub struct ImportSource {
1113 did: Option<ast::DefId>,
1116 impl Clean<ViewPath> for ast::ViewPath {
1117 fn clean(&self) -> ViewPath {
1119 ast::ViewPathSimple(ref i, ref p, id) =>
1120 SimpleImport(i.clean(), resolve_use_source(p.clean(), id)),
1121 ast::ViewPathGlob(ref p, id) =>
1122 GlobImport(resolve_use_source(p.clean(), id)),
1123 ast::ViewPathList(ref p, ref pl, id) => {
1124 ImportList(resolve_use_source(p.clean(), id),
1125 pl.clean().move_iter().collect())
1131 #[deriving(Clone, Encodable, Decodable)]
1132 pub struct ViewListIdent {
1134 source: Option<ast::DefId>,
1137 impl Clean<ViewListIdent> for ast::PathListIdent {
1138 fn clean(&self) -> ViewListIdent {
1140 name: self.node.name.clean(),
1141 source: resolve_def(self.node.id),
1146 impl Clean<Vec<Item>> for ast::ForeignMod {
1147 fn clean(&self) -> Vec<Item> {
1152 impl Clean<Item> for ast::ForeignItem {
1153 fn clean(&self) -> Item {
1154 let inner = match self.node {
1155 ast::ForeignItemFn(ref decl, ref generics) => {
1156 ForeignFunctionItem(Function {
1158 generics: generics.clean(),
1159 purity: ast::ExternFn,
1162 ast::ForeignItemStatic(ref ty, mutbl) => {
1163 ForeignStaticItem(Static {
1165 mutability: if mutbl {Mutable} else {Immutable},
1171 name: Some(self.ident.clean()),
1172 attrs: self.attrs.clean().move_iter().collect(),
1173 source: self.span.clean(),
1175 visibility: self.vis.clean(),
1184 fn to_src(&self) -> ~str;
1187 impl ToSource for syntax::codemap::Span {
1188 fn to_src(&self) -> ~str {
1189 debug!("converting span {:?} to snippet", self.clean());
1190 let cm = local_data::get(super::ctxtkey, |x| x.unwrap().clone()).sess().codemap().clone();
1191 let sn = match cm.span_to_snippet(*self) {
1195 debug!("got snippet {}", sn);
1200 fn lit_to_str(lit: &ast::Lit) -> ~str {
1202 ast::LitStr(ref st, _) => st.get().to_owned(),
1203 ast::LitBinary(ref data) => format!("{:?}", data.as_slice()),
1204 ast::LitChar(c) => ~"'" + std::char::from_u32(c).unwrap().to_str() + "'",
1205 ast::LitInt(i, _t) => i.to_str(),
1206 ast::LitUint(u, _t) => u.to_str(),
1207 ast::LitIntUnsuffixed(i) => i.to_str(),
1208 ast::LitFloat(ref f, _t) => f.get().to_str(),
1209 ast::LitFloatUnsuffixed(ref f) => f.get().to_str(),
1210 ast::LitBool(b) => b.to_str(),
1215 fn name_from_pat(p: &ast::Pat) -> ~str {
1217 debug!("Trying to get a name from pattern: {:?}", p);
1221 PatWildMulti => ~"..",
1222 PatIdent(_, ref p, _) => path_to_str(p),
1223 PatEnum(ref p, _) => path_to_str(p),
1224 PatStruct(..) => fail!("tried to get argument name from pat_struct, \
1225 which is not allowed in function arguments"),
1226 PatTup(..) => ~"(tuple arg NYI)",
1227 PatUniq(p) => name_from_pat(p),
1228 PatRegion(p) => name_from_pat(p),
1230 warn!("tried to get argument name from PatLit, \
1231 which is silly in function arguments");
1234 PatRange(..) => fail!("tried to get argument name from PatRange, \
1235 which is not allowed in function arguments"),
1236 PatVec(..) => fail!("tried to get argument name from pat_vec, \
1237 which is not allowed in function arguments")
1241 /// Given a Type, resolve it using the def_map
1242 fn resolve_type(path: Path, tpbs: Option<Vec<TyParamBound> >,
1243 id: ast::NodeId) -> Type {
1244 let cx = local_data::get(super::ctxtkey, |x| *x.unwrap());
1245 let tycx = match cx.maybe_typed {
1246 core::Typed(ref tycx) => tycx,
1247 // If we're extracting tests, this return value doesn't matter.
1248 core::NotTyped(_) => return Bool
1250 debug!("searching for {:?} in defmap", id);
1251 let d = match tycx.def_map.borrow().find(&id) {
1254 debug!("could not find {:?} in defmap (`{}`)", id, tycx.map.node_to_str(id));
1255 fail!("Unexpected failure: unresolved id not in defmap (this is a bug!)")
1259 let (def_id, kind) = match d {
1260 ast::DefFn(i, _) => (i, TypeFunction),
1261 ast::DefSelfTy(i) => return Self(i),
1262 ast::DefTy(i) => (i, TypeEnum),
1263 ast::DefTrait(i) => {
1264 debug!("saw DefTrait in def_to_id");
1267 ast::DefPrimTy(p) => match p {
1268 ast::TyStr => return String,
1269 ast::TyBool => return Bool,
1270 _ => return Primitive(p)
1272 ast::DefTyParam(i, _) => return Generic(i.node),
1273 ast::DefStruct(i) => (i, TypeStruct),
1274 ast::DefTyParamBinder(i) => {
1275 debug!("found a typaram_binder, what is it? {}", i);
1276 return TyParamBinder(i);
1278 x => fail!("resolved type maps to a weird def {:?}", x),
1280 if ast_util::is_local(def_id) {
1281 ResolvedPath{ path: path, typarams: tpbs, id: def_id.node }
1283 let fqn = csearch::get_item_path(tycx, def_id);
1284 let fqn = fqn.move_iter().map(|i| i.to_str()).collect();
1290 krate: def_id.krate,
1295 fn resolve_use_source(path: Path, id: ast::NodeId) -> ImportSource {
1298 did: resolve_def(id),
1302 fn resolve_def(id: ast::NodeId) -> Option<ast::DefId> {
1303 let cx = local_data::get(super::ctxtkey, |x| *x.unwrap());
1304 match cx.maybe_typed {
1305 core::Typed(ref tcx) => {
1306 tcx.def_map.borrow().find(&id).map(|&d| ast_util::def_id_of_def(d))
1308 core::NotTyped(_) => None
1312 #[deriving(Clone, Encodable, Decodable)]
1317 impl Clean<Item> for doctree::Macro {
1318 fn clean(&self) -> Item {
1320 name: Some(self.name.clean()),
1321 attrs: self.attrs.clean(),
1322 source: self.where.clean(),
1323 visibility: ast::Public.clean(),
1325 inner: MacroItem(Macro {
1326 source: self.where.to_src(),