Skip to content

Commit

Permalink
[Rgen] Add needed factory methods to cast an enum to its primitive ty…
Browse files Browse the repository at this point in the history
…pe. (#22062)

Case to the underlying type if it is not a smart enum, else use the
CastToNative method.

---------

Co-authored-by: Alex Soto <[email protected]>
Co-authored-by: GitHub Actions Autoformatter <[email protected]>
  • Loading branch information
3 people authored Jan 29, 2025
1 parent cd6d5a2 commit 5c242ec
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ static partial class BindingSyntaxFactory {
/// <summary>
/// Returns the expression needed to cast a parameter to its native type.
/// </summary>
/// <param name="parameter">The parameter whose castin we need to generate. The type info has to be
/// and enum and be marked as native. If it is not the method returns null</param>
/// <returns></returns>
/// <param name="parameter">The parameter whose casting we need to generate. The type info has to be
/// and enum and be marked as native. If it is not, the method returns null</param>
/// <returns>The cast C# expression.</returns>
internal static CastExpressionSyntax? CastToNative (in Parameter parameter)
{
// not an enum and not a native value. we cannot calculate the casting expression.
Expand All @@ -48,6 +48,35 @@ static partial class BindingSyntaxFactory {
return castExpression;
}

/// <summary>
/// Returns the expression needed to cast an enum parameter to its primitive type to be used in marshaling.
/// </summary>
/// <param name="parameter">The parameter for which we need to generate the casting. The type info has to be
/// an enumerator. If it is not, the method returns null.</param>
/// <returns>The cast C# expression.</returns>
internal static CastExpressionSyntax? CastToPrimitive (in Parameter parameter)
{
if (!parameter.Type.IsEnum) {
return null;
}

if (parameter.Type.IsNativeEnum) {
// return the native casting
return CastToNative (parameter);
}

// returns the enum primitive to be used
var marshalType = parameter.Type.ToMarshallType ();
if (marshalType is null)
return null;

// (byte) parameter
var castExpression = CastExpression (
type: IdentifierName (marshalType),
expression: IdentifierName (parameter.Name).WithLeadingTrivia (Space));
return castExpression;
}

/// <summary>
/// Returns the expression needed to cast a bool to a byte to be used in a native call.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,54 @@ void CastToNativeTests (Parameter parameter, string? expectedCast)
}
}

class TestDataCastToPrimitive : IEnumerable<object []> {
public IEnumerator<object []> GetEnumerator ()
{
// not enum parameter
var boolParam = new Parameter (
position: 0,
type: ReturnTypeForBool (),
name: "myParam");
yield return [boolParam, null!];

var enumParam = new Parameter (
position: 0,
type: ReturnTypeForEnum ("MyEnum", isNativeEnum: false),
name: "myParam");

yield return [enumParam, "(int) myParam"];

var byteParam = new Parameter (
position: 0,
type: ReturnTypeForEnum ("MyEnum", isNativeEnum: false, underlyingType: SpecialType.System_Byte),
name: "myParam");

yield return [byteParam, "(byte) myParam"];


var longParam = new Parameter (
position: 0,
type: ReturnTypeForEnum ("MyEnum", isNativeEnum: false, underlyingType: SpecialType.System_Int64),
name: "myParam");

yield return [longParam, "(long) myParam"];
}
IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
}

[Theory]
[ClassData (typeof (TestDataCastToPrimitive))]
void CastToPrimitiveTests (Parameter parameter, string? expectedCast)
{
var expression = CastToPrimitive (parameter);
if (expectedCast is null) {
Assert.Null (expression);
} else {
Assert.NotNull (expression);
Assert.Equal (expectedCast, expression?.ToString ());
}
}

[Fact]
void CastToByteTests ()
{
Expand Down

0 comments on commit 5c242ec

Please sign in to comment.