Skip to content

Commit d8263c4

Browse files
committed
add function to retrace a DefPath to a DefId
used after loading state from previous compilation
1 parent a9b6205 commit d8263c4

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/librustc/hir/map/definitions.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,56 @@ impl Definitions {
203203
}
204204
}
205205

206+
pub fn retrace_path(&self, path: &DefPath) -> Option<DefIndex> {
207+
debug!("retrace_path(path={:?})", path);
208+
209+
// we assume that we only want to retrace paths relative to
210+
// the crate root
211+
assert!(path.is_local());
212+
213+
let root_key = DefKey {
214+
parent: None,
215+
disambiguated_data: DisambiguatedDefPathData {
216+
data: DefPathData::CrateRoot,
217+
disambiguator: 0,
218+
},
219+
};
220+
let root_id = self.key_map[&root_key];
221+
222+
debug!("retrace_path: root_id={:?}", root_id);
223+
224+
let mut id = root_id;
225+
for data in &path.data {
226+
let key = DefKey { parent: Some(id), disambiguated_data: data.clone() };
227+
debug!("key = {:?}", key);
228+
id = match self.key_map.get(&key) {
229+
Some(&id) => id,
230+
None => return None
231+
};
232+
}
233+
234+
Some(id)
235+
}
236+
206237
pub fn create_def_with_parent(&mut self,
207238
parent: Option<DefIndex>,
208239
node_id: ast::NodeId,
209240
data: DefPathData)
210241
-> DefIndex {
242+
debug!("create_def_with_parent(parent={:?}, node_id={:?}, data={:?})",
243+
parent, node_id, data);
244+
211245
assert!(!self.node_map.contains_key(&node_id),
212246
"adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
213247
node_id,
214248
data,
215249
self.data[self.node_map[&node_id].as_usize()]);
216250

251+
assert!(parent.is_some() ^ match data {
252+
DefPathData::CrateRoot | DefPathData::InlinedRoot(_) => true,
253+
_ => false,
254+
});
255+
217256
// Find a unique DefKey. This basically means incrementing the disambiguator
218257
// until we get no match.
219258
let mut key = DefKey {
@@ -228,12 +267,17 @@ impl Definitions {
228267
key.disambiguated_data.disambiguator += 1;
229268
}
230269

270+
debug!("create_def_with_parent: after disambiguation, key = {:?}", key);
271+
231272
// Create the definition.
232273
let index = DefIndex::new(self.data.len());
233274
self.data.push(DefData { key: key.clone(), node_id: node_id });
275+
debug!("create_def_with_parent: node_map[{:?}] = {:?}", node_id, index);
234276
self.node_map.insert(node_id, index);
277+
debug!("create_def_with_parent: key_map[{:?}] = {:?}", key, index);
235278
self.key_map.insert(key, index);
236279

280+
237281
index
238282
}
239283
}

src/librustc/hir/map/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ impl<'ast> Map<'ast> {
282282
self.definitions.borrow().def_path(def_id.index)
283283
}
284284

285+
pub fn retrace_path(&self, path: &DefPath) -> Option<DefId> {
286+
self.definitions.borrow().retrace_path(path)
287+
.map(DefId::local)
288+
}
289+
285290
pub fn local_def_id(&self, node: NodeId) -> DefId {
286291
self.opt_local_def_id(node).unwrap_or_else(|| {
287292
bug!("local_def_id: no entry for `{}`, which has a map of `{:?}`",

0 commit comments

Comments
 (0)