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
|
|
|
// Evaluate expr and store result in env (used for bind).
|
2012-02-02 05:37:17 -06:00
|
|
|
env_expr(@ast::expr, ty::t),
|
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-03-14 19:31:16 -05:00
|
|
|
fn ev_to_str(ccx: @crate_ctxt, ev: environment_value) -> str {
|
2012-01-08 15:32:40 -06:00
|
|
|
alt ev {
|
2012-02-02 05:37:17 -06:00
|
|
|
env_expr(ex, _) { expr_to_str(ex) }
|
2012-01-08 15:32:40 -06:00
|
|
|
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-03-15 11:42:33 -05:00
|
|
|
let cbox_ty = tuplify_cbox_ty(tcx, cdata_ty, ty::mk_type(tcx));
|
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-01-06 17:12:42 -06:00
|
|
|
bound_values: [environment_value])
|
2012-02-01 20:52:08 -06:00
|
|
|
-> (ty::t, [ty::t]) {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut bound_tys = [];
|
2012-01-05 18:19:12 -06:00
|
|
|
|
|
|
|
// Compute the closed over data
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(bound_values) {|bv|
|
2012-01-05 18:19:12 -06:00
|
|
|
bound_tys += [alt bv {
|
|
|
|
env_copy(_, t, _) { t }
|
|
|
|
env_move(_, t, _) { t }
|
|
|
|
env_ref(_, t, _) { t }
|
2012-02-02 05:37:17 -06:00
|
|
|
env_expr(_, t) { t }
|
2012-01-05 18:19:12 -06:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
let bound_data_ty = ty::mk_tup(tcx, bound_tys);
|
2012-03-09 03:47:40 -06:00
|
|
|
// FIXME[mono] remove tuple of tydescs from closure types
|
|
|
|
let cdata_ty = ty::mk_tup(tcx, [ty::mk_tup(tcx, []),
|
2012-02-01 20:52:08 -06:00
|
|
|
bound_data_ty]);
|
|
|
|
#debug["cdata_ty=%s", ty_to_str(tcx, cdata_ty)];
|
|
|
|
ret (cdata_ty, bound_tys);
|
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-02-17 06:17:40 -06:00
|
|
|
-> (block, ValueRef, [ValueRef]) {
|
2012-03-22 15:44:20 -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-02-17 06:17:40 -06:00
|
|
|
fn nuke_ref_count(bcx: block, box: ValueRef) {
|
2012-03-22 15:44:20 -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-02-01 20:52:08 -06:00
|
|
|
let box = PointerCast(bcx, box, T_opaque_box_ptr(ccx));
|
2012-05-04 00:31:38 -05:00
|
|
|
let ref_cnt = GEPi(bcx, box, [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-03-15 11:42:33 -05:00
|
|
|
fn store_tydesc(bcx: block,
|
|
|
|
cdata_ty: ty::t,
|
|
|
|
box: ValueRef,
|
|
|
|
&ti: option<@tydesc_info>) -> block {
|
2012-05-04 00:31:38 -05:00
|
|
|
let bound_tydesc = GEPi(bcx, box, [0u, abi::box_field_tydesc]);
|
2012-03-21 09:42:20 -05:00
|
|
|
let td = base::get_tydesc(bcx.ccx(), cdata_ty, ti);
|
2012-02-01 20:52:08 -06:00
|
|
|
Store(bcx, td, bound_tydesc);
|
|
|
|
bcx
|
|
|
|
}
|
2012-01-05 18:19:12 -06:00
|
|
|
|
2012-02-01 20:52:08 -06:00
|
|
|
// Allocate and initialize the box:
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut ti = none;
|
|
|
|
let mut temp_cleanups = [];
|
2012-02-01 20:52:08 -06:00
|
|
|
let (bcx, box) = alt ck {
|
2012-01-19 00:37:22 -06:00
|
|
|
ty::ck_box {
|
2012-03-23 08:45:47 -05:00
|
|
|
let box = malloc_boxed_raw(bcx, cdata_ty, ti);
|
2012-02-01 20:52:08 -06:00
|
|
|
(bcx, box)
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
ty::ck_uniq {
|
2012-02-01 20:52:08 -06:00
|
|
|
let uniq_cbox_ty = mk_tuplified_uniq_cbox_ty(tcx, cdata_ty);
|
2012-03-23 08:45:47 -05:00
|
|
|
let box = uniq::alloc_uniq(bcx, uniq_cbox_ty);
|
2012-02-01 20:52:08 -06:00
|
|
|
nuke_ref_count(bcx, box);
|
2012-03-15 11:42:33 -05:00
|
|
|
let bcx = store_tydesc(bcx, cdata_ty, box, ti);
|
2012-02-01 20:52:08 -06:00
|
|
|
(bcx, box)
|
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-03-23 08:45:47 -05:00
|
|
|
let box = base::alloc_ty(bcx, cbox_ty);
|
2012-02-01 20:52:08 -06:00
|
|
|
nuke_ref_count(bcx, box);
|
|
|
|
(bcx, box)
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-07 04:25:04 -06:00
|
|
|
base::lazily_emit_tydesc_glue(ccx, abi::tydesc_field_take_glue, ti);
|
|
|
|
base::lazily_emit_tydesc_glue(ccx, abi::tydesc_field_drop_glue, ti);
|
|
|
|
base::lazily_emit_tydesc_glue(ccx, abi::tydesc_field_free_glue, ti);
|
2012-01-05 18:19:12 -06:00
|
|
|
|
|
|
|
ret (bcx, box, temp_cleanups);
|
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,
|
|
|
|
bound_values: [environment_value],
|
|
|
|
ck: ty::closure_kind) -> closure_result {
|
2012-03-22 15:44:20 -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-02-01 20:52:08 -06:00
|
|
|
let (cdata_ty, bound_tys) =
|
2012-03-09 03:47:40 -06:00
|
|
|
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-03-15 08:47:03 -05:00
|
|
|
let mut (bcx, llbox, temp_cleanups) =
|
2012-02-01 20:52:08 -06:00
|
|
|
allocate_cbox(bcx, ck, cdata_ty);
|
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;
|
2011-12-16 08:55:42 -06:00
|
|
|
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-05-04 00:31:38 -05:00
|
|
|
[0u, abi::box_field_body, abi::closure_body_bindings, i]);
|
2011-12-14 15:57:10 -06:00
|
|
|
alt bv {
|
2012-02-02 05:37:17 -06:00
|
|
|
env_expr(e, _) {
|
2012-01-27 06:17:06 -06:00
|
|
|
bcx = base::trans_expr_save_in(bcx, e, bound_data);
|
2012-01-05 18:19:12 -06:00
|
|
|
add_clean_temp_mem(bcx, bound_data, bound_tys[i]);
|
|
|
|
temp_cleanups += [bound_data];
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
env_copy(val, ty, 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-01-19 00:37:22 -06:00
|
|
|
env_copy(val, ty, 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-01-19 00:37:22 -06:00
|
|
|
env_copy(_, _, temporary) {
|
2012-03-05 18:27:27 -06: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-01-19 00:37:22 -06:00
|
|
|
env_ref(val, ty, owned) {
|
2012-01-05 18:19:12 -06:00
|
|
|
Store(bcx, val, bound_data);
|
2011-12-19 14:50:31 -06:00
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
env_ref(val, ty, 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-01-19 00:37:22 -06:00
|
|
|
env_ref(_, _, temporary) {
|
2012-03-05 18:27:27 -06:00
|
|
|
fail "cannot capture temporary upvar";
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-27 08:14:12 -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,
|
2011-12-19 14:50:31 -06: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-03-22 15:44:20 -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-03-15 08:47:03 -05:00
|
|
|
let mut env_vals = [];
|
|
|
|
let mut bcx = bcx0;
|
|
|
|
let ccx = bcx.ccx(), tcx = ccx.tcx;
|
2011-12-19 14:50:31 -06:00
|
|
|
|
|
|
|
// Package up the captured upvars
|
|
|
|
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-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);
|
|
|
|
env_vals += [env_ref(lv.val, ty, lv.kind)];
|
|
|
|
}
|
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
|
|
|
};
|
|
|
|
if mv { env_vals += [env_move(lv.val, ty, lv.kind)]; }
|
|
|
|
else { 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 {
|
2011-12-19 14:50:31 -06:00
|
|
|
env_vals += [env_move(lv.val, ty, lv.kind)];
|
|
|
|
}
|
2012-01-19 03:03:57 -06:00
|
|
|
capture::cap_drop {
|
2012-02-22 06:35:17 -06:00
|
|
|
assert lv.kind == 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-04-06 14:14:09 -05:00
|
|
|
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()));
|
|
|
|
env_vals +=
|
|
|
|
[env_ref(flagptr, ty::mk_mut_ptr(tcx, ty::mk_bool(tcx)), owned),
|
|
|
|
env_ref(nil_ret, ty::mk_nil_ptr(tcx), owned)];
|
|
|
|
}
|
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,
|
2011-12-19 14:50:31 -06: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-03-22 15:44:20 -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;
|
2011-12-19 14:50:31 -06:00
|
|
|
vec::iter(cap_vars) { |cap_var|
|
|
|
|
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-05-04 00:31:38 -05:00
|
|
|
GEPi(bcx, llcdata, [0u, abi::closure_body_bindings, 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-05-04 00:31:38 -05:00
|
|
|
[0u, abi::closure_body_bindings, i]));
|
2012-03-27 05:33:13 -05:00
|
|
|
let retptr = Load(bcx, GEPi(bcx, llcdata,
|
2012-05-04 00:31:38 -05:00
|
|
|
[0u, abi::closure_body_bindings, 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
|
|
|
sp: span,
|
|
|
|
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-03-22 15:44:20 -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-02-03 02:53:37 -06:00
|
|
|
let sub_path = bcx.fcx.path + [path_name("anon")];
|
|
|
|
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-03-20 17:15:57 -05:00
|
|
|
register_fn(ccx, sp, sub_path, id);
|
2011-12-14 15:57:10 -06:00
|
|
|
|
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-03-23 21:49:01 -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-03-26 09:09:27 -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-03-26 09:09:27 -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;
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn trans_bind(cx: block, f: @ast::expr, args: [option<@ast::expr>],
|
|
|
|
id: ast::node_id, dest: dest) -> block {
|
2012-03-22 15:44:20 -05:00
|
|
|
let _icx = cx.insn_ctxt("closure::trans_bind");
|
2011-12-14 15:57:10 -06:00
|
|
|
let f_res = trans_callee(cx, f);
|
2012-02-02 05:37:17 -06:00
|
|
|
ret trans_bind_1(cx, expr_ty(cx, f), f_res, args,
|
2012-02-09 07:33:00 -06:00
|
|
|
node_id_type(cx, id), dest);
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn trans_bind_1(cx: block, outgoing_fty: ty::t,
|
2011-12-14 15:57:10 -06:00
|
|
|
f_res: lval_maybe_callee,
|
2012-01-31 19:05:20 -06:00
|
|
|
args: [option<@ast::expr>], pair_ty: ty::t,
|
2012-02-17 06:17:40 -06:00
|
|
|
dest: dest) -> block {
|
2012-03-22 15:44:20 -05:00
|
|
|
let _icx = cx.insn_ctxt("closure::trans_bind1");
|
2012-02-21 07:20:18 -06:00
|
|
|
let ccx = cx.ccx();
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut bound: [@ast::expr] = [];
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(args) {|argopt|
|
2012-01-19 00:37:22 -06:00
|
|
|
alt argopt { none { } some(e) { bound += [e]; } }
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut bcx = f_res.bcx;
|
2011-12-14 15:57:10 -06:00
|
|
|
if dest == ignore {
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(bound) {|ex| bcx = trans_expr(bcx, ex, ignore); }
|
2011-12-14 15:57:10 -06:00
|
|
|
ret bcx;
|
|
|
|
}
|
|
|
|
|
2012-03-09 03:47:40 -06:00
|
|
|
if bound.len() == 0u &&
|
2012-02-14 03:47:39 -06:00
|
|
|
(f_res.env == null_env || f_res.env == is_closure) {
|
2011-12-14 15:57:10 -06:00
|
|
|
// Trivial 'binding': just return the closure
|
|
|
|
let lv = lval_maybe_callee_to_lval(f_res, pair_ty);
|
2012-03-23 08:45:47 -05:00
|
|
|
memmove_ty(lv.bcx, get_dest_addr(dest), lv.val, pair_ty);
|
|
|
|
ret lv.bcx;
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Arrange for the bound function to live in the first binding spot
|
|
|
|
// if the function is not statically known.
|
2012-02-14 03:47:39 -06:00
|
|
|
let (env_vals, target_info) = alt f_res.env {
|
|
|
|
null_env { ([], target_static(f_res.val)) }
|
|
|
|
is_closure {
|
2011-12-14 15:57:10 -06:00
|
|
|
// Cast the function we are binding to be the type that the
|
|
|
|
// closure will expect it to have. The type the closure knows
|
|
|
|
// about has the type parameters substituted with the real types.
|
2012-01-27 05:55:21 -06:00
|
|
|
let llclosurety = T_ptr(type_of(ccx, outgoing_fty));
|
2012-02-14 03:47:39 -06:00
|
|
|
let src_loc = PointerCast(bcx, f_res.val, llclosurety);
|
|
|
|
([env_copy(src_loc, pair_ty, owned)], target_closure)
|
|
|
|
}
|
2012-03-16 06:57:07 -05:00
|
|
|
self_env(slf, slf_t, none) {
|
|
|
|
([env_copy(slf, slf_t, owned)], target_static_self(f_res.val))
|
|
|
|
}
|
|
|
|
self_env(_, slf_t, some(slf)) {
|
|
|
|
let cast = PointerCast(bcx, f_res.val, T_ptr(T_nil()));
|
|
|
|
([env_copy(cast, ty::mk_nil_ptr(ccx.tcx), owned_imm),
|
|
|
|
env_copy(slf, slf_t, owned_imm)], target_self)
|
2012-02-14 03:47:39 -06:00
|
|
|
}
|
2011-12-14 15:57:10 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Actually construct the closure
|
2012-02-01 20:52:08 -06:00
|
|
|
let {llbox, cdata_ty, bcx} = store_environment(
|
2012-03-09 03:47:40 -06:00
|
|
|
bcx, env_vals + vec::map(bound, {|x| env_expr(x, expr_ty(bcx, x))}),
|
2012-01-10 08:49:15 -06:00
|
|
|
ty::ck_box);
|
2011-12-14 15:57:10 -06:00
|
|
|
|
|
|
|
// Make thunk
|
2012-02-03 02:53:37 -06:00
|
|
|
let llthunk = trans_bind_thunk(
|
2012-03-08 12:02:22 -06:00
|
|
|
cx.fcx.ccx, cx.fcx.path, pair_ty, outgoing_fty, args,
|
2012-03-09 03:47:40 -06:00
|
|
|
cdata_ty, target_info);
|
2011-12-14 15:57:10 -06:00
|
|
|
|
|
|
|
// Fill the function pair
|
2011-12-15 13:06:48 -06:00
|
|
|
fill_fn_pair(bcx, get_dest_addr(dest), llthunk.val, llbox);
|
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-03-22 15:44:20 -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-05-04 00:31:38 -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-02-17 06:17:40 -06:00
|
|
|
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) }
|
2011-12-15 13:06:48 -06:00
|
|
|
_ { fail "make_fn_glue invoked on non-function type" }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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-03-22 15:44:20 -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-02-17 06:17:40 -06:00
|
|
|
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-05-04 00:31:38 -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-05-04 00:31:38 -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
|
|
|
|
let malloc = ccx.upcalls.shared_malloc;
|
2012-02-21 08:01:19 -06:00
|
|
|
let cbox_out = Call(bcx, malloc, [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-05-04 00:31:38 -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
|
|
|
|
let ti = none;
|
2012-05-04 00:31:38 -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-01-05 18:19:12 -06:00
|
|
|
abi::tydesc_field_take_glue, ti);
|
|
|
|
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-03-22 15:44:20 -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-03-22 15:44:20 -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-02-21 07:20:18 -06:00
|
|
|
let ccx = bcx.ccx(), tcx = ccx.tcx;
|
2012-02-17 06:17:40 -06:00
|
|
|
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-05-04 00:31:38 -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
|
|
|
|
let ti = none;
|
2012-05-04 00:31:38 -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-01-05 18:19:12 -06:00
|
|
|
abi::tydesc_field_drop_glue, ti);
|
|
|
|
|
|
|
|
// Free the ty descr (if necc) and the box itself
|
|
|
|
alt ck {
|
2012-01-19 00:37:22 -06:00
|
|
|
ty::ck_block { fail "Impossible"; }
|
|
|
|
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-03-15 11:42:33 -05:00
|
|
|
let bcx = free_ty(bcx, tydesc, ty::mk_type(tcx));
|
2012-01-05 18:19:12 -06:00
|
|
|
trans_shared_free(bcx, cbox)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-15 13:06:48 -06:00
|
|
|
}
|
|
|
|
|
2012-02-14 03:47:39 -06:00
|
|
|
enum target_info {
|
|
|
|
target_closure,
|
|
|
|
target_static(ValueRef),
|
2012-03-16 06:57:07 -05:00
|
|
|
target_self,
|
|
|
|
target_static_self(ValueRef),
|
2012-02-14 03:47:39 -06:00
|
|
|
}
|
|
|
|
|
2011-12-15 13:06:48 -06:00
|
|
|
// pth is cx.path
|
2012-03-14 19:31:16 -05:00
|
|
|
fn trans_bind_thunk(ccx: @crate_ctxt,
|
2012-02-03 02:53:37 -06:00
|
|
|
path: path,
|
2011-12-15 13:06:48 -06:00
|
|
|
incoming_fty: ty::t,
|
|
|
|
outgoing_fty: ty::t,
|
2012-01-31 19:05:20 -06:00
|
|
|
args: [option<@ast::expr>],
|
2012-02-01 20:52:08 -06:00
|
|
|
cdata_ty: ty::t,
|
2012-03-09 03:47:40 -06:00
|
|
|
target_info: target_info)
|
2011-12-15 13:06:48 -06:00
|
|
|
-> {val: ValueRef, ty: TypeRef} {
|
2012-03-22 15:44:20 -05:00
|
|
|
let _icx = ccx.insn_ctxt("closure::trans_bind_thunk");
|
2012-02-03 02:53:37 -06:00
|
|
|
let tcx = ccx.tcx;
|
2012-02-01 20:52:08 -06:00
|
|
|
#debug["trans_bind_thunk[incoming_fty=%s,outgoing_fty=%s,\
|
2012-03-08 12:02:22 -06:00
|
|
|
cdata_ty=%s]",
|
2012-02-01 20:52:08 -06:00
|
|
|
ty_to_str(tcx, incoming_fty),
|
|
|
|
ty_to_str(tcx, outgoing_fty),
|
2012-03-08 12:02:22 -06:00
|
|
|
ty_to_str(tcx, cdata_ty)];
|
2012-02-01 20:52:08 -06:00
|
|
|
|
2011-12-15 13:06:48 -06:00
|
|
|
// Here we're not necessarily constructing a thunk in the sense of
|
|
|
|
// "function with no arguments". The result of compiling 'bind f(foo,
|
|
|
|
// bar, baz)' would be a thunk that, when called, applies f to those
|
|
|
|
// arguments and returns the result. But we're stretching the meaning of
|
|
|
|
// the word "thunk" here to also mean the result of compiling, say, 'bind
|
|
|
|
// f(foo, _, baz)', or any other bind expression that binds f and leaves
|
|
|
|
// some (or all) of the arguments unbound.
|
|
|
|
|
|
|
|
// Here, 'incoming_fty' is the type of the entire bind expression, while
|
|
|
|
// 'outgoing_fty' is the type of the function that is having some of its
|
|
|
|
// arguments bound. If f is a function that takes three arguments of type
|
|
|
|
// int and returns int, and we're translating, say, 'bind f(3, _, 5)',
|
|
|
|
// then outgoing_fty is the type of f, which is (int, int, int) -> int,
|
|
|
|
// and incoming_fty is the type of 'bind f(3, _, 5)', which is int -> int.
|
|
|
|
|
|
|
|
// Once translated, the entire bind expression will be the call f(foo,
|
|
|
|
// bar, baz) wrapped in a (so-called) thunk that takes 'bar' as its
|
|
|
|
// argument and that has bindings of 'foo' to 3 and 'baz' to 5 and a
|
|
|
|
// pointer to 'f' all saved in its environment. So, our job is to
|
|
|
|
// construct and return that thunk.
|
|
|
|
|
|
|
|
// Give the thunk a name, type, and value.
|
2012-02-03 02:53:37 -06:00
|
|
|
let s = mangle_internal_name_by_path_and_seq(ccx, path, "thunk");
|
|
|
|
let llthunk_ty = get_pair_fn_ty(type_of(ccx, incoming_fty));
|
|
|
|
let llthunk = decl_internal_cdecl_fn(ccx.llmod, s, llthunk_ty);
|
2011-12-15 13:06:48 -06:00
|
|
|
|
|
|
|
// Create a new function context and block context for the thunk, and hold
|
|
|
|
// onto a pointer to the first block in the function for later use.
|
2012-02-03 02:53:37 -06:00
|
|
|
let fcx = new_fn_ctxt(ccx, path, llthunk, none);
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut bcx = top_scope_block(fcx, none);
|
2011-12-15 13:06:48 -06:00
|
|
|
let lltop = bcx.llbb;
|
|
|
|
// Since we might need to construct derived tydescs that depend on
|
|
|
|
// our bound tydescs, we need to load tydescs out of the environment
|
|
|
|
// before derived tydescs are constructed. To do this, we load them
|
|
|
|
// in the load_env block.
|
2012-02-17 06:17:40 -06:00
|
|
|
let l_bcx = raw_block(fcx, fcx.llloadenv);
|
2011-12-15 13:06:48 -06:00
|
|
|
|
|
|
|
// The 'llenv' that will arrive in the thunk we're creating is an
|
2012-02-01 20:52:08 -06:00
|
|
|
// environment that will contain the values of its arguments and a
|
|
|
|
// pointer to the original function. This environment is always
|
|
|
|
// stored like an opaque box (see big comment at the header of the
|
|
|
|
// file), so we load the body body, which contains the type descr
|
|
|
|
// and cached data.
|
|
|
|
let llcdata = base::opaque_box_body(l_bcx, cdata_ty, fcx.llenv);
|
2011-12-15 13:06:48 -06:00
|
|
|
|
|
|
|
// "target", in this context, means the function that's having some of its
|
|
|
|
// arguments bound and that will be called inside the thunk we're
|
|
|
|
// creating. (In our running example, target is the function f.) Pick
|
|
|
|
// out the pointer to the target function from the environment. The
|
|
|
|
// target function lives in the first binding spot.
|
2012-02-14 03:47:39 -06:00
|
|
|
let (lltargetfn, lltargetenv, starting_idx) = alt target_info {
|
|
|
|
target_static(fptr) {
|
2012-05-04 00:31:38 -05:00
|
|
|
(fptr, llvm::LLVMGetUndef(T_opaque_cbox_ptr(ccx)), 0u)
|
2011-12-15 13:06:48 -06:00
|
|
|
}
|
2012-02-14 03:47:39 -06:00
|
|
|
target_closure {
|
2012-05-04 00:31:38 -05:00
|
|
|
let pair = GEPi(bcx, llcdata, [0u, abi::closure_body_bindings, 0u]);
|
2011-12-15 13:06:48 -06:00
|
|
|
let lltargetenv =
|
2012-05-04 00:31:38 -05:00
|
|
|
Load(bcx, GEPi(bcx, pair, [0u, abi::fn_field_box]));
|
2011-12-15 13:06:48 -06:00
|
|
|
let lltargetfn = Load
|
2012-05-04 00:31:38 -05:00
|
|
|
(bcx, GEPi(bcx, pair, [0u, abi::fn_field_code]));
|
|
|
|
(lltargetfn, lltargetenv, 1u)
|
2011-12-15 13:06:48 -06:00
|
|
|
}
|
2012-03-16 06:57:07 -05:00
|
|
|
target_self {
|
|
|
|
let fptr = Load(bcx, GEPi(bcx, llcdata,
|
2012-05-04 00:31:38 -05:00
|
|
|
[0u, abi::closure_body_bindings, 0u]));
|
|
|
|
let slfbox = GEPi(bcx, llcdata, [0u, abi::closure_body_bindings, 1u]);
|
|
|
|
let selfptr = GEPi(bcx, Load(bcx, slfbox), [0u, abi::box_field_body]);
|
|
|
|
(fptr, PointerCast(bcx, selfptr, T_opaque_cbox_ptr(ccx)), 2u)
|
2012-03-16 06:57:07 -05:00
|
|
|
}
|
|
|
|
target_static_self(fptr) {
|
2012-05-04 00:31:38 -05:00
|
|
|
let slfptr = GEPi(bcx, llcdata, [0u, abi::closure_body_bindings, 0u]);
|
|
|
|
(fptr, PointerCast(bcx, slfptr, T_opaque_cbox_ptr(ccx)), 1u)
|
2012-02-14 03:47:39 -06:00
|
|
|
}
|
2011-12-15 13:06:48 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// And then, pick out the target function's own environment. That's what
|
|
|
|
// we'll use as the environment the thunk gets.
|
|
|
|
|
|
|
|
// Get the types of the arguments to f.
|
2012-02-03 08:15:28 -06:00
|
|
|
let outgoing_args = ty::ty_fn_args(outgoing_fty);
|
2011-12-15 13:06:48 -06:00
|
|
|
|
|
|
|
// Set up the three implicit arguments to the thunk.
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut llargs: [ValueRef] = [fcx.llretptr, lltargetenv];
|
2011-12-15 13:06:48 -06:00
|
|
|
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut a: uint = first_real_arg; // retptr, env come first
|
2012-05-04 00:31:38 -05:00
|
|
|
let mut b: uint = starting_idx;
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut outgoing_arg_index: uint = 0u;
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(args) {|arg|
|
2011-12-15 13:06:48 -06:00
|
|
|
let out_arg = outgoing_args[outgoing_arg_index];
|
|
|
|
alt arg {
|
|
|
|
// Arg provided at binding time; thunk copies it from
|
|
|
|
// closure.
|
|
|
|
some(e) {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut val =
|
2012-05-04 00:31:38 -05:00
|
|
|
GEPi(bcx, llcdata, [0u, abi::closure_body_bindings, b]);
|
2012-02-02 18:50:17 -06:00
|
|
|
|
|
|
|
alt ty::resolved_mode(tcx, out_arg.mode) {
|
|
|
|
ast::by_val {
|
|
|
|
val = Load(bcx, val);
|
|
|
|
}
|
|
|
|
ast::by_copy {
|
2012-03-23 08:45:47 -05:00
|
|
|
let alloc = alloc_ty(bcx, out_arg.ty);
|
|
|
|
memmove_ty(bcx, alloc, val, out_arg.ty);
|
2011-12-15 13:06:48 -06:00
|
|
|
bcx = take_ty(bcx, alloc, out_arg.ty);
|
|
|
|
val = alloc;
|
2012-02-02 18:50:17 -06:00
|
|
|
}
|
2012-02-15 13:25:39 -06:00
|
|
|
ast::by_ref | ast::by_mutbl_ref | ast::by_move { }
|
2011-12-15 13:06:48 -06:00
|
|
|
}
|
|
|
|
llargs += [val];
|
2012-05-04 00:31:38 -05:00
|
|
|
b += 1u;
|
2011-12-15 13:06:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Arg will be provided when the thunk is invoked.
|
2012-01-19 00:37:22 -06:00
|
|
|
none {
|
2012-03-12 04:05:15 -05:00
|
|
|
llargs += [llvm::LLVMGetParam(llthunk, a as c_uint)];
|
2012-01-18 05:29:37 -06:00
|
|
|
a += 1u;
|
2011-12-15 13:06:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
outgoing_arg_index += 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cast the outgoing function to the appropriate type.
|
|
|
|
// This is necessary because the type of the function that we have
|
|
|
|
// in the closure does not know how many type descriptors the function
|
|
|
|
// needs to take.
|
2012-03-15 08:47:03 -05:00
|
|
|
let lltargetty = type_of_fn_from_ty(ccx, outgoing_fty);
|
|
|
|
let lltargetfn = PointerCast(bcx, lltargetfn, T_ptr(lltargetty));
|
2011-12-15 13:06:48 -06:00
|
|
|
Call(bcx, lltargetfn, llargs);
|
|
|
|
build_return(bcx);
|
|
|
|
finish_fn(fcx, lltop);
|
|
|
|
ret {val: llthunk, ty: llthunk_ty};
|
|
|
|
}
|