[corlib] Fixed StringBuilder construction bugs in marshalling caused by changes to...
[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 override ParameterInfo [] GetParametersInternal ()
124                 {
125                         if (parameters == null)
126                                 return EmptyArray<ParameterInfo>.Value;
127
128                         ParameterInfo [] retval = new ParameterInfo [parameters.Length];
129                         for (int i = 0; i < parameters.Length; i++)
130                                 retval [i] = ParameterInfo.New (pinfo == null ? null
131                                         : pinfo [i + 1], parameters [i], this, i + 1);
132
133                         return retval;
134                 }
135                 
136                 internal override int GetParametersCount ()
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                 [Obsolete]
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 > GetParametersCount ())
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 (this);
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 }
413 #endif