2003-09-29 Zoltan Varga <vargaz@freemail.hu>
[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         private string tname;
25         private string nspace;
26         private Type parent;
27         private Type nesting_type;
28         private Type[] interfaces;
29         private MethodBuilder[] methods;
30         private ConstructorBuilder[] ctors;
31         private PropertyBuilder[] properties;
32         private FieldBuilder[] fields;
33         private EventBuilder[] events;
34         private CustomAttributeBuilder[] cattrs;
35         internal TypeBuilder[] subtypes;
36         private TypeAttributes attrs;
37         private int table_idx;
38         private ModuleBuilder pmodule;
39         private int class_size;
40         private PackingSize packing_size;
41         private MonoGenericParam[] generic_params;
42         private Type created;
43         string fullname;
44
45         public const int UnspecifiedTypeSize = 0;
46
47                 protected override TypeAttributes GetAttributeFlagsImpl () {
48                         return attrs;
49                 }
50                 
51                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
52                 private extern void setup_internal_class (TypeBuilder tb);
53                 
54                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
55                 private extern void create_internal_class (TypeBuilder tb);
56                 
57                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
58                 private extern void setup_generic_class (TypeBuilder tb);
59
60                 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr) {
61                         this.parent = null;
62                         this.attrs = attr;
63                         this.class_size = -1;
64                         fullname = this.tname = "<Module>";
65                         this.nspace = "";
66                         pmodule = mb;
67                         setup_internal_class (this);
68                 }
69
70                 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type) {
71                         int sep_index;
72                         this.parent = parent;
73                         this.attrs = attr;
74                         this.class_size = type_size;
75                         this.packing_size = packing_size;
76                         this.nesting_type = nesting_type;
77                         sep_index = name.LastIndexOf('.');
78                         if (sep_index != -1) {
79                                 this.tname = name.Substring (sep_index + 1);
80                                 this.nspace = name.Substring (0, sep_index);
81                         } else {
82                                 this.tname = name;
83                                 this.nspace = "";
84                         }
85                         if (interfaces != null) {
86                                 this.interfaces = new Type[interfaces.Length];
87                                 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
88                         }
89                         pmodule = mb;
90                         // skip .<Module> ?
91                         table_idx = mb.get_next_table_index (this, 0x02, true);
92                         setup_internal_class (this);
93                         fullname = GetFullName ();
94                 }
95
96                 public override Assembly Assembly {
97                         get {return pmodule.Assembly;}
98                 }
99
100                 public override string AssemblyQualifiedName {
101                         get {
102                                 return fullname + ", " + Assembly.GetName().FullName;
103                         }
104                 }
105                 public override Type BaseType {
106                         get {
107                                 return parent;
108                         }
109                 }
110                 public override Type DeclaringType {get {return nesting_type;}}
111
112 /*              public override bool IsSubclassOf (Type c)
113                 {
114                         Type t;
115                         if (c == null)
116                                 return false;
117                         if (c == this)
118                                 return false;
119                         t = parent;
120                         while (t != null) {
121                                 if (c == t)
122                                         return true;
123                                 t = t.BaseType;
124                         }
125                         return false;
126                 }*/
127
128                 [MonoTODO]
129                 public override Type UnderlyingSystemType {
130                         get {
131                                 // This should return the type itself for non-enum types but 
132                                 // that breaks mcs.
133                                 if (fields != null) {
134                                         foreach (FieldBuilder f in fields) {
135                                                 if ((f.Attributes & FieldAttributes.Static) == 0)
136                                                         return f.FieldType;
137                                         }
138                                 }
139                                 throw new InvalidOperationException ("Underlying type information on enumeration is not specified.");
140                         }
141                 }
142
143                 string GetFullName () {
144                         if (nesting_type != null)
145                                 return String.Concat (nesting_type.FullName, "+", tname);
146                         if ((nspace != null) && (nspace.Length > 0))
147                                 return String.Concat (nspace, ".", tname);
148                         return tname;
149                 }
150         
151                 public override string FullName {
152                         get {
153                                 return fullname;
154                         }
155                 }
156         
157                 public override Guid GUID {
158                         get {
159                             throw not_supported ();
160                         }
161                 }
162
163                 public override Module Module {
164                         get {return pmodule;}
165                 }
166                 public override string Name {
167                         get {return tname;}
168                 }
169                 public override string Namespace {
170                         get {return nspace;}
171                 }
172                 public PackingSize PackingSize {
173                         get {return packing_size;}
174                 }
175                 public int Size {
176                         get { return class_size; }
177                 }
178                 public override Type ReflectedType {get {return nesting_type;}}
179                 public override MemberTypes MemberType { 
180                         get {return MemberTypes.TypeInfo;}
181                 }
182
183                 [MonoTODO]
184                 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
185                         throw new NotImplementedException ();
186                 }
187
188                 public void AddInterfaceImplementation( Type interfaceType) {
189                         if (interfaceType == null)
190                                 throw new ArgumentNullException ("interfaceType");
191                         if (is_created)
192                                 throw not_after_created ();
193
194                         if (interfaces != null) {
195                                 // Check for duplicates
196                                 foreach (Type t in interfaces)
197                                         if (t == interfaceType)
198                                                 return;
199
200                                 Type[] ifnew = new Type [interfaces.Length + 1];
201                                 interfaces.CopyTo (ifnew, 0);
202                                 ifnew [interfaces.Length] = interfaceType;
203                                 interfaces = ifnew;
204                         } else {
205                                 interfaces = new Type [1];
206                                 interfaces [0] = interfaceType;
207                         }
208                 }
209
210                 [MonoTODO]
211                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
212                                                                        CallingConventions callConvention, Type[] types,
213                                                                        ParameterModifier[] modifiers)
214                 {
215                         if (ctors == null)
216                                 return null;
217
218                         ConstructorBuilder found = null;
219                         int count = 0;
220                         
221                         foreach (ConstructorBuilder cb in ctors){
222                                 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
223                                         continue;
224                                 found = cb;
225                                 count++;
226                         }
227
228                         if (count == 0)
229                                 return null;
230                         if (types == null){
231                                 if (count > 1)
232                                         throw new AmbiguousMatchException ();
233                                 return found;
234                         }
235                         MethodBase[] match = new MethodBase [count];
236                         if (count == 1)
237                                 match [0] = found;
238                         else {
239                                 count = 0;
240                                 foreach (ConstructorInfo m in ctors) {
241                                         if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
242                                                 continue;
243                                         match [count++] = m;
244                                 }
245                         }
246                         if (binder == null)
247                                 binder = Binder.DefaultBinder;
248                         return (ConstructorInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
249                 }
250
251                 public override bool IsDefined( Type attributeType, bool inherit)
252                 {
253                         throw not_supported ();
254                 }
255                 
256                 public override object[] GetCustomAttributes(bool inherit)
257                 {
258                         throw not_supported ();
259                 }
260                 
261                 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
262                 {
263                         throw not_supported ();
264                 }
265
266                 public TypeBuilder DefineNestedType (string name) {
267                         return DefineNestedType (name, TypeAttributes.NestedPrivate, pmodule.assemblyb.corlib_object_type, null);
268                 }
269
270                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
271                         return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
272                 }
273
274                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
275                         return DefineNestedType (name, attr, parent, null);
276                 }
277
278                 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
279                                                       PackingSize packsize, int typesize)
280                 {
281                         check_name ("name", name);
282                         // Visibility must be NestedXXX
283                         /* This breaks mcs
284                         if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
285                                 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
286                                 throw new ArgumentException ("attr", "Bad type flags for nested type.");
287                         */
288                         if (interfaces != null)
289                                 foreach (Type iface in interfaces)
290                                         if (iface == null)
291                                                 throw new ArgumentNullException ("interfaces");
292
293                         TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize, this);
294                         res.fullname = res.GetFullName ();
295                         pmodule.RegisterTypeName (res, res.fullname);
296                         if (subtypes != null) {
297                                 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
298                                 System.Array.Copy (subtypes, new_types, subtypes.Length);
299                                 new_types [subtypes.Length] = res;
300                                 subtypes = new_types;
301                         } else {
302                                 subtypes = new TypeBuilder [1];
303                                 subtypes [0] = res;
304                         }
305                         return res;
306                 }
307
308                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
309                         return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
310                 }
311
312                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
313                         return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
314                 }
315
316                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
317                         return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
318                 }
319
320                 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes)
321                 {
322                         if (is_created)
323                                 throw not_after_created ();
324                         ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes);
325                         if (ctors != null) {
326                                 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
327                                 System.Array.Copy (ctors, new_ctors, ctors.Length);
328                                 new_ctors [ctors.Length] = cb;
329                                 ctors = new_ctors;
330                         } else {
331                                 ctors = new ConstructorBuilder [1];
332                                 ctors [0] = cb;
333                         }
334                         return cb;
335                 }
336
337                 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
338                 {
339                         ConstructorBuilder cb = DefineConstructor (attributes, CallingConventions.Standard, null);
340
341                         Type parent_type;
342
343                         if (parent != null)
344                                 parent_type = parent;
345                         else
346                                 parent_type = pmodule.assemblyb.corlib_object_type;
347
348                         ConstructorInfo parent_constructor =
349                                 parent_type.GetConstructor (
350                                         BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
351                                         null, Type.EmptyTypes, null);
352
353                         ILGenerator ig = cb.GetILGenerator ();
354                         if (parent_constructor != null){
355                                 ig.Emit (OpCodes.Ldarg_0);
356                                 ig.Emit (OpCodes.Call, parent_constructor);
357                         }
358                         ig.Emit (OpCodes.Ret);
359                         return cb;
360                 }
361
362                 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
363                         return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
364                 }
365
366                 private void append_method (MethodBuilder mb) {
367                         if (methods != null) {
368                                 MethodBuilder[] new_methods = new MethodBuilder [methods.Length+1];
369                                 System.Array.Copy (methods, new_methods, methods.Length);
370                                 new_methods [methods.Length] = mb;
371                                 methods = new_methods;
372                         } else {
373                                 methods = new MethodBuilder [1];
374                                 methods [0] = mb;
375                         }
376                 }
377
378                 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
379                         check_name ("name", name);
380                         if (is_created)
381                                 throw not_after_created ();
382                         if (IsInterface && (
383                                 !((attributes & MethodAttributes.Abstract) != 0) || 
384                                 !((attributes & MethodAttributes.Virtual) != 0)))
385                                 throw new ArgumentException ("attributes", "Interface method must be abstract and virtual.");
386
387                         if (returnType == null)
388                                 returnType = pmodule.assemblyb.corlib_void_type;
389                         MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes);
390                         append_method (res);
391                         return res;
392                 }
393
394                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
395                         check_name ("name", name);
396                         check_name ("dllName", dllName);
397                         check_name ("entryName", entryName);
398                         if ((attributes & MethodAttributes.Abstract) != 0)
399                                 throw new ArgumentException ("attributes", "PInvoke methods must be static and native and cannot be abstract.");
400                         if (IsInterface)
401                                 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");            
402                         if (is_created)
403                                 throw not_after_created ();
404
405                         MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes,
406                                 dllName, entryName, nativeCallConv, nativeCharSet);
407                         append_method (res);
408                         return res;
409                 }
410
411                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
412                         return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
413                                 nativeCallConv, nativeCharSet);
414                 }
415
416                 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
417                         if (methodInfoBody == null)
418                                 throw new ArgumentNullException ("methodInfoBody");
419                         if (methodInfoDeclaration == null)
420                                 throw new ArgumentNullException ("methodInfoDeclaration");
421                         if (is_created)
422                                 throw not_after_created ();
423
424                         if (methodInfoBody is MethodBuilder) {
425                                 MethodBuilder mb = (MethodBuilder)methodInfoBody;
426                                 mb.set_override (methodInfoDeclaration);
427                         }
428                 }
429
430                 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
431                         check_name ("fieldName", fieldName);
432                         if (type == typeof (void))
433                                 throw new ArgumentException ("type",  "Bad field type in defining field.");
434                         if (is_created)
435                                 throw not_after_created ();
436
437                         FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes);
438                         if (fields != null) {
439                                 FieldBuilder[] new_fields = new FieldBuilder [fields.Length+1];
440                                 System.Array.Copy (fields, new_fields, fields.Length);
441                                 new_fields [fields.Length] = res;
442                                 fields = new_fields;
443                         } else {
444                                 fields = new FieldBuilder [1];
445                                 fields [0] = res;
446                                 create_internal_class (this);
447                         }
448                         return res;
449                 }
450
451                 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
452                         check_name ("name", name);
453                         if (parameterTypes != null)
454                                 foreach (Type param in parameterTypes)
455                                         if (param == null)
456                                                 throw new ArgumentNullException ("parameterTypes");
457                         if (is_created)
458                                 throw not_after_created ();
459
460                         PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
461
462                         if (properties != null) {
463                                 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
464                                 System.Array.Copy (properties, new_properties, properties.Length);
465                                 new_properties [properties.Length] = res;
466                                 properties = new_properties;
467                         } else {
468                                 properties = new PropertyBuilder [1];
469                                 properties [0] = res;
470                         }
471                         return res;
472                 }
473
474                 public ConstructorBuilder DefineTypeInitializer() {
475                         return DefineConstructor (MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, null);
476                 }
477
478                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
479                 private extern Type create_runtime_class (TypeBuilder tb);
480                 
481                 public Type CreateType() {
482                         /* handle nesting_type */
483                         if (is_created)
484                                 throw not_after_created ();
485                         if (methods != null) {
486                                 foreach (MethodBuilder method in methods) {
487                                         method.fixup ();
488                                 }
489                         }
490
491                         //
492                         // On classes, define a default constructor if not provided
493                         //
494                         if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>"))
495                                 DefineDefaultConstructor (MethodAttributes.Public);
496
497                         if (ctors != null){
498                                 foreach (ConstructorBuilder ctor in ctors) 
499                                         ctor.fixup ();
500                         }
501                         
502                         created = create_runtime_class (this);
503                         if (created != null)
504                                 return created;
505                         return this;
506                 }
507
508                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
509                 {
510                         if (ctors == null)
511                                 return new ConstructorInfo [0];
512                         ArrayList l = new ArrayList ();
513                         bool match;
514                         MethodAttributes mattrs;
515                         
516                         foreach (ConstructorBuilder c in ctors) {
517                                 match = false;
518                                 mattrs = c.Attributes;
519                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
520                                         if ((bindingAttr & BindingFlags.Public) != 0)
521                                                 match = true;
522                                 } else {
523                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
524                                                 match = true;
525                                 }
526                                 if (!match)
527                                         continue;
528                                 match = false;
529                                 if ((mattrs & MethodAttributes.Static) != 0) {
530                                         if ((bindingAttr & BindingFlags.Static) != 0)
531                                                 match = true;
532                                 } else {
533                                         if ((bindingAttr & BindingFlags.Instance) != 0)
534                                                 match = true;
535                                 }
536                                 if (!match)
537                                         continue;
538                                 l.Add (c);
539                         }
540                         ConstructorInfo[] result = new ConstructorInfo [l.Count];
541                         l.CopyTo (result);
542                         return result;
543                 }
544
545                 public override Type GetElementType () { 
546                         throw not_supported ();
547                 }
548
549                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
550                         throw not_supported ();
551                 }
552
553                 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
554                         // FIXME: Under MS.NET, this throws a NotImplementedException
555                         // But mcs calls this method. How can that be?
556                         return new EventInfo [0];
557                 }
558
559                 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
560                         throw not_supported ();
561                 }
562
563                 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
564                         if (fields == null)
565                                 return new FieldInfo [0];
566                         ArrayList l = new ArrayList ();
567                         bool match;
568                         FieldAttributes mattrs;
569                         
570                         foreach (FieldInfo c in fields) {
571                                 match = false;
572                                 mattrs = c.Attributes;
573                                 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
574                                         if ((bindingAttr & BindingFlags.Public) != 0)
575                                                 match = true;
576                                 } else {
577                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
578                                                 match = true;
579                                 }
580                                 if (!match)
581                                         continue;
582                                 match = false;
583                                 if ((mattrs & FieldAttributes.Static) != 0) {
584                                         if ((bindingAttr & BindingFlags.Static) != 0)
585                                                 match = true;
586                                 } else {
587                                         if ((bindingAttr & BindingFlags.Instance) != 0)
588                                                 match = true;
589                                 }
590                                 if (!match)
591                                         continue;
592                                 l.Add (c);
593                         }
594                         FieldInfo[] result = new FieldInfo [l.Count];
595                         l.CopyTo (result);
596                         return result;
597                 }
598
599                 public override Type GetInterface (string name, bool ignoreCase) {
600                         throw not_supported ();
601                 }
602                 
603                 public override Type[] GetInterfaces () {
604                         if (interfaces != null) {
605                                 Type[] ret = new Type [interfaces.Length];
606                                 interfaces.CopyTo (ret, 0);
607                                 return ret;
608                         } else {
609                                 return Type.EmptyTypes;
610                         }
611                 }
612
613                 public override MemberInfo[] GetMember (string name, MemberTypes type,
614                                                                                                 BindingFlags bindingAttr) {
615                         throw not_supported ();
616                 }
617
618                 public override MemberInfo[] GetMembers (BindingFlags bindingAttr) {
619                         throw not_supported ();
620                 }
621
622                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
623                         if (methods == null)
624                                 return new MethodInfo [0];
625                         ArrayList l = new ArrayList ();
626                         bool match;
627                         MethodAttributes mattrs;
628
629                         foreach (MethodInfo c in methods) {
630                                 match = false;
631                                 mattrs = c.Attributes;
632                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
633                                         if ((bindingAttr & BindingFlags.Public) != 0)
634                                                 match = true;
635                                 } else {
636                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
637                                                 match = true;
638                                 }
639                                 if (!match)
640                                         continue;
641                                 match = false;
642                                 if ((mattrs & MethodAttributes.Static) != 0) {
643                                         if ((bindingAttr & BindingFlags.Static) != 0)
644                                                 match = true;
645                                 } else {
646                                         if ((bindingAttr & BindingFlags.Instance) != 0)
647                                                 match = true;
648                                 }
649                                 if (!match)
650                                         continue;
651                                 l.Add (c);
652                         }
653                         MethodInfo[] result = new MethodInfo [l.Count];
654                         l.CopyTo (result);
655                         return result;
656                 }
657
658                 protected override MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
659                         throw not_supported ();
660                 }
661                 
662                 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
663                         throw not_supported ();
664                 }
665
666                 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
667                         bool match;
668                         ArrayList result = new ArrayList ();
669                 
670                         if (subtypes == null)
671                                 return Type.EmptyTypes;
672                         foreach (TypeBuilder t in subtypes) {
673                                 match = false;
674                                 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
675                                         if ((bindingAttr & BindingFlags.Public) != 0)
676                                                 match = true;
677                                 } else {
678                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
679                                                 match = true;
680                                 }
681                                 if (!match)
682                                         continue;
683                                 result.Add (t);
684                         }
685                         Type[] r = new Type [result.Count];
686                         result.CopyTo (r);
687                         return r;
688                 }
689
690                 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
691                         if (properties == null)
692                                 return new PropertyInfo [0];
693                         ArrayList l = new ArrayList ();
694                         bool match;
695                         MethodAttributes mattrs;
696                         MethodInfo accessor;
697                         
698                         foreach (PropertyInfo c in properties) {
699                                 match = false;
700                                 accessor = c.GetGetMethod (true);
701                                 if (accessor == null)
702                                         accessor = c.GetSetMethod (true);
703                                 if (accessor == null)
704                                         continue;
705                                 mattrs = accessor.Attributes;
706                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
707                                         if ((bindingAttr & BindingFlags.Public) != 0)
708                                                 match = true;
709                                 } else {
710                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
711                                                 match = true;
712                                 }
713                                 if (!match)
714                                         continue;
715                                 match = false;
716                                 if ((mattrs & MethodAttributes.Static) != 0) {
717                                         if ((bindingAttr & BindingFlags.Static) != 0)
718                                                 match = true;
719                                 } else {
720                                         if ((bindingAttr & BindingFlags.Instance) != 0)
721                                                 match = true;
722                                 }
723                                 if (!match)
724                                         continue;
725                                 l.Add (c);
726                         }
727                         PropertyInfo[] result = new PropertyInfo [l.Count];
728                         l.CopyTo (result);
729                         return result;
730                 }
731                 
732                 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
733                         throw not_supported ();
734                 }
735
736                 protected override bool HasElementTypeImpl () {
737                         // According to the MSDN docs, this is supported for TypeBuilders,
738                         // but in reality, it is not
739                         throw not_supported ();
740                         //                      return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
741                 }
742
743                 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
744                         throw not_supported ();
745                 }
746
747                 protected override bool IsArrayImpl ()
748                 {
749                         return Type.IsArrayImpl (this);
750                 }
751
752                 protected override bool IsByRefImpl () {
753                         // FIXME
754                         return false;
755                 }
756                 protected override bool IsCOMObjectImpl () {
757                         return false;
758                 }
759                 protected override bool IsPointerImpl () {
760                         // FIXME
761                         return false;
762                 }
763                 protected override bool IsPrimitiveImpl () {
764                         // FIXME
765                         return false;
766                 }
767                 protected override bool IsValueTypeImpl () {
768                         return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
769                                 this != pmodule.assemblyb.corlib_value_type &&
770                                 this != pmodule.assemblyb.corlib_enum_type);
771                 }
772                 
773                 public override RuntimeTypeHandle TypeHandle { 
774                         get { 
775                                 throw not_supported (); 
776                         } 
777                 }
778
779                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
780                         if (customBuilder == null)
781                                 throw new ArgumentNullException ("customBuilder");
782
783                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
784                         if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
785                                 byte[] data = customBuilder.Data;
786                                 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
787                                 layout_kind = (int)data [2];
788                                 layout_kind |= ((int)data [3]) << 8;
789                                 attrs &= ~TypeAttributes.LayoutMask;
790                                 switch ((LayoutKind)layout_kind) {
791                                 case LayoutKind.Auto:
792                                         attrs |= TypeAttributes.AutoLayout;
793                                         break;
794                                 case LayoutKind.Explicit:
795                                         attrs |= TypeAttributes.ExplicitLayout;
796                                         break;
797                                 case LayoutKind.Sequential:
798                                         attrs |= TypeAttributes.SequentialLayout;
799                                         break;
800                                 default:
801                                         // we should ignore it since it can be any value anyway...
802                                         throw new Exception ("Error in customattr");
803                                 }
804                                 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
805                                 int pos = 6;
806                                 if (first_type_name == "System.Int16")
807                                         pos = 4;
808                                 int nnamed = (int)data [pos++];
809                                 nnamed |= ((int)data [pos++]) << 8;
810                                 for (int i = 0; i < nnamed; ++i) {
811                                         byte named_type = data [pos++];
812                                         byte type = data [pos++];
813                                         int len = CustomAttributeBuilder.decode_len (data, pos, out pos);
814                                         string named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
815                                         pos += len;
816                                         /* all the fields are integers in StructLayout */
817                                         int value = (int)data [pos++];
818                                         value |= ((int)data [pos++]) << 8;
819                                         value |= ((int)data [pos++]) << 16;
820                                         value |= ((int)data [pos++]) << 24;
821                                         switch (named_name) {
822                                         case "CharSet":
823                                                 switch ((CharSet)value) {
824                                                 case CharSet.None:
825                                                 case CharSet.Ansi:
826                                                         break;
827                                                 case CharSet.Unicode:
828                                                         attrs |= TypeAttributes.UnicodeClass;
829                                                         break;
830                                                 case CharSet.Auto:
831                                                         attrs |= TypeAttributes.AutoClass;
832                                                         break;
833                                                 default:
834                                                         break; // error out...
835                                                 }
836                                                 break;
837                                         case "Pack":
838                                                 packing_size = (PackingSize)value;
839                                                 break;
840                                         case "Size":
841                                                 class_size = value;
842                                                 break;
843                                         default:
844                                                 break; // error out...
845                                         }
846                                 }
847                                 return;
848                         } else if (attrname == "System.SerializableAttribute") {
849                                 attrs |= TypeAttributes.Serializable;
850                                 return;
851                         }
852                         if (cattrs != null) {
853                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
854                                 cattrs.CopyTo (new_array, 0);
855                                 new_array [cattrs.Length] = customBuilder;
856                                 cattrs = new_array;
857                         } else {
858                                 cattrs = new CustomAttributeBuilder [1];
859                                 cattrs [0] = customBuilder;
860                         }
861                 }
862                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
863                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
864                 }
865
866                 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
867                         check_name ("name", name);
868                         if (eventtype == null)
869                                 throw new ArgumentNullException ("eventtype");
870                         if (is_created)
871                                 throw not_after_created ();
872
873                         EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
874                         if (events != null) {
875                                 EventBuilder[] new_events = new EventBuilder [events.Length+1];
876                                 System.Array.Copy (events, new_events, events.Length);
877                                 new_events [events.Length] = res;
878                                 events = new_events;
879                         } else {
880                                 events = new EventBuilder [1];
881                                 events [0] = res;
882                         }
883                         return res;
884                 }
885
886                 static int InitializedDataCount = 0;
887                 
888                 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
889                         check_name ("name", name);
890                         if (data == null)
891                                 throw new ArgumentNullException ("data");
892                         if ((data.Length == 0) || (data.Length > 0x3f0000))
893                                 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
894                         if (is_created)
895                                 throw not_after_created ();
896
897                         string s = "$ArrayType$"+InitializedDataCount.ToString();
898                         TypeBuilder datablobtype = DefineNestedType (s,
899                                 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
900                                 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, data.Length);
901                         datablobtype.CreateType ();
902                         FieldBuilder res = DefineField (name, datablobtype, attributes|FieldAttributes.Assembly|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
903                         res.SetRVAData (data);
904                         InitializedDataCount++;
905                         return res;
906                 }
907
908                 [MonoTODO]
909                 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
910                         check_name ("name", name);
911                         if ((size <= 0) || (size > 0x3f0000))
912                                 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
913                         if (is_created)
914                                 throw not_after_created ();
915
916                         throw new NotImplementedException ();
917                 }
918
919                 public TypeToken TypeToken {
920                         get {
921                                 return new TypeToken (0x02000000 | table_idx);
922                         }
923                 }
924                 public void SetParent (Type parentType) {
925                         if (parentType == null)
926                                 throw new ArgumentNullException ("parentType");
927                         if (is_created)
928                                 throw not_after_created ();
929
930                         parent = parentType;
931                 }
932                 internal int get_next_table_index (object obj, int table, bool inc) {
933                         return pmodule.get_next_table_index (obj, table, inc);
934                 }
935
936                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
937                 {
938                         if (created == null)
939                                 throw new NotSupportedException ("This method is not implemented for incomplete types.");
940
941                         return created.GetInterfaceMap (interfaceType);
942                 }
943
944                 internal bool is_created {
945                         get {
946                                 return created != null;
947                         }
948                 }
949
950                 private Exception not_supported ()
951                 {
952                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
953                 }
954
955                 private Exception not_after_created ()
956                 {
957                         return new InvalidOperationException ("Unable to change after type has been created.");
958                 }
959
960                 private void check_name (string argName, string name)
961                 {
962                         if (name == null)
963                                 throw new ArgumentNullException (argName);
964                         if (name == "")
965                                 throw new ArgumentException (argName, "Empty name is not legal.");
966                         if (name.IndexOf ((char)0) != -1)
967                                 throw new ArgumentException (argName, "Illegal name.");
968                 }
969
970 #if GENERICS
971                 public override Type GetGenericTypeDefinition ()
972                 {
973                         setup_generic_class (this);
974
975                         return base.GetGenericTypeDefinition ();
976                 }
977
978                 public override bool HasGenericParameters {
979                         get {
980                                 throw new NotImplementedException ();
981                         }
982                 }
983
984                 public override bool HasUnboundGenericParameters {
985                         get {
986                                 throw new NotImplementedException ();
987                         }
988                 }
989
990                 public extern override bool IsUnboundGenericParameter {
991                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
992                         get;
993                 }
994
995                 public override int GenericParameterPosition {
996                         get {
997                                 throw new Exception ("Unimplemented");
998                         }
999                 }
1000
1001                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1002                 private extern static Type define_generic_parameter (TypeBuilder tb, MonoGenericParam param);
1003                 
1004                 public Type DefineGenericParameter (string name, Type[] constraints)
1005                 {
1006                         MonoGenericParam gparam = new MonoGenericParam (name, constraints);
1007
1008                         if (generic_params != null) {
1009                                 MonoGenericParam[] new_generic_params = new MonoGenericParam [generic_params.Length+1];
1010                                 System.Array.Copy (generic_params, new_generic_params, generic_params.Length);
1011                                 new_generic_params [generic_params.Length] = gparam;
1012                                 generic_params = new_generic_params;
1013                         } else {
1014                                 generic_params = new MonoGenericParam [1];
1015                                 generic_params [0] = gparam;
1016                         }
1017
1018                         return define_generic_parameter (this, gparam);
1019                 }
1020 #endif
1021
1022                 internal sealed class MonoGenericParam {
1023                         private readonly uint Handle;
1024
1025                         public readonly Type Type;
1026                         public readonly string Name;
1027                         public readonly int Flags;
1028                         public readonly Type[] Constraints;
1029
1030                         public MonoGenericParam (string name, Type[] constraints)
1031                         {
1032                                 this.Name = name;
1033                                 this.Constraints = constraints;
1034                         }
1035                 }
1036         }
1037 }