Browse Source

Fixed the binding of multiple identical function pointers with a calling convention.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/924/head
Dimitar Dobrev 8 years ago
parent
commit
046c428852
  1. 9
      src/AST/CppTypePrinter.cs
  2. 6
      src/Generator/Generators/CLI/CLIMarshal.cs
  3. 5
      src/Generator/Passes/DelegatesPass.cs
  4. 4
      tests/Common/Common.Tests.cs
  5. 10
      tests/Common/Common.cpp
  6. 2
      tests/Common/Common.h

9
src/AST/CppTypePrinter.cs

@ -89,7 +89,14 @@ namespace CppSharp.AST @@ -89,7 +89,14 @@ namespace CppSharp.AST
if (arguments.Count > 0)
args = VisitParameters(function.Parameters, hasNames: false);
return string.Format("{0} (*)({1})", returnType.Visit(this), args);
var callingConvention = string.Empty;
if (function.CallingConvention != CallingConvention.Default &&
function.CallingConvention != CallingConvention.C)
{
string conventionString = function.CallingConvention.ToString();
callingConvention = $"__{conventionString.ToLowerInvariant()} ";
}
return $"{returnType.Visit(this)} ({callingConvention}*)({args})";
}
var qual = GetStringQuals(quals, false);

6
src/Generator/Generators/CLI/CLIMarshal.cs

@ -488,7 +488,7 @@ namespace CppSharp.Generators.CLI @@ -488,7 +488,7 @@ namespace CppSharp.Generators.CLI
return returnType.Visit(this);
}
public bool VisitDelegateType(FunctionType function, string type)
public bool VisitDelegateType(string type)
{
// We marshal function pointer types by calling
// GetFunctionPointerForDelegate to get a native function
@ -533,7 +533,7 @@ namespace CppSharp.Generators.CLI @@ -533,7 +533,7 @@ namespace CppSharp.Generators.CLI
var cppTypePrinter = new CppTypePrinter();
var cppTypeName = pointer.Visit(cppTypePrinter, quals);
return VisitDelegateType(function, cppTypeName);
return VisitDelegateType(cppTypeName);
}
Enumeration @enum;
@ -631,7 +631,7 @@ namespace CppSharp.Generators.CLI @@ -631,7 +631,7 @@ namespace CppSharp.Generators.CLI
cppTypeName = decl.Type.Visit(cppTypePrinter, quals);
}
VisitDelegateType(func, cppTypeName);
VisitDelegateType(cppTypeName);
return true;
}

5
src/Generator/Passes/DelegatesPass.cs

@ -115,7 +115,10 @@ namespace CppSharp.Passes @@ -115,7 +115,10 @@ namespace CppSharp.Passes
return existingDelegate;
// Add a new delegate with the calling convention appended to its name
delegateName += newFunctionType.CallingConvention;
delegateName += '_' + newFunctionType.CallingConvention.ToString();
existingDelegate = delegates.SingleOrDefault(t => t.Name == delegateName);
if (existingDelegate != null)
return existingDelegate;
}
var namespaceDelegates = GetDeclContextForDelegates(((Declaration) decl).Namespace);

4
tests/Common/Common.Tests.cs

@ -562,6 +562,10 @@ public class CommonTests : GeneratorTestFixture @@ -562,6 +562,10 @@ public class CommonTests : GeneratorTestFixture
var testDelegates = new TestDelegates();
int value = testDelegates.MarshalAnonymousDelegate(i => i * 2);
Assert.AreEqual(2, value);
int value5 = testDelegates.MarshalAnonymousDelegate5(i => i * 2);
Assert.AreEqual(4, value5);
int value6 = testDelegates.MarshalAnonymousDelegate6(i => i * 2);
Assert.AreEqual(6, value6);
}
[Test]

10
tests/Common/Common.cpp

@ -434,6 +434,16 @@ int (*TestDelegates::MarshalAnonymousDelegate4())(int n) @@ -434,6 +434,16 @@ int (*TestDelegates::MarshalAnonymousDelegate4())(int n)
return f;
}
int TestDelegates::MarshalAnonymousDelegate5(int (STDCALL *del)(int))
{
return del(2);
}
int TestDelegates::MarshalAnonymousDelegate6(int (STDCALL *del)(int))
{
return del(3);
}
ClassA::ClassA(int value)
{
Value = value;

2
tests/Common/Common.h

@ -387,6 +387,8 @@ struct DLL_API TestDelegates @@ -387,6 +387,8 @@ struct DLL_API TestDelegates
void MarshalAnonymousDelegate2(int (*del)(int n));
void MarshalAnonymousDelegate3(float (*del)(float n));
int (*MarshalAnonymousDelegate4())(int n);
int MarshalAnonymousDelegate5(int (STDCALL *del)(int n));
int MarshalAnonymousDelegate6(int (STDCALL *del)(int n));
void MarshalDelegateInAnotherUnit(DelegateInAnotherUnit del);

Loading…
Cancel
Save