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