2015-09-17 13:29:59 -05:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-12-14 10:27:39 -06:00
|
|
|
//! For each definition, we track the following data. A definition
|
|
|
|
//! here is defined somewhat circularly as "something with a def-id",
|
|
|
|
//! 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.
|
|
|
|
|
2017-03-14 09:50:40 -05:00
|
|
|
use hir;
|
2017-06-07 03:45:20 -05:00
|
|
|
use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE, DefIndexAddressSpace,
|
|
|
|
CRATE_DEF_INDEX};
|
2017-05-18 03:54:20 -05:00
|
|
|
use ich::Fingerprint;
|
2017-08-01 06:44:20 -05:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2018-01-02 05:36:12 -06:00
|
|
|
use rustc_data_structures::indexed_vec::{IndexVec};
|
2016-12-13 17:45:03 -06:00
|
|
|
use rustc_data_structures::stable_hasher::StableHasher;
|
2016-12-14 10:27:39 -06:00
|
|
|
use serialize::{Encodable, Decodable, Encoder, Decoder};
|
2017-10-24 10:49:58 -05:00
|
|
|
use session::CrateDisambiguator;
|
2018-07-03 04:16:38 -05:00
|
|
|
use std::borrow::Borrow;
|
2016-08-01 18:55:20 -05:00
|
|
|
use std::fmt::Write;
|
2017-04-03 12:20:26 -05:00
|
|
|
use std::hash::Hash;
|
2017-06-13 06:47:13 -05:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::ext::hygiene::Mark;
|
2016-11-16 02:21:52 -06:00
|
|
|
use syntax::symbol::{Symbol, InternedString};
|
2018-02-17 06:22:58 -06:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2015-09-17 13:29:59 -05:00
|
|
|
use util::nodemap::NodeMap;
|
|
|
|
|
2016-12-16 18:12:37 -06:00
|
|
|
/// The DefPathTable maps DefIndexes to DefKeys and vice versa.
|
|
|
|
/// Internally the DefPathTable holds a tree of DefKeys, where each DefKey
|
|
|
|
/// stores the DefIndex of its parent.
|
|
|
|
/// There is one DefPathTable for each crate.
|
2016-12-14 10:27:39 -06:00
|
|
|
pub struct DefPathTable {
|
2017-03-16 12:17:18 -05:00
|
|
|
index_to_key: [Vec<DefKey>; 2],
|
2017-05-31 06:54:38 -05:00
|
|
|
def_path_hashes: [Vec<DefPathHash>; 2],
|
2016-12-14 10:27:39 -06:00
|
|
|
}
|
|
|
|
|
2017-03-16 12:17:18 -05:00
|
|
|
// Unfortunately we have to provide a manual impl of Clone because of the
|
|
|
|
// fixed-sized array field.
|
|
|
|
impl Clone for DefPathTable {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
DefPathTable {
|
|
|
|
index_to_key: [self.index_to_key[0].clone(),
|
|
|
|
self.index_to_key[1].clone()],
|
2017-04-03 12:20:26 -05:00
|
|
|
def_path_hashes: [self.def_path_hashes[0].clone(),
|
|
|
|
self.def_path_hashes[1].clone()],
|
2017-03-16 12:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 10:27:39 -06:00
|
|
|
impl DefPathTable {
|
2017-03-16 12:17:18 -05:00
|
|
|
|
|
|
|
fn allocate(&mut self,
|
|
|
|
key: DefKey,
|
2017-05-31 06:54:38 -05:00
|
|
|
def_path_hash: DefPathHash,
|
2017-03-16 12:17:18 -05:00
|
|
|
address_space: DefIndexAddressSpace)
|
|
|
|
-> DefIndex {
|
|
|
|
let index = {
|
|
|
|
let index_to_key = &mut self.index_to_key[address_space.index()];
|
2018-01-02 05:36:12 -06:00
|
|
|
let index = DefIndex::from_array_index(index_to_key.len(), address_space);
|
2017-03-16 12:17:18 -05:00
|
|
|
debug!("DefPathTable::insert() - {:?} <-> {:?}", key, index);
|
2017-07-20 08:32:06 -05:00
|
|
|
index_to_key.push(key);
|
2017-03-16 12:17:18 -05:00
|
|
|
index
|
|
|
|
};
|
2017-04-03 12:20:26 -05:00
|
|
|
self.def_path_hashes[address_space.index()].push(def_path_hash);
|
|
|
|
debug_assert!(self.def_path_hashes[address_space.index()].len() ==
|
|
|
|
self.index_to_key[address_space.index()].len());
|
2016-12-14 10:27:39 -06:00
|
|
|
index
|
|
|
|
}
|
|
|
|
|
Generate documentation for auto-trait impls
A new section is added to both both struct and trait doc pages.
On struct/enum pages, a new 'Auto Trait Implementations' section displays any
synthetic implementations for auto traits. Currently, this is only done
for Send and Sync.
On trait pages, a new 'Auto Implementors' section displays all types
which automatically implement the trait. Effectively, this is a list of
all public types in the standard library.
Synthesized impls for a particular auto trait ('synthetic impls') take
into account generic bounds. For example, a type 'struct Foo<T>(T)' will
have 'impl<T> Send for Foo<T> where T: Send' generated for it.
Manual implementations of auto traits are also taken into account. If we have
the following types:
'struct Foo<T>(T)'
'struct Wrapper<T>(Foo<T>)'
'unsafe impl<T> Send for Wrapper<T>' // pretend that Wrapper<T> makes
this sound somehow
Then Wrapper will have the following impl generated:
'impl<T> Send for Wrapper<T>'
reflecting the fact that 'T: Send' need not hold for 'Wrapper<T>: Send'
to hold
Lifetimes, HRTBS, and projections (e.g. '<T as Iterator>::Item') are
taken into account by synthetic impls
However, if a type can *never* implement a particular auto trait
(e.g. 'struct MyStruct<T>(*const T)'), then a negative impl will be
generated (in this case, 'impl<T> !Send for MyStruct<T>')
All of this means that a user should be able to copy-paste a synthetic
impl into their code, without any observable changes in behavior
(assuming the rest of the program remains unchanged).
2017-11-22 15:16:55 -06:00
|
|
|
pub fn next_id(&self, address_space: DefIndexAddressSpace) -> DefIndex {
|
|
|
|
DefIndex::from_array_index(self.index_to_key[address_space.index()].len(), address_space)
|
|
|
|
}
|
|
|
|
|
2016-12-14 10:27:39 -06:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn def_key(&self, index: DefIndex) -> DefKey {
|
2017-03-16 12:17:18 -05:00
|
|
|
self.index_to_key[index.address_space().index()]
|
|
|
|
[index.as_array_index()].clone()
|
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 {
|
2017-09-01 11:24:02 -05:00
|
|
|
let ret = self.def_path_hashes[index.address_space().index()]
|
|
|
|
[index.as_array_index()];
|
|
|
|
debug!("def_path_hash({:?}) = {:?}", index, ret);
|
|
|
|
return ret
|
2017-04-03 12:20:26 -05:00
|
|
|
}
|
|
|
|
|
2017-05-31 07:53:39 -05:00
|
|
|
pub fn add_def_path_hashes_to(&self,
|
|
|
|
cnum: CrateNum,
|
|
|
|
out: &mut FxHashMap<DefPathHash, DefId>) {
|
2018-01-02 05:36:12 -06:00
|
|
|
for &address_space in &[DefIndexAddressSpace::Low, DefIndexAddressSpace::High] {
|
2017-05-31 07:53:39 -05:00
|
|
|
out.extend(
|
|
|
|
(&self.def_path_hashes[address_space.index()])
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(index, &hash)| {
|
|
|
|
let def_id = DefId {
|
|
|
|
krate: cnum,
|
2018-01-02 05:36:12 -06:00
|
|
|
index: DefIndex::from_array_index(index, address_space),
|
2017-05-31 07:53:39 -05:00
|
|
|
};
|
|
|
|
(hash, def_id)
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn size(&self) -> usize {
|
2017-07-20 08:32:06 -05:00
|
|
|
self.index_to_key.iter().map(|v| v.len()).sum()
|
2017-05-31 07:53:39 -05:00
|
|
|
}
|
2016-12-14 10:27:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Encodable for DefPathTable {
|
|
|
|
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
2017-04-03 12:20:26 -05:00
|
|
|
// Index to key
|
2017-03-16 12:17:18 -05:00
|
|
|
self.index_to_key[DefIndexAddressSpace::Low.index()].encode(s)?;
|
2017-04-03 12:20:26 -05:00
|
|
|
self.index_to_key[DefIndexAddressSpace::High.index()].encode(s)?;
|
|
|
|
|
|
|
|
// DefPath hashes
|
|
|
|
self.def_path_hashes[DefIndexAddressSpace::Low.index()].encode(s)?;
|
|
|
|
self.def_path_hashes[DefIndexAddressSpace::High.index()].encode(s)?;
|
|
|
|
|
|
|
|
Ok(())
|
2016-12-14 10:27:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for DefPathTable {
|
|
|
|
fn decode<D: Decoder>(d: &mut D) -> Result<DefPathTable, D::Error> {
|
2017-03-16 12:17:18 -05:00
|
|
|
let index_to_key_lo: Vec<DefKey> = Decodable::decode(d)?;
|
2017-04-03 12:20:26 -05:00
|
|
|
let index_to_key_hi: Vec<DefKey> = Decodable::decode(d)?;
|
2017-03-16 12:17:18 -05:00
|
|
|
|
2017-05-31 06:54:38 -05:00
|
|
|
let def_path_hashes_lo: Vec<DefPathHash> = Decodable::decode(d)?;
|
|
|
|
let def_path_hashes_hi: Vec<DefPathHash> = Decodable::decode(d)?;
|
2017-04-03 12:20:26 -05:00
|
|
|
|
|
|
|
let index_to_key = [index_to_key_lo, index_to_key_hi];
|
|
|
|
let def_path_hashes = [def_path_hashes_lo, def_path_hashes_hi];
|
2017-03-16 12:17:18 -05:00
|
|
|
|
2016-12-14 10:27:39 -06:00
|
|
|
Ok(DefPathTable {
|
2017-07-03 13:19:51 -05:00
|
|
|
index_to_key,
|
|
|
|
def_path_hashes,
|
2016-12-14 10:27:39 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-12-13 16:48:52 -06:00
|
|
|
|
|
|
|
|
2016-12-16 18:12:37 -06:00
|
|
|
/// The definition table containing node definitions.
|
|
|
|
/// It holds the DefPathTable for local DefIds/DefPaths and it also stores a
|
|
|
|
/// mapping from NodeIds to local DefIds.
|
2015-09-17 13:29:59 -05:00
|
|
|
pub struct Definitions {
|
2016-12-14 10:27:39 -06:00
|
|
|
table: DefPathTable,
|
2016-12-13 16:48:52 -06:00
|
|
|
node_to_def_index: NodeMap<DefIndex>,
|
2017-03-16 12:17:18 -05:00
|
|
|
def_index_to_node: [Vec<ast::NodeId>; 2],
|
2017-03-14 09:50:40 -05:00
|
|
|
pub(super) node_to_hir_id: IndexVec<ast::NodeId, hir::HirId>,
|
2018-06-23 11:27:28 -05:00
|
|
|
/// If `Mark` is an ID of some macro expansion,
|
|
|
|
/// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
|
|
|
|
parent_modules_of_macro_defs: FxHashMap<Mark, DefId>,
|
|
|
|
/// Item with a given `DefIndex` was defined during opaque macro expansion with ID `Mark`.
|
|
|
|
/// It can actually be defined during transparent macro expansions inside that opaque expansion,
|
|
|
|
/// but transparent expansions are ignored here.
|
|
|
|
opaque_expansions_that_defined: FxHashMap<DefIndex, Mark>,
|
2017-08-01 06:44:20 -05:00
|
|
|
next_disambiguator: FxHashMap<(DefIndex, DefPathData), u32>,
|
2018-02-17 06:22:58 -06:00
|
|
|
def_index_to_span: FxHashMap<DefIndex, Span>,
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
|
2017-03-16 12:17:18 -05:00
|
|
|
// Unfortunately we have to provide a manual impl of Clone because of the
|
|
|
|
// fixed-sized array field.
|
|
|
|
impl Clone for Definitions {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Definitions {
|
|
|
|
table: self.table.clone(),
|
|
|
|
node_to_def_index: self.node_to_def_index.clone(),
|
|
|
|
def_index_to_node: [
|
|
|
|
self.def_index_to_node[0].clone(),
|
|
|
|
self.def_index_to_node[1].clone(),
|
|
|
|
],
|
|
|
|
node_to_hir_id: self.node_to_hir_id.clone(),
|
2018-06-23 11:27:28 -05:00
|
|
|
parent_modules_of_macro_defs: self.parent_modules_of_macro_defs.clone(),
|
|
|
|
opaque_expansions_that_defined: self.opaque_expansions_that_defined.clone(),
|
2017-08-01 06:44:20 -05:00
|
|
|
next_disambiguator: self.next_disambiguator.clone(),
|
2018-02-17 06:22:58 -06:00
|
|
|
def_index_to_span: self.def_index_to_span.clone(),
|
2017-03-16 12:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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`.
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
|
|
|
pub struct DefKey {
|
|
|
|
/// Parent path.
|
|
|
|
pub parent: Option<DefIndex>,
|
|
|
|
|
|
|
|
/// Identifier of this node.
|
|
|
|
pub disambiguated_data: DisambiguatedDefPathData,
|
|
|
|
}
|
|
|
|
|
2017-04-03 12:20:26 -05:00
|
|
|
impl DefKey {
|
2017-05-31 06:54:38 -05:00
|
|
|
fn compute_stable_hash(&self, parent_hash: DefPathHash) -> DefPathHash {
|
2017-04-03 12:20:26 -05:00
|
|
|
let mut hasher = StableHasher::new();
|
|
|
|
|
|
|
|
// We hash a 0u8 here to disambiguate between regular DefPath hashes,
|
|
|
|
// and the special "root_parent" below.
|
|
|
|
0u8.hash(&mut hasher);
|
|
|
|
parent_hash.hash(&mut hasher);
|
2017-06-13 06:47:13 -05:00
|
|
|
|
|
|
|
let DisambiguatedDefPathData {
|
|
|
|
ref data,
|
|
|
|
disambiguator,
|
|
|
|
} = self.disambiguated_data;
|
|
|
|
|
|
|
|
::std::mem::discriminant(data).hash(&mut hasher);
|
2018-05-22 07:31:56 -05:00
|
|
|
if let Some(name) = data.get_opt_name() {
|
|
|
|
name.hash(&mut hasher);
|
|
|
|
}
|
2017-06-13 06:47:13 -05:00
|
|
|
|
|
|
|
disambiguator.hash(&mut hasher);
|
|
|
|
|
2017-05-31 06:54:38 -05:00
|
|
|
DefPathHash(hasher.finish())
|
2017-04-03 12:20:26 -05:00
|
|
|
}
|
|
|
|
|
2017-10-24 10:49:58 -05:00
|
|
|
fn root_parent_stable_hash(crate_name: &str,
|
|
|
|
crate_disambiguator: CrateDisambiguator)
|
|
|
|
-> DefPathHash {
|
2017-04-03 12:20:26 -05:00
|
|
|
let mut hasher = StableHasher::new();
|
|
|
|
// Disambiguate this from a regular DefPath hash,
|
|
|
|
// see compute_stable_hash() above.
|
|
|
|
1u8.hash(&mut hasher);
|
|
|
|
crate_name.hash(&mut hasher);
|
|
|
|
crate_disambiguator.hash(&mut hasher);
|
2017-05-31 06:54:38 -05:00
|
|
|
DefPathHash(hasher.finish())
|
2017-04-03 12:20:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
/// Pair of `DefPathData` and an integer disambiguator. The integer is
|
|
|
|
/// normally 0, but in the event that there are multiple defs with the
|
|
|
|
/// same `parent` and `data`, we use this field to disambiguate
|
|
|
|
/// between them. This introduces some artificial ordering dependency
|
|
|
|
/// but means that if you have (e.g.) two impls for the same type in
|
|
|
|
/// the same module, they do get distinct def-ids.
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
|
|
|
pub struct DisambiguatedDefPathData {
|
|
|
|
pub data: DefPathData,
|
|
|
|
pub disambiguator: u32
|
|
|
|
}
|
|
|
|
|
2016-03-16 04:40:14 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
|
|
|
pub struct DefPath {
|
|
|
|
/// the path leading from the crate root to the item
|
|
|
|
pub data: Vec<DisambiguatedDefPathData>,
|
|
|
|
|
|
|
|
/// what krate root is this path relative to?
|
2016-08-31 06:00:29 -05:00
|
|
|
pub krate: CrateNum,
|
2016-03-16 04:40:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DefPath {
|
|
|
|
pub fn is_local(&self) -> bool {
|
|
|
|
self.krate == LOCAL_CRATE
|
|
|
|
}
|
|
|
|
|
2016-12-16 11:48:54 -06:00
|
|
|
pub fn make<FN>(krate: CrateNum,
|
2016-03-16 04:40:14 -05:00
|
|
|
start_index: DefIndex,
|
|
|
|
mut get_key: FN) -> DefPath
|
|
|
|
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();
|
|
|
|
DefPath { data: data, krate: krate }
|
|
|
|
}
|
2016-08-01 18:55:20 -05:00
|
|
|
|
2017-03-14 09:50:40 -05:00
|
|
|
/// Returns a string representation of the DefPath without
|
|
|
|
/// the crate-prefix. This method is useful if you don't have
|
|
|
|
/// a TyCtxt available.
|
|
|
|
pub fn to_string_no_crate(&self) -> String {
|
|
|
|
let mut s = String::with_capacity(self.data.len() * 16);
|
|
|
|
|
|
|
|
for component in &self.data {
|
|
|
|
write!(s,
|
|
|
|
"::{}[{}]",
|
|
|
|
component.data.as_interned_str(),
|
|
|
|
component.disambiguator)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
s
|
|
|
|
}
|
2017-11-04 06:27:08 -05:00
|
|
|
|
|
|
|
/// Return filename friendly string of the DefPah without
|
|
|
|
/// the crate-prefix. This method is useful if you don't have
|
|
|
|
/// a TyCtxt available.
|
|
|
|
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 {
|
2017-11-05 02:14:00 -06:00
|
|
|
opt_delimiter.map(|d| s.push(d));
|
|
|
|
opt_delimiter = Some('-');
|
2017-11-04 06:27:08 -05:00
|
|
|
if component.disambiguator == 0 {
|
2017-11-05 02:14:00 -06:00
|
|
|
write!(s, "{}", component.data.as_interned_str()).unwrap();
|
2017-11-04 06:27:08 -05:00
|
|
|
} else {
|
|
|
|
write!(s,
|
2017-11-05 02:14:00 -06:00
|
|
|
"{}[{}]",
|
2017-11-04 06:27:08 -05:00
|
|
|
component.data.as_interned_str(),
|
|
|
|
component.disambiguator)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
2016-03-16 04:40:14 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 06:47:13 -05:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Hash, RustcEncodable, RustcDecodable)]
|
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.
|
2015-12-21 15:24:15 -06:00
|
|
|
/// The crate root (marker)
|
2015-09-17 13:29:59 -05:00
|
|
|
CrateRoot,
|
2016-12-16 11:51:36 -06:00
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
// Catch-all for random DefId things like DUMMY_NODE_ID
|
|
|
|
Misc,
|
|
|
|
|
|
|
|
// Different kinds of items and item-like things:
|
2015-12-21 15:24:15 -06:00
|
|
|
/// An impl
|
2016-03-16 04:47:18 -05:00
|
|
|
Impl,
|
2018-03-29 23:51:39 -05:00
|
|
|
/// A trait
|
|
|
|
Trait(InternedString),
|
|
|
|
/// An associated type **declaration** (i.e., in a trait)
|
|
|
|
AssocTypeInTrait(InternedString),
|
|
|
|
/// An associated type **value** (i.e., in an impl)
|
|
|
|
AssocTypeInImpl(InternedString),
|
2015-12-21 15:24:15 -06:00
|
|
|
/// Something in the type NS
|
2017-09-01 11:24:02 -05:00
|
|
|
TypeNs(InternedString),
|
2015-12-21 15:24:15 -06:00
|
|
|
/// Something in the value NS
|
2017-09-01 11:24:02 -05:00
|
|
|
ValueNs(InternedString),
|
2015-12-21 15:24:15 -06:00
|
|
|
/// A module declaration
|
2017-09-01 11:24:02 -05:00
|
|
|
Module(InternedString),
|
2015-12-21 15:24:15 -06:00
|
|
|
/// A macro rule
|
2017-09-01 11:24:02 -05:00
|
|
|
MacroDef(InternedString),
|
2015-12-21 15:24:15 -06:00
|
|
|
/// A closure expression
|
2015-09-17 13:29:59 -05:00
|
|
|
ClosureExpr,
|
|
|
|
|
|
|
|
// Subportions of items
|
2015-12-21 15:24:15 -06:00
|
|
|
/// A type parameter (generic parameter)
|
2017-09-01 11:24:02 -05:00
|
|
|
TypeParam(InternedString),
|
2015-12-21 15:24:15 -06:00
|
|
|
/// A lifetime definition
|
2018-05-26 17:56:34 -05:00
|
|
|
LifetimeParam(InternedString),
|
2015-12-21 15:24:15 -06:00
|
|
|
/// A variant of a enum
|
2017-09-01 11:24:02 -05:00
|
|
|
EnumVariant(InternedString),
|
2015-12-21 15:24:15 -06:00
|
|
|
/// A struct field
|
2017-09-01 11:24:02 -05:00
|
|
|
Field(InternedString),
|
2015-12-21 15:24:15 -06:00
|
|
|
/// Implicit ctor for a tuple-like struct
|
|
|
|
StructCtor,
|
2018-05-17 13:28:50 -05:00
|
|
|
/// A constant expression (see {ast,hir}::AnonConst).
|
|
|
|
AnonConst,
|
2018-06-20 03:59:24 -05:00
|
|
|
/// An `impl Trait` type node
|
|
|
|
ImplTrait,
|
2017-06-07 03:45:20 -05:00
|
|
|
|
|
|
|
/// GlobalMetaData identifies a piece of crate metadata that is global to
|
|
|
|
/// a whole crate (as opposed to just one item). GlobalMetaData components
|
|
|
|
/// are only supposed to show up right below the crate root.
|
2017-09-01 11:24:02 -05:00
|
|
|
GlobalMetaData(InternedString)
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
|
2017-05-31 06:54:38 -05:00
|
|
|
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
|
|
|
|
RustcEncodable, RustcDecodable)]
|
|
|
|
pub struct DefPathHash(pub Fingerprint);
|
|
|
|
|
|
|
|
impl_stable_hash_for!(tuple_struct DefPathHash { fingerprint });
|
|
|
|
|
2018-07-03 04:16:38 -05:00
|
|
|
impl Borrow<Fingerprint> for DefPathHash {
|
|
|
|
#[inline]
|
|
|
|
fn borrow(&self) -> &Fingerprint {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
impl Definitions {
|
2015-12-21 15:24:15 -06:00
|
|
|
/// Create new empty definition map.
|
2015-09-17 13:29:59 -05:00
|
|
|
pub fn new() -> Definitions {
|
|
|
|
Definitions {
|
2016-12-14 10:27:39 -06:00
|
|
|
table: DefPathTable {
|
2017-03-16 12:17:18 -05:00
|
|
|
index_to_key: [vec![], vec![]],
|
2017-04-03 12:20:26 -05:00
|
|
|
def_path_hashes: [vec![], vec![]],
|
2016-12-14 10:27:39 -06:00
|
|
|
},
|
2016-12-13 16:48:52 -06:00
|
|
|
node_to_def_index: NodeMap(),
|
2017-03-16 12:17:18 -05:00
|
|
|
def_index_to_node: [vec![], vec![]],
|
2017-03-14 09:50:40 -05:00
|
|
|
node_to_hir_id: IndexVec::new(),
|
2018-06-23 11:27:28 -05:00
|
|
|
parent_modules_of_macro_defs: FxHashMap(),
|
|
|
|
opaque_expansions_that_defined: FxHashMap(),
|
2017-08-01 06:44:20 -05:00
|
|
|
next_disambiguator: FxHashMap(),
|
2018-02-17 06:22:58 -06:00
|
|
|
def_index_to_span: FxHashMap(),
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 11:51:36 -06:00
|
|
|
pub fn def_path_table(&self) -> &DefPathTable {
|
|
|
|
&self.table
|
|
|
|
}
|
|
|
|
|
2015-12-21 15:24:15 -06:00
|
|
|
/// Get the number of definitions.
|
2017-03-16 12:17:18 -05:00
|
|
|
pub fn def_index_counts_lo_hi(&self) -> (usize, usize) {
|
2017-06-07 03:45:20 -05:00
|
|
|
(self.table.index_to_key[DefIndexAddressSpace::Low.index()].len(),
|
|
|
|
self.table.index_to_key[DefIndexAddressSpace::High.index()].len())
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn def_key(&self, index: DefIndex) -> DefKey {
|
2016-12-14 10:27:39 -06:00
|
|
|
self.table.def_key(index)
|
2015-09-17 13:29:59 -05: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 {
|
2017-04-03 12:20:26 -05:00
|
|
|
self.table.def_path_hash(index)
|
|
|
|
}
|
|
|
|
|
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).
|
|
|
|
pub fn def_path(&self, index: DefIndex) -> DefPath {
|
2016-03-16 04:40:14 -05:00
|
|
|
DefPath::make(LOCAL_CRATE, index, |p| self.def_key(p))
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
|
2017-08-08 07:33:51 -05:00
|
|
|
#[inline]
|
2015-09-17 13:29:59 -05:00
|
|
|
pub fn opt_def_index(&self, node: ast::NodeId) -> Option<DefIndex> {
|
2016-12-13 16:48:52 -06:00
|
|
|
self.node_to_def_index.get(&node).cloned()
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
|
2017-08-08 07:33:51 -05:00
|
|
|
#[inline]
|
2015-09-17 13:29:59 -05:00
|
|
|
pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
|
|
|
|
self.opt_def_index(node).map(DefId::local)
|
|
|
|
}
|
|
|
|
|
2017-08-08 07:33:51 -05:00
|
|
|
#[inline]
|
2016-04-23 22:26:10 -05:00
|
|
|
pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
|
|
|
|
self.opt_local_def_id(node).unwrap()
|
|
|
|
}
|
|
|
|
|
2017-08-08 07:33:51 -05:00
|
|
|
#[inline]
|
2015-09-17 13:29:59 -05:00
|
|
|
pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
|
|
|
|
if def_id.krate == LOCAL_CRATE {
|
2017-03-16 12:17:18 -05:00
|
|
|
let space_index = def_id.index.address_space().index();
|
|
|
|
let array_index = def_id.index.as_array_index();
|
2017-06-07 03:45:20 -05:00
|
|
|
let node_id = self.def_index_to_node[space_index][array_index];
|
|
|
|
if node_id != ast::DUMMY_NODE_ID {
|
|
|
|
Some(node_id)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2015-09-17 13:29:59 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 07:33:51 -05:00
|
|
|
#[inline]
|
2017-03-30 08:27:27 -05:00
|
|
|
pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
|
|
|
|
self.node_to_hir_id[node_id]
|
|
|
|
}
|
|
|
|
|
2017-08-08 07:33:51 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> hir::HirId {
|
|
|
|
let space_index = def_index.address_space().index();
|
|
|
|
let array_index = def_index.as_array_index();
|
|
|
|
let node_id = self.def_index_to_node[space_index][array_index];
|
|
|
|
self.node_to_hir_id[node_id]
|
|
|
|
}
|
|
|
|
|
2018-02-17 06:22:58 -06:00
|
|
|
/// Retrieve the span of the given `DefId` if `DefId` is in the local crate, the span exists and
|
|
|
|
/// it's not DUMMY_SP
|
|
|
|
#[inline]
|
|
|
|
pub fn opt_span(&self, def_id: DefId) -> Option<Span> {
|
|
|
|
if def_id.krate == LOCAL_CRATE {
|
2018-06-24 17:00:21 -05:00
|
|
|
self.def_index_to_span.get(&def_id.index).cloned()
|
2018-02-17 06:22:58 -06:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 12:20:26 -05:00
|
|
|
/// Add a definition with a parent definition.
|
|
|
|
pub fn create_root_def(&mut self,
|
|
|
|
crate_name: &str,
|
2017-10-24 10:49:58 -05:00
|
|
|
crate_disambiguator: CrateDisambiguator)
|
2017-04-03 12:20:26 -05:00
|
|
|
-> DefIndex {
|
|
|
|
let key = DefKey {
|
|
|
|
parent: None,
|
|
|
|
disambiguated_data: DisambiguatedDefPathData {
|
|
|
|
data: DefPathData::CrateRoot,
|
|
|
|
disambiguator: 0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let parent_hash = DefKey::root_parent_stable_hash(crate_name,
|
|
|
|
crate_disambiguator);
|
|
|
|
let def_path_hash = key.compute_stable_hash(parent_hash);
|
|
|
|
|
|
|
|
// Create the definition.
|
|
|
|
let address_space = super::ITEM_LIKE_SPACE;
|
2017-06-07 03:45:20 -05:00
|
|
|
let root_index = self.table.allocate(key, def_path_hash, address_space);
|
|
|
|
assert_eq!(root_index, CRATE_DEF_INDEX);
|
2017-04-03 12:20:26 -05:00
|
|
|
assert!(self.def_index_to_node[address_space.index()].is_empty());
|
|
|
|
self.def_index_to_node[address_space.index()].push(ast::CRATE_NODE_ID);
|
2017-06-07 03:45:20 -05:00
|
|
|
self.node_to_def_index.insert(ast::CRATE_NODE_ID, root_index);
|
2017-04-03 12:20:26 -05:00
|
|
|
|
2017-06-07 03:45:20 -05:00
|
|
|
// Allocate some other DefIndices that always must exist.
|
|
|
|
GlobalMetaDataKind::allocate_def_indices(self);
|
|
|
|
|
|
|
|
root_index
|
2017-04-03 12:20:26 -05:00
|
|
|
}
|
|
|
|
|
2015-12-21 15:24:15 -06:00
|
|
|
/// Add a definition with a parent definition.
|
2015-09-17 13:29:59 -05:00
|
|
|
pub fn create_def_with_parent(&mut self,
|
2017-04-03 12:20:26 -05:00
|
|
|
parent: DefIndex,
|
2015-09-17 13:29:59 -05:00
|
|
|
node_id: ast::NodeId,
|
2017-03-16 12:17:18 -05:00
|
|
|
data: DefPathData,
|
2017-03-24 18:03:15 -05:00
|
|
|
address_space: DefIndexAddressSpace,
|
2018-02-17 06:22:58 -06:00
|
|
|
expansion: Mark,
|
|
|
|
span: Span)
|
2015-09-17 13:29:59 -05:00
|
|
|
-> DefIndex {
|
2016-03-28 16:39:57 -05:00
|
|
|
debug!("create_def_with_parent(parent={:?}, node_id={:?}, data={:?})",
|
|
|
|
parent, node_id, data);
|
|
|
|
|
2016-12-13 16:48:52 -06:00
|
|
|
assert!(!self.node_to_def_index.contains_key(&node_id),
|
2015-09-17 13:29:59 -05:00
|
|
|
"adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
|
|
|
|
node_id,
|
|
|
|
data,
|
2016-12-14 10:27:39 -06:00
|
|
|
self.table.def_key(self.node_to_def_index[&node_id]));
|
2015-09-17 13:29:59 -05:00
|
|
|
|
2017-04-03 12:20:26 -05:00
|
|
|
// The root node must be created with create_root_def()
|
|
|
|
assert!(data != DefPathData::CrateRoot);
|
2016-03-28 16:39:57 -05:00
|
|
|
|
2017-08-01 06:44:20 -05:00
|
|
|
// Find the next free disambiguator for this key.
|
|
|
|
let disambiguator = {
|
|
|
|
let next_disamb = self.next_disambiguator.entry((parent, data.clone())).or_insert(0);
|
|
|
|
let disambiguator = *next_disamb;
|
|
|
|
*next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow");
|
|
|
|
disambiguator
|
|
|
|
};
|
|
|
|
|
|
|
|
let key = DefKey {
|
2017-04-03 12:20:26 -05:00
|
|
|
parent: Some(parent),
|
2015-09-17 13:29:59 -05:00
|
|
|
disambiguated_data: DisambiguatedDefPathData {
|
2017-08-01 06:44:20 -05:00
|
|
|
data, disambiguator
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-03 12:20:26 -05:00
|
|
|
let parent_hash = self.table.def_path_hash(parent);
|
|
|
|
let def_path_hash = key.compute_stable_hash(parent_hash);
|
|
|
|
|
2016-03-28 16:39:57 -05:00
|
|
|
debug!("create_def_with_parent: after disambiguation, key = {:?}", key);
|
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
// Create the definition.
|
2017-04-03 12:20:26 -05:00
|
|
|
let index = self.table.allocate(key, def_path_hash, address_space);
|
2017-03-16 12:17:18 -05:00
|
|
|
assert_eq!(index.as_array_index(),
|
|
|
|
self.def_index_to_node[address_space.index()].len());
|
|
|
|
self.def_index_to_node[address_space.index()].push(node_id);
|
2017-06-07 03:45:20 -05:00
|
|
|
|
|
|
|
// Some things for which we allocate DefIndices don't correspond to
|
|
|
|
// anything in the AST, so they don't have a NodeId. For these cases
|
|
|
|
// we don't need a mapping from NodeId to DefIndex.
|
|
|
|
if node_id != ast::DUMMY_NODE_ID {
|
|
|
|
debug!("create_def_with_parent: def_index_to_node[{:?} <-> {:?}", index, node_id);
|
|
|
|
self.node_to_def_index.insert(node_id, index);
|
|
|
|
}
|
|
|
|
|
2017-11-28 01:07:44 -06:00
|
|
|
let expansion = expansion.modern();
|
|
|
|
if expansion != Mark::root() {
|
2018-06-23 11:27:28 -05:00
|
|
|
self.opaque_expansions_that_defined.insert(index, expansion);
|
2017-03-25 21:11:30 -05:00
|
|
|
}
|
2017-03-16 12:17:18 -05:00
|
|
|
|
2018-06-24 17:00:21 -05:00
|
|
|
// The span is added if it isn't dummy
|
|
|
|
if !span.is_dummy() {
|
2018-02-17 06:22:58 -06:00
|
|
|
self.def_index_to_span.insert(index, span);
|
|
|
|
}
|
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
index
|
|
|
|
}
|
2017-03-14 09:50:40 -05:00
|
|
|
|
|
|
|
/// Initialize the ast::NodeId to HirId mapping once it has been generated during
|
|
|
|
/// AST to HIR lowering.
|
|
|
|
pub fn init_node_id_to_hir_id_mapping(&mut self,
|
|
|
|
mapping: IndexVec<ast::NodeId, hir::HirId>) {
|
|
|
|
assert!(self.node_to_hir_id.is_empty(),
|
|
|
|
"Trying initialize NodeId -> HirId mapping twice");
|
|
|
|
self.node_to_hir_id = mapping;
|
|
|
|
}
|
2017-03-24 18:03:15 -05:00
|
|
|
|
2018-06-23 11:27:28 -05:00
|
|
|
pub fn opaque_expansion_that_defined(&self, index: DefIndex) -> Mark {
|
|
|
|
self.opaque_expansions_that_defined.get(&index).cloned().unwrap_or(Mark::root())
|
2017-03-24 18:03:15 -05:00
|
|
|
}
|
|
|
|
|
2018-06-23 11:27:28 -05:00
|
|
|
pub fn parent_module_of_macro_def(&self, mark: Mark) -> DefId {
|
|
|
|
self.parent_modules_of_macro_defs[&mark]
|
2017-03-24 18:03:15 -05:00
|
|
|
}
|
|
|
|
|
2018-06-23 11:27:28 -05:00
|
|
|
pub fn add_parent_module_of_macro_def(&mut self, mark: Mark, module: DefId) {
|
|
|
|
self.parent_modules_of_macro_defs.insert(mark, module);
|
2017-03-24 18:03:15 -05:00
|
|
|
}
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DefPathData {
|
2017-09-01 11:24:02 -05:00
|
|
|
pub fn get_opt_name(&self) -> Option<InternedString> {
|
2016-09-17 05:34:55 -05:00
|
|
|
use self::DefPathData::*;
|
|
|
|
match *self {
|
2017-06-13 06:47:13 -05:00
|
|
|
TypeNs(name) |
|
2018-03-29 23:51:39 -05:00
|
|
|
Trait(name) |
|
|
|
|
AssocTypeInTrait(name) |
|
|
|
|
AssocTypeInImpl(name) |
|
2017-06-13 06:47:13 -05:00
|
|
|
ValueNs(name) |
|
|
|
|
Module(name) |
|
|
|
|
MacroDef(name) |
|
|
|
|
TypeParam(name) |
|
2018-05-26 17:56:34 -05:00
|
|
|
LifetimeParam(name) |
|
2017-06-13 06:47:13 -05:00
|
|
|
EnumVariant(name) |
|
|
|
|
Field(name) |
|
|
|
|
GlobalMetaData(name) => Some(name),
|
2016-09-17 05:34:55 -05:00
|
|
|
|
|
|
|
Impl |
|
|
|
|
CrateRoot |
|
|
|
|
Misc |
|
|
|
|
ClosureExpr |
|
|
|
|
StructCtor |
|
2018-05-17 13:28:50 -05:00
|
|
|
AnonConst |
|
2018-06-20 03:59:24 -05:00
|
|
|
ImplTrait => None
|
2016-09-17 05:34:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
pub fn as_interned_str(&self) -> InternedString {
|
|
|
|
use self::DefPathData::*;
|
2016-11-17 08:04:20 -06:00
|
|
|
let s = match *self {
|
2017-06-13 06:47:13 -05:00
|
|
|
TypeNs(name) |
|
2018-03-29 23:51:39 -05:00
|
|
|
Trait(name) |
|
|
|
|
AssocTypeInTrait(name) |
|
|
|
|
AssocTypeInImpl(name) |
|
2017-06-13 06:47:13 -05:00
|
|
|
ValueNs(name) |
|
|
|
|
Module(name) |
|
|
|
|
MacroDef(name) |
|
|
|
|
TypeParam(name) |
|
2018-05-26 17:56:34 -05:00
|
|
|
LifetimeParam(name) |
|
2017-06-13 06:47:13 -05:00
|
|
|
EnumVariant(name) |
|
|
|
|
Field(name) |
|
|
|
|
GlobalMetaData(name) => {
|
2017-09-01 11:24:02 -05:00
|
|
|
return name
|
2016-03-16 04:47:18 -05:00
|
|
|
}
|
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
// note that this does not show up in user printouts
|
2016-11-17 08:04:20 -06:00
|
|
|
CrateRoot => "{{root}}",
|
2015-09-17 13:29:59 -05:00
|
|
|
|
2016-11-17 08:04:20 -06:00
|
|
|
Impl => "{{impl}}",
|
|
|
|
Misc => "{{?}}",
|
|
|
|
ClosureExpr => "{{closure}}",
|
|
|
|
StructCtor => "{{constructor}}",
|
2018-05-17 13:28:50 -05:00
|
|
|
AnonConst => "{{constant}}",
|
2018-06-20 03:59:24 -05:00
|
|
|
ImplTrait => "{{impl-Trait}}",
|
2016-11-17 08:04:20 -06:00
|
|
|
};
|
2016-07-22 10:56:22 -05:00
|
|
|
|
2018-04-11 16:02:41 -05:00
|
|
|
Symbol::intern(s).as_interned_str()
|
2015-09-17 13:29:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_string(&self) -> String {
|
|
|
|
self.as_interned_str().to_string()
|
|
|
|
}
|
|
|
|
}
|
2017-03-26 19:46:00 -05:00
|
|
|
|
2017-06-07 03:45:20 -05:00
|
|
|
// We define the GlobalMetaDataKind enum with this macro because we want to
|
|
|
|
// make sure that we exhaustively iterate over all variants when registering
|
|
|
|
// the corresponding DefIndices in the DefTable.
|
|
|
|
macro_rules! define_global_metadata_kind {
|
|
|
|
(pub enum GlobalMetaDataKind {
|
|
|
|
$($variant:ident),*
|
|
|
|
}) => (
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash,
|
|
|
|
RustcEncodable, RustcDecodable)]
|
|
|
|
pub enum GlobalMetaDataKind {
|
|
|
|
$($variant),*
|
|
|
|
}
|
|
|
|
|
2017-07-20 08:32:06 -05:00
|
|
|
const GLOBAL_MD_ADDRESS_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::High;
|
|
|
|
|
2017-06-07 03:45:20 -05:00
|
|
|
impl GlobalMetaDataKind {
|
|
|
|
fn allocate_def_indices(definitions: &mut Definitions) {
|
|
|
|
$({
|
|
|
|
let instance = GlobalMetaDataKind::$variant;
|
|
|
|
definitions.create_def_with_parent(
|
|
|
|
CRATE_DEF_INDEX,
|
|
|
|
ast::DUMMY_NODE_ID,
|
2018-04-11 16:02:41 -05:00
|
|
|
DefPathData::GlobalMetaData(instance.name().as_interned_str()),
|
2017-07-20 08:32:06 -05:00
|
|
|
GLOBAL_MD_ADDRESS_SPACE,
|
2018-02-17 06:22:58 -06:00
|
|
|
Mark::root(),
|
|
|
|
DUMMY_SP
|
2017-06-07 03:45:20 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Make sure calling def_index does not crash.
|
|
|
|
instance.def_index(&definitions.table);
|
|
|
|
})*
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn def_index(&self, def_path_table: &DefPathTable) -> DefIndex {
|
|
|
|
let def_key = DefKey {
|
|
|
|
parent: Some(CRATE_DEF_INDEX),
|
|
|
|
disambiguated_data: DisambiguatedDefPathData {
|
2018-04-11 16:02:41 -05:00
|
|
|
data: DefPathData::GlobalMetaData(self.name().as_interned_str()),
|
2017-06-07 03:45:20 -05:00
|
|
|
disambiguator: 0,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-20 08:32:06 -05:00
|
|
|
// These DefKeys are all right after the root,
|
|
|
|
// so a linear search is fine.
|
|
|
|
let index = def_path_table.index_to_key[GLOBAL_MD_ADDRESS_SPACE.index()]
|
|
|
|
.iter()
|
|
|
|
.position(|k| *k == def_key)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
DefIndex::from_array_index(index, GLOBAL_MD_ADDRESS_SPACE)
|
2017-06-07 03:45:20 -05:00
|
|
|
}
|
|
|
|
|
2017-06-13 06:47:13 -05:00
|
|
|
fn name(&self) -> Symbol {
|
2017-06-07 03:45:20 -05:00
|
|
|
|
|
|
|
let string = match *self {
|
|
|
|
$(
|
|
|
|
GlobalMetaDataKind::$variant => {
|
|
|
|
concat!("{{GlobalMetaData::", stringify!($variant), "}}")
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
};
|
|
|
|
|
2017-06-13 06:47:13 -05:00
|
|
|
Symbol::intern(string)
|
2017-06-07 03:45:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
define_global_metadata_kind!(pub enum GlobalMetaDataKind {
|
|
|
|
Krate,
|
|
|
|
CrateDeps,
|
|
|
|
DylibDependencyFormats,
|
|
|
|
LangItems,
|
|
|
|
LangItemsMissing,
|
|
|
|
NativeLibraries,
|
|
|
|
CodeMap,
|
|
|
|
Impls,
|
|
|
|
ExportedSymbols
|
|
|
|
});
|