rust/src/librustdoc/doctree.rs

183 lines
4.2 KiB
Rust
Raw Normal View History

// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2013-08-15 16:28:54 -04:00
//! This module is used to store stuff from Rust's AST in a more convenient
//! manner (and with prettier names) before cleaning.
use syntax;
2013-09-05 10:14:35 -04:00
use syntax::codemap::Span;
2013-08-15 16:28:54 -04:00
use syntax::ast;
2013-09-05 10:14:35 -04:00
use syntax::ast::{Ident, NodeId};
2013-08-15 16:28:54 -04:00
pub struct Module {
2013-09-05 10:14:35 -04:00
name: Option<Ident>,
attrs: Vec<ast::Attribute> ,
2013-09-05 10:14:35 -04:00
where: Span,
structs: Vec<Struct> ,
enums: Vec<Enum> ,
fns: Vec<Function> ,
mods: Vec<Module> ,
2013-08-15 16:28:54 -04:00
id: NodeId,
typedefs: Vec<Typedef> ,
statics: Vec<Static> ,
traits: Vec<Trait> ,
vis: ast::Visibility,
impls: Vec<Impl> ,
foreigns: Vec<ast::ForeignMod> ,
view_items: Vec<ast::ViewItem> ,
macros: Vec<Macro> ,
is_crate: bool,
2013-08-15 16:28:54 -04:00
}
impl Module {
2013-09-05 10:14:35 -04:00
pub fn new(name: Option<Ident>) -> Module {
2013-08-15 16:28:54 -04:00
Module {
name : name,
id: 0,
vis: ast::Private,
where: syntax::codemap::DUMMY_SP,
attrs : Vec::new(),
structs : Vec::new(),
enums : Vec::new(),
fns : Vec::new(),
mods : Vec::new(),
typedefs : Vec::new(),
statics : Vec::new(),
traits : Vec::new(),
impls : Vec::new(),
view_items : Vec::new(),
foreigns : Vec::new(),
macros : Vec::new(),
is_crate : false,
2013-08-15 16:28:54 -04:00
}
}
}
#[deriving(Show, Clone, Encodable, Decodable)]
2013-08-15 16:28:54 -04:00
pub enum StructType {
/// A normal struct
Plain,
/// A tuple struct
Tuple,
/// A newtype struct (tuple struct with one element)
Newtype,
/// A unit struct
Unit
}
pub enum TypeBound {
RegionBound,
TraitBound(ast::TraitRef)
2013-08-15 16:28:54 -04:00
}
pub struct Struct {
vis: ast::Visibility,
2013-08-15 16:28:54 -04:00
id: NodeId,
struct_type: StructType,
2013-09-05 10:14:35 -04:00
name: Ident,
2013-08-15 16:28:54 -04:00
generics: ast::Generics,
attrs: Vec<ast::Attribute> ,
fields: Vec<ast::StructField> ,
2013-09-05 10:14:35 -04:00
where: Span,
2013-08-15 16:28:54 -04:00
}
pub struct Enum {
vis: ast::Visibility,
variants: Vec<Variant> ,
2013-08-15 16:28:54 -04:00
generics: ast::Generics,
attrs: Vec<ast::Attribute> ,
2013-08-15 16:28:54 -04:00
id: NodeId,
2013-09-05 10:14:35 -04:00
where: Span,
name: Ident,
2013-08-15 16:28:54 -04:00
}
pub struct Variant {
2013-09-05 10:14:35 -04:00
name: Ident,
attrs: Vec<ast::Attribute> ,
kind: ast::VariantKind,
2013-08-15 16:28:54 -04:00
id: ast::NodeId,
vis: ast::Visibility,
2013-09-05 10:14:35 -04:00
where: Span,
2013-08-15 16:28:54 -04:00
}
pub struct Function {
decl: ast::FnDecl,
attrs: Vec<ast::Attribute> ,
2013-08-15 16:28:54 -04:00
id: NodeId,
2013-09-05 10:14:35 -04:00
name: Ident,
vis: ast::Visibility,
purity: ast::Purity,
2013-09-05 10:14:35 -04:00
where: Span,
2013-08-15 16:28:54 -04:00
generics: ast::Generics,
}
pub struct Typedef {
ty: ast::P<ast::Ty>,
2013-08-15 16:28:54 -04:00
gen: ast::Generics,
2013-09-05 10:14:35 -04:00
name: Ident,
2013-08-15 16:28:54 -04:00
id: ast::NodeId,
attrs: Vec<ast::Attribute> ,
2013-09-05 10:14:35 -04:00
where: Span,
vis: ast::Visibility,
2013-08-15 16:28:54 -04:00
}
pub struct Static {
type_: ast::P<ast::Ty>,
2013-09-05 10:14:35 -04:00
mutability: ast::Mutability,
expr: @ast::Expr,
name: Ident,
attrs: Vec<ast::Attribute> ,
vis: ast::Visibility,
2013-08-15 16:28:54 -04:00
id: ast::NodeId,
2013-09-05 10:14:35 -04:00
where: Span,
2013-08-15 16:28:54 -04:00
}
pub struct Trait {
2013-09-05 10:14:35 -04:00
name: Ident,
methods: Vec<ast::TraitMethod> , //should be TraitMethod
2013-08-15 16:28:54 -04:00
generics: ast::Generics,
parents: Vec<ast::TraitRef> ,
attrs: Vec<ast::Attribute> ,
2013-08-15 16:28:54 -04:00
id: ast::NodeId,
2013-09-05 10:14:35 -04:00
where: Span,
vis: ast::Visibility,
2013-08-15 16:28:54 -04:00
}
pub struct Impl {
generics: ast::Generics,
trait_: Option<ast::TraitRef>,
for_: ast::P<ast::Ty>,
methods: Vec<@ast::Method> ,
attrs: Vec<ast::Attribute> ,
2013-09-05 10:14:35 -04:00
where: Span,
vis: ast::Visibility,
2013-08-15 16:28:54 -04:00
id: ast::NodeId,
}
pub struct Macro {
name: Ident,
id: ast::NodeId,
attrs: Vec<ast::Attribute> ,
where: Span,
}
pub fn struct_type_from_def(sd: &ast::StructDef) -> StructType {
2013-08-15 16:28:54 -04:00
if sd.ctor_id.is_some() {
// We are in a tuple-struct
match sd.fields.len() {
0 => Unit,
1 => Newtype,
_ => Tuple
}
} else {
Plain
}
}