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 support to the export method to provide custom marshalling. #22009

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Changes from 1 commit
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
Next Next commit
[Rgen] Add support to the export method to provide custom marshalling.
Add a new flag that can be used to let the generator know that a exposed
method or property can have a custom marshalling call.

Later the analyzer will make sure that only when the appropiate flag is
set, that the extra name parameters are allowed.
mandel-macaque committed Jan 18, 2025

Verified

This commit was signed with the committer’s verified signature.
sawa-zen Takayoshi Sawada
commit f0e0e27a8efd1bae8970439555d58329a54f3cbb
24 changes: 24 additions & 0 deletions src/ObjCBindings/ExportAttribute.cs
Original file line number Diff line number Diff line change
@@ -27,6 +27,30 @@ public class ExportAttribute<T> : Attribute where T : Enum {
/// </summary >
public ArgumentSemantic ArgumentSemantic { get; set; } = ArgumentSemantic.None;

/// <summary>
/// Get/Set the native prefix to be used in the custom marshal directive.
///
/// The generator will only respect this value if the CustomMarshalDirective flag is set.
/// If the flag is not set the analyzer will raise an compiling error.
/// </summary >
public string? NativePrefix { get; set; } = null;

/// <summary>
/// Get/Set the native suffix to be used in the custom marshal directive.
///
/// The generator will only respect this value if the CustomMarshalDirective flag is set.
/// If the flag is not set the analyzer will raise an compiling error.
/// </summary >
public string? NativeSuffix { get; set; } = null;

/// <summary>
/// Get/Set the library to be used in the custom marshal directive.
///
/// The generator will only respect this value if the CustomMarshalDirective flag is set.
/// If the flag is not set the analyzer will raise an compiling error.
/// </summary >
public string? Library { get; set; } = null;

protected ExportAttribute () { }

/// <summary>
19 changes: 19 additions & 0 deletions src/ObjCBindings/ExportTag.cs
Original file line number Diff line number Diff line change
@@ -49,6 +49,16 @@ public enum Method : Int64 {
/// </summary>
MarshalNativeExceptions = 1 << 4,


/// <summary>
/// Instruct the generator to use a custom marshal directive for the method. When this flag is applied the
/// following name parameters must be provided:
/// - NativePrefix: The prefix to be used in the native method name.
/// - NativeSuffix: The suffix to be used in the native method name.
/// - Library: The library to be used in the custom marshal directive.
/// </summary>
CustomMarshalDirective = 1 << 5,

}

/// <summary>
@@ -78,6 +88,15 @@ public enum Property : Int64 {
/// </summary>
MarshalNativeExceptions = 1 << 4,

/// <summary>
/// Instruct the generator to use a custom marshal directive for the method. When this flag is applied the
/// following name parameters must be provided:
/// - NativePrefix: The prefix to be used in the native method name.
/// - NativeSuffix: The suffix to be used in the native method name.
/// - Library: The library to be used in the custom marshal directive.
/// </summary>
CustomMarshalDirective = 1 << 5,

}
}

86 changes: 79 additions & 7 deletions src/rgen/Microsoft.Macios.Generator/Attributes/ExportData.cs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
// Licensed under the MIT License.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Microsoft.CodeAnalysis;
using ObjCRuntime;

@@ -27,6 +28,27 @@ namespace Microsoft.Macios.Generator.Attributes;
/// Argument semantics to use with the selector.
/// </summary>
public ArgumentSemantic ArgumentSemantic { get; } = ArgumentSemantic.None;

/// <summary>
/// Get the native prefix to be used in the custom marshal directive.
///
/// Should only be present with the CustomeMarshalDirective flag.
/// </summary >
public string? NativePrefix { get; init; }

/// <summary>
/// Get the native sufix to be used in the custom marshal directive.
///
/// Should only be present with the CustomeMarshalDirective flag.
/// </summary >
public string? NativeSuffix { get; init; }

/// <summary>
/// Get the library to be used in the custom marshal directive.
///
/// Should only be present with the CustomeMarshalDirective flag.
/// </summary >
public string? Library { get; init; }

public ExportData () { }

