From 514d2921fcb11f1dff2093760c79cd7db6d59421 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 24 Sep 2023 22:39:59 +0200 Subject: [PATCH] Remove language checks for C# 11 --- ...ator.CreateBuildHlslSourceMethod.Syntax.cs | 9 ++--- .../ID2D1ShaderGenerator.cs | 18 ++++------ .../ComputeSharp.SourceGeneration.projitems | 1 - .../IncrementalValueProviderExtensions.cs | 35 ------------------- ...ator.CreateBuildHlslSourceMethod.Syntax.cs | 14 +++----- .../IShaderGenerator.cs | 16 ++++----- 6 files changed, 18 insertions(+), 75 deletions(-) delete mode 100644 src/ComputeSharp.SourceGeneration/Extensions/IncrementalValueProviderExtensions.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs index 9cfecca8..5fbbe563 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs @@ -20,15 +20,10 @@ partial class ID2D1ShaderGenerator /// /// The input HLSL source. /// The depth of the hierarchy for this type (used to calculate the right indentation). - /// Whether to use a raw multiline string literal expression /// The resulting instance for the BuildHlslSource method. - public static MethodDeclarationSyntax GetSyntax(string hlslSource, int hierarchyDepth, bool useRawMultiLineStringLiteralExpression) + public static MethodDeclarationSyntax GetSyntax(string hlslSource, int hierarchyDepth) { - SyntaxToken hlslSourceLiteralExpression = useRawMultiLineStringLiteralExpression switch - { - true => SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSource, hierarchyDepth), - false => Literal(hlslSource) - }; + SyntaxToken hlslSourceLiteralExpression = SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSource, hierarchyDepth); // This code produces a method declaration as follows: // diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index 33cb52ea..8684d51e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -313,24 +313,18 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Info.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadDispatchData)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); - // Check whether raw multiline string literals can be used (C# 11) - IncrementalValueProvider canUseRawMultilineStringLiterals = - context.ParseOptionsProvider - .Select((item, _) => item is CSharpParseOptions { LanguageVersion: >= LanguageVersion.CSharp11 }); - - // Get the BuildHlslSource() info (hierarchy, HLSL source and parsing options) - IncrementalValuesProvider<((HierarchyInfo Hierarchy, string HlslSource) Info, bool CanUseRawMultilineStringLiterals)> hlslSourceInfo = + // Get the BuildHlslSource() info (hierarchy and HLSL source) + IncrementalValuesProvider<(HierarchyInfo Hierarchy, string HlslSource)> hlslSourceInfo = shaderInfoWithErrors - .Select(static (item, _) => (item.Hierarchy, item.HlslShaderSource.HlslSource)) - .Combine(canUseRawMultilineStringLiterals); + .Select(static (item, _) => (item.Hierarchy, item.HlslShaderSource.HlslSource)); // Generate the BuildHlslSource() methods context.RegisterSourceOutput(hlslSourceInfo, static (context, item) => { - MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.Info.HlslSource, item.Info.Hierarchy.Hierarchy.Length, item.CanUseRawMultilineStringLiterals); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Info.Hierarchy, buildHlslStringMethod, canUseSkipLocalsInit: false); + MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.HlslSource, item.Hierarchy.Hierarchy.Length); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, buildHlslStringMethod, canUseSkipLocalsInit: false); - context.AddSource($"{item.Info.Hierarchy.FullyQualifiedMetadataName}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); + context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); // Get a filtered sequence to enable caching for the HLSL source info, before the shader compilation step diff --git a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems index 1fa9c05e..0e4410f2 100644 --- a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems +++ b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems @@ -10,7 +10,6 @@ - diff --git a/src/ComputeSharp.SourceGeneration/Extensions/IncrementalValueProviderExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/IncrementalValueProviderExtensions.cs deleted file mode 100644 index 70c8fbd5..00000000 --- a/src/ComputeSharp.SourceGeneration/Extensions/IncrementalValueProviderExtensions.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Microsoft.CodeAnalysis; - -namespace ComputeSharp.SourceGeneration.Extensions; - -/// -/// Extension methods for the type. -/// -internal static class IncrementalValueProviderExtensions -{ - /// - /// Combines three instances. - /// - /// The type of values produced by the first input. - /// The type of values produced by the second input. - /// The type of values produced by the third input. - /// The first input. - /// The second input. - /// The third input. - /// The resulting combined result. - public static IncrementalValueProvider<(T1, T2, T3)> Combine( - this IncrementalValueProvider source1, - IncrementalValueProvider source2, - IncrementalValueProvider source3) - { - return - source1 - .Combine(source2) - .Combine(source3) - .Select(static (items, _) => (items.Left.Left, items.Left.Right, items.Right)); - } -} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs index e83b3614..c0484d51 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs @@ -25,13 +25,12 @@ partial class IShaderGenerator /// The input instance to use. /// Indicates whether or not dynamic shaders are supported. /// The depth of the hierarchy for this type (used to calculate the right indentation). - /// Whether to use a raw multiline string literal expression /// The resulting instance for the BuildHlslSource method. - public static MethodDeclarationSyntax GetSyntax(HlslShaderSourceInfo hlslSourceInfo, bool supportsDynamicShaders, int hierarchyDepth, bool useRawMultiLineStringLiteralExpression) + public static MethodDeclarationSyntax GetSyntax(HlslShaderSourceInfo hlslSourceInfo, bool supportsDynamicShaders, int hierarchyDepth) { // Generate the necessary body statements depending on whether dynamic shaders are supported ImmutableArray bodyStatements = supportsDynamicShaders - ? GenerateRenderMethodBody(hlslSourceInfo, hierarchyDepth, useRawMultiLineStringLiteralExpression) + ? GenerateRenderMethodBody(hlslSourceInfo, hierarchyDepth) : GenerateEmptyMethodBody(); // This code produces a method declaration as follows: @@ -83,9 +82,8 @@ partial class IShaderGenerator /// /// The input instance to use. /// The depth of the hierarchy for this type (used to calculate the right indentation). - /// Whether to use a raw multiline string literal expression /// The series of statements to build the HLSL source to compile to execute the current shader. - private static ImmutableArray GenerateRenderMethodBody(HlslShaderSourceInfo hlslSourceInfo, int hierarchyDepth, bool useRawMultiLineStringLiteralExpression) + private static ImmutableArray GenerateRenderMethodBody(HlslShaderSourceInfo hlslSourceInfo, int hierarchyDepth) { using ImmutableArrayBuilder statements = ImmutableArrayBuilder.Rent(); @@ -121,11 +119,7 @@ partial class IShaderGenerator sizeHint += textBuilder.Length; - SyntaxToken hlslSourceLiteralExpression = useRawMultiLineStringLiteralExpression switch - { - true => SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSource, hierarchyDepth), - false => Literal(hlslSource) - }; + SyntaxToken hlslSourceLiteralExpression = SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSource, hierarchyDepth); statements.Add( ExpressionStatement( diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index 384f8239..de5aeb12 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -152,24 +152,20 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator context.AddSource($"{item.Info.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadDispatchData)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); - // Check whether raw multiline string literals can be used (C# 11) - IncrementalValueProvider canUseRawMultilineStringLiterals = - context.ParseOptionsProvider - .Select((item, _) => item is CSharpParseOptions { LanguageVersion: >= LanguageVersion.CSharp11 }); - // Get the BuildHlslSource info (hierarchy, HLSL source and parsing options) - IncrementalValuesProvider<((HierarchyInfo Hierarchy, HlslShaderSourceInfo HlslShaderSource) Info, (bool CanUseSkipLocalsInit, bool SupportsDynamicShaders, bool CanUseRawMultilineStringLiterals) Options)> hlslSourceInfo = + IncrementalValuesProvider<(((HierarchyInfo Hierarchy, HlslShaderSourceInfo HlslShaderSource) Left, bool CanUseSkipLocalsInit) Left, bool SupportsDynamicShaders)> hlslSourceInfo = shaderInfoWithErrors .Select(static (item, _) => (item.Hierarchy, item.HlslShaderSource)) - .Combine(canUseSkipLocalsInit.Combine(supportsDynamicShaders, canUseRawMultilineStringLiterals)); + .Combine(canUseSkipLocalsInit) + .Combine(supportsDynamicShaders); // Generate the BuildHlslSource() methods context.RegisterSourceOutput(hlslSourceInfo, static (context, item) => { - MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.Info.HlslShaderSource, item.Options.SupportsDynamicShaders, item.Info.Hierarchy.Hierarchy.Length, item.Options.CanUseRawMultilineStringLiterals); - CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Info.Hierarchy, buildHlslStringMethod, item.Options.CanUseSkipLocalsInit); + MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.Left.Left.HlslShaderSource, item.SupportsDynamicShaders, item.Left.Left.Hierarchy.Hierarchy.Length); + CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Left.Left.Hierarchy, buildHlslStringMethod, item.Left.CanUseSkipLocalsInit); - context.AddSource($"{item.Info.Hierarchy.FullyQualifiedMetadataName}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); + context.AddSource($"{item.Left.Left.Hierarchy.FullyQualifiedMetadataName}.{nameof(BuildHlslSource)}.g.cs", compilationUnit.GetText(Encoding.UTF8)); }); // Get the LoadDispatchMetadata() info (hierarchy and dispatch metadata info)