Merge pull request #696 from Sergio0694/dev/get-syntax-token

Flow cancellation token to SyntaxReference.GetSyntax() calls
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-11-30 21:02:19 +01:00
コミット d80d3710da
3ファイル変更
+44 -10

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

+7 -4
ファイルの表示
@@ -87,6 +87,7 @@ partial class D2DPixelShaderDescriptorGenerator
structDeclarationSymbol,
discoveredTypes,
constantDefinitions,
token,
out bool fieldsNeedD2D1RequiresScenePosition);
token.ThrowIfCancellationRequested();
@@ -234,6 +235,7 @@ partial class D2DPixelShaderDescriptorGenerator
/// <param name="structDeclarationSymbol">The type symbol for the shader type.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <param name="token">The <see cref="CancellationToken"/> used to cancel the operation, if needed.</param>
/// <param name="needsD2D1RequiresScenePosition">Whether or not the shader needs the <c>[D2DRequiresScenePosition]</c> annotation.</param>
/// <returns>A sequence of static constant fields in <paramref name="structDeclarationSymbol"/>.</returns>
private static ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> GetStaticFields(
@@ -242,6 +244,7 @@ partial class D2DPixelShaderDescriptorGenerator
INamedTypeSymbol structDeclarationSymbol,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IFieldSymbol, string> constantDefinitions,
CancellationToken token,
out bool needsD2D1RequiresScenePosition)
{
using ImmutableArrayBuilder<(string, string, string?)> builder = new();
@@ -251,12 +254,12 @@ partial class D2DPixelShaderDescriptorGenerator
foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers())
{
// Find all declared static fields in the type
if (memberSymbol is not IFieldSymbol { IsImplicitlyDeclared: false, IsStatic: true, IsConst: false, DeclaringSyntaxReferences: [SyntaxReference fieldReference, ..] } fieldSymbol)
if (memberSymbol is not IFieldSymbol { IsImplicitlyDeclared: false, IsStatic: true, IsConst: false, } fieldSymbol)
{
continue;
}
if (fieldReference.GetSyntax() is not VariableDeclaratorSyntax variableDeclarator)
if (!fieldSymbol.TryGetSyntaxNode(token, out VariableDeclaratorSyntax? variableDeclarator))
{
continue;
}
@@ -326,12 +329,12 @@ partial class D2DPixelShaderDescriptorGenerator
foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers())
{
// Find all declared methods in the type
if (memberSymbol is not IMethodSymbol { IsImplicitlyDeclared: false, DeclaringSyntaxReferences: [SyntaxReference methodReference, ..] } methodSymbol)
if (memberSymbol is not IMethodSymbol { IsImplicitlyDeclared: false, } methodSymbol)
{
continue;
}
if (methodReference.GetSyntax() is not MethodDeclarationSyntax methodDeclaration)
if (!methodSymbol.TryGetSyntaxNode(token, out MethodDeclarationSyntax? methodDeclaration))
{
continue;
}
+28
ファイルの表示
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.SourceGeneration.Extensions;
@@ -117,4 +118,31 @@ internal static class ISymbolExtensions
return false;
}
/// <summary>
/// Tries to get a syntax node with a given type from an input symbol.
/// </summary>
/// <typeparam name="T">The type of syntax node to look for.</typeparam>
/// <param name="symbol">The input <see cref="ISymbol"/> instance to get the syntax node for.</param>
/// <param name="token">The <see cref="CancellationToken"/> used to cancel the operation, if needed.</param>
/// <param name="syntaxNode">The resulting <typeparamref name="T"/> syntax node, if found.</param>
/// <returns>Whether or not a syntax node of type <typeparamref name="T"/> was retrieved successfully.</returns>
public static bool TryGetSyntaxNode<T>(this ISymbol symbol, CancellationToken token, [NotNullWhen(true)] out T? syntaxNode)
where T : SyntaxNode
{
// If there are no syntax references, there is nothing to do
if (symbol.DeclaringSyntaxReferences is not [SyntaxReference syntaxReference, ..])
{
syntaxNode = null;
return false;
}
// Get the target node, and check that it's of the desired type
T? candidateNode = syntaxReference.GetSyntax(token) as T;
syntaxNode = candidateNode;
return candidateNode is not null;
}
}
+9 -6
ファイルの表示
@@ -102,7 +102,8 @@ partial class ComputeShaderDescriptorGenerator
semanticModelProvider,
structDeclarationSymbol,
discoveredTypes,
constantDefinitions);
constantDefinitions,
token);
token.ThrowIfCancellationRequested();
@@ -247,25 +248,27 @@ partial class ComputeShaderDescriptorGenerator
/// <param name="structDeclarationSymbol">The type symbol for the shader type.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <param name="token">The <see cref="CancellationToken"/> used to cancel the operation, if needed.</param>
/// <returns>A sequence of static constant fields in <paramref name="structDeclarationSymbol"/>.</returns>
private static ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> GetStaticFields(
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
SemanticModelProvider semanticModel,
INamedTypeSymbol structDeclarationSymbol,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IFieldSymbol, string> constantDefinitions)
IDictionary<IFieldSymbol, string> constantDefinitions,
CancellationToken token)
{
using ImmutableArrayBuilder<(string, string, string?)> builder = new();
foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers())
{
// Find all declared static fields in the type
if (memberSymbol is not IFieldSymbol { IsImplicitlyDeclared: false, IsStatic: true, IsConst: false, DeclaringSyntaxReferences: [SyntaxReference fieldReference, ..] } fieldSymbol)
if (memberSymbol is not IFieldSymbol { IsImplicitlyDeclared: false, IsStatic: true, IsConst: false, } fieldSymbol)
{
continue;
}
if (fieldReference.GetSyntax() is not VariableDeclaratorSyntax variableDeclarator)
if (!fieldSymbol.TryGetSyntaxNode(token, out VariableDeclaratorSyntax? variableDeclarator))
{
continue;
}
@@ -379,12 +382,12 @@ partial class ComputeShaderDescriptorGenerator
foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers())
{
// Find all declared methods in the type
if (memberSymbol is not IMethodSymbol { IsImplicitlyDeclared: false, DeclaringSyntaxReferences: [SyntaxReference methodReference, ..] } methodSymbol)
if (memberSymbol is not IMethodSymbol { IsImplicitlyDeclared: false, } methodSymbol)
{
continue;
}
if (methodReference.GetSyntax() is not MethodDeclarationSyntax methodDeclaration)
if (!methodSymbol.TryGetSyntaxNode(token, out MethodDeclarationSyntax? methodDeclaration))
{
continue;
}