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