]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/def_id.rs
fb2c873d740ca9b43b1bf56b1bde5aa4e136d3d4
[rust.git] / src / librustc / hir / def_id.rs
1 use ty;
2 use hir::map::definitions::FIRST_FREE_HIGH_DEF_INDEX;
3 use rustc_data_structures::indexed_vec::Idx;
4 use serialize;
5 use std::fmt;
6 use std::u32;
7
8 newtype_index! {
9     pub struct CrateId {
10         ENCODABLE = custom
11     }
12 }
13
14 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
15 pub enum CrateNum {
16     /// Virtual crate for builtin macros
17     // FIXME(jseyfried): this is also used for custom derives until proc-macro crates get
18     // `CrateNum`s.
19     BuiltinMacros,
20     /// A CrateNum value that indicates that something is wrong.
21     Invalid,
22     /// A special CrateNum that we use for the tcx.rcache when decoding from
23     /// the incr. comp. cache.
24     ReservedForIncrCompCache,
25     Index(CrateId),
26 }
27
28 impl ::std::fmt::Debug for CrateNum {
29     fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
30         match self {
31             CrateNum::Index(id) => write!(fmt, "crate{}", id.private),
32             CrateNum::Invalid => write!(fmt, "invalid crate"),
33             CrateNum::BuiltinMacros => write!(fmt, "builtin macros crate"),
34             CrateNum::ReservedForIncrCompCache => write!(fmt, "crate for decoding incr comp cache"),
35         }
36     }
37 }
38
39 /// Item definitions in the currently-compiled crate would have the CrateNum
40 /// LOCAL_CRATE in their DefId.
41 pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32_const(0));
42
43
44 impl Idx for CrateNum {
45     #[inline]
46     fn new(value: usize) -> Self {
47         CrateNum::Index(Idx::new(value))
48     }
49
50     #[inline]
51     fn index(self) -> usize {
52         match self {
53             CrateNum::Index(idx) => Idx::index(idx),
54             _ => bug!("Tried to get crate index of {:?}", self),
55         }
56     }
57 }
58
59 impl CrateNum {
60     pub fn new(x: usize) -> CrateNum {
61         CrateNum::from_usize(x)
62     }
63
64     pub fn from_usize(x: usize) -> CrateNum {
65         CrateNum::Index(CrateId::from_usize(x))
66     }
67
68     pub fn from_u32(x: u32) -> CrateNum {
69         CrateNum::Index(CrateId::from_u32(x))
70     }
71
72     pub fn as_usize(self) -> usize {
73         match self {
74             CrateNum::Index(id) => id.as_usize(),
75             _ => bug!("tried to get index of nonstandard crate {:?}", self),
76         }
77     }
78
79     pub fn as_u32(self) -> u32 {
80         match self {
81             CrateNum::Index(id) => id.as_u32(),
82             _ => bug!("tried to get index of nonstandard crate {:?}", self),
83         }
84     }
85
86     pub fn as_def_id(&self) -> DefId { DefId { krate: *self, index: CRATE_DEF_INDEX } }
87 }
88
89 impl fmt::Display for CrateNum {
90     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91         match self {
92             CrateNum::Index(id) => fmt::Display::fmt(&id.private, f),
93             CrateNum::Invalid => write!(f, "invalid crate"),
94             CrateNum::BuiltinMacros => write!(f, "builtin macros crate"),
95             CrateNum::ReservedForIncrCompCache => write!(f, "crate for decoding incr comp cache"),
96         }
97     }
98 }
99
100 impl serialize::UseSpecializedEncodable for CrateNum {}
101 impl serialize::UseSpecializedDecodable for CrateNum {}
102
103 /// A DefIndex is an index into the hir-map for a crate, identifying a
104 /// particular definition. It should really be considered an interned
105 /// shorthand for a particular DefPath.
106 ///
107 /// At the moment we are allocating the numerical values of DefIndexes from two
108 /// address spaces: DefIndexAddressSpace::Low and DefIndexAddressSpace::High.
109 /// This allows us to allocate the DefIndexes of all item-likes
110 /// (Items, TraitItems, and ImplItems) into one of these spaces and
111 /// consequently use a simple array for lookup tables keyed by DefIndex and
112 /// known to be densely populated. This is especially important for the HIR map.
113 ///
114 /// Since the DefIndex is mostly treated as an opaque ID, you probably
115 /// don't have to care about these address spaces.
116
117 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
118 pub struct DefIndex(u32);
119
120 /// The crate root is always assigned index 0 by the AST Map code,
121 /// thanks to `NodeCollector::new`.
122 pub const CRATE_DEF_INDEX: DefIndex = DefIndex(0);
123
124 impl fmt::Debug for DefIndex {
125     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126         write!(f,
127                "DefIndex({}:{})",
128                self.address_space().index(),
129                self.as_array_index())
130     }
131 }
132
133 impl DefIndex {
134     #[inline]
135     pub fn address_space(&self) -> DefIndexAddressSpace {
136         match self.0 & 1 {
137             0 => DefIndexAddressSpace::Low,
138             1 => DefIndexAddressSpace::High,
139             _ => unreachable!()
140         }
141     }
142
143     /// Converts this DefIndex into a zero-based array index.
144     /// This index is the offset within the given DefIndexAddressSpace.
145     #[inline]
146     pub fn as_array_index(&self) -> usize {
147         (self.0 >> 1) as usize
148     }
149
150     #[inline]
151     pub fn from_array_index(i: usize, address_space: DefIndexAddressSpace) -> DefIndex {
152         DefIndex::from_raw_u32(((i << 1) | (address_space as usize)) as u32)
153     }
154
155     // Proc macros from a proc-macro crate have a kind of virtual DefIndex. This
156     // function maps the index of the macro within the crate (which is also the
157     // index of the macro in the CrateMetadata::proc_macros array) to the
158     // corresponding DefIndex.
159     pub fn from_proc_macro_index(proc_macro_index: usize) -> DefIndex {
160         // DefIndex for proc macros start from FIRST_FREE_HIGH_DEF_INDEX,
161         // because the first FIRST_FREE_HIGH_DEF_INDEX indexes are reserved
162         // for internal use.
163         let def_index = DefIndex::from_array_index(
164             proc_macro_index.checked_add(FIRST_FREE_HIGH_DEF_INDEX)
165                 .expect("integer overflow adding `proc_macro_index`"),
166             DefIndexAddressSpace::High);
167         assert!(def_index != CRATE_DEF_INDEX);
168         def_index
169     }
170
171     // This function is the reverse of from_proc_macro_index() above.
172     pub fn to_proc_macro_index(self: DefIndex) -> usize {
173         assert_eq!(self.address_space(), DefIndexAddressSpace::High);
174
175         self.as_array_index().checked_sub(FIRST_FREE_HIGH_DEF_INDEX)
176             .unwrap_or_else(|| {
177                 bug!("using local index {:?} as proc-macro index", self)
178             })
179     }
180
181     // Don't use this if you don't know about the DefIndex encoding.
182     pub fn from_raw_u32(x: u32) -> DefIndex {
183         DefIndex(x)
184     }
185
186     // Don't use this if you don't know about the DefIndex encoding.
187     pub fn as_raw_u32(&self) -> u32 {
188         self.0
189     }
190 }
191
192 impl serialize::UseSpecializedEncodable for DefIndex {}
193 impl serialize::UseSpecializedDecodable for DefIndex {}
194
195 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
196 pub enum DefIndexAddressSpace {
197     Low = 0,
198     High = 1,
199 }
200
201 impl DefIndexAddressSpace {
202     #[inline]
203     pub fn index(&self) -> usize {
204         *self as usize
205     }
206 }
207
208 /// A `DefId` identifies a particular *definition*, by combining a crate
209 /// index and a def index.
210 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
211 pub struct DefId {
212     pub krate: CrateNum,
213     pub index: DefIndex,
214 }
215
216 impl fmt::Debug for DefId {
217     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218         write!(f, "DefId({}/{}:{}",
219                self.krate,
220                self.index.address_space().index(),
221                self.index.as_array_index())?;
222
223         ty::tls::with_opt(|opt_tcx| {
224             if let Some(tcx) = opt_tcx {
225                 write!(f, " ~ {}", tcx.def_path_debug_str(*self))?;
226             }
227             Ok(())
228         })?;
229
230         write!(f, ")")
231     }
232 }
233
234 impl DefId {
235     /// Make a local `DefId` with the given index.
236     #[inline]
237     pub fn local(index: DefIndex) -> DefId {
238         DefId { krate: LOCAL_CRATE, index: index }
239     }
240
241     #[inline]
242     pub fn is_local(self) -> bool {
243         self.krate == LOCAL_CRATE
244     }
245
246     #[inline]
247     pub fn to_local(self) -> LocalDefId {
248         LocalDefId::from_def_id(self)
249     }
250 }
251
252 impl serialize::UseSpecializedEncodable for DefId {}
253 impl serialize::UseSpecializedDecodable for DefId {}
254
255 /// A LocalDefId is equivalent to a DefId with `krate == LOCAL_CRATE`. Since
256 /// we encode this information in the type, we can ensure at compile time that
257 /// no DefIds from upstream crates get thrown into the mix. There are quite a
258 /// few cases where we know that only DefIds from the local crate are expected
259 /// and a DefId from a different crate would signify a bug somewhere. This
260 /// is when LocalDefId comes in handy.
261 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
262 pub struct LocalDefId(DefIndex);
263
264 impl LocalDefId {
265     #[inline]
266     pub fn from_def_id(def_id: DefId) -> LocalDefId {
267         assert!(def_id.is_local());
268         LocalDefId(def_id.index)
269     }
270
271     #[inline]
272     pub fn to_def_id(self) -> DefId {
273         DefId {
274             krate: LOCAL_CRATE,
275             index: self.0
276         }
277     }
278 }
279
280 impl fmt::Debug for LocalDefId {
281     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
282         self.to_def_id().fmt(f)
283     }
284 }
285
286 impl serialize::UseSpecializedEncodable for LocalDefId {}
287 impl serialize::UseSpecializedDecodable for LocalDefId {}