diff --git a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs
index c549a814..78307cef 100644
--- a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs
+++ b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Diagnostics.Contracts;
+using System.Linq;
using ComputeSharp.SourceGenerators.Mappings;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
@@ -144,5 +145,23 @@ namespace ComputeSharp.SourceGenerators.Extensions
return node;
}
+
+ ///
+ /// Returns a instance with no accessibility modifiers.
+ ///
+ /// The input node.
+ /// A node just like but with no accessibility modifiers.
+ [Pure]
+ public static MethodDeclarationSyntax WithoutAccessibilityModifiers(this MethodDeclarationSyntax node)
+ {
+ return node.WithModifiers(TokenList(node.Modifiers.Where(static modifier => modifier.Kind() switch
+ {
+ SyntaxKind.PublicKeyword => false,
+ SyntaxKind.PrivateKeyword => false,
+ SyntaxKind.ProtectedKeyword => false,
+ SyntaxKind.InternalKeyword => false,
+ _ => true
+ }).ToArray()));
+ }
}
}
diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
index 303f2e42..d127b6de 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
@@ -41,8 +41,9 @@ namespace ComputeSharp.SourceGenerators
// Only process compute shader types
if (!structDeclarationSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) continue;
- // The list to use to track all discovered custom types
+ // We need to sets to track all discovered custom types and static methods
HashSet discoveredTypes = new(SymbolEqualityComparer.Default);
+ Dictionary staticMethods = new(SymbolEqualityComparer.Default);
// Helper that converts a sequence of string sequences into a nested array expression.
// That is, this applies the following transformation:
@@ -72,15 +73,23 @@ namespace ComputeSharp.SourceGenerators
.AddExpressions(values.Select(static value => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(value))).ToArray()));
}
+ // Explore the syntax tree and extract the processed info
+ var processedMembers = GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray();
+ var processedMethods = GetProcessedMethods(structDeclaration, semanticModel, discoveredTypes, staticMethods).ToArray();
+ var processedTypes = GetProcessedTypes(discoveredTypes).ToArray();
+
+ // Merge the static methods as well
+ processedMethods = processedMethods.Concat(staticMethods.Select(foo => new[] { "", foo.Value.ToFullString() })).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(NestedArrayExpression(GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray())),
- AttributeArgument(NestedArrayExpression(GetProcessedMethods(structDeclaration, semanticModel, discoveredTypes).ToArray())),
- AttributeArgument(ArrayExpression(GetProcessedTypes(discoveredTypes))))))
+ AttributeArgument(NestedArrayExpression(processedMembers)),
+ AttributeArgument(NestedArrayExpression(processedMethods)),
+ AttributeArgument(ArrayExpression(processedTypes)))))
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
.NormalizeWhitespace()
@@ -123,13 +132,15 @@ namespace ComputeSharp.SourceGenerators
///
/// The instance for the current type.
/// The instance for the type to process.
- /// The collection of currently discovered types.
+ /// The collection of currently discovered types.
+ /// The set of discovered and processed static methods.
/// A sequence of processed methods in .
[Pure]
private static IEnumerable> GetProcessedMethods(
StructDeclarationSyntax structDeclaration,
SemanticModel semanticModel,
- ICollection types)
+ ICollection discoveredTypes,
+ IDictionary staticMethods)
{
// Find all declared methods in the type
ImmutableArray methodDeclarations = (
@@ -140,7 +151,7 @@ namespace ComputeSharp.SourceGenerators
foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations)
{
IMethodSymbol methodDeclarationSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration)!;
- ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, types);
+ ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods);
// Rewrite the method syntax tree
var processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia();
diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
index 62577adc..135745fc 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
@@ -26,6 +26,11 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
private readonly ICollection discoveredTypes;
+ ///
+ /// The collection of discovered static methods.
+ ///
+ private readonly IDictionary staticMethods;
+
///
/// The collection of processed local functions in the current tree.
///
@@ -41,10 +46,15 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
/// The instance for the target syntax tree.
/// The set of discovered custom types.
- public ShaderSourceRewriter(SemanticModel semanticModel, ICollection discoveredTypes)
+ /// The set of discovered and processed static methods.
+ public ShaderSourceRewriter(
+ SemanticModel semanticModel,
+ ICollection discoveredTypes,
+ IDictionary staticMethods)
{
this.semanticModel = semanticModel;
this.discoveredTypes = discoveredTypes;
+ this.staticMethods = staticMethods;
this.localFunctions = new();
}
@@ -149,7 +159,10 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax node)
{
- return ((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!).WithBlockBody();
+ return
+ ((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!)
+ .WithBlockBody()
+ .WithoutAccessibilityModifiers();
}
///
@@ -232,6 +245,22 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
return updatedNode.WithExpression(ParseExpression(functionIdentifier));
}
+
+ // If the method is an external static method, import and rewrite it as well.
+ // This assumes that the target method is actually part of the same compilation.
+ var methodIdentifier = method.GetFullMetadataName().Replace(".", "__");
+
+ if (!this.staticMethods.ContainsKey(method))
+ {
+ ShaderSourceRewriter shaderSourceRewriter = new(this.semanticModel, this.discoveredTypes, this.staticMethods);
+ MethodDeclarationSyntax
+ methodNode = (MethodDeclarationSyntax)method.DeclaringSyntaxReferences[0].GetSyntax(),
+ processedMethod = shaderSourceRewriter.Visit(methodNode)!.NormalizeWhitespace().WithoutTrivia();
+
+ this.staticMethods.Add(method, processedMethod.WithIdentifier(Identifier(methodIdentifier)));
+ }
+
+ return updatedNode.WithExpression(ParseExpression(methodIdentifier));
}
return updatedNode;