|
1 | 1 | use crate::def_id::{LocalDefId, CRATE_DEF_INDEX};
|
| 2 | +use rustc_index::vec::IndexVec; |
2 | 3 | use std::fmt;
|
3 | 4 |
|
4 | 5 | /// Uniquely identifies a node in the HIR of the current crate. It is
|
@@ -46,3 +47,45 @@ pub const CRATE_HIR_ID: HirId = HirId {
|
46 | 47 | };
|
47 | 48 |
|
48 | 49 | 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