1 //! Write the output of rustc's analysis to an implementor of Dump.
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
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.
16 use rustc::hir::def::{Res, DefKind as HirDefKind};
17 use rustc::hir::def_id::DefId;
18 use rustc::session::config::Input;
20 use rustc::ty::{self, DefIdTree, TyCtxt};
21 use rustc_data_structures::fx::FxHashSet;
26 use syntax::ast::{self, Attribute, NodeId, PatKind};
28 use syntax::visit::{self, Visitor};
29 use syntax::print::pprust::{
31 generic_params_to_string,
35 use syntax::source_map::{DUMMY_SP, respan};
36 use syntax::walk_list;
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;
45 use rls_data::{CompilationOptions, CratePreludeData, Def, DefKind, GlobalCrateId, Import,
46 ImportKind, Ref, RefKind, Relation, RelationKind, SpanData};
48 use log::{debug, error};
50 macro_rules! down_cast_data {
51 ($id:ident, $kind:ident, $sp:expr) => {
52 let $id = if let super::Data::$kind(data) = $id {
55 span_bug!($sp, "unexpected data kind: {:?}", $id);
60 macro_rules! access_from {
61 ($save_ctxt:expr, $item:expr, $id:expr) => {
63 public: $item.vis.node.is_pub(),
64 reachable: $save_ctxt.access_levels.is_reachable($id),
69 macro_rules! access_from_vis {
70 ($save_ctxt:expr, $vis:expr, $id:expr) => {
72 public: $vis.node.is_pub(),
73 reachable: $save_ctxt.access_levels.is_reachable($id),
78 pub struct DumpVisitor<'l, 'tcx> {
79 pub save_ctxt: SaveContext<'l, 'tcx>,
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>,
93 impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
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());
104 // mac_defs: FxHashSet::default(),
105 // macro_calls: FxHashSet::default(),
109 pub fn analysis(&self) -> &rls_data::Analysis {
110 self.dumper.analysis()
113 fn nest_tables<F>(&mut self, item_id: NodeId, f: F)
115 F: FnOnce(&mut Self),
117 let item_def_id = self.tcx.hir().local_def_id_from_node_id(item_id);
119 let tables = if self.tcx.has_typeck_tables(item_def_id) {
120 self.tcx.typeck_tables_of(item_def_id)
122 self.save_ctxt.empty_tables
125 let old_tables = self.save_ctxt.tables;
126 self.save_ctxt.tables = tables;
128 self.save_ctxt.tables = old_tables;
131 fn span_from_span(&self, span: Span) -> SpanData {
132 self.save_ctxt.span_from_span(span)
135 fn lookup_def_id(&self, ref_id: NodeId) -> Option<DefId> {
136 self.save_ctxt.lookup_def_id(ref_id)
139 pub fn dump_crate_info(&mut self, name: &str, krate: &ast::Crate) {
140 let source_file = self.tcx.sess.local_crate_source_file.as_ref();
141 let crate_root = source_file.map(|source_file| {
142 let source_file = Path::new(source_file);
143 match source_file.file_name() {
144 Some(_) => source_file.parent().unwrap().display(),
145 None => source_file.display(),
149 let data = CratePreludeData {
150 crate_id: GlobalCrateId {
152 disambiguator: self.tcx
154 .local_crate_disambiguator()
158 crate_root: crate_root.unwrap_or_else(|| "<no source>".to_owned()),
159 external_crates: self.save_ctxt.get_external_crates(),
160 span: self.span_from_span(krate.span),
163 self.dumper.crate_prelude(data);
166 pub fn dump_compilation_options(&mut self, input: &Input, crate_name: &str) {
167 // Apply possible `remap-path-prefix` remapping to the input source file
168 // (and don't include remapping args anymore)
169 let (program, arguments) = {
170 let remap_arg_indices = {
171 let mut indices = FxHashSet::default();
172 // Args are guaranteed to be valid UTF-8 (checked early)
173 for (i, e) in env::args().enumerate() {
174 if e.starts_with("--remap-path-prefix=") {
176 } else if e == "--remap-path-prefix" {
178 indices.insert(i + 1);
184 let mut args = env::args()
186 .filter(|(i, _)| !remap_arg_indices.contains(i))
189 Input::File(ref path) if path == Path::new(&arg) => {
190 let mapped = &self.tcx.sess.local_crate_source_file;
201 (args.next().unwrap(), args.collect())
204 let data = CompilationOptions {
205 directory: self.tcx.sess.working_dir.0.clone(),
208 output: self.save_ctxt.compilation_output(crate_name),
211 self.dumper.compilation_opts(data);
214 fn write_sub_paths(&mut self, path: &ast::Path) {
215 for seg in &path.segments {
216 if let Some(data) = self.save_ctxt.get_path_segment_data(seg) {
217 self.dumper.dump_ref(data);
222 // As write_sub_paths, but does not process the last ident in the path (assuming it
223 // will be processed elsewhere). See note on write_sub_paths about global.
224 fn write_sub_paths_truncated(&mut self, path: &ast::Path) {
225 for seg in &path.segments[..path.segments.len() - 1] {
226 if let Some(data) = self.save_ctxt.get_path_segment_data(seg) {
227 self.dumper.dump_ref(data);
232 fn process_formals(&mut self, formals: &'l [ast::Param], qualname: &str) {
234 self.visit_pat(&arg.pat);
235 let mut collector = PathCollector::new();
236 collector.visit_pat(&arg.pat);
238 for (id, ident, ..) in collector.collected_idents {
239 let hir_id = self.tcx.hir().node_to_hir_id(id);
240 let typ = match self.save_ctxt.tables.node_type_opt(hir_id) {
241 Some(s) => s.to_string(),
244 if !self.span.filter_generated(ident.span) {
245 let id = id_from_node_id(id, &self.save_ctxt);
246 let span = self.span_from_span(ident.span);
248 self.dumper.dump_def(
254 kind: DefKind::Local,
257 name: ident.to_string(),
258 qualname: format!("{}::{}", qualname, ident.to_string()),
276 body: Option<&'l ast::Block>,
279 generics: &'l ast::Generics,
280 vis: ast::Visibility,
283 debug!("process_method: {}:{}", id, ident);
285 let hir_id = self.tcx.hir().node_to_hir_id(id);
286 self.nest_tables(id, |v| {
287 if let Some(mut method_data) = v.save_ctxt.get_method_data(id, ident, span) {
288 v.process_formals(&sig.decl.inputs, &method_data.qualname);
289 v.process_generic_params(&generics, &method_data.qualname, id);
291 method_data.value = crate::make_signature(&sig.decl, &generics);
292 method_data.sig = sig::method_signature(id, ident, generics, sig, &v.save_ctxt);
294 v.dumper.dump_def(&access_from_vis!(v.save_ctxt, vis, hir_id), method_data);
297 // walk arg and return types
298 for arg in &sig.decl.inputs {
302 if let ast::FunctionRetTy::Ty(ref ret_ty) = sig.decl.output {
303 // In async functions, return types are desugared and redefined
304 // as an `impl Trait` existential type. Because of this, to match
305 // the definition paths when resolving nested types we need to
306 // start walking from the newly-created definition.
307 match sig.header.asyncness.node {
308 ast::IsAsync::Async { return_impl_trait_id, .. } => {
309 v.nest_tables(return_impl_trait_id, |v| v.visit_ty(ret_ty))
311 _ => v.visit_ty(ret_ty)
316 if let Some(body) = body {
322 fn process_struct_field_def(&mut self, field: &ast::StructField, parent_id: NodeId) {
323 let field_data = self.save_ctxt.get_field_data(field, parent_id);
324 if let Some(field_data) = field_data {
325 let hir_id = self.tcx.hir().node_to_hir_id(field.id);
326 self.dumper.dump_def(&access_from!(self.save_ctxt, field, hir_id), field_data);
330 // Dump generic params bindings, then visit_generics
331 fn process_generic_params(
333 generics: &'l ast::Generics,
337 for param in &generics.params {
339 ast::GenericParamKind::Lifetime { .. } => {}
340 ast::GenericParamKind::Type { .. } => {
341 let param_ss = param.ident.span;
342 let name = escape(self.span.snippet(param_ss));
343 // Append $id to name to make sure each one is unique.
344 let qualname = format!("{}::{}${}", prefix, name, id);
345 if !self.span.filter_generated(param_ss) {
346 let id = id_from_node_id(param.id, &self.save_ctxt);
347 let span = self.span_from_span(param_ss);
349 self.dumper.dump_def(
360 value: String::new(),
371 ast::GenericParamKind::Const { .. } => {}
374 self.visit_generics(generics);
380 decl: &'l ast::FnDecl,
381 header: &'l ast::FnHeader,
382 ty_params: &'l ast::Generics,
383 body: &'l ast::Block,
385 let hir_id = self.tcx.hir().node_to_hir_id(item.id);
386 self.nest_tables(item.id, |v| {
387 if let Some(fn_data) = v.save_ctxt.get_item_data(item) {
388 down_cast_data!(fn_data, DefData, item.span);
389 v.process_formals(&decl.inputs, &fn_data.qualname);
390 v.process_generic_params(ty_params, &fn_data.qualname, item.id);
392 v.dumper.dump_def(&access_from!(v.save_ctxt, item, hir_id), fn_data);
395 for arg in &decl.inputs {
399 if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output {
400 if let ast::TyKind::ImplTrait(..) = ret_ty.kind {
401 // FIXME: Opaque type desugaring prevents us from easily
402 // processing trait bounds. See `visit_ty` for more details.
404 // In async functions, return types are desugared and redefined
405 // as an `impl Trait` existential type. Because of this, to match
406 // the definition paths when resolving nested types we need to
407 // start walking from the newly-created definition.
408 match header.asyncness.node {
409 ast::IsAsync::Async { return_impl_trait_id, .. } => {
410 v.nest_tables(return_impl_trait_id, |v| v.visit_ty(ret_ty))
412 _ => v.visit_ty(ret_ty)
417 v.visit_block(&body);
421 fn process_static_or_const_item(
427 let hir_id = self.tcx.hir().node_to_hir_id(item.id);
428 self.nest_tables(item.id, |v| {
429 if let Some(var_data) = v.save_ctxt.get_item_data(item) {
430 down_cast_data!(var_data, DefData, item.span);
431 v.dumper.dump_def(&access_from!(v.save_ctxt, item, hir_id), var_data);
438 fn process_assoc_const(
443 expr: Option<&'l ast::Expr>,
445 vis: ast::Visibility,
446 attrs: &'l [Attribute],
448 let qualname = format!("::{}",
449 self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(id)));
451 if !self.span.filter_generated(ident.span) {
452 let sig = sig::assoc_const_signature(id, ident.name, typ, expr, &self.save_ctxt);
453 let span = self.span_from_span(ident.span);
454 let hir_id = self.tcx.hir().node_to_hir_id(id);
456 self.dumper.dump_def(
457 &access_from_vis!(self.save_ctxt, vis, hir_id),
459 kind: DefKind::Const,
460 id: id_from_node_id(id, &self.save_ctxt),
462 name: ident.name.to_string(),
464 value: ty_to_string(&typ),
465 parent: Some(id_from_def_id(parent_id)),
468 docs: self.save_ctxt.docs_for_attrs(attrs),
470 attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt),
475 // walk type and init value
476 self.nest_tables(id, |v| {
478 if let Some(expr) = expr {
484 // FIXME tuple structs should generate tuple-specific data.
488 def: &'l ast::VariantData,
489 ty_params: &'l ast::Generics,
491 debug!("process_struct {:?} {:?}", item, item.span);
492 let name = item.ident.to_string();
493 let qualname = format!("::{}",
494 self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id)));
496 let kind = match item.kind {
497 ast::ItemKind::Struct(_, _) => DefKind::Struct,
498 ast::ItemKind::Union(_, _) => DefKind::Union,
502 let (value, fields) = match item.kind {
503 ast::ItemKind::Struct(ast::VariantData::Struct(ref fields, ..), ..) |
504 ast::ItemKind::Union(ast::VariantData::Struct(ref fields, ..), ..) => {
505 let include_priv_fields = !self.save_ctxt.config.pub_only;
506 let fields_str = fields
509 .filter_map(|(i, f)| {
510 if include_priv_fields || f.vis.node.is_pub() {
512 .map(|i| i.to_string())
513 .or_else(|| Some(i.to_string()))
520 let value = format!("{} {{ {} }}", name, fields_str);
525 .map(|f| id_from_node_id(f.id, &self.save_ctxt))
529 _ => (String::new(), vec![]),
532 if !self.span.filter_generated(item.ident.span) {
533 let span = self.span_from_span(item.ident.span);
534 let hir_id = self.tcx.hir().node_to_hir_id(item.id);
535 self.dumper.dump_def(
536 &access_from!(self.save_ctxt, item, hir_id),
539 id: id_from_node_id(item.id, &self.save_ctxt),
542 qualname: qualname.clone(),
547 docs: self.save_ctxt.docs_for_attrs(&item.attrs),
548 sig: sig::item_signature(item, &self.save_ctxt),
549 attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt),
554 self.nest_tables(item.id, |v| {
555 for field in def.fields() {
556 v.process_struct_field_def(field, item.id);
557 v.visit_ty(&field.ty);
560 v.process_generic_params(ty_params, &qualname, item.id);
567 enum_definition: &'l ast::EnumDef,
568 ty_params: &'l ast::Generics,
570 let enum_data = self.save_ctxt.get_item_data(item);
571 let enum_data = match enum_data {
575 down_cast_data!(enum_data, DefData, item.span);
577 let hir_id = self.tcx.hir().node_to_hir_id(item.id);
578 let access = access_from!(self.save_ctxt, item, hir_id);
580 for variant in &enum_definition.variants {
581 let name = variant.ident.name.to_string();
582 let qualname = format!("{}::{}", enum_data.qualname, name);
583 let name_span = variant.ident.span;
586 ast::VariantData::Struct(ref fields, ..) => {
587 let fields_str = fields
591 f.ident.map(|i| i.to_string()).unwrap_or_else(|| i.to_string())
595 let value = format!("{}::{} {{ {} }}", enum_data.name, name, fields_str);
596 if !self.span.filter_generated(name_span) {
597 let span = self.span_from_span(name_span);
598 let id = id_from_node_id(variant.id, &self.save_ctxt);
599 let parent = Some(id_from_node_id(item.id, &self.save_ctxt));
601 self.dumper.dump_def(
604 kind: DefKind::StructVariant,
613 docs: self.save_ctxt.docs_for_attrs(&variant.attrs),
614 sig: sig::variant_signature(variant, &self.save_ctxt),
615 attributes: lower_attributes(
616 variant.attrs.clone(),
624 let mut value = format!("{}::{}", enum_data.name, name);
625 if let &ast::VariantData::Tuple(ref fields, _) = v {
627 value.push_str(&fields
629 .map(|f| ty_to_string(&f.ty))
634 if !self.span.filter_generated(name_span) {
635 let span = self.span_from_span(name_span);
636 let id = id_from_node_id(variant.id, &self.save_ctxt);
637 let parent = Some(id_from_node_id(item.id, &self.save_ctxt));
639 self.dumper.dump_def(
642 kind: DefKind::TupleVariant,
651 docs: self.save_ctxt.docs_for_attrs(&variant.attrs),
652 sig: sig::variant_signature(variant, &self.save_ctxt),
653 attributes: lower_attributes(
654 variant.attrs.clone(),
664 for field in variant.data.fields() {
665 self.process_struct_field_def(field, variant.id);
666 self.visit_ty(&field.ty);
669 self.process_generic_params(ty_params, &enum_data.qualname, item.id);
670 self.dumper.dump_def(&access, enum_data);
676 generics: &'l ast::Generics,
677 trait_ref: &'l Option<ast::TraitRef>,
679 impl_items: &'l [ast::AssocItem],
681 if let Some(impl_data) = self.save_ctxt.get_item_data(item) {
682 if !self.span.filter_generated(item.span) {
683 if let super::Data::RelationData(rel, imp) = impl_data {
684 self.dumper.dump_relation(rel);
685 self.dumper.dump_impl(imp);
687 span_bug!(item.span, "unexpected data kind: {:?}", impl_data);
692 let map = &self.tcx.hir();
693 self.nest_tables(item.id, |v| {
695 if let &Some(ref trait_ref) = trait_ref {
696 v.process_path(trait_ref.ref_id, &trait_ref.path);
698 v.process_generic_params(generics, "", item.id);
699 for impl_item in impl_items {
700 v.process_impl_item(impl_item, map.local_def_id_from_node_id(item.id));
708 generics: &'l ast::Generics,
709 trait_refs: &'l ast::GenericBounds,
710 methods: &'l [ast::AssocItem],
712 let name = item.ident.to_string();
713 let qualname = format!("::{}",
714 self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id)));
715 let mut val = name.clone();
716 if !generics.params.is_empty() {
717 val.push_str(&generic_params_to_string(&generics.params));
719 if !trait_refs.is_empty() {
721 val.push_str(&bounds_to_string(trait_refs));
723 if !self.span.filter_generated(item.ident.span) {
724 let id = id_from_node_id(item.id, &self.save_ctxt);
725 let span = self.span_from_span(item.ident.span);
726 let children = methods
728 .map(|i| id_from_node_id(i.id, &self.save_ctxt))
730 let hir_id = self.tcx.hir().node_to_hir_id(item.id);
731 self.dumper.dump_def(
732 &access_from!(self.save_ctxt, item, hir_id),
734 kind: DefKind::Trait,
738 qualname: qualname.clone(),
743 docs: self.save_ctxt.docs_for_attrs(&item.attrs),
744 sig: sig::item_signature(item, &self.save_ctxt),
745 attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt),
751 for super_bound in trait_refs.iter() {
752 let trait_ref = match *super_bound {
753 ast::GenericBound::Trait(ref trait_ref, _) => trait_ref,
754 ast::GenericBound::Outlives(..) => continue,
757 let trait_ref = &trait_ref.trait_ref;
758 if let Some(id) = self.lookup_def_id(trait_ref.ref_id) {
759 let sub_span = trait_ref.path.segments.last().unwrap().ident.span;
760 if !self.span.filter_generated(sub_span) {
761 let span = self.span_from_span(sub_span);
762 self.dumper.dump_ref(Ref {
765 ref_id: id_from_def_id(id),
768 self.dumper.dump_relation(Relation {
769 kind: RelationKind::SuperTrait,
771 from: id_from_def_id(id),
772 to: id_from_node_id(item.id, &self.save_ctxt),
778 // walk generics and methods
779 self.process_generic_params(generics, &qualname, item.id);
780 for method in methods {
781 let map = &self.tcx.hir();
782 self.process_trait_item(method, map.local_def_id_from_node_id(item.id))
786 // `item` is the module in question, represented as an item.
787 fn process_mod(&mut self, item: &ast::Item) {
788 if let Some(mod_data) = self.save_ctxt.get_item_data(item) {
789 down_cast_data!(mod_data, DefData, item.span);
790 let hir_id = self.tcx.hir().node_to_hir_id(item.id);
791 self.dumper.dump_def(&access_from!(self.save_ctxt, item, hir_id), mod_data);
795 fn dump_path_ref(&mut self, id: NodeId, path: &ast::Path) {
796 let path_data = self.save_ctxt.get_path_data(id, path);
797 if let Some(path_data) = path_data {
798 self.dumper.dump_ref(path_data);
802 fn process_path(&mut self, id: NodeId, path: &'l ast::Path) {
803 if self.span.filter_generated(path.span) {
806 self.dump_path_ref(id, path);
809 for seg in &path.segments {
810 if let Some(ref generic_args) = seg.args {
811 match **generic_args {
812 ast::GenericArgs::AngleBracketed(ref data) => {
813 for arg in &data.args {
814 if let ast::GenericArg::Type(ty) = arg {
819 ast::GenericArgs::Parenthesized(ref data) => {
820 for t in &data.inputs {
823 if let ast::FunctionRetTy::Ty(ty) = &data.output {
831 self.write_sub_paths_truncated(path);
834 fn process_struct_lit(
838 fields: &'l [ast::Field],
839 variant: &'l ty::VariantDef,
840 base: &'l Option<P<ast::Expr>>,
842 if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) {
843 self.write_sub_paths_truncated(path);
844 down_cast_data!(struct_lit_data, RefData, ex.span);
845 if !generated_code(ex.span) {
846 self.dumper.dump_ref(struct_lit_data);
849 for field in fields {
850 if let Some(field_data) = self.save_ctxt.get_field_ref_data(field, variant) {
851 self.dumper.dump_ref(field_data);
854 self.visit_expr(&field.expr)
858 walk_list!(self, visit_expr, base);
861 fn process_method_call(
864 seg: &'l ast::PathSegment,
865 args: &'l [P<ast::Expr>],
867 debug!("process_method_call {:?} {:?}", ex, ex.span);
868 if let Some(mcd) = self.save_ctxt.get_expr_data(ex) {
869 down_cast_data!(mcd, RefData, ex.span);
870 if !generated_code(ex.span) {
871 self.dumper.dump_ref(mcd);
875 // Explicit types in the turbo-fish.
876 if let Some(ref generic_args) = seg.args {
877 if let ast::GenericArgs::AngleBracketed(ref data) = **generic_args {
878 for arg in &data.args {
880 ast::GenericArg::Type(ty) => self.visit_ty(ty),
887 // walk receiver and args
888 walk_list!(self, visit_expr, args);
891 fn process_pat(&mut self, p: &'l ast::Pat) {
893 PatKind::Struct(ref _path, ref fields, _) => {
894 // FIXME do something with _path?
895 let hir_id = self.tcx.hir().node_to_hir_id(p.id);
896 let adt = match self.save_ctxt.tables.node_type_opt(hir_id) {
897 Some(ty) => ty.ty_adt_def().unwrap(),
899 visit::walk_pat(self, p);
903 let variant = adt.variant_of_res(self.save_ctxt.get_path_res(p.id));
905 for field in fields {
906 if let Some(index) = self.tcx.find_field_index(field.ident, variant) {
907 if !self.span.filter_generated(field.ident.span) {
908 let span = self.span_from_span(field.ident.span);
909 self.dumper.dump_ref(Ref {
910 kind: RefKind::Variable,
912 ref_id: id_from_def_id(variant.fields[index].did),
916 self.visit_pat(&field.pat);
919 _ => visit::walk_pat(self, p),
923 fn process_var_decl(&mut self, pat: &'l ast::Pat) {
924 // The pattern could declare multiple new vars,
925 // we must walk the pattern and collect them all.
926 let mut collector = PathCollector::new();
927 collector.visit_pat(&pat);
928 self.visit_pat(&pat);
930 // Process collected paths.
931 for (id, ident, _) in collector.collected_idents {
932 match self.save_ctxt.get_path_res(id) {
933 Res::Local(hir_id) => {
934 let id = self.tcx.hir().hir_to_node_id(hir_id);
935 let typ = self.save_ctxt.tables.node_type_opt(hir_id)
936 .map(|t| t.to_string())
937 .unwrap_or_default();
939 // Rust uses the id of the pattern for var lookups, so we'll use it too.
940 if !self.span.filter_generated(ident.span) {
941 let qualname = format!("{}${}", ident.to_string(), id);
942 let id = id_from_node_id(id, &self.save_ctxt);
943 let span = self.span_from_span(ident.span);
945 self.dumper.dump_def(
951 kind: DefKind::Local,
954 name: ident.to_string(),
967 Res::Def(HirDefKind::Ctor(..), _) |
968 Res::Def(HirDefKind::Const, _) |
969 Res::Def(HirDefKind::AssocConst, _) |
970 Res::Def(HirDefKind::Struct, _) |
971 Res::Def(HirDefKind::Variant, _) |
972 Res::Def(HirDefKind::TyAlias, _) |
973 Res::Def(HirDefKind::AssocTy, _) |
975 self.dump_path_ref(id, &ast::Path::from_ident(ident));
978 "unexpected definition kind when processing collected idents: {:?}",
984 for (id, ref path) in collector.collected_paths {
985 self.process_path(id, path);
989 /// Extracts macro use and definition information from the AST node defined
990 /// by the given NodeId, using the expansion information from the node's
993 /// If the span is not macro-generated, do nothing, else use callee and
994 /// callsite spans to record macro definition and use data, using the
995 /// mac_uses and mac_defs sets to prevent multiples.
996 fn process_macro_use(&mut self, _span: Span) {
997 // FIXME if we're not dumping the defs (see below), there is no point
998 // dumping refs either.
999 // let source_span = span.source_callsite();
1000 // if !self.macro_calls.insert(source_span) {
1004 // let data = match self.save_ctxt.get_macro_use_data(span) {
1006 // Some(data) => data,
1009 // self.dumper.macro_use(data);
1011 // FIXME write the macro def
1012 // let mut hasher = DefaultHasher::new();
1013 // data.callee_span.hash(&mut hasher);
1014 // let hash = hasher.finish();
1015 // let qualname = format!("{}::{}", data.name, hash);
1016 // Don't write macro definition for imported macros
1017 // if !self.mac_defs.contains(&data.callee_span)
1018 // && !data.imported {
1019 // self.mac_defs.insert(data.callee_span);
1020 // if let Some(sub_span) = self.span.span_for_macro_def_name(data.callee_span) {
1021 // self.dumper.macro_data(MacroData {
1023 // name: data.name.clone(),
1024 // qualname: qualname.clone(),
1025 // // FIXME where do macro docs come from?
1026 // docs: String::new(),
1027 // }.lower(self.tcx));
1032 fn process_trait_item(&mut self, trait_item: &'l ast::AssocItem, trait_id: DefId) {
1033 self.process_macro_use(trait_item.span);
1034 let vis_span = trait_item.span.shrink_to_lo();
1035 match trait_item.kind {
1036 ast::AssocItemKind::Const(ref ty, ref expr) => {
1037 self.process_assoc_const(
1041 expr.as_ref().map(|e| &**e),
1043 respan(vis_span, ast::VisibilityKind::Public),
1047 ast::AssocItemKind::Fn(ref sig, ref body) => {
1048 self.process_method(
1050 body.as_ref().map(|x| &**x),
1053 &trait_item.generics,
1054 respan(vis_span, ast::VisibilityKind::Public),
1058 ast::AssocItemKind::TyAlias(ref bounds, ref default_ty) => {
1059 // FIXME do something with _bounds (for type refs)
1060 let name = trait_item.ident.name.to_string();
1061 let qualname = format!("::{}",
1062 self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(trait_item.id)));
1064 if !self.span.filter_generated(trait_item.ident.span) {
1065 let span = self.span_from_span(trait_item.ident.span);
1066 let id = id_from_node_id(trait_item.id, &self.save_ctxt);
1068 self.dumper.dump_def(
1074 kind: DefKind::Type,
1079 value: self.span.snippet(trait_item.span),
1080 parent: Some(id_from_def_id(trait_id)),
1083 docs: self.save_ctxt.docs_for_attrs(&trait_item.attrs),
1084 sig: sig::assoc_type_signature(
1088 default_ty.as_ref().map(|ty| &**ty),
1091 attributes: lower_attributes(trait_item.attrs.clone(), &self.save_ctxt),
1096 if let &Some(ref default_ty) = default_ty {
1097 self.visit_ty(default_ty)
1100 ast::AssocItemKind::Macro(_) => {}
1104 fn process_impl_item(&mut self, impl_item: &'l ast::AssocItem, impl_id: DefId) {
1105 self.process_macro_use(impl_item.span);
1106 match impl_item.kind {
1107 ast::AssocItemKind::Const(ref ty, ref expr) => {
1108 self.process_assoc_const(
1114 impl_item.vis.clone(),
1118 ast::AssocItemKind::Fn(ref sig, ref body) => {
1119 self.process_method(
1124 &impl_item.generics,
1125 impl_item.vis.clone(),
1129 ast::AssocItemKind::TyAlias(_, None) => {}
1130 ast::AssocItemKind::TyAlias(_, Some(ref ty)) => {
1131 // FIXME: uses of the assoc type should ideally point to this
1132 // 'def' and the name here should be a ref to the def in the
1136 ast::AssocItemKind::Macro(_) => {}
1140 /// Dumps imports in a use tree recursively.
1142 /// A use tree is an import that may contain nested braces (RFC 2128). The `use_tree` parameter
1143 /// is the current use tree under scrutiny, while `id` and `prefix` are its corresponding node
1144 /// ID and path. `root_item` is the topmost use tree in the hierarchy.
1146 /// If `use_tree` is a simple or glob import, it is dumped into the analysis data. Otherwise,
1147 /// each child use tree is dumped recursively.
1148 fn process_use_tree(&mut self,
1149 use_tree: &'l ast::UseTree,
1151 root_item: &'l ast::Item,
1152 prefix: &ast::Path) {
1153 let path = &use_tree.prefix;
1155 // The access is calculated using the current tree ID, but with the root tree's visibility
1156 // (since nested trees don't have their own visibility).
1157 let hir_id = self.tcx.hir().node_to_hir_id(id);
1158 let access = access_from!(self.save_ctxt, root_item, hir_id);
1160 // The parent `DefId` of a given use tree is always the enclosing item.
1161 let parent = self.save_ctxt.tcx.hir().opt_local_def_id_from_node_id(id)
1162 .and_then(|id| self.save_ctxt.tcx.parent(id))
1163 .map(id_from_def_id);
1165 match use_tree.kind {
1166 ast::UseTreeKind::Simple(alias, ..) => {
1167 let ident = use_tree.ident();
1168 let path = ast::Path {
1169 segments: prefix.segments
1171 .chain(path.segments.iter())
1177 let sub_span = path.segments.last().unwrap().ident.span;
1178 if !self.span.filter_generated(sub_span) {
1179 let ref_id = self.lookup_def_id(id).map(|id| id_from_def_id(id));
1180 let alias_span = alias.map(|i| self.span_from_span(i.span));
1181 let span = self.span_from_span(sub_span);
1182 self.dumper.import(&access, Import {
1183 kind: ImportKind::Use,
1187 name: ident.to_string(),
1188 value: String::new(),
1191 self.write_sub_paths_truncated(&path);
1194 ast::UseTreeKind::Glob => {
1195 let path = ast::Path {
1196 segments: prefix.segments
1198 .chain(path.segments.iter())
1204 // Make a comma-separated list of names of imported modules.
1205 let def_id = self.tcx.hir().local_def_id_from_node_id(id);
1206 let names = self.tcx.names_imported_by_glob_use(def_id);
1207 let names: Vec<_> = names.iter().map(|n| n.to_string()).collect();
1209 // Otherwise it's a span with wrong macro expansion info, which
1210 // we don't want to track anyway, since it's probably macro-internal `use`
1211 if let Some(sub_span) =
1212 self.span.sub_span_of_token(use_tree.span, token::BinOp(token::Star))
1214 if !self.span.filter_generated(use_tree.span) {
1215 let span = self.span_from_span(sub_span);
1217 self.dumper.import(&access, Import {
1218 kind: ImportKind::GlobUse,
1222 name: "*".to_owned(),
1223 value: names.join(", "),
1226 self.write_sub_paths(&path);
1230 ast::UseTreeKind::Nested(ref nested_items) => {
1231 let prefix = ast::Path {
1232 segments: prefix.segments
1234 .chain(path.segments.iter())
1239 for &(ref tree, id) in nested_items {
1240 self.process_use_tree(tree, id, root_item, &prefix);
1246 fn process_bounds(&mut self, bounds: &'l ast::GenericBounds) {
1247 for bound in bounds {
1248 if let ast::GenericBound::Trait(ref trait_ref, _) = *bound {
1249 self.process_path(trait_ref.trait_ref.ref_id, &trait_ref.trait_ref.path)
1255 impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
1256 fn visit_mod(&mut self, m: &'l ast::Mod, span: Span, attrs: &[ast::Attribute], id: NodeId) {
1257 // Since we handle explicit modules ourselves in visit_item, this should
1258 // only get called for the root module of a crate.
1259 assert_eq!(id, ast::CRATE_NODE_ID);
1261 let qualname = format!("::{}",
1262 self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(id)));
1264 let cm = self.tcx.sess.source_map();
1265 let filename = cm.span_to_filename(span);
1266 let data_id = id_from_node_id(id, &self.save_ctxt);
1267 let children = m.items
1269 .map(|i| id_from_node_id(i.id, &self.save_ctxt))
1271 let span = self.span_from_span(span);
1273 self.dumper.dump_def(
1281 name: String::new(),
1284 value: filename.to_string(),
1288 docs: self.save_ctxt.docs_for_attrs(attrs),
1290 attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt),
1293 visit::walk_mod(self, m);
1296 fn visit_item(&mut self, item: &'l ast::Item) {
1297 use syntax::ast::ItemKind::*;
1298 self.process_macro_use(item.span);
1300 Use(ref use_tree) => {
1301 let prefix = ast::Path {
1305 self.process_use_tree(use_tree, item.id, item, &prefix);
1308 let name_span = item.ident.span;
1309 if !self.span.filter_generated(name_span) {
1310 let span = self.span_from_span(name_span);
1311 let parent = self.save_ctxt.tcx.hir().opt_local_def_id_from_node_id(item.id)
1312 .and_then(|id| self.save_ctxt.tcx.parent(id))
1313 .map(id_from_def_id);
1320 kind: ImportKind::ExternCrate,
1324 name: item.ident.to_string(),
1325 value: String::new(),
1331 Fn(ref sig, ref ty_params, ref body) => {
1332 self.process_fn(item, &sig.decl, &sig.header, ty_params, &body)
1334 Static(ref typ, _, ref expr) => self.process_static_or_const_item(item, typ, expr),
1335 Const(ref typ, ref expr) => self.process_static_or_const_item(item, &typ, &expr),
1336 Struct(ref def, ref ty_params) | Union(ref def, ref ty_params) => {
1337 self.process_struct(item, def, ty_params)
1339 Enum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
1340 Impl(.., ref ty_params, ref trait_ref, ref typ, ref impl_items) => {
1341 self.process_impl(item, ty_params, trait_ref, &typ, impl_items)
1343 Trait(_, _, ref generics, ref trait_refs, ref methods) => {
1344 self.process_trait(item, generics, trait_refs, methods)
1347 self.process_mod(item);
1348 visit::walk_mod(self, m);
1350 TyAlias(ref ty, ref ty_params) => {
1351 let qualname = format!("::{}",
1352 self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id)));
1353 let value = ty_to_string(&ty);
1354 if !self.span.filter_generated(item.ident.span) {
1355 let span = self.span_from_span(item.ident.span);
1356 let id = id_from_node_id(item.id, &self.save_ctxt);
1357 let hir_id = self.tcx.hir().node_to_hir_id(item.id);
1359 self.dumper.dump_def(
1360 &access_from!(self.save_ctxt, item, hir_id),
1362 kind: DefKind::Type,
1365 name: item.ident.to_string(),
1366 qualname: qualname.clone(),
1371 docs: self.save_ctxt.docs_for_attrs(&item.attrs),
1372 sig: sig::item_signature(item, &self.save_ctxt),
1373 attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt),
1379 self.process_generic_params(ty_params, &qualname, item.id);
1382 _ => visit::walk_item(self, item),
1386 fn visit_generics(&mut self, generics: &'l ast::Generics) {
1387 for param in &generics.params {
1389 ast::GenericParamKind::Lifetime { .. } => {}
1390 ast::GenericParamKind::Type { ref default, .. } => {
1391 self.process_bounds(¶m.bounds);
1392 if let Some(ref ty) = default {
1396 ast::GenericParamKind::Const { ref ty } => {
1397 self.process_bounds(¶m.bounds);
1402 for pred in &generics.where_clause.predicates {
1403 if let ast::WherePredicate::BoundPredicate(ref wbp) = *pred {
1404 self.process_bounds(&wbp.bounds);
1405 self.visit_ty(&wbp.bounded_ty);
1410 fn visit_ty(&mut self, t: &'l ast::Ty) {
1411 self.process_macro_use(t.span);
1413 ast::TyKind::Path(_, ref path) => {
1414 if generated_code(t.span) {
1418 if let Some(id) = self.lookup_def_id(t.id) {
1419 let sub_span = path.segments.last().unwrap().ident.span;
1420 let span = self.span_from_span(sub_span);
1421 self.dumper.dump_ref(Ref {
1422 kind: RefKind::Type,
1424 ref_id: id_from_def_id(id),
1428 self.write_sub_paths_truncated(path);
1429 visit::walk_path(self, path);
1431 ast::TyKind::Array(ref element, ref length) => {
1432 self.visit_ty(element);
1433 self.nest_tables(length.id, |v| v.visit_expr(&length.value));
1435 ast::TyKind::ImplTrait(id, ref bounds) => {
1436 // FIXME: As of writing, the opaque type lowering introduces
1437 // another DefPath scope/segment (used to declare the resulting
1438 // opaque type item).
1439 // However, the synthetic scope does *not* have associated
1440 // typeck tables, which means we can't nest it and we fire an
1441 // assertion when resolving the qualified type paths in trait
1443 // This will panic if called on return type `impl Trait`, which
1444 // we guard against in `process_fn`.
1445 self.nest_tables(id, |v| v.process_bounds(bounds));
1447 _ => visit::walk_ty(self, t),
1451 fn visit_expr(&mut self, ex: &'l ast::Expr) {
1452 debug!("visit_expr {:?}", ex.kind);
1453 self.process_macro_use(ex.span);
1455 ast::ExprKind::Struct(ref path, ref fields, ref base) => {
1456 let expr_hir_id = self.save_ctxt.tcx.hir().node_to_hir_id(ex.id);
1457 let hir_expr = self.save_ctxt.tcx.hir().expect_expr(expr_hir_id);
1458 let adt = match self.save_ctxt.tables.expr_ty_opt(&hir_expr) {
1459 Some(ty) if ty.ty_adt_def().is_some() => ty.ty_adt_def().unwrap(),
1461 visit::walk_expr(self, ex);
1465 let node_id = self.save_ctxt.tcx.hir().hir_to_node_id(hir_expr.hir_id);
1466 let res = self.save_ctxt.get_path_res(node_id);
1467 self.process_struct_lit(ex, path, fields, adt.variant_of_res(res), base)
1469 ast::ExprKind::MethodCall(ref seg, ref args) => self.process_method_call(ex, seg, args),
1470 ast::ExprKind::Field(ref sub_ex, _) => {
1471 self.visit_expr(&sub_ex);
1473 if let Some(field_data) = self.save_ctxt.get_expr_data(ex) {
1474 down_cast_data!(field_data, RefData, ex.span);
1475 if !generated_code(ex.span) {
1476 self.dumper.dump_ref(field_data);
1480 ast::ExprKind::Closure(_, _, _, ref decl, ref body, _fn_decl_span) => {
1481 let id = format!("${}", ex.id);
1483 // walk arg and return types
1484 for arg in &decl.inputs {
1485 self.visit_ty(&arg.ty);
1488 if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output {
1489 self.visit_ty(&ret_ty);
1493 self.nest_tables(ex.id, |v| {
1494 v.process_formals(&decl.inputs, &id);
1498 ast::ExprKind::ForLoop(ref pattern, ref subexpression, ref block, _) => {
1499 self.process_var_decl(pattern);
1500 debug!("for loop, walk sub-expr: {:?}", subexpression.kind);
1501 self.visit_expr(subexpression);
1502 visit::walk_block(self, block);
1504 ast::ExprKind::Let(ref pat, ref scrutinee) => {
1505 self.process_var_decl(pat);
1506 self.visit_expr(scrutinee);
1508 ast::ExprKind::Repeat(ref element, ref count) => {
1509 self.visit_expr(element);
1510 self.nest_tables(count.id, |v| v.visit_expr(&count.value));
1512 // In particular, we take this branch for call and path expressions,
1513 // where we'll index the idents involved just by continuing to walk.
1514 _ => visit::walk_expr(self, ex),
1518 fn visit_pat(&mut self, p: &'l ast::Pat) {
1519 self.process_macro_use(p.span);
1520 self.process_pat(p);
1523 fn visit_arm(&mut self, arm: &'l ast::Arm) {
1524 self.process_var_decl(&arm.pat);
1525 if let Some(expr) = &arm.guard {
1526 self.visit_expr(expr);
1528 self.visit_expr(&arm.body);
1531 fn visit_path(&mut self, p: &'l ast::Path, id: NodeId) {
1532 self.process_path(id, p);
1535 fn visit_stmt(&mut self, s: &'l ast::Stmt) {
1536 self.process_macro_use(s.span);
1537 visit::walk_stmt(self, s)
1540 fn visit_local(&mut self, l: &'l ast::Local) {
1541 self.process_macro_use(l.span);
1542 self.process_var_decl(&l.pat);
1544 // Just walk the initialiser and type (don't want to walk the pattern again).
1545 walk_list!(self, visit_ty, &l.ty);
1546 walk_list!(self, visit_expr, &l.init);
1549 fn visit_foreign_item(&mut self, item: &'l ast::ForeignItem) {
1550 let hir_id = self.tcx.hir().node_to_hir_id(item.id);
1551 let access = access_from!(self.save_ctxt, item, hir_id);
1554 ast::ForeignItemKind::Fn(ref decl, ref generics) => {
1555 if let Some(fn_data) = self.save_ctxt.get_extern_item_data(item) {
1556 down_cast_data!(fn_data, DefData, item.span);
1558 self.process_generic_params(generics, &fn_data.qualname, item.id);
1559 self.dumper.dump_def(&access, fn_data);
1562 for arg in &decl.inputs {
1563 self.visit_ty(&arg.ty);
1566 if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output {
1567 self.visit_ty(&ret_ty);
1570 ast::ForeignItemKind::Static(ref ty, _) => {
1571 if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) {
1572 down_cast_data!(var_data, DefData, item.span);
1573 self.dumper.dump_def(&access, var_data);
1578 ast::ForeignItemKind::Ty => {
1579 if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) {
1580 down_cast_data!(var_data, DefData, item.span);
1581 self.dumper.dump_def(&access, var_data);
1584 ast::ForeignItemKind::Macro(..) => {}