]> git.lizzy.rs Git - rust.git/blob - src/librustc_save_analysis/dump_visitor.rs
Use Config::full_docs to trim documentation
[rust.git] / src / librustc_save_analysis / dump_visitor.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Write the output of rustc's analysis to an implementor of Dump.
12 //!
13 //! Dumping the analysis is implemented by walking the AST and getting a bunch of
14 //! info out from all over the place. We use Def IDs to identify objects. The
15 //! tricky part is getting syntactic (span, source text) and semantic (reference
16 //! Def IDs) information for parts of expressions which the compiler has discarded.
17 //! E.g., in a path `foo::bar::baz`, the compiler only keeps a span for the whole
18 //! path and a reference to `baz`, but we want spans and references for all three
19 //! idents.
20 //!
21 //! SpanUtils is used to manipulate spans. In particular, to extract sub-spans
22 //! from spans (e.g., the span for `bar` from the above example path).
23 //! DumpVisitor walks the AST and processes it, and an implementor of Dump
24 //! is used for recording the output in a format-agnostic way (see CsvDumper
25 //! for an example).
26
27 use rustc::hir::def::Def as HirDef;
28 use rustc::hir::def_id::DefId;
29 use rustc::hir::map::Node;
30 use rustc::session::Session;
31 use rustc::ty::{self, TyCtxt};
32
33 use std::path::Path;
34
35 use syntax::ast::{self, NodeId, PatKind, Attribute, CRATE_NODE_ID};
36 use syntax::parse::token;
37 use syntax::symbol::keywords;
38 use syntax::visit::{self, Visitor};
39 use syntax::print::pprust::{path_to_string, ty_to_string, bounds_to_string, generics_to_string};
40 use syntax::ptr::P;
41 use syntax::codemap::Spanned;
42 use syntax_pos::*;
43
44 use {escape, generated_code, SaveContext, PathCollector, lower_attributes, Dump};
45 use span_utils::SpanUtils;
46 use sig;
47
48 use rls_data::{CratePreludeData, Import, ImportKind, SpanData, Ref, RefKind,
49                Def, DefKind, Relation, RelationKind};
50
51 macro_rules! down_cast_data {
52     ($id:ident, $kind:ident, $sp:expr) => {
53         let $id = if let super::Data::$kind(data) = $id {
54             data
55         } else {
56             span_bug!($sp, "unexpected data kind: {:?}", $id);
57         };
58     };
59 }
60
61 pub struct DumpVisitor<'l, 'tcx: 'l, 'll, D: 'll> {
62     save_ctxt: SaveContext<'l, 'tcx>,
63     sess: &'l Session,
64     tcx: TyCtxt<'l, 'tcx, 'tcx>,
65     dumper: &'ll mut D,
66
67     span: SpanUtils<'l>,
68
69     cur_scope: NodeId,
70
71     // Set of macro definition (callee) spans, and the set
72     // of macro use (callsite) spans. We store these to ensure
73     // we only write one macro def per unique macro definition, and
74     // one macro use per unique callsite span.
75     // mac_defs: HashSet<Span>,
76 }
77
78 impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
79     pub fn new(save_ctxt: SaveContext<'l, 'tcx>,
80                dumper: &'ll mut D)
81                -> DumpVisitor<'l, 'tcx, 'll, D> {
82         let span_utils = SpanUtils::new(&save_ctxt.tcx.sess);
83         DumpVisitor {
84             sess: &save_ctxt.tcx.sess,
85             tcx: save_ctxt.tcx,
86             save_ctxt: save_ctxt,
87             dumper: dumper,
88             span: span_utils.clone(),
89             cur_scope: CRATE_NODE_ID,
90             // mac_defs: HashSet::new(),
91         }
92     }
93
94     fn nest_scope<F>(&mut self, scope_id: NodeId, f: F)
95         where F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, D>)
96     {
97         let parent_scope = self.cur_scope;
98         self.cur_scope = scope_id;
99         f(self);
100         self.cur_scope = parent_scope;
101     }
102
103     fn nest_tables<F>(&mut self, item_id: NodeId, f: F)
104         where F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, D>)
105     {
106         let item_def_id = self.tcx.hir.local_def_id(item_id);
107         if self.tcx.has_typeck_tables(item_def_id) {
108             let tables = self.tcx.typeck_tables_of(item_def_id);
109             let old_tables = self.save_ctxt.tables;
110             self.save_ctxt.tables = tables;
111             f(self);
112             self.save_ctxt.tables = old_tables;
113         } else {
114             f(self);
115         }
116     }
117
118     fn span_from_span(&self, span: Span) -> SpanData {
119         self.save_ctxt.span_from_span(span)
120     }
121
122     pub fn dump_crate_info(&mut self, name: &str, krate: &ast::Crate) {
123         let source_file = self.tcx.sess.local_crate_source_file.as_ref();
124         let crate_root = source_file.map(|source_file| {
125             let source_file = Path::new(source_file);
126             match source_file.file_name() {
127                 Some(_) => source_file.parent().unwrap().display().to_string(),
128                 None => source_file.display().to_string(),
129             }
130         });
131
132         let data = CratePreludeData {
133             crate_name: name.into(),
134             crate_root: crate_root.unwrap_or("<no source>".to_owned()),
135             external_crates: self.save_ctxt.get_external_crates(),
136             span: self.span_from_span(krate.span),
137         };
138
139         self.dumper.crate_prelude(data);
140     }
141
142     // Return all non-empty prefixes of a path.
143     // For each prefix, we return the span for the last segment in the prefix and
144     // a str representation of the entire prefix.
145     fn process_path_prefixes(&self, path: &ast::Path) -> Vec<(Span, String)> {
146         let spans = self.span.spans_for_path_segments(path);
147         let segments = &path.segments[if path.is_global() { 1 } else { 0 }..];
148
149         // Paths to enums seem to not match their spans - the span includes all the
150         // variants too. But they seem to always be at the end, so I hope we can cope with
151         // always using the first ones. So, only error out if we don't have enough spans.
152         // What could go wrong...?
153         if spans.len() < segments.len() {
154             if generated_code(path.span) {
155                 return vec![];
156             }
157             error!("Mis-calculated spans for path '{}'. Found {} spans, expected {}. Found spans:",
158                    path_to_string(path),
159                    spans.len(),
160                    segments.len());
161             for s in &spans {
162                 let loc = self.sess.codemap().lookup_char_pos(s.lo);
163                 error!("    '{}' in {}, line {}",
164                        self.span.snippet(*s),
165                        loc.file.name,
166                        loc.line);
167             }
168             error!("    master span: {:?}: `{}`", path.span, self.span.snippet(path.span));
169             return vec![];
170         }
171
172         let mut result: Vec<(Span, String)> = vec![];
173
174         let mut segs = vec![];
175         for (i, (seg, span)) in segments.iter().zip(&spans).enumerate() {
176             segs.push(seg.clone());
177             let sub_path = ast::Path {
178                 span: *span, // span for the last segment
179                 segments: segs,
180             };
181             let qualname = if i == 0 && path.is_global() {
182                 format!("::{}", path_to_string(&sub_path))
183             } else {
184                 path_to_string(&sub_path)
185             };
186             result.push((*span, qualname));
187             segs = sub_path.segments;
188         }
189
190         result
191     }
192
193     fn write_sub_paths(&mut self, path: &ast::Path) {
194         let sub_paths = self.process_path_prefixes(path);
195         for (span, _) in sub_paths {
196             let span = self.span_from_span(span);
197             self.dumper.dump_ref(Ref {
198                 kind: RefKind::Mod,
199                 span,
200                 ref_id: ::null_id(),
201             });
202         }
203     }
204
205     // As write_sub_paths, but does not process the last ident in the path (assuming it
206     // will be processed elsewhere). See note on write_sub_paths about global.
207     fn write_sub_paths_truncated(&mut self, path: &ast::Path) {
208         let sub_paths = self.process_path_prefixes(path);
209         let len = sub_paths.len();
210         if len <= 1 {
211             return;
212         }
213
214         for (span, _) in sub_paths.into_iter().take(len - 1) {
215             let span = self.span_from_span(span);
216             self.dumper.dump_ref(Ref {
217                 kind: RefKind::Mod,
218                 span,
219                 ref_id: ::null_id(),
220             });
221         }
222     }
223
224     // As write_sub_paths, but expects a path of the form module_path::trait::method
225     // Where trait could actually be a struct too.
226     fn write_sub_path_trait_truncated(&mut self, path: &ast::Path) {
227         let sub_paths = self.process_path_prefixes(path);
228         let len = sub_paths.len();
229         if len <= 1 {
230             return;
231         }
232         let sub_paths = &sub_paths[.. (len-1)];
233
234         // write the trait part of the sub-path
235         let (ref span, _) = sub_paths[len-2];
236         let span = self.span_from_span(*span);
237         self.dumper.dump_ref(Ref {
238             kind: RefKind::Type,
239             ref_id: ::null_id(),
240             span,
241         });
242
243         // write the other sub-paths
244         if len <= 2 {
245             return;
246         }
247         let sub_paths = &sub_paths[..len-2];
248         for &(ref span, _) in sub_paths {
249             let span = self.span_from_span(*span);
250             self.dumper.dump_ref(Ref {
251                 kind: RefKind::Mod,
252                 span,
253                 ref_id: ::null_id(),
254             });
255         }
256     }
257
258     fn lookup_def_id(&self, ref_id: NodeId) -> Option<DefId> {
259         match self.save_ctxt.get_path_def(ref_id) {
260             HirDef::PrimTy(..) | HirDef::SelfTy(..) | HirDef::Err => None,
261             def => Some(def.def_id()),
262         }
263     }
264
265     fn process_def_kind(&mut self,
266                         ref_id: NodeId,
267                         span: Span,
268                         sub_span: Option<Span>,
269                         def_id: DefId) {
270         if self.span.filter_generated(sub_span, span) {
271             return;
272         }
273
274         let def = self.save_ctxt.get_path_def(ref_id);
275         match def {
276             HirDef::Mod(_) => {
277                 let span = self.span_from_span(sub_span.expect("No span found for mod ref"));
278                 self.dumper.dump_ref(Ref {
279                     kind: RefKind::Mod,
280                     span,
281                     ref_id: ::id_from_def_id(def_id),
282                 });
283             }
284             HirDef::Struct(..) |
285             HirDef::Variant(..) |
286             HirDef::Union(..) |
287             HirDef::Enum(..) |
288             HirDef::TyAlias(..) |
289             HirDef::Trait(_) => {
290                 let span = self.span_from_span(sub_span.expect("No span found for type ref"));
291                 self.dumper.dump_ref(Ref {
292                     kind: RefKind::Type,
293                     span,
294                     ref_id: ::id_from_def_id(def_id),
295                 });
296             }
297             HirDef::Static(..) |
298             HirDef::Const(..) |
299             HirDef::StructCtor(..) |
300             HirDef::VariantCtor(..) => {
301                 let span = self.span_from_span(sub_span.expect("No span found for var ref"));
302                 self.dumper.dump_ref(Ref {
303                     kind: RefKind::Variable,
304                     span,
305                     ref_id: ::id_from_def_id(def_id),
306                 });
307             }
308             HirDef::Fn(..) => {
309                 let span = self.span_from_span(sub_span.expect("No span found for fn ref"));
310                 self.dumper.dump_ref(Ref {
311                     kind: RefKind::Function,
312                     span,
313                     ref_id: ::id_from_def_id(def_id),
314                 });
315             }
316             // With macros 2.0, we can legitimately get a ref to a macro, but
317             // we don't handle it properly for now (FIXME).
318             HirDef::Macro(..) => {}
319             HirDef::Local(..) |
320             HirDef::Upvar(..) |
321             HirDef::SelfTy(..) |
322             HirDef::Label(_) |
323             HirDef::TyParam(..) |
324             HirDef::Method(..) |
325             HirDef::AssociatedTy(..) |
326             HirDef::AssociatedConst(..) |
327             HirDef::PrimTy(_) |
328             HirDef::GlobalAsm(_) |
329             HirDef::Err => {
330                span_bug!(span,
331                          "process_def_kind for unexpected item: {:?}",
332                          def);
333             }
334         }
335     }
336
337     fn process_formals(&mut self, formals: &'l [ast::Arg], qualname: &str) {
338         for arg in formals {
339             self.visit_pat(&arg.pat);
340             let mut collector = PathCollector::new();
341             collector.visit_pat(&arg.pat);
342             let span_utils = self.span.clone();
343             for &(id, ref p, ..) in &collector.collected_paths {
344                 let typ = match self.save_ctxt.tables.node_types.get(&id) {
345                     Some(s) => s.to_string(),
346                     None => continue,
347                 };
348                 // get the span only for the name of the variable (I hope the path is only ever a
349                 // variable name, but who knows?)
350                 let sub_span = span_utils.span_for_last_ident(p.span);
351                 if !self.span.filter_generated(sub_span, p.span) {
352                     let id = ::id_from_node_id(id, &self.save_ctxt);
353                     let span = self.span_from_span(sub_span.expect("No span found for variable"));
354
355                     self.dumper.dump_def(false, Def {
356                         kind: DefKind::Local,
357                         id,
358                         span,
359                         name: path_to_string(p),
360                         qualname: format!("{}::{}", qualname, path_to_string(p)),
361                         value: typ,
362                         parent: None,
363                         children: vec![],
364                         decl_id: None,
365                         docs: String::new(),
366                         sig: None,
367                         attributes:vec![],
368                     });
369                 }
370             }
371         }
372     }
373
374     fn process_method(&mut self,
375                       sig: &'l ast::MethodSig,
376                       body: Option<&'l ast::Block>,
377                       id: ast::NodeId,
378                       name: ast::Ident,
379                       vis: ast::Visibility,
380                       span: Span) {
381         debug!("process_method: {}:{}", id, name);
382
383         if let Some(mut method_data) = self.save_ctxt.get_method_data(id, name.name, span) {
384
385             let sig_str = ::make_signature(&sig.decl, &sig.generics);
386             if body.is_some() {
387                 self.nest_tables(id, |v| {
388                     v.process_formals(&sig.decl.inputs, &method_data.qualname)
389                 });
390             }
391
392             self.process_generic_params(&sig.generics, span, &method_data.qualname, id);
393
394             method_data.value = sig_str;
395             method_data.sig = sig::method_signature(id, name, sig, &self.save_ctxt);
396             self.dumper.dump_def(vis == ast::Visibility::Public, method_data);
397         }
398
399         // walk arg and return types
400         for arg in &sig.decl.inputs {
401             self.visit_ty(&arg.ty);
402         }
403
404         if let ast::FunctionRetTy::Ty(ref ret_ty) = sig.decl.output {
405             self.visit_ty(ret_ty);
406         }
407
408         // walk the fn body
409         if let Some(body) = body {
410             self.nest_tables(id, |v| v.nest_scope(id, |v| v.visit_block(body)));
411         }
412     }
413
414     fn process_trait_ref(&mut self, trait_ref: &'l ast::TraitRef) {
415         let trait_ref_data = self.save_ctxt.get_trait_ref_data(trait_ref);
416         if let Some(trait_ref_data) = trait_ref_data {
417             self.dumper.dump_ref(trait_ref_data);
418         }
419         self.process_path(trait_ref.ref_id, &trait_ref.path);
420     }
421
422     fn process_struct_field_def(&mut self, field: &ast::StructField, parent_id: NodeId) {
423         let field_data = self.save_ctxt.get_field_data(field, parent_id);
424         if let Some(field_data) = field_data {
425             self.dumper.dump_def(field.vis == ast::Visibility::Public, field_data);
426         }
427     }
428
429     // Dump generic params bindings, then visit_generics
430     fn process_generic_params(&mut self,
431                               generics: &'l ast::Generics,
432                               full_span: Span,
433                               prefix: &str,
434                               id: NodeId) {
435         // We can't only use visit_generics since we don't have spans for param
436         // bindings, so we reparse the full_span to get those sub spans.
437         // However full span is the entire enum/fn/struct block, so we only want
438         // the first few to match the number of generics we're looking for.
439         let param_sub_spans = self.span.spans_for_ty_params(full_span,
440                                                             (generics.ty_params.len() as isize));
441         for (param, param_ss) in generics.ty_params.iter().zip(param_sub_spans) {
442             let name = escape(self.span.snippet(param_ss));
443             // Append $id to name to make sure each one is unique
444             let qualname = format!("{}::{}${}",
445                                    prefix,
446                                    name,
447                                    id);
448             if !self.span.filter_generated(Some(param_ss), full_span) {
449                 let id = ::id_from_node_id(param.id, &self.save_ctxt);
450                 let span = self.span_from_span(param_ss);
451
452                 self.dumper.dump_def(false, Def {
453                     kind: DefKind::Type,
454                     id,
455                     span,
456                     name,
457                     qualname,
458                     value: String::new(),
459                     parent: None,
460                     children: vec![],
461                     decl_id: None,
462                     docs: String::new(),
463                     sig: None,
464                     attributes: vec![],
465                 });
466             }
467         }
468         self.visit_generics(generics);
469     }
470
471     fn process_fn(&mut self,
472                   item: &'l ast::Item,
473                   decl: &'l ast::FnDecl,
474                   ty_params: &'l ast::Generics,
475                   body: &'l ast::Block) {
476         if let Some(fn_data) = self.save_ctxt.get_item_data(item) {
477             down_cast_data!(fn_data, DefData, item.span);
478             self.nest_tables(item.id, |v| v.process_formals(&decl.inputs, &fn_data.qualname));
479             self.process_generic_params(ty_params, item.span, &fn_data.qualname, item.id);
480             self.dumper.dump_def(item.vis == ast::Visibility::Public, fn_data);
481         }
482
483         for arg in &decl.inputs {
484             self.visit_ty(&arg.ty);
485         }
486
487         if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output {
488             self.visit_ty(&ret_ty);
489         }
490
491         self.nest_tables(item.id, |v| v.nest_scope(item.id, |v| v.visit_block(&body)));
492     }
493
494     fn process_static_or_const_item(&mut self,
495                                     item: &'l ast::Item,
496                                     typ: &'l ast::Ty,
497                                     expr: &'l ast::Expr) {
498         if let Some(var_data) = self.save_ctxt.get_item_data(item) {
499             down_cast_data!(var_data, DefData, item.span);
500             self.dumper.dump_def(item.vis == ast::Visibility::Public, var_data);
501         }
502         self.visit_ty(&typ);
503         self.visit_expr(expr);
504     }
505
506     fn process_assoc_const(&mut self,
507                            id: ast::NodeId,
508                            name: ast::Name,
509                            span: Span,
510                            typ: &'l ast::Ty,
511                            expr: Option<&'l ast::Expr>,
512                            parent_id: DefId,
513                            vis: ast::Visibility,
514                            attrs: &'l [Attribute]) {
515         let qualname = format!("::{}", self.tcx.node_path_str(id));
516
517         let sub_span = self.span.sub_span_after_keyword(span, keywords::Const);
518
519         if !self.span.filter_generated(sub_span, span) {
520             let sig = sig::assoc_const_signature(id, name, typ, expr, &self.save_ctxt);
521             let id = ::id_from_node_id(id, &self.save_ctxt);
522             let span = self.span_from_span(sub_span.expect("No span found for variable"));
523
524             self.dumper.dump_def(vis == ast::Visibility::Public, Def {
525                 kind: DefKind::Const,
526                 id,
527                 span,
528                 name: name.to_string(),
529                 qualname,
530                 value: ty_to_string(&typ),
531                 parent: Some(::id_from_def_id(parent_id)),
532                 children: vec![],
533                 decl_id: None,
534                 docs: self.save_ctxt.docs_for_attrs(attrs),
535                 sig,
536                 attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt),
537             });
538         }
539
540         // walk type and init value
541         self.visit_ty(typ);
542         if let Some(expr) = expr {
543             self.visit_expr(expr);
544         }
545     }
546
547     // FIXME tuple structs should generate tuple-specific data.
548     fn process_struct(&mut self,
549                       item: &'l ast::Item,
550                       def: &'l ast::VariantData,
551                       ty_params: &'l ast::Generics) {
552         let name = item.ident.to_string();
553         let qualname = format!("::{}", self.tcx.node_path_str(item.id));
554
555         let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Struct);
556         let (value, fields) =
557             if let ast::ItemKind::Struct(ast::VariantData::Struct(ref fields, _), _) = item.node
558         {
559             let fields_str = fields.iter()
560                                    .enumerate()
561                                    .map(|(i, f)| f.ident.map(|i| i.to_string())
562                                                   .unwrap_or(i.to_string()))
563                                    .collect::<Vec<_>>()
564                                    .join(", ");
565             (format!("{} {{ {} }}", name, fields_str),
566              fields.iter().map(|f| ::id_from_node_id(f.id, &self.save_ctxt)).collect())
567         } else {
568             (String::new(), vec![])
569         };
570
571         if !self.span.filter_generated(sub_span, item.span) {
572             let span = self.span_from_span(sub_span.expect("No span found for struct"));
573             self.dumper.dump_def(item.vis == ast::Visibility::Public, Def {
574                 kind: DefKind::Struct,
575                 id: ::id_from_node_id(item.id, &self.save_ctxt),
576                 span,
577                 name,
578                 qualname: qualname.clone(),
579                 value,
580                 parent: None,
581                 children: fields,
582                 decl_id: None,
583                 docs: self.save_ctxt.docs_for_attrs(&item.attrs),
584                 sig: sig::item_signature(item, &self.save_ctxt),
585                 attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt),
586             });
587         }
588
589         for field in def.fields() {
590             self.process_struct_field_def(field, item.id);
591             self.visit_ty(&field.ty);
592         }
593
594         self.process_generic_params(ty_params, item.span, &qualname, item.id);
595     }
596
597     fn process_enum(&mut self,
598                     item: &'l ast::Item,
599                     enum_definition: &'l ast::EnumDef,
600                     ty_params: &'l ast::Generics) {
601         let enum_data = self.save_ctxt.get_item_data(item);
602         let enum_data = match enum_data {
603             None => return,
604             Some(data) => data,
605         };
606         down_cast_data!(enum_data, DefData, item.span);
607
608         for variant in &enum_definition.variants {
609             let name = variant.node.name.name.to_string();
610             let mut qualname = enum_data.qualname.clone();
611             qualname.push_str("::");
612             qualname.push_str(&name);
613
614             match variant.node.data {
615                 ast::VariantData::Struct(ref fields, _) => {
616                     let sub_span = self.span.span_for_first_ident(variant.span);
617                     let fields_str = fields.iter()
618                                            .enumerate()
619                                            .map(|(i, f)| f.ident.map(|i| i.to_string())
620                                                           .unwrap_or(i.to_string()))
621                                            .collect::<Vec<_>>()
622                                            .join(", ");
623                     let value = format!("{}::{} {{ {} }}", enum_data.name, name, fields_str);
624                     if !self.span.filter_generated(sub_span, variant.span) {
625                         let span = self.span_from_span(
626                             sub_span.expect("No span found for struct variant"));
627                         let id = ::id_from_node_id(variant.node.data.id(), &self.save_ctxt);
628                         let parent = Some(::id_from_node_id(item.id, &self.save_ctxt));
629
630                         self.dumper.dump_def(item.vis == ast::Visibility::Public, Def {
631                             kind: DefKind::Struct,
632                             id,
633                             span,
634                             name,
635                             qualname,
636                             value,
637                             parent,
638                             children: vec![],
639                             decl_id: None,
640                             docs: self.save_ctxt.docs_for_attrs(&variant.node.attrs),
641                             sig: sig::variant_signature(variant, &self.save_ctxt),
642                             attributes: lower_attributes(variant.node.attrs.clone(),
643                                                          &self.save_ctxt),
644                         });
645                     }
646                 }
647                 ref v => {
648                     let sub_span = self.span.span_for_first_ident(variant.span);
649                     let mut value = format!("{}::{}", enum_data.name, name);
650                     if let &ast::VariantData::Tuple(ref fields, _) = v {
651                         value.push('(');
652                         value.push_str(&fields.iter()
653                                               .map(|f| ty_to_string(&f.ty))
654                                               .collect::<Vec<_>>()
655                                               .join(", "));
656                         value.push(')');
657                     }
658                     if !self.span.filter_generated(sub_span, variant.span) {
659                         let span =
660                             self.span_from_span(sub_span.expect("No span found for tuple variant"));
661                         let id = ::id_from_node_id(variant.node.data.id(), &self.save_ctxt);
662                         let parent = Some(::id_from_node_id(item.id, &self.save_ctxt));
663
664                         self.dumper.dump_def(item.vis == ast::Visibility::Public, Def {
665                             kind: DefKind::Tuple,
666                             id,
667                             span,
668                             name,
669                             qualname,
670                             value,
671                             parent,
672                             children: vec![],
673                             decl_id: None,
674                             docs: self.save_ctxt.docs_for_attrs(&variant.node.attrs),
675                             sig: sig::variant_signature(variant, &self.save_ctxt),
676                             attributes: lower_attributes(variant.node.attrs.clone(),
677                                                          &self.save_ctxt),
678                         });
679                     }
680                 }
681             }
682
683
684             for field in variant.node.data.fields() {
685                 self.process_struct_field_def(field, variant.node.data.id());
686                 self.visit_ty(&field.ty);
687             }
688         }
689         self.process_generic_params(ty_params, item.span, &enum_data.qualname, item.id);
690         self.dumper.dump_def(item.vis == ast::Visibility::Public, enum_data);
691     }
692
693     fn process_impl(&mut self,
694                     item: &'l ast::Item,
695                     type_parameters: &'l ast::Generics,
696                     trait_ref: &'l Option<ast::TraitRef>,
697                     typ: &'l ast::Ty,
698                     impl_items: &'l [ast::ImplItem]) {
699         if let Some(impl_data) = self.save_ctxt.get_item_data(item) {
700             down_cast_data!(impl_data, RelationData, item.span);
701             self.dumper.dump_relation(impl_data);
702         }
703         self.visit_ty(&typ);
704         if let &Some(ref trait_ref) = trait_ref {
705             self.process_path(trait_ref.ref_id, &trait_ref.path);
706         }
707         self.process_generic_params(type_parameters, item.span, "", item.id);
708         for impl_item in impl_items {
709             let map = &self.tcx.hir;
710             self.process_impl_item(impl_item, map.local_def_id(item.id));
711         }
712     }
713
714     fn process_trait(&mut self,
715                      item: &'l ast::Item,
716                      generics: &'l ast::Generics,
717                      trait_refs: &'l ast::TyParamBounds,
718                      methods: &'l [ast::TraitItem]) {
719         let name = item.ident.to_string();
720         let qualname = format!("::{}", self.tcx.node_path_str(item.id));
721         let mut val = name.clone();
722         if !generics.lifetimes.is_empty() || !generics.ty_params.is_empty() {
723             val.push_str(&generics_to_string(generics));
724         }
725         if !trait_refs.is_empty() {
726             val.push_str(": ");
727             val.push_str(&bounds_to_string(trait_refs));
728         }
729         let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Trait);
730         if !self.span.filter_generated(sub_span, item.span) {
731             let id = ::id_from_node_id(item.id, &self.save_ctxt);
732             let span = self.span_from_span(sub_span.expect("No span found for trait"));
733             let children =
734                 methods.iter().map(|i| ::id_from_node_id(i.id, &self.save_ctxt)).collect();
735             self.dumper.dump_def(item.vis == ast::Visibility::Public, Def {
736                 kind: DefKind::Trait,
737                 id,
738                 span,
739                 name,
740                 qualname: qualname.clone(),
741                 value: val,
742                 parent: None,
743                 children,
744                 decl_id: None,
745                 docs: self.save_ctxt.docs_for_attrs(&item.attrs),
746                 sig: sig::item_signature(item, &self.save_ctxt),
747                 attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt),
748             });
749         }
750
751         // super-traits
752         for super_bound in trait_refs.iter() {
753             let trait_ref = match *super_bound {
754                 ast::TraitTyParamBound(ref trait_ref, _) => {
755                     trait_ref
756                 }
757                 ast::RegionTyParamBound(..) => {
758                     continue;
759                 }
760             };
761
762             let trait_ref = &trait_ref.trait_ref;
763             if let Some(id) = self.lookup_def_id(trait_ref.ref_id) {
764                 let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span);
765                 if !self.span.filter_generated(sub_span, trait_ref.path.span) {
766                     let span = self.span_from_span(sub_span.expect("No span found for trait ref"));
767                     self.dumper.dump_ref(Ref {
768                         kind: RefKind::Type,
769                         span,
770                         ref_id: ::id_from_def_id(id),
771                     });
772                 }
773
774                 if !self.span.filter_generated(sub_span, trait_ref.path.span) {
775                     let sub_span = self.span_from_span(sub_span.expect("No span for inheritance"));
776                     self.dumper.dump_relation(Relation {
777                         kind: RelationKind::SuperTrait,
778                         span: sub_span,
779                         from: ::id_from_def_id(id),
780                         to: ::id_from_node_id(item.id, &self.save_ctxt),
781                     });
782                 }
783             }
784         }
785
786         // walk generics and methods
787         self.process_generic_params(generics, item.span, &qualname, item.id);
788         for method in methods {
789             let map = &self.tcx.hir;
790             self.process_trait_item(method, map.local_def_id(item.id))
791         }
792     }
793
794     // `item` is the module in question, represented as an item.
795     fn process_mod(&mut self, item: &ast::Item) {
796         if let Some(mod_data) = self.save_ctxt.get_item_data(item) {
797             down_cast_data!(mod_data, DefData, item.span);
798             self.dumper.dump_def(item.vis == ast::Visibility::Public, mod_data);
799         }
800     }
801
802     fn process_path(&mut self, id: NodeId, path: &ast::Path) {
803         let path_data = self.save_ctxt.get_path_data(id, path);
804         if generated_code(path.span) && path_data.is_none() {
805             return;
806         }
807
808         let path_data = match path_data {
809             Some(pd) => pd,
810             None => {
811                 return;
812             }
813         };
814
815         self.dumper.dump_ref(path_data);
816
817         // Modules or types in the path prefix.
818         match self.save_ctxt.get_path_def(id) {
819             HirDef::Method(did) => {
820                 let ti = self.tcx.associated_item(did);
821                 if ti.kind == ty::AssociatedKind::Method && ti.method_has_self_argument {
822                     self.write_sub_path_trait_truncated(path);
823                 }
824             }
825             HirDef::Fn(..) |
826             HirDef::Const(..) |
827             HirDef::Static(..) |
828             HirDef::StructCtor(..) |
829             HirDef::VariantCtor(..) |
830             HirDef::AssociatedConst(..) |
831             HirDef::Local(..) |
832             HirDef::Upvar(..) |
833             HirDef::Struct(..) |
834             HirDef::Union(..) |
835             HirDef::Variant(..) |
836             HirDef::TyAlias(..) |
837             HirDef::AssociatedTy(..) => self.write_sub_paths_truncated(path),
838             _ => {}
839         }
840     }
841
842     fn process_struct_lit(&mut self,
843                           ex: &'l ast::Expr,
844                           path: &'l ast::Path,
845                           fields: &'l [ast::Field],
846                           variant: &'l ty::VariantDef,
847                           base: &'l Option<P<ast::Expr>>) {
848         self.write_sub_paths_truncated(path);
849
850         if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) {
851             down_cast_data!(struct_lit_data, RefData, ex.span);
852             if !generated_code(ex.span) {
853                 self.dumper.dump_ref(struct_lit_data);
854             }
855
856             for field in fields {
857                 if let Some(field_data) = self.save_ctxt
858                                               .get_field_ref_data(field, variant) {
859                     self.dumper.dump_ref(field_data);
860                 }
861
862                 self.visit_expr(&field.expr)
863             }
864         }
865
866         walk_list!(self, visit_expr, base);
867     }
868
869     fn process_method_call(&mut self, ex: &'l ast::Expr, args: &'l [P<ast::Expr>]) {
870         if let Some(mcd) = self.save_ctxt.get_expr_data(ex) {
871             down_cast_data!(mcd, RefData, ex.span);
872             if !generated_code(ex.span) {
873                 self.dumper.dump_ref(mcd);
874             }
875         }
876
877         // walk receiver and args
878         walk_list!(self, visit_expr, args);
879     }
880
881     fn process_pat(&mut self, p: &'l ast::Pat) {
882         match p.node {
883             PatKind::Struct(ref _path, ref fields, _) => {
884                 // FIXME do something with _path?
885                 let adt = match self.save_ctxt.tables.node_id_to_type_opt(p.id) {
886                     Some(ty) => ty.ty_adt_def().unwrap(),
887                     None => {
888                         visit::walk_pat(self, p);
889                         return;
890                     }
891                 };
892                 let variant = adt.variant_of_def(self.save_ctxt.get_path_def(p.id));
893
894                 for &Spanned { node: ref field, span } in fields {
895                     let sub_span = self.span.span_for_first_ident(span);
896                     if let Some(f) = variant.find_field_named(field.ident.name) {
897                         if !self.span.filter_generated(sub_span, span) {
898                             let span =
899                                 self.span_from_span(sub_span.expect("No span fund for var ref"));
900                             self.dumper.dump_ref(Ref {
901                                 kind: RefKind::Variable,
902                                 span,
903                                 ref_id: ::id_from_def_id(f.did),
904                             });
905                         }
906                     }
907                     self.visit_pat(&field.pat);
908                 }
909             }
910             _ => visit::walk_pat(self, p),
911         }
912     }
913
914
915     fn process_var_decl(&mut self, p: &'l ast::Pat, value: String) {
916         // The local could declare multiple new vars, we must walk the
917         // pattern and collect them all.
918         let mut collector = PathCollector::new();
919         collector.visit_pat(&p);
920         self.visit_pat(&p);
921
922         for &(id, ref p, immut) in &collector.collected_paths {
923             let mut value = match immut {
924                 ast::Mutability::Immutable => value.to_string(),
925                 _ => String::new(),
926             };
927             let typ = match self.save_ctxt.tables.node_types.get(&id) {
928                 Some(typ) => {
929                     let typ = typ.to_string();
930                     if !value.is_empty() {
931                         value.push_str(": ");
932                     }
933                     value.push_str(&typ);
934                     typ
935                 }
936                 None => String::new(),
937             };
938
939             // Get the span only for the name of the variable (I hope the path
940             // is only ever a variable name, but who knows?).
941             let sub_span = self.span.span_for_last_ident(p.span);
942             // Rust uses the id of the pattern for var lookups, so we'll use it too.
943             if !self.span.filter_generated(sub_span, p.span) {
944                 let qualname = format!("{}${}", path_to_string(p), id);
945                 let id = ::id_from_node_id(id, &self.save_ctxt);
946                 let span = self.span_from_span(sub_span.expect("No span found for variable"));
947
948                 self.dumper.dump_def(false, Def {
949                     kind: DefKind::Local,
950                     id,
951                     span,
952                     name: path_to_string(p),
953                     qualname,
954                     value: typ,
955                     parent: None,
956                     children: vec![],
957                     decl_id: None,
958                     docs: String::new(),
959                     sig: None,
960                     attributes:vec![],
961                 });
962             }
963         }
964     }
965
966     /// Extract macro use and definition information from the AST node defined
967     /// by the given NodeId, using the expansion information from the node's
968     /// span.
969     ///
970     /// If the span is not macro-generated, do nothing, else use callee and
971     /// callsite spans to record macro definition and use data, using the
972     /// mac_uses and mac_defs sets to prevent multiples.
973     fn process_macro_use(&mut self, span: Span) {
974         let data = match self.save_ctxt.get_macro_use_data(span) {
975             None => return,
976             Some(data) => data,
977         };
978
979         // FIXME write the macro def
980         // let mut hasher = DefaultHasher::new();
981         // data.callee_span.hash(&mut hasher);
982         // let hash = hasher.finish();
983         // let qualname = format!("{}::{}", data.name, hash);
984         // Don't write macro definition for imported macros
985         // if !self.mac_defs.contains(&data.callee_span)
986         //     && !data.imported {
987         //     self.mac_defs.insert(data.callee_span);
988         //     if let Some(sub_span) = self.span.span_for_macro_def_name(data.callee_span) {
989         //         self.dumper.macro_data(MacroData {
990         //             span: sub_span,
991         //             name: data.name.clone(),
992         //             qualname: qualname.clone(),
993         //             // FIXME where do macro docs come from?
994         //             docs: String::new(),
995         //         }.lower(self.tcx));
996         //     }
997         // }
998         self.dumper.macro_use(data);
999     }
1000
1001     fn process_trait_item(&mut self, trait_item: &'l ast::TraitItem, trait_id: DefId) {
1002         self.process_macro_use(trait_item.span);
1003         match trait_item.node {
1004             ast::TraitItemKind::Const(ref ty, ref expr) => {
1005                 self.process_assoc_const(trait_item.id,
1006                                          trait_item.ident.name,
1007                                          trait_item.span,
1008                                          &ty,
1009                                          expr.as_ref().map(|e| &**e),
1010                                          trait_id,
1011                                          ast::Visibility::Public,
1012                                          &trait_item.attrs);
1013             }
1014             ast::TraitItemKind::Method(ref sig, ref body) => {
1015                 self.process_method(sig,
1016                                     body.as_ref().map(|x| &**x),
1017                                     trait_item.id,
1018                                     trait_item.ident,
1019                                     ast::Visibility::Public,
1020                                     trait_item.span);
1021             }
1022             ast::TraitItemKind::Type(ref bounds, ref default_ty) => {
1023                 // FIXME do something with _bounds (for type refs)
1024                 let name = trait_item.ident.name.to_string();
1025                 let qualname = format!("::{}", self.tcx.node_path_str(trait_item.id));
1026                 let sub_span = self.span.sub_span_after_keyword(trait_item.span, keywords::Type);
1027
1028                 if !self.span.filter_generated(sub_span, trait_item.span) {
1029                     let span = self.span_from_span(sub_span.expect("No span found for assoc type"));
1030                     let id = ::id_from_node_id(trait_item.id, &self.save_ctxt);
1031
1032                     self.dumper.dump_def(true, Def {
1033                         kind: DefKind::Type,
1034                         id,
1035                         span,
1036                         name,
1037                         qualname,
1038                         value: self.span.snippet(trait_item.span),
1039                         parent: Some(::id_from_def_id(trait_id)),
1040                         children: vec![],
1041                         decl_id: None,
1042                         docs: self.save_ctxt.docs_for_attrs(&trait_item.attrs),
1043                         sig: sig::assoc_type_signature(trait_item.id,
1044                                                        trait_item.ident,
1045                                                        Some(bounds),
1046                                                        default_ty.as_ref().map(|ty| &**ty),
1047                                                        &self.save_ctxt),
1048                         attributes: lower_attributes(trait_item.attrs.clone(), &self.save_ctxt),
1049                     });
1050                 }
1051
1052                 if let &Some(ref default_ty) = default_ty {
1053                     self.visit_ty(default_ty)
1054                 }
1055             }
1056             ast::TraitItemKind::Macro(_) => {}
1057         }
1058     }
1059
1060     fn process_impl_item(&mut self, impl_item: &'l ast::ImplItem, impl_id: DefId) {
1061         self.process_macro_use(impl_item.span);
1062         match impl_item.node {
1063             ast::ImplItemKind::Const(ref ty, ref expr) => {
1064                 self.process_assoc_const(impl_item.id,
1065                                          impl_item.ident.name,
1066                                          impl_item.span,
1067                                          &ty,
1068                                          Some(expr),
1069                                          impl_id,
1070                                          impl_item.vis.clone(),
1071                                          &impl_item.attrs);
1072             }
1073             ast::ImplItemKind::Method(ref sig, ref body) => {
1074                 self.process_method(sig,
1075                                     Some(body),
1076                                     impl_item.id,
1077                                     impl_item.ident,
1078                                     impl_item.vis.clone(),
1079                                     impl_item.span);
1080             }
1081             ast::ImplItemKind::Type(ref ty) => {
1082                 // FIXME uses of the assoc type should ideally point to this
1083                 // 'def' and the name here should be a ref to the def in the
1084                 // trait.
1085                 self.visit_ty(ty)
1086             }
1087             ast::ImplItemKind::Macro(_) => {}
1088         }
1089     }
1090 }
1091
1092 impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll, D> {
1093     fn visit_mod(&mut self, m: &'l ast::Mod, span: Span, attrs: &[ast::Attribute], id: NodeId) {
1094         // Since we handle explicit modules ourselves in visit_item, this should
1095         // only get called for the root module of a crate.
1096         assert_eq!(id, ast::CRATE_NODE_ID);
1097
1098         let qualname = format!("::{}", self.tcx.node_path_str(id));
1099
1100         let cm = self.tcx.sess.codemap();
1101         let filename = cm.span_to_filename(span);
1102         let data_id = ::id_from_node_id(id, &self.save_ctxt);
1103         let children = m.items.iter().map(|i| ::id_from_node_id(i.id, &self.save_ctxt)).collect();
1104         let span = self.span_from_span(span);
1105
1106         self.dumper.dump_def(true, Def {
1107             kind: DefKind::Mod,
1108             id: data_id,
1109             name: String::new(),
1110             qualname,
1111             span,
1112             value: filename,
1113             children,
1114             parent: None,
1115             decl_id: None,
1116             docs: self.save_ctxt.docs_for_attrs(attrs),
1117             sig: None,
1118             attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt),
1119         });
1120         self.nest_scope(id, |v| visit::walk_mod(v, m));
1121     }
1122
1123     fn visit_item(&mut self, item: &'l ast::Item) {
1124         use syntax::ast::ItemKind::*;
1125         self.process_macro_use(item.span);
1126         match item.node {
1127             Use(ref use_item) => {
1128                 match use_item.node {
1129                     ast::ViewPathSimple(ident, ref path) => {
1130                         let sub_span = self.span.span_for_last_ident(path.span);
1131                         let mod_id = match self.lookup_def_id(item.id) {
1132                             Some(def_id) => {
1133                                 self.process_def_kind(item.id, path.span, sub_span, def_id);
1134                                 Some(def_id)
1135                             }
1136                             None => None,
1137                         };
1138
1139                         // 'use' always introduces an alias, if there is not an explicit
1140                         // one, there is an implicit one.
1141                         let sub_span = match self.span.sub_span_after_keyword(use_item.span,
1142                                                                               keywords::As) {
1143                             Some(sub_span) => Some(sub_span),
1144                             None => sub_span,
1145                         };
1146
1147                         if !self.span.filter_generated(sub_span, path.span) {
1148                             let span =
1149                                 self.span_from_span(sub_span.expect("No span found for use"));
1150                             self.dumper.import(item.vis == ast::Visibility::Public, Import {
1151                                 kind: ImportKind::Use,
1152                                 ref_id: mod_id.map(|id| ::id_from_def_id(id)),
1153                                 span,
1154                                 name: ident.to_string(),
1155                                 value: String::new(),
1156                             });
1157                         }
1158                         self.write_sub_paths_truncated(path);
1159                     }
1160                     ast::ViewPathGlob(ref path) => {
1161                         // Make a comma-separated list of names of imported modules.
1162                         let mut names = vec![];
1163                         let glob_map = &self.save_ctxt.analysis.glob_map;
1164                         let glob_map = glob_map.as_ref().unwrap();
1165                         if glob_map.contains_key(&item.id) {
1166                             for n in glob_map.get(&item.id).unwrap() {
1167                                 names.push(n.to_string());
1168                             }
1169                         }
1170
1171                         let sub_span = self.span
1172                                            .sub_span_of_token(item.span, token::BinOp(token::Star));
1173                         if !self.span.filter_generated(sub_span, item.span) {
1174                             let span =
1175                                 self.span_from_span(sub_span.expect("No span found for use glob"));
1176                             self.dumper.import(item.vis == ast::Visibility::Public, Import {
1177                                 kind: ImportKind::GlobUse,
1178                                 ref_id: None,
1179                                 span,
1180                                 name: "*".to_owned(),
1181                                 value: names.join(", "),
1182                             });
1183                         }
1184                         self.write_sub_paths(path);
1185                     }
1186                     ast::ViewPathList(ref path, ref list) => {
1187                         for plid in list {
1188                             let id = plid.node.id;
1189                             if let Some(def_id) = self.lookup_def_id(id) {
1190                                 let span = plid.span;
1191                                 self.process_def_kind(id, span, Some(span), def_id);
1192                             }
1193                         }
1194
1195                         self.write_sub_paths(path);
1196                     }
1197                 }
1198             }
1199             ExternCrate(_) => {
1200                 let alias_span = self.span.span_for_last_ident(item.span);
1201
1202                 if !self.span.filter_generated(alias_span, item.span) {
1203                     let span =
1204                         self.span_from_span(alias_span.expect("No span found for extern crate"));
1205                     self.dumper.import(false, Import {
1206                         kind: ImportKind::ExternCrate,
1207                         ref_id: None,
1208                         span,
1209                         name: item.ident.to_string(),
1210                         value: String::new(),
1211                     });
1212                 }
1213             }
1214             Fn(ref decl, .., ref ty_params, ref body) =>
1215                 self.process_fn(item, &decl, ty_params, &body),
1216             Static(ref typ, _, ref expr) =>
1217                 self.process_static_or_const_item(item, typ, expr),
1218             Const(ref typ, ref expr) =>
1219                 self.process_static_or_const_item(item, &typ, &expr),
1220             Struct(ref def, ref ty_params) => self.process_struct(item, def, ty_params),
1221             Enum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
1222             Impl(..,
1223                  ref ty_params,
1224                  ref trait_ref,
1225                  ref typ,
1226                  ref impl_items) => {
1227                 self.process_impl(item, ty_params, trait_ref, &typ, impl_items)
1228             }
1229             Trait(_, ref generics, ref trait_refs, ref methods) =>
1230                 self.process_trait(item, generics, trait_refs, methods),
1231             Mod(ref m) => {
1232                 self.process_mod(item);
1233                 self.nest_scope(item.id, |v| visit::walk_mod(v, m));
1234             }
1235             Ty(ref ty, ref ty_params) => {
1236                 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
1237                 let value = ty_to_string(&ty);
1238                 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Type);
1239                 if !self.span.filter_generated(sub_span, item.span) {
1240                     let span = self.span_from_span(sub_span.expect("No span found for typedef"));
1241                     let id = ::id_from_node_id(item.id, &self.save_ctxt);
1242
1243                     self.dumper.dump_def(item.vis == ast::Visibility::Public, Def {
1244                         kind: DefKind::Type,
1245                         id,
1246                         span,
1247                         name: item.ident.to_string(),
1248                         qualname: qualname.clone(),
1249                         value,
1250                         parent: None,
1251                         children: vec![],
1252                         decl_id: None,
1253                         docs: self.save_ctxt.docs_for_attrs(&item.attrs),
1254                         sig: sig::item_signature(item, &self.save_ctxt),
1255                         attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt),
1256                     });
1257                 }
1258
1259                 self.visit_ty(&ty);
1260                 self.process_generic_params(ty_params, item.span, &qualname, item.id);
1261             }
1262             Mac(_) => (),
1263             _ => visit::walk_item(self, item),
1264         }
1265     }
1266
1267     fn visit_generics(&mut self, generics: &'l ast::Generics) {
1268         for param in generics.ty_params.iter() {
1269             for bound in param.bounds.iter() {
1270                 if let ast::TraitTyParamBound(ref trait_ref, _) = *bound {
1271                     self.process_trait_ref(&trait_ref.trait_ref);
1272                 }
1273             }
1274             if let Some(ref ty) = param.default {
1275                 self.visit_ty(&ty);
1276             }
1277         }
1278     }
1279
1280     fn visit_ty(&mut self, t: &'l ast::Ty) {
1281         self.process_macro_use(t.span);
1282         match t.node {
1283             ast::TyKind::Path(_, ref path) => {
1284                 if generated_code(t.span) {
1285                     return;
1286                 }
1287
1288                 if let Some(id) = self.lookup_def_id(t.id) {
1289                     if let Some(sub_span) = self.span.sub_span_for_type_name(t.span) {
1290                         let span = self.span_from_span(sub_span);
1291                         self.dumper.dump_ref(Ref {
1292                             kind: RefKind::Type,
1293                             span,
1294                             ref_id: ::id_from_def_id(id),
1295                         });
1296                     }
1297                 }
1298
1299                 self.write_sub_paths_truncated(path);
1300                 visit::walk_path(self, path);
1301             }
1302             ast::TyKind::Array(ref element, ref length) => {
1303                 self.visit_ty(element);
1304                 self.nest_tables(length.id, |v| v.visit_expr(length));
1305             }
1306             _ => visit::walk_ty(self, t),
1307         }
1308     }
1309
1310     fn visit_expr(&mut self, ex: &'l ast::Expr) {
1311         debug!("visit_expr {:?}", ex.node);
1312         self.process_macro_use(ex.span);
1313         match ex.node {
1314             ast::ExprKind::Struct(ref path, ref fields, ref base) => {
1315                 let hir_expr = self.save_ctxt.tcx.hir.expect_expr(ex.id);
1316                 let adt = match self.save_ctxt.tables.expr_ty_opt(&hir_expr) {
1317                     Some(ty) => ty.ty_adt_def().unwrap(),
1318                     None => {
1319                         visit::walk_expr(self, ex);
1320                         return;
1321                     }
1322                 };
1323                 let def = self.save_ctxt.get_path_def(hir_expr.id);
1324                 self.process_struct_lit(ex, path, fields, adt.variant_of_def(def), base)
1325             }
1326             ast::ExprKind::MethodCall(.., ref args) => self.process_method_call(ex, args),
1327             ast::ExprKind::Field(ref sub_ex, _) => {
1328                 self.visit_expr(&sub_ex);
1329
1330                 if let Some(field_data) = self.save_ctxt.get_expr_data(ex) {
1331                     down_cast_data!(field_data, RefData, ex.span);
1332                     if !generated_code(ex.span) {
1333                         self.dumper.dump_ref(field_data);
1334                     }
1335                 }
1336             }
1337             ast::ExprKind::TupField(ref sub_ex, idx) => {
1338                 self.visit_expr(&sub_ex);
1339
1340                 let hir_node = match self.save_ctxt.tcx.hir.find(sub_ex.id) {
1341                     Some(Node::NodeExpr(expr)) => expr,
1342                     _ => {
1343                         debug!("Missing or weird node for sub-expression {} in {:?}",
1344                                sub_ex.id, ex);
1345                         return;
1346                     }
1347                 };
1348                 let ty = match self.save_ctxt.tables.expr_ty_adjusted_opt(&hir_node) {
1349                     Some(ty) => &ty.sty,
1350                     None => {
1351                         visit::walk_expr(self, ex);
1352                         return;
1353                     }
1354                 };
1355                 match *ty {
1356                     ty::TyAdt(def, _) => {
1357                         let sub_span = self.span.sub_span_after_token(ex.span, token::Dot);
1358                         if !self.span.filter_generated(sub_span, ex.span) {
1359                             let span =
1360                                 self.span_from_span(sub_span.expect("No span found for var ref"));
1361                             self.dumper.dump_ref(Ref {
1362                                 kind: RefKind::Variable,
1363                                 span: span,
1364                                 ref_id: ::id_from_def_id(def.struct_variant().fields[idx.node].did),
1365                             });
1366                         }
1367                     }
1368                     ty::TyTuple(..) => {}
1369                     _ => span_bug!(ex.span,
1370                                    "Expected struct or tuple type, found {:?}",
1371                                    ty),
1372                 }
1373             }
1374             ast::ExprKind::Closure(_, ref decl, ref body, _fn_decl_span) => {
1375                 let mut id = String::from("$");
1376                 id.push_str(&ex.id.to_string());
1377
1378                 // walk arg and return types
1379                 for arg in &decl.inputs {
1380                     self.visit_ty(&arg.ty);
1381                 }
1382
1383                 if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output {
1384                     self.visit_ty(&ret_ty);
1385                 }
1386
1387                 // walk the body
1388                 self.nest_tables(ex.id, |v| {
1389                     v.process_formals(&decl.inputs, &id);
1390                     v.nest_scope(ex.id, |v| v.visit_expr(body))
1391                 });
1392             }
1393             ast::ExprKind::ForLoop(ref pattern, ref subexpression, ref block, _) |
1394             ast::ExprKind::WhileLet(ref pattern, ref subexpression, ref block, _) => {
1395                 let value = self.span.snippet(subexpression.span);
1396                 self.process_var_decl(pattern, value);
1397                 debug!("for loop, walk sub-expr: {:?}", subexpression.node);
1398                 visit::walk_expr(self, subexpression);
1399                 visit::walk_block(self, block);
1400             }
1401             ast::ExprKind::IfLet(ref pattern, ref subexpression, ref block, ref opt_else) => {
1402                 let value = self.span.snippet(subexpression.span);
1403                 self.process_var_decl(pattern, value);
1404                 visit::walk_expr(self, subexpression);
1405                 visit::walk_block(self, block);
1406                 opt_else.as_ref().map(|el| visit::walk_expr(self, el));
1407             }
1408             ast::ExprKind::Repeat(ref element, ref count) => {
1409                 self.visit_expr(element);
1410                 self.nest_tables(count.id, |v| v.visit_expr(count));
1411             }
1412             // In particular, we take this branch for call and path expressions,
1413             // where we'll index the idents involved just by continuing to walk.
1414             _ => {
1415                 visit::walk_expr(self, ex)
1416             }
1417         }
1418     }
1419
1420     fn visit_mac(&mut self, mac: &'l ast::Mac) {
1421         // These shouldn't exist in the AST at this point, log a span bug.
1422         span_bug!(mac.span, "macro invocation should have been expanded out of AST");
1423     }
1424
1425     fn visit_pat(&mut self, p: &'l ast::Pat) {
1426         self.process_macro_use(p.span);
1427         self.process_pat(p);
1428     }
1429
1430     fn visit_arm(&mut self, arm: &'l ast::Arm) {
1431         let mut collector = PathCollector::new();
1432         for pattern in &arm.pats {
1433             // collect paths from the arm's patterns
1434             collector.visit_pat(&pattern);
1435             self.visit_pat(&pattern);
1436         }
1437
1438         // This is to get around borrow checking, because we need mut self to call process_path.
1439         let mut paths_to_process = vec![];
1440
1441         // process collected paths
1442         for &(id, ref p, immut) in &collector.collected_paths {
1443             match self.save_ctxt.get_path_def(id) {
1444                 HirDef::Local(def_id) => {
1445                     let id = self.tcx.hir.as_local_node_id(def_id).unwrap();
1446                     let mut value = if immut == ast::Mutability::Immutable {
1447                         self.span.snippet(p.span).to_string()
1448                     } else {
1449                         "<mutable>".to_string()
1450                     };
1451                     let typ = self.save_ctxt.tables.node_types
1452                                   .get(&id).map(|t| t.to_string()).unwrap_or(String::new());
1453                     value.push_str(": ");
1454                     value.push_str(&typ);
1455
1456                     assert!(p.segments.len() == 1,
1457                             "qualified path for local variable def in arm");
1458                     if !self.span.filter_generated(Some(p.span), p.span) {
1459                         let qualname = format!("{}${}", path_to_string(p), id);
1460                         let id = ::id_from_node_id(id, &self.save_ctxt);
1461                         let span = self.span_from_span(p.span);
1462
1463                         self.dumper.dump_def(false, Def {
1464                             kind: DefKind::Local,
1465                             id,
1466                             span,
1467                             name: path_to_string(p),
1468                             qualname,
1469                             value: typ,
1470                             parent: None,
1471                             children: vec![],
1472                             decl_id: None,
1473                             docs: String::new(),
1474                             sig: None,
1475                             attributes:vec![],
1476                         });
1477                     }
1478                 }
1479                 HirDef::StructCtor(..) | HirDef::VariantCtor(..) |
1480                 HirDef::Const(..) | HirDef::AssociatedConst(..) |
1481                 HirDef::Struct(..) | HirDef::Variant(..) |
1482                 HirDef::TyAlias(..) | HirDef::AssociatedTy(..) |
1483                 HirDef::SelfTy(..) => {
1484                     paths_to_process.push((id, p.clone()))
1485                 }
1486                 def => error!("unexpected definition kind when processing collected paths: {:?}",
1487                               def),
1488             }
1489         }
1490
1491         for &(id, ref path) in &paths_to_process {
1492             self.process_path(id, path);
1493         }
1494         walk_list!(self, visit_expr, &arm.guard);
1495         self.visit_expr(&arm.body);
1496     }
1497
1498     fn visit_path(&mut self, p: &'l ast::Path, id: NodeId) {
1499         self.process_path(id, p);
1500     }
1501
1502     fn visit_stmt(&mut self, s: &'l ast::Stmt) {
1503         self.process_macro_use(s.span);
1504         visit::walk_stmt(self, s)
1505     }
1506
1507     fn visit_local(&mut self, l: &'l ast::Local) {
1508         self.process_macro_use(l.span);
1509         let value = l.init.as_ref().map(|i| self.span.snippet(i.span)).unwrap_or(String::new());
1510         self.process_var_decl(&l.pat, value);
1511
1512         // Just walk the initialiser and type (don't want to walk the pattern again).
1513         walk_list!(self, visit_ty, &l.ty);
1514         walk_list!(self, visit_expr, &l.init);
1515     }
1516
1517     fn visit_foreign_item(&mut self, item: &'l ast::ForeignItem) {
1518         match item.node {
1519             ast::ForeignItemKind::Fn(ref decl, ref generics) => {
1520                 if let Some(fn_data) = self.save_ctxt.get_extern_item_data(item) {
1521                     down_cast_data!(fn_data, DefData, item.span);
1522
1523                     self.nest_tables(item.id, |v| v.process_formals(&decl.inputs,
1524                                                                     &fn_data.qualname));
1525                     self.process_generic_params(generics, item.span, &fn_data.qualname, item.id);
1526                     self.dumper.dump_def(item.vis == ast::Visibility::Public, fn_data);
1527                 }
1528
1529                 for arg in &decl.inputs {
1530                     self.visit_ty(&arg.ty);
1531                 }
1532
1533                 if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output {
1534                     self.visit_ty(&ret_ty);
1535                 }
1536             }
1537             ast::ForeignItemKind::Static(ref ty, _) => {
1538                 if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) {
1539                     down_cast_data!(var_data, DefData, item.span);
1540                     self.dumper.dump_def(item.vis == ast::Visibility::Public, var_data);
1541                 }
1542
1543                 self.visit_ty(ty);
1544             }
1545         }
1546     }
1547 }