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