diff --git a/src/ComputeSharp.SourceGenerators/Helpers/SyntaxFactoryHelper.cs b/src/ComputeSharp.SourceGenerators/Helpers/SyntaxFactoryHelper.cs index 5c6d27b8..80403c01 100644 --- a/src/ComputeSharp.SourceGenerators/Helpers/SyntaxFactoryHelper.cs +++ b/src/ComputeSharp.SourceGenerators/Helpers/SyntaxFactoryHelper.cs @@ -12,28 +12,6 @@ namespace ComputeSharp.SourceGenerators.Helpers /// internal static class SyntaxFactoryHelper { - /// - /// Creates a [] array expression from the given sequence of instances. - /// - /// That it, it applies the following transformation: - /// - /// { "S1", "S2" } => new string[] { "S1", "S2" } - /// - /// - /// - /// The input sequence of instances. - /// An instance with the described contents. - [Pure] - public static ArrayCreationExpressionSyntax ArrayExpression(IEnumerable values) - { - return - ArrayCreationExpression( - ArrayType(PredefinedType(Token(SyntaxKind.StringKeyword))) - .AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList(OmittedArraySizeExpression())))) - .WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression) - .AddExpressions(values.Select(static value => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(value))).ToArray())); - } - /// /// Creates an [] array expression with the nested groups in the input sequence. /// diff --git a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs index e3f91e72..898c36a4 100644 --- a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs @@ -66,11 +66,10 @@ namespace ComputeSharp.SourceGenerators { // We need to sets to track all discovered custom types and static methods HashSet discoveredTypes = new(SymbolEqualityComparer.Default); - Dictionary staticMethods = new(SymbolEqualityComparer.Default); Dictionary constantDefinitions = new(SymbolEqualityComparer.Default); // Explore the syntax tree and extract the processed info - var (invokeMethod, localFunctions) = GetProcessedMethods(context, methodDeclaration, semanticModel, discoveredTypes, staticMethods, constantDefinitions); + var (invokeMethod, processedMethods) = GetProcessedMethods(context, methodDeclaration, semanticModel, discoveredTypes, constantDefinitions); var processedTypes = IShaderGenerator.GetProcessedTypes(discoveredTypes).ToArray(); var processedConstants = IShaderGenerator.GetProcessedConstants(constantDefinitions); @@ -80,9 +79,9 @@ namespace ComputeSharp.SourceGenerators AttributeList(SingletonSeparatedList( Attribute(IdentifierName(typeof(ShaderMethodSourceAttribute).FullName)).AddArgumentListArguments( AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(methodDeclarationSymbol.GetFullMetadataName(true)))), - AttributeArgument(NestedArrayExpression(processedTypes)), AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(invokeMethod))), - AttributeArgument(ArrayExpression(localFunctions)), + AttributeArgument(NestedArrayExpression(processedTypes)), + AttributeArgument(NestedArrayExpression(processedMethods)), AttributeArgument(NestedArrayExpression(processedConstants))))) .WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList())) .WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword)))) @@ -100,18 +99,18 @@ namespace ComputeSharp.SourceGenerators /// The instance for the current method. /// The instance for the method to process. /// The collection of currently discovered types. - /// The set of discovered and processed static methods. /// The collection of discovered constant definitions. - /// A sequence of processed methods in (main method and local functions). + /// A sequence of processed methods in (main method and all captured methods). [Pure] - private static (string TargetMethod, IEnumerable LocalFunctions) GetProcessedMethods( + private static (string TargetMethod, IEnumerable<(string Signature, string Definition)> ProcessedMethods) GetProcessedMethods( GeneratorExecutionContext context, MethodDeclarationSyntax methodDeclaration, SemanticModelProvider semanticModel, ICollection discoveredTypes, - IDictionary staticMethods, IDictionary constantDefinitions) { + Dictionary staticMethods = new(SymbolEqualityComparer.Default); + ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, constantDefinitions, context); // Rewrite the method syntax tree @@ -122,15 +121,25 @@ namespace ComputeSharp.SourceGenerators .NormalizeWhitespace(eol: "\n") .ToFullString(); - List localFunctions = new(shaderSourceRewriter.LocalFunctions.Count); + List<(string, string)> processedMethods = new(shaderSourceRewriter.LocalFunctions.Count); // Emit the extracted local functions foreach (var localFunction in shaderSourceRewriter.LocalFunctions) { - localFunctions.Add(localFunction.Value.NormalizeWhitespace(eol: "\n").ToFullString()); + processedMethods.Add(( + localFunction.Value.AsDefinition().NormalizeWhitespace(eol: "\n").ToFullString(), + localFunction.Value.NormalizeWhitespace(eol: "\n").ToFullString())); } - return (targetMethod, localFunctions); + // Emit the discovered static methods + foreach (var staticMethod in staticMethods.Values) + { + processedMethods.Add(( + staticMethod.AsDefinition().NormalizeWhitespace(eol: "\n").ToFullString(), + staticMethod.NormalizeWhitespace(eol: "\n").ToFullString())); + } + + return (targetMethod, processedMethods); } } } diff --git a/src/ComputeSharp/Core/Attributes/__Internals/ShaderMethodSourceAttribute.cs b/src/ComputeSharp/Core/Attributes/__Internals/ShaderMethodSourceAttribute.cs index bde41457..fd76f48f 100644 --- a/src/ComputeSharp/Core/Attributes/__Internals/ShaderMethodSourceAttribute.cs +++ b/src/ComputeSharp/Core/Attributes/__Internals/ShaderMethodSourceAttribute.cs @@ -23,16 +23,16 @@ namespace ComputeSharp.__Internals /// internal const string InvokeMethodIdentifier = "____"; - /// - /// The source code for the target entry point method. - /// - private readonly string invokeMethod; - /// /// The fully qualified name of the current method. /// private readonly string methodName; + /// + /// The source code for the target entry point method. + /// + private readonly string invokeMethod; + /// /// The collection of custom types. /// @@ -41,7 +41,7 @@ namespace ComputeSharp.__Internals /// /// The collection of processed methods. /// - private readonly string[] methods; + private readonly Dictionary methods; /// /// The collection of discovered constants. @@ -52,16 +52,16 @@ namespace ComputeSharp.__Internals /// Creates a new instance with the specified parameters. /// /// The fully qualified name of the current method. - /// The collection of custom types. /// The source code for the target entry point method. + /// The collection of custom types. /// The collection of processed methods. /// The collection of discovered constants. - public ShaderMethodSourceAttribute(string methodName, object[] types, string invokeMethod, string[] methods, object[] constants) + public ShaderMethodSourceAttribute(string methodName, string invokeMethod, object[] types, object[] methods, object[] constants) { - this.invokeMethod = invokeMethod; this.methodName = methodName; + this.invokeMethod = invokeMethod; this.types = types.Cast().ToDictionary(static c => c[0], static c => c[1]); - this.methods = methods; + this.methods = methods.Cast().ToDictionary(static c => c[0], static c => c[1]); this.constants = constants.Cast().ToDictionary(static c => c[0], static c => c[1]); }