diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs index 1ccd952e..24bd33b5 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs @@ -44,20 +44,19 @@ namespace ComputeSharp.SourceGenerators // The list to use to track all discovered custom types HashSet discoveredTypes = new(SymbolEqualityComparer.Default); - // Helper that converts a sequence of string pairs into a nested array expression. + // Helper that converts a sequence of string sequences into a nested array expression. // That is, this applies the following transformation: // - { ("K1", "V1"), ("K2", "V2") } => new object[] { new[] { "K1", "V1" }, new[] { "K2", "V2" } } - static ArrayCreationExpressionSyntax NestedPairsArrayExpression(IEnumerable<(string Key, string Value)> pairs) + static ArrayCreationExpressionSyntax NestedArrayExpression(IEnumerable> groups) { return ArrayCreationExpression( ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword))) .AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList(OmittedArraySizeExpression())))) .WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression) - .AddExpressions(pairs.Select(static pair => + .AddExpressions(groups.Select(static group => ImplicitArrayCreationExpression(InitializerExpression(SyntaxKind.ArrayInitializerExpression).AddExpressions( - LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(pair.Key)), - LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(pair.Value))))).ToArray())); + group.Select(static item => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(item))).ToArray()))).ToArray())); } // Helper that converts a sequence of strings into an array expression. @@ -79,8 +78,8 @@ namespace ComputeSharp.SourceGenerators AttributeList(SingletonSeparatedList( Attribute(IdentifierName(typeof(IComputeShaderSourceAttribute).FullName)).AddArgumentListArguments( AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(structDeclarationSymbol.GetFullMetadataName()))), - AttributeArgument(NestedPairsArrayExpression(GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray())), - AttributeArgument(NestedPairsArrayExpression(GetProcessedMethods(structDeclaration, semanticModel, discoveredTypes).ToArray())), + AttributeArgument(NestedArrayExpression(GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray())), + AttributeArgument(NestedArrayExpression(GetProcessedMethods(structDeclaration, semanticModel, discoveredTypes).ToArray())), AttributeArgument(ArrayExpression(GetProcessedTypes(discoveredTypes)))))) .WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList())) .WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword)))) @@ -99,14 +98,14 @@ namespace ComputeSharp.SourceGenerators /// The collection of currently discovered types. /// A sequence of captured members in . [Pure] - private static IEnumerable<(string Key, string Value)> GetProcessedMembers(INamedTypeSymbol structDeclarationSymbol, ICollection types) + private static IEnumerable> GetProcessedMembers(INamedTypeSymbol structDeclarationSymbol, ICollection types) { foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) { _ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping); // Yield back the current mapping for the name (if the name used a reserved keyword) - yield return (fieldSymbol.Name, mapping ?? fieldSymbol.Name); + yield return new[] { fieldSymbol.Name, mapping ?? fieldSymbol.Name }; // Track the type of items in the current buffer if (fieldSymbol.Type is INamedTypeSymbol fieldType && @@ -125,7 +124,7 @@ namespace ComputeSharp.SourceGenerators /// The collection of currently discovered types. /// A sequence of processed methods in . [Pure] - private static IEnumerable<(string Key, string Value)> GetProcessedMethods( + private static IEnumerable> GetProcessedMethods( StructDeclarationSyntax structDeclaration, SemanticModel semanticModel, ICollection types) @@ -159,12 +158,12 @@ namespace ComputeSharp.SourceGenerators // Produce the final method source var processedMethodSource = processedMethod.NormalizeWhitespace().ToFullString(); - yield return (methodDeclarationSymbol.Name, processedMethodSource); + yield return new[] { methodDeclarationSymbol.Name, processedMethodSource }; // Emit the extracted local functions foreach (var localFunction in shaderSourceRewriter.LocalFunctions) { - yield return (localFunction.Key, localFunction.Value.NormalizeWhitespace().ToFullString()); + yield return new[] { localFunction.Key, localFunction.Value.NormalizeWhitespace().ToFullString() }; } } }