1111use ty;
1212
1313use rustc_data_structures:: indexed_vec:: Idx ;
14- use serialize:: { self , Encoder , Decoder } ;
15-
14+ use serialize;
1615use std:: fmt;
1716use std:: u32;
1817
@@ -32,6 +31,10 @@ newtype_index!(CrateNum
3231
3332 /// A CrateNum value that indicates that something is wrong.
3433 const INVALID_CRATE = u32 :: MAX - 1 ,
34+
35+ /// A special CrateNum that we use for the tcx.rcache when decoding from
36+ /// the incr. comp. cache.
37+ const RESERVED_FOR_INCR_COMP_CACHE = u32 :: MAX - 2 ,
3538 } ) ;
3639
3740impl CrateNum {
@@ -61,17 +64,8 @@ impl fmt::Display for CrateNum {
6164 }
6265}
6366
64- impl serialize:: UseSpecializedEncodable for CrateNum {
65- fn default_encode < S : Encoder > ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
66- s. emit_u32 ( self . 0 )
67- }
68- }
69-
70- impl serialize:: UseSpecializedDecodable for CrateNum {
71- fn default_decode < D : Decoder > ( d : & mut D ) -> Result < CrateNum , D :: Error > {
72- d. read_u32 ( ) . map ( CrateNum )
73- }
74- }
67+ impl serialize:: UseSpecializedEncodable for CrateNum { }
68+ impl serialize:: UseSpecializedDecodable for CrateNum { }
7569
7670/// A DefIndex is an index into the hir-map for a crate, identifying a
7771/// particular definition. It should really be considered an interned
@@ -88,6 +82,7 @@ impl serialize::UseSpecializedDecodable for CrateNum {
8882/// don't have to care about these ranges.
8983newtype_index ! ( DefIndex
9084 {
85+ ENCODABLE = custom
9186 DEBUG_FORMAT = custom,
9287
9388 /// The start of the "high" range of DefIndexes.
@@ -146,6 +141,9 @@ impl DefIndex {
146141 }
147142}
148143
144+ impl serialize:: UseSpecializedEncodable for DefIndex { }
145+ impl serialize:: UseSpecializedDecodable for DefIndex { }
146+
149147#[ derive( Copy , Clone , Eq , PartialEq , Hash ) ]
150148pub enum DefIndexAddressSpace {
151149 Low = 0 ,
@@ -166,7 +164,7 @@ impl DefIndexAddressSpace {
166164
167165/// A DefId identifies a particular *definition*, by combining a crate
168166/// index and a def index.
169- #[ derive( Clone , Eq , Ord , PartialOrd , PartialEq , RustcEncodable , RustcDecodable , Hash , Copy ) ]
167+ #[ derive( Clone , Eq , Ord , PartialOrd , PartialEq , Hash , Copy ) ]
170168pub struct DefId {
171169 pub krate : CrateNum ,
172170 pub index : DefIndex ,
@@ -188,14 +186,58 @@ impl fmt::Debug for DefId {
188186 }
189187}
190188
191-
192189impl DefId {
193190 /// Make a local `DefId` with the given index.
191+ #[ inline]
194192 pub fn local ( index : DefIndex ) -> DefId {
195193 DefId { krate : LOCAL_CRATE , index : index }
196194 }
197195
198- pub fn is_local ( & self ) -> bool {
196+ #[ inline]
197+ pub fn is_local ( self ) -> bool {
199198 self . krate == LOCAL_CRATE
200199 }
200+
201+ #[ inline]
202+ pub fn to_local ( self ) -> LocalDefId {
203+ LocalDefId :: from_def_id ( self )
204+ }
201205}
206+
207+ impl serialize:: UseSpecializedEncodable for DefId { }
208+ impl serialize:: UseSpecializedDecodable for DefId { }
209+
210+ /// A LocalDefId is equivalent to a DefId with `krate == LOCAL_CRATE`. Since
211+ /// we encode this information in the type, we can ensure at compile time that
212+ /// no DefIds from upstream crates get thrown into the mix. There are quite a
213+ /// few cases where we know that only DefIds from the local crate are expected
214+ /// and a DefId from a different crate would signify a bug somewhere. This
215+ /// is when LocalDefId comes in handy.
216+ #[ derive( Clone , Copy , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
217+ pub struct LocalDefId ( DefIndex ) ;
218+
219+ impl LocalDefId {
220+
221+ #[ inline]
222+ pub fn from_def_id ( def_id : DefId ) -> LocalDefId {
223+ assert ! ( def_id. is_local( ) ) ;
224+ LocalDefId ( def_id. index )
225+ }
226+
227+ #[ inline]
228+ pub fn to_def_id ( self ) -> DefId {
229+ DefId {
230+ krate : LOCAL_CRATE ,
231+ index : self . 0
232+ }
233+ }
234+ }
235+
236+ impl fmt:: Debug for LocalDefId {
237+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
238+ self . to_def_id ( ) . fmt ( f)
239+ }
240+ }
241+
242+ impl serialize:: UseSpecializedEncodable for LocalDefId { }
243+ impl serialize:: UseSpecializedDecodable for LocalDefId { }
0 commit comments