Browse Source

Generate valid C# for returned function pointers

Fixes https://github.com/mono/CppSharp/issues/1226.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
const-wchar_t
Dimitar Dobrev 5 years ago
parent
commit
6dcb2ac31a
  1. 4
      src/AST/FunctionExtensions.cs
  2. 2
      src/Generator/Generators/CSharp/CSharpSources.cs
  3. 2
      src/Generator/Passes/DelegatesPass.cs
  4. 3
      tests/Common/Common.Tests.cs
  5. 10
      tests/Common/Common.cpp
  6. 4
      tests/Common/Common.h

4
src/AST/FunctionExtensions.cs

@ -33,7 +33,9 @@ namespace CppSharp.AST @@ -33,7 +33,9 @@ namespace CppSharp.AST
{
@params.Add(new Parameter
{
QualifiedType = universalDelegate && param.Kind == ParameterKind.IndirectReturnType ?
QualifiedType = universalDelegate &&
(param.Kind == ParameterKind.IndirectReturnType ||
param.Type.Desugar().IsPointerTo(out FunctionType functionType)) ?
pointer : param.QualifiedType,
Kind = param.Kind,
Usage = param.Usage,

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

@ -1835,7 +1835,7 @@ namespace CppSharp.Generators.CSharp @@ -1835,7 +1835,7 @@ namespace CppSharp.Generators.CSharp
}
TypePrinterResult retType;
TypePrinter.PushMarshalKind(MarshalKind.GenericDelegate);
TypePrinter.PushMarshalKind(MarshalKind.VTableReturnValue);
var @params = GatherInternalParams(method, out retType);
var vTableMethodDelegateName = GetVTableMethodDelegateName(method);

2
src/Generator/Passes/DelegatesPass.cs

@ -73,11 +73,13 @@ namespace CppSharp.Passes @@ -73,11 +73,13 @@ namespace CppSharp.Passes
ReturnType = method.ReturnType
};
TypePrinter.PushMarshalKind(MarshalKind.VTableReturnValue);
functionType.Parameters.AddRange(
method.GatherInternalParams(Context.ParserOptions.IsItaniumLikeAbi, true));
method.FunctionType = CheckForDelegate(new QualifiedType(functionType),
method.Namespace, @private: true);
TypePrinter.PopMarshalKind();
return true;
}

3
tests/Common/Common.Tests.cs

@ -576,6 +576,9 @@ public class CommonTests : GeneratorTestFixture @@ -576,6 +576,9 @@ public class CommonTests : GeneratorTestFixture
Assert.That(prop.conflict, Is.EqualTo(CommonTest.TestProperties.Conflict.Value1));
prop.conflict = CommonTest.TestProperties.Conflict.Value2;
Assert.That(prop.conflict, Is.EqualTo(CommonTest.TestProperties.Conflict.Value2));
prop.Callback = x => 4 * x;
Assert.That(prop.Callback(5), Is.EqualTo(20));
}
using (var prop = new HasOverridenSetter())
{

10
tests/Common/Common.cpp

@ -718,6 +718,16 @@ void TestProperties::SetConflict(Conflict conflict) @@ -718,6 +718,16 @@ void TestProperties::SetConflict(Conflict conflict)
_conflict = conflict;
}
int(*TestProperties::getCallback())(int)
{
return _callback;
}
void TestProperties::setCallback(int(*value)(int))
{
_callback = value;
}
HasOverridenSetter::HasOverridenSetter()
{
}

4
tests/Common/Common.h

@ -649,6 +649,9 @@ public: @@ -649,6 +649,9 @@ public:
Conflict GetConflict();
void SetConflict(Conflict _conflict);
virtual int(*getCallback())(int);
virtual void setCallback(int(*value)(int));
private:
int FieldValue;
double _refToPrimitiveInSetter;
@ -656,6 +659,7 @@ private: @@ -656,6 +659,7 @@ private:
int _setterReturnsBoolean;
int _virtualSetterReturnsBoolean;
Conflict _conflict;
int(*_callback)(int);
};
class DLL_API HasOverridenSetter : public TestProperties

Loading…
Cancel
Save