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