Skip to content

Commit

Permalink
[Rgen] Add method in the syntaxt factory to cast a bool to a byte.
Browse files Browse the repository at this point in the history
This is needed because the pinvokes do not use bools but bytes.
  • Loading branch information
mandel-macaque committed Jan 28, 2025
1 parent db76488 commit e999d4a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Macios.Generator.Attributes;
using Microsoft.Macios.Generator.DataModel;
Expand Down Expand Up @@ -47,6 +48,34 @@ static partial class BindingSyntaxFactory {
return castExpression;
}

/// <summary>
/// Returns the expression needed to cast a bool to a byte to be used in a native call.
/// </summary>
/// <param name="parameter">The parameter to cast.</param>
/// <returns>A conditional expression that casts a bool to a byte.</returns>
internal static ConditionalExpressionSyntax? CastToByte (in Parameter parameter)
{
if (parameter.Type.SpecialType != SpecialType.System_Boolean)
return null;
// (byte) 1
var castOne = CastExpression (
PredefinedType (Token (SyntaxKind.ByteKeyword)),
LiteralExpression (SyntaxKind.NumericLiteralExpression, Literal (1)).WithLeadingTrivia (Space).WithTrailingTrivia (Space)
);
// (byte) 0
var castZero = CastExpression (
PredefinedType (Token (SyntaxKind.ByteKeyword)),
LiteralExpression (SyntaxKind.NumericLiteralExpression, Literal (0)).WithLeadingTrivia (Space)
).WithLeadingTrivia (Space);

// with this exact space count
// foo ? (byte) 1 : (byte) 0
return ConditionalExpression (
condition: IdentifierName (parameter.Name).WithTrailingTrivia (Space),
whenTrue: castOne.WithLeadingTrivia (Space),
whenFalse: castZero);
}

static string? GetObjCMessageSendMethodName<T> (ExportData<T> exportData,
TypeInfo returnType, ImmutableArray<Parameter> parameters, bool isSuper = false, bool isStret = false) where T : Enum
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.Macios.Generator.Tests.Emitters;

public class BindingSyntaxFactoryObjCRuntimeTests {

class TestDataCodeChangesFromClassDeclaration : IEnumerable<object []> {
class TestDataCastToNativeTests : IEnumerable<object []> {
public IEnumerator<object []> GetEnumerator ()
{

Expand Down Expand Up @@ -51,7 +51,7 @@ public IEnumerator<object []> GetEnumerator ()
}

[Theory]
[ClassData (typeof (TestDataCodeChangesFromClassDeclaration))]
[ClassData (typeof (TestDataCastToNativeTests))]
void CastToNativeTests (Parameter parameter, string? expectedCast)
{
var expression = CastToNative (parameter);
Expand All @@ -62,4 +62,17 @@ void CastToNativeTests (Parameter parameter, string? expectedCast)
Assert.Equal (expectedCast, expression?.ToString ());
}
}

[Fact]
void CastToByteTests ()
{
var boolParameter = new Parameter (0, ReturnTypeForBool (), "myParameter");
var conditionalExpr = CastToByte (boolParameter);
Assert.NotNull (conditionalExpr);
Assert.Equal ("myParameter ? (byte) 1 : (byte) 0", conditionalExpr.ToString ());

var intParameter = new Parameter (1, ReturnTypeForInt (), "myParameter");
conditionalExpr = CastToByte (intParameter);
Assert.Null (conditionalExpr);
}
}

0 comments on commit e999d4a

Please sign in to comment.