2003-05-19 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / MethodBuilder.cs
1
2 //
3 // System.Reflection.Emit/MethodBuilder.cs
4 //
5 // Author:
6 //   Paolo Molaro (lupus@ximian.com)
7 //
8 // (C) 2001 Ximian, Inc.  http://www.ximian.com
9 //
10
11 using System;
12 using System.Reflection;
13 using System.Reflection.Emit;
14 using System.Globalization;
15 using System.Runtime.CompilerServices;
16 using System.Runtime.InteropServices;
17
18 namespace System.Reflection.Emit {
19
20         public sealed class MethodBuilder : MethodInfo {
21                 private RuntimeMethodHandle mhandle;
22                 private Type rtype;
23                 private Type[] parameters;
24                 private MethodAttributes attrs;
25                 private MethodImplAttributes iattrs;
26                 private string name;
27                 private int table_idx;
28                 private byte[] code;
29                 private ILGenerator ilgen;
30                 private TypeBuilder type;
31                 private ParameterBuilder[] pinfo;
32                 private CustomAttributeBuilder[] cattrs;
33                 private MethodInfo override_method;
34                 private string pi_dll;
35                 private string pi_entry;
36                 private CharSet ncharset;
37                 private CallingConvention native_cc;
38                 private CallingConventions call_conv;
39                 private bool init_locals = true;
40
41                 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
42                         this.name = name;
43                         this.attrs = attributes;
44                         this.call_conv = callingConvention;
45                         this.rtype = returnType;
46                         // The MSDN docs does not specify this, but the MS MethodBuilder
47                         // appends a HasThis flag if the method is not static
48                         if ((attributes & MethodAttributes.Static) == 0)
49                                 this.call_conv |= CallingConventions.HasThis;
50                         if (parameterTypes != null) {
51                                 this.parameters = new Type [parameterTypes.Length];
52                                 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
53                         }
54                         type = tb;
55                         table_idx = get_next_table_index (this, 0x06, true);
56                         //Console.WriteLine ("index for "+name+" set to "+table_idx.ToString());
57                 }
58
59                 internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, 
60                         CallingConventions callingConvention, Type returnType, Type[] parameterTypes, 
61                         String dllName, String entryName, CallingConvention nativeCConv, CharSet nativeCharset) 
62                         : this (tb, name, attributes, callingConvention, returnType, parameterTypes) {
63                         pi_dll = dllName;
64                         pi_entry = entryName;
65                         native_cc = nativeCConv;
66                         ncharset = nativeCharset;
67                 }
68
69                 public bool InitLocals {
70                         get {return init_locals;}
71                         set {init_locals = value;}
72                 }
73
74                 internal TypeBuilder TypeBuilder {
75                         get {return type;}
76                 }
77
78                 public override RuntimeMethodHandle MethodHandle {
79                         get {
80                                 throw NotSupported ();
81                         }
82                 }
83
84                 public override Type ReturnType {get {return rtype;}}
85                 public override Type ReflectedType {get {return type;}}
86                 public override Type DeclaringType {get {return type;}}
87                 public override string Name {get {return name;}}
88                 public override MethodAttributes Attributes {get {return attrs;}}
89                 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
90                         get {return null;}
91                 }
92
93                 public override CallingConventions CallingConvention { 
94                         get { return call_conv; }
95                 }
96
97                 public MethodToken GetToken() {
98                         return new MethodToken(0x06000000 | table_idx);
99                 }
100                 
101                 public override MethodInfo GetBaseDefinition() {
102                         return this;
103                 }
104                 public override MethodImplAttributes GetMethodImplementationFlags() {
105                         return iattrs;
106                 }
107                 public override ParameterInfo[] GetParameters() {
108                         if (parameters == null)
109                                 return null;
110
111                         ParameterInfo[] retval = new ParameterInfo [parameters.Length];
112                         for (int i = 0; i < parameters.Length; i++) {
113                                 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
114                         }
115                         return retval;
116                 }
117
118                 public Module GetModule () {
119                         return type.Module;
120                 }
121
122                 public void CreateMethodBody( byte[] il, int count) {
123                         if ((il != null) && ((count < 0) || (count > il.Length)))
124                                 throw new ArgumentException ("Index was out of range.  Must be non-negative and less than the size of the collection.");
125
126                         if ((code != null) || type.is_created)
127                                 throw new InvalidOperationException ("Type definition of the method is complete.");
128
129                         if (il == null)
130                                 code = null;
131                         else {
132                                 code = new byte [count];
133                                 System.Array.Copy(il, code, count);
134                         }
135                 }
136
137                 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
138                         throw NotSupported ();
139                 }
140                 public override bool IsDefined (Type attribute_type, bool inherit) {
141                         throw NotSupported ();
142                 }
143                 public override object[] GetCustomAttributes( bool inherit) {
144                         throw NotSupported ();
145                 }
146                 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
147                         throw NotSupported ();
148                 }
149                 public ILGenerator GetILGenerator () {
150                         return GetILGenerator (64);
151                 }
152
153                 public ILGenerator GetILGenerator (int size) {
154                         if (((iattrs & MethodImplAttributes.CodeTypeMask) != 
155                                  MethodImplAttributes.IL) ||
156                                 ((iattrs & MethodImplAttributes.ManagedMask) != 
157                                  MethodImplAttributes.Managed))
158                                 throw new InvalidOperationException ("Method body should not exist.");
159                         if (ilgen != null)
160                                 return ilgen;
161                         ilgen = new ILGenerator (this, size);
162                         return ilgen;
163                 }
164                 
165                 public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
166                 {
167                         //
168                         // Extension: Mono allows position == 0 for the return attribute
169                         //
170                         if ((position < 0) || (position > parameters.Length))
171                                 throw new ArgumentOutOfRangeException ("position");
172
173                         RejectIfCreated ();
174
175                         ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
176                         if (pinfo == null)
177                                 pinfo = new ParameterBuilder [parameters.Length + 1];
178                         pinfo [position] = pb;
179                         return pb;
180                 }
181
182                 internal void fixup () {
183                         if (ilgen != null)
184                                 ilgen.label_fixup ();
185                 }
186
187                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
188                         if (customBuilder == null)
189                                 throw new ArgumentNullException ("customBuilder");
190                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
191                         if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
192                                 byte[] data = customBuilder.Data;
193                                 int impla; // the (stupid) ctor takes a short or an int ... 
194                                 impla = (int)data [2];
195                                 impla |= ((int)data [3]) << 8;
196                                 SetImplementationFlags ((MethodImplAttributes)impla);
197                                 return;
198                         }
199                         if (cattrs != null) {
200                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
201                                 cattrs.CopyTo (new_array, 0);
202                                 new_array [cattrs.Length] = customBuilder;
203                                 cattrs = new_array;
204                         } else {
205                                 cattrs = new CustomAttributeBuilder [1];
206                                 cattrs [0] = customBuilder;
207                         }
208                 }
209                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
210                         if (con == null)
211                                 throw new ArgumentNullException ("con");
212                         if (binaryAttribute == null)
213                                 throw new ArgumentNullException ("binaryAttribute");
214                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
215                 }
216                 public void SetImplementationFlags( MethodImplAttributes attributes) {
217                         RejectIfCreated ();
218                         iattrs = attributes;
219                 }
220                 internal override int get_next_table_index (object obj, int table, bool inc) {
221                     return type.get_next_table_index (obj, table, inc);
222                 }
223
224                 internal void set_override (MethodInfo mdecl) {
225                         override_method = mdecl;
226                 }
227
228                 private void RejectIfCreated () {
229                         if (type.is_created)
230                                 throw new InvalidOperationException ("Type definition of the method is complete.");
231                 }
232
233                 private Exception NotSupported () {
234                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
235                 }
236         }
237 }
238