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