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
#![no_std]
#![allow(non_camel_case_types)]

//! This just provides the numeric C types, for basic FFI purposes.
//!
//! Also, that [`pick!`] macro is nifty.

/// Does all our conditional compilation selection.
#[macro_export]
macro_rules! pick {
  // with a trailing else
  ($(if #[cfg($($test:meta),*)] {
      $($if_tokens:tt)*
    })else+ else {
      $($else_tokens:tt)*
    }) => {
    $crate::pick!{
      @__forests [ ] ;
      $( [ {$($test),*} {$($if_tokens)*} ], )*
      [ { } {$($else_tokens)*} ],
    }
  };

  // without a trailing else
  (if #[cfg($($if_meta:meta),*)] {
      $($if_tokens:tt)*
    } $(else if #[cfg($($else_meta:meta),*)] {
      $($else_tokens:tt)*
    })*) => {
    $crate::pick!{
      @__forests [ ] ;
      [ {$($if_meta),*} {$($if_tokens)*} ],
      $( [ {$($else_meta),*} {$($else_tokens)*} ], )*
    }
  };

  // private
  (@__forests [$($not:meta,)*];) => {
    /* halt expansion */
  };

  // private
  (@__forests [$($not:meta,)*]; [{$($m:meta),*} {$($tokens:tt)*}], $($rest:tt)*) => {
    // This "one weird trick" works because you can't apply a `cfg` to an
    // expression, only an item or a block, but a macro usage is an item, so
    // we're configuring the macro usage, which (if configured in) will then
    // contain a token tree that turns into either an item or an expression.
    #[cfg(all( $($m,)* not(any($($not),*)) ))]
    $crate::pick!{ @__identity $($tokens)* }

    $crate::pick!{ @__forests [ $($not,)* $($m,)* ] ; $($rest)* }
  };

  // private
  (@__identity $($tokens:tt)*) => {
    $($tokens)*
  };
}

pub use core::ffi::c_void;

pick! {
  // c_char
  if #[cfg(any(
    all(
      target_os = "linux",
      any(
        target_arch = "aarch64",
        target_arch = "arm",
        target_arch = "hexagon",
        target_arch = "powerpc",
        target_arch = "powerpc64",
        target_arch = "s390x",
        target_arch = "riscv64",
        target_arch = "riscv32"
      )
    ),
    all(
      target_os = "android",
      any(target_arch = "aarch64", target_arch = "arm")
    ),
    all(target_os = "l4re", target_arch = "x86_64"),
    all(
      target_os = "freebsd",
      any(
        target_arch = "aarch64",
        target_arch = "arm",
        target_arch = "powerpc",
        target_arch = "powerpc64"
      )
    ),
    all(
      target_os = "netbsd",
      any(target_arch = "aarch64", target_arch = "arm", target_arch = "powerpc")
    ),
    all(target_os = "openbsd", target_arch = "aarch64"),
    all(
      target_os = "vxworks",
      any(
        target_arch = "aarch64",
        target_arch = "arm",
        target_arch = "powerpc64",
        target_arch = "powerpc"
      )
    ),
    all(target_os = "fuchsia", target_arch = "aarch64")
  ))] {
    pub type c_char = c_uchar;
  } else if #[cfg(not(any(
    all(
      target_os = "linux",
      any(
        target_arch = "aarch64",
        target_arch = "arm",
        target_arch = "hexagon",
        target_arch = "powerpc",
        target_arch = "powerpc64",
        target_arch = "s390x",
        target_arch = "riscv64",
        target_arch = "riscv32"
      )
    ),
    all(
      target_os = "android",
      any(target_arch = "aarch64", target_arch = "arm")
    ),
    all(target_os = "l4re", target_arch = "x86_64"),
    all(
      target_os = "freebsd",
      any(
        target_arch = "aarch64",
        target_arch = "arm",
        target_arch = "powerpc",
        target_arch = "powerpc64"
      )
    ),
    all(
      target_os = "netbsd",
      any(target_arch = "aarch64", target_arch = "arm", target_arch = "powerpc")
    ),
    all(target_os = "openbsd", target_arch = "aarch64"),
    all(
      target_os = "vxworks",
      any(
        target_arch = "aarch64",
        target_arch = "arm",
        target_arch = "powerpc64",
        target_arch = "powerpc"
      )
    ),
    all(target_os = "fuchsia", target_arch = "aarch64")
  )))] {
    pub type c_char = c_schar;
  } else {
    compile_error!("The format of c_char is unknown!");
  }
}

pub type c_schar = i8;

pub type c_uchar = u8;

pub type c_short = i16;

pub type c_ushort = u16;

pick! {
  // c_int / c_uint
  if #[cfg(any(
    target_arch = "aarch64",
    target_arch = "arm",
    target_arch = "asmjs",
    target_arch = "wasm32",
    target_arch = "wasm64",
    target_arch = "powerpc",
    target_arch = "powerpc64",
    target_arch = "s390x",
    target_arch = "riscv32",
    target_arch = "riscv64",
    target_arch = "mips",
    target_arch = "mips64",
    target_arch = "sparc64",
    target_arch = "x86",
    target_arch = "x86_64",
    target_arch = "nvptx",
    target_arch = "nvptx64",
    target_arch = "xtensa",
  ))] {
    pub type c_int = i32;
    pub type c_uint = u32;
  } else if #[cfg(target_arch = "msp430")] {
    pub type c_int = i16;
    pub type c_uint = u16;
  } else {
    compile_error!("The format of c_int / c_uint is unknown!");
  }
}

pick! {
  // c_long / c_ulong
  if #[cfg(windows)] {
    pub type c_long = i32;
    pub type c_ulong = u32;
  } else if #[cfg(any(target_os = "redox", target_os = "solaris"))] {
    pub type c_long = i64;
    pub type c_ulong = u64;
  } else if #[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))] {
    pub type c_long = i32;
    pub type c_ulong = u32;
  } else if #[cfg(target_pointer_width = "64")] {
    pub type c_long = i64;
    pub type c_ulong = u64;
  } else {
    compile_error!("The format of c_long / c_ulong is unknown!");
  }
}

pub type c_longlong = i64;

pub type c_ulonglong = u64;

pub type c_float = f32;

pub type c_double = f64;

pick! {
  if #[cfg(feature = "int_extras")] {
    pub type intmax_t = i64;
    pub type intptr_t = isize;
    pub type ptrdiff_t = isize;
    pub type size_t = usize;
    pub type ssize_t = isize;
    pub type uintmax_t = u64;
    pub type uintptr_t = usize;
  }
}