Skip to content

Commit 376b35e

Browse files
committed
rustc: Remove the overly complex variant_indices and n_ary_variant_indices tables
1 parent eeecc8d commit 376b35e

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

src/comp/middle/trans.rs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ type glue_fns = rec(ValueRef activate_glue,
4444
ValueRef exit_task_glue,
4545
vec[ValueRef] upcall_glues);
4646

47+
tag arity { nullary; n_ary(uint); }
4748
type tag_info = rec(type_handle th,
48-
hashmap[ast.def_id, uint] variant_indices,
49-
hashmap[ast.def_id, uint] n_ary_variant_indices);
49+
mutable vec[tup(ast.def_id,arity)] variants);
5050

5151
state type crate_ctxt = rec(session.session sess,
5252
ModuleRef llmod,
@@ -55,7 +55,7 @@ state type crate_ctxt = rec(session.session sess,
5555
hashmap[str, ValueRef] fn_names,
5656
hashmap[ast.def_id, ValueRef] fn_ids,
5757
hashmap[ast.def_id, @ast.item] items,
58-
hashmap[ast.def_id, tag_info] tags,
58+
hashmap[ast.def_id, @tag_info] tags,
5959
@glue_fns glues,
6060
namegen names,
6161
str path);
@@ -1133,14 +1133,22 @@ fn trans_name(@block_ctxt cx, &ast.name n, &option.t[ast.def] dopt)
11331133
case (ast.def_variant(?tid, ?vid)) {
11341134
check (cx.fcx.ccx.tags.contains_key(tid));
11351135
auto info = cx.fcx.ccx.tags.get(tid);
1136-
if (info.n_ary_variant_indices.contains_key(vid)) {
1137-
cx.fcx.ccx.sess.unimpl("n-ary tag constructors in " +
1138-
"trans");
1139-
} else {
1140-
// Nullary tag variant case.
1141-
auto idx = info.variant_indices.get(vid);
1142-
auto elems = vec(C_int(idx as int));
1143-
ret tup(res(cx, C_struct(elems)), false);
1136+
auto i = 0;
1137+
for (tup(ast.def_id,arity) v in info.variants) {
1138+
if (vid == v._0) {
1139+
alt (v._1) {
1140+
case (nullary) {
1141+
auto elems = vec(C_int(i));
1142+
ret tup(res(cx, C_struct(elems)), false);
1143+
}
1144+
case (n_ary(_)) {
1145+
cx.fcx.ccx.sess.unimpl("n-ary tag " +
1146+
"constructor in " +
1147+
"trans");
1148+
}
1149+
}
1150+
}
1151+
i += 1;
11441152
}
11451153
}
11461154
case (_) {
@@ -1716,17 +1724,15 @@ fn resolve_tag_types_for_item(&@crate_ctxt cx, @ast.item i) -> @crate_ctxt {
17161724
let vec[TypeRef] variant_tys = vec();
17171725

17181726
auto info = cx.tags.get(tag_id);
1719-
auto variant_indices = info.variant_indices;
1720-
auto n_ary_variant_indices = info.n_ary_variant_indices;
1727+
let vec[tup(ast.def_id,arity)] variant_info = vec();
17211728

17221729
auto tag_ty;
17231730
if (_vec.len[ast.variant](variants) == 0u) {
17241731
tag_ty = T_struct(vec(T_int()));
17251732
} else {
1726-
auto variant_idx = 0u;
1727-
auto n_ary_variant_idx = 0u;
1728-
1733+
auto n_ary_idx = 0u;
17291734
for (ast.variant variant in variants) {
1735+
auto arity_info;
17301736
if (_vec.len[@ast.ty](variant.args) > 0u) {
17311737
let vec[TypeRef] lltys = vec();
17321738

@@ -1741,18 +1747,20 @@ fn resolve_tag_types_for_item(&@crate_ctxt cx, @ast.item i) -> @crate_ctxt {
17411747

17421748
variant_tys += vec(T_struct(lltys));
17431749

1744-
n_ary_variant_indices.insert(variant.id,
1745-
n_ary_variant_idx);
1746-
n_ary_variant_idx += 1u;
1750+
arity_info = n_ary(n_ary_idx);
1751+
n_ary_idx += 1u;
1752+
} else {
1753+
arity_info = nullary;
17471754
}
17481755

1749-
variant_indices.insert(variant.id, variant_idx);
1750-
variant_idx += 1u;
1756+
variant_info += vec(tup(variant.id, arity_info));
17511757
}
17521758

17531759
tag_ty = T_struct(vec(T_int(), T_union(variant_tys)));
17541760
}
17551761

1762+
info.variants = variant_info;
1763+
17561764
auto th = cx.tags.get(tag_id).th.llth;
17571765
llvm.LLVMRefineType(llvm.LLVMResolveTypeHandle(th), tag_ty);
17581766
}
@@ -1782,9 +1790,9 @@ fn collect_item(&@crate_ctxt cx, @ast.item i) -> @crate_ctxt {
17821790
case (ast.item_tag(_, ?variants, _, ?tag_id)) {
17831791
auto vi = new_def_hash[uint]();
17841792
auto navi = new_def_hash[uint]();
1785-
cx.tags.insert(tag_id, rec(th=mk_type_handle(),
1786-
variant_indices=vi,
1787-
n_ary_variant_indices=navi));
1793+
let vec[tup(ast.def_id,arity)] variant_info = vec();
1794+
cx.tags.insert(tag_id, @rec(th=mk_type_handle(),
1795+
mutable variants=variant_info));
17881796
}
17891797

17901798
case (_) { /* fall through */ }
@@ -1973,7 +1981,7 @@ fn trans_crate(session.session sess, @ast.crate crate, str output) {
19731981
fn_names = new_str_hash[ValueRef](),
19741982
fn_ids = new_def_hash[ValueRef](),
19751983
items = new_def_hash[@ast.item](),
1976-
tags = new_def_hash[tag_info](),
1984+
tags = new_def_hash[@tag_info](),
19771985
glues = glues,
19781986
names = namegen(0),
19791987
path = "_rust");

0 commit comments

Comments
 (0)