1 use rustc_target::abi::call::FnType;
6 use rustc::ty::{self, Ty, Instance};
8 #[derive(Copy, Clone, Debug)]
9 pub struct VirtualIndex(u64);
11 pub const DESTRUCTOR: VirtualIndex = VirtualIndex(0);
12 pub const SIZE: VirtualIndex = VirtualIndex(1);
13 pub const ALIGN: VirtualIndex = VirtualIndex(2);
15 impl<'a, 'tcx: 'a> VirtualIndex {
16 pub fn from_index(index: usize) -> Self {
17 VirtualIndex(index as u64 + 3)
20 pub fn get_fn<Bx: BuilderMethods<'a, 'tcx>>(
24 fn_ty: &FnType<'tcx, Ty<'tcx>>
26 // Load the data pointer from the object.
27 debug!("get_fn({:?}, {:?})", llvtable, self);
29 let llvtable = bx.pointercast(
31 bx.type_ptr_to(bx.fn_ptr_backend_type(fn_ty))
33 let ptr_align = bx.tcx().data_layout.pointer_align.abi;
34 let gep = bx.inbounds_gep(llvtable, &[bx.const_usize(self.0)]);
35 let ptr = bx.load(gep, ptr_align);
36 bx.nonnull_metadata(ptr);
37 // Vtable loads are invariant
38 bx.set_invariant_load(ptr);
42 pub fn get_usize<Bx: BuilderMethods<'a, 'tcx>>(
47 // Load the data pointer from the object.
48 debug!("get_int({:?}, {:?})", llvtable, self);
50 let llvtable = bx.pointercast(llvtable, bx.type_ptr_to(bx.type_isize()));
51 let usize_align = bx.tcx().data_layout.pointer_align.abi;
52 let gep = bx.inbounds_gep(llvtable, &[bx.const_usize(self.0)]);
53 let ptr = bx.load(gep, usize_align);
54 // Vtable loads are invariant
55 bx.set_invariant_load(ptr);
60 /// Creates a dynamic vtable for the given type and vtable origin.
61 /// This is used only for objects.
63 /// The vtables are cached instead of created on every call.
65 /// The `trait_ref` encodes the erased self type. Hence if we are
66 /// making an object `Foo<dyn Trait>` from a value of type `Foo<T>`, then
67 /// `trait_ref` would map `T:Trait`.
68 pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
71 trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
75 debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
78 if let Some(&val) = cx.vtables().borrow().get(&(ty, trait_ref)) {
82 // Not in the cache. Build it.
83 let nullptr = cx.const_null(cx.type_i8p());
86 let methods = if let Some(trait_ref) = trait_ref {
87 methods_root = tcx.vtable_methods(trait_ref.with_self_ty(tcx, ty));
93 let methods = methods.cloned().map(|opt_mth| {
94 opt_mth.map_or(nullptr, |(def_id, substs)| {
95 callee::resolve_and_get_fn_for_vtable(cx, def_id, substs)
99 let layout = cx.layout_of(ty);
100 // /////////////////////////////////////////////////////////////////////////////////////////////
101 // If you touch this code, be sure to also make the corresponding changes to
102 // `get_vtable` in rust_mir/interpret/traits.rs
103 // /////////////////////////////////////////////////////////////////////////////////////////////
104 let components: Vec<_> = [
105 cx.get_fn(Instance::resolve_drop_in_place(cx.tcx(), ty)),
106 cx.const_usize(layout.size.bytes()),
107 cx.const_usize(layout.align.abi.bytes())
108 ].iter().cloned().chain(methods).collect();
110 let vtable_const = cx.const_struct(&components, false);
111 let align = cx.data_layout().pointer_align.abi;
112 let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
114 cx.create_vtable_metadata(ty, vtable);
116 cx.vtables().borrow_mut().insert((ty, trait_ref), vtable);