|
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
|
@@ -45,3 +46,45 @@ pub const CRATE_HIR_ID: HirId = HirId {
|
45 | 46 | owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
|
46 | 47 | local_id: ItemLocalId::from_u32(0),
|
47 | 48 | };
|
| 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