MethodBody.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // MethodBody.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Threading;
  30. using Mono.Collections.Generic;
  31. namespace Mono.Cecil.Cil
  32. {
  33. public sealed class MethodBody : IVariableDefinitionProvider
  34. {
  35. readonly internal MethodDefinition method;
  36. internal ParameterDefinition this_parameter;
  37. internal int max_stack_size;
  38. internal int code_size;
  39. internal bool init_locals;
  40. internal MetadataToken local_var_token;
  41. internal Collection<Instruction> instructions;
  42. internal Collection<ExceptionHandler> exceptions;
  43. internal Collection<VariableDefinition> variables;
  44. Scope scope;
  45. public MethodDefinition Method
  46. {
  47. get { return method; }
  48. }
  49. public int MaxStackSize
  50. {
  51. get { return max_stack_size; }
  52. set { max_stack_size = value; }
  53. }
  54. public int CodeSize
  55. {
  56. get { return code_size; }
  57. }
  58. public bool InitLocals
  59. {
  60. get { return init_locals; }
  61. set { init_locals = value; }
  62. }
  63. public MetadataToken LocalVarToken
  64. {
  65. get { return local_var_token; }
  66. set { local_var_token = value; }
  67. }
  68. public Collection<Instruction> Instructions
  69. {
  70. get { return instructions ?? (instructions = new InstructionCollection()); }
  71. }
  72. public bool HasExceptionHandlers
  73. {
  74. get { return !Mixin.IsNullOrEmpty(exceptions); }
  75. }
  76. public Collection<ExceptionHandler> ExceptionHandlers
  77. {
  78. get { return exceptions ?? (exceptions = new Collection<ExceptionHandler>()); }
  79. }
  80. public bool HasVariables
  81. {
  82. get { return !Mixin.IsNullOrEmpty(variables); }
  83. }
  84. public Collection<VariableDefinition> Variables
  85. {
  86. get { return variables ?? (variables = new VariableDefinitionCollection()); }
  87. }
  88. public Scope Scope
  89. {
  90. get { return scope; }
  91. set { scope = value; }
  92. }
  93. public ParameterDefinition ThisParameter
  94. {
  95. get
  96. {
  97. if (method == null || method.DeclaringType == null)
  98. throw new NotSupportedException();
  99. if (!method.HasThis)
  100. return null;
  101. if (this_parameter == null)
  102. this_parameter = ThisParameterFor(method);
  103. return this_parameter;
  104. }
  105. }
  106. static ParameterDefinition ThisParameterFor(MethodDefinition method)
  107. {
  108. var declaring_type = method.DeclaringType;
  109. var type = declaring_type.IsValueType || declaring_type.IsPrimitive
  110. ? new PointerType(declaring_type)
  111. : declaring_type as TypeReference;
  112. return new ParameterDefinition(type, method);
  113. }
  114. public MethodBody(MethodDefinition method)
  115. {
  116. this.method = method;
  117. }
  118. public ILProcessor GetILProcessor()
  119. {
  120. return new ILProcessor(this);
  121. }
  122. }
  123. public interface IVariableDefinitionProvider
  124. {
  125. bool HasVariables { get; }
  126. Collection<VariableDefinition> Variables { get; }
  127. }
  128. class VariableDefinitionCollection : Collection<VariableDefinition>
  129. {
  130. internal VariableDefinitionCollection()
  131. {
  132. }
  133. internal VariableDefinitionCollection(int capacity)
  134. : base(capacity)
  135. {
  136. }
  137. protected override void OnAdd(VariableDefinition item, int index)
  138. {
  139. item.index = index;
  140. }
  141. protected override void OnInsert(VariableDefinition item, int index)
  142. {
  143. item.index = index;
  144. for (int i = index; i < size; i++)
  145. items[i].index = i + 1;
  146. }
  147. protected override void OnSet(VariableDefinition item, int index)
  148. {
  149. item.index = index;
  150. }
  151. protected override void OnRemove(VariableDefinition item, int index)
  152. {
  153. item.index = -1;
  154. for (int i = index + 1; i < size; i++)
  155. items[i].index = i - 1;
  156. }
  157. }
  158. class InstructionCollection : Collection<Instruction>
  159. {
  160. internal InstructionCollection()
  161. {
  162. }
  163. internal InstructionCollection(int capacity)
  164. : base(capacity)
  165. {
  166. }
  167. protected override void OnAdd(Instruction item, int index)
  168. {
  169. if (index == 0)
  170. return;
  171. var previous = items[index - 1];
  172. previous.next = item;
  173. item.previous = previous;
  174. }
  175. protected override void OnInsert(Instruction item, int index)
  176. {
  177. if (size == 0)
  178. return;
  179. var current = items[index];
  180. if (current == null)
  181. {
  182. var last = items[index - 1];
  183. last.next = item;
  184. item.previous = last;
  185. return;
  186. }
  187. var previous = current.previous;
  188. if (previous != null)
  189. {
  190. previous.next = item;
  191. item.previous = previous;
  192. }
  193. current.previous = item;
  194. item.next = current;
  195. }
  196. protected override void OnSet(Instruction item, int index)
  197. {
  198. var current = items[index];
  199. item.previous = current.previous;
  200. item.next = current.next;
  201. current.previous = null;
  202. current.next = null;
  203. }
  204. protected override void OnRemove(Instruction item, int index)
  205. {
  206. var previous = item.previous;
  207. if (previous != null)
  208. previous.next = item.next;
  209. var next = item.next;
  210. if (next != null)
  211. next.previous = item.previous;
  212. item.previous = null;
  213. item.next = null;
  214. }
  215. }
  216. }