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};
36 use middle::ty::{self, Ty};
43 use syntax::ast::{self, NodeId, DefId};
44 use syntax::ast_map::NodeItem;
45 use syntax::codemap::*;
46 use syntax::parse::token::{self, get_ident, keywords};
47 use syntax::owned_slice::OwnedSlice;
48 use syntax::visit::{self, Visitor};
49 use syntax::print::pprust::{path_to_string, ty_to_string};
52 use super::span_utils::SpanUtils;
53 use super::recorder::{Recorder, FmtStrs};
58 pub struct DumpCsvVisitor<'l, 'tcx: 'l> {
59 save_ctxt: SaveContext<'l, 'tcx>,
61 analysis: &'l ty::CrateAnalysis<'tcx>,
69 impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
70 pub fn new(sess: &'l Session,
71 analysis: &'l ty::CrateAnalysis<'tcx>,
72 output_file: Box<File>) -> DumpCsvVisitor<'l, 'tcx> {
75 save_ctxt: SaveContext::new(sess, analysis, SpanUtils {
77 err_count: Cell::new(0)
82 err_count: Cell::new(0)
84 fmt: FmtStrs::new(box Recorder {
90 err_count: Cell::new(0)
96 fn nest<F>(&mut self, scope_id: NodeId, f: F) where
97 F: FnOnce(&mut DumpCsvVisitor<'l, 'tcx>),
99 let parent_scope = self.cur_scope;
100 self.cur_scope = scope_id;
102 self.cur_scope = parent_scope;
105 pub fn dump_crate_info(&mut self, name: &str, krate: &ast::Crate) {
106 // The current crate.
107 self.fmt.crate_str(krate.span, name);
109 // Dump info about all the external crates referenced from this crate.
110 for c in &self.save_ctxt.get_external_crates() {
111 self.fmt.external_crate_str(krate.span, &c.name, c.number);
113 self.fmt.recorder.record("end_external_crates\n");
116 // Return all non-empty prefixes of a path.
117 // For each prefix, we return the span for the last segment in the prefix and
118 // a str representation of the entire prefix.
119 fn process_path_prefixes(&self, path: &ast::Path) -> Vec<(Span, String)> {
120 let spans = self.span.spans_for_path_segments(path);
122 // Paths to enums seem to not match their spans - the span includes all the
123 // variants too. But they seem to always be at the end, so I hope we can cope with
124 // always using the first ones. So, only error out if we don't have enough spans.
125 // What could go wrong...?
126 if spans.len() < path.segments.len() {
127 error!("Mis-calculated spans for path '{}'. \
128 Found {} spans, expected {}. Found spans:",
129 path_to_string(path), spans.len(), path.segments.len());
131 let loc = self.sess.codemap().lookup_char_pos(s.lo);
132 error!(" '{}' in {}, line {}",
133 self.span.snippet(*s), loc.file.name, loc.line);
138 let mut result: Vec<(Span, String)> = vec!();
140 let mut segs = vec!();
141 for (i, (seg, span)) in path.segments.iter().zip(spans.iter()).enumerate() {
142 segs.push(seg.clone());
143 let sub_path = ast::Path{span: *span, // span for the last segment
146 let qualname = if i == 0 && path.global {
147 format!("::{}", path_to_string(&sub_path))
149 path_to_string(&sub_path)
151 result.push((*span, qualname));
152 segs = sub_path.segments;
158 // The global arg allows us to override the global-ness of the path (which
159 // actually means 'does the path start with `::`', rather than 'is the path
160 // semantically global). We use the override for `use` imports (etc.) where
161 // the syntax is non-global, but the semantics are global.
162 fn write_sub_paths(&mut self, path: &ast::Path, global: bool) {
163 let sub_paths = self.process_path_prefixes(path);
164 for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
165 let qualname = if i == 0 && global && !path.global {
166 format!("::{}", qualname)
170 self.fmt.sub_mod_ref_str(path.span,
177 // As write_sub_paths, but does not process the last ident in the path (assuming it
178 // will be processed elsewhere). See note on write_sub_paths about global.
179 fn write_sub_paths_truncated(&mut self, path: &ast::Path, global: bool) {
180 let sub_paths = self.process_path_prefixes(path);
181 let len = sub_paths.len();
186 let sub_paths = &sub_paths[..len-1];
187 for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
188 let qualname = if i == 0 && global && !path.global {
189 format!("::{}", qualname)
193 self.fmt.sub_mod_ref_str(path.span,
200 // As write_sub_paths, but expects a path of the form module_path::trait::method
201 // Where trait could actually be a struct too.
202 fn write_sub_path_trait_truncated(&mut self, path: &ast::Path) {
203 let sub_paths = self.process_path_prefixes(path);
204 let len = sub_paths.len();
208 let sub_paths = &sub_paths[.. (len-1)];
210 // write the trait part of the sub-path
211 let (ref span, ref qualname) = sub_paths[len-2];
212 self.fmt.sub_type_ref_str(path.span,
216 // write the other sub-paths
220 let sub_paths = &sub_paths[..len-2];
221 for &(ref span, ref qualname) in sub_paths {
222 self.fmt.sub_mod_ref_str(path.span,
229 // looks up anything, not just a type
230 fn lookup_type_ref(&self, ref_id: NodeId) -> Option<DefId> {
231 if !self.analysis.ty_cx.def_map.borrow().contains_key(&ref_id) {
232 self.sess.bug(&format!("def_map has no key for {} in lookup_type_ref",
235 let def = self.analysis.ty_cx.def_map.borrow().get(&ref_id).unwrap().full_def();
237 def::DefPrimTy(_) => None,
238 _ => Some(def.def_id()),
242 fn lookup_def_kind(&self, ref_id: NodeId, span: Span) -> Option<recorder::Row> {
243 let def_map = self.analysis.ty_cx.def_map.borrow();
244 if !def_map.contains_key(&ref_id) {
245 self.sess.span_bug(span, &format!("def_map has no key for {} in lookup_def_kind",
248 let def = def_map.get(&ref_id).unwrap().full_def();
251 def::DefForeignMod(_) => Some(recorder::ModRef),
252 def::DefStruct(_) => Some(recorder::StructRef),
254 def::DefAssociatedTy(..) |
255 def::DefTrait(_) => Some(recorder::TypeRef),
256 def::DefStatic(_, _) |
258 def::DefAssociatedConst(..) |
260 def::DefVariant(_, _, _) |
261 def::DefUpvar(..) => Some(recorder::VarRef),
263 def::DefFn(..) => Some(recorder::FnRef),
268 def::DefTyParam(..) |
271 def::DefPrimTy(_) => {
272 self.sess.span_bug(span, &format!("lookup_def_kind for unexpected item: {:?}",
278 fn process_formals(&mut self, formals: &Vec<ast::Arg>, qualname: &str) {
280 self.visit_pat(&arg.pat);
281 let mut collector = PathCollector::new();
282 collector.visit_pat(&arg.pat);
283 let span_utils = self.span.clone();
284 for &(id, ref p, _, _) in &collector.collected_paths {
287 &self.analysis.ty_cx,
288 *self.analysis.ty_cx.node_types().get(&id).unwrap());
289 // get the span only for the name of the variable (I hope the path is only ever a
290 // variable name, but who knows?)
291 self.fmt.formal_str(p.span,
292 span_utils.span_for_last_ident(p.span),
301 fn process_method(&mut self, sig: &ast::MethodSig,
302 body: Option<&ast::Block>,
303 id: ast::NodeId, name: ast::Name,
305 if generated_code(span) {
309 debug!("process_method: {}:{}", id, token::get_name(name));
312 // The qualname for a method is the trait name or name of the struct in an impl in
313 // which the method is declared in, followed by the method's name.
314 let qualname = match ty::impl_of_method(&self.analysis.ty_cx,
315 ast_util::local_def(id)) {
316 Some(impl_id) => match self.analysis.ty_cx.map.get(impl_id.node) {
320 ast::ItemImpl(_, _, _, _, ref ty, _) => {
321 let mut result = String::from_str("<");
322 result.push_str(&ty_to_string(&**ty));
324 match ty::trait_of_item(&self.analysis.ty_cx,
325 ast_util::local_def(id)) {
327 result.push_str(" as ");
329 &ty::item_path_str(&self.analysis.ty_cx, def_id));
333 result.push_str(">");
337 self.sess.span_bug(span,
338 &format!("Container {} for method {} not an impl?",
344 self.sess.span_bug(span,
345 &format!("Container {} for method {} is not a node item {:?}",
346 impl_id.node, id, self.analysis.ty_cx.map.get(impl_id.node)));
349 None => match ty::trait_of_item(&self.analysis.ty_cx,
350 ast_util::local_def(id)) {
352 scope_id = def_id.node;
353 match self.analysis.ty_cx.map.get(def_id.node) {
355 format!("::{}", ty::item_path_str(&self.analysis.ty_cx, def_id))
358 self.sess.span_bug(span,
359 &format!("Could not find container {} for method {}",
365 self.sess.span_bug(span,
366 &format!("Could not find container for method {}", id));
371 let qualname = &format!("{}::{}", qualname, &token::get_name(name));
373 // record the decl for this def (if it has one)
374 let decl_id = ty::trait_item_of_item(&self.analysis.ty_cx,
375 ast_util::local_def(id))
377 let def_id = new_id.def_id();
378 if def_id.node != 0 && def_id != ast_util::local_def(id) {
385 let sub_span = self.span.sub_span_after_keyword(span, keywords::Fn);
387 self.fmt.method_str(span,
393 self.process_formals(&sig.decl.inputs, qualname);
395 self.fmt.method_decl_str(span,
402 // walk arg and return types
403 for arg in &sig.decl.inputs {
404 self.visit_ty(&arg.ty);
407 if let ast::Return(ref ret_ty) = sig.decl.output {
408 self.visit_ty(ret_ty);
412 if let Some(body) = body {
413 self.nest(id, |v| v.visit_block(body));
416 self.process_generic_params(&sig.generics,
422 fn process_trait_ref(&mut self,
423 trait_ref: &ast::TraitRef) {
424 match self.lookup_type_ref(trait_ref.ref_id) {
426 let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span);
427 self.fmt.ref_str(recorder::TypeRef,
432 visit::walk_path(self, &trait_ref.path);
438 fn process_struct_field_def(&mut self,
439 field: &ast::StructField,
442 match field.node.kind {
443 ast::NamedField(ident, _) => {
444 let name = get_ident(ident);
445 let qualname = format!("{}::{}", qualname, name);
448 &self.analysis.ty_cx,
449 *self.analysis.ty_cx.node_types().get(&field.node.id).unwrap());
450 match self.span.sub_span_before_token(field.span, token::Colon) {
451 Some(sub_span) => self.fmt.field_str(field.span,
458 None => self.sess.span_bug(field.span,
459 &format!("Could not find sub-span for field {}",
467 // Dump generic params bindings, then visit_generics
468 fn process_generic_params(&mut self,
469 generics:&ast::Generics,
473 // We can't only use visit_generics since we don't have spans for param
474 // bindings, so we reparse the full_span to get those sub spans.
475 // However full span is the entire enum/fn/struct block, so we only want
476 // the first few to match the number of generics we're looking for.
477 let param_sub_spans = self.span.spans_for_ty_params(full_span,
478 (generics.ty_params.len() as isize));
479 for (param, param_ss) in generics.ty_params.iter().zip(param_sub_spans.iter()) {
480 // Append $id to name to make sure each one is unique
481 let name = format!("{}::{}${}",
483 escape(self.span.snippet(*param_ss)),
485 self.fmt.typedef_str(full_span,
491 self.visit_generics(generics);
494 fn process_fn(&mut self,
497 ty_params: &ast::Generics,
499 let fn_data = self.save_ctxt.get_item_data(item);
500 if let super::Data::FunctionData(fn_data) = fn_data {
501 self.fmt.fn_str(item.span,
508 self.process_formals(&decl.inputs, &fn_data.qualname);
509 self.process_generic_params(ty_params, item.span, &fn_data.qualname, item.id);
511 self.sess.span_bug(item.span, "expected FunctionData");
514 for arg in &decl.inputs {
515 self.visit_ty(&arg.ty);
518 if let ast::Return(ref ret_ty) = decl.output {
519 self.visit_ty(&ret_ty);
522 self.nest(item.id, |v| v.visit_block(&body));
525 fn process_static_or_const_item(&mut self,
530 let var_data = self.save_ctxt.get_item_data(item);
531 if let super::Data::VariableData(var_data) = var_data {
532 self.fmt.static_str(item.span,
538 &var_data.type_value,
541 self.sess.span_bug(item.span, "expected VariableData");
545 self.visit_expr(expr);
548 fn process_const(&mut self,
555 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(id));
557 let sub_span = self.span.sub_span_after_keyword(span,
560 self.fmt.static_str(span,
563 &get_ident((*ident).clone()),
565 &self.span.snippet(expr.span),
566 &ty_to_string(&*typ),
569 // walk type and init value
571 self.visit_expr(expr);
574 fn process_struct(&mut self,
576 def: &ast::StructDef,
577 ty_params: &ast::Generics) {
578 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
580 let ctor_id = match def.ctor_id {
581 Some(node_id) => node_id,
584 let val = self.span.snippet(item.span);
585 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Struct);
586 self.fmt.struct_str(item.span,
595 for field in &def.fields {
596 self.process_struct_field_def(field, &qualname, item.id);
597 self.visit_ty(&*field.node.ty);
600 self.process_generic_params(ty_params, item.span, &qualname, item.id);
603 fn process_enum(&mut self,
605 enum_definition: &ast::EnumDef,
606 ty_params: &ast::Generics) {
607 let enum_data = self.save_ctxt.get_item_data(item);
608 if let super::Data::EnumData(enum_data) = enum_data {
609 self.fmt.enum_str(item.span,
610 Some(enum_data.span),
615 for variant in &enum_definition.variants {
616 let name = &get_ident(variant.node.name);
617 let mut qualname = enum_data.qualname.clone();
618 qualname.push_str("::");
619 qualname.push_str(name);
620 let val = self.span.snippet(variant.span);
621 match variant.node.kind {
622 ast::TupleVariantKind(ref args) => {
623 // first ident in span is the variant's name
624 self.fmt.tuple_variant_str(variant.span,
625 self.span.span_for_first_ident(variant.span),
633 self.visit_ty(&*arg.ty);
636 ast::StructVariantKind(ref struct_def) => {
637 let ctor_id = match struct_def.ctor_id {
638 Some(node_id) => node_id,
641 self.fmt.struct_variant_str(variant.span,
642 self.span.span_for_first_ident(variant.span),
650 for field in &struct_def.fields {
651 self.process_struct_field_def(field, &qualname, variant.node.id);
652 self.visit_ty(&*field.node.ty);
657 self.process_generic_params(ty_params, item.span, &enum_data.qualname, item.id);
659 self.sess.span_bug(item.span, "expected EnumData");
663 fn process_impl(&mut self,
665 type_parameters: &ast::Generics,
666 trait_ref: &Option<ast::TraitRef>,
668 impl_items: &[P<ast::ImplItem>]) {
669 let trait_id = trait_ref.as_ref().and_then(|tr| self.lookup_type_ref(tr.ref_id));
671 // Common case impl for a struct or something basic.
672 ast::TyPath(None, ref path) => {
673 let sub_span = self.span.sub_span_for_type_name(path.span);
674 let self_id = self.lookup_type_ref(typ.id).map(|id| {
675 self.fmt.ref_str(recorder::TypeRef,
682 self.fmt.impl_str(path.span,
690 // Less useful case, impl for a compound type.
691 self.visit_ty(&*typ);
693 let sub_span = self.span.sub_span_for_type_name(typ.span);
694 self.fmt.impl_str(typ.span,
704 Some(ref trait_ref) => self.process_trait_ref(trait_ref),
708 self.process_generic_params(type_parameters, item.span, "", item.id);
709 for impl_item in impl_items {
710 self.visit_impl_item(impl_item);
714 fn process_trait(&mut self,
716 generics: &ast::Generics,
717 trait_refs: &OwnedSlice<ast::TyParamBound>,
718 methods: &[P<ast::TraitItem>]) {
719 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
720 let val = self.span.snippet(item.span);
721 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Trait);
722 self.fmt.trait_str(item.span,
730 for super_bound in &**trait_refs {
731 let trait_ref = match *super_bound {
732 ast::TraitTyParamBound(ref trait_ref, _) => {
735 ast::RegionTyParamBound(..) => {
740 let trait_ref = &trait_ref.trait_ref;
741 match self.lookup_type_ref(trait_ref.ref_id) {
743 let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span);
744 self.fmt.ref_str(recorder::TypeRef,
749 self.fmt.inherit_str(trait_ref.path.span,
758 // walk generics and methods
759 self.process_generic_params(generics, item.span, &qualname, item.id);
760 for method in methods {
761 self.visit_trait_item(method)
765 fn process_mod(&mut self,
766 item: &ast::Item) { // The module in question, represented as an item.
767 let mod_data = self.save_ctxt.get_item_data(item);
768 if let super::Data::ModData(mod_data) = mod_data {
769 self.fmt.mod_str(item.span,
776 self.sess.span_bug(item.span, "expected ModData");
780 fn process_path(&mut self,
784 ref_kind: Option<recorder::Row>) {
785 if generated_code(span) {
789 let def_map = self.analysis.ty_cx.def_map.borrow();
790 if !def_map.contains_key(&id) {
791 self.sess.span_bug(span,
792 &format!("def_map has no key for {} in visit_expr", id));
794 let def = def_map.get(&id).unwrap().full_def();
795 let sub_span = self.span.span_for_last_ident(span);
801 def::DefAssociatedConst(..) |
802 def::DefVariant(..) => self.fmt.ref_str(ref_kind.unwrap_or(recorder::VarRef),
807 def::DefStruct(def_id) => self.fmt.ref_str(recorder::StructRef,
812 def::DefTy(def_id, _) => self.fmt.ref_str(recorder::TypeRef,
817 def::DefMethod(declid, provenence) => {
818 let sub_span = self.span.sub_span_for_meth_name(span);
819 let defid = if declid.krate == ast::LOCAL_CRATE {
820 let ti = ty::impl_or_trait_item(&self.analysis.ty_cx,
823 def::FromTrait(def_id) => {
824 Some(ty::trait_items(&self.analysis.ty_cx,
828 mr.name() == ti.name()
833 def::FromImpl(def_id) => {
834 let impl_items = self.analysis
838 Some(impl_items.get(&def_id)
842 ty::impl_or_trait_item(
843 &self.analysis.ty_cx,
845 ).name() == ti.name()
854 self.fmt.meth_call_str(span,
860 def::DefFn(def_id, _) => {
861 self.fmt.fn_call_str(span,
866 _ => self.sess.span_bug(span,
867 &format!("Unexpected def kind while looking \
868 up path in `{}`: `{:?}`",
869 self.span.snippet(span),
872 // modules or types in the path prefix
874 def::DefMethod(did, _) => {
875 let ti = ty::impl_or_trait_item(&self.analysis.ty_cx, did);
876 if let ty::MethodTraitItem(m) = ti {
877 if m.explicit_self == ty::StaticExplicitSelfCategory {
878 self.write_sub_path_trait_truncated(path);
883 def::DefStatic(_,_) |
885 def::DefAssociatedConst(..) |
887 def::DefVariant(..) |
888 def::DefFn(..) => self.write_sub_paths_truncated(path, false),
893 fn process_struct_lit(&mut self,
896 fields: &Vec<ast::Field>,
897 base: &Option<P<ast::Expr>>) {
898 if generated_code(path.span) {
902 self.write_sub_paths_truncated(path, false);
904 let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, ex).sty;
905 let struct_def = match *ty {
906 ty::ty_struct(def_id, _) => {
907 let sub_span = self.span.span_for_last_ident(path.span);
908 self.fmt.ref_str(recorder::StructRef,
918 for field in fields {
920 Some(struct_def) => {
921 let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, struct_def);
923 if generated_code(field.ident.span) {
926 if f.name == field.ident.node.name {
927 // We don't really need a sub-span here, but no harm done
928 let sub_span = self.span.span_for_last_ident(field.ident.span);
929 self.fmt.ref_str(recorder::VarRef,
940 self.visit_expr(&*field.expr)
942 visit::walk_expr_opt(self, base)
945 fn process_method_call(&mut self,
947 args: &Vec<P<ast::Expr>>) {
948 let method_map = self.analysis.ty_cx.method_map.borrow();
949 let method_callee = method_map.get(&ty::MethodCall::expr(ex.id)).unwrap();
950 let (def_id, decl_id) = match method_callee.origin {
951 ty::MethodStatic(def_id) |
952 ty::MethodStaticClosure(def_id) => {
953 // method invoked on an object with a concrete type (not a static method)
955 match ty::trait_item_of_item(&self.analysis.ty_cx,
958 Some(decl_id) => Some(decl_id.def_id()),
961 // This incantation is required if the method referenced is a
962 // trait's default implementation.
963 let def_id = match ty::impl_or_trait_item(&self.analysis
966 ty::MethodTraitItem(method) => {
967 method.provided_source.unwrap_or(def_id)
971 "save::process_method_call: non-method \
972 DefId in MethodStatic or MethodStaticClosure"),
974 (Some(def_id), decl_id)
976 ty::MethodTypeParam(ref mp) => {
977 // method invoked on a type parameter
978 let trait_item = ty::trait_item(&self.analysis.ty_cx,
981 (None, Some(trait_item.def_id()))
983 ty::MethodTraitObject(ref mo) => {
984 // method invoked on a trait instance
985 let trait_item = ty::trait_item(&self.analysis.ty_cx,
988 (None, Some(trait_item.def_id()))
991 let sub_span = self.span.sub_span_for_meth_name(ex.span);
992 self.fmt.meth_call_str(ex.span,
998 // walk receiver and args
999 visit::walk_exprs(self, &args);
1002 fn process_pat(&mut self, p:&ast::Pat) {
1003 if generated_code(p.span) {
1008 ast::PatStruct(ref path, ref fields, _) => {
1009 visit::walk_path(self, path);
1011 let def = self.analysis.ty_cx.def_map.borrow().get(&p.id).unwrap().full_def();
1012 let struct_def = match def {
1013 def::DefConst(..) | def::DefAssociatedConst(..) => None,
1014 def::DefVariant(_, variant_id, _) => Some(variant_id),
1016 match ty::ty_to_def_id(ty::node_id_to_type(&self.analysis.ty_cx, p.id)) {
1018 self.sess.span_bug(p.span,
1019 &format!("Could not find struct_def for `{}`",
1020 self.span.snippet(p.span)));
1022 Some(def_id) => Some(def_id),
1027 if let Some(struct_def) = struct_def {
1028 let struct_fields = ty::lookup_struct_fields(&self.analysis.ty_cx, struct_def);
1029 for &Spanned { node: ref field, span } in fields {
1030 let sub_span = self.span.span_for_first_ident(span);
1031 for f in &struct_fields {
1032 if f.name == field.ident.name {
1033 self.fmt.ref_str(recorder::VarRef,
1041 self.visit_pat(&*field.pat);
1045 _ => visit::walk_pat(self, p)
1050 impl<'l, 'tcx, 'v> Visitor<'v> for DumpCsvVisitor<'l, 'tcx> {
1051 fn visit_item(&mut self, item: &ast::Item) {
1052 if generated_code(item.span) {
1057 ast::ItemUse(ref use_item) => {
1058 match use_item.node {
1059 ast::ViewPathSimple(ident, ref path) => {
1060 let sub_span = self.span.span_for_last_ident(path.span);
1061 let mod_id = match self.lookup_type_ref(item.id) {
1063 match self.lookup_def_kind(item.id, path.span) {
1064 Some(kind) => self.fmt.ref_str(kind,
1076 // 'use' always introduces an alias, if there is not an explicit
1077 // one, there is an implicit one.
1079 match self.span.sub_span_after_keyword(use_item.span, keywords::As) {
1080 Some(sub_span) => Some(sub_span),
1084 self.fmt.use_alias_str(path.span,
1090 self.write_sub_paths_truncated(path, true);
1092 ast::ViewPathGlob(ref path) => {
1093 // Make a comma-separated list of names of imported modules.
1094 let mut name_string = String::new();
1095 let glob_map = &self.analysis.glob_map;
1096 let glob_map = glob_map.as_ref().unwrap();
1097 if glob_map.contains_key(&item.id) {
1098 for n in glob_map.get(&item.id).unwrap() {
1099 if !name_string.is_empty() {
1100 name_string.push_str(", ");
1102 name_string.push_str(n.as_str());
1106 let sub_span = self.span.sub_span_of_token(path.span,
1107 token::BinOp(token::Star));
1108 self.fmt.use_glob_str(path.span,
1113 self.write_sub_paths(path, true);
1115 ast::ViewPathList(ref path, ref list) => {
1118 ast::PathListIdent { id, .. } => {
1119 match self.lookup_type_ref(id) {
1121 match self.lookup_def_kind(id, plid.span) {
1126 def_id, self.cur_scope);
1133 ast::PathListMod { .. } => ()
1137 self.write_sub_paths(path, true);
1141 ast::ItemExternCrate(ref s) => {
1142 let name = get_ident(item.ident);
1144 let location = match *s {
1145 Some(s) => s.to_string(),
1146 None => name.to_string(),
1148 let alias_span = self.span.span_for_last_ident(item.span);
1149 let cnum = match self.sess.cstore.find_extern_mod_stmt_cnum(item.id) {
1153 self.fmt.extern_crate_str(item.span,
1161 ast::ItemFn(ref decl, _, _, _, ref ty_params, ref body) =>
1162 self.process_fn(item, &**decl, ty_params, &**body),
1163 ast::ItemStatic(ref typ, _, ref expr) =>
1164 self.process_static_or_const_item(item, typ, expr),
1165 ast::ItemConst(ref typ, ref expr) =>
1166 self.process_static_or_const_item(item, &typ, &expr),
1167 ast::ItemStruct(ref def, ref ty_params) => self.process_struct(item, &**def, ty_params),
1168 ast::ItemEnum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
1173 ref impl_items) => {
1174 self.process_impl(item,
1180 ast::ItemTrait(_, ref generics, ref trait_refs, ref methods) =>
1181 self.process_trait(item, generics, trait_refs, methods),
1182 ast::ItemMod(ref m) => {
1183 self.process_mod(item);
1184 self.nest(item.id, |v| visit::walk_mod(v, m));
1186 ast::ItemTy(ref ty, ref ty_params) => {
1187 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
1188 let value = ty_to_string(&**ty);
1189 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Type);
1190 self.fmt.typedef_str(item.span,
1196 self.visit_ty(&**ty);
1197 self.process_generic_params(ty_params, item.span, &qualname, item.id);
1199 ast::ItemMac(_) => (),
1200 _ => visit::walk_item(self, item),
1204 fn visit_generics(&mut self, generics: &ast::Generics) {
1205 for param in &*generics.ty_params {
1206 for bound in &*param.bounds {
1207 if let ast::TraitTyParamBound(ref trait_ref, _) = *bound {
1208 self.process_trait_ref(&trait_ref.trait_ref);
1211 if let Some(ref ty) = param.default {
1212 self.visit_ty(&**ty);
1217 fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
1218 match trait_item.node {
1219 ast::ConstTraitItem(ref ty, Some(ref expr)) => {
1220 self.process_const(trait_item.id, &trait_item.ident,
1221 trait_item.span, &*ty, &*expr);
1223 ast::MethodTraitItem(ref sig, ref body) => {
1224 self.process_method(sig, body.as_ref().map(|x| &**x),
1225 trait_item.id, trait_item.ident.name, trait_item.span);
1227 ast::ConstTraitItem(_, None) |
1228 ast::TypeTraitItem(..) => {}
1232 fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
1233 match impl_item.node {
1234 ast::ConstImplItem(ref ty, ref expr) => {
1235 self.process_const(impl_item.id, &impl_item.ident,
1236 impl_item.span, &ty, &expr);
1238 ast::MethodImplItem(ref sig, ref body) => {
1239 self.process_method(sig, Some(body), impl_item.id,
1240 impl_item.ident.name, impl_item.span);
1242 ast::TypeImplItem(_) |
1243 ast::MacImplItem(_) => {}
1247 fn visit_ty(&mut self, t: &ast::Ty) {
1248 if generated_code(t.span) {
1253 ast::TyPath(_, ref path) => {
1254 match self.lookup_type_ref(t.id) {
1256 let sub_span = self.span.sub_span_for_type_name(t.span);
1257 self.fmt.ref_str(recorder::TypeRef,
1266 self.write_sub_paths_truncated(path, false);
1268 visit::walk_path(self, path);
1270 _ => visit::walk_ty(self, t),
1274 fn visit_expr(&mut self, ex: &ast::Expr) {
1275 if generated_code(ex.span) {
1280 ast::ExprCall(ref _f, ref _args) => {
1281 // Don't need to do anything for function calls,
1282 // because just walking the callee path does what we want.
1283 visit::walk_expr(self, ex);
1285 ast::ExprPath(_, ref path) => {
1286 self.process_path(ex.id, path.span, path, None);
1287 visit::walk_expr(self, ex);
1289 ast::ExprStruct(ref path, ref fields, ref base) =>
1290 self.process_struct_lit(ex, path, fields, base),
1291 ast::ExprMethodCall(_, _, ref args) => self.process_method_call(ex, args),
1292 ast::ExprField(ref sub_ex, _) => {
1293 if generated_code(sub_ex.span) {
1297 self.visit_expr(&sub_ex);
1299 let field_data = self.save_ctxt.get_expr_data(ex);
1300 if let super::Data::VariableRefData(field_data) = field_data {
1301 self.fmt.ref_str(recorder::VarRef,
1303 Some(field_data.span),
1307 self.sess.span_bug(ex.span, "expected VariableRefData");
1310 ast::ExprTupField(ref sub_ex, idx) => {
1311 if generated_code(sub_ex.span) {
1315 self.visit_expr(&**sub_ex);
1317 let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty;
1319 ty::ty_struct(def_id, _) => {
1320 let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, def_id);
1321 for (i, f) in fields.iter().enumerate() {
1323 let sub_span = self.span.sub_span_after_token(ex.span, token::Dot);
1324 self.fmt.ref_str(recorder::VarRef,
1334 _ => self.sess.span_bug(ex.span,
1335 &format!("Expected struct or tuple \
1336 type, found {:?}", ty)),
1339 ast::ExprClosure(_, ref decl, ref body) => {
1340 if generated_code(body.span) {
1344 let mut id = String::from_str("$");
1345 id.push_str(&ex.id.to_string());
1346 self.process_formals(&decl.inputs, &id);
1348 // walk arg and return types
1349 for arg in &decl.inputs {
1350 self.visit_ty(&*arg.ty);
1353 if let ast::Return(ref ret_ty) = decl.output {
1354 self.visit_ty(&**ret_ty);
1358 self.nest(ex.id, |v| v.visit_block(&**body));
1361 visit::walk_expr(self, ex)
1366 fn visit_mac(&mut self, _: &ast::Mac) {
1367 // Just stop, macros are poison to us.
1370 fn visit_pat(&mut self, p: &ast::Pat) {
1371 self.process_pat(p);
1374 fn visit_arm(&mut self, arm: &ast::Arm) {
1375 let mut collector = PathCollector::new();
1376 for pattern in &arm.pats {
1377 // collect paths from the arm's patterns
1378 collector.visit_pat(&pattern);
1379 self.visit_pat(&pattern);
1382 // This is to get around borrow checking, because we need mut self to call process_path.
1383 let mut paths_to_process = vec![];
1384 // process collected paths
1385 for &(id, ref p, immut, ref_kind) in &collector.collected_paths {
1386 let def_map = self.analysis.ty_cx.def_map.borrow();
1387 if !def_map.contains_key(&id) {
1388 self.sess.span_bug(p.span,
1389 &format!("def_map has no key for {} in visit_arm",
1392 let def = def_map.get(&id).unwrap().full_def();
1394 def::DefLocal(id) => {
1395 let value = if immut == ast::MutImmutable {
1396 self.span.snippet(p.span).to_string()
1398 "<mutable>".to_string()
1401 assert!(p.segments.len() == 1, "qualified path for local variable def in arm");
1402 self.fmt.variable_str(p.span,
1409 def::DefVariant(..) | def::DefTy(..) | def::DefStruct(..) => {
1410 paths_to_process.push((id, p.clone(), Some(ref_kind)))
1412 // FIXME(nrc) what are these doing here?
1413 def::DefStatic(_, _) |
1415 def::DefAssociatedConst(..) => {}
1416 _ => error!("unexpected definition kind when processing collected paths: {:?}",
1420 for &(id, ref path, ref_kind) in &paths_to_process {
1421 self.process_path(id, path.span, path, ref_kind);
1423 visit::walk_expr_opt(self, &arm.guard);
1424 self.visit_expr(&*arm.body);
1427 fn visit_stmt(&mut self, s: &ast::Stmt) {
1428 if generated_code(s.span) {
1432 visit::walk_stmt(self, s)
1435 fn visit_local(&mut self, l: &ast::Local) {
1436 if generated_code(l.span) {
1440 // The local could declare multiple new vars, we must walk the
1441 // pattern and collect them all.
1442 let mut collector = PathCollector::new();
1443 collector.visit_pat(&l.pat);
1444 self.visit_pat(&l.pat);
1446 let value = self.span.snippet(l.span);
1448 for &(id, ref p, immut, _) in &collector.collected_paths {
1449 let value = if immut == ast::MutImmutable {
1452 "<mutable>".to_string()
1454 let types = self.analysis.ty_cx.node_types();
1455 let typ = ppaux::ty_to_string(&self.analysis.ty_cx, *types.get(&id).unwrap());
1456 // Get the span only for the name of the variable (I hope the path
1457 // is only ever a variable name, but who knows?).
1458 let sub_span = self.span.span_for_last_ident(p.span);
1459 // Rust uses the id of the pattern for var lookups, so we'll use it too.
1460 self.fmt.variable_str(p.span,
1468 // Just walk the initialiser and type (don't want to walk the pattern again).
1469 visit::walk_ty_opt(self, &l.ty);
1470 visit::walk_expr_opt(self, &l.init);