]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/map/definitions.rs
Various minor/cosmetic improvements to code
[rust.git] / src / librustc / hir / map / definitions.rs
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.
4 //
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.
10
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.
16
17 use hir;
18 use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE, DefIndexAddressSpace,
19                   CRATE_DEF_INDEX};
20 use ich::Fingerprint;
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;
27 use std::fmt::Write;
28 use std::hash::Hash;
29 use syntax::ast;
30 use syntax::ext::hygiene::Mark;
31 use syntax::symbol::{Symbol, InternedString};
32 use syntax_pos::{Span, DUMMY_SP};
33 use util::nodemap::NodeMap;
34
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 #[derive(Default)]
40 pub struct DefPathTable {
41     index_to_key: [Vec<DefKey>; 2],
42     def_path_hashes: [Vec<DefPathHash>; 2],
43 }
44
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 {
49         DefPathTable {
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()],
54         }
55     }
56 }
57
58 impl DefPathTable {
59
60     fn allocate(&mut self,
61                 key: DefKey,
62                 def_path_hash: DefPathHash,
63                 address_space: DefIndexAddressSpace)
64                 -> DefIndex {
65         let index = {
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);
70             index
71         };
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());
75         index
76     }
77
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)
80     }
81
82     #[inline(always)]
83     pub fn def_key(&self, index: DefIndex) -> DefKey {
84         self.index_to_key[index.address_space().index()]
85                          [index.as_array_index()].clone()
86     }
87
88     #[inline(always)]
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);
93         return ret
94     }
95
96     pub fn add_def_path_hashes_to(&self,
97                                   cnum: CrateNum,
98                                   out: &mut FxHashMap<DefPathHash, DefId>) {
99         for &address_space in &[DefIndexAddressSpace::Low, DefIndexAddressSpace::High] {
100             out.extend(
101                 (&self.def_path_hashes[address_space.index()])
102                     .iter()
103                     .enumerate()
104                     .map(|(index, &hash)| {
105                         let def_id = DefId {
106                             krate: cnum,
107                             index: DefIndex::from_array_index(index, address_space),
108                         };
109                         (hash, def_id)
110                     })
111             );
112         }
113     }
114
115     pub fn size(&self) -> usize {
116         self.index_to_key.iter().map(|v| v.len()).sum()
117     }
118 }
119
120
121 impl Encodable for DefPathTable {
122     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
123         // Index to key
124         self.index_to_key[DefIndexAddressSpace::Low.index()].encode(s)?;
125         self.index_to_key[DefIndexAddressSpace::High.index()].encode(s)?;
126
127         // DefPath hashes
128         self.def_path_hashes[DefIndexAddressSpace::Low.index()].encode(s)?;
129         self.def_path_hashes[DefIndexAddressSpace::High.index()].encode(s)?;
130
131         Ok(())
132     }
133 }
134
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)?;
139
140         let def_path_hashes_lo: Vec<DefPathHash> = Decodable::decode(d)?;
141         let def_path_hashes_hi: Vec<DefPathHash> = Decodable::decode(d)?;
142
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];
145
146         Ok(DefPathTable {
147             index_to_key,
148             def_path_hashes,
149         })
150     }
151 }
152
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 {
158     table: DefPathTable,
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>,
169 }
170
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)]
175 pub struct DefKey {
176     /// Parent path.
177     pub parent: Option<DefIndex>,
178
179     /// Identifier of this node.
180     pub disambiguated_data: DisambiguatedDefPathData,
181 }
182
183 impl DefKey {
184     fn compute_stable_hash(&self, parent_hash: DefPathHash) -> DefPathHash {
185         let mut hasher = StableHasher::new();
186
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);
191
192         let DisambiguatedDefPathData {
193             ref data,
194             disambiguator,
195         } = self.disambiguated_data;
196
197         ::std::mem::discriminant(data).hash(&mut hasher);
198         if let Some(name) = data.get_opt_name() {
199             name.hash(&mut hasher);
200         }
201
202         disambiguator.hash(&mut hasher);
203
204         DefPathHash(hasher.finish())
205     }
206
207     fn root_parent_stable_hash(crate_name: &str,
208                                crate_disambiguator: CrateDisambiguator)
209                                -> DefPathHash {
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())
217     }
218 }
219
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
230 }
231
232 #[derive(Clone, Debug, Hash, RustcEncodable, RustcDecodable)]
233 pub struct DefPath {
234     /// the path leading from the crate root to the item
235     pub data: Vec<DisambiguatedDefPathData>,
236
237     /// what krate root is this path relative to?
238     pub krate: CrateNum,
239 }
240
241 impl DefPath {
242     pub fn is_local(&self) -> bool {
243         self.krate == LOCAL_CRATE
244     }
245
246     pub fn make<FN>(krate: CrateNum,
247                     start_index: DefIndex,
248                     mut get_key: FN) -> DefPath
249         where FN: FnMut(DefIndex) -> DefKey
250     {
251         let mut data = vec![];
252         let mut index = Some(start_index);
253         loop {
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());
261                     break;
262                 }
263                 _ => {
264                     data.push(key.disambiguated_data);
265                     index = key.parent;
266                 }
267             }
268         }
269         data.reverse();
270         DefPath { data: data, krate: krate }
271     }
272
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);
278
279         for component in &self.data {
280             write!(s,
281                    "::{}[{}]",
282                    component.data.as_interned_str(),
283                    component.disambiguator)
284                 .unwrap();
285         }
286
287         s
288     }
289
290     /// Return filename friendly string of the DefPah with the
291     /// crate-prefix.
292     pub fn to_string_friendly<F>(&self, crate_imported_name: F) -> String
293         where F: FnOnce(CrateNum) -> Symbol
294     {
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);
297
298         write!(s, "::{}", crate_name_str).unwrap();
299
300         for component in &self.data {
301             if component.disambiguator == 0 {
302                 write!(s, "::{}", component.data.as_interned_str()).unwrap();
303             } else {
304                 write!(s,
305                        "{}[{}]",
306                        component.data.as_interned_str(),
307                        component.disambiguator)
308                     .unwrap();
309             }
310         }
311
312         s
313     }
314
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);
320
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();
327             } else {
328                 write!(s,
329                        "{}[{}]",
330                        component.data.as_interned_str(),
331                        component.disambiguator)
332                     .unwrap();
333             }
334         }
335         s
336     }
337 }
338
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)
344     CrateRoot,
345     // Catch-all for random DefId things like DUMMY_NODE_ID
346     Misc,
347     // Different kinds of items and item-like things:
348     /// An impl
349     Impl,
350     /// A trait
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),
364     /// A macro rule
365     MacroDef(InternedString),
366     /// A closure expression
367     ClosureExpr,
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),
375     /// A struct field
376     Field(InternedString),
377     /// Implicit ctor for a tuple-like struct
378     StructCtor,
379     /// A constant expression (see {ast,hir}::AnonConst).
380     AnonConst,
381     /// An `impl Trait` type node
382     ImplTrait,
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)
387 }
388
389 #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
390          RustcEncodable, RustcDecodable)]
391 pub struct DefPathHash(pub Fingerprint);
392
393 impl_stable_hash_for!(tuple_struct DefPathHash { fingerprint });
394
395 impl Borrow<Fingerprint> for DefPathHash {
396     #[inline]
397     fn borrow(&self) -> &Fingerprint {
398         &self.0
399     }
400 }
401
402 impl Definitions {
403     /// Create new empty definition map.
404     ///
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
408     ///     ascending order.
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
412     ///     ascending order.
413     ///
414     /// FIXME: there is probably a better place to put this comment.
415     pub fn new() -> Self {
416         Self::default()
417     }
418
419     pub fn def_path_table(&self) -> &DefPathTable {
420         &self.table
421     }
422
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())
427     }
428
429     pub fn def_key(&self, index: DefIndex) -> DefKey {
430         self.table.def_key(index)
431     }
432
433     #[inline(always)]
434     pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
435         self.table.def_path_hash(index)
436     }
437
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))
445     }
446
447     #[inline]
448     pub fn opt_def_index(&self, node: ast::NodeId) -> Option<DefIndex> {
449         self.node_to_def_index.get(&node).cloned()
450     }
451
452     #[inline]
453     pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
454         self.opt_def_index(node).map(DefId::local)
455     }
456
457     #[inline]
458     pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
459         self.opt_local_def_id(node).unwrap()
460     }
461
462     #[inline]
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 {
469                 Some(node_id)
470             } else {
471                 None
472             }
473         } else {
474             None
475         }
476     }
477
478     #[inline]
479     pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
480         self.node_to_hir_id[node_id]
481     }
482
483     #[inline]
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]
489     }
490
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
493     #[inline]
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()
497         } else {
498             None
499         }
500     }
501
502     /// Add a definition with a parent definition.
503     pub fn create_root_def(&mut self,
504                            crate_name: &str,
505                            crate_disambiguator: CrateDisambiguator)
506                            -> DefIndex {
507         let key = DefKey {
508             parent: None,
509             disambiguated_data: DisambiguatedDefPathData {
510                 data: DefPathData::CrateRoot,
511                 disambiguator: 0
512             }
513         };
514
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);
518
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);
526
527         // Allocate some other DefIndices that always must exist.
528         GlobalMetaDataKind::allocate_def_indices(self);
529
530         root_index
531     }
532
533     /// Add a definition with a parent definition.
534     pub fn create_def_with_parent(&mut self,
535                                   parent: DefIndex,
536                                   node_id: ast::NodeId,
537                                   data: DefPathData,
538                                   address_space: DefIndexAddressSpace,
539                                   expansion: Mark,
540                                   span: Span)
541                                   -> DefIndex {
542         debug!("create_def_with_parent(parent={:?}, node_id={:?}, data={:?})",
543                parent, node_id, data);
544
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: {:?}",
547                 node_id,
548                 data,
549                 self.table.def_key(self.node_to_def_index[&node_id]));
550
551         // The root node must be created with create_root_def()
552         assert!(data != DefPathData::CrateRoot);
553
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");
559             disambiguator
560         };
561
562         let key = DefKey {
563             parent: Some(parent),
564             disambiguated_data: DisambiguatedDefPathData {
565                 data, disambiguator
566             }
567         };
568
569         let parent_hash = self.table.def_path_hash(parent);
570         let def_path_hash = key.compute_stable_hash(parent_hash);
571
572         debug!("create_def_with_parent: after disambiguation, key = {:?}", key);
573
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);
579
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);
586         }
587
588         if expansion != Mark::root() {
589             self.expansions_that_defined.insert(index, expansion);
590         }
591
592         // The span is added if it isn't dummy
593         if !span.is_dummy() {
594             self.def_index_to_span.insert(index, span);
595         }
596
597         index
598     }
599
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;
607     }
608
609     pub fn expansion_that_defined(&self, index: DefIndex) -> Mark {
610         self.expansions_that_defined.get(&index).cloned().unwrap_or(Mark::root())
611     }
612
613     pub fn parent_module_of_macro_def(&self, mark: Mark) -> DefId {
614         self.parent_modules_of_macro_defs[&mark]
615     }
616
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);
619     }
620 }
621
622 impl DefPathData {
623     pub fn get_opt_name(&self) -> Option<InternedString> {
624         use self::DefPathData::*;
625         match *self {
626             TypeNs(name) |
627             Trait(name) |
628             AssocTypeInTrait(name) |
629             AssocTypeInImpl(name) |
630             AssocExistentialInImpl(name) |
631             ValueNs(name) |
632             Module(name) |
633             MacroDef(name) |
634             TypeParam(name) |
635             LifetimeParam(name) |
636             EnumVariant(name) |
637             Field(name) |
638             GlobalMetaData(name) => Some(name),
639
640             Impl |
641             CrateRoot |
642             Misc |
643             ClosureExpr |
644             StructCtor |
645             AnonConst |
646             ImplTrait => None
647         }
648     }
649
650     pub fn as_interned_str(&self) -> InternedString {
651         use self::DefPathData::*;
652         let s = match *self {
653             TypeNs(name) |
654             Trait(name) |
655             AssocTypeInTrait(name) |
656             AssocTypeInImpl(name) |
657             AssocExistentialInImpl(name) |
658             ValueNs(name) |
659             Module(name) |
660             MacroDef(name) |
661             TypeParam(name) |
662             LifetimeParam(name) |
663             EnumVariant(name) |
664             Field(name) |
665             GlobalMetaData(name) => {
666                 return name
667             }
668             // note that this does not show up in user printouts
669             CrateRoot => "{{root}}",
670             Impl => "{{impl}}",
671             Misc => "{{?}}",
672             ClosureExpr => "{{closure}}",
673             StructCtor => "{{constructor}}",
674             AnonConst => "{{constant}}",
675             ImplTrait => "{{impl-Trait}}",
676         };
677
678         Symbol::intern(s).as_interned_str()
679     }
680
681     pub fn to_string(&self) -> String {
682         self.as_interned_str().to_string()
683     }
684 }
685
686 macro_rules! count {
687     () => (0usize);
688     ( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));
689 }
690
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 {
696         $($variant:ident),*
697     }) => (
698         #[derive(Clone, Copy, Debug, Hash, RustcEncodable, RustcDecodable)]
699         pub enum GlobalMetaDataKind {
700             $($variant),*
701         }
702
703         const GLOBAL_MD_ADDRESS_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::High;
704         pub const FIRST_FREE_HIGH_DEF_INDEX: usize = count!($($variant)*);
705
706         impl GlobalMetaDataKind {
707             fn allocate_def_indices(definitions: &mut Definitions) {
708                 $({
709                     let instance = GlobalMetaDataKind::$variant;
710                     definitions.create_def_with_parent(
711                         CRATE_DEF_INDEX,
712                         ast::DUMMY_NODE_ID,
713                         DefPathData::GlobalMetaData(instance.name().as_interned_str()),
714                         GLOBAL_MD_ADDRESS_SPACE,
715                         Mark::root(),
716                         DUMMY_SP
717                     );
718
719                     // Make sure calling def_index does not crash.
720                     instance.def_index(&definitions.table);
721                 })*
722             }
723
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()),
729                         disambiguator: 0,
730                     }
731                 };
732
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()]
736                                           .iter()
737                                           .position(|k| *k == def_key)
738                                           .unwrap();
739
740                 DefIndex::from_array_index(index, GLOBAL_MD_ADDRESS_SPACE)
741             }
742
743             fn name(&self) -> Symbol {
744
745                 let string = match *self {
746                     $(
747                         GlobalMetaDataKind::$variant => {
748                             concat!("{{GlobalMetaData::", stringify!($variant), "}}")
749                         }
750                     )*
751                 };
752
753                 Symbol::intern(string)
754             }
755         }
756     )
757 }
758
759 define_global_metadata_kind!(pub enum GlobalMetaDataKind {
760     Krate,
761     CrateDeps,
762     DylibDependencyFormats,
763     LangItems,
764     LangItemsMissing,
765     NativeLibraries,
766     SourceMap,
767     Impls,
768     ExportedSymbols
769 });