Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: dotnet/macios
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1bf0dcaac66036f76a0be8818fb3336ebd20307b
Choose a base ref
..
head repository: dotnet/macios
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: fec4ab532e1f21f554486b77abac24cf22a4efeb
Choose a head ref
Showing with 591 additions and 832 deletions.
  1. +35 −0 src/ObjCBindings/BindFromAttribute.cs
  2. +1 −0 src/frameworks.sources
  3. +3 −0 src/rgen/Microsoft.Macios.Bindings.Analyzer/Microsoft.Macios.Bindings.Analyzer.csproj
  4. +98 −0 src/rgen/Microsoft.Macios.Generator/Attributes/BindFromData.cs
  5. +1 −1 src/rgen/Microsoft.Macios.Generator/Attributes/FieldData.cs
  6. +1 −0 src/rgen/Microsoft.Macios.Generator/AttributesNames.cs
  7. +0 −17 src/rgen/Microsoft.Macios.Generator/DataModel/Accessor.Generator.cs
  8. +12 −1 src/rgen/Microsoft.Macios.Generator/DataModel/Accessor.cs
  9. +8 −1 src/rgen/Microsoft.Macios.Generator/DataModel/Method.Generator.cs
  10. +4 −0 src/rgen/Microsoft.Macios.Generator/DataModel/Method.cs
  11. +8 −1 src/rgen/Microsoft.Macios.Generator/DataModel/Parameter.Generator.cs
  12. +7 −3 src/rgen/Microsoft.Macios.Generator/DataModel/Parameter.cs
  13. +7 −19 src/rgen/Microsoft.Macios.Generator/DataModel/Property.Generator.cs
  14. +18 −2 src/rgen/Microsoft.Macios.Generator/DataModel/Property.cs
  15. +0 −5 src/rgen/Microsoft.Macios.Generator/Extensions/TypeSymbolExtensions.Core.cs
  16. +3 −1 src/rgen/Microsoft.Macios.Generator/Extensions/TypeSymbolExtensions.Generator.cs
  17. +2 −2 src/rgen/Microsoft.Macios.Transformer/Attributes/AsyncData.cs
  18. +2 −2 src/rgen/Microsoft.Macios.Transformer/Attributes/BackingFieldTypeData.cs
  19. +2 −2 src/rgen/Microsoft.Macios.Transformer/Attributes/BaseTypeData.cs
  20. +1 −1 src/rgen/Microsoft.Macios.Transformer/AttributesNames.cs
  21. +2 −39 src/rgen/Microsoft.Macios.Transformer/DataModel/Accessor.Transformer.cs
  22. +75 −8 src/rgen/Microsoft.Macios.Transformer/DataModel/Method.Transformer.cs
  23. +0 −109 src/rgen/Microsoft.Macios.Transformer/DataModel/ModifiersFlags.cs
  24. +8 −1 src/rgen/Microsoft.Macios.Transformer/DataModel/Parameter.Transformer.cs
  25. +13 −97 src/rgen/Microsoft.Macios.Transformer/DataModel/Property.Transformer.cs
  26. +3 −3 src/rgen/Microsoft.Macios.Transformer/DataModel/TypeInfo.Transformer.cs
  27. +8 −4 src/rgen/Microsoft.Macios.Transformer/Extensions/TypeSymbolExtensions.Transformer.cs
  28. +2 −2 tests/rgen/Microsoft.Macios.Generator.Tests/Attributes/FieldDataTests.cs
  29. +3 −3 tests/rgen/Microsoft.Macios.Generator.Tests/DataModel/EnumMemberCodeChangesTests.cs
  30. +69 −0 tests/rgen/Microsoft.Macios.Generator.Tests/DataModel/MethodTests.cs
  31. +41 −0 tests/rgen/Microsoft.Macios.Generator.Tests/DataModel/PropertyTests.cs
  32. +5 −24 tests/rgen/Microsoft.Macios.Generator.Tests/TestDataFactory.cs
  33. +1 −1 tests/rgen/Microsoft.Macios.Transformer.Tests/Attributes/StrongDictionaryDataTests.cs
  34. +92 −102 tests/rgen/Microsoft.Macios.Transformer.Tests/DataModel/MethodTests.cs
  35. +44 −48 tests/rgen/Microsoft.Macios.Transformer.Tests/DataModel/ParameterTests.cs
  36. +0 −329 tests/rgen/Microsoft.Macios.Transformer.Tests/DataModel/PropertyTests.cs
  37. +4 −4 tests/rgen/Microsoft.Macios.Transformer.Tests/Extensions/TypeSymbolExtensionsTests.cs
  38. +8 −0 tests/rgen/Microsoft.Macios.Transformer.Tests/Microsoft.Macios.Transformer.Tests.csproj
