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.
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.
20 use rustc::ty::{self, Ty};
21 use rustc::ty::layout::HasDataLayout;
24 #[derive(Copy, Clone, Debug)]
25 pub struct VirtualIndex(u64);
27 pub const DESTRUCTOR: VirtualIndex = VirtualIndex(0);
28 pub const SIZE: VirtualIndex = VirtualIndex(1);
29 pub const ALIGN: VirtualIndex = VirtualIndex(2);
31 impl<'a, 'tcx> VirtualIndex {
32 pub fn from_index(index: usize) -> Self {
33 VirtualIndex(index as u64 + 3)
36 pub fn get_fn(self, bcx: &Builder<'a, 'tcx>,
38 fn_ty: &FnType<'tcx>) -> ValueRef {
39 // Load the data pointer from the object.
40 debug!("get_fn({:?}, {:?})", Value(llvtable), self);
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);
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);
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);
62 /// Creates a dynamic vtable for the given type and vtable origin.
63 /// This is used only for objects.
65 /// The vtables are cached instead of created on every call.
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>,
72 trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>)
77 debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
80 if let Some(&val) = ccx.vtables().borrow().get(&(ty, trait_ref)) {
84 // Not in the cache. Build it.
85 let nullptr = C_null(Type::i8p(ccx));
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();
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)
102 components.extend(methods);
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");
109 debuginfo::create_vtable_metadata(ccx, ty, vtable);
111 ccx.vtables().borrow_mut().insert((ty, trait_ref), vtable);