Skip to content

Commit

Permalink
[Rgen] Add factory method to generate the return variable. (#22173)
Browse files Browse the repository at this point in the history
  • Loading branch information
mandel-macaque authored Feb 13, 2025
1 parent 9546611 commit f5fab03
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ readonly partial struct TypeInfo {
/// </summary>
public bool ArrayElementTypeIsWrapped { get; init; }

/// <summary>
/// Get the name of the variable for the type when it is used as a return value.
/// </summary>
public string ReturnVariableName => "ret"; // nothing fancy for now

internal TypeInfo (ITypeSymbol symbol, Compilation compilation) :
this (
symbol is IArrayTypeSymbol arrayTypeSymbol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Macios.Generator.Attributes;
using Microsoft.Macios.Generator.DataModel;
using Microsoft.Macios.Generator.Extensions;
using Microsoft.Macios.Generator.Formatters;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using TypeInfo = Microsoft.Macios.Generator.DataModel.TypeInfo;
using Parameter = Microsoft.Macios.Generator.DataModel.Parameter;
Expand Down Expand Up @@ -668,6 +669,17 @@ internal static LocalDeclarationStatementSyntax GetExceptionHandleAuxVariable ()
.NormalizeWhitespace (); // no special mono style
}

internal static (string Name, LocalDeclarationStatementSyntax Declaration) GetReturnValueAuxVariable (in TypeInfo returnType)
{
var typeSyntax = returnType.GetIdentifierSyntax ();
var variableName = returnType.ReturnVariableName;
// generates Type ret; The GetIdentifierSyntax will ensure that the correct type and nullable annotation is used
var declaration = LocalDeclarationStatement (
VariableDeclaration (typeSyntax.WithTrailingTrivia (Space))
.WithVariables (SingletonSeparatedList (VariableDeclarator (Identifier (variableName)))));
return (Name: variableName, Declaration: declaration);
}

/// <summary>
/// Returns a using statement or block for a local declaration.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
Expand All @@ -10,6 +9,7 @@
using Xunit;
using static Microsoft.Macios.Generator.Emitters.BindingSyntaxFactory;
using static Microsoft.Macios.Generator.Tests.TestDataFactory;
using TypeInfo = Microsoft.Macios.Generator.DataModel.TypeInfo;

namespace Microsoft.Macios.Generator.Tests.Emitters;

Expand Down Expand Up @@ -731,4 +731,75 @@ public IEnumerator<object []> GetEnumerator ()
[ClassData (typeof (TestDataUsingTests))]
void UsingTests (LocalDeclarationStatementSyntax declaration, string expectedDeclaration)
=> Assert.Equal (expectedDeclaration, Using (declaration).ToString ());

class TestDataGetReturnValueAuxVariable : IEnumerable<object []> {
public IEnumerator<object []> GetEnumerator ()
{
yield return [
ReturnTypeForBool (),
"ret",
"bool ret;"
];

yield return [
ReturnTypeForString (),
"ret",
"string ret;"
];

yield return [
ReturnTypeForString (isNullable: true),
"ret",
"string? ret;"
];

yield return [
ReturnTypeForNSObject ("NSLocale"),
"ret",
"NSLocale ret;"
];

yield return [
ReturnTypeForNSObject ("NSLocale", isNullable: true),
"ret",
"NSLocale? ret;"
];

yield return [
ReturnTypeForStruct ("MyStruct"),
"ret",
"MyStruct ret;"
];

yield return [
ReturnTypeForArray ("int"),
"ret",
"int[] ret;"
];

yield return [
ReturnTypeForArray ("int", isNullable: true),
"ret",
"int[]? ret;"
];

yield return [
ReturnTypeForArray ("int?", isNullable: true),
"ret",
"int?[]? ret;"
];

}

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

[Theory]
[ClassData (typeof (TestDataGetReturnValueAuxVariable))]
void GetReturnValueAuxVariableTests (TypeInfo typeInfo, string expectedVariable, string expectedDeclaration)
{
var (name, declaration) = GetReturnValueAuxVariable (typeInfo);
Assert.Equal (expectedVariable, name);
Assert.Equal (expectedDeclaration, declaration.ToString ());
}
}

10 comments on commit f5fab03

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.