Skip to content

Commit 9432626

Browse files
committed
Eradicate fold from capture.rs
The pass now uses walk.
1 parent 0b2cfca commit 9432626

File tree

1 file changed

+54
-59
lines changed

1 file changed

+54
-59
lines changed

Diff for: src/comp/middle/capture.rs

+54-59
Original file line numberDiff line numberDiff line change
@@ -5,107 +5,102 @@ import std.option;
55
import std.option.some;
66
import std.option.none;
77
import std._int;
8+
import std._vec;
89
import util.common;
910

1011
type fn_id_of_local = std.map.hashmap[ast.def_id, ast.def_id];
11-
type env = rec(option.t[ast.def_id] current_context, // fn or obj
12+
type env = rec(mutable vec[ast.def_id] current_context, // fn or obj
1213
fn_id_of_local idmap,
1314
session.session sess);
1415

15-
fn update_env_for_item(&env e, @ast.item i) -> env {
16+
fn current_context(&env e) -> ast.def_id {
17+
ret e.current_context.(_vec.len(e.current_context) - 1u);
18+
}
19+
20+
fn enter_item(@env e, @ast.item i) {
1621
alt (i.node) {
1722
case (ast.item_fn(?name, _, _, ?id, _)) {
18-
ret rec(current_context = some(id) with e);
23+
_vec.push(e.current_context, id);
1924
}
2025
case (ast.item_obj(_, _, _, ?ids, _)) {
21-
ret rec(current_context = some(ids.ty) with e);
26+
_vec.push(e.current_context, ids.ty);
27+
}
28+
case (_) {}
29+
}
30+
}
31+
32+
fn leave_item(@env e, @ast.item i) {
33+
alt (i.node) {
34+
case (ast.item_fn(?name, _, _, ?id, _)) {
35+
_vec.pop(e.current_context);
2236
}
23-
case (_) {
24-
ret e;
37+
case (ast.item_obj(_, _, _, ?ids, _)) {
38+
_vec.pop(e.current_context);
2539
}
40+
case (_) {}
2641
}
2742
}
2843

29-
fn update_env_for_expr(&env e, @ast.expr x) -> env {
44+
fn walk_expr(@env e, @ast.expr x) {
3045
alt (x.node) {
3146
case (ast.expr_for(?d, _, _, _)) {
3247
alt (d.node) {
3348
case (ast.decl_local(?local)) {
34-
auto curr_context =
35-
option.get[ast.def_id](e.current_context);
36-
e.idmap.insert(local.id, curr_context);
37-
}
38-
case (_) {
49+
e.idmap.insert(local.id, current_context(*e));
3950
}
51+
case (_) { }
4052
}
4153
}
4254
case (ast.expr_for_each(?d, _, _, _)) {
4355
alt (d.node) {
4456
case (ast.decl_local(?local)) {
45-
auto curr_context =
46-
option.get[ast.def_id](e.current_context);
47-
e.idmap.insert(local.id, curr_context);
48-
}
49-
case (_) {
57+
e.idmap.insert(local.id, current_context(*e));
5058
}
59+
case (_) { }
60+
}
61+
}
62+
case (ast.expr_path(_, ?def, _)) {
63+
auto local_id;
64+
alt (option.get(def)) {
65+
case (ast.def_local(?id)) { local_id = id; }
66+
case (_) { ret; }
67+
}
68+
69+
auto df = ast.def_id_of_def(option.get(def));
70+
auto def_context = option.get(e.idmap.find(df));
71+
72+
if (current_context(*e) != def_context) {
73+
e.sess.span_err(x.span,
74+
"attempted dynamic environment-capture");
5175
}
5276
}
5377
case (_) { }
5478
}
55-
ret e;
5679
}
5780

58-
fn update_env_for_block(&env e, &ast.block b) -> env {
59-
auto curr_context = option.get[ast.def_id](e.current_context);
60-
81+
fn walk_block(@env e, &ast.block b) {
6182
for each (@tup(ast.ident, ast.block_index_entry) it in
6283
b.node.index.items()) {
6384
alt (it._1) {
6485
case (ast.bie_local(?local)) {
65-
e.idmap.insert(local.id, curr_context);
66-
}
67-
case (_) {
86+
e.idmap.insert(local.id, current_context(*e));
6887
}
88+
case (_) { }
6989
}
7090
}
71-
72-
ret e;
73-
}
74-
75-
fn fold_expr_path(&env e, &ast.span sp, &ast.path p, &option.t[ast.def] d,
76-
ast.ann a) -> @ast.expr {
77-
auto local_id;
78-
alt (option.get[ast.def](d)) {
79-
case (ast.def_local(?id)) {
80-
local_id = id;
81-
}
82-
case (_) {
83-
ret @fold.respan[ast.expr_](sp, ast.expr_path(p, d, a));
84-
}
85-
}
86-
87-
auto curr_context = option.get[ast.def_id](e.current_context);
88-
auto x = ast.def_id_of_def(option.get[ast.def](d));
89-
auto def_context = option.get[ast.def_id](e.idmap.find(x));
90-
91-
if (curr_context != def_context) {
92-
e.sess.span_err(sp, "attempted dynamic environment-capture");
93-
}
94-
95-
ret @fold.respan[ast.expr_](sp, ast.expr_path(p, d, a));
9691
}
9792

9893
fn check_for_captures(session.session sess, @ast.crate crate) {
99-
let fold.ast_fold[env] fld = fold.new_identity_fold[env]();
100-
fld = @rec( update_env_for_item = bind update_env_for_item(_,_),
101-
update_env_for_block = bind update_env_for_block(_,_),
102-
update_env_for_expr = bind update_env_for_expr(_,_),
103-
fold_expr_path = bind fold_expr_path(_,_,_,_,_)
104-
with *fld);
105-
auto idmap = common.new_def_hash[ast.def_id]();
106-
auto e = rec(current_context = none[ast.def_id], idmap = idmap,
107-
sess = sess);
108-
fold.fold_crate[env](e, fld, crate);
94+
let vec[ast.def_id] curctx = vec();
95+
auto env = @rec(mutable current_context = curctx,
96+
idmap = common.new_def_hash[ast.def_id](),
97+
sess = sess);
98+
auto visitor = rec(visit_item_pre = bind enter_item(env, _),
99+
visit_item_post = bind leave_item(env, _),
100+
visit_block_pre = bind walk_block(env, _),
101+
visit_expr_pre = bind walk_expr(env, _)
102+
with walk.default_visitor());
103+
walk.walk_crate(visitor, *crate);
109104
}
110105

111106
// Local Variables:

0 commit comments

Comments
 (0)