]> git.proxmox.com Git - rustc.git/blob - src/librustc_save_analysis/data.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.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 std::hash::Hasher;
17
18 use rustc::hir::def_id::DefId;
19 use rustc::ty;
20 use syntax::ast::{CrateNum, NodeId};
21 use syntax::codemap::Span;
22
23 pub struct CrateData {
24 pub name: String,
25 pub number: u32,
26 }
27
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.
30 #[derive(Debug)]
31 pub enum Data {
32 /// Data for Enums.
33 EnumData(EnumData),
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),
42 /// Data for impls.
43 ImplData(ImplData2),
44 /// Data for trait inheritance.
45 InheritanceData(InheritanceData),
46 /// Data about a macro declaration.
47 MacroData(MacroData),
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),
54 /// Data for modules.
55 ModData(ModData),
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.
63 TraitData(TraitData),
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.
71 UseData(UseData),
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),
79 }
80
81 /// Data for the prelude of a crate.
82 #[derive(Debug)]
83 pub struct CratePreludeData {
84 pub crate_name: String,
85 pub crate_root: Option<String>,
86 pub external_crates: Vec<ExternalCrateData>
87 }
88
89 /// Data for external crates in the prelude of a crate.
90 #[derive(Debug)]
91 pub struct ExternalCrateData {
92 pub name: String,
93 pub num: CrateNum
94 }
95
96 /// Data for enum declarations.
97 #[derive(Clone, Debug)]
98 pub struct EnumData {
99 pub id: NodeId,
100 pub value: String,
101 pub qualname: String,
102 pub span: Span,
103 pub scope: NodeId,
104 }
105
106 /// Data for extern crates.
107 #[derive(Debug)]
108 pub struct ExternCrateData {
109 pub id: NodeId,
110 pub name: String,
111 pub crate_num: CrateNum,
112 pub location: String,
113 pub span: Span,
114 pub scope: NodeId,
115 }
116
117 /// Data about a function call.
118 #[derive(Debug)]
119 pub struct FunctionCallData {
120 pub span: Span,
121 pub scope: NodeId,
122 pub ref_id: DefId,
123 }
124
125 /// Data for all kinds of functions and methods.
126 #[derive(Clone, Debug)]
127 pub struct FunctionData {
128 pub id: NodeId,
129 pub name: String,
130 pub qualname: String,
131 pub declaration: Option<DefId>,
132 pub span: Span,
133 pub scope: NodeId,
134 }
135
136 /// Data about a function call.
137 #[derive(Debug)]
138 pub struct FunctionRefData {
139 pub span: Span,
140 pub scope: NodeId,
141 pub ref_id: DefId,
142 }
143
144 #[derive(Debug)]
145 pub struct ImplData {
146 pub id: NodeId,
147 pub span: Span,
148 pub scope: NodeId,
149 pub trait_ref: Option<DefId>,
150 pub self_ref: Option<DefId>,
151 }
152
153 #[derive(Debug)]
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 {
157 pub id: NodeId,
158 pub span: Span,
159 pub scope: NodeId,
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
162 // feels wrong.
163 pub trait_ref: Option<TypeRefData>,
164 pub self_ref: Option<TypeRefData>,
165 }
166
167 #[derive(Debug)]
168 pub struct InheritanceData {
169 pub span: Span,
170 pub base_id: DefId,
171 pub deriv_id: NodeId
172 }
173
174 /// Data about a macro declaration.
175 #[derive(Debug)]
176 pub struct MacroData {
177 pub span: Span,
178 pub name: String,
179 pub qualname: String,
180 }
181
182 /// Data about a macro use.
183 #[derive(Debug)]
184 pub struct MacroUseData {
185 pub span: Span,
186 pub name: String,
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,
191 pub scope: NodeId,
192 pub imported: bool,
193 }
194
195 /// Data about a method call.
196 #[derive(Debug)]
197 pub struct MethodCallData {
198 pub span: Span,
199 pub scope: NodeId,
200 pub ref_id: Option<DefId>,
201 pub decl_id: Option<DefId>,
202 }
203
204 /// Data for method declarations (methods with a body are treated as functions).
205 #[derive(Clone, Debug)]
206 pub struct MethodData {
207 pub id: NodeId,
208 pub qualname: String,
209 pub span: Span,
210 pub scope: NodeId,
211 }
212
213 /// Data for modules.
214 #[derive(Debug)]
215 pub struct ModData {
216 pub id: NodeId,
217 pub name: String,
218 pub qualname: String,
219 pub span: Span,
220 pub scope: NodeId,
221 pub filename: String,
222 }
223
224 /// Data for a reference to a module.
225 #[derive(Debug)]
226 pub struct ModRefData {
227 pub span: Span,
228 pub scope: NodeId,
229 pub ref_id: Option<DefId>,
230 pub qualname: String
231 }
232
233 #[derive(Debug)]
234 pub struct StructData {
235 pub span: Span,
236 pub id: NodeId,
237 pub ctor_id: NodeId,
238 pub qualname: String,
239 pub scope: NodeId,
240 pub value: String
241 }
242
243 #[derive(Debug)]
244 pub struct StructVariantData {
245 pub span: Span,
246 pub id: NodeId,
247 pub qualname: String,
248 pub type_value: String,
249 pub value: String,
250 pub scope: NodeId
251 }
252
253 #[derive(Debug)]
254 pub struct TraitData {
255 pub span: Span,
256 pub id: NodeId,
257 pub qualname: String,
258 pub scope: NodeId,
259 pub value: String
260 }
261
262 #[derive(Debug)]
263 pub struct TupleVariantData {
264 pub span: Span,
265 pub id: NodeId,
266 pub name: String,
267 pub qualname: String,
268 pub type_value: String,
269 pub value: String,
270 pub scope: NodeId
271 }
272
273 /// Data for a typedef.
274 #[derive(Debug)]
275 pub struct TypedefData {
276 pub id: NodeId,
277 pub span: Span,
278 pub qualname: String,
279 pub value: String,
280 }
281
282 /// Data for a reference to a type or trait.
283 #[derive(Clone, Debug)]
284 pub struct TypeRefData {
285 pub span: Span,
286 pub scope: NodeId,
287 pub ref_id: Option<DefId>,
288 pub qualname: String,
289 }
290
291 #[derive(Debug)]
292 pub struct UseData {
293 pub id: NodeId,
294 pub span: Span,
295 pub name: String,
296 pub mod_id: Option<DefId>,
297 pub scope: NodeId
298 }
299
300 #[derive(Debug)]
301 pub struct UseGlobData {
302 pub id: NodeId,
303 pub span: Span,
304 pub names: Vec<String>,
305 pub scope: NodeId
306 }
307
308 /// Data for local and global variables (consts and statics).
309 #[derive(Debug)]
310 pub struct VariableData {
311 pub id: NodeId,
312 pub name: String,
313 pub qualname: String,
314 pub span: Span,
315 pub scope: NodeId,
316 pub value: String,
317 pub type_value: String,
318 }
319
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)).
322 #[derive(Debug)]
323 pub struct VariableRefData {
324 pub name: String,
325 pub span: Span,
326 pub scope: NodeId,
327 pub ref_id: DefId,
328 }
329
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()
341 }
342 }
343
344 // Macro to implement a normalize() function (see below for usage)
345 macro_rules! impl_normalize {
346 ($($t:ty => $($field:ident),*);*) => {
347 $(
348 impl $t {
349 pub fn normalize<'a>(mut self, tcx: &ty::TyCtxt<'a>) -> $t {
350 $(
351 self.$field = normalize_node_id(tcx, self.$field) as u32;
352 )*
353 self
354 }
355 }
356 )*
357 }
358 }
359
360 impl_normalize! {
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;
372 ModRefData => scope;
373 StructData => ctor_id, id, scope;
374 StructVariantData => id, scope;
375 TupleVariantData => id, scope;
376 TraitData => id, scope;
377 TypedefData => id;
378 TypeRefData => scope;
379 UseData => id, scope;
380 UseGlobData => id, scope;
381 VariableData => id;
382 VariableRefData => scope
383 }