]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_save_analysis/src/dump_visitor.rs
Auto merge of #83166 - cjgillot:defkey-vge, r=oli-obk
[rust.git] / compiler / rustc_save_analysis / src / 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_ast as ast;
17 use rustc_ast::walk_list;
18 use rustc_data_structures::fx::FxHashSet;
19 use rustc_hir as hir;
20 use rustc_hir::def::{DefKind as HirDefKind, Res};
21 use rustc_hir::def_id::{DefId, LocalDefId};
22 use rustc_hir::intravisit::{self, Visitor};
23 use rustc_hir_pretty::{bounds_to_string, fn_to_string, generic_params_to_string, ty_to_string};
24 use rustc_middle::hir::map::Map;
25 use rustc_middle::span_bug;
26 use rustc_middle::ty::{self, DefIdTree, TyCtxt};
27 use rustc_session::config::Input;
28 use rustc_span::source_map::respan;
29 use rustc_span::symbol::Ident;
30 use rustc_span::*;
31
32 use std::env;
33 use std::path::Path;
34
35 use crate::dumper::{Access, Dumper};
36 use crate::sig;
37 use crate::span_utils::SpanUtils;
38 use crate::{
39     escape, generated_code, id_from_def_id, id_from_hir_id, lower_attributes, PathCollector,
40     SaveContext,
41 };
42
43 use rls_data::{
44     CompilationOptions, CratePreludeData, Def, DefKind, GlobalCrateId, Import, ImportKind, Ref,
45     RefKind, Relation, RelationKind, SpanData,
46 };
47
48 use tracing::{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 { public: $vis.node.is_pub(), reachable: $save_ctxt.access_levels.is_reachable($id) }
72     };
73 }
74
75 pub struct DumpVisitor<'tcx> {
76     pub save_ctxt: SaveContext<'tcx>,
77     tcx: TyCtxt<'tcx>,
78     dumper: Dumper,
79
80     span: SpanUtils<'tcx>,
81     // Set of macro definition (callee) spans, and the set
82     // of macro use (callsite) spans. We store these to ensure
83     // we only write one macro def per unique macro definition, and
84     // one macro use per unique callsite span.
85     // mac_defs: FxHashSet<Span>,
86     // macro_calls: FxHashSet<Span>,
87 }
88
89 impl<'tcx> DumpVisitor<'tcx> {
90     pub fn new(save_ctxt: SaveContext<'tcx>) -> DumpVisitor<'tcx> {
91         let span_utils = SpanUtils::new(&save_ctxt.tcx.sess);
92         let dumper = Dumper::new(save_ctxt.config.clone());
93         DumpVisitor {
94             tcx: save_ctxt.tcx,
95             save_ctxt,
96             dumper,
97             span: span_utils,
98             // mac_defs: FxHashSet::default(),
99             // macro_calls: FxHashSet::default(),
100         }
101     }
102
103     pub fn analysis(&self) -> &rls_data::Analysis {
104         self.dumper.analysis()
105     }
106
107     fn nest_typeck_results<F>(&mut self, item_def_id: LocalDefId, f: F)
108     where
109         F: FnOnce(&mut Self),
110     {
111         let typeck_results = if self.tcx.has_typeck_results(item_def_id) {
112             Some(self.tcx.typeck(item_def_id))
113         } else {
114             None
115         };
116
117         let old_maybe_typeck_results = self.save_ctxt.maybe_typeck_results;
118         self.save_ctxt.maybe_typeck_results = typeck_results;
119         f(self);
120         self.save_ctxt.maybe_typeck_results = old_maybe_typeck_results;
121     }
122
123     fn span_from_span(&self, span: Span) -> SpanData {
124         self.save_ctxt.span_from_span(span)
125     }
126
127     fn lookup_def_id(&self, ref_id: hir::HirId) -> Option<DefId> {
128         self.save_ctxt.lookup_def_id(ref_id)
129     }
130
131     pub fn dump_crate_info(&mut self, name: &str, krate: &hir::Crate<'_>) {
132         let source_file = self.tcx.sess.local_crate_source_file.as_ref();
133         let crate_root = source_file.map(|source_file| {
134             let source_file = Path::new(source_file);
135             match source_file.file_name() {
136                 Some(_) => source_file.parent().unwrap().display(),
137                 None => source_file.display(),
138             }
139             .to_string()
140         });
141
142         let data = CratePreludeData {
143             crate_id: GlobalCrateId {
144                 name: name.into(),
145                 disambiguator: self
146                     .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.item.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)| match input {
182                     Input::File(ref path) if path == Path::new(&arg) => {
183                         let mapped = &self.tcx.sess.local_crate_source_file;
184                         mapped.as_ref().unwrap().to_string_lossy().into()
185                     }
186                     _ => arg,
187                 });
188
189             (args.next().unwrap(), args.collect())
190         };
191
192         let data = CompilationOptions {
193             directory: self.tcx.sess.working_dir.0.clone(),
194             program,
195             arguments,
196             output: self.save_ctxt.compilation_output(crate_name),
197         };
198
199         self.dumper.compilation_opts(data);
200     }
201
202     fn write_segments(&mut self, segments: impl IntoIterator<Item = &'tcx hir::PathSegment<'tcx>>) {
203         for seg in segments {
204             if let Some(data) = self.save_ctxt.get_path_segment_data(seg) {
205                 self.dumper.dump_ref(data);
206             }
207         }
208     }
209
210     fn write_sub_paths(&mut self, path: &'tcx hir::Path<'tcx>) {
211         self.write_segments(path.segments)
212     }
213
214     // As write_sub_paths, but does not process the last ident in the path (assuming it
215     // will be processed elsewhere). See note on write_sub_paths about global.
216     fn write_sub_paths_truncated(&mut self, path: &'tcx hir::Path<'tcx>) {
217         if let [segments @ .., _] = path.segments {
218             self.write_segments(segments)
219         }
220     }
221
222     fn process_formals(&mut self, formals: &'tcx [hir::Param<'tcx>], qualname: &str) {
223         for arg in formals {
224             self.visit_pat(&arg.pat);
225             let mut collector = PathCollector::new(self.tcx);
226             collector.visit_pat(&arg.pat);
227
228             for (hir_id, ident, ..) in collector.collected_idents {
229                 let typ = match self.save_ctxt.typeck_results().node_type_opt(hir_id) {
230                     Some(s) => s.to_string(),
231                     None => continue,
232                 };
233                 if !self.span.filter_generated(ident.span) {
234                     let id = id_from_hir_id(hir_id, &self.save_ctxt);
235                     let span = self.span_from_span(ident.span);
236
237                     self.dumper.dump_def(
238                         &Access { public: false, reachable: false },
239                         Def {
240                             kind: DefKind::Local,
241                             id,
242                             span,
243                             name: ident.to_string(),
244                             qualname: format!("{}::{}", qualname, ident.to_string()),
245                             value: typ,
246                             parent: None,
247                             children: vec![],
248                             decl_id: None,
249                             docs: String::new(),
250                             sig: None,
251                             attributes: vec![],
252                         },
253                     );
254                 }
255             }
256         }
257     }
258
259     fn process_method(
260         &mut self,
261         sig: &'tcx hir::FnSig<'tcx>,
262         body: Option<hir::BodyId>,
263         hir_id: hir::HirId,
264         ident: Ident,
265         generics: &'tcx hir::Generics<'tcx>,
266         vis: &hir::Visibility<'tcx>,
267         span: Span,
268     ) {
269         debug!("process_method: {}:{}", hir_id, ident);
270
271         let map = &self.tcx.hir();
272         self.nest_typeck_results(map.local_def_id(hir_id), |v| {
273             if let Some(mut method_data) = v.save_ctxt.get_method_data(hir_id, ident, span) {
274                 if let Some(body) = body {
275                     v.process_formals(map.body(body).params, &method_data.qualname);
276                 }
277                 v.process_generic_params(&generics, &method_data.qualname, hir_id);
278
279                 method_data.value =
280                     fn_to_string(sig.decl, sig.header, Some(ident.name), generics, vis, &[], None);
281                 method_data.sig = sig::method_signature(hir_id, ident, generics, sig, &v.save_ctxt);
282
283                 v.dumper.dump_def(&access_from_vis!(v.save_ctxt, vis, hir_id), method_data);
284             }
285
286             // walk arg and return types
287             for arg in sig.decl.inputs {
288                 v.visit_ty(arg);
289             }
290
291             if let hir::FnRetTy::Return(ref ret_ty) = sig.decl.output {
292                 v.visit_ty(ret_ty)
293             }
294
295             // walk the fn body
296             if let Some(body) = body {
297                 v.visit_expr(&map.body(body).value);
298             }
299         });
300     }
301
302     fn process_struct_field_def(
303         &mut self,
304         field: &'tcx hir::FieldDef<'tcx>,
305         parent_id: hir::HirId,
306     ) {
307         let field_data = self.save_ctxt.get_field_data(field, parent_id);
308         if let Some(field_data) = field_data {
309             self.dumper.dump_def(&access_from!(self.save_ctxt, field, field.hir_id), field_data);
310         }
311     }
312
313     // Dump generic params bindings, then visit_generics
314     fn process_generic_params(
315         &mut self,
316         generics: &'tcx hir::Generics<'tcx>,
317         prefix: &str,
318         id: hir::HirId,
319     ) {
320         for param in generics.params {
321             match param.kind {
322                 hir::GenericParamKind::Lifetime { .. } => {}
323                 hir::GenericParamKind::Type {
324                     synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
325                     ..
326                 } => {
327                     return self
328                         .nest_typeck_results(self.tcx.hir().local_def_id(param.hir_id), |this| {
329                             this.visit_generics(generics)
330                         });
331                 }
332                 hir::GenericParamKind::Type { .. } => {
333                     let param_ss = param.name.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_hir_id(param.hir_id, &self.save_ctxt);
339                         let span = self.span_from_span(param_ss);
340
341                         self.dumper.dump_def(
342                             &Access { public: false, reachable: false },
343                             Def {
344                                 kind: DefKind::Type,
345                                 id,
346                                 span,
347                                 name,
348                                 qualname,
349                                 value: String::new(),
350                                 parent: None,
351                                 children: vec![],
352                                 decl_id: None,
353                                 docs: String::new(),
354                                 sig: None,
355                                 attributes: vec![],
356                             },
357                         );
358                     }
359                 }
360                 hir::GenericParamKind::Const { .. } => {}
361             }
362         }
363
364         self.visit_generics(generics)
365     }
366
367     fn process_fn(
368         &mut self,
369         item: &'tcx hir::Item<'tcx>,
370         decl: &'tcx hir::FnDecl<'tcx>,
371         _header: &'tcx hir::FnHeader,
372         ty_params: &'tcx hir::Generics<'tcx>,
373         body: hir::BodyId,
374     ) {
375         let map = &self.tcx.hir();
376         self.nest_typeck_results(item.def_id, |v| {
377             let body = map.body(body);
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(body.params, &fn_data.qualname);
381                 v.process_generic_params(ty_params, &fn_data.qualname, item.hir_id());
382
383                 v.dumper.dump_def(&access_from!(v.save_ctxt, item, item.hir_id()), fn_data);
384             }
385
386             for arg in decl.inputs {
387                 v.visit_ty(arg)
388             }
389
390             if let hir::FnRetTy::Return(ref ret_ty) = decl.output {
391                 v.visit_ty(ret_ty)
392             }
393
394             v.visit_expr(&body.value);
395         });
396     }
397
398     fn process_static_or_const_item(
399         &mut self,
400         item: &'tcx hir::Item<'tcx>,
401         typ: &'tcx hir::Ty<'tcx>,
402         expr: &'tcx hir::Expr<'tcx>,
403     ) {
404         self.nest_typeck_results(item.def_id, |v| {
405             if let Some(var_data) = v.save_ctxt.get_item_data(item) {
406                 down_cast_data!(var_data, DefData, item.span);
407                 v.dumper.dump_def(&access_from!(v.save_ctxt, item, item.hir_id()), var_data);
408             }
409             v.visit_ty(&typ);
410             v.visit_expr(expr);
411         });
412     }
413
414     fn process_assoc_const(
415         &mut self,
416         hir_id: hir::HirId,
417         ident: Ident,
418         typ: &'tcx hir::Ty<'tcx>,
419         expr: Option<&'tcx hir::Expr<'tcx>>,
420         parent_id: DefId,
421         vis: &hir::Visibility<'tcx>,
422         attrs: &'tcx [ast::Attribute],
423     ) {
424         let qualname =
425             format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(hir_id).to_def_id()));
426
427         if !self.span.filter_generated(ident.span) {
428             let sig = sig::assoc_const_signature(hir_id, ident.name, typ, expr, &self.save_ctxt);
429             let span = self.span_from_span(ident.span);
430
431             self.dumper.dump_def(
432                 &access_from_vis!(self.save_ctxt, vis, hir_id),
433                 Def {
434                     kind: DefKind::Const,
435                     id: id_from_hir_id(hir_id, &self.save_ctxt),
436                     span,
437                     name: ident.name.to_string(),
438                     qualname,
439                     value: ty_to_string(&typ),
440                     parent: Some(id_from_def_id(parent_id)),
441                     children: vec![],
442                     decl_id: None,
443                     docs: self.save_ctxt.docs_for_attrs(attrs),
444                     sig,
445                     attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt),
446                 },
447             );
448         }
449
450         // walk type and init value
451         self.nest_typeck_results(self.tcx.hir().local_def_id(hir_id), |v| {
452             v.visit_ty(typ);
453             if let Some(expr) = expr {
454                 v.visit_expr(expr);
455             }
456         });
457     }
458
459     // FIXME tuple structs should generate tuple-specific data.
460     fn process_struct(
461         &mut self,
462         item: &'tcx hir::Item<'tcx>,
463         def: &'tcx hir::VariantData<'tcx>,
464         ty_params: &'tcx hir::Generics<'tcx>,
465     ) {
466         debug!("process_struct {:?} {:?}", item, item.span);
467         let name = item.ident.to_string();
468         let qualname = format!("::{}", self.tcx.def_path_str(item.def_id.to_def_id()));
469
470         let kind = match item.kind {
471             hir::ItemKind::Struct(_, _) => DefKind::Struct,
472             hir::ItemKind::Union(_, _) => DefKind::Union,
473             _ => unreachable!(),
474         };
475
476         let (value, fields) = match item.kind {
477             hir::ItemKind::Struct(hir::VariantData::Struct(ref fields, ..), ..)
478             | hir::ItemKind::Union(hir::VariantData::Struct(ref fields, ..), ..) => {
479                 let include_priv_fields = !self.save_ctxt.config.pub_only;
480                 let fields_str = fields
481                     .iter()
482                     .filter_map(|f| {
483                         if include_priv_fields || f.vis.node.is_pub() {
484                             Some(f.ident.to_string())
485                         } else {
486                             None
487                         }
488                     })
489                     .collect::<Vec<_>>()
490                     .join(", ");
491                 let value = format!("{} {{ {} }}", name, fields_str);
492                 (value, fields.iter().map(|f| id_from_hir_id(f.hir_id, &self.save_ctxt)).collect())
493             }
494             _ => (String::new(), vec![]),
495         };
496
497         if !self.span.filter_generated(item.ident.span) {
498             let span = self.span_from_span(item.ident.span);
499             let attrs = self.tcx.hir().attrs(item.hir_id());
500             self.dumper.dump_def(
501                 &access_from!(self.save_ctxt, item, item.hir_id()),
502                 Def {
503                     kind,
504                     id: id_from_def_id(item.def_id.to_def_id()),
505                     span,
506                     name,
507                     qualname: qualname.clone(),
508                     value,
509                     parent: None,
510                     children: fields,
511                     decl_id: None,
512                     docs: self.save_ctxt.docs_for_attrs(attrs),
513                     sig: sig::item_signature(item, &self.save_ctxt),
514                     attributes: lower_attributes(attrs.to_vec(), &self.save_ctxt),
515                 },
516             );
517         }
518
519         self.nest_typeck_results(item.def_id, |v| {
520             for field in def.fields() {
521                 v.process_struct_field_def(field, item.hir_id());
522                 v.visit_ty(&field.ty);
523             }
524
525             v.process_generic_params(ty_params, &qualname, item.hir_id());
526         });
527     }
528
529     fn process_enum(
530         &mut self,
531         item: &'tcx hir::Item<'tcx>,
532         enum_definition: &'tcx hir::EnumDef<'tcx>,
533         ty_params: &'tcx hir::Generics<'tcx>,
534     ) {
535         let enum_data = self.save_ctxt.get_item_data(item);
536         let enum_data = match enum_data {
537             None => return,
538             Some(data) => data,
539         };
540         down_cast_data!(enum_data, DefData, item.span);
541
542         let access = access_from!(self.save_ctxt, item, item.hir_id());
543
544         for variant in enum_definition.variants {
545             let name = variant.ident.name.to_string();
546             let qualname = format!("{}::{}", enum_data.qualname, name);
547             let name_span = variant.ident.span;
548
549             match variant.data {
550                 hir::VariantData::Struct(ref fields, ..) => {
551                     let fields_str =
552                         fields.iter().map(|f| f.ident.to_string()).collect::<Vec<_>>().join(", ");
553                     let value = format!("{}::{} {{ {} }}", enum_data.name, name, fields_str);
554                     if !self.span.filter_generated(name_span) {
555                         let span = self.span_from_span(name_span);
556                         let id = id_from_hir_id(variant.id, &self.save_ctxt);
557                         let parent = Some(id_from_def_id(item.def_id.to_def_id()));
558                         let attrs = self.tcx.hir().attrs(variant.id);
559
560                         self.dumper.dump_def(
561                             &access,
562                             Def {
563                                 kind: DefKind::StructVariant,
564                                 id,
565                                 span,
566                                 name,
567                                 qualname,
568                                 value,
569                                 parent,
570                                 children: vec![],
571                                 decl_id: None,
572                                 docs: self.save_ctxt.docs_for_attrs(attrs),
573                                 sig: sig::variant_signature(variant, &self.save_ctxt),
574                                 attributes: lower_attributes(attrs.to_vec(), &self.save_ctxt),
575                             },
576                         );
577                     }
578                 }
579                 ref v => {
580                     let mut value = format!("{}::{}", enum_data.name, name);
581                     if let hir::VariantData::Tuple(fields, _) = v {
582                         value.push('(');
583                         value.push_str(
584                             &fields
585                                 .iter()
586                                 .map(|f| ty_to_string(&f.ty))
587                                 .collect::<Vec<_>>()
588                                 .join(", "),
589                         );
590                         value.push(')');
591                     }
592                     if !self.span.filter_generated(name_span) {
593                         let span = self.span_from_span(name_span);
594                         let id = id_from_hir_id(variant.id, &self.save_ctxt);
595                         let parent = Some(id_from_def_id(item.def_id.to_def_id()));
596                         let attrs = self.tcx.hir().attrs(variant.id);
597
598                         self.dumper.dump_def(
599                             &access,
600                             Def {
601                                 kind: DefKind::TupleVariant,
602                                 id,
603                                 span,
604                                 name,
605                                 qualname,
606                                 value,
607                                 parent,
608                                 children: vec![],
609                                 decl_id: None,
610                                 docs: self.save_ctxt.docs_for_attrs(attrs),
611                                 sig: sig::variant_signature(variant, &self.save_ctxt),
612                                 attributes: lower_attributes(attrs.to_vec(), &self.save_ctxt),
613                             },
614                         );
615                     }
616                 }
617             }
618
619             for field in variant.data.fields() {
620                 self.process_struct_field_def(field, variant.id);
621                 self.visit_ty(field.ty);
622             }
623         }
624         self.process_generic_params(ty_params, &enum_data.qualname, item.hir_id());
625         self.dumper.dump_def(&access, enum_data);
626     }
627
628     fn process_impl(&mut self, item: &'tcx hir::Item<'tcx>, impl_: &'tcx hir::Impl<'tcx>) {
629         if let Some(impl_data) = self.save_ctxt.get_item_data(item) {
630             if !self.span.filter_generated(item.span) {
631                 if let super::Data::RelationData(rel, imp) = impl_data {
632                     self.dumper.dump_relation(rel);
633                     self.dumper.dump_impl(imp);
634                 } else {
635                     span_bug!(item.span, "unexpected data kind: {:?}", impl_data);
636                 }
637             }
638         }
639
640         let map = &self.tcx.hir();
641         self.nest_typeck_results(item.def_id, |v| {
642             v.visit_ty(&impl_.self_ty);
643             if let Some(trait_ref) = &impl_.of_trait {
644                 v.process_path(trait_ref.hir_ref_id, &hir::QPath::Resolved(None, &trait_ref.path));
645             }
646             v.process_generic_params(&impl_.generics, "", item.hir_id());
647             for impl_item in impl_.items {
648                 v.process_impl_item(map.impl_item(impl_item.id), item.def_id.to_def_id());
649             }
650         });
651     }
652
653     fn process_trait(
654         &mut self,
655         item: &'tcx hir::Item<'tcx>,
656         generics: &'tcx hir::Generics<'tcx>,
657         trait_refs: hir::GenericBounds<'tcx>,
658         methods: &'tcx [hir::TraitItemRef],
659     ) {
660         let name = item.ident.to_string();
661         let qualname = format!("::{}", self.tcx.def_path_str(item.def_id.to_def_id()));
662         let mut val = name.clone();
663         if !generics.params.is_empty() {
664             val.push_str(&generic_params_to_string(generics.params));
665         }
666         if !trait_refs.is_empty() {
667             val.push_str(": ");
668             val.push_str(&bounds_to_string(trait_refs));
669         }
670         if !self.span.filter_generated(item.ident.span) {
671             let id = id_from_def_id(item.def_id.to_def_id());
672             let span = self.span_from_span(item.ident.span);
673             let children =
674                 methods.iter().map(|i| id_from_def_id(i.id.def_id.to_def_id())).collect();
675             let attrs = self.tcx.hir().attrs(item.hir_id());
676             self.dumper.dump_def(
677                 &access_from!(self.save_ctxt, item, item.hir_id()),
678                 Def {
679                     kind: DefKind::Trait,
680                     id,
681                     span,
682                     name,
683                     qualname: qualname.clone(),
684                     value: val,
685                     parent: None,
686                     children,
687                     decl_id: None,
688                     docs: self.save_ctxt.docs_for_attrs(attrs),
689                     sig: sig::item_signature(item, &self.save_ctxt),
690                     attributes: lower_attributes(attrs.to_vec(), &self.save_ctxt),
691                 },
692             );
693         }
694
695         // super-traits
696         for super_bound in trait_refs.iter() {
697             let (def_id, sub_span) = match *super_bound {
698                 hir::GenericBound::Trait(ref trait_ref, _) => (
699                     self.lookup_def_id(trait_ref.trait_ref.hir_ref_id),
700                     trait_ref.trait_ref.path.segments.last().unwrap().ident.span,
701                 ),
702                 hir::GenericBound::LangItemTrait(lang_item, span, _, _) => {
703                     (Some(self.tcx.require_lang_item(lang_item, Some(span))), span)
704                 }
705                 hir::GenericBound::Outlives(..) => continue,
706             };
707
708             if let Some(id) = def_id {
709                 if !self.span.filter_generated(sub_span) {
710                     let span = self.span_from_span(sub_span);
711                     self.dumper.dump_ref(Ref {
712                         kind: RefKind::Type,
713                         span: span.clone(),
714                         ref_id: id_from_def_id(id),
715                     });
716
717                     self.dumper.dump_relation(Relation {
718                         kind: RelationKind::SuperTrait,
719                         span,
720                         from: id_from_def_id(id),
721                         to: id_from_def_id(item.def_id.to_def_id()),
722                     });
723                 }
724             }
725         }
726
727         // walk generics and methods
728         self.process_generic_params(generics, &qualname, item.hir_id());
729         for method in methods {
730             let map = &self.tcx.hir();
731             self.process_trait_item(map.trait_item(method.id), item.def_id.to_def_id())
732         }
733     }
734
735     // `item` is the module in question, represented as an( item.
736     fn process_mod(&mut self, item: &'tcx hir::Item<'tcx>) {
737         if let Some(mod_data) = self.save_ctxt.get_item_data(item) {
738             down_cast_data!(mod_data, DefData, item.span);
739             self.dumper.dump_def(&access_from!(self.save_ctxt, item, item.hir_id()), mod_data);
740         }
741     }
742
743     fn dump_path_ref(&mut self, id: hir::HirId, path: &hir::QPath<'tcx>) {
744         let path_data = self.save_ctxt.get_path_data(id, path);
745         if let Some(path_data) = path_data {
746             self.dumper.dump_ref(path_data);
747         }
748     }
749
750     fn dump_path_segment_ref(&mut self, id: hir::HirId, segment: &hir::PathSegment<'tcx>) {
751         let segment_data = self.save_ctxt.get_path_segment_data_with_id(segment, id);
752         if let Some(segment_data) = segment_data {
753             self.dumper.dump_ref(segment_data);
754         }
755     }
756
757     fn process_path(&mut self, id: hir::HirId, path: &hir::QPath<'tcx>) {
758         if self.span.filter_generated(path.span()) {
759             return;
760         }
761         self.dump_path_ref(id, path);
762
763         // Type arguments
764         let segments = match path {
765             hir::QPath::Resolved(ty, path) => {
766                 if let Some(ty) = ty {
767                     self.visit_ty(ty);
768                 }
769                 path.segments
770             }
771             hir::QPath::TypeRelative(ty, segment) => {
772                 self.visit_ty(ty);
773                 std::slice::from_ref(*segment)
774             }
775             hir::QPath::LangItem(..) => return,
776         };
777         for seg in segments {
778             if let Some(ref generic_args) = seg.args {
779                 for arg in generic_args.args {
780                     if let hir::GenericArg::Type(ref ty) = arg {
781                         self.visit_ty(ty);
782                     }
783                 }
784             }
785         }
786
787         if let hir::QPath::Resolved(_, path) = path {
788             self.write_sub_paths_truncated(path);
789         }
790     }
791
792     fn process_struct_lit(
793         &mut self,
794         ex: &'tcx hir::Expr<'tcx>,
795         path: &'tcx hir::QPath<'tcx>,
796         fields: &'tcx [hir::ExprField<'tcx>],
797         variant: &'tcx ty::VariantDef,
798         rest: Option<&'tcx hir::Expr<'tcx>>,
799     ) {
800         if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) {
801             if let hir::QPath::Resolved(_, path) = path {
802                 self.write_sub_paths_truncated(path);
803             }
804             down_cast_data!(struct_lit_data, RefData, ex.span);
805             if !generated_code(ex.span) {
806                 self.dumper.dump_ref(struct_lit_data);
807             }
808
809             for field in fields {
810                 if let Some(field_data) = self.save_ctxt.get_field_ref_data(field, variant) {
811                     self.dumper.dump_ref(field_data);
812                 }
813
814                 self.visit_expr(&field.expr)
815             }
816         }
817
818         if let Some(base) = rest {
819             self.visit_expr(&base);
820         }
821     }
822
823     fn process_method_call(
824         &mut self,
825         ex: &'tcx hir::Expr<'tcx>,
826         seg: &'tcx hir::PathSegment<'tcx>,
827         args: &'tcx [hir::Expr<'tcx>],
828     ) {
829         debug!("process_method_call {:?} {:?}", ex, ex.span);
830         if let Some(mcd) = self.save_ctxt.get_expr_data(ex) {
831             down_cast_data!(mcd, RefData, ex.span);
832             if !generated_code(ex.span) {
833                 self.dumper.dump_ref(mcd);
834             }
835         }
836
837         // Explicit types in the turbo-fish.
838         if let Some(generic_args) = seg.args {
839             for arg in generic_args.args {
840                 if let hir::GenericArg::Type(ty) = arg {
841                     self.visit_ty(&ty)
842                 };
843             }
844         }
845
846         // walk receiver and args
847         walk_list!(self, visit_expr, args);
848     }
849
850     fn process_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
851         match p.kind {
852             hir::PatKind::Struct(ref _path, fields, _) => {
853                 // FIXME do something with _path?
854                 let adt = match self.save_ctxt.typeck_results().node_type_opt(p.hir_id) {
855                     Some(ty) if ty.ty_adt_def().is_some() => ty.ty_adt_def().unwrap(),
856                     _ => {
857                         intravisit::walk_pat(self, p);
858                         return;
859                     }
860                 };
861                 let variant = adt.variant_of_res(self.save_ctxt.get_path_res(p.hir_id));
862
863                 for field in fields {
864                     if let Some(index) = self.tcx.find_field_index(field.ident, variant) {
865                         if !self.span.filter_generated(field.ident.span) {
866                             let span = self.span_from_span(field.ident.span);
867                             self.dumper.dump_ref(Ref {
868                                 kind: RefKind::Variable,
869                                 span,
870                                 ref_id: id_from_def_id(variant.fields[index].did),
871                             });
872                         }
873                     }
874                     self.visit_pat(&field.pat);
875                 }
876             }
877             _ => intravisit::walk_pat(self, p),
878         }
879     }
880
881     fn process_var_decl(&mut self, pat: &'tcx hir::Pat<'tcx>) {
882         // The pattern could declare multiple new vars,
883         // we must walk the pattern and collect them all.
884         let mut collector = PathCollector::new(self.tcx);
885         collector.visit_pat(&pat);
886         self.visit_pat(&pat);
887
888         // Process collected paths.
889         for (id, ident, _) in collector.collected_idents {
890             let res = self.save_ctxt.get_path_res(id);
891             match res {
892                 Res::Local(hir_id) => {
893                     let typ = self
894                         .save_ctxt
895                         .typeck_results()
896                         .node_type_opt(hir_id)
897                         .map(|t| t.to_string())
898                         .unwrap_or_default();
899
900                     // Rust uses the id of the pattern for var lookups, so we'll use it too.
901                     if !self.span.filter_generated(ident.span) {
902                         let qualname = format!("{}${}", ident.to_string(), hir_id);
903                         let id = id_from_hir_id(hir_id, &self.save_ctxt);
904                         let span = self.span_from_span(ident.span);
905
906                         self.dumper.dump_def(
907                             &Access { public: false, reachable: false },
908                             Def {
909                                 kind: DefKind::Local,
910                                 id,
911                                 span,
912                                 name: ident.to_string(),
913                                 qualname,
914                                 value: typ,
915                                 parent: None,
916                                 children: vec![],
917                                 decl_id: None,
918                                 docs: String::new(),
919                                 sig: None,
920                                 attributes: vec![],
921                             },
922                         );
923                     }
924                 }
925                 Res::Def(
926                     HirDefKind::Ctor(..)
927                     | HirDefKind::Const
928                     | HirDefKind::AssocConst
929                     | HirDefKind::Struct
930                     | HirDefKind::Variant
931                     | HirDefKind::TyAlias
932                     | HirDefKind::AssocTy,
933                     _,
934                 )
935                 | Res::SelfTy(..) => {
936                     self.dump_path_segment_ref(id, &hir::PathSegment::from_ident(ident));
937                 }
938                 def => {
939                     error!("unexpected definition kind when processing collected idents: {:?}", def)
940                 }
941             }
942         }
943
944         for (id, ref path) in collector.collected_paths {
945             self.process_path(id, path);
946         }
947     }
948
949     /// Extracts macro use and definition information from the AST node defined
950     /// by the given NodeId, using the expansion information from the node's
951     /// span.
952     ///
953     /// If the span is not macro-generated, do nothing, else use callee and
954     /// callsite spans to record macro definition and use data, using the
955     /// mac_uses and mac_defs sets to prevent multiples.
956     fn process_macro_use(&mut self, _span: Span) {
957         // FIXME if we're not dumping the defs (see below), there is no point
958         // dumping refs either.
959         // let source_span = span.source_callsite();
960         // if !self.macro_calls.insert(source_span) {
961         //     return;
962         // }
963
964         // let data = match self.save_ctxt.get_macro_use_data(span) {
965         //     None => return,
966         //     Some(data) => data,
967         // };
968
969         // self.dumper.macro_use(data);
970
971         // FIXME write the macro def
972         // let mut hasher = DefaultHasher::new();
973         // data.callee_span.hash(&mut hasher);
974         // let hash = hasher.finish();
975         // let qualname = format!("{}::{}", data.name, hash);
976         // Don't write macro definition for imported macros
977         // if !self.mac_defs.contains(&data.callee_span)
978         //     && !data.imported {
979         //     self.mac_defs.insert(data.callee_span);
980         //     if let Some(sub_span) = self.span.span_for_macro_def_name(data.callee_span) {
981         //         self.dumper.macro_data(MacroData {
982         //             span: sub_span,
983         //             name: data.name.clone(),
984         //             qualname: qualname.clone(),
985         //             // FIXME where do macro docs come from?
986         //             docs: String::new(),
987         //         }.lower(self.tcx));
988         //     }
989         // }
990     }
991
992     fn process_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>, trait_id: DefId) {
993         self.process_macro_use(trait_item.span);
994         let vis_span = trait_item.span.shrink_to_lo();
995         match trait_item.kind {
996             hir::TraitItemKind::Const(ref ty, body) => {
997                 let body = body.map(|b| &self.tcx.hir().body(b).value);
998                 let respan = respan(vis_span, hir::VisibilityKind::Public);
999                 let attrs = self.tcx.hir().attrs(trait_item.hir_id());
1000                 self.process_assoc_const(
1001                     trait_item.hir_id(),
1002                     trait_item.ident,
1003                     &ty,
1004                     body,
1005                     trait_id,
1006                     &respan,
1007                     attrs,
1008                 );
1009             }
1010             hir::TraitItemKind::Fn(ref sig, ref trait_fn) => {
1011                 let body =
1012                     if let hir::TraitFn::Provided(body) = trait_fn { Some(*body) } else { None };
1013                 let respan = respan(vis_span, hir::VisibilityKind::Public);
1014                 self.process_method(
1015                     sig,
1016                     body,
1017                     trait_item.hir_id(),
1018                     trait_item.ident,
1019                     &trait_item.generics,
1020                     &respan,
1021                     trait_item.span,
1022                 );
1023             }
1024             hir::TraitItemKind::Type(ref bounds, ref default_ty) => {
1025                 // FIXME do something with _bounds (for type refs)
1026                 let name = trait_item.ident.name.to_string();
1027                 let qualname =
1028                     format!("::{}", self.tcx.def_path_str(trait_item.def_id.to_def_id()));
1029
1030                 if !self.span.filter_generated(trait_item.ident.span) {
1031                     let span = self.span_from_span(trait_item.ident.span);
1032                     let id = id_from_def_id(trait_item.def_id.to_def_id());
1033                     let attrs = self.tcx.hir().attrs(trait_item.hir_id());
1034
1035                     self.dumper.dump_def(
1036                         &Access { public: true, reachable: true },
1037                         Def {
1038                             kind: DefKind::Type,
1039                             id,
1040                             span,
1041                             name,
1042                             qualname,
1043                             value: self.span.snippet(trait_item.span),
1044                             parent: Some(id_from_def_id(trait_id)),
1045                             children: vec![],
1046                             decl_id: None,
1047                             docs: self.save_ctxt.docs_for_attrs(attrs),
1048                             sig: sig::assoc_type_signature(
1049                                 trait_item.hir_id(),
1050                                 trait_item.ident,
1051                                 Some(bounds),
1052                                 default_ty.as_ref().map(|ty| &**ty),
1053                                 &self.save_ctxt,
1054                             ),
1055                             attributes: lower_attributes(attrs.to_vec(), &self.save_ctxt),
1056                         },
1057                     );
1058                 }
1059
1060                 if let Some(default_ty) = default_ty {
1061                     self.visit_ty(default_ty)
1062                 }
1063             }
1064         }
1065     }
1066
1067     fn process_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>, impl_id: DefId) {
1068         self.process_macro_use(impl_item.span);
1069         match impl_item.kind {
1070             hir::ImplItemKind::Const(ref ty, body) => {
1071                 let body = self.tcx.hir().body(body);
1072                 let attrs = self.tcx.hir().attrs(impl_item.hir_id());
1073                 self.process_assoc_const(
1074                     impl_item.hir_id(),
1075                     impl_item.ident,
1076                     &ty,
1077                     Some(&body.value),
1078                     impl_id,
1079                     &impl_item.vis,
1080                     attrs,
1081                 );
1082             }
1083             hir::ImplItemKind::Fn(ref sig, body) => {
1084                 self.process_method(
1085                     sig,
1086                     Some(body),
1087                     impl_item.hir_id(),
1088                     impl_item.ident,
1089                     &impl_item.generics,
1090                     &impl_item.vis,
1091                     impl_item.span,
1092                 );
1093             }
1094             hir::ImplItemKind::TyAlias(ref ty) => {
1095                 // FIXME: uses of the assoc type should ideally point to this
1096                 // 'def' and the name here should be a ref to the def in the
1097                 // trait.
1098                 self.visit_ty(ty)
1099             }
1100         }
1101     }
1102
1103     pub(crate) fn process_crate(&mut self, krate: &'tcx hir::Crate<'tcx>) {
1104         let id = hir::CRATE_HIR_ID;
1105         let qualname =
1106             format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(id).to_def_id()));
1107
1108         let sm = self.tcx.sess.source_map();
1109         let filename = sm.span_to_filename(krate.item.span);
1110         let data_id = id_from_hir_id(id, &self.save_ctxt);
1111         let children = krate
1112             .item
1113             .module
1114             .item_ids
1115             .iter()
1116             .map(|i| id_from_def_id(i.def_id.to_def_id()))
1117             .collect();
1118         let span = self.span_from_span(krate.item.span);
1119         let attrs = self.tcx.hir().attrs(id);
1120
1121         self.dumper.dump_def(
1122             &Access { public: true, reachable: true },
1123             Def {
1124                 kind: DefKind::Mod,
1125                 id: data_id,
1126                 name: String::new(),
1127                 qualname,
1128                 span,
1129                 value: filename.to_string(),
1130                 children,
1131                 parent: None,
1132                 decl_id: None,
1133                 docs: self.save_ctxt.docs_for_attrs(attrs),
1134                 sig: None,
1135                 attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt),
1136             },
1137         );
1138         intravisit::walk_crate(self, krate);
1139     }
1140
1141     fn process_bounds(&mut self, bounds: hir::GenericBounds<'tcx>) {
1142         for bound in bounds {
1143             if let hir::GenericBound::Trait(ref trait_ref, _) = *bound {
1144                 self.process_path(
1145                     trait_ref.trait_ref.hir_ref_id,
1146                     &hir::QPath::Resolved(None, &trait_ref.trait_ref.path),
1147                 )
1148             }
1149         }
1150     }
1151 }
1152
1153 impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
1154     type Map = Map<'tcx>;
1155
1156     fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
1157         intravisit::NestedVisitorMap::All(self.tcx.hir())
1158     }
1159
1160     fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
1161         self.process_macro_use(item.span);
1162         match item.kind {
1163             hir::ItemKind::Use(path, hir::UseKind::Single) => {
1164                 let sub_span = path.segments.last().unwrap().ident.span;
1165                 if !self.span.filter_generated(sub_span) {
1166                     let access = access_from!(self.save_ctxt, item, item.hir_id());
1167                     let ref_id = self.lookup_def_id(item.hir_id()).map(id_from_def_id);
1168                     let span = self.span_from_span(sub_span);
1169                     let parent =
1170                         self.save_ctxt.tcx.parent(item.def_id.to_def_id()).map(id_from_def_id);
1171                     self.dumper.import(
1172                         &access,
1173                         Import {
1174                             kind: ImportKind::Use,
1175                             ref_id,
1176                             span,
1177                             alias_span: None,
1178                             name: item.ident.to_string(),
1179                             value: String::new(),
1180                             parent,
1181                         },
1182                     );
1183                     self.write_sub_paths_truncated(&path);
1184                 }
1185             }
1186             hir::ItemKind::Use(path, hir::UseKind::Glob) => {
1187                 // Make a comma-separated list of names of imported modules.
1188                 let names = self.tcx.names_imported_by_glob_use(item.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) = self.span.sub_span_of_star(item.span) {
1194                     if !self.span.filter_generated(item.span) {
1195                         let access = access_from!(self.save_ctxt, item, item.hir_id());
1196                         let span = self.span_from_span(sub_span);
1197                         let parent =
1198                             self.save_ctxt.tcx.parent(item.def_id.to_def_id()).map(id_from_def_id);
1199                         self.dumper.import(
1200                             &access,
1201                             Import {
1202                                 kind: ImportKind::GlobUse,
1203                                 ref_id: None,
1204                                 span,
1205                                 alias_span: None,
1206                                 name: "*".to_owned(),
1207                                 value: names.join(", "),
1208                                 parent,
1209                             },
1210                         );
1211                         self.write_sub_paths(&path);
1212                     }
1213                 }
1214             }
1215             hir::ItemKind::ExternCrate(_) => {
1216                 let name_span = item.ident.span;
1217                 if !self.span.filter_generated(name_span) {
1218                     let span = self.span_from_span(name_span);
1219                     let parent =
1220                         self.save_ctxt.tcx.parent(item.def_id.to_def_id()).map(id_from_def_id);
1221                     self.dumper.import(
1222                         &Access { public: false, reachable: false },
1223                         Import {
1224                             kind: ImportKind::ExternCrate,
1225                             ref_id: None,
1226                             span,
1227                             alias_span: None,
1228                             name: item.ident.to_string(),
1229                             value: String::new(),
1230                             parent,
1231                         },
1232                     );
1233                 }
1234             }
1235             hir::ItemKind::Fn(ref sig, ref ty_params, body) => {
1236                 self.process_fn(item, sig.decl, &sig.header, ty_params, body)
1237             }
1238             hir::ItemKind::Static(ref typ, _, body) => {
1239                 let body = self.tcx.hir().body(body);
1240                 self.process_static_or_const_item(item, typ, &body.value)
1241             }
1242             hir::ItemKind::Const(ref typ, body) => {
1243                 let body = self.tcx.hir().body(body);
1244                 self.process_static_or_const_item(item, typ, &body.value)
1245             }
1246             hir::ItemKind::Struct(ref def, ref ty_params)
1247             | hir::ItemKind::Union(ref def, ref ty_params) => {
1248                 self.process_struct(item, def, ty_params)
1249             }
1250             hir::ItemKind::Enum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
1251             hir::ItemKind::Impl(ref impl_) => self.process_impl(item, impl_),
1252             hir::ItemKind::Trait(_, _, ref generics, ref trait_refs, methods) => {
1253                 self.process_trait(item, generics, trait_refs, methods)
1254             }
1255             hir::ItemKind::Mod(ref m) => {
1256                 self.process_mod(item);
1257                 intravisit::walk_mod(self, m, item.hir_id());
1258             }
1259             hir::ItemKind::TyAlias(ty, ref generics) => {
1260                 let qualname = format!("::{}", self.tcx.def_path_str(item.def_id.to_def_id()));
1261                 let value = ty_to_string(&ty);
1262                 if !self.span.filter_generated(item.ident.span) {
1263                     let span = self.span_from_span(item.ident.span);
1264                     let id = id_from_def_id(item.def_id.to_def_id());
1265                     let attrs = self.tcx.hir().attrs(item.hir_id());
1266
1267                     self.dumper.dump_def(
1268                         &access_from!(self.save_ctxt, item, item.hir_id()),
1269                         Def {
1270                             kind: DefKind::Type,
1271                             id,
1272                             span,
1273                             name: item.ident.to_string(),
1274                             qualname: qualname.clone(),
1275                             value,
1276                             parent: None,
1277                             children: vec![],
1278                             decl_id: None,
1279                             docs: self.save_ctxt.docs_for_attrs(attrs),
1280                             sig: sig::item_signature(item, &self.save_ctxt),
1281                             attributes: lower_attributes(attrs.to_vec(), &self.save_ctxt),
1282                         },
1283                     );
1284                 }
1285
1286                 self.visit_ty(ty);
1287                 self.process_generic_params(generics, &qualname, item.hir_id());
1288             }
1289             _ => intravisit::walk_item(self, item),
1290         }
1291     }
1292
1293     fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
1294         for param in generics.params {
1295             match param.kind {
1296                 hir::GenericParamKind::Lifetime { .. } => {}
1297                 hir::GenericParamKind::Type { ref default, .. } => {
1298                     self.process_bounds(param.bounds);
1299                     if let Some(ref ty) = default {
1300                         self.visit_ty(ty);
1301                     }
1302                 }
1303                 hir::GenericParamKind::Const { ref ty, ref default } => {
1304                     self.process_bounds(param.bounds);
1305                     self.visit_ty(ty);
1306                     if let Some(default) = default {
1307                         self.visit_anon_const(default);
1308                     }
1309                 }
1310             }
1311         }
1312         for pred in generics.where_clause.predicates {
1313             if let hir::WherePredicate::BoundPredicate(ref wbp) = *pred {
1314                 self.process_bounds(wbp.bounds);
1315                 self.visit_ty(wbp.bounded_ty);
1316             }
1317         }
1318     }
1319
1320     fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx>) {
1321         self.process_macro_use(t.span);
1322         match t.kind {
1323             hir::TyKind::Path(ref path) => {
1324                 if generated_code(t.span) {
1325                     return;
1326                 }
1327
1328                 if let Some(id) = self.lookup_def_id(t.hir_id) {
1329                     let sub_span = path.last_segment_span();
1330                     let span = self.span_from_span(sub_span);
1331                     self.dumper.dump_ref(Ref {
1332                         kind: RefKind::Type,
1333                         span,
1334                         ref_id: id_from_def_id(id),
1335                     });
1336                 }
1337
1338                 if let hir::QPath::Resolved(_, path) = path {
1339                     self.write_sub_paths_truncated(path);
1340                 }
1341                 intravisit::walk_qpath(self, path, t.hir_id, t.span);
1342             }
1343             hir::TyKind::Array(ref ty, ref anon_const) => {
1344                 self.visit_ty(ty);
1345                 let map = self.tcx.hir();
1346                 self.nest_typeck_results(self.tcx.hir().local_def_id(anon_const.hir_id), |v| {
1347                     v.visit_expr(&map.body(anon_const.body).value)
1348                 });
1349             }
1350             hir::TyKind::OpaqueDef(item_id, _) => {
1351                 let item = self.tcx.hir().item(item_id);
1352                 self.nest_typeck_results(item_id.def_id, |v| v.visit_item(item));
1353             }
1354             _ => intravisit::walk_ty(self, t),
1355         }
1356     }
1357
1358     fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
1359         debug!("visit_expr {:?}", ex.kind);
1360         self.process_macro_use(ex.span);
1361         match ex.kind {
1362             hir::ExprKind::Struct(ref path, ref fields, ref rest) => {
1363                 let hir_expr = self.save_ctxt.tcx.hir().expect_expr(ex.hir_id);
1364                 let adt = match self.save_ctxt.typeck_results().expr_ty_opt(&hir_expr) {
1365                     Some(ty) if ty.ty_adt_def().is_some() => ty.ty_adt_def().unwrap(),
1366                     _ => {
1367                         intravisit::walk_expr(self, ex);
1368                         return;
1369                     }
1370                 };
1371                 let res = self.save_ctxt.get_path_res(hir_expr.hir_id);
1372                 self.process_struct_lit(ex, path, fields, adt.variant_of_res(res), *rest)
1373             }
1374             hir::ExprKind::MethodCall(ref seg, _, args, _) => {
1375                 self.process_method_call(ex, seg, args)
1376             }
1377             hir::ExprKind::Field(ref sub_ex, _) => {
1378                 self.visit_expr(&sub_ex);
1379
1380                 if let Some(field_data) = self.save_ctxt.get_expr_data(ex) {
1381                     down_cast_data!(field_data, RefData, ex.span);
1382                     if !generated_code(ex.span) {
1383                         self.dumper.dump_ref(field_data);
1384                     }
1385                 }
1386             }
1387             hir::ExprKind::Closure(_, ref decl, body, _fn_decl_span, _) => {
1388                 let id = format!("${}", ex.hir_id);
1389
1390                 // walk arg and return types
1391                 for ty in decl.inputs {
1392                     self.visit_ty(ty);
1393                 }
1394
1395                 if let hir::FnRetTy::Return(ref ret_ty) = decl.output {
1396                     self.visit_ty(ret_ty);
1397                 }
1398
1399                 // walk the body
1400                 let map = self.tcx.hir();
1401                 self.nest_typeck_results(self.tcx.hir().local_def_id(ex.hir_id), |v| {
1402                     let body = map.body(body);
1403                     v.process_formals(body.params, &id);
1404                     v.visit_expr(&body.value)
1405                 });
1406             }
1407             hir::ExprKind::Repeat(ref expr, ref anon_const) => {
1408                 self.visit_expr(expr);
1409                 let map = self.tcx.hir();
1410                 self.nest_typeck_results(self.tcx.hir().local_def_id(anon_const.hir_id), |v| {
1411                     v.visit_expr(&map.body(anon_const.body).value)
1412                 });
1413             }
1414             // In particular, we take this branch for call and path expressions,
1415             // where we'll index the idents involved just by continuing to walk.
1416             _ => intravisit::walk_expr(self, ex),
1417         }
1418     }
1419
1420     fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
1421         self.process_macro_use(p.span);
1422         self.process_pat(p);
1423     }
1424
1425     fn visit_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) {
1426         self.process_var_decl(&arm.pat);
1427         if let Some(hir::Guard::If(expr)) = &arm.guard {
1428             self.visit_expr(expr);
1429         }
1430         self.visit_expr(&arm.body);
1431     }
1432
1433     fn visit_qpath(&mut self, path: &'tcx hir::QPath<'tcx>, id: hir::HirId, _: Span) {
1434         self.process_path(id, path);
1435     }
1436
1437     fn visit_stmt(&mut self, s: &'tcx hir::Stmt<'tcx>) {
1438         self.process_macro_use(s.span);
1439         intravisit::walk_stmt(self, s)
1440     }
1441
1442     fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
1443         self.process_macro_use(l.span);
1444         self.process_var_decl(&l.pat);
1445
1446         // Just walk the initialiser and type (don't want to walk the pattern again).
1447         walk_list!(self, visit_ty, &l.ty);
1448         walk_list!(self, visit_expr, &l.init);
1449     }
1450
1451     fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'tcx>) {
1452         let access = access_from!(self.save_ctxt, item, item.hir_id());
1453
1454         match item.kind {
1455             hir::ForeignItemKind::Fn(decl, _, ref generics) => {
1456                 if let Some(fn_data) = self.save_ctxt.get_extern_item_data(item) {
1457                     down_cast_data!(fn_data, DefData, item.span);
1458
1459                     self.process_generic_params(generics, &fn_data.qualname, item.hir_id());
1460                     self.dumper.dump_def(&access, fn_data);
1461                 }
1462
1463                 for ty in decl.inputs {
1464                     self.visit_ty(ty);
1465                 }
1466
1467                 if let hir::FnRetTy::Return(ref ret_ty) = decl.output {
1468                     self.visit_ty(ret_ty);
1469                 }
1470             }
1471             hir::ForeignItemKind::Static(ref ty, _) => {
1472                 if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) {
1473                     down_cast_data!(var_data, DefData, item.span);
1474                     self.dumper.dump_def(&access, var_data);
1475                 }
1476
1477                 self.visit_ty(ty);
1478             }
1479             hir::ForeignItemKind::Type => {
1480                 if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) {
1481                     down_cast_data!(var_data, DefData, item.span);
1482                     self.dumper.dump_def(&access, var_data);
1483                 }
1484             }
1485         }
1486     }
1487 }