]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/symbol_map.rs
Auto merge of #41437 - cuviper:remove-unstable-deprecated, r=alexcrichton
[rust.git] / src / librustc_trans / symbol_map.rs
1 // Copyright 2016 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 use context::SharedCrateContext;
12 use monomorphize::Instance;
13 use rustc::ty::TyCtxt;
14 use std::borrow::Cow;
15 use syntax::codemap::Span;
16 use trans_item::TransItem;
17 use util::nodemap::FxHashMap;
18
19 // In the SymbolMap we collect the symbol names of all translation items of
20 // the current crate. This map exists as a performance optimization. Symbol
21 // names of translation items are deterministic and fully defined by the item.
22 // Thus they could also always be recomputed if needed.
23
24 pub struct SymbolMap<'tcx> {
25     index: FxHashMap<TransItem<'tcx>, (usize, usize)>,
26     arena: String,
27 }
28
29 impl<'tcx> SymbolMap<'tcx> {
30
31     pub fn build<'a, I>(scx: &SharedCrateContext<'a, 'tcx>,
32                         trans_items: I)
33                         -> SymbolMap<'tcx>
34         where I: Iterator<Item=TransItem<'tcx>>
35     {
36         // Check for duplicate symbol names
37         let tcx = scx.tcx();
38         let mut symbols: Vec<_> = trans_items.map(|trans_item| {
39             (trans_item, trans_item.compute_symbol_name(tcx))
40         }).collect();
41
42         (&mut symbols[..]).sort_by(|&(_, ref sym1), &(_, ref sym2)|{
43             sym1.cmp(sym2)
44         });
45
46         for pair in (&symbols[..]).windows(2) {
47             let sym1 = &pair[0].1;
48             let sym2 = &pair[1].1;
49
50             if *sym1 == *sym2 {
51                 let trans_item1 = pair[0].0;
52                 let trans_item2 = pair[1].0;
53
54                 let span1 = get_span(scx.tcx(), trans_item1);
55                 let span2 = get_span(scx.tcx(), trans_item2);
56
57                 // Deterministically select one of the spans for error reporting
58                 let span = match (span1, span2) {
59                     (Some(span1), Some(span2)) => {
60                         Some(if span1.lo.0 > span2.lo.0 {
61                             span1
62                         } else {
63                             span2
64                         })
65                     }
66                     (Some(span), None) |
67                     (None, Some(span)) => Some(span),
68                     _ => None
69                 };
70
71                 let error_message = format!("symbol `{}` is already defined", sym1);
72
73                 if let Some(span) = span {
74                     scx.sess().span_fatal(span, &error_message)
75                 } else {
76                     scx.sess().fatal(&error_message)
77                 }
78             }
79         }
80
81         let mut symbol_map = SymbolMap {
82             index: FxHashMap(),
83             arena: String::with_capacity(1024),
84         };
85
86         for (trans_item, symbol) in symbols {
87             let start_index = symbol_map.arena.len();
88             symbol_map.arena.push_str(&symbol[..]);
89             let end_index = symbol_map.arena.len();
90             let prev_entry = symbol_map.index.insert(trans_item,
91                                                      (start_index, end_index));
92             if prev_entry.is_some() {
93                 bug!("TransItem encountered twice?")
94             }
95         }
96
97         fn get_span<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
98                               trans_item: TransItem<'tcx>) -> Option<Span> {
99             match trans_item {
100                 TransItem::Fn(Instance { def, .. }) => {
101                     tcx.hir.as_local_node_id(def.def_id())
102                 }
103                 TransItem::Static(node_id) |
104                 TransItem::GlobalAsm(node_id) => {
105                     Some(node_id)
106                 }
107             }.map(|node_id| {
108                 tcx.hir.span(node_id)
109             })
110         }
111
112         symbol_map
113     }
114
115     pub fn get(&self, trans_item: TransItem<'tcx>) -> Option<&str> {
116         self.index.get(&trans_item).map(|&(start_index, end_index)| {
117             &self.arena[start_index .. end_index]
118         })
119     }
120
121     pub fn get_or_compute<'map, 'scx>(&'map self,
122                                       scx: &SharedCrateContext<'scx, 'tcx>,
123                                       trans_item: TransItem<'tcx>)
124                                       -> Cow<'map, str> {
125         if let Some(sym) = self.get(trans_item) {
126             Cow::from(sym)
127         } else {
128             Cow::from(trans_item.compute_symbol_name(scx.tcx()))
129         }
130     }
131 }