]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/glue.rs
move projection mode into parameter environment
[rust.git] / src / librustc_trans / glue.rs
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.
4 //
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.
10
11 //!
12 //
13 // Code relating to drop glue.
14
15 use std;
16
17 use llvm;
18 use llvm::{ValueRef};
19 use rustc::traits;
20 use rustc::ty::{self, Ty, TypeFoldable};
21 use rustc::ty::layout::LayoutTyper;
22 use common::*;
23 use meth;
24 use monomorphize;
25 use value::Value;
26 use builder::Builder;
27
28 pub fn needs_drop_glue<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>, t: Ty<'tcx>) -> bool {
29     assert!(t.is_normalized_for_trans());
30
31     let t = scx.tcx().erase_regions(&t);
32
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.
36     //
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.)
41
42     if !scx.type_needs_drop(t) {
43         return false;
44     }
45     match t.sty {
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.
53                         false
54                     } else {
55                         true
56                     }
57                 })
58             } else {
59                 true
60             }
61         }
62         _ => true
63     }
64 }
65
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: {:?}",
69            t, Value(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);
77         return (size, align);
78     }
79     assert!(!info.is_null());
80     match t.sty {
81         ty::TyAdt(def, substs) => {
82             let ccx = bcx.ccx;
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);
89
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())
93                 }
94                 _ => {
95                     bug!("size_and_align_of_dst: expcted Univariant for `{}`, found {:#?}",
96                          t, layout);
97                 }
98             };
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);
103
104             // Recurse to get the size of the dynamically sized field (must be
105             // the last field).
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);
109
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.)
116
117             // Return the sum of sizes and max of aligns.
118             let size = bcx.add(sized_size, unsized_size);
119
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)
128                 }
129                 _ => bcx.select(bcx.icmp(llvm::IntUGT, sized_align, unsized_align),
130                                 sized_align,
131                                 unsized_align)
132             };
133
134             // Issue #27023: must add any necessary padding to `size`
135             // (to make it a multiple of `align`) before returning it.
136             //
137             // Namely, the returned size should be, in C notation:
138             //
139             //   `size + ((size & (align-1)) ? align : 0)`
140             //
141             // emulated via the semi-standard fast bit trick:
142             //
143             //   `(size + (align-1)) & -align`
144
145             let addend = bcx.sub(align, C_uint(bcx.ccx, 1_u64));
146             let size = bcx.and(bcx.add(size, addend), bcx.neg(align));
147
148             (size, align)
149         }
150         ty::TyDynamic(..) => {
151             // load size/align from vtable
152             (meth::SIZE.get_usize(bcx, info), meth::ALIGN.get_usize(bcx, info))
153         }
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)))
160         }
161         _ => bug!("Unexpected unsized type, found {}", t)
162     }
163 }