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