save-analysis: move fields to the API

This commit is contained in:
Nick Cameron 2015-06-05 16:03:48 +12:00
parent cc44423566
commit abe5f7b95a
2 changed files with 47 additions and 28 deletions

View file

@ -437,30 +437,20 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
fn process_struct_field_def(&mut self,
field: &ast::StructField,
qualname: &str,
scope_id: NodeId) {
match field.node.kind {
ast::NamedField(ident, _) => {
let name = get_ident(ident);
let qualname = format!("{}::{}", qualname, name);
let typ =
ppaux::ty_to_string(
&self.analysis.ty_cx,
*self.analysis.ty_cx.node_types().get(&field.node.id).unwrap());
match self.span.sub_span_before_token(field.span, token::Colon) {
Some(sub_span) => self.fmt.field_str(field.span,
Some(sub_span),
field.node.id,
&name,
&qualname,
&typ,
scope_id),
None => self.sess.span_bug(field.span,
&format!("Could not find sub-span for field {}",
qualname)),
}
},
_ => (),
parent_id: NodeId) {
let field_data = self.save_ctxt.get_field_data(field, parent_id);
if let Some(field_data) = field_data {
if let super::Data::VariableData(field_data) = field_data {
self.fmt.field_str(field.span,
Some(field_data.span),
field_data.id,
&field_data.name,
&field_data.qualname,
&field_data.type_value,
field_data.scope);
} else {
self.sess.span_bug(field.span, "expected VariableData");
}
}
}
@ -593,8 +583,8 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
// fields
for field in &def.fields {
self.process_struct_field_def(field, &qualname, item.id);
self.visit_ty(&*field.node.ty);
self.process_struct_field_def(field, item.id);
self.visit_ty(&field.node.ty);
}
self.process_generic_params(ty_params, item.span, &qualname, item.id);
@ -648,7 +638,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
item.id);
for field in &struct_def.fields {
self.process_struct_field_def(field, &qualname, variant.node.id);
self.process_struct_field_def(field, variant.node.id);
self.visit_ty(&*field.node.ty);
}
}

View file

@ -23,9 +23,11 @@ use syntax::parse::token::{self, get_ident, keywords};
use syntax::visit::{self, Visitor};
use syntax::print::pprust::ty_to_string;
use util::ppaux;
use self::span_utils::SpanUtils;
mod span_utils;
mod recorder;
@ -47,7 +49,7 @@ pub struct CrateData {
pub enum Data {
/// Data for all kinds of functions and methods.
FunctionData(FunctionData),
/// Data for local and global variables (consts and statics).
/// Data for local and global variables (consts and statics), and fields.
VariableData(VariableData),
/// Data for modules.
ModData(ModData),
@ -218,6 +220,33 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
}
}
// FIXME: we ought to be able to get the parent id ourselves, but we can't
// for now.
pub fn get_field_data(&self, field: &ast::StructField, parent: NodeId) -> Option<Data> {
match field.node.kind {
ast::NamedField(ident, _) => {
let name = get_ident(ident);
let qualname = format!("::{}::{}",
self.analysis.ty_cx.map.path_to_string(parent),
name);
let typ = ppaux::ty_to_string(&self.analysis.ty_cx,
*self.analysis.ty_cx.node_types()
.get(&field.node.id).unwrap());
let sub_span = self.span_utils.sub_span_before_token(field.span, token::Colon);
Some(Data::VariableData(VariableData {
id: field.node.id,
name: get_ident(ident).to_string(),
qualname: qualname,
span: sub_span.unwrap(),
scope: parent,
value: "".to_owned(),
type_value: typ,
}))
},
_ => None,
}
}
pub fn get_expr_data(&self, expr: &ast::Expr) -> Data {
match expr.node {
ast::ExprField(ref sub_ex, ident) => {