From 4d89bfa001b5e95661d726bddb15841dc2dfd122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Matos?= Date: Tue, 1 Mar 2016 18:47:49 +0000 Subject: [PATCH] Fixed code generation for constant arrays of function pointers. Fixes https://github.com/mono/CppSharp/issues/632 and SDL sample. --- .../Generators/CSharp/CSharpTypePrinter.cs | 13 +++++++++++-- tests/Common/Common.h | 11 +++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index c73f6439..208411e6 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -126,8 +126,10 @@ namespace CppSharp.Generators.CSharp array.SizeType == ArrayType.ArraySize.Constant) { Type arrayType = array.Type.Desugar(); + PrimitiveType primitiveType; - if (arrayType.IsPointerToPrimitiveType(out primitiveType)) + if (arrayType.IsPointerToPrimitiveType(out primitiveType) && + !(arrayType is FunctionType)) { if (primitiveType == PrimitiveType.Void) { @@ -146,11 +148,18 @@ namespace CppSharp.Generators.CSharp }; } + var arrayElemType = array.Type.Visit(this, quals).ToString(); + + // C# does not support fixed arrays of machine pointer type (void* or IntPtr). + // In that case, replace it by a pointer to an integer type of the same size. + if (arrayElemType == IntPtrType) + arrayElemType = driver.TargetInfo.PointerWidth == 64 ? "long" : "int"; + // Do not write the fixed keyword multiple times for nested array types var fixedKeyword = array.Type is ArrayType ? string.Empty : "fixed "; return new CSharpTypePrinterResult() { - Type = string.Format("{0}{1}", fixedKeyword, array.Type.Visit(this, quals)), + Type = string.Format("{0}{1}", fixedKeyword, arrayElemType), NameSuffix = string.Format("[{0}]", array.Size) }; } diff --git a/tests/Common/Common.h b/tests/Common/Common.h index a68a718b..d1bdcb68 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -617,6 +617,17 @@ LPCWSTR TestWideStrings::GetWideNullPointer() { return 0; } enum struct MyEnum { A, B, C }; +typedef void (*VoidPtrRetFunctionTypedef) (); + +class DLL_API TestFixedArrays +{ +public: + TestFixedArrays(); + VoidPtrRetFunctionTypedef Array[10]; +}; + +TestFixedArrays::TestFixedArrays() {} + class DLL_API TestArraysPointers { public: