Skip to content

Commit

Permalink
[Rgen] Add method in the syntax factory to cast a bool to a byte. (#2…
Browse files Browse the repository at this point in the history
…2061)

This is needed because the pinvokes do not use bools but bytes.
mandel-macaque authored Jan 29, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent a70bd3e commit 3b647ca
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;
@@ -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
{
Original file line number Diff line number Diff line change
@@ -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 ()
{

@@ -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);
@@ -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 3b647ca

Please sign in to comment.