Skip to content

Commit 448ce12

Browse files
committed
Auto merge of #25783 - nrc:save-api-2, r=@huonw
A little more work on the save-analysis API. r? @huonw
2 parents 6a3d55a + b2c8719 commit 448ce12

File tree

2 files changed

+109
-40
lines changed

2 files changed

+109
-40
lines changed

src/librustc_trans/save/dump_csv.rs

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
508508
self.process_formals(&decl.inputs, &fn_data.qualname);
509509
self.process_generic_params(ty_params, item.span, &fn_data.qualname, item.id);
510510
} else {
511-
unreachable!();
511+
self.sess.span_bug(item.span, "expected FunctionData");
512512
}
513513

514514
for arg in &decl.inputs {
@@ -538,7 +538,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
538538
&var_data.type_value,
539539
var_data.scope);
540540
} else {
541-
unreachable!();
541+
self.sess.span_bug(item.span, "expected VariableData");
542542
}
543543

544544
self.visit_ty(&typ);
@@ -768,22 +768,18 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
768768
}
769769

770770
fn process_mod(&mut self,
771-
item: &ast::Item, // The module in question, represented as an item.
772-
m: &ast::Mod) {
773-
let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
774-
775-
let cm = self.sess.codemap();
776-
let filename = cm.span_to_filename(m.inner);
777-
778-
let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Mod);
779-
self.fmt.mod_str(item.span,
780-
sub_span,
781-
item.id,
782-
&qualname[..],
783-
self.cur_scope,
784-
&filename[..]);
785-
786-
self.nest(item.id, |v| visit::walk_mod(v, m));
771+
item: &ast::Item) { // The module in question, represented as an item.
772+
let mod_data = self.save_ctxt.get_item_data(item);
773+
if let super::Data::ModData(mod_data) = mod_data {
774+
self.fmt.mod_str(item.span,
775+
Some(mod_data.span),
776+
mod_data.id,
777+
&mod_data.qualname,
778+
mod_data.scope,
779+
&mod_data.filename);
780+
} else {
781+
self.sess.span_bug(item.span, "expected ModData");
782+
}
787783
}
788784

