From 1420bd9216a43c50d567f6e4cec9b49e6e8911fd Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Sun, 14 Oct 2018 15:31:32 +0300 Subject: [PATCH] Fixed the generated C# for public fields with types mapped to primitive. Signed-off-by: Dimitar Dobrev --- src/Generator/AST/Utils.cs | 6 +++--- src/Generator/Generators/CSharp/CSharpSources.cs | 3 ++- src/Generator/Passes/CheckIgnoredDecls.cs | 4 +++- tests/CSharp/CSharp.Tests.cs | 1 + tests/CSharp/CSharp.cpp | 4 ++-- tests/CSharp/CSharp.h | 1 + 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Generator/AST/Utils.cs b/src/Generator/AST/Utils.cs index 96bb1535..6ebbe19c 100644 --- a/src/Generator/AST/Utils.cs +++ b/src/Generator/AST/Utils.cs @@ -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 }; diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 35f3b857..222a30c3 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -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})"; } diff --git a/src/Generator/Passes/CheckIgnoredDecls.cs b/src/Generator/Passes/CheckIgnoredDecls.cs index 2559a558..7df0d0c8 100644 --- a/src/Generator/Passes/CheckIgnoredDecls.cs +++ b/src/Generator/Passes/CheckIgnoredDecls.cs @@ -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; diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index 5c80378b..4ed3a469 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -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()) diff --git a/tests/CSharp/CSharp.cpp b/tests/CSharp/CSharp.cpp index 0be74837..8f6b070d 100644 --- a/tests/CSharp/CSharp.cpp +++ b/tests/CSharp/CSharp.cpp @@ -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; diff --git a/tests/CSharp/CSharp.h b/tests/CSharp/CSharp.h index 676e6dbd..d84d45ef 100644 --- a/tests/CSharp/CSharp.h +++ b/tests/CSharp/CSharp.h @@ -37,6 +37,7 @@ public: int operator --(); bool btest[5]; + QFlags publicFieldMappedToEnum; protected: int P;