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);
80 ty::TyAdt(def, substs) => {
82 // First get the size of all statically known fields.
83 // Don't use size_of because it also rounds up to alignment, which we
84 // want to avoid, as the unsized field's alignment could be smaller.
85 assert!(!t.is_simd());
86 let layout = ccx.layout_of(t);
87 debug!("DST {} layout: {:?}", t, layout);
89 let (sized_size, sized_align) = match *layout {
90 ty::layout::Layout::Univariant { ref variant, .. } => {
91 (variant.offsets.last().map_or(0, |o| o.bytes()), variant.align.abi())
94 bug!("size_and_align_of_dst: expcted Univariant for `{}`, found {:#?}",
98 debug!("DST {} statically sized prefix size: {} align: {}",
99 t, sized_size, sized_align);
100 let sized_size = C_uint(ccx, sized_size);
101 let sized_align = C_uint(ccx, sized_align);
103 // Recurse to get the size of the dynamically sized field (must be
105 let last_field = def.struct_variant().fields.last().unwrap();
106 let field_ty = monomorphize::field_ty(bcx.tcx(), substs, last_field);
107 let (unsized_size, unsized_align) = size_and_align_of_dst(bcx, field_ty, info);
109 // FIXME (#26403, #27023): We should be adding padding
110 // to `sized_size` (to accommodate the `unsized_align`
111 // required of the unsized field that follows) before
112 // summing it with `sized_size`. (Note that since #26403
113 // is unfixed, we do not yet add the necessary padding
114 // here. But this is where the add would go.)
116 // Return the sum of sizes and max of aligns.
117 let size = bcx.add(sized_size, unsized_size);
119 // Choose max of two known alignments (combined value must
120 // be aligned according to more restrictive of the two).
121 let align = match (const_to_opt_u128(sized_align, false),
122 const_to_opt_u128(unsized_align, false)) {
123 (Some(sized_align), Some(unsized_align)) => {
124 // If both alignments are constant, (the sized_align should always be), then
125 // pick the correct alignment statically.
126 C_uint(ccx, std::cmp::max(sized_align, unsized_align) as u64)
128 _ => bcx.select(bcx.icmp(llvm::IntUGT, sized_align, unsized_align),
133 // Issue #27023: must add any necessary padding to `size`
134 // (to make it a multiple of `align`) before returning it.
136 // Namely, the returned size should be, in C notation:
138 // `size + ((size & (align-1)) ? align : 0)`
140 // emulated via the semi-standard fast bit trick:
142 // `(size + (align-1)) & -align`
144 let addend = bcx.sub(align, C_uint(bcx.ccx, 1_u64));
145 let size = bcx.and(bcx.add(size, addend), bcx.neg(align));
149 ty::TyDynamic(..) => {
150 // load size/align from vtable
151 (meth::SIZE.get_usize(bcx, info), meth::ALIGN.get_usize(bcx, info))
153 ty::TySlice(_) | ty::TyStr => {
154 let unit = t.sequence_element_type(bcx.tcx());
155 // The info in this case is the length of the str, so the size is that
156 // times the unit size.
157 (bcx.mul(info, C_uint(bcx.ccx, bcx.ccx.size_of(unit))),
158 C_uint(bcx.ccx, bcx.ccx.align_of(unit)))
160 _ => bug!("Unexpected unsized type, found {}", t)