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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! Helpers to introspect Rust code when generating bindings, mostly derived by the `#[ffi_...]` macros.

use crate::lang::c::{ArrayType, CType, Constant, ConstantValue, FnPointerType, Function, FunctionSignature, Parameter, PrimitiveType, PrimitiveValue, Variant};
use std::ptr::NonNull;

/// Implemented for a constant-helper produced by [`ffi_constant`](crate::ffi_constant), gives meta info for a constant.
///
/// # Safety
///
/// This trait must be implemented correctly, or else the generated bindings and this constant will
/// disagree in type or value, causing UB.
pub unsafe trait ConstantInfo {
    fn constant_info() -> Constant;
}

/// Implemented for a type via [`ffi_type`](crate::ffi_type), gives meta info for a type.
///
/// # Safety
///
/// This trait must be implemented correctly, or else the generated bindings disagree in
/// their type layout from the actual Rust type, leading to immediate UB upon function invocation.
pub unsafe trait CTypeInfo {
    fn type_info() -> CType;
}

/// Implemented for a function-helper produced by [`ffi_function`](crate::ffi_function), gives meta info for a function.
///
/// # Safety
///
/// This trait must be implemented correctly, or else the generated bindings signatures disagree from
/// their Rust counterparts, leading to immediate UB upon function invocation.
pub unsafe trait FunctionInfo {
    /// The function as a `fn` type.
    type Signature;

    // TODO: Code gen for this is total mess, ignore.
    // The Callback<> surrogate type to obtain proper CTypes when our extern "C" fn() can't be inferred
    // properly because of lifetimes.
    // type Surrogate;

    fn function_info() -> Function;
}

/// Implemented for an enum via [`ffi_type`](crate::ffi_type) allows us to translate a variant into its meta information.
///
/// # Safety
///
/// This trait must be implemented correctly, or else the generated bindings disagree on variant values
/// with their Rust counterparts, leading to UB when invoked with non-existent numbers.
pub unsafe trait VariantInfo {
    fn variant_info(&self) -> Variant;
}

macro_rules! impl_ctype_primitive {
    (
        $rust_type:ty,
        $primitive:expr
    ) => {
        unsafe impl CTypeInfo for $rust_type {
            fn type_info() -> CType {
                CType::Primitive($primitive)
            }
        }
    };
}

macro_rules! impl_const_value_primitive {
    (
        $rust_type:ty,
        $x:path
    ) => {
        impl From<$rust_type> for ConstantValue {
            fn from(x: $rust_type) -> Self {
                Self::Primitive($x(x))
            }
        }
    };
}

impl_const_value_primitive!(u8, PrimitiveValue::U8);
impl_const_value_primitive!(u16, PrimitiveValue::U16);
impl_const_value_primitive!(u32, PrimitiveValue::U32);
impl_const_value_primitive!(u64, PrimitiveValue::U64);
impl_const_value_primitive!(i8, PrimitiveValue::I8);
impl_const_value_primitive!(i16, PrimitiveValue::I16);
impl_const_value_primitive!(i32, PrimitiveValue::I32);
impl_const_value_primitive!(i64, PrimitiveValue::I64);
impl_const_value_primitive!(f32, PrimitiveValue::F32);
impl_const_value_primitive!(f64, PrimitiveValue::F64);
impl_const_value_primitive!(bool, PrimitiveValue::Bool);

impl_ctype_primitive!(std::ffi::c_void, PrimitiveType::Void);
impl_ctype_primitive!((), PrimitiveType::Void);
impl_ctype_primitive!(u8, PrimitiveType::U8);
impl_ctype_primitive!(u16, PrimitiveType::U16);
impl_ctype_primitive!(u32, PrimitiveType::U32);
impl_ctype_primitive!(u64, PrimitiveType::U64);
impl_ctype_primitive!(i8, PrimitiveType::I8);
impl_ctype_primitive!(i16, PrimitiveType::I16);
impl_ctype_primitive!(i32, PrimitiveType::I32);
impl_ctype_primitive!(i64, PrimitiveType::I64);
impl_ctype_primitive!(f32, PrimitiveType::F32);
impl_ctype_primitive!(f64, PrimitiveType::F64);
impl_ctype_primitive!(bool, PrimitiveType::Bool);
impl_ctype_primitive!(std::num::NonZeroU8, PrimitiveType::U8);
impl_ctype_primitive!(std::num::NonZeroU16, PrimitiveType::U16);
impl_ctype_primitive!(std::num::NonZeroU32, PrimitiveType::U32);
impl_ctype_primitive!(std::num::NonZeroU64, PrimitiveType::U64);
impl_ctype_primitive!(std::num::NonZeroI8, PrimitiveType::I8);
impl_ctype_primitive!(std::num::NonZeroI16, PrimitiveType::I16);
impl_ctype_primitive!(std::num::NonZeroI32, PrimitiveType::I32);
impl_ctype_primitive!(std::num::NonZeroI64, PrimitiveType::I64);
impl_ctype_primitive!(Option<std::num::NonZeroU8>, PrimitiveType::U8);
impl_ctype_primitive!(Option<std::num::NonZeroU16>, PrimitiveType::U16);
impl_ctype_primitive!(Option<std::num::NonZeroU32>, PrimitiveType::U32);
impl_ctype_primitive!(Option<std::num::NonZeroU64>, PrimitiveType::U64);
impl_ctype_primitive!(Option<std::num::NonZeroI8>, PrimitiveType::I8);
impl_ctype_primitive!(Option<std::num::NonZeroI16>, PrimitiveType::I16);
impl_ctype_primitive!(Option<std::num::NonZeroI32>, PrimitiveType::I32);
impl_ctype_primitive!(Option<std::num::NonZeroI64>, PrimitiveType::I64);

