|
| 1 | +use std::cmp::Ordering; |
1 | 2 | use std::fmt;
|
| 3 | +use std::hash::{Hash, Hasher}; |
| 4 | +use std::str::FromStr; |
2 | 5 |
|
3 | 6 | #[cfg(feature = "nightly")]
|
4 |
| -use rustc_macros::{Decodable, Encodable, HashStable_Generic}; |
| 7 | +use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd}; |
| 8 | +#[cfg(feature = "nightly")] |
| 9 | +use rustc_macros::{Decodable, Encodable}; |
5 | 10 |
|
6 | 11 | #[cfg(test)]
|
7 | 12 | mod tests;
|
8 | 13 |
|
9 | 14 | use ExternAbi as Abi;
|
10 | 15 |
|
11 |
| -#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug)] |
12 |
| -#[cfg_attr(feature = "nightly", derive(HashStable_Generic, Encodable, Decodable))] |
| 16 | +#[derive(Clone, Copy, Debug)] |
| 17 | +#[cfg_attr(feature = "nightly", derive(Encodable, Decodable))] |
13 | 18 | pub enum ExternAbi {
|
14 | 19 | // Some of the ABIs come first because every time we add a new ABI, we have to re-bless all the
|
15 | 20 | // hashing tests. These are used in many places, so giving them stable values reduces test
|
@@ -69,7 +74,123 @@ pub enum ExternAbi {
|
69 | 74 | RiscvInterruptS,
|
70 | 75 | }
|
71 | 76 |
|
72 |
| -impl Abi { |
| 77 | +macro_rules! abi_impls { |
| 78 | + ($e_name:ident = { |
| 79 | + $($variant:ident $({ unwind: $uw:literal })? =><= $tok:literal,)* |
| 80 | + }) => { |
| 81 | + impl $e_name { |
| 82 | + pub const ALL_VARIANTS: &[Self] = &[ |
| 83 | + $($e_name::$variant $({ unwind: $uw })*,)* |
| 84 | + ]; |
| 85 | + pub const fn as_str(&self) -> &'static str { |
| 86 | + match self { |
| 87 | + $($e_name::$variant $( { unwind: $uw } )* => $tok,)* |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + impl ::core::str::FromStr for $e_name { |
| 93 | + type Err = AbiFromStrErr; |
| 94 | + fn from_str(s: &str) -> Result<$e_name, Self::Err> { |
| 95 | + match s { |
| 96 | + $($tok => Ok($e_name::$variant $({ unwind: $uw })*),)* |
| 97 | + _ => Err(AbiFromStrErr::Unknown), |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +pub enum AbiFromStrErr { |
| 105 | + Unknown, |
| 106 | +} |
| 107 | + |
| 108 | +abi_impls! { |
| 109 | + ExternAbi = { |
| 110 | + C { unwind: false } =><= "C", |
| 111 | + CCmseNonSecureCall =><= "C-cmse-nonsecure-call", |
| 112 | + CCmseNonSecureEntry =><= "C-cmse-nonsecure-entry", |
| 113 | + C { unwind: true } =><= "C-unwind", |
| 114 | + Rust =><= "Rust", |
| 115 | + Aapcs { unwind: false } =><= "aapcs", |
| 116 | + Aapcs { unwind: true } =><= "aapcs-unwind", |
| 117 | + AvrInterrupt =><= "avr-interrupt", |
| 118 | + AvrNonBlockingInterrupt =><= "avr-non-blocking-interrupt", |
| 119 | + Cdecl { unwind: false } =><= "cdecl", |
| 120 | + Cdecl { unwind: true } =><= "cdecl-unwind", |
| 121 | + EfiApi =><= "efiapi", |
| 122 | + Fastcall { unwind: false } =><= "fastcall", |
| 123 | + Fastcall { unwind: true } =><= "fastcall-unwind", |
| 124 | + GpuKernel =><= "gpu-kernel", |
| 125 | + Msp430Interrupt =><= "msp430-interrupt", |
| 126 | + PtxKernel =><= "ptx-kernel", |
| 127 | + RiscvInterruptM =><= "riscv-interrupt-m", |
| 128 | + RiscvInterruptS =><= "riscv-interrupt-s", |
| 129 | + RustCall =><= "rust-call", |
| 130 | + RustCold =><= "rust-cold", |
| 131 | + RustIntrinsic =><= "rust-intrinsic", |
| 132 | + Stdcall { unwind: false } =><= "stdcall", |
| 133 | + Stdcall { unwind: true } =><= "stdcall-unwind", |
| 134 | + System { unwind: false } =><= "system", |
| 135 | + System { unwind: true } =><= "system-unwind", |
| 136 | + SysV64 { unwind: false } =><= "sysv64", |
| 137 | + SysV64 { unwind: true } =><= "sysv64-unwind", |
| 138 | + Thiscall { unwind: false } =><= "thiscall", |
| 139 | + Thiscall { unwind: true } =><= "thiscall-unwind", |
| 140 | + Unadjusted =><= "unadjusted", |
| 141 | + Vectorcall { unwind: false } =><= "vectorcall", |
| 142 | + Vectorcall { unwind: true } =><= "vectorcall-unwind", |
| 143 | + Win64 { unwind: false } =><= "win64", |
| 144 | + Win64 { unwind: true } =><= "win64-unwind", |
| 145 | + X86Interrupt =><= "x86-interrupt", |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +impl Ord for ExternAbi { |
| 150 | + fn cmp(&self, rhs: &Self) -> Ordering { |
| 151 | + self.as_str().cmp(rhs.as_str()) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +impl PartialOrd for ExternAbi { |
| 156 | + fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> { |
| 157 | + Some(self.cmp(rhs)) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +impl PartialEq for ExternAbi { |
| 162 | + fn eq(&self, rhs: &Self) -> bool { |
| 163 | + self.cmp(rhs) == Ordering::Equal |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +impl Eq for ExternAbi {} |
| 168 | + |
| 169 | +impl Hash for ExternAbi { |
| 170 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 171 | + self.as_str().hash(state); |
| 172 | + // double-assurance of a prefix breaker |
| 173 | + u32::from_be_bytes(*b"ABI\0").hash(state); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +#[cfg(feature = "nightly")] |
| 178 | +impl<C> HashStable<C> for ExternAbi { |
| 179 | + #[inline] |
| 180 | + fn hash_stable(&self, _: &mut C, hasher: &mut StableHasher) { |
| 181 | + Hash::hash(self, hasher); |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +#[cfg(feature = "nightly")] |
| 186 | +impl StableOrd for ExternAbi { |
| 187 | + const CAN_USE_UNSTABLE_SORT: bool = true; |
| 188 | + |
| 189 | + // because each ABI is hashed like a string, there is no possible instability |
| 190 | + const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = (); |
| 191 | +} |
| 192 | + |
| 193 | +impl ExternAbi { |
73 | 194 | pub fn supports_varargs(self) -> bool {
|
74 | 195 | // * C and Cdecl obviously support varargs.
|
75 | 196 | // * C can be based on Aapcs, SysV64 or Win64, so they must support varargs.
|
@@ -145,15 +266,11 @@ pub const AbiDatas: &[AbiData] = &[
|
145 | 266 | pub struct AbiUnsupported {}
|
146 | 267 | /// Returns the ABI with the given name (if any).
|
147 | 268 | pub fn lookup(name: &str) -> Result<Abi, AbiUnsupported> {
|
148 |
| - AbiDatas |
149 |
| - .iter() |
150 |
| - .find(|abi_data| name == abi_data.name) |
151 |
| - .map(|&x| x.abi) |
152 |
| - .ok_or_else(|| AbiUnsupported {}) |
| 269 | + ExternAbi::from_str(name).map_err(|_| AbiUnsupported {}) |
153 | 270 | }
|
154 | 271 |
|
155 | 272 | pub fn all_names() -> Vec<&'static str> {
|
156 |
| - AbiDatas.iter().map(|d| d.name).collect() |
| 273 | + ExternAbi::ALL_VARIANTS.iter().map(|abi| abi.as_str()).collect() |
157 | 274 | }
|
158 | 275 |
|
159 | 276 | impl Abi {
|
@@ -229,8 +346,8 @@ impl Abi {
|
229 | 346 | }
|
230 | 347 | }
|
231 | 348 |
|
232 |
| -impl fmt::Display for Abi { |
| 349 | +impl fmt::Display for ExternAbi { |
233 | 350 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
234 |
| - write!(f, "\"{}\"", self.name()) |
| 351 | + write!(f, "\"{}\"", self.as_str()) |
235 | 352 | }
|
236 | 353 | }
|
0 commit comments