Add some ID conversion methods to HIR map and Definitions.

This commit is contained in:
Michael Woerister 2017-08-08 14:33:51 +02:00
parent fbc7398bad
commit a8cf6cc6db
2 changed files with 23 additions and 0 deletions

View File

@ -434,18 +434,22 @@ impl Definitions {
DefPath::make(LOCAL_CRATE, index, |p| self.def_key(p))
}
#[inline]
pub fn opt_def_index(&self, node: ast::NodeId) -> Option<DefIndex> {
self.node_to_def_index.get(&node).cloned()
}
#[inline]
pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
self.opt_def_index(node).map(DefId::local)
}
#[inline]
pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
self.opt_local_def_id(node).unwrap()
}
#[inline]
pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
if def_id.krate == LOCAL_CRATE {
let space_index = def_id.index.address_space().index();
@ -461,6 +465,7 @@ impl Definitions {
}
}
#[inline]
pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
self.node_to_hir_id[node_id]
}
@ -473,6 +478,14 @@ impl Definitions {
.unwrap()
}
#[inline]
pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> hir::HirId {
let space_index = def_index.address_space().index();
let array_index = def_index.as_array_index();
let node_id = self.def_index_to_node[space_index][array_index];
self.node_to_hir_id[node_id]
}
/// Add a definition with a parent definition.
pub fn create_root_def(&mut self,
crate_name: &str,

View File

@ -401,6 +401,16 @@ impl<'hir> Map<'hir> {
self.definitions.node_to_hir_id(node_id)
}
#[inline]
pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> HirId {
self.definitions.def_index_to_hir_id(def_index)
}
#[inline]
pub fn def_index_to_node_id(&self, def_index: DefIndex) -> NodeId {
self.definitions.as_local_node_id(DefId::local(def_index)).unwrap()
}
fn entry_count(&self) -> usize {
self.map.len()
}