diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs index decf1272..e475010c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs @@ -26,10 +26,10 @@ partial class D2DPixelShaderSourceGenerator /// /// Validates that the return type of the annotated method is valid and returns the type name. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input instance to process. /// The HLSL source to compile, if present. - public static string? GetInvalidReturnType(ImmutableArray.Builder diagnostics, IMethodSymbol methodSymbol) + public static string? GetInvalidReturnType(ImmutableArray.Builder diagnostics, IMethodSymbol methodSymbol) { if (!(methodSymbol.ReturnType is INamedTypeSymbol { @@ -55,10 +55,10 @@ partial class D2DPixelShaderSourceGenerator /// /// Extracts the HLSL source from a method with the annotation. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input instance to process. /// The HLSL source to compile, if present. - public static string GetHlslSource(ImmutableArray.Builder diagnostics, IMethodSymbol methodSymbol) + public static string GetHlslSource(ImmutableArray.Builder diagnostics, IMethodSymbol methodSymbol) { _ = methodSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute", out AttributeData? attributeData); @@ -77,10 +77,10 @@ partial class D2DPixelShaderSourceGenerator /// /// Extracts the shader profile for a target method, if present. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input instance to process. /// The shader profile to use to compile the shader, if present. - public static D2D1ShaderProfile GetShaderProfile(ImmutableArray.Builder diagnostics, IMethodSymbol methodSymbol) + public static D2D1ShaderProfile GetShaderProfile(ImmutableArray.Builder diagnostics, IMethodSymbol methodSymbol) { if (methodSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute", out AttributeData? attributeData)) { @@ -104,10 +104,10 @@ partial class D2DPixelShaderSourceGenerator /// /// Extracts the compile options for the current shader. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input instance to process. /// The compile options to use to compile the shader, if present. - public static D2D1CompileOptions GetCompileOptions(ImmutableArray.Builder diagnostics, IMethodSymbol methodSymbol) + public static D2D1CompileOptions GetCompileOptions(ImmutableArray.Builder diagnostics, IMethodSymbol methodSymbol) { if (methodSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData)) { @@ -135,7 +135,7 @@ partial class D2DPixelShaderSourceGenerator /// The used to cancel the operation, if needed. /// The resulting diagnostic from the processing operation, if any. /// The instance with the compiled shader bytecode. - public static unsafe ImmutableArray GetBytecode(HlslShaderMethodSourceInfo sourceInfo, CancellationToken token, out DiagnosticInfo? diagnostic) + public static unsafe ImmutableArray GetBytecode(HlslShaderMethodSourceInfo sourceInfo, CancellationToken token, out DeferredDiagnosticInfo? diagnostic) { ImmutableArray bytecode = ImmutableArray.Empty; @@ -169,7 +169,7 @@ partial class D2DPixelShaderSourceGenerator } catch (Win32Exception e) { - diagnostic = new DiagnosticInfo( + diagnostic = DeferredDiagnosticInfo.Create( D2DPixelShaderSourceCompilationFailedWithWin32Exception, sourceInfo.MethodName, e.HResult, @@ -177,7 +177,7 @@ partial class D2DPixelShaderSourceGenerator } catch (FxcCompilationException e) { - diagnostic = new DiagnosticInfo( + diagnostic = DeferredDiagnosticInfo.Create( D2DPixelShaderSourceCompilationFailedWithFxcCompilationException, sourceInfo.MethodName, ID2D1ShaderGenerator.LoadBytecode.FixupExceptionMessage(e.Message)); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs index 8216e002..431050d1 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using ComputeSharp.D2D1.SourceGenerators.Models; @@ -31,7 +32,7 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato MethodDeclarationSyntax methodDeclaration = (MethodDeclarationSyntax)context.TargetNode; IMethodSymbol methodSymbol = (IMethodSymbol)context.TargetSymbol; - ImmutableArray.Builder diagnostics = ImmutableArray.CreateBuilder(); + ImmutableArray.Builder diagnostics = ImmutableArray.CreateBuilder(); // Get the processed HLSL first and check for cancellation string hlslSource = Execute.GetHlslSource(diagnostics, methodSymbol); @@ -59,17 +60,20 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato }); // Output the diagnostics - context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, _) => item.Diagnostcs)); + context.ReportDiagnostics( + shaderInfoWithErrors + .Select(static (item, _) => item.Diagnostcs) + .WithComparer(EqualityComparer.Default.ForImmutableArray())); // Compile the requested shader bytecodes - IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeMethodInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeMethodInfo BytecodeInfo, DeferredDiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors = shaderInfoWithErrors .Select(static (item, token) => { ImmutableArray bytecode = Execute.GetBytecode( item.HlslShaderMethodSource, token, - out DiagnosticInfo? diagnostic); + out DeferredDiagnosticInfo? diagnostic); token.ThrowIfCancellationRequested(); @@ -84,7 +88,7 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato }); // Gather the diagnostics - IncrementalValuesProvider embeddedBytecodeDiagnostics = + IncrementalValuesProvider embeddedBytecodeDiagnostics = embeddedBytecodeWithErrors .Select(static (item, token) => (item.Hierarchy.FullyQualifiedMetadataName, item.Diagnostic)) .Where(static item => item.Diagnostic is not null) @@ -92,11 +96,8 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato .Select(static (item, token) => { INamedTypeSymbol typeSymbol = item.Right.GetTypeByMetadataName(item.Left.FullyQualifiedMetadataName)!; - - return Diagnostic.Create( - item.Left.Diagnostic!.Descriptor, - typeSymbol.Locations.FirstOrDefault(), - new object[] { typeSymbol }.Concat(item.Left.Diagnostic.Args).ToArray()); + + return DiagnosticInfo.Create(item.Left.Diagnostic!.Descriptor, typeSymbol, new object[] { typeSymbol }.Concat(item.Left.Diagnostic.Arguments).ToArray()); }); // Output the diagnostics diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DResourceTextureIndexUseAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DResourceTextureIndexUseAnalyzer.cs index f8cb979e..711ff539 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DResourceTextureIndexUseAnalyzer.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidD2DResourceTextureIndexUseAnalyzer.cs @@ -39,7 +39,7 @@ public sealed class InvalidD2DResourceTextureIndexUseAnalyzer : DiagnosticAnalyz { Diagnostic diagnostic = Diagnostic.Create( InvalidD2DResourceTextureIndexAttributeUse, - fieldSymbol.Locations.FirstOrDefault(), + fieldSymbol.Locations.First(), fieldSymbol.Name, fieldSymbol.ContainingType, fieldSymbol.Type); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs index 537ff7f4..0cef3803 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -6,6 +6,7 @@ using System.Text; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; +using ComputeSharp.SourceGeneration.Models; using ComputeSharp.SourceGeneration.SyntaxRewriters; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; @@ -28,7 +29,7 @@ partial class ID2D1ShaderGenerator /// /// Gathers all necessary information on a transpiled HLSL source for a given shader type. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input object currently in use. /// The node to process. /// The for . @@ -37,7 +38,7 @@ partial class ID2D1ShaderGenerator /// The indicess of the complex shader inputs. /// The HLSL source for the shader. public static string GetHlslSource( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, Compilation compilation, StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, @@ -94,13 +95,13 @@ partial class ID2D1ShaderGenerator /// /// Gets a sequence of captured fields and their mapped names. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input instance to process. /// The collection of currently discovered types. /// The sequence of captured fields in . /// The sequence of captured resource textures in . private static void GetInstanceFields( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol, ICollection types, out ImmutableArray<(string Name, string HlslType)> valueFields, @@ -182,7 +183,7 @@ partial class ID2D1ShaderGenerator /// /// Gets a sequence of shader static fields and their mapped names. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The instance for the type to process. /// The instance for the current type. /// The type symbol for the shader type. @@ -191,7 +192,7 @@ partial class ID2D1ShaderGenerator /// 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( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, SemanticModelProvider semanticModel, StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, @@ -251,7 +252,7 @@ partial class ID2D1ShaderGenerator /// /// Gets a sequence of processed methods declared within a given type. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The type symbol for the shader type. /// The instance for the current type. /// The instance for the type to process. @@ -261,7 +262,7 @@ partial class ID2D1ShaderGenerator /// Whether or not the shader needs the [D2DRequiresScenePosition] annotation. /// A sequence of processed methods in , and the entry point. private static (string EntryPoint, ImmutableArray<(string Signature, string Definition)> Methods) GetProcessedMethods( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, SemanticModelProvider semanticModel, @@ -370,12 +371,12 @@ partial class ID2D1ShaderGenerator /// /// Gets the sequence of processed discovered custom types. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The type symbol for the shader type. /// The sequence of discovered custom types. /// A sequence of custom type definitions to add to the shader source. private static ImmutableArray<(string Name, string Definition)> GetDeclaredTypes( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol, IEnumerable types) { @@ -434,9 +435,9 @@ partial class ID2D1ShaderGenerator /// /// Finds and reports all invalid declared properties in a shader. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input instance to process. - private static void DetectAndReportInvalidPropertyDeclarations(ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol) + private static void DetectAndReportInvalidPropertyDeclarations(ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol) { foreach (var memberSymbol in structDeclarationSymbol.GetMembers()) { @@ -457,12 +458,12 @@ partial class ID2D1ShaderGenerator /// /// Reports diagnostics for invalid uses of [D2DRequiresScenePosition] in a shader. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input instance to process. /// Whether the shader type is declaring the need for scene position. /// Whether the shader is using APIs that rely on scene position. private static void ReportInvalidD2DRequiresScenePositionUse( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol, bool requiresScenePosition, bool usesPositionDependentMethods) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs index 255b23bb..d9583b49 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateGetInputTypeMethod.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; @@ -19,14 +20,14 @@ partial class ID2D1ShaderGenerator /// /// Extracts the input info for the current shader. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input instance to process. /// The number of shader inputs to declare. /// The indicess of the simple shader inputs. /// The indices of the complex shader inputs. /// The combined and serialized input types for each available input. public static void GetInfo( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol, out int inputCount, out ImmutableArray inputSimpleIndices, diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs index 8b7be511..2661e394 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs @@ -48,10 +48,10 @@ partial class ID2D1ShaderGenerator /// /// Extracts the compile options for the current shader. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input instance to process. /// The compile options to use to compile the shader, if present. - public static D2D1CompileOptions? GetCompileOptions(ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol) + public static D2D1CompileOptions? GetCompileOptions(ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol) { if (structDeclarationSymbol.TryGetAttributeWithFullMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData)) { @@ -123,7 +123,7 @@ partial class ID2D1ShaderGenerator HlslShaderSourceInfo sourceInfo, CancellationToken token, out D2D1CompileOptions options, - out DiagnosticInfo? diagnostic) + out DeferredDiagnosticInfo? diagnostic) { ImmutableArray bytecode = ImmutableArray.Empty; @@ -165,12 +165,12 @@ partial class ID2D1ShaderGenerator catch (Win32Exception e) { options = default; - diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, FixupExceptionMessage(e.Message)); + diagnostic = DeferredDiagnosticInfo.Create(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, FixupExceptionMessage(e.Message)); } catch (FxcCompilationException e) { options = default; - diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithFxcCompilationException, FixupExceptionMessage(e.Message)); + diagnostic = DeferredDiagnosticInfo.Create(EmbeddedBytecodeFailedWithFxcCompilationException, FixupExceptionMessage(e.Message)); } End: diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs index a99339bc..25b20138 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.cs @@ -7,6 +7,7 @@ using ComputeSharp.Core.Helpers; using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Mappings; +using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; @@ -23,12 +24,12 @@ partial class ID2D1ShaderGenerator /// /// Explores a given type hierarchy and generates statements to load fields. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The current shader type being explored. /// The size of the shader constant buffer. /// The sequence of instances for all captured resources and values. public static ImmutableArray GetInfo( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, ITypeSymbol structDeclarationSymbol, out int constantBufferSizeInBytes) { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadInputDescriptionsMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadInputDescriptionsMethod.cs index 1bfbf0c5..ca9a12e4 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadInputDescriptionsMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadInputDescriptionsMethod.cs @@ -4,6 +4,7 @@ using System.Collections.Immutable; using System.Linq; using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; @@ -20,11 +21,11 @@ partial class ID2D1ShaderGenerator /// /// Extracts the input descriptions for the current shader. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input instance to process. /// The produced input descriptions for the shader. public static void GetInfo( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol, out ImmutableArray inputDescriptions) { diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs index fb4b0ee4..3a128e56 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadResourceTextureDescriptionsMethod.cs @@ -4,6 +4,7 @@ using System.Linq; using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Mappings; +using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; @@ -20,12 +21,12 @@ partial class ID2D1ShaderGenerator /// /// Extracts the resource texture descriptions for the current shader. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input instance to process. /// The number of inputs for the shader. /// The produced resource texture descriptions for the shader. public static void GetInfo( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol, int inputCount, out ImmutableArray resourceTextureDescriptions) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index a3350ad5..7d89a486 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Text; @@ -41,7 +42,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator return default; } - ImmutableArray.Builder diagnostics = ImmutableArray.CreateBuilder(); + ImmutableArray.Builder diagnostics = ImmutableArray.CreateBuilder(); // LoadDispatchData() info ImmutableArray fieldInfos = LoadDispatchData.GetInfo( @@ -130,7 +131,10 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator compilation.HasAccessibleTypeWithMetadataName("System.Runtime.CompilerServices.SkipLocalsInitAttribute")); // Output the diagnostics - context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, _) => item.Diagnostcs)); + context.ReportDiagnostics( + shaderInfoWithErrors + .Select(static (item, _) => item.Diagnostcs) + .WithComparer(EqualityComparer.Default.ForImmutableArray())); // Get the GetInputCount() info (hierarchy and input count) IncrementalValuesProvider<(HierarchyInfo Hierarchy, int InputCount)> inputCountInfo = @@ -217,7 +221,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator .Select(static (item, _) => (item.Hierarchy, item.HlslShaderSource)); // Compile the requested shader bytecodes - IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo, DeferredDiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors = shaderBytecodeInfo .Select(static (item, token) => { @@ -225,7 +229,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator item.Source, token, out D2D1CompileOptions options, - out DiagnosticInfo? diagnostic); + out DeferredDiagnosticInfo? diagnostic); token.ThrowIfCancellationRequested(); @@ -239,7 +243,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator }); // Gather the diagnostics - IncrementalValuesProvider embeddedBytecodeDiagnostics = + IncrementalValuesProvider embeddedBytecodeDiagnostics = embeddedBytecodeWithErrors .Select(static (item, _) => (item.Hierarchy.FullyQualifiedMetadataName, item.Diagnostic)) .Where(static item => item.Diagnostic is not null) @@ -247,11 +251,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator .Select(static (item, _) => { INamedTypeSymbol typeSymbol = item.Right.GetTypeByMetadataName(item.Left.FullyQualifiedMetadataName)!; - - return Diagnostic.Create( - item.Left.Diagnostic!.Descriptor, - typeSymbol.Locations.FirstOrDefault(), - new object[] { typeSymbol }.Concat(item.Left.Diagnostic.Args).ToArray()); + + return DiagnosticInfo.Create(item.Left.Diagnostic!.Descriptor, typeSymbol, new object[] { typeSymbol }.Concat(item.Left.Diagnostic.Arguments).ToArray()); }); // Output the diagnostics diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1PixelShaderSourceInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1PixelShaderSourceInfo.cs index 41e1011c..2a3e64dc 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1PixelShaderSourceInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1PixelShaderSourceInfo.cs @@ -4,7 +4,6 @@ using System.Collections.Immutable; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; -using Microsoft.CodeAnalysis; namespace ComputeSharp.D2D1.SourceGenerators.Models; @@ -17,7 +16,7 @@ namespace ComputeSharp.D2D1.SourceGenerators.Models; internal sealed record D2D1PixelShaderSourceInfo( HierarchyInfo Hierarchy, HlslShaderMethodSourceInfo HlslShaderMethodSource, - ImmutableArray Diagnostcs) + ImmutableArray Diagnostcs) { /// public bool Equals(D2D1PixelShaderSourceInfo? obj) => Comparer.Default.Equals(this, obj); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs index ed1692f0..643889ba 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/D2D1ShaderInfo.cs @@ -4,7 +4,6 @@ using System.Collections.Immutable; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; -using Microsoft.CodeAnalysis; namespace ComputeSharp.D2D1.SourceGenerators.Models; @@ -29,7 +28,7 @@ internal sealed record D2D1ShaderInfo( OutputBufferInfo OutputBuffer, InputDescriptionsInfo InputDescriptions, D2D1PixelOptions PixelOptions, - ImmutableArray Diagnostcs) + ImmutableArray Diagnostcs) { /// public bool Equals(D2D1ShaderInfo? obj) => Comparer.Default.Equals(this, obj); diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs index bda83b72..be59ad0a 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs @@ -6,6 +6,7 @@ using System.Linq; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; +using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -42,9 +43,9 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter protected readonly IDictionary ConstantDefinitions; /// - /// The collection of produced instances. + /// The collection of produced instances. /// - protected readonly ImmutableArray.Builder Diagnostics; + protected readonly ImmutableArray.Builder Diagnostics; /// /// Creates a new instance with the specified parameters. @@ -52,12 +53,12 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter /// The instance for the target syntax tree. /// The set of discovered custom types. /// The collection of discovered constant definitions. - /// The collection of produced instances. + /// The collection of produced instances. protected HlslSourceRewriter( SemanticModelProvider semanticModel, ICollection discoveredTypes, IDictionary constantDefinitions, - ImmutableArray.Builder diagnostics) + ImmutableArray.Builder diagnostics) { SemanticModel = semanticModel; DiscoveredTypes = discoveredTypes; diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index 6a960102..c32f24ef 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -6,6 +6,7 @@ using System.Linq; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; +using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -63,7 +64,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter /// The set of discovered custom types. /// The set of discovered and processed static methods. /// The collection of discovered constant definitions. - /// The collection of produced instances. + /// The collection of produced instances. /// Whether or not the current instance is processing a shader entry point. public ShaderSourceRewriter( INamedTypeSymbol shaderType, @@ -71,7 +72,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter ICollection discoveredTypes, IDictionary staticMethods, IDictionary constantDefinitions, - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, bool isEntryPoint) : base(semanticModel, discoveredTypes, constantDefinitions, diagnostics) { @@ -89,13 +90,13 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter /// The set of discovered custom types. /// The set of discovered and processed static methods. /// The collection of discovered constant definitions. - /// The collection of produced instances. + /// The collection of produced instances. public ShaderSourceRewriter( SemanticModelProvider semanticModel, ICollection discoveredTypes, IDictionary staticMethods, IDictionary constantDefinitions, - ImmutableArray.Builder diagnostics) + ImmutableArray.Builder diagnostics) : base(semanticModel, discoveredTypes, constantDefinitions, diagnostics) { this.staticMethods = staticMethods; diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs index 9bef84d5..653d08d5 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs @@ -6,6 +6,7 @@ using System.Linq; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; +using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -25,12 +26,12 @@ internal sealed partial class StaticFieldRewriter : HlslSourceRewriter /// The instance for the target syntax tree. /// The set of discovered custom types. /// The collection of discovered constant definitions. - /// The collection of produced instances. + /// The collection of produced instances. public StaticFieldRewriter( SemanticModelProvider semanticModel, ICollection discoveredTypes, IDictionary constantDefinitions, - ImmutableArray.Builder diagnostics) + ImmutableArray.Builder diagnostics) : base(semanticModel, discoveredTypes, constantDefinitions, diagnostics) { } diff --git a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems index 9b0bfa64..754e9d60 100644 --- a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems +++ b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems @@ -19,6 +19,7 @@ + diff --git a/src/ComputeSharp.SourceGeneration/Extensions/DiagnosticsExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/DiagnosticsExtensions.cs index 69745a4e..01923f37 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/DiagnosticsExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/DiagnosticsExtensions.cs @@ -1,5 +1,5 @@ using System.Collections.Immutable; -using System.Linq; +using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; namespace ComputeSharp.SourceGeneration.Extensions; @@ -12,33 +12,33 @@ internal static class DiagnosticsExtensions /// /// Adds a new diagnostics to the target builder. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input for the diagnostics to create. /// The source to attach the diagnostics to. /// The optional arguments for the formatted message to include. public static void Add( - this ImmutableArray.Builder diagnostics, + this ImmutableArray.Builder diagnostics, DiagnosticDescriptor descriptor, ISymbol symbol, params object[] args) { - diagnostics.Add(Diagnostic.Create(descriptor, symbol.Locations.FirstOrDefault(), args)); + diagnostics.Add(DiagnosticInfo.Create(descriptor, symbol, args)); } /// /// Adds a new diagnostics to the target builder. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input for the diagnostics to create. /// The source to attach the diagnostics to. /// The optional arguments for the formatted message to include. public static void Add( - this ImmutableArray.Builder diagnostics, + this ImmutableArray.Builder diagnostics, DiagnosticDescriptor descriptor, SyntaxNode node, params object[] args) { - diagnostics.Add(Diagnostic.Create(descriptor, node.GetLocation(), args)); + diagnostics.Add(DiagnosticInfo.Create(descriptor, node, args)); } /// @@ -46,11 +46,11 @@ internal static class DiagnosticsExtensions /// /// The input instance. /// The input sequence of diagnostics. - public static void ReportDiagnostics(this IncrementalGeneratorInitializationContext context, IncrementalValuesProvider diagnostics) + public static void ReportDiagnostics(this IncrementalGeneratorInitializationContext context, IncrementalValuesProvider diagnostics) { context.RegisterSourceOutput(diagnostics, static (context, diagnostic) => { - context.ReportDiagnostic(diagnostic); + context.ReportDiagnostic(diagnostic.ToDiagnostic()); }); } @@ -59,13 +59,13 @@ internal static class DiagnosticsExtensions /// /// The input instance. /// The input sequence of diagnostics. - public static void ReportDiagnostics(this IncrementalGeneratorInitializationContext context, IncrementalValuesProvider> diagnostics) + public static void ReportDiagnostics(this IncrementalGeneratorInitializationContext context, IncrementalValuesProvider> diagnostics) { context.RegisterSourceOutput(diagnostics, static (context, diagnostics) => { - foreach (Diagnostic diagnostic in diagnostics) + foreach (DiagnosticInfo diagnostic in diagnostics) { - context.ReportDiagnostic(diagnostic); + context.ReportDiagnostic(diagnostic.ToDiagnostic()); } }); } diff --git a/src/ComputeSharp.SourceGeneration/Extensions/IEqualityComparerExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/IEqualityComparerExtensions.cs index 0c7a9aa7..e76d9a93 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/IEqualityComparerExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/IEqualityComparerExtensions.cs @@ -22,7 +22,7 @@ internal static class IEqualityComparerExtensions /// /// An implementation for an value. /// - public sealed class Comparer : IEqualityComparer> + private sealed class Comparer : IEqualityComparer> { /// /// The comparer. diff --git a/src/ComputeSharp.SourceGeneration/Models/DeferredDiagnosticInfo.cs b/src/ComputeSharp.SourceGeneration/Models/DeferredDiagnosticInfo.cs new file mode 100644 index 00000000..32034299 --- /dev/null +++ b/src/ComputeSharp.SourceGeneration/Models/DeferredDiagnosticInfo.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Helpers; +using Microsoft.CodeAnalysis; + +namespace ComputeSharp.SourceGeneration.Models; + +/// +/// A model for a serializeable diagnostic info to be reconstructed later in a pipeline. +/// +/// The wrapped instance. +/// The diagnostic arguments. +internal sealed record DeferredDiagnosticInfo(DiagnosticDescriptor Descriptor, ImmutableArray Arguments) +{ + /// + public bool Equals(DeferredDiagnosticInfo? obj) => Comparer.Default.Equals(this, obj); + + /// + public override int GetHashCode() => Comparer.Default.GetHashCode(this); + + /// + /// Creates a new instance with the specified parameters. + /// + /// The input for the diagnostics to create. + /// The optional arguments for the formatted message to include. + /// A new instance with the specified parameters. + public static DeferredDiagnosticInfo Create(DiagnosticDescriptor descriptor, params object[] args) + { + return new(descriptor, args.Select(static arg => arg.ToString()).ToImmutableArray()); + } + + /// + /// An implementation for . + /// + private sealed class Comparer : Comparer + { + /// + protected override void AddToHashCode(ref HashCode hashCode, DeferredDiagnosticInfo obj) + { + hashCode.Add(obj.Descriptor); + hashCode.AddRange(obj.Arguments); + } + + /// + protected override bool AreEqual(DeferredDiagnosticInfo x, DeferredDiagnosticInfo y) + { + return + x.Descriptor.Equals(y.Descriptor) && + x.Arguments.SequenceEqual(y.Arguments); + } + } +} diff --git a/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs b/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs index 98649b75..6eb80f9a 100644 --- a/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs +++ b/src/ComputeSharp.SourceGeneration/Models/DiagnosticInfo.cs @@ -1,17 +1,26 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; +using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Text; namespace ComputeSharp.SourceGeneration.Models; /// -/// A model for a serializeable diagnostic provider. +/// A model for a serializeable diagnostic info. /// /// The wrapped instance. -/// The diagnostic args. -internal sealed record DiagnosticInfo(DiagnosticDescriptor Descriptor, params object[] Args) +/// The tree to use as location for the diagnostic, if available. +/// The span to use as location for the diagnostic. +/// The diagnostic arguments. +internal sealed record DiagnosticInfo( + DiagnosticDescriptor Descriptor, + SyntaxTree? SyntaxTree, + TextSpan TextSpan, + ImmutableArray Arguments) { /// public bool Equals(DiagnosticInfo? obj) => Comparer.Default.Equals(this, obj); @@ -19,6 +28,48 @@ internal sealed record DiagnosticInfo(DiagnosticDescriptor Descriptor, params ob /// public override int GetHashCode() => Comparer.Default.GetHashCode(this); + /// + /// Creates a new instance with the state from this model. + /// + /// A new instance with the state from this model. + public Diagnostic ToDiagnostic() + { + if (SyntaxTree is not null) + { + return Diagnostic.Create(Descriptor, Location.Create(SyntaxTree, TextSpan), Arguments.ToArray()); + } + + return Diagnostic.Create(Descriptor, null, Arguments.ToArray()); + } + + /// + /// Creates a new instance with the specified parameters. + /// + /// The input for the diagnostics to create. + /// The source to attach the diagnostics to. + /// The optional arguments for the formatted message to include. + /// A new instance with the specified parameters. + public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, ISymbol symbol, params object[] args) + { + Location location = symbol.Locations.First(); + + return new(descriptor, location.SourceTree, location.SourceSpan, args.Select(static arg => arg.ToString()).ToImmutableArray()); + } + + /// + /// Creates a new instance with the specified parameters. + /// + /// The input for the diagnostics to create. + /// The source to attach the diagnostics to. + /// The optional arguments for the formatted message to include. + /// A new instance with the specified parameters. + public static DiagnosticInfo Create(DiagnosticDescriptor descriptor, SyntaxNode node, params object[] args) + { + Location location = node.GetLocation(); + + return new(descriptor, location.SourceTree, location.SourceSpan, args.Select(static arg => arg.ToString()).ToImmutableArray()); + } + /// /// An implementation for . /// @@ -28,11 +79,9 @@ internal sealed record DiagnosticInfo(DiagnosticDescriptor Descriptor, params ob protected override void AddToHashCode(ref HashCode hashCode, DiagnosticInfo obj) { hashCode.Add(obj.Descriptor); - - foreach (object arg in obj.Args) - { - hashCode.Add(arg); - } + hashCode.Add(obj.SyntaxTree); + hashCode.Add(obj.TextSpan); + hashCode.AddRange(obj.Arguments); } /// @@ -40,7 +89,9 @@ internal sealed record DiagnosticInfo(DiagnosticDescriptor Descriptor, params ob { return x.Descriptor.Equals(y.Descriptor) && - x.Args.SequenceEqual(y.Args); + x.SyntaxTree == y.SyntaxTree && + x.TextSpan.Equals(y.TextSpan) && + x.Arguments.SequenceEqual(y.Arguments); } } } diff --git a/src/ComputeSharp.SourceGeneration/Models/Result{T}.cs b/src/ComputeSharp.SourceGeneration/Models/Result{T}.cs index 923ec833..5b300a5b 100644 --- a/src/ComputeSharp.SourceGeneration/Models/Result{T}.cs +++ b/src/ComputeSharp.SourceGeneration/Models/Result{T}.cs @@ -1,5 +1,4 @@ using System.Collections.Immutable; -using Microsoft.CodeAnalysis; namespace ComputeSharp.SourceGeneration.Models; @@ -9,4 +8,4 @@ namespace ComputeSharp.SourceGeneration.Models; /// The type of the wrapped value. /// The wrapped value for the current result. /// The associated diagnostic errors, if any. -internal sealed record Result(TValue Value, ImmutableArray Errors); +internal sealed record Result(TValue Value, ImmutableArray Errors); diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs index 020a1e84..2bb75f0e 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -6,6 +6,7 @@ using System.Text; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; +using ComputeSharp.SourceGeneration.Models; using ComputeSharp.SourceGeneration.SyntaxRewriters; using ComputeSharp.SourceGenerators.Models; using ComputeSharp.SourceGenerators.SyntaxRewriters; @@ -39,9 +40,9 @@ partial class IShaderGenerator Compilation compilation, StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, - out ImmutableArray diagnostics) + out ImmutableArray diagnostics) { - ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); + ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); // Detect invalid properties DetectAndReportInvalidPropertyDeclarations(builder, structDeclarationSymbol); @@ -94,7 +95,7 @@ partial class IShaderGenerator /// /// Gets a sequence of captured fields and their mapped names. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input instance to process. /// The collection of currently discovered types. /// Indicates whether or not represents a compute shader. @@ -103,7 +104,7 @@ partial class IShaderGenerator ImmutableArray<(string MetadataName, string Name, string HlslType)>, ImmutableArray<(string Name, string HlslType)>) GetInstanceFields( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol, ICollection types, bool isComputeShader) @@ -186,7 +187,7 @@ partial class IShaderGenerator /// /// Gets a sequence of shader static fields and their mapped names. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The instance for the type to process. /// The instance for the current type. /// The type symbol for the shader type. @@ -194,7 +195,7 @@ partial class IShaderGenerator /// The collection of discovered constant definitions. /// A sequence of static constant fields in . private static ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> GetStaticFields( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, SemanticModelProvider semanticModel, StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, @@ -256,12 +257,12 @@ partial class IShaderGenerator /// /// Gets a sequence of captured members and their mapped names. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input instance to process. /// The collection of currently discovered types. /// A sequence of captured members in . private static ImmutableArray<(string Name, string Type, int? Count)> GetSharedBuffers( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol, ICollection types) { @@ -312,7 +313,7 @@ partial class IShaderGenerator /// /// Gets a sequence of processed methods declared within a given type. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The type symbol for the shader type. /// The instance for the current type. /// The instance for the type to process. @@ -322,7 +323,7 @@ partial class IShaderGenerator /// Indicates whether or not represents a compute shader. /// A sequence of processed methods in , and the entry point. private static (string EntryPoint, ImmutableArray<(string Signature, string Definition)> Methods, bool IsSamplerUser) GetProcessedMethods( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, SemanticModelProvider semanticModel, @@ -441,10 +442,10 @@ partial class IShaderGenerator /// /// The sequence of discovered custom types. /// The symbol for the current object being processed. - /// The collection of produced instances. + /// The collection of produced instances. /// A sequence of custom type definitions to add to the shader source. internal static ImmutableArray<(string Name, string Definition)> GetDeclaredTypes( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, ISymbol sourceSymbol, IEnumerable types) { @@ -503,9 +504,9 @@ partial class IShaderGenerator /// /// Finds and reports all invalid declared properties in a shader. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The input instance to process. - private static void DetectAndReportInvalidPropertyDeclarations(ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol) + private static void DetectAndReportInvalidPropertyDeclarations(ImmutableArray.Builder diagnostics, INamedTypeSymbol structDeclarationSymbol) { foreach (var memberSymbol in structDeclarationSymbol.GetMembers()) { diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs index 8ecf75a9..2326268c 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs @@ -42,9 +42,9 @@ partial class IShaderGenerator INamedTypeSymbol structDeclarationSymbol, bool isDynamicShader, bool supportsDynamicShaders, - out ImmutableArray diagnostics) + out ImmutableArray diagnostics) { - ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); + ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); AttributeData? attribute = structDeclarationSymbol .GetAttributes() @@ -149,7 +149,7 @@ partial class IShaderGenerator ThreadIdsInfo threadIds, string hlslSource, CancellationToken token, - out DiagnosticInfo? diagnostic) + out DeferredDiagnosticInfo? diagnostic) { ImmutableArray bytecode = ImmutableArray.Empty; @@ -189,11 +189,11 @@ partial class IShaderGenerator } catch (Win32Exception e) { - diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, FixupExceptionMessage(e.Message)); + diagnostic = DeferredDiagnosticInfo.Create(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, FixupExceptionMessage(e.Message)); } catch (DxcCompilationException e) { - diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithDxcCompilationException, FixupExceptionMessage(e.Message)); + diagnostic = DeferredDiagnosticInfo.Create(EmbeddedBytecodeFailedWithDxcCompilationException, FixupExceptionMessage(e.Message)); } End: diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs index 665162db..932c1e3a 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs @@ -6,6 +6,7 @@ using System.Runtime.CompilerServices; using ComputeSharp.Core.Helpers; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Mappings; +using ComputeSharp.SourceGeneration.Models; using ComputeSharp.SourceGenerators.Models; using Microsoft.CodeAnalysis; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; @@ -34,7 +35,7 @@ partial class IShaderGenerator Type shaderInterfaceType, out int resourceCount, out int root32BitConstantCount, - out ImmutableArray diagnostics) + out ImmutableArray diagnostics) { // Helper method that uses boxes instead of ref-s (illegal in enumerators) static IEnumerable GetCapturedFieldInfos( @@ -87,7 +88,7 @@ partial class IShaderGenerator } } - ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); + ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); // Setup the resource and byte offsets for tracking. Pixel shaders have only two // implicitly captured int values, as they're always dispatched over a 2D texture. diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index 40022e47..9f0a1608 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -88,7 +88,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator item.Left.Left.Type, out int resourceCount, out int root32BitConstantCount, - out ImmutableArray dispatchDataDiagnostics); + out ImmutableArray dispatchDataDiagnostics); token.ThrowIfCancellationRequested(); @@ -106,7 +106,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator item.Right, item.Left.Left.Syntax, item.Left.Left.Symbol, - out ImmutableArray hlslSourceDiagnostics); + out ImmutableArray hlslSourceDiagnostics); token.ThrowIfCancellationRequested(); @@ -115,7 +115,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator item.Left.Left.Symbol, !hlslSourceInfo.Delegates.IsEmpty, item.Left.Right, - out ImmutableArray threadIdsDiagnostics); + out ImmutableArray threadIdsDiagnostics); token.ThrowIfCancellationRequested(); @@ -206,11 +206,11 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator .Select(static (item, token) => (item.Dispatch.Hierarchy, BuildHlslSource.GetNonDynamicHlslSource(item.Hlsl), item.ThreadIds)); // Compile the requested shader bytecodes - IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo, DeferredDiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors = shaderBytecodeInfo .Select(static (item, token) => { - ImmutableArray bytecode = LoadBytecode.GetBytecode(item.ThreadIds, item.Hlsl, token, out DiagnosticInfo? diagnostic); + ImmutableArray bytecode = LoadBytecode.GetBytecode(item.ThreadIds, item.Hlsl, token, out DeferredDiagnosticInfo? diagnostic); token.ThrowIfCancellationRequested(); @@ -224,7 +224,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator }); // Gather the diagnostics - IncrementalValuesProvider embeddedBytecodeDiagnostics = + IncrementalValuesProvider embeddedBytecodeDiagnostics = embeddedBytecodeWithErrors .Select(static (item, token) => (item.Hierarchy.FullyQualifiedMetadataName, item.Diagnostic)) .Where(static item => item.Diagnostic is not null) @@ -232,11 +232,8 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator .Select(static (item, token) => { INamedTypeSymbol typeSymbol = item.Right.GetTypeByMetadataName(item.Left.FullyQualifiedMetadataName)!; - - return Diagnostic.Create( - item.Left.Diagnostic!.Descriptor, - typeSymbol.Locations.FirstOrDefault(), - new object[] { typeSymbol }.Concat(item.Left.Diagnostic.Args).ToArray()); + + return DiagnosticInfo.Create(item.Left.Diagnostic!.Descriptor, typeSymbol, new object[] { typeSymbol }.Concat(item.Left.Diagnostic.Arguments).ToArray()); }); // Output the diagnostics diff --git a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs index 936069b3..d1998d13 100644 --- a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs @@ -46,7 +46,7 @@ public sealed partial class ShaderMethodSourceGenerator : IIncrementalGenerator item.Right, item.Left.Syntax, item.Left.Symbol, - out ImmutableArray diagnostics); + out ImmutableArray diagnostics); return new Result(sourceInfo, diagnostics); }); @@ -85,9 +85,9 @@ public sealed partial class ShaderMethodSourceGenerator : IIncrementalGenerator Compilation compilation, CSharpSyntaxNode methodDeclaration, IMethodSymbol methodSymbol, - out ImmutableArray diagnostics) + out ImmutableArray diagnostics) { - ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); + ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); // We need to sets to track all discovered custom types and static methods HashSet discoveredTypes = new(SymbolEqualityComparer.Default); @@ -112,14 +112,14 @@ public sealed partial class ShaderMethodSourceGenerator : IIncrementalGenerator /// /// Gets a sequence of processed methods from a target method declaration. /// - /// The collection of produced instances. + /// The collection of produced instances. /// The or instance for the current method. /// The instance for the method to process. /// The collection of currently discovered types. /// The collection of discovered constant definitions. /// A sequence of processed methods in (main method and all captured methods). private static (string TargetMethod, ImmutableArray<(string Signature, string Definition)> DependentMethods) GetProcessedMethods( - ImmutableArray.Builder diagnostics, + ImmutableArray.Builder diagnostics, CSharpSyntaxNode methodDeclaration, SemanticModelProvider semanticModel, ICollection discoveredTypes,