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