Skip to content

Commit 788fddd

Browse files
committed
save-analysis: API-ify struct lits
1 parent 04b32ce commit 788fddd

File tree

2 files changed

+65
-33
lines changed

2 files changed

+65
-33
lines changed

src/librustc_trans/save/dump_csv.rs

+23-32
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
708708
&val);
709709

710710
// super-traits
711-
for super_bound in &trait_refs {
711+
for super_bound in trait_refs.iter() {
712712
let trait_ref = match *super_bound {
713713
ast::TraitTyParamBound(ref trait_ref, _) => {
714714
trait_ref
@@ -882,44 +882,35 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
882882

883883
self.write_sub_paths_truncated(path, false);
884884

885-
let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, ex).sty;
886-
let struct_def = match *ty {
887-
ty::TyStruct(def_id, _) => {
888-
let sub_span = self.span.span_for_last_ident(path.span);
889-
self.fmt.ref_str(recorder::TypeRef,
890-
path.span,
891-
sub_span,
892-
def_id,
893-
self.cur_scope);
894-
Some(def_id)
895-
}
896-
_ => None
885+
let struct_lit_data = self.save_ctxt.get_expr_data(ex);
886+
let struct_def = if let super::Data::TypeRefData(struct_lit_data) = struct_lit_data {
887+
self.fmt.ref_str(recorder::TypeRef,
888+
ex.span,
889+
Some(struct_lit_data.span),
890+
struct_lit_data.ref_id,
891+
struct_lit_data.scope);
892+
struct_lit_data.ref_id
893+
} else {
894+
self.sess.span_bug(ex.span, "expected TypeRefData");
897895
};
898896

899897
for field in fields {
900-
match struct_def {
901-
Some(struct_def) => {
902-
let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, struct_def);
903-
for f in &fields {
904-
if generated_code(field.ident.span) {
905-
continue;
906-
}
907-
if f.name == field.ident.node.name {
908-
// We don't really need a sub-span here, but no harm done
909-
let sub_span = self.span.span_for_last_ident(field.ident.span);
910-
self.fmt.ref_str(recorder::VarRef,
911-
field.ident.span,
912-
sub_span,
913-
f.id,
914-
self.cur_scope);
915-
}
916-
}
917-
}
918-
None => {}
898+
if generated_code(field.ident.span) {
899+
continue;
919900
}
920901

902+
let field_data = self.save_ctxt.get_field_ref_data(field,
903+
struct_def,
904+
self.cur_scope);
905+
self.fmt.ref_str(recorder::VarRef,
906+
field.ident.span,
907+
Some(field_data.span),
908+
field_data.ref_id,
909+
field_data.scope);
910+
921911
self.visit_expr(&field.expr)
922912
}
913+
923914
visit::walk_expr_opt(self, base)
924915
}
925916

src/librustc_trans/save/mod.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,54 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
349349
&format!("Expected struct type, found {:?}", ty)),
350350
}
351351
}
352+
ast::ExprStruct(ref path, _, _) => {
353+
let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, expr).sty;
354+
match *ty {
355+
ty::ty_struct(def_id, _) => {
356+
let sub_span = self.span_utils.span_for_last_ident(path.span);
357+
Data::TypeRefData(TypeRefData {
358+
span: sub_span.unwrap(),
359+
scope: self.analysis.ty_cx.map.get_parent(expr.id),
360+
ref_id: def_id,
361+
})
362+
}
363+
_ => {
364+
self.sess.span_bug(expr.span,
365+
&format!("expected ty_struct, found {:?}", ty));
366+
}
367+
}
368+
}
352369
_ => {
353370
// FIXME
354371
unimplemented!();
355372
}
356373
}
357374
}
358375

376+
pub fn get_field_ref_data(&self,
377+
field_ref: &ast::Field,
378+
struct_id: DefId,
379+
parent: NodeId)
380+
-> VariableRefData {
381+
let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, struct_id);
382+
let field_name = get_ident(field_ref.ident.node).to_string();
383+
for f in &fields {
384+
if f.name == field_ref.ident.node.name {
385+
// We don't really need a sub-span here, but no harm done
386+
let sub_span = self.span_utils.span_for_last_ident(field_ref.ident.span);
387+
return VariableRefData {
388+
name: field_name,
389+
span: sub_span.unwrap(),
390+
scope: parent,
391+
ref_id: f.id,
392+
};
393+
}
394+
}
395+
396+
self.sess.span_bug(field_ref.span,
397+
&format!("Couldn't find field {}", field_name));
398+
}
399+
359400
pub fn get_data_for_id(&self, _id: &NodeId) -> Data {
360401
// FIXME
361402
unimplemented!();
@@ -400,7 +441,7 @@ impl<'v> Visitor<'v> for PathCollector {
400441
self.collected_paths.push((p.id,
401442
path.clone(),
402443
ast::MutMutable,
403-
recorder::StructRef));
444+
recorder::TypeRef));
404445
}
405446
ast::PatEnum(ref path, _) |
406447
ast::PatQPath(_, ref path) => {

0 commit comments

Comments
 (0)