]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/meth.rs
All Builder methods now take &mut self instead of &self
[rust.git] / src / librustc_codegen_ssa / meth.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use rustc_target::abi::call::FnType;
12 use callee;
13 use rustc_mir::monomorphize;
14
15 use interfaces::*;
16
17 use rustc::ty::{self, Ty};
18
19 #[derive(Copy, Clone, Debug)]
20 pub struct VirtualIndex(u64);
21
22 pub const DESTRUCTOR: VirtualIndex = VirtualIndex(0);
23 pub const SIZE: VirtualIndex = VirtualIndex(1);
24 pub const ALIGN: VirtualIndex = VirtualIndex(2);
25
26 impl<'a, 'tcx: 'a> VirtualIndex {
27     pub fn from_index(index: usize) -> Self {
28         VirtualIndex(index as u64 + 3)
29     }
30
31     pub fn get_fn<Bx: BuilderMethods<'a, 'tcx>>(
32         self,
33         bx: &mut Bx,
34         llvtable: Bx::Value,
35         fn_ty: &FnType<'tcx, Ty<'tcx>>
36     ) -> Bx::Value {
37         // Load the data pointer from the object.
38         debug!("get_fn({:?}, {:?})", llvtable, self);
39
40         let llvtable = bx.pointercast(
41             llvtable,
42             bx.cx().type_ptr_to(bx.cx().fn_ptr_backend_type(fn_ty))
43         );
44         let ptr_align = bx.tcx().data_layout.pointer_align;
45         let gep = bx.inbounds_gep(llvtable, &[bx.cx().const_usize(self.0)]);
46         let ptr = bx.load(gep, ptr_align);
47         bx.nonnull_metadata(ptr);
48         // Vtable loads are invariant
49         bx.set_invariant_load(ptr);
50         ptr
51     }
52
53     pub fn get_usize<Bx: BuilderMethods<'a, 'tcx>>(
54         self,
55         bx: &mut Bx,
56         llvtable: Bx::Value
57     ) -> Bx::Value {
58         // Load the data pointer from the object.
59         debug!("get_int({:?}, {:?})", llvtable, self);
60
61         let llvtable = bx.pointercast(llvtable, bx.cx().type_ptr_to(bx.cx().type_isize()));
62         let usize_align = bx.tcx().data_layout.pointer_align;
63         let gep = bx.inbounds_gep(llvtable, &[bx.cx().const_usize(self.0)]);
64         let ptr = bx.load(gep, usize_align);
65         // Vtable loads are invariant
66         bx.set_invariant_load(ptr);
67         ptr
68     }
69 }
70
71 /// Creates a dynamic vtable for the given type and vtable origin.
72 /// This is used only for objects.
73 ///
74 /// The vtables are cached instead of created on every call.
75 ///
76 /// The `trait_ref` encodes the erased self type. Hence if we are
77 /// making an object `Foo<Trait>` from a value of type `Foo<T>`, then
78 /// `trait_ref` would map `T:Trait`.
79 pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
80     cx: &Cx,
81     ty: Ty<'tcx>,
82     trait_ref: ty::PolyExistentialTraitRef<'tcx>,
83 ) -> Cx::Value {
84     let tcx = cx.tcx();
85
86     debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
87
88     // Check the cache.
89     if let Some(&val) = cx.vtables().borrow().get(&(ty, trait_ref)) {
90         return val;
91     }
92
93     // Not in the cache. Build it.
94     let nullptr = cx.const_null(cx.type_i8p());
95
96     let methods = tcx.vtable_methods(trait_ref.with_self_ty(tcx, ty));
97     let methods = methods.iter().cloned().map(|opt_mth| {
98         opt_mth.map_or(nullptr, |(def_id, substs)| {
99             callee::resolve_and_get_fn_for_vtable(cx, def_id, substs)
100         })
101     });
102
103     let (size, align) = cx.layout_of(ty).size_and_align();
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(monomorphize::resolve_drop_in_place(cx.tcx(), ty)),
110         cx.const_usize(size.bytes()),
111         cx.const_usize(align.abi())
112     ].iter().cloned().chain(methods).collect();
113
114     let vtable_const = cx.const_struct(&components, false);
115     let align = cx.data_layout().pointer_align;
116     let vtable = cx.static_addr_of(vtable_const, align, Some("vtable"));
117
118     cx.create_vtable_metadata(ty, vtable);
119
120     cx.vtables().borrow_mut().insert((ty, trait_ref), vtable);
121     vtable
122 }