]>
Commit | Line | Data |
---|---|---|
5e7ed085 FG |
1 | //! Platform-specific types, as defined by C. |
2 | //! | |
3 | //! Code that interacts via FFI will almost certainly be using the | |
4 | //! base types provided by C, which aren't nearly as nicely defined | |
5 | //! as Rust's primitive types. This module provides types which will | |
6 | //! match those defined by C, so that code that interacts with C will | |
7 | //! refer to the correct types. | |
8 | ||
b7449926 | 9 | #![stable(feature = "", since = "1.30.0")] |
b7449926 XL |
10 | #![allow(non_camel_case_types)] |
11 | ||
48663c56 | 12 | use crate::fmt; |
dc9dc135 | 13 | use crate::marker::PhantomData; |
5e7ed085 | 14 | use crate::num::*; |
dc9dc135 | 15 | use crate::ops::{Deref, DerefMut}; |
b7449926 | 16 | |
064997fb | 17 | #[stable(feature = "core_c_str", since = "1.64.0")] |
04454e1e FG |
18 | pub use self::c_str::{CStr, FromBytesUntilNulError, FromBytesWithNulError}; |
19 | ||
20 | mod c_str; | |
21 | ||
5e7ed085 FG |
22 | macro_rules! type_alias_no_nz { |
23 | { | |
24 | $Docfile:tt, $Alias:ident = $Real:ty; | |
25 | $( $Cfg:tt )* | |
26 | } => { | |
27 | #[doc = include_str!($Docfile)] | |
28 | $( $Cfg )* | |
064997fb | 29 | #[stable(feature = "core_ffi_c", since = "1.64.0")] |
5e7ed085 FG |
30 | pub type $Alias = $Real; |
31 | } | |
32 | } | |
33 | ||
34 | // To verify that the NonZero types in this file's macro invocations correspond | |
35 | // | |
36 | // perl -n < library/std/src/os/raw/mod.rs -e 'next unless m/type_alias\!/; die "$_ ?" unless m/, (c_\w+) = (\w+), NonZero_(\w+) = NonZero(\w+)/; die "$_ ?" unless $3 eq $1 and $4 eq ucfirst $2' | |
37 | // | |
38 | // NB this does not check that the main c_* types are right. | |
39 | ||
40 | macro_rules! type_alias { | |
41 | { | |
42 | $Docfile:tt, $Alias:ident = $Real:ty, $NZAlias:ident = $NZReal:ty; | |
43 | $( $Cfg:tt )* | |
44 | } => { | |
45 | type_alias_no_nz! { $Docfile, $Alias = $Real; $( $Cfg )* } | |
46 | ||
47 | #[doc = concat!("Type alias for `NonZero` version of [`", stringify!($Alias), "`]")] | |
48 | #[unstable(feature = "raw_os_nonzero", issue = "82363")] | |
49 | $( $Cfg )* | |
50 | pub type $NZAlias = $NZReal; | |
51 | } | |
52 | } | |
53 | ||
54 | type_alias! { "c_char.md", c_char = c_char_definition::c_char, NonZero_c_char = c_char_definition::NonZero_c_char; | |
55 | // Make this type alias appear cfg-dependent so that Clippy does not suggest | |
56 | // replacing `0 as c_char` with `0_i8`/`0_u8`. This #[cfg(all())] can be removed | |
57 | // after the false positive in https://github.com/rust-lang/rust-clippy/issues/8093 | |
58 | // is fixed. | |
59 | #[cfg(all())] | |
60 | #[doc(cfg(all()))] } | |
04454e1e | 61 | |
5e7ed085 FG |
62 | type_alias! { "c_schar.md", c_schar = i8, NonZero_c_schar = NonZeroI8; } |
63 | type_alias! { "c_uchar.md", c_uchar = u8, NonZero_c_uchar = NonZeroU8; } | |
64 | type_alias! { "c_short.md", c_short = i16, NonZero_c_short = NonZeroI16; } | |
65 | type_alias! { "c_ushort.md", c_ushort = u16, NonZero_c_ushort = NonZeroU16; } | |
04454e1e FG |
66 | |
67 | type_alias! { "c_int.md", c_int = c_int_definition::c_int, NonZero_c_int = c_int_definition::NonZero_c_int; | |
68 | #[doc(cfg(all()))] } | |
69 | type_alias! { "c_uint.md", c_uint = c_int_definition::c_uint, NonZero_c_uint = c_int_definition::NonZero_c_uint; | |
70 | #[doc(cfg(all()))] } | |
71 | ||
72 | type_alias! { "c_long.md", c_long = c_long_definition::c_long, NonZero_c_long = c_long_definition::NonZero_c_long; | |
73 | #[doc(cfg(all()))] } | |
74 | type_alias! { "c_ulong.md", c_ulong = c_long_definition::c_ulong, NonZero_c_ulong = c_long_definition::NonZero_c_ulong; | |
75 | #[doc(cfg(all()))] } | |
76 | ||
5e7ed085 FG |
77 | type_alias! { "c_longlong.md", c_longlong = i64, NonZero_c_longlong = NonZeroI64; } |
78 | type_alias! { "c_ulonglong.md", c_ulonglong = u64, NonZero_c_ulonglong = NonZeroU64; } | |
04454e1e | 79 | |
5e7ed085 FG |
80 | type_alias_no_nz! { "c_float.md", c_float = f32; } |
81 | type_alias_no_nz! { "c_double.md", c_double = f64; } | |
82 | ||
83 | /// Equivalent to C's `size_t` type, from `stddef.h` (or `cstddef` for C++). | |
b7449926 | 84 | /// |
5e7ed085 FG |
85 | /// This type is currently always [`usize`], however in the future there may be |
86 | /// platforms where this is not the case. | |
87 | #[unstable(feature = "c_size_t", issue = "88345")] | |
88 | pub type c_size_t = usize; | |
89 | ||
90 | /// Equivalent to C's `ptrdiff_t` type, from `stddef.h` (or `cstddef` for C++). | |
b7449926 | 91 | /// |
5e7ed085 FG |
92 | /// This type is currently always [`isize`], however in the future there may be |
93 | /// platforms where this is not the case. | |
94 | #[unstable(feature = "c_size_t", issue = "88345")] | |
95 | pub type c_ptrdiff_t = isize; | |
96 | ||
97 | /// Equivalent to C's `ssize_t` (on POSIX) or `SSIZE_T` (on Windows) type. | |
e74abb32 | 98 | /// |
5e7ed085 FG |
99 | /// This type is currently always [`isize`], however in the future there may be |
100 | /// platforms where this is not the case. | |
101 | #[unstable(feature = "c_size_t", issue = "88345")] | |
102 | pub type c_ssize_t = isize; | |
103 | ||
104 | mod c_char_definition { | |
105 | cfg_if! { | |
106 | // These are the targets on which c_char is unsigned. | |
107 | if #[cfg(any( | |
108 | all( | |
109 | target_os = "linux", | |
110 | any( | |
111 | target_arch = "aarch64", | |
112 | target_arch = "arm", | |
113 | target_arch = "hexagon", | |
114 | target_arch = "powerpc", | |
115 | target_arch = "powerpc64", | |
116 | target_arch = "s390x", | |
117 | target_arch = "riscv64", | |
118 | target_arch = "riscv32" | |
119 | ) | |
120 | ), | |
121 | all(target_os = "android", any(target_arch = "aarch64", target_arch = "arm")), | |
122 | all(target_os = "l4re", target_arch = "x86_64"), | |
123 | all( | |
124 | any(target_os = "freebsd", target_os = "openbsd"), | |
125 | any( | |
126 | target_arch = "aarch64", | |
127 | target_arch = "arm", | |
128 | target_arch = "powerpc", | |
129 | target_arch = "powerpc64", | |
130 | target_arch = "riscv64" | |
131 | ) | |
132 | ), | |
133 | all( | |
134 | target_os = "netbsd", | |
fe692bf9 FG |
135 | any( |
136 | target_arch = "aarch64", | |
137 | target_arch = "arm", | |
138 | target_arch = "powerpc", | |
139 | target_arch = "riscv64" | |
140 | ) | |
5e7ed085 FG |
141 | ), |
142 | all( | |
143 | target_os = "vxworks", | |
144 | any( | |
145 | target_arch = "aarch64", | |
146 | target_arch = "arm", | |
147 | target_arch = "powerpc64", | |
148 | target_arch = "powerpc" | |
149 | ) | |
150 | ), | |
49aad941 FG |
151 | all( |
152 | target_os = "fuchsia", | |
153 | any(target_arch = "aarch64", target_arch = "riscv64") | |
154 | ), | |
9ffffee4 | 155 | all(target_os = "nto", target_arch = "aarch64"), |
923072b8 | 156 | target_os = "horizon" |
5e7ed085 FG |
157 | ))] { |
158 | pub type c_char = u8; | |
159 | pub type NonZero_c_char = crate::num::NonZeroU8; | |
160 | } else { | |
161 | // On every other target, c_char is signed. | |
162 | pub type c_char = i8; | |
163 | pub type NonZero_c_char = crate::num::NonZeroI8; | |
164 | } | |
165 | } | |
166 | } | |
167 | ||
04454e1e FG |
168 | mod c_int_definition { |
169 | cfg_if! { | |
170 | if #[cfg(any(target_arch = "avr", target_arch = "msp430"))] { | |
171 | pub type c_int = i16; | |
172 | pub type NonZero_c_int = crate::num::NonZeroI16; | |
173 | pub type c_uint = u16; | |
174 | pub type NonZero_c_uint = crate::num::NonZeroU16; | |
175 | } else { | |
176 | pub type c_int = i32; | |
177 | pub type NonZero_c_int = crate::num::NonZeroI32; | |
178 | pub type c_uint = u32; | |
179 | pub type NonZero_c_uint = crate::num::NonZeroU32; | |
180 | } | |
181 | } | |
182 | } | |
183 | ||
184 | mod c_long_definition { | |
185 | cfg_if! { | |
186 | if #[cfg(all(target_pointer_width = "64", not(windows)))] { | |
187 | pub type c_long = i64; | |
188 | pub type NonZero_c_long = crate::num::NonZeroI64; | |
189 | pub type c_ulong = u64; | |
190 | pub type NonZero_c_ulong = crate::num::NonZeroU64; | |
191 | } else { | |
192 | // The minimal size of `long` in the C standard is 32 bits | |
193 | pub type c_long = i32; | |
194 | pub type NonZero_c_long = crate::num::NonZeroI32; | |
195 | pub type c_ulong = u32; | |
196 | pub type NonZero_c_ulong = crate::num::NonZeroU32; | |
197 | } | |
198 | } | |
199 | } | |
200 | ||
0731742a | 201 | // N.B., for LLVM to recognize the void pointer type and by extension |
b7449926 XL |
202 | // functions like malloc(), we need to have it represented as i8* in |
203 | // LLVM bitcode. The enum used here ensures this and prevents misuse | |
9fa01778 | 204 | // of the "raw" type by only having private variants. We need two |
b7449926 | 205 | // variants, because the compiler complains about the repr attribute |
9fa01778 XL |
206 | // otherwise and we need at least one variant as otherwise the enum |
207 | // would be uninhabited and at least dereferencing such pointers would | |
208 | // be UB. | |
5e7ed085 | 209 | #[doc = include_str!("c_void.md")] |
fe692bf9 | 210 | #[lang = "c_void"] |
49aad941 | 211 | #[cfg_attr(not(doc), repr(u8))] // work around https://github.com/rust-lang/rust/issues/90435 |
e74abb32 | 212 | #[stable(feature = "core_c_void", since = "1.30.0")] |
b7449926 | 213 | pub enum c_void { |
60c5eb7d XL |
214 | #[unstable( |
215 | feature = "c_void_variant", | |
216 | reason = "temporary implementation detail", | |
dfeec247 | 217 | issue = "none" |
60c5eb7d XL |
218 | )] |
219 | #[doc(hidden)] | |
220 | __variant1, | |
221 | #[unstable( | |
222 | feature = "c_void_variant", | |
223 | reason = "temporary implementation detail", | |
dfeec247 | 224 | issue = "none" |
60c5eb7d XL |
225 | )] |
226 | #[doc(hidden)] | |
227 | __variant2, | |
b7449926 XL |
228 | } |
229 | ||
230 | #[stable(feature = "std_debug", since = "1.16.0")] | |
231 | impl fmt::Debug for c_void { | |
48663c56 | 232 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
cdc7bbd5 | 233 | f.debug_struct("c_void").finish() |
b7449926 XL |
234 | } |
235 | } | |
a1dfa0c6 XL |
236 | |
237 | /// Basic implementation of a `va_list`. | |
dc9dc135 | 238 | // The name is WIP, using `VaListImpl` for now. |
60c5eb7d | 239 | #[cfg(any( |
9c376795 FG |
240 | all( |
241 | not(target_arch = "aarch64"), | |
242 | not(target_arch = "powerpc"), | |
243 | not(target_arch = "s390x"), | |
244 | not(target_arch = "x86_64") | |
245 | ), | |
fe692bf9 | 246 | all(target_arch = "aarch64", any(target_os = "macos", target_os = "ios", target_os = "tvos")), |
3c0e092e | 247 | target_family = "wasm", |
60c5eb7d | 248 | target_arch = "asmjs", |
064997fb FG |
249 | target_os = "uefi", |
250 | windows, | |
60c5eb7d | 251 | ))] |
49aad941 | 252 | #[cfg_attr(not(doc), repr(transparent))] // work around https://github.com/rust-lang/rust/issues/90435 |
60c5eb7d XL |
253 | #[unstable( |
254 | feature = "c_variadic", | |
255 | reason = "the `c_variadic` feature has not been properly tested on \ | |
256 | all supported platforms", | |
257 | issue = "44930" | |
258 | )] | |
dc9dc135 XL |
259 | #[lang = "va_list"] |
260 | pub struct VaListImpl<'f> { | |
261 | ptr: *mut c_void, | |
416331ca XL |
262 | |
263 | // Invariant over `'f`, so each `VaListImpl<'f>` object is tied to | |
264 | // the region of the function it's defined in | |
265 | _marker: PhantomData<&'f mut &'f c_void>, | |
a1dfa0c6 XL |
266 | } |
267 | ||
60c5eb7d | 268 | #[cfg(any( |
9c376795 FG |
269 | all( |
270 | not(target_arch = "aarch64"), | |
271 | not(target_arch = "powerpc"), | |
272 | not(target_arch = "s390x"), | |
273 | not(target_arch = "x86_64") | |
274 | ), | |
fe692bf9 | 275 | all(target_arch = "aarch64", any(target_os = "macos", target_os = "ios", target_os = "tvos")), |
3c0e092e | 276 | target_family = "wasm", |
60c5eb7d | 277 | target_arch = "asmjs", |
064997fb FG |
278 | target_os = "uefi", |
279 | windows, | |
60c5eb7d XL |
280 | ))] |
281 | #[unstable( | |
282 | feature = "c_variadic", | |
283 | reason = "the `c_variadic` feature has not been properly tested on \ | |
284 | all supported platforms", | |
285 | issue = "44930" | |
286 | )] | |
dc9dc135 | 287 | impl<'f> fmt::Debug for VaListImpl<'f> { |
48663c56 | 288 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
dc9dc135 | 289 | write!(f, "va_list* {:p}", self.ptr) |
a1dfa0c6 XL |
290 | } |
291 | } | |
292 | ||
293 | /// AArch64 ABI implementation of a `va_list`. See the | |
532ac7d7 | 294 | /// [AArch64 Procedure Call Standard] for more details. |
a1dfa0c6 XL |
295 | /// |
296 | /// [AArch64 Procedure Call Standard]: | |
297 | /// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0055b/IHI0055B_aapcs64.pdf | |
29967ef6 XL |
298 | #[cfg(all( |
299 | target_arch = "aarch64", | |
fe692bf9 | 300 | not(any(target_os = "macos", target_os = "ios", target_os = "tvos")), |
064997fb FG |
301 | not(target_os = "uefi"), |
302 | not(windows), | |
29967ef6 | 303 | ))] |
49aad941 | 304 | #[cfg_attr(not(doc), repr(C))] // work around https://github.com/rust-lang/rust/issues/66401 |
a1dfa0c6 | 305 | #[derive(Debug)] |
60c5eb7d XL |
306 | #[unstable( |
307 | feature = "c_variadic", | |
308 | reason = "the `c_variadic` feature has not been properly tested on \ | |
309 | all supported platforms", | |
310 | issue = "44930" | |
311 | )] | |
dc9dc135 XL |
312 | #[lang = "va_list"] |
313 | pub struct VaListImpl<'f> { | |
532ac7d7 XL |
314 | stack: *mut c_void, |
315 | gr_top: *mut c_void, | |
316 | vr_top: *mut c_void, | |
a1dfa0c6 XL |
317 | gr_offs: i32, |
318 | vr_offs: i32, | |
416331ca | 319 | _marker: PhantomData<&'f mut &'f c_void>, |
a1dfa0c6 XL |
320 | } |
321 | ||
322 | /// PowerPC ABI implementation of a `va_list`. | |
064997fb | 323 | #[cfg(all(target_arch = "powerpc", not(target_os = "uefi"), not(windows)))] |
49aad941 | 324 | #[cfg_attr(not(doc), repr(C))] // work around https://github.com/rust-lang/rust/issues/66401 |
a1dfa0c6 | 325 | #[derive(Debug)] |
60c5eb7d XL |
326 | #[unstable( |
327 | feature = "c_variadic", | |
328 | reason = "the `c_variadic` feature has not been properly tested on \ | |
329 | all supported platforms", | |
330 | issue = "44930" | |
331 | )] | |
dc9dc135 XL |
332 | #[lang = "va_list"] |
333 | pub struct VaListImpl<'f> { | |
a1dfa0c6 XL |
334 | gpr: u8, |
335 | fpr: u8, | |
336 | reserved: u16, | |
532ac7d7 XL |
337 | overflow_arg_area: *mut c_void, |
338 | reg_save_area: *mut c_void, | |
416331ca | 339 | _marker: PhantomData<&'f mut &'f c_void>, |
a1dfa0c6 XL |
340 | } |
341 | ||
9c376795 FG |
342 | /// s390x ABI implementation of a `va_list`. |
343 | #[cfg(target_arch = "s390x")] | |
49aad941 | 344 | #[cfg_attr(not(doc), repr(C))] // work around https://github.com/rust-lang/rust/issues/66401 |
9c376795 FG |
345 | #[derive(Debug)] |
346 | #[unstable( | |
347 | feature = "c_variadic", | |
348 | reason = "the `c_variadic` feature has not been properly tested on \ | |
349 | all supported platforms", | |
350 | issue = "44930" | |
351 | )] | |
352 | #[lang = "va_list"] | |
353 | pub struct VaListImpl<'f> { | |
354 | gpr: i64, | |
355 | fpr: i64, | |
356 | overflow_arg_area: *mut c_void, | |
357 | reg_save_area: *mut c_void, | |
358 | _marker: PhantomData<&'f mut &'f c_void>, | |
359 | } | |
360 | ||
a1dfa0c6 | 361 | /// x86_64 ABI implementation of a `va_list`. |
064997fb | 362 | #[cfg(all(target_arch = "x86_64", not(target_os = "uefi"), not(windows)))] |
49aad941 | 363 | #[cfg_attr(not(doc), repr(C))] // work around https://github.com/rust-lang/rust/issues/66401 |
a1dfa0c6 | 364 | #[derive(Debug)] |
60c5eb7d XL |
365 | #[unstable( |
366 | feature = "c_variadic", | |
367 | reason = "the `c_variadic` feature has not been properly tested on \ | |
368 | all supported platforms", | |
369 | issue = "44930" | |
370 | )] | |
dc9dc135 XL |
371 | #[lang = "va_list"] |
372 | pub struct VaListImpl<'f> { | |
a1dfa0c6 XL |
373 | gp_offset: i32, |
374 | fp_offset: i32, | |
532ac7d7 XL |
375 | overflow_arg_area: *mut c_void, |
376 | reg_save_area: *mut c_void, | |
416331ca | 377 | _marker: PhantomData<&'f mut &'f c_void>, |
a1dfa0c6 XL |
378 | } |
379 | ||
dc9dc135 | 380 | /// A wrapper for a `va_list` |
49aad941 | 381 | #[cfg_attr(not(doc), repr(transparent))] // work around https://github.com/rust-lang/rust/issues/90435 |
dc9dc135 | 382 | #[derive(Debug)] |
60c5eb7d XL |
383 | #[unstable( |
384 | feature = "c_variadic", | |
385 | reason = "the `c_variadic` feature has not been properly tested on \ | |
386 | all supported platforms", | |
387 | issue = "44930" | |
388 | )] | |
dc9dc135 | 389 | pub struct VaList<'a, 'f: 'a> { |
60c5eb7d XL |
390 | #[cfg(any( |
391 | all( | |
392 | not(target_arch = "aarch64"), | |
393 | not(target_arch = "powerpc"), | |
9c376795 | 394 | not(target_arch = "s390x"), |
60c5eb7d XL |
395 | not(target_arch = "x86_64") |
396 | ), | |
fe692bf9 FG |
397 | all( |
398 | target_arch = "aarch64", | |
399 | any(target_os = "macos", target_os = "ios", target_os = "tvos") | |
400 | ), | |
3c0e092e | 401 | target_family = "wasm", |
60c5eb7d | 402 | target_arch = "asmjs", |
064997fb FG |
403 | target_os = "uefi", |
404 | windows, | |
60c5eb7d | 405 | ))] |
dc9dc135 XL |
406 | inner: VaListImpl<'f>, |
407 | ||
60c5eb7d | 408 | #[cfg(all( |
9c376795 FG |
409 | any( |
410 | target_arch = "aarch64", | |
411 | target_arch = "powerpc", | |
412 | target_arch = "s390x", | |
413 | target_arch = "x86_64" | |
414 | ), | |
fe692bf9 FG |
415 | any( |
416 | not(target_arch = "aarch64"), | |
417 | not(any(target_os = "macos", target_os = "ios", target_os = "tvos")) | |
418 | ), | |
3c0e092e | 419 | not(target_family = "wasm"), |
60c5eb7d | 420 | not(target_arch = "asmjs"), |
064997fb FG |
421 | not(target_os = "uefi"), |
422 | not(windows), | |
60c5eb7d | 423 | ))] |
dc9dc135 XL |
424 | inner: &'a mut VaListImpl<'f>, |
425 | ||
426 | _marker: PhantomData<&'a mut VaListImpl<'f>>, | |
427 | } | |
428 | ||
60c5eb7d | 429 | #[cfg(any( |
9c376795 FG |
430 | all( |
431 | not(target_arch = "aarch64"), | |
432 | not(target_arch = "powerpc"), | |
433 | not(target_arch = "s390x"), | |
434 | not(target_arch = "x86_64") | |
435 | ), | |
fe692bf9 | 436 | all(target_arch = "aarch64", any(target_os = "macos", target_os = "ios", target_os = "tvos")), |
3c0e092e | 437 | target_family = "wasm", |
60c5eb7d | 438 | target_arch = "asmjs", |
064997fb FG |
439 | target_os = "uefi", |
440 | windows, | |
60c5eb7d XL |
441 | ))] |
442 | #[unstable( | |
443 | feature = "c_variadic", | |
444 | reason = "the `c_variadic` feature has not been properly tested on \ | |
445 | all supported platforms", | |
446 | issue = "44930" | |
447 | )] | |
dc9dc135 XL |
448 | impl<'f> VaListImpl<'f> { |
449 | /// Convert a `VaListImpl` into a `VaList` that is binary-compatible with C's `va_list`. | |
450 | #[inline] | |
451 | pub fn as_va_list<'a>(&'a mut self) -> VaList<'a, 'f> { | |
60c5eb7d | 452 | VaList { inner: VaListImpl { ..*self }, _marker: PhantomData } |
dc9dc135 XL |
453 | } |
454 | } | |
455 | ||
60c5eb7d | 456 | #[cfg(all( |
9c376795 FG |
457 | any( |
458 | target_arch = "aarch64", | |
459 | target_arch = "powerpc", | |
460 | target_arch = "s390x", | |
461 | target_arch = "x86_64" | |
462 | ), | |
fe692bf9 FG |
463 | any( |
464 | not(target_arch = "aarch64"), | |
465 | not(any(target_os = "macos", target_os = "ios", target_os = "tvos")) | |
466 | ), | |
3c0e092e | 467 | not(target_family = "wasm"), |
60c5eb7d | 468 | not(target_arch = "asmjs"), |
064997fb FG |
469 | not(target_os = "uefi"), |
470 | not(windows), | |
60c5eb7d XL |
471 | ))] |
472 | #[unstable( | |
473 | feature = "c_variadic", | |
474 | reason = "the `c_variadic` feature has not been properly tested on \ | |
475 | all supported platforms", | |
476 | issue = "44930" | |
477 | )] | |
dc9dc135 XL |
478 | impl<'f> VaListImpl<'f> { |
479 | /// Convert a `VaListImpl` into a `VaList` that is binary-compatible with C's `va_list`. | |
480 | #[inline] | |
481 | pub fn as_va_list<'a>(&'a mut self) -> VaList<'a, 'f> { | |
60c5eb7d | 482 | VaList { inner: self, _marker: PhantomData } |
dc9dc135 XL |
483 | } |
484 | } | |
485 | ||
60c5eb7d XL |
486 | #[unstable( |
487 | feature = "c_variadic", | |
488 | reason = "the `c_variadic` feature has not been properly tested on \ | |
489 | all supported platforms", | |
490 | issue = "44930" | |
491 | )] | |
dc9dc135 XL |
492 | impl<'a, 'f: 'a> Deref for VaList<'a, 'f> { |
493 | type Target = VaListImpl<'f>; | |
494 | ||
495 | #[inline] | |
496 | fn deref(&self) -> &VaListImpl<'f> { | |
497 | &self.inner | |
498 | } | |
499 | } | |
500 | ||
60c5eb7d XL |
501 | #[unstable( |
502 | feature = "c_variadic", | |
503 | reason = "the `c_variadic` feature has not been properly tested on \ | |
504 | all supported platforms", | |
505 | issue = "44930" | |
506 | )] | |
dc9dc135 XL |
507 | impl<'a, 'f: 'a> DerefMut for VaList<'a, 'f> { |
508 | #[inline] | |
509 | fn deref_mut(&mut self) -> &mut VaListImpl<'f> { | |
510 | &mut self.inner | |
511 | } | |
512 | } | |
a1dfa0c6 XL |
513 | |
514 | // The VaArgSafe trait needs to be used in public interfaces, however, the trait | |
515 | // itself must not be allowed to be used outside this module. Allowing users to | |
516 | // implement the trait for a new type (thereby allowing the va_arg intrinsic to | |
517 | // be used on a new type) is likely to cause undefined behavior. | |
518 | // | |
519 | // FIXME(dlrobertson): In order to use the VaArgSafe trait in a public interface | |
520 | // but also ensure it cannot be used elsewhere, the trait needs to be public | |
521 | // within a private module. Once RFC 2145 has been implemented look into | |
522 | // improving this. | |
523 | mod sealed_trait { | |
29967ef6 | 524 | /// Trait which permits the allowed types to be used with [super::VaListImpl::arg]. |
60c5eb7d XL |
525 | #[unstable( |
526 | feature = "c_variadic", | |
527 | reason = "the `c_variadic` feature has not been properly tested on \ | |
528 | all supported platforms", | |
529 | issue = "44930" | |
530 | )] | |
a1dfa0c6 XL |
531 | pub trait VaArgSafe {} |
532 | } | |
533 | ||
534 | macro_rules! impl_va_arg_safe { | |
535 | ($($t:ty),+) => { | |
536 | $( | |
537 | #[unstable(feature = "c_variadic", | |
538 | reason = "the `c_variadic` feature has not been properly tested on \ | |
539 | all supported platforms", | |
9fa01778 | 540 | issue = "44930")] |
a1dfa0c6 XL |
541 | impl sealed_trait::VaArgSafe for $t {} |
542 | )+ | |
543 | } | |
544 | } | |
545 | ||
60c5eb7d XL |
546 | impl_va_arg_safe! {i8, i16, i32, i64, usize} |
547 | impl_va_arg_safe! {u8, u16, u32, u64, isize} | |
548 | impl_va_arg_safe! {f64} | |
a1dfa0c6 | 549 | |
60c5eb7d XL |
550 | #[unstable( |
551 | feature = "c_variadic", | |
552 | reason = "the `c_variadic` feature has not been properly tested on \ | |
553 | all supported platforms", | |
554 | issue = "44930" | |
555 | )] | |
a1dfa0c6 | 556 | impl<T> sealed_trait::VaArgSafe for *mut T {} |
60c5eb7d XL |
557 | #[unstable( |
558 | feature = "c_variadic", | |
559 | reason = "the `c_variadic` feature has not been properly tested on \ | |
560 | all supported platforms", | |
561 | issue = "44930" | |
562 | )] | |
a1dfa0c6 XL |
563 | impl<T> sealed_trait::VaArgSafe for *const T {} |
564 | ||
60c5eb7d XL |
565 | #[unstable( |
566 | feature = "c_variadic", | |
567 | reason = "the `c_variadic` feature has not been properly tested on \ | |
568 | all supported platforms", | |
569 | issue = "44930" | |
570 | )] | |
dc9dc135 | 571 | impl<'f> VaListImpl<'f> { |
a1dfa0c6 | 572 | /// Advance to the next arg. |
dc9dc135 | 573 | #[inline] |
a1dfa0c6 | 574 | pub unsafe fn arg<T: sealed_trait::VaArgSafe>(&mut self) -> T { |
f035d41b XL |
575 | // SAFETY: the caller must uphold the safety contract for `va_arg`. |
576 | unsafe { va_arg(self) } | |
a1dfa0c6 XL |
577 | } |
578 | ||
9fa01778 | 579 | /// Copies the `va_list` at the current location. |
532ac7d7 | 580 | pub unsafe fn with_copy<F, R>(&self, f: F) -> R |
60c5eb7d XL |
581 | where |
582 | F: for<'copy> FnOnce(VaList<'copy, 'f>) -> R, | |
583 | { | |
dc9dc135 XL |
584 | let mut ap = self.clone(); |
585 | let ret = f(ap.as_va_list()); | |
f035d41b XL |
586 | // SAFETY: the caller must uphold the safety contract for `va_end`. |
587 | unsafe { | |
588 | va_end(&mut ap); | |
589 | } | |
a1dfa0c6 XL |
590 | ret |
591 | } | |
592 | } | |
593 | ||
60c5eb7d XL |
594 | #[unstable( |
595 | feature = "c_variadic", | |
596 | reason = "the `c_variadic` feature has not been properly tested on \ | |
597 | all supported platforms", | |
598 | issue = "44930" | |
599 | )] | |
dc9dc135 XL |
600 | impl<'f> Clone for VaListImpl<'f> { |
601 | #[inline] | |
602 | fn clone(&self) -> Self { | |
603 | let mut dest = crate::mem::MaybeUninit::uninit(); | |
60c5eb7d | 604 | // SAFETY: we write to the `MaybeUninit`, thus it is initialized and `assume_init` is legal |
dc9dc135 XL |
605 | unsafe { |
606 | va_copy(dest.as_mut_ptr(), self); | |
607 | dest.assume_init() | |
608 | } | |
609 | } | |
610 | } | |
611 | ||
60c5eb7d XL |
612 | #[unstable( |
613 | feature = "c_variadic", | |
614 | reason = "the `c_variadic` feature has not been properly tested on \ | |
615 | all supported platforms", | |
616 | issue = "44930" | |
617 | )] | |
dc9dc135 XL |
618 | impl<'f> Drop for VaListImpl<'f> { |
619 | fn drop(&mut self) { | |
620 | // FIXME: this should call `va_end`, but there's no clean way to | |
621 | // guarantee that `drop` always gets inlined into its caller, | |
622 | // so the `va_end` would get directly called from the same function as | |
623 | // the corresponding `va_copy`. `man va_end` states that C requires this, | |
624 | // and LLVM basically follows the C semantics, so we need to make sure | |
625 | // that `va_end` is always called from the same function as `va_copy`. | |
626 | // For more details, see https://github.com/rust-lang/rust/pull/59625 | |
627 | // and https://llvm.org/docs/LangRef.html#llvm-va-end-intrinsic. | |
628 | // | |
629 | // This works for now, since `va_end` is a no-op on all current LLVM targets. | |
630 | } | |
631 | } | |
632 | ||
a1dfa0c6 XL |
633 | extern "rust-intrinsic" { |
634 | /// Destroy the arglist `ap` after initialization with `va_start` or | |
635 | /// `va_copy`. | |
353b0b11 | 636 | #[rustc_nounwind] |
dc9dc135 | 637 | fn va_end(ap: &mut VaListImpl<'_>); |
a1dfa0c6 | 638 | |
9fa01778 | 639 | /// Copies the current location of arglist `src` to the arglist `dst`. |
353b0b11 | 640 | #[rustc_nounwind] |
dc9dc135 | 641 | fn va_copy<'f>(dest: *mut VaListImpl<'f>, src: &VaListImpl<'f>); |
a1dfa0c6 XL |
642 | |
643 | /// Loads an argument of type `T` from the `va_list` `ap` and increment the | |
644 | /// argument `ap` points to. | |
353b0b11 | 645 | #[rustc_nounwind] |
dc9dc135 | 646 | fn va_arg<T: sealed_trait::VaArgSafe>(ap: &mut VaListImpl<'_>) -> T; |
a1dfa0c6 | 647 | } |