Merge pull request #485 from mtausig/master
[mono.git] / mcs / class / corlib / System.Reflection.Emit / TypeBuilder.cs
index 1358079b578f35e581ed127c622c15a777510683..fc60d1a377a67fdd7d7b114b8b0c026db552da5a 100644 (file)
@@ -48,6 +48,7 @@ namespace System.Reflection.Emit
        [ComVisible (true)]
        [ComDefaultInterface (typeof (_TypeBuilder))]
        [ClassInterface (ClassInterfaceType.None)]
+       [StructLayout (LayoutKind.Sequential)]
        public sealed class TypeBuilder : Type, _TypeBuilder
        {
 #pragma warning disable 169            
@@ -144,7 +145,7 @@ namespace System.Reflection.Emit
                        }
                        pmodule = mb;
 
-                       if (((attr & TypeAttributes.Interface) == 0) && (parent == null) && !IsCompilerContext)
+                       if (((attr & TypeAttributes.Interface) == 0) && (parent == null))
                                this.parent = typeof (object);
 
                        // skip .<Module> ?
@@ -195,7 +196,7 @@ namespace System.Reflection.Emit
                                if (is_created)
                                        return created.UnderlyingSystemType;
 
-                               if (!IsCompilerContext && IsEnum) {
+                               if (IsEnum) {
                                        if (underlying_type != null)
                                                return underlying_type;
                                        throw new InvalidOperationException (
@@ -361,7 +362,7 @@ namespace System.Reflection.Emit
 
                public override bool IsDefined (Type attributeType, bool inherit)
                {
-                       if (!is_created && !IsCompilerContext)
+                       if (!is_created)
                                throw new NotSupportedException ();
                        /*
                         * MS throws NotSupported here, but we can't because some corlib
@@ -473,14 +474,19 @@ namespace System.Reflection.Emit
                [ComVisible (true)]
                public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
                {
-                       Type parent_type;
+                       Type parent_type, old_parent_type;
 
                        if (parent != null)
                                parent_type = parent;
                        else
                                parent_type = pmodule.assemblyb.corlib_object_type;
 
+                       old_parent_type = parent_type;
                        parent_type = parent_type.InternalResolve ();
+                       /*This avoids corlib to have self references.*/
+                       if (parent_type == typeof (object) || parent_type == typeof (ValueType))
+                               parent_type = old_parent_type;
+
                        ConstructorInfo parent_constructor =
                                parent_type.GetConstructor (
                                        BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
@@ -659,7 +665,7 @@ namespace System.Reflection.Emit
                                create_internal_class (this);
                        }
 
-                       if (IsEnum && !IsCompilerContext) {
+                       if (IsEnum) {
                                if (underlying_type == null && (attributes & FieldAttributes.Static) == 0)
                                        underlying_type = type;
                        }
@@ -669,10 +675,22 @@ namespace System.Reflection.Emit
 
                public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes)
                {
-                       return DefineProperty (name, attributes, returnType, null, null, parameterTypes, null, null);
+                       return DefineProperty (name, attributes, 0, returnType, null, null, parameterTypes, null, null);
                }
+               
+#if NET_4_0
+               public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
+               {
+                       return DefineProperty (name, attributes, callingConvention, returnType , null, null, parameterTypes, null, null);
+               }       
+#endif
 
-               public  PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
+               public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
+               {
+                       return DefineProperty (name, attributes, 0, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
+               }
+               
+               public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
                {
                        check_name ("name", name);
                        if (parameterTypes != null)
@@ -681,16 +699,13 @@ namespace System.Reflection.Emit
                                                throw new ArgumentNullException ("parameterTypes");
                        check_not_created ();
 
-                       PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
+                       PropertyBuilder res = new PropertyBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
 
                        if (properties != null) {
-                               PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
-                               System.Array.Copy (properties, new_properties, properties.Length);
-                               new_properties [properties.Length] = res;
-                               properties = new_properties;
+                               Array.Resize (ref properties, properties.Length + 1);
+                               properties [properties.Length - 1] = res;
                        } else {
-                               properties = new PropertyBuilder [1];
-                               properties [0] = res;
+                               properties = new PropertyBuilder [1] { res };
                        }
                        return res;
                }
@@ -766,6 +781,15 @@ namespace System.Reflection.Emit
                                }
                        }
 
+                       //
+                       // On classes, define a default constructor if not provided
+                       //
+                       if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>") && 
+                               (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed) && !has_ctor_method ())
+                               DefineDefaultConstructor (MethodAttributes.Public);
+
+                       createTypeCalled = true;
+
                        if ((parent != null) && parent.IsSealed)
                                throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because the parent type is sealed.");
 
@@ -783,19 +807,11 @@ namespace System.Reflection.Emit
                                }
                        }
 
-                       //
-                       // On classes, define a default constructor if not provided
-                       //
-                       if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>") && 
-                               (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed) && !has_ctor_method ())
-                               DefineDefaultConstructor (MethodAttributes.Public);
-
                        if (ctors != null){
                                foreach (ConstructorBuilder ctor in ctors) 
                                        ctor.fixup ();
                        }
 
