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-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2013-01-07 16:16:52 -06: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;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::typeck;
|
2012-12-13 15:05:22 -06:00
|
|
|
use util::common::indenter;
|
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
|
|
|
|
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,
|
|
|
|
self_mode: ast::rmode
|
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 {
|
2012-08-28 17:54:45 -05:00
|
|
|
let _icx = bcx.insn_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(*) |
|
2012-08-28 17:54:45 -05:00
|
|
|
ast::def_const(*) | 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(*) |
|
|
|
|
ast::def_self_ty(*) => {
|
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
|
|
|
|
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
|
|
|
let _icx = bcx.insn_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-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-03-21 07:33:52 -05:00
|
|
|
let _icx = bcx.insn_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-03-28 20:39:09 -05:00
|
|
|
assert!(type_params.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);
|
|
|
|
|
2012-10-08 14:39:30 -05:00
|
|
|
// Modify the def_id if this is a default method; we want to be
|
|
|
|
// monomorphizing the trait's code.
|
2013-03-28 21:04:13 -05:00
|
|
|
let (def_id, opt_impl_did) = match tcx.provided_method_sources.find(&def_id) {
|
2012-10-08 14:39:30 -05:00
|
|
|
None => (def_id, None),
|
|
|
|
Some(source) => (source.method_id, Some(source.impl_id))
|
|
|
|
};
|
|
|
|
|
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-03-28 20:39:09 -05:00
|
|
|
assert!(def_id.crate == ast::local_crate);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
let mut (val, must_cast) =
|
2012-08-28 17:54:45 -05:00
|
|
|
monomorphize::monomorphic_fn(ccx, def_id, type_params,
|
2012-10-08 14:39:30 -05:00
|
|
|
vtables, opt_impl_did, Some(ref_id));
|
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(
|
|
|
|
bcx, val, T_ptr(type_of::type_of_fn_from_ty(ccx, ref_ty)));
|
|
|
|
}
|
|
|
|
return FnData {llfn: val};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the actual function pointer.
|
|
|
|
let mut val = {
|
|
|
|
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 {
|
2012-08-28 17:54:45 -05:00
|
|
|
let _icx = in_cx.insn_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,
|
|
|
|
rcvr: @ast::expr,
|
|
|
|
args: CallArgs,
|
|
|
|
dest: expr::Dest)
|
2013-04-18 17:53:29 -05:00
|
|
|
-> block {
|
2012-11-30 13:18:25 -06:00
|
|
|
let _icx = in_cx.insn_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(),
|
|
|
|
node_id_type(in_cx, call_ex.callee_id),
|
|
|
|
expr_ty(in_cx, call_ex),
|
|
|
|
|cx| {
|
2013-02-05 21:41:45 -06:00
|
|
|
match cx.ccx().maps.method_map.find(&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()));
|
|
|
|
|
2013-03-23 17:55:58 -05:00
|
|
|
// FIXME(#5562): removing this copy causes a segfault
|
|
|
|
// before stage2
|
|
|
|
let origin = /*bad*/ copy *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
|
|
|
|
2012-11-30 13:18:25 -06:00
|
|
|
meth::trans_method_callee(cx,
|
|
|
|
call_ex.callee_id,
|
|
|
|
rcvr,
|
2013-03-23 17:55:58 -05:00
|
|
|
origin)
|
2012-11-30 13:18:25 -06:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
cx.tcx().sess.span_bug(call_ex.span,
|
|
|
|
~"method call expr wasn't in \
|
|
|
|
method map")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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);
|
|
|
|
return callee::trans_call_inner(
|
|
|
|
bcx, None, fty, rty,
|
|
|
|
|bcx| trans_fn_ref_with_vtables_to_callee(bcx, did, 0, ~[], None),
|
2012-09-19 20:00:26 -05:00
|
|
|
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);
|
2012-09-25 14:17:20 -05:00
|
|
|
let mut llfnty = type_of::type_of(callee.bcx.ccx(),
|
|
|
|
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-01-08 16:00:45 -06:00
|
|
|
visit::visit_block(body, cx, visit::mk_vt(@visit::Visitor {
|
2012-08-28 17:54:45 -05:00
|
|
|
visit_item: |_i, _cx, _v| { },
|
2013-02-04 16:02:01 -06:00
|
|
|
visit_expr: |e: @ast::expr, cx: @mut bool, v| {
|
|
|
|
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,
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => visit::visit_expr(e, cx, v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
..*visit::default_visitor()
|
|
|
|
}));
|
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 {
|
2012-08-28 17:54:45 -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) => {
|
|
|
|
args.len() > 0u && match vec::last(args).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 {
|
|
|
|
let flag = alloca(bcx, T_bool());
|
|
|
|
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) => {
|
|
|
|
(d.llfn, llvm::LLVMGetUndef(T_opaque_box_ptr(ccx)))
|
|
|
|
}
|
|
|
|
Method(d) => {
|
|
|
|
// Weird but true: we pass self in the *environment* slot!
|
|
|
|
let llself = PointerCast(bcx, d.llself,
|
|
|
|
T_opaque_box_ptr(ccx));
|
|
|
|
(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
|
|
|
|
|
|
|
if ty::type_is_immediate(ret_ty) {
|
|
|
|
unsafe {
|
|
|
|
llargs.push(llvm::LLVMGetUndef(T_ptr(T_i8())));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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 {
|
|
|
|
Method(d) if d.self_mode == ast::by_copy => {
|
|
|
|
revoke_clean(bcx, d.llself);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2012-10-26 20:23:45 -05:00
|
|
|
// Uncomment this to debug calls.
|
|
|
|
/*
|
|
|
|
io::println(fmt!("calling: %s", bcx.val_str(llfn)));
|
|
|
|
for llargs.each |llarg| {
|
|
|
|
io::println(fmt!("arg: %s", bcx.val_str(*llarg)));
|
|
|
|
}
|
|
|
|
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-04-18 17:53:29 -05:00
|
|
|
if ty::type_is_immediate(ret_ty) {
|
|
|
|
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-03-03 06:33:39 -06:00
|
|
|
for (copy bcx.fcx.loop_ret).each |&(flagptr, _)| {
|
2013-02-19 01:40:42 -06:00
|
|
|
Store(bcx, C_bool(true), flagptr);
|
2013-04-18 17:53:29 -05:00
|
|
|
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 {
|
|
|
|
llvm::LLVMGetUndef(T_ptr(T_nil()))
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
let _icx = cx.insn_ctxt("trans_args");
|
|
|
|
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;
|
2012-09-18 23:41:37 -05:00
|
|
|
for vec::eachi(arg_exprs) |i, arg_expr| {
|
2012-08-28 17:54:45 -05:00
|
|
|
let arg_val = unpack_result!(bcx, {
|
2012-09-21 20:43:30 -05:00
|
|
|
trans_arg_expr(bcx, arg_tys[i], *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).
|
2012-09-18 23:41:37 -05:00
|
|
|
for vec::each(temp_cleanups) |c| {
|
|
|
|
revoke_clean(bcx, *c)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-03-19 13:40:34 -05:00
|
|
|
return 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,
|
|
|
|
formal_ty: ty::arg,
|
|
|
|
arg_expr: @ast::expr,
|
2013-04-17 11:15:37 -05:00
|
|
|
temp_cleanups: &mut ~[ValueRef],
|
|
|
|
ret_flag: Option<ValueRef>,
|
|
|
|
autoref_arg: AutorefArg) -> Result {
|
2012-08-28 17:54:45 -05:00
|
|
|
let _icx = bcx.insn_ctxt("trans_arg_expr");
|
|
|
|
let ccx = bcx.ccx();
|
|
|
|
|
|
|
|
debug!("trans_arg_expr(formal_ty=(%?,%s), arg_expr=%s, \
|
2012-09-11 23:25:01 -05:00
|
|
|
ret_flag=%?)",
|
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
|
|
|
formal_ty.mode,
|
|
|
|
formal_ty.ty.repr(bcx.tcx()),
|
|
|
|
arg_expr.repr(bcx.tcx()),
|
2012-09-26 18:27:12 -05:00
|
|
|
ret_flag.map(|v| bcx.val_str(*v)));
|
2012-08-28 17:54:45 -05:00
|
|
|
let _indenter = indenter();
|
|
|
|
|
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
|
|
|
_
|
|
|
|
}) =>
|
2012-09-18 12:41:05 -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,
|
|
|
|
mode: ByRef,
|
2013-01-10 12:59:58 -06:00
|
|
|
source: RevokeClean}}
|
2012-09-18 12:41:05 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bcx.sess().impossible_case(
|
|
|
|
arg_expr.span, ~"ret_flag with non-loop-\
|
|
|
|
body expr");
|
|
|
|
}
|
|
|
|
}
|
2012-09-14 14:01:43 -05:00
|
|
|
}
|
2012-09-18 12:41:05 -05:00
|
|
|
};
|
|
|
|
let mut arg_datum = arg_datumblock.datum;
|
|
|
|
let mut 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
|
|
|
// finally, deal with the various modes
|
|
|
|
let arg_mode = ty::resolved_mode(ccx.tcx, formal_ty.mode);
|
|
|
|
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).
|
|
|
|
let llformal_ty = type_of::type_of(ccx, formal_ty.ty);
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
val = llvm::LLVMGetUndef(llformal_ty);
|
|
|
|
}
|
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 => {
|
|
|
|
match arg_mode {
|
2012-10-06 00:07:53 -05:00
|
|
|
ast::by_ref => {
|
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);
|
2012-09-19 20:00:26 -05:00
|
|
|
val = arg_datum.to_ref_llval(bcx);
|
2012-09-14 14:01:43 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-01-10 12:59:58 -06:00
|
|
|
ast::by_copy => {
|
2013-02-27 18:28:37 -06:00
|
|
|
debug!("by copy arg with type %s, storing to scratch",
|
2013-03-01 18:38:39 -06:00
|
|
|
bcx.ty_to_str(arg_datum.ty));
|
2012-09-19 20:00:26 -05:00
|
|
|
let scratch = scratch_datum(bcx, arg_datum.ty, false);
|
|
|
|
|
2013-01-10 12:59:58 -06:00
|
|
|
arg_datum.store_to_datum(bcx, arg_expr.id,
|
|
|
|
INIT, scratch);
|
2012-09-19 20:00:26 -05:00
|
|
|
|
|
|
|
// 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);
|
2012-09-26 19:33:34 -05:00
|
|
|
temp_cleanups.push(scratch.val);
|
2012-09-20 14:29:15 -05:00
|
|
|
|
|
|
|
match arg_datum.appropriate_mode() {
|
|
|
|
ByValue => {
|
|
|
|
val = Load(bcx, scratch.val);
|
|
|
|
}
|
|
|
|
ByRef => {
|
|
|
|
val = scratch.val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-18 12:41:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if formal_ty.ty != arg_datum.ty {
|
|
|
|
// this could happen due to e.g. subtyping
|
2013-03-08 19:44:37 -06:00
|
|
|
let llformal_ty = type_of::type_of_explicit_arg(ccx, &formal_ty);
|
2012-09-18 12:41:05 -05:00
|
|
|
debug!("casting actual type (%s) to match formal (%s)",
|
|
|
|
bcx.val_str(val), bcx.llty_str(llformal_ty));
|
|
|
|
val = PointerCast(bcx, val, llformal_ty);
|
2012-09-14 14:01:43 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-18 12:41:05 -05:00
|
|
|
|
|
|
|
debug!("--- trans_arg_expr passing %s", val_str(bcx.ccx().tn, val));
|
|
|
|
return rslt(bcx, val);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|