Skip to content

Commit

Permalink
Add some ID conversion methods to HIR map and Definitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed Aug 11, 2017
1 parent fbc7398 commit a8cf6cc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/librustc/hir/map/definitions.rs
Expand Up @@ -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();
Expand All @@ -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]
}
Expand All @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions src/librustc/hir/map/mod.rs
Expand Up @@ -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()
}
Expand Down

0 comments on commit a8cf6cc

Please sign in to comment.