Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rgen] Share DataModel between the generator and the transformer. #22015

Merged
merged 2 commits into from
Jan 20, 2025
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
@@ -0,0 +1,75 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.Macios.Generator.Attributes;
using Microsoft.Macios.Generator.Availability;

namespace Microsoft.Macios.Generator.DataModel;

readonly partial struct Accessor {

/// <summary>
/// The data of the field attribute used to mark the value as a property binding.
/// </summary>
public ExportData<ObjCBindings.Property>? ExportPropertyData { get; init; }

public bool MarshalNativeExceptions
=> ExportPropertyData is not null && ExportPropertyData.Value.Flags.HasFlag (ObjCBindings.Property.MarshalNativeExceptions);

/// <summary>
/// Create a new code change in a property accessor.
/// </summary>
/// <param name="accessorKind">The kind of accessor.</param>
/// <param name="symbolAvailability">The os availability of the symbol.</param>
/// <param name="exportPropertyData">The data of the export attribute found in the accessor.</param>
/// <param name="attributes">The list of attributes attached to the accessor.</param>
/// <param name="modifiers">The list of visibility modifiers of the accessor.</param>
public Accessor (AccessorKind accessorKind,
SymbolAvailability symbolAvailability,
ExportData<ObjCBindings.Property>? exportPropertyData,
ImmutableArray<AttributeCodeChange> attributes,
ImmutableArray<SyntaxToken> modifiers)
{
Kind = accessorKind;
SymbolAvailability = symbolAvailability;
ExportPropertyData = exportPropertyData;
Attributes = attributes;
Modifiers = modifiers;
}

/// <summary>
/// Retrieve the selector to be used with the associated property.
/// </summary>
/// <param name="associatedProperty">The property associated with the accessor.</param>
/// <returns>The selector to use for the accessor.</returns>
public string? GetSelector (in Property associatedProperty)
{
// this is not a property but a field, we cannot retrieve a selector.
if (!associatedProperty.IsProperty)
return null;

// There are two possible cases, the current accessor has an export attribute, if that
// is the case, we will use the selector in that attribute. Otherwise, we have:
//
// * getter: return the property selector.
// * setter: use the registrar code (it has the right logic) to get the setter.
if (ExportPropertyData is null) {
return Kind == AccessorKind.Getter
? associatedProperty.ExportPropertyData.Value.Selector
: Registrar.Registrar.CreateSetterSelector (associatedProperty.ExportPropertyData.Value.Selector);
}

return ExportPropertyData.Value.Selector;
}

/// <summary>
/// Returns if the accessor should marshal native exceptions with the associated property.
/// </summary>
/// <param name="property">The property associated with the accessor.</param>
/// <returns>True if either the accessor or the property were marked with the MarshalNativeExceptions flag.</returns>
public bool ShouldMarshalNativeExceptions (in Property property)
=> MarshalNativeExceptions || property.MarshalNativeExceptions;

}
67 changes: 3 additions & 64 deletions src/rgen/Microsoft.Macios.Generator/DataModel/Accessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.Macios.Generator.Attributes;
using Microsoft.Macios.Generator.Availability;

namespace Microsoft.Macios.Generator.DataModel;

readonly struct Accessor : IEquatable<Accessor> {
[StructLayout (LayoutKind.Auto)]
readonly partial struct Accessor : IEquatable<Accessor> {
/// <summary>
/// The kind of accessor.
/// </summary>
Expand All @@ -21,14 +22,6 @@ namespace Microsoft.Macios.Generator.DataModel;
/// </summary>
public SymbolAvailability SymbolAvailability { get; }

/// <summary>
/// The data of the field attribute used to mark the value as a property binding.
/// </summary>
public ExportData<ObjCBindings.Property>? ExportPropertyData { get; init; }

public bool MarshalNativeExceptions
=> ExportPropertyData is not null && ExportPropertyData.Value.Flags.HasFlag (ObjCBindings.Property.MarshalNativeExceptions);

/// <summary>
/// List of attribute code changes of the accessor.
/// </summary>
Expand All @@ -39,60 +32,6 @@ public bool MarshalNativeExceptions
/// </summary>
public ImmutableArray<SyntaxToken> Modifiers { get; }

/// <summary>
/// Create a new code change in a property accessor.
/// </summary>
/// <param name="accessorKind">The kind of accessor.</param>
/// <param name="symbolAvailability">The os availability of the symbol.</param>
/// <param name="exportPropertyData">The data of the export attribute found in the accessor.</param>
/// <param name="attributes">The list of attributes attached to the accessor.</param>
/// <param name="modifiers">The list of visibility modifiers of the accessor.</param>
public Accessor (AccessorKind accessorKind,
SymbolAvailability symbolAvailability,
ExportData<ObjCBindings.Property>? exportPropertyData,
ImmutableArray<AttributeCodeChange> attributes,
ImmutableArray<SyntaxToken> modifiers)
{
Kind = accessorKind;
SymbolAvailability = symbolAvailability;
ExportPropertyData = exportPropertyData;
Attributes = attributes;
Modifiers = modifiers;
}

/// <summary>
/// Retrieve the selector to be used with the associated property.
/// </summary>
/// <param name="associatedProperty">The property associated with the accessor.</param>
/// <returns>The selector to use for the accessor.</returns>
public string? GetSelector (in Property associatedProperty)
{
// this is not a property but a field, we cannot retrieve a selector.
if (!associatedProperty.IsProperty)
return null;

// There are two possible cases, the current accessor has an export attribute, if that
// is the case, we will use the selector in that attribute. Otherwise, we have:
//
// * getter: return the property selector.
// * setter: use the registrar code (it has the right logic) to get the setter.
if (ExportPropertyData is null) {
return Kind == AccessorKind.Getter
? associatedProperty.ExportPropertyData.Value.Selector
: Registrar.Registrar.CreateSetterSelector (associatedProperty.ExportPropertyData.Value.Selector);
}

return ExportPropertyData.Value.Selector;
}

/// <summary>
/// Returns if the accessor should marshal native exceptions with the associated property.
/// </summary>
/// <param name="property">The property associated with the accessor.</param>
/// <returns>True if either the accessor or the property were marked with the MarshalNativeExceptions flag.</returns>
public bool ShouldMarshalNativeExceptions (in Property property)
=> MarshalNativeExceptions || property.MarshalNativeExceptions;

/// <inheritdoc />
public bool Equals (Accessor other)
{
Expand Down
Loading