Skip to content

Commit 14133d3

Browse files
committed
trans: move exported_name's logic into symbol_names.
1 parent 3918474 commit 14133d3

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
@@ -2437,47 +2437,6 @@ pub fn create_entry_wrapper(ccx: &CrateContext, sp: Span, main_llfn: ValueRef) {
24372437
}
24382438
}
24392439

2440-
pub fn exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
2441-
instance: Instance<'tcx>,
2442-
attrs: &[ast::Attribute])
2443-
-> String {
2444-
let id = ccx.tcx().map.as_local_node_id(instance.def).unwrap();
2445-
2446-
if ccx.sess().plugin_registrar_fn.get() == Some(id) {
2447-
let svh = &ccx.link_meta().crate_hash;
2448-
let idx = instance.def.index;
2449-
return ccx.sess().generate_plugin_registrar_symbol(svh, idx);
2450-
}
2451-
2452-
match ccx.external_srcs().borrow().get(&id) {
2453-
Some(&did) => {
2454-
let sym = ccx.sess().cstore.item_symbol(did);
2455-
debug!("found item {} in other crate...", sym);
2456-
return sym;
2457-
}
2458-
None => {}
2459-
}
2460-
2461-
match attr::find_export_name_attr(ccx.sess().diagnostic(), attrs) {
2462-
// Use provided name
2463-
Some(name) => name.to_string(),
2464-
_ => {
2465-
if attr::contains_name(attrs, "no_mangle") {
2466-
// Don't mangle
2467-
ccx.tcx().map.name(id).as_str().to_string()
2468-
} else {
2469-
match weak_lang_items::link_name(attrs) {
2470-
Some(name) => name.to_string(),
2471-
None => {
2472-
// Usual name mangling
2473-
symbol_names::exported_name(ccx, &instance)
2474-
}
2475-
}
2476-
}
2477-
}
2478-
}
2479-
}
2480-
24812440
pub fn imported_name(name: ast::Name, attrs: &[ast::Attribute]) -> InternedString {
24822441
match attr::first_attr_value_str_by_name(attrs, "link_name") {
24832442
Some(ln) => ln.clone(),
@@ -2840,7 +2799,8 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
28402799
reachable_symbols.extend(syms.into_iter().filter(|did| {
28412800
sess.cstore.is_extern_item(shared_ccx.tcx(), *did)
28422801
}).map(|did| {
2843-
sess.cstore.item_symbol(did)
2802+
let instance = Instance::mono(shared_ccx.tcx(), did);
2803+
symbol_names::exported_name(&shared_ccx, instance)
28442804
}));
28452805
}
28462806

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;
2526
use trans_item::TransItem;
@@ -1021,13 +1022,13 @@ pub fn get_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, def_id: DefId)
10211022
let llty = type_of::type_of(ccx, ty);
10221023
match ccx.tcx().map.get(id) {
10231024
hir_map::NodeItem(&hir::Item {
1024-
ref attrs, span, node: hir::ItemStatic(..), ..
1025+
span, node: hir::ItemStatic(..), ..
10251026
}) => {
10261027
// If this static came from an external crate, then
10271028
// we need to get the symbol from metadata instead of
10281029
// using the current crate's name/version
10291030
// information in the hash of the symbol
1030-
let sym = exported_name(ccx, instance, attrs);
1031+
let sym = symbol_names::exported_name(ccx.shared(), instance);
10311032
debug!("making {}", sym);
10321033

10331034
// Create the global before evaluating the initializer;
@@ -1104,7 +1105,7 @@ pub fn get_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, def_id: DefId)
11041105
} else {
11051106
// FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
11061107
// FIXME(nagisa): investigate whether it can be changed into define_global
1107-
let name = ccx.sess().cstore.item_symbol(def_id);
1108+
let name = symbol_names::exported_name(ccx.shared(), instance);
11081109
let g = declare::declare_global(ccx, &name, type_of::type_of(ccx, ty));
11091110
// Thread-local statics in some other crate need to *always* be linked
11101111
// 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
@@ -504,6 +504,10 @@ impl<'b, 'tcx> SharedCrateContext<'b, 'tcx> {
504504
scheme.generics.regions.map(|_| ty::ReStatic)))
505505
}
506506

507+
pub fn symbol_hasher(&self) -> &RefCell<Sha256> {
508+
&self.symbol_hasher
509+
}
510+
507511
pub fn metadata_symbol_name(&self) -> String {
508512
format!("rust_metadata_{}_{}",
509513
self.link_meta().crate_name,

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(self.ccx.shared(), 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)