@@ -71,27 +71,23 @@ impl serialize::UseSpecializedDecodable for CrateNum {}
7171/// particular definition. It should really be considered an interned
7272/// shorthand for a particular DefPath.
7373///
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
7878/// consequently use a simple array for lookup tables keyed by DefIndex and
7979/// known to be densely populated. This is especially important for the HIR map.
8080///
8181/// 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.
8783
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 ) ;
9090
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- } ) ;
9591
9692impl fmt:: Debug for DefIndex {
9793 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
@@ -104,40 +100,50 @@ impl fmt::Debug for DefIndex {
104100
105101impl DefIndex {
106102 #[ 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+ }
109109 }
110110
111+ /// Converts this DefIndex into a zero-based array index.
112+ /// This index is the offset within the given DefIndexAddressSpace.
111113 #[ 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
114116 }
115117
116118 #[ 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 )
119121 }
120122
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
128132 }
129133
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 ( )
137137 }
138138
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
141147 }
142148}
143149
@@ -155,11 +161,6 @@ impl DefIndexAddressSpace {
155161 pub fn index ( & self ) -> usize {
156162 * self as usize
157163 }
158-
159- #[ inline]
160- pub fn start ( & self ) -> usize {
161- self . index ( ) * DEF_INDEX_HI_START . as_usize ( )
162- }
163164}
164165
165166/// A DefId identifies a particular *definition*, by combining a crate
0 commit comments