First pass killing compiler context support code.
authorRodrigo Kumpera <kumpera@gmail.com>
Tue, 10 Jul 2012 21:38:32 +0000 (18:38 -0300)
committerRodrigo Kumpera <kumpera@gmail.com>
Tue, 10 Jul 2012 21:38:32 +0000 (18:38 -0300)
* MonoGenericClass.cs: Switch to MS's behavior and kill
on ton of code in the process. Only MCS depended on it
and now we can kill it.

mcs/class/corlib/System.Reflection/MonoGenericClass.cs
mcs/class/corlib/Test/System.Reflection/MonoGenericClassTest.cs

index feccf4c174ec256eafef3f750ae0d4ec11ed64fd..68658fd6b88e6e07d0a43d266d5dcf2b082e709b 100644 (file)
@@ -60,7 +60,6 @@ namespace System.Reflection
                #endregion
 
                Hashtable fields, ctors, methods;
-               int event_count;
 
                internal MonoGenericClass ()
                {
@@ -117,34 +116,6 @@ namespace System.Reflection
                        return tb.GetConstructorsInternal (flags);
                }
 
-               /*
-               MethodInfo[] GetMethodsFromGTD (BindingFlags bf)
-               {
-                       TypeBuilder tb = generic_type as TypeBuilder;
-                       if (tb == null)
-                               return generic_type.GetMethods (bf);
-
-                       MethodInfo[] res = new MethodInfo [tb.num_methods];
-                       if (tb.num_methods > 0)
-                               Array.Copy (tb.methods, res, tb.num_methods);
-
-                       return res;
-               }
-               */
-
-               FieldInfo[] GetFieldsFromGTD (BindingFlags bf)
-               {
-                       TypeBuilder tb = generic_type as TypeBuilder;
-                       if (tb == null)
-                               return generic_type.GetFields (bf);
-
-                       FieldInfo[] res = new FieldInfo [tb.num_fields];
-                       if (tb.num_fields > 0)
-                               Array.Copy (tb.fields, res, tb.num_fields);
-
-                       return res;
-               }
-
                /*@hint might not be honored so it required aditional filtering
                TODO move filtering into here for the TypeBuilder case and remove the hint ugliness 
                */
@@ -177,29 +148,6 @@ namespace System.Reflection
                        return res;
                }
 
-               static Type PeelType (Type t) {
-                       if (t.HasElementType)
-                               return PeelType (t.GetElementType ());
-                       if (t.IsGenericType && !t.IsGenericParameter)
-                               return t.GetGenericTypeDefinition ();
-                       return t;
-               }
-
-               static PropertyInfo[] GetPropertiesInternal (Type type, BindingFlags bf)
-               {
-                       TypeBuilder tb = type as TypeBuilder;
-                       if (tb != null)
-                               return tb.properties;
-                       return type.GetProperties (bf); 
-               }
-
-               Type[] GetInterfacesFromGTD ()
-               {
-                       TypeBuilder tb = generic_type as TypeBuilder;
-                       if (tb != null)
-                               return tb.interfaces;
-                       return generic_type.GetInterfaces ();   
-               }
 
                internal bool IsCreated {
                        get {
@@ -219,14 +167,12 @@ namespace System.Reflection
                        MonoGenericClass parent = GetParentType () as MonoGenericClass;
                        if (parent != null)
                                parent.initialize ();
-                       EventInfo[] events = GetEventsFromGTD (flags);
-                       event_count = events.Length;
                                
                        initialize (generic_type.GetMethods (flags),
                                                GetConstructorsFromGTD (flags),
                                                generic_type.GetFields (flags),
                                                generic_type.GetProperties (flags),
-                                               events);
+                                               GetEventsFromGTD (flags));
 
                        initialized = true;
                }
