3 use rustc_macros::HashStable_Generic;
4 use rustc_span::symbol::sym;
5 use rustc_span::{Span, Symbol};
10 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug)]
11 #[derive(HashStable_Generic, Encodable, Decodable)]
13 // Some of the ABIs come first because every time we add a new ABI, we have to re-bless all the
14 // hashing tests. These are used in many places, so giving them stable values reduces test
15 // churn. The specific values are meaningless.
18 Cdecl { unwind: bool },
19 Stdcall { unwind: bool },
20 Fastcall { unwind: bool },
21 Vectorcall { unwind: bool },
22 Thiscall { unwind: bool },
23 Aapcs { unwind: bool },
24 Win64 { unwind: bool },
25 SysV64 { unwind: bool },
32 AvrNonBlockingInterrupt,
35 System { unwind: bool },
43 #[derive(Copy, Clone)]
47 /// Name of this ABI as we like it called.
51 #[allow(non_upper_case_globals)]
52 const AbiDatas: &[AbiData] = &[
53 AbiData { abi: Abi::Rust, name: "Rust" },
54 AbiData { abi: Abi::C { unwind: false }, name: "C" },
55 AbiData { abi: Abi::C { unwind: true }, name: "C-unwind" },
56 AbiData { abi: Abi::Cdecl { unwind: false }, name: "cdecl" },
57 AbiData { abi: Abi::Cdecl { unwind: true }, name: "cdecl-unwind" },
58 AbiData { abi: Abi::Stdcall { unwind: false }, name: "stdcall" },
59 AbiData { abi: Abi::Stdcall { unwind: true }, name: "stdcall-unwind" },
60 AbiData { abi: Abi::Fastcall { unwind: false }, name: "fastcall" },
61 AbiData { abi: Abi::Fastcall { unwind: true }, name: "fastcall-unwind" },
62 AbiData { abi: Abi::Vectorcall { unwind: false }, name: "vectorcall" },
63 AbiData { abi: Abi::Vectorcall { unwind: true }, name: "vectorcall-unwind" },
64 AbiData { abi: Abi::Thiscall { unwind: false }, name: "thiscall" },
65 AbiData { abi: Abi::Thiscall { unwind: true }, name: "thiscall-unwind" },
66 AbiData { abi: Abi::Aapcs { unwind: false }, name: "aapcs" },
67 AbiData { abi: Abi::Aapcs { unwind: true }, name: "aapcs-unwind" },
68 AbiData { abi: Abi::Win64 { unwind: false }, name: "win64" },
69 AbiData { abi: Abi::Win64 { unwind: true }, name: "win64-unwind" },
70 AbiData { abi: Abi::SysV64 { unwind: false }, name: "sysv64" },
71 AbiData { abi: Abi::SysV64 { unwind: true }, name: "sysv64-unwind" },
72 AbiData { abi: Abi::PtxKernel, name: "ptx-kernel" },
73 AbiData { abi: Abi::Msp430Interrupt, name: "msp430-interrupt" },
74 AbiData { abi: Abi::X86Interrupt, name: "x86-interrupt" },
75 AbiData { abi: Abi::AmdGpuKernel, name: "amdgpu-kernel" },
76 AbiData { abi: Abi::EfiApi, name: "efiapi" },
77 AbiData { abi: Abi::AvrInterrupt, name: "avr-interrupt" },
78 AbiData { abi: Abi::AvrNonBlockingInterrupt, name: "avr-non-blocking-interrupt" },
79 AbiData { abi: Abi::CCmseNonSecureCall, name: "C-cmse-nonsecure-call" },
80 AbiData { abi: Abi::Wasm, name: "wasm" },
81 AbiData { abi: Abi::System { unwind: false }, name: "system" },
82 AbiData { abi: Abi::System { unwind: true }, name: "system-unwind" },
83 AbiData { abi: Abi::RustIntrinsic, name: "rust-intrinsic" },
84 AbiData { abi: Abi::RustCall, name: "rust-call" },
85 AbiData { abi: Abi::PlatformIntrinsic, name: "platform-intrinsic" },
86 AbiData { abi: Abi::Unadjusted, name: "unadjusted" },
87 AbiData { abi: Abi::RustCold, name: "rust-cold" },
90 /// Returns the ABI with the given name (if any).
91 pub fn lookup(name: &str) -> Option<Abi> {
92 AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
95 pub fn all_names() -> Vec<&'static str> {
96 AbiDatas.iter().map(|d| d.name).collect()
99 pub fn enabled_names(features: &rustc_feature::Features, span: Span) -> Vec<&'static str> {
103 .filter(|name| is_enabled(features, span, name).is_ok())
107 pub enum AbiDisabled {
108 Unstable { feature: Symbol, explain: &'static str },
112 fn gate_feature_post(
113 features: &rustc_feature::Features,
116 explain: &'static str,
117 ) -> Result<(), AbiDisabled> {
118 if !features.enabled(feature) && !span.allows_unstable(feature) {
119 Err(AbiDisabled::Unstable { feature, explain })
126 features: &rustc_feature::Features,
129 ) -> Result<(), AbiDisabled> {
132 "Rust" | "C" | "cdecl" | "stdcall" | "fastcall" | "aapcs" | "win64" | "sysv64"
133 | "system" => Ok(()),
134 "rust-intrinsic" => {
135 gate_feature_post(features, sym::intrinsics, span, "intrinsics are subject to change")
137 "platform-intrinsic" => gate_feature_post(
139 sym::platform_intrinsics,
141 "platform intrinsics are experimental and possibly buggy",
143 "vectorcall" => gate_feature_post(
147 "vectorcall is experimental and subject to change",
149 "thiscall" => gate_feature_post(
153 "thiscall is experimental and subject to change",
155 "rust-call" => gate_feature_post(
157 sym::unboxed_closures,
159 "rust-call ABI is subject to change",
161 "rust-cold" => gate_feature_post(
165 "rust-cold is experimental and subject to change",
167 "ptx-kernel" => gate_feature_post(
171 "PTX ABIs are experimental and subject to change",
173 "unadjusted" => gate_feature_post(
177 "unadjusted ABI is an implementation detail and perma-unstable",
179 "msp430-interrupt" => gate_feature_post(
181 sym::abi_msp430_interrupt,
183 "msp430-interrupt ABI is experimental and subject to change",
185 "x86-interrupt" => gate_feature_post(
187 sym::abi_x86_interrupt,
189 "x86-interrupt ABI is experimental and subject to change",
191 "amdgpu-kernel" => gate_feature_post(
193 sym::abi_amdgpu_kernel,
195 "amdgpu-kernel ABI is experimental and subject to change",
197 "avr-interrupt" | "avr-non-blocking-interrupt" => gate_feature_post(
199 sym::abi_avr_interrupt,
201 "avr-interrupt and avr-non-blocking-interrupt ABIs are experimental and subject to change",
203 "efiapi" => gate_feature_post(
207 "efiapi ABI is experimental and subject to change",
209 "C-cmse-nonsecure-call" => gate_feature_post(
211 sym::abi_c_cmse_nonsecure_call,
213 "C-cmse-nonsecure-call ABI is experimental and subject to change",
215 "C-unwind" => gate_feature_post(
219 "C-unwind ABI is experimental and subject to change",
221 "stdcall-unwind" => gate_feature_post(
225 "stdcall-unwind ABI is experimental and subject to change",
227 "system-unwind" => gate_feature_post(
231 "system-unwind ABI is experimental and subject to change",
233 "thiscall-unwind" => gate_feature_post(
237 "thiscall-unwind ABI is experimental and subject to change",
239 "cdecl-unwind" => gate_feature_post(
243 "cdecl-unwind ABI is experimental and subject to change",
245 "fastcall-unwind" => gate_feature_post(
249 "fastcall-unwind ABI is experimental and subject to change",
251 "vectorcall-unwind" => gate_feature_post(
255 "vectorcall-unwind ABI is experimental and subject to change",
257 "aapcs-unwind" => gate_feature_post(
261 "aapcs-unwind ABI is experimental and subject to change",
263 "win64-unwind" => gate_feature_post(
267 "win64-unwind ABI is experimental and subject to change",
269 "sysv64-unwind" => gate_feature_post(
273 "sysv64-unwind ABI is experimental and subject to change",
275 "wasm" => gate_feature_post(
279 "wasm ABI is experimental and subject to change",
281 _ => Err(AbiDisabled::Unrecognized),
286 /// Default ABI chosen for `extern fn` declarations without an explicit ABI.
287 pub const FALLBACK: Abi = Abi::C { unwind: false };
290 pub fn index(self) -> usize {
291 // N.B., this ordering MUST match the AbiDatas array above.
292 // (This is ensured by the test indices_are_correct().)
295 // Cross-platform ABIs
297 C { unwind: false } => 1,
298 C { unwind: true } => 2,
299 // Platform-specific ABIs
300 Cdecl { unwind: false } => 3,
301 Cdecl { unwind: true } => 4,
302 Stdcall { unwind: false } => 5,
303 Stdcall { unwind: true } => 6,
304 Fastcall { unwind: false } => 7,
305 Fastcall { unwind: true } => 8,
306 Vectorcall { unwind: false } => 9,
307 Vectorcall { unwind: true } => 10,
308 Thiscall { unwind: false } => 11,
309 Thiscall { unwind: true } => 12,
310 Aapcs { unwind: false } => 13,
311 Aapcs { unwind: true } => 14,
312 Win64 { unwind: false } => 15,
313 Win64 { unwind: true } => 16,
314 SysV64 { unwind: false } => 17,
315 SysV64 { unwind: true } => 18,
317 Msp430Interrupt => 20,
322 AvrNonBlockingInterrupt => 25,
323 CCmseNonSecureCall => 26,
325 // Cross-platform ABIs
326 System { unwind: false } => 28,
327 System { unwind: true } => 29,
330 PlatformIntrinsic => 32,
338 .find(|(_, AbiData { abi, .. })| *abi == self)
339 .map(|(index, _)| index)
340 .expect("abi variant has associated data")
342 "Abi index did not match `AbiDatas` ordering"
348 pub fn data(self) -> &'static AbiData {
349 &AbiDatas[self.index()]
352 pub fn name(self) -> &'static str {
357 impl fmt::Display for Abi {
358 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
360 abi => write!(f, "\"{}\"", abi.name()),