]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/meth.rs
Rollup merge of #53545 - FelixMcFelix:fix-50865-beta, r=petrochenkov
[rust.git] / src / librustc_codegen_llvm / 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 abi::{FnType, FnTypeExt};
12 use callee;
13 use common::*;
14 use builder::Builder;
15 use consts;
16 use monomorphize;
17 use type_::Type;
18 use value::Value;
19
20 use rustc::ty::{self, Ty};
21 use rustc::ty::layout::HasDataLayout;
22 use debuginfo;
23
24 #[derive(Copy, Clone, Debug)]
25 pub struct VirtualIndex(u64);
26
27 pub const DESTRUCTOR: VirtualIndex = VirtualIndex(0);
28 pub const SIZE: VirtualIndex = VirtualIndex(1);
29 pub const ALIGN: VirtualIndex = VirtualIndex(2);
30
31 impl<'a, 'tcx> VirtualIndex {
32     pub fn from_index(index: usize) -> Self {
33         VirtualIndex(index as u64 + 3)
34     }
35
36     pub fn get_fn(self, bx: &Builder<'a, 'll, 'tcx>,
37                   llvtable: &'ll Value,
38                   fn_ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Value {
39         // Load the data pointer from the object.
40         debug!("get_fn({:?}, {:?})", llvtable, self);
41
42         let llvtable = bx.pointercast(llvtable, fn_ty.llvm_type(bx.cx).ptr_to().ptr_to());
43         let ptr_align = bx.tcx().data_layout.pointer_align;
44         let ptr = bx.load(bx.inbounds_gep(llvtable, &[C_usize(bx.cx, self.0)]), ptr_align);
45         bx.nonnull_metadata(ptr);
46         // Vtable loads are invariant
47         bx.set_invariant_load(ptr);
48         ptr
49     }
50
51     pub fn get_usize(self, bx: &Builder<'a, 'll, 'tcx>, llvtable: &'ll Value) -> &'ll Value {
52         // Load the data pointer from the object.
53         debug!("get_int({:?}, {:?})", llvtable, self);
54
55         let llvtable = bx.pointercast(llvtable, Type::isize(bx.cx).ptr_to());
56         let usize_align = bx.tcx().data_layout.pointer_align;
57         let ptr = bx.load(bx.inbounds_gep(llvtable, &[C_usize(bx.cx, self.0)]), usize_align);
58         // Vtable loads are invariant
59         bx.set_invariant_load(ptr);
60         ptr
61     }
62 }
63
64 /// Creates a dynamic vtable for the given type and vtable origin.
65 /// This is used only for objects.
66 ///
67 /// The vtables are cached instead of created on every call.
68 ///
69 /// The `trait_ref` encodes the erased self type. Hence if we are
70 /// making an object `Foo<Trait>` from a value of type `Foo<T>`, then
71 /// `trait_ref` would map `T:Trait`.
72 pub fn get_vtable(
73     cx: &CodegenCx<'ll, 'tcx>,
74     ty: Ty<'tcx>,
75     trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
76 ) -> &'ll Value {
77     let tcx = cx.tcx;
78
79     debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
80
81     // Check the cache.
82     if let Some(&val) = cx.vtables.borrow().get(&(ty, trait_ref)) {
83         return val;
84     }
85
86     // Not in the cache. Build it.
87     let nullptr = C_null(Type::i8p(cx));
88
89     let (size, align) = cx.size_and_align_of(ty);
90     let mut components: Vec<_> = [
91         callee::get_fn(cx, monomorphize::resolve_drop_in_place(cx.tcx, ty)),
92         C_usize(cx, size.bytes()),
93         C_usize(cx, align.abi())
94     ].iter().cloned().collect();
95
96     if let Some(trait_ref) = trait_ref {
97         let trait_ref = trait_ref.with_self_ty(tcx, ty);
98         let methods = tcx.vtable_methods(trait_ref);
99         let methods = methods.iter().cloned().map(|opt_mth| {
100             opt_mth.map_or(nullptr, |(def_id, substs)| {
101                 callee::resolve_and_get_fn(cx, def_id, substs)
102             })
103         });
104         components.extend(methods);
105     }
106
107     let vtable_const = C_struct(cx, &components, false);
108     let align = cx.data_layout().pointer_align;
109     let vtable = consts::addr_of(cx, vtable_const, align, Some("vtable"));
110
111     debuginfo::create_vtable_metadata(cx, ty, vtable);
112
113     cx.vtables.borrow_mut().insert((ty, trait_ref), vtable);
114     vtable
115 }