diff --git a/csbindgen-tests/src/lib.rs b/csbindgen-tests/src/lib.rs index 0b91338..c252b88 100644 --- a/csbindgen-tests/src/lib.rs +++ b/csbindgen-tests/src/lib.rs @@ -606,6 +606,9 @@ pub struct CallbackTable { pub foobar: extern "C" fn(i: i32) -> i32, } + +pub extern "C" fn reference_type(_a: &i32, _b: &*mut i32, _c: &[u8; 16], _d: &Context) {} + pub struct InternalHiddenContext { pub a: i32 } @@ -620,6 +623,7 @@ pub unsafe extern "C" fn init_treat_as_empty_struct_context(_out: NonNull RustType { type_kind: TypeKind::Function(parameters, ret), }; } + syn::Type::Reference(t) => { + let result = parse_type(&*t.elem); + let is_mut = t.mutability.is_some(); + + match result { + RustType { + type_kind: TypeKind::Pointer(pt, _), + .. + } => match pt { + PointerType::ConstPointer | PointerType::MutPointer => { + return RustType { + type_name: result.type_name, + type_kind: TypeKind::Pointer( + if is_mut { + PointerType::MutPointer + } else { + PointerType::ConstPointer + }, + Box::new(parse_type(&*t.elem)), + ), + }; + } + PointerType::ConstPointerPointer | PointerType::MutConstPointerPointer => { + return RustType { + type_name: result.type_name, + type_kind: TypeKind::Pointer( + if is_mut { + PointerType::MutConstPointerPointer + } else { + PointerType::ConstPointerPointer + }, + Box::new(parse_type(&*t.elem)), + ), + }; + } + PointerType::ConstMutPointerPointer | PointerType::MutPointerPointer => { + return RustType { + type_name: result.type_name, + type_kind: TypeKind::Pointer( + if is_mut { + PointerType::MutPointerPointer + } else { + PointerType::ConstMutPointerPointer + }, + Box::new(parse_type(&*t.elem)), + ), + }; + } + _ => {} + }, + _ => { + // &*t.elem is not pointer + return RustType { + type_name: result.type_name, + type_kind: TypeKind::Pointer( + PointerType::ConstPointer, + Box::new(parse_type(&*t.elem)), + ), + }; + } + } + } _ => {} }; diff --git a/dotnet-sandbox/NativeMethods.cs b/dotnet-sandbox/NativeMethods.cs index 0003c51..bd405a2 100644 --- a/dotnet-sandbox/NativeMethods.cs +++ b/dotnet-sandbox/NativeMethods.cs @@ -176,6 +176,10 @@ namespace CsBindgen [DllImport(__DllName, EntryPoint = "call_bindgen_lz4", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] public static extern void call_bindgen_lz4(); + + [DllImport(__DllName, EntryPoint = "reference_type", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void reference_type(int* _a, int** _b, void/* byte[] */* _c, Context* _d); + [DllImport(__DllName, EntryPoint = "init_treat_as_empty_struct_context", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] public static extern void init_treat_as_empty_struct_context(TreatAsEmptyStruct** _out); @@ -183,6 +187,7 @@ namespace CsBindgen public static extern void free_treat_as_empty_struct_context(TreatAsEmptyStruct* _src); + } [StructLayout(LayoutKind.Sequential)]