2 use hir::map::definitions::FIRST_FREE_HIGH_DEF_INDEX;
3 use rustc_data_structures::indexed_vec::Idx;
14 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
16 /// Virtual crate for builtin macros
17 // FIXME(jseyfried): this is also used for custom derives until proc-macro crates get
20 /// A special CrateNum that we use for the tcx.rcache when decoding from
21 /// the incr. comp. cache.
22 ReservedForIncrCompCache,
26 impl ::std::fmt::Debug for CrateNum {
27 fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
29 CrateNum::Index(id) => write!(fmt, "crate{}", id.private),
30 CrateNum::BuiltinMacros => write!(fmt, "builtin macros crate"),
31 CrateNum::ReservedForIncrCompCache => write!(fmt, "crate for decoding incr comp cache"),
36 /// Item definitions in the currently-compiled crate would have the CrateNum
37 /// LOCAL_CRATE in their DefId.
38 pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32_const(0));
41 impl Idx for CrateNum {
43 fn new(value: usize) -> Self {
44 CrateNum::Index(Idx::new(value))
48 fn index(self) -> usize {
50 CrateNum::Index(idx) => Idx::index(idx),
51 _ => bug!("Tried to get crate index of {:?}", self),
57 pub fn new(x: usize) -> CrateNum {
58 CrateNum::from_usize(x)
61 pub fn from_usize(x: usize) -> CrateNum {
62 CrateNum::Index(CrateId::from_usize(x))
65 pub fn from_u32(x: u32) -> CrateNum {
66 CrateNum::Index(CrateId::from_u32(x))
69 pub fn as_usize(self) -> usize {
71 CrateNum::Index(id) => id.as_usize(),
72 _ => bug!("tried to get index of nonstandard crate {:?}", self),
76 pub fn as_u32(self) -> u32 {
78 CrateNum::Index(id) => id.as_u32(),
79 _ => bug!("tried to get index of nonstandard crate {:?}", self),
83 pub fn as_def_id(&self) -> DefId { DefId { krate: *self, index: CRATE_DEF_INDEX } }
86 impl fmt::Display for CrateNum {
87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 CrateNum::Index(id) => fmt::Display::fmt(&id.private, f),
90 CrateNum::BuiltinMacros => write!(f, "builtin macros crate"),
91 CrateNum::ReservedForIncrCompCache => write!(f, "crate for decoding incr comp cache"),
96 impl serialize::UseSpecializedEncodable for CrateNum {}
97 impl serialize::UseSpecializedDecodable for CrateNum {}
99 /// A DefIndex is an index into the hir-map for a crate, identifying a
100 /// particular definition. It should really be considered an interned
101 /// shorthand for a particular DefPath.
103 /// At the moment we are allocating the numerical values of DefIndexes from two
104 /// address spaces: DefIndexAddressSpace::Low and DefIndexAddressSpace::High.
105 /// This allows us to allocate the DefIndexes of all item-likes
106 /// (Items, TraitItems, and ImplItems) into one of these spaces and
107 /// consequently use a simple array for lookup tables keyed by DefIndex and
108 /// known to be densely populated. This is especially important for the HIR map.
110 /// Since the DefIndex is mostly treated as an opaque ID, you probably
111 /// don't have to care about these address spaces.
113 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
114 pub struct DefIndex(u32);
116 /// The crate root is always assigned index 0 by the AST Map code,
117 /// thanks to `NodeCollector::new`.
118 pub const CRATE_DEF_INDEX: DefIndex = DefIndex(0);
120 impl fmt::Debug for DefIndex {
121 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124 self.address_space().index(),
125 self.as_array_index())
131 pub fn address_space(&self) -> DefIndexAddressSpace {
133 0 => DefIndexAddressSpace::Low,
134 1 => DefIndexAddressSpace::High,
139 /// Converts this DefIndex into a zero-based array index.
140 /// This index is the offset within the given DefIndexAddressSpace.
142 pub fn as_array_index(&self) -> usize {
143 (self.0 >> 1) as usize
147 pub fn from_array_index(i: usize, address_space: DefIndexAddressSpace) -> DefIndex {
148 DefIndex::from_raw_u32(((i << 1) | (address_space as usize)) as u32)
151 // Proc macros from a proc-macro crate have a kind of virtual DefIndex. This
152 // function maps the index of the macro within the crate (which is also the
153 // index of the macro in the CrateMetadata::proc_macros array) to the
154 // corresponding DefIndex.
155 pub fn from_proc_macro_index(proc_macro_index: usize) -> DefIndex {
156 // DefIndex for proc macros start from FIRST_FREE_HIGH_DEF_INDEX,
157 // because the first FIRST_FREE_HIGH_DEF_INDEX indexes are reserved
159 let def_index = DefIndex::from_array_index(
160 proc_macro_index.checked_add(FIRST_FREE_HIGH_DEF_INDEX)
161 .expect("integer overflow adding `proc_macro_index`"),
162 DefIndexAddressSpace::High);
163 assert!(def_index != CRATE_DEF_INDEX);
167 // This function is the reverse of from_proc_macro_index() above.
168 pub fn to_proc_macro_index(self: DefIndex) -> usize {
169 assert_eq!(self.address_space(), DefIndexAddressSpace::High);
171 self.as_array_index().checked_sub(FIRST_FREE_HIGH_DEF_INDEX)
173 bug!("using local index {:?} as proc-macro index", self)
177 // Don't use this if you don't know about the DefIndex encoding.
178 pub fn from_raw_u32(x: u32) -> DefIndex {
182 // Don't use this if you don't know about the DefIndex encoding.
183 pub fn as_raw_u32(&self) -> u32 {
188 impl serialize::UseSpecializedEncodable for DefIndex {}
189 impl serialize::UseSpecializedDecodable for DefIndex {}
191 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
192 pub enum DefIndexAddressSpace {
197 impl DefIndexAddressSpace {
199 pub fn index(&self) -> usize {
204 /// A `DefId` identifies a particular *definition*, by combining a crate
205 /// index and a def index.
206 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
212 impl fmt::Debug for DefId {
213 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
214 write!(f, "DefId({}/{}:{}",
216 self.index.address_space().index(),
217 self.index.as_array_index())?;
219 ty::tls::with_opt(|opt_tcx| {
220 if let Some(tcx) = opt_tcx {
221 write!(f, " ~ {}", tcx.def_path_debug_str(*self))?;
231 /// Make a local `DefId` with the given index.
233 pub fn local(index: DefIndex) -> DefId {
234 DefId { krate: LOCAL_CRATE, index: index }
238 pub fn is_local(self) -> bool {
239 self.krate == LOCAL_CRATE
243 pub fn to_local(self) -> LocalDefId {
244 LocalDefId::from_def_id(self)
248 impl serialize::UseSpecializedEncodable for DefId {}
249 impl serialize::UseSpecializedDecodable for DefId {}
251 /// A LocalDefId is equivalent to a DefId with `krate == LOCAL_CRATE`. Since
252 /// we encode this information in the type, we can ensure at compile time that
253 /// no DefIds from upstream crates get thrown into the mix. There are quite a
254 /// few cases where we know that only DefIds from the local crate are expected
255 /// and a DefId from a different crate would signify a bug somewhere. This
256 /// is when LocalDefId comes in handy.
257 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
258 pub struct LocalDefId(DefIndex);
262 pub fn from_def_id(def_id: DefId) -> LocalDefId {
263 assert!(def_id.is_local());
264 LocalDefId(def_id.index)
268 pub fn to_def_id(self) -> DefId {
276 impl fmt::Debug for LocalDefId {
277 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
278 self.to_def_id().fmt(f)
282 impl serialize::UseSpecializedEncodable for LocalDefId {}
283 impl serialize::UseSpecializedDecodable for LocalDefId {}