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