Browse Source

Fixed the generated C# for a case of a typedef of a function pointer.

Typedefs of function pointers can be written in two ways:

typedef void (*typedefedFuncPtr)();
int f(typedefedFuncPtr fptr);

typedef void (typedefedFuncPtr)();
int f(typedefedFuncPtr* fptr);

Up until now we only supported the former.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/890/head
Dimitar Dobrev 9 years ago
parent
commit
46b40bbe05
  1. 4
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 4
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  3. 3
      src/Generator/Passes/DelegatesPass.cs
  4. 4
      tests/CSharp/CSharp.Tests.cs
  5. 2
      tests/CSharp/CSharp.cpp
  6. 4
      tests/CSharp/CSharp.h

4
src/Generator/Generators/CSharp/CSharpSources.cs

@ -2959,9 +2959,9 @@ namespace CppSharp.Generators.CSharp
GenerateDeclarationCommon(typedef); GenerateDeclarationCommon(typedef);
FunctionType functionType; var functionType = typedef.Type as FunctionType;
if (typedef.Type.IsPointerTo(out functionType)) if (functionType != null || typedef.Type.IsPointerTo(out functionType))
{ {
PushBlock(BlockKind.Typedef); PushBlock(BlockKind.Typedef);
var attributedType = typedef.Type.GetPointee() as AttributedType; var attributedType = typedef.Type.GetPointee() as AttributedType;

4
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -320,8 +320,8 @@ namespace CppSharp.Generators.CSharp
} }
} }
FunctionType func; FunctionType func = decl.Type as FunctionType;
if (decl.Type.IsPointerTo(out func)) if (func != null || decl.Type.IsPointerTo(out func))
{ {
if (ContextKind == TypePrinterContextKind.Native) if (ContextKind == TypePrinterContextKind.Native)
return IntPtrType; return IntPtrType;

3
src/Generator/Passes/DelegatesPass.cs

@ -143,9 +143,10 @@ namespace CppSharp.Passes
private void VisitFunctionTypeParameters(Function function) private void VisitFunctionTypeParameters(Function function)
{ {
foreach (var param in from param in function.Parameters foreach (var param in from param in function.Parameters
where !(param.Type is TypedefType)
let paramType = param.Type.Desugar() let paramType = param.Type.Desugar()
where paramType.IsAddress() && where paramType.IsAddress() &&
paramType.GetFinalPointee().Desugar() is FunctionType paramType.GetPointee() is FunctionType
select param) select param)
{ {
var module = function.TranslationUnit.Module; var module = function.TranslationUnit.Module;

4
tests/CSharp/CSharp.Tests.cs

@ -856,10 +856,10 @@ public unsafe class CSharpTests : GeneratorTestFixture
} }
} }
[Test, Platform(Exclude = "Win", Reason = "This test crashes our Windows build, possibly a problem with the NUnit runner there.")] [Test]
public void TestFuncWithTypedefedFuncPtrAsParam() public void TestFuncWithTypedefedFuncPtrAsParam()
{ {
Func_int_IntPtr_CSharp_Bar___Internal function = (a, b) => 5; TypedefedFuncPtr function = (a, b) => 5;
Assert.That(CSharp.CSharp.FuncWithTypedefedFuncPtrAsParam(function), Is.EqualTo(5)); Assert.That(CSharp.CSharp.FuncWithTypedefedFuncPtrAsParam(function), Is.EqualTo(5));
} }
} }

2
tests/CSharp/CSharp.cpp

@ -1355,7 +1355,7 @@ void useStdStringJustAsParameter(std::string s)
{ {
} }
int funcWithTypedefedFuncPtrAsParam(typedefedFuncPtr *func) int funcWithTypedefedFuncPtrAsParam(typedefedFuncPtr* func)
{ {
Foo* a = 0; Foo* a = 0;
Bar b; Bar b;

4
tests/CSharp/CSharp.h

@ -1218,5 +1218,5 @@ extern const ComplexArrayElement ArrayOfVariableSize[];
void useStdStringJustAsParameter(std::string s); void useStdStringJustAsParameter(std::string s);
typedef int (typedefedFuncPtr)(Foo *a, Bar b); typedef int (typedefedFuncPtr)(Foo* a, Bar b);
int DLL_API funcWithTypedefedFuncPtrAsParam(typedefedFuncPtr *func); int DLL_API funcWithTypedefedFuncPtrAsParam(typedefedFuncPtr* func);

Loading…
Cancel
Save