diff --git a/src/AST/Class.cs b/src/AST/Class.cs index b05c95ee..b1ce8318 100644 --- a/src/AST/Class.cs +++ b/src/AST/Class.cs @@ -181,6 +181,8 @@ namespace CppSharp.AST public bool IsInterface => Type == ClassType.Interface; + public bool HasUnionFields { get; set; } + public bool IsAbstractImpl { get { return Methods.Any(m => m.SynthKind == FunctionSynthKind.AbstractImplCall); } diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 9c88a4e1..42791bb0 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -792,7 +792,7 @@ namespace CppSharp.Generators.CSharp private bool CanUseSequentialLayout(Class @class) { - if (@class.IsUnion) + if (@class.IsUnion || @class.HasUnionFields) return false; foreach (var field in @class.Fields) diff --git a/src/Generator/Passes/FlattenAnonymousTypesToFields.cs b/src/Generator/Passes/FlattenAnonymousTypesToFields.cs index b769127a..95f797ac 100644 --- a/src/Generator/Passes/FlattenAnonymousTypesToFields.cs +++ b/src/Generator/Passes/FlattenAnonymousTypesToFields.cs @@ -31,6 +31,8 @@ namespace CppSharp.Passes ReplaceField(@class, i, fieldType); fieldType.Fields.Clear(); fieldType.ExplicitlyIgnore(); + if (fieldType.IsUnion) + @class.HasUnionFields = true; } if (@class.Layout == null) diff --git a/tests/Common/Common.Tests.cs b/tests/Common/Common.Tests.cs index 9eee5630..21a26241 100644 --- a/tests/Common/Common.Tests.cs +++ b/tests/Common/Common.Tests.cs @@ -389,6 +389,11 @@ public class CommonTests nestedPublic.J = 5; Assert.That(nestedPublic.L, Is.EqualTo(5)); Assert.That(nestedPublic.G, Is.Not.EqualTo(0)); + using (var nestedUnionWithNested = new NestedUnionWithNested()) + { + nestedUnionWithNested.NestedField1 = 50; + Assert.That(nestedUnionWithNested.UnionField, Is.EqualTo(50)); + } } [Test] diff --git a/tests/Common/Common.h b/tests/Common/Common.h index 1063b741..12bd621b 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -1333,6 +1333,19 @@ public: static ReturnByValueWithReturnParam generate(); }; +struct DLL_API NestedUnionWithNested +{ + union + { + struct + { + int nestedField1; + int nestedField2; + }; + int unionField; + }; +}; + template void TemplatedFunction(T type) {