Skip to content

Commit 99e4ceb

Browse files
[RGen] Add a factory method to create the aux variable for a BindFrom(typeof(NSString)).
The aux method will return the call to the GetConstant from the enum.
1 parent fe3580e commit 99e4ceb

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,36 @@ static partial class BindingSyntaxFactory {
437437
return LocalDeclarationStatement (declaration);
438438
}
439439

440+
internal static LocalDeclarationStatementSyntax? GetNSStringSmartEnumAuxVariable (in Parameter parameter)
441+
{
442+
if (!parameter.Type.IsSmartEnum)
443+
return null;
444+
445+
var variableName = parameter.GetNameForVariableType (Parameter.VariableType.BindFrom);
446+
if (variableName is null)
447+
return null;
448+
449+
// smart enums are very simple to do, we need to call the GetConstant that was generated as an extension
450+
// method
451+
var factoryInvocation = InvocationExpression (
452+
MemberAccessExpression (
453+
SyntaxKind.SimpleMemberAccessExpression,
454+
IdentifierName (parameter.Name),
455+
IdentifierName ("GetConstant").WithTrailingTrivia (Space))
456+
);
457+
458+
var declarator =
459+
VariableDeclarator (Identifier (variableName).WithLeadingTrivia (Space).WithTrailingTrivia (Space))
460+
.WithInitializer (EqualsValueClause (factoryInvocation.WithLeadingTrivia (Space)));
461+
462+
// generats: var nba_variable = NSNumber.FromDouble(value);
463+
var declaration = VariableDeclaration (IdentifierName (Identifier (
464+
TriviaList (), SyntaxKind.VarKeyword, "var", "var", TriviaList ())))
465+
.WithVariables (SingletonSeparatedList (declarator));
466+
467+
return LocalDeclarationStatement (declaration);
468+
}
469+
440470
static string? GetObjCMessageSendMethodName<T> (ExportData<T> exportData,
441471
TypeInfo returnType, ImmutableArray<Parameter> parameters, bool isSuper = false, bool isStret = false)
442472
where T : Enum

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,35 @@ void GetNSValueAuxVariableTests (in Parameter parameter, string? expectedDeclara
451451
Assert.Equal (expectedDeclaration, declaration.ToString ());
452452
}
453453
}
454+
455+
class TestDataGetNSStringSmartEnumAuxVariableTests : IEnumerable<object []> {
456+
public IEnumerator<object []> GetEnumerator ()
457+
{
458+
yield return [
459+
new Parameter (0, ReturnTypeForEnum ("CoreAnimation.CATransform3D", isSmartEnum: true), "myParam"),
460+
"var nsb_myParam = myParam.GetConstant ();",
461+
];
462+
463+
yield return [
464+
new Parameter (0, ReturnTypeForEnum ("CoreAnimation.CATransform3D", isSmartEnum: false), "myParam"),
465+
null!
466+
];
467+
}
468+
469+
IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
470+
}
471+
472+
[Theory]
473+
[ClassData (typeof (TestDataGetNSStringSmartEnumAuxVariableTests))]
474+
void GetNSStringSmartEnumAuxVariableTests (in Parameter parameter, string? expectedDeclaration)
475+
{
476+
var declaration = GetNSStringSmartEnumAuxVariable (parameter);
477+
if (expectedDeclaration is null) {
478+
Assert.Null (declaration);
479+
} else {
480+
Assert.NotNull (declaration);
481+
Assert.Equal (expectedDeclaration, declaration.ToString ());
482+
}
483+
}
484+
454485
}

0 commit comments

Comments
 (0)