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.
|
|
|
|
|
2015-02-15 14:09:26 -06:00
|
|
|
use arena::TypedArena;
|
|
|
|
use back::link::{self, mangle_internal_name_by_path_and_seq};
|
2015-06-18 13:07:36 -05:00
|
|
|
use llvm::{ValueRef, get_params};
|
2015-08-16 05:32:28 -05:00
|
|
|
use middle::def_id::DefId;
|
2015-06-28 00:04:15 -05:00
|
|
|
use middle::infer;
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::adt;
|
2015-02-28 15:53:12 -06:00
|
|
|
use trans::attributes;
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::base::*;
|
|
|
|
use trans::build::*;
|
2015-02-15 14:09:26 -06:00
|
|
|
use trans::callee::{self, ArgVals, Callee, TraitItem, MethodData};
|
|
|
|
use trans::cleanup::{CleanupMethods, CustomScope, ScopeId};
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::common::*;
|
2015-12-11 21:29:35 -06:00
|
|
|
use trans::datum::{self, Datum, rvalue_scratch_datum, Rvalue};
|
2015-02-15 14:09:26 -06:00
|
|
|
use trans::debuginfo::{self, DebugLoc};
|
2015-03-03 17:08:06 -06:00
|
|
|
use trans::declare;
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::expr;
|
2015-07-16 08:46:35 -05:00
|
|
|
use trans::monomorphize::{MonoId};
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::type_of::*;
|
2016-01-16 09:03:09 -06:00
|
|
|
use trans::Disr;
|
2015-06-30 04:18:03 -05:00
|
|
|
use middle::ty;
|
2014-11-15 19:30:33 -06:00
|
|
|
use session::config::FullDebugInfo;
|
2012-12-13 15:05:22 -06:00
|
|
|
|
2015-02-15 14:09:26 -06:00
|
|
|
use syntax::abi::RustCall;
|
2012-12-13 15:05:22 -06:00
|
|
|
use syntax::ast;
|
2015-12-31 07:06:23 -06:00
|
|
|
use syntax::attr::{ThinAttributes, ThinAttributesExt};
|
2011-12-14 15:57:10 -06:00
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
use rustc_front::hir;
|
|
|
|
|
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>,
|
2015-09-17 13:29:59 -05:00
|
|
|
closure_def_id: DefId,
|
2015-01-24 14:00:03 -06:00
|
|
|
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.
|
2015-09-17 13:29:59 -05:00
|
|
|
let closure_ty = node_id_type(bcx, bcx.fcx.id);
|
|
|
|
let self_type = self_type_for_closure(bcx.ccx(), closure_def_id, closure_ty);
|
|
|
|
let kind = kind_for_closure(bcx.ccx(), closure_def_id);
|
2015-01-24 14:00:03 -06:00
|
|
|
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-09-07 13:10:25 -05:00
|
|
|
let upvar_id = ty::UpvarId { var_id: freevar.def.var_id(),
|
2015-09-04 12:52:28 -05:00
|
|
|
closure_expr_id: bcx.fcx.id };
|
2015-01-24 14:54:52 -06:00
|
|
|
let upvar_capture = bcx.tcx().upvar_capture(upvar_id).unwrap();
|
2015-08-24 15:51:57 -05:00
|
|
|
let mut upvar_ptr = StructGEP(bcx, llenv, 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
|
|
|
|
}
|
|
|
|
};
|
2015-09-07 13:10:25 -05:00
|
|
|
let node_id = freevar.def.var_id();
|
2015-09-04 12:52:28 -05:00
|
|
|
bcx.fcx.llupvars.borrow_mut().insert(node_id, 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 {
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
let hint = bcx.fcx.lldropflag_hints.borrow().hint_datum(upvar_id.var_id);
|
2014-07-30 00:08:39 -05:00
|
|
|
bcx.fcx.schedule_drop_mem(arg_scope_id,
|
|
|
|
upvar_ptr,
|
2015-09-04 12:52:28 -05:00
|
|
|
node_id_type(bcx, node_id),
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
hint)
|
2014-07-30 00:08:39 -05:00
|
|
|
}
|
2014-11-27 08:52:16 -06:00
|
|
|
|
|
|
|
if let Some(env_pointer_alloca) = env_pointer_alloca {
|
|
|
|
debuginfo::create_captured_var_metadata(
|
|
|
|
bcx,
|
2015-09-04 12:52:28 -05:00
|
|
|
node_id,
|
2014-11-27 08:52:16 -06:00
|
|
|
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-09-17 13:29:59 -05:00
|
|
|
Closure(DefId, &'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,
|
2015-09-17 13:29:59 -05:00
|
|
|
ClosureEnv::Closure(def_id, freevars) => {
|
2015-01-26 14:18:48 -06:00
|
|
|
if freevars.is_empty() {
|
|
|
|
bcx
|
|
|
|
} else {
|
2015-09-17 13:29:59 -05:00
|
|
|
load_closure_environment(bcx, def_id, arg_scope, freevars)
|
2015-01-26 14:18:48 -06:00
|
|
|
}
|
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-07-16 08:46:35 -05:00
|
|
|
pub fn get_or_create_closure_declaration<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
2015-08-16 05:32:28 -05:00
|
|
|
closure_id: DefId,
|
2015-07-16 08:46:35 -05:00
|
|
|
substs: &ty::ClosureSubsts<'tcx>)
|
|
|
|
-> ValueRef {
|
2014-11-06 01:50:10 -06:00
|
|
|
// Normalize type so differences in regions and typedefs don't cause
|
|
|
|
// duplicate declarations
|
2015-09-14 16:47:14 -05:00
|
|
|
let substs = ccx.tcx().erase_regions(substs);
|
2014-10-18 12:46:57 -05:00
|
|
|
let mono_id = MonoId {
|
|
|
|
def: closure_id,
|
2015-07-16 08:46:35 -05:00
|
|
|
params: &substs.func_substs.types
|
2014-10-18 12:46:57 -05:00
|
|
|
};
|
|
|
|
|
2015-07-16 08:46:35 -05:00
|
|
|
if let Some(&llfn) = ccx.closure_vals().borrow().get(&mono_id) {
|
2015-07-23 20:08:29 -05:00
|
|
|
debug!("get_or_create_closure_declaration(): found closure {:?}: {:?}",
|
2015-07-16 08:46:35 -05:00
|
|
|
mono_id, ccx.tn().val_to_string(llfn));
|
|
|
|
return llfn;
|
2014-05-29 00:26:56 -05:00
|
|
|
}
|
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
let path = ccx.tcx().def_path(closure_id);
|
|
|
|
let symbol = mangle_internal_name_by_path_and_seq(path, "closure");
|
2014-05-29 00:26:56 -05:00
|
|
|
|
2015-07-16 08:46:35 -05:00
|
|
|
let function_type = ccx.tcx().mk_closure_from_closure_substs(closure_id, Box::new(substs));
|
2015-07-24 03:48:32 -05:00
|
|
|
let llfn = declare::define_internal_rust_fn(ccx, &symbol[..], function_type);
|
2014-05-29 00:26:56 -05:00
|
|
|
|
|
|
|
// set an inline hint for all closures
|
2015-03-03 17:03:25 -06:00
|
|
|
attributes::inline(llfn, attributes::InlineAttr::Hint);
|
2014-05-29 00:26:56 -05:00
|
|
|
|
2015-01-24 14:00:03 -06:00
|
|
|
debug!("get_or_create_declaration_if_closure(): inserting new \
|
2015-06-02 18:31:23 -05:00
|
|
|
closure {:?} (type {}): {:?}",
|
2014-10-18 12:46:57 -05:00
|
|
|
mono_id,
|
2015-06-02 18:31:23 -05:00
|
|
|
ccx.tn().type_to_string(val_ty(llfn)),
|
|
|
|
ccx.tn().val_to_string(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-07-16 08:46:35 -05:00
|
|
|
llfn
|
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>,
|
2015-07-31 02:04:06 -05:00
|
|
|
decl: &hir::FnDecl,
|
|
|
|
body: &hir::Block,
|
2015-01-29 06:03:34 -06:00
|
|
|
id: ast::NodeId,
|
2015-09-17 13:29:59 -05:00
|
|
|
closure_def_id: DefId, // (*)
|
2015-12-31 07:06:23 -06:00
|
|
|
closure_substs: &'tcx ty::ClosureSubsts<'tcx>,
|
|
|
|
closure_expr_attrs: &ThinAttributes)
|
2015-01-29 06:03:34 -06:00
|
|
|
-> Option<Block<'a, 'tcx>>
|
2015-01-01 16:58:57 -06:00
|
|
|
{
|
2015-09-17 13:29:59 -05:00
|
|
|
// (*) Note that in the case of inlined functions, the `closure_def_id` will be the
|
|
|
|
// defid of the closure in its original crate, whereas `id` will be the id of the local
|
|
|
|
// inlined copy.
|
|
|
|
|
2015-07-16 08:46:35 -05:00
|
|
|
let param_substs = closure_substs.func_substs;
|
|
|
|
|
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-06-02 18:31:23 -05:00
|
|
|
let _icx = push_ctxt("closure::trans_closure_expr");
|
2014-05-29 00:26:56 -05:00
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
debug!("trans_closure_expr(id={:?}, closure_def_id={:?}, closure_substs={:?})",
|
|
|
|
id, closure_def_id, closure_substs);
|
2014-05-29 00:26:56 -05:00
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
let llfn = get_or_create_closure_declaration(ccx, closure_def_id, closure_substs);
|
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-06-28 00:04:15 -05:00
|
|
|
|
|
|
|
let infcx = infer::normalizing_infer_ctxt(ccx.tcx(), &ccx.tcx().tables);
|
2015-09-17 13:29:59 -05:00
|
|
|
let function_type = infcx.closure_type(closure_def_id, closure_substs);
|
2014-05-29 00:26:56 -05:00
|
|
|
|
2014-09-14 15:55:25 -05:00
|
|
|
let freevars: Vec<ty::Freevar> =
|
2015-06-25 15:42:17 -05:00
|
|
|
tcx.with_freevars(id, |fv| fv.iter().cloned().collect());
|
2014-05-29 00:26:56 -05:00
|
|
|
|
2015-06-25 15:42:17 -05:00
|
|
|
let sig = tcx.erase_late_bound_regions(&function_type.sig);
|
2015-11-14 14:12:12 -06:00
|
|
|
let sig = infer::normalize_associated_type(ccx.tcx(), &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-07-16 08:46:35 -05:00
|
|
|
llfn,
|
2015-01-29 06:03:34 -06:00
|
|
|
param_substs,
|
2014-05-29 00:26:56 -05:00
|
|
|
id,
|
2015-12-31 07:06:23 -06:00
|
|
|
closure_expr_attrs.as_attr_slice(),
|
2015-01-06 04:03:42 -06:00
|
|
|
sig.output,
|
2015-01-04 08:55:16 -06:00
|
|
|
function_type.abi,
|
2015-09-17 13:29:59 -05:00
|
|
|
ClosureEnv::Closure(closure_def_id, &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-06-02 18:31:23 -05:00
|
|
|
debug!("trans_closure_expr() 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);
|
2015-12-06 07:38:29 -06:00
|
|
|
let upvar_slot_dest = adt::trans_field_ptr(
|
2016-01-16 09:03:09 -06:00
|
|
|
bcx, &*repr, adt::MaybeSizedValue::sized(dest_addr), Disr(0), i);
|
2015-09-07 13:10:25 -05:00
|
|
|
let upvar_id = ty::UpvarId { var_id: freevar.def.var_id(),
|
2015-01-24 14:54:52 -06:00
|
|
|
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
|
|
|
}
|
2016-01-16 09:03:09 -06:00
|
|
|
adt::trans_set_discr(bcx, &*repr, dest_addr, Disr(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-02-15 14:09:26 -06:00
|
|
|
|
|
|
|
pub fn trans_closure_method<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>,
|
2015-08-16 05:32:28 -05:00
|
|
|
closure_def_id: DefId,
|
2015-07-16 08:46:35 -05:00
|
|
|
substs: ty::ClosureSubsts<'tcx>,
|
2015-02-15 14:09:26 -06:00
|
|
|
trait_closure_kind: ty::ClosureKind)
|
|
|
|
-> ValueRef
|
|
|
|
{
|
2015-07-16 08:46:35 -05:00
|
|
|
// If this is a closure, redirect to it.
|
|
|
|
let llfn = get_or_create_closure_declaration(ccx, closure_def_id, &substs);
|
2015-02-15 14:09:26 -06:00
|
|
|
|
|
|
|
// If the closure is a Fn closure, but a FnOnce is needed (etc),
|
|
|
|
// then adapt the self type
|
|
|
|
let closure_kind = ccx.tcx().closure_kind(closure_def_id);
|
|
|
|
trans_closure_adapter_shim(ccx,
|
|
|
|
closure_def_id,
|
|
|
|
substs,
|
|
|
|
closure_kind,
|
|
|
|
trait_closure_kind,
|
|
|
|
llfn)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_closure_adapter_shim<'a, 'tcx>(
|
|
|
|
ccx: &'a CrateContext<'a, 'tcx>,
|
2015-08-16 05:32:28 -05:00
|
|
|
closure_def_id: DefId,
|
2015-07-16 08:46:35 -05:00
|
|
|
substs: ty::ClosureSubsts<'tcx>,
|
2015-02-15 14:09:26 -06:00
|
|
|
llfn_closure_kind: ty::ClosureKind,
|
|
|
|
trait_closure_kind: ty::ClosureKind,
|
|
|
|
llfn: ValueRef)
|
|
|
|
-> ValueRef
|
|
|
|
{
|
|
|
|
let _icx = push_ctxt("trans_closure_adapter_shim");
|
|
|
|
let tcx = ccx.tcx();
|
|
|
|
|
|
|
|
debug!("trans_closure_adapter_shim(llfn_closure_kind={:?}, \
|
|
|
|
trait_closure_kind={:?}, \
|
|
|
|
llfn={})",
|
|
|
|
llfn_closure_kind,
|
|
|
|
trait_closure_kind,
|
|
|
|
ccx.tn().val_to_string(llfn));
|
|
|
|
|
|
|
|
match (llfn_closure_kind, trait_closure_kind) {
|
|
|
|
(ty::FnClosureKind, ty::FnClosureKind) |
|
|
|
|
(ty::FnMutClosureKind, ty::FnMutClosureKind) |
|
|
|
|
(ty::FnOnceClosureKind, ty::FnOnceClosureKind) => {
|
|
|
|
// No adapter needed.
|
|
|
|
llfn
|
|
|
|
}
|
|
|
|
(ty::FnClosureKind, ty::FnMutClosureKind) => {
|
|
|
|
// The closure fn `llfn` is a `fn(&self, ...)`. We want a
|
|
|
|
// `fn(&mut self, ...)`. In fact, at trans time, these are
|
|
|
|
// basically the same thing, so we can just return llfn.
|
|
|
|
llfn
|
|
|
|
}
|
|
|
|
(ty::FnClosureKind, ty::FnOnceClosureKind) |
|
|
|
|
(ty::FnMutClosureKind, ty::FnOnceClosureKind) => {
|
|
|
|
// The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
|
|
|
|
// self, ...)`. We want a `fn(self, ...)`. We can produce
|
|
|
|
// this by doing something like:
|
|
|
|
//
|
|
|
|
// fn call_once(self, ...) { call_mut(&self, ...) }
|
|
|
|
// fn call_once(mut self, ...) { call_mut(&mut self, ...) }
|
|
|
|
//
|
|
|
|
// These are both the same at trans time.
|
2015-07-16 08:46:35 -05:00
|
|
|
trans_fn_once_adapter_shim(ccx, closure_def_id, substs, llfn)
|
2015-02-15 14:09:26 -06:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
tcx.sess.bug(&format!("trans_closure_adapter_shim: cannot convert {:?} to {:?}",
|
|
|
|
llfn_closure_kind,
|
|
|
|
trait_closure_kind));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_fn_once_adapter_shim<'a, 'tcx>(
|
|
|
|
ccx: &'a CrateContext<'a, 'tcx>,
|
2015-08-16 05:32:28 -05:00
|
|
|
closure_def_id: DefId,
|
2015-07-16 08:46:35 -05:00
|
|
|
substs: ty::ClosureSubsts<'tcx>,
|
2015-02-15 14:09:26 -06:00
|
|
|
llreffn: ValueRef)
|
|
|
|
-> ValueRef
|
|
|
|
{
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("trans_fn_once_adapter_shim(closure_def_id={:?}, substs={:?}, llreffn={})",
|
|
|
|
closure_def_id,
|
|
|
|
substs,
|
2015-02-15 14:09:26 -06:00
|
|
|
ccx.tn().val_to_string(llreffn));
|
|
|
|
|
|
|
|
let tcx = ccx.tcx();
|
2015-06-28 00:04:15 -05:00
|
|
|
let infcx = infer::normalizing_infer_ctxt(ccx.tcx(), &ccx.tcx().tables);
|
2015-02-15 14:09:26 -06:00
|
|
|
|
|
|
|
// Find a version of the closure type. Substitute static for the
|
|
|
|
// region since it doesn't really matter.
|
2015-07-16 08:46:35 -05:00
|
|
|
let closure_ty = tcx.mk_closure_from_closure_substs(closure_def_id, Box::new(substs.clone()));
|
2015-06-24 20:09:46 -05:00
|
|
|
let ref_closure_ty = tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic), closure_ty);
|
2015-02-15 14:09:26 -06:00
|
|
|
|
|
|
|
// Make a version with the type of by-ref closure.
|
2015-07-16 08:46:35 -05:00
|
|
|
let ty::ClosureTy { unsafety, abi, mut sig } = infcx.closure_type(closure_def_id, &substs);
|
2015-02-15 14:09:26 -06:00
|
|
|
sig.0.inputs.insert(0, ref_closure_ty); // sig has no self type as of yet
|
|
|
|
let llref_bare_fn_ty = tcx.mk_bare_fn(ty::BareFnTy { unsafety: unsafety,
|
|
|
|
abi: abi,
|
|
|
|
sig: sig.clone() });
|
2015-06-24 20:09:46 -05:00
|
|
|
let llref_fn_ty = tcx.mk_fn(None, llref_bare_fn_ty);
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("trans_fn_once_adapter_shim: llref_fn_ty={:?}",
|
|
|
|
llref_fn_ty);
|
2015-02-15 14:09:26 -06:00
|
|
|
|
|
|
|
// Make a version of the closure type with the same arguments, but
|
|
|
|
// with argument #0 being by value.
|
|
|
|
assert_eq!(abi, RustCall);
|
|
|
|
sig.0.inputs[0] = closure_ty;
|
|
|
|
let llonce_bare_fn_ty = tcx.mk_bare_fn(ty::BareFnTy { unsafety: unsafety,
|
|
|
|
abi: abi,
|
|
|
|
sig: sig });
|
2015-06-24 20:09:46 -05:00
|
|
|
let llonce_fn_ty = tcx.mk_fn(None, llonce_bare_fn_ty);
|
2015-02-15 14:09:26 -06:00
|
|
|
|
|
|
|
// Create the by-value helper.
|
|
|
|
let function_name = link::mangle_internal_name_by_type_and_seq(ccx, llonce_fn_ty, "once_shim");
|
2015-07-20 15:27:38 -05:00
|
|
|
let lloncefn = declare::define_internal_rust_fn(ccx, &function_name,
|
|
|
|
llonce_fn_ty);
|
2015-06-25 15:42:17 -05:00
|
|
|
let sig = tcx.erase_late_bound_regions(&llonce_bare_fn_ty.sig);
|
2015-11-14 14:12:12 -06:00
|
|
|
let sig = infer::normalize_associated_type(ccx.tcx(), &sig);
|
|
|
|
|
2015-02-15 14:09:26 -06:00
|
|
|
let (block_arena, fcx): (TypedArena<_>, FunctionContext);
|
|
|
|
block_arena = TypedArena::new();
|
|
|
|
fcx = new_fn_ctxt(ccx,
|
|
|
|
lloncefn,
|
|
|
|
ast::DUMMY_NODE_ID,
|
|
|
|
false,
|
|
|
|
sig.output,
|
2015-07-16 08:46:35 -05:00
|
|
|
substs.func_substs,
|
2015-02-15 14:09:26 -06:00
|
|
|
None,
|
|
|
|
&block_arena);
|
|
|
|
let mut bcx = init_function(&fcx, false, sig.output);
|
|
|
|
|
2015-06-18 13:07:36 -05:00
|
|
|
let llargs = get_params(fcx.llfn);
|
|
|
|
|
2015-02-15 14:09:26 -06:00
|
|
|
// the first argument (`self`) will be the (by value) closure env.
|
|
|
|
let self_scope = fcx.push_custom_cleanup_scope();
|
|
|
|
let self_scope_id = CustomScope(self_scope);
|
|
|
|
let rvalue_mode = datum::appropriate_rvalue_mode(ccx, closure_ty);
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 16:57:40 -05:00
|
|
|
let self_idx = fcx.arg_offset();
|
|
|
|
let llself = llargs[self_idx];
|
2015-02-15 14:09:26 -06:00
|
|
|
let env_datum = Datum::new(llself, closure_ty, Rvalue::new(rvalue_mode));
|
|
|
|
let env_datum = unpack_datum!(bcx,
|
|
|
|
env_datum.to_lvalue_datum_in_scope(bcx, "self",
|
|
|
|
self_scope_id));
|
|
|
|
|
|
|
|
debug!("trans_fn_once_adapter_shim: env_datum={}",
|
|
|
|
bcx.val_to_string(env_datum.val));
|
|
|
|
|
|
|
|
let dest =
|
|
|
|
fcx.llretslotptr.get().map(
|
|
|
|
|_| expr::SaveIn(fcx.get_ret_slot(bcx, sig.output, "ret_slot")));
|
|
|
|
|
|
|
|
let callee_data = TraitItem(MethodData { llfn: llreffn,
|
|
|
|
llself: env_datum.val });
|
|
|
|
|
2015-07-02 21:22:54 -05:00
|
|
|
bcx = callee::trans_call_inner(bcx, DebugLoc::None, |bcx, _| {
|
|
|
|
Callee {
|
|
|
|
bcx: bcx,
|
|
|
|
data: callee_data,
|
|
|
|
ty: llref_fn_ty
|
|
|
|
}
|
|
|
|
}, ArgVals(&llargs[(self_idx + 1)..]), dest).bcx;
|
2015-02-15 14:09:26 -06:00
|
|
|
|
|
|
|
fcx.pop_custom_cleanup_scope(self_scope);
|
|
|
|
|
|
|
|
finish_fn(&fcx, bcx, sig.output, DebugLoc::None);
|
|
|
|
|
|
|
|
lloncefn
|
|
|
|
}
|