]> git.lizzy.rs Git - rust.git/blob - src/librustc_save_analysis/data.rs
field signatures
[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     pub sig: Signature,
294 }
295
296 #[derive(Debug, RustcEncodable)]
297 pub struct StructVariantData {
298     pub span: Span,
299     pub name: String,
300     pub id: NodeId,
301     pub qualname: String,
302     pub type_value: String,
303     pub value: String,
304     pub scope: NodeId,
305     pub parent: Option<DefId>,
306     pub docs: String,
307 }
308
309 #[derive(Debug, RustcEncodable)]
310 pub struct TraitData {
311     pub span: Span,
312     pub id: NodeId,
313     pub name: String,
314     pub qualname: String,
315     pub scope: NodeId,
316     pub value: String,
317     pub items: Vec<NodeId>,
318     pub visibility: Visibility,
319     pub docs: String,
320 }
321
322 #[derive(Debug, RustcEncodable)]
323 pub struct TupleVariantData {
324     pub span: Span,
325     pub id: NodeId,
326     pub name: String,
327     pub qualname: String,
328     pub type_value: String,
329     pub value: String,
330     pub scope: NodeId,
331     pub parent: Option<DefId>,
332     pub docs: String,
333 }
334
335 /// Data for a typedef.
336 #[derive(Debug, RustcEncodable)]
337 pub struct TypeDefData {
338     pub id: NodeId,
339     pub name: String,
340     pub span: Span,
341     pub qualname: String,
342     pub value: String,
343     pub visibility: Visibility,
344     pub parent: Option<DefId>,
345     pub docs: String,
346 }
347
348 /// Data for a reference to a type or trait.
349 #[derive(Clone, Debug, RustcEncodable)]
350 pub struct TypeRefData {
351     pub span: Span,
352     pub scope: NodeId,
353     pub ref_id: Option<DefId>,
354     pub qualname: String,
355 }
356
357 #[derive(Debug, RustcEncodable)]
358 pub struct UseData {
359     pub id: NodeId,
360     pub span: Span,
361     pub name: String,
362     pub mod_id: Option<DefId>,
363     pub scope: NodeId,
364     pub visibility: Visibility,
365 }
366
367 #[derive(Debug, RustcEncodable)]
368 pub struct UseGlobData {
369     pub id: NodeId,
370     pub span: Span,
371     pub names: Vec<String>,
372     pub scope: NodeId,
373     pub visibility: Visibility,
374 }
375
376 /// Data for local and global variables (consts and statics).
377 #[derive(Debug, RustcEncodable)]
378 pub struct VariableData {
379     pub id: NodeId,
380     pub kind: VariableKind,
381     pub name: String,
382     pub qualname: String,
383     pub span: Span,
384     pub scope: NodeId,
385     pub parent: Option<DefId>,
386     pub value: String,
387     pub type_value: String,
388     pub visibility: Visibility,
389     pub docs: String,
390     pub sig: Option<Signature>,
391 }
392
393 #[derive(Debug, RustcEncodable)]
394 pub enum VariableKind {
395     Static,
396     Const,
397     Local,
398     Field,
399 }
400
401 /// Data for the use of some item (e.g., the use of a local variable, which
402 /// will refer to that variables declaration (by ref_id)).
403 #[derive(Debug, RustcEncodable)]
404 pub struct VariableRefData {
405     pub name: String,
406     pub span: Span,
407     pub scope: NodeId,
408     pub ref_id: DefId,
409 }
410
411
412 /// Encodes information about the signature of a definition. This should have
413 /// enough information to create a nice display about a definition without
414 /// access to the source code.
415 #[derive(Debug, RustcEncodable)]
416 pub struct Signature {
417     pub span: Span,
418     pub text: String,
419     // These identify the main identifier for the defintion as byte offsets into
420     // `text`. E.g., of `foo` in `pub fn foo(...)`
421     pub ident_start: usize,
422     pub ident_end: usize,
423     pub defs: Vec<SigElement>,
424     pub refs: Vec<SigElement>,
425 }
426
427 /// An element of a signature. `start` and `end` are byte offsets into the `text`
428 /// of the parent `Signature`.
429 #[derive(Debug, RustcEncodable)]
430 pub struct SigElement {
431     pub id: DefId,
432     pub start: usize,
433     pub end: usize,
434 }