2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2013-06-29 05:11:14 -05:00
|
|
|
/*!
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-06-29 05:11:14 -05:00
|
|
|
use std::vec;
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-02-25 13:11:21 -06:00
|
|
|
use back::abi;
|
|
|
|
use driver::session;
|
add sret + noalias to the out pointer parameter
This brings Rust in line with how `clang` handles return pointers.
Example:
pub fn bar() -> [uint, .. 8] {
let a = [0, .. 8];
a
}
Before:
; Function Attrs: nounwind uwtable
define void @_ZN3bar17ha4635c6f704bfa334v0.0E([8 x i64]* nocapture, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #1 {
"function top level":
%a = alloca [8 x i64], align 8
%2 = bitcast [8 x i64]* %a to i8*
call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 64, i32 8, i1 false)
%3 = bitcast [8 x i64]* %0 to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* %2, i64 64, i32 8, i1 false)
ret void
}
After:
; Function Attrs: nounwind uwtable
define void @_ZN3bar17ha4635c6f704bfa334v0.0E([8 x i64]* noalias nocapture sret, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #1 {
"function top level":
%2 = bitcast [8 x i64]* %0 to i8*
call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 64, i32 8, i1 false)
ret void
}
Closes #9072
Closes #7298
Closes #9154
2013-09-10 13:28:59 -05:00
|
|
|
use lib::llvm::{ValueRef, NoAliasAttribute, StructRetAttribute};
|
2013-02-25 13:11:21 -06:00
|
|
|
use lib::llvm::llvm;
|
|
|
|
use metadata::csearch;
|
|
|
|
use middle::trans::base;
|
|
|
|
use middle::trans::base::*;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::build::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::callee;
|
2014-01-15 13:39:08 -06:00
|
|
|
use middle::trans::cleanup;
|
|
|
|
use middle::trans::cleanup::CleanupMethods;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::common;
|
|
|
|
use middle::trans::common::*;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::datum::*;
|
|
|
|
use middle::trans::datum::Datum;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::expr;
|
|
|
|
use middle::trans::glue;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::inline;
|
|
|
|
use middle::trans::meth;
|
|
|
|
use middle::trans::monomorphize;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::type_of;
|
2013-05-21 14:25:44 -05:00
|
|
|
use middle::trans::foreign;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::ty;
|
2013-06-12 13:05:03 -05:00
|
|
|
use middle::subst::Subst;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::typeck;
|
2013-06-12 16:22:17 -05:00
|
|
|
use middle::typeck::coherence::make_substs_for_receiver_types;
|
2014-03-06 11:24:11 -06:00
|
|
|
use middle::typeck::MethodCall;
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
use util::ppaux::Repr;
|
2012-12-13 15:05:22 -06:00
|
|
|
|
2013-06-16 05:52:44 -05:00
|
|
|
use middle::trans::type_::Type;
|
|
|
|
|
2014-03-08 14:36:22 -06:00
|
|
|
use std::vec_ng::Vec;
|
|
|
|
use std::vec_ng;
|
2012-08-28 17:54:45 -05:00
|
|
|
use syntax::ast;
|
2013-05-21 14:25:44 -05:00
|
|
|
use syntax::abi::AbiSet;
|
2013-02-25 13:11:21 -06:00
|
|
|
use syntax::ast_map;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub struct MethodData {
|
2012-09-07 16:50:47 -05:00
|
|
|
llfn: ValueRef,
|
|
|
|
llself: ValueRef,
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub enum CalleeData {
|
2014-01-15 13:39:08 -06:00
|
|
|
Closure(Datum<Lvalue>),
|
2014-01-27 06:18:36 -06:00
|
|
|
|
|
|
|
// 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).
|
|
|
|
Fn(/* llfn */ ValueRef),
|
|
|
|
|
|
|
|
TraitMethod(MethodData)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
pub struct Callee<'a> {
|
|
|
|
bcx: &'a Block<'a>,
|
2012-09-07 16:50:47 -05:00
|
|
|
data: CalleeData
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
fn trans<'a>(bcx: &'a Block<'a>, expr: &ast::Expr) -> Callee<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_callee");
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("callee::trans(expr={})", expr.repr(bcx.tcx()));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// pick out special kinds of expressions that can be called:
|
|
|
|
match expr.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprPath(_) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return trans_def(bcx, bcx.def(expr.id), expr);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// any other expressions are closures:
|
2013-02-27 18:28:37 -06:00
|
|
|
return datum_callee(bcx, expr);
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn datum_callee<'a>(bcx: &'a Block<'a>, expr: &ast::Expr) -> Callee<'a> {
|
2014-01-15 13:39:08 -06:00
|
|
|
let DatumBlock {bcx: mut bcx, datum} = expr::trans(bcx, expr);
|
2013-02-27 18:28:37 -06:00
|
|
|
match ty::get(datum.ty).sty {
|
2013-11-28 14:22:53 -06:00
|
|
|
ty::ty_bare_fn(..) => {
|
2014-01-15 13:39:08 -06:00
|
|
|
let llval = datum.to_llscalarish(bcx);
|
2014-01-27 06:18:36 -06:00
|
|
|
return Callee {bcx: bcx, data: Fn(llval)};
|
2013-02-27 18:28:37 -06:00
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
ty::ty_closure(..) => {
|
2014-01-15 13:39:08 -06:00
|
|
|
let datum = unpack_datum!(
|
|
|
|
bcx, datum.to_lvalue_datum(bcx, "callee", expr.id));
|
2013-02-27 18:28:37 -06:00
|
|
|
return Callee {bcx: bcx, data: Closure(datum)};
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(
|
|
|
|
expr.span,
|
2014-02-06 03:38:08 -06:00
|
|
|
format!("type of callee is neither bare-fn nor closure: {}",
|
2013-02-27 18:28:37 -06:00
|
|
|
bcx.ty_to_str(datum.ty)));
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-27 06:18:36 -06:00
|
|
|
fn fn_callee<'a>(bcx: &'a Block<'a>, llfn: ValueRef) -> Callee<'a> {
|
|
|
|
return Callee {bcx: bcx, data: Fn(llfn)};
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_def<'a>(bcx: &'a Block<'a>, def: ast::Def, ref_expr: &ast::Expr)
|
|
|
|
-> Callee<'a> {
|
2012-08-28 17:54:45 -05:00
|
|
|
match def {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::DefFn(did, _) |
|
|
|
|
ast::DefStaticMethod(did, ast::FromImpl(_), _) => {
|
2014-03-06 11:24:11 -06:00
|
|
|
fn_callee(bcx, trans_fn_ref(bcx, did, ExprId(ref_expr.id)))
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::DefStaticMethod(impl_did,
|
2013-08-08 13:38:10 -05:00
|
|
|
ast::FromTrait(trait_did),
|
|
|
|
_) => {
|
2012-10-12 19:00:08 -05:00
|
|
|
fn_callee(bcx, meth::trans_static_method_callee(bcx, impl_did,
|
|
|
|
trait_did,
|
2012-08-28 17:54:45 -05:00
|
|
|
ref_expr.id))
|
|
|
|
}
|
2013-09-08 19:36:01 -05:00
|
|
|
ast::DefVariant(tid, vid, _) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
// nullary variants are not callable
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(ty::enum_variant_with_id(bcx.tcx(),
|
2013-03-06 15:58:02 -06:00
|
|
|
tid,
|
|
|
|
vid).args.len() > 0u);
|
2014-03-06 11:24:11 -06:00
|
|
|
fn_callee(bcx, trans_fn_ref(bcx, vid, ExprId(ref_expr.id)))
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::DefStruct(def_id) => {
|
2014-03-06 11:24:11 -06:00
|
|
|
fn_callee(bcx, trans_fn_ref(bcx, def_id, ExprId(ref_expr.id)))
|
2012-10-24 16:36:00 -05:00
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::DefStatic(..) |
|
|
|
|
ast::DefArg(..) |
|
|
|
|
ast::DefLocal(..) |
|
|
|
|
ast::DefBinding(..) |
|
2014-01-27 06:18:36 -06:00
|
|
|
ast::DefUpvar(..) => {
|
2013-02-27 18:28:37 -06:00
|
|
|
datum_callee(bcx, ref_expr)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::DefMod(..) | ast::DefForeignMod(..) | ast::DefTrait(..) |
|
|
|
|
ast::DefTy(..) | ast::DefPrimTy(..) |
|
|
|
|
ast::DefUse(..) | ast::DefTyParamBinder(..) |
|
|
|
|
ast::DefRegion(..) | ast::DefLabel(..) | ast::DefTyParam(..) |
|
|
|
|
ast::DefSelfTy(..) | ast::DefMethod(..) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
bcx.tcx().sess.span_bug(
|
|
|
|
ref_expr.span,
|
2014-02-06 03:38:08 -06:00
|
|
|
format!("cannot translate def {:?} \
|
2012-08-28 17:54:45 -05:00
|
|
|
to a callable thing!", def));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 11:24:11 -06:00
|
|
|
pub fn trans_fn_ref(bcx: &Block, def_id: ast::DefId, node: ExprOrMethodCall) -> ValueRef {
|
2012-09-10 14:25:45 -05:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* 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. */
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_fn_ref");
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-03-06 11:24:11 -06:00
|
|
|
let type_params = node_id_type_params(bcx, node);
|
|
|
|
let vtables = match node {
|
|
|
|
ExprId(id) => node_vtables(bcx, id),
|
|
|
|
MethodCall(method_call) if method_call.autoderef == 0 => {
|
|
|
|
node_vtables(bcx, method_call.expr_id)
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
debug!("trans_fn_ref(def_id={}, node={:?}, type_params={}, vtables={})",
|
|
|
|
def_id.repr(bcx.tcx()), node, type_params.repr(bcx.tcx()),
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
vtables.repr(bcx.tcx()));
|
2014-03-06 11:24:11 -06:00
|
|
|
trans_fn_ref_with_vtables(bcx, def_id, node,
|
2014-03-08 14:36:22 -06:00
|
|
|
type_params.as_slice(),
|
|
|
|
vtables)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-02-26 08:06:45 -06:00
|
|
|
fn trans_fn_ref_with_vtables_to_callee<'a>(bcx: &'a Block<'a>,
|
2014-01-07 10:54:58 -06:00
|
|
|
def_id: ast::DefId,
|
|
|
|
ref_id: ast::NodeId,
|
|
|
|
type_params: &[ty::t],
|
|
|
|
vtables: Option<typeck::vtable_res>)
|
|
|
|
-> Callee<'a> {
|
2012-08-28 17:54:45 -05:00
|
|
|
Callee {bcx: bcx,
|
2014-03-06 11:24:11 -06:00
|
|
|
data: Fn(trans_fn_ref_with_vtables(bcx, def_id, ExprId(ref_id),
|
2012-08-28 17:54:45 -05:00
|
|
|
type_params, vtables))}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn resolve_default_method_vtables(bcx: &Block,
|
2013-09-01 20:45:37 -05:00
|
|
|
impl_id: ast::DefId,
|
2013-06-28 13:18:09 -05:00
|
|
|
method: &ty::Method,
|
|
|
|
substs: &ty::substs,
|
|
|
|
impl_vtables: Option<typeck::vtable_res>)
|
2013-07-22 18:40:31 -05:00
|
|
|
-> (typeck::vtable_res, typeck::vtable_param_res) {
|
2013-06-28 13:18:09 -05:00
|
|
|
|
|
|
|
// Get the vtables that the impl implements the trait at
|
2013-07-22 18:40:31 -05:00
|
|
|
let impl_res = ty::lookup_impl_vtables(bcx.tcx(), impl_id);
|
2013-06-28 13:18:09 -05:00
|
|
|
|
|
|
|
// Build up a param_substs that we are going to resolve the
|
|
|
|
// trait_vtables under.
|
|
|
|
let param_substs = Some(@param_substs {
|
2013-07-02 14:47:32 -05:00
|
|
|
tys: substs.tps.clone(),
|
2013-06-28 13:18:09 -05:00
|
|
|
self_ty: substs.self_ty,
|
|
|
|
vtables: impl_vtables,
|
2013-07-22 18:40:31 -05:00
|
|
|
self_vtables: None
|
2013-06-28 13:18:09 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
let trait_vtables_fixed = resolve_vtables_under_param_substs(
|
2013-07-22 18:40:31 -05:00
|
|
|
bcx.tcx(), param_substs, impl_res.trait_vtables);
|
2013-06-28 13:18:09 -05:00
|
|
|
|
|
|
|
// Now we pull any vtables for parameters on the actual method.
|
2014-01-31 22:57:59 -06:00
|
|
|
let num_method_vtables = method.generics.type_param_defs().len();
|
2013-06-28 13:18:09 -05:00
|
|
|
let method_vtables = match impl_vtables {
|
|
|
|
Some(vtables) => {
|
|
|
|
let num_impl_type_parameters =
|
|
|
|
vtables.len() - num_method_vtables;
|
|
|
|
vtables.tailn(num_impl_type_parameters).to_owned()
|
|
|
|
},
|
2014-03-04 12:02:49 -06:00
|
|
|
None => vec::from_elem(num_method_vtables, @Vec::new())
|
2013-06-28 13:18:09 -05:00
|
|
|
};
|
|
|
|
|
2014-03-08 14:36:22 -06:00
|
|
|
let param_vtables = @(vec_ng::append((*trait_vtables_fixed).clone(),
|
|
|
|
method_vtables));
|
2013-07-22 18:40:31 -05:00
|
|
|
|
|
|
|
let self_vtables = resolve_param_vtables_under_param_substs(
|
|
|
|
bcx.tcx(), param_substs, impl_res.self_vtables);
|
|
|
|
|
|
|
|
(param_vtables, self_vtables)
|
2013-06-28 13:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn trans_fn_ref_with_vtables(
|
2014-01-07 10:54:58 -06:00
|
|
|
bcx: &Block, //
|
2013-09-01 20:45:37 -05:00
|
|
|
def_id: ast::DefId, // def id of fn
|
2014-03-06 11:24:11 -06:00
|
|
|
node: ExprOrMethodCall, // node id of use of fn; may be zero if N/A
|
2013-03-05 19:36:39 -06:00
|
|
|
type_params: &[ty::t], // values for fn's ty params
|
2013-07-08 19:28:36 -05:00
|
|
|
vtables: Option<typeck::vtable_res>) // vtables for the call
|
2014-01-27 06:18:36 -06:00
|
|
|
-> ValueRef {
|
2013-05-21 14:25:44 -05:00
|
|
|
/*!
|
|
|
|
* 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
|
2014-03-06 11:24:11 -06:00
|
|
|
* - `node`: node id of the reference to the fn/method, if applicable.
|
2013-05-21 14:25:44 -05:00
|
|
|
* 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
|
|
|
|
*/
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_fn_ref_with_vtables");
|
2012-08-28 17:54:45 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
let tcx = ccx.tcx;
|
|
|
|
|
2014-03-06 11:24:11 -06:00
|
|
|
debug!("trans_fn_ref_with_vtables(bcx={}, def_id={}, node={:?}, \
|
2013-09-28 00:38:08 -05:00
|
|
|
type_params={}, vtables={})",
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
bcx.to_str(),
|
|
|
|
def_id.repr(bcx.tcx()),
|
2014-03-06 11:24:11 -06:00
|
|
|
node,
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
type_params.repr(bcx.tcx()),
|
|
|
|
vtables.repr(bcx.tcx()));
|
2012-09-10 14:25:45 -05:00
|
|
|
|
2013-06-17 18:43:22 -05:00
|
|
|
assert!(type_params.iter().all(|t| !ty::type_needs_infer(*t)));
|
2013-03-21 07:34:18 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
// Polytype of the function item (may have type params)
|
|
|
|
let fn_tpt = ty::lookup_item_type(tcx, def_id);
|
|
|
|
|
2013-07-24 15:52:57 -05:00
|
|
|
let substs = ty::substs { regions: ty::ErasedRegions,
|
2013-06-25 16:07:44 -05:00
|
|
|
self_ty: None,
|
2014-03-08 14:36:22 -06:00
|
|
|
tps: /*bad*/ Vec::from_slice(type_params) };
|
2013-06-12 13:05:03 -05:00
|
|
|
|
2013-08-23 16:34:00 -05:00
|
|
|
// Load the info for the appropriate trait if necessary.
|
|
|
|
match ty::trait_of_method(tcx, def_id) {
|
|
|
|
None => {}
|
|
|
|
Some(trait_id) => {
|
|
|
|
ty::populate_implementations_for_trait_if_necessary(tcx, trait_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-12 13:05:03 -05:00
|
|
|
// We need to do a bunch of special handling for default methods.
|
|
|
|
// We need to modify the def_id and our substs in order to monomorphize
|
|
|
|
// the function.
|
2013-07-22 18:40:31 -05:00
|
|
|
let (is_default, def_id, substs, self_vtables, vtables) =
|
2013-07-15 19:26:56 -05:00
|
|
|
match ty::provided_source(tcx, def_id) {
|
|
|
|
None => (false, def_id, substs, None, vtables),
|
|
|
|
Some(source_id) => {
|
2013-06-12 13:05:03 -05:00
|
|
|
// There are two relevant substitutions when compiling
|
|
|
|
// default methods. First, there is the substitution for
|
|
|
|
// the type parameters of the impl we are using and the
|
|
|
|
// method we are calling. This substitution is the substs
|
|
|
|
// argument we already have.
|
|
|
|
// In order to compile a default method, though, we need
|
|
|
|
// to consider another substitution: the substitution for
|
|
|
|
// the type parameters on trait; the impl we are using
|
|
|
|
// implements the trait at some particular type
|
|
|
|
// parameters, and we need to substitute for those first.
|
|
|
|
// So, what we need to do is find this substitution and
|
|
|
|
// compose it with the one we already have.
|
|
|
|
|
2013-08-23 16:34:00 -05:00
|
|
|
let impl_id = ty::method(tcx, def_id).container_id();
|
2013-07-15 19:26:56 -05:00
|
|
|
let method = ty::method(tcx, source_id);
|
|
|
|
let trait_ref = ty::impl_trait_ref(tcx, impl_id)
|
2013-06-12 16:22:17 -05:00
|
|
|
.expect("could not find trait_ref for impl with \
|
|
|
|
default methods");
|
|
|
|
|
|
|
|
// Compute the first substitution
|
|
|
|
let first_subst = make_substs_for_receiver_types(
|
2013-07-15 19:26:56 -05:00
|
|
|
tcx, impl_id, trait_ref, method);
|
2013-06-12 16:22:17 -05:00
|
|
|
|
|
|
|
// And compose them
|
2013-06-12 13:05:03 -05:00
|
|
|
let new_substs = first_subst.subst(tcx, &substs);
|
2013-06-28 13:18:09 -05:00
|
|
|
|
|
|
|
|
2013-07-22 18:40:31 -05:00
|
|
|
let (param_vtables, self_vtables) =
|
2013-07-15 19:26:56 -05:00
|
|
|
resolve_default_method_vtables(bcx, impl_id,
|
2013-07-22 19:41:07 -05:00
|
|
|
method, &substs, vtables);
|
2013-06-28 13:18:09 -05:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("trans_fn_with_vtables - default method: \
|
2013-09-28 00:38:08 -05:00
|
|
|
substs = {}, trait_subst = {}, \
|
|
|
|
first_subst = {}, new_subst = {}, \
|
|
|
|
vtables = {}, \
|
|
|
|
self_vtable = {}, param_vtables = {}",
|
2013-06-12 16:22:17 -05:00
|
|
|
substs.repr(tcx), trait_ref.substs.repr(tcx),
|
2013-06-28 13:18:09 -05:00
|
|
|
first_subst.repr(tcx), new_substs.repr(tcx),
|
2013-07-22 18:40:31 -05:00
|
|
|
vtables.repr(tcx),
|
|
|
|
self_vtables.repr(tcx), param_vtables.repr(tcx));
|
2013-06-12 16:22:17 -05:00
|
|
|
|
2013-07-15 19:26:56 -05:00
|
|
|
(true, source_id,
|
2013-07-22 18:40:31 -05:00
|
|
|
new_substs, Some(self_vtables), Some(param_vtables))
|
2013-06-12 13:05:03 -05:00
|
|
|
}
|
2012-10-08 14:39:30 -05:00
|
|
|
};
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
// 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 = {
|
2014-02-05 15:15:24 -06:00
|
|
|
if def_id.krate != ast::LOCAL_CRATE {
|
2013-07-11 16:34:10 -05:00
|
|
|
inline::maybe_instantiate_inline(ccx, def_id)
|
2012-08-28 17:54:45 -05:00
|
|
|
} else {
|
|
|
|
def_id
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-08 14:39:30 -05:00
|
|
|
// 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.
|
2014-02-13 23:07:09 -06:00
|
|
|
let must_monomorphise = if type_params.len() > 0 || is_default {
|
|
|
|
true
|
2014-02-05 15:15:24 -06:00
|
|
|
} else if def_id.krate == ast::LOCAL_CRATE {
|
2014-02-13 23:07:09 -06:00
|
|
|
let map_node = session::expect(
|
|
|
|
ccx.sess,
|
|
|
|
ccx.tcx.map.find(def_id.node),
|
|
|
|
|| format!("local item should be in ast map"));
|
|
|
|
|
|
|
|
match map_node {
|
|
|
|
ast_map::NodeForeignItem(_) => {
|
|
|
|
ccx.tcx.map.get_foreign_abis(def_id.node).is_intrinsic()
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-02-13 23:07:09 -06:00
|
|
|
_ => false
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-10-26 20:23:45 -05:00
|
|
|
} else {
|
2014-02-13 23:07:09 -06:00
|
|
|
false
|
|
|
|
};
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// Create a monomorphic verison of generic functions
|
|
|
|
if must_monomorphise {
|
|
|
|
// Should be either intra-crate or inlined.
|
2014-02-05 15:15:24 -06:00
|
|
|
assert_eq!(def_id.krate, ast::LOCAL_CRATE);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-03-06 11:24:11 -06:00
|
|
|
let ref_id = match node {
|
|
|
|
ExprId(id) if id != 0 => Some(id),
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
|
2013-06-06 20:54:14 -05:00
|
|
|
let (val, must_cast) =
|
2013-06-12 13:05:03 -05:00
|
|
|
monomorphize::monomorphic_fn(ccx, def_id, &substs,
|
2013-07-22 18:40:31 -05:00
|
|
|
vtables, self_vtables,
|
2014-03-06 11:24:11 -06:00
|
|
|
ref_id);
|
2013-06-06 20:54:14 -05:00
|
|
|
let mut val = val;
|
2014-03-06 11:24:11 -06:00
|
|
|
if must_cast && node != ExprId(0) {
|
2012-08-28 17:54:45 -05:00
|
|
|
// Monotype of the REFERENCE to the function (type params
|
|
|
|
// are subst'd)
|
2014-03-06 11:24:11 -06:00
|
|
|
let ref_ty = match node {
|
|
|
|
ExprId(id) => node_id_type(bcx, id),
|
|
|
|
MethodCall(method_call) => {
|
|
|
|
let t = bcx.ccx().maps.method_map.borrow().get().get(&method_call).ty;
|
|
|
|
monomorphize_type(bcx, t)
|
|
|
|
}
|
2014-02-26 08:06:45 -06:00
|
|
|
};
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
val = PointerCast(
|
2014-01-27 06:18:36 -06:00
|
|
|
bcx, val, type_of::type_of_fn_from_ty(ccx, ref_ty).ptr_to());
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-01-27 06:18:36 -06:00
|
|
|
return val;
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the actual function pointer.
|
2013-05-21 14:25:44 -05:00
|
|
|
let mut val = {
|
2014-02-05 15:15:24 -06:00
|
|
|
if def_id.krate == ast::LOCAL_CRATE {
|
2012-08-28 17:54:45 -05:00
|
|
|
// Internal reference.
|
|
|
|
get_item_val(ccx, def_id.node)
|
|
|
|
} else {
|
|
|
|
// External reference.
|
|
|
|
trans_external_path(ccx, def_id, fn_tpt.ty)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
// This is subtle and surprising, but sometimes we have to bitcast
|
|
|
|
// the resulting fn pointer. The reason has to do with external
|
|
|
|
// functions. If you have two crates that both bind the same C
|
|
|
|
// library, they may not use precisely the same types: for
|
|
|
|
// example, they will probably each declare their own structs,
|
|
|
|
// which are distinct types from LLVM's point of view (nominal
|
|
|
|
// types).
|
|
|
|
//
|
|
|
|
// Now, if those two crates are linked into an application, and
|
|
|
|
// they contain inlined code, you can wind up with a situation
|
|
|
|
// where both of those functions wind up being loaded into this
|
|
|
|
// application simultaneously. In that case, the same function
|
|
|
|
// (from LLVM's point of view) requires two types. But of course
|
|
|
|
// LLVM won't allow one function to have two types.
|
|
|
|
//
|
|
|
|
// What we currently do, therefore, is declare the function with
|
|
|
|
// one of the two types (whichever happens to come first) and then
|
|
|
|
// bitcast as needed when the function is referenced to make sure
|
|
|
|
// it has the type we expect.
|
|
|
|
//
|
|
|
|
// This can occur on either a crate-local or crate-external
|
|
|
|
// reference. It also occurs when testing libcore and in some
|
|
|
|
// other weird situations. Annoying.
|
2014-01-27 06:18:36 -06:00
|
|
|
let llty = type_of::type_of_fn_from_ty(ccx, fn_tpt.ty);
|
2013-05-21 14:25:44 -05:00
|
|
|
let llptrty = llty.ptr_to();
|
|
|
|
if val_ty(val) != llptrty {
|
|
|
|
val = BitCast(bcx, val, llptrty);
|
|
|
|
}
|
|
|
|
|
2014-01-27 06:18:36 -06:00
|
|
|
val
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ______________________________________________________________________
|
|
|
|
// Translating calls
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
pub fn trans_call<'a>(
|
|
|
|
in_cx: &'a Block<'a>,
|
2013-09-30 12:37:17 -05:00
|
|
|
call_ex: &ast::Expr,
|
|
|
|
f: &ast::Expr,
|
2013-01-29 19:57:02 -06:00
|
|
|
args: CallArgs,
|
|
|
|
dest: expr::Dest)
|
2014-01-07 10:54:58 -06:00
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_call");
|
2013-04-18 17:53:29 -05:00
|
|
|
trans_call_inner(in_cx,
|
2014-01-15 13:39:08 -06:00
|
|
|
Some(common::expr_info(call_ex)),
|
2013-04-18 17:53:29 -05:00
|
|
|
expr_ty(in_cx, f),
|
2014-01-15 13:39:08 -06:00
|
|
|
|cx, _| trans(cx, f),
|
2013-04-18 17:53:29 -05:00
|
|
|
args,
|
2014-01-27 06:18:36 -06:00
|
|
|
Some(dest)).bcx
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
pub fn trans_method_call<'a>(
|
2014-02-26 08:06:45 -06:00
|
|
|
bcx: &'a Block<'a>,
|
2013-09-30 12:37:17 -05:00
|
|
|
call_ex: &ast::Expr,
|
|
|
|
rcvr: &ast::Expr,
|
2013-01-29 19:57:02 -06:00
|
|
|
args: CallArgs,
|
|
|
|
dest: expr::Dest)
|
2014-01-07 10:54:58 -06:00
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_method_call");
|
2014-02-26 08:06:45 -06:00
|
|
|
debug!("trans_method_call(call_ex={})", call_ex.repr(bcx.tcx()));
|
2014-03-06 11:24:11 -06:00
|
|
|
let method_call = MethodCall::expr(call_ex.id);
|
|
|
|
let method_ty = bcx.ccx().maps.method_map.borrow().get().get(&method_call).ty;
|
2012-11-30 13:18:25 -06:00
|
|
|
trans_call_inner(
|
2014-02-26 08:06:45 -06:00
|
|
|
bcx,
|
2014-01-15 13:39:08 -06:00
|
|
|
Some(common::expr_info(call_ex)),
|
2014-02-26 08:06:45 -06:00
|
|
|
monomorphize_type(bcx, method_ty),
|
2014-01-15 13:39:08 -06:00
|
|
|
|cx, arg_cleanup_scope| {
|
2014-03-06 11:24:11 -06:00
|
|
|
meth::trans_method_callee(cx, method_call, Some(rcvr), arg_cleanup_scope)
|
2012-11-30 13:18:25 -06:00
|
|
|
},
|
|
|
|
args,
|
2014-01-27 06:18:36 -06:00
|
|
|
Some(dest)).bcx
|
2012-11-30 13:18:25 -06:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
pub fn trans_lang_call<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-09-01 20:45:37 -05:00
|
|
|
did: ast::DefId,
|
2013-02-27 20:34:04 -06:00
|
|
|
args: &[ValueRef],
|
2013-07-08 01:12:01 -05:00
|
|
|
dest: Option<expr::Dest>)
|
2014-01-07 10:54:58 -06:00
|
|
|
-> Result<'a> {
|
2014-02-05 15:15:24 -06:00
|
|
|
let fty = if did.krate == ast::LOCAL_CRATE {
|
2012-08-28 17:54:45 -05:00
|
|
|
ty::node_id_to_type(bcx.ccx().tcx, did.node)
|
|
|
|
} else {
|
|
|
|
csearch::get_type(bcx.ccx().tcx, did).ty
|
|
|
|
};
|
2013-04-24 03:29:46 -05:00
|
|
|
callee::trans_call_inner(bcx,
|
|
|
|
None,
|
|
|
|
fty,
|
2014-01-15 13:39:08 -06:00
|
|
|
|bcx, _| {
|
2013-04-24 03:29:46 -05:00
|
|
|
trans_fn_ref_with_vtables_to_callee(bcx,
|
|
|
|
did,
|
|
|
|
0,
|
2013-05-19 00:07:44 -05:00
|
|
|
[],
|
2013-04-24 03:29:46 -05:00
|
|
|
None)
|
|
|
|
},
|
|
|
|
ArgVals(args),
|
2014-01-27 06:18:36 -06:00
|
|
|
dest)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
pub fn trans_lang_call_with_type_params<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-09-01 20:45:37 -05:00
|
|
|
did: ast::DefId,
|
2013-02-27 20:34:04 -06:00
|
|
|
args: &[ValueRef],
|
2013-03-05 19:36:39 -06:00
|
|
|
type_params: &[ty::t],
|
2013-02-27 20:34:04 -06:00
|
|
|
dest: expr::Dest)
|
2014-01-07 10:54:58 -06:00
|
|
|
-> &'a Block<'a> {
|
2012-09-25 14:17:20 -05:00
|
|
|
let fty;
|
2014-02-05 15:15:24 -06:00
|
|
|
if did.krate == ast::LOCAL_CRATE {
|
2012-09-25 14:17:20 -05:00
|
|
|
fty = ty::node_id_to_type(bcx.tcx(), did.node);
|
|
|
|
} else {
|
|
|
|
fty = csearch::get_type(bcx.tcx(), did).ty;
|
|
|
|
}
|
|
|
|
|
|
|
|
return callee::trans_call_inner(
|
2014-01-15 13:39:08 -06:00
|
|
|
bcx,
|
|
|
|
None,
|
|
|
|
fty,
|
|
|
|
|bcx, _| {
|
2012-09-25 14:17:20 -05:00
|
|
|
let callee =
|
2013-01-10 08:29:26 -06:00
|
|
|
trans_fn_ref_with_vtables_to_callee(bcx, did, 0,
|
2013-03-05 19:36:39 -06:00
|
|
|
type_params,
|
2012-09-25 14:17:20 -05:00
|
|
|
None);
|
|
|
|
|
|
|
|
let new_llval;
|
|
|
|
match callee.data {
|
2014-01-27 06:18:36 -06:00
|
|
|
Fn(llfn) => {
|
2012-09-25 14:17:20 -05:00
|
|
|
let substituted = ty::subst_tps(callee.bcx.tcx(),
|
2012-10-08 14:39:30 -05:00
|
|
|
type_params,
|
|
|
|
None,
|
|
|
|
fty);
|
2013-04-12 00:15:30 -05:00
|
|
|
let llfnty = type_of::type_of(callee.bcx.ccx(),
|
2012-09-25 14:17:20 -05:00
|
|
|
substituted);
|
2014-01-27 06:18:36 -06:00
|
|
|
new_llval = PointerCast(callee.bcx, llfn, llfnty);
|
2012-09-25 14:17:20 -05:00
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
_ => fail!()
|
2012-09-25 14:17:20 -05:00
|
|
|
}
|
2014-01-27 06:18:36 -06:00
|
|
|
Callee { bcx: callee.bcx, data: Fn(new_llval) }
|
2012-09-25 14:17:20 -05:00
|
|
|
},
|
2014-01-27 06:18:36 -06:00
|
|
|
ArgVals(args), Some(dest)).bcx;
|
2012-09-25 14:17:20 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
pub fn trans_call_inner<'a>(
|
2014-01-15 13:39:08 -06:00
|
|
|
bcx: &'a Block<'a>,
|
2013-04-18 17:53:29 -05:00
|
|
|
call_info: Option<NodeInfo>,
|
2013-05-21 14:25:44 -05:00
|
|
|
callee_ty: ty::t,
|
2014-01-15 13:39:08 -06:00
|
|
|
get_callee: |bcx: &'a Block<'a>,
|
|
|
|
arg_cleanup_scope: cleanup::ScopeId|
|
|
|
|
-> Callee<'a>,
|
2013-04-18 17:53:29 -05:00
|
|
|
args: CallArgs,
|
2014-01-27 06:18:36 -06:00
|
|
|
dest: Option<expr::Dest>)
|
2014-01-07 10:54:58 -06:00
|
|
|
-> Result<'a> {
|
2013-05-21 14:25:44 -05:00
|
|
|
/*!
|
|
|
|
* This behemoth of a function translates function calls.
|
|
|
|
* Unfortunately, in order to generate more efficient LLVM
|
|
|
|
* output at -O0, it has quite a complex signature (refactoring
|
|
|
|
* this into two functions seems like a good idea).
|
|
|
|
*
|
|
|
|
* In particular, for lang items, it is invoked with a dest of
|
2014-01-15 13:39:08 -06:00
|
|
|
* None, and in that case the return value contains the result of
|
|
|
|
* the fn. The lang item must not return a structural type or else
|
|
|
|
* all heck breaks loose.
|
|
|
|
*
|
|
|
|
* For non-lang items, `dest` is always Some, and hence the result
|
|
|
|
* is written into memory somewhere. Nonetheless we return the
|
|
|
|
* actual return value of the function.
|
2013-05-21 14:25:44 -05:00
|
|
|
*/
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
// Introduce a temporary cleanup scope that will contain cleanups
|
|
|
|
// for the arguments while they are being evaluated. The purpose
|
|
|
|
// this cleanup is to ensure that, should a failure occur while
|
|
|
|
// evaluating argument N, the values for arguments 0...N-1 are all
|
|
|
|
// cleaned up. If no failure occurs, the values are handed off to
|
|
|
|
// the callee, and hence none of the cleanups in this temporary
|
|
|
|
// scope will ever execute.
|
|
|
|
let fcx = bcx.fcx;
|
|
|
|
let ccx = fcx.ccx;
|
|
|
|
let arg_cleanup_scope = fcx.push_custom_cleanup_scope();
|
2013-05-21 14:25:44 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
let callee = get_callee(bcx, cleanup::CustomScope(arg_cleanup_scope));
|
|
|
|
let mut bcx = callee.bcx;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-27 06:18:36 -06:00
|
|
|
let (llfn, llenv, llself) = match callee.data {
|
|
|
|
Fn(llfn) => {
|
|
|
|
(llfn, None, None)
|
|
|
|
}
|
|
|
|
TraitMethod(d) => {
|
|
|
|
(d.llfn, None, Some(d.llself))
|
|
|
|
}
|
|
|
|
Closure(d) => {
|
|
|
|
// Closures are represented as (llfn, llclosure) pair:
|
|
|
|
// load the requisite values out.
|
|
|
|
let pair = d.to_llref();
|
|
|
|
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, Some(llenv), None)
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-04 16:26:51 -06:00
|
|
|
let (abi, ret_ty) = match ty::get(callee_ty).sty {
|
|
|
|
ty::ty_bare_fn(ref f) => (f.abis, f.sig.output),
|
|
|
|
ty::ty_closure(ref f) => (AbiSet::Rust(), f.sig.output),
|
|
|
|
_ => fail!("expected bare rust fn or closure in trans_call_inner")
|
2014-01-15 13:39:08 -06:00
|
|
|
};
|
|
|
|
let is_rust_fn =
|
|
|
|
abi.is_rust() ||
|
|
|
|
abi.is_intrinsic();
|
|
|
|
|
|
|
|
// Generate a location to store the result. If the user does
|
|
|
|
// not care about the result, just make a stack slot.
|
|
|
|
let opt_llretslot = match dest {
|
|
|
|
None => {
|
|
|
|
assert!(!type_of::return_uses_outptr(ccx, ret_ty));
|
|
|
|
None
|
|
|
|
}
|
|
|
|
Some(expr::SaveIn(dst)) => Some(dst),
|
|
|
|
Some(expr::Ignore) => {
|
2014-01-16 18:10:17 -06:00
|
|
|
if !type_is_zero_size(ccx, ret_ty) {
|
2014-01-15 13:39:08 -06:00
|
|
|
Some(alloc_ty(bcx, ret_ty, "__llret"))
|
|
|
|
} else {
|
2014-01-16 14:11:22 -06:00
|
|
|
let llty = type_of::type_of(ccx, ret_ty);
|
|
|
|
Some(C_undef(llty.ptr_to()))
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
};
|
2013-03-19 13:40:34 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
let mut llresult = unsafe {
|
|
|
|
llvm::LLVMGetUndef(Type::nil().ptr_to().to_ref())
|
|
|
|
};
|
2013-04-18 17:53:29 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
// The code below invokes the function, using either the Rust
|
|
|
|
// conventions (if it is a rust fn) or the native conventions
|
|
|
|
// (otherwise). The important part is that, when all is sad
|
|
|
|
// and done, either the return value of the function will have been
|
|
|
|
// written in opt_llretslot (if it is Some) or `llresult` will be
|
|
|
|
// set appropriately (otherwise).
|
|
|
|
if is_rust_fn {
|
2014-03-04 12:02:49 -06:00
|
|
|
let mut llargs = Vec::new();
|
2014-01-15 13:39:08 -06:00
|
|
|
|
|
|
|
// Push the out-pointer if we use an out-pointer for this
|
|
|
|
// return type, otherwise push "undef".
|
|
|
|
if type_of::return_uses_outptr(ccx, ret_ty) {
|
|
|
|
llargs.push(opt_llretslot.unwrap());
|
|
|
|
}
|
2013-05-21 14:25:44 -05:00
|
|
|
|
2014-01-27 06:18:36 -06:00
|
|
|
// Push the environment (or a trait object's self).
|
|
|
|
match (llenv, llself) {
|
|
|
|
(Some(llenv), None) => llargs.push(llenv),
|
|
|
|
(None, Some(llself)) => llargs.push(llself),
|
|
|
|
_ => {}
|
|
|
|
}
|
2013-04-18 17:53:29 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
// Push the arguments.
|
2014-01-27 06:18:36 -06:00
|
|
|
bcx = trans_args(bcx, args, callee_ty, &mut llargs,
|
|
|
|
cleanup::CustomScope(arg_cleanup_scope),
|
|
|
|
llself.is_some());
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
fcx.pop_custom_cleanup_scope(arg_cleanup_scope);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
// A function pointer is called without the declaration
|
|
|
|
// available, so we have to apply any attributes with ABI
|
|
|
|
// implications directly to the call instruction. Right now,
|
|
|
|
// the only attribute we need to worry about is `sret`.
|
2014-03-04 12:02:49 -06:00
|
|
|
let mut attrs = Vec::new();
|
2014-01-15 13:39:08 -06:00
|
|
|
if type_of::return_uses_outptr(ccx, ret_ty) {
|
|
|
|
attrs.push((1, StructRetAttribute));
|
|
|
|
}
|
add sret + noalias to the out pointer parameter
This brings Rust in line with how `clang` handles return pointers.
Example:
pub fn bar() -> [uint, .. 8] {
let a = [0, .. 8];
a
}
Before:
; Function Attrs: nounwind uwtable
define void @_ZN3bar17ha4635c6f704bfa334v0.0E([8 x i64]* nocapture, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #1 {
"function top level":
%a = alloca [8 x i64], align 8
%2 = bitcast [8 x i64]* %a to i8*
call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 64, i32 8, i1 false)
%3 = bitcast [8 x i64]* %0 to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* %2, i64 64, i32 8, i1 false)
ret void
}
After:
; Function Attrs: nounwind uwtable
define void @_ZN3bar17ha4635c6f704bfa334v0.0E([8 x i64]* noalias nocapture sret, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #1 {
"function top level":
%2 = bitcast [8 x i64]* %0 to i8*
call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 64, i32 8, i1 false)
ret void
}
Closes #9072
Closes #7298
Closes #9154
2013-09-10 13:28:59 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
// The `noalias` attribute on the return value is useful to a
|
|
|
|
// function ptr caller.
|
|
|
|
match ty::get(ret_ty).sty {
|
|
|
|
// `~` pointer return values never alias because ownership
|
|
|
|
// is transferred
|
2014-01-27 06:18:36 -06:00
|
|
|
ty::ty_uniq(..) | ty::ty_vec(_, ty::vstore_uniq) => {
|
2014-01-15 13:39:08 -06:00
|
|
|
attrs.push((0, NoAliasAttribute));
|
add sret + noalias to the out pointer parameter
This brings Rust in line with how `clang` handles return pointers.
Example:
pub fn bar() -> [uint, .. 8] {
let a = [0, .. 8];
a
}
Before:
; Function Attrs: nounwind uwtable
define void @_ZN3bar17ha4635c6f704bfa334v0.0E([8 x i64]* nocapture, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #1 {
"function top level":
%a = alloca [8 x i64], align 8
%2 = bitcast [8 x i64]* %a to i8*
call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 64, i32 8, i1 false)
%3 = bitcast [8 x i64]* %0 to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* %2, i64 64, i32 8, i1 false)
ret void
}
After:
; Function Attrs: nounwind uwtable
define void @_ZN3bar17ha4635c6f704bfa334v0.0E([8 x i64]* noalias nocapture sret, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #1 {
"function top level":
%2 = bitcast [8 x i64]* %0 to i8*
call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 64, i32 8, i1 false)
ret void
}
Closes #9072
Closes #7298
Closes #9154
2013-09-10 13:28:59 -05:00
|
|
|
}
|
2014-01-27 06:18:36 -06:00
|
|
|
_ => {}
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
add sret + noalias to the out pointer parameter
This brings Rust in line with how `clang` handles return pointers.
Example:
pub fn bar() -> [uint, .. 8] {
let a = [0, .. 8];
a
}
Before:
; Function Attrs: nounwind uwtable
define void @_ZN3bar17ha4635c6f704bfa334v0.0E([8 x i64]* nocapture, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #1 {
"function top level":
%a = alloca [8 x i64], align 8
%2 = bitcast [8 x i64]* %a to i8*
call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 64, i32 8, i1 false)
%3 = bitcast [8 x i64]* %0 to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* %2, i64 64, i32 8, i1 false)
ret void
}
After:
; Function Attrs: nounwind uwtable
define void @_ZN3bar17ha4635c6f704bfa334v0.0E([8 x i64]* noalias nocapture sret, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #1 {
"function top level":
%2 = bitcast [8 x i64]* %0 to i8*
call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 64, i32 8, i1 false)
ret void
}
Closes #9072
Closes #7298
Closes #9154
2013-09-10 13:28:59 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
// Invoke the actual rust fn and update bcx/llresult.
|
2014-03-08 14:36:22 -06:00
|
|
|
let (llret, b) = base::invoke(bcx,
|
|
|
|
llfn,
|
|
|
|
llargs,
|
|
|
|
attrs.as_slice(),
|
|
|
|
call_info);
|
2014-01-15 13:39:08 -06:00
|
|
|
bcx = b;
|
|
|
|
llresult = llret;
|
|
|
|
|
|
|
|
// If the Rust convention for this type is return via
|
|
|
|
// the return value, copy it into llretslot.
|
|
|
|
match opt_llretslot {
|
|
|
|
Some(llretslot) => {
|
|
|
|
if !type_of::return_uses_outptr(bcx.ccx(), ret_ty) &&
|
2014-01-16 18:10:17 -06:00
|
|
|
!type_is_zero_size(bcx.ccx(), ret_ty)
|
2014-01-15 13:39:08 -06:00
|
|
|
{
|
|
|
|
Store(bcx, llret, llretslot);
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
None => {}
|
2012-10-26 20:23:45 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
} else {
|
|
|
|
// Lang items are the only case where dest is None, and
|
|
|
|
// they are always Rust fns.
|
|
|
|
assert!(dest.is_some());
|
|
|
|
|
2014-03-04 12:02:49 -06:00
|
|
|
let mut llargs = Vec::new();
|
2014-01-15 13:39:08 -06:00
|
|
|
let arg_tys = match args {
|
|
|
|
ArgExprs(a) => a.iter().map(|x| expr_ty(bcx, *x)).collect(),
|
2014-01-27 06:18:36 -06:00
|
|
|
_ => fail!("expected arg exprs.")
|
2014-01-15 13:39:08 -06:00
|
|
|
};
|
2014-03-06 11:24:11 -06:00
|
|
|
bcx = trans_args(bcx, args, callee_ty, &mut llargs,
|
|
|
|
cleanup::CustomScope(arg_cleanup_scope), false);
|
|
|
|
fcx.pop_custom_cleanup_scope(arg_cleanup_scope);
|
|
|
|
bcx = foreign::trans_native_call(bcx, callee_ty,
|
|
|
|
llfn, opt_llretslot.unwrap(),
|
|
|
|
llargs.as_slice(), arg_tys);
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
2013-04-18 17:53:29 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
// If the caller doesn't care about the result of this fn call,
|
|
|
|
// drop the temporary slot we made.
|
|
|
|
match dest {
|
|
|
|
None => {
|
|
|
|
assert!(!type_of::return_uses_outptr(bcx.ccx(), ret_ty));
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
Some(expr::Ignore) => {
|
|
|
|
// drop the value if it is not being saved.
|
|
|
|
bcx = glue::drop_ty(bcx, opt_llretslot.unwrap(), ret_ty);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
Some(expr::SaveIn(_)) => { }
|
|
|
|
}
|
2013-05-21 14:25:44 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
if ty::type_is_bot(ret_ty) {
|
|
|
|
Unreachable(bcx);
|
|
|
|
}
|
|
|
|
|
|
|
|
rslt(bcx, llresult)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
pub enum CallArgs<'a> {
|
|
|
|
ArgExprs(&'a [@ast::Expr]),
|
2014-03-06 11:24:11 -06:00
|
|
|
ArgOverloadedOp(Datum<Expr>, Option<(Datum<Expr>, ast::NodeId)>),
|
2013-12-10 01:16:18 -06:00
|
|
|
ArgVals(&'a [ValueRef])
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-27 06:18:36 -06:00
|
|
|
fn trans_args<'a>(cx: &'a Block<'a>,
|
2013-04-17 11:15:37 -05:00
|
|
|
args: CallArgs,
|
|
|
|
fn_ty: ty::t,
|
2014-03-04 12:02:49 -06:00
|
|
|
llargs: &mut Vec<ValueRef> ,
|
2014-01-27 06:18:36 -06:00
|
|
|
arg_cleanup_scope: cleanup::ScopeId,
|
|
|
|
ignore_self: bool)
|
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_args");
|
2013-03-19 13:40:34 -05:00
|
|
|
let arg_tys = ty::ty_fn_args(fn_ty);
|
2013-10-25 00:56:34 -05:00
|
|
|
let variadic = ty::fn_is_variadic(fn_ty);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-03-19 13:40:34 -05:00
|
|
|
let mut bcx = cx;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// 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 {
|
2014-01-27 06:18:36 -06:00
|
|
|
ArgExprs(arg_exprs) => {
|
|
|
|
let num_formal_args = arg_tys.len();
|
2014-03-06 11:24:11 -06:00
|
|
|
for (i, &arg_expr) in arg_exprs.iter().enumerate() {
|
2014-01-27 06:18:36 -06:00
|
|
|
if i == 0 && ignore_self {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let arg_ty = if i >= num_formal_args {
|
|
|
|
assert!(variadic);
|
2014-03-06 11:24:11 -06:00
|
|
|
expr_ty_adjusted(cx, arg_expr)
|
2014-01-27 06:18:36 -06:00
|
|
|
} else {
|
2014-03-08 14:36:22 -06:00
|
|
|
*arg_tys.get(i)
|
2014-01-27 06:18:36 -06:00
|
|
|
};
|
2014-03-06 11:24:11 -06:00
|
|
|
|
|
|
|
let arg_datum = unpack_datum!(bcx, expr::trans(bcx, arg_expr));
|
2014-01-27 06:18:36 -06:00
|
|
|
llargs.push(unpack_result!(bcx, {
|
2014-03-06 11:24:11 -06:00
|
|
|
trans_arg_datum(bcx, arg_ty, arg_datum,
|
|
|
|
arg_cleanup_scope,
|
|
|
|
DontAutorefArg)
|
2014-01-27 06:18:36 -06:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2014-03-06 11:24:11 -06:00
|
|
|
ArgOverloadedOp(lhs, rhs) => {
|
2014-01-27 06:18:36 -06:00
|
|
|
assert!(!variadic);
|
|
|
|
|
|
|
|
llargs.push(unpack_result!(bcx, {
|
2014-03-06 11:24:11 -06:00
|
|
|
trans_arg_datum(bcx, *arg_tys.get(0), lhs,
|
|
|
|
arg_cleanup_scope,
|
|
|
|
DontAutorefArg)
|
2014-01-27 06:18:36 -06:00
|
|
|
}));
|
|
|
|
|
2014-03-06 11:24:11 -06:00
|
|
|
match rhs {
|
|
|
|
Some((rhs, rhs_id)) => {
|
2014-01-27 06:18:36 -06:00
|
|
|
assert_eq!(arg_tys.len(), 2);
|
|
|
|
|
|
|
|
llargs.push(unpack_result!(bcx, {
|
2014-03-06 11:24:11 -06:00
|
|
|
trans_arg_datum(bcx, *arg_tys.get(1), rhs,
|
|
|
|
arg_cleanup_scope,
|
|
|
|
DoAutorefArg(rhs_id))
|
2014-01-27 06:18:36 -06:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
None => assert_eq!(arg_tys.len(), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ArgVals(vs) => {
|
|
|
|
llargs.push_all(vs);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-24 17:34:20 -05:00
|
|
|
bcx
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub enum AutorefArg {
|
2012-09-19 20:00:26 -05:00
|
|
|
DontAutorefArg,
|
2014-03-06 11:24:11 -06:00
|
|
|
DoAutorefArg(ast::NodeId)
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
|
|
|
|
2014-03-06 11:24:11 -06:00
|
|
|
pub fn trans_arg_datum<'a>(
|
2014-01-07 10:54:58 -06:00
|
|
|
bcx: &'a Block<'a>,
|
2013-04-26 21:13:38 -05:00
|
|
|
formal_arg_ty: ty::t,
|
2014-03-06 11:24:11 -06:00
|
|
|
arg_datum: Datum<Expr>,
|
2014-01-15 13:39:08 -06:00
|
|
|
arg_cleanup_scope: cleanup::ScopeId,
|
2014-01-07 10:54:58 -06:00
|
|
|
autoref_arg: AutorefArg)
|
|
|
|
-> Result<'a> {
|
2014-03-06 11:24:11 -06:00
|
|
|
let _icx = push_ctxt("trans_arg_datum");
|
2014-01-15 13:39:08 -06:00
|
|
|
let mut bcx = bcx;
|
2012-08-28 17:54:45 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
|
2014-03-06 11:24:11 -06:00
|
|
|
debug!("trans_arg_datum({})",
|
|
|
|
formal_arg_ty.repr(bcx.tcx()));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
let arg_datum_ty = arg_datum.ty;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!(" arg datum: {}", arg_datum.to_str(bcx.ccx()));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-09-18 12:41:05 -05:00
|
|
|
let mut val;
|
2014-01-15 13:39:08 -06:00
|
|
|
if ty::type_is_bot(arg_datum_ty) {
|
2012-09-18 12:41:05 -05:00
|
|
|
// 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).
|
2013-04-26 21:13:38 -05:00
|
|
|
let llformal_arg_ty = type_of::type_of(ccx, formal_arg_ty);
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-06-16 05:52:44 -05:00
|
|
|
val = llvm::LLVMGetUndef(llformal_arg_ty.to_ref());
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-09-18 12:41:05 -05:00
|
|
|
} else {
|
2012-09-20 14:29:15 -05:00
|
|
|
// FIXME(#3548) use the adjustments table
|
2012-09-19 20:00:26 -05:00
|
|
|
match autoref_arg {
|
2014-03-06 11:24:11 -06:00
|
|
|
DoAutorefArg(arg_id) => {
|
2014-01-15 13:39:08 -06:00
|
|
|
// We will pass argument by reference
|
|
|
|
// We want an lvalue, so that we can pass by reference and
|
|
|
|
let arg_datum = unpack_datum!(
|
2014-03-06 11:24:11 -06:00
|
|
|
bcx, arg_datum.to_lvalue_datum(bcx, "arg", arg_id));
|
2014-01-15 13:39:08 -06:00
|
|
|
val = arg_datum.val;
|
2013-01-10 12:59:58 -06:00
|
|
|
}
|
2012-09-19 20:00:26 -05:00
|
|
|
DontAutorefArg => {
|
2014-01-15 13:39:08 -06:00
|
|
|
// Make this an rvalue, since we are going to be
|
|
|
|
// passing ownership.
|
|
|
|
let arg_datum = unpack_datum!(
|
|
|
|
bcx, arg_datum.to_rvalue_datum(bcx, "arg"));
|
|
|
|
|
|
|
|
// Now that arg_datum is owned, get it into the appropriate
|
|
|
|
// mode (ref vs value).
|
|
|
|
let arg_datum = unpack_datum!(
|
|
|
|
bcx, arg_datum.to_appropriate_datum(bcx));
|
|
|
|
|
|
|
|
// Technically, ownership of val passes to the callee.
|
|
|
|
// However, we must cleanup should we fail before the
|
|
|
|
// callee is actually invoked.
|
|
|
|
val = arg_datum.add_clean(bcx.fcx, arg_cleanup_scope);
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-09-18 12:41:05 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
if formal_arg_ty != arg_datum_ty {
|
2012-09-18 12:41:05 -05:00
|
|
|
// this could happen due to e.g. subtyping
|
2013-05-21 14:25:44 -05:00
|
|
|
let llformal_arg_ty = type_of::type_of_explicit_arg(ccx, formal_arg_ty);
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("casting actual type ({}) to match formal ({})",
|
2013-06-14 22:16:03 -05:00
|
|
|
bcx.val_to_str(val), bcx.llty_str(llformal_arg_ty));
|
2013-04-26 21:13:38 -05:00
|
|
|
val = PointerCast(bcx, val, llformal_arg_ty);
|
2012-09-14 14:01:43 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-18 12:41:05 -05:00
|
|
|
|
2014-03-06 11:24:11 -06:00
|
|
|
debug!("--- trans_arg_datum passing {}", bcx.val_to_str(val));
|
|
|
|
rslt(bcx, val)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|