Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ public ImmutableArray<Diagnostic> ValidateSmartEnum (BaseTypeDeclarationSyntax d
public ImmutableArray<Diagnostic> Analyze (string matchedAttribute, PlatformName _,
BaseTypeDeclarationSyntax declarationNode, INamedTypeSymbol symbol)
=> matchedAttribute switch {
AttributesNames.BindingClassAttribute => ValidateClass (declarationNode, symbol),
AttributesNames.BindingCategoryAttribute => ValidateCategory (declarationNode, symbol),
AttributesNames.BindingProtocolAttribute => ValidateProtocol (declarationNode, symbol),
AttributesNames.BindingSmartEnumAttribute => ValidateSmartEnum (declarationNode, symbol),
AttributesNames.BindingStrongDictionaryAttribute => ValidateStrongDictionary (declarationNode, symbol),
AttributesNames.ClassAttribute => ValidateClass (declarationNode, symbol),
AttributesNames.CategoryAttribute => ValidateCategory (declarationNode, symbol),
AttributesNames.ProtocolAttribute => ValidateProtocol (declarationNode, symbol),
AttributesNames.SmartEnumAttribute => ValidateSmartEnum (declarationNode, symbol),
AttributesNames.StrongDictionaryAttribute => ValidateStrongDictionary (declarationNode, symbol),
_ => throw new InvalidOperationException ($"Not recognized attribute {matchedAttribute}.")
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public override void Initialize (AnalysisContext context)
void AnalysisContext (SyntaxNodeAnalysisContext context)
=> this.AnalyzeBindingType (context);

static readonly HashSet<string> attributes = [AttributesNames.BindingSmartEnumAttribute];
static readonly HashSet<string> attributes = [AttributesNames.SmartEnumAttribute];
public IReadOnlySet<string> AttributeNames => attributes;

public ImmutableArray<Diagnostic> Analyze (string matchedAttribute, PlatformName platformName, EnumDeclarationSyntax declarationNode,
Expand Down
34 changes: 17 additions & 17 deletions src/rgen/Microsoft.Macios.Generator/AttributesNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ namespace Microsoft.Macios.Generator;
/// </summary>
static class AttributesNames {

public const string BindingCategoryAttribute = "ObjCBindings.BindingTypeAttribute<ObjCBindings.Category>";
public const string BindingClassAttribute = "ObjCBindings.BindingTypeAttribute<ObjCBindings.Class>";
public const string BindingCoreImageFilterAttribute = "ObjCBindings.BindingTypeAttribute<ObjCBindings.CoreImageFilter>";
public const string BindingSmartEnumAttribute = "ObjCBindings.BindingTypeAttribute<ObjCBindings.SmartEnum>";
public const string CategoryAttribute = "ObjCBindings.BindingTypeAttribute<ObjCBindings.Category>";
public const string ClassAttribute = "ObjCBindings.BindingTypeAttribute<ObjCBindings.Class>";
public const string CoreImageFilterAttribute = "ObjCBindings.BindingTypeAttribute<ObjCBindings.CoreImageFilter>";
public const string SmartEnumAttribute = "ObjCBindings.BindingTypeAttribute<ObjCBindings.SmartEnum>";
public const string BindFromAttribute = "ObjCBindings.BindFromAttribute";
public const string BindingProtocolAttribute = "ObjCBindings.BindingTypeAttribute<ObjCBindings.Protocol>";
public const string BindingStrongDictionaryAttribute = "ObjCBindings.BindingTypeAttribute<ObjCBindings.StrongDictionary>";
public const string ProtocolAttribute = "ObjCBindings.BindingTypeAttribute<ObjCBindings.Protocol>";
public const string StrongDictionaryAttribute = "ObjCBindings.BindingTypeAttribute<ObjCBindings.StrongDictionary>";
public const string FieldAttribute = "ObjCBindings.FieldAttribute";
public const string EnumFieldAttribute = "ObjCBindings.FieldAttribute<ObjCBindings.EnumValue>";
public const string FieldPropertyAttribute = "ObjCBindings.FieldAttribute<ObjCBindings.Property>";
Expand All @@ -27,32 +27,32 @@ static class AttributesNames {
public const string NativeAttribute = "ObjCRuntime.NativeAttribute";

public static readonly string [] BindingTypes = [
BindingCategoryAttribute,
BindingClassAttribute,
BindingProtocolAttribute,
BindingStrongDictionaryAttribute,
BindingCoreImageFilterAttribute,
BindingSmartEnumAttribute,
CategoryAttribute,
ClassAttribute,
ProtocolAttribute,
StrongDictionaryAttribute,
CoreImageFilterAttribute,
SmartEnumAttribute,
];


public static string? GetBindingTypeAttributeName<T> () where T : Enum
{
var type = typeof (T);
if (type == typeof (ObjCBindings.Category)) {
return BindingCategoryAttribute;
return CategoryAttribute;
}
if (type == typeof (ObjCBindings.Class)) {
return BindingClassAttribute;
return ClassAttribute;
}
if (type == typeof (ObjCBindings.Protocol)) {
return BindingProtocolAttribute;
return ProtocolAttribute;
}
if (type == typeof (ObjCBindings.StrongDictionary)) {
return BindingStrongDictionaryAttribute;
return StrongDictionaryAttribute;
}
if (type == typeof (ObjCBindings.SmartEnum)) {
return BindingSmartEnumAttribute;
return SmartEnumAttribute;
}

return null;
Expand Down
41 changes: 38 additions & 3 deletions src/rgen/Microsoft.Macios.Generator/DataModel/TypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ public bool IsDictionaryContainer {
/// </summary>
public bool IsGenericType { get; init; }

/// <summary>
/// True if the type represents a ObjC protocol.
/// </summary>
public bool IsProtocol { get; init; }

readonly ImmutableArray<string> parents = [];
/// <summary>
/// Array of the parent types of the type.
Expand Down Expand Up @@ -239,6 +244,7 @@ symbol is IArrayTypeSymbol arrayTypeSymbol
IsDelegate = symbol.TypeKind == TypeKind.Delegate;
IsNativeIntegerType = symbol.IsNativeIntegerType;
IsNativeEnum = symbol.HasAttribute (AttributesNames.NativeAttribute);
IsProtocol = symbol.HasAttribute (AttributesNames.ProtocolAttribute);

// data that we can get from the symbol without being INamedType
symbol.GetInheritance (
Expand Down Expand Up @@ -318,6 +324,8 @@ public bool Equals (TypeInfo other)
return false;
if (Delegate != other.Delegate)
return false;
if (IsProtocol != other.IsProtocol)
return false;

// compare base classes and interfaces, order does not matter at all
var listComparer = new CollectionComparer<string> ();
Expand All @@ -338,7 +346,32 @@ public override bool Equals (object? obj)
/// <inheritdoc/>
public override int GetHashCode ()
{
return HashCode.Combine (FullyQualifiedName, IsNullable, IsBlittable, IsSmartEnum, IsArray, IsReferenceType, IsVoid);
var hashCode = new HashCode ();
hashCode.Add (FullyQualifiedName);
hashCode.Add (SpecialType);
hashCode.Add (MetadataName);
hashCode.Add (IsNullable);
hashCode.Add (IsBlittable);
hashCode.Add (IsSmartEnum);
hashCode.Add (IsArray);
hashCode.Add (IsReferenceType);
hashCode.Add (IsStruct);
hashCode.Add (IsVoid);
hashCode.Add (EnumUnderlyingType);
hashCode.Add (IsInterface);
hashCode.Add (IsNativeIntegerType);
hashCode.Add (IsNativeEnum);
hashCode.Add (Delegate);
hashCode.Add (IsProtocol);
foreach (var parent in parents) {
hashCode.Add (parent);
}

foreach (var @interface in interfaces) {
hashCode.Add (@interface);
}

return hashCode.ToHashCode ();
}

public static bool operator == (TypeInfo left, TypeInfo right)
Expand Down Expand Up @@ -415,13 +448,15 @@ public override string ToString ()
sb.Append ($"IsArray: {IsArray}, ");
sb.Append ($"IsReferenceType: {IsReferenceType}, ");
sb.Append ($"IsStruct: {IsStruct}, ");
sb.Append ($"IsVoid : {IsVoid}, ");
sb.Append ($"IsNSObject : {IsNSObject}, ");
sb.Append ($"IsVoid: {IsVoid}, ");
sb.Append ($"IsNSObject: {IsNSObject}, ");
sb.Append ($"IsDictionaryContainer: {IsDictionaryContainer}, ");
sb.Append ($"IsNativeObject: {IsINativeObject}, ");
sb.Append ($"IsInterface: {IsInterface}, ");
sb.Append ($"IsNativeIntegerType: {IsNativeIntegerType}, ");
sb.Append ($"IsNativeEnum: {IsNativeEnum}, ");
sb.Append ($"IsProtocol: {IsProtocol}, ");
sb.Append ($"Delegate: {Delegate?.ToString () ?? "null"}, ");
sb.Append ($"EnumUnderlyingType: '{EnumUnderlyingType?.ToString () ?? "null"}', ");
sb.Append ("Parents: [");
sb.AppendJoin (", ", parents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static bool IsSmartEnum (this ITypeSymbol symbol)
// a type is a smart enum if its type is a enum one AND it was decorated with the
// binding type attribute
return symbol.TypeKind == TypeKind.Enum
&& symbol.HasAttribute (AttributesNames.BindingSmartEnumAttribute);
&& symbol.HasAttribute (AttributesNames.SmartEnumAttribute);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public void GetFieldAttributeName<T> (T @enum, string? expectedName) where T : E
[Theory]
[InlineData (StringComparison.Ordinal, null)]
[InlineData (EnumValue.Default, null)]
[InlineData (Category.Default, AttributesNames.BindingCategoryAttribute)]
[InlineData (Class.Default, AttributesNames.BindingClassAttribute)]
[InlineData (Protocol.Default, AttributesNames.BindingProtocolAttribute)]
[InlineData (StrongDictionary.Default, AttributesNames.BindingStrongDictionaryAttribute)]
[InlineData (Category.Default, AttributesNames.CategoryAttribute)]
[InlineData (Class.Default, AttributesNames.ClassAttribute)]
[InlineData (Protocol.Default, AttributesNames.ProtocolAttribute)]
[InlineData (StrongDictionary.Default, AttributesNames.StrongDictionaryAttribute)]
public void GetBindingTypeAttributeName<T> (T @enum, string? expectedName) where T : Enum
{
Assert.NotNull (@enum);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public enum AVCaptureDeviceType {
Assert.Equal ("AVFoundation.AVCaptureDeviceType", codeChanges.FullyQualifiedSymbol);
Assert.Equal (BindingType.SmartEnum, codeChanges.BindingType);
Assert.Single (codeChanges.Attributes);
Assert.Equal (AttributesNames.BindingSmartEnumAttribute, codeChanges.Attributes [0].Name);
Assert.Equal (AttributesNames.SmartEnumAttribute, codeChanges.Attributes [0].Name);
Assert.Empty (codeChanges.EnumMembers);
Assert.Equal (BindingType.SmartEnum, codeChanges.BindingType);
}
Expand Down Expand Up @@ -80,7 +80,7 @@ public enum AVCaptureDeviceType {
Assert.Equal ("AVFoundation.AVCaptureDeviceType", codeChanges.FullyQualifiedSymbol);
Assert.Equal (BindingType.SmartEnum, codeChanges.BindingType);
Assert.Single (codeChanges.Attributes);
Assert.Equal (AttributesNames.BindingSmartEnumAttribute, codeChanges.Attributes [0].Name);
Assert.Equal (AttributesNames.SmartEnumAttribute, codeChanges.Attributes [0].Name);
Assert.Equal (BindingType.SmartEnum, codeChanges.BindingType);
// validate that we have the 3 members and their attrs
Assert.Equal (3, codeChanges.EnumMembers.Length);
Expand Down Expand Up @@ -123,7 +123,7 @@ public enum AVCaptureDeviceType {
Assert.Equal ("AVFoundation.AVCaptureDeviceType", codeChanges.FullyQualifiedSymbol);
Assert.Equal (BindingType.SmartEnum, codeChanges.BindingType);
Assert.Single (codeChanges.Attributes);
Assert.Equal (AttributesNames.BindingSmartEnumAttribute, codeChanges.Attributes [0].Name);
Assert.Equal (AttributesNames.SmartEnumAttribute, codeChanges.Attributes [0].Name);
Assert.Empty (codeChanges.EnumMembers);
Assert.Equal (BindingType.SmartEnum, codeChanges.BindingType);
}
Expand Down Expand Up @@ -159,7 +159,7 @@ public enum AVCaptureDeviceType {
Assert.Equal ("AVFoundation.AVCaptureDeviceType", codeChanges.FullyQualifiedSymbol);
Assert.Equal (BindingType.SmartEnum, codeChanges.BindingType);
Assert.Single (codeChanges.Attributes);
Assert.Equal (AttributesNames.BindingSmartEnumAttribute, codeChanges.Attributes [0].Name);
Assert.Equal (AttributesNames.SmartEnumAttribute, codeChanges.Attributes [0].Name);
Assert.Equal (BindingType.SmartEnum, codeChanges.BindingType);
// validate that we have the 3 members and their attrs
Assert.Equal (2, codeChanges.EnumMembers.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,53 @@ public class TestClass {
BindAs = new (ReturnTypeForNSObject ("Foundation.NSNumber")),
}
];

const string protocolProperty = @"
using System;
using Foundation;
using ObjCBindings;

namespace Test;

using ObjCBindings;

[BindingType<Protocol>]
public partial interface ITestProtocol {
}

public class TestClass {

[Export<Property>(""name"")]
public ITestProtocol Name { get; }
}
";
yield return [
protocolProperty,
new Property (
name: "Name",
returnType: ReturnTypeForInterface ("Test.ITestProtocol", isProtocol: true),
symbolAvailability: new (),
attributes: [
new (name: "ObjCBindings.ExportAttribute<ObjCBindings.Property>", arguments: ["name"]),
],
modifiers: [
SyntaxFactory.Token (kind: SyntaxKind.PublicKeyword),
],
accessors: [
new (
accessorKind: AccessorKind.Getter,
symbolAvailability: new (),
exportPropertyData: null,
attributes: [],
modifiers: []
)
]
) {
NeedsBackingField = true,
RequiresDirtyCheck = true,
ExportPropertyData = new (selector: "name"),
}
];
}

IEnumerator IEnumerable.GetEnumerator ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,17 @@ public static TypeInfo ReturnTypeForGeneric (string genericName, bool isNullable
isNullable: isNullable
);

public static TypeInfo ReturnTypeForInterface (string interfaceName)
public static TypeInfo ReturnTypeForInterface (string interfaceName, bool isNullable = false, bool isProtocol = false)
=> new (
name: interfaceName,
isNullable: isNullable,
isArray: false,
isReferenceType: true
) {
Parents = [],
IsInterface = true,
IsProtocol = isProtocol,
Parents = [],
Interfaces = []
};

public static TypeInfo ReturnTypeForStruct (string structName, bool isBlittable = false)
Expand Down