]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/map/definitions.rs
remove FIXMEs for functions that won't go away
[rust.git] / src / librustc / hir / map / definitions.rs
1 //! For each definition, we track the following data. A definition
2 //! here is defined somewhat circularly as "something with a `DefId`",
3 //! but it generally corresponds to things like structs, enums, etc.
4 //! There are also some rather random cases (like const initializer
5 //! expressions) that are mostly just leftovers.
6
7 use crate::hir;
8 use crate::hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE, CRATE_DEF_INDEX};
9 use crate::ich::Fingerprint;
10 use rustc_data_structures::fx::FxHashMap;
11 use rustc_data_structures::indexed_vec::{IndexVec};
12 use rustc_data_structures::stable_hasher::StableHasher;
13 use crate::session::CrateDisambiguator;
14 use std::borrow::Borrow;
15 use std::fmt::Write;
16 use std::hash::Hash;
17 use syntax::ast;
18 use syntax::ext::hygiene::Mark;
19 use syntax::symbol::{Symbol, sym, InternedString};
20 use syntax_pos::{Span, DUMMY_SP};
21 use crate::util::nodemap::NodeMap;
22
23 /// The DefPathTable maps DefIndexes to DefKeys and vice versa.
24 /// Internally the DefPathTable holds a tree of DefKeys, where each DefKey
25 /// stores the DefIndex of its parent.
26 /// There is one DefPathTable for each crate.
27 #[derive(Clone, Default, RustcDecodable, RustcEncodable)]
28 pub struct DefPathTable {
29     index_to_key: Vec<DefKey>,
30     def_path_hashes: Vec<DefPathHash>,
31 }
32
33 impl DefPathTable {
34     fn allocate(&mut self,
35                 key: DefKey,
36                 def_path_hash: DefPathHash)
37                 -> DefIndex {
38         let index = {
39             let index = DefIndex::from(self.index_to_key.len());
40             debug!("DefPathTable::insert() - {:?} <-> {:?}", key, index);
41             self.index_to_key.push(key);
42             index
43         };
44         self.def_path_hashes.push(def_path_hash);
45         debug_assert!(self.def_path_hashes.len() == self.index_to_key.len());
46         index
47     }
48
49     pub fn next_id(&self) -> DefIndex {
50         DefIndex::from(self.index_to_key.len())
51     }
52
53     #[inline(always)]
54     pub fn def_key(&self, index: DefIndex) -> DefKey {
55         self.index_to_key[index.index()].clone()
56     }
57
58     #[inline(always)]
59     pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
60         let ret = self.def_path_hashes[index.index()];
61         debug!("def_path_hash({:?}) = {:?}", index, ret);
62         return ret
63     }
64
65     pub fn add_def_path_hashes_to(&self,
66                                   cnum: CrateNum,
67                                   out: &mut FxHashMap<DefPathHash, DefId>) {
68         out.extend(
69             self.def_path_hashes
70                 .iter()
71                 .enumerate()
72                 .map(|(index, &hash)| {
73                     let def_id = DefId {
74                         krate: cnum,
75                         index: DefIndex::from(index),
76                     };
77                     (hash, def_id)
78                 })
79         );
80     }
81
82     pub fn size(&self) -> usize {
83         self.index_to_key.len()
84     }
85 }
86
87 /// The definition table containing node definitions.
88 /// It holds the `DefPathTable` for local `DefId`s/`DefPath`s and it also stores a
89 /// mapping from `NodeId`s to local `DefId`s.
90 #[derive(Clone, Default)]
91 pub struct Definitions {
92     table: DefPathTable,
93     node_to_def_index: NodeMap<DefIndex>,
94     def_index_to_node: Vec<ast::NodeId>,
95     pub(super) node_to_hir_id: IndexVec<ast::NodeId, hir::HirId>,
96     /// If `Mark` is an ID of some macro expansion,
97     /// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
98     parent_modules_of_macro_defs: FxHashMap<Mark, DefId>,
99     /// Item with a given `DefIndex` was defined during macro expansion with ID `Mark`.
100     expansions_that_defined: FxHashMap<DefIndex, Mark>,
101     next_disambiguator: FxHashMap<(DefIndex, DefPathData), u32>,
102     def_index_to_span: FxHashMap<DefIndex, Span>,
103 }
104
105 /// A unique identifier that we can use to lookup a definition
106 /// precisely. It combines the index of the definition's parent (if
107 /// any) with a `DisambiguatedDefPathData`.
108 #[derive(Clone, PartialEq, Debug, Hash, RustcEncodable, RustcDecodable)]
109 pub struct DefKey {
110     /// The parent path.
111     pub parent: Option<DefIndex>,
112
113     /// The identifier of this node.
114     pub disambiguated_data: DisambiguatedDefPathData,
115 }
116
117 impl DefKey {
118     fn compute_stable_hash(&self, parent_hash: DefPathHash) -> DefPathHash {
119         let mut hasher = StableHasher::new();
120
121         // We hash a 0u8 here to disambiguate between regular DefPath hashes,
122         // and the special "root_parent" below.
123         0u8.hash(&mut hasher);
124         parent_hash.hash(&mut hasher);
125
126         let DisambiguatedDefPathData {
127             ref data,
128             disambiguator,
129         } = self.disambiguated_data;
130
131         ::std::mem::discriminant(data).hash(&mut hasher);
132         if let Some(name) = data.get_opt_name() {
133             name.hash(&mut hasher);
134         }
135
136         disambiguator.hash(&mut hasher);
137
138         DefPathHash(hasher.finish())
139     }
140
141     fn root_parent_stable_hash(crate_name: &str,
142                                crate_disambiguator: CrateDisambiguator)
143                                -> DefPathHash {
144         let mut hasher = StableHasher::new();
145         // Disambiguate this from a regular DefPath hash,
146         // see compute_stable_hash() above.
147         1u8.hash(&mut hasher);
148         crate_name.hash(&mut hasher);
149         crate_disambiguator.hash(&mut hasher);
150         DefPathHash(hasher.finish())
151     }
152 }
153
154 /// A pair of `DefPathData` and an integer disambiguator. The integer is
155 /// normally 0, but in the event that there are multiple defs with the
156 /// same `parent` and `data`, we use this field to disambiguate
157 /// between them. This introduces some artificial ordering dependency
158 /// but means that if you have (e.g.) two impls for the same type in
159 /// the same module, they do get distinct `DefId`s.
160 #[derive(Clone, PartialEq, Debug, Hash, RustcEncodable, RustcDecodable)]
161 pub struct DisambiguatedDefPathData {
162     pub data: DefPathData,
163     pub disambiguator: u32
164 }
165
166 #[derive(Clone, Debug, Hash, RustcEncodable, RustcDecodable)]
167 pub struct DefPath {
168     /// The path leading from the crate root to the item.
169     pub data: Vec<DisambiguatedDefPathData>,
170
171     /// The crate root this path is relative to.
172     pub krate: CrateNum,
173 }
174
175 impl DefPath {
176     pub fn is_local(&self) -> bool {
177         self.krate == LOCAL_CRATE
178     }
179
180     pub fn make<FN>(krate: CrateNum,
181                     start_index: DefIndex,
182                     mut get_key: FN) -> DefPath
183         where FN: FnMut(DefIndex) -> DefKey
184     {
185         let mut data = vec![];
186         let mut index = Some(start_index);
187         loop {
188             debug!("DefPath::make: krate={:?} index={:?}", krate, index);
189             let p = index.unwrap();
190             let key = get_key(p);
191             debug!("DefPath::make: key={:?}", key);
192             match key.disambiguated_data.data {
193                 DefPathData::CrateRoot => {
194                     assert!(key.parent.is_none());
195                     break;
196                 }
197                 _ => {
198                     data.push(key.disambiguated_data);
199                     index = key.parent;
200                 }
201             }
202         }
203         data.reverse();
204         DefPath { data: data, krate: krate }
205     }
206
207     /// Returns a string representation of the `DefPath` without
208     /// the crate-prefix. This method is useful if you don't have
209     /// a `TyCtxt` available.
210     pub fn to_string_no_crate(&self) -> String {
211         let mut s = String::with_capacity(self.data.len() * 16);
212
213         for component in &self.data {
214             write!(s,
215                    "::{}[{}]",
216                    component.data.as_interned_str(),
217                    component.disambiguator)
218                 .unwrap();
219         }
220
221         s
222     }
223
224     /// Returns a filename-friendly string for the `DefPath`, with the
225     /// crate-prefix.
226     pub fn to_string_friendly<F>(&self, crate_imported_name: F) -> String
227         where F: FnOnce(CrateNum) -> Symbol
228     {
229         let crate_name_str = crate_imported_name(self.krate).as_str();
230         let mut s = String::with_capacity(crate_name_str.len() + self.data.len() * 16);
231
232         write!(s, "::{}", crate_name_str).unwrap();
233
234         for component in &self.data {
235             if component.disambiguator == 0 {
236                 write!(s, "::{}", component.data.as_interned_str()).unwrap();
237             } else {
238                 write!(s,
239                        "{}[{}]",
240                        component.data.as_interned_str(),
241                        component.disambiguator)
242                        .unwrap();
243             }
244         }
245
246         s
247     }
248
249     /// Returns a filename-friendly string of the `DefPath`, without
250     /// the crate-prefix. This method is useful if you don't have
251     /// a `TyCtxt` available.
252     pub fn to_filename_friendly_no_crate(&self) -> String {
253         let mut s = String::with_capacity(self.data.len() * 16);
254
255         let mut opt_delimiter = None;
256         for component in &self.data {
257             opt_delimiter.map(|d| s.push(d));
258             opt_delimiter = Some('-');
259             if component.disambiguator == 0 {
260                 write!(s, "{}", component.data.as_interned_str()).unwrap();
261             } else {
262                 write!(s,
263                        "{}[{}]",
264                        component.data.as_interned_str(),
265                        component.disambiguator)
266                        .unwrap();
267             }
268         }
269         s
270     }
271 }
272
273 #[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
274 pub enum DefPathData {
275     // Root: these should only be used for the root nodes, because
276     // they are treated specially by the `def_path` function.
277     /// The crate root (marker)
278     CrateRoot,
279     // Catch-all for random DefId things like `DUMMY_NODE_ID`
280     Misc,
281     // Different kinds of items and item-like things:
282     /// An impl
283     Impl,
284     /// Something in the type NS
285     TypeNs(InternedString),
286     /// Something in the value NS
287     ValueNs(InternedString),
288     /// Something in the macro NS
289     MacroNs(InternedString),
290     /// Something in the lifetime NS
291     LifetimeNs(InternedString),
292     /// A closure expression
293     ClosureExpr,
294     // Subportions of items
295     /// Implicit ctor for a unit or tuple-like struct or enum variant.
296     Ctor,
297     /// A constant expression (see {ast,hir}::AnonConst).
298     AnonConst,
299     /// An `impl Trait` type node
300     ImplTrait,
301     /// Identifies a piece of crate metadata that is global to a whole crate
302     /// (as opposed to just one item). `GlobalMetaData` components are only
303     /// supposed to show up right below the crate root.
304     GlobalMetaData(InternedString),
305 }
306
307 #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
308          RustcEncodable, RustcDecodable)]
309 pub struct DefPathHash(pub Fingerprint);
310
311 impl_stable_hash_for!(tuple_struct DefPathHash { fingerprint });
312
313 impl Borrow<Fingerprint> for DefPathHash {
314     #[inline]
315     fn borrow(&self) -> &Fingerprint {
316         &self.0
317     }
318 }
319
320 impl Definitions {
321     pub fn def_path_table(&self) -> &DefPathTable {
322         &self.table
323     }
324
325     /// Gets the number of definitions.
326     pub fn def_index_count(&self) -> usize {
327         self.table.index_to_key.len()
328     }
329
330     pub fn def_key(&self, index: DefIndex) -> DefKey {
331         self.table.def_key(index)
332     }
333
334     #[inline(always)]
335     pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
336         self.table.def_path_hash(index)
337     }
338
339     /// Returns the path from the crate root to `index`. The root
340     /// nodes are not included in the path (i.e., this will be an
341     /// empty vector for the crate root). For an inlined item, this
342     /// will be the path of the item in the external crate (but the
343     /// path will begin with the path to the external crate).
344     pub fn def_path(&self, index: DefIndex) -> DefPath {
345         DefPath::make(LOCAL_CRATE, index, |p| self.def_key(p))
346     }
347
348     #[inline]
349     pub fn opt_def_index(&self, node: ast::NodeId) -> Option<DefIndex> {
350         self.node_to_def_index.get(&node).cloned()
351     }
352
353     #[inline]
354     pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
355         self.opt_def_index(node).map(DefId::local)
356     }
357
358     #[inline]
359     pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
360         self.opt_local_def_id(node).unwrap()
361     }
362
363     #[inline]
364     pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
365         if def_id.krate == LOCAL_CRATE {
366             let node_id = self.def_index_to_node[def_id.index.index()];
367             if node_id != ast::DUMMY_NODE_ID {
368                 return Some(node_id);
369             }
370         }
371         None
372     }
373
374     #[inline]
375     pub fn as_local_hir_id(&self, def_id: DefId) -> Option<hir::HirId> {
376         if def_id.krate == LOCAL_CRATE {
377             let hir_id = self.def_index_to_hir_id(def_id.index);
378             if hir_id != hir::DUMMY_HIR_ID {
379                 Some(hir_id)
380             } else {
381                 None
382             }
383         } else {
384             None
385         }
386     }
387
388     #[inline]
389     pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
390         self.node_to_hir_id[node_id]
391     }
392
393     #[inline]
394     pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> hir::HirId {
395         let node_id = self.def_index_to_node[def_index.index()];
396         self.node_to_hir_id[node_id]
397     }
398
399     /// Retrieves the span of the given `DefId` if `DefId` is in the local crate, the span exists
400     /// and it's not `DUMMY_SP`.
401     #[inline]
402     pub fn opt_span(&self, def_id: DefId) -> Option<Span> {
403         if def_id.krate == LOCAL_CRATE {
404             self.def_index_to_span.get(&def_id.index).cloned()
405         } else {
406             None
407         }
408     }
409
410     /// Adds a root definition (no parent) and a few other reserved definitions.
411     ///
412     /// After the initial definitions are created the first `FIRST_FREE_DEF_INDEX` indexes
413     /// are taken, so the "user" indexes will be allocated starting with `FIRST_FREE_DEF_INDEX`
414     /// in ascending order.
415     pub fn create_root_def(&mut self,
416                            crate_name: &str,
417                            crate_disambiguator: CrateDisambiguator)
418                            -> DefIndex {
419         let key = DefKey {
420             parent: None,
421             disambiguated_data: DisambiguatedDefPathData {
422                 data: DefPathData::CrateRoot,
423                 disambiguator: 0
424             }
425         };
426
427         let parent_hash = DefKey::root_parent_stable_hash(crate_name,
428                                                           crate_disambiguator);
429         let def_path_hash = key.compute_stable_hash(parent_hash);
430
431         // Create the definition.
432         let root_index = self.table.allocate(key, def_path_hash);
433         assert_eq!(root_index, CRATE_DEF_INDEX);
434         assert!(self.def_index_to_node.is_empty());
435         self.def_index_to_node.push(ast::CRATE_NODE_ID);
436         self.node_to_def_index.insert(ast::CRATE_NODE_ID, root_index);
437
438         // Allocate some other DefIndices that always must exist.
439         GlobalMetaDataKind::allocate_def_indices(self);
440
441         root_index
442     }
443
444     /// Adds a definition with a parent definition.
445     pub fn create_def_with_parent(&mut self,
446                                   parent: DefIndex,
447                                   node_id: ast::NodeId,
448                                   data: DefPathData,
449                                   expansion: Mark,
450                                   span: Span)
451                                   -> DefIndex {
452         debug!("create_def_with_parent(parent={:?}, node_id={:?}, data={:?})",
453                parent, node_id, data);
454
455         assert!(!self.node_to_def_index.contains_key(&node_id),
456                 "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
457                 node_id,
458                 data,
459                 self.table.def_key(self.node_to_def_index[&node_id]));
460
461         // The root node must be created with create_root_def()
462         assert!(data != DefPathData::CrateRoot);
463
464         // Find the next free disambiguator for this key.
465         let disambiguator = {
466             let next_disamb = self.next_disambiguator.entry((parent, data.clone())).or_insert(0);
467             let disambiguator = *next_disamb;
468             *next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow");
469             disambiguator
470         };
471
472         let key = DefKey {
473             parent: Some(parent),
474             disambiguated_data: DisambiguatedDefPathData {
475                 data, disambiguator
476             }
477         };
478
479         let parent_hash = self.table.def_path_hash(parent);
480         let def_path_hash = key.compute_stable_hash(parent_hash);
481
482         debug!("create_def_with_parent: after disambiguation, key = {:?}", key);
483
484         // Create the definition.
485         let index = self.table.allocate(key, def_path_hash);
486         assert_eq!(index.index(), self.def_index_to_node.len());
487         self.def_index_to_node.push(node_id);
488
489         // Some things for which we allocate DefIndices don't correspond to
490         // anything in the AST, so they don't have a NodeId. For these cases
491         // we don't need a mapping from NodeId to DefIndex.
492         if node_id != ast::DUMMY_NODE_ID {
493             debug!("create_def_with_parent: def_index_to_node[{:?} <-> {:?}", index, node_id);
494             self.node_to_def_index.insert(node_id, index);
495         }
496
497         if expansion != Mark::root() {
498             self.expansions_that_defined.insert(index, expansion);
499         }
500
501         // The span is added if it isn't dummy
502         if !span.is_dummy() {
503             self.def_index_to_span.insert(index, span);
504         }
505
506         index
507     }
508
509     /// Initialize the `ast::NodeId` to `HirId` mapping once it has been generated during
510     /// AST to HIR lowering.
511     pub fn init_node_id_to_hir_id_mapping(&mut self,
512                                           mapping: IndexVec<ast::NodeId, hir::HirId>) {
513         assert!(self.node_to_hir_id.is_empty(),
514                 "Trying initialize NodeId -> HirId mapping twice");
515         self.node_to_hir_id = mapping;
516     }
517
518     pub fn expansion_that_defined(&self, index: DefIndex) -> Mark {
519         self.expansions_that_defined.get(&index).cloned().unwrap_or(Mark::root())
520     }
521
522     pub fn parent_module_of_macro_def(&self, mark: Mark) -> DefId {
523         self.parent_modules_of_macro_defs[&mark]
524     }
525
526     pub fn add_parent_module_of_macro_def(&mut self, mark: Mark, module: DefId) {
527         self.parent_modules_of_macro_defs.insert(mark, module);
528     }
529 }
530
531 impl DefPathData {
532     pub fn get_opt_name(&self) -> Option<InternedString> {
533         use self::DefPathData::*;
534         match *self {
535             TypeNs(name) |
536             ValueNs(name) |
537             MacroNs(name) |
538             LifetimeNs(name) |
539             GlobalMetaData(name) => Some(name),
540
541             Impl |
542             CrateRoot |
543             Misc |
544             ClosureExpr |
545             Ctor |
546             AnonConst |
547             ImplTrait => None
548         }
549     }
550
551     pub fn as_interned_str(&self) -> InternedString {
552         use self::DefPathData::*;
553         let s = match *self {
554             TypeNs(name) |
555             ValueNs(name) |
556             MacroNs(name) |
557             LifetimeNs(name) |
558             GlobalMetaData(name) => {
559                 return name
560             }
561             // Note that this does not show up in user print-outs.
562             CrateRoot => sym::double_braced_crate,
563             Impl => sym::double_braced_impl,
564             Misc => sym::double_braced_misc,
565             ClosureExpr => sym::double_braced_closure,
566             Ctor => sym::double_braced_constructor,
567             AnonConst => sym::double_braced_constant,
568             ImplTrait => sym::double_braced_opaque,
569         };
570
571         s.as_interned_str()
572     }
573
574     pub fn to_string(&self) -> String {
575         self.as_interned_str().to_string()
576     }
577 }
578
579 /// Evaluates to the number of tokens passed to it.
580 ///
581 /// Logarithmic counting: every one or two recursive expansions, the number of
582 /// tokens to count is divided by two, instead of being reduced by one.
583 /// Therefore, the recursion depth is the binary logarithm of the number of
584 /// tokens to count, and the expanded tree is likewise very small.
585 macro_rules! count {
586     ()                     => (0usize);
587     ($one:tt)              => (1usize);
588     ($($pairs:tt $_p:tt)*) => (count!($($pairs)*) << 1usize);
589     ($odd:tt $($rest:tt)*) => (count!($($rest)*) | 1usize);
590 }
591
592 // We define the GlobalMetaDataKind enum with this macro because we want to
593 // make sure that we exhaustively iterate over all variants when registering
594 // the corresponding DefIndices in the DefTable.
595 macro_rules! define_global_metadata_kind {
596     (pub enum GlobalMetaDataKind {
597         $($variant:ident),*
598     }) => (
599         #[derive(Clone, Copy, Debug, Hash, RustcEncodable, RustcDecodable)]
600         pub enum GlobalMetaDataKind {
601             $($variant),*
602         }
603
604         pub const FIRST_FREE_DEF_INDEX: usize = 1 + count!($($variant)*);
605
606         impl GlobalMetaDataKind {
607             fn allocate_def_indices(definitions: &mut Definitions) {
608                 $({
609                     let instance = GlobalMetaDataKind::$variant;
610                     definitions.create_def_with_parent(
611                         CRATE_DEF_INDEX,
612                         ast::DUMMY_NODE_ID,
613                         DefPathData::GlobalMetaData(instance.name().as_interned_str()),
614                         Mark::root(),
615                         DUMMY_SP
616                     );
617
618                     // Make sure calling def_index does not crash.
619                     instance.def_index(&definitions.table);
620                 })*
621             }
622
623             pub fn def_index(&self, def_path_table: &DefPathTable) -> DefIndex {
624                 let def_key = DefKey {
625                     parent: Some(CRATE_DEF_INDEX),
626                     disambiguated_data: DisambiguatedDefPathData {
627                         data: DefPathData::GlobalMetaData(self.name().as_interned_str()),
628                         disambiguator: 0,
629                     }
630                 };
631
632                 // These DefKeys are all right after the root,
633                 // so a linear search is fine.
634                 let index = def_path_table.index_to_key
635                                           .iter()
636                                           .position(|k| *k == def_key)
637                                           .unwrap();
638
639                 DefIndex::from(index)
640             }
641
642             fn name(&self) -> Symbol {
643
644                 let string = match *self {
645                     $(
646                         GlobalMetaDataKind::$variant => {
647                             concat!("{{GlobalMetaData::", stringify!($variant), "}}")
648                         }
649                     )*
650                 };
651
652                 Symbol::intern(string)
653             }
654         }
655     )
656 }
657
658 define_global_metadata_kind!(pub enum GlobalMetaDataKind {
659     Krate,
660     CrateDeps,
661     DylibDependencyFormats,
662     LangItems,
663     LangItemsMissing,
664     NativeLibraries,
665     SourceMap,
666     Impls,
667     ExportedSymbols
668 });