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-09-18 13:46:39 -05:00
|
|
|
use back::{link, abi};
|
2012-12-23 16:41:37 -06:00
|
|
|
use driver;
|
2012-12-13 15:05:22 -06:00
|
|
|
use lib::llvm::llvm::LLVMGetParam;
|
2012-09-04 13:54:36 -05:00
|
|
|
use lib::llvm::llvm;
|
|
|
|
use lib::llvm::{ValueRef, TypeRef};
|
2012-12-23 16:41:37 -06:00
|
|
|
use lib;
|
2012-12-13 15:05:22 -06:00
|
|
|
use metadata::csearch;
|
|
|
|
use middle::trans::base::*;
|
|
|
|
use middle::trans::build::*;
|
|
|
|
use middle::trans::callee::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::callee;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::common::*;
|
|
|
|
use middle::trans::expr::{SaveIn, Ignore};
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::expr;
|
|
|
|
use middle::trans::glue;
|
|
|
|
use middle::trans::inline;
|
|
|
|
use middle::trans::monomorphize;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::type_of::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::typeck;
|
2012-09-04 13:54:36 -05:00
|
|
|
use util::ppaux::{ty_to_str, tys_to_str};
|
2012-12-13 15:05:22 -06:00
|
|
|
|
|
|
|
use core::libc::c_uint;
|
|
|
|
use std::map::HashMap;
|
|
|
|
use syntax::ast_map::{path, path_mod, path_name, node_id_to_str};
|
|
|
|
use syntax::ast_util::local_def;
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::print::pprust::expr_to_str;
|
2012-12-13 15:05:22 -06:00
|
|
|
use syntax::{ast, ast_map};
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
fn macros() { include!("macros.rs"); } // FIXME(#3114): Macro import/export.
|
2012-01-02 05:21:44 -06:00
|
|
|
|
2012-08-21 21:51:43 -05: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()`.
|
|
|
|
*/
|
2012-03-14 19:31:16 -05:00
|
|
|
fn trans_impl(ccx: @crate_ctxt, path: path, name: ast::ident,
|
2012-10-05 19:49:13 -05:00
|
|
|
methods: ~[@ast::method], tps: ~[ast::ty_param],
|
2012-10-08 14:39:30 -05:00
|
|
|
self_ty: Option<ty::t>, id: ast::node_id) {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = ccx.insn_ctxt("impl::trans_impl");
|
2012-08-01 19:30:05 -05:00
|
|
|
if tps.len() > 0u { return; }
|
2012-06-28 15:52:13 -05:00
|
|
|
let sub_path = vec::append_one(path, path_name(name));
|
2012-09-18 23:41:13 -05:00
|
|
|
for vec::each(methods) |method| {
|
2012-08-21 21:51:43 -05:00
|
|
|
if method.tps.len() == 0u {
|
|
|
|
let llfn = get_item_val(ccx, method.id);
|
|
|
|
let path = vec::append_one(sub_path, path_name(method.ident));
|
2012-10-08 14:39:30 -05:00
|
|
|
|
|
|
|
let param_substs_opt;
|
|
|
|
match self_ty {
|
|
|
|
None => param_substs_opt = None,
|
|
|
|
Some(self_ty) => {
|
2013-01-06 13:16:14 -06:00
|
|
|
param_substs_opt = Some(param_substs {
|
2012-10-08 14:39:30 -05:00
|
|
|
tys: ~[],
|
|
|
|
vtables: None,
|
|
|
|
bounds: @~[],
|
|
|
|
self_ty: Some(self_ty)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trans_method(ccx, path, *method, param_substs_opt, self_ty, llfn,
|
|
|
|
ast_util::local_def(id));
|
2012-03-08 06:30:22 -06:00
|
|
|
}
|
2012-01-02 05:21:44 -06:00
|
|
|
}
|
|
|
|
}
|
2012-01-02 06:26:51 -06:00
|
|
|
|
2012-08-21 21:51:43 -05:00
|
|
|
/**
|
|
|
|
Translates a (possibly monomorphized) method body.
|
|
|
|
|
|
|
|
# Parameters
|
|
|
|
|
|
|
|
- `path`: the path to the method
|
|
|
|
- `method`: the AST node for the method
|
|
|
|
- `param_substs`: if this is a generic method, the current values for
|
|
|
|
type parameters and so forth, else none
|
2012-10-05 19:49:13 -05:00
|
|
|
- `base_self_ty`: optionally, the explicit self type for this method. This
|
|
|
|
will be none if this is not a default method and must always be present
|
|
|
|
if this is a default method.
|
2012-08-21 21:51:43 -05:00
|
|
|
- `llfn`: the LLVM ValueRef for the method
|
2012-10-08 14:39:30 -05:00
|
|
|
- `impl_id`: the node ID of the impl this method is inside
|
2012-08-21 21:51:43 -05:00
|
|
|
*/
|
|
|
|
fn trans_method(ccx: @crate_ctxt,
|
|
|
|
path: path,
|
|
|
|
method: &ast::method,
|
2012-08-20 14:23:37 -05:00
|
|
|
param_substs: Option<param_substs>,
|
2012-10-05 19:49:13 -05:00
|
|
|
base_self_ty: Option<ty::t>,
|
2012-10-08 14:39:30 -05:00
|
|
|
llfn: ValueRef,
|
|
|
|
impl_id: ast::def_id) {
|
2012-08-21 21:51:43 -05:00
|
|
|
|
2012-08-22 15:18:29 -05:00
|
|
|
// figure out how self is being passed
|
2012-08-21 21:51:43 -05:00
|
|
|
let self_arg = match method.self_ty.node {
|
|
|
|
ast::sty_static => {
|
|
|
|
no_self
|
|
|
|
}
|
2012-08-22 15:18:29 -05:00
|
|
|
_ => {
|
|
|
|
// determine the (monomorphized) type that `self` maps to for
|
|
|
|
// this method
|
2012-10-05 19:49:13 -05:00
|
|
|
let self_ty;
|
|
|
|
match base_self_ty {
|
|
|
|
None => self_ty = ty::node_id_to_type(ccx.tcx, method.self_id),
|
|
|
|
Some(provided_self_ty) => self_ty = provided_self_ty
|
|
|
|
}
|
2012-08-22 15:18:29 -05:00
|
|
|
let self_ty = match param_substs {
|
2012-10-08 14:39:30 -05:00
|
|
|
None => self_ty,
|
2013-01-06 13:16:14 -06:00
|
|
|
Some(param_substs {tys: ref tys, _}) => {
|
2012-10-08 14:39:30 -05:00
|
|
|
ty::subst_tps(ccx.tcx, *tys, None, self_ty)
|
|
|
|
}
|
2012-08-22 15:18:29 -05:00
|
|
|
};
|
2012-11-26 20:08:01 -06:00
|
|
|
debug!("calling trans_fn with base_self_ty %s, self_ty %s",
|
|
|
|
match base_self_ty {
|
|
|
|
None => ~"(none)",
|
|
|
|
Some(x) => ty_to_str(ccx.tcx, x),
|
|
|
|
},
|
|
|
|
ty_to_str(ccx.tcx, self_ty));
|
2012-08-22 15:18:29 -05:00
|
|
|
match method.self_ty.node {
|
|
|
|
ast::sty_value => {
|
|
|
|
impl_owned_self(self_ty)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
impl_self(self_ty)
|
|
|
|
}
|
|
|
|
}
|
2012-08-21 21:51:43 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// generate the actual code
|
|
|
|
trans_fn(ccx,
|
|
|
|
path,
|
|
|
|
method.decl,
|
|
|
|
method.body,
|
|
|
|
llfn,
|
|
|
|
self_arg,
|
|
|
|
param_substs,
|
2012-10-08 14:39:30 -05:00
|
|
|
method.id,
|
|
|
|
Some(impl_id));
|
2012-08-21 21:51:43 -05:00
|
|
|
}
|
|
|
|
|
2012-10-08 14:39:30 -05:00
|
|
|
fn trans_self_arg(bcx: block,
|
|
|
|
base: @ast::expr,
|
2012-08-28 17:54:45 -05:00
|
|
|
mentry: typeck::method_map_entry) -> Result {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("impl::trans_self_arg");
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut temp_cleanups = ~[];
|
2012-10-08 14:39:30 -05:00
|
|
|
|
|
|
|
// Compute the mode and type of self.
|
2012-09-11 23:25:01 -05:00
|
|
|
let self_arg = {mode: mentry.self_arg.mode,
|
|
|
|
ty: monomorphize_type(bcx, mentry.self_arg.ty)};
|
2012-10-08 14:39:30 -05:00
|
|
|
|
2012-09-11 23:25:01 -05:00
|
|
|
let result = trans_arg_expr(bcx, self_arg, base,
|
2012-09-19 20:00:26 -05:00
|
|
|
&mut temp_cleanups, None, DontAutorefArg);
|
2012-09-11 23:25:01 -05:00
|
|
|
|
|
|
|
// FIXME(#3446)---this is wrong, actually. The temp_cleanups
|
|
|
|
// should be revoked only after all arguments have been passed.
|
|
|
|
for temp_cleanups.each |c| {
|
2012-09-19 18:55:01 -05:00
|
|
|
revoke_clean(bcx, *c)
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
2012-02-24 19:45:16 -06:00
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return result;
|
2012-01-02 09:50:51 -06:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn trans_method_callee(bcx: block, callee_id: ast::node_id,
|
2012-10-08 14:39:30 -05:00
|
|
|
self: @ast::expr, mentry: typeck::method_map_entry) ->
|
|
|
|
Callee {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("impl::trans_method_callee");
|
2012-10-08 14:39:30 -05:00
|
|
|
|
|
|
|
// Replace method_self with method_static here.
|
|
|
|
let mut origin = mentry.origin;
|
|
|
|
match origin {
|
|
|
|
typeck::method_self(copy trait_id, copy method_index) => {
|
|
|
|
// Get the ID of the impl we're inside.
|
|
|
|
let impl_def_id = bcx.fcx.impl_id.get();
|
|
|
|
|
2012-10-22 15:33:12 -05:00
|
|
|
debug!("impl_def_id is %?", impl_def_id);
|
2012-10-08 14:39:30 -05:00
|
|
|
|
|
|
|
// Get the ID of the method we're calling.
|
|
|
|
let method_name =
|
|
|
|
ty::trait_methods(bcx.tcx(), trait_id)[method_index].ident;
|
|
|
|
let method_id = method_with_name(bcx.ccx(), impl_def_id,
|
|
|
|
method_name);
|
|
|
|
origin = typeck::method_static(method_id);
|
|
|
|
}
|
|
|
|
typeck::method_static(*) | typeck::method_param(*) |
|
|
|
|
typeck::method_trait(*) => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
match origin {
|
2012-08-28 17:54:45 -05:00
|
|
|
typeck::method_static(did) => {
|
|
|
|
let callee_fn = callee::trans_fn_ref(bcx, did, callee_id);
|
|
|
|
let Result {bcx, val} = trans_self_arg(bcx, self, mentry);
|
2012-09-11 23:25:01 -05:00
|
|
|
let tcx = bcx.tcx();
|
2012-08-28 17:54:45 -05:00
|
|
|
Callee {
|
|
|
|
bcx: bcx,
|
|
|
|
data: Method(MethodData {
|
|
|
|
llfn: callee_fn.llfn,
|
|
|
|
llself: val,
|
|
|
|
self_ty: node_id_type(bcx, self.id),
|
2012-09-11 23:25:01 -05:00
|
|
|
self_mode: ty::resolved_mode(tcx, mentry.self_arg.mode)
|
2012-08-28 17:54:45 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
typeck::method_param({trait_id:trait_id, method_num:off,
|
|
|
|
param_num:p, bound_num:b}) => {
|
|
|
|
match bcx.fcx.param_substs {
|
2012-09-10 14:25:45 -05:00
|
|
|
Some(ref substs) => {
|
|
|
|
let vtbl = base::find_vtable(bcx.tcx(), substs, p, b);
|
2012-08-28 17:54:45 -05:00
|
|
|
trans_monomorphized_callee(bcx, callee_id, self, mentry,
|
|
|
|
trait_id, off, vtbl)
|
|
|
|
}
|
|
|
|
// how to get rid of this?
|
|
|
|
None => fail ~"trans_method_callee: missing param_substs"
|
|
|
|
}
|
|
|
|
}
|
2012-10-05 18:55:42 -05:00
|
|
|
typeck::method_trait(_, off, vstore) => {
|
2012-11-28 19:33:30 -06:00
|
|
|
trans_trait_callee(bcx,
|
|
|
|
callee_id,
|
|
|
|
off,
|
|
|
|
self,
|
|
|
|
vstore,
|
|
|
|
mentry.explicit_self)
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
2012-10-06 00:07:53 -05:00
|
|
|
typeck::method_self(*) => {
|
2012-10-08 14:39:30 -05:00
|
|
|
fail ~"method_self should have been handled above"
|
2012-10-05 20:51:36 -05:00
|
|
|
}
|
2012-01-26 05:26:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
fn trans_static_method_callee(bcx: block,
|
|
|
|
method_id: ast::def_id,
|
2012-10-12 19:00:08 -05:00
|
|
|
trait_id: ast::def_id,
|
2012-08-28 17:54:45 -05:00
|
|
|
callee_id: ast::node_id) -> FnData
|
|
|
|
{
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("impl::trans_static_method_callee");
|
2012-08-02 18:01:38 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
|
2012-10-12 19:00:08 -05:00
|
|
|
debug!("trans_static_method_callee(method_id=%?, trait_id=%s, \
|
|
|
|
callee_id=%?)",
|
|
|
|
method_id,
|
|
|
|
ty::item_path_str(bcx.tcx(), trait_id),
|
|
|
|
callee_id);
|
|
|
|
let _indenter = indenter();
|
|
|
|
|
|
|
|
// When we translate a static fn defined in a trait like:
|
|
|
|
//
|
|
|
|
// trait<T1...Tn> Trait {
|
|
|
|
// static fn foo<M1...Mn>(...) {...}
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// bound to. Due to the fact that we use a flattened list of
|
|
|
|
// impls, one per bound, this means we have to total up the bounds
|
|
|
|
// found on the type parametesr T1...Tn to find the index of the
|
|
|
|
// one we are interested in.
|
|
|
|
let bound_index = {
|
|
|
|
let trait_polyty = ty::lookup_item_type(bcx.tcx(), trait_id);
|
2012-11-28 14:34:30 -06:00
|
|
|
ty::count_traits_and_supertraits(bcx.tcx(), *trait_polyty.bounds)
|
2012-10-12 19:00:08 -05:00
|
|
|
};
|
|
|
|
|
2012-08-02 18:01:38 -05:00
|
|
|
let mname = if method_id.crate == ast::local_crate {
|
2012-08-23 19:39:07 -05:00
|
|
|
match bcx.tcx().items.get(method_id.node) {
|
2012-08-28 17:54:45 -05:00
|
|
|
ast_map::node_trait_method(trait_method, _, _) => {
|
|
|
|
ast_util::trait_method_to_ty_method(*trait_method).ident
|
|
|
|
}
|
|
|
|
_ => fail ~"callee is not a trait method"
|
2012-08-02 18:01:38 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let path = csearch::get_item_path(bcx.tcx(), method_id);
|
|
|
|
match path[path.len()-1] {
|
2012-08-28 17:54:45 -05:00
|
|
|
path_name(s) => { s }
|
|
|
|
path_mod(_) => { fail ~"path doesn't have a name?" }
|
2012-08-02 18:01:38 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
debug!("trans_static_method_callee: method_id=%?, callee_id=%?, \
|
2012-07-18 18:18:02 -05:00
|
|
|
name=%s", method_id, callee_id, ccx.sess.str_of(mname));
|
2012-08-02 18:01:38 -05:00
|
|
|
|
|
|
|
let vtbls = resolve_vtables_in_fn_ctxt(
|
|
|
|
bcx.fcx, ccx.maps.vtable_map.get(callee_id));
|
|
|
|
|
2012-10-12 19:00:08 -05:00
|
|
|
match vtbls[bound_index] {
|
2012-09-10 14:25:45 -05:00
|
|
|
typeck::vtable_static(impl_did, rcvr_substs, rcvr_origins) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
let mth_id = method_with_name(bcx.ccx(), impl_did, mname);
|
2012-09-10 14:25:45 -05:00
|
|
|
let callee_substs = combine_impl_and_methods_tps(
|
|
|
|
bcx, mth_id, impl_did, callee_id, rcvr_substs);
|
|
|
|
let callee_origins = combine_impl_and_methods_origins(
|
|
|
|
bcx, mth_id, impl_did, callee_id, rcvr_origins);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
let FnData {llfn: lval} =
|
2012-09-10 17:12:37 -05:00
|
|
|
trans_fn_ref_with_vtables(bcx,
|
|
|
|
mth_id,
|
|
|
|
callee_id,
|
|
|
|
callee_substs,
|
|
|
|
Some(callee_origins));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
let callee_ty = node_id_type(bcx, callee_id);
|
|
|
|
let llty = T_ptr(type_of_fn_from_ty(ccx, callee_ty));
|
|
|
|
FnData {llfn: PointerCast(bcx, lval, llty)}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
fail ~"vtable_param left in monomorphized \
|
|
|
|
function's vtable substs";
|
|
|
|
}
|
2012-08-02 18:01:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn method_from_methods(ms: ~[@ast::method], name: ast::ident)
|
2012-12-02 20:29:22 -06:00
|
|
|
-> Option<ast::def_id> {
|
|
|
|
ms.find(|m| m.ident == name).map(|m| local_def(m.id))
|
2012-04-13 14:22:35 -05:00
|
|
|
}
|
|
|
|
|
2012-03-08 09:10:25 -06:00
|
|
|
fn method_with_name(ccx: @crate_ctxt, impl_id: ast::def_id,
|
2012-03-08 05:15:02 -06:00
|
|
|
name: ast::ident) -> ast::def_id {
|
|
|
|
if impl_id.crate == ast::local_crate {
|
2012-08-23 19:39:07 -05:00
|
|
|
match ccx.tcx.items.get(impl_id.node) {
|
2012-11-13 21:08:01 -06:00
|
|
|
ast_map::node_item(@{node: ast::item_impl(_, _, _, ms), _}, _) => {
|
2012-12-02 20:29:22 -06:00
|
|
|
method_from_methods(ms, name).get()
|
2012-04-13 14:22:35 -05:00
|
|
|
}
|
2012-12-02 20:29:22 -06:00
|
|
|
_ => fail ~"method_with_name"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
csearch::get_impl_method(ccx.sess.cstore, impl_id, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn method_with_name_or_default(ccx: @crate_ctxt, impl_id: ast::def_id,
|
|
|
|
name: ast::ident) -> ast::def_id {
|
|
|
|
if impl_id.crate == ast::local_crate {
|
|
|
|
match ccx.tcx.items.get(impl_id.node) {
|
|
|
|
ast_map::node_item(@{node: ast::item_impl(_, _, _, ms), _}, _) => {
|
|
|
|
let did = method_from_methods(ms, name);
|
|
|
|
if did.is_some() {
|
|
|
|
return did.get();
|
|
|
|
} else {
|
|
|
|
// Look for a default method
|
|
|
|
let pmm = ccx.tcx.provided_methods;
|
|
|
|
match pmm.find(impl_id) {
|
|
|
|
Some(pmis) => {
|
|
|
|
for pmis.each |pmi| {
|
|
|
|
if pmi.method_info.ident == name {
|
|
|
|
debug!("XXX %?", pmi.method_info.did);
|
|
|
|
return pmi.method_info.did;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fail
|
|
|
|
}
|
|
|
|
None => fail
|
|
|
|
}
|
|
|
|
}
|
2012-03-08 05:15:02 -06:00
|
|
|
}
|
2012-08-23 19:39:07 -05:00
|
|
|
_ => fail ~"method_with_name"
|
2012-03-08 05:15:02 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
csearch::get_impl_method(ccx.sess.cstore, impl_id, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-23 02:25:14 -05:00
|
|
|
fn method_ty_param_count(ccx: @crate_ctxt, m_id: ast::def_id,
|
|
|
|
i_id: ast::def_id) -> uint {
|
2012-12-10 15:47:54 -06:00
|
|
|
debug!("method_ty_param_count: m_id: %?, i_id: %?", m_id, i_id);
|
2012-03-09 01:44:53 -06:00
|
|
|
if m_id.crate == ast::local_crate {
|
2012-12-02 20:29:22 -06:00
|
|
|
match ccx.tcx.items.find(m_id.node) {
|
|
|
|
Some(ast_map::node_method(m, _, _)) => m.tps.len(),
|
|
|
|
None => {
|
|
|
|
match ccx.tcx.provided_method_sources.find(m_id) {
|
|
|
|
Some(source) => {
|
2012-12-07 18:48:55 -06:00
|
|
|
method_ty_param_count(
|
|
|
|
ccx, source.method_id, source.impl_id)
|
2012-12-02 20:29:22 -06:00
|
|
|
}
|
|
|
|
None => fail
|
|
|
|
}
|
|
|
|
}
|
2012-12-10 15:47:54 -06:00
|
|
|
Some(ast_map::node_trait_method(@ast::provided(@ref m), _, _))
|
|
|
|
=> {
|
2012-12-02 20:29:22 -06:00
|
|
|
m.tps.len()
|
|
|
|
}
|
2012-12-10 15:47:54 -06:00
|
|
|
copy e => fail fmt!("method_ty_param_count %?", e)
|
2012-03-09 01:44:53 -06:00
|
|
|
}
|
|
|
|
} else {
|
2012-04-23 02:25:14 -05:00
|
|
|
csearch::get_type_param_count(ccx.sess.cstore, m_id) -
|
|
|
|
csearch::get_type_param_count(ccx.sess.cstore, i_id)
|
2012-03-09 01:44:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
fn trans_monomorphized_callee(bcx: block,
|
|
|
|
callee_id: ast::node_id,
|
2012-08-16 18:44:22 -05:00
|
|
|
base: @ast::expr,
|
|
|
|
mentry: typeck::method_map_entry,
|
2012-08-28 17:54:45 -05:00
|
|
|
trait_id: ast::def_id,
|
|
|
|
n_method: uint,
|
2012-08-02 18:01:38 -05:00
|
|
|
vtbl: typeck::vtable_origin)
|
2012-08-28 17:54:45 -05:00
|
|
|
-> Callee
|
|
|
|
{
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("impl::trans_monomorphized_callee");
|
2012-09-10 14:25:45 -05:00
|
|
|
return match vtbl {
|
|
|
|
typeck::vtable_static(impl_did, rcvr_substs, rcvr_origins) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
let mname = ty::trait_methods(ccx.tcx, trait_id)[n_method].ident;
|
2012-12-02 20:29:22 -06:00
|
|
|
let mth_id = method_with_name_or_default(
|
|
|
|
bcx.ccx(), impl_did, mname);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// obtain the `self` value:
|
|
|
|
let Result {bcx, val: llself_val} =
|
|
|
|
trans_self_arg(bcx, base, mentry);
|
|
|
|
|
|
|
|
// create a concatenated set of substitutions which includes
|
|
|
|
// those from the impl and those from the method:
|
2012-09-10 14:25:45 -05:00
|
|
|
let callee_substs = combine_impl_and_methods_tps(
|
|
|
|
bcx, mth_id, impl_did, callee_id, rcvr_substs);
|
|
|
|
let callee_origins = combine_impl_and_methods_origins(
|
|
|
|
bcx, mth_id, impl_did, callee_id, rcvr_origins);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// translate the function
|
|
|
|
let callee = trans_fn_ref_with_vtables(
|
2012-09-10 14:25:45 -05:00
|
|
|
bcx, mth_id, callee_id, callee_substs, Some(callee_origins));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// create a llvalue that represents the fn ptr
|
|
|
|
let fn_ty = node_id_type(bcx, callee_id);
|
|
|
|
let llfn_ty = T_ptr(type_of_fn_from_ty(ccx, fn_ty));
|
|
|
|
let llfn_val = PointerCast(bcx, callee.llfn, llfn_ty);
|
|
|
|
|
|
|
|
// combine the self environment with the rest
|
2012-09-11 23:25:01 -05:00
|
|
|
let tcx = bcx.tcx();
|
2012-08-28 17:54:45 -05:00
|
|
|
Callee {
|
|
|
|
bcx: bcx,
|
|
|
|
data: Method(MethodData {
|
|
|
|
llfn: llfn_val,
|
|
|
|
llself: llself_val,
|
|
|
|
self_ty: node_id_type(bcx, base.id),
|
2012-09-11 23:25:01 -05:00
|
|
|
self_mode: ty::resolved_mode(tcx, mentry.self_arg.mode)
|
2012-08-28 17:54:45 -05:00
|
|
|
})
|
|
|
|
}
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
2012-10-05 18:55:42 -05:00
|
|
|
typeck::vtable_trait(_, _) => {
|
2012-11-28 19:33:30 -06:00
|
|
|
trans_trait_callee(bcx,
|
|
|
|
callee_id,
|
|
|
|
n_method,
|
|
|
|
base,
|
|
|
|
ty::vstore_box,
|
|
|
|
mentry.explicit_self)
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
2012-08-26 11:58:45 -05:00
|
|
|
typeck::vtable_param(*) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
fail ~"vtable_param left in monomorphized function's vtable substs";
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
2012-09-10 14:25:45 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn combine_impl_and_methods_tps(bcx: block,
|
|
|
|
mth_did: ast::def_id,
|
|
|
|
impl_did: ast::def_id,
|
|
|
|
callee_id: ast::node_id,
|
|
|
|
rcvr_substs: ~[ty::t])
|
|
|
|
-> ~[ty::t]
|
|
|
|
{
|
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* 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();
|
|
|
|
let n_m_tps = method_ty_param_count(ccx, mth_did, impl_did);
|
|
|
|
let node_substs = node_id_type_params(bcx, callee_id);
|
|
|
|
let ty_substs
|
|
|
|
= vec::append(rcvr_substs,
|
|
|
|
vec::tailn(node_substs,
|
|
|
|
node_substs.len() - n_m_tps));
|
|
|
|
debug!("n_m_tps=%?", n_m_tps);
|
2012-09-21 20:43:30 -05:00
|
|
|
debug!("rcvr_substs=%?", rcvr_substs.map(|t| bcx.ty_to_str(*t)));
|
|
|
|
debug!("node_substs=%?", node_substs.map(|t| bcx.ty_to_str(*t)));
|
|
|
|
debug!("ty_substs=%?", ty_substs.map(|t| bcx.ty_to_str(*t)));
|
2012-09-10 14:25:45 -05:00
|
|
|
|
|
|
|
return ty_substs;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn combine_impl_and_methods_origins(bcx: block,
|
|
|
|
mth_did: ast::def_id,
|
|
|
|
impl_did: ast::def_id,
|
|
|
|
callee_id: ast::node_id,
|
|
|
|
rcvr_origins: typeck::vtable_res)
|
|
|
|
-> typeck::vtable_res
|
|
|
|
{
|
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Similar to `combine_impl_and_methods_tps`, but for vtables.
|
|
|
|
* This is much messier because of the flattened layout we are
|
|
|
|
* currently using (for some reason that I fail to understand).
|
|
|
|
* The proper fix is described in #3446.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
// Find the bounds for the method, which are the tail of the
|
|
|
|
// bounds found in the item type, as the item type combines the
|
|
|
|
// rcvr + method bounds.
|
|
|
|
let ccx = bcx.ccx(), tcx = bcx.tcx();
|
|
|
|
let n_m_tps = method_ty_param_count(ccx, mth_did, impl_did);
|
|
|
|
let {bounds: r_m_bounds, _} = ty::lookup_item_type(tcx, mth_did);
|
|
|
|
let n_r_m_tps = r_m_bounds.len(); // rcvr + method tps
|
|
|
|
let m_boundss = vec::view(*r_m_bounds, n_r_m_tps - n_m_tps, n_r_m_tps);
|
|
|
|
|
|
|
|
// Flatten out to find the number of vtables the method expects.
|
2012-12-12 18:50:28 -06:00
|
|
|
let m_vtables = ty::count_traits_and_supertraits(tcx, m_boundss);
|
2012-09-10 14:25:45 -05:00
|
|
|
|
|
|
|
// Find the vtables we computed at type check time and monomorphize them
|
|
|
|
let r_m_origins = match node_vtables(bcx, callee_id) {
|
|
|
|
Some(vt) => vt,
|
|
|
|
None => @~[]
|
|
|
|
};
|
|
|
|
|
|
|
|
// Extract those that belong to method:
|
|
|
|
let m_origins = vec::tailn(*r_m_origins, r_m_origins.len() - m_vtables);
|
|
|
|
|
|
|
|
// Combine rcvr + method to find the final result:
|
|
|
|
@vec::append(*rcvr_origins, m_origins)
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
|
|
|
|
2012-09-10 14:25:45 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
fn trans_trait_callee(bcx: block,
|
|
|
|
callee_id: ast::node_id,
|
|
|
|
n_method: uint,
|
2012-10-05 18:55:42 -05:00
|
|
|
self_expr: @ast::expr,
|
2012-11-28 19:33:30 -06:00
|
|
|
vstore: ty::vstore,
|
|
|
|
explicit_self: ast::self_ty_)
|
|
|
|
-> Callee
|
2012-08-28 17:54:45 -05:00
|
|
|
{
|
|
|
|
//!
|
|
|
|
//
|
|
|
|
// Create a method callee where the method is coming from a trait
|
|
|
|
// instance (e.g., @Trait type). In this case, we must pull the
|
|
|
|
// fn pointer out of the vtable that is packaged up with the
|
2012-10-05 18:55:42 -05:00
|
|
|
// @/~/&Trait instance. @/~/&Traits are represented as a pair, so we
|
|
|
|
// first evaluate the self expression (expected a by-ref result) and then
|
2012-08-28 17:54:45 -05:00
|
|
|
// extract the self data and vtable out of the pair.
|
|
|
|
|
|
|
|
let _icx = bcx.insn_ctxt("impl::trans_trait_callee");
|
|
|
|
let mut bcx = bcx;
|
2012-12-13 15:09:38 -06:00
|
|
|
let self_datum = unpack_datum!(bcx,
|
|
|
|
expr::trans_to_datum(bcx, self_expr));
|
2012-08-28 17:54:45 -05:00
|
|
|
let llpair = self_datum.to_ref_llval(bcx);
|
2012-12-13 15:09:38 -06:00
|
|
|
|
|
|
|
let llpair = match explicit_self {
|
|
|
|
ast::sty_region(_) => Load(bcx, llpair),
|
|
|
|
ast::sty_static | ast::sty_by_ref | ast::sty_value |
|
|
|
|
ast::sty_box(_) | ast::sty_uniq(_) => llpair
|
|
|
|
};
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let callee_ty = node_id_type(bcx, callee_id);
|
2012-11-28 19:33:30 -06:00
|
|
|
trans_trait_callee_from_llval(bcx,
|
|
|
|
callee_ty,
|
|
|
|
n_method,
|
|
|
|
llpair,
|
|
|
|
vstore,
|
|
|
|
explicit_self)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_trait_callee_from_llval(bcx: block,
|
|
|
|
callee_ty: ty::t,
|
|
|
|
n_method: uint,
|
2012-10-05 18:55:42 -05:00
|
|
|
llpair: ValueRef,
|
2012-11-28 19:33:30 -06:00
|
|
|
vstore: ty::vstore,
|
|
|
|
explicit_self: ast::self_ty_)
|
|
|
|
-> Callee
|
2012-08-28 17:54:45 -05:00
|
|
|
{
|
|
|
|
//!
|
|
|
|
//
|
|
|
|
// Same as `trans_trait_callee()` above, except that it is given
|
|
|
|
// a by-ref pointer to the @Trait pair.
|
|
|
|
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("impl::trans_trait_callee");
|
2012-05-14 17:48:58 -05:00
|
|
|
let ccx = bcx.ccx();
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
|
|
|
|
// Load the vtable from the @Trait pair
|
2012-11-28 19:33:30 -06:00
|
|
|
debug!("(translating trait callee) loading vtable from pair %s",
|
|
|
|
val_str(bcx.ccx().tn, llpair));
|
2012-08-28 17:54:45 -05:00
|
|
|
let llvtable = Load(bcx,
|
|
|
|
PointerCast(bcx,
|
|
|
|
GEPi(bcx, llpair, [0u, 0u]),
|
|
|
|
T_ptr(T_ptr(T_vtable()))));
|
|
|
|
|
2012-10-05 18:55:42 -05:00
|
|
|
// Load the box from the @Trait pair and GEP over the box header if
|
|
|
|
// necessary:
|
2012-11-28 19:33:30 -06:00
|
|
|
let mut llself;
|
|
|
|
debug!("(translating trait callee) loading second index from pair");
|
2012-08-28 17:54:45 -05:00
|
|
|
let llbox = Load(bcx, GEPi(bcx, llpair, [0u, 1u]));
|
|
|
|
|
2012-11-28 19:33:30 -06:00
|
|
|
// Munge `llself` appropriately for the type of `self` in the method.
|
2012-11-29 12:59:49 -06:00
|
|
|
let self_mode;
|
2012-11-28 19:33:30 -06:00
|
|
|
match explicit_self {
|
|
|
|
ast::sty_static => {
|
|
|
|
bcx.tcx().sess.bug(~"shouldn't see static method here");
|
|
|
|
}
|
2012-11-29 12:59:49 -06:00
|
|
|
ast::sty_by_ref => {
|
|
|
|
// We need to pass a pointer to a pointer to the payload.
|
|
|
|
match vstore {
|
|
|
|
ty::vstore_box | ty::vstore_uniq => {
|
|
|
|
llself = GEPi(bcx, llbox, [0u, abi::box_field_body]);
|
|
|
|
}
|
|
|
|
ty::vstore_slice(_) => {
|
|
|
|
llself = llbox;
|
|
|
|
}
|
|
|
|
ty::vstore_fixed(*) => {
|
|
|
|
bcx.tcx().sess.bug(~"vstore_fixed trait");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self_mode = ast::by_ref;
|
|
|
|
}
|
2012-11-28 19:33:30 -06:00
|
|
|
ast::sty_value => {
|
|
|
|
bcx.tcx().sess.bug(~"methods with by-value self should not be \
|
|
|
|
called on objects");
|
|
|
|
}
|
|
|
|
ast::sty_region(_) => {
|
2012-11-29 12:59:49 -06:00
|
|
|
// As before, we need to pass a pointer to a pointer to the
|
|
|
|
// payload.
|
|
|
|
match vstore {
|
|
|
|
ty::vstore_box | ty::vstore_uniq => {
|
|
|
|
llself = GEPi(bcx, llbox, [0u, abi::box_field_body]);
|
|
|
|
}
|
|
|
|
ty::vstore_slice(_) => {
|
|
|
|
llself = llbox;
|
|
|
|
}
|
|
|
|
ty::vstore_fixed(*) => {
|
|
|
|
bcx.tcx().sess.bug(~"vstore_fixed trait");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 19:33:30 -06:00
|
|
|
let llscratch = alloca(bcx, val_ty(llself));
|
|
|
|
Store(bcx, llself, llscratch);
|
|
|
|
llself = llscratch;
|
2012-11-29 12:59:49 -06:00
|
|
|
|
|
|
|
self_mode = ast::by_ref;
|
2012-11-28 19:33:30 -06:00
|
|
|
}
|
|
|
|
ast::sty_box(_) => {
|
|
|
|
// Bump the reference count on the box.
|
|
|
|
debug!("(translating trait callee) callee type is `%s`",
|
|
|
|
bcx.ty_to_str(callee_ty));
|
2012-11-29 12:59:49 -06:00
|
|
|
bcx = glue::take_ty(bcx, llbox, callee_ty);
|
|
|
|
|
|
|
|
// Pass a pointer to the box.
|
|
|
|
match vstore {
|
|
|
|
ty::vstore_box => llself = llbox,
|
|
|
|
_ => bcx.tcx().sess.bug(~"@self receiver with non-@Trait")
|
|
|
|
}
|
|
|
|
|
|
|
|
let llscratch = alloca(bcx, val_ty(llself));
|
|
|
|
Store(bcx, llself, llscratch);
|
|
|
|
llself = llscratch;
|
|
|
|
|
|
|
|
self_mode = ast::by_ref;
|
|
|
|
}
|
|
|
|
ast::sty_uniq(_) => {
|
|
|
|
// Pass the unique pointer.
|
|
|
|
match vstore {
|
|
|
|
ty::vstore_uniq => llself = llbox,
|
|
|
|
_ => bcx.tcx().sess.bug(~"~self receiver with non-~Trait")
|
|
|
|
}
|
|
|
|
|
|
|
|
let llscratch = alloca(bcx, val_ty(llself));
|
|
|
|
Store(bcx, llself, llscratch);
|
|
|
|
llself = llscratch;
|
|
|
|
|
|
|
|
self_mode = ast::by_ref;
|
2012-11-28 19:33:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
// Load the function from the vtable and cast it to the expected type.
|
2012-11-28 19:33:30 -06:00
|
|
|
debug!("(translating trait callee) loading method");
|
2012-08-28 17:54:45 -05:00
|
|
|
let llcallee_ty = type_of::type_of_fn_from_ty(ccx, callee_ty);
|
|
|
|
let mptr = Load(bcx, GEPi(bcx, llvtable, [0u, n_method]));
|
|
|
|
let mptr = PointerCast(bcx, mptr, T_ptr(llcallee_ty));
|
|
|
|
|
|
|
|
return Callee {
|
|
|
|
bcx: bcx,
|
|
|
|
data: Method(MethodData {
|
|
|
|
llfn: mptr,
|
|
|
|
llself: llself,
|
|
|
|
self_ty: ty::mk_opaque_box(bcx.tcx()),
|
2012-11-29 12:59:49 -06:00
|
|
|
self_mode: self_mode,
|
2012-08-28 17:54:45 -05:00
|
|
|
/* XXX: Some(llbox) */
|
|
|
|
})
|
|
|
|
};
|
2012-01-07 15:44:14 -06:00
|
|
|
}
|
|
|
|
|
2012-03-12 10:31:22 -05:00
|
|
|
fn vtable_id(ccx: @crate_ctxt, origin: typeck::vtable_origin) -> mono_id {
|
2012-08-23 19:39:07 -05:00
|
|
|
match origin {
|
2012-08-28 17:54:45 -05:00
|
|
|
typeck::vtable_static(impl_id, substs, sub_vtables) => {
|
|
|
|
monomorphize::make_mono_id(
|
2012-10-08 14:39:30 -05:00
|
|
|
ccx,
|
|
|
|
impl_id,
|
|
|
|
substs,
|
|
|
|
if (*sub_vtables).len() == 0u {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(sub_vtables)
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
None)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
typeck::vtable_trait(trait_id, substs) => {
|
2013-01-06 13:16:14 -06:00
|
|
|
@mono_id_ {
|
|
|
|
def: trait_id,
|
|
|
|
params: vec::map(substs, |t| mono_precise(*t, None)),
|
|
|
|
impl_did_opt: None
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
// can't this be checked at the callee?
|
|
|
|
_ => fail ~"vtable_id"
|
2012-03-08 09:10:25 -06:00
|
|
|
}
|
2012-01-12 09:57:30 -06:00
|
|
|
}
|
|
|
|
|
2012-03-08 09:17:59 -06:00
|
|
|
fn get_vtable(ccx: @crate_ctxt, origin: typeck::vtable_origin)
|
2012-01-12 09:57:30 -06:00
|
|
|
-> ValueRef {
|
2012-03-12 10:31:22 -05:00
|
|
|
let hash_id = vtable_id(ccx, origin);
|
2012-08-06 14:34:08 -05:00
|
|
|
match ccx.vtables.find(hash_id) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(val) => val,
|
|
|
|
None => match origin {
|
2012-08-03 21:59:04 -05:00
|
|
|
typeck::vtable_static(id, substs, sub_vtables) => {
|
2012-03-08 09:17:59 -06:00
|
|
|
make_impl_vtable(ccx, id, substs, sub_vtables)
|
2012-01-02 09:50:51 -06:00
|
|
|
}
|
2012-08-23 19:39:07 -05:00
|
|
|
_ => fail ~"get_vtable: expected a static origin"
|
2012-03-08 09:10:25 -06:00
|
|
|
}
|
2012-01-02 09:50:51 -06:00
|
|
|
}
|
2012-01-12 09:57:30 -06:00
|
|
|
}
|
2012-01-02 06:26:51 -06:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn make_vtable(ccx: @crate_ctxt, ptrs: ~[ValueRef]) -> ValueRef {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = ccx.insn_ctxt("impl::make_vtable");
|
2012-03-08 09:10:25 -06:00
|
|
|
let tbl = C_struct(ptrs);
|
2012-11-29 19:51:16 -06:00
|
|
|
let vt_gvar =
|
|
|
|
str::as_c_str(ccx.sess.str_of((ccx.names)(~"vtable")), |buf| {
|
2012-03-08 09:10:25 -06:00
|
|
|
llvm::LLVMAddGlobal(ccx.llmod, val_ty(tbl), buf)
|
2012-01-12 09:57:30 -06:00
|
|
|
});
|
2012-03-08 09:10:25 -06: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 09:57:30 -06:00
|
|
|
}
|
2012-01-02 06:26:51 -06:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn make_impl_vtable(ccx: @crate_ctxt, impl_id: ast::def_id, substs: ~[ty::t],
|
2012-03-08 09:17:59 -06:00
|
|
|
vtables: typeck::vtable_res) -> ValueRef {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = ccx.insn_ctxt("impl::make_impl_vtable");
|
2012-03-08 09:10:25 -06:00
|
|
|
let tcx = ccx.tcx;
|
2012-07-18 18:49:55 -05:00
|
|
|
|
|
|
|
// XXX: This should support multiple traits.
|
2012-08-28 17:54:45 -05:00
|
|
|
let trt_id = driver::session::expect(
|
|
|
|
tcx.sess,
|
2012-10-05 18:55:42 -05:00
|
|
|
ty::ty_to_def_id(ty::impl_traits(tcx, impl_id, ty::vstore_box)[0]),
|
2012-08-28 17:54:45 -05:00
|
|
|
|| ~"make_impl_vtable: non-trait-type implemented");
|
2012-07-18 18:49:55 -05:00
|
|
|
|
2012-03-15 09:07:25 -05:00
|
|
|
let has_tps = (*ty::lookup_item_type(ccx.tcx, impl_id).bounds).len() > 0u;
|
2012-07-30 16:43:44 -05:00
|
|
|
make_vtable(ccx, vec::map(*ty::trait_methods(tcx, trt_id), |im| {
|
2012-10-08 14:39:30 -05:00
|
|
|
let fty = ty::subst_tps(tcx, substs, None, ty::mk_fn(tcx, im.fty));
|
2012-05-09 08:09:58 -05:00
|
|
|
if (*im.tps).len() > 0u || ty::type_has_self(fty) {
|
2012-11-28 19:33:30 -06:00
|
|
|
debug!("(making impl vtable) method has self or type params: %s",
|
|
|
|
tcx.sess.str_of(im.ident));
|
2012-03-16 08:28:45 -05:00
|
|
|
C_null(T_ptr(T_nil()))
|
2012-03-08 09:10:25 -06:00
|
|
|
} else {
|
2012-11-28 19:33:30 -06:00
|
|
|
debug!("(making impl vtable) adding method to vtable: %s",
|
|
|
|
tcx.sess.str_of(im.ident));
|
2012-05-29 16:39:22 -05:00
|
|
|
let mut m_id = method_with_name(ccx, impl_id, im.ident);
|
2012-03-15 09:07:25 -05:00
|
|
|
if has_tps {
|
2012-05-29 16:39:22 -05:00
|
|
|
// If the method is in another crate, need to make an inlined
|
|
|
|
// copy first
|
|
|
|
if m_id.crate != ast::local_crate {
|
2012-10-08 14:39:30 -05:00
|
|
|
// XXX: Set impl ID here?
|
|
|
|
m_id = inline::maybe_instantiate_inline(ccx, m_id, true);
|
2012-05-29 16:39:22 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
monomorphize::monomorphic_fn(ccx, m_id, substs,
|
2012-10-08 14:39:30 -05:00
|
|
|
Some(vtables), None, None).val
|
2012-03-15 09:07:25 -05:00
|
|
|
} else if m_id.crate == ast::local_crate {
|
|
|
|
get_item_val(ccx, m_id.node)
|
|
|
|
} else {
|
|
|
|
trans_external_path(ccx, m_id, fty)
|
|
|
|
}
|
2012-01-12 09:57:30 -06:00
|
|
|
}
|
2012-06-26 15:55:56 -05:00
|
|
|
}))
|
2012-01-12 09:57:30 -06:00
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
fn trans_trait_cast(bcx: block,
|
|
|
|
val: @ast::expr,
|
|
|
|
id: ast::node_id,
|
2012-10-31 17:09:26 -05:00
|
|
|
dest: expr::Dest,
|
|
|
|
vstore: ty::vstore)
|
2012-08-28 17:54:45 -05:00
|
|
|
-> block
|
|
|
|
{
|
2012-10-03 17:18:50 -05:00
|
|
|
let mut bcx = bcx;
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("impl::trans_cast");
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
let lldest = match dest {
|
|
|
|
Ignore => {
|
|
|
|
return expr::trans_into(bcx, val, Ignore);
|
|
|
|
}
|
|
|
|
SaveIn(dest) => dest
|
|
|
|
};
|
|
|
|
|
2012-02-21 07:20:18 -06:00
|
|
|
let ccx = bcx.ccx();
|
2012-02-10 04:32:03 -06:00
|
|
|
let v_ty = expr_ty(bcx, val);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-10-31 17:09:26 -05:00
|
|
|
match vstore {
|
|
|
|
ty::vstore_slice(*) | ty::vstore_box => {
|
|
|
|
let mut llboxdest = GEPi(bcx, lldest, [0u, 1u]);
|
|
|
|
if bcx.tcx().legacy_boxed_traits.contains_key(id) {
|
|
|
|
// Allocate an @ box and store the value into it
|
|
|
|
let {bcx: new_bcx, box: llbox, body: body} =
|
|
|
|
malloc_boxed(bcx, v_ty);
|
|
|
|
bcx = new_bcx;
|
|
|
|
add_clean_free(bcx, llbox, heap_shared);
|
|
|
|
bcx = expr::trans_into(bcx, val, SaveIn(body));
|
|
|
|
revoke_clean(bcx, llbox);
|
|
|
|
|
|
|
|
// Store the @ box into the pair
|
|
|
|
Store(bcx, llbox, PointerCast(bcx,
|
|
|
|
llboxdest,
|
|
|
|
T_ptr(val_ty(llbox))));
|
|
|
|
} else {
|
|
|
|
// Just store the pointer into the pair.
|
|
|
|
llboxdest = PointerCast(bcx,
|
|
|
|
llboxdest,
|
|
|
|
T_ptr(type_of::type_of(bcx.ccx(),
|
|
|
|
v_ty)));
|
|
|
|
bcx = expr::trans_into(bcx, val, SaveIn(llboxdest));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::vstore_uniq => {
|
|
|
|
// Translate the uniquely-owned value into the second element of
|
|
|
|
// the triple. (The first element is the vtable.)
|
|
|
|
let mut llvaldest = GEPi(bcx, lldest, [0, 1]);
|
|
|
|
llvaldest = PointerCast(bcx,
|
|
|
|
llvaldest,
|
|
|
|
T_ptr(type_of::type_of(bcx.ccx(), v_ty)));
|
|
|
|
bcx = expr::trans_into(bcx, val, SaveIn(llvaldest));
|
|
|
|
|
|
|
|
// Get the type descriptor of the wrapped value and store it into
|
|
|
|
// the third element of the triple as well.
|
|
|
|
let tydesc = get_tydesc(bcx.ccx(), v_ty);
|
|
|
|
glue::lazily_emit_all_tydesc_glue(bcx.ccx(), tydesc);
|
|
|
|
let lltydescdest = GEPi(bcx, lldest, [0, 2]);
|
|
|
|
Store(bcx, tydesc.tydesc, lltydescdest);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(val.span, ~"unexpected vstore in \
|
|
|
|
trans_trait_cast");
|
|
|
|
}
|
2012-10-03 17:18:50 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-10-31 17:09:26 -05:00
|
|
|
// Store the vtable into the pair or triple.
|
2012-03-08 09:17:59 -06:00
|
|
|
let orig = ccx.maps.vtable_map.get(id)[0];
|
2012-03-08 09:10:25 -06:00
|
|
|
let orig = resolve_vtable_in_fn_ctxt(bcx.fcx, orig);
|
|
|
|
let vtable = get_vtable(bcx.ccx(), orig);
|
2012-08-28 17:54:45 -05:00
|
|
|
Store(bcx, vtable, PointerCast(bcx,
|
|
|
|
GEPi(bcx, lldest, [0u, 0u]),
|
2012-03-08 09:10:25 -06:00
|
|
|
T_ptr(val_ty(vtable))));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-02-10 04:32:03 -06:00
|
|
|
bcx
|
2012-01-07 15:44:14 -06:00
|
|
|
}
|