@@ -62,6 +84,12 @@ public static bool TryParse (AttributeData attributeData,
string? selector = null;
ArgumentSemantic argumentSemantic = ArgumentSemantic.None;
T? flags = default;

// custom marshal directive values
string? nativePrefix = null;
string? nativeSuffix = null;
string? library = null;

switch (count) {
case 1:
selector = (string?) attributeData.ConstructorArguments [0].Value!;
@@ -106,14 +134,35 @@ public static bool TryParse (AttributeData attributeData,
case "Flags":
flags = (T) value.Value!;
break;
case "NativePrefix":
nativePrefix = (string?) value.Value!;
break;
case "NativeSuffix":
nativeSuffix = (string?) value.Value!;
break;
case "Library":
library = (string?) value.Value!;
break;
default:
data = null;
return false;
}
}

data = flags is not null ?
new (selector, argumentSemantic, flags) : new (selector, argumentSemantic);
if (flags is not null) {
data = new (selector, argumentSemantic, flags) {
NativePrefix = nativePrefix,
NativeSuffix = nativeSuffix,
Library = library
};
return true;
}

data = new(selector, argumentSemantic) {
NativePrefix = nativePrefix,
NativeSuffix = nativeSuffix,
Library = library
};
return true;
}

@@ -124,10 +173,18 @@ public bool Equals (ExportData<T> other)
return false;
if (ArgumentSemantic != other.ArgumentSemantic)
return false;
if (Flags is not null && other.Flags is not null) {
return Flags.Equals (other.Flags);
}
return false;
if (NativePrefix != other.NativePrefix)
return false;
if (NativeSuffix != other.NativeSuffix)
return false;
if (Library != other.Library)
return false;
return (Flags, other.Flags) switch {
(null, null) => true,
(null, _) => false,
(_, null) => false,
(_, _) => Flags!.Equals (other.Flags)
};
}

/// <inheritdoc />
@@ -155,6 +212,21 @@ public override int GetHashCode ()
/// <inheritdoc />
public override string ToString ()
{
return $"{{ Type: '{typeof (T).FullName}', Selector: '{Selector ?? "null"}', ArgumentSemantic: '{ArgumentSemantic}', Flags: '{Flags}' }}";
var sb = new StringBuilder ("{ Type: '");
sb.Append (typeof (T).FullName);
sb.Append ("', Selector: '");
sb.Append (Selector ?? "null");
sb.Append ("', ArgumentSemantic: '");
sb.Append (ArgumentSemantic);
sb.Append ("', Flags: '");
sb.Append (Flags);
sb.Append ("', NativePrefix: '");
sb.Append (NativePrefix ?? "null");
sb.Append ("', NativeSuffix: '");
sb.Append (NativeSuffix ?? "null");
sb.Append ("', Library: '");
sb.Append (Library ?? "null");
sb.Append ("' }");
return sb.ToString ();
}
}
Original file line number Diff line number Diff line change
@@ -54,22 +54,31 @@ public IEnumerator<object []> GetEnumerator ()
yield return [
Method.Default,
new ExportData<Method> ("symbol", ArgumentSemantic.None, Method.Default),
"{ Type: 'ObjCBindings.Method', Selector: 'symbol', ArgumentSemantic: 'None', Flags: 'Default' }"
"{ Type: 'ObjCBindings.Method', Selector: 'symbol', ArgumentSemantic: 'None', Flags: 'Default', NativePrefix: 'null', NativeSuffix: 'null', Library: 'null' }"
];
yield return [
Method.Default,
new ExportData<Method> ("symbol"),
"{ Type: 'ObjCBindings.Method', Selector: 'symbol', ArgumentSemantic: 'None', Flags: 'Default' }"
"{ Type: 'ObjCBindings.Method', Selector: 'symbol', ArgumentSemantic: 'None', Flags: 'Default', NativePrefix: 'null', NativeSuffix: 'null', Library: 'null' }"
];
yield return [
Property.Default,
new ExportData<Property> ("symbol", ArgumentSemantic.Retain, Property.Default),
"{ Type: 'ObjCBindings.Property', Selector: 'symbol', ArgumentSemantic: 'Retain', Flags: 'Default' }"
"{ Type: 'ObjCBindings.Property', Selector: 'symbol', ArgumentSemantic: 'Retain', Flags: 'Default', NativePrefix: 'null', NativeSuffix: 'null', Library: 'null' }"
];
yield return [
Property.Default,
new ExportData<Property> ("symbol"),
"{ Type: 'ObjCBindings.Property', Selector: 'symbol', ArgumentSemantic: 'None', Flags: 'Default' }"
"{ Type: 'ObjCBindings.Property', Selector: 'symbol', ArgumentSemantic: 'None', Flags: 'Default', NativePrefix: 'null', NativeSuffix: 'null', Library: 'null' }"
];
yield return [
Method.Default,
new ExportData<Method> ("symbol", ArgumentSemantic.None,
Method.Default | Method.CustomMarshalDirective) {
NativePrefix = "xamarin_",
Library = "__Internal"
},
"{ Type: 'ObjCBindings.Method', Selector: 'symbol', ArgumentSemantic: 'None', Flags: 'CustomMarshalDirective', NativePrefix: 'xamarin_', NativeSuffix: 'null', Library: '__Internal' }"
];
}

