]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/trans/closure.rs
Imported Upstream version 1.3.0+dfsg1
[rustc.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_params};
14 use middle::infer;
15 use trans::adt;
16 use trans::attributes;
17 use trans::base::*;
18 use trans::build::*;
19 use trans::callee::{self, ArgVals, Callee, TraitItem, MethodData};
20 use trans::cleanup::{CleanupMethods, CustomScope, ScopeId};
21 use trans::common::*;
22 use trans::datum::{self, Datum, rvalue_scratch_datum, Rvalue, ByValue};
23 use trans::debuginfo::{self, DebugLoc};
24 use trans::declare;
25 use trans::expr;
26 use trans::monomorphize::{MonoId};
27 use trans::type_of::*;
28 use middle::ty;
29 use session::config::FullDebugInfo;
30
31 use syntax::abi::RustCall;
32 use syntax::ast;
33 use syntax::ast_util;
34
35
36 fn load_closure_environment<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
37 arg_scope_id: ScopeId,
38 freevars: &[ty::Freevar])
39 -> Block<'blk, 'tcx>
40 {
41 let _icx = push_ctxt("closure::load_closure_environment");
42
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,
51 self_type,
52 "closure_env");
53 store_ty(bcx, bcx.fcx.llenv.unwrap(), datum.val, self_type);
54 datum.val
55 } else {
56 bcx.fcx.llenv.unwrap()
57 };
58
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);
64 Some(alloc)
65 } else {
66 None
67 };
68
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);
78 true
79 }
80 };
81 let def_id = freevar.def.def_id();
82 bcx.fcx.llupvars.borrow_mut().insert(def_id.node, upvar_ptr);
83
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,
87 upvar_ptr,
88 node_id_type(bcx, def_id.node),
89 hint)
90 }
91
92 if let Some(env_pointer_alloca) = env_pointer_alloca {
93 debuginfo::create_captured_var_metadata(
94 bcx,
95 def_id.node,
96 env_pointer_alloca,
97 i,
98 captured_by_ref,
99 freevar.span);
100 }
101 }
102
103 bcx
104 }
105
106 pub enum ClosureEnv<'a> {
107 NotClosure,
108 Closure(&'a [ty::Freevar]),
109 }
110
111 impl<'a> ClosureEnv<'a> {
112 pub fn load<'blk,'tcx>(self, bcx: Block<'blk, 'tcx>, arg_scope: ScopeId)
113 -> Block<'blk, 'tcx>
114 {
115 match self {
116 ClosureEnv::NotClosure => bcx,
117 ClosureEnv::Closure(freevars) => {
118 if freevars.is_empty() {
119 bcx
120 } else {
121 load_closure_environment(bcx, arg_scope, freevars)
122 }
123 }
124 }
125 }
126 }
127
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>)
133 -> ValueRef {
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 {
138 def: closure_id,
139 params: &substs.func_substs.types
140 };
141
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));
145 return llfn;
146 }
147
148 let symbol = ccx.tcx().map.with_path(closure_id.node, |path| {
149 mangle_internal_name_by_path_and_seq(path, "closure")
150 });
151
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);
154
155 // set an inline hint for all closures
156 attributes::inline(llfn, attributes::InlineAttr::Hint);
157
158 debug!("get_or_create_declaration_if_closure(): inserting new \
159 closure {:?} (type {}): {:?}",
160 mono_id,
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);
164
165 llfn
166 }
167
168 pub enum Dest<'a, 'tcx: 'a> {
169 SaveIn(Block<'a, 'tcx>, ValueRef),
170 Ignore(&'a CrateContext<'a, 'tcx>)
171 }
172
173 pub fn trans_closure_expr<'a, 'tcx>(dest: Dest<'a, 'tcx>,
174 decl: &ast::FnDecl,
175 body: &ast::Block,
176 id: ast::NodeId,
177 closure_substs: &'tcx ty::ClosureSubsts<'tcx>)
178 -> Option<Block<'a, 'tcx>>
179 {
180 let param_substs = closure_substs.func_substs;
181
182 let ccx = match dest {
183 Dest::SaveIn(bcx, _) => bcx.ccx(),
184 Dest::Ignore(ccx) => ccx
185 };
186 let tcx = ccx.tcx();
187 let _icx = push_ctxt("closure::trans_closure_expr");
188
189 debug!("trans_closure_expr()");
190
191 let closure_id = ast_util::local_def(id);
192 let llfn = get_or_create_closure_declaration(ccx, closure_id, closure_substs);
193
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.
199
200 let infcx = infer::normalizing_infer_ctxt(ccx.tcx(), &ccx.tcx().tables);
201 let function_type = infcx.closure_type(closure_id, closure_substs);
202
203 let freevars: Vec<ty::Freevar> =
204 tcx.with_freevars(id, |fv| fv.iter().cloned().collect());
205
206 let sig = tcx.erase_late_bound_regions(&function_type.sig);
207
208 trans_closure(ccx,
209 decl,
210 body,
211 llfn,
212 param_substs,
213 id,
214 &[],
215 sig.output,
216 function_type.abi,
217 ClosureEnv::Closure(&freevars));
218
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),
224 Dest::Ignore(_) => {
225 debug!("trans_closure_expr() ignoring result");
226 return None;
227 }
228 };
229
230 let repr = adt::represent_type(ccx, node_id_type(bcx, id));
231
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);
241 }
242 ty::UpvarCapture::ByRef(..) => {
243 Store(bcx, datum.to_llref(), upvar_slot_dest);
244 }
245 }
246 }
247 adt::trans_set_discr(bcx, &*repr, dest_addr, 0);
248
249 Some(bcx)
250 }
251
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)
256 -> ValueRef
257 {
258 // If this is a closure, redirect to it.
259 let llfn = get_or_create_closure_declaration(ccx, closure_def_id, &substs);
260
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,
265 closure_def_id,
266 substs,
267 closure_kind,
268 trait_closure_kind,
269 llfn)
270 }
271
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,
278 llfn: ValueRef)
279 -> ValueRef
280 {
281 let _icx = push_ctxt("trans_closure_adapter_shim");
282 let tcx = ccx.tcx();
283
284 debug!("trans_closure_adapter_shim(llfn_closure_kind={:?}, \
285 trait_closure_kind={:?}, \
286 llfn={})",
287 llfn_closure_kind,
288 trait_closure_kind,
289 ccx.tn().val_to_string(llfn));
290
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.
296 llfn
297 }
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.
302 llfn
303 }
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:
309 //
310 // fn call_once(self, ...) { call_mut(&self, ...) }
311 // fn call_once(mut self, ...) { call_mut(&mut self, ...) }
312 //
313 // These are both the same at trans time.
314 trans_fn_once_adapter_shim(ccx, closure_def_id, substs, llfn)
315 }
316 _ => {
317 tcx.sess.bug(&format!("trans_closure_adapter_shim: cannot convert {:?} to {:?}",
318 llfn_closure_kind,
319 trait_closure_kind));
320 }
321 }
322 }
323
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>,
328 llreffn: ValueRef)
329 -> ValueRef
330 {
331 debug!("trans_fn_once_adapter_shim(closure_def_id={:?}, substs={:?}, llreffn={})",
332 closure_def_id,
333 substs,
334 ccx.tn().val_to_string(llreffn));
335
336 let tcx = ccx.tcx();
337 let infcx = infer::normalizing_infer_ctxt(ccx.tcx(), &ccx.tcx().tables);
338
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);
343
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,
348 abi: abi,
349 sig: sig.clone() });
350 let llref_fn_ty = tcx.mk_fn(None, llref_bare_fn_ty);
351 debug!("trans_fn_once_adapter_shim: llref_fn_ty={:?}",
352 llref_fn_ty);
353
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,
359 abi: abi,
360 sig: sig });
361 let llonce_fn_ty = tcx.mk_fn(None, llonce_bare_fn_ty);
362
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,
366 llonce_fn_ty);
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,
371 lloncefn,
372 ast::DUMMY_NODE_ID,
373 false,
374 sig.output,
375 substs.func_substs,
376 None,
377 &block_arena);
378 let mut bcx = init_function(&fcx, false, sig.output);
379
380 let llargs = get_params(fcx.llfn);
381
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",
391 self_scope_id));
392
393 debug!("trans_fn_once_adapter_shim: env_datum={}",
394 bcx.val_to_string(env_datum.val));
395
396 let dest =
397 fcx.llretslotptr.get().map(
398 |_| expr::SaveIn(fcx.get_ret_slot(bcx, sig.output, "ret_slot")));
399
400 let callee_data = TraitItem(MethodData { llfn: llreffn,
401 llself: env_datum.val });
402
403 bcx = callee::trans_call_inner(bcx, DebugLoc::None, |bcx, _| {
404 Callee {
405 bcx: bcx,
406 data: callee_data,
407 ty: llref_fn_ty
408 }
409 }, ArgVals(&llargs[(self_idx + 1)..]), dest).bcx;
410
411 fcx.pop_custom_cleanup_scope(self_scope);
412
413 finish_fn(&fcx, bcx, sig.output, DebugLoc::None);
414
415 lloncefn
416 }