]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/meth.rs
Auto merge of #101837 - scottmcm:box-array-from-vec, r=m-ou-se
[rust.git] / compiler / rustc_codegen_ssa / src / meth.rs
1 use crate::traits::*;
2
3 use rustc_middle::ty::{self, subst::GenericArgKind, Ty};
4 use rustc_session::config::Lto;
5 use rustc_symbol_mangling::typeid_for_trait_ref;
6 use rustc_target::abi::call::FnAbi;
7
8 #[derive(Copy, Clone, Debug)]
9 pub struct VirtualIndex(u64);
10
11 impl<'a, 'tcx> VirtualIndex {
12     pub fn from_index(index: usize) -> Self {
13         VirtualIndex(index as u64)
14     }
15
16     pub fn get_fn<Bx: BuilderMethods<'a, 'tcx>>(
17         self,
18         bx: &mut Bx,
19         llvtable: Bx::Value,
20         ty: Ty<'tcx>,
21         fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
22     ) -> Bx::Value {
23         // Load the data pointer from the object.
24         debug!("get_fn({llvtable:?}, {ty:?}, {self:?})");
25         let llty = bx.fn_ptr_backend_type(fn_abi);
26         let llvtable = bx.pointercast(llvtable, bx.type_ptr_to(llty));
27
28         if bx.cx().sess().opts.unstable_opts.virtual_function_elimination
29             && bx.cx().sess().lto() == Lto::Fat
30         {
31             let typeid =
32                 bx.typeid_metadata(typeid_for_trait_ref(bx.tcx(), expect_dyn_trait_in_self(ty)));
33             let vtable_byte_offset = self.0 * bx.data_layout().pointer_size.bytes();
34             let type_checked_load = bx.type_checked_load(llvtable, vtable_byte_offset, typeid);
35             let func = bx.extract_value(type_checked_load, 0);
36             bx.pointercast(func, llty)
37         } else {
38             let ptr_align = bx.tcx().data_layout.pointer_align.abi;
39             let gep = bx.inbounds_gep(llty, llvtable, &[bx.const_usize(self.0)]);
40             let ptr = bx.load(llty, gep, ptr_align);
41             bx.nonnull_metadata(ptr);
42             // VTable loads are invariant.
43             bx.set_invariant_load(ptr);
44             ptr
45         }
46     }
47
48     pub fn get_usize<Bx: BuilderMethods<'a, 'tcx>>(
49         self,
50         bx: &mut Bx,
51         llvtable: Bx::Value,
52     ) -> Bx::Value {
53         // Load the data pointer from the object.
54         debug!("get_int({:?}, {:?})", llvtable, self);
55
56         let llty = bx.type_isize();
57         let llvtable = bx.pointercast(llvtable, bx.type_ptr_to(llty));
58         let usize_align = bx.tcx().data_layout.pointer_align.abi;
59         let gep = bx.inbounds_gep(llty, llvtable, &[bx.const_usize(self.0)]);
60         let ptr = bx.load(llty, gep, usize_align);
61         // VTable loads are invariant.
62         bx.set_invariant_load(ptr);
63         ptr
64     }
65 }
66
67 /// This takes a valid `self` receiver type and extracts the principal trait
68 /// ref of the type.
69 fn expect_dyn_trait_in_self<'tcx>(ty: Ty<'tcx>) -> ty::PolyExistentialTraitRef<'tcx> {
70     for arg in ty.peel_refs().walk() {
71         if let GenericArgKind::Type(ty) = arg.unpack() {
72             if let ty::Dynamic(data, _, _) = ty.kind() {
73                 return data.principal().expect("expected principal trait object");
74             }
75         }
76     }
77
78     bug!("expected a `dyn Trait` ty, found {ty:?}")
79 }
80
81 /// Creates a dynamic vtable for the given type and vtable origin.
82 /// This is used only for objects.
83 ///
84 /// The vtables are cached instead of created on every call.
85 ///
86 /// The `trait_ref` encodes the erased self type. Hence if we are
87 /// making an object `Foo<dyn Trait>` from a value of type `Foo<T>`, then
88 /// `trait_ref` would map `T: Trait`.
89 #[instrument(level = "debug", skip(cx))]
90 pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
91     cx: &Cx,
92     ty: Ty<'tcx>,
93     trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
94 ) -> Cx::Value {
95     let tcx = cx.tcx();
96
97     // Check the cache.
98     if let Some(&val) = cx.vtables().borrow().get(&(ty, trait_ref)) {
99         return val;
100     }
101
102     let vtable_alloc_id = tcx.vtable_allocation((ty, trait_ref));
103     let vtable_allocation = tcx.global_alloc(vtable_alloc_id).unwrap_memory();
104     let vtable_const = cx.const_data_from_alloc(vtable_allocation);
105     let align = cx.data_layout().pointer_align.abi;
106     let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
107
108     cx.create_vtable_debuginfo(ty, trait_ref, vtable);
109     cx.vtables().borrow_mut().insert((ty, trait_ref), vtable);
110     vtable
111 }