From 62e4cfed3bd5ae2cf32963bd0b7ce035f744f6c2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 25 Nov 2020 01:56:15 +0100 Subject: [PATCH] Refactored source generators to use imported types --- .../AutoConstructorAttributeGenerator.cs | 6 ++++-- .../IComputeShaderSourceAttributeGenerator.cs | 15 +++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/AutoConstructorAttributeGenerator.cs b/src/ComputeSharp.SourceGenerators/AutoConstructorAttributeGenerator.cs index 667d9df7..1eed23e6 100644 --- a/src/ComputeSharp.SourceGenerators/AutoConstructorAttributeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/AutoConstructorAttributeGenerator.cs @@ -25,7 +25,9 @@ namespace ComputeSharp.SourceGenerators ImmutableArray attributes = ( from tree in context.Compilation.SyntaxTrees from attribute in tree.GetRoot().DescendantNodes().OfType() - where attribute.Name.ToString() == "AutoConstructor" + let symbol = context.Compilation.GetSemanticModel(attribute.SyntaxTree) + let typeInfo = symbol.GetTypeInfo(attribute) + where typeInfo.Type is { Name: nameof(AutoConstructorAttribute) } select attribute).ToImmutableArray(); foreach (AttributeSyntax attribute in attributes) @@ -84,7 +86,7 @@ namespace ComputeSharp.SourceGenerators .ToFullString(); // Add the partial type - context.AddSource($"__ComputeSharp_AutoConstructorAttribute_{structDeclarationSymbol.Name}", SourceText.From(source, Encoding.UTF8)); + context.AddSource($"__ComputeSharp_{typeof(AutoConstructorAttribute).FullName}_{structDeclarationSymbol.Name}", SourceText.From(source, Encoding.UTF8)); } } } diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs index a7eefd01..36ed489f 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs @@ -8,6 +8,8 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Text; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; +#pragma warning disable CS0618 + namespace ComputeSharp.SourceGenerators { [Generator] @@ -33,7 +35,7 @@ namespace ComputeSharp.SourceGenerators INamedTypeSymbol structDeclarationSymbol = semanticModel.GetDeclaredSymbol(structDeclaration)!; // Only process compute shader types - if (!structDeclarationSymbol.Interfaces.Any(interfaceSymbol => interfaceSymbol.Name == "IComputeShader")) continue; + if (!structDeclarationSymbol.Interfaces.Any(interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue; var structFullName = structDeclarationSymbol.GetFullMetadataName(); @@ -50,11 +52,12 @@ namespace ComputeSharp.SourceGenerators // Rewrite the method syntax tree var processedMethod = new ShaderSourceRewriter(semanticModel).Visit(methodDeclaration)!.WithoutTrivia(); - if (methodDeclarationSymbol.Name == "Execute" && + // 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() == "ComputeSharp.ThreadIds") + methodDeclarationSymbol.Parameters[0].Type.ToDisplayString() == typeof(ThreadIds).FullName) { var parameterName = methodDeclarationSymbol.Parameters[0].Name; @@ -68,7 +71,7 @@ namespace ComputeSharp.SourceGenerators var source = CompilationUnit().AddAttributeLists( AttributeList(SingletonSeparatedList( - Attribute(IdentifierName("ComputeSharp.IComputeShaderSource")).AddArgumentListArguments( + Attribute(IdentifierName(typeof(IComputeShaderSourceAttribute).FullName)).AddArgumentListArguments( AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(structFullName))), AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(methodDeclarationSymbol.Name))), AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(processedMethodSource)))))) @@ -77,7 +80,7 @@ namespace ComputeSharp.SourceGenerators .NormalizeWhitespace() .ToFullString(); - var generatedFileName = $"__ComputeSharp_IComputeShaderSourceAttribute_{structDeclarationSymbol.Name}_{methodDeclarationSymbol.Name}"; + var generatedFileName = $"__ComputeSharp_{nameof(IComputeShaderSourceAttribute)}_{structDeclarationSymbol.Name}_{methodDeclarationSymbol.Name}"; // Add the method source attribute context.AddSource(generatedFileName, SourceText.From(source, Encoding.UTF8)); @@ -191,7 +194,7 @@ namespace ComputeSharp.SourceGenerators if (node.IsKind(SyntaxKind.SimpleMemberAccessExpression) && this.semanticModel.GetSymbolInfo(node).Symbol is ISymbol nodeSymbol && - nodeSymbol.ContainingType.ToDisplayString() == "ComputeSharp.ThreadIds") + nodeSymbol.ContainingType.ToDisplayString() == typeof(ThreadIds).FullName) { // When accessing ThreadIds members, they are in lowercase in HLSL. This is // because the type is mapped to uint3, which has the xyz members.