]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/vtable.rs
Refactor vtable format.
[rust.git] / compiler / rustc_middle / src / ty / vtable.rs
1 use std::convert::TryFrom;
2
3 use crate::mir::interpret::{alloc_range, AllocId, Allocation, Pointer, Scalar, ScalarMaybeUninit};
4 use crate::ty::fold::TypeFoldable;
5 use crate::ty::{self, DefId, PolyExistentialTraitRef, SubstsRef, Ty, TyCtxt};
6 use rustc_ast::Mutability;
7
8 #[derive(Clone, Copy, Debug, PartialEq, HashStable)]
9 pub enum VtblEntry<'tcx> {
10     MetadataDropInPlace,
11     MetadataSize,
12     MetadataAlign,
13     Vacant,
14     Method(DefId, SubstsRef<'tcx>),
15     TraitVPtr(PolyExistentialTraitRef<'tcx>),
16 }
17
18 pub const COMMON_VTABLE_ENTRIES: &[VtblEntry<'_>] =
19     &[VtblEntry::MetadataDropInPlace, VtblEntry::MetadataSize, VtblEntry::MetadataAlign];
20
21 pub const COMMON_VTABLE_ENTRIES_DROPINPLACE: usize = 0;
22 pub const COMMON_VTABLE_ENTRIES_SIZE: usize = 1;
23 pub const COMMON_VTABLE_ENTRIES_ALIGN: usize = 2;
24
25 impl<'tcx> TyCtxt<'tcx> {
26     /// Retrieves an allocation that represents the contents of a vtable.
27     /// There's a cache within `TyCtxt` so it will be deduplicated.
28     pub fn vtable_allocation(
29         self,
30         ty: Ty<'tcx>,
31         poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
32     ) -> AllocId {
33         let tcx = self;
34         let vtables_cache = tcx.vtables_cache.lock();
35         if let Some(alloc_id) = vtables_cache.get(&(ty, poly_trait_ref)).cloned() {
36             return alloc_id;
37         }
38         drop(vtables_cache);
39
40         // See https://github.com/rust-lang/rust/pull/86475#discussion_r655162674
41         assert!(
42             !ty.needs_subst() && !poly_trait_ref.map_or(false, |trait_ref| trait_ref.needs_subst())
43         );
44         let param_env = ty::ParamEnv::reveal_all();
45         let vtable_entries = if let Some(poly_trait_ref) = poly_trait_ref {
46             let trait_ref = poly_trait_ref.with_self_ty(tcx, ty);
47             let trait_ref = tcx.erase_regions(trait_ref);
48
49             tcx.vtable_entries(trait_ref)
50         } else {
51             COMMON_VTABLE_ENTRIES
52         };
53
54         let layout =
55             tcx.layout_of(param_env.and(ty)).expect("failed to build vtable representation");
56         assert!(!layout.is_unsized(), "can't create a vtable for an unsized type");
57         let size = layout.size.bytes();
58         let align = layout.align.abi.bytes();
59
60         let ptr_size = tcx.data_layout.pointer_size;
61         let ptr_align = tcx.data_layout.pointer_align.abi;
62
63         let vtable_size = ptr_size * u64::try_from(vtable_entries.len()).unwrap();
64         let mut vtable =
65             Allocation::uninit(vtable_size, ptr_align, /* panic_on_fail */ true).unwrap();
66
67         // No need to do any alignment checks on the memory accesses below, because we know the
68         // allocation is correctly aligned as we created it above. Also we're only offsetting by
69         // multiples of `ptr_align`, which means that it will stay aligned to `ptr_align`.
70
71         for (idx, entry) in vtable_entries.iter().enumerate() {
72             let idx: u64 = u64::try_from(idx).unwrap();
73             let scalar = match entry {
74                 VtblEntry::MetadataDropInPlace => {
75                     let instance = ty::Instance::resolve_drop_in_place(tcx, ty);
76                     let fn_alloc_id = tcx.create_fn_alloc(instance);
77                     let fn_ptr = Pointer::from(fn_alloc_id);
78                     ScalarMaybeUninit::from_pointer(fn_ptr, &tcx)
79                 }
80                 VtblEntry::MetadataSize => Scalar::from_uint(size, ptr_size).into(),
81                 VtblEntry::MetadataAlign => Scalar::from_uint(align, ptr_size).into(),
82                 VtblEntry::Vacant => continue,
83                 VtblEntry::Method(def_id, substs) => {
84                     // See https://github.com/rust-lang/rust/pull/86475#discussion_r655162674
85                     assert!(!substs.needs_subst());
86
87                     // Prepare the fn ptr we write into the vtable.
88                     let instance =
89                         ty::Instance::resolve_for_vtable(tcx, param_env, *def_id, substs)
90                             .expect("resolution failed during building vtable representation")
91                             .polymorphize(tcx);
92                     let fn_alloc_id = tcx.create_fn_alloc(instance);
93                     let fn_ptr = Pointer::from(fn_alloc_id);
94                     ScalarMaybeUninit::from_pointer(fn_ptr, &tcx)
95                 }
96                 VtblEntry::TraitVPtr(trait_ref) => {
97                     let supertrait_alloc_id = self.vtable_allocation(ty, Some(*trait_ref));
98                     let vptr = Pointer::from(supertrait_alloc_id);
99                     ScalarMaybeUninit::from_pointer(vptr, &tcx)
100                 }
101             };
102             vtable
103                 .write_scalar(&tcx, alloc_range(ptr_size * idx, ptr_size), scalar)
104                 .expect("failed to build vtable representation");
105         }
106
107         vtable.mutability = Mutability::Not;
108         let alloc_id = tcx.create_memory_alloc(tcx.intern_const_alloc(vtable));
109         let mut vtables_cache = self.vtables_cache.lock();
110         vtables_cache.insert((ty, poly_trait_ref), alloc_id);
111         alloc_id
112     }
113 }