]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/doctree.rs
76c11a4e7cdd8b86bd22203b1b01c77c68a90c81
[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::ast;
17 use syntax::ast::{Name, NodeId};
18 use syntax::attr;
19 use syntax::ptr::P;
20 use syntax::codemap::Spanned;
21 use syntax_pos::{self, Span};
22
23 use rustc::hir;
24 use rustc::hir::def_id::CrateNum;
25
26 pub struct Module {
27     pub name: Option<Name>,
28     pub attrs: hir::HirVec<ast::Attribute>,
29     pub where_outer: Span,
30     pub where_inner: Span,
31     pub extern_crates: Vec<ExternCrate>,
32     pub imports: Vec<Import>,
33     pub structs: Vec<Struct>,
34     pub unions: Vec<Union>,
35     pub enums: Vec<Enum>,
36     pub fns: Vec<Function>,
37     pub mods: Vec<Module>,
38     pub id: NodeId,
39     pub typedefs: Vec<Typedef>,
40     pub statics: Vec<Static>,
41     pub constants: Vec<Constant>,
42     pub traits: Vec<Trait>,
43     pub vis: hir::Visibility,
44     pub stab: Option<attr::Stability>,
45     pub depr: Option<attr::Deprecation>,
46     pub impls: Vec<Impl>,
47     pub foreigns: Vec<hir::ForeignMod>,
48     pub macros: Vec<Macro>,
49     pub is_crate: bool,
50 }
51
52 impl Module {
53     pub fn new(name: Option<Name>) -> Module {
54         Module {
55             name       : name,
56             id: ast::CRATE_NODE_ID,
57             vis: Spanned { span: syntax_pos::DUMMY_SP, node: hir::VisibilityInherited },
58             stab: None,
59             depr: None,
60             where_outer: syntax_pos::DUMMY_SP,
61             where_inner: syntax_pos::DUMMY_SP,
62             attrs      : hir::HirVec::new(),
63             extern_crates: Vec::new(),
64             imports    : Vec::new(),
65             structs    : Vec::new(),
66             unions     : Vec::new(),
67             enums      : Vec::new(),
68             fns        : Vec::new(),
69             mods       : Vec::new(),
70             typedefs   : Vec::new(),
71             statics    : Vec::new(),
72             constants  : Vec::new(),
73             traits     : Vec::new(),
74             impls      : Vec::new(),
75             foreigns   : Vec::new(),
76             macros     : Vec::new(),
77             is_crate   : false,
78         }
79     }
80 }
81
82 #[derive(Debug, Clone, RustcEncodable, RustcDecodable, Copy)]
83 pub enum StructType {
84     /// A braced struct
85     Plain,
86     /// A tuple struct
87     Tuple,
88     /// A unit struct
89     Unit,
90 }
91
92 pub enum TypeBound {
93     RegionBound,
94     TraitBound(hir::TraitRef)
95 }
96
97 pub struct Struct {
98     pub vis: hir::Visibility,
99     pub stab: Option<attr::Stability>,
100     pub depr: Option<attr::Deprecation>,
101     pub id: NodeId,
102     pub struct_type: StructType,
103     pub name: Name,
104     pub generics: hir::Generics,
105     pub attrs: hir::HirVec<ast::Attribute>,
106     pub fields: hir::HirVec<hir::StructField>,
107     pub whence: Span,
108 }
109
110 pub struct Union {
111     pub vis: hir::Visibility,
112     pub stab: Option<attr::Stability>,
113     pub depr: Option<attr::Deprecation>,
114     pub id: NodeId,
115     pub struct_type: StructType,
116     pub name: Name,
117     pub generics: hir::Generics,
118     pub attrs: hir::HirVec<ast::Attribute>,
119     pub fields: hir::HirVec<hir::StructField>,
120     pub whence: Span,
121 }
122
123 pub struct Enum {
124     pub vis: hir::Visibility,
125     pub stab: Option<attr::Stability>,
126     pub depr: Option<attr::Deprecation>,
127     pub variants: hir::HirVec<Variant>,
128     pub generics: hir::Generics,
129     pub attrs: hir::HirVec<ast::Attribute>,
130     pub id: NodeId,
131     pub whence: Span,
132     pub name: Name,
133 }
134
135 pub struct Variant {
136     pub name: Name,
137     pub attrs: hir::HirVec<ast::Attribute>,
138     pub def: hir::VariantData,
139     pub stab: Option<attr::Stability>,
140     pub depr: Option<attr::Deprecation>,
141     pub whence: Span,
142 }
143
144 pub struct Function {
145     pub decl: hir::FnDecl,
146     pub attrs: hir::HirVec<ast::Attribute>,
147     pub id: NodeId,
148     pub name: Name,
149     pub vis: hir::Visibility,
150     pub stab: Option<attr::Stability>,
151     pub depr: Option<attr::Deprecation>,
152     pub header: hir::FnHeader,
153     pub whence: Span,
154     pub generics: hir::Generics,
155     pub body: hir::BodyId,
156 }
157
158 pub struct Typedef {
159     pub ty: P<hir::Ty>,
160     pub gen: hir::Generics,
161     pub name: Name,
162     pub id: ast::NodeId,
163     pub attrs: hir::HirVec<ast::Attribute>,
164     pub whence: Span,
165     pub vis: hir::Visibility,
166     pub stab: Option<attr::Stability>,
167     pub depr: Option<attr::Deprecation>,
168 }
169
170 #[derive(Debug)]
171 pub struct Static {
172     pub type_: P<hir::Ty>,
173     pub mutability: hir::Mutability,
174     pub expr: hir::BodyId,
175     pub name: Name,
176     pub attrs: hir::HirVec<ast::Attribute>,
177     pub vis: hir::Visibility,
178     pub stab: Option<attr::Stability>,
179     pub depr: Option<attr::Deprecation>,
180     pub id: ast::NodeId,
181     pub whence: Span,
182 }
183
184 pub struct Constant {
185     pub type_: P<hir::Ty>,
186     pub expr: hir::BodyId,
187     pub name: Name,
188     pub attrs: hir::HirVec<ast::Attribute>,
189     pub vis: hir::Visibility,
190     pub stab: Option<attr::Stability>,
191     pub depr: Option<attr::Deprecation>,
192     pub id: ast::NodeId,
193     pub whence: Span,
194 }
195
196 pub struct Trait {
197     pub is_auto: hir::IsAuto,
198     pub unsafety: hir::Unsafety,
199     pub name: Name,
200     pub items: hir::HirVec<hir::TraitItem>,
201     pub generics: hir::Generics,
202     pub bounds: hir::HirVec<hir::GenericBound>,
203     pub attrs: hir::HirVec<ast::Attribute>,
204     pub id: ast::NodeId,
205     pub whence: Span,
206     pub vis: hir::Visibility,
207     pub stab: Option<attr::Stability>,
208     pub depr: Option<attr::Deprecation>,
209 }
210
211 #[derive(Debug)]
212 pub struct Impl {
213     pub unsafety: hir::Unsafety,
214     pub polarity: hir::ImplPolarity,
215     pub defaultness: hir::Defaultness,
216     pub generics: hir::Generics,
217     pub trait_: Option<hir::TraitRef>,
218     pub for_: P<hir::Ty>,
219     pub items: hir::HirVec<hir::ImplItem>,
220     pub attrs: hir::HirVec<ast::Attribute>,
221     pub whence: Span,
222     pub vis: hir::Visibility,
223     pub stab: Option<attr::Stability>,
224     pub depr: Option<attr::Deprecation>,
225     pub id: ast::NodeId,
226 }
227
228 // For Macro we store the DefId instead of the NodeId, since we also create
229 // these imported macro_rules (which only have a DUMMY_NODE_ID).
230 pub struct Macro {
231     pub name: Name,
232     pub def_id: hir::def_id::DefId,
233     pub attrs: hir::HirVec<ast::Attribute>,
234     pub whence: Span,
235     pub matchers: hir::HirVec<Span>,
236     pub stab: Option<attr::Stability>,
237     pub depr: Option<attr::Deprecation>,
238     pub imported_from: Option<Name>,
239 }
240
241 pub struct ExternCrate {
242     pub name: Name,
243     pub cnum: CrateNum,
244     pub path: Option<String>,
245     pub vis: hir::Visibility,
246     pub attrs: hir::HirVec<ast::Attribute>,
247     pub whence: Span,
248 }
249
250 pub struct Import {
251     pub name: Name,
252     pub id: NodeId,
253     pub vis: hir::Visibility,
254     pub attrs: hir::HirVec<ast::Attribute>,
255     pub path: hir::Path,
256     pub glob: bool,
257     pub whence: Span,
258 }
259
260 pub fn struct_type_from_def(vdata: &hir::VariantData) -> StructType {
261     match *vdata {
262         hir::VariantData::Struct(..) => Plain,
263         hir::VariantData::Tuple(..) => Tuple,
264         hir::VariantData::Unit(..) => Unit,
265     }
266 }