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.
20 use rustc::ty::{self, Ty, TypeFoldable};
21 use rustc::ty::layout::LayoutTyper;
28 pub fn needs_drop_glue<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>, t: Ty<'tcx>) -> bool {
29 assert!(t.is_normalized_for_trans());
31 let t = scx.tcx().erase_regions(&t);
33 // FIXME (#22815): note that type_needs_drop conservatively
34 // approximates in some cases and may say a type expression
35 // requires drop glue when it actually does not.
37 // (In this case it is not clear whether any harm is done, i.e.
38 // erroneously returning `true` in some cases where we could have
39 // returned `false` does not appear unsound. The impact on
40 // code quality is unknown at this time.)
42 if !scx.type_needs_drop(t) {
46 ty::TyAdt(def, _) if def.is_box() => {
47 let typ = t.boxed_ty();
48 if !scx.type_needs_drop(typ) && scx.type_is_sized(typ) {
49 scx.tcx().infer_ctxt(traits::Reveal::All).enter(|infcx| {
50 let layout = t.layout(&infcx).unwrap();
51 if layout.size(scx).bytes() == 0 {
52 // `Box<ZeroSizeType>` does not allocate.
66 pub fn size_and_align_of_dst<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, t: Ty<'tcx>, info: ValueRef)
67 -> (ValueRef, ValueRef) {
68 debug!("calculate size of DST: {}; with lost info: {:?}",
70 if bcx.ccx.shared().type_is_sized(t) {
71 let size = bcx.ccx.size_of(t);
72 let align = bcx.ccx.align_of(t);
73 debug!("size_and_align_of_dst t={} info={:?} size: {} align: {}",
74 t, Value(info), size, align);
75 let size = C_uint(bcx.ccx, size);
76 let align = C_uint(bcx.ccx, align);
79 assert!(!info.is_null());
81 ty::TyAdt(def, substs) => {
83 // First get the size of all statically known fields.
84 // Don't use size_of because it also rounds up to alignment, which we
85 // want to avoid, as the unsized field's alignment could be smaller.
86 assert!(!t.is_simd());
87 let layout = ccx.layout_of(t);
88 debug!("DST {} layout: {:?}", t, layout);
90 let (sized_size, sized_align) = match *layout {
91 ty::layout::Layout::Univariant { ref variant, .. } => {
92 (variant.offsets.last().map_or(0, |o| o.bytes()), variant.align.abi())
95 bug!("size_and_align_of_dst: expcted Univariant for `{}`, found {:#?}",
99 debug!("DST {} statically sized prefix size: {} align: {}",
100 t, sized_size, sized_align);
101 let sized_size = C_uint(ccx, sized_size);
102 let sized_align = C_uint(ccx, sized_align);
104 // Recurse to get the size of the dynamically sized field (must be
106 let last_field = def.struct_variant().fields.last().unwrap();
107 let field_ty = monomorphize::field_ty(bcx.tcx(), substs, last_field);
108 let (unsized_size, unsized_align) = size_and_align_of_dst(bcx, field_ty, info);
110 // FIXME (#26403, #27023): We should be adding padding
111 // to `sized_size` (to accommodate the `unsized_align`
112 // required of the unsized field that follows) before
113 // summing it with `sized_size`. (Note that since #26403
114 // is unfixed, we do not yet add the necessary padding
115 // here. But this is where the add would go.)
117 // Return the sum of sizes and max of aligns.
118 let size = bcx.add(sized_size, unsized_size);
120 // Choose max of two known alignments (combined value must
121 // be aligned according to more restrictive of the two).
122 let align = match (const_to_opt_u128(sized_align, false),
123 const_to_opt_u128(unsized_align, false)) {
124 (Some(sized_align), Some(unsized_align)) => {
125 // If both alignments are constant, (the sized_align should always be), then
126 // pick the correct alignment statically.
127 C_uint(ccx, std::cmp::max(sized_align, unsized_align) as u64)
129 _ => bcx.select(bcx.icmp(llvm::IntUGT, sized_align, unsized_align),
134 // Issue #27023: must add any necessary padding to `size`
135 // (to make it a multiple of `align`) before returning it.
137 // Namely, the returned size should be, in C notation:
139 // `size + ((size & (align-1)) ? align : 0)`
141 // emulated via the semi-standard fast bit trick:
143 // `(size + (align-1)) & -align`
145 let addend = bcx.sub(align, C_uint(bcx.ccx, 1_u64));
146 let size = bcx.and(bcx.add(size, addend), bcx.neg(align));
150 ty::TyDynamic(..) => {
151 // load size/align from vtable
152 (meth::SIZE.get_usize(bcx, info), meth::ALIGN.get_usize(bcx, info))
154 ty::TySlice(_) | ty::TyStr => {
155 let unit = t.sequence_element_type(bcx.tcx());
156 // The info in this case is the length of the str, so the size is that
157 // times the unit size.
158 (bcx.mul(info, C_uint(bcx.ccx, bcx.ccx.size_of(unit))),
159 C_uint(bcx.ccx, bcx.ccx.align_of(unit)))
161 _ => bug!("Unexpected unsized type, found {}", t)