]> git.proxmox.com Git - rustc.git/blame - src/librustc_save_analysis/dump_visitor.rs
New upstream version 1.14.0+dfsg1
[rustc.git] / src / librustc_save_analysis / dump_visitor.rs
CommitLineData
d9579d0f
AL
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
54a0048b 11//! Write the output of rustc's analysis to an implementor of Dump. The data is
d9579d0f
AL
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).
54a0048b
SL
26//! DumpVisitor walks the AST and processes it, and an implementor of Dump
27//! is used for recording the output in a format-agnostic way (see CsvDumper
28//! for an example).
d9579d0f 29
9e0c209e 30use rustc::hir;
54a0048b 31use rustc::hir::def::Def;
9e0c209e
SL
32use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
33use rustc::hir::map::{Node, NodeItem};
54a0048b 34use rustc::session::Session;
3157f602 35use rustc::ty::{self, TyCtxt, ImplOrTraitItem, ImplOrTraitItemContainer};
d9579d0f 36
7453a54e 37use std::collections::HashSet;
9e0c209e 38use std::collections::hash_map::DefaultHasher;
54a0048b 39use std::hash::*;
d9579d0f 40
9e0c209e 41use syntax::ast::{self, NodeId, PatKind, Attribute, CRATE_NODE_ID};
c1a9b12d 42use syntax::parse::token::{self, keywords};
d9579d0f 43use syntax::visit::{self, Visitor};
a7813a04 44use syntax::print::pprust::{path_to_string, ty_to_string, bounds_to_string, generics_to_string};
d9579d0f 45use syntax::ptr::P;
3157f602
XL
46use syntax::codemap::Spanned;
47use syntax_pos::*;
d9579d0f 48
9e0c209e 49use super::{escape, generated_code, SaveContext, PathCollector, docs_for_attrs};
54a0048b
SL
50use super::data::*;
51use super::dump::Dump;
9e0c209e 52use super::external_data::{Lower, make_def_id};
d9579d0f 53use super::span_utils::SpanUtils;
54a0048b 54use super::recorder;
d9579d0f 55
62682a34 56macro_rules! down_cast_data {
54a0048b 57 ($id:ident, $kind:ident, $sp:expr) => {
62682a34
SL
58 let $id = if let super::Data::$kind(data) = $id {
59 data
60 } else {
54a0048b 61 span_bug!($sp, "unexpected data kind: {:?}", $id);
5bcae85e 62 };
62682a34
SL
63 };
64}
d9579d0f 65
a7813a04 66pub struct DumpVisitor<'l, 'tcx: 'l, 'll, D: 'll> {
d9579d0f
AL
67 save_ctxt: SaveContext<'l, 'tcx>,
68 sess: &'l Session,
a7813a04 69 tcx: TyCtxt<'l, 'tcx, 'tcx>,
b039eaaf 70 analysis: &'l ty::CrateAnalysis<'l>,
a7813a04 71 dumper: &'ll mut D,
d9579d0f
AL
72
73 span: SpanUtils<'l>,
d9579d0f 74
e9174d1e 75 cur_scope: NodeId,
7453a54e
SL
76
77 // Set of macro definition (callee) spans, and the set
78 // of macro use (callsite) spans. We store these to ensure
79 // we only write one macro def per unique macro definition, and
80 // one macro use per unique callsite span.
81 mac_defs: HashSet<Span>,
82 mac_uses: HashSet<Span>,
d9579d0f
AL
83}
84
a7813a04
XL
85impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
86 pub fn new(tcx: TyCtxt<'l, 'tcx, 'tcx>,
87 save_ctxt: SaveContext<'l, 'tcx>,
b039eaaf 88 analysis: &'l ty::CrateAnalysis<'l>,
a7813a04
XL
89 dumper: &'ll mut D)
90 -> DumpVisitor<'l, 'tcx, 'll, D> {
c1a9b12d 91 let span_utils = SpanUtils::new(&tcx.sess);
54a0048b 92 DumpVisitor {
62682a34
SL
93 sess: &tcx.sess,
94 tcx: tcx,
a7813a04 95 save_ctxt: save_ctxt,
d9579d0f 96 analysis: analysis,
54a0048b 97 dumper: dumper,
62682a34 98 span: span_utils.clone(),
9e0c209e 99 cur_scope: CRATE_NODE_ID,
7453a54e
SL
100 mac_defs: HashSet::new(),
101 mac_uses: HashSet::new(),
d9579d0f
AL
102 }
103 }
104
e9174d1e 105 fn nest<F>(&mut self, scope_id: NodeId, f: F)
a7813a04 106 where F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, D>)
d9579d0f
AL
107 {
108 let parent_scope = self.cur_scope;
109 self.cur_scope = scope_id;
110 f(self);
111 self.cur_scope = parent_scope;
112 }
113
114 pub fn dump_crate_info(&mut self, name: &str, krate: &ast::Crate) {
92a42be0 115 let source_file = self.tcx.sess.local_crate_source_file.as_ref();
54a0048b
SL
116 let crate_root = source_file.map(|source_file| {
117 match source_file.file_name() {
92a42be0
SL
118 Some(_) => source_file.parent().unwrap().display().to_string(),
119 None => source_file.display().to_string(),
54a0048b
SL
120 }
121 });
122
123 // Info about all the external crates referenced from this crate.
124 let external_crates = self.save_ctxt.get_external_crates().into_iter().map(|c| {
a7813a04 125 let lo_loc = self.span.sess.codemap().lookup_char_pos(c.span.lo);
54a0048b
SL
126 ExternalCrateData {
127 name: c.name,
9e0c209e 128 num: CrateNum::from_u32(c.number),
a7813a04 129 file_name: SpanUtils::make_path_string(&lo_loc.file.name),
54a0048b
SL
130 }
131 }).collect();
92a42be0 132
d9579d0f 133 // The current crate.
54a0048b
SL
134 let data = CratePreludeData {
135 crate_name: name.into(),
a7813a04
XL
136 crate_root: crate_root.unwrap_or("<no source>".to_owned()),
137 external_crates: external_crates,
138 span: krate.span,
54a0048b 139 };
d9579d0f 140
a7813a04 141 self.dumper.crate_prelude(data.lower(self.tcx));
d9579d0f
AL
142 }
143
144 // Return all non-empty prefixes of a path.
145 // For each prefix, we return the span for the last segment in the prefix and
146 // a str representation of the entire prefix.
147 fn process_path_prefixes(&self, path: &ast::Path) -> Vec<(Span, String)> {
148 let spans = self.span.spans_for_path_segments(path);
149
150 // Paths to enums seem to not match their spans - the span includes all the
151 // variants too. But they seem to always be at the end, so I hope we can cope with
152 // always using the first ones. So, only error out if we don't have enough spans.
153 // What could go wrong...?
154 if spans.len() < path.segments.len() {
7453a54e 155 if generated_code(path.span) {
c30ab7b3 156 return vec![];
7453a54e 157 }
b039eaaf
SL
158 error!("Mis-calculated spans for path '{}'. Found {} spans, expected {}. Found spans:",
159 path_to_string(path),
160 spans.len(),
161 path.segments.len());
d9579d0f
AL
162 for s in &spans {
163 let loc = self.sess.codemap().lookup_char_pos(s.lo);
164 error!(" '{}' in {}, line {}",
b039eaaf
SL
165 self.span.snippet(*s),
166 loc.file.name,
167 loc.line);
d9579d0f 168 }
c30ab7b3
SL
169 error!(" master span: {:?}: `{}`", path.span, self.span.snippet(path.span));
170 return vec![];
d9579d0f
AL
171 }
172
c30ab7b3 173 let mut result: Vec<(Span, String)> = vec![];
d9579d0f 174
c30ab7b3 175 let mut segs = vec![];
62682a34 176 for (i, (seg, span)) in path.segments.iter().zip(&spans).enumerate() {
d9579d0f 177 segs.push(seg.clone());
e9174d1e
SL
178 let sub_path = ast::Path {
179 span: *span, // span for the last segment
180 global: path.global,
181 segments: segs,
182 };
d9579d0f
AL
183 let qualname = if i == 0 && path.global {
184 format!("::{}", path_to_string(&sub_path))
185 } else {
186 path_to_string(&sub_path)
187 };
188 result.push((*span, qualname));
189 segs = sub_path.segments;
190 }
191
192 result
193 }
194
195 // The global arg allows us to override the global-ness of the path (which
196 // actually means 'does the path start with `::`', rather than 'is the path
197 // semantically global). We use the override for `use` imports (etc.) where
198 // the syntax is non-global, but the semantics are global.
199 fn write_sub_paths(&mut self, path: &ast::Path, global: bool) {
200 let sub_paths = self.process_path_prefixes(path);
201 for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
202 let qualname = if i == 0 && global && !path.global {
203 format!("::{}", qualname)
204 } else {
205 qualname.clone()
206 };
a7813a04 207 self.dumper.mod_ref(ModRefData {
54a0048b
SL
208 span: *span,
209 qualname: qualname,
210 scope: self.cur_scope,
211 ref_id: None
a7813a04 212 }.lower(self.tcx));
d9579d0f
AL
213 }
214 }
215
216 // As write_sub_paths, but does not process the last ident in the path (assuming it
217 // will be processed elsewhere). See note on write_sub_paths about global.
218 fn write_sub_paths_truncated(&mut self, path: &ast::Path, global: bool) {
219 let sub_paths = self.process_path_prefixes(path);
220 let len = sub_paths.len();
221 if len <= 1 {
222 return;
223 }
224
225 let sub_paths = &sub_paths[..len-1];
226 for (i, &(ref span, ref qualname)) in sub_paths.iter().enumerate() {
227 let qualname = if i == 0 && global && !path.global {
228 format!("::{}", qualname)
229 } else {
230 qualname.clone()
231 };
a7813a04 232 self.dumper.mod_ref(ModRefData {
54a0048b
SL
233 span: *span,
234 qualname: qualname,
235 scope: self.cur_scope,
236 ref_id: None
a7813a04 237 }.lower(self.tcx));
d9579d0f
AL
238 }
239 }
240
241 // As write_sub_paths, but expects a path of the form module_path::trait::method
242 // Where trait could actually be a struct too.
243 fn write_sub_path_trait_truncated(&mut self, path: &ast::Path) {
244 let sub_paths = self.process_path_prefixes(path);
245 let len = sub_paths.len();
246 if len <= 1 {
247 return;
248 }
249 let sub_paths = &sub_paths[.. (len-1)];
250
251 // write the trait part of the sub-path
252 let (ref span, ref qualname) = sub_paths[len-2];
a7813a04 253 self.dumper.type_ref(TypeRefData {
54a0048b
SL
254 ref_id: None,
255 span: *span,
256 qualname: qualname.to_owned(),
9e0c209e 257 scope: CRATE_NODE_ID
a7813a04 258 }.lower(self.tcx));
d9579d0f
AL
259
260 // write the other sub-paths
261 if len <= 2 {
262 return;
263 }
264 let sub_paths = &sub_paths[..len-2];
265 for &(ref span, ref qualname) in sub_paths {
a7813a04 266 self.dumper.mod_ref(ModRefData {
54a0048b
SL
267 span: *span,
268 qualname: qualname.to_owned(),
269 scope: self.cur_scope,
270 ref_id: None
a7813a04 271 }.lower(self.tcx));
d9579d0f
AL
272 }
273 }
274
c30ab7b3 275 fn lookup_def_id(&self, ref_id: NodeId) -> Option<DefId> {
9e0c209e
SL
276 self.tcx.expect_def_or_none(ref_id).and_then(|def| {
277 match def {
c30ab7b3 278 Def::PrimTy(..) | Def::SelfTy(..) => None,
9e0c209e
SL
279 def => Some(def.def_id()),
280 }
281 })
d9579d0f
AL
282 }
283
54a0048b
SL
284 fn process_def_kind(&mut self,
285 ref_id: NodeId,
286 span: Span,
287 sub_span: Option<Span>,
288 def_id: DefId,
289 scope: NodeId) {
290 if self.span.filter_generated(sub_span, span) {
291 return;
292 }
293
3157f602 294 let def = self.tcx.expect_def(ref_id);
d9579d0f 295 match def {
9e0c209e 296 Def::Mod(_) => {
a7813a04 297 self.dumper.mod_ref(ModRefData {
54a0048b
SL
298 span: sub_span.expect("No span found for mod ref"),
299 ref_id: Some(def_id),
300 scope: scope,
301 qualname: String::new()
a7813a04 302 }.lower(self.tcx));
54a0048b
SL
303 }
304 Def::Struct(..) |
c30ab7b3 305 Def::Variant(..) |
9e0c209e 306 Def::Union(..) |
7453a54e
SL
307 Def::Enum(..) |
308 Def::TyAlias(..) |
54a0048b 309 Def::Trait(_) => {
a7813a04 310 self.dumper.type_ref(TypeRefData {
54a0048b
SL
311 span: sub_span.expect("No span found for type ref"),
312 ref_id: Some(def_id),
313 scope: scope,
314 qualname: String::new()
a7813a04 315 }.lower(self.tcx));
54a0048b 316 }
9e0c209e 317 Def::Static(..) |
c30ab7b3
SL
318 Def::Const(..) |
319 Def::StructCtor(..) |
320 Def::VariantCtor(..) => {
a7813a04 321 self.dumper.variable_ref(VariableRefData {
54a0048b
SL
322 span: sub_span.expect("No span found for var ref"),
323 ref_id: def_id,
324 scope: scope,
325 name: String::new()
a7813a04 326 }.lower(self.tcx));
54a0048b
SL
327 }
328 Def::Fn(..) => {
a7813a04 329 self.dumper.function_ref(FunctionRefData {
54a0048b
SL
330 span: sub_span.expect("No span found for fn ref"),
331 ref_id: def_id,
332 scope: scope
a7813a04 333 }.lower(self.tcx));
54a0048b 334 }
c30ab7b3
SL
335 Def::Local(..) |
336 Def::Upvar(..) |
7453a54e
SL
337 Def::SelfTy(..) |
338 Def::Label(_) |
339 Def::TyParam(..) |
340 Def::Method(..) |
c30ab7b3
SL
341 Def::AssociatedTy(..) |
342 Def::AssociatedConst(..) |
7453a54e
SL
343 Def::PrimTy(_) |
344 Def::Err => {
54a0048b
SL
345 span_bug!(span,
346 "process_def_kind for unexpected item: {:?}",
347 def);
e9174d1e 348 }
d9579d0f
AL
349 }
350 }
351
352 fn process_formals(&mut self, formals: &Vec<ast::Arg>, qualname: &str) {
353 for arg in formals {
354 self.visit_pat(&arg.pat);
355 let mut collector = PathCollector::new();
356 collector.visit_pat(&arg.pat);
357 let span_utils = self.span.clone();
9e0c209e 358 for &(id, ref p, ..) in &collector.collected_paths {
c30ab7b3 359 let typ = self.tcx.tables().node_types.get(&id).unwrap().to_string();
d9579d0f
AL
360 // get the span only for the name of the variable (I hope the path is only ever a
361 // variable name, but who knows?)
54a0048b
SL
362 let sub_span = span_utils.span_for_last_ident(p.span);
363 if !self.span.filter_generated(sub_span, p.span) {
a7813a04 364 self.dumper.variable(VariableData {
54a0048b 365 id: id,
3157f602 366 kind: VariableKind::Local,
54a0048b
SL
367 span: sub_span.expect("No span found for variable"),
368 name: path_to_string(p),
369 qualname: format!("{}::{}", qualname, path_to_string(p)),
370 type_value: typ,
371 value: String::new(),
9e0c209e
SL
372 scope: CRATE_NODE_ID,
373 parent: None,
374 visibility: Visibility::Inherited,
375 docs: String::new(),
a7813a04 376 }.lower(self.tcx));
54a0048b 377 }
d9579d0f
AL
378 }
379 }
380 }
381
c1a9b12d
SL
382 fn process_method(&mut self,
383 sig: &ast::MethodSig,
d9579d0f 384 body: Option<&ast::Block>,
c1a9b12d
SL
385 id: ast::NodeId,
386 name: ast::Name,
9e0c209e
SL
387 vis: Visibility,
388 attrs: &[Attribute],
d9579d0f 389 span: Span) {
c1a9b12d 390 debug!("process_method: {}:{}", id, name);
d9579d0f 391
7453a54e 392 if let Some(method_data) = self.save_ctxt.get_method_data(id, name, span) {
d9579d0f 393
a7813a04 394 let sig_str = ::make_signature(&sig.decl, &sig.generics);
7453a54e 395 if body.is_some() {
7453a54e 396 self.process_formals(&sig.decl.inputs, &method_data.qualname);
7453a54e 397 }
3157f602
XL
398
399 // If the method is defined in an impl, then try and find the corresponding
400 // method decl in a trait, and if there is one, make a decl_id for it. This
401 // requires looking up the impl, then the trait, then searching for a method
402 // with the right name.
403 if !self.span.filter_generated(Some(method_data.span), span) {
404 let container =
405 self.tcx.impl_or_trait_item(self.tcx.map.local_def_id(id)).container();
9e0c209e
SL
406 let mut trait_id;
407 let mut decl_id = None;
408 match container {
409 ImplOrTraitItemContainer::ImplContainer(id) => {
410 trait_id = self.tcx.trait_id_of_impl(id);
411
412 match trait_id {
413 Some(id) => {
414 for item in &**self.tcx.trait_items(id) {
415 if let &ImplOrTraitItem::MethodTraitItem(ref m) = item {
416 if m.name == name {
417 decl_id = Some(m.def_id);
418 break;
419 }
420 }
421 }
422 }
423 None => {
424 if let Some(NodeItem(item)) = self.tcx.map.get_if_local(id) {
425 if let hir::ItemImpl(_, _, _, _, ref ty, _) = item.node {
c30ab7b3 426 trait_id = self.lookup_def_id(ty.id);
9e0c209e 427 }
3157f602
XL
428 }
429 }
430 }
9e0c209e
SL
431 }
432 ImplOrTraitItemContainer::TraitContainer(id) => {
433 trait_id = Some(id);
434 }
435 }
3157f602
XL
436
437 self.dumper.method(MethodData {
438 id: method_data.id,
439 name: method_data.name,
440 span: method_data.span,
441 scope: method_data.scope,
442 qualname: method_data.qualname.clone(),
443 value: sig_str,
444 decl_id: decl_id,
9e0c209e
SL
445 parent: trait_id,
446 visibility: vis,
447 docs: docs_for_attrs(attrs),
3157f602
XL
448 }.lower(self.tcx));
449 }
450
7453a54e 451 self.process_generic_params(&sig.generics, span, &method_data.qualname, id);
d9579d0f
AL
452 }
453
454 // walk arg and return types
455 for arg in &sig.decl.inputs {
456 self.visit_ty(&arg.ty);
457 }
458
7453a54e 459 if let ast::FunctionRetTy::Ty(ref ret_ty) = sig.decl.output {
d9579d0f
AL
460 self.visit_ty(ret_ty);
461 }
462
463 // walk the fn body
464 if let Some(body) = body {
465 self.nest(id, |v| v.visit_block(body));
466 }
d9579d0f
AL
467 }
468
62682a34
SL
469 fn process_trait_ref(&mut self, trait_ref: &ast::TraitRef) {
470 let trait_ref_data = self.save_ctxt.get_trait_ref_data(trait_ref, self.cur_scope);
471 if let Some(trait_ref_data) = trait_ref_data {
54a0048b 472 if !self.span.filter_generated(Some(trait_ref_data.span), trait_ref.path.span) {
a7813a04 473 self.dumper.type_ref(trait_ref_data.lower(self.tcx));
54a0048b
SL
474 }
475
62682a34 476 visit::walk_path(self, &trait_ref.path);
d9579d0f
AL
477 }
478 }
479
e9174d1e 480 fn process_struct_field_def(&mut self, field: &ast::StructField, parent_id: NodeId) {
62682a34 481 let field_data = self.save_ctxt.get_field_data(field, parent_id);
54a0048b
SL
482 if let Some(mut field_data) = field_data {
483 if !self.span.filter_generated(Some(field_data.span), field.span) {
54a0048b 484 field_data.value = String::new();
a7813a04 485 self.dumper.variable(field_data.lower(self.tcx));
54a0048b 486 }
d9579d0f
AL
487 }
488 }
489
490 // Dump generic params bindings, then visit_generics
491 fn process_generic_params(&mut self,
e9174d1e 492 generics: &ast::Generics,
d9579d0f
AL
493 full_span: Span,
494 prefix: &str,
495 id: NodeId) {
496 // We can't only use visit_generics since we don't have spans for param
497 // bindings, so we reparse the full_span to get those sub spans.
498 // However full span is the entire enum/fn/struct block, so we only want
499 // the first few to match the number of generics we're looking for.
500 let param_sub_spans = self.span.spans_for_ty_params(full_span,
b039eaaf 501 (generics.ty_params.len() as isize));
62682a34 502 for (param, param_ss) in generics.ty_params.iter().zip(param_sub_spans) {
a7813a04 503 let name = escape(self.span.snippet(param_ss));
d9579d0f 504 // Append $id to name to make sure each one is unique
a7813a04
XL
505 let qualname = format!("{}::{}${}",
506 prefix,
507 name,
508 id);
54a0048b 509 if !self.span.filter_generated(Some(param_ss), full_span) {
a7813a04 510 self.dumper.typedef(TypeDefData {
54a0048b 511 span: param_ss,
a7813a04 512 name: name,
54a0048b 513 id: param.id,
a7813a04 514 qualname: qualname,
9e0c209e
SL
515 value: String::new(),
516 visibility: Visibility::Inherited,
517 parent: None,
518 docs: String::new(),
a7813a04 519 }.lower(self.tcx));
54a0048b 520 }
d9579d0f
AL
521 }
522 self.visit_generics(generics);
523 }
524
525 fn process_fn(&mut self,
526 item: &ast::Item,
527 decl: &ast::FnDecl,
528 ty_params: &ast::Generics,
529 body: &ast::Block) {
7453a54e 530 if let Some(fn_data) = self.save_ctxt.get_item_data(item) {
54a0048b
SL
531 down_cast_data!(fn_data, FunctionData, item.span);
532 if !self.span.filter_generated(Some(fn_data.span), item.span) {
a7813a04 533 self.dumper.function(fn_data.clone().lower(self.tcx));
54a0048b 534 }
7453a54e
SL
535
536 self.process_formals(&decl.inputs, &fn_data.qualname);
537 self.process_generic_params(ty_params, item.span, &fn_data.qualname, item.id);
538 }
d9579d0f
AL
539
540 for arg in &decl.inputs {
541 self.visit_ty(&arg.ty);
542 }
543
7453a54e 544 if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output {
d9579d0f
AL
545 self.visit_ty(&ret_ty);
546 }
547
548 self.nest(item.id, |v| v.visit_block(&body));
549 }
550
e9174d1e 551 fn process_static_or_const_item(&mut self, item: &ast::Item, typ: &ast::Ty, expr: &ast::Expr) {
7453a54e 552 if let Some(var_data) = self.save_ctxt.get_item_data(item) {
54a0048b
SL
553 down_cast_data!(var_data, VariableData, item.span);
554 if !self.span.filter_generated(Some(var_data.span), item.span) {
a7813a04 555 self.dumper.variable(var_data.lower(self.tcx));
54a0048b 556 }
7453a54e 557 }
d9579d0f
AL
558 self.visit_ty(&typ);
559 self.visit_expr(expr);
560 }
561
9e0c209e
SL
562 fn process_assoc_const(&mut self,
563 id: ast::NodeId,
564 name: ast::Name,
565 span: Span,
566 typ: &ast::Ty,
567 expr: &ast::Expr,
568 parent_id: DefId,
569 vis: Visibility,
570 attrs: &[Attribute]) {
54a0048b 571 let qualname = format!("::{}", self.tcx.node_path_str(id));
d9579d0f 572
b039eaaf 573 let sub_span = self.span.sub_span_after_keyword(span, keywords::Const);
d9579d0f 574
54a0048b 575 if !self.span.filter_generated(sub_span, span) {
a7813a04 576 self.dumper.variable(VariableData {
54a0048b 577 span: sub_span.expect("No span found for variable"),
3157f602 578 kind: VariableKind::Const,
54a0048b
SL
579 id: id,
580 name: name.to_string(),
581 qualname: qualname,
582 value: self.span.snippet(expr.span),
583 type_value: ty_to_string(&typ),
9e0c209e
SL
584 scope: self.cur_scope,
585 parent: Some(parent_id),
586 visibility: vis,
587 docs: docs_for_attrs(attrs),
a7813a04 588 }.lower(self.tcx));
54a0048b 589 }
d9579d0f
AL
590
591 // walk type and init value
592 self.visit_ty(typ);
593 self.visit_expr(expr);
594 }
595
a7813a04 596 // FIXME tuple structs should generate tuple-specific data.
d9579d0f
AL
597 fn process_struct(&mut self,
598 item: &ast::Item,
b039eaaf 599 def: &ast::VariantData,
d9579d0f 600 ty_params: &ast::Generics) {
a7813a04 601 let name = item.ident.to_string();
54a0048b 602 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
d9579d0f 603
d9579d0f 604 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Struct);
3157f602
XL
605 let (val, fields) =
606 if let ast::ItemKind::Struct(ast::VariantData::Struct(ref fields, _), _) = item.node
607 {
a7813a04
XL
608 let fields_str = fields.iter()
609 .enumerate()
610 .map(|(i, f)| f.ident.map(|i| i.to_string())
611 .unwrap_or(i.to_string()))
612 .collect::<Vec<_>>()
613 .join(", ");
3157f602 614 (format!("{} {{ {} }}", name, fields_str), fields.iter().map(|f| f.id).collect())
a7813a04 615 } else {
3157f602 616 (String::new(), vec![])
a7813a04
XL
617 };
618
54a0048b 619 if !self.span.filter_generated(sub_span, item.span) {
a7813a04 620 self.dumper.struct_data(StructData {
54a0048b
SL
621 span: sub_span.expect("No span found for struct"),
622 id: item.id,
a7813a04 623 name: name,
54a0048b
SL
624 ctor_id: def.id(),
625 qualname: qualname.clone(),
626 scope: self.cur_scope,
3157f602
XL
627 value: val,
628 fields: fields,
9e0c209e
SL
629 visibility: From::from(&item.vis),
630 docs: docs_for_attrs(&item.attrs),
a7813a04 631 }.lower(self.tcx));
54a0048b
SL
632 }
633
d9579d0f
AL
634
635 // fields
b039eaaf 636 for field in def.fields() {
62682a34 637 self.process_struct_field_def(field, item.id);
54a0048b 638 self.visit_ty(&field.ty);
d9579d0f
AL
639 }
640
62682a34 641 self.process_generic_params(ty_params, item.span, &qualname, item.id);
d9579d0f
AL
642 }
643
644 fn process_enum(&mut self,
645 item: &ast::Item,
646 enum_definition: &ast::EnumDef,
647 ty_params: &ast::Generics) {
62682a34 648 let enum_data = self.save_ctxt.get_item_data(item);
7453a54e
SL
649 let enum_data = match enum_data {
650 None => return,
651 Some(data) => data,
652 };
54a0048b 653 down_cast_data!(enum_data, EnumData, item.span);
a7813a04
XL
654 if !self.span.filter_generated(Some(enum_data.span), item.span) {
655 self.dumper.enum_data(enum_data.clone().lower(self.tcx));
54a0048b 656 }
62682a34 657
d9579d0f 658 for variant in &enum_definition.variants {
a7813a04 659 let name = variant.node.name.name.to_string();
62682a34 660 let mut qualname = enum_data.qualname.clone();
d9579d0f 661 qualname.push_str("::");
a7813a04 662 qualname.push_str(&name);
b039eaaf 663
7453a54e 664 match variant.node.data {
a7813a04 665 ast::VariantData::Struct(ref fields, _) => {
54a0048b 666 let sub_span = self.span.span_for_first_ident(variant.span);
a7813a04
XL
667 let fields_str = fields.iter()
668 .enumerate()
669 .map(|(i, f)| f.ident.map(|i| i.to_string())
670 .unwrap_or(i.to_string()))
671 .collect::<Vec<_>>()
672 .join(", ");
673 let val = format!("{}::{} {{ {} }}", enum_data.name, name, fields_str);
54a0048b 674 if !self.span.filter_generated(sub_span, variant.span) {
a7813a04 675 self.dumper.struct_variant(StructVariantData {
54a0048b
SL
676 span: sub_span.expect("No span found for struct variant"),
677 id: variant.node.data.id(),
a7813a04 678 name: name,
54a0048b
SL
679 qualname: qualname,
680 type_value: enum_data.qualname.clone(),
681 value: val,
9e0c209e
SL
682 scope: enum_data.scope,
683 parent: Some(make_def_id(item.id, &self.tcx.map)),
684 docs: docs_for_attrs(&variant.node.attrs),
a7813a04 685 }.lower(self.tcx));
54a0048b 686 }
7453a54e 687 }
a7813a04 688 ref v => {
54a0048b 689 let sub_span = self.span.span_for_first_ident(variant.span);
a7813a04
XL
690 let mut val = format!("{}::{}", enum_data.name, name);
691 if let &ast::VariantData::Tuple(ref fields, _) = v {
692 val.push('(');
693 val.push_str(&fields.iter()
694 .map(|f| ty_to_string(&f.ty))
695 .collect::<Vec<_>>()
696 .join(", "));
697 val.push(')');
698 }
54a0048b 699 if !self.span.filter_generated(sub_span, variant.span) {
a7813a04 700 self.dumper.tuple_variant(TupleVariantData {
54a0048b
SL
701 span: sub_span.expect("No span found for tuple variant"),
702 id: variant.node.data.id(),
a7813a04 703 name: name,
54a0048b
SL
704 qualname: qualname,
705 type_value: enum_data.qualname.clone(),
706 value: val,
9e0c209e
SL
707 scope: enum_data.scope,
708 parent: Some(make_def_id(item.id, &self.tcx.map)),
709 docs: docs_for_attrs(&variant.node.attrs),
a7813a04 710 }.lower(self.tcx));
54a0048b 711 }
7453a54e
SL
712 }
713 }
714
b039eaaf
SL
715
716 for field in variant.node.data.fields() {
717 self.process_struct_field_def(field, variant.node.data.id());
54a0048b 718 self.visit_ty(&field.ty);
d9579d0f
AL
719 }
720 }
62682a34 721 self.process_generic_params(ty_params, item.span, &enum_data.qualname, enum_data.id);
d9579d0f
AL
722 }
723
724 fn process_impl(&mut self,
725 item: &ast::Item,
726 type_parameters: &ast::Generics,
727 trait_ref: &Option<ast::TraitRef>,
728 typ: &ast::Ty,
7453a54e
SL
729 impl_items: &[ast::ImplItem]) {
730 let mut has_self_ref = false;
731 if let Some(impl_data) = self.save_ctxt.get_item_data(item) {
54a0048b 732 down_cast_data!(impl_data, ImplData, item.span);
7453a54e
SL
733 if let Some(ref self_ref) = impl_data.self_ref {
734 has_self_ref = true;
54a0048b 735 if !self.span.filter_generated(Some(self_ref.span), item.span) {
a7813a04 736 self.dumper.type_ref(self_ref.clone().lower(self.tcx));
54a0048b 737 }
62682a34 738 }
7453a54e 739 if let Some(ref trait_ref_data) = impl_data.trait_ref {
54a0048b 740 if !self.span.filter_generated(Some(trait_ref_data.span), item.span) {
a7813a04 741 self.dumper.type_ref(trait_ref_data.clone().lower(self.tcx));
54a0048b
SL
742 }
743
7453a54e 744 visit::walk_path(self, &trait_ref.as_ref().unwrap().path);
d9579d0f 745 }
7453a54e 746
54a0048b 747 if !self.span.filter_generated(Some(impl_data.span), item.span) {
a7813a04 748 self.dumper.impl_data(ImplData {
54a0048b
SL
749 id: impl_data.id,
750 span: impl_data.span,
751 scope: impl_data.scope,
752 trait_ref: impl_data.trait_ref.map(|d| d.ref_id.unwrap()),
753 self_ref: impl_data.self_ref.map(|d| d.ref_id.unwrap())
a7813a04 754 }.lower(self.tcx));
54a0048b 755 }
d9579d0f 756 }
7453a54e
SL
757 if !has_self_ref {
758 self.visit_ty(&typ);
d9579d0f 759 }
d9579d0f
AL
760 self.process_generic_params(type_parameters, item.span, "", item.id);
761 for impl_item in impl_items {
9e0c209e
SL
762 let map = &self.tcx.map;
763 self.process_impl_item(impl_item, make_def_id(item.id, map));
d9579d0f
AL
764 }
765 }
766
767 fn process_trait(&mut self,
768 item: &ast::Item,
769 generics: &ast::Generics,
9cc50fc6 770 trait_refs: &ast::TyParamBounds,
7453a54e 771 methods: &[ast::TraitItem]) {
a7813a04 772 let name = item.ident.to_string();
54a0048b 773 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
a7813a04
XL
774 let mut val = name.clone();
775 if !generics.lifetimes.is_empty() || !generics.ty_params.is_empty() {
776 val.push_str(&generics_to_string(generics));
777 }
778 if !trait_refs.is_empty() {
779 val.push_str(": ");
780 val.push_str(&bounds_to_string(trait_refs));
781 }
d9579d0f 782 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Trait);
54a0048b 783 if !self.span.filter_generated(sub_span, item.span) {
a7813a04 784 self.dumper.trait_data(TraitData {
54a0048b
SL
785 span: sub_span.expect("No span found for trait"),
786 id: item.id,
a7813a04 787 name: name,
54a0048b
SL
788 qualname: qualname.clone(),
789 scope: self.cur_scope,
3157f602
XL
790 value: val,
791 items: methods.iter().map(|i| i.id).collect(),
9e0c209e
SL
792 visibility: From::from(&item.vis),
793 docs: docs_for_attrs(&item.attrs),
a7813a04 794 }.lower(self.tcx));
54a0048b 795 }
d9579d0f
AL
796
797 // super-traits
62682a34 798 for super_bound in trait_refs.iter() {
d9579d0f
AL
799 let trait_ref = match *super_bound {
800 ast::TraitTyParamBound(ref trait_ref, _) => {
801 trait_ref
802 }
803 ast::RegionTyParamBound(..) => {
804 continue;
805 }
806 };
807
808 let trait_ref = &trait_ref.trait_ref;
c30ab7b3 809 if let Some(id) = self.lookup_def_id(trait_ref.ref_id) {
54a0048b
SL
810 let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span);
811 if !self.span.filter_generated(sub_span, trait_ref.path.span) {
a7813a04 812 self.dumper.type_ref(TypeRefData {
54a0048b
SL
813 span: sub_span.expect("No span found for trait ref"),
814 ref_id: Some(id),
815 scope: self.cur_scope,
816 qualname: String::new()
a7813a04 817 }.lower(self.tcx));
54a0048b
SL
818 }
819
820 if !self.span.filter_generated(sub_span, trait_ref.path.span) {
821 let sub_span = sub_span.expect("No span for inheritance");
822 self.dumper.inheritance(InheritanceData {
823 span: sub_span,
824 base_id: id,
825 deriv_id: item.id
a7813a04 826 }.lower(self.tcx));
e9174d1e 827 }
d9579d0f
AL
828 }
829 }
830
831 // walk generics and methods
62682a34 832 self.process_generic_params(generics, item.span, &qualname, item.id);
d9579d0f 833 for method in methods {
9e0c209e
SL
834 let map = &self.tcx.map;
835 self.process_trait_item(method, make_def_id(item.id, map))
d9579d0f
AL
836 }
837 }
838
e9174d1e
SL
839 // `item` is the module in question, represented as an item.
840 fn process_mod(&mut self, item: &ast::Item) {
7453a54e 841 if let Some(mod_data) = self.save_ctxt.get_item_data(item) {
54a0048b
SL
842 down_cast_data!(mod_data, ModData, item.span);
843 if !self.span.filter_generated(Some(mod_data.span), item.span) {
a7813a04 844 self.dumper.mod_data(mod_data.lower(self.tcx));
54a0048b 845 }
7453a54e 846 }
d9579d0f
AL
847 }
848
e9174d1e 849 fn process_path(&mut self, id: NodeId, path: &ast::Path, ref_kind: Option<recorder::Row>) {
7453a54e
SL
850 let path_data = self.save_ctxt.get_path_data(id, path);
851 if generated_code(path.span) && path_data.is_none() {
c1a9b12d 852 return;
d9579d0f
AL
853 }
854
c1a9b12d
SL
855 let path_data = match path_data {
856 Some(pd) => pd,
857 None => {
c30ab7b3 858 return;
c1a9b12d
SL
859 }
860 };
54a0048b 861
c1a9b12d 862 match path_data {
54a0048b
SL
863 Data::VariableRefData(vrd) => {
864 // FIXME: this whole block duplicates the code in process_def_kind
865 if !self.span.filter_generated(Some(vrd.span), path.span) {
866 match ref_kind {
867 Some(recorder::TypeRef) => {
a7813a04 868 self.dumper.type_ref(TypeRefData {
54a0048b
SL
869 span: vrd.span,
870 ref_id: Some(vrd.ref_id),
871 scope: vrd.scope,
872 qualname: String::new()
a7813a04 873 }.lower(self.tcx));
54a0048b
SL
874 }
875 Some(recorder::FnRef) => {
a7813a04 876 self.dumper.function_ref(FunctionRefData {
54a0048b
SL
877 span: vrd.span,
878 ref_id: vrd.ref_id,
879 scope: vrd.scope
a7813a04 880 }.lower(self.tcx));
54a0048b
SL
881 }
882 Some(recorder::ModRef) => {
a7813a04 883 self.dumper.mod_ref( ModRefData {
54a0048b
SL
884 span: vrd.span,
885 ref_id: Some(vrd.ref_id),
886 scope: vrd.scope,
887 qualname: String::new()
a7813a04 888 }.lower(self.tcx));
54a0048b
SL
889 }
890 Some(recorder::VarRef) | None
a7813a04 891 => self.dumper.variable_ref(vrd.lower(self.tcx))
54a0048b
SL
892 }
893 }
c1a9b12d
SL
894
895 }
54a0048b
SL
896 Data::TypeRefData(trd) => {
897 if !self.span.filter_generated(Some(trd.span), path.span) {
a7813a04 898 self.dumper.type_ref(trd.lower(self.tcx));
54a0048b 899 }
c1a9b12d 900 }
54a0048b
SL
901 Data::MethodCallData(mcd) => {
902 if !self.span.filter_generated(Some(mcd.span), path.span) {
a7813a04 903 self.dumper.method_call(mcd.lower(self.tcx));
54a0048b 904 }
c1a9b12d
SL
905 }
906 Data::FunctionCallData(fcd) => {
54a0048b 907 if !self.span.filter_generated(Some(fcd.span), path.span) {
a7813a04 908 self.dumper.function_call(fcd.lower(self.tcx));
54a0048b 909 }
c1a9b12d
SL
910 }
911 _ => {
54a0048b 912 span_bug!(path.span, "Unexpected data: {:?}", path_data);
d9579d0f 913 }
d9579d0f 914 }
c1a9b12d
SL
915
916 // Modules or types in the path prefix.
3157f602 917 match self.tcx.expect_def(id) {
7453a54e 918 Def::Method(did) => {
c1a9b12d 919 let ti = self.tcx.impl_or_trait_item(did);
d9579d0f 920 if let ty::MethodTraitItem(m) = ti {
9cc50fc6 921 if m.explicit_self == ty::ExplicitSelfCategory::Static {
d9579d0f
AL
922 self.write_sub_path_trait_truncated(path);
923 }
924 }
925 }
c30ab7b3 926 Def::Fn(..) |
7453a54e 927 Def::Const(..) |
c30ab7b3
SL
928 Def::Static(..) |
929 Def::StructCtor(..) |
930 Def::VariantCtor(..) |
7453a54e 931 Def::AssociatedConst(..) |
c30ab7b3
SL
932 Def::Local(..) |
933 Def::Upvar(..) |
7453a54e 934 Def::Struct(..) |
c30ab7b3 935 Def::Union(..) |
7453a54e 936 Def::Variant(..) |
c30ab7b3
SL
937 Def::TyAlias(..) |
938 Def::AssociatedTy(..) => self.write_sub_paths_truncated(path, false),
e9174d1e 939 _ => {}
d9579d0f
AL
940 }
941 }
942
943 fn process_struct_lit(&mut self,
944 ex: &ast::Expr,
945 path: &ast::Path,
946 fields: &Vec<ast::Field>,
e9174d1e 947 variant: ty::VariantDef,
d9579d0f 948 base: &Option<P<ast::Expr>>) {
d9579d0f
AL
949 self.write_sub_paths_truncated(path, false);
950
62682a34 951 if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) {
54a0048b
SL
952 down_cast_data!(struct_lit_data, TypeRefData, ex.span);
953 if !self.span.filter_generated(Some(struct_lit_data.span), ex.span) {
a7813a04 954 self.dumper.type_ref(struct_lit_data.lower(self.tcx));
54a0048b
SL
955 }
956
c1a9b12d 957 let scope = self.save_ctxt.enclosing_scope(ex.id);
62682a34
SL
958
959 for field in fields {
7453a54e
SL
960 if let Some(field_data) = self.save_ctxt
961 .get_field_ref_data(field, variant, scope) {
d9579d0f 962
54a0048b 963 if !self.span.filter_generated(Some(field_data.span), field.ident.span) {
a7813a04 964 self.dumper.variable_ref(field_data.lower(self.tcx));
54a0048b 965 }
7453a54e 966 }
62682a34
SL
967
968 self.visit_expr(&field.expr)
969 }
d9579d0f 970 }
62682a34 971
b039eaaf 972 walk_list!(self, visit_expr, base);
d9579d0f
AL
973 }
974
e9174d1e 975 fn process_method_call(&mut self, ex: &ast::Expr, args: &Vec<P<ast::Expr>>) {
54a0048b
SL
976 if let Some(mcd) = self.save_ctxt.get_expr_data(ex) {
977 down_cast_data!(mcd, MethodCallData, ex.span);
978 if !self.span.filter_generated(Some(mcd.span), ex.span) {
a7813a04 979 self.dumper.method_call(mcd.lower(self.tcx));
54a0048b 980 }
c1a9b12d 981 }
d9579d0f
AL
982
983 // walk receiver and args
b039eaaf 984 walk_list!(self, visit_expr, args);
d9579d0f
AL
985 }
986
e9174d1e 987 fn process_pat(&mut self, p: &ast::Pat) {
d9579d0f 988 match p.node {
7453a54e 989 PatKind::Struct(ref path, ref fields, _) => {
d9579d0f 990 visit::walk_path(self, path);
c30ab7b3 991 let adt = self.tcx.tables().node_id_to_type(p.id).ty_adt_def().unwrap();
3157f602 992 let variant = adt.variant_of_def(self.tcx.expect_def(p.id));
d9579d0f 993
e9174d1e 994 for &Spanned { node: ref field, span } in fields {
e9174d1e
SL
995 let sub_span = self.span.span_for_first_ident(span);
996 if let Some(f) = variant.find_field_named(field.ident.name) {
54a0048b 997 if !self.span.filter_generated(sub_span, span) {
a7813a04 998 self.dumper.variable_ref(VariableRefData {
54a0048b
SL
999 span: sub_span.expect("No span fund for var ref"),
1000 ref_id: f.did,
1001 scope: self.cur_scope,
1002 name: String::new()
a7813a04 1003 }.lower(self.tcx));
54a0048b 1004 }
d9579d0f 1005 }
e9174d1e 1006 self.visit_pat(&field.pat);
d9579d0f
AL
1007 }
1008 }
e9174d1e 1009 _ => visit::walk_pat(self, p),
d9579d0f
AL
1010 }
1011 }
b039eaaf
SL
1012
1013
1014 fn process_var_decl(&mut self, p: &ast::Pat, value: String) {
1015 // The local could declare multiple new vars, we must walk the
1016 // pattern and collect them all.
1017 let mut collector = PathCollector::new();
1018 collector.visit_pat(&p);
1019 self.visit_pat(&p);
1020
1021 for &(id, ref p, immut, _) in &collector.collected_paths {
9e0c209e
SL
1022 let mut value = match immut {
1023 ast::Mutability::Immutable => value.to_string(),
1024 _ => String::new(),
b039eaaf 1025 };
c30ab7b3 1026 let typ = match self.tcx.tables().node_types.get(&id) {
9e0c209e
SL
1027 Some(typ) => {
1028 let typ = typ.to_string();
1029 if !value.is_empty() {
1030 value.push_str(": ");
1031 }
1032 value.push_str(&typ);
1033 typ
1034 }
1035 None => String::new(),
1036 };
1037
b039eaaf
SL
1038 // Get the span only for the name of the variable (I hope the path
1039 // is only ever a variable name, but who knows?).
1040 let sub_span = self.span.span_for_last_ident(p.span);
1041 // Rust uses the id of the pattern for var lookups, so we'll use it too.
54a0048b 1042 if !self.span.filter_generated(sub_span, p.span) {
a7813a04 1043 self.dumper.variable(VariableData {
54a0048b 1044 span: sub_span.expect("No span found for variable"),
3157f602 1045 kind: VariableKind::Local,
54a0048b
SL
1046 id: id,
1047 name: path_to_string(p),
1048 qualname: format!("{}${}", path_to_string(p), id),
1049 value: value,
1050 type_value: typ,
9e0c209e
SL
1051 scope: CRATE_NODE_ID,
1052 parent: None,
1053 visibility: Visibility::Inherited,
1054 docs: String::new(),
a7813a04 1055 }.lower(self.tcx));
54a0048b 1056 }
b039eaaf
SL
1057 }
1058 }
7453a54e
SL
1059
1060 /// Extract macro use and definition information from the AST node defined
1061 /// by the given NodeId, using the expansion information from the node's
1062 /// span.
1063 ///
1064 /// If the span is not macro-generated, do nothing, else use callee and
1065 /// callsite spans to record macro definition and use data, using the
1066 /// mac_uses and mac_defs sets to prevent multiples.
1067 fn process_macro_use(&mut self, span: Span, id: NodeId) {
1068 let data = match self.save_ctxt.get_macro_use_data(span, id) {
1069 None => return,
1070 Some(data) => data,
1071 };
9e0c209e 1072 let mut hasher = DefaultHasher::new();
7453a54e
SL
1073 data.callee_span.hash(&mut hasher);
1074 let hash = hasher.finish();
1075 let qualname = format!("{}::{}", data.name, hash);
1076 // Don't write macro definition for imported macros
1077 if !self.mac_defs.contains(&data.callee_span)
1078 && !data.imported {
1079 self.mac_defs.insert(data.callee_span);
1080 if let Some(sub_span) = self.span.span_for_macro_def_name(data.callee_span) {
a7813a04 1081 self.dumper.macro_data(MacroData {
54a0048b
SL
1082 span: sub_span,
1083 name: data.name.clone(),
9e0c209e
SL
1084 qualname: qualname.clone(),
1085 // FIXME where do macro docs come from?
1086 docs: String::new(),
a7813a04 1087 }.lower(self.tcx));
7453a54e
SL
1088 }
1089 }
1090 if !self.mac_uses.contains(&data.span) {
1091 self.mac_uses.insert(data.span);
1092 if let Some(sub_span) = self.span.span_for_macro_use_name(data.span) {
a7813a04 1093 self.dumper.macro_use(MacroUseData {
54a0048b
SL
1094 span: sub_span,
1095 name: data.name,
1096 qualname: qualname,
1097 scope: data.scope,
1098 callee_span: data.callee_span,
9e0c209e 1099 imported: data.imported,
a7813a04 1100 }.lower(self.tcx));
7453a54e
SL
1101 }
1102 }
1103 }
9e0c209e
SL
1104
1105 fn process_trait_item(&mut self, trait_item: &ast::TraitItem, trait_id: DefId) {
1106 self.process_macro_use(trait_item.span, trait_item.id);
1107 match trait_item.node {
1108 ast::TraitItemKind::Const(ref ty, Some(ref expr)) => {
1109 self.process_assoc_const(trait_item.id,
1110 trait_item.ident.name,
1111 trait_item.span,
1112 &ty,
1113 &expr,
1114 trait_id,
1115 Visibility::Public,
1116 &trait_item.attrs);
1117 }
1118 ast::TraitItemKind::Method(ref sig, ref body) => {
1119 self.process_method(sig,
1120 body.as_ref().map(|x| &**x),
1121 trait_item.id,
1122 trait_item.ident.name,
1123 Visibility::Public,
1124 &trait_item.attrs,
1125 trait_item.span);
1126 }
1127 ast::TraitItemKind::Const(_, None) |
1128 ast::TraitItemKind::Type(..) |
1129 ast::TraitItemKind::Macro(_) => {}
1130 }
1131 }
1132
1133 fn process_impl_item(&mut self, impl_item: &ast::ImplItem, impl_id: DefId) {
1134 self.process_macro_use(impl_item.span, impl_item.id);
1135 match impl_item.node {
1136 ast::ImplItemKind::Const(ref ty, ref expr) => {
1137 self.process_assoc_const(impl_item.id,
1138 impl_item.ident.name,
1139 impl_item.span,
1140 &ty,
1141 &expr,
1142 impl_id,
1143 From::from(&impl_item.vis),
1144 &impl_item.attrs);
1145 }
1146 ast::ImplItemKind::Method(ref sig, ref body) => {
1147 self.process_method(sig,
1148 Some(body),
1149 impl_item.id,
1150 impl_item.ident.name,
1151 From::from(&impl_item.vis),
1152 &impl_item.attrs,
1153 impl_item.span);
1154 }
1155 ast::ImplItemKind::Type(_) |
1156 ast::ImplItemKind::Macro(_) => {}
1157 }
1158 }
d9579d0f
AL
1159}
1160
3157f602 1161impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D> {
d9579d0f 1162 fn visit_item(&mut self, item: &ast::Item) {
7453a54e
SL
1163 use syntax::ast::ItemKind::*;
1164 self.process_macro_use(item.span, item.id);
d9579d0f 1165 match item.node {
7453a54e 1166 Use(ref use_item) => {
d9579d0f
AL
1167 match use_item.node {
1168 ast::ViewPathSimple(ident, ref path) => {
1169 let sub_span = self.span.span_for_last_ident(path.span);
c30ab7b3 1170 let mod_id = match self.lookup_def_id(item.id) {
d9579d0f 1171 Some(def_id) => {
54a0048b
SL
1172 let scope = self.cur_scope;
1173 self.process_def_kind(item.id, path.span, sub_span, def_id, scope);
1174
d9579d0f 1175 Some(def_id)
e9174d1e 1176 }
d9579d0f
AL
1177 None => None,
1178 };
1179
1180 // 'use' always introduces an alias, if there is not an explicit
1181 // one, there is an implicit one.
b039eaaf
SL
1182 let sub_span = match self.span.sub_span_after_keyword(use_item.span,
1183 keywords::As) {
1184 Some(sub_span) => Some(sub_span),
1185 None => sub_span,
1186 };
d9579d0f 1187
54a0048b 1188 if !self.span.filter_generated(sub_span, path.span) {
a7813a04 1189 self.dumper.use_data(UseData {
54a0048b
SL
1190 span: sub_span.expect("No span found for use"),
1191 id: item.id,
1192 mod_id: mod_id,
a7813a04 1193 name: ident.to_string(),
9e0c209e
SL
1194 scope: self.cur_scope,
1195 visibility: From::from(&item.vis),
a7813a04 1196 }.lower(self.tcx));
54a0048b 1197 }
d9579d0f
AL
1198 self.write_sub_paths_truncated(path, true);
1199 }
1200 ast::ViewPathGlob(ref path) => {
1201 // Make a comma-separated list of names of imported modules.
54a0048b 1202 let mut names = vec![];
d9579d0f
AL
1203 let glob_map = &self.analysis.glob_map;
1204 let glob_map = glob_map.as_ref().unwrap();
1205 if glob_map.contains_key(&item.id) {
1206 for n in glob_map.get(&item.id).unwrap() {
54a0048b 1207 names.push(n.to_string());
d9579d0f
AL
1208 }
1209 }
1210
b039eaaf 1211 let sub_span = self.span
a7813a04
XL
1212 .sub_span_of_token(item.span, token::BinOp(token::Star));
1213 if !self.span.filter_generated(sub_span, item.span) {
1214 self.dumper.use_glob(UseGlobData {
54a0048b
SL
1215 span: sub_span.expect("No span found for use glob"),
1216 id: item.id,
1217 names: names,
9e0c209e
SL
1218 scope: self.cur_scope,
1219 visibility: From::from(&item.vis),
a7813a04 1220 }.lower(self.tcx));
54a0048b 1221 }
d9579d0f
AL
1222 self.write_sub_paths(path, true);
1223 }
1224 ast::ViewPathList(ref path, ref list) => {
1225 for plid in list {
9e0c209e
SL
1226 let scope = self.cur_scope;
1227 let id = plid.node.id;
c30ab7b3 1228 if let Some(def_id) = self.lookup_def_id(id) {
9e0c209e
SL
1229 let span = plid.span;
1230 self.process_def_kind(id, span, Some(span), def_id, scope);
d9579d0f
AL
1231 }
1232 }
1233
1234 self.write_sub_paths(path, true);
1235 }
1236 }
1237 }
7453a54e 1238 ExternCrate(ref s) => {
d9579d0f
AL
1239 let location = match *s {
1240 Some(s) => s.to_string(),
c1a9b12d 1241 None => item.ident.to_string(),
d9579d0f
AL
1242 };
1243 let alias_span = self.span.span_for_last_ident(item.span);
92a42be0 1244 let cnum = match self.sess.cstore.extern_mod_stmt_cnum(item.id) {
d9579d0f 1245 Some(cnum) => cnum,
9e0c209e 1246 None => LOCAL_CRATE,
d9579d0f 1247 };
54a0048b
SL
1248
1249 if !self.span.filter_generated(alias_span, item.span) {
a7813a04 1250 self.dumper.extern_crate(ExternCrateData {
54a0048b 1251 id: item.id,
a7813a04 1252 name: item.ident.to_string(),
54a0048b
SL
1253 crate_num: cnum,
1254 location: location,
1255 span: alias_span.expect("No span found for extern crate"),
1256 scope: self.cur_scope,
a7813a04 1257 }.lower(self.tcx));
54a0048b 1258 }
d9579d0f 1259 }
9e0c209e 1260 Fn(ref decl, .., ref ty_params, ref body) =>
7453a54e
SL
1261 self.process_fn(item, &decl, ty_params, &body),
1262 Static(ref typ, _, ref expr) =>
d9579d0f 1263 self.process_static_or_const_item(item, typ, expr),
7453a54e 1264 Const(ref typ, ref expr) =>
d9579d0f 1265 self.process_static_or_const_item(item, &typ, &expr),
7453a54e
SL
1266 Struct(ref def, ref ty_params) => self.process_struct(item, def, ty_params),
1267 Enum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
9e0c209e 1268 Impl(..,
d9579d0f
AL
1269 ref ty_params,
1270 ref trait_ref,
1271 ref typ,
1272 ref impl_items) => {
b039eaaf 1273 self.process_impl(item, ty_params, trait_ref, &typ, impl_items)
d9579d0f 1274 }
7453a54e 1275 Trait(_, ref generics, ref trait_refs, ref methods) =>
d9579d0f 1276 self.process_trait(item, generics, trait_refs, methods),
7453a54e 1277 Mod(ref m) => {
62682a34
SL
1278 self.process_mod(item);
1279 self.nest(item.id, |v| visit::walk_mod(v, m));
1280 }
7453a54e 1281 Ty(ref ty, ref ty_params) => {
54a0048b 1282 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
7453a54e 1283 let value = ty_to_string(&ty);
d9579d0f 1284 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Type);
54a0048b 1285 if !self.span.filter_generated(sub_span, item.span) {
a7813a04 1286 self.dumper.typedef(TypeDefData {
54a0048b 1287 span: sub_span.expect("No span found for typedef"),
a7813a04 1288 name: item.ident.to_string(),
54a0048b
SL
1289 id: item.id,
1290 qualname: qualname.clone(),
9e0c209e
SL
1291 value: value,
1292 visibility: From::from(&item.vis),
1293 parent: None,
1294 docs: docs_for_attrs(&item.attrs),
a7813a04 1295 }.lower(self.tcx));
54a0048b 1296 }
d9579d0f 1297
7453a54e 1298 self.visit_ty(&ty);
d9579d0f 1299 self.process_generic_params(ty_params, item.span, &qualname, item.id);
e9174d1e 1300 }
7453a54e 1301 Mac(_) => (),
d9579d0f
AL
1302 _ => visit::walk_item(self, item),
1303 }
1304 }
1305
1306 fn visit_generics(&mut self, generics: &ast::Generics) {
62682a34
SL
1307 for param in generics.ty_params.iter() {
1308 for bound in param.bounds.iter() {
d9579d0f
AL
1309 if let ast::TraitTyParamBound(ref trait_ref, _) = *bound {
1310 self.process_trait_ref(&trait_ref.trait_ref);
1311 }
1312 }
1313 if let Some(ref ty) = param.default {
7453a54e 1314 self.visit_ty(&ty);
d9579d0f
AL
1315 }
1316 }
1317 }
1318
d9579d0f 1319 fn visit_ty(&mut self, t: &ast::Ty) {
7453a54e 1320 self.process_macro_use(t.span, t.id);
d9579d0f 1321 match t.node {
7453a54e 1322 ast::TyKind::Path(_, ref path) => {
c30ab7b3 1323 if let Some(id) = self.lookup_def_id(t.id) {
54a0048b
SL
1324 let sub_span = self.span.sub_span_for_type_name(t.span);
1325 if !self.span.filter_generated(sub_span, t.span) {
a7813a04 1326 self.dumper.type_ref(TypeRefData {
54a0048b
SL
1327 span: sub_span.expect("No span found for type ref"),
1328 ref_id: Some(id),
1329 scope: self.cur_scope,
1330 qualname: String::new()
a7813a04 1331 }.lower(self.tcx));
e9174d1e 1332 }
d9579d0f
AL
1333 }
1334
1335 self.write_sub_paths_truncated(path, false);
1336
1337 visit::walk_path(self, path);
e9174d1e 1338 }
d9579d0f
AL
1339 _ => visit::walk_ty(self, t),
1340 }
1341 }
1342
1343 fn visit_expr(&mut self, ex: &ast::Expr) {
7453a54e 1344 self.process_macro_use(ex.span, ex.id);
d9579d0f 1345 match ex.node {
7453a54e 1346 ast::ExprKind::Call(ref _f, ref _args) => {
d9579d0f
AL
1347 // Don't need to do anything for function calls,
1348 // because just walking the callee path does what we want.
1349 visit::walk_expr(self, ex);
1350 }
7453a54e 1351 ast::ExprKind::Path(_, ref path) => {
c1a9b12d 1352 self.process_path(ex.id, path, None);
d9579d0f
AL
1353 visit::walk_expr(self, ex);
1354 }
7453a54e 1355 ast::ExprKind::Struct(ref path, ref fields, ref base) => {
a7813a04 1356 let hir_expr = self.save_ctxt.tcx.map.expect_expr(ex.id);
c30ab7b3 1357 let adt = self.tcx.tables().expr_ty(&hir_expr).ty_adt_def().unwrap();
3157f602 1358 let def = self.tcx.expect_def(hir_expr.id);
b039eaaf 1359 self.process_struct_lit(ex, path, fields, adt.variant_of_def(def), base)
e9174d1e 1360 }
9e0c209e 1361 ast::ExprKind::MethodCall(.., ref args) => self.process_method_call(ex, args),
7453a54e 1362 ast::ExprKind::Field(ref sub_ex, _) => {
62682a34
SL
1363 self.visit_expr(&sub_ex);
1364
1365 if let Some(field_data) = self.save_ctxt.get_expr_data(ex) {
54a0048b
SL
1366 down_cast_data!(field_data, VariableRefData, ex.span);
1367 if !self.span.filter_generated(Some(field_data.span), ex.span) {
a7813a04 1368 self.dumper.variable_ref(field_data.lower(self.tcx));
54a0048b 1369 }
d9579d0f 1370 }
e9174d1e 1371 }
7453a54e
SL
1372 ast::ExprKind::TupField(ref sub_ex, idx) => {
1373 self.visit_expr(&sub_ex);
d9579d0f 1374
5bcae85e
SL
1375 let hir_node = match self.save_ctxt.tcx.map.find(sub_ex.id) {
1376 Some(Node::NodeExpr(expr)) => expr,
1377 _ => {
1378 debug!("Missing or weird node for sub-expression {} in {:?}",
1379 sub_ex.id, ex);
1380 return;
1381 }
1382 };
c30ab7b3 1383 let ty = &self.tcx.tables().expr_ty_adjusted(&hir_node).sty;
d9579d0f 1384 match *ty {
9e0c209e 1385 ty::TyAdt(def, _) => {
e9174d1e 1386 let sub_span = self.span.sub_span_after_token(ex.span, token::Dot);
54a0048b 1387 if !self.span.filter_generated(sub_span, ex.span) {
a7813a04 1388 self.dumper.variable_ref(VariableRefData {
54a0048b
SL
1389 span: sub_span.expect("No span found for var ref"),
1390 ref_id: def.struct_variant().fields[idx.node].did,
1391 scope: self.cur_scope,
1392 name: String::new()
a7813a04 1393 }.lower(self.tcx));
54a0048b 1394 }
d9579d0f 1395 }
62682a34 1396 ty::TyTuple(_) => {}
54a0048b
SL
1397 _ => span_bug!(ex.span,
1398 "Expected struct or tuple type, found {:?}",
1399 ty),
d9579d0f 1400 }
e9174d1e 1401 }
a7813a04 1402 ast::ExprKind::Closure(_, ref decl, ref body, _fn_decl_span) => {
62682a34 1403 let mut id = String::from("$");
d9579d0f 1404 id.push_str(&ex.id.to_string());
62682a34 1405 self.process_formals(&decl.inputs, &id);
d9579d0f
AL
1406
1407 // walk arg and return types
1408 for arg in &decl.inputs {
7453a54e 1409 self.visit_ty(&arg.ty);
d9579d0f
AL
1410 }
1411
7453a54e
SL
1412 if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output {
1413 self.visit_ty(&ret_ty);
d9579d0f
AL
1414 }
1415
1416 // walk the body
7453a54e 1417 self.nest(ex.id, |v| v.visit_block(&body));
e9174d1e 1418 }
7453a54e
SL
1419 ast::ExprKind::ForLoop(ref pattern, ref subexpression, ref block, _) |
1420 ast::ExprKind::WhileLet(ref pattern, ref subexpression, ref block, _) => {
a7813a04 1421 let value = self.span.snippet(subexpression.span);
b039eaaf
SL
1422 self.process_var_decl(pattern, value);
1423 visit::walk_expr(self, subexpression);
1424 visit::walk_block(self, block);
1425 }
7453a54e 1426 ast::ExprKind::IfLet(ref pattern, ref subexpression, ref block, ref opt_else) => {
a7813a04 1427 let value = self.span.snippet(subexpression.span);
b039eaaf
SL
1428 self.process_var_decl(pattern, value);
1429 visit::walk_expr(self, subexpression);
1430 visit::walk_block(self, block);
1431 opt_else.as_ref().map(|el| visit::walk_expr(self, el));
1432 }
d9579d0f
AL
1433 _ => {
1434 visit::walk_expr(self, ex)
1435 }
1436 }
1437 }
1438
7453a54e
SL
1439 fn visit_mac(&mut self, mac: &ast::Mac) {
1440 // These shouldn't exist in the AST at this point, log a span bug.
54a0048b 1441 span_bug!(mac.span, "macro invocation should have been expanded out of AST");
d9579d0f
AL
1442 }
1443
1444 fn visit_pat(&mut self, p: &ast::Pat) {
7453a54e 1445 self.process_macro_use(p.span, p.id);
d9579d0f
AL
1446 self.process_pat(p);
1447 }
1448
1449 fn visit_arm(&mut self, arm: &ast::Arm) {
1450 let mut collector = PathCollector::new();
1451 for pattern in &arm.pats {
1452 // collect paths from the arm's patterns
1453 collector.visit_pat(&pattern);
1454 self.visit_pat(&pattern);
1455 }
1456
1457 // This is to get around borrow checking, because we need mut self to call process_path.
1458 let mut paths_to_process = vec![];
c1a9b12d 1459
d9579d0f
AL
1460 // process collected paths
1461 for &(id, ref p, immut, ref_kind) in &collector.collected_paths {
3157f602 1462 match self.tcx.expect_def(id) {
9e0c209e
SL
1463 Def::Local(def_id) => {
1464 let id = self.tcx.map.as_local_node_id(def_id).unwrap();
1465 let mut value = if immut == ast::Mutability::Immutable {
d9579d0f
AL
1466 self.span.snippet(p.span).to_string()
1467 } else {
1468 "<mutable>".to_string()
1469 };
c30ab7b3 1470 let typ = self.tcx.tables().node_types
9e0c209e
SL
1471 .get(&id).map(|t| t.to_string()).unwrap_or(String::new());
1472 value.push_str(": ");
1473 value.push_str(&typ);
d9579d0f 1474
b039eaaf
SL
1475 assert!(p.segments.len() == 1,
1476 "qualified path for local variable def in arm");
54a0048b 1477 if !self.span.filter_generated(Some(p.span), p.span) {
a7813a04 1478 self.dumper.variable(VariableData {
54a0048b 1479 span: p.span,
3157f602 1480 kind: VariableKind::Local,
54a0048b
SL
1481 id: id,
1482 name: path_to_string(p),
1483 qualname: format!("{}${}", path_to_string(p), id),
1484 value: value,
9e0c209e
SL
1485 type_value: typ,
1486 scope: CRATE_NODE_ID,
1487 parent: None,
1488 visibility: Visibility::Inherited,
1489 docs: String::new(),
a7813a04 1490 }.lower(self.tcx));
54a0048b 1491 }
d9579d0f 1492 }
c30ab7b3
SL
1493 Def::StructCtor(..) | Def::VariantCtor(..) |
1494 Def::Const(..) | Def::AssociatedConst(..) |
1495 Def::Struct(..) | Def::Variant(..) |
1496 Def::TyAlias(..) | Def::AssociatedTy(..) |
1497 Def::SelfTy(..) => {
d9579d0f
AL
1498 paths_to_process.push((id, p.clone(), Some(ref_kind)))
1499 }
3157f602
XL
1500 def => error!("unexpected definition kind when processing collected paths: {:?}",
1501 def),
d9579d0f
AL
1502 }
1503 }
c1a9b12d 1504
d9579d0f 1505 for &(id, ref path, ref_kind) in &paths_to_process {
c1a9b12d 1506 self.process_path(id, path, ref_kind);
d9579d0f 1507 }
b039eaaf 1508 walk_list!(self, visit_expr, &arm.guard);
c1a9b12d 1509 self.visit_expr(&arm.body);
d9579d0f
AL
1510 }
1511
1512 fn visit_stmt(&mut self, s: &ast::Stmt) {
3157f602 1513 self.process_macro_use(s.span, s.id);
d9579d0f
AL
1514 visit::walk_stmt(self, s)
1515 }
1516
1517 fn visit_local(&mut self, l: &ast::Local) {
7453a54e 1518 self.process_macro_use(l.span, l.id);
a7813a04 1519 let value = l.init.as_ref().map(|i| self.span.snippet(i.span)).unwrap_or(String::new());
b039eaaf 1520 self.process_var_decl(&l.pat, value);
d9579d0f
AL
1521
1522 // Just walk the initialiser and type (don't want to walk the pattern again).
b039eaaf
SL
1523 walk_list!(self, visit_ty, &l.ty);
1524 walk_list!(self, visit_expr, &l.init);
d9579d0f
AL
1525 }
1526}