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