2012-09-04 13:54:36 -05:00
|
|
|
use libc::c_uint;
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::ast_util;
|
|
|
|
use lib::llvm::llvm;
|
|
|
|
use lib::llvm::{ValueRef, TypeRef};
|
|
|
|
use common::*;
|
|
|
|
use build::*;
|
|
|
|
use base::*;
|
|
|
|
use type_of::*;
|
|
|
|
use back::abi;
|
|
|
|
use syntax::codemap::span;
|
|
|
|
use syntax::print::pprust::expr_to_str;
|
2012-09-18 13:46:39 -05:00
|
|
|
use back::link::{
|
2011-12-15 13:06:48 -06:00
|
|
|
mangle_internal_name_by_path,
|
|
|
|
mangle_internal_name_by_path_and_seq};
|
2012-09-04 13:54:36 -05:00
|
|
|
use util::ppaux::ty_to_str;
|
|
|
|
use syntax::ast_map::{path, path_mod, path_name};
|
|
|
|
use driver::session::session;
|
2012-09-10 17:38:28 -05:00
|
|
|
use std::map::HashMap;
|
2012-08-28 17:54:45 -05:00
|
|
|
use datum::{Datum, INIT, ByRef, ByValue, FromLvalue};
|
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-08-28 17:54:45 -05:00
|
|
|
enum EnvAction {
|
|
|
|
/// Copy the value from this llvm ValueRef into the environment.
|
|
|
|
EnvStore,
|
2011-12-19 14:50:31 -06:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
/// Move the value from this llvm ValueRef into the environment.
|
|
|
|
EnvMove,
|
2011-12-19 14:50:31 -06:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
/// Access by reference (used for stack closures).
|
|
|
|
EnvRef
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
struct EnvValue {
|
2012-09-07 16:50:47 -05:00
|
|
|
action: EnvAction,
|
|
|
|
datum: Datum
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EnvAction {
|
|
|
|
fn to_str() -> ~str {
|
|
|
|
match self {
|
|
|
|
EnvStore => ~"EnvStore",
|
|
|
|
EnvMove => ~"EnvMove",
|
|
|
|
EnvRef => ~"EnvRef"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EnvValue {
|
|
|
|
fn to_str(ccx: @crate_ctxt) -> ~str {
|
|
|
|
fmt!("%s(%s)", self.action.to_str(), self.datum.to_str(ccx))
|
2012-01-08 15:32:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-01 19:30:05 -05:00
|
|
|
return ty::mk_imm_uniq(tcx, cbox_ty);
|
2012-02-01 20:52:08 -06:00
|
|
|
}
|
|
|
|
|
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-08-28 17:54:45 -05:00
|
|
|
bound_values: ~[EnvValue])
|
2012-07-09 21:29:14 -05:00
|
|
|
-> ty::t {
|
2012-08-28 17:54:45 -05:00
|
|
|
// determine the types of the values in the env. Note that this
|
|
|
|
// is the actual types that will be stored in the map, not the
|
|
|
|
// logical types as the user sees them, so by-ref upvars must be
|
|
|
|
// converted to ptrs.
|
|
|
|
let bound_tys = bound_values.map(|bv| {
|
|
|
|
match bv.action {
|
|
|
|
EnvStore | EnvMove => bv.datum.ty,
|
|
|
|
EnvRef => ty::mk_mut_ptr(tcx, bv.datum.ty)
|
|
|
|
}
|
|
|
|
});
|
2012-07-09 21:43:06 -05:00
|
|
|
let cdata_ty = ty::mk_tup(tcx, bound_tys);
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("cdata_ty=%s", ty_to_str(tcx, cdata_ty));
|
2012-08-01 19:30:05 -05:00
|
|
|
return cdata_ty;
|
2011-12-15 13:06:48 -06:00
|
|
|
}
|
|
|
|
|
2012-11-04 22:41:00 -06:00
|
|
|
fn allocate_cbox(bcx: block, proto: ast::Proto, cdata_ty: ty::t)
|
2012-08-28 17:54:45 -05:00
|
|
|
-> Result
|
|
|
|
{
|
2012-08-14 18:45:43 -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-08-14 18:45:43 -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-08-27 14:16:37 -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-11-04 22:41:00 -06:00
|
|
|
match proto {
|
|
|
|
ast::ProtoBox => {
|
|
|
|
malloc_raw(bcx, cdata_ty, heap_shared)
|
|
|
|
}
|
|
|
|
ast::ProtoUniq => {
|
|
|
|
malloc_raw(bcx, cdata_ty, heap_exchange)
|
|
|
|
}
|
|
|
|
ast::ProtoBorrowed => {
|
|
|
|
let cbox_ty = tuplify_box_ty(tcx, cdata_ty);
|
|
|
|
let llbox = base::alloc_ty(bcx, cbox_ty);
|
|
|
|
nuke_ref_count(bcx, llbox);
|
|
|
|
rslt(bcx, llbox)
|
|
|
|
}
|
|
|
|
ast::ProtoBare => {
|
|
|
|
let cdata_llty = type_of(bcx.ccx(), cdata_ty);
|
|
|
|
rslt(bcx, C_null(cdata_llty))
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
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-08-28 17:54:45 -05:00
|
|
|
bound_values: ~[EnvValue],
|
2012-11-04 22:41:00 -06:00
|
|
|
proto: ast::Proto) -> closure_result {
|
2012-08-14 18:45:43 -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-11-04 22:41:00 -06:00
|
|
|
let Result {bcx: bcx, val: llbox} = allocate_cbox(bcx, proto, cdata_ty);
|
2012-07-09 20:03:37 -05:00
|
|
|
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-08-22 19:24:52 -05: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-09-18 23:41:37 -05:00
|
|
|
for vec::eachi(bound_values) |i, bv| {
|
2012-08-28 17:54:45 -05:00
|
|
|
debug!("Copy %s into closure", bv.to_str(ccx));
|
2012-02-14 17:21:53 -06:00
|
|
|
|
2012-05-17 23:53:49 -05:00
|
|
|
if !ccx.sess.no_asm_comments() {
|
2012-08-22 19:24:52 -05:00
|
|
|
add_comment(bcx, fmt!("Copy %s into closure",
|
2012-08-28 17:54:45 -05:00
|
|
|
bv.to_str(ccx)));
|
2012-01-08 15:32:40 -06:00
|
|
|
}
|
|
|
|
|
2012-08-27 14:16:37 -05:00
|
|
|
let bound_data = GEPi(bcx, llbox, [0u, abi::box_field_body, i]);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
match bv.action {
|
|
|
|
EnvStore => {
|
|
|
|
bcx = bv.datum.store_to(bcx, INIT, bound_data);
|
|
|
|
}
|
|
|
|
EnvMove => {
|
|
|
|
bcx = bv.datum.move_to(bcx, INIT, bound_data);
|
|
|
|
}
|
|
|
|
EnvRef => {
|
|
|
|
Store(bcx, bv.datum.to_ref_llval(bcx), bound_data);
|
|
|
|
}
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
2012-09-18 23:41:37 -05:00
|
|
|
for vec::each(temp_cleanups) |cleanup| {
|
|
|
|
revoke_clean(bcx, *cleanup);
|
|
|
|
}
|
2011-12-14 15:57:10 -06:00
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return {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-11-04 22:41:00 -06:00
|
|
|
proto: ast::Proto,
|
2012-08-20 14:23:37 -05:00
|
|
|
include_ret_handle: Option<ValueRef>) -> closure_result {
|
2012-08-14 18:45:43 -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-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx0;;
|
2012-03-15 08:47:03 -05:00
|
|
|
let ccx = bcx.ccx(), tcx = ccx.tcx;
|
2011-12-19 14:50:31 -06:00
|
|
|
|
|
|
|
// Package up the captured upvars
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut env_vals = ~[];
|
2012-09-18 23:41:37 -05:00
|
|
|
for vec::each(cap_vars) |cap_var| {
|
|
|
|
debug!("Building closure: captured variable %?", *cap_var);
|
2012-09-19 00:54:57 -05:00
|
|
|
let datum = expr::trans_local_var(bcx, cap_var.def);
|
2012-08-06 14:34:08 -05:00
|
|
|
match cap_var.mode {
|
2012-08-28 17:54:45 -05:00
|
|
|
capture::cap_ref => {
|
2012-11-04 22:41:00 -06:00
|
|
|
assert proto == ast::ProtoBorrowed;
|
2012-09-26 19:33:34 -05:00
|
|
|
env_vals.push(EnvValue {action: EnvRef,
|
|
|
|
datum: datum});
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
capture::cap_copy => {
|
2012-09-26 19:33:34 -05:00
|
|
|
env_vals.push(EnvValue {action: EnvStore,
|
|
|
|
datum: datum});
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
capture::cap_move => {
|
2012-09-26 19:33:34 -05:00
|
|
|
env_vals.push(EnvValue {action: EnvMove,
|
|
|
|
datum: datum});
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
capture::cap_drop => {
|
|
|
|
bcx = datum.drop_val(bcx);
|
|
|
|
datum.cancel_clean(bcx);
|
|
|
|
}
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// If this is a `for` loop body, add two special environment
|
|
|
|
// variables:
|
2012-09-21 21:37:57 -05:00
|
|
|
do option::iter(&include_ret_handle) |flagptr| {
|
2012-08-28 17:54:45 -05:00
|
|
|
// Flag indicating we have returned (a by-ref bool):
|
2012-09-27 19:01:28 -05:00
|
|
|
let flag_datum = Datum {val: *flagptr, ty: ty::mk_bool(tcx),
|
2012-08-28 17:54:45 -05:00
|
|
|
mode: ByRef, source: FromLvalue};
|
2012-09-26 19:33:34 -05:00
|
|
|
env_vals.push(EnvValue {action: EnvRef,
|
|
|
|
datum: flag_datum});
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// Return value (we just pass a by-ref () and cast it later to
|
|
|
|
// the right thing):
|
|
|
|
let ret_true = match bcx.fcx.loop_ret {
|
|
|
|
Some({retptr, _}) => retptr,
|
|
|
|
None => bcx.fcx.llretptr
|
2012-03-27 05:33:13 -05:00
|
|
|
};
|
2012-08-28 17:54:45 -05:00
|
|
|
let ret_casted = PointerCast(bcx, ret_true, T_ptr(T_nil()));
|
|
|
|
let ret_datum = Datum {val: ret_casted, ty: ty::mk_nil(tcx),
|
|
|
|
mode: ByRef, source: FromLvalue};
|
2012-09-26 19:33:34 -05:00
|
|
|
env_vals.push(EnvValue {action: EnvRef,
|
|
|
|
datum: ret_datum});
|
2012-03-27 05:33:13 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-11-04 22:41:00 -06:00
|
|
|
return store_environment(bcx, env_vals, proto);
|
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,
|
2012-11-04 22:41:00 -06:00
|
|
|
proto: ast::Proto) {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = fcx.insn_ctxt("closure::load_environment");
|
2012-11-15 16:51:45 -06:00
|
|
|
|
|
|
|
let llloadenv = match fcx.llloadenv {
|
|
|
|
Some(ll) => ll,
|
|
|
|
None => {
|
|
|
|
let ll =
|
|
|
|
str::as_c_str(~"load_env",
|
|
|
|
|buf|
|
|
|
|
llvm::LLVMAppendBasicBlock(fcx.llfn, buf));
|
|
|
|
fcx.llloadenv = Some(ll);
|
|
|
|
ll
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let bcx = raw_block(fcx, false, 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-09-18 23:41:37 -05:00
|
|
|
for vec::each(cap_vars) |cap_var| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match cap_var.mode {
|
2012-08-03 21:59:04 -05:00
|
|
|
capture::cap_drop => { /* ignore */ }
|
|
|
|
_ => {
|
2012-08-27 14:16:37 -05:00
|
|
|
let mut upvarptr = GEPi(bcx, llcdata, [0u, i]);
|
2012-11-04 22:41:00 -06:00
|
|
|
match proto {
|
|
|
|
ast::ProtoBorrowed => { upvarptr = Load(bcx, upvarptr); }
|
|
|
|
ast::ProtoBox | ast::ProtoUniq | ast::ProtoBare => {}
|
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 {
|
2012-08-27 14:16:37 -05:00
|
|
|
let flagptr = Load(bcx, GEPi(bcx, llcdata, [0u, i]));
|
2012-06-25 22:00:46 -05:00
|
|
|
let retptr = Load(bcx,
|
2012-08-27 14:16:37 -05:00
|
|
|
GEPi(bcx, llcdata, [0u, i+1u]));
|
2012-08-20 14:23:37 -05:00
|
|
|
fcx.loop_ret = Some({flagptr: flagptr, retptr: retptr});
|
2012-03-27 05:33:13 -05:00
|
|
|
}
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn trans_expr_fn(bcx: block,
|
2012-11-04 22:41:00 -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-08-20 14:23:37 -05:00
|
|
|
is_loop_body: Option<Option<ValueRef>>,
|
2012-08-28 17:54:45 -05:00
|
|
|
dest: expr::Dest) -> block {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("closure::trans_expr_fn");
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
let dest_addr = match dest {
|
|
|
|
expr::SaveIn(p) => p,
|
|
|
|
expr::Ignore => {
|
|
|
|
return bcx; // closure construction is non-side-effecting
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-17 12:48:19 -05:00
|
|
|
let ccx = bcx.ccx();
|
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-18 18:18:02 -05:00
|
|
|
let sub_path = vec::append_one(bcx.fcx.path,
|
|
|
|
path_name(special_idents::anon));
|
2012-09-29 01:03:29 -05:00
|
|
|
let s = mangle_internal_name_by_path_and_seq(ccx, sub_path, ~"expr_fn");
|
2011-12-14 15:57:10 -06:00
|
|
|
let llfn = decl_internal_cdecl_fn(ccx.llmod, s, llfnty);
|
|
|
|
|
2012-11-04 22:41:00 -06:00
|
|
|
let trans_closure_env = fn@(proto: ast::Proto) -> Result {
|
2012-08-10 20:15:08 -05:00
|
|
|
let cap_vars = capture::compute_capture_vars(ccx.tcx, id, proto,
|
|
|
|
cap_clause);
|
2012-08-20 14:23:37 -05:00
|
|
|
let ret_handle = match is_loop_body { Some(x) => x, None => None };
|
2012-11-04 22:41:00 -06:00
|
|
|
let {llbox, cdata_ty, bcx} = build_closure(bcx, cap_vars, proto,
|
2012-03-27 05:33:13 -05:00
|
|
|
ret_handle);
|
2012-03-08 06:30:22 -06:00
|
|
|
trans_closure(ccx, sub_path, decl, body, llfn, no_self,
|
2012-10-08 14:39:30 -05:00
|
|
|
bcx.fcx.param_substs, id, None, |fcx| {
|
2012-03-27 05:33:13 -05:00
|
|
|
load_environment(fcx, cdata_ty, cap_vars,
|
2012-11-04 22:41:00 -06:00
|
|
|
ret_handle.is_some(), proto);
|
2012-06-30 18:19:07 -05:00
|
|
|
}, |bcx| {
|
2012-09-21 21:37:57 -05:00
|
|
|
if is_loop_body.is_some() {
|
2012-03-27 05:33:13 -05:00
|
|
|
Store(bcx, C_bool(true), bcx.fcx.llretptr);
|
|
|
|
}
|
2011-12-14 15:57:10 -06:00
|
|
|
});
|
2012-08-28 17:54:45 -05:00
|
|
|
rslt(bcx, llbox)
|
2011-12-15 13:06:48 -06:00
|
|
|
};
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let Result {bcx: bcx, val: closure} = match proto {
|
2012-11-04 22:41:00 -06:00
|
|
|
ast::ProtoBorrowed | ast::ProtoBox | ast::ProtoUniq => {
|
|
|
|
trans_closure_env(proto)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-11-04 22:41:00 -06:00
|
|
|
ast::ProtoBare => {
|
2012-08-28 17:54:45 -05:00
|
|
|
trans_closure(ccx, sub_path, decl, body, llfn, no_self, None,
|
2012-10-08 14:39:30 -05:00
|
|
|
id, None, |_fcx| { }, |_bcx| { });
|
2012-08-28 17:54:45 -05:00
|
|
|
rslt(bcx, C_null(T_opaque_box_ptr(ccx)))
|
|
|
|
}
|
2011-12-14 15:57:10 -06:00
|
|
|
};
|
2012-08-28 17:54:45 -05:00
|
|
|
fill_fn_pair(bcx, dest_addr, llfn, closure);
|
2012-07-17 12:48:19 -05:00
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return bcx;
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
|
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)
|
2012-11-04 22:41:00 -06:00
|
|
|
-> block
|
|
|
|
{
|
2012-08-14 18:45:43 -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-09-07 09:37:19 -05:00
|
|
|
let proto = ty::ty_fn_proto(t);
|
|
|
|
match proto {
|
2012-11-04 22:41:00 -06:00
|
|
|
ast::ProtoBare | ast::ProtoBorrowed => bcx,
|
|
|
|
ast::ProtoUniq | ast::ProtoBox => {
|
|
|
|
let box_cell_v = GEPi(cx, v, [0u, abi::fn_field_box]);
|
|
|
|
let box_ptr_v = Load(cx, box_cell_v);
|
|
|
|
do with_cond(cx, IsNotNull(cx, box_ptr_v)) |bcx| {
|
|
|
|
let closure_ty = ty::mk_opaque_closure_ptr(tcx, proto);
|
|
|
|
glue_fn(bcx, box_cell_v, closure_ty)
|
|
|
|
}
|
2012-09-07 09:37:19 -05:00
|
|
|
}
|
|
|
|
}
|
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-11-04 22:41:00 -06:00
|
|
|
proto: ast::Proto,
|
2012-01-05 18:19:12 -06:00
|
|
|
cboxptr: ValueRef) // ptr to ptr to the opaque closure
|
2012-11-04 22:41:00 -06:00
|
|
|
-> block
|
|
|
|
{
|
2012-01-05 18:19:12 -06:00
|
|
|
// Easy cases:
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("closure::make_opaque_cbox_take_glue");
|
2012-11-04 22:41:00 -06:00
|
|
|
match proto {
|
|
|
|
ast::ProtoBare | ast::ProtoBorrowed => {
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
ast::ProtoBox => {
|
2012-08-28 17:54:45 -05:00
|
|
|
glue::incr_refcnt_of_boxed(bcx, Load(bcx, cboxptr));
|
|
|
|
return bcx;
|
|
|
|
}
|
2012-11-04 22:41:00 -06:00
|
|
|
ast::ProtoUniq => {
|
|
|
|
/* hard case: fallthrough to code below */
|
|
|
|
}
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
|
|
|
|
2012-11-04 22:41:00 -06:00
|
|
|
// fn~ requires 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-08-27 14:16:37 -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-08-27 14:16:37 -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-07-17 12:48:19 -05:00
|
|
|
let malloc = ~"exchange_malloc";
|
|
|
|
let opaque_tydesc = PointerCast(bcx, tydesc, T_ptr(T_i8()));
|
|
|
|
let rval = alloca_zeroed(bcx, T_ptr(T_i8()));
|
2012-08-28 17:54:45 -05:00
|
|
|
let bcx = callee::trans_rtcall(bcx, malloc, ~[opaque_tydesc, sz],
|
|
|
|
expr::SaveIn(rval));
|
2012-07-17 12:48:19 -05:00
|
|
|
let cbox_out = PointerCast(bcx, Load(bcx, rval), llopaquecboxty);
|
2012-11-16 13:26:26 -06:00
|
|
|
call_memcpy(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-08-27 14:16:37 -05:00
|
|
|
let tydesc_out = GEPi(bcx, cbox_out, [0u, abi::box_field_tydesc]);
|
2012-08-28 17:54:45 -05:00
|
|
|
let bcx = glue::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-08-27 14:16:37 -05:00
|
|
|
let cdata_out = GEPi(bcx, cbox_out, [0u, abi::box_field_body]);
|
2012-08-28 17:54:45 -05:00
|
|
|
glue::call_tydesc_glue_full(bcx, cdata_out, tydesc,
|
|
|
|
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-11-04 22:41:00 -06:00
|
|
|
proto: ast::Proto,
|
2012-01-05 18:19:12 -06:00
|
|
|
cboxptr: ValueRef) // ptr to the opaque closure
|
2012-02-17 06:17:40 -06:00
|
|
|
-> block {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("closure::make_opaque_cbox_drop_glue");
|
2012-11-04 22:41:00 -06:00
|
|
|
match proto {
|
|
|
|
ast::ProtoBare | ast::ProtoBorrowed => bcx,
|
|
|
|
ast::ProtoBox => {
|
2012-08-28 17:54:45 -05:00
|
|
|
glue::decr_refcnt_maybe_free(
|
|
|
|
bcx, Load(bcx, cboxptr),
|
2012-11-04 22:41:00 -06:00
|
|
|
ty::mk_opaque_closure_ptr(bcx.tcx(), proto))
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-11-04 22:41:00 -06:00
|
|
|
ast::ProtoUniq => {
|
2012-08-28 17:54:45 -05:00
|
|
|
glue::free_ty(
|
|
|
|
bcx, cboxptr,
|
2012-11-04 22:41:00 -06:00
|
|
|
ty::mk_opaque_closure_ptr(bcx.tcx(), proto))
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
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-11-04 22:41:00 -06:00
|
|
|
proto: ast::Proto,
|
2012-08-06 16:14:17 -05:00
|
|
|
cbox: ValueRef) // ptr to ptr to the opaque closure
|
2012-02-17 06:17:40 -06:00
|
|
|
-> block {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("closure::make_opaque_cbox_free_glue");
|
2012-11-04 22:41:00 -06:00
|
|
|
match proto {
|
|
|
|
ast::ProtoBare | ast::ProtoBorrowed => {
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
ast::ProtoBox | ast::ProtoUniq => {
|
|
|
|
/* hard cases: fallthrough to code below */
|
|
|
|
}
|
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);
|
2012-08-06 16:14:17 -05:00
|
|
|
let cbox = Load(bcx, cbox);
|
2012-08-27 14:16:37 -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-08-27 14:16:37 -05:00
|
|
|
let cdata = GEPi(bcx, cbox, [0u, abi::box_field_body]);
|
2012-08-28 17:54:45 -05:00
|
|
|
glue::call_tydesc_glue_full(bcx, cdata, tydesc,
|
|
|
|
abi::tydesc_field_drop_glue, None);
|
2012-01-05 18:19:12 -06:00
|
|
|
|
|
|
|
// Free the ty descr (if necc) and the box itself
|
2012-11-04 22:41:00 -06:00
|
|
|
match proto {
|
|
|
|
ast::ProtoBox => glue::trans_free(bcx, cbox),
|
|
|
|
ast::ProtoUniq => glue::trans_unique_free(bcx, cbox),
|
|
|
|
ast::ProtoBare | ast::ProtoBorrowed => {
|
|
|
|
bcx.sess().bug(~"impossible")
|
|
|
|
}
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
|
|
|
}
|
2011-12-15 13:06:48 -06:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
|