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