Skip to content

Commit

Permalink
[RGen] Implement the BindAttribute parsing logic for the transformer.
Browse files Browse the repository at this point in the history
  • Loading branch information
mandel-macaque committed Jan 24, 2025
1 parent b2ae11a commit 117ffb2
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/rgen/Microsoft.Macios.Transformer/Attributes/BindData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;

namespace Microsoft.Macios.Transformer.Attributes;

readonly struct BindData : IEquatable<BindData> {

public string Selector { get; }
public bool Virtual { get; init; }

public BindData (string selector, bool isVirtual = false)
{
Selector = selector;
Virtual = isVirtual;
}

public static bool TryParse (AttributeData attributeData,
[NotNullWhen (true)] out BindData? data)
{
data = null;
var count = attributeData.ConstructorArguments.Length;
string selector;
bool @virtual = false;
// custom marshal directive values

switch (count) {
case 1:
selector = (string) attributeData.ConstructorArguments [0].Value!;
break;
default:
// 0 should not be an option..
return false;
}

if (attributeData.NamedArguments.Length == 0) {
data = new (selector);
return true;
}

foreach (var (argumentName, value) in attributeData.NamedArguments) {
switch (argumentName) {
case "Selector":
selector = (string) value.Value!;
break;
case "Virtual":
@virtual = (bool) value.Value!;
break;
default:
data = null;
return false;
}
}

data = new(selector) { Virtual = @virtual, };
return true;
}

public bool Equals (BindData other)
{
if (Selector != other.Selector)
return false;
return Virtual == other.Virtual;
}

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

/// <inheritdoc />
public override int GetHashCode ()
=> HashCode.Combine (Selector, Virtual);


public static bool operator == (BindData x, BindData y)
{
return x.Equals (y);
}

public static bool operator != (BindData x, BindData y)
{
return !(x == y);
}

/// <inheritdoc />
public override string ToString ()
{
return $"{{ BindSelector: '{Selector}', Virtual: {Virtual} }}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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 BindDataTests : BaseTransformerTestClass {

class TestDataTryCreate : IEnumerable<object []> {
public IEnumerator<object []> GetEnumerator ()
{
var path = "/some/random/path.cs";
var simpleBind = @"
using System;
using Foundation;
using ObjCRuntime;
using UIKit;
namespace Test;
[NoTV]
[MacCatalyst (13, 1)]
[DisableDefaultCtor]
[Abstract]
[BaseType (typeof (NSObject))]
interface UIFeedbackGenerator : UIInteraction {
[Bind (""someSelector"")]
void Prepare ();
}
";

yield return [(Sorunce: simpleBind, Path: path), new BindData ("someSelector")];

var virtualBind = @"
using System;
using Foundation;
using ObjCRuntime;
using UIKit;
namespace Test;
[NoTV]
[MacCatalyst (13, 1)]
[DisableDefaultCtor]
[Abstract]
[BaseType (typeof (NSObject))]
interface UIFeedbackGenerator : UIInteraction {
[Bind (""someSelector"", Virtual = true)]
void Prepare ();
}
";

yield return [(Sorunce: virtualBind, Path: path), new BindData ("someSelector", true)];
}

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

[Theory]
[AllSupportedPlatformsClassData<TestDataTryCreate>]
void TryeCreateTests (ApplePlatform platform, (string Source, string Path) source,
BindData 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<MethodDeclarationSyntax> ()
.LastOrDefault ();
Assert.NotNull (declaration);

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

0 comments on commit 117ffb2

Please sign in to comment.