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