1 use rustc_data_structures::AtomicRef;
2 use rustc_index::vec::Idx;
6 rustc_index::newtype_index! {
12 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
14 /// A special `CrateNum` that we use for the `tcx.rcache` when decoding from
15 /// the incr. comp. cache.
16 ReservedForIncrCompCache,
20 impl ::std::fmt::Debug for CrateNum {
21 fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
23 CrateNum::Index(id) => write!(fmt, "crate{}", id.private),
24 CrateNum::ReservedForIncrCompCache => write!(fmt, "crate for decoding incr comp cache"),
29 /// Item definitions in the currently-compiled crate would have the `CrateNum`
30 /// `LOCAL_CRATE` in their `DefId`.
31 pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32_const(0));
33 impl Idx for CrateNum {
35 fn new(value: usize) -> Self {
36 CrateNum::Index(Idx::new(value))
40 fn index(self) -> usize {
42 CrateNum::Index(idx) => Idx::index(idx),
43 _ => panic!("Tried to get crate index of {:?}", self),
49 pub fn new(x: usize) -> CrateNum {
50 CrateNum::from_usize(x)
53 pub fn from_usize(x: usize) -> CrateNum {
54 CrateNum::Index(CrateId::from_usize(x))
57 pub fn from_u32(x: u32) -> CrateNum {
58 CrateNum::Index(CrateId::from_u32(x))
61 pub fn as_usize(self) -> usize {
63 CrateNum::Index(id) => id.as_usize(),
64 _ => panic!("tried to get index of non-standard crate {:?}", self),
68 pub fn as_u32(self) -> u32 {
70 CrateNum::Index(id) => id.as_u32(),
71 _ => panic!("tried to get index of non-standard crate {:?}", self),
75 pub fn as_def_id(&self) -> DefId {
76 DefId { krate: *self, index: CRATE_DEF_INDEX }
80 impl fmt::Display for CrateNum {
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83 CrateNum::Index(id) => fmt::Display::fmt(&id.private, f),
84 CrateNum::ReservedForIncrCompCache => write!(f, "crate for decoding incr comp cache"),
89 impl rustc_serialize::UseSpecializedEncodable for CrateNum {}
90 impl rustc_serialize::UseSpecializedDecodable for CrateNum {}
92 rustc_index::newtype_index! {
93 /// A DefIndex is an index into the hir-map for a crate, identifying a
94 /// particular definition. It should really be considered an interned
95 /// shorthand for a particular DefPath.
97 DEBUG_FORMAT = "DefIndex({})",
99 /// The crate root is always assigned index 0 by the AST Map code,
100 /// thanks to `NodeCollector::new`.
101 const CRATE_DEF_INDEX = 0,
105 impl rustc_serialize::UseSpecializedEncodable for DefIndex {}
106 impl rustc_serialize::UseSpecializedDecodable for DefIndex {}
108 /// A `DefId` identifies a particular *definition*, by combining a crate
109 /// index and a def index.
110 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
117 /// Makes a local `DefId` from the given `DefIndex`.
119 pub fn local(index: DefIndex) -> DefId {
120 DefId { krate: LOCAL_CRATE, index: index }
124 pub fn is_local(self) -> bool {
125 self.krate == LOCAL_CRATE
129 pub fn to_local(self) -> LocalDefId {
130 LocalDefId::from_def_id(self)
133 pub fn is_top_level_module(self) -> bool {
134 self.is_local() && self.index == CRATE_DEF_INDEX
138 impl rustc_serialize::UseSpecializedEncodable for DefId {}
139 impl rustc_serialize::UseSpecializedDecodable for DefId {}
141 pub fn default_def_id_debug(def_id: DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142 f.debug_struct("DefId").field("krate", &def_id.krate).field("index", &def_id.index).finish()
145 pub static DEF_ID_DEBUG: AtomicRef<fn(DefId, &mut fmt::Formatter<'_>) -> fmt::Result> =
146 AtomicRef::new(&(default_def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
148 impl fmt::Debug for DefId {
149 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150 (*DEF_ID_DEBUG)(*self, f)
154 rustc_data_structures::define_id_collections!(DefIdMap, DefIdSet, DefId);
156 /// A LocalDefId is equivalent to a DefId with `krate == LOCAL_CRATE`. Since
157 /// we encode this information in the type, we can ensure at compile time that
158 /// no DefIds from upstream crates get thrown into the mix. There are quite a
159 /// few cases where we know that only DefIds from the local crate are expected
160 /// and a DefId from a different crate would signify a bug somewhere. This
161 /// is when LocalDefId comes in handy.
162 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
163 pub struct LocalDefId(DefIndex);
167 pub fn from_def_id(def_id: DefId) -> LocalDefId {
168 assert!(def_id.is_local());
169 LocalDefId(def_id.index)
173 pub fn to_def_id(self) -> DefId {
174 DefId { krate: LOCAL_CRATE, index: self.0 }
178 impl fmt::Debug for LocalDefId {
179 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180 self.to_def_id().fmt(f)
184 impl rustc_serialize::UseSpecializedEncodable for LocalDefId {}
185 impl rustc_serialize::UseSpecializedDecodable for LocalDefId {}