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