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