@@ -279,21 +225,7 @@ namespace System.Reflection
                }
                
                public override Type BaseType {
-                       get {
-                               Type parent = GetParentType ();
-                               return parent != null ? parent : generic_type.BaseType;
-                       }
-               }
-
-               Type[] GetInterfacesInternal ()
-               {
-                       Type[] ifaces = GetInterfacesFromGTD ();
-                       if (ifaces == null)
-                               return Type.EmptyTypes;
-                       Type[] res = new Type [ifaces.Length];
-                       for (int i = 0; i < res.Length; ++i)
-                               res [i] = InflateType (ifaces [i]);
-                       return res;
+                       get { return generic_type.BaseType; }
                }
 
                public override Type[] GetInterfaces ()
@@ -343,55 +275,6 @@ namespace System.Reflection
                        throw new NotSupportedException ();
                }
 
-               MethodInfo[] GetMethodsInternal (BindingFlags bf, MonoGenericClass reftype)
-               {
-                       if (reftype != this)
-                               bf |= BindingFlags.DeclaredOnly; /*To avoid duplicates*/
-
-                       MethodInfo[] methods = GetMethodsFromGTDWithHint (bf);
-                       if (methods.Length == 0)
-                               return new MethodInfo [0];
-
-                       ArrayList l = new ArrayList ();
-                       bool match;
-                       MethodAttributes mattrs;
-
-                       initialize ();
-
-                       for (int i = 0; i < methods.Length; ++i) {
-                               MethodInfo c = methods [i];
-
-                               match = false;
-                               mattrs = c.Attributes;
-                               if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
-                                       if ((bf & BindingFlags.Public) != 0)
-                                               match = true;
-                               } else {
-                                       if ((bf & BindingFlags.NonPublic) != 0)
-                                               match = true;
-                               }
-                               if (!match)
-                                       continue;
-                               match = false;
-                               if ((mattrs & MethodAttributes.Static) != 0) {
-                                       if ((bf & BindingFlags.Static) != 0)
-                                               match = true;
-                               } else {
-                                       if ((bf & BindingFlags.Instance) != 0)
-                                               match = true;
-                               }
-                               if (!match)
-                                       continue;
-                               if (c.DeclaringType.IsGenericTypeDefinition)
-                                       c = TypeBuilder.GetMethod (this, c);
-                               l.Add (c);
-                       }
-
-                       MethodInfo[] result = new MethodInfo [l.Count];
-                       l.CopyTo (result);
-                       return result;
-               }
-
                public override ConstructorInfo[] GetConstructors (BindingFlags bf)
                {
                        throw new NotSupportedException ();
@@ -446,189 +329,24 @@ namespace System.Reflection
                        throw new NotSupportedException ();
                }
 
