2012-03-12 22:04:27 -05:00
|
|
|
import libc::c_uint;
|
2011-12-14 15:57:10 -06:00
|
|
|
import syntax::ast;
|
|
|
|
import syntax::ast_util;
|
2011-12-15 13:06:48 -06:00
|
|
|
import lib::llvm::llvm;
|
2012-02-01 04:04:56 -06:00
|
|
|
import lib::llvm::{ValueRef, TypeRef};
|
2012-01-27 06:17:06 -06:00
|
|
|
import common::*;
|
|
|
|
import build::*;
|
|
|
|
import base::*;
|
2012-02-21 08:22:55 -06:00
|
|
|
import type_of::*;
|
|
|
|
import type_of::type_of; // Issue #1873
|
2011-12-14 15:57:10 -06:00
|
|
|
import back::abi;
|
|
|
|
import syntax::codemap::span;
|
2012-01-08 15:32:40 -06:00
|
|
|
import syntax::print::pprust::expr_to_str;
|
2011-12-15 13:06:48 -06:00
|
|
|
import back::link::{
|
|
|
|
mangle_internal_name_by_path,
|
|
|
|
mangle_internal_name_by_path_and_seq};
|
2012-01-06 17:12:42 -06:00
|
|
|
import util::ppaux::ty_to_str;
|
2012-05-22 01:34:34 -05:00
|
|
|
import syntax::ast_map::{path, path_mod, path_name};
|
2012-02-09 07:33:00 -06:00
|
|
|
import driver::session::session;
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2012-05-25 02:14:40 -05:00
|
|
|
import dvec::extensions;
|
2011-12-14 15:57:10 -06:00
|
|
|
|
2011-12-15 13:06:48 -06:00
|
|
|
// ___Good to know (tm)__________________________________________________
|
|
|
|
//
|
|
|
|
// The layout of a closure environment in memory is
|
|
|
|
// roughly as follows:
|
|
|
|
//
|
2012-02-01 20:52:08 -06:00
|
|
|
// struct rust_opaque_box { // see rust_internal.h
|
|
|
|
// unsigned ref_count; // only used for fn@()
|
|
|
|
// type_desc *tydesc; // describes closure_data struct
|
|
|
|
// rust_opaque_box *prev; // (used internally by memory alloc)
|
|
|
|
// rust_opaque_box *next; // (used internally by memory alloc)
|
|
|
|
// struct closure_data {
|
|
|
|
// type_desc *bound_tdescs[]; // bound descriptors
|
|
|
|
// struct {
|
|
|
|
// upvar1_t upvar1;
|
|
|
|
// ...
|
|
|
|
// upvarN_t upvarN;
|
|
|
|
// } bound_data;
|
|
|
|
// }
|
2011-12-15 13:06:48 -06:00
|
|
|
// };
|
|
|
|
//
|
2012-02-01 20:52:08 -06:00
|
|
|
// Note that the closure is itself a rust_opaque_box. This is true
|
|
|
|
// even for fn~ and fn&, because we wish to keep binary compatibility
|
|
|
|
// between all kinds of closures. The allocation strategy for this
|
|
|
|
// closure depends on the closure type. For a sendfn, the closure
|
|
|
|
// (and the referenced type descriptors) will be allocated in the
|
|
|
|
// exchange heap. For a fn, the closure is allocated in the task heap
|
|
|
|
// and is reference counted. For a block, the closure is allocated on
|
|
|
|
// the stack.
|
2011-12-15 13:06:48 -06:00
|
|
|
//
|
2012-02-01 20:52:08 -06:00
|
|
|
// ## Opaque closures and the embedded type descriptor ##
|
2012-01-05 18:19:12 -06:00
|
|
|
//
|
|
|
|
// One interesting part of closures is that they encapsulate the data
|
|
|
|
// that they close over. So when I have a ptr to a closure, I do not
|
|
|
|
// know how many type descriptors it contains nor what upvars are
|
|
|
|
// captured within. That means I do not know precisely how big it is
|
|
|
|
// nor where its fields are located. This is called an "opaque
|
|
|
|
// closure".
|
|
|
|
//
|
2012-02-01 20:52:08 -06:00
|
|
|
// Typically an opaque closure suffices because we only manipulate it
|
|
|
|
// by ptr. The routine common::T_opaque_box_ptr() returns an
|
|
|
|
// appropriate type for such an opaque closure; it allows access to
|
|
|
|
// the box fields, but not the closure_data itself.
|
2012-01-05 18:19:12 -06:00
|
|
|
//
|
|
|
|
// But sometimes, such as when cloning or freeing a closure, we need
|
|
|
|
// to know the full information. That is where the type descriptor
|
|
|
|
// that defines the closure comes in handy. We can use its take and
|
|
|
|
// drop glue functions to allocate/free data as needed.
|
|
|
|
//
|
|
|
|
// ## Subtleties concerning alignment ##
|
|
|
|
//
|
2012-02-01 20:52:08 -06:00
|
|
|
// It is important that we be able to locate the closure data *without
|
|
|
|
// knowing the kind of data that is being bound*. This can be tricky
|
|
|
|
// because the alignment requirements of the bound data affects the
|
|
|
|
// alignment requires of the closure_data struct as a whole. However,
|
|
|
|
// right now this is a non-issue in any case, because the size of the
|
|
|
|
// rust_opaque_box header is always a mutiple of 16-bytes, which is
|
|
|
|
// the maximum alignment requirement we ever have to worry about.
|
2012-01-05 18:19:12 -06:00
|
|
|
//
|
2012-02-01 20:52:08 -06:00
|
|
|
// The only reason alignment matters is that, in order to learn what data
|
|
|
|
// is bound, we would normally first load the type descriptors: but their
|
|
|
|
// location is ultimately depend on their content! There is, however, a
|
|
|
|
// workaround. We can load the tydesc from the rust_opaque_box, which
|
|
|
|
// describes the closure_data struct and has self-contained derived type
|
|
|
|
// descriptors, and read the alignment from there. It's just annoying to
|
|
|
|
// do. Hopefully should this ever become an issue we'll have monomorphized
|
|
|
|
// and type descriptors will all be a bad dream.
|
2012-01-05 18:19:12 -06:00
|
|
|
//
|
2011-12-15 13:06:48 -06:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2012-01-19 16:24:03 -06:00
|
|
|
enum environment_value {
|
2011-12-19 14:50:31 -06:00
|
|
|
// Copy the value from this llvm ValueRef into the environment.
|
2012-01-19 19:56:05 -06:00
|
|
|
env_copy(ValueRef, ty::t, lval_kind),
|
2011-12-19 14:50:31 -06:00
|
|
|
|
|
|
|
// Move the value from this llvm ValueRef into the environment.
|
2012-01-19 19:56:05 -06:00
|
|
|
env_move(ValueRef, ty::t, lval_kind),
|
2011-12-19 14:50:31 -06:00
|
|
|
|
|
|
|
// Access by reference (used for blocks).
|
2012-01-19 19:56:05 -06:00
|
|
|
env_ref(ValueRef, ty::t, lval_kind),
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn ev_to_str(ccx: @crate_ctxt, ev: environment_value) -> ~str {
|
2012-01-08 15:32:40 -06:00
|
|
|
alt ev {
|
|
|
|
env_copy(v, t, lk) { #fmt("copy(%s,%s)", val_str(ccx.tn, v),
|
|
|
|
ty_to_str(ccx.tcx, t)) }
|
|
|
|
env_move(v, t, lk) { #fmt("move(%s,%s)", val_str(ccx.tn, v),
|
|
|
|
ty_to_str(ccx.tcx, t)) }
|
|
|
|
env_ref(v, t, lk) { #fmt("ref(%s,%s)", val_str(ccx.tn, v),
|
|
|
|
ty_to_str(ccx.tcx, t)) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 20:52:08 -06:00
|
|
|
fn mk_tuplified_uniq_cbox_ty(tcx: ty::ctxt, cdata_ty: ty::t) -> ty::t {
|
2012-05-08 18:30:00 -05:00
|
|
|
let cbox_ty = tuplify_box_ty(tcx, cdata_ty);
|
2012-02-01 20:52:08 -06:00
|
|
|
ret ty::mk_imm_uniq(tcx, cbox_ty);
|
|
|
|
}
|
|
|
|
|
2012-01-05 18:19:12 -06:00
|
|
|
// Given a closure ty, emits a corresponding tuple ty
|
|
|
|
fn mk_closure_tys(tcx: ty::ctxt,
|
2012-06-29 18:26:56 -05:00
|
|
|
bound_values: ~[environment_value])
|
2012-07-09 21:29:14 -05:00
|
|
|
-> ty::t {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut bound_tys = ~[];
|
2012-01-05 18:19:12 -06:00
|
|
|
|
|
|
|
// Compute the closed over data
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(bound_values) |bv| {
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(bound_tys, alt bv {
|
2012-01-05 18:19:12 -06:00
|
|
|
env_copy(_, t, _) { t }
|
|
|
|
env_move(_, t, _) { t }
|
|
|
|
env_ref(_, t, _) { t }
|
2012-06-26 02:39:18 -05:00
|
|
|
});
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
2012-07-09 21:43:06 -05:00
|
|
|
let cdata_ty = ty::mk_tup(tcx, bound_tys);
|
2012-02-01 20:52:08 -06:00
|
|
|
#debug["cdata_ty=%s", ty_to_str(tcx, cdata_ty)];
|
2012-07-09 21:29:14 -05:00
|
|
|
ret cdata_ty;
|
2011-12-15 13:06:48 -06:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn allocate_cbox(bcx: block,
|
2012-01-05 18:19:12 -06:00
|
|
|
ck: ty::closure_kind,
|
2012-02-01 20:52:08 -06:00
|
|
|
cdata_ty: ty::t)
|
2012-07-09 20:03:37 -05:00
|
|
|
-> ValueRef {
|
2012-07-14 00:57:48 -05:00
|
|
|
let _icx = bcx.insn_ctxt(~"closure::allocate_cbox");
|
2012-02-21 07:20:18 -06:00
|
|
|
let ccx = bcx.ccx(), tcx = ccx.tcx;
|
2012-01-05 18:19:12 -06:00
|
|
|
|
2012-06-27 16:10:24 -05:00
|
|
|
fn nuke_ref_count(bcx: block, llbox: ValueRef) {
|
2012-07-14 00:57:48 -05:00
|
|
|
let _icx = bcx.insn_ctxt(~"closure::nuke_ref_count");
|
2012-02-01 20:52:08 -06:00
|
|
|
// Initialize ref count to arbitrary value for debugging:
|
2012-02-21 07:20:18 -06:00
|
|
|
let ccx = bcx.ccx();
|
2012-06-27 16:10:24 -05:00
|
|
|
let llbox = PointerCast(bcx, llbox, T_opaque_box_ptr(ccx));
|
2012-06-29 18:26:56 -05:00
|
|
|
let ref_cnt = GEPi(bcx, llbox, ~[0u, abi::box_field_refcnt]);
|
2012-02-01 20:52:08 -06:00
|
|
|
let rc = C_int(ccx, 0x12345678);
|
|
|
|
Store(bcx, rc, ref_cnt);
|
|
|
|
}
|
2012-02-01 20:50:19 -06:00
|
|
|
|
2012-02-01 20:52:08 -06:00
|
|
|
// Allocate and initialize the box:
|
2012-07-09 20:03:37 -05:00
|
|
|
let llbox = alt ck {
|
2012-01-19 00:37:22 -06:00
|
|
|
ty::ck_box {
|
2012-07-09 20:03:37 -05:00
|
|
|
malloc_raw(bcx, cdata_ty, heap_shared)
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
ty::ck_uniq {
|
2012-07-09 20:03:37 -05:00
|
|
|
malloc_raw(bcx, cdata_ty, heap_exchange)
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
ty::ck_block {
|
2012-02-01 20:52:08 -06:00
|
|
|
let cbox_ty = tuplify_box_ty(tcx, cdata_ty);
|
2012-06-27 16:10:24 -05:00
|
|
|
let llbox = base::alloc_ty(bcx, cbox_ty);
|
|
|
|
nuke_ref_count(bcx, llbox);
|
2012-07-09 20:03:37 -05:00
|
|
|
llbox
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-09 20:03:37 -05:00
|
|
|
ret llbox;
|
2011-12-15 13:06:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type closure_result = {
|
2012-01-05 18:19:12 -06:00
|
|
|
llbox: ValueRef, // llvalue of ptr to closure
|
2012-02-01 20:52:08 -06:00
|
|
|
cdata_ty: ty::t, // type of the closure data
|
2012-02-17 06:17:40 -06:00
|
|
|
bcx: block // final bcx
|
2011-12-15 13:06:48 -06:00
|
|
|
};
|
|
|
|
|
2011-12-14 15:57:10 -06:00
|
|
|
// Given a block context and a list of tydescs and values to bind
|
|
|
|
// construct a closure out of them. If copying is true, it is a
|
|
|
|
// heap allocated closure that copies the upvars into environment.
|
|
|
|
// Otherwise, it is stack allocated and copies pointers to the upvars.
|
2012-03-09 03:47:40 -06:00
|
|
|
fn store_environment(bcx: block,
|
2012-06-29 18:26:56 -05:00
|
|
|
bound_values: ~[environment_value],
|
2012-03-09 03:47:40 -06:00
|
|
|
ck: ty::closure_kind) -> closure_result {
|
2012-07-14 00:57:48 -05:00
|
|
|
let _icx = bcx.insn_ctxt(~"closure::store_environment");
|
2012-02-21 07:20:18 -06:00
|
|
|
let ccx = bcx.ccx(), tcx = ccx.tcx;
|
2011-12-14 15:57:10 -06:00
|
|
|
|
2012-01-05 18:19:12 -06:00
|
|
|
// compute the shape of the closure
|
2012-07-09 21:29:14 -05:00
|
|
|
let cdata_ty = mk_closure_tys(tcx, bound_values);
|
2011-12-14 15:57:10 -06:00
|
|
|
|
2012-01-05 18:19:12 -06:00
|
|
|
// allocate closure in the heap
|
2012-07-09 20:03:37 -05:00
|
|
|
let llbox = allocate_cbox(bcx, ck, cdata_ty);
|
|
|
|
let mut temp_cleanups = ~[];
|
2011-12-15 13:06:48 -06:00
|
|
|
|
2012-01-05 18:19:12 -06:00
|
|
|
// cbox_ty has the form of a tuple: (a, b, c) we want a ptr to a
|
|
|
|
// tuple. This could be a ptr in uniq or a box or on stack,
|
|
|
|
// whatever.
|
2012-02-01 20:52:08 -06:00
|
|
|
let cbox_ty = tuplify_box_ty(tcx, cdata_ty);
|
2012-02-15 13:25:39 -06:00
|
|
|
let cboxptr_ty = ty::mk_ptr(tcx, {ty:cbox_ty, mutbl:ast::m_imm});
|
2012-03-12 04:05:15 -05:00
|
|
|
|
|
|
|
let llbox = PointerCast(bcx, llbox, type_of(ccx, cboxptr_ty));
|
2012-02-14 17:21:53 -06:00
|
|
|
#debug["tuplify_box_ty = %s", ty_to_str(tcx, cbox_ty)];
|
2011-12-15 13:06:48 -06:00
|
|
|
|
2011-12-14 15:57:10 -06:00
|
|
|
// Copy expr values into boxed bindings.
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut bcx = bcx;
|
2012-06-30 18:19:07 -05:00
|
|
|
do vec::iteri(bound_values) |i, bv| {
|
2012-02-14 17:21:53 -06:00
|
|
|
#debug["Copy %s into closure", ev_to_str(ccx, bv)];
|
|
|
|
|
2012-05-17 23:53:49 -05:00
|
|
|
if !ccx.sess.no_asm_comments() {
|
2012-01-08 15:32:40 -06:00
|
|
|
add_comment(bcx, #fmt("Copy %s into closure",
|
|
|
|
ev_to_str(ccx, bv)));
|
|
|
|
}
|
|
|
|
|
2012-03-12 03:26:54 -05:00
|
|
|
let bound_data = GEPi(bcx, llbox,
|
2012-07-09 21:43:06 -05:00
|
|
|
~[0u, abi::box_field_body, i]);
|
2011-12-14 15:57:10 -06:00
|
|
|
alt bv {
|
2012-07-16 22:17:57 -05:00
|
|
|
env_copy(val, ty, lv_owned) {
|
2011-12-19 14:50:31 -06:00
|
|
|
let val1 = load_if_immediate(bcx, val, ty);
|
2012-01-27 06:17:06 -06:00
|
|
|
bcx = base::copy_val(bcx, INIT, bound_data, val1, ty);
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
2012-07-16 22:17:57 -05:00
|
|
|
env_copy(val, ty, lv_owned_imm) {
|
2012-01-27 06:17:06 -06:00
|
|
|
bcx = base::copy_val(bcx, INIT, bound_data, val, ty);
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
2012-07-16 22:17:57 -05:00
|
|
|
env_copy(_, _, lv_temporary) {
|
2012-07-14 00:57:48 -05:00
|
|
|
fail ~"cannot capture temporary upvar";
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
|
|
|
env_move(val, ty, kind) {
|
|
|
|
let src = {bcx:bcx, val:val, kind:kind};
|
2012-01-05 18:19:12 -06:00
|
|
|
bcx = move_val(bcx, INIT, bound_data, src, ty);
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
2012-07-16 22:17:57 -05:00
|
|
|
env_ref(val, ty, lv_owned) {
|
2012-05-22 12:54:12 -05:00
|
|
|
#debug["> storing %s into %s",
|
|
|
|
val_str(bcx.ccx().tn, val),
|
|
|
|
val_str(bcx.ccx().tn, bound_data)];
|
2012-01-05 18:19:12 -06:00
|
|
|
Store(bcx, val, bound_data);
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
2012-07-16 22:17:57 -05:00
|
|
|
env_ref(val, ty, lv_owned_imm) {
|
2011-12-19 14:50:31 -06:00
|
|
|
let addr = do_spill_noroot(bcx, val);
|
2012-01-05 18:19:12 -06:00
|
|
|
Store(bcx, addr, bound_data);
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
2012-07-16 22:17:57 -05:00
|
|
|
env_ref(_, _, lv_temporary) {
|
2012-07-14 00:57:48 -05:00
|
|
|
fail ~"cannot capture temporary upvar";
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(temp_cleanups) |cleanup| { revoke_clean(bcx, cleanup); }
|
2011-12-14 15:57:10 -06:00
|
|
|
|
2012-02-01 20:52:08 -06:00
|
|
|
ret {llbox: llbox, cdata_ty: cdata_ty, bcx: bcx};
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Given a context and a list of upvars, build a closure. This just
|
2011-12-15 13:06:48 -06:00
|
|
|
// collects the upvars and packages them up for store_environment.
|
2012-02-17 06:17:40 -06:00
|
|
|
fn build_closure(bcx0: block,
|
2012-06-29 18:26:56 -05:00
|
|
|
cap_vars: ~[capture::capture_var],
|
2012-02-28 15:40:35 -06:00
|
|
|
ck: ty::closure_kind,
|
2012-03-27 05:33:13 -05:00
|
|
|
id: ast::node_id,
|
|
|
|
include_ret_handle: option<ValueRef>) -> closure_result {
|
2012-07-14 00:57:48 -05:00
|
|
|
let _icx = bcx0.insn_ctxt(~"closure::build_closure");
|
2011-12-14 15:57:10 -06:00
|
|
|
// If we need to, package up the iterator body to call
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut env_vals = ~[];
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut bcx = bcx0;
|
|
|
|
let ccx = bcx.ccx(), tcx = ccx.tcx;
|
2011-12-19 14:50:31 -06:00
|
|
|
|
|
|
|
// Package up the captured upvars
|
2012-06-30 18:19:07 -05:00
|
|
|
do vec::iter(cap_vars) |cap_var| {
|
2012-02-27 18:05:17 -06:00
|
|
|
#debug["Building closure: captured variable %?", cap_var];
|
2011-12-19 14:50:31 -06:00
|
|
|
let lv = trans_local_var(bcx, cap_var.def);
|
|
|
|
let nid = ast_util::def_id_of_def(cap_var.def).node;
|
2012-05-22 12:54:12 -05:00
|
|
|
#debug["Node id is %s",
|
|
|
|
syntax::ast_map::node_id_to_str(bcx.ccx().tcx.items, nid)];
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut ty = node_id_type(bcx, nid);
|
2011-12-19 14:50:31 -06:00
|
|
|
alt cap_var.mode {
|
2012-01-19 03:03:57 -06:00
|
|
|
capture::cap_ref {
|
2012-01-10 08:49:15 -06:00
|
|
|
assert ck == ty::ck_block;
|
2011-12-19 14:50:31 -06:00
|
|
|
ty = ty::mk_mut_ptr(tcx, ty);
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(env_vals, env_ref(lv.val, ty, lv.kind));
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
2012-01-19 03:03:57 -06:00
|
|
|
capture::cap_copy {
|
2012-05-25 02:14:40 -05:00
|
|
|
let mv = alt check ccx.maps.last_use_map.find(id) {
|
2012-02-28 15:40:35 -06:00
|
|
|
none { false }
|
2012-05-25 02:14:40 -05:00
|
|
|
some(vars) { (*vars).contains(nid) }
|
2012-02-28 15:40:35 -06:00
|
|
|
};
|
2012-06-26 02:39:18 -05:00
|
|
|
if mv { vec::push(env_vals, env_move(lv.val, ty, lv.kind)); }
|
|
|
|
else { vec::push(env_vals, env_copy(lv.val, ty, lv.kind)); }
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
2012-01-19 03:03:57 -06:00
|
|
|
capture::cap_move {
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(env_vals, env_move(lv.val, ty, lv.kind));
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
2012-01-19 03:03:57 -06:00
|
|
|
capture::cap_drop {
|
2012-07-16 22:17:57 -05:00
|
|
|
assert lv.kind == lv_owned;
|
2011-12-19 14:50:31 -06:00
|
|
|
bcx = drop_ty(bcx, lv.val, ty);
|
2012-05-28 22:52:11 -05:00
|
|
|
bcx = zero_mem(bcx, lv.val, ty);
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
}
|
2012-06-30 18:19:07 -05:00
|
|
|
do option::iter(include_ret_handle) |flagptr| {
|
2012-03-27 05:33:13 -05:00
|
|
|
let our_ret = alt bcx.fcx.loop_ret {
|
|
|
|
some({retptr, _}) { retptr }
|
|
|
|
none { bcx.fcx.llretptr }
|
|
|
|
};
|
|
|
|
let nil_ret = PointerCast(bcx, our_ret, T_ptr(T_nil()));
|
2012-06-28 15:52:13 -05:00
|
|
|
vec::push(env_vals,
|
|
|
|
env_ref(flagptr,
|
2012-07-16 22:17:57 -05:00
|
|
|
ty::mk_mut_ptr(tcx, ty::mk_bool(tcx)), lv_owned));
|
2012-06-28 15:52:13 -05:00
|
|
|
vec::push(env_vals,
|
2012-07-16 22:17:57 -05:00
|
|
|
env_ref(nil_ret, ty::mk_nil_ptr(tcx), lv_owned));
|
2012-03-27 05:33:13 -05:00
|
|
|
}
|
2012-03-09 03:47:40 -06:00
|
|
|
ret store_environment(bcx, env_vals, ck);
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Given an enclosing block context, a new function context, a closure type,
|
|
|
|
// and a list of upvars, generate code to load and populate the environment
|
|
|
|
// with the upvars and type descriptors.
|
2012-03-08 12:02:22 -06:00
|
|
|
fn load_environment(fcx: fn_ctxt,
|
2012-02-01 20:52:08 -06:00
|
|
|
cdata_ty: ty::t,
|
2012-06-29 18:26:56 -05:00
|
|
|
cap_vars: ~[capture::capture_var],
|
2012-03-27 05:33:13 -05:00
|
|
|
load_ret_handle: bool,
|
2011-12-15 13:06:48 -06:00
|
|
|
ck: ty::closure_kind) {
|
2012-07-14 00:57:48 -05:00
|
|
|
let _icx = fcx.insn_ctxt(~"closure::load_environment");
|
2012-02-17 06:17:40 -06:00
|
|
|
let bcx = raw_block(fcx, fcx.llloadenv);
|
2012-01-05 18:19:12 -06:00
|
|
|
|
2012-02-01 20:52:08 -06:00
|
|
|
// Load a pointer to the closure data, skipping over the box header:
|
|
|
|
let llcdata = base::opaque_box_body(bcx, cdata_ty, fcx.llenv);
|
2011-12-14 15:57:10 -06:00
|
|
|
|
|
|
|
// Populate the upvars from the environment.
|
2012-05-04 00:31:38 -05:00
|
|
|
let mut i = 0u;
|
2012-06-30 18:19:07 -05:00
|
|
|
do vec::iter(cap_vars) |cap_var| {
|
2011-12-19 14:50:31 -06:00
|
|
|
alt cap_var.mode {
|
2012-01-19 03:03:57 -06:00
|
|
|
capture::cap_drop { /* ignore */ }
|
2011-12-19 14:50:31 -06:00
|
|
|
_ {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut upvarptr =
|
2012-07-09 21:43:06 -05:00
|
|
|
GEPi(bcx, llcdata, ~[0u, i]);
|
2011-12-19 14:50:31 -06:00
|
|
|
alt ck {
|
2012-03-12 03:26:54 -05:00
|
|
|
ty::ck_block { upvarptr = Load(bcx, upvarptr); }
|
2012-01-19 00:37:22 -06:00
|
|
|
ty::ck_uniq | ty::ck_box { }
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
|
|
|
let def_id = ast_util::def_id_of_def(cap_var.def);
|
2012-03-12 03:26:54 -05:00
|
|
|
fcx.llupvars.insert(def_id.node, upvarptr);
|
2012-05-04 00:31:38 -05:00
|
|
|
i += 1u;
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 05:33:13 -05:00
|
|
|
if load_ret_handle {
|
|
|
|
let flagptr = Load(bcx, GEPi(bcx, llcdata,
|
2012-07-09 21:43:06 -05:00
|
|
|
~[0u, i]));
|
2012-06-25 22:00:46 -05:00
|
|
|
let retptr = Load(bcx,
|
|
|
|
GEPi(bcx, llcdata,
|
2012-07-09 21:43:06 -05:00
|
|
|
~[0u, i+1u]));
|
2012-03-27 05:33:13 -05:00
|
|
|
fcx.loop_ret = some({flagptr: flagptr, retptr: retptr});
|
|
|
|
}
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn trans_expr_fn(bcx: block,
|
2011-12-29 22:07:55 -06:00
|
|
|
proto: ast::proto,
|
2011-12-22 10:49:54 -06:00
|
|
|
decl: ast::fn_decl,
|
|
|
|
body: ast::blk,
|
2011-12-19 14:50:31 -06:00
|
|
|
id: ast::node_id,
|
|
|
|
cap_clause: ast::capture_clause,
|
2012-03-27 05:33:13 -05:00
|
|
|
is_loop_body: option<option<ValueRef>>,
|
2012-02-17 06:17:40 -06:00
|
|
|
dest: dest) -> block {
|
2012-07-14 00:57:48 -05:00
|
|
|
let _icx = bcx.insn_ctxt(~"closure::trans_expr_fn");
|
2011-12-14 15:57:10 -06:00
|
|
|
if dest == ignore { ret bcx; }
|
2012-02-21 07:20:18 -06:00
|
|
|
let ccx = bcx.ccx(), bcx = bcx;
|
2012-02-02 05:37:17 -06:00
|
|
|
let fty = node_id_type(bcx, id);
|
2012-03-09 03:47:40 -06:00
|
|
|
let llfnty = type_of_fn_from_ty(ccx, fty);
|
2012-07-14 00:57:48 -05:00
|
|
|
let sub_path = vec::append_one(bcx.fcx.path, path_name(@~"anon"));
|
2012-02-03 02:53:37 -06:00
|
|
|
let s = mangle_internal_name_by_path(ccx, sub_path);
|
2011-12-14 15:57:10 -06:00
|
|
|
let llfn = decl_internal_cdecl_fn(ccx.llmod, s, llfnty);
|
|
|
|
|
2012-01-09 18:12:37 -06:00
|
|
|
let trans_closure_env = fn@(ck: ty::closure_kind) -> ValueRef {
|
2011-12-19 14:50:31 -06:00
|
|
|
let cap_vars = capture::compute_capture_vars(
|
2011-12-29 22:07:55 -06:00
|
|
|
ccx.tcx, id, proto, cap_clause);
|
2012-03-27 05:33:13 -05:00
|
|
|
let ret_handle = alt is_loop_body { some(x) { x } none { none } };
|
|
|
|
let {llbox, cdata_ty, bcx} = build_closure(bcx, cap_vars, ck, id,
|
|
|
|
ret_handle);
|
2012-03-08 06:30:22 -06:00
|
|
|
trans_closure(ccx, sub_path, decl, body, llfn, no_self,
|
2012-06-30 18:19:07 -05:00
|
|
|
bcx.fcx.param_substs, id, |fcx| {
|
2012-03-27 05:33:13 -05:00
|
|
|
load_environment(fcx, cdata_ty, cap_vars,
|
|
|
|
option::is_some(ret_handle), ck);
|
2012-06-30 18:19:07 -05:00
|
|
|
}, |bcx| {
|
2012-03-27 05:33:13 -05:00
|
|
|
if option::is_some(is_loop_body) {
|
|
|
|
Store(bcx, C_bool(true), bcx.fcx.llretptr);
|
|
|
|
}
|
2011-12-14 15:57:10 -06:00
|
|
|
});
|
2011-12-15 13:06:48 -06:00
|
|
|
llbox
|
|
|
|
};
|
|
|
|
|
2011-12-29 22:07:55 -06:00
|
|
|
let closure = alt proto {
|
2012-01-18 14:04:07 -06:00
|
|
|
ast::proto_any | ast::proto_block { trans_closure_env(ty::ck_block) }
|
2012-01-19 00:37:22 -06:00
|
|
|
ast::proto_box { trans_closure_env(ty::ck_box) }
|
|
|
|
ast::proto_uniq { trans_closure_env(ty::ck_uniq) }
|
|
|
|
ast::proto_bare {
|
2012-03-08 06:30:22 -06:00
|
|
|
trans_closure(ccx, sub_path, decl, body, llfn, no_self, none,
|
2012-06-30 18:19:07 -05:00
|
|
|
id, |_fcx| { }, |_bcx| { });
|
2012-02-02 05:37:17 -06:00
|
|
|
C_null(T_opaque_box_ptr(ccx))
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
};
|
2011-12-15 13:06:48 -06:00
|
|
|
fill_fn_pair(bcx, get_dest_addr(dest), llfn, closure);
|
2011-12-14 15:57:10 -06:00
|
|
|
ret bcx;
|
|
|
|
}
|
|
|
|
|
2011-12-15 13:06:48 -06:00
|
|
|
fn make_fn_glue(
|
2012-02-17 06:17:40 -06:00
|
|
|
cx: block,
|
2011-12-15 13:06:48 -06:00
|
|
|
v: ValueRef,
|
|
|
|
t: ty::t,
|
2012-02-17 06:17:40 -06:00
|
|
|
glue_fn: fn@(block, v: ValueRef, t: ty::t) -> block)
|
|
|
|
-> block {
|
2012-07-14 00:57:48 -05:00
|
|
|
let _icx = cx.insn_ctxt(~"closure::make_fn_glue");
|
2011-12-15 13:06:48 -06:00
|
|
|
let bcx = cx;
|
2012-02-21 07:20:18 -06:00
|
|
|
let tcx = cx.tcx();
|
2011-12-15 13:06:48 -06:00
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
let fn_env = fn@(ck: ty::closure_kind) -> block {
|
2012-06-29 18:26:56 -05:00
|
|
|
let box_cell_v = GEPi(cx, v, ~[0u, abi::fn_field_box]);
|
2011-12-15 13:06:48 -06:00
|
|
|
let box_ptr_v = Load(cx, box_cell_v);
|
2012-06-30 18:19:07 -05:00
|
|
|
do with_cond(cx, IsNotNull(cx, box_ptr_v)) |bcx| {
|
2012-01-05 18:19:12 -06:00
|
|
|
let closure_ty = ty::mk_opaque_closure_ptr(tcx, ck);
|
|
|
|
glue_fn(bcx, box_cell_v, closure_ty)
|
|
|
|
}
|
2011-12-15 13:06:48 -06:00
|
|
|
};
|
|
|
|
|
2012-02-03 08:15:28 -06:00
|
|
|
ret alt ty::get(t).struct {
|
2012-01-26 03:29:47 -06:00
|
|
|
ty::ty_fn({proto: ast::proto_bare, _}) |
|
|
|
|
ty::ty_fn({proto: ast::proto_block, _}) |
|
2012-01-19 00:37:22 -06:00
|
|
|
ty::ty_fn({proto: ast::proto_any, _}) { bcx }
|
|
|
|
ty::ty_fn({proto: ast::proto_uniq, _}) { fn_env(ty::ck_uniq) }
|
|
|
|
ty::ty_fn({proto: ast::proto_box, _}) { fn_env(ty::ck_box) }
|
2012-07-14 00:57:48 -05:00
|
|
|
_ { fail ~"make_fn_glue invoked on non-function type" }
|
2011-12-15 13:06:48 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-01-05 18:19:12 -06:00
|
|
|
fn make_opaque_cbox_take_glue(
|
2012-02-17 06:17:40 -06:00
|
|
|
bcx: block,
|
2012-01-05 18:19:12 -06:00
|
|
|
ck: ty::closure_kind,
|
|
|
|
cboxptr: ValueRef) // ptr to ptr to the opaque closure
|
2012-02-17 06:17:40 -06:00
|
|
|
-> block {
|
2012-01-05 18:19:12 -06:00
|
|
|
// Easy cases:
|
2012-07-14 00:57:48 -05:00
|
|
|
let _icx = bcx.insn_ctxt(~"closure::make_opaque_cbox_take_glue");
|
2012-01-05 18:19:12 -06:00
|
|
|
alt ck {
|
2012-01-19 00:37:22 -06:00
|
|
|
ty::ck_block { ret bcx; }
|
2012-03-23 08:45:47 -05:00
|
|
|
ty::ck_box { incr_refcnt_of_boxed(bcx, Load(bcx, cboxptr)); ret bcx; }
|
2012-01-19 00:37:22 -06:00
|
|
|
ty::ck_uniq { /* hard case: */ }
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hard case, a deep copy:
|
2012-02-21 07:20:18 -06:00
|
|
|
let ccx = bcx.ccx(), tcx = ccx.tcx;
|
2012-02-01 20:52:08 -06:00
|
|
|
let llopaquecboxty = T_opaque_box_ptr(ccx);
|
2012-01-05 18:19:12 -06:00
|
|
|
let cbox_in = Load(bcx, cboxptr);
|
2012-06-30 18:19:07 -05:00
|
|
|
do with_cond(bcx, IsNotNull(bcx, cbox_in)) |bcx| {
|
2012-01-05 18:19:12 -06:00
|
|
|
// Load the size from the type descr found in the cbox
|
|
|
|
let cbox_in = PointerCast(bcx, cbox_in, llopaquecboxty);
|
2012-06-29 18:26:56 -05:00
|
|
|
let tydescptr = GEPi(bcx, cbox_in, ~[0u, abi::box_field_tydesc]);
|
2012-01-05 18:19:12 -06:00
|
|
|
let tydesc = Load(bcx, tydescptr);
|
|
|
|
let tydesc = PointerCast(bcx, tydesc, T_ptr(ccx.tydesc_type));
|
2012-06-29 18:26:56 -05:00
|
|
|
let sz = Load(bcx, GEPi(bcx, tydesc, ~[0u, abi::tydesc_field_size]));
|
2012-01-05 18:19:12 -06:00
|
|
|
|
2012-02-01 20:52:08 -06:00
|
|
|
// Adjust sz to account for the rust_opaque_box header fields
|
2012-02-10 04:32:03 -06:00
|
|
|
let sz = Add(bcx, sz, shape::llsize_of(ccx, T_box_header(ccx)));
|
2012-02-01 20:52:08 -06:00
|
|
|
|
2012-01-05 18:19:12 -06:00
|
|
|
// Allocate memory, update original ptr, and copy existing data
|
2012-06-21 12:47:27 -05:00
|
|
|
let malloc = ccx.upcalls.exchange_malloc;
|
2012-06-29 18:26:56 -05:00
|
|
|
let cbox_out = Call(bcx, malloc, ~[tydesc, sz]);
|
2012-01-05 18:19:12 -06:00
|
|
|
let cbox_out = PointerCast(bcx, cbox_out, llopaquecboxty);
|
2012-03-23 08:45:47 -05:00
|
|
|
call_memmove(bcx, cbox_out, cbox_in, sz);
|
2012-01-05 18:19:12 -06:00
|
|
|
Store(bcx, cbox_out, cboxptr);
|
|
|
|
|
2012-02-01 20:52:08 -06:00
|
|
|
// Take the (deeply cloned) type descriptor
|
2012-06-29 18:26:56 -05:00
|
|
|
let tydesc_out = GEPi(bcx, cbox_out, ~[0u, abi::box_field_tydesc]);
|
2012-03-15 11:42:33 -05:00
|
|
|
let bcx = take_ty(bcx, tydesc_out, ty::mk_type(tcx));
|
2012-02-01 20:52:08 -06:00
|
|
|
|
2012-01-05 18:19:12 -06:00
|
|
|
// Take the data in the tuple
|
2012-06-29 18:26:56 -05:00
|
|
|
let cdata_out = GEPi(bcx, cbox_out, ~[0u, abi::box_field_body]);
|
2012-02-01 20:52:08 -06:00
|
|
|
call_tydesc_glue_full(bcx, cdata_out, tydesc,
|
2012-07-09 20:03:37 -05:00
|
|
|
abi::tydesc_field_take_glue, none);
|
2012-01-05 18:19:12 -06:00
|
|
|
bcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_opaque_cbox_drop_glue(
|
2012-02-17 06:17:40 -06:00
|
|
|
bcx: block,
|
2012-01-05 18:19:12 -06:00
|
|
|
ck: ty::closure_kind,
|
|
|
|
cboxptr: ValueRef) // ptr to the opaque closure
|
2012-02-17 06:17:40 -06:00
|
|
|
-> block {
|
2012-07-14 00:57:48 -05:00
|
|
|
let _icx = bcx.insn_ctxt(~"closure::make_opaque_cbox_drop_glue");
|
2012-01-05 18:19:12 -06:00
|
|
|
alt ck {
|
2012-01-19 00:37:22 -06:00
|
|
|
ty::ck_block { bcx }
|
|
|
|
ty::ck_box {
|
2012-01-05 18:19:12 -06:00
|
|
|
decr_refcnt_maybe_free(bcx, Load(bcx, cboxptr),
|
2012-02-21 07:20:18 -06:00
|
|
|
ty::mk_opaque_closure_ptr(bcx.tcx(), ck))
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
ty::ck_uniq {
|
2012-01-05 18:19:12 -06:00
|
|
|
free_ty(bcx, Load(bcx, cboxptr),
|
2012-02-21 07:20:18 -06:00
|
|
|
ty::mk_opaque_closure_ptr(bcx.tcx(), ck))
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_opaque_cbox_free_glue(
|
2012-02-17 06:17:40 -06:00
|
|
|
bcx: block,
|
2012-01-05 18:19:12 -06:00
|
|
|
ck: ty::closure_kind,
|
|
|
|
cbox: ValueRef) // ptr to the opaque closure
|
2012-02-17 06:17:40 -06:00
|
|
|
-> block {
|
2012-07-14 00:57:48 -05:00
|
|
|
let _icx = bcx.insn_ctxt(~"closure::make_opaque_cbox_free_glue");
|
2012-01-05 18:19:12 -06:00
|
|
|
alt ck {
|
2012-01-19 00:37:22 -06:00
|
|
|
ty::ck_block { ret bcx; }
|
|
|
|
ty::ck_box | ty::ck_uniq { /* hard cases: */ }
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
|
|
|
|
2012-05-14 18:31:35 -05:00
|
|
|
let ccx = bcx.ccx();
|
2012-06-30 18:19:07 -05:00
|
|
|
do with_cond(bcx, IsNotNull(bcx, cbox)) |bcx| {
|
2012-01-05 18:19:12 -06:00
|
|
|
// Load the type descr found in the cbox
|
|
|
|
let lltydescty = T_ptr(ccx.tydesc_type);
|
|
|
|
let cbox = PointerCast(bcx, cbox, T_opaque_cbox_ptr(ccx));
|
2012-06-29 18:26:56 -05:00
|
|
|
let tydescptr = GEPi(bcx, cbox, ~[0u, abi::box_field_tydesc]);
|
2012-01-05 18:19:12 -06:00
|
|
|
let tydesc = Load(bcx, tydescptr);
|
|
|
|
let tydesc = PointerCast(bcx, tydesc, lltydescty);
|
|
|
|
|
|
|
|
// Drop the tuple data then free the descriptor
|
2012-06-29 18:26:56 -05:00
|
|
|
let cdata = GEPi(bcx, cbox, ~[0u, abi::box_field_body]);
|
2012-02-01 20:52:08 -06:00
|
|
|
call_tydesc_glue_full(bcx, cdata, tydesc,
|
2012-07-09 20:03:37 -05:00
|
|
|
abi::tydesc_field_drop_glue, none);
|
2012-01-05 18:19:12 -06:00
|
|
|
|
|
|
|
// Free the ty descr (if necc) and the box itself
|
|
|
|
alt ck {
|
2012-07-14 00:57:48 -05:00
|
|
|
ty::ck_block { fail ~"Impossible"; }
|
2012-01-19 00:37:22 -06:00
|
|
|
ty::ck_box {
|
2012-02-03 02:34:42 -06:00
|
|
|
trans_free(bcx, cbox)
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
ty::ck_uniq {
|
2012-05-14 18:31:35 -05:00
|
|
|
trans_unique_free(bcx, cbox)
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-15 13:06:48 -06:00
|
|
|
}
|