2019-12-25 03:51:27 +01:00
|
|
|
use rustc_data_structures::AtomicRef;
|
2019-09-26 05:38:33 +00:00
|
|
|
use rustc_index::vec::Idx;
|
2019-12-21 21:37:15 +11:00
|
|
|
use rustc_serialize::{Decoder, Encoder};
|
2015-08-16 06:31:58 -04:00
|
|
|
use std::fmt;
|
2019-12-21 21:37:15 +11:00
|
|
|
use std::{u32, u64};
|
2015-08-16 06:31:58 -04:00
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
rustc_index::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 {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// A special `CrateNum` that we use for the `tcx.rcache` when decoding from
|
2018-09-11 11:18:58 +02:00
|
|
|
/// 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
|
|
|
|
2019-09-06 03:57:44 +01: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
|
|
|
|
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),
|
2019-12-25 03:51:27 +01:00
|
|
|
_ => panic!("Tried to get crate index of {:?}", self),
|
2018-09-11 11:18:58 +02:00
|
|
|
}
|
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-12-25 03:51:27 +01:00
|
|
|
_ => panic!("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-12-25 03:51:27 +01:00
|
|
|
_ => panic!("tried to get index of non-standard crate {:?}", self),
|
2018-09-11 12:06:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05: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-12-21 21:37:15 +11:00
|
|
|
/// As a local identifier, a `CrateNum` is only meaningful within its context, e.g. within a tcx.
|
|
|
|
/// Therefore, make sure to include the context when encode a `CrateNum`.
|
|
|
|
impl rustc_serialize::UseSpecializedEncodable for CrateNum {
|
|
|
|
fn default_encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
|
|
|
|
e.emit_u32(self.as_u32())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl rustc_serialize::UseSpecializedDecodable for CrateNum {
|
|
|
|
fn default_decode<D: Decoder>(d: &mut D) -> Result<CrateNum, D::Error> {
|
|
|
|
Ok(CrateNum::from_u32(d.read_u32()?))
|
|
|
|
}
|
|
|
|
}
|
2016-08-31 14:00:29 +03:00
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
rustc_index::newtype_index! {
|
2019-05-18 13:19:33 +02:00
|
|
|
/// 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 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-12-25 01:27:51 +01:00
|
|
|
pub fn is_top_level_module(self) -> bool {
|
|
|
|
self.is_local() && self.index == CRATE_DEF_INDEX
|
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-12-21 21:37:15 +11:00
|
|
|
impl rustc_serialize::UseSpecializedEncodable for DefId {
|
|
|
|
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
|
|
|
let krate = u64::from(self.krate.as_u32());
|
|
|
|
let index = u64::from(self.index.as_u32());
|
|
|
|
s.emit_u64((krate << 32) | index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl rustc_serialize::UseSpecializedDecodable for DefId {
|
|
|
|
fn default_decode<D: Decoder>(d: &mut D) -> Result<DefId, D::Error> {
|
|
|
|
let def_id = d.read_u64()?;
|
|
|
|
let krate = CrateNum::from_u32((def_id >> 32) as u32);
|
|
|
|
let index = DefIndex::from_u32((def_id & 0xffffffff) as u32);
|
|
|
|
Ok(DefId { krate, index })
|
|
|
|
}
|
|
|
|
}
|
2017-11-16 14:04:01 +01:00
|
|
|
|
2019-12-25 03:51:27 +01:00
|
|
|
pub fn default_def_id_debug(def_id: DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_struct("DefId").field("krate", &def_id.krate).field("index", &def_id.index).finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub static DEF_ID_DEBUG: AtomicRef<fn(DefId, &mut fmt::Formatter<'_>) -> fmt::Result> =
|
|
|
|
AtomicRef::new(&(default_def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
|
|
|
|
|
|
|
|
impl fmt::Debug for DefId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
(*DEF_ID_DEBUG)(*self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-25 00:10:10 +01:00
|
|
|
rustc_data_structures::define_id_collections!(DefIdMap, DefIdSet, DefId);
|
|
|
|
|
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 {
|
2019-12-22 17:42:04 -05:00
|
|
|
DefId { krate: LOCAL_CRATE, index: self.0 }
|
2017-11-16 14:04:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {}
|