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