Skip to content

Commit a8cf6cc

Browse files
Add some ID conversion methods to HIR map and Definitions.
1 parent fbc7398 commit a8cf6cc

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/librustc/hir/map/definitions.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,18 +434,22 @@ impl Definitions {
434434
DefPath::make(LOCAL_CRATE, index, |p| self.def_key(p))
435435
}
436436

437+
#[inline]
437438
pub fn opt_def_index(&self, node: ast::NodeId) -> Option<DefIndex> {
438439
self.node_to_def_index.get(&node).cloned()
439440
}
440441

442+
#[inline]
441443
pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
442444
self.opt_def_index(node).map(DefId::local)
443445
}
444446

447+
#[inline]
445448
pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
446449
self.opt_local_def_id(node).unwrap()
447450
}
448451

452+
#[inline]
449453
pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
450454
if def_id.krate == LOCAL_CRATE {
451455
let space_index = def_id.index.address_space().index();
@@ -461,6 +465,7 @@ impl Definitions {
461465
}
462466
}
463467

468+
#[inline]
464469
pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
465470
self.node_to_hir_id[node_id]
466471
}
@@ -473,6 +478,14 @@ impl Definitions {
473478
.unwrap()
474479
}
475480

481+
#[inline]
482+
pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> hir::HirId {
483+
let space_index = def_index.address_space().index();
484+
let array_index = def_index.as_array_index();
485+
let node_id = self.def_index_to_node[space_index][array_index];
486+
self.node_to_hir_id[node_id]
487+
}
488+
476489
/// Add a definition with a parent definition.
477490
pub fn create_root_def(&mut self,
478491
crate_name: &str,

src/librustc/hir/map/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,16 @@ impl<'hir> Map<'hir> {
401401
self.definitions.node_to_hir_id(node_id)
402402
}
403403

404+
#[inline]
405+
pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> HirId {
406+
self.definitions.def_index_to_hir_id(def_index)
407+
}
408+
409+
#[inline]
410+
pub fn def_index_to_node_id(&self, def_index: DefIndex) -> NodeId {
411+
self.definitions.as_local_node_id(DefId::local(def_index)).unwrap()
412+
}
413+
404414
fn entry_count(&self) -> usize {
405415
self.map.len()
406416
}

0 commit comments

Comments
 (0)