-               FieldInfo[] GetFieldsInternal (BindingFlags bf, MonoGenericClass reftype)
-               {
-                       FieldInfo[] fields = GetFieldsFromGTD (bf);
-                       if (fields.Length == 0)
-                               return new FieldInfo [0];
-
-                       ArrayList l = new ArrayList ();
-                       bool match;
-                       FieldAttributes fattrs;
-
-                       initialize ();
-
-                       for (int i = 0; i < fields.Length; i++) {
-                               FieldInfo c = fields [i];
-
-                               match = false;
-                               fattrs = c.Attributes;
-                               if ((fattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
-                                       if ((bf & BindingFlags.Public) != 0)
-                                               match = true;
-                               } else {
-                                       if ((bf & BindingFlags.NonPublic) != 0)
-                                               match = true;
-                               }
-                               if (!match)
-                                       continue;
-                               match = false;
-                               if ((fattrs & FieldAttributes.Static) != 0) {
-                                       if ((bf & BindingFlags.Static) != 0)
-                                               match = true;
-                               } else {
-                                       if ((bf & BindingFlags.Instance) != 0)
-                                               match = true;
-                               }
-                               if (!match)
-                                       continue;
-                               l.Add (TypeBuilder.GetField (this, c));
-                       }
-
-                       FieldInfo[] result = new FieldInfo [l.Count];
-                       l.CopyTo (result);
-                       return result;
-               }
-
                public override PropertyInfo[] GetProperties (BindingFlags bf)
                {
                        throw new NotSupportedException ();
                }
 
-               PropertyInfo[] GetPropertiesInternal (BindingFlags bf, MonoGenericClass reftype)
-               {
-                       PropertyInfo[] props = GetPropertiesInternal (generic_type, bf);
-                       if (props == null || props.Length == 0)
-                               return new PropertyInfo [0];
-
-                       ArrayList l = new ArrayList ();
-                       bool match;
-                       MethodAttributes mattrs;
-                       MethodInfo accessor;
-
-                       initialize ();
-
-                       foreach (PropertyInfo pinfo in props) {
-                               match = false;
-                               accessor = pinfo.GetGetMethod (true);
-                               if (accessor == null)
-                                       accessor = pinfo.GetSetMethod (true);
-                               if (accessor == null)
-                                       continue;
-                               mattrs = accessor.Attributes;
-                               if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
-                                       if ((bf & BindingFlags.Public) != 0)
-                                               match = true;
-                               } else {
-                                       if ((bf & BindingFlags.NonPublic) != 0)
-                                               match = true;
-                               }
-                               if (!match)
-                                       continue;
-                               match = false;
-                               if ((mattrs & MethodAttributes.Static) != 0) {
-                                       if ((bf & BindingFlags.Static) != 0)
-                                               match = true;
-                               } else {
-                                       if ((bf & BindingFlags.Instance) != 0)
-                                               match = true;
-                               }
-                               if (!match)
-                                       continue;
-                               l.Add (new PropertyOnTypeBuilderInst (reftype, pinfo));
-                       }
-                       PropertyInfo[] result = new PropertyInfo [l.Count];
-                       l.CopyTo (result);
-                       return result;
-               }
-
                public override EventInfo[] GetEvents (BindingFlags bf)
                {
                        throw new NotSupportedException ();
                }
