Skip to content

Commit 277d42f

Browse files
committed
rustc: Only declare each native function once. std.rc now links.
1 parent a1aad28 commit 277d42f

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/comp/middle/trans.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ state type crate_ctxt = rec(session.session sess,
7171
hashmap[str, ValueRef] upcalls,
7272
hashmap[str, ValueRef] intrinsics,
7373
hashmap[str, ValueRef] item_names,
74+
hashmap[str, ValueRef] native_fns,
7475
hashmap[ast.def_id, ValueRef] item_ids,
7576
hashmap[ast.def_id, @ast.item] items,
7677
hashmap[ast.def_id,
@@ -5452,7 +5453,19 @@ fn decl_native_fn_and_pair(@crate_ctxt cx,
54525453
auto abi = ty.ty_fn_abi(fn_type);
54535454
auto llfnty = type_of_native_fn(cx, abi, ty.ty_fn_args(fn_type),
54545455
ty.ty_fn_ret(fn_type), num_ty_param);
5455-
auto function = decl_cdecl_fn(cx.llmod, name, llfnty);
5456+
5457+
// We can only declare a native function with a given name once; LLVM
5458+
// unhelpfully mangles the names if we try to multiply declare one.
5459+
auto function;
5460+
if (!cx.native_fns.contains_key(name)) {
5461+
function = decl_cdecl_fn(cx.llmod, name, llfnty);
5462+
cx.native_fns.insert(name, function);
5463+
} else {
5464+
// We support type-punning a native function by giving it different
5465+
// Rust types.
5466+
auto llorigfn = cx.native_fns.get(name);
5467+
function = bcx.build.PointerCast(llorigfn, T_ptr(llfnty));
5468+
}
54565469

54575470
let vec[ValueRef] call_args = vec();
54585471
auto arg_n = 3u;
@@ -6200,6 +6213,7 @@ fn trans_crate(session.session sess, @ast.crate crate, str output,
62006213
upcalls = new_str_hash[ValueRef](),
62016214
intrinsics = intrinsics,
62026215
item_names = new_str_hash[ValueRef](),
6216+
native_fns = new_str_hash[ValueRef](),
62036217
item_ids = new_def_hash[ValueRef](),
62046218
items = new_def_hash[@ast.item](),
62056219
native_items = new_def_hash[@ast.native_item](),

0 commit comments

Comments
 (0)