Merge pull request #1216 from AndreyAkinshin/master
[mono.git] / mcs / class / corlib / System.Reflection.Emit / TypeBuilder.cs
1 //
2 // System.Reflection.Emit.TypeBuilder.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //   Marek Safar (marek.safar@gmail.com)
7 //
8 // (C) 2001 Ximian, Inc.  http://www.ximian.com
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 #if !FULL_AOT_RUNTIME
35 using System;
36 using System.Text;
37 using System.Reflection;
38 using System.Reflection.Emit;
39 using System.Runtime.CompilerServices;
40 using System.Runtime.InteropServices;
41 using System.Globalization;
42 using System.Collections;
43 using System.Security;
44 using System.Security.Permissions;
45 using System.Diagnostics.SymbolStore;
46
47 #if !NET_4_5
48 using TypeInfo = System.Type;
49 #endif
50
51 namespace System.Reflection.Emit
52 {
53         [ComVisible (true)]
54         [ComDefaultInterface (typeof (_TypeBuilder))]
55         [ClassInterface (ClassInterfaceType.None)]
56         [StructLayout (LayoutKind.Sequential)]
57         public sealed class TypeBuilder : TypeInfo, _TypeBuilder
58         {
59 #pragma warning disable 169             
60                 #region Sync with reflection.h
61                 private string tname;
62                 private string nspace;
63                 private Type parent;
64                 private Type nesting_type;
65                 internal Type[] interfaces;
66                 internal int num_methods;
67                 internal MethodBuilder[] methods;
68                 internal ConstructorBuilder[] ctors;
69                 internal PropertyBuilder[] properties;
70                 internal int num_fields;
71                 internal FieldBuilder[] fields;
72                 internal EventBuilder[] events;
73                 private CustomAttributeBuilder[] cattrs;
74                 internal TypeBuilder[] subtypes;
75                 internal TypeAttributes attrs;
76                 private int table_idx;
77                 private ModuleBuilder pmodule;
78                 private int class_size;
79                 private PackingSize packing_size;
80                 private IntPtr generic_container;
81                 private GenericTypeParameterBuilder[] generic_params;
82                 private RefEmitPermissionSet[] permissions;
83                 private TypeInfo created;
84                 #endregion
85 #pragma warning restore 169             
86                 
87                 string fullname;
88                 bool createTypeCalled;
89                 private Type underlying_type;
90
91                 public const int UnspecifiedTypeSize = 0;
92                 
93                 protected override TypeAttributes GetAttributeFlagsImpl ()
94                 {
95                         return attrs;
96                 }
97                 
98                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
99                 private extern void setup_internal_class (TypeBuilder tb);
100                 
101                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
102                 private extern void create_internal_class (TypeBuilder tb);
103                 
104                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
105                 private extern void setup_generic_class ();
106
107                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
108                 private extern void create_generic_class ();
109
110                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
111                 private extern EventInfo get_event_info (EventBuilder eb);
112
113                 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr, int table_idx)
114                 {
115                         this.parent = null;
116                         this.attrs = attr;
117                         this.class_size = UnspecifiedTypeSize;
118                         this.table_idx = table_idx;
119                         fullname = this.tname = table_idx == 1 ? "<Module>" : "type_" + table_idx.ToString ();
120                         this.nspace = String.Empty;
121                         pmodule = mb;
122                         setup_internal_class (this);
123                 }
124
125                 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type)
126                 {
127                         int sep_index;
128                         this.parent = parent;
129                         this.attrs = attr;
130                         this.class_size = type_size;
131                         this.packing_size = packing_size;
132                         this.nesting_type = nesting_type;
133
134                         check_name ("fullname", name);
135
136                         if (parent == null && (attr & TypeAttributes.Interface) != 0 && (attr & TypeAttributes.Abstract) == 0)
137                                 throw new InvalidOperationException ("Interface must be declared abstract.");
138
139                         sep_index = name.LastIndexOf('.');
140                         if (sep_index != -1) {
141                                 this.tname = name.Substring (sep_index + 1);
142                                 this.nspace = name.Substring (0, sep_index);
143                         } else {
144                                 this.tname = name;
145                                 this.nspace = String.Empty;
146                         }
147                         if (interfaces != null) {
148                                 this.interfaces = new Type[interfaces.Length];
149                                 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
150                         }
151                         pmodule = mb;
152
153                         if (((attr & TypeAttributes.Interface) == 0) && (parent == null))
154                                 this.parent = typeof (object);
155
156                         // skip .<Module> ?
157                         table_idx = mb.get_next_table_index (this, 0x02, true);
158                         setup_internal_class (this);
159                         fullname = GetFullName ();
160                 }
161
162                 public override Assembly Assembly {
163                         get {return pmodule.Assembly;}
164                 }
165
166                 public override string AssemblyQualifiedName {
167                         get {
168                                 return fullname + ", " + Assembly.FullName;
169                         }
170                 }
171
172                 public override Type BaseType {
173                         get {
174                                 return parent;
175                         }
176                 }
177
178                 public override Type DeclaringType {
179                         get { return nesting_type; }
180                 }
181
182                 [ComVisible (true)]
183                 public override bool IsSubclassOf (Type c)
184                 {
185                         Type t;
186                         if (c == null)
187                                 return false;
188                         if (c == this)
189                                 return false;
190                         t = parent;
191                         while (t != null) {
192                                 if (c == t)
193                                         return true;
194                                 t = t.BaseType;
195                         }
196                         return false;
197                 }
198
199                 public override Type UnderlyingSystemType {
200                         get {
201                                 if (is_created)
202                                         return created.UnderlyingSystemType;
203
204                                 if (IsEnum) {
205                                         if (underlying_type != null)
206                                                 return underlying_type;
207                                         throw new InvalidOperationException (
208                                                 "Enumeration type is not defined.");
209                                 }
210
211                                 return this;
212                         }
213                 }
214
215                 string GetFullName ()
216                 {
217                         if (nesting_type != null)
218                                 return String.Concat (nesting_type.FullName, "+", tname);
219                         if ((nspace != null) && (nspace.Length > 0))
220                                 return String.Concat (nspace, ".", tname);
221                         return tname;
222                 }
223         
224                 public override string FullName {
225                         get {
226                                 return fullname;
227                         }
228                 }
229         
230                 public override Guid GUID {
231                         get {
232                                 check_created ();
233                                 return created.GUID;
234                         }
235                 }
236
237                 public override Module Module {
238                         get {return pmodule;}
239                 }
240
241                 public override string Name {
242                         get {return tname;}
243                 }
244
245                 public override string Namespace {
246                         get {return nspace;}
247                 }
248
249                 public PackingSize PackingSize {
250                         get {return packing_size;}
251                 }
252
253                 public int Size {
254                         get { return class_size; }
255                 }
256
257                 public override Type ReflectedType {
258                         get { return nesting_type; }
259                 }
260
261                 public void AddDeclarativeSecurity (SecurityAction action, PermissionSet pset)
262                 {
263 #if !NET_2_1
264                         if (pset == null)
265                                 throw new ArgumentNullException ("pset");
266                         if ((action == SecurityAction.RequestMinimum) ||
267                                 (action == SecurityAction.RequestOptional) ||
268                                 (action == SecurityAction.RequestRefuse))
269                                 throw new ArgumentOutOfRangeException ("Request* values are not permitted", "action");
270
271                         check_not_created ();
272
273                         if (permissions != null) {
274                                 /* Check duplicate actions */
275                                 foreach (RefEmitPermissionSet set in permissions)
276                                         if (set.action == action)
277                                                 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
278
279                                 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
280                                 permissions.CopyTo (new_array, 0);
281                                 permissions = new_array;
282                         }
283                         else
284                                 permissions = new RefEmitPermissionSet [1];
285
286                         permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
287                         attrs |= TypeAttributes.HasSecurity;
288 #endif
289                 }
290
291                 [ComVisible (true)]
292                 public void AddInterfaceImplementation (Type interfaceType)
293                 {
294                         if (interfaceType == null)
295                                 throw new ArgumentNullException ("interfaceType");
296                         check_not_created ();
297
298                         if (interfaces != null) {
299                                 // Check for duplicates
300                                 foreach (Type t in interfaces)
301                                         if (t == interfaceType)
302                                                 return;
303
304                                 Type[] ifnew = new Type [interfaces.Length + 1];
305                                 interfaces.CopyTo (ifnew, 0);
306                                 ifnew [interfaces.Length] = interfaceType;
307                                 interfaces = ifnew;
308                         } else {
309                                 interfaces = new Type [1];
310                                 interfaces [0] = interfaceType;
311                         }
312                 }
313
314                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
315                                                                        CallingConventions callConvention, Type[] types,
316                                                                        ParameterModifier[] modifiers)
317                 {
318                         check_created ();
319
320                         if (created == typeof (object)) {
321                                 /* 
322                                  * This happens when building corlib. Calling created.GetConstructor 
323                                  * would return constructors from the real mscorlib, instead of the
324                                  * newly built one.
325                                  */
326
327                                 if (ctors == null)
328                                         return null;
329  
330                                 ConstructorBuilder found = null;
331                                 int count = 0;
332                         
333                                 foreach (ConstructorBuilder cb in ctors) {
334                                         if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
335                                                 continue;
336                                         found = cb;
337                                         count++;
338                                 }
339
340                                 if (count == 0)
341                                         return null;
342                                 if (types == null) {
343                                         if (count > 1)
344                                                 throw new AmbiguousMatchException ();
345                                         return found;
346                                 }
347                                 MethodBase[] match = new MethodBase [count];
348                                 if (count == 1)
349                                         match [0] = found;
350                                 else {
351                                         count = 0;
352                                         foreach (ConstructorInfo m in ctors) {
353                                                 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
354                                                         continue;
355                                                 match [count++] = m;
356                                         }
357                                 }
358                                 if (binder == null)
359                                         binder = Binder.DefaultBinder;
360                                 return (ConstructorInfo) binder.SelectMethod (bindingAttr, match,
361                                                                                                                           types, modifiers);
362                         }
363
364                         return created.GetConstructor (bindingAttr, binder, 
365                                 callConvention, types, modifiers);
366                 }
367
368                 public override bool IsDefined (Type attributeType, bool inherit)
369                 {
370                         if (!is_created)
371                                 throw new NotSupportedException ();
372                         /*
373                          * MS throws NotSupported here, but we can't because some corlib
374                          * classes make calls to IsDefined.
375                          */
376                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
377                 }
378                 
379                 public override object[] GetCustomAttributes(bool inherit)
380                 {
381                         check_created ();
382
383                         return created.GetCustomAttributes (inherit);
384                 }
385                 
386                 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
387                 {
388                         check_created ();
389
390                         return created.GetCustomAttributes (attributeType, inherit);
391                 }
392
393                 public TypeBuilder DefineNestedType (string name)
394                 {
395                         return DefineNestedType (name, TypeAttributes.NestedPrivate,
396                                 pmodule.assemblyb.corlib_object_type, null);
397                 }
398
399                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr)
400                 {
401                         return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
402                 }
403
404                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent)
405                 {
406                         return DefineNestedType (name, attr, parent, null);
407                 }
408
409                 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
410                                                       PackingSize packSize, int typeSize)
411                 {
412                         // Visibility must be NestedXXX
413                         /* This breaks mcs
414                         if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
415                                 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
416                                 throw new ArgumentException ("attr", "Bad type flags for nested type.");
417                         */
418                         if (interfaces != null)
419                                 foreach (Type iface in interfaces)
420                                         if (iface == null)
421                                                 throw new ArgumentNullException ("interfaces");
422
423                         TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packSize, typeSize, this);
424                         res.fullname = res.GetFullName ();
425                         pmodule.RegisterTypeName (res, res.fullname);
426                         if (subtypes != null) {
427                                 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
428                                 System.Array.Copy (subtypes, new_types, subtypes.Length);
429                                 new_types [subtypes.Length] = res;
430                                 subtypes = new_types;
431                         } else {
432                                 subtypes = new TypeBuilder [1];
433                                 subtypes [0] = res;
434                         }
435                         return res;
436                 }
437
438                 [ComVisible (true)]
439                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces)
440                 {
441                         return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
442                 }
443
444                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typeSize)
445                 {
446                         return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typeSize);
447                 }
448
449                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packSize)
450                 {
451                         return DefineNestedType (name, attr, parent, null, packSize, UnspecifiedTypeSize);
452                 }
453
454                 [ComVisible (true)]
455                 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes)
456                 {
457                         return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
458                 }
459
460                 [ComVisible (true)]
461                 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
462                 {
463                         check_not_created ();
464                         ConstructorBuilder cb = new ConstructorBuilder (this, attributes,
465                                 callingConvention, parameterTypes, requiredCustomModifiers,
466                                 optionalCustomModifiers);
467                         if (ctors != null) {
468                                 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
469                                 System.Array.Copy (ctors, new_ctors, ctors.Length);
470                                 new_ctors [ctors.Length] = cb;
471                                 ctors = new_ctors;
472                         } else {
473                                 ctors = new ConstructorBuilder [1];
474                                 ctors [0] = cb;
475                         }
476                         return cb;
477                 }
478
479                 [ComVisible (true)]
480                 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
481                 {
482                         Type parent_type, old_parent_type;
483
484                         if (parent != null)
485                                 parent_type = parent;
486                         else
487                                 parent_type = pmodule.assemblyb.corlib_object_type;
488
489                         old_parent_type = parent_type;
490                         parent_type = parent_type.InternalResolve ();
491                         /*This avoids corlib to have self references.*/
492                         if (parent_type == typeof (object) || parent_type == typeof (ValueType))
493                                 parent_type = old_parent_type;
494
495                         ConstructorInfo parent_constructor =
496                                 parent_type.GetConstructor (
497                                         BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
498                                         null, Type.EmptyTypes, null);
499                         if (parent_constructor == null) {
500                                 throw new NotSupportedException ("Parent does"
501                                         + " not have a default constructor."
502                                         + " The default constructor must be"
503                                         + " explicitly defined.");
504                         }
505
506                         ConstructorBuilder cb = DefineConstructor (attributes, 
507                                 CallingConventions.Standard, Type.EmptyTypes);
508                         ILGenerator ig = cb.GetILGenerator ();
509                         ig.Emit (OpCodes.Ldarg_0);
510                         ig.Emit (OpCodes.Call, parent_constructor);
511                         ig.Emit (OpCodes.Ret);
512                         return cb;
513                 }
514
515                 private void append_method (MethodBuilder mb)
516                 {
517                         if (methods != null) {
518                                 if (methods.Length == num_methods) {
519                                         MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
520                                         System.Array.Copy (methods, new_methods, num_methods);
521                                         methods = new_methods;
522                                 }
523                         } else {
524                                 methods = new MethodBuilder [1];
525                         }
526                         methods [num_methods] = mb;
527                         num_methods ++;
528                 }
529
530                 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes)
531                 {
532                         return DefineMethod (name, attributes, CallingConventions.Standard,
533                                 returnType, parameterTypes);
534                 }
535
536                 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
537                 {
538                         return DefineMethod (name, attributes, callingConvention, returnType,
539                                 null, null, parameterTypes, null, null);
540                 }
541
542                 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
543                 {
544                         check_name ("name", name);
545                         check_not_created ();
546                         if (IsInterface && (
547                                 !((attributes & MethodAttributes.Abstract) != 0) || 
548                                 !((attributes & MethodAttributes.Virtual) != 0)) &&
549                                 !(((attributes & MethodAttributes.Static) != 0)))
550                                 throw new ArgumentException ("Interface method must be abstract and virtual.");
551
552                         if (returnType == null)
553                                 returnType = pmodule.assemblyb.corlib_void_type;
554                         MethodBuilder res = new MethodBuilder (this, name, attributes, 
555                                 callingConvention, returnType,
556                                 returnTypeRequiredCustomModifiers,
557                                 returnTypeOptionalCustomModifiers, parameterTypes,
558                                 parameterTypeRequiredCustomModifiers,
559                                 parameterTypeOptionalCustomModifiers);
560                         append_method (res);
561                         return res;
562                 }
563
564                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
565                 {
566                         return DefinePInvokeMethod (name, dllName, entryName, attributes,
567                                 callingConvention, returnType, null, null, parameterTypes,
568                                 null, null, nativeCallConv, nativeCharSet);
569                 }
570
571                 public MethodBuilder DefinePInvokeMethod (
572                                                 string name, 
573                                                 string dllName, 
574                                                 string entryName, MethodAttributes attributes, 
575                                                 CallingConventions callingConvention, 
576                                                 Type returnType, 
577                                                 Type[] returnTypeRequiredCustomModifiers, 
578                                                 Type[] returnTypeOptionalCustomModifiers, 
579                                                 Type[] parameterTypes, 
580                                                 Type[][] parameterTypeRequiredCustomModifiers, 
581                                                 Type[][] parameterTypeOptionalCustomModifiers, 
582                                                 CallingConvention nativeCallConv, 
583                                                 CharSet nativeCharSet)
584                 {
585                         check_name ("name", name);
586                         check_name ("dllName", dllName);
587                         check_name ("entryName", entryName);
588                         if ((attributes & MethodAttributes.Abstract) != 0)
589                                 throw new ArgumentException ("PInvoke methods must be static and native and cannot be abstract.");
590                         if (IsInterface)
591                                 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
592                         check_not_created ();
593
594                         MethodBuilder res 
595                                 = new MethodBuilder (
596                                                 this, 
597                                                 name, 
598                                                 attributes, 
599                                                 callingConvention,
600                                                 returnType, 
601                                                 returnTypeRequiredCustomModifiers, 
602                                                 returnTypeOptionalCustomModifiers, 
603                                                 parameterTypes, 
604                                                 parameterTypeRequiredCustomModifiers, 
605                                                 parameterTypeOptionalCustomModifiers,
606                                                 dllName, 
607                                                 entryName, 
608                                                 nativeCallConv, 
609                                                 nativeCharSet);
610                         append_method (res);
611                         return res;
612                 }
613
614                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
615                         return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
616                                 nativeCallConv, nativeCharSet);
617                 }
618
619                 public MethodBuilder DefineMethod (string name, MethodAttributes attributes)
620                 {
621                         return DefineMethod (name, attributes, CallingConventions.Standard);
622                 }
623
624                 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention)
625                 {
626                         return DefineMethod (name, attributes, callingConvention, null, null);
627                 }
628
629                 public void DefineMethodOverride (MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
630                 {
631                         if (methodInfoBody == null)
632                                 throw new ArgumentNullException ("methodInfoBody");
633                         if (methodInfoDeclaration == null)
634                                 throw new ArgumentNullException ("methodInfoDeclaration");
635                         check_not_created ();
636                         if (methodInfoBody.DeclaringType != this)
637                                 throw new ArgumentException ("method body must belong to this type");
638
639                         if (methodInfoBody is MethodBuilder) {
640                                 MethodBuilder mb = (MethodBuilder)methodInfoBody;
641                                 mb.set_override (methodInfoDeclaration);
642                         }
643                 }
644
645                 public FieldBuilder DefineField (string fieldName, Type type, FieldAttributes attributes)
646                 {
647                         return DefineField (fieldName, type, null, null, attributes);
648                 }
649
650                 public FieldBuilder DefineField (string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
651                 {
652                         check_name ("fieldName", fieldName);
653                         if (type == typeof (void))
654                                 throw new ArgumentException ("Bad field type in defining field.");
655                         check_not_created ();
656
657                         FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomModifiers, optionalCustomModifiers);
658                         if (fields != null) {
659                                 if (fields.Length == num_fields) {
660                                         FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
661                                         System.Array.Copy (fields, new_fields, num_fields);
662                                         fields = new_fields;
663                                 }
664                                 fields [num_fields] = res;
665                                 num_fields ++;
666                         } else {
667                                 fields = new FieldBuilder [1];
668                                 fields [0] = res;
669                                 num_fields ++;
670                                 create_internal_class (this);
671                         }
672
673                         if (IsEnum) {
674                                 if (underlying_type == null && (attributes & FieldAttributes.Static) == 0)
675                                         underlying_type = type;
676                         }
677
678                         return res;
679                 }
680
681                 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes)
682                 {
683                         return DefineProperty (name, attributes, 0, returnType, null, null, parameterTypes, null, null);
684                 }
685                 
686 #if NET_4_0
687                 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
688                 {
689                         return DefineProperty (name, attributes, callingConvention, returnType , null, null, parameterTypes, null, null);
690                 }       
691 #endif
692
693                 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
694                 {
695                         return DefineProperty (name, attributes, 0, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
696                 }
697                 
698                 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
699                 {
700                         check_name ("name", name);
701                         if (parameterTypes != null)
702                                 foreach (Type param in parameterTypes)
703                                         if (param == null)
704                                                 throw new ArgumentNullException ("parameterTypes");
705                         check_not_created ();
706
707                         PropertyBuilder res = new PropertyBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
708
709                         if (properties != null) {
710                                 Array.Resize (ref properties, properties.Length + 1);
711                                 properties [properties.Length - 1] = res;
712                         } else {
713                                 properties = new PropertyBuilder [1] { res };
714                         }
715                         return res;
716                 }
717
718                 [ComVisible (true)]
719                 public ConstructorBuilder DefineTypeInitializer()
720                 {
721                         return DefineConstructor (MethodAttributes.Public |
722                                 MethodAttributes.Static | MethodAttributes.SpecialName |
723                                 MethodAttributes.RTSpecialName, CallingConventions.Standard,
724                                 null);
725                 }
726
727                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
728                 private extern TypeInfo create_runtime_class (TypeBuilder tb);
729
730                 private bool is_nested_in (Type t)
731                 {
732                         while (t != null) {
733                                 if (t == this)
734                                         return true;
735                                 else
736                                         t = t.DeclaringType;
737                         }
738                         return false;
739                 }
740
741                 // Return whenever this type has a ctor defined using DefineMethod ()
742                 private bool has_ctor_method () {
743                         MethodAttributes ctor_attrs = MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
744
745                         for (int i = 0; i < num_methods; ++i) {
746                                 MethodBuilder mb = (MethodBuilder)(methods[i]);
747
748                                 if (mb.Name == ConstructorInfo.ConstructorName && (mb.Attributes & ctor_attrs) == ctor_attrs)
749                                         return true;
750                         }
751
752                         return false;
753             }
754
755                 public Type CreateType ()
756                 {
757                         return CreateTypeInfo ();
758                 }
759                 
760 #if NET_4_5
761                 public
762 #endif
763                 TypeInfo CreateTypeInfo ()
764                 {
765                         /* handle nesting_type */
766                         if (createTypeCalled)
767                                 return created;
768
769                         if (!IsInterface && (parent == null) && (this != pmodule.assemblyb.corlib_object_type) && (FullName != "<Module>")) {
770                                 SetParent (pmodule.assemblyb.corlib_object_type);
771                         }
772
773                         create_generic_class ();
774
775                         // Fire TypeResolve events for fields whose type is an unfinished
776                         // value type.
777                         if (fields != null) {
778                                 foreach (FieldBuilder fb in fields) {
779                                         if (fb == null)
780                                                 continue;
781                                         Type ft = fb.FieldType;
782                                         if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
783                                                 TypeBuilder tb = (TypeBuilder)ft;
784                                                 if (!tb.is_created) {
785                                                         AppDomain.CurrentDomain.DoTypeResolve (tb);
786                                                         if (!tb.is_created) {
787                                                                 // FIXME: We should throw an exception here,
788                                                                 // but mcs expects that the type is created
789                                                                 // even if the exception is thrown
790                                                                 //throw new TypeLoadException ("Could not load type " + tb);
791                                                         }
792                                                 }
793                                         }
794                                 }
795                         }
796
797                         //
798                         // On classes, define a default constructor if not provided
799                         //
800                         if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>") && 
801                                 (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed) && !has_ctor_method ())
802                                 DefineDefaultConstructor (MethodAttributes.Public);
803
804                         createTypeCalled = true;
805
806                         if ((parent != null) && parent.IsSealed)
807                                 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because the parent type is sealed.");
808
809                         if (parent == pmodule.assemblyb.corlib_enum_type && methods != null)
810                                 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is an enum with methods.");
811                         if (interfaces != null) {
812                                 foreach (var iface in interfaces) {
813                                         if (iface.IsNestedPrivate && iface.Assembly != Assembly)
814                                                 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is implements the inaccessible interface '" + iface.FullName + "'.");
815                                 }
816                         }
817
818                         if (methods != null) {
819                                 bool is_concrete = !IsAbstract;
820                                 for (int i = 0; i < num_methods; ++i) {
821                                         MethodBuilder mb = (MethodBuilder)(methods[i]);
822                                         if (is_concrete && mb.IsAbstract)
823                                                 throw new InvalidOperationException ("Type is concrete but has abstract method " + mb);
824                                         mb.check_override ();
825                                         mb.fixup ();
826                                 }
827                         }
828
829                         if (ctors != null){
830                                 foreach (ConstructorBuilder ctor in ctors) 
831                                         ctor.fixup ();
832                         }
833
834                         created = create_runtime_class (this);
835                         if (created != null)
836                                 return created;
837                         return this;
838                 }
839
840                 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
841                 {
842                         symbolWriter.OpenNamespace (this.Namespace);
843
844                         if (methods != null) {
845                                 for (int i = 0; i < num_methods; ++i) {
846                                         MethodBuilder metb = (MethodBuilder) methods[i]; 
847                                         metb.GenerateDebugInfo (symbolWriter);
848                                 }
849                         }
850
851                         if (ctors != null) {
852                                 foreach (ConstructorBuilder ctor in ctors)
853                                         ctor.GenerateDebugInfo (symbolWriter);
854                         }
855                         
856                         symbolWriter.CloseNamespace ();
857
858                         if (subtypes != null) {
859                                 for (int i = 0; i < subtypes.Length; ++i)
860                                         subtypes [i].GenerateDebugInfo (symbolWriter);
861                         }
862                 }
863
864                 [ComVisible (true)]
865                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
866                 {
867                         if (is_created)
868                                 return created.GetConstructors (bindingAttr);
869
870                         throw new NotSupportedException ();
871                 }
872
873                 internal ConstructorInfo[] GetConstructorsInternal (BindingFlags bindingAttr)
874                 {
875                         if (ctors == null)
876                                 return new ConstructorInfo [0];
877                         ArrayList l = new ArrayList ();
878                         bool match;
879                         MethodAttributes mattrs;
880                         
881                         foreach (ConstructorBuilder c in ctors) {
882                                 match = false;
883                                 mattrs = c.Attributes;
884                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
885                                         if ((bindingAttr & BindingFlags.Public) != 0)
886                                                 match = true;
887                                 } else {
888                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
889                                                 match = true;
890                                 }
891                                 if (!match)
892                                         continue;
893                                 match = false;
894                                 if ((mattrs & MethodAttributes.Static) != 0) {
895                                         if ((bindingAttr & BindingFlags.Static) != 0)
896                                                 match = true;
897                                 } else {
898                                         if ((bindingAttr & BindingFlags.Instance) != 0)
899                                                 match = true;
900                                 }
901                                 if (!match)
902                                         continue;
903                                 l.Add (c);
904                         }
905                         ConstructorInfo[] result = new ConstructorInfo [l.Count];
906                         l.CopyTo (result);
907                         return result;
908                 }
909
910                 public override Type GetElementType ()
911                 {
912                         throw new NotSupportedException ();
913                 }
914
915                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
916                 {
917                         check_created ();
918                         return created.GetEvent (name, bindingAttr);
919                 }
920
921                 /* Needed to keep signature compatibility with MS.NET */
922                 public override EventInfo[] GetEvents ()
923                 {
924                         return GetEvents (DefaultBindingFlags);
925                 }
926
927                 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
928                 {
929                         if (is_created)
930                                 return created.GetEvents (bindingAttr);
931                         throw new NotSupportedException ();
932                 }
933
934                 // This is only used from MonoGenericInst.initialize().
935                 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
936                 {
937                         if (events == null)
938                                 return new EventInfo [0];
939                         ArrayList l = new ArrayList ();
940                         bool match;
941                         MethodAttributes mattrs;
942                         MethodInfo accessor;
943
944                         foreach (EventBuilder eb in events) {
945                                 if (eb == null)
946                                         continue;
947                                 EventInfo c = get_event_info (eb);
948                                 match = false;
949                                 accessor = c.GetAddMethod (true);
950                                 if (accessor == null)
951                                         accessor = c.GetRemoveMethod (true);
952                                 if (accessor == null)
953                                         continue;
954                                 mattrs = accessor.Attributes;
955                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
956                                         if ((bindingAttr & BindingFlags.Public) != 0)
957                                                 match = true;
958                                 } else {
959                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
960                                                 match = true;
961                                 }
962                                 if (!match)
963                                         continue;
964                                 match = false;
965                                 if ((mattrs & MethodAttributes.Static) != 0) {
966                                         if ((bindingAttr & BindingFlags.Static) != 0)
967                                                 match = true;
968                                 } else {
969                                         if ((bindingAttr & BindingFlags.Instance) != 0)
970                                                 match = true;
971                                 }
972                                 if (!match)
973                                         continue;
974                                 l.Add (c);
975                         }
976                         EventInfo[] result = new EventInfo [l.Count];
977                         l.CopyTo (result);
978                         return result;
979                 }
980
981                 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
982                 {
983                         if (created != null)
984                                 return created.GetField (name, bindingAttr);
985
986                         if (fields == null)
987                                 return null;
988
989                         bool match;
990                         FieldAttributes mattrs;
991                         
992                         foreach (FieldInfo c in fields) {
993                                 if (c == null)
994                                         continue;
995                                 if (c.Name != name)
996                                         continue;
997                                 match = false;
998                                 mattrs = c.Attributes;
999                                 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
1000                                         if ((bindingAttr & BindingFlags.Public) != 0)
1001                                                 match = true;
1002                                 } else {
1003                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
1004                                                 match = true;
1005                                 }
1006                                 if (!match)
1007                                         continue;
1008                                 match = false;
1009                                 if ((mattrs & FieldAttributes.Static) != 0) {
1010                                         if ((bindingAttr & BindingFlags.Static) != 0)
1011                                                 match = true;
1012                                 } else {
1013                                         if ((bindingAttr & BindingFlags.Instance) != 0)
1014                                                 match = true;
1015                                 }
1016                                 if (!match)
1017                                         continue;
1018                                 return c;
1019                         }
1020                         return null;
1021                 }
1022
1023                 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
1024                 {
1025                         if (created != null)
1026                                 return created.GetFields (bindingAttr);
1027
1028                         if (fields == null)
1029                                 return new FieldInfo [0];
1030                         ArrayList l = new ArrayList ();
1031                         bool match;
1032                         FieldAttributes mattrs;
1033                         
1034                         foreach (FieldInfo c in fields) {
1035                                 if (c == null)
1036                                         continue;
1037                                 match = false;
1038                                 mattrs = c.Attributes;
1039                                 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
1040                                         if ((bindingAttr & BindingFlags.Public) != 0)
1041                                                 match = true;
1042                                 } else {
1043                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
1044                                                 match = true;
1045                                 }
1046                                 if (!match)
1047                                         continue;
1048                                 match = false;
1049                                 if ((mattrs & FieldAttributes.Static) != 0) {
1050                                         if ((bindingAttr & BindingFlags.Static) != 0)
1051                                                 match = true;
1052                                 } else {
1053                                         if ((bindingAttr & BindingFlags.Instance) != 0)
1054                                                 match = true;
1055                                 }
1056                                 if (!match)
1057                                         continue;
1058                                 l.Add (c);
1059                         }
1060                         FieldInfo[] result = new FieldInfo [l.Count];
1061                         l.CopyTo (result);
1062                         return result;
1063                 }
1064
1065                 public override Type GetInterface (string name, bool ignoreCase)
1066                 {
1067                         check_created ();
1068                         return created.GetInterface (name, ignoreCase);
1069                 }
1070                 
1071                 public override Type[] GetInterfaces ()
1072                 {
1073                         if (is_created)
1074                                 return created.GetInterfaces ();
1075
1076                         if (interfaces != null) {
1077                                 Type[] ret = new Type [interfaces.Length];
1078                                 interfaces.CopyTo (ret, 0);
1079                                 return ret;
1080                         } else {
1081                                 return Type.EmptyTypes;
1082                         }
1083                 }
1084
1085                 public override MemberInfo[] GetMember (string name, MemberTypes type,
1086                                                                                                 BindingFlags bindingAttr)
1087                 {
1088                         check_created ();
1089                         return created.GetMember (name, type, bindingAttr);
1090                 }
1091
1092                 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
1093                 {
1094                         check_created ();
1095                         return created.GetMembers (bindingAttr);
1096                 }
1097
1098                 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type)
1099                 {
1100                         MethodInfo[] candidates;
1101                         bool match;
1102                         MethodAttributes mattrs;
1103
1104                         if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
1105                                 MethodInfo [] parent_methods = parent.GetMethods (bindingAttr);
1106                                 ArrayList parent_candidates = new ArrayList (parent_methods.Length);
1107
1108                                 bool flatten = (bindingAttr & BindingFlags.FlattenHierarchy) != 0;
1109
1110                                 for (int i = 0; i < parent_methods.Length; i++) {
1111                                         MethodInfo m = parent_methods [i];
1112
1113                                         mattrs = m.Attributes;
1114
1115                                         if (m.IsStatic && !flatten)
1116                                                 continue;
1117
1118                                         switch (mattrs & MethodAttributes.MemberAccessMask) {
1119                                         case MethodAttributes.Public:
1120                                                 match = (bindingAttr & BindingFlags.Public) != 0;
1121                                                 break;
1122                                         case MethodAttributes.Assembly:
1123                                                 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1124                                                 break;
1125                                         case MethodAttributes.Private:
1126                                                 match = false;
1127                                                 break;
1128                                         default:
1129                                                 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1130                                                 break;
1131                                         }
1132
1133                                         if (match)
1134                                                 parent_candidates.Add (m);
1135                                 }
1136
1137                                 if (methods == null) {
1138                                         candidates = new MethodInfo [parent_candidates.Count];
1139                                         parent_candidates.CopyTo (candidates);
1140                                 } else {
1141                                         candidates = new MethodInfo [methods.Length + parent_candidates.Count];
1142                                         parent_candidates.CopyTo (candidates, 0);
1143                                         methods.CopyTo (candidates, parent_candidates.Count);
1144                                 }
1145                         }
1146                         else
1147                                 candidates = methods;
1148
1149                         if (candidates == null)
1150                                 return new MethodInfo [0];
1151
1152                         ArrayList l = new ArrayList ();
1153
1154                         foreach (MethodInfo c in candidates) {
1155                                 if (c == null)
1156                                         continue;
1157                                 if (name != null) {
1158                                         if (String.Compare (c.Name, name, ignoreCase) != 0)
1159                                                 continue;
1160                                 }
1161                                 match = false;
1162                                 mattrs = c.Attributes;
1163                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1164                                         if ((bindingAttr & BindingFlags.Public) != 0)
1165                                                 match = true;
1166                                 } else {
1167                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
1168                                                 match = true;
1169                                 }
1170                                 if (!match)
1171                                         continue;
1172                                 match = false;
1173                                 if ((mattrs & MethodAttributes.Static) != 0) {
1174                                         if ((bindingAttr & BindingFlags.Static) != 0)
1175                                                 match = true;
1176                                 } else {
1177                                         if ((bindingAttr & BindingFlags.Instance) != 0)
1178                                                 match = true;
1179                                 }
1180                                 if (!match)
1181                                         continue;
1182                                 l.Add (c);
1183                         }
1184
1185                         MethodInfo[] result = new MethodInfo [l.Count];
1186                         l.CopyTo (result);
1187                         return result;
1188                 }
1189
1190                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
1191                 {
1192                         return GetMethodsByName (null, bindingAttr, false, this);
1193                 }
1194
1195                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
1196                                                              Binder binder,
1197                                                              CallingConventions callConvention,
1198                                                              Type[] types, ParameterModifier[] modifiers)
1199                 {
1200                         check_created ();
1201
1202                         bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
1203                         MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
1204                         MethodInfo found = null;
1205                         MethodBase[] match;
1206                         int typesLen = (types != null) ? types.Length : 0;
1207                         int count = 0;
1208                         
1209                         foreach (MethodInfo m in methods) {
1210                                 // Under MS.NET, Standard|HasThis matches Standard...
1211                                 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
1212                                         continue;
1213                                 found = m;
1214                                 count++;
1215                         }
1216
1217                         if (count == 0)
1218                                 return null;
1219                         
1220                         if (count == 1 && typesLen == 0) 
1221                                 return found;
1222
1223                         match = new MethodBase [count];
1224                         if (count == 1)
1225                                 match [0] = found;
1226                         else {
1227                                 count = 0;
1228                                 foreach (MethodInfo m in methods) {
1229                                         if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
1230                                                 continue;
1231                                         match [count++] = m;
1232                                 }
1233                         }
1234                         
1235                         if (types == null) 
1236                                 return (MethodInfo) Binder.FindMostDerivedMatch (match);
1237
1238                         if (binder == null)
1239                                 binder = Binder.DefaultBinder;
1240                         
1241                         return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
1242                 }
1243
1244                 public override Type GetNestedType (string name, BindingFlags bindingAttr)
1245                 {
1246                         check_created ();
1247
1248                         if (subtypes == null)
1249                                 return null;
1250
1251                         foreach (TypeBuilder t in subtypes) {
1252                                 if (!t.is_created)
1253                                         continue;
1254                                 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1255                                         if ((bindingAttr & BindingFlags.Public) == 0)
1256                                                 continue;
1257                                 } else {
1258                                         if ((bindingAttr & BindingFlags.NonPublic) == 0)
1259                                                 continue;
1260                                 }
1261                                 if (t.Name == name)
1262                                         return t.created;
1263                         }
1264
1265                         return null;
1266                 }
1267
1268                 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
1269                 {
1270                         if (!is_created)
1271                                 throw new NotSupportedException ();
1272
1273                         bool match;
1274                         ArrayList result = new ArrayList ();
1275
1276                         if (subtypes == null)
1277                                 return Type.EmptyTypes;
1278                         foreach (TypeBuilder t in subtypes) {
1279                                 match = false;
1280                                 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1281                                         if ((bindingAttr & BindingFlags.Public) != 0)
1282                                                 match = true;
1283                                 } else {
1284                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
1285                                                 match = true;
1286                                 }
1287                                 if (!match)
1288                                         continue;
1289                                 result.Add (t);
1290                         }
1291                         Type[] r = new Type [result.Count];
1292                         result.CopyTo (r);
1293                         return r;
1294                 }
1295
1296                 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
1297                 {
1298                         if (is_created)
1299                                 return created.GetProperties (bindingAttr);
1300
1301                         if (properties == null)
1302                                 return new PropertyInfo [0];
1303                         ArrayList l = new ArrayList ();
1304                         bool match;
1305                         MethodAttributes mattrs;
1306                         MethodInfo accessor;
1307                         
1308                         foreach (PropertyInfo c in properties) {
1309                                 match = false;
1310                                 accessor = c.GetGetMethod (true);
1311                                 if (accessor == null)
1312                                         accessor = c.GetSetMethod (true);
1313                                 if (accessor == null)
1314                                         continue;
1315                                 mattrs = accessor.Attributes;
1316                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1317                                         if ((bindingAttr & BindingFlags.Public) != 0)
1318                                                 match = true;
1319                                 } else {
1320                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
1321                                                 match = true;
1322                                 }
1323                                 if (!match)
1324                                         continue;
1325                                 match = false;
1326                                 if ((mattrs & MethodAttributes.Static) != 0) {
1327                                         if ((bindingAttr & BindingFlags.Static) != 0)
1328                                                 match = true;
1329                                 } else {
1330                                         if ((bindingAttr & BindingFlags.Instance) != 0)
1331                                                 match = true;
1332                                 }
1333                                 if (!match)
1334                                         continue;
1335                                 l.Add (c);
1336                         }
1337                         PropertyInfo[] result = new PropertyInfo [l.Count];
1338                         l.CopyTo (result);
1339                         return result;
1340                 }
1341                 
1342                 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1343                 {
1344                         throw not_supported ();
1345                 }
1346
1347                 protected override bool HasElementTypeImpl ()
1348                 {
1349                         // a TypeBuilder can never represent an array, pointer
1350                         if (!is_created)
1351                                 return false;
1352
1353                         return created.HasElementType;
1354                 }
1355
1356                 public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
1357                 {
1358                         check_created ();
1359                         return created.InvokeMember (name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
1360                 }
1361
1362                 protected override bool IsArrayImpl ()
1363                 {
1364                         return false; /*A TypeBuilder never represents a non typedef type.*/
1365                 }
1366
1367                 protected override bool IsByRefImpl ()
1368                 {
1369                         return false; /*A TypeBuilder never represents a non typedef type.*/
1370                 }
1371
1372                 protected override bool IsCOMObjectImpl ()
1373                 {
1374                         return ((GetAttributeFlagsImpl () & TypeAttributes.Import) != 0);
1375                 }
1376
1377                 protected override bool IsPointerImpl ()
1378                 {
1379                         return false; /*A TypeBuilder never represents a non typedef type.*/
1380                 }
1381
1382                 protected override bool IsPrimitiveImpl ()
1383                 {
1384                         // FIXME
1385                         return false;
1386                 }
1387
1388                 // FIXME: I doubt just removing this still works.
1389                 protected override bool IsValueTypeImpl ()
1390                 {
1391                         if (this == pmodule.assemblyb.corlib_value_type || this == pmodule.assemblyb.corlib_enum_type)
1392                                 return false;
1393                         Type parent_type = parent;
1394                         while (parent_type != null) {
1395                                 if (parent_type == pmodule.assemblyb.corlib_value_type)
1396                                         return true;
1397                                 parent_type = parent_type.BaseType;
1398                         }
1399                         return false;
1400                 }
1401                 
1402                 public override Type MakeArrayType ()
1403                 {
1404                         return new ArrayType (this, 0);
1405                 }
1406
1407                 public override Type MakeArrayType (int rank)
1408                 {
1409                         if (rank < 1)
1410                                 throw new IndexOutOfRangeException ();
1411                         return new ArrayType (this, rank);
1412                 }
1413
1414                 public override Type MakeByRefType ()
1415                 {
1416                         return new ByRefType (this);
1417                 }
1418
1419                 public override Type MakeGenericType (params Type [] typeArguments)
1420                 {
1421                         //return base.MakeGenericType (typeArguments);
1422
1423                         if (!IsGenericTypeDefinition)
1424                                 throw new InvalidOperationException ("not a generic type definition");
1425                         if (typeArguments == null)
1426                                 throw new ArgumentNullException ("typeArguments");
1427
1428                         if (generic_params.Length != typeArguments.Length)
1429                                 throw new ArgumentException (String.Format ("The type or method has {0} generic parameter(s) but {1} generic argument(s) where provided. A generic argument must be provided for each generic parameter.", generic_params.Length, typeArguments.Length), "typeArguments");
1430
1431                         foreach (Type t in typeArguments) {
1432                                 if (t == null)
1433                                         throw new ArgumentNullException ("typeArguments");                              
1434                         }
1435
1436                         Type[] copy = new Type [typeArguments.Length];
1437                         typeArguments.CopyTo (copy, 0);
1438                         return pmodule.assemblyb.MakeGenericType (this, copy);
1439                 }
1440
1441                 public override Type MakePointerType ()
1442                 {
1443                         return new PointerType (this);
1444                 }
1445
1446                 public override RuntimeTypeHandle TypeHandle {
1447                         get {
1448                                 check_created ();
1449                                 return created.TypeHandle;
1450                         }
1451                 }
1452                 
1453                 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
1454                 {
1455                         if (customBuilder == null)
1456                                 throw new ArgumentNullException ("customBuilder");
1457
1458                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
1459                         if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1460                                 byte[] data = customBuilder.Data;
1461                                 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1462                                 layout_kind = (int)data [2];
1463                                 layout_kind |= ((int)data [3]) << 8;
1464                                 attrs &= ~TypeAttributes.LayoutMask;
1465                                 switch ((LayoutKind)layout_kind) {
1466                                 case LayoutKind.Auto:
1467                                         attrs |= TypeAttributes.AutoLayout;
1468                                         break;
1469                                 case LayoutKind.Explicit:
1470                                         attrs |= TypeAttributes.ExplicitLayout;
1471                                         break;
1472                                 case LayoutKind.Sequential:
1473                                         attrs |= TypeAttributes.SequentialLayout;
1474                                         break;
1475                                 default:
1476                                         // we should ignore it since it can be any value anyway...
1477                                         throw new Exception ("Error in customattr");
1478                                 }
1479                                 
1480                                 var ctor_type = customBuilder.Ctor is ConstructorBuilder ? ((ConstructorBuilder)customBuilder.Ctor).parameters[0] : customBuilder.Ctor.GetParametersInternal()[0].ParameterType;
1481                                 int pos = 6;
1482                                 if (ctor_type.FullName == "System.Int16")
1483                                         pos = 4;
1484                                 int nnamed = (int)data [pos++];
1485                                 nnamed |= ((int)data [pos++]) << 8;
1486                                 for (int i = 0; i < nnamed; ++i) {
1487                                         //byte named_type = data [pos++];
1488                                         pos ++;
1489                                         byte type = data [pos++];
1490                                         int len;
1491                                         string named_name;
1492
1493                                         if (type == 0x55) {
1494                                                 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1495                                                 //string named_typename = 
1496                                                 CustomAttributeBuilder.string_from_bytes (data, pos, len);
1497                                                 pos += len;
1498                                                 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1499                                                 //        See related code/FIXME in mono/mono/metadata/reflection.c
1500                                         }
1501
1502                                         len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1503                                         named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1504                                         pos += len;
1505                                         /* all the fields are integers in StructLayout */
1506                                         int value = (int)data [pos++];
1507                                         value |= ((int)data [pos++]) << 8;
1508                                         value |= ((int)data [pos++]) << 16;
1509                                         value |= ((int)data [pos++]) << 24;
1510                                         switch (named_name) {
1511                                         case "CharSet":
1512                                                 switch ((CharSet)value) {
1513                                                 case CharSet.None:
1514                                                 case CharSet.Ansi:
1515                                                         attrs &= ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass);
1516                                                         break;
1517                                                 case CharSet.Unicode:
1518                                                         attrs &= ~TypeAttributes.AutoClass;
1519                                                         attrs |= TypeAttributes.UnicodeClass;
1520                                                         break;
1521                                                 case CharSet.Auto:
1522                                                         attrs &= ~TypeAttributes.UnicodeClass;
1523                                                         attrs |= TypeAttributes.AutoClass;
1524                                                         break;
1525                                                 default:
1526                                                         break; // error out...
1527                                                 }
1528                                                 break;
1529                                         case "Pack":
1530                                                 packing_size = (PackingSize)value;
1531                                                 break;
1532                                         case "Size":
1533                                                 class_size = value;
1534                                                 break;
1535                                         default:
1536                                                 break; // error out...
1537                                         }
1538                                 }
1539                                 return;
1540                         } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
1541                                 attrs |= TypeAttributes.SpecialName;
1542                                 return;
1543                         } else if (attrname == "System.SerializableAttribute") {
1544                                 attrs |= TypeAttributes.Serializable;
1545                                 return;
1546                         } else if (attrname == "System.Runtime.InteropServices.ComImportAttribute") {
1547                                 attrs |= TypeAttributes.Import;
1548                                 return;
1549                         } else if (attrname == "System.Security.SuppressUnmanagedCodeSecurityAttribute") {
1550                                 attrs |= TypeAttributes.HasSecurity;
1551                         }
1552
1553                         if (cattrs != null) {
1554                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1555                                 cattrs.CopyTo (new_array, 0);
1556                                 new_array [cattrs.Length] = customBuilder;
1557                                 cattrs = new_array;
1558                         } else {
1559                                 cattrs = new CustomAttributeBuilder [1];
1560                                 cattrs [0] = customBuilder;
1561                         }
1562                 }
1563
1564                 [ComVisible (true)]
1565                 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
1566                 {
1567                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1568                 }
1569
1570                 public EventBuilder DefineEvent (string name, EventAttributes attributes, Type eventtype)
1571                 {
1572                         check_name ("name", name);
1573                         if (eventtype == null)
1574                                 throw new ArgumentNullException ("type");
1575                         check_not_created ();
1576
1577                         EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1578                         if (events != null) {
1579                                 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1580                                 System.Array.Copy (events, new_events, events.Length);
1581                                 new_events [events.Length] = res;
1582                                 events = new_events;
1583                         } else {
1584                                 events = new EventBuilder [1];
1585                                 events [0] = res;
1586                         }
1587                         return res;
1588                 }
1589
1590                 public FieldBuilder DefineInitializedData (string name, byte[] data, FieldAttributes attributes) {
1591                         if (data == null)
1592                                 throw new ArgumentNullException ("data");
1593
1594                         FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1595                         res.SetRVAData (data);
1596                         return res;
1597                 }
1598
1599                 public FieldBuilder DefineUninitializedData (string name, int size, FieldAttributes attributes)
1600                 {
1601                         if (name == null)
1602                                 throw new ArgumentNullException ("name");
1603                         if (name.Length == 0)
1604                                 throw new ArgumentException ("Empty name is not legal", "name");
1605                         if ((size <= 0) || (size > 0x3f0000))
1606                                 throw new ArgumentException ("Data size must be > 0 and < 0x3f0000");
1607                         check_not_created ();
1608
1609                         string typeName = "$ArrayType$" + size;
1610                         Type datablobtype = pmodule.GetRegisteredType (fullname + "+" + typeName);
1611                         if (datablobtype == null) {
1612                                 TypeBuilder tb = DefineNestedType (typeName,
1613                                         TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1614                                         pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1615                                 tb.CreateType ();
1616                                 datablobtype = tb;
1617                         }
1618                         return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1619                 }
1620
1621                 public TypeToken TypeToken {
1622                         get {
1623                                 return new TypeToken (0x02000000 | table_idx);
1624                         }
1625                 }
1626
1627                 public void SetParent (Type parent)
1628                 {
1629                         check_not_created ();
1630
1631                         if (parent == null) {
1632                                 if ((attrs & TypeAttributes.Interface) != 0) {
1633                                         if ((attrs & TypeAttributes.Abstract) == 0)
1634                                                 throw new InvalidOperationException ("Interface must be declared abstract.");
1635                                         this.parent = null;
1636                                 } else {
1637                                         this.parent = typeof (object);
1638                                 }
1639                         } else {
1640                                 this.parent = parent;
1641                         }
1642
1643                         // will just set the parent-related bits if called a second time
1644                         setup_internal_class (this);
1645                 }
1646
1647                 internal int get_next_table_index (object obj, int table, bool inc) {
1648                         return pmodule.get_next_table_index (obj, table, inc);
1649                 }
1650
1651                 [ComVisible (true)]
1652                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1653                 {
1654                         if (created == null)
1655                                 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1656
1657                         return created.GetInterfaceMap (interfaceType);
1658                 }
1659
1660                 internal override Type InternalResolve ()
1661                 {
1662                         check_created ();
1663                         return created;
1664                 }
1665
1666                 internal bool is_created {
1667                         get {
1668                                 return createTypeCalled;
1669                         }
1670                 }
1671
1672                 private Exception not_supported ()
1673                 {
1674                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1675                 }
1676
1677                 private void check_not_created ()
1678                 {
1679                         if (is_created)
1680                                 throw new InvalidOperationException ("Unable to change after type has been created.");
1681                 }
1682
1683                 private void check_created ()
1684                 {
1685                         if (!is_created)
1686                                 throw not_supported ();
1687                 }
1688
1689                 private void check_name (string argName, string name)
1690                 {
1691                         if (name == null)
1692                                 throw new ArgumentNullException (argName);
1693                         if (name.Length == 0)
1694                                 throw new ArgumentException ("Empty name is not legal", argName);
1695                         if (name [0] == ((char)0))
1696                                 throw new ArgumentException ("Illegal name", argName);
1697                 }
1698
1699                 public override String ToString ()
1700                 {
1701                         return FullName;
1702                 }
1703
1704                 [MonoTODO]
1705                 public override bool IsAssignableFrom (Type c)
1706                 {
1707                         return base.IsAssignableFrom (c);
1708                 }
1709
1710                 [MonoTODO ("arrays")]
1711                 internal bool IsAssignableTo (Type c)
1712                 {
1713                         if (c == this)
1714                                 return true;
1715
1716                         if (c.IsInterface) {
1717                                 if (parent != null && is_created) {
1718                                         if (c.IsAssignableFrom (parent))
1719                                                 return true;
1720                                 }
1721
1722                                 if (interfaces == null)
1723                                         return false;
1724                                 foreach (Type t in interfaces)
1725                                         if (c.IsAssignableFrom (t))
1726                                                 return true;
1727                                 if (!is_created)
1728                                         return false;
1729                         }
1730
1731                         if (parent == null)
1732                                 return c == typeof (object);
1733                         else
1734                                 return c.IsAssignableFrom (parent);
1735                 }
1736
1737                 public bool IsCreated ()
1738                 {
1739                         return is_created;
1740                 }
1741
1742                 public override Type[] GetGenericArguments ()
1743                 {
1744                         if (generic_params == null)
1745                                 return null;
1746                         Type[] args = new Type [generic_params.Length];
1747                         generic_params.CopyTo (args, 0);
1748                         return args;
1749                 }
1750
1751                 public override Type GetGenericTypeDefinition ()
1752                 {
1753                         if (generic_params == null)
1754                                 throw new InvalidOperationException ("Type is not generic");
1755                         return this;
1756                 }
1757
1758                 public override bool ContainsGenericParameters {
1759                         get {
1760                                 return generic_params != null;
1761                         }
1762                 }
1763
1764                 public extern override bool IsGenericParameter {
1765                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
1766                         get;
1767                 }
1768
1769                 public override GenericParameterAttributes GenericParameterAttributes {
1770                         get { return GenericParameterAttributes.None; }
1771                 }
1772
1773                 public override bool IsGenericTypeDefinition {
1774                         get {
1775                                 return generic_params != null;
1776                         }
1777                 }
1778
1779                 public override bool IsGenericType {
1780                         get { return IsGenericTypeDefinition; }
1781                 }
1782
1783                 [MonoTODO]
1784                 public override int GenericParameterPosition {
1785                         get {
1786                                 return 0;
1787                         }
1788                 }
1789
1790                 public override MethodBase DeclaringMethod {
1791                         get {
1792                                 return null;
1793                         }
1794                 }
1795
1796                 public GenericTypeParameterBuilder[] DefineGenericParameters (params string[] names)
1797                 {
1798                         if (names == null)
1799                                 throw new ArgumentNullException ("names");
1800                         if (names.Length == 0)
1801                                 throw new ArgumentException ("names");
1802
1803                         setup_generic_class ();
1804
1805                         generic_params = new GenericTypeParameterBuilder [names.Length];
1806                         for (int i = 0; i < names.Length; i++) {
1807                                 string item = names [i];
1808                                 if (item == null)
1809                                         throw new ArgumentNullException ("names");
1810                                 generic_params [i] = new GenericTypeParameterBuilder (this, null, item, i);
1811                         }
1812
1813                         return generic_params;
1814                 }
1815
1816                 public static ConstructorInfo GetConstructor (Type type, ConstructorInfo constructor)
1817                 {
1818                         /*FIXME I would expect the same checks of GetMethod here*/
1819                         if (type == null)
1820                                 throw new ArgumentException ("Type is not generic", "type");
1821
1822                         if (!type.IsGenericType)
1823                                 throw new ArgumentException ("Type is not a generic type", "type");
1824
1825                         if (type.IsGenericTypeDefinition)
1826                                 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1827
1828                         if (constructor == null)
1829                                 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1830
1831                         if (!constructor.DeclaringType.IsGenericTypeDefinition)
1832                                 throw new ArgumentException ("constructor declaring type is not a generic type definition", "constructor");
1833                         if (constructor.DeclaringType != type.GetGenericTypeDefinition ())
1834                                 throw new ArgumentException ("constructor declaring type is not the generic type definition of type", "constructor");
1835
1836                         ConstructorInfo res = type.GetConstructor (constructor);
1837                         if (res == null)
1838                                 throw new ArgumentException ("constructor not found");
1839
1840                         return res;
1841                 }
1842
1843                 static bool IsValidGetMethodType (Type type)
1844                 {
1845                         if (type is TypeBuilder || type is MonoGenericClass)
1846                                 return true;
1847                         /*GetMethod() must work with TypeBuilders after CreateType() was called.*/
1848                         if (type.Module is ModuleBuilder)
1849                                 return true;
1850                         if (type.IsGenericParameter)
1851                                 return false;
1852
1853                         Type[] inst = type.GetGenericArguments ();
1854                         if (inst == null)
1855                                 return false;
1856                         for (int i = 0; i < inst.Length; ++i) {
1857                                 if (IsValidGetMethodType (inst [i]))
1858                                         return true;
1859                         }
1860                         return false;
1861                 }
1862
1863                 public static MethodInfo GetMethod (Type type, MethodInfo method)
1864                 {
1865                         if (!IsValidGetMethodType (type))
1866                                 throw new ArgumentException ("type is not TypeBuilder but " + type.GetType (), "type");
1867
1868                         if (type is TypeBuilder && type.ContainsGenericParameters)
1869                                 type = type.MakeGenericType (type.GetGenericArguments ());
1870
1871                         if (!type.IsGenericType)
1872                                 throw new ArgumentException ("type is not a generic type", "type");
1873
1874                         if (!method.DeclaringType.IsGenericTypeDefinition)
1875                                 throw new ArgumentException ("method declaring type is not a generic type definition", "method");
1876                         if (method.DeclaringType != type.GetGenericTypeDefinition ())
1877                                 throw new ArgumentException ("method declaring type is not the generic type definition of type", "method");
1878                         if (method == null)
1879                                 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1880
1881                         MethodInfo res = type.GetMethod (method);
1882                         if (res == null)
1883                                 throw new ArgumentException (String.Format ("method {0} not found in type {1}", method.Name, type));
1884                                 
1885                         return res;
1886                 }
1887
1888                 public static FieldInfo GetField (Type type, FieldInfo field)
1889                 {
1890                         if (!type.IsGenericType)
1891                                 throw new ArgumentException ("Type is not a generic type", "type");
1892
1893                         if (type.IsGenericTypeDefinition)
1894                                 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1895
1896                         if (field is FieldOnTypeBuilderInst)
1897                                 throw new ArgumentException ("The specified field must be declared on a generic type definition.", "field");
1898
1899                         if (field.DeclaringType != type.GetGenericTypeDefinition ())
1900                                 throw new ArgumentException ("field declaring type is not the generic type definition of type", "method");
1901
1902                         FieldInfo res = type.GetField (field);
1903                         if (res == null)
1904                                 throw new System.Exception ("field not found");
1905                         else
1906                                 return res;
1907                 }
1908
1909                 internal TypeCode GetTypeCodeInternal () {
1910                         if (parent == pmodule.assemblyb.corlib_enum_type) {
1911                                 for (int i = 0; i < num_fields; ++i) {
1912                                         FieldBuilder f = fields [i];
1913                                         if (!f.IsStatic)
1914                                                 return Type.GetTypeCode (f.FieldType);
1915                                 }
1916                                 throw new InvalidOperationException ("Enum basetype field not defined");
1917                         } else {
1918                                 return Type.GetTypeCodeInternal (this);
1919                         }
1920                 }
1921
1922
1923                 void _TypeBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1924                 {
1925                         throw new NotImplementedException ();
1926                 }
1927
1928                 void _TypeBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1929                 {
1930                         throw new NotImplementedException ();
1931                 }
1932
1933                 void _TypeBuilder.GetTypeInfoCount (out uint pcTInfo)
1934                 {
1935                         throw new NotImplementedException ();
1936                 }
1937
1938                 void _TypeBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1939                 {
1940                         throw new NotImplementedException ();
1941                 }
1942
1943                 internal override bool IsUserType {
1944                         get {
1945                                 return false;
1946                         }
1947                 }
1948
1949 #if NET_4_5
1950                 public override bool IsConstructedGenericType {
1951                         get { return false; }
1952                 }
1953
1954                 public override bool IsAssignableFrom (TypeInfo typeInfo)
1955                 {
1956                         return base.IsAssignableFrom (typeInfo);
1957                 }
1958 #endif
1959         }
1960 }
1961 #endif