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 <[email protected]>
  • Loading branch information
ddobrev committed Jun 29, 2019
1 parent 153a4d0 commit 0e0b4cc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
10 changes: 8 additions & 2 deletions src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,13 @@ public void GenerateClassInternals(Class @class)
{
PushBlock(BlockKind.InternalsClass);
if (!Options.GenerateSequentialLayout || @class.IsUnion)
WriteLine($"[StructLayout(LayoutKind.Explicit, Size = {@class.Layout.Size})]");
{
/// 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.
int size = @class.Layout.Size == 1 && @class.Layout.DataSize == 0 ?
0 : @class.Layout.Size;
WriteLine($"[StructLayout(LayoutKind.Explicit, Size = {size})]");
}
else if (@class.MaxFieldAlignment > 0)
WriteLine($"[StructLayout(LayoutKind.Sequential, Pack = {@class.MaxFieldAlignment})]");

Expand Down Expand Up @@ -2885,7 +2891,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.DataSize});");
names.Insert(0, Helpers.ReturnIdentifier);
}
WriteLine("{0}({1});", functionName, string.Join(", ", names));
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 0e0b4cc

Please sign in to comment.