Skip to content

save-analysis: some tweaks #34298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/librustc_save_analysis/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ pub struct EnumData {
pub qualname: String,
pub span: Span,
pub scope: NodeId,
pub variants: Vec<NodeId>,

}

/// Data for extern crates.
Expand Down Expand Up @@ -212,6 +214,7 @@ pub struct MethodData {
pub span: Span,
pub scope: NodeId,
pub value: String,
pub decl_id: Option<DefId>,
}

/// Data for modules.
Expand All @@ -223,6 +226,7 @@ pub struct ModData {
pub span: Span,
pub scope: NodeId,
pub filename: String,
pub items: Vec<NodeId>,
}

/// Data for a reference to a module.
Expand All @@ -242,7 +246,8 @@ pub struct StructData {
pub ctor_id: NodeId,
pub qualname: String,
pub scope: NodeId,
pub value: String
pub value: String,
pub fields: Vec<NodeId>,
}

#[derive(Debug, RustcEncodable)]
Expand All @@ -263,7 +268,8 @@ pub struct TraitData {
pub name: String,
pub qualname: String,
pub scope: NodeId,
pub value: String
pub value: String,
pub items: Vec<NodeId>,
}

#[derive(Debug, RustcEncodable)]
Expand Down Expand Up @@ -317,6 +323,7 @@ pub struct UseGlobData {
#[derive(Debug, RustcEncodable)]
pub struct VariableData {
pub id: NodeId,
pub kind: VariableKind,
pub name: String,
pub qualname: String,
pub span: Span,
Expand All @@ -325,6 +332,14 @@ pub struct VariableData {
pub type_value: String,
}

#[derive(Debug, RustcEncodable)]
pub enum VariableKind {
Static,
Const,
Local,
Field,
}

/// Data for the use of some item (e.g., the use of a local variable, which
/// will refer to that variables declaration (by ref_id)).
#[derive(Debug, RustcEncodable)]
Expand Down
71 changes: 48 additions & 23 deletions src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
use rustc::hir::def::Def;
use rustc::hir::def_id::DefId;
use rustc::session::Session;
use rustc::ty::{self, TyCtxt};
use rustc::ty::{self, TyCtxt, ImplOrTraitItem, ImplOrTraitItemContainer};

use std::collections::HashSet;
use std::hash::*;
Expand Down Expand Up @@ -356,6 +356,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
if !self.span.filter_generated(sub_span, p.span) {
self.dumper.variable(VariableData {
id: id,
kind: VariableKind::Local,
span: sub_span.expect("No span found for variable"),
name: path_to_string(p),
qualname: format!("{}::{}", qualname, path_to_string(p)),
Expand All @@ -380,24 +381,42 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {

let sig_str = ::make_signature(&sig.decl, &sig.generics);
if body.is_some() {
if !self.span.filter_generated(Some(method_data.span), span) {
let mut data = method_data.clone();
data.value = sig_str;
self.dumper.function(data.lower(self.tcx));
}
self.process_formals(&sig.decl.inputs, &method_data.qualname);
} else {
if !self.span.filter_generated(Some(method_data.span), span) {
self.dumper.method(MethodData {
id: method_data.id,
name: method_data.name,
span: method_data.span,
scope: method_data.scope,
qualname: method_data.qualname.clone(),
value: sig_str,
}.lower(self.tcx));
}
}

// If the method is defined in an impl, then try and find the corresponding
// method decl in a trait, and if there is one, make a decl_id for it. This
// requires looking up the impl, then the trait, then searching for a method
// with the right name.
if !self.span.filter_generated(Some(method_data.span), span) {
let container =
self.tcx.impl_or_trait_item(self.tcx.map.local_def_id(id)).container();
let decl_id = if let ImplOrTraitItemContainer::ImplContainer(id) = container {
self.tcx.trait_id_of_impl(id).and_then(|id| {
for item in &**self.tcx.trait_items(id) {
if let &ImplOrTraitItem::MethodTraitItem(ref m) = item {
if m.name == name {
return Some(m.def_id);
}
}
}
None
})
} else {
None
};

self.dumper.method(MethodData {
id: method_data.id,
name: method_data.name,
span: method_data.span,
scope: method_data.scope,
qualname: method_data.qualname.clone(),
value: sig_str,
decl_id: decl_id,
}.lower(self.tcx));
}

self.process_generic_params(&sig.generics, span, &method_data.qualname, id);
}

Expand Down Expand Up @@ -519,6 +538,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
if !self.span.filter_generated(sub_span, span) {
self.dumper.variable(VariableData {
span: sub_span.expect("No span found for variable"),
kind: VariableKind::Const,
id: id,
name: name.to_string(),
qualname: qualname,
Expand All @@ -542,17 +562,18 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
let qualname = format!("::{}", self.tcx.node_path_str(item.id));

let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Struct);
let val = if let ast::ItemKind::Struct(ast::VariantData::Struct(ref fields, _), _) =
item.node {
let (val, fields) =
if let ast::ItemKind::Struct(ast::VariantData::Struct(ref fields, _), _) = item.node
{
let fields_str = fields.iter()
.enumerate()
.map(|(i, f)| f.ident.map(|i| i.to_string())
.unwrap_or(i.to_string()))
.collect::<Vec<_>>()
.join(", ");
format!("{} {{ {} }}", name, fields_str)
(format!("{} {{ {} }}", name, fields_str), fields.iter().map(|f| f.id).collect())
} else {
String::new()
(String::new(), vec![])
};

if !self.span.filter_generated(sub_span, item.span) {
Expand All @@ -563,7 +584,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
ctor_id: def.id(),
qualname: qualname.clone(),
scope: self.cur_scope,
value: val
value: val,
fields: fields,
}.lower(self.tcx));
}

Expand Down Expand Up @@ -718,7 +740,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
name: name,
qualname: qualname.clone(),
scope: self.cur_scope,
value: val
value: val,
items: methods.iter().map(|i| i.id).collect(),
}.lower(self.tcx));
}

Expand Down Expand Up @@ -958,6 +981,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
if !self.span.filter_generated(sub_span, p.span) {
self.dumper.variable(VariableData {
span: sub_span.expect("No span found for variable"),
kind: VariableKind::Local,
id: id,
name: path_to_string(p),
qualname: format!("{}${}", path_to_string(p), id),
Expand Down Expand Up @@ -1366,6 +1390,7 @@ impl<'v, 'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'v> for DumpVisitor<'l, 'tcx,
if !self.span.filter_generated(Some(p.span), p.span) {
self.dumper.variable(VariableData {
span: p.span,
kind: VariableKind::Local,
id: id,
name: path_to_string(p),
qualname: format!("{}${}", path_to_string(p), id),
Expand Down
20 changes: 16 additions & 4 deletions src/librustc_save_analysis/external_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc::ty::TyCtxt;
use syntax::ast::{CrateNum, NodeId};
use syntax::codemap::{Span, CodeMap};

use super::data;
use data;

// FIXME: this should be pub(crate), but the current snapshot doesn't allow it yet
pub trait Lower {
Expand Down Expand Up @@ -90,6 +90,7 @@ pub struct EnumData {
pub qualname: String,
pub span: SpanData,
pub scope: DefId,
pub variants: Vec<DefId>
}

impl Lower for data::EnumData {
Expand All @@ -103,6 +104,7 @@ impl Lower for data::EnumData {
qualname: self.qualname,
span: SpanData::from_span(self.span, tcx.sess.codemap()),
scope: make_def_id(self.scope, &tcx.map),
variants: self.variants.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
}
}
}
Expand Down Expand Up @@ -319,6 +321,7 @@ pub struct MethodData {
pub span: SpanData,
pub scope: DefId,
pub value: String,
pub decl_id: Option<DefId>,
}

impl Lower for data::MethodData {
Expand All @@ -332,6 +335,7 @@ impl Lower for data::MethodData {
id: make_def_id(self.id, &tcx.map),
qualname: self.qualname,
value: self.value,
decl_id: self.decl_id,
}
}
}
Expand All @@ -345,6 +349,7 @@ pub struct ModData {
pub span: SpanData,
pub scope: DefId,
pub filename: String,
pub items: Vec<DefId>,
}

impl Lower for data::ModData {
Expand All @@ -358,6 +363,7 @@ impl Lower for data::ModData {
span: SpanData::from_span(self.span, tcx.sess.codemap()),
scope: make_def_id(self.scope, &tcx.map),
filename: self.filename,
items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
}
}
}
Expand Down Expand Up @@ -392,7 +398,8 @@ pub struct StructData {
pub ctor_id: DefId,
pub qualname: String,
pub scope: DefId,
pub value: String
pub value: String,
pub fields: Vec<DefId>,
}

impl Lower for data::StructData {
Expand All @@ -406,7 +413,8 @@ impl Lower for data::StructData {
ctor_id: make_def_id(self.ctor_id, &tcx.map),
qualname: self.qualname,
scope: make_def_id(self.scope, &tcx.map),
value: self.value
value: self.value,
fields: self.fields.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
}
}
}
Expand Down Expand Up @@ -445,7 +453,8 @@ pub struct TraitData {
pub id: DefId,
pub qualname: String,
pub scope: DefId,
pub value: String
pub value: String,
pub items: Vec<DefId>,
}

impl Lower for data::TraitData {
Expand All @@ -459,6 +468,7 @@ impl Lower for data::TraitData {
qualname: self.qualname,
scope: make_def_id(self.scope, &tcx.map),
value: self.value,
items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
}
}
}
Expand Down Expand Up @@ -585,6 +595,7 @@ impl Lower for data::UseGlobData {
pub struct VariableData {
pub id: DefId,
pub name: String,
pub kind: data::VariableKind,
pub qualname: String,
pub span: SpanData,
pub scope: DefId,
Expand All @@ -598,6 +609,7 @@ impl Lower for data::VariableData {
fn lower(self, tcx: TyCtxt) -> VariableData {
VariableData {
id: make_def_id(self.id, &tcx.map),
kind: self.kind,
name: self.name,
qualname: self.qualname,
span: SpanData::from_span(self.span, tcx.sess.codemap()),
Expand Down
Loading