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