]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/trans/closure.rs
Imported Upstream version 1.0.0~beta.3
[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_param};
14 use middle::mem_categorization::Typer;
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::{self, MonoId};
27 use trans::type_of::*;
28 use middle::ty::{self, ClosureTyper};
29 use middle::subst::Substs;
30 use session::config::FullDebugInfo;
31 use util::ppaux::Repr;
32
33 use syntax::abi::RustCall;
34 use syntax::ast;
35 use syntax::ast_util;
36
37
38 fn load_closure_environment<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
39 arg_scope_id: ScopeId,
40 freevars: &[ty::Freevar])
41 -> Block<'blk, 'tcx>
42 {
43 let _icx = push_ctxt("closure::load_closure_environment");
44
45 // Special case for small by-value selfs.
46 let closure_id = ast_util::local_def(bcx.fcx.id);
47 let self_type = self_type_for_closure(bcx.ccx(), closure_id,
48 node_id_type(bcx, closure_id.node));
49 let kind = kind_for_closure(bcx.ccx(), closure_id);
50 let llenv = if kind == ty::FnOnceClosureKind &&
51 !arg_is_indirect(bcx.ccx(), self_type) {
52 let datum = rvalue_scratch_datum(bcx,
53 self_type,
54 "closure_env");
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() {
72 let upvar_id = ty::UpvarId { var_id: freevar.def.local_node_id(),
73 closure_expr_id: closure_id.node };
74 let upvar_capture = bcx.tcx().upvar_capture(upvar_id).unwrap();
75 let mut upvar_ptr = GEPi(bcx, llenv, &[0, i]);
76 let captured_by_ref = match upvar_capture {
77 ty::UpvarCapture::ByValue => false,
78 ty::UpvarCapture::ByRef(..) => {
79 upvar_ptr = Load(bcx, upvar_ptr);
80 true
81 }
82 };
83 let def_id = freevar.def.def_id();
84 bcx.fcx.llupvars.borrow_mut().insert(def_id.node, upvar_ptr);
85
86 if kind == ty::FnOnceClosureKind && !captured_by_ref {
87 bcx.fcx.schedule_drop_mem(arg_scope_id,
88 upvar_ptr,
89 node_id_type(bcx, def_id.node))
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_declaration_if_closure<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
131 closure_id: ast::DefId,
132 substs: &Substs<'tcx>)
133 -> Option<Datum<'tcx, Rvalue>> {
134 if !ccx.tcx().closure_kinds.borrow().contains_key(&closure_id) {
135 // Not a closure.
136 return None
137 }
138
139 let function_type = ty::node_id_to_type(ccx.tcx(), closure_id.node);
140 let function_type = monomorphize::apply_param_substs(ccx.tcx(), substs, &function_type);
141
142 // Normalize type so differences in regions and typedefs don't cause
143 // duplicate declarations
144 let function_type = erase_regions(ccx.tcx(), &function_type);
145 let params = match function_type.sty {
146 ty::ty_closure(_, substs) => &substs.types,
147 _ => unreachable!()
148 };
149 let mono_id = MonoId {
150 def: closure_id,
151 params: params
152 };
153
154 match ccx.closure_vals().borrow().get(&mono_id) {
155 Some(&llfn) => {
156 debug!("get_or_create_declaration_if_closure(): found closure");
157 return Some(Datum::new(llfn, function_type, Rvalue::new(ByValue)))
158 }
159 None => {}
160 }
161
162 let symbol = ccx.tcx().map.with_path(closure_id.node, |path| {
163 mangle_internal_name_by_path_and_seq(path, "closure")
164 });
165
166 // Currently there’s only a single user of get_or_create_declaration_if_closure and it
167 // unconditionally defines the function, therefore we use define_* here.
168 let llfn = declare::define_internal_rust_fn(ccx, &symbol[..], function_type).unwrap_or_else(||{
169 ccx.sess().bug(&format!("symbol `{}` already defined", symbol));
170 });
171
172 // set an inline hint for all closures
173 attributes::inline(llfn, attributes::InlineAttr::Hint);
174
175 debug!("get_or_create_declaration_if_closure(): inserting new \
176 closure {:?} (type {})",
177 mono_id,
178 ccx.tn().type_to_string(val_ty(llfn)));
179 ccx.closure_vals().borrow_mut().insert(mono_id, llfn);
180
181 Some(Datum::new(llfn, function_type, Rvalue::new(ByValue)))
182 }
183
184 pub enum Dest<'a, 'tcx: 'a> {
185 SaveIn(Block<'a, 'tcx>, ValueRef),
186 Ignore(&'a CrateContext<'a, 'tcx>)
187 }
188
189 pub fn trans_closure_expr<'a, 'tcx>(dest: Dest<'a, 'tcx>,
190 decl: &ast::FnDecl,
191 body: &ast::Block,
192 id: ast::NodeId,
193 param_substs: &'tcx Substs<'tcx>)
194 -> Option<Block<'a, 'tcx>>
195 {
196 let ccx = match dest {
197 Dest::SaveIn(bcx, _) => bcx.ccx(),
198 Dest::Ignore(ccx) => ccx
199 };
200 let tcx = ccx.tcx();
201 let _icx = push_ctxt("closure::trans_closure");
202
203 debug!("trans_closure()");
204
205 let closure_id = ast_util::local_def(id);
206 let llfn = get_or_create_declaration_if_closure(
207 ccx,
208 closure_id,
209 param_substs).unwrap();
210
211 // Get the type of this closure. Use the current `param_substs` as
212 // the closure substitutions. This makes sense because the closure
213 // takes the same set of type arguments as the enclosing fn, and
214 // this function (`trans_closure`) is invoked at the point
215 // of the closure expression.
216 let typer = NormalizingClosureTyper::new(tcx);
217 let function_type = typer.closure_type(closure_id, param_substs);
218
219 let freevars: Vec<ty::Freevar> =
220 ty::with_freevars(tcx, id, |fv| fv.iter().cloned().collect());
221
222 let sig = ty::erase_late_bound_regions(tcx, &function_type.sig);
223
224 trans_closure(ccx,
225 decl,
226 body,
227 llfn.val,
228 param_substs,
229 id,
230 &[],
231 sig.output,
232 function_type.abi,
233 ClosureEnv::Closure(&freevars[..]));
234
235 // Don't hoist this to the top of the function. It's perfectly legitimate
236 // to have a zero-size closure (in which case dest will be `Ignore`) and
237 // we must still generate the closure body.
238 let (mut bcx, dest_addr) = match dest {
239 Dest::SaveIn(bcx, p) => (bcx, p),
240 Dest::Ignore(_) => {
241 debug!("trans_closure() ignoring result");
242 return None;
243 }
244 };
245
246 let repr = adt::represent_type(ccx, node_id_type(bcx, id));
247
248 // Create the closure.
249 for (i, freevar) in freevars.iter().enumerate() {
250 let datum = expr::trans_local_var(bcx, freevar.def);
251 let upvar_slot_dest = adt::trans_field_ptr(bcx, &*repr, dest_addr, 0, i);
252 let upvar_id = ty::UpvarId { var_id: freevar.def.local_node_id(),
253 closure_expr_id: id };
254 match tcx.upvar_capture(upvar_id).unwrap() {
255 ty::UpvarCapture::ByValue => {
256 bcx = datum.store_to(bcx, upvar_slot_dest);
257 }
258 ty::UpvarCapture::ByRef(..) => {
259 Store(bcx, datum.to_llref(), upvar_slot_dest);
260 }
261 }
262 }
263 adt::trans_set_discr(bcx, &*repr, dest_addr, 0);
264
265 Some(bcx)
266 }
267
268 pub fn trans_closure_method<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>,
269 closure_def_id: ast::DefId,
270 substs: Substs<'tcx>,
271 node: ExprOrMethodCall,
272 param_substs: &'tcx Substs<'tcx>,
273 trait_closure_kind: ty::ClosureKind)
274 -> ValueRef
275 {
276 // The substitutions should have no type parameters remaining
277 // after passing through fulfill_obligation
278 let llfn = callee::trans_fn_ref_with_substs(ccx,
279 closure_def_id,
280 node,
281 param_substs,
282 substs.clone()).val;
283
284 // If the closure is a Fn closure, but a FnOnce is needed (etc),
285 // then adapt the self type
286 let closure_kind = ccx.tcx().closure_kind(closure_def_id);
287 trans_closure_adapter_shim(ccx,
288 closure_def_id,
289 substs,
290 closure_kind,
291 trait_closure_kind,
292 llfn)
293 }
294
295 fn trans_closure_adapter_shim<'a, 'tcx>(
296 ccx: &'a CrateContext<'a, 'tcx>,
297 closure_def_id: ast::DefId,
298 substs: Substs<'tcx>,
299 llfn_closure_kind: ty::ClosureKind,
300 trait_closure_kind: ty::ClosureKind,
301 llfn: ValueRef)
302 -> ValueRef
303 {
304 let _icx = push_ctxt("trans_closure_adapter_shim");
305 let tcx = ccx.tcx();
306
307 debug!("trans_closure_adapter_shim(llfn_closure_kind={:?}, \
308 trait_closure_kind={:?}, \
309 llfn={})",
310 llfn_closure_kind,
311 trait_closure_kind,
312 ccx.tn().val_to_string(llfn));
313
314 match (llfn_closure_kind, trait_closure_kind) {
315 (ty::FnClosureKind, ty::FnClosureKind) |
316 (ty::FnMutClosureKind, ty::FnMutClosureKind) |
317 (ty::FnOnceClosureKind, ty::FnOnceClosureKind) => {
318 // No adapter needed.
319 llfn
320 }
321 (ty::FnClosureKind, ty::FnMutClosureKind) => {
322 // The closure fn `llfn` is a `fn(&self, ...)`. We want a
323 // `fn(&mut self, ...)`. In fact, at trans time, these are
324 // basically the same thing, so we can just return llfn.
325 llfn
326 }
327 (ty::FnClosureKind, ty::FnOnceClosureKind) |
328 (ty::FnMutClosureKind, ty::FnOnceClosureKind) => {
329 // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
330 // self, ...)`. We want a `fn(self, ...)`. We can produce
331 // this by doing something like:
332 //
333 // fn call_once(self, ...) { call_mut(&self, ...) }
334 // fn call_once(mut self, ...) { call_mut(&mut self, ...) }
335 //
336 // These are both the same at trans time.
337 trans_fn_once_adapter_shim(ccx, closure_def_id, substs, llfn)
338 }
339 _ => {
340 tcx.sess.bug(&format!("trans_closure_adapter_shim: cannot convert {:?} to {:?}",
341 llfn_closure_kind,
342 trait_closure_kind));
343 }
344 }
345 }
346
347 fn trans_fn_once_adapter_shim<'a, 'tcx>(
348 ccx: &'a CrateContext<'a, 'tcx>,
349 closure_def_id: ast::DefId,
350 substs: Substs<'tcx>,
351 llreffn: ValueRef)
352 -> ValueRef
353 {
354 debug!("trans_fn_once_adapter_shim(closure_def_id={}, substs={}, llreffn={})",
355 closure_def_id.repr(ccx.tcx()),
356 substs.repr(ccx.tcx()),
357 ccx.tn().val_to_string(llreffn));
358
359 let tcx = ccx.tcx();
360 let typer = NormalizingClosureTyper::new(tcx);
361
362 // Find a version of the closure type. Substitute static for the
363 // region since it doesn't really matter.
364 let substs = tcx.mk_substs(substs);
365 let closure_ty = ty::mk_closure(tcx, closure_def_id, substs);
366 let ref_closure_ty = ty::mk_imm_rptr(tcx, tcx.mk_region(ty::ReStatic), closure_ty);
367
368 // Make a version with the type of by-ref closure.
369 let ty::ClosureTy { unsafety, abi, mut sig } = typer.closure_type(closure_def_id, substs);
370 sig.0.inputs.insert(0, ref_closure_ty); // sig has no self type as of yet
371 let llref_bare_fn_ty = tcx.mk_bare_fn(ty::BareFnTy { unsafety: unsafety,
372 abi: abi,
373 sig: sig.clone() });
374 let llref_fn_ty = ty::mk_bare_fn(tcx, None, llref_bare_fn_ty);
375 debug!("trans_fn_once_adapter_shim: llref_fn_ty={}",
376 llref_fn_ty.repr(tcx));
377
378 // Make a version of the closure type with the same arguments, but
379 // with argument #0 being by value.
380 assert_eq!(abi, RustCall);
381 sig.0.inputs[0] = closure_ty;
382 let llonce_bare_fn_ty = tcx.mk_bare_fn(ty::BareFnTy { unsafety: unsafety,
383 abi: abi,
384 sig: sig });
385 let llonce_fn_ty = ty::mk_bare_fn(tcx, None, llonce_bare_fn_ty);
386
387 // Create the by-value helper.
388 let function_name = link::mangle_internal_name_by_type_and_seq(ccx, llonce_fn_ty, "once_shim");
389 let lloncefn = declare::define_internal_rust_fn(ccx, &function_name[..], llonce_fn_ty)
390 .unwrap_or_else(||{
391 ccx.sess().bug(&format!("symbol `{}` already defined", function_name));
392 });
393
394 let sig = ty::erase_late_bound_regions(tcx, &llonce_bare_fn_ty.sig);
395 let (block_arena, fcx): (TypedArena<_>, FunctionContext);
396 block_arena = TypedArena::new();
397 fcx = new_fn_ctxt(ccx,
398 lloncefn,
399 ast::DUMMY_NODE_ID,
400 false,
401 sig.output,
402 substs,
403 None,
404 &block_arena);
405 let mut bcx = init_function(&fcx, false, sig.output);
406
407 // the first argument (`self`) will be the (by value) closure env.
408 let self_scope = fcx.push_custom_cleanup_scope();
409 let self_scope_id = CustomScope(self_scope);
410 let rvalue_mode = datum::appropriate_rvalue_mode(ccx, closure_ty);
411 let llself = get_param(lloncefn, fcx.arg_pos(0) as u32);
412 let env_datum = Datum::new(llself, closure_ty, Rvalue::new(rvalue_mode));
413 let env_datum = unpack_datum!(bcx,
414 env_datum.to_lvalue_datum_in_scope(bcx, "self",
415 self_scope_id));
416
417 debug!("trans_fn_once_adapter_shim: env_datum={}",
418 bcx.val_to_string(env_datum.val));
419
420 // the remaining arguments will be packed up in a tuple.
421 let input_tys = match sig.inputs[1].sty {
422 ty::ty_tup(ref tys) => &**tys,
423 _ => bcx.sess().bug(&format!("trans_fn_once_adapter_shim: not rust-call! \
424 closure_def_id={}",
425 closure_def_id.repr(tcx)))
426 };
427 let llargs: Vec<_> =
428 input_tys.iter()
429 .enumerate()
430 .map(|(i, _)| get_param(lloncefn, fcx.arg_pos(i+1) as u32))
431 .collect();
432
433 let dest =
434 fcx.llretslotptr.get().map(
435 |_| expr::SaveIn(fcx.get_ret_slot(bcx, sig.output, "ret_slot")));
436
437 let callee_data = TraitItem(MethodData { llfn: llreffn,
438 llself: env_datum.val });
439
440 bcx = callee::trans_call_inner(bcx,
441 DebugLoc::None,
442 llref_fn_ty,
443 |bcx, _| Callee { bcx: bcx, data: callee_data },
444 ArgVals(&llargs),
445 dest).bcx;
446
447 fcx.pop_custom_cleanup_scope(self_scope);
448
449 finish_fn(&fcx, bcx, sig.output, DebugLoc::None);
450
451 lloncefn
452 }