2012-12-03 16:48:01 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2013-03-26 16:38:07 -04:00
|
|
|
use back::abi;
|
2012-09-04 11:54:36 -07:00
|
|
|
use lib::llvm::llvm;
|
2013-03-26 16:38:07 -04:00
|
|
|
use lib::llvm::ValueRef;
|
2012-12-23 17:41:37 -05:00
|
|
|
use lib;
|
2012-12-13 13:05:22 -08:00
|
|
|
use metadata::csearch;
|
|
|
|
use middle::trans::base::*;
|
|
|
|
use middle::trans::build::*;
|
|
|
|
use middle::trans::callee::*;
|
2012-12-23 17:41:37 -05:00
|
|
|
use middle::trans::callee;
|
2014-01-15 14:39:08 -05:00
|
|
|
use middle::trans::cleanup;
|
2012-12-13 13:05:22 -08:00
|
|
|
use middle::trans::common::*;
|
2013-06-28 01:40:27 +02:00
|
|
|
use middle::trans::datum::*;
|
2012-12-13 13:05:22 -08:00
|
|
|
use middle::trans::expr::{SaveIn, Ignore};
|
2012-12-23 17:41:37 -05:00
|
|
|
use middle::trans::expr;
|
|
|
|
use middle::trans::glue;
|
|
|
|
use middle::trans::monomorphize;
|
2014-03-08 21:36:22 +01:00
|
|
|
use middle::trans::type_::Type;
|
2012-12-13 13:05:22 -08:00
|
|
|
use middle::trans::type_of::*;
|
2013-02-25 14:11:21 -05:00
|
|
|
use middle::ty;
|
2012-12-23 17:41:37 -05:00
|
|
|
use middle::typeck;
|
2014-03-06 19:24:11 +02:00
|
|
|
use middle::typeck::MethodCall;
|
2013-02-25 14:11:21 -05:00
|
|
|
use util::common::indenter;
|
2013-06-29 14:07:24 +09:00
|
|
|
use util::ppaux::Repr;
|
2012-12-13 13:05:22 -08:00
|
|
|
|
2013-08-03 17:13:14 -07:00
|
|
|
use std::c_str::ToCStr;
|
2014-03-08 21:36:22 +01:00
|
|
|
use std::vec_ng::Vec;
|
|
|
|
use std::vec_ng;
|
2013-09-06 23:56:17 -07:00
|
|
|
use syntax::parse::token;
|
2014-02-14 07:07:09 +02:00
|
|
|
use syntax::{ast, ast_map, visit};
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2012-08-21 19:51:43 -07:00
|
|
|
/**
|
|
|
|
The main "translation" pass for methods. Generates code
|
|
|
|
for non-monomorphized methods only. Other methods will
|
|
|
|
be generated once they are invoked with specific type parameters,
|
|
|
|
see `trans::base::lval_static_fn()` or `trans::base::monomorphic_fn()`.
|
|
|
|
*/
|
2013-12-19 16:47:15 -08:00
|
|
|
pub fn trans_impl(ccx: @CrateContext,
|
2013-09-02 02:50:59 +02:00
|
|
|
name: ast::Ident,
|
2014-01-09 15:05:33 +02:00
|
|
|
methods: &[@ast::Method],
|
2013-05-15 17:38:52 -07:00
|
|
|
generics: &ast::Generics,
|
2013-07-27 10:25:59 +02:00
|
|
|
id: ast::NodeId) {
|
2014-01-27 14:18:36 +02:00
|
|
|
let _icx = push_ctxt("meth::trans_impl");
|
2013-03-15 15:24:24 -04:00
|
|
|
let tcx = ccx.tcx;
|
|
|
|
|
2014-02-14 07:07:09 +02:00
|
|
|
debug!("trans_impl(name={}, id={:?})", name.repr(tcx), id);
|
2013-03-15 15:24:24 -04:00
|
|
|
|
2013-08-28 23:28:06 -07:00
|
|
|
// Both here and below with generic methods, be sure to recurse and look for
|
|
|
|
// items that we need to translate.
|
|
|
|
if !generics.ty_params.is_empty() {
|
2013-09-25 10:58:40 +02:00
|
|
|
let mut v = TransItemVisitor{ ccx: ccx };
|
2013-08-28 23:28:06 -07:00
|
|
|
for method in methods.iter() {
|
2013-09-25 10:58:40 +02:00
|
|
|
visit::walk_method_helper(&mut v, *method, ());
|
2013-08-28 23:28:06 -07:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2013-08-03 12:45:23 -04:00
|
|
|
for method in methods.iter() {
|
2013-02-14 21:50:03 -08:00
|
|
|
if method.generics.ty_params.len() == 0u {
|
2012-08-21 19:51:43 -07:00
|
|
|
let llfn = get_item_val(ccx, method.id);
|
2014-02-14 07:07:09 +02:00
|
|
|
trans_fn(ccx, method.decl, method.body,
|
2014-01-27 14:18:36 +02:00
|
|
|
llfn, None, method.id, []);
|
2013-08-28 23:28:06 -07:00
|
|
|
} else {
|
2013-09-25 10:58:40 +02:00
|
|
|
let mut v = TransItemVisitor{ ccx: ccx };
|
|
|
|
visit::walk_method_helper(&mut v, *method, ());
|
2012-03-08 13:30:22 +01:00
|
|
|
}
|
2012-01-02 12:21:44 +01:00
|
|
|
}
|
|
|
|
}
|
2012-01-02 13:26:51 +01:00
|
|
|
|
2013-07-02 12:47:32 -07:00
|
|
|
/// Translates a (possibly monomorphized) method body.
|
|
|
|
///
|
|
|
|
/// Parameters:
|
|
|
|
/// * `method`: the AST node for the method
|
|
|
|
/// * `param_substs`: if this is a generic method, the current values for
|
2014-01-27 14:18:36 +02:00
|
|
|
/// type parameters and so forth, else None
|
|
|
|
/// * `llfn`: the LLVM ValueRef for the method
|
2013-07-02 12:47:32 -07:00
|
|
|
///
|
2014-02-14 07:07:09 +02:00
|
|
|
pub fn trans_method(ccx: @CrateContext, method: &ast::Method,
|
2013-02-18 12:36:30 -08:00
|
|
|
param_substs: Option<@param_substs>,
|
2014-01-27 14:18:36 +02:00
|
|
|
llfn: ValueRef) -> ValueRef {
|
2014-02-14 07:07:09 +02:00
|
|
|
trans_fn(ccx, method.decl, method.body,
|
2014-01-27 14:18:36 +02:00
|
|
|
llfn, param_substs, method.id, []);
|
2014-01-11 16:39:32 +02:00
|
|
|
llfn
|
2012-01-02 16:50:51 +01:00
|
|
|
}
|
|
|
|
|
2014-01-07 08:54:58 -08:00
|
|
|
pub fn trans_method_callee<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2014-03-06 19:24:11 +02:00
|
|
|
method_call: MethodCall,
|
|
|
|
self_expr: Option<&ast::Expr>,
|
2014-01-15 14:39:08 -05:00
|
|
|
arg_cleanup_scope: cleanup::ScopeId)
|
2014-01-07 08:54:58 -08:00
|
|
|
-> Callee<'a> {
|
2014-01-27 14:18:36 +02:00
|
|
|
let _icx = push_ctxt("meth::trans_method_callee");
|
2012-10-08 12:39:30 -07:00
|
|
|
|
2014-02-26 16:06:45 +02:00
|
|
|
let (origin, method_ty) = match bcx.ccx().maps.method_map
|
2014-03-06 19:24:11 +02:00
|
|
|
.borrow().get().find(&method_call) {
|
2014-02-26 16:06:45 +02:00
|
|
|
Some(method) => {
|
2014-03-06 19:24:11 +02:00
|
|
|
debug!("trans_method_callee({:?}, method={})",
|
|
|
|
method_call, method.repr(bcx.tcx()));
|
2014-02-26 16:06:45 +02:00
|
|
|
(method.origin, method.ty)
|
|
|
|
}
|
|
|
|
None => {
|
2014-03-05 16:36:01 +02:00
|
|
|
bcx.sess().span_bug(bcx.tcx().map.span(method_call.expr_id),
|
|
|
|
"method call expr wasn't in method map")
|
2014-02-26 16:06:45 +02:00
|
|
|
}
|
|
|
|
};
|
2013-03-21 08:33:52 -04:00
|
|
|
|
2014-02-19 22:11:45 +02:00
|
|
|
match origin {
|
2014-02-26 16:01:36 +02:00
|
|
|
typeck::MethodStatic(did) => {
|
2012-08-28 15:54:45 -07:00
|
|
|
Callee {
|
|
|
|
bcx: bcx,
|
2014-03-06 19:24:11 +02:00
|
|
|
data: Fn(callee::trans_fn_ref(bcx, did, MethodCall(method_call)))
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
}
|
2014-02-26 16:01:36 +02:00
|
|
|
typeck::MethodParam(typeck::MethodParam {
|
2013-01-16 19:24:10 -08:00
|
|
|
trait_id: trait_id,
|
|
|
|
method_num: off,
|
|
|
|
param_num: p,
|
|
|
|
bound_num: b
|
|
|
|
}) => {
|
2012-08-28 15:54:45 -07:00
|
|
|
match bcx.fcx.param_substs {
|
2013-02-18 12:36:30 -08:00
|
|
|
Some(substs) => {
|
2013-08-23 14:34:00 -07:00
|
|
|
ty::populate_implementations_for_trait_if_necessary(
|
|
|
|
bcx.tcx(),
|
|
|
|
trait_id);
|
|
|
|
|
2014-01-11 16:39:32 +02:00
|
|
|
let vtbl = find_vtable(bcx.tcx(), substs, p, b);
|
2014-03-06 19:24:11 +02:00
|
|
|
trans_monomorphized_callee(bcx, method_call,
|
2014-01-27 14:18:36 +02:00
|
|
|
trait_id, off, vtbl)
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
// how to get rid of this?
|
2013-10-21 13:08:31 -07:00
|
|
|
None => fail!("trans_method_callee: missing param_substs")
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
}
|
2013-06-20 09:29:24 -07:00
|
|
|
|
2014-02-26 16:01:36 +02:00
|
|
|
typeck::MethodObject(ref mt) => {
|
2014-03-06 19:24:11 +02:00
|
|
|
let self_expr = match self_expr {
|
|
|
|
Some(self_expr) => self_expr,
|
|
|
|
None => {
|
2014-03-05 16:36:01 +02:00
|
|
|
bcx.sess().span_bug(bcx.tcx().map.span(method_call.expr_id),
|
|
|
|
"self expr wasn't provided for trait object \
|
|
|
|
callee (trying to call overloaded op?)")
|
2014-03-06 19:24:11 +02:00
|
|
|
}
|
|
|
|
};
|
2012-11-28 17:33:30 -08:00
|
|
|
trans_trait_callee(bcx,
|
2014-02-26 16:06:45 +02:00
|
|
|
monomorphize_type(bcx, method_ty),
|
2013-08-13 13:22:58 -07:00
|
|
|
mt.real_index,
|
2014-03-06 19:24:11 +02:00
|
|
|
self_expr,
|
2014-01-15 14:39:08 -05:00
|
|
|
arg_cleanup_scope)
|
2012-02-09 11:17:11 +01:00
|
|
|
}
|
2012-01-26 12:26:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 08:54:58 -08:00
|
|
|
pub fn trans_static_method_callee(bcx: &Block,
|
2013-09-02 03:45:37 +02:00
|
|
|
method_id: ast::DefId,
|
|
|
|
trait_id: ast::DefId,
|
2014-02-26 16:06:45 +02:00
|
|
|
expr_id: ast::NodeId)
|
2014-01-27 14:18:36 +02:00
|
|
|
-> ValueRef {
|
|
|
|
let _icx = push_ctxt("meth::trans_static_method_callee");
|
2012-08-02 16:01:38 -07:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("trans_static_method_callee(method_id={:?}, trait_id={}, \
|
2014-02-26 16:06:45 +02:00
|
|
|
expr_id={:?})",
|
2012-10-12 17:00:08 -07:00
|
|
|
method_id,
|
|
|
|
ty::item_path_str(bcx.tcx(), trait_id),
|
2014-02-26 16:06:45 +02:00
|
|
|
expr_id);
|
2012-10-12 17:00:08 -07:00
|
|
|
let _indenter = indenter();
|
|
|
|
|
2013-08-23 14:34:00 -07:00
|
|
|
ty::populate_implementations_for_trait_if_necessary(bcx.tcx(), trait_id);
|
|
|
|
|
2012-10-12 17:00:08 -07:00
|
|
|
// When we translate a static fn defined in a trait like:
|
|
|
|
//
|
|
|
|
// trait<T1...Tn> Trait {
|
2013-03-21 19:07:54 -07:00
|
|
|
// fn foo<M1...Mn>(...) {...}
|
2012-10-12 17:00:08 -07:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// this winds up being translated as something like:
|
|
|
|
//
|
|
|
|
// fn foo<T1...Tn,self: Trait<T1...Tn>,M1...Mn>(...) {...}
|
|
|
|
//
|
|
|
|
// So when we see a call to this function foo, we have to figure
|
|
|
|
// out which impl the `Trait<T1...Tn>` bound on the type `self` was
|
2013-06-26 17:20:53 -07:00
|
|
|
// bound to.
|
|
|
|
let bound_index = ty::lookup_trait_def(bcx.tcx(), trait_id).
|
2014-02-01 15:57:59 +11:00
|
|
|
generics.type_param_defs().len();
|
2012-10-12 17:00:08 -07:00
|
|
|
|
2014-02-05 22:15:24 +01:00
|
|
|
let mname = if method_id.krate == ast::LOCAL_CRATE {
|
2014-02-14 07:07:09 +02:00
|
|
|
match bcx.tcx().map.get(method_id.node) {
|
|
|
|
ast_map::NodeTraitMethod(method) => {
|
|
|
|
let ident = match *method {
|
|
|
|
ast::Required(ref m) => m.ident,
|
|
|
|
ast::Provided(ref m) => m.ident
|
|
|
|
};
|
|
|
|
ident.name
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
2014-02-14 07:07:09 +02:00
|
|
|
_ => fail!("callee is not a trait method")
|
2012-08-02 16:01:38 -07:00
|
|
|
}
|
|
|
|
} else {
|
2014-02-14 07:07:09 +02:00
|
|
|
csearch::get_item_path(bcx.tcx(), method_id).last().unwrap().name()
|
2012-08-02 16:01:38 -07:00
|
|
|
};
|
2014-02-26 16:06:45 +02:00
|
|
|
debug!("trans_static_method_callee: method_id={:?}, expr_id={:?}, \
|
|
|
|
name={}", method_id, expr_id, token::get_name(mname));
|
2012-08-02 16:01:38 -07:00
|
|
|
|
2014-02-26 16:06:45 +02:00
|
|
|
let vtbls = ccx.maps.vtable_map.borrow().get().get_copy(&expr_id);
|
2013-12-19 20:10:37 -08:00
|
|
|
let vtbls = resolve_vtables_in_fn_ctxt(bcx.fcx, vtbls);
|
2012-08-02 16:01:38 -07:00
|
|
|
|
2014-03-08 21:36:22 +01:00
|
|
|
match vtbls.get(bound_index).get(0) {
|
|
|
|
&typeck::vtable_static(impl_did, ref rcvr_substs, rcvr_origins) => {
|
2013-06-17 19:43:22 -04:00
|
|
|
assert!(rcvr_substs.iter().all(|t| !ty::type_needs_infer(*t)));
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2014-02-14 07:07:09 +02:00
|
|
|
let mth_id = method_with_name(ccx, impl_did, mname);
|
2013-06-26 17:20:53 -07:00
|
|
|
let (callee_substs, callee_origins) =
|
|
|
|
combine_impl_and_methods_tps(
|
2014-03-06 19:24:11 +02:00
|
|
|
bcx, mth_id, ExprId(expr_id),
|
2014-03-08 21:36:22 +01:00
|
|
|
rcvr_substs.as_slice(), rcvr_origins);
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2014-03-06 19:24:11 +02:00
|
|
|
let llfn = trans_fn_ref_with_vtables(bcx, mth_id, ExprId(expr_id),
|
2014-03-08 21:36:22 +01:00
|
|
|
callee_substs.as_slice(),
|
2014-01-27 14:18:36 +02:00
|
|
|
Some(callee_origins));
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2014-02-26 16:06:45 +02:00
|
|
|
let callee_ty = node_id_type(bcx, expr_id);
|
2014-01-27 14:18:36 +02:00
|
|
|
let llty = type_of_fn_from_ty(ccx, callee_ty).ptr_to();
|
|
|
|
PointerCast(bcx, llfn, llty)
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
_ => {
|
2013-10-21 13:08:31 -07:00
|
|
|
fail!("vtable_param left in monomorphized \
|
2013-01-31 17:51:01 -08:00
|
|
|
function's vtable substs");
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
2012-08-02 16:01:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 16:47:15 -08:00
|
|
|
pub fn method_with_name(ccx: &CrateContext,
|
2013-09-02 03:45:37 +02:00
|
|
|
impl_id: ast::DefId,
|
2013-09-05 13:57:52 -07:00
|
|
|
name: ast::Name) -> ast::DefId {
|
2013-12-18 17:14:25 -08:00
|
|
|
{
|
|
|
|
let impl_method_cache = ccx.impl_method_cache.borrow();
|
|
|
|
let meth_id_opt = impl_method_cache.get().find_copy(&(impl_id, name));
|
|
|
|
match meth_id_opt {
|
|
|
|
Some(m) => return m,
|
|
|
|
None => {}
|
|
|
|
}
|
2013-06-12 14:22:17 -07:00
|
|
|
}
|
2013-06-14 17:38:17 +12:00
|
|
|
|
2013-12-19 19:19:19 -08:00
|
|
|
let impls = ccx.tcx.impls.borrow();
|
|
|
|
let imp = impls.get().find(&impl_id)
|
2013-07-15 14:19:36 -07:00
|
|
|
.expect("could not find impl while translating");
|
2013-09-05 13:57:52 -07:00
|
|
|
let meth = imp.methods.iter().find(|m| m.ident.name == name)
|
2013-07-15 14:19:36 -07:00
|
|
|
.expect("could not find method while translating");
|
2012-03-08 12:15:02 +01:00
|
|
|
|
2013-12-18 17:14:25 -08:00
|
|
|
let mut impl_method_cache = ccx.impl_method_cache.borrow_mut();
|
|
|
|
impl_method_cache.get().insert((impl_id, name), meth.def_id);
|
2013-07-15 15:24:16 -07:00
|
|
|
meth.def_id
|
2012-03-09 08:44:53 +01:00
|
|
|
}
|
|
|
|
|
2014-01-27 14:18:36 +02:00
|
|
|
fn trans_monomorphized_callee<'a>(bcx: &'a Block<'a>,
|
2014-03-06 19:24:11 +02:00
|
|
|
method_call: MethodCall,
|
2013-09-02 03:45:37 +02:00
|
|
|
trait_id: ast::DefId,
|
2013-01-30 11:46:19 -08:00
|
|
|
n_method: uint,
|
2014-01-27 14:18:36 +02:00
|
|
|
vtbl: typeck::vtable_origin)
|
2014-01-07 08:54:58 -08:00
|
|
|
-> Callee<'a> {
|
2014-01-27 14:18:36 +02:00
|
|
|
let _icx = push_ctxt("meth::trans_monomorphized_callee");
|
2014-02-26 16:06:45 +02:00
|
|
|
match vtbl {
|
2013-03-20 01:17:42 -04:00
|
|
|
typeck::vtable_static(impl_did, ref rcvr_substs, rcvr_origins) => {
|
2012-08-28 15:54:45 -07:00
|
|
|
let ccx = bcx.ccx();
|
2013-03-27 10:26:57 -04:00
|
|
|
let mname = ty::trait_method(ccx.tcx, trait_id, n_method).ident;
|
2013-09-05 13:57:52 -07:00
|
|
|
let mth_id = method_with_name(bcx.ccx(), impl_did, mname.name);
|
2012-08-28 15:54:45 -07:00
|
|
|
|
|
|
|
// create a concatenated set of substitutions which includes
|
|
|
|
// those from the impl and those from the method:
|
2013-06-26 17:20:53 -07:00
|
|
|
let (callee_substs, callee_origins) =
|
|
|
|
combine_impl_and_methods_tps(
|
2014-03-06 19:24:11 +02:00
|
|
|
bcx, mth_id, MethodCall(method_call),
|
2014-03-08 21:36:22 +01:00
|
|
|
rcvr_substs.as_slice(), rcvr_origins);
|
2012-08-28 15:54:45 -07:00
|
|
|
|
|
|
|
// translate the function
|
2014-01-27 14:18:36 +02:00
|
|
|
let llfn = trans_fn_ref_with_vtables(bcx,
|
|
|
|
mth_id,
|
2014-03-06 19:24:11 +02:00
|
|
|
MethodCall(method_call),
|
2014-03-08 21:36:22 +01:00
|
|
|
callee_substs.as_slice(),
|
2014-01-27 14:18:36 +02:00
|
|
|
Some(callee_origins));
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2014-01-27 14:18:36 +02:00
|
|
|
Callee { bcx: bcx, data: Fn(llfn) }
|
2012-02-09 11:17:11 +01:00
|
|
|
}
|
2013-11-28 12:22:53 -08:00
|
|
|
typeck::vtable_param(..) => {
|
2013-10-21 13:08:31 -07:00
|
|
|
fail!("vtable_param left in monomorphized function's vtable substs");
|
2012-02-09 11:17:11 +01:00
|
|
|
}
|
2014-02-26 16:06:45 +02:00
|
|
|
}
|
2012-09-10 12:25:45 -07:00
|
|
|
}
|
|
|
|
|
2014-02-26 16:06:45 +02:00
|
|
|
fn combine_impl_and_methods_tps(bcx: &Block,
|
|
|
|
mth_did: ast::DefId,
|
2014-03-06 19:24:11 +02:00
|
|
|
node: ExprOrMethodCall,
|
2014-02-26 16:06:45 +02:00
|
|
|
rcvr_substs: &[ty::t],
|
|
|
|
rcvr_origins: typeck::vtable_res)
|
2014-03-04 10:02:49 -08:00
|
|
|
-> (Vec<ty::t> , typeck::vtable_res) {
|
2012-09-10 12:25:45 -07:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Creates a concatenated set of substitutions which includes
|
|
|
|
* those from the impl and those from the method. This are
|
|
|
|
* some subtle complications here. Statically, we have a list
|
|
|
|
* of type parameters like `[T0, T1, T2, M1, M2, M3]` where
|
|
|
|
* `Tn` are type parameters that appear on the receiver. For
|
|
|
|
* example, if the receiver is a method parameter `A` with a
|
|
|
|
* bound like `trait<B,C,D>` then `Tn` would be `[B,C,D]`.
|
|
|
|
*
|
|
|
|
* The weird part is that the type `A` might now be bound to
|
|
|
|
* any other type, such as `foo<X>`. In that case, the vector
|
|
|
|
* we want is: `[X, M1, M2, M3]`. Therefore, what we do now is
|
|
|
|
* to slice off the method type parameters and append them to
|
|
|
|
* the type parameters from the type that the receiver is
|
|
|
|
* mapped to. */
|
|
|
|
|
|
|
|
let ccx = bcx.ccx();
|
2013-07-15 14:19:36 -07:00
|
|
|
let method = ty::method(ccx.tcx, mth_did);
|
2014-02-01 15:57:59 +11:00
|
|
|
let n_m_tps = method.generics.type_param_defs().len();
|
2014-03-06 19:24:11 +02:00
|
|
|
let node_substs = node_id_type_params(bcx, node);
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("rcvr_substs={:?}", rcvr_substs.repr(ccx.tcx));
|
2012-09-10 12:25:45 -07:00
|
|
|
let ty_substs
|
2014-03-08 21:36:22 +01:00
|
|
|
= vec_ng::append(Vec::from_slice(rcvr_substs),
|
|
|
|
node_substs.tailn(node_substs.len() - n_m_tps));
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("n_m_tps={:?}", n_m_tps);
|
|
|
|
debug!("node_substs={:?}", node_substs.repr(ccx.tcx));
|
|
|
|
debug!("ty_substs={:?}", ty_substs.repr(ccx.tcx));
|
2012-09-10 12:25:45 -07:00
|
|
|
|
|
|
|
|
2013-06-26 17:20:53 -07:00
|
|
|
// Now, do the same work for the vtables. The vtables might not
|
|
|
|
// exist, in which case we need to make them.
|
2014-03-06 19:24:11 +02:00
|
|
|
let vtables = match node {
|
|
|
|
ExprId(id) => node_vtables(bcx, id),
|
2014-03-08 18:33:39 +02:00
|
|
|
MethodCall(method_call) => {
|
|
|
|
if method_call.autoderef == 0 {
|
|
|
|
node_vtables(bcx, method_call.expr_id)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2014-03-06 19:24:11 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let r_m_origins = match vtables {
|
2012-09-10 12:25:45 -07:00
|
|
|
Some(vt) => vt,
|
2014-03-08 21:36:22 +01:00
|
|
|
None => @Vec::from_elem(node_substs.len(), @Vec::new())
|
2012-09-10 12:25:45 -07:00
|
|
|
};
|
2013-06-26 17:20:53 -07:00
|
|
|
let vtables
|
2014-03-08 21:36:22 +01:00
|
|
|
= @vec_ng::append(Vec::from_slice(rcvr_origins.as_slice()),
|
|
|
|
r_m_origins.tailn(r_m_origins.len() - n_m_tps));
|
2012-09-10 12:25:45 -07:00
|
|
|
|
2014-02-26 16:06:45 +02:00
|
|
|
(ty_substs, vtables)
|
2012-02-09 11:17:11 +01:00
|
|
|
}
|
|
|
|
|
2014-01-27 14:18:36 +02:00
|
|
|
fn trans_trait_callee<'a>(bcx: &'a Block<'a>,
|
2014-02-26 16:06:45 +02:00
|
|
|
method_ty: ty::t,
|
2013-01-30 11:46:19 -08:00
|
|
|
n_method: uint,
|
2014-01-15 14:39:08 -05:00
|
|
|
self_expr: &ast::Expr,
|
|
|
|
arg_cleanup_scope: cleanup::ScopeId)
|
2014-01-07 08:54:58 -08:00
|
|
|
-> Callee<'a> {
|
2013-08-11 13:42:26 -04:00
|
|
|
/*!
|
|
|
|
* Create a method callee where the method is coming from a trait
|
2014-02-07 00:38:33 +02:00
|
|
|
* object (e.g., ~Trait type). In this case, we must pull the fn
|
2013-08-11 13:42:26 -04:00
|
|
|
* pointer out of the vtable that is packaged up with the object.
|
|
|
|
* Objects are represented as a pair, so we first evaluate the self
|
|
|
|
* expression and then extract the self data and vtable out of the
|
|
|
|
* pair.
|
|
|
|
*/
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2014-01-27 14:18:36 +02:00
|
|
|
let _icx = push_ctxt("meth::trans_trait_callee");
|
2012-08-28 15:54:45 -07:00
|
|
|
let mut bcx = bcx;
|
2013-08-11 13:42:26 -04:00
|
|
|
|
2014-01-15 20:31:20 -05:00
|
|
|
// Translate self_datum and take ownership of the value by
|
|
|
|
// converting to an rvalue.
|
|
|
|
let self_datum = unpack_datum!(
|
|
|
|
bcx, expr::trans(bcx, self_expr));
|
|
|
|
|
2014-02-13 21:52:53 +02:00
|
|
|
let llval = if ty::type_needs_drop(bcx.tcx(), self_datum.ty) {
|
|
|
|
let self_datum = unpack_datum!(
|
|
|
|
bcx, self_datum.to_rvalue_datum(bcx, "trait_callee"));
|
|
|
|
|
|
|
|
// Convert to by-ref since `trans_trait_callee_from_llval` wants it
|
|
|
|
// that way.
|
|
|
|
let self_datum = unpack_datum!(
|
|
|
|
bcx, self_datum.to_ref_datum(bcx));
|
2014-01-15 20:31:20 -05:00
|
|
|
|
2014-02-13 21:52:53 +02:00
|
|
|
// Arrange cleanup in case something should go wrong before the
|
|
|
|
// actual call occurs.
|
|
|
|
self_datum.add_clean(bcx.fcx, arg_cleanup_scope)
|
|
|
|
} else {
|
|
|
|
// We don't have to do anything about cleanups for &Trait and &mut Trait.
|
|
|
|
assert!(self_datum.kind.is_by_ref());
|
|
|
|
self_datum.val
|
|
|
|
};
|
2013-08-11 13:42:26 -04:00
|
|
|
|
2014-02-26 16:06:45 +02:00
|
|
|
trans_trait_callee_from_llval(bcx, method_ty, n_method, llval)
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
2014-01-15 20:31:20 -05:00
|
|
|
pub fn trans_trait_callee_from_llval<'a>(bcx: &'a Block<'a>,
|
|
|
|
callee_ty: ty::t,
|
|
|
|
n_method: uint,
|
|
|
|
llpair: ValueRef)
|
|
|
|
-> Callee<'a> {
|
2013-08-11 13:42:26 -04:00
|
|
|
/*!
|
|
|
|
* Same as `trans_trait_callee()` above, except that it is given
|
|
|
|
* a by-ref pointer to the object pair.
|
|
|
|
*/
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2014-01-27 14:18:36 +02:00
|
|
|
let _icx = push_ctxt("meth::trans_trait_callee");
|
2012-05-14 15:48:58 -07:00
|
|
|
let ccx = bcx.ccx();
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2013-08-11 13:42:26 -04:00
|
|
|
// Load the data pointer from the object.
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("(translating trait callee) loading second index from pair");
|
2013-06-28 01:40:27 +02:00
|
|
|
let llboxptr = GEPi(bcx, llpair, [0u, abi::trt_field_box]);
|
|
|
|
let llbox = Load(bcx, llboxptr);
|
2014-01-27 14:18:36 +02:00
|
|
|
let llself = PointerCast(bcx, llbox, Type::i8p());
|
2013-06-29 16:43:39 +02:00
|
|
|
|
2012-08-28 15:54:45 -07:00
|
|
|
// Load the function from the vtable and cast it to the expected type.
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("(translating trait callee) loading method");
|
2014-01-27 14:18:36 +02:00
|
|
|
// Replace the self type (&Self or ~Self) with an opaque pointer.
|
|
|
|
let llcallee_ty = match ty::get(callee_ty).sty {
|
|
|
|
ty::ty_bare_fn(ref f) if f.abis.is_rust() => {
|
|
|
|
type_of_rust_fn(ccx, true, f.sig.inputs.slice_from(1), f.sig.output)
|
|
|
|
}
|
|
|
|
_ => {
|
2014-03-05 16:36:01 +02:00
|
|
|
ccx.sess().bug("meth::trans_trait_callee given non-bare-rust-fn");
|
2014-01-27 14:18:36 +02:00
|
|
|
}
|
|
|
|
};
|
2013-08-11 13:42:26 -04:00
|
|
|
let llvtable = Load(bcx,
|
|
|
|
PointerCast(bcx,
|
|
|
|
GEPi(bcx, llpair,
|
|
|
|
[0u, abi::trt_field_vtable]),
|
|
|
|
Type::vtable().ptr_to().ptr_to()));
|
2013-05-15 17:38:52 -07:00
|
|
|
let mptr = Load(bcx, GEPi(bcx, llvtable, [0u, n_method + 1]));
|
2013-06-16 15:45:48 +12:00
|
|
|
let mptr = PointerCast(bcx, mptr, llcallee_ty.ptr_to());
|
2012-08-28 15:54:45 -07:00
|
|
|
|
|
|
|
return Callee {
|
|
|
|
bcx: bcx,
|
2014-01-27 14:18:36 +02:00
|
|
|
data: TraitMethod(MethodData {
|
2012-08-28 15:54:45 -07:00
|
|
|
llfn: mptr,
|
2013-08-11 13:42:26 -04:00
|
|
|
llself: llself,
|
2012-08-28 15:54:45 -07:00
|
|
|
})
|
|
|
|
};
|
2012-01-07 22:44:14 +01:00
|
|
|
}
|
|
|
|
|
2013-12-19 16:47:15 -08:00
|
|
|
pub fn vtable_id(ccx: @CrateContext,
|
2013-05-17 17:27:44 -07:00
|
|
|
origin: &typeck::vtable_origin)
|
2013-01-30 11:46:19 -08:00
|
|
|
-> mono_id {
|
2012-08-23 17:39:07 -07:00
|
|
|
match origin {
|
2013-05-17 17:27:44 -07:00
|
|
|
&typeck::vtable_static(impl_id, ref substs, sub_vtables) => {
|
2013-07-01 18:38:41 -07:00
|
|
|
let psubsts = param_substs {
|
2013-07-02 12:47:32 -07:00
|
|
|
tys: (*substs).clone(),
|
2013-07-01 18:38:41 -07:00
|
|
|
vtables: Some(sub_vtables),
|
|
|
|
self_ty: None,
|
2013-07-22 16:40:31 -07:00
|
|
|
self_vtables: None
|
2013-07-01 18:38:41 -07:00
|
|
|
};
|
|
|
|
|
2012-08-28 15:54:45 -07:00
|
|
|
monomorphize::make_mono_id(
|
2012-10-08 12:39:30 -07:00
|
|
|
ccx,
|
|
|
|
impl_id,
|
2013-09-26 16:54:15 -04:00
|
|
|
&psubsts)
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
2013-03-05 17:49:50 -05:00
|
|
|
|
2012-08-28 15:54:45 -07:00
|
|
|
// can't this be checked at the callee?
|
2013-10-21 13:08:31 -07:00
|
|
|
_ => fail!("vtable_id")
|
2012-03-08 16:10:25 +01:00
|
|
|
}
|
2012-01-12 16:57:30 +01:00
|
|
|
}
|
|
|
|
|
2013-05-15 17:38:52 -07:00
|
|
|
/// Creates a returns a dynamic vtable for the given type and vtable origin.
|
|
|
|
/// This is used only for objects.
|
2014-01-07 08:54:58 -08:00
|
|
|
pub fn get_vtable(bcx: &Block,
|
2013-05-15 17:38:52 -07:00
|
|
|
self_ty: ty::t,
|
2013-08-13 13:22:58 -07:00
|
|
|
origins: typeck::vtable_param_res)
|
2013-05-15 17:38:52 -07:00
|
|
|
-> ValueRef {
|
2013-08-13 13:22:58 -07:00
|
|
|
let ccx = bcx.ccx();
|
2014-01-27 14:18:36 +02:00
|
|
|
let _icx = push_ctxt("meth::get_vtable");
|
2013-08-13 13:22:58 -07:00
|
|
|
|
|
|
|
// Check the cache.
|
2014-03-08 21:36:22 +01:00
|
|
|
let hash_id = (self_ty, vtable_id(ccx, origins.get(0)));
|
2013-12-18 17:00:56 -08:00
|
|
|
{
|
|
|
|
let vtables = ccx.vtables.borrow();
|
|
|
|
match vtables.get().find(&hash_id) {
|
|
|
|
Some(&val) => { return val }
|
|
|
|
None => { }
|
|
|
|
}
|
2013-08-13 13:22:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not in the cache. Actually build it.
|
2014-03-08 21:36:22 +01:00
|
|
|
let mut methods = Vec::new();
|
|
|
|
for origin in origins.iter() {
|
2013-08-13 13:22:58 -07:00
|
|
|
match *origin {
|
|
|
|
typeck::vtable_static(id, ref substs, sub_vtables) => {
|
2014-03-08 21:36:22 +01:00
|
|
|
let vtable_methods = emit_vtable_methods(bcx,
|
|
|
|
id,
|
|
|
|
substs.as_slice(),
|
|
|
|
sub_vtables);
|
|
|
|
for vtable_method in vtable_methods.move_iter() {
|
|
|
|
methods.push(vtable_method)
|
|
|
|
}
|
2013-05-15 17:38:52 -07:00
|
|
|
}
|
2014-03-05 16:36:01 +02:00
|
|
|
_ => ccx.sess().bug("get_vtable: expected a static origin"),
|
2012-01-02 16:50:51 +01:00
|
|
|
}
|
2014-03-08 21:36:22 +01:00
|
|
|
}
|
2013-08-13 13:22:58 -07:00
|
|
|
|
2014-02-06 01:07:06 -05:00
|
|
|
// Generate a destructor for the vtable.
|
|
|
|
let drop_glue = glue::get_drop_glue(ccx, self_ty);
|
2014-03-08 21:36:22 +01:00
|
|
|
let vtable = make_vtable(ccx, drop_glue, methods.as_slice());
|
2013-12-18 17:00:56 -08:00
|
|
|
|
|
|
|
let mut vtables = ccx.vtables.borrow_mut();
|
|
|
|
vtables.get().insert(hash_id, vtable);
|
2013-08-13 13:22:58 -07:00
|
|
|
return vtable;
|
2012-01-12 16:57:30 +01:00
|
|
|
}
|
2012-01-02 13:26:51 +01:00
|
|
|
|
2013-05-15 17:38:52 -07:00
|
|
|
/// Helper function to declare and initialize the vtable.
|
2013-12-19 16:47:15 -08:00
|
|
|
pub fn make_vtable(ccx: &CrateContext,
|
2014-02-06 01:07:06 -05:00
|
|
|
drop_glue: ValueRef,
|
2013-05-15 17:38:52 -07:00
|
|
|
ptrs: &[ValueRef])
|
|
|
|
-> ValueRef {
|
2013-01-10 21:23:07 -08:00
|
|
|
unsafe {
|
2014-01-27 14:18:36 +02:00
|
|
|
let _icx = push_ctxt("meth::make_vtable");
|
2013-05-15 17:38:52 -07:00
|
|
|
|
2014-03-04 10:02:49 -08:00
|
|
|
let mut components = vec!(drop_glue);
|
2013-08-03 12:45:23 -04:00
|
|
|
for &ptr in ptrs.iter() {
|
2013-05-15 17:38:52 -07:00
|
|
|
components.push(ptr)
|
|
|
|
}
|
|
|
|
|
2014-03-08 21:36:22 +01:00
|
|
|
let tbl = C_struct(components.as_slice(), false);
|
2013-09-06 23:56:17 -07:00
|
|
|
let sym = token::gensym("vtable");
|
2013-11-21 15:42:55 -08:00
|
|
|
let vt_gvar = format!("vtable{}", sym).with_c_str(|buf| {
|
2013-06-16 22:52:44 +12:00
|
|
|
llvm::LLVMAddGlobal(ccx.llmod, val_ty(tbl).to_ref(), buf)
|
2013-11-21 15:42:55 -08:00
|
|
|
});
|
2013-01-10 21:23:07 -08:00
|
|
|
llvm::LLVMSetInitializer(vt_gvar, tbl);
|
|
|
|
llvm::LLVMSetGlobalConstant(vt_gvar, lib::llvm::True);
|
|
|
|
lib::llvm::SetLinkage(vt_gvar, lib::llvm::InternalLinkage);
|
|
|
|
vt_gvar
|
|
|
|
}
|
2012-01-12 16:57:30 +01:00
|
|
|
}
|
2012-01-02 13:26:51 +01:00
|
|
|
|
2014-01-07 08:54:58 -08:00
|
|
|
fn emit_vtable_methods(bcx: &Block,
|
2013-09-02 03:45:37 +02:00
|
|
|
impl_id: ast::DefId,
|
2013-08-13 13:22:58 -07:00
|
|
|
substs: &[ty::t],
|
|
|
|
vtables: typeck::vtable_res)
|
2014-03-04 10:02:49 -08:00
|
|
|
-> Vec<ValueRef> {
|
2013-06-07 10:29:21 -07:00
|
|
|
let ccx = bcx.ccx();
|
2012-03-08 16:10:25 +01:00
|
|
|
let tcx = ccx.tcx;
|
2012-07-18 16:49:55 -07:00
|
|
|
|
2013-05-16 15:54:51 -07:00
|
|
|
let trt_id = match ty::impl_trait_ref(tcx, impl_id) {
|
|
|
|
Some(t_id) => t_id.def_id,
|
2014-03-05 16:36:01 +02:00
|
|
|
None => ccx.sess().bug("make_impl_vtable: don't know how to \
|
|
|
|
make a vtable for a type impl!")
|
2013-05-16 15:54:51 -07:00
|
|
|
};
|
2013-03-27 06:16:28 -04:00
|
|
|
|
2013-08-23 14:34:00 -07:00
|
|
|
ty::populate_implementations_for_trait_if_necessary(bcx.tcx(), trt_id);
|
|
|
|
|
2013-05-15 17:38:52 -07:00
|
|
|
let trait_method_def_ids = ty::trait_method_def_ids(tcx, trt_id);
|
2013-11-21 15:42:55 -08:00
|
|
|
trait_method_def_ids.map(|method_def_id| {
|
2013-08-21 19:11:30 -07:00
|
|
|
let ident = ty::method(tcx, *method_def_id).ident;
|
|
|
|
// The substitutions we have are on the impl, so we grab
|
|
|
|
// the method type from the impl to substitute into.
|
2013-09-05 13:57:52 -07:00
|
|
|
let m_id = method_with_name(ccx, impl_id, ident.name);
|
2013-08-21 19:11:30 -07:00
|
|
|
let m = ty::method(tcx, m_id);
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("(making impl vtable) emitting method {} at subst {}",
|
2013-08-21 19:11:30 -07:00
|
|
|
m.repr(tcx),
|
|
|
|
substs.repr(tcx));
|
2014-01-08 00:56:16 +01:00
|
|
|
if m.generics.has_type_params() ||
|
|
|
|
ty::type_has_self(ty::mk_bare_fn(tcx, m.fty.clone())) {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("(making impl vtable) method has self or type params: {}",
|
2014-02-14 07:07:09 +02:00
|
|
|
token::get_ident(ident));
|
2013-06-16 15:45:48 +12:00
|
|
|
C_null(Type::nil().ptr_to())
|
2012-03-08 16:10:25 +01:00
|
|
|
} else {
|
2014-03-06 19:24:11 +02:00
|
|
|
trans_fn_ref_with_vtables(bcx, m_id, ExprId(0), substs, Some(vtables))
|
2012-01-12 16:57:30 +01:00
|
|
|
}
|
2013-11-21 15:42:55 -08:00
|
|
|
})
|
2012-01-12 16:57:30 +01:00
|
|
|
}
|
|
|
|
|
2014-01-15 14:39:08 -05:00
|
|
|
pub fn trans_trait_cast<'a>(bcx: &'a Block<'a>,
|
|
|
|
datum: Datum<Expr>,
|
|
|
|
id: ast::NodeId,
|
|
|
|
dest: expr::Dest)
|
|
|
|
-> &'a Block<'a> {
|
|
|
|
/*!
|
|
|
|
* Generates the code to convert from a pointer (`~T`, `&T`, etc)
|
|
|
|
* into an object (`~Trait`, `&Trait`, etc). This means creating a
|
2014-01-15 20:31:20 -05:00
|
|
|
* pair where the first word is the vtable and the second word is
|
|
|
|
* the pointer.
|
2014-01-15 14:39:08 -05:00
|
|
|
*/
|
|
|
|
|
2012-10-03 15:18:50 -07:00
|
|
|
let mut bcx = bcx;
|
2014-01-27 14:18:36 +02:00
|
|
|
let _icx = push_ctxt("meth::trans_cast");
|
2012-08-28 15:54:45 -07:00
|
|
|
|
|
|
|
let lldest = match dest {
|
|
|
|
Ignore => {
|
2014-01-15 14:39:08 -05:00
|
|
|
return datum.clean(bcx, "trait_cast", id);
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
SaveIn(dest) => dest
|
|
|
|
};
|
|
|
|
|
2012-02-21 14:20:18 +01:00
|
|
|
let ccx = bcx.ccx();
|
2014-01-15 14:39:08 -05:00
|
|
|
let v_ty = datum.ty;
|
|
|
|
let llbox_ty = type_of(bcx.ccx(), datum.ty);
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2014-01-15 14:39:08 -05:00
|
|
|
// Store the pointer into the first half of pair.
|
2013-05-15 17:38:52 -07:00
|
|
|
let mut llboxdest = GEPi(bcx, lldest, [0u, abi::trt_field_box]);
|
2014-01-15 14:39:08 -05:00
|
|
|
llboxdest = PointerCast(bcx, llboxdest, llbox_ty.ptr_to());
|
|
|
|
bcx = datum.store_to(bcx, llboxdest);
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2014-01-15 14:39:08 -05:00
|
|
|
// Store the vtable into the second half of pair.
|
2013-08-13 13:22:58 -07:00
|
|
|
// This is structured a bit funny because of dynamic borrow failures.
|
|
|
|
let origins = {
|
2013-12-19 20:10:37 -08:00
|
|
|
let res = {
|
|
|
|
let vtable_map = ccx.maps.vtable_map.borrow();
|
|
|
|
*vtable_map.get().get(&id)
|
|
|
|
};
|
|
|
|
let res = resolve_vtables_in_fn_ctxt(bcx.fcx, res);
|
2014-03-08 21:36:22 +01:00
|
|
|
*res.get(0)
|
2013-08-13 13:22:58 -07:00
|
|
|
};
|
|
|
|
let vtable = get_vtable(bcx, v_ty, origins);
|
2014-01-15 14:39:08 -05:00
|
|
|
let llvtabledest = GEPi(bcx, lldest, [0u, abi::trt_field_vtable]);
|
|
|
|
let llvtabledest = PointerCast(bcx, llvtabledest, val_ty(vtable).ptr_to());
|
|
|
|
Store(bcx, vtable, llvtabledest);
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2012-02-10 11:32:03 +01:00
|
|
|
bcx
|
2012-01-07 22:44:14 +01:00
|
|
|
}
|