]> git.lizzy.rs Git - rust.git/blobdiff - src/librustdoc/clean.rs
Replace all ~"" with "".to_owned()
[rust.git] / src / librustdoc / clean.rs
index e8854215c009cd580910d3a9c7dfa9d00bde33f5..fcd6629096c9940f22fd5c2891cacc87176f0b33 100644 (file)
 use rustc::metadata::csearch;
 use rustc::metadata::decoder;
 
+use std::local_data;
+use std::strbuf::StrBuf;
 use std;
 
 use core;
 use doctree;
 use visit_ast;
-use std::local_data;
 
 pub trait Clean<T> {
     fn clean(&self) -> T;
@@ -64,9 +65,9 @@ fn clean(&self) -> Vec<U> {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Crate {
-    name: ~str,
-    module: Option<Item>,
-    externs: Vec<(ast::CrateNum, ExternalCrate)> ,
+    pub name: ~str,
+    pub module: Option<Item>,
+    pub externs: Vec<(ast::CrateNum, ExternalCrate)>,
 }
 
 impl<'a> Clean<Crate> for visit_ast::RustdocVisitor<'a> {
@@ -92,8 +93,8 @@ fn clean(&self) -> Crate {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct ExternalCrate {
-    name: ~str,
-    attrs: Vec<Attribute> ,
+    pub name: ~str,
+    pub attrs: Vec<Attribute>,
 }
 
 impl Clean<ExternalCrate> for cstore::crate_metadata {
@@ -113,13 +114,13 @@ fn clean(&self) -> ExternalCrate {
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Item {
     /// Stringified span
-    source: Span,
+    pub source: Span,
     /// Not everything has a name. E.g., impls
-    name: Option<~str>,
-    attrs: Vec<Attribute> ,
-    inner: ItemEnum,
-    visibility: Option<Visibility>,
-    id: ast::NodeId,
+    pub name: Option<~str>,
+    pub attrs: Vec<Attribute> ,
+    pub inner: ItemEnum,
+    pub visibility: Option<Visibility>,
+    pub id: ast::NodeId,
 }
 
 impl Item {
@@ -174,20 +175,26 @@ pub enum ItemEnum {
     StaticItem(Static),
     TraitItem(Trait),
     ImplItem(Impl),
+    /// `use` and `extern crate`
     ViewItemItem(ViewItem),
+    /// A method signature only. Used for required methods in traits (ie,
+    /// non-default-methods).
     TyMethodItem(TyMethod),
+    /// A method with a body.
     MethodItem(Method),
     StructFieldItem(StructField),
     VariantItem(Variant),
+    /// `fn`s from an extern block
     ForeignFunctionItem(Function),
+    /// `static`s from an extern block
     ForeignStaticItem(Static),
     MacroItem(Macro),
 }
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Module {
-    items: Vec<Item> ,
-    is_crate: bool,
+    pub items: Vec<Item>,
+    pub is_crate: bool,
 }
 
 impl Clean<Item> for doctree::Module {
@@ -195,7 +202,7 @@ fn clean(&self) -> Item {
         let name = if self.name.is_some() {
             self.name.unwrap().clean()
         } else {
-            ~""
+            "".to_owned()
         };
         let mut foreigns = Vec::new();
         for subforeigns in self.foreigns.clean().move_iter() {
@@ -283,9 +290,10 @@ fn name_str_pair(&self) -> Option<(InternedString, InternedString)> {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct TyParam {
-    name: ~str,
-    id: ast::NodeId,
-    bounds: Vec<TyParamBound> }
+    pub name: ~str,
+    pub id: ast::NodeId,
+    pub bounds: Vec<TyParamBound>,
+}
 
 impl Clean<TyParam> for ast::TyParam {
     fn clean(&self) -> TyParam {
@@ -332,8 +340,9 @@ fn clean(&self) -> Lifetime {
 // maybe use a Generic enum and use ~[Generic]?
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Generics {
-    lifetimes: Vec<Lifetime> ,
-    type_params: Vec<TyParam> }
+    pub lifetimes: Vec<Lifetime>,
+    pub type_params: Vec<TyParam>,
+}
 
 impl Clean<Generics> for ast::Generics {
     fn clean(&self) -> Generics {
@@ -346,10 +355,10 @@ fn clean(&self) -> Generics {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Method {
-    generics: Generics,
-    self_: SelfTy,
-    purity: ast::Purity,
-    decl: FnDecl,
+    pub generics: Generics,
+    pub self_: SelfTy,
+    pub fn_style: ast::FnStyle,
+    pub decl: FnDecl,
 }
 
 impl Clean<Item> for ast::Method {
@@ -375,7 +384,7 @@ fn clean(&self) -> Item {
             inner: MethodItem(Method {
                 generics: self.generics.clean(),
                 self_: self.explicit_self.clean(),
-                purity: self.purity.clone(),
+                fn_style: self.fn_style.clone(),
                 decl: decl,
             }),
         }
@@ -384,10 +393,10 @@ fn clean(&self) -> Item {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct TyMethod {
-    purity: ast::Purity,
-    decl: FnDecl,
-    generics: Generics,
-    self_: SelfTy,
+    pub fn_style: ast::FnStyle,
+    pub decl: FnDecl,
+    pub generics: Generics,
+    pub self_: SelfTy,
 }
 
 impl Clean<Item> for ast::TypeMethod {
@@ -411,7 +420,7 @@ fn clean(&self) -> Item {
             id: self.id,
             visibility: None,
             inner: TyMethodItem(TyMethod {
-                purity: self.purity.clone(),
+                fn_style: self.fn_style.clone(),
                 decl: decl,
                 self_: self.explicit_self.clean(),
                 generics: self.generics.clean(),
@@ -441,9 +450,9 @@ fn clean(&self) -> SelfTy {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Function {
-    decl: FnDecl,
-    generics: Generics,
-    purity: ast::Purity,
+    pub decl: FnDecl,
+    pub generics: Generics,
+    pub fn_style: ast::FnStyle,
 }
 
 impl Clean<Item> for doctree::Function {
@@ -457,7 +466,7 @@ fn clean(&self) -> Item {
             inner: FunctionItem(Function {
                 decl: self.decl.clean(),
                 generics: self.generics.clean(),
-                purity: self.purity,
+                fn_style: self.fn_style,
             }),
         }
     }
@@ -465,23 +474,20 @@ fn clean(&self) -> Item {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct ClosureDecl {
-    sigil: ast::Sigil,
-    region: Option<Lifetime>,
-    lifetimes: Vec<Lifetime> ,
-    decl: FnDecl,
-    onceness: ast::Onceness,
-    purity: ast::Purity,
-    bounds: Vec<TyParamBound> }
+    pub lifetimes: Vec<Lifetime>,
+    pub decl: FnDecl,
+    pub onceness: ast::Onceness,
+    pub fn_style: ast::FnStyle,
+    pub bounds: Vec<TyParamBound>,
+}
 
 impl Clean<ClosureDecl> for ast::ClosureTy {
     fn clean(&self) -> ClosureDecl {
         ClosureDecl {
-            sigil: self.sigil,
-            region: self.region.clean(),
             lifetimes: self.lifetimes.clean().move_iter().collect(),
             decl: self.decl.clean(),
             onceness: self.onceness,
-            purity: self.purity,
+            fn_style: self.fn_style,
             bounds: match self.bounds {
                 Some(ref x) => x.clean().move_iter().collect(),
                 None        => Vec::new()
@@ -492,14 +498,15 @@ fn clean(&self) -> ClosureDecl {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct FnDecl {
-    inputs: Arguments,
-    output: Type,
-    cf: RetStyle,
-    attrs: Vec<Attribute> }
+    pub inputs: Arguments,
+    pub output: Type,
+    pub cf: RetStyle,
+    pub attrs: Vec<Attribute>,
+}
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Arguments {
-    values: Vec<Argument> ,
+    pub values: Vec<Argument>,
 }
 
 impl Clean<FnDecl> for ast::FnDecl {
@@ -517,9 +524,9 @@ fn clean(&self) -> FnDecl {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Argument {
-    type_: Type,
-    name: ~str,
-    id: ast::NodeId
+    pub type_: Type,
+    pub name: ~str,
+    pub id: ast::NodeId,
 }
 
 impl Clean<Argument> for ast::Arg {
@@ -549,9 +556,9 @@ fn clean(&self) -> RetStyle {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Trait {
-    methods: Vec<TraitMethod> ,
-    generics: Generics,
-    parents: Vec<Type> ,
+    pub methods: Vec<TraitMethod>,
+    pub generics: Generics,
+    pub parents: Vec<Type>,
 }
 
 impl Clean<Item> for doctree::Trait {
@@ -620,17 +627,17 @@ fn clean(&self) -> TraitMethod {
 pub enum Type {
     /// structs/enums/traits (anything that'd be an ast::TyPath)
     ResolvedPath {
-        path: Path,
-        typarams: Option<Vec<TyParamBound> >,
-        id: ast::NodeId,
+        pub path: Path,
+        pub typarams: Option<Vec<TyParamBound>>,
+        pub id: ast::NodeId,
     },
     /// Same as above, but only external variants
     ExternalPath {
-        path: Path,
-        typarams: Option<Vec<TyParamBound> >,
-        fqn: Vec<~str> ,
-        kind: TypeKind,
-        krate: ast::CrateNum,
+        pub path: Path,
+        pub typarams: Option<Vec<TyParamBound>>,
+        pub fqn: Vec<~str>,
+        pub kind: TypeKind,
+        pub krate: ast::CrateNum,
     },
     // I have no idea how to usefully use this.
     TyParamBinder(ast::NodeId),
@@ -641,7 +648,8 @@ pub enum Type {
     Self(ast::NodeId),
     /// Primitives are just the fixed-size numeric types (plus int/uint/float), and char.
     Primitive(ast::PrimTy),
-    Closure(~ClosureDecl),
+    Closure(~ClosureDecl, Option<Lifetime>),
+    Proc(~ClosureDecl),
     /// extern "ABI" fn
     BareFunction(~BareFunctionDecl),
     Tuple(Vec<Type> ),
@@ -656,7 +664,11 @@ pub enum Type {
     Unique(~Type),
     Managed(~Type),
     RawPointer(Mutability, ~Type),
-    BorrowedRef { lifetime: Option<Lifetime>, mutability: Mutability, type_: ~Type},
+    BorrowedRef {
+        pub lifetime: Option<Lifetime>,
+        pub mutability: Mutability,
+        pub type_: ~Type,
+    },
     // region, raw, other boxes, mutable
 }
 
@@ -691,7 +703,8 @@ fn clean(&self) -> Type {
                              tpbs.clean().map(|x| x.move_iter().collect()),
                              id)
             }
-            TyClosure(ref c) => Closure(~c.clean()),
+            TyClosure(ref c, region) => Closure(~c.clean(), region.clean()),
+            TyProc(ref c) => Proc(~c.clean()),
             TyBareFn(ref barefn) => BareFunction(~barefn.clean()),
             TyBot => Bottom,
             ref x => fail!("Unimplemented type {:?}", x),
@@ -701,7 +714,7 @@ fn clean(&self) -> Type {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct StructField {
-    type_: Type,
+    pub type_: Type,
 }
 
 impl Clean<Item> for ast::StructField {
@@ -733,10 +746,10 @@ fn clean(&self) -> Option<Visibility> {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Struct {
-    struct_type: doctree::StructType,
-    generics: Generics,
-    fields: Vec<Item> ,
-    fields_stripped: bool,
+    pub struct_type: doctree::StructType,
+    pub generics: Generics,
+    pub fields: Vec<Item>,
+    pub fields_stripped: bool,
 }
 
 impl Clean<Item> for doctree::Struct {
@@ -762,9 +775,9 @@ fn clean(&self) -> Item {
 /// only as a variant in an enum.
 #[deriving(Clone, Encodable, Decodable)]
 pub struct VariantStruct {
-    struct_type: doctree::StructType,
-    fields: Vec<Item> ,
-    fields_stripped: bool,
+    pub struct_type: doctree::StructType,
+    pub fields: Vec<Item>,
+    pub fields_stripped: bool,
 }
 
 impl Clean<VariantStruct> for syntax::ast::StructDef {
@@ -779,9 +792,9 @@ fn clean(&self) -> VariantStruct {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Enum {
-    variants: Vec<Item> ,
-    generics: Generics,
-    variants_stripped: bool,
+    pub variants: Vec<Item>,
+    pub generics: Generics,
+    pub variants_stripped: bool,
 }
 
 impl Clean<Item> for doctree::Enum {
@@ -803,7 +816,7 @@ fn clean(&self) -> Item {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Variant {
-    kind: VariantKind,
+    pub kind: VariantKind,
 }
 
 impl Clean<Item> for doctree::Variant {
@@ -845,11 +858,11 @@ fn clean(&self) -> VariantKind {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Span {
-    filename: ~str,
-    loline: uint,
-    locol: uint,
-    hiline: uint,
-    hicol: uint,
+    pub filename: ~str,
+    pub loline: uint,
+    pub locol: uint,
+    pub hiline: uint,
+    pub hicol: uint,
 }
 
 impl Clean<Span> for syntax::codemap::Span {
@@ -870,8 +883,8 @@ fn clean(&self) -> Span {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Path {
-    global: bool,
-    segments: Vec<PathSegment> ,
+    pub global: bool,
+    pub segments: Vec<PathSegment>,
 }
 
 impl Clean<Path> for ast::Path {
@@ -885,9 +898,9 @@ fn clean(&self) -> Path {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct PathSegment {
-    name: ~str,
-    lifetimes: Vec<Lifetime> ,
-    types: Vec<Type> ,
+    pub name: ~str,
+    pub lifetimes: Vec<Lifetime>,
+    pub types: Vec<Type>,
 }
 
 impl Clean<PathSegment> for ast::PathSegment {
@@ -903,7 +916,7 @@ fn clean(&self) -> PathSegment {
 fn path_to_str(p: &ast::Path) -> ~str {
     use syntax::parse::token;
 
-    let mut s = ~"";
+    let mut s = StrBuf::new();
     let mut first = true;
     for i in p.segments.iter().map(|x| token::get_ident(x.identifier)) {
         if !first || p.global {
@@ -913,7 +926,7 @@ fn path_to_str(p: &ast::Path) -> ~str {
         }
         s.push_str(i.get());
     }
-    s
+    s.into_owned()
 }
 
 impl Clean<~str> for ast::Ident {
@@ -924,8 +937,8 @@ fn clean(&self) -> ~str {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Typedef {
-    type_: Type,
-    generics: Generics,
+    pub type_: Type,
+    pub generics: Generics,
 }
 
 impl Clean<Item> for doctree::Typedef {
@@ -946,34 +959,34 @@ fn clean(&self) -> Item {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct BareFunctionDecl {
-    purity: ast::Purity,
-    generics: Generics,
-    decl: FnDecl,
-    abi: ~str
+    pub fn_style: ast::FnStyle,
+    pub generics: Generics,
+    pub decl: FnDecl,
+    pub abi: ~str,
 }
 
 impl Clean<BareFunctionDecl> for ast::BareFnTy {
     fn clean(&self) -> BareFunctionDecl {
         BareFunctionDecl {
-            purity: self.purity,
+            fn_style: self.fn_style,
             generics: Generics {
                 lifetimes: self.lifetimes.clean().move_iter().collect(),
                 type_params: Vec::new(),
             },
             decl: self.decl.clean(),
-            abi: self.abis.to_str(),
+            abi: self.abi.to_str(),
         }
     }
 }
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Static {
-    type_: Type,
-    mutability: Mutability,
+    pub type_: Type,
+    pub mutability: Mutability,
     /// It's useful to have the value of a static documented, but I have no
     /// desire to represent expressions (that'd basically be all of the AST,
     /// which is huge!). So, have a string.
-    expr: ~str,
+    pub expr: ~str,
 }
 
 impl Clean<Item> for doctree::Static {
@@ -1011,14 +1024,26 @@ fn clean(&self) -> Mutability {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Impl {
-    generics: Generics,
-    trait_: Option<Type>,
-    for_: Type,
-    methods: Vec<Item> ,
+    pub generics: Generics,
+    pub trait_: Option<Type>,
+    pub for_: Type,
+    pub methods: Vec<Item>,
+    pub derived: bool,
 }
 
 impl Clean<Item> for doctree::Impl {
     fn clean(&self) -> Item {
+        let mut derived = false;
+        for attr in self.attrs.iter() {
+            match attr.node.value.node {
+                ast::MetaWord(ref s) => {
+                    if s.get() == "automatically_derived" {
+                        derived = true;
+                    }
+                }
+                _ => {}
+            }
+        }
         Item {
             name: None,
             attrs: self.attrs.clean(),
@@ -1030,6 +1055,7 @@ fn clean(&self) -> Item {
                 trait_: self.trait_.clean(),
                 for_: self.for_.clean(),
                 methods: self.methods.clean(),
+                derived: derived,
             }),
         }
     }
@@ -1037,7 +1063,7 @@ fn clean(&self) -> Item {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct ViewItem {
-    inner: ViewItemInner
+    pub inner: ViewItemInner,
 }
 
 impl Clean<Item> for ast::ViewItem {
@@ -1090,8 +1116,8 @@ pub enum ViewPath {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct ImportSource {
-    path: Path,
-    did: Option<ast::DefId>,
+    pub path: Path,
+    pub did: Option<ast::DefId>,
 }
 
 impl Clean<ViewPath> for ast::ViewPath {
@@ -1111,8 +1137,8 @@ fn clean(&self) -> ViewPath {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct ViewListIdent {
-    name: ~str,
-    source: Option<ast::DefId>,
+    pub name: ~str,
+    pub source: Option<ast::DefId>,
 }
 
 impl Clean<ViewListIdent> for ast::PathListIdent {
@@ -1137,14 +1163,14 @@ fn clean(&self) -> Item {
                 ForeignFunctionItem(Function {
                     decl: decl.clean(),
                     generics: generics.clean(),
-                    purity: ast::ExternFn,
+                    fn_style: ast::ExternFn,
                 })
             }
             ast::ForeignItemStatic(ref ty, mutbl) => {
                 ForeignStaticItem(Static {
                     type_: ty.clean(),
                     mutability: if mutbl {Mutable} else {Immutable},
-                    expr: ~"",
+                    expr: "".to_owned(),
                 })
             }
         };
@@ -1171,7 +1197,7 @@ fn to_src(&self) -> ~str {
         let cm = local_data::get(super::ctxtkey, |x| x.unwrap().clone()).sess().codemap().clone();
         let sn = match cm.span_to_snippet(*self) {
             Some(x) => x,
-            None    => ~""
+            None    => "".to_owned()
         };
         debug!("got snippet {}", sn);
         sn
@@ -1181,15 +1207,15 @@ fn to_src(&self) -> ~str {
 fn lit_to_str(lit: &ast::Lit) -> ~str {
     match lit.node {
         ast::LitStr(ref st, _) => st.get().to_owned(),
-        ast::LitBinary(ref data) => format!("{:?}", data.deref().as_slice()),
-        ast::LitChar(c) => ~"'" + std::char::from_u32(c).unwrap().to_str() + "'",
+        ast::LitBinary(ref data) => format!("{:?}", data.as_slice()),
+        ast::LitChar(c) => "'".to_owned() + std::char::from_u32(c).unwrap().to_str() + "'",
         ast::LitInt(i, _t) => i.to_str(),
         ast::LitUint(u, _t) => u.to_str(),
         ast::LitIntUnsuffixed(i) => i.to_str(),
         ast::LitFloat(ref f, _t) => f.get().to_str(),
         ast::LitFloatUnsuffixed(ref f) => f.get().to_str(),
         ast::LitBool(b) => b.to_str(),
-        ast::LitNil => ~"",
+        ast::LitNil => "".to_owned(),
     }
 }
 
@@ -1198,19 +1224,19 @@ fn name_from_pat(p: &ast::Pat) -> ~str {
     debug!("Trying to get a name from pattern: {:?}", p);
 
     match p.node {
-        PatWild => ~"_",
-        PatWildMulti => ~"..",
+        PatWild => "_".to_owned(),
+        PatWildMulti => "..".to_owned(),
         PatIdent(_, ref p, _) => path_to_str(p),
         PatEnum(ref p, _) => path_to_str(p),
         PatStruct(..) => fail!("tried to get argument name from pat_struct, \
                                 which is not allowed in function arguments"),
-        PatTup(..) => ~"(tuple arg NYI)",
+        PatTup(..) => "(tuple arg NYI)".to_owned(),
         PatUniq(p) => name_from_pat(p),
         PatRegion(p) => name_from_pat(p),
         PatLit(..) => {
             warn!("tried to get argument name from PatLit, \
                   which is silly in function arguments");
-            ~"()"
+            "()".to_owned()
         },
         PatRange(..) => fail!("tried to get argument name from PatRange, \
                               which is not allowed in function arguments"),
@@ -1229,16 +1255,15 @@ fn resolve_type(path: Path, tpbs: Option<Vec<TyParamBound> >,
         core::NotTyped(_) => return Bool
     };
     debug!("searching for {:?} in defmap", id);
-    let def_map = tycx.def_map.borrow();
-    let d = match def_map.get().find(&id) {
-        Some(k) => k,
+    let d = match tycx.def_map.borrow().find(&id) {
+        Some(&k) => k,
         None => {
             debug!("could not find {:?} in defmap (`{}`)", id, tycx.map.node_to_str(id));
             fail!("Unexpected failure: unresolved id not in defmap (this is a bug!)")
         }
     };
 
-    let (def_id, kind) = match *d {
+    let (def_id, kind) = match d {
         ast::DefFn(i, _) => (i, TypeFunction),
         ast::DefSelfTy(i) => return Self(i),
         ast::DefTy(i) => (i, TypeEnum),
@@ -1285,8 +1310,7 @@ fn resolve_def(id: ast::NodeId) -> Option<ast::DefId> {
     let cx = local_data::get(super::ctxtkey, |x| *x.unwrap());
     match cx.maybe_typed {
         core::Typed(ref tcx) => {
-            let def_map = tcx.def_map.borrow();
-            def_map.get().find(&id).map(|&d| ast_util::def_id_of_def(d))
+            tcx.def_map.borrow().find(&id).map(|&d| ast_util::def_id_of_def(d))
         }
         core::NotTyped(_) => None
     }
@@ -1294,7 +1318,7 @@ fn resolve_def(id: ast::NodeId) -> Option<ast::DefId> {
 
 #[deriving(Clone, Encodable, Decodable)]
 pub struct Macro {
-    source: ~str,
+    pub source: ~str,
 }
 
 impl Clean<Item> for doctree::Macro {