diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs index 8372f9a7..d070d38e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs @@ -284,13 +284,20 @@ partial class D2DPixelShaderDescriptorGenerator false => $"static {HlslKnownTypes.GetMappedName(typeSymbol)}" }; + token.ThrowIfCancellationRequested(); + StaticFieldRewriter staticFieldRewriter = new( semanticModel, discoveredTypes, constantDefinitions, - diagnostics); + diagnostics, + token); - string? assignment = staticFieldRewriter.Visit(variableDeclarator)?.NormalizeWhitespace(eol: "\n").ToFullString(); + ExpressionSyntax? processedDeclaration = staticFieldRewriter.Visit(variableDeclarator); + + token.ThrowIfCancellationRequested(); + + string? assignment = processedDeclaration?.NormalizeWhitespace(eol: "\n").ToFullString(); needsD2D1RequiresScenePosition |= staticFieldRewriter.NeedsD2DRequiresScenePositionAttribute; @@ -368,6 +375,7 @@ partial class D2DPixelShaderDescriptorGenerator constructors, constantDefinitions, diagnostics, + token, isShaderEntryPoint); // Rewrite the method syntax tree diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs index 84dfae49..320805ef 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Threading; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; @@ -33,16 +34,19 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter /// The set of discovered custom types. /// The collection of discovered constant definitions. /// The collection of produced instances. + /// The value for the current operation. protected HlslSourceRewriter( SemanticModelProvider semanticModel, ICollection discoveredTypes, IDictionary constantDefinitions, - ImmutableArrayBuilder diagnostics) + ImmutableArrayBuilder diagnostics, + CancellationToken token) { SemanticModel = semanticModel; DiscoveredTypes = discoveredTypes; ConstantDefinitions = constantDefinitions; Diagnostics = diagnostics; + CancellationToken = token; } /// @@ -65,6 +69,11 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter /// protected ImmutableArrayBuilder Diagnostics { get; } + /// + /// Gets the value for the current operation. + /// + protected CancellationToken CancellationToken { get; } + /// public sealed override SyntaxNode VisitCastExpression(CastExpressionSyntax node) { @@ -118,6 +127,8 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter /// The rewritten for the object creation expression. private SyntaxNode VisitObjectCreationExpression(BaseObjectCreationExpressionSyntax node, BaseObjectCreationExpressionSyntax updatedNode, TypeSyntax targetType) { + CancellationToken.ThrowIfCancellationRequested(); + ITypeSymbol? typeSymbol = SemanticModel.For(node).GetTypeInfo(node).Type; // Handle the edge case of the type being null (shouldn't really happen) @@ -192,6 +203,8 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter /// public sealed override SyntaxNode? VisitLiteralExpression(LiteralExpressionSyntax node) { + CancellationToken.ThrowIfCancellationRequested(); + LiteralExpressionSyntax updatedNode = (LiteralExpressionSyntax)base.VisitLiteralExpression(node)!; if (updatedNode.IsKind(SyntaxKind.DefaultLiteralExpression)) @@ -250,6 +263,8 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter /// public sealed override SyntaxNode? VisitElementAccessExpression(ElementAccessExpressionSyntax node) { + CancellationToken.ThrowIfCancellationRequested(); + ElementAccessExpressionSyntax updatedNode = (ElementAccessExpressionSyntax)base.VisitElementAccessExpression(node)!; if (SemanticModel.For(node).GetOperation(node) is IPropertyReferenceOperation operation) @@ -313,6 +328,8 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter /// public sealed override SyntaxNode? VisitAssignmentExpression(AssignmentExpressionSyntax node) { + CancellationToken.ThrowIfCancellationRequested(); + AssignmentExpressionSyntax updatedNode = (AssignmentExpressionSyntax)base.VisitAssignmentExpression(node)!; if (SemanticModel.For(node).GetOperation(node) is ICompoundAssignmentOperation { OperatorMethod: { ContainingType.ContainingNamespace.Name: "ComputeSharp" } method }) @@ -340,6 +357,8 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter /// public sealed override SyntaxNode? VisitBinaryExpression(BinaryExpressionSyntax node) { + CancellationToken.ThrowIfCancellationRequested(); + BinaryExpressionSyntax updatedNode = (BinaryExpressionSyntax)base.VisitBinaryExpression(node)!; // Process binary operations to see if the target operator method is an intrinsic diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index b5afaf6f..dbc9b541 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Threading; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; @@ -77,6 +78,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter /// The collection of discovered constructors for custom struct types. /// The collection of discovered constant definitions. /// The collection of produced instances. + /// The value for the current operation. /// Whether or not the current instance is processing a shader entry point. public ShaderSourceRewriter( INamedTypeSymbol shaderType, @@ -87,8 +89,9 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter IDictionary constructors, IDictionary constantDefinitions, ImmutableArrayBuilder diagnostics, + CancellationToken token, bool isEntryPoint) - : base(semanticModel, discoveredTypes, constantDefinitions, diagnostics) + : base(semanticModel, discoveredTypes, constantDefinitions, diagnostics, token) { this.shaderType = shaderType; this.staticMethods = staticMethods; @@ -109,6 +112,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter /// The collection of discovered constructors for custom struct types. /// The collection of discovered constant definitions. /// The collection of produced instances. + /// The value for the current operation. private ShaderSourceRewriter( SemanticModelProvider semanticModel, ICollection discoveredTypes, @@ -116,8 +120,9 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter IDictionary instanceMethods, IDictionary constructors, IDictionary constantDefinitions, - ImmutableArrayBuilder diagnostics) - : base(semanticModel, discoveredTypes, constantDefinitions, diagnostics) + ImmutableArrayBuilder diagnostics, + CancellationToken token) + : base(semanticModel, discoveredTypes, constantDefinitions, diagnostics, token) { this.staticMethods = staticMethods; this.instanceMethods = instanceMethods; @@ -308,6 +313,8 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter /// public override SyntaxNode? VisitLocalFunctionStatement(LocalFunctionStatementSyntax node) { + CancellationToken.ThrowIfCancellationRequested(); + // If the current identifier matches the one for the current method, it means the local function // statement is the one being inspected, and it's nto coming from a local function in a method // being parsed. That is, this was a blobal method that was annotated in source. @@ -369,6 +376,8 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter /// public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node) { + CancellationToken.ThrowIfCancellationRequested(); + MemberAccessExpressionSyntax updatedNode = (MemberAccessExpressionSyntax)base.VisitMemberAccessExpression(node)!; if (node.IsKind(SyntaxKind.SimpleMemberAccessExpression) && @@ -477,6 +486,8 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter /// public override SyntaxNode? VisitInvocationExpression(InvocationExpressionSyntax node) { + CancellationToken.ThrowIfCancellationRequested(); + InvocationExpressionSyntax updatedNode = (InvocationExpressionSyntax)base.VisitInvocationExpression(node)!; if (SemanticModel.For(node).GetOperation(node) is IInvocationOperation { TargetMethod: IMethodSymbol method } operation) @@ -520,7 +531,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter if (!this.staticMethods.ContainsKey(method)) { - if (method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not MethodDeclarationSyntax methodNode) + if (!method.TryGetSyntaxNode(CancellationToken, out MethodDeclarationSyntax? methodNode)) { Diagnostics.Add(InvalidMethodOrConstructorCall, node, method); @@ -534,7 +545,8 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter this.instanceMethods, this.constructors, ConstantDefinitions, - Diagnostics); + Diagnostics, + CancellationToken); MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.WithoutTrivia(); @@ -574,7 +586,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter // Same as for static methods, ensure the method is available in source, and process it if so if (!this.instanceMethods.ContainsKey(method)) { - if (method.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not MethodDeclarationSyntax methodNode) + if (!method.TryGetSyntaxNode(CancellationToken, out MethodDeclarationSyntax? methodNode)) { Diagnostics.Add(InvalidMethodOrConstructorCall, node, method); @@ -588,7 +600,8 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter this.instanceMethods, this.constructors, ConstantDefinitions, - Diagnostics); + Diagnostics, + CancellationToken); MethodDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(methodNode)!.WithoutTrivia(); @@ -660,7 +673,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter // (even if IsImplicitlyDeclared is false) and a primary constructor. This is deliberately not supported, as there // are very subtle ways in which captures can interact with fields and methods, and we cannot guarantee to track and // rewrite all of them correctly to exactly preserve the same semantics. So we just block such cases entirely as well. - if (constructor.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not ConstructorDeclarationSyntax constructorNode) + if (!constructor.TryGetSyntaxNode(CancellationToken, out ConstructorDeclarationSyntax? constructorNode)) { Diagnostics.Add(InvalidMethodOrConstructorCall, node, constructor); @@ -682,7 +695,8 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter this.instanceMethods, this.constructors, ConstantDefinitions, - Diagnostics); + Diagnostics, + CancellationToken); ConstructorDeclarationSyntax processedMethod = shaderSourceRewriter.Visit(constructorNode)!.WithoutTrivia(); diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs index 2ce06ff4..9981bf5d 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Threading; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; @@ -23,11 +24,14 @@ namespace ComputeSharp.SourceGeneration.SyntaxRewriters; /// The set of discovered custom types. /// The collection of discovered constant definitions. /// The collection of produced instances. +/// The value for the current operation. internal sealed partial class StaticFieldRewriter( SemanticModelProvider semanticModel, ICollection discoveredTypes, IDictionary constantDefinitions, - ImmutableArrayBuilder diagnostics) : HlslSourceRewriter(semanticModel, discoveredTypes, constantDefinitions, diagnostics) + ImmutableArrayBuilder diagnostics, + CancellationToken token) + : HlslSourceRewriter(semanticModel, discoveredTypes, constantDefinitions, diagnostics, token) { /// public ExpressionSyntax? Visit(VariableDeclaratorSyntax? node) @@ -43,6 +47,8 @@ internal sealed partial class StaticFieldRewriter( /// public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node) { + CancellationToken.ThrowIfCancellationRequested(); + MemberAccessExpressionSyntax updatedNode = (MemberAccessExpressionSyntax)base.VisitMemberAccessExpression(node)!; if (node.IsKind(SyntaxKind.SimpleMemberAccessExpression) && diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs index f749251b..c2e3c8d9 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs @@ -298,13 +298,20 @@ partial class ComputeShaderDescriptorGenerator false => $"static {HlslKnownTypes.GetMappedName(typeSymbol)}" }; + token.ThrowIfCancellationRequested(); + StaticFieldRewriter staticFieldRewriter = new( semanticModel, discoveredTypes, constantDefinitions, - diagnostics); + diagnostics, + token); - string? assignment = staticFieldRewriter.Visit(variableDeclarator)?.NormalizeWhitespace(eol: "\n").ToFullString(); + ExpressionSyntax? processedDeclaration = staticFieldRewriter.Visit(variableDeclarator); + + token.ThrowIfCancellationRequested(); + + string? assignment = processedDeclaration?.NormalizeWhitespace(eol: "\n").ToFullString(); builder.Add((mapping ?? fieldSymbol.Name, typeDeclaration, assignment)); } @@ -427,6 +434,7 @@ partial class ComputeShaderDescriptorGenerator constructors, constantDefinitions, diagnostics, + token, isShaderEntryPoint); // Rewrite the method syntax tree diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidComputeContextCopyAnalyzer.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidComputeContextCopyAnalyzer.cs index 392f8d74..9bfad686 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidComputeContextCopyAnalyzer.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/Analyzers/InvalidComputeContextCopyAnalyzer.cs @@ -239,7 +239,7 @@ public sealed class InvalidComputeContextCopyAnalyzer : DiagnosticAnalyzer return; } - context.ReportDiagnostic(Diagnostic.Create(InvalidComputeContextCopy, symbol.DeclaringSyntaxReferences[0].GetSyntax().GetLocation())); + context.ReportDiagnostic(Diagnostic.Create(InvalidComputeContextCopy, symbol.DeclaringSyntaxReferences[0].GetSyntax(context.CancellationToken).GetLocation())); } }, SymbolKind.Field); });