1 use rustc_target::abi::call::FnAbi;
5 use rustc::ty::{self, Ty, Instance};
7 #[derive(Copy, Clone, Debug)]
8 pub struct VirtualIndex(u64);
10 pub const DESTRUCTOR: VirtualIndex = VirtualIndex(0);
11 pub const SIZE: VirtualIndex = VirtualIndex(1);
12 pub const ALIGN: VirtualIndex = VirtualIndex(2);
14 impl<'a, 'tcx> VirtualIndex {
15 pub fn from_index(index: usize) -> Self {
16 VirtualIndex(index as u64 + 3)
19 pub fn get_fn<Bx: BuilderMethods<'a, 'tcx>>(
23 fn_abi: &FnAbi<'tcx, Ty<'tcx>>
25 // Load the data pointer from the object.
26 debug!("get_fn({:?}, {:?})", llvtable, self);
28 let llvtable = bx.pointercast(
30 bx.type_ptr_to(bx.fn_ptr_backend_type(fn_abi))
32 let ptr_align = bx.tcx().data_layout.pointer_align.abi;
33 let gep = bx.inbounds_gep(llvtable, &[bx.const_usize(self.0)]);
34 let ptr = bx.load(gep, ptr_align);
35 bx.nonnull_metadata(ptr);
36 // Vtable loads are invariant
37 bx.set_invariant_load(ptr);
41 pub fn get_usize<Bx: BuilderMethods<'a, 'tcx>>(
46 // Load the data pointer from the object.
47 debug!("get_int({:?}, {:?})", llvtable, self);
49 let llvtable = bx.pointercast(llvtable, bx.type_ptr_to(bx.type_isize()));
50 let usize_align = bx.tcx().data_layout.pointer_align.abi;
51 let gep = bx.inbounds_gep(llvtable, &[bx.const_usize(self.0)]);
52 let ptr = bx.load(gep, usize_align);
53 // Vtable loads are invariant
54 bx.set_invariant_load(ptr);
59 /// Creates a dynamic vtable for the given type and vtable origin.
60 /// This is used only for objects.
62 /// The vtables are cached instead of created on every call.
64 /// The `trait_ref` encodes the erased self type. Hence if we are
65 /// making an object `Foo<dyn Trait>` from a value of type `Foo<T>`, then
66 /// `trait_ref` would map `T:Trait`.
67 pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
70 trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
74 debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
77 if let Some(&val) = cx.vtables().borrow().get(&(ty, trait_ref)) {
81 // Not in the cache. Build it.
82 let nullptr = cx.const_null(cx.type_i8p());
85 let methods = if let Some(trait_ref) = trait_ref {
86 methods_root = tcx.vtable_methods(trait_ref.with_self_ty(tcx, ty));
92 let methods = methods.cloned().map(|opt_mth| {
93 opt_mth.map_or(nullptr, |(def_id, substs)| {
95 ty::Instance::resolve_for_vtable(
97 ty::ParamEnv::reveal_all(),
105 let layout = cx.layout_of(ty);
106 // /////////////////////////////////////////////////////////////////////////////////////////////
107 // If you touch this code, be sure to also make the corresponding changes to
108 // `get_vtable` in rust_mir/interpret/traits.rs
109 // /////////////////////////////////////////////////////////////////////////////////////////////
110 let components: Vec<_> = [
111 cx.get_fn_addr(Instance::resolve_drop_in_place(cx.tcx(), ty)),
112 cx.const_usize(layout.size.bytes()),
113 cx.const_usize(layout.align.abi.bytes())
114 ].iter().cloned().chain(methods).collect();
116 let vtable_const = cx.const_struct(&components, false);
117 let align = cx.data_layout().pointer_align.abi;
118 let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
120 cx.create_vtable_metadata(ty, vtable);
122 cx.vtables().borrow_mut().insert((ty, trait_ref), vtable);