forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoctree.rs
273 lines (250 loc) · 7.2 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
//! 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 rustc_ast as ast;
use rustc_span::hygiene::MacroKind;
use rustc_span::{self, Span, Symbol};
use rustc_hir as hir;
use rustc_hir::def_id::CrateNum;
use rustc_hir::HirId;
pub struct Module<'hir> {
pub name: Option<Symbol>,
pub attrs: &'hir [ast::Attribute],
pub where_outer: Span,
pub where_inner: Span,
pub extern_crates: Vec<ExternCrate<'hir>>,
pub imports: Vec<Import<'hir>>,
pub structs: Vec<Struct<'hir>>,
pub unions: Vec<Union<'hir>>,
pub enums: Vec<Enum<'hir>>,
pub fns: Vec<Function<'hir>>,
pub mods: Vec<Module<'hir>>,
pub id: hir::HirId,
pub typedefs: Vec<Typedef<'hir>>,
pub opaque_tys: Vec<OpaqueTy<'hir>>,
pub statics: Vec<Static<'hir>>,
pub constants: Vec<Constant<'hir>>,
pub traits: Vec<Trait<'hir>>,
pub vis: &'hir hir::Visibility<'hir>,
pub impls: Vec<Impl<'hir>>,
pub foreigns: Vec<ForeignItem<'hir>>,
pub macros: Vec<Macro<'hir>>,
pub proc_macros: Vec<ProcMacro<'hir>>,
pub trait_aliases: Vec<TraitAlias<'hir>>,
pub is_crate: bool,
}
impl Module<'hir> {
pub fn new(
name: Option<Symbol>,
attrs: &'hir [ast::Attribute],
vis: &'hir hir::Visibility<'hir>,
) -> Module<'hir> {
Module {
name,
id: hir::CRATE_HIR_ID,
vis,
where_outer: rustc_span::DUMMY_SP,
where_inner: rustc_span::DUMMY_SP,
attrs,
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(),
opaque_tys: Vec::new(),
statics: Vec::new(),
constants: Vec::new(),
traits: Vec::new(),
impls: Vec::new(),
foreigns: Vec::new(),
macros: Vec::new(),
proc_macros: Vec::new(),
trait_aliases: Vec::new(),
is_crate: false,
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum StructType {
/// A braced struct
Plain,
/// A tuple struct
Tuple,
/// A unit struct
Unit,
}
pub struct Struct<'hir> {
pub vis: &'hir hir::Visibility<'hir>,
pub id: hir::HirId,
pub struct_type: StructType,
pub name: Symbol,
pub generics: &'hir hir::Generics<'hir>,
pub attrs: &'hir [ast::Attribute],
pub fields: &'hir [hir::StructField<'hir>],
pub span: Span,
}
pub struct Union<'hir> {
pub vis: &'hir hir::Visibility<'hir>,
pub id: hir::HirId,
pub struct_type: StructType,
pub name: Symbol,
pub generics: &'hir hir::Generics<'hir>,
pub attrs: &'hir [ast::Attribute],
pub fields: &'hir [hir::StructField<'hir>],
pub span: Span,
}
pub struct Enum<'hir> {
pub vis: &'hir hir::Visibility<'hir>,
pub variants: Vec<Variant<'hir>>,
pub generics: &'hir hir::Generics<'hir>,
pub attrs: &'hir [ast::Attribute],
pub id: hir::HirId,
pub span: Span,
pub name: Symbol,
}
pub struct Variant<'hir> {
pub name: Symbol,
pub id: hir::HirId,
pub attrs: &'hir [ast::Attribute],
pub def: &'hir hir::VariantData<'hir>,
pub span: Span,
}
pub struct Function<'hir> {
pub decl: &'hir hir::FnDecl<'hir>,
pub attrs: &'hir [ast::Attribute],
pub id: hir::HirId,
pub name: Symbol,
pub vis: &'hir hir::Visibility<'hir>,
pub header: hir::FnHeader,
pub span: Span,
pub generics: &'hir hir::Generics<'hir>,
pub body: hir::BodyId,
}
pub struct Typedef<'hir> {
pub ty: &'hir hir::Ty<'hir>,
pub gen: &'hir hir::Generics<'hir>,
pub name: Symbol,
pub id: hir::HirId,
pub attrs: &'hir [ast::Attribute],
pub span: Span,
pub vis: &'hir hir::Visibility<'hir>,
}
pub struct OpaqueTy<'hir> {
pub opaque_ty: &'hir hir::OpaqueTy<'hir>,
pub name: Symbol,
pub id: hir::HirId,
pub attrs: &'hir [ast::Attribute],
pub span: Span,
pub vis: &'hir hir::Visibility<'hir>,
}
#[derive(Debug)]
pub struct Static<'hir> {
pub type_: &'hir hir::Ty<'hir>,
pub mutability: hir::Mutability,
pub expr: hir::BodyId,
pub name: Symbol,
pub attrs: &'hir [ast::Attribute],
pub vis: &'hir hir::Visibility<'hir>,
pub id: hir::HirId,
pub span: Span,
}
pub struct Constant<'hir> {
pub type_: &'hir hir::Ty<'hir>,
pub expr: hir::BodyId,
pub name: Symbol,
pub attrs: &'hir [ast::Attribute],
pub vis: &'hir hir::Visibility<'hir>,
pub id: hir::HirId,
pub span: Span,
}
pub struct Trait<'hir> {
pub is_auto: hir::IsAuto,
pub unsafety: hir::Unsafety,
pub name: Symbol,
pub items: Vec<&'hir hir::TraitItem<'hir>>,
pub generics: &'hir hir::Generics<'hir>,
pub bounds: &'hir [hir::GenericBound<'hir>],
pub attrs: &'hir [ast::Attribute],
pub id: hir::HirId,
pub span: Span,
pub vis: &'hir hir::Visibility<'hir>,
}
pub struct TraitAlias<'hir> {
pub name: Symbol,
pub generics: &'hir hir::Generics<'hir>,
pub bounds: &'hir [hir::GenericBound<'hir>],
pub attrs: &'hir [ast::Attribute],
pub id: hir::HirId,
pub span: Span,
pub vis: &'hir hir::Visibility<'hir>,
}
#[derive(Debug)]
pub struct Impl<'hir> {
pub unsafety: hir::Unsafety,
pub polarity: hir::ImplPolarity,
pub defaultness: hir::Defaultness,
pub constness: hir::Constness,
pub generics: &'hir hir::Generics<'hir>,
pub trait_: &'hir Option<hir::TraitRef<'hir>>,
pub for_: &'hir hir::Ty<'hir>,
pub items: Vec<&'hir hir::ImplItem<'hir>>,
pub attrs: &'hir [ast::Attribute],
pub span: Span,
pub vis: &'hir hir::Visibility<'hir>,
pub id: hir::HirId,
}
pub struct ForeignItem<'hir> {
pub vis: &'hir hir::Visibility<'hir>,
pub id: hir::HirId,
pub name: Symbol,
pub kind: &'hir hir::ForeignItemKind<'hir>,
pub attrs: &'hir [ast::Attribute],
pub span: Span,
}
// 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<'hir> {
pub name: Symbol,
pub hid: hir::HirId,
pub def_id: hir::def_id::DefId,
pub attrs: &'hir [ast::Attribute],
pub span: Span,
pub matchers: Vec<Span>,
pub imported_from: Option<Symbol>,
}
pub struct ExternCrate<'hir> {
pub name: Symbol,
pub hir_id: HirId,
pub cnum: CrateNum,
pub path: Option<String>,
pub vis: &'hir hir::Visibility<'hir>,
pub attrs: &'hir [ast::Attribute],
pub span: Span,
}
pub struct Import<'hir> {
pub name: Symbol,
pub id: hir::HirId,
pub vis: &'hir hir::Visibility<'hir>,
pub attrs: &'hir [ast::Attribute],
pub path: &'hir hir::Path<'hir>,
pub glob: bool,
pub span: Span,
}
pub struct ProcMacro<'hir> {
pub name: Symbol,
pub id: hir::HirId,
pub kind: MacroKind,
pub helpers: Vec<Symbol>,
pub attrs: &'hir [ast::Attribute],
pub span: Span,
}
pub fn struct_type_from_def(vdata: &hir::VariantData<'_>) -> StructType {
match *vdata {
hir::VariantData::Struct(..) => Plain,
hir::VariantData::Tuple(..) => Tuple,
hir::VariantData::Unit(..) => Unit,
}
}