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