Mon Jan 13 11:37:14 CET 2003 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / TypeBuilder.cs
1 //
2 // System.Reflection.Emit/TypeBuilder.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.  http://www.ximian.com
8 //
9
10 using System;
11 using System.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.ToString();
95                         }
96                 }
97                 public override Type BaseType {
98                         get {
99                                 return parent;
100                         }
101                 }
102                 public override Type DeclaringType {get {return nesting_type;}}
103                 public override Type UnderlyingSystemType {
104                         get {
105                                 if (fields != null) {
106                                         foreach (FieldBuilder f in fields) {
107                                                 if ((f.Attributes & FieldAttributes.Static) == 0)
108                                                         return f.FieldType;
109                                         }
110                                 }
111                                 throw new InvalidOperationException (String.Format ("typebuilder: {0}", this));
112                         }
113                 }
114
115                 string GetFullName () {
116                         if (nesting_type != null)
117                                 return String.Concat (nesting_type.FullName, "+", tname);
118                         if ((nspace != null) && (nspace.Length > 0))
119                                 return String.Concat (nspace, ".", tname);
120                         return tname;
121                 }
122         
123                 public override string FullName {
124                         get {
125                                 return fullname;
126                         }
127                 }
128         
129                 public override Guid GUID {
130                         get {return Guid.Empty;}
131                 }
132
133                 public override Module Module {
134                         get {return pmodule;}
135                 }
136                 public override string Name {
137                         get {return tname;}
138                 }
139                 public override string Namespace {
140                         get {return nspace;}
141                 }
142                 public PackingSize PackingSize {
143                         get {return packing_size;}
144                 }
145                 public override Type ReflectedType {get {return nesting_type;}}
146                 public override MemberTypes MemberType { 
147                         get {return MemberTypes.TypeInfo;}
148                 }
149
150                 [MonoTODO]
151                 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
152                         throw new NotImplementedException ();
153                 }
154
155                 public void AddInterfaceImplementation( Type interfaceType) {
156                         if (interfaces != null) {
157                                 Type[] ifnew = new Type [interfaces.Length + 1];
158                                 interfaces.CopyTo (ifnew, 0);
159                                 ifnew [interfaces.Length] = interfaceType;
160                                 interfaces = ifnew;
161                         } else {
162                                 interfaces = new Type [1];
163                                 interfaces [0] = interfaceType;
164                         }
165                 }
166
167                 [MonoTODO]
168                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
169                         throw new NotImplementedException ();
170                 }
171
172                 public override bool IsDefined( Type attributeType, bool inherit) {
173                         return false;
174                 }
175                 public override object[] GetCustomAttributes(bool inherit) {
176                         return null;
177                 }
178                 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
179                         return null;
180                 }
181
182                 [MonoTODO]
183                 public TypeBuilder DefineNestedType (string name) {
184                         // FIXME: LAMESPEC: what other attributes should we use here as default?
185                         return DefineNestedType (name, TypeAttributes.Public, pmodule.assemblyb.corlib_object_type, null);
186                 }
187
188                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
189                         return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
190                 }
191
192                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
193                         return DefineNestedType (name, attr, parent, null);
194                 }
195
196                 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packsize, int typesize) {
197                         TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize);
198                         res.nesting_type = this;
199                         res.fullname = res.GetFullName ();
200                         pmodule.RegisterTypeName (res, res.fullname);
201                         if (subtypes != null) {
202                                 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
203                                 System.Array.Copy (subtypes, new_types, subtypes.Length);
204                                 new_types [subtypes.Length] = res;
205                                 subtypes = new_types;
206                         } else {
207                                 subtypes = new TypeBuilder [1];
208                                 subtypes [0] = res;
209                         }
210                         return res;
211                 }
212
213                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
214                         return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
215                 }
216
217                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
218                         return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
219                 }
220
221                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
222                         return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
223                 }
224
225                 public ConstructorBuilder DefineConstructor( MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
226                         ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes);
227                         if (ctors != null) {
228                                 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
229                                 System.Array.Copy (ctors, new_ctors, ctors.Length);
230                                 new_ctors [ctors.Length] = cb;
231                                 ctors = new_ctors;
232                         } else {
233                                 ctors = new ConstructorBuilder [1];
234                                 ctors [0] = cb;
235                         }
236                         return cb;
237                 }
238
239                 public ConstructorBuilder DefineDefaultConstructor( MethodAttributes attributes) {
240                         return DefineConstructor (attributes, CallingConventions.Standard, null);
241                 }
242
243                 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
244                         return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
245                 }
246
247                 private void append_method (MethodBuilder mb) {
248                         if (methods != null) {
249                                 MethodBuilder[] new_methods = new MethodBuilder [methods.Length+1];
250                                 System.Array.Copy (methods, new_methods, methods.Length);
251                                 new_methods [methods.Length] = mb;
252                                 methods = new_methods;
253                         } else {
254                                 methods = new MethodBuilder [1];
255                                 methods [0] = mb;
256                         }
257                 }
258
259                 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
260                         MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes);
261                         append_method (res);
262                         return res;
263                 }
264
265                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
266                         MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes,
267                                 dllName, entryName, nativeCallConv, nativeCharSet);
268                         append_method (res);
269                         return res;
270                 }
271
272                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
273                         return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
274                                 nativeCallConv, nativeCharSet);
275                 }
276
277                 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
278                         if (methodInfoBody is MethodBuilder) {
279                                 MethodBuilder mb = (MethodBuilder)methodInfoBody;
280                                 mb.set_override (methodInfoDeclaration);
281                         }
282                 }
283
284                 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
285                         FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes);
286                         if (fields != null) {
287                                 FieldBuilder[] new_fields = new FieldBuilder [fields.Length+1];
288                                 System.Array.Copy (fields, new_fields, fields.Length);
289                                 new_fields [fields.Length] = res;
290                                 fields = new_fields;
291                         } else {
292                                 fields = new FieldBuilder [1];
293                                 fields [0] = res;
294                                 create_internal_class (this);
295                         }
296                         return res;
297                 }
298
299                 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
300                         PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
301
302                         if (properties != null) {
303                                 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
304                                 System.Array.Copy (properties, new_properties, properties.Length);
305                                 new_properties [properties.Length] = res;
306                                 properties = new_properties;
307                         } else {
308                                 properties = new PropertyBuilder [1];
309                                 properties [0] = res;
310                         }
311                         return res;
312                 }
313
314                 [MonoTODO]
315                 public ConstructorBuilder DefineTypeInitializer() {
316                         throw new NotImplementedException ();
317                 }
318
319                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
320                 private extern Type create_runtime_class (TypeBuilder tb);
321                 
322                 public Type CreateType() {
323                         /* handle nesting_type */
324                         if (created != null) {
325                                 string err="type already created: " + created.ToString();
326                                 throw new InvalidOperationException (err);
327                         }
328                         if (methods != null) {
329                                 foreach (MethodBuilder method in methods) {
330                                         method.fixup ();
331                                 }
332                         }
333                         if (ctors != null) {
334                                 foreach (ConstructorBuilder ctor in ctors) {
335                                         ctor.fixup ();
336                                 }
337                         }
338                         created = create_runtime_class (this);
339                         if (created != null)
340                                 return created;
341                         return this;
342                 }
343
344                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr) {
345                         if (ctors == null)
346                                 return new ConstructorInfo [0];
347                         ArrayList l = new ArrayList ();
348                         bool match;
349                         MethodAttributes mattrs;
350                         
351                         foreach (ConstructorBuilder c in ctors) {
352                                 match = false;
353                                 mattrs = c.Attributes;
354                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
355                                         if ((bindingAttr & BindingFlags.Public) != 0)
356                                                 match = true;
357                                 } else {
358                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
359                                                 match = true;
360                                 }
361                                 if (!match)
362                                         continue;
363                                 match = false;
364                                 if ((mattrs & MethodAttributes.Static) != 0) {
365                                         if ((bindingAttr & BindingFlags.Static) != 0)
366                                                 match = true;
367                                 } else {
368                                         if ((bindingAttr & BindingFlags.Instance) != 0)
369                                                 match = true;
370                                 }
371                                 if (!match)
372                                         continue;
373                                 l.Add (c);
374                         }
375                         ConstructorInfo[] result = new ConstructorInfo [l.Count];
376                         l.CopyTo (result);
377                         return result;
378                 }
379
380                 public override Type GetElementType () { return null; }
381
382                 [MonoTODO]
383                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
384                         throw new NotImplementedException ();
385                 }
386
387                 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
388                         return new EventInfo [0];
389                 }
390
391                 [MonoTODO]
392                 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
393                         //FIXME
394                         throw new NotImplementedException ();
395                 }
396
397                 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
398                         if (fields == null)
399                                 return new FieldInfo [0];
400                         ArrayList l = new ArrayList ();
401                         bool match;
402                         FieldAttributes mattrs;
403                         
404                         foreach (FieldInfo c in fields) {
405                                 match = false;
406                                 mattrs = c.Attributes;
407                                 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
408                                         if ((bindingAttr & BindingFlags.Public) != 0)
409                                                 match = true;
410                                 } else {
411                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
412                                                 match = true;
413                                 }
414                                 if (!match)
415                                         continue;
416                                 match = false;
417                                 if ((mattrs & FieldAttributes.Static) != 0) {
418                                         if ((bindingAttr & BindingFlags.Static) != 0)
419                                                 match = true;
420                                 } else {
421                                         if ((bindingAttr & BindingFlags.Instance) != 0)
422                                                 match = true;
423                                 }
424                                 if (!match)
425                                         continue;
426                                 l.Add (c);
427                         }
428                         FieldInfo[] result = new FieldInfo [l.Count];
429                         l.CopyTo (result);
430                         return result;
431                 }
432
433                 [MonoTODO]
434                 public override Type GetInterface (string name, bool ignoreCase) {
435                         throw new NotImplementedException ();
436                 }
437                 
438                 public override Type[] GetInterfaces () {
439                         if (interfaces != null) {
440                                 Type[] ret = new Type [interfaces.Length];
441                                 interfaces.CopyTo (ret, 0);
442                                 return ret;
443                         } else {
444                                 return Type.EmptyTypes;
445                         }
446                 }
447
448                 [MonoTODO]
449                 public override MemberInfo[] GetMembers( BindingFlags bindingAttr) {
450                         // FIXME
451                         throw new NotImplementedException ();
452                 }
453
454                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
455                         if (methods == null)
456                                 return new MethodInfo [0];
457                         ArrayList l = new ArrayList ();
458                         bool match;
459                         MethodAttributes mattrs;
460
461                         foreach (MethodInfo c in methods) {
462                                 match = false;
463                                 mattrs = c.Attributes;
464                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
465                                         if ((bindingAttr & BindingFlags.Public) != 0)
466                                                 match = true;
467                                 } else {
468                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
469                                                 match = true;
470                                 }
471                                 if (!match)
472                                         continue;
473                                 match = false;
474                                 if ((mattrs & MethodAttributes.Static) != 0) {
475                                         if ((bindingAttr & BindingFlags.Static) != 0)
476                                                 match = true;
477                                 } else {
478                                         if ((bindingAttr & BindingFlags.Instance) != 0)
479                                                 match = true;
480                                 }
481                                 if (!match)
482                                         continue;
483                                 l.Add (c);
484                         }
485                         MethodInfo[] result = new MethodInfo [l.Count];
486                         l.CopyTo (result);
487                         return result;
488                 }
489
490                 [MonoTODO]
491                 protected override MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
492                         // FIXME
493                         throw new NotImplementedException ();
494                 }
495                 
496                 [MonoTODO]
497                 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
498                         // FIXME
499                         throw new NotImplementedException ();
500                 }
501
502                 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
503                         bool match;
504                         ArrayList result = new ArrayList ();
505                 
506                         if (subtypes == null)
507                                 return Type.EmptyTypes;
508                         foreach (TypeBuilder t in subtypes) {
509                                 match = false;
510                                 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
511                                         if ((bindingAttr & BindingFlags.Public) != 0)
512                                                 match = true;
513                                 } else {
514                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
515                                                 match = true;
516                                 }
517                                 if (!match)
518                                         continue;
519                                 result.Add (t);
520                         }
521                         Type[] r = new Type [result.Count];
522                         result.CopyTo (r);
523                         return r;
524                 }
525
526                 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
527                         if (properties == null)
528                                 return new PropertyInfo [0];
529                         ArrayList l = new ArrayList ();
530                         bool match;
531                         MethodAttributes mattrs;
532                         MethodInfo accessor;
533                         
534                         foreach (PropertyInfo c in properties) {
535                                 match = false;
536                                 accessor = c.GetGetMethod (true);
537                                 if (accessor == null)
538                                         accessor = c.GetSetMethod (true);
539                                 if (accessor == null)
540                                         continue;
541                                 mattrs = accessor.Attributes;
542                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
543                                         if ((bindingAttr & BindingFlags.Public) != 0)
544                                                 match = true;
545                                 } else {
546                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
547                                                 match = true;
548                                 }
549                                 if (!match)
550                                         continue;
551                                 match = false;
552                                 if ((mattrs & MethodAttributes.Static) != 0) {
553                                         if ((bindingAttr & BindingFlags.Static) != 0)
554                                                 match = true;
555                                 } else {
556                                         if ((bindingAttr & BindingFlags.Instance) != 0)
557                                                 match = true;
558                                 }
559                                 if (!match)
560                                         continue;
561                                 l.Add (c);
562                         }
563                         PropertyInfo[] result = new PropertyInfo [l.Count];
564                         l.CopyTo (result);
565                         return result;
566                 }
567                 
568                 [MonoTODO]
569                 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
570                         // FIXME
571                         throw new NotImplementedException ();
572                 }
573
574                 protected override bool HasElementTypeImpl () {
575                         return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
576                 }
577
578                 [MonoTODO]
579                 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
580                         // FIXME
581                         throw new NotImplementedException ();
582                 }
583
584                 protected override bool IsArrayImpl () {
585                         return type_is_subtype_of (this, typeof (System.Array), false);
586                 }
587                 protected override bool IsByRefImpl () {
588                         // FIXME
589                         return false;
590                 }
591                 protected override bool IsCOMObjectImpl () {
592                         return false;
593                 }
594                 protected override bool IsPointerImpl () {
595                         // FIXME
596                         return false;
597                 }
598                 protected override bool IsPrimitiveImpl () {
599                         // FIXME
600                         return false;
601                 }
602                 protected override bool IsValueTypeImpl () {
603                         return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
604                                 this != pmodule.assemblyb.corlib_value_type &&
605                                 this != pmodule.assemblyb.corlib_enum_type);
606                 }
607                 
608                 public override RuntimeTypeHandle TypeHandle { get { return _impl; } }
609
610                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
611                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
612                         if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
613                                 byte[] data = customBuilder.Data;
614                                 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
615                                 layout_kind = (int)data [2];
616                                 layout_kind |= ((int)data [3]) << 8;
617                                 attrs &= ~TypeAttributes.LayoutMask;
618                                 switch ((LayoutKind)layout_kind) {
619                                 case LayoutKind.Auto:
620                                         attrs |= TypeAttributes.AutoLayout;
621                                         break;
622                                 case LayoutKind.Explicit:
623                                         attrs |= TypeAttributes.ExplicitLayout;
624                                         break;
625                                 case LayoutKind.Sequential:
626                                         attrs |= TypeAttributes.SequentialLayout;
627                                         break;
628                                 default:
629                                         // we should ignore it since it can be any value anyway...
630                                         throw new Exception ("Error in customattr");
631                                 }
632                                 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
633                                 int pos = 6;
634                                 if (first_type_name == "System.Int16")
635                                         pos = 4;
636                                 int nnamed = (int)data [pos++];
637                                 nnamed |= ((int)data [pos++]) << 8;
638                                 for (int i = 0; i < nnamed; ++i) {
639                                         byte named_type = data [pos++];
640                                         byte type = data [pos++];
641                                         int len = CustomAttributeBuilder.decode_len (data, pos, out pos);
642                                         string named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
643                                         pos += len;
644                                         /* all the fields are integers in StructLayout */
645                                         int value = (int)data [pos++];
646                                         value |= ((int)data [pos++]) << 8;
647                                         value |= ((int)data [pos++]) << 16;
648                                         value |= ((int)data [pos++]) << 24;
649                                         switch (named_name) {
650                                         case "CharSet":
651                                                 switch ((CharSet)value) {
652                                                 case CharSet.None:
653                                                 case CharSet.Ansi:
654                                                         break;
655                                                 case CharSet.Unicode:
656                                                         attrs |= TypeAttributes.UnicodeClass;
657                                                         break;
658                                                 case CharSet.Auto:
659                                                         attrs |= TypeAttributes.AutoClass;
660                                                         break;
661                                                 default:
662                                                         break; // error out...
663                                                 }
664                                                 break;
665                                         case "Pack":
666                                                 packing_size = (PackingSize)value;
667                                                 break;
668                                         case "Size":
669                                                 class_size = value;
670                                                 break;
671                                         default:
672                                                 break; // error out...
673                                         }
674                                 }
675                                 return;
676                         } else if (attrname == "System.SerializableAttribute") {
677                                 attrs |= TypeAttributes.Serializable;
678                                 return;
679                         }
680                         if (cattrs != null) {
681                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
682                                 cattrs.CopyTo (new_array, 0);
683                                 new_array [cattrs.Length] = customBuilder;
684                                 cattrs = new_array;
685                         } else {
686                                 cattrs = new CustomAttributeBuilder [1];
687                                 cattrs [0] = customBuilder;
688                         }
689                 }
690                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
691                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
692                 }
693
694                 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
695                         EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
696                         if (events != null) {
697                                 EventBuilder[] new_events = new EventBuilder [events.Length+1];
698                                 System.Array.Copy (events, new_events, events.Length);
699                                 new_events [events.Length] = res;
700                                 events = new_events;
701                         } else {
702                                 events = new EventBuilder [1];
703                                 events [0] = res;
704                         }
705                         return res;
706                 }
707
708                 static int InitializedDataCount = 0;
709                 
710                 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
711                         TypeBuilder datablobtype = DefineNestedType ("$ArrayType$"+InitializedDataCount.ToString(),
712                                 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
713                                 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, data.Length);
714                         datablobtype.packing_size = PackingSize.Size1;
715                         datablobtype.class_size = data.Length;
716                         datablobtype.CreateType ();
717                         FieldBuilder res = DefineField (name, datablobtype, attributes|FieldAttributes.Assembly|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
718                         res.SetRVAData (data);
719                         InitializedDataCount++;
720                         return res;
721                 }
722
723                 [MonoTODO]
724                 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
725                         throw new NotImplementedException ();
726                 }
727
728                 public TypeToken TypeToken {
729                         get {
730                                 return new TypeToken (0x02000000 | table_idx);
731                         }
732                 }
733                 public void SetParent (Type parentType) {
734                         parent = parentType;
735                 }
736                 internal int get_next_table_index (object obj, int table, bool inc) {
737                         return pmodule.get_next_table_index (obj, table, inc);
738                 }
739
740                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
741                 {
742                         if (created == null)
743                                 throw new NotSupportedException ("This method is not implemented for incomplete types.");
744
745                         return created.GetInterfaceMap (interfaceType);
746                 }
747         }
748 }