35 changes: 35 additions & 0 deletions src/ObjCBindings/BindFromAttribute.cs
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;
}
}
1 change: 1 addition & 0 deletions src/frameworks.sources
Original file line number Diff line number Diff line change
@@ -1935,6 +1935,7 @@ SHARED_CORE_SOURCES = \
DotNetGlobals.cs \
MinimumVersions.cs \
MonoPInvokeCallbackAttribute.cs \
ObjCBindings/BindFromAttribute.cs \
ObjCBindings/BindingTypeAttribute.cs \
ObjCBindings/BindingTypeTag.cs \
ObjCBindings/ExportAttribute.cs \
Original file line number Diff line number Diff line change
@@ -39,6 +39,9 @@
<Compile Include="../Microsoft.Macios.Generator/DictionaryComparer.cs" >
<Link>Generator/DictionaryComparer.cs</Link>
</Compile>
<Compile Include="../Microsoft.Macios.Generator/Attributes/BindFromData.cs" >
<Link>Generator/Attributes/BindFromData.cs</Link>
</Compile>
<Compile Include="../Microsoft.Macios.Generator/Attributes/FieldData.cs" >
<Link>Generator/Attributes/FieldData.cs</Link>
</Compile>
98 changes: 98 additions & 0 deletions src/rgen/Microsoft.Macios.Generator/Attributes/BindFromData.cs
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"}' }}";
}
}
Original file line number Diff line number Diff line change
@@ -137,6 +137,6 @@ public override int GetHashCode ()
/// <inheritdoc />
public override string ToString ()
{
return $"{{ SymbolName: '{SymbolName}' LibraryName: '{LibraryName ?? "null"}', Flags: '{Flags}' }}";
return $"{{ SymbolName: '{SymbolName}', LibraryName: '{LibraryName ?? "null"}', Flags: '{Flags}' }}";
}
}
1 change: 1 addition & 0 deletions src/rgen/Microsoft.Macios.Generator/AttributesNames.cs
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ static class AttributesNames {
public const string BindingAttribute = "ObjCBindings.BindingTypeAttribute";
public const string BindingCategoryAttribute = "ObjCBindings.BindingTypeAttribute<ObjCBindings.Category>";
public const string BindingClassAttribute = "ObjCBindings.BindingTypeAttribute<ObjCBindings.Class>";
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 FieldAttribute = "ObjCBindings.FieldAttribute";
Original file line number Diff line number Diff line change
@@ -2,8 +2,6 @@
// Licensed under the MIT License.

using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.Macios.Generator.Attributes;
using Microsoft.Macios.Generator.Availability;
@@ -17,9 +15,6 @@ readonly partial struct Accessor {
/// </summary>
public ExportData<ObjCBindings.Property>? ExportPropertyData { get; init; }

/// <summary>
/// State if we should marshal native exceptions when generating the accessor.
/// </summary>
public bool MarshalNativeExceptions
=> ExportPropertyData is not null && ExportPropertyData.Value.Flags.HasFlag (ObjCBindings.Property.MarshalNativeExceptions);

@@ -77,16 +72,4 @@ public Accessor (AccessorKind accessorKind,
public bool ShouldMarshalNativeExceptions (in Property property)
=> MarshalNativeExceptions || property.MarshalNativeExceptions;

/// <inheritdoc />
public override string ToString ()
{
var sb = new StringBuilder ($"{{ Kind: {Kind}, ");
sb.Append ($"Supported Platforms: {SymbolAvailability}, ");
sb.Append ($"ExportData: {ExportPropertyData?.ToString () ?? "null"} Modifiers: [");
sb.AppendJoin (",", Modifiers.Select (x => x.Text));
sb.Append ("], Attributes: [");
sb.AppendJoin (", ", Attributes);
sb.Append ("] }");
return sb.ToString ();
}
}
13 changes: 12 additions & 1 deletion src/rgen/Microsoft.Macios.Generator/DataModel/Accessor.cs
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ namespace Microsoft.Macios.Generator.DataModel;
/// <summary>
/// List of attribute code changes of the accessor.
/// </summary>
public ImmutableArray<AttributeCodeChange> Attributes { get; } = [];
public ImmutableArray<AttributeCodeChange> Attributes { get; }

/// <summary>
/// List of modifiers of the accessor.
@@ -70,4 +70,15 @@ public override int GetHashCode ()
{
return !left.Equals (right);
}

/// <inheritdoc />
public override string ToString ()
{
var sb = new StringBuilder ($"{{ Kind: {Kind}, Supported Platforms: {SymbolAvailability}, ExportData: {ExportPropertyData?.ToString () ?? "null"} Modifiers: [");
sb.AppendJoin (",", Modifiers.Select (x => x.Text));
sb.Append ("], Attributes: [");
sb.AppendJoin (", ", Attributes);
sb.Append ("] }");
return sb.ToString ();
}
}
Original file line number Diff line number Diff line change
@@ -20,6 +20,11 @@ readonly partial struct Method {
/// </summary>
public ExportData<ObjCBindings.Method> ExportMethodData { get; }

/// <summary>
/// Returns the bind from data if present in the binding.
/// </summary>
public BindFromData? BindAs { get; init; }

/// <summary>
/// Returns if the method was marked as thread safe.
/// </summary>
@@ -79,7 +84,9 @@ public static bool TryCreate (MethodDeclarationSyntax declaration, RootBindingCo
exportMethodData: exportData,
attributes: attributes,
modifiers: [.. declaration.Modifiers],
parameters: parametersBucket.ToImmutableArray ());
parameters: parametersBucket.ToImmutableArray ()) {
BindAs = method.GetBindFromData (),
};

return true;
}
4 changes: 4 additions & 0 deletions src/rgen/Microsoft.Macios.Generator/DataModel/Method.cs
Original file line number Diff line number Diff line change
@@ -61,6 +61,8 @@ public bool Equals (Method other)
return false;
if (ExportMethodData != other.ExportMethodData)
return false;
if (BindAs != other.BindAs)
return false;

