]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/meth.rs
rustdoc: pretty-print Unevaluated expressions in types.
[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 rustc::traits;
13 use callee;
14 use common::*;
15 use builder::Builder;
16 use consts;
17 use machine;
18 use monomorphize;
19 use type_::Type;
20 use value::Value;
21 use rustc::ty;
22
23 #[derive(Copy, Clone, Debug)]
24 pub struct VirtualIndex(usize);
25
26 pub const DESTRUCTOR: VirtualIndex = VirtualIndex(0);
27 pub const SIZE: VirtualIndex = VirtualIndex(1);
28 pub const ALIGN: VirtualIndex = VirtualIndex(2);
29
30 impl<'a, 'tcx> VirtualIndex {
31     pub fn from_index(index: usize) -> Self {
32         VirtualIndex(index + 3)
33     }
34
35     pub fn get_fn(self, bcx: &Builder<'a, 'tcx>, llvtable: ValueRef) -> ValueRef {
36         // Load the data pointer from the object.
37         debug!("get_fn({:?}, {:?})", Value(llvtable), self);
38
39         let ptr = bcx.load_nonnull(bcx.gepi(llvtable, &[self.0]), None);
40         // Vtable loads are invariant
41         bcx.set_invariant_load(ptr);
42         ptr
43     }
44
45     pub fn get_usize(self, bcx: &Builder<'a, 'tcx>, llvtable: ValueRef) -> ValueRef {
46         // Load the data pointer from the object.
47         debug!("get_int({:?}, {:?})", Value(llvtable), self);
48
49         let llvtable = bcx.pointercast(llvtable, Type::isize(bcx.ccx).ptr_to());
50         let ptr = bcx.load(bcx.gepi(llvtable, &[self.0]), None);
51         // Vtable loads are invariant
52         bcx.set_invariant_load(ptr);
53         ptr
54     }
55 }
56
57 /// Creates a dynamic vtable for the given type and vtable origin.
58 /// This is used only for objects.
59 ///
60 /// The vtables are cached instead of created on every call.
61 ///
62 /// The `trait_ref` encodes the erased self type. Hence if we are
63 /// making an object `Foo<Trait>` from a value of type `Foo<T>`, then
64 /// `trait_ref` would map `T:Trait`.
65 pub fn get_vtable<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
66                             ty: ty::Ty<'tcx>,
67                             trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>)
68                             -> ValueRef
69 {
70     let tcx = ccx.tcx();
71
72     debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
73
74     // Check the cache.
75     if let Some(&val) = ccx.vtables().borrow().get(&(ty, trait_ref)) {
76         return val;
77     }
78
79     // Not in the cache. Build it.
80     let nullptr = C_null(Type::nil(ccx).ptr_to());
81
82     let mut components: Vec<_> = [
83         callee::get_fn(ccx, monomorphize::resolve_drop_in_place(ccx.shared(), ty)),
84         C_usize(ccx, ccx.size_of(ty)),
85         C_usize(ccx, ccx.align_of(ty) as u64)
86     ].iter().cloned().collect();
87
88     if let Some(trait_ref) = trait_ref {
89         let trait_ref = trait_ref.with_self_ty(tcx, ty);
90         let methods = traits::get_vtable_methods(tcx, trait_ref).map(|opt_mth| {
91             opt_mth.map_or(nullptr, |(def_id, substs)| {
92                 callee::resolve_and_get_fn(ccx, def_id, substs)
93             })
94         });
95         components.extend(methods);
96     }
97
98     let vtable_const = C_struct(ccx, &components, false);
99     let align = machine::llalign_of_pref(ccx, val_ty(vtable_const));
100     let vtable = consts::addr_of(ccx, vtable_const, align, "vtable");
101
102     ccx.vtables().borrow_mut().insert((ty, trait_ref), vtable);
103     vtable
104 }