-       
-               EventInfo[] GetEventsInternal (BindingFlags bf, MonoGenericClass reftype) {
-                       TypeBuilder tb = generic_type as TypeBuilder;
-                       if (tb == null) {
-                               EventInfo[] res = generic_type.GetEvents (bf);
-                               for (int i = 0; i < res.Length; ++i)
-                                       res [i] = new EventOnTypeBuilderInst (this, res [i]);
-                               return res;
-                       }
-                       EventBuilder[] events = tb.events;
-
-                       if (events == null || events.Length == 0)
-                               return new EventInfo [0];
-
-                       initialize ();
-
-                       ArrayList l = new ArrayList ();
-                       bool match;
-                       MethodAttributes mattrs;
-                       MethodInfo accessor;
-
-                       for (int i = 0; i < event_count; ++i) {
-                               EventBuilder ev = events [i];
-
-                               match = false;
-                               accessor = ev.add_method;
-                               if (accessor == null)
-                                       accessor = ev.remove_method;
-                               if (accessor == null)
-                                       continue;
-                               mattrs = accessor.Attributes;
-                               if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
-                                       if ((bf & BindingFlags.Public) != 0)
-                                               match = true;
-                               } else {
-                                       if ((bf & BindingFlags.NonPublic) != 0)
-                                               match = true;
-                               }
-                               if (!match)
-                                       continue;
-                               match = false;
-                               if ((mattrs & MethodAttributes.Static) != 0) {
-                                       if ((bf & BindingFlags.Static) != 0)
-                                               match = true;
-                               } else {
-                                       if ((bf & BindingFlags.Instance) != 0)
-                                               match = true;
-                               }
-                               if (!match)
-                                       continue;
-                               l.Add (new EventOnTypeBuilderInst (this, ev));
-                       }
-                       EventInfo[] result = new EventInfo [l.Count];
-                       l.CopyTo (result);
-                       return result;
-               }
 
                public override Type[] GetNestedTypes (BindingFlags bf)
                {
-                       return generic_type.GetNestedTypes (bf);
+                       throw new NotSupportedException ();
                }
 
                public override bool IsAssignableFrom (Type c)
                {
-                       if (c == this)
-                               return true;
-
-                       Type[] interfaces = GetInterfacesInternal ();
-
-                       if (c.IsInterface) {
-                               if (interfaces == null)
-                                       return false;
-                               foreach (Type t in interfaces)
-                                       if (c.IsAssignableFrom (t))
-                                               return true;
-                               return false;
-                       }
-
-                       Type parent = GetParentType ();
-                       if (parent == null)
-                               return c == typeof (object);
-                       else
-                               return c.IsAssignableFrom (parent);
+                       throw new NotSupportedException ();
                }
 
                public override Type UnderlyingSystemType {
@@ -717,10 +435,6 @@ namespace System.Reflection
 
                public override bool ContainsGenericParameters {
                        get {
-                               /*FIXME remove this once compound types are not instantiated using MGC*/
-                               if (HasElementType)
-                                       return GetElementType ().ContainsGenericParameters;
-
                                foreach (Type t in type_arguments) {
                                        if (t.ContainsGenericParameters)
                                                return true;
@@ -734,11 +448,11 @@ namespace System.Reflection
                }
 
                public override bool IsGenericType {
-                       get { return !HasElementType; }
+                       get { return true; }
                }
 
                public override Type DeclaringType {
-                       get { return InflateType (generic_type.DeclaringType); }
+                       get { return generic_type.DeclaringType; }
                }
 
                public override RuntimeTypeHandle TypeHandle {
@@ -881,7 +595,7 @@ namespace System.Reflection
                        throw new NotSupportedException ();
                }
 
-               internal virtual bool IsUserType {
+               internal override bool IsUserType {
                        get {
                                foreach (var t in type_arguments) {
                                        if (t.IsUserType)
index 045eeb4e0cf6dc0ace15096949eb194eba9f3761..e0cec246a6c721d41bfa7d18f5a061883ad31867 100644 (file)
@@ -148,6 +148,14 @@ namespace MonoTests.System.Reflection.Emit
                                ginst.GetCustomAttributes (typeof (int), true);
                                Assert.Fail ("#13");
                        } catch (NotSupportedException) {  }
+                       try {
+                               ginst.IsAssignableFrom (ginst);
+                               Assert.Fail ("#14");
+                       } catch (NotSupportedException) {  }
+                       try {
+                               ginst.GetNestedTypes (BindingFlags.Public);
+                               Assert.Fail ("#14");
+                       } catch (NotSupportedException) {  }
                }
 
                [Test]
@@ -166,5 +174,33 @@ namespace MonoTests.System.Reflection.Emit
                        /*This must not throw*/
                        rtInst.IsDefined (typeof (int), true);
                }
+
+               public class Bar<T> {
+                       public class Foo<T> {}
+               }
+
+               [Test]
+               public void DeclaringTypeMustReturnNonInflatedType ()
+               {
+                       var ut = new TypeDelegator (typeof (int));
+                       var ut2 = typeof(Bar<>.Foo<>);
+                       var t = ut2.MakeGenericType (ut, ut);
+                       Assert.AreSame (typeof (Bar<>), t.DeclaringType, "#1");
+               }
+
+               public class Base<T> {}
+               public class SubClass<K> : Base<K> {}
+
+               [Test]
+               public void BaseTypeMustReturnNonInflatedType ()
+               {
+                       var ut = new TypeDelegator (typeof (int));
+                       var ut2 = typeof(SubClass<>);
+                       var t = ut2.MakeGenericType (ut);
+                       //This is Base<K> where K is SubClass::K
+                       var expected = typeof (Base<>).MakeGenericType (typeof (SubClass<>).GetGenericArguments ()[0]);
+                       Assert.AreSame (expected, t.BaseType, "#1");
+                       
+               }
        }
 }