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;
41 using System.Diagnostics.SymbolStore;
43 namespace System.Reflection.Emit
47 [ComDefaultInterface (typeof (_MethodBuilder))]
49 [ClassInterface (ClassInterfaceType.None)]
50 public sealed class MethodBuilder : MethodInfo, _MethodBuilder
52 private RuntimeMethodHandle mhandle;
54 private Type[] parameters;
55 private MethodAttributes attrs;
56 private MethodImplAttributes iattrs;
58 private int table_idx;
60 private ILGenerator ilgen;
61 private TypeBuilder type;
62 private ParameterBuilder[] pinfo;
63 private CustomAttributeBuilder[] cattrs;
64 private MethodInfo override_method;
65 private string pi_dll;
66 private string pi_entry;
67 private CharSet charset;
68 private uint extra_flags; /* this encodes set_last_error etc */
69 private CallingConvention native_cc;
70 private CallingConventions call_conv;
71 private bool init_locals = true;
72 private IntPtr generic_container;
73 #if NET_2_0 || BOOTSTRAP_NET_2_0
74 private GenericTypeParameterBuilder[] generic_params;
76 private Object generic_params; /* so offsets are the same */
78 private Type[] returnModReq;
79 private Type[] returnModOpt;
80 private Type[][] paramModReq;
81 private Type[][] paramModOpt;
82 private RefEmitPermissionSet[] permissions;
84 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt)
87 this.attrs = attributes;
88 this.call_conv = callingConvention;
89 this.rtype = returnType;
90 this.returnModReq = returnModReq;
91 this.returnModOpt = returnModOpt;
92 this.paramModReq = paramModReq;
93 this.paramModOpt = paramModOpt;
94 // The MSDN docs does not specify this, but the MS MethodBuilder
95 // appends a HasThis flag if the method is not static
96 if ((attributes & MethodAttributes.Static) == 0)
97 this.call_conv |= CallingConventions.HasThis;
98 if (parameterTypes != null) {
99 for (int i = 0; i < parameterTypes.Length; ++i)
100 if (parameterTypes [i] == null)
101 throw new ArgumentException ("Elements of the parameterTypes array cannot be null", "parameterTypes");
103 this.parameters = new Type [parameterTypes.Length];
104 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
107 table_idx = get_next_table_index (this, 0x06, true);
110 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes,
111 CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt,
112 String dllName, String entryName, CallingConvention nativeCConv, CharSet nativeCharset)
113 : this (tb, name, attributes, callingConvention, returnType, returnModReq, returnModOpt, parameterTypes, paramModReq, paramModOpt)
116 pi_entry = entryName;
117 native_cc = nativeCConv;
118 charset = nativeCharset;
121 public bool InitLocals {
122 get {return init_locals;}
123 set {init_locals = value;}
126 internal TypeBuilder TypeBuilder {
130 public override RuntimeMethodHandle MethodHandle {
132 throw NotSupported ();
136 public override Type ReturnType {
137 get { return rtype; }
140 public override Type ReflectedType {
144 public override Type DeclaringType {
148 public override string Name {
152 public override MethodAttributes Attributes {
153 get { return attrs; }
156 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
160 public override CallingConventions CallingConvention {
161 get { return call_conv; }
164 [MonoTODO("Not implemented")]
165 public string Signature {
167 throw new NotImplementedException ();
172 internal bool BestFitMapping {
174 extra_flags = (uint) ((extra_flags & ~0x30) | (uint)(value ? 0x10 : 0x20));
179 internal bool ThrowOnUnmappableChar {
181 extra_flags = (uint) ((extra_flags & ~0x3000) | (uint)(value ? 0x1000 : 0x2000));
186 internal bool ExactSpelling {
188 extra_flags = (uint) ((extra_flags & ~0x01) | (uint)(value ? 0x01 : 0x00));
193 internal bool SetLastError {
195 extra_flags = (uint) ((extra_flags & ~0x40) | (uint)(value ? 0x40 : 0x00));
199 public MethodToken GetToken()
201 return new MethodToken(0x06000000 | table_idx);
204 public override MethodInfo GetBaseDefinition()
209 public override MethodImplAttributes GetMethodImplementationFlags()
214 public override ParameterInfo[] GetParameters()
216 if (!type.is_created)
217 throw NotSupported ();
218 if (parameters == null)
221 ParameterInfo[] retval = new ParameterInfo [parameters.Length];
222 for (int i = 0; i < parameters.Length; i++) {
223 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
228 internal override int GetParameterCount ()
230 if (parameters == null)
233 return parameters.Length;
236 public Module GetModule ()
241 public void CreateMethodBody (byte[] il, int count)
243 if ((il != null) && ((count < 0) || (count > il.Length)))
244 throw new ArgumentOutOfRangeException ("Index was out of range. Must be non-negative and less than the size of the collection.");
246 if ((code != null) || type.is_created)
247 throw new InvalidOperationException ("Type definition of the method is complete.");
252 code = new byte [count];
253 System.Array.Copy(il, code, count);
257 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
259 throw NotSupported ();
262 public override bool IsDefined (Type attribute_type, bool inherit)
264 throw NotSupported ();
267 public override object[] GetCustomAttributes (bool inherit)
270 * On MS.NET, this always returns not_supported, but we can't do this
271 * since there would be no way to obtain custom attributes of
272 * dynamically created ctors.
275 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
277 throw NotSupported ();
280 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
283 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
285 throw NotSupported ();
288 public ILGenerator GetILGenerator ()
290 return GetILGenerator (64);
293 public ILGenerator GetILGenerator (int size)
295 if (((iattrs & MethodImplAttributes.CodeTypeMask) !=
296 MethodImplAttributes.IL) ||
297 ((iattrs & MethodImplAttributes.ManagedMask) !=
298 MethodImplAttributes.Managed))
299 throw new InvalidOperationException ("Method body should not exist.");
302 ilgen = new ILGenerator (type.Module, ((ModuleBuilder)type.Module).GetTokenGenerator (), size);
306 public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
311 // Extension: Mono allows position == 0 for the return attribute
313 if ((position < 0) || (position > parameters.Length))
314 throw new ArgumentOutOfRangeException ("position");
316 ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
318 pinfo = new ParameterBuilder [parameters.Length + 1];
319 pinfo [position] = pb;
323 internal void fixup ()
325 if (((attrs & (MethodAttributes.Abstract | MethodAttributes.PinvokeImpl)) == 0) && ((iattrs & (MethodImplAttributes.Runtime | MethodImplAttributes.InternalCall)) == 0)) {
327 // do not allow zero length method body on MS.NET 2.0 (and higher)
328 if (((ilgen == null) || (ILGenerator.Mono_GetCurrentOffset (ilgen) == 0)) && (code == null || code.Length == 0))
330 if (((ilgen == null) || (ILGenerator.Mono_GetCurrentOffset (ilgen) == 0)) && (code == null))
332 throw new InvalidOperationException (
333 String.Format ("Method '{0}.{1}' does not have a method body.",
334 DeclaringType.Name, Name));
337 ilgen.label_fixup ();
340 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
342 if (ilgen != null && ilgen.HasDebugInfo) {
343 SymbolToken token = new SymbolToken (GetToken().Token);
344 symbolWriter.OpenMethod (token);
345 symbolWriter.SetSymAttribute (token, "__name", System.Text.Encoding.UTF8.GetBytes (Name));
346 ilgen.GenerateDebugInfo (symbolWriter);
347 symbolWriter.CloseMethod ();
351 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
353 if (customBuilder == null)
354 throw new ArgumentNullException ("customBuilder");
356 switch (customBuilder.Ctor.ReflectedType.FullName) {
357 case "System.Runtime.CompilerServices.MethodImplAttribute":
358 byte[] data = customBuilder.Data;
359 int impla; // the (stupid) ctor takes a short or an int ...
360 impla = (int)data [2];
361 impla |= ((int)data [3]) << 8;
362 iattrs |= (MethodImplAttributes)impla;
365 case "System.Runtime.InteropServices.DllImportAttribute":
366 CustomAttributeBuilder.CustomAttributeInfo attr = CustomAttributeBuilder.decode_cattr (customBuilder);
367 bool preserveSig = true;
370 * It would be easier to construct a DllImportAttribute from
371 * the custom attribute builder, but the DllImportAttribute
372 * does not contain all the information required here, ie.
373 * - some parameters, like BestFitMapping has three values
374 * ("on", "off", "missing"), but DllImportAttribute only
375 * contains two (on/off).
376 * - PreserveSig is true by default, while it is false by
377 * default in DllImportAttribute.
380 pi_dll = (string)attr.ctorArgs[0];
381 if (pi_dll == null || pi_dll.Length == 0)
382 throw new ArgumentException ("DllName cannot be empty");
384 native_cc = System.Runtime.InteropServices.CallingConvention.Winapi;
386 for (int i = 0; i < attr.namedParamNames.Length; ++i) {
387 string name = attr.namedParamNames [i];
388 object value = attr.namedParamValues [i];
390 if (name == "CallingConvention")
391 native_cc = (CallingConvention)value;
392 else if (name == "CharSet")
393 charset = (CharSet)value;
394 else if (name == "EntryPoint")
395 pi_entry = (string)value;
396 else if (name == "ExactSpelling")
397 ExactSpelling = (bool)value;
398 else if (name == "SetLastError")
399 SetLastError = (bool)value;
400 else if (name == "PreserveSig")
401 preserveSig = (bool)value;
403 else if (name == "BestFitMapping")
404 BestFitMapping = (bool)value;
405 else if (name == "ThrowOnUnmappableChar")
406 ThrowOnUnmappableChar = (bool)value;
410 attrs |= MethodAttributes.PinvokeImpl;
412 iattrs |= MethodImplAttributes.PreserveSig;
415 case "System.Runtime.InteropServices.PreserveSigAttribute":
416 iattrs |= MethodImplAttributes.PreserveSig;
419 case "System.Runtime.CompilerServices.SpecialNameAttribute":
420 attrs |= MethodAttributes.SpecialName;
423 case "System.Security.SuppressUnmanagedCodeSecurityAttribute":
424 attrs |= MethodAttributes.HasSecurity;
428 if (cattrs != null) {
429 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
430 cattrs.CopyTo (new_array, 0);
431 new_array [cattrs.Length] = customBuilder;
434 cattrs = new CustomAttributeBuilder [1];
435 cattrs [0] = customBuilder;
442 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
445 throw new ArgumentNullException ("con");
446 if (binaryAttribute == null)
447 throw new ArgumentNullException ("binaryAttribute");
448 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
451 public void SetImplementationFlags (MethodImplAttributes attributes)
457 public void AddDeclarativeSecurity (SecurityAction action, PermissionSet pset)
460 throw new ArgumentNullException ("pset");
461 if ((action == SecurityAction.RequestMinimum) ||
462 (action == SecurityAction.RequestOptional) ||
463 (action == SecurityAction.RequestRefuse))
464 throw new ArgumentOutOfRangeException ("Request* values are not permitted", "action");
468 if (permissions != null) {
469 /* Check duplicate actions */
470 foreach (RefEmitPermissionSet set in permissions)
471 if (set.action == action)
472 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
474 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
475 permissions.CopyTo (new_array, 0);
476 permissions = new_array;
479 permissions = new RefEmitPermissionSet [1];
481 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
482 attrs |= MethodAttributes.HasSecurity;
486 [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
488 public void SetMarshal (UnmanagedMarshal unmanagedMarshal)
491 throw new NotImplementedException ();
495 public void SetSymCustomAttribute (string name, byte[] data)
498 throw new NotImplementedException ();
501 public override string ToString()
503 return "MethodBuilder [" + type.Name + "::" + name + "]";
507 public override bool Equals (object obj)
509 return base.Equals (obj);
512 public override int GetHashCode ()
514 return name.GetHashCode ();
517 internal override int get_next_table_index (object obj, int table, bool inc)
519 return type.get_next_table_index (obj, table, inc);
522 internal void set_override (MethodInfo mdecl)
524 override_method = mdecl;
527 private void RejectIfCreated ()
530 throw new InvalidOperationException ("Type definition of the method is complete.");
533 private Exception NotSupported ()
535 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
538 #if NET_2_0 || BOOTSTRAP_NET_2_0
539 [MethodImplAttribute(MethodImplOptions.InternalCall)]
540 public override extern MethodInfo MakeGenericMethod (params Type [] types);
542 public override bool IsGenericMethodDefinition {
544 return generic_params != null;
548 public override bool IsGenericMethod {
550 return generic_params != null;
554 public override MethodInfo GetGenericMethodDefinition ()
556 if (!IsGenericMethodDefinition)
557 throw new InvalidOperationException ();
562 public override Type[] GetGenericArguments ()
564 if (generic_params == null)
567 Type[] result = new Type [generic_params.Length];
568 for (int i = 0; i < generic_params.Length; i++)
569 result [i] = generic_params [i];
574 public GenericTypeParameterBuilder[] DefineGenericParameters (params string[] names)
577 throw new ArgumentNullException ("names");
579 generic_params = new GenericTypeParameterBuilder [names.Length];
580 for (int i = 0; i < names.Length; i++)
581 generic_params [i] = new GenericTypeParameterBuilder (
582 type, this, names [i], i);
584 return generic_params;
587 public void SetGenericMethodSignature (MethodAttributes attributes, CallingConventions callingConvention, Type return_type, Type[] parameter_types)
591 this.attrs = attributes;
592 this.call_conv = callingConvention;
593 if ((attributes & MethodAttributes.Static) == 0)
594 this.call_conv |= CallingConventions.HasThis;
596 this.rtype = return_type;
597 this.parameters = new Type [parameter_types.Length];
598 System.Array.Copy (parameter_types, this.parameters, parameter_types.Length);
601 public void SetReturnType (Type returnType)
606 public void SetParameters (params Type[] parameterTypes)
608 if (parameterTypes != null) {
609 for (int i = 0; i < parameterTypes.Length; ++i)
610 if (parameterTypes [i] == null)
611 throw new ArgumentException ("Elements of the parameterTypes array cannot be null", "parameterTypes");
613 this.parameters = new Type [parameterTypes.Length];
614 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
618 public void SetSignature (Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
620 SetReturnType (returnType);
621 SetParameters (parameterTypes);
622 this.returnModReq = returnTypeRequiredCustomModifiers;
623 this.returnModOpt = returnTypeOptionalCustomModifiers;
624 this.paramModReq = parameterTypeRequiredCustomModifiers;
625 this.paramModOpt = parameterTypeOptionalCustomModifiers;
628 public override Module Module {
635 void _MethodBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
637 throw new NotImplementedException ();
640 void _MethodBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
642 throw new NotImplementedException ();
645 void _MethodBuilder.GetTypeInfoCount (out uint pcTInfo)
647 throw new NotImplementedException ();
650 void _MethodBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
652 throw new NotImplementedException ();