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_param, get_params};
14 use middle::def_id::DefId;
16 use middle::traits::ProjectionMode;
17 use trans::abi::{Abi, FnType};
19 use trans::attributes;
22 use trans::callee::{self, ArgVals, Callee};
23 use trans::cleanup::{CleanupMethods, CustomScope, ScopeId};
25 use trans::datum::{ByRef, Datum, lvalue_scratch_datum};
26 use trans::datum::{rvalue_scratch_datum, Rvalue};
27 use trans::debuginfo::{self, DebugLoc};
30 use trans::monomorphize::{Instance};
31 use trans::value::Value;
33 use middle::ty::{self, Ty, TyCtxt};
34 use session::config::FullDebugInfo;
42 fn load_closure_environment<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
43 closure_def_id: DefId,
44 arg_scope_id: ScopeId,
46 let _icx = push_ctxt("closure::load_closure_environment");
47 let kind = kind_for_closure(bcx.ccx(), closure_def_id);
49 let env_arg = &bcx.fcx.fn_ty.args[0];
50 let mut env_idx = bcx.fcx.fn_ty.ret.is_indirect() as usize;
52 // Special case for small by-value selfs.
53 let llenv = if kind == ty::ClosureKind::FnOnce && !env_arg.is_indirect() {
54 let closure_ty = node_id_type(bcx, id);
55 let llenv = rvalue_scratch_datum(bcx, closure_ty, "closure_env").val;
56 env_arg.store_fn_arg(&bcx.build(), &mut env_idx, llenv);
59 get_param(bcx.fcx.llfn, env_idx as c_uint)
62 // Store the pointer to closure data in an alloca for debug info because that's what the
63 // llvm.dbg.declare intrinsic expects
64 let env_pointer_alloca = if bcx.sess().opts.debuginfo == FullDebugInfo {
65 let alloc = alloca(bcx, val_ty(llenv), "__debuginfo_env_ptr");
66 Store(bcx, llenv, alloc);
72 bcx.tcx().with_freevars(id, |fv| {
73 for (i, freevar) in fv.iter().enumerate() {
74 let upvar_id = ty::UpvarId { var_id: freevar.def.var_id(),
75 closure_expr_id: id };
76 let upvar_capture = bcx.tcx().upvar_capture(upvar_id).unwrap();
77 let mut upvar_ptr = StructGEP(bcx, llenv, i);
78 let captured_by_ref = match upvar_capture {
79 ty::UpvarCapture::ByValue => false,
80 ty::UpvarCapture::ByRef(..) => {
81 upvar_ptr = Load(bcx, upvar_ptr);
85 let node_id = freevar.def.var_id();
86 bcx.fcx.llupvars.borrow_mut().insert(node_id, upvar_ptr);
88 if kind == ty::ClosureKind::FnOnce && !captured_by_ref {
89 let hint = bcx.fcx.lldropflag_hints.borrow().hint_datum(upvar_id.var_id);
90 bcx.fcx.schedule_drop_mem(arg_scope_id,
92 node_id_type(bcx, node_id),
96 if let Some(env_pointer_alloca) = env_pointer_alloca {
97 debuginfo::create_captured_var_metadata(
109 pub enum ClosureEnv {
111 Closure(DefId, ast::NodeId),
115 pub fn load<'blk,'tcx>(self, bcx: Block<'blk, 'tcx>, arg_scope: ScopeId) {
116 if let ClosureEnv::Closure(def_id, id) = self {
117 load_closure_environment(bcx, def_id, arg_scope, id);
122 fn get_self_type<'tcx>(tcx: &TyCtxt<'tcx>,
126 match tcx.closure_kind(closure_id) {
127 ty::ClosureKind::Fn => {
128 tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic), fn_ty)
130 ty::ClosureKind::FnMut => {
131 tcx.mk_mut_ref(tcx.mk_region(ty::ReStatic), fn_ty)
133 ty::ClosureKind::FnOnce => fn_ty,
137 /// Returns the LLVM function declaration for a closure, creating it if
138 /// necessary. If the ID does not correspond to a closure ID, returns None.
139 fn get_or_create_closure_declaration<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
141 substs: &ty::ClosureSubsts<'tcx>)
143 // Normalize type so differences in regions and typedefs don't cause
144 // duplicate declarations
146 let substs = tcx.erase_regions(substs);
147 let instance = Instance {
149 params: &substs.func_substs.types
152 if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
153 debug!("get_or_create_closure_declaration(): found closure {:?}: {:?}",
154 instance, Value(llfn));
158 let path = tcx.def_path(closure_id);
159 let symbol = mangle_internal_name_by_path_and_seq(path, "closure");
161 // Compute the rust-call form of the closure call method.
162 let infcx = infer::normalizing_infer_ctxt(tcx, &tcx.tables, ProjectionMode::Any);
163 let sig = &infcx.closure_type(closure_id, &substs).sig;
164 let sig = tcx.erase_late_bound_regions(sig);
165 let sig = infer::normalize_associated_type(tcx, &sig);
166 let closure_type = tcx.mk_closure_from_closure_substs(closure_id, Box::new(substs));
167 let function_type = tcx.mk_fn_ptr(ty::BareFnTy {
168 unsafety: hir::Unsafety::Normal,
170 sig: ty::Binder(ty::FnSig {
171 inputs: Some(get_self_type(tcx, closure_id, closure_type))
172 .into_iter().chain(sig.inputs).collect(),
177 let llfn = declare::define_internal_fn(ccx, &symbol, function_type);
179 // set an inline hint for all closures
180 attributes::inline(llfn, attributes::InlineAttr::Hint);
182 debug!("get_or_create_declaration_if_closure(): inserting new \
184 instance, Value(llfn));
185 ccx.instances().borrow_mut().insert(instance, llfn);
190 pub enum Dest<'a, 'tcx: 'a> {
191 SaveIn(Block<'a, 'tcx>, ValueRef),
192 Ignore(&'a CrateContext<'a, 'tcx>)
195 pub fn trans_closure_expr<'a, 'tcx>(dest: Dest<'a, 'tcx>,
199 closure_def_id: DefId, // (*)
200 closure_substs: &ty::ClosureSubsts<'tcx>)
201 -> Option<Block<'a, 'tcx>>
203 // (*) Note that in the case of inlined functions, the `closure_def_id` will be the
204 // defid of the closure in its original crate, whereas `id` will be the id of the local
207 let param_substs = closure_substs.func_substs;
209 let ccx = match dest {
210 Dest::SaveIn(bcx, _) => bcx.ccx(),
211 Dest::Ignore(ccx) => ccx
214 let _icx = push_ctxt("closure::trans_closure_expr");
216 debug!("trans_closure_expr(id={:?}, closure_def_id={:?}, closure_substs={:?})",
217 id, closure_def_id, closure_substs);
219 let llfn = get_or_create_closure_declaration(ccx, closure_def_id, closure_substs);
221 // Get the type of this closure. Use the current `param_substs` as
222 // the closure substitutions. This makes sense because the closure
223 // takes the same set of type arguments as the enclosing fn, and
224 // this function (`trans_closure`) is invoked at the point
225 // of the closure expression.
227 let infcx = infer::normalizing_infer_ctxt(ccx.tcx(), &ccx.tcx().tables, ProjectionMode::Any);
228 let function_type = infcx.closure_type(closure_def_id, closure_substs);
230 let sig = tcx.erase_late_bound_regions(&function_type.sig);
231 let sig = infer::normalize_associated_type(ccx.tcx(), &sig);
233 let closure_type = tcx.mk_closure_from_closure_substs(closure_def_id,
234 Box::new(closure_substs.clone()));
235 let sig = ty::FnSig {
236 inputs: Some(get_self_type(tcx, closure_def_id, closure_type))
237 .into_iter().chain(sig.inputs).collect(),
241 let fn_ty = FnType::new(ccx, Abi::RustCall, &sig, &[]);
252 ClosureEnv::Closure(closure_def_id, id));
254 // Don't hoist this to the top of the function. It's perfectly legitimate
255 // to have a zero-size closure (in which case dest will be `Ignore`) and
256 // we must still generate the closure body.
257 let (mut bcx, dest_addr) = match dest {
258 Dest::SaveIn(bcx, p) => (bcx, p),
260 debug!("trans_closure_expr() ignoring result");
265 let repr = adt::represent_type(ccx, node_id_type(bcx, id));
267 // Create the closure.
268 tcx.with_freevars(id, |fv| {
269 for (i, freevar) in fv.iter().enumerate() {
270 let datum = expr::trans_var(bcx, freevar.def);
271 let upvar_slot_dest = adt::trans_field_ptr(
272 bcx, &repr, adt::MaybeSizedValue::sized(dest_addr), Disr(0), i);
273 let upvar_id = ty::UpvarId { var_id: freevar.def.var_id(),
274 closure_expr_id: id };
275 match tcx.upvar_capture(upvar_id).unwrap() {
276 ty::UpvarCapture::ByValue => {
277 bcx = datum.store_to(bcx, upvar_slot_dest);
279 ty::UpvarCapture::ByRef(..) => {
280 Store(bcx, datum.to_llref(), upvar_slot_dest);
285 adt::trans_set_discr(bcx, &repr, dest_addr, Disr(0));
290 pub fn trans_closure_method<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>,
291 closure_def_id: DefId,
292 substs: ty::ClosureSubsts<'tcx>,
293 trait_closure_kind: ty::ClosureKind)
296 // If this is a closure, redirect to it.
297 let llfn = get_or_create_closure_declaration(ccx, closure_def_id, &substs);
299 // If the closure is a Fn closure, but a FnOnce is needed (etc),
300 // then adapt the self type
301 let llfn_closure_kind = ccx.tcx().closure_kind(closure_def_id);
303 let _icx = push_ctxt("trans_closure_adapter_shim");
306 debug!("trans_closure_adapter_shim(llfn_closure_kind={:?}, \
307 trait_closure_kind={:?}, llfn={:?})",
308 llfn_closure_kind, trait_closure_kind, Value(llfn));
310 match (llfn_closure_kind, trait_closure_kind) {
311 (ty::ClosureKind::Fn, ty::ClosureKind::Fn) |
312 (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) |
313 (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
314 // No adapter needed.
317 (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
318 // The closure fn `llfn` is a `fn(&self, ...)`. We want a
319 // `fn(&mut self, ...)`. In fact, at trans time, these are
320 // basically the same thing, so we can just return llfn.
323 (ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) |
324 (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
325 // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
326 // self, ...)`. We want a `fn(self, ...)`. We can produce
327 // this by doing something like:
329 // fn call_once(self, ...) { call_mut(&self, ...) }
330 // fn call_once(mut self, ...) { call_mut(&mut self, ...) }
332 // These are both the same at trans time.
333 trans_fn_once_adapter_shim(ccx, closure_def_id, substs, llfn)
336 tcx.sess.bug(&format!("trans_closure_adapter_shim: cannot convert {:?} to {:?}",
338 trait_closure_kind));
343 fn trans_fn_once_adapter_shim<'a, 'tcx>(
344 ccx: &'a CrateContext<'a, 'tcx>,
345 closure_def_id: DefId,
346 substs: ty::ClosureSubsts<'tcx>,
350 debug!("trans_fn_once_adapter_shim(closure_def_id={:?}, substs={:?}, llreffn={:?})",
351 closure_def_id, substs, Value(llreffn));
354 let infcx = infer::normalizing_infer_ctxt(ccx.tcx(), &ccx.tcx().tables, ProjectionMode::Any);
356 // Find a version of the closure type. Substitute static for the
357 // region since it doesn't really matter.
358 let closure_ty = tcx.mk_closure_from_closure_substs(closure_def_id, Box::new(substs.clone()));
359 let ref_closure_ty = tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic), closure_ty);
361 // Make a version with the type of by-ref closure.
362 let ty::ClosureTy { unsafety, abi, mut sig } = infcx.closure_type(closure_def_id, &substs);
363 sig.0.inputs.insert(0, ref_closure_ty); // sig has no self type as of yet
364 let llref_fn_ty = tcx.mk_fn_ptr(ty::BareFnTy {
369 debug!("trans_fn_once_adapter_shim: llref_fn_ty={:?}",
373 // Make a version of the closure type with the same arguments, but
374 // with argument #0 being by value.
375 assert_eq!(abi, Abi::RustCall);
376 sig.0.inputs[0] = closure_ty;
378 let sig = tcx.erase_late_bound_regions(&sig);
379 let sig = infer::normalize_associated_type(ccx.tcx(), &sig);
380 let fn_ty = FnType::new(ccx, abi, &sig, &[]);
382 let llonce_fn_ty = tcx.mk_fn_ptr(ty::BareFnTy {
388 // Create the by-value helper.
389 let function_name = link::mangle_internal_name_by_type_and_seq(ccx, llonce_fn_ty, "once_shim");
390 let lloncefn = declare::define_internal_fn(ccx, &function_name, llonce_fn_ty);
392 let (block_arena, fcx): (TypedArena<_>, FunctionContext);
393 block_arena = TypedArena::new();
394 fcx = FunctionContext::new(ccx, lloncefn, fn_ty, None, substs.func_substs, &block_arena);
395 let mut bcx = fcx.init(false, None);
398 // the first argument (`self`) will be the (by value) closure env.
399 let self_scope = fcx.push_custom_cleanup_scope();
400 let self_scope_id = CustomScope(self_scope);
402 let mut llargs = get_params(fcx.llfn);
403 let mut self_idx = fcx.fn_ty.ret.is_indirect() as usize;
404 let env_arg = &fcx.fn_ty.args[0];
405 let llenv = if env_arg.is_indirect() {
406 Datum::new(llargs[self_idx], closure_ty, Rvalue::new(ByRef))
407 .add_clean(&fcx, self_scope_id)
409 unpack_datum!(bcx, lvalue_scratch_datum(bcx, closure_ty, "self",
411 self_scope_id, |bcx, llval| {
412 let mut llarg_idx = self_idx;
413 env_arg.store_fn_arg(&bcx.build(), &mut llarg_idx, llval);
414 bcx.fcx.schedule_lifetime_end(self_scope_id, llval);
419 debug!("trans_fn_once_adapter_shim: env={:?}", Value(llenv));
420 // Adjust llargs such that llargs[self_idx..] has the call arguments.
421 // For zero-sized closures that means sneaking in a new argument.
422 if env_arg.is_ignore() {
425 llargs[self_idx] = llenv;
427 llargs.insert(0, llenv);
430 llargs[self_idx] = llenv;
434 fcx.llretslotptr.get().map(
435 |_| expr::SaveIn(fcx.get_ret_slot(bcx, "ret_slot")));
437 let callee = Callee {
438 data: callee::Fn(llreffn),
441 bcx = callee.call(bcx, DebugLoc::None, ArgVals(&llargs[self_idx..]), dest).bcx;
443 fcx.pop_and_trans_custom_cleanup_scope(bcx, self_scope);
445 fcx.finish(bcx, DebugLoc::None);