]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/map/definitions.rs
Switch wasm math symbols to their original names
[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 pub struct DefPathTable {
40     index_to_key: [Vec<DefKey>; 2],
41     def_path_hashes: [Vec<DefPathHash>; 2],
42 }
43
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 {
48         DefPathTable {
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()],
53         }
54     }
55 }
56
57 impl DefPathTable {
58
59     fn allocate(&mut self,
60                 key: DefKey,
61                 def_path_hash: DefPathHash,
62                 address_space: DefIndexAddressSpace)
63                 -> DefIndex {
64         let index = {
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);
69             index
70         };
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());
74         index
75     }
76
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)
79     }
80
81     #[inline(always)]
82     pub fn def_key(&self, index: DefIndex) -> DefKey {
83         self.index_to_key[index.address_space().index()]
84                          [index.as_array_index()].clone()
85     }
86
87     #[inline(always)]
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);
92         return ret
93     }
94
95     pub fn add_def_path_hashes_to(&self,
96                                   cnum: CrateNum,
97                                   out: &mut FxHashMap<DefPathHash, DefId>) {
98         for &address_space in &[DefIndexAddressSpace::Low, DefIndexAddressSpace::High] {
99             out.extend(
100                 (&self.def_path_hashes[address_space.index()])
101                     .iter()
102                     .enumerate()
103                     .map(|(index, &hash)| {
104                         let def_id = DefId {
105                             krate: cnum,
106                             index: DefIndex::from_array_index(index, address_space),
107                         };
108                         (hash, def_id)
109                     })
110             );
111         }
112     }
113
114     pub fn size(&self) -> usize {
115         self.index_to_key.iter().map(|v| v.len()).sum()
116     }
117 }
118
119
120 impl Encodable for DefPathTable {
121     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
122         // Index to key
123         self.index_to_key[DefIndexAddressSpace::Low.index()].encode(s)?;
124         self.index_to_key[DefIndexAddressSpace::High.index()].encode(s)?;
125
126         // DefPath hashes
127         self.def_path_hashes[DefIndexAddressSpace::Low.index()].encode(s)?;
128         self.def_path_hashes[DefIndexAddressSpace::High.index()].encode(s)?;
129
130         Ok(())
131     }
132 }
133
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)?;
138
139         let def_path_hashes_lo: Vec<DefPathHash> = Decodable::decode(d)?;
140         let def_path_hashes_hi: Vec<DefPathHash> = Decodable::decode(d)?;
141
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];
144
145         Ok(DefPathTable {
146             index_to_key,
147             def_path_hashes,
148         })
149     }
150 }
151
152
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.
156 #[derive(Clone)]
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 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);
295
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();
302             } else {
303                 write!(s,
304                        "{}[{}]",
305                        component.data.as_interned_str(),
306                        component.disambiguator)
307                     .unwrap();
308             }
309         }
310         s
311     }
312 }
313
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)
319     CrateRoot,
320
321     // Catch-all for random DefId things like DUMMY_NODE_ID
322     Misc,
323
324     // Different kinds of items and item-like things:
325     /// An impl
326     Impl,
327     /// A trait
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),
341     /// A macro rule
342     MacroDef(InternedString),
343     /// A closure expression
344     ClosureExpr,
345
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),
353     /// A struct field
354     Field(InternedString),
355     /// Implicit ctor for a tuple-like struct
356     StructCtor,
357     /// A constant expression (see {ast,hir}::AnonConst).
358     AnonConst,
359     /// An `impl Trait` type node
360     ImplTrait,
361
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)
366 }
367
368 #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
369          RustcEncodable, RustcDecodable)]
370 pub struct DefPathHash(pub Fingerprint);
371
372 impl_stable_hash_for!(tuple_struct DefPathHash { fingerprint });
373
374 impl Borrow<Fingerprint> for DefPathHash {
375     #[inline]
376     fn borrow(&self) -> &Fingerprint {
377         &self.0
378     }
379 }
380
381 impl Definitions {
382     /// Create new empty definition map.
383     ///
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
387     ///     ascending order.
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
391     ///     ascending order.
392     ///
393     /// FIXME: there is probably a better place to put this comment.
394     pub fn new() -> Definitions {
395         Definitions {
396             table: DefPathTable {
397                 index_to_key: [vec![], vec![]],
398                 def_path_hashes: [vec![], vec![]],
399             },
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(),
407         }
408     }
409
410     pub fn def_path_table(&self) -> &DefPathTable {
411         &self.table
412     }
413
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())
418     }
419
420     pub fn def_key(&self, index: DefIndex) -> DefKey {
421         self.table.def_key(index)
422     }
423
424     #[inline(always)]
425     pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
426         self.table.def_path_hash(index)
427     }
428
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))
436     }
437
438     #[inline]
439     pub fn opt_def_index(&self, node: ast::NodeId) -> Option<DefIndex> {
440         self.node_to_def_index.get(&node).cloned()
441     }
442
443     #[inline]
444     pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
445         self.opt_def_index(node).map(DefId::local)
446     }
447
448     #[inline]
449     pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
450         self.opt_local_def_id(node).unwrap()
451     }
452
453     #[inline]
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 {
460                 Some(node_id)
461             } else {
462                 None
463             }
464         } else {
465             None
466         }
467     }
468
469     #[inline]
470     pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
471         self.node_to_hir_id[node_id]
472     }
473
474     #[inline]
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]
480     }
481
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
484     #[inline]
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()
488         } else {
489             None
490         }
491     }
492
493     /// Add a definition with a parent definition.
494     pub fn create_root_def(&mut self,
495                            crate_name: &str,
496                            crate_disambiguator: CrateDisambiguator)
497                            -> DefIndex {
498         let key = DefKey {
499             parent: None,
500             disambiguated_data: DisambiguatedDefPathData {
501                 data: DefPathData::CrateRoot,
502                 disambiguator: 0
503             }
504         };
505
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);
509
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);
517
518         // Allocate some other DefIndices that always must exist.
519         GlobalMetaDataKind::allocate_def_indices(self);
520
521         root_index
522     }
523
524     /// Add a definition with a parent definition.
525     pub fn create_def_with_parent(&mut self,
526                                   parent: DefIndex,
527                                   node_id: ast::NodeId,
528                                   data: DefPathData,
529                                   address_space: DefIndexAddressSpace,
530                                   expansion: Mark,
531                                   span: Span)
532                                   -> DefIndex {
533         debug!("create_def_with_parent(parent={:?}, node_id={:?}, data={:?})",
534                parent, node_id, data);
535
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: {:?}",
538                 node_id,
539                 data,
540                 self.table.def_key(self.node_to_def_index[&node_id]));
541
542         // The root node must be created with create_root_def()
543         assert!(data != DefPathData::CrateRoot);
544
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");
550             disambiguator
551         };
552
553         let key = DefKey {
554             parent: Some(parent),
555             disambiguated_data: DisambiguatedDefPathData {
556                 data, disambiguator
557             }
558         };
559
560         let parent_hash = self.table.def_path_hash(parent);
561         let def_path_hash = key.compute_stable_hash(parent_hash);
562
563         debug!("create_def_with_parent: after disambiguation, key = {:?}", key);
564
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);
570
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);
577         }
578
579         if expansion != Mark::root() {
580             self.expansions_that_defined.insert(index, expansion);
581         }
582
583         // The span is added if it isn't dummy
584         if !span.is_dummy() {
585             self.def_index_to_span.insert(index, span);
586         }
587
588         index
589     }
590
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;
598     }
599
600     pub fn expansion_that_defined(&self, index: DefIndex) -> Mark {
601         self.expansions_that_defined.get(&index).cloned().unwrap_or(Mark::root())
602     }
603
604     pub fn parent_module_of_macro_def(&self, mark: Mark) -> DefId {
605         self.parent_modules_of_macro_defs[&mark]
606     }
607
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);
610     }
611 }
612
613 impl DefPathData {
614     pub fn get_opt_name(&self) -> Option<InternedString> {
615         use self::DefPathData::*;
616         match *self {
617             TypeNs(name) |
618             Trait(name) |
619             AssocTypeInTrait(name) |
620             AssocTypeInImpl(name) |
621             AssocExistentialInImpl(name) |
622             ValueNs(name) |
623             Module(name) |
624             MacroDef(name) |
625             TypeParam(name) |
626             LifetimeParam(name) |
627             EnumVariant(name) |
628             Field(name) |
629             GlobalMetaData(name) => Some(name),
630
631             Impl |
632             CrateRoot |
633             Misc |
634             ClosureExpr |
635             StructCtor |
636             AnonConst |
637             ImplTrait => None
638         }
639     }
640
641     pub fn as_interned_str(&self) -> InternedString {
642         use self::DefPathData::*;
643         let s = match *self {
644             TypeNs(name) |
645             Trait(name) |
646             AssocTypeInTrait(name) |
647             AssocTypeInImpl(name) |
648             AssocExistentialInImpl(name) |
649             ValueNs(name) |
650             Module(name) |
651             MacroDef(name) |
652             TypeParam(name) |
653             LifetimeParam(name) |
654             EnumVariant(name) |
655             Field(name) |
656             GlobalMetaData(name) => {
657                 return name
658             }
659
660             // note that this does not show up in user printouts
661             CrateRoot => "{{root}}",
662
663             Impl => "{{impl}}",
664             Misc => "{{?}}",
665             ClosureExpr => "{{closure}}",
666             StructCtor => "{{constructor}}",
667             AnonConst => "{{constant}}",
668             ImplTrait => "{{impl-Trait}}",
669         };
670
671         Symbol::intern(s).as_interned_str()
672     }
673
674     pub fn to_string(&self) -> String {
675         self.as_interned_str().to_string()
676     }
677 }
678
679 macro_rules! count {
680     () => (0usize);
681     ( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));
682 }
683
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 {
689         $($variant:ident),*
690     }) => (
691         #[derive(Clone, Copy, Debug, Hash, RustcEncodable, RustcDecodable)]
692         pub enum GlobalMetaDataKind {
693             $($variant),*
694         }
695
696         const GLOBAL_MD_ADDRESS_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::High;
697         pub const FIRST_FREE_HIGH_DEF_INDEX: usize = count!($($variant)*);
698
699         impl GlobalMetaDataKind {
700             fn allocate_def_indices(definitions: &mut Definitions) {
701                 $({
702                     let instance = GlobalMetaDataKind::$variant;
703                     definitions.create_def_with_parent(
704                         CRATE_DEF_INDEX,
705                         ast::DUMMY_NODE_ID,
706                         DefPathData::GlobalMetaData(instance.name().as_interned_str()),
707                         GLOBAL_MD_ADDRESS_SPACE,
708                         Mark::root(),
709                         DUMMY_SP
710                     );
711
712                     // Make sure calling def_index does not crash.
713                     instance.def_index(&definitions.table);
714                 })*
715             }
716
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()),
722                         disambiguator: 0,
723                     }
724                 };
725
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()]
729                                           .iter()
730                                           .position(|k| *k == def_key)
731                                           .unwrap();
732
733                 DefIndex::from_array_index(index, GLOBAL_MD_ADDRESS_SPACE)
734             }
735
736             fn name(&self) -> Symbol {
737
738                 let string = match *self {
739                     $(
740                         GlobalMetaDataKind::$variant => {
741                             concat!("{{GlobalMetaData::", stringify!($variant), "}}")
742                         }
743                     )*
744                 };
745
746                 Symbol::intern(string)
747             }
748         }
749     )
750 }
751
752 define_global_metadata_kind!(pub enum GlobalMetaDataKind {
753     Krate,
754     CrateDeps,
755     DylibDependencyFormats,
756     LangItems,
757     LangItemsMissing,
758     NativeLibraries,
759     SourceMap,
760     Impls,
761     ExportedSymbols
762 });