rust/src/librustdoc/doctree.rs

181 lines
4.0 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 15:28:54 -05: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 09:14:35 -05:00
use syntax::codemap::Span;
2013-08-15 15:28:54 -05:00
use syntax::ast;
2013-09-05 09:14:35 -05:00
use syntax::ast::{Ident, NodeId};
2013-08-15 15:28:54 -05:00
pub struct Module {
2013-09-05 09:14:35 -05:00
name: Option<Ident>,
2013-08-15 15:28:54 -05:00
attrs: ~[ast::Attribute],
2013-09-05 09:14:35 -05:00
where: Span,
2013-08-15 15:28:54 -05:00
structs: ~[Struct],
enums: ~[Enum],
fns: ~[Function],
mods: ~[Module],
id: NodeId,
typedefs: ~[Typedef],
statics: ~[Static],
traits: ~[Trait],
vis: ast::Visibility,
2013-08-15 15:28:54 -05:00
impls: ~[Impl],
foreigns: ~[ast::ForeignMod],
view_items: ~[ast::ViewItem],
macros: ~[Macro],
2013-08-15 15:28:54 -05:00
}
impl Module {
2013-09-05 09:14:35 -05:00
pub fn new(name: Option<Ident>) -> Module {
2013-08-15 15:28:54 -05:00
Module {
name : name,
id: 0,
vis: ast::Private,
where: syntax::codemap::DUMMY_SP,
2013-08-15 15:28:54 -05:00
attrs : ~[],
structs : ~[],
enums : ~[],
fns : ~[],
mods : ~[],
typedefs : ~[],
statics : ~[],
traits : ~[],
impls : ~[],
view_items : ~[],
foreigns : ~[],
macros : ~[],
2013-08-15 15:28:54 -05:00
}
}
}
#[deriving(Show, Clone, Encodable, Decodable)]
2013-08-15 15:28:54 -05: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 15:28:54 -05:00
}
pub struct Struct {
vis: ast::Visibility,
2013-08-15 15:28:54 -05:00
id: NodeId,
struct_type: StructType,
2013-09-05 09:14:35 -05:00
name: Ident,
2013-08-15 15:28:54 -05:00
generics: ast::Generics,
attrs: ~[ast::Attribute],
fields: ~[ast::StructField],
2013-09-05 09:14:35 -05:00
where: Span,
2013-08-15 15:28:54 -05:00
}
pub struct Enum {
vis: ast::Visibility,
2013-08-15 15:28:54 -05:00
variants: ~[Variant],
generics: ast::Generics,
attrs: ~[ast::Attribute],
id: NodeId,
2013-09-05 09:14:35 -05:00
where: Span,
name: Ident,
2013-08-15 15:28:54 -05:00
}
pub struct Variant {
2013-09-05 09:14:35 -05:00
name: Ident,
2013-08-15 15:28:54 -05:00
attrs: ~[ast::Attribute],
kind: ast::VariantKind,
2013-08-15 15:28:54 -05:00
id: ast::NodeId,
vis: ast::Visibility,
2013-09-05 09:14:35 -05:00
where: Span,
2013-08-15 15:28:54 -05:00
}
pub struct Function {
decl: ast::FnDecl,
2013-08-15 15:28:54 -05:00
attrs: ~[ast::Attribute],
id: NodeId,
2013-09-05 09:14:35 -05:00
name: Ident,
vis: ast::Visibility,
purity: ast::Purity,
2013-09-05 09:14:35 -05:00
where: Span,
2013-08-15 15:28:54 -05:00
generics: ast::Generics,
}
pub struct Typedef {
ty: ast::P<ast::Ty>,
2013-08-15 15:28:54 -05:00
gen: ast::Generics,
2013-09-05 09:14:35 -05:00
name: Ident,
2013-08-15 15:28:54 -05:00
id: ast::NodeId,
attrs: ~[ast::Attribute],
2013-09-05 09:14:35 -05:00
where: Span,
vis: ast::Visibility,
2013-08-15 15:28:54 -05:00
}
pub struct Static {
type_: ast::P<ast::Ty>,
2013-09-05 09:14:35 -05:00
mutability: ast::Mutability,
expr: @ast::Expr,
name: Ident,
2013-08-15 15:28:54 -05:00
attrs: ~[ast::Attribute],
vis: ast::Visibility,
2013-08-15 15:28:54 -05:00
id: ast::NodeId,
2013-09-05 09:14:35 -05:00
where: Span,
2013-08-15 15:28:54 -05:00
}
pub struct Trait {
2013-09-05 09:14:35 -05:00
name: Ident,
methods: ~[ast::TraitMethod], //should be TraitMethod
2013-08-15 15:28:54 -05:00
generics: ast::Generics,
parents: ~[ast::TraitRef],
2013-08-15 15:28:54 -05:00
attrs: ~[ast::Attribute],
id: ast::NodeId,
2013-09-05 09:14:35 -05:00
where: Span,
vis: ast::Visibility,
2013-08-15 15:28:54 -05:00
}
pub struct Impl {
generics: ast::Generics,
trait_: Option<ast::TraitRef>,
for_: ast::P<ast::Ty>,
methods: ~[@ast::Method],
2013-08-15 15:28:54 -05:00
attrs: ~[ast::Attribute],
2013-09-05 09:14:35 -05:00
where: Span,
vis: ast::Visibility,
2013-08-15 15:28:54 -05:00
id: ast::NodeId,
}
pub struct Macro {
name: Ident,
id: ast::NodeId,
attrs: ~[ast::Attribute],
where: Span,
}
pub fn struct_type_from_def(sd: &ast::StructDef) -> StructType {
2013-08-15 15:28:54 -05:00
if sd.ctor_id.is_some() {
// We are in a tuple-struct
match sd.fields.len() {
0 => Unit,
1 => Newtype,
_ => Tuple
}
} else {
Plain
}
}