1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use arena
::TypedArena
;
12 use back
::link
::{self, mangle_internal_name_by_path_and_seq}
;
13 use llvm
::{ValueRef, get_params}
;
16 use trans
::attributes
;
19 use trans
::callee
::{self, ArgVals, Callee, TraitItem, MethodData}
;
20 use trans
::cleanup
::{CleanupMethods, CustomScope, ScopeId}
;
22 use trans
::datum
::{self, Datum, rvalue_scratch_datum, Rvalue, ByValue}
;
23 use trans
::debuginfo
::{self, DebugLoc}
;
26 use trans
::monomorphize
::{MonoId}
;
27 use trans
::type_of
::*;
29 use session
::config
::FullDebugInfo
;
31 use syntax
::abi
::RustCall
;
36 fn load_closure_environment
<'blk
, 'tcx
>(bcx
: Block
<'blk
, 'tcx
>,
37 arg_scope_id
: ScopeId
,
38 freevars
: &[ty
::Freevar
])
41 let _icx
= push_ctxt("closure::load_closure_environment");
43 // Special case for small by-value selfs.
44 let closure_id
= ast_util
::local_def(bcx
.fcx
.id
);
45 let self_type
= self_type_for_closure(bcx
.ccx(), closure_id
,
46 node_id_type(bcx
, closure_id
.node
));
47 let kind
= kind_for_closure(bcx
.ccx(), closure_id
);
48 let llenv
= if kind
== ty
::FnOnceClosureKind
&&
49 !arg_is_indirect(bcx
.ccx(), self_type
) {
50 let datum
= rvalue_scratch_datum(bcx
,
53 store_ty(bcx
, bcx
.fcx
.llenv
.unwrap(), datum
.val
, self_type
);
56 bcx
.fcx
.llenv
.unwrap()
59 // Store the pointer to closure data in an alloca for debug info because that's what the
60 // llvm.dbg.declare intrinsic expects
61 let env_pointer_alloca
= if bcx
.sess().opts
.debuginfo
== FullDebugInfo
{
62 let alloc
= alloca(bcx
, val_ty(llenv
), "__debuginfo_env_ptr");
63 Store(bcx
, llenv
, alloc
);
69 for (i
, freevar
) in freevars
.iter().enumerate() {
70 let upvar_id
= ty
::UpvarId
{ var_id
: freevar
.def
.local_node_id(),
71 closure_expr_id
: closure_id
.node
};
72 let upvar_capture
= bcx
.tcx().upvar_capture(upvar_id
).unwrap();
73 let mut upvar_ptr
= GEPi(bcx
, llenv
, &[0, i
]);
74 let captured_by_ref
= match upvar_capture
{
75 ty
::UpvarCapture
::ByValue
=> false,
76 ty
::UpvarCapture
::ByRef(..) => {
77 upvar_ptr
= Load(bcx
, upvar_ptr
);
81 let def_id
= freevar
.def
.def_id();
82 bcx
.fcx
.llupvars
.borrow_mut().insert(def_id
.node
, upvar_ptr
);
84 if kind
== ty
::FnOnceClosureKind
&& !captured_by_ref
{
85 let hint
= bcx
.fcx
.lldropflag_hints
.borrow().hint_datum(upvar_id
.var_id
);
86 bcx
.fcx
.schedule_drop_mem(arg_scope_id
,
88 node_id_type(bcx
, def_id
.node
),
92 if let Some(env_pointer_alloca
) = env_pointer_alloca
{
93 debuginfo
::create_captured_var_metadata(
106 pub enum ClosureEnv
<'a
> {
108 Closure(&'a
[ty
::Freevar
]),
111 impl<'a
> ClosureEnv
<'a
> {
112 pub fn load
<'blk
,'tcx
>(self, bcx
: Block
<'blk
, 'tcx
>, arg_scope
: ScopeId
)
116 ClosureEnv
::NotClosure
=> bcx
,
117 ClosureEnv
::Closure(freevars
) => {
118 if freevars
.is_empty() {
121 load_closure_environment(bcx
, arg_scope
, freevars
)
128 /// Returns the LLVM function declaration for a closure, creating it if
129 /// necessary. If the ID does not correspond to a closure ID, returns None.
130 pub fn get_or_create_closure_declaration
<'a
, 'tcx
>(ccx
: &CrateContext
<'a
, 'tcx
>,
131 closure_id
: ast
::DefId
,
132 substs
: &ty
::ClosureSubsts
<'tcx
>)
134 // Normalize type so differences in regions and typedefs don't cause
135 // duplicate declarations
136 let substs
= erase_regions(ccx
.tcx(), substs
);
137 let mono_id
= MonoId
{
139 params
: &substs
.func_substs
.types
142 if let Some(&llfn
) = ccx
.closure_vals().borrow().get(&mono_id
) {
143 debug
!("get_or_create_closure_declaration(): found closure {:?}: {:?}",
144 mono_id
, ccx
.tn().val_to_string(llfn
));
148 let symbol
= ccx
.tcx().map
.with_path(closure_id
.node
, |path
| {
149 mangle_internal_name_by_path_and_seq(path
, "closure")
152 let function_type
= ccx
.tcx().mk_closure_from_closure_substs(closure_id
, Box
::new(substs
));
153 let llfn
= declare
::define_internal_rust_fn(ccx
, &symbol
[..], function_type
);
155 // set an inline hint for all closures
156 attributes
::inline(llfn
, attributes
::InlineAttr
::Hint
);
158 debug
!("get_or_create_declaration_if_closure(): inserting new \
159 closure {:?} (type {}): {:?}",
161 ccx
.tn().type_to_string(val_ty(llfn
)),
162 ccx
.tn().val_to_string(llfn
));
163 ccx
.closure_vals().borrow_mut().insert(mono_id
, llfn
);
168 pub enum Dest
<'a
, 'tcx
: 'a
> {
169 SaveIn(Block
<'a
, 'tcx
>, ValueRef
),
170 Ignore(&'a CrateContext
<'a
, 'tcx
>)
173 pub fn trans_closure_expr
<'a
, 'tcx
>(dest
: Dest
<'a
, 'tcx
>,
177 closure_substs
: &'tcx ty
::ClosureSubsts
<'tcx
>)
178 -> Option
<Block
<'a
, 'tcx
>>
180 let param_substs
= closure_substs
.func_substs
;
182 let ccx
= match dest
{
183 Dest
::SaveIn(bcx
, _
) => bcx
.ccx(),
184 Dest
::Ignore(ccx
) => ccx
187 let _icx
= push_ctxt("closure::trans_closure_expr");
189 debug
!("trans_closure_expr()");
191 let closure_id
= ast_util
::local_def(id
);
192 let llfn
= get_or_create_closure_declaration(ccx
, closure_id
, closure_substs
);
194 // Get the type of this closure. Use the current `param_substs` as
195 // the closure substitutions. This makes sense because the closure
196 // takes the same set of type arguments as the enclosing fn, and
197 // this function (`trans_closure`) is invoked at the point
198 // of the closure expression.
200 let infcx
= infer
::normalizing_infer_ctxt(ccx
.tcx(), &ccx
.tcx().tables
);
201 let function_type
= infcx
.closure_type(closure_id
, closure_substs
);
203 let freevars
: Vec
<ty
::Freevar
> =
204 tcx
.with_freevars(id
, |fv
| fv
.iter().cloned().collect());
206 let sig
= tcx
.erase_late_bound_regions(&function_type
.sig
);
217 ClosureEnv
::Closure(&freevars
));
219 // Don't hoist this to the top of the function. It's perfectly legitimate
220 // to have a zero-size closure (in which case dest will be `Ignore`) and
221 // we must still generate the closure body.
222 let (mut bcx
, dest_addr
) = match dest
{
223 Dest
::SaveIn(bcx
, p
) => (bcx
, p
),
225 debug
!("trans_closure_expr() ignoring result");
230 let repr
= adt
::represent_type(ccx
, node_id_type(bcx
, id
));
232 // Create the closure.
233 for (i
, freevar
) in freevars
.iter().enumerate() {
234 let datum
= expr
::trans_local_var(bcx
, freevar
.def
);
235 let upvar_slot_dest
= adt
::trans_field_ptr(bcx
, &*repr
, dest_addr
, 0, i
);
236 let upvar_id
= ty
::UpvarId
{ var_id
: freevar
.def
.local_node_id(),
237 closure_expr_id
: id
};
238 match tcx
.upvar_capture(upvar_id
).unwrap() {
239 ty
::UpvarCapture
::ByValue
=> {
240 bcx
= datum
.store_to(bcx
, upvar_slot_dest
);
242 ty
::UpvarCapture
::ByRef(..) => {
243 Store(bcx
, datum
.to_llref(), upvar_slot_dest
);
247 adt
::trans_set_discr(bcx
, &*repr
, dest_addr
, 0);
252 pub fn trans_closure_method
<'a
, 'tcx
>(ccx
: &'a CrateContext
<'a
, 'tcx
>,
253 closure_def_id
: ast
::DefId
,
254 substs
: ty
::ClosureSubsts
<'tcx
>,
255 trait_closure_kind
: ty
::ClosureKind
)
258 // If this is a closure, redirect to it.
259 let llfn
= get_or_create_closure_declaration(ccx
, closure_def_id
, &substs
);
261 // If the closure is a Fn closure, but a FnOnce is needed (etc),
262 // then adapt the self type
263 let closure_kind
= ccx
.tcx().closure_kind(closure_def_id
);
264 trans_closure_adapter_shim(ccx
,
272 fn trans_closure_adapter_shim
<'a
, 'tcx
>(
273 ccx
: &'a CrateContext
<'a
, 'tcx
>,
274 closure_def_id
: ast
::DefId
,
275 substs
: ty
::ClosureSubsts
<'tcx
>,
276 llfn_closure_kind
: ty
::ClosureKind
,
277 trait_closure_kind
: ty
::ClosureKind
,
281 let _icx
= push_ctxt("trans_closure_adapter_shim");
284 debug
!("trans_closure_adapter_shim(llfn_closure_kind={:?}, \
285 trait_closure_kind={:?}, \
289 ccx
.tn().val_to_string(llfn
));
291 match (llfn_closure_kind
, trait_closure_kind
) {
292 (ty
::FnClosureKind
, ty
::FnClosureKind
) |
293 (ty
::FnMutClosureKind
, ty
::FnMutClosureKind
) |
294 (ty
::FnOnceClosureKind
, ty
::FnOnceClosureKind
) => {
295 // No adapter needed.
298 (ty
::FnClosureKind
, ty
::FnMutClosureKind
) => {
299 // The closure fn `llfn` is a `fn(&self, ...)`. We want a
300 // `fn(&mut self, ...)`. In fact, at trans time, these are
301 // basically the same thing, so we can just return llfn.
304 (ty
::FnClosureKind
, ty
::FnOnceClosureKind
) |
305 (ty
::FnMutClosureKind
, ty
::FnOnceClosureKind
) => {
306 // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
307 // self, ...)`. We want a `fn(self, ...)`. We can produce
308 // this by doing something like:
310 // fn call_once(self, ...) { call_mut(&self, ...) }
311 // fn call_once(mut self, ...) { call_mut(&mut self, ...) }
313 // These are both the same at trans time.
314 trans_fn_once_adapter_shim(ccx
, closure_def_id
, substs
, llfn
)
317 tcx
.sess
.bug(&format
!("trans_closure_adapter_shim: cannot convert {:?} to {:?}",
319 trait_closure_kind
));
324 fn trans_fn_once_adapter_shim
<'a
, 'tcx
>(
325 ccx
: &'a CrateContext
<'a
, 'tcx
>,
326 closure_def_id
: ast
::DefId
,
327 substs
: ty
::ClosureSubsts
<'tcx
>,
331 debug
!("trans_fn_once_adapter_shim(closure_def_id={:?}, substs={:?}, llreffn={})",
334 ccx
.tn().val_to_string(llreffn
));
337 let infcx
= infer
::normalizing_infer_ctxt(ccx
.tcx(), &ccx
.tcx().tables
);
339 // Find a version of the closure type. Substitute static for the
340 // region since it doesn't really matter.
341 let closure_ty
= tcx
.mk_closure_from_closure_substs(closure_def_id
, Box
::new(substs
.clone()));
342 let ref_closure_ty
= tcx
.mk_imm_ref(tcx
.mk_region(ty
::ReStatic
), closure_ty
);
344 // Make a version with the type of by-ref closure.
345 let ty
::ClosureTy { unsafety, abi, mut sig }
= infcx
.closure_type(closure_def_id
, &substs
);
346 sig
.0.inputs
.insert(0, ref_closure_ty
); // sig has no self type as of yet
347 let llref_bare_fn_ty
= tcx
.mk_bare_fn(ty
::BareFnTy
{ unsafety
: unsafety
,
350 let llref_fn_ty
= tcx
.mk_fn(None
, llref_bare_fn_ty
);
351 debug
!("trans_fn_once_adapter_shim: llref_fn_ty={:?}",
354 // Make a version of the closure type with the same arguments, but
355 // with argument #0 being by value.
356 assert_eq
!(abi
, RustCall
);
357 sig
.0.inputs
[0] = closure_ty
;
358 let llonce_bare_fn_ty
= tcx
.mk_bare_fn(ty
::BareFnTy
{ unsafety
: unsafety
,
361 let llonce_fn_ty
= tcx
.mk_fn(None
, llonce_bare_fn_ty
);
363 // Create the by-value helper.
364 let function_name
= link
::mangle_internal_name_by_type_and_seq(ccx
, llonce_fn_ty
, "once_shim");
365 let lloncefn
= declare
::define_internal_rust_fn(ccx
, &function_name
,
367 let sig
= tcx
.erase_late_bound_regions(&llonce_bare_fn_ty
.sig
);
368 let (block_arena
, fcx
): (TypedArena
<_
>, FunctionContext
);
369 block_arena
= TypedArena
::new();
370 fcx
= new_fn_ctxt(ccx
,
378 let mut bcx
= init_function(&fcx
, false, sig
.output
);
380 let llargs
= get_params(fcx
.llfn
);
382 // the first argument (`self`) will be the (by value) closure env.
383 let self_scope
= fcx
.push_custom_cleanup_scope();
384 let self_scope_id
= CustomScope(self_scope
);
385 let rvalue_mode
= datum
::appropriate_rvalue_mode(ccx
, closure_ty
);
386 let self_idx
= fcx
.arg_offset();
387 let llself
= llargs
[self_idx
];
388 let env_datum
= Datum
::new(llself
, closure_ty
, Rvalue
::new(rvalue_mode
));
389 let env_datum
= unpack_datum
!(bcx
,
390 env_datum
.to_lvalue_datum_in_scope(bcx
, "self",
393 debug
!("trans_fn_once_adapter_shim: env_datum={}",
394 bcx
.val_to_string(env_datum
.val
));
397 fcx
.llretslotptr
.get().map(
398 |_
| expr
::SaveIn(fcx
.get_ret_slot(bcx
, sig
.output
, "ret_slot")));
400 let callee_data
= TraitItem(MethodData
{ llfn
: llreffn
,
401 llself
: env_datum
.val
});
403 bcx
= callee
::trans_call_inner(bcx
, DebugLoc
::None
, |bcx
, _
| {
409 }, ArgVals(&llargs
[(self_idx
+ 1)..]), dest
).bcx
;
411 fcx
.pop_custom_cleanup_scope(self_scope
);
413 finish_fn(&fcx
, bcx
, sig
.output
, DebugLoc
::None
);