Skip to content

Commit

Permalink
[Rgen] Add the code needed to parse the core image property attr in t…
Browse files Browse the repository at this point in the history
…he transformer.

We need to be able to parse it to convert it to the new API.
  • Loading branch information
mandel-macaque committed Jan 24, 2025
1 parent 86b5d59 commit 8203293
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CoreImageFilterPropertyData> {

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;

/// <inheritdoc />
public override bool Equals (object? obj)
{
return obj is CoreImageFilterPropertyData other && Equals (other);
}

/// <inheritdoc />
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);
}

/// <inheritdoc />
public override string ToString ()
{
return $"{{ Name: '{Name}' }}";
}
}
1 change: 1 addition & 0 deletions src/rgen/Microsoft.Macios.Transformer/AttributesNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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<object []> {
public IEnumerator<object []> 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<TestDataTryCreate>]
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<PropertyDeclarationSyntax> ()
.LastOrDefault ();
Assert.NotNull (declaration);

var symbol = semanticModel.GetDeclaredSymbol (declaration);
Assert.NotNull (symbol);
var exportData =
symbol.GetAttribute<CoreImageFilterPropertyData> (AttributesNames.CoreImageFilterPropertyAttribute,
CoreImageFilterPropertyData.TryParse);
Assert.Equal (expectedData, exportData);
}
}

0 comments on commit 8203293

Please sign in to comment.