xref: /rust-libc-0.2.174/src/macros.rs (revision 7f86bf8d)
1 /// A macro for defining #[cfg] if-else statements.
2 ///
3 /// This is similar to the `if/elif` C preprocessor macro by allowing definition
4 /// of a cascade of `#[cfg]` cases, emitting the implementation which matches
5 /// first.
6 ///
7 /// This allows you to conveniently provide a long list #[cfg]'d blocks of code
8 /// without having to rewrite each clause multiple times.
9 macro_rules! cfg_if {
10     // match if/else chains with a final `else`
11     ($(
12         if #[cfg($($meta:meta),*)] { $($it:item)* }
13     ) else * else {
14         $($it2:item)*
15     }) => {
16         cfg_if! {
17             @__items
18             () ;
19             $( ( ($($meta),*) ($($it)*) ), )*
20             ( () ($($it2)*) ),
21         }
22     };
23 
24     // match if/else chains lacking a final `else`
25     (
26         if #[cfg($($i_met:meta),*)] { $($i_it:item)* }
27         $(
28             else if #[cfg($($e_met:meta),*)] { $($e_it:item)* }
29         )*
30     ) => {
31         cfg_if! {
32             @__items
33             () ;
34             ( ($($i_met),*) ($($i_it)*) ),
35             $( ( ($($e_met),*) ($($e_it)*) ), )*
36             ( () () ),
37         }
38     };
39 
40     // Internal and recursive macro to emit all the items
41     //
42     // Collects all the negated `cfg`s in a list at the beginning and after the
43     // semicolon is all the remaining items
44     (@__items ($($not:meta,)*) ; ) => {};
45     (@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ),
46      $($rest:tt)*) => {
47         // Emit all items within one block, applying an appropriate #[cfg]. The
48         // #[cfg] will require all `$m` matchers specified and must also negate
49         // all previous matchers.
50         cfg_if! { @__apply cfg(all($($m,)* not(any($($not),*)))), $($it)* }
51 
52         // Recurse to emit all other items in `$rest`, and when we do so add all
53         // our `$m` matchers to the list of `$not` matchers as future emissions
54         // will have to negate everything we just matched as well.
55         cfg_if! { @__items ($($not,)* $($m,)*) ; $($rest)* }
56     };
57 
58     // Internal macro to Apply a cfg attribute to a list of items
59     (@__apply $m:meta, $($it:item)*) => {
60         $(#[$m] $it)*
61     };
62 }
63 
64 /// Create an internal crate prelude with `core` reexports and common types.
65 macro_rules! prelude {
66     () => {
67         /// Frequently-used types that are available on all platforms
68         ///
69         /// We need to reexport the core types so this works with `rust-dep-of-std`.
70         mod prelude {
71             // Exports from `core`
72             #[allow(unused_imports)]
73             pub(crate) use ::core::clone::Clone;
74             #[allow(unused_imports)]
75             pub(crate) use ::core::marker::{Copy, Send, Sync};
76             #[allow(unused_imports)]
77             pub(crate) use ::core::option::Option;
78             #[allow(unused_imports)]
79             pub(crate) use ::core::{fmt, hash, iter, mem};
80 
81             // Commonly used types defined in this crate
82             #[allow(unused_imports)]
83             pub(crate) use crate::{
84                 c_char, c_double, c_float, c_int, c_long, c_longlong, c_short, c_uchar, c_uint,
85                 c_ulong, c_ulonglong, c_ushort, c_void, intptr_t, size_t, ssize_t, uintptr_t,
86             };
87         }
88     };
89 }
90 
91 /// Implement `Clone` and `Copy` for a struct, as well as `Debug`, `Eq`, `Hash`, and
92 /// `PartialEq` if the `extra_traits` feature is enabled.
93 ///
94 /// Use [`s_no_extra_traits`] for structs where the `extra_traits` feature does not
95 /// make sense, and for unions.
96 macro_rules! s {
97     ($(
98         $(#[$attr:meta])*
99         pub $t:ident $i:ident { $($field:tt)* }
100     )*) => ($(
101         s!(it: $(#[$attr])* pub $t $i { $($field)* });
102     )*);
103 
104     (it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => (
105         compile_error!("unions cannot derive extra traits, use s_no_extra_traits instead");
106     );
107 
108     (it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => (
109         __item! {
110             #[repr(C)]
111             #[cfg_attr(
112                 feature = "extra_traits",
113                 ::core::prelude::v1::derive(Debug, Eq, Hash, PartialEq)
114             )]
115             #[::core::prelude::v1::derive(::core::clone::Clone, ::core::marker::Copy)]
116             #[allow(deprecated)]
117             $(#[$attr])*
118             pub struct $i { $($field)* }
119         }
120     );
121 }
122 
123 /// Implement `Clone` and `Copy` for a tuple struct, as well as `Debug`, `Eq`, `Hash`,
124 /// and `PartialEq` if the `extra_traits` feature is enabled.
125 ///
126 /// This is the same as [`s`] but works for tuple structs.
127 macro_rules! s_paren {
128     ($(
129         $(#[$attr:meta])*
130         pub struct $i:ident ( $($field:tt)* );
131     )*) => ($(
132         __item! {
133             #[cfg_attr(
134                 feature = "extra_traits",
135                 ::core::prelude::v1::derive(Debug, Eq, Hash, PartialEq)
136             )]
137             #[::core::prelude::v1::derive(::core::clone::Clone, ::core::marker::Copy)]
138             $(#[$attr])*
139             pub struct $i ( $($field)* );
140         }
141     )*);
142 }
143 
144 /// Implement `Clone` and `Copy` for a struct with no `extra_traits` feature.
145 ///
146 /// Most items will prefer to use [`s`].
147 macro_rules! s_no_extra_traits {
148     ($(
149         $(#[$attr:meta])*
150         pub $t:ident $i:ident { $($field:tt)* }
151     )*) => ($(
152         s_no_extra_traits!(it: $(#[$attr])* pub $t $i { $($field)* });
153     )*);
154 
155     (it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => (
156         __item! {
157             #[repr(C)]
158             #[::core::prelude::v1::derive(::core::clone::Clone, ::core::marker::Copy)]
159             $(#[$attr])*
160             pub union $i { $($field)* }
161         }
162 
163         #[cfg(feature = "extra_traits")]
164         impl ::core::fmt::Debug for $i {
165             fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
166                 f.debug_struct(::core::stringify!($i)).finish_non_exhaustive()
167             }
168         }
169     );
170 
171     (it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => (
172         __item! {
173             #[repr(C)]
174             #[::core::prelude::v1::derive(::core::clone::Clone, ::core::marker::Copy)]
175             $(#[$attr])*
176             pub struct $i { $($field)* }
177         }
178     );
179 }
180 
181 /// Specify that an enum should have no traits that aren't specified in the macro
182 /// invocation, i.e. no `Clone` or `Copy`.
183 macro_rules! missing {
184     ($(
185         $(#[$attr:meta])*
186         pub enum $i:ident {}
187     )*) => ($(
188         $(#[$attr])*
189         #[allow(missing_copy_implementations)]
190         pub enum $i { }
191     )*);
192 }
193 
194 /// Implement `Clone` and `Copy` for an enum, as well as `Debug`, `Eq`, `Hash`, and
195 /// `PartialEq` if the `extra_traits` feature is enabled.
196 macro_rules! e {
197     ($(
198         $(#[$attr:meta])*
199         pub enum $i:ident { $($field:tt)* }
200     )*) => ($(
201         __item! {
202             #[cfg_attr(
203                 feature = "extra_traits",
204                 ::core::prelude::v1::derive(Debug, Eq, Hash, PartialEq)
205             )]
206             #[::core::prelude::v1::derive(::core::clone::Clone, ::core::marker::Copy)]
207             $(#[$attr])*
208             pub enum $i { $($field)* }
209         }
210     )*);
211 }
212 
213 // This is a pretty horrible hack to allow us to conditionally mark some functions as 'const',
214 // without requiring users of this macro to care "libc_const_extern_fn".
215 //
216 // When 'libc_const_extern_fn' is enabled, we emit the captured 'const' keyword in the expanded
217 // function.
218 //
219 // When 'libc_const_extern_fn' is disabled, we always emit a plain 'pub unsafe extern fn'.
220 // Note that the expression matched by the macro is exactly the same - this allows
221 // users of this macro to work whether or not 'libc_const_extern_fn' is enabled
222 //
223 // Unfortunately, we need to duplicate most of this macro between the 'cfg_if' blocks.
224 // This is because 'const unsafe extern fn' won't even parse on older compilers,
225 // so we need to avoid emitting it at all of 'libc_const_extern_fn'.
226 //
227 // Specifically, moving the 'cfg_if' into the macro body will *not* work. Doing so would cause the
228 // '#[cfg(libc_const_extern_fn)]' to be emitted into user code. The 'cfg' gate will not stop Rust
229 // from trying to parse the 'pub const unsafe extern fn', so users would get a compiler error even
230 // when the 'libc_const_extern_fn' feature is disabled.
231 
232 // FIXME(ctest): ctest can't handle `const extern` functions, we should be able to remove this
233 // cfg completely.
234 // FIXME(ctest): ctest can't handle `$(,)?` so we use `$(,)*` which isn't quite correct.
235 cfg_if! {
236     if #[cfg(libc_const_extern_fn)] {
237         /// Define an `unsafe` function that is const as long as `libc_const_extern_fn` is enabled.
238         macro_rules! f {
239             ($(
240                 $(#[$attr:meta])*
241                 pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
242                     $body:block
243             )*) => ($(
244                 #[inline]
245                 $(#[$attr])*
246                 pub $($constness)* unsafe extern "C" fn $i($($arg: $argty),*) -> $ret
247                     $body
248             )*)
249         }
250 
251         /// Define a safe function that is const as long as `libc_const_extern_fn` is enabled.
252         macro_rules! safe_f {
253             ($(
254                 $(#[$attr:meta])*
255                 pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
256                     $body:block
257             )*) => ($(
258                 #[inline]
259                 $(#[$attr])*
260                 pub $($constness)* extern "C" fn $i($($arg: $argty),*) -> $ret
261                     $body
262             )*)
263         }
264 
265         /// A nonpublic function that is const as long as `libc_const_extern_fn` is enabled.
266         macro_rules! const_fn {
267             ($(
268                 $(#[$attr:meta])*
269                 $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
270                     $body:block
271             )*) => ($(
272                 #[inline]
273                 $(#[$attr])*
274                 $($constness)* fn $i($($arg: $argty),*) -> $ret
275                     $body
276             )*)
277         }
278     } else {
279         /// Define an `unsafe` function that is const as long as `libc_const_extern_fn` is enabled.
280         macro_rules! f {
281             ($(
282                 $(#[$attr:meta])*
283                 pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
284                     $body:block
285             )*) => ($(
286                 #[inline]
287                 $(#[$attr])*
288                 pub unsafe extern "C" fn $i($($arg: $argty),*) -> $ret
289                     $body
290             )*)
291         }
292 
293         /// Define a safe function that is const as long as `libc_const_extern_fn` is enabled.
294         macro_rules! safe_f {
295             ($(
296                 $(#[$attr:meta])*
297                 pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
298                     $body:block
299             )*) => ($(
300                 #[inline]
301                 $(#[$attr])*
302                 pub extern "C" fn $i($($arg: $argty),*) -> $ret
303                     $body
304             )*)
305         }
306 
307         /// A nonpublic function that is const as long as `libc_const_extern_fn` is enabled.
308         macro_rules! const_fn {
309             ($(
310                 $(#[$attr:meta])*
311                 $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
312                     $body:block
313             )*) => ($(
314                 #[inline]
315                 $(#[$attr])*
316                 fn $i($($arg: $argty),*) -> $ret
317                     $body
318             )*)
319         }
320     }
321 }
322 
323 macro_rules! __item {
324     ($i:item) => {
325         $i
326     };
327 }
328 
329 // This macro is used to deprecate items that should be accessed via the mach2 crate
330 macro_rules! deprecated_mach {
331     (pub const $id:ident: $ty:ty = $expr:expr;) => {
332         #[deprecated(
333             since = "0.2.55",
334             note = "Use the `mach2` crate instead",
335         )]
336         #[allow(deprecated)]
337         pub const $id: $ty = $expr;
338     };
339     ($(pub const $id:ident: $ty:ty = $expr:expr;)*) => {
340         $(
341             deprecated_mach!(
342                 pub const $id: $ty = $expr;
343             );
344         )*
345     };
346     (pub type $id:ident = $ty:ty;) => {
347         #[deprecated(
348             since = "0.2.55",
349             note = "Use the `mach2` crate instead",
350         )]
351         #[allow(deprecated)]
352         pub type $id = $ty;
353     };
354     ($(pub type $id:ident = $ty:ty;)*) => {
355         $(
356             deprecated_mach!(
357                 pub type $id = $ty;
358             );
359         )*
360     }
361 }
362