* cs-parser.jay (GetTokenName): Mark GENERATE_COMPLETION and COMPLETE_COMPLETION...
[mono.git] / mcs / mcs / typemanager.cs
index 30a705ee960775259c59a644ea5beedc726dbd6f..055c2f5e03859d098f007ad15b23a4df4ee8acbe 100644 (file)
@@ -47,7 +47,6 @@ namespace Mono.CSharp {
        static public Type float_type;
        static public Type double_type;
        static public Type char_type;
-       static public Type char_ptr_type;
        static public Type short_type;
        static public Type decimal_type;
        static public Type bool_type;
@@ -65,7 +64,6 @@ namespace Mono.CSharp {
        static public Type ienumerator_type;
        static public Type ienumerable_type;
        static public Type idisposable_type;
-       static public Type default_member_type;
        static public Type iasyncresult_type;
        static public Type asynccallback_type;
        static public Type intptr_type;
@@ -73,47 +71,19 @@ namespace Mono.CSharp {
        static public Type runtime_field_handle_type;
        static public Type runtime_argument_handle_type;
        static public Type attribute_type;
-       static public Type attribute_usage_type;
-       static public Type decimal_constant_attribute_type;
-       static public Type dllimport_type;
-       static public Type methodimpl_attr_type;
-       static public Type marshal_as_attr_type;
-       static public Type param_array_type;
        static public Type void_ptr_type;
-       static public Type indexer_name_type;
        static public Type exception_type;
-       static public Type obsolete_attribute_type;
-       static public Type conditional_attribute_type;
-       static public Type in_attribute_type;
-       static public Type out_attribute_type;
-       static public Type extension_attribute_type;
-       static public Type default_parameter_value_attribute_type;
 
        static public Type anonymous_method_type;
-       static public Type cls_compliant_attribute_type;
        static public Type typed_reference_type;
        static public Type arg_iterator_type;
        static public Type mbr_type;
-       static public Type struct_layout_attribute_type;
-       static public Type field_offset_attribute_type;
-       static public Type security_attr_type;
-       static public Type required_attr_type;
-       static public Type guid_attr_type;
-       static public Type assembly_culture_attribute_type;
-       static public Type assembly_version_attribute_type;
-       static public Type coclass_attr_type;
-       static public Type comimport_attr_type;
        public static Type runtime_helpers_type;
-       public static Type internals_visible_attr_type;
 
        // 
        // C# 2.0
        //
-       static internal Type fixed_buffer_attr_type;
-       static internal Type default_charset_type;
-       static internal Type type_forwarder_attr_type;
        static internal Type isvolatile_type;
-       static public Type activator_type;
        static public Type generic_ilist_type;
        static public Type generic_icollection_type;
        static public Type generic_ienumerator_type;
@@ -146,43 +116,28 @@ namespace Mono.CSharp {
        //
        // These methods are called by code generated by the compiler
        //
-       static public MethodInfo string_isinterned_string;
+       static public FieldInfo string_empty;
        static public MethodInfo system_type_get_type_from_handle;
        static public MethodInfo bool_movenext_void;
        static public MethodInfo void_dispose_void;
        static public MethodInfo void_monitor_enter_object;
        static public MethodInfo void_monitor_exit_object;
        static public MethodInfo void_initializearray_array_fieldhandle;
-       static public MethodInfo int_getlength_int;
        static public MethodInfo delegate_combine_delegate_delegate;
        static public MethodInfo delegate_remove_delegate_delegate;
-       static public MethodInfo int_get_offset_to_string_data;
+       static public PropertyInfo int_get_offset_to_string_data;
        static public MethodInfo int_interlocked_compare_exchange;
        static public PropertyInfo ienumerator_getcurrent;
        public static MethodInfo methodbase_get_type_from_handle;
        public static MethodInfo methodbase_get_type_from_handle_generic;
        public static MethodInfo fieldinfo_get_field_from_handle;
        static public MethodInfo activator_create_instance;
-       
+
        //
-       // The attribute constructors.
+       // The constructors.
        //
        static public ConstructorInfo void_decimal_ctor_five_args;
        static public ConstructorInfo void_decimal_ctor_int_arg;
-       static public ConstructorInfo default_member_ctor;
-       static public ConstructorInfo decimal_constant_attribute_ctor;
-       static internal ConstructorInfo struct_layout_attribute_ctor;
-       static public ConstructorInfo field_offset_attribute_ctor;
-       public static ConstructorInfo invalid_operation_exception_ctor;
-
-       static public CustomAttributeBuilder param_array_attr;
-       static CustomAttributeBuilder compiler_generated_attr;
-
-       // C# 2.0
-       static internal ConstructorInfo fixed_buffer_attr_ctor;
-
-       // C# 3.0
-       static internal CustomAttributeBuilder extension_attribute_attr;
 
        static PtrHashtable builder_to_declspace;
 
@@ -194,12 +149,6 @@ namespace Mono.CSharp {
        // </remarks>
        static PtrHashtable builder_to_ifaces;
 
-       // <remarks>
-       //   Maps PropertyBuilder to a Type array that contains
-       //   the arguments to the indexer
-       // </remarks>
-       static Hashtable indexer_arguments;
-
        // <remarks>
        //   Maps a MethodBase to its ParameterData (either InternalParameters or ReflectionParameters)
        // <remarks>
@@ -235,7 +184,6 @@ namespace Mono.CSharp {
                builder_to_member_cache = null;
                builder_to_ifaces = null;
                builder_to_type_param = null;
-               indexer_arguments = null;
                method_params = null;
                builder_to_method = null;
                
@@ -243,7 +191,6 @@ namespace Mono.CSharp {
                events = null;
                type_hash = null;
                propertybuilder_to_property = null;
-               assembly_internals_vis_attrs = null;
 
                TypeHandle.CleanUp ();
        }
@@ -254,42 +201,41 @@ namespace Mono.CSharp {
        //
        static void InitExpressionTypes ()
        {
-               system_object_expr  = new TypeLookupExpression ("System.Object");
-               system_string_expr  = new TypeLookupExpression ("System.String");
-               system_boolean_expr = new TypeLookupExpression ("System.Boolean");
-               system_decimal_expr = new TypeLookupExpression ("System.Decimal");
-               system_single_expr  = new TypeLookupExpression ("System.Single");
-               system_double_expr  = new TypeLookupExpression ("System.Double");
-               system_sbyte_expr   = new TypeLookupExpression ("System.SByte");
-               system_byte_expr    = new TypeLookupExpression ("System.Byte");
-               system_int16_expr   = new TypeLookupExpression ("System.Int16");
-               system_uint16_expr  = new TypeLookupExpression ("System.UInt16");
-               system_int32_expr   = new TypeLookupExpression ("System.Int32");
-               system_uint32_expr  = new TypeLookupExpression ("System.UInt32");
-               system_int64_expr   = new TypeLookupExpression ("System.Int64");
-               system_uint64_expr  = new TypeLookupExpression ("System.UInt64");
-               system_char_expr    = new TypeLookupExpression ("System.Char");
-               system_void_expr    = new TypeLookupExpression ("System.Void");
-               system_valuetype_expr  = new TypeLookupExpression ("System.ValueType");
-               system_intptr_expr  = new TypeLookupExpression ("System.IntPtr");
+               system_object_expr  = new TypeLookupExpression ("System", "Object");
+               system_string_expr  = new TypeLookupExpression ("System", "String");
+               system_boolean_expr = new TypeLookupExpression ("System", "Boolean");
+               system_decimal_expr = new TypeLookupExpression ("System", "Decimal");
+               system_single_expr  = new TypeLookupExpression ("System", "Single");
+               system_double_expr  = new TypeLookupExpression ("System", "Double");
+               system_sbyte_expr   = new TypeLookupExpression ("System", "SByte");
+               system_byte_expr    = new TypeLookupExpression ("System", "Byte");
+               system_int16_expr   = new TypeLookupExpression ("System", "Int16");
+               system_uint16_expr  = new TypeLookupExpression ("System", "UInt16");
+               system_int32_expr   = new TypeLookupExpression ("System", "Int32");
+               system_uint32_expr  = new TypeLookupExpression ("System", "UInt32");
+               system_int64_expr   = new TypeLookupExpression ("System", "Int64");
+               system_uint64_expr  = new TypeLookupExpression ("System", "UInt64");
+               system_char_expr    = new TypeLookupExpression ("System", "Char");
+               system_void_expr    = new TypeLookupExpression ("System", "Void");
+               system_valuetype_expr  = new TypeLookupExpression ("System", "ValueType");
+               system_intptr_expr  = new TypeLookupExpression ("System", "IntPtr");
        }
 
        static TypeManager ()
        {
                Reset ();
-
-               InitExpressionTypes ();
        }
 
        static public void Reset ()
        {
+               InitExpressionTypes ();
+               
                builder_to_declspace = new PtrHashtable ();
                builder_to_member_cache = new PtrHashtable ();
                builder_to_method = new PtrHashtable ();
                builder_to_type_param = new PtrHashtable ();
                method_params = new PtrHashtable ();
                method_overrides = new PtrHashtable ();
-               indexer_arguments = new PtrHashtable ();
                builder_to_ifaces = new PtrHashtable ();
                
                fieldbuilders_to_fields = new Hashtable ();
@@ -297,39 +243,31 @@ namespace Mono.CSharp {
                fields = new Hashtable ();
                type_hash = new DoubleHash ();
                assembly_internals_vis_attrs = new PtrHashtable ();
+               iface_cache = new PtrHashtable ();
+               
+               closure = new Closure ();
+               FilterWithClosure_delegate = new MemberFilter (closure.Filter);
 
                // TODO: I am really bored by all this static stuff
-               string_isinterned_string =
                system_type_get_type_from_handle =
                bool_movenext_void =
                void_dispose_void =
                void_monitor_enter_object =
                void_monitor_exit_object =
                void_initializearray_array_fieldhandle =
-               int_getlength_int =
                delegate_combine_delegate_delegate =
                delegate_remove_delegate_delegate =
-               int_get_offset_to_string_data =
                int_interlocked_compare_exchange =
                methodbase_get_type_from_handle =
                methodbase_get_type_from_handle_generic =
                fieldinfo_get_field_from_handle =
                activator_create_instance = null;
 
+               int_get_offset_to_string_data =
                ienumerator_getcurrent = null;
 
                void_decimal_ctor_five_args =
-               void_decimal_ctor_int_arg =
-               default_member_ctor =
-               decimal_constant_attribute_ctor =
-               struct_layout_attribute_ctor =
-               field_offset_attribute_ctor =
-               invalid_operation_exception_ctor =
-               fixed_buffer_attr_ctor = null;
-
-               param_array_attr =
-               compiler_generated_attr =
-               extension_attribute_attr = null;
+               void_decimal_ctor_int_arg = null;
 
                isvolatile_type = null;
                        
@@ -382,7 +320,7 @@ namespace Mono.CSharp {
 
        public static MemberCache LookupMemberCache (Type t)
        {
-               if (t.Module == CodeGen.Module.Builder) {
+               if (t.Module == RootContext.ToplevelTypes.Builder) {
                        DeclSpace container = (DeclSpace)builder_to_declspace [t];
                        if (container != null)
                                return container.MemberCache;
@@ -523,24 +461,6 @@ namespace Mono.CSharp {
                return null;
        }
 
-       public static CustomAttributeBuilder GetCompilerGeneratedAttribute (Location loc)
-       {
-               if (compiler_generated_attr != null)
-                       return compiler_generated_attr;
-
-               Type t = TypeManager.CoreLookupType (
-                       "System.Runtime.CompilerServices", "CompilerGeneratedAttribute", Kind.Class, true);
-
-               // TODO: it cannot be null
-               if (t == null)
-                       return null;
-
-               compiler_generated_attr = new CustomAttributeBuilder (
-                       GetPredefinedConstructor (t, loc, Type.EmptyTypes), new object[0]);
-
-               return compiler_generated_attr;
-       }
-
        public static Type GetNestedType (Type t, string name)
        {
                object ret = null;
@@ -559,7 +479,7 @@ namespace Mono.CSharp {
        public static void LoadAllImportedTypes ()
        {
                AllClsTopLevelTypes = new Hashtable (1500);
-               foreach (Assembly a in RootNamespace.Global.Assemblies) {
+               foreach (Assembly a in GlobalRootNamespace.Instance.Assemblies) {
                        foreach (Type t in a.GetExportedTypes ()) {
                                AllClsTopLevelTypes [t.FullName.ToLower (System.Globalization.CultureInfo.InvariantCulture)] = null;
                        }
@@ -568,7 +488,7 @@ namespace Mono.CSharp {
 
        public static bool NamespaceClash (string name, Location loc)
        {
-               if (! RootNamespace.Global.IsNamespace (name))
+               if (!GlobalRootNamespace.Instance.IsNamespace (name))
                        return false;
 
                Report.Error (519, loc, String.Format ("`{0}' clashes with a predefined namespace", name));
@@ -586,44 +506,62 @@ namespace Mono.CSharp {
                if (t == typeof (ArglistParameter))
                        return "__arglist";
                        
-               if (t == typeof (AnonymousMethod))
+               if (t == typeof (AnonymousMethodBody))
                        return "anonymous method";
 
-               return CSharpName (GetFullName (t));
-    }
+               if (t == typeof (MethodGroupExpr))
+                       return "method group";
+
+               if (t == null)
+                       return "internal error";
+
+               return CSharpName (GetFullName (t), t);
+       }
 
        static readonly char [] elements = new char [] { '*', '[' };
 
-       public static string CSharpName (string name)
+       public static string CSharpName (string name, Type type)
        {
                if (name.Length > 10) {
+                       string s;
                        switch (name) {
-                       case "System.Int32": return "int";
-                       case "System.Int64": return "long";
-                       case "System.String": return "string";
-                       case "System.Boolean": return "bool";
-                       case "System.Void": return "void";
-                       case "System.Object": return "object";
-                       case "System.UInt32": return "uint";
-                       case "System.Int16": return "short";
-                       case "System.UInt16": return "ushort";
-                       case "System.UInt64": return "ulong";
-                       case "System.Single": return "float";
-                       case "System.Double": return "double";
-                       case "System.Decimal": return "decimal";
-                       case "System.Char": return "char";
-                       case "System.Byte": return "byte";
-                       case "System.SByte": return "sbyte";
+                       case "System.Int32": s = "int"; break;
+                       case "System.Int64": s = "long"; break;
+                       case "System.String": s = "string"; break;
+                       case "System.Boolean": s = "bool"; break;
+                       case "System.Void": s = "void"; break;
+                       case "System.Object": s = "object"; break;
+                       case "System.UInt32": s = "uint"; break;
+                       case "System.Int16": s = "short"; break;
+                       case "System.UInt16": s = "ushort"; break;
+                       case "System.UInt64": s = "ulong"; break;
+                       case "System.Single": s = "float"; break;
+                       case "System.Double": s = "double"; break;
+                       case "System.Decimal": s = "decimal"; break;
+                       case "System.Char": s = "char"; break;
+                       case "System.Byte": s = "byte"; break;
+                       case "System.SByte": s = "sbyte"; break;
+                       default: s = null; break;
                        }
 
+                       if (s != null) {
+                               //
+                               // Predefined names can come from mscorlib only
+                               //
+                               if (type == null || type.Module.Name == "mscorlib.dll" || !RootContext.StdLib)
+                                       return s;
+                                       
+                               return name;
+                       }
+
+                       if (name [0] == AnonymousTypeClass.ClassNamePrefix [0] && name.StartsWith (AnonymousTypeClass.ClassNamePrefix))
+                               return AnonymousTypeClass.SignatureForError;
+
                        int idx = name.IndexOfAny (elements, 10);
                        if (idx > 0)
-                               return CSharpName (name.Substring (0, idx)) + name.Substring (idx);
+                               return CSharpName (name.Substring (0, idx), type) + name.Substring (idx);
                }
 
-               if (name [0] == AnonymousTypeClass.ClassNamePrefix [0] && name.StartsWith (AnonymousTypeClass.ClassNamePrefix))
-                       return AnonymousTypeClass.SignatureForError;
-
                return name.Replace ('+', '.');
        }
 
@@ -641,19 +579,8 @@ namespace Mono.CSharp {
                }
                return sb.ToString ();
        }
-       
-       // Used for error reporting to show symbolic name instead of underlying value
-       public static string CSharpEnumValue (Type t, object value)
-       {
-               t = DropGenericTypeArguments (t);
-               Enum e = LookupDeclSpace (t) as Enum;
-               if (e == null)
-                       return System.Enum.GetName (t, value);
-
-               return e.GetDefinition (value).GetSignatureForError ();
-       }
 
-        /// <summary>
+       /// <summary>
        ///  Returns the signature of the method with full namespace classification
        /// </summary>
        static public string GetFullNameSignature (MemberInfo mi)
@@ -713,7 +640,7 @@ namespace Mono.CSharp {
        {
                if (t.IsArray) {
                        string dimension = t.Name.Substring (t.Name.LastIndexOf ('['));
-                       return GetFullName (t.GetElementType ()) + dimension;
+                       return GetFullName (GetElementType (t)) + dimension;
                }
 
                if (IsNullableType (t) && !t.IsGenericTypeDefinition) {
@@ -763,15 +690,21 @@ namespace Mono.CSharp {
                StringBuilder sig = new StringBuilder (CSharpName (mb.DeclaringType));
                sig.Append ('.');
 
-               ParameterData iparams = GetParameterData (mb);
+               AParametersCollection iparams = GetParameterData (mb);
                string parameters = iparams.GetSignatureForError ();
                int accessor_end = 0;
 
                if (!mb.IsConstructor && TypeManager.IsSpecialMethod (mb)) {
                        string op_name = Operator.GetName (mb.Name);
                        if (op_name != null) {
-                               sig.Append ("operator ");
-                               sig.Append (op_name);
+                               if (op_name == "explicit" || op_name == "implicit") {
+                                       sig.Append (op_name);
+                                       sig.Append (" operator ");
+                                       sig.Append (CSharpName (((MethodInfo)mb).ReturnType));
+                               } else {
+                                       sig.Append ("operator ");
+                                       sig.Append (op_name);
+                               }
                                sig.Append (parameters);
                                return sig.ToString ();
                        }
@@ -801,9 +734,8 @@ namespace Mono.CSharp {
                        else {
                                sig.Append (mb.Name);
 
-#if GMCS_SOURCE
-                               if (TypeManager.IsGenericMethod (mb)) {
-                                       Type[] args = mb.GetGenericArguments ();
+                               if (IsGenericMethod (mb)) {
+                                       Type[] args = GetGenericArguments (mb);
                                        sig.Append ('<');
                                        for (int i = 0; i < args.Length; i++) {
                                                if (i > 0)
@@ -812,7 +744,6 @@ namespace Mono.CSharp {
                                        }
                                        sig.Append ('>');
                                }
-#endif
                        }
 
                        sig.Append (parameters);
@@ -828,14 +759,10 @@ namespace Mono.CSharp {
 
        public static string GetMethodName (MethodInfo m)
        {
-#if GMCS_SOURCE
                if (!IsGenericMethodDefinition (m) && !IsGenericMethod (m))
                        return m.Name;
 
-               return MemberName.MakeName (m.Name, m.GetGenericArguments ().Length);
-#else
-               return m.Name;
-#endif
+               return MemberName.MakeName (m.Name, TypeManager.GetGenericArguments (m).Length);
        }
 
        static public string CSharpSignature (EventInfo ei)
@@ -849,20 +776,8 @@ namespace Mono.CSharp {
        //
        public static Type CoreLookupType (string ns_name, string name, Kind type_kind, bool required)
        {
-               Expression expr;
-               if (RootContext.StdLib) {
-                       Namespace ns = RootNamespace.Global.GetNamespace (ns_name, true);
-                       expr = ns.Lookup (RootContext.ToplevelTypes, name, Location.Null);
-               } else {
-                       if (!required)
-                               Report.DisableReporting ();
-
-                       TypeLookupExpression tle = new TypeLookupExpression (ns_name + "." + name);
-                       expr = tle.ResolveAsTypeTerminal (RootContext.ToplevelTypes, false);
-
-                       if (!required)
-                               Report.EnableReporting ();
-               }
+               Namespace ns = GlobalRootNamespace.Instance.GetNamespace (ns_name, true);
+               Expression expr = ns.Lookup (RootContext.ToplevelTypes, name, Location.Null);
 
                if (expr == null) {
                        if (required) {
@@ -877,7 +792,7 @@ namespace Mono.CSharp {
                        return t;
 
                // TODO: All predefined imported types have to have correct signature
-               if (t.Module != CodeGen.Module.Builder)
+               if (t.Module != RootContext.ToplevelTypes.Builder)
                        return t;
 
                DeclSpace ds = (DeclSpace)RootContext.ToplevelTypes.GetDefinition (t.FullName);
@@ -908,7 +823,7 @@ namespace Mono.CSharp {
                                        if (mb == null)
                                                continue;
 
-                                       ParameterData pd = TypeManager.GetParameterData (mb);
+                                       AParametersCollection pd = TypeManager.GetParameterData (mb);
                                        if (IsEqual (pd.Types, args))
                                                return member;
                                }
@@ -987,13 +902,21 @@ namespace Mono.CSharp {
                attribute_type = CoreLookupType ("System", "Attribute", Kind.Class, true);
 
                int32_type    = CoreLookupType ("System", "Int32", Kind.Struct, true);
+               system_int32_expr.Type = int32_type;
                int64_type    = CoreLookupType ("System", "Int64", Kind.Struct, true);
-               uint32_type   = CoreLookupType ("System", "UInt32", Kind.Struct, true); 
-               uint64_type   = CoreLookupType ("System", "UInt64", Kind.Struct, true); 
+               system_int64_expr.Type = int64_type;
+               uint32_type   = CoreLookupType ("System", "UInt32", Kind.Struct, true);
+               system_uint32_expr.Type = uint32_type;
+               uint64_type   = CoreLookupType ("System", "UInt64", Kind.Struct, true);
+               system_uint64_expr.Type = uint64_type;
                byte_type     = CoreLookupType ("System", "Byte", Kind.Struct, true);
+               system_byte_expr.Type = byte_type;
                sbyte_type    = CoreLookupType ("System", "SByte", Kind.Struct, true);
+               system_sbyte_expr.Type = sbyte_type;
                short_type    = CoreLookupType ("System", "Int16", Kind.Struct, true);
+               system_int16_expr.Type = short_type;
                ushort_type   = CoreLookupType ("System", "UInt16", Kind.Struct, true);
+               system_uint16_expr.Type = ushort_type;
 
                ienumerator_type     = CoreLookupType ("System.Collections", "IEnumerator", Kind.Interface, true);
                ienumerable_type     = CoreLookupType ("System.Collections", "IEnumerable", Kind.Interface, true);
@@ -1003,12 +926,19 @@ namespace Mono.CSharp {
                generic_ienumerator_type = CoreLookupType ("System.Collections.Generic", "IEnumerator`1", Kind.Interface, false);
 
                char_type     = CoreLookupType ("System", "Char", Kind.Struct, true);
+               system_char_expr.Type = char_type;
                string_type   = CoreLookupType ("System", "String", Kind.Class, true);
+               system_string_expr.Type = string_type;
                float_type    = CoreLookupType ("System", "Single", Kind.Struct, true);
+               system_single_expr.Type = float_type;
                double_type   = CoreLookupType ("System", "Double", Kind.Struct, true);
+               system_double_expr.Type = double_type;
                decimal_type  = CoreLookupType ("System", "Decimal", Kind.Struct, true);
+               system_decimal_expr.Type = decimal_type;
                bool_type     = CoreLookupType ("System", "Boolean", Kind.Struct, true);
+               system_boolean_expr.Type = bool_type;
                intptr_type = CoreLookupType ("System", "IntPtr", Kind.Struct, true);
+               system_intptr_expr.Type = intptr_type;
                uintptr_type = CoreLookupType ("System", "UIntPtr", Kind.Struct, true);
 
                multicast_delegate_type = CoreLookupType ("System", "MulticastDelegate", Kind.Class, true);
@@ -1017,14 +947,15 @@ namespace Mono.CSharp {
                enum_type       = CoreLookupType ("System", "Enum", Kind.Class, true);
                array_type      = CoreLookupType ("System", "Array", Kind.Class, true);
                void_type       = CoreLookupType ("System", "Void", Kind.Struct, true);
+               system_void_expr.Type = void_type;
                type_type       = CoreLookupType ("System", "Type", Kind.Class, true);
                exception_type = CoreLookupType ("System", "Exception", Kind.Class, true);
 
                runtime_field_handle_type = CoreLookupType ("System", "RuntimeFieldHandle", Kind.Struct, true);
                runtime_handle_type = CoreLookupType ("System", "RuntimeTypeHandle", Kind.Struct, true);
 
-               param_array_type = CoreLookupType ("System", "ParamArrayAttribute", Kind.Class, true);
-               out_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "OutAttribute", Kind.Class, true);
+               PredefinedAttributes.Get.ParamArray.Resolve (false);
+               PredefinedAttributes.Get.Out.Resolve (false);
 
                return Report.Errors == 0;
        }
@@ -1034,14 +965,19 @@ namespace Mono.CSharp {
        //
        public static void InitOptionalCoreTypes ()
        {
+               //
+               // These are only used for compare purposes
+               //
+               anonymous_method_type = typeof (AnonymousMethodBody);
+               null_type = typeof (NullLiteral);
+               
                void_ptr_type = GetPointerType (void_type);
-               char_ptr_type = GetPointerType (char_type);
 
                //
                // Initialize InternalsVisibleTo as the very first optional type. Otherwise we would populate
                // types cache with incorrect accessiblity when any of optional types is internal.
                //
-               internals_visible_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "InternalsVisibleToAttribute", Kind.Class, false);
+               PredefinedAttributes.Get.Initialize ();
 
                runtime_argument_handle_type = CoreLookupType ("System", "RuntimeArgumentHandle", Kind.Struct, false);
                asynccallback_type = CoreLookupType ("System", "AsyncCallback", Kind.Delegate, false);
@@ -1053,33 +989,12 @@ namespace Mono.CSharp {
                //
                // Optional attributes, used for error reporting only
                //
-               obsolete_attribute_type = CoreLookupType ("System", "ObsoleteAttribute", Kind.Class, false);
-               if (obsolete_attribute_type != null) {
-                       Class c = TypeManager.LookupClass (obsolete_attribute_type);
-                       if (c != null)
-                               c.DefineMembers ();
-               }
-
-               dllimport_type = CoreLookupType ("System.Runtime.InteropServices", "DllImportAttribute", Kind.Class, false);
-               methodimpl_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "MethodImplAttribute", Kind.Class, false);
-               marshal_as_attr_type = CoreLookupType ("System.Runtime.InteropServices", "MarshalAsAttribute", Kind.Class, false);
-               in_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "InAttribute", Kind.Class, false);
-               indexer_name_type = CoreLookupType ("System.Runtime.CompilerServices", "IndexerNameAttribute", Kind.Class, false);
-               conditional_attribute_type = CoreLookupType ("System.Diagnostics", "ConditionalAttribute", Kind.Class, false);
-               cls_compliant_attribute_type = CoreLookupType ("System", "CLSCompliantAttribute", Kind.Class, false);
-               security_attr_type = CoreLookupType ("System.Security.Permissions", "SecurityAttribute", Kind.Class, false);
-               required_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "RequiredAttributeAttribute", Kind.Class, false);
-               guid_attr_type = CoreLookupType ("System.Runtime.InteropServices", "GuidAttribute", Kind.Class, false);
-               assembly_culture_attribute_type = CoreLookupType ("System.Reflection", "AssemblyCultureAttribute", Kind.Class, false);
-               assembly_version_attribute_type = CoreLookupType ("System.Reflection", "AssemblyVersionAttribute", Kind.Class, false);
-               comimport_attr_type = CoreLookupType ("System.Runtime.InteropServices", "ComImportAttribute", Kind.Class, false);
-               coclass_attr_type = CoreLookupType ("System.Runtime.InteropServices", "CoClassAttribute", Kind.Class, false);
-               attribute_usage_type = CoreLookupType ("System", "AttributeUsageAttribute", Kind.Class, false);
-               default_parameter_value_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "DefaultParameterValueAttribute", Kind.Class, false);
-
-               // New in .NET 2.0
-               default_charset_type = CoreLookupType ("System.Runtime.InteropServices", "DefaultCharSetAttribute", Kind.Class, false);
-               type_forwarder_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "TypeForwardedToAttribute", Kind.Class, false);
+               //if (PredefinedAttributes.Get.Obsolete.IsDefined) {
+               //    Class c = TypeManager.LookupClass (PredefinedAttributes.Get.Obsolete.Type);
+               //    if (c != null)
+               //        c.Define ();
+               //}
+
                generic_ilist_type = CoreLookupType ("System.Collections.Generic", "IList`1", Kind.Interface, false);
                generic_icollection_type = CoreLookupType ("System.Collections.Generic", "ICollection`1", Kind.Interface, false);
                generic_ienumerable_type = CoreLookupType ("System.Collections.Generic", "IEnumerable`1", Kind.Interface, false);
@@ -1088,17 +1003,10 @@ namespace Mono.CSharp {
                //
                // Optional types which are used as types and for member lookup
                //
-               default_member_type = CoreLookupType ("System.Reflection", "DefaultMemberAttribute", Kind.Class, false);
                runtime_helpers_type = CoreLookupType ("System.Runtime.CompilerServices", "RuntimeHelpers", Kind.Class, false);
-               decimal_constant_attribute_type = CoreLookupType ("System.Runtime.CompilerServices", "DecimalConstantAttribute", Kind.Class, false);
-               struct_layout_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "StructLayoutAttribute", Kind.Class, false);
-               field_offset_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "FieldOffsetAttribute", Kind.Class, false);
-
-               // New in .NET 2.0
-               fixed_buffer_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "FixedBufferAttribute", Kind.Class, false);
 
                // New in .NET 3.5
-               extension_attribute_type = CoreLookupType ("System.Runtime.CompilerServices", "ExtensionAttribute", Kind.Class, false);
+               // Note: extension_attribute_type is already loaded
                expression_type = CoreLookupType ("System.Linq.Expressions", "Expression`1", Kind.Class, false);
 
                if (!RootContext.StdLib) {
@@ -1130,28 +1038,6 @@ namespace Mono.CSharp {
                                        TypeManager.CSharpName (system_4_type_arg));
                        }
                }
-
-               system_string_expr.Type = string_type;
-               system_boolean_expr.Type = bool_type;
-               system_decimal_expr.Type = decimal_type;
-               system_single_expr.Type = float_type;
-               system_double_expr.Type = double_type;
-               system_sbyte_expr.Type = sbyte_type;
-               system_byte_expr.Type = byte_type;
-               system_int16_expr.Type = short_type;
-               system_uint16_expr.Type = ushort_type;
-               system_int32_expr.Type = int32_type;
-               system_uint32_expr.Type = uint32_type;
-               system_int64_expr.Type = int64_type;
-               system_uint64_expr.Type = uint64_type;
-               system_char_expr.Type = char_type;
-               system_void_expr.Type = void_type;
-
-               //
-               // These are only used for compare purposes
-               //
-               anonymous_method_type = typeof (AnonymousMethod);
-               null_type = typeof (NullLiteral);
        }
 
        const BindingFlags instance_and_static = BindingFlags.Static | BindingFlags.Instance;
@@ -1271,6 +1157,17 @@ namespace Mono.CSharp {
                                        mt, bf, name, FilterWithClosure_delegate, null);
                        }
 
+                       //
+                       // We have to take care of arrays specially, because GetType on
+                       // a TypeBuilder array will return a Type, not a TypeBuilder,
+                       // and we can not call FindMembers on this type.
+                       //
+                       if (t.IsArray) {
+                               used_cache = true;
+                               return TypeHandle.ArrayType.MemberCache.FindMembers (
+                                       mt, bf, name, FilterWithClosure_delegate, null);
+                       }
+
                        if (t.IsGenericType && !t.IsGenericTypeDefinition)
                                t = t.GetGenericTypeDefinition ();
 #else
@@ -1374,12 +1271,10 @@ namespace Mono.CSharp {
 
        public static bool IsDelegateType (Type t)
        {
-#if GMCS_SOURCE
-               if (t.IsGenericParameter)
+               if (TypeManager.IsGenericParameter (t))
                        return false;
-#endif
 
-               if (t == TypeManager.delegate_type)
+               if (t == TypeManager.delegate_type || t == TypeManager.multicast_delegate_type)
                        return false;
 
                t = DropGenericTypeArguments (t);
@@ -1408,16 +1303,15 @@ namespace Mono.CSharp {
                return t == attribute_type && t.BaseType != null || IsSubclassOf (t, attribute_type);
        }
        
-       static Stack unmanaged_enclosing_types = new Stack (4);
-
        //
        // Whether a type is unmanaged.  This is used by the unsafe code (25.2)
        //
+       // mcs4: delete, DeclSpace.IsUnmanagedType is replacement
        public static bool IsUnmanagedType (Type t)
        {
-               // Avoid infloops in the case of: unsafe struct Foo { Foo *x; }
-               if (unmanaged_enclosing_types.Contains (t))
-                       return true;
+               DeclSpace ds = TypeManager.LookupDeclSpace (t);
+               if (ds != null)
+                       return ds.IsUnmanagedType ();
 
                // builtins that are not unmanaged types
                if (t == TypeManager.object_type || t == TypeManager.string_type)
@@ -1431,7 +1325,7 @@ namespace Mono.CSharp {
 
                // Someone did the work of checking if the ElementType of t is unmanaged.  Let's not repeat it.
                if (t.IsPointer)
-                       return true;
+                       return IsUnmanagedType (GetElementType (t));
 
                // Arrays are disallowed, even if we mark them with [MarshalAs(UnmanagedType.ByValArray, ...)]
                if (t.IsArray)
@@ -1440,48 +1334,28 @@ namespace Mono.CSharp {
                if (!IsValueType (t))
                        return false;
 
-#if GMCS_SOURCE
                for (Type p = t.DeclaringType; p != null; p = p.DeclaringType) {
-                       if (p.IsGenericTypeDefinition)
+                       if (IsGenericTypeDefinition (p))
                                return false;
                }
-#endif
-
-               unmanaged_enclosing_types.Push (t);
 
                bool retval = true;
-
-               if (t is TypeBuilder) {
-                       TypeContainer tc = LookupTypeContainer (t);
-                       if (tc.Fields != null){
-                               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;
-                                       if (f.MemberType == null)
-                                               continue;
-                                       if (!IsUnmanagedType (f.MemberType)){
-                                               Report.SymbolRelatedToPreviousError (f.Location, CSharpName (t) + "." + f.Name);
-                                               retval = false;
-                                       }
-                               }
-                       }
-               } else {
+               {
                        FieldInfo [] fields = t.GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                        
                        foreach (FieldInfo f in fields){
                                if (!IsUnmanagedType (f.FieldType)){
-                                       Report.SymbolRelatedToPreviousError (f);
                                        retval = false;
                                }
                        }
                }
 
-               unmanaged_enclosing_types.Pop ();
-
                return retval;
        }
-                       
+
+       //
+       // Null is considered to be a reference type
+       //                      
        public static bool IsReferenceType (Type t)
        {
                if (TypeManager.IsGenericParameter (t)) {
@@ -1492,15 +1366,25 @@ namespace Mono.CSharp {
                        return constraints.IsReferenceType;
                }
 
-               if (t == TypeManager.null_type)
-                       return false;
-
-               return !t.IsValueType;
+               return !IsStruct (t) && !IsEnumType (t);
        }                       
                
        public static bool IsValueType (Type t)
        {
-               return t.IsValueType || IsGenericParameter (t);
+               if (TypeManager.IsGenericParameter (t)) {
+                       GenericConstraints constraints = TypeManager.GetTypeParameterConstraints (t);
+                       if (constraints == null)
+                               return false;
+
+                       return constraints.IsValueType;
+               }
+
+               return IsStruct (t) || IsEnumType (t);
+       }
+
+       public static bool IsStruct (Type t)
+       {
+               return t.BaseType == value_type && t != enum_type && t.IsSealed;
        }
        
        public static bool IsInterfaceType (Type t)
@@ -1616,6 +1500,11 @@ namespace Mono.CSharp {
                return false;
        }
 
+       public static bool IsSpecialType (Type t)
+       {
+               return t == arg_iterator_type || t == typed_reference_type;
+       }
+
        //
        // Checks whether `extern_type' is friend of the output assembly
        //
@@ -1626,18 +1515,23 @@ namespace Mono.CSharp {
 
                if (assembly_internals_vis_attrs.Contains (assembly))
                        return (bool)(assembly_internals_vis_attrs [assembly]);
-                               
-               if (internals_visible_attr_type == null)
+
+               PredefinedAttribute pa = PredefinedAttributes.Get.InternalsVisibleTo;
+               // HACK: Do very early resolve of SRE type checking
+               if (pa.Type == null)
+                       pa.Resolve (true);
+
+               if (!pa.IsDefined)
                        return false;
                
-               object [] attrs = assembly.GetCustomAttributes (internals_visible_attr_type, false);
+               object [] attrs = assembly.GetCustomAttributes (pa.Type, false);
                if (attrs.Length == 0) {
                        assembly_internals_vis_attrs.Add (assembly, false);
                        return false;
                }
 
                bool is_friend = false;
-#if GMCS_SOURCE
+
                AssemblyName this_name = CodeGen.Assembly.Name;
                byte [] this_token = this_name.GetPublicKeyToken ();
                foreach (InternalsVisibleToAttribute attr in attrs) {
@@ -1646,7 +1540,11 @@ namespace Mono.CSharp {
                        
                        AssemblyName aname = null;
                        try {
+#if GMCS_SOURCE
                                aname = new AssemblyName (attr.AssemblyName);
+#else
+                               throw new NotSupportedException ();
+#endif
                        } catch (FileLoadException) {
                        } catch (ArgumentException) {
                        }
@@ -1669,12 +1567,11 @@ namespace Mono.CSharp {
                        is_friend = true;
                        break;
                }
-#endif
+
                assembly_internals_vis_attrs.Add (assembly, is_friend);
                return is_friend;
        }
 
-#if GMCS_SOURCE
        static bool CompareKeyTokens (byte [] token1, byte [] token2)
        {
                for (int i = 0; i < token1.Length; i++)
@@ -1686,12 +1583,10 @@ namespace Mono.CSharp {
 
        static void Error_FriendAccessNameNotMatching (string other_name)
        {
-               Report.Error (281, "Friend access was granted to `" + other_name + 
-                               "', but the output assembly is named `" + CodeGen.Assembly.Name.FullName +
-                               "'. Try adding a reference to `" + other_name + 
-                               "' or change the output assembly name to match it");
+               Report.Error (281,
+                       "Friend access was granted to `{0}', but the output assembly is named `{1}'. Try adding a reference to `{0}' or change the output assembly name to match it",
+                       other_name, CodeGen.Assembly.Name.FullName);
        }
-#endif
 
         //
         // Do the right thing when returning the element type of an
@@ -1721,7 +1616,7 @@ namespace Mono.CSharp {
                        return e.UnderlyingType;
 
                // TODO: cache it ?
-               FieldInfo fi = GetPredefinedField (t, Enum.UnderlyingValueField, Location.Null);
+               FieldInfo fi = GetPredefinedField (t, Enum.UnderlyingValueField, Location.Null, Type.EmptyTypes);
                if (fi == null)
                        return TypeManager.int32_type;
 
@@ -1737,23 +1632,30 @@ namespace Mono.CSharp {
        ///   for anything which is dynamic, and we need this in a number of places,
        ///   we register this information here, and use it afterwards.
        /// </remarks>
-       static public void RegisterMethod (MethodBase mb, Parameters ip)
+       static public void RegisterMethod (MethodBase mb, AParametersCollection ip)
        {
                method_params.Add (mb, ip);
        }
+
+       static public void RegisterIndexer (PropertyBuilder pb, AParametersCollection p)
+       {
+               method_params.Add (pb, p);
+       }
        
-       static public ParameterData GetParameterData (MethodBase mb)
+       static public AParametersCollection GetParameterData (MethodBase mb)
        {
-               ParameterData pd = (ParameterData)method_params [mb];
+               AParametersCollection pd = (AParametersCollection) method_params [mb];
                if (pd == null) {
 #if MS_COMPATIBLE
                        if (mb.IsGenericMethod && !mb.IsGenericMethodDefinition) {
                                MethodInfo mi = ((MethodInfo) mb).GetGenericMethodDefinition ();
                                pd = GetParameterData (mi);
+                               /*
                                if (mi.IsGenericMethod)
                                        pd = pd.InflateTypes (mi.GetGenericArguments (), mb.GetGenericArguments ());
                                else
                                        pd = pd.InflateTypes (mi.DeclaringType.GetGenericArguments (), mb.GetGenericArguments ());
+                               */
                                method_params.Add (mb, pd);
                                return pd;
                        }
@@ -1762,14 +1664,41 @@ namespace Mono.CSharp {
                                throw new InternalErrorException ("Parameters are not registered for method `{0}'",
                                        TypeManager.CSharpName (mb.DeclaringType) + "." + mb.Name);
                        }
+
+                       pd = ParametersImported.Create (mb);
+#else
+                       MethodBase generic = TypeManager.DropGenericMethodArguments (mb);
+                       if (generic != mb) {
+                               pd = TypeManager.GetParameterData (generic);
+                               pd = ParametersImported.Create (pd, mb);
+                       } else {
+                               pd = ParametersImported.Create (mb);
+                       }
 #endif
-                       pd = new ReflectionParameters (mb);
                        method_params.Add (mb, pd);
                }
                return pd;
        }
 
-       public static ParameterData GetDelegateParameters (Type t)
+       public static AParametersCollection GetParameterData (PropertyInfo pi)
+       {
+               AParametersCollection pd = (AParametersCollection)method_params [pi];
+               if (pd == null) {
+                       if (pi is PropertyBuilder)
+                               return ParametersCompiled.EmptyReadOnlyParameters;
+
+                       ParameterInfo [] p = pi.GetIndexParameters ();
+                       if (p == null)
+                               return ParametersCompiled.EmptyReadOnlyParameters;
+
+                       pd = ParametersImported.Create (p, null);
+                       method_params.Add (pi, pd);
+               }
+
+               return pd;
+       }
+
+       public static AParametersCollection GetDelegateParameters (Type t)
        {
                Delegate d = builder_to_declspace [t] as Delegate;
                if (d != null)
@@ -1803,38 +1732,6 @@ namespace Mono.CSharp {
                return (MethodBase) method_overrides [m];
        }
 
-       /// <summary>
-       ///    Returns the argument types for an indexer based on its PropertyInfo
-       ///
-       ///    For dynamic indexers, we use the compiler provided types, for
-       ///    indexers from existing assemblies we load them from GetParameters,
-       ///    and insert them into the cache
-       /// </summary>
-       static public Type [] GetArgumentTypes (PropertyInfo indexer)
-       {
-               if (indexer_arguments.Contains (indexer))
-                       return (Type []) indexer_arguments [indexer];
-               else if (indexer is PropertyBuilder)
-                       // If we're a PropertyBuilder and not in the
-                       // `indexer_arguments' hash, then we're a property and
-                       // not an indexer.
-                       return Type.EmptyTypes;
-               else {
-                       ParameterInfo [] pi = indexer.GetIndexParameters ();
-                       // Property, not an indexer.
-                       if (pi == null)
-                               return Type.EmptyTypes;
-                       int c = pi.Length;
-                       Type [] types = new Type [c];
-                       
-                       for (int i = 0; i < c; i++)
-                               types [i] = pi [i].ParameterType;
-
-                       indexer_arguments.Add (indexer, types);
-                       return types;
-               }
-       }
-       
        public static void RegisterConstant (FieldInfo fb, IConstant ic)
        {
                fields.Add (fb, ic);
@@ -1908,14 +1805,6 @@ namespace Mono.CSharp {
                return (EventField) events [ei];
        }
 
-       static public bool RegisterIndexer (PropertyBuilder pb, MethodBase get,
-                                            MethodBase set, Type[] args)
-       {
-               indexer_arguments.Add (pb, args);
-
-               return true;
-       }
-
        public static bool CheckStructCycles (TypeContainer tc, Hashtable seen)
        {
                Hashtable hash = new Hashtable ();
@@ -2029,7 +1918,7 @@ namespace Mono.CSharp {
                return ret;
        }
                
-       static PtrHashtable iface_cache = new PtrHashtable ();
+       static PtrHashtable iface_cache;
                
        /// <summary>
        ///   This function returns the interfaces in the type `t'.  Works with
@@ -2080,7 +1969,6 @@ namespace Mono.CSharp {
 
                        iface_cache [t] = result;
                        return result;
-#if GMCS_SOURCE
                } else if (t is GenericTypeParameterBuilder){
                        Type[] type_ifaces = (Type []) builder_to_ifaces [t];
                        if (type_ifaces == null || type_ifaces.Length == 0)
@@ -2088,7 +1976,6 @@ namespace Mono.CSharp {
 
                        iface_cache [t] = type_ifaces;
                        return type_ifaces;
-#endif
                } else {
                        Type[] ifaces = t.GetInterfaces ();
                        iface_cache [t] = ifaces;
@@ -2124,7 +2011,7 @@ namespace Mono.CSharp {
 
                        if (interfaces != null){
                                foreach (Type i in interfaces){
-                                       if (i == iface)
+                                       if (i == iface || IsVariantOf (i, iface))
                                                return true;
                                }
                        }
@@ -2135,6 +2022,39 @@ namespace Mono.CSharp {
                return false;
        }
 
+       public static bool IsVariantOf (Type type1, Type type2)
+       {
+#if GMCS_SOURCE
+               if (type1.IsGenericType && type2.IsGenericType) {
+                       Type generic_target_type = type2.GetGenericTypeDefinition ();
+                       if (type1.GetGenericTypeDefinition () == generic_target_type) {
+                               Type [] t1 = type1.GetGenericArguments ();
+                               Type [] t2 = type2.GetGenericArguments ();
+                               int i = 0;
+                               foreach (Type t in generic_target_type.GetGenericArguments ()) {
+                                       if ((t.GenericParameterAttributes & GenericParameterAttributes.VarianceMask) != 0) {
+                                               // FIXME this is not right
+                                               if (IsValueType (t1[i]) || IsValueType (t2[i])) {
+                                                       return false;
+                                               }
+                                               if ((t.GenericParameterAttributes & GenericParameterAttributes.Covariant) != 0 && !t2[i].IsAssignableFrom (t1[i])) {
+                                                       return false;
+                                               }
+                                               if ((t.GenericParameterAttributes & GenericParameterAttributes.Contravariant) != 0 && !t1[i].IsAssignableFrom (t2[i])) {
+                                                       return false;
+                                               }
+                                       } else if (t1[i] != t2[i]) {
+                                               return false;
+                                       }
+                                       i++;
+                               }
+                               return true;
+                       }
+               }
+#endif
+               return false;
+       }
+
        static NumberFormatInfo nf_provider = CultureInfo.CurrentCulture.NumberFormat;
 
        // This is a custom version of Convert.ChangeType() which works
@@ -2296,6 +2216,10 @@ namespace Mono.CSharp {
                if (IsUnmanagedType (t))
                        return true;
 
+               while (t.IsPointer)
+                       t = GetElementType (t);
+
+               Report.SymbolRelatedToPreviousError (t);
                Report.Error (208, loc, "Cannot take the address of, get the size of, or declare a pointer to a managed type `{0}'",
                        CSharpName (t));
 
@@ -2317,12 +2241,15 @@ namespace Mono.CSharp {
                        TypeContainer tc = t.IsInterface ? LookupInterface (t) : LookupTypeContainer (t);
                        return tc == null ? TypeContainer.DefaultIndexerName : tc.IndexerName;
                }
-               
-               System.Attribute attr = System.Attribute.GetCustomAttribute (
-                       t, TypeManager.default_member_type);
-               if (attr != null){
-                       DefaultMemberAttribute dma = (DefaultMemberAttribute) attr;
-                       return dma.MemberName;
+
+               PredefinedAttribute pa = PredefinedAttributes.Get.DefaultMember;
+               if (pa.IsDefined) {
+                       System.Attribute attr = System.Attribute.GetCustomAttribute (
+                               t, pa.Type);
+                       if (attr != null) {
+                               DefaultMemberAttribute dma = (DefaultMemberAttribute) attr;
+                               return dma.MemberName;
+                       }
                }
 
                return TypeContainer.DefaultIndexerName;
@@ -2383,19 +2310,18 @@ namespace Mono.CSharp {
                        if (a.GetArrayRank () != b.GetArrayRank ())
                                return false;
 
-                       return IsSignatureEqual (a.GetElementType (), b.GetElementType ());
+                       return IsSignatureEqual (GetElementType (a), GetElementType (b));
                }
 
                if (a.IsByRef && b.IsByRef)
-                       return IsSignatureEqual (a.GetElementType (), b.GetElementType ());
+                       return IsSignatureEqual (GetElementType (a), GetElementType (b));
 
-#if GMCS_SOURCE
-               if (a.IsGenericType && b.IsGenericType) {
-                       if (a.GetGenericTypeDefinition () != b.GetGenericTypeDefinition ())
+               if (IsGenericType (a) && IsGenericType (b)) {
+                       if (DropGenericTypeArguments (a) != DropGenericTypeArguments (b))
                                return false;
 
-                       Type[] aargs = a.GetGenericArguments ();
-                       Type[] bargs = b.GetGenericArguments ();
+                       Type[] aargs = GetTypeArguments (a);
+                       Type[] bargs = GetTypeArguments (b);
 
                        if (aargs.Length != bargs.Length)
                                return false;
@@ -2407,7 +2333,6 @@ namespace Mono.CSharp {
 
                        return true;
                }
-#endif
 
                return false;
        }
@@ -2415,11 +2340,14 @@ namespace Mono.CSharp {
        //
        // Returns whether the array of memberinfos contains the given method
        //
-       public static bool ArrayContainsMethod (MemberInfo [] array, MethodBase new_method)
+       public static bool ArrayContainsMethod (MemberInfo [] array, MethodBase new_method, bool ignoreDeclType)
        {
                Type [] new_args = TypeManager.GetParameterData (new_method).Types;
                
                foreach (MethodBase method in array) {
+                       if (!ignoreDeclType && method.DeclaringType != new_method.DeclaringType)
+                               continue;
+               
                        if (method.Name != new_method.Name)
                                continue;
 
@@ -2473,7 +2401,7 @@ namespace Mono.CSharp {
                foreach (MemberInfo mi in new_members){
                        MethodBase new_method = (MethodBase) mi;
                        
-                       if (!ArrayContainsMethod (target_array, new_method))
+                       if (!ArrayContainsMethod (target_array, new_method, true))
                                target_list.Add (new_method);
                }
                return target_list;
@@ -2570,30 +2498,37 @@ namespace Mono.CSharp {
                        if (a.BaseType == TypeManager.enum_type || b.BaseType == TypeManager.enum_type)
                                return a.FullName == b.FullName;
 
+                       // Some types are never equal
+                       if (a == TypeManager.null_type || a == TypeManager.anonymous_method_type)
+                               return false;
+
                        return true;
                }
 
-#if GMCS_SOURCE
-               if (a.IsGenericParameter && b.IsGenericParameter) {
+               if (IsGenericParameter (a) && IsGenericParameter (b)) {
                        // TODO: needs more testing before cleaning up
                        //if (a.DeclaringMethod != b.DeclaringMethod &&
                        //    (a.DeclaringMethod == null || b.DeclaringMethod == null))
                        //      return false;
+#if GMCS_SOURCE
                        return a.GenericParameterPosition == b.GenericParameterPosition;
+#else
+                       throw new NotSupportedException ();
+#endif
                }
 
                if (a.IsArray && b.IsArray) {
                        if (a.GetArrayRank () != b.GetArrayRank ())
                                return false;
-                       return IsEqual (a.GetElementType (), b.GetElementType ());
+                       return IsEqual (GetElementType (a), GetElementType (b));
                }
 
                if (a.IsByRef && b.IsByRef)
                        return IsEqual (a.GetElementType (), b.GetElementType ());
 
-               if (a.IsGenericType && b.IsGenericType) {
-                       Type adef = a.GetGenericTypeDefinition ();
-                       Type bdef = b.GetGenericTypeDefinition ();
+               if (IsGenericType (a) && IsGenericType (b)) {
+                       Type adef = DropGenericTypeArguments (a);
+                       Type bdef = DropGenericTypeArguments (b);
 
                        if (adef != bdef)
                                return false;
@@ -2601,8 +2536,8 @@ namespace Mono.CSharp {
                        if (adef.IsEnum && bdef.IsEnum)
                                return true;
 
-                       Type[] aargs = a.GetGenericArguments ();
-                       Type[] bargs = b.GetGenericArguments ();
+                       Type[] aargs = GetTypeArguments (a);
+                       Type[] bargs = GetTypeArguments (b);
 
                        if (aargs.Length != bargs.Length)
                                return false;
@@ -2614,17 +2549,23 @@ namespace Mono.CSharp {
 
                        return true;
                }
-#endif
 
                return false;
        }
 
        public static bool IsEqual (Type[] a, Type[] b)
        {
-               if (a.Length != b.Length)
+               if (a == null || b == null || a.Length != b.Length)
                        return false;
 
                for (int i = 0; i < a.Length; ++i) {
+                       if (a [i] == null || b [i] == null) {
+                               if (a [i] == b [i])
+                                       continue;
+
+                               return false;
+                       }
+               
                        if (!IsEqual (a [i], b [i]))
                                return false;
                }
@@ -2804,30 +2745,6 @@ namespace Mono.CSharp {
                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
@@ -2968,8 +2885,8 @@ namespace Mono.CSharp {
                }
        }
 
-       static Closure closure = new Closure ();
-       static MemberFilter FilterWithClosure_delegate = new MemberFilter (closure.Filter);
+       static Closure closure;
+       static MemberFilter FilterWithClosure_delegate;
 
        //
        // Looks up a member called `name' in the `queried_type'.  This lookup
@@ -3268,12 +3185,37 @@ namespace Mono.CSharp {
        {
                MethodInfo get_method = pi.GetGetMethod (true);
                MethodInfo set_method = pi.GetSetMethod (true);
+               int g_count = 0;
+               int s_count = 0;
                if (get_method != null && set_method != null) {
-                       int g_count = get_method.GetParameters ().Length;
-                       int s_count = set_method.GetParameters ().Length;
+                       g_count = get_method.GetParameters ().Length;
+                       s_count = set_method.GetParameters ().Length;
                        if (g_count + 1 != s_count)
                                return false;
+               } else if (get_method != null) {
+                       g_count = get_method.GetParameters ().Length;
+               } else if (set_method != null) {
+                       s_count = set_method.GetParameters ().Length;
                }
+
+               //
+               // DefaultMemberName and indexer name has to match to identify valid C# indexer
+               //
+               PredefinedAttribute pa = PredefinedAttributes.Get.DefaultMember;
+               if ((s_count > 1 || g_count > 0) && pa.IsDefined) {
+                       object[] o = pi.DeclaringType.GetCustomAttributes (pa.Type, false);
+                       if (o.Length == 0)
+                               return false;
+                       
+                       DefaultMemberAttribute dma = (DefaultMemberAttribute) o [0];
+                       if (dma.MemberName != pi.Name)
+                               return false;
+                       if (get_method != null && "get_" + dma.MemberName != get_method.Name)
+                               return false;
+                       if (set_method != null && "set_" + dma.MemberName != set_method.Name)
+                               return false;
+               }
+
                return true;
        }
 
@@ -3408,11 +3350,12 @@ public sealed class TypeHandle : IMemberContainer {
        {
                MemberInfo [] members;
 
-#if GMCS_SOURCE
                if (type is GenericTypeParameterBuilder)
                        return MemberList.Empty;
-#endif
 
+#if MS_COMPATIBLE
+               type = TypeManager.DropGenericTypeArguments (type);
+#endif
                if (mt == MemberTypes.Event)
                        members = type.GetEvents (bf | BindingFlags.DeclaredOnly);
                else