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