789785
fn process_path(&mut self,
@@ -1188,7 +1184,10 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DumpCsvVisitor<'l, 'tcx> {
11881184
}
11891185
ast::ItemTrait(_, ref generics, ref trait_refs, ref methods) =>
11901186
self.process_trait(item, generics, trait_refs, methods),
1191-
ast::ItemMod(ref m) => self.process_mod(item, m),
1187+
ast::ItemMod(ref m) => {
1188+
self.process_mod(item);
1189+
self.nest(item.id, |v| visit::walk_mod(v, m));
1190+
}
11921191
ast::ItemTy(ref ty, ref ty_params) => {
11931192
let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
11941193
let value = ty_to_string(&**ty);
@@ -1295,30 +1294,22 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DumpCsvVisitor<'l, 'tcx> {
12951294
ast::ExprStruct(ref path, ref fields, ref base) =>
12961295
self.process_struct_lit(ex, path, fields, base),
12971296
ast::ExprMethodCall(_, _, ref args) => self.process_method_call(ex, args),
1298-
ast::ExprField(ref sub_ex, ident) => {
1297+
ast::ExprField(ref sub_ex, _) => {
12991298
if generated_code(sub_ex.span) {
13001299
return
13011300
}
13021301

1303-
self.visit_expr(&**sub_ex);
1304-
let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty;
1305-
match *ty {
1306-
ty::ty_struct(def_id, _) => {
1307-
let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, def_id);
1308-
for f in &fields {
1309-
if f.name == ident.node.name {
1310-
let sub_span = self.span.span_for_last_ident(ex.span);
1311-
self.fmt.ref_str(recorder::VarRef,
1312-
ex.span,
1313-
sub_span,
1314-
f.id,
1315-
self.cur_scope);
1316-
break;
1317-
}
1318-
}
1319-
}
1320-
_ => self.sess.span_bug(ex.span,
1321-
&format!("Expected struct type, found {:?}", ty)),
1302+
self.visit_expr(&sub_ex);
1303+
1304+
let field_data = self.save_ctxt.get_expr_data(ex);
1305+
if let super::Data::VariableRefData(field_data) = field_data {
1306+
self.fmt.ref_str(recorder::VarRef,
1307+
ex.span,
1308+
Some(field_data.span),
1309+
field_data.ref_id,
1310+
field_data.scope);
1311+
} else {
1312+
self.sess.span_bug(ex.span, "expected VariableRefData");
13221313
}
13231314
},
13241315
ast::ExprTupField(ref sub_ex, idx) => {

src/librustc_trans/save/mod.rs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ pub enum Data {
4949
FunctionData(FunctionData),
5050
/// Data for local and global variables (consts and statics).
5151
VariableData(VariableData),
52+
/// Data for modules.
53+
ModData(ModData),
54+
55+
/// Data for the use of some variable (e.g., the use of a local variable, which
56+
/// will refere to that variables declaration).
57+
VariableRefData(VariableRefData),
5258
}
5359

5460
/// Data for all kinds of functions and methods.
@@ -72,6 +78,26 @@ pub struct VariableData {
7278
pub type_value: String,
7379
}
7480

81+
/// Data for modules.
82+
pub struct ModData {
83+
pub id: NodeId,
84+
pub name: String,
85+
pub qualname: String,
86+
pub span: Span,
87+
pub scope: NodeId,
88+
pub filename: String,
89+
}
90+
91+
/// Data for the use of some item (e.g., the use of a local variable, which
92+
/// will refere to that variables declaration (by ref_id)).
93+
pub struct VariableRefData {
94+
pub name: String,
95+
pub span: Span,
96+
pub scope: NodeId,
97+
pub ref_id: DefId,
98+
}
99+
100+
75101
impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
76102
pub fn new(sess: &'l Session,
77103
analysis: &'l ty::CrateAnalysis<'tcx>,
@@ -97,7 +123,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
97123

98124
pub fn get_item_data(&self, item: &ast::Item) -> Data {
99125
match item.node {
100-
ast::Item_::ItemFn(..) => {
126+
ast::ItemFn(..) => {
101127
let name = self.analysis.ty_cx.map.path_to_string(item.id);
102128
let qualname = format!("::{}", name);
103129
let sub_span = self.span_utils.sub_span_after_keyword(item.span, keywords::Fn);
@@ -146,6 +172,58 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
146172
type_value: ty_to_string(&typ),
147173
})
148174
}
175+
ast::ItemMod(ref m) => {
176+
let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
177+
178+
let cm = self.sess.codemap();
179+
let filename = cm.span_to_filename(m.inner);
180+
181+
let sub_span = self.span_utils.sub_span_after_keyword(item.span, keywords::Mod);
182+
183+
Data::ModData(ModData {
184+
id: item.id,
185+
name: get_ident(item.ident).to_string(),
186+
qualname: qualname,
187+
span: sub_span.unwrap(),
188+
scope: self.analysis.ty_cx.map.get_parent(item.id),
189+
filename: filename,
190+
})
191+
}
192+
_ => {
193+
// FIXME
194+
unimplemented!();
195+
}
196+
}
197+
}
198+
199+
pub fn get_expr_data(&self, expr: &ast::Expr) -> Data {
200+
match expr.node {
201+
ast::ExprField(ref sub_ex, ident) => {
202+
let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, &sub_ex).sty;
203+
match *ty {
204+
ty::ty_struct(def_id, _) => {
205+
let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, def_id);
206+
for f in &fields {
207+
if f.name == ident.node.name {
208+
let sub_span = self.span_utils.span_for_last_ident(expr.span);
209+
return Data::VariableRefData(VariableRefData {
210+
name: get_ident(ident.node).to_string(),
211+
span: sub_span.unwrap(),
212+
scope: self.analysis.ty_cx.map.get_parent(expr.id),
213+
ref_id: f.id,
214+
});
215+
}
216+
}
217+
218+
self.sess.span_bug(expr.span,
219+
&format!("Couldn't find field {} on {:?}",
220+
&get_ident(ident.node),
221+
ty))
222+
}
223+
_ => self.sess.span_bug(expr.span,
224+
&format!("Expected struct type, found {:?}", ty)),
225+
}
226+
}
149227
_ => {
150228
// FIXME
151229
unimplemented!();

0 commit comments

Comments
 (0)