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 use rls_data::ExternalCrateData;
23 pub struct CrateData {
29 /// Data for any entity in the Rust language. The actual data contained varies
30 /// with the kind of entity being queried. See the nested structs for details.
35 /// Data for extern crates.
36 ExternCrateData(ExternCrateData),
37 /// Data about a function call.
38 FunctionCallData(FunctionCallData),
39 /// Data for all kinds of functions and methods.
40 FunctionData(FunctionData),
41 /// Data about a function ref.
42 FunctionRefData(FunctionRefData),
45 /// Data for trait inheritance.
46 InheritanceData(InheritanceData),
47 /// Data about a macro declaration.
49 /// Data about a macro use.
50 MacroUseData(MacroUseData),
51 /// Data about a method call.
52 MethodCallData(MethodCallData),
53 /// Data for method declarations (methods with a body are treated as functions).
54 MethodData(MethodData),
57 /// Data for a reference to a module.
58 ModRefData(ModRefData),
59 /// Data for a struct declaration.
60 StructData(StructData),
61 /// Data for a struct variant.
62 StructVariantDat(StructVariantData),
63 /// Data for a trait declaration.
65 /// Data for a tuple variant.
66 TupleVariantData(TupleVariantData),
67 /// Data for a typedef.
68 TypeDefData(TypeDefData),
69 /// Data for a reference to a type or trait.
70 TypeRefData(TypeRefData),
71 /// Data for a use statement.
73 /// Data for a global use statement.
74 UseGlobData(UseGlobData),
75 /// Data for local and global variables (consts and statics), and fields.
76 VariableData(VariableData),
77 /// Data for the use of some variable (e.g., the use of a local variable, which
78 /// will refere to that variables declaration).
79 VariableRefData(VariableRefData),
82 #[derive(Eq, PartialEq, Clone, Copy, Debug)]
89 impl<'a> From<&'a ast::Visibility> for Visibility {
90 fn from(v: &'a ast::Visibility) -> Visibility {
92 ast::Visibility::Public => Visibility::Public,
93 ast::Visibility::Crate(_) => Visibility::Restricted,
94 ast::Visibility::Restricted { .. } => Visibility::Restricted,
95 ast::Visibility::Inherited => Visibility::Inherited,
100 impl<'a> From<&'a hir::Visibility> for Visibility {
101 fn from(v: &'a hir::Visibility) -> Visibility {
103 hir::Visibility::Public => Visibility::Public,
104 hir::Visibility::Crate => Visibility::Restricted,
105 hir::Visibility::Restricted { .. } => Visibility::Restricted,
106 hir::Visibility::Inherited => Visibility::Inherited,
111 /// Data for the prelude of a crate.
113 pub struct CratePreludeData {
114 pub crate_name: String,
115 pub crate_root: String,
116 pub external_crates: Vec<ExternalCrateData>,
120 /// Data for enum declarations.
121 #[derive(Clone, Debug)]
122 pub struct EnumData {
126 pub qualname: String,
129 pub variants: Vec<NodeId>,
130 pub visibility: Visibility,
133 pub attributes: Vec<Attribute>,
136 /// Data for extern crates.
138 pub struct ExternCrateData {
141 pub crate_num: CrateNum,
142 pub location: String,
147 /// Data about a function call.
149 pub struct FunctionCallData {
155 /// Data for all kinds of functions and methods.
156 #[derive(Clone, Debug)]
157 pub struct FunctionData {
160 pub qualname: String,
161 pub declaration: Option<DefId>,
165 pub visibility: Visibility,
166 pub parent: Option<DefId>,
169 pub attributes: Vec<Attribute>,
172 /// Data about a function call.
174 pub struct FunctionRefData {
181 pub struct ImplData {
185 pub trait_ref: Option<DefId>,
186 pub self_ref: Option<DefId>,
190 // FIXME: this struct should not exist. However, removing it requires heavy
191 // refactoring of dump_visitor.rs. See PR 31838 for more info.
192 pub struct ImplData2 {
196 // FIXME: I'm not really sure inline data is the best way to do this. Seems
197 // OK in this case, but generalising leads to returning chunks of AST, which
199 pub trait_ref: Option<TypeRefData>,
200 pub self_ref: Option<TypeRefData>,
204 pub struct InheritanceData {
210 /// Data about a macro declaration.
212 pub struct MacroData {
215 pub qualname: String,
219 /// Data about a macro use.
221 pub struct MacroUseData {
224 pub qualname: String,
225 // Because macro expansion happens before ref-ids are determined,
226 // we use the callee span to reference the associated macro definition.
227 pub callee_span: Span,
232 /// Data about a method call.
234 pub struct MethodCallData {
237 pub ref_id: Option<DefId>,
238 pub decl_id: Option<DefId>,
241 /// Data for method declarations (methods with a body are treated as functions).
242 #[derive(Clone, Debug)]
243 pub struct MethodData {
246 pub qualname: String,
250 pub decl_id: Option<DefId>,
251 pub parent: Option<DefId>,
252 pub visibility: Visibility,
255 pub attributes: Vec<Attribute>,
258 /// Data for modules.
263 pub qualname: String,
266 pub filename: String,
267 pub items: Vec<NodeId>,
268 pub visibility: Visibility,
270 pub sig: Option<Signature>,
271 pub attributes: Vec<Attribute>,
274 /// Data for a reference to a module.
276 pub struct ModRefData {
279 pub ref_id: Option<DefId>,
284 pub struct StructData {
289 pub qualname: String,
292 pub fields: Vec<NodeId>,
293 pub visibility: Visibility,
296 pub attributes: Vec<Attribute>,
300 pub struct StructVariantData {
304 pub qualname: String,
305 pub type_value: String,
308 pub parent: Option<DefId>,
311 pub attributes: Vec<Attribute>,
315 pub struct TraitData {
319 pub qualname: String,
322 pub items: Vec<NodeId>,
323 pub visibility: Visibility,
326 pub attributes: Vec<Attribute>,
330 pub struct TupleVariantData {
334 pub qualname: String,
335 pub type_value: String,
338 pub parent: Option<DefId>,
341 pub attributes: Vec<Attribute>,
344 /// Data for a typedef.
346 pub struct TypeDefData {
350 pub qualname: String,
352 pub visibility: Visibility,
353 pub parent: Option<DefId>,
355 pub sig: Option<Signature>,
356 pub attributes: Vec<Attribute>,
359 /// Data for a reference to a type or trait.
360 #[derive(Clone, Debug)]
361 pub struct TypeRefData {
364 pub ref_id: Option<DefId>,
365 pub qualname: String,
373 pub mod_id: Option<DefId>,
375 pub visibility: Visibility,
379 pub struct UseGlobData {
382 pub names: Vec<String>,
384 pub visibility: Visibility,
387 /// Data for local and global variables (consts and statics).
389 pub struct VariableData {
391 pub kind: VariableKind,
393 pub qualname: String,
396 pub parent: Option<DefId>,
398 pub type_value: String,
399 pub visibility: Visibility,
401 pub sig: Option<Signature>,
402 pub attributes: Vec<Attribute>,
406 pub enum VariableKind {
413 /// Data for the use of some item (e.g., the use of a local variable, which
414 /// will refer to that variables declaration (by ref_id)).
416 pub struct VariableRefData {
424 /// Encodes information about the signature of a definition. This should have
425 /// enough information to create a nice display about a definition without
426 /// access to the source code.
427 #[derive(Clone, Debug)]
428 pub struct Signature {
431 // These identify the main identifier for the defintion as byte offsets into
432 // `text`. E.g., of `foo` in `pub fn foo(...)`
433 pub ident_start: usize,
434 pub ident_end: usize,
435 pub defs: Vec<SigElement>,
436 pub refs: Vec<SigElement>,
439 /// An element of a signature. `start` and `end` are byte offsets into the `text`
440 /// of the parent `Signature`.
441 #[derive(Clone, Debug)]
442 pub struct SigElement {