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