Skip to content

Commit 6758a85

Browse files
committed
Introduce HirIdVec.
1 parent 625594f commit 6758a85

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

compiler/rustc_hir/src/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
@@ -45,3 +46,45 @@ pub const CRATE_HIR_ID: HirId = HirId {
4546
owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
4647
local_id: ItemLocalId::from_u32(0),
4748
};
49+
50+
#[derive(Clone, Default, Debug, Encodable, Decodable)]
51+
pub struct HirIdVec<T> {
52+
map: IndexVec<LocalDefId, IndexVec<ItemLocalId, T>>,
53+
}
54+
55+
impl<T> HirIdVec<T> {
56+
pub fn push_owner(&mut self, id: LocalDefId) {
57+
self.map.ensure_contains_elem(id, IndexVec::new);
58+
}
59+
60+
pub fn push(&mut self, id: HirId, value: T) {
61+
if id.local_id == ItemLocalId::from_u32(0) {
62+
self.push_owner(id.owner);
63+
}
64+
let submap = &mut self.map[id.owner];
65+
let _ret_id = submap.push(value);
66+
debug_assert_eq!(_ret_id, id.local_id);
67+
}
68+
69+
pub fn get(&self, id: HirId) -> Option<&T> {
70+
self.map.get(id.owner)?.get(id.local_id)
71+
}
72+
73+
pub fn get_owner(&self, id: LocalDefId) -> &IndexVec<ItemLocalId, T> {
74+
&self.map[id]
75+
}
76+
}
77+
78+
impl<T> std::ops::Index<HirId> for HirIdVec<T> {
79+
type Output = T;
80+
81+
fn index(&self, id: HirId) -> &T {
82+
&self.map[id.owner][id.local_id]
83+
}
84+
}
85+
86+
impl<T> std::ops::IndexMut<HirId> for HirIdVec<T> {
87+
fn index_mut(&mut self, id: HirId) -> &mut T {
88+
&mut self.map[id.owner][id.local_id]
89+
}
90+
}

0 commit comments

Comments
 (0)