about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLuqman Aden <me@luqman.ca>2022-11-30 23:09:51 -0800
committerLuqman Aden <me@luqman.ca>2023-05-05 16:00:19 -0700
commita3800535b1c9213fa99d897d317bfcf0ba7bf426 (patch)
tree9616c064964b61927f513b6bd42c7251e46640c3
parent23d09aebc8c6b89ba86bce2c38a0fc31f227d722 (diff)
downloadrust-a3800535b1c9213fa99d897d317bfcf0ba7bf426.tar.gz
rust-a3800535b1c9213fa99d897d317bfcf0ba7bf426.zip
Add helper methods inherent_align and to_union on Abi.
-rw-r--r--compiler/rustc_abi/src/lib.rs27
1 files changed, 26 insertions, 1 deletions
diff --git a/compiler/rustc_abi/src/lib.rs b/compiler/rustc_abi/src/lib.rs
index d01a9b00304..bbbc417e892 100644
--- a/compiler/rustc_abi/src/lib.rs
+++ b/compiler/rustc_abi/src/lib.rs
@@ -1,11 +1,11 @@
 #![cfg_attr(feature = "nightly", feature(step_trait, rustc_attrs, min_specialization))]
 
-use std::fmt;
 #[cfg(feature = "nightly")]
 use std::iter::Step;
 use std::num::{NonZeroUsize, ParseIntError};
 use std::ops::{Add, AddAssign, Mul, RangeInclusive, Sub};
 use std::str::FromStr;
+use std::{cmp, fmt};
 
 use bitflags::bitflags;
 use rustc_data_structures::intern::Interned;
@@ -1272,6 +1272,31 @@ impl Abi {
     pub fn is_scalar(&self) -> bool {
         matches!(*self, Abi::Scalar(_))
     }
+
+    /// Returns the fixed alignment of this ABI, if any
+    pub fn inherent_align<C: HasDataLayout>(&self, cx: &C) -> Option<AbiAndPrefAlign> {
+        Some(match *self {
+            Abi::Scalar(s) => s.align(cx),
+            Abi::ScalarPair(s1, s2) => {
+                AbiAndPrefAlign::new(cmp::max(s1.align(cx).abi, s2.align(cx).abi))
+            }
+            Abi::Vector { element, count } => {
+                cx.data_layout().vector_align(element.size(cx) * count)
+            }
+            Abi::Uninhabited | Abi::Aggregate { .. } => return None,
+        })
+    }
+
+    /// Discard valid range information and allow undef
+    pub fn to_union(&self) -> Self {
+        assert!(self.is_sized());
+        match *self {
+            Abi::Scalar(s) => Abi::Scalar(s.to_union()),
+            Abi::ScalarPair(s1, s2) => Abi::ScalarPair(s1.to_union(), s2.to_union()),
+            Abi::Vector { element, count } => Abi::Vector { element: element.to_union(), count },
+            Abi::Uninhabited | Abi::Aggregate { .. } => Abi::Aggregate { sized: true },
+        }
+    }
 }
 
 #[derive(PartialEq, Eq, Hash, Clone, Debug)]