11
11
use ty;
12
12
13
13
use rustc_data_structures:: indexed_vec:: Idx ;
14
- use serialize:: { self , Encoder , Decoder } ;
15
-
14
+ use serialize;
16
15
use std:: fmt;
17
16
use std:: u32;
18
17
@@ -32,6 +31,10 @@ newtype_index!(CrateNum
32
31
33
32
/// A CrateNum value that indicates that something is wrong.
34
33
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 ,
35
38
} ) ;
36
39
37
40
impl CrateNum {
@@ -61,17 +64,8 @@ impl fmt::Display for CrateNum {
61
64
}
62
65
}
63
66
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 { }
75
69
76
70
/// A DefIndex is an index into the hir-map for a crate, identifying a
77
71
/// particular definition. It should really be considered an interned
@@ -88,6 +82,7 @@ impl serialize::UseSpecializedDecodable for CrateNum {
88
82
/// don't have to care about these ranges.
89
83
newtype_index ! ( DefIndex
90
84
{
85
+ ENCODABLE = custom
91
86
DEBUG_FORMAT = custom,
92
87
93
88
/// The start of the "high" range of DefIndexes.
@@ -146,6 +141,9 @@ impl DefIndex {
146
141
}
147
142
}
148
143
144
+ impl serialize:: UseSpecializedEncodable for DefIndex { }
145
+ impl serialize:: UseSpecializedDecodable for DefIndex { }
146
+
149
147
#[ derive( Copy , Clone , Eq , PartialEq , Hash ) ]
150
148
pub enum DefIndexAddressSpace {
151
149
Low = 0 ,
@@ -166,7 +164,7 @@ impl DefIndexAddressSpace {
166
164
167
165
/// A DefId identifies a particular *definition*, by combining a crate
168
166
/// 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 ) ]
170
168
pub struct DefId {
171
169
pub krate : CrateNum ,
172
170
pub index : DefIndex ,
@@ -188,14 +186,58 @@ impl fmt::Debug for DefId {
188
186
}
189
187
}
190
188
191
-
192
189
impl DefId {
193
190
/// Make a local `DefId` with the given index.
191
+ #[ inline]
194
192
pub fn local ( index : DefIndex ) -> DefId {
195
193
DefId { krate : LOCAL_CRATE , index : index }
196
194
}
197
195
198
- pub fn is_local ( & self ) -> bool {
196
+ #[ inline]
197
+ pub fn is_local ( self ) -> bool {
199
198
self . krate == LOCAL_CRATE
200
199
}
200
+
201
+ #[ inline]
202
+ pub fn to_local ( self ) -> LocalDefId {
203
+ LocalDefId :: from_def_id ( self )
204
+ }
201
205
}
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