]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/doctree.rs
c5849f5aa28b6376531d1d26616de52f9608209e
[rust.git] / src / librustdoc / doctree.rs
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.
4 //
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.
10
11 //! This module is used to store stuff from Rust's AST in a more convenient
12 //! manner (and with prettier names) before cleaning.
13
14 use syntax;
15 use syntax::codemap::Span;
16 use syntax::ast;
17 use syntax::ast::{Ident, NodeId};
18
19 pub struct Module {
20     name: Option<Ident>,
21     attrs: ~[ast::Attribute],
22     where: Span,
23     structs: ~[Struct],
24     enums: ~[Enum],
25     fns: ~[Function],
26     mods: ~[Module],
27     id: NodeId,
28     typedefs: ~[Typedef],
29     statics: ~[Static],
30     traits: ~[Trait],
31     vis: ast::Visibility,
32     impls: ~[Impl],
33     foreigns: ~[ast::ForeignMod],
34     view_items: ~[ast::ViewItem],
35     macros: ~[Macro],
36     is_crate: bool,
37 }
38
39 impl Module {
40     pub fn new(name: Option<Ident>) -> Module {
41         Module {
42             name       : name,
43             id: 0,
44             vis: ast::Private,
45             where: syntax::codemap::DUMMY_SP,
46             attrs      : ~[],
47             structs    : ~[],
48             enums      : ~[],
49             fns        : ~[],
50             mods       : ~[],
51             typedefs   : ~[],
52             statics    : ~[],
53             traits     : ~[],
54             impls      : ~[],
55             view_items : ~[],
56             foreigns   : ~[],
57             macros     : ~[],
58             is_crate   : false,
59         }
60     }
61 }
62
63 #[deriving(Show, Clone, Encodable, Decodable)]
64 pub enum StructType {
65     /// A normal struct
66     Plain,
67     /// A tuple struct
68     Tuple,
69     /// A newtype struct (tuple struct with one element)
70     Newtype,
71     /// A unit struct
72     Unit
73 }
74
75 pub enum TypeBound {
76     RegionBound,
77     TraitBound(ast::TraitRef)
78 }
79
80 pub struct Struct {
81     vis: ast::Visibility,
82     id: NodeId,
83     struct_type: StructType,
84     name: Ident,
85     generics: ast::Generics,
86     attrs: ~[ast::Attribute],
87     fields: ~[ast::StructField],
88     where: Span,
89 }
90
91 pub struct Enum {
92     vis: ast::Visibility,
93     variants: ~[Variant],
94     generics: ast::Generics,
95     attrs: ~[ast::Attribute],
96     id: NodeId,
97     where: Span,
98     name: Ident,
99 }
100
101 pub struct Variant {
102     name: Ident,
103     attrs: ~[ast::Attribute],
104     kind: ast::VariantKind,
105     id: ast::NodeId,
106     vis: ast::Visibility,
107     where: Span,
108 }
109
110 pub struct Function {
111     decl: ast::FnDecl,
112     attrs: ~[ast::Attribute],
113     id: NodeId,
114     name: Ident,
115     vis: ast::Visibility,
116     purity: ast::Purity,
117     where: Span,
118     generics: ast::Generics,
119 }
120
121 pub struct Typedef {
122     ty: ast::P<ast::Ty>,
123     gen: ast::Generics,
124     name: Ident,
125     id: ast::NodeId,
126     attrs: ~[ast::Attribute],
127     where: Span,
128     vis: ast::Visibility,
129 }
130
131 pub struct Static {
132     type_: ast::P<ast::Ty>,
133     mutability: ast::Mutability,
134     expr: @ast::Expr,
135     name: Ident,
136     attrs: ~[ast::Attribute],
137     vis: ast::Visibility,
138     id: ast::NodeId,
139     where: Span,
140 }
141
142 pub struct Trait {
143     name: Ident,
144     methods: ~[ast::TraitMethod], //should be TraitMethod
145     generics: ast::Generics,
146     parents: ~[ast::TraitRef],
147     attrs: ~[ast::Attribute],
148     id: ast::NodeId,
149     where: Span,
150     vis: ast::Visibility,
151 }
152
153 pub struct Impl {
154     generics: ast::Generics,
155     trait_: Option<ast::TraitRef>,
156     for_: ast::P<ast::Ty>,
157     methods: ~[@ast::Method],
158     attrs: ~[ast::Attribute],
159     where: Span,
160     vis: ast::Visibility,
161     id: ast::NodeId,
162 }
163
164 pub struct Macro {
165     name: Ident,
166     id: ast::NodeId,
167     attrs: ~[ast::Attribute],
168     where: Span,
169 }
170
171 pub fn struct_type_from_def(sd: &ast::StructDef) -> StructType {
172     if sd.ctor_id.is_some() {
173         // We are in a tuple-struct
174         match sd.fields.len() {
175             0 => Unit,
176             1 => Newtype,
177             _ => Tuple
178         }
179     } else {
180         Plain
181     }
182 }