]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/save/dump_csv.rs
7f66d3a833fde3e8fa4b31646902116ad3c690f7
[rustc.git] / src / librustc_trans / save / dump_csv.rs
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.
4 //
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.
10
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.
15 //!
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
22 //! idents.
23 //!
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.
29
30
31 use super::{escape, generated_code, recorder, SaveContext, PathCollector};
32
33 use session::Session;
34
35 use middle::def;
36 use middle::ty::{self, Ty};
37
38 use std::cell::Cell;
39 use std::fs::File;
40 use std::path::Path;
41
42 use syntax::ast_util;
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};
50 use syntax::ptr::P;
51
52 use super::span_utils::SpanUtils;
53 use super::recorder::{Recorder, FmtStrs};
54
55 use util::ppaux;
56
57
58 pub struct DumpCsvVisitor<'l, 'tcx: 'l> {
59 save_ctxt: SaveContext<'l, 'tcx>,
60 sess: &'l Session,
61 analysis: &'l ty::CrateAnalysis<'tcx>,
62
63 span: SpanUtils<'l>,
64 fmt: FmtStrs<'l>,
65
66 cur_scope: NodeId
67 }
68
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> {
73 DumpCsvVisitor {
74 sess: sess,
75 save_ctxt: SaveContext::new(sess, analysis, SpanUtils {
76 sess: sess,
77 err_count: Cell::new(0)
78 }),
79 analysis: analysis,
80 span: SpanUtils {
81 sess: sess,
82 err_count: Cell::new(0)
83 },
84 fmt: FmtStrs::new(box Recorder {
85 out: output_file,
86 dump_spans: false,
87 },
88 SpanUtils {
89 sess: sess,
90 err_count: Cell::new(0)
91 }),
92 cur_scope: 0
93 }
94 }
95
96 fn nest<F>(&mut self, scope_id: NodeId, f: F) where
97 F: FnOnce(&mut DumpCsvVisitor<'l, 'tcx>),
98 {
99 let parent_scope = self.cur_scope;
100 self.cur_scope = scope_id;
101 f(self);
102 self.cur_scope = parent_scope;
103 }
104
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);
108
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);
112 }
113 self.fmt.recorder.record("end_external_crates\n");
114 }
115
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);
121
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());
130 for s in &spans {
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);
134 }
135 return vec!();
136 }
137
138 let mut result: Vec<(Span, String)> = vec!();
139
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
144 global: path.global,
145 segments: segs};
146 let qualname = if i == 0 && path.global {
147 format!("::{}", path_to_string(&sub_path))
148 } else {
149 path_to_string(&sub_path)
150 };
151 result.push((*span, qualname));
152 segs = sub_path.segments;
153 }
154
155 result
156 }
157
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)
167 } else {
168 qualname.clone()
169 };
170 self.fmt.sub_mod_ref_str(path.span,
171 *span,
172 &qualname[..],
173 self.cur_scope);
174 }
175 }
176
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();
182 if len <= 1 {
183 return;
184 }
185
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)
190 } else {
191 qualname.clone()
192 };
193 self.fmt.sub_mod_ref_str(path.span,
194 *span,
195 &qualname[..],
196 self.cur_scope);
197 }
198 }
199
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();
205 if len <= 1 {
206 return;
207 }
208 let sub_paths = &sub_paths[.. (len-1)];
209
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,
213 *span,
214 &qualname[..]);
215
216 // write the other sub-paths
217 if len <= 2 {
218 return;
219 }
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,
223 *span,
224 &qualname[..],
225 self.cur_scope);
226 }
227 }
228
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",
233 ref_id));
234 }
235 let def = self.analysis.ty_cx.def_map.borrow().get(&ref_id).unwrap().full_def();
236 match def {
237 def::DefPrimTy(_) => None,
238 _ => Some(def.def_id()),
239 }
240 }
241
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",
246 ref_id));
247 }
248 let def = def_map.get(&ref_id).unwrap().full_def();
249 match def {
250 def::DefMod(_) |
251 def::DefForeignMod(_) => Some(recorder::ModRef),
252 def::DefStruct(_) => Some(recorder::StructRef),
253 def::DefTy(..) |
254 def::DefAssociatedTy(..) |
255 def::DefTrait(_) => Some(recorder::TypeRef),
256 def::DefStatic(_, _) |
257 def::DefConst(_) |
258 def::DefAssociatedConst(..) |
259 def::DefLocal(_) |
260 def::DefVariant(_, _, _) |
261 def::DefUpvar(..) => Some(recorder::VarRef),
262
263 def::DefFn(..) => Some(recorder::FnRef),
264
265 def::DefSelfTy(..) |
266 def::DefRegion(_) |
267 def::DefLabel(_) |
268 def::DefTyParam(..) |
269 def::DefUse(_) |
270 def::DefMethod(..) |
271 def::DefPrimTy(_) => {
272 self.sess.span_bug(span, &format!("lookup_def_kind for unexpected item: {:?}",
273 def));
274 },
275 }
276 }
277
278 fn process_formals(&mut self, formals: &Vec<ast::Arg>, qualname: &str) {
279 for arg in formals {
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 {
285 let typ =
286 ppaux::ty_to_string(
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),
293 id,
294 qualname,
295 &path_to_string(p),
296 &typ[..]);
297 }
298 }
299 }
300
301 fn process_method(&mut self, sig: &ast::MethodSig,
302 body: Option<&ast::Block>,
303 id: ast::NodeId, name: ast::Name,
304 span: Span) {
305 if generated_code(span) {
306 return;
307 }
308
309 debug!("process_method: {}:{}", id, token::get_name(name));
310
311 let mut scope_id;
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) {
317 NodeItem(item) => {
318 scope_id = item.id;
319 match item.node {
320 ast::ItemImpl(_, _, _, _, ref ty, _) => {
321 let mut result = String::from_str("<");
322 result.push_str(&ty_to_string(&**ty));
323
324 match ty::trait_of_item(&self.analysis.ty_cx,
325 ast_util::local_def(id)) {
326 Some(def_id) => {
327 result.push_str(" as ");
328 result.push_str(
329 &ty::item_path_str(&self.analysis.ty_cx, def_id));
330 },
331 None => {}
332 }
333 result.push_str(">");
334 result
335 }
336 _ => {
337 self.sess.span_bug(span,
338 &format!("Container {} for method {} not an impl?",
339 impl_id.node, id));
340 },
341 }
342 },
343 _ => {
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)));
347 },
348 },
349 None => match ty::trait_of_item(&self.analysis.ty_cx,
350 ast_util::local_def(id)) {
351 Some(def_id) => {
352 scope_id = def_id.node;
353 match self.analysis.ty_cx.map.get(def_id.node) {
354 NodeItem(_) => {
355 format!("::{}", ty::item_path_str(&self.analysis.ty_cx, def_id))
356 }
357 _ => {
358 self.sess.span_bug(span,
359 &format!("Could not find container {} for method {}",
360 def_id.node, id));
361 }
362 }
363 },
364 None => {
365 self.sess.span_bug(span,
366 &format!("Could not find container for method {}", id));
367 },
368 },
369 };
370
371 let qualname = &format!("{}::{}", qualname, &token::get_name(name));
372
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))
376 .and_then(|new_id| {
377 let def_id = new_id.def_id();
378 if def_id.node != 0 && def_id != ast_util::local_def(id) {
379 Some(def_id)
380 } else {
381 None
382 }
383 });
384
385 let sub_span = self.span.sub_span_after_keyword(span, keywords::Fn);
386 if body.is_some() {
387 self.fmt.method_str(span,
388 sub_span,
389 id,
390 qualname,
391 decl_id,
392 scope_id);
393 self.process_formals(&sig.decl.inputs, qualname);
394 } else {
395 self.fmt.method_decl_str(span,
396 sub_span,
397 id,
398 qualname,
399 scope_id);
400 }
401
402 // walk arg and return types
403 for arg in &sig.decl.inputs {
404 self.visit_ty(&arg.ty);
405 }
406
407 if let ast::Return(ref ret_ty) = sig.decl.output {
408 self.visit_ty(ret_ty);
409 }
410
411 // walk the fn body
412 if let Some(body) = body {
413 self.nest(id, |v| v.visit_block(body));
414 }
415
416 self.process_generic_params(&sig.generics,
417 span,
418 qualname,
419 id);
420 }
421
422 fn process_trait_ref(&mut self,
423 trait_ref: &ast::TraitRef) {
424 match self.lookup_type_ref(trait_ref.ref_id) {
425 Some(id) => {
426 let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span);
427 self.fmt.ref_str(recorder::TypeRef,
428 trait_ref.path.span,
429 sub_span,
430 id,
431 self.cur_scope);
432 visit::walk_path(self, &trait_ref.path);
433 },
434 None => ()
435 }
436 }
437
438 fn process_struct_field_def(&mut self,
439 field: &ast::StructField,
440 qualname: &str,
441 scope_id: NodeId) {
442 match field.node.kind {
443 ast::NamedField(ident, _) => {
444 let name = get_ident(ident);
445 let qualname = format!("{}::{}", qualname, name);
446 let typ =
447 ppaux::ty_to_string(
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,
452 Some(sub_span),
453 field.node.id,
454 &name[..],
455 &qualname[..],
456 &typ[..],
457 scope_id),
458 None => self.sess.span_bug(field.span,
459 &format!("Could not find sub-span for field {}",
460 qualname)),
461 }
462 },
463 _ => (),
464 }
465 }
466
467 // Dump generic params bindings, then visit_generics
468 fn process_generic_params(&mut self,
469 generics:&ast::Generics,
470 full_span: Span,
471 prefix: &str,
472 id: NodeId) {
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!("{}::{}${}",
482 prefix,
483 escape(self.span.snippet(*param_ss)),
484 id);
485 self.fmt.typedef_str(full_span,
486 Some(*param_ss),
487 param.id,
488 &name[..],
489 "");
490 }
491 self.visit_generics(generics);
492 }
493
494 fn process_fn(&mut self,
495 item: &ast::Item,
496 decl: &ast::FnDecl,
497 ty_params: &ast::Generics,
498 body: &ast::Block) {
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,
502 Some(fn_data.span),
503 fn_data.id,
504 &fn_data.qualname,
505 fn_data.scope);
506
507
508 self.process_formals(&decl.inputs, &fn_data.qualname);
509 self.process_generic_params(ty_params, item.span, &fn_data.qualname, item.id);
510 } else {
511 unreachable!();
512 }
513
514 for arg in &decl.inputs {
515 self.visit_ty(&arg.ty);
516 }
517
518 if let ast::Return(ref ret_ty) = decl.output {
519 self.visit_ty(&ret_ty);
520 }
521
522 self.nest(item.id, |v| v.visit_block(&body));
523 }
524
525 fn process_static_or_const_item(&mut self,
526 item: &ast::Item,
527 typ: &ast::Ty,
528 expr: &ast::Expr)
529 {
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,
533 Some(var_data.span),
534 var_data.id,
535 &var_data.name,
536 &var_data.qualname,
537 &var_data.value,
538 &var_data.type_value,
539 var_data.scope);
540 } else {
541 unreachable!();
542 }
543
544 self.visit_ty(&typ);
545 self.visit_expr(expr);
546 }
547
548 fn process_const(&mut self,
549 id: ast::NodeId,
550 ident: &ast::Ident,
551 span: Span,
552 typ: &ast::Ty,
553 expr: &ast::Expr)
554 {
555 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(id));
556
557 let sub_span = self.span.sub_span_after_keyword(span,
558 keywords::Const);
559
560 self.fmt.static_str(span,
561 sub_span,
562 id,
563 &get_ident((*ident).clone()),
564 &qualname[..],
565 &self.span.snippet(expr.span),
566 &ty_to_string(&*typ),
567 self.cur_scope);
568
569 // walk type and init value
570 self.visit_ty(typ);
571 self.visit_expr(expr);
572 }
573
574 fn process_struct(&mut self,
575 item: &ast::Item,
576 def: &ast::StructDef,
577 ty_params: &ast::Generics) {
578 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
579
580 let ctor_id = match def.ctor_id {
581 Some(node_id) => node_id,
582 None => -1,
583 };
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,
587 sub_span,
588 item.id,
589 ctor_id,
590 &qualname[..],
591 self.cur_scope,
592 &val[..]);
593
594 // fields
595 for field in &def.fields {
596 self.process_struct_field_def(field, &qualname[..], item.id);
597 self.visit_ty(&*field.node.ty);
598 }
599
600 self.process_generic_params(ty_params, item.span, &qualname[..], item.id);
601 }
602
603 fn process_enum(&mut self,
604 item: &ast::Item,
605 enum_definition: &ast::EnumDef,
606 ty_params: &ast::Generics) {
607 let enum_name = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
608 let val = self.span.snippet(item.span);
609 match self.span.sub_span_after_keyword(item.span, keywords::Enum) {
610 Some(sub_span) => self.fmt.enum_str(item.span,
611 Some(sub_span),
612 item.id,
613 &enum_name[..],
614 self.cur_scope,
615 &val[..]),
616 None => self.sess.span_bug(item.span,
617 &format!("Could not find subspan for enum {}",
618 enum_name)),
619 }
620 for variant in &enum_definition.variants {
621 let name = get_ident(variant.node.name);
622 let name = &name;
623 let mut qualname = enum_name.clone();
624 qualname.push_str("::");
625 qualname.push_str(name);
626 let val = self.span.snippet(variant.span);
627 match variant.node.kind {
628 ast::TupleVariantKind(ref args) => {
629 // first ident in span is the variant's name
630 self.fmt.tuple_variant_str(variant.span,
631 self.span.span_for_first_ident(variant.span),
632 variant.node.id,
633 name,
634 &qualname[..],
635 &enum_name[..],
636 &val[..],
637 item.id);
638 for arg in args {
639 self.visit_ty(&*arg.ty);
640 }
641 }
642 ast::StructVariantKind(ref struct_def) => {
643 let ctor_id = match struct_def.ctor_id {
644 Some(node_id) => node_id,
645 None => -1,
646 };
647 self.fmt.struct_variant_str(
648 variant.span,
649 self.span.span_for_first_ident(variant.span),
650 variant.node.id,
651 ctor_id,
652 &qualname[..],
653 &enum_name[..],
654 &val[..],
655 item.id);
656
657 for field in &struct_def.fields {
658 self.process_struct_field_def(field, &qualname, variant.node.id);
659 self.visit_ty(&*field.node.ty);
660 }
661 }
662 }
663 }
664
665 self.process_generic_params(ty_params, item.span, &enum_name[..], item.id);
666 }
667
668 fn process_impl(&mut self,
669 item: &ast::Item,
670 type_parameters: &ast::Generics,
671 trait_ref: &Option<ast::TraitRef>,
672 typ: &ast::Ty,
673 impl_items: &[P<ast::ImplItem>]) {
674 let trait_id = trait_ref.as_ref().and_then(|tr| self.lookup_type_ref(tr.ref_id));
675 match typ.node {
676 // Common case impl for a struct or something basic.
677 ast::TyPath(None, ref path) => {
678 let sub_span = self.span.sub_span_for_type_name(path.span);
679 let self_id = self.lookup_type_ref(typ.id).map(|id| {
680 self.fmt.ref_str(recorder::TypeRef,
681 path.span,
682 sub_span,
683 id,
684 self.cur_scope);
685 id
686 });
687 self.fmt.impl_str(path.span,
688 sub_span,
689 item.id,
690 self_id,
691 trait_id,
692 self.cur_scope);
693 },
694 _ => {
695 // Less useful case, impl for a compound type.
696 self.visit_ty(&*typ);
697
698 let sub_span = self.span.sub_span_for_type_name(typ.span);
699 self.fmt.impl_str(typ.span,
700 sub_span,
701 item.id,
702 None,
703 trait_id,
704 self.cur_scope);
705 }
706 }
707
708 match *trait_ref {
709 Some(ref trait_ref) => self.process_trait_ref(trait_ref),
710 None => (),
711 }
712
713 self.process_generic_params(type_parameters, item.span, "", item.id);
714 for impl_item in impl_items {
715 self.visit_impl_item(impl_item);
716 }
717 }
718
719 fn process_trait(&mut self,
720 item: &ast::Item,
721 generics: &ast::Generics,
722 trait_refs: &OwnedSlice<ast::TyParamBound>,
723 methods: &[P<ast::TraitItem>]) {
724 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
725 let val = self.span.snippet(item.span);
726 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Trait);
727 self.fmt.trait_str(item.span,
728 sub_span,
729 item.id,
730 &qualname[..],
731 self.cur_scope,
732 &val[..]);
733
734 // super-traits
735 for super_bound in &**trait_refs {
736 let trait_ref = match *super_bound {
737 ast::TraitTyParamBound(ref trait_ref, _) => {
738 trait_ref
739 }
740 ast::RegionTyParamBound(..) => {
741 continue;
742 }
743 };
744
745 let trait_ref = &trait_ref.trait_ref;
746 match self.lookup_type_ref(trait_ref.ref_id) {
747 Some(id) => {
748 let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span);
749 self.fmt.ref_str(recorder::TypeRef,
750 trait_ref.path.span,
751 sub_span,
752 id,
753 self.cur_scope);
754 self.fmt.inherit_str(trait_ref.path.span,
755 sub_span,
756 id,
757 item.id);
758 },
759 None => ()
760 }
761 }
762
763 // walk generics and methods
764 self.process_generic_params(generics, item.span, &qualname[..], item.id);
765 for method in methods {
766 self.visit_trait_item(method)
767 }
768 }
769
770 fn process_mod(&mut self,
771 item: &ast::Item, // The module in question, represented as an item.
772 m: &ast::Mod) {
773 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
774
775 let cm = self.sess.codemap();
776 let filename = cm.span_to_filename(m.inner);
777
778 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Mod);
779 self.fmt.mod_str(item.span,
780 sub_span,
781 item.id,
782 &qualname[..],
783 self.cur_scope,
784 &filename[..]);
785
786 self.nest(item.id, |v| visit::walk_mod(v, m));
787 }
788
789 fn process_path(&mut self,
790 id: NodeId,
791 span: Span,
792 path: &ast::Path,
793 ref_kind: Option<recorder::Row>) {
794 if generated_code(span) {
795 return
796 }
797
798 let def_map = self.analysis.ty_cx.def_map.borrow();
799 if !def_map.contains_key(&id) {
800 self.sess.span_bug(span,
801 &format!("def_map has no key for {} in visit_expr", id));
802 }
803 let def = def_map.get(&id).unwrap().full_def();
804 let sub_span = self.span.span_for_last_ident(span);
805 match def {
806 def::DefUpvar(..) |
807 def::DefLocal(..) |
808 def::DefStatic(..) |
809 def::DefConst(..) |
810 def::DefAssociatedConst(..) |
811 def::DefVariant(..) => self.fmt.ref_str(ref_kind.unwrap_or(recorder::VarRef),
812 span,
813 sub_span,
814 def.def_id(),
815 self.cur_scope),
816 def::DefStruct(def_id) => self.fmt.ref_str(recorder::StructRef,
817 span,
818 sub_span,
819 def_id,
820 self.cur_scope),
821 def::DefTy(def_id, _) => self.fmt.ref_str(recorder::TypeRef,
822 span,
823 sub_span,
824 def_id,
825 self.cur_scope),
826 def::DefMethod(declid, provenence) => {
827 let sub_span = self.span.sub_span_for_meth_name(span);
828 let defid = if declid.krate == ast::LOCAL_CRATE {
829 let ti = ty::impl_or_trait_item(&self.analysis.ty_cx,
830 declid);
831 match provenence {
832 def::FromTrait(def_id) => {
833 Some(ty::trait_items(&self.analysis.ty_cx,
834 def_id)
835 .iter()
836 .find(|mr| {
837 mr.name() == ti.name()
838 })
839 .unwrap()
840 .def_id())
841 }
842 def::FromImpl(def_id) => {
843 let impl_items = self.analysis
844 .ty_cx
845 .impl_items
846 .borrow();
847 Some(impl_items.get(&def_id)
848 .unwrap()
849 .iter()
850 .find(|mr| {
851 ty::impl_or_trait_item(
852 &self.analysis.ty_cx,
853 mr.def_id()
854 ).name() == ti.name()
855 })
856 .unwrap()
857 .def_id())
858 }
859 }
860 } else {
861 None
862 };
863 self.fmt.meth_call_str(span,
864 sub_span,
865 defid,
866 Some(declid),
867 self.cur_scope);
868 },
869 def::DefFn(def_id, _) => {
870 self.fmt.fn_call_str(span,
871 sub_span,
872 def_id,
873 self.cur_scope)
874 }
875 _ => self.sess.span_bug(span,
876 &format!("Unexpected def kind while looking \
877 up path in `{}`: `{:?}`",
878 self.span.snippet(span),
879 def)),
880 }
881 // modules or types in the path prefix
882 match def {
883 def::DefMethod(did, _) => {
884 let ti = ty::impl_or_trait_item(&self.analysis.ty_cx, did);
885 if let ty::MethodTraitItem(m) = ti {
886 if m.explicit_self == ty::StaticExplicitSelfCategory {
887 self.write_sub_path_trait_truncated(path);
888 }
889 }
890 }
891 def::DefLocal(_) |
892 def::DefStatic(_,_) |
893 def::DefConst(..) |
894 def::DefAssociatedConst(..) |
895 def::DefStruct(_) |
896 def::DefVariant(..) |
897 def::DefFn(..) => self.write_sub_paths_truncated(path, false),
898 _ => {},
899 }
900 }
901
902 fn process_struct_lit(&mut self,
903 ex: &ast::Expr,
904 path: &ast::Path,
905 fields: &Vec<ast::Field>,
906 base: &Option<P<ast::Expr>>) {
907 if generated_code(path.span) {
908 return
909 }
910
911 self.write_sub_paths_truncated(path, false);
912
913 let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, ex).sty;
914 let struct_def = match *ty {
915 ty::ty_struct(def_id, _) => {
916 let sub_span = self.span.span_for_last_ident(path.span);
917 self.fmt.ref_str(recorder::StructRef,
918 path.span,
919 sub_span,
920 def_id,
921 self.cur_scope);
922 Some(def_id)
923 }
924 _ => None
925 };
926
927 for field in fields {
928 match struct_def {
929 Some(struct_def) => {
930 let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, struct_def);
931 for f in &fields {
932 if generated_code(field.ident.span) {
933 continue;
934 }
935 if f.name == field.ident.node.name {
936 // We don't really need a sub-span here, but no harm done
937 let sub_span = self.span.span_for_last_ident(field.ident.span);
938 self.fmt.ref_str(recorder::VarRef,
939 field.ident.span,
940 sub_span,
941 f.id,
942 self.cur_scope);
943 }
944 }
945 }
946 None => {}
947 }
948
949 self.visit_expr(&*field.expr)
950 }
951 visit::walk_expr_opt(self, base)
952 }
953
954 fn process_method_call(&mut self,
955 ex: &ast::Expr,
956 args: &Vec<P<ast::Expr>>) {
957 let method_map = self.analysis.ty_cx.method_map.borrow();
958 let method_callee = method_map.get(&ty::MethodCall::expr(ex.id)).unwrap();
959 let (def_id, decl_id) = match method_callee.origin {
960 ty::MethodStatic(def_id) |
961 ty::MethodStaticClosure(def_id) => {
962 // method invoked on an object with a concrete type (not a static method)
963 let decl_id =
964 match ty::trait_item_of_item(&self.analysis.ty_cx,
965 def_id) {
966 None => None,
967 Some(decl_id) => Some(decl_id.def_id()),
968 };
969
970 // This incantation is required if the method referenced is a
971 // trait's default implementation.
972 let def_id = match ty::impl_or_trait_item(&self.analysis
973 .ty_cx,
974 def_id) {
975 ty::MethodTraitItem(method) => {
976 method.provided_source.unwrap_or(def_id)
977 }
978 _ => self.sess
979 .span_bug(ex.span,
980 "save::process_method_call: non-method \
981 DefId in MethodStatic or MethodStaticClosure"),
982 };
983 (Some(def_id), decl_id)
984 }
985 ty::MethodTypeParam(ref mp) => {
986 // method invoked on a type parameter
987 let trait_item = ty::trait_item(&self.analysis.ty_cx,
988 mp.trait_ref.def_id,
989 mp.method_num);
990 (None, Some(trait_item.def_id()))
991 }
992 ty::MethodTraitObject(ref mo) => {
993 // method invoked on a trait instance
994 let trait_item = ty::trait_item(&self.analysis.ty_cx,
995 mo.trait_ref.def_id,
996 mo.method_num);
997 (None, Some(trait_item.def_id()))
998 }
999 };
1000 let sub_span = self.span.sub_span_for_meth_name(ex.span);
1001 self.fmt.meth_call_str(ex.span,
1002 sub_span,
1003 def_id,
1004 decl_id,
1005 self.cur_scope);
1006
1007 // walk receiver and args
1008 visit::walk_exprs(self, &args[..]);
1009 }
1010
1011 fn process_pat(&mut self, p:&ast::Pat) {
1012 if generated_code(p.span) {
1013 return
1014 }
1015
1016 match p.node {
1017 ast::PatStruct(ref path, ref fields, _) => {
1018 visit::walk_path(self, path);
1019
1020 let def = self.analysis.ty_cx.def_map.borrow().get(&p.id).unwrap().full_def();
1021 let struct_def = match def {
1022 def::DefConst(..) | def::DefAssociatedConst(..) => None,
1023 def::DefVariant(_, variant_id, _) => Some(variant_id),
1024 _ => {
1025 match ty::ty_to_def_id(ty::node_id_to_type(&self.analysis.ty_cx, p.id)) {
1026 None => {
1027 self.sess.span_bug(p.span,
1028 &format!("Could not find struct_def for `{}`",
1029 self.span.snippet(p.span)));
1030 }
1031 Some(def_id) => Some(def_id),
1032 }
1033 }
1034 };
1035
1036 if let Some(struct_def) = struct_def {
1037 let struct_fields = ty::lookup_struct_fields(&self.analysis.ty_cx, struct_def);
1038 for &Spanned { node: ref field, span } in fields {
1039 let sub_span = self.span.span_for_first_ident(span);
1040 for f in &struct_fields {
1041 if f.name == field.ident.name {
1042 self.fmt.ref_str(recorder::VarRef,
1043 span,
1044 sub_span,
1045 f.id,
1046 self.cur_scope);
1047 break;
1048 }
1049 }
1050 self.visit_pat(&*field.pat);
1051 }
1052 }
1053 }
1054 _ => visit::walk_pat(self, p)
1055 }
1056 }
1057 }
1058
1059 impl<'l, 'tcx, 'v> Visitor<'v> for DumpCsvVisitor<'l, 'tcx> {
1060 fn visit_item(&mut self, item: &ast::Item) {
1061 if generated_code(item.span) {
1062 return
1063 }
1064
1065 match item.node {
1066 ast::ItemUse(ref use_item) => {
1067 match use_item.node {
1068 ast::ViewPathSimple(ident, ref path) => {
1069 let sub_span = self.span.span_for_last_ident(path.span);
1070 let mod_id = match self.lookup_type_ref(item.id) {
1071 Some(def_id) => {
1072 match self.lookup_def_kind(item.id, path.span) {
1073 Some(kind) => self.fmt.ref_str(kind,
1074 path.span,
1075 sub_span,
1076 def_id,
1077 self.cur_scope),
1078 None => {},
1079 }
1080 Some(def_id)
1081 },
1082 None => None,
1083 };
1084
1085 // 'use' always introduces an alias, if there is not an explicit
1086 // one, there is an implicit one.
1087 let sub_span =
1088 match self.span.sub_span_after_keyword(use_item.span, keywords::As) {
1089 Some(sub_span) => Some(sub_span),
1090 None => sub_span,
1091 };
1092
1093 self.fmt.use_alias_str(path.span,
1094 sub_span,
1095 item.id,
1096 mod_id,
1097 &get_ident(ident),
1098 self.cur_scope);
1099 self.write_sub_paths_truncated(path, true);
1100 }
1101 ast::ViewPathGlob(ref path) => {
1102 // Make a comma-separated list of names of imported modules.
1103 let mut name_string = String::new();
1104 let glob_map = &self.analysis.glob_map;
1105 let glob_map = glob_map.as_ref().unwrap();
1106 if glob_map.contains_key(&item.id) {
1107 for n in glob_map.get(&item.id).unwrap() {
1108 if !name_string.is_empty() {
1109 name_string.push_str(", ");
1110 }
1111 name_string.push_str(n.as_str());
1112 }
1113 }
1114
1115 let sub_span = self.span.sub_span_of_token(path.span,
1116 token::BinOp(token::Star));
1117 self.fmt.use_glob_str(path.span,
1118 sub_span,
1119 item.id,
1120 &name_string,
1121 self.cur_scope);
1122 self.write_sub_paths(path, true);
1123 }
1124 ast::ViewPathList(ref path, ref list) => {
1125 for plid in list {
1126 match plid.node {
1127 ast::PathListIdent { id, .. } => {
1128 match self.lookup_type_ref(id) {
1129 Some(def_id) =>
1130 match self.lookup_def_kind(id, plid.span) {
1131 Some(kind) => {
1132 self.fmt.ref_str(
1133 kind, plid.span,
1134 Some(plid.span),
1135 def_id, self.cur_scope);
1136 }
1137 None => ()
1138 },
1139 None => ()
1140 }
1141 },
1142 ast::PathListMod { .. } => ()
1143 }
1144 }
1145
1146 self.write_sub_paths(path, true);
1147 }
1148 }
1149 }
1150 ast::ItemExternCrate(ref s) => {
1151 let name = get_ident(item.ident);
1152 let name = &name;
1153 let location = match *s {
1154 Some(s) => s.to_string(),
1155 None => name.to_string(),
1156 };
1157 let alias_span = self.span.span_for_last_ident(item.span);
1158 let cnum = match self.sess.cstore.find_extern_mod_stmt_cnum(item.id) {
1159 Some(cnum) => cnum,
1160 None => 0,
1161 };
1162 self.fmt.extern_crate_str(item.span,
1163 alias_span,
1164 item.id,
1165 cnum,
1166 name,
1167 &location[..],
1168 self.cur_scope);
1169 }
1170 ast::ItemFn(ref decl, _, _, ref ty_params, ref body) =>
1171 self.process_fn(item, &**decl, ty_params, &**body),
1172 ast::ItemStatic(ref typ, _, ref expr) =>
1173 self.process_static_or_const_item(item, typ, expr),
1174 ast::ItemConst(ref typ, ref expr) =>
1175 self.process_static_or_const_item(item, &typ, &expr),
1176 ast::ItemStruct(ref def, ref ty_params) => self.process_struct(item, &**def, ty_params),
1177 ast::ItemEnum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
1178 ast::ItemImpl(_, _,
1179 ref ty_params,
1180 ref trait_ref,
1181 ref typ,
1182 ref impl_items) => {
1183 self.process_impl(item,
1184 ty_params,
1185 trait_ref,
1186 &**typ,
1187 impl_items)
1188 }
1189 ast::ItemTrait(_, ref generics, ref trait_refs, ref methods) =>
1190 self.process_trait(item, generics, trait_refs, methods),
1191 ast::ItemMod(ref m) => self.process_mod(item, m),
1192 ast::ItemTy(ref ty, ref ty_params) => {
1193 let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
1194 let value = ty_to_string(&**ty);
1195 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Type);
1196 self.fmt.typedef_str(item.span,
1197 sub_span,
1198 item.id,
1199 &qualname[..],
1200 &value[..]);
1201
1202 self.visit_ty(&**ty);
1203 self.process_generic_params(ty_params, item.span, &qualname, item.id);
1204 },
1205 ast::ItemMac(_) => (),
1206 _ => visit::walk_item(self, item),
1207 }
1208 }
1209
1210 fn visit_generics(&mut self, generics: &ast::Generics) {
1211 for param in &*generics.ty_params {
1212 for bound in &*param.bounds {
1213 if let ast::TraitTyParamBound(ref trait_ref, _) = *bound {
1214 self.process_trait_ref(&trait_ref.trait_ref);
1215 }
1216 }
1217 if let Some(ref ty) = param.default {
1218 self.visit_ty(&**ty);
1219 }
1220 }
1221 }
1222
1223 fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
1224 match trait_item.node {
1225 ast::ConstTraitItem(ref ty, Some(ref expr)) => {
1226 self.process_const(trait_item.id, &trait_item.ident,
1227 trait_item.span, &*ty, &*expr);
1228 }
1229 ast::MethodTraitItem(ref sig, ref body) => {
1230 self.process_method(sig, body.as_ref().map(|x| &**x),
1231 trait_item.id, trait_item.ident.name, trait_item.span);
1232 }
1233 ast::ConstTraitItem(_, None) |
1234 ast::TypeTraitItem(..) => {}
1235 }
1236 }
1237
1238 fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
1239 match impl_item.node {
1240 ast::ConstImplItem(ref ty, ref expr) => {
1241 self.process_const(impl_item.id, &impl_item.ident,
1242 impl_item.span, &ty, &expr);
1243 }
1244 ast::MethodImplItem(ref sig, ref body) => {
1245 self.process_method(sig, Some(body), impl_item.id,
1246 impl_item.ident.name, impl_item.span);
1247 }
1248 ast::TypeImplItem(_) |
1249 ast::MacImplItem(_) => {}
1250 }
1251 }
1252
1253 fn visit_ty(&mut self, t: &ast::Ty) {
1254 if generated_code(t.span) {
1255 return
1256 }
1257
1258 match t.node {
1259 ast::TyPath(_, ref path) => {
1260 match self.lookup_type_ref(t.id) {
1261 Some(id) => {
1262 let sub_span = self.span.sub_span_for_type_name(t.span);
1263 self.fmt.ref_str(recorder::TypeRef,
1264 t.span,
1265 sub_span,
1266 id,
1267 self.cur_scope);
1268 },
1269 None => ()
1270 }
1271
1272 self.write_sub_paths_truncated(path, false);
1273
1274 visit::walk_path(self, path);
1275 },
1276 _ => visit::walk_ty(self, t),
1277 }
1278 }
1279
1280 fn visit_expr(&mut self, ex: &ast::Expr) {
1281 if generated_code(ex.span) {
1282 return
1283 }
1284
1285 match ex.node {
1286 ast::ExprCall(ref _f, ref _args) => {
1287 // Don't need to do anything for function calls,
1288 // because just walking the callee path does what we want.
1289 visit::walk_expr(self, ex);
1290 }
1291 ast::ExprPath(_, ref path) => {
1292 self.process_path(ex.id, path.span, path, None);
1293 visit::walk_expr(self, ex);
1294 }
1295 ast::ExprStruct(ref path, ref fields, ref base) =>
1296 self.process_struct_lit(ex, path, fields, base),
1297 ast::ExprMethodCall(_, _, ref args) => self.process_method_call(ex, args),
1298 ast::ExprField(ref sub_ex, ident) => {
1299 if generated_code(sub_ex.span) {
1300 return
1301 }
1302
1303 self.visit_expr(&**sub_ex);
1304 let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty;
1305 match *ty {
1306 ty::ty_struct(def_id, _) => {
1307 let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, def_id);
1308 for f in &fields {
1309 if f.name == ident.node.name {
1310 let sub_span = self.span.span_for_last_ident(ex.span);
1311 self.fmt.ref_str(recorder::VarRef,
1312 ex.span,
1313 sub_span,
1314 f.id,
1315 self.cur_scope);
1316 break;
1317 }
1318 }
1319 }
1320 _ => self.sess.span_bug(ex.span,
1321 &format!("Expected struct type, found {:?}", ty)),
1322 }
1323 },
1324 ast::ExprTupField(ref sub_ex, idx) => {
1325 if generated_code(sub_ex.span) {
1326 return
1327 }
1328
1329 self.visit_expr(&**sub_ex);
1330
1331 let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty;
1332 match *ty {
1333 ty::ty_struct(def_id, _) => {
1334 let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, def_id);
1335 for (i, f) in fields.iter().enumerate() {
1336 if i == idx.node {
1337 let sub_span = self.span.sub_span_after_token(ex.span, token::Dot);
1338 self.fmt.ref_str(recorder::VarRef,
1339 ex.span,
1340 sub_span,
1341 f.id,
1342 self.cur_scope);
1343 break;
1344 }
1345 }
1346 }
1347 ty::ty_tup(_) => {}
1348 _ => self.sess.span_bug(ex.span,
1349 &format!("Expected struct or tuple \
1350 type, found {:?}", ty)),
1351 }
1352 },
1353 ast::ExprClosure(_, ref decl, ref body) => {
1354 if generated_code(body.span) {
1355 return
1356 }
1357
1358 let mut id = String::from_str("$");
1359 id.push_str(&ex.id.to_string());
1360 self.process_formals(&decl.inputs, &id[..]);
1361
1362 // walk arg and return types
1363 for arg in &decl.inputs {
1364 self.visit_ty(&*arg.ty);
1365 }
1366
1367 if let ast::Return(ref ret_ty) = decl.output {
1368 self.visit_ty(&**ret_ty);
1369 }
1370
1371 // walk the body
1372 self.nest(ex.id, |v| v.visit_block(&**body));
1373 },
1374 _ => {
1375 visit::walk_expr(self, ex)
1376 }
1377 }
1378 }
1379
1380 fn visit_mac(&mut self, _: &ast::Mac) {
1381 // Just stop, macros are poison to us.
1382 }
1383
1384 fn visit_pat(&mut self, p: &ast::Pat) {
1385 self.process_pat(p);
1386 }
1387
1388 fn visit_arm(&mut self, arm: &ast::Arm) {
1389 let mut collector = PathCollector::new();
1390 for pattern in &arm.pats {
1391 // collect paths from the arm's patterns
1392 collector.visit_pat(&pattern);
1393 self.visit_pat(&pattern);
1394 }
1395
1396 // This is to get around borrow checking, because we need mut self to call process_path.
1397 let mut paths_to_process = vec![];
1398 // process collected paths
1399 for &(id, ref p, immut, ref_kind) in &collector.collected_paths {
1400 let def_map = self.analysis.ty_cx.def_map.borrow();
1401 if !def_map.contains_key(&id) {
1402 self.sess.span_bug(p.span,
1403 &format!("def_map has no key for {} in visit_arm",
1404 id));
1405 }
1406 let def = def_map.get(&id).unwrap().full_def();
1407 match def {
1408 def::DefLocal(id) => {
1409 let value = if immut == ast::MutImmutable {
1410 self.span.snippet(p.span).to_string()
1411 } else {
1412 "<mutable>".to_string()
1413 };
1414
1415 assert!(p.segments.len() == 1, "qualified path for local variable def in arm");
1416 self.fmt.variable_str(p.span,
1417 Some(p.span),
1418 id,
1419 &path_to_string(p),
1420 &value[..],
1421 "")
1422 }
1423 def::DefVariant(..) | def::DefTy(..) | def::DefStruct(..) => {
1424 paths_to_process.push((id, p.clone(), Some(ref_kind)))
1425 }
1426 // FIXME(nrc) what are these doing here?
1427 def::DefStatic(_, _) |
1428 def::DefConst(..) |
1429 def::DefAssociatedConst(..) => {}
1430 _ => error!("unexpected definition kind when processing collected paths: {:?}",
1431 def)
1432 }
1433 }
1434 for &(id, ref path, ref_kind) in &paths_to_process {
1435 self.process_path(id, path.span, path, ref_kind);
1436 }
1437 visit::walk_expr_opt(self, &arm.guard);
1438 self.visit_expr(&*arm.body);
1439 }
1440
1441 fn visit_stmt(&mut self, s: &ast::Stmt) {
1442 if generated_code(s.span) {
1443 return
1444 }
1445
1446 visit::walk_stmt(self, s)
1447 }
1448
1449 fn visit_local(&mut self, l: &ast::Local) {
1450 if generated_code(l.span) {
1451 return
1452 }
1453
1454 // The local could declare multiple new vars, we must walk the
1455 // pattern and collect them all.
1456 let mut collector = PathCollector::new();
1457 collector.visit_pat(&l.pat);
1458 self.visit_pat(&l.pat);
1459
1460 let value = self.span.snippet(l.span);
1461
1462 for &(id, ref p, immut, _) in &collector.collected_paths {
1463 let value = if immut == ast::MutImmutable {
1464 value.to_string()
1465 } else {
1466 "<mutable>".to_string()
1467 };
1468 let types = self.analysis.ty_cx.node_types();
1469 let typ = ppaux::ty_to_string(&self.analysis.ty_cx, *types.get(&id).unwrap());
1470 // Get the span only for the name of the variable (I hope the path
1471 // is only ever a variable name, but who knows?).
1472 let sub_span = self.span.span_for_last_ident(p.span);
1473 // Rust uses the id of the pattern for var lookups, so we'll use it too.
1474 self.fmt.variable_str(p.span,
1475 sub_span,
1476 id,
1477 &path_to_string(p),
1478 &value[..],
1479 &typ[..]);
1480 }
1481
1482 // Just walk the initialiser and type (don't want to walk the pattern again).
1483 visit::walk_ty_opt(self, &l.ty);
1484 visit::walk_expr_opt(self, &l.init);
1485 }
1486 }