Skip to content

Commit c8c415b

Browse files
committed
Generate valid C# for static object-typed fields
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 12f456e commit c8c415b

File tree

5 files changed

+12
-2
lines changed

5 files changed

+12
-2
lines changed

src/Generator/Generators/CSharp/CSharpMarshal.cs

+1
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ public override bool VisitClassDecl(Class @class)
286286
{
287287
if (Context.MarshalKind == MarshalKind.NativeField ||
288288
Context.MarshalKind == MarshalKind.Variable ||
289+
Context.MarshalKind == MarshalKind.ReturnVariableArray ||
289290
!originalClass.HasNonTrivialDestructor)
290291
{
291292
Context.Return.Write($"{qualifiedClass}.{Helpers.CreateInstanceIdentifier}({Context.ReturnVarName})");

src/Generator/Generators/CSharp/CSharpSources.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1294,9 +1294,12 @@ private bool GenerateVariableGetter(Variable var)
12941294
ctx.PushMarshalKind(MarshalKind.ReturnVariableArray);
12951295

12961296
var arrayType = var.Type.Desugar() as ArrayType;
1297-
var isRefTypeArray = arrayType != null && var.Namespace is Class @class && @class.IsRefType;
1297+
var isRefTypeArray = arrayType != null && var.Namespace is Class context && context.IsRefType;
12981298
var elementType = arrayType?.Type.Desugar();
1299-
if (!isRefTypeArray && elementType == null)
1299+
Type type = (var.QualifiedType.Type.GetFinalPointee() ?? var.QualifiedType.Type).Desugar();
1300+
if (type.TryGetClass(out Class @class) && @class.IsRefType)
1301+
ctx.ReturnVarName = $"new {TypePrinter.IntPtrType}({ptr})";
1302+
else if (!isRefTypeArray && elementType == null)
13001303
ctx.ReturnVarName = $"*{ptr}";
13011304
else if (elementType == null || elementType.IsPrimitiveType() ||
13021305
arrayType.SizeType == ArrayType.ArraySize.Constant)

tests/Common/Common.Tests.cs

+2
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,8 @@ public void TestStaticFields()
914914
Assert.That(Foo.ReadWrite, Is.EqualTo(15));
915915
Foo.ReadWrite = 25;
916916
Assert.That(Foo.ReadWrite, Is.EqualTo(25));
917+
Foo.StaticField.A = 20;
918+
Assert.That(Foo.StaticField.A, Is.EqualTo(20));
917919
}
918920

919921
[Test]

tests/Common/Common.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ char16_t Foo::returnChar16()
6060
return 'a';
6161
}
6262

63+
Foo Foo::staticField;
64+
6365
Foo2::Foo2() {}
6466

6567
Foo2::Foo2(const Foo2& other) : Foo(other), C(other.C), valueTypeField(other.valueTypeField) {}

tests/Common/Common.h

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ class DLL_API Foo
105105

106106
int fooPtr();
107107
char16_t returnChar16();
108+
109+
static Foo staticField;
108110
};
109111

110112
struct DLL_API Bar

0 commit comments

Comments
 (0)