Method.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Method.cs
  2. // (C) Sergey Chaban ([email protected])
  3. using System;
  4. using System.Collections;
  5. using System.Reflection;
  6. using System.Reflection.Emit;
  7. namespace Mono.ILASM {
  8. public class MethodName {
  9. private static int methodCount = 0;
  10. private bool isCtor;
  11. private string name;
  12. /// <summary>
  13. /// </summary>
  14. public MethodName () : this ("M_" + (methodCount++))
  15. {
  16. }
  17. /// <summary>
  18. /// </summary>
  19. /// <param name="name"></param>
  20. public MethodName (string name) : this (name, false)
  21. {
  22. }
  23. /// <summary>
  24. /// </summary>
  25. /// <param name="name"></param>
  26. /// <param name="ctor"></param>
  27. public MethodName (string name, bool ctor)
  28. {
  29. this.name = name;
  30. this.isCtor = ctor;
  31. }
  32. /// <summary>
  33. /// </summary>
  34. public string Name {
  35. get {
  36. return name;
  37. }
  38. set {
  39. name = value;
  40. }
  41. }
  42. /// <summary>
  43. /// </summary>
  44. public bool IsCtor {
  45. get {
  46. return isCtor;
  47. }
  48. set {
  49. isCtor = value;
  50. }
  51. }
  52. }
  53. /// <summary>
  54. /// </summary>
  55. public class Method {
  56. private MethodName name;
  57. private MethodAttributes attrs;
  58. private CallingConventions callConv;
  59. private string retType;
  60. private ArrayList instructions;
  61. /// <summary>
  62. /// </summary>
  63. public Method ()
  64. {
  65. name = new MethodName ();
  66. attrs = 0;
  67. }
  68. /// <summary>
  69. /// </summary>
  70. public string Name {
  71. get {
  72. return name.Name;
  73. }
  74. set {
  75. name.Name = value;
  76. }
  77. }
  78. /// <summary>
  79. /// </summary>
  80. /// <param name="name"></param>
  81. public void SetMethodName (MethodName name)
  82. {
  83. this.name = name;
  84. }
  85. /// <summary>
  86. /// </summary>
  87. public bool IsCtor {
  88. get {
  89. return name.IsCtor;
  90. }
  91. set {
  92. name.IsCtor = value;
  93. }
  94. }
  95. /// <summary>
  96. /// </summary>
  97. public string RetType {
  98. get {
  99. return retType;
  100. }
  101. set {
  102. retType = value;
  103. }
  104. }
  105. /// <summary>
  106. /// </summary>
  107. public MethodAttributes Attrs {
  108. get {
  109. return attrs;
  110. }
  111. set {
  112. attrs = value;
  113. }
  114. }
  115. /// <summary>
  116. /// </summary>
  117. public CallingConventions CallConv {
  118. get {
  119. return callConv;
  120. }
  121. set {
  122. callConv = value;
  123. }
  124. }
  125. /// <summary>
  126. /// </summary>
  127. /// <param name="instr"></param>
  128. public void AddInstruction (InstrBase instr)
  129. {
  130. if (instr == null) {
  131. throw new System.NullReferenceException ("<null> instruction");
  132. }
  133. if (instructions == null) {
  134. this.instructions = new ArrayList ();
  135. }
  136. instructions.Add (instr);
  137. }
  138. /// <summary>
  139. /// </summary>
  140. public int InstrCount {
  141. get {
  142. return (instructions != null) ? instructions.Count : 0;
  143. }
  144. }
  145. /// <summary>
  146. /// </summary>
  147. /// <returns></returns>
  148. public override string ToString ()
  149. {
  150. return String.Format ("IL.Method [Name: {0}, Attrs: {1}, CallConv: {2}, RetType: {3}, Instr: {4}]",
  151. Name, Attrs, CallConv, RetType, InstrCount);
  152. }
  153. /// <summary>
  154. /// </summary>
  155. /// <param name="tb"></param>
  156. public void Emit (Class host)
  157. {
  158. TypeBuilder tb = host.TypeBuilder;
  159. if (IsCtor) {
  160. } else {
  161. Type rt = host.CodeGen.RefTypes.Lookup (RetType);
  162. MethodBuilder mb = tb.DefineMethod (Name, Attrs, CallConv, rt, null);
  163. ILGenerator ilgen = mb.GetILGenerator ();
  164. foreach (InstrBase instr in instructions) {
  165. instr.Emit (ilgen);
  166. }
  167. }
  168. }
  169. }
  170. }