From 1f9c735bd9d787ac34bfa21f6bf2866048cc8b9d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 23 Aug 2019 17:10:19 +0200 Subject: [PATCH] Implemented field declaration unwrapping in source code --- .../Translation/MethodDecompiler.cs | 61 +++++++++++-------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs index 4901365a..cd2e083f 100644 --- a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs @@ -6,6 +6,7 @@ using System.IO; using System.Linq; using System.Reflection; using System.Reflection.Metadata; +using System.Text; using System.Text.RegularExpressions; using ICSharpCode.Decompiler; using ICSharpCode.Decompiler.CSharp; @@ -198,40 +199,50 @@ namespace ComputeSharp.Shaders.Translation List parsedFields = new List(); // Local function to recursively extract all the nested fields - void ParseSyntaxTree(string source) + void ParseNestedFields(string source) { // Find the field declarations - MatchCollection matches = NestedClosureFieldRegex.Matches(source); - if (matches.Count == 0) return; + Match match = NestedClosureFieldRegex.Match(source); + if (!match.Success) return; - foreach (Match match in matches) - { - // Load the nested closure type - string fullname = match.Groups[1].Value.Replace(".<>", "+<>"); // Fullname of a nested type - Type type = AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetType(fullname)).First(t => t != null); + // Load the nested closure type + string fullname = match.Groups[1].Value.Replace(".<>", "+<>"); // Fullname of a nested type + Type type = AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetType(fullname)).First(t => t != null); - // Decompile the type and get a readable source code - EntityHandle typeHandle = MetadataTokenHelpers.TryAsEntityHandle(type.MetadataToken) ?? throw new InvalidOperationException(); - CSharpDecompiler decompiler = Decompilers[type.Assembly.Location]; - string - sourceCode = decompiler.DecompileAsString(typeHandle), - typeFixedCode = ClosureTypeDeclarationRegex.Replace(sourceCode, "Scope"); + // Decompile the type and get a readable source code + EntityHandle typeHandle = MetadataTokenHelpers.TryAsEntityHandle(type.MetadataToken) ?? throw new InvalidOperationException(); + CSharpDecompiler decompiler = Decompilers[type.Assembly.Location]; + string + sourceCode = decompiler.DecompileAsString(typeHandle), + typeFixedCode = ClosureTypeDeclarationRegex.Replace(sourceCode, "Scope"); - // Load the syntax tree and retrieve the list of fields - SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(typeFixedCode); - FieldDeclarationSyntax[] fields = syntaxTree.GetRoot().DescendantNodes().OfType().First().ChildNodes().OfType().ToArray(); + // Load the syntax tree and retrieve the list of fields + SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(typeFixedCode); + FieldDeclarationSyntax[] fields = syntaxTree.GetRoot().DescendantNodes().OfType().First().ChildNodes().OfType().ToArray(); - // Add the captured fields - foreach (FieldDeclarationSyntax field in fields) - if (!field.Declaration.Variables.ToFullString().StartsWith("CS$<>")) - parsedFields.Add(field); + // Add the captured fields + foreach (FieldDeclarationSyntax field in fields) + if (!field.Declaration.Variables.ToFullString().StartsWith("CS$<>")) + parsedFields.Add(field); - // Explore the new decompiled type - ParseSyntaxTree(typeFixedCode); - } + // Explore the new decompiled type + ParseNestedFields(typeFixedCode); } - ParseSyntaxTree(source); + ParseNestedFields(source); + + if (parsedFields.Count > 0) + { + // Build the aggregated string with all the captured fields + StringBuilder builder = new StringBuilder(); + foreach (FieldDeclarationSyntax field in parsedFields) + builder.AppendLine(field.ToFullString()); + string fields = builder.ToString().TrimEnd(' ', '\r', '\n'); + + Match match = NestedClosureFieldRegex.Match(source); + source = source.Remove(match.Index - 11, match.Length + 12); + source = source.Insert(match.Index - 11, fields); + } return source; }