From 734e9a09029c80297c7f1d6e33d13ce434247c51 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 23 Aug 2019 16:58:08 +0200 Subject: [PATCH] Added draft for the UnwrapSyntaxTree API --- .../Translation/MethodDecompiler.cs | 86 +++++++++++++++---- 1 file changed, 71 insertions(+), 15 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs index ae59e697..4901365a 100644 --- a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs @@ -99,6 +99,25 @@ namespace ComputeSharp.Shaders.Translation } } + /// + /// Creates a new instance targeting a given assembly + /// + /// The path of the assembly to decompile + /// A instance that can be used on the targeted assembly + [Pure] + private static CSharpDecompiler CreateDecompiler(string assemblyPath) + { + DecompilerSettings decompilerSettings = new DecompilerSettings(ICSharpCode.Decompiler.CSharp.LanguageVersion.Latest) + { + ObjectOrCollectionInitializers = false, + UsingDeclarations = false, + CSharpFormattingOptions = { IndentationString = IndentedTextWriter.DefaultTabString } + }; + + UniversalAssemblyResolver resolver = new UniversalAssemblyResolver(assemblyPath, false, "netstandard"); + return new CSharpDecompiler(assemblyPath, resolver, decompilerSettings); + } + /// /// A used to preprocess the closure type declarations for both lambda expressions and local methods /// @@ -149,8 +168,11 @@ namespace ComputeSharp.Shaders.Translation methodFixedCode = methodFixedCode.Insert(lastClosedBracketsIndex, methodOnlyIndentedSourceCode); } + // Unwrap the nested fields + string unwrappedCode = UnwrapSyntaxTree(methodFixedCode); + // Load the type syntax tree - SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(methodFixedCode); + SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(unwrappedCode); // Get the root node to return rootNode = syntaxTree.GetRoot().DescendantNodes().OfType().First(node => node.GetLeadingTrivia().ToFullString().Contains(methodInfo.Name)); @@ -162,22 +184,56 @@ namespace ComputeSharp.Shaders.Translation } /// - /// Creates a new instance targeting a given assembly + /// A used to find fields that represent nested closure types /// - /// The path of the assembly to decompile - /// A instance that can be used on the targeted assembly - [Pure] - private static CSharpDecompiler CreateDecompiler(string assemblyPath) - { - DecompilerSettings decompilerSettings = new DecompilerSettings(ICSharpCode.Decompiler.CSharp.LanguageVersion.Latest) - { - ObjectOrCollectionInitializers = false, - UsingDeclarations = false, - CSharpFormattingOptions = { IndentationString = IndentedTextWriter.DefaultTabString } - }; + private static readonly Regex NestedClosureFieldRegex = new Regex(@"(?<=public )((?:\w+\.)+<>[\w_]+) (CS\$<>[\w_]+)(?=;)", RegexOptions.Compiled); - UniversalAssemblyResolver resolver = new UniversalAssemblyResolver(assemblyPath, false, "netstandard"); - return new CSharpDecompiler(assemblyPath, resolver, decompilerSettings); + /// + /// Unwraps all the nested captured fields in the given source code, moving them to the top level + /// + /// The input source code to process + [Pure] + private string UnwrapSyntaxTree(string source) + { + List parsedFields = new List(); + + // Local function to recursively extract all the nested fields + void ParseSyntaxTree(string source) + { + // Find the field declarations + MatchCollection matches = NestedClosureFieldRegex.Matches(source); + if (matches.Count == 0) 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); + + // 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(); + + // 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); + } + } + + ParseSyntaxTree(source); + + return source; } } }