From 8203293b2cd1b9163021c9d2d7bc2ccd59ea48a3 Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Saenz Date: Fri, 24 Jan 2025 18:23:11 -0500 Subject: [PATCH] [Rgen] Add the code needed to parse the core image property attr in the transformer. We need to be able to parse it to convert it to the new API. --- .../Attributes/BindData.cs | 1 - .../Attributes/CoreImageFilterPropertyData.cs | 83 +++++++++++++++++++ .../AttributesNames.cs | 1 + .../CoreImageFilterPropertyTests.cs | 70 ++++++++++++++++ 4 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/rgen/Microsoft.Macios.Transformer/Attributes/CoreImageFilterPropertyData.cs create mode 100644 tests/rgen/Microsoft.Macios.Transformer.Tests/Attributes/CoreImageFilterPropertyTests.cs diff --git a/src/rgen/Microsoft.Macios.Transformer/Attributes/BindData.cs b/src/rgen/Microsoft.Macios.Transformer/Attributes/BindData.cs index 888ca5e17df..5ed72e12019 100644 --- a/src/rgen/Microsoft.Macios.Transformer/Attributes/BindData.cs +++ b/src/rgen/Microsoft.Macios.Transformer/Attributes/BindData.cs @@ -24,7 +24,6 @@ public static bool TryParse (AttributeData attributeData, var count = attributeData.ConstructorArguments.Length; string selector; bool @virtual = false; - // custom marshal directive values switch (count) { case 1: diff --git a/src/rgen/Microsoft.Macios.Transformer/Attributes/CoreImageFilterPropertyData.cs b/src/rgen/Microsoft.Macios.Transformer/Attributes/CoreImageFilterPropertyData.cs new file mode 100644 index 00000000000..88d2d88f3f4 --- /dev/null +++ b/src/rgen/Microsoft.Macios.Transformer/Attributes/CoreImageFilterPropertyData.cs @@ -0,0 +1,83 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using System.Diagnostics.CodeAnalysis; +using Microsoft.CodeAnalysis; + +namespace Microsoft.Macios.Transformer.Attributes; + +readonly struct CoreImageFilterPropertyData : IEquatable { + + public string Name { get; } + + public CoreImageFilterPropertyData (string name) + { + Name = name; + } + + public static bool TryParse (AttributeData attributeData, + [NotNullWhen (true)] out CoreImageFilterPropertyData? data) + { + data = null; + var count = attributeData.ConstructorArguments.Length; + string name; + + switch (count) { + case 1: + name = (string) attributeData.ConstructorArguments [0].Value!; + break; + default: + // 0 should not be an option.. + return false; + } + + if (attributeData.NamedArguments.Length == 0) { + data = new (name); + return true; + } + + foreach (var (argumentName, value) in attributeData.NamedArguments) { + switch (argumentName) { + case "Name": + name = (string) value.Value!; + break; + default: + data = null; + return false; + } + } + + data = new(name); + return true; + } + + public bool Equals (CoreImageFilterPropertyData other) + => Name == other.Name; + + /// + public override bool Equals (object? obj) + { + return obj is CoreImageFilterPropertyData other && Equals (other); + } + + /// + public override int GetHashCode () + => HashCode.Combine (Name); + + + public static bool operator == (CoreImageFilterPropertyData x, CoreImageFilterPropertyData y) + { + return x.Equals (y); + } + + public static bool operator != (CoreImageFilterPropertyData x, CoreImageFilterPropertyData y) + { + return !(x == y); + } + + /// + public override string ToString () + { + return $"{{ Name: '{Name}' }}"; + } +} diff --git a/src/rgen/Microsoft.Macios.Transformer/AttributesNames.cs b/src/rgen/Microsoft.Macios.Transformer/AttributesNames.cs index dbc15a7fe23..e74cb4e45bb 100644 --- a/src/rgen/Microsoft.Macios.Transformer/AttributesNames.cs +++ b/src/rgen/Microsoft.Macios.Transformer/AttributesNames.cs @@ -47,6 +47,7 @@ static class AttributesNames { [BindingFlag] public const string CheckDisposedAttribute = "CheckDisposedAttribute"; public const string CoreImageFilterAttribute = "CoreImageFilterAttribute"; + public const string CoreImageFilterPropertyAttribute = "CoreImageFilterPropertyAttribute"; public const string DefaultCtorVisibilityAttribute = "DefaultCtorVisibilityAttribute"; [BindingFlag (AttributeTargets.Field)] public const string DefaultEnumValueAttribute = "DefaultEnumValueAttribute"; diff --git a/tests/rgen/Microsoft.Macios.Transformer.Tests/Attributes/CoreImageFilterPropertyTests.cs b/tests/rgen/Microsoft.Macios.Transformer.Tests/Attributes/CoreImageFilterPropertyTests.cs new file mode 100644 index 00000000000..104fe1f8038 --- /dev/null +++ b/tests/rgen/Microsoft.Macios.Transformer.Tests/Attributes/CoreImageFilterPropertyTests.cs @@ -0,0 +1,70 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using System.Collections; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.Macios.Generator.Extensions; +using Microsoft.Macios.Transformer.Attributes; +using Xamarin.Tests; +using Xamarin.Utils; + +namespace Microsoft.Macios.Transformer.Tests.Attributes; + +public class CoreImageFilterPropertyTests : BaseTransformerTestClass { + class TestDataTryCreate : IEnumerable { + public IEnumerator GetEnumerator () + { + const string path = "/some/random/path.cs"; + + const string coreImageFilterWithProperties = @" +using CoreImage; +using Foundation; +using ObjCRuntime; +using Mono.Cecil; + +namespace NS; + +[CoreImageFilter (IntPtrCtorVisibility = MethodAttributes.Family)] // was already protected in classic +[BaseType (typeof (CIFilter))] +interface CICompositingFilter : CIAccordionFoldTransitionProtocol { + + [CoreImageFilterProperty (""inputBackgroundImage"")] + CIImage BackgroundImage { get; set; } +} +"; + yield return [ + (Source: coreImageFilterWithProperties, Path: path), + new CoreImageFilterPropertyData ("inputBackgroundImage") + ]; + } + + IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); + } + + [Theory] + [AllSupportedPlatformsClassData] + void TryCreateTests (ApplePlatform platform, (string Source, string Path) source, + CoreImageFilterPropertyData expectedData) + { + // create a compilation used to create the transformer + var compilation = CreateCompilation (platform, sources: source); + var syntaxTree = compilation.SyntaxTrees.ForSource (source); + Assert.NotNull (syntaxTree); + + var semanticModel = compilation.GetSemanticModel (syntaxTree); + Assert.NotNull (semanticModel); + + var declaration = syntaxTree.GetRoot () + .DescendantNodes ().OfType () + .LastOrDefault (); + Assert.NotNull (declaration); + + var symbol = semanticModel.GetDeclaredSymbol (declaration); + Assert.NotNull (symbol); + var exportData = + symbol.GetAttribute (AttributesNames.CoreImageFilterPropertyAttribute, + CoreImageFilterPropertyData.TryParse); + Assert.Equal (expectedData, exportData); + } +}