diff --git a/src/rgen/Microsoft.Macios.Generator/DataModel/Parameter.Generator.cs b/src/rgen/Microsoft.Macios.Generator/DataModel/Parameter.Generator.cs new file mode 100644 index 00000000000..839bba52667 --- /dev/null +++ b/src/rgen/Microsoft.Macios.Generator/DataModel/Parameter.Generator.cs @@ -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, + } + + /// + /// Returns the name of the aux variable that would have needed for the given parameter. Use the + /// variable type to name it. + /// + /// The type of aux variable. + /// The name of the aux variable to use. + 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 + }; +} diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/DataModel/ParameterTests.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/DataModel/ParameterTests.cs new file mode 100644 index 00000000000..fbc2af9f32e --- /dev/null +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/DataModel/ParameterTests.cs @@ -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 { + public IEnumerator 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)); +}