Tue Jul 9 19:03:03 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, pmodule.assemblyb.corlib_object_type, null);
169                 }
170
171                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
172                         return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, 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                         return type_is_subtype_of (this, typeof (System.Array), false);
557                 }
558                 protected override bool IsByRefImpl () {
559                         // FIXME
560                         return false;
561                 }
562                 protected override bool IsCOMObjectImpl () {
563                         return false;
564                 }
565                 protected override bool IsPointerImpl () {
566                         // FIXME
567                         return false;
568                 }
569                 protected override bool IsPrimitiveImpl () {
570                         // FIXME
571                         return false;
572                 }
573                 protected override bool IsValueTypeImpl () {
574                         //Console.WriteLine ("is value type: {0}: {1} ({2}) base: {3}", AssemblyQualifiedName, type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false), pmodule.assemblyb.corlib_value_type.AssemblyQualifiedName, BaseType != null?BaseType.AssemblyQualifiedName: "");
575                         return (type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false));
576 //                              this != pmodule.assemblyb.corlib_value_type &&
577 //                              this != typeof (System.Enum); /* FIXME: handle a TypeBuilder System.Enum */
578
579 /*                      return type_is_subtype_of (this, typeof(System.ValueType), false) &&
580                                 this != typeof(System.ValueType) &&
581                                 this != typeof (System.Enum);*/
582                 }
583                 
584                 public override RuntimeTypeHandle TypeHandle { get { return _impl; } }
585
586                 private static int decode_len (byte[] data, int pos, out int rpos) {
587                         int len = 0;
588                         if ((data [pos] & 0x80) == 0) {
589                                 len = (int)(data [pos++] & 0x7f);
590                         } else if ((data [pos] & 0x40) == 0) {
591                                 len = ((data [pos] & 0x3f) << 8) + data [pos + 1];
592                                 pos += 2;
593                         } else {
594                                 len = ((data [pos] & 0x1f) << 24) + (data [pos + 1] << 16) + (data [pos + 2] << 8) + data [pos + 3];
595                                 pos += 4;
596                         }
597                         rpos = pos;
598                         return len;
599                 }
600
601                 private static string string_from_bytes (byte[] data, int pos, int len) {
602                         char[] chars = new char [len];
603                         // FIXME: use a utf8 decoder here
604                         for (int i = 0; i < len; ++i)
605                                 chars [i] = (char)data [pos + i];
606                         return new String (chars);
607                 }
608
609                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
610                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
611                         if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
612                                 byte[] data = customBuilder.Data;
613                                 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
614                                 layout_kind = (int)data [2];
615                                 layout_kind |= ((int)data [3]) << 8;
616                                 attrs &= ~TypeAttributes.LayoutMask;
617                                 switch ((LayoutKind)layout_kind) {
618                                 case LayoutKind.Auto:
619                                         attrs |= TypeAttributes.AutoLayout;
620                                         break;
621                                 case LayoutKind.Explicit:
622                                         attrs |= TypeAttributes.ExplicitLayout;
623                                         break;
624                                 case LayoutKind.Sequential:
625                                         attrs |= TypeAttributes.SequentialLayout;
626                                         break;
627                                 default:
628                                         // we should ignore it since it can be any value anyway...
629                                         throw new Exception ("Error in customattr");
630                                 }
631                                 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
632                                 int pos = 6;
633                                 if (first_type_name == "System.Int16")
634                                         pos = 4;
635                                 int nnamed = (int)data [pos++];
636                                 nnamed |= ((int)data [pos++]) << 8;
637                                 for (int i = 0; i < nnamed; ++i) {
638                                         byte type = data [pos++];
639                                         int len = decode_len (data, pos, out pos);
640                                         string named_name = string_from_bytes (data, pos, len);
641                                         pos += len;
642                                         /* all the fields are integers in StructLayout */
643                                         int value = (int)data [pos++];
644                                         value |= ((int)data [pos++]) << 8;
645                                         value |= ((int)data [pos++]) << 16;
646                                         value |= ((int)data [pos++]) << 24;
647                                         switch (named_name) {
648                                         case "CharSet":
649                                                 switch ((CharSet)value) {
650                                                 case CharSet.None:
651                                                 case CharSet.Ansi:
652                                                         break;
653                                                 case CharSet.Unicode:
654                                                         attrs |= TypeAttributes.UnicodeClass;
655                                                         break;
656                                                 case CharSet.Auto:
657                                                         attrs |= TypeAttributes.AutoClass;
658                                                         break;
659                                                 default:
660                                                         break; // error out...
661                                                 }
662                                                 break;
663                                         case "Pack":
664                                                 packing_size = (PackingSize)value;
665                                                 break;
666                                         case "Size":
667                                                 class_size = value;
668                                                 break;
669                                         default:
670                                                 break; // error out...
671                                         }
672                                 }
673                                 return;
674                         } else if (attrname == "System.SerializableAttribute") {
675                                 attrs |= TypeAttributes.Serializable;
676                                 return;
677                         }
678                         if (cattrs != null) {
679                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
680                                 cattrs.CopyTo (new_array, 0);
681                                 new_array [cattrs.Length] = customBuilder;
682                                 cattrs = new_array;
683                         } else {
684                                 cattrs = new CustomAttributeBuilder [1];
685                                 cattrs [0] = customBuilder;
686                         }
687                 }
688                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
689                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
690                 }
691
692                 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
693                         EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
694                         if (events != null) {
695                                 EventBuilder[] new_events = new EventBuilder [events.Length+1];
696                                 System.Array.Copy (events, new_events, events.Length);
697                                 new_events [events.Length] = res;
698                                 events = new_events;
699                         } else {
700                                 events = new EventBuilder [1];
701                                 events [0] = res;
702                         }
703                         return res;
704                 }
705
706                 static int InitializedDataCount = 0;
707                 
708                 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
709                         TypeBuilder datablobtype = pmodule.DefineType ("$ArrayType$"+InitializedDataCount.ToString(),
710                                 TypeAttributes.Public|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
711                                 pmodule.assemblyb.corlib_value_type, PackingSize.Size1, data.Length);
712                         datablobtype.packing_size = PackingSize.Size1;
713                         datablobtype.class_size = data.Length;
714                         datablobtype.CreateType ();
715                         FieldBuilder res = DefineField (name, datablobtype, attributes|FieldAttributes.Assembly|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
716                         res.SetRVAData (data);
717                         InitializedDataCount++;
718                         return res;
719                 }
720
721                 [MonoTODO]
722                 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
723                         throw new NotImplementedException ();
724                 }
725
726                 public TypeToken TypeToken {
727                         get {
728                                 return new TypeToken (0x02000000 | table_idx);
729                         }
730                 }
731                 public void SetParent (Type parentType) {
732                         parent = parentType;
733                 }
734                 internal int get_next_table_index (object obj, int table, bool inc) {
735                         return pmodule.get_next_table_index (obj, table, inc);
736                 }
737
738         }
739 }