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<~[U]> for ~[T] {
39 fn clean(&self) -> ~[U] {
40 self.iter().map(|x| x.clean()).collect()
44 impl<T: Clean<U>, U> Clean<Vec<U>> for Vec<T> {
45 fn clean(&self) -> Vec<U> {
46 self.iter().map(|x| x.clean()).collect()
50 impl<T: Clean<U>, U> Clean<U> for @T {
51 fn clean(&self) -> U {
56 impl<T: Clean<U>, U> Clean<Option<U>> for Option<T> {
57 fn clean(&self) -> Option<U> {
60 &Some(ref v) => Some(v.clean())
65 impl<T: Clean<U>, U> Clean<Vec<U>> for syntax::owned_slice::OwnedSlice<T> {
66 fn clean(&self) -> Vec<U> {
67 self.iter().map(|x| x.clean()).collect()
71 #[deriving(Clone, Encodable, Decodable)]
75 externs: ~[(ast::CrateNum, ExternalCrate)],
78 impl<'a> Clean<Crate> for visit_ast::RustdocVisitor<'a> {
79 fn clean(&self) -> Crate {
80 use syntax::attr::find_crateid;
81 let cx = local_data::get(super::ctxtkey, |x| *x.unwrap());
83 let mut externs = ~[];
84 cx.sess().cstore.iter_crate_data(|n, meta| {
85 externs.push((n, meta.clean()));
89 name: match find_crateid(self.attrs) {
91 None => fail!("rustdoc requires a `crate_id` crate attribute"),
93 module: Some(self.module.clean()),
99 #[deriving(Clone, Encodable, Decodable)]
100 pub struct ExternalCrate {
105 impl Clean<ExternalCrate> for cstore::crate_metadata {
106 fn clean(&self) -> ExternalCrate {
108 name: self.name.to_owned(),
109 attrs: decoder::get_crate_attributes(self.data()).clean()
116 /// Anything with a source location and set of attributes and, optionally, a
117 /// name. That is, anything that can be documented. This doesn't correspond
118 /// directly to the AST's concept of an item; it's a strict superset.
119 #[deriving(Clone, Encodable, Decodable)]
123 /// Not everything has a name. E.g., impls
127 visibility: Option<Visibility>,
132 /// Finds the `doc` attribute as a List and returns the list of attributes
134 pub fn doc_list<'a>(&'a self) -> Option<&'a [Attribute]> {
135 for attr in self.attrs.iter() {
137 List(ref x, ref list) if "doc" == *x => { return Some(list.as_slice()); }
144 /// Finds the `doc` attribute as a NameValue and returns the corresponding
146 pub fn doc_value<'a>(&'a self) -> Option<&'a str> {
147 for attr in self.attrs.iter() {
149 NameValue(ref x, ref v) if "doc" == *x => { return Some(v.as_slice()); }
156 pub fn is_mod(&self) -> bool {
157 match self.inner { ModuleItem(..) => true, _ => false }
159 pub fn is_trait(&self) -> bool {
160 match self.inner { TraitItem(..) => true, _ => false }
162 pub fn is_struct(&self) -> bool {
163 match self.inner { StructItem(..) => true, _ => false }
165 pub fn is_enum(&self) -> bool {
166 match self.inner { EnumItem(..) => true, _ => false }
168 pub fn is_fn(&self) -> bool {
169 match self.inner { FunctionItem(..) => true, _ => false }
173 #[deriving(Clone, Encodable, Decodable)]
177 FunctionItem(Function),
179 TypedefItem(Typedef),
183 ViewItemItem(ViewItem),
184 TyMethodItem(TyMethod),
186 StructFieldItem(StructField),
187 VariantItem(Variant),
188 ForeignFunctionItem(Function),
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 = ~[];
207 for subforeigns in self.foreigns.clean().move_iter() {
208 for foreign in subforeigns.move_iter() {
209 foreigns.push(foreign)
212 let items: ~[~[Item]] = ~[
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,
233 items: items.concat_vec(),
239 #[deriving(Clone, Encodable, Decodable)]
242 List(~str, ~[Attribute]),
243 NameValue(~str, ~str)
246 impl Clean<Attribute> for ast::MetaItem {
247 fn clean(&self) -> Attribute {
249 ast::MetaWord(ref s) => Word(s.get().to_owned()),
250 ast::MetaList(ref s, ref l) => {
251 List(s.get().to_owned(), l.clean().move_iter().collect())
253 ast::MetaNameValue(ref s, ref v) => {
254 NameValue(s.get().to_owned(), lit_to_str(v))
260 impl Clean<Attribute> for ast::Attribute {
261 fn clean(&self) -> Attribute {
262 self.desugar_doc().node.value.clean()
266 // This is a rough approximation that gets us what we want.
267 impl<'a> attr::AttrMetaMethods for &'a Attribute {
268 fn name(&self) -> InternedString {
270 Word(ref n) | List(ref n, _) | NameValue(ref n, _) => {
271 token::intern_and_get_ident(*n)
276 fn value_str(&self) -> Option<InternedString> {
278 NameValue(_, ref v) => Some(token::intern_and_get_ident(*v)),
282 fn meta_item_list<'a>(&'a self) -> Option<&'a [@ast::MetaItem]> { None }
283 fn name_str_pair(&self) -> Option<(InternedString, InternedString)> {
288 #[deriving(Clone, Encodable, Decodable)]
292 bounds: ~[TyParamBound]
295 impl Clean<TyParam> for ast::TyParam {
296 fn clean(&self) -> TyParam {
298 name: self.ident.clean(),
300 bounds: self.bounds.clean().move_iter().collect(),
305 #[deriving(Clone, Encodable, Decodable)]
306 pub enum TyParamBound {
311 impl Clean<TyParamBound> for ast::TyParamBound {
312 fn clean(&self) -> TyParamBound {
314 ast::RegionTyParamBound => RegionBound,
315 ast::TraitTyParamBound(ref t) => TraitBound(t.clean()),
320 #[deriving(Clone, Encodable, Decodable)]
321 pub struct Lifetime(~str);
324 pub fn get_ref<'a>(&'a self) -> &'a str {
325 let Lifetime(ref s) = *self;
331 impl Clean<Lifetime> for ast::Lifetime {
332 fn clean(&self) -> Lifetime {
333 Lifetime(token::get_name(self.name).get().to_owned())
337 // maybe use a Generic enum and use ~[Generic]?
338 #[deriving(Clone, Encodable, Decodable)]
339 pub struct Generics {
340 lifetimes: ~[Lifetime],
341 type_params: ~[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: ~[Lifetime],
478 onceness: ast::Onceness,
480 bounds: ~[TyParamBound]
483 impl Clean<ClosureDecl> for ast::ClosureTy {
484 fn clean(&self) -> ClosureDecl {
487 region: self.region.clean(),
488 lifetimes: self.lifetimes.clean().move_iter().collect(),
489 decl: self.decl.clean(),
490 onceness: self.onceness,
492 bounds: match self.bounds {
493 Some(ref x) => x.clean().move_iter().collect(),
500 #[deriving(Clone, Encodable, Decodable)]
508 #[deriving(Clone, Encodable, Decodable)]
509 pub struct Arguments {
513 impl Clean<FnDecl> for ast::FnDecl {
514 fn clean(&self) -> FnDecl {
517 values: self.inputs.iter().map(|x| x.clean()).collect(),
519 output: (self.output.clean()),
526 #[deriving(Clone, Encodable, Decodable)]
527 pub struct Argument {
533 impl Clean<Argument> for ast::Arg {
534 fn clean(&self) -> Argument {
536 name: name_from_pat(self.pat),
537 type_: (self.ty.clean()),
543 #[deriving(Clone, Encodable, Decodable)]
549 impl Clean<RetStyle> for ast::RetStyle {
550 fn clean(&self) -> RetStyle {
552 ast::Return => Return,
553 ast::NoReturn => NoReturn
558 #[deriving(Clone, Encodable, Decodable)]
560 methods: ~[TraitMethod],
565 impl Clean<Item> for doctree::Trait {
566 fn clean(&self) -> Item {
568 name: Some(self.name.clean()),
569 attrs: self.attrs.clean(),
570 source: self.where.clean(),
572 visibility: self.vis.clean(),
573 inner: TraitItem(Trait {
574 methods: self.methods.clean(),
575 generics: self.generics.clean(),
576 parents: self.parents.clean(),
582 impl Clean<Type> for ast::TraitRef {
583 fn clean(&self) -> Type {
584 resolve_type(self.path.clean(), None, self.ref_id)
588 #[deriving(Clone, Encodable, Decodable)]
589 pub enum TraitMethod {
595 pub fn is_req(&self) -> bool {
597 &Required(..) => true,
601 pub fn is_def(&self) -> bool {
603 &Provided(..) => true,
607 pub fn item<'a>(&'a self) -> &'a Item {
609 Required(ref item) => item,
610 Provided(ref item) => item,
615 impl Clean<TraitMethod> for ast::TraitMethod {
616 fn clean(&self) -> TraitMethod {
618 &ast::Required(ref t) => Required(t.clean()),
619 &ast::Provided(ref t) => Provided(t.clean()),
624 /// A representation of a Type suitable for hyperlinking purposes. Ideally one can get the original
625 /// type out of the AST/ty::ctxt given one of these, if more information is needed. Most importantly
626 /// it does not preserve mutability or boxes.
627 #[deriving(Clone, Encodable, Decodable)]
629 /// structs/enums/traits (anything that'd be an ast::TyPath)
632 typarams: Option<~[TyParamBound]>,
635 /// Same as above, but only external variants
638 typarams: Option<~[TyParamBound]>,
641 krate: ast::CrateNum,
643 // I have no idea how to usefully use this.
644 TyParamBinder(ast::NodeId),
645 /// For parameterized types, so the consumer of the JSON don't go looking
646 /// for types which don't exist anywhere.
647 Generic(ast::NodeId),
648 /// For references to self
650 /// Primitives are just the fixed-size numeric types (plus int/uint/float), and char.
651 Primitive(ast::PrimTy),
652 Closure(~ClosureDecl),
654 BareFunction(~BareFunctionDecl),
657 FixedVector(~Type, ~str),
666 RawPointer(Mutability, ~Type),
667 BorrowedRef { lifetime: Option<Lifetime>, mutability: Mutability, type_: ~Type},
668 // region, raw, other boxes, mutable
671 #[deriving(Clone, Encodable, Decodable)]
679 impl Clean<Type> for ast::Ty {
680 fn clean(&self) -> Type {
682 debug!("cleaning type `{:?}`", self);
683 let codemap = local_data::get(super::ctxtkey, |x| *x.unwrap()).sess().codemap();
684 debug!("span corresponds to `{}`", codemap.span_to_str(self.span));
687 TyPtr(ref m) => RawPointer(m.mutbl.clean(), ~m.ty.clean()),
688 TyRptr(ref l, ref m) =>
689 BorrowedRef {lifetime: l.clean(), mutability: m.mutbl.clean(),
690 type_: ~m.ty.clean()},
691 TyBox(ty) => Managed(~ty.clean()),
692 TyUniq(ty) => Unique(~ty.clean()),
693 TyVec(ty) => Vector(~ty.clean()),
694 TyFixedLengthVec(ty, ref e) => FixedVector(~ty.clean(),
696 TyTup(ref tys) => Tuple(tys.iter().map(|x| x.clean()).collect()),
697 TyPath(ref p, ref tpbs, id) => {
698 resolve_type(p.clean(),
699 tpbs.clean().map(|x| x.move_iter().collect()),
702 TyClosure(ref c) => Closure(~c.clean()),
703 TyBareFn(ref barefn) => BareFunction(~barefn.clean()),
705 ref x => fail!("Unimplemented type {:?}", x),
710 #[deriving(Clone, Encodable, Decodable)]
711 pub struct StructField {
715 impl Clean<Item> for ast::StructField {
716 fn clean(&self) -> Item {
717 let (name, vis) = match self.node.kind {
718 ast::NamedField(id, vis) => (Some(id), Some(vis)),
723 attrs: self.node.attrs.clean().move_iter().collect(),
724 source: self.span.clean(),
727 inner: StructFieldItem(StructField {
728 type_: self.node.ty.clean(),
734 pub type Visibility = ast::Visibility;
736 impl Clean<Option<Visibility>> for ast::Visibility {
737 fn clean(&self) -> Option<Visibility> {
742 #[deriving(Clone, Encodable, Decodable)]
744 struct_type: doctree::StructType,
747 fields_stripped: bool,
750 impl Clean<Item> for doctree::Struct {
751 fn clean(&self) -> Item {
753 name: Some(self.name.clean()),
754 attrs: self.attrs.clean(),
755 source: self.where.clean(),
757 visibility: self.vis.clean(),
758 inner: StructItem(Struct {
759 struct_type: self.struct_type,
760 generics: self.generics.clean(),
761 fields: self.fields.clean(),
762 fields_stripped: false,
768 /// This is a more limited form of the standard Struct, different in that
769 /// it lacks the things most items have (name, id, parameterization). Found
770 /// only as a variant in an enum.
771 #[deriving(Clone, Encodable, Decodable)]
772 pub struct VariantStruct {
773 struct_type: doctree::StructType,
775 fields_stripped: bool,
778 impl Clean<VariantStruct> for syntax::ast::StructDef {
779 fn clean(&self) -> VariantStruct {
781 struct_type: doctree::struct_type_from_def(self),
782 fields: self.fields.clean().move_iter().collect(),
783 fields_stripped: false,
788 #[deriving(Clone, Encodable, Decodable)]
792 variants_stripped: bool,
795 impl Clean<Item> for doctree::Enum {
796 fn clean(&self) -> Item {
798 name: Some(self.name.clean()),
799 attrs: self.attrs.clean(),
800 source: self.where.clean(),
802 visibility: self.vis.clean(),
803 inner: EnumItem(Enum {
804 variants: self.variants.clean(),
805 generics: self.generics.clean(),
806 variants_stripped: false,
812 #[deriving(Clone, Encodable, Decodable)]
817 impl Clean<Item> for doctree::Variant {
818 fn clean(&self) -> Item {
820 name: Some(self.name.clean()),
821 attrs: self.attrs.clean(),
822 source: self.where.clean(),
823 visibility: self.vis.clean(),
825 inner: VariantItem(Variant {
826 kind: self.kind.clean(),
832 #[deriving(Clone, Encodable, Decodable)]
833 pub enum VariantKind {
835 TupleVariant(~[Type]),
836 StructVariant(VariantStruct),
839 impl Clean<VariantKind> for ast::VariantKind {
840 fn clean(&self) -> VariantKind {
842 &ast::TupleVariantKind(ref args) => {
846 TupleVariant(args.iter().map(|x| x.ty.clean()).collect())
849 &ast::StructVariantKind(ref sd) => StructVariant(sd.clean()),
854 #[deriving(Clone, Encodable, Decodable)]
863 impl Clean<Span> for syntax::codemap::Span {
864 fn clean(&self) -> Span {
865 let cm = local_data::get(super::ctxtkey, |x| *x.unwrap()).sess().codemap();
866 let filename = cm.span_to_filename(*self);
867 let lo = cm.lookup_char_pos(self.lo);
868 let hi = cm.lookup_char_pos(self.hi);
870 filename: filename.to_owned(),
872 locol: lo.col.to_uint(),
874 hicol: hi.col.to_uint(),
879 #[deriving(Clone, Encodable, Decodable)]
882 segments: ~[PathSegment],
885 impl Clean<Path> for ast::Path {
886 fn clean(&self) -> Path {
889 segments: self.segments.clean().move_iter().collect(),
894 #[deriving(Clone, Encodable, Decodable)]
895 pub struct PathSegment {
897 lifetimes: ~[Lifetime],
901 impl Clean<PathSegment> for ast::PathSegment {
902 fn clean(&self) -> PathSegment {
904 name: self.identifier.clean(),
905 lifetimes: self.lifetimes.clean().move_iter().collect(),
906 types: self.types.clean().move_iter().collect()
911 fn path_to_str(p: &ast::Path) -> ~str {
912 use syntax::parse::token;
915 let mut first = true;
916 for i in p.segments.iter().map(|x| token::get_ident(x.identifier)) {
917 if !first || p.global {
927 impl Clean<~str> for ast::Ident {
928 fn clean(&self) -> ~str {
929 token::get_ident(*self).get().to_owned()
933 #[deriving(Clone, Encodable, Decodable)]
939 impl Clean<Item> for doctree::Typedef {
940 fn clean(&self) -> Item {
942 name: Some(self.name.clean()),
943 attrs: self.attrs.clean(),
944 source: self.where.clean(),
946 visibility: self.vis.clean(),
947 inner: TypedefItem(Typedef {
948 type_: self.ty.clean(),
949 generics: self.gen.clean(),
955 #[deriving(Clone, Encodable, Decodable)]
956 pub struct BareFunctionDecl {
963 impl Clean<BareFunctionDecl> for ast::BareFnTy {
964 fn clean(&self) -> BareFunctionDecl {
968 lifetimes: self.lifetimes.clean().move_iter().collect(),
971 decl: self.decl.clean(),
972 abi: self.abis.to_str(),
977 #[deriving(Clone, Encodable, Decodable)]
980 mutability: Mutability,
981 /// It's useful to have the value of a static documented, but I have no
982 /// desire to represent expressions (that'd basically be all of the AST,
983 /// which is huge!). So, have a string.
987 impl Clean<Item> for doctree::Static {
988 fn clean(&self) -> Item {
989 debug!("claning static {}: {:?}", self.name.clean(), self);
991 name: Some(self.name.clean()),
992 attrs: self.attrs.clean(),
993 source: self.where.clean(),
995 visibility: self.vis.clean(),
996 inner: StaticItem(Static {
997 type_: self.type_.clean(),
998 mutability: self.mutability.clean(),
999 expr: self.expr.span.to_src(),
1005 #[deriving(Show, Clone, Encodable, Decodable)]
1006 pub enum Mutability {
1011 impl Clean<Mutability> for ast::Mutability {
1012 fn clean(&self) -> Mutability {
1014 &ast::MutMutable => Mutable,
1015 &ast::MutImmutable => Immutable,
1020 #[deriving(Clone, Encodable, Decodable)]
1023 trait_: Option<Type>,
1028 impl Clean<Item> for doctree::Impl {
1029 fn clean(&self) -> Item {
1032 attrs: self.attrs.clean(),
1033 source: self.where.clean(),
1035 visibility: self.vis.clean(),
1036 inner: ImplItem(Impl {
1037 generics: self.generics.clean(),
1038 trait_: self.trait_.clean(),
1039 for_: self.for_.clean(),
1040 methods: self.methods.clean(),
1046 #[deriving(Clone, Encodable, Decodable)]
1047 pub struct ViewItem {
1048 inner: ViewItemInner
1051 impl Clean<Item> for ast::ViewItem {
1052 fn clean(&self) -> Item {
1055 attrs: self.attrs.clean().move_iter().collect(),
1056 source: self.span.clean(),
1058 visibility: self.vis.clean(),
1059 inner: ViewItemItem(ViewItem {
1060 inner: self.node.clean()
1066 #[deriving(Clone, Encodable, Decodable)]
1067 pub enum ViewItemInner {
1068 ExternCrate(~str, Option<~str>, ast::NodeId),
1072 impl Clean<ViewItemInner> for ast::ViewItem_ {
1073 fn clean(&self) -> ViewItemInner {
1075 &ast::ViewItemExternCrate(ref i, ref p, ref id) => {
1076 let string = match *p {
1078 Some((ref x, _)) => Some(x.get().to_owned()),
1080 ExternCrate(i.clean(), string, *id)
1082 &ast::ViewItemUse(ref vp) => {
1083 Import(vp.clean().move_iter().collect())
1089 #[deriving(Clone, Encodable, Decodable)]
1091 // use str = source;
1092 SimpleImport(~str, ImportSource),
1094 GlobImport(ImportSource),
1095 // use source::{a, b, c};
1096 ImportList(ImportSource, ~[ViewListIdent]),
1099 #[deriving(Clone, Encodable, Decodable)]
1100 pub struct ImportSource {
1102 did: Option<ast::DefId>,
1105 impl Clean<ViewPath> for ast::ViewPath {
1106 fn clean(&self) -> ViewPath {
1108 ast::ViewPathSimple(ref i, ref p, id) =>
1109 SimpleImport(i.clean(), resolve_use_source(p.clean(), id)),
1110 ast::ViewPathGlob(ref p, id) =>
1111 GlobImport(resolve_use_source(p.clean(), id)),
1112 ast::ViewPathList(ref p, ref pl, id) => {
1113 ImportList(resolve_use_source(p.clean(), id),
1114 pl.clean().move_iter().collect())
1120 #[deriving(Clone, Encodable, Decodable)]
1121 pub struct ViewListIdent {
1123 source: Option<ast::DefId>,
1126 impl Clean<ViewListIdent> for ast::PathListIdent {
1127 fn clean(&self) -> ViewListIdent {
1129 name: self.node.name.clean(),
1130 source: resolve_def(self.node.id),
1135 impl Clean<Vec<Item>> for ast::ForeignMod {
1136 fn clean(&self) -> Vec<Item> {
1141 impl Clean<Item> for ast::ForeignItem {
1142 fn clean(&self) -> Item {
1143 let inner = match self.node {
1144 ast::ForeignItemFn(ref decl, ref generics) => {
1145 ForeignFunctionItem(Function {
1147 generics: generics.clean(),
1148 purity: ast::ExternFn,
1151 ast::ForeignItemStatic(ref ty, mutbl) => {
1152 ForeignStaticItem(Static {
1154 mutability: if mutbl {Mutable} else {Immutable},
1160 name: Some(self.ident.clean()),
1161 attrs: self.attrs.clean().move_iter().collect(),
1162 source: self.span.clean(),
1164 visibility: self.vis.clean(),
1173 fn to_src(&self) -> ~str;
1176 impl ToSource for syntax::codemap::Span {
1177 fn to_src(&self) -> ~str {
1178 debug!("converting span {:?} to snippet", self.clean());
1179 let cm = local_data::get(super::ctxtkey, |x| x.unwrap().clone()).sess().codemap().clone();
1180 let sn = match cm.span_to_snippet(*self) {
1184 debug!("got snippet {}", sn);
1189 fn lit_to_str(lit: &ast::Lit) -> ~str {
1191 ast::LitStr(ref st, _) => st.get().to_owned(),
1192 ast::LitBinary(ref data) => format!("{:?}", data.deref().as_slice()),
1193 ast::LitChar(c) => ~"'" + std::char::from_u32(c).unwrap().to_str() + "'",
1194 ast::LitInt(i, _t) => i.to_str(),
1195 ast::LitUint(u, _t) => u.to_str(),
1196 ast::LitIntUnsuffixed(i) => i.to_str(),
1197 ast::LitFloat(ref f, _t) => f.get().to_str(),
1198 ast::LitFloatUnsuffixed(ref f) => f.get().to_str(),
1199 ast::LitBool(b) => b.to_str(),
1204 fn name_from_pat(p: &ast::Pat) -> ~str {
1206 debug!("Trying to get a name from pattern: {:?}", p);
1210 PatWildMulti => ~"..",
1211 PatIdent(_, ref p, _) => path_to_str(p),
1212 PatEnum(ref p, _) => path_to_str(p),
1213 PatStruct(..) => fail!("tried to get argument name from pat_struct, \
1214 which is not allowed in function arguments"),
1215 PatTup(..) => ~"(tuple arg NYI)",
1216 PatUniq(p) => name_from_pat(p),
1217 PatRegion(p) => name_from_pat(p),
1219 warn!("tried to get argument name from PatLit, \
1220 which is silly in function arguments");
1223 PatRange(..) => fail!("tried to get argument name from PatRange, \
1224 which is not allowed in function arguments"),
1225 PatVec(..) => fail!("tried to get argument name from pat_vec, \
1226 which is not allowed in function arguments")
1230 /// Given a Type, resolve it using the def_map
1231 fn resolve_type(path: Path, tpbs: Option<~[TyParamBound]>,
1232 id: ast::NodeId) -> Type {
1233 let cx = local_data::get(super::ctxtkey, |x| *x.unwrap());
1234 let tycx = match cx.maybe_typed {
1235 core::Typed(ref tycx) => tycx,
1236 // If we're extracting tests, this return value doesn't matter.
1237 core::NotTyped(_) => return Bool
1239 debug!("searching for {:?} in defmap", id);
1240 let def_map = tycx.def_map.borrow();
1241 let d = match def_map.get().find(&id) {
1244 debug!("could not find {:?} in defmap (`{}`)", id, tycx.map.node_to_str(id));
1245 fail!("Unexpected failure: unresolved id not in defmap (this is a bug!)")
1249 let (def_id, kind) = match *d {
1250 ast::DefFn(i, _) => (i, TypeFunction),
1251 ast::DefSelfTy(i) => return Self(i),
1252 ast::DefTy(i) => (i, TypeEnum),
1253 ast::DefTrait(i) => {
1254 debug!("saw DefTrait in def_to_id");
1257 ast::DefPrimTy(p) => match p {
1258 ast::TyStr => return String,
1259 ast::TyBool => return Bool,
1260 _ => return Primitive(p)
1262 ast::DefTyParam(i, _) => return Generic(i.node),
1263 ast::DefStruct(i) => (i, TypeStruct),
1264 ast::DefTyParamBinder(i) => {
1265 debug!("found a typaram_binder, what is it? {}", i);
1266 return TyParamBinder(i);
1268 x => fail!("resolved type maps to a weird def {:?}", x),
1270 if ast_util::is_local(def_id) {
1271 ResolvedPath{ path: path, typarams: tpbs, id: def_id.node }
1273 let fqn = csearch::get_item_path(tycx, def_id);
1274 let fqn = fqn.move_iter().map(|i| i.to_str()).to_owned_vec();
1275 ExternalPath{ path: path, typarams: tpbs, fqn: fqn, kind: kind,
1276 krate: def_id.krate }
1280 fn resolve_use_source(path: Path, id: ast::NodeId) -> ImportSource {
1283 did: resolve_def(id),
1287 fn resolve_def(id: ast::NodeId) -> Option<ast::DefId> {
1288 let cx = local_data::get(super::ctxtkey, |x| *x.unwrap());
1289 match cx.maybe_typed {
1290 core::Typed(ref tcx) => {
1291 let def_map = tcx.def_map.borrow();
1292 def_map.get().find(&id).map(|&d| ast_util::def_id_of_def(d))
1294 core::NotTyped(_) => None
1298 #[deriving(Clone, Encodable, Decodable)]
1303 impl Clean<Item> for doctree::Macro {
1304 fn clean(&self) -> Item {
1306 name: Some(self.name.clean()),
1307 attrs: self.attrs.clean(),
1308 source: self.where.clean(),
1309 visibility: ast::Public.clean(),
1311 inner: MacroItem(Macro {
1312 source: self.where.to_src(),