]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/meth.rs
Rollup merge of #76033 - camelid:patch-7, r=Dylan-DPC
[rust.git] / compiler / rustc_codegen_ssa / src / meth.rs
1 use crate::traits::*;
2
3 use rustc_middle::ty::{self, Instance, Ty};
4 use rustc_target::abi::call::FnAbi;
5
6 #[derive(Copy, Clone, Debug)]
7 pub struct VirtualIndex(u64);
8
9 pub const DESTRUCTOR: VirtualIndex = VirtualIndex(0);
10 pub const SIZE: VirtualIndex = VirtualIndex(1);
11 pub const ALIGN: VirtualIndex = VirtualIndex(2);
12
13 impl<'a, 'tcx> VirtualIndex {
14     pub fn from_index(index: usize) -> Self {
15         VirtualIndex(index as u64 + 3)
16     }
17
18     pub fn get_fn<Bx: BuilderMethods<'a, 'tcx>>(
19         self,
20         bx: &mut Bx,
21         llvtable: Bx::Value,
22         fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
23     ) -> Bx::Value {
24         // Load the data pointer from the object.
25         debug!("get_fn({:?}, {:?})", llvtable, self);
26
27         let llvtable = bx.pointercast(llvtable, bx.type_ptr_to(bx.fn_ptr_backend_type(fn_abi)));
28         let ptr_align = bx.tcx().data_layout.pointer_align.abi;
29         let gep = bx.inbounds_gep(llvtable, &[bx.const_usize(self.0)]);
30         let ptr = bx.load(gep, ptr_align);
31         bx.nonnull_metadata(ptr);
32         // Vtable loads are invariant.
33         bx.set_invariant_load(ptr);
34         ptr
35     }
36
37     pub fn get_usize<Bx: BuilderMethods<'a, 'tcx>>(
38         self,
39         bx: &mut Bx,
40         llvtable: Bx::Value,
41     ) -> Bx::Value {
42         // Load the data pointer from the object.
43         debug!("get_int({:?}, {:?})", llvtable, self);
44
45         let llvtable = bx.pointercast(llvtable, bx.type_ptr_to(bx.type_isize()));
46         let usize_align = bx.tcx().data_layout.pointer_align.abi;
47         let gep = bx.inbounds_gep(llvtable, &[bx.const_usize(self.0)]);
48         let ptr = bx.load(gep, usize_align);
49         // Vtable loads are invariant.
50         bx.set_invariant_load(ptr);
51         ptr
52     }
53 }
54
55 /// Creates a dynamic vtable for the given type and vtable origin.
56 /// This is used only for objects.
57 ///
58 /// The vtables are cached instead of created on every call.
59 ///
60 /// The `trait_ref` encodes the erased self type. Hence if we are
61 /// making an object `Foo<dyn Trait>` from a value of type `Foo<T>`, then
62 /// `trait_ref` would map `T: Trait`.
63 pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
64     cx: &Cx,
65     ty: Ty<'tcx>,
66     trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
67 ) -> Cx::Value {
68     let tcx = cx.tcx();
69
70     debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
71
72     // Check the cache.
73     if let Some(&val) = cx.vtables().borrow().get(&(ty, trait_ref)) {
74         return val;
75     }
76
77     // Not in the cache; build it.
78     let nullptr = cx.const_null(cx.type_i8p_ext(cx.data_layout().instruction_address_space));
79
80     let methods_root;
81     let methods = if let Some(trait_ref) = trait_ref {
82         methods_root = tcx.vtable_methods(trait_ref.with_self_ty(tcx, ty));
83         methods_root.iter()
84     } else {
85         (&[]).iter()
86     };
87
88     let methods = methods.cloned().map(|opt_mth| {
89         opt_mth.map_or(nullptr, |(def_id, substs)| {
90             cx.get_fn_addr(
91                 ty::Instance::resolve_for_vtable(
92                     cx.tcx(),
93                     ty::ParamEnv::reveal_all(),
94                     def_id,
95                     substs,
96                 )
97                 .unwrap()
98                 .polymorphize(cx.tcx()),
99             )
100         })
101     });
102
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_addr(Instance::resolve_drop_in_place(cx.tcx(), ty)),
110         cx.const_usize(layout.size.bytes()),
111         cx.const_usize(layout.align.abi.bytes()),
112     ]
113     .iter()
114     .cloned()
115     .chain(methods)
116     .collect();
117
118     let vtable_const = cx.const_struct(&components, false);
119     let align = cx.data_layout().pointer_align.abi;
120     let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
121
122     cx.create_vtable_metadata(ty, vtable);
123
124     cx.vtables().borrow_mut().insert((ty, trait_ref), vtable);
125     vtable
126 }