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.

99 lines
3.5 KiB

  1. // Copyright (c) 2018 Daniel Grunwald
  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;
  19. using System.Collections.Generic;
  20. using System.Diagnostics;
  21. using System.Text;
  22. using ICSharpCode.Decompiler.TypeSystem;
  23. namespace ICSharpCode.Decompiler.IL
  24. {
  25. class TupleTransform
  26. {
  27. /// <summary>
  28. /// Matches an 'ldflda' instruction accessing a tuple element.
  29. ///
  30. /// E.g. matches:
  31. /// <c>ldflda Item1(ldflda Rest(target))</c>
  32. /// </summary>
  33. public static bool MatchTupleFieldAccess(LdFlda inst, out IType tupleType, out ILInstruction target, out int position)
  34. {
  35. tupleType = inst.Field.DeclaringType;
  36. target = inst.Target;
  37. if (!inst.Field.Name.StartsWith("Item", StringComparison.Ordinal))
  38. {
  39. position = 0;
  40. return false;
  41. }
  42. if (!int.TryParse(inst.Field.Name.Substring(4), out position))
  43. return false;
  44. if (!TupleType.IsTupleCompatible(tupleType, out _))
  45. return false;
  46. while (target is LdFlda ldflda && ldflda.Field.Name == "Rest" && TupleType.IsTupleCompatible(ldflda.Field.DeclaringType, out _))
  47. {
  48. tupleType = ldflda.Field.DeclaringType;
  49. target = ldflda.Target;
  50. position += TupleType.RestPosition - 1;
  51. }
  52. return true;
  53. }
  54. /// <summary>
  55. /// Matches 'newobj TupleType(...)'.
  56. /// Takes care of flattening long tuples.
  57. /// </summary>
  58. public static bool MatchTupleConstruction(NewObj newobj, out ILInstruction[] arguments)
  59. {
  60. arguments = null;
  61. if (newobj == null)
  62. return false;
  63. if (!TupleType.IsTupleCompatible(newobj.Method.DeclaringType, out int elementCount))
  64. return false;
  65. arguments = new ILInstruction[elementCount];
  66. int outIndex = 0;
  67. while (elementCount >= TupleType.RestPosition)
  68. {
  69. if (newobj.Arguments.Count != TupleType.RestPosition)
  70. return false;
  71. for (int pos = 1; pos < TupleType.RestPosition; pos++)
  72. {
  73. arguments[outIndex++] = newobj.Arguments[pos - 1];
  74. }
  75. elementCount -= TupleType.RestPosition - 1;
  76. Debug.Assert(outIndex + elementCount == arguments.Length);
  77. newobj = newobj.Arguments.Last() as NewObj;
  78. if (newobj == null)
  79. return false;
  80. if (!TupleType.IsTupleCompatible(newobj.Method.DeclaringType, out int restElementCount))
  81. return false;
  82. if (restElementCount != elementCount)
  83. return false;
  84. }
  85. Debug.Assert(outIndex + elementCount == arguments.Length);
  86. if (newobj.Arguments.Count != elementCount)
  87. return false;
  88. for (int i = 0; i < elementCount; i++)
  89. {
  90. arguments[outIndex++] = newobj.Arguments[i];
  91. }
  92. return true;
  93. }
  94. }
  95. }