@@ -71,27 +71,23 @@ impl serialize::UseSpecializedDecodable for CrateNum {}
71
71
/// particular definition. It should really be considered an interned
72
72
/// shorthand for a particular DefPath.
73
73
///
74
- /// At the moment we are allocating the numerical values of DefIndexes into two
75
- /// ranges: the "low" range (starting at zero) and the "high" range (starting at
76
- /// DEF_INDEX_HI_START). This allows us to allocate the DefIndexes of all
77
- /// item-likes (Items, TraitItems, and ImplItems) into one of these ranges and
74
+ /// At the moment we are allocating the numerical values of DefIndexes from two
75
+ /// address spaces: DefIndexAddressSpace::Low and DefIndexAddressSpace::High.
76
+ /// This allows us to allocate the DefIndexes of all item-likes
77
+ /// (Items, TraitItems, and ImplItems) into one of these spaces and
78
78
/// consequently use a simple array for lookup tables keyed by DefIndex and
79
79
/// known to be densely populated. This is especially important for the HIR map.
80
80
///
81
81
/// Since the DefIndex is mostly treated as an opaque ID, you probably
82
- /// don't have to care about these ranges.
83
- newtype_index ! ( DefIndex
84
- {
85
- ENCODABLE = custom
86
- DEBUG_FORMAT = custom,
82
+ /// don't have to care about these address spaces.
87
83
88
- /// The start of the "high" range of DefIndexes.
89
- const DEF_INDEX_HI_START = 1 << 31 ,
84
+ #[ derive( Clone , Eq , Ord , PartialOrd , PartialEq , Hash , Copy ) ]
85
+ pub struct DefIndex ( u32 ) ;
86
+
87
+ /// The crate root is always assigned index 0 by the AST Map code,
88
+ /// thanks to `NodeCollector::new`.
89
+ pub const CRATE_DEF_INDEX : DefIndex = DefIndex ( 0 ) ;
90
90
91
- /// The crate root is always assigned index 0 by the AST Map code,
92
- /// thanks to `NodeCollector::new`.
93
- const CRATE_DEF_INDEX = 0 ,
94
- } ) ;
95
91
96
92
impl fmt:: Debug for DefIndex {
97
93
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
@@ -104,40 +100,50 @@ impl fmt::Debug for DefIndex {
104
100
105
101
impl DefIndex {
106
102
#[ inline]
107
- pub fn from_u32 ( x : u32 ) -> DefIndex {
108
- DefIndex ( x)
103
+ pub fn address_space ( & self ) -> DefIndexAddressSpace {
104
+ match self . 0 & 1 {
105
+ 0 => DefIndexAddressSpace :: Low ,
106
+ 1 => DefIndexAddressSpace :: High ,
107
+ _ => unreachable ! ( )
108
+ }
109
109
}
110
110
111
+ /// Converts this DefIndex into a zero-based array index.
112
+ /// This index is the offset within the given DefIndexAddressSpace.
111
113
#[ inline]
112
- pub fn as_usize ( & self ) -> usize {
113
- self . 0 as usize
114
+ pub fn as_array_index ( & self ) -> usize {
115
+ ( self . 0 >> 1 ) as usize
114
116
}
115
117
116
118
#[ inline]
117
- pub fn as_u32 ( & self ) -> u32 {
118
- self . 0
119
+ pub fn from_array_index ( i : usize , address_space : DefIndexAddressSpace ) -> DefIndex {
120
+ DefIndex :: from_raw_u32 ( ( ( i << 1 ) | ( address_space as usize ) ) as u32 )
119
121
}
120
122
121
- #[ inline]
122
- pub fn address_space ( & self ) -> DefIndexAddressSpace {
123
- if self . 0 < DEF_INDEX_HI_START . 0 {
124
- DefIndexAddressSpace :: Low
125
- } else {
126
- DefIndexAddressSpace :: High
127
- }
123
+ // Proc macros from a proc-macro crate have a kind of virtual DefIndex. This
124
+ // function maps the index of the macro within the crate (which is also the
125
+ // index of the macro in the CrateMetadata::proc_macros array) to the
126
+ // corresponding DefIndex.
127
+ pub fn from_proc_macro_index ( proc_macro_index : usize ) -> DefIndex {
128
+ let def_index = DefIndex :: from_array_index ( proc_macro_index,
129
+ DefIndexAddressSpace :: High ) ;
130
+ assert ! ( def_index != CRATE_DEF_INDEX ) ;
131
+ def_index
128
132
}
129
133
130
- /// Converts this DefIndex into a zero-based array index.
131
- /// This index is the offset within the given "range" of the DefIndex,
132
- /// that is, if the DefIndex is part of the "high" range, the resulting
133
- /// index will be (DefIndex - DEF_INDEX_HI_START).
134
- #[ inline]
135
- pub fn as_array_index ( & self ) -> usize {
136
- ( self . 0 & !DEF_INDEX_HI_START . 0 ) as usize
134
+ // This function is the reverse of from_proc_macro_index() above.
135
+ pub fn to_proc_macro_index ( self : DefIndex ) -> usize {
136
+ self . as_array_index ( )
137
137
}
138
138
139
- pub fn from_array_index ( i : usize , address_space : DefIndexAddressSpace ) -> DefIndex {
140
- DefIndex :: new ( address_space. start ( ) + i)
139
+ // Don't use this if you don't know about the DefIndex encoding.
140
+ pub fn from_raw_u32 ( x : u32 ) -> DefIndex {
141
+ DefIndex ( x)
142
+ }
143
+
144
+ // Don't use this if you don't know about the DefIndex encoding.
145
+ pub fn as_raw_u32 ( & self ) -> u32 {
146
+ self . 0
141
147
}
142
148
}
143
149
@@ -155,11 +161,6 @@ impl DefIndexAddressSpace {
155
161
pub fn index ( & self ) -> usize {
156
162
* self as usize
157
163
}
158
-
159
- #[ inline]
160
- pub fn start ( & self ) -> usize {
161
- self . index ( ) * DEF_INDEX_HI_START . as_usize ( )
162
- }
163
164
}
164
165
165
166
/// A DefId identifies a particular *definition*, by combining a crate
0 commit comments