]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/save/mod.rs
7758039e40a418ddbf2a522ee0c55ad68f53f75a
[rust.git] / src / librustc_trans / save / mod.rs
1 // Copyright 2012-2014 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 //! DxrVisitor walks the AST and processes it.
29
30 use session::Session;
31
32 use middle::def;
33 use middle::ty::{self, Ty};
34
35 use std::cell::Cell;
36 use std::old_io::{self, File, fs};
37 use std::env;
38
39 use syntax::ast_util::{self, PostExpansionMethod};
40 use syntax::ast::{self, NodeId, DefId};
41 use syntax::ast_map::NodeItem;
42 use syntax::attr;
43 use syntax::codemap::*;
44 use syntax::parse::token::{self, get_ident, 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 self::span_utils::SpanUtils;
51 use self::recorder::{Recorder, FmtStrs};
52
53 use util::ppaux;
54
55 mod span_utils;
56 mod recorder;
57
58 // Helper function to escape quotes in a string
59 fn escape(s: String) -> String {
60     s.replace("\"", "\"\"")
61 }
62
63 // If the expression is a macro expansion or other generated code, run screaming and don't index.
64 fn generated_code(span: Span) -> bool {
65     span.expn_id != NO_EXPANSION || span  == DUMMY_SP
66 }
67
68 struct DxrVisitor<'l, 'tcx: 'l> {
69     sess: &'l Session,
70     analysis: &'l ty::CrateAnalysis<'tcx>,
71
72     collected_paths: Vec<(NodeId, ast::Path, bool, recorder::Row)>,
73     collecting: bool,
74
75     span: SpanUtils<'l>,
76     fmt: FmtStrs<'l>,
77
78     cur_scope: NodeId
79 }
80
81 impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
82     fn nest<F>(&mut self, scope_id: NodeId, f: F) where
83         F: FnOnce(&mut DxrVisitor<'l, 'tcx>),
84     {
85         let parent_scope = self.cur_scope;
86         self.cur_scope = scope_id;
87         f(self);
88         self.cur_scope = parent_scope;
89     }
90
91     fn dump_crate_info(&mut self, name: &str, krate: &ast::Crate) {
92         // the current crate
93         self.fmt.crate_str(krate.span, name);
94
95         // dump info about all the external crates referenced from this crate
96         self.sess.cstore.iter_crate_data(|n, cmd| {
97             self.fmt.external_crate_str(krate.span, &cmd.name[], n);
98         });
99         self.fmt.recorder.record("end_external_crates\n");
100     }
101
102     // Return all non-empty prefixes of a path.
103     // For each prefix, we return the span for the last segment in the prefix and
104     // a str representation of the entire prefix.
105     fn process_path_prefixes(&self, path: &ast::Path) -> Vec<(Span, String)> {
106         let spans = self.span.spans_for_path_segments(path);
107
108         // Paths to enums seem to not match their spans - the span includes all the
109         // variants too. But they seem to always be at the end, so I hope we can cope with
110         // always using the first ones. So, only error out if we don't have enough spans.
111         // What could go wrong...?
112         if spans.len() < path.segments.len() {
113             error!("Mis-calculated spans for path '{}'. \
114                     Found {} spans, expected {}. Found spans:",
115                    path_to_string(path), spans.len(), path.segments.len());
116             for s in &spans {
117                 let loc = self.sess.codemap().lookup_char_pos(s.lo);
118                 error!("    '{}' in {}, line {}",
119                        self.span.snippet(*s), loc.file.name, loc.line);
120             }
121             return vec!();
122         }
123
124         let mut result: Vec<(Span, String)> = vec!();
125
126         let mut segs = vec!();
127         for (i, (seg, span)) in path.segments.iter().zip(spans.iter()).enumerate() {
128             segs.push(seg.clone());
129             let sub_path = ast::Path{span: *span, // span for the last segment
130                                      global: path.global,
131                                      segments: segs};
132             let qualname = if i == 0 && path.global {
133                 format!("::{}", path_to_string(&sub_path))
134             } else {
135                 path_to_string(&sub_path)
136             };
137             result.push((*span, qualname));
138             segs = sub_path.segments;
139         }
140
141         result
142     }
143
144     // The global arg allows us to override the global-ness of the path (which
145     // actually means 'does the path start with `::`', rather than 'is the path
146     // semantically global). We use the override for `use` imports (etc.) where
147     // the syntax is non-global, but the semantics are global.
148     fn write_sub_paths(&mut self, path: &ast::Path, global: bool) {
149         let sub_paths = self.process_path_prefixes(path);
150         for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
151             let qualname = if i == 0 && global && !path.global {
152                 format!("::{}", qualname)
153             } else {
154                 qualname.clone()
155             };
156             self.fmt.sub_mod_ref_str(path.span,
157                                      *span,
158                                      &qualname[],
159                                      self.cur_scope);
160         }
161     }
162
163     // As write_sub_paths, but does not process the last ident in the path (assuming it
164     // will be processed elsewhere). See note on write_sub_paths about global.
165     fn write_sub_paths_truncated(&mut self, path: &ast::Path, global: bool) {
166         let sub_paths = self.process_path_prefixes(path);
167         let len = sub_paths.len();
168         if len <= 1 {
169             return;
170         }
171
172         let sub_paths = &sub_paths[..len-1];
173         for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
174             let qualname = if i == 0 && global && !path.global {
175                 format!("::{}", qualname)
176             } else {
177                 qualname.clone()
178             };
179             self.fmt.sub_mod_ref_str(path.span,
180                                      *span,
181                                      &qualname[],
182                                      self.cur_scope);
183         }
184     }
185
186     // As write_sub_paths, but expects a path of the form module_path::trait::method
187     // Where trait could actually be a struct too.
188     fn write_sub_path_trait_truncated(&mut self, path: &ast::Path) {
189         let sub_paths = self.process_path_prefixes(path);
190         let len = sub_paths.len();
191         if len <= 1 {
192             return;
193         }
194         let sub_paths = &sub_paths[.. (len-1)];
195
196         // write the trait part of the sub-path
197         let (ref span, ref qualname) = sub_paths[len-2];
198         self.fmt.sub_type_ref_str(path.span,
199                                   *span,
200                                   &qualname[]);
201
202         // write the other sub-paths
203         if len <= 2 {
204             return;
205         }
206         let sub_paths = &sub_paths[..len-2];
207         for &(ref span, ref qualname) in sub_paths {
208             self.fmt.sub_mod_ref_str(path.span,
209                                      *span,
210                                      &qualname[],
211                                      self.cur_scope);
212         }
213     }
214
215     // looks up anything, not just a type
216     fn lookup_type_ref(&self, ref_id: NodeId) -> Option<DefId> {
217         if !self.analysis.ty_cx.def_map.borrow().contains_key(&ref_id) {
218             self.sess.bug(&format!("def_map has no key for {} in lookup_type_ref",
219                                   ref_id)[]);
220         }
221         let def = (*self.analysis.ty_cx.def_map.borrow())[ref_id];
222         match def {
223             def::DefPrimTy(_) => None,
224             _ => Some(def.def_id()),
225         }
226     }
227
228     fn lookup_def_kind(&self, ref_id: NodeId, span: Span) -> Option<recorder::Row> {
229         let def_map = self.analysis.ty_cx.def_map.borrow();
230         if !def_map.contains_key(&ref_id) {
231             self.sess.span_bug(span, &format!("def_map has no key for {} in lookup_def_kind",
232                                              ref_id)[]);
233         }
234         let def = (*def_map)[ref_id];
235         match def {
236             def::DefMod(_) |
237             def::DefForeignMod(_) => Some(recorder::ModRef),
238             def::DefStruct(_) => Some(recorder::StructRef),
239             def::DefTy(..) |
240             def::DefAssociatedTy(..) |
241             def::DefAssociatedPath(..) |
242             def::DefTrait(_) => Some(recorder::TypeRef),
243             def::DefStatic(_, _) |
244             def::DefConst(_) |
245             def::DefLocal(_) |
246             def::DefVariant(_, _, _) |
247             def::DefUpvar(..) => Some(recorder::VarRef),
248
249             def::DefFn(..) => Some(recorder::FnRef),
250
251             def::DefSelfTy(_) |
252             def::DefRegion(_) |
253             def::DefTyParamBinder(_) |
254             def::DefLabel(_) |
255             def::DefStaticMethod(..) |
256             def::DefTyParam(..) |
257             def::DefUse(_) |
258             def::DefMethod(..) |
259             def::DefPrimTy(_) => {
260                 self.sess.span_bug(span, &format!("lookup_def_kind for unexpected item: {:?}",
261                                                  def)[]);
262             },
263         }
264     }
265
266     fn process_formals(&mut self, formals: &Vec<ast::Arg>, qualname: &str) {
267         for arg in formals {
268             assert!(self.collected_paths.len() == 0 && !self.collecting);
269             self.collecting = true;
270             self.visit_pat(&*arg.pat);
271             self.collecting = false;
272             let span_utils = self.span.clone();
273             for &(id, ref p, _, _) in &self.collected_paths {
274                 let typ = ppaux::ty_to_string(&self.analysis.ty_cx,
275                     (*self.analysis.ty_cx.node_types.borrow())[id]);
276                 // get the span only for the name of the variable (I hope the path is only ever a
277                 // variable name, but who knows?)
278                 self.fmt.formal_str(p.span,
279                                     span_utils.span_for_last_ident(p.span),
280                                     id,
281                                     qualname,
282                                     &path_to_string(p)[],
283                                     &typ[]);
284             }
285             self.collected_paths.clear();
286         }
287     }
288
289     fn process_method(&mut self, method: &ast::Method) {
290         if generated_code(method.span) {
291             return;
292         }
293
294         let mut scope_id;
295         // The qualname for a method is the trait name or name of the struct in an impl in
296         // which the method is declared in, followed by the method's name.
297         let qualname = match ty::impl_of_method(&self.analysis.ty_cx,
298                                                 ast_util::local_def(method.id)) {
299             Some(impl_id) => match self.analysis.ty_cx.map.get(impl_id.node) {
300                 NodeItem(item) => {
301                     scope_id = item.id;
302                     match item.node {
303                         ast::ItemImpl(_, _, _, _, ref ty, _) => {
304                             let mut result = String::from_str("<");
305                             result.push_str(&ty_to_string(&**ty)[]);
306
307                             match ty::trait_of_item(&self.analysis.ty_cx,
308                                                     ast_util::local_def(method.id)) {
309                                 Some(def_id) => {
310                                     result.push_str(" as ");
311                                     result.push_str(
312                                         ty::item_path_str(&self.analysis.ty_cx, def_id).as_slice());
313                                 },
314                                 None => {}
315                             }
316                             result.push_str(">");
317                             result
318                         }
319                         _ => {
320                             self.sess.span_bug(method.span,
321                                                &format!("Container {} for method {} not an impl?",
322                                                        impl_id.node, method.id)[]);
323                         },
324                     }
325                 },
326                 _ => {
327                     self.sess.span_bug(method.span,
328                                        &format!(
329                                            "Container {} for method {} is not a node item {:?}",
330                                            impl_id.node,
331                                            method.id,
332                                            self.analysis.ty_cx.map.get(impl_id.node))[]);
333                 },
334             },
335             None => match ty::trait_of_item(&self.analysis.ty_cx,
336                                             ast_util::local_def(method.id)) {
337                 Some(def_id) => {
338                     scope_id = def_id.node;
339                     match self.analysis.ty_cx.map.get(def_id.node) {
340                         NodeItem(_) => {
341                             format!("::{}", ty::item_path_str(&self.analysis.ty_cx, def_id))
342                         }
343                         _ => {
344                             self.sess.span_bug(method.span,
345                                                &format!("Could not find container {} for method {}",
346                                                        def_id.node, method.id)[]);
347                         }
348                     }
349                 },
350                 None => {
351                     self.sess.span_bug(method.span,
352                                        &format!("Could not find container for method {}",
353                                                method.id)[]);
354                 },
355             },
356         };
357
358         let qualname = format!("{}::{}", qualname, get_ident(method.pe_ident()).get());
359         let qualname = &qualname[];
360
361         // record the decl for this def (if it has one)
362         let decl_id = ty::trait_item_of_item(&self.analysis.ty_cx,
363                                              ast_util::local_def(method.id))
364             .and_then(|def_id| {
365                 if match def_id {
366                     ty::MethodTraitItemId(def_id) => {
367                         def_id.node != 0 && def_id != ast_util::local_def(method.id)
368                     }
369                     ty::TypeTraitItemId(_) => false,
370                 } {
371                     Some(def_id)
372                 } else {
373                     None
374                 }
375             });
376         let decl_id = match decl_id {
377             None => None,
378             Some(id) => Some(id.def_id()),
379         };
380
381         let sub_span = self.span.sub_span_after_keyword(method.span, keywords::Fn);
382         self.fmt.method_str(method.span,
383                             sub_span,
384                             method.id,
385                             qualname,
386                             decl_id,
387                             scope_id);
388
389         self.process_formals(&method.pe_fn_decl().inputs, qualname);
390
391         // walk arg and return types
392         for arg in &method.pe_fn_decl().inputs {
393             self.visit_ty(&*arg.ty);
394         }
395
396         if let ast::Return(ref ret_ty) = method.pe_fn_decl().output {
397             self.visit_ty(&**ret_ty);
398         }
399
400         // walk the fn body
401         self.nest(method.id, |v| v.visit_block(&*method.pe_body()));
402
403         self.process_generic_params(method.pe_generics(),
404                                     method.span,
405                                     qualname,
406                                     method.id);
407     }
408
409     fn process_trait_ref(&mut self,
410                          trait_ref: &ast::TraitRef) {
411         match self.lookup_type_ref(trait_ref.ref_id) {
412             Some(id) => {
413                 let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span);
414                 self.fmt.ref_str(recorder::TypeRef,
415                                  trait_ref.path.span,
416                                  sub_span,
417                                  id,
418                                  self.cur_scope);
419                 visit::walk_path(self, &trait_ref.path);
420             },
421             None => ()
422         }
423     }
424
425     fn process_struct_field_def(&mut self,
426                                 field: &ast::StructField,
427                                 qualname: &str,
428                                 scope_id: NodeId) {
429         match field.node.kind {
430             ast::NamedField(ident, _) => {
431                 let name = get_ident(ident);
432                 let qualname = format!("{}::{}", qualname, name);
433                 let typ = ppaux::ty_to_string(&self.analysis.ty_cx,
434                     (*self.analysis.ty_cx.node_types.borrow())[field.node.id]);
435                 match self.span.sub_span_before_token(field.span, token::Colon) {
436                     Some(sub_span) => self.fmt.field_str(field.span,
437                                                          Some(sub_span),
438                                                          field.node.id,
439                                                          &name.get()[],
440                                                          &qualname[],
441                                                          &typ[],
442                                                          scope_id),
443                     None => self.sess.span_bug(field.span,
444                                                &format!("Could not find sub-span for field {}",
445                                                        qualname)[]),
446                 }
447             },
448             _ => (),
449         }
450     }
451
452     // Dump generic params bindings, then visit_generics
453     fn process_generic_params(&mut self,
454                               generics:&ast::Generics,
455                               full_span: Span,
456                               prefix: &str,
457                               id: NodeId) {
458         // We can't only use visit_generics since we don't have spans for param
459         // bindings, so we reparse the full_span to get those sub spans.
460         // However full span is the entire enum/fn/struct block, so we only want
461         // the first few to match the number of generics we're looking for.
462         let param_sub_spans = self.span.spans_for_ty_params(full_span,
463                                                            (generics.ty_params.len() as int));
464         for (param, param_ss) in generics.ty_params.iter().zip(param_sub_spans.iter()) {
465             // Append $id to name to make sure each one is unique
466             let name = format!("{}::{}${}",
467                                prefix,
468                                escape(self.span.snippet(*param_ss)),
469                                id);
470             self.fmt.typedef_str(full_span,
471                                  Some(*param_ss),
472                                  param.id,
473                                  &name[],
474                                  "");
475         }
476         self.visit_generics(generics);
477     }
478
479     fn process_fn(&mut self,
480                   item: &ast::Item,
481                   decl: &ast::FnDecl,
482                   ty_params: &ast::Generics,
483                   body: &ast::Block) {
484         let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
485
486         let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Fn);
487         self.fmt.fn_str(item.span,
488                         sub_span,
489                         item.id,
490                         &qualname[],
491                         self.cur_scope);
492
493         self.process_formals(&decl.inputs, &qualname[]);
494
495         // walk arg and return types
496         for arg in &decl.inputs {
497             self.visit_ty(&*arg.ty);
498         }
499
500         if let ast::Return(ref ret_ty) = decl.output {
501             self.visit_ty(&**ret_ty);
502         }
503
504         // walk the body
505         self.nest(item.id, |v| v.visit_block(&*body));
506
507         self.process_generic_params(ty_params, item.span, &qualname[], item.id);
508     }
509
510     fn process_static(&mut self,
511                       item: &ast::Item,
512                       typ: &ast::Ty,
513                       mt: ast::Mutability,
514                       expr: &ast::Expr)
515     {
516         let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
517
518         // If the variable is immutable, save the initialising expression.
519         let value = match mt {
520             ast::MutMutable => String::from_str("<mutable>"),
521             ast::MutImmutable => self.span.snippet(expr.span),
522         };
523
524         let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Static);
525         self.fmt.static_str(item.span,
526                             sub_span,
527                             item.id,
528                             get_ident(item.ident).get(),
529                             &qualname[],
530                             &value[],
531                             &ty_to_string(&*typ)[],
532                             self.cur_scope);
533
534         // walk type and init value
535         self.visit_ty(&*typ);
536         self.visit_expr(expr);
537     }
538
539     fn process_const(&mut self,
540                       item: &ast::Item,
541                       typ: &ast::Ty,
542                       expr: &ast::Expr)
543     {
544         let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
545
546         let sub_span = self.span.sub_span_after_keyword(item.span,
547                                                         keywords::Const);
548         self.fmt.static_str(item.span,
549                             sub_span,
550                             item.id,
551                             get_ident(item.ident).get(),
552                             &qualname[],
553                             "",
554                             &ty_to_string(&*typ)[],
555                             self.cur_scope);
556
557         // walk type and init value
558         self.visit_ty(&*typ);
559         self.visit_expr(expr);
560     }
561
562     fn process_struct(&mut self,
563                       item: &ast::Item,
564                       def: &ast::StructDef,
565                       ty_params: &ast::Generics) {
566         let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
567
568         let ctor_id = match def.ctor_id {
569             Some(node_id) => node_id,
570             None => -1,
571         };
572         let val = self.span.snippet(item.span);
573         let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Struct);
574         self.fmt.struct_str(item.span,
575                             sub_span,
576                             item.id,
577                             ctor_id,
578                             &qualname[],
579                             self.cur_scope,
580                             &val[]);
581
582         // fields
583         for field in &def.fields {
584             self.process_struct_field_def(field, &qualname[], item.id);
585             self.visit_ty(&*field.node.ty);
586         }
587
588         self.process_generic_params(ty_params, item.span, &qualname[], item.id);
589     }
590
591     fn process_enum(&mut self,
592                     item: &ast::Item,
593                     enum_definition: &ast::EnumDef,
594                     ty_params: &ast::Generics) {
595         let enum_name = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
596         let val = self.span.snippet(item.span);
597         match self.span.sub_span_after_keyword(item.span, keywords::Enum) {
598             Some(sub_span) => self.fmt.enum_str(item.span,
599                                                 Some(sub_span),
600                                                 item.id,
601                                                 &enum_name[],
602                                                 self.cur_scope,
603                                                 &val[]),
604             None => self.sess.span_bug(item.span,
605                                        &format!("Could not find subspan for enum {}",
606                                                enum_name)[]),
607         }
608         for variant in &enum_definition.variants {
609             let name = get_ident(variant.node.name);
610             let name = name.get();
611             let mut qualname = enum_name.clone();
612             qualname.push_str("::");
613             qualname.push_str(name);
614             let val = self.span.snippet(variant.span);
615             match variant.node.kind {
616                 ast::TupleVariantKind(ref args) => {
617                     // first ident in span is the variant's name
618                     self.fmt.tuple_variant_str(variant.span,
619                                                self.span.span_for_first_ident(variant.span),
620                                                variant.node.id,
621                                                name,
622                                                &qualname[],
623                                                &enum_name[],
624                                                &val[],
625                                                item.id);
626                     for arg in args {
627                         self.visit_ty(&*arg.ty);
628                     }
629                 }
630                 ast::StructVariantKind(ref struct_def) => {
631                     let ctor_id = match struct_def.ctor_id {
632                         Some(node_id) => node_id,
633                         None => -1,
634                     };
635                     self.fmt.struct_variant_str(
636                         variant.span,
637                         self.span.span_for_first_ident(variant.span),
638                         variant.node.id,
639                         ctor_id,
640                         &qualname[],
641                         &enum_name[],
642                         &val[],
643                         item.id);
644
645                     for field in &struct_def.fields {
646                         self.process_struct_field_def(field, qualname.as_slice(), variant.node.id);
647                         self.visit_ty(&*field.node.ty);
648                     }
649                 }
650             }
651         }
652
653         self.process_generic_params(ty_params, item.span, &enum_name[], item.id);
654     }
655
656     fn process_impl(&mut self,
657                     item: &ast::Item,
658                     type_parameters: &ast::Generics,
659                     trait_ref: &Option<ast::TraitRef>,
660                     typ: &ast::Ty,
661                     impl_items: &Vec<ast::ImplItem>) {
662         let trait_id = trait_ref.as_ref().and_then(|tr| self.lookup_type_ref(tr.ref_id));
663         match typ.node {
664             // Common case impl for a struct or something basic.
665             ast::TyPath(ref path, id) => {
666                 match self.lookup_type_ref(id) {
667                     Some(id) => {
668                         let sub_span = self.span.sub_span_for_type_name(path.span);
669                         self.fmt.ref_str(recorder::TypeRef,
670                                          path.span,
671                                          sub_span,
672                                          id,
673                                          self.cur_scope);
674                         self.fmt.impl_str(path.span,
675                                           sub_span,
676                                           item.id,
677                                           Some(id),
678                                           trait_id,
679                                           self.cur_scope);
680                     },
681                     None => ()
682                 }
683             },
684             _ => {
685                 // Less useful case, impl for a compound type.
686                 self.visit_ty(&*typ);
687
688                 let sub_span = self.span.sub_span_for_type_name(typ.span);
689                 self.fmt.impl_str(typ.span,
690                                   sub_span,
691                                   item.id,
692                                   None,
693                                   trait_id,
694                                   self.cur_scope);
695             }
696         }
697
698         match *trait_ref {
699             Some(ref trait_ref) => self.process_trait_ref(trait_ref),
700             None => (),
701         }
702
703         self.process_generic_params(type_parameters, item.span, "", item.id);
704         for impl_item in impl_items {
705             match *impl_item {
706                 ast::MethodImplItem(ref method) => {
707                     visit::walk_method_helper(self, &**method)
708                 }
709                 ast::TypeImplItem(ref typedef) => {
710                     visit::walk_ty(self, &*typedef.typ)
711                 }
712             }
713         }
714     }
715
716     fn process_trait(&mut self,
717                      item: &ast::Item,
718                      generics: &ast::Generics,
719                      trait_refs: &OwnedSlice<ast::TyParamBound>,
720                      methods: &Vec<ast::TraitItem>) {
721         let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
722         let val = self.span.snippet(item.span);
723         let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Trait);
724         self.fmt.trait_str(item.span,
725                            sub_span,
726                            item.id,
727                            &qualname[],
728                            self.cur_scope,
729                            &val[]);
730
731         // super-traits
732         for super_bound in &**trait_refs {
733             let trait_ref = match *super_bound {
734                 ast::TraitTyParamBound(ref trait_ref, _) => {
735                     trait_ref
736                 }
737                 ast::RegionTyParamBound(..) => {
738                     continue;
739                 }
740             };
741
742             let trait_ref = &trait_ref.trait_ref;
743             match self.lookup_type_ref(trait_ref.ref_id) {
744                 Some(id) => {
745                     let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span);
746                     self.fmt.ref_str(recorder::TypeRef,
747                                      trait_ref.path.span,
748                                      sub_span,
749                                      id,
750                                      self.cur_scope);
751                     self.fmt.inherit_str(trait_ref.path.span,
752                                          sub_span,
753                                          id,
754                                          item.id);
755                 },
756                 None => ()
757             }
758         }
759
760         // walk generics and methods
761         self.process_generic_params(generics, item.span, &qualname[], item.id);
762         for method in methods {
763             self.visit_trait_item(method)
764         }
765     }
766
767     fn process_mod(&mut self,
768                    item: &ast::Item,  // The module in question, represented as an item.
769                    m: &ast::Mod) {
770         let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
771
772         let cm = self.sess.codemap();
773         let filename = cm.span_to_filename(m.inner);
774
775         let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Mod);
776         self.fmt.mod_str(item.span,
777                          sub_span,
778                          item.id,
779                          &qualname[],
780                          self.cur_scope,
781                          &filename[]);
782
783         self.nest(item.id, |v| visit::walk_mod(v, m));
784     }
785
786     fn process_path(&mut self,
787                     id: NodeId,
788                     span: Span,
789                     path: &ast::Path,
790                     ref_kind: Option<recorder::Row>) {
791         if generated_code(span) {
792             return
793         }
794
795         let def_map = self.analysis.ty_cx.def_map.borrow();
796         if !def_map.contains_key(&id) {
797             self.sess.span_bug(span,
798                                format!("def_map has no key for {} in visit_expr", id).as_slice());
799         }
800         let def = &(*def_map)[id];
801         let sub_span = self.span.span_for_last_ident(span);
802         match *def {
803             def::DefUpvar(..) |
804             def::DefLocal(..) |
805             def::DefStatic(..) |
806             def::DefConst(..) |
807             def::DefVariant(..) => self.fmt.ref_str(ref_kind.unwrap_or(recorder::VarRef),
808                                                     span,
809                                                     sub_span,
810                                                     def.def_id(),
811                                                     self.cur_scope),
812             def::DefStruct(def_id) => self.fmt.ref_str(recorder::StructRef,
813                                                        span,
814                                                        sub_span,
815                                                        def_id,
816                                                        self.cur_scope),
817             def::DefTy(def_id, _) => self.fmt.ref_str(recorder::TypeRef,
818                                                       span,
819                                                       sub_span,
820                                                       def_id,
821                                                       self.cur_scope),
822             def::DefStaticMethod(declid, provenence) |
823             def::DefMethod(declid, _, provenence) => {
824                 let sub_span = self.span.sub_span_for_meth_name(span);
825                 let defid = if declid.krate == ast::LOCAL_CRATE {
826                     let ti = ty::impl_or_trait_item(&self.analysis.ty_cx,
827                                                     declid);
828                     match provenence {
829                         def::FromTrait(def_id) => {
830                             Some(ty::trait_items(&self.analysis.ty_cx,
831                                                  def_id)
832                                     .iter()
833                                     .find(|mr| {
834                                         mr.name() == ti.name()
835                                     })
836                                     .unwrap()
837                                     .def_id())
838                         }
839                         def::FromImpl(def_id) => {
840                             let impl_items = self.analysis
841                                                  .ty_cx
842                                                  .impl_items
843                                                  .borrow();
844                             Some((*impl_items)[def_id]
845                                            .iter()
846                                            .find(|mr| {
847                                                 ty::impl_or_trait_item(
848                                                     &self.analysis.ty_cx,
849                                                     mr.def_id()
850                                                 ).name() == ti.name()
851                                             })
852                                            .unwrap()
853                                            .def_id())
854                         }
855                     }
856                 } else {
857                     None
858                 };
859                 self.fmt.meth_call_str(span,
860                                        sub_span,
861                                        defid,
862                                        Some(declid),
863                                        self.cur_scope);
864             },
865             def::DefFn(def_id, _) => {
866                 self.fmt.fn_call_str(span,
867                                      sub_span,
868                                      def_id,
869                                      self.cur_scope)
870             }
871             _ => self.sess.span_bug(span,
872                                     &format!("Unexpected def kind while looking \
873                                               up path in `{}`: `{:?}`",
874                                              self.span.snippet(span),
875                                              *def)[]),
876         }
877         // modules or types in the path prefix
878         match *def {
879             def::DefStaticMethod(..) => self.write_sub_path_trait_truncated(path),
880             def::DefLocal(_) |
881             def::DefStatic(_,_) |
882             def::DefConst(..) |
883             def::DefStruct(_) |
884             def::DefVariant(..) |
885             def::DefFn(..) => self.write_sub_paths_truncated(path, false),
886             _ => {},
887         }
888     }
889
890     fn process_struct_lit(&mut self,
891                           ex: &ast::Expr,
892                           path: &ast::Path,
893                           fields: &Vec<ast::Field>,
894                           base: &Option<P<ast::Expr>>) {
895         if generated_code(path.span) {
896             return
897         }
898
899         self.write_sub_paths_truncated(path, false);
900
901         let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, ex).sty;
902         let struct_def = match *ty {
903             ty::ty_struct(def_id, _) => {
904                 let sub_span = self.span.span_for_last_ident(path.span);
905                 self.fmt.ref_str(recorder::StructRef,
906                                  path.span,
907                                  sub_span,
908                                  def_id,
909                                  self.cur_scope);
910                 Some(def_id)
911             }
912             _ => None
913         };
914
915         for field in fields {
916             match struct_def {
917                 Some(struct_def) => {
918                     let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, struct_def);
919                     for f in &fields {
920                         if generated_code(field.ident.span) {
921                             continue;
922                         }
923                         if f.name == field.ident.node.name {
924                             // We don't really need a sub-span here, but no harm done
925                             let sub_span = self.span.span_for_last_ident(field.ident.span);
926                             self.fmt.ref_str(recorder::VarRef,
927                                              field.ident.span,
928                                              sub_span,
929                                              f.id,
930                                              self.cur_scope);
931                         }
932                     }
933                 }
934                 None => {}
935             }
936
937             self.visit_expr(&*field.expr)
938         }
939         visit::walk_expr_opt(self, base)
940     }
941
942     fn process_method_call(&mut self,
943                            ex: &ast::Expr,
944                            args: &Vec<P<ast::Expr>>) {
945         let method_map = self.analysis.ty_cx.method_map.borrow();
946         let method_callee = &(*method_map)[ty::MethodCall::expr(ex.id)];
947         let (def_id, decl_id) = match method_callee.origin {
948             ty::MethodStatic(def_id) |
949             ty::MethodStaticClosure(def_id) => {
950                 // method invoked on an object with a concrete type (not a static method)
951                 let decl_id =
952                     match ty::trait_item_of_item(&self.analysis.ty_cx,
953                                                  def_id) {
954                         None => None,
955                         Some(decl_id) => Some(decl_id.def_id()),
956                     };
957
958                 // This incantation is required if the method referenced is a
959                 // trait's default implementation.
960                 let def_id = match ty::impl_or_trait_item(&self.analysis
961                                                                .ty_cx,
962                                                           def_id) {
963                     ty::MethodTraitItem(method) => {
964                         method.provided_source.unwrap_or(def_id)
965                     }
966                     ty::TypeTraitItem(_) => def_id,
967                 };
968                 (Some(def_id), decl_id)
969             }
970             ty::MethodTypeParam(ref mp) => {
971                 // method invoked on a type parameter
972                 let trait_item = ty::trait_item(&self.analysis.ty_cx,
973                                                 mp.trait_ref.def_id,
974                                                 mp.method_num);
975                 (None, Some(trait_item.def_id()))
976             }
977             ty::MethodTraitObject(ref mo) => {
978                 // method invoked on a trait instance
979                 let trait_item = ty::trait_item(&self.analysis.ty_cx,
980                                                 mo.trait_ref.def_id,
981                                                 mo.method_num);
982                 (None, Some(trait_item.def_id()))
983             }
984         };
985         let sub_span = self.span.sub_span_for_meth_name(ex.span);
986         self.fmt.meth_call_str(ex.span,
987                                sub_span,
988                                def_id,
989                                decl_id,
990                                self.cur_scope);
991
992         // walk receiver and args
993         visit::walk_exprs(self, &args[]);
994     }
995
996     fn process_pat(&mut self, p:&ast::Pat) {
997         if generated_code(p.span) {
998             return
999         }
1000
1001         match p.node {
1002             ast::PatStruct(ref path, ref fields, _) => {
1003                 self.collected_paths.push((p.id, path.clone(), false, recorder::StructRef));
1004                 visit::walk_path(self, path);
1005                 let struct_def = match self.lookup_type_ref(p.id) {
1006                     Some(sd) => sd,
1007                     None => {
1008                         self.sess.span_bug(p.span,
1009                                            &format!("Could not find struct_def for `{}`",
1010                                                    self.span.snippet(p.span))[]);
1011                     }
1012                 };
1013                 for &Spanned { node: ref field, span } in fields {
1014                     let sub_span = self.span.span_for_first_ident(span);
1015                     let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, struct_def);
1016                     for f in fields {
1017                         if f.name == field.ident.name {
1018                             self.fmt.ref_str(recorder::VarRef,
1019                                              span,
1020                                              sub_span,
1021                                              f.id,
1022                                              self.cur_scope);
1023                             break;
1024                         }
1025                     }
1026                     self.visit_pat(&*field.pat);
1027                 }
1028             }
1029             ast::PatEnum(ref path, _) => {
1030                 self.collected_paths.push((p.id, path.clone(), false, recorder::VarRef));
1031                 visit::walk_pat(self, p);
1032             }
1033             ast::PatIdent(bm, ref path1, ref optional_subpattern) => {
1034                 let immut = match bm {
1035                     // Even if the ref is mut, you can't change the ref, only
1036                     // the data pointed at, so showing the initialising expression
1037                     // is still worthwhile.
1038                     ast::BindByRef(_) => true,
1039                     ast::BindByValue(mt) => {
1040                         match mt {
1041                             ast::MutMutable => false,
1042                             ast::MutImmutable => true,
1043                         }
1044                     }
1045                 };
1046                 // collect path for either visit_local or visit_arm
1047                 let path = ast_util::ident_to_path(path1.span,path1.node);
1048                 self.collected_paths.push((p.id, path, immut, recorder::VarRef));
1049                 match *optional_subpattern {
1050                     None => {}
1051                     Some(ref subpattern) => self.visit_pat(&**subpattern)
1052                 }
1053             }
1054             _ => visit::walk_pat(self, p)
1055         }
1056     }
1057 }
1058
1059 impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
1060     fn visit_item(&mut self, item: &ast::Item) {
1061         if generated_code(item.span) {
1062             return
1063         }
1064
1065         match item.node {
1066             ast::ItemUse(ref use_item) => {
1067                 match use_item.node {
1068                     ast::ViewPathSimple(ident, ref path) => {
1069                         let sub_span = self.span.span_for_last_ident(path.span);
1070                         let mod_id = match self.lookup_type_ref(item.id) {
1071                             Some(def_id) => {
1072                                 match self.lookup_def_kind(item.id, path.span) {
1073                                     Some(kind) => self.fmt.ref_str(kind,
1074                                                                    path.span,
1075                                                                    sub_span,
1076                                                                    def_id,
1077                                                                    self.cur_scope),
1078                                     None => {},
1079                                 }
1080                                 Some(def_id)
1081                             },
1082                             None => None,
1083                         };
1084
1085                         // 'use' always introduces an alias, if there is not an explicit
1086                         // one, there is an implicit one.
1087                         let sub_span =
1088                             match self.span.sub_span_after_keyword(use_item.span, keywords::As) {
1089                                 Some(sub_span) => Some(sub_span),
1090                                 None => sub_span,
1091                             };
1092
1093                         self.fmt.use_alias_str(path.span,
1094                                                sub_span,
1095                                                item.id,
1096                                                mod_id,
1097                                                get_ident(ident).get(),
1098                                                self.cur_scope);
1099                         self.write_sub_paths_truncated(path, true);
1100                     }
1101                     ast::ViewPathGlob(ref path) => {
1102                         // Make a comma-separated list of names of imported modules.
1103                         let mut name_string = String::new();
1104                         let glob_map = &self.analysis.glob_map;
1105                         let glob_map = glob_map.as_ref().unwrap();
1106                         if glob_map.contains_key(&item.id) {
1107                             for n in &glob_map[item.id] {
1108                                 if name_string.len() > 0 {
1109                                     name_string.push_str(", ");
1110                                 }
1111                                 name_string.push_str(n.as_str());
1112                             }
1113                         }
1114
1115                         let sub_span = self.span.sub_span_of_token(path.span,
1116                                                                    token::BinOp(token::Star));
1117                         self.fmt.use_glob_str(path.span,
1118                                               sub_span,
1119                                               item.id,
1120                                               name_string.as_slice(),
1121                                               self.cur_scope);
1122                         self.write_sub_paths(path, true);
1123                     }
1124                     ast::ViewPathList(ref path, ref list) => {
1125                         for plid in list {
1126                             match plid.node {
1127                                 ast::PathListIdent { id, .. } => {
1128                                     match self.lookup_type_ref(id) {
1129                                         Some(def_id) =>
1130                                             match self.lookup_def_kind(id, plid.span) {
1131                                                 Some(kind) => {
1132                                                     self.fmt.ref_str(
1133                                                         kind, plid.span,
1134                                                         Some(plid.span),
1135                                                         def_id, self.cur_scope);
1136                                                 }
1137                                                 None => ()
1138                                             },
1139                                         None => ()
1140                                     }
1141                                 },
1142                                 ast::PathListMod { .. } => ()
1143                             }
1144                         }
1145
1146                         self.write_sub_paths(path, true);
1147                     }
1148                 }
1149             }
1150             ast::ItemExternCrate(ref s) => {
1151                 let name = get_ident(item.ident);
1152                 let name = name.get();
1153                 let location = match *s {
1154                     Some((ref s, _)) => s.get().to_string(),
1155                     None => name.to_string(),
1156                 };
1157                 let alias_span = self.span.span_for_last_ident(item.span);
1158                 let cnum = match self.sess.cstore.find_extern_mod_stmt_cnum(item.id) {
1159                     Some(cnum) => cnum,
1160                     None => 0,
1161                 };
1162                 self.fmt.extern_crate_str(item.span,
1163                                           alias_span,
1164                                           item.id,
1165                                           cnum,
1166                                           name,
1167                                           &location[],
1168                                           self.cur_scope);
1169             }
1170             ast::ItemFn(ref decl, _, _, ref ty_params, ref body) =>
1171                 self.process_fn(item, &**decl, ty_params, &**body),
1172             ast::ItemStatic(ref typ, mt, ref expr) =>
1173                 self.process_static(item, &**typ, mt, &**expr),
1174             ast::ItemConst(ref typ, ref expr) =>
1175                 self.process_const(item, &**typ, &**expr),
1176             ast::ItemStruct(ref def, ref ty_params) => self.process_struct(item, &**def, ty_params),
1177             ast::ItemEnum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
1178             ast::ItemImpl(_, _,
1179                           ref ty_params,
1180                           ref trait_ref,
1181                           ref typ,
1182                           ref impl_items) => {
1183                 self.process_impl(item,
1184                                   ty_params,
1185                                   trait_ref,
1186                                   &**typ,
1187                                   impl_items)
1188             }
1189             ast::ItemTrait(_, ref generics, ref trait_refs, ref methods) =>
1190                 self.process_trait(item, generics, trait_refs, methods),
1191             ast::ItemMod(ref m) => self.process_mod(item, m),
1192             ast::ItemTy(ref ty, ref ty_params) => {
1193                 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
1194                 let value = ty_to_string(&**ty);
1195                 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Type);
1196                 self.fmt.typedef_str(item.span,
1197                                      sub_span,
1198                                      item.id,
1199                                      &qualname[],
1200                                      &value[]);
1201
1202                 self.visit_ty(&**ty);
1203                 self.process_generic_params(ty_params, item.span, qualname.as_slice(), item.id);
1204             },
1205             ast::ItemMac(_) => (),
1206             _ => visit::walk_item(self, item),
1207         }
1208     }
1209
1210     fn visit_generics(&mut self, generics: &ast::Generics) {
1211         for param in &*generics.ty_params {
1212             for bound in &*param.bounds {
1213                 if let ast::TraitTyParamBound(ref trait_ref, _) = *bound {
1214                     self.process_trait_ref(&trait_ref.trait_ref);
1215                 }
1216             }
1217             if let Some(ref ty) = param.default {
1218                 self.visit_ty(&**ty);
1219             }
1220         }
1221     }
1222
1223     // We don't actually index functions here, that is done in visit_item/ItemFn.
1224     // Here we just visit methods.
1225     fn visit_fn(&mut self,
1226                 fk: visit::FnKind<'v>,
1227                 fd: &'v ast::FnDecl,
1228                 b: &'v ast::Block,
1229                 s: Span,
1230                 _: ast::NodeId) {
1231         if generated_code(s) {
1232             return;
1233         }
1234
1235         match fk {
1236             visit::FkMethod(_, _, method) => self.process_method(method),
1237             _ => visit::walk_fn(self, fk, fd, b, s),
1238         }
1239     }
1240
1241     fn visit_trait_item(&mut self, tm: &ast::TraitItem) {
1242         match *tm {
1243             ast::RequiredMethod(ref method_type) => {
1244                 if generated_code(method_type.span) {
1245                     return;
1246                 }
1247
1248                 let mut scope_id;
1249                 let mut qualname = match ty::trait_of_item(&self.analysis.ty_cx,
1250                                                            ast_util::local_def(method_type.id)) {
1251                     Some(def_id) => {
1252                         scope_id = def_id.node;
1253                         format!("::{}::", ty::item_path_str(&self.analysis.ty_cx, def_id))
1254                     },
1255                     None => {
1256                         self.sess.span_bug(method_type.span,
1257                                            &format!("Could not find trait for method {}",
1258                                                    method_type.id)[]);
1259                     },
1260                 };
1261
1262                 qualname.push_str(get_ident(method_type.ident).get());
1263                 let qualname = &qualname[];
1264
1265                 let sub_span = self.span.sub_span_after_keyword(method_type.span, keywords::Fn);
1266                 self.fmt.method_decl_str(method_type.span,
1267                                          sub_span,
1268                                          method_type.id,
1269                                          qualname,
1270                                          scope_id);
1271
1272                 // walk arg and return types
1273                 for arg in &method_type.decl.inputs {
1274                     self.visit_ty(&*arg.ty);
1275                 }
1276
1277                 if let ast::Return(ref ret_ty) = method_type.decl.output {
1278                     self.visit_ty(&**ret_ty);
1279                 }
1280
1281                 self.process_generic_params(&method_type.generics,
1282                                             method_type.span,
1283                                             qualname,
1284                                             method_type.id);
1285             }
1286             ast::ProvidedMethod(ref method) => self.process_method(&**method),
1287             ast::TypeTraitItem(_) => {}
1288         }
1289     }
1290
1291     fn visit_ty(&mut self, t: &ast::Ty) {
1292         if generated_code(t.span) {
1293             return
1294         }
1295
1296         match t.node {
1297             ast::TyPath(ref path, id) => {
1298                 match self.lookup_type_ref(id) {
1299                     Some(id) => {
1300                         let sub_span = self.span.sub_span_for_type_name(t.span);
1301                         self.fmt.ref_str(recorder::TypeRef,
1302                                          t.span,
1303                                          sub_span,
1304                                          id,
1305                                          self.cur_scope);
1306                     },
1307                     None => ()
1308                 }
1309
1310                 self.write_sub_paths_truncated(path, false);
1311
1312                 visit::walk_path(self, path);
1313             },
1314             _ => visit::walk_ty(self, t),
1315         }
1316     }
1317
1318     fn visit_expr(&mut self, ex: &ast::Expr) {
1319         if generated_code(ex.span) {
1320             return
1321         }
1322
1323         match ex.node {
1324             ast::ExprCall(ref _f, ref _args) => {
1325                 // Don't need to do anything for function calls,
1326                 // because just walking the callee path does what we want.
1327                 visit::walk_expr(self, ex);
1328             },
1329             ast::ExprPath(ref path) => {
1330                 self.process_path(ex.id, path.span, path, None);
1331                 visit::walk_path(self, path);
1332             }
1333             ast::ExprQPath(ref qpath) => {
1334                 let mut path = qpath.trait_ref.path.clone();
1335                 path.segments.push(qpath.item_path.clone());
1336                 self.process_path(ex.id, ex.span, &path, None);
1337                 visit::walk_qpath(self, ex.span, &**qpath);
1338             }
1339             ast::ExprStruct(ref path, ref fields, ref base) =>
1340                 self.process_struct_lit(ex, path, fields, base),
1341             ast::ExprMethodCall(_, _, ref args) => self.process_method_call(ex, args),
1342             ast::ExprField(ref sub_ex, ident) => {
1343                 if generated_code(sub_ex.span) {
1344                     return
1345                 }
1346
1347                 self.visit_expr(&**sub_ex);
1348                 let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty;
1349                 match *ty {
1350                     ty::ty_struct(def_id, _) => {
1351                         let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, def_id);
1352                         for f in &fields {
1353                             if f.name == ident.node.name {
1354                                 let sub_span = self.span.span_for_last_ident(ex.span);
1355                                 self.fmt.ref_str(recorder::VarRef,
1356                                                  ex.span,
1357                                                  sub_span,
1358                                                  f.id,
1359                                                  self.cur_scope);
1360                                 break;
1361                             }
1362                         }
1363                     }
1364                     _ => self.sess.span_bug(ex.span,
1365                                             &format!("Expected struct type, found {:?}", ty)[]),
1366                 }
1367             },
1368             ast::ExprTupField(ref sub_ex, idx) => {
1369                 if generated_code(sub_ex.span) {
1370                     return
1371                 }
1372
1373                 self.visit_expr(&**sub_ex);
1374
1375                 let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty;
1376                 match *ty {
1377                     ty::ty_struct(def_id, _) => {
1378                         let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, def_id);
1379                         for (i, f) in fields.iter().enumerate() {
1380                             if i == idx.node {
1381                                 let sub_span = self.span.sub_span_after_token(ex.span, token::Dot);
1382                                 self.fmt.ref_str(recorder::VarRef,
1383                                                  ex.span,
1384                                                  sub_span,
1385                                                  f.id,
1386                                                  self.cur_scope);
1387                                 break;
1388                             }
1389                         }
1390                     }
1391                     ty::ty_tup(_) => {}
1392                     _ => self.sess.span_bug(ex.span,
1393                                             &format!("Expected struct or tuple \
1394                                                       type, found {:?}", ty)[]),
1395                 }
1396             },
1397             ast::ExprClosure(_, _, ref decl, ref body) => {
1398                 if generated_code(body.span) {
1399                     return
1400                 }
1401
1402                 let mut id = String::from_str("$");
1403                 id.push_str(&ex.id.to_string()[]);
1404                 self.process_formals(&decl.inputs, &id[]);
1405
1406                 // walk arg and return types
1407                 for arg in &decl.inputs {
1408                     self.visit_ty(&*arg.ty);
1409                 }
1410
1411                 if let ast::Return(ref ret_ty) = decl.output {
1412                     self.visit_ty(&**ret_ty);
1413                 }
1414
1415                 // walk the body
1416                 self.nest(ex.id, |v| v.visit_block(&**body));
1417             },
1418             _ => {
1419                 visit::walk_expr(self, ex)
1420             },
1421         }
1422     }
1423
1424     fn visit_mac(&mut self, _: &ast::Mac) {
1425         // Just stop, macros are poison to us.
1426     }
1427
1428     fn visit_pat(&mut self, p: &ast::Pat) {
1429         self.process_pat(p);
1430         if !self.collecting {
1431             self.collected_paths.clear();
1432         }
1433     }
1434
1435     fn visit_arm(&mut self, arm: &ast::Arm) {
1436         assert!(self.collected_paths.len() == 0 && !self.collecting);
1437         self.collecting = true;
1438         for pattern in &arm.pats {
1439             // collect paths from the arm's patterns
1440             self.visit_pat(&**pattern);
1441         }
1442
1443         // This is to get around borrow checking, because we need mut self to call process_path.
1444         let mut paths_to_process = vec![];
1445         // process collected paths
1446         for &(id, ref p, ref immut, ref_kind) in &self.collected_paths {
1447             let def_map = self.analysis.ty_cx.def_map.borrow();
1448             if !def_map.contains_key(&id) {
1449                 self.sess.span_bug(p.span,
1450                                    &format!("def_map has no key for {} in visit_arm",
1451                                            id)[]);
1452             }
1453             let def = &(*def_map)[id];
1454             match *def {
1455                 def::DefLocal(id)  => {
1456                     let value = if *immut {
1457                         self.span.snippet(p.span).to_string()
1458                     } else {
1459                         "<mutable>".to_string()
1460                     };
1461
1462                     assert!(p.segments.len() == 1, "qualified path for local variable def in arm");
1463                     self.fmt.variable_str(p.span,
1464                                           Some(p.span),
1465                                           id,
1466                                           &path_to_string(p)[],
1467                                           &value[],
1468                                           "")
1469                 }
1470                 def::DefVariant(..) | def::DefTy(..) | def::DefStruct(..) => {
1471                     paths_to_process.push((id, p.clone(), Some(ref_kind)))
1472                 }
1473                 // FIXME(nrc) what are these doing here?
1474                 def::DefStatic(_, _) => {}
1475                 def::DefConst(..) => {}
1476                 _ => error!("unexpected definition kind when processing collected paths: {:?}",
1477                             *def)
1478             }
1479         }
1480         for &(id, ref path, ref_kind) in &paths_to_process {
1481             self.process_path(id, path.span, path, ref_kind);
1482         }
1483         self.collecting = false;
1484         self.collected_paths.clear();
1485         visit::walk_expr_opt(self, &arm.guard);
1486         self.visit_expr(&*arm.body);
1487     }
1488
1489     fn visit_stmt(&mut self, s: &ast::Stmt) {
1490         if generated_code(s.span) {
1491             return
1492         }
1493
1494         visit::walk_stmt(self, s)
1495     }
1496
1497     fn visit_local(&mut self, l: &ast::Local) {
1498         if generated_code(l.span) {
1499             return
1500         }
1501
1502         // The local could declare multiple new vars, we must walk the
1503         // pattern and collect them all.
1504         assert!(self.collected_paths.len() == 0 && !self.collecting);
1505         self.collecting = true;
1506         self.visit_pat(&*l.pat);
1507         self.collecting = false;
1508
1509         let value = self.span.snippet(l.span);
1510
1511         for &(id, ref p, ref immut, _) in &self.collected_paths {
1512             let value = if *immut { value.to_string() } else { "<mutable>".to_string() };
1513             let types = self.analysis.ty_cx.node_types.borrow();
1514             let typ = ppaux::ty_to_string(&self.analysis.ty_cx, (*types)[id]);
1515             // Get the span only for the name of the variable (I hope the path
1516             // is only ever a variable name, but who knows?).
1517             let sub_span = self.span.span_for_last_ident(p.span);
1518             // Rust uses the id of the pattern for var lookups, so we'll use it too.
1519             self.fmt.variable_str(p.span,
1520                                   sub_span,
1521                                   id,
1522                                   &path_to_string(p)[],
1523                                   &value[],
1524                                   &typ[]);
1525         }
1526         self.collected_paths.clear();
1527
1528         // Just walk the initialiser and type (don't want to walk the pattern again).
1529         visit::walk_ty_opt(self, &l.ty);
1530         visit::walk_expr_opt(self, &l.init);
1531     }
1532 }
1533
1534 pub fn process_crate(sess: &Session,
1535                      krate: &ast::Crate,
1536                      analysis: &ty::CrateAnalysis,
1537                      odir: Option<&Path>) {
1538     if generated_code(krate.span) {
1539         return;
1540     }
1541
1542     assert!(analysis.glob_map.is_some());
1543     let cratename = match attr::find_crate_name(&krate.attrs[]) {
1544         Some(name) => name.get().to_string(),
1545         None => {
1546             info!("Could not find crate name, using 'unknown_crate'");
1547             String::from_str("unknown_crate")
1548         },
1549     };
1550
1551     info!("Dumping crate {}", cratename);
1552
1553     // find a path to dump our data to
1554     let mut root_path = match env::var_string("DXR_RUST_TEMP_FOLDER") {
1555         Ok(val) => Path::new(val),
1556         Err(..) => match odir {
1557             Some(val) => val.join("dxr"),
1558             None => Path::new("dxr-temp"),
1559         },
1560     };
1561
1562     match fs::mkdir_recursive(&root_path, old_io::USER_RWX) {
1563         Err(e) => sess.err(&format!("Could not create directory {}: {}",
1564                            root_path.display(), e)[]),
1565         _ => (),
1566     }
1567
1568     {
1569         let disp = root_path.display();
1570         info!("Writing output to {}", disp);
1571     }
1572
1573     // Create output file.
1574     let mut out_name = cratename.clone();
1575     out_name.push_str(".csv");
1576     root_path.push(out_name);
1577     let output_file = match File::create(&root_path) {
1578         Ok(f) => box f,
1579         Err(e) => {
1580             let disp = root_path.display();
1581             sess.fatal(&format!("Could not open {}: {}", disp, e)[]);
1582         }
1583     };
1584     root_path.pop();
1585
1586     let mut visitor = DxrVisitor {
1587         sess: sess,
1588         analysis: analysis,
1589         collected_paths: vec!(),
1590         collecting: false,
1591         fmt: FmtStrs::new(box Recorder {
1592                             out: output_file as Box<Writer+'static>,
1593                             dump_spans: false,
1594                         },
1595                         SpanUtils {
1596                             sess: sess,
1597                             err_count: Cell::new(0)
1598                         }),
1599         span: SpanUtils {
1600             sess: sess,
1601             err_count: Cell::new(0)
1602         },
1603         cur_scope: 0
1604     };
1605
1606     visitor.dump_crate_info(&cratename[], krate);
1607
1608     visit::walk_crate(&mut visitor, krate);
1609 }