1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//!
//
// Handles translation of callees as well as other call-related
// things. Callees are a superset of normal rust values and sometimes
// have different representations. In particular, top-level fn items
// and methods are represented as just a fn ptr and not a full
// closure.
use back::abi;
use driver::session;
use lib;
use lib::llvm::ValueRef;
use lib::llvm::llvm;
use metadata::csearch;
use middle::trans::base;
use middle::trans::base::*;
use middle::trans::build::*;
use middle::trans::callee;
use middle::trans::closure;
use middle::trans::common;
use middle::trans::common::*;
use middle::trans::datum::*;
use middle::trans::datum::Datum;
use middle::trans::expr;
use middle::trans::glue;
use middle::trans::inline;
use middle::trans::meth;
use middle::trans::monomorphize;
use middle::trans::type_of;
use middle::ty;
use middle::typeck;
use util::common::indenter;
use util::ppaux::Repr;
use syntax::ast;
use syntax::ast_map;
use syntax::visit;
// Represents a (possibly monomorphized) top-level fn item or method
// item. Note that this is just the fn-ptr and is not a Rust closure
// value (which is a pair).
pub struct FnData {
llfn: ValueRef,
}
pub struct MethodData {
llfn: ValueRef,
llself: ValueRef,
self_ty: ty::t,
self_mode: ty::SelfMode,
}
pub enum CalleeData {
Closure(Datum),
Fn(FnData),
Method(MethodData)
}
pub struct Callee {
bcx: block,
data: CalleeData
}
pub fn trans(bcx: block, expr: @ast::expr) -> Callee {
let _icx = bcx.insn_ctxt("trans_callee");
debug!("callee::trans(expr=%s)", expr.repr(bcx.tcx()));
// pick out special kinds of expressions that can be called:
match expr.node {
ast::expr_path(_) => {
return trans_def(bcx, bcx.def(expr.id), expr);
}
_ => {}
}
// any other expressions are closures:
return datum_callee(bcx, expr);
fn datum_callee(bcx: block, expr: @ast::expr) -> Callee {
let DatumBlock {bcx, datum} = expr::trans_to_datum(bcx, expr);
match ty::get(datum.ty).sty {
ty::ty_bare_fn(*) => {
let llval = datum.to_appropriate_llval(bcx);
return Callee {bcx: bcx, data: Fn(FnData {llfn: llval})};
}
ty::ty_closure(*) => {
return Callee {bcx: bcx, data: Closure(datum)};
}
_ => {
bcx.tcx().sess.span_bug(
expr.span,
fmt!("Type of callee is neither bare-fn nor closure: %s",
bcx.ty_to_str(datum.ty)));
}
}
}
fn fn_callee(bcx: block, fd: FnData) -> Callee {
return Callee {bcx: bcx, data: Fn(fd)};
}
fn trans_def(bcx: block, def: ast::def, ref_expr: @ast::expr) -> Callee {
match def {
ast::def_fn(did, _) | ast::def_static_method(did, None, _) => {
fn_callee(bcx, trans_fn_ref(bcx, did, ref_expr.id))
}
ast::def_static_method(impl_did, Some(trait_did), _) => {
fn_callee(bcx, meth::trans_static_method_callee(bcx, impl_did,
trait_did,
ref_expr.id))
}
ast::def_variant(tid, vid) => {
// nullary variants are not callable
assert!(ty::enum_variant_with_id(bcx.tcx(),
tid,
vid).args.len() > 0u);
fn_callee(bcx, trans_fn_ref(bcx, vid, ref_expr.id))
}
ast::def_struct(def_id) => {
fn_callee(bcx, trans_fn_ref(bcx, def_id, ref_expr.id))
}
ast::def_arg(*) |
ast::def_local(*) |
ast::def_binding(*) |
ast::def_upvar(*) |
ast::def_self(*) => {
datum_callee(bcx, ref_expr)
}
ast::def_mod(*) | ast::def_foreign_mod(*) | ast::def_trait(*) |
ast::def_const(*) | ast::def_ty(*) | ast::def_prim_ty(*) |
ast::def_use(*) | ast::def_typaram_binder(*) |
ast::def_region(*) | ast::def_label(*) | ast::def_ty_param(*) |
ast::def_self_ty(*) => {
bcx.tcx().sess.span_bug(
ref_expr.span,
fmt!("Cannot translate def %? \
to a callable thing!", def));
}
}
}
}
pub fn trans_fn_ref_to_callee(bcx: block,
def_id: ast::def_id,
ref_id: ast::node_id) -> Callee {
Callee {bcx: bcx,
data: Fn(trans_fn_ref(bcx, def_id, ref_id))}
}
pub fn trans_fn_ref(bcx: block,
def_id: ast::def_id,
ref_id: ast::node_id) -> FnData {
/*!
*
* Translates a reference (with id `ref_id`) to the fn/method
* with id `def_id` into a function pointer. This may require
* monomorphization or inlining. */
let _icx = bcx.insn_ctxt("trans_fn_ref");
let type_params = node_id_type_params(bcx, ref_id);
let vtables = node_vtables(bcx, ref_id);
debug!("trans_fn_ref(def_id=%s, ref_id=%?, type_params=%s, vtables=%s)",
def_id.repr(bcx.tcx()), ref_id, type_params.repr(bcx.tcx()),
vtables.repr(bcx.tcx()));
trans_fn_ref_with_vtables(bcx, def_id, ref_id, type_params, vtables)
}
pub fn trans_fn_ref_with_vtables_to_callee(
bcx: block,
def_id: ast::def_id,
ref_id: ast::node_id,
type_params: &[ty::t],
vtables: Option<typeck::vtable_res>)
-> Callee {
Callee {bcx: bcx,
data: Fn(trans_fn_ref_with_vtables(bcx, def_id, ref_id,
type_params, vtables))}
}
pub fn trans_fn_ref_with_vtables(
bcx: block, //
def_id: ast::def_id, // def id of fn
ref_id: ast::node_id, // node id of use of fn; may be zero if N/A
type_params: &[ty::t], // values for fn's ty params
vtables: Option<typeck::vtable_res>)
-> FnData {
//!
//
// Translates a reference to a fn/method item, monomorphizing and
// inlining as it goes.
//
// # Parameters
//
// - `bcx`: the current block where the reference to the fn occurs
// - `def_id`: def id of the fn or method item being referenced
// - `ref_id`: node id of the reference to the fn/method, if applicable.
// This parameter may be zero; but, if so, the resulting value may not
// have the right type, so it must be cast before being used.
// - `type_params`: values for each of the fn/method's type parameters
// - `vtables`: values for each bound on each of the type parameters
let _icx = bcx.insn_ctxt("trans_fn_ref_with_vtables");
let ccx = bcx.ccx();
let tcx = ccx.tcx;
debug!("trans_fn_ref_with_vtables(bcx=%s, def_id=%s, ref_id=%?, \
type_params=%s, vtables=%s)",
bcx.to_str(),
def_id.repr(bcx.tcx()),
ref_id,
type_params.repr(bcx.tcx()),
vtables.repr(bcx.tcx()));
assert!(type_params.all(|t| !ty::type_needs_infer(*t)));
// Polytype of the function item (may have type params)
let fn_tpt = ty::lookup_item_type(tcx, def_id);
// Modify the def_id if this is a default method; we want to be
// monomorphizing the trait's code.
let (def_id, opt_impl_did) = match tcx.provided_method_sources.find(&def_id) {
None => (def_id, None),
Some(source) => (source.method_id, Some(source.impl_id))
};
// Check whether this fn has an inlined copy and, if so, redirect
// def_id to the local id of the inlined copy.
let def_id = {
if def_id.crate != ast::local_crate {
let may_translate = opt_impl_did.is_none();
inline::maybe_instantiate_inline(ccx, def_id, may_translate)
} else {
def_id
}
};
// We must monomorphise if the fn has type parameters, is a rust
// intrinsic, or is a default method. In particular, if we see an
// intrinsic that is inlined from a different crate, we want to reemit the
// intrinsic instead of trying to call it in the other crate.
let must_monomorphise;
if type_params.len() > 0 || opt_impl_did.is_some() {
must_monomorphise = true;
} else if def_id.crate == ast::local_crate {
let map_node = session::expect(
ccx.sess,
ccx.tcx.items.find(&def_id.node),
|| fmt!("local item should be in ast map"));
match *map_node {
ast_map::node_foreign_item(_, abis, _, _) => {
must_monomorphise = abis.is_intrinsic()
}
_ => {
must_monomorphise = false;
}
}
} else {
must_monomorphise = false;
}
// Create a monomorphic verison of generic functions
if must_monomorphise {
// Should be either intra-crate or inlined.
assert!(def_id.crate == ast::local_crate);
let mut (val, must_cast) =
monomorphize::monomorphic_fn(ccx, def_id, type_params,
vtables, opt_impl_did, Some(ref_id));
if must_cast && ref_id != 0 {
// Monotype of the REFERENCE to the function (type params
// are subst'd)
let ref_ty = common::node_id_type(bcx, ref_id);
val = PointerCast(
bcx, val, T_ptr(type_of::type_of_fn_from_ty(ccx, ref_ty)));
}
return FnData {llfn: val};
}
// Find the actual function pointer.
let val = {
if def_id.crate == ast::local_crate {
// Internal reference.
get_item_val(ccx, def_id.node)
} else {
// External reference.
trans_external_path(ccx, def_id, fn_tpt.ty)
}
};
return FnData {llfn: val};
}
// ______________________________________________________________________
// Translating calls
pub fn trans_call(in_cx: block,
call_ex: @ast::expr,
f: @ast::expr,
args: CallArgs,
id: ast::node_id,
dest: expr::Dest)
-> block {
let _icx = in_cx.insn_ctxt("trans_call");
trans_call_inner(in_cx,
call_ex.info(),
expr_ty(in_cx, f),
node_id_type(in_cx, id),
|cx| trans(cx, f),
args,
dest,
DontAutorefArg)
}
pub fn trans_method_call(in_cx: block,
call_ex: @ast::expr,
rcvr: @ast::expr,
args: CallArgs,
dest: expr::Dest)
-> block {
let _icx = in_cx.insn_ctxt("trans_method_call");
debug!("trans_method_call(call_ex=%s, rcvr=%s)",
call_ex.repr(in_cx.tcx()),
rcvr.repr(in_cx.tcx()));
trans_call_inner(
in_cx,
call_ex.info(),
node_id_type(in_cx, call_ex.callee_id),
expr_ty(in_cx, call_ex),
|cx| {
match cx.ccx().maps.method_map.find(&call_ex.id) {
Some(origin) => {
debug!("origin for %s: %s",
call_ex.repr(in_cx.tcx()),
origin.repr(in_cx.tcx()));
// FIXME(#5562): removing this copy causes a segfault
// before stage2
let origin = /*bad*/ copy *origin;
meth::trans_method_callee(cx,
call_ex.callee_id,
rcvr,
origin)
}
None => {
cx.tcx().sess.span_bug(call_ex.span, "method call expr wasn't in method map")
}
}
},
args,
dest,
DontAutorefArg)
}
pub fn trans_lang_call(bcx: block,
did: ast::def_id,
args: &[ValueRef],
dest: expr::Dest)
-> block {
let fty = if did.crate == ast::local_crate {
ty::node_id_to_type(bcx.ccx().tcx, did.node)
} else {
csearch::get_type(bcx.ccx().tcx, did).ty
};
let rty = ty::ty_fn_ret(fty);
callee::trans_call_inner(bcx,
None,
fty,
rty,
|bcx| {
trans_fn_ref_with_vtables_to_callee(bcx,
did,
0,
~[],
None)
},
ArgVals(args),
dest,
DontAutorefArg)
}
pub fn trans_lang_call_with_type_params(bcx: block,
did: ast::def_id,
args: &[ValueRef],
type_params: &[ty::t],
dest: expr::Dest)
-> block {
let fty;
if did.crate == ast::local_crate {
fty = ty::node_id_to_type(bcx.tcx(), did.node);
} else {
fty = csearch::get_type(bcx.tcx(), did).ty;
}
let rty = ty::ty_fn_ret(fty);
return callee::trans_call_inner(
bcx, None, fty, rty,
|bcx| {
let callee =
trans_fn_ref_with_vtables_to_callee(bcx, did, 0,
type_params,
None);
let new_llval;
match callee.data {
Fn(fn_data) => {
let substituted = ty::subst_tps(callee.bcx.tcx(),
type_params,
None,
fty);
let llfnty = type_of::type_of(callee.bcx.ccx(),
substituted);
new_llval = PointerCast(callee.bcx, fn_data.llfn, llfnty);
}
_ => fail!()
}
Callee { bcx: callee.bcx, data: Fn(FnData { llfn: new_llval }) }
},
ArgVals(args), dest, DontAutorefArg);
}
pub fn body_contains_ret(body: &ast::blk) -> bool {
let cx = @mut false;
visit::visit_block(body, cx, visit::mk_vt(@visit::Visitor {
visit_item: |_i, _cx, _v| { },
visit_expr: |e: @ast::expr, cx: @mut bool, v| {
if !*cx {
match e.node {
ast::expr_ret(_) => *cx = true,
_ => visit::visit_expr(e, cx, v),
}
}
},
..*visit::default_visitor()
}));
*cx
}
// See [Note-arg-mode]
pub fn trans_call_inner(in_cx: block,
call_info: Option<NodeInfo>,
fn_expr_ty: ty::t,
ret_ty: ty::t,
get_callee: &fn(block) -> Callee,
args: CallArgs,
dest: expr::Dest,
autoref_arg: AutorefArg)
-> block {
do base::with_scope(in_cx, call_info, ~"call") |cx| {
let ret_in_loop = match args {
ArgExprs(args) => {
args.len() > 0u && match vec::last(args).node {
ast::expr_loop_body(@ast::expr {
node: ast::expr_fn_block(_, ref body),
_
}) => body_contains_ret(body),
_ => false
}
}
_ => false
};
let callee = get_callee(cx);
let mut bcx = callee.bcx;
let ccx = cx.ccx();
let ret_flag = if ret_in_loop {
let flag = alloca(bcx, T_bool());
Store(bcx, C_bool(false), flag);
Some(flag)
} else {
None
};
let (llfn, llenv) = unsafe {
match callee.data {
Fn(d) => {
(d.llfn, llvm::LLVMGetUndef(T_opaque_box_ptr(ccx)))
}
Method(d) => {
// Weird but true: we pass self in the *environment* slot!
let llself = PointerCast(bcx,
d.llself,
T_opaque_box_ptr(ccx));
(d.llfn, llself)
}
Closure(d) => {
// Closures are represented as (llfn, llclosure) pair:
// load the requisite values out.
let pair = d.to_ref_llval(bcx);
let llfn = GEPi(bcx, pair, [0u, abi::fn_field_code]);
let llfn = Load(bcx, llfn);
let llenv = GEPi(bcx, pair, [0u, abi::fn_field_box]);
let llenv = Load(bcx, llenv);
(llfn, llenv)
}
}
};
let llretslot = trans_ret_slot(bcx, fn_expr_ty, dest);
let mut llargs = ~[];
if ty::type_is_immediate(ret_ty) {
unsafe {
llargs.push(llvm::LLVMGetUndef(T_ptr(T_i8())));
}
} else {
llargs.push(llretslot);
}
llargs.push(llenv);
bcx = trans_args(bcx, args, fn_expr_ty,
ret_flag, autoref_arg, &mut llargs);
// Now that the arguments have finished evaluating, we need to revoke
// the cleanup for the self argument, if it exists
match callee.data {
Method(d) if d.self_mode == ty::ByCopy => {
revoke_clean(bcx, d.llself);
}
_ => {}
}
// Uncomment this to debug calls.
/*
io::println(fmt!("calling: %s", bcx.val_str(llfn)));
for llargs.each |llarg| {
io::println(fmt!("arg: %s", bcx.val_str(*llarg)));
}
io::println("---");
*/
// If the block is terminated, then one or more of the args
// has type _|_. Since that means it diverges, the code for
// the call itself is unreachable.
let (llresult, new_bcx) = base::invoke(bcx, llfn, llargs);
bcx = new_bcx;
match dest {
expr::Ignore => {
// drop the value if it is not being saved.
unsafe {
if llvm::LLVMIsUndef(llretslot) != lib::llvm::True {
if ty::type_is_immediate(ret_ty) {
let llscratchptr = alloc_ty(bcx, ret_ty);
Store(bcx, llresult, llscratchptr);
bcx = glue::drop_ty(bcx, llscratchptr, ret_ty);
} else {
bcx = glue::drop_ty(bcx, llretslot, ret_ty);
}
}
}
}
expr::SaveIn(lldest) => {
// If this is an immediate, store into the result location.
// (If this was not an immediate, the result will already be
// directly written into the output slot.)
if ty::type_is_immediate(ret_ty) {
Store(bcx, llresult, lldest);
}
}
}
if ty::type_is_bot(ret_ty) {
Unreachable(bcx);
} else if ret_in_loop {
let ret_flag_result = bool_to_i1(bcx, Load(bcx, ret_flag.get()));
bcx = do with_cond(bcx, ret_flag_result) |bcx| {
for (copy bcx.fcx.loop_ret).each |&(flagptr, _)| {
Store(bcx, C_bool(true), flagptr);
Store(bcx, C_bool(false), bcx.fcx.llretptr.get());
}
base::cleanup_and_leave(bcx, None, Some(bcx.fcx.llreturn));
Unreachable(bcx);
bcx
}
}
bcx
}
}
pub enum CallArgs<'self> {
ArgExprs(&'self [@ast::expr]),
ArgVals(&'self [ValueRef])
}
pub fn trans_ret_slot(bcx: block, fn_ty: ty::t, dest: expr::Dest)
-> ValueRef {
let retty = ty::ty_fn_ret(fn_ty);
match dest {
expr::SaveIn(dst) => dst,
expr::Ignore => {
if ty::type_is_nil(retty) {
unsafe {
llvm::LLVMGetUndef(T_ptr(T_nil()))
}
} else {
alloc_ty(bcx, retty)
}
}
}
}
pub fn trans_args(cx: block,
args: CallArgs,
fn_ty: ty::t,
ret_flag: Option<ValueRef>,
autoref_arg: AutorefArg,
llargs: &mut ~[ValueRef]) -> block
{
let _icx = cx.insn_ctxt("trans_args");
let mut temp_cleanups = ~[];
let arg_tys = ty::ty_fn_args(fn_ty);
let mut bcx = cx;
// First we figure out the caller's view of the types of the arguments.
// This will be needed if this is a generic call, because the callee has
// to cast her view of the arguments to the caller's view.
match args {
ArgExprs(arg_exprs) => {
let last = arg_exprs.len() - 1u;
for vec::eachi(arg_exprs) |i, arg_expr| {
let arg_val = unpack_result!(bcx, {
trans_arg_expr(bcx,
arg_tys[i],
ty::ByCopy,
*arg_expr,
&mut temp_cleanups,
if i == last { ret_flag } else { None },
autoref_arg)
});
llargs.push(arg_val);
}
}
ArgVals(vs) => {
llargs.push_all(vs);
}
}
// now that all arguments have been successfully built, we can revoke any
// temporary cleanups, as they are only needed if argument construction
// should fail (for example, cleanup of copy mode args).
for vec::each(temp_cleanups) |c| {
revoke_clean(bcx, *c)
}
return bcx;
}
pub enum AutorefArg {
DontAutorefArg,
DoAutorefArg
}
// temp_cleanups: cleanups that should run only if failure occurs before the
// call takes place:
pub fn trans_arg_expr(bcx: block,
formal_ty: ty::arg,
self_mode: ty::SelfMode,
arg_expr: @ast::expr,
temp_cleanups: &mut ~[ValueRef],
ret_flag: Option<ValueRef>,
autoref_arg: AutorefArg) -> Result {
let _icx = bcx.insn_ctxt("trans_arg_expr");
let ccx = bcx.ccx();
debug!("trans_arg_expr(formal_ty=(%s), self_mode=%?, arg_expr=%s, \
ret_flag=%?)",
formal_ty.ty.repr(bcx.tcx()),
self_mode,
arg_expr.repr(bcx.tcx()),
ret_flag.map(|v| bcx.val_str(*v)));
let _indenter = indenter();
// translate the arg expr to a datum
let arg_datumblock = match ret_flag {
None => expr::trans_to_datum(bcx, arg_expr),
// If there is a ret_flag, this *must* be a loop body
Some(_) => {
match arg_expr.node {
ast::expr_loop_body(
blk @ @ast::expr {
node: ast::expr_fn_block(ref decl, ref body),
_
}) => {
let scratch_ty = expr_ty(bcx, arg_expr);
let scratch = alloc_ty(bcx, scratch_ty);
let arg_ty = expr_ty(bcx, arg_expr);
let sigil = ty::ty_closure_sigil(arg_ty);
let bcx = closure::trans_expr_fn(
bcx, sigil, decl, body, arg_expr.id,
blk.id, Some(ret_flag), expr::SaveIn(scratch));
DatumBlock {bcx: bcx,
datum: Datum {val: scratch,
ty: scratch_ty,
mode: ByRef,
source: RevokeClean}}
}
_ => {
bcx.sess().impossible_case(
arg_expr.span, ~"ret_flag with non-loop-\
body expr");
}
}
}
};
let mut arg_datum = arg_datumblock.datum;
let bcx = arg_datumblock.bcx;
debug!(" arg datum: %s", arg_datum.to_str(bcx.ccx()));
let mut val;
if ty::type_is_bot(arg_datum.ty) {
// For values of type _|_, we generate an
// "undef" value, as such a value should never
// be inspected. It's important for the value
// to have type lldestty (the callee's expected type).
let llformal_ty = type_of::type_of(ccx, formal_ty.ty);
unsafe {
val = llvm::LLVMGetUndef(llformal_ty);
}
} else {
// FIXME(#3548) use the adjustments table
match autoref_arg {
DoAutorefArg => {
assert!(!
bcx.ccx().maps.moves_map.contains(&arg_expr.id));
val = arg_datum.to_ref_llval(bcx);
}
DontAutorefArg => {
match self_mode {
ty::ByRef => {
// This assertion should really be valid, but because
// the explicit self code currently passes by-ref, it
// does not hold.
//
//assert !bcx.ccx().maps.moves_map.contains_key(
// &arg_expr.id);
debug!("by ref arg with type %s",
bcx.ty_to_str(arg_datum.ty));
val = arg_datum.to_ref_llval(bcx);
}
ty::ByCopy => {
debug!("by copy arg with type %s, storing to scratch",
bcx.ty_to_str(arg_datum.ty));
let scratch = scratch_datum(bcx, arg_datum.ty, false);
arg_datum.store_to_datum(bcx,
arg_expr.id,
INIT,
scratch);
// Technically, ownership of val passes to the callee.
// However, we must cleanup should we fail before the
// callee is actually invoked.
scratch.add_clean(bcx);
temp_cleanups.push(scratch.val);
match arg_datum.appropriate_mode() {
ByValue => val = Load(bcx, scratch.val),
ByRef => val = scratch.val,
}
}
}
}
}
if formal_ty.ty != arg_datum.ty {
// this could happen due to e.g. subtyping
let llformal_ty = type_of::type_of_explicit_arg(ccx, &formal_ty);
let llformal_ty = match self_mode {
ty::ByRef => T_ptr(llformal_ty),
ty::ByCopy => llformal_ty,
};
debug!("casting actual type (%s) to match formal (%s)",
bcx.val_str(val), bcx.llty_str(llformal_ty));
val = PointerCast(bcx, val, llformal_ty);
}
}
debug!("--- trans_arg_expr passing %s", val_str(bcx.ccx().tn, val));
return rslt(bcx, val);
}
|