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, Attribute, 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,
139 pub attributes: Vec<Attribute>,
142 /// Data for extern crates.
143 #[derive(Debug, RustcEncodable)]
144 pub struct ExternCrateData {
147 pub crate_num: CrateNum,
148 pub location: String,
153 /// Data about a function call.
154 #[derive(Debug, RustcEncodable)]
155 pub struct FunctionCallData {
161 /// Data for all kinds of functions and methods.
162 #[derive(Clone, Debug, RustcEncodable)]
163 pub struct FunctionData {
166 pub qualname: String,
167 pub declaration: Option<DefId>,
171 pub visibility: Visibility,
172 pub parent: Option<DefId>,
175 pub attributes: Vec<Attribute>,
178 /// Data about a function call.
179 #[derive(Debug, RustcEncodable)]
180 pub struct FunctionRefData {
186 #[derive(Debug, RustcEncodable)]
187 pub struct ImplData {
191 pub trait_ref: Option<DefId>,
192 pub self_ref: Option<DefId>,
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 {
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
205 pub trait_ref: Option<TypeRefData>,
206 pub self_ref: Option<TypeRefData>,
209 #[derive(Debug, RustcEncodable)]
210 pub struct InheritanceData {
216 /// Data about a macro declaration.
217 #[derive(Debug, RustcEncodable)]
218 pub struct MacroData {
221 pub qualname: String,
225 /// Data about a macro use.
226 #[derive(Debug, RustcEncodable)]
227 pub struct MacroUseData {
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,
238 /// Data about a method call.
239 #[derive(Debug, RustcEncodable)]
240 pub struct MethodCallData {
243 pub ref_id: Option<DefId>,
244 pub decl_id: Option<DefId>,
247 /// Data for method declarations (methods with a body are treated as functions).
248 #[derive(Clone, Debug, RustcEncodable)]
249 pub struct MethodData {
252 pub qualname: String,
256 pub decl_id: Option<DefId>,
257 pub parent: Option<DefId>,
258 pub visibility: Visibility,
261 pub attributes: Vec<Attribute>,
264 /// Data for modules.
265 #[derive(Debug, RustcEncodable)]
269 pub qualname: String,
272 pub filename: String,
273 pub items: Vec<NodeId>,
274 pub visibility: Visibility,
277 pub attributes: Vec<Attribute>,
280 /// Data for a reference to a module.
281 #[derive(Debug, RustcEncodable)]
282 pub struct ModRefData {
285 pub ref_id: Option<DefId>,
289 #[derive(Debug, RustcEncodable)]
290 pub struct StructData {
295 pub qualname: String,
298 pub fields: Vec<NodeId>,
299 pub visibility: Visibility,
302 pub attributes: Vec<Attribute>,
305 #[derive(Debug, RustcEncodable)]
306 pub struct StructVariantData {
310 pub qualname: String,
311 pub type_value: String,
314 pub parent: Option<DefId>,
317 pub attributes: Vec<Attribute>,
320 #[derive(Debug, RustcEncodable)]
321 pub struct TraitData {
325 pub qualname: String,
328 pub items: Vec<NodeId>,
329 pub visibility: Visibility,
332 pub attributes: Vec<Attribute>,
335 #[derive(Debug, RustcEncodable)]
336 pub struct TupleVariantData {
340 pub qualname: String,
341 pub type_value: String,
344 pub parent: Option<DefId>,
347 pub attributes: Vec<Attribute>,
350 /// Data for a typedef.
351 #[derive(Debug, RustcEncodable)]
352 pub struct TypeDefData {
356 pub qualname: String,
358 pub visibility: Visibility,
359 pub parent: Option<DefId>,
361 pub sig: Option<Signature>,
362 pub attributes: Vec<Attribute>,
365 /// Data for a reference to a type or trait.
366 #[derive(Clone, Debug, RustcEncodable)]
367 pub struct TypeRefData {
370 pub ref_id: Option<DefId>,
371 pub qualname: String,
374 #[derive(Debug, RustcEncodable)]
379 pub mod_id: Option<DefId>,
381 pub visibility: Visibility,
384 #[derive(Debug, RustcEncodable)]
385 pub struct UseGlobData {
388 pub names: Vec<String>,
390 pub visibility: Visibility,
393 /// Data for local and global variables (consts and statics).
394 #[derive(Debug, RustcEncodable)]
395 pub struct VariableData {
397 pub kind: VariableKind,
399 pub qualname: String,
402 pub parent: Option<DefId>,
404 pub type_value: String,
405 pub visibility: Visibility,
407 pub sig: Option<Signature>,
408 pub attributes: Vec<Attribute>,
411 #[derive(Debug, RustcEncodable)]
412 pub enum VariableKind {
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 {
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 {
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>,
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 {