2014-05-28 14:36:05 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2014-01-27 06:18:36 -06:00
|
|
|
use back::link::mangle_internal_name_by_path_and_seq;
|
2015-01-29 06:03:34 -06:00
|
|
|
use llvm::ValueRef;
|
2014-09-14 15:55:25 -05:00
|
|
|
use middle::mem_categorization::Typer;
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::adt;
|
|
|
|
use trans::base::*;
|
|
|
|
use trans::build::*;
|
|
|
|
use trans::cleanup::{CleanupMethods, ScopeId};
|
|
|
|
use trans::common::*;
|
2015-01-18 12:27:41 -06:00
|
|
|
use trans::datum::{Datum, rvalue_scratch_datum};
|
2015-01-04 10:47:58 -06:00
|
|
|
use trans::datum::{Rvalue, ByValue};
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::debuginfo;
|
|
|
|
use trans::expr;
|
2015-01-03 21:42:21 -06:00
|
|
|
use trans::monomorphize::{self, MonoId};
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::type_of::*;
|
2015-01-24 14:00:03 -06:00
|
|
|
use middle::ty::{self, ClosureTyper};
|
2015-01-01 16:58:57 -06:00
|
|
|
use middle::subst::{Substs};
|
2014-11-15 19:30:33 -06:00
|
|
|
use session::config::FullDebugInfo;
|
2012-12-13 15:05:22 -06:00
|
|
|
|
|
|
|
use syntax::ast;
|
2014-05-29 00:26:56 -05:00
|
|
|
use syntax::ast_util;
|
2011-12-14 15:57:10 -06:00
|
|
|
|
|
|
|
|
2015-01-24 14:00:03 -06:00
|
|
|
fn load_closure_environment<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
|
|
|
arg_scope_id: ScopeId,
|
|
|
|
freevars: &[ty::Freevar])
|
2015-01-24 14:54:52 -06:00
|
|
|
-> Block<'blk, 'tcx>
|
|
|
|
{
|
2015-01-24 14:00:03 -06:00
|
|
|
let _icx = push_ctxt("closure::load_closure_environment");
|
2014-05-29 00:26:56 -05:00
|
|
|
|
2014-07-30 00:08:39 -05:00
|
|
|
// Special case for small by-value selfs.
|
2014-09-29 14:11:30 -05:00
|
|
|
let closure_id = ast_util::local_def(bcx.fcx.id);
|
2015-01-24 14:00:03 -06:00
|
|
|
let self_type = self_type_for_closure(bcx.ccx(), closure_id,
|
2014-10-18 12:46:57 -05:00
|
|
|
node_id_type(bcx, closure_id.node));
|
2015-01-24 14:00:03 -06:00
|
|
|
let kind = kind_for_closure(bcx.ccx(), closure_id);
|
|
|
|
let llenv = if kind == ty::FnOnceClosureKind &&
|
2014-07-30 00:08:39 -05:00
|
|
|
!arg_is_indirect(bcx.ccx(), self_type) {
|
|
|
|
let datum = rvalue_scratch_datum(bcx,
|
|
|
|
self_type,
|
2015-01-24 14:00:03 -06:00
|
|
|
"closure_env");
|
2014-07-30 00:08:39 -05:00
|
|
|
store_ty(bcx, bcx.fcx.llenv.unwrap(), datum.val, self_type);
|
|
|
|
datum.val
|
|
|
|
} else {
|
|
|
|
bcx.fcx.llenv.unwrap()
|
|
|
|
};
|
|
|
|
|
2014-11-27 08:52:16 -06: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 bcx.sess().opts.debuginfo == FullDebugInfo {
|
2014-12-05 17:35:16 -06:00
|
|
|
let alloc = alloca(bcx, val_ty(llenv), "__debuginfo_env_ptr");
|
2014-11-27 08:52:16 -06:00
|
|
|
Store(bcx, llenv, alloc);
|
|
|
|
Some(alloc)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2014-05-29 00:26:56 -05:00
|
|
|
for (i, freevar) in freevars.iter().enumerate() {
|
2015-01-24 14:54:52 -06:00
|
|
|
let upvar_id = ty::UpvarId { var_id: freevar.def.local_node_id(),
|
|
|
|
closure_expr_id: closure_id.node };
|
|
|
|
let upvar_capture = bcx.tcx().upvar_capture(upvar_id).unwrap();
|
2014-11-17 02:39:01 -06:00
|
|
|
let mut upvar_ptr = GEPi(bcx, llenv, &[0, i]);
|
2015-01-24 14:54:52 -06:00
|
|
|
let captured_by_ref = match upvar_capture {
|
|
|
|
ty::UpvarCapture::ByValue => false,
|
|
|
|
ty::UpvarCapture::ByRef(..) => {
|
2014-11-27 08:52:16 -06:00
|
|
|
upvar_ptr = Load(bcx, upvar_ptr);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
};
|
2014-05-29 00:26:56 -05:00
|
|
|
let def_id = freevar.def.def_id();
|
|
|
|
bcx.fcx.llupvars.borrow_mut().insert(def_id.node, upvar_ptr);
|
2014-07-30 00:08:39 -05:00
|
|
|
|
2015-01-24 14:54:52 -06:00
|
|
|
if kind == ty::FnOnceClosureKind && !captured_by_ref {
|
2014-07-30 00:08:39 -05:00
|
|
|
bcx.fcx.schedule_drop_mem(arg_scope_id,
|
|
|
|
upvar_ptr,
|
|
|
|
node_id_type(bcx, def_id.node))
|
|
|
|
}
|
2014-11-27 08:52:16 -06:00
|
|
|
|
|
|
|
if let Some(env_pointer_alloca) = env_pointer_alloca {
|
|
|
|
debuginfo::create_captured_var_metadata(
|
|
|
|
bcx,
|
|
|
|
def_id.node,
|
|
|
|
env_pointer_alloca,
|
|
|
|
i,
|
|
|
|
captured_by_ref,
|
|
|
|
freevar.span);
|
|
|
|
}
|
2014-05-29 00:26:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bcx
|
|
|
|
}
|
|
|
|
|
2015-01-26 14:18:48 -06:00
|
|
|
pub enum ClosureEnv<'a> {
|
2014-09-29 14:11:30 -05:00
|
|
|
NotClosure,
|
2015-01-26 14:18:48 -06:00
|
|
|
Closure(&'a [ty::Freevar]),
|
2014-09-29 14:11:30 -05:00
|
|
|
}
|
|
|
|
|
2015-01-26 14:18:48 -06:00
|
|
|
impl<'a> ClosureEnv<'a> {
|
|
|
|
pub fn load<'blk,'tcx>(self, bcx: Block<'blk, 'tcx>, arg_scope: ScopeId)
|
|
|
|
-> Block<'blk, 'tcx>
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
ClosureEnv::NotClosure => bcx,
|
|
|
|
ClosureEnv::Closure(freevars) => {
|
|
|
|
if freevars.is_empty() {
|
|
|
|
bcx
|
|
|
|
} else {
|
|
|
|
load_closure_environment(bcx, arg_scope, freevars)
|
|
|
|
}
|
2014-09-29 14:11:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-24 14:15:08 -06:00
|
|
|
/// Returns the LLVM function declaration for a closure, creating it if
|
|
|
|
/// necessary. If the ID does not correspond to a closure ID, returns None.
|
2015-01-24 14:00:03 -06:00
|
|
|
pub fn get_or_create_declaration_if_closure<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
closure_id: ast::DefId,
|
|
|
|
substs: &Substs<'tcx>)
|
|
|
|
-> Option<Datum<'tcx, Rvalue>> {
|
2015-01-30 10:37:23 -06:00
|
|
|
if !ccx.tcx().closure_kinds.borrow().contains_key(&closure_id) {
|
2015-01-24 14:15:08 -06:00
|
|
|
// Not a closure.
|
2014-05-29 00:26:56 -05:00
|
|
|
return None
|
|
|
|
}
|
|
|
|
|
2015-01-04 10:47:58 -06:00
|
|
|
let function_type = ty::node_id_to_type(ccx.tcx(), closure_id.node);
|
|
|
|
let function_type = monomorphize::apply_param_substs(ccx.tcx(), substs, &function_type);
|
2014-11-06 01:50:10 -06:00
|
|
|
|
|
|
|
// Normalize type so differences in regions and typedefs don't cause
|
|
|
|
// duplicate declarations
|
2015-01-07 08:36:11 -06:00
|
|
|
let function_type = erase_regions(ccx.tcx(), &function_type);
|
2014-10-31 03:51:16 -05:00
|
|
|
let params = match function_type.sty {
|
2015-01-29 06:03:34 -06:00
|
|
|
ty::ty_closure(_, _, substs) => &substs.types,
|
2014-10-18 12:46:57 -05:00
|
|
|
_ => unreachable!()
|
|
|
|
};
|
|
|
|
let mono_id = MonoId {
|
|
|
|
def: closure_id,
|
|
|
|
params: params
|
|
|
|
};
|
|
|
|
|
2015-01-24 14:00:03 -06:00
|
|
|
match ccx.closure_vals().borrow().get(&mono_id) {
|
2015-01-04 10:47:58 -06:00
|
|
|
Some(&llfn) => {
|
2015-01-24 14:15:08 -06:00
|
|
|
debug!("get_or_create_declaration_if_closure(): found closure");
|
2015-01-04 10:47:58 -06:00
|
|
|
return Some(Datum::new(llfn, function_type, Rvalue::new(ByValue)))
|
2014-05-29 00:26:56 -05:00
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
|
2014-09-05 11:18:53 -05:00
|
|
|
let symbol = ccx.tcx().map.with_path(closure_id.node, |path| {
|
2015-01-24 14:00:03 -06:00
|
|
|
mangle_internal_name_by_path_and_seq(path, "closure")
|
2014-05-29 00:26:56 -05:00
|
|
|
});
|
|
|
|
|
2015-02-18 13:48:57 -06:00
|
|
|
let llfn = decl_internal_rust_fn(ccx, function_type, &symbol[..]);
|
2014-05-29 00:26:56 -05:00
|
|
|
|
|
|
|
// set an inline hint for all closures
|
|
|
|
set_inline_hint(llfn);
|
|
|
|
|
2015-01-24 14:00:03 -06:00
|
|
|
debug!("get_or_create_declaration_if_closure(): inserting new \
|
2014-12-20 02:09:35 -06:00
|
|
|
closure {:?} (type {})",
|
2014-10-18 12:46:57 -05:00
|
|
|
mono_id,
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tn().type_to_string(val_ty(llfn)));
|
2015-01-24 14:00:03 -06:00
|
|
|
ccx.closure_vals().borrow_mut().insert(mono_id, llfn);
|
2014-05-29 00:26:56 -05:00
|
|
|
|
2015-01-04 10:47:58 -06:00
|
|
|
Some(Datum::new(llfn, function_type, Rvalue::new(ByValue)))
|
2014-05-29 00:26:56 -05:00
|
|
|
}
|
|
|
|
|
2015-01-29 06:03:34 -06:00
|
|
|
pub enum Dest<'a, 'tcx: 'a> {
|
|
|
|
SaveIn(Block<'a, 'tcx>, ValueRef),
|
|
|
|
Ignore(&'a CrateContext<'a, 'tcx>)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn trans_closure_expr<'a, 'tcx>(dest: Dest<'a, 'tcx>,
|
|
|
|
decl: &ast::FnDecl,
|
|
|
|
body: &ast::Block,
|
|
|
|
id: ast::NodeId,
|
|
|
|
param_substs: &'tcx Substs<'tcx>)
|
|
|
|
-> Option<Block<'a, 'tcx>>
|
2015-01-01 16:58:57 -06:00
|
|
|
{
|
2015-01-29 06:03:34 -06:00
|
|
|
let ccx = match dest {
|
|
|
|
Dest::SaveIn(bcx, _) => bcx.ccx(),
|
|
|
|
Dest::Ignore(ccx) => ccx
|
|
|
|
};
|
|
|
|
let tcx = ccx.tcx();
|
2015-01-24 14:00:03 -06:00
|
|
|
let _icx = push_ctxt("closure::trans_closure");
|
2014-05-29 00:26:56 -05:00
|
|
|
|
2015-01-24 14:00:03 -06:00
|
|
|
debug!("trans_closure()");
|
2014-05-29 00:26:56 -05:00
|
|
|
|
|
|
|
let closure_id = ast_util::local_def(id);
|
2015-01-24 14:00:03 -06:00
|
|
|
let llfn = get_or_create_declaration_if_closure(
|
2015-01-29 06:03:34 -06:00
|
|
|
ccx,
|
2014-11-06 01:50:10 -06:00
|
|
|
closure_id,
|
2015-01-29 06:03:34 -06:00
|
|
|
param_substs).unwrap();
|
2014-05-29 00:26:56 -05:00
|
|
|
|
2015-01-01 16:58:57 -06:00
|
|
|
// Get the type of this closure. Use the current `param_substs` as
|
|
|
|
// the closure substitutions. This makes sense because the closure
|
|
|
|
// takes the same set of type arguments as the enclosing fn, and
|
2015-01-24 14:00:03 -06:00
|
|
|
// this function (`trans_closure`) is invoked at the point
|
2015-01-01 16:58:57 -06:00
|
|
|
// of the closure expression.
|
2015-01-29 06:03:34 -06:00
|
|
|
let typer = NormalizingClosureTyper::new(tcx);
|
|
|
|
let function_type = typer.closure_type(closure_id, param_substs);
|
2014-05-29 00:26:56 -05:00
|
|
|
|
2014-09-14 15:55:25 -05:00
|
|
|
let freevars: Vec<ty::Freevar> =
|
2015-02-14 23:19:50 -06:00
|
|
|
ty::with_freevars(tcx, id, |fv| fv.iter().cloned().collect());
|
2014-05-29 00:26:56 -05:00
|
|
|
|
2015-01-29 06:03:34 -06:00
|
|
|
let sig = ty::erase_late_bound_regions(tcx, &function_type.sig);
|
2015-01-06 04:03:42 -06:00
|
|
|
|
2015-01-29 06:03:34 -06:00
|
|
|
trans_closure(ccx,
|
2014-05-29 00:26:56 -05:00
|
|
|
decl,
|
|
|
|
body,
|
2015-01-04 10:47:58 -06:00
|
|
|
llfn.val,
|
2015-01-29 06:03:34 -06:00
|
|
|
param_substs,
|
2014-05-29 00:26:56 -05:00
|
|
|
id,
|
2014-11-17 02:39:01 -06:00
|
|
|
&[],
|
2015-01-06 04:03:42 -06:00
|
|
|
sig.output,
|
2015-01-04 08:55:16 -06:00
|
|
|
function_type.abi,
|
2015-02-18 13:48:57 -06:00
|
|
|
ClosureEnv::Closure(&freevars[..]));
|
2014-05-29 00:26:56 -05:00
|
|
|
|
|
|
|
// Don't hoist this to the top of the function. It's perfectly legitimate
|
2015-01-24 14:15:08 -06:00
|
|
|
// to have a zero-size closure (in which case dest will be `Ignore`) and
|
|
|
|
// we must still generate the closure body.
|
2015-01-29 06:03:34 -06:00
|
|
|
let (mut bcx, dest_addr) = match dest {
|
|
|
|
Dest::SaveIn(bcx, p) => (bcx, p),
|
|
|
|
Dest::Ignore(_) => {
|
2015-01-24 14:00:03 -06:00
|
|
|
debug!("trans_closure() ignoring result");
|
2015-01-29 06:03:34 -06:00
|
|
|
return None;
|
2014-05-29 00:26:56 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-29 06:03:34 -06:00
|
|
|
let repr = adt::represent_type(ccx, node_id_type(bcx, id));
|
2014-05-29 00:26:56 -05:00
|
|
|
|
|
|
|
// Create the closure.
|
2014-09-29 14:11:30 -05:00
|
|
|
for (i, freevar) in freevars.iter().enumerate() {
|
2014-05-29 00:26:56 -05:00
|
|
|
let datum = expr::trans_local_var(bcx, freevar.def);
|
|
|
|
let upvar_slot_dest = adt::trans_field_ptr(bcx,
|
|
|
|
&*repr,
|
|
|
|
dest_addr,
|
|
|
|
0,
|
2014-07-30 00:08:39 -05:00
|
|
|
i);
|
2015-01-24 14:54:52 -06:00
|
|
|
let upvar_id = ty::UpvarId { var_id: freevar.def.local_node_id(),
|
|
|
|
closure_expr_id: id };
|
2015-01-29 06:03:34 -06:00
|
|
|
match tcx.upvar_capture(upvar_id).unwrap() {
|
2015-01-24 14:54:52 -06:00
|
|
|
ty::UpvarCapture::ByValue => {
|
2014-10-02 21:43:16 -05:00
|
|
|
bcx = datum.store_to(bcx, upvar_slot_dest);
|
|
|
|
}
|
2015-01-24 14:54:52 -06:00
|
|
|
ty::UpvarCapture::ByRef(..) => {
|
2014-10-02 21:43:16 -05:00
|
|
|
Store(bcx, datum.to_llref(), upvar_slot_dest);
|
|
|
|
}
|
|
|
|
}
|
2014-05-29 00:26:56 -05:00
|
|
|
}
|
2014-07-10 17:09:21 -05:00
|
|
|
adt::trans_set_discr(bcx, &*repr, dest_addr, 0);
|
2014-05-29 00:26:56 -05:00
|
|
|
|
2015-01-29 06:03:34 -06:00
|
|
|
Some(bcx)
|
2014-05-29 00:26:56 -05:00
|
|
|
}
|
2015-01-01 18:56:28 -06:00
|
|
|
|