]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/save/dump_csv.rs
Rollup merge of #28216 - christopherdumas:fix_28196, r=steveklabnik
[rust.git] / src / librustc_trans / save / dump_csv.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 //! Output a CSV file containing the output from rustc's analysis. 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 //! Recorder is used for recording the output in csv format. FmtStrs separates
27 //! the format of the output away from extracting it from the compiler.
28 //! DumpCsvVisitor walks the AST and processes it.
29
30
31 use super::{escape, generated_code, recorder, SaveContext, PathCollector, Data};
32
33 use session::Session;
34
35 use middle::def;
36 use middle::def_id::DefId;
37 use middle::ty::{self, Ty};
38
39 use std::fs::File;
40 use std::path::Path;
41
42 use syntax::ast::{self, NodeId};
43 use syntax::codemap::*;
44 use syntax::parse::token::{self, keywords};
45 use syntax::owned_slice::OwnedSlice;
46 use syntax::visit::{self, Visitor};
47 use syntax::print::pprust::{path_to_string, ty_to_string};
48 use syntax::ptr::P;
49
50 use rustc_front::lowering::lower_expr;
51
52 use super::span_utils::SpanUtils;
53 use super::recorder::{Recorder, FmtStrs};
54
55 macro_rules! down_cast_data {
56     ($id:ident, $kind:ident, $this:ident, $sp:expr) => {
57         let $id = if let super::Data::$kind(data) = $id {
58             data
59         } else {
60             $this.sess.span_bug($sp, &format!("unexpected data kind: {:?}", $id));
61         };
62     };
63 }
64
65 pub struct DumpCsvVisitor<'l, 'tcx: 'l> {
66     save_ctxt: SaveContext<'l, 'tcx>,
67     sess: &'l Session,
68     tcx: &'l ty::ctxt<'tcx>,
69     analysis: &'l ty::CrateAnalysis,
70
71     span: SpanUtils<'l>,
72     fmt: FmtStrs<'l>,
73
74     cur_scope: NodeId
75 }
76
77 impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
78     pub fn new(tcx: &'l ty::ctxt<'tcx>,
79                analysis: &'l ty::CrateAnalysis,
80                output_file: Box<File>) -> DumpCsvVisitor<'l, 'tcx> {
81         let span_utils = SpanUtils::new(&tcx.sess);
82         DumpCsvVisitor {
83             sess: &tcx.sess,
84             tcx: tcx,
85             save_ctxt: SaveContext::from_span_utils(tcx, span_utils.clone()),
86             analysis: analysis,
87             span: span_utils.clone(),
88             fmt: FmtStrs::new(box Recorder {
89                                 out: output_file,
90                                 dump_spans: false,
91                               }, span_utils),
92             cur_scope: 0
93         }
94     }
95
96     fn nest<F>(&mut self, scope_id: NodeId, f: F) where
97         F: FnOnce(&mut DumpCsvVisitor<'l, 'tcx>),
98     {
99         let parent_scope = self.cur_scope;
100         self.cur_scope = scope_id;
101         f(self);
102         self.cur_scope = parent_scope;
103     }
104
105     pub fn dump_crate_info(&mut self, name: &str, krate: &ast::Crate) {
106         // The current crate.
107         self.fmt.crate_str(krate.span, name);
108
109         // Dump info about all the external crates referenced from this crate.
110         for c in &self.save_ctxt.get_external_crates() {
111             self.fmt.external_crate_str(krate.span, &c.name, c.number);
112         }
113         self.fmt.recorder.record("end_external_crates\n");
114     }
115
116     // Return all non-empty prefixes of a path.
117     // For each prefix, we return the span for the last segment in the prefix and
118     // a str representation of the entire prefix.
119     fn process_path_prefixes(&self, path: &ast::Path) -> Vec<(Span, String)> {
120         let spans = self.span.spans_for_path_segments(path);
121
122         // Paths to enums seem to not match their spans - the span includes all the
123         // variants too. But they seem to always be at the end, so I hope we can cope with
124         // always using the first ones. So, only error out if we don't have enough spans.
125         // What could go wrong...?
126         if spans.len() < path.segments.len() {
127             error!("Mis-calculated spans for path '{}'. \
128                     Found {} spans, expected {}. Found spans:",
129                    path_to_string(path), spans.len(), path.segments.len());
130             for s in &spans {
131                 let loc = self.sess.codemap().lookup_char_pos(s.lo);
132                 error!("    '{}' in {}, line {}",
133                        self.span.snippet(*s), loc.file.name, loc.line);
134             }
135             return vec!();
136         }
137
138         let mut result: Vec<(Span, String)> = vec!();
139
140         let mut segs = vec!();
141         for (i, (seg, span)) in path.segments.iter().zip(&spans).enumerate() {
142             segs.push(seg.clone());
143             let sub_path = ast::Path{span: *span, // span for the last segment
144                                      global: path.global,
145                                      segments: segs};
146             let qualname = if i == 0 && path.global {
147                 format!("::{}", path_to_string(&sub_path))
148             } else {
149                 path_to_string(&sub_path)
150             };
151             result.push((*span, qualname));
152             segs = sub_path.segments;
153         }
154
155         result
156     }
157
158     // The global arg allows us to override the global-ness of the path (which
159     // actually means 'does the path start with `::`', rather than 'is the path
160     // semantically global). We use the override for `use` imports (etc.) where
161     // the syntax is non-global, but the semantics are global.
162     fn write_sub_paths(&mut self, path: &ast::Path, global: bool) {
163         let sub_paths = self.process_path_prefixes(path);
164         for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
165             let qualname = if i == 0 && global && !path.global {
166                 format!("::{}", qualname)
167             } else {
168                 qualname.clone()
169             };
170             self.fmt.sub_mod_ref_str(path.span,
171                                      *span,
172                                      &qualname,
173                                      self.cur_scope);
174         }
175     }
176
177     // As write_sub_paths, but does not process the last ident in the path (assuming it
178     // will be processed elsewhere). See note on write_sub_paths about global.
179     fn write_sub_paths_truncated(&mut self, path: &ast::Path, global: bool) {
180         let sub_paths = self.process_path_prefixes(path);
181         let len = sub_paths.len();
182         if len <= 1 {
183             return;
184         }
185
186         let sub_paths = &sub_paths[..len-1];
187         for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
188             let qualname = if i == 0 && global && !path.global {
189                 format!("::{}", qualname)
190             } else {
191                 qualname.clone()
192             };
193             self.fmt.sub_mod_ref_str(path.span,
194                                      *span,
195                                      &qualname,
196                                      self.cur_scope);
197         }
198     }
199
200     // As write_sub_paths, but expects a path of the form module_path::trait::method
201     // Where trait could actually be a struct too.
202     fn write_sub_path_trait_truncated(&mut self, path: &ast::Path) {
203         let sub_paths = self.process_path_prefixes(path);
204         let len = sub_paths.len();
205         if len <= 1 {
206             return;
207         }
208         let sub_paths = &sub_paths[.. (len-1)];
209
210         // write the trait part of the sub-path
211         let (ref span, ref qualname) = sub_paths[len-2];
212         self.fmt.sub_type_ref_str(path.span,
213                                   *span,
214                                   &qualname);
215
216         // write the other sub-paths
217         if len <= 2 {
218             return;
219         }
220         let sub_paths = &sub_paths[..len-2];
221         for &(ref span, ref qualname) in sub_paths {
222             self.fmt.sub_mod_ref_str(path.span,
223                                      *span,
224                                      &qualname,
225                                      self.cur_scope);
226         }
227     }
228
229     // looks up anything, not just a type
230     fn lookup_type_ref(&self, ref_id: NodeId) -> Option<DefId> {
231         if !self.tcx.def_map.borrow().contains_key(&ref_id) {
232             self.sess.bug(&format!("def_map has no key for {} in lookup_type_ref",
233                                   ref_id));
234         }
235         let def = self.tcx.def_map.borrow().get(&ref_id).unwrap().full_def();
236         match def {
237             def::DefPrimTy(_) => None,
238             _ => Some(def.def_id()),
239         }
240     }
241
242     fn lookup_def_kind(&self, ref_id: NodeId, span: Span) -> Option<recorder::Row> {
243         let def_map = self.tcx.def_map.borrow();
244         if !def_map.contains_key(&ref_id) {
245             self.sess.span_bug(span, &format!("def_map has no key for {} in lookup_def_kind",
246                                              ref_id));
247         }
248         let def = def_map.get(&ref_id).unwrap().full_def();
249         match def {
250             def::DefMod(_) |
251             def::DefForeignMod(_) => Some(recorder::ModRef),
252             def::DefStruct(_) => Some(recorder::TypeRef),
253             def::DefTy(..) |
254             def::DefAssociatedTy(..) |
255             def::DefTrait(_) => Some(recorder::TypeRef),
256             def::DefStatic(_, _) |
257             def::DefConst(_) |
258             def::DefAssociatedConst(..) |
259             def::DefLocal(_) |
260             def::DefVariant(_, _, _) |
261             def::DefUpvar(..) => Some(recorder::VarRef),
262
263             def::DefFn(..) => Some(recorder::FnRef),
264
265             def::DefSelfTy(..) |
266             def::DefRegion(_) |
267             def::DefLabel(_) |
268             def::DefTyParam(..) |
269             def::DefUse(_) |
270             def::DefMethod(..) |
271             def::DefPrimTy(_) => {
272                 self.sess.span_bug(span, &format!("lookup_def_kind for unexpected item: {:?}",
273                                                  def));
274             },
275         }
276     }
277
278     fn process_formals(&mut self, formals: &Vec<ast::Arg>, qualname: &str) {
279         for arg in formals {
280             self.visit_pat(&arg.pat);
281             let mut collector = PathCollector::new();
282             collector.visit_pat(&arg.pat);
283             let span_utils = self.span.clone();
284             for &(id, ref p, _, _) in &collector.collected_paths {
285                 let typ = self.tcx.node_types().get(&id).unwrap().to_string();
286                 // get the span only for the name of the variable (I hope the path is only ever a
287                 // variable name, but who knows?)
288                 self.fmt.formal_str(p.span,
289                                     span_utils.span_for_last_ident(p.span),
290                                     id,
291                                     qualname,
292                                     &path_to_string(p),
293                                     &typ);
294             }
295         }
296     }
297
298     fn process_method(&mut self,
299                       sig: &ast::MethodSig,
300                       body: Option<&ast::Block>,
301                       id: ast::NodeId,
302                       name: ast::Name,
303                       span: Span) {
304         if generated_code(span) {
305             return;
306         }
307
308         debug!("process_method: {}:{}", id, name);
309
310         let method_data = self.save_ctxt.get_method_data(id, name, span);
311
312         if body.is_some() {
313             self.fmt.method_str(span,
314                                 Some(method_data.span),
315                                 method_data.id,
316                                 &method_data.qualname,
317                                 method_data.declaration,
318                                 method_data.scope);
319             self.process_formals(&sig.decl.inputs, &method_data.qualname);
320         } else {
321             self.fmt.method_decl_str(span,
322                                      Some(method_data.span),
323                                      method_data.id,
324                                      &method_data.qualname,
325                                      method_data.scope);
326         }
327
328         // walk arg and return types
329         for arg in &sig.decl.inputs {
330             self.visit_ty(&arg.ty);
331         }
332
333         if let ast::Return(ref ret_ty) = sig.decl.output {
334             self.visit_ty(ret_ty);
335         }
336
337         // walk the fn body
338         if let Some(body) = body {
339             self.nest(id, |v| v.visit_block(body));
340         }
341
342         self.process_generic_params(&sig.generics,
343                                     span,
344                                     &method_data.qualname,
345                                     id);
346     }
347
348     fn process_trait_ref(&mut self, trait_ref: &ast::TraitRef) {
349         let trait_ref_data = self.save_ctxt.get_trait_ref_data(trait_ref, self.cur_scope);
350         if let Some(trait_ref_data) = trait_ref_data {
351             self.fmt.ref_str(recorder::TypeRef,
352                              trait_ref.path.span,
353                              Some(trait_ref_data.span),
354                              trait_ref_data.ref_id,
355                              trait_ref_data.scope);
356             visit::walk_path(self, &trait_ref.path);
357         }
358     }
359
360     fn process_struct_field_def(&mut self,
361                                 field: &ast::StructField,
362                                 parent_id: NodeId) {
363         let field_data = self.save_ctxt.get_field_data(field, parent_id);
364         if let Some(field_data) = field_data {
365             self.fmt.field_str(field.span,
366                                Some(field_data.span),
367                                field_data.id,
368                                &field_data.name,
369                                &field_data.qualname,
370                                &field_data.type_value,
371                                field_data.scope);
372         }
373     }
374
375     // Dump generic params bindings, then visit_generics
376     fn process_generic_params(&mut self,
377                               generics:&ast::Generics,
378                               full_span: Span,
379                               prefix: &str,
380                               id: NodeId) {
381         // We can't only use visit_generics since we don't have spans for param
382         // bindings, so we reparse the full_span to get those sub spans.
383         // However full span is the entire enum/fn/struct block, so we only want
384         // the first few to match the number of generics we're looking for.
385         let param_sub_spans = self.span.spans_for_ty_params(full_span,
386                                                            (generics.ty_params.len() as isize));
387         for (param, param_ss) in generics.ty_params.iter().zip(param_sub_spans) {
388             // Append $id to name to make sure each one is unique
389             let name = format!("{}::{}${}",
390                                prefix,
391                                escape(self.span.snippet(param_ss)),
392                                id);
393             self.fmt.typedef_str(full_span,
394                                  Some(param_ss),
395                                  param.id,
396                                  &name,
397                                  "");
398         }
399         self.visit_generics(generics);
400     }
401
402     fn process_fn(&mut self,
403                   item: &ast::Item,
404                   decl: &ast::FnDecl,
405                   ty_params: &ast::Generics,
406                   body: &ast::Block) {
407         let fn_data = self.save_ctxt.get_item_data(item);
408         down_cast_data!(fn_data, FunctionData, self, item.span);
409         self.fmt.fn_str(item.span,
410                         Some(fn_data.span),
411                         fn_data.id,
412                         &fn_data.qualname,
413                         fn_data.scope);
414
415
416         self.process_formals(&decl.inputs, &fn_data.qualname);
417         self.process_generic_params(ty_params, item.span, &fn_data.qualname, item.id);
418
419         for arg in &decl.inputs {
420             self.visit_ty(&arg.ty);
421         }
422
423         if let ast::Return(ref ret_ty) = decl.output {
424             self.visit_ty(&ret_ty);
425         }
426
427         self.nest(item.id, |v| v.visit_block(&body));
428     }
429
430     fn process_static_or_const_item(&mut self,
431                                     item: &ast::Item,
432                                     typ: &ast::Ty,
433                                     expr: &ast::Expr)
434     {
435         let var_data = self.save_ctxt.get_item_data(item);
436         down_cast_data!(var_data, VariableData, self, item.span);
437         self.fmt.static_str(item.span,
438                             Some(var_data.span),
439                             var_data.id,
440                             &var_data.name,
441                             &var_data.qualname,
442                             &var_data.value,
443                             &var_data.type_value,
444                             var_data.scope);
445
446         self.visit_ty(&typ);
447         self.visit_expr(expr);
448     }
449
450     fn process_const(&mut self,
451                      id: ast::NodeId,
452                      ident: &ast::Ident,
453                      span: Span,
454                      typ: &ast::Ty,
455                      expr: &ast::Expr)
456     {
457         let qualname = format!("::{}", self.tcx.map.path_to_string(id));
458
459         let sub_span = self.span.sub_span_after_keyword(span,
460                                                         keywords::Const);
461
462         self.fmt.static_str(span,
463                             sub_span,
464                             id,
465                             &ident.name.as_str(),
466                             &qualname,
467                             &self.span.snippet(expr.span),
468                             &ty_to_string(&*typ),
469                             self.cur_scope);
470
471         // walk type and init value
472         self.visit_ty(typ);
473         self.visit_expr(expr);
474     }
475
476     fn process_struct(&mut self,
477                       item: &ast::Item,
478                       def: &ast::StructDef,
479                       ty_params: &ast::Generics) {
480         let qualname = format!("::{}", self.tcx.map.path_to_string(item.id));
481
482         let ctor_id = match def.ctor_id {
483             Some(node_id) => node_id,
484             None => ast::DUMMY_NODE_ID,
485         };
486         let val = self.span.snippet(item.span);
487         let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Struct);
488         self.fmt.struct_str(item.span,
489                             sub_span,
490                             item.id,
491                             ctor_id,
492                             &qualname,
493                             self.cur_scope,
494                             &val);
495
496         // fields
497         for field in &def.fields {
498             self.process_struct_field_def(field, item.id);
499             self.visit_ty(&field.node.ty);
500         }
501
502         self.process_generic_params(ty_params, item.span, &qualname, item.id);
503     }
504
505     fn process_enum(&mut self,
506                     item: &ast::Item,
507                     enum_definition: &ast::EnumDef,
508                     ty_params: &ast::Generics) {
509         let enum_data = self.save_ctxt.get_item_data(item);
510         down_cast_data!(enum_data, EnumData, self, item.span);
511         self.fmt.enum_str(item.span,
512                           Some(enum_data.span),
513                           enum_data.id,
514                           &enum_data.qualname,
515                           enum_data.scope,
516                           &enum_data.value);
517
518         for variant in &enum_definition.variants {
519             let name = &variant.node.name.name.as_str();
520             let mut qualname = enum_data.qualname.clone();
521             qualname.push_str("::");
522             qualname.push_str(name);
523             let val = self.span.snippet(variant.span);
524             match variant.node.kind {
525                 ast::TupleVariantKind(ref args) => {
526                     // first ident in span is the variant's name
527                     self.fmt.tuple_variant_str(variant.span,
528                                                self.span.span_for_first_ident(variant.span),
529                                                variant.node.id,
530                                                name,
531                                                &qualname,
532                                                &enum_data.qualname,
533                                                &val,
534                                                enum_data.id);
535                     for arg in args {
536                         self.visit_ty(&*arg.ty);
537                     }
538                 }
539                 ast::StructVariantKind(ref struct_def) => {
540                     let ctor_id = match struct_def.ctor_id {
541                         Some(node_id) => node_id,
542                         None => ast::DUMMY_NODE_ID,
543                     };
544                     self.fmt.struct_variant_str(variant.span,
545                                                 self.span.span_for_first_ident(variant.span),
546                                                 variant.node.id,
547                                                 ctor_id,
548                                                 &qualname,
549                                                 &enum_data.qualname,
550                                                 &val,
551                                                 enum_data.id);
552
553                     for field in &struct_def.fields {
554                         self.process_struct_field_def(field, variant.node.id);
555                         self.visit_ty(&*field.node.ty);
556                     }
557                 }
558             }
559         }
560         self.process_generic_params(ty_params, item.span, &enum_data.qualname, enum_data.id);
561     }
562
563     fn process_impl(&mut self,
564                     item: &ast::Item,
565                     type_parameters: &ast::Generics,
566                     trait_ref: &Option<ast::TraitRef>,
567                     typ: &ast::Ty,
568                     impl_items: &[P<ast::ImplItem>]) {
569         let impl_data = self.save_ctxt.get_item_data(item);
570         down_cast_data!(impl_data, ImplData, self, item.span);
571         match impl_data.self_ref {
572             Some(ref self_ref) => {
573                 self.fmt.ref_str(recorder::TypeRef,
574                                  item.span,
575                                  Some(self_ref.span),
576                                  self_ref.ref_id,
577                                  self_ref.scope);
578             }
579             None => {
580                 self.visit_ty(&typ);
581             }
582         }
583         if let Some(ref trait_ref_data) = impl_data.trait_ref {
584             self.fmt.ref_str(recorder::TypeRef,
585                              item.span,
586                              Some(trait_ref_data.span),
587                              trait_ref_data.ref_id,
588                              trait_ref_data.scope);
589             visit::walk_path(self, &trait_ref.as_ref().unwrap().path);
590         }
591
592         self.fmt.impl_str(item.span,
593                           Some(impl_data.span),
594                           impl_data.id,
595                           impl_data.self_ref.map(|data| data.ref_id),
596                           impl_data.trait_ref.map(|data| data.ref_id),
597                           impl_data.scope);
598
599         self.process_generic_params(type_parameters, item.span, "", item.id);
600         for impl_item in impl_items {
601             self.visit_impl_item(impl_item);
602         }
603     }
604
605     fn process_trait(&mut self,
606                      item: &ast::Item,
607                      generics: &ast::Generics,
608                      trait_refs: &OwnedSlice<ast::TyParamBound>,
609                      methods: &[P<ast::TraitItem>]) {
610         let qualname = format!("::{}", self.tcx.map.path_to_string(item.id));
611         let val = self.span.snippet(item.span);
612         let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Trait);
613         self.fmt.trait_str(item.span,
614                            sub_span,
615                            item.id,
616                            &qualname,
617                            self.cur_scope,
618                            &val);
619
620         // super-traits
621         for super_bound in trait_refs.iter() {
622             let trait_ref = match *super_bound {
623                 ast::TraitTyParamBound(ref trait_ref, _) => {
624                     trait_ref
625                 }
626                 ast::RegionTyParamBound(..) => {
627                     continue;
628                 }
629             };
630
631             let trait_ref = &trait_ref.trait_ref;
632             match self.lookup_type_ref(trait_ref.ref_id) {
633                 Some(id) => {
634                     let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span);
635                     self.fmt.ref_str(recorder::TypeRef,
636                                      trait_ref.path.span,
637                                      sub_span,
638                                      id,
639                                      self.cur_scope);
640                     self.fmt.inherit_str(trait_ref.path.span,
641                                          sub_span,
642                                          id,
643                                          item.id);
644                 },
645                 None => ()
646             }
647         }
648
649         // walk generics and methods
650         self.process_generic_params(generics, item.span, &qualname, item.id);
651         for method in methods {
652             self.visit_trait_item(method)
653         }
654     }
655
656     fn process_mod(&mut self,
657                    item: &ast::Item) {  // The module in question, represented as an item.
658         let mod_data = self.save_ctxt.get_item_data(item);
659         down_cast_data!(mod_data, ModData, self, item.span);
660         self.fmt.mod_str(item.span,
661                          Some(mod_data.span),
662                          mod_data.id,
663                          &mod_data.qualname,
664                          mod_data.scope,
665                          &mod_data.filename);
666     }
667
668     fn process_path(&mut self,
669                     id: NodeId,
670                     path: &ast::Path,
671                     ref_kind: Option<recorder::Row>) {
672         if generated_code(path.span) {
673             return;
674         }
675
676         let path_data = self.save_ctxt.get_path_data(id, path);
677         let path_data = match path_data {
678             Some(pd) => pd,
679             None => {
680                 self.tcx.sess.span_bug(path.span,
681                                        &format!("Unexpected def kind while looking \
682                                                  up path in `{}`",
683                                                 self.span.snippet(path.span)))
684             }
685         };
686         match path_data {
687             Data::VariableRefData(ref vrd) => {
688                 self.fmt.ref_str(ref_kind.unwrap_or(recorder::VarRef),
689                                                     path.span,
690                                                     Some(vrd.span),
691                                                     vrd.ref_id,
692                                                     vrd.scope);
693
694             }
695             Data::TypeRefData(ref trd) => {
696                 self.fmt.ref_str(recorder::TypeRef,
697                                  path.span,
698                                  Some(trd.span),
699                                  trd.ref_id,
700                                  trd.scope);
701             }
702             Data::MethodCallData(ref mcd) => {
703                 self.fmt.meth_call_str(path.span,
704                                        Some(mcd.span),
705                                        mcd.ref_id,
706                                        mcd.decl_id,
707                                        mcd.scope);
708             }
709             Data::FunctionCallData(fcd) => {
710                 self.fmt.fn_call_str(path.span,
711                                      Some(fcd.span),
712                                      fcd.ref_id,
713                                      fcd.scope);
714             }
715             _ => {
716                 self.sess.span_bug(path.span,
717                                    &format!("Unexpected data: {:?}", path_data));
718             }
719         }
720
721         // Modules or types in the path prefix.
722         let def_map = self.tcx.def_map.borrow();
723         let def = def_map.get(&id).unwrap().full_def();
724         match def {
725             def::DefMethod(did) => {
726                 let ti = self.tcx.impl_or_trait_item(did);
727                 if let ty::MethodTraitItem(m) = ti {
728                     if m.explicit_self == ty::StaticExplicitSelfCategory {
729                         self.write_sub_path_trait_truncated(path);
730                     }
731                 }
732             }
733             def::DefLocal(_) |
734             def::DefStatic(_,_) |
735             def::DefConst(..) |
736             def::DefAssociatedConst(..) |
737             def::DefStruct(_) |
738             def::DefVariant(..) |
739             def::DefFn(..) => self.write_sub_paths_truncated(path, false),
740             _ => {},
741         }
742     }
743
744     fn process_struct_lit(&mut self,
745                           ex: &ast::Expr,
746                           path: &ast::Path,
747                           fields: &Vec<ast::Field>,
748                           variant: ty::VariantDef,
749                           base: &Option<P<ast::Expr>>) {
750         if generated_code(path.span) {
751             return
752         }
753
754         self.write_sub_paths_truncated(path, false);
755
756         if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) {
757             down_cast_data!(struct_lit_data, TypeRefData, self, ex.span);
758             self.fmt.ref_str(recorder::TypeRef,
759                              ex.span,
760                              Some(struct_lit_data.span),
761                              struct_lit_data.ref_id,
762                              struct_lit_data.scope);
763             let scope = self.save_ctxt.enclosing_scope(ex.id);
764
765             for field in fields {
766                 if generated_code(field.ident.span) {
767                     continue;
768                 }
769
770                 let field_data = self.save_ctxt.get_field_ref_data(field,
771                                                                    variant,
772                                                                    scope);
773                 self.fmt.ref_str(recorder::VarRef,
774                                  field.ident.span,
775                                  Some(field_data.span),
776                                  field_data.ref_id,
777                                  field_data.scope);
778
779                 self.visit_expr(&field.expr)
780             }
781         }
782
783         visit::walk_expr_opt(self, base)
784     }
785
786     fn process_method_call(&mut self,
787                            ex: &ast::Expr,
788                            args: &Vec<P<ast::Expr>>) {
789         if let Some(call_data) = self.save_ctxt.get_expr_data(ex) {
790             down_cast_data!(call_data, MethodCallData, self, ex.span);
791             self.fmt.meth_call_str(ex.span,
792                                    Some(call_data.span),
793                                    call_data.ref_id,
794                                    call_data.decl_id,
795                                    call_data.scope);
796         }
797
798         // walk receiver and args
799         visit::walk_exprs(self, &args);
800     }
801
802     fn process_pat(&mut self, p:&ast::Pat) {
803         if generated_code(p.span) {
804             return;
805         }
806
807         match p.node {
808             ast::PatStruct(ref path, ref fields, _) => {
809                 visit::walk_path(self, path);
810                 let adt = self.tcx.node_id_to_type(p.id).ty_adt_def().unwrap();
811                 let def = self.tcx.def_map.borrow()[&p.id].full_def();
812                 let variant = adt.variant_of_def(def);
813
814                 for &Spanned { node: ref field, span } in fields {
815                     if generated_code(span) {
816                         continue;
817                     }
818
819                     let sub_span = self.span.span_for_first_ident(span);
820                     if let Some(f) = variant.find_field_named(field.ident.name) {
821                         self.fmt.ref_str(recorder::VarRef,
822                                          span,
823                                          sub_span,
824                                          f.did,
825                                          self.cur_scope);
826                     }
827                     self.visit_pat(&field.pat);
828                 }
829             }
830             _ => visit::walk_pat(self, p)
831         }
832     }
833 }
834
835 impl<'l, 'tcx, 'v> Visitor<'v> for DumpCsvVisitor<'l, 'tcx> {
836     fn visit_item(&mut self, item: &ast::Item) {
837         if generated_code(item.span) {
838             return
839         }
840
841         match item.node {
842             ast::ItemUse(ref use_item) => {
843                 match use_item.node {
844                     ast::ViewPathSimple(ident, ref path) => {
845                         let sub_span = self.span.span_for_last_ident(path.span);
846                         let mod_id = match self.lookup_type_ref(item.id) {
847                             Some(def_id) => {
848                                 match self.lookup_def_kind(item.id, path.span) {
849                                     Some(kind) => self.fmt.ref_str(kind,
850                                                                    path.span,
851                                                                    sub_span,
852                                                                    def_id,
853                                                                    self.cur_scope),
854                                     None => {},
855                                 }
856                                 Some(def_id)
857                             },
858                             None => None,
859                         };
860
861                         // 'use' always introduces an alias, if there is not an explicit
862                         // one, there is an implicit one.
863                         let sub_span =
864                             match self.span.sub_span_after_keyword(use_item.span, keywords::As) {
865                                 Some(sub_span) => Some(sub_span),
866                                 None => sub_span,
867                             };
868
869                         self.fmt.use_alias_str(path.span,
870                                                sub_span,
871                                                item.id,
872                                                mod_id,
873                                                &ident.name.as_str(),
874                                                self.cur_scope);
875                         self.write_sub_paths_truncated(path, true);
876                     }
877                     ast::ViewPathGlob(ref path) => {
878                         // Make a comma-separated list of names of imported modules.
879                         let mut name_string = String::new();
880                         let glob_map = &self.analysis.glob_map;
881                         let glob_map = glob_map.as_ref().unwrap();
882                         if glob_map.contains_key(&item.id) {
883                             for n in glob_map.get(&item.id).unwrap() {
884                                 if !name_string.is_empty() {
885                                     name_string.push_str(", ");
886                                 }
887                                 name_string.push_str(&n.as_str());
888                             }
889                         }
890
891                         let sub_span = self.span.sub_span_of_token(path.span,
892                                                                    token::BinOp(token::Star));
893                         self.fmt.use_glob_str(path.span,
894                                               sub_span,
895                                               item.id,
896                                               &name_string,
897                                               self.cur_scope);
898                         self.write_sub_paths(path, true);
899                     }
900                     ast::ViewPathList(ref path, ref list) => {
901                         for plid in list {
902                             match plid.node {
903                                 ast::PathListIdent { id, .. } => {
904                                     match self.lookup_type_ref(id) {
905                                         Some(def_id) =>
906                                             match self.lookup_def_kind(id, plid.span) {
907                                                 Some(kind) => {
908                                                     self.fmt.ref_str(
909                                                         kind, plid.span,
910                                                         Some(plid.span),
911                                                         def_id, self.cur_scope);
912                                                 }
913                                                 None => ()
914                                             },
915                                         None => ()
916                                     }
917                                 },
918                                 ast::PathListMod { .. } => ()
919                             }
920                         }
921
922                         self.write_sub_paths(path, true);
923                     }
924                 }
925             }
926             ast::ItemExternCrate(ref s) => {
927                 let location = match *s {
928                     Some(s) => s.to_string(),
929                     None => item.ident.to_string(),
930                 };
931                 let alias_span = self.span.span_for_last_ident(item.span);
932                 let cnum = match self.sess.cstore.find_extern_mod_stmt_cnum(item.id) {
933                     Some(cnum) => cnum,
934                     None => 0,
935                 };
936                 self.fmt.extern_crate_str(item.span,
937                                           alias_span,
938                                           item.id,
939                                           cnum,
940                                           &item.ident.name.as_str(),
941                                           &location,
942                                           self.cur_scope);
943             }
944             ast::ItemFn(ref decl, _, _, _, ref ty_params, ref body) =>
945                 self.process_fn(item, &**decl, ty_params, &**body),
946             ast::ItemStatic(ref typ, _, ref expr) =>
947                 self.process_static_or_const_item(item, typ, expr),
948             ast::ItemConst(ref typ, ref expr) =>
949                 self.process_static_or_const_item(item, &typ, &expr),
950             ast::ItemStruct(ref def, ref ty_params) => self.process_struct(item, &**def, ty_params),
951             ast::ItemEnum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
952             ast::ItemImpl(_, _,
953                           ref ty_params,
954                           ref trait_ref,
955                           ref typ,
956                           ref impl_items) => {
957                 self.process_impl(item,
958                                   ty_params,
959                                   trait_ref,
960                                   &typ,
961                                   impl_items)
962             }
963             ast::ItemTrait(_, ref generics, ref trait_refs, ref methods) =>
964                 self.process_trait(item, generics, trait_refs, methods),
965             ast::ItemMod(ref m) => {
966                 self.process_mod(item);
967                 self.nest(item.id, |v| visit::walk_mod(v, m));
968             }
969             ast::ItemTy(ref ty, ref ty_params) => {
970                 let qualname = format!("::{}", self.tcx.map.path_to_string(item.id));
971                 let value = ty_to_string(&**ty);
972                 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Type);
973                 self.fmt.typedef_str(item.span,
974                                      sub_span,
975                                      item.id,
976                                      &qualname,
977                                      &value);
978
979                 self.visit_ty(&**ty);
980                 self.process_generic_params(ty_params, item.span, &qualname, item.id);
981             },
982             ast::ItemMac(_) => (),
983             _ => visit::walk_item(self, item),
984         }
985     }
986
987     fn visit_generics(&mut self, generics: &ast::Generics) {
988         for param in generics.ty_params.iter() {
989             for bound in param.bounds.iter() {
990                 if let ast::TraitTyParamBound(ref trait_ref, _) = *bound {
991                     self.process_trait_ref(&trait_ref.trait_ref);
992                 }
993             }
994             if let Some(ref ty) = param.default {
995                 self.visit_ty(&**ty);
996             }
997         }
998     }
999
1000     fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
1001         match trait_item.node {
1002             ast::ConstTraitItem(ref ty, Some(ref expr)) => {
1003                 self.process_const(trait_item.id, &trait_item.ident,
1004                                    trait_item.span, &*ty, &*expr);
1005             }
1006             ast::MethodTraitItem(ref sig, ref body) => {
1007                 self.process_method(sig,
1008                                     body.as_ref().map(|x| &**x),
1009                                     trait_item.id,
1010                                     trait_item.ident.name,
1011                                     trait_item.span);
1012             }
1013             ast::ConstTraitItem(_, None) |
1014             ast::TypeTraitItem(..) => {}
1015         }
1016     }
1017
1018     fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
1019         match impl_item.node {
1020             ast::ConstImplItem(ref ty, ref expr) => {
1021                 self.process_const(impl_item.id, &impl_item.ident,
1022                                    impl_item.span, &ty, &expr);
1023             }
1024             ast::MethodImplItem(ref sig, ref body) => {
1025                 self.process_method(sig,
1026                                     Some(body),
1027                                     impl_item.id,
1028                                     impl_item.ident.name,
1029                                     impl_item.span);
1030             }
1031             ast::TypeImplItem(_) |
1032             ast::MacImplItem(_) => {}
1033         }
1034     }
1035
1036     fn visit_ty(&mut self, t: &ast::Ty) {
1037         if generated_code(t.span) {
1038             return
1039         }
1040
1041         match t.node {
1042             ast::TyPath(_, ref path) => {
1043                 match self.lookup_type_ref(t.id) {
1044                     Some(id) => {
1045                         let sub_span = self.span.sub_span_for_type_name(t.span);
1046                         self.fmt.ref_str(recorder::TypeRef,
1047                                          t.span,
1048                                          sub_span,
1049                                          id,
1050                                          self.cur_scope);
1051                     },
1052                     None => ()
1053                 }
1054
1055                 self.write_sub_paths_truncated(path, false);
1056
1057                 visit::walk_path(self, path);
1058             },
1059             _ => visit::walk_ty(self, t),
1060         }
1061     }
1062
1063     fn visit_expr(&mut self, ex: &ast::Expr) {
1064         if generated_code(ex.span) {
1065             return
1066         }
1067
1068         match ex.node {
1069             ast::ExprCall(ref _f, ref _args) => {
1070                 // Don't need to do anything for function calls,
1071                 // because just walking the callee path does what we want.
1072                 visit::walk_expr(self, ex);
1073             }
1074             ast::ExprPath(_, ref path) => {
1075                 self.process_path(ex.id, path, None);
1076                 visit::walk_expr(self, ex);
1077             }
1078             ast::ExprStruct(ref path, ref fields, ref base) => {
1079                 let hir_expr = lower_expr(ex);
1080                 let adt = self.tcx.expr_ty(&hir_expr).ty_adt_def().unwrap();
1081                 let def = self.tcx.resolve_expr(&hir_expr);
1082                 self.process_struct_lit(ex,
1083                                         path,
1084                                         fields,
1085                                         adt.variant_of_def(def),
1086                                         base)
1087             }
1088             ast::ExprMethodCall(_, _, ref args) => self.process_method_call(ex, args),
1089             ast::ExprField(ref sub_ex, _) => {
1090                 if generated_code(sub_ex.span) {
1091                     return
1092                 }
1093
1094                 self.visit_expr(&sub_ex);
1095
1096                 if let Some(field_data) = self.save_ctxt.get_expr_data(ex) {
1097                     down_cast_data!(field_data, VariableRefData, self, ex.span);
1098                     self.fmt.ref_str(recorder::VarRef,
1099                                      ex.span,
1100                                      Some(field_data.span),
1101                                      field_data.ref_id,
1102                                      field_data.scope);
1103                 }
1104             },
1105             ast::ExprTupField(ref sub_ex, idx) => {
1106                 if generated_code(sub_ex.span) {
1107                     return
1108                 }
1109
1110                 self.visit_expr(&**sub_ex);
1111
1112                 let hir_node = self.tcx.map.expect_expr(sub_ex.id);
1113                 let ty = &self.tcx.expr_ty_adjusted(&hir_node).sty;
1114                 match *ty {
1115                     ty::TyStruct(def, _) => {
1116                         let sub_span = self.span.sub_span_after_token(ex.span, token::Dot);
1117                         self.fmt.ref_str(recorder::VarRef,
1118                                          ex.span,
1119                                          sub_span,
1120                                          def.struct_variant().fields[idx.node].did,
1121                                          self.cur_scope);
1122                     }
1123                     ty::TyTuple(_) => {}
1124                     _ => self.sess.span_bug(ex.span,
1125                                             &format!("Expected struct or tuple \
1126                                                       type, found {:?}", ty)),
1127                 }
1128             },
1129             ast::ExprClosure(_, ref decl, ref body) => {
1130                 if generated_code(body.span) {
1131                     return
1132                 }
1133
1134                 let mut id = String::from("$");
1135                 id.push_str(&ex.id.to_string());
1136                 self.process_formals(&decl.inputs, &id);
1137
1138                 // walk arg and return types
1139                 for arg in &decl.inputs {
1140                     self.visit_ty(&*arg.ty);
1141                 }
1142
1143                 if let ast::Return(ref ret_ty) = decl.output {
1144                     self.visit_ty(&**ret_ty);
1145                 }
1146
1147                 // walk the body
1148                 self.nest(ex.id, |v| v.visit_block(&**body));
1149             },
1150             _ => {
1151                 visit::walk_expr(self, ex)
1152             }
1153         }
1154     }
1155
1156     fn visit_mac(&mut self, _: &ast::Mac) {
1157         // Just stop, macros are poison to us.
1158     }
1159
1160     fn visit_pat(&mut self, p: &ast::Pat) {
1161         self.process_pat(p);
1162     }
1163
1164     fn visit_arm(&mut self, arm: &ast::Arm) {
1165         let mut collector = PathCollector::new();
1166         for pattern in &arm.pats {
1167             // collect paths from the arm's patterns
1168             collector.visit_pat(&pattern);
1169             self.visit_pat(&pattern);
1170         }
1171
1172         // This is to get around borrow checking, because we need mut self to call process_path.
1173         let mut paths_to_process = vec![];
1174
1175         // process collected paths
1176         for &(id, ref p, immut, ref_kind) in &collector.collected_paths {
1177             let def_map = self.tcx.def_map.borrow();
1178             if !def_map.contains_key(&id) {
1179                 self.sess.span_bug(p.span,
1180                                    &format!("def_map has no key for {} in visit_arm",
1181                                            id));
1182             }
1183             let def = def_map.get(&id).unwrap().full_def();
1184             match def {
1185                 def::DefLocal(id)  => {
1186                     let value = if immut == ast::MutImmutable {
1187                         self.span.snippet(p.span).to_string()
1188                     } else {
1189                         "<mutable>".to_string()
1190                     };
1191
1192                     assert!(p.segments.len() == 1, "qualified path for local variable def in arm");
1193                     self.fmt.variable_str(p.span,
1194                                           Some(p.span),
1195                                           id,
1196                                           &path_to_string(p),
1197                                           &value,
1198                                           "")
1199                 }
1200                 def::DefVariant(..) | def::DefTy(..) | def::DefStruct(..) => {
1201                     paths_to_process.push((id, p.clone(), Some(ref_kind)))
1202                 }
1203                 // FIXME(nrc) what are these doing here?
1204                 def::DefStatic(_, _) |
1205                 def::DefConst(..) |
1206                 def::DefAssociatedConst(..) => {}
1207                 _ => error!("unexpected definition kind when processing collected paths: {:?}",
1208                             def)
1209             }
1210         }
1211
1212         for &(id, ref path, ref_kind) in &paths_to_process {
1213             self.process_path(id, path, ref_kind);
1214         }
1215         visit::walk_expr_opt(self, &arm.guard);
1216         self.visit_expr(&arm.body);
1217     }
1218
1219     fn visit_stmt(&mut self, s: &ast::Stmt) {
1220         if generated_code(s.span) {
1221             return
1222         }
1223
1224         visit::walk_stmt(self, s)
1225     }
1226
1227     fn visit_local(&mut self, l: &ast::Local) {
1228         if generated_code(l.span) {
1229             return
1230         }
1231
1232         // The local could declare multiple new vars, we must walk the
1233         // pattern and collect them all.
1234         let mut collector = PathCollector::new();
1235         collector.visit_pat(&l.pat);
1236         self.visit_pat(&l.pat);
1237
1238         let value = self.span.snippet(l.span);
1239
1240         for &(id, ref p, immut, _) in &collector.collected_paths {
1241             let value = if immut == ast::MutImmutable {
1242                 value.to_string()
1243             } else {
1244                 "<mutable>".to_string()
1245             };
1246             let types = self.tcx.node_types();
1247             let typ = types.get(&id).unwrap().to_string();
1248             // Get the span only for the name of the variable (I hope the path
1249             // is only ever a variable name, but who knows?).
1250             let sub_span = self.span.span_for_last_ident(p.span);
1251             // Rust uses the id of the pattern for var lookups, so we'll use it too.
1252             self.fmt.variable_str(p.span,
1253                                   sub_span,
1254                                   id,
1255                                   &path_to_string(p),
1256                                   &value,
1257                                   &typ);
1258         }
1259
1260         // Just walk the initialiser and type (don't want to walk the pattern again).
1261         visit::walk_ty_opt(self, &l.ty);
1262         visit::walk_expr_opt(self, &l.init);
1263     }
1264 }