Added support for static methods in parent class
このコミットが含まれているのは:
1 親
1d708f4e69
コミット
1c7428f1ea
4ファイル変更
+70
-4
検索条件に一致するファイルはありません
@@ -168,6 +168,33 @@ namespace ComputeSharp.Shaders.Extensions
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks a <see cref="InvocationExpressionSyntax"/> instance and replaces it to be HLSL compatible, if needed
|
||||
/// </summary>
|
||||
/// <param name="node">The input <see cref="InvocationExpressionSyntax"/> to check and modify if needed</param>
|
||||
/// <param name="method">The info on parsed static methods, if any</param>
|
||||
/// <returns>A <see cref="SyntaxNode"/> instance that is compatible with HLSL</returns>
|
||||
[Pure]
|
||||
public static InvocationExpressionSyntax ReplaceInvocation(this InvocationExpressionSyntax node, Type declaringType, out (string Name, MethodInfo MethodInfo)? method)
|
||||
{
|
||||
// Set the method to null
|
||||
method = null;
|
||||
|
||||
string cSharpName = node.Expression.ToString();
|
||||
while ((declaringType = declaringType.DeclaringType) != null)
|
||||
{
|
||||
MethodInfo methodInfo = declaringType.GetMethod(cSharpName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
if (methodInfo == null) return node;
|
||||
|
||||
string name = $"{methodInfo.DeclaringType.Name}_{methodInfo.Name}";
|
||||
method = (name, methodInfo);
|
||||
|
||||
return SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression(name), node.ArgumentList).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Processes a loaded static member, either a field or a property
|
||||
/// </summary>
|
||||
|
||||
@@ -299,7 +299,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
MethodDecompiler.Instance.GetSyntaxTree(Action.Method, MethodType.Closure, out MethodDeclarationSyntax root, out SemanticModel semanticModel);
|
||||
|
||||
// Rewrite the shader method (eg. to fix the type declarations)
|
||||
ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel);
|
||||
ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel, ShaderType);
|
||||
root = (MethodDeclarationSyntax)syntaxRewriter.Visit(root);
|
||||
|
||||
// Extract the implicit local functions
|
||||
@@ -340,7 +340,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
MethodDecompiler.Instance.GetSyntaxTree(methodInfo, MethodType.Static, out MethodDeclarationSyntax root, out SemanticModel semanticModel);
|
||||
|
||||
// Rewrite the method
|
||||
ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel);
|
||||
ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel, methodInfo.DeclaringType);
|
||||
root = (MethodDeclarationSyntax)syntaxRewriter.Visit(root);
|
||||
|
||||
// Register the captured static members
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using ComputeSharp.Shaders.Extensions;
|
||||
using ComputeSharp.Shaders.Translation.Models;
|
||||
@@ -18,11 +19,20 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// </summary>
|
||||
private readonly SemanticModel SemanticModel;
|
||||
|
||||
/// <summary>
|
||||
/// The declaring type that hosts the root from which the current syntax tree is inspected
|
||||
/// </summary>
|
||||
private readonly Type DeclaringType;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ShaderSyntaxRewriter"/> instance with the specified parameters
|
||||
/// </summary>
|
||||
/// <param name="semanticModel"></param>
|
||||
public ShaderSyntaxRewriter(SemanticModel semanticModel) => SemanticModel = semanticModel;
|
||||
public ShaderSyntaxRewriter(SemanticModel semanticModel, Type declaringType)
|
||||
{
|
||||
SemanticModel = semanticModel;
|
||||
DeclaringType = declaringType;
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, ReadableMember> _StaticMembers = new Dictionary<string, ReadableMember>();
|
||||
|
||||
@@ -119,6 +129,21 @@ namespace ComputeSharp.Shaders.Translation
|
||||
return syntaxNode;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node)
|
||||
{
|
||||
node = (InvocationExpressionSyntax)base.VisitInvocationExpression(node);
|
||||
node = node.ReplaceInvocation(DeclaringType, out var method);
|
||||
|
||||
// Register the captured member, if present
|
||||
if (method.HasValue && !_StaticMethods.ContainsKey(method.Value.Name))
|
||||
{
|
||||
_StaticMethods.Add(method.Value.Name, method.Value.MethodInfo);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node)
|
||||
{
|
||||
|
||||
@@ -135,5 +135,19 @@ namespace ComputeSharp.Tests
|
||||
|
||||
Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f);
|
||||
}
|
||||
|
||||
public static float Square(float x) => x * x;
|
||||
|
||||
[TestMethod]
|
||||
public void StaticMethodInSameClass()
|
||||
{
|
||||
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1);
|
||||
|
||||
Gpu.Default.For(1, id => buffer[0] = Square(3));
|
||||
|
||||
float[] result = buffer.GetData();
|
||||
|
||||
Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f);
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする