From 7331b30bc5ccfbe750a575f2ddcba253968851d2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 29 Nov 2023 22:19:23 +0100 Subject: [PATCH] Handle partials by gathering symbols for fields, methods --- ...uteShaderDescriptorGenerator.HlslSource.cs | 129 +++++++++--------- 1 file changed, 65 insertions(+), 64 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs index 8bf4add2..c9a1fc0b 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs @@ -71,9 +71,9 @@ partial class ComputeShaderDescriptorGenerator string? implicitTextureType = isComputeShader ? null : HlslKnownTypes.GetMappedNameForPixelShaderType(pixelShaderSymbol!); (ImmutableArray<(string MetadataName, string Name, string HlslType)> resourceFields, ImmutableArray<(string Name, string HlslType)> valueFields) = GetInstanceFields(diagnostics, structDeclarationSymbol, discoveredTypes, isComputeShader); ImmutableArray<(string Name, string Type, int? Count)> sharedBuffers = GetSharedBuffers(diagnostics, structDeclarationSymbol, discoveredTypes); - (string entryPoint, ImmutableArray<(string Signature, string Definition)> processedMethods, isSamplerUsed) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, instanceMethods, constantDefinitions, isComputeShader); + (string entryPoint, ImmutableArray<(string Signature, string Definition)> processedMethods, isSamplerUsed) = GetProcessedMethods(diagnostics, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, instanceMethods, constantDefinitions, isComputeShader); (string, string)? implicitSamplerField = isSamplerUsed ? ("SamplerState", "__sampler") : default((string, string)?); - ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions); + ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclarationSymbol, discoveredTypes, constantDefinitions); // Process the discovered types and constants ImmutableArray<(string Name, string Definition)> declaredTypes = HlslDefinitionsSyntaxProcessor.GetDeclaredTypes(diagnostics, structDeclarationSymbol, discoveredTypes, instanceMethods); @@ -204,7 +204,6 @@ partial class ComputeShaderDescriptorGenerator /// /// 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. /// The collection of currently discovered types. /// The collection of discovered constant definitions. @@ -212,56 +211,56 @@ partial class ComputeShaderDescriptorGenerator private static ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> GetStaticFields( ImmutableArrayBuilder diagnostics, SemanticModelProvider semanticModel, - StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, ICollection discoveredTypes, IDictionary constantDefinitions) { using ImmutableArrayBuilder<(string, string, string?)> builder = new(); - foreach (FieldDeclarationSyntax fieldDeclaration in structDeclaration.Members.OfType()) + foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers()) { - foreach (VariableDeclaratorSyntax variableDeclarator in fieldDeclaration.Declaration.Variables) + // Find all declared static fields in the type + if (memberSymbol is not IFieldSymbol { IsImplicitlyDeclared: false, IsStatic: true, IsConst: false, DeclaringSyntaxReferences: [SyntaxReference fieldReference, ..] } fieldSymbol) { - IFieldSymbol fieldSymbol = (IFieldSymbol)semanticModel.For(variableDeclarator).GetDeclaredSymbol(variableDeclarator)!; - - if (!fieldSymbol.IsStatic || fieldSymbol.IsConst) - { - continue; - } - - if (fieldSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.GroupSharedAttribute", out _)) - { - continue; - } - - // Constant properties must be of a primitive, vector or matrix type - if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol || - !HlslKnownTypes.IsKnownHlslType(typeSymbol.GetFullyQualifiedMetadataName())) - { - diagnostics.Add(InvalidShaderStaticFieldType, variableDeclarator, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type); - - continue; - } - - _ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping); - - string typeDeclaration = fieldSymbol.IsReadOnly switch - { - true => $"static const {HlslKnownTypes.GetMappedName(typeSymbol)}", - false => $"static {HlslKnownTypes.GetMappedName(typeSymbol)}" - }; - - StaticFieldRewriter staticFieldRewriter = new( - semanticModel, - discoveredTypes, - constantDefinitions, - diagnostics); - - string? assignment = staticFieldRewriter.Visit(variableDeclarator)?.NormalizeWhitespace(eol: "\n").ToFullString(); - - builder.Add((mapping ?? fieldSymbol.Name, typeDeclaration, assignment)); + continue; } + + if (fieldReference.GetSyntax() is not VariableDeclaratorSyntax variableDeclarator) + { + continue; + } + + if (fieldSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.GroupSharedAttribute", out _)) + { + continue; + } + + // Constant properties must be of a primitive, vector or matrix type + if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol || + !HlslKnownTypes.IsKnownHlslType(typeSymbol.GetFullyQualifiedMetadataName())) + { + diagnostics.Add(InvalidShaderStaticFieldType, variableDeclarator, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type); + + continue; + } + + _ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping); + + string typeDeclaration = fieldSymbol.IsReadOnly switch + { + true => $"static const {HlslKnownTypes.GetMappedName(typeSymbol)}", + false => $"static {HlslKnownTypes.GetMappedName(typeSymbol)}" + }; + + StaticFieldRewriter staticFieldRewriter = new( + semanticModel, + discoveredTypes, + constantDefinitions, + diagnostics); + + string? assignment = staticFieldRewriter.Visit(variableDeclarator)?.NormalizeWhitespace(eol: "\n").ToFullString(); + + builder.Add((mapping ?? fieldSymbol.Name, typeDeclaration, assignment)); } return builder.ToImmutable(); @@ -313,17 +312,15 @@ partial class ComputeShaderDescriptorGenerator /// /// 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. /// The collection of currently discovered types. /// The set of discovered and processed static methods. /// The collection of discovered instance methods for custom struct types. /// The collection of discovered constant definitions. /// Indicates whether or not represents a compute shader. - /// A sequence of processed methods in , and the entry point. + /// A sequence of processed methods in , and the entry point. private static (string EntryPoint, ImmutableArray<(string Signature, string Definition)> Methods, bool IsSamplerUser) GetProcessedMethods( ImmutableArrayBuilder diagnostics, - StructDeclarationSyntax structDeclaration, INamedTypeSymbol structDeclarationSymbol, SemanticModelProvider semanticModel, ICollection discoveredTypes, @@ -332,34 +329,38 @@ partial class ComputeShaderDescriptorGenerator IDictionary constantDefinitions, bool isComputeShader) { - // Find all declared methods in the type - ImmutableArray methodDeclarations = ( - from syntaxNode in structDeclaration.DescendantNodes() - where syntaxNode.IsKind(SyntaxKind.MethodDeclaration) - select (MethodDeclarationSyntax)syntaxNode).ToImmutableArray(); - using ImmutableArrayBuilder<(string, string)> methods = new(); string? entryPoint = null; bool isSamplerUsed = false; - foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations) + foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers()) { - IMethodSymbol methodDeclarationSymbol = semanticModel.For(methodDeclaration).GetDeclaredSymbol(methodDeclaration)!; + // Find all declared methods in the type + if (memberSymbol is not IMethodSymbol { IsImplicitlyDeclared: false, DeclaringSyntaxReferences: [SyntaxReference methodReference, ..] } methodSymbol) + { + continue; + } + + if (methodReference.GetSyntax() is not MethodDeclarationSyntax methodDeclaration) + { + continue; + } + bool isShaderEntryPoint = (isComputeShader && - methodDeclarationSymbol.Name == nameof(IComputeShader.Execute) && - methodDeclarationSymbol.ReturnsVoid && - methodDeclarationSymbol.TypeParameters.Length == 0 && - methodDeclarationSymbol.Parameters.Length == 0) || + methodSymbol.Name == nameof(IComputeShader.Execute) && + methodSymbol.ReturnsVoid && + methodSymbol.TypeParameters.Length == 0 && + methodSymbol.Parameters.Length == 0) || (!isComputeShader && - methodDeclarationSymbol.Name == nameof(IComputeShader.Execute) && - methodDeclarationSymbol.ReturnType is not null && // TODO: match for pixel type - methodDeclarationSymbol.TypeParameters.Length == 0 && - methodDeclarationSymbol.Parameters.Length == 0); + methodSymbol.Name == nameof(IComputeShader.Execute) && + methodSymbol.ReturnType is not null && // TODO: match for pixel type + methodSymbol.TypeParameters.Length == 0 && + methodSymbol.Parameters.Length == 0); // Except for the entry point, ignore explicit interface implementations - if (!isShaderEntryPoint && !methodDeclarationSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty) + if (!isShaderEntryPoint && !methodSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty) { continue; }