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