]> git.lizzy.rs Git - rust.git/blob - src/librustc_save_analysis/data.rs
Rollup merge of #42496 - Razaekel:feature/integer_max-min, r=BurntSushi
[rust.git] / src / librustc_save_analysis / data.rs
1 // Copyright 2012-2014 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 //! Structs representing the analysis data from a crate.
12 //!
13 //! The `Dump` trait can be used together with `DumpVisitor` in order to
14 //! retrieve the data from a crate.
15
16 use rustc::hir;
17 use rustc::hir::def_id::{CrateNum, DefId};
18 use syntax::ast::{self, Attribute, NodeId};
19 use syntax_pos::Span;
20
21 use rls_data::{ExternalCrateData, Signature};
22
23 pub struct CrateData {
24     pub name: String,
25     pub number: u32,
26     pub span: Span,
27 }
28
29 /// Data for any entity in the Rust language. The actual data contained varies
30 /// with the kind of entity being queried. See the nested structs for details.
31 #[derive(Debug)]
32 pub enum Data {
33     /// Data for Enums.
34     EnumData(EnumData),
35     /// Data for extern crates.
36     ExternCrateData(ExternCrateData),
37     /// Data about a function call.
38     FunctionCallData(FunctionCallData),
39     /// Data for all kinds of functions and methods.
40     FunctionData(FunctionData),
41     /// Data about a function ref.
42     FunctionRefData(FunctionRefData),
43     /// Data for impls.
44     ImplData(ImplData2),
45     /// Data for trait inheritance.
46     InheritanceData(InheritanceData),
47     /// Data about a macro declaration.
48     MacroData(MacroData),
49     /// Data about a macro use.
50     MacroUseData(MacroUseData),
51     /// Data about a method call.
52     MethodCallData(MethodCallData),
53     /// Data for method declarations (methods with a body are treated as functions).
54     MethodData(MethodData),
55     /// Data for modules.
56     ModData(ModData),
57     /// Data for a reference to a module.
58     ModRefData(ModRefData),
59     /// Data for a struct declaration.
60     StructData(StructData),
61     /// Data for a struct variant.
62     StructVariantDat(StructVariantData),
63     /// Data for a trait declaration.
64     TraitData(TraitData),
65     /// Data for a tuple variant.
66     TupleVariantData(TupleVariantData),
67     /// Data for a typedef.
68     TypeDefData(TypeDefData),
69     /// Data for a reference to a type or trait.
70     TypeRefData(TypeRefData),
71     /// Data for a use statement.
72     UseData(UseData),
73     /// Data for a global use statement.
74     UseGlobData(UseGlobData),
75     /// Data for local and global variables (consts and statics), and fields.
76     VariableData(VariableData),
77     /// Data for the use of some variable (e.g., the use of a local variable, which
78     /// will refere to that variables declaration).
79     VariableRefData(VariableRefData),
80 }
81
82 #[derive(Eq, PartialEq, Clone, Copy, Debug)]
83 pub enum Visibility {
84     Public,
85     Restricted,
86     Inherited,
87 }
88
89 impl<'a> From<&'a ast::Visibility> for Visibility {
90     fn from(v: &'a ast::Visibility) -> Visibility {
91         match *v {
92             ast::Visibility::Public => Visibility::Public,
93             ast::Visibility::Crate(_) => Visibility::Restricted,
94             ast::Visibility::Restricted { .. } => Visibility::Restricted,
95             ast::Visibility::Inherited => Visibility::Inherited,
96         }
97     }
98 }
99
100 impl<'a> From<&'a hir::Visibility> for Visibility {
101     fn from(v: &'a hir::Visibility) -> Visibility {
102         match *v {
103             hir::Visibility::Public => Visibility::Public,
104             hir::Visibility::Crate => Visibility::Restricted,
105             hir::Visibility::Restricted { .. } => Visibility::Restricted,
106             hir::Visibility::Inherited => Visibility::Inherited,
107         }
108     }
109 }
110
111 /// Data for the prelude of a crate.
112 #[derive(Debug)]
113 pub struct CratePreludeData {
114     pub crate_name: String,
115     pub crate_root: String,
116     pub external_crates: Vec<ExternalCrateData>,
117     pub span: Span,
118 }
119
120 /// Data for enum declarations.
121 #[derive(Clone, Debug)]
122 pub struct EnumData {
123     pub id: NodeId,
124     pub name: String,
125     pub value: String,
126     pub qualname: String,
127     pub span: Span,
128     pub scope: NodeId,
129     pub variants: Vec<NodeId>,
130     pub visibility: Visibility,
131     pub docs: String,
132     pub sig: Option<Signature>,
133     pub attributes: Vec<Attribute>,
134 }
135
136 /// Data for extern crates.
137 #[derive(Debug)]
138 pub struct ExternCrateData {
139     pub id: NodeId,
140     pub name: String,
141     pub crate_num: CrateNum,
142     pub location: String,
143     pub span: Span,
144     pub scope: NodeId,
145 }
146
147 /// Data about a function call.
148 #[derive(Debug)]
149 pub struct FunctionCallData {
150     pub span: Span,
151     pub scope: NodeId,
152     pub ref_id: DefId,
153 }
154
155 /// Data for all kinds of functions and methods.
156 #[derive(Clone, Debug)]
157 pub struct FunctionData {
158     pub id: NodeId,
159     pub name: String,
160     pub qualname: String,
161     pub declaration: Option<DefId>,
162     pub span: Span,
163     pub scope: NodeId,
164     pub value: String,
165     pub visibility: Visibility,
166     pub parent: Option<DefId>,
167     pub docs: String,
168     pub sig: Option<Signature>,
169     pub attributes: Vec<Attribute>,
170 }
171
172 /// Data about a function call.
173 #[derive(Debug)]
174 pub struct FunctionRefData {
175     pub span: Span,
176     pub scope: NodeId,
177     pub ref_id: DefId,
178 }
179
180 #[derive(Debug)]
181 pub struct ImplData {
182     pub id: NodeId,
183     pub span: Span,
184     pub scope: NodeId,
185     pub trait_ref: Option<DefId>,
186     pub self_ref: Option<DefId>,
187 }
188
189 #[derive(Debug)]
190 // FIXME: this struct should not exist. However, removing it requires heavy
191 // refactoring of dump_visitor.rs. See PR 31838 for more info.
192 pub struct ImplData2 {
193     pub id: NodeId,
194     pub span: Span,
195     pub scope: NodeId,
196     // FIXME: I'm not really sure inline data is the best way to do this. Seems
197     // OK in this case, but generalising leads to returning chunks of AST, which
198     // feels wrong.
199     pub trait_ref: Option<TypeRefData>,
200     pub self_ref: Option<TypeRefData>,
201 }
202
203 #[derive(Debug)]
204 pub struct InheritanceData {
205     pub span: Span,
206     pub base_id: DefId,
207     pub deriv_id: NodeId
208 }
209
210 /// Data about a macro declaration.
211 #[derive(Debug)]
212 pub struct MacroData {
213     pub span: Span,
214     pub name: String,
215     pub qualname: String,
216     pub docs: String,
217 }
218
219 /// Data about a macro use.
220 #[derive(Debug)]
221 pub struct MacroUseData {
222     pub span: Span,
223     pub name: String,
224     pub qualname: String,
225     // Because macro expansion happens before ref-ids are determined,
226     // we use the callee span to reference the associated macro definition.
227     pub callee_span: Span,
228     pub scope: NodeId,
229     pub imported: bool,
230 }
231
232 /// Data about a method call.
233 #[derive(Debug)]
234 pub struct MethodCallData {
235     pub span: Span,
236     pub scope: NodeId,
237     pub ref_id: Option<DefId>,
238     pub decl_id: Option<DefId>,
239 }
240
241 /// Data for method declarations (methods with a body are treated as functions).
242 #[derive(Clone, Debug)]
243 pub struct MethodData {
244     pub id: NodeId,
245     pub name: String,
246     pub qualname: String,
247     pub span: Span,
248     pub scope: NodeId,
249     pub value: String,
250     pub decl_id: Option<DefId>,
251     pub parent: Option<DefId>,
252     pub visibility: Visibility,
253     pub docs: String,
254     pub sig: Option<Signature>,
255     pub attributes: Vec<Attribute>,
256 }
257
258 /// Data for modules.
259 #[derive(Debug)]
260 pub struct ModData {
261     pub id: NodeId,
262     pub name: String,
263     pub qualname: String,
264     pub span: Span,
265     pub scope: NodeId,
266     pub filename: String,
267     pub items: Vec<NodeId>,
268     pub visibility: Visibility,
269     pub docs: String,
270     pub sig: Option<Signature>,
271     pub attributes: Vec<Attribute>,
272 }
273
274 /// Data for a reference to a module.
275 #[derive(Debug)]
276 pub struct ModRefData {
277     pub span: Span,
278     pub scope: NodeId,
279     pub ref_id: Option<DefId>,
280     pub qualname: String
281 }
282
283 #[derive(Debug)]
284 pub struct StructData {
285     pub span: Span,
286     pub name: String,
287     pub id: NodeId,
288     pub ctor_id: NodeId,
289     pub qualname: String,
290     pub scope: NodeId,
291     pub value: String,
292     pub fields: Vec<NodeId>,
293     pub visibility: Visibility,
294     pub docs: String,
295     pub sig: Option<Signature>,
296     pub attributes: Vec<Attribute>,
297 }
298
299 #[derive(Debug)]
300 pub struct StructVariantData {
301     pub span: Span,
302     pub name: String,
303     pub id: NodeId,
304     pub qualname: String,
305     pub type_value: String,
306     pub value: String,
307     pub scope: NodeId,
308     pub parent: Option<DefId>,
309     pub docs: String,
310     pub sig: Option<Signature>,
311     pub attributes: Vec<Attribute>,
312 }
313
314 #[derive(Debug)]
315 pub struct TraitData {
316     pub span: Span,
317     pub id: NodeId,
318     pub name: String,
319     pub qualname: String,
320     pub scope: NodeId,
321     pub value: String,
322     pub items: Vec<NodeId>,
323     pub visibility: Visibility,
324     pub docs: String,
325     pub sig: Option<Signature>,
326     pub attributes: Vec<Attribute>,
327 }
328
329 #[derive(Debug)]
330 pub struct TupleVariantData {
331     pub span: Span,
332     pub id: NodeId,
333     pub name: String,
334     pub qualname: String,
335     pub type_value: String,
336     pub value: String,
337     pub scope: NodeId,
338     pub parent: Option<DefId>,
339     pub docs: String,
340     pub sig: Option<Signature>,
341     pub attributes: Vec<Attribute>,
342 }
343
344 /// Data for a typedef.
345 #[derive(Debug)]
346 pub struct TypeDefData {
347     pub id: NodeId,
348     pub name: String,
349     pub span: Span,
350     pub qualname: String,
351     pub value: String,
352     pub visibility: Visibility,
353     pub parent: Option<DefId>,
354     pub docs: String,
355     pub sig: Option<Signature>,
356     pub attributes: Vec<Attribute>,
357 }
358
359 /// Data for a reference to a type or trait.
360 #[derive(Clone, Debug)]
361 pub struct TypeRefData {
362     pub span: Span,
363     pub scope: NodeId,
364     pub ref_id: Option<DefId>,
365     pub qualname: String,
366 }
367
368 #[derive(Debug)]
369 pub struct UseData {
370     pub id: NodeId,
371     pub span: Span,
372     pub name: String,
373     pub mod_id: Option<DefId>,
374     pub scope: NodeId,
375     pub visibility: Visibility,
376 }
377
378 #[derive(Debug)]
379 pub struct UseGlobData {
380     pub id: NodeId,
381     pub span: Span,
382     pub names: Vec<String>,
383     pub scope: NodeId,
384     pub visibility: Visibility,
385 }
386
387 /// Data for local and global variables (consts and statics).
388 #[derive(Debug)]
389 pub struct VariableData {
390     pub id: NodeId,
391     pub kind: VariableKind,
392     pub name: String,
393     pub qualname: String,
394     pub span: Span,
395     pub scope: NodeId,
396     pub parent: Option<DefId>,
397     pub value: String,
398     pub type_value: String,
399     pub visibility: Visibility,
400     pub docs: String,
401     pub sig: Option<Signature>,
402     pub attributes: Vec<Attribute>,
403 }
404
405 #[derive(Debug)]
406 pub enum VariableKind {
407     Static,
408     Const,
409     Local,
410     Field,
411 }
412
413 /// Data for the use of some item (e.g., the use of a local variable, which
414 /// will refer to that variables declaration (by ref_id)).
415 #[derive(Debug)]
416 pub struct VariableRefData {
417     pub name: String,
418     pub span: Span,
419     pub scope: NodeId,
420     pub ref_id: DefId,
421 }