-                       createTypeCalled = true;
                        created = create_runtime_class (this);
                        if (created != null)
                                return created;
@@ -832,10 +848,7 @@ namespace System.Reflection.Emit
                        if (is_created)
                                return created.GetConstructors (bindingAttr);
 
-                       if (!IsCompilerContext)
-                               throw new NotSupportedException ();
-
-                       return GetConstructorsInternal (bindingAttr);
+                       throw new NotSupportedException ();
                }
 
                internal ConstructorInfo[] GetConstructorsInternal (BindingFlags bindingAttr)
@@ -896,9 +909,7 @@ namespace System.Reflection.Emit
                {
                        if (is_created)
                                return created.GetEvents (bindingAttr);
-                       if (!IsCompilerContext)
-                               throw new NotSupportedException ();
-                       return new EventInfo [0]; /*FIXME shouldn't we return the events here?*/
+                       throw new NotSupportedException ();
                }
 
                // This is only used from MonoGenericInst.initialize().
@@ -1237,7 +1248,7 @@ namespace System.Reflection.Emit
 
                public override Type[] GetNestedTypes (BindingFlags bindingAttr)
                {
-                       if (!is_created && !IsCompilerContext)
+                       if (!is_created)
                                throw new NotSupportedException ();
 
                        bool match;
@@ -1420,14 +1431,6 @@ namespace System.Reflection.Emit
                        }
                }
                
-               //
-               // Used internally by mcs only
-               //
-               internal void SetCharSet (TypeAttributes ta)
-               {
-                       this.attrs = ta;
-               }
-
                public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
                {
                        if (customBuilder == null)
@@ -1454,9 +1457,10 @@ namespace System.Reflection.Emit
                                        // we should ignore it since it can be any value anyway...
                                        throw new Exception ("Error in customattr");
                                }
-                               string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
+                               
+                               var ctor_type = customBuilder.Ctor is ConstructorBuilder ? ((ConstructorBuilder)customBuilder.Ctor).parameters[0] : customBuilder.Ctor.GetParameters()[0].ParameterType;
                                int pos = 6;
-                               if (first_type_name == "System.Int16")
+                               if (ctor_type.FullName == "System.Int16")
                                        pos = 4;
                                int nnamed = (int)data [pos++];
                                nnamed |= ((int)data [pos++]) << 8;
@@ -1634,12 +1638,6 @@ namespace System.Reflection.Emit
                        return created.GetInterfaceMap (interfaceType);
                }
 
-               internal override bool IsCompilerContext {
-                       get {
-                               return pmodule.assemblyb.IsCompilerContext;
-                       }
-               }
-
                internal override Type InternalResolve ()
                {
                        check_created ();
@@ -1648,7 +1646,7 @@ namespace System.Reflection.Emit
 
                internal bool is_created {
                        get {
-                               return created != null;
+                               return createTypeCalled;
                        }
                }
 
@@ -1811,6 +1809,11 @@ namespace System.Reflection.Emit
                        if (constructor == null)
                                throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
 
+                       if (!constructor.DeclaringType.IsGenericTypeDefinition)
+                               throw new ArgumentException ("constructor declaring type is not a generic type definition", "constructor");
+                       if (constructor.DeclaringType != type.GetGenericTypeDefinition ())
+                               throw new ArgumentException ("constructor declaring type is not the generic type definition of type", "constructor");
+
                        ConstructorInfo res = type.GetConstructor (constructor);
                        if (res == null)
                                throw new ArgumentException ("constructor not found");
@@ -1874,6 +1877,9 @@ namespace System.Reflection.Emit
                        if (field is FieldOnTypeBuilderInst)
                                throw new ArgumentException ("The specified field must be declared on a generic type definition.", "field");
 
+                       if (field.DeclaringType != type.GetGenericTypeDefinition ())
+                               throw new ArgumentException ("field declaring type is not the generic type definition of type", "method");
+
                        FieldInfo res = type.GetField (field);
                        if (res == null)
                                throw new System.Exception ("field not found");
@@ -1881,6 +1887,20 @@ namespace System.Reflection.Emit
                                return res;
                }
 
+               internal TypeCode GetTypeCodeInternal () {
+                       if (parent == pmodule.assemblyb.corlib_enum_type) {
+                               for (int i = 0; i < num_fields; ++i) {
+                                       FieldBuilder f = fields [i];
+                                       if (!f.IsStatic)
+                                               return Type.GetTypeCode (f.FieldType);
+                               }
+                               throw new InvalidOperationException ("Enum basetype field not defined");
+                       } else {
+                               return Type.GetTypeCodeInternal (this);
+                       }
+               }
+
+
                void _TypeBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
                {
                        throw new NotImplementedException ();
@@ -1900,5 +1920,11 @@ namespace System.Reflection.Emit
                {
                        throw new NotImplementedException ();
                }
+
+               internal override bool IsUserType {
+                       get {
+                               return false;
+                       }
+               }
        }
 }