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