Skip to content

Commit 76a36df

Browse files
committed
Introduce HirIdVec.
1 parent 3603d31 commit 76a36df

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/librustc_hir/hir_id.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::def_id::{LocalDefId, CRATE_DEF_INDEX};
2+
use rustc_index::vec::IndexVec;
23
use std::fmt;
34

45
/// Uniquely identifies a node in the HIR of the current crate. It is
@@ -46,3 +47,45 @@ pub const CRATE_HIR_ID: HirId = HirId {
4647
};
4748

4849
pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX;
50+
51+
#[derive(Clone, Default, Debug, RustcEncodable, RustcDecodable)]
52+
pub struct HirIdVec<T> {
53+
map: IndexVec<LocalDefId, IndexVec<ItemLocalId, T>>,
54+
}
55+
56+
impl<T> HirIdVec<T> {
57+
pub fn push_owner(&mut self, id: LocalDefId) {
58+
self.map.ensure_contains_elem(id, IndexVec::new);
59+
}
60+
61+
pub fn push(&mut self, id: HirId, value: T) {
62+
if id.local_id == ItemLocalId::from_u32(0) {
63+
self.push_owner(id.owner);
64+
}
65+
let submap = &mut self.map[id.owner];
66+
let _ret_id = submap.push(value);
67+
debug_assert_eq!(_ret_id, id.local_id);
68+
}
69+
70+
pub fn get(&self, id: HirId) -> Option<&T> {
71+
self.map.get(id.owner)?.get(id.local_id)
72+
}
73+
74+
pub fn get_owner(&self, id: LocalDefId) -> &IndexVec<ItemLocalId, T> {
75+
&self.map[id]
76+
}
77+
}
78+
79+
impl<T> std::ops::Index<HirId> for HirIdVec<T> {
80+
type Output = T;
81+
82+
fn index(&self, id: HirId) -> &T {
83+
&self.map[id.owner][id.local_id]
84+
}
85+
}
86+
87+
impl<T> std::ops::IndexMut<HirId> for HirIdVec<T> {
88+
fn index_mut(&mut self, id: HirId) -> &mut T {
89+
&mut self.map[id.owner][id.local_id]
90+
}
91+
}

0 commit comments

Comments
 (0)