1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rustc_target::abi::call::FnType;
13 use rustc_mir::monomorphize;
17 use rustc::ty::{self, Ty};
19 #[derive(Copy, Clone, Debug)]
20 pub struct VirtualIndex(u64);
22 pub const DESTRUCTOR: VirtualIndex = VirtualIndex(0);
23 pub const SIZE: VirtualIndex = VirtualIndex(1);
24 pub const ALIGN: VirtualIndex = VirtualIndex(2);
26 impl<'a, 'tcx: 'a> VirtualIndex {
27 pub fn from_index(index: usize) -> Self {
28 VirtualIndex(index as u64 + 3)
31 pub fn get_fn<Bx: BuilderMethods<'a, 'tcx>>(
35 fn_ty: &FnType<'tcx, Ty<'tcx>>
37 // Load the data pointer from the object.
38 debug!("get_fn({:?}, {:?})", llvtable, self);
40 let llvtable = bx.pointercast(
42 bx.cx().type_ptr_to(bx.cx().fn_ptr_backend_type(fn_ty))
44 let ptr_align = bx.tcx().data_layout.pointer_align.abi;
45 let gep = bx.inbounds_gep(llvtable, &[bx.cx().const_usize(self.0)]);
46 let ptr = bx.load(gep, ptr_align);
47 bx.nonnull_metadata(ptr);
48 // Vtable loads are invariant
49 bx.set_invariant_load(ptr);
53 pub fn get_usize<Bx: BuilderMethods<'a, 'tcx>>(
58 // Load the data pointer from the object.
59 debug!("get_int({:?}, {:?})", llvtable, self);
61 let llvtable = bx.pointercast(llvtable, bx.cx().type_ptr_to(bx.cx().type_isize()));
62 let usize_align = bx.tcx().data_layout.pointer_align.abi;
63 let gep = bx.inbounds_gep(llvtable, &[bx.cx().const_usize(self.0)]);
64 let ptr = bx.load(gep, usize_align);
65 // Vtable loads are invariant
66 bx.set_invariant_load(ptr);
71 /// Creates a dynamic vtable for the given type and vtable origin.
72 /// This is used only for objects.
74 /// The vtables are cached instead of created on every call.
76 /// The `trait_ref` encodes the erased self type. Hence if we are
77 /// making an object `Foo<dyn Trait>` from a value of type `Foo<T>`, then
78 /// `trait_ref` would map `T:Trait`.
79 pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
82 trait_ref: ty::PolyExistentialTraitRef<'tcx>,
86 debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
89 if let Some(&val) = cx.vtables().borrow().get(&(ty, trait_ref)) {
93 // Not in the cache. Build it.
94 let nullptr = cx.const_null(cx.type_i8p());
96 let methods = tcx.vtable_methods(trait_ref.with_self_ty(tcx, ty));
97 let methods = methods.iter().cloned().map(|opt_mth| {
98 opt_mth.map_or(nullptr, |(def_id, substs)| {
99 callee::resolve_and_get_fn_for_vtable(cx, def_id, substs)
103 let layout = cx.layout_of(ty);
104 // /////////////////////////////////////////////////////////////////////////////////////////////
105 // If you touch this code, be sure to also make the corresponding changes to
106 // `get_vtable` in rust_mir/interpret/traits.rs
107 // /////////////////////////////////////////////////////////////////////////////////////////////
108 let components: Vec<_> = [
109 cx.get_fn(monomorphize::resolve_drop_in_place(cx.tcx(), ty)),
110 cx.const_usize(layout.size.bytes()),
111 cx.const_usize(layout.align.abi.bytes())
112 ].iter().cloned().chain(methods).collect();
114 let vtable_const = cx.const_struct(&components, false);
115 let align = cx.data_layout().pointer_align.abi;
116 let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
118 cx.create_vtable_metadata(ty, vtable);
120 cx.vtables().borrow_mut().insert((ty, trait_ref), vtable);