This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / System.Reflection.Emit / TypeBuilder.cs
index e82ce82bd00f32bb17ae174a52528e255df9ecfc..edcf429d9cc55167a63c75ed876ab94c314b6d44 100644 (file)
@@ -7,7 +7,31 @@
 // (C) 2001 Ximian, Inc.  http://www.ximian.com
 //
 
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
+using System.Text;
 using System.Reflection;
 using System.Reflection.Emit;
 using System.Runtime.CompilerServices;
@@ -20,23 +44,33 @@ using System.Security.Permissions;
 namespace System.Reflection.Emit {
 
        public sealed class TypeBuilder : Type {
+       #region Sync with reflection.h
        private string tname;
        private string nspace;
        private Type parent;
        private Type nesting_type;
        private Type[] interfaces;
+       private int num_methods;
        private MethodBuilder[] methods;
        private ConstructorBuilder[] ctors;
        private PropertyBuilder[] properties;
+       private int num_fields;
        private FieldBuilder[] fields;
        private EventBuilder[] events;
        private CustomAttributeBuilder[] cattrs;
        internal TypeBuilder[] subtypes;
-       private TypeAttributes attrs;
+       internal TypeAttributes attrs;
        private int table_idx;
        private ModuleBuilder pmodule;
        private int class_size;
        private PackingSize packing_size;
+#if NET_2_0 || BOOTSTRAP_NET_2_0
+       private GenericTypeParameterBuilder[] generic_params;
+#else
+        private Object generic_params; /* so offsets don't change */
+#endif
+       private RefEmitPermissionSet[] permissions;     
+       #endregion
        private Type created;
        string fullname;
 
@@ -52,11 +86,18 @@ namespace System.Reflection.Emit {
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private extern void create_internal_class (TypeBuilder tb);
                
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               private extern void setup_generic_class (TypeBuilder tb);
+
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               private extern EventInfo get_event_info (EventBuilder eb);
+
                internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr) {
                        this.parent = null;
                        this.attrs = attr;
-                       this.class_size = -1;
+                       this.class_size = 0;
                        fullname = this.tname = "<Module>";
+                       this.table_idx = 1;
                        this.nspace = "";
                        pmodule = mb;
                        setup_internal_class (this);
@@ -127,7 +168,7 @@ namespace System.Reflection.Emit {
                                // that breaks mcs.
                                if (fields != null) {
                                        foreach (FieldBuilder f in fields) {
-                                               if ((f.Attributes & FieldAttributes.Static) == 0)
+                                               if ((f != null) && (f.Attributes & FieldAttributes.Static) == 0)
                                                        return f.FieldType;
                                        }
                                }
@@ -171,13 +212,33 @@ namespace System.Reflection.Emit {
                        get { return class_size; }
                }
                public override Type ReflectedType {get {return nesting_type;}}
-               public override MemberTypes MemberType { 
-                       get {return MemberTypes.TypeInfo;}
-               }
 
-               [MonoTODO]
                public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
-                       throw new NotImplementedException ();
+                       if (pset == null)
+                               throw new ArgumentNullException ("pset");
+                       if ((action == SecurityAction.RequestMinimum) ||
+                               (action == SecurityAction.RequestOptional) ||
+                               (action == SecurityAction.RequestRefuse))
+                               throw new ArgumentException ("Request* values are not permitted", "action");
+
+                       if (is_created)
+                               throw not_after_created ();
+
+                       if (permissions != null) {
+                               /* Check duplicate actions */
+                               foreach (RefEmitPermissionSet set in permissions)
+                                       if (set.action == action)
+                                               throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
+
+                               RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
+                               permissions.CopyTo (new_array, 0);
+                               permissions = new_array;
+                       }
+                       else
+                               permissions = new RefEmitPermissionSet [1];
+
+                       permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
+                       attrs |= TypeAttributes.HasSecurity;
                }
 
                public void AddInterfaceImplementation( Type interfaceType) {
@@ -245,7 +306,11 @@ namespace System.Reflection.Emit {
 
                public override bool IsDefined( Type attributeType, bool inherit)
                {
-                       throw not_supported ();
+                       /*
+                        * MS throws NotSupported here, but we can't because some corlib
+                        * classes make calls to IsDefined.
+                        */
+                       return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
                }
                
                public override object[] GetCustomAttributes(bool inherit)
@@ -312,11 +377,20 @@ namespace System.Reflection.Emit {
                        return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
                }
 
-               public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes)
+               public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
+                       return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
+               }
+
+#if NET_2_0 || BOOTSTRAP_NET_2_0
+               public
+#else
+               internal
+#endif
+               ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
                {
                        if (is_created)
                                throw not_after_created ();
-                       ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes);
+                       ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
                        if (ctors != null) {
                                ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
                                System.Array.Copy (ctors, new_ctors, ctors.Length);
@@ -331,11 +405,19 @@ namespace System.Reflection.Emit {
 
                public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
                {
-                       ConstructorBuilder cb = DefineConstructor (attributes, CallingConventions.Standard, null);
+                       ConstructorBuilder cb = DefineConstructor (attributes, CallingConventions.Standard, new Type [0]);
+
+                       Type parent_type;
+
+                       if (parent != null)
+                               parent_type = parent;
+                       else
+                               parent_type = pmodule.assemblyb.corlib_object_type;
 
-                       ConstructorInfo parent_constructor = parent.GetConstructor (
-                               BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
-                               null, Type.EmptyTypes, null);
+                       ConstructorInfo parent_constructor =
+                               parent_type.GetConstructor (
+                                       BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
+                                       null, Type.EmptyTypes, null);
 
                        ILGenerator ig = cb.GetILGenerator ();
                        if (parent_constructor != null){
@@ -352,17 +434,28 @@ namespace System.Reflection.Emit {
 
                private void append_method (MethodBuilder mb) {
                        if (methods != null) {
-                               MethodBuilder[] new_methods = new MethodBuilder [methods.Length+1];
-                               System.Array.Copy (methods, new_methods, methods.Length);
-                               new_methods [methods.Length] = mb;
-                               methods = new_methods;
+                               if (methods.Length == num_methods) {
+                                       MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
+                                       System.Array.Copy (methods, new_methods, num_methods);
+                                       methods = new_methods;
+                               }
                        } else {
                                methods = new MethodBuilder [1];
-                               methods [0] = mb;
                        }
+                       methods [num_methods] = mb;
+                       num_methods ++;
                }
 
                public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
+                       return DefineMethod (name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
+               }
+
+#if NET_2_0 || BOOTSTRAP_NET_2_0
+               public
+#else
+               internal
+#endif
+               MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) {
                        check_name ("name", name);
                        if (is_created)
                                throw not_after_created ();
@@ -373,12 +466,33 @@ namespace System.Reflection.Emit {
 
                        if (returnType == null)
                                returnType = pmodule.assemblyb.corlib_void_type;
-                       MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes);
+                       MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
                        append_method (res);
                        return res;
                }
 
                public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
+                       return DefinePInvokeMethod (name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
+               }
+
+#if NET_2_0 || BOOTSTRAP_NET_2_0
+               public
+#else
+               internal
+#endif
+               MethodBuilder DefinePInvokeMethod (
+                                               string name, 
+                                               string dllName, 
+                                               string entryName, MethodAttributes attributes, 
+                                               CallingConventions callingConvention, 
+                                               Type returnType, 
+                                               Type[] returnTypeRequiredCustomModifiers, 
+                                               Type[] returnTypeOptionalCustomModifiers, 
+                                               Type[] parameterTypes, 
+                                               Type[][] parameterTypeRequiredCustomModifiers, 
+                                               Type[][] parameterTypeOptionalCustomModifiers, 
+                                               CallingConvention nativeCallConv, 
+                                               CharSet nativeCharSet) {
                        check_name ("name", name);
                        check_name ("dllName", dllName);
                        check_name ("entryName", entryName);
@@ -389,8 +503,22 @@ namespace System.Reflection.Emit {
                        if (is_created)
                                throw not_after_created ();
 
-                       MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes,
-                               dllName, entryName, nativeCallConv, nativeCharSet);
+                       MethodBuilder res 
+                               = new MethodBuilder (
+                                               this, 
+                                               name, 
+                                               attributes, 
+                                               callingConvention,
+                                               returnType, 
+                                               returnTypeRequiredCustomModifiers, 
+                                               returnTypeOptionalCustomModifiers, 
+                                               parameterTypes, 
+                                               parameterTypeRequiredCustomModifiers, 
+                                               parameterTypeOptionalCustomModifiers,
+                                               dllName, 
+                                               entryName, 
+                                               nativeCallConv, 
+                                               nativeCharSet);
                        append_method (res);
                        return res;
                }
@@ -415,21 +543,34 @@ namespace System.Reflection.Emit {
                }
 
                public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
+                       return DefineField (fieldName, type, null, null, attributes);
+               }
+
+#if NET_2_0 || BOOTSTRAP_NET_2_0
+               public
+#else
+               internal
+#endif
+           FieldBuilder DefineField( string fieldName, Type type, Type[] requiredCustomAttributes, Type[] optionalCustomAttributes, FieldAttributes attributes) {
                        check_name ("fieldName", fieldName);
                        if (type == typeof (void))
                                throw new ArgumentException ("type",  "Bad field type in defining field.");
                        if (is_created)
                                throw not_after_created ();
 
-                       FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes);
+                       FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomAttributes, optionalCustomAttributes);
                        if (fields != null) {
-                               FieldBuilder[] new_fields = new FieldBuilder [fields.Length+1];
-                               System.Array.Copy (fields, new_fields, fields.Length);
-                               new_fields [fields.Length] = res;
-                               fields = new_fields;
+                               if (fields.Length == num_fields) {
+                                       FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
+                                       System.Array.Copy (fields, new_fields, num_fields);
+                                       fields = new_fields;
+                               }
+                               fields [num_fields] = res;
+                               num_fields ++;
                        } else {
                                fields = new FieldBuilder [1];
                                fields [0] = res;
+                               num_fields ++;
                                create_internal_class (this);
                        }
                        return res;
@@ -464,17 +605,49 @@ namespace System.Reflection.Emit {
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private extern Type create_runtime_class (TypeBuilder tb);
+
+               private bool is_nested_in (Type t) {
+                       while (t != null) {
+                               if (t == this)
+                                       return true;
+                               else
+                                       t = t.DeclaringType;
+                       }
+                       return false;
+               }
                
                public Type CreateType() {
                        /* handle nesting_type */
                        if (is_created)
                                throw not_after_created ();
-                       if (methods != null) {
-                               foreach (MethodBuilder method in methods) {
-                                       method.fixup ();
+
+                       // Fire TypeResolve events for fields whose type is an unfinished
+                       // value type.
+                       if (fields != null) {
+                               foreach (FieldBuilder fb in fields) {
+                                       if (fb == null)
+                                               continue;
+                                       Type ft = fb.FieldType;
+                                       if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
+                                               TypeBuilder tb = (TypeBuilder)ft;
+                                               if (!tb.is_created) {
+                                                       AppDomain.CurrentDomain.DoTypeResolve (tb);
+                                                       if (!tb.is_created) {
+                                                               // FIXME: We should throw an exception here,
+                                                               // but mcs expects that the type is created
+                                                               // even if the exception is thrown
+                                                               //throw new TypeLoadException ("Could not load type " + tb);
+                                                       }
+                                               }
+                                       }
                                }
                        }
 
+                       if (methods != null) {
+                               for (int i = 0; i < num_methods; ++i)
+                                       ((MethodBuilder)(methods[i])).fixup ();
+                       }
+
                        //
                        // On classes, define a default constructor if not provided
                        //
@@ -485,7 +658,7 @@ namespace System.Reflection.Emit {
                                foreach (ConstructorBuilder ctor in ctors) 
                                        ctor.fixup ();
                        }
-                       
+
                        created = create_runtime_class (this);
                        if (created != null)
                                return created;
@@ -537,14 +710,101 @@ namespace System.Reflection.Emit {
                        throw not_supported ();
                }
 
+               /* Needed to keep signature compatibility with MS.NET */
+               public override EventInfo[] GetEvents ()
+               {
+                       return GetEvents (DefaultBindingFlags);
+               }
+
                public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
                        // FIXME: Under MS.NET, this throws a NotImplementedException
                        // But mcs calls this method. How can that be?
                        return new EventInfo [0];
                }
 
+               // This is only used from MonoGenericInst.initialize().
+               internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
+               {
+                       if (events == null)
+                               return new EventInfo [0];
+                       ArrayList l = new ArrayList ();
+                       bool match;
+                       MethodAttributes mattrs;
+                       MethodInfo accessor;
+
+                       foreach (EventBuilder eb in events) {
+                               if (eb == null)
+                                       continue;
+                               EventInfo c = get_event_info (eb);
+                               match = false;
+                               accessor = c.GetAddMethod (true);
+                               if (accessor == null)
+                                       accessor = c.GetRemoveMethod (true);
+                               if (accessor == null)
+                                       continue;
+                               mattrs = accessor.Attributes;
+                               if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
+                                       if ((bindingAttr & BindingFlags.Public) != 0)
+                                               match = true;
+                               } else {
+                                       if ((bindingAttr & BindingFlags.NonPublic) != 0)
+                                               match = true;
+                               }
+                               if (!match)
+                                       continue;
+                               match = false;
+                               if ((mattrs & MethodAttributes.Static) != 0) {
+                                       if ((bindingAttr & BindingFlags.Static) != 0)
+                                               match = true;
+                               } else {
+                                       if ((bindingAttr & BindingFlags.Instance) != 0)
+                                               match = true;
+                               }
+                               if (!match)
+                                       continue;
+                               l.Add (c);
+                       }
+                       EventInfo[] result = new EventInfo [l.Count];
+                       l.CopyTo (result);
+                       return result;
+               }
+
                public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
-                       throw not_supported ();
+                       if (fields == null)
+                               return null;
+
+                       bool match;
+                       FieldAttributes mattrs;
+                       
+                       foreach (FieldInfo c in fields) {
+                               if (c == null)
+                                       continue;
+                               if (c.Name != name)
+                                       continue;
+                               match = false;
+                               mattrs = c.Attributes;
+                               if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
+                                       if ((bindingAttr & BindingFlags.Public) != 0)
+                                               match = true;
+                               } else {
+                                       if ((bindingAttr & BindingFlags.NonPublic) != 0)
+                                               match = true;
+                               }
+                               if (!match)
+                                       continue;
+                               match = false;
+                               if ((mattrs & FieldAttributes.Static) != 0) {
+                                       if ((bindingAttr & BindingFlags.Static) != 0)
+                                               match = true;
+                               } else {
+                                       if ((bindingAttr & BindingFlags.Instance) != 0)
+                                               match = true;
+                               }
+                               if (!match)
+                                       continue;
+                               return c;
+                       }
+                       return null;
                }
 
                public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
@@ -555,6 +815,8 @@ namespace System.Reflection.Emit {
                        FieldAttributes mattrs;
                        
                        foreach (FieldInfo c in fields) {
+                               if (c == null)
+                                       continue;
                                match = false;
                                mattrs = c.Attributes;
                                if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
@@ -606,14 +868,35 @@ namespace System.Reflection.Emit {
                        throw not_supported ();
                }
 
-               public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
-                       if (methods == null)
+               private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type) {
+                       MethodInfo[] candidates;
+                       if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
+                               MethodInfo[] parent_methods = parent.GetMethods (bindingAttr);
+                               if (methods == null)
+                                       candidates = parent_methods;
+                               else {
+                                       candidates = new MethodInfo [methods.Length + parent_methods.Length];
+                                       parent_methods.CopyTo (candidates, 0);
+                                       methods.CopyTo (candidates, parent_methods.Length);
+                               }
+                       }
+                       else
+                               candidates = methods;
+                                       
+                       if (candidates == null)
                                return new MethodInfo [0];
+
                        ArrayList l = new ArrayList ();
                        bool match;
                        MethodAttributes mattrs;
 
-                       foreach (MethodInfo c in methods) {
+                       foreach (MethodInfo c in candidates) {
+                               if (c == null)
+                                       continue;
+                               if (name != null) {
+                                       if (String.Compare (c.Name, name, ignoreCase) != 0)
+                                               continue;
+                               }
                                match = false;
                                mattrs = c.Attributes;
                                if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
@@ -637,15 +920,67 @@ namespace System.Reflection.Emit {
                                        continue;
                                l.Add (c);
                        }
+
                        MethodInfo[] result = new MethodInfo [l.Count];
                        l.CopyTo (result);
                        return result;
                }
 
-               protected override MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
-                       throw not_supported ();
+               public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
+                       return GetMethodsByName (null, bindingAttr, false, this);
                }
-               
+
+               protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
+                                                            Binder binder,
+                                                            CallingConventions callConvention,
+                                                            Type[] types, ParameterModifier[] modifiers)
+               {
+                       if (!is_created)
+                               /* MS.Net throws this exception if the type is unfinished... */
+                               throw not_supported ();
+
+                       bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
+                       MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
+                       MethodInfo found = null;
+                       MethodBase[] match;
+                       int typesLen = (types != null) ? types.Length : 0;
+                       int count = 0;
+                       
+                       foreach (MethodInfo m in methods) {
+                               // Under MS.NET, Standard|HasThis matches Standard...
+                               if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
+                                       continue;
+                               found = m;
+                               count++;
+                       }
+
+                       if (count == 0)
+                               return null;
+                       
+                       if (count == 1 && typesLen == 0) 
+                               return found;
+
+                       match = new MethodBase [count];
+                       if (count == 1)
+                               match [0] = found;
+                       else {
+                               count = 0;
+                               foreach (MethodInfo m in methods) {
+                                       if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
+                                               continue;
+                                       match [count++] = m;
+                               }
+                       }
+                       
+                       if (types == null) 
+                               return (MethodInfo) Binder.FindMostDerivedMatch (match);
+
+                       if (binder == null)
+                               binder = Binder.DefaultBinder;
+                       
+                       return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
+               }
+
                public override Type GetNestedType( string name, BindingFlags bindingAttr) {
                        throw not_supported ();
                }
@@ -653,7 +988,7 @@ namespace System.Reflection.Emit {
                public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
                        bool match;
                        ArrayList result = new ArrayList ();
-               
+
                        if (subtypes == null)
                                return Type.EmptyTypes;
                        foreach (TypeBuilder t in subtypes) {
@@ -797,8 +1132,19 @@ namespace System.Reflection.Emit {
                                for (int i = 0; i < nnamed; ++i) {
                                        byte named_type = data [pos++];
                                        byte type = data [pos++];
-                                       int len = CustomAttributeBuilder.decode_len (data, pos, out pos);
-                                       string named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
+                                       int len;
+                                       string named_name;
+
+                                       if (type == 0x55) {
+                                               len = CustomAttributeBuilder.decode_len (data, pos, out pos);
+                                               string named_typename = CustomAttributeBuilder.string_from_bytes (data, pos, len);
+                                               pos += len;
+                                               // FIXME: Check that 'named_type' and 'named_typename' match, etc.
+                                               //        See related code/FIXME in mono/mono/metadata/reflection.c
+                                       }
+
+                                       len = CustomAttributeBuilder.decode_len (data, pos, out pos);
+                                       named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
                                        pos += len;
                                        /* all the fields are integers in StructLayout */
                                        int value = (int)data [pos++];
@@ -870,37 +1216,34 @@ namespace System.Reflection.Emit {
                        return res;
                }
 
-               static int InitializedDataCount = 0;
-               
                public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
-                       check_name ("name", name);
                        if (data == null)
                                throw new ArgumentNullException ("data");
                        if ((data.Length == 0) || (data.Length > 0x3f0000))
                                throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
-                       if (is_created)
-                               throw not_after_created ();
 
-                       string s = "$ArrayType$"+InitializedDataCount.ToString();
-                       TypeBuilder datablobtype = DefineNestedType (s,
-                               TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
-                               pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, data.Length);
-                       datablobtype.CreateType ();
-                       FieldBuilder res = DefineField (name, datablobtype, attributes|FieldAttributes.Assembly|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
+                       FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
                        res.SetRVAData (data);
-                       InitializedDataCount++;
+
                        return res;
                }
 
-               [MonoTODO]
+               static int UnmanagedDataCount = 0;
+               
                public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
                        check_name ("name", name);
                        if ((size <= 0) || (size > 0x3f0000))
-                               throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
+                               throw new ArgumentException ("size", "Data size must be > 0 and < 0x3f0000");
                        if (is_created)
                                throw not_after_created ();
 
-                       throw new NotImplementedException ();
+                       string s = "$ArrayType$"+UnmanagedDataCount.ToString();
+                       UnmanagedDataCount++;
+                       TypeBuilder datablobtype = DefineNestedType (s,
+                               TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
+                               pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
+                       datablobtype.CreateType ();
+                       return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
                }
 
                public TypeToken TypeToken {
@@ -915,6 +1258,8 @@ namespace System.Reflection.Emit {
                                throw not_after_created ();
 
                        parent = parentType;
+                       // will just set the parent-related bits if called a second time
+                       setup_internal_class (this);
                }
                internal int get_next_table_index (object obj, int table, bool inc) {
                        return pmodule.get_next_table_index (obj, table, inc);
@@ -954,30 +1299,82 @@ namespace System.Reflection.Emit {
                                throw new ArgumentException (argName, "Illegal name.");
                }
 
-#if GENERICS
-               public override bool HasGenericParameters {
+               public override String ToString ()
+               {
+                       return FullName;
+               }
+
+               [MonoTODO]
+               public override bool IsAssignableFrom (Type c)
+               {
+                       return base.IsAssignableFrom (c);
+               }
+
+               [MonoTODO]
+               public override bool IsSubclassOf (Type c)
+               {
+                       return base.IsSubclassOf (c);
+               }
+
+#if NET_2_0 || BOOTSTRAP_NET_2_0
+               public override Type[] GetGenericArguments ()
+               {
+                       if (generic_params != null)
+                               return generic_params;
+
+                       throw new InvalidOperationException ();
+               }
+
+               public override Type GetGenericTypeDefinition ()
+               {
+                       setup_generic_class (this);
+
+                       return base.GetGenericTypeDefinition ();
+               }
+
+               public override bool HasGenericArguments {
                        get {
                                throw new NotImplementedException ();
                        }
                }
 
-               public override bool HasUnboundGenericParameters {
+               public override bool ContainsGenericParameters {
                        get {
-                               throw new NotImplementedException ();
+                               return generic_params != null;
                        }
                }
 
-               public override bool IsUnboundGenericParameter {
+               public extern override bool IsGenericParameter {
+                       [MethodImplAttribute(MethodImplOptions.InternalCall)]
+                       get;
+               }
+
+               public override int GenericParameterPosition {
                        get {
                                throw new NotImplementedException ();
                        }
                }
 
-               public override int GenericParameterPosition {
+               public override MethodInfo DeclaringMethod {
                        get {
-                               throw new Exception ("Unimplemented");
+                               throw new NotImplementedException ();
                        }
                }
+
+               public GenericTypeParameterBuilder[] DefineGenericParameters (string[] names)
+               {
+                       generic_params = new GenericTypeParameterBuilder [names.Length];
+                       for (int i = 0; i < names.Length; i++)
+                               generic_params [i] = new GenericTypeParameterBuilder (
+                                       this, null, names [i], i);
+
+                       return generic_params;
+               }
+
+               public MethodBuilder DefineGenericMethod (string name, MethodAttributes attributes)
+               {
+                       return DefineMethod (name, attributes, CallingConventions.Standard, null, null);
+               }
 #endif
        }
 }