1 // Copyright 2012-2013 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.
13 // Code relating to drop glue.
19 use rustc::ty::{self, Ty};
20 use rustc::ty::layout::LayoutTyper;
27 pub fn size_and_align_of_dst<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, t: Ty<'tcx>, info: ValueRef)
28 -> (ValueRef, ValueRef) {
29 debug!("calculate size of DST: {}; with lost info: {:?}",
31 if bcx.ccx.shared().type_is_sized(t) {
32 let size = bcx.ccx.size_of(t);
33 let align = bcx.ccx.align_of(t);
34 debug!("size_and_align_of_dst t={} info={:?} size: {} align: {}",
35 t, Value(info), size, align);
36 let size = C_usize(bcx.ccx, size);
37 let align = C_usize(bcx.ccx, align as u64);
40 assert!(!info.is_null());
42 ty::TyAdt(..) | ty::TyTuple(..) => {
44 // First get the size of all statically known fields.
45 // Don't use size_of because it also rounds up to alignment, which we
46 // want to avoid, as the unsized field's alignment could be smaller.
47 assert!(!t.is_simd());
48 let layout = ccx.layout_of(t);
49 debug!("DST {} layout: {:?}", t, layout);
51 let (sized_size, sized_align) = match *layout {
52 ty::layout::Layout::Univariant { ref variant, .. } => {
53 (variant.offsets.last().map_or(0, |o| o.bytes()), variant.align.abi())
56 bug!("size_and_align_of_dst: expcted Univariant for `{}`, found {:#?}",
60 debug!("DST {} statically sized prefix size: {} align: {}",
61 t, sized_size, sized_align);
62 let sized_size = C_usize(ccx, sized_size);
63 let sized_align = C_usize(ccx, sized_align);
65 // Recurse to get the size of the dynamically sized field (must be
67 let field_ty = match t.sty {
68 ty::TyAdt(def, substs) => {
69 let last_field = def.struct_variant().fields.last().unwrap();
70 monomorphize::field_ty(bcx.tcx(), substs, last_field)
72 ty::TyTuple(tys, _) => tys.last().unwrap(),
75 let (unsized_size, unsized_align) = size_and_align_of_dst(bcx, field_ty, info);
77 // FIXME (#26403, #27023): We should be adding padding
78 // to `sized_size` (to accommodate the `unsized_align`
79 // required of the unsized field that follows) before
80 // summing it with `sized_size`. (Note that since #26403
81 // is unfixed, we do not yet add the necessary padding
82 // here. But this is where the add would go.)
84 // Return the sum of sizes and max of aligns.
85 let size = bcx.add(sized_size, unsized_size);
87 // Choose max of two known alignments (combined value must
88 // be aligned according to more restrictive of the two).
89 let align = match (const_to_opt_u128(sized_align, false),
90 const_to_opt_u128(unsized_align, false)) {
91 (Some(sized_align), Some(unsized_align)) => {
92 // If both alignments are constant, (the sized_align should always be), then
93 // pick the correct alignment statically.
94 C_usize(ccx, std::cmp::max(sized_align, unsized_align) as u64)
96 _ => bcx.select(bcx.icmp(llvm::IntUGT, sized_align, unsized_align),
101 // Issue #27023: must add any necessary padding to `size`
102 // (to make it a multiple of `align`) before returning it.
104 // Namely, the returned size should be, in C notation:
106 // `size + ((size & (align-1)) ? align : 0)`
108 // emulated via the semi-standard fast bit trick:
110 // `(size + (align-1)) & -align`
112 let addend = bcx.sub(align, C_usize(bcx.ccx, 1));
113 let size = bcx.and(bcx.add(size, addend), bcx.neg(align));
117 ty::TyDynamic(..) => {
118 // load size/align from vtable
119 (meth::SIZE.get_usize(bcx, info), meth::ALIGN.get_usize(bcx, info))
121 ty::TySlice(_) | ty::TyStr => {
122 let unit = t.sequence_element_type(bcx.tcx());
123 // The info in this case is the length of the str, so the size is that
124 // times the unit size.
125 (bcx.mul(info, C_usize(bcx.ccx, bcx.ccx.size_of(unit))),
126 C_usize(bcx.ccx, bcx.ccx.align_of(unit) as u64))
128 _ => bug!("Unexpected unsized type, found {}", t)