]> git.lizzy.rs Git - rust.git/commitdiff
Definitions: Split out NodeId <-> DefIndex mapping
authorMichael Woerister <michaelwoerister@posteo.net>
Tue, 13 Dec 2016 22:48:52 +0000 (17:48 -0500)
committerMichael Woerister <michaelwoerister@posteo.net>
Sat, 17 Dec 2016 00:13:18 +0000 (19:13 -0500)
src/librustc/hir/map/definitions.rs

index a684563512da652a726785817238d02efda070c5..72c1ff9ee2efcae3ba99b3a4e58a329619028e0c 100644 (file)
 use ty::TyCtxt;
 use util::nodemap::NodeMap;
 
+//! For each definition, we track the following data.  A definition
+//! here is defined somewhat circularly as "something with a def-id",
+//! but it generally corresponds to things like structs, enums, etc.
+//! There are also some rather random cases (like const initializer
+//! expressions) that are mostly just leftovers.
+
+
 /// The definition table containing node definitions
 #[derive(Clone)]
 pub struct Definitions {
-    data: Vec<DefData>,
+    data: Vec<DefKey>,
     key_map: FxHashMap<DefKey, DefIndex>,
-    node_map: NodeMap<DefIndex>,
+    node_to_def_index: NodeMap<DefIndex>,
+    def_index_to_node: Vec<ast::NodeId>,
 }
 
 /// A unique identifier that we can use to lookup a definition
@@ -50,19 +58,6 @@ pub struct DisambiguatedDefPathData {
     pub disambiguator: u32
 }
 
-/// For each definition, we track the following data.  A definition
-/// here is defined somewhat circularly as "something with a def-id",
-/// but it generally corresponds to things like structs, enums, etc.
-/// There are also some rather random cases (like const initializer
-/// expressions) that are mostly just leftovers.
-#[derive(Clone, Debug)]
-pub struct DefData {
-    pub key: DefKey,
-
-    /// Local ID within the HIR.
-    pub node_id: ast::NodeId,
-}
-
 #[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
 pub struct DefPath {
     /// the path leading from the crate root to the item
@@ -221,7 +216,8 @@ pub fn new() -> Definitions {
         Definitions {
             data: vec![],
             key_map: FxHashMap(),
-            node_map: NodeMap(),
+            node_to_def_index: NodeMap(),
+            def_index_to_node: vec![],
         }
     }
 
@@ -248,7 +244,7 @@ pub fn def_path(&self, index: DefIndex) -> DefPath {
     }
 
     pub fn opt_def_index(&self, node: ast::NodeId) -> Option<DefIndex> {
-        self.node_map.get(&node).cloned()
+        self.node_to_def_index.get(&node).cloned()
     }
 
     pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
@@ -262,7 +258,8 @@ pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
     pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
         if def_id.krate == LOCAL_CRATE {
             assert!(def_id.index.as_usize() < self.data.len());
-            Some(self.data[def_id.index.as_usize()].node_id)
+            // Some(self.data[def_id.index.as_usize()].node_id)
+            Some(self.def_index_to_node[def_id.index.as_usize()])
         } else {
             None
         }
@@ -277,11 +274,11 @@ pub fn create_def_with_parent(&mut self,
         debug!("create_def_with_parent(parent={:?}, node_id={:?}, data={:?})",
                parent, node_id, data);
 
-        assert!(!self.node_map.contains_key(&node_id),
+        assert!(!self.node_to_def_index.contains_key(&node_id),
                 "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
                 node_id,
                 data,
-                self.data[self.node_map[&node_id].as_usize()]);
+                self.data[self.node_to_def_index[&node_id].as_usize()]);
 
         assert!(parent.is_some() ^ match data {
             DefPathData::CrateRoot | DefPathData::InlinedRoot(_) => true,
@@ -306,9 +303,10 @@ pub fn create_def_with_parent(&mut self,
 
         // Create the definition.
         let index = DefIndex::new(self.data.len());
-        self.data.push(DefData { key: key.clone(), node_id: node_id });
-        debug!("create_def_with_parent: node_map[{:?}] = {:?}", node_id, index);
-        self.node_map.insert(node_id, index);
+        self.data.push(DefData { key: key.clone() });
+        self.def_index_to_node.push(node_id);
+        debug!("create_def_with_parent: node_to_def_index[{:?}] = {:?}", node_id, index);
+        self.node_to_def_index.insert(node_id, index);
         debug!("create_def_with_parent: key_map[{:?}] = {:?}", key, index);
         self.key_map.insert(key, index);