forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.rs
446 lines (409 loc) · 11.2 KB
/
data.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright 2012-2014 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.
//! Structs representing the analysis data from a crate.
//!
//! The `Dump` trait can be used together with `DumpVisitor` in order to
//! retrieve the data from a crate.
use rustc::hir;
use rustc::hir::def_id::{CrateNum, DefId};
use syntax::ast::{self, Attribute, NodeId};
use syntax_pos::Span;
use rls_data::ExternalCrateData;
pub struct CrateData {
pub name: String,
pub number: u32,
pub span: Span,
}
/// Data for any entity in the Rust language. The actual data contained varies
/// with the kind of entity being queried. See the nested structs for details.
#[derive(Debug)]
pub enum Data {
/// Data for Enums.
EnumData(EnumData),
/// Data for extern crates.
ExternCrateData(ExternCrateData),
/// Data about a function call.
FunctionCallData(FunctionCallData),
/// Data for all kinds of functions and methods.
FunctionData(FunctionData),
/// Data about a function ref.
FunctionRefData(FunctionRefData),
/// Data for impls.
ImplData(ImplData2),
/// Data for trait inheritance.
InheritanceData(InheritanceData),
/// Data about a macro declaration.
MacroData(MacroData),
/// Data about a macro use.
MacroUseData(MacroUseData),
/// Data about a method call.
MethodCallData(MethodCallData),
/// Data for method declarations (methods with a body are treated as functions).
MethodData(MethodData),
/// Data for modules.
ModData(ModData),
/// Data for a reference to a module.
ModRefData(ModRefData),
/// Data for a struct declaration.
StructData(StructData),
/// Data for a struct variant.
StructVariantDat(StructVariantData),
/// Data for a trait declaration.
TraitData(TraitData),
/// Data for a tuple variant.
TupleVariantData(TupleVariantData),
/// Data for a typedef.
TypeDefData(TypeDefData),
/// Data for a reference to a type or trait.
TypeRefData(TypeRefData),
/// Data for a use statement.
UseData(UseData),
/// Data for a global use statement.
UseGlobData(UseGlobData),
/// Data for local and global variables (consts and statics), and fields.
VariableData(VariableData),
/// Data for the use of some variable (e.g., the use of a local variable, which
/// will refere to that variables declaration).
VariableRefData(VariableRefData),
}
#[derive(Eq, PartialEq, Clone, Copy, Debug)]
pub enum Visibility {
Public,
Restricted,
Inherited,
}
impl<'a> From<&'a ast::Visibility> for Visibility {
fn from(v: &'a ast::Visibility) -> Visibility {
match *v {
ast::Visibility::Public => Visibility::Public,
ast::Visibility::Crate(_) => Visibility::Restricted,
ast::Visibility::Restricted { .. } => Visibility::Restricted,
ast::Visibility::Inherited => Visibility::Inherited,
}
}
}
impl<'a> From<&'a hir::Visibility> for Visibility {
fn from(v: &'a hir::Visibility) -> Visibility {
match *v {
hir::Visibility::Public => Visibility::Public,
hir::Visibility::Crate => Visibility::Restricted,
hir::Visibility::Restricted { .. } => Visibility::Restricted,
hir::Visibility::Inherited => Visibility::Inherited,
}
}
}
/// Data for the prelude of a crate.
#[derive(Debug)]
pub struct CratePreludeData {
pub crate_name: String,
pub crate_root: String,
pub external_crates: Vec<ExternalCrateData>,
pub span: Span,
}
/// Data for enum declarations.
#[derive(Clone, Debug)]
pub struct EnumData {
pub id: NodeId,
pub name: String,
pub value: String,
pub qualname: String,
pub span: Span,
pub scope: NodeId,
pub variants: Vec<NodeId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
/// Data for extern crates.
#[derive(Debug)]
pub struct ExternCrateData {
pub id: NodeId,
pub name: String,
pub crate_num: CrateNum,
pub location: String,
pub span: Span,
pub scope: NodeId,
}
/// Data about a function call.
#[derive(Debug)]
pub struct FunctionCallData {
pub span: Span,
pub scope: NodeId,
pub ref_id: DefId,
}
/// Data for all kinds of functions and methods.
#[derive(Clone, Debug)]
pub struct FunctionData {
pub id: NodeId,
pub name: String,
pub qualname: String,
pub declaration: Option<DefId>,
pub span: Span,
pub scope: NodeId,
pub value: String,
pub visibility: Visibility,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
/// Data about a function call.
#[derive(Debug)]
pub struct FunctionRefData {
pub span: Span,
pub scope: NodeId,
pub ref_id: DefId,
}
#[derive(Debug)]
pub struct ImplData {
pub id: NodeId,
pub span: Span,
pub scope: NodeId,
pub trait_ref: Option<DefId>,
pub self_ref: Option<DefId>,
}
#[derive(Debug)]
// FIXME: this struct should not exist. However, removing it requires heavy
// refactoring of dump_visitor.rs. See PR 31838 for more info.
pub struct ImplData2 {
pub id: NodeId,
pub span: Span,
pub scope: NodeId,
// FIXME: I'm not really sure inline data is the best way to do this. Seems
// OK in this case, but generalising leads to returning chunks of AST, which
// feels wrong.
pub trait_ref: Option<TypeRefData>,
pub self_ref: Option<TypeRefData>,
}
#[derive(Debug)]
pub struct InheritanceData {
pub span: Span,
pub base_id: DefId,
pub deriv_id: NodeId
}
/// Data about a macro declaration.
#[derive(Debug)]
pub struct MacroData {
pub span: Span,
pub name: String,
pub qualname: String,
pub docs: String,
}
/// Data about a macro use.
#[derive(Debug)]
pub struct MacroUseData {
pub span: Span,
pub name: String,
pub qualname: String,
// Because macro expansion happens before ref-ids are determined,
// we use the callee span to reference the associated macro definition.
pub callee_span: Span,
pub scope: NodeId,
pub imported: bool,
}
/// Data about a method call.
#[derive(Debug)]
pub struct MethodCallData {
pub span: Span,
pub scope: NodeId,
pub ref_id: Option<DefId>,
pub decl_id: Option<DefId>,
}
/// Data for method declarations (methods with a body are treated as functions).
#[derive(Clone, Debug)]
pub struct MethodData {
pub id: NodeId,
pub name: String,
pub qualname: String,
pub span: Span,
pub scope: NodeId,
pub value: String,
pub decl_id: Option<DefId>,
pub parent: Option<DefId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
/// Data for modules.
#[derive(Debug)]
pub struct ModData {
pub id: NodeId,
pub name: String,
pub qualname: String,
pub span: Span,
pub scope: NodeId,
pub filename: String,
pub items: Vec<NodeId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}
/// Data for a reference to a module.
#[derive(Debug)]
pub struct ModRefData {
pub span: Span,
pub scope: NodeId,
pub ref_id: Option<DefId>,
pub qualname: String
}
#[derive(Debug)]
pub struct StructData {
pub span: Span,
pub name: String,
pub id: NodeId,
pub ctor_id: NodeId,
pub qualname: String,
pub scope: NodeId,
pub value: String,
pub fields: Vec<NodeId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
#[derive(Debug)]
pub struct StructVariantData {
pub span: Span,
pub name: String,
pub id: NodeId,
pub qualname: String,
pub type_value: String,
pub value: String,
pub scope: NodeId,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
#[derive(Debug)]
pub struct TraitData {
pub span: Span,
pub id: NodeId,
pub name: String,
pub qualname: String,
pub scope: NodeId,
pub value: String,
pub items: Vec<NodeId>,
pub visibility: Visibility,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
#[derive(Debug)]
pub struct TupleVariantData {
pub span: Span,
pub id: NodeId,
pub name: String,
pub qualname: String,
pub type_value: String,
pub value: String,
pub scope: NodeId,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Signature,
pub attributes: Vec<Attribute>,
}
/// Data for a typedef.
#[derive(Debug)]
pub struct TypeDefData {
pub id: NodeId,
pub name: String,
pub span: Span,
pub qualname: String,
pub value: String,
pub visibility: Visibility,
pub parent: Option<DefId>,
pub docs: String,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}
/// Data for a reference to a type or trait.
#[derive(Clone, Debug)]
pub struct TypeRefData {
pub span: Span,
pub scope: NodeId,
pub ref_id: Option<DefId>,
pub qualname: String,
}
#[derive(Debug)]
pub struct UseData {
pub id: NodeId,
pub span: Span,
pub name: String,
pub mod_id: Option<DefId>,
pub scope: NodeId,
pub visibility: Visibility,
}
#[derive(Debug)]
pub struct UseGlobData {
pub id: NodeId,
pub span: Span,
pub names: Vec<String>,
pub scope: NodeId,
pub visibility: Visibility,
}
/// Data for local and global variables (consts and statics).
#[derive(Debug)]
pub struct VariableData {
pub id: NodeId,
pub kind: VariableKind,
pub name: String,
pub qualname: String,
pub span: Span,
pub scope: NodeId,
pub parent: Option<DefId>,
pub value: String,
pub type_value: String,
pub visibility: Visibility,
pub docs: String,
pub sig: Option<Signature>,
pub attributes: Vec<Attribute>,
}
#[derive(Debug)]
pub enum VariableKind {
Static,
Const,
Local,
Field,
}
/// Data for the use of some item (e.g., the use of a local variable, which
/// will refer to that variables declaration (by ref_id)).
#[derive(Debug)]
pub struct VariableRefData {
pub name: String,
pub span: Span,
pub scope: NodeId,
pub ref_id: DefId,
}
/// Encodes information about the signature of a definition. This should have
/// enough information to create a nice display about a definition without
/// access to the source code.
#[derive(Clone, Debug)]
pub struct Signature {
pub span: Span,
pub text: String,
// These identify the main identifier for the defintion as byte offsets into
// `text`. E.g., of `foo` in `pub fn foo(...)`
pub ident_start: usize,
pub ident_end: usize,
pub defs: Vec<SigElement>,
pub refs: Vec<SigElement>,
}
/// An element of a signature. `start` and `end` are byte offsets into the `text`
/// of the parent `Signature`.
#[derive(Clone, Debug)]
pub struct SigElement {
pub id: DefId,
pub start: usize,
pub end: usize,
}