Skip to content

Commit ec9d7ab

Browse files
committed
rustc: Have tag_variants() return a new variant_info structure instead of an AST node
1 parent 8b211f4 commit ec9d7ab

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

src/comp/middle/trans.rs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,8 +1120,8 @@ fn static_size_of_tag(@crate_ctxt cx, @ty.t t) -> uint {
11201120
// Compute max(variant sizes).
11211121
auto max_size = 0u;
11221122
auto variants = tag_variants(cx, tid);
1123-
for (ast.variant variant in variants) {
1124-
auto tup_ty = ty.plain_tup_ty(variant_types(cx, variant));
1123+
for (variant_info variant in variants) {
1124+
auto tup_ty = ty.plain_tup_ty(variant.args);
11251125

11261126
// Perform any type parameter substitutions.
11271127
tup_ty = ty.substitute_ty_params(ty_params, subtys, tup_ty);
@@ -1192,9 +1192,9 @@ fn dynamic_size_of(@block_ctxt cx, @ty.t t) -> result {
11921192

11931193
auto ty_params = tag_ty_params(bcx.fcx.ccx, tid);
11941194
auto variants = tag_variants(bcx.fcx.ccx, tid);
1195-
for (ast.variant variant in variants) {
1196-
// Perform type substitution on the raw variant types.
1197-
let vec[@ty.t] raw_tys = variant_types(bcx.fcx.ccx, variant);
1195+
for (variant_info variant in variants) {
1196+
// Perform type substitution on the raw argument types.
1197+
let vec[@ty.t] raw_tys = variant.args;
11981198
let vec[@ty.t] tys = vec();
11991199
for (@ty.t raw_ty in raw_tys) {
12001200
auto t = ty.substitute_ty_params(ty_params, tps, raw_ty);
@@ -1367,12 +1367,12 @@ fn GEP_tag(@block_ctxt cx,
13671367

13681368
// Synthesize a tuple type so that GEP_tup_like() can work its magic.
13691369
// Separately, store the type of the element we're interested in.
1370-
auto arg_tys = arg_tys_of_fn(variant.node.ann);
1370+
auto arg_tys = variant.args;
13711371
auto elem_ty = ty.plain_ty(ty.ty_nil); // typestate infelicity
13721372
auto i = 0;
13731373
let vec[@ty.t] true_arg_tys = vec();
1374-
for (ty.arg a in arg_tys) {
1375-
auto arg_ty = ty.substitute_ty_params(ty_params, ty_substs, a.ty);
1374+
for (@ty.t aty in arg_tys) {
1375+
auto arg_ty = ty.substitute_ty_params(ty_params, ty_substs, aty);
13761376
true_arg_tys += vec(arg_ty);
13771377
if (i == ix) {
13781378
elem_ty = arg_ty;
@@ -1902,44 +1902,47 @@ fn decr_refcnt_and_if_zero(@block_ctxt cx,
19021902

19031903
// Tag information
19041904

1905-
fn variant_types(@crate_ctxt cx, &ast.variant v) -> vec[@ty.t] {
1906-
let vec[@ty.t] tys = vec();
1907-
alt (ty.ann_to_type(v.node.ann).struct) {
1908-
case (ty.ty_fn(_, ?args, _)) {
1909-
for (ty.arg arg in args) {
1910-
tys += vec(arg.ty);
1911-
}
1912-
}
1913-
case (ty.ty_tag(_, _)) { /* nothing */ }
1914-
case (_) { fail; }
1915-
}
1916-
ret tys;
1917-
}
1918-
19191905
// Returns the type parameters of a tag.
19201906
fn tag_ty_params(@crate_ctxt cx, ast.def_id id) -> vec[ast.def_id] {
19211907
ret ty.lookup_generic_item_type(cx.sess, cx.type_cache, id)._0;
19221908
}
19231909

1924-
// Returns the variants in a tag.
1925-
fn tag_variants(@crate_ctxt cx, ast.def_id id) -> vec[ast.variant] {
1910+
type variant_info = rec(vec[@ty.t] args, @ty.t ctor_ty, ast.def_id id);
1911+
1912+
// Returns information about the variants in a tag.
1913+
fn tag_variants(@crate_ctxt cx, ast.def_id id) -> vec[variant_info] {
1914+
// FIXME: This doesn't work for external variants.
19261915
check (cx.items.contains_key(id));
19271916
alt (cx.items.get(id).node) {
1928-
case (ast.item_tag(_, ?variants, _, _, _)) { ret variants; }
1917+
case (ast.item_tag(_, ?variants, _, _, _)) {
1918+
let vec[variant_info] result = vec();
1919+
for (ast.variant variant in variants) {
1920+
auto ctor_ty = node_ann_type(cx, variant.node.ann);
1921+
let vec[@ty.t] arg_tys = vec();
1922+
if (_vec.len[ast.variant_arg](variant.node.args) > 0u) {
1923+
for (ty.arg a in ty.ty_fn_args(ctor_ty)) {
1924+
arg_tys += vec(a.ty);
1925+
}
1926+
}
1927+
auto did = variant.node.id;
1928+
result += vec(rec(args=arg_tys, ctor_ty=ctor_ty, id=did));
1929+
}
1930+
ret result;
1931+
}
19291932
}
19301933
fail; // not reached
19311934
}
19321935

1933-
// Returns the tag variant with the given ID.
1936+
// Returns information about the tag variant with the given ID.
19341937
fn tag_variant_with_id(@crate_ctxt cx,
19351938
&ast.def_id tag_id,
1936-
&ast.def_id variant_id) -> ast.variant {
1939+
&ast.def_id variant_id) -> variant_info {
19371940
auto variants = tag_variants(cx, tag_id);
19381941

19391942
auto i = 0u;
1940-
while (i < _vec.len[ast.variant](variants)) {
1943+
while (i < _vec.len[variant_info](variants)) {
19411944
auto variant = variants.(i);
1942-
if (common.def_eq(variant.node.id, variant_id)) {
1945+
if (common.def_eq(variant.id, variant_id)) {
19431946
ret variant;
19441947
}
19451948
i += 1u;
@@ -2040,7 +2043,7 @@ fn iter_structural_ty_full(@block_ctxt cx,
20402043
}
20412044
case (ty.ty_tag(?tid, ?tps)) {
20422045
auto variants = tag_variants(cx.fcx.ccx, tid);
2043-
auto n_variants = _vec.len[ast.variant](variants);
2046+
auto n_variants = _vec.len[variant_info](variants);
20442047

20452048
// Cast the tags to types we can GEP into.
20462049
auto lltagty = T_opaque_tag_ptr(cx.fcx.ccx.tn);
@@ -2076,28 +2079,28 @@ fn iter_structural_ty_full(@block_ctxt cx,
20762079
auto ty_params = tag_ty_params(bcx.fcx.ccx, tid);
20772080

20782081
auto i = 0u;
2079-
for (ast.variant variant in variants) {
2082+
for (variant_info variant in variants) {
20802083
auto variant_cx = new_sub_block_ctxt(bcx,
20812084
"tag-iter-variant-" +
20822085
_uint.to_str(i, 10u));
20832086
llvm.LLVMAddCase(llswitch, C_int(i as int), variant_cx.llbb);
20842087

2085-
if (_vec.len[ast.variant_arg](variant.node.args) > 0u) {
2088+
if (_vec.len[@ty.t](variant.args) > 0u) {
20862089
// N-ary variant.
2087-
auto fn_ty = ty.ann_to_type(variants.(i).node.ann);
2090+
auto fn_ty = variant.ctor_ty;
20882091
alt (fn_ty.struct) {
20892092
case (ty.ty_fn(_, ?args, _)) {
20902093
auto j = 0;
20912094
for (ty.arg a in args) {
20922095
auto v = vec(C_int(0), C_int(j as int));
20932096

20942097
auto rslt = GEP_tag(variant_cx, llunion_a_ptr,
2095-
tid, variants.(i).node.id, tps, j);
2098+
tid, variant.id, tps, j);
20962099
auto llfldp_a = rslt.val;
20972100
variant_cx = rslt.bcx;
20982101

20992102
rslt = GEP_tag(variant_cx, llunion_b_ptr, tid,
2100-
variants.(i).node.id, tps, j);
2103+
variant.id, tps, j);
21012104
auto llfldp_b = rslt.val;
21022105
variant_cx = rslt.bcx;
21032106

@@ -3454,8 +3457,8 @@ fn trans_pat_match(@block_ctxt cx, @ast.pat pat, ValueRef llval,
34543457

34553458
auto variants = tag_variants(cx.fcx.ccx, vdef._0);
34563459
auto i = 0;
3457-
for (ast.variant v in variants) {
3458-
auto this_variant_id = v.node.id;
3460+
for (variant_info v in variants) {
3461+
auto this_variant_id = v.id;
34593462
if (variant_id._0 == this_variant_id._0 &&
34603463
variant_id._1 == this_variant_id._1) {
34613464
variant_tag = i;

0 commit comments

Comments
 (0)