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