From a8cf6cc6db13efea1057dfefbe9e6b583b23ea4f Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Tue, 8 Aug 2017 14:33:51 +0200 Subject: [PATCH] Add some ID conversion methods to HIR map and Definitions. --- src/librustc/hir/map/definitions.rs | 13 +++++++++++++ src/librustc/hir/map/mod.rs | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/librustc/hir/map/definitions.rs b/src/librustc/hir/map/definitions.rs index 67a4d71d90c1c..b371366bc5d55 100644 --- a/src/librustc/hir/map/definitions.rs +++ b/src/librustc/hir/map/definitions.rs @@ -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 { self.node_to_def_index.get(&node).cloned() } + #[inline] pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option { 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 { 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, diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 3b73647f0eb0e..99ed4736a512d 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -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() }