Browse Source

Fixed the generation of sequential layouts to handle unions.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/778/head
Dimitar Dobrev 9 years ago
parent
commit
ab32637e14
  1. 30
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 2
      src/Generator/Options.cs

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

@ -561,15 +561,9 @@ namespace CppSharp.Generators.CSharp
public void GenerateClassInternals(Class @class) public void GenerateClassInternals(Class @class)
{ {
PushBlock(CSharpBlockKind.InternalsClass); PushBlock(CSharpBlockKind.InternalsClass);
if (Options.GenerateSequentialLayout) if (!Options.GenerateSequentialLayout || @class.IsUnion)
{ WriteLine($"[StructLayout(LayoutKind.Explicit, Size = {@class.Layout.Size})]");
WriteLine("[StructLayout(LayoutKind.Sequential)]"); // no else because the layout is sequential for structures by default
}
else
{
WriteLine("[StructLayout(LayoutKind.Explicit, Size = {0})]",
@class.Layout.Size);
}
GenerateClassInternalHead(@class); GenerateClassInternalHead(@class);
WriteStartBraceIndent(); WriteStartBraceIndent();
@ -577,7 +571,7 @@ namespace CppSharp.Generators.CSharp
TypePrinter.PushContext(TypePrinterContextKind.Native); TypePrinter.PushContext(TypePrinterContextKind.Native);
foreach (var field in @class.Layout.Fields) foreach (var field in @class.Layout.Fields)
GenerateClassInternalsField(field); GenerateClassInternalsField(field, @class);
if (@class.IsGenerated) if (@class.IsGenerated)
{ {
var functions = GatherClassInternalFunctions(@class); var functions = GatherClassInternalFunctions(@class);
@ -754,7 +748,7 @@ namespace CppSharp.Generators.CSharp
} }
} }
private void GenerateClassInternalsField(LayoutField field) private void GenerateClassInternalsField(LayoutField field, Class @class)
{ {
Declaration decl; Declaration decl;
field.QualifiedType.Type.TryGetDeclaration(out decl); field.QualifiedType.Type.TryGetDeclaration(out decl);
@ -779,14 +773,8 @@ namespace CppSharp.Generators.CSharp
PushBlock(CSharpBlockKind.Field); PushBlock(CSharpBlockKind.Field);
if (Options.GenerateSequentialLayout) if (!Options.GenerateSequentialLayout || @class.IsUnion)
{ WriteLine($"[FieldOffset({field.Offset})]");
// For the sequential layout there is no need to set an offset
}
else
{
WriteLine("[FieldOffset({0})]", field.Offset);
}
TypePrinter.PushMarshalKind(MarshalKind.NativeField); TypePrinter.PushMarshalKind(MarshalKind.NativeField);
var fieldTypePrinted = field.QualifiedType.CSharpType(TypePrinter); var fieldTypePrinted = field.QualifiedType.CSharpType(TypePrinter);
@ -803,11 +791,11 @@ namespace CppSharp.Generators.CSharp
if (field.Expression != null) if (field.Expression != null)
{ {
var fieldValuePrinted = field.Expression.CSharpValue(ExpressionPrinter); var fieldValuePrinted = field.Expression.CSharpValue(ExpressionPrinter);
Write("{0} {1} {2} = {3};", access, fieldType, fieldName, fieldValuePrinted); Write($"{access} {fieldType} {fieldName} = {fieldValuePrinted};");
} }
else else
{ {
Write("{0} {1} {2};", access, fieldType, fieldName); Write($"{access} {fieldType} {fieldName};");
} }
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);

2
src/Generator/Options.cs

@ -161,7 +161,7 @@ namespace CppSharp
public bool OutputInteropIncludes; public bool OutputInteropIncludes;
public bool GenerateFunctionTemplates; public bool GenerateFunctionTemplates;
public bool GenerateInternalImports; public bool GenerateInternalImports;
public bool GenerateSequentialLayout; public bool GenerateSequentialLayout { get; set; }
public bool UseHeaderDirectories; public bool UseHeaderDirectories;
/// <summary> /// <summary>

Loading…
Cancel
Save