Skip to content

save-analysis: normalise node ids before emitting. #29040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/librustc_trans/save/dump_csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub struct DumpCsvVisitor<'l, 'tcx: 'l> {
analysis: &'l ty::CrateAnalysis,

span: SpanUtils<'l>,
fmt: FmtStrs<'l>,
fmt: FmtStrs<'l, 'tcx>,

cur_scope: NodeId,
}
Expand All @@ -91,7 +91,8 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
out: output_file,
dump_spans: false,
},
span_utils),
span_utils,
tcx),
cur_scope: 0,
}
}
Expand Down
66 changes: 63 additions & 3 deletions src/librustc_trans/save/recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use super::span_utils::SpanUtils;

use metadata::cstore::LOCAL_CRATE;
use middle::def_id::{CRATE_DEF_INDEX, DefId};
use middle::ty;

use std::io::Write;

Expand Down Expand Up @@ -51,9 +52,10 @@ impl Recorder {
}
}

pub struct FmtStrs<'a> {
pub struct FmtStrs<'a, 'tcx: 'a> {
pub recorder: Box<Recorder>,
span: SpanUtils<'a>,
tcx: &'a ty::ctxt<'tcx>,
}

macro_rules! s { ($e:expr) => { format!("{}", $e) }}
Expand Down Expand Up @@ -96,11 +98,29 @@ pub enum Row {
FnRef,
}

