Skip to content

Commit 759317c

Browse files
committed
Factor out closure construction from trans_for_each.
1 parent 0957525 commit 759317c

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/comp/middle/trans.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4768,20 +4768,10 @@ fn trans_for_each(&@block_ctxt cx, &@ast::local local, &@ast::expr seq,
47684768
// Step 3: Call iter passing [lliterbody, llenv], plus other args.
47694769
alt (seq.node) {
47704770
case (ast::expr_call(?f, ?args)) {
4771-
auto pair = alloca(cx, T_fn_pair(*lcx.ccx, iter_body_llty));
4772-
auto code_cell =
4773-
cx.build.GEP(pair, ~[C_int(0), C_int(abi::fn_field_code)]);
4774-
cx.build.Store(lliterbody, code_cell);
4775-
auto env_cell =
4776-
cx.build.GEP(pair, ~[C_int(0), C_int(abi::fn_field_box)]);
4777-
auto llenvblobptr =
4778-
cx.build.PointerCast(llenvptr,
4779-
T_opaque_closure_ptr(*lcx.ccx));
4780-
cx.build.Store(llenvblobptr, env_cell);
4781-
// log "lliterbody: " + val_str(lcx.ccx.tn, lliterbody);
4782-
4783-
r = trans_call(cx, f, some[ValueRef](cx.build.Load(pair)), args,
4784-
seq.id);
4771+
auto pair = create_real_fn_pair(cx, iter_body_llty,
4772+
lliterbody, llenvptr);
4773+
r = trans_call(cx, f, some[ValueRef](cx.build.Load(pair)),
4774+
args, seq.id);
47854775
ret rslt(r.bcx, C_nil());
47864776
}
47874777
}
@@ -8714,6 +8704,26 @@ fn create_fn_pair(&@crate_ctxt cx, str ps, TypeRef llfnty, ValueRef llfn,
87148704
ret gvar;
87158705
}
87168706

8707+
// Create a /real/ closure: this is like create_fn_pair, but creates a
8708+
// a fn value on the stack with a specified environment (which need not be
8709+
// on the stack).
8710+
fn create_real_fn_pair(&@block_ctxt cx, TypeRef llfnty,
8711+
ValueRef llfn, ValueRef llenvptr) -> ValueRef {
8712+
auto lcx = cx.fcx.lcx;
8713+
8714+
auto pair = alloca(cx, T_fn_pair(*lcx.ccx, llfnty));
8715+
auto code_cell =
8716+
cx.build.GEP(pair, ~[C_int(0), C_int(abi::fn_field_code)]);
8717+
cx.build.Store(llfn, code_cell);
8718+
auto env_cell =
8719+
cx.build.GEP(pair, ~[C_int(0), C_int(abi::fn_field_box)]);
8720+
auto llenvblobptr =
8721+
cx.build.PointerCast(llenvptr,
8722+
T_opaque_closure_ptr(*lcx.ccx));
8723+
cx.build.Store(llenvblobptr, env_cell);
8724+
ret pair;
8725+
}
8726+
87178727
fn register_fn_pair(&@crate_ctxt cx, str ps, TypeRef llfnty, ValueRef llfn,
87188728
ast::node_id id) {
87198729
// FIXME: We should also hide the unexported pairs in crates.

0 commit comments

Comments
 (0)