2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2012-12-13 15:05:22 -06:00
|
|
|
use back::abi;
|
|
|
|
use back::link::{mangle_internal_name_by_path_and_seq};
|
2013-07-21 09:19:34 -05:00
|
|
|
use lib::llvm::ValueRef;
|
2013-01-10 12:59:58 -06:00
|
|
|
use middle::moves;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::base::*;
|
|
|
|
use middle::trans::build::*;
|
|
|
|
use middle::trans::common::*;
|
2013-08-11 20:12:57 -05:00
|
|
|
use middle::trans::datum::{Datum, INIT};
|
2013-08-23 11:45:02 -05:00
|
|
|
use middle::trans::debuginfo;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::expr;
|
|
|
|
use middle::trans::glue;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::type_of::*;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::ty;
|
2012-09-04 13:54:36 -05:00
|
|
|
use util::ppaux::ty_to_str;
|
2012-12-13 15:05:22 -06:00
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::vec;
|
2012-12-13 15:05:22 -06:00
|
|
|
use syntax::ast;
|
2013-03-26 15:38:07 -05:00
|
|
|
use syntax::ast_map::path_name;
|
2012-12-13 15:05:22 -06:00
|
|
|
use syntax::ast_util;
|
2013-02-25 13:11:21 -06:00
|
|
|
use syntax::parse::token::special_idents;
|
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
|
2013-03-01 14:11:07 -06:00
|
|
|
// unsigned ref_count; // only used for @fn()
|
2012-02-01 20:52:08 -06:00
|
|
|
// 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
|
2013-11-19 15:22:03 -06:00
|
|
|
// even for ~fn and ||, because we wish to keep binary compatibility
|
2012-02-01 20:52:08 -06:00
|
|
|
// 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
|
2013-06-15 22:45:48 -05:00
|
|
|
// by ptr. The routine Type::opaque_box().ptr_to() returns an
|
2012-02-01 20:52:08 -06:00
|
|
|
// 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
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub enum EnvAction {
|
2012-08-28 17:54:45 -05:00
|
|
|
/// Copy the value from this llvm ValueRef into the environment.
|
2013-01-10 12:59:58 -06:00
|
|
|
EnvCopy,
|
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
|
|
|
}
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub struct EnvValue {
|
2012-09-07 16:50:47 -05:00
|
|
|
action: EnvAction,
|
|
|
|
datum: Datum
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl EnvAction {
|
|
|
|
pub fn to_str(&self) -> ~str {
|
2013-02-03 22:47:26 -06:00
|
|
|
match *self {
|
2013-01-10 12:59:58 -06:00
|
|
|
EnvCopy => ~"EnvCopy",
|
2012-08-28 17:54:45 -05:00
|
|
|
EnvMove => ~"EnvMove",
|
|
|
|
EnvRef => ~"EnvRef"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl EnvValue {
|
2013-06-13 02:19:50 -05:00
|
|
|
pub fn to_str(&self, ccx: &CrateContext) -> ~str {
|
2013-09-28 00:38:08 -05:00
|
|
|
format!("{}({})", self.action.to_str(), self.datum.to_str(ccx))
|
2012-01-08 15:32:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub 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
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn mk_closure_tys(tcx: ty::ctxt,
|
2013-05-17 19:27:44 -05:00
|
|
|
bound_values: &[EnvValue])
|
2013-01-30 13:46:19 -06: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 {
|
2013-01-10 12:59:58 -06:00
|
|
|
EnvCopy | EnvMove => bv.datum.ty,
|
2012-08-28 17:54:45 -05:00
|
|
|
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);
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("cdata_ty={}", 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
|
|
|
}
|
|
|
|
|
2013-12-18 16:54:42 -06:00
|
|
|
fn heap_for_unique_closure(bcx: @Block, t: ty::t) -> heap {
|
2013-11-05 13:50:33 -06:00
|
|
|
if ty::type_contents(bcx.tcx(), t).owns_managed() {
|
2013-06-30 02:22:18 -05:00
|
|
|
heap_managed_unique
|
|
|
|
} else {
|
|
|
|
heap_exchange_closure
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 16:54:42 -06:00
|
|
|
pub fn allocate_cbox(bcx: @Block, sigil: ast::Sigil, cdata_ty: ty::t)
|
2013-01-30 13:46:19 -06:00
|
|
|
-> Result {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("closure::allocate_cbox");
|
2013-06-04 23:43:41 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
let tcx = ccx.tcx;
|
2012-01-05 18:19:12 -06:00
|
|
|
|
2012-02-01 20:52:08 -06:00
|
|
|
// Allocate and initialize the box:
|
2013-01-31 19:12:29 -06:00
|
|
|
match sigil {
|
|
|
|
ast::ManagedSigil => {
|
2013-09-10 20:57:24 -05:00
|
|
|
tcx.sess.bug("trying to trans allocation of @fn")
|
2012-11-04 22:41:00 -06:00
|
|
|
}
|
2013-01-31 19:12:29 -06:00
|
|
|
ast::OwnedSigil => {
|
2013-06-30 02:22:18 -05:00
|
|
|
malloc_raw(bcx, cdata_ty, heap_for_unique_closure(bcx, cdata_ty))
|
2012-11-04 22:41:00 -06:00
|
|
|
}
|
2013-01-31 19:12:29 -06:00
|
|
|
ast::BorrowedSigil => {
|
2012-11-04 22:41:00 -06:00
|
|
|
let cbox_ty = tuplify_box_ty(tcx, cdata_ty);
|
2013-06-20 14:21:37 -05:00
|
|
|
let llbox = alloc_ty(bcx, cbox_ty, "__closure");
|
2012-11-04 22:41:00 -06:00
|
|
|
rslt(bcx, llbox)
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2011-12-15 13:06:48 -06:00
|
|
|
}
|
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
pub struct ClosureResult {
|
|
|
|
llbox: ValueRef, // llvalue of ptr to closure
|
|
|
|
cdata_ty: ty::t, // type of the closure data
|
2013-12-18 16:54:42 -06:00
|
|
|
bcx: @Block // final bcx
|
2013-02-19 01:40:42 -06:00
|
|
|
}
|
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.
|
2013-12-18 16:54:42 -06:00
|
|
|
pub fn store_environment(bcx: @Block,
|
2013-01-30 13:46:19 -06:00
|
|
|
bound_values: ~[EnvValue],
|
2013-09-10 20:57:24 -05:00
|
|
|
sigil: ast::Sigil)
|
|
|
|
-> ClosureResult {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("closure::store_environment");
|
2013-06-04 23:43:41 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
let tcx = ccx.tcx;
|
2011-12-14 15:57:10 -06:00
|
|
|
|
2013-06-25 04:32:02 -05:00
|
|
|
// compute the type of the closure
|
2013-05-17 19:27:44 -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
|
|
|
// 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);
|
2013-09-01 20:45:37 -05:00
|
|
|
let cboxptr_ty = ty::mk_ptr(tcx, ty::mt {ty:cbox_ty, mutbl:ast::MutImmutable});
|
2013-06-25 11:04:50 -05:00
|
|
|
let llboxptr_ty = type_of(ccx, cboxptr_ty);
|
|
|
|
|
|
|
|
// If there are no bound values, no point in allocating anything.
|
|
|
|
if bound_values.is_empty() {
|
|
|
|
return ClosureResult {llbox: C_null(llboxptr_ty),
|
|
|
|
cdata_ty: cdata_ty,
|
|
|
|
bcx: bcx};
|
|
|
|
}
|
2012-03-12 04:05:15 -05:00
|
|
|
|
2013-06-25 11:04:50 -05:00
|
|
|
// allocate closure in the heap
|
|
|
|
let Result {bcx: bcx, val: llbox} = allocate_cbox(bcx, sigil, cdata_ty);
|
|
|
|
|
|
|
|
let llbox = PointerCast(bcx, llbox, llboxptr_ty);
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("tuplify_box_ty = {}", 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;
|
2013-08-03 11:45:23 -05:00
|
|
|
for (i, bv) in bound_values.iter().enumerate() {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Copy {} into closure", bv.to_str(ccx));
|
2012-02-14 17:21:53 -06:00
|
|
|
|
2013-05-04 13:29:08 -05:00
|
|
|
if ccx.sess.asm_comments() {
|
2013-09-28 00:38:08 -05:00
|
|
|
add_comment(bcx, format!("Copy {} 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 {
|
2013-01-10 12:59:58 -06:00
|
|
|
EnvCopy => {
|
|
|
|
bcx = bv.datum.copy_to(bcx, INIT, bound_data);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
ClosureResult { 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.
|
2013-12-18 16:54:42 -06:00
|
|
|
pub fn build_closure(bcx0: @Block,
|
2013-01-10 12:59:58 -06:00
|
|
|
cap_vars: &[moves::CaptureVar],
|
2013-08-11 20:12:57 -05:00
|
|
|
sigil: ast::Sigil) -> ClosureResult {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("closure::build_closure");
|
2013-06-25 11:04:50 -05:00
|
|
|
|
2011-12-14 15:57:10 -06:00
|
|
|
// If we need to, package up the iterator body to call
|
2013-04-22 22:19:05 -05:00
|
|
|
let bcx = bcx0;
|
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 = ~[];
|
2013-08-03 11:45:23 -05:00
|
|
|
for cap_var in cap_vars.iter() {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Building closure: captured variable {:?}", *cap_var);
|
2013-01-10 12:59:58 -06:00
|
|
|
let datum = expr::trans_local_var(bcx, cap_var.def);
|
2012-08-06 14:34:08 -05:00
|
|
|
match cap_var.mode {
|
2013-01-10 12:59:58 -06:00
|
|
|
moves::CapRef => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(sigil, ast::BorrowedSigil);
|
2012-09-26 19:33:34 -05:00
|
|
|
env_vals.push(EnvValue {action: EnvRef,
|
|
|
|
datum: datum});
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
moves::CapCopy => {
|
|
|
|
env_vals.push(EnvValue {action: EnvCopy,
|
2012-09-26 19:33:34 -05:00
|
|
|
datum: datum});
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
moves::CapMove => {
|
2012-09-26 19:33:34 -05:00
|
|
|
env_vals.push(EnvValue {action: EnvMove,
|
|
|
|
datum: datum});
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
return store_environment(bcx, env_vals, sigil);
|
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.
|
2013-07-17 05:12:08 -05:00
|
|
|
pub fn load_environment(fcx: @mut FunctionContext,
|
2013-01-30 13:46:19 -06:00
|
|
|
cdata_ty: ty::t,
|
2013-01-10 12:59:58 -06:00
|
|
|
cap_vars: &[moves::CaptureVar],
|
2013-01-31 19:12:29 -06:00
|
|
|
sigil: ast::Sigil) {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("closure::load_environment");
|
2012-11-15 16:51:45 -06:00
|
|
|
|
2013-07-17 14:59:58 -05:00
|
|
|
// Don't bother to create the block if there's nothing to load
|
2013-08-11 20:12:57 -05:00
|
|
|
if cap_vars.len() == 0 {
|
2013-07-17 14:59:58 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-03 18:59:24 -05:00
|
|
|
let bcx = fcx.entry_bcx.unwrap();
|
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:
|
2013-12-20 22:33:22 -06:00
|
|
|
let llcdata = opaque_box_body(bcx, cdata_ty, fcx.llenv.get());
|
2011-12-14 15:57:10 -06:00
|
|
|
|
2013-09-03 11:23:59 -05:00
|
|
|
// Store the pointer to closure data in an alloca for debug info because that's what the
|
|
|
|
// llvm.dbg.declare intrinsic expects
|
|
|
|
let env_pointer_alloca = if fcx.ccx.sess.opts.extra_debuginfo {
|
|
|
|
let alloc = alloc_ty(bcx, ty::mk_mut_ptr(bcx.tcx(), cdata_ty), "__debuginfo_env_ptr");
|
|
|
|
Store(bcx, llcdata, alloc);
|
|
|
|
Some(alloc)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
// Populate the upvars from the environment
|
2012-05-04 00:31:38 -05:00
|
|
|
let mut i = 0u;
|
2013-08-03 11:45:23 -05:00
|
|
|
for cap_var in cap_vars.iter() {
|
2013-01-10 12:59:58 -06:00
|
|
|
let mut upvarptr = GEPi(bcx, llcdata, [0u, i]);
|
2013-01-31 19:12:29 -06:00
|
|
|
match sigil {
|
|
|
|
ast::BorrowedSigil => { upvarptr = Load(bcx, upvarptr); }
|
|
|
|
ast::ManagedSigil | ast::OwnedSigil => {}
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
let def_id = ast_util::def_id_of_def(cap_var.def);
|
2013-12-19 22:20:04 -06:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut llupvars = fcx.llupvars.borrow_mut();
|
|
|
|
llupvars.get().insert(def_id.node, upvarptr);
|
|
|
|
}
|
2013-08-23 11:45:02 -05:00
|
|
|
|
2013-09-03 11:23:59 -05:00
|
|
|
for &env_pointer_alloca in env_pointer_alloca.iter() {
|
|
|
|
debuginfo::create_captured_var_metadata(
|
|
|
|
bcx,
|
|
|
|
def_id.node,
|
|
|
|
cdata_ty,
|
|
|
|
env_pointer_alloca,
|
|
|
|
i,
|
|
|
|
sigil,
|
|
|
|
cap_var.span);
|
2013-08-23 11:45:02 -05:00
|
|
|
}
|
|
|
|
|
2013-01-10 12:59:58 -06:00
|
|
|
i += 1u;
|
2011-12-14 15:57:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 16:54:42 -06:00
|
|
|
pub fn trans_expr_fn(bcx: @Block,
|
2013-01-31 19:12:29 -06:00
|
|
|
sigil: ast::Sigil,
|
|
|
|
decl: &ast::fn_decl,
|
2013-07-19 00:38:55 -05:00
|
|
|
body: &ast::Block,
|
2013-07-27 03:25:59 -05:00
|
|
|
outer_id: ast::NodeId,
|
|
|
|
user_id: ast::NodeId,
|
2013-12-18 16:54:42 -06:00
|
|
|
dest: expr::Dest) -> @Block {
|
2013-01-07 12:16:30 -06:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Translates the body of a closure expression.
|
|
|
|
*
|
2013-01-31 19:12:29 -06:00
|
|
|
* - `sigil`
|
2013-01-07 12:16:30 -06:00
|
|
|
* - `decl`
|
|
|
|
* - `body`
|
2013-01-12 01:42:07 -06:00
|
|
|
* - `outer_id`: The id of the closure expression with the correct
|
2013-06-06 02:38:41 -05:00
|
|
|
* type. This is usually the same as `user_id`, but in the
|
2013-01-12 01:42:07 -06:00
|
|
|
* case of a `for` loop, the `outer_id` will have the return
|
|
|
|
* type of boolean, and the `user_id` will have the return type
|
|
|
|
* of `nil`.
|
|
|
|
* - `user_id`: The id of the closure as the user expressed it.
|
|
|
|
Generally the same as `outer_id`
|
2013-01-07 12:16:30 -06:00
|
|
|
* - `cap_clause`: information about captured variables, if any.
|
2013-01-12 01:42:07 -06:00
|
|
|
* - `dest`: where to write the closure value, which must be a
|
|
|
|
(fn ptr, env) pair
|
2013-01-07 12:16:30 -06:00
|
|
|
*/
|
|
|
|
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_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();
|
2013-01-07 12:16:30 -06:00
|
|
|
let fty = node_id_type(bcx, outer_id);
|
2013-09-10 17:42:01 -05:00
|
|
|
let f = match ty::get(fty).sty {
|
|
|
|
ty::ty_closure(ref f) => f,
|
2013-10-21 15:08:31 -05:00
|
|
|
_ => fail!("expected closure")
|
2013-09-10 17:42:01 -05:00
|
|
|
};
|
2013-04-18 17:53:29 -05:00
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
let sub_path = vec::append_one(bcx.fcx.path.clone(),
|
2012-07-18 18:18:02 -05:00
|
|
|
path_name(special_idents::anon));
|
2013-01-07 16:16:52 -06:00
|
|
|
// XXX: Bad copy.
|
|
|
|
let s = mangle_internal_name_by_path_and_seq(ccx,
|
2013-07-02 14:47:32 -05:00
|
|
|
sub_path.clone(),
|
2013-05-02 03:16:07 -05:00
|
|
|
"expr_fn");
|
2013-09-10 17:42:01 -05:00
|
|
|
let llfn = decl_internal_rust_fn(ccx, f.sig.inputs, f.sig.output, s);
|
2011-12-14 15:57:10 -06:00
|
|
|
|
2013-08-11 20:12:57 -05:00
|
|
|
// set an inline hint for all closures
|
|
|
|
set_inline_hint(llfn);
|
2013-04-18 17:53:29 -05:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
let Result {bcx: bcx, val: closure} = match sigil {
|
|
|
|
ast::BorrowedSigil | ast::ManagedSigil | ast::OwnedSigil => {
|
2013-12-19 22:03:08 -06:00
|
|
|
let cap_vars = {
|
|
|
|
let capture_map = ccx.maps.capture_map.borrow();
|
|
|
|
capture_map.get().get_copy(&user_id)
|
|
|
|
};
|
2013-02-19 01:40:42 -06:00
|
|
|
let ClosureResult {llbox, cdata_ty, bcx}
|
2013-08-11 20:12:57 -05:00
|
|
|
= build_closure(bcx, cap_vars, sigil);
|
2013-03-29 18:55:04 -05:00
|
|
|
trans_closure(ccx,
|
|
|
|
sub_path,
|
|
|
|
decl,
|
|
|
|
body,
|
|
|
|
llfn,
|
|
|
|
no_self,
|
2013-06-27 19:41:35 -05:00
|
|
|
bcx.fcx.param_substs,
|
2013-03-29 18:55:04 -05:00
|
|
|
user_id,
|
|
|
|
[],
|
2013-08-11 20:12:57 -05:00
|
|
|
ty::ty_fn_ret(fty),
|
|
|
|
|fcx| load_environment(fcx, cdata_ty, cap_vars, sigil));
|
2013-01-10 12:59:58 -06:00
|
|
|
rslt(bcx, llbox)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2013-12-18 16:54:42 -06:00
|
|
|
pub fn make_closure_glue(cx: @Block,
|
2013-11-19 15:22:03 -06:00
|
|
|
v: ValueRef,
|
|
|
|
t: ty::t,
|
2013-12-18 16:54:42 -06:00
|
|
|
glue_fn: |@Block, v: ValueRef, t: ty::t|
|
|
|
|
-> @Block)
|
|
|
|
-> @Block {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("closure::make_closure_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
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
let sigil = ty::ty_closure_sigil(t);
|
|
|
|
match sigil {
|
|
|
|
ast::BorrowedSigil => bcx,
|
|
|
|
ast::OwnedSigil | ast::ManagedSigil => {
|
2012-11-04 22:41:00 -06:00
|
|
|
let box_cell_v = GEPi(cx, v, [0u, abi::fn_field_box]);
|
|
|
|
let box_ptr_v = Load(cx, box_cell_v);
|
2013-11-21 17:42:55 -06:00
|
|
|
with_cond(cx, IsNotNull(cx, box_ptr_v), |bcx| {
|
2013-01-31 19:12:29 -06:00
|
|
|
let closure_ty = ty::mk_opaque_closure_ptr(tcx, sigil);
|
2012-11-04 22:41:00 -06:00
|
|
|
glue_fn(bcx, box_cell_v, closure_ty)
|
2013-11-21 17:42:55 -06:00
|
|
|
})
|
2012-09-07 09:37:19 -05:00
|
|
|
}
|
|
|
|
}
|
2011-12-15 13:06:48 -06:00
|
|
|
}
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn make_opaque_cbox_drop_glue(
|
2013-12-18 16:54:42 -06:00
|
|
|
bcx: @Block,
|
2013-01-31 19:12:29 -06:00
|
|
|
sigil: ast::Sigil,
|
2012-01-05 18:19:12 -06:00
|
|
|
cboxptr: ValueRef) // ptr to the opaque closure
|
2013-12-18 16:54:42 -06:00
|
|
|
-> @Block {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("closure::make_opaque_cbox_drop_glue");
|
2013-01-31 19:12:29 -06:00
|
|
|
match sigil {
|
|
|
|
ast::BorrowedSigil => bcx,
|
|
|
|
ast::ManagedSigil => {
|
2013-09-10 20:57:24 -05:00
|
|
|
bcx.tcx().sess.bug("trying to trans drop glue of @fn")
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-01-31 19:12:29 -06:00
|
|
|
ast::OwnedSigil => {
|
2012-08-28 17:54:45 -05:00
|
|
|
glue::free_ty(
|
|
|
|
bcx, cboxptr,
|
2013-01-31 19:12:29 -06:00
|
|
|
ty::mk_opaque_closure_ptr(bcx.tcx(), sigil))
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 16:54:42 -06:00
|
|
|
/// `cbox` is a pointer to a pointer to an opaque closure.
|
|
|
|
pub fn make_opaque_cbox_free_glue(bcx: @Block,
|
|
|
|
sigil: ast::Sigil,
|
|
|
|
cbox: ValueRef)
|
|
|
|
-> @Block {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("closure::make_opaque_cbox_free_glue");
|
2013-01-31 19:12:29 -06:00
|
|
|
match sigil {
|
|
|
|
ast::BorrowedSigil => {
|
2012-11-04 22:41:00 -06:00
|
|
|
return bcx;
|
|
|
|
}
|
2013-01-31 19:12:29 -06:00
|
|
|
ast::ManagedSigil | ast::OwnedSigil => {
|
2012-11-04 22:41:00 -06:00
|
|
|
/* 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();
|
2013-11-21 17:42:55 -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
|
2013-06-15 22:45:48 -05:00
|
|
|
let lltydescty = ccx.tydesc_type.ptr_to();
|
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
|
2013-09-10 20:57:24 -05:00
|
|
|
glue::trans_exchange_free(bcx, cbox);
|
|
|
|
|
|
|
|
bcx
|
2013-11-21 17:42:55 -06:00
|
|
|
})
|
2011-12-15 13:06:48 -06:00
|
|
|
}
|