Skip to content

Commit a380053

Browse files
committed
Add helper methods inherent_align and to_union on Abi.
1 parent 23d09ae commit a380053

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

compiler/rustc_abi/src/lib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![cfg_attr(feature = "nightly", feature(step_trait, rustc_attrs, min_specialization))]
22

3-
use std::fmt;
43
#[cfg(feature = "nightly")]
54
use std::iter::Step;
65
use std::num::{NonZeroUsize, ParseIntError};
76
use std::ops::{Add, AddAssign, Mul, RangeInclusive, Sub};
87
use std::str::FromStr;
8+
use std::{cmp, fmt};
99

1010
use bitflags::bitflags;
1111
use rustc_data_structures::intern::Interned;
@@ -1272,6 +1272,31 @@ impl Abi {
12721272
pub fn is_scalar(&self) -> bool {
12731273
matches!(*self, Abi::Scalar(_))
12741274
}
1275+
1276+
/// Returns the fixed alignment of this ABI, if any
1277+
pub fn inherent_align<C: HasDataLayout>(&self, cx: &C) -> Option<AbiAndPrefAlign> {
1278+
Some(match *self {
1279+
Abi::Scalar(s) => s.align(cx),
1280+
Abi::ScalarPair(s1, s2) => {
1281+
AbiAndPrefAlign::new(cmp::max(s1.align(cx).abi, s2.align(cx).abi))
1282+
}
1283+
Abi::Vector { element, count } => {
1284+
cx.data_layout().vector_align(element.size(cx) * count)
1285+
}
1286+
Abi::Uninhabited | Abi::Aggregate { .. } => return None,
1287+
})
1288+
}
1289+
1290+
/// Discard valid range information and allow undef
1291+
pub fn to_union(&self) -> Self {
1292+
assert!(self.is_sized());
1293+
match *self {
1294+
Abi::Scalar(s) => Abi::Scalar(s.to_union()),
1295+
Abi::ScalarPair(s1, s2) => Abi::ScalarPair(s1.to_union(), s2.to_union()),
1296+
Abi::Vector { element, count } => Abi::Vector { element: element.to_union(), count },
1297+
Abi::Uninhabited | Abi::Aggregate { .. } => Abi::Aggregate { sized: true },
1298+
}
1299+
}
12751300
}
12761301

12771302
#[derive(PartialEq, Eq, Hash, Clone, Debug)]

0 commit comments

Comments
 (0)