Skip to content

Commit abe5f7b

Browse files
committed
save-analysis: move fields to the API
1 parent cc44423 commit abe5f7b

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

src/librustc_trans/save/dump_csv.rs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -437,30 +437,20 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
437437

438438
fn process_struct_field_def(&mut self,
439439
field: &ast::StructField,
440-
qualname: &str,
441-
scope_id: NodeId) {
442-
match field.node.kind {
443-
ast::NamedField(ident, _) => {
444-
let name = get_ident(ident);
445-
let qualname = format!("{}::{}", qualname, name);
446-
let typ =
447-
ppaux::ty_to_string(
448-
&self.analysis.ty_cx,
449-
*self.analysis.ty_cx.node_types().get(&field.node.id).unwrap());
450-
match self.span.sub_span_before_token(field.span, token::Colon) {
451-
Some(sub_span) => self.fmt.field_str(field.span,
452-
Some(sub_span),
453-
field.node.id,
454-
&name,
455-
&qualname,
456-
&typ,
457-
scope_id),
458-
None => self.sess.span_bug(field.span,
459-
&format!("Could not find sub-span for field {}",
460-
qualname)),
461-
}
462-
},
463-
_ => (),
440+
parent_id: NodeId) {
441+
let field_data = self.save_ctxt.get_field_data(field, parent_id);
442+
if let Some(field_data) = field_data {
443+
if let super::Data::VariableData(field_data) = field_data {
444+
self.fmt.field_str(field.span,
445+
Some(field_data.span),
446+
field_data.id,
447+
&field_data.name,
448+
&field_data.qualname,
449+
&field_data.type_value,
450+
field_data.scope);
451+
} else {
452+
self.sess.span_bug(field.span, "expected VariableData");
453+
}
464454
}
465455
}
466456

@@ -593,8 +583,8 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
593583

594584
// fields
595585
for field in &def.fields {
596-
self.process_struct_field_def(field, &qualname, item.id);
597-
self.visit_ty(&*field.node.ty);
586+
self.process_struct_field_def(field, item.id);
587+
self.visit_ty(&field.node.ty);
598588
}
599589

600590
self.process_generic_params(ty_params, item.span, &qualname, item.id);
@@ -648,7 +638,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
648638
item.id);
649639

650640
for field in &struct_def.fields {
651-
self.process_struct_field_def(field, &qualname, variant.node.id);
641+
self.process_struct_field_def(field, variant.node.id);
652642
self.visit_ty(&*field.node.ty);
653643
}
654644
}

src/librustc_trans/save/mod.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ use syntax::parse::token::{self, get_ident, keywords};
2323
use syntax::visit::{self, Visitor};
2424
use syntax::print::pprust::ty_to_string;
2525

26+
use util::ppaux;
2627

2728
use self::span_utils::SpanUtils;
2829

30+
2931
mod span_utils;
3032
mod recorder;
3133

@@ -47,7 +49,7 @@ pub struct CrateData {
4749
pub enum Data {
4850
/// Data for all kinds of functions and methods.
4951
FunctionData(FunctionData),
50-
/// Data for local and global variables (consts and statics).
52+
/// Data for local and global variables (consts and statics), and fields.
5153
VariableData(VariableData),
5254
/// Data for modules.
5355
ModData(ModData),
@@ -218,6 +220,33 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
218220
}
219221
}
220222

223+
// FIXME: we ought to be able to get the parent id ourselves, but we can't
224+
// for now.
225+
pub fn get_field_data(&self, field: &ast::StructField, parent: NodeId) -> Option<Data> {
226+
match field.node.kind {
227+
ast::NamedField(ident, _) => {
228+
let name = get_ident(ident);
229+
let qualname = format!("::{}::{}",
230+
self.analysis.ty_cx.map.path_to_string(parent),
231+
name);
232+
let typ = ppaux::ty_to_string(&self.analysis.ty_cx,
233+
*self.analysis.ty_cx.node_types()
234+
.get(&field.node.id).unwrap());
235+
let sub_span = self.span_utils.sub_span_before_token(field.span, token::Colon);
236+
Some(Data::VariableData(VariableData {
237+
id: field.node.id,
238+
name: get_ident(ident).to_string(),
239+
qualname: qualname,
240+
span: sub_span.unwrap(),
241+
scope: parent,
242+
value: "".to_owned(),
243+
type_value: typ,
244+
}))
245+
},
246+
_ => None,
247+
}
248+
}
249+
221250
pub fn get_expr_data(&self, expr: &ast::Expr) -> Data {
222251
match expr.node {
223252
ast::ExprField(ref sub_ex, ident) => {

0 commit comments

Comments
 (0)