Switch to compiler-tester
[mono.git] / mcs / class / corlib / System.Reflection.Emit / ConstructorBuilder.cs
1 //
2 // System.Reflection.Emit/ConstructorBuilder.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 using System;
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.InteropServices;
40 using System.Diagnostics.SymbolStore;
41
42 namespace System.Reflection.Emit {
43
44 #if NET_2_0
45         [ComVisible (true)]
46         [ClassInterfaceAttribute (ClassInterfaceType.None)]
47         [ComDefaultInterfaceAttribute (typeof (_ConstructorBuilder))]
48 #endif
49         public sealed class ConstructorBuilder : ConstructorInfo {
50                 private RuntimeMethodHandle mhandle;
51                 private ILGenerator ilgen;
52                 private Type[] parameters;
53                 private MethodAttributes attrs;
54                 private MethodImplAttributes iattrs;
55                 private int table_idx;
56                 private CallingConventions call_conv;
57                 private TypeBuilder type;
58                 private ParameterBuilder[] pinfo;
59                 private CustomAttributeBuilder[] cattrs;
60                 private bool init_locals = true;
61                 private Type[][] paramModReq;
62                 private Type[][] paramModOpt;
63                 private RefEmitPermissionSet[] permissions;
64
65                 internal ConstructorBuilder (TypeBuilder tb, MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt) {
66                         attrs = attributes | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
67                         call_conv = callingConvention;
68                         if (parameterTypes != null) {
69                                 for (int i = 0; i < parameterTypes.Length; ++i)
70                                         if (parameterTypes [i] == null)
71                                                 throw new ArgumentException ("Elements of the parameterTypes array cannot be null", "parameterTypes");
72
73                                 this.parameters = new Type [parameterTypes.Length];
74                                 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
75                         }
76                         type = tb;
77                         table_idx = get_next_table_index (this, 0x06, true);
78                         this.paramModReq = paramModReq;
79                         this.paramModOpt = paramModOpt;
80                 }
81                 
82                 public bool InitLocals {
83                         get {return init_locals;}
84                         set {init_locals = value;}
85                 }
86
87                 internal TypeBuilder TypeBuilder {
88                         get {return type;}
89                 }
90                 
91                 public override MethodImplAttributes GetMethodImplementationFlags() {
92                         return iattrs;
93                 }
94                 public override ParameterInfo[] GetParameters() {
95                         if (parameters == null)
96                                 return null;
97
98                         ParameterInfo[] retval = new ParameterInfo [parameters.Length];
99                         for (int i = 0; i < parameters.Length; i++) {
100                                 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i+1], parameters [i], this, i + 1);
101                         }
102
103                         return retval;
104                 }
105                 
106                 internal override int GetParameterCount ()
107                 {
108                         if (parameters == null)
109                                 return 0;
110                         
111                         return parameters.Length;
112                 }
113                 
114                 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
115                         throw not_supported ();
116                 }
117                 public override object Invoke(BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture) {
118                         throw not_supported ();
119                 }
120
121                 public override RuntimeMethodHandle MethodHandle { 
122                         get {
123                                 return mhandle;
124                         }
125                 }
126
127                 public override MethodAttributes Attributes { 
128                         get {return attrs;} 
129                 }
130                 public override Type ReflectedType { get {return type;}}
131                 public override Type DeclaringType { get {return type;}}
132                 public Type ReturnType { get {return null;}}
133                 public override string Name { 
134                         get {return (attrs & MethodAttributes.Static) != 0 ? ".cctor" : ".ctor";}
135                 }
136                 public string Signature {
137                         get {return "constructor signature";}
138                 }
139
140                 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
141                         if (pset == null)
142                                 throw new ArgumentNullException ("pset");
143                         if ((action == SecurityAction.RequestMinimum) ||
144                                 (action == SecurityAction.RequestOptional) ||
145                                 (action == SecurityAction.RequestRefuse))
146                                 throw new ArgumentException ("Request* values are not permitted", "action");
147
148                         RejectIfCreated ();
149
150                         if (permissions != null) {
151                                 /* Check duplicate actions */
152                                 foreach (RefEmitPermissionSet set in permissions)
153                                         if (set.action == action)
154                                                 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
155
156                                 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
157                                 permissions.CopyTo (new_array, 0);
158                                 permissions = new_array;
159                         }
160                         else
161                                 permissions = new RefEmitPermissionSet [1];
162
163                         permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
164                         attrs |= MethodAttributes.HasSecurity;
165                 }
166
167                 public ParameterBuilder DefineParameter(int iSequence, ParameterAttributes attributes, string strParamName)
168                 {
169                         if ((iSequence < 1) || (iSequence > parameters.Length))
170                                 throw new ArgumentOutOfRangeException ("iSequence");
171                         if (type.is_created)
172                                 throw not_after_created ();
173
174                         ParameterBuilder pb = new ParameterBuilder (this, iSequence, attributes, strParamName);
175                         if (pinfo == null)
176                                 pinfo = new ParameterBuilder [parameters.Length + 1];
177                         pinfo [iSequence] = pb;
178                         return pb;
179                 }
180
181                 public override bool IsDefined (Type attribute_type, bool inherit) {
182                         throw not_supported ();
183                 }
184
185                 public override object [] GetCustomAttributes (bool inherit) {
186                         throw not_supported ();
187                 }
188
189                 public override object [] GetCustomAttributes (Type attribute_type, bool inherit) {
190                         throw not_supported ();
191                 }
192
193                 public ILGenerator GetILGenerator () {
194                         return GetILGenerator (64);
195                 }
196
197 #if NET_2_0
198                 public
199 #else
200                 internal 
201 #endif
202                 ILGenerator GetILGenerator (int size) {
203                         ilgen = new ILGenerator (type.Module, ((ModuleBuilder)type.Module).GetTokenGenerator (), size);
204                         return ilgen;
205                 }
206
207                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
208                         if (customBuilder == null)
209                                 throw new ArgumentNullException ("customBuilder");
210
211                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
212                         if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
213                                 byte[] data = customBuilder.Data;
214                                 int impla; // the (stupid) ctor takes a short or an int ... 
215                                 impla = (int)data [2];
216                                 impla |= ((int)data [3]) << 8;
217                                 SetImplementationFlags ((MethodImplAttributes)impla);
218                                 return;
219                         }
220                         if (cattrs != null) {
221                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
222                                 cattrs.CopyTo (new_array, 0);
223                                 new_array [cattrs.Length] = customBuilder;
224                                 cattrs = new_array;
225                         } else {
226                                 cattrs = new CustomAttributeBuilder [1];
227                                 cattrs [0] = customBuilder;
228                         }
229                 }
230
231 #if NET_2_0
232                 [ComVisible (true)]
233 #endif
234                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
235                         if (con == null)
236                                 throw new ArgumentNullException ("con");
237                         if (binaryAttribute == null)
238                                 throw new ArgumentNullException ("binaryAttribute");
239
240                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
241                 }
242                 public void SetImplementationFlags( MethodImplAttributes attributes) {
243                         if (type.is_created)
244                                 throw not_after_created ();
245
246                         iattrs = attributes;
247                 }
248                 public Module GetModule() {
249                         return type.Module;
250                 }
251                 public MethodToken GetToken() {
252                         return new MethodToken (0x06000000 | table_idx);
253                 }
254
255                 [MonoTODO]
256                 public void SetSymCustomAttribute( string name, byte[] data) {
257                         if (type.is_created)
258                                 throw not_after_created ();
259                 }
260
261 #if NET_2_0 || BOOTSTRAP_NET_2_0
262                 public override bool Mono_IsInflatedMethod {
263                         get {
264                                 return false;
265                         }
266                 }
267
268                 public override bool HasGenericParameters {
269                         get {
270                                 return false;
271                         }
272                 }
273
274                 public override Module Module {
275                         get {
276                                 return base.Module;
277                         }
278                 }
279 #endif
280
281                 public override string ToString() {
282                         return "ConstructorBuilder ['" + type.Name + "']";
283                 }
284
285                 internal void fixup () {
286                         if (((attrs & (MethodAttributes.Abstract | MethodAttributes.PinvokeImpl)) == 0) && ((iattrs & (MethodImplAttributes.Runtime | MethodImplAttributes.InternalCall)) == 0)) {
287                         if ((ilgen == null) || (ILGenerator.Mono_GetCurrentOffset (ilgen) == 0))
288                                 throw new InvalidOperationException ("Method '" + Name + "' does not have a method body.");
289                         }
290                         if (ilgen != null)
291                                 ilgen.label_fixup ();
292                 }
293                 
294                 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
295                 {
296                         if (ilgen != null && ilgen.HasDebugInfo) {
297                                 SymbolToken token = new SymbolToken (GetToken().Token);
298                                 symbolWriter.OpenMethod (token);
299                                 symbolWriter.SetSymAttribute (token, "__name", System.Text.Encoding.UTF8.GetBytes (Name));
300                                 ilgen.GenerateDebugInfo (symbolWriter);
301                                 symbolWriter.CloseMethod ();
302                         }
303                 }
304
305                 internal override int get_next_table_index (object obj, int table, bool inc) {
306                         return type.get_next_table_index (obj, table, inc);
307                 }
308
309                 private void RejectIfCreated () {
310                         if (type.is_created)
311                                 throw new InvalidOperationException ("Type definition of the method is complete.");
312                 }
313
314                 private Exception not_supported () {
315                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
316                 }
317
318                 private Exception not_after_created () {
319                         return new InvalidOperationException ("Unable to change after type has been created.");
320                 }
321         }
322 }