impl<'a> FmtStrs<'a> {
pub fn new(rec: Box<Recorder>, span: SpanUtils<'a>) -> FmtStrs<'a> {
impl<'a, 'tcx: 'a> FmtStrs<'a, 'tcx> {
pub fn new(rec: Box<Recorder>,
span: SpanUtils<'a>,
tcx: &'a ty::ctxt<'tcx>)
-> FmtStrs<'a, 'tcx> {
FmtStrs {
recorder: rec,
span: span,
tcx: tcx,
}
}

// Emitted ids are used to cross-reference items across crates. DefIds and
// NodeIds do not usually correspond in any way. The strategy is to use the
// index from the DefId as a crate-local id. However, within a crate, DefId
// indices and NodeIds can overlap. So, we must adjust the NodeIds. If an
// item can be identified by a DefId as well as a NodeId, then we use the
// DefId index as the id. If it can't, then we have to use the NodeId, but
// need to adjust it so it will not clash with any possible DefId index.
fn normalize_node_id(&self, id: NodeId) -> usize {
match self.tcx.map.opt_local_def_id(id) {
Some(id) => id.index.as_usize(),
None => id as usize + self.tcx.map.num_local_def_ids()
}
}

Expand Down Expand Up @@ -321,6 +341,7 @@ impl<'a> FmtStrs<'a> {
let mut qualname = String::from(name);
qualname.push_str("$");
qualname.push_str(&id.to_string());
let id = self.normalize_node_id(id);
self.check_and_record(Variable,
span,
sub_span,
Expand All @@ -338,6 +359,7 @@ impl<'a> FmtStrs<'a> {
let mut qualname = String::from(fn_name);
qualname.push_str("::");
qualname.push_str(name);
let id = self.normalize_node_id(id);
self.check_and_record(Variable,
span,
sub_span,
Expand All @@ -354,6 +376,8 @@ impl<'a> FmtStrs<'a> {
value: &str,
typ: &str,
scope_id: NodeId) {
let id = self.normalize_node_id(id);
let scope_id = self.normalize_node_id(scope_id);
self.check_and_record(Variable,
span,
sub_span,
Expand All @@ -368,6 +392,8 @@ impl<'a> FmtStrs<'a> {
qualname: &str,
typ: &str,
scope_id: NodeId) {
let id = self.normalize_node_id(id);
let scope_id = self.normalize_node_id(scope_id);
self.check_and_record(Variable,
span,
sub_span,
Expand All @@ -381,6 +407,8 @@ impl<'a> FmtStrs<'a> {
name: &str,
scope_id: NodeId,
value: &str) {
let id = self.normalize_node_id(id);
let scope_id = self.normalize_node_id(scope_id);
self.check_and_record(Enum, span, sub_span, svec!(id, name, scope_id, value));
}

Expand All @@ -393,6 +421,8 @@ impl<'a> FmtStrs<'a> {
typ: &str,
val: &str,
scope_id: NodeId) {
let id = self.normalize_node_id(id);
let scope_id = self.normalize_node_id(scope_id);
self.check_and_record(Variant,
span,
sub_span,
Expand All @@ -408,6 +438,9 @@ impl<'a> FmtStrs<'a> {
typ: &str,
val: &str,
scope_id: NodeId) {
let id = self.normalize_node_id(id);
let scope_id = self.normalize_node_id(scope_id);
let ctor_id = self.normalize_node_id(ctor_id);
self.check_and_record(VariantStruct,
span,
sub_span,
Expand All @@ -420,6 +453,8 @@ impl<'a> FmtStrs<'a> {
id: NodeId,
name: &str,
scope_id: NodeId) {
let id = self.normalize_node_id(id);
let scope_id = self.normalize_node_id(scope_id);
self.check_and_record(Function,
span,
sub_span,
Expand All @@ -433,6 +468,8 @@ impl<'a> FmtStrs<'a> {
name: &str,
decl_id: Option<DefId>,
scope_id: NodeId) {
let id = self.normalize_node_id(id);
let scope_id = self.normalize_node_id(scope_id);
let values = match decl_id {
Some(decl_id) => svec!(id,
name,
Expand All @@ -450,6 +487,8 @@ impl<'a> FmtStrs<'a> {
id: NodeId,
name: &str,
scope_id: NodeId) {
let id = self.normalize_node_id(id);
let scope_id = self.normalize_node_id(scope_id);
self.check_and_record(MethodDecl, span, sub_span, svec!(id, name, scope_id));
}

Expand All @@ -461,6 +500,9 @@ impl<'a> FmtStrs<'a> {
name: &str,
scope_id: NodeId,
value: &str) {
let id = self.normalize_node_id(id);
let scope_id = self.normalize_node_id(scope_id);
let ctor_id = self.normalize_node_id(ctor_id);
self.check_and_record(Struct,
span,
sub_span,
Expand All @@ -474,6 +516,8 @@ impl<'a> FmtStrs<'a> {
name: &str,
scope_id: NodeId,
value: &str) {
let id = self.normalize_node_id(id);
let scope_id = self.normalize_node_id(scope_id);
self.check_and_record(Trait, span, sub_span, svec!(id, name, scope_id, value));
}

Expand All @@ -484,6 +528,8 @@ impl<'a> FmtStrs<'a> {
ref_id: Option<DefId>,
trait_id: Option<DefId>,
scope_id: NodeId) {
let id = self.normalize_node_id(id);
let scope_id = self.normalize_node_id(scope_id);
let ref_id = ref_id.unwrap_or(CRATE_ROOT_DEF_ID);
let trait_id = trait_id.unwrap_or(CRATE_ROOT_DEF_ID);
self.check_and_record(Impl,
Expand All @@ -504,6 +550,8 @@ impl<'a> FmtStrs<'a> {
name: &str,
parent: NodeId,
filename: &str) {
let id = self.normalize_node_id(id);
let parent = self.normalize_node_id(parent);
self.check_and_record(Module,
span,
sub_span,
Expand All @@ -517,6 +565,8 @@ impl<'a> FmtStrs<'a> {
mod_id: Option<DefId>,
name: &str,
parent: NodeId) {
let id = self.normalize_node_id(id);
let parent = self.normalize_node_id(parent);
let mod_id = mod_id.unwrap_or(CRATE_ROOT_DEF_ID);
self.check_and_record(UseAlias,
span,
Expand All @@ -530,6 +580,8 @@ impl<'a> FmtStrs<'a> {
id: NodeId,
values: &str,
parent: NodeId) {
let id = self.normalize_node_id(id);
let parent = self.normalize_node_id(parent);
self.check_and_record(UseGlob, span, sub_span, svec!(id, values, parent));
}

Expand All @@ -541,6 +593,8 @@ impl<'a> FmtStrs<'a> {
name: &str,
loc: &str,
parent: NodeId) {
let id = self.normalize_node_id(id);
let parent = self.normalize_node_id(parent);
self.check_and_record(ExternCrate,
span,
sub_span,
Expand All @@ -552,6 +606,7 @@ impl<'a> FmtStrs<'a> {
sub_span: Option<Span>,
base_id: DefId,
deriv_id: NodeId) {
let deriv_id = self.normalize_node_id(deriv_id);
self.check_and_record(Inheritance,
span,
sub_span,
Expand All @@ -563,6 +618,7 @@ impl<'a> FmtStrs<'a> {
sub_span: Option<Span>,
id: DefId,
scope_id: NodeId) {
let scope_id = self.normalize_node_id(scope_id);
self.check_and_record(FnCall,
span,
sub_span,
Expand All @@ -575,6 +631,7 @@ impl<'a> FmtStrs<'a> {
defid: Option<DefId>,
declid: Option<DefId>,
scope_id: NodeId) {
let scope_id = self.normalize_node_id(scope_id);
let defid = defid.unwrap_or(CRATE_ROOT_DEF_ID);
let (dcn, dck) = match declid {
Some(declid) => (s!(declid.index.as_usize()), s!(declid.krate)),
Expand All @@ -587,6 +644,7 @@ impl<'a> FmtStrs<'a> {
}

pub fn sub_mod_ref_str(&mut self, span: Span, sub_span: Span, qualname: &str, parent: NodeId) {
let parent = self.normalize_node_id(parent);
self.record_with_span(ModRef, span, sub_span, svec!(0, 0, qualname, parent));
}

Expand All @@ -596,6 +654,7 @@ impl<'a> FmtStrs<'a> {
id: NodeId,
qualname: &str,
value: &str) {
let id = self.normalize_node_id(id);
self.check_and_record(Typedef, span, sub_span, svec!(id, qualname, value));
}

Expand All @@ -621,6 +680,7 @@ impl<'a> FmtStrs<'a> {
sub_span: Option<Span>,
id: DefId,
scope_id: NodeId) {
let scope_id = self.normalize_node_id(scope_id);
self.check_and_record(kind,
span,
sub_span,
Expand Down