From c540ffa61dcd3711426263f4d8c566d12a738568 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 30 Nov 2023 20:41:52 +0100 Subject: [PATCH 1/2] Add ISymbol.TryGetSyntaxNode extension --- .../Extensions/ISymbolExtensions.cs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs index a7c58ed1..612b5fb7 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs @@ -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; } + + /// + /// Tries to get a syntax node with a given type from an input symbol. + /// + /// The type of syntax node to look for. + /// The input instance to get the syntax node for. + /// The used to cancel the operation, if needed. + /// The resulting syntax node, if found. + /// Whether or not a syntax node of type was retrieved successfully. + public static bool TryGetSyntaxNode(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; + } } \ No newline at end of file From c9272392d13c054bf8d11b492893fd2e7ccc7e1e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 30 Nov 2023 20:42:06 +0100 Subject: [PATCH 2/2] Pass cancellation token to SyntaxReferece.GetSyntax() --- ...2DPixelShaderDescriptorGenerator.HlslSource.cs | 11 +++++++---- ...ComputeShaderDescriptorGenerator.HlslSource.cs | 15 +++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs index dc3838d8..e6c8dc8b 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs @@ -87,6 +87,7 @@ partial class D2DPixelShaderDescriptorGenerator structDeclarationSymbol, discoveredTypes, constantDefinitions, + token, out bool fieldsNeedD2D1RequiresScenePosition); token.ThrowIfCancellationRequested(); @@ -234,6 +235,7 @@ partial class D2DPixelShaderDescriptorGenerator /// The type symbol for the shader type. /// The collection of currently discovered types. /// The collection of discovered constant definitions. + /// The used to cancel the operation, if needed. /// Whether or not the shader needs the [D2DRequiresScenePosition] annotation. /// A sequence of static constant fields in . private static ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> GetStaticFields( @@ -242,6 +244,7 @@ partial class D2DPixelShaderDescriptorGenerator INamedTypeSymbol structDeclarationSymbol, ICollection discoveredTypes, IDictionary 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; } diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs index 1c346c43..0ee84697 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs @@ -102,7 +102,8 @@ partial class ComputeShaderDescriptorGenerator semanticModelProvider, structDeclarationSymbol, discoveredTypes, - constantDefinitions); + constantDefinitions, + token); token.ThrowIfCancellationRequested(); @@ -247,25 +248,27 @@ partial class ComputeShaderDescriptorGenerator /// The type symbol for the shader type. /// The collection of currently discovered types. /// The collection of discovered constant definitions. + /// The used to cancel the operation, if needed. /// A sequence of static constant fields in . private static ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> GetStaticFields( ImmutableArrayBuilder diagnostics, SemanticModelProvider semanticModel, INamedTypeSymbol structDeclarationSymbol, ICollection discoveredTypes, - IDictionary constantDefinitions) + IDictionary 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; }