-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RGen] Implement the BindFrom attribute. (#22087)
The BindFromAttribute is similar to the BindAsAttribute but in the opossite direction. This allows the bindings to be cleaner since we can specify in the partial method the return type we really want and the NSNumber/NSString transformation will be done in the generated code. --------- Co-authored-by: GitHub Actions Autoformatter <[email protected]> Co-authored-by: Jonathan Peppers <[email protected]>
- Loading branch information
1 parent
f3c4396
commit 5c5febc
Showing
21 changed files
with
345 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Diagnostics.CodeAnalysis; | ||
using ObjCRuntime; | ||
|
||
#nullable enable | ||
|
||
namespace ObjCBindings { | ||
|
||
/// <summary> | ||
/// Attribute to bind from a specific type. | ||
/// </summary> | ||
[Experimental ("APL0003")] | ||
[AttributeUsage (AttributeTargets.ReturnValue | AttributeTargets.Property | AttributeTargets.Parameter, AllowMultiple = false)] | ||
public class BindFromAttribute : Attribute { | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BindFromAttribute"/> class. | ||
/// </summary> | ||
public BindFromAttribute (Type type) | ||
{ | ||
Type = type; | ||
} | ||
|
||
/// <summary> | ||
/// The type to bind from. | ||
/// </summary> | ||
public Type Type { get; set; } | ||
|
||
/// <summary> | ||
/// The original type that was bound from. | ||
/// </summary> | ||
public Type? OriginalType { get; set; } = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/rgen/Microsoft.Macios.Generator/Attributes/BindFromData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Microsoft.Macios.Generator.Attributes; | ||
|
||
readonly struct BindFromData : IEquatable<BindFromData> { | ||
|
||
public string Type { get; } | ||
public string? OriginalType { get; } | ||
|
||
public BindFromData (string type) | ||
{ | ||
Type = type; | ||
} | ||
|
||
public BindFromData (string type, string? originalType) | ||
{ | ||
Type = type; | ||
OriginalType = originalType; | ||
} | ||
|
||
|
||
public static bool TryParse (AttributeData attributeData, | ||
[NotNullWhen (true)] out BindFromData? data) | ||
{ | ||
data = null; | ||
var count = attributeData.ConstructorArguments.Length; | ||
string? type; | ||
string? originalType = null; | ||
|
||
switch (count) { | ||
case 1: | ||
type = ((INamedTypeSymbol) attributeData.ConstructorArguments [0].Value!).ToDisplayString (); | ||
break; | ||
default: | ||
// no other constructors are available | ||
return false; | ||
} | ||
|
||
if (attributeData.NamedArguments.Length == 0) { | ||
data = new (type); | ||
return true; | ||
} | ||
|
||
foreach (var (name, value) in attributeData.NamedArguments) { | ||
switch (name) { | ||
case "Type": | ||
type = ((INamedTypeSymbol) value.Value!).ToDisplayString (); | ||
break; | ||
case "OriginalType": | ||
originalType = ((INamedTypeSymbol) value.Value!).ToDisplayString (); | ||
break; | ||
default: | ||
data = null; | ||
return false; | ||
} | ||
} | ||
data = new (type, originalType); | ||
return true; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool Equals (BindFromData other) | ||
{ | ||
return Type == other.Type && OriginalType == other.OriginalType; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool Equals (object? obj) | ||
{ | ||
return obj is BindFromData other && Equals (other); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override int GetHashCode () | ||
{ | ||
return HashCode.Combine (Type, OriginalType); | ||
} | ||
|
||
public static bool operator == (BindFromData x, BindFromData y) | ||
{ | ||
return x.Equals (y); | ||
} | ||
|
||
public static bool operator != (BindFromData x, BindFromData y) | ||
{ | ||
return !(x == y); | ||
} | ||
|
||
public override string ToString () | ||
{ | ||
return $"{{ Type: '{Type}', OriginalType: '{OriginalType ?? "null"}' }}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.