]> git.proxmox.com Git - rustc.git/blame - src/librustc_trans/trans/closure.rs
Imported Upstream version 1.6.0+dfsg1
[rustc.git] / src / librustc_trans / trans / closure.rs
CommitLineData
1a4d82fc
JJ
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.
4//
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.
10
c34b1796
AL
11use arena::TypedArena;
12use back::link::{self, mangle_internal_name_by_path_and_seq};
62682a34 13use llvm::{ValueRef, get_params};
e9174d1e 14use middle::def_id::DefId;
c1a9b12d 15use middle::infer;
1a4d82fc 16use trans::adt;
9346a6ac 17use trans::attributes;
1a4d82fc
JJ
18use trans::base::*;
19use trans::build::*;
c34b1796
AL
20use trans::callee::{self, ArgVals, Callee, TraitItem, MethodData};
21use trans::cleanup::{CleanupMethods, CustomScope, ScopeId};
1a4d82fc 22use trans::common::*;
c34b1796
AL
23use trans::datum::{self, Datum, rvalue_scratch_datum, Rvalue, ByValue};
24use trans::debuginfo::{self, DebugLoc};
9346a6ac 25use trans::declare;
1a4d82fc 26use trans::expr;
c1a9b12d 27use trans::monomorphize::{MonoId};
1a4d82fc 28use trans::type_of::*;
c1a9b12d 29use middle::ty;
1a4d82fc 30use session::config::FullDebugInfo;
1a4d82fc 31
c34b1796 32use syntax::abi::RustCall;
1a4d82fc 33use syntax::ast;
e9174d1e
SL
34
35use rustc_front::hir;
1a4d82fc 36
1a4d82fc 37
85aaf69f 38fn load_closure_environment<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
b039eaaf 39 closure_def_id: DefId,
85aaf69f
SL
40 arg_scope_id: ScopeId,
41 freevars: &[ty::Freevar])
42 -> Block<'blk, 'tcx>
43{
44 let _icx = push_ctxt("closure::load_closure_environment");
1a4d82fc
JJ
45
46 // Special case for small by-value selfs.
b039eaaf
SL
47 let closure_ty = node_id_type(bcx, bcx.fcx.id);
48 let self_type = self_type_for_closure(bcx.ccx(), closure_def_id, closure_ty);
49 let kind = kind_for_closure(bcx.ccx(), closure_def_id);
85aaf69f 50 let llenv = if kind == ty::FnOnceClosureKind &&
1a4d82fc
JJ
51 !arg_is_indirect(bcx.ccx(), self_type) {
52 let datum = rvalue_scratch_datum(bcx,
53 self_type,
85aaf69f 54 "closure_env");
1a4d82fc
JJ
55 store_ty(bcx, bcx.fcx.llenv.unwrap(), datum.val, self_type);
56 datum.val
57 } else {
58 bcx.fcx.llenv.unwrap()
59 };
60
61 // Store the pointer to closure data in an alloca for debug info because that's what the
62 // llvm.dbg.declare intrinsic expects
63 let env_pointer_alloca = if bcx.sess().opts.debuginfo == FullDebugInfo {
64 let alloc = alloca(bcx, val_ty(llenv), "__debuginfo_env_ptr");
65 Store(bcx, llenv, alloc);
66 Some(alloc)
67 } else {
68 None
69 };
70
71 for (i, freevar) in freevars.iter().enumerate() {
b039eaaf
SL
72 let upvar_id = ty::UpvarId { var_id: freevar.def.var_id(),
73 closure_expr_id: bcx.fcx.id };
85aaf69f 74 let upvar_capture = bcx.tcx().upvar_capture(upvar_id).unwrap();
e9174d1e 75 let mut upvar_ptr = StructGEP(bcx, llenv, i);
85aaf69f
SL
76 let captured_by_ref = match upvar_capture {
77 ty::UpvarCapture::ByValue => false,
78 ty::UpvarCapture::ByRef(..) => {
1a4d82fc
JJ
79 upvar_ptr = Load(bcx, upvar_ptr);
80 true
81 }
1a4d82fc 82 };
b039eaaf
SL
83 let node_id = freevar.def.var_id();
84 bcx.fcx.llupvars.borrow_mut().insert(node_id, upvar_ptr);
1a4d82fc 85
85aaf69f 86 if kind == ty::FnOnceClosureKind && !captured_by_ref {
c1a9b12d 87 let hint = bcx.fcx.lldropflag_hints.borrow().hint_datum(upvar_id.var_id);
1a4d82fc
JJ
88 bcx.fcx.schedule_drop_mem(arg_scope_id,
89 upvar_ptr,
b039eaaf 90 node_id_type(bcx, node_id),
c1a9b12d 91 hint)
1a4d82fc
JJ
92 }
93
94 if let Some(env_pointer_alloca) = env_pointer_alloca {
95 debuginfo::create_captured_var_metadata(
96 bcx,
b039eaaf 97 node_id,
1a4d82fc
JJ
98 env_pointer_alloca,
99 i,
100 captured_by_ref,
101 freevar.span);
102 }
103 }
104
105 bcx
106}
107
85aaf69f 108pub enum ClosureEnv<'a> {
1a4d82fc 109 NotClosure,
b039eaaf 110 Closure(DefId, &'a [ty::Freevar]),
1a4d82fc
JJ
111}
112
85aaf69f
SL
113impl<'a> ClosureEnv<'a> {
114 pub fn load<'blk,'tcx>(self, bcx: Block<'blk, 'tcx>, arg_scope: ScopeId)
115 -> Block<'blk, 'tcx>
116 {
117 match self {
118 ClosureEnv::NotClosure => bcx,
b039eaaf 119 ClosureEnv::Closure(def_id, freevars) => {
85aaf69f
SL
120 if freevars.is_empty() {
121 bcx
122 } else {
b039eaaf 123 load_closure_environment(bcx, def_id, arg_scope, freevars)
85aaf69f 124 }
1a4d82fc
JJ
125 }
126 }
127 }
128}
129
85aaf69f
SL
130/// Returns the LLVM function declaration for a closure, creating it if
131/// necessary. If the ID does not correspond to a closure ID, returns None.
c1a9b12d 132pub fn get_or_create_closure_declaration<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
e9174d1e 133 closure_id: DefId,
c1a9b12d
SL
134 substs: &ty::ClosureSubsts<'tcx>)
135 -> ValueRef {
1a4d82fc
JJ
136 // Normalize type so differences in regions and typedefs don't cause
137 // duplicate declarations
e9174d1e 138 let substs = ccx.tcx().erase_regions(substs);
1a4d82fc
JJ
139 let mono_id = MonoId {
140 def: closure_id,
c1a9b12d 141 params: &substs.func_substs.types
1a4d82fc
JJ
142 };
143
c1a9b12d
SL
144 if let Some(&llfn) = ccx.closure_vals().borrow().get(&mono_id) {
145 debug!("get_or_create_closure_declaration(): found closure {:?}: {:?}",
146 mono_id, ccx.tn().val_to_string(llfn));
147 return llfn;
1a4d82fc
JJ
148 }
149
b039eaaf
SL
150 let path = ccx.tcx().def_path(closure_id);
151 let symbol = mangle_internal_name_by_path_and_seq(path, "closure");
1a4d82fc 152
c1a9b12d
SL
153 let function_type = ccx.tcx().mk_closure_from_closure_substs(closure_id, Box::new(substs));
154 let llfn = declare::define_internal_rust_fn(ccx, &symbol[..], function_type);
1a4d82fc
JJ
155
156 // set an inline hint for all closures
9346a6ac 157 attributes::inline(llfn, attributes::InlineAttr::Hint);
1a4d82fc 158
85aaf69f 159 debug!("get_or_create_declaration_if_closure(): inserting new \
62682a34 160 closure {:?} (type {}): {:?}",
1a4d82fc 161 mono_id,
62682a34
SL
162 ccx.tn().type_to_string(val_ty(llfn)),
163 ccx.tn().val_to_string(llfn));
85aaf69f 164 ccx.closure_vals().borrow_mut().insert(mono_id, llfn);
1a4d82fc 165
c1a9b12d 166 llfn
1a4d82fc
JJ
167}
168
85aaf69f
SL
169pub enum Dest<'a, 'tcx: 'a> {
170 SaveIn(Block<'a, 'tcx>, ValueRef),
171 Ignore(&'a CrateContext<'a, 'tcx>)
172}
173
174pub fn trans_closure_expr<'a, 'tcx>(dest: Dest<'a, 'tcx>,
e9174d1e
SL
175 decl: &hir::FnDecl,
176 body: &hir::Block,
85aaf69f 177 id: ast::NodeId,
b039eaaf 178 closure_def_id: DefId, // (*)
c1a9b12d 179 closure_substs: &'tcx ty::ClosureSubsts<'tcx>)
85aaf69f 180 -> Option<Block<'a, 'tcx>>
1a4d82fc 181{
b039eaaf
SL
182 // (*) Note that in the case of inlined functions, the `closure_def_id` will be the
183 // defid of the closure in its original crate, whereas `id` will be the id of the local
184 // inlined copy.
185
c1a9b12d
SL
186 let param_substs = closure_substs.func_substs;
187
85aaf69f
SL
188 let ccx = match dest {
189 Dest::SaveIn(bcx, _) => bcx.ccx(),
190 Dest::Ignore(ccx) => ccx
191 };
192 let tcx = ccx.tcx();
62682a34 193 let _icx = push_ctxt("closure::trans_closure_expr");
1a4d82fc 194
b039eaaf
SL
195 debug!("trans_closure_expr(id={:?}, closure_def_id={:?}, closure_substs={:?})",
196 id, closure_def_id, closure_substs);
1a4d82fc 197
b039eaaf 198 let llfn = get_or_create_closure_declaration(ccx, closure_def_id, closure_substs);
1a4d82fc
JJ
199
200 // Get the type of this closure. Use the current `param_substs` as
201 // the closure substitutions. This makes sense because the closure
202 // takes the same set of type arguments as the enclosing fn, and
85aaf69f 203 // this function (`trans_closure`) is invoked at the point
1a4d82fc 204 // of the closure expression.
c1a9b12d
SL
205
206 let infcx = infer::normalizing_infer_ctxt(ccx.tcx(), &ccx.tcx().tables);
b039eaaf 207 let function_type = infcx.closure_type(closure_def_id, closure_substs);
1a4d82fc
JJ
208
209 let freevars: Vec<ty::Freevar> =
c1a9b12d 210 tcx.with_freevars(id, |fv| fv.iter().cloned().collect());
1a4d82fc 211
c1a9b12d 212 let sig = tcx.erase_late_bound_regions(&function_type.sig);
92a42be0 213 let sig = infer::normalize_associated_type(ccx.tcx(), &sig);
1a4d82fc 214
85aaf69f 215 trans_closure(ccx,
1a4d82fc
JJ
216 decl,
217 body,
c1a9b12d 218 llfn,
85aaf69f 219 param_substs,
1a4d82fc
JJ
220 id,
221 &[],
222 sig.output,
223 function_type.abi,
b039eaaf 224 ClosureEnv::Closure(closure_def_id, &freevars));
1a4d82fc
JJ
225
226 // Don't hoist this to the top of the function. It's perfectly legitimate
85aaf69f
SL
227 // to have a zero-size closure (in which case dest will be `Ignore`) and
228 // we must still generate the closure body.
229 let (mut bcx, dest_addr) = match dest {
230 Dest::SaveIn(bcx, p) => (bcx, p),
231 Dest::Ignore(_) => {
62682a34 232 debug!("trans_closure_expr() ignoring result");
85aaf69f 233 return None;
1a4d82fc
JJ
234 }
235 };
236
85aaf69f 237 let repr = adt::represent_type(ccx, node_id_type(bcx, id));
1a4d82fc
JJ
238
239 // Create the closure.
240 for (i, freevar) in freevars.iter().enumerate() {
241 let datum = expr::trans_local_var(bcx, freevar.def);
92a42be0
SL
242 let upvar_slot_dest = adt::trans_field_ptr(
243 bcx, &*repr, adt::MaybeSizedValue::sized(dest_addr), 0, i);
b039eaaf 244 let upvar_id = ty::UpvarId { var_id: freevar.def.var_id(),
85aaf69f
SL
245 closure_expr_id: id };
246 match tcx.upvar_capture(upvar_id).unwrap() {
247 ty::UpvarCapture::ByValue => {
1a4d82fc
JJ
248 bcx = datum.store_to(bcx, upvar_slot_dest);
249 }
85aaf69f 250 ty::UpvarCapture::ByRef(..) => {
1a4d82fc
JJ
251 Store(bcx, datum.to_llref(), upvar_slot_dest);
252 }
253 }
254 }
255 adt::trans_set_discr(bcx, &*repr, dest_addr, 0);
256
85aaf69f 257 Some(bcx)
1a4d82fc
JJ
258}
259
c34b1796 260pub fn trans_closure_method<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>,
e9174d1e 261 closure_def_id: DefId,
c1a9b12d 262 substs: ty::ClosureSubsts<'tcx>,
c34b1796
AL
263 trait_closure_kind: ty::ClosureKind)
264 -> ValueRef
265{
c1a9b12d
SL
266 // If this is a closure, redirect to it.
267 let llfn = get_or_create_closure_declaration(ccx, closure_def_id, &substs);
c34b1796
AL
268
269 // If the closure is a Fn closure, but a FnOnce is needed (etc),
270 // then adapt the self type
271 let closure_kind = ccx.tcx().closure_kind(closure_def_id);
272 trans_closure_adapter_shim(ccx,
273 closure_def_id,
274 substs,
275 closure_kind,
276 trait_closure_kind,
277 llfn)
278}
279
280fn trans_closure_adapter_shim<'a, 'tcx>(
281 ccx: &'a CrateContext<'a, 'tcx>,
e9174d1e 282 closure_def_id: DefId,
c1a9b12d 283 substs: ty::ClosureSubsts<'tcx>,
c34b1796
AL
284 llfn_closure_kind: ty::ClosureKind,
285 trait_closure_kind: ty::ClosureKind,
286 llfn: ValueRef)
287 -> ValueRef
288{
289 let _icx = push_ctxt("trans_closure_adapter_shim");
290 let tcx = ccx.tcx();
291
292 debug!("trans_closure_adapter_shim(llfn_closure_kind={:?}, \
293 trait_closure_kind={:?}, \
294 llfn={})",
295 llfn_closure_kind,
296 trait_closure_kind,
297 ccx.tn().val_to_string(llfn));
298
299 match (llfn_closure_kind, trait_closure_kind) {
300 (ty::FnClosureKind, ty::FnClosureKind) |
301 (ty::FnMutClosureKind, ty::FnMutClosureKind) |
302 (ty::FnOnceClosureKind, ty::FnOnceClosureKind) => {
303 // No adapter needed.
304 llfn
305 }
306 (ty::FnClosureKind, ty::FnMutClosureKind) => {
307 // The closure fn `llfn` is a `fn(&self, ...)`. We want a
308 // `fn(&mut self, ...)`. In fact, at trans time, these are
309 // basically the same thing, so we can just return llfn.
310 llfn
311 }
312 (ty::FnClosureKind, ty::FnOnceClosureKind) |
313 (ty::FnMutClosureKind, ty::FnOnceClosureKind) => {
314 // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
315 // self, ...)`. We want a `fn(self, ...)`. We can produce
316 // this by doing something like:
317 //
318 // fn call_once(self, ...) { call_mut(&self, ...) }
319 // fn call_once(mut self, ...) { call_mut(&mut self, ...) }
320 //
321 // These are both the same at trans time.
322 trans_fn_once_adapter_shim(ccx, closure_def_id, substs, llfn)
323 }
324 _ => {
325 tcx.sess.bug(&format!("trans_closure_adapter_shim: cannot convert {:?} to {:?}",
326 llfn_closure_kind,
327 trait_closure_kind));
328 }
329 }
330}
331
332fn trans_fn_once_adapter_shim<'a, 'tcx>(
333 ccx: &'a CrateContext<'a, 'tcx>,
e9174d1e 334 closure_def_id: DefId,
c1a9b12d 335 substs: ty::ClosureSubsts<'tcx>,
c34b1796
AL
336 llreffn: ValueRef)
337 -> ValueRef
338{
62682a34
SL
339 debug!("trans_fn_once_adapter_shim(closure_def_id={:?}, substs={:?}, llreffn={})",
340 closure_def_id,
341 substs,
c34b1796
AL
342 ccx.tn().val_to_string(llreffn));
343
344 let tcx = ccx.tcx();
c1a9b12d 345 let infcx = infer::normalizing_infer_ctxt(ccx.tcx(), &ccx.tcx().tables);
c34b1796
AL
346
347 // Find a version of the closure type. Substitute static for the
348 // region since it doesn't really matter.
c1a9b12d
SL
349 let closure_ty = tcx.mk_closure_from_closure_substs(closure_def_id, Box::new(substs.clone()));
350 let ref_closure_ty = tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic), closure_ty);
c34b1796
AL
351
352 // Make a version with the type of by-ref closure.
c1a9b12d 353 let ty::ClosureTy { unsafety, abi, mut sig } = infcx.closure_type(closure_def_id, &substs);
c34b1796
AL
354 sig.0.inputs.insert(0, ref_closure_ty); // sig has no self type as of yet
355 let llref_bare_fn_ty = tcx.mk_bare_fn(ty::BareFnTy { unsafety: unsafety,
356 abi: abi,
357 sig: sig.clone() });
c1a9b12d 358 let llref_fn_ty = tcx.mk_fn(None, llref_bare_fn_ty);
62682a34
SL
359 debug!("trans_fn_once_adapter_shim: llref_fn_ty={:?}",
360 llref_fn_ty);
c34b1796
AL
361
362 // Make a version of the closure type with the same arguments, but
363 // with argument #0 being by value.
364 assert_eq!(abi, RustCall);
365 sig.0.inputs[0] = closure_ty;
366 let llonce_bare_fn_ty = tcx.mk_bare_fn(ty::BareFnTy { unsafety: unsafety,
367 abi: abi,
368 sig: sig });
c1a9b12d 369 let llonce_fn_ty = tcx.mk_fn(None, llonce_bare_fn_ty);
c34b1796
AL
370
371 // Create the by-value helper.
372 let function_name = link::mangle_internal_name_by_type_and_seq(ccx, llonce_fn_ty, "once_shim");
c1a9b12d
SL
373 let lloncefn = declare::define_internal_rust_fn(ccx, &function_name,
374 llonce_fn_ty);
375 let sig = tcx.erase_late_bound_regions(&llonce_bare_fn_ty.sig);
92a42be0
SL
376 let sig = infer::normalize_associated_type(ccx.tcx(), &sig);
377
c34b1796
AL
378 let (block_arena, fcx): (TypedArena<_>, FunctionContext);
379 block_arena = TypedArena::new();
380 fcx = new_fn_ctxt(ccx,
381 lloncefn,
382 ast::DUMMY_NODE_ID,
383 false,
384 sig.output,
c1a9b12d 385 substs.func_substs,
c34b1796
AL
386 None,
387 &block_arena);
388 let mut bcx = init_function(&fcx, false, sig.output);
389
62682a34
SL
390 let llargs = get_params(fcx.llfn);
391
c34b1796
AL
392 // the first argument (`self`) will be the (by value) closure env.
393 let self_scope = fcx.push_custom_cleanup_scope();
394 let self_scope_id = CustomScope(self_scope);
395 let rvalue_mode = datum::appropriate_rvalue_mode(ccx, closure_ty);
62682a34
SL
396 let self_idx = fcx.arg_offset();
397 let llself = llargs[self_idx];
c34b1796
AL
398 let env_datum = Datum::new(llself, closure_ty, Rvalue::new(rvalue_mode));
399 let env_datum = unpack_datum!(bcx,
400 env_datum.to_lvalue_datum_in_scope(bcx, "self",
401 self_scope_id));
402
403 debug!("trans_fn_once_adapter_shim: env_datum={}",
404 bcx.val_to_string(env_datum.val));
405
c34b1796
AL
406 let dest =
407 fcx.llretslotptr.get().map(
408 |_| expr::SaveIn(fcx.get_ret_slot(bcx, sig.output, "ret_slot")));
409
410 let callee_data = TraitItem(MethodData { llfn: llreffn,
411 llself: env_datum.val });
412
c1a9b12d
SL
413 bcx = callee::trans_call_inner(bcx, DebugLoc::None, |bcx, _| {
414 Callee {
415 bcx: bcx,
416 data: callee_data,
417 ty: llref_fn_ty
418 }
419 }, ArgVals(&llargs[(self_idx + 1)..]), dest).bcx;
c34b1796
AL
420
421 fcx.pop_custom_cleanup_scope(self_scope);
422
423 finish_fn(&fcx, bcx, sig.output, DebugLoc::None);
424
425 lloncefn
426}