Forget to commit.
[mono.git] / mcs / mcs / typemanager.cs
index 0d4f0b657bd6b31cd664d3952b065002a56b95cb..ef1a806073e195c6677067be9ad0bf24d36fcf62 100644 (file)
@@ -31,7 +31,10 @@ using System.Diagnostics;
 
 namespace Mono.CSharp {
 
-public partial class TypeManager {
+#if GMCS_SOURCE
+       partial
+#endif
+       class TypeManager {
        //
        // A list of core types that the compiler requires or uses
        //
@@ -57,7 +60,6 @@ public partial class TypeManager {
        static public Type multicast_delegate_type;
        static public Type void_type;
        static public Type null_type;
-       static public Type enumeration_type;
        static public Type array_type;
        static public Type runtime_handle_type;
        static public Type icloneable_type;
@@ -70,6 +72,7 @@ public partial class TypeManager {
        static public Type iasyncresult_type;
        static public Type asynccallback_type;
        static public Type intptr_type;
+       static public Type uintptr_type;
        static public Type monitor_type;
        static public Type interlocked_type;
        static public Type runtime_field_handle_type;
@@ -167,7 +170,7 @@ public partial class TypeManager {
        static public MethodInfo string_concat_object_object;
        static public MethodInfo string_concat_object_object_object;
        static public MethodInfo string_concat_object_dot_dot_dot;
-       static public MethodInfo string_isinterneted_string;
+       static public MethodInfo string_isinterned_string;
        static public MethodInfo system_type_get_type_from_handle;
        static public MethodInfo bool_movenext_void;
        static public MethodInfo ienumerable_getenumerator_void;
@@ -275,9 +278,7 @@ public partial class TypeManager {
                builder_to_method = null;
                
                fields = null;
-               fieldbuilders_to_fields = null;
                events = null;
-               priv_fields_events = null;
                type_hash = null;
                propertybuilder_to_property = null;
 
@@ -298,7 +299,7 @@ public partial class TypeManager {
 
                if (!(mi is MethodBase))
                        return false;
-               
+
                if (mi.Name != sig.name)
                        return false;
 
@@ -388,6 +389,7 @@ public partial class TypeManager {
 #endif
 
                // to uncover regressions
+               AllClsTopLevelTypes = null;
                cons_param_array_attribute = null;
        }
 
@@ -663,6 +665,16 @@ public partial class TypeManager {
                + match.Groups [2].Captures [0].Value;
        }
 
+       // Used for error reporting to show symbolic name instead of underlying value
+       public static string CSharpEnumValue (Type t, object value)
+       {
+               Enum e = LookupDeclSpace (t) as Enum;
+               if (e == null)
+                       return System.Enum.GetName (t, value);
+
+               return e.GetDefinition (value).GetSignatureForError ();
+       }
+
         /// <summary>
        ///  Returns the signature of the method with full namespace classification
        /// </summary>
@@ -764,10 +776,9 @@ public partial class TypeManager {
 
                ParameterData iparams = GetParameterData (mb);
                string parameters = iparams.GetSignatureForError ();
-               string accessor = "";
+               int accessor_end = 0;
 
-               // Is property
-               if (mb.IsSpecialName) {
+               if (!mb.IsConstructor && TypeManager.IsSpecialMethod (mb)) {
                        Operator.OpType ot = Operator.GetOperatorType (mb.Name);
                        if (ot != Operator.OpType.TOP) {
                                sig.Append ("operator ");
@@ -776,28 +787,24 @@ public partial class TypeManager {
                                return sig.ToString ();
                        }
 
-                       if (mb.Name.StartsWith ("get_") || mb.Name.StartsWith ("set_")) {
-                               accessor = mb.Name.Substring (0, 3);
+                       bool is_getter = mb.Name.StartsWith ("get_");
+                       bool is_setter = mb.Name.StartsWith ("set_");
+                       if (is_getter || is_setter || mb.Name.StartsWith ("add_")) {
+                               accessor_end = 3;
+                       } else if (mb.Name.StartsWith ("remove_")) {
+                               accessor_end = 6;
                        }
-               }
 
-               // Is indexer
-               if (mb.IsSpecialName && !mb.IsConstructor) {
-                       if (iparams.Count > (mb.Name.StartsWith ("get_") ? 0 : 1)) {
+                       // Is indexer
+                       if (iparams.Count > (is_getter ? 0 : 1)) {
                                sig.Append ("this[");
-                               if (show_accessor) {
+                               if (is_getter)
                                        sig.Append (parameters.Substring (1, parameters.Length - 2));
-                               }
-                               else {
-                                       int before_ret_val = parameters.LastIndexOf (',');
-                                       if (before_ret_val < 0)
-                                               sig.Append (parameters.Substring (1, parameters.Length - 2));
-                                       else
-                                               sig.Append (parameters.Substring (1, before_ret_val - 1));
-                               }
+                               else
+                                       sig.Append (parameters.Substring (1, parameters.LastIndexOf (',') - 1));
                                sig.Append (']');
                        } else {
-                               sig.Append (mb.Name.Substring (4));
+                               sig.Append (mb.Name.Substring (accessor_end + 1));
                        }
                } else {
                        if (mb.Name == ".ctor")
@@ -822,9 +829,9 @@ public partial class TypeManager {
                        sig.Append (parameters);
                }
 
-               if (show_accessor && accessor.Length > 0) {
+               if (show_accessor && accessor_end > 0) {
                        sig.Append ('.');
-                       sig.Append (accessor);
+                       sig.Append (mb.Name.Substring (0, accessor_end));
                }
 
                return sig.ToString ();
@@ -844,7 +851,7 @@ public partial class TypeManager {
 
        static public string CSharpSignature (EventInfo ei)
        {
-               return CSharpName (ei.DeclaringType) + '.' + ei.Name;
+               return CSharpName (ei.DeclaringType) + "." + ei.Name;
        }
 
        /// <summary>
@@ -856,8 +863,12 @@ public partial class TypeManager {
                Namespace ns = RootNamespace.Global.GetNamespace (ns_name, true);
                FullNamedExpression fne = ns.Lookup (RootContext.ToplevelTypes, name, Location.Null);
                Type t = fne == null ? null : fne.Type;
-               if (t == null)
+               if (t == null) {
                        Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
+                       return null;
+               }
+
+               AttributeTester.RegisterNonObsoleteType (t);
                return t;
        }
 
@@ -899,7 +910,7 @@ public partial class TypeManager {
                return GetMethod (t, name, args, false, report_errors);
        }
 
-       static MethodInfo GetMethod (Type t, string name, Type [] args)
+       public static MethodInfo GetMethod (Type t, string name, Type [] args)
        {
                return GetMethod (t, name, args, true);
        }
@@ -986,7 +997,9 @@ public partial class TypeManager {
        public static void InitCoreTypes ()
        {
                object_type   = CoreLookupType ("System", "Object");
+               system_object_expr.Type = object_type;
                value_type    = CoreLookupType ("System", "ValueType");
+               system_valuetype_expr.Type = value_type;
 
                InitEnumUnderlyingTypes ();
 
@@ -1018,6 +1031,7 @@ public partial class TypeManager {
                interlocked_type     = CoreLookupType ("System.Threading", "Interlocked");
                monitor_type         = CoreLookupType ("System.Threading", "Monitor");
                intptr_type          = CoreLookupType ("System", "IntPtr");
+               uintptr_type         = CoreLookupType ("System", "UIntPtr");
 
                attribute_type       = CoreLookupType ("System", "Attribute");
                attribute_usage_type = CoreLookupType ("System", "AttributeUsageAttribute");
@@ -1136,7 +1150,6 @@ public partial class TypeManager {
                        }
                }
 
-               system_object_expr.Type = object_type;
                system_string_expr.Type = string_type;
                system_boolean_expr.Type = bool_type;
                system_decimal_expr.Type = decimal_type;
@@ -1154,7 +1167,6 @@ public partial class TypeManager {
                system_void_expr.Type = void_type;
                system_asynccallback_expr.Type = asynccallback_type;
                system_iasyncresult_expr.Type = iasyncresult_type;
-               system_valuetype_expr.Type = value_type;
 
                //
                // These are only used for compare purposes
@@ -1195,7 +1207,7 @@ public partial class TypeManager {
                        string_type, "Concat", params_object);
 
                Type [] string_ = { string_type };
-               string_isinterneted_string = GetMethod (
+               string_isinterned_string = GetMethod (
                        string_type, "IsInterned", string_);
                
                Type [] runtime_type_handle = { runtime_handle_type };
@@ -1428,6 +1440,11 @@ public partial class TypeManager {
        {
                MemberCache cache;
 
+#if GMCS_SOURCE && MS_COMPATIBLE
+        if (t.IsGenericType && !t.IsGenericTypeDefinition)
+            t = t.GetGenericTypeDefinition();
+#endif
+
                //
                // If this is a dynamic type, it's always in the `builder_to_declspace' hash table
                // and we can ask the DeclSpace for the MemberCache.
@@ -1463,7 +1480,7 @@ public partial class TypeManager {
                // a TypeBuilder array will return a Type, not a TypeBuilder,
                // and we can not call FindMembers on this type.
                //
-               if (t == TypeManager.array_type || t.IsSubclassOf (TypeManager.array_type)) {
+               if (t.IsArray) { //  == TypeManager.array_type || t.IsSubclassOf (TypeManager.array_type)) {
                        used_cache = true;
                        return TypeHandle.ArrayType.MemberCache.FindMembers (
                                mt, bf, name, FilterWithClosure_delegate, null);
@@ -1550,7 +1567,7 @@ public partial class TypeManager {
                        return true;
 
 #if MS_COMPATIBLE && GMCS_SOURCE
-               if (t.IsGenericParameter)
+               if (t.IsGenericParameter || t.IsGenericType)
                        return false;
 #endif
                return t.IsEnum;
@@ -1623,7 +1640,7 @@ public partial class TypeManager {
                if (t is TypeBuilder) {
                        TypeContainer tc = LookupTypeContainer (t);
                        if (tc.Fields != null){
-                               foreach (FieldMember f in tc.Fields){
+                               foreach (FieldBase f in tc.Fields){
                                        // Avoid using f.FieldBuilder: f.Define () may not yet have been invoked.
                                        if ((f.ModFlags & Modifiers.STATIC) != 0)
                                                continue;
@@ -1970,13 +1987,9 @@ public partial class TypeManager {
                return (PropertyBase)propertybuilder_to_property [pi];
        }
 
-       static public bool RegisterFieldBase (FieldBuilder fb, FieldBase f)
+       static public void RegisterFieldBase (FieldBuilder fb, FieldBase f)
        {
-               if (fieldbuilders_to_fields.Contains (fb))
-                       return false;
-
                fieldbuilders_to_fields.Add (fb, f);
-               return true;
        }
 
        //
@@ -1991,23 +2004,11 @@ public partial class TypeManager {
 #endif
                return (FieldBase) fieldbuilders_to_fields [fb];
        }
-       
-       static public void RegisterEvent (MyEventBuilder eb, MethodBase add, MethodBase remove)
-       {
-               if (events == null)
-                       events = new Hashtable ();
-
-               if (!events.Contains (eb)) {
-                       events.Add (eb, new Pair (add, remove));
-               }
-       }
 
        static public MethodInfo GetAddMethod (EventInfo ei)
        {
                if (ei is MyEventBuilder) {
-                       Pair pair = (Pair) events [ei];
-
-                       return (MethodInfo) pair.First;
+                       return ((MyEventBuilder)ei).GetAddMethod (true);
                }
                return ei.GetAddMethod (true);
        }
@@ -2015,36 +2016,27 @@ public partial class TypeManager {
        static public MethodInfo GetRemoveMethod (EventInfo ei)
        {
                if (ei is MyEventBuilder) {
-                       Pair pair = (Pair) events [ei];
-
-                       return (MethodInfo) pair.Second;
+                       return ((MyEventBuilder)ei).GetRemoveMethod (true);
                }
                return ei.GetRemoveMethod (true);
        }
 
-       static Hashtable priv_fields_events;
-
-       static public bool RegisterPrivateFieldOfEvent (EventInfo einfo, FieldBuilder builder)
+       static public void RegisterEventField (EventInfo einfo, EventField e)
        {
-               if (priv_fields_events == null)
-                       priv_fields_events = new Hashtable ();
-
-               if (priv_fields_events.Contains (einfo))
-                       return false;
-
-               priv_fields_events.Add (einfo, builder);
+               if (events == null)
+                       events = new Hashtable ();
 
-               return true;
+               events.Add (einfo, e);
        }
 
-       static public MemberInfo GetPrivateFieldOfEvent (EventInfo ei)
+       static public EventField GetEventField (EventInfo ei)
        {
-               if (priv_fields_events == null)
+               if (events == null)
                        return null;
-               else
-                       return (MemberInfo) priv_fields_events [ei];
+
+               return (EventField) events [ei];
        }
-               
+
        static public bool RegisterIndexer (PropertyBuilder pb, MethodBase get,
                                             MethodBase set, Type[] args)
        {
@@ -2075,7 +2067,7 @@ public partial class TypeManager {
                if (tc.Fields == null)
                        return true;
 
-               foreach (FieldMember field in tc.Fields) {
+               foreach (FieldBase field in tc.Fields) {
                        if (field.FieldBuilder == null || field.FieldBuilder.IsStatic)
                                continue;
 
@@ -2679,6 +2671,15 @@ public partial class TypeManager {
 #endif
        }
 
+       public static bool IsGenericTypeDefinition (Type type)
+       {
+#if GMCS_SOURCE
+               return type.IsGenericTypeDefinition;
+#else
+               return false;
+#endif
+       }
+
        public static bool ContainsGenericParameters (Type type)
        {
 #if GMCS_SOURCE
@@ -2688,10 +2689,34 @@ public partial class TypeManager {
 #endif
        }
 
+       public static FieldInfo GetGenericFieldDefinition (FieldInfo fi)
+       {
+#if GMCS_SOURCE
+               if (fi.DeclaringType.IsGenericTypeDefinition ||
+                   !fi.DeclaringType.IsGenericType)
+                       return fi;
+
+               Type t = fi.DeclaringType.GetGenericTypeDefinition ();
+               BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic |
+                       BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
+
+               foreach (FieldInfo f in t.GetFields (bf))
+                       if (f.MetadataToken == fi.MetadataToken)
+                               return f;
+#endif
+
+               return fi;
+       }
+
        public static bool IsEqual (Type a, Type b)
        {
-               if (a.Equals (b))
+               if (a.Equals (b)) {
+                       // MS BCL returns true even if enum types are different
+                       if (a.BaseType == TypeManager.enum_type || b.BaseType == TypeManager.enum_type)
+                               return a.FullName == b.FullName;
+
                        return true;
+               }
 
 #if GMCS_SOURCE
                if (a.IsGenericParameter && b.IsGenericParameter) {
@@ -2760,6 +2785,10 @@ public partial class TypeManager {
                BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic |
                        BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
 
+#if MS_COMPATIBLE
+               return m;
+#endif
+
                if (m is ConstructorInfo) {
                        foreach (ConstructorInfo c in t.GetConstructors (bf))
                                if (c.MetadataToken == m.MetadataToken)
@@ -2882,6 +2911,39 @@ public partial class TypeManager {
                return mb.IsGenericMethod;
 #else
                return false;
+#endif
+       }
+
+       public static bool IsNullableType (Type t)
+       {
+#if GMCS_SOURCE
+               return generic_nullable_type == DropGenericTypeArguments (t);
+#else
+               return false;
+#endif
+       }
+
+       public static bool IsNullableTypeOf (Type t, Type nullable)
+       {
+#if GMCS_SOURCE
+               if (!IsNullableType (t))
+                       return false;
+
+               return GetTypeArguments (t) [0] == nullable;
+#else
+               return false;
+#endif
+       }
+
+       public static bool IsNullableValueType (Type t)
+       {
+#if GMCS_SOURCE
+               if (!IsNullableType (t))
+                       return false;
+
+               return GetTypeArguments (t) [0].IsValueType;
+#else
+               return false;
 #endif
        }
 #endregion
@@ -2964,7 +3026,10 @@ public partial class TypeManager {
 
                                if (ma == MethodAttributes.Public)
                                        return true;
-                               
+
+                               if (ma == MethodAttributes.PrivateScope)
+                                       return false;
+
                                if (ma == MethodAttributes.Private)
                                        return private_ok ||
                                                IsPrivateAccessible (invocation_type, m.DeclaringType) ||
@@ -2989,7 +3054,10 @@ public partial class TypeManager {
                                
                                if (fa == FieldAttributes.Public)
                                        return true;
-                               
+
+                               if (fa == FieldAttributes.PrivateScope)
+                                       return false;
+
                                if (fa == FieldAttributes.Private)
                                        return private_ok ||
                                                IsPrivateAccessible (invocation_type, m.DeclaringType) ||
@@ -3233,19 +3301,79 @@ public partial class TypeManager {
                return null;
        }
 
-       // Tests whether external method is really special
-       public static bool IsSpecialMethod (MethodBase mb)
+       const BindingFlags AllMembers = BindingFlags.Public | BindingFlags.NonPublic |
+                                                                       BindingFlags.Static | BindingFlags.Instance | 
+                                                                       BindingFlags.DeclaredOnly;
+
+       // Currently is designed to work with external types only
+       public static PropertyInfo GetPropertyFromAccessor (MethodBase mb)
        {
+               if (!mb.IsSpecialName)
+                       return null;
+
                string name = mb.Name;
-               if (name.StartsWith ("get_") || name.StartsWith ("set_"))
-                       return mb.DeclaringType.GetProperty (name.Substring (4)) != null;
-                               
+               if (name.Length < 5)
+                       return null;
+
+               if (name [3] != '_')
+                       return null;
+
+               if (name.StartsWith ("get") || name.StartsWith ("set")) {
+                       MemberInfo[] pi = mb.DeclaringType.FindMembers (MemberTypes.Property, AllMembers,
+                               Type.FilterName, name.Substring (4));
+
+                       if (pi == null)
+                               return null;
+
+                       // This can happen when property is indexer (it can have same name but different parameters)
+                       foreach (PropertyInfo p in pi) {
+                               foreach (MethodInfo p_mi in p.GetAccessors (true)) {
+                                       if (p_mi == mb || TypeManager.GetParameterData (p_mi).Equals (TypeManager.GetParameterData (mb)))
+                                               return p;
+                               }
+                       }
+               }
+
+               return null;
+       }
+
+       // Currently is designed to work with external types only
+       public static MemberInfo GetEventFromAccessor (MethodBase mb)
+       {
+               if (!mb.IsSpecialName)
+                       return null;
+
+               string name = mb.Name;
+               if (name.Length < 5)
+                       return null;
+
                if (name.StartsWith ("add_"))
-                       return mb.DeclaringType.GetEvent (name.Substring (4)) != null;
+                       return mb.DeclaringType.GetEvent (name.Substring (4), AllMembers);
 
                if (name.StartsWith ("remove_"))
-                       return mb.DeclaringType.GetEvent (name.Substring (7)) != null;
+                       return mb.DeclaringType.GetEvent (name.Substring (7), AllMembers);
+
+               return null;
+       }
+
+       // Tests whether external method is really special
+       public static bool IsSpecialMethod (MethodBase mb)
+       {
+               if (!mb.IsSpecialName)
+                       return false;
+
+               IMethodData md = TypeManager.GetMethod (mb);
+               if (md != null) 
+                       return (md is AbstractPropertyEventMethod || md is Operator);
+
+               PropertyInfo pi = GetPropertyFromAccessor (mb);
+               if (pi != null)
+                       return IsValidProperty (pi);
+                               
+               if (GetEventFromAccessor (mb) != null)
+                       return true;
 
+               string name = mb.Name;
                if (name.StartsWith ("op_")){
                        foreach (string oname in Unary.oper_names) {
                                if (oname == name)
@@ -3260,6 +3388,22 @@ public partial class TypeManager {
                return false;
        }
 
+       // Tests whether imported property is valid C# property.
+       // TODO: It seems to me that we should do a lot of sanity tests before
+       // we accept property as C# property
+       static bool IsValidProperty (PropertyInfo pi)
+       {
+               MethodInfo get_method = pi.GetGetMethod (true);
+               MethodInfo set_method = pi.GetSetMethod (true);
+               if (get_method != null && set_method != null) {
+                       int g_count = get_method.GetParameters ().Length;
+                       int s_count = set_method.GetParameters ().Length;
+                       if (g_count + 1 != s_count)
+                               return false;
+               }
+               return true;
+       }
+
 #endregion
        
 }