Skip to content

Commit 5c242ec

Browse files
mandel-macaquedalexsotoGitHub Actions Autoformatter
authored
[Rgen] Add needed factory methods to cast an enum to its primitive type. (#22062)
Case to the underlying type if it is not a smart enum, else use the CastToNative method. --------- Co-authored-by: Alex Soto <alex@soto.dev> Co-authored-by: GitHub Actions Autoformatter <github-actions-autoformatter@xamarin.com>
1 parent cd6d5a2 commit 5c242ec

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

src/rgen/Microsoft.Macios.Generator/Emitters/BindingSyntaxFactory.ObjCRuntime.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ static partial class BindingSyntaxFactory {
2323
/// <summary>
2424
/// Returns the expression needed to cast a parameter to its native type.
2525
/// </summary>
26-
/// <param name="parameter">The parameter whose castin we need to generate. The type info has to be
27-
/// and enum and be marked as native. If it is not the method returns null</param>
28-
/// <returns></returns>
26+
/// <param name="parameter">The parameter whose casting we need to generate. The type info has to be
27+
/// and enum and be marked as native. If it is not, the method returns null</param>
28+
/// <returns>The cast C# expression.</returns>
2929
internal static CastExpressionSyntax? CastToNative (in Parameter parameter)
3030
{
3131
// not an enum and not a native value. we cannot calculate the casting expression.
@@ -48,6 +48,35 @@ static partial class BindingSyntaxFactory {
4848
return castExpression;
4949
}
5050

51+
/// <summary>
52+
/// Returns the expression needed to cast an enum parameter to its primitive type to be used in marshaling.
53+
/// </summary>
54+
/// <param name="parameter">The parameter for which we need to generate the casting. The type info has to be
55+
/// an enumerator. If it is not, the method returns null.</param>
56+
/// <returns>The cast C# expression.</returns>
57+
internal static CastExpressionSyntax? CastToPrimitive (in Parameter parameter)
58+
{
59+
if (!parameter.Type.IsEnum) {
60+
return null;
61+
}
62+
63+
if (parameter.Type.IsNativeEnum) {
64+
// return the native casting
65+
return CastToNative (parameter);
66+
}
67+
68+
// returns the enum primitive to be used
69+
var marshalType = parameter.Type.ToMarshallType ();
70+
if (marshalType is null)
71+
return null;
72+
73+
// (byte) parameter
74+
var castExpression = CastExpression (
75+
type: IdentifierName (marshalType),
76+
expression: IdentifierName (parameter.Name).WithLeadingTrivia (Space));
77+
return castExpression;
78+
}
79+
5180
/// <summary>
5281
/// Returns the expression needed to cast a bool to a byte to be used in a native call.
5382
/// </summary>

tests/rgen/Microsoft.Macios.Generator.Tests/Emitters/BindingSyntaxFactoryObjCRuntimeTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,54 @@ void CastToNativeTests (Parameter parameter, string? expectedCast)
6363
}
6464
}
6565

66+
class TestDataCastToPrimitive : IEnumerable<object []> {
67+
public IEnumerator<object []> GetEnumerator ()
68+
{
69+
// not enum parameter
70+
var boolParam = new Parameter (
71+
position: 0,
72+
type: ReturnTypeForBool (),
73+
name: "myParam");
74+
yield return [boolParam, null!];
75+
76+
var enumParam = new Parameter (
77+
position: 0,
78+
type: ReturnTypeForEnum ("MyEnum", isNativeEnum: false),
79+
name: "myParam");
80+
81+
yield return [enumParam, "(int) myParam"];
82+
83+
var byteParam = new Parameter (
84+
position: 0,
85+
type: ReturnTypeForEnum ("MyEnum", isNativeEnum: false, underlyingType: SpecialType.System_Byte),
86+
name: "myParam");
87+
88+
yield return [byteParam, "(byte) myParam"];
89+
90+
91+
var longParam = new Parameter (
92+
position: 0,
93+
type: ReturnTypeForEnum ("MyEnum", isNativeEnum: false, underlyingType: SpecialType.System_Int64),
94+
name: "myParam");
95+
96+
yield return [longParam, "(long) myParam"];
97+
}
98+
IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
99+
}
100+
101+
[Theory]
102+
[ClassData (typeof (TestDataCastToPrimitive))]
103+
void CastToPrimitiveTests (Parameter parameter, string? expectedCast)
104+
{
105+
var expression = CastToPrimitive (parameter);
106+
if (expectedCast is null) {
107+
Assert.Null (expression);
108+
} else {
109+
Assert.NotNull (expression);
110+
Assert.Equal (expectedCast, expression?.ToString ());
111+
}
112+
}
113+
66114
[Fact]
67115
void CastToByteTests ()
68116
{

0 commit comments

Comments
 (0)