From 1f694466e67a5cc3f43c0550e9debd0c1498fa6e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 4 Sep 2019 10:03:39 +0200 Subject: [PATCH] Minor code refactoring --- .../Translation/MethodDecompiler.cs | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs index b96e6d2e..5a815988 100644 --- a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs @@ -107,6 +107,30 @@ namespace ComputeSharp.Shaders.Translation return new CSharpDecompiler(assemblyPath, resolver, decompilerSettings); } + /// + /// Decompiles a method or the whole declaring type + /// + /// The target to decompile + /// Specifies whether or not to force the decompilation of just the given method, even if not static + /// The decompiled source code + [Pure] + private string DecompileMethodOrDeclaringType(MethodInfo methodInfo, bool methodOnly = false) + { + // Get the handle of the containing type method + string assemblyPath = methodInfo.DeclaringType?.Assembly.Location ?? throw new InvalidOperationException(); + int metadataToken = methodInfo.IsStatic || methodOnly ? methodInfo.MetadataToken : methodInfo.DeclaringType.MetadataToken; + EntityHandle typeHandle = MetadataTokenHelpers.TryAsEntityHandle(metadataToken) ?? throw new InvalidOperationException(); + + // Get or create a decompiler for the target assembly, and decompile the type + if (!Decompilers.TryGetValue(assemblyPath, out CSharpDecompiler decompiler)) + { + decompiler = CreateDecompiler(assemblyPath); + Decompilers.Add(assemblyPath, decompiler); + } + + return decompiler.DecompileAsString(typeHandle); + } + /// /// A used to preprocess the closure type declarations for both lambda expressions and local methods /// @@ -127,29 +151,17 @@ namespace ComputeSharp.Shaders.Translation { lock (Lock) { - // Get the handle of the containing type method - string assemblyPath = methodInfo.DeclaringType?.Assembly.Location ?? throw new InvalidOperationException(); - EntityHandle typeHandle = MetadataTokenHelpers.TryAsEntityHandle(methodInfo.DeclaringType.MetadataToken) ?? throw new InvalidOperationException(); - - // Get or create a decompiler for the target assembly, and decompile the type - if (!Decompilers.TryGetValue(assemblyPath, out CSharpDecompiler decompiler)) - { - decompiler = CreateDecompiler(assemblyPath); - Decompilers.Add(assemblyPath, decompiler); - } - // Decompile the method source and fix the method declaration for local methods converted to lambdas string - sourceCode = decompiler.DecompileAsString(typeHandle), + 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")) { - EntityHandle methodHandle = MetadataTokenHelpers.TryAsEntityHandle(methodInfo.MetadataToken) ?? throw new InvalidOperationException(); string - methodOnlySourceCode = decompiler.DecompileAsString(methodHandle), + methodOnlySourceCode = DecompileMethodOrDeclaringType(methodInfo, true), methodOnlyFixedSourceCode = LambdaMethodDeclarationRegex.Replace(methodOnlySourceCode, m => $"// {m.Value}{Environment.NewLine} internal void Main"), methodOnlyIndentedSourceCode = $" {methodOnlyFixedSourceCode.Replace(Environment.NewLine, $"{Environment.NewLine} ")}";