2 // System.Reflection.Emit/MethodBuilder.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Reflection;
35 using System.Reflection.Emit;
36 using System.Globalization;
37 using System.Security;
38 using System.Security.Permissions;
39 using System.Runtime.CompilerServices;
40 using System.Runtime.InteropServices;
42 namespace System.Reflection.Emit {
44 public sealed class MethodBuilder : MethodInfo {
45 private RuntimeMethodHandle mhandle;
47 private Type[] parameters;
48 private MethodAttributes attrs;
49 private MethodImplAttributes iattrs;
51 private int table_idx;
53 private ILGenerator ilgen;
54 private TypeBuilder type;
55 private ParameterBuilder[] pinfo;
56 private CustomAttributeBuilder[] cattrs;
57 private MethodInfo override_method;
58 private string pi_dll;
59 private string pi_entry;
60 private CharSet ncharset;
61 private CallingConvention native_cc;
62 private CallingConventions call_conv;
63 private bool init_locals = true;
64 #if NET_2_0 || BOOTSTRAP_NET_2_0
65 private GenericTypeParameterBuilder[] generic_params;
67 private Object generic_params; /* so offsets are the same */
69 private Type[] returnModReq;
70 private Type[] returnModOpt;
71 private Type[][] paramModReq;
72 private Type[][] paramModOpt;
73 private RefEmitPermissionSet[] permissions;
75 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt) {
77 this.attrs = attributes;
78 this.call_conv = callingConvention;
79 this.rtype = returnType;
80 this.returnModReq = returnModReq;
81 this.returnModOpt = returnModOpt;
82 this.paramModReq = paramModReq;
83 this.paramModOpt = paramModOpt;
84 // The MSDN docs does not specify this, but the MS MethodBuilder
85 // appends a HasThis flag if the method is not static
86 if ((attributes & MethodAttributes.Static) == 0)
87 this.call_conv |= CallingConventions.HasThis;
88 if (parameterTypes != null) {
89 this.parameters = new Type [parameterTypes.Length];
90 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
93 table_idx = get_next_table_index (this, 0x06, true);
94 //Console.WriteLine ("index for "+name+" set to "+table_idx.ToString());
97 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes,
98 CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt,
99 String dllName, String entryName, CallingConvention nativeCConv, CharSet nativeCharset)
100 : this (tb, name, attributes, callingConvention, returnType, returnModReq, returnModOpt, parameterTypes, paramModReq, paramModOpt) {
102 pi_entry = entryName;
103 native_cc = nativeCConv;
104 ncharset = nativeCharset;
107 public bool InitLocals {
108 get {return init_locals;}
109 set {init_locals = value;}
112 internal TypeBuilder TypeBuilder {
116 public override RuntimeMethodHandle MethodHandle {
118 throw NotSupported ();
122 public override Type ReturnType {get {return rtype;}}
123 public override Type ReflectedType {get {return type;}}
124 public override Type DeclaringType {get {return type;}}
125 public override string Name {get {return name;}}
126 public override MethodAttributes Attributes {get {return attrs;}}
127 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
131 public override CallingConventions CallingConvention {
132 get { return call_conv; }
136 public string Signature {
138 throw new NotImplementedException ();
142 public MethodToken GetToken() {
143 return new MethodToken(0x06000000 | table_idx);
146 public override MethodInfo GetBaseDefinition() {
149 public override MethodImplAttributes GetMethodImplementationFlags() {
152 public override ParameterInfo[] GetParameters() {
153 if (!type.is_created)
154 throw NotSupported ();
155 if (parameters == null)
158 ParameterInfo[] retval = new ParameterInfo [parameters.Length];
159 for (int i = 0; i < parameters.Length; i++) {
160 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
165 internal override int GetParameterCount ()
167 if (parameters == null)
170 return parameters.Length;
173 public Module GetModule () {
177 public void CreateMethodBody( byte[] il, int count) {
178 if ((il != null) && ((count < 0) || (count > il.Length)))
179 throw new ArgumentException ("Index was out of range. Must be non-negative and less than the size of the collection.");
181 if ((code != null) || type.is_created)
182 throw new InvalidOperationException ("Type definition of the method is complete.");
187 code = new byte [count];
188 System.Array.Copy(il, code, count);
192 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
193 throw NotSupported ();
195 public override bool IsDefined (Type attribute_type, bool inherit) {
196 throw NotSupported ();
198 public override object[] GetCustomAttributes( bool inherit) {
199 throw NotSupported ();
201 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
202 throw NotSupported ();
204 public ILGenerator GetILGenerator () {
205 return GetILGenerator (64);
208 public ILGenerator GetILGenerator (int size) {
209 if (((iattrs & MethodImplAttributes.CodeTypeMask) !=
210 MethodImplAttributes.IL) ||
211 ((iattrs & MethodImplAttributes.ManagedMask) !=
212 MethodImplAttributes.Managed))
213 throw new InvalidOperationException ("Method body should not exist.");
216 ilgen = new ILGenerator (type.Module, ((ModuleBuilder)type.Module).GetTokenGenerator (), size);
220 public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
225 // Extension: Mono allows position == 0 for the return attribute
227 if ((position < 0) || (position > parameters.Length))
228 throw new ArgumentOutOfRangeException ("position");
230 ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
232 pinfo = new ParameterBuilder [parameters.Length + 1];
233 pinfo [position] = pb;
237 internal void fixup () {
239 ilgen.label_fixup ();
242 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
243 if (customBuilder == null)
244 throw new ArgumentNullException ("customBuilder");
245 string attrname = customBuilder.Ctor.ReflectedType.FullName;
246 if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
247 byte[] data = customBuilder.Data;
248 int impla; // the (stupid) ctor takes a short or an int ...
249 impla = (int)data [2];
250 impla |= ((int)data [3]) << 8;
251 SetImplementationFlags ((MethodImplAttributes)impla);
254 if (cattrs != null) {
255 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
256 cattrs.CopyTo (new_array, 0);
257 new_array [cattrs.Length] = customBuilder;
260 cattrs = new CustomAttributeBuilder [1];
261 cattrs [0] = customBuilder;
264 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
266 throw new ArgumentNullException ("con");
267 if (binaryAttribute == null)
268 throw new ArgumentNullException ("binaryAttribute");
269 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
271 public void SetImplementationFlags( MethodImplAttributes attributes) {
276 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
278 throw new ArgumentNullException ("pset");
279 if ((action == SecurityAction.RequestMinimum) ||
280 (action == SecurityAction.RequestOptional) ||
281 (action == SecurityAction.RequestRefuse))
282 throw new ArgumentException ("Request* values are not permitted", "action");
286 if (permissions != null) {
287 /* Check duplicate actions */
288 foreach (RefEmitPermissionSet set in permissions)
289 if (set.action == action)
290 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
292 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
293 permissions.CopyTo (new_array, 0);
294 permissions = new_array;
297 permissions = new RefEmitPermissionSet [1];
299 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
300 attrs |= MethodAttributes.HasSecurity;
304 public void SetMarshal (UnmanagedMarshal unmanagedMarshal)
307 throw new NotImplementedException ();
311 public void SetSymCustomAttribute (string name, byte[] data)
314 throw new NotImplementedException ();
317 public override string ToString()
319 return "MethodBuilder [" + type.Name + "::" + name + "]";
323 public override bool Equals (object obj)
325 return base.Equals (obj);
328 public override int GetHashCode ()
330 return name.GetHashCode ();
333 internal override int get_next_table_index (object obj, int table, bool inc) {
334 return type.get_next_table_index (obj, table, inc);
337 internal void set_override (MethodInfo mdecl) {
338 override_method = mdecl;
341 private void RejectIfCreated () {
343 throw new InvalidOperationException ("Type definition of the method is complete.");
346 private Exception NotSupported () {
347 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
350 #if NET_2_0 || BOOTSTRAP_NET_2_0
351 [MethodImplAttribute(MethodImplOptions.InternalCall)]
352 public override extern MethodInfo BindGenericParameters (Type [] types);
354 public override bool Mono_IsInflatedMethod {
360 public override bool HasGenericParameters {
362 return generic_params != null;
366 public override Type[] GetGenericArguments ()
368 if (generic_params == null)
371 Type[] result = new Type [generic_params.Length];
372 for (int i = 0; i < generic_params.Length; i++)
373 result [i] = generic_params [i];
378 public GenericTypeParameterBuilder[] DefineGenericParameters (string[] names)
380 generic_params = new GenericTypeParameterBuilder [names.Length];
381 for (int i = 0; i < names.Length; i++)
382 generic_params [i] = new GenericTypeParameterBuilder (
383 type, this, names [i], i);
385 return generic_params;
388 public void SetGenericMethodSignature (MethodAttributes attributes, CallingConventions callingConvention, Type return_type, Type[] parameter_types)
392 this.attrs = attributes;
393 this.call_conv = callingConvention;
394 if ((attributes & MethodAttributes.Static) == 0)
395 this.call_conv |= CallingConventions.HasThis;
397 this.rtype = return_type;
398 this.parameters = new Type [parameter_types.Length];
399 System.Array.Copy (parameter_types, this.parameters, parameter_types.Length);