]> git.proxmox.com Git - rustc.git/blob - src/librustc_save_analysis/dump_visitor.rs
New upstream version 1.31.0~beta.4+dfsg1
[rustc.git] / src / librustc_save_analysis / dump_visitor.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 //! Write the output of rustc's analysis to an implementor of Dump.
12 //!
13 //! Dumping the analysis is implemented by walking the AST and getting a bunch of
14 //! info out from all over the place. We use Def IDs to identify objects. The
15 //! tricky part is getting syntactic (span, source text) and semantic (reference
16 //! Def IDs) information for parts of expressions which the compiler has discarded.
17 //! E.g., in a path `foo::bar::baz`, the compiler only keeps a span for the whole
18 //! path and a reference to `baz`, but we want spans and references for all three
19 //! idents.
20 //!
21 //! SpanUtils is used to manipulate spans. In particular, to extract sub-spans
22 //! from spans (e.g., the span for `bar` from the above example path).
23 //! DumpVisitor walks the AST and processes it, and JsonDumper is used for
24 //! recording the output.
25
26 use rustc::hir::def::Def as HirDef;
27 use rustc::hir::def_id::DefId;
28 use rustc::session::config::Input;
29 use rustc::ty::{self, TyCtxt};
30 use rustc_data_structures::fx::FxHashSet;
31
32 use std::path::Path;
33 use std::env;
34
35 use syntax::ast::{self, Attribute, NodeId, PatKind, CRATE_NODE_ID};
36 use syntax::parse::token;
37 use syntax::symbol::keywords;
38 use syntax::visit::{self, Visitor};
39 use syntax::print::pprust::{
40 bounds_to_string,
41 generic_params_to_string,
42 path_to_string,
43 ty_to_string
44 };
45 use syntax::ptr::P;
46 use syntax::source_map::{Spanned, DUMMY_SP, respan};
47 use syntax_pos::*;
48
49 use {escape, generated_code, lower_attributes, PathCollector, SaveContext};
50 use json_dumper::{Access, DumpOutput, JsonDumper};
51 use span_utils::SpanUtils;
52 use sig;
53
54 use rls_data::{CompilationOptions, CratePreludeData, Def, DefKind, GlobalCrateId, Import,
55 ImportKind, Ref, RefKind, Relation, RelationKind, SpanData};
56
57 macro_rules! down_cast_data {
58 ($id:ident, $kind:ident, $sp:expr) => {
59 let $id = if let super::Data::$kind(data) = $id {
60 data
61 } else {
62 span_bug!($sp, "unexpected data kind: {:?}", $id);
63 };
64 };
65 }
66
67 macro_rules! access_from {
68 ($save_ctxt:expr, $vis:expr, $id:expr) => {
69 Access {
70 public: $vis.node.is_pub(),
71 reachable: $save_ctxt.analysis.access_levels.is_reachable($id),
72 }
73 };
74
75 ($save_ctxt:expr, $item:expr) => {
76 Access {
77 public: $item.vis.node.is_pub(),
78 reachable: $save_ctxt.analysis.access_levels.is_reachable($item.id),
79 }
80 };
81 }
82
83 pub struct DumpVisitor<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> {
84 save_ctxt: SaveContext<'l, 'tcx>,
85 tcx: TyCtxt<'l, 'tcx, 'tcx>,
86 dumper: &'ll mut JsonDumper<O>,
87
88 span: SpanUtils<'l>,
89
90 cur_scope: NodeId,
91
92 // Set of macro definition (callee) spans, and the set
93 // of macro use (callsite) spans. We store these to ensure
94 // we only write one macro def per unique macro definition, and
95 // one macro use per unique callsite span.
96 // mac_defs: FxHashSet<Span>,
97 macro_calls: FxHashSet<Span>,
98 }
99
100 impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
101 pub fn new(
102 save_ctxt: SaveContext<'l, 'tcx>,
103 dumper: &'ll mut JsonDumper<O>,
104 ) -> DumpVisitor<'l, 'tcx, 'll, O> {
105 let span_utils = SpanUtils::new(&save_ctxt.tcx.sess);
106 DumpVisitor {
107 tcx: save_ctxt.tcx,
108 save_ctxt,
109 dumper,
110 span: span_utils,
111 cur_scope: CRATE_NODE_ID,
112 // mac_defs: FxHashSet::default(),
113 macro_calls: FxHashSet::default(),
114 }
115 }
116
117 fn nest_scope<F>(&mut self, scope_id: NodeId, f: F)
118 where
119 F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, O>),
120 {
121 let parent_scope = self.cur_scope;
122 self.cur_scope = scope_id;
123 f(self);
124 self.cur_scope = parent_scope;
125 }
126
127 fn nest_tables<F>(&mut self, item_id: NodeId, f: F)
128 where
129 F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, O>),
130 {
131 let item_def_id = self.tcx.hir.local_def_id(item_id);
132 if self.tcx.has_typeck_tables(item_def_id) {
133 let tables = self.tcx.typeck_tables_of(item_def_id);
134 let old_tables = self.save_ctxt.tables;
135 self.save_ctxt.tables = tables;
136 f(self);
137 self.save_ctxt.tables = old_tables;
138 } else {
139 f(self);
140 }
141 }
142
143 fn span_from_span(&self, span: Span) -> SpanData {
144 self.save_ctxt.span_from_span(span)
145 }
146
147 pub fn dump_crate_info(&mut self, name: &str, krate: &ast::Crate) {
148 let source_file = self.tcx.sess.local_crate_source_file.as_ref();
149 let crate_root = source_file.map(|source_file| {
150 let source_file = Path::new(source_file);
151 match source_file.file_name() {
152 Some(_) => source_file.parent().unwrap().display(),
153 None => source_file.display(),
154 }.to_string()
155 });
156
157 let data = CratePreludeData {
158 crate_id: GlobalCrateId {
159 name: name.into(),
160 disambiguator: self.tcx
161 .sess
162 .local_crate_disambiguator()
163 .to_fingerprint()
164 .as_value(),
165 },
166 crate_root: crate_root.unwrap_or_else(|| "<no source>".to_owned()),
167 external_crates: self.save_ctxt.get_external_crates(),
168 span: self.span_from_span(krate.span),
169 };
170
171 self.dumper.crate_prelude(data);
172 }
173
174 pub fn dump_compilation_options(&mut self, input: &Input, crate_name: &str) {
175 // Apply possible `remap-path-prefix` remapping to the input source file
176 // (and don't include remapping args anymore)
177 let (program, arguments) = {
178 let remap_arg_indices = {
179 let mut indices = FxHashSet::default();
180 // Args are guaranteed to be valid UTF-8 (checked early)
181 for (i, e) in env::args().enumerate() {
182 if e.starts_with("--remap-path-prefix=") {
183 indices.insert(i);
184 } else if e == "--remap-path-prefix" {
185 indices.insert(i);
186 indices.insert(i + 1);
187 }
188 }
189 indices
190 };
191
192 let mut args = env::args()
193 .enumerate()
194 .filter(|(i, _)| !remap_arg_indices.contains(i))
195 .map(|(_, arg)| {
196 match input {
197 Input::File(ref path) if path == Path::new(&arg) => {
198 let mapped = &self.tcx.sess.local_crate_source_file;
199 mapped
200 .as_ref()
201 .unwrap()
202 .to_string_lossy()
203 .into()
204 },
205 _ => arg,
206 }
207 });
208
209 (args.next().unwrap(), args.collect())
210 };
211
212 let data = CompilationOptions {
213 directory: self.tcx.sess.working_dir.0.clone(),
214 program,
215 arguments,
216 output: self.save_ctxt.compilation_output(crate_name),
217 };
218
219 self.dumper.compilation_opts(data);
220 }
221
222 // Return all non-empty prefixes of a path.
223 // For each prefix, we return the span for the last segment in the prefix and
224 // a str representation of the entire prefix.
225 fn process_path_prefixes(&self, path: &ast::Path) -> Vec<(Span, String)> {
226 let segments = &path.segments[if path.is_global() { 1 } else { 0 }..];
227
228 let mut result = Vec::with_capacity(segments.len());
229 let mut segs = Vec::with_capacity(segments.len());
230
231 for (i, seg) in segments.iter().enumerate() {
232 segs.push(seg.clone());
233 let sub_path = ast::Path {
234 span: seg.ident.span, // span for the last segment
235 segments: segs,
236 };
237 let qualname = if i == 0 && path.is_global() {
238 format!("::{}", path_to_string(&sub_path))
239 } else {
240 path_to_string(&sub_path)
241 };
242 result.push((seg.ident.span, qualname));
243 segs = sub_path.segments;
244 }
245
246 result
247 }
248
249 fn write_sub_paths(&mut self, path: &ast::Path) {
250 let sub_paths = self.process_path_prefixes(path);
251 for (span, _) in sub_paths {
252 let span = self.span_from_span(span);
253 self.dumper.dump_ref(Ref {
254 kind: RefKind::Mod,
255 span,
256 ref_id: ::null_id(),
257 });
258 }
259 }
260
261 // As write_sub_paths, but does not process the last ident in the path (assuming it
262 // will be processed elsewhere). See note on write_sub_paths about global.
263 fn write_sub_paths_truncated(&mut self, path: &ast::Path) {
264 let sub_paths = self.process_path_prefixes(path);
265 let len = sub_paths.len();
266 if len <= 1 {
267 return;
268 }
269
270 for (span, _) in sub_paths.into_iter().take(len - 1) {
271 let span = self.span_from_span(span);
272 self.dumper.dump_ref(Ref {
273 kind: RefKind::Mod,
274 span,
275 ref_id: ::null_id(),
276 });
277 }
278 }
279
280 // As write_sub_paths, but expects a path of the form module_path::trait::method
281 // Where trait could actually be a struct too.
282 fn write_sub_path_trait_truncated(&mut self, path: &ast::Path) {
283 let sub_paths = self.process_path_prefixes(path);
284 let len = sub_paths.len();
285 if len <= 1 {
286 return;
287 }
288 let sub_paths = &sub_paths[..(len - 1)];
289
290 // write the trait part of the sub-path
291 let (ref span, _) = sub_paths[len - 2];
292 let span = self.span_from_span(*span);
293 self.dumper.dump_ref(Ref {
294 kind: RefKind::Type,
295 ref_id: ::null_id(),
296 span,
297 });
298
299 // write the other sub-paths
300 if len <= 2 {
301 return;
302 }
303 let sub_paths = &sub_paths[..len - 2];
304 for &(ref span, _) in sub_paths {
305 let span = self.span_from_span(*span);
306 self.dumper.dump_ref(Ref {
307 kind: RefKind::Mod,
308 span,
309 ref_id: ::null_id(),
310 });
311 }
312 }
313
314 fn lookup_def_id(&self, ref_id: NodeId) -> Option<DefId> {
315 match self.save_ctxt.get_path_def(ref_id) {
316 HirDef::PrimTy(..) | HirDef::SelfTy(..) | HirDef::Err => None,
317 def => Some(def.def_id()),
318 }
319 }
320
321 fn process_formals(&mut self, formals: &'l [ast::Arg], qualname: &str) {
322 for arg in formals {
323 self.visit_pat(&arg.pat);
324 let mut collector = PathCollector::new();
325 collector.visit_pat(&arg.pat);
326 let span_utils = self.span.clone();
327
328 for (id, ident, ..) in collector.collected_idents {
329 let hir_id = self.tcx.hir.node_to_hir_id(id);
330 let typ = match self.save_ctxt.tables.node_id_to_type_opt(hir_id) {
331 Some(s) => s.to_string(),
332 None => continue,
333 };
334 let sub_span = span_utils.span_for_last_ident(ident.span);
335 if !self.span.filter_generated(sub_span, ident.span) {
336 let id = ::id_from_node_id(id, &self.save_ctxt);
337 let span = self.span_from_span(sub_span.expect("No span found for variable"));
338
339 self.dumper.dump_def(
340 &Access {
341 public: false,
342 reachable: false,
343 },
344 Def {
345 kind: DefKind::Local,
346 id,
347 span,
348 name: ident.to_string(),
349 qualname: format!("{}::{}", qualname, ident.to_string()),
350 value: typ,
351 parent: None,
352 children: vec![],
353 decl_id: None,
354 docs: String::new(),
355 sig: None,
356 attributes: vec![],
357 },
358 );
359 }
360 }
361 }
362 }
363
364 fn process_method(
365 &mut self,
366 sig: &'l ast::MethodSig,
367 body: Option<&'l ast::Block>,
368 id: ast::NodeId,
369 ident: ast::Ident,
370 generics: &'l ast::Generics,
371 vis: ast::Visibility,
372 span: Span,
373 ) {
374 debug!("process_method: {}:{}", id, ident);
375
376 if let Some(mut method_data) = self.save_ctxt.get_method_data(id, ident.name, span) {
377 let sig_str = ::make_signature(&sig.decl, &generics);
378 if body.is_some() {
379 self.nest_tables(
380 id,
381 |v| v.process_formals(&sig.decl.inputs, &method_data.qualname),
382 );
383 }
384
385 self.process_generic_params(&generics, span, &method_data.qualname, id);
386
387 method_data.value = sig_str;
388 method_data.sig = sig::method_signature(id, ident, generics, sig, &self.save_ctxt);
389 self.dumper.dump_def(&access_from!(self.save_ctxt, vis, id), method_data);
390 }
391
392 // walk arg and return types
393 for arg in &sig.decl.inputs {
394 self.visit_ty(&arg.ty);
395 }
396
397 if let ast::FunctionRetTy::Ty(ref ret_ty) = sig.decl.output {
398 self.visit_ty(ret_ty);
399 }
400
401 // walk the fn body
402 if let Some(body) = body {
403 self.nest_tables(id, |v| v.nest_scope(id, |v| v.visit_block(body)));
404 }
405 }
406
407 fn process_struct_field_def(&mut self, field: &ast::StructField, parent_id: NodeId) {
408 let field_data = self.save_ctxt.get_field_data(field, parent_id);
409 if let Some(field_data) = field_data {
410 self.dumper.dump_def(&access_from!(self.save_ctxt, field), field_data);
411 }
412 }
413
414 // Dump generic params bindings, then visit_generics
415 fn process_generic_params(
416 &mut self,
417 generics: &'l ast::Generics,
418 full_span: Span,
419 prefix: &str,
420 id: NodeId,
421 ) {
422 for param in &generics.params {
423 match param.kind {
424 ast::GenericParamKind::Lifetime { .. } => {}
425 ast::GenericParamKind::Type { .. } => {
426 let param_ss = param.ident.span;
427 let name = escape(self.span.snippet(param_ss));
428 // Append $id to name to make sure each one is unique.
429 let qualname = format!("{}::{}${}", prefix, name, id);
430 if !self.span.filter_generated(Some(param_ss), full_span) {
431 let id = ::id_from_node_id(param.id, &self.save_ctxt);
432 let span = self.span_from_span(param_ss);
433
434 self.dumper.dump_def(
435 &Access {
436 public: false,
437 reachable: false,
438 },
439 Def {
440 kind: DefKind::Type,
441 id,
442 span,
443 name,
444 qualname,
445 value: String::new(),
446 parent: None,
447 children: vec![],
448 decl_id: None,
449 docs: String::new(),
450 sig: None,
451 attributes: vec![],
452 },
453 );
454 }
455 }
456 }
457 }
458 self.visit_generics(generics);
459 }
460
461 fn process_fn(
462 &mut self,
463 item: &'l ast::Item,
464 decl: &'l ast::FnDecl,
465 ty_params: &'l ast::Generics,
466 body: &'l ast::Block,
467 ) {
468 if let Some(fn_data) = self.save_ctxt.get_item_data(item) {
469 down_cast_data!(fn_data, DefData, item.span);
470 self.nest_tables(
471 item.id,
472 |v| v.process_formals(&decl.inputs, &fn_data.qualname),
473 );
474 self.process_generic_params(ty_params, item.span, &fn_data.qualname, item.id);
475 self.dumper.dump_def(&access_from!(self.save_ctxt, item), fn_data);
476 }
477
478 for arg in &decl.inputs {
479 self.visit_ty(&arg.ty);
480 }
481
482 if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output {
483 self.visit_ty(&ret_ty);
484 }
485
486 self.nest_tables(item.id, |v| v.nest_scope(item.id, |v| v.visit_block(&body)));
487 }
488
489 fn process_static_or_const_item(
490 &mut self,
491 item: &'l ast::Item,
492 typ: &'l ast::Ty,
493 expr: &'l ast::Expr,
494 ) {
495 self.nest_tables(item.id, |v| {
496 if let Some(var_data) = v.save_ctxt.get_item_data(item) {
497 down_cast_data!(var_data, DefData, item.span);
498 v.dumper.dump_def(&access_from!(v.save_ctxt, item), var_data);
499 }
500 v.visit_ty(&typ);
501 v.visit_expr(expr);
502 });
503 }
504
505 fn process_assoc_const(
506 &mut self,
507 id: ast::NodeId,
508 name: ast::Name,
509 span: Span,
510 typ: &'l ast::Ty,
511 expr: Option<&'l ast::Expr>,
512 parent_id: DefId,
513 vis: ast::Visibility,
514 attrs: &'l [Attribute],
515 ) {
516 let qualname = format!("::{}", self.tcx.node_path_str(id));
517
518 let sub_span = self.span.sub_span_after_keyword(span, keywords::Const);
519
520 if !self.span.filter_generated(sub_span, span) {
521 let sig = sig::assoc_const_signature(id, name, typ, expr, &self.save_ctxt);
522 let span = self.span_from_span(sub_span.expect("No span found for variable"));
523
524 self.dumper.dump_def(
525 &access_from!(self.save_ctxt, vis, id),
526 Def {
527 kind: DefKind::Const,
528 id: ::id_from_node_id(id, &self.save_ctxt),
529 span,
530 name: name.to_string(),
531 qualname,
532 value: ty_to_string(&typ),
533 parent: Some(::id_from_def_id(parent_id)),
534 children: vec![],
535 decl_id: None,
536 docs: self.save_ctxt.docs_for_attrs(attrs),
537 sig,
538 attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt),
539 },
540 );
541 }
542
543 // walk type and init value
544 self.visit_ty(typ);
545 if let Some(expr) = expr {
546 self.visit_expr(expr);
547 }
548 }
549
550 // FIXME tuple structs should generate tuple-specific data.
551 fn process_struct(
552 &mut self,
553 item: &'l ast::Item,
554 def: &'l ast::VariantData,
555 ty_params: &'l ast::Generics,
556 ) {
557 debug!("process_struct {:?} {:?}", item, item.span);
558 let name = item.ident.to_string();
559 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
560
561 let (kind, keyword) = match item.node {
562 ast::ItemKind::Struct(_, _) => (DefKind::Struct, keywords::Struct),
563 ast::ItemKind::Union(_, _) => (DefKind::Union, keywords::Union),
564 _ => unreachable!(),
565 };
566
567 let sub_span = self.span.sub_span_after_keyword(item.span, keyword);
568 let (value, fields) = match item.node {
569 ast::ItemKind::Struct(ast::VariantData::Struct(ref fields, _), _) |
570 ast::ItemKind::Union(ast::VariantData::Struct(ref fields, _), _) => {
571 let include_priv_fields = !self.save_ctxt.config.pub_only;
572 let fields_str = fields
573 .iter()
574 .enumerate()
575 .filter_map(|(i, f)| {
576 if include_priv_fields || f.vis.node.is_pub() {
577 f.ident
578 .map(|i| i.to_string())
579 .or_else(|| Some(i.to_string()))
580 } else {
581 None
582 }
583 })
584 .collect::<Vec<_>>()
585 .join(", ");
586 let value = format!("{} {{ {} }}", name, fields_str);
587 (
588 value,
589 fields
590 .iter()
591 .map(|f| ::id_from_node_id(f.id, &self.save_ctxt))
592 .collect(),
593 )
594 }
595 _ => (String::new(), vec![]),
596 };
597
598 if !self.span.filter_generated(sub_span, item.span) {
599 let span = self.span_from_span(sub_span.expect("No span found for struct"));
600 self.dumper.dump_def(
601 &access_from!(self.save_ctxt, item),
602 Def {
603 kind,
604 id: ::id_from_node_id(item.id, &self.save_ctxt),
605 span,
606 name,
607 qualname: qualname.clone(),
608 value,
609 parent: None,
610 children: fields,
611 decl_id: None,
612 docs: self.save_ctxt.docs_for_attrs(&item.attrs),
613 sig: sig::item_signature(item, &self.save_ctxt),
614 attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt),
615 },
616 );
617 }
618
619 for field in def.fields() {
620 self.process_struct_field_def(field, item.id);
621 self.visit_ty(&field.ty);
622 }
623
624 self.process_generic_params(ty_params, item.span, &qualname, item.id);
625 }
626
627 fn process_enum(
628 &mut self,
629 item: &'l ast::Item,
630 enum_definition: &'l ast::EnumDef,
631 ty_params: &'l ast::Generics,
632 ) {
633 let enum_data = self.save_ctxt.get_item_data(item);
634 let enum_data = match enum_data {
635 None => return,
636 Some(data) => data,
637 };
638 down_cast_data!(enum_data, DefData, item.span);
639
640 let access = access_from!(self.save_ctxt, item);
641
642 for variant in &enum_definition.variants {
643 let name = variant.node.ident.name.to_string();
644 let qualname = format!("{}::{}", enum_data.qualname, name);
645
646 match variant.node.data {
647 ast::VariantData::Struct(ref fields, _) => {
648 let sub_span = self.span.span_for_first_ident(variant.span);
649 let fields_str = fields
650 .iter()
651 .enumerate()
652 .map(|(i, f)| {
653 f.ident.map(|i| i.to_string()).unwrap_or_else(|| i.to_string())
654 })
655 .collect::<Vec<_>>()
656 .join(", ");
657 let value = format!("{}::{} {{ {} }}", enum_data.name, name, fields_str);
658 if !self.span.filter_generated(sub_span, variant.span) {
659 let span = self
660 .span_from_span(sub_span.expect("No span found for struct variant"));
661 let id = ::id_from_node_id(variant.node.data.id(), &self.save_ctxt);
662 let parent = Some(::id_from_node_id(item.id, &self.save_ctxt));
663
664 self.dumper.dump_def(
665 &access,
666 Def {
667 kind: DefKind::StructVariant,
668 id,
669 span,
670 name,
671 qualname,
672 value,
673 parent,
674 children: vec![],
675 decl_id: None,
676 docs: self.save_ctxt.docs_for_attrs(&variant.node.attrs),
677 sig: sig::variant_signature(variant, &self.save_ctxt),
678 attributes: lower_attributes(
679 variant.node.attrs.clone(),
680 &self.save_ctxt,
681 ),
682 },
683 );
684 }
685 }
686 ref v => {
687 let sub_span = self.span.span_for_first_ident(variant.span);
688 let mut value = format!("{}::{}", enum_data.name, name);
689 if let &ast::VariantData::Tuple(ref fields, _) = v {
690 value.push('(');
691 value.push_str(&fields
692 .iter()
693 .map(|f| ty_to_string(&f.ty))
694 .collect::<Vec<_>>()
695 .join(", "));
696 value.push(')');
697 }
698 if !self.span.filter_generated(sub_span, variant.span) {
699 let span =
700 self.span_from_span(sub_span.expect("No span found for tuple variant"));
701 let id = ::id_from_node_id(variant.node.data.id(), &self.save_ctxt);
702 let parent = Some(::id_from_node_id(item.id, &self.save_ctxt));
703
704 self.dumper.dump_def(
705 &access,
706 Def {
707 kind: DefKind::TupleVariant,
708 id,
709 span,
710 name,
711 qualname,
712 value,
713 parent,
714 children: vec![],
715 decl_id: None,
716 docs: self.save_ctxt.docs_for_attrs(&variant.node.attrs),
717 sig: sig::variant_signature(variant, &self.save_ctxt),
718 attributes: lower_attributes(
719 variant.node.attrs.clone(),
720 &self.save_ctxt,
721 ),
722 },
723 );
724 }
725 }
726 }
727
728
729 for field in variant.node.data.fields() {
730 self.process_struct_field_def(field, variant.node.data.id());
731 self.visit_ty(&field.ty);
732 }
733 }
734 self.process_generic_params(ty_params, item.span, &enum_data.qualname, item.id);
735 self.dumper.dump_def(&access, enum_data);
736 }
737
738 fn process_impl(
739 &mut self,
740 item: &'l ast::Item,
741 type_parameters: &'l ast::Generics,
742 trait_ref: &'l Option<ast::TraitRef>,
743 typ: &'l ast::Ty,
744 impl_items: &'l [ast::ImplItem],
745 ) {
746 if let Some(impl_data) = self.save_ctxt.get_item_data(item) {
747 if let super::Data::RelationData(rel, imp) = impl_data {
748 self.dumper.dump_relation(rel);
749 self.dumper.dump_impl(imp);
750 } else {
751 span_bug!(item.span, "unexpected data kind: {:?}", impl_data);
752 }
753 }
754 self.visit_ty(&typ);
755 if let &Some(ref trait_ref) = trait_ref {
756 self.process_path(trait_ref.ref_id, &trait_ref.path);
757 }
758 self.process_generic_params(type_parameters, item.span, "", item.id);
759 for impl_item in impl_items {
760 let map = &self.tcx.hir;
761 self.process_impl_item(impl_item, map.local_def_id(item.id));
762 }
763 }
764
765 fn process_trait(
766 &mut self,
767 item: &'l ast::Item,
768 generics: &'l ast::Generics,
769 trait_refs: &'l ast::GenericBounds,
770 methods: &'l [ast::TraitItem],
771 ) {
772 let name = item.ident.to_string();
773 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
774 let mut val = name.clone();
775 if !generics.params.is_empty() {
776 val.push_str(&generic_params_to_string(&generics.params));
777 }
778 if !trait_refs.is_empty() {
779 val.push_str(": ");
780 val.push_str(&bounds_to_string(trait_refs));
781 }
782 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Trait);
783 if !self.span.filter_generated(sub_span, item.span) {
784 let id = ::id_from_node_id(item.id, &self.save_ctxt);
785 let span = self.span_from_span(sub_span.expect("No span found for trait"));
786 let children = methods
787 .iter()
788 .map(|i| ::id_from_node_id(i.id, &self.save_ctxt))
789 .collect();
790 self.dumper.dump_def(
791 &access_from!(self.save_ctxt, item),
792 Def {
793 kind: DefKind::Trait,
794 id,
795 span,
796 name,
797 qualname: qualname.clone(),
798 value: val,
799 parent: None,
800 children,
801 decl_id: None,
802 docs: self.save_ctxt.docs_for_attrs(&item.attrs),
803 sig: sig::item_signature(item, &self.save_ctxt),
804 attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt),
805 },
806 );
807 }
808
809 // super-traits
810 for super_bound in trait_refs.iter() {
811 let trait_ref = match *super_bound {
812 ast::GenericBound::Trait(ref trait_ref, _) => trait_ref,
813 ast::GenericBound::Outlives(..) => continue,
814 };
815
816 let trait_ref = &trait_ref.trait_ref;
817 if let Some(id) = self.lookup_def_id(trait_ref.ref_id) {
818 let sub_span = self.span.sub_span_for_type_name(trait_ref.path.span);
819 if !self.span.filter_generated(sub_span, trait_ref.path.span) {
820 let span = self.span_from_span(sub_span.expect("No span found for trait ref"));
821 self.dumper.dump_ref(Ref {
822 kind: RefKind::Type,
823 span,
824 ref_id: ::id_from_def_id(id),
825 });
826 }
827
828 if !self.span.filter_generated(sub_span, trait_ref.path.span) {
829 let sub_span = self.span_from_span(sub_span.expect("No span for inheritance"));
830 self.dumper.dump_relation(Relation {
831 kind: RelationKind::SuperTrait,
832 span: sub_span,
833 from: ::id_from_def_id(id),
834 to: ::id_from_node_id(item.id, &self.save_ctxt),
835 });
836 }
837 }
838 }
839
840 // walk generics and methods
841 self.process_generic_params(generics, item.span, &qualname, item.id);
842 for method in methods {
843 let map = &self.tcx.hir;
844 self.process_trait_item(method, map.local_def_id(item.id))
845 }
846 }
847
848 // `item` is the module in question, represented as an item.
849 fn process_mod(&mut self, item: &ast::Item) {
850 if let Some(mod_data) = self.save_ctxt.get_item_data(item) {
851 down_cast_data!(mod_data, DefData, item.span);
852 self.dumper.dump_def(&access_from!(self.save_ctxt, item), mod_data);
853 }
854 }
855
856 fn dump_path_ref(&mut self, id: NodeId, path: &ast::Path) {
857 let path_data = self.save_ctxt.get_path_data(id, path);
858 if let Some(path_data) = path_data {
859 self.dumper.dump_ref(path_data);
860 }
861 }
862
863 fn process_path(&mut self, id: NodeId, path: &'l ast::Path) {
864 debug!("process_path {:?}", path);
865 if generated_code(path.span) {
866 return;
867 }
868 self.dump_path_ref(id, path);
869
870 // Type arguments
871 for seg in &path.segments {
872 if let Some(ref generic_args) = seg.args {
873 match **generic_args {
874 ast::GenericArgs::AngleBracketed(ref data) => {
875 for arg in &data.args {
876 match arg {
877 ast::GenericArg::Type(ty) => self.visit_ty(ty),
878 _ => {}
879 }
880 }
881 }
882 ast::GenericArgs::Parenthesized(ref data) => {
883 for t in &data.inputs {
884 self.visit_ty(t);
885 }
886 if let Some(ref t) = data.output {
887 self.visit_ty(t);
888 }
889 }
890 }
891 }
892 }
893
894 // Modules or types in the path prefix.
895 match self.save_ctxt.get_path_def(id) {
896 HirDef::Method(did) => {
897 let ti = self.tcx.associated_item(did);
898 if ti.kind == ty::AssociatedKind::Method && ti.method_has_self_argument {
899 self.write_sub_path_trait_truncated(path);
900 }
901 }
902 HirDef::Fn(..) |
903 HirDef::Const(..) |
904 HirDef::Static(..) |
905 HirDef::StructCtor(..) |
906 HirDef::VariantCtor(..) |
907 HirDef::AssociatedConst(..) |
908 HirDef::Local(..) |
909 HirDef::Upvar(..) |
910 HirDef::Struct(..) |
911 HirDef::Union(..) |
912 HirDef::Variant(..) |
913 HirDef::TyAlias(..) |
914 HirDef::AssociatedTy(..) => self.write_sub_paths_truncated(path),
915 _ => {}
916 }
917 }
918
919 fn process_struct_lit(
920 &mut self,
921 ex: &'l ast::Expr,
922 path: &'l ast::Path,
923 fields: &'l [ast::Field],
924 variant: &'l ty::VariantDef,
925 base: &'l Option<P<ast::Expr>>,
926 ) {
927 self.write_sub_paths_truncated(path);
928
929 if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) {
930 down_cast_data!(struct_lit_data, RefData, ex.span);
931 if !generated_code(ex.span) {
932 self.dumper.dump_ref(struct_lit_data);
933 }
934
935 for field in fields {
936 if let Some(field_data) = self.save_ctxt.get_field_ref_data(field, variant) {
937 self.dumper.dump_ref(field_data);
938 }
939
940 self.visit_expr(&field.expr)
941 }
942 }
943
944 walk_list!(self, visit_expr, base);
945 }
946
947 fn process_method_call(
948 &mut self,
949 ex: &'l ast::Expr,
950 seg: &'l ast::PathSegment,
951 args: &'l [P<ast::Expr>],
952 ) {
953 debug!("process_method_call {:?} {:?}", ex, ex.span);
954 if let Some(mcd) = self.save_ctxt.get_expr_data(ex) {
955 down_cast_data!(mcd, RefData, ex.span);
956 if !generated_code(ex.span) {
957 self.dumper.dump_ref(mcd);
958 }
959 }
960
961 // Explicit types in the turbo-fish.
962 if let Some(ref generic_args) = seg.args {
963 if let ast::GenericArgs::AngleBracketed(ref data) = **generic_args {
964 for arg in &data.args {
965 match arg {
966 ast::GenericArg::Type(ty) => self.visit_ty(ty),
967 _ => {}
968 }
969 }
970 }
971 }
972
973 // walk receiver and args
974 walk_list!(self, visit_expr, args);
975 }
976
977 fn process_pat(&mut self, p: &'l ast::Pat) {
978 match p.node {
979 PatKind::Struct(ref _path, ref fields, _) => {
980 // FIXME do something with _path?
981 let hir_id = self.tcx.hir.node_to_hir_id(p.id);
982 let adt = match self.save_ctxt.tables.node_id_to_type_opt(hir_id) {
983 Some(ty) => ty.ty_adt_def().unwrap(),
984 None => {
985 visit::walk_pat(self, p);
986 return;
987 }
988 };
989 let variant = adt.variant_of_def(self.save_ctxt.get_path_def(p.id));
990
991 for &Spanned { node: ref field, span } in fields {
992 let sub_span = self.span.span_for_first_ident(span);
993 if let Some(index) = self.tcx.find_field_index(field.ident, variant) {
994 if !self.span.filter_generated(sub_span, span) {
995 let span =
996 self.span_from_span(sub_span.expect("No span fund for var ref"));
997 self.dumper.dump_ref(Ref {
998 kind: RefKind::Variable,
999 span,
1000 ref_id: ::id_from_def_id(variant.fields[index].did),
1001 });
1002 }
1003 }
1004 self.visit_pat(&field.pat);
1005 }
1006 }
1007 _ => visit::walk_pat(self, p),
1008 }
1009 }
1010
1011 fn process_var_decl_multi(&mut self, pats: &'l [P<ast::Pat>]) {
1012 let mut collector = PathCollector::new();
1013 for pattern in pats {
1014 // collect paths from the arm's patterns
1015 collector.visit_pat(&pattern);
1016 self.visit_pat(&pattern);
1017 }
1018
1019 // process collected paths
1020 for (id, ident, immut) in collector.collected_idents {
1021 match self.save_ctxt.get_path_def(id) {
1022 HirDef::Local(id) => {
1023 let mut value = if immut == ast::Mutability::Immutable {
1024 self.span.snippet(ident.span)
1025 } else {
1026 "<mutable>".to_owned()
1027 };
1028 let hir_id = self.tcx.hir.node_to_hir_id(id);
1029 let typ = self.save_ctxt
1030 .tables
1031 .node_id_to_type_opt(hir_id)
1032 .map(|t| t.to_string())
1033 .unwrap_or_default();
1034 value.push_str(": ");
1035 value.push_str(&typ);
1036
1037 if !self.span.filter_generated(Some(ident.span), ident.span) {
1038 let qualname = format!("{}${}", ident.to_string(), id);
1039 let id = ::id_from_node_id(id, &self.save_ctxt);
1040 let span = self.span_from_span(ident.span);
1041
1042 self.dumper.dump_def(
1043 &Access {
1044 public: false,
1045 reachable: false,
1046 },
1047 Def {
1048 kind: DefKind::Local,
1049 id,
1050 span,
1051 name: ident.to_string(),
1052 qualname,
1053 value: typ,
1054 parent: None,
1055 children: vec![],
1056 decl_id: None,
1057 docs: String::new(),
1058 sig: None,
1059 attributes: vec![],
1060 },
1061 );
1062 }
1063 }
1064 HirDef::StructCtor(..) |
1065 HirDef::VariantCtor(..) |
1066 HirDef::Const(..) |
1067 HirDef::AssociatedConst(..) |
1068 HirDef::Struct(..) |
1069 HirDef::Variant(..) |
1070 HirDef::TyAlias(..) |
1071 HirDef::AssociatedTy(..) |
1072 HirDef::SelfTy(..) => {
1073 self.dump_path_ref(id, &ast::Path::from_ident(ident));
1074 }
1075 def => error!(
1076 "unexpected definition kind when processing collected idents: {:?}",
1077 def
1078 ),
1079 }
1080 }
1081
1082 for (id, ref path) in collector.collected_paths {
1083 self.process_path(id, path);
1084 }
1085 }
1086
1087 fn process_var_decl(&mut self, p: &'l ast::Pat, value: String) {
1088 // The local could declare multiple new vars, we must walk the
1089 // pattern and collect them all.
1090 let mut collector = PathCollector::new();
1091 collector.visit_pat(&p);
1092 self.visit_pat(&p);
1093
1094 for (id, ident, immut) in collector.collected_idents {
1095 let mut value = match immut {
1096 ast::Mutability::Immutable => value.to_string(),
1097 _ => String::new(),
1098 };
1099 let hir_id = self.tcx.hir.node_to_hir_id(id);
1100 let typ = match self.save_ctxt.tables.node_id_to_type_opt(hir_id) {
1101 Some(typ) => {
1102 let typ = typ.to_string();
1103 if !value.is_empty() {
1104 value.push_str(": ");
1105 }
1106 value.push_str(&typ);
1107 typ
1108 }
1109 None => String::new(),
1110 };
1111
1112 // Get the span only for the name of the variable (I hope the path
1113 // is only ever a variable name, but who knows?).
1114 let sub_span = self.span.span_for_last_ident(ident.span);
1115 // Rust uses the id of the pattern for var lookups, so we'll use it too.
1116 if !self.span.filter_generated(sub_span, ident.span) {
1117 let qualname = format!("{}${}", ident.to_string(), id);
1118 let id = ::id_from_node_id(id, &self.save_ctxt);
1119 let span = self.span_from_span(sub_span.expect("No span found for variable"));
1120
1121 self.dumper.dump_def(
1122 &Access {
1123 public: false,
1124 reachable: false,
1125 },
1126 Def {
1127 kind: DefKind::Local,
1128 id,
1129 span,
1130 name: ident.to_string(),
1131 qualname,
1132 value: typ,
1133 parent: None,
1134 children: vec![],
1135 decl_id: None,
1136 docs: String::new(),
1137 sig: None,
1138 attributes: vec![],
1139 },
1140 );
1141 }
1142 }
1143 }
1144
1145 /// Extract macro use and definition information from the AST node defined
1146 /// by the given NodeId, using the expansion information from the node's
1147 /// span.
1148 ///
1149 /// If the span is not macro-generated, do nothing, else use callee and
1150 /// callsite spans to record macro definition and use data, using the
1151 /// mac_uses and mac_defs sets to prevent multiples.
1152 fn process_macro_use(&mut self, span: Span) {
1153 let source_span = span.source_callsite();
1154 if !self.macro_calls.insert(source_span) {
1155 return;
1156 }
1157
1158 let data = match self.save_ctxt.get_macro_use_data(span) {
1159 None => return,
1160 Some(data) => data,
1161 };
1162
1163 self.dumper.macro_use(data);
1164
1165 // FIXME write the macro def
1166 // let mut hasher = DefaultHasher::new();
1167 // data.callee_span.hash(&mut hasher);
1168 // let hash = hasher.finish();
1169 // let qualname = format!("{}::{}", data.name, hash);
1170 // Don't write macro definition for imported macros
1171 // if !self.mac_defs.contains(&data.callee_span)
1172 // && !data.imported {
1173 // self.mac_defs.insert(data.callee_span);
1174 // if let Some(sub_span) = self.span.span_for_macro_def_name(data.callee_span) {
1175 // self.dumper.macro_data(MacroData {
1176 // span: sub_span,
1177 // name: data.name.clone(),
1178 // qualname: qualname.clone(),
1179 // // FIXME where do macro docs come from?
1180 // docs: String::new(),
1181 // }.lower(self.tcx));
1182 // }
1183 // }
1184 }
1185
1186 fn process_trait_item(&mut self, trait_item: &'l ast::TraitItem, trait_id: DefId) {
1187 self.process_macro_use(trait_item.span);
1188 let vis_span = trait_item.span.shrink_to_lo();
1189 match trait_item.node {
1190 ast::TraitItemKind::Const(ref ty, ref expr) => {
1191 self.process_assoc_const(
1192 trait_item.id,
1193 trait_item.ident.name,
1194 trait_item.span,
1195 &ty,
1196 expr.as_ref().map(|e| &**e),
1197 trait_id,
1198 respan(vis_span, ast::VisibilityKind::Public),
1199 &trait_item.attrs,
1200 );
1201 }
1202 ast::TraitItemKind::Method(ref sig, ref body) => {
1203 self.process_method(
1204 sig,
1205 body.as_ref().map(|x| &**x),
1206 trait_item.id,
1207 trait_item.ident,
1208 &trait_item.generics,
1209 respan(vis_span, ast::VisibilityKind::Public),
1210 trait_item.span,
1211 );
1212 }
1213 ast::TraitItemKind::Type(ref bounds, ref default_ty) => {
1214 // FIXME do something with _bounds (for type refs)
1215 let name = trait_item.ident.name.to_string();
1216 let qualname = format!("::{}", self.tcx.node_path_str(trait_item.id));
1217 let sub_span = self.span
1218 .sub_span_after_keyword(trait_item.span, keywords::Type);
1219
1220 if !self.span.filter_generated(sub_span, trait_item.span) {
1221 let span = self.span_from_span(sub_span.expect("No span found for assoc type"));
1222 let id = ::id_from_node_id(trait_item.id, &self.save_ctxt);
1223
1224 self.dumper.dump_def(
1225 &Access {
1226 public: true,
1227 reachable: true,
1228 },
1229 Def {
1230 kind: DefKind::Type,
1231 id,
1232 span,
1233 name,
1234 qualname,
1235 value: self.span.snippet(trait_item.span),
1236 parent: Some(::id_from_def_id(trait_id)),
1237 children: vec![],
1238 decl_id: None,
1239 docs: self.save_ctxt.docs_for_attrs(&trait_item.attrs),
1240 sig: sig::assoc_type_signature(
1241 trait_item.id,
1242 trait_item.ident,
1243 Some(bounds),
1244 default_ty.as_ref().map(|ty| &**ty),
1245 &self.save_ctxt,
1246 ),
1247 attributes: lower_attributes(trait_item.attrs.clone(), &self.save_ctxt),
1248 },
1249 );
1250 }
1251
1252 if let &Some(ref default_ty) = default_ty {
1253 self.visit_ty(default_ty)
1254 }
1255 }
1256 ast::TraitItemKind::Macro(_) => {}
1257 }
1258 }
1259
1260 fn process_impl_item(&mut self, impl_item: &'l ast::ImplItem, impl_id: DefId) {
1261 self.process_macro_use(impl_item.span);
1262 match impl_item.node {
1263 ast::ImplItemKind::Const(ref ty, ref expr) => {
1264 self.process_assoc_const(
1265 impl_item.id,
1266 impl_item.ident.name,
1267 impl_item.span,
1268 &ty,
1269 Some(expr),
1270 impl_id,
1271 impl_item.vis.clone(),
1272 &impl_item.attrs,
1273 );
1274 }
1275 ast::ImplItemKind::Method(ref sig, ref body) => {
1276 self.process_method(
1277 sig,
1278 Some(body),
1279 impl_item.id,
1280 impl_item.ident,
1281 &impl_item.generics,
1282 impl_item.vis.clone(),
1283 impl_item.span,
1284 );
1285 }
1286 ast::ImplItemKind::Type(ref ty) => {
1287 // FIXME uses of the assoc type should ideally point to this
1288 // 'def' and the name here should be a ref to the def in the
1289 // trait.
1290 self.visit_ty(ty)
1291 }
1292 ast::ImplItemKind::Existential(ref bounds) => {
1293 // FIXME uses of the assoc type should ideally point to this
1294 // 'def' and the name here should be a ref to the def in the
1295 // trait.
1296 for bound in bounds.iter() {
1297 if let ast::GenericBound::Trait(trait_ref, _) = bound {
1298 self.process_path(trait_ref.trait_ref.ref_id, &trait_ref.trait_ref.path)
1299 }
1300 }
1301 }
1302 ast::ImplItemKind::Macro(_) => {}
1303 }
1304 }
1305
1306 /// Dumps imports in a use tree recursively.
1307 ///
1308 /// A use tree is an import that may contain nested braces (RFC 2128). The `use_tree` parameter
1309 /// is the current use tree under scrutiny, while `id` and `prefix` are its corresponding node
1310 /// id and path. `root_item` is the topmost use tree in the hierarchy.
1311 ///
1312 /// If `use_tree` is a simple or glob import, it is dumped into the analysis data. Otherwise,
1313 /// each child use tree is dumped recursively.
1314 fn process_use_tree(&mut self,
1315 use_tree: &'l ast::UseTree,
1316 id: NodeId,
1317 root_item: &'l ast::Item,
1318 prefix: &ast::Path) {
1319 let path = &use_tree.prefix;
1320
1321 // The access is calculated using the current tree ID, but with the root tree's visibility
1322 // (since nested trees don't have their own visibility).
1323 let access = access_from!(self.save_ctxt, root_item.vis, id);
1324
1325 // The parent def id of a given use tree is always the enclosing item.
1326 let parent = self.save_ctxt.tcx.hir.opt_local_def_id(id)
1327 .and_then(|id| self.save_ctxt.tcx.parent_def_id(id))
1328 .map(::id_from_def_id);
1329
1330 match use_tree.kind {
1331 ast::UseTreeKind::Simple(..) => {
1332 let ident = use_tree.ident();
1333 let path = ast::Path {
1334 segments: prefix.segments
1335 .iter()
1336 .chain(path.segments.iter())
1337 .cloned()
1338 .collect(),
1339 span: path.span,
1340 };
1341
1342 let sub_span = self.span.span_for_last_ident(path.span);
1343 let alias_span = self.span.sub_span_after_keyword(use_tree.span, keywords::As);
1344 let ref_id = self.lookup_def_id(id);
1345
1346 if !self.span.filter_generated(sub_span, path.span) {
1347 let span = self.span_from_span(sub_span.expect("No span found for use"));
1348 let alias_span = alias_span.map(|sp| self.span_from_span(sp));
1349 self.dumper.import(&access, Import {
1350 kind: ImportKind::Use,
1351 ref_id: ref_id.map(|id| ::id_from_def_id(id)),
1352 span,
1353 alias_span,
1354 name: ident.to_string(),
1355 value: String::new(),
1356 parent,
1357 });
1358 }
1359 self.write_sub_paths_truncated(&path);
1360 }
1361 ast::UseTreeKind::Glob => {
1362 let path = ast::Path {
1363 segments: prefix.segments
1364 .iter()
1365 .chain(path.segments.iter())
1366 .cloned()
1367 .collect(),
1368 span: path.span,
1369 };
1370
1371 // Make a comma-separated list of names of imported modules.
1372 let glob_map = &self.save_ctxt.analysis.glob_map;
1373 let glob_map = glob_map.as_ref().unwrap();
1374 let names = if glob_map.contains_key(&id) {
1375 glob_map.get(&id).unwrap().iter().map(|n| n.to_string()).collect()
1376 } else {
1377 Vec::new()
1378 };
1379
1380 let sub_span = self.span.sub_span_of_token(use_tree.span,
1381 token::BinOp(token::Star));
1382 if !self.span.filter_generated(sub_span, use_tree.span) {
1383 let span =
1384 self.span_from_span(sub_span.expect("No span found for use glob"));
1385 self.dumper.import(&access, Import {
1386 kind: ImportKind::GlobUse,
1387 ref_id: None,
1388 span,
1389 alias_span: None,
1390 name: "*".to_owned(),
1391 value: names.join(", "),
1392 parent,
1393 });
1394 }
1395 self.write_sub_paths(&path);
1396 }
1397 ast::UseTreeKind::Nested(ref nested_items) => {
1398 let prefix = ast::Path {
1399 segments: prefix.segments
1400 .iter()
1401 .chain(path.segments.iter())
1402 .cloned()
1403 .collect(),
1404 span: path.span,
1405 };
1406 for &(ref tree, id) in nested_items {
1407 self.process_use_tree(tree, id, root_item, &prefix);
1408 }
1409 }
1410 }
1411 }
1412
1413 fn process_bounds(&mut self, bounds: &'l ast::GenericBounds) {
1414 for bound in bounds {
1415 if let ast::GenericBound::Trait(ref trait_ref, _) = *bound {
1416 self.process_path(trait_ref.trait_ref.ref_id, &trait_ref.trait_ref.path)
1417 }
1418 }
1419 }
1420 }
1421
1422 impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll, O> {
1423 fn visit_mod(&mut self, m: &'l ast::Mod, span: Span, attrs: &[ast::Attribute], id: NodeId) {
1424 // Since we handle explicit modules ourselves in visit_item, this should
1425 // only get called for the root module of a crate.
1426 assert_eq!(id, ast::CRATE_NODE_ID);
1427
1428 let qualname = format!("::{}", self.tcx.node_path_str(id));
1429
1430 let cm = self.tcx.sess.source_map();
1431 let filename = cm.span_to_filename(span);
1432 let data_id = ::id_from_node_id(id, &self.save_ctxt);
1433 let children = m.items
1434 .iter()
1435 .map(|i| ::id_from_node_id(i.id, &self.save_ctxt))
1436 .collect();
1437 let span = self.span_from_span(span);
1438
1439 self.dumper.dump_def(
1440 &Access {
1441 public: true,
1442 reachable: true,
1443 },
1444 Def {
1445 kind: DefKind::Mod,
1446 id: data_id,
1447 name: String::new(),
1448 qualname,
1449 span,
1450 value: filename.to_string(),
1451 children,
1452 parent: None,
1453 decl_id: None,
1454 docs: self.save_ctxt.docs_for_attrs(attrs),
1455 sig: None,
1456 attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt),
1457 },
1458 );
1459 self.nest_scope(id, |v| visit::walk_mod(v, m));
1460 }
1461
1462 fn visit_item(&mut self, item: &'l ast::Item) {
1463 use syntax::ast::ItemKind::*;
1464 self.process_macro_use(item.span);
1465 match item.node {
1466 Use(ref use_tree) => {
1467 let prefix = ast::Path {
1468 segments: vec![],
1469 span: DUMMY_SP,
1470 };
1471 self.process_use_tree(use_tree, item.id, item, &prefix);
1472 }
1473 ExternCrate(_) => {
1474 let alias_span = self.span.span_for_last_ident(item.span);
1475
1476 if !self.span.filter_generated(alias_span, item.span) {
1477 let span =
1478 self.span_from_span(alias_span.expect("No span found for extern crate"));
1479 let parent = self.save_ctxt.tcx.hir.opt_local_def_id(item.id)
1480 .and_then(|id| self.save_ctxt.tcx.parent_def_id(id))
1481 .map(::id_from_def_id);
1482 self.dumper.import(
1483 &Access {
1484 public: false,
1485 reachable: false,
1486 },
1487 Import {
1488 kind: ImportKind::ExternCrate,
1489 ref_id: None,
1490 span,
1491 alias_span: None,
1492 name: item.ident.to_string(),
1493 value: String::new(),
1494 parent,
1495 },
1496 );
1497 }
1498 }
1499 Fn(ref decl, .., ref ty_params, ref body) => {
1500 self.process_fn(item, &decl, ty_params, &body)
1501 }
1502 Static(ref typ, _, ref expr) => self.process_static_or_const_item(item, typ, expr),
1503 Const(ref typ, ref expr) => self.process_static_or_const_item(item, &typ, &expr),
1504 Struct(ref def, ref ty_params) | Union(ref def, ref ty_params) => {
1505 self.process_struct(item, def, ty_params)
1506 }
1507 Enum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
1508 Impl(.., ref ty_params, ref trait_ref, ref typ, ref impl_items) => {
1509 self.process_impl(item, ty_params, trait_ref, &typ, impl_items)
1510 }
1511 Trait(_, _, ref generics, ref trait_refs, ref methods) => {
1512 self.process_trait(item, generics, trait_refs, methods)
1513 }
1514 Mod(ref m) => {
1515 self.process_mod(item);
1516 self.nest_scope(item.id, |v| visit::walk_mod(v, m));
1517 }
1518 Ty(ref ty, ref ty_params) => {
1519 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
1520 let value = ty_to_string(&ty);
1521 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Type);
1522 if !self.span.filter_generated(sub_span, item.span) {
1523 let span = self.span_from_span(sub_span.expect("No span found for typedef"));
1524 let id = ::id_from_node_id(item.id, &self.save_ctxt);
1525
1526 self.dumper.dump_def(
1527 &access_from!(self.save_ctxt, item),
1528 Def {
1529 kind: DefKind::Type,
1530 id,
1531 span,
1532 name: item.ident.to_string(),
1533 qualname: qualname.clone(),
1534 value,
1535 parent: None,
1536 children: vec![],
1537 decl_id: None,
1538 docs: self.save_ctxt.docs_for_attrs(&item.attrs),
1539 sig: sig::item_signature(item, &self.save_ctxt),
1540 attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt),
1541 },
1542 );
1543 }
1544
1545 self.visit_ty(&ty);
1546 self.process_generic_params(ty_params, item.span, &qualname, item.id);
1547 }
1548 Existential(ref _bounds, ref ty_params) => {
1549 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
1550 // FIXME do something with _bounds
1551 let value = String::new();
1552 let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Type);
1553 if !self.span.filter_generated(sub_span, item.span) {
1554 let span = self.span_from_span(sub_span.expect("No span found for typedef"));
1555 let id = ::id_from_node_id(item.id, &self.save_ctxt);
1556
1557 self.dumper.dump_def(
1558 &access_from!(self.save_ctxt, item),
1559 Def {
1560 kind: DefKind::Type,
1561 id,
1562 span,
1563 name: item.ident.to_string(),
1564 qualname: qualname.clone(),
1565 value,
1566 parent: None,
1567 children: vec![],
1568 decl_id: None,
1569 docs: self.save_ctxt.docs_for_attrs(&item.attrs),
1570 sig: sig::item_signature(item, &self.save_ctxt),
1571 attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt),
1572 },
1573 );
1574 }
1575
1576 self.process_generic_params(ty_params, item.span, &qualname, item.id);
1577 }
1578 Mac(_) => (),
1579 _ => visit::walk_item(self, item),
1580 }
1581 }
1582
1583 fn visit_generics(&mut self, generics: &'l ast::Generics) {
1584 for param in &generics.params {
1585 if let ast::GenericParamKind::Type { ref default, .. } = param.kind {
1586 self.process_bounds(&param.bounds);
1587 if let Some(ref ty) = default {
1588 self.visit_ty(&ty);
1589 }
1590 }
1591 }
1592 for pred in &generics.where_clause.predicates {
1593 if let ast::WherePredicate::BoundPredicate(ref wbp) = *pred {
1594 self.process_bounds(&wbp.bounds);
1595 self.visit_ty(&wbp.bounded_ty);
1596 }
1597 }
1598 }
1599
1600 fn visit_ty(&mut self, t: &'l ast::Ty) {
1601 self.process_macro_use(t.span);
1602 match t.node {
1603 ast::TyKind::Path(_, ref path) => {
1604 if generated_code(t.span) {
1605 return;
1606 }
1607
1608 if let Some(id) = self.lookup_def_id(t.id) {
1609 if let Some(sub_span) = self.span.sub_span_for_type_name(t.span) {
1610 let span = self.span_from_span(sub_span);
1611 self.dumper.dump_ref(Ref {
1612 kind: RefKind::Type,
1613 span,
1614 ref_id: ::id_from_def_id(id),
1615 });
1616 }
1617 }
1618
1619 self.write_sub_paths_truncated(path);
1620 visit::walk_path(self, path);
1621 }
1622 ast::TyKind::Array(ref element, ref length) => {
1623 self.visit_ty(element);
1624 self.nest_tables(length.id, |v| v.visit_expr(&length.value));
1625 }
1626 _ => visit::walk_ty(self, t),
1627 }
1628 }
1629
1630 fn visit_expr(&mut self, ex: &'l ast::Expr) {
1631 debug!("visit_expr {:?}", ex.node);
1632 self.process_macro_use(ex.span);
1633 match ex.node {
1634 ast::ExprKind::Struct(ref path, ref fields, ref base) => {
1635 let hir_expr = self.save_ctxt.tcx.hir.expect_expr(ex.id);
1636 let adt = match self.save_ctxt.tables.expr_ty_opt(&hir_expr) {
1637 Some(ty) if ty.ty_adt_def().is_some() => ty.ty_adt_def().unwrap(),
1638 _ => {
1639 visit::walk_expr(self, ex);
1640 return;
1641 }
1642 };
1643 let def = self.save_ctxt.get_path_def(hir_expr.id);
1644 self.process_struct_lit(ex, path, fields, adt.variant_of_def(def), base)
1645 }
1646 ast::ExprKind::MethodCall(ref seg, ref args) => self.process_method_call(ex, seg, args),
1647 ast::ExprKind::Field(ref sub_ex, _) => {
1648 self.visit_expr(&sub_ex);
1649
1650 if let Some(field_data) = self.save_ctxt.get_expr_data(ex) {
1651 down_cast_data!(field_data, RefData, ex.span);
1652 if !generated_code(ex.span) {
1653 self.dumper.dump_ref(field_data);
1654 }
1655 }
1656 }
1657 ast::ExprKind::Closure(_, _, _, ref decl, ref body, _fn_decl_span) => {
1658 let id = format!("${}", ex.id);
1659
1660 // walk arg and return types
1661 for arg in &decl.inputs {
1662 self.visit_ty(&arg.ty);
1663 }
1664
1665 if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output {
1666 self.visit_ty(&ret_ty);
1667 }
1668
1669 // walk the body
1670 self.nest_tables(ex.id, |v| {
1671 v.process_formals(&decl.inputs, &id);
1672 v.nest_scope(ex.id, |v| v.visit_expr(body))
1673 });
1674 }
1675 ast::ExprKind::ForLoop(ref pattern, ref subexpression, ref block, _) => {
1676 let value = self.span.snippet(subexpression.span);
1677 self.process_var_decl(pattern, value);
1678 debug!("for loop, walk sub-expr: {:?}", subexpression.node);
1679 self.visit_expr(subexpression);
1680 visit::walk_block(self, block);
1681 }
1682 ast::ExprKind::WhileLet(ref pats, ref subexpression, ref block, _) => {
1683 self.process_var_decl_multi(pats);
1684 debug!("for loop, walk sub-expr: {:?}", subexpression.node);
1685 self.visit_expr(subexpression);
1686 visit::walk_block(self, block);
1687 }
1688 ast::ExprKind::IfLet(ref pats, ref subexpression, ref block, ref opt_else) => {
1689 self.process_var_decl_multi(pats);
1690 self.visit_expr(subexpression);
1691 visit::walk_block(self, block);
1692 opt_else.as_ref().map(|el| self.visit_expr(el));
1693 }
1694 ast::ExprKind::Repeat(ref element, ref count) => {
1695 self.visit_expr(element);
1696 self.nest_tables(count.id, |v| v.visit_expr(&count.value));
1697 }
1698 // In particular, we take this branch for call and path expressions,
1699 // where we'll index the idents involved just by continuing to walk.
1700 _ => visit::walk_expr(self, ex),
1701 }
1702 }
1703
1704 fn visit_mac(&mut self, mac: &'l ast::Mac) {
1705 // These shouldn't exist in the AST at this point, log a span bug.
1706 span_bug!(
1707 mac.span,
1708 "macro invocation should have been expanded out of AST"
1709 );
1710 }
1711
1712 fn visit_pat(&mut self, p: &'l ast::Pat) {
1713 self.process_macro_use(p.span);
1714 self.process_pat(p);
1715 }
1716
1717 fn visit_arm(&mut self, arm: &'l ast::Arm) {
1718 self.process_var_decl_multi(&arm.pats);
1719 match arm.guard {
1720 Some(ast::Guard::If(ref expr)) => self.visit_expr(expr),
1721 _ => {}
1722 }
1723 self.visit_expr(&arm.body);
1724 }
1725
1726 fn visit_path(&mut self, p: &'l ast::Path, id: NodeId) {
1727 self.process_path(id, p);
1728 }
1729
1730 fn visit_stmt(&mut self, s: &'l ast::Stmt) {
1731 self.process_macro_use(s.span);
1732 visit::walk_stmt(self, s)
1733 }
1734
1735 fn visit_local(&mut self, l: &'l ast::Local) {
1736 self.process_macro_use(l.span);
1737 let value = l.init
1738 .as_ref()
1739 .map(|i| self.span.snippet(i.span))
1740 .unwrap_or_default();
1741 self.process_var_decl(&l.pat, value);
1742
1743 // Just walk the initialiser and type (don't want to walk the pattern again).
1744 walk_list!(self, visit_ty, &l.ty);
1745 walk_list!(self, visit_expr, &l.init);
1746 }
1747
1748 fn visit_foreign_item(&mut self, item: &'l ast::ForeignItem) {
1749 let access = access_from!(self.save_ctxt, item);
1750
1751 match item.node {
1752 ast::ForeignItemKind::Fn(ref decl, ref generics) => {
1753 if let Some(fn_data) = self.save_ctxt.get_extern_item_data(item) {
1754 down_cast_data!(fn_data, DefData, item.span);
1755
1756 self.nest_tables(
1757 item.id,
1758 |v| v.process_formals(&decl.inputs, &fn_data.qualname),
1759 );
1760 self.process_generic_params(generics, item.span, &fn_data.qualname, item.id);
1761 self.dumper.dump_def(&access, fn_data);
1762 }
1763
1764 for arg in &decl.inputs {
1765 self.visit_ty(&arg.ty);
1766 }
1767
1768 if let ast::FunctionRetTy::Ty(ref ret_ty) = decl.output {
1769 self.visit_ty(&ret_ty);
1770 }
1771 }
1772 ast::ForeignItemKind::Static(ref ty, _) => {
1773 if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) {
1774 down_cast_data!(var_data, DefData, item.span);
1775 self.dumper.dump_def(&access, var_data);
1776 }
1777
1778 self.visit_ty(ty);
1779 }
1780 ast::ForeignItemKind::Ty => {
1781 if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) {
1782 down_cast_data!(var_data, DefData, item.span);
1783 self.dumper.dump_def(&access, var_data);
1784 }
1785 }
1786 ast::ForeignItemKind::Macro(..) => {}
1787 }
1788 }
1789 }