Skip to content

Commit

Permalink
Generate valid C# for static object-typed fields
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <[email protected]>
  • Loading branch information
ddobrev committed Oct 29, 2021
1 parent 12f456e commit c8c415b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Generator/Generators/CSharp/CSharpMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ public override bool VisitClassDecl(Class @class)
{
if (Context.MarshalKind == MarshalKind.NativeField ||
Context.MarshalKind == MarshalKind.Variable ||
Context.MarshalKind == MarshalKind.ReturnVariableArray ||
!originalClass.HasNonTrivialDestructor)
{
Context.Return.Write($"{qualifiedClass}.{Helpers.CreateInstanceIdentifier}({Context.ReturnVarName})");
Expand Down
7 changes: 5 additions & 2 deletions src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,9 +1294,12 @@ private bool GenerateVariableGetter(Variable var)
ctx.PushMarshalKind(MarshalKind.ReturnVariableArray);

var arrayType = var.Type.Desugar() as ArrayType;
var isRefTypeArray = arrayType != null && var.Namespace is Class @class && @class.IsRefType;
var isRefTypeArray = arrayType != null && var.Namespace is Class context && context.IsRefType;
var elementType = arrayType?.Type.Desugar();
if (!isRefTypeArray && elementType == null)
Type type = (var.QualifiedType.Type.GetFinalPointee() ?? var.QualifiedType.Type).Desugar();
if (type.TryGetClass(out Class @class) && @class.IsRefType)
ctx.ReturnVarName = $"new {TypePrinter.IntPtrType}({ptr})";
else if (!isRefTypeArray && elementType == null)
ctx.ReturnVarName = $"*{ptr}";
else if (elementType == null || elementType.IsPrimitiveType() ||
arrayType.SizeType == ArrayType.ArraySize.Constant)
Expand Down
2 changes: 2 additions & 0 deletions tests/Common/Common.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,8 @@ public void TestStaticFields()
Assert.That(Foo.ReadWrite, Is.EqualTo(15));
Foo.ReadWrite = 25;
Assert.That(Foo.ReadWrite, Is.EqualTo(25));
Foo.StaticField.A = 20;
Assert.That(Foo.StaticField.A, Is.EqualTo(20));
}

[Test]
Expand Down
2 changes: 2 additions & 0 deletions tests/Common/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ char16_t Foo::returnChar16()
return 'a';
}

Foo Foo::staticField;

Foo2::Foo2() {}

Foo2::Foo2(const Foo2& other) : Foo(other), C(other.C), valueTypeField(other.valueTypeField) {}
Expand Down
2 changes: 2 additions & 0 deletions tests/Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class DLL_API Foo

int fooPtr();
char16_t returnChar16();

static Foo staticField;
};

struct DLL_API Bar
Expand Down

0 comments on commit c8c415b

Please sign in to comment.