Avoid creating an AssemblyBuilder in DynamicMethod if not needed.
[mono.git] / mcs / class / corlib / System.Reflection.Emit / DynamicMethod.cs
1 //
2 // System.Reflection.Emit.DynamicMethod.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //   Zoltan Varga (vargaz@freemail.hu)
7 //
8 // (C) 2003 Ximian, Inc.  http://www.ximian.com
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34
35 using System;
36 using System.Reflection;
37 using System.Reflection.Emit;
38 using System.Globalization;
39 using System.Runtime.CompilerServices;
40 using System.Runtime.InteropServices;
41
42 namespace System.Reflection.Emit {
43
44         [ComVisible (true)]
45         public sealed class DynamicMethod : MethodInfo {
46
47 #pragma warning disable 169, 414, 649
48                 #region Sync with reflection.h
49                 private RuntimeMethodHandle mhandle;
50                 private string name;
51                 private Type returnType;
52                 private Type[] parameters;
53                 private MethodAttributes attributes;
54                 private CallingConventions callingConvention;
55                 private Module module;
56                 private bool skipVisibility;
57                 private bool init_locals = true;
58                 private ILGenerator ilgen;
59                 private int nrefs;
60                 private object[] refs;
61                 private IntPtr referenced_by;
62                 private Type owner;
63                 #endregion
64 #pragma warning restore 169, 414, 649
65                 
66                 private Delegate deleg;
67                 private MonoMethod method;
68                 private ParameterBuilder[] pinfo;
69                 internal bool creating;
70                 private DynamicILInfo il_info;
71
72                 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Module m) : this (name, returnType, parameterTypes, m, false) {
73                 }
74
75                 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner) : this (name, returnType, parameterTypes, owner, false) {
76                 }
77
78                 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, m, skipVisibility) {
79                 }
80
81                 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner, skipVisibility) {
82                 }
83
84                 public DynamicMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility) : this (name, attributes, callingConvention, returnType, parameterTypes, owner, owner.Module, skipVisibility, false) {
85                 }
86
87                 public DynamicMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility) : this (name, attributes, callingConvention, returnType, parameterTypes, null, m, skipVisibility, false) {
88                 }
89
90                 public DynamicMethod (string name, Type returnType, Type[] parameterTypes) : this (name, returnType, parameterTypes, false) {
91                 }
92
93                 [MonoTODO ("Visibility is not restricted")]
94                 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, bool restrictedSkipVisibility)
95                         : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, null, null, restrictedSkipVisibility, true)
96                 {
97                 }
98
99                 DynamicMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type [] parameterTypes, Type owner, Module m, bool skipVisibility, bool anonHosted)
100                 {
101                         if (name == null)
102                                 throw new ArgumentNullException ("name");
103                         if (returnType == null)
104                                 returnType = typeof (void);
105                         if ((m == null) && !anonHosted)
106                                 throw new ArgumentNullException ("m");
107                         if (returnType.IsByRef)
108                                 throw new ArgumentException ("Return type can't be a byref type", "returnType");
109                         if (parameterTypes != null) {
110                                 for (int i = 0; i < parameterTypes.Length; ++i)
111                                         if (parameterTypes [i] == null)
112                                                 throw new ArgumentException ("Parameter " + i + " is null", "parameterTypes");
113                         }
114
115                         if (m == null)
116                                 m = AnonHostModuleHolder.AnonHostModule;
117
118                         this.name = name;
119                         this.attributes = attributes | MethodAttributes.Static;
120                         this.callingConvention = callingConvention;
121                         this.returnType = returnType;
122                         this.parameters = parameterTypes;
123                         this.owner = owner;
124                         this.module = m;
125                         this.skipVisibility = skipVisibility;
126                 }
127
128                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
129                 private extern void create_dynamic_method (DynamicMethod m);
130
131                 private void CreateDynMethod () {
132                         if (mhandle.Value == IntPtr.Zero) {
133                                 if (ilgen == null || ilgen.ILOffset == 0)
134                                         throw new InvalidOperationException ("Method '" + name + "' does not have a method body.");
135
136                                 ilgen.label_fixup ();
137
138                                 // Have to create all DynamicMethods referenced by this one
139                                 try {
140                                         // Used to avoid cycles
141                                         creating = true;
142                                         if (refs != null) {
143                                                 for (int i = 0; i < refs.Length; ++i) {
144                                                         if (refs [i] is DynamicMethod) {
145                                                                 DynamicMethod m = (DynamicMethod)refs [i];
146                                                                 if (!m.creating)
147                                                                         m.CreateDynMethod ();
148                                                         }
149                                                 }
150                                         }
151                                 } finally {
152                                         creating = false;
153                                 }
154
155                                 create_dynamic_method (this);
156                         }
157                 }
158
159                 [ComVisible (true)]
160                 public Delegate CreateDelegate (Type delegateType)
161                 {
162                         if (delegateType == null)
163                                 throw new ArgumentNullException ("delegateType");
164                         if (deleg != null)
165                                 return deleg;
166
167                         CreateDynMethod ();
168
169                         deleg = Delegate.CreateDelegate (delegateType, this);
170                         return deleg;
171                 }
172
173                 [ComVisible (true)]
174                 public Delegate CreateDelegate (Type delegateType, object target)
175                 {
176                         if (delegateType == null)
177                                 throw new ArgumentNullException ("delegateType");
178
179                         CreateDynMethod ();
180
181                         /* Can't cache the delegate since it is different for each target */
182                         return Delegate.CreateDelegate (delegateType, target, this);
183                 }
184                 
185                 public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string parameterName)
186                 {
187                         //
188                         // Extension: Mono allows position == 0 for the return attribute
189                         //
190                         if ((position < 0) || (position > parameters.Length))
191                                 throw new ArgumentOutOfRangeException ("position");
192
193                         RejectIfCreated ();
194
195                         ParameterBuilder pb = new ParameterBuilder (this, position, attributes, parameterName);
196                         if (pinfo == null)
197                                 pinfo = new ParameterBuilder [parameters.Length + 1];
198                         pinfo [position] = pb;
199                         return pb;
200                 }
201
202                 public override MethodInfo GetBaseDefinition () {
203                         return this;
204                 }
205
206                 [MonoTODO("Not implemented")]
207                 public override object[] GetCustomAttributes (bool inherit) {
208                         throw new NotImplementedException ();
209                 }
210
211                 [MonoTODO("Not implemented")]
212                 public override object[] GetCustomAttributes (Type attributeType,
213                                                                                                           bool inherit) {
214                         throw new NotImplementedException ();
215                 }
216
217                 public DynamicILInfo GetDynamicILInfo () {
218                         if (il_info == null)
219                                 il_info = new DynamicILInfo (this);
220                         return il_info;
221                 }
222
223                 public ILGenerator GetILGenerator () {
224                         return GetILGenerator (64);
225                 }
226
227                 public ILGenerator GetILGenerator (int streamSize) {
228                         if (((GetMethodImplementationFlags () & MethodImplAttributes.CodeTypeMask) != 
229                                  MethodImplAttributes.IL) ||
230                                 ((GetMethodImplementationFlags () & MethodImplAttributes.ManagedMask) != 
231                                  MethodImplAttributes.Managed))
232                                 throw new InvalidOperationException ("Method body should not exist.");
233                         if (ilgen != null)
234                                 return ilgen;
235                         ilgen = new ILGenerator (Module, new DynamicMethodTokenGenerator (this), streamSize);
236                         return ilgen;
237                 }               
238
239                 public override MethodImplAttributes GetMethodImplementationFlags () {
240                         return MethodImplAttributes.IL | MethodImplAttributes.Managed;
241                 }
242
243                 public override ParameterInfo[] GetParameters () {
244                         if (parameters == null)
245                                 return new ParameterInfo [0];
246
247                         ParameterInfo[] retval = new ParameterInfo [parameters.Length];
248                         for (int i = 0; i < parameters.Length; i++) {
249                                 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
250                         }
251                         return retval;
252                 }
253                 
254                 internal override int GetParameterCount ()
255                 {
256                         return parameters == null ? 0 : parameters.Length;
257                 }               
258
259                 internal override Type GetParameterType (int pos) {
260                         return parameters [pos];
261                 }
262
263                 /*
264                 public override object Invoke (object obj, object[] parameters) {
265                         CreateDynMethod ();
266                         if (method == null)
267                                 method = new MonoMethod (mhandle);
268                         return method.Invoke (obj, parameters);
269                 }
270                 */
271
272                 public override object Invoke (object obj, BindingFlags invokeAttr,
273                                                                            Binder binder, object[] parameters,
274                                                                            CultureInfo culture)
275                 {
276                         try {
277                                 CreateDynMethod ();
278                                 if (method == null)
279                                         method = new MonoMethod (mhandle);
280
281                                 return method.Invoke (obj, parameters);
282                         }
283                         catch (MethodAccessException mae) {
284                                 throw new TargetInvocationException ("Method cannot be invoked.", mae);
285                         }
286                 }
287
288                 [MonoTODO("Not implemented")]
289                 public override bool IsDefined (Type attributeType, bool inherit) {
290                         throw new NotImplementedException ();
291                 }
292
293                 public override string ToString () {
294                         string parms = String.Empty;
295                         ParameterInfo[] p = GetParameters ();
296                         for (int i = 0; i < p.Length; ++i) {
297                                 if (i > 0)
298                                         parms = parms + ", ";
299                                 parms = parms + p [i].ParameterType.Name;
300                         }
301                         return ReturnType.Name+" "+Name+"("+parms+")";
302                 }
303
304                 public override MethodAttributes Attributes {
305                         get {
306                                 return attributes;
307                         }
308                 }
309
310                 public override CallingConventions CallingConvention {
311                         get {
312                                 return callingConvention;
313                         }
314                 }
315
316                 public override Type DeclaringType {
317                         get {
318                                 return null;
319                         }
320                 }
321
322                 public bool InitLocals {
323                         get {
324                                 return init_locals;
325                         }
326                         set {
327                                 init_locals = value;
328                         }
329                 }
330
331                 public override RuntimeMethodHandle MethodHandle {
332                         get {
333                                 return mhandle;
334                         }
335                 }
336
337                 public override Module Module {
338                         get {
339                                 return module;
340                         }
341                 }
342
343                 public override string Name {
344                         get {
345                                 return name;
346                         }
347                 }
348
349                 public override Type ReflectedType {
350                         get {
351                                 return null;
352                         }
353                 }
354
355                 [MonoTODO("Not implemented")]
356                 public override ParameterInfo ReturnParameter {
357                         get {
358                                 throw new NotImplementedException ();
359                         }
360                 }
361
362                 public override Type ReturnType {
363                         get {
364                                 return returnType;
365                         }
366                 }
367
368                 [MonoTODO("Not implemented")]
369                 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
370                         get {
371                                 throw new NotImplementedException ();
372                         }
373                 }
374
375 /*
376                 public override int MetadataToken {
377                         get {
378                                 return 0;
379                         }
380                 }
381 */
382
383                 private void RejectIfCreated () {
384                         if (mhandle.Value != IntPtr.Zero)
385                                 throw new InvalidOperationException ("Type definition of the method is complete.");
386                 }
387
388                 internal int AddRef (object reference) {
389                         if (refs == null)
390                                 refs = new object [4];
391                         if (nrefs >= refs.Length - 1) {
392                                 object [] new_refs = new object [refs.Length * 2];
393                                 System.Array.Copy (refs, new_refs, refs.Length);
394                                 refs = new_refs;
395                         }
396                         refs [nrefs] = reference;
397                         /* Reserved by the runtime */
398                         refs [nrefs + 1] = null;
399                         nrefs += 2;
400                         return nrefs - 1;
401                 }
402
403                 // This class takes care of constructing the module in a thread safe manner
404                 class AnonHostModuleHolder {
405                         public static Module anon_host_module;
406
407                         static AnonHostModuleHolder () {
408                                 AssemblyName aname = new AssemblyName ();
409                                 aname.Name = "Anonymously Hosted DynamicMethods Assembly";
410                                 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.Run);
411
412                                 anon_host_module = ab.GetManifestModule ();
413                         }
414
415                         public static Module AnonHostModule {
416                                 get {
417                                         return anon_host_module;
418                                 }
419                         }
420                 }
421         }
422
423         internal class DynamicMethodTokenGenerator : TokenGenerator {
424
425                 private DynamicMethod m;
426
427                 public DynamicMethodTokenGenerator (DynamicMethod m) {
428                         this.m = m;
429                 }
430
431                 public int GetToken (string str) {
432                         return m.AddRef (str);
433                 }
434
435                 public int GetToken (MethodInfo method, Type[] opt_param_types) {
436                         throw new InvalidOperationException ();
437                 }
438
439                 public int GetToken (MemberInfo member, bool create_open_instance) {
440                         return m.AddRef (member);
441                 }
442
443                 public int GetToken (SignatureHelper helper) {
444                         return m.AddRef (helper);
445                 }
446         }
447 }
448