2012-01-14 18:05:07 -06:00
|
|
|
import option::*;
|
|
|
|
import pat_util::*;
|
2011-07-05 04:48:19 -05:00
|
|
|
import syntax::ast::*;
|
2011-08-21 23:44:41 -05:00
|
|
|
import syntax::ast_util::*;
|
2011-07-05 04:48:19 -05:00
|
|
|
import syntax::visit;
|
|
|
|
import syntax::codemap::span;
|
2011-08-21 23:44:41 -05:00
|
|
|
import syntax::ast_util::respan;
|
2012-01-12 10:59:49 -06:00
|
|
|
import driver::session::session;
|
2012-01-14 18:05:07 -06:00
|
|
|
import aux::*;
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2011-05-14 21:02:30 -05:00
|
|
|
|
2012-03-26 20:35:18 -05:00
|
|
|
type ctxt = {cs: @mut [sp_constr], tcx: ty::ctxt};
|
2011-05-26 18:02:25 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn collect_pred(e: @expr, cx: ctxt, v: visit::vt<ctxt>) {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt e.node {
|
2011-08-19 17:16:48 -05:00
|
|
|
expr_check(_, ch) { *cx.cs += [expr_to_constr(cx.tcx, ch)]; }
|
|
|
|
expr_if_check(ex, _, _) { *cx.cs += [expr_to_constr(cx.tcx, ex)]; }
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
// If it's a call, generate appropriate instances of the
|
|
|
|
// call's constraints.
|
2011-10-21 07:11:24 -05:00
|
|
|
expr_call(operator, operands, _) {
|
2012-04-06 13:01:43 -05:00
|
|
|
for constraints_expr(cx.tcx, operator).each {|c|
|
2011-07-27 07:19:39 -05:00
|
|
|
let ct: sp_constr =
|
|
|
|
respan(c.span,
|
|
|
|
aux::substitute_constr_args(cx.tcx, operands, c));
|
2011-08-19 17:16:48 -05:00
|
|
|
*cx.cs += [ct];
|
2011-06-01 20:10:10 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
_ { }
|
2011-06-01 20:10:10 -05:00
|
|
|
}
|
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-07-13 17:44:09 -05:00
|
|
|
|
2011-12-20 13:03:21 -06:00
|
|
|
fn find_locals(tcx: ty::ctxt,
|
2011-12-29 22:07:55 -06:00
|
|
|
fk: visit::fn_kind,
|
2011-12-20 13:03:21 -06:00
|
|
|
f_decl: fn_decl,
|
|
|
|
f_body: blk,
|
|
|
|
sp: span,
|
2011-09-12 04:27:30 -05:00
|
|
|
id: node_id) -> ctxt {
|
2012-03-26 20:35:18 -05:00
|
|
|
let cx: ctxt = {cs: @mut [], tcx: tcx};
|
2011-08-13 02:09:25 -05:00
|
|
|
let visitor = visit::default_visitor::<ctxt>();
|
2012-03-15 08:47:03 -05:00
|
|
|
let visitor =
|
2012-05-24 11:26:58 -05:00
|
|
|
@{visit_expr: collect_pred,
|
2011-12-29 22:07:55 -06:00
|
|
|
visit_fn: bind do_nothing(_, _, _, _, _, _, _)
|
2011-12-20 13:03:21 -06:00
|
|
|
with *visitor};
|
2011-12-29 22:07:55 -06:00
|
|
|
visit::visit_fn(fk, f_decl, f_body, sp,
|
|
|
|
id, cx, visit::mk_vt(visitor));
|
2011-06-01 20:10:10 -05:00
|
|
|
ret cx;
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn add_constraint(tcx: ty::ctxt, c: sp_constr, next: uint, tbl: constr_map) ->
|
|
|
|
uint {
|
2011-12-22 19:53:53 -06:00
|
|
|
log(debug,
|
2011-12-22 16:42:52 -06:00
|
|
|
constraint_to_str(tcx, c) + " |-> " + uint::str(next));
|
2012-05-24 11:26:58 -05:00
|
|
|
|
|
|
|
let {path: p, def_id: d_id, args: args} = c.node;
|
|
|
|
alt tbl.find(d_id) {
|
|
|
|
some(ct) {
|
|
|
|
let {path: _, descs: pds} = ct;
|
|
|
|
*pds += [respan(c.span, {args: args, bit_num: next})];
|
|
|
|
}
|
|
|
|
none {
|
|
|
|
let rslt: @mut [pred_args] =
|
|
|
|
@mut [respan(c.span, {args: args, bit_num: next})];
|
|
|
|
tbl.insert(d_id, {path:p, descs:rslt});
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
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-12-20 13:03:21 -06:00
|
|
|
fn mk_fn_info(ccx: crate_ctxt,
|
2011-12-29 22:07:55 -06:00
|
|
|
fk: visit::fn_kind,
|
2011-12-20 13:03:21 -06:00
|
|
|
f_decl: fn_decl,
|
|
|
|
f_body: blk,
|
|
|
|
f_sp: span,
|
|
|
|
id: node_id) {
|
2011-12-29 22:07:55 -06:00
|
|
|
let name = visit::name_of_fn(fk);
|
2012-01-09 09:24:53 -06:00
|
|
|
let res_map = new_def_hash::<constraint>();
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut next: uint = 0u;
|
2011-05-14 21:02:30 -05:00
|
|
|
|
2011-12-29 22:07:55 -06:00
|
|
|
let cx: ctxt = find_locals(ccx.tcx, fk, f_decl, f_body, f_sp, id);
|
2011-06-01 20:10:10 -05:00
|
|
|
/* now we have to add bit nums for both the constraints
|
|
|
|
and the variables... */
|
|
|
|
|
2012-04-06 13:01:43 -05:00
|
|
|
let mut i = 0u, l = vec::len(*cx.cs);
|
|
|
|
while i < l {
|
2012-05-24 11:26:58 -05:00
|
|
|
next = add_constraint(cx.tcx, copy cx.cs[i], next, res_map);
|
2012-04-06 13:01:43 -05:00
|
|
|
i += 1u;
|
2011-05-14 21:02:30 -05:00
|
|
|
}
|
2011-07-21 18:03:30 -05:00
|
|
|
/* if this function has any constraints, instantiate them to the
|
|
|
|
argument names and add them */
|
2012-04-06 13:01:43 -05:00
|
|
|
for f_decl.constraints.each {|c|
|
|
|
|
let sc = ast_constr_to_sp_constr(cx.tcx, f_decl.inputs, c);
|
2011-07-21 18:03:30 -05:00
|
|
|
next = add_constraint(cx.tcx, sc, next, res_map);
|
|
|
|
}
|
|
|
|
|
2012-03-26 20:35:18 -05:00
|
|
|
let v: @mut [node_id] = @mut [];
|
2011-07-27 07:19:39 -05:00
|
|
|
let rslt =
|
|
|
|
{constrs: res_map,
|
2011-09-28 05:23:08 -05:00
|
|
|
num_constraints: next,
|
2011-12-20 13:03:21 -06:00
|
|
|
cf: f_decl.cf,
|
2011-07-27 07:19:39 -05:00
|
|
|
used_vars: v};
|
2011-06-24 12:04:08 -05:00
|
|
|
ccx.fm.insert(id, rslt);
|
2011-12-22 18:13:40 -06:00
|
|
|
#debug("%s has %u constraints", name, num_constraints(rslt));
|
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-09-12 04:27:30 -05:00
|
|
|
fn mk_f_to_fn_info(ccx: crate_ctxt, c: @crate) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let visitor =
|
2011-12-23 11:45:02 -06:00
|
|
|
visit::mk_simple_visitor(@{visit_fn:
|
2011-12-29 22:07:55 -06:00
|
|
|
bind mk_fn_info(ccx, _, _, _, _, _)
|
2011-12-20 13:03:21 -06:00
|
|
|
with *visit::default_simple_visitor()});
|
2011-07-26 09:47:13 -05:00
|
|
|
visit::visit_crate(*c, (), visitor);
|
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
|
|
|
|
// End:
|
|
|
|
//
|