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;
20 use interfaces::{BuilderMethods, CommonMethods};
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;
47 bx.inbounds_gep(llvtable, &[CodegenCx::c_usize(bx.cx(), self.0)]),
50 bx.nonnull_metadata(ptr);
51 // Vtable loads are invariant
52 bx.set_invariant_load(ptr);
58 bx: &Builder<'a, 'll, 'tcx>,
61 // Load the data pointer from the object.
62 debug!("get_int({:?}, {:?})", llvtable, self);
64 let llvtable = bx.pointercast(llvtable, Type::isize(bx.cx()).ptr_to());
65 let usize_align = bx.tcx().data_layout.pointer_align;
67 bx.inbounds_gep(llvtable, &[CodegenCx::c_usize(bx.cx(), self.0)]),
70 // Vtable loads are invariant
71 bx.set_invariant_load(ptr);
76 /// Creates a dynamic vtable for the given type and vtable origin.
77 /// This is used only for objects.
79 /// The vtables are cached instead of created on every call.
81 /// The `trait_ref` encodes the erased self type. Hence if we are
82 /// making an object `Foo<Trait>` from a value of type `Foo<T>`, then
83 /// `trait_ref` would map `T:Trait`.
85 cx: &CodegenCx<'ll, 'tcx>,
87 trait_ref: ty::PolyExistentialTraitRef<'tcx>,
91 debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
94 if let Some(&val) = cx.vtables.borrow().get(&(ty, trait_ref)) {
98 // Not in the cache. Build it.
99 let nullptr = cx.c_null(Type::i8p(cx));
101 let methods = tcx.vtable_methods(trait_ref.with_self_ty(tcx, ty));
102 let methods = methods.iter().cloned().map(|opt_mth| {
103 opt_mth.map_or(nullptr, |(def_id, substs)| {
104 callee::resolve_and_get_fn_for_vtable(cx, def_id, substs)
108 let (size, align) = cx.size_and_align_of(ty);
109 // /////////////////////////////////////////////////////////////////////////////////////////////
110 // If you touch this code, be sure to also make the corresponding changes to
111 // `get_vtable` in rust_mir/interpret/traits.rs
112 // /////////////////////////////////////////////////////////////////////////////////////////////
113 let components: Vec<_> = [
114 callee::get_fn(cx, monomorphize::resolve_drop_in_place(cx.tcx, ty)),
115 CodegenCx::c_usize(cx, size.bytes()),
116 CodegenCx::c_usize(cx, align.abi())
117 ].iter().cloned().chain(methods).collect();
119 let vtable_const = CodegenCx::c_struct(cx, &components, false);
120 let align = cx.data_layout().pointer_align;
121 let vtable = consts::addr_of(cx, vtable_const, align, Some("vtable"));
123 debuginfo::create_vtable_metadata(cx, ty, vtable);
125 cx.vtables.borrow_mut().insert((ty, trait_ref), vtable);