You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.8 KiB

  1. // Copyright (c) 2017 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System.Linq;
  19. using System.Reflection.Metadata;
  20. using ICSharpCode.Decompiler.TypeSystem;
  21. namespace ICSharpCode.Decompiler.IL.Transforms
  22. {
  23. class ProxyCallReplacer : IILTransform
  24. {
  25. public void Run(ILFunction function, ILTransformContext context)
  26. {
  27. if (!context.Settings.AsyncAwait)
  28. return;
  29. foreach (var inst in function.Descendants.OfType<CallInstruction>())
  30. {
  31. Run(inst, context);
  32. }
  33. }
  34. void Run(CallInstruction inst, ILTransformContext context)
  35. {
  36. if (inst.Method.IsStatic)
  37. return;
  38. if (inst.Method.MetadataToken.IsNil || inst.Method.MetadataToken.Kind != HandleKind.MethodDefinition)
  39. return;
  40. var handle = (MethodDefinitionHandle)inst.Method.MetadataToken;
  41. if (!IsDefinedInCurrentOrOuterClass(inst.Method, context.Function.Method.DeclaringTypeDefinition))
  42. return;
  43. if (!inst.Method.IsCompilerGeneratedOrIsInCompilerGeneratedClass())
  44. return;
  45. var metadata = context.PEFile.Metadata;
  46. MethodDefinition methodDef = metadata.GetMethodDefinition((MethodDefinitionHandle)inst.Method.MetadataToken);
  47. if (!methodDef.HasBody())
  48. return;
  49. // Use the callee's generic context
  50. var genericContext = new GenericContext(inst.Method);
  51. // partially copied from CSharpDecompiler
  52. var ilReader = context.CreateILReader();
  53. var body = context.PEFile.GetMethodBody(methodDef.RelativeVirtualAddress);
  54. var proxyFunction = ilReader.ReadIL(handle, body, genericContext, ILFunctionKind.TopLevelFunction, context.CancellationToken);
  55. var transformContext = new ILTransformContext(context, proxyFunction);
  56. proxyFunction.RunTransforms(CSharp.CSharpDecompiler.EarlyILTransforms(), transformContext);
  57. if (!(proxyFunction.Body is BlockContainer blockContainer))
  58. return;
  59. if (blockContainer.Blocks.Count != 1)
  60. return;
  61. var block = blockContainer.Blocks[0];
  62. Call call;
  63. ILInstruction returnValue;
  64. switch (block.Instructions.Count)
  65. {
  66. case 1:
  67. // leave IL_0000 (call Test(ldloc this, ldloc A_1))
  68. if (!block.Instructions[0].MatchLeave(blockContainer, out returnValue))
  69. return;
  70. call = returnValue as Call;
  71. break;
  72. case 2:
  73. // call Test(ldloc this, ldloc A_1)
  74. // leave IL_0000(nop)
  75. call = block.Instructions[0] as Call;
  76. if (!block.Instructions[1].MatchLeave(blockContainer, out returnValue))
  77. return;
  78. if (!returnValue.MatchNop())
  79. return;
  80. break;
  81. default:
  82. return;
  83. }
  84. if (call == null || call.Method.IsConstructor)
  85. {
  86. return;
  87. }
  88. if (call.Method.IsStatic || call.Method.Parameters.Count != inst.Method.Parameters.Count)
  89. {
  90. return;
  91. }
  92. // check if original arguments are only correct ldloc calls
  93. for (int i = 0; i < call.Arguments.Count; i++)
  94. {
  95. var originalArg = call.Arguments[i];
  96. if (!originalArg.MatchLdLoc(out ILVariable var) ||
  97. var.Kind != VariableKind.Parameter ||
  98. var.Index != i - 1)
  99. {
  100. return;
  101. }
  102. }
  103. context.Step("Replace proxy: " + inst.Method.Name + " with " + call.Method.Name, inst);
  104. // Apply the wrapper call's substitution to the actual method call.
  105. Call newInst = new Call(call.Method.Specialize(inst.Method.Substitution));
  106. // copy flags
  107. newInst.ConstrainedTo = call.ConstrainedTo;
  108. newInst.ILStackWasEmpty = inst.ILStackWasEmpty;
  109. newInst.IsTail = call.IsTail & inst.IsTail;
  110. // copy IL ranges
  111. newInst.AddILRange(inst);
  112. newInst.Arguments.ReplaceList(inst.Arguments);
  113. inst.ReplaceWith(newInst);
  114. }
  115. static bool IsDefinedInCurrentOrOuterClass(IMethod method, ITypeDefinition declaringTypeDefinition)
  116. {
  117. while (declaringTypeDefinition != null)
  118. {
  119. if (method.DeclaringTypeDefinition == declaringTypeDefinition)
  120. return true;
  121. declaringTypeDefinition = declaringTypeDefinition.DeclaringTypeDefinition;
  122. }
  123. return false;
  124. }
  125. }
  126. }