@@ -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 ) ]
8393pub struct DefIndex ( u32 ) ;
8494
8595impl 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`.
106141pub 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