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
use crate::HashStableContext;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::AtomicRef;
use rustc_index::vec::Idx;
use rustc_serialize::{Decoder, Encoder};
use std::fmt;
use std::{u32, u64};
rustc_index::newtype_index! {
pub struct CrateId {
ENCODABLE = custom
}
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum CrateNum {
ReservedForIncrCompCache,
Index(CrateId),
}
pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32_const(0));
impl Idx for CrateNum {
#[inline]
fn new(value: usize) -> Self {
CrateNum::Index(Idx::new(value))
}
#[inline]
fn index(self) -> usize {
match self {
CrateNum::Index(idx) => Idx::index(idx),
_ => panic!("Tried to get crate index of {:?}", self),
}
}
}
impl CrateNum {
pub fn new(x: usize) -> CrateNum {
CrateNum::from_usize(x)
}
pub fn from_usize(x: usize) -> CrateNum {
CrateNum::Index(CrateId::from_usize(x))
}
pub fn from_u32(x: u32) -> CrateNum {
CrateNum::Index(CrateId::from_u32(x))
}
pub fn as_usize(self) -> usize {
match self {
CrateNum::Index(id) => id.as_usize(),
_ => panic!("tried to get index of non-standard crate {:?}", self),
}
}
pub fn as_u32(self) -> u32 {
match self {
CrateNum::Index(id) => id.as_u32(),
_ => panic!("tried to get index of non-standard crate {:?}", self),
}
}
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 {
match self {
CrateNum::Index(id) => fmt::Display::fmt(&id.private, f),
CrateNum::ReservedForIncrCompCache => write!(f, "crate for decoding incr comp cache"),
}
}
}
impl rustc_serialize::UseSpecializedEncodable for CrateNum {
fn default_encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
e.emit_u32(self.as_u32())
}
}
impl rustc_serialize::UseSpecializedDecodable for CrateNum {
fn default_decode<D: Decoder>(d: &mut D) -> Result<CrateNum, D::Error> {
Ok(CrateNum::from_u32(d.read_u32()?))
}
}
impl ::std::fmt::Debug for CrateNum {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
match self {
CrateNum::Index(id) => write!(fmt, "crate{}", id.private),
CrateNum::ReservedForIncrCompCache => write!(fmt, "crate for decoding incr comp cache"),
}
}
}
rustc_index::newtype_index! {
pub struct DefIndex {
DEBUG_FORMAT = "DefIndex({})",
const CRATE_DEF_INDEX = 0,
}
}
impl rustc_serialize::UseSpecializedEncodable for DefIndex {}
impl rustc_serialize::UseSpecializedDecodable for DefIndex {}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
pub struct DefId {
pub krate: CrateNum,
pub index: DefIndex,
}
impl DefId {
#[inline]
pub fn local(index: DefIndex) -> DefId {
DefId { krate: LOCAL_CRATE, index: index }
}
#[inline]
pub fn is_local(self) -> bool {
self.krate == LOCAL_CRATE
}
#[inline]
pub fn to_local(self) -> LocalDefId {
LocalDefId::from_def_id(self)
}
pub fn is_top_level_module(self) -> bool {
self.is_local() && self.index == CRATE_DEF_INDEX
}
}
impl rustc_serialize::UseSpecializedEncodable for DefId {
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
let krate = u64::from(self.krate.as_u32());
let index = u64::from(self.index.as_u32());
s.emit_u64((krate << 32) | index)
}
}
impl rustc_serialize::UseSpecializedDecodable for DefId {
fn default_decode<D: Decoder>(d: &mut D) -> Result<DefId, D::Error> {
let def_id = d.read_u64()?;
let krate = CrateNum::from_u32((def_id >> 32) as u32);
let index = DefIndex::from_u32((def_id & 0xffffffff) as u32);
Ok(DefId { krate, index })
}
}
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, DefId);
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct LocalDefId(DefIndex);
impl LocalDefId {
#[inline]
pub fn from_def_id(def_id: DefId) -> LocalDefId {
assert!(def_id.is_local());
LocalDefId(def_id.index)
}
#[inline]
pub fn to_def_id(self) -> DefId {
DefId { krate: LOCAL_CRATE, index: self.0 }
}
}
impl fmt::Debug for LocalDefId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.to_def_id().fmt(f)
}
}
impl rustc_serialize::UseSpecializedEncodable for LocalDefId {}
impl rustc_serialize::UseSpecializedDecodable for LocalDefId {}
impl<CTX: HashStableContext> HashStable<CTX> for DefId {
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
hcx.hash_def_id(*self, hasher)
}
}