2011-12-19 14:50:31 -06:00
|
|
|
import syntax::{ast, ast_util};
|
2012-01-12 10:59:49 -06:00
|
|
|
import driver::session::session;
|
2012-05-19 07:52:01 -05:00
|
|
|
import syntax::codemap::span;
|
2011-12-19 14:50:31 -06:00
|
|
|
import std::map;
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2011-12-19 14:50:31 -06:00
|
|
|
|
|
|
|
export capture_mode;
|
|
|
|
export capture_var;
|
|
|
|
export capture_map;
|
|
|
|
export check_capture_clause;
|
|
|
|
export compute_capture_vars;
|
|
|
|
export cap_copy;
|
|
|
|
export cap_move;
|
|
|
|
export cap_drop;
|
|
|
|
export cap_ref;
|
|
|
|
|
2012-01-19 16:24:03 -06:00
|
|
|
enum capture_mode {
|
2012-05-19 07:52:01 -05:00
|
|
|
cap_copy, // Copy the value into the closure.
|
|
|
|
cap_move, // Move the value into the closure.
|
|
|
|
cap_drop, // Drop value after creating closure.
|
|
|
|
cap_ref, // Reference directly from parent stack frame (block fn).
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type capture_var = {
|
2012-05-19 07:52:01 -05:00
|
|
|
def: ast::def, // Variable being accessed free
|
|
|
|
span: span, // Location of access or cap item
|
|
|
|
cap_item: option<ast::capture_item>, // Capture item, if any
|
|
|
|
mode: capture_mode // How variable is being accessed
|
2011-12-19 14:50:31 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
type capture_map = map::hashmap<ast::def_id, capture_var>;
|
|
|
|
|
|
|
|
// checks the capture clause for a fn_expr() and issues warnings or
|
|
|
|
// errors for any irregularities which we identify.
|
|
|
|
fn check_capture_clause(tcx: ty::ctxt,
|
|
|
|
fn_expr_id: ast::node_id,
|
|
|
|
cap_clause: ast::capture_clause) {
|
|
|
|
let freevars = freevars::get_freevars(tcx, fn_expr_id);
|
2012-03-14 14:07:23 -05:00
|
|
|
let seen_defs = map::int_hash();
|
2011-12-19 14:50:31 -06:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
for (*cap_clause).each |cap_item| {
|
2011-12-19 14:50:31 -06:00
|
|
|
let cap_def = tcx.def_map.get(cap_item.id);
|
2012-06-30 18:19:07 -05:00
|
|
|
if !vec::any(*freevars, |fv| fv.def == cap_def ) {
|
2011-12-19 14:50:31 -06:00
|
|
|
tcx.sess.span_warn(
|
|
|
|
cap_item.span,
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("captured variable `%s` not used in closure",
|
|
|
|
tcx.sess.str_of(cap_item.name)));
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let cap_def_id = ast_util::def_id_of_def(cap_def).node;
|
|
|
|
if !seen_defs.insert(cap_def_id, ()) {
|
|
|
|
tcx.sess.span_err(
|
|
|
|
cap_item.span,
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("variable `%s` captured more than once",
|
|
|
|
tcx.sess.str_of(cap_item.name)));
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_capture_vars(tcx: ty::ctxt,
|
|
|
|
fn_expr_id: ast::node_id,
|
2012-08-10 20:15:08 -05:00
|
|
|
fn_proto: ty::fn_proto,
|
2012-06-29 18:26:56 -05:00
|
|
|
cap_clause: ast::capture_clause) -> ~[capture_var] {
|
2011-12-19 14:50:31 -06:00
|
|
|
let freevars = freevars::get_freevars(tcx, fn_expr_id);
|
2012-03-14 14:07:23 -05:00
|
|
|
let cap_map = map::int_hash();
|
2011-12-19 14:50:31 -06:00
|
|
|
|
2012-05-04 14:33:04 -05:00
|
|
|
// first add entries for anything explicitly named in the cap clause
|
2011-12-19 14:50:31 -06:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
for (*cap_clause).each |cap_item| {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("Doing capture var: %s (%?)",
|
|
|
|
tcx.sess.str_of(cap_item.name), cap_item.id);
|
2012-05-29 18:11:15 -05:00
|
|
|
|
2011-12-19 14:50:31 -06:00
|
|
|
let cap_def = tcx.def_map.get(cap_item.id);
|
|
|
|
let cap_def_id = ast_util::def_id_of_def(cap_def).node;
|
2012-05-04 14:33:04 -05:00
|
|
|
if cap_item.is_move {
|
|
|
|
// if we are moving the value in, but it's not actually used,
|
|
|
|
// must drop it.
|
2012-06-30 18:19:07 -05:00
|
|
|
if vec::any(*freevars, |fv| fv.def == cap_def ) {
|
2012-05-19 07:52:01 -05:00
|
|
|
cap_map.insert(cap_def_id, {def:cap_def,
|
|
|
|
span: cap_item.span,
|
|
|
|
cap_item: some(cap_item),
|
|
|
|
mode:cap_move});
|
2012-05-04 14:33:04 -05:00
|
|
|
} else {
|
2012-05-19 07:52:01 -05:00
|
|
|
cap_map.insert(cap_def_id, {def:cap_def,
|
|
|
|
span: cap_item.span,
|
|
|
|
cap_item: some(cap_item),
|
|
|
|
mode:cap_drop});
|
2012-05-04 14:33:04 -05:00
|
|
|
}
|
2011-12-19 14:50:31 -06:00
|
|
|
} else {
|
2012-05-04 14:33:04 -05:00
|
|
|
// if we are copying the value in, but it's not actually used,
|
|
|
|
// just ignore it.
|
2012-06-30 18:19:07 -05:00
|
|
|
if vec::any(*freevars, |fv| fv.def == cap_def ) {
|
2012-05-19 07:52:01 -05:00
|
|
|
cap_map.insert(cap_def_id, {def:cap_def,
|
|
|
|
span: cap_item.span,
|
|
|
|
cap_item: some(cap_item),
|
|
|
|
mode:cap_copy});
|
2012-05-04 14:33:04 -05:00
|
|
|
}
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-04 14:33:04 -05:00
|
|
|
// now go through anything that is referenced but was not explicitly
|
|
|
|
// named and add that
|
|
|
|
|
2012-08-10 20:15:08 -05:00
|
|
|
let implicit_mode;
|
|
|
|
if ty::is_blockish(fn_proto) {
|
|
|
|
implicit_mode = cap_ref;
|
|
|
|
} else {
|
|
|
|
implicit_mode = cap_copy;
|
|
|
|
}
|
2011-12-19 14:50:31 -06:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
do vec::iter(*freevars) |fvar| {
|
2011-12-19 14:50:31 -06:00
|
|
|
let fvar_def_id = ast_util::def_id_of_def(fvar.def).node;
|
2012-08-06 14:34:08 -05:00
|
|
|
match cap_map.find(fvar_def_id) {
|
2012-08-03 21:59:04 -05:00
|
|
|
option::some(_) => { /* was explicitly named, do nothing */ }
|
|
|
|
option::none => {
|
2012-05-19 07:52:01 -05:00
|
|
|
cap_map.insert(fvar_def_id, {def:fvar.def,
|
|
|
|
span: fvar.span,
|
|
|
|
cap_item: none,
|
|
|
|
mode:implicit_mode});
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut result = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for cap_map.each_value |cap_var| { vec::push(result, cap_var); }
|
2012-08-01 19:30:05 -05:00
|
|
|
return result;
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|