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;
|
2013-03-26 15:38:07 -05:00
|
|
|
use lib::llvm::ValueRef;
|
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;
|
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;
|
|
|
|
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;
|
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;
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
use syntax::ast;
|
2013-02-25 13:11:21 -06:00
|
|
|
use syntax::ast_map;
|
2013-07-19 20:42:11 -05:00
|
|
|
use syntax::oldvisit;
|
2012-08-28 17:54:45 -05: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).
|
2013-01-29 19:57:02 -06:00
|
|
|
pub struct FnData {
|
2012-09-07 16:50:47 -05:00
|
|
|
llfn: ValueRef,
|
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,
|
2013-06-29 10:45:59 -05:00
|
|
|
temp_cleanup: Option<ValueRef>,
|
2013-04-24 03:29:46 -05:00
|
|
|
self_mode: ty::SelfMode,
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub enum CalleeData {
|
2012-08-28 17:54:45 -05:00
|
|
|
Closure(Datum),
|
|
|
|
Fn(FnData),
|
|
|
|
Method(MethodData)
|
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub struct Callee {
|
2013-07-17 05:12:08 -05:00
|
|
|
bcx: @mut Block,
|
2012-09-07 16:50:47 -05:00
|
|
|
data: CalleeData
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-07-17 05:12:08 -05:00
|
|
|
pub fn trans(bcx: @mut Block, expr: @ast::expr) -> Callee {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_callee");
|
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
|
|
|
debug!("callee::trans(expr=%s)", 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 {
|
|
|
|
ast::expr_path(_) => {
|
|
|
|
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);
|
|
|
|
|
2013-07-17 05:12:08 -05:00
|
|
|
fn datum_callee(bcx: @mut Block, expr: @ast::expr) -> Callee {
|
2013-02-27 18:28:37 -06:00
|
|
|
let DatumBlock {bcx, datum} = expr::trans_to_datum(bcx, expr);
|
|
|
|
match ty::get(datum.ty).sty {
|
|
|
|
ty::ty_bare_fn(*) => {
|
|
|
|
let llval = datum.to_appropriate_llval(bcx);
|
|
|
|
return Callee {bcx: bcx, data: Fn(FnData {llfn: llval})};
|
|
|
|
}
|
|
|
|
ty::ty_closure(*) => {
|
|
|
|
return Callee {bcx: bcx, data: Closure(datum)};
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(
|
|
|
|
expr.span,
|
|
|
|
fmt!("Type of callee is neither bare-fn nor closure: %s",
|
|
|
|
bcx.ty_to_str(datum.ty)));
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-07-17 05:12:08 -05:00
|
|
|
fn fn_callee(bcx: @mut Block, fd: FnData) -> Callee {
|
2012-08-28 17:54:45 -05:00
|
|
|
return Callee {bcx: bcx, data: Fn(fd)};
|
|
|
|
}
|
|
|
|
|
2013-07-17 05:12:08 -05:00
|
|
|
fn trans_def(bcx: @mut Block, def: ast::def, ref_expr: @ast::expr) -> Callee {
|
2012-08-28 17:54:45 -05:00
|
|
|
match def {
|
2012-10-18 15:29:34 -05:00
|
|
|
ast::def_fn(did, _) | ast::def_static_method(did, None, _) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
fn_callee(bcx, trans_fn_ref(bcx, did, ref_expr.id))
|
|
|
|
}
|
2012-10-18 15:29:34 -05:00
|
|
|
ast::def_static_method(impl_did, Some(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))
|
|
|
|
}
|
|
|
|
ast::def_variant(tid, vid) => {
|
|
|
|
// 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);
|
2012-08-28 17:54:45 -05:00
|
|
|
fn_callee(bcx, trans_fn_ref(bcx, vid, ref_expr.id))
|
|
|
|
}
|
2012-12-10 15:47:54 -06:00
|
|
|
ast::def_struct(def_id) => {
|
2012-10-24 16:36:00 -05:00
|
|
|
fn_callee(bcx, trans_fn_ref(bcx, def_id, ref_expr.id))
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
ast::def_arg(*) |
|
|
|
|
ast::def_local(*) |
|
|
|
|
ast::def_binding(*) |
|
|
|
|
ast::def_upvar(*) |
|
|
|
|
ast::def_self(*) => {
|
2013-02-27 18:28:37 -06:00
|
|
|
datum_callee(bcx, ref_expr)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-03-27 09:26:57 -05:00
|
|
|
ast::def_mod(*) | ast::def_foreign_mod(*) | ast::def_trait(*) |
|
2013-06-21 20:46:34 -05:00
|
|
|
ast::def_static(*) | ast::def_ty(*) | ast::def_prim_ty(*) |
|
2012-10-24 16:36:00 -05:00
|
|
|
ast::def_use(*) | ast::def_typaram_binder(*) |
|
2012-12-10 13:58:37 -06:00
|
|
|
ast::def_region(*) | ast::def_label(*) | ast::def_ty_param(*) |
|
2013-06-14 20:21:47 -05:00
|
|
|
ast::def_self_ty(*) | ast::def_method(*) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
bcx.tcx().sess.span_bug(
|
|
|
|
ref_expr.span,
|
|
|
|
fmt!("Cannot translate def %? \
|
|
|
|
to a callable thing!", def));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 05:12:08 -05:00
|
|
|
pub fn trans_fn_ref_to_callee(bcx: @mut Block,
|
2013-01-29 19:57:02 -06:00
|
|
|
def_id: ast::def_id,
|
2013-07-27 03:25:59 -05:00
|
|
|
ref_id: ast::NodeId) -> Callee {
|
2012-08-28 17:54:45 -05:00
|
|
|
Callee {bcx: bcx,
|
|
|
|
data: Fn(trans_fn_ref(bcx, def_id, ref_id))}
|
|
|
|
}
|
|
|
|
|
2013-07-17 05:12:08 -05:00
|
|
|
pub fn trans_fn_ref(bcx: @mut Block,
|
2013-01-29 19:57:02 -06:00
|
|
|
def_id: ast::def_id,
|
2013-07-27 03:25:59 -05:00
|
|
|
ref_id: ast::NodeId) -> FnData {
|
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
|
|
|
|
|
|
|
let type_params = node_id_type_params(bcx, ref_id);
|
2012-09-10 14:25:45 -05:00
|
|
|
let vtables = node_vtables(bcx, ref_id);
|
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
|
|
|
debug!("trans_fn_ref(def_id=%s, ref_id=%?, type_params=%s, vtables=%s)",
|
|
|
|
def_id.repr(bcx.tcx()), ref_id, type_params.repr(bcx.tcx()),
|
|
|
|
vtables.repr(bcx.tcx()));
|
2012-09-10 14:25:45 -05:00
|
|
|
trans_fn_ref_with_vtables(bcx, def_id, ref_id, type_params, vtables)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn trans_fn_ref_with_vtables_to_callee(
|
2013-07-17 05:12:08 -05:00
|
|
|
bcx: @mut Block,
|
2013-01-29 19:57:02 -06:00
|
|
|
def_id: ast::def_id,
|
2013-07-27 03:25:59 -05:00
|
|
|
ref_id: ast::NodeId,
|
2013-03-05 19:36:39 -06:00
|
|
|
type_params: &[ty::t],
|
2013-01-29 19:57:02 -06:00
|
|
|
vtables: Option<typeck::vtable_res>)
|
|
|
|
-> Callee {
|
2012-08-28 17:54:45 -05:00
|
|
|
Callee {bcx: bcx,
|
|
|
|
data: Fn(trans_fn_ref_with_vtables(bcx, def_id, ref_id,
|
|
|
|
type_params, vtables))}
|
|
|
|
}
|
|
|
|
|
2013-07-17 05:12:08 -05:00
|
|
|
fn resolve_default_method_vtables(bcx: @mut Block,
|
2013-06-28 13:18:09 -05:00
|
|
|
impl_id: ast::def_id,
|
|
|
|
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.
|
|
|
|
let num_method_vtables = method.generics.type_param_defs.len();
|
|
|
|
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()
|
|
|
|
},
|
|
|
|
None => vec::from_elem(num_method_vtables, @~[])
|
|
|
|
};
|
|
|
|
|
2013-07-22 18:40:31 -05:00
|
|
|
let param_vtables = @(*trait_vtables_fixed + method_vtables);
|
|
|
|
|
|
|
|
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(
|
2013-07-17 05:12:08 -05:00
|
|
|
bcx: @mut Block, //
|
2013-01-29 19:57:02 -06:00
|
|
|
def_id: ast::def_id, // def id of fn
|
2013-07-27 03:25:59 -05:00
|
|
|
ref_id: ast::NodeId, // 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
|
2013-01-29 19:57:02 -06:00
|
|
|
-> FnData {
|
2012-08-28 17:54:45 -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
|
2012-09-25 14:17:20 -05:00
|
|
|
// - `ref_id`: node id of the reference to the fn/method, if applicable.
|
|
|
|
// This parameter may be zero; but, if so, the resulting value may not
|
|
|
|
// have the right type, so it must be cast before being used.
|
2012-08-28 17:54:45 -05:00
|
|
|
// - `type_params`: values for each of the fn/method's type parameters
|
|
|
|
// - `vtables`: values for each bound on each of the type parameters
|
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
debug!("trans_fn_ref_with_vtables(bcx=%s, def_id=%s, ref_id=%?, \
|
|
|
|
type_params=%s, vtables=%s)",
|
|
|
|
bcx.to_str(),
|
|
|
|
def_id.repr(bcx.tcx()),
|
|
|
|
ref_id,
|
|
|
|
type_params.repr(bcx.tcx()),
|
|
|
|
vtables.repr(bcx.tcx()));
|
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,
|
2013-06-12 13:05:03 -05:00
|
|
|
tps: /*bad*/ type_params.to_owned() };
|
|
|
|
|
|
|
|
// 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-07-15 19:26:56 -05:00
|
|
|
let impl_id = ty::method(tcx, def_id).container_id;
|
|
|
|
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-06-12 13:05:03 -05:00
|
|
|
debug!("trans_fn_with_vtables - default method: \
|
2013-06-12 16:22:17 -05:00
|
|
|
substs = %s, trait_subst = %s, \
|
2013-06-28 13:18:09 -05:00
|
|
|
first_subst = %s, new_subst = %s, \
|
2013-07-22 18:40:31 -05:00
|
|
|
vtables = %s, \
|
|
|
|
self_vtable = %s, param_vtables = %s",
|
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 = {
|
2013-07-27 03:25:59 -05:00
|
|
|
if def_id.crate != 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.
|
2012-10-26 20:23:45 -05:00
|
|
|
let must_monomorphise;
|
2013-07-15 19:26:56 -05:00
|
|
|
if type_params.len() > 0 || is_default {
|
2012-10-26 20:23:45 -05:00
|
|
|
must_monomorphise = true;
|
2013-07-27 03:25:59 -05:00
|
|
|
} else if def_id.crate == ast::LOCAL_CRATE {
|
2012-10-26 20:23:45 -05:00
|
|
|
let map_node = session::expect(
|
|
|
|
ccx.sess,
|
2013-02-05 21:41:45 -06:00
|
|
|
ccx.tcx.items.find(&def_id.node),
|
2012-10-26 20:23:45 -05:00
|
|
|
|| fmt!("local item should be in ast map"));
|
|
|
|
|
2013-03-23 20:45:27 -05:00
|
|
|
match *map_node {
|
2013-03-13 21:25:28 -05:00
|
|
|
ast_map::node_foreign_item(_, abis, _, _) => {
|
|
|
|
must_monomorphise = abis.is_intrinsic()
|
2012-10-26 20:23:45 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
must_monomorphise = false;
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
2012-10-26 20:23:45 -05:00
|
|
|
} else {
|
|
|
|
must_monomorphise = 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.
|
2013-07-27 03:25:59 -05:00
|
|
|
assert_eq!(def_id.crate, ast::LOCAL_CRATE);
|
2012-08-28 17:54:45 -05: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,
|
2013-07-22 18:40:31 -05:00
|
|
|
vtables, self_vtables,
|
2013-07-15 19:26:56 -05:00
|
|
|
Some(ref_id));
|
2013-06-06 20:54:14 -05:00
|
|
|
let mut val = val;
|
2012-09-25 14:17:20 -05:00
|
|
|
if must_cast && ref_id != 0 {
|
2012-08-28 17:54:45 -05:00
|
|
|
// Monotype of the REFERENCE to the function (type params
|
|
|
|
// are subst'd)
|
|
|
|
let ref_ty = common::node_id_type(bcx, ref_id);
|
|
|
|
|
|
|
|
val = PointerCast(
|
2013-06-15 22:45:48 -05:00
|
|
|
bcx, val, type_of::type_of_fn_from_ty(ccx, ref_ty).ptr_to());
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
return FnData {llfn: val};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the actual function pointer.
|
2013-04-12 00:15:30 -05:00
|
|
|
let val = {
|
2013-07-27 03:25:59 -05:00
|
|
|
if def_id.crate == 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)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return FnData {llfn: val};
|
|
|
|
}
|
|
|
|
|
|
|
|
// ______________________________________________________________________
|
|
|
|
// Translating calls
|
|
|
|
|
2013-07-17 05:12:08 -05:00
|
|
|
pub fn trans_call(in_cx: @mut Block,
|
2013-01-29 19:57:02 -06:00
|
|
|
call_ex: @ast::expr,
|
|
|
|
f: @ast::expr,
|
|
|
|
args: CallArgs,
|
2013-07-27 03:25:59 -05:00
|
|
|
id: ast::NodeId,
|
2013-01-29 19:57:02 -06:00
|
|
|
dest: expr::Dest)
|
2013-07-17 05:12:08 -05:00
|
|
|
-> @mut Block {
|
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,
|
|
|
|
call_ex.info(),
|
|
|
|
expr_ty(in_cx, f),
|
|
|
|
node_id_type(in_cx, id),
|
|
|
|
|cx| trans(cx, f),
|
|
|
|
args,
|
2013-07-08 01:12:01 -05:00
|
|
|
Some(dest),
|
|
|
|
DontAutorefArg).bcx
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-07-17 05:12:08 -05:00
|
|
|
pub fn trans_method_call(in_cx: @mut Block,
|
2013-01-29 19:57:02 -06:00
|
|
|
call_ex: @ast::expr,
|
2013-07-27 03:25:59 -05:00
|
|
|
callee_id: ast::NodeId,
|
2013-01-29 19:57:02 -06:00
|
|
|
rcvr: @ast::expr,
|
|
|
|
args: CallArgs,
|
|
|
|
dest: expr::Dest)
|
2013-07-17 05:12:08 -05:00
|
|
|
-> @mut Block {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_method_call");
|
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
|
|
|
debug!("trans_method_call(call_ex=%s, rcvr=%s)",
|
|
|
|
call_ex.repr(in_cx.tcx()),
|
|
|
|
rcvr.repr(in_cx.tcx()));
|
2012-11-30 13:18:25 -06:00
|
|
|
trans_call_inner(
|
|
|
|
in_cx,
|
|
|
|
call_ex.info(),
|
2013-06-01 17:31:56 -05:00
|
|
|
node_id_type(in_cx, callee_id),
|
2012-11-30 13:18:25 -06:00
|
|
|
expr_ty(in_cx, call_ex),
|
|
|
|
|cx| {
|
2013-05-05 12:50:10 -05:00
|
|
|
match cx.ccx().maps.method_map.find_copy(&call_ex.id) {
|
2013-03-23 17:55:58 -05:00
|
|
|
Some(origin) => {
|
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
|
|
|
debug!("origin for %s: %s",
|
|
|
|
call_ex.repr(in_cx.tcx()),
|
|
|
|
origin.repr(in_cx.tcx()));
|
|
|
|
|
2012-11-30 13:18:25 -06:00
|
|
|
meth::trans_method_callee(cx,
|
2013-06-01 17:31:56 -05:00
|
|
|
callee_id,
|
2012-11-30 13:18:25 -06:00
|
|
|
rcvr,
|
2013-03-23 17:55:58 -05:00
|
|
|
origin)
|
2012-11-30 13:18:25 -06:00
|
|
|
}
|
|
|
|
None => {
|
2013-05-02 11:28:53 -05:00
|
|
|
cx.tcx().sess.span_bug(call_ex.span, "method call expr wasn't in method map")
|
2012-11-30 13:18:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
args,
|
2013-07-08 01:12:01 -05:00
|
|
|
Some(dest),
|
|
|
|
DontAutorefArg).bcx
|
2012-11-30 13:18:25 -06:00
|
|
|
}
|
|
|
|
|
2013-07-17 05:12:08 -05:00
|
|
|
pub fn trans_lang_call(bcx: @mut Block,
|
2013-02-27 20:34:04 -06:00
|
|
|
did: ast::def_id,
|
|
|
|
args: &[ValueRef],
|
2013-07-08 01:12:01 -05:00
|
|
|
dest: Option<expr::Dest>)
|
|
|
|
-> Result {
|
2013-07-27 03:25:59 -05:00
|
|
|
let fty = if did.crate == 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
|
|
|
|
};
|
|
|
|
let rty = ty::ty_fn_ret(fty);
|
2013-04-24 03:29:46 -05:00
|
|
|
callee::trans_call_inner(bcx,
|
|
|
|
None,
|
|
|
|
fty,
|
|
|
|
rty,
|
|
|
|
|bcx| {
|
|
|
|
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),
|
|
|
|
dest,
|
|
|
|
DontAutorefArg)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-07-17 05:12:08 -05:00
|
|
|
pub fn trans_lang_call_with_type_params(bcx: @mut Block,
|
2013-02-27 20:34:04 -06:00
|
|
|
did: ast::def_id,
|
|
|
|
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)
|
2013-07-17 05:12:08 -05:00
|
|
|
-> @mut Block {
|
2012-09-25 14:17:20 -05:00
|
|
|
let fty;
|
2013-07-27 03:25:59 -05:00
|
|
|
if did.crate == 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
let rty = ty::ty_fn_ret(fty);
|
|
|
|
return callee::trans_call_inner(
|
|
|
|
bcx, None, fty, rty,
|
|
|
|
|bcx| {
|
|
|
|
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 {
|
|
|
|
Fn(fn_data) => {
|
|
|
|
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);
|
|
|
|
new_llval = PointerCast(callee.bcx, fn_data.llfn, llfnty);
|
|
|
|
}
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!()
|
2012-09-25 14:17:20 -05:00
|
|
|
}
|
|
|
|
Callee { bcx: callee.bcx, data: Fn(FnData { llfn: new_llval }) }
|
|
|
|
},
|
2013-07-08 01:12:01 -05:00
|
|
|
ArgVals(args), Some(dest), DontAutorefArg).bcx;
|
2012-09-25 14:17:20 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 00:38:55 -05:00
|
|
|
pub fn body_contains_ret(body: &ast::Block) -> bool {
|
2013-02-04 16:02:01 -06:00
|
|
|
let cx = @mut false;
|
2013-07-19 20:42:11 -05:00
|
|
|
oldvisit::visit_block(body, (cx, oldvisit::mk_vt(@oldvisit::Visitor {
|
2013-06-11 21:55:16 -05:00
|
|
|
visit_item: |_i, (_cx, _v)| { },
|
2013-07-19 20:42:11 -05:00
|
|
|
visit_expr: |e: @ast::expr,
|
|
|
|
(cx, v): (@mut bool, oldvisit::vt<@mut bool>)| {
|
2013-02-04 16:02:01 -06:00
|
|
|
if !*cx {
|
2012-08-28 17:54:45 -05:00
|
|
|
match e.node {
|
2013-02-04 16:02:01 -06:00
|
|
|
ast::expr_ret(_) => *cx = true,
|
2013-07-19 20:42:11 -05:00
|
|
|
_ => oldvisit::visit_expr(e, (cx, v)),
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2013-07-19 20:42:11 -05:00
|
|
|
..*oldvisit::default_visitor()
|
2013-06-11 21:55:16 -05:00
|
|
|
})));
|
2013-02-04 16:02:01 -06:00
|
|
|
*cx
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// See [Note-arg-mode]
|
2013-07-17 05:12:08 -05:00
|
|
|
pub fn trans_call_inner(in_cx: @mut Block,
|
2013-04-18 17:53:29 -05:00
|
|
|
call_info: Option<NodeInfo>,
|
|
|
|
fn_expr_ty: ty::t,
|
|
|
|
ret_ty: ty::t,
|
2013-07-17 05:12:08 -05:00
|
|
|
get_callee: &fn(@mut Block) -> Callee,
|
2013-04-18 17:53:29 -05:00
|
|
|
args: CallArgs,
|
2013-07-08 01:12:01 -05:00
|
|
|
dest: Option<expr::Dest>,
|
2013-04-18 17:53:29 -05:00
|
|
|
autoref_arg: AutorefArg)
|
2013-07-08 01:12:01 -05:00
|
|
|
-> Result {
|
|
|
|
do base::with_scope_result(in_cx, call_info, "call") |cx| {
|
2012-08-28 17:54:45 -05:00
|
|
|
let callee = get_callee(cx);
|
|
|
|
let mut bcx = callee.bcx;
|
|
|
|
let ccx = cx.ccx();
|
|
|
|
|
2013-01-10 23:23:07 -06:00
|
|
|
let (llfn, llenv) = unsafe {
|
|
|
|
match callee.data {
|
|
|
|
Fn(d) => {
|
2013-06-16 05:52:44 -05:00
|
|
|
(d.llfn, llvm::LLVMGetUndef(Type::opaque_box(ccx).ptr_to().to_ref()))
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
|
|
|
Method(d) => {
|
|
|
|
// Weird but true: we pass self in the *environment* slot!
|
2013-06-29 09:43:39 -05:00
|
|
|
(d.llfn, d.llself)
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
|
|
|
Closure(d) => {
|
|
|
|
// Closures are represented as (llfn, llclosure) pair:
|
|
|
|
// load the requisite values out.
|
|
|
|
let pair = d.to_ref_llval(bcx);
|
|
|
|
let llfn = GEPi(bcx, pair, [0u, abi::fn_field_code]);
|
|
|
|
let llfn = Load(bcx, llfn);
|
|
|
|
let llenv = GEPi(bcx, pair, [0u, abi::fn_field_box]);
|
|
|
|
let llenv = Load(bcx, llenv);
|
|
|
|
(llfn, llenv)
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-19 13:40:34 -05:00
|
|
|
let llretslot = trans_ret_slot(bcx, fn_expr_ty, dest);
|
|
|
|
|
|
|
|
let mut llargs = ~[];
|
2013-04-18 17:53:29 -05:00
|
|
|
|
2013-06-30 23:02:14 -05:00
|
|
|
if !ty::type_is_immediate(bcx.tcx(), ret_ty) {
|
2013-04-18 17:53:29 -05:00
|
|
|
llargs.push(llretslot);
|
|
|
|
}
|
|
|
|
|
2013-03-19 13:40:34 -05:00
|
|
|
llargs.push(llenv);
|
2013-08-03 22:51:29 -05:00
|
|
|
bcx = trans_args(bcx, args, fn_expr_ty, autoref_arg, &mut llargs);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// Now that the arguments have finished evaluating, we need to revoke
|
2013-06-27 18:40:27 -05:00
|
|
|
// the cleanup for the self argument
|
2012-08-28 17:54:45 -05:00
|
|
|
match callee.data {
|
2013-06-29 10:45:59 -05:00
|
|
|
Method(d) => {
|
2013-08-03 11:45:23 -05:00
|
|
|
for &v in d.temp_cleanup.iter() {
|
2013-06-29 10:45:59 -05:00
|
|
|
revoke_clean(bcx, v);
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2012-10-26 20:23:45 -05:00
|
|
|
// Uncomment this to debug calls.
|
|
|
|
/*
|
2013-07-22 11:03:39 -05:00
|
|
|
printfln!("calling: %s", bcx.val_to_str(llfn));
|
2013-08-03 11:45:23 -05:00
|
|
|
for llarg in llargs.iter() {
|
2013-07-22 11:03:39 -05:00
|
|
|
printfln!("arg: %s", bcx.val_to_str(*llarg));
|
2012-10-26 20:23:45 -05:00
|
|
|
}
|
|
|
|
io::println("---");
|
|
|
|
*/
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
// If the block is terminated, then one or more of the args
|
|
|
|
// has type _|_. Since that means it diverges, the code for
|
|
|
|
// the call itself is unreachable.
|
2013-04-18 17:53:29 -05:00
|
|
|
let (llresult, new_bcx) = base::invoke(bcx, llfn, llargs);
|
|
|
|
bcx = new_bcx;
|
|
|
|
|
|
|
|
match dest {
|
2013-07-08 01:12:01 -05:00
|
|
|
None => { assert!(ty::type_is_immediate(bcx.tcx(), ret_ty)) }
|
|
|
|
Some(expr::Ignore) => {
|
2013-04-18 17:53:29 -05:00
|
|
|
// drop the value if it is not being saved.
|
2013-07-12 03:56:40 -05:00
|
|
|
if ty::type_needs_drop(bcx.tcx(), ret_ty) {
|
|
|
|
if ty::type_is_immediate(bcx.tcx(), ret_ty) {
|
|
|
|
let llscratchptr = alloc_ty(bcx, ret_ty, "__ret");
|
|
|
|
Store(bcx, llresult, llscratchptr);
|
|
|
|
bcx = glue::drop_ty(bcx, llscratchptr, ret_ty);
|
|
|
|
} else {
|
|
|
|
bcx = glue::drop_ty(bcx, llretslot, ret_ty);
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
2013-07-08 01:12:01 -05:00
|
|
|
Some(expr::SaveIn(lldest)) => {
|
2013-04-18 17:53:29 -05:00
|
|
|
// If this is an immediate, store into the result location.
|
|
|
|
// (If this was not an immediate, the result will already be
|
|
|
|
// directly written into the output slot.)
|
2013-06-30 23:02:14 -05:00
|
|
|
if ty::type_is_immediate(bcx.tcx(), ret_ty) {
|
2013-04-18 17:53:29 -05:00
|
|
|
Store(bcx, llresult, lldest);
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-04-18 17:53:29 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
if ty::type_is_bot(ret_ty) {
|
|
|
|
Unreachable(bcx);
|
|
|
|
}
|
2013-07-08 01:12:01 -05:00
|
|
|
rslt(bcx, llresult)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-10 12:59:58 -06:00
|
|
|
|
2013-03-25 15:21:04 -05:00
|
|
|
pub enum CallArgs<'self> {
|
2013-03-14 13:22:51 -05:00
|
|
|
ArgExprs(&'self [@ast::expr]),
|
|
|
|
ArgVals(&'self [ValueRef])
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-07-17 05:12:08 -05:00
|
|
|
pub fn trans_ret_slot(bcx: @mut Block, fn_ty: ty::t, dest: Option<expr::Dest>)
|
2013-04-18 17:53:29 -05:00
|
|
|
-> ValueRef {
|
2012-08-28 17:54:45 -05:00
|
|
|
let retty = ty::ty_fn_ret(fn_ty);
|
2013-04-18 17:53:29 -05:00
|
|
|
|
2013-03-19 13:40:34 -05:00
|
|
|
match dest {
|
2013-07-08 01:12:01 -05:00
|
|
|
Some(expr::SaveIn(dst)) => dst,
|
|
|
|
_ => {
|
2013-07-08 00:42:56 -05:00
|
|
|
if ty::type_is_immediate(bcx.tcx(), retty) {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-06-16 05:52:44 -05:00
|
|
|
llvm::LLVMGetUndef(Type::nil().ptr_to().to_ref())
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
} else {
|
2013-06-20 14:21:37 -05:00
|
|
|
alloc_ty(bcx, retty, "__trans_ret_slot")
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
2013-03-19 13:40:34 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-07-17 05:12:08 -05:00
|
|
|
pub fn trans_args(cx: @mut Block,
|
2013-04-17 11:15:37 -05:00
|
|
|
args: CallArgs,
|
|
|
|
fn_ty: ty::t,
|
|
|
|
autoref_arg: AutorefArg,
|
2013-07-17 05:12:08 -05:00
|
|
|
llargs: &mut ~[ValueRef]) -> @mut Block
|
2013-03-19 13:40:34 -05:00
|
|
|
{
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_args");
|
2013-03-19 13:40:34 -05:00
|
|
|
let mut temp_cleanups = ~[];
|
|
|
|
let arg_tys = ty::ty_fn_args(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 {
|
|
|
|
ArgExprs(arg_exprs) => {
|
2013-08-03 11:45:23 -05:00
|
|
|
for (i, arg_expr) in arg_exprs.iter().enumerate() {
|
2012-08-28 17:54:45 -05:00
|
|
|
let arg_val = unpack_result!(bcx, {
|
2013-04-24 03:29:46 -05:00
|
|
|
trans_arg_expr(bcx,
|
|
|
|
arg_tys[i],
|
|
|
|
ty::ByCopy,
|
|
|
|
*arg_expr,
|
|
|
|
&mut temp_cleanups,
|
2012-09-19 20:00:26 -05:00
|
|
|
autoref_arg)
|
2012-08-28 17:54:45 -05:00
|
|
|
});
|
2012-09-26 19:33:34 -05:00
|
|
|
llargs.push(arg_val);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ArgVals(vs) => {
|
2012-09-26 19:33:34 -05:00
|
|
|
llargs.push_all(vs);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now that all arguments have been successfully built, we can revoke any
|
|
|
|
// temporary cleanups, as they are only needed if argument construction
|
|
|
|
// should fail (for example, cleanup of copy mode args).
|
2013-08-03 11:45:23 -05:00
|
|
|
for c in temp_cleanups.iter() {
|
2012-09-18 23:41:37 -05:00
|
|
|
revoke_clean(bcx, *c)
|
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,
|
|
|
|
DoAutorefArg
|
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
// temp_cleanups: cleanups that should run only if failure occurs before the
|
|
|
|
// call takes place:
|
2013-07-17 05:12:08 -05:00
|
|
|
pub fn trans_arg_expr(bcx: @mut Block,
|
2013-04-26 21:13:38 -05:00
|
|
|
formal_arg_ty: ty::t,
|
2013-04-24 03:29:46 -05:00
|
|
|
self_mode: ty::SelfMode,
|
2013-01-29 19:57:02 -06:00
|
|
|
arg_expr: @ast::expr,
|
2013-04-17 11:15:37 -05:00
|
|
|
temp_cleanups: &mut ~[ValueRef],
|
|
|
|
autoref_arg: AutorefArg) -> Result {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_arg_expr");
|
2012-08-28 17:54:45 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
|
2013-08-03 22:51:29 -05:00
|
|
|
debug!("trans_arg_expr(formal_arg_ty=(%s), self_mode=%?, arg_expr=%s)",
|
2013-04-26 21:13:38 -05:00
|
|
|
formal_arg_ty.repr(bcx.tcx()),
|
2013-04-24 03:29:46 -05:00
|
|
|
self_mode,
|
2013-08-03 22:51:29 -05:00
|
|
|
arg_expr.repr(bcx.tcx()));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-09-18 12:41:05 -05:00
|
|
|
// translate the arg expr to a datum
|
2013-08-03 22:51:29 -05:00
|
|
|
let arg_datumblock = expr::trans_to_datum(bcx, arg_expr);
|
2013-04-30 15:35:01 -05:00
|
|
|
let arg_datum = arg_datumblock.datum;
|
2013-04-12 00:15:30 -05:00
|
|
|
let bcx = arg_datumblock.bcx;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-09-18 12:41:05 -05:00
|
|
|
debug!(" arg datum: %s", 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;
|
|
|
|
if ty::type_is_bot(arg_datum.ty) {
|
|
|
|
// For values of type _|_, we generate an
|
|
|
|
// "undef" value, as such a value should never
|
|
|
|
// be inspected. It's important for the value
|
|
|
|
// to have type lldestty (the callee's expected type).
|
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 {
|
2013-01-10 12:59:58 -06:00
|
|
|
DoAutorefArg => {
|
|
|
|
val = arg_datum.to_ref_llval(bcx);
|
|
|
|
}
|
2012-09-19 20:00:26 -05:00
|
|
|
DontAutorefArg => {
|
2013-08-03 05:35:33 -05:00
|
|
|
let need_scratch = ty::type_needs_drop(bcx.tcx(), arg_datum.ty) ||
|
|
|
|
(bcx.expr_is_lval(arg_expr) &&
|
|
|
|
arg_datum.appropriate_mode(bcx.tcx()).is_by_ref());
|
|
|
|
|
|
|
|
let arg_datum = if need_scratch {
|
|
|
|
let scratch = scratch_datum(bcx, arg_datum.ty, "__self", false);
|
|
|
|
arg_datum.store_to_datum(bcx, INIT, scratch);
|
|
|
|
|
|
|
|
// Technically, ownership of val passes to the callee.
|
|
|
|
// However, we must cleanup should we fail before the
|
|
|
|
// callee is actually invoked.
|
|
|
|
scratch.add_clean(bcx);
|
|
|
|
temp_cleanups.push(scratch.val);
|
|
|
|
|
|
|
|
scratch
|
|
|
|
} else {
|
|
|
|
arg_datum
|
|
|
|
};
|
|
|
|
|
|
|
|
val = match self_mode {
|
2013-06-27 18:40:27 -05:00
|
|
|
ty::ByRef => {
|
2013-08-03 05:35:33 -05:00
|
|
|
debug!("by ref arg with type %s", bcx.ty_to_str(arg_datum.ty));
|
|
|
|
arg_datum.to_ref_llval(bcx)
|
2013-05-24 14:33:46 -05:00
|
|
|
}
|
2013-06-27 18:40:27 -05:00
|
|
|
ty::ByCopy => {
|
2013-08-03 05:35:33 -05:00
|
|
|
debug!("by copy arg with type %s", bcx.ty_to_str(arg_datum.ty));
|
|
|
|
arg_datum.to_appropriate_llval(bcx)
|
2012-09-20 14:29:15 -05:00
|
|
|
}
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-18 12:41:05 -05:00
|
|
|
}
|
|
|
|
|
2013-04-26 21:13:38 -05: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-04-26 21:13:38 -05:00
|
|
|
let llformal_arg_ty = type_of::type_of_explicit_arg(ccx, &formal_arg_ty);
|
2012-09-18 12:41:05 -05:00
|
|
|
debug!("casting actual type (%s) to match formal (%s)",
|
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
|
|
|
|
2013-06-14 22:16:03 -05:00
|
|
|
debug!("--- trans_arg_expr passing %s", bcx.val_to_str(val));
|
2012-09-18 12:41:05 -05:00
|
|
|
return rslt(bcx, val);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|