Skip to content

Commit 8f06766

Browse files
committed
---
yaml --- r: 1883 b: refs/heads/master c: dbc7289 h: refs/heads/master i: 1881: 90a12d0 1879: 5ff4f14 v: v3
1 parent 5461dd3 commit 8f06766

File tree

2 files changed

+91
-44
lines changed

2 files changed

+91
-44
lines changed

[refs]

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 643a75b86e2d5c26ff061ce61a44c39899893754
2+
refs/heads/master: dbc7289a4deb8dad8727962661bb2138a98ff5d2

trunk/src/comp/middle/trans.rs

+90-43
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ state type crate_ctxt = rec(session.session sess,
9090
ValueRef crate_ptr,
9191
hashmap[str, ValueRef] externs,
9292
hashmap[str, ValueRef] intrinsics,
93-
hashmap[str, ValueRef] item_names,
9493
hashmap[ast.def_id, ValueRef] item_ids,
9594
hashmap[ast.def_id, @ast.item] items,
9695
hashmap[ast.def_id,
@@ -108,7 +107,8 @@ state type crate_ctxt = rec(session.session sess,
108107
vec[ast.obj_field] obj_fields,
109108
@glue_fns glues,
110109
namegen names,
111-
str path);
110+
vec[str] path,
111+
std.sha1.sha1 sha);
112112

113113
state type fn_ctxt = rec(ValueRef llfn,
114114
ValueRef lltaskptr,
@@ -156,6 +156,30 @@ fn sep() -> str {
156156
ret "_";
157157
}
158158

159+
fn extend_path(@crate_ctxt cx, str name) -> @crate_ctxt {
160+
ret @rec(path = cx.path + vec(name) with *cx);
161+
}
162+
163+
fn path_name(vec[str] path) -> str {
164+
ret _str.connect(path, sep());
165+
}
166+
167+
168+
fn mangle_name_by_type(@crate_ctxt cx, @ty.t t) -> str {
169+
cx.sha.reset();
170+
auto f = metadata.def_to_str;
171+
cx.sha.input_str(metadata.ty_str(t, f));
172+
ret sep() + "rust" + sep()
173+
+ cx.sha.result_str() + sep()
174+
+ path_name(cx.path);
175+
}
176+
177+
fn mangle_name_by_seq(@crate_ctxt cx, str flav) -> str {
178+
ret sep() + "rust" + sep()
179+
+ cx.names.next(flav) + sep()
180+
+ path_name(cx.path);
181+
}
182+
159183
fn res(@block_ctxt bcx, ValueRef val) -> result {
160184
ret rec(mutable bcx = bcx,
161185
mutable val = val);
@@ -1549,7 +1573,8 @@ fn define_tydesc(@crate_ctxt cx, @ty.t t, vec[ast.def_id] typaram_defs) {
15491573
fn declare_generic_glue(@crate_ctxt cx, @ty.t t, str name) -> ValueRef {
15501574
auto llfnty = T_glue_fn(cx.tn);
15511575

1552-
auto fn_name = cx.names.next("_rust_" + name) + sep() + ty.ty_to_str(t);
1576+
auto gcx = @rec(path=vec("glue", name) with *cx);
1577+
auto fn_name = mangle_name_by_type(gcx, t);
15531578
fn_name = sanitize(fn_name);
15541579
auto llfn = decl_fastcall_fn(cx.llmod, fn_name, llfnty);
15551580
llvm.LLVMSetLinkage(llfn, lib.llvm.LLVMPrivateLinkage as llvm.Linkage);
@@ -3193,9 +3218,7 @@ fn trans_for_each(@block_ctxt cx,
31933218

31943219
// Step 2: Declare foreach body function.
31953220

3196-
let str s =
3197-
cx.fcx.ccx.names.next("_rust_foreach")
3198-
+ sep() + cx.fcx.ccx.path;
3221+
let str s = mangle_name_by_seq(cx.fcx.ccx, "foreach");
31993222

32003223
// The 'env' arg entering the body function is a fake env member (as in
32013224
// the env-part of the normal rust calling convention) that actually
@@ -3788,7 +3811,7 @@ fn trans_bind_thunk(@crate_ctxt cx,
37883811
// Construct a thunk-call with signature incoming_fty, and that copies
37893812
// args forward into a call to outgoing_fty.
37903813

3791-
let str s = cx.names.next("_rust_thunk") + sep() + cx.path;
3814+
let str s = mangle_name_by_seq(cx, "thunk");
37923815
let TypeRef llthunk_ty = get_pair_fn_ty(type_of(cx, incoming_fty));
37933816
let ValueRef llthunk = decl_fastcall_fn(cx.llmod, s, llthunk_ty);
37943817

@@ -5327,7 +5350,6 @@ fn trans_fn(@crate_ctxt cx, &ast._fn f, ast.def_id fid,
53275350
&vec[ast.ty_param] ty_params, &ast.ann ann) {
53285351

53295352
auto llfndecl = cx.item_ids.get(fid);
5330-
cx.item_names.insert(cx.path, llfndecl);
53315353

53325354
auto fcx = new_fn_ctxt(cx, llfndecl);
53335355
create_llargs_for_fn_args(fcx, f.proto,
@@ -5378,22 +5400,21 @@ fn trans_vtbl(@crate_ctxt cx, TypeRef self_ty,
53785400
}
53795401
}
53805402

5381-
let @crate_ctxt mcx = @rec(path=cx.path + sep() + m.node.ident
5382-
with *cx);
5383-
5384-
let str s = cx.names.next("_rust_method") + sep() + mcx.path;
5403+
let @crate_ctxt mcx = extend_path(cx, m.node.ident);
5404+
let str s = mangle_name_by_seq(mcx, "method");
53855405
let ValueRef llfn = decl_fastcall_fn(cx.llmod, s, llfnty);
53865406
cx.item_ids.insert(m.node.id, llfn);
53875407
cx.item_symbols.insert(m.node.id, s);
53885408

5409+
53895410
trans_fn(mcx, m.node.meth, m.node.id, some[TypeRef](self_ty),
53905411
ty_params, m.node.ann);
53915412
methods += vec(llfn);
53925413
}
53935414
auto vtbl = C_struct(methods);
5394-
auto gvar = llvm.LLVMAddGlobal(cx.llmod,
5395-
val_ty(vtbl),
5396-
_str.buf("_rust_vtbl" + sep() + cx.path));
5415+
auto vtbl_name = mangle_name_by_seq(cx, "vtbl");
5416+
auto gvar = llvm.LLVMAddGlobal(cx.llmod, val_ty(vtbl),
5417+
_str.buf(vtbl_name));
53975418
llvm.LLVMSetInitializer(gvar, vtbl);
53985419
llvm.LLVMSetGlobalConstant(gvar, True);
53995420
llvm.LLVMSetLinkage(gvar, lib.llvm.LLVMPrivateLinkage
@@ -5405,7 +5426,6 @@ fn trans_obj(@crate_ctxt cx, &ast._obj ob, ast.def_id oid,
54055426
&vec[ast.ty_param] ty_params, &ast.ann ann) {
54065427

54075428
auto llctor_decl = cx.item_ids.get(oid);
5408-
cx.item_names.insert(cx.path, llctor_decl);
54095429

54105430
// Translate obj ctor args to function arguments.
54115431
let vec[ast.arg] fn_args = vec();
@@ -5628,29 +5648,29 @@ fn trans_const(@crate_ctxt cx, @ast.expr e,
56285648
fn trans_item(@crate_ctxt cx, &ast.item item) {
56295649
alt (item.node) {
56305650
case (ast.item_fn(?name, ?f, ?tps, ?fid, ?ann)) {
5631-
auto sub_cx = @rec(path=cx.path + sep() + name with *cx);
5651+
auto sub_cx = extend_path(cx, name);
56325652
trans_fn(sub_cx, f, fid, none[TypeRef], tps, ann);
56335653
}
56345654
case (ast.item_obj(?name, ?ob, ?tps, ?oid, ?ann)) {
5635-
auto sub_cx = @rec(path=cx.path + sep() + name,
5636-
obj_typarams=tps,
5637-
obj_fields=ob.fields with *cx);
5655+
auto sub_cx = @rec(obj_typarams=tps,
5656+
obj_fields=ob.fields with
5657+
*extend_path(cx, name));
56385658
trans_obj(sub_cx, ob, oid, tps, ann);
56395659
}
56405660
case (ast.item_mod(?name, ?m, _)) {
5641-
auto sub_cx = @rec(path=cx.path + sep() + name with *cx);
5661+
auto sub_cx = extend_path(cx, name);
56425662
trans_mod(sub_cx, m);
56435663
}
56445664
case (ast.item_tag(?name, ?variants, ?tps, ?tag_id)) {
5645-
auto sub_cx = @rec(path=cx.path + sep() + name with *cx);
5665+
auto sub_cx = extend_path(cx, name);
56465666
auto i = 0;
56475667
for (ast.variant variant in variants) {
56485668
trans_tag_variant(sub_cx, tag_id, variant, i, tps);
56495669
i += 1;
56505670
}
56515671
}
56525672
case (ast.item_const(?name, _, ?expr, ?cid, ?ann)) {
5653-
auto sub_cx = @rec(path=cx.path + sep() + name with *cx);
5673+
auto sub_cx = extend_path(cx, name);
56545674
trans_const(sub_cx, expr, cid, ann);
56555675
}
56565676
case (_) { /* fall through */ }
@@ -5672,8 +5692,7 @@ fn get_pair_fn_ty(TypeRef llpairty) -> TypeRef {
56725692
}
56735693

56745694
fn decl_fn_and_pair(@crate_ctxt cx,
5675-
str kind,
5676-
str name,
5695+
str flav,
56775696
vec[ast.ty_param] ty_params,
56785697
&ast.ann ann,
56795698
ast.def_id id) {
@@ -5693,11 +5712,11 @@ fn decl_fn_and_pair(@crate_ctxt cx,
56935712
}
56945713

56955714
// Declare the function itself.
5696-
let str s = cx.names.next("_rust_" + kind) + sep() + name;
5715+
let str s = mangle_name_by_seq(cx, flav);
56975716
let ValueRef llfn = decl_fastcall_fn(cx.llmod, s, llfty);
56985717

56995718
// Declare the global constant pair that points to it.
5700-
let str ps = cx.names.next("_rust_" + kind + "_pair") + sep() + name;
5719+
let str ps = mangle_name_by_type(cx, node_ann_type(cx, ann));
57015720

57025721
register_fn_pair(cx, ps, llpairty, llfn, id);
57035722
}
@@ -5756,14 +5775,14 @@ fn decl_native_fn_and_pair(@crate_ctxt cx,
57565775

57575776
// Declare the wrapper.
57585777
auto wrapper_type = native_fn_wrapper_type(cx, num_ty_param, ann);
5759-
let str s = cx.names.next("_rust_wrapper") + sep() + name;
5778+
let str s = mangle_name_by_seq(cx, "wrapper");
57605779
let ValueRef wrapper_fn = decl_fastcall_fn(cx.llmod, s, wrapper_type);
57615780
llvm.LLVMSetLinkage(wrapper_fn, lib.llvm.LLVMPrivateLinkage
57625781
as llvm.Linkage);
57635782

57645783
// Declare the global constant pair that points to it.
57655784
auto wrapper_pair_type = T_fn_pair(cx.tn, wrapper_type);
5766-
let str ps = cx.names.next("_rust_wrapper_pair") + sep() + name;
5785+
let str ps = mangle_name_by_type(cx, node_ann_type(cx, ann));
57675786

57685787
register_fn_pair(cx, ps, wrapper_pair_type, wrapper_fn, id);
57695788

@@ -5827,8 +5846,33 @@ fn collect_native_item(&@crate_ctxt cx, @ast.native_item i) -> @crate_ctxt {
58275846
ret cx;
58285847
}
58295848

5830-
fn collect_item(&@crate_ctxt cx, @ast.item i) -> @crate_ctxt {
5849+
fn item_name(@ast.item i) -> str {
5850+
alt (i.node) {
5851+
case (ast.item_mod(?name, _, _)) {
5852+
ret name;
5853+
}
5854+
case (ast.item_tag(?name, _, _, _)) {
5855+
ret name;
5856+
}
5857+
case (ast.item_const(?name, _, _, _, _)) {
5858+
ret name;
5859+
}
5860+
case (ast.item_fn(?name, _, _, _, _)) {
5861+
ret name;
5862+
}
5863+
case (ast.item_native_mod(?name, _, _)) {
5864+
ret name;
5865+
}
5866+
case (ast.item_ty(?name, _, _, _, _)) {
5867+
ret name;
5868+
}
5869+
case (ast.item_obj(?name, _, _, _, _)) {
5870+
ret name;
5871+
}
5872+
}
5873+
}
58315874

5875+
fn collect_item(&@crate_ctxt cx, @ast.item i) -> @crate_ctxt {
58325876
alt (i.node) {
58335877
case (ast.item_const(?name, _, _, ?cid, ?ann)) {
58345878
auto typ = node_ann_type(cx, ann);
@@ -5844,35 +5888,36 @@ fn collect_item(&@crate_ctxt cx, @ast.item i) -> @crate_ctxt {
58445888
cx.items.insert(mid, i);
58455889
}
58465890

5847-
case (ast.item_tag(_, ?variants, ?tps, ?tag_id)) {
5891+
case (ast.item_tag(?name, ?variants, ?tps, ?tag_id)) {
58485892
cx.items.insert(tag_id, i);
58495893
}
5850-
58515894
case (_) { /* fall through */ }
58525895
}
5853-
ret cx;
5896+
ret extend_path(cx, item_name(i));
58545897
}
58555898

58565899
fn collect_item_pass2(&@crate_ctxt cx, @ast.item i) -> @crate_ctxt {
58575900
alt (i.node) {
58585901
case (ast.item_fn(?name, ?f, ?tps, ?fid, ?ann)) {
58595902
cx.items.insert(fid, i);
58605903
if (! cx.obj_methods.contains_key(fid)) {
5861-
decl_fn_and_pair(cx, "fn", name, tps, ann, fid);
5904+
decl_fn_and_pair(extend_path(cx, name), "fn",
5905+
tps, ann, fid);
58625906
}
58635907
}
58645908

58655909
case (ast.item_obj(?name, ?ob, ?tps, ?oid, ?ann)) {
58665910
cx.items.insert(oid, i);
5867-
decl_fn_and_pair(cx, "obj_ctor", name, tps, ann, oid);
5911+
decl_fn_and_pair(extend_path(cx, name), "obj_ctor",
5912+
tps, ann, oid);
58685913
for (@ast.method m in ob.methods) {
58695914
cx.obj_methods.insert(m.node.id, ());
58705915
}
58715916
}
58725917

58735918
case (_) { /* fall through */ }
58745919
}
5875-
ret cx;
5920+
ret extend_path(cx, item_name(i));
58765921
}
58775922

58785923

@@ -5904,7 +5949,7 @@ fn collect_tag_ctor(&@crate_ctxt cx, @ast.item i) -> @crate_ctxt {
59045949
case (ast.item_tag(_, ?variants, ?tps, _)) {
59055950
for (ast.variant variant in variants) {
59065951
if (_vec.len[ast.variant_arg](variant.args) != 0u) {
5907-
decl_fn_and_pair(cx, "tag", variant.name,
5952+
decl_fn_and_pair(extend_path(cx, variant.name), "tag",
59085953
tps, variant.ann, variant.id);
59095954
}
59105955
}
@@ -5963,7 +6008,8 @@ fn trans_constant(&@crate_ctxt cx, @ast.item it) -> @crate_ctxt {
59636008
// with consts.
59646009
auto v = C_int(1);
59656010
cx.item_ids.insert(cid, v);
5966-
auto s = cx.names.next("_rust_const") + sep() + name;
6011+
auto s = mangle_name_by_type(extend_path(cx, name),
6012+
node_ann_type(cx, ann));
59676013
cx.item_symbols.insert(cid, s);
59686014
}
59696015

@@ -6064,10 +6110,10 @@ fn find_main_fn(@crate_ctxt cx) -> ValueRef {
60646110
auto e = sep() + "main";
60656111
let ValueRef v = C_nil();
60666112
let uint n = 0u;
6067-
for each (@tup(str,ValueRef) i in cx.item_names.items()) {
6068-
if (_str.ends_with(i._0, e)) {
6113+
for each (@tup(ast.def_id, str) i in cx.item_symbols.items()) {
6114+
if (_str.ends_with(i._1, e)) {
60696115
n += 1u;
6070-
v = i._1;
6116+
v = cx.item_ids.get(i._0);
60716117
}
60726118
}
60736119
alt (n) {
@@ -6534,14 +6580,14 @@ fn trans_crate(session.session sess, @ast.crate crate, str output,
65346580
let vec[ast.ty_param] obj_typarams = vec();
65356581
let vec[ast.obj_field] obj_fields = vec();
65366582

6583+
let vec[str] pth = vec();
65376584
auto cx = @rec(sess = sess,
65386585
llmod = llmod,
65396586
td = td,
65406587
tn = tn,
65416588
crate_ptr = crate_ptr,
65426589
externs = new_str_hash[ValueRef](),
65436590
intrinsics = intrinsics,
6544-
item_names = new_str_hash[ValueRef](),
65456591
item_ids = new_def_hash[ValueRef](),
65466592
items = new_def_hash[@ast.item](),
65476593
native_items = new_def_hash[@ast.native_item](),
@@ -6557,7 +6603,8 @@ fn trans_crate(session.session sess, @ast.crate crate, str output,
65576603
obj_fields = obj_fields,
65586604
glues = glues,
65596605
names = namegen(0),
6560-
path = "_rust");
6606+
path = pth,
6607+
sha = std.sha1.mk_sha1());
65616608

65626609
create_typedefs(cx);
65636610

0 commit comments

Comments
 (0)