Skip to content

Commit 87b4071

Browse files
Definitions: Split out NodeId <-> DefIndex mapping
1 parent d250169 commit 87b4071

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

src/librustc/hir/map/definitions.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ use syntax::symbol::{Symbol, InternedString};
1818
use ty::TyCtxt;
1919
use util::nodemap::NodeMap;
2020

21+
//! For each definition, we track the following data. A definition
22+
//! here is defined somewhat circularly as "something with a def-id",
23+
//! but it generally corresponds to things like structs, enums, etc.
24+
//! There are also some rather random cases (like const initializer
25+
//! expressions) that are mostly just leftovers.
26+
27+
2128
/// The definition table containing node definitions
2229
#[derive(Clone)]
2330
pub struct Definitions {
24-
data: Vec<DefData>,
31+
data: Vec<DefKey>,
2532
key_map: FxHashMap<DefKey, DefIndex>,
26-
node_map: NodeMap<DefIndex>,
33+
node_to_def_index: NodeMap<DefIndex>,
34+
def_index_to_node: Vec<ast::NodeId>,
2735
}
2836

2937
/// A unique identifier that we can use to lookup a definition
@@ -50,19 +58,6 @@ pub struct DisambiguatedDefPathData {
5058
pub disambiguator: u32
5159
}
5260

53-
/// For each definition, we track the following data. A definition
54-
/// here is defined somewhat circularly as "something with a def-id",
55-
/// but it generally corresponds to things like structs, enums, etc.
56-
/// There are also some rather random cases (like const initializer
57-
/// expressions) that are mostly just leftovers.
58-
#[derive(Clone, Debug)]
59-
pub struct DefData {
60-
pub key: DefKey,
61-
62-
/// Local ID within the HIR.
63-
pub node_id: ast::NodeId,
64-
}
65-
6661
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
6762
pub struct DefPath {
6863
/// the path leading from the crate root to the item
@@ -221,7 +216,8 @@ impl Definitions {
221216
Definitions {
222217
data: vec![],
223218
key_map: FxHashMap(),
224-
node_map: NodeMap(),
219+
node_to_def_index: NodeMap(),
220+
def_index_to_node: vec![],
225221
}
226222
}
227223

@@ -248,7 +244,7 @@ impl Definitions {
248244
}
249245

250246
pub fn opt_def_index(&self, node: ast::NodeId) -> Option<DefIndex> {
251-
self.node_map.get(&node).cloned()
247+
self.node_to_def_index.get(&node).cloned()
252248
}
253249

254250
pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
@@ -262,7 +258,8 @@ impl Definitions {
262258
pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
263259
if def_id.krate == LOCAL_CRATE {
264260
assert!(def_id.index.as_usize() < self.data.len());
265-
Some(self.data[def_id.index.as_usize()].node_id)
261+
// Some(self.data[def_id.index.as_usize()].node_id)
262+
Some(self.def_index_to_node[def_id.index.as_usize()])
266263
} else {
267264
None
268265
}
@@ -277,11 +274,11 @@ impl Definitions {
277274
debug!("create_def_with_parent(parent={:?}, node_id={:?}, data={:?})",
278275
parent, node_id, data);
279276

280-
assert!(!self.node_map.contains_key(&node_id),
277+
assert!(!self.node_to_def_index.contains_key(&node_id),
281278
"adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
282279
node_id,
283280
data,
284-
self.data[self.node_map[&node_id].as_usize()]);
281+
self.data[self.node_to_def_index[&node_id].as_usize()]);
285282

286283
assert!(parent.is_some() ^ match data {
287284
DefPathData::CrateRoot | DefPathData::InlinedRoot(_) => true,
@@ -306,9 +303,10 @@ impl Definitions {
306303

307304
// Create the definition.
308305
let index = DefIndex::new(self.data.len());
309-
self.data.push(DefData { key: key.clone(), node_id: node_id });
310-
debug!("create_def_with_parent: node_map[{:?}] = {:?}", node_id, index);
311-
self.node_map.insert(node_id, index);
306+
self.data.push(DefData { key: key.clone() });
307+
self.def_index_to_node.push(node_id);
308+
debug!("create_def_with_parent: node_to_def_index[{:?}] = {:?}", node_id, index);
309+
self.node_to_def_index.insert(node_id, index);
312310
debug!("create_def_with_parent: key_map[{:?}] = {:?}", key, index);
313311
self.key_map.insert(key, index);
314312

0 commit comments

Comments
 (0)