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