diff --git a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.Execute.cs b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.Execute.cs index 43b92c31..d298b1b5 100644 --- a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.Execute.cs +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.Execute.cs @@ -1,5 +1,7 @@ using System.Threading; +using ComputeSharp.D2D1.WinUI.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Helpers; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -69,5 +71,98 @@ partial class CanvasEffectPropertyGenerator return false; } + + public static void WritePropertyDeclarations(EquatableArray properties, IndentedTextWriter writer) + { + // First, generate all partial property implementations at the top of the partial type declaration + foreach (CanvasEffectPropertyInfo propertyInfo in properties) + { + writer.WriteLine(skipIfPresent: true); + writer.WriteLine("/// "); + writer.WriteGeneratedAttributes(GeneratorName); + writer.WriteLine($$""" + public partial {{propertyInfo.TypeNameWithNullabilityAnnotations}} {{propertyInfo.PropertyName}} + { + get => field; + set + { + if (global::System.Collections.Generic.EqualityComparer<{{propertyInfo.TypeNameWithNullabilityAnnotations}}>.Default.Equals(field, value)) + { + return; + } + + int oldValue = field; + + On{{propertyInfo.PropertyName}}Changing(value); + On{{propertyInfo.PropertyName}}Changing(oldValue, value); + + field = value; + + On{{propertyInfo.PropertyName}}Changed(value); + On{{propertyInfo.PropertyName}}Changed(oldValue, value); + + InvalidateEffectGraph(default); + } + } + """, isMultiline: true); + } + + foreach (CanvasEffectPropertyInfo propertyInfo in properties) + { + // OnChanging, only with new value + writer.WriteLine(); + writer.WriteLine($""" + /// Executes the logic for when is changing. + /// The new property value being set. + /// This method is invoked right before the value of is changed. + """, isMultiline: true); + 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 + }; + + // OnChanging, with both values + writer.WriteLine(); + writer.WriteLine($""" + /// Executes the logic for when is changing. + /// The previous property value that is being replaced. + /// The new property value being set. + /// This method is invoked right before the value of is changed. + """, isMultiline: true); + writer.WriteGeneratedAttributes(GeneratorName, includeNonUserCodeAttributes: false); + writer.WriteLine($"partial void On{propertyInfo.PropertyName}Changing({oldValueTypeNameAsNullable} oldValue, {propertyInfo.TypeNameWithNullabilityAnnotations} newValue);"); + + // OnChanged, only with new value + writer.WriteLine(); + writer.WriteLine($""" + /// Executes the logic for when has just changed. + /// The new property value that has been set. + /// This method is invoked right after the value of is changed. + """, isMultiline: true); + writer.WriteGeneratedAttributes(GeneratorName, includeNonUserCodeAttributes: false); + writer.WriteLine($"partial void On{propertyInfo.PropertyName}Changed({propertyInfo.TypeNameWithNullabilityAnnotations} newValue);"); + + // OnChanged, with both values + writer.WriteLine(); + writer.WriteLine($""" + /// Executes the logic for when has just changed. + /// The previous property value that has been replaced. + /// The new property value that has been set. + /// This method is invoked right after the value of is changed. + """, isMultiline: true); + writer.WriteGeneratedAttributes(GeneratorName, includeNonUserCodeAttributes: false); + writer.WriteLine($"partial void On{propertyInfo.PropertyName}Changed({oldValueTypeNameAsNullable} oldValue, {propertyInfo.TypeNameWithNullabilityAnnotations} newValue);"); + } + } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs index 02471ca7..8332fcaf 100644 --- a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs @@ -1,5 +1,6 @@ using ComputeSharp.D2D1.WinUI.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; @@ -41,6 +42,11 @@ public sealed partial class CanvasEffectPropertyGenerator : IIncrementalGenerato string typeNameWithNullabilityAnnotations = propertySymbol.Type.GetFullyQualifiedNameWithNullabilityAnnotations(); bool isOldPropertyValueDirectlyReferenced = Execute.IsOldPropertyValueDirectlyReferenced(propertySymbol); + // We're using IsValueType here and not IsReferenceType to also cover unconstrained type parameter cases. + // This will cover both reference types as well T when the constraints are not struct or unmanaged. + // If this is true, it means the field storage can potentially be in a null state (even if not annotated). + bool isReferenceTypeOrUnconstraindTypeParameter = !propertySymbol.Type.IsValueType; + // Finally, get the hierarchy too HierarchyInfo hierarchyInfo = HierarchyInfo.From(typeSymbol); @@ -50,8 +56,28 @@ public sealed partial class CanvasEffectPropertyGenerator : IIncrementalGenerato Hierarchy: hierarchyInfo, PropertyName: propertySymbol.Name, TypeNameWithNullabilityAnnotations: typeNameWithNullabilityAnnotations, - IsOldPropertyValueDirectlyReferenced: isOldPropertyValueDirectlyReferenced); + IsOldPropertyValueDirectlyReferenced: isOldPropertyValueDirectlyReferenced, + IsReferenceTypeOrUnconstraindTypeParameter: isReferenceTypeOrUnconstraindTypeParameter); }) .Where(static item => item is not null)!; + + // Split and group by containing type + IncrementalValuesProvider<(HierarchyInfo Hierarchy, EquatableArray Properties)> groupedPropertyInfo = + propertyInfo + .GroupBy(keySelector: static item => item.Hierarchy, elementSelector: static item => item); + + // Generate the source files, if any + context.RegisterSourceOutput(groupedPropertyInfo, static (context, item) => + { + using IndentedTextWriter writer = new(); + + item.Hierarchy.WriteSyntax( + state: item.Properties, + writer: writer, + baseTypes: [], + memberCallbacks: [Execute.WritePropertyDeclarations]); + + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.g.cs", writer.ToString()); + }); } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Models/CanvasEffectPropertyInfo.cs b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Models/CanvasEffectPropertyInfo.cs index 2653c9ad..98ab84ae 100644 --- a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Models/CanvasEffectPropertyInfo.cs +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/Models/CanvasEffectPropertyInfo.cs @@ -9,8 +9,10 @@ namespace ComputeSharp.D2D1.WinUI.SourceGenerators.Models; /// The type name for the generated property, including nullability annotations. /// The generated property name. /// Whether the old property value is being directly referenced. +/// Indicates whether the property is of a reference type or an unconstrained type parameter. internal sealed record CanvasEffectPropertyInfo( HierarchyInfo Hierarchy, string PropertyName, string TypeNameWithNullabilityAnnotations, - bool IsOldPropertyValueDirectlyReferenced); + bool IsOldPropertyValueDirectlyReferenced, + bool IsReferenceTypeOrUnconstraindTypeParameter);