From 63bf3d98e797d2e73a6565c308e43b723d9146b8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 3 Jun 2024 01:13:01 +0200 Subject: [PATCH] Initial prototype to gather property infos --- .../CanvasEffectPropertyGenerator.Execute.cs | 31 ++++++++++++++++ .../CanvasEffectPropertyGenerator.cs | 35 +++++++++++++++++-- .../Extensions/ISymbolExtensions.cs | 10 ++++++ .../Extensions/ITypeSymbolExtensions.cs | 23 ++++++++++++ 4 files changed, 97 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.Execute.cs b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.Execute.cs index c056ab63..43b92c31 100644 --- a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.Execute.cs +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.Execute.cs @@ -38,5 +38,36 @@ partial class CanvasEffectPropertyGenerator // The property must be in a type with a base type (as it must derive from CanvasEffect) return node.Parent?.IsTypeDeclarationWithOrPotentiallyWithBaseTypes() == true; } + + /// + /// Checks whether the generated code has to directly reference the old property value. + /// + /// The input instance to process. + /// Whether the generated code needs direct access to the old property value. + public static bool IsOldPropertyValueDirectlyReferenced(IPropertySymbol propertySymbol) + { + // Check OnChanging( oldValue, newValue) first + foreach (ISymbol symbol in propertySymbol.ContainingType.GetMembers($"On{propertySymbol.Name}Changing")) + { + // No need to be too specific as we're not expecting false positives (which also wouldn't really + // cause any problems anyway, just produce slightly worse codegen). Just checking the number of + // parameters is good enough, and keeps the code very simple and cheap to run. + if (symbol is IMethodSymbol { Parameters.Length: 2 }) + { + return true; + } + } + + // Do the same for OnChanged( oldValue, newValue) + foreach (ISymbol symbol in propertySymbol.ContainingType.GetMembers($"On{propertySymbol.Name}Changed")) + { + if (symbol is IMethodSymbol { Parameters.Length: 2 }) + { + return true; + } + } + + return false; + } } } \ 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 281cb62e..02471ca7 100644 --- a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/CanvasEffectPropertyGenerator.cs @@ -1,3 +1,6 @@ +using ComputeSharp.D2D1.WinUI.SourceGenerators.Models; +using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; namespace ComputeSharp.D2D1.WinUI.SourceGenerators; @@ -16,11 +19,39 @@ public sealed partial class CanvasEffectPropertyGenerator : IIncrementalGenerato /// public void Initialize(IncrementalGeneratorInitializationContext context) { - IncrementalValuesProvider propertyInfo = + IncrementalValuesProvider propertyInfo = context.SyntaxProvider .ForAttributeWithMetadataName( "ComputeSharp.D2D1.WinUI.GeneratedCanvasEffectPropertyAttribute", Execute.IsCandidatePropertyDeclaration, - static (context, token) => (object?)null); + static (context, token) => + { + // Ensure we do have a property with a valid containing type + if (context.TargetSymbol is not IPropertySymbol { ContainingType: INamedTypeSymbol typeSymbol } propertySymbol) + { + return default; + } + + // Ensure that the containing type derives from CanvasEffect + if (!typeSymbol.InheritsFromFullyQualifiedMetadataName("ComputeSharp.D2D1.WinUI.CanvasEffect")) + { + return default; + } + + string typeNameWithNullabilityAnnotations = propertySymbol.Type.GetFullyQualifiedNameWithNullabilityAnnotations(); + bool isOldPropertyValueDirectlyReferenced = Execute.IsOldPropertyValueDirectlyReferenced(propertySymbol); + + // Finally, get the hierarchy too + HierarchyInfo hierarchyInfo = HierarchyInfo.From(typeSymbol); + + token.ThrowIfCancellationRequested(); + + return new CanvasEffectPropertyInfo( + Hierarchy: hierarchyInfo, + PropertyName: propertySymbol.Name, + TypeNameWithNullabilityAnnotations: typeNameWithNullabilityAnnotations, + IsOldPropertyValueDirectlyReferenced: isOldPropertyValueDirectlyReferenced); + }) + .Where(static item => item is not null)!; } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs index 612b5fb7..c52abfae 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs @@ -59,6 +59,16 @@ internal static class ISymbolExtensions : symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat); } + /// + /// Gets the fully qualified name for a given symbol, including nullability annotations + /// + /// The input instance. + /// The fully qualified name for . + public static string GetFullyQualifiedNameWithNullabilityAnnotations(this ISymbol symbol) + { + return symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat.AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier)); + } + /// /// Checks whether or not a given symbol has an attribute with the specified type. /// diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ITypeSymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ITypeSymbolExtensions.cs index 440a2503..5cb39bd5 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ITypeSymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ITypeSymbolExtensions.cs @@ -44,6 +44,29 @@ internal static class ITypeSymbolExtensions return builder.WrittenSpan.SequenceEqual(name.AsSpan()); } + /// + /// Checks whether or not a given inherits from a specified type. + /// + /// The target instance to check. + /// The full name of the type to check for inheritance. + /// Whether or not inherits from . + public static bool InheritsFromFullyQualifiedMetadataName(this ITypeSymbol typeSymbol, string name) + { + INamedTypeSymbol? baseType = typeSymbol.BaseType; + + while (baseType is not null) + { + if (baseType.HasFullyQualifiedMetadataName(name)) + { + return true; + } + + baseType = baseType.BaseType; + } + + return false; + } + /// /// Checks whether or not a given implements an interface of a specified type. ///