(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / System.Reflection.Emit / DynamicMethod.cs
blobffa6ba93bb893c50fd3649c34030aa2eee300dd7
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 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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.
34 #if NET_2_0 || BOOTSTRAP_NET_2_0
36 using System;
37 using System.Reflection;
38 using System.Reflection.Emit;
39 using System.Globalization;
40 using System.Runtime.CompilerServices;
41 using System.Runtime.InteropServices;
43 namespace System.Reflection.Emit {
45 public sealed class DynamicMethod : MethodInfo {
46 #region Sync with reflection.h
47 private RuntimeMethodHandle mhandle;
48 private string name;
49 private Type returnType;
50 private Type[] parameters;
51 private MethodAttributes attributes;
52 private CallingConventions callingConvention;
53 private Module module;
54 private bool skipVisibility;
55 private bool init_locals = true;
56 private ILGenerator ilgen;
57 private int nrefs;
58 private object[] refs;
59 #endregion
60 private Delegate deleg;
61 private MonoMethod method;
62 private ParameterBuilder[] pinfo;
64 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Module m) : this (name, returnType, parameterTypes, m, false) {
67 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner) : this (name, returnType, parameterTypes, owner, false) {
70 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, m, skipVisibility) {
73 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner, skipVisibility) {
76 public DynamicMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility) : this (name, attributes, callingConvention, returnType, parameterTypes, owner.Module, skipVisibility) {
79 public DynamicMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility) {
80 if (name == null)
81 throw new ArgumentNullException ("name");
82 if (name == String.Empty)
83 throw new ArgumentException ("Name can't be empty", "name");
84 if (returnType == null)
85 throw new ArgumentNullException ("returnType");
86 if (m == null)
87 throw new ArgumentNullException ("m");
88 if (returnType.IsByRef)
89 throw new ArgumentException ("Return type can't be a byref type", "returnType");
90 if (parameterTypes != null) {
91 for (int i = 0; i < parameterTypes.Length; ++i)
92 if (parameterTypes [i] == null)
93 throw new ArgumentException ("Parameter " + i + " is null", "parameterTypes");
96 this.name = name;
97 this.attributes = attributes | MethodAttributes.Static;
98 this.callingConvention = callingConvention;
99 this.returnType = returnType;
100 this.parameters = parameterTypes;
101 this.module = m;
102 this.skipVisibility = skipVisibility;
105 [MethodImplAttribute(MethodImplOptions.InternalCall)]
106 private extern void create_dynamic_method (DynamicMethod m);
108 private void CreateDynMethod () {
109 if (mhandle.Value == IntPtr.Zero)
110 create_dynamic_method (this);
113 public Delegate CreateDelegate (Type delegateType) {
114 if (delegateType == null)
115 throw new ArgumentNullException ("delegateType");
116 if (deleg != null)
117 return deleg;
119 CreateDynMethod ();
121 deleg = Delegate.CreateDelegate (delegateType, this);
122 return deleg;
125 [MonoTODO]
126 public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
129 // Extension: Mono allows position == 0 for the return attribute
131 if ((position < 0) || (position > parameters.Length))
132 throw new ArgumentOutOfRangeException ("position");
134 RejectIfCreated ();
136 throw new NotImplementedException ();
139 public override MethodInfo GetBaseDefinition () {
140 return this;
143 [MonoTODO]
144 public override object[] GetCustomAttributes (bool inherit) {
145 throw new NotImplementedException ();
148 [MonoTODO]
149 public override object[] GetCustomAttributes (Type attributeType,
150 bool inherit) {
151 throw new NotImplementedException ();
154 public ILGenerator GetILGenerator () {
155 return GetILGenerator (64);
158 public ILGenerator GetILGenerator (int size) {
159 if (((GetMethodImplementationFlags () & MethodImplAttributes.CodeTypeMask) !=
160 MethodImplAttributes.IL) ||
161 ((GetMethodImplementationFlags () & MethodImplAttributes.ManagedMask) !=
162 MethodImplAttributes.Managed))
163 throw new InvalidOperationException ("Method body should not exist.");
164 if (ilgen != null)
165 return ilgen;
166 ilgen = new ILGenerator (Module, new DynamicMethodTokenGenerator (this), size);
167 return ilgen;
170 public override MethodImplAttributes GetMethodImplementationFlags () {
171 return MethodImplAttributes.IL | MethodImplAttributes.Managed;
174 public override ParameterInfo[] GetParameters () {
175 if (parameters == null)
176 return new ParameterInfo [0];
178 ParameterInfo[] retval = new ParameterInfo [parameters.Length];
179 for (int i = 0; i < parameters.Length; i++) {
180 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
182 return retval;
185 public override object Invoke (object obj, object[] parameters) {
186 CreateDynMethod ();
187 if (method == null)
188 method = new MonoMethod (mhandle);
189 return method.Invoke (obj, parameters);
192 public override object Invoke (object obj, BindingFlags invokeAttr,
193 Binder binder, object[] parameters,
194 CultureInfo culture) {
195 CreateDynMethod ();
196 if (method == null)
197 method = new MonoMethod (mhandle);
198 return method.Invoke (obj, parameters);
201 [MonoTODO]
202 public override bool IsDefined (Type attributeType, bool inherit) {
203 throw new NotImplementedException ();
206 public override string ToString () {
207 string parms = "";
208 ParameterInfo[] p = GetParameters ();
209 for (int i = 0; i < p.Length; ++i) {
210 if (i > 0)
211 parms = parms + ", ";
212 parms = parms + p [i].ParameterType.Name;
214 return ReturnType.Name+" "+Name+"("+parms+")";
217 public override MethodAttributes Attributes {
218 get {
219 return attributes;
223 public override CallingConventions CallingConvention {
224 get {
225 return callingConvention;
229 public override Type DeclaringType {
230 get {
231 return null;
235 public bool InitLocals {
236 get {
237 return init_locals;
239 set {
240 init_locals = value;
244 public override RuntimeMethodHandle MethodHandle {
245 get {
246 return mhandle;
250 public override Module Module {
251 get {
252 return module;
256 public override string Name {
257 get {
258 return name;
262 public override Type ReflectedType {
263 get {
264 return null;
268 [MonoTODO]
269 public ParameterInfo ReturnParameter {
270 get {
271 throw new NotImplementedException ();
275 public override Type ReturnType {
276 get {
277 return returnType;
281 [MonoTODO]
282 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
283 get {
284 throw new NotImplementedException ();
288 public override int MetadataToken {
289 get {
290 return 0;
294 private void RejectIfCreated () {
295 if (mhandle.Value != IntPtr.Zero)
296 throw new InvalidOperationException ("Type definition of the method is complete.");
299 private Exception NotSupported () {
300 return new NotSupportedException ("The invoked member is not supported on a dynamic method.");
303 internal int AddRef (object reference) {
304 if (refs == null)
305 refs = new object [4];
306 if (nrefs >= refs.Length) {
307 object [] new_refs = new object [refs.Length * 2];
308 System.Array.Copy (refs, new_refs, refs.Length);
309 refs = new_refs;
311 refs [nrefs] = reference;
312 nrefs ++;
313 return nrefs;
317 internal class DynamicMethodTokenGenerator : TokenGenerator {
319 private DynamicMethod m;
321 public DynamicMethodTokenGenerator (DynamicMethod m) {
322 this.m = m;
325 public int GetToken (string str) {
326 return m.AddRef (str);
329 public int GetToken (MethodInfo method, Type[] opt_param_types) {
330 throw new InvalidOperationException ();
333 public int GetToken (MemberInfo member) {
334 return m.AddRef (member);
337 public int GetToken (SignatureHelper helper) {
338 return m.AddRef (helper);
343 #endif