2019-02-08 07:53:55 -06:00
|
|
|
//! For each definition, we track the following data. A definition
|
|
|
|
//! here is defined somewhat circularly as "something with a `DefId`",
|
2016-12-14 10:27:39 -06:00
|
|
|
//! but it generally corresponds to things like structs, enums, etc.
|
|
|
|
//! There are also some rather random cases (like const initializer
|
|
|
|
//! expressions) that are mostly just leftovers.
|
|
|
|
|
2020-03-20 22:39:48 -05:00
|
|
|
pub use crate::def_id::DefPathHash;
|
2021-06-28 12:29:55 -05:00
|
|
|
use crate::def_id::{CrateNum, DefIndex, LocalDefId, StableCrateId, CRATE_DEF_INDEX, LOCAL_CRATE};
|
2021-07-20 06:54:37 -05:00
|
|
|
use crate::def_path_hash_map::DefPathHashMap;
|
2020-03-20 22:39:48 -05:00
|
|
|
|
2017-08-01 06:44:20 -05:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2016-12-13 17:45:03 -06:00
|
|
|
use rustc_data_structures::stable_hasher::StableHasher;
|
2019-09-26 00:38:33 -05:00
|
|
|
use rustc_index::vec::IndexVec;
|
2020-09-24 04:23:01 -05:00
|
|
|
use rustc_span::symbol::{kw, sym, Symbol};
|
2020-01-07 07:38:27 -06:00
|
|
|
|
2020-08-31 16:57:48 -05:00
|
|
|
use std::fmt::{self, Write};
|
2017-04-03 12:20:26 -05:00
|
|
|
use std::hash::Hash;
|
2015-09-17 13:29:59 -05:00
|
|
|
|
2019-09-05 21:57:44 -05:00
|
|
|
/// The `DefPathTable` maps `DefIndex`es to `DefKey`s and vice versa.
|
|
|
|
/// Internally the `DefPathTable` holds a tree of `DefKey`s, where each `DefKey`
|
|
|
|
/// stores the `DefIndex` of its parent.
|
|
|
|
/// There is one `DefPathTable` for each crate.
|
2021-04-04 07:40:35 -05:00
|
|
|
#[derive(Clone, Default, Debug)]
|
2016-12-14 10:27:39 -06:00
|
|
|
pub struct DefPathTable {
|
2019-10-25 12:30:11 -05:00
|
|
|
index_to_key: IndexVec<DefIndex, DefKey>,
|
|
|
|
def_path_hashes: IndexVec<DefIndex, DefPathHash>,
|
2021-07-20 06:54:37 -05:00
|
|
|
def_path_hash_to_index: DefPathHashMap,
|
2017-03-16 12:17:18 -05:00
|
|
|
}
|
|
|
|
|
2016-12-14 10:27:39 -06:00
|
|
|
impl DefPathTable {
|
2019-05-08 14:07:12 -05:00
|
|
|
fn allocate(&mut self, key: DefKey, def_path_hash: DefPathHash) -> DefIndex {
|
2017-03-16 12:17:18 -05:00
|
|
|
let index = {
|
2019-05-18 06:19:33 -05:00
|
|
|
let index = DefIndex::from(self.index_to_key.len());
|
2017-03-16 12:17:18 -05:00
|
|
|
debug!("DefPathTable::insert() - {:?} <-> {:?}", key, index);
|
2019-05-08 14:07:12 -05:00
|
|
|
self.index_to_key.push(key);
|
2017-03-16 12:17:18 -05:00
|
|
|
index
|
|
|
|
};
|
2019-05-08 14:07:12 -05:00
|
|
|
self.def_path_hashes.push(def_path_hash);
|
|
|
|
debug_assert!(self.def_path_hashes.len() == self.index_to_key.len());
|
2021-01-27 07:28:07 -06:00
|
|
|
|
|
|
|
// Check for hash collisions of DefPathHashes. These should be
|
|
|
|
// exceedingly rare.
|
2021-07-20 06:54:37 -05:00
|
|
|
if let Some(existing) = self.def_path_hash_to_index.insert(&def_path_hash, &index) {
|
2021-01-27 07:28:07 -06:00
|
|
|
let def_path1 = DefPath::make(LOCAL_CRATE, existing, |idx| self.def_key(idx));
|
|
|
|
let def_path2 = DefPath::make(LOCAL_CRATE, index, |idx| self.def_key(idx));
|
|
|
|
|
|
|
|
// Continuing with colliding DefPathHashes can lead to correctness
|
|
|
|
// issues. We must abort compilation.
|
2021-02-04 04:01:52 -06:00
|
|
|
//
|
2022-03-01 06:02:47 -06:00
|
|
|
// The likelihood of such a collision is very small, so actually
|
2021-02-04 04:01:52 -06:00
|
|
|
// running into one could be indicative of a poor hash function
|
|
|
|
// being used.
|
|
|
|
//
|
|
|
|
// See the documentation for DefPathHash for more information.
|
2021-02-04 04:04:29 -06:00
|
|
|
panic!(
|
2022-03-30 00:42:10 -05:00
|
|
|
"found DefPathHash collision between {:?} and {:?}. \
|
2021-02-04 04:04:29 -06:00
|
|
|
Compilation cannot continue.",
|
|
|
|
def_path1, def_path2
|
|
|
|
);
|
2021-01-27 07:28:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that all DefPathHashes correctly contain the local crate's
|
|
|
|
// StableCrateId
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
if let Some(root) = self.def_path_hashes.get(CRATE_DEF_INDEX) {
|
|
|
|
assert!(def_path_hash.stable_crate_id() == root.stable_crate_id());
|
|
|
|
}
|
|
|
|
|
2016-12-14 10:27:39 -06:00
|
|
|
index
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn def_key(&self, index: DefIndex) -> DefKey {
|
2019-10-25 12:30:11 -05:00
|
|
|
self.index_to_key[index]
|
2016-12-14 10:27:39 -06:00
|
|
|
}
|
|
|
|
|
2017-04-03 12:20:26 -05:00
|
|
|
#[inline(always)]
|
2017-05-31 06:54:38 -05:00
|
|
|
pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
|
2019-10-25 12:30:11 -05:00
|
|
|
let hash = self.def_path_hashes[index];
|
|
|
|
debug!("def_path_hash({:?}) = {:?}", index, hash);
|
|
|
|
hash
|
2017-04-03 12:20:26 -05:00
|
|
|
}
|
|
|
|
|
2020-07-29 11:26:15 -05:00
|
|
|
pub fn enumerated_keys_and_path_hashes(
|
|
|
|
&self,
|
2021-07-20 06:54:37 -05:00
|
|
|
) -> impl Iterator<Item = (DefIndex, &DefKey, &DefPathHash)> + ExactSizeIterator + '_ {
|
2020-07-29 11:26:15 -05:00
|
|
|
self.index_to_key
|
|
|
|
.iter_enumerated()
|
|
|
|
.map(move |(index, key)| (index, key, &self.def_path_hashes[index]))
|
2017-05-31 07:53:39 -05:00
|
|
|
}
|
2016-12-14 10:27:39 -06:00
|
|
|
}
|
|
|
|
|
2016-12-16 18:12:37 -06:00
|
|
|
/// The definition table containing node definitions.
|
2020-06-21 09:49:38 -05:00
|
|
|
/// It holds the `DefPathTable` for `LocalDefId`s/`DefPath`s.
|
|
|
|
/// It also stores mappings to convert `LocalDefId`s to/from `HirId`s.
|
2021-04-04 07:40:35 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2015-09-17 13:29:59 -05:00
|
|
|
pub struct Definitions {
|
2016-12-14 10:27:39 -06:00
|
|
|
table: DefPathTable,
|
2021-04-02 09:47:08 -05:00
|
|
|
next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>,
|
2020-02-12 08:11:33 -06:00
|
|
|
|
2021-07-20 06:54:37 -05:00
|
|
|
/// The [StableCrateId] of the local crate.
|
|
|
|
stable_crate_id: StableCrateId,
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A unique identifier that we can use to lookup a definition
|
|
|
|
/// precisely. It combines the index of the definition's parent (if
|
|
|
|
/// any) with a `DisambiguatedDefPathData`.
|
2020-06-11 09:49:57 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Encodable, Decodable)]
|
2015-09-17 13:29:59 -05:00
|
|
|
pub struct DefKey {
|
2019-02-08 07:53:55 -06:00
|
|
|
/// The parent path.
|
2015-09-17 13:29:59 -05:00
|
|
|
pub parent: Option<DefIndex>,
|
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// The identifier of this node.
|
2015-09-17 13:29:59 -05:00
|
|
|
pub disambiguated_data: DisambiguatedDefPathData,
|
|
|
|
}
|
|
|
|
|
2017-04-03 12:20:26 -05:00
|
|
|
impl DefKey {
|
2021-02-04 04:04:29 -06:00
|
|
|
pub(crate) fn compute_stable_hash(&self, parent: DefPathHash) -> DefPathHash {
|
2017-04-03 12:20:26 -05:00
|
|
|
let mut hasher = StableHasher::new();
|
|
|
|
|
2021-01-27 07:28:07 -06:00
|
|
|
parent.hash(&mut hasher);
|
2017-06-13 06:47:13 -05:00
|
|
|
|
|
|
|
let DisambiguatedDefPathData { ref data, disambiguator } = self.disambiguated_data;
|
|
|
|
|
2020-10-13 03:17:05 -05:00
|
|
|
std::mem::discriminant(data).hash(&mut hasher);
|
2018-05-22 07:31:56 -05:00
|
|
|
if let Some(name) = data.get_opt_name() {
|
2019-10-20 22:25:08 -05:00
|
|
|
// Get a stable hash by considering the symbol chars rather than
|
|
|
|
// the symbol index.
|
|
|
|
name.as_str().hash(&mut hasher);
|
2018-05-22 07:31:56 -05:00
|
|
|
}
|
2017-06-13 06:47:13 -05:00
|
|
|
|
|
|
|
disambiguator.hash(&mut hasher);
|
|
|
|
|
2021-01-27 07:28:07 -06:00
|
|
|
let local_hash: u64 = hasher.finish();
|
2017-04-03 12:20:26 -05:00
|
|
|
|
2021-01-27 07:28:07 -06:00
|
|
|
// Construct the new DefPathHash, making sure that the `crate_id`
|
|
|
|
// portion of the hash is properly copied from the parent. This way the
|
|
|
|
// `crate_id` part will be recursively propagated from the root to all
|
|
|
|
// DefPathHashes in this DefPathTable.
|
|
|
|
DefPathHash::new(parent.stable_crate_id(), local_hash)
|
2017-04-03 12:20:26 -05:00
|
|
|
}
|
2022-04-08 16:06:20 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn get_opt_name(&self) -> Option<Symbol> {
|
|
|
|
self.disambiguated_data.data.get_opt_name()
|
|
|
|
}
|
2017-04-03 12:20:26 -05:00
|
|
|
}
|
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// A pair of `DefPathData` and an integer disambiguator. The integer is
|
2019-09-05 21:57:44 -05:00
|
|
|
/// normally `0`, but in the event that there are multiple defs with the
|
2015-09-17 13:29:59 -05:00
|
|
|
/// same `parent` and `data`, we use this field to disambiguate
|
|
|
|
/// between them. This introduces some artificial ordering dependency
|
2019-09-05 21:57:44 -05:00
|
|
|
/// but means that if you have, e.g., two impls for the same type in
|
2019-02-08 07:53:55 -06:00
|
|
|
/// the same module, they do get distinct `DefId`s.
|
2020-06-11 09:49:57 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Encodable, Decodable)]
|
2015-09-17 13:29:59 -05:00
|
|
|
pub struct DisambiguatedDefPathData {
|
|
|
|
pub data: DefPathData,
|
|
|
|
pub disambiguator: u32,
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:26:15 -05:00
|
|
|
impl DisambiguatedDefPathData {
|
|
|
|
pub fn fmt_maybe_verbose(&self, writer: &mut impl Write, verbose: bool) -> fmt::Result {
|
2020-08-31 18:42:30 -05:00
|
|
|
match self.data.name() {
|
2020-08-31 16:57:48 -05:00
|
|
|
DefPathDataName::Named(name) => {
|
2020-09-23 17:38:38 -05:00
|
|
|
if verbose && self.disambiguator != 0 {
|
2020-08-31 17:26:15 -05:00
|
|
|
write!(writer, "{}#{}", name, self.disambiguator)
|
2020-09-23 17:38:38 -05:00
|
|
|
} else {
|
2021-12-14 21:39:23 -06:00
|
|
|
writer.write_str(name.as_str())
|
2020-08-31 16:57:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
DefPathDataName::Anon { namespace } => {
|
2020-08-31 17:26:15 -05:00
|
|
|
write!(writer, "{{{}#{}}}", namespace, self.disambiguator)
|
2020-08-31 16:57:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:26:15 -05:00
|
|
|
impl fmt::Display for DisambiguatedDefPathData {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
self.fmt_maybe_verbose(f, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
#[derive(Clone, Debug, Encodable, Decodable)]
|
2016-03-16 04:40:14 -05:00
|
|
|
pub struct DefPath {
|
2019-02-08 07:53:55 -06:00
|
|
|
/// The path leading from the crate root to the item.
|
2016-03-16 04:40:14 -05:00
|
|
|
pub data: Vec<DisambiguatedDefPathData>,
|
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// The crate root this path is relative to.
|
2016-08-31 06:00:29 -05:00
|
|
|
pub krate: CrateNum,
|
2016-03-16 04:40:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DefPath {
|
2016-12-16 11:48:54 -06:00
|
|
|
pub fn make<FN>(krate: CrateNum, start_index: DefIndex, mut get_key: FN) -> DefPath
|
2016-03-16 04:40:14 -05:00
|
|
|
where
|
|
|
|
FN: FnMut(DefIndex) -> DefKey,
|
|
|
|
{
|
|
|
|
let mut data = vec![];
|
|
|
|
let mut index = Some(start_index);
|
|
|
|
loop {
|
2016-05-06 13:52:57 -05:00
|
|
|
debug!("DefPath::make: krate={:?} index={:?}", krate, index);
|
2016-03-16 04:40:14 -05:00
|
|
|
let p = index.unwrap();
|
|
|
|
let key = get_key(p);
|
2016-05-06 13:52:57 -05:00
|
|
|
debug!("DefPath::make: key={:?}", key);
|
2016-03-16 04:40:14 -05:00
|
|
|
match key.disambiguated_data.data {
|
|
|
|
DefPathData::CrateRoot => {
|
|
|
|
assert!(key.parent.is_none());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
data.push(key.disambiguated_data);
|
|
|
|
index = key.parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data.reverse();
|
2020-03-06 12:28:44 -06:00
|
|
|
DefPath { data, krate }
|
2016-03-16 04:40:14 -05:00
|
|
|
}
|
2016-08-01 18:55:20 -05:00
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Returns a string representation of the `DefPath` without
|
2017-03-14 09:50:40 -05:00
|
|
|
/// the crate-prefix. This method is useful if you don't have
|
2019-02-08 07:53:55 -06:00
|
|
|
/// a `TyCtxt` available.
|
2020-09-23 17:38:38 -05:00
|
|
|
pub fn to_string_no_crate_verbose(&self) -> String {
|
2017-03-14 09:50:40 -05:00
|
|
|
let mut s = String::with_capacity(self.data.len() * 16);
|
|
|
|
|
|
|
|
for component in &self.data {
|
2020-08-31 16:57:48 -05:00
|
|
|
write!(s, "::{}", component).unwrap();
|
2018-09-18 19:09:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Returns a filename-friendly string of the `DefPath`, without
|
2017-11-04 06:27:08 -05:00
|
|
|
/// the crate-prefix. This method is useful if you don't have
|
2019-02-08 07:53:55 -06:00
|
|
|
/// a `TyCtxt` available.
|
2017-11-04 06:27:08 -05:00
|
|
|
pub fn to_filename_friendly_no_crate(&self) -> String {
|
|
|
|
let mut s = String::with_capacity(self.data.len() * 16);
|
|
|
|
|
2017-11-05 02:14:00 -06:00
|
|
|
let mut opt_delimiter = None;
|
2017-11-04 06:27:08 -05:00
|
|
|
for component in &self.data {
|
2020-04-24 15:58:41 -05:00
|
|
|
s.extend(opt_delimiter);
|
2017-11-05 02:14:00 -06:00
|
|
|
opt_delimiter = Some('-');
|
2020-08-31 16:57:48 -05:00
|
|
|
write!(s, "{}", component).unwrap();
|
2017-11-04 06:27:08 -05:00
|
|
|
}
|
2020-08-31 16:57:48 -05:00
|
|
|
|
2017-11-04 06:27:08 -05:00
|
|
|
s
|
|
|
|
}
|
2016-03-16 04:40:14 -05:00
|
|
|
}
|
|
|
|
|
2020-06-11 09:49:57 -05:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
|
2015-09-17 13:29:59 -05:00
|
|
|
pub enum DefPathData {
|
|
|
|
// Root: these should only be used for the root nodes, because
|
|
|
|
// they are treated specially by the `def_path` function.
|
2019-09-05 21:57:44 -05:00
|
|
|
/// The crate root (marker).
|
2015-09-17 13:29:59 -05:00
|
|
|
CrateRoot,
|
2019-09-05 21:57:44 -05:00
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
// Different kinds of items and item-like things:
|
2019-09-05 21:57:44 -05:00
|
|
|
/// An impl.
|
2016-03-16 04:47:18 -05:00
|
|
|
Impl,
|
2021-12-17 02:45:15 -06:00
|
|
|
/// An `extern` block.
|
|
|
|
ForeignMod,
|
2022-05-12 13:12:35 -05:00
|
|
|
/// A `use` item.
|
|
|
|
Use,
|
|
|
|
/// A global asm item.
|
|
|
|
GlobalAsm,
|
2019-09-05 21:57:44 -05:00
|
|
|
/// Something in the type namespace.
|
2019-10-20 22:25:08 -05:00
|
|
|
TypeNs(Symbol),
|
2019-09-05 21:57:44 -05:00
|
|
|
/// Something in the value namespace.
|
2019-10-20 22:25:08 -05:00
|
|
|
ValueNs(Symbol),
|
2019-09-05 21:57:44 -05:00
|
|
|
/// Something in the macro namespace.
|
2019-10-20 22:25:08 -05:00
|
|
|
MacroNs(Symbol),
|
2019-09-05 21:57:44 -05:00
|
|
|
/// Something in the lifetime namespace.
|
2019-10-20 22:25:08 -05:00
|
|
|
LifetimeNs(Symbol),
|
2019-09-05 21:57:44 -05:00
|
|
|
/// A closure expression.
|
2015-09-17 13:29:59 -05:00
|
|
|
ClosureExpr,
|
2019-09-05 21:57:44 -05:00
|
|
|
|
|
|
|
// Subportions of items:
|
|
|
|
/// Implicit constructor for a unit or tuple-like struct or enum variant.
|
2019-03-24 09:49:58 -05:00
|
|
|
Ctor,
|
2019-09-05 21:57:44 -05:00
|
|
|
/// A constant expression (see `{ast,hir}::AnonConst`).
|
2018-05-17 13:28:50 -05:00
|
|
|
AnonConst,
|
2019-09-05 21:57:44 -05:00
|
|
|
/// An `impl Trait` type node.
|
2018-06-20 03:59:24 -05:00
|
|
|
ImplTrait,
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Definitions {
|
2016-12-16 11:51:36 -06:00
|
|
|
pub fn def_path_table(&self) -> &DefPathTable {
|
|
|
|
&self.table
|
|
|
|
}
|
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Gets the number of definitions.
|
2019-05-08 14:07:12 -05:00
|
|
|
pub fn def_index_count(&self) -> usize {
|
|
|
|
self.table.index_to_key.len()
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
|
2021-05-31 19:00:00 -05:00
|
|
|
#[inline]
|
2019-11-03 06:36:59 -06:00
|
|
|
pub fn def_key(&self, id: LocalDefId) -> DefKey {
|
|
|
|
self.table.def_key(id.local_def_index)
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
|
2017-04-03 12:20:26 -05:00
|
|
|
#[inline(always)]
|
2019-11-03 06:36:59 -06:00
|
|
|
pub fn def_path_hash(&self, id: LocalDefId) -> DefPathHash {
|
|
|
|
self.table.def_path_hash(id.local_def_index)
|
2017-04-03 12:20:26 -05:00
|
|
|
}
|
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
/// Returns the path from the crate root to `index`. The root
|
|
|
|
/// nodes are not included in the path (i.e., this will be an
|
|
|
|
/// empty vector for the crate root). For an inlined item, this
|
|
|
|
/// will be the path of the item in the external crate (but the
|
|
|
|
/// path will begin with the path to the external crate).
|
2019-11-03 06:36:59 -06:00
|
|
|
pub fn def_path(&self, id: LocalDefId) -> DefPath {
|
|
|
|
DefPath::make(LOCAL_CRATE, id.local_def_index, |index| {
|
|
|
|
self.def_key(LocalDefId { local_def_index: index })
|
|
|
|
})
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
|
2019-05-08 14:07:12 -05:00
|
|
|
/// Adds a root definition (no parent) and a few other reserved definitions.
|
2022-05-30 11:49:17 -05:00
|
|
|
pub fn new(stable_crate_id: StableCrateId) -> Definitions {
|
2017-04-03 12:20:26 -05:00
|
|
|
let key = DefKey {
|
|
|
|
parent: None,
|
|
|
|
disambiguated_data: DisambiguatedDefPathData {
|
|
|
|
data: DefPathData::CrateRoot,
|
|
|
|
disambiguator: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-01-27 07:28:07 -06:00
|
|
|
let parent_hash = DefPathHash::new(stable_crate_id, 0);
|
2017-04-03 12:20:26 -05:00
|
|
|
let def_path_hash = key.compute_stable_hash(parent_hash);
|
|
|
|
|
2020-06-21 09:49:38 -05:00
|
|
|
// Create the root definition.
|
|
|
|
let mut table = DefPathTable::default();
|
|
|
|
let root = LocalDefId { local_def_index: table.allocate(key, def_path_hash) };
|
2019-11-03 06:36:59 -06:00
|
|
|
assert_eq!(root.local_def_index, CRATE_DEF_INDEX);
|
|
|
|
|
2022-05-30 11:49:17 -05:00
|
|
|
Definitions { table, next_disambiguator: Default::default(), stable_crate_id }
|
2020-06-21 09:49:38 -05:00
|
|
|
}
|
|
|
|
|
2019-02-28 16:43:53 -06:00
|
|
|
/// Adds a definition with a parent definition.
|
2022-05-30 11:49:17 -05:00
|
|
|
pub fn create_def(&mut self, parent: LocalDefId, data: DefPathData) -> LocalDefId {
|
2022-07-16 13:30:17 -05:00
|
|
|
// We can't use `Debug` implementation for `LocalDefId` here, since it tries to acquire a
|
|
|
|
// reference to `Definitions` and we're already holding a mutable reference.
|
|
|
|
debug!(
|
|
|
|
"create_def(parent={}, data={data:?})",
|
|
|
|
self.def_path(parent).to_string_no_crate_verbose(),
|
|
|
|
);
|
2015-09-17 13:29:59 -05:00
|
|
|
|
2019-09-05 21:57:44 -05:00
|
|
|
// The root node must be created with `create_root_def()`.
|
2017-04-03 12:20:26 -05:00
|
|
|
assert!(data != DefPathData::CrateRoot);
|
2016-03-28 16:39:57 -05:00
|
|
|
|
2021-04-02 09:47:08 -05:00
|
|
|
// Find the next free disambiguator for this key.
|
|
|
|
let disambiguator = {
|
|
|
|
let next_disamb = self.next_disambiguator.entry((parent, data)).or_insert(0);
|
|
|
|
let disambiguator = *next_disamb;
|
|
|
|
*next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow");
|
|
|
|
disambiguator
|
|
|
|
};
|
2017-08-01 06:44:20 -05:00
|
|
|
let key = DefKey {
|
2019-11-03 06:36:59 -06:00
|
|
|
parent: Some(parent.local_def_index),
|
2015-09-17 13:29:59 -05:00
|
|
|
disambiguated_data: DisambiguatedDefPathData { data, disambiguator },
|
|
|
|
};
|
|
|
|
|
2019-11-03 06:36:59 -06:00
|
|
|
let parent_hash = self.table.def_path_hash(parent.local_def_index);
|
2017-04-03 12:20:26 -05:00
|
|
|
let def_path_hash = key.compute_stable_hash(parent_hash);
|
|
|
|
|
2020-06-21 09:49:38 -05:00
|
|
|
debug!("create_def: after disambiguation, key = {:?}", key);
|
2016-03-28 16:39:57 -05:00
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
// Create the definition.
|
2022-05-30 11:49:17 -05:00
|
|
|
LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) }
|
2021-04-04 09:04:38 -05:00
|
|
|
}
|
|
|
|
|
2020-11-28 15:07:00 -06:00
|
|
|
pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
|
2021-09-22 12:28:20 -05:00
|
|
|
self.table.def_path_hashes.indices().map(|local_def_index| LocalDefId { local_def_index })
|
2020-11-28 15:07:00 -06:00
|
|
|
}
|
2021-06-08 15:18:53 -05:00
|
|
|
|
|
|
|
#[inline(always)]
|
2022-01-19 16:36:44 -06:00
|
|
|
pub fn local_def_path_hash_to_def_id(
|
|
|
|
&self,
|
|
|
|
hash: DefPathHash,
|
|
|
|
err: &mut dyn FnMut() -> !,
|
|
|
|
) -> LocalDefId {
|
2021-07-20 06:54:37 -05:00
|
|
|
debug_assert!(hash.stable_crate_id() == self.stable_crate_id);
|
2021-06-08 15:18:53 -05:00
|
|
|
self.table
|
|
|
|
.def_path_hash_to_index
|
|
|
|
.get(&hash)
|
2021-07-20 06:54:37 -05:00
|
|
|
.map(|local_def_index| LocalDefId { local_def_index })
|
2022-01-19 16:36:44 -06:00
|
|
|
.unwrap_or_else(|| err())
|
2021-07-20 06:54:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn def_path_hash_to_def_index_map(&self) -> &DefPathHashMap {
|
|
|
|
&self.table.def_path_hash_to_index
|
2021-06-08 15:18:53 -05:00
|
|
|
}
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:26:15 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
2020-08-31 12:11:44 -05:00
|
|
|
pub enum DefPathDataName {
|
|
|
|
Named(Symbol),
|
|
|
|
Anon { namespace: Symbol },
|
|
|
|
}
|
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
impl DefPathData {
|
2019-10-20 22:25:08 -05:00
|
|
|
pub fn get_opt_name(&self) -> Option<Symbol> {
|
2016-09-17 05:34:55 -05:00
|
|
|
use self::DefPathData::*;
|
|
|
|
match *self {
|
2019-11-05 05:49:36 -06:00
|
|
|
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
|
2019-12-22 16:42:04 -06:00
|
|
|
|
2022-05-12 13:12:35 -05:00
|
|
|
Impl | ForeignMod | CrateRoot | Use | GlobalAsm | ClosureExpr | Ctor | AnonConst
|
|
|
|
| ImplTrait => None,
|
2016-09-17 05:34:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:26:15 -05:00
|
|
|
pub fn name(&self) -> DefPathDataName {
|
2015-09-17 13:29:59 -05:00
|
|
|
use self::DefPathData::*;
|
2019-10-20 22:25:08 -05:00
|
|
|
match *self {
|
2020-08-31 12:11:44 -05:00
|
|
|
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => {
|
|
|
|
DefPathDataName::Named(name)
|
|
|
|
}
|
2019-02-28 16:43:53 -06:00
|
|
|
// Note that this does not show up in user print-outs.
|
2020-08-31 12:11:44 -05:00
|
|
|
CrateRoot => DefPathDataName::Anon { namespace: kw::Crate },
|
|
|
|
Impl => DefPathDataName::Anon { namespace: kw::Impl },
|
2021-12-17 02:45:15 -06:00
|
|
|
ForeignMod => DefPathDataName::Anon { namespace: kw::Extern },
|
2022-05-12 13:12:35 -05:00
|
|
|
Use => DefPathDataName::Anon { namespace: kw::Use },
|
|
|
|
GlobalAsm => DefPathDataName::Anon { namespace: sym::global_asm },
|
2020-08-31 12:11:44 -05:00
|
|
|
ClosureExpr => DefPathDataName::Anon { namespace: sym::closure },
|
|
|
|
Ctor => DefPathDataName::Anon { namespace: sym::constructor },
|
|
|
|
AnonConst => DefPathDataName::Anon { namespace: sym::constant },
|
|
|
|
ImplTrait => DefPathDataName::Anon { namespace: sym::opaque },
|
2019-10-20 22:25:08 -05:00
|
|
|
}
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
2020-08-31 16:57:48 -05:00
|
|
|
}
|
2015-09-17 13:29:59 -05:00
|
|
|
|
2020-08-31 16:57:48 -05:00
|
|
|
impl fmt::Display for DefPathData {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-08-31 17:26:15 -05:00
|
|
|
match self.name() {
|
2021-12-14 21:39:23 -06:00
|
|
|
DefPathDataName::Named(name) => f.write_str(name.as_str()),
|
2020-09-23 17:38:38 -05:00
|
|
|
// FIXME(#70334): this will generate legacy {{closure}}, {{impl}}, etc
|
2020-08-31 16:57:48 -05:00
|
|
|
DefPathDataName::Anon { namespace } => write!(f, "{{{{{}}}}}", namespace),
|
2020-08-31 12:11:44 -05:00
|
|
|
}
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
}
|