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.
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.
11 use back::link::mangle_internal_name_by_path_and_seq;
13 use middle::mem_categorization::Typer;
17 use trans::cleanup::{CleanupMethods, ScopeId};
19 use trans::datum::{Datum, rvalue_scratch_datum};
20 use trans::datum::{Rvalue, ByValue};
23 use trans::monomorphize::{self, MonoId};
24 use trans::type_of::*;
25 use middle::ty::{self, ClosureTyper};
26 use middle::subst::{Substs};
27 use session::config::FullDebugInfo;
33 fn load_closure_environment<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
34 arg_scope_id: ScopeId,
35 freevars: &[ty::Freevar])
38 let _icx = push_ctxt("closure::load_closure_environment");
40 // Special case for small by-value selfs.
41 let closure_id = ast_util::local_def(bcx.fcx.id);
42 let self_type = self_type_for_closure(bcx.ccx(), closure_id,
43 node_id_type(bcx, closure_id.node));
44 let kind = kind_for_closure(bcx.ccx(), closure_id);
45 let llenv = if kind == ty::FnOnceClosureKind &&
46 !arg_is_indirect(bcx.ccx(), self_type) {
47 let datum = rvalue_scratch_datum(bcx,
50 store_ty(bcx, bcx.fcx.llenv.unwrap(), datum.val, self_type);
53 bcx.fcx.llenv.unwrap()
56 // Store the pointer to closure data in an alloca for debug info because that's what the
57 // llvm.dbg.declare intrinsic expects
58 let env_pointer_alloca = if bcx.sess().opts.debuginfo == FullDebugInfo {
59 let alloc = alloca(bcx, val_ty(llenv), "__debuginfo_env_ptr");
60 Store(bcx, llenv, alloc);
66 for (i, freevar) in freevars.iter().enumerate() {
67 let upvar_id = ty::UpvarId { var_id: freevar.def.local_node_id(),
68 closure_expr_id: closure_id.node };
69 let upvar_capture = bcx.tcx().upvar_capture(upvar_id).unwrap();
70 let mut upvar_ptr = GEPi(bcx, llenv, &[0, i]);
71 let captured_by_ref = match upvar_capture {
72 ty::UpvarCapture::ByValue => false,
73 ty::UpvarCapture::ByRef(..) => {
74 upvar_ptr = Load(bcx, upvar_ptr);
78 let def_id = freevar.def.def_id();
79 bcx.fcx.llupvars.borrow_mut().insert(def_id.node, upvar_ptr);
81 if kind == ty::FnOnceClosureKind && !captured_by_ref {
82 bcx.fcx.schedule_drop_mem(arg_scope_id,
84 node_id_type(bcx, def_id.node))
87 if let Some(env_pointer_alloca) = env_pointer_alloca {
88 debuginfo::create_captured_var_metadata(
101 pub enum ClosureEnv<'a> {
103 Closure(&'a [ty::Freevar]),
106 impl<'a> ClosureEnv<'a> {
107 pub fn load<'blk,'tcx>(self, bcx: Block<'blk, 'tcx>, arg_scope: ScopeId)
111 ClosureEnv::NotClosure => bcx,
112 ClosureEnv::Closure(freevars) => {
113 if freevars.is_empty() {
116 load_closure_environment(bcx, arg_scope, freevars)
123 /// Returns the LLVM function declaration for a closure, creating it if
124 /// necessary. If the ID does not correspond to a closure ID, returns None.
125 pub fn get_or_create_declaration_if_closure<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
126 closure_id: ast::DefId,
127 substs: &Substs<'tcx>)
128 -> Option<Datum<'tcx, Rvalue>> {
129 if !ccx.tcx().closure_kinds.borrow().contains_key(&closure_id) {
134 let function_type = ty::node_id_to_type(ccx.tcx(), closure_id.node);
135 let function_type = monomorphize::apply_param_substs(ccx.tcx(), substs, &function_type);
137 // Normalize type so differences in regions and typedefs don't cause
138 // duplicate declarations
139 let function_type = erase_regions(ccx.tcx(), &function_type);
140 let params = match function_type.sty {
141 ty::ty_closure(_, _, substs) => &substs.types,
144 let mono_id = MonoId {
149 match ccx.closure_vals().borrow().get(&mono_id) {
151 debug!("get_or_create_declaration_if_closure(): found closure");
152 return Some(Datum::new(llfn, function_type, Rvalue::new(ByValue)))
157 let symbol = ccx.tcx().map.with_path(closure_id.node, |path| {
158 mangle_internal_name_by_path_and_seq(path, "closure")
161 let llfn = decl_internal_rust_fn(ccx, function_type, &symbol[]);
163 // set an inline hint for all closures
164 set_inline_hint(llfn);
166 debug!("get_or_create_declaration_if_closure(): inserting new \
167 closure {:?} (type {})",
169 ccx.tn().type_to_string(val_ty(llfn)));
170 ccx.closure_vals().borrow_mut().insert(mono_id, llfn);
172 Some(Datum::new(llfn, function_type, Rvalue::new(ByValue)))
175 pub enum Dest<'a, 'tcx: 'a> {
176 SaveIn(Block<'a, 'tcx>, ValueRef),
177 Ignore(&'a CrateContext<'a, 'tcx>)
180 pub fn trans_closure_expr<'a, 'tcx>(dest: Dest<'a, 'tcx>,
184 param_substs: &'tcx Substs<'tcx>)
185 -> Option<Block<'a, 'tcx>>
187 let ccx = match dest {
188 Dest::SaveIn(bcx, _) => bcx.ccx(),
189 Dest::Ignore(ccx) => ccx
192 let _icx = push_ctxt("closure::trans_closure");
194 debug!("trans_closure()");
196 let closure_id = ast_util::local_def(id);
197 let llfn = get_or_create_declaration_if_closure(
200 param_substs).unwrap();
202 // Get the type of this closure. Use the current `param_substs` as
203 // the closure substitutions. This makes sense because the closure
204 // takes the same set of type arguments as the enclosing fn, and
205 // this function (`trans_closure`) is invoked at the point
206 // of the closure expression.
207 let typer = NormalizingClosureTyper::new(tcx);
208 let function_type = typer.closure_type(closure_id, param_substs);
210 let freevars: Vec<ty::Freevar> =
211 ty::with_freevars(tcx, id, |fv| fv.iter().map(|&fv| fv).collect());
213 let sig = ty::erase_late_bound_regions(tcx, &function_type.sig);
224 ClosureEnv::Closure(&freevars[]));
226 // Don't hoist this to the top of the function. It's perfectly legitimate
227 // to have a zero-size closure (in which case dest will be `Ignore`) and
228 // we must still generate the closure body.
229 let (mut bcx, dest_addr) = match dest {
230 Dest::SaveIn(bcx, p) => (bcx, p),
232 debug!("trans_closure() ignoring result");
237 let repr = adt::represent_type(ccx, node_id_type(bcx, id));
239 // Create the closure.
240 for (i, freevar) in freevars.iter().enumerate() {
241 let datum = expr::trans_local_var(bcx, freevar.def);
242 let upvar_slot_dest = adt::trans_field_ptr(bcx,
247 let upvar_id = ty::UpvarId { var_id: freevar.def.local_node_id(),
248 closure_expr_id: id };
249 match tcx.upvar_capture(upvar_id).unwrap() {
250 ty::UpvarCapture::ByValue => {
251 bcx = datum.store_to(bcx, upvar_slot_dest);
253 ty::UpvarCapture::ByRef(..) => {
254 Store(bcx, datum.to_llref(), upvar_slot_dest);
258 adt::trans_set_discr(bcx, &*repr, dest_addr, 0);