]> git.lizzy.rs Git - rust.git/blob - src/librustc_save_analysis/lib.rs
Auto merge of #51717 - Mark-Simulacrum:snap, r=alexcrichton
[rust.git] / src / librustc_save_analysis / lib.rs
1 // Copyright 2012-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 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
12        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
13        html_root_url = "https://doc.rust-lang.org/nightly/")]
14 #![feature(custom_attribute)]
15 #![allow(unused_attributes)]
16
17 #![recursion_limit="256"]
18
19 #[macro_use]
20 extern crate rustc;
21
22 #[macro_use]
23 extern crate log;
24 extern crate rustc_data_structures;
25 extern crate rustc_serialize;
26 extern crate rustc_target;
27 extern crate rustc_typeck;
28 #[macro_use]
29 extern crate syntax;
30 extern crate syntax_pos;
31
32 extern crate rls_data;
33 extern crate rls_span;
34
35
36 mod json_dumper;
37 mod dump_visitor;
38 #[macro_use]
39 mod span_utils;
40 mod sig;
41
42 use rustc::hir;
43 use rustc::hir::def::Def as HirDef;
44 use rustc::hir::map::{Node, NodeTraitItem, NodeImplItem};
45 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
46 use rustc::middle::cstore::ExternCrate;
47 use rustc::session::config::CrateType::CrateTypeExecutable;
48 use rustc::ty::{self, TyCtxt};
49 use rustc_typeck::hir_ty_to_ty;
50
51 use std::cell::Cell;
52 use std::default::Default;
53 use std::env;
54 use std::fs::File;
55 use std::path::{Path, PathBuf};
56
57 use syntax::ast::{self, Attribute, NodeId, PatKind};
58 use syntax::parse::lexer::comments::strip_doc_comment_decoration;
59 use syntax::parse::token;
60 use syntax::print::pprust;
61 use syntax::symbol::keywords;
62 use syntax::visit::{self, Visitor};
63 use syntax::print::pprust::{arg_to_string, ty_to_string};
64 use syntax::codemap::MacroAttribute;
65 use syntax_pos::*;
66
67 use json_dumper::JsonDumper;
68 use dump_visitor::DumpVisitor;
69 use span_utils::SpanUtils;
70
71 use rls_data::{Def, DefKind, ExternalCrateData, GlobalCrateId, MacroRef, Ref, RefKind, Relation,
72                RelationKind, SpanData, Impl, ImplKind};
73 use rls_data::config::Config;
74
75
76 pub struct SaveContext<'l, 'tcx: 'l> {
77     tcx: TyCtxt<'l, 'tcx, 'tcx>,
78     tables: &'l ty::TypeckTables<'tcx>,
79     analysis: &'l ty::CrateAnalysis,
80     span_utils: SpanUtils<'tcx>,
81     config: Config,
82     impl_counter: Cell<u32>,
83 }
84
85 #[derive(Debug)]
86 pub enum Data {
87     RefData(Ref),
88     DefData(Def),
89     RelationData(Relation, Impl),
90 }
91
92 impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
93     fn span_from_span(&self, span: Span) -> SpanData {
94         use rls_span::{Column, Row};
95
96         let cm = self.tcx.sess.codemap();
97         let start = cm.lookup_char_pos(span.lo());
98         let end = cm.lookup_char_pos(span.hi());
99
100         SpanData {
101             file_name: start.file.name.clone().to_string().into(),
102             byte_start: span.lo().0,
103             byte_end: span.hi().0,
104             line_start: Row::new_one_indexed(start.line as u32),
105             line_end: Row::new_one_indexed(end.line as u32),
106             column_start: Column::new_one_indexed(start.col.0 as u32 + 1),
107             column_end: Column::new_one_indexed(end.col.0 as u32 + 1),
108         }
109     }
110
111     // List external crates used by the current crate.
112     pub fn get_external_crates(&self) -> Vec<ExternalCrateData> {
113         let mut result = Vec::new();
114
115         for &n in self.tcx.crates().iter() {
116             let span = match *self.tcx.extern_crate(n.as_def_id()) {
117                 Some(ExternCrate { span, .. }) => span,
118                 None => {
119                     debug!("Skipping crate {}, no data", n);
120                     continue;
121                 }
122             };
123             let lo_loc = self.span_utils.sess.codemap().lookup_char_pos(span.lo());
124             result.push(ExternalCrateData {
125                 // FIXME: change file_name field to PathBuf in rls-data
126                 // https://github.com/nrc/rls-data/issues/7
127                 file_name: SpanUtils::make_path_string(&lo_loc.file.name),
128                 num: n.as_u32(),
129                 id: GlobalCrateId {
130                     name: self.tcx.crate_name(n).to_string(),
131                     disambiguator: self.tcx.crate_disambiguator(n).to_fingerprint().as_value(),
132                 },
133             });
134         }
135
136         result
137     }
138
139     pub fn get_extern_item_data(&self, item: &ast::ForeignItem) -> Option<Data> {
140         let qualname = format!("::{}", self.tcx.node_path_str(item.id));
141         match item.node {
142             ast::ForeignItemKind::Fn(ref decl, ref generics) => {
143                 let sub_span = self.span_utils
144                     .sub_span_after_keyword(item.span, keywords::Fn);
145                 filter!(self.span_utils, sub_span, item.span, None);
146
147                 Some(Data::DefData(Def {
148                     kind: DefKind::Function,
149                     id: id_from_node_id(item.id, self),
150                     span: self.span_from_span(sub_span.unwrap()),
151                     name: item.ident.to_string(),
152                     qualname,
153                     value: make_signature(decl, generics),
154                     parent: None,
155                     children: vec![],
156                     decl_id: None,
157                     docs: self.docs_for_attrs(&item.attrs),
158                     sig: sig::foreign_item_signature(item, self),
159                     attributes: lower_attributes(item.attrs.clone(), self),
160                 }))
161             }
162             ast::ForeignItemKind::Static(ref ty, m) => {
163                 let keyword = if m { keywords::Mut } else { keywords::Static };
164                 let sub_span = self.span_utils.sub_span_after_keyword(item.span, keyword);
165                 filter!(self.span_utils, sub_span, item.span, None);
166
167                 let id = ::id_from_node_id(item.id, self);
168                 let span = self.span_from_span(sub_span.unwrap());
169
170                 Some(Data::DefData(Def {
171                     kind: DefKind::Static,
172                     id,
173                     span,
174                     name: item.ident.to_string(),
175                     qualname,
176                     value: ty_to_string(ty),
177                     parent: None,
178                     children: vec![],
179                     decl_id: None,
180                     docs: self.docs_for_attrs(&item.attrs),
181                     sig: sig::foreign_item_signature(item, self),
182                     attributes: lower_attributes(item.attrs.clone(), self),
183                 }))
184             }
185             // FIXME(plietar): needs a new DefKind in rls-data
186             ast::ForeignItemKind::Ty => None,
187             ast::ForeignItemKind::Macro(..) => None,
188         }
189     }
190
191     pub fn get_item_data(&self, item: &ast::Item) -> Option<Data> {
192         match item.node {
193             ast::ItemKind::Fn(ref decl, .., ref generics, _) => {
194                 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
195                 let sub_span = self.span_utils
196                     .sub_span_after_keyword(item.span, keywords::Fn);
197                 filter!(self.span_utils, sub_span, item.span, None);
198                 Some(Data::DefData(Def {
199                     kind: DefKind::Function,
200                     id: id_from_node_id(item.id, self),
201                     span: self.span_from_span(sub_span.unwrap()),
202                     name: item.ident.to_string(),
203                     qualname,
204                     value: make_signature(decl, generics),
205                     parent: None,
206                     children: vec![],
207                     decl_id: None,
208                     docs: self.docs_for_attrs(&item.attrs),
209                     sig: sig::item_signature(item, self),
210                     attributes: lower_attributes(item.attrs.clone(), self),
211                 }))
212             }
213             ast::ItemKind::Static(ref typ, mt, _) => {
214                 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
215
216                 let keyword = match mt {
217                     ast::Mutability::Mutable => keywords::Mut,
218                     ast::Mutability::Immutable => keywords::Static,
219                 };
220
221                 let sub_span = self.span_utils.sub_span_after_keyword(item.span, keyword);
222                 filter!(self.span_utils, sub_span, item.span, None);
223
224                 let id = id_from_node_id(item.id, self);
225                 let span = self.span_from_span(sub_span.unwrap());
226
227                 Some(Data::DefData(Def {
228                     kind: DefKind::Static,
229                     id,
230                     span,
231                     name: item.ident.to_string(),
232                     qualname,
233                     value: ty_to_string(&typ),
234                     parent: None,
235                     children: vec![],
236                     decl_id: None,
237                     docs: self.docs_for_attrs(&item.attrs),
238                     sig: sig::item_signature(item, self),
239                     attributes: lower_attributes(item.attrs.clone(), self),
240                 }))
241             }
242             ast::ItemKind::Const(ref typ, _) => {
243                 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
244                 let sub_span = self.span_utils
245                     .sub_span_after_keyword(item.span, keywords::Const);
246                 filter!(self.span_utils, sub_span, item.span, None);
247
248                 let id = id_from_node_id(item.id, self);
249                 let span = self.span_from_span(sub_span.unwrap());
250
251                 Some(Data::DefData(Def {
252                     kind: DefKind::Const,
253                     id,
254                     span,
255                     name: item.ident.to_string(),
256                     qualname,
257                     value: ty_to_string(typ),
258                     parent: None,
259                     children: vec![],
260                     decl_id: None,
261                     docs: self.docs_for_attrs(&item.attrs),
262                     sig: sig::item_signature(item, self),
263                     attributes: lower_attributes(item.attrs.clone(), self),
264                 }))
265             }
266             ast::ItemKind::Mod(ref m) => {
267                 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
268
269                 let cm = self.tcx.sess.codemap();
270                 let filename = cm.span_to_filename(m.inner);
271
272                 let sub_span = self.span_utils
273                     .sub_span_after_keyword(item.span, keywords::Mod);
274                 filter!(self.span_utils, sub_span, item.span, None);
275
276                 Some(Data::DefData(Def {
277                     kind: DefKind::Mod,
278                     id: id_from_node_id(item.id, self),
279                     name: item.ident.to_string(),
280                     qualname,
281                     span: self.span_from_span(sub_span.unwrap()),
282                     value: filename.to_string(),
283                     parent: None,
284                     children: m.items
285                         .iter()
286                         .map(|i| id_from_node_id(i.id, self))
287                         .collect(),
288                     decl_id: None,
289                     docs: self.docs_for_attrs(&item.attrs),
290                     sig: sig::item_signature(item, self),
291                     attributes: lower_attributes(item.attrs.clone(), self),
292                 }))
293             }
294             ast::ItemKind::Enum(ref def, _) => {
295                 let name = item.ident.to_string();
296                 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
297                 let sub_span = self.span_utils
298                     .sub_span_after_keyword(item.span, keywords::Enum);
299                 filter!(self.span_utils, sub_span, item.span, None);
300                 let variants_str = def.variants
301                     .iter()
302                     .map(|v| v.node.ident.to_string())
303                     .collect::<Vec<_>>()
304                     .join(", ");
305                 let value = format!("{}::{{{}}}", name, variants_str);
306                 Some(Data::DefData(Def {
307                     kind: DefKind::Enum,
308                     id: id_from_node_id(item.id, self),
309                     span: self.span_from_span(sub_span.unwrap()),
310                     name,
311                     qualname,
312                     value,
313                     parent: None,
314                     children: def.variants
315                         .iter()
316                         .map(|v| id_from_node_id(v.node.data.id(), self))
317                         .collect(),
318                     decl_id: None,
319                     docs: self.docs_for_attrs(&item.attrs),
320                     sig: sig::item_signature(item, self),
321                     attributes: lower_attributes(item.attrs.to_owned(), self),
322                 }))
323             }
324             ast::ItemKind::Impl(.., ref trait_ref, ref typ, ref impls) => {
325                 if let ast::TyKind::Path(None, ref path) = typ.node {
326                     // Common case impl for a struct or something basic.
327                     if generated_code(path.span) {
328                         return None;
329                     }
330                     let sub_span = self.span_utils.sub_span_for_type_name(path.span);
331                     filter!(self.span_utils, sub_span, typ.span, None);
332
333                     let impl_id = self.next_impl_id();
334                     let span = self.span_from_span(sub_span.unwrap());
335
336                     let type_data = self.lookup_ref_id(typ.id);
337                     type_data.map(|type_data| {
338                         Data::RelationData(Relation {
339                             kind: RelationKind::Impl {
340                                 id: impl_id,
341                             },
342                             span: span.clone(),
343                             from: id_from_def_id(type_data),
344                             to: trait_ref
345                                 .as_ref()
346                                 .and_then(|t| self.lookup_ref_id(t.ref_id))
347                                 .map(id_from_def_id)
348                                 .unwrap_or(null_id()),
349                         },
350                         Impl {
351                             id: impl_id,
352                             kind: match *trait_ref {
353                                 Some(_) => ImplKind::Direct,
354                                 None => ImplKind::Inherent,
355                             },
356                             span: span,
357                             value: String::new(),
358                             parent: None,
359                             children: impls
360                                 .iter()
361                                 .map(|i| id_from_node_id(i.id, self))
362                                 .collect(),
363                             docs: String::new(),
364                             sig: None,
365                             attributes: vec![],
366                         })
367                     })
368                 } else {
369                     None
370                 }
371             }
372             _ => {
373                 // FIXME
374                 bug!();
375             }
376         }
377     }
378
379     pub fn get_field_data(&self, field: &ast::StructField, scope: NodeId) -> Option<Def> {
380         if let Some(ident) = field.ident {
381             let name = ident.to_string();
382             let qualname = format!("::{}::{}", self.tcx.node_path_str(scope), ident);
383             let sub_span = self.span_utils
384                 .sub_span_before_token(field.span, token::Colon);
385             filter!(self.span_utils, sub_span, field.span, None);
386             let def_id = self.tcx.hir.local_def_id(field.id);
387             let typ = self.tcx.type_of(def_id).to_string();
388
389
390             let id = id_from_node_id(field.id, self);
391             let span = self.span_from_span(sub_span.unwrap());
392
393             Some(Def {
394                 kind: DefKind::Field,
395                 id,
396                 span,
397                 name,
398                 qualname,
399                 value: typ,
400                 parent: Some(id_from_node_id(scope, self)),
401                 children: vec![],
402                 decl_id: None,
403                 docs: self.docs_for_attrs(&field.attrs),
404                 sig: sig::field_signature(field, self),
405                 attributes: lower_attributes(field.attrs.clone(), self),
406             })
407         } else {
408             None
409         }
410     }
411
412     // FIXME would be nice to take a MethodItem here, but the ast provides both
413     // trait and impl flavours, so the caller must do the disassembly.
414     pub fn get_method_data(&self, id: ast::NodeId, name: ast::Name, span: Span) -> Option<Def> {
415         // The qualname for a method is the trait name or name of the struct in an impl in
416         // which the method is declared in, followed by the method's name.
417         let (qualname, parent_scope, decl_id, docs, attributes) =
418             match self.tcx.impl_of_method(self.tcx.hir.local_def_id(id)) {
419                 Some(impl_id) => match self.tcx.hir.get_if_local(impl_id) {
420                     Some(Node::NodeItem(item)) => match item.node {
421                         hir::ItemImpl(.., ref ty, _) => {
422                             let mut qualname = String::from("<");
423                             qualname.push_str(&self.tcx.hir.node_to_pretty_string(ty.id));
424
425                             let mut trait_id = self.tcx.trait_id_of_impl(impl_id);
426                             let mut decl_id = None;
427                             let mut docs = String::new();
428                             let mut attrs = vec![];
429                             if let Some(NodeImplItem(item)) = self.tcx.hir.find(id) {
430                                 docs = self.docs_for_attrs(&item.attrs);
431                                 attrs = item.attrs.to_vec();
432                             }
433
434                             if let Some(def_id) = trait_id {
435                                 // A method in a trait impl.
436                                 qualname.push_str(" as ");
437                                 qualname.push_str(&self.tcx.item_path_str(def_id));
438                                 self.tcx
439                                     .associated_items(def_id)
440                                     .find(|item| item.ident.name == name)
441                                     .map(|item| decl_id = Some(item.def_id));
442                             }
443                             qualname.push_str(">");
444
445                             (qualname, trait_id, decl_id, docs, attrs)
446                         }
447                         _ => {
448                             span_bug!(
449                                 span,
450                                 "Container {:?} for method {} not an impl?",
451                                 impl_id,
452                                 id
453                             );
454                         }
455                     },
456                     r => {
457                         span_bug!(
458                             span,
459                             "Container {:?} for method {} is not a node item {:?}",
460                             impl_id,
461                             id,
462                             r
463                         );
464                     }
465                 },
466                 None => match self.tcx.trait_of_item(self.tcx.hir.local_def_id(id)) {
467                     Some(def_id) => {
468                         let mut docs = String::new();
469                         let mut attrs = vec![];
470
471                         if let Some(NodeTraitItem(item)) = self.tcx.hir.find(id) {
472                             docs = self.docs_for_attrs(&item.attrs);
473                             attrs = item.attrs.to_vec();
474                         }
475
476                         (
477                             format!("::{}", self.tcx.item_path_str(def_id)),
478                             Some(def_id),
479                             None,
480                             docs,
481                             attrs,
482                         )
483                     }
484                     None => {
485                         debug!("Could not find container for method {} at {:?}", id, span);
486                         // This is not necessarily a bug, if there was a compilation error,
487                         // the tables we need might not exist.
488                         return None;
489                     }
490                 },
491             };
492
493         let qualname = format!("{}::{}", qualname, name);
494
495         let sub_span = self.span_utils.sub_span_after_keyword(span, keywords::Fn);
496         filter!(self.span_utils, sub_span, span, None);
497
498         Some(Def {
499             kind: DefKind::Method,
500             id: id_from_node_id(id, self),
501             span: self.span_from_span(sub_span.unwrap()),
502             name: name.to_string(),
503             qualname,
504             // FIXME you get better data here by using the visitor.
505             value: String::new(),
506             parent: parent_scope.map(|id| id_from_def_id(id)),
507             children: vec![],
508             decl_id: decl_id.map(|id| id_from_def_id(id)),
509             docs,
510             sig: None,
511             attributes: lower_attributes(attributes, self),
512         })
513     }
514
515     pub fn get_trait_ref_data(&self, trait_ref: &ast::TraitRef) -> Option<Ref> {
516         self.lookup_ref_id(trait_ref.ref_id).and_then(|def_id| {
517             let span = trait_ref.path.span;
518             if generated_code(span) {
519                 return None;
520             }
521             let sub_span = self.span_utils.sub_span_for_type_name(span).or(Some(span));
522             filter!(self.span_utils, sub_span, span, None);
523             let span = self.span_from_span(sub_span.unwrap());
524             Some(Ref {
525                 kind: RefKind::Type,
526                 span,
527                 ref_id: id_from_def_id(def_id),
528             })
529         })
530     }
531
532     pub fn get_expr_data(&self, expr: &ast::Expr) -> Option<Data> {
533         let hir_node = self.tcx.hir.expect_expr(expr.id);
534         let ty = self.tables.expr_ty_adjusted_opt(&hir_node);
535         if ty.is_none() || ty.unwrap().sty == ty::TyError {
536             return None;
537         }
538         match expr.node {
539             ast::ExprKind::Field(ref sub_ex, ident) => {
540                 let hir_node = match self.tcx.hir.find(sub_ex.id) {
541                     Some(Node::NodeExpr(expr)) => expr,
542                     _ => {
543                         debug!(
544                             "Missing or weird node for sub-expression {} in {:?}",
545                             sub_ex.id,
546                             expr
547                         );
548                         return None;
549                     }
550                 };
551                 match self.tables.expr_ty_adjusted(&hir_node).sty {
552                     ty::TyAdt(def, _) if !def.is_enum() => {
553                         let variant = &def.non_enum_variant();
554                         let index = self.tcx.find_field_index(ident, variant).unwrap();
555                         let sub_span = self.span_utils.span_for_last_ident(expr.span);
556                         filter!(self.span_utils, sub_span, expr.span, None);
557                         let span = self.span_from_span(sub_span.unwrap());
558                         return Some(Data::RefData(Ref {
559                             kind: RefKind::Variable,
560                             span,
561                             ref_id: id_from_def_id(variant.fields[index].did),
562                         }));
563                     }
564                     ty::TyTuple(..) => None,
565                     _ => {
566                         debug!("Expected struct or union type, found {:?}", ty);
567                         None
568                     }
569                 }
570             }
571             ast::ExprKind::Struct(ref path, ..) => {
572                 match self.tables.expr_ty_adjusted(&hir_node).sty {
573                     ty::TyAdt(def, _) if !def.is_enum() => {
574                         let sub_span = self.span_utils.span_for_last_ident(path.span);
575                         filter!(self.span_utils, sub_span, path.span, None);
576                         let span = self.span_from_span(sub_span.unwrap());
577                         Some(Data::RefData(Ref {
578                             kind: RefKind::Type,
579                             span,
580                             ref_id: id_from_def_id(def.did),
581                         }))
582                     }
583                     _ => {
584                         // FIXME ty could legitimately be an enum, but then we will fail
585                         // later if we try to look up the fields.
586                         debug!("expected struct or union, found {:?}", ty);
587                         None
588                     }
589                 }
590             }
591             ast::ExprKind::MethodCall(ref seg, ..) => {
592                 let expr_hir_id = self.tcx.hir.definitions().node_to_hir_id(expr.id);
593                 let method_id = match self.tables.type_dependent_defs().get(expr_hir_id) {
594                     Some(id) => id.def_id(),
595                     None => {
596                         debug!("Could not resolve method id for {:?}", expr);
597                         return None;
598                     }
599                 };
600                 let (def_id, decl_id) = match self.tcx.associated_item(method_id).container {
601                     ty::ImplContainer(_) => (Some(method_id), None),
602                     ty::TraitContainer(_) => (None, Some(method_id)),
603                 };
604                 let sub_span = seg.ident.span;
605                 filter!(self.span_utils, Some(sub_span), expr.span, None);
606                 let span = self.span_from_span(sub_span);
607                 Some(Data::RefData(Ref {
608                     kind: RefKind::Function,
609                     span,
610                     ref_id: def_id
611                         .or(decl_id)
612                         .map(|id| id_from_def_id(id))
613                         .unwrap_or(null_id()),
614                 }))
615             }
616             ast::ExprKind::Path(_, ref path) => {
617                 self.get_path_data(expr.id, path).map(|d| Data::RefData(d))
618             }
619             _ => {
620                 // FIXME
621                 bug!();
622             }
623         }
624     }
625
626     pub fn get_path_def(&self, id: NodeId) -> HirDef {
627         match self.tcx.hir.get(id) {
628             Node::NodeTraitRef(tr) => tr.path.def,
629
630             Node::NodeItem(&hir::Item {
631                 node: hir::ItemUse(ref path, _),
632                 ..
633             }) |
634             Node::NodeVisibility(&hir::Visibility::Restricted { ref path, .. }) => path.def,
635
636             Node::NodeExpr(&hir::Expr {
637                 node: hir::ExprStruct(ref qpath, ..),
638                 ..
639             }) |
640             Node::NodeExpr(&hir::Expr {
641                 node: hir::ExprPath(ref qpath),
642                 ..
643             }) |
644             Node::NodePat(&hir::Pat {
645                 node: hir::PatKind::Path(ref qpath),
646                 ..
647             }) |
648             Node::NodePat(&hir::Pat {
649                 node: hir::PatKind::Struct(ref qpath, ..),
650                 ..
651             }) |
652             Node::NodePat(&hir::Pat {
653                 node: hir::PatKind::TupleStruct(ref qpath, ..),
654                 ..
655             }) => {
656                 let hir_id = self.tcx.hir.node_to_hir_id(id);
657                 self.tables.qpath_def(qpath, hir_id)
658             }
659
660             Node::NodeBinding(&hir::Pat {
661                 node: hir::PatKind::Binding(_, canonical_id, ..),
662                 ..
663             }) => HirDef::Local(canonical_id),
664
665             Node::NodeTy(ty) => if let hir::Ty {
666                 node: hir::TyPath(ref qpath),
667                 ..
668             } = *ty
669             {
670                 match *qpath {
671                     hir::QPath::Resolved(_, ref path) => path.def,
672                     hir::QPath::TypeRelative(..) => {
673                         let ty = hir_ty_to_ty(self.tcx, ty);
674                         if let ty::TyProjection(proj) = ty.sty {
675                             return HirDef::AssociatedTy(proj.item_def_id);
676                         }
677                         HirDef::Err
678                     }
679                 }
680             } else {
681                 HirDef::Err
682             },
683
684             _ => HirDef::Err,
685         }
686     }
687
688     pub fn get_path_data(&self, id: NodeId, path: &ast::Path) -> Option<Ref> {
689         // Returns true if the path is function type sugar, e.g., `Fn(A) -> B`.
690         fn fn_type(path: &ast::Path) -> bool {
691             if path.segments.len() != 1 {
692                 return false;
693             }
694             if let Some(ref generic_args) = path.segments[0].args {
695                 if let ast::GenericArgs::Parenthesized(_) = **generic_args {
696                     return true;
697                 }
698             }
699             false
700         }
701
702         if path.segments.is_empty() {
703             return None;
704         }
705
706         let def = self.get_path_def(id);
707         let last_seg = &path.segments[path.segments.len() - 1];
708         let sub_span = last_seg.ident.span;
709         filter!(self.span_utils, Some(sub_span), path.span, None);
710         match def {
711             HirDef::Upvar(id, ..) | HirDef::Local(id) => {
712                 let span = self.span_from_span(sub_span);
713                 Some(Ref {
714                     kind: RefKind::Variable,
715                     span,
716                     ref_id: id_from_node_id(id, self),
717                 })
718             }
719             HirDef::Static(..) |
720             HirDef::Const(..) |
721             HirDef::AssociatedConst(..) |
722             HirDef::VariantCtor(..) => {
723                 let span = self.span_from_span(sub_span);
724                 Some(Ref {
725                     kind: RefKind::Variable,
726                     span,
727                     ref_id: id_from_def_id(def.def_id()),
728                 })
729             }
730             HirDef::Trait(def_id) if fn_type(path) => {
731                 // Function type bounds are desugared in the parser, so we have to
732                 // special case them here.
733                 let fn_span = self.span_utils.span_for_first_ident(path.span);
734                 fn_span.map(|span| {
735                     Ref {
736                         kind: RefKind::Type,
737                         span: self.span_from_span(span),
738                         ref_id: id_from_def_id(def_id),
739                     }
740                 })
741             }
742             HirDef::Struct(def_id) |
743             HirDef::Variant(def_id, ..) |
744             HirDef::Union(def_id) |
745             HirDef::Enum(def_id) |
746             HirDef::TyAlias(def_id) |
747             HirDef::TyForeign(def_id) |
748             HirDef::TraitAlias(def_id) |
749             HirDef::AssociatedTy(def_id) |
750             HirDef::Trait(def_id) |
751             HirDef::Existential(def_id) |
752             HirDef::TyParam(def_id) => {
753                 let span = self.span_from_span(sub_span);
754                 Some(Ref {
755                     kind: RefKind::Type,
756                     span,
757                     ref_id: id_from_def_id(def_id),
758                 })
759             }
760             HirDef::StructCtor(def_id, _) => {
761                 // This is a reference to a tuple struct where the def_id points
762                 // to an invisible constructor function. That is not a very useful
763                 // def, so adjust to point to the tuple struct itself.
764                 let span = self.span_from_span(sub_span);
765                 let parent_def_id = self.tcx.parent_def_id(def_id).unwrap();
766                 Some(Ref {
767                     kind: RefKind::Type,
768                     span,
769                     ref_id: id_from_def_id(parent_def_id),
770                 })
771             }
772             HirDef::Method(decl_id) => {
773                 let def_id = if decl_id.is_local() {
774                     let ti = self.tcx.associated_item(decl_id);
775                     self.tcx
776                         .associated_items(ti.container.id())
777                         .find(|item| item.ident.name == ti.ident.name &&
778                                      item.defaultness.has_value())
779                         .map(|item| item.def_id)
780                 } else {
781                     None
782                 };
783                 let span = self.span_from_span(sub_span);
784                 Some(Ref {
785                     kind: RefKind::Function,
786                     span,
787                     ref_id: id_from_def_id(def_id.unwrap_or(decl_id)),
788                 })
789             }
790             HirDef::Fn(def_id) => {
791                 let span = self.span_from_span(sub_span);
792                 Some(Ref {
793                     kind: RefKind::Function,
794                     span,
795                     ref_id: id_from_def_id(def_id),
796                 })
797             }
798             HirDef::Mod(def_id) => {
799                 let span = self.span_from_span(sub_span);
800                 Some(Ref {
801                     kind: RefKind::Mod,
802                     span,
803                     ref_id: id_from_def_id(def_id),
804                 })
805             }
806             HirDef::PrimTy(..) |
807             HirDef::SelfTy(..) |
808             HirDef::Label(..) |
809             HirDef::Macro(..) |
810             HirDef::GlobalAsm(..) |
811             HirDef::Err => None,
812         }
813     }
814
815     pub fn get_field_ref_data(
816         &self,
817         field_ref: &ast::Field,
818         variant: &ty::VariantDef,
819     ) -> Option<Ref> {
820         let index = self.tcx.find_field_index(field_ref.ident, variant).unwrap();
821         // We don't really need a sub-span here, but no harm done
822         let sub_span = self.span_utils.span_for_last_ident(field_ref.ident.span);
823         filter!(self.span_utils, sub_span, field_ref.ident.span, None);
824         let span = self.span_from_span(sub_span.unwrap());
825         Some(Ref {
826             kind: RefKind::Variable,
827             span,
828             ref_id: id_from_def_id(variant.fields[index].did),
829         })
830     }
831
832     /// Attempt to return MacroRef for any AST node.
833     ///
834     /// For a given piece of AST defined by the supplied Span and NodeId,
835     /// returns None if the node is not macro-generated or the span is malformed,
836     /// else uses the expansion callsite and callee to return some MacroRef.
837     pub fn get_macro_use_data(&self, span: Span) -> Option<MacroRef> {
838         if !generated_code(span) {
839             return None;
840         }
841         // Note we take care to use the source callsite/callee, to handle
842         // nested expansions and ensure we only generate data for source-visible
843         // macro uses.
844         let callsite = span.source_callsite();
845         let callsite_span = self.span_from_span(callsite);
846         let callee = span.source_callee()?;
847         let callee_span = callee.def_site?;
848
849         // Ignore attribute macros, their spans are usually mangled
850         if let MacroAttribute(_) = callee.format {
851             return None;
852         }
853
854         // If the callee is an imported macro from an external crate, need to get
855         // the source span and name from the session, as their spans are localized
856         // when read in, and no longer correspond to the source.
857         if let Some(mac) = self.tcx
858             .sess
859             .imported_macro_spans
860             .borrow()
861             .get(&callee_span)
862         {
863             let &(ref mac_name, mac_span) = mac;
864             let mac_span = self.span_from_span(mac_span);
865             return Some(MacroRef {
866                 span: callsite_span,
867                 qualname: mac_name.clone(), // FIXME: generate the real qualname
868                 callee_span: mac_span,
869             });
870         }
871
872         let callee_span = self.span_from_span(callee_span);
873         Some(MacroRef {
874             span: callsite_span,
875             qualname: callee.format.name().to_string(), // FIXME: generate the real qualname
876             callee_span,
877         })
878     }
879
880     fn lookup_ref_id(&self, ref_id: NodeId) -> Option<DefId> {
881         match self.get_path_def(ref_id) {
882             HirDef::PrimTy(_) | HirDef::SelfTy(..) | HirDef::Err => None,
883             def => Some(def.def_id()),
884         }
885     }
886
887     fn docs_for_attrs(&self, attrs: &[Attribute]) -> String {
888         let mut result = String::new();
889
890         for attr in attrs {
891             if attr.check_name("doc") {
892                 if let Some(val) = attr.value_str() {
893                     if attr.is_sugared_doc {
894                         result.push_str(&strip_doc_comment_decoration(&val.as_str()));
895                     } else {
896                         result.push_str(&val.as_str());
897                     }
898                     result.push('\n');
899                 } else if let Some(meta_list) = attr.meta_item_list() {
900                     meta_list.into_iter()
901                              .filter(|it| it.check_name("include"))
902                              .filter_map(|it| it.meta_item_list().map(|l| l.to_owned()))
903                              .flat_map(|it| it)
904                              .filter(|meta| meta.check_name("contents"))
905                              .filter_map(|meta| meta.value_str())
906                              .for_each(|val| {
907                                  result.push_str(&val.as_str());
908                                  result.push('\n');
909                              });
910                 }
911             }
912         }
913
914         if !self.config.full_docs {
915             if let Some(index) = result.find("\n\n") {
916                 result.truncate(index);
917             }
918         }
919
920         result
921     }
922
923     fn next_impl_id(&self) -> u32 {
924         let next = self.impl_counter.get();
925         self.impl_counter.set(next + 1);
926         next
927     }
928 }
929
930 fn make_signature(decl: &ast::FnDecl, generics: &ast::Generics) -> String {
931     let mut sig = "fn ".to_owned();
932     if !generics.params.is_empty() {
933         sig.push('<');
934         sig.push_str(&generics
935             .params
936             .iter()
937             .map(|param| param.ident.to_string())
938             .collect::<Vec<_>>()
939             .join(", "));
940         sig.push_str("> ");
941     }
942     sig.push('(');
943     sig.push_str(&decl.inputs
944         .iter()
945         .map(arg_to_string)
946         .collect::<Vec<_>>()
947         .join(", "));
948     sig.push(')');
949     match decl.output {
950         ast::FunctionRetTy::Default(_) => sig.push_str(" -> ()"),
951         ast::FunctionRetTy::Ty(ref t) => sig.push_str(&format!(" -> {}", ty_to_string(t))),
952     }
953
954     sig
955 }
956
957 // An AST visitor for collecting paths (e.g., the names of structs) and formal
958 // variables (idents) from patterns.
959 struct PathCollector<'l> {
960     collected_paths: Vec<(NodeId, &'l ast::Path)>,
961     collected_idents: Vec<(NodeId, ast::Ident, ast::Mutability)>,
962 }
963
964 impl<'l> PathCollector<'l> {
965     fn new() -> PathCollector<'l> {
966         PathCollector {
967             collected_paths: vec![],
968             collected_idents: vec![],
969         }
970     }
971 }
972
973 impl<'l, 'a: 'l> Visitor<'a> for PathCollector<'l> {
974     fn visit_pat(&mut self, p: &'a ast::Pat) {
975         match p.node {
976             PatKind::Struct(ref path, ..) => {
977                 self.collected_paths.push((p.id, path));
978             }
979             PatKind::TupleStruct(ref path, ..) | PatKind::Path(_, ref path) => {
980                 self.collected_paths.push((p.id, path));
981             }
982             PatKind::Ident(bm, ident, _) => {
983                 debug!(
984                     "PathCollector, visit ident in pat {}: {:?} {:?}",
985                     ident,
986                     p.span,
987                     ident.span
988                 );
989                 let immut = match bm {
990                     // Even if the ref is mut, you can't change the ref, only
991                     // the data pointed at, so showing the initialising expression
992                     // is still worthwhile.
993                     ast::BindingMode::ByRef(_) => ast::Mutability::Immutable,
994                     ast::BindingMode::ByValue(mt) => mt,
995                 };
996                 self.collected_idents
997                     .push((p.id, ident, immut));
998             }
999             _ => {}
1000         }
1001         visit::walk_pat(self, p);
1002     }
1003 }
1004
1005 /// Defines what to do with the results of saving the analysis.
1006 pub trait SaveHandler {
1007     fn save<'l, 'tcx>(
1008         &mut self,
1009         save_ctxt: SaveContext<'l, 'tcx>,
1010         krate: &ast::Crate,
1011         cratename: &str,
1012     );
1013 }
1014
1015 /// Dump the save-analysis results to a file.
1016 pub struct DumpHandler<'a> {
1017     odir: Option<&'a Path>,
1018     cratename: String,
1019 }
1020
1021 impl<'a> DumpHandler<'a> {
1022     pub fn new(odir: Option<&'a Path>, cratename: &str) -> DumpHandler<'a> {
1023         DumpHandler {
1024             odir,
1025             cratename: cratename.to_owned(),
1026         }
1027     }
1028
1029     fn output_file(&self, ctx: &SaveContext) -> File {
1030         let sess = &ctx.tcx.sess;
1031         let file_name = match ctx.config.output_file {
1032             Some(ref s) => PathBuf::from(s),
1033             None => {
1034                 let mut root_path = match self.odir {
1035                     Some(val) => val.join("save-analysis"),
1036                     None => PathBuf::from("save-analysis-temp"),
1037                 };
1038
1039                 if let Err(e) = std::fs::create_dir_all(&root_path) {
1040                     error!("Could not create directory {}: {}", root_path.display(), e);
1041                 }
1042
1043                 let executable = sess.crate_types
1044                     .borrow()
1045                     .iter()
1046                     .any(|ct| *ct == CrateTypeExecutable);
1047                 let mut out_name = if executable {
1048                     "".to_owned()
1049                 } else {
1050                     "lib".to_owned()
1051                 };
1052                 out_name.push_str(&self.cratename);
1053                 out_name.push_str(&sess.opts.cg.extra_filename);
1054                 out_name.push_str(".json");
1055                 root_path.push(&out_name);
1056
1057                 root_path
1058             }
1059         };
1060
1061         info!("Writing output to {}", file_name.display());
1062
1063         let output_file = File::create(&file_name).unwrap_or_else(
1064             |e| sess.fatal(&format!("Could not open {}: {}", file_name.display(), e)),
1065         );
1066
1067         output_file
1068     }
1069 }
1070
1071 impl<'a> SaveHandler for DumpHandler<'a> {
1072     fn save<'l, 'tcx>(
1073         &mut self,
1074         save_ctxt: SaveContext<'l, 'tcx>,
1075         krate: &ast::Crate,
1076         cratename: &str,
1077     ) {
1078         let output = &mut self.output_file(&save_ctxt);
1079         let mut dumper = JsonDumper::new(output, save_ctxt.config.clone());
1080         let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
1081
1082         visitor.dump_crate_info(cratename, krate);
1083         visit::walk_crate(&mut visitor, krate);
1084     }
1085 }
1086
1087 /// Call a callback with the results of save-analysis.
1088 pub struct CallbackHandler<'b> {
1089     pub callback: &'b mut FnMut(&rls_data::Analysis),
1090 }
1091
1092 impl<'b> SaveHandler for CallbackHandler<'b> {
1093     fn save<'l, 'tcx>(
1094         &mut self,
1095         save_ctxt: SaveContext<'l, 'tcx>,
1096         krate: &ast::Crate,
1097         cratename: &str,
1098     ) {
1099         // We're using the JsonDumper here because it has the format of the
1100         // save-analysis results that we will pass to the callback. IOW, we are
1101         // using the JsonDumper to collect the save-analysis results, but not
1102         // actually to dump them to a file. This is all a bit convoluted and
1103         // there is certainly a simpler design here trying to get out (FIXME).
1104         let mut dumper = JsonDumper::with_callback(self.callback, save_ctxt.config.clone());
1105         let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
1106
1107         visitor.dump_crate_info(cratename, krate);
1108         visit::walk_crate(&mut visitor, krate);
1109     }
1110 }
1111
1112 pub fn process_crate<'l, 'tcx, H: SaveHandler>(
1113     tcx: TyCtxt<'l, 'tcx, 'tcx>,
1114     krate: &ast::Crate,
1115     analysis: &'l ty::CrateAnalysis,
1116     cratename: &str,
1117     config: Option<Config>,
1118     mut handler: H,
1119 ) {
1120     tcx.dep_graph.with_ignore(|| {
1121         assert!(analysis.glob_map.is_some());
1122
1123         info!("Dumping crate {}", cratename);
1124
1125         let save_ctxt = SaveContext {
1126             tcx,
1127             tables: &ty::TypeckTables::empty(None),
1128             analysis,
1129             span_utils: SpanUtils::new(&tcx.sess),
1130             config: find_config(config),
1131             impl_counter: Cell::new(0),
1132         };
1133
1134         handler.save(save_ctxt, krate, cratename)
1135     })
1136 }
1137
1138 fn find_config(supplied: Option<Config>) -> Config {
1139     if let Some(config) = supplied {
1140         return config;
1141     }
1142     match env::var_os("RUST_SAVE_ANALYSIS_CONFIG") {
1143         Some(config_string) => rustc_serialize::json::decode(config_string.to_str().unwrap())
1144             .expect("Could not deserialize save-analysis config"),
1145         None => Config::default(),
1146     }
1147 }
1148
1149 // Utility functions for the module.
1150
1151 // Helper function to escape quotes in a string
1152 fn escape(s: String) -> String {
1153     s.replace("\"", "\"\"")
1154 }
1155
1156 // Helper function to determine if a span came from a
1157 // macro expansion or syntax extension.
1158 fn generated_code(span: Span) -> bool {
1159     span.ctxt() != NO_EXPANSION || span.is_dummy()
1160 }
1161
1162 // DefId::index is a newtype and so the JSON serialisation is ugly. Therefore
1163 // we use our own Id which is the same, but without the newtype.
1164 fn id_from_def_id(id: DefId) -> rls_data::Id {
1165     rls_data::Id {
1166         krate: id.krate.as_u32(),
1167         index: id.index.as_raw_u32(),
1168     }
1169 }
1170
1171 fn id_from_node_id(id: NodeId, scx: &SaveContext) -> rls_data::Id {
1172     let def_id = scx.tcx.hir.opt_local_def_id(id);
1173     def_id.map(|id| id_from_def_id(id)).unwrap_or_else(|| {
1174         // Create a *fake* `DefId` out of a `NodeId` by subtracting the `NodeId`
1175         // out of the maximum u32 value. This will work unless you have *billions*
1176         // of definitions in a single crate (very unlikely to actually happen).
1177         rls_data::Id {
1178             krate: LOCAL_CRATE.as_u32(),
1179             index: !id.as_u32(),
1180         }
1181     })
1182 }
1183
1184 fn null_id() -> rls_data::Id {
1185     rls_data::Id {
1186         krate: u32::max_value(),
1187         index: u32::max_value(),
1188     }
1189 }
1190
1191 fn lower_attributes(attrs: Vec<Attribute>, scx: &SaveContext) -> Vec<rls_data::Attribute> {
1192     attrs.into_iter()
1193     // Only retain real attributes. Doc comments are lowered separately.
1194     .filter(|attr| attr.path != "doc")
1195     .map(|mut attr| {
1196         // Remove the surrounding '#[..]' or '#![..]' of the pretty printed
1197         // attribute. First normalize all inner attribute (#![..]) to outer
1198         // ones (#[..]), then remove the two leading and the one trailing character.
1199         attr.style = ast::AttrStyle::Outer;
1200         let value = pprust::attribute_to_string(&attr);
1201         // This str slicing works correctly, because the leading and trailing characters
1202         // are in the ASCII range and thus exactly one byte each.
1203         let value = value[2..value.len()-1].to_string();
1204
1205         rls_data::Attribute {
1206             value,
1207             span: scx.span_from_span(attr.span),
1208         }
1209     }).collect()
1210 }