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.
|
|
|
|
|
2017-03-08 10:33:21 -06:00
|
|
|
use llvm::ValueRef;
|
2017-09-21 12:40:50 -05:00
|
|
|
use abi::FnType;
|
2017-03-08 10:33:21 -06:00
|
|
|
use callee;
|
2016-03-22 12:23:36 -05:00
|
|
|
use common::*;
|
2016-12-31 17:00:24 -06:00
|
|
|
use builder::Builder;
|
2016-03-22 12:23:36 -05:00
|
|
|
use consts;
|
2017-03-13 18:08:21 -05:00
|
|
|
use monomorphize;
|
2016-03-22 12:23:36 -05:00
|
|
|
use type_::Type;
|
|
|
|
use value::Value;
|
2017-09-14 20:44:23 -05:00
|
|
|
use rustc::ty::{self, Ty};
|
2017-06-01 13:50:53 -05:00
|
|
|
use rustc::ty::layout::HasDataLayout;
|
2017-05-27 14:46:42 -05:00
|
|
|
use debuginfo;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2017-03-13 18:08:21 -05:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2017-06-25 04:41:24 -05:00
|
|
|
pub struct VirtualIndex(u64);
|
2017-03-13 18:08:21 -05:00
|
|
|
|
|
|
|
pub const DESTRUCTOR: VirtualIndex = VirtualIndex(0);
|
|
|
|
pub const SIZE: VirtualIndex = VirtualIndex(1);
|
|
|
|
pub const ALIGN: VirtualIndex = VirtualIndex(2);
|
|
|
|
|
|
|
|
impl<'a, 'tcx> VirtualIndex {
|
|
|
|
pub fn from_index(index: usize) -> Self {
|
2017-06-25 04:41:24 -05:00
|
|
|
VirtualIndex(index as u64 + 3)
|
2017-03-13 18:08:21 -05:00
|
|
|
}
|
|
|
|
|
2018-01-04 23:12:32 -06:00
|
|
|
pub fn get_fn(self, bx: &Builder<'a, 'tcx>,
|
2017-09-21 12:40:50 -05:00
|
|
|
llvtable: ValueRef,
|
|
|
|
fn_ty: &FnType<'tcx>) -> ValueRef {
|
2017-03-13 18:08:21 -05:00
|
|
|
// Load the data pointer from the object.
|
|
|
|
debug!("get_fn({:?}, {:?})", Value(llvtable), self);
|
|
|
|
|
2018-01-04 23:12:32 -06:00
|
|
|
let llvtable = bx.pointercast(llvtable, fn_ty.llvm_type(bx.cx).ptr_to().ptr_to());
|
|
|
|
let ptr_align = bx.tcx().data_layout.pointer_align;
|
|
|
|
let ptr = bx.load(bx.inbounds_gep(llvtable, &[C_usize(bx.cx, self.0)]), ptr_align);
|
|
|
|
bx.nonnull_metadata(ptr);
|
2017-03-13 18:08:21 -05:00
|
|
|
// Vtable loads are invariant
|
2018-01-04 23:12:32 -06:00
|
|
|
bx.set_invariant_load(ptr);
|
2017-03-13 18:08:21 -05:00
|
|
|
ptr
|
|
|
|
}
|
|
|
|
|
2018-01-04 23:12:32 -06:00
|
|
|
pub fn get_usize(self, bx: &Builder<'a, 'tcx>, llvtable: ValueRef) -> ValueRef {
|
2017-03-13 18:08:21 -05:00
|
|
|
// Load the data pointer from the object.
|
|
|
|
debug!("get_int({:?}, {:?})", Value(llvtable), self);
|
|
|
|
|
2018-01-04 23:12:32 -06:00
|
|
|
let llvtable = bx.pointercast(llvtable, Type::isize(bx.cx).ptr_to());
|
|
|
|
let usize_align = bx.tcx().data_layout.pointer_align;
|
|
|
|
let ptr = bx.load(bx.inbounds_gep(llvtable, &[C_usize(bx.cx, self.0)]), usize_align);
|
2017-03-13 18:08:21 -05:00
|
|
|
// Vtable loads are invariant
|
2018-01-04 23:12:32 -06:00
|
|
|
bx.set_invariant_load(ptr);
|
2017-03-13 18:08:21 -05:00
|
|
|
ptr
|
|
|
|
}
|
2012-01-07 15:44:14 -06:00
|
|
|
}
|
|
|
|
|
2016-09-08 05:58:05 -05:00
|
|
|
/// Creates a dynamic vtable for the given type and vtable origin.
|
2013-05-15 19:38:52 -05:00
|
|
|
/// This is used only for objects.
|
2014-09-12 10:42:58 -05:00
|
|
|
///
|
2016-09-08 05:58:05 -05:00
|
|
|
/// The vtables are cached instead of created on every call.
|
|
|
|
///
|
2014-09-12 10:42:58 -05: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-13 19:36:41 -05:00
|
|
|
/// `trait_ref` would map `T:Trait`.
|
2018-01-04 23:04:08 -06:00
|
|
|
pub fn get_vtable<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
|
2017-09-14 20:44:23 -05:00
|
|
|
ty: Ty<'tcx>,
|
2016-11-16 10:21:49 -06:00
|
|
|
trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>)
|
2015-01-29 06:03:34 -06:00
|
|
|
-> ValueRef
|
2014-05-31 17:53:13 -05:00
|
|
|
{
|
2018-01-04 23:04:08 -06:00
|
|
|
let tcx = cx.tcx;
|
2013-08-13 15:22:58 -05:00
|
|
|
|
2016-11-16 10:21:49 -06:00
|
|
|
debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
|
2015-01-29 06:03:34 -06:00
|
|
|
|
2013-08-13 15:22:58 -05:00
|
|
|
// Check the cache.
|
2018-01-04 23:04:08 -06:00
|
|
|
if let Some(&val) = cx.vtables.borrow().get(&(ty, trait_ref)) {
|
2016-10-17 21:00:20 -05:00
|
|
|
return val;
|
2013-08-13 15:22:58 -05:00
|
|
|
}
|
|
|
|
|
2014-09-12 10:42:58 -05:00
|
|
|
// Not in the cache. Build it.
|
2018-01-04 23:04:08 -06:00
|
|
|
let nullptr = C_null(Type::i8p(cx));
|
2013-08-13 15:22:58 -05:00
|
|
|
|
2018-01-04 23:04:08 -06:00
|
|
|
let (size, align) = cx.size_and_align_of(ty);
|
2016-11-16 10:21:49 -06:00
|
|
|
let mut components: Vec<_> = [
|
2018-01-04 23:04:08 -06:00
|
|
|
callee::get_fn(cx, monomorphize::resolve_drop_in_place(cx.tcx, ty)),
|
|
|
|
C_usize(cx, size.bytes()),
|
|
|
|
C_usize(cx, align.abi())
|
2016-11-16 10:21:49 -06:00
|
|
|
].iter().cloned().collect();
|
|
|
|
|
|
|
|
if let Some(trait_ref) = trait_ref {
|
|
|
|
let trait_ref = trait_ref.with_self_ty(tcx, ty);
|
2017-10-09 10:39:53 -05:00
|
|
|
let methods = tcx.vtable_methods(trait_ref);
|
|
|
|
let methods = methods.iter().cloned().map(|opt_mth| {
|
2016-11-16 10:21:49 -06:00
|
|
|
opt_mth.map_or(nullptr, |(def_id, substs)| {
|
2018-01-04 23:04:08 -06:00
|
|
|
callee::resolve_and_get_fn(cx, def_id, substs)
|
2016-11-16 10:21:49 -06:00
|
|
|
})
|
|
|
|
});
|
|
|
|
components.extend(methods);
|
|
|
|
}
|
2015-01-29 06:03:34 -06:00
|
|
|
|
2018-01-04 23:04:08 -06:00
|
|
|
let vtable_const = C_struct(cx, &components, false);
|
|
|
|
let align = cx.data_layout().pointer_align;
|
|
|
|
let vtable = consts::addr_of(cx, vtable_const, align, "vtable");
|
2013-12-18 19:00:56 -06:00
|
|
|
|
2018-01-04 23:04:08 -06:00
|
|
|
debuginfo::create_vtable_metadata(cx, ty, vtable);
|
2017-05-27 14:46:42 -05:00
|
|
|
|
2018-01-04 23:04:08 -06:00
|
|
|
cx.vtables.borrow_mut().insert((ty, trait_ref), vtable);
|
2014-04-10 06:04:45 -05:00
|
|
|
vtable
|
2012-01-12 09:57:30 -06:00
|
|
|
}
|