]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/meth.rs
Rollup merge of #105598 - RalfJung:more-comments, r=the8472
[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 func = bx.type_checked_load(llvtable, vtable_byte_offset, typeid);
35             bx.pointercast(func, llty)
36         } else {
37             let ptr_align = bx.tcx().data_layout.pointer_align.abi;
38             let gep = bx.inbounds_gep(llty, llvtable, &[bx.const_usize(self.0)]);
39             let ptr = bx.load(llty, gep, ptr_align);
40             bx.nonnull_metadata(ptr);
41             // VTable loads are invariant.
42             bx.set_invariant_load(ptr);
43             ptr
44         }
45     }
46
47     pub fn get_usize<Bx: BuilderMethods<'a, 'tcx>>(
48         self,
49         bx: &mut Bx,
50         llvtable: Bx::Value,
51     ) -> Bx::Value {
52         // Load the data pointer from the object.
53         debug!("get_int({:?}, {:?})", llvtable, self);
54
55         let llty = bx.type_isize();
56         let llvtable = bx.pointercast(llvtable, bx.type_ptr_to(llty));
57         let usize_align = bx.tcx().data_layout.pointer_align.abi;
58         let gep = bx.inbounds_gep(llty, llvtable, &[bx.const_usize(self.0)]);
59         let ptr = bx.load(llty, gep, usize_align);
60         // VTable loads are invariant.
61         bx.set_invariant_load(ptr);
62         ptr
63     }
64 }
65
66 /// This takes a valid `self` receiver type and extracts the principal trait
67 /// ref of the type.
68 fn expect_dyn_trait_in_self<'tcx>(ty: Ty<'tcx>) -> ty::PolyExistentialTraitRef<'tcx> {
69     for arg in ty.peel_refs().walk() {
70         if let GenericArgKind::Type(ty) = arg.unpack() {
71             if let ty::Dynamic(data, _, _) = ty.kind() {
72                 return data.principal().expect("expected principal trait object");
73             }
74         }
75     }
76
77     bug!("expected a `dyn Trait` ty, found {ty:?}")
78 }
79
80 /// Creates a dynamic vtable for the given type and vtable origin.
81 /// This is used only for objects.
82 ///
83 /// The vtables are cached instead of created on every call.
84 ///
85 /// The `trait_ref` encodes the erased self type. Hence if we are
86 /// making an object `Foo<dyn Trait>` from a value of type `Foo<T>`, then
87 /// `trait_ref` would map `T: Trait`.
88 #[instrument(level = "debug", skip(cx))]
89 pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
90     cx: &Cx,
91     ty: Ty<'tcx>,
92     trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
93 ) -> Cx::Value {
94     let tcx = cx.tcx();
95
96     // Check the cache.
97     if let Some(&val) = cx.vtables().borrow().get(&(ty, trait_ref)) {
98         return val;
99     }
100
101     let vtable_alloc_id = tcx.vtable_allocation((ty, trait_ref));
102     let vtable_allocation = tcx.global_alloc(vtable_alloc_id).unwrap_memory();
103     let vtable_const = cx.const_data_from_alloc(vtable_allocation);
104     let align = cx.data_layout().pointer_align.abi;
105     let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
106
107     cx.create_vtable_debuginfo(ty, trait_ref, vtable);
108     cx.vtables().borrow_mut().insert((ty, trait_ref), vtable);
109     vtable
110 }