Skip to content

Commit

Permalink
Generate with no hacks correctly sized layouts
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
  • Loading branch information
ddobrev committed Jun 30, 2019
1 parent 153a4d0 commit d90d328
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 17 deletions.
7 changes: 6 additions & 1 deletion src/AST/ClassExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace CppSharp.AST
{
public static class ClassExtensions
public static class ClassExtensions
{
public static IEnumerable<Function> GetFunctionOverloads(this Class @class,
Function function)
Expand Down Expand Up @@ -211,6 +211,11 @@ public static bool HasDependentValueFieldInLayout(this Class @class)
b => b.Class).Any(HasDependentValueFieldInLayout);
}

public static int GetSize(this ClassLayout layout) =>
/// There's at least one ABI (System V) that gives to empty structs
/// size 1 in C++ and size 0 in C. The former causes crashes in older versions of Mono.
layout.Size == 1 && layout.DataSize == 0 ? 0 : layout.Size;

private static bool IsValueDependent(Type type)
{
var desugared = type.Desugar();
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public void GenerateClassInternals(Class @class)
{
PushBlock(BlockKind.InternalsClass);
if (!Options.GenerateSequentialLayout || @class.IsUnion)
WriteLine($"[StructLayout(LayoutKind.Explicit, Size = {@class.Layout.Size})]");
WriteLine($"[StructLayout(LayoutKind.Explicit, Size = {@class.Layout.GetSize()})]");
else if (@class.MaxFieldAlignment > 0)
WriteLine($"[StructLayout(LayoutKind.Sequential, Pack = {@class.MaxFieldAlignment})]");

Expand Down Expand Up @@ -2885,7 +2885,7 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters
((Method) method.OriginalFunction).IsConstructor)
{
WriteLine($@"Marshal.AllocHGlobal({
((Class) method.OriginalNamespace).Layout.Size});");
((Class) method.OriginalNamespace).Layout.GetSize()});");
names.Insert(0, Helpers.ReturnIdentifier);
}
WriteLine("{0}({1});", functionName, string.Join(", ", names));
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Generators/CSharp/CSharpTypePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public override TypePrinterResult VisitArrayType(ArrayType array,
return new TypePrinterResult
{
Type = "fixed byte",
NameSuffix = $"[{array.Size * @class.Layout.Size}]"
NameSuffix = $"[{array.Size * @class.Layout.GetSize()}]"
};
}

Expand Down
13 changes: 0 additions & 13 deletions src/Generator/Passes/CheckAbiParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ namespace CppSharp.Passes
/// </summary>
public class CheckAbiParameters : TranslationUnitPass
{
public override bool VisitClassDecl(Class @class)
{
if (!base.VisitClassDecl(@class))
return false;

if (@class.IsDependent || @class.Layout.Fields.Count > 0 || @class.Fields.Count > 0)
return false;

@class.Layout.Size = @class.Layout.DataSize = 0;

return true;
}

public override bool VisitFunctionDecl(Function function)
{
if (!VisitDeclaration(function))
Expand Down

0 comments on commit d90d328

Please sign in to comment.