From 3fe9084e47e38245c4a76bda8e073c50753755bd Mon Sep 17 00:00:00 2001 From: yamachu Date: Wed, 6 Sep 2023 22:27:06 +0900 Subject: [PATCH 1/2] Add reference type parser --- csbindgen/src/parser.rs | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/csbindgen/src/parser.rs b/csbindgen/src/parser.rs index bdfba17..5c918d1 100644 --- a/csbindgen/src/parser.rs +++ b/csbindgen/src/parser.rs @@ -476,6 +476,68 @@ fn parse_type(t: &syn::Type) -> 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)), + ), + }; + } + } + } _ => {} }; From 8a047b31c559e7002f8d2f26b615c2dc15b5275a Mon Sep 17 00:00:00 2001 From: yamachu Date: Wed, 6 Sep 2023 22:27:51 +0900 Subject: [PATCH 2/2] Add reference type sample case --- csbindgen-tests/src/lib.rs | 2 ++ dotnet-sandbox/NativeMethods.cs | 3 +++ 2 files changed, 5 insertions(+) diff --git a/csbindgen-tests/src/lib.rs b/csbindgen-tests/src/lib.rs index 4983013..7bd3bc0 100644 --- a/csbindgen-tests/src/lib.rs +++ b/csbindgen-tests/src/lib.rs @@ -606,6 +606,8 @@ 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) {} + // fn run_physix(){ // unsafe { // let foundation = physx_create_foundation(); diff --git a/dotnet-sandbox/NativeMethods.cs b/dotnet-sandbox/NativeMethods.cs index 01e7a4b..ef6c335 100644 --- a/dotnet-sandbox/NativeMethods.cs +++ b/dotnet-sandbox/NativeMethods.cs @@ -176,6 +176,9 @@ 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); + }