Browse Source

Fixed the generated C# for public fields with types mapped to primitive.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1139/head
Dimitar Dobrev 7 years ago
parent
commit
1420bd9216
  1. 6
      src/Generator/AST/Utils.cs
  2. 3
      src/Generator/Generators/CSharp/CSharpSources.cs
  3. 4
      src/Generator/Passes/CheckIgnoredDecls.cs
  4. 1
      tests/CSharp/CSharp.Tests.cs
  5. 4
      tests/CSharp/CSharp.cpp
  6. 1
      tests/CSharp/CSharp.h

6
src/Generator/AST/Utils.cs

@ -204,11 +204,11 @@ namespace CppSharp.AST @@ -204,11 +204,11 @@ namespace CppSharp.AST
return false;
}
private static bool IsMappedToPrimitive(ITypeMapDatabase typeMaps,
Type type, ClassTemplateSpecialization specialization)
public static bool IsMappedToPrimitive(ITypeMapDatabase typeMaps,
Type type, Declaration declaration)
{
TypeMap typeMap;
if (!typeMaps.FindTypeMap(specialization, out typeMap))
if (!typeMaps.FindTypeMap(declaration, out typeMap))
return false;
var typePrinterContext = new TypePrinterContext { Type = type };

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

@ -1180,7 +1180,8 @@ namespace CppSharp.Generators.CSharp @@ -1180,7 +1180,8 @@ namespace CppSharp.Generators.CSharp
// Class field getter should return a reference object instead of a copy. Wrapping `returnVar` in
// IntPtr ensures that non-copying object constructor is invoked.
Class typeClass;
if (field.Type.TryGetClass(out typeClass) && !typeClass.IsValueType)
if (field.Type.TryGetClass(out typeClass) && !typeClass.IsValueType &&
!ASTUtils.IsMappedToPrimitive(Context.TypeMaps, field.Type, typeClass))
returnVar = $"new {CSharpTypePrinter.IntPtrType}(&{returnVar})";
}

4
src/Generator/Passes/CheckIgnoredDecls.cs

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

1
tests/CSharp/CSharp.Tests.cs

@ -54,6 +54,7 @@ public unsafe class CSharpTests : GeneratorTestFixture @@ -54,6 +54,7 @@ public unsafe class CSharpTests : GeneratorTestFixture
hasSecondaryBaseWithAbstractWithDefaultArg.Abstract();
hasSecondaryBaseWithAbstractWithDefaultArg.AbstractWithNoDefaultArg(foo);
}
Assert.That(foo.PublicFieldMappedToEnum, Is.EqualTo(TestFlag.Flag2));
Assert.That(foo.ReturnConstRef(), Is.EqualTo(5));
}
using (var hasOverride = new HasOverrideOfHasPropertyWithDerivedType())

4
tests/CSharp/CSharp.cpp

@ -2,13 +2,13 @@ @@ -2,13 +2,13 @@
#include "CSharp.h"
Foo::Foo(const char* name)
Foo::Foo(const char* name) : publicFieldMappedToEnum(TestFlag::Flag2)
{
A = 10;
P = 50;
}
Foo::Foo(int a, int p)
Foo::Foo(int a, int p) : publicFieldMappedToEnum(TestFlag::Flag2)
{
A = a;
P = p;

1
tests/CSharp/CSharp.h

@ -37,6 +37,7 @@ public: @@ -37,6 +37,7 @@ public:
int operator --();
bool btest[5];
QFlags<TestFlag> publicFieldMappedToEnum;
protected:
int P;

Loading…
Cancel
Save