]> git.lizzy.rs Git - rust.git/blob - src/librustc_target/abi/mod.rs
346e5667a7babbd3613d320c51e5009c62cbb77f
[rust.git] / src / librustc_target / abi / mod.rs
1 // Copyright 2017 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 pub use self::Integer::*;
12 pub use self::Primitive::*;
13
14 use spec::Target;
15
16 use std::cmp;
17 use std::ops::{Add, Deref, Sub, Mul, AddAssign, Range, RangeInclusive};
18
19 pub mod call;
20
21 /// Parsed [Data layout](http://llvm.org/docs/LangRef.html#data-layout)
22 /// for a target, which contains everything needed to compute layouts.
23 pub struct TargetDataLayout {
24     pub endian: Endian,
25     pub i1_align: Align,
26     pub i8_align: Align,
27     pub i16_align: Align,
28     pub i32_align: Align,
29     pub i64_align: Align,
30     pub i128_align: Align,
31     pub f32_align: Align,
32     pub f64_align: Align,
33     pub pointer_size: Size,
34     pub pointer_align: Align,
35     pub aggregate_align: Align,
36
37     /// Alignments for vector types.
38     pub vector_align: Vec<(Size, Align)>
39 }
40
41 impl Default for TargetDataLayout {
42     /// Creates an instance of `TargetDataLayout`.
43     fn default() -> TargetDataLayout {
44         TargetDataLayout {
45             endian: Endian::Big,
46             i1_align: Align::from_bits(8, 8).unwrap(),
47             i8_align: Align::from_bits(8, 8).unwrap(),
48             i16_align: Align::from_bits(16, 16).unwrap(),
49             i32_align: Align::from_bits(32, 32).unwrap(),
50             i64_align: Align::from_bits(32, 64).unwrap(),
51             i128_align: Align::from_bits(32, 64).unwrap(),
52             f32_align: Align::from_bits(32, 32).unwrap(),
53             f64_align: Align::from_bits(64, 64).unwrap(),
54             pointer_size: Size::from_bits(64),
55             pointer_align: Align::from_bits(64, 64).unwrap(),
56             aggregate_align: Align::from_bits(0, 64).unwrap(),
57             vector_align: vec![
58                 (Size::from_bits(64), Align::from_bits(64, 64).unwrap()),
59                 (Size::from_bits(128), Align::from_bits(128, 128).unwrap())
60             ]
61         }
62     }
63 }
64
65 impl TargetDataLayout {
66     pub fn parse(target: &Target) -> Result<TargetDataLayout, String> {
67         // Parse a bit count from a string.
68         let parse_bits = |s: &str, kind: &str, cause: &str| {
69             s.parse::<u64>().map_err(|err| {
70                 format!("invalid {} `{}` for `{}` in \"data-layout\": {}",
71                         kind, s, cause, err)
72             })
73         };
74
75         // Parse a size string.
76         let size = |s: &str, cause: &str| {
77             parse_bits(s, "size", cause).map(Size::from_bits)
78         };
79
80         // Parse an alignment string.
81         let align = |s: &[&str], cause: &str| {
82             if s.is_empty() {
83                 return Err(format!("missing alignment for `{}` in \"data-layout\"", cause));
84             }
85             let abi = parse_bits(s[0], "alignment", cause)?;
86             let pref = s.get(1).map_or(Ok(abi), |pref| parse_bits(pref, "alignment", cause))?;
87             Align::from_bits(abi, pref).map_err(|err| {
88                 format!("invalid alignment for `{}` in \"data-layout\": {}",
89                         cause, err)
90             })
91         };
92
93         let mut dl = TargetDataLayout::default();
94         let mut i128_align_src = 64;
95         for spec in target.data_layout.split("-") {
96             match &spec.split(":").collect::<Vec<_>>()[..] {
97                 &["e"] => dl.endian = Endian::Little,
98                 &["E"] => dl.endian = Endian::Big,
99                 &["a", ref a..] => dl.aggregate_align = align(a, "a")?,
100                 &["f32", ref a..] => dl.f32_align = align(a, "f32")?,
101                 &["f64", ref a..] => dl.f64_align = align(a, "f64")?,
102                 &[p @ "p", s, ref a..] | &[p @ "p0", s, ref a..] => {
103                     dl.pointer_size = size(s, p)?;
104                     dl.pointer_align = align(a, p)?;
105                 }
106                 &[s, ref a..] if s.starts_with("i") => {
107                     let bits = match s[1..].parse::<u64>() {
108                         Ok(bits) => bits,
109                         Err(_) => {
110                             size(&s[1..], "i")?; // For the user error.
111                             continue;
112                         }
113                     };
114                     let a = align(a, s)?;
115                     match bits {
116                         1 => dl.i1_align = a,
117                         8 => dl.i8_align = a,
118                         16 => dl.i16_align = a,
119                         32 => dl.i32_align = a,
120                         64 => dl.i64_align = a,
121                         _ => {}
122                     }
123                     if bits >= i128_align_src && bits <= 128 {
124                         // Default alignment for i128 is decided by taking the alignment of
125                         // largest-sized i{64...128}.
126                         i128_align_src = bits;
127                         dl.i128_align = a;
128                     }
129                 }
130                 &[s, ref a..] if s.starts_with("v") => {
131                     let v_size = size(&s[1..], "v")?;
132                     let a = align(a, s)?;
133                     if let Some(v) = dl.vector_align.iter_mut().find(|v| v.0 == v_size) {
134                         v.1 = a;
135                         continue;
136                     }
137                     // No existing entry, add a new one.
138                     dl.vector_align.push((v_size, a));
139                 }
140                 _ => {} // Ignore everything else.
141             }
142         }
143
144         // Perform consistency checks against the Target information.
145         let endian_str = match dl.endian {
146             Endian::Little => "little",
147             Endian::Big => "big"
148         };
149         if endian_str != target.target_endian {
150             return Err(format!("inconsistent target specification: \"data-layout\" claims \
151                                 architecture is {}-endian, while \"target-endian\" is `{}`",
152                                endian_str, target.target_endian));
153         }
154
155         if dl.pointer_size.bits().to_string() != target.target_pointer_width {
156             return Err(format!("inconsistent target specification: \"data-layout\" claims \
157                                 pointers are {}-bit, while \"target-pointer-width\" is `{}`",
158                                dl.pointer_size.bits(), target.target_pointer_width));
159         }
160
161         Ok(dl)
162     }
163
164     /// Return exclusive upper bound on object size.
165     ///
166     /// The theoretical maximum object size is defined as the maximum positive `isize` value.
167     /// This ensures that the `offset` semantics remain well-defined by allowing it to correctly
168     /// index every address within an object along with one byte past the end, along with allowing
169     /// `isize` to store the difference between any two pointers into an object.
170     ///
171     /// The upper bound on 64-bit currently needs to be lower because LLVM uses a 64-bit integer
172     /// to represent object size in bits. It would need to be 1 << 61 to account for this, but is
173     /// currently conservatively bounded to 1 << 47 as that is enough to cover the current usable
174     /// address space on 64-bit ARMv8 and x86_64.
175     pub fn obj_size_bound(&self) -> u64 {
176         match self.pointer_size.bits() {
177             16 => 1 << 15,
178             32 => 1 << 31,
179             64 => 1 << 47,
180             bits => panic!("obj_size_bound: unknown pointer bit size {}", bits)
181         }
182     }
183
184     pub fn ptr_sized_integer(&self) -> Integer {
185         match self.pointer_size.bits() {
186             16 => I16,
187             32 => I32,
188             64 => I64,
189             bits => panic!("ptr_sized_integer: unknown pointer bit size {}", bits)
190         }
191     }
192
193     pub fn vector_align(&self, vec_size: Size) -> Align {
194         for &(size, align) in &self.vector_align {
195             if size == vec_size {
196                 return align;
197             }
198         }
199         // Default to natural alignment, which is what LLVM does.
200         // That is, use the size, rounded up to a power of 2.
201         let align = vec_size.bytes().next_power_of_two();
202         Align::from_bytes(align, align).unwrap()
203     }
204 }
205
206 pub trait HasDataLayout: Copy {
207     fn data_layout(&self) -> &TargetDataLayout;
208 }
209
210 impl<'a> HasDataLayout for &'a TargetDataLayout {
211     fn data_layout(&self) -> &TargetDataLayout {
212         self
213     }
214 }
215
216 /// Endianness of the target, which must match cfg(target-endian).
217 #[derive(Copy, Clone)]
218 pub enum Endian {
219     Little,
220     Big
221 }
222
223 /// Size of a type in bytes.
224 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
225 pub struct Size {
226     raw: u64
227 }
228
229 impl Size {
230     pub fn from_bits(bits: u64) -> Size {
231         // Avoid potential overflow from `bits + 7`.
232         Size::from_bytes(bits / 8 + ((bits % 8) + 7) / 8)
233     }
234
235     pub fn from_bytes(bytes: u64) -> Size {
236         if bytes >= (1 << 61) {
237             panic!("Size::from_bytes: {} bytes in bits doesn't fit in u64", bytes)
238         }
239         Size {
240             raw: bytes
241         }
242     }
243
244     pub fn bytes(self) -> u64 {
245         self.raw
246     }
247
248     pub fn bits(self) -> u64 {
249         self.bytes() * 8
250     }
251
252     pub fn abi_align(self, align: Align) -> Size {
253         let mask = align.abi() - 1;
254         Size::from_bytes((self.bytes() + mask) & !mask)
255     }
256
257     pub fn is_abi_aligned(self, align: Align) -> bool {
258         let mask = align.abi() - 1;
259         self.bytes() & mask == 0
260     }
261
262     pub fn checked_add<C: HasDataLayout>(self, offset: Size, cx: C) -> Option<Size> {
263         let dl = cx.data_layout();
264
265         // Each Size is less than dl.obj_size_bound(), so the sum is
266         // also less than 1 << 62 (and therefore can't overflow).
267         let bytes = self.bytes() + offset.bytes();
268
269         if bytes < dl.obj_size_bound() {
270             Some(Size::from_bytes(bytes))
271         } else {
272             None
273         }
274     }
275
276     pub fn checked_mul<C: HasDataLayout>(self, count: u64, cx: C) -> Option<Size> {
277         let dl = cx.data_layout();
278
279         match self.bytes().checked_mul(count) {
280             Some(bytes) if bytes < dl.obj_size_bound() => {
281                 Some(Size::from_bytes(bytes))
282             }
283             _ => None
284         }
285     }
286 }
287
288 // Panicking addition, subtraction and multiplication for convenience.
289 // Avoid during layout computation, return `LayoutError` instead.
290
291 impl Add for Size {
292     type Output = Size;
293     fn add(self, other: Size) -> Size {
294         // Each Size is less than 1 << 61, so the sum is
295         // less than 1 << 62 (and therefore can't overflow).
296         Size::from_bytes(self.bytes() + other.bytes())
297     }
298 }
299
300 impl Sub for Size {
301     type Output = Size;
302     fn sub(self, other: Size) -> Size {
303         // Each Size is less than 1 << 61, so an underflow
304         // would result in a value larger than 1 << 61,
305         // which Size::from_bytes will catch for us.
306         Size::from_bytes(self.bytes() - other.bytes())
307     }
308 }
309
310 impl Mul<u64> for Size {
311     type Output = Size;
312     fn mul(self, count: u64) -> Size {
313         match self.bytes().checked_mul(count) {
314             Some(bytes) => Size::from_bytes(bytes),
315             None => {
316                 panic!("Size::mul: {} * {} doesn't fit in u64", self.bytes(), count)
317             }
318         }
319     }
320 }
321
322 impl AddAssign for Size {
323     fn add_assign(&mut self, other: Size) {
324         *self = *self + other;
325     }
326 }
327
328 /// Alignment of a type in bytes, both ABI-mandated and preferred.
329 /// Each field is a power of two, giving the alignment a maximum value of
330 /// 2<sup>(2<sup>8</sup> - 1)</sup>, which is limited by LLVM to a i32,
331 /// with a maximum capacity of 2<sup>31</sup> - 1 or 2147483647.
332 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
333 pub struct Align {
334     abi_pow2: u8,
335     pref_pow2: u8,
336 }
337
338 impl Align {
339     pub fn from_bits(abi: u64, pref: u64) -> Result<Align, String> {
340         Align::from_bytes(Size::from_bits(abi).bytes(),
341                           Size::from_bits(pref).bytes())
342     }
343
344     pub fn from_bytes(abi: u64, pref: u64) -> Result<Align, String> {
345         let log2 = |align: u64| {
346             // Treat an alignment of 0 bytes like 1-byte alignment.
347             if align == 0 {
348                 return Ok(0);
349             }
350
351             let mut bytes = align;
352             let mut pow: u8 = 0;
353             while (bytes & 1) == 0 {
354                 pow += 1;
355                 bytes >>= 1;
356             }
357             if bytes != 1 {
358                 Err(format!("`{}` is not a power of 2", align))
359             } else if pow > 30 {
360                 Err(format!("`{}` is too large", align))
361             } else {
362                 Ok(pow)
363             }
364         };
365
366         Ok(Align {
367             abi_pow2: log2(abi)?,
368             pref_pow2: log2(pref)?,
369         })
370     }
371
372     pub fn abi(self) -> u64 {
373         1 << self.abi_pow2
374     }
375
376     pub fn pref(self) -> u64 {
377         1 << self.pref_pow2
378     }
379
380     pub fn abi_bits(self) -> u64 {
381         self.abi() * 8
382     }
383
384     pub fn pref_bits(self) -> u64 {
385         self.pref() * 8
386     }
387
388     pub fn min(self, other: Align) -> Align {
389         Align {
390             abi_pow2: cmp::min(self.abi_pow2, other.abi_pow2),
391             pref_pow2: cmp::min(self.pref_pow2, other.pref_pow2),
392         }
393     }
394
395     pub fn max(self, other: Align) -> Align {
396         Align {
397             abi_pow2: cmp::max(self.abi_pow2, other.abi_pow2),
398             pref_pow2: cmp::max(self.pref_pow2, other.pref_pow2),
399         }
400     }
401 }
402
403 /// Integers, also used for enum discriminants.
404 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
405 pub enum Integer {
406     I8,
407     I16,
408     I32,
409     I64,
410     I128,
411 }
412
413 impl Integer {
414     pub fn size(&self) -> Size {
415         match *self {
416             I8 => Size::from_bytes(1),
417             I16 => Size::from_bytes(2),
418             I32 => Size::from_bytes(4),
419             I64  => Size::from_bytes(8),
420             I128  => Size::from_bytes(16),
421         }
422     }
423
424     pub fn align<C: HasDataLayout>(&self, cx: C) -> Align {
425         let dl = cx.data_layout();
426
427         match *self {
428             I8 => dl.i8_align,
429             I16 => dl.i16_align,
430             I32 => dl.i32_align,
431             I64 => dl.i64_align,
432             I128 => dl.i128_align,
433         }
434     }
435
436     /// Find the smallest Integer type which can represent the signed value.
437     pub fn fit_signed(x: i128) -> Integer {
438         match x {
439             -0x0000_0000_0000_0080...0x0000_0000_0000_007f => I8,
440             -0x0000_0000_0000_8000...0x0000_0000_0000_7fff => I16,
441             -0x0000_0000_8000_0000...0x0000_0000_7fff_ffff => I32,
442             -0x8000_0000_0000_0000...0x7fff_ffff_ffff_ffff => I64,
443             _ => I128
444         }
445     }
446
447     /// Find the smallest Integer type which can represent the unsigned value.
448     pub fn fit_unsigned(x: u128) -> Integer {
449         match x {
450             0...0x0000_0000_0000_00ff => I8,
451             0...0x0000_0000_0000_ffff => I16,
452             0...0x0000_0000_ffff_ffff => I32,
453             0...0xffff_ffff_ffff_ffff => I64,
454             _ => I128,
455         }
456     }
457
458     /// Find the smallest integer with the given alignment.
459     pub fn for_abi_align<C: HasDataLayout>(cx: C, align: Align) -> Option<Integer> {
460         let dl = cx.data_layout();
461
462         let wanted = align.abi();
463         for &candidate in &[I8, I16, I32, I64, I128] {
464             if wanted == candidate.align(dl).abi() && wanted == candidate.size().bytes() {
465                 return Some(candidate);
466             }
467         }
468         None
469     }
470
471     /// Find the largest integer with the given alignment or less.
472     pub fn approximate_abi_align<C: HasDataLayout>(cx: C, align: Align) -> Integer {
473         let dl = cx.data_layout();
474
475         let wanted = align.abi();
476         // FIXME(eddyb) maybe include I128 in the future, when it works everywhere.
477         for &candidate in &[I64, I32, I16] {
478             if wanted >= candidate.align(dl).abi() && wanted >= candidate.size().bytes() {
479                 return candidate;
480             }
481         }
482         I8
483     }
484 }
485
486 /// Fundamental unit of memory access and layout.
487 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
488 pub enum Primitive {
489     /// The `bool` is the signedness of the `Integer` type.
490     ///
491     /// One would think we would not care about such details this low down,
492     /// but some ABIs are described in terms of C types and ISAs where the
493     /// integer arithmetic is done on {sign,zero}-extended registers, e.g.
494     /// a negative integer passed by zero-extension will appear positive in
495     /// the callee, and most operations on it will produce the wrong values.
496     Int(Integer, bool),
497     F32,
498     F64,
499     Pointer
500 }
501
502 impl<'a, 'tcx> Primitive {
503     pub fn size<C: HasDataLayout>(self, cx: C) -> Size {
504         let dl = cx.data_layout();
505
506         match self {
507             Int(i, _) => i.size(),
508             F32 => Size::from_bits(32),
509             F64 => Size::from_bits(64),
510             Pointer => dl.pointer_size
511         }
512     }
513
514     pub fn align<C: HasDataLayout>(self, cx: C) -> Align {
515         let dl = cx.data_layout();
516
517         match self {
518             Int(i, _) => i.align(dl),
519             F32 => dl.f32_align,
520             F64 => dl.f64_align,
521             Pointer => dl.pointer_align
522         }
523     }
524 }
525
526 /// Information about one scalar component of a Rust type.
527 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
528 pub struct Scalar {
529     pub value: Primitive,
530
531     /// Inclusive wrap-around range of valid values, that is, if
532     /// min > max, it represents min..=u128::MAX followed by 0..=max.
533     // FIXME(eddyb) always use the shortest range, e.g. by finding
534     // the largest space between two consecutive valid values and
535     // taking everything else as the (shortest) valid range.
536     pub valid_range: RangeInclusive<u128>,
537 }
538
539 impl Scalar {
540     pub fn is_bool(&self) -> bool {
541         if let Int(I8, _) = self.value {
542             self.valid_range == (0..=1)
543         } else {
544             false
545         }
546     }
547
548     /// Returns the valid range as a `x..y` range.
549     ///
550     /// If `x` and `y` are equal, the range is full, not empty.
551     pub fn valid_range_exclusive<C: HasDataLayout>(&self, cx: C) -> Range<u128> {
552         // For a (max) value of -1, max will be `-1 as usize`, which overflows.
553         // However, that is fine here (it would still represent the full range),
554         // i.e., if the range is everything.
555         let bits = self.value.size(cx).bits();
556         assert!(bits <= 128);
557         let mask = !0u128 >> (128 - bits);
558         let start = self.valid_range.start;
559         let end = self.valid_range.end;
560         assert_eq!(start, start & mask);
561         assert_eq!(end, end & mask);
562         start..(end.wrapping_add(1) & mask)
563     }
564 }
565
566 /// Describes how the fields of a type are located in memory.
567 #[derive(PartialEq, Eq, Hash, Debug)]
568 pub enum FieldPlacement {
569     /// All fields start at no offset. The `usize` is the field count.
570     Union(usize),
571
572     /// Array/vector-like placement, with all fields of identical types.
573     Array {
574         stride: Size,
575         count: u64
576     },
577
578     /// Struct-like placement, with precomputed offsets.
579     ///
580     /// Fields are guaranteed to not overlap, but note that gaps
581     /// before, between and after all the fields are NOT always
582     /// padding, and as such their contents may not be discarded.
583     /// For example, enum variants leave a gap at the start,
584     /// where the discriminant field in the enum layout goes.
585     Arbitrary {
586         /// Offsets for the first byte of each field,
587         /// ordered to match the source definition order.
588         /// This vector does not go in increasing order.
589         // FIXME(eddyb) use small vector optimization for the common case.
590         offsets: Vec<Size>,
591
592         /// Maps source order field indices to memory order indices,
593         /// depending how fields were permuted.
594         // FIXME(camlorn) also consider small vector  optimization here.
595         memory_index: Vec<u32>
596     }
597 }
598
599 impl FieldPlacement {
600     pub fn count(&self) -> usize {
601         match *self {
602             FieldPlacement::Union(count) => count,
603             FieldPlacement::Array { count, .. } => {
604                 let usize_count = count as usize;
605                 assert_eq!(usize_count as u64, count);
606                 usize_count
607             }
608             FieldPlacement::Arbitrary { ref offsets, .. } => offsets.len()
609         }
610     }
611
612     pub fn offset(&self, i: usize) -> Size {
613         match *self {
614             FieldPlacement::Union(_) => Size::from_bytes(0),
615             FieldPlacement::Array { stride, count } => {
616                 let i = i as u64;
617                 assert!(i < count);
618                 stride * i
619             }
620             FieldPlacement::Arbitrary { ref offsets, .. } => offsets[i]
621         }
622     }
623
624     pub fn memory_index(&self, i: usize) -> usize {
625         match *self {
626             FieldPlacement::Union(_) |
627             FieldPlacement::Array { .. } => i,
628             FieldPlacement::Arbitrary { ref memory_index, .. } => {
629                 let r = memory_index[i];
630                 assert_eq!(r as usize as u32, r);
631                 r as usize
632             }
633         }
634     }
635
636     /// Get source indices of the fields by increasing offsets.
637     #[inline]
638     pub fn index_by_increasing_offset<'a>(&'a self) -> impl Iterator<Item=usize>+'a {
639         let mut inverse_small = [0u8; 64];
640         let mut inverse_big = vec![];
641         let use_small = self.count() <= inverse_small.len();
642
643         // We have to write this logic twice in order to keep the array small.
644         if let FieldPlacement::Arbitrary { ref memory_index, .. } = *self {
645             if use_small {
646                 for i in 0..self.count() {
647                     inverse_small[memory_index[i] as usize] = i as u8;
648                 }
649             } else {
650                 inverse_big = vec![0; self.count()];
651                 for i in 0..self.count() {
652                     inverse_big[memory_index[i] as usize] = i as u32;
653                 }
654             }
655         }
656
657         (0..self.count()).map(move |i| {
658             match *self {
659                 FieldPlacement::Union(_) |
660                 FieldPlacement::Array { .. } => i,
661                 FieldPlacement::Arbitrary { .. } => {
662                     if use_small { inverse_small[i] as usize }
663                     else { inverse_big[i] as usize }
664                 }
665             }
666         })
667     }
668 }
669
670 /// Describes how values of the type are passed by target ABIs,
671 /// in terms of categories of C types there are ABI rules for.
672 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
673 pub enum Abi {
674     Uninhabited,
675     Scalar(Scalar),
676     ScalarPair(Scalar, Scalar),
677     Vector {
678         element: Scalar,
679         count: u64
680     },
681     Aggregate {
682         /// If true, the size is exact, otherwise it's only a lower bound.
683         sized: bool,
684     }
685 }
686
687 impl Abi {
688     /// Returns true if the layout corresponds to an unsized type.
689     pub fn is_unsized(&self) -> bool {
690         match *self {
691             Abi::Uninhabited |
692             Abi::Scalar(_) |
693             Abi::ScalarPair(..) |
694             Abi::Vector { .. } => false,
695             Abi::Aggregate { sized } => !sized
696         }
697     }
698
699     /// Returns true if this is a single signed integer scalar
700     pub fn is_signed(&self) -> bool {
701         match *self {
702             Abi::Scalar(ref scal) => match scal.value {
703                 Primitive::Int(_, signed) => signed,
704                 _ => false,
705             },
706             _ => false,
707         }
708     }
709 }
710
711 #[derive(PartialEq, Eq, Hash, Debug)]
712 pub enum Variants {
713     /// Single enum variants, structs/tuples, unions, and all non-ADTs.
714     Single {
715         index: usize
716     },
717
718     /// General-case enums: for each case there is a struct, and they all have
719     /// all space reserved for the discriminant, and their first field starts
720     /// at a non-0 offset, after where the discriminant would go.
721     Tagged {
722         discr: Scalar,
723         variants: Vec<LayoutDetails>,
724     },
725
726     /// Multiple cases distinguished by a niche (values invalid for a type):
727     /// the variant `dataful_variant` contains a niche at an arbitrary
728     /// offset (field 0 of the enum), which for a variant with discriminant
729     /// `d` is set to `(d - niche_variants.start).wrapping_add(niche_start)`.
730     ///
731     /// For example, `Option<(usize, &T)>`  is represented such that
732     /// `None` has a null pointer for the second tuple field, and
733     /// `Some` is the identity function (with a non-null reference).
734     NicheFilling {
735         dataful_variant: usize,
736         niche_variants: RangeInclusive<usize>,
737         niche: Scalar,
738         niche_start: u128,
739         variants: Vec<LayoutDetails>,
740     }
741 }
742
743 #[derive(PartialEq, Eq, Hash, Debug)]
744 pub struct LayoutDetails {
745     pub variants: Variants,
746     pub fields: FieldPlacement,
747     pub abi: Abi,
748     pub align: Align,
749     pub size: Size
750 }
751
752 impl LayoutDetails {
753     pub fn scalar<C: HasDataLayout>(cx: C, scalar: Scalar) -> Self {
754         let size = scalar.value.size(cx);
755         let align = scalar.value.align(cx);
756         LayoutDetails {
757             variants: Variants::Single { index: 0 },
758             fields: FieldPlacement::Union(0),
759             abi: Abi::Scalar(scalar),
760             size,
761             align,
762         }
763     }
764
765     pub fn uninhabited(field_count: usize) -> Self {
766         let align = Align::from_bytes(1, 1).unwrap();
767         LayoutDetails {
768             variants: Variants::Single { index: 0 },
769             fields: FieldPlacement::Union(field_count),
770             abi: Abi::Uninhabited,
771             align,
772             size: Size::from_bytes(0)
773         }
774     }
775 }
776
777 /// The details of the layout of a type, alongside the type itself.
778 /// Provides various type traversal APIs (e.g. recursing into fields).
779 ///
780 /// Note that the details are NOT guaranteed to always be identical
781 /// to those obtained from `layout_of(ty)`, as we need to produce
782 /// layouts for which Rust types do not exist, such as enum variants
783 /// or synthetic fields of enums (i.e. discriminants) and fat pointers.
784 #[derive(Copy, Clone, Debug)]
785 pub struct TyLayout<'a, Ty> {
786     pub ty: Ty,
787     pub details: &'a LayoutDetails
788 }
789
790 impl<'a, Ty> Deref for TyLayout<'a, Ty> {
791     type Target = &'a LayoutDetails;
792     fn deref(&self) -> &&'a LayoutDetails {
793         &self.details
794     }
795 }
796
797 pub trait LayoutOf {
798     type Ty;
799     type TyLayout;
800
801     fn layout_of(self, ty: Self::Ty) -> Self::TyLayout;
802 }
803
804 pub trait TyLayoutMethods<'a, C: LayoutOf<Ty = Self>>: Sized {
805     fn for_variant(this: TyLayout<'a, Self>, cx: C, variant_index: usize) -> TyLayout<'a, Self>;
806     fn field(this: TyLayout<'a, Self>, cx: C, i: usize) -> C::TyLayout;
807 }
808
809 impl<'a, Ty> TyLayout<'a, Ty> {
810     pub fn for_variant<C>(self, cx: C, variant_index: usize) -> Self
811     where Ty: TyLayoutMethods<'a, C>, C: LayoutOf<Ty = Ty> {
812         Ty::for_variant(self, cx, variant_index)
813     }
814     pub fn field<C>(self, cx: C, i: usize) -> C::TyLayout
815     where Ty: TyLayoutMethods<'a, C>, C: LayoutOf<Ty = Ty> {
816         Ty::field(self, cx, i)
817     }
818 }
819
820 impl<'a, Ty> TyLayout<'a, Ty> {
821     /// Returns true if the layout corresponds to an unsized type.
822     pub fn is_unsized(&self) -> bool {
823         self.abi.is_unsized()
824     }
825
826     /// Returns true if the type is a ZST and not unsized.
827     pub fn is_zst(&self) -> bool {
828         match self.abi {
829             Abi::Uninhabited => true,
830             Abi::Scalar(_) |
831             Abi::ScalarPair(..) |
832             Abi::Vector { .. } => false,
833             Abi::Aggregate { sized } => sized && self.size.bytes() == 0
834         }
835     }
836
837     pub fn size_and_align(&self) -> (Size, Align) {
838         (self.size, self.align)
839     }
840 }