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