Skip to content

Commit

Permalink
[Rgen] Add factory method that will create an aux variable for string…
Browse files Browse the repository at this point in the history
…s. (#22080)

Co-authored-by: GitHub Actions Autoformatter <[email protected]>
  • Loading branch information
mandel-macaque and GitHub Actions Autoformatter authored Jan 30, 2025
1 parent f501981 commit 5d0bf15
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ namespace Microsoft.Macios.Generator.DataModel;
readonly partial struct Parameter {

public enum VariableType {
BlockLiteral,
Handle,
NSArray,
BlockLiteral,
NSString,
NSStringStruct,
PrimitivePointer,
StringPointer,
}

/// <summary>
Expand All @@ -19,11 +22,17 @@ public enum VariableType {
/// <param name="variableType">The type of aux variable.</param>
/// <returns>The name of the aux variable to use.</returns>
public string? GetNameForVariableType (VariableType variableType)
=> variableType switch {
VariableType.Handle => $"{Name}__handle__",
VariableType.NSArray => $"nsa_{Name}",
VariableType.BlockLiteral => $"block_ptr_{Name}",
VariableType.PrimitivePointer => $"converted_{Name}",
{
var cleanedName = Name.Replace ("@", "");
return variableType switch {
VariableType.BlockLiteral => $"block_ptr_{cleanedName}",
VariableType.Handle => $"{cleanedName}__handle__",
VariableType.NSArray => $"nsa_{cleanedName}",
VariableType.NSString => $"ns{cleanedName}",
VariableType.NSStringStruct => $"_s{cleanedName}",
VariableType.PrimitivePointer => $"converted_{cleanedName}",
VariableType.StringPointer => $"_p{cleanedName}",
_ => null
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ static partial class BindingSyntaxFactory {
var enumBackingValue = parameter.Type.EnumUnderlyingType.Value.GetKeyword ();
var castExpression = CastExpression (IdentifierName (marshalType), // (IntPtr/UIntPtr) cast
CastExpression (
IdentifierName (enumBackingValue),
IdentifierName (parameter.Name)
.WithLeadingTrivia (Space))
IdentifierName (enumBackingValue),
IdentifierName (parameter.Name)
.WithLeadingTrivia (Space))
.WithLeadingTrivia (Space)); // (backingfield) (variable) cast
return castExpression;
}
Expand Down Expand Up @@ -89,14 +89,15 @@ static partial class BindingSyntaxFactory {
return null;
// (byte) 1
var castOne = CastExpression (
PredefinedType (Token (SyntaxKind.ByteKeyword)),
LiteralExpression (SyntaxKind.NumericLiteralExpression, Literal (1)).WithLeadingTrivia (Space).WithTrailingTrivia (Space)
);
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);
PredefinedType (Token (SyntaxKind.ByteKeyword)),
LiteralExpression (SyntaxKind.NumericLiteralExpression, Literal (0)).WithLeadingTrivia (Space)
).WithLeadingTrivia (Space);

// with this exact space count
// foo ? (byte) 1 : (byte) 0
Expand Down Expand Up @@ -210,7 +211,8 @@ static partial class BindingSyntaxFactory {

// generates: variable = {FactoryCall}
var declarator = VariableDeclarator (Identifier (variableName))
.WithInitializer (EqualsValueClause (factoryInvocation.WithLeadingTrivia (Space)).WithLeadingTrivia (Space));
.WithInitializer (EqualsValueClause (factoryInvocation.WithLeadingTrivia (Space))
.WithLeadingTrivia (Space));
// generates the final statement:
// var x = zone.GetHandle ();
// or
Expand All @@ -228,6 +230,38 @@ static partial class BindingSyntaxFactory {
));
}

internal static LocalDeclarationStatementSyntax? GetStringAuxVariable (in Parameter parameter)
{
if (parameter.Type.Name != "string")
return null;

var variableName = parameter.GetNameForVariableType (Parameter.VariableType.NSString);
if (variableName is null)
return null;

// generates: CFString.CreateNative ({parameter.Name});
var cfstringFactoryInvocation = InvocationExpression (
MemberAccessExpression (
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName ("CFString"),
IdentifierName ("CreateNative").WithTrailingTrivia (Space))
)
.WithArgumentList (ArgumentList (SingletonSeparatedList (
Argument (IdentifierName (parameter.Name)))));

// generates {var} = CFString.CreateNative ({parameter.Name});
var declarator = VariableDeclarator (Identifier (variableName).WithLeadingTrivia (Space).WithTrailingTrivia (Space))
.WithInitializer (EqualsValueClause (cfstringFactoryInvocation.WithLeadingTrivia (Space)));


// put everythign together
var declaration = VariableDeclaration (IdentifierName (Identifier (
TriviaList (), SyntaxKind.VarKeyword, "var", "var", TriviaList ())))
.WithVariables (SingletonSeparatedList (declarator));

return LocalDeclarationStatement (declaration);
}

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 @@ -19,6 +19,8 @@ public IEnumerator<object []> GetEnumerator ()
yield return [exampleParameter, Parameter.VariableType.BlockLiteral, $"block_ptr_{exampleParameter.Name}"];
yield return [exampleParameter, Parameter.VariableType.PrimitivePointer, $"converted_{exampleParameter.Name}"];
yield return [exampleParameter, Parameter.VariableType.NSArray, $"nsa_{exampleParameter.Name}"];
yield return [exampleParameter, Parameter.VariableType.NSString, $"ns{exampleParameter.Name}"];
yield return [exampleParameter, Parameter.VariableType.NSStringStruct, $"_s{exampleParameter.Name}"];
}

IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void GetNSArrayAuxVariableTests (in Parameter parameter, string? expectedDeclara
}
}

class TestDataCodeChangesFromClassDeclaration : IEnumerable<object []> {
class TestDataGetHandleAuxVariableTests : IEnumerable<object []> {
public IEnumerator<object []> GetEnumerator ()
{
// nsobject type
Expand Down Expand Up @@ -236,7 +236,7 @@ public IEnumerator<object []> GetEnumerator ()
}

[Theory]
[ClassData (typeof (TestDataCodeChangesFromClassDeclaration))]
[ClassData (typeof (TestDataGetHandleAuxVariableTests))]
void GetHandleAuxVariableTests (in Parameter parameter, string? expectedDeclaration, bool withNullAllowed)
{
var declaration = GetHandleAuxVariable (parameter, withNullAllowed: withNullAllowed);
Expand All @@ -247,4 +247,34 @@ void GetHandleAuxVariableTests (in Parameter parameter, string? expectedDeclarat
Assert.Equal (expectedDeclaration, declaration.ToString ());
}
}

class TestDataGetStringAuxVariableTest : IEnumerable<object []> {
public IEnumerator<object []> GetEnumerator ()
{
yield return [
new Parameter (0, ReturnTypeForString (), "myParam"),
"var nsmyParam = CFString.CreateNative (myParam);",
];

yield return [
new Parameter (0, ReturnTypeForBool (), "myParam"),
null!,
];
}

IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
}

[Theory]
[ClassData (typeof (TestDataGetStringAuxVariableTest))]
void GetStringAuxVariableTests (in Parameter parameter, string? expectedDeclaration)
{
var declaration = GetStringAuxVariable (parameter);
if (expectedDeclaration is null) {
Assert.Null (declaration);
} else {
Assert.NotNull (declaration);
Assert.Equal (expectedDeclaration, declaration.ToString ());
}
}
}

0 comments on commit 5d0bf15

Please sign in to comment.