Skip to content

Commit

Permalink
Revert "Simplified the C# marshalling of Boolean fields."
Browse files Browse the repository at this point in the history
This reverts commit dc270ef.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
  • Loading branch information
ddobrev committed Jul 11, 2017
1 parent dc270ef commit 54f3af1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/Generator/Generators/CSharp/CSharpMarshal.cs
Expand Up @@ -216,6 +216,14 @@ public override bool VisitPrimitiveType(PrimitiveType primitive, TypeQualifiers
goto default;
case PrimitiveType.Char16:
return false;
case PrimitiveType.Bool:
if (Context.MarshalKind == MarshalKind.NativeField)
{
// returned structs must be blittable and bool isn't
Context.Return.Write("{0} != 0", Context.ReturnVarName);
return true;
}
goto default;
default:
Context.Return.Write(Context.ReturnVarName);
return true;
Expand Down Expand Up @@ -641,6 +649,14 @@ public override bool VisitPrimitiveType(PrimitiveType primitive, TypeQualifiers
goto default;
case PrimitiveType.Char16:
return false;
case PrimitiveType.Bool:
if (Context.MarshalKind == MarshalKind.NativeField)
{
// returned structs must be blittable and bool isn't
Context.Return.Write("(byte) ({0} ? 1 : 0)", Context.Parameter.Name);
return true;
}
goto default;
default:
Context.Return.Write(Context.Parameter.Name);
return true;
Expand Down
10 changes: 8 additions & 2 deletions src/Generator/Generators/CSharp/CSharpSources.cs
Expand Up @@ -612,13 +612,19 @@ where @base.IsClass

private void GenerateClassInternalsField(LayoutField field, Class @class)
{
Declaration decl;
field.QualifiedType.Type.TryGetDeclaration(out decl);

var arrayType = field.QualifiedType.Type.Desugar() as ArrayType;
var coreType = field.QualifiedType.Type.Desugar();
if (arrayType != null && arrayType.SizeType == ArrayType.ArraySize.Constant)
coreType = arrayType.Type.Desugar();

TypePrinterResult retType = TypePrinter.VisitFieldDecl(
new Field { Name = field.Name, QualifiedType = field.QualifiedType });

PushBlock(BlockKind.Field);

if (field.QualifiedType.Type.Desugar().IsPrimitiveType(PrimitiveType.Bool))
WriteLine("[MarshalAs(UnmanagedType.I1)]");
if (!Options.GenerateSequentialLayout || @class.IsUnion)
WriteLine($"[FieldOffset({field.Offset})]");
Write($"internal {retType}{retType.NameSuffix}");
Expand Down
5 changes: 4 additions & 1 deletion src/Generator/Generators/CSharp/CSharpTypePrinter.cs
Expand Up @@ -494,7 +494,10 @@ static string GetIntString(PrimitiveType primitive, ParserTargetInfo targetInfo)
{
switch (primitive)
{
case PrimitiveType.Bool: return "bool";
case PrimitiveType.Bool:
// returned structs must be blittable and bool isn't
return MarshalKind == MarshalKind.NativeField ?
"byte" : "bool";
case PrimitiveType.Void: return "void";
case PrimitiveType.Char16:
case PrimitiveType.Char32:
Expand Down

0 comments on commit 54f3af1

Please sign in to comment.