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