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-03-08 05:15:02 -06:00
|
|
|
import syntax::ast;
|
|
|
|
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-02-03 02:53:37 -06:00
|
|
|
import ast_map::{path, path_mod, path_name};
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2012-01-02 05:21:44 -06:00
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn trans_impl(ccx: @crate_ctxt, path: path, name: ast::ident,
|
2012-03-07 05:54:00 -06:00
|
|
|
methods: [@ast::method], tps: [ast::ty_param]) {
|
2012-03-08 06:30:22 -06:00
|
|
|
if tps.len() > 0u { ret; }
|
2012-02-03 02:53:37 -06:00
|
|
|
let sub_path = path + [path_name(name)];
|
2012-01-02 05:21:44 -06:00
|
|
|
for m in methods {
|
2012-03-08 06:30:22 -06:00
|
|
|
if m.tps.len() == 0u {
|
|
|
|
let llfn = get_item_val(ccx, m.id);
|
|
|
|
trans_fn(ccx, sub_path + [path_name(m.ident)], m.decl, m.body,
|
|
|
|
llfn, impl_self(ty::node_id_to_type(ccx.tcx, m.self_id)),
|
|
|
|
none, m.id, none);
|
|
|
|
}
|
2012-01-02 05:21:44 -06:00
|
|
|
}
|
|
|
|
}
|
2012-01-02 06:26:51 -06:00
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn trans_self_arg(bcx: block, base: @ast::expr) -> result {
|
2012-02-02 05:37:17 -06:00
|
|
|
let basety = expr_ty(bcx, base);
|
2012-02-02 18:50:17 -06:00
|
|
|
let m_by_ref = ast::expl(ast::by_ref);
|
2012-02-24 19:45:16 -06:00
|
|
|
let temp_cleanups = [];
|
|
|
|
let result = trans_arg_expr(bcx, {mode: m_by_ref, ty: basety},
|
|
|
|
T_ptr(type_of_or_i8(bcx.ccx(), basety)), base,
|
|
|
|
temp_cleanups);
|
|
|
|
|
|
|
|
// by-ref self argument should not require cleanup in the case of
|
|
|
|
// other arguments failing:
|
|
|
|
assert temp_cleanups == [];
|
|
|
|
|
|
|
|
ret 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-01-26 05:26:14 -06:00
|
|
|
self: @ast::expr, origin: typeck::method_origin)
|
|
|
|
-> lval_maybe_callee {
|
|
|
|
alt origin {
|
|
|
|
typeck::method_static(did) {
|
2012-02-09 04:17:11 -06:00
|
|
|
trans_static_callee(bcx, callee_id, self, did, none)
|
2012-01-26 05:26:14 -06:00
|
|
|
}
|
|
|
|
typeck::method_param(iid, off, p, b) {
|
2012-03-08 09:10:25 -06:00
|
|
|
alt check bcx.fcx.param_substs {
|
2012-02-09 04:17:11 -06:00
|
|
|
some(substs) {
|
|
|
|
trans_monomorphized_callee(bcx, callee_id, self,
|
|
|
|
iid, off, p, b, substs)
|
|
|
|
}
|
|
|
|
}
|
2012-01-26 05:26:14 -06:00
|
|
|
}
|
2012-03-08 09:10:25 -06:00
|
|
|
typeck::method_iface(_, off) {
|
|
|
|
trans_iface_callee(bcx, self, callee_id, off)
|
2012-01-26 05:26:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-07 15:44:14 -06:00
|
|
|
// Method callee where the method is statically known
|
2012-02-17 06:17:40 -06:00
|
|
|
fn trans_static_callee(bcx: block, callee_id: ast::node_id,
|
2012-02-09 04:17:11 -06:00
|
|
|
base: @ast::expr, did: ast::def_id,
|
|
|
|
substs: option<([ty::t], typeck::dict_res)>)
|
2012-01-26 05:26:14 -06:00
|
|
|
-> lval_maybe_callee {
|
2012-01-02 09:50:51 -06:00
|
|
|
let {bcx, val} = trans_self_arg(bcx, base);
|
2012-02-14 03:47:39 -06:00
|
|
|
{env: self_env(val, node_id_type(bcx, base.id))
|
|
|
|
with lval_static_fn(bcx, did, callee_id, substs)}
|
2012-01-02 09:50:51 -06:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn wrapper_fn_ty(ccx: @crate_ctxt, dict_ty: TypeRef, fty: ty::t,
|
2012-01-30 04:52:34 -06:00
|
|
|
tps: @[ty::param_bounds]) -> {ty: ty::t, llty: TypeRef} {
|
|
|
|
let bare_fn_ty = type_of_fn_from_ty(ccx, fty, *tps);
|
2012-01-12 09:57:30 -06:00
|
|
|
let {inputs, output} = llfn_arg_tys(bare_fn_ty);
|
|
|
|
{ty: fty, llty: T_fn([dict_ty] + inputs, output)}
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn trans_vtable_callee(bcx: block, env: callee_env, dict: ValueRef,
|
2012-03-08 09:10:25 -06:00
|
|
|
callee_id: ast::node_id, n_method: uint)
|
|
|
|
-> lval_maybe_callee {
|
2012-02-21 07:20:18 -06:00
|
|
|
let bcx = bcx, ccx = bcx.ccx(), tcx = ccx.tcx;
|
2012-03-08 09:10:25 -06:00
|
|
|
let fty = node_id_type(bcx, callee_id);
|
|
|
|
let llfty = type_of::type_of_fn_from_ty(ccx, fty, []);
|
|
|
|
let vtable = PointerCast(bcx, dict,
|
2012-01-12 09:57:30 -06:00
|
|
|
T_ptr(T_array(T_ptr(llfty), n_method + 1u)));
|
2012-01-02 09:50:51 -06:00
|
|
|
let mptr = Load(bcx, GEPi(bcx, vtable, [0, n_method as int]));
|
|
|
|
{bcx: bcx, val: mptr, kind: owned,
|
2012-02-14 03:47:39 -06:00
|
|
|
env: env,
|
2012-03-08 09:10:25 -06:00
|
|
|
generic: generic_none}
|
2012-01-02 09:50:51 -06: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 {
|
|
|
|
alt check ccx.tcx.items.get(impl_id.node) {
|
|
|
|
ast_map::node_item(@{node: ast::item_impl(_, _, _, ms), _}, _) {
|
|
|
|
local_def(option::get(vec::find(ms, {|m| m.ident == name})).id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
csearch::get_impl_method(ccx.sess.cstore, impl_id, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn trans_monomorphized_callee(bcx: block, callee_id: ast::node_id,
|
2012-02-09 04:17:11 -06:00
|
|
|
base: @ast::expr, iface_id: ast::def_id,
|
|
|
|
n_method: uint, n_param: uint, n_bound: uint,
|
|
|
|
substs: param_substs) -> lval_maybe_callee {
|
|
|
|
alt find_dict_in_fn_ctxt(substs, n_param, n_bound) {
|
|
|
|
typeck::dict_static(impl_did, tys, sub_origins) {
|
2012-02-21 07:20:18 -06:00
|
|
|
let tcx = bcx.tcx();
|
2012-02-09 04:17:11 -06:00
|
|
|
let mname = ty::iface_methods(tcx, iface_id)[n_method].ident;
|
2012-03-08 05:15:02 -06:00
|
|
|
let mth_id = method_with_name(bcx.ccx(), impl_did, mname);
|
|
|
|
ret trans_static_callee(bcx, callee_id, base, mth_id,
|
2012-02-09 04:17:11 -06:00
|
|
|
some((tys, sub_origins)));
|
|
|
|
}
|
2012-03-08 09:10:25 -06:00
|
|
|
typeck::dict_iface(iid, tps) {
|
|
|
|
ret trans_iface_callee(bcx, base, callee_id, n_method);
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
|
|
|
typeck::dict_param(n_param, n_bound) {
|
|
|
|
fail "dict_param left in monomorphized function's dict substs";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-07 15:44:14 -06:00
|
|
|
// Method callee where the dict comes from a boxed iface
|
2012-03-08 09:10:25 -06:00
|
|
|
fn trans_iface_callee(bcx: block, base: @ast::expr,
|
|
|
|
callee_id: ast::node_id, n_method: uint)
|
2012-01-07 15:44:14 -06:00
|
|
|
-> lval_maybe_callee {
|
|
|
|
let {bcx, val} = trans_temp_expr(bcx, base);
|
2012-02-10 04:32:03 -06:00
|
|
|
let dict = Load(bcx, PointerCast(bcx, GEPi(bcx, val, [0, 0]),
|
2012-01-07 15:44:14 -06:00
|
|
|
T_ptr(T_ptr(T_dict()))));
|
2012-02-10 04:32:03 -06:00
|
|
|
let box = Load(bcx, GEPi(bcx, val, [0, 1]));
|
2012-01-07 15:44:14 -06:00
|
|
|
// FIXME[impl] I doubt this is alignment-safe
|
2012-02-14 03:47:39 -06:00
|
|
|
let self = GEPi(bcx, box, [0, abi::box_field_body]);
|
2012-03-08 09:10:25 -06:00
|
|
|
trans_vtable_callee(bcx, self_env(self, expr_ty(bcx, base)), dict,
|
|
|
|
callee_id, n_method)
|
2012-01-07 15:44:14 -06:00
|
|
|
}
|
|
|
|
|
2012-01-02 06:26:51 -06:00
|
|
|
fn llfn_arg_tys(ft: TypeRef) -> {inputs: [TypeRef], output: TypeRef} {
|
2012-01-06 07:22:31 -06:00
|
|
|
let out_ty = llvm::LLVMGetReturnType(ft);
|
|
|
|
let n_args = llvm::LLVMCountParamTypes(ft);
|
2012-03-12 17:52:30 -05:00
|
|
|
let args = vec::from_elem(n_args as uint, 0 as TypeRef);
|
2012-02-28 22:43:39 -06:00
|
|
|
unsafe { llvm::LLVMGetParamTypes(ft, vec::unsafe::to_ptr(args)); }
|
2012-01-02 06:26:51 -06:00
|
|
|
{inputs: args, output: out_ty}
|
|
|
|
}
|
|
|
|
|
2012-02-09 04:17:11 -06:00
|
|
|
fn find_dict_in_fn_ctxt(ps: param_substs, n_param: uint, n_bound: uint)
|
|
|
|
-> typeck::dict_origin {
|
|
|
|
let dict_off = n_bound, i = 0u;
|
|
|
|
// Dicts are stored in a flat array, finding the right one is
|
|
|
|
// somewhat awkward
|
|
|
|
for bounds in *ps.bounds {
|
|
|
|
i += 1u;
|
|
|
|
if i >= n_param { break; }
|
|
|
|
for bound in *bounds {
|
|
|
|
alt bound { ty::bound_iface(_) { dict_off += 1u; } _ {} }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
option::get(ps.dicts)[dict_off]
|
|
|
|
}
|
|
|
|
|
2012-03-08 09:10:25 -06:00
|
|
|
fn resolve_vtables_in_fn_ctxt(fcx: fn_ctxt, vts: typeck::dict_res)
|
|
|
|
-> typeck::dict_res {
|
|
|
|
@vec::map(*vts, {|d| resolve_vtable_in_fn_ctxt(fcx, d)})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_vtable_in_fn_ctxt(fcx: fn_ctxt, vt: typeck::dict_origin)
|
|
|
|
-> typeck::dict_origin {
|
|
|
|
alt vt {
|
|
|
|
typeck::dict_static(iid, tys, sub) {
|
|
|
|
let tys = alt fcx.param_substs {
|
|
|
|
some(substs) {
|
|
|
|
vec::map(tys, {|t|
|
|
|
|
ty::substitute_type_params(fcx.ccx.tcx, substs.tys, t)
|
|
|
|
})
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
2012-03-08 09:10:25 -06:00
|
|
|
_ { tys }
|
|
|
|
};
|
|
|
|
typeck::dict_static(iid, tys, resolve_vtables_in_fn_ctxt(fcx, sub))
|
|
|
|
}
|
|
|
|
typeck::dict_param(n_param, n_bound) {
|
|
|
|
alt check fcx.param_substs {
|
|
|
|
some(substs) {
|
|
|
|
find_dict_in_fn_ctxt(substs, n_param, n_bound)
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
2012-03-08 09:10:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ { vt }
|
2012-02-09 04:17:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-08 09:10:25 -06:00
|
|
|
fn vtable_id(origin: typeck::dict_origin) -> mono_id {
|
|
|
|
alt check origin {
|
|
|
|
typeck::dict_static(impl_id, substs, sub_dicts) {
|
|
|
|
@{def: impl_id, substs: substs,
|
|
|
|
dicts: if (*sub_dicts).len() == 0u { no_dicts }
|
|
|
|
else { some_dicts(vec::map(*sub_dicts, vtable_id)) } }
|
|
|
|
}
|
|
|
|
typeck::dict_iface(iface_id, substs) {
|
|
|
|
@{def: iface_id, substs: substs, dicts: no_dicts}
|
|
|
|
}
|
|
|
|
}
|
2012-01-12 09:57:30 -06:00
|
|
|
}
|
|
|
|
|
2012-03-08 09:10:25 -06:00
|
|
|
fn get_vtable(ccx: @crate_ctxt, origin: typeck::dict_origin)
|
2012-01-12 09:57:30 -06:00
|
|
|
-> ValueRef {
|
2012-03-08 09:10:25 -06:00
|
|
|
let hash_id = vtable_id(origin);
|
|
|
|
alt ccx.vtables.find(hash_id) {
|
|
|
|
some(val) { val }
|
|
|
|
none {
|
|
|
|
alt check origin {
|
|
|
|
typeck::dict_static(id, substs, sub_dicts) {
|
|
|
|
make_impl_vtable(ccx, id, substs, sub_dicts)
|
|
|
|
}
|
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-03-08 09:10:25 -06:00
|
|
|
fn make_vtable(ccx: @crate_ctxt, ptrs: [ValueRef]) -> ValueRef {
|
|
|
|
let tbl = C_struct(ptrs);
|
|
|
|
let vt_gvar = str::as_c_str(ccx.names("vtable"), {|buf|
|
|
|
|
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-03-08 09:10:25 -06:00
|
|
|
fn make_impl_vtable(ccx: @crate_ctxt, impl_id: ast::def_id, substs: [ty::t],
|
|
|
|
dicts: typeck::dict_res) -> ValueRef {
|
|
|
|
let tcx = ccx.tcx;
|
|
|
|
let ifce_id = ty::ty_to_def_id(option::get(ty::impl_iface(tcx, impl_id)));
|
|
|
|
make_vtable(ccx, vec::map(*ty::iface_methods(tcx, ifce_id), {|im|
|
|
|
|
let fty = ty::substitute_type_params(tcx, substs,
|
|
|
|
ty::mk_fn(tcx, im.fty));
|
|
|
|
if (*im.tps).len() > 0u || ty::type_has_vars(fty) {
|
|
|
|
C_null(type_of_fn_from_ty(ccx, fty, []))
|
|
|
|
} else {
|
|
|
|
let m_id = method_with_name(ccx, impl_id, im.ident);
|
|
|
|
option::get(monomorphic_fn(ccx, m_id, substs, some(dicts))).llfn
|
2012-01-12 09:57:30 -06:00
|
|
|
}
|
2012-03-08 09:10:25 -06:00
|
|
|
}))
|
2012-01-12 09:57:30 -06:00
|
|
|
}
|
|
|
|
|
2012-03-08 09:10:25 -06:00
|
|
|
/*
|
|
|
|
fn make_iface_vtable(ccx: @crate_ctxt, pt: path, it: @ast::item) {
|
2012-02-03 02:53:37 -06:00
|
|
|
let new_pt = pt + [path_name(it.ident), path_name(int::str(it.id))];
|
2012-03-08 05:15:02 -06:00
|
|
|
let i_did = local_def(it.id), i = 0u;
|
2012-01-12 09:57:30 -06:00
|
|
|
let ptrs = vec::map(*ty::iface_methods(ccx.tcx, i_did), {|m|
|
2012-02-03 02:53:37 -06:00
|
|
|
let w = trans_iface_wrapper(ccx, new_pt + [path_name(m.ident)], m, i);
|
2012-01-02 06:26:51 -06:00
|
|
|
i += 1u;
|
2012-01-12 09:57:30 -06:00
|
|
|
w
|
|
|
|
});
|
2012-02-03 02:53:37 -06:00
|
|
|
let s = link::mangle_internal_name_by_path(
|
|
|
|
ccx, new_pt + [path_name("!vtable")]);
|
2012-01-12 09:57:30 -06:00
|
|
|
trans_vtable(ccx, it.id, s, ptrs);
|
2012-01-02 06:26:51 -06:00
|
|
|
}
|
|
|
|
|
2012-01-06 07:22:31 -06:00
|
|
|
fn dict_is_static(tcx: ty::ctxt, origin: typeck::dict_origin) -> bool {
|
|
|
|
alt origin {
|
|
|
|
typeck::dict_static(_, ts, origs) {
|
2012-02-03 08:15:28 -06:00
|
|
|
vec::all(ts, {|t| !ty::type_has_params(t)}) &&
|
2012-01-06 07:22:31 -06:00
|
|
|
vec::all(*origs, {|o| dict_is_static(tcx, o)})
|
|
|
|
}
|
2012-01-12 09:57:30 -06:00
|
|
|
typeck::dict_iface(_) { true }
|
|
|
|
_ { false }
|
2012-01-06 07:22:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn get_dict(bcx: block, origin: typeck::dict_origin) -> result {
|
2012-02-21 07:20:18 -06:00
|
|
|
let ccx = bcx.ccx();
|
2012-01-06 07:22:31 -06:00
|
|
|
alt origin {
|
|
|
|
typeck::dict_static(impl_did, tys, sub_origins) {
|
2012-01-06 10:29:06 -06:00
|
|
|
if dict_is_static(ccx.tcx, origin) {
|
2012-01-06 07:22:31 -06:00
|
|
|
ret rslt(bcx, get_static_dict(bcx, origin));
|
|
|
|
}
|
|
|
|
let {bcx, ptrs} = get_dict_ptrs(bcx, origin);
|
2012-02-14 02:10:47 -06:00
|
|
|
let pty = T_ptr(T_i8()), dict_ty = T_array(pty, ptrs.len());
|
2012-01-06 07:22:31 -06:00
|
|
|
let dict = alloca(bcx, dict_ty), i = 0;
|
|
|
|
for ptr in ptrs {
|
|
|
|
Store(bcx, PointerCast(bcx, ptr, pty), GEPi(bcx, dict, [0, i]));
|
|
|
|
i += 1;
|
|
|
|
}
|
2012-01-06 10:29:06 -06:00
|
|
|
dict = Call(bcx, ccx.upcalls.intern_dict,
|
2012-02-14 02:10:47 -06:00
|
|
|
[C_uint(ccx, ptrs.len()),
|
2012-01-06 10:29:06 -06:00
|
|
|
PointerCast(bcx, dict, T_ptr(T_dict()))]);
|
|
|
|
rslt(bcx, dict)
|
2012-01-06 07:22:31 -06:00
|
|
|
}
|
|
|
|
typeck::dict_param(n_param, n_bound) {
|
|
|
|
rslt(bcx, option::get(bcx.fcx.lltyparams[n_param].dicts)[n_bound])
|
|
|
|
}
|
2012-01-12 09:57:30 -06:00
|
|
|
typeck::dict_iface(did) {
|
|
|
|
ret rslt(bcx, get_static_dict(bcx, origin));
|
|
|
|
}
|
2012-01-06 07:22:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dict_id(tcx: ty::ctxt, origin: typeck::dict_origin) -> dict_id {
|
|
|
|
alt origin {
|
|
|
|
typeck::dict_static(did, ts, origs) {
|
|
|
|
let d_params = [], orig = 0u;
|
2012-02-14 02:10:47 -06:00
|
|
|
if ts.len() == 0u { ret @{def: did, params: d_params}; }
|
2012-01-06 07:22:31 -06:00
|
|
|
let impl_params = ty::lookup_item_type(tcx, did).bounds;
|
|
|
|
vec::iter2(ts, *impl_params) {|t, bounds|
|
|
|
|
d_params += [dict_param_ty(t)];
|
|
|
|
for bound in *bounds {
|
|
|
|
alt bound {
|
|
|
|
ty::bound_iface(_) {
|
|
|
|
d_params += [dict_param_dict(dict_id(tcx, origs[orig]))];
|
|
|
|
orig += 1u;
|
|
|
|
}
|
2012-01-30 04:52:34 -06:00
|
|
|
_ {}
|
2012-01-06 07:22:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-12 09:57:30 -06:00
|
|
|
@{def: did, params: d_params}
|
|
|
|
}
|
|
|
|
typeck::dict_iface(did) {
|
|
|
|
@{def: did, params: []}
|
2012-01-06 07:22:31 -06:00
|
|
|
}
|
2012-01-30 23:00:57 -06:00
|
|
|
_ {
|
2012-03-05 18:27:27 -06:00
|
|
|
tcx.sess.bug("unexpected dict_param in dict_id");
|
2012-01-30 23:00:57 -06:00
|
|
|
}
|
2012-01-06 07:22:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn get_static_dict(bcx: block, origin: typeck::dict_origin)
|
2012-01-06 07:22:31 -06:00
|
|
|
-> ValueRef {
|
2012-02-21 07:20:18 -06:00
|
|
|
let ccx = bcx.ccx();
|
2012-01-06 07:22:31 -06:00
|
|
|
let id = dict_id(ccx.tcx, origin);
|
|
|
|
alt ccx.dicts.find(id) {
|
|
|
|
some(d) { ret d; }
|
2012-01-19 00:37:22 -06:00
|
|
|
none {}
|
2012-01-06 07:22:31 -06:00
|
|
|
}
|
|
|
|
let ptrs = C_struct(get_dict_ptrs(bcx, origin).ptrs);
|
2012-01-13 02:32:05 -06:00
|
|
|
let name = ccx.names("dict");
|
2012-03-14 17:10:34 -05:00
|
|
|
let gvar = str::as_c_str(name, {|buf|
|
2012-01-06 07:22:31 -06:00
|
|
|
llvm::LLVMAddGlobal(ccx.llmod, val_ty(ptrs), buf)
|
|
|
|
});
|
|
|
|
llvm::LLVMSetGlobalConstant(gvar, lib::llvm::True);
|
|
|
|
llvm::LLVMSetInitializer(gvar, ptrs);
|
2012-02-01 04:04:56 -06:00
|
|
|
lib::llvm::SetLinkage(gvar, lib::llvm::InternalLinkage);
|
2012-01-06 07:22:31 -06:00
|
|
|
let cast = llvm::LLVMConstPointerCast(gvar, T_ptr(T_dict()));
|
|
|
|
ccx.dicts.insert(id, cast);
|
|
|
|
cast
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn get_dict_ptrs(bcx: block, origin: typeck::dict_origin)
|
|
|
|
-> {bcx: block, ptrs: [ValueRef]} {
|
2012-02-21 07:20:18 -06:00
|
|
|
let ccx = bcx.ccx();
|
2012-03-14 19:31:16 -05:00
|
|
|
fn get_vtable(ccx: @crate_ctxt, did: ast::def_id) -> ValueRef {
|
2012-01-12 09:57:30 -06:00
|
|
|
if did.crate == ast::local_crate {
|
2012-03-07 05:21:08 -06:00
|
|
|
get_item_val(ccx, did.node)
|
2012-01-05 06:57:27 -06:00
|
|
|
} else {
|
2012-01-12 10:59:49 -06:00
|
|
|
let name = csearch::get_symbol(ccx.sess.cstore, did);
|
2012-01-05 06:57:27 -06:00
|
|
|
get_extern_const(ccx.externs, ccx.llmod, name, T_ptr(T_i8()))
|
2012-01-12 09:57:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
alt origin {
|
|
|
|
typeck::dict_static(impl_did, tys, sub_origins) {
|
2012-01-02 09:50:51 -06:00
|
|
|
let impl_params = ty::lookup_item_type(ccx.tcx, impl_did).bounds;
|
2012-01-12 09:57:30 -06:00
|
|
|
let ptrs = [get_vtable(ccx, impl_did)];
|
2012-02-17 08:45:38 -06:00
|
|
|
let origin = 0u, bcx = bcx;
|
2012-01-06 07:22:31 -06:00
|
|
|
vec::iter2(*impl_params, tys) {|param, ty|
|
2012-02-17 08:45:38 -06:00
|
|
|
let rslt = get_tydesc_simple(bcx, ty, true);
|
2012-01-02 09:50:51 -06:00
|
|
|
ptrs += [rslt.val];
|
|
|
|
bcx = rslt.bcx;
|
|
|
|
for bound in *param {
|
|
|
|
alt bound {
|
|
|
|
ty::bound_iface(_) {
|
|
|
|
let res = get_dict(bcx, sub_origins[origin]);
|
|
|
|
ptrs += [res.val];
|
|
|
|
bcx = res.bcx;
|
|
|
|
origin += 1u;
|
|
|
|
}
|
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-06 07:22:31 -06:00
|
|
|
{bcx: bcx, ptrs: ptrs}
|
2012-01-03 09:37:41 -06:00
|
|
|
}
|
2012-01-12 09:57:30 -06:00
|
|
|
typeck::dict_iface(did) {
|
|
|
|
{bcx: bcx, ptrs: [get_vtable(ccx, did)]}
|
|
|
|
}
|
2012-01-30 23:00:57 -06:00
|
|
|
_ {
|
2012-03-05 18:27:27 -06:00
|
|
|
bcx.tcx().sess.bug("unexpected dict_param in get_dict_ptrs");
|
2012-01-30 23:00:57 -06:00
|
|
|
}
|
2012-01-02 09:50:51 -06:00
|
|
|
}
|
2012-01-06 07:22:31 -06:00
|
|
|
}
|
2012-03-08 09:10:25 -06:00
|
|
|
}*/
|
2012-01-07 15:44:14 -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-02-10 04:32:03 -06:00
|
|
|
if dest == ignore { ret 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);
|
|
|
|
let {bcx, box, body} = trans_malloc_boxed(bcx, v_ty);
|
|
|
|
add_clean_free(bcx, box, false);
|
|
|
|
bcx = trans_expr_save_in(bcx, val, body);
|
|
|
|
revoke_clean(bcx, box);
|
|
|
|
let result = get_dest_addr(dest);
|
|
|
|
Store(bcx, box, PointerCast(bcx, GEPi(bcx, result, [0, 1]),
|
|
|
|
T_ptr(val_ty(box))));
|
2012-03-08 09:10:25 -06:00
|
|
|
let orig = ccx.maps.dict_map.get(id)[0];
|
|
|
|
let orig = resolve_vtable_in_fn_ctxt(bcx.fcx, orig);
|
|
|
|
let vtable = get_vtable(bcx.ccx(), orig);
|
|
|
|
Store(bcx, vtable, PointerCast(bcx, GEPi(bcx, result, [0, 0]),
|
|
|
|
T_ptr(val_ty(vtable))));
|
2012-02-10 04:32:03 -06:00
|
|
|
bcx
|
2012-01-07 15:44:14 -06:00
|
|
|
}
|