xref: /rust-libc-0.2.174/src/macros.rs (revision 036c68ff)
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 /// Implement `Clone` and `Copy` for a struct, as well as `Debug`, `Eq`, `Hash`, and
65 /// `PartialEq` if the `extra_traits` feature is enabled.
66 ///
67 /// Use [`s_no_extra_traits`] for structs where the `extra_traits` feature does not
68 /// make sense, and for unions.
69 macro_rules! s {
70     ($(
71         $(#[$attr:meta])*
72         pub $t:ident $i:ident { $($field:tt)* }
73     )*) => ($(
74         s!(it: $(#[$attr])* pub $t $i { $($field)* });
75     )*);
76 
77     (it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => (
78         compile_error!("unions cannot derive extra traits, use s_no_extra_traits instead");
79     );
80 
81     (it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => (
82         __item! {
83             #[repr(C)]
84             #[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))]
85             #[derive(Copy, Clone)]
86             #[allow(deprecated)]
87             $(#[$attr])*
88             pub struct $i { $($field)* }
89         }
90     );
91 }
92 
93 /// Implement `Clone` and `Copy` for a tuple struct, as well as `Debug`, `Eq`, `Hash`,
94 /// and `PartialEq` if the `extra_traits` feature is enabled.
95 ///
96 /// This is the same as [`s`] but works for tuple structs.
97 macro_rules! s_paren {
98     ($(
99         $(#[$attr:meta])*
100         pub struct $i:ident ( $($field:tt)* );
101     )*) => ($(
102         __item! {
103             #[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))]
104             #[derive(Copy, Clone)]
105             $(#[$attr])*
106             pub struct $i ( $($field)* );
107         }
108     )*);
109 }
110 
111 /// Implement `Clone` and `Copy` for a struct with no `extra_traits` feature.
112 ///
113 /// Most items will prefer to use [`s`].
114 macro_rules! s_no_extra_traits {
115     ($(
116         $(#[$attr:meta])*
117         pub $t:ident $i:ident { $($field:tt)* }
118     )*) => ($(
119         s_no_extra_traits!(it: $(#[$attr])* pub $t $i { $($field)* });
120     )*);
121 
122     (it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => (
123         __item! {
124             #[repr(C)]
125             #[derive(Copy, Clone)]
126             $(#[$attr])*
127             pub union $i { $($field)* }
128         }
129     );
130 
131     (it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => (
132         __item! {
133             #[repr(C)]
134             #[derive(Copy, Clone)]
135             $(#[$attr])*
136             pub struct $i { $($field)* }
137         }
138     );
139 }
140 
141 /// Specify that an enum should have no traits that aren't specified in the macro
142 /// invocation, i.e. no `Clone` or `Copy`.
143 macro_rules! missing {
144     ($(
145         $(#[$attr:meta])*
146         pub enum $i:ident {}
147     )*) => ($(
148         $(#[$attr])*
149         #[allow(missing_copy_implementations)]
150         pub enum $i { }
151     )*);
152 }
153 
154 /// Implement `Clone` and `Copy` for an enum, as well as `Debug`, `Eq`, `Hash`, and
155 /// `PartialEq` if the `extra_traits` feature is enabled.
156 macro_rules! e {
157     ($(
158         $(#[$attr:meta])*
159         pub enum $i:ident { $($field:tt)* }
160     )*) => ($(
161         __item! {
162             #[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))]
163             #[derive(Copy, Clone)]
164             $(#[$attr])*
165             pub enum $i { $($field)* }
166         }
167     )*);
168 }
169 
170 // This is a pretty horrible hack to allow us to conditionally mark
171 // some functions as 'const', without requiring users of this macro
172 // to care about the "const-extern-fn" feature.
173 //
174 // When 'const-extern-fn' is enabled, we emit the captured 'const' keyword
175 // in the expanded function.
176 //
177 // When 'const-extern-fn' is disabled, we always emit a plain 'pub unsafe extern fn'.
178 // Note that the expression matched by the macro is exactly the same - this allows
179 // users of this macro to work whether or not 'const-extern-fn' is enabled
180 //
181 // Unfortunately, we need to duplicate most of this macro between the 'cfg_if' blocks.
182 // This is because 'const unsafe extern fn' won't even parse on older compilers,
183 // so we need to avoid emitting it at all of 'const-extern-fn'.
184 //
185 // Specifically, moving the 'cfg_if' into the macro body will *not* work.
186 // Doing so would cause the '#[cfg(feature = "const-extern-fn")]' to be emitted
187 // into user code. The 'cfg' gate will not stop Rust from trying to parse the
188 // 'pub const unsafe extern fn', so users would get a compiler error even when
189 // the 'const-extern-fn' feature is disabled
190 //
191 // Note that users of this macro need to place 'const' in a weird position
192 // (after the closing ')' for the arguments, but before the return type).
193 // This was the only way I could satisfy the following two requirements:
194 // 1. Avoid ambiguity errors from 'macro_rules!' (which happen when writing '$foo:ident fn'
195 // 2. Allow users of this macro to mix 'pub fn foo' and 'pub const fn bar' within the same
196 // 'f!' block
197 
198 // FIXME(ctest): ctest can't handle `const extern` functions, we should be able to remove this
199 // cfg completely.
200 // FIXME(ctest): ctest can't handle `$(,)?` so we use `$(,)*` which isn't quite correct.
201 cfg_if! {
202     if #[cfg(feature = "const-extern-fn")] {
203         /// Define an `unsafe` function that is const as long as `const-extern-fn` is enabled.
204         macro_rules! f {
205             ($(
206                 $(#[$attr:meta])*
207                 pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
208                     $body:block
209             )*) => ($(
210                 #[inline]
211                 $(#[$attr])*
212                 pub $($constness)* unsafe extern fn $i($($arg: $argty),*) -> $ret
213                     $body
214             )*)
215         }
216 
217         /// Define a safe function that is const as long as `const-extern-fn` is enabled.
218         macro_rules! safe_f {
219             ($(
220                 $(#[$attr:meta])*
221                 pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
222                     $body:block
223             )*) => ($(
224                 #[inline]
225                 $(#[$attr])*
226                 pub $($constness)* extern fn $i($($arg: $argty),*) -> $ret
227                     $body
228             )*)
229         }
230 
231         /// A nonpublic function that is const as long as `const-extern-fn` is enabled.
232         macro_rules! const_fn {
233             ($(
234                 $(#[$attr:meta])*
235                 $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
236                     $body:block
237             )*) => ($(
238                 #[inline]
239                 $(#[$attr])*
240                 $($constness)* fn $i($($arg: $argty),*) -> $ret
241                     $body
242             )*)
243         }
244     } else {
245         /// Define an `unsafe` function that is const as long as `const-extern-fn` is enabled.
246         macro_rules! f {
247             ($(
248                 $(#[$attr:meta])*
249                 pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
250                     $body:block
251             )*) => ($(
252                 #[inline]
253                 $(#[$attr])*
254                 pub unsafe extern fn $i($($arg: $argty),*) -> $ret
255                     $body
256             )*)
257         }
258 
259         /// Define a safe function that is const as long as `const-extern-fn` is enabled.
260         macro_rules! safe_f {
261             ($(
262                 $(#[$attr:meta])*
263                 pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
264                     $body:block
265             )*) => ($(
266                 #[inline]
267                 $(#[$attr])*
268                 pub extern fn $i($($arg: $argty),*) -> $ret
269                     $body
270             )*)
271         }
272 
273         /// A nonpublic function that is const as long as `const-extern-fn` is enabled.
274         macro_rules! const_fn {
275             ($(
276                 $(#[$attr:meta])*
277                 $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
278                     $body:block
279             )*) => ($(
280                 #[inline]
281                 $(#[$attr])*
282                 fn $i($($arg: $argty),*) -> $ret
283                     $body
284             )*)
285         }
286     }
287 }
288 
289 macro_rules! __item {
290     ($i:item) => {
291         $i
292     };
293 }
294 
295 // This macro is used to deprecate items that should be accessed via the mach2 crate
296 macro_rules! deprecated_mach {
297     (pub const $id:ident: $ty:ty = $expr:expr;) => {
298         #[deprecated(
299             since = "0.2.55",
300             note = "Use the `mach2` crate instead",
301         )]
302         #[allow(deprecated)]
303         pub const $id: $ty = $expr;
304     };
305     ($(pub const $id:ident: $ty:ty = $expr:expr;)*) => {
306         $(
307             deprecated_mach!(
308                 pub const $id: $ty = $expr;
309             );
310         )*
311     };
312     (pub type $id:ident = $ty:ty;) => {
313         #[deprecated(
314             since = "0.2.55",
315             note = "Use the `mach2` crate instead",
316         )]
317         #[allow(deprecated)]
318         pub type $id = $ty;
319     };
320     ($(pub type $id:ident = $ty:ty;)*) => {
321         $(
322             deprecated_mach!(
323                 pub type $id = $ty;
324             );
325         )*
326     }
327 }
328