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