1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! For each definition, we track the following data. A definition
12 //! here is defined somewhat circularly as "something with a def-id",
13 //! but it generally corresponds to things like structs, enums, etc.
14 //! There are also some rather random cases (like const initializer
15 //! expressions) that are mostly just leftovers.
18 use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE, DefIndexAddressSpace,
21 use rustc_data_structures::fx::FxHashMap;
22 use rustc_data_structures::indexed_vec::{IndexVec};
23 use rustc_data_structures::stable_hasher::StableHasher;
24 use serialize::{Encodable, Decodable, Encoder, Decoder};
25 use session::CrateDisambiguator;
26 use std::borrow::Borrow;
30 use syntax::ext::hygiene::Mark;
31 use syntax::symbol::{Symbol, InternedString};
32 use syntax_pos::{Span, DUMMY_SP};
33 use util::nodemap::NodeMap;
35 /// The DefPathTable maps DefIndexes to DefKeys and vice versa.
36 /// Internally the DefPathTable holds a tree of DefKeys, where each DefKey
37 /// stores the DefIndex of its parent.
38 /// There is one DefPathTable for each crate.
40 pub struct DefPathTable {
41 index_to_key: [Vec<DefKey>; 2],
42 def_path_hashes: [Vec<DefPathHash>; 2],
45 // Unfortunately we have to provide a manual impl of Clone because of the
46 // fixed-sized array field.
47 impl Clone for DefPathTable {
48 fn clone(&self) -> Self {
50 index_to_key: [self.index_to_key[0].clone(),
51 self.index_to_key[1].clone()],
52 def_path_hashes: [self.def_path_hashes[0].clone(),
53 self.def_path_hashes[1].clone()],
60 fn allocate(&mut self,
62 def_path_hash: DefPathHash,
63 address_space: DefIndexAddressSpace)
66 let index_to_key = &mut self.index_to_key[address_space.index()];
67 let index = DefIndex::from_array_index(index_to_key.len(), address_space);
68 debug!("DefPathTable::insert() - {:?} <-> {:?}", key, index);
69 index_to_key.push(key);
72 self.def_path_hashes[address_space.index()].push(def_path_hash);
73 debug_assert!(self.def_path_hashes[address_space.index()].len() ==
74 self.index_to_key[address_space.index()].len());
78 pub fn next_id(&self, address_space: DefIndexAddressSpace) -> DefIndex {
79 DefIndex::from_array_index(self.index_to_key[address_space.index()].len(), address_space)
83 pub fn def_key(&self, index: DefIndex) -> DefKey {
84 self.index_to_key[index.address_space().index()]
85 [index.as_array_index()].clone()
89 pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
90 let ret = self.def_path_hashes[index.address_space().index()]
91 [index.as_array_index()];
92 debug!("def_path_hash({:?}) = {:?}", index, ret);
96 pub fn add_def_path_hashes_to(&self,
98 out: &mut FxHashMap<DefPathHash, DefId>) {
99 for &address_space in &[DefIndexAddressSpace::Low, DefIndexAddressSpace::High] {
101 (&self.def_path_hashes[address_space.index()])
104 .map(|(index, &hash)| {
107 index: DefIndex::from_array_index(index, address_space),
115 pub fn size(&self) -> usize {
116 self.index_to_key.iter().map(|v| v.len()).sum()
121 impl Encodable for DefPathTable {
122 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
124 self.index_to_key[DefIndexAddressSpace::Low.index()].encode(s)?;
125 self.index_to_key[DefIndexAddressSpace::High.index()].encode(s)?;
128 self.def_path_hashes[DefIndexAddressSpace::Low.index()].encode(s)?;
129 self.def_path_hashes[DefIndexAddressSpace::High.index()].encode(s)?;
135 impl Decodable for DefPathTable {
136 fn decode<D: Decoder>(d: &mut D) -> Result<DefPathTable, D::Error> {
137 let index_to_key_lo: Vec<DefKey> = Decodable::decode(d)?;
138 let index_to_key_hi: Vec<DefKey> = Decodable::decode(d)?;
140 let def_path_hashes_lo: Vec<DefPathHash> = Decodable::decode(d)?;
141 let def_path_hashes_hi: Vec<DefPathHash> = Decodable::decode(d)?;
143 let index_to_key = [index_to_key_lo, index_to_key_hi];
144 let def_path_hashes = [def_path_hashes_lo, def_path_hashes_hi];
153 /// The definition table containing node definitions.
154 /// It holds the `DefPathTable` for local `DefId`s/`DefPath`s and it also stores a
155 /// mapping from `NodeId`s to local `DefId`s.
156 #[derive(Clone, Default)]
157 pub struct Definitions {
159 node_to_def_index: NodeMap<DefIndex>,
160 def_index_to_node: [Vec<ast::NodeId>; 2],
161 pub(super) node_to_hir_id: IndexVec<ast::NodeId, hir::HirId>,
162 /// If `Mark` is an ID of some macro expansion,
163 /// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
164 parent_modules_of_macro_defs: FxHashMap<Mark, DefId>,
165 /// Item with a given `DefIndex` was defined during macro expansion with ID `Mark`.
166 expansions_that_defined: FxHashMap<DefIndex, Mark>,
167 next_disambiguator: FxHashMap<(DefIndex, DefPathData), u32>,
168 def_index_to_span: FxHashMap<DefIndex, Span>,
171 /// A unique identifier that we can use to lookup a definition
172 /// precisely. It combines the index of the definition's parent (if
173 /// any) with a `DisambiguatedDefPathData`.
174 #[derive(Clone, PartialEq, Debug, Hash, RustcEncodable, RustcDecodable)]
177 pub parent: Option<DefIndex>,
179 /// Identifier of this node.
180 pub disambiguated_data: DisambiguatedDefPathData,
184 fn compute_stable_hash(&self, parent_hash: DefPathHash) -> DefPathHash {
185 let mut hasher = StableHasher::new();
187 // We hash a 0u8 here to disambiguate between regular DefPath hashes,
188 // and the special "root_parent" below.
189 0u8.hash(&mut hasher);
190 parent_hash.hash(&mut hasher);
192 let DisambiguatedDefPathData {
195 } = self.disambiguated_data;
197 ::std::mem::discriminant(data).hash(&mut hasher);
198 if let Some(name) = data.get_opt_name() {
199 name.hash(&mut hasher);
202 disambiguator.hash(&mut hasher);
204 DefPathHash(hasher.finish())
207 fn root_parent_stable_hash(crate_name: &str,
208 crate_disambiguator: CrateDisambiguator)
210 let mut hasher = StableHasher::new();
211 // Disambiguate this from a regular DefPath hash,
212 // see compute_stable_hash() above.
213 1u8.hash(&mut hasher);
214 crate_name.hash(&mut hasher);
215 crate_disambiguator.hash(&mut hasher);
216 DefPathHash(hasher.finish())
220 /// Pair of `DefPathData` and an integer disambiguator. The integer is
221 /// normally 0, but in the event that there are multiple defs with the
222 /// same `parent` and `data`, we use this field to disambiguate
223 /// between them. This introduces some artificial ordering dependency
224 /// but means that if you have (e.g.) two impls for the same type in
225 /// the same module, they do get distinct def-ids.
226 #[derive(Clone, PartialEq, Debug, Hash, RustcEncodable, RustcDecodable)]
227 pub struct DisambiguatedDefPathData {
228 pub data: DefPathData,
229 pub disambiguator: u32
232 #[derive(Clone, Debug, Hash, RustcEncodable, RustcDecodable)]
234 /// the path leading from the crate root to the item
235 pub data: Vec<DisambiguatedDefPathData>,
237 /// what krate root is this path relative to?
242 pub fn is_local(&self) -> bool {
243 self.krate == LOCAL_CRATE
246 pub fn make<FN>(krate: CrateNum,
247 start_index: DefIndex,
248 mut get_key: FN) -> DefPath
249 where FN: FnMut(DefIndex) -> DefKey
251 let mut data = vec![];
252 let mut index = Some(start_index);
254 debug!("DefPath::make: krate={:?} index={:?}", krate, index);
255 let p = index.unwrap();
256 let key = get_key(p);
257 debug!("DefPath::make: key={:?}", key);
258 match key.disambiguated_data.data {
259 DefPathData::CrateRoot => {
260 assert!(key.parent.is_none());
264 data.push(key.disambiguated_data);
270 DefPath { data: data, krate: krate }
273 /// Returns a string representation of the DefPath without
274 /// the crate-prefix. This method is useful if you don't have
275 /// a TyCtxt available.
276 pub fn to_string_no_crate(&self) -> String {
277 let mut s = String::with_capacity(self.data.len() * 16);
279 for component in &self.data {
282 component.data.as_interned_str(),
283 component.disambiguator)
290 /// Return filename friendly string of the DefPah with the
292 pub fn to_string_friendly<F>(&self, crate_imported_name: F) -> String
293 where F: FnOnce(CrateNum) -> Symbol
295 let crate_name_str = crate_imported_name(self.krate).as_str();
296 let mut s = String::with_capacity(crate_name_str.len() + self.data.len() * 16);
298 write!(s, "::{}", crate_name_str).unwrap();
300 for component in &self.data {
301 if component.disambiguator == 0 {
302 write!(s, "::{}", component.data.as_interned_str()).unwrap();
306 component.data.as_interned_str(),
307 component.disambiguator)
315 /// Return filename friendly string of the DefPah without
316 /// the crate-prefix. This method is useful if you don't have
317 /// a TyCtxt available.
318 pub fn to_filename_friendly_no_crate(&self) -> String {
319 let mut s = String::with_capacity(self.data.len() * 16);
321 let mut opt_delimiter = None;
322 for component in &self.data {
323 opt_delimiter.map(|d| s.push(d));
324 opt_delimiter = Some('-');
325 if component.disambiguator == 0 {
326 write!(s, "{}", component.data.as_interned_str()).unwrap();
330 component.data.as_interned_str(),
331 component.disambiguator)
339 #[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
340 pub enum DefPathData {
341 // Root: these should only be used for the root nodes, because
342 // they are treated specially by the `def_path` function.
343 /// The crate root (marker)
345 // Catch-all for random DefId things like DUMMY_NODE_ID
347 // Different kinds of items and item-like things:
351 Trait(InternedString),
352 /// An associated type **declaration** (i.e., in a trait)
353 AssocTypeInTrait(InternedString),
354 /// An associated type **value** (i.e., in an impl)
355 AssocTypeInImpl(InternedString),
356 /// An existential associated type **value** (i.e., in an impl)
357 AssocExistentialInImpl(InternedString),
358 /// Something in the type NS
359 TypeNs(InternedString),
360 /// Something in the value NS
361 ValueNs(InternedString),
362 /// A module declaration
363 Module(InternedString),
365 MacroDef(InternedString),
366 /// A closure expression
368 // Subportions of items
369 /// A type parameter (generic parameter)
370 TypeParam(InternedString),
371 /// A lifetime definition
372 LifetimeParam(InternedString),
373 /// A variant of a enum
374 EnumVariant(InternedString),
376 Field(InternedString),
377 /// Implicit ctor for a tuple-like struct
379 /// A constant expression (see {ast,hir}::AnonConst).
381 /// An `impl Trait` type node
383 /// GlobalMetaData identifies a piece of crate metadata that is global to
384 /// a whole crate (as opposed to just one item). GlobalMetaData components
385 /// are only supposed to show up right below the crate root.
386 GlobalMetaData(InternedString)
389 #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
390 RustcEncodable, RustcDecodable)]
391 pub struct DefPathHash(pub Fingerprint);
393 impl_stable_hash_for!(tuple_struct DefPathHash { fingerprint });
395 impl Borrow<Fingerprint> for DefPathHash {
397 fn borrow(&self) -> &Fingerprint {
403 /// Create new empty definition map.
405 /// The DefIndex returned from a new Definitions are as follows:
406 /// 1. At DefIndexAddressSpace::Low,
407 /// CRATE_ROOT has index 0:0, and then new indexes are allocated in
409 /// 2. At DefIndexAddressSpace::High,
410 /// the first FIRST_FREE_HIGH_DEF_INDEX indexes are reserved for
411 /// internal use, then 1:FIRST_FREE_HIGH_DEF_INDEX are allocated in
414 /// FIXME: there is probably a better place to put this comment.
415 pub fn new() -> Self {
419 pub fn def_path_table(&self) -> &DefPathTable {
423 /// Get the number of definitions.
424 pub fn def_index_counts_lo_hi(&self) -> (usize, usize) {
425 (self.table.index_to_key[DefIndexAddressSpace::Low.index()].len(),
426 self.table.index_to_key[DefIndexAddressSpace::High.index()].len())
429 pub fn def_key(&self, index: DefIndex) -> DefKey {
430 self.table.def_key(index)
434 pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
435 self.table.def_path_hash(index)
438 /// Returns the path from the crate root to `index`. The root
439 /// nodes are not included in the path (i.e., this will be an
440 /// empty vector for the crate root). For an inlined item, this
441 /// will be the path of the item in the external crate (but the
442 /// path will begin with the path to the external crate).
443 pub fn def_path(&self, index: DefIndex) -> DefPath {
444 DefPath::make(LOCAL_CRATE, index, |p| self.def_key(p))
448 pub fn opt_def_index(&self, node: ast::NodeId) -> Option<DefIndex> {
449 self.node_to_def_index.get(&node).cloned()
453 pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
454 self.opt_def_index(node).map(DefId::local)
458 pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
459 self.opt_local_def_id(node).unwrap()
463 pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
464 if def_id.krate == LOCAL_CRATE {
465 let space_index = def_id.index.address_space().index();
466 let array_index = def_id.index.as_array_index();
467 let node_id = self.def_index_to_node[space_index][array_index];
468 if node_id != ast::DUMMY_NODE_ID {
479 pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
480 self.node_to_hir_id[node_id]
484 pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> hir::HirId {
485 let space_index = def_index.address_space().index();
486 let array_index = def_index.as_array_index();
487 let node_id = self.def_index_to_node[space_index][array_index];
488 self.node_to_hir_id[node_id]
491 /// Retrieve the span of the given `DefId` if `DefId` is in the local crate, the span exists and
492 /// it's not DUMMY_SP
494 pub fn opt_span(&self, def_id: DefId) -> Option<Span> {
495 if def_id.krate == LOCAL_CRATE {
496 self.def_index_to_span.get(&def_id.index).cloned()
502 /// Add a definition with a parent definition.
503 pub fn create_root_def(&mut self,
505 crate_disambiguator: CrateDisambiguator)
509 disambiguated_data: DisambiguatedDefPathData {
510 data: DefPathData::CrateRoot,
515 let parent_hash = DefKey::root_parent_stable_hash(crate_name,
516 crate_disambiguator);
517 let def_path_hash = key.compute_stable_hash(parent_hash);
519 // Create the definition.
520 let address_space = super::ITEM_LIKE_SPACE;
521 let root_index = self.table.allocate(key, def_path_hash, address_space);
522 assert_eq!(root_index, CRATE_DEF_INDEX);
523 assert!(self.def_index_to_node[address_space.index()].is_empty());
524 self.def_index_to_node[address_space.index()].push(ast::CRATE_NODE_ID);
525 self.node_to_def_index.insert(ast::CRATE_NODE_ID, root_index);
527 // Allocate some other DefIndices that always must exist.
528 GlobalMetaDataKind::allocate_def_indices(self);
533 /// Add a definition with a parent definition.
534 pub fn create_def_with_parent(&mut self,
536 node_id: ast::NodeId,
538 address_space: DefIndexAddressSpace,
542 debug!("create_def_with_parent(parent={:?}, node_id={:?}, data={:?})",
543 parent, node_id, data);
545 assert!(!self.node_to_def_index.contains_key(&node_id),
546 "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
549 self.table.def_key(self.node_to_def_index[&node_id]));
551 // The root node must be created with create_root_def()
552 assert!(data != DefPathData::CrateRoot);
554 // Find the next free disambiguator for this key.
555 let disambiguator = {
556 let next_disamb = self.next_disambiguator.entry((parent, data.clone())).or_insert(0);
557 let disambiguator = *next_disamb;
558 *next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow");
563 parent: Some(parent),
564 disambiguated_data: DisambiguatedDefPathData {
569 let parent_hash = self.table.def_path_hash(parent);
570 let def_path_hash = key.compute_stable_hash(parent_hash);
572 debug!("create_def_with_parent: after disambiguation, key = {:?}", key);
574 // Create the definition.
575 let index = self.table.allocate(key, def_path_hash, address_space);
576 assert_eq!(index.as_array_index(),
577 self.def_index_to_node[address_space.index()].len());
578 self.def_index_to_node[address_space.index()].push(node_id);
580 // Some things for which we allocate DefIndices don't correspond to
581 // anything in the AST, so they don't have a NodeId. For these cases
582 // we don't need a mapping from NodeId to DefIndex.
583 if node_id != ast::DUMMY_NODE_ID {
584 debug!("create_def_with_parent: def_index_to_node[{:?} <-> {:?}", index, node_id);
585 self.node_to_def_index.insert(node_id, index);
588 if expansion != Mark::root() {
589 self.expansions_that_defined.insert(index, expansion);
592 // The span is added if it isn't dummy
593 if !span.is_dummy() {
594 self.def_index_to_span.insert(index, span);
600 /// Initialize the ast::NodeId to HirId mapping once it has been generated during
601 /// AST to HIR lowering.
602 pub fn init_node_id_to_hir_id_mapping(&mut self,
603 mapping: IndexVec<ast::NodeId, hir::HirId>) {
604 assert!(self.node_to_hir_id.is_empty(),
605 "Trying initialize NodeId -> HirId mapping twice");
606 self.node_to_hir_id = mapping;
609 pub fn expansion_that_defined(&self, index: DefIndex) -> Mark {
610 self.expansions_that_defined.get(&index).cloned().unwrap_or(Mark::root())
613 pub fn parent_module_of_macro_def(&self, mark: Mark) -> DefId {
614 self.parent_modules_of_macro_defs[&mark]
617 pub fn add_parent_module_of_macro_def(&mut self, mark: Mark, module: DefId) {
618 self.parent_modules_of_macro_defs.insert(mark, module);
623 pub fn get_opt_name(&self) -> Option<InternedString> {
624 use self::DefPathData::*;
628 AssocTypeInTrait(name) |
629 AssocTypeInImpl(name) |
630 AssocExistentialInImpl(name) |
635 LifetimeParam(name) |
638 GlobalMetaData(name) => Some(name),
650 pub fn as_interned_str(&self) -> InternedString {
651 use self::DefPathData::*;
652 let s = match *self {
655 AssocTypeInTrait(name) |
656 AssocTypeInImpl(name) |
657 AssocExistentialInImpl(name) |
662 LifetimeParam(name) |
665 GlobalMetaData(name) => {
668 // note that this does not show up in user printouts
669 CrateRoot => "{{root}}",
672 ClosureExpr => "{{closure}}",
673 StructCtor => "{{constructor}}",
674 AnonConst => "{{constant}}",
675 ImplTrait => "{{impl-Trait}}",
678 Symbol::intern(s).as_interned_str()
681 pub fn to_string(&self) -> String {
682 self.as_interned_str().to_string()
688 ( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));
691 // We define the GlobalMetaDataKind enum with this macro because we want to
692 // make sure that we exhaustively iterate over all variants when registering
693 // the corresponding DefIndices in the DefTable.
694 macro_rules! define_global_metadata_kind {
695 (pub enum GlobalMetaDataKind {
698 #[derive(Clone, Copy, Debug, Hash, RustcEncodable, RustcDecodable)]
699 pub enum GlobalMetaDataKind {
703 const GLOBAL_MD_ADDRESS_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::High;
704 pub const FIRST_FREE_HIGH_DEF_INDEX: usize = count!($($variant)*);
706 impl GlobalMetaDataKind {
707 fn allocate_def_indices(definitions: &mut Definitions) {
709 let instance = GlobalMetaDataKind::$variant;
710 definitions.create_def_with_parent(
713 DefPathData::GlobalMetaData(instance.name().as_interned_str()),
714 GLOBAL_MD_ADDRESS_SPACE,
719 // Make sure calling def_index does not crash.
720 instance.def_index(&definitions.table);
724 pub fn def_index(&self, def_path_table: &DefPathTable) -> DefIndex {
725 let def_key = DefKey {
726 parent: Some(CRATE_DEF_INDEX),
727 disambiguated_data: DisambiguatedDefPathData {
728 data: DefPathData::GlobalMetaData(self.name().as_interned_str()),
733 // These DefKeys are all right after the root,
734 // so a linear search is fine.
735 let index = def_path_table.index_to_key[GLOBAL_MD_ADDRESS_SPACE.index()]
737 .position(|k| *k == def_key)
740 DefIndex::from_array_index(index, GLOBAL_MD_ADDRESS_SPACE)
743 fn name(&self) -> Symbol {
745 let string = match *self {
747 GlobalMetaDataKind::$variant => {
748 concat!("{{GlobalMetaData::", stringify!($variant), "}}")
753 Symbol::intern(string)
759 define_global_metadata_kind!(pub enum GlobalMetaDataKind {
762 DylibDependencyFormats,