]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/trans/callee.rs
Merge tag 'upstream/1.5.0+dfsg1'
[rustc.git] / src / librustc_trans / trans / callee.rs
1 // Copyright 2012 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 //! Handles translation of callees as well as other call-related
12 //! things. Callees are a superset of normal rust values and sometimes
13 //! have different representations. In particular, top-level fn items
14 //! and methods are represented as just a fn ptr and not a full
15 //! closure.
16
17 pub use self::AutorefArg::*;
18 pub use self::CalleeData::*;
19 pub use self::CallArgs::*;
20
21 use arena::TypedArena;
22 use back::link;
23 use session;
24 use llvm::{self, ValueRef, get_params};
25 use metadata::cstore::LOCAL_CRATE;
26 use middle::def;
27 use middle::def_id::DefId;
28 use middle::infer::normalize_associated_type;
29 use middle::subst;
30 use middle::subst::{Substs};
31 use rustc::front::map as hir_map;
32 use trans::adt;
33 use trans::base;
34 use trans::base::*;
35 use trans::build::*;
36 use trans::callee;
37 use trans::cleanup;
38 use trans::cleanup::CleanupMethods;
39 use trans::common::{self, Block, Result, NodeIdAndSpan, ExprId, CrateContext,
40 ExprOrMethodCall, FunctionContext, MethodCallKey};
41 use trans::consts;
42 use trans::datum::*;
43 use trans::debuginfo::{DebugLoc, ToDebugLoc};
44 use trans::declare;
45 use trans::expr;
46 use trans::glue;
47 use trans::inline;
48 use trans::foreign;
49 use trans::intrinsic;
50 use trans::meth;
51 use trans::monomorphize;
52 use trans::type_::Type;
53 use trans::type_of;
54 use middle::ty::{self, Ty, HasTypeFlags, RegionEscape};
55 use middle::ty::MethodCall;
56 use rustc_front::hir;
57
58 use syntax::abi as synabi;
59 use syntax::ast;
60 use syntax::ptr::P;
61
62 #[derive(Copy, Clone)]
63 pub struct MethodData {
64 pub llfn: ValueRef,
65 pub llself: ValueRef,
66 }
67
68 pub enum CalleeData<'tcx> {
69 // Constructor for enum variant/tuple-like-struct
70 // i.e. Some, Ok
71 NamedTupleConstructor(ty::Disr),
72
73 // Represents a (possibly monomorphized) top-level fn item or method
74 // item. Note that this is just the fn-ptr and is not a Rust closure
75 // value (which is a pair).
76 Fn(/* llfn */ ValueRef),
77
78 Intrinsic(ast::NodeId, subst::Substs<'tcx>),
79
80 TraitItem(MethodData)
81 }
82
83 pub struct Callee<'blk, 'tcx: 'blk> {
84 pub bcx: Block<'blk, 'tcx>,
85 pub data: CalleeData<'tcx>,
86 pub ty: Ty<'tcx>
87 }
88
89 fn trans<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr: &hir::Expr)
90 -> Callee<'blk, 'tcx> {
91 let _icx = push_ctxt("trans_callee");
92 debug!("callee::trans(expr={:?})", expr);
93
94 // pick out special kinds of expressions that can be called:
95 match expr.node {
96 hir::ExprPath(..) => {
97 return trans_def(bcx, bcx.def(expr.id), expr);
98 }
99 _ => {}
100 }
101
102 // any other expressions are closures:
103 return datum_callee(bcx, expr);
104
105 fn datum_callee<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr: &hir::Expr)
106 -> Callee<'blk, 'tcx> {
107 let DatumBlock { bcx, datum, .. } = expr::trans(bcx, expr);
108 match datum.ty.sty {
109 ty::TyBareFn(..) => {
110 Callee {
111 bcx: bcx,
112 ty: datum.ty,
113 data: Fn(datum.to_llscalarish(bcx))
114 }
115 }
116 _ => {
117 bcx.tcx().sess.span_bug(
118 expr.span,
119 &format!("type of callee is neither bare-fn nor closure: {}",
120 datum.ty));
121 }
122 }
123 }
124
125 fn fn_callee<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, datum: Datum<'tcx, Rvalue>)
126 -> Callee<'blk, 'tcx> {
127 Callee {
128 bcx: bcx,
129 data: Fn(datum.val),
130 ty: datum.ty
131 }
132 }
133
134 fn trans_def<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
135 def: def::Def,
136 ref_expr: &hir::Expr)
137 -> Callee<'blk, 'tcx> {
138 debug!("trans_def(def={:?}, ref_expr={:?})", def, ref_expr);
139 let expr_ty = common::node_id_type(bcx, ref_expr.id);
140 match def {
141 def::DefFn(did, _) if {
142 let maybe_def_id = inline::get_local_instance(bcx.ccx(), did);
143 let maybe_ast_node = maybe_def_id.and_then(|def_id| {
144 let node_id = bcx.tcx().map.as_local_node_id(def_id).unwrap();
145 bcx.tcx().map.find(node_id)
146 });
147 match maybe_ast_node {
148 Some(hir_map::NodeStructCtor(_)) => true,
149 _ => false
150 }
151 } => {
152 Callee {
153 bcx: bcx,
154 data: NamedTupleConstructor(0),
155 ty: expr_ty
156 }
157 }
158 def::DefFn(did, _) if match expr_ty.sty {
159 ty::TyBareFn(_, ref f) => f.abi == synabi::RustIntrinsic ||
160 f.abi == synabi::PlatformIntrinsic,
161 _ => false
162 } => {
163 let substs = common::node_id_substs(bcx.ccx(),
164 ExprId(ref_expr.id),
165 bcx.fcx.param_substs);
166 let def_id = inline::maybe_instantiate_inline(bcx.ccx(), did);
167 let node_id = bcx.tcx().map.as_local_node_id(def_id).unwrap();
168 Callee { bcx: bcx, data: Intrinsic(node_id, substs), ty: expr_ty }
169 }
170 def::DefFn(did, _) => {
171 fn_callee(bcx, trans_fn_ref(bcx.ccx(), did, ExprId(ref_expr.id),
172 bcx.fcx.param_substs))
173 }
174 def::DefMethod(meth_did) => {
175 let method_item = bcx.tcx().impl_or_trait_item(meth_did);
176 let fn_datum = match method_item.container() {
177 ty::ImplContainer(_) => {
178 trans_fn_ref(bcx.ccx(), meth_did,
179 ExprId(ref_expr.id),
180 bcx.fcx.param_substs)
181 }
182 ty::TraitContainer(trait_did) => {
183 meth::trans_static_method_callee(bcx.ccx(),
184 meth_did,
185 trait_did,
186 ref_expr.id,
187 bcx.fcx.param_substs)
188 }
189 };
190 fn_callee(bcx, fn_datum)
191 }
192 def::DefVariant(tid, vid, _) => {
193 let vinfo = bcx.tcx().lookup_adt_def(tid).variant_with_id(vid);
194 assert_eq!(vinfo.kind(), ty::VariantKind::Tuple);
195
196 Callee {
197 bcx: bcx,
198 data: NamedTupleConstructor(vinfo.disr_val),
199 ty: expr_ty
200 }
201 }
202 def::DefStruct(_) => {
203 Callee {
204 bcx: bcx,
205 data: NamedTupleConstructor(0),
206 ty: expr_ty
207 }
208 }
209 def::DefStatic(..) |
210 def::DefConst(..) |
211 def::DefAssociatedConst(..) |
212 def::DefLocal(..) |
213 def::DefUpvar(..) => {
214 datum_callee(bcx, ref_expr)
215 }
216 def::DefMod(..) | def::DefForeignMod(..) | def::DefTrait(..) |
217 def::DefTy(..) | def::DefPrimTy(..) | def::DefAssociatedTy(..) |
218 def::DefUse(..) | def::DefLabel(..) | def::DefTyParam(..) |
219 def::DefSelfTy(..) => {
220 bcx.tcx().sess.span_bug(
221 ref_expr.span,
222 &format!("cannot translate def {:?} \
223 to a callable thing!", def));
224 }
225 }
226 }
227 }
228
229 /// Translates a reference (with id `ref_id`) to the fn/method with id `def_id` into a function
230 /// pointer. This may require monomorphization or inlining.
231 pub fn trans_fn_ref<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
232 def_id: DefId,
233 node: ExprOrMethodCall,
234 param_substs: &'tcx subst::Substs<'tcx>)
235 -> Datum<'tcx, Rvalue> {
236 let _icx = push_ctxt("trans_fn_ref");
237
238 let substs = common::node_id_substs(ccx, node, param_substs);
239 debug!("trans_fn_ref(def_id={:?}, node={:?}, substs={:?})",
240 def_id,
241 node,
242 substs);
243 trans_fn_ref_with_substs(ccx, def_id, node, param_substs, substs)
244 }
245
246 /// Translates an adapter that implements the `Fn` trait for a fn
247 /// pointer. This is basically the equivalent of something like:
248 ///
249 /// ```
250 /// impl<'a> Fn(&'a int) -> &'a int for fn(&int) -> &int {
251 /// extern "rust-abi" fn call(&self, args: (&'a int,)) -> &'a int {
252 /// (*self)(args.0)
253 /// }
254 /// }
255 /// ```
256 ///
257 /// but for the bare function type given.
258 pub fn trans_fn_pointer_shim<'a, 'tcx>(
259 ccx: &'a CrateContext<'a, 'tcx>,
260 closure_kind: ty::ClosureKind,
261 bare_fn_ty: Ty<'tcx>)
262 -> ValueRef
263 {
264 let _icx = push_ctxt("trans_fn_pointer_shim");
265 let tcx = ccx.tcx();
266
267 // Normalize the type for better caching.
268 let bare_fn_ty = tcx.erase_regions(&bare_fn_ty);
269
270 // If this is an impl of `Fn` or `FnMut` trait, the receiver is `&self`.
271 let is_by_ref = match closure_kind {
272 ty::FnClosureKind | ty::FnMutClosureKind => true,
273 ty::FnOnceClosureKind => false,
274 };
275 let bare_fn_ty_maybe_ref = if is_by_ref {
276 tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic), bare_fn_ty)
277 } else {
278 bare_fn_ty
279 };
280
281 // Check if we already trans'd this shim.
282 match ccx.fn_pointer_shims().borrow().get(&bare_fn_ty_maybe_ref) {
283 Some(&llval) => { return llval; }
284 None => { }
285 }
286
287 debug!("trans_fn_pointer_shim(bare_fn_ty={:?})",
288 bare_fn_ty);
289
290 // Construct the "tuply" version of `bare_fn_ty`. It takes two arguments: `self`,
291 // which is the fn pointer, and `args`, which is the arguments tuple.
292 let (opt_def_id, sig) =
293 match bare_fn_ty.sty {
294 ty::TyBareFn(opt_def_id,
295 &ty::BareFnTy { unsafety: hir::Unsafety::Normal,
296 abi: synabi::Rust,
297 ref sig }) => {
298 (opt_def_id, sig)
299 }
300
301 _ => {
302 tcx.sess.bug(&format!("trans_fn_pointer_shim invoked on invalid type: {}",
303 bare_fn_ty));
304 }
305 };
306 let sig = tcx.erase_late_bound_regions(sig);
307 let tuple_input_ty = tcx.mk_tup(sig.inputs.to_vec());
308 let tuple_fn_ty = tcx.mk_fn(opt_def_id,
309 tcx.mk_bare_fn(ty::BareFnTy {
310 unsafety: hir::Unsafety::Normal,
311 abi: synabi::RustCall,
312 sig: ty::Binder(ty::FnSig {
313 inputs: vec![bare_fn_ty_maybe_ref,
314 tuple_input_ty],
315 output: sig.output,
316 variadic: false
317 })}));
318 debug!("tuple_fn_ty: {:?}", tuple_fn_ty);
319
320 //
321 let function_name = link::mangle_internal_name_by_type_and_seq(ccx, bare_fn_ty,
322 "fn_pointer_shim");
323 let llfn = declare::declare_internal_rust_fn(ccx, &function_name[..], tuple_fn_ty);
324
325 //
326 let empty_substs = tcx.mk_substs(Substs::trans_empty());
327 let (block_arena, fcx): (TypedArena<_>, FunctionContext);
328 block_arena = TypedArena::new();
329 fcx = new_fn_ctxt(ccx,
330 llfn,
331 ast::DUMMY_NODE_ID,
332 false,
333 sig.output,
334 empty_substs,
335 None,
336 &block_arena);
337 let mut bcx = init_function(&fcx, false, sig.output);
338
339 let llargs = get_params(fcx.llfn);
340
341 let self_idx = fcx.arg_offset();
342 // the first argument (`self`) will be ptr to the fn pointer
343 let llfnpointer = if is_by_ref {
344 Load(bcx, llargs[self_idx])
345 } else {
346 llargs[self_idx]
347 };
348
349 assert!(!fcx.needs_ret_allocas);
350
351 let dest = fcx.llretslotptr.get().map(|_|
352 expr::SaveIn(fcx.get_ret_slot(bcx, sig.output, "ret_slot"))
353 );
354
355 bcx = trans_call_inner(bcx, DebugLoc::None, |bcx, _| {
356 Callee {
357 bcx: bcx,
358 data: Fn(llfnpointer),
359 ty: bare_fn_ty
360 }
361 }, ArgVals(&llargs[(self_idx + 1)..]), dest).bcx;
362
363 finish_fn(&fcx, bcx, sig.output, DebugLoc::None);
364
365 ccx.fn_pointer_shims().borrow_mut().insert(bare_fn_ty_maybe_ref, llfn);
366
367 llfn
368 }
369
370 /// Translates a reference to a fn/method item, monomorphizing and
371 /// inlining as it goes.
372 ///
373 /// # Parameters
374 ///
375 /// - `ccx`: the crate context
376 /// - `def_id`: def id of the fn or method item being referenced
377 /// - `node`: node id of the reference to the fn/method, if applicable.
378 /// This parameter may be zero; but, if so, the resulting value may not
379 /// have the right type, so it must be cast before being used.
380 /// - `param_substs`: if the `node` is in a polymorphic function, these
381 /// are the substitutions required to monomorphize its type
382 /// - `substs`: values for each of the fn/method's parameters
383 pub fn trans_fn_ref_with_substs<'a, 'tcx>(
384 ccx: &CrateContext<'a, 'tcx>,
385 def_id: DefId,
386 node: ExprOrMethodCall,
387 param_substs: &'tcx subst::Substs<'tcx>,
388 substs: subst::Substs<'tcx>)
389 -> Datum<'tcx, Rvalue>
390 {
391 let _icx = push_ctxt("trans_fn_ref_with_substs");
392 let tcx = ccx.tcx();
393
394 debug!("trans_fn_ref_with_substs(def_id={:?}, node={:?}, \
395 param_substs={:?}, substs={:?})",
396 def_id,
397 node,
398 param_substs,
399 substs);
400
401 assert!(!substs.types.needs_infer());
402 assert!(!substs.types.has_escaping_regions());
403 let substs = substs.erase_regions();
404
405 // Check whether this fn has an inlined copy and, if so, redirect
406 // def_id to the local id of the inlined copy.
407 let def_id = inline::maybe_instantiate_inline(ccx, def_id);
408
409 fn is_named_tuple_constructor(tcx: &ty::ctxt, def_id: DefId) -> bool {
410 let node_id = match tcx.map.as_local_node_id(def_id) {
411 Some(n) => n,
412 None => { return false; }
413 };
414 let map_node = session::expect(
415 &tcx.sess,
416 tcx.map.find(node_id),
417 || "local item should be in ast map".to_string());
418
419 match map_node {
420 hir_map::NodeVariant(v) => {
421 v.node.data.is_tuple()
422 }
423 hir_map::NodeStructCtor(_) => true,
424 _ => false
425 }
426 }
427 let must_monomorphise =
428 !substs.types.is_empty() || is_named_tuple_constructor(tcx, def_id);
429
430 debug!("trans_fn_ref_with_substs({:?}) must_monomorphise: {}",
431 def_id, must_monomorphise);
432
433 // Create a monomorphic version of generic functions
434 if must_monomorphise {
435 // Should be either intra-crate or inlined.
436 assert_eq!(def_id.krate, LOCAL_CRATE);
437
438 let opt_ref_id = match node {
439 ExprId(id) => if id != 0 { Some(id) } else { None },
440 MethodCallKey(_) => None,
441 };
442
443 let substs = tcx.mk_substs(substs);
444 let (val, fn_ty, must_cast) =
445 monomorphize::monomorphic_fn(ccx, def_id, substs, opt_ref_id);
446 if must_cast && node != ExprId(0) {
447 // Monotype of the REFERENCE to the function (type params
448 // are subst'd)
449 let ref_ty = match node {
450 ExprId(id) => tcx.node_id_to_type(id),
451 MethodCallKey(method_call) => {
452 tcx.tables.borrow().method_map[&method_call].ty
453 }
454 };
455 let ref_ty = monomorphize::apply_param_substs(tcx,
456 param_substs,
457 &ref_ty);
458 let llptrty = type_of::type_of_fn_from_ty(ccx, ref_ty).ptr_to();
459 if llptrty != common::val_ty(val) {
460 let val = consts::ptrcast(val, llptrty);
461 return Datum::new(val, ref_ty, Rvalue::new(ByValue));
462 }
463 }
464 return Datum::new(val, fn_ty, Rvalue::new(ByValue));
465 }
466
467 // Type scheme of the function item (may have type params)
468 let fn_type_scheme = tcx.lookup_item_type(def_id);
469 let fn_type = normalize_associated_type(tcx, &fn_type_scheme.ty);
470
471 // Find the actual function pointer.
472 let mut val = {
473 if let Some(node_id) = ccx.tcx().map.as_local_node_id(def_id) {
474 // Internal reference.
475 get_item_val(ccx, node_id)
476 } else {
477 // External reference.
478 trans_external_path(ccx, def_id, fn_type)
479 }
480 };
481
482 // This is subtle and surprising, but sometimes we have to bitcast
483 // the resulting fn pointer. The reason has to do with external
484 // functions. If you have two crates that both bind the same C
485 // library, they may not use precisely the same types: for
486 // example, they will probably each declare their own structs,
487 // which are distinct types from LLVM's point of view (nominal
488 // types).
489 //
490 // Now, if those two crates are linked into an application, and
491 // they contain inlined code, you can wind up with a situation
492 // where both of those functions wind up being loaded into this
493 // application simultaneously. In that case, the same function
494 // (from LLVM's point of view) requires two types. But of course
495 // LLVM won't allow one function to have two types.
496 //
497 // What we currently do, therefore, is declare the function with
498 // one of the two types (whichever happens to come first) and then
499 // bitcast as needed when the function is referenced to make sure
500 // it has the type we expect.
501 //
502 // This can occur on either a crate-local or crate-external
503 // reference. It also occurs when testing libcore and in some
504 // other weird situations. Annoying.
505 let llty = type_of::type_of_fn_from_ty(ccx, fn_type);
506 let llptrty = llty.ptr_to();
507 if common::val_ty(val) != llptrty {
508 debug!("trans_fn_ref_with_substs(): casting pointer!");
509 val = consts::ptrcast(val, llptrty);
510 } else {
511 debug!("trans_fn_ref_with_substs(): not casting pointer!");
512 }
513
514 Datum::new(val, fn_type, Rvalue::new(ByValue))
515 }
516
517 // ______________________________________________________________________
518 // Translating calls
519
520 pub fn trans_call<'a, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
521 call_expr: &hir::Expr,
522 f: &hir::Expr,
523 args: CallArgs<'a, 'tcx>,
524 dest: expr::Dest)
525 -> Block<'blk, 'tcx> {
526 let _icx = push_ctxt("trans_call");
527 trans_call_inner(bcx,
528 call_expr.debug_loc(),
529 |bcx, _| trans(bcx, f),
530 args,
531 Some(dest)).bcx
532 }
533
534 pub fn trans_method_call<'a, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
535 call_expr: &hir::Expr,
536 rcvr: &hir::Expr,
537 args: CallArgs<'a, 'tcx>,
538 dest: expr::Dest)
539 -> Block<'blk, 'tcx> {
540 let _icx = push_ctxt("trans_method_call");
541 debug!("trans_method_call(call_expr={:?})", call_expr);
542 let method_call = MethodCall::expr(call_expr.id);
543 trans_call_inner(
544 bcx,
545 call_expr.debug_loc(),
546 |cx, arg_cleanup_scope| {
547 meth::trans_method_callee(cx, method_call, Some(rcvr), arg_cleanup_scope)
548 },
549 args,
550 Some(dest)).bcx
551 }
552
553 pub fn trans_lang_call<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
554 did: DefId,
555 args: &[ValueRef],
556 dest: Option<expr::Dest>,
557 debug_loc: DebugLoc)
558 -> Result<'blk, 'tcx> {
559 callee::trans_call_inner(bcx, debug_loc, |bcx, _| {
560 let datum = trans_fn_ref_with_substs(bcx.ccx(),
561 did,
562 ExprId(0),
563 bcx.fcx.param_substs,
564 subst::Substs::trans_empty());
565 Callee {
566 bcx: bcx,
567 data: Fn(datum.val),
568 ty: datum.ty
569 }
570 }, ArgVals(args), dest)
571 }
572
573 /// This behemoth of a function translates function calls. Unfortunately, in
574 /// order to generate more efficient LLVM output at -O0, it has quite a complex
575 /// signature (refactoring this into two functions seems like a good idea).
576 ///
577 /// In particular, for lang items, it is invoked with a dest of None, and in
578 /// that case the return value contains the result of the fn. The lang item must
579 /// not return a structural type or else all heck breaks loose.
580 ///
581 /// For non-lang items, `dest` is always Some, and hence the result is written
582 /// into memory somewhere. Nonetheless we return the actual return value of the
583 /// function.
584 pub fn trans_call_inner<'a, 'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
585 debug_loc: DebugLoc,
586 get_callee: F,
587 args: CallArgs<'a, 'tcx>,
588 dest: Option<expr::Dest>)
589 -> Result<'blk, 'tcx> where
590 F: FnOnce(Block<'blk, 'tcx>, cleanup::ScopeId) -> Callee<'blk, 'tcx>,
591 {
592 // Introduce a temporary cleanup scope that will contain cleanups
593 // for the arguments while they are being evaluated. The purpose
594 // this cleanup is to ensure that, should a panic occur while
595 // evaluating argument N, the values for arguments 0...N-1 are all
596 // cleaned up. If no panic occurs, the values are handed off to
597 // the callee, and hence none of the cleanups in this temporary
598 // scope will ever execute.
599 let fcx = bcx.fcx;
600 let ccx = fcx.ccx;
601 let arg_cleanup_scope = fcx.push_custom_cleanup_scope();
602
603 let callee = get_callee(bcx, cleanup::CustomScope(arg_cleanup_scope));
604 let mut bcx = callee.bcx;
605
606 let (abi, ret_ty) = match callee.ty.sty {
607 ty::TyBareFn(_, ref f) => {
608 let output = bcx.tcx().erase_late_bound_regions(&f.sig.output());
609 (f.abi, output)
610 }
611 _ => panic!("expected bare rust fn or closure in trans_call_inner")
612 };
613
614 let (llfn, llself) = match callee.data {
615 Fn(llfn) => {
616 (llfn, None)
617 }
618 TraitItem(d) => {
619 (d.llfn, Some(d.llself))
620 }
621 Intrinsic(node, substs) => {
622 assert!(abi == synabi::RustIntrinsic || abi == synabi::PlatformIntrinsic);
623 assert!(dest.is_some());
624
625 let call_info = match debug_loc {
626 DebugLoc::At(id, span) => NodeIdAndSpan { id: id, span: span },
627 DebugLoc::None => {
628 bcx.sess().bug("No call info for intrinsic call?")
629 }
630 };
631
632 return intrinsic::trans_intrinsic_call(bcx, node, callee.ty,
633 arg_cleanup_scope, args,
634 dest.unwrap(), substs,
635 call_info);
636 }
637 NamedTupleConstructor(disr) => {
638 assert!(dest.is_some());
639 fcx.pop_custom_cleanup_scope(arg_cleanup_scope);
640
641 return base::trans_named_tuple_constructor(bcx,
642 callee.ty,
643 disr,
644 args,
645 dest.unwrap(),
646 debug_loc);
647 }
648 };
649
650 // Intrinsics should not become actual functions.
651 // We trans them in place in `trans_intrinsic_call`
652 assert!(abi != synabi::RustIntrinsic && abi != synabi::PlatformIntrinsic);
653
654 let is_rust_fn = abi == synabi::Rust || abi == synabi::RustCall;
655
656 // Generate a location to store the result. If the user does
657 // not care about the result, just make a stack slot.
658 let opt_llretslot = dest.and_then(|dest| match dest {
659 expr::SaveIn(dst) => Some(dst),
660 expr::Ignore => {
661 let ret_ty = match ret_ty {
662 ty::FnConverging(ret_ty) => ret_ty,
663 ty::FnDiverging => ccx.tcx().mk_nil()
664 };
665 if !is_rust_fn ||
666 type_of::return_uses_outptr(ccx, ret_ty) ||
667 bcx.fcx.type_needs_drop(ret_ty) {
668 // Push the out-pointer if we use an out-pointer for this
669 // return type, otherwise push "undef".
670 if common::type_is_zero_size(ccx, ret_ty) {
671 let llty = type_of::type_of(ccx, ret_ty);
672 Some(common::C_undef(llty.ptr_to()))
673 } else {
674 let llresult = alloc_ty(bcx, ret_ty, "__llret");
675 call_lifetime_start(bcx, llresult);
676 Some(llresult)
677 }
678 } else {
679 None
680 }
681 }
682 });
683
684 let mut llresult = unsafe {
685 llvm::LLVMGetUndef(Type::nil(ccx).ptr_to().to_ref())
686 };
687
688 // The code below invokes the function, using either the Rust
689 // conventions (if it is a rust fn) or the native conventions
690 // (otherwise). The important part is that, when all is said
691 // and done, either the return value of the function will have been
692 // written in opt_llretslot (if it is Some) or `llresult` will be
693 // set appropriately (otherwise).
694 if is_rust_fn {
695 let mut llargs = Vec::new();
696
697 if let (ty::FnConverging(ret_ty), Some(mut llretslot)) = (ret_ty, opt_llretslot) {
698 if type_of::return_uses_outptr(ccx, ret_ty) {
699 let llformal_ret_ty = type_of::type_of(ccx, ret_ty).ptr_to();
700 let llret_ty = common::val_ty(llretslot);
701 if llformal_ret_ty != llret_ty {
702 // this could happen due to e.g. subtyping
703 debug!("casting actual return type ({}) to match formal ({})",
704 bcx.llty_str(llret_ty), bcx.llty_str(llformal_ret_ty));
705 llretslot = PointerCast(bcx, llretslot, llformal_ret_ty);
706 }
707 llargs.push(llretslot);
708 }
709 }
710
711 // Push a trait object's self.
712 if let Some(llself) = llself {
713 llargs.push(llself);
714 }
715
716 // Push the arguments.
717 bcx = trans_args(bcx,
718 args,
719 callee.ty,
720 &mut llargs,
721 cleanup::CustomScope(arg_cleanup_scope),
722 llself.is_some(),
723 abi);
724
725 fcx.scopes.borrow_mut().last_mut().unwrap().drop_non_lifetime_clean();
726
727 // Invoke the actual rust fn and update bcx/llresult.
728 let (llret, b) = base::invoke(bcx,
729 llfn,
730 &llargs[..],
731 callee.ty,
732 debug_loc);
733 bcx = b;
734 llresult = llret;
735
736 // If the Rust convention for this type is return via
737 // the return value, copy it into llretslot.
738 match (opt_llretslot, ret_ty) {
739 (Some(llretslot), ty::FnConverging(ret_ty)) => {
740 if !type_of::return_uses_outptr(bcx.ccx(), ret_ty) &&
741 !common::type_is_zero_size(bcx.ccx(), ret_ty)
742 {
743 store_ty(bcx, llret, llretslot, ret_ty)
744 }
745 }
746 (_, _) => {}
747 }
748 } else {
749 // Lang items are the only case where dest is None, and
750 // they are always Rust fns.
751 assert!(dest.is_some());
752
753 let mut llargs = Vec::new();
754 let arg_tys = match args {
755 ArgExprs(a) => a.iter().map(|x| common::expr_ty_adjusted(bcx, &**x)).collect(),
756 _ => panic!("expected arg exprs.")
757 };
758 bcx = trans_args(bcx,
759 args,
760 callee.ty,
761 &mut llargs,
762 cleanup::CustomScope(arg_cleanup_scope),
763 false,
764 abi);
765 fcx.scopes.borrow_mut().last_mut().unwrap().drop_non_lifetime_clean();
766
767 bcx = foreign::trans_native_call(bcx,
768 callee.ty,
769 llfn,
770 opt_llretslot.unwrap(),
771 &llargs[..],
772 arg_tys,
773 debug_loc);
774 }
775
776 fcx.pop_and_trans_custom_cleanup_scope(bcx, arg_cleanup_scope);
777
778 // If the caller doesn't care about the result of this fn call,
779 // drop the temporary slot we made.
780 match (dest, opt_llretslot, ret_ty) {
781 (Some(expr::Ignore), Some(llretslot), ty::FnConverging(ret_ty)) => {
782 // drop the value if it is not being saved.
783 bcx = glue::drop_ty(bcx,
784 llretslot,
785 ret_ty,
786 debug_loc);
787 call_lifetime_end(bcx, llretslot);
788 }
789 _ => {}
790 }
791
792 if ret_ty == ty::FnDiverging {
793 Unreachable(bcx);
794 }
795
796 Result::new(bcx, llresult)
797 }
798
799 pub enum CallArgs<'a, 'tcx> {
800 // Supply value of arguments as a list of expressions that must be
801 // translated. This is used in the common case of `foo(bar, qux)`.
802 ArgExprs(&'a [P<hir::Expr>]),
803
804 // Supply value of arguments as a list of LLVM value refs; frequently
805 // used with lang items and so forth, when the argument is an internal
806 // value.
807 ArgVals(&'a [ValueRef]),
808
809 // For overloaded operators: `(lhs, Option(rhs, rhs_id), autoref)`. `lhs`
810 // is the left-hand-side and `rhs/rhs_id` is the datum/expr-id of
811 // the right-hand-side argument (if any). `autoref` indicates whether the `rhs`
812 // arguments should be auto-referenced
813 ArgOverloadedOp(Datum<'tcx, Expr>, Option<(Datum<'tcx, Expr>, ast::NodeId)>, bool),
814
815 // Supply value of arguments as a list of expressions that must be
816 // translated, for overloaded call operators.
817 ArgOverloadedCall(Vec<&'a hir::Expr>),
818 }
819
820 fn trans_args_under_call_abi<'blk, 'tcx>(
821 mut bcx: Block<'blk, 'tcx>,
822 arg_exprs: &[P<hir::Expr>],
823 fn_ty: Ty<'tcx>,
824 llargs: &mut Vec<ValueRef>,
825 arg_cleanup_scope: cleanup::ScopeId,
826 ignore_self: bool)
827 -> Block<'blk, 'tcx>
828 {
829 let args = bcx.tcx().erase_late_bound_regions(&fn_ty.fn_args());
830
831 // Translate the `self` argument first.
832 if !ignore_self {
833 let arg_datum = unpack_datum!(bcx, expr::trans(bcx, &*arg_exprs[0]));
834 bcx = trans_arg_datum(bcx,
835 args[0],
836 arg_datum,
837 arg_cleanup_scope,
838 DontAutorefArg,
839 llargs);
840 }
841
842 // Now untuple the rest of the arguments.
843 let tuple_expr = &arg_exprs[1];
844 let tuple_type = common::node_id_type(bcx, tuple_expr.id);
845
846 match tuple_type.sty {
847 ty::TyTuple(ref field_types) => {
848 let tuple_datum = unpack_datum!(bcx,
849 expr::trans(bcx, &**tuple_expr));
850 let tuple_lvalue_datum =
851 unpack_datum!(bcx,
852 tuple_datum.to_lvalue_datum(bcx,
853 "args",
854 tuple_expr.id));
855 let repr = adt::represent_type(bcx.ccx(), tuple_type);
856 let repr_ptr = &*repr;
857 for (i, field_type) in field_types.iter().enumerate() {
858 let arg_datum = tuple_lvalue_datum.get_element(
859 bcx,
860 field_type,
861 |srcval| {
862 adt::trans_field_ptr(bcx, repr_ptr, srcval, 0, i)
863 }).to_expr_datum();
864 bcx = trans_arg_datum(bcx,
865 field_type,
866 arg_datum,
867 arg_cleanup_scope,
868 DontAutorefArg,
869 llargs);
870 }
871 }
872 _ => {
873 bcx.sess().span_bug(tuple_expr.span,
874 "argument to `.call()` wasn't a tuple?!")
875 }
876 };
877
878 bcx
879 }
880
881 fn trans_overloaded_call_args<'blk, 'tcx>(
882 mut bcx: Block<'blk, 'tcx>,
883 arg_exprs: Vec<&hir::Expr>,
884 fn_ty: Ty<'tcx>,
885 llargs: &mut Vec<ValueRef>,
886 arg_cleanup_scope: cleanup::ScopeId,
887 ignore_self: bool)
888 -> Block<'blk, 'tcx> {
889 // Translate the `self` argument first.
890 let arg_tys = bcx.tcx().erase_late_bound_regions( &fn_ty.fn_args());
891 if !ignore_self {
892 let arg_datum = unpack_datum!(bcx, expr::trans(bcx, arg_exprs[0]));
893 bcx = trans_arg_datum(bcx,
894 arg_tys[0],
895 arg_datum,
896 arg_cleanup_scope,
897 DontAutorefArg,
898 llargs);
899 }
900
901 // Now untuple the rest of the arguments.
902 let tuple_type = arg_tys[1];
903 match tuple_type.sty {
904 ty::TyTuple(ref field_types) => {
905 for (i, &field_type) in field_types.iter().enumerate() {
906 let arg_datum =
907 unpack_datum!(bcx, expr::trans(bcx, arg_exprs[i + 1]));
908 bcx = trans_arg_datum(bcx,
909 field_type,
910 arg_datum,
911 arg_cleanup_scope,
912 DontAutorefArg,
913 llargs);
914 }
915 }
916 _ => {
917 bcx.sess().span_bug(arg_exprs[0].span,
918 "argument to `.call()` wasn't a tuple?!")
919 }
920 };
921
922 bcx
923 }
924
925 pub fn trans_args<'a, 'blk, 'tcx>(cx: Block<'blk, 'tcx>,
926 args: CallArgs<'a, 'tcx>,
927 fn_ty: Ty<'tcx>,
928 llargs: &mut Vec<ValueRef>,
929 arg_cleanup_scope: cleanup::ScopeId,
930 ignore_self: bool,
931 abi: synabi::Abi)
932 -> Block<'blk, 'tcx> {
933 debug!("trans_args(abi={})", abi);
934
935 let _icx = push_ctxt("trans_args");
936 let arg_tys = cx.tcx().erase_late_bound_regions(&fn_ty.fn_args());
937 let variadic = fn_ty.fn_sig().0.variadic;
938
939 let mut bcx = cx;
940
941 // First we figure out the caller's view of the types of the arguments.
942 // This will be needed if this is a generic call, because the callee has
943 // to cast her view of the arguments to the caller's view.
944 match args {
945 ArgExprs(arg_exprs) => {
946 if abi == synabi::RustCall {
947 // This is only used for direct calls to the `call`,
948 // `call_mut` or `call_once` functions.
949 return trans_args_under_call_abi(cx,
950 arg_exprs,
951 fn_ty,
952 llargs,
953 arg_cleanup_scope,
954 ignore_self)
955 }
956
957 let num_formal_args = arg_tys.len();
958 for (i, arg_expr) in arg_exprs.iter().enumerate() {
959 if i == 0 && ignore_self {
960 continue;
961 }
962 let arg_ty = if i >= num_formal_args {
963 assert!(variadic);
964 common::expr_ty_adjusted(cx, &**arg_expr)
965 } else {
966 arg_tys[i]
967 };
968
969 let arg_datum = unpack_datum!(bcx, expr::trans(bcx, &**arg_expr));
970 bcx = trans_arg_datum(bcx, arg_ty, arg_datum,
971 arg_cleanup_scope,
972 DontAutorefArg,
973 llargs);
974 }
975 }
976 ArgOverloadedCall(arg_exprs) => {
977 return trans_overloaded_call_args(cx,
978 arg_exprs,
979 fn_ty,
980 llargs,
981 arg_cleanup_scope,
982 ignore_self)
983 }
984 ArgOverloadedOp(lhs, rhs, autoref) => {
985 assert!(!variadic);
986
987 bcx = trans_arg_datum(bcx, arg_tys[0], lhs,
988 arg_cleanup_scope,
989 DontAutorefArg,
990 llargs);
991
992 if let Some((rhs, rhs_id)) = rhs {
993 assert_eq!(arg_tys.len(), 2);
994 bcx = trans_arg_datum(bcx, arg_tys[1], rhs,
995 arg_cleanup_scope,
996 if autoref { DoAutorefArg(rhs_id) } else { DontAutorefArg },
997 llargs);
998 } else {
999 assert_eq!(arg_tys.len(), 1);
1000 }
1001 }
1002 ArgVals(vs) => {
1003 llargs.push_all(vs);
1004 }
1005 }
1006
1007 bcx
1008 }
1009
1010 #[derive(Copy, Clone)]
1011 pub enum AutorefArg {
1012 DontAutorefArg,
1013 DoAutorefArg(ast::NodeId)
1014 }
1015
1016 pub fn trans_arg_datum<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1017 formal_arg_ty: Ty<'tcx>,
1018 arg_datum: Datum<'tcx, Expr>,
1019 arg_cleanup_scope: cleanup::ScopeId,
1020 autoref_arg: AutorefArg,
1021 llargs: &mut Vec<ValueRef>)
1022 -> Block<'blk, 'tcx> {
1023 let _icx = push_ctxt("trans_arg_datum");
1024 let mut bcx = bcx;
1025 let ccx = bcx.ccx();
1026
1027 debug!("trans_arg_datum({:?})",
1028 formal_arg_ty);
1029
1030 let arg_datum_ty = arg_datum.ty;
1031
1032 debug!(" arg datum: {}", arg_datum.to_string(bcx.ccx()));
1033
1034 let mut val;
1035 // FIXME(#3548) use the adjustments table
1036 match autoref_arg {
1037 DoAutorefArg(arg_id) => {
1038 // We will pass argument by reference
1039 // We want an lvalue, so that we can pass by reference and
1040 let arg_datum = unpack_datum!(
1041 bcx, arg_datum.to_lvalue_datum(bcx, "arg", arg_id));
1042 val = arg_datum.val;
1043 }
1044 DontAutorefArg if common::type_is_fat_ptr(bcx.tcx(), arg_datum_ty) &&
1045 !bcx.fcx.type_needs_drop(arg_datum_ty) => {
1046 val = arg_datum.val
1047 }
1048 DontAutorefArg => {
1049 // Make this an rvalue, since we are going to be
1050 // passing ownership.
1051 let arg_datum = unpack_datum!(
1052 bcx, arg_datum.to_rvalue_datum(bcx, "arg"));
1053
1054 // Now that arg_datum is owned, get it into the appropriate
1055 // mode (ref vs value).
1056 let arg_datum = unpack_datum!(
1057 bcx, arg_datum.to_appropriate_datum(bcx));
1058
1059 // Technically, ownership of val passes to the callee.
1060 // However, we must cleanup should we panic before the
1061 // callee is actually invoked.
1062 val = arg_datum.add_clean(bcx.fcx, arg_cleanup_scope);
1063 }
1064 }
1065
1066 if type_of::arg_is_indirect(ccx, formal_arg_ty) && formal_arg_ty != arg_datum_ty {
1067 // this could happen due to e.g. subtyping
1068 let llformal_arg_ty = type_of::type_of_explicit_arg(ccx, formal_arg_ty);
1069 debug!("casting actual type ({}) to match formal ({})",
1070 bcx.val_to_string(val), bcx.llty_str(llformal_arg_ty));
1071 debug!("Rust types: {:?}; {:?}", arg_datum_ty,
1072 formal_arg_ty);
1073 val = PointerCast(bcx, val, llformal_arg_ty);
1074 }
1075
1076 debug!("--- trans_arg_datum passing {}", bcx.val_to_string(val));
1077
1078 if common::type_is_fat_ptr(bcx.tcx(), formal_arg_ty) {
1079 llargs.push(Load(bcx, expr::get_dataptr(bcx, val)));
1080 llargs.push(Load(bcx, expr::get_meta(bcx, val)));
1081 } else {
1082 llargs.push(val);
1083 }
1084
1085 bcx
1086 }