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];
154 /// The definition table containing node definitions.
155 /// It holds the DefPathTable for local DefIds/DefPaths and it also stores a
156 /// mapping from NodeIds to local DefIds.
157 #[derive(Clone, Default)]
158 pub struct Definitions {
160 node_to_def_index: NodeMap<DefIndex>,
161 def_index_to_node: [Vec<ast::NodeId>; 2],
162 pub(super) node_to_hir_id: IndexVec<ast::NodeId, hir::HirId>,
163 /// If `Mark` is an ID of some macro expansion,
164 /// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
165 parent_modules_of_macro_defs: FxHashMap<Mark, DefId>,
166 /// Item with a given `DefIndex` was defined during macro expansion with ID `Mark`.
167 expansions_that_defined: FxHashMap<DefIndex, Mark>,
168 next_disambiguator: FxHashMap<(DefIndex, DefPathData), u32>,
169 def_index_to_span: FxHashMap<DefIndex, Span>,
172 /// A unique identifier that we can use to lookup a definition
173 /// precisely. It combines the index of the definition's parent (if
174 /// any) with a `DisambiguatedDefPathData`.
175 #[derive(Clone, PartialEq, Debug, Hash, RustcEncodable, RustcDecodable)]
178 pub parent: Option<DefIndex>,
180 /// Identifier of this node.
181 pub disambiguated_data: DisambiguatedDefPathData,
185 fn compute_stable_hash(&self, parent_hash: DefPathHash) -> DefPathHash {
186 let mut hasher = StableHasher::new();
188 // We hash a 0u8 here to disambiguate between regular DefPath hashes,
189 // and the special "root_parent" below.
190 0u8.hash(&mut hasher);
191 parent_hash.hash(&mut hasher);
193 let DisambiguatedDefPathData {
196 } = self.disambiguated_data;
198 ::std::mem::discriminant(data).hash(&mut hasher);
199 if let Some(name) = data.get_opt_name() {
200 name.hash(&mut hasher);
203 disambiguator.hash(&mut hasher);
205 DefPathHash(hasher.finish())
208 fn root_parent_stable_hash(crate_name: &str,
209 crate_disambiguator: CrateDisambiguator)
211 let mut hasher = StableHasher::new();
212 // Disambiguate this from a regular DefPath hash,
213 // see compute_stable_hash() above.
214 1u8.hash(&mut hasher);
215 crate_name.hash(&mut hasher);
216 crate_disambiguator.hash(&mut hasher);
217 DefPathHash(hasher.finish())
221 /// Pair of `DefPathData` and an integer disambiguator. The integer is
222 /// normally 0, but in the event that there are multiple defs with the
223 /// same `parent` and `data`, we use this field to disambiguate
224 /// between them. This introduces some artificial ordering dependency
225 /// but means that if you have (e.g.) two impls for the same type in
226 /// the same module, they do get distinct def-ids.
227 #[derive(Clone, PartialEq, Debug, Hash, RustcEncodable, RustcDecodable)]
228 pub struct DisambiguatedDefPathData {
229 pub data: DefPathData,
230 pub disambiguator: u32
233 #[derive(Clone, Debug, Hash, RustcEncodable, RustcDecodable)]
235 /// the path leading from the crate root to the item
236 pub data: Vec<DisambiguatedDefPathData>,
238 /// what krate root is this path relative to?
243 pub fn is_local(&self) -> bool {
244 self.krate == LOCAL_CRATE
247 pub fn make<FN>(krate: CrateNum,
248 start_index: DefIndex,
249 mut get_key: FN) -> DefPath
250 where FN: FnMut(DefIndex) -> DefKey
252 let mut data = vec![];
253 let mut index = Some(start_index);
255 debug!("DefPath::make: krate={:?} index={:?}", krate, index);
256 let p = index.unwrap();
257 let key = get_key(p);
258 debug!("DefPath::make: key={:?}", key);
259 match key.disambiguated_data.data {
260 DefPathData::CrateRoot => {
261 assert!(key.parent.is_none());
265 data.push(key.disambiguated_data);
271 DefPath { data: data, krate: krate }
274 /// Returns a string representation of the DefPath without
275 /// the crate-prefix. This method is useful if you don't have
276 /// a TyCtxt available.
277 pub fn to_string_no_crate(&self) -> String {
278 let mut s = String::with_capacity(self.data.len() * 16);
280 for component in &self.data {
283 component.data.as_interned_str(),
284 component.disambiguator)
291 /// Return filename friendly string of the DefPah with the
293 pub fn to_string_friendly<F>(&self, crate_imported_name: F) -> String
294 where F: FnOnce(CrateNum) -> Symbol
296 let crate_name_str = crate_imported_name(self.krate).as_str();
297 let mut s = String::with_capacity(crate_name_str.len() + self.data.len() * 16);
299 write!(s, "::{}", crate_name_str).unwrap();
301 for component in &self.data {
302 if component.disambiguator == 0 {
303 write!(s, "::{}", component.data.as_interned_str()).unwrap();
307 component.data.as_interned_str(),
308 component.disambiguator)
316 /// Return filename friendly string of the DefPah without
317 /// the crate-prefix. This method is useful if you don't have
318 /// a TyCtxt available.
319 pub fn to_filename_friendly_no_crate(&self) -> String {
320 let mut s = String::with_capacity(self.data.len() * 16);
322 let mut opt_delimiter = None;
323 for component in &self.data {
324 opt_delimiter.map(|d| s.push(d));
325 opt_delimiter = Some('-');
326 if component.disambiguator == 0 {
327 write!(s, "{}", component.data.as_interned_str()).unwrap();
331 component.data.as_interned_str(),
332 component.disambiguator)
340 #[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
341 pub enum DefPathData {
342 // Root: these should only be used for the root nodes, because
343 // they are treated specially by the `def_path` function.
344 /// The crate root (marker)
346 // Catch-all for random DefId things like DUMMY_NODE_ID
348 // Different kinds of items and item-like things:
352 Trait(InternedString),
353 /// An associated type **declaration** (i.e., in a trait)
354 AssocTypeInTrait(InternedString),
355 /// An associated type **value** (i.e., in an impl)
356 AssocTypeInImpl(InternedString),
357 /// An existential associated type **value** (i.e., in an impl)
358 AssocExistentialInImpl(InternedString),
359 /// Something in the type NS
360 TypeNs(InternedString),
361 /// Something in the value NS
362 ValueNs(InternedString),
363 /// A module declaration
364 Module(InternedString),
366 MacroDef(InternedString),
367 /// A closure expression
369 // Subportions of items
370 /// A type parameter (generic parameter)
371 TypeParam(InternedString),
372 /// A lifetime definition
373 LifetimeParam(InternedString),
374 /// A variant of a enum
375 EnumVariant(InternedString),
377 Field(InternedString),
378 /// Implicit ctor for a tuple-like struct
380 /// A constant expression (see {ast,hir}::AnonConst).
382 /// An `impl Trait` type node
384 /// GlobalMetaData identifies a piece of crate metadata that is global to
385 /// a whole crate (as opposed to just one item). GlobalMetaData components
386 /// are only supposed to show up right below the crate root.
387 GlobalMetaData(InternedString)
390 #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
391 RustcEncodable, RustcDecodable)]
392 pub struct DefPathHash(pub Fingerprint);
394 impl_stable_hash_for!(tuple_struct DefPathHash { fingerprint });
396 impl Borrow<Fingerprint> for DefPathHash {
398 fn borrow(&self) -> &Fingerprint {
404 /// Create new empty definition map.
406 /// The DefIndex returned from a new Definitions are as follows:
407 /// 1. At DefIndexAddressSpace::Low,
408 /// CRATE_ROOT has index 0:0, and then new indexes are allocated in
410 /// 2. At DefIndexAddressSpace::High,
411 /// the first FIRST_FREE_HIGH_DEF_INDEX indexes are reserved for
412 /// internal use, then 1:FIRST_FREE_HIGH_DEF_INDEX are allocated in
415 /// FIXME: there is probably a better place to put this comment.
416 pub fn new() -> Self {
420 pub fn def_path_table(&self) -> &DefPathTable {
424 /// Get the number of definitions.
425 pub fn def_index_counts_lo_hi(&self) -> (usize, usize) {
426 (self.table.index_to_key[DefIndexAddressSpace::Low.index()].len(),
427 self.table.index_to_key[DefIndexAddressSpace::High.index()].len())
430 pub fn def_key(&self, index: DefIndex) -> DefKey {
431 self.table.def_key(index)
435 pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
436 self.table.def_path_hash(index)
439 /// Returns the path from the crate root to `index`. The root
440 /// nodes are not included in the path (i.e., this will be an
441 /// empty vector for the crate root). For an inlined item, this
442 /// will be the path of the item in the external crate (but the
443 /// path will begin with the path to the external crate).
444 pub fn def_path(&self, index: DefIndex) -> DefPath {
445 DefPath::make(LOCAL_CRATE, index, |p| self.def_key(p))
449 pub fn opt_def_index(&self, node: ast::NodeId) -> Option<DefIndex> {
450 self.node_to_def_index.get(&node).cloned()
454 pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
455 self.opt_def_index(node).map(DefId::local)
459 pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
460 self.opt_local_def_id(node).unwrap()
464 pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
465 if def_id.krate == LOCAL_CRATE {
466 let space_index = def_id.index.address_space().index();
467 let array_index = def_id.index.as_array_index();
468 let node_id = self.def_index_to_node[space_index][array_index];
469 if node_id != ast::DUMMY_NODE_ID {
480 pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
481 self.node_to_hir_id[node_id]
485 pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> hir::HirId {
486 let space_index = def_index.address_space().index();
487 let array_index = def_index.as_array_index();
488 let node_id = self.def_index_to_node[space_index][array_index];
489 self.node_to_hir_id[node_id]
492 /// Retrieve the span of the given `DefId` if `DefId` is in the local crate, the span exists and
493 /// it's not DUMMY_SP
495 pub fn opt_span(&self, def_id: DefId) -> Option<Span> {
496 if def_id.krate == LOCAL_CRATE {
497 self.def_index_to_span.get(&def_id.index).cloned()
503 /// Add a definition with a parent definition.
504 pub fn create_root_def(&mut self,
506 crate_disambiguator: CrateDisambiguator)
510 disambiguated_data: DisambiguatedDefPathData {
511 data: DefPathData::CrateRoot,
516 let parent_hash = DefKey::root_parent_stable_hash(crate_name,
517 crate_disambiguator);
518 let def_path_hash = key.compute_stable_hash(parent_hash);
520 // Create the definition.
521 let address_space = super::ITEM_LIKE_SPACE;
522 let root_index = self.table.allocate(key, def_path_hash, address_space);
523 assert_eq!(root_index, CRATE_DEF_INDEX);
524 assert!(self.def_index_to_node[address_space.index()].is_empty());
525 self.def_index_to_node[address_space.index()].push(ast::CRATE_NODE_ID);
526 self.node_to_def_index.insert(ast::CRATE_NODE_ID, root_index);
528 // Allocate some other DefIndices that always must exist.
529 GlobalMetaDataKind::allocate_def_indices(self);
534 /// Add a definition with a parent definition.
535 pub fn create_def_with_parent(&mut self,
537 node_id: ast::NodeId,
539 address_space: DefIndexAddressSpace,
543 debug!("create_def_with_parent(parent={:?}, node_id={:?}, data={:?})",
544 parent, node_id, data);
546 assert!(!self.node_to_def_index.contains_key(&node_id),
547 "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
550 self.table.def_key(self.node_to_def_index[&node_id]));
552 // The root node must be created with create_root_def()
553 assert!(data != DefPathData::CrateRoot);
555 // Find the next free disambiguator for this key.
556 let disambiguator = {
557 let next_disamb = self.next_disambiguator.entry((parent, data.clone())).or_insert(0);
558 let disambiguator = *next_disamb;
559 *next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow");
564 parent: Some(parent),
565 disambiguated_data: DisambiguatedDefPathData {
570 let parent_hash = self.table.def_path_hash(parent);
571 let def_path_hash = key.compute_stable_hash(parent_hash);
573 debug!("create_def_with_parent: after disambiguation, key = {:?}", key);
575 // Create the definition.
576 let index = self.table.allocate(key, def_path_hash, address_space);
577 assert_eq!(index.as_array_index(),
578 self.def_index_to_node[address_space.index()].len());
579 self.def_index_to_node[address_space.index()].push(node_id);
581 // Some things for which we allocate DefIndices don't correspond to
582 // anything in the AST, so they don't have a NodeId. For these cases
583 // we don't need a mapping from NodeId to DefIndex.
584 if node_id != ast::DUMMY_NODE_ID {
585 debug!("create_def_with_parent: def_index_to_node[{:?} <-> {:?}", index, node_id);
586 self.node_to_def_index.insert(node_id, index);
589 if expansion != Mark::root() {
590 self.expansions_that_defined.insert(index, expansion);
593 // The span is added if it isn't dummy
594 if !span.is_dummy() {
595 self.def_index_to_span.insert(index, span);
601 /// Initialize the ast::NodeId to HirId mapping once it has been generated during
602 /// AST to HIR lowering.
603 pub fn init_node_id_to_hir_id_mapping(&mut self,
604 mapping: IndexVec<ast::NodeId, hir::HirId>) {
605 assert!(self.node_to_hir_id.is_empty(),
606 "Trying initialize NodeId -> HirId mapping twice");
607 self.node_to_hir_id = mapping;
610 pub fn expansion_that_defined(&self, index: DefIndex) -> Mark {
611 self.expansions_that_defined.get(&index).cloned().unwrap_or(Mark::root())
614 pub fn parent_module_of_macro_def(&self, mark: Mark) -> DefId {
615 self.parent_modules_of_macro_defs[&mark]
618 pub fn add_parent_module_of_macro_def(&mut self, mark: Mark, module: DefId) {
619 self.parent_modules_of_macro_defs.insert(mark, module);
624 pub fn get_opt_name(&self) -> Option<InternedString> {
625 use self::DefPathData::*;
629 AssocTypeInTrait(name) |
630 AssocTypeInImpl(name) |
631 AssocExistentialInImpl(name) |
636 LifetimeParam(name) |
639 GlobalMetaData(name) => Some(name),
651 pub fn as_interned_str(&self) -> InternedString {
652 use self::DefPathData::*;
653 let s = match *self {
656 AssocTypeInTrait(name) |
657 AssocTypeInImpl(name) |
658 AssocExistentialInImpl(name) |
663 LifetimeParam(name) |
666 GlobalMetaData(name) => {
669 // note that this does not show up in user printouts
670 CrateRoot => "{{root}}",
673 ClosureExpr => "{{closure}}",
674 StructCtor => "{{constructor}}",
675 AnonConst => "{{constant}}",
676 ImplTrait => "{{impl-Trait}}",
679 Symbol::intern(s).as_interned_str()
682 pub fn to_string(&self) -> String {
683 self.as_interned_str().to_string()
689 ( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));
692 // We define the GlobalMetaDataKind enum with this macro because we want to
693 // make sure that we exhaustively iterate over all variants when registering
694 // the corresponding DefIndices in the DefTable.
695 macro_rules! define_global_metadata_kind {
696 (pub enum GlobalMetaDataKind {
699 #[derive(Clone, Copy, Debug, Hash, RustcEncodable, RustcDecodable)]
700 pub enum GlobalMetaDataKind {
704 const GLOBAL_MD_ADDRESS_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::High;
705 pub const FIRST_FREE_HIGH_DEF_INDEX: usize = count!($($variant)*);
707 impl GlobalMetaDataKind {
708 fn allocate_def_indices(definitions: &mut Definitions) {
710 let instance = GlobalMetaDataKind::$variant;
711 definitions.create_def_with_parent(
714 DefPathData::GlobalMetaData(instance.name().as_interned_str()),
715 GLOBAL_MD_ADDRESS_SPACE,
720 // Make sure calling def_index does not crash.
721 instance.def_index(&definitions.table);
725 pub fn def_index(&self, def_path_table: &DefPathTable) -> DefIndex {
726 let def_key = DefKey {
727 parent: Some(CRATE_DEF_INDEX),
728 disambiguated_data: DisambiguatedDefPathData {
729 data: DefPathData::GlobalMetaData(self.name().as_interned_str()),
734 // These DefKeys are all right after the root,
735 // so a linear search is fine.
736 let index = def_path_table.index_to_key[GLOBAL_MD_ADDRESS_SPACE.index()]
738 .position(|k| *k == def_key)
741 DefIndex::from_array_index(index, GLOBAL_MD_ADDRESS_SPACE)
744 fn name(&self) -> Symbol {
746 let string = match *self {
748 GlobalMetaDataKind::$variant => {
749 concat!("{{GlobalMetaData::", stringify!($variant), "}}")
754 Symbol::intern(string)
760 define_global_metadata_kind!(pub enum GlobalMetaDataKind {
763 DylibDependencyFormats,