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.
11 use abi::{FnType, FnTypeExt};
20 use traits::BuilderMethods;
22 use rustc::ty::{self, Ty};
23 use rustc::ty::layout::HasDataLayout;
26 #[derive(Copy, Clone, Debug)]
27 pub struct VirtualIndex(u64);
29 pub const DESTRUCTOR: VirtualIndex = VirtualIndex(0);
30 pub const SIZE: VirtualIndex = VirtualIndex(1);
31 pub const ALIGN: VirtualIndex = VirtualIndex(2);
33 impl<'a, 'tcx> VirtualIndex {
34 pub fn from_index(index: usize) -> Self {
35 VirtualIndex(index as u64 + 3)
38 pub fn get_fn(self, bx: &Builder<'a, 'll, 'tcx>,
40 fn_ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Value {
41 // Load the data pointer from the object.
42 debug!("get_fn({:?}, {:?})", llvtable, self);
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);
55 bx: &Builder<'a, 'll, 'tcx, &'ll Value>,
58 // Load the data pointer from the object.
59 debug!("get_int({:?}, {:?})", llvtable, self);
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);
70 /// Creates a dynamic vtable for the given type and vtable origin.
71 /// This is used only for objects.
73 /// The vtables are cached instead of created on every call.
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`.
79 cx: &CodegenCx<'ll, 'tcx>,
81 trait_ref: ty::PolyExistentialTraitRef<'tcx>,
85 debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
88 if let Some(&val) = cx.vtables.borrow().get(&(ty, trait_ref)) {
92 // Not in the cache. Build it.
93 let nullptr = C_null(Type::i8p(cx));
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)
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();
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"));
117 debuginfo::create_vtable_metadata(cx, ty, vtable);
119 cx.vtables.borrow_mut().insert((ty, trait_ref), vtable);