/ILSpy/TreeNodes/Analyzer/Helpers.cs

http://github.com/icsharpcode/ILSpy · C# · 144 lines · 103 code · 17 blank · 24 comment · 42 complexity · 4d60004a9c4ef0b3bc05d1d7b13a3fa2 MD5 · raw file

  1. // Copyright (c) 2011 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;
  19. using System.Linq;
  20. using ICSharpCode.Decompiler;
  21. using Mono.Cecil;
  22. using Mono.Cecil.Cil;
  23. namespace ICSharpCode.ILSpy.TreeNodes.Analyzer
  24. {
  25. internal static class Helpers
  26. {
  27. public static bool IsReferencedBy(TypeDefinition type, TypeReference typeRef)
  28. {
  29. // TODO: move it to a better place after adding support for more cases.
  30. if (type == null)
  31. throw new ArgumentNullException("type");
  32. if (typeRef == null)
  33. throw new ArgumentNullException("typeRef");
  34. if (type == typeRef)
  35. return true;
  36. if (type.Name != typeRef.Name)
  37. return false;
  38. if (type.Namespace != typeRef.Namespace)
  39. return false;
  40. if (type.DeclaringType != null || typeRef.DeclaringType != null) {
  41. if (type.DeclaringType == null || typeRef.DeclaringType == null)
  42. return false;
  43. if (!IsReferencedBy(type.DeclaringType, typeRef.DeclaringType))
  44. return false;
  45. }
  46. return true;
  47. }
  48. public static MemberReference GetOriginalCodeLocation(MemberReference member)
  49. {
  50. if (member is MethodDefinition)
  51. return GetOriginalCodeLocation((MethodDefinition)member);
  52. return member;
  53. }
  54. public static MethodDefinition GetOriginalCodeLocation(MethodDefinition method)
  55. {
  56. if (method.IsCompilerGenerated()) {
  57. return FindMethodUsageInType(method.DeclaringType, method) ?? method;
  58. }
  59. var typeUsage = GetOriginalCodeLocation(method.DeclaringType);
  60. return typeUsage ?? method;
  61. }
  62. /// <summary>
  63. /// Given a compiler-generated type, returns the method where that type is used.
  64. /// Used to detect the 'parent method' for a lambda/iterator/async state machine.
  65. /// </summary>
  66. public static MethodDefinition GetOriginalCodeLocation(TypeDefinition type)
  67. {
  68. if (type != null && type.DeclaringType != null && type.IsCompilerGenerated()) {
  69. if (type.IsValueType) {
  70. // Value types might not have any constructor; but they must be stored in a local var
  71. // because 'initobj' (or 'call .ctor') expects a managed ref.
  72. return FindVariableOfTypeUsageInType(type.DeclaringType, type);
  73. } else {
  74. MethodDefinition constructor = GetTypeConstructor(type);
  75. if (constructor == null)
  76. return null;
  77. return FindMethodUsageInType(type.DeclaringType, constructor);
  78. }
  79. }
  80. return null;
  81. }
  82. private static MethodDefinition GetTypeConstructor(TypeDefinition type)
  83. {
  84. return type.Methods.FirstOrDefault(method => method.Name == ".ctor");
  85. }
  86. private static MethodDefinition FindMethodUsageInType(TypeDefinition type, MethodDefinition analyzedMethod)
  87. {
  88. string name = analyzedMethod.Name;
  89. foreach (MethodDefinition method in type.Methods) {
  90. bool found = false;
  91. if (!method.HasBody)
  92. continue;
  93. foreach (Instruction instr in method.Body.Instructions) {
  94. MethodReference mr = instr.Operand as MethodReference;
  95. if (mr != null && mr.Name == name &&
  96. IsReferencedBy(analyzedMethod.DeclaringType, mr.DeclaringType) &&
  97. mr.Resolve() == analyzedMethod) {
  98. found = true;
  99. break;
  100. }
  101. }
  102. method.Body = null;
  103. if (found)
  104. return method;
  105. }
  106. return null;
  107. }
  108. private static MethodDefinition FindVariableOfTypeUsageInType(TypeDefinition type, TypeDefinition variableType)
  109. {
  110. foreach (MethodDefinition method in type.Methods) {
  111. bool found = false;
  112. if (!method.HasBody)
  113. continue;
  114. foreach (var v in method.Body.Variables) {
  115. if (v.VariableType.ResolveWithinSameModule() == variableType) {
  116. found = true;
  117. break;
  118. }
  119. }
  120. method.Body = null;
  121. if (found)
  122. return method;
  123. }
  124. return null;
  125. }
  126. }
  127. }