Initial prototype to gather property infos

このコミットが含まれているのは:
Sergio Pedri committed 2024-10-17 21:41:03 -07:00
1 親 616766bc80
コミット 63bf3d98e7
4ファイル変更
+97 -2

検索条件に一致するファイルはありません

@@ -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<ClassDeclarationSyntax>() == true;
}
/// <summary>
/// Checks whether the generated code has to directly reference the old property value.
/// </summary>
/// <param name="propertySymbol">The input <see cref="IPropertySymbol"/> instance to process.</param>
/// <returns>Whether the generated code needs direct access to the old property value.</returns>
public static bool IsOldPropertyValueDirectlyReferenced(IPropertySymbol propertySymbol)
{
// Check On<PROPERTY_NAME>Changing(<PROPERTY_TYPE> oldValue, <PROPERTY_TYPE> 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 On<PROPERTY_NAME>Changed(<PROPERTY_TYPE> oldValue, <PROPERTY_TYPE> newValue)
foreach (ISymbol symbol in propertySymbol.ContainingType.GetMembers($"On{propertySymbol.Name}Changed"))
{
if (symbol is IMethodSymbol { Parameters.Length: 2 })
{
return true;
}
}
return false;
}
}
}
+33 -2
ファイルの表示
@@ -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
/// <inheritdoc/>
public void Initialize(IncrementalGeneratorInitializationContext context)
{
IncrementalValuesProvider<object?> propertyInfo =
IncrementalValuesProvider<CanvasEffectPropertyInfo> 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)!;
}
}
+10
ファイルの表示
@@ -59,6 +59,16 @@ internal static class ISymbolExtensions
: symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat);
}
/// <summary>
/// Gets the fully qualified name for a given symbol, including nullability annotations
/// </summary>
/// <param name="symbol">The input <see cref="ISymbol"/> instance.</param>
/// <returns>The fully qualified name for <paramref name="symbol"/>.</returns>
public static string GetFullyQualifiedNameWithNullabilityAnnotations(this ISymbol symbol)
{
return symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat.AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier));
}
/// <summary>
/// Checks whether or not a given symbol has an attribute with the specified type.
/// </summary>
+23
ファイルの表示
@@ -44,6 +44,29 @@ internal static class ITypeSymbolExtensions
return builder.WrittenSpan.SequenceEqual(name.AsSpan());
}
/// <summary>
/// Checks whether or not a given <see cref="ITypeSymbol"/> inherits from a specified type.
/// </summary>
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
/// <param name="name">The full name of the type to check for inheritance.</param>
/// <returns>Whether or not <paramref name="typeSymbol"/> inherits from <paramref name="name"/>.</returns>
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;
}
/// <summary>
/// Checks whether or not a given <see cref="ITypeSymbol"/> implements an interface of a specified type.
/// </summary>