CodeMethod.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. // Copyright (C) Lluis Sanchez Gual, 2004
  22. //
  23. using System;
  24. using System.Collections;
  25. using System.IO;
  26. using System.Reflection;
  27. using System.Reflection.Emit;
  28. namespace Mono.CodeGeneration
  29. {
  30. public class CodeMethod
  31. {
  32. MethodBase methodBase;
  33. CodeBuilder builder;
  34. string name;
  35. MethodAttributes attributes;
  36. Type returnType;
  37. TypeBuilder typeBuilder;
  38. Type[] parameterTypes;
  39. ArrayList customAttributes = new ArrayList ();
  40. CodeClass cls;
  41. internal static CodeMethod DefineMethod (CodeClass cls, string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes)
  42. {
  43. return new CodeMethod (cls, name, attributes, returnType, parameterTypes);
  44. }
  45. public static CodeMethod DefineConstructor (CodeClass cls, MethodAttributes attributes, Type[] parameterTypes)
  46. {
  47. return new CodeMethod (cls, attributes, parameterTypes);
  48. }
  49. internal CodeMethod (CodeClass cls, string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes)
  50. {
  51. this.cls = cls;
  52. this.typeBuilder = cls.TypeBuilder;
  53. this.name = name;
  54. this.attributes = attributes;
  55. this.returnType = returnType;
  56. this.parameterTypes = parameterTypes;
  57. methodBase = typeBuilder.DefineMethod (name, attributes, returnType, parameterTypes);
  58. builder = new CodeBuilder (cls);
  59. }
  60. CodeMethod (CodeClass cls, MethodAttributes attributes, Type[] parameterTypes)
  61. {
  62. this.cls = cls;
  63. this.typeBuilder = cls.TypeBuilder;
  64. this.attributes = attributes;
  65. this.parameterTypes = parameterTypes;
  66. this.name = typeBuilder.Name;
  67. methodBase = typeBuilder.DefineConstructor (attributes, CallingConventions.Standard, parameterTypes);
  68. builder = new CodeBuilder (cls);
  69. }
  70. public TypeBuilder DeclaringType
  71. {
  72. get { return typeBuilder; }
  73. }
  74. public MethodInfo MethodInfo
  75. {
  76. get { return methodBase as MethodInfo; }
  77. }
  78. public MethodBase MethodBase
  79. {
  80. get { return methodBase; }
  81. }
  82. public string Name
  83. {
  84. get { return name; }
  85. }
  86. public MethodAttributes Attributes
  87. {
  88. get { return attributes; }
  89. }
  90. public Type ReturnType
  91. {
  92. get { return returnType; }
  93. }
  94. public Type[] ParameterTypes
  95. {
  96. get { return parameterTypes; }
  97. }
  98. public CodeBuilder CodeBuilder
  99. {
  100. get { return builder; }
  101. }
  102. public bool IsStatic
  103. {
  104. get { return (attributes & MethodAttributes.Static) != 0; }
  105. }
  106. public CodeCustomAttribute CreateCustomAttribute (Type attributeType)
  107. {
  108. return CreateCustomAttribute (attributeType,
  109. Type.EmptyTypes, new object [0]);
  110. }
  111. public CodeCustomAttribute CreateCustomAttribute (Type attributeType, Type [] ctorArgTypes, object [] ctorArgs)
  112. {
  113. return CreateCustomAttribute (attributeType,
  114. ctorArgTypes, ctorArgs, new string [0], new object [0]);
  115. }
  116. public CodeCustomAttribute CreateCustomAttribute (Type attributeType, Type [] ctorArgTypes, object [] ctorArgs, string [] namedArgFieldNames, object [] namedArgValues)
  117. {
  118. CodeCustomAttribute cca = CodeCustomAttribute.Create (
  119. attributeType, ctorArgTypes, ctorArgs, namedArgFieldNames, namedArgValues);
  120. SetCustomAttribute (cca);
  121. return cca;
  122. }
  123. public CodeCustomAttribute CreateCustomAttribute (Type attributeType, Type [] ctorArgTypes, CodeLiteral [] ctorArgs, FieldInfo [] fields, CodeLiteral [] fieldValues)
  124. {
  125. CodeCustomAttribute cca = CodeCustomAttribute.Create (
  126. attributeType, ctorArgTypes, ctorArgs, fields, fieldValues);
  127. SetCustomAttribute (cca);
  128. return cca;
  129. }
  130. void SetCustomAttribute (CodeCustomAttribute cca)
  131. {
  132. if (methodBase is MethodBuilder)
  133. ((MethodBuilder) methodBase).SetCustomAttribute (cca.Builder);
  134. else if (methodBase is ConstructorBuilder)
  135. ((ConstructorBuilder) methodBase).SetCustomAttribute (cca.Builder);
  136. customAttributes.Add (cca);
  137. }
  138. public string PrintCode ()
  139. {
  140. StringWriter sw = new StringWriter ();
  141. CodeWriter cw = new CodeWriter (sw);
  142. PrintCode (cw);
  143. return sw.ToString ();
  144. }
  145. public virtual void PrintCode (CodeWriter cp)
  146. {
  147. cp.BeginLine ();
  148. foreach (CodeCustomAttribute a in customAttributes)
  149. a.PrintCode (cp);
  150. if ((methodBase.Attributes & MethodAttributes.Static) != 0)
  151. cp.Write ("static ");
  152. if ((methodBase.Attributes & MethodAttributes.Public) != 0)
  153. cp.Write ("public ");
  154. if (returnType != null) cp.Write (returnType + " ");
  155. cp.Write (name + " (");
  156. for (int n=0; n<parameterTypes.Length; n++) {
  157. if (n > 0) cp.Write (", ");
  158. cp.Write (parameterTypes[n] + " arg" + n);
  159. }
  160. cp.Write (")");
  161. cp.EndLine ();
  162. cp.WriteLineInd ("{");
  163. builder.PrintCode (cp);
  164. cp.WriteLineUnind ("}");
  165. }
  166. public CodeArgumentReference GetArg (int n)
  167. {
  168. if (n < 0 || n >= parameterTypes.Length)
  169. throw new InvalidOperationException ("Invalid argument number");
  170. int narg = IsStatic ? n : n + 1;
  171. return new CodeArgumentReference (parameterTypes[n], narg, "arg" + n);
  172. }
  173. public CodeArgumentReference GetThis ()
  174. {
  175. if (IsStatic)
  176. throw new InvalidOperationException ("'this' not available in static methods");
  177. return new CodeArgumentReference (DeclaringType, 0, "this");
  178. }
  179. public void Generate ()
  180. {
  181. ILGenerator gen = methodBase is MethodInfo ? ((MethodBuilder)methodBase).GetILGenerator() : ((ConstructorBuilder)methodBase).GetILGenerator();
  182. Label returnLabel = gen.DefineLabel ();
  183. builder.ReturnLabel = returnLabel;
  184. builder.Generate (gen);
  185. gen.MarkLabel(returnLabel);
  186. gen.Emit(OpCodes.Ret);
  187. }
  188. public void UpdateMethodBase (Type type)
  189. {
  190. if (methodBase is MethodInfo)
  191. methodBase = type.GetMethod (methodBase.Name, parameterTypes);
  192. else
  193. methodBase = type.GetConstructor (parameterTypes);
  194. }
  195. }
  196. }