@@ -27,6 +27,7 @@ import syntax::ast;
27
27
import syntax:: walk;
28
28
import driver:: session;
29
29
import middle:: ty;
30
+ import middle:: freevars;
30
31
import back:: link;
31
32
import back:: x86;
32
33
import back:: abi;
@@ -4107,76 +4108,6 @@ fn trans_for(&@block_ctxt cx, &@ast::local local, &@ast::expr seq,
4107
4108
4108
4109
// Iterator translation
4109
4110
4110
- // Searches through part of the AST for all references to locals or
4111
- // upvars in this frame and returns the list of definition IDs thus found.
4112
- // Since we want to be able to collect upvars in some arbitrary piece
4113
- // of the AST, we take a walker function that we invoke with a visitor
4114
- // in order to start the search.
4115
- fn collect_upvars( & @block_ctxt cx, & fn ( & walk:: ast_visitor) walker,
4116
- ast:: node_id[ ] initial_decls) -> ast:: node_id[ ] {
4117
- type env =
4118
- @rec( mutable ast:: node_id[ ] refs,
4119
- hashmap[ ast:: node_id, ( ) ] decls,
4120
- resolve:: def_map def_map,
4121
- session:: session sess) ;
4122
-
4123
- fn walk_fn( env e, & ast:: _fn f, & ast:: ty_param[ ] tps, & span sp,
4124
- & ast:: fn_ident i, ast:: node_id nid) {
4125
- for ( ast:: arg a in f. decl. inputs) { e. decls. insert( a. id, ( ) ) ; }
4126
- }
4127
- fn walk_expr( env e, & @ast:: expr expr) {
4128
- alt ( expr. node) {
4129
- case ( ast:: expr_path( ?path) ) {
4130
- if ( ! e. def_map. contains_key( expr. id) ) {
4131
- e. sess. span_fatal( expr. span,
4132
- "internal error in collect_upvars") ;
4133
- }
4134
- alt ( e. def_map. get( expr. id) ) {
4135
- case ( ast:: def_arg( ?did) ) { e. refs += ~[ did. _1] ; }
4136
- case ( ast:: def_local( ?did) ) { e. refs += ~[ did. _1] ; }
4137
- case ( ast:: def_binding( ?did) ) { e. refs += ~[ did. _1] ; }
4138
- case ( _) { /* no-op */ }
4139
- }
4140
- }
4141
- case ( _) { }
4142
- }
4143
- }
4144
- fn walk_local( env e, & @ast:: local local) {
4145
- e. decls. insert( local. node. id, ( ) ) ;
4146
- }
4147
- fn walk_pat( env e, & @ast:: pat p) {
4148
- alt ( p. node) {
4149
- case ( ast:: pat_bind( _) ) {
4150
- e. decls. insert( p. id, ( ) ) ;
4151
- }
4152
- case ( _) { }
4153
- }
4154
- }
4155
- let hashmap[ ast:: node_id, ( ) ] decls = new_int_hash[ ( ) ] ( ) ;
4156
- for ( ast:: node_id decl in initial_decls) { decls. insert( decl, ( ) ) ; }
4157
-
4158
- let env e =
4159
- @rec( mutable refs=~[ ] ,
4160
- decls=decls,
4161
- def_map=cx. fcx. lcx. ccx. tcx. def_map,
4162
- sess=cx. fcx. lcx. ccx. tcx. sess) ;
4163
- auto visitor =
4164
- @rec( visit_fn_pre=bind walk_fn( e, _, _, _, _, _) ,
4165
- visit_local_pre=bind walk_local( e, _) ,
4166
- visit_expr_pre=bind walk_expr( e, _) ,
4167
- visit_pat_pre=bind walk_pat( e, _)
4168
- with walk:: default_visitor( ) ) ;
4169
- walker( * visitor) ;
4170
- // Calculate (refs - decls). This is the set of captured upvars.
4171
-
4172
- let ast:: node_id[ ] result = ~[ ] ;
4173
- for ( ast:: node_id ref_id_ in e. refs) {
4174
- auto ref_id = ref_id_;
4175
- if ( !decls. contains_key( ref_id) ) { result += ~[ ref_id] ; }
4176
- }
4177
- ret result;
4178
- }
4179
-
4180
4111
// Finds the ValueRef associated with a variable in a function
4181
4112
// context. It checks locals, upvars, and args.
4182
4113
fn find_variable( & @fn_ctxt fcx, ast:: node_id nid) -> ValueRef {
@@ -4353,8 +4284,9 @@ fn trans_for_each(&@block_ctxt cx, &@ast::local local, &@ast::expr seq,
4353
4284
// FIXME: possibly support alias-mode here?
4354
4285
auto decl_ty = node_id_type( lcx. ccx, local. node. id) ;
4355
4286
auto decl_id = local. node. id;
4356
- auto upvars = collect_upvars( cx, bind walk:: walk_block( _, body) ,
4357
- ~[ decl_id] ) ;
4287
+ auto upvars = freevars:: collect_upvars( cx. fcx. lcx. ccx. tcx,
4288
+ bind walk:: walk_block( _, body) ,
4289
+ ~[ decl_id] ) ;
4358
4290
4359
4291
auto environment_data = build_environment( cx, upvars) ;
4360
4292
auto llenvptr = environment_data. _0;
0 commit comments