Merge pull request #498 from Unroll-Me/master
[mono.git] / mcs / class / corlib / System.Reflection.Emit / TypeBuilder.cs
index 0a7be28f12e0fdf5cc4146d4913b26bd6bbf5c2e..462717057ccb0cf364ce10a8d0b79e8dc6b5a74c 100644 (file)
@@ -31,6 +31,7 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
+#if !FULL_AOT_RUNTIME
 using System;
 using System.Text;
 using System.Reflection;
@@ -48,6 +49,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 +146,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 +197,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 +363,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 +475,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 +666,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 +676,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 +700,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;
                }
@@ -833,10 +849,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)
@@ -897,9 +910,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().
@@ -1238,7 +1249,7 @@ namespace System.Reflection.Emit
 
                public override Type[] GetNestedTypes (BindingFlags bindingAttr)
                {
-                       if (!is_created && !IsCompilerContext)
+                       if (!is_created)
                                throw new NotSupportedException ();
 
                        bool match;
@@ -1421,14 +1432,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)
@@ -1455,9 +1458,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;
@@ -1635,12 +1639,6 @@ namespace System.Reflection.Emit
                        return created.GetInterfaceMap (interfaceType);
                }
 
-               internal override bool IsCompilerContext {
-                       get {
-                               return pmodule.assemblyb.IsCompilerContext;
-                       }
-               }
-
                internal override Type InternalResolve ()
                {
                        check_created ();
@@ -1812,6 +1810,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");
@@ -1875,6 +1878,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");
@@ -1882,6 +1888,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 ();
@@ -1901,5 +1921,12 @@ namespace System.Reflection.Emit
                {
                        throw new NotImplementedException ();
                }
+
+               internal override bool IsUserType {
+                       get {
+                               return false;
+                       }
+               }
        }
 }
+#endif