]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/def_id.rs
Uplift match_def_path from Clippy
[rust.git] / src / librustc / hir / def_id.rs
1 use crate::ty::{self, print::Printer, subst::Kind, Ty, TyCtxt};
2 use crate::hir::map::definitions::{DisambiguatedDefPathData, 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 use syntax::symbol::{LocalInternedString, Symbol};
8
9 newtype_index! {
10     pub struct CrateId {
11         ENCODABLE = custom
12     }
13 }
14
15 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
16 pub enum CrateNum {
17     /// Virtual crate for builtin macros
18     // FIXME(jseyfried): this is also used for custom derives until proc-macro crates get
19     // `CrateNum`s.
20     BuiltinMacros,
21     /// A special CrateNum that we use for the tcx.rcache when decoding from
22     /// the incr. comp. cache.
23     ReservedForIncrCompCache,
24     Index(CrateId),
25 }
26
27 impl ::std::fmt::Debug for CrateNum {
28     fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
29         match self {
30             CrateNum::Index(id) => write!(fmt, "crate{}", id.private),
31             CrateNum::BuiltinMacros => write!(fmt, "builtin macros crate"),
32             CrateNum::ReservedForIncrCompCache => write!(fmt, "crate for decoding incr comp cache"),
33         }
34     }
35 }
36
37 /// Item definitions in the currently-compiled crate would have the CrateNum
38 /// LOCAL_CRATE in their DefId.
39 pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32_const(0));
40
41
42 impl Idx for CrateNum {
43     #[inline]
44     fn new(value: usize) -> Self {
45         CrateNum::Index(Idx::new(value))
46     }
47
48     #[inline]
49     fn index(self) -> usize {
50         match self {
51             CrateNum::Index(idx) => Idx::index(idx),
52             _ => bug!("Tried to get crate index of {:?}", self),
53         }
54     }
55 }
56
57 impl CrateNum {
58     pub fn new(x: usize) -> CrateNum {
59         CrateNum::from_usize(x)
60     }
61
62     pub fn from_usize(x: usize) -> CrateNum {
63         CrateNum::Index(CrateId::from_usize(x))
64     }
65
66     pub fn from_u32(x: u32) -> CrateNum {
67         CrateNum::Index(CrateId::from_u32(x))
68     }
69
70     pub fn as_usize(self) -> usize {
71         match self {
72             CrateNum::Index(id) => id.as_usize(),
73             _ => bug!("tried to get index of nonstandard crate {:?}", self),
74         }
75     }
76
77     pub fn as_u32(self) -> u32 {
78         match self {
79             CrateNum::Index(id) => id.as_u32(),
80             _ => bug!("tried to get index of nonstandard crate {:?}", self),
81         }
82     }
83
84     pub fn as_def_id(&self) -> DefId { DefId { krate: *self, index: CRATE_DEF_INDEX } }
85 }
86
87 impl fmt::Display for CrateNum {
88     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89         match self {
90             CrateNum::Index(id) => fmt::Display::fmt(&id.private, f),
91             CrateNum::BuiltinMacros => write!(f, "builtin macros crate"),
92             CrateNum::ReservedForIncrCompCache => write!(f, "crate for decoding incr comp cache"),
93         }
94     }
95 }
96
97 impl serialize::UseSpecializedEncodable for CrateNum {}
98 impl serialize::UseSpecializedDecodable for CrateNum {}
99
100 /// A DefIndex is an index into the hir-map for a crate, identifying a
101 /// particular definition. It should really be considered an interned
102 /// shorthand for a particular DefPath.
103 ///
104 /// At the moment we are allocating the numerical values of DefIndexes from two
105 /// address spaces: DefIndexAddressSpace::Low and DefIndexAddressSpace::High.
106 /// This allows us to allocate the DefIndexes of all item-likes
107 /// (Items, TraitItems, and ImplItems) into one of these spaces and
108 /// consequently use a simple array for lookup tables keyed by DefIndex and
109 /// known to be densely populated. This is especially important for the HIR map.
110 ///
111 /// Since the DefIndex is mostly treated as an opaque ID, you probably
112 /// don't have to care about these address spaces.
113
114 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
115 pub struct DefIndex(u32);
116
117 /// The crate root is always assigned index 0 by the AST Map code,
118 /// thanks to `NodeCollector::new`.
119 pub const CRATE_DEF_INDEX: DefIndex = DefIndex(0);
120
121 impl fmt::Debug for DefIndex {
122     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123         write!(f,
124                "DefIndex({}:{})",
125                self.address_space().index(),
126                self.as_array_index())
127     }
128 }
129
130 impl DefIndex {
131     #[inline]
132     pub fn address_space(&self) -> DefIndexAddressSpace {
133         match self.0 & 1 {
134             0 => DefIndexAddressSpace::Low,
135             1 => DefIndexAddressSpace::High,
136             _ => unreachable!()
137         }
138     }
139
140     /// Converts this DefIndex into a zero-based array index.
141     /// This index is the offset within the given DefIndexAddressSpace.
142     #[inline]
143     pub fn as_array_index(&self) -> usize {
144         (self.0 >> 1) as usize
145     }
146
147     #[inline]
148     pub fn from_array_index(i: usize, address_space: DefIndexAddressSpace) -> DefIndex {
149         DefIndex::from_raw_u32(((i << 1) | (address_space as usize)) as u32)
150     }
151
152     // Proc macros from a proc-macro crate have a kind of virtual DefIndex. This
153     // function maps the index of the macro within the crate (which is also the
154     // index of the macro in the CrateMetadata::proc_macros array) to the
155     // corresponding DefIndex.
156     pub fn from_proc_macro_index(proc_macro_index: usize) -> DefIndex {
157         // DefIndex for proc macros start from FIRST_FREE_HIGH_DEF_INDEX,
158         // because the first FIRST_FREE_HIGH_DEF_INDEX indexes are reserved
159         // for internal use.
160         let def_index = DefIndex::from_array_index(
161             proc_macro_index.checked_add(FIRST_FREE_HIGH_DEF_INDEX)
162                 .expect("integer overflow adding `proc_macro_index`"),
163             DefIndexAddressSpace::High);
164         assert!(def_index != CRATE_DEF_INDEX);
165         def_index
166     }
167
168     // This function is the reverse of from_proc_macro_index() above.
169     pub fn to_proc_macro_index(self: DefIndex) -> usize {
170         assert_eq!(self.address_space(), DefIndexAddressSpace::High);
171
172         self.as_array_index().checked_sub(FIRST_FREE_HIGH_DEF_INDEX)
173             .unwrap_or_else(|| {
174                 bug!("using local index {:?} as proc-macro index", self)
175             })
176     }
177
178     // Don't use this if you don't know about the DefIndex encoding.
179     pub fn from_raw_u32(x: u32) -> DefIndex {
180         DefIndex(x)
181     }
182
183     // Don't use this if you don't know about the DefIndex encoding.
184     pub fn as_raw_u32(&self) -> u32 {
185         self.0
186     }
187 }
188
189 impl serialize::UseSpecializedEncodable for DefIndex {}
190 impl serialize::UseSpecializedDecodable for DefIndex {}
191
192 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
193 pub enum DefIndexAddressSpace {
194     Low = 0,
195     High = 1,
196 }
197
198 impl DefIndexAddressSpace {
199     #[inline]
200     pub fn index(&self) -> usize {
201         *self as usize
202     }
203 }
204
205 /// A `DefId` identifies a particular *definition*, by combining a crate
206 /// index and a def index.
207 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
208 pub struct DefId {
209     pub krate: CrateNum,
210     pub index: DefIndex,
211 }
212
213 impl fmt::Debug for DefId {
214     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215         write!(f, "DefId({}/{}:{}",
216                self.krate,
217                self.index.address_space().index(),
218                self.index.as_array_index())?;
219
220         ty::tls::with_opt(|opt_tcx| {
221             if let Some(tcx) = opt_tcx {
222                 write!(f, " ~ {}", tcx.def_path_debug_str(*self))?;
223             }
224             Ok(())
225         })?;
226
227         write!(f, ")")
228     }
229 }
230
231 impl DefId {
232     /// Makes a local `DefId` from the given `DefIndex`.
233     #[inline]
234     pub fn local(index: DefIndex) -> DefId {
235         DefId { krate: LOCAL_CRATE, index: index }
236     }
237
238     #[inline]
239     pub fn is_local(self) -> bool {
240         self.krate == LOCAL_CRATE
241     }
242
243     #[inline]
244     pub fn to_local(self) -> LocalDefId {
245         LocalDefId::from_def_id(self)
246     }
247
248     pub fn describe_as_module(&self, tcx: TyCtxt<'_, '_, '_>) -> String {
249         if self.is_local() && self.index == CRATE_DEF_INDEX {
250             format!("top-level module")
251         } else {
252             format!("module `{}`", tcx.def_path_str(*self))
253         }
254     }
255
256     /// Check if a `DefId`'s path matches the given absolute type path usage.
257     // Uplifted from rust-lang/rust-clippy
258     pub fn match_path<'a, 'tcx>(self, tcx: TyCtxt<'a, 'tcx, 'tcx>, path: &[&str]) -> bool {
259         pub struct AbsolutePathPrinter<'a, 'tcx> {
260             pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
261         }
262
263         impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> {
264             type Error = !;
265
266             type Path = Vec<LocalInternedString>;
267             type Region = ();
268             type Type = ();
269             type DynExistential = ();
270
271             fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
272                 self.tcx
273             }
274
275             fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
276                 Ok(())
277             }
278
279             fn print_type(self, _ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
280                 Ok(())
281             }
282
283             fn print_dyn_existential(
284                 self,
285                 _predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
286                 ) -> Result<Self::DynExistential, Self::Error> {
287                 Ok(())
288             }
289
290             fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
291                 Ok(vec![self.tcx.original_crate_name(cnum).as_str()])
292             }
293
294             fn path_qualified(
295                 self,
296                 self_ty: Ty<'tcx>,
297                 trait_ref: Option<ty::TraitRef<'tcx>>,
298                 ) -> Result<Self::Path, Self::Error> {
299                 if trait_ref.is_none() {
300                     if let ty::Adt(def, substs) = self_ty.sty {
301                         return self.print_def_path(def.did, substs);
302                     }
303                 }
304
305                 // This shouldn't ever be needed, but just in case:
306                 Ok(vec![match trait_ref {
307                     Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)).as_str(),
308                     None => Symbol::intern(&format!("<{}>", self_ty)).as_str(),
309                 }])
310             }
311
312             fn path_append_impl(
313                 self,
314                 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
315                 _disambiguated_data: &DisambiguatedDefPathData,
316                 self_ty: Ty<'tcx>,
317                 trait_ref: Option<ty::TraitRef<'tcx>>,
318                 ) -> Result<Self::Path, Self::Error> {
319                 let mut path = print_prefix(self)?;
320
321                 // This shouldn't ever be needed, but just in case:
322                 path.push(match trait_ref {
323                     Some(trait_ref) => {
324                         Symbol::intern(&format!("<impl {} for {}>", trait_ref, self_ty)).as_str()
325                     },
326                     None => Symbol::intern(&format!("<impl {}>", self_ty)).as_str(),
327                 });
328
329                 Ok(path)
330             }
331
332             fn path_append(
333                 self,
334                 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
335                 disambiguated_data: &DisambiguatedDefPathData,
336                 ) -> Result<Self::Path, Self::Error> {
337                 let mut path = print_prefix(self)?;
338                 path.push(disambiguated_data.data.as_interned_str().as_str());
339                 Ok(path)
340             }
341
342             fn path_generic_args(
343                 self,
344                 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
345                 _args: &[Kind<'tcx>],
346                 ) -> Result<Self::Path, Self::Error> {
347                 print_prefix(self)
348             }
349         }
350
351         let names = AbsolutePathPrinter { tcx }.print_def_path(self, &[]).unwrap();
352
353         names.len() == path.len()
354             && names.into_iter().zip(path.iter()).all(|(a, &b)| *a == *b)
355     }
356 }
357
358 impl serialize::UseSpecializedEncodable for DefId {}
359 impl serialize::UseSpecializedDecodable for DefId {}
360
361 /// A LocalDefId is equivalent to a DefId with `krate == LOCAL_CRATE`. Since
362 /// we encode this information in the type, we can ensure at compile time that
363 /// no DefIds from upstream crates get thrown into the mix. There are quite a
364 /// few cases where we know that only DefIds from the local crate are expected
365 /// and a DefId from a different crate would signify a bug somewhere. This
366 /// is when LocalDefId comes in handy.
367 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
368 pub struct LocalDefId(DefIndex);
369
370 impl LocalDefId {
371     #[inline]
372     pub fn from_def_id(def_id: DefId) -> LocalDefId {
373         assert!(def_id.is_local());
374         LocalDefId(def_id.index)
375     }
376
377     #[inline]
378     pub fn to_def_id(self) -> DefId {
379         DefId {
380             krate: LOCAL_CRATE,
381             index: self.0
382         }
383     }
384 }
385
386 impl fmt::Debug for LocalDefId {
387     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
388         self.to_def_id().fmt(f)
389     }
390 }
391
392 impl serialize::UseSpecializedEncodable for LocalDefId {}
393 impl serialize::UseSpecializedDecodable for LocalDefId {}