Skip to content

Commit 06e00c4

Browse files
committed
trans: move exported_name's logic into symbol_names.
1 parent fc0872a commit 06e00c4

File tree

8 files changed

+62
-70
lines changed

8 files changed

+62
-70
lines changed

src/librustc_trans/back/symbol_names.rs

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,18 @@
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::{CrateContext, gensym_name};
100+
use common::{CrateContext, SharedCrateContext, gensym_name};
101101
use monomorphize::Instance;
102102
use util::sha2::{Digest, Sha256};
103103

104-
use rustc::middle::cstore;
104+
use rustc::middle::{cstore, weak_lang_items};
105105
use rustc::hir::def_id::DefId;
106106
use rustc::ty::{self, TyCtxt, TypeFoldable};
107-
use rustc::ty::item_path::{ItemPathBuffer, RootMode};
107+
use rustc::ty::item_path::{self, ItemPathBuffer, RootMode};
108108
use rustc::hir::map::definitions::{DefPath, DefPathData};
109109

110110
use std::fmt::Write;
111+
use syntax::attr;
111112
use syntax::parse::token::{self, InternedString};
112113
use serialize::hex::ToHex;
113114

@@ -134,7 +135,7 @@ fn def_path_to_string<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_path: &DefPath)
134135
s
135136
}
136137

137-
fn get_symbol_hash<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
138+
fn get_symbol_hash<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
138139

139140
// path to the item this name is for
140141
def_path: &DefPath,
@@ -152,9 +153,9 @@ fn get_symbol_hash<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
152153
debug!("get_symbol_hash(def_path={:?}, parameters={:?})",
153154
def_path, parameters);
154155

155-
let tcx = ccx.tcx();
156+
let tcx = scx.tcx();
156157

157-
let mut hash_state = ccx.symbol_hasher().borrow_mut();
158+
let mut hash_state = scx.symbol_hasher().borrow_mut();
158159

159160
hash_state.reset();
160161

@@ -187,22 +188,47 @@ fn get_symbol_hash<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
187188
}
188189
}
189190

