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