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