diff --git a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.Execute.cs b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.Execute.cs index 878468cf..6afb2900 100644 --- a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.Execute.cs +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.Execute.cs @@ -41,6 +41,60 @@ partial class CanvasEffectPropertyGenerator return node.Parent?.IsTypeDeclarationWithOrPotentiallyWithBaseTypes() == true; } + /// + /// Tries to get the accessibility of the property and accessors, if possible. + /// + /// The input node. + /// The input instance. + /// The accessibility of the property, if available. + /// The accessibility of the accessor, if available. + /// The accessibility of the accessor, if available. + /// Whether the property was valid and the accessibilities could be retrieved. + public static bool TryGetAccessibilityModifiers( + PropertyDeclarationSyntax node, + IPropertySymbol symbol, + out Accessibility declaredAccessibility, + out Accessibility getterAccessibility, + out Accessibility setterAccessibility) + { + declaredAccessibility = Accessibility.NotApplicable; + getterAccessibility = Accessibility.NotApplicable; + setterAccessibility = Accessibility.NotApplicable; + + // Ensure that we have a getter and a setter, and that the setter is not init-only + if (symbol is not { GetMethod: { } getMethod, SetMethod: { IsInitOnly: false } setMethod }) + { + return false; + } + + // Track the property accessibility if explicitly set + if (node.Modifiers.Count > 0) + { + declaredAccessibility = symbol.DeclaredAccessibility; + } + + // Track the accessors accessibility, if explicitly set + foreach (AccessorDeclarationSyntax accessor in node.AccessorList?.Accessors ?? []) + { + if (accessor.Modifiers.Count == 0) + { + continue; + } + + switch (accessor.Kind()) + { + case SyntaxKind.GetAccessorDeclaration: + getterAccessibility = getMethod.DeclaredAccessibility; + break; + case SyntaxKind.SetAccessorDeclaration: + setterAccessibility = setMethod.DeclaredAccessibility; + break; + } + } + + return true; + } + /// /// Gets the invalidation type to use for the generated effect property. /// @@ -60,24 +114,51 @@ partial class CanvasEffectPropertyGenerator public static void WritePropertyDeclarations(EquatableArray properties, IndentedTextWriter writer) { + // Helper to get the nullable type name for the initial property value + static string GetOldValueTypeNameAsNullable(CanvasEffectPropertyInfo propertyInfo) + { + // Prepare the nullable type for the previous property value. This is needed because if the type is a reference + // type, the previous value might be null even if the property type is not nullable, as the first invocation would + // happen when the property is first set to some value that is not null (but the backing field would still be so). + // As a cheap way to check whether we need to add nullable, we can simply check whether the type name with nullability + // annotations ends with a '?'. If it doesn't and the type is a reference type, we add it. Otherwise, we keep it. + return propertyInfo.IsReferenceTypeOrUnconstraindTypeParameter switch + { + true when !propertyInfo.TypeNameWithNullabilityAnnotations.EndsWith("?") + => $"{propertyInfo.TypeNameWithNullabilityAnnotations}?", + _ => propertyInfo.TypeNameWithNullabilityAnnotations + }; + } + + // Helper to get the accessibility with a trailing space + static string GetExpressionWithTrailingSpace(Accessibility accessibility) + { + return accessibility.GetExpression() switch + { + { Length: > 0 } expression => expression + " ", + _ => "" + }; + } + // First, generate all partial property implementations at the top of the partial type declaration foreach (CanvasEffectPropertyInfo propertyInfo in properties) { - writer.WriteLine(skipIfPresent: true); + string oldValueTypeNameAsNullable = GetOldValueTypeNameAsNullable(propertyInfo); + writer.WriteLine("/// "); writer.WriteGeneratedAttributes(GeneratorName); writer.WriteLine($$""" - public partial {{propertyInfo.TypeNameWithNullabilityAnnotations}} {{propertyInfo.PropertyName}} + {{GetExpressionWithTrailingSpace(propertyInfo.DeclaredAccessibility)}}partial {{propertyInfo.TypeNameWithNullabilityAnnotations}} {{propertyInfo.PropertyName}} { - get => field; - set + {{GetExpressionWithTrailingSpace(propertyInfo.GetterAccessibility)}}get => field; + {{GetExpressionWithTrailingSpace(propertyInfo.SetterAccessibility)}}set { - if (global::System.Collections.Generic.EqualityComparer<{{propertyInfo.TypeNameWithNullabilityAnnotations}}>.Default.Equals(field, value)) + if (global::System.Collections.Generic.EqualityComparer<{{oldValueTypeNameAsNullable}}>.Default.Equals(field, value)) { return; } - int oldValue = field; + {{oldValueTypeNameAsNullable}} oldValue = field; On{{propertyInfo.PropertyName}}Changing(value); On{{propertyInfo.PropertyName}}Changing(oldValue, value); @@ -91,12 +172,14 @@ partial class CanvasEffectPropertyGenerator } } """, isMultiline: true); + writer.WriteLine(); } + // Next, emit all partial method declarations at the bottom of the file foreach (CanvasEffectPropertyInfo propertyInfo in properties) { // OnChanging, only with new value - writer.WriteLine(); + writer.WriteLine(skipIfPresent: true); writer.WriteLine($""" /// Executes the logic for when is changing. /// The new property value being set. @@ -105,17 +188,7 @@ partial class CanvasEffectPropertyGenerator writer.WriteGeneratedAttributes(GeneratorName, includeNonUserCodeAttributes: false); writer.WriteLine($"partial void On{propertyInfo.PropertyName}Changing({propertyInfo.TypeNameWithNullabilityAnnotations} newValue);"); - // Prepare the nullable type for the previous property value. This is needed because if the type is a reference - // type, the previous value might be null even if the property type is not nullable, as the first invocation would - // happen when the property is first set to some value that is not null (but the backing field would still be so). - // As a cheap way to check whether we need to add nullable, we can simply check whether the type name with nullability - // annotations ends with a '?'. If it doesn't and the type is a reference type, we add it. Otherwise, we keep it. - string oldValueTypeNameAsNullable = propertyInfo.IsReferenceTypeOrUnconstraindTypeParameter switch - { - true when !propertyInfo.TypeNameWithNullabilityAnnotations.EndsWith("?") - => $"{propertyInfo.TypeNameWithNullabilityAnnotations}?", - _ => propertyInfo.TypeNameWithNullabilityAnnotations - }; + string oldValueTypeNameAsNullable = GetOldValueTypeNameAsNullable(propertyInfo); // OnChanging, with both values writer.WriteLine(); diff --git a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs index 2ebcfa07..90cb20b3 100644 --- a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs @@ -3,6 +3,7 @@ using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; namespace ComputeSharp.D2D1.WinUI.SourceGenerators; @@ -41,6 +42,19 @@ public sealed partial class CanvasEffectPropertyGenerator : IIncrementalGenerato token.ThrowIfCancellationRequested(); + // Get the accessibility values, if the property is valid + if (!Execute.TryGetAccessibilityModifiers( + node: (PropertyDeclarationSyntax)context.TargetNode, + symbol: propertySymbol, + out Accessibility declaredAccessibility, + out Accessibility getterAccessibility, + out Accessibility setterAccessibility)) + { + return default; + } + + token.ThrowIfCancellationRequested(); + string typeNameWithNullabilityAnnotations = propertySymbol.Type.GetFullyQualifiedNameWithNullabilityAnnotations(); token.ThrowIfCancellationRequested(); @@ -62,6 +76,9 @@ public sealed partial class CanvasEffectPropertyGenerator : IIncrementalGenerato return new CanvasEffectPropertyInfo( Hierarchy: hierarchyInfo, PropertyName: propertySymbol.Name, + DeclaredAccessibility: declaredAccessibility, + GetterAccessibility: getterAccessibility, + SetterAccessibility: setterAccessibility, TypeNameWithNullabilityAnnotations: typeNameWithNullabilityAnnotations, IsReferenceTypeOrUnconstraindTypeParameter: isReferenceTypeOrUnconstraindTypeParameter, InvalidationType: invalidationType); diff --git a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Models/CanvasEffectPropertyInfo.cs b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Models/CanvasEffectPropertyInfo.cs index e4aed430..a49fb448 100644 --- a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Models/CanvasEffectPropertyInfo.cs +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Models/CanvasEffectPropertyInfo.cs @@ -1,4 +1,5 @@ using ComputeSharp.SourceGeneration.Models; +using Microsoft.CodeAnalysis; namespace ComputeSharp.D2D1.WinUI.SourceGenerators.Models; @@ -6,13 +7,19 @@ namespace ComputeSharp.D2D1.WinUI.SourceGenerators.Models; /// A model representing a generated canvas effect property. /// /// The hierarchy info for the shader type. -/// The type name for the generated property, including nullability annotations. /// The generated property name. +/// The accessibility of the property, if available. +/// The accessibility of the accessor, if available. +/// The accessibility of the accessor, if available. +/// The type name for the generated property, including nullability annotations. /// Indicates whether the property is of a reference type or an unconstrained type parameter. /// The invalidation type to request. internal sealed record CanvasEffectPropertyInfo( HierarchyInfo Hierarchy, string PropertyName, + Accessibility DeclaredAccessibility, + Accessibility GetterAccessibility, + Accessibility SetterAccessibility, string TypeNameWithNullabilityAnnotations, bool IsReferenceTypeOrUnconstraindTypeParameter, CanvasEffectInvalidationType InvalidationType);