@@ -3792,29 +3792,28 @@ fn find_variable(&@fn_ctxt fcx, ast::node_id nid) -> ValueRef {
3792
3792
// Given a block context and a list of upvars, construct a closure that
3793
3793
// contains pointers to all of the upvars and all of the tydescs in
3794
3794
// scope. Return the ValueRef and TypeRef corresponding to the closure.
3795
- fn build_environment( & @block_ctxt cx, & ast :: node_id [ ] upvars) ->
3795
+ fn build_environment( & @block_ctxt cx, & freevar_set upvars) ->
3796
3796
rec( ValueRef ptr, TypeRef ptrty) {
3797
- auto upvar_count = std:: ivec:: len( upvars) ;
3798
3797
auto has_iterbody = !option:: is_none( cx. fcx. lliterbody) ;
3799
- if ( has_iterbody) { upvar_count += 1 u; }
3800
3798
auto llbindingsptr;
3801
3799
3802
- if ( upvar_count > 0 u) {
3800
+ if ( upvars . size ( ) > 0 u || has_iterbody ) {
3803
3801
// Gather up the upvars.
3804
3802
let ValueRef [ ] llbindings = ~[ ] ;
3805
3803
let TypeRef [ ] llbindingtys = ~[ ] ;
3806
3804
if ( has_iterbody) {
3807
3805
llbindings += ~[ option:: get( cx. fcx. lliterbody) ] ;
3808
3806
llbindingtys += ~[ val_ty( llbindings. ( 0 ) ) ] ;
3809
3807
}
3810
- for ( ast:: node_id nid in upvars) {
3808
+ for each ( ast:: node_id nid in upvars. keys ( ) ) {
3811
3809
auto llbinding = find_variable( cx. fcx, nid) ;
3812
3810
llbindings += ~[ llbinding] ;
3813
3811
llbindingtys += ~[ val_ty( llbinding) ] ;
3814
3812
}
3815
3813
3816
3814
// Create an array of bindings and copy in aliases to the upvars.
3817
3815
llbindingsptr = alloca( cx, T_struct ( llbindingtys) ) ;
3816
+ auto upvar_count = std:: ivec:: len( llbindings) ;
3818
3817
auto i = 0 u;
3819
3818
while ( i < upvar_count) {
3820
3819
auto llbindingptr =
@@ -3859,7 +3858,7 @@ fn build_environment(&@block_ctxt cx, &ast::node_id[] upvars) ->
3859
3858
// and a list of upvars, generate code to load and populate the environment
3860
3859
// with the upvars and type descriptors.
3861
3860
fn load_environment( & @block_ctxt cx, & @fn_ctxt fcx,
3862
- TypeRef llenvptrty, & ast :: node_id [ ] upvars) {
3861
+ TypeRef llenvptrty, & freevar_set upvars) {
3863
3862
auto copy_args_bcx = new_raw_block_ctxt( fcx, fcx. llcopyargs) ;
3864
3863
3865
3864
// Populate the upvars from the environment.
@@ -3871,22 +3870,20 @@ fn load_environment(&@block_ctxt cx, &@fn_ctxt fcx,
3871
3870
C_int ( abi:: closure_elt_bindings) ] ) ;
3872
3871
auto llremotebindingsptr =
3873
3872
copy_args_bcx. build. Load ( llremotebindingsptrptr) ;
3874
- auto base = 0 u ;
3873
+
3875
3874
auto i = 0 u;
3876
- auto end = std:: ivec:: len( upvars) ;
3877
3875
if ( !option:: is_none( cx. fcx. lliterbody) ) {
3878
- base += 1 u;
3876
+ i += 1 u;
3879
3877
auto lliterbodyptr =
3880
3878
copy_args_bcx. build. GEP ( llremotebindingsptr,
3881
3879
~[ C_int ( 0 ) , C_int ( 0 ) ] ) ;
3882
3880
auto lliterbody = copy_args_bcx. build. Load ( lliterbodyptr) ;
3883
3881
fcx. lliterbody = some( lliterbody) ;
3884
3882
}
3885
- while ( i < end) {
3886
- auto upvar_id = upvars. ( i) ;
3883
+ for each ( ast:: node_id upvar_id in upvars. keys( ) ) {
3887
3884
auto llupvarptrptr =
3888
3885
copy_args_bcx. build. GEP ( llremotebindingsptr,
3889
- ~[ C_int ( 0 ) , C_int ( base+ i as int) ] ) ;
3886
+ ~[ C_int ( 0 ) , C_int ( i as int) ] ) ;
3890
3887
auto llupvarptr = copy_args_bcx. build. Load ( llupvarptrptr) ;
3891
3888
fcx. llupvars. insert( upvar_id, llupvarptr) ;
3892
3889
i += 1 u;
@@ -3943,7 +3940,7 @@ fn trans_for_each(&@block_ctxt cx, &@ast::local local, &@ast::expr seq,
3943
3940
auto decl_id = local. node. id;
3944
3941
auto upvars = get_freevars( lcx. ccx. tcx, body. node. id) ;
3945
3942
3946
- auto llenv = build_environment( cx, * upvars) ;
3943
+ auto llenv = build_environment( cx, upvars) ;
3947
3944
3948
3945
// Step 2: Declare foreach body function.
3949
3946
let str s =
@@ -3964,7 +3961,7 @@ fn trans_for_each(&@block_ctxt cx, &@ast::local local, &@ast::expr seq,
3964
3961
3965
3962
// Generate code to load the environment out of the
3966
3963
// environment pointer.
3967
- load_environment( cx, fcx, llenv. ptrty, * upvars) ;
3964
+ load_environment( cx, fcx, llenv. ptrty, upvars) ;
3968
3965
3969
3966
// Add an upvar for the loop variable alias.
3970
3967
fcx. llupvars. insert( decl_id, llvm:: LLVMGetParam ( fcx. llfn, 3 u) ) ;
0 commit comments