Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rgen] Add a central location for the aux variable naming. #22063

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.Macios.Generator.DataModel;

readonly partial struct Parameter {

public enum VariableType {
Handle,
NSArray,
BlockLiteral,
PrimitivePointer,
}

/// <summary>
/// Returns the name of the aux variable that would have needed for the given parameter. Use the
/// variable type to name it.
/// </summary>
/// <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}",
_ => null
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections;
using System.Collections.Generic;
using Microsoft.Macios.Generator.DataModel;
using Xunit;
using static Microsoft.Macios.Generator.Tests.TestDataFactory;

namespace Microsoft.Macios.Generator.Tests.DataModel;

public class ParameterTests {

class TestDataGetVariableName : IEnumerable<object []> {
public IEnumerator<object []> GetEnumerator ()
{
var exampleParameter = new Parameter (0, ReturnTypeForBool (), "firstParameter");
yield return [exampleParameter, Parameter.VariableType.Handle, $"{exampleParameter.Name}__handle__"];
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}"];
}

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

[Theory]
[ClassData (typeof (TestDataGetVariableName))]
void GetNameForVariableTypeTests (Parameter parameter, Parameter.VariableType variableType, string expectedName)
=> Assert.Equal (expectedName, parameter.GetNameForVariableType (variableType));
}