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