unsafe impl<T> CTypeInfo for NonNull<T>
where
    T: CTypeInfo,
{
    fn type_info() -> CType {
        CType::ReadWritePointer(Box::new(T::type_info()))
    }
}

unsafe impl<'a, T> CTypeInfo for &'a T
where
    T: CTypeInfo + Sized + 'static,
{
    fn type_info() -> CType {
        CType::ReadPointer(Box::new(T::type_info()))
    }
}

unsafe impl<'a, T> CTypeInfo for &'a mut T
where
    T: CTypeInfo + Sized + 'static,
{
    fn type_info() -> CType {
        CType::ReadWritePointer(Box::new(T::type_info()))
    }
}

unsafe impl<'a, T> CTypeInfo for *const T
where
    T: CTypeInfo + Sized + 'static,
{
    fn type_info() -> CType {
        CType::ReadPointer(Box::new(T::type_info()))
    }
}

unsafe impl<'a, T> CTypeInfo for *mut T
where
    T: CTypeInfo + Sized + 'static,
{
    fn type_info() -> CType {
        CType::ReadWritePointer(Box::new(T::type_info()))
    }
}

unsafe impl<'a, T> CTypeInfo for Option<&'a T>
where
    T: CTypeInfo + Sized + 'static,
{
    fn type_info() -> CType {
        CType::ReadPointer(Box::new(T::type_info()))
    }
}

unsafe impl<'a, T> CTypeInfo for Option<&'a mut T>
where
    T: CTypeInfo + Sized + 'static,
{
    fn type_info() -> CType {
        CType::ReadWritePointer(Box::new(T::type_info()))
    }
}

unsafe impl<R> CTypeInfo for extern "C" fn() -> R
where
    R: CTypeInfo,
{
    fn type_info() -> CType {
        let sig = FunctionSignature::new(vec![], R::type_info());
        CType::FnPointer(FnPointerType::new(sig))
    }
}

unsafe impl<R> CTypeInfo for Option<extern "C" fn() -> R>
where
    R: CTypeInfo,
{
    fn type_info() -> CType {
        let sig = FunctionSignature::new(vec![], R::type_info());
        CType::FnPointer(FnPointerType::new(sig))
    }
}

unsafe impl<T1, R> CTypeInfo for extern "C" fn(T1) -> R
where
    T1: CTypeInfo,
    R: CTypeInfo,
{
    fn type_info() -> CType {
        let sig = FunctionSignature::new(vec![Parameter::new("x0".to_string(), T1::type_info())], R::type_info());
        CType::FnPointer(FnPointerType::new(sig))
    }
}

unsafe impl<'a, T1, R> CTypeInfo for Option<extern "C" fn(T1) -> R>
where
    T1: CTypeInfo + 'a,
    R: CTypeInfo + 'a,
{
    fn type_info() -> CType {
        let sig = FunctionSignature::new(vec![Parameter::new("x0".to_string(), T1::type_info())], R::type_info());
        CType::FnPointer(FnPointerType::new(sig))
    }
}

unsafe impl<'a, T1, T2, R> CTypeInfo for extern "C" fn(T1, T2) -> R
where
    T1: CTypeInfo + 'a,
    T2: CTypeInfo + 'a,
    R: CTypeInfo + 'a,
{
    fn type_info() -> CType {
        let sig = FunctionSignature::new(
            vec![Parameter::new("x0".to_string(), T1::type_info()), Parameter::new("x1".to_string(), T1::type_info())],
            R::type_info(),
        );
        CType::FnPointer(FnPointerType::new(sig))
    }
}

unsafe impl<'a, T1, T2, R> CTypeInfo for Option<extern "C" fn(T1, T2) -> R>
where
    T1: CTypeInfo + 'a,
    T2: CTypeInfo + 'a,
    R: CTypeInfo + 'a,
{
    fn type_info() -> CType {
        let sig = FunctionSignature::new(
            vec![Parameter::new("x0".to_string(), T1::type_info()), Parameter::new("x1".to_string(), T1::type_info())],
            R::type_info(),
        );
        CType::FnPointer(FnPointerType::new(sig))
    }
}

unsafe impl<'a, T1, T2, T3, R> CTypeInfo for extern "C" fn(T1, T2, T3) -> R
where
    T1: CTypeInfo + 'a,
    T2: CTypeInfo + 'a,
    T3: CTypeInfo + 'a,
    R: CTypeInfo + 'a,
{
    fn type_info() -> CType {
        let sig = FunctionSignature::new(
            vec![
                Parameter::new("x0".to_string(), T1::type_info()),
                Parameter::new("x1".to_string(), T1::type_info()),
                Parameter::new("x2".to_string(), T2::type_info()),
            ],
            R::type_info(),
        );
        CType::FnPointer(FnPointerType::new(sig))
    }
}

unsafe impl<'a, T1, T2, T3, R> CTypeInfo for Option<extern "C" fn(T1, T2, T3) -> R>
where
    T1: CTypeInfo + 'a,
    T2: CTypeInfo + 'a,
    T3: CTypeInfo + 'a,
    R: CTypeInfo + 'a,
{
    fn type_info() -> CType {
        let sig = FunctionSignature::new(
            vec![
                Parameter::new("x0".to_string(), T1::type_info()),
                Parameter::new("x1".to_string(), T1::type_info()),
                Parameter::new("x2".to_string(), T2::type_info()),
            ],
            R::type_info(),
        );
        CType::FnPointer(FnPointerType::new(sig))
    }
}

unsafe impl<T, const N: usize> CTypeInfo for [T; N]
where
    T: CTypeInfo,
{
    fn type_info() -> CType {
        CType::Array(ArrayType::new(T::type_info(), N))
    }
}