Tab
[mono.git] / mcs / class / corlib / System.Reflection.Emit / TypeBuilder.cs
index 36c4db1226f3d167c3b5562ae2dbb1bacaca61fe..88409ed5fe5ce966c2e3b4a95fa7b411662ec7bd 100644 (file)
@@ -1,5 +1,5 @@
 //
-// System.Reflection.Emit/TypeBuilder.cs
+// System.Reflection.Emit.TypeBuilder.cs
 //
 // Author:
 //   Paolo Molaro (lupus@ximian.com)
@@ -85,7 +85,7 @@ namespace System.Reflection.Emit
                bool createTypeCalled;
                private Type underlying_type;
 
-       public const int UnspecifiedTypeSize = 0;
+               public const int UnspecifiedTypeSize = 0;
 
                protected override TypeAttributes GetAttributeFlagsImpl ()
                {
@@ -401,7 +401,7 @@ namespace System.Reflection.Emit
                }
 
                private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
-                                                     PackingSize packsize, int typesize)
+                                                     PackingSize packSize, int typeSize)
                {
                        // Visibility must be NestedXXX
                        /* This breaks mcs
@@ -414,7 +414,7 @@ namespace System.Reflection.Emit
                                        if (iface == null)
                                                throw new ArgumentNullException ("interfaces");
 
-                       TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize, this);
+                       TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packSize, typeSize, this);
                        res.fullname = res.GetFullName ();
                        pmodule.RegisterTypeName (res, res.fullname);
                        if (subtypes != null) {
@@ -437,14 +437,14 @@ namespace System.Reflection.Emit
                        return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
                }
 
-               public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize)
+               public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typeSize)
                {
-                       return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
+                       return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typeSize);
                }
 
-               public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize)
+               public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packSize)
                {
-                       return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
+                       return DefineNestedType (name, attr, parent, null, packSize, UnspecifiedTypeSize);
                }
 
 #if NET_2_0
@@ -633,9 +633,9 @@ namespace System.Reflection.Emit
                        return DefineMethod (name, attributes, CallingConventions.Standard);
                }
 
-               public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callConv)
+               public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention)
                {
-                       return DefineMethod (name, attributes, callConv, null, null);
+                       return DefineMethod (name, attributes, callingConvention, null, null);
                }
 #endif
 
@@ -663,14 +663,14 @@ namespace System.Reflection.Emit
 #else
                internal
 #endif
-               FieldBuilder DefineField (string fieldName, Type type, Type[] requiredCustomAttributes, Type[] optionalCustomAttributes, FieldAttributes attributes)
+               FieldBuilder DefineField (string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
                {
                        check_name ("fieldName", fieldName);
                        if (type == typeof (void))
                                throw new ArgumentException ("Bad field type in defining field.");
                        check_not_created ();
 
-                       FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomAttributes, optionalCustomAttributes);
+                       FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomModifiers, optionalCustomModifiers);
                        if (fields != null) {
                                if (fields.Length == num_fields) {
                                        FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
@@ -751,6 +751,20 @@ namespace System.Reflection.Emit
                        }
                        return false;
                }
+
+               // Return whenever this type has a ctor defined using DefineMethod ()
+               private bool has_ctor_method () {
+                       MethodAttributes ctor_attrs = MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
+
+                       for (int i = 0; i < num_methods; ++i) {
+                               MethodBuilder mb = (MethodBuilder)(methods[i]);
+
+                               if (mb.Name == ConstructorInfo.ConstructorName && (mb.Attributes & ctor_attrs) == ctor_attrs)
+                                       return true;
+                       }
+
+                       return false;
+           }
                
                public Type CreateType()
                {
@@ -804,7 +818,7 @@ 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))
+                               (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed) && !has_ctor_method ())
                                DefineDefaultConstructor (MethodAttributes.Public);
 
                        if (ctors != null){
@@ -1687,12 +1701,18 @@ namespace System.Reflection.Emit
                                return true;
 
                        if (c.IsInterface) {
+                               if (parent != null && is_created) {
+                                       if (c.IsAssignableFrom (parent))
+                                               return true;
+                               }
+
                                if (interfaces == null)
                                        return false;
                                foreach (Type t in interfaces)
                                        if (c.IsAssignableFrom (t))
                                                return true;
-                               return false;
+                               if (!is_created)
+                                       return false;
                        }
 
                        if (parent == null)
@@ -1767,27 +1787,58 @@ namespace System.Reflection.Emit
                        return generic_params;
                }
 
-               public static ConstructorInfo GetConstructor (Type instanciated, ConstructorInfo ctor)
+               public static ConstructorInfo GetConstructor (Type type, ConstructorInfo constructor)
                {
-                       ConstructorInfo res = instanciated.GetConstructor (ctor);
+                       ConstructorInfo res = type.GetConstructor (constructor);
                        if (res == null)
                                throw new System.Exception ("constructor not found");
                        else
                                return res;
                }
 
-               public static MethodInfo GetMethod (Type instanciated, MethodInfo meth)
+               static bool IsValidGetMethodType (Type type)
                {
-                       MethodInfo res = instanciated.GetMethod (meth);
+                       if (type is TypeBuilder || type is MonoGenericClass)
+                               return true;
+                       /*GetMethod() must work with TypeBuilders after CreateType() was called.*/
+                       if (type.Module is ModuleBuilder)
+                               return true;
+                       if (type.IsGenericParameter)
+                               return false;
+
+                       Type[] inst = type.GetGenericArguments ();
+                       if (inst == null)
+                               return false;
+                       for (int i = 0; i < inst.Length; ++i) {
+                               if (IsValidGetMethodType (inst [i]))
+                                       return true;
+                       }
+                       return false;
+               }
+
+               public static MethodInfo GetMethod (Type type, MethodInfo method)
+               {
+                       if (!IsValidGetMethodType (type))
+                               throw new ArgumentException ("type is not TypeBuilder but " + type.GetType (), "type");
+
+                       if (!type.IsGenericType)
+                               throw new ArgumentException ("type is not a generic type", "type");
+
+                       if (!method.DeclaringType.IsGenericTypeDefinition)
+                               throw new ArgumentException ("method declaring type is not a generic type definition", "method");
+                       if (method.DeclaringType != type.GetGenericTypeDefinition ())
+                               throw new ArgumentException ("method declaring type is not the generic type definition of type", "method");
+
+                       MethodInfo res = type.GetMethod (method);
                        if (res == null)
-                               throw new System.Exception ("method not found");
-                       else
-                               return res;
+                               throw new ArgumentException (String.Format ("method {0} not found in type {1}", method.Name, type));
+                               
+                       return res;
                }
 
-               public static FieldInfo GetField (Type instanciated, FieldInfo fld)
+               public static FieldInfo GetField (Type type, FieldInfo field)
                {
-                       FieldInfo res = instanciated.GetField (fld);
+                       FieldInfo res = type.GetField (field);
                        if (res == null)
                                throw new System.Exception ("field not found");
                        else