Browse Source

Fix the generated C# for fields of type function pointer

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1204/head
Dimitar Dobrev 6 years ago
parent
commit
d735f391b1
  1. 36
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  2. 2
      src/Generator/Passes/CheckIgnoredDecls.cs
  3. 20
      src/Generator/Passes/DelegatesPass.cs
  4. 10
      src/Generator/Passes/RenamePass.cs
  5. 10
      tests/CSharp/CSharp.Tests.cs
  6. 8
      tests/CSharp/CSharp.cpp
  7. 8
      tests/CSharp/CSharp.h

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

@ -157,42 +157,6 @@ namespace CppSharp.Generators.CSharp @@ -157,42 +157,6 @@ namespace CppSharp.Generators.CSharp
return base.VisitBuiltinType(builtin, quals);
}
public override TypePrinterResult VisitFunctionType(FunctionType function,
TypeQualifiers quals)
{
var arguments = function.Parameters;
var returnType = function.ReturnType;
var args = string.Empty;
PushMarshalKind(MarshalKind.GenericDelegate);
if (arguments.Count > 0)
args = VisitParameters(function.Parameters, hasNames: false).Type;
PopMarshalKind();
if (ContextKind != TypePrinterContextKind.Managed)
return IntPtrType;
if (returnType.Type.IsPrimitiveType(PrimitiveType.Void))
{
if (!string.IsNullOrEmpty(args))
args = string.Format("<{0}>", args);
return string.Format("Action{0}", args);
}
if (!string.IsNullOrEmpty(args))
args = string.Format(", {0}", args);
PushMarshalKind(MarshalKind.GenericDelegate);
var returnTypePrinterResult = returnType.Visit(this);
PopMarshalKind();
return string.Format("Func<{0}{1}>", returnTypePrinterResult, args);
}
private bool allowStrings = true;
public override TypePrinterResult VisitPointerType(PointerType pointer,

2
src/Generator/Passes/CheckIgnoredDecls.cs

@ -110,7 +110,7 @@ namespace CppSharp.Passes @@ -110,7 +110,7 @@ namespace CppSharp.Passes
type.TryGetDeclaration(out decl);
string msg = "internal";
TypeMap typeMap;
if (!(type is FunctionType) && (decl == null ||
if ((decl == null ||
((decl.GenerationKind != GenerationKind.Internal ||
Context.TypeMaps.FindTypeMap(type, out typeMap)) &&
!HasInvalidType(field, out msg))))

20
src/Generator/Passes/DelegatesPass.cs

@ -76,6 +76,26 @@ namespace CppSharp.Passes @@ -76,6 +76,26 @@ namespace CppSharp.Passes
return true;
}
public override bool VisitProperty(Property property)
{
if (!base.VisitProperty(property))
return false;
property.QualifiedType = CheckForDelegate(property.QualifiedType, property);
return true;
}
public override bool VisitFieldDecl(Field field)
{
if (!base.VisitFieldDecl(field))
return false;
field.QualifiedType = CheckForDelegate(field.QualifiedType, field);
return true;
}
private QualifiedType CheckForDelegate(QualifiedType type, ITypedDecl decl)
{
if (type.Type is TypedefType)

10
src/Generator/Passes/RenamePass.cs

@ -190,8 +190,14 @@ namespace CppSharp.Passes @@ -190,8 +190,14 @@ namespace CppSharp.Passes
declarations.RemoveAll(d => specialization.TemplatedDecl.TemplatedDecl == d);
var @class = decl.Namespace as Class;
if (@class != null && @class.IsDependent)
declarations.AddRange(@class.TemplateParameters);
if (@class != null)
{
declarations.AddRange(from typedefDecl in @class.Typedefs
where typedefDecl.Type.Desugar() is FunctionType
select typedefDecl);
if (@class.IsDependent)
declarations.AddRange(@class.TemplateParameters);
}
var result = declarations.Any(d => d != decl && d.Name == newName);
if (result)

10
tests/CSharp/CSharp.Tests.cs

@ -1261,6 +1261,16 @@ public unsafe class CSharpTests : GeneratorTestFixture @@ -1261,6 +1261,16 @@ public unsafe class CSharpTests : GeneratorTestFixture
Assert.That(CSharp.CSharp.TakeConstCharStarRef("Test"), Is.EqualTo("Test"));
}
[Test]
public void Test()
{
using (var hasFunctionPtrField = new HasFunctionPtrField())
{
hasFunctionPtrField.FunctionPtrField = @string => @string.Length;
Assert.That(hasFunctionPtrField.FunctionPtrField("Test"), Is.EqualTo(4));
}
}
public class Inter : SimpleInterface
{
public override int Size => s;

8
tests/CSharp/CSharp.cpp

@ -1543,6 +1543,14 @@ void InterfaceTester::setInterface(SimpleInterface* i) @@ -1543,6 +1543,14 @@ void InterfaceTester::setInterface(SimpleInterface* i)
interface = i;
}
HasFunctionPtrField::HasFunctionPtrField()
{
}
HasFunctionPtrField::~HasFunctionPtrField()
{
}
void va_listFunction(va_list v)
{
}

8
tests/CSharp/CSharp.h

@ -1295,6 +1295,14 @@ private: @@ -1295,6 +1295,14 @@ private:
SimpleInterface* interface;
};
class DLL_API HasFunctionPtrField
{
public:
HasFunctionPtrField();
~HasFunctionPtrField();
int (*functionPtrField)(const char*);
};
DLL_API void va_listFunction(va_list v);
DLL_API char* returnCharPointer();
DLL_API const char* takeConstCharStarRef(const char*& c);

Loading…
Cancel
Save