]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/meth.rs
Generalized base.rs#call_memcpy and everything that it uses
[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 traits::BuilderMethods;
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(bx.inbounds_gep(llvtable, &[C_usize(bx.cx, self.0)]), 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(
54         self,
55         bx: &Builder<'a, 'll, 'tcx, &'ll Value>,
56         llvtable: &'ll Value
57     ) -> &'ll Value {
58         // Load the data pointer from the object.
59         debug!("get_int({:?}, {:?})", llvtable, self);
60
61         let llvtable = bx.pointercast(llvtable, Type::isize(bx.cx).ptr_to());
62         let usize_align = bx.tcx().data_layout.pointer_align;
63         let ptr = bx.load(bx.inbounds_gep(llvtable, &[C_usize(bx.cx, self.0)]), usize_align);
64         // Vtable loads are invariant
65         bx.set_invariant_load(ptr);
66         ptr
67     }
68 }
69
70 /// Creates a dynamic vtable for the given type and vtable origin.
71 /// This is used only for objects.
72 ///
73 /// The vtables are cached instead of created on every call.
74 ///
75 /// The `trait_ref` encodes the erased self type. Hence if we are
76 /// making an object `Foo<Trait>` from a value of type `Foo<T>`, then
77 /// `trait_ref` would map `T:Trait`.
78 pub fn get_vtable(
79     cx: &CodegenCx<'ll, 'tcx>,
80     ty: Ty<'tcx>,
81     trait_ref: ty::PolyExistentialTraitRef<'tcx>,
82 ) -> &'ll Value {
83     let tcx = cx.tcx;
84
85     debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
86
87     // Check the cache.
88     if let Some(&val) = cx.vtables.borrow().get(&(ty, trait_ref)) {
89         return val;
90     }
91
92     // Not in the cache. Build it.
93     let nullptr = C_null(Type::i8p(cx));
94
95     let methods = tcx.vtable_methods(trait_ref.with_self_ty(tcx, ty));
96     let methods = methods.iter().cloned().map(|opt_mth| {
97         opt_mth.map_or(nullptr, |(def_id, substs)| {
98             callee::resolve_and_get_fn_for_vtable(cx, def_id, substs)
99         })
100     });
101
102     let (size, align) = cx.size_and_align_of(ty);
103     // /////////////////////////////////////////////////////////////////////////////////////////////
104     // If you touch this code, be sure to also make the corresponding changes to
105     // `get_vtable` in rust_mir/interpret/traits.rs
106     // /////////////////////////////////////////////////////////////////////////////////////////////
107     let components: Vec<_> = [
108         callee::get_fn(cx, monomorphize::resolve_drop_in_place(cx.tcx, ty)),
109         C_usize(cx, size.bytes()),
110         C_usize(cx, align.abi())
111     ].iter().cloned().chain(methods).collect();
112
113     let vtable_const = C_struct(cx, &components, false);
114     let align = cx.data_layout().pointer_align;
115     let vtable = consts::addr_of(cx, vtable_const, align, Some("vtable"));
116
117     debuginfo::create_vtable_metadata(cx, ty, vtable);
118
119     cx.vtables.borrow_mut().insert((ty, trait_ref), vtable);
120     vtable
121 }