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.
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.
11 //! Structs representing the analysis data from a crate.
13 //! The `Dump` trait can be used together with `DumpVisitor` in order to
14 //! retrieve the data from a crate.
17 use rustc::hir::def_id::{CrateNum, DefId};
18 use syntax::ast::{self, NodeId};
21 pub struct CrateData {
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)]
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),
43 /// Data for trait inheritance.
44 InheritanceData(InheritanceData),
45 /// Data about a macro declaration.
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),
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.
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.
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),
80 #[derive(Eq, PartialEq, Clone, Copy, Debug, RustcEncodable)]
87 impl<'a> From<&'a ast::Visibility> for Visibility {
88 fn from(v: &'a ast::Visibility) -> Visibility {
90 ast::Visibility::Public => Visibility::Public,
91 ast::Visibility::Crate(_) => Visibility::Restricted,
92 ast::Visibility::Restricted { .. } => Visibility::Restricted,
93 ast::Visibility::Inherited => Visibility::Inherited,
98 impl<'a> From<&'a hir::Visibility> for Visibility {
99 fn from(v: &'a hir::Visibility) -> Visibility {
101 hir::Visibility::Public => Visibility::Public,
102 hir::Visibility::Crate => Visibility::Restricted,
103 hir::Visibility::Restricted { .. } => Visibility::Restricted,
104 hir::Visibility::Inherited => Visibility::Inherited,
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>,
118 /// Data for external crates in the prelude of a crate.
119 #[derive(Debug, RustcEncodable)]
120 pub struct ExternalCrateData {
123 pub file_name: String,
126 /// Data for enum declarations.
127 #[derive(Clone, Debug, RustcEncodable)]
128 pub struct EnumData {
132 pub qualname: String,
135 pub variants: Vec<NodeId>,
136 pub visibility: Visibility,
140 /// Data for extern crates.
141 #[derive(Debug, RustcEncodable)]
142 pub struct ExternCrateData {
145 pub crate_num: CrateNum,
146 pub location: String,
151 /// Data about a function call.
152 #[derive(Debug, RustcEncodable)]
153 pub struct FunctionCallData {
159 /// Data for all kinds of functions and methods.
160 #[derive(Clone, Debug, RustcEncodable)]
161 pub struct FunctionData {
164 pub qualname: String,
165 pub declaration: Option<DefId>,
169 pub visibility: Visibility,
170 pub parent: Option<DefId>,
174 /// Data about a function call.
175 #[derive(Debug, RustcEncodable)]
176 pub struct FunctionRefData {
182 #[derive(Debug, RustcEncodable)]
183 pub struct ImplData {
187 pub trait_ref: Option<DefId>,
188 pub self_ref: Option<DefId>,
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 {
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
201 pub trait_ref: Option<TypeRefData>,
202 pub self_ref: Option<TypeRefData>,
205 #[derive(Debug, RustcEncodable)]
206 pub struct InheritanceData {
212 /// Data about a macro declaration.
213 #[derive(Debug, RustcEncodable)]
214 pub struct MacroData {
217 pub qualname: String,
221 /// Data about a macro use.
222 #[derive(Debug, RustcEncodable)]
223 pub struct MacroUseData {
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,
234 /// Data about a method call.
235 #[derive(Debug, RustcEncodable)]
236 pub struct MethodCallData {
239 pub ref_id: Option<DefId>,
240 pub decl_id: Option<DefId>,
243 /// Data for method declarations (methods with a body are treated as functions).
244 #[derive(Clone, Debug, RustcEncodable)]
245 pub struct MethodData {
248 pub qualname: String,
252 pub decl_id: Option<DefId>,
253 pub parent: Option<DefId>,
254 pub visibility: Visibility,
258 /// Data for modules.
259 #[derive(Debug, RustcEncodable)]
263 pub qualname: String,
266 pub filename: String,
267 pub items: Vec<NodeId>,
268 pub visibility: Visibility,
272 /// Data for a reference to a module.
273 #[derive(Debug, RustcEncodable)]
274 pub struct ModRefData {
277 pub ref_id: Option<DefId>,
281 #[derive(Debug, RustcEncodable)]
282 pub struct StructData {
287 pub qualname: String,
290 pub fields: Vec<NodeId>,
291 pub visibility: Visibility,
295 #[derive(Debug, RustcEncodable)]
296 pub struct StructVariantData {
300 pub qualname: String,
301 pub type_value: String,
304 pub parent: Option<DefId>,
308 #[derive(Debug, RustcEncodable)]
309 pub struct TraitData {
313 pub qualname: String,
316 pub items: Vec<NodeId>,
317 pub visibility: Visibility,
321 #[derive(Debug, RustcEncodable)]
322 pub struct TupleVariantData {
326 pub qualname: String,
327 pub type_value: String,
330 pub parent: Option<DefId>,
334 /// Data for a typedef.
335 #[derive(Debug, RustcEncodable)]
336 pub struct TypeDefData {
340 pub qualname: String,
342 pub visibility: Visibility,
343 pub parent: Option<DefId>,
347 /// Data for a reference to a type or trait.
348 #[derive(Clone, Debug, RustcEncodable)]
349 pub struct TypeRefData {
352 pub ref_id: Option<DefId>,
353 pub qualname: String,
356 #[derive(Debug, RustcEncodable)]
361 pub mod_id: Option<DefId>,
363 pub visibility: Visibility,
366 #[derive(Debug, RustcEncodable)]
367 pub struct UseGlobData {
370 pub names: Vec<String>,
372 pub visibility: Visibility,
375 /// Data for local and global variables (consts and statics).
376 #[derive(Debug, RustcEncodable)]
377 pub struct VariableData {
379 pub kind: VariableKind,
381 pub qualname: String,
384 pub parent: Option<DefId>,
386 pub type_value: String,
387 pub visibility: Visibility,
391 #[derive(Debug, RustcEncodable)]
392 pub enum VariableKind {
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 {