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};
13 use context::CodegenCx;
19 use interfaces::{BuilderMethods, CommonMethods, TypeMethods};
21 use rustc::ty::{self, Ty};
22 use rustc::ty::layout::HasDataLayout;
25 #[derive(Copy, Clone, Debug)]
26 pub struct VirtualIndex(u64);
28 pub const DESTRUCTOR: VirtualIndex = VirtualIndex(0);
29 pub const SIZE: VirtualIndex = VirtualIndex(1);
30 pub const ALIGN: VirtualIndex = VirtualIndex(2);
32 impl<'a, 'tcx> VirtualIndex {
33 pub fn from_index(index: usize) -> Self {
34 VirtualIndex(index as u64 + 3)
37 pub fn get_fn(self, bx: &Builder<'a, 'll, 'tcx>,
39 fn_ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Value {
40 // Load the data pointer from the object.
41 debug!("get_fn({:?}, {:?})", llvtable, self);
43 let llvtable = bx.pointercast(
45 bx.cx().type_ptr_to(fn_ty.ptr_to_llvm_type(bx.cx()))
47 let ptr_align = bx.tcx().data_layout.pointer_align;
49 bx.inbounds_gep(llvtable, &[bx.cx().const_usize(self.0)]),
52 bx.nonnull_metadata(ptr);
53 // Vtable loads are invariant
54 bx.set_invariant_load(ptr);
60 bx: &Builder<'a, 'll, 'tcx>,
63 // Load the data pointer from the object.
64 debug!("get_int({:?}, {:?})", llvtable, self);
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;
69 bx.inbounds_gep(llvtable, &[bx.cx().const_usize(self.0)]),
72 // Vtable loads are invariant
73 bx.set_invariant_load(ptr);
78 /// Creates a dynamic vtable for the given type and vtable origin.
79 /// This is used only for objects.
81 /// The vtables are cached instead of created on every call.
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`.
87 cx: &CodegenCx<'ll, 'tcx>,
89 trait_ref: ty::PolyExistentialTraitRef<'tcx>,
93 debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
96 if let Some(&val) = cx.vtables.borrow().get(&(ty, trait_ref)) {
100 // Not in the cache. Build it.
101 let nullptr = cx.const_null(cx.type_i8p());
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)
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();
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"));
125 debuginfo::create_vtable_metadata(cx, ty, vtable);
127 cx.vtables.borrow_mut().insert((ty, trait_ref), vtable);