diff options
| author | Brian Anderson <[email protected]> | 2011-03-24 21:54:19 -0400 |
|---|---|---|
| committer | Graydon Hoare <[email protected]> | 2011-03-25 11:01:52 -0700 |
| commit | ebc4df3c7add208195e84940f69648be793b328f (patch) | |
| tree | 18ef8e7b7dc9f6199679186755497dfa8421d86f /src | |
| parent | Refactor ast.local to make room for initialization via recv (diff) | |
| download | rust-ebc4df3c7add208195e84940f69648be793b328f.tar.xz rust-ebc4df3c7add208195e84940f69648be793b328f.zip | |
Implement local declarations with receive. Un-XFAIL decl-with-recv.rs.
Diffstat (limited to 'src')
| -rw-r--r-- | src/comp/front/parser.rs | 20 | ||||
| -rw-r--r-- | src/comp/middle/fold.rs | 9 | ||||
| -rw-r--r-- | src/comp/middle/trans.rs | 30 | ||||
| -rw-r--r-- | src/comp/middle/typeck.rs | 11 |
4 files changed, 51 insertions, 19 deletions
diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index 23b4a936..a903124d 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -1365,13 +1365,21 @@ impure fn parse_expr_inner(parser p) -> @ast.expr { } impure fn parse_initializer(parser p) -> option.t[ast.initializer] { - if (p.peek() == token.EQ) { - p.bump(); - ret some(rec(op = ast.init_assign, - expr = parse_expr(p))); + alt (p.peek()) { + case (token.EQ) { + p.bump(); + ret some(rec(op = ast.init_assign, + expr = parse_expr(p))); + } + case (token.LARROW) { + p.bump(); + ret some(rec(op = ast.init_recv, + expr = parse_expr(p))); + } + case (_) { + ret none[ast.initializer]; + } } - - ret none[ast.initializer]; } impure fn parse_pat(parser p) -> @ast.pat { diff --git a/src/comp/middle/fold.rs b/src/comp/middle/fold.rs index b59a1f3b..9525e58b 100644 --- a/src/comp/middle/fold.rs +++ b/src/comp/middle/fold.rs @@ -437,7 +437,7 @@ fn fold_decl[ENV](&ENV env, ast_fold[ENV] fld, @decl d) -> @decl { alt (d.node) { case (ast.decl_local(?local)) { auto ty_ = none[@ast.ty]; - auto initopt = none[ast.initializer]; + auto init_ = none[ast.initializer]; alt (local.ty) { case (some[@ast.ty](?t)) { ty_ = some[@ast.ty](fold_ty(env, fld, t)); @@ -446,13 +446,12 @@ fn fold_decl[ENV](&ENV env, ast_fold[ENV] fld, @decl d) -> @decl { } alt (local.init) { case (some[ast.initializer](?init)) { - auto init_ = rec(expr = fold_expr(env, fld, init.expr) - with init); - initopt = some[ast.initializer](init_); + auto e = fold_expr(env, fld, init.expr); + init_ = some[ast.initializer](rec(expr = e with init)); } case (_) { /* fall through */ } } - let @ast.local local_ = @rec(ty=ty_, init=initopt with *local); + let @ast.local local_ = @rec(ty=ty_, init=init_ with *local); ret fld.fold_decl_local(env_, d.span, local_); } diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index 5c7c8ff7..0acbb7e1 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -4839,22 +4839,31 @@ fn trans_recv(@block_ctxt cx, @ast.expr lhs, @ast.expr rhs, auto data = trans_lval(bcx, lhs); check (data.is_mem); bcx = data.res.bcx; + auto unit_ty = node_ann_type(bcx.fcx.ccx, ann); + + // FIXME: calculate copy init-ness in typestate. + ret recv_val(bcx, data.res.val, rhs, unit_ty, DROP_EXISTING); + } + +fn recv_val(@block_ctxt cx, ValueRef lhs, @ast.expr rhs, + @ty.t unit_ty, copy_action action) -> result { + + auto bcx = cx; auto prt = trans_expr(bcx, rhs); bcx = prt.bcx; auto sub = trans_upcall(bcx, "upcall_recv", - vec(vp2i(bcx, data.res.val), + vec(vp2i(bcx, lhs), vp2i(bcx, prt.val))); bcx = sub.bcx; - auto unit_ty = node_ann_type(cx.fcx.ccx, ann); - auto data_load = load_scalar_or_boxed(bcx, data.res.val, unit_ty); - auto cp = copy_ty(bcx, DROP_EXISTING, data.res.val, data_load, unit_ty); + auto data_load = load_scalar_or_boxed(bcx, lhs, unit_ty); + auto cp = copy_ty(bcx, action, lhs, data_load, unit_ty); bcx = cp.bcx; // TODO: Any cleanup need to be done here? - ret res(bcx, data.res.val); + ret res(bcx, lhs); } fn init_local(@block_ctxt cx, @ast.local local) -> result { @@ -4870,8 +4879,15 @@ fn init_local(@block_ctxt cx, @ast.local local) -> result { alt (local.init) { case (some[ast.initializer](?init)) { - auto sub = trans_expr(bcx, init.expr); - bcx = copy_ty(sub.bcx, INIT, llptr, sub.val, ty).bcx; + alt (init.op) { + case (ast.init_assign) { + auto sub = trans_expr(bcx, init.expr); + bcx = copy_ty(sub.bcx, INIT, llptr, sub.val, ty).bcx; + } + case (ast.init_recv) { + bcx = recv_val(bcx, llptr, init.expr, ty, INIT).bcx; + } + } } case (_) { if (middle.ty.type_has_dynamic_size(ty)) { diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index a381ae93..11309202 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -2409,7 +2409,16 @@ fn check_decl_local(&@fn_ctxt fcx, &@ast.decl decl) -> @ast.decl { case (some[ast.initializer](?init)) { auto expr_0 = check_expr(fcx, init.expr); auto lty = plain_ty(ty.ty_local(local.id)); - auto expr_1 = demand_expr(fcx, lty, expr_0); + auto expr_1; + alt (init.op) { + case (ast.init_assign) { + expr_1 = demand_expr(fcx, lty, expr_0); + } + case (ast.init_recv) { + auto port_ty = plain_ty(ty.ty_port(lty)); + expr_1 = demand_expr(fcx, port_ty, expr_0); + } + } auto init_0 = rec(expr = expr_1 with init); initopt = some[ast.initializer](init_0); } |