2012-12-04 00:48:01 +00: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.
|
|
|
|
|
2018-04-25 13:45:29 +00:00
|
|
|
use abi::{FnType, FnTypeExt};
|
2017-03-08 16:33:21 +00:00
|
|
|
use callee;
|
2016-12-31 23:00:24 +00:00
|
|
|
use builder::Builder;
|
2017-03-13 23:08:21 +00:00
|
|
|
use monomorphize;
|
2016-03-22 17:23:36 +00:00
|
|
|
use value::Value;
|
2018-07-10 10:28:39 +00:00
|
|
|
|
2018-09-13 12:58:19 +00:00
|
|
|
use interfaces::*;
|
2018-08-07 15:14:40 +00:00
|
|
|
|
2017-09-15 01:44:23 +00:00
|
|
|
use rustc::ty::{self, Ty};
|
2018-09-13 12:58:19 +00:00
|
|
|
use rustc::ty::layout::HasTyCtxt;
|
2012-08-28 22:54:45 +00:00
|
|
|
|
2017-03-13 23:08:21 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2017-06-25 09:41:24 +00:00
|
|
|
pub struct VirtualIndex(u64);
|
2017-03-13 23:08:21 +00:00
|
|
|
|
|
|
|
pub const DESTRUCTOR: VirtualIndex = VirtualIndex(0);
|
|
|
|
pub const SIZE: VirtualIndex = VirtualIndex(1);
|
|
|
|
pub const ALIGN: VirtualIndex = VirtualIndex(2);
|
|
|
|
|
2018-09-14 15:48:57 +00:00
|
|
|
impl<'a, 'tcx: 'a> VirtualIndex {
|
2017-03-13 23:08:21 +00:00
|
|
|
pub fn from_index(index: usize) -> Self {
|
2017-06-25 09:41:24 +00:00
|
|
|
VirtualIndex(index as u64 + 3)
|
2017-03-13 23:08:21 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 14:52:53 +00:00
|
|
|
pub fn get_fn(self, bx: &Builder<'a, 'll, 'tcx>,
|
2018-07-10 10:28:39 +00:00
|
|
|
llvtable: &'ll Value,
|
|
|
|
fn_ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Value {
|
2017-03-13 23:08:21 +00:00
|
|
|
// Load the data pointer from the object.
|
2018-07-10 10:28:39 +00:00
|
|
|
debug!("get_fn({:?}, {:?})", llvtable, self);
|
2017-03-13 23:08:21 +00:00
|
|
|
|
2018-09-05 21:14:03 +00:00
|
|
|
let llvtable = bx.pointercast(
|
|
|
|
llvtable,
|
2018-09-06 20:52:15 +00:00
|
|
|
bx.cx().type_ptr_to(fn_ty.ptr_to_llvm_type(bx.cx()))
|
2018-09-05 21:14:03 +00:00
|
|
|
);
|
2018-01-05 05:12:32 +00:00
|
|
|
let ptr_align = bx.tcx().data_layout.pointer_align;
|
2018-08-28 15:03:46 +00:00
|
|
|
let ptr = bx.load(
|
2018-09-06 18:57:42 +00:00
|
|
|
bx.inbounds_gep(llvtable, &[bx.cx().const_usize(self.0)]),
|
2018-08-28 15:03:46 +00:00
|
|
|
ptr_align
|
|
|
|
);
|
2018-01-05 05:12:32 +00:00
|
|
|
bx.nonnull_metadata(ptr);
|
2017-03-13 23:08:21 +00:00
|
|
|
// Vtable loads are invariant
|
2018-01-05 05:12:32 +00:00
|
|
|
bx.set_invariant_load(ptr);
|
2017-03-13 23:08:21 +00:00
|
|
|
ptr
|
|
|
|
}
|
|
|
|
|
2018-09-14 15:48:57 +00:00
|
|
|
pub fn get_usize<Bx: BuilderMethods<'a, 'tcx>>(
|
2018-08-07 15:14:40 +00:00
|
|
|
self,
|
2018-09-14 15:48:57 +00:00
|
|
|
bx: &Bx,
|
|
|
|
llvtable: Bx::Value
|
|
|
|
) -> Bx::Value {
|
2017-03-13 23:08:21 +00:00
|
|
|
// Load the data pointer from the object.
|
2018-07-10 10:28:39 +00:00
|
|
|
debug!("get_int({:?}, {:?})", llvtable, self);
|
2017-03-13 23:08:21 +00:00
|
|
|
|
2018-09-06 20:52:15 +00:00
|
|
|
let llvtable = bx.pointercast(llvtable, bx.cx().type_ptr_to(bx.cx().type_isize()));
|
2018-01-05 05:12:32 +00:00
|
|
|
let usize_align = bx.tcx().data_layout.pointer_align;
|
2018-08-28 15:03:46 +00:00
|
|
|
let ptr = bx.load(
|
2018-09-06 18:57:42 +00:00
|
|
|
bx.inbounds_gep(llvtable, &[bx.cx().const_usize(self.0)]),
|
2018-08-28 15:03:46 +00:00
|
|
|
usize_align
|
|
|
|
);
|
2017-03-13 23:08:21 +00:00
|
|
|
// Vtable loads are invariant
|
2018-01-05 05:12:32 +00:00
|
|
|
bx.set_invariant_load(ptr);
|
2017-03-13 23:08:21 +00:00
|
|
|
ptr
|
|
|
|
}
|
2012-01-07 21:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-09-08 10:58:05 +00:00
|
|
|
/// Creates a dynamic vtable for the given type and vtable origin.
|
2013-05-16 00:38:52 +00:00
|
|
|
/// This is used only for objects.
|
2014-09-12 15:42:58 +00:00
|
|
|
///
|
2016-09-08 10:58:05 +00:00
|
|
|
/// The vtables are cached instead of created on every call.
|
|
|
|
///
|
2014-09-12 15:42:58 +00: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 00:36:41 +00:00
|
|
|
/// `trait_ref` would map `T:Trait`.
|
2018-09-13 12:58:19 +00:00
|
|
|
pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
|
|
|
|
cx: &Cx,
|
2018-07-10 10:28:39 +00:00
|
|
|
ty: Ty<'tcx>,
|
2018-10-13 09:32:49 +00:00
|
|
|
trait_ref: ty::PolyExistentialTraitRef<'tcx>,
|
2018-09-13 12:58:19 +00:00
|
|
|
) -> Cx::Value {
|
|
|
|
let tcx = cx.tcx();
|
2013-08-13 20:22:58 +00:00
|
|
|
|
2016-11-16 16:21:49 +00:00
|
|
|
debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
|
2015-01-29 12:03:34 +00:00
|
|
|
|
2013-08-13 20:22:58 +00:00
|
|
|
// Check the cache.
|
2018-09-13 12:58:19 +00:00
|
|
|
if let Some(&val) = cx.vtables().borrow().get(&(ty, trait_ref)) {
|
2016-10-18 02:00:20 +00:00
|
|
|
return val;
|
2013-08-13 20:22:58 +00:00
|
|
|
}
|
|
|
|
|
2014-09-12 15:42:58 +00:00
|
|
|
// Not in the cache. Build it.
|
2018-09-06 20:52:15 +00:00
|
|
|
let nullptr = cx.const_null(cx.type_i8p());
|
2013-08-13 20:22:58 +00:00
|
|
|
|
2018-10-13 09:32:49 +00:00
|
|
|
let methods = tcx.vtable_methods(trait_ref.with_self_ty(tcx, ty));
|
|
|
|
let methods = methods.iter().cloned().map(|opt_mth| {
|
|
|
|
opt_mth.map_or(nullptr, |(def_id, substs)| {
|
2018-09-11 14:30:55 +00:00
|
|
|
callee::resolve_and_get_fn_for_vtable(cx, def_id, substs)
|
2018-10-13 09:32:49 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2018-09-13 12:58:19 +00:00
|
|
|
let (size, align) = cx.layout_of(ty).size_and_align();
|
2018-10-12 15:08:36 +00:00
|
|
|
// /////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// If you touch this code, be sure to also make the corresponding changes to
|
|
|
|
// `get_vtable` in rust_mir/interpret/traits.rs
|
|
|
|
// /////////////////////////////////////////////////////////////////////////////////////////////
|
2018-10-13 09:32:49 +00:00
|
|
|
let components: Vec<_> = [
|
2018-09-13 12:58:19 +00:00
|
|
|
cx.get_fn(monomorphize::resolve_drop_in_place(cx.tcx(), ty)),
|
2018-09-06 18:57:42 +00:00
|
|
|
cx.const_usize(size.bytes()),
|
|
|
|
cx.const_usize(align.abi())
|
2018-10-13 09:32:49 +00:00
|
|
|
].iter().cloned().chain(methods).collect();
|
2015-01-29 12:03:34 +00:00
|
|
|
|
2018-09-06 18:57:42 +00:00
|
|
|
let vtable_const = cx.const_struct(&components, false);
|
2018-01-05 05:04:08 +00:00
|
|
|
let align = cx.data_layout().pointer_align;
|
2018-09-10 14:28:47 +00:00
|
|
|
let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
|
2013-12-19 01:00:56 +00:00
|
|
|
|
2018-09-13 12:58:19 +00:00
|
|
|
cx.create_vtable_metadata(ty, vtable);
|
2017-05-27 19:46:42 +00:00
|
|
|
|
2018-09-13 12:58:19 +00:00
|
|
|
cx.vtables().borrow_mut().insert((ty, trait_ref), vtable);
|
2014-04-10 11:04:45 +00:00
|
|
|
vtable
|
2012-01-12 15:57:30 +00:00
|
|
|
}
|