diff --git a/src/ComputeSharp.Shaders/Translation/Enums/MethodType.cs b/src/ComputeSharp.Shaders/Translation/Enums/MethodType.cs new file mode 100644 index 00000000..7df4f830 --- /dev/null +++ b/src/ComputeSharp.Shaders/Translation/Enums/MethodType.cs @@ -0,0 +1,18 @@ +namespace ComputeSharp.Shaders.Translation.Enums +{ + /// + /// An that indicates a type of method to decompile + /// + internal enum MethodType + { + /// + /// An instance method that belongs to a closure class + /// + Closure, + + /// + /// A standalone, static method + /// + Static + } +} diff --git a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs index 5a815988..2c1ea26a 100644 --- a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs @@ -9,6 +9,7 @@ using System.Reflection.Metadata; using System.Text; using System.Text.RegularExpressions; using ComputeSharp.Shaders.Mappings; +using ComputeSharp.Shaders.Translation.Enums; using ICSharpCode.Decompiler; using ICSharpCode.Decompiler.CSharp; using ICSharpCode.Decompiler.Metadata; @@ -131,55 +132,26 @@ namespace ComputeSharp.Shaders.Translation return decompiler.DecompileAsString(typeHandle); } - /// - /// A used to preprocess the closure type declarations for both lambda expressions and local methods - /// - private static readonly Regex ClosureTypeDeclarationRegex = new Regex(@"(?<=private sealed class )<\w*>[\w_]+", RegexOptions.Compiled); - - /// - /// A used to preprocess the entry point declaration for both lambda expressions and local methods - /// - private static readonly Regex LambdaMethodDeclarationRegex = new Regex(@"(?:private|internal) void <\w+>[\w_|]+(?=\()", RegexOptions.Compiled); - /// /// Decompiles a target method and returns its and info /// /// The input to inspect + /// The type of method to decompile /// The root node for the syntax tree of the input method /// The semantic model for the input method - public void GetSyntaxTree(MethodInfo methodInfo, out MethodDeclarationSyntax rootNode, out SemanticModel semanticModel) + public void GetSyntaxTree(MethodInfo methodInfo, MethodType methodType, out MethodDeclarationSyntax rootNode, out SemanticModel semanticModel) { lock (Lock) { - // Decompile the method source and fix the method declaration for local methods converted to lambdas - string - sourceCode = DecompileMethodOrDeclaringType(methodInfo), - typeFixedCode = ClosureTypeDeclarationRegex.Replace(sourceCode, "Shader"), - methodFixedCode = LambdaMethodDeclarationRegex.Replace(typeFixedCode, m => $"// {m.Value}{Environment.NewLine} internal void Main"); - - // Workaround for some local methods not being decompiled correctly - if (!methodFixedCode.Contains("internal void Main")) + string sourceCode = methodType switch { - string - methodOnlySourceCode = DecompileMethodOrDeclaringType(methodInfo, true), - methodOnlyFixedSourceCode = LambdaMethodDeclarationRegex.Replace(methodOnlySourceCode, m => $"// {m.Value}{Environment.NewLine} internal void Main"), - methodOnlyIndentedSourceCode = $" {methodOnlyFixedSourceCode.Replace(Environment.NewLine, $"{Environment.NewLine} ")}"; - - int lastClosedBracketsIndex = methodFixedCode.LastIndexOf('}'); - methodFixedCode = methodFixedCode.Insert(lastClosedBracketsIndex, methodOnlyIndentedSourceCode); - } - - // Unwrap the nested fields - string unwrappedSourceCode = UnwrapSyntaxTree(methodFixedCode); - - // Remove the in keyword from the source - string inFixedSourceCode = Regex.Replace(unwrappedSourceCode, @"(? GetSyntaxTreeForClosureMethod(methodInfo), + MethodType.Static => GetSyntaxTreeForStaticMethod(methodInfo), + _ => throw new ArgumentOutOfRangeException(nameof(methodType), $"Invalid method type: {methodType}") + }; // Load the type syntax tree - SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(outFixedSourceCode); + SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(sourceCode); // Get the root node to return rootNode = syntaxTree.GetRoot().DescendantNodes().OfType().First(node => node.GetLeadingTrivia().ToFullString().Contains(methodInfo.Name)); @@ -190,6 +162,68 @@ namespace ComputeSharp.Shaders.Translation } } + /// + /// Decompiles a target closure method + /// + /// The input to inspect + [Pure] + private string GetSyntaxTreeForClosureMethod(MethodInfo methodInfo) + { + // Decompile the method source and fix the method declaration for local methods converted to lambdas + string + sourceCode = DecompileMethodOrDeclaringType(methodInfo), + typeFixedCode = ClosureTypeDeclarationRegex.Replace(sourceCode, "Shader"), + methodFixedCode = LambdaMethodDeclarationRegex.Replace(typeFixedCode, m => $"// {m.Value}{Environment.NewLine} internal void Main"); + + // Workaround for some local methods not being decompiled correctly + if (!methodFixedCode.Contains("internal void Main")) + { + string + methodOnlySourceCode = DecompileMethodOrDeclaringType(methodInfo, true), + methodOnlyFixedSourceCode = LambdaMethodDeclarationRegex.Replace(methodOnlySourceCode, m => $"// {m.Value}{Environment.NewLine} internal void Main"), + methodOnlyIndentedSourceCode = $" {methodOnlyFixedSourceCode.Replace(Environment.NewLine, $"{Environment.NewLine} ")}"; + + int lastClosedBracketsIndex = methodFixedCode.LastIndexOf('}'); + methodFixedCode = methodFixedCode.Insert(lastClosedBracketsIndex, methodOnlyIndentedSourceCode); + } + + // Unwrap the nested fields + string unwrappedSourceCode = UnwrapSyntaxTree(methodFixedCode); + + // Remove the in keyword from the source + string inFixedSourceCode = Regex.Replace(unwrappedSourceCode, @"(? + /// Decompiles a target method and returns its and info + /// + /// The input to inspect + [Pure] + private string GetSyntaxTreeForStaticMethod(MethodInfo methodInfo) + { + string + sourceCode = DecompileMethodOrDeclaringType(methodInfo), + inFixedSourceCode = Regex.Replace(sourceCode, @"(? + /// A used to preprocess the closure type declarations for both lambda expressions and local methods + /// + private static readonly Regex ClosureTypeDeclarationRegex = new Regex(@"(?<=private sealed class )<\w*>[\w_]+", RegexOptions.Compiled); + + /// + /// A used to preprocess the entry point declaration for both lambda expressions and local methods + /// + private static readonly Regex LambdaMethodDeclarationRegex = new Regex(@"(?:private|internal) void <\w+>[\w_|]+(?=\()", RegexOptions.Compiled); + /// /// A used to find fields that represent nested closure types /// diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 599ade23..b4654506 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -8,6 +8,7 @@ using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Shaders.Mappings; using ComputeSharp.Shaders.Renderer.Models.Fields; using ComputeSharp.Shaders.Renderer.Models.Fields.Abstract; +using ComputeSharp.Shaders.Translation.Enums; using ComputeSharp.Shaders.Translation.Models; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -243,7 +244,7 @@ namespace ComputeSharp.Shaders.Translation private void LoadMethodSource() { // Decompile the shader method - MethodDecompiler.Instance.GetSyntaxTree(Action.Method, out MethodDeclarationSyntax root, out SemanticModel semanticModel); + 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);