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