]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/doctree.rs
5140cca03ea340eb1ffc0c0301330f7682f57dff
[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 pub use self::StructType::*;
14 pub use self::TypeBound::*;
15
16 use syntax;
17 use syntax::codemap::Span;
18 use syntax::abi;
19 use syntax::ast;
20 use syntax::ast::{Ident, NodeId};
21 use syntax::ptr::P;
22 use rustc_front::hir;
23 use rustc_front::attr;
24
25 pub struct Module {
26     pub name: Option<Ident>,
27     pub attrs: Vec<hir::Attribute>,
28     pub where_outer: Span,
29     pub where_inner: Span,
30     pub extern_crates: Vec<ExternCrate>,
31     pub imports: Vec<Import>,
32     pub structs: Vec<Struct>,
33     pub enums: Vec<Enum>,
34     pub fns: Vec<Function>,
35     pub mods: Vec<Module>,
36     pub id: NodeId,
37     pub typedefs: Vec<Typedef>,
38     pub statics: Vec<Static>,
39     pub constants: Vec<Constant>,
40     pub traits: Vec<Trait>,
41     pub vis: hir::Visibility,
42     pub stab: Option<attr::Stability>,
43     pub impls: Vec<Impl>,
44     pub def_traits: Vec<DefaultImpl>,
45     pub foreigns: Vec<hir::ForeignMod>,
46     pub macros: Vec<Macro>,
47     pub is_crate: bool,
48 }
49
50 impl Module {
51     pub fn new(name: Option<Ident>) -> Module {
52         Module {
53             name       : name,
54             id: 0,
55             vis: hir::Inherited,
56             stab: None,
57             where_outer: syntax::codemap::DUMMY_SP,
58             where_inner: syntax::codemap::DUMMY_SP,
59             attrs      : Vec::new(),
60             extern_crates: Vec::new(),
61             imports    : Vec::new(),
62             structs    : Vec::new(),
63             enums      : Vec::new(),
64             fns        : Vec::new(),
65             mods       : Vec::new(),
66             typedefs   : Vec::new(),
67             statics    : Vec::new(),
68             constants  : Vec::new(),
69             traits     : Vec::new(),
70             impls      : Vec::new(),
71             def_traits : Vec::new(),
72             foreigns   : Vec::new(),
73             macros     : Vec::new(),
74             is_crate   : false,
75         }
76     }
77 }
78
79 #[derive(Debug, Clone, RustcEncodable, RustcDecodable, Copy)]
80 pub enum StructType {
81     /// A normal struct
82     Plain,
83     /// A tuple struct
84     Tuple,
85     /// A newtype struct (tuple struct with one element)
86     Newtype,
87     /// A unit struct
88     Unit
89 }
90
91 pub enum TypeBound {
92     RegionBound,
93     TraitBound(hir::TraitRef)
94 }
95
96 pub struct Struct {
97     pub vis: hir::Visibility,
98     pub stab: Option<attr::Stability>,
99     pub id: NodeId,
100     pub struct_type: StructType,
101     pub name: Ident,
102     pub generics: hir::Generics,
103     pub attrs: Vec<hir::Attribute>,
104     pub fields: Vec<hir::StructField>,
105     pub whence: Span,
106 }
107
108 pub struct Enum {
109     pub vis: hir::Visibility,
110     pub stab: Option<attr::Stability>,
111     pub variants: Vec<Variant>,
112     pub generics: hir::Generics,
113     pub attrs: Vec<hir::Attribute>,
114     pub id: NodeId,
115     pub whence: Span,
116     pub name: Ident,
117 }
118
119 pub struct Variant {
120     pub name: Ident,
121     pub attrs: Vec<hir::Attribute>,
122     pub kind: hir::VariantKind,
123     pub id: ast::NodeId,
124     pub vis: hir::Visibility,
125     pub stab: Option<attr::Stability>,
126     pub whence: Span,
127 }
128
129 pub struct Function {
130     pub decl: hir::FnDecl,
131     pub attrs: Vec<hir::Attribute>,
132     pub id: NodeId,
133     pub name: Ident,
134     pub vis: hir::Visibility,
135     pub stab: Option<attr::Stability>,
136     pub unsafety: hir::Unsafety,
137     pub constness: hir::Constness,
138     pub whence: Span,
139     pub generics: hir::Generics,
140     pub abi: abi::Abi,
141 }
142
143 pub struct Typedef {
144     pub ty: P<hir::Ty>,
145     pub gen: hir::Generics,
146     pub name: Ident,
147     pub id: ast::NodeId,
148     pub attrs: Vec<hir::Attribute>,
149     pub whence: Span,
150     pub vis: hir::Visibility,
151     pub stab: Option<attr::Stability>,
152 }
153
154 #[derive(Debug)]
155 pub struct Static {
156     pub type_: P<hir::Ty>,
157     pub mutability: hir::Mutability,
158     pub expr: P<hir::Expr>,
159     pub name: Ident,
160     pub attrs: Vec<hir::Attribute>,
161     pub vis: hir::Visibility,
162     pub stab: Option<attr::Stability>,
163     pub id: ast::NodeId,
164     pub whence: Span,
165 }
166
167 pub struct Constant {
168     pub type_: P<hir::Ty>,
169     pub expr: P<hir::Expr>,
170     pub name: Ident,
171     pub attrs: Vec<hir::Attribute>,
172     pub vis: hir::Visibility,
173     pub stab: Option<attr::Stability>,
174     pub id: ast::NodeId,
175     pub whence: Span,
176 }
177
178 pub struct Trait {
179     pub unsafety: hir::Unsafety,
180     pub name: Ident,
181     pub items: Vec<P<hir::TraitItem>>, //should be TraitItem
182     pub generics: hir::Generics,
183     pub bounds: Vec<hir::TyParamBound>,
184     pub attrs: Vec<hir::Attribute>,
185     pub id: ast::NodeId,
186     pub whence: Span,
187     pub vis: hir::Visibility,
188     pub stab: Option<attr::Stability>,
189 }
190
191 pub struct Impl {
192     pub unsafety: hir::Unsafety,
193     pub polarity: hir::ImplPolarity,
194     pub generics: hir::Generics,
195     pub trait_: Option<hir::TraitRef>,
196     pub for_: P<hir::Ty>,
197     pub items: Vec<P<hir::ImplItem>>,
198     pub attrs: Vec<hir::Attribute>,
199     pub whence: Span,
200     pub vis: hir::Visibility,
201     pub stab: Option<attr::Stability>,
202     pub id: ast::NodeId,
203 }
204
205 pub struct DefaultImpl {
206     pub unsafety: hir::Unsafety,
207     pub trait_: hir::TraitRef,
208     pub id: ast::NodeId,
209     pub attrs: Vec<hir::Attribute>,
210     pub whence: Span,
211 }
212
213 pub struct Macro {
214     pub name: Ident,
215     pub id: ast::NodeId,
216     pub attrs: Vec<hir::Attribute>,
217     pub whence: Span,
218     pub stab: Option<attr::Stability>,
219     pub imported_from: Option<Ident>,
220 }
221
222 pub struct ExternCrate {
223     pub name: Ident,
224     pub path: Option<String>,
225     pub vis: hir::Visibility,
226     pub attrs: Vec<hir::Attribute>,
227     pub whence: Span,
228 }
229
230 pub struct Import {
231     pub id: NodeId,
232     pub vis: hir::Visibility,
233     pub attrs: Vec<hir::Attribute>,
234     pub node: hir::ViewPath_,
235     pub whence: Span,
236 }
237
238 pub fn struct_type_from_def(sd: &hir::StructDef) -> StructType {
239     if sd.ctor_id.is_some() {
240         // We are in a tuple-struct
241         match sd.fields.len() {
242             0 => Unit,
243             1 => Newtype,
244             _ => Tuple
245         }
246     } else {
247         Plain
248     }
249 }