1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Part of the Crubit project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//! # One to one FFI.
//!
//! In `ffi_11`, if a type is distinct in C/C++, it is distinct in Rust. This
//! relationship varies from platform to platform.
//!
//! For example, `char` and `signed char` are distinct types in C++. This means
//! that in `ffi_11`, there is a distinct `ffi_11::c_char` type which is not the
//! same as `ffi::c_schar`, even if `char` is signed. This is unlike the
//! `std::ffi` module, which would instead define both `c_char` and `c_schar`
//! as aliases to `i8`.
//!
//! As another example, on some platforms, `int64_t` is `long`, while on other
//! platforms, it is `long long`. Exactly one of `ffi_11::c_long` or
//! `ffi_11::c_longlong` will be `i64`, depending on the platform.
//!
//! ## Guarantees and backwards compatibility
//!
//! `ffi_11` offers the following guarantees:
//!
//! * Every unique C/C++ type is given a unique Rust type: the Rust->C/C++ type
//! mapping is one-to-one.
//! * A given type `c_<X>` is the same type as a builtin `iN` or `uN` type if,
//! and only if, the corresponding C++ type is the same type as the standard
//! library `(u)intN_t` on this platform.
//!
//! For example, `c_int` is `i32` if `int32_t` is a type alias to `int`.
//! Otherwise, `c_int` will be a different type. (Either a newtype, or a
//! non-`i32` primitive 32 bit integer type.)
//!
//! There are no type identity guarantees other than the above. For example,
//! `long long` may be a `i64`,`isize`, or a newtype, depending on the platform.
//!
//! There is also no guarantee that every Rust primitive type has a C++
//! equivalent. The 1:1 relationship only applies to the types in the `ffi_11`
//! module. To get the equivalent high-fidelity interop in C++, you would need
//! an equivalent `<ffi_11.h>` header, defined in a similar fashion and using
//! newtypes to define C++ types that correspond to Rust primitives that
//! otherwise have no C++ equivalent. (For example, the typical Windows ABI
//! only has one 64 bit integer type, while Rust has two.)
//!
//! ## Supported Operations
//!
//! The following operations are supported:
//!
//! * `From`: any `ffi_11` type can be converted to or from a builtin or
//! `ffi_11` type if the conversion is lossless. For example, `c_int` can
//! always be converted to `c_long`, but not to `c_ulong`. And `i32` can
//! always be converted to `c_int`, but `i64` can only on some platforms.
//!
//! * Separately from the above, `c_char` can be converted to and from both
//! `i8` and `u8` using `From` and `Into`. It is considered an
//! ambiguously-signed type for portability.
//!
//! ## Supported platforms
//!
//! For now, the only supported platforms are:
//!
//! * LP64: Any LP64 platform which uses the smallest suitable fundamental type
//! for `intN_t`. For example, Linux on x86_64 or Aarch64. But not OpenBSD.
//! * LLP64: 64-bit Windows.
//!
//! We will add support over time to other commonly used platforms.
//!
//! TODO(b/333759161): get and test a compatibility matrix, including
//! (currently untested) Windows.
//!
//! ## Unfinished Work
//!
//! This module is still embryonic, and is missing the following:
//!
//! * Support for `long long` on Linux. This depends on a decision about what
//! the type should be. For example, it could be `isize`, or a newtype.
//!
//! * `TryFrom` impls for lossy conversions.
//!
//! * Any/all other operations (e.g. arithmetic) one might want to support on newtypes.
//!
//! ## References
//!
//! * Discussion: ["Rust / C++ interop, and type collisions" in #t-lang/interop](https://rust-lang.zulipchat.com/#narrow/channel/427678-t-lang.2Finterop/topic/Rust.20.2F.20C.2B.2B.20interop.2C.20and.20type.20collisions)
extern crate core;
use ;
pub use c_void;
// ===============================
// The classic C fundamental types
// ===============================
//
// Implementation note: Clang picks the _smallest_ available fundamental type to
// be intN_t, with the exception of int16_t on e.g. AVR (int, instead of short),
// and int64_t on e.g. OpenBSD (long long, instead of long). This makes it
// relatively straightforward to maintain guarantee #2 of mapping the
// fundamental types backwards to the correct fixed-size type. We just need to
// know the sizes and whether or not we're on one of the unusual platforms.
pub type c_float = f32;
pub type c_double = f64;
// TODO(devinj): If you use cpp_type="char", Crubit will try to escape the type name, currently.
// To work around this, we can use decltype('a')) or similar.
//
// Implementation note: we can use `u8` instead of `i8` on the assumption that signedness doesn't
// change the ABI. The actual platform sign is hidden from users, though they can convert to
// `core::ffi::c_char` if they need it.
new_integer!
// Unlike the other new_integer! types, char converts to/from any type with the same bit width.
// Attributes on the aliases so that, opportunistically, we use e.g. `signed
// char` instead of `std::int8_t`, though they are the same thing. It's a
// spelling convenience, and optional for all aliases.
// TODO(devinj): These crubit annotate calls are currently no-ops.
//
// This doesn't result in _incorrect_ bindings -- actually, it would break, the same as `char`,
// if it worked. But the results are less readable than if we directly used the correct
// type: `signed char` instead of `std::int8_t`, `unsigned char` instead of `std::uint8_t`, etc.
pub type c_schar = i8;
pub type c_uchar = u8;
pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
/// LP64 with long int64_t.
// TODO(b/333759161): This is the mirror image of the above.
//
// /// LP64 with long long int64_t
// ///
// /// TODO(b/333759161): List out the full list of LP64 platforms which use long
// /// long here.
// #[cfg(all(target_pointer_width = "64", any(target_os = "openbsd")))]
// mod long_integers {
// pub type c_long = isize;
// pub type c_ulong = usize;
// pub type c_longlong = i64;
// pub type c_ulonglong = u64;
// }
/// LLP64 (Windows)