Skip to content

Commit

Permalink
Fixed the generated C# for public fields with types mapped to primitive.
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <[email protected]>
  • Loading branch information
ddobrev committed Oct 14, 2018
1 parent 0c87a77 commit 1420bd9
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Generator/AST/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ private static ClassTemplateSpecialization GetParentSpecialization(Type type)
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 };
Expand Down
3 changes: 2 additions & 1 deletion src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,8 @@ private void GenerateFieldGetter(Field field, Class @class, QualifiedType return
// 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})";
}

Expand Down
4 changes: 3 additions & 1 deletion src/Generator/Passes/CheckIgnoredDecls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ public override bool VisitFieldDecl(Field field)
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;

Expand Down
1 change: 1 addition & 0 deletions tests/CSharp/CSharp.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void TestUncompilableCode()
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())
Expand Down
4 changes: 2 additions & 2 deletions tests/CSharp/CSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/CSharp/CSharp.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class DLL_API Foo
int operator --();

bool btest[5];
QFlags<TestFlag> publicFieldMappedToEnum;

protected:
int P;
Expand Down

0 comments on commit 1420bd9

Please sign in to comment.