]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/meth.rs
Fixes doc important trait display on mobile
[rust.git] / src / librustc_trans / 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 llvm::ValueRef;
12 use abi::FnType;
13 use callee;
14 use common::*;
15 use builder::Builder;
16 use consts;
17 use monomorphize;
18 use type_::Type;
19 use value::Value;
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, bcx: &Builder<'a, 'tcx>,
37                   llvtable: ValueRef,
38                   fn_ty: &FnType<'tcx>) -> ValueRef {
39         // Load the data pointer from the object.
40         debug!("get_fn({:?}, {:?})", Value(llvtable), self);
41
42         let llvtable = bcx.pointercast(llvtable, fn_ty.llvm_type(bcx.ccx).ptr_to().ptr_to());
43         let ptr = bcx.load(bcx.inbounds_gep(llvtable, &[C_usize(bcx.ccx, self.0)]), None);
44         bcx.nonnull_metadata(ptr);
45         // Vtable loads are invariant
46         bcx.set_invariant_load(ptr);
47         ptr
48     }
49
50     pub fn get_usize(self, bcx: &Builder<'a, 'tcx>, llvtable: ValueRef) -> ValueRef {
51         // Load the data pointer from the object.
52         debug!("get_int({:?}, {:?})", Value(llvtable), self);
53
54         let llvtable = bcx.pointercast(llvtable, Type::isize(bcx.ccx).ptr_to());
55         let ptr = bcx.load(bcx.inbounds_gep(llvtable, &[C_usize(bcx.ccx, self.0)]), None);
56         // Vtable loads are invariant
57         bcx.set_invariant_load(ptr);
58         ptr
59     }
60 }
61
62 /// Creates a dynamic vtable for the given type and vtable origin.
63 /// This is used only for objects.
64 ///
65 /// The vtables are cached instead of created on every call.
66 ///
67 /// The `trait_ref` encodes the erased self type. Hence if we are
68 /// making an object `Foo<Trait>` from a value of type `Foo<T>`, then
69 /// `trait_ref` would map `T:Trait`.
70 pub fn get_vtable<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
71                             ty: Ty<'tcx>,
72                             trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>)
73                             -> ValueRef
74 {
75     let tcx = ccx.tcx();
76
77     debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
78
79     // Check the cache.
80     if let Some(&val) = ccx.vtables().borrow().get(&(ty, trait_ref)) {
81         return val;
82     }
83
84     // Not in the cache. Build it.
85     let nullptr = C_null(Type::i8p(ccx));
86
87     let (size, align) = ccx.size_and_align_of(ty);
88     let mut components: Vec<_> = [
89         callee::get_fn(ccx, monomorphize::resolve_drop_in_place(ccx.tcx(), ty)),
90         C_usize(ccx, size.bytes()),
91         C_usize(ccx, align.abi())
92     ].iter().cloned().collect();
93
94     if let Some(trait_ref) = trait_ref {
95         let trait_ref = trait_ref.with_self_ty(tcx, ty);
96         let methods = tcx.vtable_methods(trait_ref);
97         let methods = methods.iter().cloned().map(|opt_mth| {
98             opt_mth.map_or(nullptr, |(def_id, substs)| {
99                 callee::resolve_and_get_fn(ccx, def_id, substs)
100             })
101         });
102         components.extend(methods);
103     }
104
105     let vtable_const = C_struct(ccx, &components, false);
106     let align = ccx.data_layout().pointer_align;
107     let vtable = consts::addr_of(ccx, vtable_const, align, "vtable");
108
109     debuginfo::create_vtable_metadata(ccx, ty, vtable);
110
111     ccx.vtables().borrow_mut().insert((ty, trait_ref), vtable);
112     vtable
113 }