]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/meth.rs
Merge commit 'e9d1a0a7b0b28dd422f1a790ccde532acafbf193' into sync_cg_clif-2022-08-24
[rust.git] / compiler / rustc_codegen_ssa / src / meth.rs
1 use crate::traits::*;
2
3 use rustc_middle::ty::{self, subst::GenericArgKind, ExistentialPredicate, Ty, TyCtxt};
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(), get_trait_ref(bx.tcx(), 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 fn get_trait_ref<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::PolyExistentialTraitRef<'tcx> {
68     for arg in ty.peel_refs().walk() {
69         if let GenericArgKind::Type(ty) = arg.unpack() {
70             if let ty::Dynamic(trait_refs, _) = ty.kind() {
71                 return trait_refs[0].map_bound(|trait_ref| match trait_ref {
72                     ExistentialPredicate::Trait(tr) => tr,
73                     ExistentialPredicate::Projection(proj) => proj.trait_ref(tcx),
74                     ExistentialPredicate::AutoTrait(_) => {
75                         bug!("auto traits don't have functions")
76                     }
77                 });
78             }
79         }
80     }
81
82     bug!("expected a `dyn Trait` ty, found {ty:?}")
83 }
84
85 /// Creates a dynamic vtable for the given type and vtable origin.
86 /// This is used only for objects.
87 ///
88 /// The vtables are cached instead of created on every call.
89 ///
90 /// The `trait_ref` encodes the erased self type. Hence if we are
91 /// making an object `Foo<dyn Trait>` from a value of type `Foo<T>`, then
92 /// `trait_ref` would map `T: Trait`.
93 pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
94     cx: &Cx,
95     ty: Ty<'tcx>,
96     trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
97 ) -> Cx::Value {
98     let tcx = cx.tcx();
99
100     debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
101
102     // Check the cache.
103     if let Some(&val) = cx.vtables().borrow().get(&(ty, trait_ref)) {
104         return val;
105     }
106
107     let vtable_alloc_id = tcx.vtable_allocation((ty, trait_ref));
108     let vtable_allocation = tcx.global_alloc(vtable_alloc_id).unwrap_memory();
109     let vtable_const = cx.const_data_from_alloc(vtable_allocation);
110     let align = cx.data_layout().pointer_align.abi;
111     let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
112
113     cx.create_vtable_debuginfo(ty, trait_ref, vtable);
114     cx.vtables().borrow_mut().insert((ty, trait_ref), vtable);
115     vtable
116 }