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.
|
|
|
|
|
|
2016-05-27 13:27:56 -04:00
|
|
|
use attributes;
|
2015-06-18 20:07:36 +02:00
|
|
|
use llvm::{ValueRef, get_params};
|
2016-09-19 12:47:47 +03:00
|
|
|
use rustc::traits;
|
2016-12-18 22:24:42 -07:00
|
|
|
use callee::{Callee, CalleeData};
|
2016-03-22 19:23:36 +02:00
|
|
|
use common::*;
|
|
|
|
|
use consts;
|
|
|
|
|
use declare;
|
|
|
|
|
use glue;
|
|
|
|
|
use machine;
|
2016-09-19 12:47:47 +03:00
|
|
|
use monomorphize::Instance;
|
2016-03-22 19:23:36 +02:00
|
|
|
use type_::Type;
|
|
|
|
|
use type_of::*;
|
|
|
|
|
use value::Value;
|
2016-09-19 12:47:47 +03:00
|
|
|
use rustc::ty;
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2014-08-06 11:59:40 +02:00
|
|
|
// drop_glue pointer, size, align.
|
2015-03-25 17:06:52 -07:00
|
|
|
const VTABLE_OFFSET: usize = 3;
|
2014-08-06 11:59:40 +02:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
/// Extracts a method from a trait object's vtable, at the specified index.
|
2016-12-17 19:54:32 -07:00
|
|
|
pub fn get_virtual_method<'a, 'tcx>(bcx: &BlockAndBuilder<'a, 'tcx>,
|
|
|
|
|
llvtable: ValueRef,
|
|
|
|
|
vtable_index: usize)
|
|
|
|
|
-> ValueRef {
|
2013-08-11 13:42:26 -04:00
|
|
|
// Load the data pointer from the object.
|
2016-03-06 13:23:20 +02:00
|
|
|
debug!("get_virtual_method(vtable_index={}, llvtable={:?})",
|
|
|
|
|
vtable_index, Value(llvtable));
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-12-11 08:59:20 -07:00
|
|
|
bcx.load(bcx.gepi(llvtable, &[vtable_index + VTABLE_OFFSET]))
|
2012-01-07 22:44:14 +01:00
|
|
|
}
|
|
|
|
|
|
2014-12-23 05:26:34 -05:00
|
|
|
/// Generate a shim function that allows an object type like `SomeTrait` to
|
|
|
|
|
/// implement the type `SomeTrait`. Imagine a trait definition:
|
|
|
|
|
///
|
2015-12-02 09:06:28 +00:00
|
|
|
/// trait SomeTrait { fn get(&self) -> i32; ... }
|
2014-12-23 05:26:34 -05:00
|
|
|
///
|
|
|
|
|
/// And a generic bit of code:
|
|
|
|
|
///
|
|
|
|
|
/// fn foo<T:SomeTrait>(t: &T) {
|
|
|
|
|
/// let x = SomeTrait::get;
|
|
|
|
|
/// x(t)
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// What is the value of `x` when `foo` is invoked with `T=SomeTrait`?
|
2015-10-07 23:11:25 +01:00
|
|
|
/// The answer is that it is a shim function generated by this routine:
|
2014-12-23 05:26:34 -05:00
|
|
|
///
|
2015-12-02 09:06:28 +00:00
|
|
|
/// fn shim(t: &SomeTrait) -> i32 {
|
2014-12-23 05:26:34 -05:00
|
|
|
/// // ... call t.get() virtually ...
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// In fact, all virtual calls can be thought of as normal trait calls
|
|
|
|
|
/// that go through this shim function.
|
2016-03-06 17:32:47 +02:00
|
|
|
pub fn trans_object_shim<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>,
|
2016-09-19 12:47:47 +03:00
|
|
|
callee: Callee<'tcx>)
|
2016-03-06 16:30:21 +02:00
|
|
|
-> ValueRef {
|
2016-09-19 12:47:47 +03:00
|
|
|
debug!("trans_object_shim({:?})", callee);
|
|
|
|
|
|
2016-12-19 19:16:36 -07:00
|
|
|
let function_name = match callee.ty.sty {
|
|
|
|
|
ty::TyFnDef(def_id, substs, _) => {
|
2016-09-19 12:47:47 +03:00
|
|
|
let instance = Instance::new(def_id, substs);
|
2016-12-19 19:16:36 -07:00
|
|
|
instance.symbol_name(ccx.shared())
|
2016-09-19 12:47:47 +03:00
|
|
|
}
|
|
|
|
|
_ => bug!()
|
|
|
|
|
};
|
2014-12-23 05:26:34 -05:00
|
|
|
|
2016-09-19 12:47:47 +03:00
|
|
|
let llfn = declare::define_internal_fn(ccx, &function_name, callee.ty);
|
2016-05-27 13:27:56 -04:00
|
|
|
attributes::set_frame_pointer_elimination(ccx, llfn);
|
2014-12-23 05:26:34 -05:00
|
|
|
|
2016-12-19 19:16:36 -07:00
|
|
|
let fcx = FunctionContext::new(ccx, llfn);
|
2016-12-17 12:27:48 -07:00
|
|
|
let bcx = fcx.get_entry_block();
|
2014-12-23 05:26:34 -05:00
|
|
|
|
2016-12-18 22:24:42 -07:00
|
|
|
let mut llargs = get_params(fcx.llfn);
|
2016-12-18 09:07:35 -07:00
|
|
|
let fn_ret = callee.ty.fn_ret();
|
|
|
|
|
let fn_ty = callee.direct_fn_type(ccx, &[]);
|
|
|
|
|
|
2016-12-18 22:24:42 -07:00
|
|
|
let fn_ptr = match callee.data {
|
|
|
|
|
CalleeData::Virtual(idx) => {
|
|
|
|
|
let fn_ptr = get_virtual_method(&bcx,
|
|
|
|
|
llargs.remove(fn_ty.ret.is_indirect() as usize + 1), idx);
|
2016-12-19 16:25:00 -07:00
|
|
|
let llty = fn_ty.llvm_type(bcx.ccx).ptr_to();
|
2016-12-18 22:24:42 -07:00
|
|
|
bcx.pointercast(fn_ptr, llty)
|
|
|
|
|
},
|
|
|
|
|
_ => bug!("trans_object_shim called with non-virtual callee"),
|
|
|
|
|
};
|
|
|
|
|
let llret = bcx.call(fn_ptr, &llargs, None);
|
2016-12-18 09:07:35 -07:00
|
|
|
fn_ty.apply_attrs_callsite(llret);
|
|
|
|
|
|
|
|
|
|
if fn_ret.0.is_never() {
|
|
|
|
|
bcx.unreachable();
|
|
|
|
|
} else {
|
2016-12-19 19:16:36 -07:00
|
|
|
if fn_ty.ret.is_indirect() || fn_ty.ret.is_ignore() {
|
2016-12-19 17:59:26 -07:00
|
|
|
bcx.ret_void();
|
|
|
|
|
} else {
|
|
|
|
|
bcx.ret(llret);
|
|
|
|
|
}
|
2016-12-18 09:07:35 -07:00
|
|
|
}
|
2014-12-23 05:26:34 -05:00
|
|
|
|
2016-03-06 16:30:21 +02:00
|
|
|
llfn
|
2014-12-23 05:26:34 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-08 12:58:05 +02:00
|
|
|
/// Creates a dynamic vtable for the given type and vtable origin.
|
2013-05-15 17:38:52 -07:00
|
|
|
/// This is used only for objects.
|
2014-09-12 11:42:58 -04:00
|
|
|
///
|
2016-09-08 12:58:05 +02:00
|
|
|
/// The vtables are cached instead of created on every call.
|
|
|
|
|
///
|
2014-09-12 11:42:58 -04:00
|
|
|
/// The `trait_ref` encodes the erased self type. Hence if we are
|
|
|
|
|
/// making an object `Foo<Trait>` from a value of type `Foo<T>`, then
|
2015-03-14 02:36:41 +02:00
|
|
|
/// `trait_ref` would map `T:Trait`.
|
2015-01-29 14:03:34 +02:00
|
|
|
pub fn get_vtable<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
2016-11-16 09:21:49 -07:00
|
|
|
ty: ty::Ty<'tcx>,
|
|
|
|
|
trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>)
|
2015-01-29 14:03:34 +02:00
|
|
|
-> ValueRef
|
2014-05-31 18:53:13 -04:00
|
|
|
{
|
2015-01-29 14:03:34 +02:00
|
|
|
let tcx = ccx.tcx();
|
2013-08-13 13:22:58 -07:00
|
|
|
|
2016-11-16 09:21:49 -07:00
|
|
|
debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
|
2015-01-29 14:03:34 +02:00
|
|
|
|
2013-08-13 13:22:58 -07:00
|
|
|
// Check the cache.
|
2016-11-16 09:21:49 -07:00
|
|
|
if let Some(&val) = ccx.vtables().borrow().get(&(ty, trait_ref)) {
|
2016-10-17 19:00:20 -07:00
|
|
|
return val;
|
2013-08-13 13:22:58 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-12 11:42:58 -04:00
|
|
|
// Not in the cache. Build it.
|
2016-09-19 12:47:47 +03:00
|
|
|
let nullptr = C_null(Type::nil(ccx).ptr_to());
|
2013-08-13 13:22:58 -07:00
|
|
|
|
2016-11-16 09:21:49 -07:00
|
|
|
let size_ty = sizing_type_of(ccx, ty);
|
2014-08-06 11:59:40 +02:00
|
|
|
let size = machine::llsize_of_alloc(ccx, size_ty);
|
2016-11-16 09:21:49 -07:00
|
|
|
let align = align_of(ccx, ty);
|
2014-08-06 11:59:40 +02:00
|
|
|
|
2016-11-16 09:21:49 -07:00
|
|
|
let mut components: Vec<_> = [
|
2015-01-29 14:03:34 +02:00
|
|
|
// Generate a destructor for the vtable.
|
2016-11-16 09:21:49 -07:00
|
|
|
glue::get_drop_glue(ccx, ty),
|
2015-01-29 14:03:34 +02:00
|
|
|
C_uint(ccx, size),
|
|
|
|
|
C_uint(ccx, align)
|
2016-11-16 09:21:49 -07:00
|
|
|
].iter().cloned().collect();
|
|
|
|
|
|
|
|
|
|
if let Some(trait_ref) = trait_ref {
|
|
|
|
|
let trait_ref = trait_ref.with_self_ty(tcx, ty);
|
|
|
|
|
let methods = traits::get_vtable_methods(tcx, trait_ref).map(|opt_mth| {
|
|
|
|
|
opt_mth.map_or(nullptr, |(def_id, substs)| {
|
|
|
|
|
Callee::def(ccx, def_id, substs).reify(ccx)
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
components.extend(methods);
|
|
|
|
|
}
|
2015-01-29 14:03:34 +02:00
|
|
|
|
2015-10-09 01:26:21 +02:00
|
|
|
let vtable_const = C_struct(ccx, &components, false);
|
|
|
|
|
let align = machine::llalign_of_pref(ccx, val_ty(vtable_const));
|
|
|
|
|
let vtable = consts::addr_of(ccx, vtable_const, align, "vtable");
|
2013-12-18 17:00:56 -08:00
|
|
|
|
2016-11-16 09:21:49 -07:00
|
|
|
ccx.vtables().borrow_mut().insert((ty, trait_ref), vtable);
|
2014-04-10 14:04:45 +03:00
|
|
|
vtable
|
2012-01-12 16:57:30 +01:00
|
|
|
}
|