]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/symbol_cache.rs
Rollup merge of #41456 - jessicah:haiku-support, r=alexcrichton
[rust.git] / src / librustc_trans / symbol_cache.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 rustc::ty::TyCtxt;
12 use std::cell::RefCell;
13 use syntax_pos::symbol::{InternedString, Symbol};
14 use trans_item::TransItem;
15 use util::nodemap::FxHashMap;
16
17 // In the SymbolCache we collect the symbol names of translation items
18 // and cache them for later reference. This is just a performance
19 // optimization and the cache is populated lazilly; symbol names of
20 // translation items are deterministic and fully defined by the item.
21 // Thus they can always be recomputed if needed.
22
23 pub struct SymbolCache<'a, 'tcx: 'a> {
24     tcx: TyCtxt<'a, 'tcx, 'tcx>,
25     index: RefCell<FxHashMap<TransItem<'tcx>, Symbol>>,
26 }
27
28 impl<'a, 'tcx> SymbolCache<'a, 'tcx> {
29     pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
30         SymbolCache {
31             tcx: tcx,
32             index: RefCell::new(FxHashMap())
33         }
34     }
35
36     pub fn get(&self, trans_item: TransItem<'tcx>) -> InternedString {
37         let mut index = self.index.borrow_mut();
38         index.entry(trans_item)
39              .or_insert_with(|| Symbol::intern(&trans_item.compute_symbol_name(self.tcx)))
40              .as_str()
41     }
42 }