]>
Commit | Line | Data |
---|---|---|
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 |
11 | use arena::TypedArena; |
12 | use back::link::{self, mangle_internal_name_by_path_and_seq}; | |
62682a34 | 13 | use llvm::{ValueRef, get_params}; |
c1a9b12d | 14 | use middle::infer; |
1a4d82fc | 15 | use trans::adt; |
9346a6ac | 16 | use trans::attributes; |
1a4d82fc JJ |
17 | use trans::base::*; |
18 | use trans::build::*; | |
c34b1796 AL |
19 | use trans::callee::{self, ArgVals, Callee, TraitItem, MethodData}; |
20 | use trans::cleanup::{CleanupMethods, CustomScope, ScopeId}; | |
1a4d82fc | 21 | use trans::common::*; |
c34b1796 AL |
22 | use trans::datum::{self, Datum, rvalue_scratch_datum, Rvalue, ByValue}; |
23 | use trans::debuginfo::{self, DebugLoc}; | |
9346a6ac | 24 | use trans::declare; |
1a4d82fc | 25 | use trans::expr; |
c1a9b12d | 26 | use trans::monomorphize::{MonoId}; |
1a4d82fc | 27 | use trans::type_of::*; |
c1a9b12d | 28 | use middle::ty; |
1a4d82fc | 29 | use session::config::FullDebugInfo; |
1a4d82fc | 30 | |
c34b1796 | 31 | use syntax::abi::RustCall; |
1a4d82fc JJ |
32 | use syntax::ast; |
33 | use syntax::ast_util; | |
34 | ||
1a4d82fc | 35 | |
85aaf69f SL |
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"); | |
1a4d82fc JJ |
42 | |
43 | // Special case for small by-value selfs. | |
44 | let closure_id = ast_util::local_def(bcx.fcx.id); | |
85aaf69f | 45 | let self_type = self_type_for_closure(bcx.ccx(), closure_id, |
1a4d82fc | 46 | node_id_type(bcx, closure_id.node)); |
85aaf69f SL |
47 | let kind = kind_for_closure(bcx.ccx(), closure_id); |
48 | let llenv = if kind == ty::FnOnceClosureKind && | |
1a4d82fc JJ |
49 | !arg_is_indirect(bcx.ccx(), self_type) { |
50 | let datum = rvalue_scratch_datum(bcx, | |
51 | self_type, | |
85aaf69f | 52 | "closure_env"); |
1a4d82fc JJ |
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() { | |
85aaf69f SL |
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(); | |
1a4d82fc | 73 | let mut upvar_ptr = GEPi(bcx, llenv, &[0, i]); |
85aaf69f SL |
74 | let captured_by_ref = match upvar_capture { |
75 | ty::UpvarCapture::ByValue => false, | |
76 | ty::UpvarCapture::ByRef(..) => { | |
1a4d82fc JJ |
77 | upvar_ptr = Load(bcx, upvar_ptr); |
78 | true | |
79 | } | |
1a4d82fc JJ |
80 | }; |
81 | let def_id = freevar.def.def_id(); | |
82 | bcx.fcx.llupvars.borrow_mut().insert(def_id.node, upvar_ptr); | |
83 | ||
85aaf69f | 84 | if kind == ty::FnOnceClosureKind && !captured_by_ref { |
c1a9b12d | 85 | let hint = bcx.fcx.lldropflag_hints.borrow().hint_datum(upvar_id.var_id); |
1a4d82fc JJ |
86 | bcx.fcx.schedule_drop_mem(arg_scope_id, |
87 | upvar_ptr, | |
c1a9b12d SL |
88 | node_id_type(bcx, def_id.node), |
89 | hint) | |
1a4d82fc JJ |
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 | ||
85aaf69f | 106 | pub enum ClosureEnv<'a> { |
1a4d82fc | 107 | NotClosure, |
85aaf69f | 108 | Closure(&'a [ty::Freevar]), |
1a4d82fc JJ |
109 | } |
110 | ||
85aaf69f SL |
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 | } | |
1a4d82fc JJ |
123 | } |
124 | } | |
125 | } | |
126 | } | |
127 | ||
85aaf69f SL |
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. | |
c1a9b12d SL |
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 { | |
1a4d82fc JJ |
134 | // Normalize type so differences in regions and typedefs don't cause |
135 | // duplicate declarations | |
c1a9b12d | 136 | let substs = erase_regions(ccx.tcx(), substs); |
1a4d82fc JJ |
137 | let mono_id = MonoId { |
138 | def: closure_id, | |
c1a9b12d | 139 | params: &substs.func_substs.types |
1a4d82fc JJ |
140 | }; |
141 | ||
c1a9b12d SL |
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; | |
1a4d82fc JJ |
146 | } |
147 | ||
148 | let symbol = ccx.tcx().map.with_path(closure_id.node, |path| { | |
85aaf69f | 149 | mangle_internal_name_by_path_and_seq(path, "closure") |
1a4d82fc JJ |
150 | }); |
151 | ||
c1a9b12d SL |
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); | |
1a4d82fc JJ |
154 | |
155 | // set an inline hint for all closures | |
9346a6ac | 156 | attributes::inline(llfn, attributes::InlineAttr::Hint); |
1a4d82fc | 157 | |
85aaf69f | 158 | debug!("get_or_create_declaration_if_closure(): inserting new \ |
62682a34 | 159 | closure {:?} (type {}): {:?}", |
1a4d82fc | 160 | mono_id, |
62682a34 SL |
161 | ccx.tn().type_to_string(val_ty(llfn)), |
162 | ccx.tn().val_to_string(llfn)); | |
85aaf69f | 163 | ccx.closure_vals().borrow_mut().insert(mono_id, llfn); |
1a4d82fc | 164 | |
c1a9b12d | 165 | llfn |
1a4d82fc JJ |
166 | } |
167 | ||
85aaf69f SL |
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, | |
c1a9b12d | 177 | closure_substs: &'tcx ty::ClosureSubsts<'tcx>) |
85aaf69f | 178 | -> Option<Block<'a, 'tcx>> |
1a4d82fc | 179 | { |
c1a9b12d SL |
180 | let param_substs = closure_substs.func_substs; |
181 | ||
85aaf69f SL |
182 | let ccx = match dest { |
183 | Dest::SaveIn(bcx, _) => bcx.ccx(), | |
184 | Dest::Ignore(ccx) => ccx | |
185 | }; | |
186 | let tcx = ccx.tcx(); | |
62682a34 | 187 | let _icx = push_ctxt("closure::trans_closure_expr"); |
1a4d82fc | 188 | |
62682a34 | 189 | debug!("trans_closure_expr()"); |
1a4d82fc JJ |
190 | |
191 | let closure_id = ast_util::local_def(id); | |
c1a9b12d | 192 | let llfn = get_or_create_closure_declaration(ccx, closure_id, closure_substs); |
1a4d82fc JJ |
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 | |
85aaf69f | 197 | // this function (`trans_closure`) is invoked at the point |
1a4d82fc | 198 | // of the closure expression. |
c1a9b12d SL |
199 | |
200 | let infcx = infer::normalizing_infer_ctxt(ccx.tcx(), &ccx.tcx().tables); | |
201 | let function_type = infcx.closure_type(closure_id, closure_substs); | |
1a4d82fc JJ |
202 | |
203 | let freevars: Vec<ty::Freevar> = | |
c1a9b12d | 204 | tcx.with_freevars(id, |fv| fv.iter().cloned().collect()); |
1a4d82fc | 205 | |
c1a9b12d | 206 | let sig = tcx.erase_late_bound_regions(&function_type.sig); |
1a4d82fc | 207 | |
85aaf69f | 208 | trans_closure(ccx, |
1a4d82fc JJ |
209 | decl, |
210 | body, | |
c1a9b12d | 211 | llfn, |
85aaf69f | 212 | param_substs, |
1a4d82fc JJ |
213 | id, |
214 | &[], | |
215 | sig.output, | |
216 | function_type.abi, | |
62682a34 | 217 | ClosureEnv::Closure(&freevars)); |
1a4d82fc JJ |
218 | |
219 | // Don't hoist this to the top of the function. It's perfectly legitimate | |
85aaf69f SL |
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(_) => { | |
62682a34 | 225 | debug!("trans_closure_expr() ignoring result"); |
85aaf69f | 226 | return None; |
1a4d82fc JJ |
227 | } |
228 | }; | |
229 | ||
85aaf69f | 230 | let repr = adt::represent_type(ccx, node_id_type(bcx, id)); |
1a4d82fc JJ |
231 | |
232 | // Create the closure. | |
233 | for (i, freevar) in freevars.iter().enumerate() { | |
234 | let datum = expr::trans_local_var(bcx, freevar.def); | |
c34b1796 | 235 | let upvar_slot_dest = adt::trans_field_ptr(bcx, &*repr, dest_addr, 0, i); |
85aaf69f SL |
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 => { | |
1a4d82fc JJ |
240 | bcx = datum.store_to(bcx, upvar_slot_dest); |
241 | } | |
85aaf69f | 242 | ty::UpvarCapture::ByRef(..) => { |
1a4d82fc JJ |
243 | Store(bcx, datum.to_llref(), upvar_slot_dest); |
244 | } | |
245 | } | |
246 | } | |
247 | adt::trans_set_discr(bcx, &*repr, dest_addr, 0); | |
248 | ||
85aaf69f | 249 | Some(bcx) |
1a4d82fc JJ |
250 | } |
251 | ||
c34b1796 AL |
252 | pub fn trans_closure_method<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>, |
253 | closure_def_id: ast::DefId, | |
c1a9b12d | 254 | substs: ty::ClosureSubsts<'tcx>, |
c34b1796 AL |
255 | trait_closure_kind: ty::ClosureKind) |
256 | -> ValueRef | |
257 | { | |
c1a9b12d SL |
258 | // If this is a closure, redirect to it. |
259 | let llfn = get_or_create_closure_declaration(ccx, closure_def_id, &substs); | |
c34b1796 AL |
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, | |
c1a9b12d | 275 | substs: ty::ClosureSubsts<'tcx>, |
c34b1796 AL |
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, | |
c1a9b12d | 327 | substs: ty::ClosureSubsts<'tcx>, |
c34b1796 AL |
328 | llreffn: ValueRef) |
329 | -> ValueRef | |
330 | { | |
62682a34 SL |
331 | debug!("trans_fn_once_adapter_shim(closure_def_id={:?}, substs={:?}, llreffn={})", |
332 | closure_def_id, | |
333 | substs, | |
c34b1796 AL |
334 | ccx.tn().val_to_string(llreffn)); |
335 | ||
336 | let tcx = ccx.tcx(); | |
c1a9b12d | 337 | let infcx = infer::normalizing_infer_ctxt(ccx.tcx(), &ccx.tcx().tables); |
c34b1796 AL |
338 | |
339 | // Find a version of the closure type. Substitute static for the | |
340 | // region since it doesn't really matter. | |
c1a9b12d SL |
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); | |
c34b1796 AL |
343 | |
344 | // Make a version with the type of by-ref closure. | |
c1a9b12d | 345 | let ty::ClosureTy { unsafety, abi, mut sig } = infcx.closure_type(closure_def_id, &substs); |
c34b1796 AL |
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() }); | |
c1a9b12d | 350 | let llref_fn_ty = tcx.mk_fn(None, llref_bare_fn_ty); |
62682a34 SL |
351 | debug!("trans_fn_once_adapter_shim: llref_fn_ty={:?}", |
352 | llref_fn_ty); | |
c34b1796 AL |
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 }); | |
c1a9b12d | 361 | let llonce_fn_ty = tcx.mk_fn(None, llonce_bare_fn_ty); |
c34b1796 AL |
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"); | |
c1a9b12d SL |
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); | |
c34b1796 AL |
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, | |
c1a9b12d | 375 | substs.func_substs, |
c34b1796 AL |
376 | None, |
377 | &block_arena); | |
378 | let mut bcx = init_function(&fcx, false, sig.output); | |
379 | ||
62682a34 SL |
380 | let llargs = get_params(fcx.llfn); |
381 | ||
c34b1796 AL |
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); | |
62682a34 SL |
386 | let self_idx = fcx.arg_offset(); |
387 | let llself = llargs[self_idx]; | |
c34b1796 AL |
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 | ||
c34b1796 AL |
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 | ||
c1a9b12d SL |
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; | |
c34b1796 AL |
410 | |
411 | fcx.pop_custom_cleanup_scope(self_scope); | |
412 | ||
413 | finish_fn(&fcx, bcx, sig.output, DebugLoc::None); | |
414 | ||
415 | lloncefn | |
416 | } |