diff --git a/src/ComputeSharp.SourceGenerators/Extensions/INamedTypeSymbolExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/INamedTypeSymbolExtensions.cs index e9337e19..fb88fac2 100644 --- a/src/ComputeSharp.SourceGenerators/Extensions/INamedTypeSymbolExtensions.cs +++ b/src/ComputeSharp.SourceGenerators/Extensions/INamedTypeSymbolExtensions.cs @@ -21,7 +21,7 @@ namespace ComputeSharp.SourceGenerators.Extensions { return symbol switch { - INamespaceSymbol ns when ns.IsGlobalNamespace => builder.Insert(0, "global::"), + INamespaceSymbol ns when ns.IsGlobalNamespace => builder, INamespaceSymbol ns when ns.ContainingNamespace is { IsGlobalNamespace: false } => BuildFrom(ns.ContainingNamespace, builder.Insert(0, $".{ns.MetadataName}")), ITypeSymbol ts when ts.ContainingType is ISymbol pt => BuildFrom(pt, builder.Insert(0, $"+{ts.MetadataName}")), diff --git a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs index 24d9371a..e8eaef5a 100644 --- a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs +++ b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs @@ -11,6 +11,9 @@ namespace ComputeSharp.SourceGenerators.Extensions /// internal static class SyntaxNodeExtensions { + /// + /// A custom instance with fully qualified style, without global::. + /// private static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalFormat = new( globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Omitted, typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces, @@ -60,5 +63,21 @@ namespace ComputeSharp.SourceGenerators.Extensions return ParseTypeName(typeName); } + + /// + /// Returns a with a block body. + /// + /// The input node. + /// A node like the one in input, but always with a block body. + [Pure] + public static MethodDeclarationSyntax WithBlockBody(this MethodDeclarationSyntax node) + { + return node.WithBody((node.Body, node.ExpressionBody) switch + { + (BlockSyntax block, _) => block, + (_, ArrowExpressionClauseSyntax arrow) => Block(ExpressionStatement(arrow.Expression)), + _ => Block() + }); + } } } diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs index cc89734d..30b7527d 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs @@ -47,20 +47,11 @@ namespace ComputeSharp.SourceGenerators { IMethodSymbol methodDeclarationSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration)!; - // Extract the source from either a block or an expression body - var methodWithBlockBody = methodDeclaration.WithBody( - (methodDeclaration.Body, methodDeclaration.ExpressionBody) switch - { - (BlockSyntax block, _) => block, - (_, ArrowExpressionClauseSyntax arrow) => Block(ExpressionStatement(arrow.Expression)), - _ => Block() - }); - // Rewrite the method syntax tree - var processedMethod = new ShaderSourceRewriter(semanticModel) - .Visit(methodWithBlockBody) - .NormalizeWhitespace() - .ToFullString(); + var processedMethod = new ShaderSourceRewriter(semanticModel).Visit(methodDeclaration)!.WithoutTrivia(); + + // Produce the final method source + var processedMethodSource = processedMethod.NormalizeWhitespace().ToFullString(); // Create the compilation unit with the source attribute var source = @@ -69,7 +60,7 @@ namespace ComputeSharp.SourceGenerators Attribute(IdentifierName("ComputeSharp.IComputeShaderSource")).AddArgumentListArguments( AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(structFullName))), AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(methodDeclarationSymbol.Name))), - AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(processedMethod)))))) + AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(processedMethodSource)))))) .WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList())) .WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword)))) .NormalizeWhitespace() @@ -102,6 +93,13 @@ namespace ComputeSharp.SourceGenerators this.semanticModel = semanticModel; } + /// + public TNode? Visit(TNode? node) + where TNode : SyntaxNode + { + return (TNode?)base.Visit(node); + } + /// public override SyntaxNode VisitParameter(ParameterSyntax node) { @@ -168,6 +166,12 @@ namespace ComputeSharp.SourceGenerators return node; } + + /// + public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax node) + { + return ((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!).WithBlockBody(); + } } } }