diff options
Diffstat (limited to 'src/comp/middle')
| -rw-r--r-- | src/comp/middle/fold.rs | 30 | ||||
| -rw-r--r-- | src/comp/middle/trans.rs | 40 | ||||
| -rw-r--r-- | src/comp/middle/ty.rs | 2 | ||||
| -rw-r--r-- | src/comp/middle/typeck.rs | 55 |
4 files changed, 35 insertions, 92 deletions
diff --git a/src/comp/middle/fold.rs b/src/comp/middle/fold.rs index 8de85749..1fc91693 100644 --- a/src/comp/middle/fold.rs +++ b/src/comp/middle/fold.rs @@ -107,8 +107,7 @@ type ast_fold[ENV] = (fn(&ENV e, &span sp, @expr cond, &block thn, - &vec[tup(@expr, block)] elifs, - &option.t[block] els, + &option.t[@expr] els, ann a) -> @expr) fold_expr_if, (fn(&ENV e, &span sp, @@ -590,27 +589,17 @@ fn fold_expr[ENV](&ENV env, ast_fold[ENV] fld, &@expr e) -> @expr { ret fld.fold_expr_cast(env_, e.span, ee, tt, at); } - case (ast.expr_if(?cnd, ?thn, ?elifs, ?els, ?t)) { + case (ast.expr_if(?cnd, ?thn, ?els, ?t)) { auto ccnd = fold_expr(env_, fld, cnd); auto tthn = fold_block(env_, fld, thn); - - let vec[tup(@ast.expr, ast.block)] eelifs = vec(); - for (tup(@expr, block) elif in elifs) { - auto elifcnd = elif._0; - auto elifthn = elif._1; - auto elifccnd = fold_expr(env_, fld, elifcnd); - auto eliftthn = fold_block(env_, fld, elifthn); - eelifs += tup(elifccnd, eliftthn); - } - - auto eels = none[block]; + auto eels = none[@expr]; alt (els) { - case (some[block](?b)) { - eels = some(fold_block(env_, fld, b)); + case (some[@expr](?e)) { + eels = some(fold_expr(env_, fld, e)); } case (_) { /* fall through */ } } - ret fld.fold_expr_if(env_, e.span, ccnd, tthn, eelifs, eels, t); + ret fld.fold_expr_if(env_, e.span, ccnd, tthn, eels, t); } case (ast.expr_for(?decl, ?seq, ?body, ?t)) { @@ -1189,9 +1178,8 @@ fn identity_fold_expr_cast[ENV](&ENV env, &span sp, @ast.expr e, fn identity_fold_expr_if[ENV](&ENV env, &span sp, @expr cond, &block thn, - &vec[tup(@expr, block)] elifs, - &option.t[block] els, ann a) -> @expr { - ret @respan(sp, ast.expr_if(cond, thn, elifs, els, a)); + &option.t[@expr] els, ann a) -> @expr { + ret @respan(sp, ast.expr_if(cond, thn, els, a)); } fn identity_fold_expr_for[ENV](&ENV env, &span sp, @@ -1554,7 +1542,7 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] { fold_expr_unary = bind identity_fold_expr_unary[ENV](_,_,_,_,_), fold_expr_lit = bind identity_fold_expr_lit[ENV](_,_,_,_), fold_expr_cast = bind identity_fold_expr_cast[ENV](_,_,_,_,_), - fold_expr_if = bind identity_fold_expr_if[ENV](_,_,_,_,_,_,_), + fold_expr_if = bind identity_fold_expr_if[ENV](_,_,_,_,_,_), fold_expr_for = bind identity_fold_expr_for[ENV](_,_,_,_,_,_), fold_expr_for_each = bind identity_fold_expr_for_each[ENV](_,_,_,_,_,_), diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index de19a01a..4fab3454 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -2833,9 +2833,8 @@ fn join_results(@block_ctxt parent_cx, ret res(join_cx, phi); } -fn trans_if(@block_ctxt cx, @ast.expr cond, &ast.block thn, - &vec[tup(@ast.expr, ast.block)] elifs, - &option.t[ast.block] els) -> result { +fn trans_if(@block_ctxt cx, @ast.expr cond, + &ast.block thn, &option.t[@ast.expr] els) -> result { auto cond_res = trans_expr(cx, cond); @@ -2845,25 +2844,11 @@ fn trans_if(@block_ctxt cx, @ast.expr cond, &ast.block thn, auto else_cx = new_scope_block_ctxt(cx, "else"); auto else_res = res(else_cx, C_nil()); - auto num_elifs = _vec.len[tup(@ast.expr, ast.block)](elifs); - if (num_elifs > 0u) { - auto next_elif = elifs.(0u); - auto next_elifthn = next_elif._0; - auto next_elifcnd = next_elif._1; - auto rest_elifs = _vec.shift[tup(@ast.expr, ast.block)](elifs); - else_res = trans_if(else_cx, next_elifthn, next_elifcnd, - rest_elifs, els); - } - - /* else: FIXME: rustboot has a problem here - with preconditions inside an else block */ - if (num_elifs == 0u) { - alt (els) { - case (some[ast.block](?eblk)) { - else_res = trans_block(else_cx, eblk); - } - case (_) { /* fall through */ } + alt (els) { + case (some[@ast.expr](?elexpr)) { + else_res = trans_expr(else_cx, elexpr); } + case (_) { /* fall through */ } } cond_res.bcx.build.CondBr(cond_res.val, @@ -4303,8 +4288,8 @@ fn trans_expr(@block_ctxt cx, @ast.expr e) -> result { ret trans_binary(cx, op, x, y); } - case (ast.expr_if(?cond, ?thn, ?elifs, ?els, _)) { - ret trans_if(cx, cond, thn, elifs, els); + case (ast.expr_if(?cond, ?thn, ?els, _)) { + ret trans_if(cx, cond, thn, els); } case (ast.expr_for(?decl, ?seq, ?body, _)) { @@ -4328,14 +4313,7 @@ fn trans_expr(@block_ctxt cx, @ast.expr e) -> result { } case (ast.expr_block(?blk, _)) { - auto sub_cx = new_scope_block_ctxt(cx, "block-expr body"); - auto next_cx = new_sub_block_ctxt(cx, "next"); - auto sub = trans_block(sub_cx, blk); - - cx.build.Br(sub_cx.llbb); - sub.bcx.build.Br(next_cx.llbb); - - ret res(next_cx, sub.val); + ret trans_block(cx, blk); } case (ast.expr_assign(?dst, ?src, ?ann)) { diff --git a/src/comp/middle/ty.rs b/src/comp/middle/ty.rs index 0999e416..0c540f5c 100644 --- a/src/comp/middle/ty.rs +++ b/src/comp/middle/ty.rs @@ -712,7 +712,7 @@ fn expr_ty(@ast.expr expr) -> @t { case (ast.expr_unary(_, _, ?ann)) { ret ann_to_type(ann); } case (ast.expr_lit(_, ?ann)) { ret ann_to_type(ann); } case (ast.expr_cast(_, _, ?ann)) { ret ann_to_type(ann); } - case (ast.expr_if(_, _, _, _, ?ann)) { ret ann_to_type(ann); } + case (ast.expr_if(_, _, _, ?ann)) { ret ann_to_type(ann); } case (ast.expr_for(_, _, _, ?ann)) { ret ann_to_type(ann); } case (ast.expr_for_each(_, _, _, ?ann)) { ret ann_to_type(ann); } diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index 6b826f1b..1818c4fd 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -1272,28 +1272,20 @@ fn demand_expr_full(&@fn_ctxt fcx, @ty.t expected, @ast.expr e, e_1 = ast.expr_cast(sube, ast_ty, ast.ann_type(t, none[vec[@ty.t]])); } - case (ast.expr_if(?cond, ?then_0, ?elifs_0, ?else_0, ?ann)) { + case (ast.expr_if(?cond, ?then_0, ?else_0, ?ann)) { auto t = demand_full(fcx, e.span, expected, ann_to_type(ann), adk); auto then_1 = demand_block(fcx, expected, then_0); - let vec[tup(@ast.expr, ast.block)] elifs_1 = vec(); - for (tup(@ast.expr, ast.block) elif in elifs_0) { - auto elifcond = elif._0; - auto elifthn_0 = elif._1; - auto elifthn_1 = demand_block(fcx, expected, elifthn_0); - elifs_1 += tup(elifcond, elifthn_1); - } - auto else_1; alt (else_0) { - case (none[ast.block]) { else_1 = none[ast.block]; } - case (some[ast.block](?b_0)) { - auto b_1 = demand_block(fcx, expected, b_0); - else_1 = some[ast.block](b_1); + case (none[@ast.expr]) { else_1 = none[@ast.expr]; } + case (some[@ast.expr](?e_0)) { + auto e_1 = demand_expr(fcx, expected, e_0); + else_1 = some[@ast.expr](e_1); } } - e_1 = ast.expr_if(cond, then_1, elifs_1, else_1, + e_1 = ast.expr_if(cond, then_1, else_1, ast.ann_type(t, none[vec[@ty.t]])); } case (ast.expr_for(?decl, ?seq, ?bloc, ?ann)) { @@ -1875,39 +1867,24 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr { ret @fold.respan[ast.expr_](expr.span, newexpr); } - case (ast.expr_if(?cond, ?thn, ?elifs, ?elsopt, _)) { + case (ast.expr_if(?cond, ?thn, ?elsopt, _)) { auto cond_0 = check_expr(fcx, cond); auto cond_1 = demand_expr(fcx, plain_ty(ty.ty_bool), cond_0); auto thn_0 = check_block(fcx, thn); auto thn_t = block_ty(thn_0); - auto num_elifs = _vec.len[tup(@ast.expr, ast.block)](elifs); - let vec[tup(@ast.expr, ast.block)] elifs_1 = vec(); - for each (uint i in _uint.range(0u, num_elifs)) { - auto elif = elifs.(i); - auto elifcond = elif._0; - auto elifcond_0 = check_expr(fcx, cond); - auto elifcond_1 = demand_expr(fcx, - plain_ty(ty.ty_bool), - elifcond_0); - auto elifthn = elif._1; - auto elifthn_0 = check_block(fcx, elifthn); - auto elifthn_1 = demand_block(fcx, thn_t, elifthn_0); - elifs_1 += tup(elifcond_1, elifthn_1); - } - auto elsopt_1; auto elsopt_t; alt (elsopt) { - case (some[ast.block](?els)) { - auto els_0 = check_block(fcx, els); - auto els_1 = demand_block(fcx, thn_t, els_0); - elsopt_1 = some[ast.block](els_1); - elsopt_t = block_ty(els_1); - } - case (none[ast.block]) { - elsopt_1 = none[ast.block]; + case (some[@ast.expr](?els)) { + auto els_0 = check_expr(fcx, els); + auto els_1 = demand_expr(fcx, thn_t, els_0); + elsopt_1 = some[@ast.expr](els_1); + elsopt_t = expr_ty(els_1); + } + case (none[@ast.expr]) { + elsopt_1 = none[@ast.expr]; elsopt_t = plain_ty(ty.ty_nil); } } @@ -1917,7 +1894,7 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr { auto ann = ast.ann_type(elsopt_t, none[vec[@ty.t]]); ret @fold.respan[ast.expr_](expr.span, ast.expr_if(cond_1, thn_1, - elifs_1, elsopt_1, ann)); + elsopt_1, ann)); } case (ast.expr_for(?decl, ?seq, ?body, _)) { |