]> git.lizzy.rs Git - rust.git/blob - src/librustc_save_analysis/data.rs
6caf81380e40dad51f8c33c1d90a51c7dd5f7065
[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 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     pub sig: Signature,
139     pub attributes: Vec<Attribute>,
140 }
141
142 /// Data for extern crates.
143 #[derive(Debug, RustcEncodable)]
144 pub struct ExternCrateData {
145     pub id: NodeId,
146     pub name: String,
147     pub crate_num: CrateNum,
148     pub location: String,
149     pub span: Span,
150     pub scope: NodeId,
151 }
152
153 /// Data about a function call.
154 #[derive(Debug, RustcEncodable)]
155 pub struct FunctionCallData {
156     pub span: Span,
157     pub scope: NodeId,
158     pub ref_id: DefId,
159 }
160
161 /// Data for all kinds of functions and methods.
162 #[derive(Clone, Debug, RustcEncodable)]
163 pub struct FunctionData {
164     pub id: NodeId,
165     pub name: String,
166     pub qualname: String,
167     pub declaration: Option<DefId>,
168     pub span: Span,
169     pub scope: NodeId,
170     pub value: String,
171     pub visibility: Visibility,
172     pub parent: Option<DefId>,
173     pub docs: String,
174     pub sig: Signature,
175     pub attributes: Vec<Attribute>,
176 }
177
178 /// Data about a function call.
179 #[derive(Debug, RustcEncodable)]
180 pub struct FunctionRefData {
181     pub span: Span,
182     pub scope: NodeId,
183     pub ref_id: DefId,
184 }
185
186 #[derive(Debug, RustcEncodable)]
187 pub struct ImplData {
188     pub id: NodeId,
189     pub span: Span,
190     pub scope: NodeId,
191     pub trait_ref: Option<DefId>,
192     pub self_ref: Option<DefId>,
193 }
194
195 #[derive(Debug, RustcEncodable)]
196 // FIXME: this struct should not exist. However, removing it requires heavy
197 // refactoring of dump_visitor.rs. See PR 31838 for more info.
198 pub struct ImplData2 {
199     pub id: NodeId,
200     pub span: Span,
201     pub scope: NodeId,
202     // FIXME: I'm not really sure inline data is the best way to do this. Seems
203     // OK in this case, but generalising leads to returning chunks of AST, which
204     // feels wrong.
205     pub trait_ref: Option<TypeRefData>,
206     pub self_ref: Option<TypeRefData>,
207 }
208
209 #[derive(Debug, RustcEncodable)]
210 pub struct InheritanceData {
211     pub span: Span,
212     pub base_id: DefId,
213     pub deriv_id: NodeId
214 }
215
216 /// Data about a macro declaration.
217 #[derive(Debug, RustcEncodable)]
218 pub struct MacroData {
219     pub span: Span,
220     pub name: String,
221     pub qualname: String,
222     pub docs: String,
223 }
224
225 /// Data about a macro use.
226 #[derive(Debug, RustcEncodable)]
227 pub struct MacroUseData {
228     pub span: Span,
229     pub name: String,
230     pub qualname: String,
231     // Because macro expansion happens before ref-ids are determined,
232     // we use the callee span to reference the associated macro definition.
233     pub callee_span: Span,
234     pub scope: NodeId,
235     pub imported: bool,
236 }
237
238 /// Data about a method call.
239 #[derive(Debug, RustcEncodable)]
240 pub struct MethodCallData {
241     pub span: Span,
242     pub scope: NodeId,
243     pub ref_id: Option<DefId>,
244     pub decl_id: Option<DefId>,
245 }
246
247 /// Data for method declarations (methods with a body are treated as functions).
248 #[derive(Clone, Debug, RustcEncodable)]
249 pub struct MethodData {
250     pub id: NodeId,
251     pub name: String,
252     pub qualname: String,
253     pub span: Span,
254     pub scope: NodeId,
255     pub value: String,
256     pub decl_id: Option<DefId>,
257     pub parent: Option<DefId>,
258     pub visibility: Visibility,
259     pub docs: String,
260     pub sig: Signature,
261     pub attributes: Vec<Attribute>,
262 }
263
264 /// Data for modules.
265 #[derive(Debug, RustcEncodable)]
266 pub struct ModData {
267     pub id: NodeId,
268     pub name: String,
269     pub qualname: String,
270     pub span: Span,
271     pub scope: NodeId,
272     pub filename: String,
273     pub items: Vec<NodeId>,
274     pub visibility: Visibility,
275     pub docs: String,
276     pub sig: Signature,
277     pub attributes: Vec<Attribute>,
278 }
279
280 /// Data for a reference to a module.
281 #[derive(Debug, RustcEncodable)]
282 pub struct ModRefData {
283     pub span: Span,
284     pub scope: NodeId,
285     pub ref_id: Option<DefId>,
286     pub qualname: String
287 }
288
289 #[derive(Debug, RustcEncodable)]
290 pub struct StructData {
291     pub span: Span,
292     pub name: String,
293     pub id: NodeId,
294     pub ctor_id: NodeId,
295     pub qualname: String,
296     pub scope: NodeId,
297     pub value: String,
298     pub fields: Vec<NodeId>,
299     pub visibility: Visibility,
300     pub docs: String,
301     pub sig: Signature,
302     pub attributes: Vec<Attribute>,
303 }
304
305 #[derive(Debug, RustcEncodable)]
306 pub struct StructVariantData {
307     pub span: Span,
308     pub name: String,
309     pub id: NodeId,
310     pub qualname: String,
311     pub type_value: String,
312     pub value: String,
313     pub scope: NodeId,
314     pub parent: Option<DefId>,
315     pub docs: String,
316     pub sig: Signature,
317     pub attributes: Vec<Attribute>,
318 }
319
320 #[derive(Debug, RustcEncodable)]
321 pub struct TraitData {
322     pub span: Span,
323     pub id: NodeId,
324     pub name: String,
325     pub qualname: String,
326     pub scope: NodeId,
327     pub value: String,
328     pub items: Vec<NodeId>,
329     pub visibility: Visibility,
330     pub docs: String,
331     pub sig: Signature,
332     pub attributes: Vec<Attribute>,
333 }
334
335 #[derive(Debug, RustcEncodable)]
336 pub struct TupleVariantData {
337     pub span: Span,
338     pub id: NodeId,
339     pub name: String,
340     pub qualname: String,
341     pub type_value: String,
342     pub value: String,
343     pub scope: NodeId,
344     pub parent: Option<DefId>,
345     pub docs: String,
346     pub sig: Signature,
347     pub attributes: Vec<Attribute>,
348 }
349
350 /// Data for a typedef.
351 #[derive(Debug, RustcEncodable)]
352 pub struct TypeDefData {
353     pub id: NodeId,
354     pub name: String,
355     pub span: Span,
356     pub qualname: String,
357     pub value: String,
358     pub visibility: Visibility,
359     pub parent: Option<DefId>,
360     pub docs: String,
361     pub sig: Option<Signature>,
362     pub attributes: Vec<Attribute>,
363 }
364
365 /// Data for a reference to a type or trait.
366 #[derive(Clone, Debug, RustcEncodable)]
367 pub struct TypeRefData {
368     pub span: Span,
369     pub scope: NodeId,
370     pub ref_id: Option<DefId>,
371     pub qualname: String,
372 }
373
374 #[derive(Debug, RustcEncodable)]
375 pub struct UseData {
376     pub id: NodeId,
377     pub span: Span,
378     pub name: String,
379     pub mod_id: Option<DefId>,
380     pub scope: NodeId,
381     pub visibility: Visibility,
382 }
383
384 #[derive(Debug, RustcEncodable)]
385 pub struct UseGlobData {
386     pub id: NodeId,
387     pub span: Span,
388     pub names: Vec<String>,
389     pub scope: NodeId,
390     pub visibility: Visibility,
391 }
392
393 /// Data for local and global variables (consts and statics).
394 #[derive(Debug, RustcEncodable)]
395 pub struct VariableData {
396     pub id: NodeId,
397     pub kind: VariableKind,
398     pub name: String,
399     pub qualname: String,
400     pub span: Span,
401     pub scope: NodeId,
402     pub parent: Option<DefId>,
403     pub value: String,
404     pub type_value: String,
405     pub visibility: Visibility,
406     pub docs: String,
407     pub sig: Option<Signature>,
408     pub attributes: Vec<Attribute>,
409 }
410
411 #[derive(Debug, RustcEncodable)]
412 pub enum VariableKind {
413     Static,
414     Const,
415     Local,
416     Field,
417 }
418
419 /// Data for the use of some item (e.g., the use of a local variable, which
420 /// will refer to that variables declaration (by ref_id)).
421 #[derive(Debug, RustcEncodable)]
422 pub struct VariableRefData {
423     pub name: String,
424     pub span: Span,
425     pub scope: NodeId,
426     pub ref_id: DefId,
427 }
428
429
430 /// Encodes information about the signature of a definition. This should have
431 /// enough information to create a nice display about a definition without
432 /// access to the source code.
433 #[derive(Clone, Debug, RustcEncodable)]
434 pub struct Signature {
435     pub span: Span,
436     pub text: String,
437     // These identify the main identifier for the defintion as byte offsets into
438     // `text`. E.g., of `foo` in `pub fn foo(...)`
439     pub ident_start: usize,
440     pub ident_end: usize,
441     pub defs: Vec<SigElement>,
442     pub refs: Vec<SigElement>,
443 }
444
445 /// An element of a signature. `start` and `end` are byte offsets into the `text`
446 /// of the parent `Signature`.
447 #[derive(Clone, Debug, RustcEncodable)]
448 pub struct SigElement {
449     pub id: DefId,
450     pub start: usize,
451     pub end: usize,
452 }