]> git.proxmox.com Git - rustc.git/blob - src/librustc_save_analysis/dump_visitor.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc_save_analysis / dump_visitor.rs
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
11 //! Write the output of rustc's analysis to an implementor of Dump. The data is
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).
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).
29
30 use rustc::hir::def::Def;
31 use rustc::hir::def_id::DefId;
32 use rustc::session::Session;
33 use rustc::ty::{self, TyCtxt};
34
35 use std::collections::HashSet;
36 use std::hash::*;
37
38 use syntax::ast::{self, NodeId, PatKind};
39 use syntax::codemap::*;
40 use syntax::parse::token::{self, keywords};
41 use syntax::visit::{self, Visitor};
42 use syntax::print::pprust::{path_to_string, ty_to_string};
43 use syntax::ptr::P;
44
45 use rustc::hir::lowering::{lower_expr, LoweringContext};
46
47 use super::{escape, generated_code, SaveContext, PathCollector};
48 use super::data::*;
49 use super::dump::Dump;
50 use super::span_utils::SpanUtils;
51 use super::recorder;
52
53 macro_rules! down_cast_data {
54 ($id:ident, $kind:ident, $sp:expr) => {
55 let $id = if let super::Data::$kind(data) = $id {
56 data
57 } else {
58 span_bug!($sp, "unexpected data kind: {:?}", $id);
59 }
60 };
61 }
62
63 pub struct DumpVisitor<'l, 'tcx: 'l, D: 'l> {
64 save_ctxt: SaveContext<'l, 'tcx>,
65 sess: &'l Session,
66 tcx: &'l TyCtxt<'tcx>,
67 analysis: &'l ty::CrateAnalysis<'l>,
68 dumper: &'l mut D,
69
70 span: SpanUtils<'l>,
71
72 cur_scope: NodeId,
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
81 }
82
83 impl <'l, 'tcx, D> DumpVisitor<'l, 'tcx, D>
84 where D: Dump
85 {
86 pub fn new(tcx: &'l TyCtxt<'tcx>,
87 lcx: &'l LoweringContext<'l>,
88 analysis: &'l ty::CrateAnalysis<'l>,
89 dumper: &'l mut D)
90 -> DumpVisitor<'l, 'tcx, D> {
91 let span_utils = SpanUtils::new(&tcx.sess);
92 DumpVisitor {
93 sess: &tcx.sess,
94 tcx: tcx,
95 save_ctxt: SaveContext::from_span_utils(tcx, lcx, span_utils.clone()),
96 analysis: analysis,
97 dumper: dumper,
98 span: span_utils.clone(),
99 cur_scope: 0,
100 mac_defs: HashSet::new(),
101 mac_uses: HashSet::new(),
102 }
103 }
104
105 fn nest<F>(&mut self, scope_id: NodeId, f: F)
106 where F: FnOnce(&mut DumpVisitor<'l, 'tcx, D>)
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) {
115 let source_file = self.tcx.sess.local_crate_source_file.as_ref();
116 let crate_root = source_file.map(|source_file| {
117 match source_file.file_name() {
118 Some(_) => source_file.parent().unwrap().display().to_string(),
119 None => source_file.display().to_string(),
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();
130
131 // The current crate.
132 let data = CratePreludeData {
133 crate_name: name.into(),
134 crate_root: crate_root,
135 external_crates: external_crates
136 };
137
138 self.dumper.crate_prelude(krate.span, data);
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() {
152 if generated_code(path.span) {
153 return vec!();
154 }
155 error!("Mis-calculated spans for path '{}'. Found {} spans, expected {}. Found spans:",
156 path_to_string(path),
157 spans.len(),
158 path.segments.len());
159 for s in &spans {
160 let loc = self.sess.codemap().lookup_char_pos(s.lo);
161 error!(" '{}' in {}, line {}",
162 self.span.snippet(*s),
163 loc.file.name,
164 loc.line);
165 }
166 return vec!();
167 }
168
169 let mut result: Vec<(Span, String)> = vec!();
170
171 let mut segs = vec!();
172 for (i, (seg, span)) in path.segments.iter().zip(&spans).enumerate() {
173 segs.push(seg.clone());
174 let sub_path = ast::Path {
175 span: *span, // span for the last segment
176 global: path.global,
177 segments: segs,
178 };
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 };
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));
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 };
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));
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];
249 self.dumper.type_ref(path.span, TypeRefData {
250 ref_id: None,
251 span: *span,
252 qualname: qualname.to_owned(),
253 scope: 0
254 });
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 {
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));
268 }
269 }
270
271 // looks up anything, not just a type
272 fn lookup_type_ref(&self, ref_id: NodeId) -> Option<DefId> {
273 if !self.tcx.def_map.borrow().contains_key(&ref_id) {
274 bug!("def_map has no key for {} in lookup_type_ref", ref_id);
275 }
276 let def = self.tcx.def_map.borrow().get(&ref_id).unwrap().full_def();
277 match def {
278 Def::PrimTy(..) => None,
279 Def::SelfTy(..) => None,
280 _ => Some(def.def_id()),
281 }
282 }
283
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
294 let def_map = self.tcx.def_map.borrow();
295 if !def_map.contains_key(&ref_id) {
296 span_bug!(span,
297 "def_map has no key for {} in lookup_def_kind",
298 ref_id);
299 }
300 let def = def_map.get(&ref_id).unwrap().full_def();
301 match def {
302 Def::Mod(_) |
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(..) |
312 Def::Enum(..) |
313 Def::TyAlias(..) |
314 Def::AssociatedTy(..) |
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 }
323 Def::Static(_, _) |
324 Def::Const(_) |
325 Def::AssociatedConst(..) |
326 Def::Local(..) |
327 Def::Variant(..) |
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 }
343 Def::SelfTy(..) |
344 Def::Label(_) |
345 Def::TyParam(..) |
346 Def::Method(..) |
347 Def::PrimTy(_) |
348 Def::Err => {
349 span_bug!(span,
350 "process_def_kind for unexpected item: {:?}",
351 def);
352 }
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 {
363 let typ = self.tcx.node_types().get(&id).unwrap().to_string();
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?)
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 }
378 }
379 }
380 }
381
382 fn process_method(&mut self,
383 sig: &ast::MethodSig,
384 body: Option<&ast::Block>,
385 id: ast::NodeId,
386 name: ast::Name,
387 span: Span) {
388 debug!("process_method: {}:{}", id, name);
389
390 if let Some(method_data) = self.save_ctxt.get_method_data(id, name, span) {
391
392 if body.is_some() {
393 if !self.span.filter_generated(Some(method_data.span), span) {
394 self.dumper.function(span, method_data.clone().normalize(&self.tcx));
395 }
396 self.process_formals(&sig.decl.inputs, &method_data.qualname);
397 } else {
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 }
406 }
407 self.process_generic_params(&sig.generics, span, &method_data.qualname, id);
408 }
409
410 // walk arg and return types
411 for arg in &sig.decl.inputs {
412 self.visit_ty(&arg.ty);
413 }
414
415 if let ast::FunctionRetTy::Ty(ref ret_ty) = sig.decl.output {
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 }
423 }
424
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 {
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
432 visit::walk_path(self, &trait_ref.path);
433 }
434 }
435
436 fn process_struct_field_def(&mut self, field: &ast::StructField, parent_id: NodeId) {
437 let field_data = self.save_ctxt.get_field_data(field, parent_id);
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 }
444 }
445 }
446
447 // Dump generic params bindings, then visit_generics
448 fn process_generic_params(&mut self,
449 generics: &ast::Generics,
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,
458 (generics.ty_params.len() as isize));
459 for (param, param_ss) in generics.ty_params.iter().zip(param_sub_spans) {
460 // Append $id to name to make sure each one is unique
461 let name = format!("{}::{}${}",
462 prefix,
463 escape(self.span.snippet(param_ss)),
464 id);
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 }
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) {
482 if let Some(fn_data) = self.save_ctxt.get_item_data(item) {
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 }
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 }
491
492 for arg in &decl.inputs {
493 self.visit_ty(&arg.ty);
494 }
495
496 if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output {
497 self.visit_ty(&ret_ty);
498 }
499
500 self.nest(item.id, |v| v.visit_block(&body));
501 }
502
503 fn process_static_or_const_item(&mut self, item: &ast::Item, typ: &ast::Ty, expr: &ast::Expr) {
504 if let Some(var_data) = self.save_ctxt.get_item_data(item) {
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 }
511 }
512 self.visit_ty(&typ);
513 self.visit_expr(expr);
514 }
515
516 fn process_const(&mut self,
517 id: ast::NodeId,
518 name: ast::Name,
519 span: Span,
520 typ: &ast::Ty,
521 expr: &ast::Expr) {
522 let qualname = format!("::{}", self.tcx.node_path_str(id));
523
524 let sub_span = self.span.sub_span_after_keyword(span, keywords::Const);
525
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 }
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,
545 def: &ast::VariantData,
546 ty_params: &ast::Generics) {
547 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
548
549 let val = self.span.snippet(item.span);
550 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Struct);
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
562
563 // fields
564 for field in def.fields() {
565 self.process_struct_field_def(field, item.id);
566 self.visit_ty(&field.ty);
567 }
568
569 self.process_generic_params(ty_params, item.span, &qualname, item.id);
570 }
571
572 fn process_enum(&mut self,
573 item: &ast::Item,
574 enum_definition: &ast::EnumDef,
575 ty_params: &ast::Generics) {
576 let enum_data = self.save_ctxt.get_item_data(item);
577 let enum_data = match enum_data {
578 None => return,
579 Some(data) => data,
580 };
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 }
586
587 for variant in &enum_definition.variants {
588 let name = &variant.node.name.name.as_str();
589 let mut qualname = enum_data.qualname.clone();
590 qualname.push_str("::");
591 qualname.push_str(name);
592 let val = self.span.snippet(variant.span);
593
594 match variant.node.data {
595 ast::VariantData::Struct(..) => {
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 }
607 }
608 _ => {
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 }
621 }
622 }
623
624
625 for field in variant.node.data.fields() {
626 self.process_struct_field_def(field, variant.node.data.id());
627 self.visit_ty(&field.ty);
628 }
629 }
630 self.process_generic_params(ty_params, item.span, &enum_data.qualname, enum_data.id);
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,
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) {
641 down_cast_data!(impl_data, ImplData, item.span);
642 if let Some(ref self_ref) = impl_data.self_ref {
643 has_self_ref = true;
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 }
647 }
648 if let Some(ref trait_ref_data) = impl_data.trait_ref {
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
653 visit::walk_path(self, &trait_ref.as_ref().unwrap().path);
654 }
655
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 }
665 }
666 if !has_self_ref {
667 self.visit_ty(&typ);
668 }
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,
678 trait_refs: &ast::TyParamBounds,
679 methods: &[ast::TraitItem]) {
680 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
681 let val = self.span.snippet(item.span);
682 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Trait);
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 }
692
693 // super-traits
694 for super_bound in trait_refs.iter() {
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;
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));
723 }
724 }
725 }
726
727 // walk generics and methods
728 self.process_generic_params(generics, item.span, &qualname, item.id);
729 for method in methods {
730 self.visit_trait_item(method)
731 }
732 }
733
734 // `item` is the module in question, represented as an item.
735 fn process_mod(&mut self, item: &ast::Item) {
736 if let Some(mod_data) = self.save_ctxt.get_item_data(item) {
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 }
741 }
742 }
743
744 fn process_path(&mut self, id: NodeId, path: &ast::Path, ref_kind: Option<recorder::Row>) {
745 let path_data = self.save_ctxt.get_path_data(id, path);
746 if generated_code(path.span) && path_data.is_none() {
747 return;
748 }
749
750 let path_data = match path_data {
751 Some(pd) => pd,
752 None => {
753 span_bug!(path.span,
754 "Unexpected def kind while looking up path in `{}`",
755 self.span.snippet(path.span))
756 }
757 };
758
759 match path_data {
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 }
791
792 }
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 }
797 }
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 }
802 }
803 Data::FunctionCallData(fcd) => {
804 if !self.span.filter_generated(Some(fcd.span), path.span) {
805 self.dumper.function_call(path.span, fcd.normalize(&self.tcx));
806 }
807 }
808 _ => {
809 span_bug!(path.span, "Unexpected data: {:?}", path_data);
810 }
811 }
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();
816 match def {
817 Def::Method(did) => {
818 let ti = self.tcx.impl_or_trait_item(did);
819 if let ty::MethodTraitItem(m) = ti {
820 if m.explicit_self == ty::ExplicitSelfCategory::Static {
821 self.write_sub_path_trait_truncated(path);
822 }
823 }
824 }
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),
832 _ => {}
833 }
834 }
835
836 fn process_struct_lit(&mut self,
837 ex: &ast::Expr,
838 path: &ast::Path,
839 fields: &Vec<ast::Field>,
840 variant: ty::VariantDef,
841 base: &Option<P<ast::Expr>>) {
842 self.write_sub_paths_truncated(path, false);
843
844 if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) {
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
850 let scope = self.save_ctxt.enclosing_scope(ex.id);
851
852 for field in fields {
853 if let Some(field_data) = self.save_ctxt
854 .get_field_ref_data(field, variant, scope) {
855
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 }
859 }
860
861 self.visit_expr(&field.expr)
862 }
863 }
864
865 walk_list!(self, visit_expr, base);
866 }
867
868 fn process_method_call(&mut self, ex: &ast::Expr, args: &Vec<P<ast::Expr>>) {
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 }
874 }
875
876 // walk receiver and args
877 walk_list!(self, visit_expr, args);
878 }
879
880 fn process_pat(&mut self, p: &ast::Pat) {
881 match p.node {
882 PatKind::Struct(ref path, ref fields, _) => {
883 visit::walk_path(self, path);
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);
887
888 for &Spanned { node: ref field, span } in fields {
889 let sub_span = self.span.span_for_first_ident(span);
890 if let Some(f) = variant.find_field_named(field.ident.name) {
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 }
899 }
900 self.visit_pat(&field.pat);
901 }
902 }
903 _ => visit::walk_pat(self, p),
904 }
905 }
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 {
916 let value = if immut == ast::Mutability::Immutable {
917 value.to_string()
918 } else {
919 "<mutable>".to_string()
920 };
921 let types = self.tcx.node_types();
922 let typ = types.get(&id).map(|t| t.to_string()).unwrap_or(String::new());
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.
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 }
938 }
939 }
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) {
962 self.dumper.macro_data(data.callee_span, MacroData {
963 span: sub_span,
964 name: data.name.clone(),
965 qualname: qualname.clone()
966 });
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) {
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));
980 }
981 }
982 }
983 }
984
985 impl<'l, 'tcx, 'v, D: Dump + 'l> Visitor<'v> for DumpVisitor<'l, 'tcx, D> {
986 fn visit_item(&mut self, item: &ast::Item) {
987 use syntax::ast::ItemKind::*;
988 self.process_macro_use(item.span, item.id);
989 match item.node {
990 Use(ref use_item) => {
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) => {
996 let scope = self.cur_scope;
997 self.process_def_kind(item.id, path.span, sub_span, def_id, scope);
998
999 Some(def_id)
1000 }
1001 None => None,
1002 };
1003
1004 // 'use' always introduces an alias, if there is not an explicit
1005 // one, there is an implicit one.
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 };
1011
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 }
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.
1025 let mut names = vec![];
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() {
1030 names.push(n.to_string());
1031 }
1032 }
1033
1034 let sub_span = self.span
1035 .sub_span_of_token(path.span, token::BinOp(token::Star));
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 }
1044 self.write_sub_paths(path, true);
1045 }
1046 ast::ViewPathList(ref path, ref list) => {
1047 for plid in list {
1048 match plid.node {
1049 ast::PathListItemKind::Ident { id, .. } => {
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);
1057 }
1058 }
1059 ast::PathListItemKind::Mod { .. } => (),
1060 }
1061 }
1062
1063 self.write_sub_paths(path, true);
1064 }
1065 }
1066 }
1067 ExternCrate(ref s) => {
1068 let location = match *s {
1069 Some(s) => s.to_string(),
1070 None => item.ident.to_string(),
1071 };
1072 let alias_span = self.span.span_for_last_ident(item.span);
1073 let cnum = match self.sess.cstore.extern_mod_stmt_cnum(item.id) {
1074 Some(cnum) => cnum,
1075 None => 0,
1076 };
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 }
1088 }
1089 Fn(ref decl, _, _, _, ref ty_params, ref body) =>
1090 self.process_fn(item, &decl, ty_params, &body),
1091 Static(ref typ, _, ref expr) =>
1092 self.process_static_or_const_item(item, typ, expr),
1093 Const(ref typ, ref expr) =>
1094 self.process_static_or_const_item(item, &typ, &expr),
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(_, _,
1098 ref ty_params,
1099 ref trait_ref,
1100 ref typ,
1101 ref impl_items) => {
1102 self.process_impl(item, ty_params, trait_ref, &typ, impl_items)
1103 }
1104 Trait(_, ref generics, ref trait_refs, ref methods) =>
1105 self.process_trait(item, generics, trait_refs, methods),
1106 Mod(ref m) => {
1107 self.process_mod(item);
1108 self.nest(item.id, |v| visit::walk_mod(v, m));
1109 }
1110 Ty(ref ty, ref ty_params) => {
1111 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
1112 let value = ty_to_string(&ty);
1113 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Type);
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 }
1122
1123 self.visit_ty(&ty);
1124 self.process_generic_params(ty_params, item.span, &qualname, item.id);
1125 }
1126 Mac(_) => (),
1127 _ => visit::walk_item(self, item),
1128 }
1129 }
1130
1131 fn visit_generics(&mut self, generics: &ast::Generics) {
1132 for param in generics.ty_params.iter() {
1133 for bound in param.bounds.iter() {
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 {
1139 self.visit_ty(&ty);
1140 }
1141 }
1142 }
1143
1144 fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
1145 self.process_macro_use(trait_item.span, trait_item.id);
1146 match trait_item.node {
1147 ast::TraitItemKind::Const(ref ty, Some(ref expr)) => {
1148 self.process_const(trait_item.id,
1149 trait_item.ident.name,
1150 trait_item.span,
1151 &ty,
1152 &expr);
1153 }
1154 ast::TraitItemKind::Method(ref sig, ref body) => {
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);
1160 }
1161 ast::TraitItemKind::Const(_, None) |
1162 ast::TraitItemKind::Type(..) => {}
1163 }
1164 }
1165
1166 fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
1167 self.process_macro_use(impl_item.span, impl_item.id);
1168 match impl_item.node {
1169 ast::ImplItemKind::Const(ref ty, ref expr) => {
1170 self.process_const(impl_item.id,
1171 impl_item.ident.name,
1172 impl_item.span,
1173 &ty,
1174 &expr);
1175 }
1176 ast::ImplItemKind::Method(ref sig, ref body) => {
1177 self.process_method(sig,
1178 Some(body),
1179 impl_item.id,
1180 impl_item.ident.name,
1181 impl_item.span);
1182 }
1183 ast::ImplItemKind::Type(_) |
1184 ast::ImplItemKind::Macro(_) => {}
1185 }
1186 }
1187
1188 fn visit_ty(&mut self, t: &ast::Ty) {
1189 self.process_macro_use(t.span, t.id);
1190 match t.node {
1191 ast::TyKind::Path(_, ref path) => {
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));
1201 }
1202 }
1203
1204 self.write_sub_paths_truncated(path, false);
1205
1206 visit::walk_path(self, path);
1207 }
1208 _ => visit::walk_ty(self, t),
1209 }
1210 }
1211
1212 fn visit_expr(&mut self, ex: &ast::Expr) {
1213 self.process_macro_use(ex.span, ex.id);
1214 match ex.node {
1215 ast::ExprKind::Call(ref _f, ref _args) => {
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 }
1220 ast::ExprKind::Path(_, ref path) => {
1221 self.process_path(ex.id, path, None);
1222 visit::walk_expr(self, ex);
1223 }
1224 ast::ExprKind::Struct(ref path, ref fields, ref base) => {
1225 let hir_expr = lower_expr(self.save_ctxt.lcx, ex);
1226 let adt = self.tcx.expr_ty(&hir_expr).ty_adt_def().unwrap();
1227 let def = self.tcx.resolve_expr(&hir_expr);
1228 self.process_struct_lit(ex, path, fields, adt.variant_of_def(def), base)
1229 }
1230 ast::ExprKind::MethodCall(_, _, ref args) => self.process_method_call(ex, args),
1231 ast::ExprKind::Field(ref sub_ex, _) => {
1232 self.visit_expr(&sub_ex);
1233
1234 if let Some(field_data) = self.save_ctxt.get_expr_data(ex) {
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 }
1239 }
1240 }
1241 ast::ExprKind::TupField(ref sub_ex, idx) => {
1242 self.visit_expr(&sub_ex);
1243
1244 let hir_node = lower_expr(self.save_ctxt.lcx, sub_ex);
1245 let ty = &self.tcx.expr_ty_adjusted(&hir_node).sty;
1246 match *ty {
1247 ty::TyStruct(def, _) => {
1248 let sub_span = self.span.sub_span_after_token(ex.span, token::Dot);
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 }
1257 }
1258 ty::TyTuple(_) => {}
1259 _ => span_bug!(ex.span,
1260 "Expected struct or tuple type, found {:?}",
1261 ty),
1262 }
1263 }
1264 ast::ExprKind::Closure(_, ref decl, ref body) => {
1265 let mut id = String::from("$");
1266 id.push_str(&ex.id.to_string());
1267 self.process_formals(&decl.inputs, &id);
1268
1269 // walk arg and return types
1270 for arg in &decl.inputs {
1271 self.visit_ty(&arg.ty);
1272 }
1273
1274 if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output {
1275 self.visit_ty(&ret_ty);
1276 }
1277
1278 // walk the body
1279 self.nest(ex.id, |v| v.visit_block(&body));
1280 }
1281 ast::ExprKind::ForLoop(ref pattern, ref subexpression, ref block, _) |
1282 ast::ExprKind::WhileLet(ref pattern, ref subexpression, ref block, _) => {
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 }
1288 ast::ExprKind::IfLet(ref pattern, ref subexpression, ref block, ref opt_else) => {
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 }
1295 _ => {
1296 visit::walk_expr(self, ex)
1297 }
1298 }
1299 }
1300
1301 fn visit_mac(&mut self, mac: &ast::Mac) {
1302 // These shouldn't exist in the AST at this point, log a span bug.
1303 span_bug!(mac.span, "macro invocation should have been expanded out of AST");
1304 }
1305
1306 fn visit_pat(&mut self, p: &ast::Pat) {
1307 self.process_macro_use(p.span, p.id);
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![];
1321
1322 // process collected paths
1323 for &(id, ref p, immut, ref_kind) in &collector.collected_paths {
1324 let def_map = self.tcx.def_map.borrow();
1325 if !def_map.contains_key(&id) {
1326 span_bug!(p.span, "def_map has no key for {} in visit_arm", id);
1327 }
1328 let def = def_map.get(&id).unwrap().full_def();
1329 match def {
1330 Def::Local(_, id) => {
1331 let value = if immut == ast::Mutability::Immutable {
1332 self.span.snippet(p.span).to_string()
1333 } else {
1334 "<mutable>".to_string()
1335 };
1336
1337 assert!(p.segments.len() == 1,
1338 "qualified path for local variable def in arm");
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 }
1350 }
1351 Def::Variant(..) | Def::Enum(..) |
1352 Def::TyAlias(..) | Def::Struct(..) => {
1353 paths_to_process.push((id, p.clone(), Some(ref_kind)))
1354 }
1355 // FIXME(nrc) what are these doing here?
1356 Def::Static(_, _) |
1357 Def::Const(..) |
1358 Def::AssociatedConst(..) => {}
1359 _ => error!("unexpected definition kind when processing collected paths: {:?}",
1360 def),
1361 }
1362 }
1363
1364 for &(id, ref path, ref_kind) in &paths_to_process {
1365 self.process_path(id, path, ref_kind);
1366 }
1367 walk_list!(self, visit_expr, &arm.guard);
1368 self.visit_expr(&arm.body);
1369 }
1370
1371 fn visit_stmt(&mut self, s: &ast::Stmt) {
1372 let id = s.node.id();
1373 self.process_macro_use(s.span, id.unwrap());
1374 visit::walk_stmt(self, s)
1375 }
1376
1377 fn visit_local(&mut self, l: &ast::Local) {
1378 self.process_macro_use(l.span, l.id);
1379 let value = self.span.snippet(l.span);
1380 self.process_var_decl(&l.pat, value);
1381
1382 // Just walk the initialiser and type (don't want to walk the pattern again).
1383 walk_list!(self, visit_ty, &l.ty);
1384 walk_list!(self, visit_expr, &l.init);
1385 }
1386 }