]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/map/definitions.rs
eb9bd183fd939a12d8defaa5240d64f4f3d716eb
[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
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 {
159     table: DefPathTable,
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>,
170 }
171
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)]
176 pub struct DefKey {
177     /// Parent path.
178     pub parent: Option<DefIndex>,
179
180     /// Identifier of this node.
181     pub disambiguated_data: DisambiguatedDefPathData,
182 }
183
184 impl DefKey {
185     fn compute_stable_hash(&self, parent_hash: DefPathHash) -> DefPathHash {
186         let mut hasher = StableHasher::new();
187
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);
192
193         let DisambiguatedDefPathData {
194             ref data,
195             disambiguator,
196         } = self.disambiguated_data;
197
198         ::std::mem::discriminant(data).hash(&mut hasher);
199         if let Some(name) = data.get_opt_name() {
200             name.hash(&mut hasher);
201         }
202
203         disambiguator.hash(&mut hasher);
204
205         DefPathHash(hasher.finish())
206     }
207
208     fn root_parent_stable_hash(crate_name: &str,
209                                crate_disambiguator: CrateDisambiguator)
210                                -> DefPathHash {
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())
218     }
219 }
220
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
231 }
232
233 #[derive(Clone, Debug, Hash, RustcEncodable, RustcDecodable)]
234 pub struct DefPath {
235     /// the path leading from the crate root to the item
236     pub data: Vec<DisambiguatedDefPathData>,
237
238     /// what krate root is this path relative to?
239     pub krate: CrateNum,
240 }
241
242 impl DefPath {
243     pub fn is_local(&self) -> bool {
244         self.krate == LOCAL_CRATE
245     }
246
247     pub fn make<FN>(krate: CrateNum,
248                     start_index: DefIndex,
249                     mut get_key: FN) -> DefPath
250         where FN: FnMut(DefIndex) -> DefKey
251     {
252         let mut data = vec![];
253         let mut index = Some(start_index);
254         loop {
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());
262                     break;
263                 }
264                 _ => {
265                     data.push(key.disambiguated_data);
266                     index = key.parent;
267                 }
268             }
269         }
270         data.reverse();
271         DefPath { data: data, krate: krate }
272     }
273
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);
279
280         for component in &self.data {
281             write!(s,
282                    "::{}[{}]",
283                    component.data.as_interned_str(),
284                    component.disambiguator)
285                 .unwrap();
286         }
287
288         s
289     }
290
291     /// Return filename friendly string of the DefPah with the
292     /// crate-prefix.
293     pub fn to_string_friendly<F>(&self, crate_imported_name: F) -> String
294         where F: FnOnce(CrateNum) -> Symbol
295     {
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);
298
299         write!(s, "::{}", crate_name_str).unwrap();
300
301         for component in &self.data {
302             if component.disambiguator == 0 {
303                 write!(s, "::{}", component.data.as_interned_str()).unwrap();
304             } else {
305                 write!(s,
306                        "{}[{}]",
307                        component.data.as_interned_str(),
308                        component.disambiguator)
309                     .unwrap();
310             }
311         }
312
313         s
314     }
315
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);
321
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();
328             } else {
329                 write!(s,
330                        "{}[{}]",
331                        component.data.as_interned_str(),
332                        component.disambiguator)
333                     .unwrap();
334             }
335         }
336         s
337     }
338 }
339
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)
345     CrateRoot,
346     // Catch-all for random DefId things like DUMMY_NODE_ID
347     Misc,
348     // Different kinds of items and item-like things:
349     /// An impl
350     Impl,
351     /// A trait
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),
365     /// A macro rule
366     MacroDef(InternedString),
367     /// A closure expression
368     ClosureExpr,
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),
376     /// A struct field
377     Field(InternedString),
378     /// Implicit ctor for a tuple-like struct
379     StructCtor,
380     /// A constant expression (see {ast,hir}::AnonConst).
381     AnonConst,
382     /// An `impl Trait` type node
383     ImplTrait,
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)
388 }
389
390 #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
391          RustcEncodable, RustcDecodable)]
392 pub struct DefPathHash(pub Fingerprint);
393
394 impl_stable_hash_for!(tuple_struct DefPathHash { fingerprint });
395
396 impl Borrow<Fingerprint> for DefPathHash {
397     #[inline]
398     fn borrow(&self) -> &Fingerprint {
399         &self.0
400     }
401 }
402
403 impl Definitions {
404     /// Create new empty definition map.
405     ///
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
409     ///     ascending order.
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
413     ///     ascending order.
414     ///
415     /// FIXME: there is probably a better place to put this comment.
416     pub fn new() -> Self {
417         Self::default()
418     }
419
420     pub fn def_path_table(&self) -> &DefPathTable {
421         &self.table
422     }
423
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())
428     }
429
430     pub fn def_key(&self, index: DefIndex) -> DefKey {
431         self.table.def_key(index)
432     }
433
434     #[inline(always)]
435     pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
436         self.table.def_path_hash(index)
437     }
438
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))
446     }
447
448     #[inline]
449     pub fn opt_def_index(&self, node: ast::NodeId) -> Option<DefIndex> {
450         self.node_to_def_index.get(&node).cloned()
451     }
452
453     #[inline]
454     pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
455         self.opt_def_index(node).map(DefId::local)
456     }
457
458     #[inline]
459     pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
460         self.opt_local_def_id(node).unwrap()
461     }
462
463     #[inline]
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 {
470                 Some(node_id)
471             } else {
472                 None
473             }
474         } else {
475             None
476         }
477     }
478
479     #[inline]
480     pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
481         self.node_to_hir_id[node_id]
482     }
483
484     #[inline]
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]
490     }
491
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
494     #[inline]
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()
498         } else {
499             None
500         }
501     }
502
503     /// Add a definition with a parent definition.
504     pub fn create_root_def(&mut self,
505                            crate_name: &str,
506                            crate_disambiguator: CrateDisambiguator)
507                            -> DefIndex {
508         let key = DefKey {
509             parent: None,
510             disambiguated_data: DisambiguatedDefPathData {
511                 data: DefPathData::CrateRoot,
512                 disambiguator: 0
513             }
514         };
515
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);
519
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);
527
528         // Allocate some other DefIndices that always must exist.
529         GlobalMetaDataKind::allocate_def_indices(self);
530
531         root_index
532     }
533
534     /// Add a definition with a parent definition.
535     pub fn create_def_with_parent(&mut self,
536                                   parent: DefIndex,
537                                   node_id: ast::NodeId,
538                                   data: DefPathData,
539                                   address_space: DefIndexAddressSpace,
540                                   expansion: Mark,
541                                   span: Span)
542                                   -> DefIndex {
543         debug!("create_def_with_parent(parent={:?}, node_id={:?}, data={:?})",
544                parent, node_id, data);
545
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: {:?}",
548                 node_id,
549                 data,
550                 self.table.def_key(self.node_to_def_index[&node_id]));
551
552         // The root node must be created with create_root_def()
553         assert!(data != DefPathData::CrateRoot);
554
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");
560             disambiguator
561         };
562
563         let key = DefKey {
564             parent: Some(parent),
565             disambiguated_data: DisambiguatedDefPathData {
566                 data, disambiguator
567             }
568         };
569
570         let parent_hash = self.table.def_path_hash(parent);
571         let def_path_hash = key.compute_stable_hash(parent_hash);
572
573         debug!("create_def_with_parent: after disambiguation, key = {:?}", key);
574
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);
580
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);
587         }
588
589         if expansion != Mark::root() {
590             self.expansions_that_defined.insert(index, expansion);
591         }
592
593         // The span is added if it isn't dummy
594         if !span.is_dummy() {
595             self.def_index_to_span.insert(index, span);
596         }
597
598         index
599     }
600
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;
608     }
609
610     pub fn expansion_that_defined(&self, index: DefIndex) -> Mark {
611         self.expansions_that_defined.get(&index).cloned().unwrap_or(Mark::root())
612     }
613
614     pub fn parent_module_of_macro_def(&self, mark: Mark) -> DefId {
615         self.parent_modules_of_macro_defs[&mark]
616     }
617
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);
620     }
621 }
622
623 impl DefPathData {
624     pub fn get_opt_name(&self) -> Option<InternedString> {
625         use self::DefPathData::*;
626         match *self {
627             TypeNs(name) |
628             Trait(name) |
629             AssocTypeInTrait(name) |
630             AssocTypeInImpl(name) |
631             AssocExistentialInImpl(name) |
632             ValueNs(name) |
633             Module(name) |
634             MacroDef(name) |
635             TypeParam(name) |
636             LifetimeParam(name) |
637             EnumVariant(name) |
638             Field(name) |
639             GlobalMetaData(name) => Some(name),
640
641             Impl |
642             CrateRoot |
643             Misc |
644             ClosureExpr |
645             StructCtor |
646             AnonConst |
647             ImplTrait => None
648         }
649     }
650
651     pub fn as_interned_str(&self) -> InternedString {
652         use self::DefPathData::*;
653         let s = match *self {
654             TypeNs(name) |
655             Trait(name) |
656             AssocTypeInTrait(name) |
657             AssocTypeInImpl(name) |
658             AssocExistentialInImpl(name) |
659             ValueNs(name) |
660             Module(name) |
661             MacroDef(name) |
662             TypeParam(name) |
663             LifetimeParam(name) |
664             EnumVariant(name) |
665             Field(name) |
666             GlobalMetaData(name) => {
667                 return name
668             }
669             // note that this does not show up in user printouts
670             CrateRoot => "{{root}}",
671             Impl => "{{impl}}",
672             Misc => "{{?}}",
673             ClosureExpr => "{{closure}}",
674             StructCtor => "{{constructor}}",
675             AnonConst => "{{constant}}",
676             ImplTrait => "{{impl-Trait}}",
677         };
678
679         Symbol::intern(s).as_interned_str()
680     }
681
682     pub fn to_string(&self) -> String {
683         self.as_interned_str().to_string()
684     }
685 }
686
687 macro_rules! count {
688     () => (0usize);
689     ( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));
690 }
691
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 {
697         $($variant:ident),*
698     }) => (
699         #[derive(Clone, Copy, Debug, Hash, RustcEncodable, RustcDecodable)]
700         pub enum GlobalMetaDataKind {
701             $($variant),*
702         }
703
704         const GLOBAL_MD_ADDRESS_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::High;
705         pub const FIRST_FREE_HIGH_DEF_INDEX: usize = count!($($variant)*);
706
707         impl GlobalMetaDataKind {
708             fn allocate_def_indices(definitions: &mut Definitions) {
709                 $({
710                     let instance = GlobalMetaDataKind::$variant;
711                     definitions.create_def_with_parent(
712                         CRATE_DEF_INDEX,
713                         ast::DUMMY_NODE_ID,
714                         DefPathData::GlobalMetaData(instance.name().as_interned_str()),
715                         GLOBAL_MD_ADDRESS_SPACE,
716                         Mark::root(),
717                         DUMMY_SP
718                     );
719
720                     // Make sure calling def_index does not crash.
721                     instance.def_index(&definitions.table);
722                 })*
723             }
724
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()),
730                         disambiguator: 0,
731                     }
732                 };
733
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()]
737                                           .iter()
738                                           .position(|k| *k == def_key)
739                                           .unwrap();
740
741                 DefIndex::from_array_index(index, GLOBAL_MD_ADDRESS_SPACE)
742             }
743
744             fn name(&self) -> Symbol {
745
746                 let string = match *self {
747                     $(
748                         GlobalMetaDataKind::$variant => {
749                             concat!("{{GlobalMetaData::", stringify!($variant), "}}")
750                         }
751                     )*
752                 };
753
754                 Symbol::intern(string)
755             }
756         }
757     )
758 }
759
760 define_global_metadata_kind!(pub enum GlobalMetaDataKind {
761     Krate,
762     CrateDeps,
763     DylibDependencyFormats,
764     LangItems,
765     LangItemsMissing,
766     NativeLibraries,
767     SourceMap,
768     Impls,
769     ExportedSymbols
770 });