Skip to content

Commit c446cb0

Browse files
committed
just take tcx where we can
The more we can things dependent on just tcx, the easier it will be to make queries etc later on.
1 parent 39a58c3 commit c446cb0

9 files changed

+62
-66
lines changed

src/librustc_trans/back/symbol_export.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl ExportedSymbols {
5353
scx.tcx().hir.local_def_id(node_id)
5454
})
5555
.map(|def_id| {
56-
let name = symbol_for_def_id(scx, def_id, symbol_map);
56+
let name = symbol_for_def_id(scx.tcx(), def_id, symbol_map);
5757
let export_level = export_level(scx, def_id);
5858
debug!("EXPORTED SYMBOL (local): {} ({:?})", name, export_level);
5959
(name, export_level)
@@ -108,7 +108,7 @@ impl ExportedSymbols {
108108
.exported_symbols(cnum)
109109
.iter()
110110
.map(|&def_id| {
111-
let name = symbol_name(Instance::mono(scx.tcx(), def_id), scx);
111+
let name = symbol_name(Instance::mono(scx.tcx(), def_id), scx.tcx());
112112
let export_level = if special_runtime_crate {
113113
// We can probably do better here by just ensuring that
114114
// it has hidden visibility rather than public
@@ -214,21 +214,21 @@ pub fn is_below_threshold(level: SymbolExportLevel,
214214
}
215215
}
216216

217-
fn symbol_for_def_id<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
217+
fn symbol_for_def_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
218218
def_id: DefId,
219219
symbol_map: &SymbolMap<'tcx>)
220220
-> String {
221221
// Just try to look things up in the symbol map. If nothing's there, we
222222
// recompute.
223-
if let Some(node_id) = scx.tcx().hir.as_local_node_id(def_id) {
223+
if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
224224
if let Some(sym) = symbol_map.get(TransItem::Static(node_id)) {
225225
return sym.to_owned();
226226
}
227227
}
228228

229-
let instance = Instance::mono(scx.tcx(), def_id);
229+
let instance = Instance::mono(tcx, def_id);
230230

231231
symbol_map.get(TransItem::Fn(instance))
232232
.map(str::to_owned)
233-
.unwrap_or_else(|| symbol_name(instance, scx))
233+
.unwrap_or_else(|| symbol_name(instance, tcx))
234234
}

src/librustc_trans/back/symbol_names.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,12 @@
9797
//! virtually impossible. Thus, symbol hash generation exclusively relies on
9898
//! DefPaths which are much more robust in the face of changes to the code base.
9999
100-
use common::SharedCrateContext;
101100
use monomorphize::Instance;
102101

103102
use rustc::middle::weak_lang_items;
104103
use rustc::hir::def_id::DefId;
105104
use rustc::hir::map as hir_map;
106-
use rustc::ty::{self, Ty, TypeFoldable};
105+
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
107106
use rustc::ty::fold::TypeVisitor;
108107
use rustc::ty::item_path::{self, ItemPathBuffer, RootMode};
109108
use rustc::ty::subst::Substs;
@@ -113,7 +112,7 @@ use rustc::util::common::record_time;
113112
use syntax::attr;
114113
use syntax::symbol::{Symbol, InternedString};
115114

116-
fn get_symbol_hash<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
115+
fn get_symbol_hash<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
117116

118117
// the DefId of the item this name is for
119118
def_id: Option<DefId>,
@@ -130,8 +129,6 @@ fn get_symbol_hash<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
130129
-> String {
131130
debug!("get_symbol_hash(def_id={:?}, parameters={:?})", def_id, substs);
132131

133-
let tcx = scx.tcx();
134-
135132
let mut hasher = ty::util::TypeIdHasher::<u64>::new(tcx);
136133

137134
record_time(&tcx.sess.perf_stats.symbol_hash_time, || {
@@ -157,8 +154,8 @@ fn get_symbol_hash<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
157154
// in case the same instances is emitted in two crates of the same
158155
// project.
159156
if substs.types().next().is_some() {
160-
hasher.hash(scx.tcx().crate_name.as_str());
161-
hasher.hash(scx.sess().local_crate_disambiguator().as_str());
157+
hasher.hash(tcx.crate_name.as_str());
158+
hasher.hash(tcx.sess.local_crate_disambiguator().as_str());
162159
}
163160
}
164161
});
@@ -168,37 +165,37 @@ fn get_symbol_hash<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
168165
}
169166

170167
pub fn symbol_name<'a, 'tcx>(instance: Instance<'tcx>,
171-
scx: &SharedCrateContext<'a, 'tcx>) -> String {
168+
tcx: TyCtxt<'a, 'tcx, 'tcx>) -> String {
172169
let def_id = instance.def_id();
173170
let substs = instance.substs;
174171

175172
debug!("symbol_name(def_id={:?}, substs={:?})",
176173
def_id, substs);
177174

178-
let node_id = scx.tcx().hir.as_local_node_id(def_id);
175+
let node_id = tcx.hir.as_local_node_id(def_id);
179176

180177
if let Some(id) = node_id {
181-
if scx.sess().plugin_registrar_fn.get() == Some(id) {
178+
if tcx.sess.plugin_registrar_fn.get() == Some(id) {
182179
let idx = def_id.index;
183-
let disambiguator = scx.sess().local_crate_disambiguator();
184-
return scx.sess().generate_plugin_registrar_symbol(disambiguator, idx);
180+
let disambiguator = tcx.sess.local_crate_disambiguator();
181+
return tcx.sess.generate_plugin_registrar_symbol(disambiguator, idx);
185182
}
186-
if scx.sess().derive_registrar_fn.get() == Some(id) {
183+
if tcx.sess.derive_registrar_fn.get() == Some(id) {
187184
let idx = def_id.index;
188-
let disambiguator = scx.sess().local_crate_disambiguator();
189-
return scx.sess().generate_derive_registrar_symbol(disambiguator, idx);
185+
let disambiguator = tcx.sess.local_crate_disambiguator();
186+
return tcx.sess.generate_derive_registrar_symbol(disambiguator, idx);
190187
}
191188
}
192189

193190
// FIXME(eddyb) Precompute a custom symbol name based on attributes.
194-
let attrs = scx.tcx().get_attrs(def_id);
191+
let attrs = tcx.get_attrs(def_id);
195192
let is_foreign = if let Some(id) = node_id {
196-
match scx.tcx().hir.get(id) {
193+
match tcx.hir.get(id) {
197194
hir_map::NodeForeignItem(_) => true,
198195
_ => false
199196
}
200197
} else {
201-
scx.sess().cstore.is_foreign_item(def_id)
198+
tcx.sess.cstore.is_foreign_item(def_id)
202199
};
203200

204201
if let Some(name) = weak_lang_items::link_name(&attrs) {
@@ -210,17 +207,17 @@ pub fn symbol_name<'a, 'tcx>(instance: Instance<'tcx>,
210207
return name.to_string();
211208
}
212209
// Don't mangle foreign items.
213-
return scx.tcx().item_name(def_id).as_str().to_string();
210+
return tcx.item_name(def_id).as_str().to_string();
214211
}
215212

216-
if let Some(name) = attr::find_export_name_attr(scx.sess().diagnostic(), &attrs) {
213+
if let Some(name) = attr::find_export_name_attr(tcx.sess.diagnostic(), &attrs) {
217214
// Use provided name
218215
return name.to_string();
219216
}
220217

221218
if attr::contains_name(&attrs, "no_mangle") {
222219
// Don't mangle
223-
return scx.tcx().item_name(def_id).as_str().to_string();
220+
return tcx.item_name(def_id).as_str().to_string();
224221
}
225222

226223
// We want to compute the "type" of this item. Unfortunately, some
@@ -230,11 +227,11 @@ pub fn symbol_name<'a, 'tcx>(instance: Instance<'tcx>,
230227
let mut ty_def_id = def_id;
231228
let instance_ty;
232229
loop {
233-
let key = scx.tcx().def_key(ty_def_id);
230+
let key = tcx.def_key(ty_def_id);
234231
match key.disambiguated_data.data {
235232
DefPathData::TypeNs(_) |
236233
DefPathData::ValueNs(_) => {
237-
instance_ty = scx.tcx().item_type(ty_def_id);
234+
instance_ty = tcx.item_type(ty_def_id);
238235
break;
239236
}
240237
_ => {
@@ -251,16 +248,16 @@ pub fn symbol_name<'a, 'tcx>(instance: Instance<'tcx>,
251248

252249
// Erase regions because they may not be deterministic when hashed
253250
// and should not matter anyhow.
254-
let instance_ty = scx.tcx().erase_regions(&instance_ty);
251+
let instance_ty = tcx.erase_regions(&instance_ty);
255252

256-
let hash = get_symbol_hash(scx, Some(def_id), instance_ty, Some(substs));
253+
let hash = get_symbol_hash(tcx, Some(def_id), instance_ty, Some(substs));
257254

258255
let mut buffer = SymbolPathBuffer {
259256
names: Vec::new()
260257
};
261258

262259
item_path::with_forced_absolute_paths(|| {
263-
scx.tcx().push_item_path(&mut buffer, def_id);
260+
tcx.push_item_path(&mut buffer, def_id);
264261
});
265262

266263
mangle(buffer.names.into_iter(), &hash)
@@ -281,11 +278,11 @@ impl ItemPathBuffer for SymbolPathBuffer {
281278
}
282279
}
283280

284-
pub fn exported_name_from_type_and_prefix<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
281+
pub fn exported_name_from_type_and_prefix<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
285282
t: Ty<'tcx>,
286283
prefix: &str)
287284
-> String {
288-
let hash = get_symbol_hash(scx, None, t, None);
285+
let hash = get_symbol_hash(tcx, None, t, None);
289286
let path = [Symbol::intern(prefix).as_str()];
290287
mangle(path.iter().cloned(), &hash)
291288
}

src/librustc_trans/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11391139

11401140
let cgu_name = String::from(cgu.name());
11411141
let cgu_id = cgu.work_product_id();
1142-
let symbol_cache = SymbolCache::new(scx);
1142+
let symbol_cache = SymbolCache::new(scx.tcx());
11431143
let symbol_name_hash = cgu.compute_symbol_name_hash(scx, &symbol_cache);
11441144

11451145
// Check whether there is a previous work-product we can
@@ -1239,7 +1239,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12391239

12401240
assert_module_sources::assert_module_sources(tcx, &modules);
12411241

1242-
symbol_names_test::report_symbol_names(&shared_ccx);
1242+
symbol_names_test::report_symbol_names(tcx);
12431243

12441244
if shared_ccx.sess().trans_stats() {
12451245
println!("--- trans stats ---");

src/librustc_trans/consts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub fn get_static(ccx: &CrateContext, def_id: DefId) -> ValueRef {
113113
hir_map::NodeForeignItem(&hir::ForeignItem {
114114
ref attrs, span, node: hir::ForeignItemStatic(..), ..
115115
}) => {
116-
let sym = symbol_names::symbol_name(instance, ccx.shared());
116+
let sym = symbol_names::symbol_name(instance, ccx.tcx());
117117
let g = if let Some(name) =
118118
attr::first_attr_value_str_by_name(&attrs, "linkage") {
119119
// If this is a static with a linkage specified, then we need to handle
@@ -173,7 +173,7 @@ pub fn get_static(ccx: &CrateContext, def_id: DefId) -> ValueRef {
173173

174174
g
175175
} else {
176-
let sym = symbol_names::symbol_name(instance, ccx.shared());
176+
let sym = symbol_names::symbol_name(instance, ccx.tcx());
177177

178178
// FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
179179
// FIXME(nagisa): investigate whether it can be changed into define_global

src/librustc_trans/partitioning.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<'tcx> CodegenUnit<'tcx> {
177177
pub fn compute_symbol_name_hash<'a>(&self,
178178
scx: &SharedCrateContext<'a, 'tcx>,
179179
symbol_cache: &SymbolCache<'a, 'tcx>)
180-
-> u64 {
180+
-> u64 {
181181
let mut state = IchHasher::new();
182182
let exported_symbols = scx.exported_symbols();
183183
let all_items = self.items_in_deterministic_order(scx.tcx(), symbol_cache);
@@ -272,14 +272,14 @@ pub fn partition<'a, 'tcx, I>(scx: &SharedCrateContext<'a, 'tcx>,
272272
let mut initial_partitioning = place_root_translation_items(scx,
273273
trans_items);
274274

275-
debug_dump(scx, "INITIAL PARTITONING:", initial_partitioning.codegen_units.iter());
275+
debug_dump(tcx, "INITIAL PARTITONING:", initial_partitioning.codegen_units.iter());
276276

277277
// If the partitioning should produce a fixed count of codegen units, merge
278278
// until that count is reached.
279279
if let PartitioningStrategy::FixedUnitCount(count) = strategy {
280280
merge_codegen_units(&mut initial_partitioning, count, &tcx.crate_name.as_str());
281281

282-
debug_dump(scx, "POST MERGING:", initial_partitioning.codegen_units.iter());
282+
debug_dump(tcx, "POST MERGING:", initial_partitioning.codegen_units.iter());
283283
}
284284

285285
// In the next step, we use the inlining map to determine which addtional
@@ -289,7 +289,7 @@ pub fn partition<'a, 'tcx, I>(scx: &SharedCrateContext<'a, 'tcx>,
289289
let post_inlining = place_inlined_translation_items(initial_partitioning,
290290
inlining_map);
291291

292-
debug_dump(scx, "POST INLINING:", post_inlining.0.iter());
292+
debug_dump(tcx, "POST INLINING:", post_inlining.0.iter());
293293

294294
// Finally, sort by codegen unit name, so that we get deterministic results
295295
let mut result = post_inlining.0;
@@ -529,15 +529,15 @@ fn numbered_codegen_unit_name(crate_name: &str, index: usize) -> InternedString
529529
Symbol::intern(&format!("{}{}{}", crate_name, NUMBERED_CODEGEN_UNIT_MARKER, index)).as_str()
530530
}
531531

532-
fn debug_dump<'a, 'b, 'tcx, I>(scx: &SharedCrateContext<'a, 'tcx>,
532+
fn debug_dump<'a, 'b, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
533533
label: &str,
534534
cgus: I)
535535
where I: Iterator<Item=&'b CodegenUnit<'tcx>>,
536536
'tcx: 'a + 'b
537537
{
538538
if cfg!(debug_assertions) {
539539
debug!("{}", label);
540-
let symbol_cache = SymbolCache::new(scx);
540+
let symbol_cache = SymbolCache::new(tcx);
541541
for cgu in cgus {
542542
debug!("CodegenUnit {}:", cgu.name);
543543

@@ -548,7 +548,7 @@ fn debug_dump<'a, 'b, 'tcx, I>(scx: &SharedCrateContext<'a, 'tcx>,
548548
.unwrap_or("<no hash>");
549549

550550
debug!(" - {} [{:?}] [{}]",
551-
trans_item.to_string(scx.tcx()),
551+
trans_item.to_string(tcx),
552552
linkage,
553553
symbol_hash);
554554
}

src/librustc_trans/symbol_cache.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use context::SharedCrateContext;
1211
use std::cell::RefCell;
1312
use std::rc::Rc;
13+
use rustc::ty::TyCtxt;
1414
use trans_item::TransItem;
1515
use util::nodemap::FxHashMap;
1616

@@ -21,22 +21,22 @@ use util::nodemap::FxHashMap;
2121
// Thus they can always be recomputed if needed.
2222

2323
pub struct SymbolCache<'a, 'tcx: 'a> {
24-
scx: &'a SharedCrateContext<'a, 'tcx>,
24+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
2525
index: RefCell<FxHashMap<TransItem<'tcx>, Rc<String>>>,
2626
}
2727

2828
impl<'a, 'tcx> SymbolCache<'a, 'tcx> {
29-
pub fn new(scx: &'a SharedCrateContext<'a, 'tcx>) -> Self {
29+
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
3030
SymbolCache {
31-
scx,
31+
tcx: tcx,
3232
index: RefCell::new(FxHashMap())
3333
}
3434
}
3535

3636
pub fn get(&self, trans_item: TransItem<'tcx>) -> Rc<String> {
3737
let mut index = self.index.borrow_mut();
3838
index.entry(trans_item)
39-
.or_insert_with(|| Rc::new(trans_item.compute_symbol_name(self.scx)))
39+
.or_insert_with(|| Rc::new(trans_item.compute_symbol_name(self.tcx)))
4040
.clone()
4141
}
4242
}

src/librustc_trans/symbol_map.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ impl<'tcx> SymbolMap<'tcx> {
3434
where I: Iterator<Item=TransItem<'tcx>>
3535
{
3636
// Check for duplicate symbol names
37+
let tcx = scx.tcx();
3738
let mut symbols: Vec<_> = trans_items.map(|trans_item| {
38-
(trans_item, trans_item.compute_symbol_name(scx))
39+
(trans_item, trans_item.compute_symbol_name(tcx))
3940
}).collect();
4041

4142
(&mut symbols[..]).sort_by(|&(_, ref sym1), &(_, ref sym2)|{
@@ -124,7 +125,7 @@ impl<'tcx> SymbolMap<'tcx> {
124125
if let Some(sym) = self.get(trans_item) {
125126
Cow::from(sym)
126127
} else {
127-
Cow::from(trans_item.compute_symbol_name(scx))
128+
Cow::from(trans_item.compute_symbol_name(scx.tcx()))
128129
}
129130
}
130131
}

0 commit comments

Comments
 (0)