Skip to content

Commit

Permalink
[Rgen] Add the missing code to complety the property datamodel in the…
Browse files Browse the repository at this point in the history
… transformer.

Add the missing methods to be able to correcly parse a property and
store it in the data model. This change adds support for Fields and
Properties.
  • Loading branch information
mandel-macaque committed Feb 1, 2025
1 parent aa4e62c commit 8826755
Show file tree
Hide file tree
Showing 13 changed files with 628 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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;
Expand All @@ -15,6 +17,9 @@ 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);

Expand Down Expand Up @@ -72,4 +77,16 @@ 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: 1 addition & 12 deletions src/rgen/Microsoft.Macios.Generator/DataModel/Accessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -70,15 +70,4 @@ 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
Expand Up @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Macios.Generator.Attributes;
using Microsoft.Macios.Generator.Availability;
using Microsoft.Macios.Generator.Context;
using Microsoft.Macios.Generator.Extensions;
using ObjCRuntime;
Expand All @@ -27,6 +28,9 @@ readonly partial struct Property {
[MemberNotNullWhen (true, nameof (ExportFieldData))]
public bool IsField => ExportFieldData is not null;

/// <summary>
/// Returns if the field was marked as a notification.
/// </summary>
public bool IsNotification
=> IsField && ExportFieldData.Value.FieldData.Flags.HasFlag (ObjCBindings.Property.Notification);

Expand Down Expand Up @@ -116,6 +120,21 @@ public bool RequiresDirtyCheck {
return fieldInfo;
}

internal Property (string name, TypeInfo returnType,
SymbolAvailability symbolAvailability,
ImmutableArray<AttributeCodeChange> attributes,
ImmutableArray<SyntaxToken> modifiers,
ImmutableArray<Accessor> accessors)
{
Name = name;
BackingField = $"_{Name}";
ReturnType = returnType;
SymbolAvailability = symbolAvailability;
Attributes = attributes;
Modifiers = modifiers;
Accessors = accessors;
}

public static bool TryCreate (PropertyDeclarationSyntax declaration, RootBindingContext context,
[NotNullWhen (true)] out Property? change)
{
Expand Down
16 changes: 1 addition & 15 deletions src/rgen/Microsoft.Macios.Generator/DataModel/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private init {
/// <summary>
/// Get the modifiers of the property.
/// </summary>
public ImmutableArray<SyntaxToken> Modifiers { get; } = [];
public ImmutableArray<SyntaxToken> Modifiers { get; init; } = [];

/// <summary>
/// Get the list of accessor changes of the property.
Expand All @@ -80,20 +80,6 @@ private init {
return null;
}

internal Property (string name, TypeInfo returnType,
SymbolAvailability symbolAvailability,
ImmutableArray<AttributeCodeChange> attributes,
ImmutableArray<SyntaxToken> modifiers, ImmutableArray<Accessor> accessors)
{
Name = name;
BackingField = $"_{Name}";
ReturnType = returnType;
SymbolAvailability = symbolAvailability;
Attributes = attributes;
Modifiers = modifiers;
Accessors = accessors;
}

bool CoreEquals (Property other)
{
// this could be a large && but ifs are more readable
Expand Down
2 changes: 1 addition & 1 deletion src/rgen/Microsoft.Macios.Transformer/AttributesNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ static class AttributesNames {
/// When this attribute is applied to a class it will just generate a static class, one that does not derive
/// from NSObject.
/// </summary>
[BindingFlag (AttributeTargets.Class | AttributeTargets.Method)]
[BindingFlag (AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property)]
public const string StaticAttribute = "StaticAttribute";

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.Macios.Generator.Availability;
using Microsoft.Macios.Transformer.Attributes;

namespace Microsoft.Macios.Generator.DataModel;

readonly partial struct Accessor {

public ExportData? ExportPropertyData { get; init; }
readonly ExportData? overrideExportData;

public Accessor () { }
/// <summary>
/// The data of the field attribute used to mark the value as a property binding.
/// </summary>
public ExportData? ExportPropertyData {
get => overrideExportData ?? ExportAttribute;
init => overrideExportData = value;
}

/// <summary>
/// State if we should marshal native exceptions when generating the accessor.
/// </summary>
public bool MarshalNativeExceptions => HasMarshalNativeExceptionsFlag;

public Accessor (AccessorKind accessorKind,
SymbolAvailability symbolAvailability,
Dictionary<string, List<AttributeData>> attributes)
{
Kind = accessorKind;
SymbolAvailability = symbolAvailability;
AttributesDictionary = attributes;

// we trust the modifiers of the property itself
Modifiers = [];
}

/// <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 ("] }");
return sb.ToString ();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ readonly partial struct Method {
/// The data of the export attribute used to mark the value as a property binding.
/// </summary>
public ExportData? ExportMethodData {
get {
return overrideExportData ?? ExportAttribute;
}
get => overrideExportData ?? ExportAttribute;
init => overrideExportData = value;
}

Expand All @@ -50,69 +48,10 @@ public Method (string type,
ReturnType = returnType;
SymbolAvailability = symbolAvailability;
Parameters = parameters;

#pragma warning disable format
// Modifiers are special because we might be dealing with several flags that the user has set in the method.
// We have to add the partial keyword so that we can have the partial implementation of the method later generated
// by the roslyn code generator
Modifiers = this switch {
// internal static partial
{ HasNewFlag: false, HasStaticFlag: true, HasInternalFlag: true }
=> [Token (SyntaxKind.InternalKeyword), Token (SyntaxKind.StaticKeyword), Token (SyntaxKind.PartialKeyword)],

// public static partial
{ HasNewFlag: false, HasStaticFlag: true, HasInternalFlag: false }
=> [Token (SyntaxKind.PublicKeyword), Token (SyntaxKind.StaticKeyword), Token (SyntaxKind.PartialKeyword)],

// internal new static partial
{ HasNewFlag: true, HasStaticFlag: true, HasInternalFlag: true}
=> [Token (SyntaxKind.InternalKeyword), Token (SyntaxKind.NewKeyword), Token (SyntaxKind.StaticKeyword), Token (SyntaxKind.PartialKeyword)],

// public new static partial
{ HasNewFlag: true, HasStaticFlag: true, HasInternalFlag: false }
=> [Token (SyntaxKind.PublicKeyword), Token (SyntaxKind.NewKeyword), Token (SyntaxKind.StaticKeyword), Token (SyntaxKind.PartialKeyword)],

// public new virtual partial
{ HasNewFlag: true, HasStaticFlag: false, HasAbstractFlag: false, HasInternalFlag: false }
=> [Token (SyntaxKind.PublicKeyword), Token (SyntaxKind.NewKeyword), Token (SyntaxKind.VirtualKeyword), Token (SyntaxKind.PartialKeyword)],

// internal new virtual partial
{ HasNewFlag: true, HasStaticFlag: false, HasAbstractFlag: false, HasInternalFlag: true }
=> [Token (SyntaxKind.InternalKeyword), Token (SyntaxKind.NewKeyword), Token (SyntaxKind.VirtualKeyword), Token (SyntaxKind.PartialKeyword)],

// public new abstract
{ HasNewFlag: true, HasAbstractFlag: true, HasInternalFlag: false}
=> [Token (SyntaxKind.PublicKeyword), Token (SyntaxKind.NewKeyword), Token (SyntaxKind.AbstractKeyword)],

// internal new abstract
{ HasNewFlag: true, HasAbstractFlag: true, HasInternalFlag: true}
=> [Token (SyntaxKind.InternalKeyword), Token (SyntaxKind.NewKeyword), Token (SyntaxKind.AbstractKeyword)],

// public override partial
{ HasNewFlag: false, HasOverrideFlag: true, HasInternalFlag: false}
=> [Token (SyntaxKind.PublicKeyword), Token (SyntaxKind.OverrideKeyword), Token (SyntaxKind.PartialKeyword)],

// internal override partial
{ HasNewFlag: false, HasOverrideFlag: true, HasInternalFlag: true}
=> [Token (SyntaxKind.InternalKeyword), Token (SyntaxKind.OverrideKeyword), Token (SyntaxKind.PartialKeyword)],

// public abstract
{ HasAbstractFlag: true, HasInternalFlag: false}
=> [Token (SyntaxKind.PublicKeyword), Token (SyntaxKind.AbstractKeyword)],

// internal abstract
{ HasAbstractFlag: true, HasInternalFlag: true}
=> [Token (SyntaxKind.InternalKeyword), Token (SyntaxKind.AbstractKeyword)],

// general case, but internal
{ HasInternalFlag: true} =>
[Token (SyntaxKind.InternalKeyword), Token (SyntaxKind.VirtualKeyword), Token (SyntaxKind.VirtualKeyword)],

// general case
_ => [Token (SyntaxKind.PublicKeyword), Token (SyntaxKind.VirtualKeyword), Token (SyntaxKind.PartialKeyword)]
};
#pragma warning restore format

// create a helper struct to retrieve the modifiers
var flags = new ModifiersFlags (HasAbstractFlag, HasInternalFlag, HasNewFlag, HasOverrideFlag, HasStaticFlag);
Modifiers = flags.ToModifiersArray ();
}

public static bool TryCreate (MethodDeclarationSyntax declaration, SemanticModel semanticModel,
Expand Down
109 changes: 109 additions & 0 deletions src/rgen/Microsoft.Macios.Transformer/DataModel/ModifiersFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace Microsoft.Macios.Generator.DataModel;

/// <summary>
/// Helper class to store the modifiers flags. It also provides a method that will
/// return the correct modifiers according to the flags;
/// </summary>
readonly record struct ModifiersFlags {

public bool HasAbstractFlag { get; }
public bool HasInternalFlag { get; }
public bool HasNewFlag { get; }
public bool HasOverrideFlag { get; }
public bool HasStaticFlag { get; }

/// <summary>
/// Create a new structure with the provided flags.
/// </summary>
/// <param name="hasAbstractFlag">The state of the abstract flag.</param>
/// <param name="hasInternalFlag">The state of the internal flag.</param>
/// <param name="hasNewFlag">The state of the new flag.</param>
/// <param name="hasOverrideFlag">The state of the override flag.</param>
/// <param name="hasStaticFlag">The state of the static flag.</param>
public ModifiersFlags (bool hasAbstractFlag, bool hasInternalFlag, bool hasNewFlag, bool hasOverrideFlag, bool hasStaticFlag)
{
HasAbstractFlag = hasAbstractFlag;
HasInternalFlag = hasInternalFlag;
HasNewFlag = hasNewFlag;
HasOverrideFlag = hasOverrideFlag;
HasStaticFlag = hasStaticFlag;
}

/// <summary>
/// Returns the list of modifiers to be used with the provided set of flags.
/// </summary>
/// <returns>The list of modifiers to use to write the transformed method/property.</returns>
public ImmutableArray<SyntaxToken> ToModifiersArray ()
{
#pragma warning disable format
// Modifiers are special because we might be dealing with several flags that the user has set in the method.
// We have to add the partial keyword so that we can have the partial implementation of the method later generated
// by the roslyn code generator
return this switch {
// internal static partial
{ HasNewFlag: false, HasStaticFlag: true, HasInternalFlag: true }
=> [Token (SyntaxKind.InternalKeyword), Token (SyntaxKind.StaticKeyword), Token (SyntaxKind.PartialKeyword)],

// public static partial
{ HasNewFlag: false, HasStaticFlag: true, HasInternalFlag: false }
=> [Token (SyntaxKind.PublicKeyword), Token (SyntaxKind.StaticKeyword), Token (SyntaxKind.PartialKeyword)],

// internal new static partial
{ HasNewFlag: true, HasStaticFlag: true, HasInternalFlag: true}
=> [Token (SyntaxKind.InternalKeyword), Token (SyntaxKind.NewKeyword), Token (SyntaxKind.StaticKeyword), Token (SyntaxKind.PartialKeyword)],

// public new static partial
{ HasNewFlag: true, HasStaticFlag: true, HasInternalFlag: false }
=> [Token (SyntaxKind.PublicKeyword), Token (SyntaxKind.NewKeyword), Token (SyntaxKind.StaticKeyword), Token (SyntaxKind.PartialKeyword)],

// public new virtual partial
{ HasNewFlag: true, HasStaticFlag: false, HasAbstractFlag: false, HasInternalFlag: false }
=> [Token (SyntaxKind.PublicKeyword), Token (SyntaxKind.NewKeyword), Token (SyntaxKind.VirtualKeyword), Token (SyntaxKind.PartialKeyword)],

// internal new virtual partial
{ HasNewFlag: true, HasStaticFlag: false, HasAbstractFlag: false, HasInternalFlag: true }
=> [Token (SyntaxKind.InternalKeyword), Token (SyntaxKind.NewKeyword), Token (SyntaxKind.VirtualKeyword), Token (SyntaxKind.PartialKeyword)],

// public new abstract
{ HasNewFlag: true, HasAbstractFlag: true, HasInternalFlag: false}
=> [Token (SyntaxKind.PublicKeyword), Token (SyntaxKind.NewKeyword), Token (SyntaxKind.AbstractKeyword)],

// internal new abstract
{ HasNewFlag: true, HasAbstractFlag: true, HasInternalFlag: true}
=> [Token (SyntaxKind.InternalKeyword), Token (SyntaxKind.NewKeyword), Token (SyntaxKind.AbstractKeyword)],

// public override partial
{ HasNewFlag: false, HasOverrideFlag: true, HasInternalFlag: false}
=> [Token (SyntaxKind.PublicKeyword), Token (SyntaxKind.OverrideKeyword), Token (SyntaxKind.PartialKeyword)],

// internal override partial
{ HasNewFlag: false, HasOverrideFlag: true, HasInternalFlag: true}
=> [Token (SyntaxKind.InternalKeyword), Token (SyntaxKind.OverrideKeyword), Token (SyntaxKind.PartialKeyword)],

// public abstract
{ HasAbstractFlag: true, HasInternalFlag: false}
=> [Token (SyntaxKind.PublicKeyword), Token (SyntaxKind.AbstractKeyword)],

// internal abstract
{ HasAbstractFlag: true, HasInternalFlag: true}
=> [Token (SyntaxKind.InternalKeyword), Token (SyntaxKind.AbstractKeyword)],

// general case, but internal
{ HasInternalFlag: true} =>
[Token (SyntaxKind.InternalKeyword), Token (SyntaxKind.VirtualKeyword), Token (SyntaxKind.VirtualKeyword)],

// general case
_ => [Token (SyntaxKind.PublicKeyword), Token (SyntaxKind.VirtualKeyword), Token (SyntaxKind.PartialKeyword)]
};
#pragma warning restore format
}

}
Loading

0 comments on commit 8826755

Please sign in to comment.