Skip to content

Commit cc98dfc

Browse files
authored
Rollup merge of #40518 - michaelwoerister:hir-id, r=eddyb
Introduce HirId, a replacement for ast::NodeId after lowering to HIR This is the first step towards implementing #40303. This PR introduces the `HirId` type and generates a `HirId` for everything that would be assigned one (i.e. stuff in the HIR), but the HIR data types still use `NodeId` for now. Changing that is a big refactoring that I want to do in a separate PR. A `HirId` uniquely identifies a node in the HIR of the current crate. It is composed of the `owner`, which is the `DefIndex` of the directly enclosing `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e. the closest "item-like"), and the `local_id` which is unique within the given owner. This PR is also running a number of consistency checks for the generated `HirId`s: - Does `NodeId` in the HIR have a corresponding `HirId`? - Is the `owner` part of each `HirId` consistent with its position in the HIR? - Do the numerical values of the `local_id` part all lie within a dense range of integers? cc @rust-lang/compiler r? @eddyb or @nikomatsakis
2 parents 916c0b8 + 090767b commit cc98dfc

File tree

13 files changed

+1541
-859
lines changed

13 files changed

+1541
-859
lines changed

src/librustc/hir/def_id.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,86 @@ impl serialize::UseSpecializedDecodable for CrateNum {
7878
/// A DefIndex is an index into the hir-map for a crate, identifying a
7979
/// particular definition. It should really be considered an interned
8080
/// shorthand for a particular DefPath.
81+
///
82+
/// At the moment we are allocating the numerical values of DefIndexes into two
83+
/// ranges: the "low" range (starting at zero) and the "high" range (starting at
84+
/// DEF_INDEX_HI_START). This allows us to allocate the DefIndexes of all
85+
/// item-likes (Items, TraitItems, and ImplItems) into one of these ranges and
86+
/// consequently use a simple array for lookup tables keyed by DefIndex and
87+
/// known to be densely populated. This is especially important for the HIR map.
88+
///
89+
/// Since the DefIndex is mostly treated as an opaque ID, you probably
90+
/// don't have to care about these ranges.
8191
#[derive(Clone, Debug, Eq, Ord, PartialOrd, PartialEq, RustcEncodable,
8292
RustcDecodable, Hash, Copy)]
8393
pub struct DefIndex(u32);
8494

8595
impl DefIndex {
96+
#[inline]
8697
pub fn new(x: usize) -> DefIndex {
8798
assert!(x < (u32::MAX as usize));
8899
DefIndex(x as u32)
89100
}
90101

102+
#[inline]
91103
pub fn from_u32(x: u32) -> DefIndex {
92104
DefIndex(x)
93105
}
94106

107+
#[inline]
95108
pub fn as_usize(&self) -> usize {
96109
self.0 as usize
97110
}
98111

112+
#[inline]
99113
pub fn as_u32(&self) -> u32 {
100114
self.0
101115
}
116+
117+
#[inline]
118+
pub fn address_space(&self) -> DefIndexAddressSpace {
119+
if self.0 < DEF_INDEX_HI_START.0 {
120+
DefIndexAddressSpace::Low
121+
} else {
122+
DefIndexAddressSpace::High
123+
}
124+
}
125+
126+
/// Converts this DefIndex into a zero-based array index.
127+
/// This index is the offset within the given "range" of the DefIndex,
128+
/// that is, if the DefIndex is part of the "high" range, the resulting
129+
/// index will be (DefIndex - DEF_INDEX_HI_START).
130+
#[inline]
131+
pub fn as_array_index(&self) -> usize {
132+
(self.0 & !DEF_INDEX_HI_START.0) as usize
133+
}
102134
}
103135

136+
/// The start of the "high" range of DefIndexes.
137+
const DEF_INDEX_HI_START: DefIndex = DefIndex(1 << 31);
138+
104139
/// The crate root is always assigned index 0 by the AST Map code,
105140
/// thanks to `NodeCollector::new`.
106141
pub const CRATE_DEF_INDEX: DefIndex = DefIndex(0);
107142

143+
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
144+
pub enum DefIndexAddressSpace {
145+
Low = 0,
146+
High = 1,
147+
}
148+
149+
impl DefIndexAddressSpace {
150+
#[inline]
151+
pub fn index(&self) -> usize {
152+
*self as usize
153+
}
154+
155+
#[inline]
156+
pub fn start(&self) -> usize {
157+
self.index() * DEF_INDEX_HI_START.as_usize()
158+
}
159+
}
160+
108161
/// A DefId identifies a particular *definition*, by combining a crate
109162
/// index and a def index.
110163
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, RustcDecodable, Hash, Copy)]

0 commit comments

Comments
 (0)