2004-03-30 Martin Baulig <martin@ximian.com>
[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 //
7 // (C) 2001 Ximian, Inc.  http://www.ximian.com
8 //
9
10 using System;
11 using System.Text;
12 using System.Reflection;
13 using System.Reflection.Emit;
14 using System.Runtime.CompilerServices;
15 using System.Runtime.InteropServices;
16 using System.Globalization;
17 using System.Collections;
18 using System.Security;
19 using System.Security.Permissions;
20
21 namespace System.Reflection.Emit {
22
23         public sealed class TypeBuilder : Type {
24         #region Sync with reflection.h
25         private string tname;
26         private string nspace;
27         private Type parent;
28         private Type nesting_type;
29         private Type[] interfaces;
30         private int num_methods;
31         private MethodBuilder[] methods;
32         private ConstructorBuilder[] ctors;
33         private PropertyBuilder[] properties;
34         private int num_fields;
35         private FieldBuilder[] fields;
36         private EventBuilder[] events;
37         private CustomAttributeBuilder[] cattrs;
38         internal TypeBuilder[] subtypes;
39         private TypeAttributes attrs;
40         private int table_idx;
41         private ModuleBuilder pmodule;
42         private int class_size;
43         private PackingSize packing_size;
44         private MonoGenericParam[] generic_params;
45         private RefEmitPermissionSet[] permissions;     
46         #endregion
47         private Type created;
48         string fullname;
49
50         public const int UnspecifiedTypeSize = 0;
51
52                 protected override TypeAttributes GetAttributeFlagsImpl () {
53                         return attrs;
54                 }
55                 
56                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
57                 private extern void setup_internal_class (TypeBuilder tb);
58                 
59                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
60                 private extern void create_internal_class (TypeBuilder tb);
61                 
62                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
63                 private extern void setup_generic_class (TypeBuilder tb);
64
65                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
66                 private extern EventInfo get_event_info (EventBuilder eb);
67
68                 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr) {
69                         this.parent = null;
70                         this.attrs = attr;
71                         this.class_size = -1;
72                         fullname = this.tname = "<Module>";
73                         this.nspace = "";
74                         pmodule = mb;
75                         setup_internal_class (this);
76                 }
77
78                 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type) {
79                         int sep_index;
80                         this.parent = parent;
81                         this.attrs = attr;
82                         this.class_size = type_size;
83                         this.packing_size = packing_size;
84                         this.nesting_type = nesting_type;
85                         sep_index = name.LastIndexOf('.');
86                         if (sep_index != -1) {
87                                 this.tname = name.Substring (sep_index + 1);
88                                 this.nspace = name.Substring (0, sep_index);
89                         } else {
90                                 this.tname = name;
91                                 this.nspace = "";
92                         }
93                         if (interfaces != null) {
94                                 this.interfaces = new Type[interfaces.Length];
95                                 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
96                         }
97                         pmodule = mb;
98                         // skip .<Module> ?
99                         table_idx = mb.get_next_table_index (this, 0x02, true);
100                         setup_internal_class (this);
101                         fullname = GetFullName ();
102                 }
103
104                 public override Assembly Assembly {
105                         get {return pmodule.Assembly;}
106                 }
107
108                 public override string AssemblyQualifiedName {
109                         get {
110                                 return fullname + ", " + Assembly.GetName().FullName;
111                         }
112                 }
113                 public override Type BaseType {
114                         get {
115                                 return parent;
116                         }
117                 }
118                 public override Type DeclaringType {get {return nesting_type;}}
119
120 /*              public override bool IsSubclassOf (Type c)
121                 {
122                         Type t;
123                         if (c == null)
124                                 return false;
125                         if (c == this)
126                                 return false;
127                         t = parent;
128                         while (t != null) {
129                                 if (c == t)
130                                         return true;
131                                 t = t.BaseType;
132                         }
133                         return false;
134                 }*/
135
136                 [MonoTODO]
137                 public override Type UnderlyingSystemType {
138                         get {
139                                 // This should return the type itself for non-enum types but 
140                                 // that breaks mcs.
141                                 if (fields != null) {
142                                         foreach (FieldBuilder f in fields) {
143                                                 if ((f != null) && (f.Attributes & FieldAttributes.Static) == 0)
144                                                         return f.FieldType;
145                                         }
146                                 }
147                                 throw new InvalidOperationException ("Underlying type information on enumeration is not specified.");
148                         }
149                 }
150
151                 string GetFullName () {
152                         if (nesting_type != null)
153                                 return String.Concat (nesting_type.FullName, "+", tname);
154                         if ((nspace != null) && (nspace.Length > 0))
155                                 return String.Concat (nspace, ".", tname);
156                         return tname;
157                 }
158         
159                 public override string FullName {
160                         get {
161                                 return fullname;
162                         }
163                 }
164         
165                 public override Guid GUID {
166                         get {
167                             throw not_supported ();
168                         }
169                 }
170
171                 public override Module Module {
172                         get {return pmodule;}
173                 }
174                 public override string Name {
175                         get {return tname;}
176                 }
177                 public override string Namespace {
178                         get {return nspace;}
179                 }
180                 public PackingSize PackingSize {
181                         get {return packing_size;}
182                 }
183                 public int Size {
184                         get { return class_size; }
185                 }
186                 public override Type ReflectedType {get {return nesting_type;}}
187
188                 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
189                         if (pset == null)
190                                 throw new ArgumentNullException ("pset");
191                         if ((action == SecurityAction.RequestMinimum) ||
192                                 (action == SecurityAction.RequestOptional) ||
193                                 (action == SecurityAction.RequestRefuse))
194                                 throw new ArgumentException ("Request* values are not permitted", "action");
195
196                         if (is_created)
197                                 throw not_after_created ();
198
199                         if (permissions != null) {
200                                 /* Check duplicate actions */
201                                 foreach (RefEmitPermissionSet set in permissions)
202                                         if (set.action == action)
203                                                 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
204
205                                 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
206                                 permissions.CopyTo (new_array, 0);
207                                 permissions = new_array;
208                         }
209                         else
210                                 permissions = new RefEmitPermissionSet [1];
211
212                         permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
213                         attrs |= TypeAttributes.HasSecurity;
214                 }
215
216                 public void AddInterfaceImplementation( Type interfaceType) {
217                         if (interfaceType == null)
218                                 throw new ArgumentNullException ("interfaceType");
219                         if (is_created)
220                                 throw not_after_created ();
221
222                         if (interfaces != null) {
223                                 // Check for duplicates
224                                 foreach (Type t in interfaces)
225                                         if (t == interfaceType)
226                                                 return;
227
228                                 Type[] ifnew = new Type [interfaces.Length + 1];
229                                 interfaces.CopyTo (ifnew, 0);
230                                 ifnew [interfaces.Length] = interfaceType;
231                                 interfaces = ifnew;
232                         } else {
233                                 interfaces = new Type [1];
234                                 interfaces [0] = interfaceType;
235                         }
236                 }
237
238                 [MonoTODO]
239                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
240                                                                        CallingConventions callConvention, Type[] types,
241                                                                        ParameterModifier[] modifiers)
242                 {
243                         if (ctors == null)
244                                 return null;
245
246                         ConstructorBuilder found = null;
247                         int count = 0;
248                         
249                         foreach (ConstructorBuilder cb in ctors){
250                                 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
251                                         continue;
252                                 found = cb;
253                                 count++;
254                         }
255
256                         if (count == 0)
257                                 return null;
258                         if (types == null){
259                                 if (count > 1)
260                                         throw new AmbiguousMatchException ();
261                                 return found;
262                         }
263                         MethodBase[] match = new MethodBase [count];
264                         if (count == 1)
265                                 match [0] = found;
266                         else {
267                                 count = 0;
268                                 foreach (ConstructorInfo m in ctors) {
269                                         if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
270                                                 continue;
271                                         match [count++] = m;
272                                 }
273                         }
274                         if (binder == null)
275                                 binder = Binder.DefaultBinder;
276                         return (ConstructorInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
277                 }
278
279                 public override bool IsDefined( Type attributeType, bool inherit)
280                 {
281                         /*
282                          * MS throws NotSupported here, but we can't because some corlib
283                          * classes make calls to IsDefined.
284                          */
285                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
286                 }
287                 
288                 public override object[] GetCustomAttributes(bool inherit)
289                 {
290                         throw not_supported ();
291                 }
292                 
293                 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
294                 {
295                         throw not_supported ();
296                 }
297
298                 public TypeBuilder DefineNestedType (string name) {
299                         return DefineNestedType (name, TypeAttributes.NestedPrivate, pmodule.assemblyb.corlib_object_type, null);
300                 }
301
302                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
303                         return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
304                 }
305
306                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
307                         return DefineNestedType (name, attr, parent, null);
308                 }
309
310                 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
311                                                       PackingSize packsize, int typesize)
312                 {
313                         check_name ("name", name);
314                         // Visibility must be NestedXXX
315                         /* This breaks mcs
316                         if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
317                                 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
318                                 throw new ArgumentException ("attr", "Bad type flags for nested type.");
319                         */
320                         if (interfaces != null)
321                                 foreach (Type iface in interfaces)
322                                         if (iface == null)
323                                                 throw new ArgumentNullException ("interfaces");
324
325                         TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize, this);
326                         res.fullname = res.GetFullName ();
327                         pmodule.RegisterTypeName (res, res.fullname);
328                         if (subtypes != null) {
329                                 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
330                                 System.Array.Copy (subtypes, new_types, subtypes.Length);
331                                 new_types [subtypes.Length] = res;
332                                 subtypes = new_types;
333                         } else {
334                                 subtypes = new TypeBuilder [1];
335                                 subtypes [0] = res;
336                         }
337                         return res;
338                 }
339
340                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
341                         return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
342                 }
343
344                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
345                         return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
346                 }
347
348                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
349                         return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
350                 }
351
352                 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
353                         return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
354                 }
355
356 #if NET_1_2
357                 public
358 #else
359                 internal
360 #endif
361                 ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
362                 {
363                         if (is_created)
364                                 throw not_after_created ();
365                         ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
366                         if (ctors != null) {
367                                 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
368                                 System.Array.Copy (ctors, new_ctors, ctors.Length);
369                                 new_ctors [ctors.Length] = cb;
370                                 ctors = new_ctors;
371                         } else {
372                                 ctors = new ConstructorBuilder [1];
373                                 ctors [0] = cb;
374                         }
375                         return cb;
376                 }
377
378                 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
379                 {
380                         ConstructorBuilder cb = DefineConstructor (attributes, CallingConventions.Standard, new Type [0]);
381
382                         Type parent_type;
383
384                         if (parent != null)
385                                 parent_type = parent;
386                         else
387                                 parent_type = pmodule.assemblyb.corlib_object_type;
388
389                         ConstructorInfo parent_constructor =
390                                 parent_type.GetConstructor (
391                                         BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
392                                         null, Type.EmptyTypes, null);
393
394                         ILGenerator ig = cb.GetILGenerator ();
395                         if (parent_constructor != null){
396                                 ig.Emit (OpCodes.Ldarg_0);
397                                 ig.Emit (OpCodes.Call, parent_constructor);
398                         }
399                         ig.Emit (OpCodes.Ret);
400                         return cb;
401                 }
402
403                 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
404                         return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
405                 }
406
407                 private void append_method (MethodBuilder mb) {
408                         if (methods != null) {
409                                 if (methods.Length == num_methods) {
410                                         MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
411                                         System.Array.Copy (methods, new_methods, num_methods);
412                                         methods = new_methods;
413                                 }
414                         } else {
415                                 methods = new MethodBuilder [1];
416                         }
417                         methods [num_methods] = mb;
418                         num_methods ++;
419                 }
420
421                 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
422                         return DefineMethod (name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
423                 }
424
425 #if NET_1_2
426                 public
427 #else
428                 internal
429 #endif
430                 MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) {
431                         check_name ("name", name);
432                         if (is_created)
433                                 throw not_after_created ();
434                         if (IsInterface && (
435                                 !((attributes & MethodAttributes.Abstract) != 0) || 
436                                 !((attributes & MethodAttributes.Virtual) != 0)))
437                                 throw new ArgumentException ("attributes", "Interface method must be abstract and virtual.");
438
439                         if (returnType == null)
440                                 returnType = pmodule.assemblyb.corlib_void_type;
441                         MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
442                         append_method (res);
443                         return res;
444                 }
445
446                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
447                         return DefinePInvokeMethod (name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
448                 }
449
450 #if NET_1_2
451                 public
452 #else
453                 internal
454 #endif
455                 MethodBuilder DefinePInvokeMethod (
456                                                 string name, 
457                                                 string dllName, 
458                                                 string entryName, MethodAttributes attributes, 
459                                                 CallingConventions callingConvention, 
460                                                 Type returnType, 
461                                                 Type[] returnTypeRequiredCustomModifiers, 
462                                                 Type[] returnTypeOptionalCustomModifiers, 
463                                                 Type[] parameterTypes, 
464                                                 Type[][] parameterTypeRequiredCustomModifiers, 
465                                                 Type[][] parameterTypeOptionalCustomModifiers, 
466                                                 CallingConvention nativeCallConv, 
467                                                 CharSet nativeCharSet) {
468                         check_name ("name", name);
469                         check_name ("dllName", dllName);
470                         check_name ("entryName", entryName);
471                         if ((attributes & MethodAttributes.Abstract) != 0)
472                                 throw new ArgumentException ("attributes", "PInvoke methods must be static and native and cannot be abstract.");
473                         if (IsInterface)
474                                 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");            
475                         if (is_created)
476                                 throw not_after_created ();
477
478                         MethodBuilder res 
479                                 = new MethodBuilder (
480                                                 this, 
481                                                 name, 
482                                                 attributes, 
483                                                 callingConvention,
484                                                 returnType, 
485                                                 returnTypeRequiredCustomModifiers, 
486                                                 returnTypeOptionalCustomModifiers, 
487                                                 parameterTypes, 
488                                                 parameterTypeRequiredCustomModifiers, 
489                                                 parameterTypeOptionalCustomModifiers,
490                                                 dllName, 
491                                                 entryName, 
492                                                 nativeCallConv, 
493                                                 nativeCharSet);
494                         append_method (res);
495                         return res;
496                 }
497
498                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
499                         return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
500                                 nativeCallConv, nativeCharSet);
501                 }
502
503                 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
504                         if (methodInfoBody == null)
505                                 throw new ArgumentNullException ("methodInfoBody");
506                         if (methodInfoDeclaration == null)
507                                 throw new ArgumentNullException ("methodInfoDeclaration");
508                         if (is_created)
509                                 throw not_after_created ();
510
511                         if (methodInfoBody is MethodBuilder) {
512                                 MethodBuilder mb = (MethodBuilder)methodInfoBody;
513                                 mb.set_override (methodInfoDeclaration);
514                         }
515                 }
516
517                 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
518                         return DefineField (fieldName, type, null, null, attributes);
519                 }
520
521 #if NET_1_2
522                 public
523 #else
524                 internal
525 #endif
526             FieldBuilder DefineField( string fieldName, Type type, Type[] requiredCustomAttributes, Type[] optionalCustomAttributes, FieldAttributes attributes) {
527                         check_name ("fieldName", fieldName);
528                         if (type == typeof (void))
529                                 throw new ArgumentException ("type",  "Bad field type in defining field.");
530                         if (is_created)
531                                 throw not_after_created ();
532
533                         FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomAttributes, optionalCustomAttributes);
534                         if (fields != null) {
535                                 if (fields.Length == num_fields) {
536                                         FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
537                                         System.Array.Copy (fields, new_fields, num_fields);
538                                         fields = new_fields;
539                                 }
540                                 fields [num_fields] = res;
541                                 num_fields ++;
542                         } else {
543                                 fields = new FieldBuilder [1];
544                                 fields [0] = res;
545                                 num_fields ++;
546                                 create_internal_class (this);
547                         }
548                         return res;
549                 }
550
551                 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
552                         check_name ("name", name);
553                         if (parameterTypes != null)
554                                 foreach (Type param in parameterTypes)
555                                         if (param == null)
556                                                 throw new ArgumentNullException ("parameterTypes");
557                         if (is_created)
558                                 throw not_after_created ();
559
560                         PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
561
562                         if (properties != null) {
563                                 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
564                                 System.Array.Copy (properties, new_properties, properties.Length);
565                                 new_properties [properties.Length] = res;
566                                 properties = new_properties;
567                         } else {
568                                 properties = new PropertyBuilder [1];
569                                 properties [0] = res;
570                         }
571                         return res;
572                 }
573
574                 public ConstructorBuilder DefineTypeInitializer() {
575                         return DefineConstructor (MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, null);
576                 }
577
578                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
579                 private extern Type create_runtime_class (TypeBuilder tb);
580
581                 private bool is_nested_in (Type t) {
582                         while (t != null) {
583                                 if (t == this)
584                                         return true;
585                                 else
586                                         t = t.DeclaringType;
587                         }
588                         return false;
589                 }
590                 
591                 public Type CreateType() {
592                         /* handle nesting_type */
593                         if (is_created)
594                                 throw not_after_created ();
595
596                         // Fire TypeResolve events for fields whose type is an unfinished
597                         // value type.
598                         if (fields != null) {
599                                 foreach (FieldBuilder fb in fields) {
600                                         if (fb == null)
601                                                 continue;
602                                         Type ft = fb.FieldType;
603                                         if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
604                                                 TypeBuilder tb = (TypeBuilder)ft;
605                                                 if (!tb.is_created) {
606                                                         AppDomain.CurrentDomain.DoTypeResolve (tb);
607                                                         if (!tb.is_created) {
608                                                                 // FIXME: We should throw an exception here,
609                                                                 // but mcs expects that the type is created
610                                                                 // even if the exception is thrown
611                                                                 //throw new TypeLoadException ("Could not load type " + tb);
612                                                         }
613                                                 }
614                                         }
615                                 }
616                         }
617
618                         if (methods != null) {
619                                 for (int i = 0; i < num_methods; ++i)
620                                         ((MethodBuilder)(methods[i])).fixup ();
621                         }
622
623                         //
624                         // On classes, define a default constructor if not provided
625                         //
626                         if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>"))
627                                 DefineDefaultConstructor (MethodAttributes.Public);
628
629                         if (ctors != null){
630                                 foreach (ConstructorBuilder ctor in ctors) 
631                                         ctor.fixup ();
632                         }
633
634                         created = create_runtime_class (this);
635                         if (created != null)
636                                 return created;
637                         return this;
638                 }
639
640                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
641                 {
642                         if (ctors == null)
643                                 return new ConstructorInfo [0];
644                         ArrayList l = new ArrayList ();
645                         bool match;
646                         MethodAttributes mattrs;
647                         
648                         foreach (ConstructorBuilder c in ctors) {
649                                 match = false;
650                                 mattrs = c.Attributes;
651                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
652                                         if ((bindingAttr & BindingFlags.Public) != 0)
653                                                 match = true;
654                                 } else {
655                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
656                                                 match = true;
657                                 }
658                                 if (!match)
659                                         continue;
660                                 match = false;
661                                 if ((mattrs & MethodAttributes.Static) != 0) {
662                                         if ((bindingAttr & BindingFlags.Static) != 0)
663                                                 match = true;
664                                 } else {
665                                         if ((bindingAttr & BindingFlags.Instance) != 0)
666                                                 match = true;
667                                 }
668                                 if (!match)
669                                         continue;
670                                 l.Add (c);
671                         }
672                         ConstructorInfo[] result = new ConstructorInfo [l.Count];
673                         l.CopyTo (result);
674                         return result;
675                 }
676
677                 public override Type GetElementType () { 
678                         throw not_supported ();
679                 }
680
681                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
682                         throw not_supported ();
683                 }
684
685                 /* Needed to keep signature compatibility with MS.NET */
686                 public override EventInfo[] GetEvents ()
687                 {
688                         return GetEvents (DefaultBindingFlags);
689                 }
690
691                 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
692                         // FIXME: Under MS.NET, this throws a NotImplementedException
693                         // But mcs calls this method. How can that be?
694                         return new EventInfo [0];
695                 }
696
697                 // This is only used from MonoGenericInst.initialize().
698                 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
699                 {
700                         if (events == null)
701                                 return new EventInfo [0];
702                         ArrayList l = new ArrayList ();
703                         bool match;
704                         MethodAttributes mattrs;
705                         MethodInfo accessor;
706
707                         foreach (EventBuilder eb in events) {
708                                 if (eb == null)
709                                         continue;
710                                 EventInfo c = get_event_info (eb);
711                                 match = false;
712                                 accessor = c.GetAddMethod (true);
713                                 if (accessor == null)
714                                         accessor = c.GetRemoveMethod (true);
715                                 if (accessor == null)
716                                         continue;
717                                 mattrs = accessor.Attributes;
718                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
719                                         if ((bindingAttr & BindingFlags.Public) != 0)
720                                                 match = true;
721                                 } else {
722                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
723                                                 match = true;
724                                 }
725                                 if (!match)
726                                         continue;
727                                 match = false;
728                                 if ((mattrs & MethodAttributes.Static) != 0) {
729                                         if ((bindingAttr & BindingFlags.Static) != 0)
730                                                 match = true;
731                                 } else {
732                                         if ((bindingAttr & BindingFlags.Instance) != 0)
733                                                 match = true;
734                                 }
735                                 if (!match)
736                                         continue;
737                                 l.Add (c);
738                         }
739                         EventInfo[] result = new EventInfo [l.Count];
740                         l.CopyTo (result);
741                         return result;
742                 }
743
744                 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
745                         if (fields == null)
746                                 return null;
747
748                         bool match;
749                         FieldAttributes mattrs;
750                         
751                         foreach (FieldInfo c in fields) {
752                                 if (c == null)
753                                         continue;
754                                 if (c.Name != name)
755                                         continue;
756                                 match = false;
757                                 mattrs = c.Attributes;
758                                 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
759                                         if ((bindingAttr & BindingFlags.Public) != 0)
760                                                 match = true;
761                                 } else {
762                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
763                                                 match = true;
764                                 }
765                                 if (!match)
766                                         continue;
767                                 match = false;
768                                 if ((mattrs & FieldAttributes.Static) != 0) {
769                                         if ((bindingAttr & BindingFlags.Static) != 0)
770                                                 match = true;
771                                 } else {
772                                         if ((bindingAttr & BindingFlags.Instance) != 0)
773                                                 match = true;
774                                 }
775                                 if (!match)
776                                         continue;
777                                 return c;
778                         }
779                         return null;
780                 }
781
782                 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
783                         if (fields == null)
784                                 return new FieldInfo [0];
785                         ArrayList l = new ArrayList ();
786                         bool match;
787                         FieldAttributes mattrs;
788                         
789                         foreach (FieldInfo c in fields) {
790                                 if (c == null)
791                                         continue;
792                                 match = false;
793                                 mattrs = c.Attributes;
794                                 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
795                                         if ((bindingAttr & BindingFlags.Public) != 0)
796                                                 match = true;
797                                 } else {
798                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
799                                                 match = true;
800                                 }
801                                 if (!match)
802                                         continue;
803                                 match = false;
804                                 if ((mattrs & FieldAttributes.Static) != 0) {
805                                         if ((bindingAttr & BindingFlags.Static) != 0)
806                                                 match = true;
807                                 } else {
808                                         if ((bindingAttr & BindingFlags.Instance) != 0)
809                                                 match = true;
810                                 }
811                                 if (!match)
812                                         continue;
813                                 l.Add (c);
814                         }
815                         FieldInfo[] result = new FieldInfo [l.Count];
816                         l.CopyTo (result);
817                         return result;
818                 }
819
820                 public override Type GetInterface (string name, bool ignoreCase) {
821                         throw not_supported ();
822                 }
823                 
824                 public override Type[] GetInterfaces () {
825                         if (interfaces != null) {
826                                 Type[] ret = new Type [interfaces.Length];
827                                 interfaces.CopyTo (ret, 0);
828                                 return ret;
829                         } else {
830                                 return Type.EmptyTypes;
831                         }
832                 }
833
834                 public override MemberInfo[] GetMember (string name, MemberTypes type,
835                                                                                                 BindingFlags bindingAttr) {
836                         throw not_supported ();
837                 }
838
839                 public override MemberInfo[] GetMembers (BindingFlags bindingAttr) {
840                         throw not_supported ();
841                 }
842
843                 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type) {
844                         MethodInfo[] candidates;
845                         if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
846                                 MethodInfo[] parent_methods = parent.GetMethods (bindingAttr);
847                                 if (methods == null)
848                                         candidates = parent_methods;
849                                 else {
850                                         candidates = new MethodInfo [methods.Length + parent_methods.Length];
851                                         parent_methods.CopyTo (candidates, 0);
852                                         methods.CopyTo (candidates, parent_methods.Length);
853                                 }
854                         }
855                         else
856                                 candidates = methods;
857                                         
858                         if (candidates == null)
859                                 return new MethodInfo [0];
860
861                         ArrayList l = new ArrayList ();
862                         bool match;
863                         MethodAttributes mattrs;
864
865                         foreach (MethodInfo c in candidates) {
866                                 if (c == null)
867                                         continue;
868                                 if (name != null) {
869                                         if (String.Compare (c.Name, name, ignoreCase) != 0)
870                                                 continue;
871                                 }
872                                 match = false;
873                                 mattrs = c.Attributes;
874                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
875                                         if ((bindingAttr & BindingFlags.Public) != 0)
876                                                 match = true;
877                                 } else {
878                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
879                                                 match = true;
880                                 }
881                                 if (!match)
882                                         continue;
883                                 match = false;
884                                 if ((mattrs & MethodAttributes.Static) != 0) {
885                                         if ((bindingAttr & BindingFlags.Static) != 0)
886                                                 match = true;
887                                 } else {
888                                         if ((bindingAttr & BindingFlags.Instance) != 0)
889                                                 match = true;
890                                 }
891                                 if (!match)
892                                         continue;
893                                 l.Add (c);
894                         }
895
896                         MethodInfo[] result = new MethodInfo [l.Count];
897                         l.CopyTo (result);
898                         return result;
899                 }
900
901                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
902                         return GetMethodsByName (null, bindingAttr, false, this);
903                 }
904
905                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
906                                                              Binder binder,
907                                                              CallingConventions callConvention,
908                                                              Type[] types, ParameterModifier[] modifiers)
909                 {
910                         if (!is_created)
911                                 /* MS.Net throws this exception if the type is unfinished... */
912                                 throw not_supported ();
913
914                         bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
915                         MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
916                         MethodInfo found = null;
917                         MethodBase[] match;
918                         int typesLen = (types != null) ? types.Length : 0;
919                         int count = 0;
920                         
921                         foreach (MethodInfo m in methods) {
922                                 // Under MS.NET, Standard|HasThis matches Standard...
923                                 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
924                                         continue;
925                                 found = m;
926                                 count++;
927                         }
928
929                         if (count == 0)
930                                 return null;
931                         
932                         if (count == 1 && typesLen == 0) 
933                                 return found;
934
935                         match = new MethodBase [count];
936                         if (count == 1)
937                                 match [0] = found;
938                         else {
939                                 count = 0;
940                                 foreach (MethodInfo m in methods) {
941                                         if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
942                                                 continue;
943                                         match [count++] = m;
944                                 }
945                         }
946                         
947                         if (types == null) 
948                                 return (MethodInfo) Binder.FindMostDerivedMatch (match);
949
950                         if (binder == null)
951                                 binder = Binder.DefaultBinder;
952                         
953                         return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
954                 }
955
956                 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
957                         throw not_supported ();
958                 }
959
960                 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
961                         bool match;
962                         ArrayList result = new ArrayList ();
963                 
964                         if (subtypes == null)
965                                 return Type.EmptyTypes;
966                         foreach (TypeBuilder t in subtypes) {
967                                 match = false;
968                                 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
969                                         if ((bindingAttr & BindingFlags.Public) != 0)
970                                                 match = true;
971                                 } else {
972                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
973                                                 match = true;
974                                 }
975                                 if (!match)
976                                         continue;
977                                 result.Add (t);
978                         }
979                         Type[] r = new Type [result.Count];
980                         result.CopyTo (r);
981                         return r;
982                 }
983
984                 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
985                         if (properties == null)
986                                 return new PropertyInfo [0];
987                         ArrayList l = new ArrayList ();
988                         bool match;
989                         MethodAttributes mattrs;
990                         MethodInfo accessor;
991                         
992                         foreach (PropertyInfo c in properties) {
993                                 match = false;
994                                 accessor = c.GetGetMethod (true);
995                                 if (accessor == null)
996                                         accessor = c.GetSetMethod (true);
997                                 if (accessor == null)
998                                         continue;
999                                 mattrs = accessor.Attributes;
1000                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1001                                         if ((bindingAttr & BindingFlags.Public) != 0)
1002                                                 match = true;
1003                                 } else {
1004                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
1005                                                 match = true;
1006                                 }
1007                                 if (!match)
1008                                         continue;
1009                                 match = false;
1010                                 if ((mattrs & MethodAttributes.Static) != 0) {
1011                                         if ((bindingAttr & BindingFlags.Static) != 0)
1012                                                 match = true;
1013                                 } else {
1014                                         if ((bindingAttr & BindingFlags.Instance) != 0)
1015                                                 match = true;
1016                                 }
1017                                 if (!match)
1018                                         continue;
1019                                 l.Add (c);
1020                         }
1021                         PropertyInfo[] result = new PropertyInfo [l.Count];
1022                         l.CopyTo (result);
1023                         return result;
1024                 }
1025                 
1026                 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
1027                         throw not_supported ();
1028                 }
1029
1030                 protected override bool HasElementTypeImpl () {
1031                         // According to the MSDN docs, this is supported for TypeBuilders,
1032                         // but in reality, it is not
1033                         throw not_supported ();
1034                         //                      return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
1035                 }
1036
1037                 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
1038                         throw not_supported ();
1039                 }
1040
1041                 protected override bool IsArrayImpl ()
1042                 {
1043                         return Type.IsArrayImpl (this);
1044                 }
1045
1046                 protected override bool IsByRefImpl () {
1047                         // FIXME
1048                         return false;
1049                 }
1050                 protected override bool IsCOMObjectImpl () {
1051                         return false;
1052                 }
1053                 protected override bool IsPointerImpl () {
1054                         // FIXME
1055                         return false;
1056                 }
1057                 protected override bool IsPrimitiveImpl () {
1058                         // FIXME
1059                         return false;
1060                 }
1061                 protected override bool IsValueTypeImpl () {
1062                         return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
1063                                 this != pmodule.assemblyb.corlib_value_type &&
1064                                 this != pmodule.assemblyb.corlib_enum_type);
1065                 }
1066                 
1067                 public override RuntimeTypeHandle TypeHandle { 
1068                         get { 
1069                                 throw not_supported (); 
1070                         } 
1071                 }
1072
1073                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
1074                         if (customBuilder == null)
1075                                 throw new ArgumentNullException ("customBuilder");
1076
1077                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
1078                         if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1079                                 byte[] data = customBuilder.Data;
1080                                 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1081                                 layout_kind = (int)data [2];
1082                                 layout_kind |= ((int)data [3]) << 8;
1083                                 attrs &= ~TypeAttributes.LayoutMask;
1084                                 switch ((LayoutKind)layout_kind) {
1085                                 case LayoutKind.Auto:
1086                                         attrs |= TypeAttributes.AutoLayout;
1087                                         break;
1088                                 case LayoutKind.Explicit:
1089                                         attrs |= TypeAttributes.ExplicitLayout;
1090                                         break;
1091                                 case LayoutKind.Sequential:
1092                                         attrs |= TypeAttributes.SequentialLayout;
1093                                         break;
1094                                 default:
1095                                         // we should ignore it since it can be any value anyway...
1096                                         throw new Exception ("Error in customattr");
1097                                 }
1098                                 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
1099                                 int pos = 6;
1100                                 if (first_type_name == "System.Int16")
1101                                         pos = 4;
1102                                 int nnamed = (int)data [pos++];
1103                                 nnamed |= ((int)data [pos++]) << 8;
1104                                 for (int i = 0; i < nnamed; ++i) {
1105                                         byte named_type = data [pos++];
1106                                         byte type = data [pos++];
1107                                         int len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1108                                         string named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1109                                         pos += len;
1110                                         /* all the fields are integers in StructLayout */
1111                                         int value = (int)data [pos++];
1112                                         value |= ((int)data [pos++]) << 8;
1113                                         value |= ((int)data [pos++]) << 16;
1114                                         value |= ((int)data [pos++]) << 24;
1115                                         switch (named_name) {
1116                                         case "CharSet":
1117                                                 switch ((CharSet)value) {
1118                                                 case CharSet.None:
1119                                                 case CharSet.Ansi:
1120                                                         break;
1121                                                 case CharSet.Unicode:
1122                                                         attrs |= TypeAttributes.UnicodeClass;
1123                                                         break;
1124                                                 case CharSet.Auto:
1125                                                         attrs |= TypeAttributes.AutoClass;
1126                                                         break;
1127                                                 default:
1128                                                         break; // error out...
1129                                                 }
1130                                                 break;
1131                                         case "Pack":
1132                                                 packing_size = (PackingSize)value;
1133                                                 break;
1134                                         case "Size":
1135                                                 class_size = value;
1136                                                 break;
1137                                         default:
1138                                                 break; // error out...
1139                                         }
1140                                 }
1141                                 return;
1142                         } else if (attrname == "System.SerializableAttribute") {
1143                                 attrs |= TypeAttributes.Serializable;
1144                                 return;
1145                         }
1146                         if (cattrs != null) {
1147                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1148                                 cattrs.CopyTo (new_array, 0);
1149                                 new_array [cattrs.Length] = customBuilder;
1150                                 cattrs = new_array;
1151                         } else {
1152                                 cattrs = new CustomAttributeBuilder [1];
1153                                 cattrs [0] = customBuilder;
1154                         }
1155                 }
1156                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
1157                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1158                 }
1159
1160                 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
1161                         check_name ("name", name);
1162                         if (eventtype == null)
1163                                 throw new ArgumentNullException ("eventtype");
1164                         if (is_created)
1165                                 throw not_after_created ();
1166
1167                         EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1168                         if (events != null) {
1169                                 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1170                                 System.Array.Copy (events, new_events, events.Length);
1171                                 new_events [events.Length] = res;
1172                                 events = new_events;
1173                         } else {
1174                                 events = new EventBuilder [1];
1175                                 events [0] = res;
1176                         }
1177                         return res;
1178                 }
1179
1180                 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
1181                         if (data == null)
1182                                 throw new ArgumentNullException ("data");
1183                         if ((data.Length == 0) || (data.Length > 0x3f0000))
1184                                 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
1185
1186                         FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1187                         res.SetRVAData (data);
1188
1189                         return res;
1190                 }
1191
1192                 static int UnmanagedDataCount = 0;
1193                 
1194                 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
1195                         check_name ("name", name);
1196                         if ((size <= 0) || (size > 0x3f0000))
1197                                 throw new ArgumentException ("size", "Data size must be > 0 and < 0x3f0000");
1198                         if (is_created)
1199                                 throw not_after_created ();
1200
1201                         string s = "$ArrayType$"+UnmanagedDataCount.ToString();
1202                         UnmanagedDataCount++;
1203                         TypeBuilder datablobtype = DefineNestedType (s,
1204                                 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1205                                 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1206                         datablobtype.CreateType ();
1207                         return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1208                 }
1209
1210                 public TypeToken TypeToken {
1211                         get {
1212                                 return new TypeToken (0x02000000 | table_idx);
1213                         }
1214                 }
1215                 public void SetParent (Type parentType) {
1216                         if (parentType == null)
1217                                 throw new ArgumentNullException ("parentType");
1218                         if (is_created)
1219                                 throw not_after_created ();
1220
1221                         parent = parentType;
1222                 }
1223                 internal int get_next_table_index (object obj, int table, bool inc) {
1224                         return pmodule.get_next_table_index (obj, table, inc);
1225                 }
1226
1227                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1228                 {
1229                         if (created == null)
1230                                 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1231
1232                         return created.GetInterfaceMap (interfaceType);
1233                 }
1234
1235                 internal bool is_created {
1236                         get {
1237                                 return created != null;
1238                         }
1239                 }
1240
1241                 private Exception not_supported ()
1242                 {
1243                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1244                 }
1245
1246                 private Exception not_after_created ()
1247                 {
1248                         return new InvalidOperationException ("Unable to change after type has been created.");
1249                 }
1250
1251                 private void check_name (string argName, string name)
1252                 {
1253                         if (name == null)
1254                                 throw new ArgumentNullException (argName);
1255                         if (name == "")
1256                                 throw new ArgumentException (argName, "Empty name is not legal.");
1257                         if (name.IndexOf ((char)0) != -1)
1258                                 throw new ArgumentException (argName, "Illegal name.");
1259                 }
1260
1261 #if NET_1_2
1262                 public override Type GetGenericTypeDefinition ()
1263                 {
1264                         setup_generic_class (this);
1265
1266                         return base.GetGenericTypeDefinition ();
1267                 }
1268
1269                 public override bool HasGenericArguments {
1270                         get {
1271                                 throw new NotImplementedException ();
1272                         }
1273                 }
1274
1275                 public override bool ContainsGenericParameters {
1276                         get {
1277                                 return generic_params != null;
1278                         }
1279                 }
1280
1281                 public extern override bool IsGenericParameter {
1282                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
1283                         get;
1284                 }
1285
1286                 public override int GenericParameterPosition {
1287                         get {
1288                                 throw new NotImplementedException ();
1289                         }
1290                 }
1291
1292                 public override MethodInfo DeclaringMethod {
1293                         get {
1294                                 throw new NotImplementedException ();
1295                         }
1296                 }
1297
1298                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1299                 private extern MonoGenericParam define_generic_parameter (string name, int index);
1300                 
1301                 public Type DefineGenericParameter (string name)
1302                 {
1303                         int index;
1304                         if (generic_params != null) {
1305                                 MonoGenericParam[] new_generic_params = new MonoGenericParam [generic_params.Length+1];
1306                                 System.Array.Copy (generic_params, new_generic_params, generic_params.Length);
1307                                 index = generic_params.Length;
1308                                 generic_params = new_generic_params;
1309                         } else {
1310                                 generic_params = new MonoGenericParam [1];
1311                                 index = 0;
1312                         }
1313
1314                         generic_params [index] = define_generic_parameter (name, index);
1315                         return generic_params [index];
1316                 }
1317
1318                 public void SetGenericParameterConstraints (int index, Type[] constraints, bool has_ctor_constraint)
1319                 {
1320                         generic_params [index].SetConstraints (constraints, has_ctor_constraint);
1321                 }
1322
1323                 public MethodBuilder DefineGenericMethod (string name, MethodAttributes attributes)
1324                 {
1325                         return DefineMethod (name, attributes, CallingConventions.Standard, null, null);
1326                 }
1327 #endif
1328         }
1329 }