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 the parsing code for the BaseTypeAttribute in the transformer. #22021

Merged
merged 6 commits into from
Jan 21, 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
158 changes: 158 additions & 0 deletions src/rgen/Microsoft.Macios.Transformer/Attributes/BaseTypeData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Diagnostics.CodeAnalysis;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.Macios.Generator;

namespace Microsoft.Macios.Transformer.Attributes;

readonly struct BaseTypeData : IEquatable<BaseTypeData> {

public string BaseType { get; } // is a type in the attribute, but we do not care for the transformation
public string? Name { get; init; } = null;
public string [] Events { get; init; } = []; // it is a collection of types, but we do not care for the transformation
public string [] Delegates { get; init; } = [];
public bool Singleton { get; init; }

public string? KeepRefUntil { get; init; } = null;

public bool IsStubClass { get; init; }

public BaseTypeData (string baseType)
{
BaseType = baseType;
}

public static bool TryParse (AttributeData attributeData,
[NotNullWhen (true)] out BaseTypeData? data)
{
data = null;
var count = attributeData.ConstructorArguments.Length;
string baseType;
string? name = null;
string [] events = [];
string [] delegates = [];
var singleton = false;
string? keepRefUntil = null;
var isStubClass = false;

// custom marshal directive values

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

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

foreach (var (argumentName, value) in attributeData.NamedArguments) {
switch (argumentName) {
case "Name":
name = (string?) value.Value!;
break;
case "Events":
events = value.Values.Select (
v => ((INamedTypeSymbol) v.Value!).ToDisplayString ())
.ToArray ();
break;
case "Delegates":
delegates = value.Values.Select (v => (string) v.Value!).ToArray ();
break;
case "Singleton":
singleton = (bool) value.Value!;
break;
case "KeepRefUntil":
keepRefUntil = (string?) value.Value!;
break;
case "IsStubClass":
isStubClass = (bool) value.Value!;
break;
default:
data = null;
return false;
}
}

data = new (baseType) {
Name = name,
Events = events,
Delegates = delegates,
Singleton = singleton,
KeepRefUntil = keepRefUntil,
IsStubClass = isStubClass,
};
return true;
}

public bool Equals (BaseTypeData other)
{
var stringCollectionComparer = new CollectionComparer<string?> (StringComparer.Ordinal);

if (BaseType != other.BaseType)
return false;
if (Name != other.Name)
return false;
if (!stringCollectionComparer.Equals (Events, other.Events))
return false;
if (!stringCollectionComparer.Equals (Delegates, other.Delegates))
return false;
if (Singleton != other.Singleton)
return false;
if (KeepRefUntil != other.KeepRefUntil)
return false;
return IsStubClass == other.IsStubClass;
}

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

/// <inheritdoc />
public override int GetHashCode ()
{
var hash = new HashCode ();
hash.Add (BaseType);
hash.Add (Name);
foreach (var e in Events) {
hash.Add (e);
}
foreach (var d in Delegates) {
hash.Add (d);
}
hash.Add (Singleton);
hash.Add (KeepRefUntil);
return hash.ToHashCode ();
}

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

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

public override string ToString ()
{
var sb = new StringBuilder ($"{{ BaseType: {BaseType}, Name: {Name ?? "null"}, ");
sb.Append ("Events: [");
sb.AppendJoin (", ", Events);
sb.Append ("], Delegates: [");
sb.AppendJoin (", ", Delegates);
sb.Append ($"], Singleton: {Singleton}, KeepRefUntil: {KeepRefUntil ?? "null"}, IsStubClass: {IsStubClass} }}");
return sb.ToString ();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Microsoft.Macios.Transformer.Attributes;

public struct ExportData : IEquatable<ExportData> {
struct ExportData : IEquatable<ExportData> {

/// <summary>
/// The exported native selector.
Expand All @@ -31,7 +31,7 @@ public ExportData (string? selector, ArgumentSemantic argumentSemantic)
}

/// <summary>
/// Try to parse the attribute data to retrieve the information of an ExportAttribute&lt;T&gt;.
/// Try to parse the attribute data to retrieve the information of an ExportAttribute.
/// </summary>
/// <param name="attributeData">The attribute data to be parsed.</param>
/// <param name="data">The parsed data. Null if we could not parse the attribute data.</param>
Expand All @@ -51,9 +51,6 @@ public static bool TryParse (AttributeData attributeData,
selector = (string?) attributeData.ConstructorArguments [0].Value!;
break;
case 2:
// there are two possible cases in this situation.
// 1. The second argument is an ArgumentSemantic
// 2. The second argument is a T
selector = (string?) attributeData.ConstructorArguments [0].Value!;
argumentSemantic = (ArgumentSemantic) attributeData.ConstructorArguments [1].Value!;
break;
Expand Down
70 changes: 66 additions & 4 deletions src/rgen/Microsoft.Macios.Transformer/Attributes/FieldData.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,82 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

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

namespace Microsoft.Macios.Transformer.Attributes;

public struct FieldData : IEquatable<FieldData> {
struct FieldData : IEquatable<FieldData> {

public string SymbolName { get; }
public string? LibraryName { get; }

internal FieldData (string symbolName, string? libraryName)
{
SymbolName = symbolName;
LibraryName = libraryName;
}

internal FieldData (string symbolName) : this (symbolName, null) { }

public static bool TryParse (AttributeData attributeData,
[NotNullWhen (true)] out FieldData? data)
{
data = default;

var count = attributeData.ConstructorArguments.Length;
string? symbolName;
string? libraryName = null;
switch (count) {
case 1:
symbolName = (string?) attributeData.ConstructorArguments [0].Value!;
break;
case 2:
symbolName = (string?) attributeData.ConstructorArguments [0].Value!;
libraryName = (string?) attributeData.ConstructorArguments [1].Value!;
break;
default:
// 0 should not be an option.
return false;
}

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

// LibraryName can be a param value
foreach (var (name, value) in attributeData.NamedArguments) {
switch (name) {
case "LibraryName":
libraryName = (string?) value.Value!;
break;
default:
data = null;
return false;
}
}
data = new (symbolName, libraryName);
return true;
}

public bool Equals (FieldData other)
{
throw new NotImplementedException ();
if (SymbolName != other.SymbolName)
return false;
return LibraryName == other.LibraryName;
}

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

/// <inheritdoc />
public override int GetHashCode ()
{
throw new NotImplementedException ();
return HashCode.Combine (SymbolName, LibraryName);
}

public static bool operator == (FieldData x, FieldData y)
Expand All @@ -31,4 +88,9 @@ public override int GetHashCode ()
{
return !(x == y);
}

public override string ToString ()
{
return $"{{ SymbolName: '{SymbolName}' LibraryName: '{LibraryName ?? "null"}' }}";
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Macios.Transformer.Attributes;

namespace Microsoft.Macios.Generator.DataModel;

/// <summary>
/// This struct works as a union to store the possible BindingTypeData that can be present in the bindings.
/// </summary>
readonly struct BindingInfo : IEquatable<BindingInfo> {

public BaseTypeData BaseTypeData { get; }

public BindingInfo (BaseTypeData baseTypeData)
{
BaseTypeData = baseTypeData;
}

/// <inheritdoc />
public bool Equals (BindingInfo other)
{
Expand Down
Loading