190-
pub fn exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
191-
instance: &Instance<'tcx>)
191+
pub fn exported_name<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
192+
instance: Instance<'tcx>)
192193
-> String {
193-
let &Instance { def: mut def_id, ref substs } = instance;
194+
let Instance { def: def_id, ref substs } = instance;
194195

195196
debug!("exported_name(def_id={:?}, substs={:?})",
196197
def_id, substs);
197198

198-
if let Some(node_id) = ccx.tcx().map.as_local_node_id(def_id) {
199-
if let Some(&src_def_id) = ccx.external_srcs().borrow().get(&node_id) {
200-
def_id = src_def_id;
199+
let node_id = scx.tcx().map.as_local_node_id(instance.def);
200+
201+
if let Some(id) = node_id {
202+
if scx.sess().plugin_registrar_fn.get() == Some(id) {
203+
let svh = &scx.link_meta().crate_hash;
204+
let idx = instance.def.index;
205+
return scx.sess().generate_plugin_registrar_symbol(svh, idx);
201206
}
202207
}
203208

204-
let def_path = ccx.tcx().def_path(def_id);
205-
assert_eq!(def_path.krate, def_id.krate);
209+
// FIXME(eddyb) Precompute a custom symbol name based on attributes.
210+
let attrs;
211+
let attrs = if let Some(id) = node_id {
212+
scx.tcx().map.attrs(id)
213+
} else {
214+
attrs = scx.sess().cstore.item_attrs(def_id);
215+
&attrs[..]
216+
};
217+
218+
if let Some(name) = attr::find_export_name_attr(scx.sess().diagnostic(), attrs) {
219+
// Use provided name
220+
return name.to_string();
221+
}
222+
223+
if attr::contains_name(attrs, "no_mangle") {
224+
// Don't mangle
225+
return scx.tcx().item_name(instance.def).as_str().to_string()
226+
}
227+
if let Some(name) = weak_lang_items::link_name(attrs) {
228+
return name.to_string();
229+
}
230+
231+
let def_path = scx.tcx().def_path(def_id);
206232

207233
// We want to compute the "type" of this item. Unfortunately, some
208234
// kinds of items (e.g., closures) don't have an entry in the
@@ -211,11 +237,11 @@ pub fn exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
211237
let mut ty_def_id = def_id;
212238
let instance_ty;
213239
loop {
214-
let key = ccx.tcx().def_key(ty_def_id);
240+
let key = scx.tcx().def_key(ty_def_id);
215241
match key.disambiguated_data.data {
216242
DefPathData::TypeNs(_) |
217243
DefPathData::ValueNs(_) => {
218-
instance_ty = ccx.tcx().lookup_item_type(ty_def_id);
244+
instance_ty = scx.tcx().lookup_item_type(ty_def_id);
219245
break;
220246
}
221247
_ => {
@@ -232,9 +258,9 @@ pub fn exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
232258

233259
// Erase regions because they may not be deterministic when hashed
234260
// and should not matter anyhow.
235-
let instance_ty = ccx.tcx().erase_regions(&instance_ty.ty);
261+
let instance_ty = scx.tcx().erase_regions(&instance_ty.ty);
236262

237-
let hash = get_symbol_hash(ccx, &def_path, instance_ty, substs.types.as_slice());
263+
let hash = get_symbol_hash(scx, &def_path, instance_ty, substs.types.as_slice());
238264

239265
let mut buffer = SymbolPathBuffer {
240266
names: Vec::with_capacity(def_path.data.len())
@@ -271,7 +297,7 @@ pub fn internal_name_from_type_and_suffix<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>
271297
data: vec![],
272298
krate: cstore::LOCAL_CRATE,
273299
};
274-
let hash = get_symbol_hash(ccx, &def_path, t, &[]);
300+
let hash = get_symbol_hash(ccx.shared(), &def_path, t, &[]);
275301
mangle(path.iter().cloned(), Some(&hash[..]))
276302
}
277303

src/librustc_trans/base.rs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,47 +2429,6 @@ pub fn create_entry_wrapper(ccx: &CrateContext, sp: Span, main_llfn: ValueRef) {
24292429
}
24302430
}
24312431

2432-
pub fn exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
2433-
instance: Instance<'tcx>,
2434-
attrs: &[ast::Attribute])
2435-
-> String {
2436-
let id = ccx.tcx().map.as_local_node_id(instance.def).unwrap();
2437-
2438-
if ccx.sess().plugin_registrar_fn.get() == Some(id) {
2439-
let svh = &ccx.link_meta().crate_hash;
2440-
let idx = instance.def.index;
2441-
return ccx.sess().generate_plugin_registrar_symbol(svh, idx);
2442-
}
2443-
2444-
match ccx.external_srcs().borrow().get(&id) {
2445-
Some(&did) => {
2446-
let sym = ccx.sess().cstore.item_symbol(did);
2447-
debug!("found item {} in other crate...", sym);
2448-
return sym;
2449-
}
2450-
None => {}
2451-
}
2452-
2453-
match attr::find_export_name_attr(ccx.sess().diagnostic(), attrs) {
2454-
// Use provided name
2455-
Some(name) => name.to_string(),
2456-
_ => {
2457-
if attr::contains_name(attrs, "no_mangle") {
2458-
// Don't mangle
2459-
ccx.tcx().map.name(id).as_str().to_string()
2460-
} else {
2461-
match weak_lang_items::link_name(attrs) {
2462-
Some(name) => name.to_string(),
2463-
None => {
2464-
// Usual name mangling
2465-
symbol_names::exported_name(ccx, &instance)
2466-
}
2467-
}
2468-
}
2469-
}
2470-
}
2471-
}
2472-
24732432
pub fn imported_name(name: ast::Name, attrs: &[ast::Attribute]) -> InternedString {
24742433
match attr::first_attr_value_str_by_name(attrs, "link_name") {
24752434
Some(ln) => ln.clone(),
@@ -2826,7 +2785,8 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
28262785
reachable_symbols.extend(syms.into_iter().filter(|did| {
28272786
sess.cstore.is_extern_item(shared_ccx.tcx(), *did)
28282787
}).map(|did| {
2829-
sess.cstore.item_symbol(did)
2788+
let instance = Instance::mono(shared_ccx.tcx(), did);
2789+
symbol_names::exported_name(&shared_ccx, instance)
28302790
}));
28312791
}
28322792
}

src/librustc_trans/callee.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
512512
Some(hir_map::NodeImplItem(&hir::ImplItem {
513513
ref attrs, id, span, node: hir::ImplItemKind::Method(..), ..
514514
})) => {
515-
let sym = exported_name(ccx, instance, attrs);
515+
let sym = symbol_names::exported_name(ccx.shared(), instance);
516516

517517
if declare::get_defined_value(ccx, &sym).is_some() {
518518
ccx.sess().span_fatal(span,
@@ -530,7 +530,8 @@ fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
530530

531531
None => {
532532
attrs = ccx.sess().cstore.item_attrs(def_id);
533-
(ccx.sess().cstore.item_symbol(def_id), &attrs[..], None)
533+
let sym = symbol_names::exported_name(ccx.shared(), instance);
534+
(sym, &attrs[..], None)
534535
}
535536

536537
ref variant => {

src/librustc_trans/closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ fn get_or_create_closure_declaration<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
150150
return llfn;
151151
}
152152

153-
let symbol = symbol_names::exported_name(ccx, &instance);
153+
let symbol = symbol_names::exported_name(ccx.shared(), instance);
154154

155155
// Compute the rust-call form of the closure call method.
156156
let sig = &tcx.closure_type(closure_id, substs).sig;

src/librustc_trans/consts.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use rustc::hir::def::Def;
1919
use rustc::hir::def_id::DefId;
2020
use rustc::hir::map as hir_map;
2121
use {abi, adt, closure, debuginfo, expr, machine};
22-
use base::{self, exported_name, imported_name, push_ctxt};
22+
use base::{self, imported_name, push_ctxt};
23+
use back::symbol_names;
2324
use callee::Callee;
2425
use collector::{self, TransItem};
2526
use common::{type_is_sized, C_nil, const_get_elt};
@@ -1020,13 +1021,13 @@ pub fn get_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, def_id: DefId)
10201021
let llty = type_of::type_of(ccx, ty);
10211022
match ccx.tcx().map.get(id) {
10221023
hir_map::NodeItem(&hir::Item {
1023-
ref attrs, span, node: hir::ItemStatic(..), ..
1024+
span, node: hir::ItemStatic(..), ..
10241025
}) => {
10251026
// If this static came from an external crate, then
10261027
// we need to get the symbol from metadata instead of
10271028
// using the current crate's name/version
10281029
// information in the hash of the symbol
1029-
let sym = exported_name(ccx, instance, attrs);
1030+
let sym = symbol_names::exported_name(ccx.shared(), instance);
10301031
debug!("making {}", sym);
10311032

10321033
// Create the global before evaluating the initializer;
@@ -1103,7 +1104,7 @@ pub fn get_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, def_id: DefId)
11031104
} else {
11041105
// FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
11051106
// FIXME(nagisa): investigate whether it can be changed into define_global
1106-
let name = ccx.sess().cstore.item_symbol(def_id);
1107+
let name = symbol_names::exported_name(ccx.shared(), instance);
11071108
let g = declare::declare_global(ccx, &name, type_of::type_of(ccx, ty));
11081109
// Thread-local statics in some other crate need to *always* be linked
11091110
// against in a thread-local fashion, so we need to be sure to apply the

src/librustc_trans/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,10 @@ impl<'b, 'tcx> SharedCrateContext<'b, 'tcx> {
487487
pub fn translation_items(&self) -> &RefCell<FnvHashMap<TransItem<'tcx>, TransItemState>> {
488488
&self.translation_items
489489
}
490+
491+
pub fn symbol_hasher(&self) -> &RefCell<Sha256> {
492+
&self.symbol_hasher
493+
}
490494
}
491495

492496
impl<'tcx> LocalCrateContext<'tcx> {

src/librustc_trans/monomorphize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
8888
monomorphizing.insert(fn_id, depth + 1);
8989
}
9090

91-
let symbol = symbol_names::exported_name(ccx, &instance);
91+
let symbol = symbol_names::exported_name(ccx.shared(), instance);
9292

9393
debug!("monomorphize_fn mangled to {}", symbol);
9494
assert!(declare::get_defined_value(ccx, &symbol).is_none());

src/librustc_trans/symbol_names_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'a, 'tcx> SymbolNamesTest<'a, 'tcx> {
5353
if attr.check_name(SYMBOL_NAME) {
5454
// for now, can only use on monomorphic names
5555
let instance = Instance::mono(tcx, def_id);
56-
let name = symbol_names::exported_name(self.ccx, &instance);
56+
let name = symbol_names::exported_name(self.ccx.shared(), instance);
5757
tcx.sess.span_err(attr.span, &format!("symbol-name({})", name));
5858
} else if attr.check_name(ITEM_PATH) {
5959
let path = tcx.item_path_str(def_id);

0 commit comments

Comments
 (0)