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.
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.
11 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
12 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
13 html_root_url = "https://doc.rust-lang.org/nightly/")]
14 #![feature(custom_attribute)]
16 #![allow(unused_attributes)]
18 #![recursion_limit="256"]
25 extern crate rustc_data_structures;
26 extern crate rustc_codegen_utils;
27 extern crate rustc_serialize;
28 extern crate rustc_target;
29 extern crate rustc_typeck;
32 extern crate syntax_pos;
34 extern crate rls_data;
35 extern crate rls_span;
45 use rustc::hir::def::Def as HirDef;
47 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
48 use rustc::middle::cstore::ExternCrate;
49 use rustc::session::config::{CrateType, Input, OutputType};
50 use rustc::ty::{self, TyCtxt};
51 use rustc_typeck::hir_ty_to_ty;
52 use rustc_codegen_utils::link::{filename_for_metadata, out_filename};
55 use std::default::Default;
58 use std::path::{Path, PathBuf};
60 use syntax::ast::{self, Attribute, NodeId, PatKind};
61 use syntax::source_map::Spanned;
62 use syntax::parse::lexer::comments::strip_doc_comment_decoration;
63 use syntax::parse::token;
64 use syntax::print::pprust;
65 use syntax::symbol::keywords;
66 use syntax::visit::{self, Visitor};
67 use syntax::print::pprust::{arg_to_string, ty_to_string};
68 use syntax::source_map::MacroAttribute;
71 use json_dumper::JsonDumper;
72 use dump_visitor::DumpVisitor;
73 use span_utils::SpanUtils;
75 use rls_data::{Def, DefKind, ExternalCrateData, GlobalCrateId, MacroRef, Ref, RefKind, Relation,
76 RelationKind, SpanData, Impl, ImplKind};
77 use rls_data::config::Config;
80 pub struct SaveContext<'l, 'tcx: 'l> {
81 tcx: TyCtxt<'l, 'tcx, 'tcx>,
82 tables: &'l ty::TypeckTables<'tcx>,
83 analysis: &'l ty::CrateAnalysis,
84 span_utils: SpanUtils<'tcx>,
86 impl_counter: Cell<u32>,
93 RelationData(Relation, Impl),
96 impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
97 fn span_from_span(&self, span: Span) -> SpanData {
98 use rls_span::{Column, Row};
100 let cm = self.tcx.sess.source_map();
101 let start = cm.lookup_char_pos(span.lo());
102 let end = cm.lookup_char_pos(span.hi());
105 file_name: start.file.name.to_string().into(),
106 byte_start: span.lo().0,
107 byte_end: span.hi().0,
108 line_start: Row::new_one_indexed(start.line as u32),
109 line_end: Row::new_one_indexed(end.line as u32),
110 column_start: Column::new_one_indexed(start.col.0 as u32 + 1),
111 column_end: Column::new_one_indexed(end.col.0 as u32 + 1),
115 // Returns path to the compilation output (e.g. libfoo-12345678.rmeta)
116 pub fn compilation_output(&self, crate_name: &str) -> PathBuf {
117 let sess = &self.tcx.sess;
118 // Save-analysis is emitted per whole session, not per each crate type
119 let crate_type = sess.crate_types.borrow()[0];
120 let outputs = &*self.tcx.output_filenames(LOCAL_CRATE);
122 if outputs.outputs.contains_key(&OutputType::Metadata) {
123 filename_for_metadata(sess, crate_name, outputs)
124 } else if outputs.outputs.should_codegen() {
125 out_filename(sess, crate_type, outputs, crate_name)
127 // Otherwise it's only a DepInfo, in which case we return early and
128 // not even reach the analysis stage.
133 // List external crates used by the current crate.
134 pub fn get_external_crates(&self) -> Vec<ExternalCrateData> {
135 let mut result = Vec::with_capacity(self.tcx.crates().len());
137 for &n in self.tcx.crates().iter() {
138 let span = match *self.tcx.extern_crate(n.as_def_id()) {
139 Some(ExternCrate { span, .. }) => span,
141 debug!("Skipping crate {}, no data", n);
145 let lo_loc = self.span_utils.sess.source_map().lookup_char_pos(span.lo());
146 result.push(ExternalCrateData {
147 // FIXME: change file_name field to PathBuf in rls-data
148 // https://github.com/nrc/rls-data/issues/7
149 file_name: self.span_utils.make_filename_string(&lo_loc.file),
152 name: self.tcx.crate_name(n).to_string(),
153 disambiguator: self.tcx.crate_disambiguator(n).to_fingerprint().as_value(),
161 pub fn get_extern_item_data(&self, item: &ast::ForeignItem) -> Option<Data> {
162 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
164 ast::ForeignItemKind::Fn(ref decl, ref generics) => {
165 let sub_span = self.span_utils
166 .sub_span_after_keyword(item.span, keywords::Fn);
167 filter!(self.span_utils, sub_span, item.span, None);
169 Some(Data::DefData(Def {
170 kind: DefKind::ForeignFunction,
171 id: id_from_node_id(item.id, self),
172 span: self.span_from_span(sub_span.unwrap()),
173 name: item.ident.to_string(),
175 value: make_signature(decl, generics),
179 docs: self.docs_for_attrs(&item.attrs),
180 sig: sig::foreign_item_signature(item, self),
181 attributes: lower_attributes(item.attrs.clone(), self),
184 ast::ForeignItemKind::Static(ref ty, m) => {
185 let keyword = if m { keywords::Mut } else { keywords::Static };
186 let sub_span = self.span_utils.sub_span_after_keyword(item.span, keyword);
187 filter!(self.span_utils, sub_span, item.span, None);
189 let id = ::id_from_node_id(item.id, self);
190 let span = self.span_from_span(sub_span.unwrap());
192 Some(Data::DefData(Def {
193 kind: DefKind::ForeignStatic,
196 name: item.ident.to_string(),
198 value: ty_to_string(ty),
202 docs: self.docs_for_attrs(&item.attrs),
203 sig: sig::foreign_item_signature(item, self),
204 attributes: lower_attributes(item.attrs.clone(), self),
207 // FIXME(plietar): needs a new DefKind in rls-data
208 ast::ForeignItemKind::Ty => None,
209 ast::ForeignItemKind::Macro(..) => None,
213 pub fn get_item_data(&self, item: &ast::Item) -> Option<Data> {
215 ast::ItemKind::Fn(ref decl, .., ref generics, _) => {
216 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
217 let sub_span = self.span_utils
218 .sub_span_after_keyword(item.span, keywords::Fn);
219 filter!(self.span_utils, sub_span, item.span, None);
220 Some(Data::DefData(Def {
221 kind: DefKind::Function,
222 id: id_from_node_id(item.id, self),
223 span: self.span_from_span(sub_span.unwrap()),
224 name: item.ident.to_string(),
226 value: make_signature(decl, generics),
230 docs: self.docs_for_attrs(&item.attrs),
231 sig: sig::item_signature(item, self),
232 attributes: lower_attributes(item.attrs.clone(), self),
235 ast::ItemKind::Static(ref typ, mt, _) => {
236 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
238 let keyword = match mt {
239 ast::Mutability::Mutable => keywords::Mut,
240 ast::Mutability::Immutable => keywords::Static,
243 let sub_span = self.span_utils.sub_span_after_keyword(item.span, keyword);
244 filter!(self.span_utils, sub_span, item.span, None);
246 let id = id_from_node_id(item.id, self);
247 let span = self.span_from_span(sub_span.unwrap());
249 Some(Data::DefData(Def {
250 kind: DefKind::Static,
253 name: item.ident.to_string(),
255 value: ty_to_string(&typ),
259 docs: self.docs_for_attrs(&item.attrs),
260 sig: sig::item_signature(item, self),
261 attributes: lower_attributes(item.attrs.clone(), self),
264 ast::ItemKind::Const(ref typ, _) => {
265 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
266 let sub_span = self.span_utils
267 .sub_span_after_keyword(item.span, keywords::Const);
268 filter!(self.span_utils, sub_span, item.span, None);
270 let id = id_from_node_id(item.id, self);
271 let span = self.span_from_span(sub_span.unwrap());
273 Some(Data::DefData(Def {
274 kind: DefKind::Const,
277 name: item.ident.to_string(),
279 value: ty_to_string(typ),
283 docs: self.docs_for_attrs(&item.attrs),
284 sig: sig::item_signature(item, self),
285 attributes: lower_attributes(item.attrs.clone(), self),
288 ast::ItemKind::Mod(ref m) => {
289 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
291 let cm = self.tcx.sess.source_map();
292 let filename = cm.span_to_filename(m.inner);
294 let sub_span = self.span_utils
295 .sub_span_after_keyword(item.span, keywords::Mod);
296 filter!(self.span_utils, sub_span, item.span, None);
298 Some(Data::DefData(Def {
300 id: id_from_node_id(item.id, self),
301 name: item.ident.to_string(),
303 span: self.span_from_span(sub_span.unwrap()),
304 value: filename.to_string(),
308 .map(|i| id_from_node_id(i.id, self))
311 docs: self.docs_for_attrs(&item.attrs),
312 sig: sig::item_signature(item, self),
313 attributes: lower_attributes(item.attrs.clone(), self),
316 ast::ItemKind::Enum(ref def, _) => {
317 let name = item.ident.to_string();
318 let qualname = format!("::{}", self.tcx.node_path_str(item.id));
319 let sub_span = self.span_utils
320 .sub_span_after_keyword(item.span, keywords::Enum);
321 filter!(self.span_utils, sub_span, item.span, None);
322 let variants_str = def.variants
324 .map(|v| v.node.ident.to_string())
327 let value = format!("{}::{{{}}}", name, variants_str);
328 Some(Data::DefData(Def {
330 id: id_from_node_id(item.id, self),
331 span: self.span_from_span(sub_span.unwrap()),
336 children: def.variants
338 .map(|v| id_from_node_id(v.node.data.id(), self))
341 docs: self.docs_for_attrs(&item.attrs),
342 sig: sig::item_signature(item, self),
343 attributes: lower_attributes(item.attrs.clone(), self),
346 ast::ItemKind::Impl(.., ref trait_ref, ref typ, ref impls) => {
347 if let ast::TyKind::Path(None, ref path) = typ.node {
348 // Common case impl for a struct or something basic.
349 if generated_code(path.span) {
352 let sub_span = self.span_utils.sub_span_for_type_name(path.span);
353 filter!(self.span_utils, sub_span, typ.span, None);
355 let impl_id = self.next_impl_id();
356 let span = self.span_from_span(sub_span.unwrap());
358 let type_data = self.lookup_ref_id(typ.id);
359 type_data.map(|type_data| {
360 Data::RelationData(Relation {
361 kind: RelationKind::Impl {
365 from: id_from_def_id(type_data),
368 .and_then(|t| self.lookup_ref_id(t.ref_id))
370 .unwrap_or_else(|| null_id()),
374 kind: match *trait_ref {
375 Some(_) => ImplKind::Direct,
376 None => ImplKind::Inherent,
379 value: String::new(),
383 .map(|i| id_from_node_id(i.id, self))
401 pub fn get_field_data(&self, field: &ast::StructField, scope: NodeId) -> Option<Def> {
402 if let Some(ident) = field.ident {
403 let name = ident.to_string();
404 let qualname = format!("::{}::{}", self.tcx.node_path_str(scope), ident);
405 let sub_span = self.span_utils
406 .sub_span_before_token(field.span, token::Colon);
407 filter!(self.span_utils, sub_span, field.span, None);
408 let def_id = self.tcx.hir.local_def_id(field.id);
409 let typ = self.tcx.type_of(def_id).to_string();
412 let id = id_from_node_id(field.id, self);
413 let span = self.span_from_span(sub_span.unwrap());
416 kind: DefKind::Field,
422 parent: Some(id_from_node_id(scope, self)),
425 docs: self.docs_for_attrs(&field.attrs),
426 sig: sig::field_signature(field, self),
427 attributes: lower_attributes(field.attrs.clone(), self),
434 // FIXME would be nice to take a MethodItem here, but the ast provides both
435 // trait and impl flavours, so the caller must do the disassembly.
436 pub fn get_method_data(&self, id: ast::NodeId, name: ast::Name, span: Span) -> Option<Def> {
437 // The qualname for a method is the trait name or name of the struct in an impl in
438 // which the method is declared in, followed by the method's name.
439 let (qualname, parent_scope, decl_id, docs, attributes) =
440 match self.tcx.impl_of_method(self.tcx.hir.local_def_id(id)) {
441 Some(impl_id) => match self.tcx.hir.get_if_local(impl_id) {
442 Some(Node::Item(item)) => match item.node {
443 hir::ItemKind::Impl(.., ref ty, _) => {
444 let mut qualname = String::from("<");
445 qualname.push_str(&self.tcx.hir.node_to_pretty_string(ty.id));
447 let trait_id = self.tcx.trait_id_of_impl(impl_id);
448 let mut decl_id = None;
449 let mut docs = String::new();
450 let mut attrs = vec![];
451 if let Some(Node::ImplItem(item)) = self.tcx.hir.find(id) {
452 docs = self.docs_for_attrs(&item.attrs);
453 attrs = item.attrs.to_vec();
456 if let Some(def_id) = trait_id {
457 // A method in a trait impl.
458 qualname.push_str(" as ");
459 qualname.push_str(&self.tcx.item_path_str(def_id));
461 .associated_items(def_id)
462 .find(|item| item.ident.name == name)
463 .map(|item| decl_id = Some(item.def_id));
465 qualname.push_str(">");
467 (qualname, trait_id, decl_id, docs, attrs)
472 "Container {:?} for method {} not an impl?",
481 "Container {:?} for method {} is not a node item {:?}",
488 None => match self.tcx.trait_of_item(self.tcx.hir.local_def_id(id)) {
490 let mut docs = String::new();
491 let mut attrs = vec![];
493 if let Some(Node::TraitItem(item)) = self.tcx.hir.find(id) {
494 docs = self.docs_for_attrs(&item.attrs);
495 attrs = item.attrs.to_vec();
499 format!("::{}", self.tcx.item_path_str(def_id)),
507 debug!("Could not find container for method {} at {:?}", id, span);
508 // This is not necessarily a bug, if there was a compilation error,
509 // the tables we need might not exist.
515 let qualname = format!("{}::{}", qualname, name);
517 let sub_span = self.span_utils.sub_span_after_keyword(span, keywords::Fn);
518 filter!(self.span_utils, sub_span, span, None);
521 kind: DefKind::Method,
522 id: id_from_node_id(id, self),
523 span: self.span_from_span(sub_span.unwrap()),
524 name: name.to_string(),
526 // FIXME you get better data here by using the visitor.
527 value: String::new(),
528 parent: parent_scope.map(|id| id_from_def_id(id)),
530 decl_id: decl_id.map(|id| id_from_def_id(id)),
533 attributes: lower_attributes(attributes, self),
537 pub fn get_trait_ref_data(&self, trait_ref: &ast::TraitRef) -> Option<Ref> {
538 self.lookup_ref_id(trait_ref.ref_id).and_then(|def_id| {
539 let span = trait_ref.path.span;
540 if generated_code(span) {
543 let sub_span = self.span_utils.sub_span_for_type_name(span).or(Some(span));
544 filter!(self.span_utils, sub_span, span, None);
545 let span = self.span_from_span(sub_span.unwrap());
549 ref_id: id_from_def_id(def_id),
554 pub fn get_expr_data(&self, expr: &ast::Expr) -> Option<Data> {
555 let hir_node = self.tcx.hir.expect_expr(expr.id);
556 let ty = self.tables.expr_ty_adjusted_opt(&hir_node);
557 if ty.is_none() || ty.unwrap().sty == ty::Error {
561 ast::ExprKind::Field(ref sub_ex, ident) => {
562 let hir_node = match self.tcx.hir.find(sub_ex.id) {
563 Some(Node::Expr(expr)) => expr,
566 "Missing or weird node for sub-expression {} in {:?}",
573 match self.tables.expr_ty_adjusted(&hir_node).sty {
574 ty::Adt(def, _) if !def.is_enum() => {
575 let variant = &def.non_enum_variant();
576 let index = self.tcx.find_field_index(ident, variant).unwrap();
577 let sub_span = self.span_utils.span_for_last_ident(expr.span);
578 filter!(self.span_utils, sub_span, expr.span, None);
579 let span = self.span_from_span(sub_span.unwrap());
580 return Some(Data::RefData(Ref {
581 kind: RefKind::Variable,
583 ref_id: id_from_def_id(variant.fields[index].did),
586 ty::Tuple(..) => None,
588 debug!("Expected struct or union type, found {:?}", ty);
593 ast::ExprKind::Struct(ref path, ..) => {
594 match self.tables.expr_ty_adjusted(&hir_node).sty {
595 ty::Adt(def, _) if !def.is_enum() => {
596 let sub_span = self.span_utils.span_for_last_ident(path.span);
597 filter!(self.span_utils, sub_span, path.span, None);
598 let span = self.span_from_span(sub_span.unwrap());
599 Some(Data::RefData(Ref {
602 ref_id: id_from_def_id(def.did),
606 // FIXME ty could legitimately be an enum, but then we will fail
607 // later if we try to look up the fields.
608 debug!("expected struct or union, found {:?}", ty);
613 ast::ExprKind::MethodCall(ref seg, ..) => {
614 let expr_hir_id = self.tcx.hir.definitions().node_to_hir_id(expr.id);
615 let method_id = match self.tables.type_dependent_defs().get(expr_hir_id) {
616 Some(id) => id.def_id(),
618 debug!("Could not resolve method id for {:?}", expr);
622 let (def_id, decl_id) = match self.tcx.associated_item(method_id).container {
623 ty::ImplContainer(_) => (Some(method_id), None),
624 ty::TraitContainer(_) => (None, Some(method_id)),
626 let sub_span = seg.ident.span;
627 filter!(self.span_utils, Some(sub_span), expr.span, None);
628 let span = self.span_from_span(sub_span);
629 Some(Data::RefData(Ref {
630 kind: RefKind::Function,
634 .map(|id| id_from_def_id(id))
635 .unwrap_or_else(|| null_id()),
638 ast::ExprKind::Path(_, ref path) => {
639 self.get_path_data(expr.id, path).map(|d| Data::RefData(d))
648 pub fn get_path_def(&self, id: NodeId) -> HirDef {
649 match self.tcx.hir.get(id) {
650 Node::TraitRef(tr) => tr.path.def,
652 Node::Item(&hir::Item {
653 node: hir::ItemKind::Use(ref path, _),
656 Node::Visibility(&Spanned {
657 node: hir::VisibilityKind::Restricted { ref path, .. }, .. }) => path.def,
659 Node::Expr(&hir::Expr {
660 node: hir::ExprKind::Struct(ref qpath, ..),
663 Node::Expr(&hir::Expr {
664 node: hir::ExprKind::Path(ref qpath),
667 Node::Pat(&hir::Pat {
668 node: hir::PatKind::Path(ref qpath),
671 Node::Pat(&hir::Pat {
672 node: hir::PatKind::Struct(ref qpath, ..),
675 Node::Pat(&hir::Pat {
676 node: hir::PatKind::TupleStruct(ref qpath, ..),
679 let hir_id = self.tcx.hir.node_to_hir_id(id);
680 self.tables.qpath_def(qpath, hir_id)
683 Node::Binding(&hir::Pat {
684 node: hir::PatKind::Binding(_, canonical_id, ..),
686 }) => HirDef::Local(canonical_id),
688 Node::Ty(ty) => if let hir::Ty {
689 node: hir::TyKind::Path(ref qpath),
694 hir::QPath::Resolved(_, ref path) => path.def,
695 hir::QPath::TypeRelative(..) => {
696 let ty = hir_ty_to_ty(self.tcx, ty);
697 if let ty::Projection(proj) = ty.sty {
698 return HirDef::AssociatedTy(proj.item_def_id);
711 pub fn get_path_data(&self, id: NodeId, path: &ast::Path) -> Option<Ref> {
712 // Returns true if the path is function type sugar, e.g., `Fn(A) -> B`.
713 fn fn_type(path: &ast::Path) -> bool {
714 if path.segments.len() != 1 {
717 if let Some(ref generic_args) = path.segments[0].args {
718 if let ast::GenericArgs::Parenthesized(_) = **generic_args {
725 if path.segments.is_empty() {
729 let def = self.get_path_def(id);
730 let last_seg = &path.segments[path.segments.len() - 1];
731 let sub_span = last_seg.ident.span;
732 filter!(self.span_utils, Some(sub_span), path.span, None);
734 HirDef::Upvar(id, ..) | HirDef::Local(id) => {
735 let span = self.span_from_span(sub_span);
737 kind: RefKind::Variable,
739 ref_id: id_from_node_id(id, self),
744 HirDef::AssociatedConst(..) |
745 HirDef::VariantCtor(..) => {
746 let span = self.span_from_span(sub_span);
748 kind: RefKind::Variable,
750 ref_id: id_from_def_id(def.def_id()),
753 HirDef::Trait(def_id) if fn_type(path) => {
754 // Function type bounds are desugared in the parser, so we have to
755 // special case them here.
756 let fn_span = self.span_utils.span_for_first_ident(path.span);
760 span: self.span_from_span(span),
761 ref_id: id_from_def_id(def_id),
765 HirDef::Struct(def_id) |
766 HirDef::Variant(def_id, ..) |
767 HirDef::Union(def_id) |
768 HirDef::Enum(def_id) |
769 HirDef::TyAlias(def_id) |
770 HirDef::ForeignTy(def_id) |
771 HirDef::TraitAlias(def_id) |
772 HirDef::AssociatedExistential(def_id) |
773 HirDef::AssociatedTy(def_id) |
774 HirDef::Trait(def_id) |
775 HirDef::Existential(def_id) |
776 HirDef::TyParam(def_id) => {
777 let span = self.span_from_span(sub_span);
781 ref_id: id_from_def_id(def_id),
784 HirDef::StructCtor(def_id, _) => {
785 // This is a reference to a tuple struct where the def_id points
786 // to an invisible constructor function. That is not a very useful
787 // def, so adjust to point to the tuple struct itself.
788 let span = self.span_from_span(sub_span);
789 let parent_def_id = self.tcx.parent_def_id(def_id).unwrap();
793 ref_id: id_from_def_id(parent_def_id),
796 HirDef::Method(decl_id) => {
797 let def_id = if decl_id.is_local() {
798 let ti = self.tcx.associated_item(decl_id);
800 .associated_items(ti.container.id())
801 .find(|item| item.ident.name == ti.ident.name &&
802 item.defaultness.has_value())
803 .map(|item| item.def_id)
807 let span = self.span_from_span(sub_span);
809 kind: RefKind::Function,
811 ref_id: id_from_def_id(def_id.unwrap_or(decl_id)),
814 HirDef::Fn(def_id) => {
815 let span = self.span_from_span(sub_span);
817 kind: RefKind::Function,
819 ref_id: id_from_def_id(def_id),
822 HirDef::Mod(def_id) => {
823 let span = self.span_from_span(sub_span);
827 ref_id: id_from_def_id(def_id),
835 HirDef::NonMacroAttr(..) |
836 HirDef::SelfCtor(..) |
841 pub fn get_field_ref_data(
843 field_ref: &ast::Field,
844 variant: &ty::VariantDef,
846 let index = self.tcx.find_field_index(field_ref.ident, variant).unwrap();
847 // We don't really need a sub-span here, but no harm done
848 let sub_span = self.span_utils.span_for_last_ident(field_ref.ident.span);
849 filter!(self.span_utils, sub_span, field_ref.ident.span, None);
850 let span = self.span_from_span(sub_span.unwrap());
852 kind: RefKind::Variable,
854 ref_id: id_from_def_id(variant.fields[index].did),
858 /// Attempt to return MacroRef for any AST node.
860 /// For a given piece of AST defined by the supplied Span and NodeId,
861 /// returns None if the node is not macro-generated or the span is malformed,
862 /// else uses the expansion callsite and callee to return some MacroRef.
863 pub fn get_macro_use_data(&self, span: Span) -> Option<MacroRef> {
864 if !generated_code(span) {
867 // Note we take care to use the source callsite/callee, to handle
868 // nested expansions and ensure we only generate data for source-visible
870 let callsite = span.source_callsite();
871 let callsite_span = self.span_from_span(callsite);
872 let callee = span.source_callee()?;
873 let callee_span = callee.def_site?;
875 // Ignore attribute macros, their spans are usually mangled
876 if let MacroAttribute(_) = callee.format {
880 // If the callee is an imported macro from an external crate, need to get
881 // the source span and name from the session, as their spans are localized
882 // when read in, and no longer correspond to the source.
883 if let Some(mac) = self.tcx
885 .imported_macro_spans
889 let &(ref mac_name, mac_span) = mac;
890 let mac_span = self.span_from_span(mac_span);
891 return Some(MacroRef {
893 qualname: mac_name.clone(), // FIXME: generate the real qualname
894 callee_span: mac_span,
898 let callee_span = self.span_from_span(callee_span);
901 qualname: callee.format.name().to_string(), // FIXME: generate the real qualname
906 fn lookup_ref_id(&self, ref_id: NodeId) -> Option<DefId> {
907 match self.get_path_def(ref_id) {
908 HirDef::PrimTy(_) | HirDef::SelfTy(..) | HirDef::Err => None,
909 def => Some(def.def_id()),
913 fn docs_for_attrs(&self, attrs: &[Attribute]) -> String {
914 let mut result = String::new();
917 if attr.check_name("doc") {
918 if let Some(val) = attr.value_str() {
919 if attr.is_sugared_doc {
920 result.push_str(&strip_doc_comment_decoration(&val.as_str()));
922 result.push_str(&val.as_str());
925 } else if let Some(meta_list) = attr.meta_item_list() {
926 meta_list.into_iter()
927 .filter(|it| it.check_name("include"))
928 .filter_map(|it| it.meta_item_list().map(|l| l.to_owned()))
930 .filter(|meta| meta.check_name("contents"))
931 .filter_map(|meta| meta.value_str())
933 result.push_str(&val.as_str());
940 if !self.config.full_docs {
941 if let Some(index) = result.find("\n\n") {
942 result.truncate(index);
949 fn next_impl_id(&self) -> u32 {
950 let next = self.impl_counter.get();
951 self.impl_counter.set(next + 1);
956 fn make_signature(decl: &ast::FnDecl, generics: &ast::Generics) -> String {
957 let mut sig = "fn ".to_owned();
958 if !generics.params.is_empty() {
960 sig.push_str(&generics
963 .map(|param| param.ident.to_string())
969 sig.push_str(&decl.inputs
976 ast::FunctionRetTy::Default(_) => sig.push_str(" -> ()"),
977 ast::FunctionRetTy::Ty(ref t) => sig.push_str(&format!(" -> {}", ty_to_string(t))),
983 // An AST visitor for collecting paths (e.g., the names of structs) and formal
984 // variables (idents) from patterns.
985 struct PathCollector<'l> {
986 collected_paths: Vec<(NodeId, &'l ast::Path)>,
987 collected_idents: Vec<(NodeId, ast::Ident, ast::Mutability)>,
990 impl<'l> PathCollector<'l> {
991 fn new() -> PathCollector<'l> {
993 collected_paths: vec![],
994 collected_idents: vec![],
999 impl<'l, 'a: 'l> Visitor<'a> for PathCollector<'l> {
1000 fn visit_pat(&mut self, p: &'a ast::Pat) {
1002 PatKind::Struct(ref path, ..) => {
1003 self.collected_paths.push((p.id, path));
1005 PatKind::TupleStruct(ref path, ..) | PatKind::Path(_, ref path) => {
1006 self.collected_paths.push((p.id, path));
1008 PatKind::Ident(bm, ident, _) => {
1010 "PathCollector, visit ident in pat {}: {:?} {:?}",
1015 let immut = match bm {
1016 // Even if the ref is mut, you can't change the ref, only
1017 // the data pointed at, so showing the initialising expression
1018 // is still worthwhile.
1019 ast::BindingMode::ByRef(_) => ast::Mutability::Immutable,
1020 ast::BindingMode::ByValue(mt) => mt,
1022 self.collected_idents
1023 .push((p.id, ident, immut));
1027 visit::walk_pat(self, p);
1031 /// Defines what to do with the results of saving the analysis.
1032 pub trait SaveHandler {
1035 save_ctxt: SaveContext<'l, 'tcx>,
1042 /// Dump the save-analysis results to a file.
1043 pub struct DumpHandler<'a> {
1044 odir: Option<&'a Path>,
1048 impl<'a> DumpHandler<'a> {
1049 pub fn new(odir: Option<&'a Path>, cratename: &str) -> DumpHandler<'a> {
1052 cratename: cratename.to_owned(),
1056 fn output_file(&self, ctx: &SaveContext) -> File {
1057 let sess = &ctx.tcx.sess;
1058 let file_name = match ctx.config.output_file {
1059 Some(ref s) => PathBuf::from(s),
1061 let mut root_path = match self.odir {
1062 Some(val) => val.join("save-analysis"),
1063 None => PathBuf::from("save-analysis-temp"),
1066 if let Err(e) = std::fs::create_dir_all(&root_path) {
1067 error!("Could not create directory {}: {}", root_path.display(), e);
1070 let executable = sess.crate_types
1073 .any(|ct| *ct == CrateType::Executable);
1074 let mut out_name = if executable {
1079 out_name.push_str(&self.cratename);
1080 out_name.push_str(&sess.opts.cg.extra_filename);
1081 out_name.push_str(".json");
1082 root_path.push(&out_name);
1088 info!("Writing output to {}", file_name.display());
1090 let output_file = File::create(&file_name).unwrap_or_else(
1091 |e| sess.fatal(&format!("Could not open {}: {}", file_name.display(), e)),
1098 impl<'a> SaveHandler for DumpHandler<'a> {
1101 save_ctxt: SaveContext<'l, 'tcx>,
1106 let output = &mut self.output_file(&save_ctxt);
1107 let mut dumper = JsonDumper::new(output, save_ctxt.config.clone());
1108 let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
1110 visitor.dump_crate_info(cratename, krate);
1111 visitor.dump_compilation_options(input, cratename);
1112 visit::walk_crate(&mut visitor, krate);
1116 /// Call a callback with the results of save-analysis.
1117 pub struct CallbackHandler<'b> {
1118 pub callback: &'b mut dyn FnMut(&rls_data::Analysis),
1121 impl<'b> SaveHandler for CallbackHandler<'b> {
1124 save_ctxt: SaveContext<'l, 'tcx>,
1129 // We're using the JsonDumper here because it has the format of the
1130 // save-analysis results that we will pass to the callback. IOW, we are
1131 // using the JsonDumper to collect the save-analysis results, but not
1132 // actually to dump them to a file. This is all a bit convoluted and
1133 // there is certainly a simpler design here trying to get out (FIXME).
1134 let mut dumper = JsonDumper::with_callback(self.callback, save_ctxt.config.clone());
1135 let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
1137 visitor.dump_crate_info(cratename, krate);
1138 visitor.dump_compilation_options(input, cratename);
1139 visit::walk_crate(&mut visitor, krate);
1143 pub fn process_crate<'l, 'tcx, H: SaveHandler>(
1144 tcx: TyCtxt<'l, 'tcx, 'tcx>,
1146 analysis: &'l ty::CrateAnalysis,
1149 config: Option<Config>,
1152 tcx.dep_graph.with_ignore(|| {
1153 assert!(analysis.glob_map.is_some());
1155 info!("Dumping crate {}", cratename);
1157 let save_ctxt = SaveContext {
1159 tables: &ty::TypeckTables::empty(None),
1161 span_utils: SpanUtils::new(&tcx.sess),
1162 config: find_config(config),
1163 impl_counter: Cell::new(0),
1166 handler.save(save_ctxt, krate, cratename, input)
1170 fn find_config(supplied: Option<Config>) -> Config {
1171 if let Some(config) = supplied {
1174 match env::var_os("RUST_SAVE_ANALYSIS_CONFIG") {
1175 Some(config_string) => rustc_serialize::json::decode(config_string.to_str().unwrap())
1176 .expect("Could not deserialize save-analysis config"),
1177 None => Config::default(),
1181 // Utility functions for the module.
1183 // Helper function to escape quotes in a string
1184 fn escape(s: String) -> String {
1185 s.replace("\"", "\"\"")
1188 // Helper function to determine if a span came from a
1189 // macro expansion or syntax extension.
1190 fn generated_code(span: Span) -> bool {
1191 span.ctxt() != NO_EXPANSION || span.is_dummy()
1194 // DefId::index is a newtype and so the JSON serialisation is ugly. Therefore
1195 // we use our own Id which is the same, but without the newtype.
1196 fn id_from_def_id(id: DefId) -> rls_data::Id {
1198 krate: id.krate.as_u32(),
1199 index: id.index.as_raw_u32(),
1203 fn id_from_node_id(id: NodeId, scx: &SaveContext) -> rls_data::Id {
1204 let def_id = scx.tcx.hir.opt_local_def_id(id);
1205 def_id.map(|id| id_from_def_id(id)).unwrap_or_else(|| {
1206 // Create a *fake* `DefId` out of a `NodeId` by subtracting the `NodeId`
1207 // out of the maximum u32 value. This will work unless you have *billions*
1208 // of definitions in a single crate (very unlikely to actually happen).
1210 krate: LOCAL_CRATE.as_u32(),
1211 index: !id.as_u32(),
1216 fn null_id() -> rls_data::Id {
1218 krate: u32::max_value(),
1219 index: u32::max_value(),
1223 fn lower_attributes(attrs: Vec<Attribute>, scx: &SaveContext) -> Vec<rls_data::Attribute> {
1225 // Only retain real attributes. Doc comments are lowered separately.
1226 .filter(|attr| attr.path != "doc")
1228 // Remove the surrounding '#[..]' or '#![..]' of the pretty printed
1229 // attribute. First normalize all inner attribute (#![..]) to outer
1230 // ones (#[..]), then remove the two leading and the one trailing character.
1231 attr.style = ast::AttrStyle::Outer;
1232 let value = pprust::attribute_to_string(&attr);
1233 // This str slicing works correctly, because the leading and trailing characters
1234 // are in the ASCII range and thus exactly one byte each.
1235 let value = value[2..value.len()-1].to_string();
1237 rls_data::Attribute {
1239 span: scx.span_from_span(attr.span),