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 rustc_front::lowering::{lower_expr, LoweringContext};
52 use super::span_utils::SpanUtils;
53 use super::recorder::{Recorder, FmtStrs};
55 macro_rules! down_cast_data {
56 ($id:ident, $kind:ident, $this:ident, $sp:expr) => {
57 let $id = if let super::Data::$kind(data) = $id {
60 $this.sess.span_bug($sp, &format!("unexpected data kind: {:?}", $id));
65 pub struct DumpCsvVisitor<'l, 'tcx: 'l> {
66 save_ctxt: SaveContext<'l, 'tcx>,
68 tcx: &'l ty::ctxt<'tcx>,
69 analysis: &'l ty::CrateAnalysis<'l>,
72 fmt: FmtStrs<'l, 'tcx>,
77 impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
78 pub fn new(tcx: &'l ty::ctxt<'tcx>,
79 lcx: &'l LoweringContext<'l>,
80 analysis: &'l ty::CrateAnalysis<'l>,
81 output_file: Box<File>)
82 -> DumpCsvVisitor<'l, 'tcx> {
83 let span_utils = SpanUtils::new(&tcx.sess);
87 save_ctxt: SaveContext::from_span_utils(tcx, lcx, span_utils.clone()),
89 span: span_utils.clone(),
90 fmt: FmtStrs::new(box Recorder {
100 fn nest<F>(&mut self, scope_id: NodeId, f: F)
101 where F: FnOnce(&mut DumpCsvVisitor<'l, 'tcx>)
103 let parent_scope = self.cur_scope;
104 self.cur_scope = scope_id;
106 self.cur_scope = parent_scope;
109 pub fn dump_crate_info(&mut self, name: &str, krate: &ast::Crate) {
110 let source_file = self.tcx.sess.local_crate_source_file.as_ref();
111 let crate_root = match source_file {
112 Some(source_file) => match source_file.file_name() {
113 Some(_) => source_file.parent().unwrap().display().to_string(),
114 None => source_file.display().to_string(),
116 None => "<no source>".to_owned(),
119 // The current crate.
120 self.fmt.crate_str(krate.span, name, &crate_root);
122 // Dump info about all the external crates referenced from this crate.
123 for c in &self.save_ctxt.get_external_crates() {
124 self.fmt.external_crate_str(krate.span, &c.name, c.number);
126 self.fmt.recorder.record("end_external_crates\n");
129 // Return all non-empty prefixes of a path.
130 // For each prefix, we return the span for the last segment in the prefix and
131 // a str representation of the entire prefix.
132 fn process_path_prefixes(&self, path: &ast::Path) -> Vec<(Span, String)> {
133 let spans = self.span.spans_for_path_segments(path);
135 // Paths to enums seem to not match their spans - the span includes all the
136 // variants too. But they seem to always be at the end, so I hope we can cope with
137 // always using the first ones. So, only error out if we don't have enough spans.
138 // What could go wrong...?
139 if spans.len() < path.segments.len() {
140 error!("Mis-calculated spans for path '{}'. Found {} spans, expected {}. Found spans:",
141 path_to_string(path),
143 path.segments.len());
145 let loc = self.sess.codemap().lookup_char_pos(s.lo);
146 error!(" '{}' in {}, line {}",
147 self.span.snippet(*s),
154 let mut result: Vec<(Span, String)> = vec!();
156 let mut segs = vec!();
157 for (i, (seg, span)) in path.segments.iter().zip(&spans).enumerate() {
158 segs.push(seg.clone());
159 let sub_path = ast::Path {
160 span: *span, // span for the last segment
164 let qualname = if i == 0 && path.global {
165 format!("::{}", path_to_string(&sub_path))
167 path_to_string(&sub_path)
169 result.push((*span, qualname));
170 segs = sub_path.segments;
176 // The global arg allows us to override the global-ness of the path (which
177 // actually means 'does the path start with `::`', rather than 'is the path
178 // semantically global). We use the override for `use` imports (etc.) where
179 // the syntax is non-global, but the semantics are global.
180 fn write_sub_paths(&mut self, path: &ast::Path, global: bool) {
181 let sub_paths = self.process_path_prefixes(path);
182 for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
183 let qualname = if i == 0 && global && !path.global {
184 format!("::{}", qualname)
188 self.fmt.sub_mod_ref_str(path.span, *span, &qualname, self.cur_scope);
192 // As write_sub_paths, but does not process the last ident in the path (assuming it
193 // will be processed elsewhere). See note on write_sub_paths about global.
194 fn write_sub_paths_truncated(&mut self, path: &ast::Path, global: bool) {
195 let sub_paths = self.process_path_prefixes(path);
196 let len = sub_paths.len();
201 let sub_paths = &sub_paths[..len-1];
202 for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
203 let qualname = if i == 0 && global && !path.global {
204 format!("::{}", qualname)
208 self.fmt.sub_mod_ref_str(path.span, *span, &qualname, self.cur_scope);
212 // As write_sub_paths, but expects a path of the form module_path::trait::method
213 // Where trait could actually be a struct too.
214 fn write_sub_path_trait_truncated(&mut self, path: &ast::Path) {
215 let sub_paths = self.process_path_prefixes(path);
216 let len = sub_paths.len();
220 let sub_paths = &sub_paths[.. (len-1)];
222 // write the trait part of the sub-path
223 let (ref span, ref qualname) = sub_paths[len-2];
224 self.fmt.sub_type_ref_str(path.span, *span, &qualname);
226 // write the other sub-paths
230 let sub_paths = &sub_paths[..len-2];
231 for &(ref span, ref qualname) in sub_paths {
232 self.fmt.sub_mod_ref_str(path.span, *span, &qualname, self.cur_scope);
236 // looks up anything, not just a type
237 fn lookup_type_ref(&self, ref_id: NodeId) -> Option<DefId> {
238 if !self.tcx.def_map.borrow().contains_key(&ref_id) {
239 self.sess.bug(&format!("def_map has no key for {} in lookup_type_ref",
242 let def = self.tcx.def_map.borrow().get(&ref_id).unwrap().full_def();
244 def::DefPrimTy(..) => None,
245 def::DefSelfTy(..) => None,
246 _ => Some(def.def_id()),
250 fn lookup_def_kind(&self, ref_id: NodeId, span: Span) -> Option<recorder::Row> {
251 let def_map = self.tcx.def_map.borrow();
252 if !def_map.contains_key(&ref_id) {
253 self.sess.span_bug(span,
254 &format!("def_map has no key for {} in lookup_def_kind",
257 let def = def_map.get(&ref_id).unwrap().full_def();
260 def::DefForeignMod(_) => Some(recorder::ModRef),
261 def::DefStruct(_) => Some(recorder::TypeRef),
263 def::DefAssociatedTy(..) |
264 def::DefTrait(_) => Some(recorder::TypeRef),
265 def::DefStatic(_, _) |
267 def::DefAssociatedConst(..) |
269 def::DefVariant(_, _, _) |
270 def::DefUpvar(..) => Some(recorder::VarRef),
272 def::DefFn(..) => Some(recorder::FnRef),
276 def::DefTyParam(..) |
281 self.sess.span_bug(span,
282 &format!("lookup_def_kind for unexpected item: {:?}", def));
287 fn process_formals(&mut self, formals: &Vec<ast::Arg>, qualname: &str) {
289 self.visit_pat(&arg.pat);
290 let mut collector = PathCollector::new();
291 collector.visit_pat(&arg.pat);
292 let span_utils = self.span.clone();
293 for &(id, ref p, _, _) in &collector.collected_paths {
294 let typ = self.tcx.node_types().get(&id).unwrap().to_string();
295 // get the span only for the name of the variable (I hope the path is only ever a
296 // variable name, but who knows?)
297 self.fmt.formal_str(p.span,
298 span_utils.span_for_last_ident(p.span),
307 fn process_method(&mut self,
308 sig: &ast::MethodSig,
309 body: Option<&ast::Block>,
313 if generated_code(span) {
317 debug!("process_method: {}:{}", id, name);
319 let method_data = self.save_ctxt.get_method_data(id, name, span);
322 self.fmt.method_str(span,
323 Some(method_data.span),
325 &method_data.qualname,
326 method_data.declaration,
328 self.process_formals(&sig.decl.inputs, &method_data.qualname);
330 self.fmt.method_decl_str(span,
331 Some(method_data.span),
333 &method_data.qualname,
337 // walk arg and return types
338 for arg in &sig.decl.inputs {
339 self.visit_ty(&arg.ty);
342 if let ast::Return(ref ret_ty) = sig.decl.output {
343 self.visit_ty(ret_ty);
347 if let Some(body) = body {
348 self.nest(id, |v| v.visit_block(body));
351 self.process_generic_params(&sig.generics, span, &method_data.qualname, id);
354 fn process_trait_ref(&mut self, trait_ref: &ast::TraitRef) {
355 let trait_ref_data = self.save_ctxt.get_trait_ref_data(trait_ref, self.cur_scope);
356 if let Some(trait_ref_data) = trait_ref_data {
357 self.fmt.ref_str(recorder::TypeRef,
359 Some(trait_ref_data.span),
360 trait_ref_data.ref_id,
361 trait_ref_data.scope);
362 visit::walk_path(self, &trait_ref.path);
366 fn process_struct_field_def(&mut self, field: &ast::StructField, parent_id: NodeId) {
367 let field_data = self.save_ctxt.get_field_data(field, parent_id);
368 if let Some(field_data) = field_data {
369 self.fmt.field_str(field.span,
370 Some(field_data.span),
373 &field_data.qualname,
374 &field_data.type_value,
379 // Dump generic params bindings, then visit_generics
380 fn process_generic_params(&mut self,
381 generics: &ast::Generics,
385 // We can't only use visit_generics since we don't have spans for param
386 // bindings, so we reparse the full_span to get those sub spans.
387 // However full span is the entire enum/fn/struct block, so we only want
388 // the first few to match the number of generics we're looking for.
389 let param_sub_spans = self.span.spans_for_ty_params(full_span,
390 (generics.ty_params.len() as isize));
391 for (param, param_ss) in generics.ty_params.iter().zip(param_sub_spans) {
392 // Append $id to name to make sure each one is unique
393 let name = format!("{}::{}${}",
395 escape(self.span.snippet(param_ss)),
397 self.fmt.typedef_str(full_span, Some(param_ss), param.id, &name, "");
399 self.visit_generics(generics);
402 fn process_fn(&mut self,
405 ty_params: &ast::Generics,
407 let fn_data = self.save_ctxt.get_item_data(item);
408 down_cast_data!(fn_data, FunctionData, self, item.span);
409 self.fmt.fn_str(item.span,
416 self.process_formals(&decl.inputs, &fn_data.qualname);
417 self.process_generic_params(ty_params, item.span, &fn_data.qualname, item.id);
419 for arg in &decl.inputs {
420 self.visit_ty(&arg.ty);
423 if let ast::Return(ref ret_ty) = decl.output {
424 self.visit_ty(&ret_ty);
427 self.nest(item.id, |v| v.visit_block(&body));
430 fn process_static_or_const_item(&mut self, item: &ast::Item, typ: &ast::Ty, expr: &ast::Expr) {
431 let var_data = self.save_ctxt.get_item_data(item);
432 down_cast_data!(var_data, VariableData, self, item.span);
433 self.fmt.static_str(item.span,
439 &var_data.type_value,
443 self.visit_expr(expr);
446 fn process_const(&mut self,
452 let qualname = format!("::{}", self.tcx.map.path_to_string(id));
454 let sub_span = self.span.sub_span_after_keyword(span, keywords::Const);
456 self.fmt.static_str(span,
461 &self.span.snippet(expr.span),
462 &ty_to_string(&*typ),
465 // walk type and init value
467 self.visit_expr(expr);
470 fn process_struct(&mut self,
472 def: &ast::VariantData,
473 ty_params: &ast::Generics) {
474 let qualname = format!("::{}", self.tcx.map.path_to_string(item.id));
476 let val = self.span.snippet(item.span);
477 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Struct);
478 self.fmt.struct_str(item.span,
487 for field in def.fields() {
488 self.process_struct_field_def(field, item.id);
489 self.visit_ty(&field.node.ty);
492 self.process_generic_params(ty_params, item.span, &qualname, item.id);
495 fn process_enum(&mut self,
497 enum_definition: &ast::EnumDef,
498 ty_params: &ast::Generics) {
499 let enum_data = self.save_ctxt.get_item_data(item);
500 down_cast_data!(enum_data, EnumData, self, item.span);
501 self.fmt.enum_str(item.span,
502 Some(enum_data.span),
508 for variant in &enum_definition.variants {
509 let name = &variant.node.name.name.as_str();
510 let mut qualname = enum_data.qualname.clone();
511 qualname.push_str("::");
512 qualname.push_str(name);
513 let val = self.span.snippet(variant.span);
515 self.fmt.struct_variant_str(variant.span,
516 self.span.span_for_first_ident(variant.span),
517 variant.node.data.id(),
518 variant.node.data.id(),
524 for field in variant.node.data.fields() {
525 self.process_struct_field_def(field, variant.node.data.id());
526 self.visit_ty(&*field.node.ty);
529 self.process_generic_params(ty_params, item.span, &enum_data.qualname, enum_data.id);
532 fn process_impl(&mut self,
534 type_parameters: &ast::Generics,
535 trait_ref: &Option<ast::TraitRef>,
537 impl_items: &[P<ast::ImplItem>]) {
538 let impl_data = self.save_ctxt.get_item_data(item);
539 down_cast_data!(impl_data, ImplData, self, item.span);
540 match impl_data.self_ref {
541 Some(ref self_ref) => {
542 self.fmt.ref_str(recorder::TypeRef,
552 if let Some(ref trait_ref_data) = impl_data.trait_ref {
553 self.fmt.ref_str(recorder::TypeRef,
555 Some(trait_ref_data.span),
556 trait_ref_data.ref_id,
557 trait_ref_data.scope);
558 visit::walk_path(self, &trait_ref.as_ref().unwrap().path);
561 self.fmt.impl_str(item.span,
562 Some(impl_data.span),
564 impl_data.self_ref.map(|data| data.ref_id),
565 impl_data.trait_ref.map(|data| data.ref_id),
568 self.process_generic_params(type_parameters, item.span, "", item.id);
569 for impl_item in impl_items {
570 self.visit_impl_item(impl_item);
574 fn process_trait(&mut self,
576 generics: &ast::Generics,
577 trait_refs: &OwnedSlice<ast::TyParamBound>,
578 methods: &[P<ast::TraitItem>]) {
579 let qualname = format!("::{}", self.tcx.map.path_to_string(item.id));
580 let val = self.span.snippet(item.span);
581 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Trait);
582 self.fmt.trait_str(item.span,
590 for super_bound in trait_refs.iter() {
591 let trait_ref = match *super_bound {
592 ast::TraitTyParamBound(ref trait_ref, _) => {
595 ast::RegionTyParamBound(..) => {
600 let trait_ref = &trait_ref.trait_ref;
601 match self.lookup_type_ref(trait_ref.ref_id) {
603 let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span);
604 self.fmt.ref_str(recorder::TypeRef,
609 self.fmt.inherit_str(trait_ref.path.span, sub_span, id, item.id);
615 // walk generics and methods
616 self.process_generic_params(generics, item.span, &qualname, item.id);
617 for method in methods {
618 self.visit_trait_item(method)
622 // `item` is the module in question, represented as an item.
623 fn process_mod(&mut self, item: &ast::Item) {
624 let mod_data = self.save_ctxt.get_item_data(item);
625 down_cast_data!(mod_data, ModData, self, item.span);
626 self.fmt.mod_str(item.span,
634 fn process_path(&mut self, id: NodeId, path: &ast::Path, ref_kind: Option<recorder::Row>) {
635 if generated_code(path.span) {
639 let path_data = self.save_ctxt.get_path_data(id, path);
640 let path_data = match path_data {
643 self.tcx.sess.span_bug(path.span,
644 &format!("Unexpected def kind while looking up path in \
646 self.span.snippet(path.span)))
650 Data::VariableRefData(ref vrd) => {
651 self.fmt.ref_str(ref_kind.unwrap_or(recorder::VarRef),
658 Data::TypeRefData(ref trd) => {
659 self.fmt.ref_str(recorder::TypeRef,
665 Data::MethodCallData(ref mcd) => {
666 self.fmt.meth_call_str(path.span,
672 Data::FunctionCallData(fcd) => {
673 self.fmt.fn_call_str(path.span, Some(fcd.span), fcd.ref_id, fcd.scope);
676 self.sess.span_bug(path.span,
677 &format!("Unexpected data: {:?}", path_data));
681 // Modules or types in the path prefix.
682 let def_map = self.tcx.def_map.borrow();
683 let def = def_map.get(&id).unwrap().full_def();
685 def::DefMethod(did) => {
686 let ti = self.tcx.impl_or_trait_item(did);
687 if let ty::MethodTraitItem(m) = ti {
688 if m.explicit_self == ty::StaticExplicitSelfCategory {
689 self.write_sub_path_trait_truncated(path);
694 def::DefStatic(_,_) |
696 def::DefAssociatedConst(..) |
698 def::DefVariant(..) |
699 def::DefFn(..) => self.write_sub_paths_truncated(path, false),
704 fn process_struct_lit(&mut self,
707 fields: &Vec<ast::Field>,
708 variant: ty::VariantDef,
709 base: &Option<P<ast::Expr>>) {
710 if generated_code(path.span) {
714 self.write_sub_paths_truncated(path, false);
716 if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) {
717 down_cast_data!(struct_lit_data, TypeRefData, self, ex.span);
718 self.fmt.ref_str(recorder::TypeRef,
720 Some(struct_lit_data.span),
721 struct_lit_data.ref_id,
722 struct_lit_data.scope);
723 let scope = self.save_ctxt.enclosing_scope(ex.id);
725 for field in fields {
726 if generated_code(field.ident.span) {
730 let field_data = self.save_ctxt.get_field_ref_data(field, variant, scope);
731 self.fmt.ref_str(recorder::VarRef,
733 Some(field_data.span),
737 self.visit_expr(&field.expr)
741 walk_list!(self, visit_expr, base);
744 fn process_method_call(&mut self, ex: &ast::Expr, args: &Vec<P<ast::Expr>>) {
745 if let Some(call_data) = self.save_ctxt.get_expr_data(ex) {
746 down_cast_data!(call_data, MethodCallData, self, ex.span);
747 self.fmt.meth_call_str(ex.span,
748 Some(call_data.span),
754 // walk receiver and args
755 walk_list!(self, visit_expr, args);
758 fn process_pat(&mut self, p: &ast::Pat) {
759 if generated_code(p.span) {
764 ast::PatStruct(ref path, ref fields, _) => {
765 visit::walk_path(self, path);
766 let adt = self.tcx.node_id_to_type(p.id).ty_adt_def().unwrap();
767 let def = self.tcx.def_map.borrow()[&p.id].full_def();
768 let variant = adt.variant_of_def(def);
770 for &Spanned { node: ref field, span } in fields {
771 if generated_code(span) {
775 let sub_span = self.span.span_for_first_ident(span);
776 if let Some(f) = variant.find_field_named(field.ident.name) {
777 self.fmt.ref_str(recorder::VarRef, span, sub_span, f.did, self.cur_scope);
779 self.visit_pat(&field.pat);
782 _ => visit::walk_pat(self, p),
787 fn process_var_decl(&mut self, p: &ast::Pat, value: String) {
788 // The local could declare multiple new vars, we must walk the
789 // pattern and collect them all.
790 let mut collector = PathCollector::new();
791 collector.visit_pat(&p);
794 for &(id, ref p, immut, _) in &collector.collected_paths {
795 let value = if immut == ast::MutImmutable {
798 "<mutable>".to_string()
800 let types = self.tcx.node_types();
801 let typ = types.get(&id).unwrap().to_string();
802 // Get the span only for the name of the variable (I hope the path
803 // is only ever a variable name, but who knows?).
804 let sub_span = self.span.span_for_last_ident(p.span);
805 // Rust uses the id of the pattern for var lookups, so we'll use it too.
806 self.fmt.variable_str(p.span,
816 impl<'l, 'tcx, 'v> Visitor<'v> for DumpCsvVisitor<'l, 'tcx> {
817 fn visit_item(&mut self, item: &ast::Item) {
818 if generated_code(item.span) {
823 ast::ItemUse(ref use_item) => {
824 match use_item.node {
825 ast::ViewPathSimple(ident, ref path) => {
826 let sub_span = self.span.span_for_last_ident(path.span);
827 let mod_id = match self.lookup_type_ref(item.id) {
829 match self.lookup_def_kind(item.id, path.span) {
830 Some(kind) => self.fmt.ref_str(kind,
842 // 'use' always introduces an alias, if there is not an explicit
843 // one, there is an implicit one.
844 let sub_span = match self.span.sub_span_after_keyword(use_item.span,
846 Some(sub_span) => Some(sub_span),
850 self.fmt.use_alias_str(path.span,
854 &ident.name.as_str(),
856 self.write_sub_paths_truncated(path, true);
858 ast::ViewPathGlob(ref path) => {
859 // Make a comma-separated list of names of imported modules.
860 let mut name_string = String::new();
861 let glob_map = &self.analysis.glob_map;
862 let glob_map = glob_map.as_ref().unwrap();
863 if glob_map.contains_key(&item.id) {
864 for n in glob_map.get(&item.id).unwrap() {
865 if !name_string.is_empty() {
866 name_string.push_str(", ");
868 name_string.push_str(&n.as_str());
872 let sub_span = self.span
873 .sub_span_of_token(path.span, token::BinOp(token::Star));
874 self.fmt.use_glob_str(path.span,
879 self.write_sub_paths(path, true);
881 ast::ViewPathList(ref path, ref list) => {
884 ast::PathListIdent { id, .. } => {
885 match self.lookup_type_ref(id) {
886 Some(def_id) => match self.lookup_def_kind(id, plid.span) {
888 self.fmt.ref_str(kind,
899 ast::PathListMod { .. } => (),
903 self.write_sub_paths(path, true);
907 ast::ItemExternCrate(ref s) => {
908 let location = match *s {
909 Some(s) => s.to_string(),
910 None => item.ident.to_string(),
912 let alias_span = self.span.span_for_last_ident(item.span);
913 let cnum = match self.sess.cstore.extern_mod_stmt_cnum(item.id) {
917 self.fmt.extern_crate_str(item.span,
921 &item.ident.name.as_str(),
925 ast::ItemFn(ref decl, _, _, _, ref ty_params, ref body) =>
926 self.process_fn(item, &**decl, ty_params, &**body),
927 ast::ItemStatic(ref typ, _, ref expr) =>
928 self.process_static_or_const_item(item, typ, expr),
929 ast::ItemConst(ref typ, ref expr) =>
930 self.process_static_or_const_item(item, &typ, &expr),
931 ast::ItemStruct(ref def, ref ty_params) => self.process_struct(item, def, ty_params),
932 ast::ItemEnum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
938 self.process_impl(item, ty_params, trait_ref, &typ, impl_items)
940 ast::ItemTrait(_, ref generics, ref trait_refs, ref methods) =>
941 self.process_trait(item, generics, trait_refs, methods),
942 ast::ItemMod(ref m) => {
943 self.process_mod(item);
944 self.nest(item.id, |v| visit::walk_mod(v, m));
946 ast::ItemTy(ref ty, ref ty_params) => {
947 let qualname = format!("::{}", self.tcx.map.path_to_string(item.id));
948 let value = ty_to_string(&**ty);
949 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Type);
950 self.fmt.typedef_str(item.span, sub_span, item.id, &qualname, &value);
952 self.visit_ty(&**ty);
953 self.process_generic_params(ty_params, item.span, &qualname, item.id);
955 ast::ItemMac(_) => (),
956 _ => visit::walk_item(self, item),
960 fn visit_generics(&mut self, generics: &ast::Generics) {
961 for param in generics.ty_params.iter() {
962 for bound in param.bounds.iter() {
963 if let ast::TraitTyParamBound(ref trait_ref, _) = *bound {
964 self.process_trait_ref(&trait_ref.trait_ref);
967 if let Some(ref ty) = param.default {
968 self.visit_ty(&**ty);
973 fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
974 match trait_item.node {
975 ast::ConstTraitItem(ref ty, Some(ref expr)) => {
976 self.process_const(trait_item.id,
977 trait_item.ident.name,
982 ast::MethodTraitItem(ref sig, ref body) => {
983 self.process_method(sig,
984 body.as_ref().map(|x| &**x),
986 trait_item.ident.name,
989 ast::ConstTraitItem(_, None) |
990 ast::TypeTraitItem(..) => {}
994 fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
995 match impl_item.node {
996 ast::ImplItemKind::Const(ref ty, ref expr) => {
997 self.process_const(impl_item.id,
998 impl_item.ident.name,
1003 ast::ImplItemKind::Method(ref sig, ref body) => {
1004 self.process_method(sig,
1007 impl_item.ident.name,
1010 ast::ImplItemKind::Type(_) |
1011 ast::ImplItemKind::Macro(_) => {}
1015 fn visit_ty(&mut self, t: &ast::Ty) {
1016 if generated_code(t.span) {
1021 ast::TyPath(_, ref path) => {
1022 match self.lookup_type_ref(t.id) {
1024 let sub_span = self.span.sub_span_for_type_name(t.span);
1025 self.fmt.ref_str(recorder::TypeRef, t.span, sub_span, id, self.cur_scope);
1030 self.write_sub_paths_truncated(path, false);
1032 visit::walk_path(self, path);
1034 _ => visit::walk_ty(self, t),
1038 fn visit_expr(&mut self, ex: &ast::Expr) {
1039 if generated_code(ex.span) {
1044 ast::ExprCall(ref _f, ref _args) => {
1045 // Don't need to do anything for function calls,
1046 // because just walking the callee path does what we want.
1047 visit::walk_expr(self, ex);
1049 ast::ExprPath(_, ref path) => {
1050 self.process_path(ex.id, path, None);
1051 visit::walk_expr(self, ex);
1053 ast::ExprStruct(ref path, ref fields, ref base) => {
1054 let hir_expr = lower_expr(self.save_ctxt.lcx, ex);
1055 let adt = self.tcx.expr_ty(&hir_expr).ty_adt_def().unwrap();
1056 let def = self.tcx.resolve_expr(&hir_expr);
1057 self.process_struct_lit(ex, path, fields, adt.variant_of_def(def), base)
1059 ast::ExprMethodCall(_, _, ref args) => self.process_method_call(ex, args),
1060 ast::ExprField(ref sub_ex, _) => {
1061 if generated_code(sub_ex.span) {
1065 self.visit_expr(&sub_ex);
1067 if let Some(field_data) = self.save_ctxt.get_expr_data(ex) {
1068 down_cast_data!(field_data, VariableRefData, self, ex.span);
1069 self.fmt.ref_str(recorder::VarRef,
1071 Some(field_data.span),
1076 ast::ExprTupField(ref sub_ex, idx) => {
1077 if generated_code(sub_ex.span) {
1081 self.visit_expr(&**sub_ex);
1083 let hir_node = lower_expr(self.save_ctxt.lcx, sub_ex);
1084 let ty = &self.tcx.expr_ty_adjusted(&hir_node).sty;
1086 ty::TyStruct(def, _) => {
1087 let sub_span = self.span.sub_span_after_token(ex.span, token::Dot);
1088 self.fmt.ref_str(recorder::VarRef,
1091 def.struct_variant().fields[idx.node].did,
1094 ty::TyTuple(_) => {}
1095 _ => self.sess.span_bug(ex.span,
1096 &format!("Expected struct or tuple type, found {:?}",
1100 ast::ExprClosure(_, ref decl, ref body) => {
1101 if generated_code(body.span) {
1105 let mut id = String::from("$");
1106 id.push_str(&ex.id.to_string());
1107 self.process_formals(&decl.inputs, &id);
1109 // walk arg and return types
1110 for arg in &decl.inputs {
1111 self.visit_ty(&*arg.ty);
1114 if let ast::Return(ref ret_ty) = decl.output {
1115 self.visit_ty(&**ret_ty);
1119 self.nest(ex.id, |v| v.visit_block(&**body));
1121 ast::ExprForLoop(ref pattern, ref subexpression, ref block, _) |
1122 ast::ExprWhileLet(ref pattern, ref subexpression, ref block, _) => {
1123 let value = self.span.snippet(mk_sp(ex.span.lo, subexpression.span.hi));
1124 self.process_var_decl(pattern, value);
1125 visit::walk_expr(self, subexpression);
1126 visit::walk_block(self, block);
1128 ast::ExprIfLet(ref pattern, ref subexpression, ref block, ref opt_else) => {
1129 let value = self.span.snippet(mk_sp(ex.span.lo, subexpression.span.hi));
1130 self.process_var_decl(pattern, value);
1131 visit::walk_expr(self, subexpression);
1132 visit::walk_block(self, block);
1133 opt_else.as_ref().map(|el| visit::walk_expr(self, el));
1136 visit::walk_expr(self, ex)
1141 fn visit_mac(&mut self, _: &ast::Mac) {
1142 // Just stop, macros are poison to us.
1145 fn visit_pat(&mut self, p: &ast::Pat) {
1146 self.process_pat(p);
1149 fn visit_arm(&mut self, arm: &ast::Arm) {
1150 let mut collector = PathCollector::new();
1151 for pattern in &arm.pats {
1152 // collect paths from the arm's patterns
1153 collector.visit_pat(&pattern);
1154 self.visit_pat(&pattern);
1157 // This is to get around borrow checking, because we need mut self to call process_path.
1158 let mut paths_to_process = vec![];
1160 // process collected paths
1161 for &(id, ref p, immut, ref_kind) in &collector.collected_paths {
1162 let def_map = self.tcx.def_map.borrow();
1163 if !def_map.contains_key(&id) {
1164 self.sess.span_bug(p.span,
1165 &format!("def_map has no key for {} in visit_arm", id));
1167 let def = def_map.get(&id).unwrap().full_def();
1169 def::DefLocal(_, id) => {
1170 let value = if immut == ast::MutImmutable {
1171 self.span.snippet(p.span).to_string()
1173 "<mutable>".to_string()
1176 assert!(p.segments.len() == 1,
1177 "qualified path for local variable def in arm");
1178 self.fmt.variable_str(p.span, Some(p.span), id, &path_to_string(p), &value, "")
1180 def::DefVariant(..) | def::DefTy(..) | def::DefStruct(..) => {
1181 paths_to_process.push((id, p.clone(), Some(ref_kind)))
1183 // FIXME(nrc) what are these doing here?
1184 def::DefStatic(_, _) |
1186 def::DefAssociatedConst(..) => {}
1187 _ => error!("unexpected definition kind when processing collected paths: {:?}",
1192 for &(id, ref path, ref_kind) in &paths_to_process {
1193 self.process_path(id, path, ref_kind);
1195 walk_list!(self, visit_expr, &arm.guard);
1196 self.visit_expr(&arm.body);
1199 fn visit_stmt(&mut self, s: &ast::Stmt) {
1200 if generated_code(s.span) {
1204 visit::walk_stmt(self, s)
1207 fn visit_local(&mut self, l: &ast::Local) {
1208 if generated_code(l.span) {
1212 let value = self.span.snippet(l.span);
1213 self.process_var_decl(&l.pat, value);
1215 // Just walk the initialiser and type (don't want to walk the pattern again).
1216 walk_list!(self, visit_ty, &l.ty);
1217 walk_list!(self, visit_expr, &l.init);