Skip to content

Commit c32b26b

Browse files
committed
librustc: Unify name mangling for functions and statics.
1 parent e823940 commit c32b26b

File tree

2 files changed

+172
-165
lines changed

2 files changed

+172
-165
lines changed

src/librustc/middle/trans/base.rs

Lines changed: 167 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,47 +2244,38 @@ pub fn trans_mod(ccx: @mut CrateContext, m: &ast::_mod) {
22442244

22452245
pub fn register_fn(ccx: @mut CrateContext,
22462246
sp: span,
2247-
path: path,
2248-
node_id: ast::node_id,
2249-
attrs: &[ast::Attribute])
2247+
sym: ~str,
2248+
node_id: ast::node_id)
22502249
-> ValueRef {
22512250
let t = ty::node_id_to_type(ccx.tcx, node_id);
2252-
register_fn_full(ccx, sp, path, node_id, attrs, t)
2251+
register_fn_full(ccx, sp, sym, node_id, t)
22532252
}
22542253

22552254
pub fn register_fn_full(ccx: @mut CrateContext,
22562255
sp: span,
2257-
path: path,
2256+
sym: ~str,
22582257
node_id: ast::node_id,
2259-
attrs: &[ast::Attribute],
22602258
node_type: ty::t)
22612259
-> ValueRef {
22622260
let llfty = type_of_fn_from_ty(ccx, node_type);
2263-
register_fn_fuller(ccx, sp, path, node_id, attrs, node_type,
2261+
register_fn_fuller(ccx, sp, sym, node_id, node_type,
22642262
lib::llvm::CCallConv, llfty)
22652263
}
22662264

22672265
pub fn register_fn_fuller(ccx: @mut CrateContext,
22682266
sp: span,
2269-
path: path,
2267+
sym: ~str,
22702268
node_id: ast::node_id,
2271-
attrs: &[ast::Attribute],
22722269
node_type: ty::t,
22732270
cc: lib::llvm::CallConv,
22742271
fn_ty: Type)
22752272
-> ValueRef {
22762273
debug!("register_fn_fuller creating fn for item %d with path %s",
22772274
node_id,
2278-
ast_map::path_to_str(path, token::get_ident_interner()));
2279-
2280-
let ps = if attr::contains_name(attrs, "no_mangle") {
2281-
path_elt_to_str(*path.last(), token::get_ident_interner())
2282-
} else {
2283-
mangle_exported_name(ccx, path, node_type)
2284-
};
2275+
ast_map::path_to_str(item_path(ccx, &node_id), token::get_ident_interner()));
22852276

2286-
let llfn = decl_fn(ccx.llmod, ps, cc, fn_ty);
2287-
ccx.item_symbols.insert(node_id, ps);
2277+
let llfn = decl_fn(ccx.llmod, sym, cc, fn_ty);
2278+
ccx.item_symbols.insert(node_id, sym);
22882279

22892280
// FIXME #4404 android JNI hacks
22902281
let is_entry = is_entry_fn(&ccx.sess, node_id) && (!*ccx.sess.building_library ||
@@ -2430,167 +2421,182 @@ pub fn fill_fn_pair(bcx: @mut Block, pair: ValueRef, llfn: ValueRef,
24302421
Store(bcx, llenvblobptr, env_cell);
24312422
}
24322423

2433-
pub fn item_path(ccx: &CrateContext, i: &ast::item) -> path {
2434-
let base = match ccx.tcx.items.get_copy(&i.id) {
2435-
ast_map::node_item(_, p) => p,
2436-
// separate map for paths?
2424+
pub fn item_path(ccx: &CrateContext, id: &ast::node_id) -> path {
2425+
match ccx.tcx.items.get_copy(id) {
2426+
ast_map::node_item(i, p) =>
2427+
vec::append((*p).clone(), [path_name(i.ident)]),
2428+
// separate map for paths?
24372429
_ => fail!("item_path")
2438-
};
2439-
vec::append((*base).clone(), [path_name(i.ident)])
2430+
}
2431+
}
2432+
2433+
fn exported_name(ccx: @mut CrateContext, path: path, ty: ty::t, attrs: &[ast::Attribute]) -> ~str {
2434+
if attr::contains_name(attrs, "no_mangle") {
2435+
path_elt_to_str(*path.last(), token::get_ident_interner())
2436+
} else {
2437+
mangle_exported_name(ccx, path, ty)
2438+
}
24402439
}
24412440

24422441
pub fn get_item_val(ccx: @mut CrateContext, id: ast::node_id) -> ValueRef {
24432442
debug!("get_item_val(id=`%?`)", id);
2443+
24442444
let val = ccx.item_vals.find_copy(&id);
24452445
match val {
2446-
Some(v) => v,
2447-
None => {
2448-
let mut exprt = false;
2449-
let item = ccx.tcx.items.get_copy(&id);
2450-
let val = match item {
2451-
ast_map::node_item(i, pth) => {
2452-
let my_path = vec::append((*pth).clone(), [path_name(i.ident)]);
2453-
let v = match i.node {
2454-
ast::item_static(_, m, expr) => {
2455-
let typ = ty::node_id_to_type(ccx.tcx, i.id);
2456-
let s =
2457-
if attr::contains_name(i.attrs, "no_mangle") {
2458-
path_elt_to_str(*my_path.last(), token::get_ident_interner())
2459-
} else {
2460-
mangle_exported_name(ccx, my_path, typ)
2461-
};
2462-
// We need the translated value here, because for enums the
2463-
// LLVM type is not fully determined by the Rust type.
2464-
let v = consts::const_expr(ccx, expr);
2465-
ccx.const_values.insert(id, v);
2466-
exprt = m == ast::m_mutbl;
2467-
unsafe {
2468-
let llty = llvm::LLVMTypeOf(v);
2469-
let g = do s.as_c_str |buf| {
2470-
llvm::LLVMAddGlobal(ccx.llmod, llty, buf)
2446+
Some(v) => v,
2447+
None => {
2448+
let mut exprt = false;
2449+
let item = ccx.tcx.items.get_copy(&id);
2450+
let val = match item {
2451+
ast_map::node_item(i, pth) => {
2452+
2453+
let my_path = vec::append((*pth).clone(), [path_name(i.ident)]);
2454+
let ty = ty::node_id_to_type(ccx.tcx, i.id);
2455+
let sym = exported_name(ccx, my_path, ty, i.attrs);
2456+
2457+
let v = match i.node {
2458+
ast::item_static(_, m, expr) => {
2459+
// We need the translated value here, because for enums the
2460+
// LLVM type is not fully determined by the Rust type.
2461+
let v = consts::const_expr(ccx, expr);
2462+
ccx.const_values.insert(id, v);
2463+
exprt = m == ast::m_mutbl;
2464+
2465+
unsafe {
2466+
let llty = llvm::LLVMTypeOf(v);
2467+
let g = do sym.as_c_str |buf| {
2468+
llvm::LLVMAddGlobal(ccx.llmod, llty, buf)
2469+
};
2470+
2471+
ccx.item_symbols.insert(i.id, sym);
2472+
g
2473+
}
2474+
}
2475+
2476+
ast::item_fn(_, purity, _, _, _) => {
2477+
let llfn = if purity != ast::extern_fn {
2478+
register_fn_full(ccx, i.span, sym, i.id, ty)
2479+
} else {
2480+
foreign::register_foreign_fn(ccx, i.span, sym, i.id)
2481+
};
2482+
set_inline_hint_if_appr(i.attrs, llfn);
2483+
llfn
2484+
}
2485+
2486+
_ => fail!("get_item_val: weird result in table")
24712487
};
2472-
ccx.item_symbols.insert(i.id, s);
2473-
g
2474-
}
2475-
}
2476-
ast::item_fn(_, purity, _, _, _) => {
2477-
let llfn = if purity != ast::extern_fn {
2478-
register_fn(ccx, i.span, my_path, i.id, i.attrs)
2479-
} else {
2480-
foreign::register_foreign_fn(ccx,
2481-
i.span,
2482-
my_path,
2483-
i.id,
2484-
i.attrs)
2485-
};
2486-
set_inline_hint_if_appr(i.attrs, llfn);
2487-
llfn
2488-
}
2489-
_ => fail!("get_item_val: weird result in table")
2490-
};
2491-
match (attr::first_attr_value_str_by_name(i.attrs, "link_section")) {
2492-
Some(sect) => unsafe {
2493-
do sect.as_c_str |buf| {
2494-
llvm::LLVMSetSection(v, buf);
2488+
2489+
match (attr::first_attr_value_str_by_name(i.attrs, "link_section")) {
2490+
Some(sect) => unsafe {
2491+
do sect.as_c_str |buf| {
2492+
llvm::LLVMSetSection(v, buf);
2493+
}
2494+
},
2495+
None => ()
24952496
}
2496-
},
2497-
None => ()
2498-
}
2499-
v
2500-
}
2501-
ast_map::node_trait_method(trait_method, _, pth) => {
2502-
debug!("get_item_val(): processing a node_trait_method");
2503-
match *trait_method {
2504-
ast::required(_) => {
2505-
ccx.sess.bug("unexpected variant: required trait method in \
2506-
get_item_val()");
2507-
}
2508-
ast::provided(m) => {
2509-
exprt = true;
2510-
register_method(ccx, id, pth, m)
2511-
}
2512-
}
2513-
}
2514-
ast_map::node_method(m, _, pth) => {
2515-
register_method(ccx, id, pth, m)
2516-
}
2517-
ast_map::node_foreign_item(ni, _, _, pth) => {
2518-
exprt = true;
2519-
match ni.node {
2520-
ast::foreign_item_fn(*) => {
2521-
register_fn(ccx, ni.span,
2522-
vec::append((*pth).clone(),
2523-
[path_name(ni.ident)]),
2524-
ni.id,
2525-
ni.attrs)
2497+
2498+
v
25262499
}
2527-
ast::foreign_item_static(*) => {
2528-
let typ = ty::node_id_to_type(ccx.tcx, ni.id);
2529-
let ident = token::ident_to_str(&ni.ident);
2530-
let g = do ident.as_c_str |buf| {
2531-
unsafe {
2532-
let ty = type_of(ccx, typ);
2533-
llvm::LLVMAddGlobal(ccx.llmod, ty.to_ref(), buf)
2500+
2501+
ast_map::node_trait_method(trait_method, _, pth) => {
2502+
debug!("get_item_val(): processing a node_trait_method");
2503+
match *trait_method {
2504+
ast::required(_) => {
2505+
ccx.sess.bug("unexpected variant: required trait method in \
2506+
get_item_val()");
25342507
}
2535-
};
2536-
g
2508+
ast::provided(m) => {
2509+
exprt = true;
2510+
register_method(ccx, id, pth, m)
2511+
}
2512+
}
25372513
}
2538-
}
2539-
}
25402514

2541-
ast_map::node_variant(ref v, enm, pth) => {
2542-
let llfn;
2543-
match v.node.kind {
2544-
ast::tuple_variant_kind(ref args) => {
2545-
assert!(args.len() != 0u);
2546-
let pth = vec::append((*pth).clone(),
2547-
[path_name(enm.ident),
2548-
path_name((*v).node.name)]);
2549-
llfn = match enm.node {
2550-
ast::item_enum(_, _) => {
2551-
register_fn(ccx, (*v).span, pth, id, enm.attrs)
2552-
}
2553-
_ => fail!("node_variant, shouldn't happen")
2554-
};
2515+
ast_map::node_method(m, _, pth) => {
2516+
register_method(ccx, id, pth, m)
25552517
}
2556-
ast::struct_variant_kind(_) => {
2557-
fail!("struct variant kind unexpected in get_item_val")
2558-
}
2559-
}
2560-
set_inline_hint(llfn);
2561-
llfn
2562-
}
25632518

2564-
ast_map::node_struct_ctor(struct_def, struct_item, struct_path) => {
2565-
// Only register the constructor if this is a tuple-like struct.
2566-
match struct_def.ctor_id {
2567-
None => {
2568-
ccx.tcx.sess.bug("attempt to register a constructor of \
2569-
a non-tuple-like struct")
2519+
ast_map::node_foreign_item(ni, _, _, pth) => {
2520+
let ty = ty::node_id_to_type(ccx.tcx, ni.id);
2521+
exprt = true;
2522+
2523+
match ni.node {
2524+
ast::foreign_item_fn(*) => {
2525+
let path = vec::append((*pth).clone(), [path_name(ni.ident)]);
2526+
let sym = exported_name(ccx, path, ty, ni.attrs);
2527+
2528+
register_fn_full(ccx, ni.span, sym, ni.id, ty)
2529+
}
2530+
ast::foreign_item_static(*) => {
2531+
let ident = token::ident_to_str(&ni.ident);
2532+
let g = do ident.as_c_str |buf| {
2533+
unsafe {
2534+
let ty = type_of(ccx, ty);
2535+
llvm::LLVMAddGlobal(ccx.llmod, ty.to_ref(), buf)
2536+
}
2537+
};
2538+
g
2539+
}
2540+
}
25702541
}
2571-
Some(ctor_id) => {
2572-
let llfn = register_fn(ccx,
2573-
struct_item.span,
2574-
(*struct_path).clone(),
2575-
ctor_id,
2576-
struct_item.attrs);
2542+
2543+
ast_map::node_variant(ref v, enm, pth) => {
2544+
let llfn;
2545+
match v.node.kind {
2546+
ast::tuple_variant_kind(ref args) => {
2547+
assert!(args.len() != 0u);
2548+
let pth = vec::append((*pth).clone(),
2549+
[path_name(enm.ident),
2550+
path_name((*v).node.name)]);
2551+
let ty = ty::node_id_to_type(ccx.tcx, id);
2552+
let sym = exported_name(ccx, pth, ty, enm.attrs);
2553+
2554+
llfn = match enm.node {
2555+
ast::item_enum(_, _) => {
2556+
register_fn_full(ccx, (*v).span, sym, id, ty)
2557+
}
2558+
_ => fail!("node_variant, shouldn't happen")
2559+
};
2560+
}
2561+
ast::struct_variant_kind(_) => {
2562+
fail!("struct variant kind unexpected in get_item_val")
2563+
}
2564+
}
25772565
set_inline_hint(llfn);
25782566
llfn
25792567
}
2568+
2569+
ast_map::node_struct_ctor(struct_def, struct_item, struct_path) => {
2570+
// Only register the constructor if this is a tuple-like struct.
2571+
match struct_def.ctor_id {
2572+
None => {
2573+
ccx.tcx.sess.bug("attempt to register a constructor of \
2574+
a non-tuple-like struct")
2575+
}
2576+
Some(ctor_id) => {
2577+
let ty = ty::node_id_to_type(ccx.tcx, ctor_id);
2578+
let sym = exported_name(ccx, (*struct_path).clone(), ty,
2579+
struct_item.attrs);
2580+
let llfn = register_fn_full(ccx, struct_item.span, sym, ctor_id, ty);
2581+
set_inline_hint(llfn);
2582+
llfn
2583+
}
2584+
}
2585+
}
2586+
2587+
ref variant => {
2588+
ccx.sess.bug(fmt!("get_item_val(): unexpected variant: %?",
2589+
variant))
2590+
}
2591+
};
2592+
2593+
if !exprt && !ccx.reachable.contains(&id) {
2594+
lib::llvm::SetLinkage(val, lib::llvm::InternalLinkage);
25802595
}
2581-
}
25822596

2583-
ref variant => {
2584-
ccx.sess.bug(fmt!("get_item_val(): unexpected variant: %?",
2585-
variant))
2586-
}
2587-
};
2588-
if !exprt && !ccx.reachable.contains(&id) {
2589-
lib::llvm::SetLinkage(val, lib::llvm::InternalLinkage);
2597+
ccx.item_vals.insert(id, val);
2598+
val
25902599
}
2591-
ccx.item_vals.insert(id, val);
2592-
val
2593-
}
25942600
}
25952601
}
25962602

@@ -2604,7 +2610,9 @@ pub fn register_method(ccx: @mut CrateContext,
26042610
path.push(path_name(gensym_name("meth")));
26052611
path.push(path_name(m.ident));
26062612

2607-
let llfn = register_fn_full(ccx, m.span, path, id, m.attrs, mty);
2613+
let sym = exported_name(ccx, path, mty, m.attrs);
2614+
2615+
let llfn = register_fn_full(ccx, m.span, sym, id, mty);
26082616
set_inline_hint_if_appr(m.attrs, llfn);
26092617
llfn
26102618
}
@@ -2618,7 +2626,7 @@ pub fn trans_constant(ccx: &mut CrateContext, it: @ast::item) {
26182626
ast::def_id { crate: ast::local_crate,
26192627
node: it.id });
26202628
let mut i = 0;
2621-
let path = item_path(ccx, it);
2629+
let path = item_path(ccx, &it.id);
26222630
for (*enum_definition).variants.iter().advance |variant| {
26232631
let p = vec::append(path.clone(), [
26242632
path_name(variant.node.name),

0 commit comments

Comments
 (0)