3 use rustc::ty::{self, Ty, Instance};
4 use rustc_target::abi::call::FnAbi;
6 #[derive(Copy, Clone, Debug)]
7 pub struct VirtualIndex(u64);
9 pub const DESTRUCTOR: VirtualIndex = VirtualIndex(0);
10 pub const SIZE: VirtualIndex = VirtualIndex(1);
11 pub const ALIGN: VirtualIndex = VirtualIndex(2);
13 impl<'a, 'tcx> VirtualIndex {
14 pub fn from_index(index: usize) -> Self {
15 VirtualIndex(index as u64 + 3)
18 pub fn get_fn<Bx: BuilderMethods<'a, 'tcx>>(
22 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
24 // Load the data pointer from the object.
25 debug!("get_fn({:?}, {:?})", llvtable, self);
27 let llvtable = bx.pointercast(
29 bx.type_ptr_to(bx.fn_ptr_backend_type(fn_abi))
31 let ptr_align = bx.tcx().data_layout.pointer_align.abi;
32 let gep = bx.inbounds_gep(llvtable, &[bx.const_usize(self.0)]);
33 let ptr = bx.load(gep, ptr_align);
34 bx.nonnull_metadata(ptr);
35 // Vtable loads are invariant.
36 bx.set_invariant_load(ptr);
40 pub fn get_usize<Bx: BuilderMethods<'a, 'tcx>>(
45 // Load the data pointer from the object.
46 debug!("get_int({:?}, {:?})", llvtable, self);
48 let llvtable = bx.pointercast(llvtable, bx.type_ptr_to(bx.type_isize()));
49 let usize_align = bx.tcx().data_layout.pointer_align.abi;
50 let gep = bx.inbounds_gep(llvtable, &[bx.const_usize(self.0)]);
51 let ptr = bx.load(gep, usize_align);
52 // Vtable loads are invariant.
53 bx.set_invariant_load(ptr);
58 /// Creates a dynamic vtable for the given type and vtable origin.
59 /// This is used only for objects.
61 /// The vtables are cached instead of created on every call.
63 /// The `trait_ref` encodes the erased self type. Hence if we are
64 /// making an object `Foo<dyn Trait>` from a value of type `Foo<T>`, then
65 /// `trait_ref` would map `T: Trait`.
66 pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
69 trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
73 debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
76 if let Some(&val) = cx.vtables().borrow().get(&(ty, trait_ref)) {
80 // Not in the cache; build it.
81 let nullptr = cx.const_null(cx.type_i8p());
84 let methods = if let Some(trait_ref) = trait_ref {
85 methods_root = tcx.vtable_methods(trait_ref.with_self_ty(tcx, ty));
91 let methods = methods.cloned().map(|opt_mth| {
92 opt_mth.map_or(nullptr, |(def_id, substs)| {
94 ty::Instance::resolve_for_vtable(
96 ty::ParamEnv::reveal_all(),
104 let layout = cx.layout_of(ty);
105 // /////////////////////////////////////////////////////////////////////////////////////////////
106 // If you touch this code, be sure to also make the corresponding changes to
107 // `get_vtable` in `rust_mir/interpret/traits.rs`.
108 // /////////////////////////////////////////////////////////////////////////////////////////////
109 let components: Vec<_> = [
110 cx.get_fn_addr(Instance::resolve_drop_in_place(cx.tcx(), ty)),
111 cx.const_usize(layout.size.bytes()),
112 cx.const_usize(layout.align.abi.bytes())
113 ].iter().cloned().chain(methods).collect();
115 let vtable_const = cx.const_struct(&components, false);
116 let align = cx.data_layout().pointer_align.abi;
117 let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
119 cx.create_vtable_metadata(ty, vtable);
121 cx.vtables().borrow_mut().insert((ty, trait_ref), vtable);