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 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, bx: &Builder<'a, 'll, 'tcx>,
38 fn_ty: &FnType<'tcx, Ty<'tcx>>) -> &'ll Value {
39 // Load the data pointer from the object.
40 debug!("get_fn({:?}, {:?})", llvtable, self);
42 let llvtable = bx.pointercast(llvtable, fn_ty.llvm_type(bx.cx).ptr_to().ptr_to());
43 let ptr_align = bx.tcx().data_layout.pointer_align;
44 let ptr = bx.load(bx.inbounds_gep(llvtable, &[C_usize(bx.cx, self.0)]), ptr_align);
45 bx.nonnull_metadata(ptr);
46 // Vtable loads are invariant
47 bx.set_invariant_load(ptr);
51 pub fn get_usize(self, bx: &Builder<'a, 'll, 'tcx>, llvtable: &'ll Value) -> &'ll Value {
52 // Load the data pointer from the object.
53 debug!("get_int({:?}, {:?})", llvtable, self);
55 let llvtable = bx.pointercast(llvtable, Type::isize(bx.cx).ptr_to());
56 let usize_align = bx.tcx().data_layout.pointer_align;
57 let ptr = bx.load(bx.inbounds_gep(llvtable, &[C_usize(bx.cx, self.0)]), usize_align);
58 // Vtable loads are invariant
59 bx.set_invariant_load(ptr);
64 /// Creates a dynamic vtable for the given type and vtable origin.
65 /// This is used only for objects.
67 /// The vtables are cached instead of created on every call.
69 /// The `trait_ref` encodes the erased self type. Hence if we are
70 /// making an object `Foo<Trait>` from a value of type `Foo<T>`, then
71 /// `trait_ref` would map `T:Trait`.
73 cx: &CodegenCx<'ll, 'tcx>,
75 trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
79 debug!("get_vtable(ty={:?}, trait_ref={:?})", ty, trait_ref);
82 if let Some(&val) = cx.vtables.borrow().get(&(ty, trait_ref)) {
86 // Not in the cache. Build it.
87 let nullptr = C_null(Type::i8p(cx));
89 let (size, align) = cx.size_and_align_of(ty);
90 let mut components: Vec<_> = [
91 callee::get_fn(cx, monomorphize::resolve_drop_in_place(cx.tcx, ty)),
92 C_usize(cx, size.bytes()),
93 C_usize(cx, align.abi())
94 ].iter().cloned().collect();
96 if let Some(trait_ref) = trait_ref {
97 let trait_ref = trait_ref.with_self_ty(tcx, ty);
98 let methods = tcx.vtable_methods(trait_ref);
99 let methods = methods.iter().cloned().map(|opt_mth| {
100 opt_mth.map_or(nullptr, |(def_id, substs)| {
101 callee::resolve_and_get_fn(cx, def_id, substs)
104 components.extend(methods);
107 let vtable_const = C_struct(cx, &components, false);
108 let align = cx.data_layout().pointer_align;
109 let vtable = consts::addr_of(cx, vtable_const, align, Some("vtable"));
111 debuginfo::create_vtable_metadata(cx, ty, vtable);
113 cx.vtables.borrow_mut().insert((ty, trait_ref), vtable);