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