Skip to content

Commit f3717da

Browse files
committed
Modify the fn vistors in walk so that they can handle functions without names. Update the typestate code to understand this.
1 parent 142ff01 commit f3717da

File tree

7 files changed

+42
-25
lines changed

7 files changed

+42
-25
lines changed

src/comp/front/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import util::common::ty_mach;
88
import util::common::filename;
99

1010
type ident = str;
11+
// Functions may or may not have names.
12+
type fn_ident = option::t[ident];
1113

1214
type path_ = rec(vec[ident] idents, vec[@ty] types);
1315

src/comp/middle/tstate/annotate.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn collect_ids_local(&@local l, @mutable vec[node_id] rs) {
5151
vec::push(*rs, l.node.id);
5252
}
5353

54-
fn node_ids_in_fn(&_fn f, &span sp, &ident i, node_id id,
54+
fn node_ids_in_fn(&_fn f, &span sp, &fn_ident i, node_id id,
5555
@mutable vec[node_id] rs) {
5656
auto collect_ids = walk::default_visitor();
5757
collect_ids =
@@ -69,15 +69,16 @@ fn init_vecs(&crate_ctxt ccx, &vec[node_id] node_ids, uint len) {
6969
}
7070
}
7171

72-
fn visit_fn(&crate_ctxt ccx, uint num_constraints, &_fn f, &span sp, &ident i,
73-
node_id id) {
72+
fn visit_fn(&crate_ctxt ccx, uint num_constraints, &_fn f, &span sp,
73+
&fn_ident i, node_id id) {
7474
let @mutable vec[node_id] node_ids = @mutable [];
7575
node_ids_in_fn(f, sp, i, id, node_ids);
7676
auto node_id_vec = *node_ids;
7777
init_vecs(ccx, node_id_vec, num_constraints);
7878
}
7979

80-
fn annotate_in_fn(&crate_ctxt ccx, &_fn f, &span sp, &ident i, node_id id) {
80+
fn annotate_in_fn(&crate_ctxt ccx, &_fn f, &span sp, &fn_ident i,
81+
node_id id) {
8182
auto f_info = get_fn_info(ccx, id);
8283
visit_fn(ccx, num_constraints(f_info), f, sp, i, id);
8384
}

src/comp/middle/tstate/ck.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import front::ast::obj_field;
88
import front::ast::_obj;
99
import front::ast::stmt;
1010
import front::ast::ident;
11+
import front::ast::fn_ident;
1112
import front::ast::node_id;
1213
import front::ast::def_id;
1314
import front::ast::local_def;
@@ -120,7 +121,7 @@ fn check_states_stmt(&fn_ctxt fcx, &@stmt s) {
120121
}
121122

122123
fn check_states_against_conditions(&fn_ctxt fcx, &_fn f, node_id id,
123-
&span sp, &ident i) {
124+
&span sp, &fn_ident i) {
124125
/* Postorder traversal instead of pre is important
125126
because we want the smallest possible erroneous statement
126127
or expression. */
@@ -172,7 +173,7 @@ fn check_states_against_conditions(&fn_ctxt fcx, &_fn f, node_id id,
172173
}
173174
}
174175

175-
fn check_fn_states(&fn_ctxt fcx, &_fn f, node_id id, &span sp, &ident i) {
176+
fn check_fn_states(&fn_ctxt fcx, &_fn f, node_id id, &span sp, &fn_ident i) {
176177
/* Compute the pre- and post-states for this function */
177178

178179
auto g = find_pre_post_state_fn;
@@ -183,12 +184,13 @@ fn check_fn_states(&fn_ctxt fcx, &_fn f, node_id id, &span sp, &ident i) {
183184
check_states_against_conditions(fcx, f, id, sp, i);
184185
}
185186

186-
fn fn_states(&crate_ctxt ccx, &_fn f, &span sp, &ident i, node_id id) {
187+
fn fn_states(&crate_ctxt ccx, &_fn f, &span sp, &fn_ident i, node_id id) {
187188
/* Look up the var-to-bit-num map for this function */
188189

189190
assert (ccx.fm.contains_key(id));
190191
auto f_info = ccx.fm.get(id);
191-
auto fcx = rec(enclosing=f_info, id=id, name=i, ccx=ccx);
192+
auto name = option::from_maybe("anon", i);
193+
auto fcx = rec(enclosing=f_info, id=id, name=name, ccx=ccx);
192194
check_fn_states(fcx, f, id, sp, i);
193195
}
194196

src/comp/middle/tstate/collect_locals.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import std::vec;
33
import std::vec::plus_option;
44
import front::ast;
55
import front::ast::*;
6+
import pretty::ppaux::fn_ident_to_string;
67
import std::option::*;
78
import middle::walk::walk_crate;
89
import middle::walk::walk_fn;
@@ -56,7 +57,7 @@ fn collect_pred(&ctxt cx, &@expr e) {
5657
}
5758
}
5859

59-
fn find_locals(&ty::ctxt tcx, &_fn f, &span sp, &ident i, node_id id)
60+
fn find_locals(&ty::ctxt tcx, &_fn f, &span sp, &fn_ident i, node_id id)
6061
-> ctxt {
6162
let ctxt cx = rec(cs=@mutable vec::alloc(0u), tcx=tcx);
6263
auto visitor = walk::default_visitor();
@@ -103,7 +104,7 @@ fn add_constraint(&ty::ctxt tcx, aux::constr c, uint next, constr_map tbl) ->
103104

104105
/* builds a table mapping each local var defined in f
105106
to a bit number in the precondition/postcondition vectors */
106-
fn mk_fn_info(&crate_ctxt ccx, &_fn f, &span f_sp, &ident f_name,
107+
fn mk_fn_info(&crate_ctxt ccx, &_fn f, &span f_sp, &fn_ident f_name,
107108
node_id id) {
108109
auto res_map = @new_int_hash[constraint]();
109110
let uint next = 0u;
@@ -121,14 +122,15 @@ fn mk_fn_info(&crate_ctxt ccx, &_fn f, &span f_sp, &ident f_name,
121122
/* add a pseudo-entry for the function's return value
122123
we can safely use the function's name itself for this purpose */
123124

124-
add_constraint(cx.tcx, respan(f_sp, rec(id=id, c=ninit(f_name))), next,
125+
auto name = fn_ident_to_string(id, f_name);
126+
add_constraint(cx.tcx, respan(f_sp, rec(id=id, c=ninit(name))), next,
125127
res_map);
126128
auto rslt =
127129
rec(constrs=res_map,
128130
num_constraints=vec::len(*cx.cs) + 1u,
129131
cf=f.decl.cf);
130132
ccx.fm.insert(id, rslt);
131-
log f_name + " has " + uistr(num_constraints(rslt)) + " constraints";
133+
log name + " has " + uistr(num_constraints(rslt)) + " constraints";
132134
}
133135

134136

src/comp/middle/tstate/pre_post_conditions.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import util::common::log_expr_err;
7878
import util::common::log_block_err;
7979
import util::common::log_block;
8080
import util::common::span;
81+
import pretty::ppaux::fn_ident_to_string;
8182

8283
fn find_pre_post_mod(&_mod m) -> _mod {
8384
log "implement find_pre_post_mod!";
@@ -686,10 +687,11 @@ fn find_pre_post_fn(&fn_ctxt fcx, &_fn f) {
686687
}
687688
}
688689

689-
fn fn_pre_post(crate_ctxt ccx, &_fn f, &span sp, &ident i, node_id id) {
690+
fn fn_pre_post(crate_ctxt ccx, &_fn f, &span sp, &fn_ident i,
691+
node_id id) {
690692
assert (ccx.fm.contains_key(id));
691-
auto fcx = rec(enclosing=ccx.fm.get(id), id=id, name=i,
692-
ccx=ccx);
693+
auto fcx = rec(enclosing=ccx.fm.get(id), id=id,
694+
name=fn_ident_to_string(id, i), ccx=ccx);
693695
find_pre_post_fn(fcx, f);
694696
}
695697
//

src/comp/middle/walk.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import std::option::none;
66
import util::common::span;
77
import util::common::respan;
88

9-
10-
// FIXME: Should visit patterns as well.
119
type ast_visitor =
1210
rec(fn() -> bool keep_going,
1311
fn() -> bool want_crate_directives,
@@ -40,8 +38,8 @@ type ast_visitor =
4038
fn(&@ast::ty) visit_ty_pre,
4139
fn(&@ast::ty) visit_ty_post,
4240
fn(&@ast::constr) visit_constr,
43-
fn(&ast::_fn, &span, &ast::ident, ast::node_id) visit_fn_pre,
44-
fn(&ast::_fn, &span, &ast::ident, ast::node_id) visit_fn_post);
41+
fn(&ast::_fn, &span, &ast::fn_ident, ast::node_id) visit_fn_pre,
42+
fn(&ast::_fn, &span, &ast::fn_ident, ast::node_id) visit_fn_post);
4543

4644
fn walk_crate(&ast_visitor v, &ast::crate c) {
4745
if (!v.keep_going()) { ret; }
@@ -102,7 +100,7 @@ fn walk_item(&ast_visitor v, @ast::item i) {
102100
alt (i.node) {
103101
case (ast::item_const(?t, ?e)) { walk_ty(v, t); walk_expr(v, e); }
104102
case (ast::item_fn(?f, _)) {
105-
walk_fn(v, f, i.span, i.ident, i.id);
103+
walk_fn(v, f, i.span, some(i.ident), i.id);
106104
}
107105
case (ast::item_mod(?m)) { walk_mod(v, m); }
108106
case (ast::item_native_mod(?nm)) { walk_native_mod(v, nm); }
@@ -118,13 +116,15 @@ fn walk_item(&ast_visitor v, @ast::item i) {
118116
for (ast::obj_field f in ob.fields) { walk_ty(v, f.ty); }
119117
for (@ast::method m in ob.methods) {
120118
v.visit_method_pre(m);
121-
walk_fn(v, m.node.meth, m.span, m.node.ident, m.node.id);
119+
walk_fn(v, m.node.meth, m.span,
120+
some(m.node.ident), m.node.id);
122121
v.visit_method_post(m);
123122
}
124123
alt (ob.dtor) {
125124
case (none) { }
126125
case (some(?m)) {
127-
walk_fn(v, m.node.meth, m.span, m.node.ident, m.node.id);
126+
walk_fn(v, m.node.meth, m.span,
127+
some(m.node.ident), m.node.id);
128128
}
129129
}
130130
}
@@ -217,7 +217,7 @@ fn walk_fn_decl(&ast_visitor v, &ast::fn_decl fd) {
217217
walk_ty(v, fd.output);
218218
}
219219

220-
fn walk_fn(&ast_visitor v, &ast::_fn f, &span sp, &ast::ident i,
220+
fn walk_fn(&ast_visitor v, &ast::_fn f, &span sp, &ast::fn_ident i,
221221
ast::node_id d) {
222222
if (!v.keep_going()) { ret; }
223223
v.visit_fn_pre(f, sp, i, d);
@@ -405,7 +405,8 @@ fn walk_expr(&ast_visitor v, @ast::expr e) {
405405
// Methods
406406
for (@ast::method m in anon_obj.methods) {
407407
v.visit_method_pre(m);
408-
walk_fn(v, m.node.meth, m.span, m.node.ident, m.node.id);
408+
walk_fn(v, m.node.meth, m.span, some(m.node.ident),
409+
m.node.id);
409410
v.visit_method_post(m);
410411
}
411412
}
@@ -447,7 +448,7 @@ fn def_visit_ty(&@ast::ty t) { }
447448

448449
fn def_visit_constr(&@ast::constr c) { }
449450

450-
fn def_visit_fn(&ast::_fn f, &span sp, &ast::ident i, ast::node_id d) { }
451+
fn def_visit_fn(&ast::_fn f, &span sp, &ast::fn_ident i, ast::node_id d) { }
451452

452453
fn default_visitor() -> ast_visitor {
453454
ret rec(keep_going=def_keep_going,

src/comp/pretty/ppaux.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ fn mode_str_1(&ty::mode m) -> str {
3434
}
3535
}
3636

37+
fn fn_ident_to_string(ast::node_id id, &ast::fn_ident i) -> str {
38+
ret alt (i) {
39+
case (none) { "anon" + istr(id) }
40+
case (some(?s)) { s }
41+
};
42+
}
43+
3744
fn ty_to_str(&ctxt cx, &t typ) -> str {
3845
fn fn_input_to_str(&ctxt cx, &rec(middle::ty::mode mode, t ty) input) ->
3946
str {

0 commit comments

Comments
 (0)