Skip to content

Commit

Permalink
save-analysis: factor out helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Jul 9, 2015
1 parent f28f79b commit df5a1ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/librustc_trans/save/dump_csv.rs
Expand Up @@ -822,6 +822,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
struct_lit_data.ref_id,
struct_lit_data.scope);
let struct_def = struct_lit_data.ref_id;
let scope = self.save_ctxt.enclosing_scope(ex.id);

for field in fields {
if generated_code(field.ident.span) {
Expand All @@ -830,7 +831,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {

let field_data = self.save_ctxt.get_field_ref_data(field,
struct_def,
self.cur_scope);
scope);
self.fmt.ref_str(recorder::VarRef,
field.ident.span,
Some(field_data.span),
Expand Down
40 changes: 20 additions & 20 deletions src/librustc_trans/save/mod.rs
Expand Up @@ -194,7 +194,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
qualname: qualname,
declaration: None,
span: sub_span.unwrap(),
scope: self.tcx.map.get_enclosing_scope(item.id).unwrap_or(0),
scope: self.enclosing_scope(item.id),
})
}
ast::ItemStatic(ref typ, mt, ref expr) => {
Expand All @@ -213,7 +213,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
name: get_ident(item.ident).to_string(),
qualname: qualname,
span: sub_span.unwrap(),
scope: self.tcx.map.get_enclosing_scope(item.id).unwrap_or(0),
scope: self.enclosing_scope(item.id),
value: value,
type_value: ty_to_string(&typ),
})
Expand All @@ -227,7 +227,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
name: get_ident(item.ident).to_string(),
qualname: qualname,
span: sub_span.unwrap(),
scope: self.tcx.map.get_enclosing_scope(item.id).unwrap_or(0),
scope: selfenclosing_scope(item.id),
value: self.span_utils.snippet(expr.span),
type_value: ty_to_string(&typ),
})
Expand All @@ -245,7 +245,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
name: get_ident(item.ident).to_string(),
qualname: qualname,
span: sub_span.unwrap(),
scope: self.tcx.map.get_enclosing_scope(item.id).unwrap_or(0),
scope: self.enclosing_scope(item.id),
filename: filename,
})
},
Expand All @@ -259,14 +259,14 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
value: val,
span: sub_span.unwrap(),
qualname: enum_name,
scope: self.tcx.map.get_enclosing_scope(item.id).unwrap_or(0),
scope: self.enclosing_scope(item.id),
})
},
ast::ItemImpl(_, _, _, ref trait_ref, ref typ, _) => {
let mut type_data = None;
let sub_span;

let parent = self.tcx.map.get_enclosing_scope(item.id).unwrap_or(0);
let parent = self.enclosing_scope(item.id);

match typ.node {
// Common case impl for a struct or something basic.
Expand Down Expand Up @@ -303,14 +303,12 @@ 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> {
pub fn get_field_data(&self, field: &ast::StructField, scope: NodeId) -> Option<Data> {
match field.node.kind {
ast::NamedField(ident, _) => {
let name = get_ident(ident);
let qualname = format!("::{}::{}",
self.tcx.map.path_to_string(parent),
self.tcx.map.path_to_string(scope),
name);
let typ = self.tcx.node_types().get(&field.node.id).unwrap()
.to_string();
Expand All @@ -320,7 +318,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
name: get_ident(ident).to_string(),
qualname: qualname,
span: sub_span.unwrap(),
scope: parent,
scope: scope,
value: "".to_owned(),
type_value: typ,
}))
Expand All @@ -329,8 +327,6 @@ 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_trait_ref_data(&self,
trait_ref: &ast::TraitRef,
parent: NodeId)
Expand Down Expand Up @@ -359,7 +355,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
return Some(Data::VariableRefData(VariableRefData {
name: get_ident(ident.node).to_string(),
span: sub_span.unwrap(),
scope: self.tcx.map.get_enclosing_scope(expr.id).unwrap_or(0),
scope: self.enclosing_scope(expr.id),
ref_id: f.id,
}));
}
Expand All @@ -382,7 +378,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
let sub_span = self.span_utils.span_for_last_ident(path.span);
Some(Data::TypeRefData(TypeRefData {
span: sub_span.unwrap(),
scope: self.tcx.map.get_enclosing_scope(expr.id).unwrap_or(0),
scope: self.enclosing_scope(expr.id),
ref_id: def_id,
}))
}
Expand All @@ -402,7 +398,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
ty::TraitContainer(_) => (None, Some(method_id))
};
let sub_span = self.span_utils.sub_span_for_meth_name(expr.span);
let parent = self.tcx.map.get_enclosing_scope(expr.id).unwrap_or(0);
let parent = self.enclosing_scope(expr.id);
Some(Data::MethodCallData(MethodCallData {
span: sub_span.unwrap(),
scope: parent,
Expand Down Expand Up @@ -441,15 +437,15 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
Data::VariableRefData(VariableRefData {
name: self.span_utils.snippet(sub_span.unwrap()),
span: sub_span.unwrap(),
scope: self.tcx.map.get_enclosing_scope(id).unwrap_or(0),
scope: self.enclosing_scope(id),
ref_id: def.def_id(),
})
}
def::DefStruct(def_id) | def::DefTy(def_id, _) => {
Data::TypeRefData(TypeRefData {
span: sub_span.unwrap(),
ref_id: def_id,
scope: self.tcx.map.get_enclosing_scope(id).unwrap_or(0),
scope: self.enclosing_scope(id),
})
}
def::DefMethod(decl_id, provenence) => {
Expand Down Expand Up @@ -484,7 +480,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
};
Data::MethodCallData(MethodCallData {
span: sub_span.unwrap(),
scope: self.tcx.map.get_enclosing_scope(id).unwrap_or(0),
scope: self.enclosing_scope(id),
ref_id: def_id,
decl_id: Some(decl_id),
})
Expand All @@ -493,7 +489,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
Data::FunctionCallData(FunctionCallData {
ref_id: def_id,
span: sub_span.unwrap(),
scope: self.tcx.map.get_enclosing_scope(id).unwrap_or(0),
scope: self.enclosing_scope(id),
})
}
_ => self.tcx.sess.span_bug(path.span,
Expand Down Expand Up @@ -545,6 +541,10 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
}
}

#[inline]
fn enclosing_scope(&self, id: NodeId) -> NodeId {
self.tcx.map.get_enclosing_scope(id).unwrap_or(0)
}
}

// An AST visitor for collecting paths from patterns.
Expand Down

0 comments on commit df5a1ca

Please sign in to comment.