From c8c4847be92a2e6fb817d02abd1992221000428b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 19 Aug 2019 10:47:26 +0200 Subject: [PATCH] Added support for skipping implicitly captured variables --- src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs | 8 ++++---- src/ComputeSharp.Shaders/Translation/ShaderLoader.cs | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs index 2fdc88b4..ae59e697 100644 --- a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs @@ -107,7 +107,7 @@ namespace ComputeSharp.Shaders.Translation /// /// 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); + private static readonly Regex LambdaMethodDeclarationRegex = new Regex(@"(?:private|internal) void <\w+>[\w_|]+(?=\()", RegexOptions.Compiled); /// /// Decompiles a target method and returns its and info @@ -134,7 +134,7 @@ namespace ComputeSharp.Shaders.Translation string sourceCode = decompiler.DecompileAsString(typeHandle), typeFixedCode = ClosureTypeDeclarationRegex.Replace(sourceCode, "Shader"), - methodFixedCode = LambdaMethodDeclarationRegex.Replace(typeFixedCode, "internal void Main"); + 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")) @@ -142,7 +142,7 @@ namespace ComputeSharp.Shaders.Translation EntityHandle methodHandle = MetadataTokenHelpers.TryAsEntityHandle(methodInfo.MetadataToken) ?? throw new InvalidOperationException(); string methodOnlySourceCode = decompiler.DecompileAsString(methodHandle), - methodOnlyFixedSourceCode = LambdaMethodDeclarationRegex.Replace(methodOnlySourceCode, "internal void Main"), + methodOnlyFixedSourceCode = LambdaMethodDeclarationRegex.Replace(methodOnlySourceCode, m => $"// {m.Value}{Environment.NewLine} internal void Main"), methodOnlyIndentedSourceCode = $" {methodOnlyFixedSourceCode.Replace(Environment.NewLine, $"{Environment.NewLine} ")}"; int lastClosedBracketsIndex = methodFixedCode.LastIndexOf('}'); @@ -153,7 +153,7 @@ namespace ComputeSharp.Shaders.Translation SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(methodFixedCode); // Get the root node to return - rootNode = syntaxTree.GetRoot().DescendantNodes().OfType().First(); + rootNode = syntaxTree.GetRoot().DescendantNodes().OfType().First(node => node.GetLeadingTrivia().ToFullString().Contains(methodInfo.Name)); // Update the incremental compilation and retrieve the syntax tree for the method _Compilation = _Compilation.AddSyntaxTrees(syntaxTree); diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 5398fff1..c64254e7 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -199,7 +199,6 @@ namespace ComputeSharp.Shaders.Translation string typeName = HlslKnownTypes.GetMappedName(fieldType); _FieldsList.Add(new CapturedFieldInfo(fieldType, typeName, fieldName)); } - else throw new NotSupportedException($"Unsupported field of type {fieldType.FullName}"); } ///