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