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
|
|
|
|
2014-06-28 14:55:17 -05:00
|
|
|
use arena::TypedArena;
|
2013-02-25 13:11:21 -06:00
|
|
|
use back::abi;
|
2014-06-28 14:55:17 -05:00
|
|
|
use back::link;
|
2013-02-25 13:11:21 -06:00
|
|
|
use driver::session;
|
2014-05-21 14:07:48 -05:00
|
|
|
use lib::llvm::ValueRef;
|
2013-02-25 13:11:21 -06:00
|
|
|
use lib::llvm::llvm;
|
|
|
|
use metadata::csearch;
|
2014-05-14 14:31:30 -05:00
|
|
|
use middle::def;
|
2014-05-13 10:35:42 -05:00
|
|
|
use middle::subst;
|
2014-05-31 17:53:13 -05:00
|
|
|
use middle::subst::{Subst, VecPerParamSpace};
|
2013-02-25 13:11:21 -06:00
|
|
|
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::*;
|
2014-06-28 14:55:17 -05:00
|
|
|
use middle::trans::datum::{Datum, KindOps};
|
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;
|
2014-06-28 14:55:17 -05:00
|
|
|
use middle::trans::foreign;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::meth;
|
|
|
|
use middle::trans::monomorphize;
|
2014-06-28 14:55:17 -05:00
|
|
|
use middle::trans::type_::Type;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::type_of;
|
|
|
|
use middle::ty;
|
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
|
|
|
|
2014-06-28 14:55:17 -05:00
|
|
|
use std::gc::Gc;
|
2012-08-28 17:54:45 -05:00
|
|
|
use syntax::ast;
|
2014-04-02 03:19:41 -05:00
|
|
|
use synabi = syntax::abi;
|
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 {
|
2014-03-28 12:05:27 -05:00
|
|
|
pub llfn: ValueRef,
|
|
|
|
pub 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> {
|
2014-03-28 12:05:27 -05:00
|
|
|
pub bcx: &'a Block<'a>,
|
|
|
|
pub 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-05-16 12:45:16 -05:00
|
|
|
format!("type of callee is neither bare-fn nor closure: \
|
|
|
|
{}",
|
|
|
|
bcx.ty_to_str(datum.ty)).as_slice());
|
2013-02-27 18:28:37 -06:00
|
|
|
}
|
|
|
|
}
|
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-05-14 14:31:30 -05:00
|
|
|
fn trans_def<'a>(bcx: &'a Block<'a>, def: def::Def, ref_expr: &ast::Expr)
|
2014-01-07 10:54:58 -06:00
|
|
|
-> Callee<'a> {
|
2012-08-28 17:54:45 -05:00
|
|
|
match def {
|
2014-05-14 14:31:30 -05:00
|
|
|
def::DefFn(did, _) |
|
|
|
|
def::DefStaticMethod(did, def::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
|
|
|
}
|
2014-05-14 14:31:30 -05:00
|
|
|
def::DefStaticMethod(impl_did,
|
|
|
|
def::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))
|
|
|
|
}
|
2014-05-14 14:31:30 -05:00
|
|
|
def::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
|
|
|
}
|
2014-05-14 14:31:30 -05:00
|
|
|
def::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
|
|
|
}
|
2014-05-14 14:31:30 -05:00
|
|
|
def::DefStatic(..) |
|
|
|
|
def::DefArg(..) |
|
|
|
|
def::DefLocal(..) |
|
|
|
|
def::DefBinding(..) |
|
|
|
|
def::DefUpvar(..) => {
|
2013-02-27 18:28:37 -06:00
|
|
|
datum_callee(bcx, ref_expr)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-05-14 14:31:30 -05:00
|
|
|
def::DefMod(..) | def::DefForeignMod(..) | def::DefTrait(..) |
|
|
|
|
def::DefTy(..) | def::DefPrimTy(..) |
|
|
|
|
def::DefUse(..) | def::DefTyParamBinder(..) |
|
|
|
|
def::DefRegion(..) | def::DefLabel(..) | def::DefTyParam(..) |
|
|
|
|
def::DefSelfTy(..) | def::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 {:?} \
|
2014-05-16 12:45:16 -05:00
|
|
|
to a callable thing!", def).as_slice());
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2014-05-07 06:20:15 -05:00
|
|
|
* 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-05-07 06:20:15 -05:00
|
|
|
let substs = node_id_substs(bcx, node);
|
2014-03-22 14:44:16 -05:00
|
|
|
let vtable_key = match node {
|
|
|
|
ExprId(id) => MethodCall::expr(id),
|
|
|
|
MethodCall(method_call) => method_call
|
2014-03-06 11:24:11 -06:00
|
|
|
};
|
2014-03-22 14:44:16 -05:00
|
|
|
let vtables = node_vtables(bcx, vtable_key);
|
2014-05-07 06:20:15 -05:00
|
|
|
debug!("trans_fn_ref(def_id={}, node={:?}, substs={}, vtables={})",
|
|
|
|
def_id.repr(bcx.tcx()),
|
|
|
|
node,
|
|
|
|
substs.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-05-07 06:20:15 -05:00
|
|
|
trans_fn_ref_with_vtables(bcx, def_id, node, substs, 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,
|
2014-05-13 10:35:42 -05:00
|
|
|
substs: subst::Substs,
|
2014-05-14 19:53:48 -05:00
|
|
|
vtables: typeck::vtable_res)
|
2014-01-07 10:54:58 -06:00
|
|
|
-> 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),
|
2014-05-07 06:20:15 -05:00
|
|
|
substs, vtables))}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
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,
|
2014-05-13 10:35:42 -05:00
|
|
|
substs: &subst::Substs,
|
2014-05-14 19:53:48 -05:00
|
|
|
impl_vtables: typeck::vtable_res)
|
2014-05-31 17:53:13 -05:00
|
|
|
-> typeck::vtable_res
|
2014-05-14 19:53:48 -05:00
|
|
|
{
|
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.
|
2014-04-20 12:04:57 -05:00
|
|
|
let param_substs = param_substs {
|
2014-05-07 06:20:15 -05:00
|
|
|
substs: (*substs).clone(),
|
2014-05-31 17:53:13 -05:00
|
|
|
vtables: impl_vtables.clone()
|
2014-04-20 12:04:57 -05:00
|
|
|
};
|
2013-06-28 13:18:09 -05:00
|
|
|
|
2014-04-10 06:04:45 -05:00
|
|
|
let mut param_vtables = resolve_vtables_under_param_substs(
|
2014-05-31 17:53:13 -05:00
|
|
|
bcx.tcx(), ¶m_substs, &impl_res);
|
2013-06-28 13:18:09 -05:00
|
|
|
|
|
|
|
// Now we pull any vtables for parameters on the actual method.
|
2014-07-04 09:39:28 -05:00
|
|
|
param_vtables.push_all(subst::FnSpace,
|
|
|
|
impl_vtables.get_slice(subst::FnSpace));
|
2013-07-22 18:40:31 -05:00
|
|
|
|
2014-05-31 17:53:13 -05:00
|
|
|
param_vtables
|
2013-06-28 13:18:09 -05:00
|
|
|
}
|
|
|
|
|
2014-06-28 14:55:17 -05:00
|
|
|
/// Translates the adapter that deconstructs a `Box<Trait>` object into
|
|
|
|
/// `Trait` so that a by-value self method can be called.
|
|
|
|
pub fn trans_unboxing_shim(bcx: &Block,
|
|
|
|
llshimmedfn: ValueRef,
|
|
|
|
method: &ty::Method,
|
|
|
|
method_id: ast::DefId,
|
|
|
|
substs: subst::Substs)
|
|
|
|
-> ValueRef {
|
|
|
|
let _icx = push_ctxt("trans_unboxing_shim");
|
|
|
|
let ccx = bcx.ccx();
|
|
|
|
let tcx = bcx.tcx();
|
|
|
|
|
|
|
|
// Transform the self type to `Box<self_type>`.
|
|
|
|
let self_type = *method.fty.sig.inputs.get(0);
|
|
|
|
let boxed_self_type = ty::mk_uniq(tcx, self_type);
|
|
|
|
let boxed_function_type = ty::FnSig {
|
|
|
|
binder_id: method.fty.sig.binder_id,
|
|
|
|
inputs: method.fty.sig.inputs.iter().enumerate().map(|(i, typ)| {
|
|
|
|
if i == 0 {
|
|
|
|
boxed_self_type
|
|
|
|
} else {
|
|
|
|
*typ
|
|
|
|
}
|
|
|
|
}).collect(),
|
|
|
|
output: method.fty.sig.output,
|
|
|
|
variadic: false,
|
|
|
|
};
|
|
|
|
let boxed_function_type = ty::BareFnTy {
|
|
|
|
fn_style: method.fty.fn_style,
|
|
|
|
abi: method.fty.abi,
|
|
|
|
sig: boxed_function_type,
|
|
|
|
};
|
|
|
|
let boxed_function_type =
|
|
|
|
ty::mk_bare_fn(tcx, boxed_function_type).subst(tcx, &substs);
|
|
|
|
let function_type =
|
|
|
|
ty::mk_bare_fn(tcx, method.fty.clone()).subst(tcx, &substs);
|
|
|
|
|
2014-07-03 10:20:04 -05:00
|
|
|
let function_name = ty::with_path(tcx, method_id, |path| {
|
2014-06-28 14:55:17 -05:00
|
|
|
link::mangle_internal_name_by_path_and_seq(path, "unboxing_shim")
|
|
|
|
});
|
|
|
|
let llfn = decl_internal_rust_fn(ccx,
|
|
|
|
boxed_function_type,
|
|
|
|
function_name.as_slice());
|
|
|
|
|
|
|
|
let block_arena = TypedArena::new();
|
|
|
|
let empty_param_substs = param_substs::empty();
|
|
|
|
let return_type = ty::ty_fn_ret(boxed_function_type);
|
|
|
|
let fcx = new_fn_ctxt(ccx,
|
|
|
|
llfn,
|
|
|
|
-1,
|
|
|
|
false,
|
|
|
|
return_type,
|
|
|
|
&empty_param_substs,
|
|
|
|
None,
|
|
|
|
&block_arena);
|
2014-07-04 18:52:12 -05:00
|
|
|
let mut bcx = init_function(&fcx, false, return_type);
|
2014-06-28 14:55:17 -05:00
|
|
|
|
|
|
|
// Create the substituted versions of the self type.
|
|
|
|
let arg_scope = fcx.push_custom_cleanup_scope();
|
|
|
|
let arg_scope_id = cleanup::CustomScope(arg_scope);
|
|
|
|
let boxed_arg_types = ty::ty_fn_args(boxed_function_type);
|
|
|
|
let boxed_self_type = *boxed_arg_types.get(0);
|
|
|
|
let arg_types = ty::ty_fn_args(function_type);
|
|
|
|
let self_type = *arg_types.get(0);
|
|
|
|
let boxed_self_kind = arg_kind(&fcx, boxed_self_type);
|
|
|
|
|
|
|
|
// Create a datum for self.
|
|
|
|
let llboxedself = unsafe {
|
|
|
|
llvm::LLVMGetParam(fcx.llfn, fcx.arg_pos(0) as u32)
|
|
|
|
};
|
|
|
|
let llboxedself = Datum::new(llboxedself,
|
|
|
|
boxed_self_type,
|
|
|
|
boxed_self_kind);
|
|
|
|
let boxed_self =
|
|
|
|
unpack_datum!(bcx,
|
|
|
|
llboxedself.to_lvalue_datum_in_scope(bcx,
|
|
|
|
"boxedself",
|
|
|
|
arg_scope_id));
|
|
|
|
|
|
|
|
// This `Load` is needed because lvalue data are always by-ref.
|
|
|
|
let llboxedself = Load(bcx, boxed_self.val);
|
|
|
|
|
|
|
|
let llself = if type_is_immediate(ccx, self_type) {
|
|
|
|
let llboxedself = Load(bcx, llboxedself);
|
|
|
|
immediate_rvalue(llboxedself, self_type)
|
|
|
|
} else {
|
|
|
|
let llself = rvalue_scratch_datum(bcx, self_type, "self");
|
|
|
|
memcpy_ty(bcx, llself.val, llboxedself, self_type);
|
|
|
|
llself
|
|
|
|
};
|
|
|
|
|
|
|
|
// Make sure we don't free the box twice!
|
|
|
|
boxed_self.kind.post_store(bcx, boxed_self.val, boxed_self_type);
|
|
|
|
|
|
|
|
// Schedule a cleanup to free the box.
|
|
|
|
fcx.schedule_free_value(arg_scope_id,
|
|
|
|
llboxedself,
|
|
|
|
cleanup::HeapExchange,
|
|
|
|
self_type);
|
|
|
|
|
|
|
|
// Now call the function.
|
|
|
|
let mut llshimmedargs = vec!(llself.val);
|
|
|
|
for i in range(1, arg_types.len()) {
|
|
|
|
llshimmedargs.push(unsafe {
|
|
|
|
llvm::LLVMGetParam(fcx.llfn, fcx.arg_pos(i) as u32)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
bcx = trans_call_inner(bcx,
|
|
|
|
None,
|
|
|
|
function_type,
|
|
|
|
|bcx, _| {
|
|
|
|
Callee {
|
|
|
|
bcx: bcx,
|
|
|
|
data: Fn(llshimmedfn),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ArgVals(llshimmedargs.as_slice()),
|
|
|
|
match fcx.llretptr.get() {
|
|
|
|
None => None,
|
|
|
|
Some(llretptr) => Some(expr::SaveIn(llretptr)),
|
|
|
|
}).bcx;
|
|
|
|
|
|
|
|
bcx = fcx.pop_and_trans_custom_cleanup_scope(bcx, arg_scope);
|
2014-07-05 14:47:14 -05:00
|
|
|
finish_fn(&fcx, bcx, return_type);
|
2014-06-28 14:55:17 -05:00
|
|
|
|
|
|
|
llfn
|
|
|
|
}
|
2013-06-28 13:18:09 -05:00
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn trans_fn_ref_with_vtables(
|
2014-05-31 17:53:13 -05:00
|
|
|
bcx: &Block, //
|
|
|
|
def_id: ast::DefId, // def id of fn
|
|
|
|
node: ExprOrMethodCall, // node id of use of fn; may be zero if N/A
|
|
|
|
substs: subst::Substs, // values for fn's ty params
|
|
|
|
vtables: typeck::vtable_res) // vtables for the call
|
|
|
|
-> 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.
|
2014-05-07 06:20:15 -05:00
|
|
|
* - `substs`: values for each of the fn/method's parameters
|
2013-05-21 14:25:44 -05:00
|
|
|
* - `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();
|
2014-03-15 15:29:34 -05:00
|
|
|
let tcx = bcx.tcx();
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-03-06 11:24:11 -06:00
|
|
|
debug!("trans_fn_ref_with_vtables(bcx={}, def_id={}, node={:?}, \
|
2014-05-07 06:20:15 -05:00
|
|
|
substs={}, 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(),
|
2014-03-15 15:29:34 -05:00
|
|
|
def_id.repr(tcx),
|
2014-03-06 11:24:11 -06:00
|
|
|
node,
|
2014-05-07 06:20:15 -05:00
|
|
|
substs.repr(tcx),
|
2014-03-15 15:29:34 -05:00
|
|
|
vtables.repr(tcx));
|
2012-09-10 14:25:45 -05:00
|
|
|
|
2014-05-31 17:53:13 -05:00
|
|
|
assert!(substs.types.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-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.
|
2014-05-31 17:53:13 -05:00
|
|
|
let (is_default, def_id, substs, vtables) =
|
2013-07-15 19:26:56 -05:00
|
|
|
match ty::provided_source(tcx, def_id) {
|
2014-05-31 17:53:13 -05:00
|
|
|
None => (false, def_id, substs, vtables),
|
2013-07-15 19:26:56 -05:00
|
|
|
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(
|
2014-05-31 17:53:13 -05:00
|
|
|
tcx, &*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
|
|
|
|
2014-04-10 06:04:45 -05:00
|
|
|
debug!("trans_fn_with_vtables - default method: \
|
|
|
|
substs = {}, trait_subst = {}, \
|
|
|
|
first_subst = {}, new_subst = {}, \
|
|
|
|
vtables = {}",
|
|
|
|
substs.repr(tcx), trait_ref.substs.repr(tcx),
|
|
|
|
first_subst.repr(tcx), new_substs.repr(tcx),
|
|
|
|
vtables.repr(tcx));
|
2013-06-28 13:18:09 -05:00
|
|
|
|
2014-05-31 17:53:13 -05:00
|
|
|
let param_vtables =
|
|
|
|
resolve_default_method_vtables(bcx, impl_id, &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: \
|
2014-05-31 17:53:13 -05:00
|
|
|
param_vtables = {}",
|
|
|
|
param_vtables.repr(tcx));
|
2013-06-12 16:22:17 -05:00
|
|
|
|
2014-05-31 17:53:13 -05:00
|
|
|
(true, source_id, new_substs, 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-05-31 17:53:13 -05:00
|
|
|
let must_monomorphise = if !substs.types.is_empty() || is_default {
|
2014-02-13 23:07:09 -06:00
|
|
|
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(
|
2014-03-05 08:36:01 -06:00
|
|
|
ccx.sess(),
|
2014-03-15 15:29:34 -05:00
|
|
|
tcx.map.find(def_id.node),
|
2014-05-25 05:17:19 -05:00
|
|
|
|| "local item should be in ast map".to_string());
|
2014-02-13 23:07:09 -06:00
|
|
|
|
|
|
|
match map_node {
|
|
|
|
ast_map::NodeForeignItem(_) => {
|
2014-04-02 03:19:41 -05:00
|
|
|
tcx.map.get_foreign_abi(def_id.node) == synabi::RustIntrinsic
|
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
|
|
|
|
2014-04-20 23:49:39 -05:00
|
|
|
// Create a monomorphic version of generic functions
|
2012-08-28 17:54:45 -05:00
|
|
|
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-08 10:33:39 -06:00
|
|
|
let opt_ref_id = match node {
|
|
|
|
ExprId(id) => if id != 0 { Some(id) } else { None },
|
|
|
|
MethodCall(_) => None,
|
2014-03-06 11:24:11 -06:00
|
|
|
};
|
|
|
|
|
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,
|
2014-05-31 17:53:13 -05:00
|
|
|
vtables, opt_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) => {
|
2014-04-09 10:18:40 -05:00
|
|
|
let t = bcx.tcx().method_map.borrow().get(&method_call).ty;
|
2014-03-06 11:24:11 -06:00
|
|
|
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);
|
2014-04-09 10:18:40 -05:00
|
|
|
let method_ty = bcx.tcx().method_map.borrow().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 {
|
2014-03-15 15:29:34 -05:00
|
|
|
ty::node_id_to_type(bcx.tcx(), did.node)
|
2012-08-28 17:54:45 -05:00
|
|
|
} else {
|
2014-03-15 15:29:34 -05:00
|
|
|
csearch::get_type(bcx.tcx(), did).ty
|
2012-08-28 17:54:45 -05:00
|
|
|
};
|
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,
|
2014-05-13 10:35:42 -05:00
|
|
|
subst::Substs::empty(),
|
2014-05-31 17:53:13 -05:00
|
|
|
VecPerParamSpace::empty())
|
2013-04-24 03:29:46 -05:00
|
|
|
},
|
|
|
|
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_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 {
|
2014-04-02 03:19:41 -05:00
|
|
|
ty::ty_bare_fn(ref f) => (f.abi, f.sig.output),
|
|
|
|
ty::ty_closure(ref f) => (synabi::Rust, f.sig.output),
|
2014-03-04 16:26:51 -06:00
|
|
|
_ => fail!("expected bare rust fn or closure in trans_call_inner")
|
2014-01-15 13:39:08 -06:00
|
|
|
};
|
2014-04-02 03:19:41 -05:00
|
|
|
let is_rust_fn = abi == synabi::Rust || abi == synabi::RustIntrinsic;
|
2014-01-15 13:39:08 -06:00
|
|
|
|
|
|
|
// 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 {
|
2014-03-15 15:29:34 -05:00
|
|
|
llvm::LLVMGetUndef(Type::nil(ccx).ptr_to().to_ref())
|
2014-01-15 13:39:08 -06:00
|
|
|
};
|
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) {
|
2014-03-18 08:46:43 -05:00
|
|
|
(Some(llenv), None) => {
|
|
|
|
llargs.push(llenv)
|
|
|
|
},
|
2014-01-27 06:18:36 -06:00
|
|
|
(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
|
|
|
// 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,
|
2014-05-21 14:07:48 -05:00
|
|
|
callee_ty,
|
2014-03-08 14:36:22 -06:00
|
|
|
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
|
|
|
{
|
2014-07-05 14:47:14 -05:00
|
|
|
store_ty(bcx, llret, llretslot, ret_ty)
|
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 {
|
2014-05-16 12:15:33 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-05-03 06:14:56 -05:00
|
|
|
Result::new(bcx, llresult)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
pub enum CallArgs<'a> {
|
2014-03-08 10:33:39 -06:00
|
|
|
// Supply value of arguments as a list of expressions that must be
|
|
|
|
// translated. This is used in the common case of `foo(bar, qux)`.
|
2014-05-16 12:15:33 -05:00
|
|
|
ArgExprs(&'a [Gc<ast::Expr>]),
|
2014-03-08 10:33:39 -06:00
|
|
|
|
|
|
|
// Supply value of arguments as a list of LLVM value refs; frequently
|
|
|
|
// used with lang items and so forth, when the argument is an internal
|
|
|
|
// value.
|
|
|
|
ArgVals(&'a [ValueRef]),
|
|
|
|
|
|
|
|
// For overloaded operators: `(lhs, Option(rhs, rhs_id))`. `lhs`
|
|
|
|
// is the left-hand-side and `rhs/rhs_id` is the datum/expr-id of
|
|
|
|
// the right-hand-side (if any).
|
2014-03-06 11:24:11 -06:00
|
|
|
ArgOverloadedOp(Datum<Expr>, Option<(Datum<Expr>, ast::NodeId)>),
|
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-05-16 12:15:33 -05: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-05-16 12:15:33 -05: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
|
|
|
|
2014-05-16 12:15:33 -05: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).
|
2014-06-04 11:25:30 -05:00
|
|
|
let llformal_arg_ty = type_of::type_of_explicit_arg(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));
|
2014-05-03 06:14:56 -05:00
|
|
|
Result::new(bcx, val)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|