-
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 parsing of the backing field attr in the transfo…
…rmer.
- Loading branch information
1 parent
0e52702
commit 8a1e32f
Showing
3 changed files
with
188 additions
and
3 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/rgen/Microsoft.Macios.Transformer/Attributes/BackingFieldTypeData.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,86 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Microsoft.Macios.Transformer.Attributes; | ||
|
||
readonly struct BackingFieldTypeData : IEquatable<BackingFieldTypeData> { | ||
|
||
public string TypeName { get; } | ||
|
||
public BackingFieldTypeData (string typeName) | ||
{ | ||
TypeName = typeName; | ||
} | ||
|
||
public static bool TryParse (AttributeData attributeData, | ||
[NotNullWhen (true)] out BackingFieldTypeData? data) | ||
{ | ||
data = null; | ||
var count = attributeData.ConstructorArguments.Length; | ||
string backingField; | ||
// custom marshal directive values | ||
|
||
switch (count) { | ||
case 1: | ||
backingField = ((INamedTypeSymbol) attributeData.ConstructorArguments [0].Value!).ToDisplayString (); | ||
break; | ||
default: | ||
// 0 should not be an option.. | ||
return false; | ||
} | ||
|
||
if (attributeData.NamedArguments.Length == 0) { | ||
data = new (backingField); | ||
return true; | ||
} | ||
|
||
foreach (var (argumentName, value) in attributeData.NamedArguments) { | ||
switch (argumentName) { | ||
case "BackingFieldType": | ||
backingField = ((INamedTypeSymbol) value.Value!).ToDisplayString (); | ||
break; | ||
default: | ||
data = null; | ||
return false; | ||
} | ||
} | ||
|
||
data = new(backingField); | ||
return true; | ||
} | ||
|
||
public bool Equals (BackingFieldTypeData other) | ||
{ | ||
return TypeName == other.TypeName; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool Equals (object? obj) | ||
{ | ||
return obj is BackingFieldTypeData other && Equals (other); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override int GetHashCode () | ||
=> TypeName.GetHashCode (); | ||
|
||
|
||
public static bool operator == (BackingFieldTypeData x, BackingFieldTypeData y) | ||
{ | ||
return x.Equals (y); | ||
} | ||
|
||
public static bool operator != (BackingFieldTypeData x, BackingFieldTypeData y) | ||
{ | ||
return !(x == y); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override string ToString () | ||
{ | ||
return $"{{ BackingFieldType: '{TypeName}' }}"; | ||
} | ||
} |
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
94 changes: 94 additions & 0 deletions
94
tests/rgen/Microsoft.Macios.Transformer.Tests/Attributes/BackingFieldTypeDataTests.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,94 @@ | ||
// 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 BackingFieldTypeDataTests : BaseTransformerTestClass { | ||
|
||
class TestDataTryCreate : IEnumerable<object []> { | ||
public IEnumerator<object []> GetEnumerator () | ||
{ | ||
var path = "/some/random/path.cs"; | ||
const string nsnumberBackingFieldType = @" | ||
using System; | ||
using Foundation; | ||
using ObjCRuntime; | ||
[NoTV, Mac (15, 0), NoiOS, NoMacCatalyst] | ||
[BackingFieldType (typeof (NSNumber))] | ||
public enum ASAuthorizationProviderExtensionEncryptionAlgorithm { | ||
[Field (""ASAuthorizationProviderExtensionEncryptionAlgorithmECDHE_A256GCM"")] | ||
EcdheA256Gcm, | ||
[Field (""ASAuthorizationProviderExtensionEncryptionAlgorithmHPKE_P256_SHA256_AES_GCM_256"")] | ||
HpkeP256Sha256AesGcm256, | ||
[Field (""ASAuthorizationProviderExtensionEncryptionAlgorithmHPKE_P384_SHA384_AES_GCM_256"")] | ||
HpkeP384Sha384AesGcm256, | ||
[Field (""ASAuthorizationProviderExtensionEncryptionAlgorithmHPKE_Curve25519_SHA256_ChachaPoly"")] | ||
HpkeCurve25519Sha256ChachaPoly, | ||
} | ||
"; | ||
yield return [(Source: nsnumberBackingFieldType, Path: path), new BackingFieldTypeData ("Foundation.NSNumber")]; | ||
|
||
const string nsstringBackingFieldType = @" | ||
using System; | ||
using Foundation; | ||
using ObjCRuntime; | ||
[NoTV, Mac (15, 0), NoiOS, NoMacCatalyst] | ||
[BackingFieldType (typeof (NSString))] | ||
public enum ASAuthorizationProviderExtensionEncryptionAlgorithm { | ||
[Field (""ASAuthorizationProviderExtensionEncryptionAlgorithmECDHE_A256GCM"")] | ||
EcdheA256Gcm, | ||
[Field (""ASAuthorizationProviderExtensionEncryptionAlgorithmHPKE_P256_SHA256_AES_GCM_256"")] | ||
HpkeP256Sha256AesGcm256, | ||
[Field (""ASAuthorizationProviderExtensionEncryptionAlgorithmHPKE_P384_SHA384_AES_GCM_256"")] | ||
HpkeP384Sha384AesGcm256, | ||
[Field (""ASAuthorizationProviderExtensionEncryptionAlgorithmHPKE_Curve25519_SHA256_ChachaPoly"")] | ||
HpkeCurve25519Sha256ChachaPoly, | ||
} | ||
"; | ||
yield return [(Source: nsstringBackingFieldType, Path: path), new BackingFieldTypeData ("Foundation.NSString")]; | ||
|
||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); | ||
} | ||
|
||
[Theory] | ||
[AllSupportedPlatformsClassData<TestDataTryCreate>] | ||
void TryeCreateTests (ApplePlatform platform, (string Source, string Path) source, BackingFieldTypeData 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<BaseTypeDeclarationSyntax> () | ||
.LastOrDefault (); | ||
Assert.NotNull (declaration); | ||
|
||
var symbol = semanticModel.GetDeclaredSymbol (declaration); | ||
Assert.NotNull (symbol); | ||
var exportData = symbol.GetAttribute<BackingFieldTypeData> ( | ||
AttributesNames.BackingFieldTypeAttribute, BackingFieldTypeData.TryParse); | ||
Assert.Equal (expectedData, exportData); | ||
} | ||
} |