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