core/ffi/primitives.rs
1//! Defines primitive types that match C's type definitions for FFI compatibility.
2//!
3//! This module is intentionally standalone to facilitate parsing when retrieving
4//! core C types.
5
6macro_rules! type_alias {
7 {
8 $Docfile:tt, $Alias:ident = $Real:ty;
9 $( $Cfg:tt )*
10 } => {
11 #[doc = include_str!($Docfile)]
12 $( $Cfg )*
13 #[stable(feature = "core_ffi_c", since = "1.64.0")]
14 pub type $Alias = $Real;
15 }
16}
17
18// `#[doc(cfg(true))]` is used to prevent rustdoc from displaying a "Available on ..." box.
19// The implementation of these constants is target-specific, but every target does define them.
20
21type_alias! { "c_char.md", c_char = c_char_definition::c_char; #[doc(cfg(true))] }
22
23type_alias! { "c_schar.md", c_schar = i8; }
24type_alias! { "c_uchar.md", c_uchar = u8; }
25type_alias! { "c_short.md", c_short = i16; }
26type_alias! { "c_ushort.md", c_ushort = u16; }
27
28type_alias! { "c_int.md", c_int = c_int_definition::c_int; #[doc(cfg(true))] }
29type_alias! { "c_uint.md", c_uint = c_int_definition::c_uint; #[doc(cfg(true))] }
30
31type_alias! { "c_long.md", c_long = c_long_definition::c_long; #[doc(cfg(true))] }
32type_alias! { "c_ulong.md", c_ulong = c_long_definition::c_ulong; #[doc(cfg(true))] }
33
34type_alias! { "c_longlong.md", c_longlong = i64; }
35type_alias! { "c_ulonglong.md", c_ulonglong = u64; }
36
37type_alias! { "c_float.md", c_float = f32; }
38type_alias! { "c_double.md", c_double= c_double_definition::c_double; #[doc(cfg(true))] }
39
40mod c_char_definition {
41 crate::cfg_select! {
42 // These are the targets on which c_char is unsigned. Usually the
43 // signedness is the same for all target_os values on a given architecture
44 // but there are some exceptions (see isSignedCharDefault() in clang).
45 // aarch64:
46 // Section 10 "Arm C and C++ language mappings" in Procedure Call Standard for the Arm®
47 // 64-bit Architecture (AArch64) says C/C++ char is unsigned byte.
48 // https://github.com/ARM-software/abi-aa/blob/2024Q3/aapcs64/aapcs64.rst#arm-c-and-c-language-mappings
49 // arm:
50 // Section 8 "Arm C and C++ Language Mappings" in Procedure Call Standard for the Arm®
51 // Architecture says C/C++ char is unsigned byte.
52 // https://github.com/ARM-software/abi-aa/blob/2024Q3/aapcs32/aapcs32.rst#arm-c-and-c-language-mappings
53 // csky:
54 // Section 2.1.2 "Primary Data Type" in C-SKY V2 CPU Applications Binary Interface
55 // Standards Manual says ANSI C char is unsigned byte.
56 // https://github.com/c-sky/csky-doc/blob/9f7121f7d40970ba5cc0f15716da033db2bb9d07/C-SKY_V2_CPU_Applications_Binary_Interface_Standards_Manual.pdf
57 // Note: this doesn't seem to match Clang's default (https://github.com/rust-lang/rust/issues/129945).
58 // hexagon:
59 // Section 3.1 "Basic data type" in Qualcomm Hexagon™ Application
60 // Binary Interface User Guide says "By default, the `char` data type is unsigned."
61 // https://docs.qualcomm.com/bundle/publicresource/80-N2040-23_REV_K_Qualcomm_Hexagon_Application_Binary_Interface_User_Guide.pdf
62 // msp430:
63 // Section 2.1 "Basic Types" in MSP430 Embedded Application Binary
64 // Interface says "The char type is unsigned by default".
65 // https://www.ti.com/lit/an/slaa534a/slaa534a.pdf
66 // powerpc/powerpc64:
67 // - PPC32 SysV: "Table 3-1 Scalar Types" in System V Application Binary Interface PowerPC
68 // Processor Supplement says ANSI C char is unsigned byte
69 // https://refspecs.linuxfoundation.org/elf/elfspec_ppc.pdf
70 // - PPC64 ELFv1: Section 3.1.4 "Fundamental Types" in 64-bit PowerPC ELF Application
71 // Binary Interface Supplement 1.9 says ANSI C is unsigned byte
72 // https://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi.html#FUND-TYPE
73 // - PPC64 ELFv2: Section 2.1.2.2 "Fundamental Types" in 64-Bit ELF V2 ABI Specification
74 // says char is unsigned byte
75 // https://openpowerfoundation.org/specifications/64bitelfabi/
76 // - AIX: XL C for AIX Language Reference says "By default, char behaves like an unsigned char."
77 // https://www.ibm.com/docs/en/xl-c-aix/13.1.3?topic=specifiers-character-types
78 // riscv32/riscv64:
79 // C/C++ type representations section in RISC-V Calling Conventions
80 // page in RISC-V ELF psABI Document says "char is unsigned."
81 // https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/draft-20240829-13bfa9f54634cb60d86b9b333e109f077805b4b3/riscv-cc.adoc#cc-type-representations
82 // s390x:
83 // - ELF: "Table 1.1.: Scalar types" in ELF Application Binary Interface s390x Supplement
84 // Version 1.6.1 categorize ISO C char in unsigned integer
85 // https://github.com/IBM/s390x-abi/releases/tag/v1.6.1
86 // - z/OS: XL C/C++ Language Reference says: "By default, char behaves like an unsigned char."
87 // https://www.ibm.com/docs/en/zos/3.1.0?topic=specifiers-character-types
88 // xtensa:
89 // Section 2.17.1 "Data Types and Alignment" of Xtensa LX Microprocessor Overview handbook
90 // says "`char` type is unsigned by default".
91 // https://loboris.eu/ESP32/Xtensa_lx%20Overview%20handbook.pdf
92 //
93 // On the following operating systems, c_char is signed by default, regardless of architecture.
94 // Darwin (macOS, iOS, etc.):
95 // Apple targets' c_char is signed by default even on arm
96 // https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Handle-data-types-and-data-alignment-properly
97 // Windows:
98 // Windows MSVC C++ Language Reference says "Microsoft-specific: Variables of type char
99 // are promoted to int as if from type signed char by default, unless the /J compilation
100 // option is used."
101 // https://learn.microsoft.com/en-us/cpp/cpp/fundamental-types-cpp?view=msvc-170#character-types
102 // Vita:
103 // Chars are signed by default on the Vita, and VITASDK follows that convention.
104 // https://github.com/vitasdk/buildscripts/blob/09c533b771591ecde88864b6acad28ffb688dbd4/patches/gcc/0001-gcc-10.patch#L33-L34
105 //
106 // L4Re:
107 // The kernel builds with -funsigned-char on all targets (but userspace follows the
108 // architecture defaults). As we only have a target for userspace apps so there are no
109 // special cases for L4Re below.
110 // https://github.com/rust-lang/rust/pull/132975#issuecomment-2484645240
111 all(
112 not(windows),
113 not(target_vendor = "apple"),
114 not(target_os = "vita"),
115 any(
116 target_arch = "aarch64",
117 target_arch = "arm",
118 target_arch = "csky",
119 target_arch = "hexagon",
120 target_arch = "msp430",
121 target_arch = "powerpc",
122 target_arch = "powerpc64",
123 target_arch = "riscv32",
124 target_arch = "riscv64",
125 target_arch = "s390x",
126 target_arch = "xtensa",
127 )
128 ) => {
129 pub(super) type c_char = u8;
130 }
131 // On every other target, c_char is signed.
132 _ => {
133 pub(super) type c_char = i8;
134 }
135 }
136}
137
138mod c_long_definition {
139 crate::cfg_select! {
140 any(
141 all(target_pointer_width = "64", not(windows)),
142 // wasm32 Linux ABI uses 64-bit long
143 all(target_arch = "wasm32", target_os = "linux")
144 ) => {
145 pub(super) type c_long = i64;
146 pub(super) type c_ulong = u64;
147 }
148 _ => {
149 // The minimal size of `long` in the C standard is 32 bits
150 pub(super) type c_long = i32;
151 pub(super) type c_ulong = u32;
152 }
153 }
154}
155
156/// Equivalent to C's `size_t` type, from `stddef.h` (or `cstddef` for C++).
157///
158/// This type is currently always [`usize`], however in the future there may be
159/// platforms where this is not the case.
160#[unstable(feature = "c_size_t", issue = "88345")]
161pub type c_size_t = usize;
162
163/// Equivalent to C's `ptrdiff_t` type, from `stddef.h` (or `cstddef` for C++).
164///
165/// This type is currently always [`isize`], however in the future there may be
166/// platforms where this is not the case.
167#[unstable(feature = "c_size_t", issue = "88345")]
168pub type c_ptrdiff_t = isize;
169
170/// Equivalent to C's `ssize_t` (on POSIX) or `SSIZE_T` (on Windows) type.
171///
172/// This type is currently always [`isize`], however in the future there may be
173/// platforms where this is not the case.
174#[unstable(feature = "c_size_t", issue = "88345")]
175pub type c_ssize_t = isize;
176
177mod c_int_definition {
178 crate::cfg_select! {
179 any(target_arch = "avr", target_arch = "msp430") => {
180 pub(super) type c_int = i16;
181 pub(super) type c_uint = u16;
182 }
183 _ => {
184 pub(super) type c_int = i32;
185 pub(super) type c_uint = u32;
186 }
187 }
188}
189
190mod c_double_definition {
191 crate::cfg_select! {
192 target_arch = "avr" => {
193 // avr:
194 // Per https://gcc.gnu.org/wiki/avr-gcc#Type_Layout. The table says `4,8` because
195 // in C the width of `double` can be changed with the `-mdouble=32/64` setting. But
196 // 32-bits is the default for the rust avr target.
197 pub(super) type c_double = f32;
198 }
199 _ => {
200 pub(super) type c_double = f64;
201 }
202 }
203}