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