]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/closure.rs
2036feb31a25bcd84975e46c6a7bb73c6099f46a
[rust.git] / src / librustc_trans / trans / closure.rs
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
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;
15 use middle::infer;
16 use middle::traits::ProjectionMode;
17 use trans::abi::{Abi, FnType};
18 use trans::adt;
19 use trans::attributes;
20 use trans::base::*;
21 use trans::build::*;
22 use trans::callee::{self, ArgVals, Callee};
23 use trans::cleanup::{CleanupMethods, CustomScope, ScopeId};
24 use trans::common::*;
25 use trans::datum::{ByRef, Datum, lvalue_scratch_datum};
26 use trans::datum::{rvalue_scratch_datum, Rvalue};
27 use trans::debuginfo::{self, DebugLoc};
28 use trans::declare;
29 use trans::expr;
30 use trans::monomorphize::{Instance};
31 use trans::value::Value;
32 use trans::Disr;
33 use middle::ty::{self, Ty, TyCtxt};
34 use session::config::FullDebugInfo;
35
36 use syntax::ast;
37
38 use rustc_front::hir;
39
40 use libc::c_uint;
41
42 fn load_closure_environment<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
43                                         closure_def_id: DefId,
44                                         arg_scope_id: ScopeId,
45                                         id: ast::NodeId) {
46     let _icx = push_ctxt("closure::load_closure_environment");
47     let kind = kind_for_closure(bcx.ccx(), closure_def_id);
48
49     let env_arg = &bcx.fcx.fn_ty.args[0];
50     let mut env_idx = bcx.fcx.fn_ty.ret.is_indirect() as usize;
51
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);
57         llenv
58     } else {
59         get_param(bcx.fcx.llfn, env_idx as c_uint)
60     };
61
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);
67         Some(alloc)
68     } else {
69         None
70     };
71
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);
82                     true
83                 }
84             };
85             let node_id = freevar.def.var_id();
86             bcx.fcx.llupvars.borrow_mut().insert(node_id, upvar_ptr);
87
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,
91                                         upvar_ptr,
92                                         node_id_type(bcx, node_id),
93                                         hint)
94             }
95
96             if let Some(env_pointer_alloca) = env_pointer_alloca {
97                 debuginfo::create_captured_var_metadata(
98                     bcx,
99                     node_id,
100                     env_pointer_alloca,
101                     i,
102                     captured_by_ref,
103                     freevar.span);
104             }
105         }
106     })
107 }
108
109 pub enum ClosureEnv {
110     NotClosure,
111     Closure(DefId, ast::NodeId),
112 }
113
114 impl ClosureEnv {
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);
118         }
119     }
120 }
121
122 fn get_self_type<'tcx>(tcx: &TyCtxt<'tcx>,
123                        closure_id: DefId,
124                        fn_ty: Ty<'tcx>)
125                        -> Ty<'tcx> {
126     match tcx.closure_kind(closure_id) {
127         ty::ClosureKind::Fn => {
128             tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic), fn_ty)
129         }
130         ty::ClosureKind::FnMut => {
131             tcx.mk_mut_ref(tcx.mk_region(ty::ReStatic), fn_ty)
132         }
133         ty::ClosureKind::FnOnce => fn_ty,
134     }
135 }
136
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>,
140                                                closure_id: DefId,
141                                                substs: &ty::ClosureSubsts<'tcx>)
142                                                -> ValueRef {
143     // Normalize type so differences in regions and typedefs don't cause
144     // duplicate declarations
145     let tcx = ccx.tcx();
146     let substs = tcx.erase_regions(substs);
147     let instance = Instance {
148         def: closure_id,
149         params: &substs.func_substs.types
150     };
151
152     if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
153         debug!("get_or_create_closure_declaration(): found closure {:?}: {:?}",
154                instance, Value(llfn));
155         return llfn;
156     }
157
158     let path = tcx.def_path(closure_id);
159     let symbol = mangle_internal_name_by_path_and_seq(path, "closure");
160
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,
169         abi: Abi::RustCall,
170         sig: ty::Binder(ty::FnSig {
171             inputs: Some(get_self_type(tcx, closure_id, closure_type))
172                         .into_iter().chain(sig.inputs).collect(),
173             output: sig.output,
174             variadic: false
175         })
176     });
177     let llfn = declare::define_internal_fn(ccx, &symbol, function_type);
178
179     // set an inline hint for all closures
180     attributes::inline(llfn, attributes::InlineAttr::Hint);
181
182     debug!("get_or_create_declaration_if_closure(): inserting new \
183             closure {:?}: {:?}",
184            instance, Value(llfn));
185     ccx.instances().borrow_mut().insert(instance, llfn);
186
187     llfn
188 }
189
190 pub enum Dest<'a, 'tcx: 'a> {
191     SaveIn(Block<'a, 'tcx>, ValueRef),
192     Ignore(&'a CrateContext<'a, 'tcx>)
193 }
194
195 pub fn trans_closure_expr<'a, 'tcx>(dest: Dest<'a, 'tcx>,
196                                     decl: &hir::FnDecl,
197                                     body: &hir::Block,
198                                     id: ast::NodeId,
199                                     closure_def_id: DefId, // (*)
200                                     closure_substs: &ty::ClosureSubsts<'tcx>)
201                                     -> Option<Block<'a, 'tcx>>
202 {
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
205     // inlined copy.
206
207     let param_substs = closure_substs.func_substs;
208
209     let ccx = match dest {
210         Dest::SaveIn(bcx, _) => bcx.ccx(),
211         Dest::Ignore(ccx) => ccx
212     };
213     let tcx = ccx.tcx();
214     let _icx = push_ctxt("closure::trans_closure_expr");
215
216     debug!("trans_closure_expr(id={:?}, closure_def_id={:?}, closure_substs={:?})",
217            id, closure_def_id, closure_substs);
218
219     let llfn = get_or_create_closure_declaration(ccx, closure_def_id, closure_substs);
220
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.
226
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);
229
230     let sig = tcx.erase_late_bound_regions(&function_type.sig);
231     let sig = infer::normalize_associated_type(ccx.tcx(), &sig);
232
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(),
238         output: sig.output,
239         variadic: false
240     };
241     let fn_ty = FnType::new(ccx, Abi::RustCall, &sig, &[]);
242
243     trans_closure(ccx,
244                   decl,
245                   body,
246                   llfn,
247                   param_substs,
248                   closure_def_id,
249                   id,
250                   fn_ty,
251                   Abi::RustCall,
252                   ClosureEnv::Closure(closure_def_id, id));
253
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),
259         Dest::Ignore(_) => {
260             debug!("trans_closure_expr() ignoring result");
261             return None;
262         }
263     };
264
265     let repr = adt::represent_type(ccx, node_id_type(bcx, id));
266
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);
278                 }
279                 ty::UpvarCapture::ByRef(..) => {
280                     Store(bcx, datum.to_llref(), upvar_slot_dest);
281                 }
282             }
283         }
284     });
285     adt::trans_set_discr(bcx, &repr, dest_addr, Disr(0));
286
287     Some(bcx)
288 }
289
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)
294                                       -> ValueRef
295 {
296     // If this is a closure, redirect to it.
297     let llfn = get_or_create_closure_declaration(ccx, closure_def_id, &substs);
298
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);
302
303     let _icx = push_ctxt("trans_closure_adapter_shim");
304     let tcx = ccx.tcx();
305
306     debug!("trans_closure_adapter_shim(llfn_closure_kind={:?}, \
307            trait_closure_kind={:?}, llfn={:?})",
308            llfn_closure_kind, trait_closure_kind, Value(llfn));
309
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.
315             llfn
316         }
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.
321             llfn
322         }
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:
328             //
329             //     fn call_once(self, ...) { call_mut(&self, ...) }
330             //     fn call_once(mut self, ...) { call_mut(&mut self, ...) }
331             //
332             // These are both the same at trans time.
333             trans_fn_once_adapter_shim(ccx, closure_def_id, substs, llfn)
334         }
335         _ => {
336             tcx.sess.bug(&format!("trans_closure_adapter_shim: cannot convert {:?} to {:?}",
337                                   llfn_closure_kind,
338                                   trait_closure_kind));
339         }
340     }
341 }
342
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>,
347     llreffn: ValueRef)
348     -> ValueRef
349 {
350     debug!("trans_fn_once_adapter_shim(closure_def_id={:?}, substs={:?}, llreffn={:?})",
351            closure_def_id, substs, Value(llreffn));
352
353     let tcx = ccx.tcx();
354     let infcx = infer::normalizing_infer_ctxt(ccx.tcx(), &ccx.tcx().tables, ProjectionMode::Any);
355
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);
360
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 {
365         unsafety: unsafety,
366         abi: abi,
367         sig: sig.clone()
368     });
369     debug!("trans_fn_once_adapter_shim: llref_fn_ty={:?}",
370            llref_fn_ty);
371
372
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;
377
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, &[]);
381
382     let llonce_fn_ty = tcx.mk_fn_ptr(ty::BareFnTy {
383         unsafety: unsafety,
384         abi: abi,
385         sig: ty::Binder(sig)
386     });
387
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);
391
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);
396
397
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);
401
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)
408     } else {
409         unpack_datum!(bcx, lvalue_scratch_datum(bcx, closure_ty, "self",
410                                                 InitAlloca::Dropped,
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);
415             bcx
416         })).val
417     };
418
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() {
423         if self_idx > 0 {
424             self_idx -= 1;
425             llargs[self_idx] = llenv;
426         } else {
427             llargs.insert(0, llenv);
428         }
429     } else {
430         llargs[self_idx] = llenv;
431     }
432
433     let dest =
434         fcx.llretslotptr.get().map(
435             |_| expr::SaveIn(fcx.get_ret_slot(bcx, "ret_slot")));
436
437     let callee = Callee {
438         data: callee::Fn(llreffn),
439         ty: llref_fn_ty
440     };
441     bcx = callee.call(bcx, DebugLoc::None, ArgVals(&llargs[self_idx..]), dest).bcx;
442
443     fcx.pop_and_trans_custom_cleanup_scope(bcx, self_scope);
444
445     fcx.finish(bcx, DebugLoc::None);
446
447     lloncefn
448 }