]> git.lizzy.rs Git - rust.git/blob - src/librustc_hir/def_id.rs
Rollup merge of #68256 - estebank:bad-sugg-span, r=petrochenkov
[rust.git] / src / librustc_hir / def_id.rs
1 use rustc_data_structures::AtomicRef;
2 use rustc_index::vec::Idx;
3 use std::fmt;
4 use std::u32;
5
6 rustc_index::newtype_index! {
7     pub struct CrateId {
8         ENCODABLE = custom
9     }
10 }
11
12 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13 pub enum CrateNum {
14     /// A special `CrateNum` that we use for the `tcx.rcache` when decoding from
15     /// the incr. comp. cache.
16     ReservedForIncrCompCache,
17     Index(CrateId),
18 }
19
20 impl ::std::fmt::Debug for CrateNum {
21     fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
22         match self {
23             CrateNum::Index(id) => write!(fmt, "crate{}", id.private),
24             CrateNum::ReservedForIncrCompCache => write!(fmt, "crate for decoding incr comp cache"),
25         }
26     }
27 }
28
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));
32
33 impl Idx for CrateNum {
34     #[inline]
35     fn new(value: usize) -> Self {
36         CrateNum::Index(Idx::new(value))
37     }
38
39     #[inline]
40     fn index(self) -> usize {
41         match self {
42             CrateNum::Index(idx) => Idx::index(idx),
43             _ => panic!("Tried to get crate index of {:?}", self),
44         }
45     }
46 }
47
48 impl CrateNum {
49     pub fn new(x: usize) -> CrateNum {
50         CrateNum::from_usize(x)
51     }
52
53     pub fn from_usize(x: usize) -> CrateNum {
54         CrateNum::Index(CrateId::from_usize(x))
55     }
56
57     pub fn from_u32(x: u32) -> CrateNum {
58         CrateNum::Index(CrateId::from_u32(x))
59     }
60
61     pub fn as_usize(self) -> usize {
62         match self {
63             CrateNum::Index(id) => id.as_usize(),
64             _ => panic!("tried to get index of non-standard crate {:?}", self),
65         }
66     }
67
68     pub fn as_u32(self) -> u32 {
69         match self {
70             CrateNum::Index(id) => id.as_u32(),
71             _ => panic!("tried to get index of non-standard crate {:?}", self),
72         }
73     }
74
75     pub fn as_def_id(&self) -> DefId {
76         DefId { krate: *self, index: CRATE_DEF_INDEX }
77     }
78 }
79
80 impl fmt::Display for CrateNum {
81     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82         match self {
83             CrateNum::Index(id) => fmt::Display::fmt(&id.private, f),
84             CrateNum::ReservedForIncrCompCache => write!(f, "crate for decoding incr comp cache"),
85         }
86     }
87 }
88
89 impl rustc_serialize::UseSpecializedEncodable for CrateNum {}
90 impl rustc_serialize::UseSpecializedDecodable for CrateNum {}
91
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.
96     pub struct DefIndex {
97         DEBUG_FORMAT = "DefIndex({})",
98
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,
102     }
103 }
104
105 impl rustc_serialize::UseSpecializedEncodable for DefIndex {}
106 impl rustc_serialize::UseSpecializedDecodable for DefIndex {}
107
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)]
111 pub struct DefId {
112     pub krate: CrateNum,
113     pub index: DefIndex,
114 }
115
116 impl DefId {
117     /// Makes a local `DefId` from the given `DefIndex`.
118     #[inline]
119     pub fn local(index: DefIndex) -> DefId {
120         DefId { krate: LOCAL_CRATE, index: index }
121     }
122
123     #[inline]
124     pub fn is_local(self) -> bool {
125         self.krate == LOCAL_CRATE
126     }
127
128     #[inline]
129     pub fn to_local(self) -> LocalDefId {
130         LocalDefId::from_def_id(self)
131     }
132
133     pub fn is_top_level_module(self) -> bool {
134         self.is_local() && self.index == CRATE_DEF_INDEX
135     }
136 }
137
138 impl rustc_serialize::UseSpecializedEncodable for DefId {}
139 impl rustc_serialize::UseSpecializedDecodable for DefId {}
140
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()
143 }
144
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<'_>) -> _));
147
148 impl fmt::Debug for DefId {
149     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150         (*DEF_ID_DEBUG)(*self, f)
151     }
152 }
153
154 rustc_data_structures::define_id_collections!(DefIdMap, DefIdSet, DefId);
155
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);
164
165 impl LocalDefId {
166     #[inline]
167     pub fn from_def_id(def_id: DefId) -> LocalDefId {
168         assert!(def_id.is_local());
169         LocalDefId(def_id.index)
170     }
171
172     #[inline]
173     pub fn to_def_id(self) -> DefId {
174         DefId { krate: LOCAL_CRATE, index: self.0 }
175     }
176 }
177
178 impl fmt::Debug for LocalDefId {
179     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180         self.to_def_id().fmt(f)
181     }
182 }
183
184 impl rustc_serialize::UseSpecializedEncodable for LocalDefId {}
185 impl rustc_serialize::UseSpecializedDecodable for LocalDefId {}