rust/src/librustc_codegen_llvm/meth.rs

129 lines
4.3 KiB
Rust
Raw Normal View History

// 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.
use abi::{FnType, FnTypeExt};
use callee;
2018-08-28 15:03:46 +00:00
use context::CodegenCx;
2016-12-31 23:00:24 +00:00
use builder::Builder;
use monomorphize;
use value::Value;
2018-09-10 14:28:47 +00:00
use interfaces::{BuilderMethods, ConstMethods, BaseTypeMethods, DerivedTypeMethods, StaticMethods};
2017-09-15 01:44:23 +00:00
use rustc::ty::{self, Ty};
use rustc::ty::layout::HasDataLayout;
use debuginfo;
#[derive(Copy, Clone, Debug)]
pub struct VirtualIndex(u64);
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 {
VirtualIndex(index as u64 + 3)
}
pub fn get_fn(self, bx: &Builder<'a, 'll, 'tcx>,
llvtable: &'ll Value,
fn_ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Value {
// Load the data pointer from the object.
debug!("get_fn({:?}, {:?})", llvtable, self);
let llvtable = bx.pointercast(
llvtable,
bx.cx().type_ptr_to(fn_ty.ptr_to_llvm_type(bx.cx()))
);
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(
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);
// Vtable loads are invariant
2018-01-05 05:12:32 +00:00
bx.set_invariant_load(ptr);
ptr
}
pub fn get_usize(
self,
bx: &Builder<'a, 'll, 'tcx>,
llvtable: &'ll Value
) -> &'ll Value {
// Load the data pointer from the object.
debug!("get_int({:?}, {:?})", llvtable, self);
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(
bx.inbounds_gep(llvtable, &[bx.cx().const_usize(self.0)]),
2018-08-28 15:03:46 +00:00
usize_align
);
// Vtable loads are invariant
2018-01-05 05:12:32 +00:00
bx.set_invariant_load(ptr);
ptr
}
}
2016-09-08 10:58:05 +00:00
/// Creates a dynamic vtable for the given type and vtable origin.
/// This is used only for objects.
///
2016-09-08 10:58:05 +00:00
/// The vtables are cached instead of created on every call.
///
/// 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
/// `trait_ref` would map `T:Trait`.
pub fn get_vtable(
cx: &CodegenCx<'ll, 'tcx>,
ty: Ty<'tcx>,
trait_ref: ty::PolyExistentialTraitRef<'tcx>,
) -> &'ll Value {
2018-01-05 05:04:08 +00:00
let tcx = cx.tcx;
debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
// Check the cache.
2018-01-05 05:04:08 +00:00
if let Some(&val) = cx.vtables.borrow().get(&(ty, trait_ref)) {
return val;
}
// Not in the cache. Build it.
let nullptr = cx.const_null(cx.type_i8p());
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)| {
callee::resolve_and_get_fn_for_vtable(cx, def_id, substs)
})
});
2018-01-05 05:04:08 +00:00
let (size, align) = cx.size_and_align_of(ty);
// /////////////////////////////////////////////////////////////////////////////////////////////
// If you touch this code, be sure to also make the corresponding changes to
// `get_vtable` in rust_mir/interpret/traits.rs
// /////////////////////////////////////////////////////////////////////////////////////////////
let components: Vec<_> = [
2018-01-05 05:04:08 +00:00
callee::get_fn(cx, monomorphize::resolve_drop_in_place(cx.tcx, ty)),
cx.const_usize(size.bytes()),
cx.const_usize(align.abi())
].iter().cloned().chain(methods).collect();
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"));
2018-01-05 05:04:08 +00:00
debuginfo::create_vtable_metadata(cx, ty, vtable);
2018-01-05 05:04:08 +00:00
cx.vtables.borrow_mut().insert((ty, trait_ref), vtable);
2014-04-10 11:04:45 +00:00
vtable
}