using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics.Contracts; using System.Linq; using System.Text; using ComputeSharp.SourceGenerators.Extensions; using ComputeSharp.SourceGenerators.Mappings; using ComputeSharp.SourceGenerators.SyntaxRewriters; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Text; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; #pragma warning disable CS0618 namespace ComputeSharp.SourceGenerators { [Generator] public class IComputeShaderSourceAttributeGenerator : ISourceGenerator { /// public void Initialize(GeneratorInitializationContext context) { } /// public void Execute(GeneratorExecutionContext context) { // Find all the struct declarations ImmutableArray structDeclarations = ( from tree in context.Compilation.SyntaxTrees from structDeclaration in tree.GetRoot().DescendantNodes().OfType() select structDeclaration).ToImmutableArray(); foreach (StructDeclarationSyntax structDeclaration in structDeclarations) { SemanticModel semanticModel = context.Compilation.GetSemanticModel(structDeclaration.SyntaxTree); INamedTypeSymbol structDeclarationSymbol = semanticModel.GetDeclaredSymbol(structDeclaration)!; // Only process compute shader types if (!structDeclarationSymbol.Interfaces.Any(interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue; // Helper that converts a sequence of string pairs 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) { return ArrayCreationExpression( ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword))) .AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList(OmittedArraySizeExpression())))) .WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression) .AddExpressions(pairs.Select(pair => ImplicitArrayCreationExpression(InitializerExpression(SyntaxKind.ArrayInitializerExpression).AddExpressions( LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(pair.Key)), LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(pair.Value))))).ToArray())); } // Create the compilation unit with the source attribute var source = CompilationUnit().AddAttributeLists( AttributeList(SingletonSeparatedList( Attribute(IdentifierName(typeof(IComputeShaderSourceAttribute).FullName)).AddArgumentListArguments( AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(structDeclarationSymbol.GetFullMetadataName()))), AttributeArgument(NestedPairsArrayExpression(GetProcessedMembers(structDeclarationSymbol))), AttributeArgument(NestedPairsArrayExpression(GetProcessedMethods(structDeclaration, semanticModel)))))) .WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList())) .WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword)))) .NormalizeWhitespace() .ToFullString(); var generatedFileName = $"__ComputeSharp_{nameof(IComputeShaderSourceAttribute)}_{structDeclarationSymbol.Name}"; // Add the method source attribute context.AddSource(generatedFileName, SourceText.From(source, Encoding.UTF8)); } } /// /// Gets a sequence of captured members and their mapped names. /// /// The input instance to process. /// A sequence of captured members in . [Pure] private static IEnumerable<(string Key, string Value)> GetProcessedMembers(INamedTypeSymbol structDeclarationSymbol) { foreach (string memberName in ( from member in structDeclarationSymbol.GetMembers() where member.Kind == SymbolKind.Field || member.Kind == SymbolKind.Property select member.Name).Distinct()) { _ = HlslKnownKeywords.TryGetMappedName(memberName, out string? mapping); yield return (memberName, mapping ?? memberName); } } /// /// Gets a sequence of processed methods declared within a given type. /// /// The instance for the current type. /// The instance for the type to process. /// A sequence of processed methods in . [Pure] private static IEnumerable<(string Key, string Value)> GetProcessedMethods( StructDeclarationSyntax structDeclaration, SemanticModel semanticModel) { // Find all declared methods in the type ImmutableArray methodDeclarations = ( from syntaxNode in structDeclaration.DescendantNodes() where syntaxNode.IsKind(SyntaxKind.MethodDeclaration) select (MethodDeclarationSyntax)syntaxNode).ToImmutableArray(); foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations) { IMethodSymbol methodDeclarationSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration)!; ShaderSourceRewriter shaderSourceRewriter = new(semanticModel); // Rewrite the method syntax tree var processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia(); // If the method is the shader entry point, do additional processing if (methodDeclarationSymbol.Name == nameof(IComputeShader.Execute) && methodDeclarationSymbol.ReturnsVoid && methodDeclarationSymbol.TypeParameters.Length == 0 && methodDeclarationSymbol.Parameters.Length == 1 && methodDeclarationSymbol.Parameters[0].Type.ToDisplayString() == typeof(ThreadIds).FullName) { var parameterName = methodDeclarationSymbol.Parameters[0].Name; processedMethod = new ExecuteMethodRewriter(parameterName).Visit(processedMethod)!; } // Produce the final method source var processedMethodSource = processedMethod.NormalizeWhitespace().ToFullString(); yield return (methodDeclarationSymbol.Name, processedMethodSource); // Emit the extracted local functions foreach (var localFunction in shaderSourceRewriter.LocalFunctions) { yield return (localFunction.Key, localFunction.Value.NormalizeWhitespace().ToFullString()); } } } } }