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 CrateNum value that indicates that something is wrong.
22 /// A special CrateNum that we use for the tcx.rcache when decoding from
23 /// the incr. comp. cache.
24 ReservedForIncrCompCache,
28 impl ::std::fmt::Debug for CrateNum {
29 fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
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"),
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));
44 impl Idx for CrateNum {
46 fn new(value: usize) -> Self {
47 CrateNum::Index(Idx::new(value))
51 fn index(self) -> usize {
53 CrateNum::Index(idx) => Idx::index(idx),
54 _ => bug!("Tried to get crate index of {:?}", self),
60 pub fn new(x: usize) -> CrateNum {
61 CrateNum::from_usize(x)
64 pub fn from_usize(x: usize) -> CrateNum {
65 CrateNum::Index(CrateId::from_usize(x))
68 pub fn from_u32(x: u32) -> CrateNum {
69 CrateNum::Index(CrateId::from_u32(x))
72 pub fn as_usize(self) -> usize {
74 CrateNum::Index(id) => id.as_usize(),
75 _ => bug!("tried to get index of nonstandard crate {:?}", self),
79 pub fn as_u32(self) -> u32 {
81 CrateNum::Index(id) => id.as_u32(),
82 _ => bug!("tried to get index of nonstandard crate {:?}", self),
86 pub fn as_def_id(&self) -> DefId { DefId { krate: *self, index: CRATE_DEF_INDEX } }
89 impl fmt::Display for CrateNum {
90 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
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"),
100 impl serialize::UseSpecializedEncodable for CrateNum {}
101 impl serialize::UseSpecializedDecodable for CrateNum {}
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.
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.
114 /// Since the DefIndex is mostly treated as an opaque ID, you probably
115 /// don't have to care about these address spaces.
117 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
118 pub struct DefIndex(u32);
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);
124 impl fmt::Debug for DefIndex {
125 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128 self.address_space().index(),
129 self.as_array_index())
135 pub fn address_space(&self) -> DefIndexAddressSpace {
137 0 => DefIndexAddressSpace::Low,
138 1 => DefIndexAddressSpace::High,
143 /// Converts this DefIndex into a zero-based array index.
144 /// This index is the offset within the given DefIndexAddressSpace.
146 pub fn as_array_index(&self) -> usize {
147 (self.0 >> 1) as usize
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)
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
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);
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);
175 self.as_array_index().checked_sub(FIRST_FREE_HIGH_DEF_INDEX)
177 bug!("using local index {:?} as proc-macro index", self)
181 // Don't use this if you don't know about the DefIndex encoding.
182 pub fn from_raw_u32(x: u32) -> DefIndex {
186 // Don't use this if you don't know about the DefIndex encoding.
187 pub fn as_raw_u32(&self) -> u32 {
192 impl serialize::UseSpecializedEncodable for DefIndex {}
193 impl serialize::UseSpecializedDecodable for DefIndex {}
195 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
196 pub enum DefIndexAddressSpace {
201 impl DefIndexAddressSpace {
203 pub fn index(&self) -> usize {
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)]
216 impl fmt::Debug for DefId {
217 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218 write!(f, "DefId({}/{}:{}",
220 self.index.address_space().index(),
221 self.index.as_array_index())?;
223 ty::tls::with_opt(|opt_tcx| {
224 if let Some(tcx) = opt_tcx {
225 write!(f, " ~ {}", tcx.def_path_debug_str(*self))?;
235 /// Make a local `DefId` with the given index.
237 pub fn local(index: DefIndex) -> DefId {
238 DefId { krate: LOCAL_CRATE, index: index }
242 pub fn is_local(self) -> bool {
243 self.krate == LOCAL_CRATE
247 pub fn to_local(self) -> LocalDefId {
248 LocalDefId::from_def_id(self)
252 impl serialize::UseSpecializedEncodable for DefId {}
253 impl serialize::UseSpecializedDecodable for DefId {}
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);
266 pub fn from_def_id(def_id: DefId) -> LocalDefId {
267 assert!(def_id.is_local());
268 LocalDefId(def_id.index)
272 pub fn to_def_id(self) -> DefId {
280 impl fmt::Debug for LocalDefId {
281 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
282 self.to_def_id().fmt(f)
286 impl serialize::UseSpecializedEncodable for LocalDefId {}
287 impl serialize::UseSpecializedDecodable for LocalDefId {}