@@ -78,33 +78,86 @@ impl serialize::UseSpecializedDecodable for CrateNum {
78
78
/// A DefIndex is an index into the hir-map for a crate, identifying a
79
79
/// particular definition. It should really be considered an interned
80
80
/// 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.
81
91
#[ derive( Clone , Debug , Eq , Ord , PartialOrd , PartialEq , RustcEncodable ,
82
92
RustcDecodable , Hash , Copy ) ]
83
93
pub struct DefIndex ( u32 ) ;
84
94
85
95
impl DefIndex {
96
+ #[ inline]
86
97
pub fn new ( x : usize ) -> DefIndex {
87
98
assert ! ( x < ( u32 :: MAX as usize ) ) ;
88
99
DefIndex ( x as u32 )
89
100
}
90
101
102
+ #[ inline]
91
103
pub fn from_u32 ( x : u32 ) -> DefIndex {
92
104
DefIndex ( x)
93
105
}
94
106
107
+ #[ inline]
95
108
pub fn as_usize ( & self ) -> usize {
96
109
self . 0 as usize
97
110
}
98
111
112
+ #[ inline]
99
113
pub fn as_u32 ( & self ) -> u32 {
100
114
self . 0
101
115
}
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
+ }
102
134
}
103
135
136
+ /// The start of the "high" range of DefIndexes.
137
+ const DEF_INDEX_HI_START : DefIndex = DefIndex ( 1 << 31 ) ;
138
+
104
139
/// The crate root is always assigned index 0 by the AST Map code,
105
140
/// thanks to `NodeCollector::new`.
106
141
pub const CRATE_DEF_INDEX : DefIndex = DefIndex ( 0 ) ;
107
142
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
+
108
161
/// A DefId identifies a particular *definition*, by combining a crate
109
162
/// index and a def index.
110
163
#[ derive( Clone , Eq , Ord , PartialOrd , PartialEq , RustcEncodable , RustcDecodable , Hash , Copy ) ]
0 commit comments