Get rid of IsCompilerContext, mcs no longer uses SRE.
[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         [ComVisible (true)]
45         [ComDefaultInterface (typeof (_ConstructorBuilder))]
46         [ClassInterface (ClassInterfaceType.None)]
47         public sealed class ConstructorBuilder : ConstructorInfo, _ConstructorBuilder {
48         
49 #pragma warning disable 169, 414
50                 private RuntimeMethodHandle mhandle;
51                 private ILGenerator ilgen;
52                 internal 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                 internal 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 #pragma warning restore 169, 414
65
66                 internal ConstructorBuilder (TypeBuilder tb, MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt)
67                 {
68                         attrs = attributes | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
69                         call_conv = callingConvention;
70                         if (parameterTypes != null) {
71                                 for (int i = 0; i < parameterTypes.Length; ++i)
72                                         if (parameterTypes [i] == null)
73                                                 throw new ArgumentException ("Elements of the parameterTypes array cannot be null", "parameterTypes");
74
75                                 this.parameters = new Type [parameterTypes.Length];
76                                 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
77                         }
78                         type = tb;
79                         this.paramModReq = paramModReq;
80                         this.paramModOpt = paramModOpt;
81                         table_idx = get_next_table_index (this, 0x06, true);
82
83                         ((ModuleBuilder) tb.Module).RegisterToken (this, GetToken ().Token);
84                 }
85
86                 [MonoTODO]
87                 public override CallingConventions CallingConvention {
88                         get {
89                                 return call_conv;
90                         }
91                 }
92
93                 public bool InitLocals {
94                         get {
95                                 return init_locals;
96                         }
97                         set {
98                                 init_locals = value;
99                         }
100                 }
101
102                 internal TypeBuilder TypeBuilder {
103                         get {
104                                 return type;
105                         }
106                 }
107                 
108                 public override MethodImplAttributes GetMethodImplementationFlags ()
109                 {
110                         return iattrs;
111                 }
112
113                 public override ParameterInfo[] GetParameters ()
114                 {
115                         if (!type.is_created)
116                                 throw not_created ();
117
118                         return GetParametersInternal ();
119                 }
120
121                 internal ParameterInfo [] GetParametersInternal ()
122                 {
123                         if (parameters == null)
124                                 return new ParameterInfo [0];
125
126                         ParameterInfo [] retval = new ParameterInfo [parameters.Length];
127                         for (int i = 0; i < parameters.Length; i++)
128                                 retval [i] = new ParameterInfo (pinfo == null ? null
129                                         : pinfo [i + 1], parameters [i], this, i + 1);
130
131                         return retval;
132                 }
133                 
134                 internal override int GetParameterCount ()
135                 {
136                         if (parameters == null)
137                                 return 0;
138                         
139                         return parameters.Length;
140                 }
141
142                 internal override Type GetParameterType (int pos) {
143                         return parameters [pos];
144                 }
145                 
146                 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
147                 {
148                         throw not_supported ();
149                 }
150
151                 public override object Invoke (BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
152                 {
153                         throw not_supported ();
154                 }
155
156                 public override RuntimeMethodHandle MethodHandle {
157                         get {
158                                 throw not_supported ();
159                         }
160                 }
161
162                 public override MethodAttributes Attributes {
163                         get {
164                                 return attrs;
165                         }
166                 }
167
168                 public override Type ReflectedType {
169                         get {
170                                 return type;
171                         }
172                 }
173
174                 public override Type DeclaringType {
175                         get {
176                                 return type;
177                         }
178                 }
179
180 #if NET_4_0
181                 [Obsolete]
182 #endif
183                 public Type ReturnType {
184                         get {
185                                 return null;
186                         }
187                 }
188
189                 public override string Name {
190                         get {
191                                 return (attrs & MethodAttributes.Static) != 0 ? ConstructorInfo.TypeConstructorName : ConstructorInfo.ConstructorName;
192                         }
193                 }
194
195                 public string Signature {
196                         get {
197                                 return "constructor signature";
198                         }
199                 }
200
201                 public void AddDeclarativeSecurity (SecurityAction action, PermissionSet pset)
202                 {
203 #if !NET_2_1
204                         if (pset == null)
205                                 throw new ArgumentNullException ("pset");
206                         if ((action == SecurityAction.RequestMinimum) ||
207                                 (action == SecurityAction.RequestOptional) ||
208                                 (action == SecurityAction.RequestRefuse))
209                                 throw new ArgumentOutOfRangeException ("action", "Request* values are not permitted");
210
211                         RejectIfCreated ();
212
213                         if (permissions != null) {
214                                 /* Check duplicate actions */
215                                 foreach (RefEmitPermissionSet set in permissions)
216                                         if (set.action == action)
217                                                 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
218
219                                 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
220                                 permissions.CopyTo (new_array, 0);
221                                 permissions = new_array;
222                         }
223                         else
224                                 permissions = new RefEmitPermissionSet [1];
225
226                         permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
227                         attrs |= MethodAttributes.HasSecurity;
228 #endif
229                 }
230
231                 public ParameterBuilder DefineParameter (int iSequence, ParameterAttributes attributes, string strParamName)
232                 {
233                         if (iSequence < 1 || iSequence > GetParameterCount ())
234                                 throw new ArgumentOutOfRangeException ("iSequence");
235                         if (type.is_created)
236                                 throw not_after_created ();
237
238                         ParameterBuilder pb = new ParameterBuilder (this, iSequence, attributes, strParamName);
239                         if (pinfo == null)
240                                 pinfo = new ParameterBuilder [parameters.Length + 1];
241                         pinfo [iSequence] = pb;
242                         return pb;
243                 }
244
245                 public override bool IsDefined (Type attributeType, bool inherit)
246                 {
247                         throw not_supported ();
248                 }
249
250                 public override object [] GetCustomAttributes (bool inherit)
251                 {
252                         throw not_supported ();
253                 }
254
255                 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
256                 {
257                         throw not_supported ();
258                 }
259
260                 public ILGenerator GetILGenerator ()
261                 {
262                         return GetILGenerator (64);
263                 }
264
265                 public ILGenerator GetILGenerator (int streamSize)
266                 {
267                         if (ilgen != null)
268                                 return ilgen;
269                         ilgen = new ILGenerator (type.Module, ((ModuleBuilder)type.Module).GetTokenGenerator (), streamSize);
270                         return ilgen;
271                 }
272
273                 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
274                 {
275                         if (customBuilder == null)
276                                 throw new ArgumentNullException ("customBuilder");
277
278                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
279                         if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
280                                 byte[] data = customBuilder.Data;
281                                 int impla; // the (stupid) ctor takes a short or an int ... 
282                                 impla = (int)data [2];
283                                 impla |= ((int)data [3]) << 8;
284                                 SetImplementationFlags ((MethodImplAttributes)impla);
285                                 return;
286                         }
287                         if (cattrs != null) {
288                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
289                                 cattrs.CopyTo (new_array, 0);
290                                 new_array [cattrs.Length] = customBuilder;
291                                 cattrs = new_array;
292                         } else {
293                                 cattrs = new CustomAttributeBuilder [1];
294                                 cattrs [0] = customBuilder;
295                         }
296                 }
297
298                 [ComVisible (true)]
299                 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
300                 {
301                         if (con == null)
302                                 throw new ArgumentNullException ("con");
303                         if (binaryAttribute == null)
304                                 throw new ArgumentNullException ("binaryAttribute");
305
306                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
307                 }
308
309                 public void SetImplementationFlags (MethodImplAttributes attributes)
310                 {
311                         if (type.is_created)
312                                 throw not_after_created ();
313
314                         iattrs = attributes;
315                 }
316
317                 public Module GetModule ()
318                 {
319                         return type.Module;
320                 }
321
322                 public MethodToken GetToken ()
323                 {
324                         return new MethodToken (0x06000000 | table_idx);
325                 }
326
327                 [MonoTODO]
328                 public void SetSymCustomAttribute (string name, byte[] data)
329                 {
330                         if (type.is_created)
331                                 throw not_after_created ();
332                 }
333
334                 public override Module Module {
335                         get {
336                                 return base.Module;
337                         }
338                 }
339
340                 public override string ToString ()
341                 {
342                         return "ConstructorBuilder ['" + type.Name + "']";
343                 }
344
345                 internal void fixup ()
346                 {
347                         if (((attrs & (MethodAttributes.Abstract | MethodAttributes.PinvokeImpl)) == 0) && ((iattrs & (MethodImplAttributes.Runtime | MethodImplAttributes.InternalCall)) == 0)) {
348                         if ((ilgen == null) || (ilgen.ILOffset == 0))
349                                 throw new InvalidOperationException ("Method '" + Name + "' does not have a method body.");
350                         }
351                         if (ilgen != null)
352                                 ilgen.label_fixup ();
353                 }
354                 
355                 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
356                 {
357                         if (ilgen != null && ilgen.HasDebugInfo) {
358                                 SymbolToken token = new SymbolToken (GetToken().Token);
359                                 symbolWriter.OpenMethod (token);
360                                 symbolWriter.SetSymAttribute (token, "__name", System.Text.Encoding.UTF8.GetBytes (Name));
361                                 ilgen.GenerateDebugInfo (symbolWriter);
362                                 symbolWriter.CloseMethod ();
363                         }
364                 }
365
366                 internal override int get_next_table_index (object obj, int table, bool inc)
367                 {
368                         return type.get_next_table_index (obj, table, inc);
369                 }
370
371                 private void RejectIfCreated ()
372                 {
373                         if (type.is_created)
374                                 throw new InvalidOperationException ("Type definition of the method is complete.");
375                 }
376
377                 private Exception not_supported ()
378                 {
379                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
380                 }
381
382                 private Exception not_after_created ()
383                 {
384                         return new InvalidOperationException ("Unable to change after type has been created.");
385                 }
386
387                 private Exception not_created ()
388                 {
389                         return new NotSupportedException ("The type is not yet created.");
390                 }
391
392                 void _ConstructorBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
393                 {
394                         throw new NotImplementedException ();
395                 }
396
397                 void _ConstructorBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
398                 {
399                         throw new NotImplementedException ();
400                 }
401
402                 void _ConstructorBuilder.GetTypeInfoCount (out uint pcTInfo)
403                 {
404                         throw new NotImplementedException ();
405                 }
406
407                 void _ConstructorBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
408                 {
409                         throw new NotImplementedException ();
410                 }
411         }
412 }