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.
39 pub struct DefPathTable {
40 index_to_key: [Vec<DefKey>; 2],
41 def_path_hashes: [Vec<DefPathHash>; 2],
44 // Unfortunately we have to provide a manual impl of Clone because of the
45 // fixed-sized array field.
46 impl Clone for DefPathTable {
47 fn clone(&self) -> Self {
49 index_to_key: [self.index_to_key[0].clone(),
50 self.index_to_key[1].clone()],
51 def_path_hashes: [self.def_path_hashes[0].clone(),
52 self.def_path_hashes[1].clone()],
59 fn allocate(&mut self,
61 def_path_hash: DefPathHash,
62 address_space: DefIndexAddressSpace)
65 let index_to_key = &mut self.index_to_key[address_space.index()];
66 let index = DefIndex::from_array_index(index_to_key.len(), address_space);
67 debug!("DefPathTable::insert() - {:?} <-> {:?}", key, index);
68 index_to_key.push(key);
71 self.def_path_hashes[address_space.index()].push(def_path_hash);
72 debug_assert!(self.def_path_hashes[address_space.index()].len() ==
73 self.index_to_key[address_space.index()].len());
77 pub fn next_id(&self, address_space: DefIndexAddressSpace) -> DefIndex {
78 DefIndex::from_array_index(self.index_to_key[address_space.index()].len(), address_space)
82 pub fn def_key(&self, index: DefIndex) -> DefKey {
83 self.index_to_key[index.address_space().index()]
84 [index.as_array_index()].clone()
88 pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
89 let ret = self.def_path_hashes[index.address_space().index()]
90 [index.as_array_index()];
91 debug!("def_path_hash({:?}) = {:?}", index, ret);
95 pub fn add_def_path_hashes_to(&self,
97 out: &mut FxHashMap<DefPathHash, DefId>) {
98 for &address_space in &[DefIndexAddressSpace::Low, DefIndexAddressSpace::High] {
100 (&self.def_path_hashes[address_space.index()])
103 .map(|(index, &hash)| {
106 index: DefIndex::from_array_index(index, address_space),
114 pub fn size(&self) -> usize {
115 self.index_to_key.iter().map(|v| v.len()).sum()
120 impl Encodable for DefPathTable {
121 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
123 self.index_to_key[DefIndexAddressSpace::Low.index()].encode(s)?;
124 self.index_to_key[DefIndexAddressSpace::High.index()].encode(s)?;
127 self.def_path_hashes[DefIndexAddressSpace::Low.index()].encode(s)?;
128 self.def_path_hashes[DefIndexAddressSpace::High.index()].encode(s)?;
134 impl Decodable for DefPathTable {
135 fn decode<D: Decoder>(d: &mut D) -> Result<DefPathTable, D::Error> {
136 let index_to_key_lo: Vec<DefKey> = Decodable::decode(d)?;
137 let index_to_key_hi: Vec<DefKey> = Decodable::decode(d)?;
139 let def_path_hashes_lo: Vec<DefPathHash> = Decodable::decode(d)?;
140 let def_path_hashes_hi: Vec<DefPathHash> = Decodable::decode(d)?;
142 let index_to_key = [index_to_key_lo, index_to_key_hi];
143 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 DefIds/DefPaths and it also stores a
155 /// mapping from NodeIds to local DefIds.
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 without
291 /// the crate-prefix. This method is useful if you don't have
292 /// a TyCtxt available.
293 pub fn to_filename_friendly_no_crate(&self) -> String {
294 let mut s = String::with_capacity(self.data.len() * 16);
296 let mut opt_delimiter = None;
297 for component in &self.data {
298 opt_delimiter.map(|d| s.push(d));
299 opt_delimiter = Some('-');
300 if component.disambiguator == 0 {
301 write!(s, "{}", component.data.as_interned_str()).unwrap();
305 component.data.as_interned_str(),
306 component.disambiguator)
314 #[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
315 pub enum DefPathData {
316 // Root: these should only be used for the root nodes, because
317 // they are treated specially by the `def_path` function.
318 /// The crate root (marker)
321 // Catch-all for random DefId things like DUMMY_NODE_ID
324 // Different kinds of items and item-like things:
328 Trait(InternedString),
329 /// An associated type **declaration** (i.e., in a trait)
330 AssocTypeInTrait(InternedString),
331 /// An associated type **value** (i.e., in an impl)
332 AssocTypeInImpl(InternedString),
333 /// An existential associated type **value** (i.e., in an impl)
334 AssocExistentialInImpl(InternedString),
335 /// Something in the type NS
336 TypeNs(InternedString),
337 /// Something in the value NS
338 ValueNs(InternedString),
339 /// A module declaration
340 Module(InternedString),
342 MacroDef(InternedString),
343 /// A closure expression
346 // Subportions of items
347 /// A type parameter (generic parameter)
348 TypeParam(InternedString),
349 /// A lifetime definition
350 LifetimeParam(InternedString),
351 /// A variant of a enum
352 EnumVariant(InternedString),
354 Field(InternedString),
355 /// Implicit ctor for a tuple-like struct
357 /// A constant expression (see {ast,hir}::AnonConst).
359 /// An `impl Trait` type node
362 /// GlobalMetaData identifies a piece of crate metadata that is global to
363 /// a whole crate (as opposed to just one item). GlobalMetaData components
364 /// are only supposed to show up right below the crate root.
365 GlobalMetaData(InternedString)
368 #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
369 RustcEncodable, RustcDecodable)]
370 pub struct DefPathHash(pub Fingerprint);
372 impl_stable_hash_for!(tuple_struct DefPathHash { fingerprint });
374 impl Borrow<Fingerprint> for DefPathHash {
376 fn borrow(&self) -> &Fingerprint {
382 /// Create new empty definition map.
384 /// The DefIndex returned from a new Definitions are as follows:
385 /// 1. At DefIndexAddressSpace::Low,
386 /// CRATE_ROOT has index 0:0, and then new indexes are allocated in
388 /// 2. At DefIndexAddressSpace::High,
389 /// the first FIRST_FREE_HIGH_DEF_INDEX indexes are reserved for
390 /// internal use, then 1:FIRST_FREE_HIGH_DEF_INDEX are allocated in
393 /// FIXME: there is probably a better place to put this comment.
394 pub fn new() -> Definitions {
396 table: DefPathTable {
397 index_to_key: [vec![], vec![]],
398 def_path_hashes: [vec![], vec![]],
400 node_to_def_index: NodeMap(),
401 def_index_to_node: [vec![], vec![]],
402 node_to_hir_id: IndexVec::new(),
403 parent_modules_of_macro_defs: FxHashMap(),
404 expansions_that_defined: FxHashMap(),
405 next_disambiguator: FxHashMap(),
406 def_index_to_span: FxHashMap(),
410 pub fn def_path_table(&self) -> &DefPathTable {
414 /// Get the number of definitions.
415 pub fn def_index_counts_lo_hi(&self) -> (usize, usize) {
416 (self.table.index_to_key[DefIndexAddressSpace::Low.index()].len(),
417 self.table.index_to_key[DefIndexAddressSpace::High.index()].len())
420 pub fn def_key(&self, index: DefIndex) -> DefKey {
421 self.table.def_key(index)
425 pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
426 self.table.def_path_hash(index)
429 /// Returns the path from the crate root to `index`. The root
430 /// nodes are not included in the path (i.e., this will be an
431 /// empty vector for the crate root). For an inlined item, this
432 /// will be the path of the item in the external crate (but the
433 /// path will begin with the path to the external crate).
434 pub fn def_path(&self, index: DefIndex) -> DefPath {
435 DefPath::make(LOCAL_CRATE, index, |p| self.def_key(p))
439 pub fn opt_def_index(&self, node: ast::NodeId) -> Option<DefIndex> {
440 self.node_to_def_index.get(&node).cloned()
444 pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
445 self.opt_def_index(node).map(DefId::local)
449 pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
450 self.opt_local_def_id(node).unwrap()
454 pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
455 if def_id.krate == LOCAL_CRATE {
456 let space_index = def_id.index.address_space().index();
457 let array_index = def_id.index.as_array_index();
458 let node_id = self.def_index_to_node[space_index][array_index];
459 if node_id != ast::DUMMY_NODE_ID {
470 pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
471 self.node_to_hir_id[node_id]
475 pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> hir::HirId {
476 let space_index = def_index.address_space().index();
477 let array_index = def_index.as_array_index();
478 let node_id = self.def_index_to_node[space_index][array_index];
479 self.node_to_hir_id[node_id]
482 /// Retrieve the span of the given `DefId` if `DefId` is in the local crate, the span exists and
483 /// it's not DUMMY_SP
485 pub fn opt_span(&self, def_id: DefId) -> Option<Span> {
486 if def_id.krate == LOCAL_CRATE {
487 self.def_index_to_span.get(&def_id.index).cloned()
493 /// Add a definition with a parent definition.
494 pub fn create_root_def(&mut self,
496 crate_disambiguator: CrateDisambiguator)
500 disambiguated_data: DisambiguatedDefPathData {
501 data: DefPathData::CrateRoot,
506 let parent_hash = DefKey::root_parent_stable_hash(crate_name,
507 crate_disambiguator);
508 let def_path_hash = key.compute_stable_hash(parent_hash);
510 // Create the definition.
511 let address_space = super::ITEM_LIKE_SPACE;
512 let root_index = self.table.allocate(key, def_path_hash, address_space);
513 assert_eq!(root_index, CRATE_DEF_INDEX);
514 assert!(self.def_index_to_node[address_space.index()].is_empty());
515 self.def_index_to_node[address_space.index()].push(ast::CRATE_NODE_ID);
516 self.node_to_def_index.insert(ast::CRATE_NODE_ID, root_index);
518 // Allocate some other DefIndices that always must exist.
519 GlobalMetaDataKind::allocate_def_indices(self);
524 /// Add a definition with a parent definition.
525 pub fn create_def_with_parent(&mut self,
527 node_id: ast::NodeId,
529 address_space: DefIndexAddressSpace,
533 debug!("create_def_with_parent(parent={:?}, node_id={:?}, data={:?})",
534 parent, node_id, data);
536 assert!(!self.node_to_def_index.contains_key(&node_id),
537 "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
540 self.table.def_key(self.node_to_def_index[&node_id]));
542 // The root node must be created with create_root_def()
543 assert!(data != DefPathData::CrateRoot);
545 // Find the next free disambiguator for this key.
546 let disambiguator = {
547 let next_disamb = self.next_disambiguator.entry((parent, data.clone())).or_insert(0);
548 let disambiguator = *next_disamb;
549 *next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow");
554 parent: Some(parent),
555 disambiguated_data: DisambiguatedDefPathData {
560 let parent_hash = self.table.def_path_hash(parent);
561 let def_path_hash = key.compute_stable_hash(parent_hash);
563 debug!("create_def_with_parent: after disambiguation, key = {:?}", key);
565 // Create the definition.
566 let index = self.table.allocate(key, def_path_hash, address_space);
567 assert_eq!(index.as_array_index(),
568 self.def_index_to_node[address_space.index()].len());
569 self.def_index_to_node[address_space.index()].push(node_id);
571 // Some things for which we allocate DefIndices don't correspond to
572 // anything in the AST, so they don't have a NodeId. For these cases
573 // we don't need a mapping from NodeId to DefIndex.
574 if node_id != ast::DUMMY_NODE_ID {
575 debug!("create_def_with_parent: def_index_to_node[{:?} <-> {:?}", index, node_id);
576 self.node_to_def_index.insert(node_id, index);
579 if expansion != Mark::root() {
580 self.expansions_that_defined.insert(index, expansion);
583 // The span is added if it isn't dummy
584 if !span.is_dummy() {
585 self.def_index_to_span.insert(index, span);
591 /// Initialize the ast::NodeId to HirId mapping once it has been generated during
592 /// AST to HIR lowering.
593 pub fn init_node_id_to_hir_id_mapping(&mut self,
594 mapping: IndexVec<ast::NodeId, hir::HirId>) {
595 assert!(self.node_to_hir_id.is_empty(),
596 "Trying initialize NodeId -> HirId mapping twice");
597 self.node_to_hir_id = mapping;
600 pub fn expansion_that_defined(&self, index: DefIndex) -> Mark {
601 self.expansions_that_defined.get(&index).cloned().unwrap_or(Mark::root())
604 pub fn parent_module_of_macro_def(&self, mark: Mark) -> DefId {
605 self.parent_modules_of_macro_defs[&mark]
608 pub fn add_parent_module_of_macro_def(&mut self, mark: Mark, module: DefId) {
609 self.parent_modules_of_macro_defs.insert(mark, module);
614 pub fn get_opt_name(&self) -> Option<InternedString> {
615 use self::DefPathData::*;
619 AssocTypeInTrait(name) |
620 AssocTypeInImpl(name) |
621 AssocExistentialInImpl(name) |
626 LifetimeParam(name) |
629 GlobalMetaData(name) => Some(name),
641 pub fn as_interned_str(&self) -> InternedString {
642 use self::DefPathData::*;
643 let s = match *self {
646 AssocTypeInTrait(name) |
647 AssocTypeInImpl(name) |
648 AssocExistentialInImpl(name) |
653 LifetimeParam(name) |
656 GlobalMetaData(name) => {
660 // note that this does not show up in user printouts
661 CrateRoot => "{{root}}",
665 ClosureExpr => "{{closure}}",
666 StructCtor => "{{constructor}}",
667 AnonConst => "{{constant}}",
668 ImplTrait => "{{impl-Trait}}",
671 Symbol::intern(s).as_interned_str()
674 pub fn to_string(&self) -> String {
675 self.as_interned_str().to_string()
681 ( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));
684 // We define the GlobalMetaDataKind enum with this macro because we want to
685 // make sure that we exhaustively iterate over all variants when registering
686 // the corresponding DefIndices in the DefTable.
687 macro_rules! define_global_metadata_kind {
688 (pub enum GlobalMetaDataKind {
691 #[derive(Clone, Copy, Debug, Hash, RustcEncodable, RustcDecodable)]
692 pub enum GlobalMetaDataKind {
696 const GLOBAL_MD_ADDRESS_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::High;
697 pub const FIRST_FREE_HIGH_DEF_INDEX: usize = count!($($variant)*);
699 impl GlobalMetaDataKind {
700 fn allocate_def_indices(definitions: &mut Definitions) {
702 let instance = GlobalMetaDataKind::$variant;
703 definitions.create_def_with_parent(
706 DefPathData::GlobalMetaData(instance.name().as_interned_str()),
707 GLOBAL_MD_ADDRESS_SPACE,
712 // Make sure calling def_index does not crash.
713 instance.def_index(&definitions.table);
717 pub fn def_index(&self, def_path_table: &DefPathTable) -> DefIndex {
718 let def_key = DefKey {
719 parent: Some(CRATE_DEF_INDEX),
720 disambiguated_data: DisambiguatedDefPathData {
721 data: DefPathData::GlobalMetaData(self.name().as_interned_str()),
726 // These DefKeys are all right after the root,
727 // so a linear search is fine.
728 let index = def_path_table.index_to_key[GLOBAL_MD_ADDRESS_SPACE.index()]
730 .position(|k| *k == def_key)
733 DefIndex::from_array_index(index, GLOBAL_MD_ADDRESS_SPACE)
736 fn name(&self) -> Symbol {
738 let string = match *self {
740 GlobalMetaDataKind::$variant => {
741 concat!("{{GlobalMetaData::", stringify!($variant), "}}")
746 Symbol::intern(string)
752 define_global_metadata_kind!(pub enum GlobalMetaDataKind {
755 DylibDependencyFormats,