Browse Source

When generating delegates attributed types need to be taken into account or proper calling convention won't get picked up.

pull/234/head
Elias Holzer 11 years ago committed by triton
parent
commit
cc3cc40fc4
  1. 7
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs
  2. 11
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 11
      tests/Basic/Basic.Tests.cs
  4. 13
      tests/Basic/Basic.h
  5. 4
      tests/Tests.h

7
src/Generator/Generators/CLI/CLIHeadersTemplate.cs

@ -716,7 +716,11 @@ namespace CppSharp.Generators.CLI @@ -716,7 +716,11 @@ namespace CppSharp.Generators.CLI
var insideClass = typedef.Namespace is Class;
var callingConvention = function.CallingConvention.ToInteropCallConv();
var attributedType = typedef.Type.GetPointee() as AttributedType;
if (attributedType != null)
{
var equivalentFunctionType = attributedType.Equivalent.Type as FunctionType;
var callingConvention = equivalentFunctionType.CallingConvention.ToInteropCallConv();
if (callingConvention != System.Runtime.InteropServices.CallingConvention.Winapi)
{
WriteLine("[{0}({1}::{2})] ",
@ -724,6 +728,7 @@ namespace CppSharp.Generators.CLI @@ -724,6 +728,7 @@ namespace CppSharp.Generators.CLI
"System::Runtime::InteropServices::CallingConvention",
callingConvention);
}
}
WriteLine("{0}{1};",
!insideClass ? "public " : "",

11
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -2543,8 +2543,17 @@ namespace CppSharp.Generators.CSharp @@ -2543,8 +2543,17 @@ namespace CppSharp.Generators.CSharp
else if (typedef.Type.IsPointerTo(out functionType))
{
PushBlock(CSharpBlockKind.Typedef);
var attributedType = typedef.Type.GetPointee() as AttributedType;
if (attributedType != null)
{
var equivalentFunctionType = attributedType.Equivalent.Type as FunctionType;
var callingConvention = equivalentFunctionType.CallingConvention.ToInteropCallConv();
if (callingConvention != System.Runtime.InteropServices.CallingConvention.Winapi)
{
WriteLine("[UnmanagedFunctionPointerAttribute(global::System.Runtime.InteropServices.CallingConvention.{0})]",
functionType.CallingConvention.ToInteropCallConv());
callingConvention);
}
}
TypePrinter.PushContext(CSharpTypePrinterContextKind.Native);
WriteLine("{0}unsafe {1};",
Helpers.GetAccess(typedef.Access),

11
tests/Basic/Basic.Tests.cs

@ -176,13 +176,12 @@ public class BasicTests : GeneratorTestFixture @@ -176,13 +176,12 @@ public class BasicTests : GeneratorTestFixture
var delegates = new TestDelegates();
var doubleSum = delegates.A(2) + delegates.B(2);
Assert.AreEqual(8, doubleSum);
}
[Test]
public void TestAttributedDelegate()
{
var result = basic.AttributedDelegate(2);
Assert.AreEqual(4, result);
var stdcall = delegates.StdCall(i => i);
Assert.AreEqual(1, stdcall);
var cdecl = delegates.CDecl(i => i);
Assert.AreEqual(1, cdecl);
}
[Test]

13
tests/Basic/Basic.h

@ -242,6 +242,8 @@ DLL_API int operator==(const Foo2& a, const Foo2& b) @@ -242,6 +242,8 @@ DLL_API int operator==(const Foo2& a, const Foo2& b)
// Tests delegates
typedef int (*DelegateInGlobalNamespace)(int);
typedef int (STDCALL *DelegateStdCall)(int);
typedef int (CDECL *DelegateCDecl)(int n);
struct DLL_API TestDelegates
{
@ -252,6 +254,9 @@ struct DLL_API TestDelegates @@ -252,6 +254,9 @@ struct DLL_API TestDelegates
static int Double(int N) { return N * 2; }
int Triple(int N) { return N * 3; }
int StdCall(DelegateStdCall del) { return del(1); }
int CDecl(DelegateCDecl del) { return del(1); }
DelegateInClass A;
DelegateInGlobalNamespace B;
// As long as we can't marshal them make sure they're ignored
@ -262,14 +267,6 @@ TestDelegates::TestDelegates() : A(Double), B(Double), C(&TestDelegates::Triple) @@ -262,14 +267,6 @@ TestDelegates::TestDelegates() : A(Double), B(Double), C(&TestDelegates::Triple)
{
}
// Tests delegate generation for attributed function types
typedef int(__cdecl *AttributedDelegate)(int n);
DLL_API int __cdecl Double(int n) { return n * 2; }
DLL_API AttributedDelegate GetAttributedDelegate()
{
return Double;
}
// Tests memory leaks in constructors
// C#: Marshal.FreeHGlobal(arg0);
struct DLL_API TestMemoryLeaks

4
tests/Tests.h

@ -2,8 +2,12 @@ @@ -2,8 +2,12 @@
#if defined(_MSC_VER)
#define DLL_API __declspec(dllexport)
#define STDCALL __stdcall
#define CDECL __cdecl
#else
#define DLL_API __attribute__ ((visibility ("default")))
#define STDCALL __attribute__((stdcall))
#define CDECL __attribute__((stdcall))
#endif
#define CS_OUT
Loading…
Cancel
Save