var attrsComparer = new AttributesEqualityComparer ();
if (!attrsComparer.Equals (Attributes, other.Attributes))
@@ -86,6 +88,7 @@ public override int GetHashCode ()
hashCode.Add (Type);
hashCode.Add (Name);
hashCode.Add (ReturnType);
hashCode.Add (BindAs);
foreach (var modifier in Modifiers) {
hashCode.Add (modifier);
}
@@ -119,6 +122,7 @@ public override string ToString ()
sb.Append ($"ReturnType: {ReturnType}, ");
sb.Append ($"SymbolAvailability: {SymbolAvailability}, ");
sb.Append ($"ExportMethodData: {ExportMethodData}, ");
sb.Append ($"BindAs: {BindAs}, ");
sb.Append ("Attributes: [");
sb.AppendJoin (", ", Attributes);
sb.Append ("], Modifiers: [");
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Macios.Generator.Attributes;
using Microsoft.Macios.Generator.Extensions;

namespace Microsoft.Macios.Generator.DataModel;
@@ -19,7 +20,12 @@ public enum VariableType {
PrimitivePointer,
StringPointer,
}


/// <summary>
/// Returns the bind from data if present in the binding.
/// </summary>
public BindFromData? BindAs { get; init; }

public static bool TryCreate (IParameterSymbol symbol, ParameterSyntax declaration, SemanticModel semanticModel,
[NotNullWhen (true)] out Parameter? parameter)
{
@@ -30,6 +36,7 @@ public static bool TryCreate (IParameterSymbol symbol, ParameterSyntax declarati
}

parameter = new (symbol.Ordinal, new (symbol.Type), symbol.Name) {
BindAs = symbol.GetBindFromData (),
IsOptional = symbol.IsOptional,
IsParams = symbol.IsParams,
IsThis = symbol.IsThis,
10 changes: 7 additions & 3 deletions src/rgen/Microsoft.Macios.Generator/DataModel/Parameter.cs
Original file line number Diff line number Diff line change
@@ -78,7 +78,7 @@ public Parameter (int position, TypeInfo type, string name)
Type = type;
Name = name;
}

/// <inheritdoc/>
public bool Equals (Parameter other)
{
@@ -98,6 +98,8 @@ public bool Equals (Parameter other)
return false;
if (ReferenceKind != other.ReferenceKind)
return false;
if (BindAs != other.BindAs)
return false;
if (Delegate != other.Delegate)
return false;

@@ -124,6 +126,7 @@ public override int GetHashCode ()
hashCode.Add (DefaultValue);
hashCode.Add ((int) ReferenceKind);
hashCode.Add (Delegate);
hashCode.Add (BindAs);
return hashCode.ToHashCode ();
}

@@ -144,13 +147,14 @@ public override string ToString ()
sb.Append ($"Position: {Position}, ");
sb.Append ($"Type: {Type}, ");
sb.Append ($"Name: {Name}, ");
sb.Append ("Attributes: ");
sb.Append ("Attributes: [");
sb.AppendJoin (", ", Attributes);
sb.Append ($" IsOptional: {IsOptional}, ");
sb.Append ($"] IsOptional: {IsOptional}, ");
sb.Append ($"IsParams: {IsParams}, ");
sb.Append ($"IsThis: {IsThis}, ");
sb.Append ($"DefaultValue: {DefaultValue}, ");
sb.Append ($"ReferenceKind: {ReferenceKind}, ");
sb.Append ($"BindAs: {BindAs?.ToString () ?? "null"}, ");
sb.Append ($"Delegate: {Delegate?.ToString () ?? "null"} }}");
return sb.ToString ();
}
Loading