]>
Commit | Line | Data |
---|---|---|
d9579d0f AL |
1 | // Copyright 2015 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 | ||
54a0048b | 11 | //! Write the output of rustc's analysis to an implementor of Dump. The data is |
d9579d0f AL |
12 | //! primarily designed to be used as input to the DXR tool, specifically its |
13 | //! Rust plugin. It could also be used by IDEs or other code browsing, search, or | |
14 | //! cross-referencing tools. | |
15 | //! | |
16 | //! Dumping the analysis is implemented by walking the AST and getting a bunch of | |
17 | //! info out from all over the place. We use Def IDs to identify objects. The | |
18 | //! tricky part is getting syntactic (span, source text) and semantic (reference | |
19 | //! Def IDs) information for parts of expressions which the compiler has discarded. | |
20 | //! E.g., in a path `foo::bar::baz`, the compiler only keeps a span for the whole | |
21 | //! path and a reference to `baz`, but we want spans and references for all three | |
22 | //! idents. | |
23 | //! | |
24 | //! SpanUtils is used to manipulate spans. In particular, to extract sub-spans | |
25 | //! from spans (e.g., the span for `bar` from the above example path). | |
54a0048b SL |
26 | //! DumpVisitor walks the AST and processes it, and an implementor of Dump |
27 | //! is used for recording the output in a format-agnostic way (see CsvDumper | |
28 | //! for an example). | |
d9579d0f | 29 | |
54a0048b SL |
30 | use rustc::hir::def::Def; |
31 | use rustc::hir::def_id::DefId; | |
32 | use rustc::session::Session; | |
33 | use rustc::ty::{self, TyCtxt}; | |
d9579d0f | 34 | |
7453a54e | 35 | use std::collections::HashSet; |
54a0048b | 36 | use std::hash::*; |
d9579d0f | 37 | |
7453a54e | 38 | use syntax::ast::{self, NodeId, PatKind}; |
d9579d0f | 39 | use syntax::codemap::*; |
c1a9b12d | 40 | use syntax::parse::token::{self, keywords}; |
d9579d0f AL |
41 | use syntax::visit::{self, Visitor}; |
42 | use syntax::print::pprust::{path_to_string, ty_to_string}; | |
43 | use syntax::ptr::P; | |
44 | ||
54a0048b | 45 | use rustc::hir::lowering::{lower_expr, LoweringContext}; |
e9174d1e | 46 | |
54a0048b SL |
47 | use super::{escape, generated_code, SaveContext, PathCollector}; |
48 | use super::data::*; | |
49 | use super::dump::Dump; | |
d9579d0f | 50 | use super::span_utils::SpanUtils; |
54a0048b | 51 | use super::recorder; |
d9579d0f | 52 | |
62682a34 | 53 | macro_rules! down_cast_data { |
54a0048b | 54 | ($id:ident, $kind:ident, $sp:expr) => { |
62682a34 SL |
55 | let $id = if let super::Data::$kind(data) = $id { |
56 | data | |
57 | } else { | |
54a0048b | 58 | span_bug!($sp, "unexpected data kind: {:?}", $id); |
b039eaaf | 59 | } |
62682a34 SL |
60 | }; |
61 | } | |
d9579d0f | 62 | |
54a0048b | 63 | pub struct DumpVisitor<'l, 'tcx: 'l, D: 'l> { |
d9579d0f AL |
64 | save_ctxt: SaveContext<'l, 'tcx>, |
65 | sess: &'l Session, | |
54a0048b | 66 | tcx: &'l TyCtxt<'tcx>, |
b039eaaf | 67 | analysis: &'l ty::CrateAnalysis<'l>, |
54a0048b | 68 | dumper: &'l mut D, |
d9579d0f AL |
69 | |
70 | span: SpanUtils<'l>, | |
d9579d0f | 71 | |
e9174d1e | 72 | cur_scope: NodeId, |
7453a54e SL |
73 | |
74 | // Set of macro definition (callee) spans, and the set | |
75 | // of macro use (callsite) spans. We store these to ensure | |
76 | // we only write one macro def per unique macro definition, and | |
77 | // one macro use per unique callsite span. | |
78 | mac_defs: HashSet<Span>, | |
79 | mac_uses: HashSet<Span>, | |
80 | ||
d9579d0f AL |
81 | } |
82 | ||
54a0048b SL |
83 | impl <'l, 'tcx, D> DumpVisitor<'l, 'tcx, D> |
84 | where D: Dump | |
85 | { | |
86 | pub fn new(tcx: &'l TyCtxt<'tcx>, | |
b039eaaf SL |
87 | lcx: &'l LoweringContext<'l>, |
88 | analysis: &'l ty::CrateAnalysis<'l>, | |
54a0048b SL |
89 | dumper: &'l mut D) |
90 | -> DumpVisitor<'l, 'tcx, D> { | |
c1a9b12d | 91 | let span_utils = SpanUtils::new(&tcx.sess); |
54a0048b | 92 | DumpVisitor { |
62682a34 SL |
93 | sess: &tcx.sess, |
94 | tcx: tcx, | |
b039eaaf | 95 | save_ctxt: SaveContext::from_span_utils(tcx, lcx, span_utils.clone()), |
d9579d0f | 96 | analysis: analysis, |
54a0048b | 97 | dumper: dumper, |
62682a34 | 98 | span: span_utils.clone(), |
e9174d1e | 99 | cur_scope: 0, |
7453a54e SL |
100 | mac_defs: HashSet::new(), |
101 | mac_uses: HashSet::new(), | |
d9579d0f AL |
102 | } |
103 | } | |
104 | ||
e9174d1e | 105 | fn nest<F>(&mut self, scope_id: NodeId, f: F) |
54a0048b | 106 | where F: FnOnce(&mut DumpVisitor<'l, 'tcx, D>) |
d9579d0f AL |
107 | { |
108 | let parent_scope = self.cur_scope; | |
109 | self.cur_scope = scope_id; | |
110 | f(self); | |
111 | self.cur_scope = parent_scope; | |
112 | } | |
113 | ||
114 | pub fn dump_crate_info(&mut self, name: &str, krate: &ast::Crate) { | |
92a42be0 | 115 | let source_file = self.tcx.sess.local_crate_source_file.as_ref(); |
54a0048b SL |
116 | let crate_root = source_file.map(|source_file| { |
117 | match source_file.file_name() { | |
92a42be0 SL |
118 | Some(_) => source_file.parent().unwrap().display().to_string(), |
119 | None => source_file.display().to_string(), | |
54a0048b SL |
120 | } |
121 | }); | |
122 | ||
123 | // Info about all the external crates referenced from this crate. | |
124 | let external_crates = self.save_ctxt.get_external_crates().into_iter().map(|c| { | |
125 | ExternalCrateData { | |
126 | name: c.name, | |
127 | num: c.number | |
128 | } | |
129 | }).collect(); | |
92a42be0 | 130 | |
d9579d0f | 131 | // The current crate. |
54a0048b SL |
132 | let data = CratePreludeData { |
133 | crate_name: name.into(), | |
134 | crate_root: crate_root, | |
135 | external_crates: external_crates | |
136 | }; | |
d9579d0f | 137 | |
54a0048b | 138 | self.dumper.crate_prelude(krate.span, data); |
d9579d0f AL |
139 | } |
140 | ||
141 | // Return all non-empty prefixes of a path. | |
142 | // For each prefix, we return the span for the last segment in the prefix and | |
143 | // a str representation of the entire prefix. | |
144 | fn process_path_prefixes(&self, path: &ast::Path) -> Vec<(Span, String)> { | |
145 | let spans = self.span.spans_for_path_segments(path); | |
146 | ||
147 | // Paths to enums seem to not match their spans - the span includes all the | |
148 | // variants too. But they seem to always be at the end, so I hope we can cope with | |
149 | // always using the first ones. So, only error out if we don't have enough spans. | |
150 | // What could go wrong...? | |
151 | if spans.len() < path.segments.len() { | |
7453a54e SL |
152 | if generated_code(path.span) { |
153 | return vec!(); | |
154 | } | |
b039eaaf SL |
155 | error!("Mis-calculated spans for path '{}'. Found {} spans, expected {}. Found spans:", |
156 | path_to_string(path), | |
157 | spans.len(), | |
158 | path.segments.len()); | |
d9579d0f AL |
159 | for s in &spans { |
160 | let loc = self.sess.codemap().lookup_char_pos(s.lo); | |
161 | error!(" '{}' in {}, line {}", | |
b039eaaf SL |
162 | self.span.snippet(*s), |
163 | loc.file.name, | |
164 | loc.line); | |
d9579d0f AL |
165 | } |
166 | return vec!(); | |
167 | } | |
168 | ||
169 | let mut result: Vec<(Span, String)> = vec!(); | |
170 | ||
171 | let mut segs = vec!(); | |
62682a34 | 172 | for (i, (seg, span)) in path.segments.iter().zip(&spans).enumerate() { |
d9579d0f | 173 | segs.push(seg.clone()); |
e9174d1e SL |
174 | let sub_path = ast::Path { |
175 | span: *span, // span for the last segment | |
176 | global: path.global, | |
177 | segments: segs, | |
178 | }; | |
d9579d0f AL |
179 | let qualname = if i == 0 && path.global { |
180 | format!("::{}", path_to_string(&sub_path)) | |
181 | } else { | |
182 | path_to_string(&sub_path) | |
183 | }; | |
184 | result.push((*span, qualname)); | |
185 | segs = sub_path.segments; | |
186 | } | |
187 | ||
188 | result | |
189 | } | |
190 | ||
191 | // The global arg allows us to override the global-ness of the path (which | |
192 | // actually means 'does the path start with `::`', rather than 'is the path | |
193 | // semantically global). We use the override for `use` imports (etc.) where | |
194 | // the syntax is non-global, but the semantics are global. | |
195 | fn write_sub_paths(&mut self, path: &ast::Path, global: bool) { | |
196 | let sub_paths = self.process_path_prefixes(path); | |
197 | for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() { | |
198 | let qualname = if i == 0 && global && !path.global { | |
199 | format!("::{}", qualname) | |
200 | } else { | |
201 | qualname.clone() | |
202 | }; | |
54a0048b SL |
203 | self.dumper.mod_ref(path.span, ModRefData { |
204 | span: *span, | |
205 | qualname: qualname, | |
206 | scope: self.cur_scope, | |
207 | ref_id: None | |
208 | }.normalize(&self.tcx)); | |
d9579d0f AL |
209 | } |
210 | } | |
211 | ||
212 | // As write_sub_paths, but does not process the last ident in the path (assuming it | |
213 | // will be processed elsewhere). See note on write_sub_paths about global. | |
214 | fn write_sub_paths_truncated(&mut self, path: &ast::Path, global: bool) { | |
215 | let sub_paths = self.process_path_prefixes(path); | |
216 | let len = sub_paths.len(); | |
217 | if len <= 1 { | |
218 | return; | |
219 | } | |
220 | ||
221 | let sub_paths = &sub_paths[..len-1]; | |
222 | for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() { | |
223 | let qualname = if i == 0 && global && !path.global { | |
224 | format!("::{}", qualname) | |
225 | } else { | |
226 | qualname.clone() | |
227 | }; | |
54a0048b SL |
228 | self.dumper.mod_ref(path.span, ModRefData { |
229 | span: *span, | |
230 | qualname: qualname, | |
231 | scope: self.cur_scope, | |
232 | ref_id: None | |
233 | }.normalize(&self.tcx)); | |
d9579d0f AL |
234 | } |
235 | } | |
236 | ||
237 | // As write_sub_paths, but expects a path of the form module_path::trait::method | |
238 | // Where trait could actually be a struct too. | |
239 | fn write_sub_path_trait_truncated(&mut self, path: &ast::Path) { | |
240 | let sub_paths = self.process_path_prefixes(path); | |
241 | let len = sub_paths.len(); | |
242 | if len <= 1 { | |
243 | return; | |
244 | } | |
245 | let sub_paths = &sub_paths[.. (len-1)]; | |
246 | ||
247 | // write the trait part of the sub-path | |
248 | let (ref span, ref qualname) = sub_paths[len-2]; | |
54a0048b SL |
249 | self.dumper.type_ref(path.span, TypeRefData { |
250 | ref_id: None, | |
251 | span: *span, | |
252 | qualname: qualname.to_owned(), | |
253 | scope: 0 | |
254 | }); | |
d9579d0f AL |
255 | |
256 | // write the other sub-paths | |
257 | if len <= 2 { | |
258 | return; | |
259 | } | |
260 | let sub_paths = &sub_paths[..len-2]; | |
261 | for &(ref span, ref qualname) in sub_paths { | |
54a0048b SL |
262 | self.dumper.mod_ref(path.span, ModRefData { |
263 | span: *span, | |
264 | qualname: qualname.to_owned(), | |
265 | scope: self.cur_scope, | |
266 | ref_id: None | |
267 | }.normalize(&self.tcx)); | |
d9579d0f AL |
268 | } |
269 | } | |
270 | ||
271 | // looks up anything, not just a type | |
272 | fn lookup_type_ref(&self, ref_id: NodeId) -> Option<DefId> { | |
62682a34 | 273 | if !self.tcx.def_map.borrow().contains_key(&ref_id) { |
54a0048b | 274 | bug!("def_map has no key for {} in lookup_type_ref", ref_id); |
d9579d0f | 275 | } |
62682a34 | 276 | let def = self.tcx.def_map.borrow().get(&ref_id).unwrap().full_def(); |
d9579d0f | 277 | match def { |
7453a54e SL |
278 | Def::PrimTy(..) => None, |
279 | Def::SelfTy(..) => None, | |
d9579d0f AL |
280 | _ => Some(def.def_id()), |
281 | } | |
282 | } | |
283 | ||
54a0048b SL |
284 | fn process_def_kind(&mut self, |
285 | ref_id: NodeId, | |
286 | span: Span, | |
287 | sub_span: Option<Span>, | |
288 | def_id: DefId, | |
289 | scope: NodeId) { | |
290 | if self.span.filter_generated(sub_span, span) { | |
291 | return; | |
292 | } | |
293 | ||
62682a34 | 294 | let def_map = self.tcx.def_map.borrow(); |
d9579d0f | 295 | if !def_map.contains_key(&ref_id) { |
54a0048b SL |
296 | span_bug!(span, |
297 | "def_map has no key for {} in lookup_def_kind", | |
298 | ref_id); | |
d9579d0f AL |
299 | } |
300 | let def = def_map.get(&ref_id).unwrap().full_def(); | |
301 | match def { | |
7453a54e | 302 | Def::Mod(_) | |
54a0048b SL |
303 | Def::ForeignMod(_) => { |
304 | self.dumper.mod_ref(span, ModRefData { | |
305 | span: sub_span.expect("No span found for mod ref"), | |
306 | ref_id: Some(def_id), | |
307 | scope: scope, | |
308 | qualname: String::new() | |
309 | }.normalize(&self.tcx)); | |
310 | } | |
311 | Def::Struct(..) | | |
7453a54e SL |
312 | Def::Enum(..) | |
313 | Def::TyAlias(..) | | |
314 | Def::AssociatedTy(..) | | |
54a0048b SL |
315 | Def::Trait(_) => { |
316 | self.dumper.type_ref(span, TypeRefData { | |
317 | span: sub_span.expect("No span found for type ref"), | |
318 | ref_id: Some(def_id), | |
319 | scope: scope, | |
320 | qualname: String::new() | |
321 | }.normalize(&self.tcx)); | |
322 | } | |
7453a54e SL |
323 | Def::Static(_, _) | |
324 | Def::Const(_) | | |
325 | Def::AssociatedConst(..) | | |
326 | Def::Local(..) | | |
327 | Def::Variant(..) | | |
54a0048b SL |
328 | Def::Upvar(..) => { |
329 | self.dumper.variable_ref(span, VariableRefData { | |
330 | span: sub_span.expect("No span found for var ref"), | |
331 | ref_id: def_id, | |
332 | scope: scope, | |
333 | name: String::new() | |
334 | }.normalize(&self.tcx)); | |
335 | } | |
336 | Def::Fn(..) => { | |
337 | self.dumper.function_ref(span, FunctionRefData { | |
338 | span: sub_span.expect("No span found for fn ref"), | |
339 | ref_id: def_id, | |
340 | scope: scope | |
341 | }.normalize(&self.tcx)); | |
342 | } | |
7453a54e SL |
343 | Def::SelfTy(..) | |
344 | Def::Label(_) | | |
345 | Def::TyParam(..) | | |
346 | Def::Method(..) | | |
347 | Def::PrimTy(_) | | |
348 | Def::Err => { | |
54a0048b SL |
349 | span_bug!(span, |
350 | "process_def_kind for unexpected item: {:?}", | |
351 | def); | |
e9174d1e | 352 | } |
d9579d0f AL |
353 | } |
354 | } | |
355 | ||
356 | fn process_formals(&mut self, formals: &Vec<ast::Arg>, qualname: &str) { | |
357 | for arg in formals { | |
358 | self.visit_pat(&arg.pat); | |
359 | let mut collector = PathCollector::new(); | |
360 | collector.visit_pat(&arg.pat); | |
361 | let span_utils = self.span.clone(); | |
362 | for &(id, ref p, _, _) in &collector.collected_paths { | |
62682a34 | 363 | let typ = self.tcx.node_types().get(&id).unwrap().to_string(); |
d9579d0f AL |
364 | // get the span only for the name of the variable (I hope the path is only ever a |
365 | // variable name, but who knows?) | |
54a0048b SL |
366 | let sub_span = span_utils.span_for_last_ident(p.span); |
367 | if !self.span.filter_generated(sub_span, p.span) { | |
368 | self.dumper.variable(p.span, VariableData { | |
369 | id: id, | |
370 | span: sub_span.expect("No span found for variable"), | |
371 | name: path_to_string(p), | |
372 | qualname: format!("{}::{}", qualname, path_to_string(p)), | |
373 | type_value: typ, | |
374 | value: String::new(), | |
375 | scope: 0 | |
376 | }.normalize(&self.tcx)); | |
377 | } | |
d9579d0f AL |
378 | } |
379 | } | |
380 | } | |
381 | ||
c1a9b12d SL |
382 | fn process_method(&mut self, |
383 | sig: &ast::MethodSig, | |
d9579d0f | 384 | body: Option<&ast::Block>, |
c1a9b12d SL |
385 | id: ast::NodeId, |
386 | name: ast::Name, | |
d9579d0f | 387 | span: Span) { |
c1a9b12d | 388 | debug!("process_method: {}:{}", id, name); |
d9579d0f | 389 | |
7453a54e | 390 | if let Some(method_data) = self.save_ctxt.get_method_data(id, name, span) { |
d9579d0f | 391 | |
7453a54e | 392 | if body.is_some() { |
54a0048b SL |
393 | if !self.span.filter_generated(Some(method_data.span), span) { |
394 | self.dumper.function(span, method_data.clone().normalize(&self.tcx)); | |
395 | } | |
7453a54e SL |
396 | self.process_formals(&sig.decl.inputs, &method_data.qualname); |
397 | } else { | |
54a0048b SL |
398 | if !self.span.filter_generated(Some(method_data.span), span) { |
399 | self.dumper.method(span, MethodData { | |
400 | id: method_data.id, | |
401 | span: method_data.span, | |
402 | scope: method_data.scope, | |
403 | qualname: method_data.qualname.clone(), | |
404 | }.normalize(&self.tcx)); | |
405 | } | |
7453a54e SL |
406 | } |
407 | self.process_generic_params(&sig.generics, span, &method_data.qualname, id); | |
d9579d0f AL |
408 | } |
409 | ||
410 | // walk arg and return types | |
411 | for arg in &sig.decl.inputs { | |
412 | self.visit_ty(&arg.ty); | |
413 | } | |
414 | ||
7453a54e | 415 | if let ast::FunctionRetTy::Ty(ref ret_ty) = sig.decl.output { |
d9579d0f AL |
416 | self.visit_ty(ret_ty); |
417 | } | |
418 | ||
419 | // walk the fn body | |
420 | if let Some(body) = body { | |
421 | self.nest(id, |v| v.visit_block(body)); | |
422 | } | |
d9579d0f AL |
423 | } |
424 | ||
62682a34 SL |
425 | fn process_trait_ref(&mut self, trait_ref: &ast::TraitRef) { |
426 | let trait_ref_data = self.save_ctxt.get_trait_ref_data(trait_ref, self.cur_scope); | |
427 | if let Some(trait_ref_data) = trait_ref_data { | |
54a0048b SL |
428 | if !self.span.filter_generated(Some(trait_ref_data.span), trait_ref.path.span) { |
429 | self.dumper.type_ref(trait_ref.path.span, trait_ref_data.normalize(&self.tcx)); | |
430 | } | |
431 | ||
62682a34 | 432 | visit::walk_path(self, &trait_ref.path); |
d9579d0f AL |
433 | } |
434 | } | |
435 | ||
e9174d1e | 436 | fn process_struct_field_def(&mut self, field: &ast::StructField, parent_id: NodeId) { |
62682a34 | 437 | let field_data = self.save_ctxt.get_field_data(field, parent_id); |
54a0048b SL |
438 | if let Some(mut field_data) = field_data { |
439 | if !self.span.filter_generated(Some(field_data.span), field.span) { | |
440 | field_data.scope = normalize_node_id(&self.tcx, field_data.scope) as u32; | |
441 | field_data.value = String::new(); | |
442 | self.dumper.variable(field.span, field_data.normalize(&self.tcx)); | |
443 | } | |
d9579d0f AL |
444 | } |
445 | } | |
446 | ||
447 | // Dump generic params bindings, then visit_generics | |
448 | fn process_generic_params(&mut self, | |
e9174d1e | 449 | generics: &ast::Generics, |
d9579d0f AL |
450 | full_span: Span, |
451 | prefix: &str, | |
452 | id: NodeId) { | |
453 | // We can't only use visit_generics since we don't have spans for param | |
454 | // bindings, so we reparse the full_span to get those sub spans. | |
455 | // However full span is the entire enum/fn/struct block, so we only want | |
456 | // the first few to match the number of generics we're looking for. | |
457 | let param_sub_spans = self.span.spans_for_ty_params(full_span, | |
b039eaaf | 458 | (generics.ty_params.len() as isize)); |
62682a34 | 459 | for (param, param_ss) in generics.ty_params.iter().zip(param_sub_spans) { |
d9579d0f AL |
460 | // Append $id to name to make sure each one is unique |
461 | let name = format!("{}::{}${}", | |
462 | prefix, | |
62682a34 | 463 | escape(self.span.snippet(param_ss)), |
d9579d0f | 464 | id); |
54a0048b SL |
465 | if !self.span.filter_generated(Some(param_ss), full_span) { |
466 | self.dumper.typedef(full_span, TypedefData { | |
467 | span: param_ss, | |
468 | id: param.id, | |
469 | qualname: name, | |
470 | value: String::new() | |
471 | }.normalize(&self.tcx)); | |
472 | } | |
d9579d0f AL |
473 | } |
474 | self.visit_generics(generics); | |
475 | } | |
476 | ||
477 | fn process_fn(&mut self, | |
478 | item: &ast::Item, | |
479 | decl: &ast::FnDecl, | |
480 | ty_params: &ast::Generics, | |
481 | body: &ast::Block) { | |
7453a54e | 482 | if let Some(fn_data) = self.save_ctxt.get_item_data(item) { |
54a0048b SL |
483 | down_cast_data!(fn_data, FunctionData, item.span); |
484 | if !self.span.filter_generated(Some(fn_data.span), item.span) { | |
485 | self.dumper.function(item.span, fn_data.clone().normalize(&self.tcx)); | |
486 | } | |
7453a54e SL |
487 | |
488 | self.process_formals(&decl.inputs, &fn_data.qualname); | |
489 | self.process_generic_params(ty_params, item.span, &fn_data.qualname, item.id); | |
490 | } | |
d9579d0f AL |
491 | |
492 | for arg in &decl.inputs { | |
493 | self.visit_ty(&arg.ty); | |
494 | } | |
495 | ||
7453a54e | 496 | if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output { |
d9579d0f AL |
497 | self.visit_ty(&ret_ty); |
498 | } | |
499 | ||
500 | self.nest(item.id, |v| v.visit_block(&body)); | |
501 | } | |
502 | ||
e9174d1e | 503 | fn process_static_or_const_item(&mut self, item: &ast::Item, typ: &ast::Ty, expr: &ast::Expr) { |
7453a54e | 504 | if let Some(var_data) = self.save_ctxt.get_item_data(item) { |
54a0048b SL |
505 | down_cast_data!(var_data, VariableData, item.span); |
506 | if !self.span.filter_generated(Some(var_data.span), item.span) { | |
507 | let mut var_data = var_data; | |
508 | var_data.scope = normalize_node_id(&self.tcx, var_data.scope) as u32; | |
509 | self.dumper.variable(item.span, var_data.normalize(&self.tcx)); | |
510 | } | |
7453a54e | 511 | } |
d9579d0f AL |
512 | self.visit_ty(&typ); |
513 | self.visit_expr(expr); | |
514 | } | |
515 | ||
516 | fn process_const(&mut self, | |
517 | id: ast::NodeId, | |
b039eaaf | 518 | name: ast::Name, |
d9579d0f AL |
519 | span: Span, |
520 | typ: &ast::Ty, | |
e9174d1e | 521 | expr: &ast::Expr) { |
54a0048b | 522 | let qualname = format!("::{}", self.tcx.node_path_str(id)); |
d9579d0f | 523 | |
b039eaaf | 524 | let sub_span = self.span.sub_span_after_keyword(span, keywords::Const); |
d9579d0f | 525 | |
54a0048b SL |
526 | if !self.span.filter_generated(sub_span, span) { |
527 | self.dumper.variable(span, VariableData { | |
528 | span: sub_span.expect("No span found for variable"), | |
529 | id: id, | |
530 | name: name.to_string(), | |
531 | qualname: qualname, | |
532 | value: self.span.snippet(expr.span), | |
533 | type_value: ty_to_string(&typ), | |
534 | scope: normalize_node_id(&self.tcx, self.cur_scope) as u32 | |
535 | }.normalize(&self.tcx)); | |
536 | } | |
d9579d0f AL |
537 | |
538 | // walk type and init value | |
539 | self.visit_ty(typ); | |
540 | self.visit_expr(expr); | |
541 | } | |
542 | ||
543 | fn process_struct(&mut self, | |
544 | item: &ast::Item, | |
b039eaaf | 545 | def: &ast::VariantData, |
d9579d0f | 546 | ty_params: &ast::Generics) { |
54a0048b | 547 | let qualname = format!("::{}", self.tcx.node_path_str(item.id)); |
d9579d0f | 548 | |
d9579d0f AL |
549 | let val = self.span.snippet(item.span); |
550 | let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Struct); | |
54a0048b SL |
551 | if !self.span.filter_generated(sub_span, item.span) { |
552 | self.dumper.struct_data(item.span, StructData { | |
553 | span: sub_span.expect("No span found for struct"), | |
554 | id: item.id, | |
555 | ctor_id: def.id(), | |
556 | qualname: qualname.clone(), | |
557 | scope: self.cur_scope, | |
558 | value: val | |
559 | }.normalize(&self.tcx)); | |
560 | } | |
561 | ||
d9579d0f AL |
562 | |
563 | // fields | |
b039eaaf | 564 | for field in def.fields() { |
62682a34 | 565 | self.process_struct_field_def(field, item.id); |
54a0048b | 566 | self.visit_ty(&field.ty); |
d9579d0f AL |
567 | } |
568 | ||
62682a34 | 569 | self.process_generic_params(ty_params, item.span, &qualname, item.id); |
d9579d0f AL |
570 | } |
571 | ||
572 | fn process_enum(&mut self, | |
573 | item: &ast::Item, | |
574 | enum_definition: &ast::EnumDef, | |
575 | ty_params: &ast::Generics) { | |
62682a34 | 576 | let enum_data = self.save_ctxt.get_item_data(item); |
7453a54e SL |
577 | let enum_data = match enum_data { |
578 | None => return, | |
579 | Some(data) => data, | |
580 | }; | |
54a0048b SL |
581 | down_cast_data!(enum_data, EnumData, item.span); |
582 | let normalized = enum_data.clone().normalize(&self.tcx); | |
583 | if !self.span.filter_generated(Some(normalized.span), item.span) { | |
584 | self.dumper.enum_data(item.span, normalized); | |
585 | } | |
62682a34 | 586 | |
d9579d0f | 587 | for variant in &enum_definition.variants { |
c1a9b12d | 588 | let name = &variant.node.name.name.as_str(); |
62682a34 | 589 | let mut qualname = enum_data.qualname.clone(); |
d9579d0f AL |
590 | qualname.push_str("::"); |
591 | qualname.push_str(name); | |
592 | let val = self.span.snippet(variant.span); | |
b039eaaf | 593 | |
7453a54e SL |
594 | match variant.node.data { |
595 | ast::VariantData::Struct(..) => { | |
54a0048b SL |
596 | let sub_span = self.span.span_for_first_ident(variant.span); |
597 | if !self.span.filter_generated(sub_span, variant.span) { | |
598 | self.dumper.struct_variant(variant.span, StructVariantData { | |
599 | span: sub_span.expect("No span found for struct variant"), | |
600 | id: variant.node.data.id(), | |
601 | qualname: qualname, | |
602 | type_value: enum_data.qualname.clone(), | |
603 | value: val, | |
604 | scope: enum_data.scope | |
605 | }.normalize(&self.tcx)); | |
606 | } | |
7453a54e SL |
607 | } |
608 | _ => { | |
54a0048b SL |
609 | let sub_span = self.span.span_for_first_ident(variant.span); |
610 | if !self.span.filter_generated(sub_span, variant.span) { | |
611 | self.dumper.tuple_variant(variant.span, TupleVariantData { | |
612 | span: sub_span.expect("No span found for tuple variant"), | |
613 | id: variant.node.data.id(), | |
614 | name: name.to_string(), | |
615 | qualname: qualname, | |
616 | type_value: enum_data.qualname.clone(), | |
617 | value: val, | |
618 | scope: enum_data.scope | |
619 | }.normalize(&self.tcx)); | |
620 | } | |
7453a54e SL |
621 | } |
622 | } | |
623 | ||
b039eaaf SL |
624 | |
625 | for field in variant.node.data.fields() { | |
626 | self.process_struct_field_def(field, variant.node.data.id()); | |
54a0048b | 627 | self.visit_ty(&field.ty); |
d9579d0f AL |
628 | } |
629 | } | |
62682a34 | 630 | self.process_generic_params(ty_params, item.span, &enum_data.qualname, enum_data.id); |
d9579d0f AL |
631 | } |
632 | ||
633 | fn process_impl(&mut self, | |
634 | item: &ast::Item, | |
635 | type_parameters: &ast::Generics, | |
636 | trait_ref: &Option<ast::TraitRef>, | |
637 | typ: &ast::Ty, | |
7453a54e SL |
638 | impl_items: &[ast::ImplItem]) { |
639 | let mut has_self_ref = false; | |
640 | if let Some(impl_data) = self.save_ctxt.get_item_data(item) { | |
54a0048b | 641 | down_cast_data!(impl_data, ImplData, item.span); |
7453a54e SL |
642 | if let Some(ref self_ref) = impl_data.self_ref { |
643 | has_self_ref = true; | |
54a0048b SL |
644 | if !self.span.filter_generated(Some(self_ref.span), item.span) { |
645 | self.dumper.type_ref(item.span, self_ref.clone().normalize(&self.tcx)); | |
646 | } | |
62682a34 | 647 | } |
7453a54e | 648 | if let Some(ref trait_ref_data) = impl_data.trait_ref { |
54a0048b SL |
649 | if !self.span.filter_generated(Some(trait_ref_data.span), item.span) { |
650 | self.dumper.type_ref(item.span, trait_ref_data.clone().normalize(&self.tcx)); | |
651 | } | |
652 | ||
7453a54e | 653 | visit::walk_path(self, &trait_ref.as_ref().unwrap().path); |
d9579d0f | 654 | } |
7453a54e | 655 | |
54a0048b SL |
656 | if !self.span.filter_generated(Some(impl_data.span), item.span) { |
657 | self.dumper.impl_data(item.span, ImplData { | |
658 | id: impl_data.id, | |
659 | span: impl_data.span, | |
660 | scope: impl_data.scope, | |
661 | trait_ref: impl_data.trait_ref.map(|d| d.ref_id.unwrap()), | |
662 | self_ref: impl_data.self_ref.map(|d| d.ref_id.unwrap()) | |
663 | }.normalize(&self.tcx)); | |
664 | } | |
d9579d0f | 665 | } |
7453a54e SL |
666 | if !has_self_ref { |
667 | self.visit_ty(&typ); | |
d9579d0f | 668 | } |
d9579d0f AL |
669 | self.process_generic_params(type_parameters, item.span, "", item.id); |
670 | for impl_item in impl_items { | |
671 | self.visit_impl_item(impl_item); | |
672 | } | |
673 | } | |
674 | ||
675 | fn process_trait(&mut self, | |
676 | item: &ast::Item, | |
677 | generics: &ast::Generics, | |
9cc50fc6 | 678 | trait_refs: &ast::TyParamBounds, |
7453a54e | 679 | methods: &[ast::TraitItem]) { |
54a0048b | 680 | let qualname = format!("::{}", self.tcx.node_path_str(item.id)); |
d9579d0f AL |
681 | let val = self.span.snippet(item.span); |
682 | let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Trait); | |
54a0048b SL |
683 | if !self.span.filter_generated(sub_span, item.span) { |
684 | self.dumper.trait_data(item.span, TraitData { | |
685 | span: sub_span.expect("No span found for trait"), | |
686 | id: item.id, | |
687 | qualname: qualname.clone(), | |
688 | scope: self.cur_scope, | |
689 | value: val | |
690 | }.normalize(&self.tcx)); | |
691 | } | |
d9579d0f AL |
692 | |
693 | // super-traits | |
62682a34 | 694 | for super_bound in trait_refs.iter() { |
d9579d0f AL |
695 | let trait_ref = match *super_bound { |
696 | ast::TraitTyParamBound(ref trait_ref, _) => { | |
697 | trait_ref | |
698 | } | |
699 | ast::RegionTyParamBound(..) => { | |
700 | continue; | |
701 | } | |
702 | }; | |
703 | ||
704 | let trait_ref = &trait_ref.trait_ref; | |
54a0048b SL |
705 | if let Some(id) = self.lookup_type_ref(trait_ref.ref_id) { |
706 | let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span); | |
707 | if !self.span.filter_generated(sub_span, trait_ref.path.span) { | |
708 | self.dumper.type_ref(trait_ref.path.span, TypeRefData { | |
709 | span: sub_span.expect("No span found for trait ref"), | |
710 | ref_id: Some(id), | |
711 | scope: self.cur_scope, | |
712 | qualname: String::new() | |
713 | }.normalize(&self.tcx)); | |
714 | } | |
715 | ||
716 | if !self.span.filter_generated(sub_span, trait_ref.path.span) { | |
717 | let sub_span = sub_span.expect("No span for inheritance"); | |
718 | self.dumper.inheritance(InheritanceData { | |
719 | span: sub_span, | |
720 | base_id: id, | |
721 | deriv_id: item.id | |
722 | }.normalize(&self.tcx)); | |
e9174d1e | 723 | } |
d9579d0f AL |
724 | } |
725 | } | |
726 | ||
727 | // walk generics and methods | |
62682a34 | 728 | self.process_generic_params(generics, item.span, &qualname, item.id); |
d9579d0f AL |
729 | for method in methods { |
730 | self.visit_trait_item(method) | |
731 | } | |
732 | } | |
733 | ||
e9174d1e SL |
734 | // `item` is the module in question, represented as an item. |
735 | fn process_mod(&mut self, item: &ast::Item) { | |
7453a54e | 736 | if let Some(mod_data) = self.save_ctxt.get_item_data(item) { |
54a0048b SL |
737 | down_cast_data!(mod_data, ModData, item.span); |
738 | if !self.span.filter_generated(Some(mod_data.span), item.span) { | |
739 | self.dumper.mod_data(mod_data.normalize(&self.tcx)); | |
740 | } | |
7453a54e | 741 | } |
d9579d0f AL |
742 | } |
743 | ||
e9174d1e | 744 | fn process_path(&mut self, id: NodeId, path: &ast::Path, ref_kind: Option<recorder::Row>) { |
7453a54e SL |
745 | let path_data = self.save_ctxt.get_path_data(id, path); |
746 | if generated_code(path.span) && path_data.is_none() { | |
c1a9b12d | 747 | return; |
d9579d0f AL |
748 | } |
749 | ||
c1a9b12d SL |
750 | let path_data = match path_data { |
751 | Some(pd) => pd, | |
752 | None => { | |
54a0048b SL |
753 | span_bug!(path.span, |
754 | "Unexpected def kind while looking up path in `{}`", | |
755 | self.span.snippet(path.span)) | |
c1a9b12d SL |
756 | } |
757 | }; | |
54a0048b | 758 | |
c1a9b12d | 759 | match path_data { |
54a0048b SL |
760 | Data::VariableRefData(vrd) => { |
761 | // FIXME: this whole block duplicates the code in process_def_kind | |
762 | if !self.span.filter_generated(Some(vrd.span), path.span) { | |
763 | match ref_kind { | |
764 | Some(recorder::TypeRef) => { | |
765 | self.dumper.type_ref(path.span, TypeRefData { | |
766 | span: vrd.span, | |
767 | ref_id: Some(vrd.ref_id), | |
768 | scope: vrd.scope, | |
769 | qualname: String::new() | |
770 | }.normalize(&self.tcx)); | |
771 | } | |
772 | Some(recorder::FnRef) => { | |
773 | self.dumper.function_ref(path.span, FunctionRefData { | |
774 | span: vrd.span, | |
775 | ref_id: vrd.ref_id, | |
776 | scope: vrd.scope | |
777 | }.normalize(&self.tcx)); | |
778 | } | |
779 | Some(recorder::ModRef) => { | |
780 | self.dumper.mod_ref(path.span, ModRefData { | |
781 | span: vrd.span, | |
782 | ref_id: Some(vrd.ref_id), | |
783 | scope: vrd.scope, | |
784 | qualname: String::new() | |
785 | }.normalize(&self.tcx)); | |
786 | } | |
787 | Some(recorder::VarRef) | None | |
788 | => self.dumper.variable_ref(path.span, vrd.normalize(&self.tcx)) | |
789 | } | |
790 | } | |
c1a9b12d SL |
791 | |
792 | } | |
54a0048b SL |
793 | Data::TypeRefData(trd) => { |
794 | if !self.span.filter_generated(Some(trd.span), path.span) { | |
795 | self.dumper.type_ref(path.span, trd.normalize(&self.tcx)); | |
796 | } | |
c1a9b12d | 797 | } |
54a0048b SL |
798 | Data::MethodCallData(mcd) => { |
799 | if !self.span.filter_generated(Some(mcd.span), path.span) { | |
800 | self.dumper.method_call(path.span, mcd.normalize(&self.tcx)); | |
801 | } | |
c1a9b12d SL |
802 | } |
803 | Data::FunctionCallData(fcd) => { | |
54a0048b SL |
804 | if !self.span.filter_generated(Some(fcd.span), path.span) { |
805 | self.dumper.function_call(path.span, fcd.normalize(&self.tcx)); | |
806 | } | |
c1a9b12d SL |
807 | } |
808 | _ => { | |
54a0048b | 809 | span_bug!(path.span, "Unexpected data: {:?}", path_data); |
d9579d0f | 810 | } |
d9579d0f | 811 | } |
c1a9b12d SL |
812 | |
813 | // Modules or types in the path prefix. | |
814 | let def_map = self.tcx.def_map.borrow(); | |
815 | let def = def_map.get(&id).unwrap().full_def(); | |
d9579d0f | 816 | match def { |
7453a54e | 817 | Def::Method(did) => { |
c1a9b12d | 818 | let ti = self.tcx.impl_or_trait_item(did); |
d9579d0f | 819 | if let ty::MethodTraitItem(m) = ti { |
9cc50fc6 | 820 | if m.explicit_self == ty::ExplicitSelfCategory::Static { |
d9579d0f AL |
821 | self.write_sub_path_trait_truncated(path); |
822 | } | |
823 | } | |
824 | } | |
7453a54e SL |
825 | Def::Local(..) | |
826 | Def::Static(_,_) | | |
827 | Def::Const(..) | | |
828 | Def::AssociatedConst(..) | | |
829 | Def::Struct(..) | | |
830 | Def::Variant(..) | | |
831 | Def::Fn(..) => self.write_sub_paths_truncated(path, false), | |
e9174d1e | 832 | _ => {} |
d9579d0f AL |
833 | } |
834 | } | |
835 | ||
836 | fn process_struct_lit(&mut self, | |
837 | ex: &ast::Expr, | |
838 | path: &ast::Path, | |
839 | fields: &Vec<ast::Field>, | |
e9174d1e | 840 | variant: ty::VariantDef, |
d9579d0f | 841 | base: &Option<P<ast::Expr>>) { |
d9579d0f AL |
842 | self.write_sub_paths_truncated(path, false); |
843 | ||
62682a34 | 844 | if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) { |
54a0048b SL |
845 | down_cast_data!(struct_lit_data, TypeRefData, ex.span); |
846 | if !self.span.filter_generated(Some(struct_lit_data.span), ex.span) { | |
847 | self.dumper.type_ref(ex.span, struct_lit_data.normalize(&self.tcx)); | |
848 | } | |
849 | ||
c1a9b12d | 850 | let scope = self.save_ctxt.enclosing_scope(ex.id); |
62682a34 SL |
851 | |
852 | for field in fields { | |
7453a54e SL |
853 | if let Some(field_data) = self.save_ctxt |
854 | .get_field_ref_data(field, variant, scope) { | |
d9579d0f | 855 | |
54a0048b SL |
856 | if !self.span.filter_generated(Some(field_data.span), field.ident.span) { |
857 | self.dumper.variable_ref(field.ident.span, field_data.normalize(&self.tcx)); | |
858 | } | |
7453a54e | 859 | } |
62682a34 SL |
860 | |
861 | self.visit_expr(&field.expr) | |
862 | } | |
d9579d0f | 863 | } |
62682a34 | 864 | |
b039eaaf | 865 | walk_list!(self, visit_expr, base); |
d9579d0f AL |
866 | } |
867 | ||
e9174d1e | 868 | fn process_method_call(&mut self, ex: &ast::Expr, args: &Vec<P<ast::Expr>>) { |
54a0048b SL |
869 | if let Some(mcd) = self.save_ctxt.get_expr_data(ex) { |
870 | down_cast_data!(mcd, MethodCallData, ex.span); | |
871 | if !self.span.filter_generated(Some(mcd.span), ex.span) { | |
872 | self.dumper.method_call(ex.span, mcd.normalize(&self.tcx)); | |
873 | } | |
c1a9b12d | 874 | } |
d9579d0f AL |
875 | |
876 | // walk receiver and args | |
b039eaaf | 877 | walk_list!(self, visit_expr, args); |
d9579d0f AL |
878 | } |
879 | ||
e9174d1e | 880 | fn process_pat(&mut self, p: &ast::Pat) { |
d9579d0f | 881 | match p.node { |
7453a54e | 882 | PatKind::Struct(ref path, ref fields, _) => { |
d9579d0f | 883 | visit::walk_path(self, path); |
e9174d1e SL |
884 | let adt = self.tcx.node_id_to_type(p.id).ty_adt_def().unwrap(); |
885 | let def = self.tcx.def_map.borrow()[&p.id].full_def(); | |
886 | let variant = adt.variant_of_def(def); | |
d9579d0f | 887 | |
e9174d1e | 888 | for &Spanned { node: ref field, span } in fields { |
e9174d1e SL |
889 | let sub_span = self.span.span_for_first_ident(span); |
890 | if let Some(f) = variant.find_field_named(field.ident.name) { | |
54a0048b SL |
891 | if !self.span.filter_generated(sub_span, span) { |
892 | self.dumper.variable_ref(span, VariableRefData { | |
893 | span: sub_span.expect("No span fund for var ref"), | |
894 | ref_id: f.did, | |
895 | scope: self.cur_scope, | |
896 | name: String::new() | |
897 | }.normalize(&self.tcx)); | |
898 | } | |
d9579d0f | 899 | } |
e9174d1e | 900 | self.visit_pat(&field.pat); |
d9579d0f AL |
901 | } |
902 | } | |
e9174d1e | 903 | _ => visit::walk_pat(self, p), |
d9579d0f AL |
904 | } |
905 | } | |
b039eaaf SL |
906 | |
907 | ||
908 | fn process_var_decl(&mut self, p: &ast::Pat, value: String) { | |
909 | // The local could declare multiple new vars, we must walk the | |
910 | // pattern and collect them all. | |
911 | let mut collector = PathCollector::new(); | |
912 | collector.visit_pat(&p); | |
913 | self.visit_pat(&p); | |
914 | ||
915 | for &(id, ref p, immut, _) in &collector.collected_paths { | |
7453a54e | 916 | let value = if immut == ast::Mutability::Immutable { |
b039eaaf SL |
917 | value.to_string() |
918 | } else { | |
919 | "<mutable>".to_string() | |
920 | }; | |
921 | let types = self.tcx.node_types(); | |
7453a54e | 922 | let typ = types.get(&id).map(|t| t.to_string()).unwrap_or(String::new()); |
b039eaaf SL |
923 | // Get the span only for the name of the variable (I hope the path |
924 | // is only ever a variable name, but who knows?). | |
925 | let sub_span = self.span.span_for_last_ident(p.span); | |
926 | // Rust uses the id of the pattern for var lookups, so we'll use it too. | |
54a0048b SL |
927 | if !self.span.filter_generated(sub_span, p.span) { |
928 | self.dumper.variable(p.span, VariableData { | |
929 | span: sub_span.expect("No span found for variable"), | |
930 | id: id, | |
931 | name: path_to_string(p), | |
932 | qualname: format!("{}${}", path_to_string(p), id), | |
933 | value: value, | |
934 | type_value: typ, | |
935 | scope: 0 | |
936 | }.normalize(&self.tcx)); | |
937 | } | |
b039eaaf SL |
938 | } |
939 | } | |
7453a54e SL |
940 | |
941 | /// Extract macro use and definition information from the AST node defined | |
942 | /// by the given NodeId, using the expansion information from the node's | |
943 | /// span. | |
944 | /// | |
945 | /// If the span is not macro-generated, do nothing, else use callee and | |
946 | /// callsite spans to record macro definition and use data, using the | |
947 | /// mac_uses and mac_defs sets to prevent multiples. | |
948 | fn process_macro_use(&mut self, span: Span, id: NodeId) { | |
949 | let data = match self.save_ctxt.get_macro_use_data(span, id) { | |
950 | None => return, | |
951 | Some(data) => data, | |
952 | }; | |
953 | let mut hasher = SipHasher::new(); | |
954 | data.callee_span.hash(&mut hasher); | |
955 | let hash = hasher.finish(); | |
956 | let qualname = format!("{}::{}", data.name, hash); | |
957 | // Don't write macro definition for imported macros | |
958 | if !self.mac_defs.contains(&data.callee_span) | |
959 | && !data.imported { | |
960 | self.mac_defs.insert(data.callee_span); | |
961 | if let Some(sub_span) = self.span.span_for_macro_def_name(data.callee_span) { | |
54a0048b SL |
962 | self.dumper.macro_data(data.callee_span, MacroData { |
963 | span: sub_span, | |
964 | name: data.name.clone(), | |
965 | qualname: qualname.clone() | |
966 | }); | |
7453a54e SL |
967 | } |
968 | } | |
969 | if !self.mac_uses.contains(&data.span) { | |
970 | self.mac_uses.insert(data.span); | |
971 | if let Some(sub_span) = self.span.span_for_macro_use_name(data.span) { | |
54a0048b SL |
972 | self.dumper.macro_use(data.span, MacroUseData { |
973 | span: sub_span, | |
974 | name: data.name, | |
975 | qualname: qualname, | |
976 | scope: data.scope, | |
977 | callee_span: data.callee_span, | |
978 | imported: data.imported | |
979 | }.normalize(&self.tcx)); | |
7453a54e SL |
980 | } |
981 | } | |
982 | } | |
d9579d0f AL |
983 | } |
984 | ||
54a0048b | 985 | impl<'l, 'tcx, 'v, D: Dump + 'l> Visitor<'v> for DumpVisitor<'l, 'tcx, D> { |
d9579d0f | 986 | fn visit_item(&mut self, item: &ast::Item) { |
7453a54e SL |
987 | use syntax::ast::ItemKind::*; |
988 | self.process_macro_use(item.span, item.id); | |
d9579d0f | 989 | match item.node { |
7453a54e | 990 | Use(ref use_item) => { |
d9579d0f AL |
991 | match use_item.node { |
992 | ast::ViewPathSimple(ident, ref path) => { | |
993 | let sub_span = self.span.span_for_last_ident(path.span); | |
994 | let mod_id = match self.lookup_type_ref(item.id) { | |
995 | Some(def_id) => { | |
54a0048b SL |
996 | let scope = self.cur_scope; |
997 | self.process_def_kind(item.id, path.span, sub_span, def_id, scope); | |
998 | ||
d9579d0f | 999 | Some(def_id) |
e9174d1e | 1000 | } |
d9579d0f AL |
1001 | None => None, |
1002 | }; | |
1003 | ||
1004 | // 'use' always introduces an alias, if there is not an explicit | |
1005 | // one, there is an implicit one. | |
b039eaaf SL |
1006 | let sub_span = match self.span.sub_span_after_keyword(use_item.span, |
1007 | keywords::As) { | |
1008 | Some(sub_span) => Some(sub_span), | |
1009 | None => sub_span, | |
1010 | }; | |
d9579d0f | 1011 | |
54a0048b SL |
1012 | if !self.span.filter_generated(sub_span, path.span) { |
1013 | self.dumper.use_data(path.span, UseData { | |
1014 | span: sub_span.expect("No span found for use"), | |
1015 | id: item.id, | |
1016 | mod_id: mod_id, | |
1017 | name: ident.name.to_string(), | |
1018 | scope: self.cur_scope | |
1019 | }.normalize(&self.tcx)); | |
1020 | } | |
d9579d0f AL |
1021 | self.write_sub_paths_truncated(path, true); |
1022 | } | |
1023 | ast::ViewPathGlob(ref path) => { | |
1024 | // Make a comma-separated list of names of imported modules. | |
54a0048b | 1025 | let mut names = vec![]; |
d9579d0f AL |
1026 | let glob_map = &self.analysis.glob_map; |
1027 | let glob_map = glob_map.as_ref().unwrap(); | |
1028 | if glob_map.contains_key(&item.id) { | |
1029 | for n in glob_map.get(&item.id).unwrap() { | |
54a0048b | 1030 | names.push(n.to_string()); |
d9579d0f AL |
1031 | } |
1032 | } | |
1033 | ||
b039eaaf SL |
1034 | let sub_span = self.span |
1035 | .sub_span_of_token(path.span, token::BinOp(token::Star)); | |
54a0048b SL |
1036 | if !self.span.filter_generated(sub_span, path.span) { |
1037 | self.dumper.use_glob(path.span, UseGlobData { | |
1038 | span: sub_span.expect("No span found for use glob"), | |
1039 | id: item.id, | |
1040 | names: names, | |
1041 | scope: self.cur_scope | |
1042 | }.normalize(&self.tcx)); | |
1043 | } | |
d9579d0f AL |
1044 | self.write_sub_paths(path, true); |
1045 | } | |
1046 | ast::ViewPathList(ref path, ref list) => { | |
1047 | for plid in list { | |
1048 | match plid.node { | |
7453a54e | 1049 | ast::PathListItemKind::Ident { id, .. } => { |
54a0048b SL |
1050 | let scope = self.cur_scope; |
1051 | if let Some(def_id) = self.lookup_type_ref(id) { | |
1052 | self.process_def_kind(id, | |
1053 | plid.span, | |
1054 | Some(plid.span), | |
1055 | def_id, | |
1056 | scope); | |
d9579d0f | 1057 | } |
e9174d1e | 1058 | } |
7453a54e | 1059 | ast::PathListItemKind::Mod { .. } => (), |
d9579d0f AL |
1060 | } |
1061 | } | |
1062 | ||
1063 | self.write_sub_paths(path, true); | |
1064 | } | |
1065 | } | |
1066 | } | |
7453a54e | 1067 | ExternCrate(ref s) => { |
d9579d0f AL |
1068 | let location = match *s { |
1069 | Some(s) => s.to_string(), | |
c1a9b12d | 1070 | None => item.ident.to_string(), |
d9579d0f AL |
1071 | }; |
1072 | let alias_span = self.span.span_for_last_ident(item.span); | |
92a42be0 | 1073 | let cnum = match self.sess.cstore.extern_mod_stmt_cnum(item.id) { |
d9579d0f AL |
1074 | Some(cnum) => cnum, |
1075 | None => 0, | |
1076 | }; | |
54a0048b SL |
1077 | |
1078 | if !self.span.filter_generated(alias_span, item.span) { | |
1079 | self.dumper.extern_crate(item.span, ExternCrateData { | |
1080 | id: item.id, | |
1081 | name: item.ident.name.to_string(), | |
1082 | crate_num: cnum, | |
1083 | location: location, | |
1084 | span: alias_span.expect("No span found for extern crate"), | |
1085 | scope: self.cur_scope, | |
1086 | }.normalize(&self.tcx)); | |
1087 | } | |
d9579d0f | 1088 | } |
7453a54e SL |
1089 | Fn(ref decl, _, _, _, ref ty_params, ref body) => |
1090 | self.process_fn(item, &decl, ty_params, &body), | |
1091 | Static(ref typ, _, ref expr) => | |
d9579d0f | 1092 | self.process_static_or_const_item(item, typ, expr), |
7453a54e | 1093 | Const(ref typ, ref expr) => |
d9579d0f | 1094 | self.process_static_or_const_item(item, &typ, &expr), |
7453a54e SL |
1095 | Struct(ref def, ref ty_params) => self.process_struct(item, def, ty_params), |
1096 | Enum(ref def, ref ty_params) => self.process_enum(item, def, ty_params), | |
1097 | Impl(_, _, | |
d9579d0f AL |
1098 | ref ty_params, |
1099 | ref trait_ref, | |
1100 | ref typ, | |
1101 | ref impl_items) => { | |
b039eaaf | 1102 | self.process_impl(item, ty_params, trait_ref, &typ, impl_items) |
d9579d0f | 1103 | } |
7453a54e | 1104 | Trait(_, ref generics, ref trait_refs, ref methods) => |
d9579d0f | 1105 | self.process_trait(item, generics, trait_refs, methods), |
7453a54e | 1106 | Mod(ref m) => { |
62682a34 SL |
1107 | self.process_mod(item); |
1108 | self.nest(item.id, |v| visit::walk_mod(v, m)); | |
1109 | } | |
7453a54e | 1110 | Ty(ref ty, ref ty_params) => { |
54a0048b | 1111 | let qualname = format!("::{}", self.tcx.node_path_str(item.id)); |
7453a54e | 1112 | let value = ty_to_string(&ty); |
d9579d0f | 1113 | let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Type); |
54a0048b SL |
1114 | if !self.span.filter_generated(sub_span, item.span) { |
1115 | self.dumper.typedef(item.span, TypedefData { | |
1116 | span: sub_span.expect("No span found for typedef"), | |
1117 | id: item.id, | |
1118 | qualname: qualname.clone(), | |
1119 | value: value | |
1120 | }.normalize(&self.tcx)); | |
1121 | } | |
d9579d0f | 1122 | |
7453a54e | 1123 | self.visit_ty(&ty); |
d9579d0f | 1124 | self.process_generic_params(ty_params, item.span, &qualname, item.id); |
e9174d1e | 1125 | } |
7453a54e | 1126 | Mac(_) => (), |
d9579d0f AL |
1127 | _ => visit::walk_item(self, item), |
1128 | } | |
1129 | } | |
1130 | ||
1131 | fn visit_generics(&mut self, generics: &ast::Generics) { | |
62682a34 SL |
1132 | for param in generics.ty_params.iter() { |
1133 | for bound in param.bounds.iter() { | |
d9579d0f AL |
1134 | if let ast::TraitTyParamBound(ref trait_ref, _) = *bound { |
1135 | self.process_trait_ref(&trait_ref.trait_ref); | |
1136 | } | |
1137 | } | |
1138 | if let Some(ref ty) = param.default { | |
7453a54e | 1139 | self.visit_ty(&ty); |
d9579d0f AL |
1140 | } |
1141 | } | |
1142 | } | |
1143 | ||
1144 | fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) { | |
7453a54e | 1145 | self.process_macro_use(trait_item.span, trait_item.id); |
d9579d0f | 1146 | match trait_item.node { |
7453a54e | 1147 | ast::TraitItemKind::Const(ref ty, Some(ref expr)) => { |
b039eaaf SL |
1148 | self.process_const(trait_item.id, |
1149 | trait_item.ident.name, | |
1150 | trait_item.span, | |
7453a54e SL |
1151 | &ty, |
1152 | &expr); | |
d9579d0f | 1153 | } |
7453a54e | 1154 | ast::TraitItemKind::Method(ref sig, ref body) => { |
c1a9b12d SL |
1155 | self.process_method(sig, |
1156 | body.as_ref().map(|x| &**x), | |
1157 | trait_item.id, | |
1158 | trait_item.ident.name, | |
1159 | trait_item.span); | |
d9579d0f | 1160 | } |
7453a54e SL |
1161 | ast::TraitItemKind::Const(_, None) | |
1162 | ast::TraitItemKind::Type(..) => {} | |
d9579d0f AL |
1163 | } |
1164 | } | |
1165 | ||
1166 | fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) { | |
7453a54e | 1167 | self.process_macro_use(impl_item.span, impl_item.id); |
d9579d0f | 1168 | match impl_item.node { |
92a42be0 | 1169 | ast::ImplItemKind::Const(ref ty, ref expr) => { |
b039eaaf SL |
1170 | self.process_const(impl_item.id, |
1171 | impl_item.ident.name, | |
1172 | impl_item.span, | |
1173 | &ty, | |
1174 | &expr); | |
d9579d0f | 1175 | } |
92a42be0 | 1176 | ast::ImplItemKind::Method(ref sig, ref body) => { |
c1a9b12d SL |
1177 | self.process_method(sig, |
1178 | Some(body), | |
1179 | impl_item.id, | |
1180 | impl_item.ident.name, | |
1181 | impl_item.span); | |
d9579d0f | 1182 | } |
92a42be0 SL |
1183 | ast::ImplItemKind::Type(_) | |
1184 | ast::ImplItemKind::Macro(_) => {} | |
d9579d0f AL |
1185 | } |
1186 | } | |
1187 | ||
1188 | fn visit_ty(&mut self, t: &ast::Ty) { | |
7453a54e | 1189 | self.process_macro_use(t.span, t.id); |
d9579d0f | 1190 | match t.node { |
7453a54e | 1191 | ast::TyKind::Path(_, ref path) => { |
54a0048b SL |
1192 | if let Some(id) = self.lookup_type_ref(t.id) { |
1193 | let sub_span = self.span.sub_span_for_type_name(t.span); | |
1194 | if !self.span.filter_generated(sub_span, t.span) { | |
1195 | self.dumper.type_ref(t.span, TypeRefData { | |
1196 | span: sub_span.expect("No span found for type ref"), | |
1197 | ref_id: Some(id), | |
1198 | scope: self.cur_scope, | |
1199 | qualname: String::new() | |
1200 | }.normalize(&self.tcx)); | |
e9174d1e | 1201 | } |
d9579d0f AL |
1202 | } |
1203 | ||
1204 | self.write_sub_paths_truncated(path, false); | |
1205 | ||
1206 | visit::walk_path(self, path); | |
e9174d1e | 1207 | } |
d9579d0f AL |
1208 | _ => visit::walk_ty(self, t), |
1209 | } | |
1210 | } | |
1211 | ||
1212 | fn visit_expr(&mut self, ex: &ast::Expr) { | |
7453a54e | 1213 | self.process_macro_use(ex.span, ex.id); |
d9579d0f | 1214 | match ex.node { |
7453a54e | 1215 | ast::ExprKind::Call(ref _f, ref _args) => { |
d9579d0f AL |
1216 | // Don't need to do anything for function calls, |
1217 | // because just walking the callee path does what we want. | |
1218 | visit::walk_expr(self, ex); | |
1219 | } | |
7453a54e | 1220 | ast::ExprKind::Path(_, ref path) => { |
c1a9b12d | 1221 | self.process_path(ex.id, path, None); |
d9579d0f AL |
1222 | visit::walk_expr(self, ex); |
1223 | } | |
7453a54e | 1224 | ast::ExprKind::Struct(ref path, ref fields, ref base) => { |
b039eaaf | 1225 | let hir_expr = lower_expr(self.save_ctxt.lcx, ex); |
e9174d1e SL |
1226 | let adt = self.tcx.expr_ty(&hir_expr).ty_adt_def().unwrap(); |
1227 | let def = self.tcx.resolve_expr(&hir_expr); | |
b039eaaf | 1228 | self.process_struct_lit(ex, path, fields, adt.variant_of_def(def), base) |
e9174d1e | 1229 | } |
7453a54e SL |
1230 | ast::ExprKind::MethodCall(_, _, ref args) => self.process_method_call(ex, args), |
1231 | ast::ExprKind::Field(ref sub_ex, _) => { | |
62682a34 SL |
1232 | self.visit_expr(&sub_ex); |
1233 | ||
1234 | if let Some(field_data) = self.save_ctxt.get_expr_data(ex) { | |
54a0048b SL |
1235 | down_cast_data!(field_data, VariableRefData, ex.span); |
1236 | if !self.span.filter_generated(Some(field_data.span), ex.span) { | |
1237 | self.dumper.variable_ref(ex.span, field_data.normalize(&self.tcx)); | |
1238 | } | |
d9579d0f | 1239 | } |
e9174d1e | 1240 | } |
7453a54e SL |
1241 | ast::ExprKind::TupField(ref sub_ex, idx) => { |
1242 | self.visit_expr(&sub_ex); | |
d9579d0f | 1243 | |
b039eaaf | 1244 | let hir_node = lower_expr(self.save_ctxt.lcx, sub_ex); |
e9174d1e | 1245 | let ty = &self.tcx.expr_ty_adjusted(&hir_node).sty; |
d9579d0f | 1246 | match *ty { |
e9174d1e SL |
1247 | ty::TyStruct(def, _) => { |
1248 | let sub_span = self.span.sub_span_after_token(ex.span, token::Dot); | |
54a0048b SL |
1249 | if !self.span.filter_generated(sub_span, ex.span) { |
1250 | self.dumper.variable_ref(ex.span, VariableRefData { | |
1251 | span: sub_span.expect("No span found for var ref"), | |
1252 | ref_id: def.struct_variant().fields[idx.node].did, | |
1253 | scope: self.cur_scope, | |
1254 | name: String::new() | |
1255 | }.normalize(&self.tcx)); | |
1256 | } | |
d9579d0f | 1257 | } |
62682a34 | 1258 | ty::TyTuple(_) => {} |
54a0048b SL |
1259 | _ => span_bug!(ex.span, |
1260 | "Expected struct or tuple type, found {:?}", | |
1261 | ty), | |
d9579d0f | 1262 | } |
e9174d1e | 1263 | } |
7453a54e | 1264 | ast::ExprKind::Closure(_, ref decl, ref body) => { |
62682a34 | 1265 | let mut id = String::from("$"); |
d9579d0f | 1266 | id.push_str(&ex.id.to_string()); |
62682a34 | 1267 | self.process_formals(&decl.inputs, &id); |
d9579d0f AL |
1268 | |
1269 | // walk arg and return types | |
1270 | for arg in &decl.inputs { | |
7453a54e | 1271 | self.visit_ty(&arg.ty); |
d9579d0f AL |
1272 | } |
1273 | ||
7453a54e SL |
1274 | if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output { |
1275 | self.visit_ty(&ret_ty); | |
d9579d0f AL |
1276 | } |
1277 | ||
1278 | // walk the body | |
7453a54e | 1279 | self.nest(ex.id, |v| v.visit_block(&body)); |
e9174d1e | 1280 | } |
7453a54e SL |
1281 | ast::ExprKind::ForLoop(ref pattern, ref subexpression, ref block, _) | |
1282 | ast::ExprKind::WhileLet(ref pattern, ref subexpression, ref block, _) => { | |
b039eaaf SL |
1283 | let value = self.span.snippet(mk_sp(ex.span.lo, subexpression.span.hi)); |
1284 | self.process_var_decl(pattern, value); | |
1285 | visit::walk_expr(self, subexpression); | |
1286 | visit::walk_block(self, block); | |
1287 | } | |
7453a54e | 1288 | ast::ExprKind::IfLet(ref pattern, ref subexpression, ref block, ref opt_else) => { |
b039eaaf SL |
1289 | let value = self.span.snippet(mk_sp(ex.span.lo, subexpression.span.hi)); |
1290 | self.process_var_decl(pattern, value); | |
1291 | visit::walk_expr(self, subexpression); | |
1292 | visit::walk_block(self, block); | |
1293 | opt_else.as_ref().map(|el| visit::walk_expr(self, el)); | |
1294 | } | |
d9579d0f AL |
1295 | _ => { |
1296 | visit::walk_expr(self, ex) | |
1297 | } | |
1298 | } | |
1299 | } | |
1300 | ||
7453a54e SL |
1301 | fn visit_mac(&mut self, mac: &ast::Mac) { |
1302 | // These shouldn't exist in the AST at this point, log a span bug. | |
54a0048b | 1303 | span_bug!(mac.span, "macro invocation should have been expanded out of AST"); |
d9579d0f AL |
1304 | } |
1305 | ||
1306 | fn visit_pat(&mut self, p: &ast::Pat) { | |
7453a54e | 1307 | self.process_macro_use(p.span, p.id); |
d9579d0f AL |
1308 | self.process_pat(p); |
1309 | } | |
1310 | ||
1311 | fn visit_arm(&mut self, arm: &ast::Arm) { | |
1312 | let mut collector = PathCollector::new(); | |
1313 | for pattern in &arm.pats { | |
1314 | // collect paths from the arm's patterns | |
1315 | collector.visit_pat(&pattern); | |
1316 | self.visit_pat(&pattern); | |
1317 | } | |
1318 | ||
1319 | // This is to get around borrow checking, because we need mut self to call process_path. | |
1320 | let mut paths_to_process = vec![]; | |
c1a9b12d | 1321 | |
d9579d0f AL |
1322 | // process collected paths |
1323 | for &(id, ref p, immut, ref_kind) in &collector.collected_paths { | |
62682a34 | 1324 | let def_map = self.tcx.def_map.borrow(); |
d9579d0f | 1325 | if !def_map.contains_key(&id) { |
54a0048b | 1326 | span_bug!(p.span, "def_map has no key for {} in visit_arm", id); |
d9579d0f AL |
1327 | } |
1328 | let def = def_map.get(&id).unwrap().full_def(); | |
1329 | match def { | |
7453a54e SL |
1330 | Def::Local(_, id) => { |
1331 | let value = if immut == ast::Mutability::Immutable { | |
d9579d0f AL |
1332 | self.span.snippet(p.span).to_string() |
1333 | } else { | |
1334 | "<mutable>".to_string() | |
1335 | }; | |
1336 | ||
b039eaaf SL |
1337 | assert!(p.segments.len() == 1, |
1338 | "qualified path for local variable def in arm"); | |
54a0048b SL |
1339 | if !self.span.filter_generated(Some(p.span), p.span) { |
1340 | self.dumper.variable(p.span, VariableData { | |
1341 | span: p.span, | |
1342 | id: id, | |
1343 | name: path_to_string(p), | |
1344 | qualname: format!("{}${}", path_to_string(p), id), | |
1345 | value: value, | |
1346 | type_value: String::new(), | |
1347 | scope: 0 | |
1348 | }.normalize(&self.tcx)); | |
1349 | } | |
d9579d0f | 1350 | } |
7453a54e SL |
1351 | Def::Variant(..) | Def::Enum(..) | |
1352 | Def::TyAlias(..) | Def::Struct(..) => { | |
d9579d0f AL |
1353 | paths_to_process.push((id, p.clone(), Some(ref_kind))) |
1354 | } | |
1355 | // FIXME(nrc) what are these doing here? | |
7453a54e SL |
1356 | Def::Static(_, _) | |
1357 | Def::Const(..) | | |
1358 | Def::AssociatedConst(..) => {} | |
d9579d0f | 1359 | _ => error!("unexpected definition kind when processing collected paths: {:?}", |
e9174d1e | 1360 | def), |
d9579d0f AL |
1361 | } |
1362 | } | |
c1a9b12d | 1363 | |
d9579d0f | 1364 | for &(id, ref path, ref_kind) in &paths_to_process { |
c1a9b12d | 1365 | self.process_path(id, path, ref_kind); |
d9579d0f | 1366 | } |
b039eaaf | 1367 | walk_list!(self, visit_expr, &arm.guard); |
c1a9b12d | 1368 | self.visit_expr(&arm.body); |
d9579d0f AL |
1369 | } |
1370 | ||
1371 | fn visit_stmt(&mut self, s: &ast::Stmt) { | |
7453a54e SL |
1372 | let id = s.node.id(); |
1373 | self.process_macro_use(s.span, id.unwrap()); | |
d9579d0f AL |
1374 | visit::walk_stmt(self, s) |
1375 | } | |
1376 | ||
1377 | fn visit_local(&mut self, l: &ast::Local) { | |
7453a54e | 1378 | self.process_macro_use(l.span, l.id); |
d9579d0f | 1379 | let value = self.span.snippet(l.span); |
b039eaaf | 1380 | self.process_var_decl(&l.pat, value); |
d9579d0f AL |
1381 | |
1382 | // Just walk the initialiser and type (don't want to walk the pattern again). | |
b039eaaf SL |
1383 | walk_list!(self, visit_ty, &l.ty); |
1384 | walk_list!(self, visit_expr, &l.init); | |
d9579d0f AL |
1385 | } |
1386 | } |