1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Output a CSV file containing the output from rustc's analysis. The data is
12 //! primarily designed to be used as input to the DXR tool, specifically its
13 //! Rust plugin. It could also be used by IDEs or other code browsing, search, or
14 //! cross-referencing tools.
16 //! Dumping the analysis is implemented by walking the AST and getting a bunch of
17 //! info out from all over the place. We use Def IDs to identify objects. The
18 //! tricky part is getting syntactic (span, source text) and semantic (reference
19 //! Def IDs) information for parts of expressions which the compiler has discarded.
20 //! E.g., in a path `foo::bar::baz`, the compiler only keeps a span for the whole
21 //! path and a reference to `baz`, but we want spans and references for all three
24 //! SpanUtils is used to manipulate spans. In particular, to extract sub-spans
25 //! from spans (e.g., the span for `bar` from the above example path).
26 //! Recorder is used for recording the output in csv format. FmtStrs separates
27 //! the format of the output away from extracting it from the compiler.
28 //! DumpCsvVisitor walks the AST and processes it.
31 use super::{escape, generated_code, recorder, SaveContext, PathCollector, Data};
36 use middle::def_id::DefId;
37 use middle::ty::{self, Ty};
42 use syntax::ast::{self, NodeId};
43 use syntax::codemap::*;
44 use syntax::parse::token::{self, keywords};
45 use syntax::owned_slice::OwnedSlice;
46 use syntax::visit::{self, Visitor};
47 use syntax::print::pprust::{path_to_string, ty_to_string};
50 use super::span_utils::SpanUtils;
51 use super::recorder::{Recorder, FmtStrs};
53 macro_rules! down_cast_data {
54 ($id:ident, $kind:ident, $this:ident, $sp:expr) => {
55 let $id = if let super::Data::$kind(data) = $id {
58 $this.sess.span_bug($sp, &format!("unexpected data kind: {:?}", $id));
63 pub struct DumpCsvVisitor<'l, 'tcx: 'l> {
64 save_ctxt: SaveContext<'l, 'tcx>,
66 tcx: &'l ty::ctxt<'tcx>,
67 analysis: &'l ty::CrateAnalysis,
75 impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
76 pub fn new(tcx: &'l ty::ctxt<'tcx>,
77 analysis: &'l ty::CrateAnalysis,
78 output_file: Box<File>) -> DumpCsvVisitor<'l, 'tcx> {
79 let span_utils = SpanUtils::new(&tcx.sess);
83 save_ctxt: SaveContext::from_span_utils(tcx, span_utils.clone()),
85 span: span_utils.clone(),
86 fmt: FmtStrs::new(box Recorder {
94 fn nest<F>(&mut self, scope_id: NodeId, f: F) where
95 F: FnOnce(&mut DumpCsvVisitor<'l, 'tcx>),
97 let parent_scope = self.cur_scope;
98 self.cur_scope = scope_id;
100 self.cur_scope = parent_scope;
103 pub fn dump_crate_info(&mut self, name: &str, krate: &ast::Crate) {
104 // The current crate.
105 self.fmt.crate_str(krate.span, name);
107 // Dump info about all the external crates referenced from this crate.
108 for c in &self.save_ctxt.get_external_crates() {
109 self.fmt.external_crate_str(krate.span, &c.name, c.number);
111 self.fmt.recorder.record("end_external_crates\n");
114 // Return all non-empty prefixes of a path.
115 // For each prefix, we return the span for the last segment in the prefix and
116 // a str representation of the entire prefix.
117 fn process_path_prefixes(&self, path: &ast::Path) -> Vec<(Span, String)> {
118 let spans = self.span.spans_for_path_segments(path);
120 // Paths to enums seem to not match their spans - the span includes all the
121 // variants too. But they seem to always be at the end, so I hope we can cope with
122 // always using the first ones. So, only error out if we don't have enough spans.
123 // What could go wrong...?
124 if spans.len() < path.segments.len() {
125 error!("Mis-calculated spans for path '{}'. \
126 Found {} spans, expected {}. Found spans:",
127 path_to_string(path), spans.len(), path.segments.len());
129 let loc = self.sess.codemap().lookup_char_pos(s.lo);
130 error!(" '{}' in {}, line {}",
131 self.span.snippet(*s), loc.file.name, loc.line);
136 let mut result: Vec<(Span, String)> = vec!();
138 let mut segs = vec!();
139 for (i, (seg, span)) in path.segments.iter().zip(&spans).enumerate() {
140 segs.push(seg.clone());
141 let sub_path = ast::Path{span: *span, // span for the last segment
144 let qualname = if i == 0 && path.global {
145 format!("::{}", path_to_string(&sub_path))
147 path_to_string(&sub_path)
149 result.push((*span, qualname));
150 segs = sub_path.segments;
156 // The global arg allows us to override the global-ness of the path (which
157 // actually means 'does the path start with `::`', rather than 'is the path
158 // semantically global). We use the override for `use` imports (etc.) where
159 // the syntax is non-global, but the semantics are global.
160 fn write_sub_paths(&mut self, path: &ast::Path, global: bool) {
161 let sub_paths = self.process_path_prefixes(path);
162 for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
163 let qualname = if i == 0 && global && !path.global {
164 format!("::{}", qualname)
168 self.fmt.sub_mod_ref_str(path.span,
175 // As write_sub_paths, but does not process the last ident in the path (assuming it
176 // will be processed elsewhere). See note on write_sub_paths about global.
177 fn write_sub_paths_truncated(&mut self, path: &ast::Path, global: bool) {
178 let sub_paths = self.process_path_prefixes(path);
179 let len = sub_paths.len();
184 let sub_paths = &sub_paths[..len-1];
185 for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
186 let qualname = if i == 0 && global && !path.global {
187 format!("::{}", qualname)
191 self.fmt.sub_mod_ref_str(path.span,
198 // As write_sub_paths, but expects a path of the form module_path::trait::method
199 // Where trait could actually be a struct too.
200 fn write_sub_path_trait_truncated(&mut self, path: &ast::Path) {
201 let sub_paths = self.process_path_prefixes(path);
202 let len = sub_paths.len();
206 let sub_paths = &sub_paths[.. (len-1)];
208 // write the trait part of the sub-path
209 let (ref span, ref qualname) = sub_paths[len-2];
210 self.fmt.sub_type_ref_str(path.span,
214 // write the other sub-paths
218 let sub_paths = &sub_paths[..len-2];
219 for &(ref span, ref qualname) in sub_paths {
220 self.fmt.sub_mod_ref_str(path.span,
227 // looks up anything, not just a type
228 fn lookup_type_ref(&self, ref_id: NodeId) -> Option<DefId> {
229 if !self.tcx.def_map.borrow().contains_key(&ref_id) {
230 self.sess.bug(&format!("def_map has no key for {} in lookup_type_ref",
233 let def = self.tcx.def_map.borrow().get(&ref_id).unwrap().full_def();
235 def::DefPrimTy(_) => None,
236 _ => Some(def.def_id()),
240 fn lookup_def_kind(&self, ref_id: NodeId, span: Span) -> Option<recorder::Row> {
241 let def_map = self.tcx.def_map.borrow();
242 if !def_map.contains_key(&ref_id) {
243 self.sess.span_bug(span, &format!("def_map has no key for {} in lookup_def_kind",
246 let def = def_map.get(&ref_id).unwrap().full_def();
249 def::DefForeignMod(_) => Some(recorder::ModRef),
250 def::DefStruct(_) => Some(recorder::TypeRef),
252 def::DefAssociatedTy(..) |
253 def::DefTrait(_) => Some(recorder::TypeRef),
254 def::DefStatic(_, _) |
256 def::DefAssociatedConst(..) |
258 def::DefVariant(_, _, _) |
259 def::DefUpvar(..) => Some(recorder::VarRef),
261 def::DefFn(..) => Some(recorder::FnRef),
266 def::DefTyParam(..) |
269 def::DefPrimTy(_) => {
270 self.sess.span_bug(span, &format!("lookup_def_kind for unexpected item: {:?}",
276 fn process_formals(&mut self, formals: &Vec<ast::Arg>, qualname: &str) {
278 self.visit_pat(&arg.pat);
279 let mut collector = PathCollector::new();
280 collector.visit_pat(&arg.pat);
281 let span_utils = self.span.clone();
282 for &(id, ref p, _, _) in &collector.collected_paths {
283 let typ = self.tcx.node_types().get(&id).unwrap().to_string();
284 // get the span only for the name of the variable (I hope the path is only ever a
285 // variable name, but who knows?)
286 self.fmt.formal_str(p.span,
287 span_utils.span_for_last_ident(p.span),
296 fn process_method(&mut self,
297 sig: &ast::MethodSig,
298 body: Option<&ast::Block>,
302 if generated_code(span) {
306 debug!("process_method: {}:{}", id, name);
308 let method_data = self.save_ctxt.get_method_data(id, name, span);
311 self.fmt.method_str(span,
312 Some(method_data.span),
314 &method_data.qualname,
315 method_data.declaration,
317 self.process_formals(&sig.decl.inputs, &method_data.qualname);
319 self.fmt.method_decl_str(span,
320 Some(method_data.span),
322 &method_data.qualname,
326 // walk arg and return types
327 for arg in &sig.decl.inputs {
328 self.visit_ty(&arg.ty);
331 if let ast::Return(ref ret_ty) = sig.decl.output {
332 self.visit_ty(ret_ty);
336 if let Some(body) = body {
337 self.nest(id, |v| v.visit_block(body));
340 self.process_generic_params(&sig.generics,
342 &method_data.qualname,
346 fn process_trait_ref(&mut self, trait_ref: &ast::TraitRef) {
347 let trait_ref_data = self.save_ctxt.get_trait_ref_data(trait_ref, self.cur_scope);
348 if let Some(trait_ref_data) = trait_ref_data {
349 self.fmt.ref_str(recorder::TypeRef,
351 Some(trait_ref_data.span),
352 trait_ref_data.ref_id,
353 trait_ref_data.scope);
354 visit::walk_path(self, &trait_ref.path);
358 fn process_struct_field_def(&mut self,
359 field: &ast::StructField,
361 let field_data = self.save_ctxt.get_field_data(field, parent_id);
362 if let Some(field_data) = field_data {
363 self.fmt.field_str(field.span,
364 Some(field_data.span),
367 &field_data.qualname,
368 &field_data.type_value,
373 // Dump generic params bindings, then visit_generics
374 fn process_generic_params(&mut self,
375 generics:&ast::Generics,
379 // We can't only use visit_generics since we don't have spans for param
380 // bindings, so we reparse the full_span to get those sub spans.
381 // However full span is the entire enum/fn/struct block, so we only want
382 // the first few to match the number of generics we're looking for.
383 let param_sub_spans = self.span.spans_for_ty_params(full_span,
384 (generics.ty_params.len() as isize));
385 for (param, param_ss) in generics.ty_params.iter().zip(param_sub_spans) {
386 // Append $id to name to make sure each one is unique
387 let name = format!("{}::{}${}",
389 escape(self.span.snippet(param_ss)),
391 self.fmt.typedef_str(full_span,
397 self.visit_generics(generics);
400 fn process_fn(&mut self,
403 ty_params: &ast::Generics,
405 let fn_data = self.save_ctxt.get_item_data(item);
406 down_cast_data!(fn_data, FunctionData, self, item.span);
407 self.fmt.fn_str(item.span,
414 self.process_formals(&decl.inputs, &fn_data.qualname);
415 self.process_generic_params(ty_params, item.span, &fn_data.qualname, item.id);
417 for arg in &decl.inputs {
418 self.visit_ty(&arg.ty);
421 if let ast::Return(ref ret_ty) = decl.output {
422 self.visit_ty(&ret_ty);
425 self.nest(item.id, |v| v.visit_block(&body));
428 fn process_static_or_const_item(&mut self,
433 let var_data = self.save_ctxt.get_item_data(item);
434 down_cast_data!(var_data, VariableData, self, item.span);
435 self.fmt.static_str(item.span,
441 &var_data.type_value,
445 self.visit_expr(expr);
448 fn process_const(&mut self,
455 let qualname = format!("::{}", self.tcx.map.path_to_string(id));
457 let sub_span = self.span.sub_span_after_keyword(span,
460 self.fmt.static_str(span,
463 &ident.name.as_str(),
465 &self.span.snippet(expr.span),
466 &ty_to_string(&*typ),
469 // walk type and init value
471 self.visit_expr(expr);
474 fn process_struct(&mut self,
476 def: &ast::StructDef,
477 ty_params: &ast::Generics) {
478 let qualname = format!("::{}", self.tcx.map.path_to_string(item.id));
480 let ctor_id = match def.ctor_id {
481 Some(node_id) => node_id,
482 None => ast::DUMMY_NODE_ID,
484 let val = self.span.snippet(item.span);
485 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Struct);
486 self.fmt.struct_str(item.span,
495 for field in &def.fields {
496 self.process_struct_field_def(field, item.id);
497 self.visit_ty(&field.node.ty);
500 self.process_generic_params(ty_params, item.span, &qualname, item.id);
503 fn process_enum(&mut self,
505 enum_definition: &ast::EnumDef,
506 ty_params: &ast::Generics) {
507 let enum_data = self.save_ctxt.get_item_data(item);
508 down_cast_data!(enum_data, EnumData, self, item.span);
509 self.fmt.enum_str(item.span,
510 Some(enum_data.span),
516 for variant in &enum_definition.variants {
517 let name = &variant.node.name.name.as_str();
518 let mut qualname = enum_data.qualname.clone();
519 qualname.push_str("::");
520 qualname.push_str(name);
521 let val = self.span.snippet(variant.span);
522 match variant.node.kind {
523 ast::TupleVariantKind(ref args) => {
524 // first ident in span is the variant's name
525 self.fmt.tuple_variant_str(variant.span,
526 self.span.span_for_first_ident(variant.span),
534 self.visit_ty(&*arg.ty);
537 ast::StructVariantKind(ref struct_def) => {
538 let ctor_id = match struct_def.ctor_id {
539 Some(node_id) => node_id,
540 None => ast::DUMMY_NODE_ID,
542 self.fmt.struct_variant_str(variant.span,
543 self.span.span_for_first_ident(variant.span),
551 for field in &struct_def.fields {
552 self.process_struct_field_def(field, variant.node.id);
553 self.visit_ty(&*field.node.ty);
558 self.process_generic_params(ty_params, item.span, &enum_data.qualname, enum_data.id);
561 fn process_impl(&mut self,
563 type_parameters: &ast::Generics,
564 trait_ref: &Option<ast::TraitRef>,
566 impl_items: &[P<ast::ImplItem>]) {
567 let impl_data = self.save_ctxt.get_item_data(item);
568 down_cast_data!(impl_data, ImplData, self, item.span);
569 match impl_data.self_ref {
570 Some(ref self_ref) => {
571 self.fmt.ref_str(recorder::TypeRef,
581 if let Some(ref trait_ref_data) = impl_data.trait_ref {
582 self.fmt.ref_str(recorder::TypeRef,
584 Some(trait_ref_data.span),
585 trait_ref_data.ref_id,
586 trait_ref_data.scope);
587 visit::walk_path(self, &trait_ref.as_ref().unwrap().path);
590 self.fmt.impl_str(item.span,
591 Some(impl_data.span),
593 impl_data.self_ref.map(|data| data.ref_id),
594 impl_data.trait_ref.map(|data| data.ref_id),
597 self.process_generic_params(type_parameters, item.span, "", item.id);
598 for impl_item in impl_items {
599 self.visit_impl_item(impl_item);
603 fn process_trait(&mut self,
605 generics: &ast::Generics,
606 trait_refs: &OwnedSlice<ast::TyParamBound>,
607 methods: &[P<ast::TraitItem>]) {
608 let qualname = format!("::{}", self.tcx.map.path_to_string(item.id));
609 let val = self.span.snippet(item.span);
610 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Trait);
611 self.fmt.trait_str(item.span,
619 for super_bound in trait_refs.iter() {
620 let trait_ref = match *super_bound {
621 ast::TraitTyParamBound(ref trait_ref, _) => {
624 ast::RegionTyParamBound(..) => {
629 let trait_ref = &trait_ref.trait_ref;
630 match self.lookup_type_ref(trait_ref.ref_id) {
632 let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span);
633 self.fmt.ref_str(recorder::TypeRef,
638 self.fmt.inherit_str(trait_ref.path.span,
647 // walk generics and methods
648 self.process_generic_params(generics, item.span, &qualname, item.id);
649 for method in methods {
650 self.visit_trait_item(method)
654 fn process_mod(&mut self,
655 item: &ast::Item) { // The module in question, represented as an item.
656 let mod_data = self.save_ctxt.get_item_data(item);
657 down_cast_data!(mod_data, ModData, self, item.span);
658 self.fmt.mod_str(item.span,
666 fn process_path(&mut self,
669 ref_kind: Option<recorder::Row>) {
670 if generated_code(path.span) {
674 let path_data = self.save_ctxt.get_path_data(id, path);
675 let path_data = match path_data {
678 self.tcx.sess.span_bug(path.span,
679 &format!("Unexpected def kind while looking \
681 self.span.snippet(path.span)))
685 Data::VariableRefData(ref vrd) => {
686 self.fmt.ref_str(ref_kind.unwrap_or(recorder::VarRef),
693 Data::TypeRefData(ref trd) => {
694 self.fmt.ref_str(recorder::TypeRef,
700 Data::MethodCallData(ref mcd) => {
701 self.fmt.meth_call_str(path.span,
707 Data::FunctionCallData(fcd) => {
708 self.fmt.fn_call_str(path.span,
714 self.sess.span_bug(path.span,
715 &format!("Unexpected data: {:?}", path_data));
719 // Modules or types in the path prefix.
720 let def_map = self.tcx.def_map.borrow();
721 let def = def_map.get(&id).unwrap().full_def();
723 def::DefMethod(did) => {
724 let ti = self.tcx.impl_or_trait_item(did);
725 if let ty::MethodTraitItem(m) = ti {
726 if m.explicit_self == ty::StaticExplicitSelfCategory {
727 self.write_sub_path_trait_truncated(path);
732 def::DefStatic(_,_) |
734 def::DefAssociatedConst(..) |
736 def::DefVariant(..) |
737 def::DefFn(..) => self.write_sub_paths_truncated(path, false),
742 fn process_struct_lit(&mut self,
745 fields: &Vec<ast::Field>,
746 variant: ty::VariantDef,
747 base: &Option<P<ast::Expr>>) {
748 if generated_code(path.span) {
752 self.write_sub_paths_truncated(path, false);
754 if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) {
755 down_cast_data!(struct_lit_data, TypeRefData, self, ex.span);
756 self.fmt.ref_str(recorder::TypeRef,
758 Some(struct_lit_data.span),
759 struct_lit_data.ref_id,
760 struct_lit_data.scope);
761 let scope = self.save_ctxt.enclosing_scope(ex.id);
763 for field in fields {
764 if generated_code(field.ident.span) {
768 let field_data = self.save_ctxt.get_field_ref_data(field,
771 self.fmt.ref_str(recorder::VarRef,
773 Some(field_data.span),
777 self.visit_expr(&field.expr)
781 visit::walk_expr_opt(self, base)
784 fn process_method_call(&mut self,
786 args: &Vec<P<ast::Expr>>) {
787 if let Some(call_data) = self.save_ctxt.get_expr_data(ex) {
788 down_cast_data!(call_data, MethodCallData, self, ex.span);
789 self.fmt.meth_call_str(ex.span,
790 Some(call_data.span),
796 // walk receiver and args
797 visit::walk_exprs(self, &args);
800 fn process_pat(&mut self, p:&ast::Pat) {
801 if generated_code(p.span) {
806 ast::PatStruct(ref path, ref fields, _) => {
807 visit::walk_path(self, path);
808 let adt = self.tcx.node_id_to_type(p.id).ty_adt_def().unwrap();
809 let def = self.tcx.def_map.borrow()[&p.id].full_def();
810 let variant = adt.variant_of_def(def);
812 for &Spanned { node: ref field, span } in fields {
813 if generated_code(span) {
817 let sub_span = self.span.span_for_first_ident(span);
818 if let Some(f) = variant.find_field_named(field.ident.name) {
819 self.fmt.ref_str(recorder::VarRef,
825 self.visit_pat(&field.pat);
828 _ => visit::walk_pat(self, p)
833 impl<'l, 'tcx, 'v> Visitor<'v> for DumpCsvVisitor<'l, 'tcx> {
834 fn visit_item(&mut self, item: &ast::Item) {
835 if generated_code(item.span) {
840 ast::ItemUse(ref use_item) => {
841 match use_item.node {
842 ast::ViewPathSimple(ident, ref path) => {
843 let sub_span = self.span.span_for_last_ident(path.span);
844 let mod_id = match self.lookup_type_ref(item.id) {
846 match self.lookup_def_kind(item.id, path.span) {
847 Some(kind) => self.fmt.ref_str(kind,
859 // 'use' always introduces an alias, if there is not an explicit
860 // one, there is an implicit one.
862 match self.span.sub_span_after_keyword(use_item.span, keywords::As) {
863 Some(sub_span) => Some(sub_span),
867 self.fmt.use_alias_str(path.span,
871 &ident.name.as_str(),
873 self.write_sub_paths_truncated(path, true);
875 ast::ViewPathGlob(ref path) => {
876 // Make a comma-separated list of names of imported modules.
877 let mut name_string = String::new();
878 let glob_map = &self.analysis.glob_map;
879 let glob_map = glob_map.as_ref().unwrap();
880 if glob_map.contains_key(&item.id) {
881 for n in glob_map.get(&item.id).unwrap() {
882 if !name_string.is_empty() {
883 name_string.push_str(", ");
885 name_string.push_str(&n.as_str());
889 let sub_span = self.span.sub_span_of_token(path.span,
890 token::BinOp(token::Star));
891 self.fmt.use_glob_str(path.span,
896 self.write_sub_paths(path, true);
898 ast::ViewPathList(ref path, ref list) => {
901 ast::PathListIdent { id, .. } => {
902 match self.lookup_type_ref(id) {
904 match self.lookup_def_kind(id, plid.span) {
909 def_id, self.cur_scope);
916 ast::PathListMod { .. } => ()
920 self.write_sub_paths(path, true);
924 ast::ItemExternCrate(ref s) => {
925 let location = match *s {
926 Some(s) => s.to_string(),
927 None => item.ident.to_string(),
929 let alias_span = self.span.span_for_last_ident(item.span);
930 let cnum = match self.sess.cstore.find_extern_mod_stmt_cnum(item.id) {
934 self.fmt.extern_crate_str(item.span,
938 &item.ident.name.as_str(),
942 ast::ItemFn(ref decl, _, _, _, ref ty_params, ref body) =>
943 self.process_fn(item, &**decl, ty_params, &**body),
944 ast::ItemStatic(ref typ, _, ref expr) =>
945 self.process_static_or_const_item(item, typ, expr),
946 ast::ItemConst(ref typ, ref expr) =>
947 self.process_static_or_const_item(item, &typ, &expr),
948 ast::ItemStruct(ref def, ref ty_params) => self.process_struct(item, &**def, ty_params),
949 ast::ItemEnum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
955 self.process_impl(item,
961 ast::ItemTrait(_, ref generics, ref trait_refs, ref methods) =>
962 self.process_trait(item, generics, trait_refs, methods),
963 ast::ItemMod(ref m) => {
964 self.process_mod(item);
965 self.nest(item.id, |v| visit::walk_mod(v, m));
967 ast::ItemTy(ref ty, ref ty_params) => {
968 let qualname = format!("::{}", self.tcx.map.path_to_string(item.id));
969 let value = ty_to_string(&**ty);
970 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Type);
971 self.fmt.typedef_str(item.span,
977 self.visit_ty(&**ty);
978 self.process_generic_params(ty_params, item.span, &qualname, item.id);
980 ast::ItemMac(_) => (),
981 _ => visit::walk_item(self, item),
985 fn visit_generics(&mut self, generics: &ast::Generics) {
986 for param in generics.ty_params.iter() {
987 for bound in param.bounds.iter() {
988 if let ast::TraitTyParamBound(ref trait_ref, _) = *bound {
989 self.process_trait_ref(&trait_ref.trait_ref);
992 if let Some(ref ty) = param.default {
993 self.visit_ty(&**ty);
998 fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
999 match trait_item.node {
1000 ast::ConstTraitItem(ref ty, Some(ref expr)) => {
1001 self.process_const(trait_item.id, &trait_item.ident,
1002 trait_item.span, &*ty, &*expr);
1004 ast::MethodTraitItem(ref sig, ref body) => {
1005 self.process_method(sig,
1006 body.as_ref().map(|x| &**x),
1008 trait_item.ident.name,
1011 ast::ConstTraitItem(_, None) |
1012 ast::TypeTraitItem(..) => {}
1016 fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
1017 match impl_item.node {
1018 ast::ConstImplItem(ref ty, ref expr) => {
1019 self.process_const(impl_item.id, &impl_item.ident,
1020 impl_item.span, &ty, &expr);
1022 ast::MethodImplItem(ref sig, ref body) => {
1023 self.process_method(sig,
1026 impl_item.ident.name,
1029 ast::TypeImplItem(_) |
1030 ast::MacImplItem(_) => {}
1034 fn visit_ty(&mut self, t: &ast::Ty) {
1035 if generated_code(t.span) {
1040 ast::TyPath(_, ref path) => {
1041 match self.lookup_type_ref(t.id) {
1043 let sub_span = self.span.sub_span_for_type_name(t.span);
1044 self.fmt.ref_str(recorder::TypeRef,
1053 self.write_sub_paths_truncated(path, false);
1055 visit::walk_path(self, path);
1057 _ => visit::walk_ty(self, t),
1061 fn visit_expr(&mut self, ex: &ast::Expr) {
1062 if generated_code(ex.span) {
1067 ast::ExprCall(ref _f, ref _args) => {
1068 // Don't need to do anything for function calls,
1069 // because just walking the callee path does what we want.
1070 visit::walk_expr(self, ex);
1072 ast::ExprPath(_, ref path) => {
1073 self.process_path(ex.id, path, None);
1074 visit::walk_expr(self, ex);
1076 ast::ExprStruct(ref path, ref fields, ref base) => {
1077 let adt = self.tcx.expr_ty(ex).ty_adt_def().unwrap();
1078 let def = self.tcx.resolve_expr(ex);
1079 self.process_struct_lit(ex,
1082 adt.variant_of_def(def),
1085 ast::ExprMethodCall(_, _, ref args) => self.process_method_call(ex, args),
1086 ast::ExprField(ref sub_ex, _) => {
1087 if generated_code(sub_ex.span) {
1091 self.visit_expr(&sub_ex);
1093 if let Some(field_data) = self.save_ctxt.get_expr_data(ex) {
1094 down_cast_data!(field_data, VariableRefData, self, ex.span);
1095 self.fmt.ref_str(recorder::VarRef,
1097 Some(field_data.span),
1102 ast::ExprTupField(ref sub_ex, idx) => {
1103 if generated_code(sub_ex.span) {
1107 self.visit_expr(&**sub_ex);
1109 let ty = &self.tcx.expr_ty_adjusted(&**sub_ex).sty;
1111 ty::TyStruct(def, _) => {
1112 let sub_span = self.span.sub_span_after_token(ex.span, token::Dot);
1113 self.fmt.ref_str(recorder::VarRef,
1116 def.struct_variant().fields[idx.node].did,
1119 ty::TyTuple(_) => {}
1120 _ => self.sess.span_bug(ex.span,
1121 &format!("Expected struct or tuple \
1122 type, found {:?}", ty)),
1125 ast::ExprClosure(_, ref decl, ref body) => {
1126 if generated_code(body.span) {
1130 let mut id = String::from("$");
1131 id.push_str(&ex.id.to_string());
1132 self.process_formals(&decl.inputs, &id);
1134 // walk arg and return types
1135 for arg in &decl.inputs {
1136 self.visit_ty(&*arg.ty);
1139 if let ast::Return(ref ret_ty) = decl.output {
1140 self.visit_ty(&**ret_ty);
1144 self.nest(ex.id, |v| v.visit_block(&**body));
1147 visit::walk_expr(self, ex)
1152 fn visit_mac(&mut self, _: &ast::Mac) {
1153 // Just stop, macros are poison to us.
1156 fn visit_pat(&mut self, p: &ast::Pat) {
1157 self.process_pat(p);
1160 fn visit_arm(&mut self, arm: &ast::Arm) {
1161 let mut collector = PathCollector::new();
1162 for pattern in &arm.pats {
1163 // collect paths from the arm's patterns
1164 collector.visit_pat(&pattern);
1165 self.visit_pat(&pattern);
1168 // This is to get around borrow checking, because we need mut self to call process_path.
1169 let mut paths_to_process = vec![];
1171 // process collected paths
1172 for &(id, ref p, immut, ref_kind) in &collector.collected_paths {
1173 let def_map = self.tcx.def_map.borrow();
1174 if !def_map.contains_key(&id) {
1175 self.sess.span_bug(p.span,
1176 &format!("def_map has no key for {} in visit_arm",
1179 let def = def_map.get(&id).unwrap().full_def();
1181 def::DefLocal(id) => {
1182 let value = if immut == ast::MutImmutable {
1183 self.span.snippet(p.span).to_string()
1185 "<mutable>".to_string()
1188 assert!(p.segments.len() == 1, "qualified path for local variable def in arm");
1189 self.fmt.variable_str(p.span,
1196 def::DefVariant(..) | def::DefTy(..) | def::DefStruct(..) => {
1197 paths_to_process.push((id, p.clone(), Some(ref_kind)))
1199 // FIXME(nrc) what are these doing here?
1200 def::DefStatic(_, _) |
1202 def::DefAssociatedConst(..) => {}
1203 _ => error!("unexpected definition kind when processing collected paths: {:?}",
1208 for &(id, ref path, ref_kind) in &paths_to_process {
1209 self.process_path(id, path, ref_kind);
1211 visit::walk_expr_opt(self, &arm.guard);
1212 self.visit_expr(&arm.body);
1215 fn visit_stmt(&mut self, s: &ast::Stmt) {
1216 if generated_code(s.span) {
1220 visit::walk_stmt(self, s)
1223 fn visit_local(&mut self, l: &ast::Local) {
1224 if generated_code(l.span) {
1228 // The local could declare multiple new vars, we must walk the
1229 // pattern and collect them all.
1230 let mut collector = PathCollector::new();
1231 collector.visit_pat(&l.pat);
1232 self.visit_pat(&l.pat);
1234 let value = self.span.snippet(l.span);
1236 for &(id, ref p, immut, _) in &collector.collected_paths {
1237 let value = if immut == ast::MutImmutable {
1240 "<mutable>".to_string()
1242 let types = self.tcx.node_types();
1243 let typ = types.get(&id).unwrap().to_string();
1244 // Get the span only for the name of the variable (I hope the path
1245 // is only ever a variable name, but who knows?).
1246 let sub_span = self.span.span_for_last_ident(p.span);
1247 // Rust uses the id of the pattern for var lookups, so we'll use it too.
1248 self.fmt.variable_str(p.span,
1256 // Just walk the initialiser and type (don't want to walk the pattern again).
1257 visit::walk_ty_opt(self, &l.ty);
1258 visit::walk_expr_opt(self, &l.init);