MethodBuilder.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //
  2. // System.Reflection.Emit/MethodBuilder.cs
  3. //
  4. // Author:
  5. // Paolo Molaro ([email protected])
  6. //
  7. // (C) 2001 Ximian, Inc. http://www.ximian.com
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Reflection;
  33. using System.Reflection.Emit;
  34. using System.Globalization;
  35. using System.Security;
  36. using System.Security.Permissions;
  37. using System.Runtime.CompilerServices;
  38. using System.Runtime.InteropServices;
  39. namespace System.Reflection.Emit {
  40. public sealed class MethodBuilder : MethodInfo {
  41. private RuntimeMethodHandle mhandle;
  42. private Type rtype;
  43. private Type[] parameters;
  44. private MethodAttributes attrs;
  45. private MethodImplAttributes iattrs;
  46. private string name;
  47. private int table_idx;
  48. private byte[] code;
  49. private ILGenerator ilgen;
  50. private TypeBuilder type;
  51. private ParameterBuilder[] pinfo;
  52. private CustomAttributeBuilder[] cattrs;
  53. private MethodInfo override_method;
  54. private string pi_dll;
  55. private string pi_entry;
  56. private CharSet ncharset;
  57. private CallingConvention native_cc;
  58. private CallingConventions call_conv;
  59. private bool init_locals = true;
  60. #if NET_2_0 || BOOTSTRAP_NET_2_0
  61. private GenericTypeParameterBuilder[] generic_params;
  62. #else
  63. private Object generic_params; /* so offsets are the same */
  64. #endif
  65. private Type[] returnModReq;
  66. private Type[] returnModOpt;
  67. private Type[][] paramModReq;
  68. private Type[][] paramModOpt;
  69. private RefEmitPermissionSet[] permissions;
  70. internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt) {
  71. this.name = name;
  72. this.attrs = attributes;
  73. this.call_conv = callingConvention;
  74. this.rtype = returnType;
  75. this.returnModReq = returnModReq;
  76. this.returnModOpt = returnModOpt;
  77. this.paramModReq = paramModReq;
  78. this.paramModOpt = paramModOpt;
  79. // The MSDN docs does not specify this, but the MS MethodBuilder
  80. // appends a HasThis flag if the method is not static
  81. if ((attributes & MethodAttributes.Static) == 0)
  82. this.call_conv |= CallingConventions.HasThis;
  83. if (parameterTypes != null) {
  84. this.parameters = new Type [parameterTypes.Length];
  85. System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
  86. }
  87. type = tb;
  88. table_idx = get_next_table_index (this, 0x06, true);
  89. //Console.WriteLine ("index for "+name+" set to "+table_idx.ToString());
  90. }
  91. internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes,
  92. CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt,
  93. String dllName, String entryName, CallingConvention nativeCConv, CharSet nativeCharset)
  94. : this (tb, name, attributes, callingConvention, returnType, returnModReq, returnModOpt, parameterTypes, paramModReq, paramModOpt) {
  95. pi_dll = dllName;
  96. pi_entry = entryName;
  97. native_cc = nativeCConv;
  98. ncharset = nativeCharset;
  99. }
  100. public bool InitLocals {
  101. get {return init_locals;}
  102. set {init_locals = value;}
  103. }
  104. internal TypeBuilder TypeBuilder {
  105. get {return type;}
  106. }
  107. public override RuntimeMethodHandle MethodHandle {
  108. get {
  109. throw NotSupported ();
  110. }
  111. }
  112. public override Type ReturnType {get {return rtype;}}
  113. public override Type ReflectedType {get {return type;}}
  114. public override Type DeclaringType {get {return type;}}
  115. public override string Name {get {return name;}}
  116. public override MethodAttributes Attributes {get {return attrs;}}
  117. public override ICustomAttributeProvider ReturnTypeCustomAttributes {
  118. get {return null;}
  119. }
  120. public override CallingConventions CallingConvention {
  121. get { return call_conv; }
  122. }
  123. [MonoTODO]
  124. public string Signature {
  125. get {
  126. throw new NotImplementedException ();
  127. }
  128. }
  129. public MethodToken GetToken() {
  130. return new MethodToken(0x06000000 | table_idx);
  131. }
  132. public override MethodInfo GetBaseDefinition() {
  133. return this;
  134. }
  135. public override MethodImplAttributes GetMethodImplementationFlags() {
  136. return iattrs;
  137. }
  138. public override ParameterInfo[] GetParameters() {
  139. if (parameters == null)
  140. return null;
  141. ParameterInfo[] retval = new ParameterInfo [parameters.Length];
  142. for (int i = 0; i < parameters.Length; i++) {
  143. retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
  144. }
  145. return retval;
  146. }
  147. internal override int GetParameterCount ()
  148. {
  149. if (parameters == null)
  150. return 0;
  151. return parameters.Length;
  152. }
  153. public Module GetModule () {
  154. return type.Module;
  155. }
  156. public void CreateMethodBody( byte[] il, int count) {
  157. if ((il != null) && ((count < 0) || (count > il.Length)))
  158. throw new ArgumentException ("Index was out of range. Must be non-negative and less than the size of the collection.");
  159. if ((code != null) || type.is_created)
  160. throw new InvalidOperationException ("Type definition of the method is complete.");
  161. if (il == null)
  162. code = null;
  163. else {
  164. code = new byte [count];
  165. System.Array.Copy(il, code, count);
  166. }
  167. }
  168. public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  169. throw NotSupported ();
  170. }
  171. public override bool IsDefined (Type attribute_type, bool inherit) {
  172. throw NotSupported ();
  173. }
  174. public override object[] GetCustomAttributes( bool inherit) {
  175. throw NotSupported ();
  176. }
  177. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  178. throw NotSupported ();
  179. }
  180. public ILGenerator GetILGenerator () {
  181. return GetILGenerator (64);
  182. }
  183. public ILGenerator GetILGenerator (int size) {
  184. if (((iattrs & MethodImplAttributes.CodeTypeMask) !=
  185. MethodImplAttributes.IL) ||
  186. ((iattrs & MethodImplAttributes.ManagedMask) !=
  187. MethodImplAttributes.Managed))
  188. throw new InvalidOperationException ("Method body should not exist.");
  189. if (ilgen != null)
  190. return ilgen;
  191. ilgen = new ILGenerator (type.Module, ((ModuleBuilder)type.Module).GetTokenGenerator (), size);
  192. return ilgen;
  193. }
  194. public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
  195. {
  196. RejectIfCreated ();
  197. //
  198. // Extension: Mono allows position == 0 for the return attribute
  199. //
  200. if ((position < 0) || (position > parameters.Length))
  201. throw new ArgumentOutOfRangeException ("position");
  202. ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
  203. if (pinfo == null)
  204. pinfo = new ParameterBuilder [parameters.Length + 1];
  205. pinfo [position] = pb;
  206. return pb;
  207. }
  208. internal void fixup () {
  209. if (ilgen != null)
  210. ilgen.label_fixup ();
  211. }
  212. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  213. if (customBuilder == null)
  214. throw new ArgumentNullException ("customBuilder");
  215. string attrname = customBuilder.Ctor.ReflectedType.FullName;
  216. if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
  217. byte[] data = customBuilder.Data;
  218. int impla; // the (stupid) ctor takes a short or an int ...
  219. impla = (int)data [2];
  220. impla |= ((int)data [3]) << 8;
  221. SetImplementationFlags ((MethodImplAttributes)impla);
  222. return;
  223. }
  224. if (cattrs != null) {
  225. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  226. cattrs.CopyTo (new_array, 0);
  227. new_array [cattrs.Length] = customBuilder;
  228. cattrs = new_array;
  229. } else {
  230. cattrs = new CustomAttributeBuilder [1];
  231. cattrs [0] = customBuilder;
  232. }
  233. }
  234. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  235. if (con == null)
  236. throw new ArgumentNullException ("con");
  237. if (binaryAttribute == null)
  238. throw new ArgumentNullException ("binaryAttribute");
  239. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  240. }
  241. public void SetImplementationFlags( MethodImplAttributes attributes) {
  242. RejectIfCreated ();
  243. iattrs = attributes;
  244. }
  245. public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
  246. if (pset == null)
  247. throw new ArgumentNullException ("pset");
  248. if ((action == SecurityAction.RequestMinimum) ||
  249. (action == SecurityAction.RequestOptional) ||
  250. (action == SecurityAction.RequestRefuse))
  251. throw new ArgumentException ("Request* values are not permitted", "action");
  252. RejectIfCreated ();
  253. if (permissions != null) {
  254. /* Check duplicate actions */
  255. foreach (RefEmitPermissionSet set in permissions)
  256. if (set.action == action)
  257. throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
  258. RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
  259. permissions.CopyTo (new_array, 0);
  260. permissions = new_array;
  261. }
  262. else
  263. permissions = new RefEmitPermissionSet [1];
  264. permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
  265. attrs |= MethodAttributes.HasSecurity;
  266. }
  267. [MonoTODO]
  268. public void SetMarshal (UnmanagedMarshal unmanagedMarshal)
  269. {
  270. RejectIfCreated ();
  271. throw new NotImplementedException ();
  272. }
  273. [MonoTODO]
  274. public void SetSymCustomAttribute (string name, byte[] data)
  275. {
  276. RejectIfCreated ();
  277. throw new NotImplementedException ();
  278. }
  279. public override string ToString()
  280. {
  281. return "MethodBuilder [" + type.Name + "::" + name + "]";
  282. }
  283. [MonoTODO]
  284. public override bool Equals (object obj)
  285. {
  286. return base.Equals (obj);
  287. }
  288. public override int GetHashCode ()
  289. {
  290. return name.GetHashCode ();
  291. }
  292. internal override int get_next_table_index (object obj, int table, bool inc) {
  293. return type.get_next_table_index (obj, table, inc);
  294. }
  295. internal void set_override (MethodInfo mdecl) {
  296. override_method = mdecl;
  297. }
  298. private void RejectIfCreated () {
  299. if (type.is_created)
  300. throw new InvalidOperationException ("Type definition of the method is complete.");
  301. }
  302. private Exception NotSupported () {
  303. return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
  304. }
  305. #if NET_2_0 || BOOTSTRAP_NET_2_0
  306. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  307. public override extern MethodInfo BindGenericParameters (Type [] types);
  308. public override bool Mono_IsInflatedMethod {
  309. get {
  310. return false;
  311. }
  312. }
  313. public override bool HasGenericParameters {
  314. get {
  315. return generic_params != null;
  316. }
  317. }
  318. public override Type[] GetGenericArguments ()
  319. {
  320. if (generic_params == null)
  321. return new Type [0];
  322. Type[] result = new Type [generic_params.Length];
  323. for (int i = 0; i < generic_params.Length; i++)
  324. result [i] = generic_params [i];
  325. return result;
  326. }
  327. public GenericTypeParameterBuilder[] DefineGenericParameters (string[] names)
  328. {
  329. generic_params = new GenericTypeParameterBuilder [names.Length];
  330. for (int i = 0; i < names.Length; i++)
  331. generic_params [i] = new GenericTypeParameterBuilder (
  332. type, this, names [i], i);
  333. return generic_params;
  334. }
  335. public void SetGenericMethodSignature (MethodAttributes attributes, CallingConventions callingConvention, Type return_type, Type[] parameter_types)
  336. {
  337. RejectIfCreated ();
  338. this.attrs = attributes;
  339. this.call_conv = callingConvention;
  340. if ((attributes & MethodAttributes.Static) == 0)
  341. this.call_conv |= CallingConventions.HasThis;
  342. this.rtype = return_type;
  343. this.parameters = new Type [parameter_types.Length];
  344. System.Array.Copy (parameter_types, this.parameters, parameter_types.Length);
  345. }
  346. #endif
  347. }
  348. }