Skip to content

Commit 1420bd9

Browse files
committed
Fixed the generated C# for public fields with types mapped to primitive.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 0c87a77 commit 1420bd9

File tree

6 files changed

+12
-7
lines changed

6 files changed

+12
-7
lines changed

src/Generator/AST/Utils.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@ private static bool IsSpecializationSelfContained(
204204
return false;
205205
}
206206

207-
private static bool IsMappedToPrimitive(ITypeMapDatabase typeMaps,
208-
Type type, ClassTemplateSpecialization specialization)
207+
public static bool IsMappedToPrimitive(ITypeMapDatabase typeMaps,
208+
Type type, Declaration declaration)
209209
{
210210
TypeMap typeMap;
211-
if (!typeMaps.FindTypeMap(specialization, out typeMap))
211+
if (!typeMaps.FindTypeMap(declaration, out typeMap))
212212
return false;
213213

214214
var typePrinterContext = new TypePrinterContext { Type = type };

src/Generator/Generators/CSharp/CSharpSources.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,8 @@ private void GenerateFieldGetter(Field field, Class @class, QualifiedType return
11801180
// Class field getter should return a reference object instead of a copy. Wrapping `returnVar` in
11811181
// IntPtr ensures that non-copying object constructor is invoked.
11821182
Class typeClass;
1183-
if (field.Type.TryGetClass(out typeClass) && !typeClass.IsValueType)
1183+
if (field.Type.TryGetClass(out typeClass) && !typeClass.IsValueType &&
1184+
!ASTUtils.IsMappedToPrimitive(Context.TypeMaps, field.Type, typeClass))
11841185
returnVar = $"new {CSharpTypePrinter.IntPtrType}(&{returnVar})";
11851186
}
11861187

src/Generator/Passes/CheckIgnoredDecls.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ public override bool VisitFieldDecl(Field field)
108108
Declaration decl;
109109
type.TryGetDeclaration(out decl);
110110
string msg = "internal";
111+
TypeMap typeMap;
111112
if (!(type is FunctionType) && (decl == null ||
112-
(decl.GenerationKind != GenerationKind.Internal &&
113+
((decl.GenerationKind != GenerationKind.Internal ||
114+
Context.TypeMaps.FindTypeMap(decl, out typeMap)) &&
113115
!HasInvalidType(field, out msg))))
114116
return false;
115117

tests/CSharp/CSharp.Tests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void TestUncompilableCode()
5454
hasSecondaryBaseWithAbstractWithDefaultArg.Abstract();
5555
hasSecondaryBaseWithAbstractWithDefaultArg.AbstractWithNoDefaultArg(foo);
5656
}
57+
Assert.That(foo.PublicFieldMappedToEnum, Is.EqualTo(TestFlag.Flag2));
5758
Assert.That(foo.ReturnConstRef(), Is.EqualTo(5));
5859
}
5960
using (var hasOverride = new HasOverrideOfHasPropertyWithDerivedType())

tests/CSharp/CSharp.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
#include "CSharp.h"
44

5-
Foo::Foo(const char* name)
5+
Foo::Foo(const char* name) : publicFieldMappedToEnum(TestFlag::Flag2)
66
{
77
A = 10;
88
P = 50;
99
}
1010

11-
Foo::Foo(int a, int p)
11+
Foo::Foo(int a, int p) : publicFieldMappedToEnum(TestFlag::Flag2)
1212
{
1313
A = a;
1414
P = p;

tests/CSharp/CSharp.h

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class DLL_API Foo
3737
int operator --();
3838

3939
bool btest[5];
40+
QFlags<TestFlag> publicFieldMappedToEnum;
4041

4142
protected:
4243
int P;

0 commit comments

Comments
 (0)