Browse Source

Fix C# layouts for classes with nameless unions

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1641/head
Dimitar Dobrev 4 years ago
parent
commit
5e328da9e7
  1. 2
      src/AST/Class.cs
  2. 2
      src/Generator/Generators/CSharp/CSharpSources.cs
  3. 2
      src/Generator/Passes/FlattenAnonymousTypesToFields.cs
  4. 5
      tests/Common/Common.Tests.cs
  5. 13
      tests/Common/Common.h

2
src/AST/Class.cs

@ -181,6 +181,8 @@ namespace CppSharp.AST
public bool IsInterface => Type == ClassType.Interface; public bool IsInterface => Type == ClassType.Interface;
public bool HasUnionFields { get; set; }
public bool IsAbstractImpl public bool IsAbstractImpl
{ {
get { return Methods.Any(m => m.SynthKind == FunctionSynthKind.AbstractImplCall); } get { return Methods.Any(m => m.SynthKind == FunctionSynthKind.AbstractImplCall); }

2
src/Generator/Generators/CSharp/CSharpSources.cs

@ -792,7 +792,7 @@ namespace CppSharp.Generators.CSharp
private bool CanUseSequentialLayout(Class @class) private bool CanUseSequentialLayout(Class @class)
{ {
if (@class.IsUnion) if (@class.IsUnion || @class.HasUnionFields)
return false; return false;
foreach (var field in @class.Fields) foreach (var field in @class.Fields)

2
src/Generator/Passes/FlattenAnonymousTypesToFields.cs

@ -31,6 +31,8 @@ namespace CppSharp.Passes
ReplaceField(@class, i, fieldType); ReplaceField(@class, i, fieldType);
fieldType.Fields.Clear(); fieldType.Fields.Clear();
fieldType.ExplicitlyIgnore(); fieldType.ExplicitlyIgnore();
if (fieldType.IsUnion)
@class.HasUnionFields = true;
} }
if (@class.Layout == null) if (@class.Layout == null)

5
tests/Common/Common.Tests.cs

@ -389,6 +389,11 @@ public class CommonTests
nestedPublic.J = 5; nestedPublic.J = 5;
Assert.That(nestedPublic.L, Is.EqualTo(5)); Assert.That(nestedPublic.L, Is.EqualTo(5));
Assert.That(nestedPublic.G, Is.Not.EqualTo(0)); Assert.That(nestedPublic.G, Is.Not.EqualTo(0));
using (var nestedUnionWithNested = new NestedUnionWithNested())
{
nestedUnionWithNested.NestedField1 = 50;
Assert.That(nestedUnionWithNested.UnionField, Is.EqualTo(50));
}
} }
[Test] [Test]

13
tests/Common/Common.h

@ -1333,6 +1333,19 @@ public:
static ReturnByValueWithReturnParam generate(); static ReturnByValueWithReturnParam generate();
}; };
struct DLL_API NestedUnionWithNested
{
union
{
struct
{
int nestedField1;
int nestedField2;
};
int unionField;
};
};
template<typename T> void TemplatedFunction(T type) template<typename T> void TemplatedFunction(T type)
{ {

Loading…
Cancel
Save