2019-04-05 23:04:56 +02:00
|
|
|
use crate::ty::{self, TyCtxt};
|
2016-08-31 14:00:29 +03:00
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
2015-08-16 06:31:58 -04:00
|
|
|
use std::fmt;
|
2015-09-17 14:29:59 -04:00
|
|
|
use std::u32;
|
2015-08-16 06:31:58 -04:00
|
|
|
|
2018-07-25 13:41:32 +03:00
|
|
|
newtype_index! {
|
2018-09-11 12:06:37 +02:00
|
|
|
pub struct CrateId {
|
2017-10-31 23:14:13 -04:00
|
|
|
ENCODABLE = custom
|
2018-09-11 11:18:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub enum CrateNum {
|
|
|
|
/// A special CrateNum that we use for the tcx.rcache when decoding from
|
|
|
|
/// the incr. comp. cache.
|
|
|
|
ReservedForIncrCompCache,
|
|
|
|
Index(CrateId),
|
|
|
|
}
|
2017-10-31 23:14:13 -04:00
|
|
|
|
2018-09-11 11:18:58 +02:00
|
|
|
impl ::std::fmt::Debug for CrateNum {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
|
2018-09-11 11:18:58 +02:00
|
|
|
match self {
|
2018-09-11 12:06:37 +02:00
|
|
|
CrateNum::Index(id) => write!(fmt, "crate{}", id.private),
|
2018-09-11 11:18:58 +02:00
|
|
|
CrateNum::ReservedForIncrCompCache => write!(fmt, "crate for decoding incr comp cache"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-29 00:13:49 -04:00
|
|
|
|
2018-09-11 11:18:58 +02:00
|
|
|
/// Item definitions in the currently-compiled crate would have the CrateNum
|
|
|
|
/// LOCAL_CRATE in their DefId.
|
2018-09-11 12:06:37 +02:00
|
|
|
pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32_const(0));
|
2017-10-29 00:13:49 -04:00
|
|
|
|
2017-11-14 12:03:57 +01:00
|
|
|
|
2018-09-11 11:18:58 +02:00
|
|
|
impl Idx for CrateNum {
|
|
|
|
#[inline]
|
|
|
|
fn new(value: usize) -> Self {
|
|
|
|
CrateNum::Index(Idx::new(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(self) -> usize {
|
|
|
|
match self {
|
|
|
|
CrateNum::Index(idx) => Idx::index(idx),
|
|
|
|
_ => bug!("Tried to get crate index of {:?}", self),
|
|
|
|
}
|
2018-07-25 13:41:32 +03:00
|
|
|
}
|
|
|
|
}
|
2016-10-28 06:52:45 +00:00
|
|
|
|
2016-08-31 14:00:29 +03:00
|
|
|
impl CrateNum {
|
|
|
|
pub fn new(x: usize) -> CrateNum {
|
2018-08-23 07:46:53 -04:00
|
|
|
CrateNum::from_usize(x)
|
2016-08-31 14:00:29 +03:00
|
|
|
}
|
2017-06-14 22:49:07 -07:00
|
|
|
|
2018-09-11 12:06:37 +02:00
|
|
|
pub fn from_usize(x: usize) -> CrateNum {
|
|
|
|
CrateNum::Index(CrateId::from_usize(x))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_u32(x: u32) -> CrateNum {
|
|
|
|
CrateNum::Index(CrateId::from_u32(x))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_usize(self) -> usize {
|
|
|
|
match self {
|
|
|
|
CrateNum::Index(id) => id.as_usize(),
|
2019-07-21 15:51:43 +02:00
|
|
|
_ => bug!("tried to get index of non-standard crate {:?}", self),
|
2018-09-11 12:06:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_u32(self) -> u32 {
|
|
|
|
match self {
|
|
|
|
CrateNum::Index(id) => id.as_u32(),
|
2019-07-21 15:51:43 +02:00
|
|
|
_ => bug!("tried to get index of non-standard crate {:?}", self),
|
2018-09-11 12:06:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-14 22:49:07 -07:00
|
|
|
pub fn as_def_id(&self) -> DefId { DefId { krate: *self, index: CRATE_DEF_INDEX } }
|
2016-08-31 14:00:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for CrateNum {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-09-11 11:18:58 +02:00
|
|
|
match self {
|
2018-09-11 12:06:37 +02:00
|
|
|
CrateNum::Index(id) => fmt::Display::fmt(&id.private, f),
|
2018-09-11 11:18:58 +02:00
|
|
|
CrateNum::ReservedForIncrCompCache => write!(f, "crate for decoding incr comp cache"),
|
|
|
|
}
|
2016-08-31 14:00:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 18:50:47 +03:00
|
|
|
impl rustc_serialize::UseSpecializedEncodable for CrateNum {}
|
|
|
|
impl rustc_serialize::UseSpecializedDecodable for CrateNum {}
|
2016-08-31 14:00:29 +03:00
|
|
|
|
2019-05-18 13:19:33 +02:00
|
|
|
newtype_index! {
|
|
|
|
/// A DefIndex is an index into the hir-map for a crate, identifying a
|
|
|
|
/// particular definition. It should really be considered an interned
|
|
|
|
/// shorthand for a particular DefPath.
|
|
|
|
pub struct DefIndex {
|
|
|
|
DEBUG_FORMAT = "DefIndex({})",
|
2017-11-04 17:48:05 -03:00
|
|
|
|
2019-05-18 13:19:33 +02:00
|
|
|
/// The crate root is always assigned index 0 by the AST Map code,
|
|
|
|
/// thanks to `NodeCollector::new`.
|
|
|
|
const CRATE_DEF_INDEX = 0,
|
2017-09-04 22:41:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 18:50:47 +03:00
|
|
|
impl rustc_serialize::UseSpecializedEncodable for DefIndex {}
|
|
|
|
impl rustc_serialize::UseSpecializedDecodable for DefIndex {}
|
2017-11-13 15:46:46 +01:00
|
|
|
|
2018-09-24 04:25:23 +01:00
|
|
|
/// A `DefId` identifies a particular *definition*, by combining a crate
|
2015-09-17 14:29:59 -04:00
|
|
|
/// index and a def index.
|
2018-03-21 01:58:25 +03:00
|
|
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
|
2015-08-16 06:31:58 -04:00
|
|
|
pub struct DefId {
|
|
|
|
pub krate: CrateNum,
|
2015-09-17 14:29:59 -04:00
|
|
|
pub index: DefIndex,
|
2015-08-16 06:31:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for DefId {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-05-18 13:19:33 +02:00
|
|
|
write!(f, "DefId({}:{}", self.krate, self.index.index())?;
|
2015-08-16 08:52:36 -04:00
|
|
|
|
2016-09-02 18:38:54 -04:00
|
|
|
ty::tls::with_opt(|opt_tcx| {
|
|
|
|
if let Some(tcx) = opt_tcx {
|
2017-11-20 17:49:21 +02:00
|
|
|
write!(f, " ~ {}", tcx.def_path_debug_str(*self))?;
|
2016-09-02 18:38:54 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})?;
|
2015-08-16 08:52:36 -04:00
|
|
|
|
2017-11-20 17:49:21 +02:00
|
|
|
write!(f, ")")
|
2015-08-16 06:31:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DefId {
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Makes a local `DefId` from the given `DefIndex`.
|
2017-11-16 14:04:01 +01:00
|
|
|
#[inline]
|
2015-09-17 14:29:59 -04:00
|
|
|
pub fn local(index: DefIndex) -> DefId {
|
|
|
|
DefId { krate: LOCAL_CRATE, index: index }
|
2015-08-16 06:31:58 -04:00
|
|
|
}
|
|
|
|
|
2017-11-16 14:04:01 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn is_local(self) -> bool {
|
2015-08-16 06:31:58 -04:00
|
|
|
self.krate == LOCAL_CRATE
|
|
|
|
}
|
2017-11-16 14:04:01 +01:00
|
|
|
|
2017-11-16 18:07:49 +01:00
|
|
|
#[inline]
|
2017-11-16 14:04:01 +01:00
|
|
|
pub fn to_local(self) -> LocalDefId {
|
|
|
|
LocalDefId::from_def_id(self)
|
|
|
|
}
|
2018-06-06 22:13:52 +02:00
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn describe_as_module(&self, tcx: TyCtxt<'_>) -> String {
|
2018-06-06 22:13:52 +02:00
|
|
|
if self.is_local() && self.index == CRATE_DEF_INDEX {
|
|
|
|
format!("top-level module")
|
|
|
|
} else {
|
2018-12-19 12:31:35 +02:00
|
|
|
format!("module `{}`", tcx.def_path_str(*self))
|
2018-06-06 22:13:52 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-16 06:31:58 -04:00
|
|
|
}
|
2017-11-13 15:46:46 +01:00
|
|
|
|
2019-07-23 18:50:47 +03:00
|
|
|
impl rustc_serialize::UseSpecializedEncodable for DefId {}
|
|
|
|
impl rustc_serialize::UseSpecializedDecodable for DefId {}
|
2017-11-16 14:04:01 +01:00
|
|
|
|
2017-11-16 18:07:49 +01:00
|
|
|
/// A LocalDefId is equivalent to a DefId with `krate == LOCAL_CRATE`. Since
|
|
|
|
/// we encode this information in the type, we can ensure at compile time that
|
|
|
|
/// no DefIds from upstream crates get thrown into the mix. There are quite a
|
|
|
|
/// few cases where we know that only DefIds from the local crate are expected
|
|
|
|
/// and a DefId from a different crate would signify a bug somewhere. This
|
|
|
|
/// is when LocalDefId comes in handy.
|
2018-03-21 01:58:25 +03:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
2017-11-16 14:04:01 +01:00
|
|
|
pub struct LocalDefId(DefIndex);
|
|
|
|
|
|
|
|
impl LocalDefId {
|
|
|
|
#[inline]
|
|
|
|
pub fn from_def_id(def_id: DefId) -> LocalDefId {
|
|
|
|
assert!(def_id.is_local());
|
|
|
|
LocalDefId(def_id.index)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn to_def_id(self) -> DefId {
|
|
|
|
DefId {
|
|
|
|
krate: LOCAL_CRATE,
|
|
|
|
index: self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for LocalDefId {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-11-16 14:04:01 +01:00
|
|
|
self.to_def_id().fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 18:50:47 +03:00
|
|
|
impl rustc_serialize::UseSpecializedEncodable for LocalDefId {}
|
|
|
|
impl rustc_serialize::UseSpecializedDecodable for LocalDefId {}
|