]>
git.proxmox.com Git - rustc.git/blob - 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.
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.
16 use std
::hash
::Hasher
;
18 use rustc
::hir
::def_id
::DefId
;
20 use syntax
::ast
::{CrateNum, NodeId}
;
21 use syntax
::codemap
::Span
;
23 pub struct CrateData
{
28 /// Data for any entity in the Rust language. The actual data contained varies
29 /// with the kind of entity being queried. See the nested structs for details.
34 /// Data for extern crates.
35 ExternCrateData(ExternCrateData
),
36 /// Data about a function call.
37 FunctionCallData(FunctionCallData
),
38 /// Data for all kinds of functions and methods.
39 FunctionData(FunctionData
),
40 /// Data about a function ref.
41 FunctionRefData(FunctionRefData
),
44 /// Data for trait inheritance.
45 InheritanceData(InheritanceData
),
46 /// Data about a macro declaration.
48 /// Data about a macro use.
49 MacroUseData(MacroUseData
),
50 /// Data about a method call.
51 MethodCallData(MethodCallData
),
52 /// Data for method declarations (methods with a body are treated as functions).
53 MethodData(MethodData
),
56 /// Data for a reference to a module.
57 ModRefData(ModRefData
),
58 /// Data for a struct declaration.
59 StructData(StructData
),
60 /// Data for a struct variant.
61 StructVariantDat(StructVariantData
),
62 /// Data for a trait declaration.
64 /// Data for a tuple variant.
65 TupleVariantData(TupleVariantData
),
66 /// Data for a typedef.
67 TypeDefData(TypedefData
),
68 /// Data for a reference to a type or trait.
69 TypeRefData(TypeRefData
),
70 /// Data for a use statement.
72 /// Data for a global use statement.
73 UseGlobData(UseGlobData
),
74 /// Data for local and global variables (consts and statics), and fields.
75 VariableData(VariableData
),
76 /// Data for the use of some variable (e.g., the use of a local variable, which
77 /// will refere to that variables declaration).
78 VariableRefData(VariableRefData
),
81 /// Data for the prelude of a crate.
83 pub struct CratePreludeData
{
84 pub crate_name
: String
,
85 pub crate_root
: Option
<String
>,
86 pub external_crates
: Vec
<ExternalCrateData
>
89 /// Data for external crates in the prelude of a crate.
91 pub struct ExternalCrateData
{
96 /// Data for enum declarations.
97 #[derive(Clone, Debug)]
101 pub qualname
: String
,
106 /// Data for extern crates.
108 pub struct ExternCrateData
{
111 pub crate_num
: CrateNum
,
112 pub location
: String
,
117 /// Data about a function call.
119 pub struct FunctionCallData
{
125 /// Data for all kinds of functions and methods.
126 #[derive(Clone, Debug)]
127 pub struct FunctionData
{
130 pub qualname
: String
,
131 pub declaration
: Option
<DefId
>,
136 /// Data about a function call.
138 pub struct FunctionRefData
{
145 pub struct ImplData
{
149 pub trait_ref
: Option
<DefId
>,
150 pub self_ref
: Option
<DefId
>,
154 // FIXME: this struct should not exist. However, removing it requires heavy
155 // refactoring of dump_visitor.rs. See PR 31838 for more info.
156 pub struct ImplData2
{
160 // FIXME: I'm not really sure inline data is the best way to do this. Seems
161 // OK in this case, but generalising leads to returning chunks of AST, which
163 pub trait_ref
: Option
<TypeRefData
>,
164 pub self_ref
: Option
<TypeRefData
>,
168 pub struct InheritanceData
{
174 /// Data about a macro declaration.
176 pub struct MacroData
{
179 pub qualname
: String
,
182 /// Data about a macro use.
184 pub struct MacroUseData
{
187 pub qualname
: String
,
188 // Because macro expansion happens before ref-ids are determined,
189 // we use the callee span to reference the associated macro definition.
190 pub callee_span
: Span
,
195 /// Data about a method call.
197 pub struct MethodCallData
{
200 pub ref_id
: Option
<DefId
>,
201 pub decl_id
: Option
<DefId
>,
204 /// Data for method declarations (methods with a body are treated as functions).
205 #[derive(Clone, Debug)]
206 pub struct MethodData
{
208 pub qualname
: String
,
213 /// Data for modules.
218 pub qualname
: String
,
221 pub filename
: String
,
224 /// Data for a reference to a module.
226 pub struct ModRefData
{
229 pub ref_id
: Option
<DefId
>,
234 pub struct StructData
{
238 pub qualname
: String
,
244 pub struct StructVariantData
{
247 pub qualname
: String
,
248 pub type_value
: String
,
254 pub struct TraitData
{
257 pub qualname
: String
,
263 pub struct TupleVariantData
{
267 pub qualname
: String
,
268 pub type_value
: String
,
273 /// Data for a typedef.
275 pub struct TypedefData
{
278 pub qualname
: String
,
282 /// Data for a reference to a type or trait.
283 #[derive(Clone, Debug)]
284 pub struct TypeRefData
{
287 pub ref_id
: Option
<DefId
>,
288 pub qualname
: String
,
296 pub mod_id
: Option
<DefId
>,
301 pub struct UseGlobData
{
304 pub names
: Vec
<String
>,
308 /// Data for local and global variables (consts and statics).
310 pub struct VariableData
{
313 pub qualname
: String
,
317 pub type_value
: String
,
320 /// Data for the use of some item (e.g., the use of a local variable, which
321 /// will refer to that variables declaration (by ref_id)).
323 pub struct VariableRefData
{
330 // Emitted ids are used to cross-reference items across crates. DefIds and
331 // NodeIds do not usually correspond in any way. The strategy is to use the
332 // index from the DefId as a crate-local id. However, within a crate, DefId
333 // indices and NodeIds can overlap. So, we must adjust the NodeIds. If an
334 // item can be identified by a DefId as well as a NodeId, then we use the
335 // DefId index as the id. If it can't, then we have to use the NodeId, but
336 // need to adjust it so it will not clash with any possible DefId index.
337 pub fn normalize_node_id
<'a
>(tcx
: &ty
::TyCtxt
<'a
>, id
: NodeId
) -> usize {
338 match tcx
.map
.opt_local_def_id(id
) {
339 Some(id
) => id
.index
.as_usize(),
340 None
=> id
as usize + tcx
.map
.num_local_def_ids()
344 // Macro to implement a normalize() function (see below for usage)
345 macro_rules
! impl_normalize
{
346 ($
($t
:ty
=> $
($field
:ident
),*);*) => {
349 pub fn normalize
<'a
>(mut self, tcx
: &ty
::TyCtxt
<'a
>) -> $t
{
351 self.$field
= normalize_node_id(tcx
, self.$field
) as u32;
361 EnumData
=> id
, scope
;
362 ExternCrateData
=> id
, scope
;
363 FunctionCallData
=> scope
;
364 FunctionData
=> id
, scope
;
365 FunctionRefData
=> scope
;
366 ImplData
=> id
, scope
;
367 InheritanceData
=> deriv_id
;
368 MacroUseData
=> scope
;
369 MethodCallData
=> scope
;
370 MethodData
=> id
, scope
;
371 ModData
=> id
, scope
;
373 StructData
=> ctor_id
, id
, scope
;
374 StructVariantData
=> id
, scope
;
375 TupleVariantData
=> id
, scope
;
376 TraitData
=> id
, scope
;
378 TypeRefData
=> scope
;
379 UseData
=> id
, scope
;
380 UseGlobData
=> id
, scope
;
382 VariableRefData
=> scope