@@ -81,6 +90,7 @@ IEnumerator IEnumerable.GetEnumerator ()
[ClassData (typeof (TestDataToString))]
void TestFieldDataToString<T> (T @enum, ExportData<T> x, string expected) where T : Enum
{
var str = x.ToString ();
Assert.NotNull (@enum);
Assert.Equal (expected, x.ToString ());
}
Original file line number Diff line number Diff line change
@@ -734,6 +734,78 @@ public partial class MyClass {
]
}
];

const string customMarshallingProperty = @"
using ObjCBindings;

namespace NS;

[BindingType<Class>]
public partial class MyClass {
[Export<Property> (""name"", Flags = Property.CustomMarshalDirective, NativePrefix = ""xamarin_"", Library = ""__Internal"")]
public partial string Name { get; set; } = string.Empty;
}
";

yield return [
customMarshallingProperty,
new CodeChanges (
bindingInfo: new (new BindingTypeData<Class> ()),
name: "MyClass",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.MyClass",
symbolAvailability: new ()
) {
Base = "object",
Interfaces = ImmutableArray<string>.Empty,
Attributes = [
new ("ObjCBindings.BindingTypeAttribute<ObjCBindings.Class>")
],
UsingDirectives = new HashSet<string> { "ObjCBindings" },
Modifiers = [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
SyntaxFactory.Token (SyntaxKind.PartialKeyword)
],
Properties = [
new (
name: "Name",
returnType: ReturnTypeForString (),
symbolAvailability: new (),
attributes: [
new ("ObjCBindings.ExportAttribute<ObjCBindings.Property>", ["name", "ObjCBindings.Property.CustomMarshalDirective", "xamarin_", "__Internal"])
],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
SyntaxFactory.Token (SyntaxKind.PartialKeyword),
],
accessors: [
new (
accessorKind: AccessorKind.Getter,
symbolAvailability: new (),
exportPropertyData: null,
attributes: [],
modifiers: []
),
new (
accessorKind: AccessorKind.Setter,
symbolAvailability: new (),
exportPropertyData: null,
attributes: [],
modifiers: []
),
]
) {
ExportPropertyData = new (
selector: "name",
argumentSemantic: ArgumentSemantic.None,
flags: Property.Default | Property.CustomMarshalDirective) {
NativePrefix = "xamarin_",
Library = "__Internal"
}
}
]
}
];

const string multiPropertyClass = @"
using ObjCBindings;
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
using Microsoft.Macios.Generator.Attributes;
using Microsoft.Macios.Generator.DataModel;
using ObjCBindings;
using ObjCRuntime;
using Xamarin.Tests;
using Xamarin.Utils;
using Xunit;
@@ -419,6 +420,78 @@ public partial interface IProtocol {
]
}
];

const string customMarshallingProperty = @"
using ObjCBindings;
namespace NS;
[BindingType<Protocol>]
public partial interface MyClass {
[Export<Property> (""name"", Flags = Property.CustomMarshalDirective, NativePrefix = ""xamarin_"", Library = ""__Internal"")]
public partial string Name { get; set; } = string.Empty;
}
";

yield return [
customMarshallingProperty,
new CodeChanges (
bindingInfo: new (new BindingTypeData<Protocol> ()),
name: "MyClass",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.MyClass",
symbolAvailability: new ()
) {
Base = null,
Interfaces = [],
Attributes = [
new ("ObjCBindings.BindingTypeAttribute<ObjCBindings.Protocol>")
],
UsingDirectives = new HashSet<string> { "ObjCBindings" },
Modifiers = [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
SyntaxFactory.Token (SyntaxKind.PartialKeyword)
],
Properties = [
new (
name: "Name",
returnType: ReturnTypeForString (),
symbolAvailability: new (),
attributes: [
new ("ObjCBindings.ExportAttribute<ObjCBindings.Property>", ["name", "ObjCBindings.Property.CustomMarshalDirective", "xamarin_", "__Internal"])
],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
SyntaxFactory.Token (SyntaxKind.PartialKeyword),
],
accessors: [
new (
accessorKind: AccessorKind.Getter,
symbolAvailability: new (),
exportPropertyData: null,
attributes: [],
modifiers: []
),
new (
accessorKind: AccessorKind.Setter,
symbolAvailability: new (),
exportPropertyData: null,
attributes: [],
modifiers: []
),
]
) {
ExportPropertyData = new (
selector: "name",
argumentSemantic: ArgumentSemantic.None,
flags: Property.Default | Property.CustomMarshalDirective) {
NativePrefix = "xamarin_",
Library = "__Internal"
}
}
]
}
];

const string multiPropertyInterface = @"
using ObjCBindings;