1 use crate::ty::{self, TyCtxt};
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 _ => bug!("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 _ => bug!("tried to get index of non-standard crate {:?}", self),
68 pub fn as_u32(self) -> u32 {
70 CrateNum::Index(id) => id.as_u32(),
71 _ => bug!("tried to get index of non-standard crate {:?}", self),
75 pub fn as_def_id(&self) -> DefId { DefId { krate: *self, index: CRATE_DEF_INDEX } }
78 impl fmt::Display for CrateNum {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81 CrateNum::Index(id) => fmt::Display::fmt(&id.private, f),
82 CrateNum::ReservedForIncrCompCache => write!(f, "crate for decoding incr comp cache"),
87 impl rustc_serialize::UseSpecializedEncodable for CrateNum {}
88 impl rustc_serialize::UseSpecializedDecodable for CrateNum {}
90 rustc_index::newtype_index! {
91 /// A DefIndex is an index into the hir-map for a crate, identifying a
92 /// particular definition. It should really be considered an interned
93 /// shorthand for a particular DefPath.
95 DEBUG_FORMAT = "DefIndex({})",
97 /// The crate root is always assigned index 0 by the AST Map code,
98 /// thanks to `NodeCollector::new`.
99 const CRATE_DEF_INDEX = 0,
103 impl rustc_serialize::UseSpecializedEncodable for DefIndex {}
104 impl rustc_serialize::UseSpecializedDecodable for DefIndex {}
106 /// A `DefId` identifies a particular *definition*, by combining a crate
107 /// index and a def index.
108 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
114 impl fmt::Debug for DefId {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 write!(f, "DefId({}:{}", self.krate, self.index.index())?;
118 ty::tls::with_opt(|opt_tcx| {
119 if let Some(tcx) = opt_tcx {
120 write!(f, " ~ {}", tcx.def_path_debug_str(*self))?;
130 /// Makes a local `DefId` from the given `DefIndex`.
132 pub fn local(index: DefIndex) -> DefId {
133 DefId { krate: LOCAL_CRATE, index: index }
137 pub fn is_local(self) -> bool {
138 self.krate == LOCAL_CRATE
142 pub fn to_local(self) -> LocalDefId {
143 LocalDefId::from_def_id(self)
146 pub fn describe_as_module(&self, tcx: TyCtxt<'_>) -> String {
147 if self.is_local() && self.index == CRATE_DEF_INDEX {
148 format!("top-level module")
150 format!("module `{}`", tcx.def_path_str(*self))
155 impl rustc_serialize::UseSpecializedEncodable for DefId {}
156 impl rustc_serialize::UseSpecializedDecodable for DefId {}
158 /// A LocalDefId is equivalent to a DefId with `krate == LOCAL_CRATE`. Since
159 /// we encode this information in the type, we can ensure at compile time that
160 /// no DefIds from upstream crates get thrown into the mix. There are quite a
161 /// few cases where we know that only DefIds from the local crate are expected
162 /// and a DefId from a different crate would signify a bug somewhere. This
163 /// is when LocalDefId comes in handy.
164 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
165 pub struct LocalDefId(DefIndex);
169 pub fn from_def_id(def_id: DefId) -> LocalDefId {
170 assert!(def_id.is_local());
171 LocalDefId(def_id.index)
175 pub fn to_def_id(self) -> DefId {
183 impl fmt::Debug for LocalDefId {
184 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
185 self.to_def_id().fmt(f)
189 impl rustc_serialize::UseSpecializedEncodable for LocalDefId {}
190 impl rustc_serialize::UseSpecializedDecodable for LocalDefId {}