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