2012-03-12 22:04:27 -05:00
|
|
|
import libc::c_uint;
|
2012-01-27 06:17:06 -06:00
|
|
|
import base::*;
|
|
|
|
import common::*;
|
2012-02-21 08:22:55 -06:00
|
|
|
import type_of::*;
|
2012-01-27 06:17:06 -06:00
|
|
|
import build::*;
|
2012-01-30 23:00:57 -06:00
|
|
|
import driver::session::session;
|
2012-05-22 01:34:34 -05:00
|
|
|
import syntax::{ast, ast_map};
|
|
|
|
import ast_map::{path, path_mod, path_name, node_id_to_str};
|
2012-05-22 20:44:28 -05:00
|
|
|
import driver::session::expect;
|
2012-08-15 17:53:58 -05:00
|
|
|
import syntax::ast_util::local_def;
|
2012-01-05 06:57:27 -06:00
|
|
|
import metadata::csearch;
|
2012-01-07 15:44:14 -06:00
|
|
|
import back::{link, abi};
|
2012-01-06 07:22:31 -06:00
|
|
|
import lib::llvm::llvm;
|
2012-02-01 04:04:56 -06:00
|
|
|
import lib::llvm::{ValueRef, TypeRef};
|
|
|
|
import lib::llvm::llvm::LLVMGetParam;
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2012-08-06 21:16:44 -05:00
|
|
|
import util::ppaux::{ty_to_str, tys_to_str};
|
|
|
|
|
|
|
|
import syntax::print::pprust::expr_to_str;
|
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-06-29 18:26:56 -05:00
|
|
|
methods: ~[@ast::method], tps: ~[ast::ty_param]) {
|
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-08-21 21:51:43 -05:00
|
|
|
for vec::each(methods) |method| {
|
|
|
|
if method.tps.len() == 0u {
|
|
|
|
let llfn = get_item_val(ccx, method.id);
|
|
|
|
let path = vec::append_one(sub_path, path_name(method.ident));
|
|
|
|
trans_method(ccx, path, method, none, llfn);
|
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
|
|
|
|
- `llfn`: the LLVM ValueRef for the method
|
|
|
|
*/
|
|
|
|
fn trans_method(ccx: @crate_ctxt,
|
|
|
|
path: path,
|
|
|
|
method: &ast::method,
|
|
|
|
param_substs: option<param_substs>,
|
|
|
|
llfn: ValueRef) {
|
|
|
|
|
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
|
|
|
|
let self_ty = ty::node_id_to_type(ccx.tcx, method.self_id);
|
|
|
|
let self_ty = match param_substs {
|
|
|
|
none => self_ty,
|
|
|
|
some({tys: ref tys, _}) => ty::subst_tps(ccx.tcx, *tys, self_ty)
|
|
|
|
};
|
|
|
|
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,
|
|
|
|
method.id);
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:44:22 -05:00
|
|
|
fn trans_self_arg(bcx: block, base: @ast::expr,
|
|
|
|
mentry: typeck::method_map_entry) -> result {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("impl::trans_self_arg");
|
2012-02-02 05:37:17 -06:00
|
|
|
let basety = expr_ty(bcx, base);
|
2012-08-16 18:44:22 -05:00
|
|
|
let mode = ast::expl(mentry.self_mode);
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut temp_cleanups = ~[];
|
2012-08-16 18:44:22 -05:00
|
|
|
let result = trans_arg_expr(bcx, {mode: mode, ty: basety},
|
2012-03-12 04:05:15 -05:00
|
|
|
T_ptr(type_of::type_of(bcx.ccx(), basety)),
|
2012-08-16 18:44:22 -05:00
|
|
|
base, temp_cleanups, none, mentry.derefs);
|
2012-02-24 19:45:16 -06:00
|
|
|
|
|
|
|
// by-ref self argument should not require cleanup in the case of
|
|
|
|
// other arguments failing:
|
2012-08-16 18:44:22 -05:00
|
|
|
//assert temp_cleanups == ~[];
|
|
|
|
//do vec::iter(temp_cleanups) |c| {
|
|
|
|
// revoke_clean(bcx, c)
|
|
|
|
//}
|
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-06-07 12:51:21 -05:00
|
|
|
self: @ast::expr, mentry: typeck::method_map_entry)
|
2012-01-26 05:26:14 -06:00
|
|
|
-> lval_maybe_callee {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("impl::trans_method_callee");
|
2012-08-06 14:34:08 -05:00
|
|
|
match mentry.origin {
|
2012-08-03 21:59:04 -05:00
|
|
|
typeck::method_static(did) => {
|
2012-08-16 18:44:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
let {bcx, val} = trans_self_arg(bcx, self, mentry);
|
|
|
|
{env: self_env(val, node_id_type(bcx, self.id), none,
|
|
|
|
mentry.self_mode)
|
2012-03-09 06:35:20 -06:00
|
|
|
with lval_static_fn(bcx, did, callee_id)}
|
2012-01-26 05:26:14 -06:00
|
|
|
}
|
2012-08-10 17:13:24 -05:00
|
|
|
typeck::method_param({trait_id:trait_id, method_num:off,
|
2012-08-03 21:59:04 -05:00
|
|
|
param_num:p, bound_num:b}) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match check bcx.fcx.param_substs {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(substs) => {
|
2012-08-02 18:01:38 -05:00
|
|
|
let vtbl = find_vtable_in_fn_ctxt(substs, p, b);
|
2012-08-16 18:44:22 -05:00
|
|
|
trans_monomorphized_callee(bcx, callee_id, self, mentry,
|
2012-08-10 17:13:24 -05:00
|
|
|
trait_id, off, vtbl)
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
|
|
|
}
|
2012-01-26 05:26:14 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
typeck::method_trait(_, off) => {
|
2012-05-14 17:48:58 -05:00
|
|
|
let {bcx, val} = trans_temp_expr(bcx, self);
|
|
|
|
let fty = node_id_type(bcx, callee_id);
|
2012-08-06 21:16:44 -05:00
|
|
|
let self_ty = node_id_type(bcx, self.id);
|
|
|
|
let {bcx, val, _} = autoderef(bcx, self.id, val, self_ty,
|
|
|
|
uint::max_value);
|
2012-07-03 18:30:42 -05:00
|
|
|
trans_trait_callee(bcx, val, fty, off)
|
2012-01-26 05:26:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 18:01:38 -05:00
|
|
|
fn trans_static_method_callee(bcx: block, method_id: ast::def_id,
|
|
|
|
callee_id: ast::node_id) -> lval_maybe_callee {
|
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();
|
|
|
|
|
|
|
|
let mname = if method_id.crate == ast::local_crate {
|
|
|
|
match check bcx.tcx().items.get(method_id.node) {
|
|
|
|
ast_map::node_trait_method(trait_method, _, _) => {
|
|
|
|
ast_util::trait_method_to_ty_method(*trait_method).ident
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let path = csearch::get_item_path(bcx.tcx(), method_id);
|
|
|
|
match path[path.len()-1] {
|
|
|
|
path_name(s) => { s }
|
|
|
|
path_mod(_) => { fail ~"path doesn't have a name?" }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
debug!("trans_static_method_callee: method_id=%?, callee_id=%?, \
|
|
|
|
name=%s", method_id, callee_id, *mname);
|
|
|
|
|
|
|
|
let vtbls = resolve_vtables_in_fn_ctxt(
|
|
|
|
bcx.fcx, ccx.maps.vtable_map.get(callee_id));
|
|
|
|
|
|
|
|
match vtbls[0] { // is index 0 always the one we want?
|
|
|
|
typeck::vtable_static(impl_did, impl_substs, sub_origins) => {
|
|
|
|
|
|
|
|
let mth_id = method_with_name(bcx.ccx(), impl_did, mname);
|
|
|
|
let n_m_tps = method_ty_param_count(ccx, mth_id, impl_did);
|
|
|
|
let node_substs = node_id_type_params(bcx, callee_id);
|
|
|
|
let ty_substs
|
|
|
|
= vec::append(impl_substs,
|
|
|
|
vec::tailn(node_substs,
|
|
|
|
node_substs.len() - n_m_tps));
|
|
|
|
|
|
|
|
let lval = lval_static_fn_inner(bcx, mth_id, callee_id, ty_substs,
|
|
|
|
some(sub_origins));
|
|
|
|
{env: null_env,
|
|
|
|
val: PointerCast(bcx, lval.val, T_ptr(type_of_fn_from_ty(
|
|
|
|
ccx, node_id_type(bcx, callee_id))))
|
|
|
|
with lval}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
fail ~"vtable_param left in monomorphized function's vtable substs";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn method_from_methods(ms: ~[@ast::method], name: ast::ident)
|
2012-06-25 22:00:46 -05:00
|
|
|
-> ast::def_id {
|
2012-06-30 18:19:07 -05:00
|
|
|
local_def(option::get(vec::find(ms, |m| m.ident == name)).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-06 14:34:08 -05:00
|
|
|
match check ccx.tcx.items.get(impl_id.node) {
|
2012-08-03 21:59:04 -05:00
|
|
|
ast_map::node_item(@{node: ast::item_impl(_, _, _, ms), _}, _) => {
|
2012-04-13 14:22:35 -05:00
|
|
|
method_from_methods(ms, name)
|
|
|
|
}
|
|
|
|
ast_map::node_item(@{node:
|
2012-08-07 17:34:07 -05:00
|
|
|
ast::item_class(struct_def, _), _}, _) => {
|
2012-08-15 17:53:58 -05:00
|
|
|
method_from_methods(struct_def.methods, 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-03-09 01:44:53 -06:00
|
|
|
if m_id.crate == ast::local_crate {
|
2012-08-06 14:34:08 -05:00
|
|
|
match check ccx.tcx.items.get(m_id.node) {
|
2012-08-03 21:59:04 -05:00
|
|
|
ast_map::node_method(m, _, _) => vec::len(m.tps),
|
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-02-17 06:17:40 -06: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-07-03 18:30:42 -05:00
|
|
|
trait_id: ast::def_id, n_method: uint,
|
2012-08-02 18:01:38 -05:00
|
|
|
vtbl: typeck::vtable_origin)
|
|
|
|
-> lval_maybe_callee {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("impl::trans_monomorphized_callee");
|
2012-08-02 18:01:38 -05:00
|
|
|
match vtbl {
|
2012-08-03 21:59:04 -05:00
|
|
|
typeck::vtable_static(impl_did, impl_substs, sub_origins) => {
|
2012-03-09 01:44:53 -06:00
|
|
|
let ccx = bcx.ccx();
|
2012-07-03 18:30:42 -05:00
|
|
|
let mname = ty::trait_methods(ccx.tcx, trait_id)[n_method].ident;
|
2012-03-08 05:15:02 -06:00
|
|
|
let mth_id = method_with_name(bcx.ccx(), impl_did, mname);
|
2012-04-23 02:25:14 -05:00
|
|
|
let n_m_tps = method_ty_param_count(ccx, mth_id, impl_did);
|
2012-03-09 01:44:53 -06:00
|
|
|
let node_substs = node_id_type_params(bcx, callee_id);
|
2012-06-28 15:52:13 -05:00
|
|
|
let ty_substs
|
|
|
|
= vec::append(impl_substs,
|
|
|
|
vec::tailn(node_substs,
|
|
|
|
node_substs.len() - n_m_tps));
|
2012-08-16 18:44:22 -05:00
|
|
|
let {bcx, val} = trans_self_arg(bcx, base, mentry);
|
2012-03-16 08:28:45 -05:00
|
|
|
let lval = lval_static_fn_inner(bcx, mth_id, callee_id, ty_substs,
|
|
|
|
some(sub_origins));
|
2012-08-16 18:44:22 -05:00
|
|
|
{env: self_env(val, node_id_type(bcx, base.id),
|
|
|
|
none, mentry.self_mode),
|
2012-03-16 08:28:45 -05:00
|
|
|
val: PointerCast(bcx, lval.val, T_ptr(type_of_fn_from_ty(
|
|
|
|
ccx, node_id_type(bcx, callee_id))))
|
|
|
|
with lval}
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
2012-08-10 17:13:24 -05:00
|
|
|
typeck::vtable_trait(trait_id, tps) => {
|
2012-05-14 17:48:58 -05:00
|
|
|
let {bcx, val} = trans_temp_expr(bcx, base);
|
|
|
|
let fty = node_id_type(bcx, callee_id);
|
2012-07-03 18:30:42 -05:00
|
|
|
trans_trait_callee(bcx, val, fty, n_method)
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
typeck::vtable_param(n_param, n_bound) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
fail ~"vtable_param left in monomorphized function's vtable substs";
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 18:30:42 -05:00
|
|
|
// Method callee where the vtable comes from a boxed trait
|
|
|
|
fn trans_trait_callee(bcx: block, val: ValueRef,
|
2012-05-14 17:48:58 -05:00
|
|
|
callee_ty: ty::t, n_method: uint)
|
2012-01-07 15:44:14 -06:00
|
|
|
-> lval_maybe_callee {
|
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-06-29 18:26:56 -05:00
|
|
|
let vtable = Load(bcx, PointerCast(bcx, GEPi(bcx, val, ~[0u, 0u]),
|
2012-05-14 17:48:58 -05:00
|
|
|
T_ptr(T_ptr(T_vtable()))));
|
2012-06-29 18:26:56 -05:00
|
|
|
let llbox = Load(bcx, GEPi(bcx, val, ~[0u, 1u]));
|
2012-06-25 22:35:32 -05:00
|
|
|
// FIXME[impl] I doubt this is alignment-safe (#2534)
|
2012-06-29 18:26:56 -05:00
|
|
|
let self = GEPi(bcx, llbox, ~[0u, abi::box_field_body]);
|
2012-08-16 18:44:22 -05:00
|
|
|
let env = self_env(self, ty::mk_opaque_box(bcx.tcx()), some(llbox),
|
|
|
|
// XXX: is this bogosity?
|
|
|
|
ast::by_ref);
|
2012-05-14 17:48:58 -05:00
|
|
|
let llfty = type_of::type_of_fn_from_ty(ccx, callee_ty);
|
|
|
|
let vtable = PointerCast(bcx, vtable,
|
|
|
|
T_ptr(T_array(T_ptr(llfty), n_method + 1u)));
|
2012-06-29 18:26:56 -05:00
|
|
|
let mptr = Load(bcx, GEPi(bcx, vtable, ~[0u, n_method]));
|
2012-07-16 22:17:57 -05:00
|
|
|
{bcx: bcx, val: mptr, kind: lv_owned, env: env}
|
2012-01-07 15:44:14 -06:00
|
|
|
}
|
|
|
|
|
2012-03-08 09:17:59 -06:00
|
|
|
fn find_vtable_in_fn_ctxt(ps: param_substs, n_param: uint, n_bound: uint)
|
|
|
|
-> typeck::vtable_origin {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut vtable_off = n_bound, i = 0u;
|
2012-03-08 09:17:59 -06:00
|
|
|
// Vtables are stored in a flat array, finding the right one is
|
2012-02-09 04:17:11 -06:00
|
|
|
// somewhat awkward
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(*ps.bounds) |bounds| {
|
2012-02-09 04:17:11 -06:00
|
|
|
if i >= n_param { break; }
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(*bounds) |bound| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match bound { ty::bound_trait(_) => vtable_off += 1u, _ => () }
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
2012-03-09 01:44:53 -06:00
|
|
|
i += 1u;
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
2012-03-08 09:17:59 -06:00
|
|
|
option::get(ps.vtables)[vtable_off]
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
|
|
|
|
2012-03-08 09:17:59 -06:00
|
|
|
fn resolve_vtables_in_fn_ctxt(fcx: fn_ctxt, vts: typeck::vtable_res)
|
|
|
|
-> typeck::vtable_res {
|
2012-06-30 18:19:07 -05:00
|
|
|
@vec::map(*vts, |d| resolve_vtable_in_fn_ctxt(fcx, d))
|
2012-03-08 09:10:25 -06:00
|
|
|
}
|
|
|
|
|
2012-08-02 17:52:25 -05:00
|
|
|
// Apply the typaram substitutions in the fn_ctxt to a vtable. This should
|
|
|
|
// eliminate any vtable_params.
|
2012-03-08 09:17:59 -06:00
|
|
|
fn resolve_vtable_in_fn_ctxt(fcx: fn_ctxt, vt: typeck::vtable_origin)
|
|
|
|
-> typeck::vtable_origin {
|
2012-08-06 14:34:08 -05:00
|
|
|
match vt {
|
2012-08-10 17:13:24 -05:00
|
|
|
typeck::vtable_static(trait_id, tys, sub) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
let tys = match fcx.param_substs {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(substs) => {
|
2012-06-30 18:19:07 -05:00
|
|
|
vec::map(tys, |t| ty::subst_tps(fcx.ccx.tcx, substs.tys, t))
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => tys
|
2012-03-08 09:10:25 -06:00
|
|
|
};
|
2012-08-10 17:13:24 -05:00
|
|
|
typeck::vtable_static(trait_id, tys,
|
|
|
|
resolve_vtables_in_fn_ctxt(fcx, sub))
|
2012-03-08 09:10:25 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
typeck::vtable_param(n_param, n_bound) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match check fcx.param_substs {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(substs) => {
|
2012-03-08 09:17:59 -06:00
|
|
|
find_vtable_in_fn_ctxt(substs, n_param, n_bound)
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
2012-03-08 09:10:25 -06:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => vt
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-12 10:31:22 -05:00
|
|
|
fn vtable_id(ccx: @crate_ctxt, origin: typeck::vtable_origin) -> mono_id {
|
2012-08-06 14:34:08 -05:00
|
|
|
match check origin {
|
2012-08-03 21:59:04 -05:00
|
|
|
typeck::vtable_static(impl_id, substs, sub_vtables) => {
|
2012-03-12 10:31:22 -05:00
|
|
|
make_mono_id(ccx, impl_id, substs,
|
|
|
|
if (*sub_vtables).len() == 0u { none }
|
|
|
|
else { some(sub_vtables) }, none)
|
2012-03-08 09:10:25 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
typeck::vtable_trait(trait_id, substs) => {
|
2012-07-03 18:30:42 -05:00
|
|
|
@{def: trait_id,
|
2012-06-30 18:19:07 -05:00
|
|
|
params: vec::map(substs, |t| mono_precise(t, none))}
|
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-03 21:59:04 -05:00
|
|
|
some(val) => val,
|
2012-08-06 14:34:08 -05:00
|
|
|
none => match check 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-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-07-14 00:57:48 -05:00
|
|
|
let vt_gvar = str::as_c_str(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-07-30 16:43:44 -05:00
|
|
|
let trt_id = expect(ccx.sess,
|
|
|
|
ty::ty_to_def_id(ty::impl_traits(tcx, impl_id)[0]),
|
|
|
|
|| ~"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-04-13 14:22:35 -05:00
|
|
|
let fty = ty::subst_tps(tcx, substs, 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-03-16 08:28:45 -05:00
|
|
|
C_null(T_ptr(T_nil()))
|
2012-03-08 09:10:25 -06:00
|
|
|
} else {
|
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 {
|
|
|
|
m_id = maybe_instantiate_inline(ccx, m_id);
|
|
|
|
}
|
2012-03-21 09:42:20 -05:00
|
|
|
monomorphic_fn(ccx, m_id, substs, some(vtables), 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-02-17 06:17:40 -06:00
|
|
|
fn trans_cast(bcx: block, val: @ast::expr, id: ast::node_id, dest: dest)
|
|
|
|
-> block {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("impl::trans_cast");
|
2012-08-01 19:30:05 -05:00
|
|
|
if dest == ignore { return trans_expr(bcx, val, ignore); }
|
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-07-17 12:48:19 -05:00
|
|
|
let {bcx: bcx, box: llbox, body: body} = malloc_boxed(bcx, v_ty);
|
2012-06-27 16:10:24 -05:00
|
|
|
add_clean_free(bcx, llbox, heap_shared);
|
2012-03-23 08:45:47 -05:00
|
|
|
let bcx = trans_expr_save_in(bcx, val, body);
|
2012-06-27 16:10:24 -05:00
|
|
|
revoke_clean(bcx, llbox);
|
2012-02-10 04:32:03 -06:00
|
|
|
let result = get_dest_addr(dest);
|
2012-06-29 18:26:56 -05:00
|
|
|
Store(bcx, llbox, PointerCast(bcx, GEPi(bcx, result, ~[0u, 1u]),
|
2012-06-27 16:10:24 -05:00
|
|
|
T_ptr(val_ty(llbox))));
|
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-06-29 18:26:56 -05:00
|
|
|
Store(bcx, vtable, PointerCast(bcx, GEPi(bcx, result, ~[0u, 0u]),
|
2012-03-08 09:10:25 -06:00
|
|
|
T_ptr(val_ty(vtable))));
|
2012-02-10 04:32:03 -06:00
|
|
|
bcx
|
2012-01-07 15:44:14 -06:00
|
|
|
}
|