1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use crate::HashStableContext;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
use rustc_data_structures::AtomicRef;
use rustc_index::vec::Idx;
use rustc_macros::HashStable_Generic;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use std::borrow::Borrow;
use std::fmt;
use std::hash::{Hash, Hasher};
rustc_index::newtype_index! {
pub struct CrateNum {
ENCODABLE = custom
DEBUG_FORMAT = "crate{}"
}
}
pub const LOCAL_CRATE: CrateNum = CrateNum::from_u32(0);
impl CrateNum {
#[inline]
pub fn new(x: usize) -> CrateNum {
CrateNum::from_usize(x)
}
#[inline]
pub fn as_def_id(self) -> DefId {
DefId { krate: self, index: CRATE_DEF_INDEX }
}
}
impl fmt::Display for CrateNum {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.private, f)
}
}
impl<E: Encoder> Encodable<E> for CrateNum {
default fn encode(&self, s: &mut E) {
s.emit_u32(self.as_u32());
}
}
impl<D: Decoder> Decodable<D> for CrateNum {
default fn decode(d: &mut D) -> CrateNum {
CrateNum::from_u32(d.read_u32())
}
}
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[derive(HashStable_Generic, Encodable, Decodable)]
pub struct DefPathHash(pub Fingerprint);
impl DefPathHash {
#[inline]
pub fn stable_crate_id(&self) -> StableCrateId {
StableCrateId(self.0.as_value().0)
}
#[inline]
pub fn local_hash(&self) -> u64 {
self.0.as_value().1
}
pub fn new(stable_crate_id: StableCrateId, local_hash: u64) -> DefPathHash {
DefPathHash(Fingerprint::new(stable_crate_id.0, local_hash))
}
}
impl Borrow<Fingerprint> for DefPathHash {
#[inline]
fn borrow(&self) -> &Fingerprint {
&self.0
}
}
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[derive(HashStable_Generic, Encodable, Decodable)]
pub struct StableCrateId(pub(crate) u64);
impl StableCrateId {
pub fn to_u64(self) -> u64 {
self.0
}
pub fn new(crate_name: &str, is_exe: bool, mut metadata: Vec<String>) -> StableCrateId {
let mut hasher = StableHasher::new();
crate_name.hash(&mut hasher);
metadata.sort();
metadata.dedup();
hasher.write(b"metadata");
for s in &metadata {
hasher.write_usize(s.len());
hasher.write(s.as_bytes());
}
hasher.write(if is_exe { b"exe" } else { b"lib" });
if let Some(val) = std::env::var_os("RUSTC_FORCE_RUSTC_VERSION") {
hasher.write(val.to_string_lossy().into_owned().as_bytes())
} else {
hasher.write(option_env!("CFG_VERSION").unwrap_or("unknown version").as_bytes());
}
StableCrateId(hasher.finish())
}
}
rustc_index::newtype_index! {
pub struct DefIndex {
ENCODABLE = custom DEBUG_FORMAT = "DefIndex({})",
const CRATE_DEF_INDEX = 0,
}
}
impl<E: Encoder> Encodable<E> for DefIndex {
default fn encode(&self, _: &mut E) {
panic!("cannot encode `DefIndex` with `{}`", std::any::type_name::<E>());
}
}
impl<D: Decoder> Decodable<D> for DefIndex {
default fn decode(_: &mut D) -> DefIndex {
panic!("cannot decode `DefIndex` with `{}`", std::any::type_name::<D>());
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Copy)]
#[cfg_attr(not(target_pointer_width = "64"), derive(Hash))]
#[repr(C)]
#[rustc_pass_by_value]
pub struct DefId {
#[cfg(not(all(target_pointer_width = "64", target_endian = "big")))]
pub index: DefIndex,
pub krate: CrateNum,
#[cfg(all(target_pointer_width = "64", target_endian = "big"))]
pub index: DefIndex,
}
#[cfg(target_pointer_width = "64")]
impl Hash for DefId {
fn hash<H: Hasher>(&self, h: &mut H) {
(((self.krate.as_u32() as u64) << 32) | (self.index.as_u32() as u64)).hash(h)
}
}
impl DefId {
#[inline]
pub fn local(index: DefIndex) -> DefId {
DefId { krate: LOCAL_CRATE, index }
}
#[inline]
pub fn is_local(self) -> bool {
self.krate == LOCAL_CRATE
}
#[inline]
pub fn as_local(self) -> Option<LocalDefId> {
if self.is_local() { Some(LocalDefId { local_def_index: self.index }) } else { None }
}
#[inline]
#[track_caller]
pub fn expect_local(self) -> LocalDefId {
match self.as_local() {
Some(local_def_id) => local_def_id,
None => panic!("DefId::expect_local: `{:?}` isn't local", self),
}
}
#[inline]
pub fn is_crate_root(self) -> bool {
self.index == CRATE_DEF_INDEX
}
#[inline]
pub fn as_crate_root(self) -> Option<CrateNum> {
if self.is_crate_root() { Some(self.krate) } else { None }
}
#[inline]
pub fn is_top_level_module(self) -> bool {
self.is_local() && self.is_crate_root()
}
}
impl From<LocalDefId> for DefId {
fn from(local: LocalDefId) -> DefId {
local.to_def_id()
}
}
impl<E: Encoder> Encodable<E> for DefId {
default fn encode(&self, s: &mut E) {
self.krate.encode(s);
self.index.encode(s);
}
}
impl<D: Decoder> Decodable<D> for DefId {
default fn decode(d: &mut D) -> DefId {
DefId { krate: Decodable::decode(d), index: Decodable::decode(d) }
}
}
pub fn default_def_id_debug(def_id: DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DefId").field("krate", &def_id.krate).field("index", &def_id.index).finish()
}
pub static DEF_ID_DEBUG: AtomicRef<fn(DefId, &mut fmt::Formatter<'_>) -> fmt::Result> =
AtomicRef::new(&(default_def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
impl fmt::Debug for DefId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(*DEF_ID_DEBUG)(*self, f)
}
}
rustc_data_structures::define_id_collections!(DefIdMap, DefIdSet, DefIdMapEntry, DefId);
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct LocalDefId {
pub local_def_index: DefIndex,
}
impl !Ord for LocalDefId {}
impl !PartialOrd for LocalDefId {}
pub const CRATE_DEF_ID: LocalDefId = LocalDefId { local_def_index: CRATE_DEF_INDEX };
impl Idx for LocalDefId {
#[inline]
fn new(idx: usize) -> Self {
LocalDefId { local_def_index: Idx::new(idx) }
}
#[inline]
fn index(self) -> usize {
self.local_def_index.index()
}
}
impl LocalDefId {
#[inline]
pub fn to_def_id(self) -> DefId {
DefId { krate: LOCAL_CRATE, index: self.local_def_index }
}
#[inline]
pub fn is_top_level_module(self) -> bool {
self == CRATE_DEF_ID
}
}
impl fmt::Debug for LocalDefId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.to_def_id().fmt(f)
}
}
impl<E: Encoder> Encodable<E> for LocalDefId {
fn encode(&self, s: &mut E) {
self.to_def_id().encode(s);
}
}
impl<D: Decoder> Decodable<D> for LocalDefId {
fn decode(d: &mut D) -> LocalDefId {
DefId::decode(d).expect_local()
}
}
rustc_data_structures::define_id_collections!(
LocalDefIdMap,
LocalDefIdSet,
LocalDefIdMapEntry,
LocalDefId
);
impl<CTX: HashStableContext> HashStable<CTX> for DefId {
#[inline]
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
self.to_stable_hash_key(hcx).hash_stable(hcx, hasher);
}
}
impl<CTX: HashStableContext> HashStable<CTX> for LocalDefId {
#[inline]
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
self.to_stable_hash_key(hcx).hash_stable(hcx, hasher);
}
}
impl<CTX: HashStableContext> HashStable<CTX> for CrateNum {
#[inline]
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
self.to_stable_hash_key(hcx).hash_stable(hcx, hasher);
}
}
impl<CTX: HashStableContext> ToStableHashKey<CTX> for DefId {
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, hcx: &CTX) -> DefPathHash {
hcx.def_path_hash(*self)
}
}
impl<CTX: HashStableContext> ToStableHashKey<CTX> for LocalDefId {
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, hcx: &CTX) -> DefPathHash {
hcx.def_path_hash(self.to_def_id())
}
}
impl<CTX: HashStableContext> ToStableHashKey<CTX> for CrateNum {
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, hcx: &CTX) -> DefPathHash {
self.as_def_id().to_stable_hash_key(hcx)
}
}