2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-17 13:41:41 -05:00
|
|
|
import std::vec;
|
|
|
|
import std::vec::plus_option;
|
2011-06-10 21:12:42 -05:00
|
|
|
import front::ast;
|
2011-06-01 20:10:10 -05:00
|
|
|
import front::ast::*;
|
2011-06-24 17:11:22 -05:00
|
|
|
import pretty::ppaux::fn_ident_to_string;
|
2011-06-22 17:41:39 -05:00
|
|
|
import std::option::*;
|
2011-05-16 19:49:46 -05:00
|
|
|
import middle::walk::walk_crate;
|
|
|
|
import middle::walk::walk_fn;
|
|
|
|
import middle::walk::ast_visitor;
|
2011-06-01 20:10:10 -05:00
|
|
|
import aux::cinit;
|
|
|
|
import aux::ninit;
|
|
|
|
import aux::npred;
|
|
|
|
import aux::cpred;
|
|
|
|
import aux::constraint;
|
2011-05-14 21:02:30 -05:00
|
|
|
import aux::fn_info;
|
|
|
|
import aux::crate_ctxt;
|
2011-06-01 20:10:10 -05:00
|
|
|
import aux::num_constraints;
|
|
|
|
import aux::constr_map;
|
2011-06-09 11:48:16 -05:00
|
|
|
import aux::expr_to_constr;
|
2011-06-10 21:12:42 -05:00
|
|
|
import aux::constraints_expr;
|
2011-06-19 15:41:21 -05:00
|
|
|
import aux::node_id_to_def_strict;
|
|
|
|
import util::common::new_int_hash;
|
2011-05-14 21:02:30 -05:00
|
|
|
import util::common::new_def_hash;
|
2011-05-18 17:43:05 -05:00
|
|
|
import util::common::uistr;
|
2011-05-31 20:24:06 -05:00
|
|
|
import util::common::span;
|
2011-06-01 20:10:10 -05:00
|
|
|
import util::common::respan;
|
2011-05-14 21:02:30 -05:00
|
|
|
|
2011-06-13 20:10:33 -05:00
|
|
|
type ctxt = rec(@mutable vec[aux::constr] cs, ty::ctxt tcx);
|
2011-05-26 18:02:25 -05:00
|
|
|
|
2011-06-30 02:18:41 -05:00
|
|
|
fn collect_local(&@local loc, &ctxt cx, &visit::vt[ctxt] v) {
|
2011-06-15 13:19:50 -05:00
|
|
|
log "collect_local: pushing " + loc.node.ident;
|
|
|
|
vec::push(*cx.cs,
|
|
|
|
respan(loc.span, rec(id=loc.node.id, c=ninit(loc.node.ident))));
|
2011-06-30 02:18:41 -05:00
|
|
|
visit::visit_local(loc, cx, v);
|
2011-05-14 21:02:30 -05:00
|
|
|
}
|
|
|
|
|
2011-06-30 02:18:41 -05:00
|
|
|
fn collect_pred(&@expr e, &ctxt cx, &visit::vt[ctxt] v) {
|
2011-06-01 20:10:10 -05:00
|
|
|
alt (e.node) {
|
2011-06-28 18:29:37 -05:00
|
|
|
case (expr_check(_, ?ch)) {
|
2011-06-21 15:16:40 -05:00
|
|
|
vec::push(*cx.cs, expr_to_constr(cx.tcx, ch));
|
2011-06-16 13:56:34 -05:00
|
|
|
}
|
2011-06-21 15:16:40 -05:00
|
|
|
case (expr_if_check(?ex, _, _)) {
|
|
|
|
vec::push(*cx.cs, expr_to_constr(cx.tcx, ex));
|
2011-06-10 21:12:42 -05:00
|
|
|
}
|
2011-06-15 17:14:30 -05:00
|
|
|
// If it's a call, generate appropriate instances of the
|
|
|
|
// call's constraints.
|
2011-06-21 15:16:40 -05:00
|
|
|
case (expr_call(?operator, ?operands)) {
|
2011-06-15 17:14:30 -05:00
|
|
|
for (@ty::constr_def c in constraints_expr(cx.tcx, operator)) {
|
|
|
|
let aux::constr ct = respan(c.span,
|
2011-06-19 15:41:21 -05:00
|
|
|
rec(id=c.node.id._1,
|
2011-06-15 17:14:30 -05:00
|
|
|
c=aux::substitute_constr_args(cx.tcx,
|
|
|
|
operands, c)));
|
|
|
|
vec::push(*cx.cs, ct);
|
2011-06-10 21:12:42 -05:00
|
|
|
}
|
2011-06-01 20:10:10 -05:00
|
|
|
}
|
|
|
|
case (_) { }
|
|
|
|
}
|
2011-06-30 02:18:41 -05:00
|
|
|
// visit subexpressions
|
|
|
|
visit::visit_expr(e, cx, v);
|
2011-06-01 20:10:10 -05:00
|
|
|
}
|
|
|
|
|
2011-06-30 11:00:44 -05:00
|
|
|
fn do_nothing(&_fn f, &vec[ty_param] tp, &span sp, &fn_ident i,
|
|
|
|
node_id iid, &ctxt cx, &visit::vt[ctxt] v) {
|
2011-06-30 02:18:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn find_locals(&ty::ctxt tcx, &_fn f, &vec[ast::ty_param] tps,
|
|
|
|
&span sp, &fn_ident i, node_id id)
|
2011-06-19 15:41:21 -05:00
|
|
|
-> ctxt {
|
2011-06-13 20:10:33 -05:00
|
|
|
let ctxt cx = rec(cs=@mutable vec::alloc(0u), tcx=tcx);
|
2011-06-30 02:18:41 -05:00
|
|
|
auto visitor = visit::default_visitor[ctxt]();
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
visitor =
|
2011-06-30 02:18:41 -05:00
|
|
|
@rec(visit_local=collect_local,
|
|
|
|
visit_expr=collect_pred,
|
2011-06-30 11:00:44 -05:00
|
|
|
visit_fn=do_nothing
|
2011-06-30 02:18:41 -05:00
|
|
|
with *visitor);
|
|
|
|
visit::visit_fn(f, tps, sp, i, id, cx, visit::vtor(visitor));
|
2011-06-01 20:10:10 -05:00
|
|
|
ret cx;
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
fn add_constraint(&ty::ctxt tcx, aux::constr c, uint next, constr_map tbl) ->
|
|
|
|
uint {
|
|
|
|
log aux::constraint_to_str(tcx, c) + " |-> " + util::common::uistr(next);
|
2011-06-13 20:10:33 -05:00
|
|
|
alt (c.node.c) {
|
2011-06-15 13:19:50 -05:00
|
|
|
case (ninit(?i)) { tbl.insert(c.node.id, cinit(next, c.span, i)); }
|
2011-06-13 20:10:33 -05:00
|
|
|
case (npred(?p, ?args)) {
|
|
|
|
alt (tbl.find(c.node.id)) {
|
|
|
|
case (some(?ct)) {
|
2011-06-01 20:10:10 -05:00
|
|
|
alt (ct) {
|
2011-06-15 13:19:50 -05:00
|
|
|
case (cinit(_, _, _)) {
|
|
|
|
tcx.sess.bug("add_constraint: same def_id used" +
|
|
|
|
" as a variable and a pred");
|
2011-06-01 20:10:10 -05:00
|
|
|
}
|
2011-06-13 20:10:33 -05:00
|
|
|
case (cpred(_, ?pds)) {
|
2011-06-15 13:19:50 -05:00
|
|
|
vec::push(*pds,
|
|
|
|
respan(c.span,
|
|
|
|
rec(args=args, bit_num=next)));
|
2011-06-01 20:10:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-13 20:10:33 -05:00
|
|
|
case (none) {
|
2011-06-15 13:19:50 -05:00
|
|
|
tbl.insert(c.node.id,
|
|
|
|
cpred(p,
|
|
|
|
@mutable [respan(c.span,
|
|
|
|
rec(args=args,
|
|
|
|
bit_num=next))]));
|
2011-06-01 20:10:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
ret next + 1u;
|
2011-05-14 21:02:30 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-14 21:02:30 -05:00
|
|
|
/* builds a table mapping each local var defined in f
|
|
|
|
to a bit number in the precondition/postcondition vectors */
|
2011-06-30 02:18:41 -05:00
|
|
|
fn mk_fn_info(&crate_ctxt ccx, &_fn f, &vec[ast::ty_param] tp,
|
|
|
|
&span f_sp, &fn_ident f_name,
|
2011-06-19 15:41:21 -05:00
|
|
|
node_id id) {
|
|
|
|
auto res_map = @new_int_hash[constraint]();
|
2011-05-14 21:02:30 -05:00
|
|
|
let uint next = 0u;
|
|
|
|
|
2011-06-30 02:18:41 -05:00
|
|
|
let ctxt cx = find_locals(ccx.tcx, f, tp, f_sp, f_name, id);
|
2011-06-01 20:10:10 -05:00
|
|
|
/* now we have to add bit nums for both the constraints
|
|
|
|
and the variables... */
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
for (aux::constr c in { *cx.cs }) {
|
2011-06-01 20:10:10 -05:00
|
|
|
next = add_constraint(cx.tcx, c, next, res_map);
|
2011-05-14 21:02:30 -05:00
|
|
|
}
|
|
|
|
/* add a pseudo-entry for the function's return value
|
|
|
|
we can safely use the function's name itself for this purpose */
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-06-24 17:11:22 -05:00
|
|
|
auto name = fn_ident_to_string(id, f_name);
|
|
|
|
add_constraint(cx.tcx, respan(f_sp, rec(id=id, c=ninit(name))), next,
|
2011-06-13 20:10:33 -05:00
|
|
|
res_map);
|
2011-06-30 02:18:41 -05:00
|
|
|
let @mutable vec[node_id] v = @mutable [];
|
2011-06-24 12:04:08 -05:00
|
|
|
auto rslt =
|
2011-06-15 13:19:50 -05:00
|
|
|
rec(constrs=res_map,
|
|
|
|
num_constraints=vec::len(*cx.cs) + 1u,
|
2011-06-30 02:18:41 -05:00
|
|
|
cf=f.decl.cf,
|
|
|
|
used_vars=v);
|
2011-06-24 12:04:08 -05:00
|
|
|
ccx.fm.insert(id, rslt);
|
2011-06-24 17:11:22 -05:00
|
|
|
log name + " has " + uistr(num_constraints(rslt)) + " constraints";
|
2011-05-14 21:02:30 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-14 21:02:30 -05:00
|
|
|
/* initializes the global fn_info_map (mapping each function ID, including
|
|
|
|
nested locally defined functions, onto a mapping from local variable name
|
|
|
|
to bit number) */
|
2011-06-15 13:19:50 -05:00
|
|
|
fn mk_f_to_fn_info(&crate_ctxt ccx, @crate c) {
|
|
|
|
let ast_visitor vars_visitor = walk::default_visitor();
|
|
|
|
vars_visitor =
|
2011-06-30 02:18:41 -05:00
|
|
|
rec(visit_fn_pre=bind mk_fn_info(ccx, _, _, _, _, _)
|
2011-06-15 13:19:50 -05:00
|
|
|
with vars_visitor);
|
|
|
|
walk_crate(vars_visitor, *c);
|
2011-05-14 21:02:30 -05:00
|
|
|
}
|
2011-05-16 19:49:46 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|
|
|
|
//
|