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.
|
|
|
|
|
2012-08-28 17:54:45 -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.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
use core::prelude::*;
|
2013-06-26 19:20:53 -05:00
|
|
|
use core::vec;
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-02-25 13:11:21 -06:00
|
|
|
use back::abi;
|
|
|
|
use driver::session;
|
|
|
|
use lib;
|
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;
|
|
|
|
use middle::trans::closure;
|
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;
|
2012-12-13 15:05:22 -06:00
|
|
|
use syntax::visit;
|
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,
|
|
|
|
self_ty: ty::t,
|
2013-04-24 03:29:46 -05:00
|
|
|
self_mode: ty::SelfMode,
|
2013-05-24 14:33:46 -05:00
|
|
|
explicit_self: ast::explicit_self_
|
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 {
|
2012-09-07 16:50:47 -05:00
|
|
|
bcx: block,
|
|
|
|
data: CalleeData
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn trans(bcx: 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);
|
|
|
|
|
|
|
|
fn datum_callee(bcx: block, expr: @ast::expr) -> Callee {
|
|
|
|
let DatumBlock {bcx, datum} = expr::trans_to_datum(bcx, expr);
|
|
|
|
match ty::get(datum.ty).sty {
|
|
|
|
ty::ty_bare_fn(*) => {
|
|
|
|
let llval = datum.to_appropriate_llval(bcx);
|
|
|
|
return Callee {bcx: bcx, data: Fn(FnData {llfn: llval})};
|
|
|
|
}
|
|
|
|
ty::ty_closure(*) => {
|
|
|
|
return Callee {bcx: bcx, data: Closure(datum)};
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(
|
|
|
|
expr.span,
|
|
|
|
fmt!("Type of callee is neither bare-fn nor closure: %s",
|
|
|
|
bcx.ty_to_str(datum.ty)));
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fn_callee(bcx: block, fd: FnData) -> Callee {
|
|
|
|
return Callee {bcx: bcx, data: Fn(fd)};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_def(bcx: block, def: ast::def, ref_expr: @ast::expr) -> Callee {
|
|
|
|
match def {
|
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-01-29 19:57:02 -06:00
|
|
|
pub fn trans_fn_ref_to_callee(bcx: block,
|
|
|
|
def_id: ast::def_id,
|
|
|
|
ref_id: ast::node_id) -> Callee {
|
2012-08-28 17:54:45 -05:00
|
|
|
Callee {bcx: bcx,
|
|
|
|
data: Fn(trans_fn_ref(bcx, def_id, ref_id))}
|
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn trans_fn_ref(bcx: block,
|
|
|
|
def_id: ast::def_id,
|
|
|
|
ref_id: ast::node_id) -> 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(
|
|
|
|
bcx: block,
|
|
|
|
def_id: ast::def_id,
|
|
|
|
ref_id: ast::node_id,
|
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-06-28 13:18:09 -05:00
|
|
|
fn get_impl_resolutions(bcx: block,
|
|
|
|
impl_id: ast::def_id)
|
|
|
|
-> typeck::vtable_res {
|
|
|
|
if impl_id.crate == ast::local_crate {
|
|
|
|
*bcx.ccx().maps.vtable_map.get(&impl_id.node)
|
|
|
|
} else {
|
|
|
|
// XXX: This is a temporary hack to work around not properly
|
|
|
|
// exporting information about resolutions for impls.
|
|
|
|
// This doesn't actually work if the trait has param bounds,
|
|
|
|
// but it does allow us to survive the case when it does not.
|
|
|
|
let trait_ref = ty::impl_trait_ref(bcx.tcx(), impl_id).get();
|
|
|
|
@vec::from_elem(trait_ref.substs.tps.len(), @~[])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_default_method_vtables(bcx: block,
|
|
|
|
impl_id: ast::def_id,
|
|
|
|
method: &ty::Method,
|
|
|
|
substs: &ty::substs,
|
|
|
|
impl_vtables: Option<typeck::vtable_res>)
|
|
|
|
-> typeck::vtable_res {
|
|
|
|
|
|
|
|
// Get the vtables that the impl implements the trait at
|
|
|
|
let trait_vtables = get_impl_resolutions(bcx, impl_id);
|
|
|
|
|
|
|
|
// Build up a param_substs that we are going to resolve the
|
|
|
|
// trait_vtables under.
|
|
|
|
let param_substs = Some(@param_substs {
|
|
|
|
tys: copy substs.tps,
|
|
|
|
self_ty: substs.self_ty,
|
|
|
|
vtables: impl_vtables,
|
|
|
|
self_vtable: None
|
|
|
|
});
|
|
|
|
|
|
|
|
let trait_vtables_fixed = resolve_vtables_under_param_substs(
|
|
|
|
bcx.tcx(), param_substs, trait_vtables);
|
|
|
|
|
|
|
|
// 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, @~[])
|
|
|
|
};
|
|
|
|
|
|
|
|
@(*trait_vtables_fixed + method_vtables)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn trans_fn_ref_with_vtables(
|
|
|
|
bcx: block, //
|
|
|
|
def_id: ast::def_id, // def id of fn
|
|
|
|
ref_id: ast::node_id, // node id of use of fn; may be zero if N/A
|
2013-03-05 19:36:39 -06:00
|
|
|
type_params: &[ty::t], // values for fn's ty params
|
2013-01-29 19:57:02 -06:00
|
|
|
vtables: Option<typeck::vtable_res>)
|
|
|
|
-> 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-06-25 16:07:44 -05:00
|
|
|
// For simplicity, we want to use the Subst trait when composing
|
|
|
|
// substitutions for default methods. The subst trait does
|
|
|
|
// substitutions with regions, though, so we put a dummy self
|
|
|
|
// region parameter in to keep it from failing. This is a hack.
|
|
|
|
let substs = ty::substs { self_r: Some(ty::re_empty),
|
|
|
|
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-06-28 13:18:09 -05:00
|
|
|
let (def_id, opt_impl_did, substs, self_vtable, vtables) =
|
2013-06-20 11:29:24 -05:00
|
|
|
match tcx.provided_method_sources.find(&def_id) {
|
2013-06-28 13:18:09 -05:00
|
|
|
None => (def_id, None, substs, None, vtables),
|
2013-06-12 13:05:03 -05:00
|
|
|
Some(source) => {
|
|
|
|
// 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-06-12 16:22:17 -05:00
|
|
|
let trait_ref = ty::impl_trait_ref(tcx, source.impl_id)
|
|
|
|
.expect("could not find trait_ref for impl with \
|
|
|
|
default methods");
|
|
|
|
let method = ty::method(tcx, source.method_id);
|
|
|
|
|
2013-06-20 11:29:24 -05:00
|
|
|
// Get all of the type params for the receiver
|
|
|
|
let param_defs = method.generics.type_param_defs;
|
|
|
|
let receiver_substs =
|
|
|
|
type_params.initn(param_defs.len()).to_owned();
|
|
|
|
let receiver_vtables = match vtables {
|
|
|
|
None => @~[],
|
|
|
|
Some(call_vtables) => {
|
2013-06-26 19:20:53 -05:00
|
|
|
@call_vtables.initn(param_defs.len()).to_owned()
|
2013-06-20 11:29:24 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let self_vtable =
|
|
|
|
typeck::vtable_static(source.impl_id, receiver_substs,
|
|
|
|
receiver_vtables);
|
2013-06-12 16:22:17 -05:00
|
|
|
// Compute the first substitution
|
|
|
|
let first_subst = make_substs_for_receiver_types(
|
|
|
|
tcx, source.impl_id, trait_ref, method);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
|
let vtables =
|
|
|
|
resolve_default_method_vtables(bcx, source.impl_id,
|
|
|
|
method, &new_substs, vtables);
|
|
|
|
|
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, \
|
|
|
|
self_vtable = %s, 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),
|
|
|
|
self_vtable.repr(tcx), vtables.repr(tcx));
|
2013-06-12 16:22:17 -05:00
|
|
|
|
2013-06-20 11:29:24 -05:00
|
|
|
(source.method_id, Some(source.impl_id),
|
2013-06-28 13:18:09 -05:00
|
|
|
new_substs, Some(self_vtable), Some(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 = {
|
|
|
|
if def_id.crate != ast::local_crate {
|
2012-10-08 14:39:30 -05:00
|
|
|
let may_translate = opt_impl_did.is_none();
|
|
|
|
inline::maybe_instantiate_inline(ccx, def_id, may_translate)
|
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;
|
|
|
|
if type_params.len() > 0 || opt_impl_did.is_some() {
|
|
|
|
must_monomorphise = true;
|
|
|
|
} else if def_id.crate == ast::local_crate {
|
|
|
|
let map_node = session::expect(
|
|
|
|
ccx.sess,
|
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-05-18 21:02:45 -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-06-20 11:29:24 -05:00
|
|
|
vtables, self_vtable,
|
|
|
|
opt_impl_did, 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 = {
|
2012-08-28 17:54:45 -05:00
|
|
|
if def_id.crate == ast::local_crate {
|
|
|
|
// Internal reference.
|
|
|
|
get_item_val(ccx, def_id.node)
|
|
|
|
} else {
|
|
|
|
// External reference.
|
|
|
|
trans_external_path(ccx, def_id, fn_tpt.ty)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return FnData {llfn: val};
|
|
|
|
}
|
|
|
|
|
|
|
|
// ______________________________________________________________________
|
|
|
|
// Translating calls
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn trans_call(in_cx: block,
|
|
|
|
call_ex: @ast::expr,
|
|
|
|
f: @ast::expr,
|
|
|
|
args: CallArgs,
|
|
|
|
id: ast::node_id,
|
|
|
|
dest: expr::Dest)
|
2013-04-18 17:53:29 -05:00
|
|
|
-> 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,
|
|
|
|
dest,
|
|
|
|
DontAutorefArg)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn trans_method_call(in_cx: block,
|
|
|
|
call_ex: @ast::expr,
|
2013-06-01 17:31:56 -05:00
|
|
|
callee_id: ast::node_id,
|
2013-01-29 19:57:02 -06:00
|
|
|
rcvr: @ast::expr,
|
|
|
|
args: CallArgs,
|
|
|
|
dest: expr::Dest)
|
2013-04-18 17:53:29 -05:00
|
|
|
-> 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,
|
|
|
|
dest,
|
|
|
|
DontAutorefArg)
|
|
|
|
}
|
|
|
|
|
2013-02-27 20:34:04 -06:00
|
|
|
pub fn trans_lang_call(bcx: block,
|
|
|
|
did: ast::def_id,
|
|
|
|
args: &[ValueRef],
|
|
|
|
dest: expr::Dest)
|
|
|
|
-> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
let fty = if did.crate == ast::local_crate {
|
|
|
|
ty::node_id_to_type(bcx.ccx().tcx, did.node)
|
|
|
|
} else {
|
|
|
|
csearch::get_type(bcx.ccx().tcx, did).ty
|
|
|
|
};
|
|
|
|
let rty = ty::ty_fn_ret(fty);
|
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-02-27 20:34:04 -06:00
|
|
|
pub fn trans_lang_call_with_type_params(bcx: block,
|
|
|
|
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)
|
|
|
|
-> block {
|
2012-09-25 14:17:20 -05:00
|
|
|
let fty;
|
|
|
|
if did.crate == ast::local_crate {
|
|
|
|
fty = ty::node_id_to_type(bcx.tcx(), did.node);
|
|
|
|
} else {
|
|
|
|
fty = csearch::get_type(bcx.tcx(), did).ty;
|
|
|
|
}
|
|
|
|
|
|
|
|
let rty = ty::ty_fn_ret(fty);
|
|
|
|
return callee::trans_call_inner(
|
|
|
|
bcx, None, fty, rty,
|
|
|
|
|bcx| {
|
|
|
|
let callee =
|
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 }) }
|
|
|
|
},
|
|
|
|
ArgVals(args), dest, DontAutorefArg);
|
|
|
|
}
|
|
|
|
|
2013-02-18 00:20:36 -06:00
|
|
|
pub fn body_contains_ret(body: &ast::blk) -> bool {
|
2013-02-04 16:02:01 -06:00
|
|
|
let cx = @mut false;
|
2013-06-11 21:55:16 -05:00
|
|
|
visit::visit_block(body, (cx, visit::mk_vt(@visit::Visitor {
|
|
|
|
visit_item: |_i, (_cx, _v)| { },
|
|
|
|
visit_expr: |e: @ast::expr, (cx, v): (@mut bool, visit::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-06-11 21:55:16 -05:00
|
|
|
_ => visit::visit_expr(e, (cx, v)),
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
..*visit::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-04-17 11:15:37 -05:00
|
|
|
pub fn trans_call_inner(in_cx: block,
|
2013-04-18 17:53:29 -05:00
|
|
|
call_info: Option<NodeInfo>,
|
|
|
|
fn_expr_ty: ty::t,
|
|
|
|
ret_ty: ty::t,
|
|
|
|
get_callee: &fn(block) -> Callee,
|
|
|
|
args: CallArgs,
|
|
|
|
dest: expr::Dest,
|
|
|
|
autoref_arg: AutorefArg)
|
|
|
|
-> block {
|
2013-05-02 03:16:07 -05:00
|
|
|
do base::with_scope(in_cx, call_info, "call") |cx| {
|
2013-01-10 12:59:58 -06:00
|
|
|
let ret_in_loop = match args {
|
2012-08-28 17:54:45 -05:00
|
|
|
ArgExprs(args) => {
|
2013-06-27 07:36:27 -05:00
|
|
|
args.len() > 0u && match args.last().node {
|
2013-01-15 15:51:43 -06:00
|
|
|
ast::expr_loop_body(@ast::expr {
|
2013-01-10 12:59:58 -06:00
|
|
|
node: ast::expr_fn_block(_, ref body),
|
2012-08-28 17:54:45 -05:00
|
|
|
_
|
2013-02-18 00:20:36 -06:00
|
|
|
}) => body_contains_ret(body),
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
};
|
|
|
|
|
|
|
|
let callee = get_callee(cx);
|
|
|
|
let mut bcx = callee.bcx;
|
|
|
|
let ccx = cx.ccx();
|
|
|
|
let ret_flag = if ret_in_loop {
|
2013-06-15 22:45:48 -05:00
|
|
|
let flag = alloca(bcx, Type::bool());
|
2012-08-28 17:54:45 -05:00
|
|
|
Store(bcx, C_bool(false), flag);
|
|
|
|
Some(flag)
|
2013-02-06 16:28:02 -06:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2012-08-28 17:54:45 -05:00
|
|
|
|
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-04-24 03:29:46 -05:00
|
|
|
let llself = PointerCast(bcx,
|
|
|
|
d.llself,
|
2013-06-15 22:45:48 -05:00
|
|
|
Type::opaque_box(ccx).ptr_to());
|
2013-01-10 23:23:07 -06:00
|
|
|
(d.llfn, llself)
|
|
|
|
}
|
|
|
|
Closure(d) => {
|
|
|
|
// Closures are represented as (llfn, llclosure) pair:
|
|
|
|
// load the requisite values out.
|
|
|
|
let pair = d.to_ref_llval(bcx);
|
|
|
|
let llfn = GEPi(bcx, pair, [0u, abi::fn_field_code]);
|
|
|
|
let llfn = Load(bcx, llfn);
|
|
|
|
let llenv = GEPi(bcx, pair, [0u, abi::fn_field_box]);
|
|
|
|
let llenv = Load(bcx, llenv);
|
|
|
|
(llfn, llenv)
|
|
|
|
}
|
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-05-22 01:17:04 -05:00
|
|
|
if !ty::type_is_immediate(ret_ty) {
|
2013-04-18 17:53:29 -05:00
|
|
|
llargs.push(llretslot);
|
|
|
|
}
|
|
|
|
|
2013-03-19 13:40:34 -05:00
|
|
|
llargs.push(llenv);
|
|
|
|
bcx = trans_args(bcx, args, fn_expr_ty,
|
|
|
|
ret_flag, autoref_arg, &mut llargs);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
|
|
|
|
// Now that the arguments have finished evaluating, we need to revoke
|
|
|
|
// the cleanup for the self argument, if it exists
|
|
|
|
match callee.data {
|
2013-05-24 14:33:46 -05:00
|
|
|
Method(d) if d.self_mode == ty::ByCopy ||
|
|
|
|
d.explicit_self == ast::sty_value => {
|
2012-08-28 17:54:45 -05:00
|
|
|
revoke_clean(bcx, d.llself);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2012-10-26 20:23:45 -05:00
|
|
|
// Uncomment this to debug calls.
|
|
|
|
/*
|
2013-06-14 22:16:03 -05:00
|
|
|
io::println(fmt!("calling: %s", bcx.val_to_str(llfn)));
|
2013-06-21 07:29:53 -05:00
|
|
|
for llargs.iter().advance |llarg| {
|
2013-06-14 22:16:03 -05:00
|
|
|
io::println(fmt!("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 {
|
2012-08-28 17:54:45 -05:00
|
|
|
expr::Ignore => {
|
2013-04-18 17:53:29 -05:00
|
|
|
// drop the value if it is not being saved.
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
if llvm::LLVMIsUndef(llretslot) != lib::llvm::True {
|
2013-05-02 17:33:08 -05:00
|
|
|
if ty::type_is_nil(ret_ty) {
|
|
|
|
// When implementing the for-loop sugar syntax, the
|
|
|
|
// type of the for-loop is nil, but the function
|
|
|
|
// it's invoking returns a bool. This is a special
|
|
|
|
// case to ignore instead of invoking the Store
|
|
|
|
// below into a scratch pointer of a mismatched
|
|
|
|
// type.
|
|
|
|
} else if ty::type_is_immediate(ret_ty) {
|
2013-04-18 17:53:29 -05:00
|
|
|
let llscratchptr = alloc_ty(bcx, ret_ty);
|
|
|
|
Store(bcx, llresult, llscratchptr);
|
|
|
|
bcx = glue::drop_ty(bcx, llscratchptr, ret_ty);
|
|
|
|
} else {
|
|
|
|
bcx = glue::drop_ty(bcx, llretslot, ret_ty);
|
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-18 17:53:29 -05:00
|
|
|
expr::SaveIn(lldest) => {
|
|
|
|
// If this is an immediate, store into the result location.
|
|
|
|
// (If this was not an immediate, the result will already be
|
|
|
|
// directly written into the output slot.)
|
|
|
|
if ty::type_is_immediate(ret_ty) {
|
|
|
|
Store(bcx, llresult, lldest);
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
} else if ret_in_loop {
|
2013-02-06 16:28:02 -06:00
|
|
|
let ret_flag_result = bool_to_i1(bcx, Load(bcx, ret_flag.get()));
|
|
|
|
bcx = do with_cond(bcx, ret_flag_result) |bcx| {
|
2013-06-10 16:50:12 -05:00
|
|
|
{
|
|
|
|
let r = (copy bcx.fcx.loop_ret);
|
|
|
|
for r.iter().advance |&(flagptr, _)| {
|
|
|
|
Store(bcx, C_bool(true), flagptr);
|
|
|
|
Store(bcx, C_bool(false), bcx.fcx.llretptr.get());
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
base::cleanup_and_leave(bcx, None, Some(bcx.fcx.llreturn));
|
|
|
|
Unreachable(bcx);
|
|
|
|
bcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-17 11:15:37 -05:00
|
|
|
pub fn trans_ret_slot(bcx: block, fn_ty: ty::t, dest: 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 {
|
2012-08-28 17:54:45 -05:00
|
|
|
expr::SaveIn(dst) => dst,
|
|
|
|
expr::Ignore => {
|
|
|
|
if ty::type_is_nil(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 {
|
|
|
|
alloc_ty(bcx, retty)
|
|
|
|
}
|
|
|
|
}
|
2013-03-19 13:40:34 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-04-17 11:15:37 -05:00
|
|
|
pub fn trans_args(cx: block,
|
|
|
|
args: CallArgs,
|
|
|
|
fn_ty: ty::t,
|
|
|
|
ret_flag: Option<ValueRef>,
|
|
|
|
autoref_arg: AutorefArg,
|
|
|
|
llargs: &mut ~[ValueRef]) -> 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) => {
|
|
|
|
let last = arg_exprs.len() - 1u;
|
2013-06-17 18:43:22 -05:00
|
|
|
for arg_exprs.iter().enumerate().advance |(i, arg_expr)| {
|
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,
|
2013-05-24 14:33:46 -05:00
|
|
|
ast::sty_static,
|
2013-04-24 03:29:46 -05:00
|
|
|
*arg_expr,
|
|
|
|
&mut temp_cleanups,
|
2012-09-19 20:00:26 -05:00
|
|
|
if i == last { ret_flag } else { None },
|
|
|
|
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-06-24 17:34:20 -05:00
|
|
|
for temp_cleanups.iter().advance |c| {
|
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-01-29 19:57:02 -06:00
|
|
|
pub fn trans_arg_expr(bcx: 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-05-24 14:33:46 -05:00
|
|
|
ex_self: ast::explicit_self_,
|
2013-01-29 19:57:02 -06:00
|
|
|
arg_expr: @ast::expr,
|
2013-04-17 11:15:37 -05:00
|
|
|
temp_cleanups: &mut ~[ValueRef],
|
|
|
|
ret_flag: Option<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-05-24 14:33:46 -05:00
|
|
|
debug!("trans_arg_expr(formal_arg_ty=(%s), explicit_self=%? self_mode=%?, arg_expr=%s, \
|
2012-09-11 23:25:01 -05:00
|
|
|
ret_flag=%?)",
|
2013-04-26 21:13:38 -05:00
|
|
|
formal_arg_ty.repr(bcx.tcx()),
|
2013-05-24 14:33:46 -05:00
|
|
|
ex_self,
|
2013-04-24 03:29:46 -05:00
|
|
|
self_mode,
|
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
|
|
|
arg_expr.repr(bcx.tcx()),
|
2013-06-14 22:16:03 -05:00
|
|
|
ret_flag.map(|v| bcx.val_to_str(*v)));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-09-18 12:41:05 -05:00
|
|
|
// translate the arg expr to a datum
|
|
|
|
let arg_datumblock = match ret_flag {
|
|
|
|
None => expr::trans_to_datum(bcx, arg_expr),
|
|
|
|
|
|
|
|
// If there is a ret_flag, this *must* be a loop body
|
|
|
|
Some(_) => {
|
|
|
|
match arg_expr.node {
|
|
|
|
ast::expr_loop_body(
|
2013-01-31 19:12:29 -06:00
|
|
|
blk @ @ast::expr {
|
|
|
|
node: ast::expr_fn_block(ref decl, ref body),
|
2013-01-07 16:16:52 -06:00
|
|
|
_
|
2013-04-24 03:29:46 -05:00
|
|
|
}) => {
|
2013-01-08 02:23:25 -06:00
|
|
|
let scratch_ty = expr_ty(bcx, arg_expr);
|
2012-09-18 12:41:05 -05:00
|
|
|
let scratch = alloc_ty(bcx, scratch_ty);
|
|
|
|
let arg_ty = expr_ty(bcx, arg_expr);
|
2013-01-31 19:12:29 -06:00
|
|
|
let sigil = ty::ty_closure_sigil(arg_ty);
|
2012-09-18 12:41:05 -05:00
|
|
|
let bcx = closure::trans_expr_fn(
|
2013-01-31 19:12:29 -06:00
|
|
|
bcx, sigil, decl, body, arg_expr.id,
|
2013-01-10 12:59:58 -06:00
|
|
|
blk.id, Some(ret_flag), expr::SaveIn(scratch));
|
2012-09-18 12:41:05 -05:00
|
|
|
DatumBlock {bcx: bcx,
|
|
|
|
datum: Datum {val: scratch,
|
|
|
|
ty: scratch_ty,
|
2013-05-29 14:49:23 -05:00
|
|
|
mode: ByRef(RevokeClean)}}
|
2012-09-18 12:41:05 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bcx.sess().impossible_case(
|
2013-05-19 00:07:44 -05:00
|
|
|
arg_expr.span, "ret_flag with non-loop-body expr");
|
2012-09-18 12:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-14 14:01:43 -05:00
|
|
|
}
|
2012-09-18 12:41:05 -05:00
|
|
|
};
|
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 => {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!
|
2013-03-22 21:26:41 -05:00
|
|
|
bcx.ccx().maps.moves_map.contains(&arg_expr.id));
|
2013-01-10 12:59:58 -06:00
|
|
|
val = arg_datum.to_ref_llval(bcx);
|
|
|
|
}
|
2012-09-19 20:00:26 -05:00
|
|
|
DontAutorefArg => {
|
2013-05-24 14:33:46 -05:00
|
|
|
match (self_mode, ex_self) {
|
|
|
|
(ty::ByRef, ast::sty_value) => {
|
|
|
|
debug!("by value self with type %s, storing to scratch",
|
|
|
|
bcx.ty_to_str(arg_datum.ty));
|
|
|
|
let scratch = scratch_datum(bcx, arg_datum.ty, false);
|
|
|
|
|
|
|
|
arg_datum.store_to_datum(bcx,
|
|
|
|
arg_expr.id,
|
|
|
|
INIT,
|
|
|
|
scratch);
|
|
|
|
|
|
|
|
// Technically, ownership of val passes to the callee.
|
|
|
|
// However, we must cleanup should we fail before the
|
|
|
|
// callee is actually invoked.
|
|
|
|
scratch.add_clean(bcx);
|
|
|
|
temp_cleanups.push(scratch.val);
|
|
|
|
|
|
|
|
val = scratch.to_ref_llval(bcx);
|
|
|
|
}
|
|
|
|
(ty::ByRef, _) => {
|
2013-01-10 12:59:58 -06:00
|
|
|
// This assertion should really be valid, but because
|
|
|
|
// the explicit self code currently passes by-ref, it
|
|
|
|
// does not hold.
|
|
|
|
//
|
2013-02-08 16:08:02 -06:00
|
|
|
//assert !bcx.ccx().maps.moves_map.contains_key(
|
2013-02-02 01:04:22 -06:00
|
|
|
// &arg_expr.id);
|
2013-04-24 03:29:46 -05:00
|
|
|
debug!("by ref arg with type %s",
|
|
|
|
bcx.ty_to_str(arg_datum.ty));
|
2012-09-19 20:00:26 -05:00
|
|
|
val = arg_datum.to_ref_llval(bcx);
|
2012-09-14 14:01:43 -05:00
|
|
|
}
|
2013-05-24 14:33:46 -05:00
|
|
|
(ty::ByCopy, _) => {
|
2013-06-19 13:28:42 -05:00
|
|
|
if ty::type_needs_drop(bcx.tcx(), arg_datum.ty) ||
|
|
|
|
arg_datum.appropriate_mode().is_by_ref() {
|
|
|
|
debug!("by copy arg with type %s, storing to scratch",
|
|
|
|
bcx.ty_to_str(arg_datum.ty));
|
|
|
|
let scratch = scratch_datum(bcx, arg_datum.ty, false);
|
|
|
|
|
|
|
|
arg_datum.store_to_datum(bcx,
|
|
|
|
arg_expr.id,
|
|
|
|
INIT,
|
|
|
|
scratch);
|
|
|
|
|
|
|
|
// Technically, ownership of val passes to the callee.
|
|
|
|
// However, we must cleanup should we fail before the
|
|
|
|
// callee is actually invoked.
|
|
|
|
scratch.add_clean(bcx);
|
|
|
|
temp_cleanups.push(scratch.val);
|
|
|
|
|
|
|
|
match scratch.appropriate_mode() {
|
|
|
|
ByValue => val = Load(bcx, scratch.val),
|
|
|
|
ByRef(_) => val = scratch.val,
|
|
|
|
}
|
|
|
|
} else {
|
2013-06-25 15:01:37 -05:00
|
|
|
debug!("by copy arg with type %s", bcx.ty_to_str(arg_datum.ty));
|
2013-06-19 13:28:42 -05:00
|
|
|
match arg_datum.mode {
|
|
|
|
ByRef(_) => val = Load(bcx, arg_datum.val),
|
|
|
|
ByValue => val = arg_datum.val,
|
|
|
|
}
|
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);
|
|
|
|
let llformal_arg_ty = match self_mode {
|
2013-06-15 22:45:48 -05:00
|
|
|
ty::ByRef => llformal_arg_ty.ptr_to(),
|
2013-04-26 21:13:38 -05:00
|
|
|
ty::ByCopy => llformal_arg_ty,
|
2013-04-24 03:29:46 -05:00
|
|
|
};
|
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
|
|
|
}
|