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