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;
}
-impl<T: Clean<U>, U> Clean<~[U]> for ~[T] {
- fn clean(&self) -> ~[U] {
- self.iter().map(|x| x.clean()).collect()
- }
-}
-
impl<T: Clean<U>, U> Clean<Vec<U>> for Vec<T> {
fn clean(&self) -> Vec<U> {
self.iter().map(|x| x.clean()).collect()
#[deriving(Clone, Encodable, Decodable)]
pub struct Crate {
- name: ~str,
- module: Option<Item>,
- externs: ~[(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> {
use syntax::attr::find_crateid;
let cx = local_data::get(super::ctxtkey, |x| *x.unwrap());
- let mut externs = ~[];
+ let mut externs = Vec::new();
cx.sess().cstore.iter_crate_data(|n, meta| {
externs.push((n, meta.clean()));
});
Crate {
- name: match find_crateid(self.attrs) {
+ name: match find_crateid(self.attrs.as_slice()) {
Some(n) => n.name,
None => fail!("rustdoc requires a `crate_id` crate attribute"),
},
#[deriving(Clone, Encodable, Decodable)]
pub struct ExternalCrate {
- name: ~str,
- attrs: ~[Attribute],
+ pub name: ~str,
+ pub attrs: Vec<Attribute>,
}
impl Clean<ExternalCrate> for cstore::crate_metadata {
#[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: ~[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 {
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: ~[Item],
- is_crate: bool,
+ pub items: Vec<Item>,
+ pub is_crate: bool,
}
impl Clean<Item> for doctree::Module {
let name = if self.name.is_some() {
self.name.unwrap().clean()
} else {
- ~""
+ "".to_owned()
};
- let mut foreigns = ~[];
+ let mut foreigns = Vec::new();
for subforeigns in self.foreigns.clean().move_iter() {
for foreign in subforeigns.move_iter() {
foreigns.push(foreign)
}
}
- let items: ~[~[Item]] = ~[
+ let items: Vec<Vec<Item> > = vec!(
self.structs.clean().move_iter().collect(),
self.enums.clean().move_iter().collect(),
self.fns.clean().move_iter().collect(),
self.impls.clean().move_iter().collect(),
self.view_items.clean().move_iter().collect(),
self.macros.clean().move_iter().collect()
- ];
+ );
Item {
name: Some(name),
attrs: self.attrs.clean(),
id: self.id,
inner: ModuleItem(Module {
is_crate: self.is_crate,
- items: items.concat_vec(),
+ items: items.iter()
+ .flat_map(|x| x.iter().map(|x| (*x).clone()))
+ .collect(),
})
}
}
#[deriving(Clone, Encodable, Decodable)]
pub enum Attribute {
Word(~str),
- List(~str, ~[Attribute]),
+ List(~str, Vec<Attribute> ),
NameValue(~str, ~str)
}
#[deriving(Clone, Encodable, Decodable)]
pub struct TyParam {
- name: ~str,
- id: ast::NodeId,
- bounds: ~[TyParamBound]
+ pub name: ~str,
+ pub id: ast::NodeId,
+ pub bounds: Vec<TyParamBound>,
}
impl Clean<TyParam> for ast::TyParam {
// maybe use a Generic enum and use ~[Generic]?
#[deriving(Clone, Encodable, Decodable)]
pub struct Generics {
- lifetimes: ~[Lifetime],
- type_params: ~[TyParam]
+ pub lifetimes: Vec<Lifetime>,
+ pub type_params: Vec<TyParam>,
}
impl Clean<Generics> for ast::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 {
},
output: (self.decl.output.clean()),
cf: self.decl.cf.clean(),
- attrs: ~[]
+ attrs: Vec::new()
};
Item {
name: Some(self.ident.clean()),
inner: MethodItem(Method {
generics: self.generics.clean(),
self_: self.explicit_self.clean(),
- purity: self.purity.clone(),
+ fn_style: self.fn_style.clone(),
decl: decl,
}),
}
#[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 {
},
output: (self.decl.output.clean()),
cf: self.decl.cf.clean(),
- attrs: ~[]
+ attrs: Vec::new()
};
Item {
name: Some(self.ident.clean()),
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(),
#[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 {
inner: FunctionItem(Function {
decl: self.decl.clean(),
generics: self.generics.clean(),
- purity: self.purity,
+ fn_style: self.fn_style,
}),
}
}
#[deriving(Clone, Encodable, Decodable)]
pub struct ClosureDecl {
- sigil: ast::Sigil,
- region: Option<Lifetime>,
- lifetimes: ~[Lifetime],
- decl: FnDecl,
- onceness: ast::Onceness,
- purity: ast::Purity,
- bounds: ~[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 => ~[]
+ None => Vec::new()
},
}
}
#[deriving(Clone, Encodable, Decodable)]
pub struct FnDecl {
- inputs: Arguments,
- output: Type,
- cf: RetStyle,
- attrs: ~[Attribute]
+ pub inputs: Arguments,
+ pub output: Type,
+ pub cf: RetStyle,
+ pub attrs: Vec<Attribute>,
}
#[deriving(Clone, Encodable, Decodable)]
pub struct Arguments {
- values: ~[Argument],
+ pub values: Vec<Argument>,
}
impl Clean<FnDecl> for ast::FnDecl {
},
output: (self.output.clean()),
cf: self.cf.clean(),
- attrs: ~[]
+ attrs: Vec::new()
}
}
}
#[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 {
#[deriving(Clone, Encodable, Decodable)]
pub struct Trait {
- methods: ~[TraitMethod],
- generics: Generics,
- parents: ~[Type],
+ pub methods: Vec<TraitMethod>,
+ pub generics: Generics,
+ pub parents: Vec<Type>,
}
impl Clean<Item> for doctree::Trait {
pub enum Type {
/// structs/enums/traits (anything that'd be an ast::TyPath)
ResolvedPath {
- path: Path,
- typarams: Option<~[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<~[TyParamBound]>,
- fqn: ~[~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),
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(~[Type]),
+ Tuple(Vec<Type> ),
Vector(~Type),
FixedVector(~Type, ~str),
String,
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
}
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),
#[deriving(Clone, Encodable, Decodable)]
pub struct StructField {
- type_: Type,
+ pub type_: Type,
}
impl Clean<Item> for ast::StructField {
#[deriving(Clone, Encodable, Decodable)]
pub struct Struct {
- struct_type: doctree::StructType,
- generics: Generics,
- fields: ~[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 {
/// only as a variant in an enum.
#[deriving(Clone, Encodable, Decodable)]
pub struct VariantStruct {
- struct_type: doctree::StructType,
- fields: ~[Item],
- fields_stripped: bool,
+ pub struct_type: doctree::StructType,
+ pub fields: Vec<Item>,
+ pub fields_stripped: bool,
}
impl Clean<VariantStruct> for syntax::ast::StructDef {
#[deriving(Clone, Encodable, Decodable)]
pub struct Enum {
- variants: ~[Item],
- generics: Generics,
- variants_stripped: bool,
+ pub variants: Vec<Item>,
+ pub generics: Generics,
+ pub variants_stripped: bool,
}
impl Clean<Item> for doctree::Enum {
#[deriving(Clone, Encodable, Decodable)]
pub struct Variant {
- kind: VariantKind,
+ pub kind: VariantKind,
}
impl Clean<Item> for doctree::Variant {
#[deriving(Clone, Encodable, Decodable)]
pub enum VariantKind {
CLikeVariant,
- TupleVariant(~[Type]),
+ TupleVariant(Vec<Type> ),
StructVariant(VariantStruct),
}
#[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 {
#[deriving(Clone, Encodable, Decodable)]
pub struct Path {
- global: bool,
- segments: ~[PathSegment],
+ pub global: bool,
+ pub segments: Vec<PathSegment>,
}
impl Clean<Path> for ast::Path {
#[deriving(Clone, Encodable, Decodable)]
pub struct PathSegment {
- name: ~str,
- lifetimes: ~[Lifetime],
- types: ~[Type],
+ pub name: ~str,
+ pub lifetimes: Vec<Lifetime>,
+ pub types: Vec<Type>,
}
impl Clean<PathSegment> for ast::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 {
}
s.push_str(i.get());
}
- s
+ s.into_owned()
}
impl Clean<~str> for ast::Ident {
#[deriving(Clone, Encodable, Decodable)]
pub struct Typedef {
- type_: Type,
- generics: Generics,
+ pub type_: Type,
+ pub generics: Generics,
}
impl Clean<Item> for doctree::Typedef {
#[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: ~[],
+ 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 {
#[deriving(Clone, Encodable, Decodable)]
pub struct Impl {
- generics: Generics,
- trait_: Option<Type>,
- for_: Type,
- methods: ~[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(),
trait_: self.trait_.clean(),
for_: self.for_.clean(),
methods: self.methods.clean(),
+ derived: derived,
}),
}
}
#[deriving(Clone, Encodable, Decodable)]
pub struct ViewItem {
- inner: ViewItemInner
+ pub inner: ViewItemInner,
}
impl Clean<Item> for ast::ViewItem {
#[deriving(Clone, Encodable, Decodable)]
pub enum ViewItemInner {
ExternCrate(~str, Option<~str>, ast::NodeId),
- Import(~[ViewPath])
+ Import(Vec<ViewPath>)
}
impl Clean<ViewItemInner> for ast::ViewItem_ {
// use source::*;
GlobImport(ImportSource),
// use source::{a, b, c};
- ImportList(ImportSource, ~[ViewListIdent]),
+ ImportList(ImportSource, Vec<ViewListIdent> ),
}
#[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 {
#[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 {
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(),
})
}
};
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
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(),
}
}
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"),
}
/// Given a Type, resolve it using the def_map
-fn resolve_type(path: Path, tpbs: Option<~[TyParamBound]>,
+fn resolve_type(path: Path, tpbs: Option<Vec<TyParamBound> >,
id: ast::NodeId) -> Type {
let cx = local_data::get(super::ctxtkey, |x| *x.unwrap());
let tycx = match cx.maybe_typed {
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),
ResolvedPath{ path: path, typarams: tpbs, id: def_id.node }
} else {
let fqn = csearch::get_item_path(tycx, def_id);
- let fqn = fqn.move_iter().map(|i| i.to_str()).to_owned_vec();
- ExternalPath{ path: path, typarams: tpbs, fqn: fqn, kind: kind,
- krate: def_id.krate }
+ let fqn = fqn.move_iter().map(|i| i.to_str()).collect();
+ ExternalPath {
+ path: path,
+ typarams: tpbs,
+ fqn: fqn,
+ kind: kind,
+ krate: def_id.krate,
+ }
}
}
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
}
#[deriving(Clone, Encodable, Decodable)]
pub struct Macro {
- source: ~str,
+ pub source: ~str,
}
impl Clean<Item> for doctree::Macro {