mcs/error recovery: handle errors caused by closing braces after a statement expressi...
[mono.git] / mcs / mcs / typemanager.cs
index 767e6d5f08ee733ab213560485011cac107bb76e..b6243c8cea2eac0ad8d6238dc9821b603470ccaf 100644 (file)
 //
 // Author: Miguel de Icaza (miguel@gnu.org)
 //         Ravi Pratap     (ravi@ximian.com)
-//         Marek Safar     (marek.safar@seznam.cz)
+//         Marek Safar     (marek.safar@gmail.com)
 //
 // Dual licensed under the terms of the MIT X11 or GNU GPL
 //
 // Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
-// Copyright 2003-2008 Novell, Inc.
+// Copyright 2003-2011 Novell, Inc.
+// Copyright 2011 Xamarin Inc
 //
 
 using System;
 using System.Globalization;
 using System.Collections.Generic;
 using System.Text;
+using System.IO;
 
 namespace Mono.CSharp
 {
        //
-       // All compiler build-in types (they have to exist otherwise the compile will not work)
+       // All compiler built-in types (they have to exist otherwise the compiler will not work)
        //
-       public class BuildinTypes
-       {
-               public readonly BuildinTypeSpec Object;
-               public readonly BuildinTypeSpec ValueType;
-               public readonly BuildinTypeSpec Attribute;
-
-               public readonly BuildinTypeSpec Int;
-               public readonly BuildinTypeSpec UInt;
-               public readonly BuildinTypeSpec Long;
-               public readonly BuildinTypeSpec ULong;
-               public readonly BuildinTypeSpec Float;
-               public readonly BuildinTypeSpec Double;
-               public readonly BuildinTypeSpec Char;
-               public readonly BuildinTypeSpec Short;
-               public readonly BuildinTypeSpec Decimal;
-               public readonly BuildinTypeSpec Bool;
-               public readonly BuildinTypeSpec SByte;
-               public readonly BuildinTypeSpec Byte;
-               public readonly BuildinTypeSpec UShort;
-               public readonly BuildinTypeSpec String;
-
-               public readonly BuildinTypeSpec Enum;
-               public readonly BuildinTypeSpec Delegate;
-               public readonly BuildinTypeSpec MulticastDelegate;
-               public readonly BuildinTypeSpec Void;
-               public readonly BuildinTypeSpec Array;
-               public readonly BuildinTypeSpec Type;
-               public readonly BuildinTypeSpec IEnumerator;
-               public readonly BuildinTypeSpec IEnumerable;
-               public readonly BuildinTypeSpec IDisposable;
-               public readonly BuildinTypeSpec IntPtr;
-               public readonly BuildinTypeSpec UIntPtr;
-               public readonly BuildinTypeSpec RuntimeFieldHandle;
-               public readonly BuildinTypeSpec RuntimeTypeHandle;
-               public readonly BuildinTypeSpec Exception;
-
-               readonly BuildinTypeSpec[] types;
-
-               public BuildinTypes ()
+       public class BuiltinTypes
+       {
+               public readonly BuiltinTypeSpec Object;
+               public readonly BuiltinTypeSpec ValueType;
+               public readonly BuiltinTypeSpec Attribute;
+
+               public readonly BuiltinTypeSpec Int;
+               public readonly BuiltinTypeSpec UInt;
+               public readonly BuiltinTypeSpec Long;
+               public readonly BuiltinTypeSpec ULong;
+               public readonly BuiltinTypeSpec Float;
+               public readonly BuiltinTypeSpec Double;
+               public readonly BuiltinTypeSpec Char;
+               public readonly BuiltinTypeSpec Short;
+               public readonly BuiltinTypeSpec Decimal;
+               public readonly BuiltinTypeSpec Bool;
+               public readonly BuiltinTypeSpec SByte;
+               public readonly BuiltinTypeSpec Byte;
+               public readonly BuiltinTypeSpec UShort;
+               public readonly BuiltinTypeSpec String;
+
+               public readonly BuiltinTypeSpec Enum;
+               public readonly BuiltinTypeSpec Delegate;
+               public readonly BuiltinTypeSpec MulticastDelegate;
+               public readonly BuiltinTypeSpec Void;
+               public readonly BuiltinTypeSpec Array;
+               public readonly BuiltinTypeSpec Type;
+               public readonly BuiltinTypeSpec IEnumerator;
+               public readonly BuiltinTypeSpec IEnumerable;
+               public readonly BuiltinTypeSpec IDisposable;
+               public readonly BuiltinTypeSpec IntPtr;
+               public readonly BuiltinTypeSpec UIntPtr;
+               public readonly BuiltinTypeSpec RuntimeFieldHandle;
+               public readonly BuiltinTypeSpec RuntimeTypeHandle;
+               public readonly BuiltinTypeSpec Exception;
+
+               //
+               // These are internal buil-in types which depend on other
+               // build-in type (mostly object)
+               //
+               public readonly BuiltinTypeSpec Dynamic;
+
+               // Predefined operators tables
+               public readonly Binary.PredefinedOperator[] OperatorsBinaryStandard;
+               public readonly Binary.PredefinedOperator[] OperatorsBinaryEquality;
+               public readonly Binary.PredefinedOperator[] OperatorsBinaryUnsafe;
+               public readonly TypeSpec[][] OperatorsUnary;
+               public readonly TypeSpec[] OperatorsUnaryMutator;
+
+               public readonly TypeSpec[] BinaryPromotionsTypes;
+               public readonly TypeSpec[] SwitchUserTypes;
+
+               readonly BuiltinTypeSpec[] types;
+
+               public BuiltinTypes ()
                {
-                       Object = new BuildinTypeSpec (MemberKind.Class, "System", "Object", BuildinTypeSpec.Type.Object);
-                       ValueType = new BuildinTypeSpec (MemberKind.Class, "System", "ValueType", BuildinTypeSpec.Type.ValueType);
-                       Attribute = new BuildinTypeSpec (MemberKind.Class, "System", "Attribute", BuildinTypeSpec.Type.Attribute);
-
-                       Int = new BuildinTypeSpec (MemberKind.Struct, "System", "Int32", BuildinTypeSpec.Type.Int);
-                       Long = new BuildinTypeSpec (MemberKind.Struct, "System", "Int64", BuildinTypeSpec.Type.Long);
-                       UInt = new BuildinTypeSpec (MemberKind.Struct, "System", "UInt32", BuildinTypeSpec.Type.UInt);
-                       ULong = new BuildinTypeSpec (MemberKind.Struct, "System", "UInt64", BuildinTypeSpec.Type.ULong);
-                       Byte = new BuildinTypeSpec (MemberKind.Struct, "System", "Byte", BuildinTypeSpec.Type.Byte);
-                       SByte = new BuildinTypeSpec (MemberKind.Struct, "System", "SByte", BuildinTypeSpec.Type.SByte);
-                       Short = new BuildinTypeSpec (MemberKind.Struct, "System", "Int16", BuildinTypeSpec.Type.Short);
-                       UShort = new BuildinTypeSpec (MemberKind.Struct, "System", "UInt16", BuildinTypeSpec.Type.UShort);
-
-                       IEnumerator = new BuildinTypeSpec (MemberKind.Interface, "System.Collections", "IEnumerator", BuildinTypeSpec.Type.IEnumerator);
-                       IEnumerable = new BuildinTypeSpec (MemberKind.Interface, "System.Collections", "IEnumerable", BuildinTypeSpec.Type.IEnumerable);
-                       IDisposable = new BuildinTypeSpec (MemberKind.Interface, "System", "IDisposable", BuildinTypeSpec.Type.IDisposable);
-
-                       Char = new BuildinTypeSpec (MemberKind.Struct, "System", "Char", BuildinTypeSpec.Type.Char);
-                       String = new BuildinTypeSpec (MemberKind.Class, "System", "String", BuildinTypeSpec.Type.String);
-                       Float = new BuildinTypeSpec (MemberKind.Struct, "System", "Single", BuildinTypeSpec.Type.Float);
-                       Double = new BuildinTypeSpec (MemberKind.Struct, "System", "Double", BuildinTypeSpec.Type.Double);
-                       Decimal = new BuildinTypeSpec (MemberKind.Struct, "System", "Decimal", BuildinTypeSpec.Type.Decimal);
-                       Bool = new BuildinTypeSpec (MemberKind.Struct, "System", "Boolean", BuildinTypeSpec.Type.Bool);
-                       IntPtr = new BuildinTypeSpec (MemberKind.Struct, "System", "IntPtr", BuildinTypeSpec.Type.IntPtr);
-                       UIntPtr = new BuildinTypeSpec (MemberKind.Struct, "System", "UIntPtr", BuildinTypeSpec.Type.UIntPtr);
-
-                       MulticastDelegate = new BuildinTypeSpec (MemberKind.Class, "System", "MulticastDelegate", BuildinTypeSpec.Type.MulticastDelegate);
-                       Delegate = new BuildinTypeSpec (MemberKind.Class, "System", "Delegate", BuildinTypeSpec.Type.Delegate);
-                       Enum = new BuildinTypeSpec (MemberKind.Class, "System", "Enum", BuildinTypeSpec.Type.Enum);
-                       Array = new BuildinTypeSpec (MemberKind.Class, "System", "Array", BuildinTypeSpec.Type.Array);
-                       Void = new BuildinTypeSpec (MemberKind.Struct, "System", "Void", BuildinTypeSpec.Type.Void);
-                       Type = new BuildinTypeSpec (MemberKind.Class, "System", "Type", BuildinTypeSpec.Type.Type);
-                       Exception = new BuildinTypeSpec (MemberKind.Class, "System", "Exception", BuildinTypeSpec.Type.Exception);
-                       RuntimeFieldHandle = new BuildinTypeSpec (MemberKind.Struct, "System", "RuntimeFieldHandle", BuildinTypeSpec.Type.RuntimeFieldHandle);
-                       RuntimeTypeHandle = new BuildinTypeSpec (MemberKind.Struct, "System", "RuntimeTypeHandle", BuildinTypeSpec.Type.RuntimeTypeHandle);
-
-                       types = new BuildinTypeSpec[] {
+                       Object = new BuiltinTypeSpec (MemberKind.Class, "System", "Object", BuiltinTypeSpec.Type.Object);
+                       ValueType = new BuiltinTypeSpec (MemberKind.Class, "System", "ValueType", BuiltinTypeSpec.Type.ValueType);
+                       Attribute = new BuiltinTypeSpec (MemberKind.Class, "System", "Attribute", BuiltinTypeSpec.Type.Attribute);
+
+                       Int = new BuiltinTypeSpec (MemberKind.Struct, "System", "Int32", BuiltinTypeSpec.Type.Int);
+                       Long = new BuiltinTypeSpec (MemberKind.Struct, "System", "Int64", BuiltinTypeSpec.Type.Long);
+                       UInt = new BuiltinTypeSpec (MemberKind.Struct, "System", "UInt32", BuiltinTypeSpec.Type.UInt);
+                       ULong = new BuiltinTypeSpec (MemberKind.Struct, "System", "UInt64", BuiltinTypeSpec.Type.ULong);
+                       Byte = new BuiltinTypeSpec (MemberKind.Struct, "System", "Byte", BuiltinTypeSpec.Type.Byte);
+                       SByte = new BuiltinTypeSpec (MemberKind.Struct, "System", "SByte", BuiltinTypeSpec.Type.SByte);
+                       Short = new BuiltinTypeSpec (MemberKind.Struct, "System", "Int16", BuiltinTypeSpec.Type.Short);
+                       UShort = new BuiltinTypeSpec (MemberKind.Struct, "System", "UInt16", BuiltinTypeSpec.Type.UShort);
+
+                       IEnumerator = new BuiltinTypeSpec (MemberKind.Interface, "System.Collections", "IEnumerator", BuiltinTypeSpec.Type.IEnumerator);
+                       IEnumerable = new BuiltinTypeSpec (MemberKind.Interface, "System.Collections", "IEnumerable", BuiltinTypeSpec.Type.IEnumerable);
+                       IDisposable = new BuiltinTypeSpec (MemberKind.Interface, "System", "IDisposable", BuiltinTypeSpec.Type.IDisposable);
+
+                       Char = new BuiltinTypeSpec (MemberKind.Struct, "System", "Char", BuiltinTypeSpec.Type.Char);
+                       String = new BuiltinTypeSpec (MemberKind.Class, "System", "String", BuiltinTypeSpec.Type.String);
+                       Float = new BuiltinTypeSpec (MemberKind.Struct, "System", "Single", BuiltinTypeSpec.Type.Float);
+                       Double = new BuiltinTypeSpec (MemberKind.Struct, "System", "Double", BuiltinTypeSpec.Type.Double);
+                       Decimal = new BuiltinTypeSpec (MemberKind.Struct, "System", "Decimal", BuiltinTypeSpec.Type.Decimal);
+                       Bool = new BuiltinTypeSpec (MemberKind.Struct, "System", "Boolean", BuiltinTypeSpec.Type.Bool);
+                       IntPtr = new BuiltinTypeSpec (MemberKind.Struct, "System", "IntPtr", BuiltinTypeSpec.Type.IntPtr);
+                       UIntPtr = new BuiltinTypeSpec (MemberKind.Struct, "System", "UIntPtr", BuiltinTypeSpec.Type.UIntPtr);
+
+                       MulticastDelegate = new BuiltinTypeSpec (MemberKind.Class, "System", "MulticastDelegate", BuiltinTypeSpec.Type.MulticastDelegate);
+                       Delegate = new BuiltinTypeSpec (MemberKind.Class, "System", "Delegate", BuiltinTypeSpec.Type.Delegate);
+                       Enum = new BuiltinTypeSpec (MemberKind.Class, "System", "Enum", BuiltinTypeSpec.Type.Enum);
+                       Array = new BuiltinTypeSpec (MemberKind.Class, "System", "Array", BuiltinTypeSpec.Type.Array);
+                       Void = new BuiltinTypeSpec (MemberKind.Void, "System", "Void", BuiltinTypeSpec.Type.Other);
+                       Type = new BuiltinTypeSpec (MemberKind.Class, "System", "Type", BuiltinTypeSpec.Type.Type);
+                       Exception = new BuiltinTypeSpec (MemberKind.Class, "System", "Exception", BuiltinTypeSpec.Type.Exception);
+                       RuntimeFieldHandle = new BuiltinTypeSpec (MemberKind.Struct, "System", "RuntimeFieldHandle", BuiltinTypeSpec.Type.Other);
+                       RuntimeTypeHandle = new BuiltinTypeSpec (MemberKind.Struct, "System", "RuntimeTypeHandle", BuiltinTypeSpec.Type.Other);
+
+                       // TODO: Maybe I should promote it to different kind for faster compares
+                       Dynamic = new BuiltinTypeSpec ("dynamic", BuiltinTypeSpec.Type.Dynamic);
+
+                       OperatorsBinaryStandard = Binary.CreateStandardOperatorsTable (this);
+                       OperatorsBinaryEquality = Binary.CreateEqualityOperatorsTable (this);
+                       OperatorsBinaryUnsafe = Binary.CreatePointerOperatorsTable (this);
+                       OperatorsUnary = Unary.CreatePredefinedOperatorsTable (this);
+                       OperatorsUnaryMutator = UnaryMutator.CreatePredefinedOperatorsTable (this);
+
+                       BinaryPromotionsTypes = ConstantFold.CreateBinaryPromotionsTypes (this);
+                       SwitchUserTypes = Switch.CreateSwitchUserTypes (this);
+
+                       types = new BuiltinTypeSpec[] {
                                Object, ValueType, Attribute,
                                Int, UInt, Long, ULong, Float, Double, Char, Short, Decimal, Bool, SByte, Byte, UShort, String,
                                Enum, Delegate, MulticastDelegate, Void, Array, Type, IEnumerator, IEnumerable, IDisposable,
                                IntPtr, UIntPtr, RuntimeFieldHandle, RuntimeTypeHandle, Exception };
-
-                       // Deal with obsolete static types
-                       // TODO: remove
-                       TypeManager.object_type = Object;
-                       TypeManager.value_type = ValueType;
-                       TypeManager.string_type = String;
-                       TypeManager.int32_type = Int;
-                       TypeManager.uint32_type = UInt;
-                       TypeManager.int64_type = Long;
-                       TypeManager.uint64_type = ULong;
-                       TypeManager.float_type = Float;
-                       TypeManager.double_type = Double;
-                       TypeManager.char_type = Char;
-                       TypeManager.short_type = Short;
-                       TypeManager.decimal_type = Decimal;
-                       TypeManager.bool_type = Bool;
-                       TypeManager.sbyte_type = SByte;
-                       TypeManager.byte_type = Byte;
-                       TypeManager.ushort_type = UShort;
-                       TypeManager.enum_type = Enum;
-                       TypeManager.delegate_type = Delegate;
-                       TypeManager.multicast_delegate_type = MulticastDelegate; ;
-                       TypeManager.void_type = Void;
-                       TypeManager.array_type = Array; ;
-                       TypeManager.runtime_handle_type = RuntimeTypeHandle;
-                       TypeManager.type_type = Type;
-                       TypeManager.ienumerator_type = IEnumerator;
-                       TypeManager.ienumerable_type = IEnumerable;
-                       TypeManager.idisposable_type = IDisposable;
-                       TypeManager.intptr_type = IntPtr;
-                       TypeManager.uintptr_type = UIntPtr;
-                       TypeManager.runtime_field_handle_type = RuntimeFieldHandle;
-                       TypeManager.attribute_type = Attribute;
-                       TypeManager.exception_type = Exception;
                }
 
-               #region Properties
-
-               public BuildinTypeSpec[] Types {
+               public BuiltinTypeSpec[] AllTypes {
                        get {
                                return types;
                        }
                }
 
-               #endregion
+               public bool CheckDefinitions (ModuleContainer module)
+               {
+                       var ctx = module.Compiler;
+                       foreach (var p in types) {
+                               var found = PredefinedType.Resolve (module, p.Kind, p.Namespace, p.Name, p.Arity);
+                               if (found == null || found == p)
+                                       continue;
+
+                               var tc = found.MemberDefinition as TypeDefinition;
+                               if (tc != null) {
+                                       var ns = module.GlobalRootNamespace.GetNamespace (p.Namespace, false);
+                                       ns.SetBuiltinType (p);
+
+                                       tc.SetPredefinedSpec (p);
+                                       p.SetDefinition (found);
+                               }
+                       }
+
+                       if (ctx.Report.Errors != 0)
+                               return false;
+
+                       // Set internal build-in types
+                       Dynamic.SetDefinition (Object);
+
+                       return true;
+               }
        }
 
+       //
+       // Compiler predefined types. Usually used for compiler generated
+       // code or for comparison against well known framework type. They
+       // may not exist as they are optional
+       //
        class PredefinedTypes
        {
-               // TODO: These two exist only to reject type comparison
-               public readonly PredefinedType TypedReference;
                public readonly PredefinedType ArgIterator;
-
+               public readonly PredefinedType TypedReference;
                public readonly PredefinedType MarshalByRefObject;
                public readonly PredefinedType RuntimeHelpers;
                public readonly PredefinedType IAsyncResult;
@@ -173,6 +195,9 @@ namespace Mono.CSharp
                public readonly PredefinedType NotSupportedException;
                public readonly PredefinedType RuntimeFieldHandle;
                public readonly PredefinedType RuntimeMethodHandle;
+               public readonly PredefinedType SecurityAction;
+               public readonly PredefinedType Dictionary;
+               public readonly PredefinedType Hashtable;
 
                //
                // C# 3.0
@@ -184,6 +209,7 @@ namespace Mono.CSharp
                public readonly PredefinedType MethodBase;
                public readonly PredefinedType MethodInfo;
                public readonly PredefinedType ConstructorInfo;
+               public readonly PredefinedType MemberBinding;
 
                //
                // C# 4.0
@@ -193,10 +219,21 @@ namespace Mono.CSharp
                public readonly PredefinedType CallSiteGeneric;
                public readonly PredefinedType BinderFlags;
 
+               //
+               // C# 5.0
+               //
+               public readonly PredefinedType AsyncVoidMethodBuilder;
+               public readonly PredefinedType AsyncTaskMethodBuilder;
+               public readonly PredefinedType AsyncTaskMethodBuilderGeneric;
+               public readonly PredefinedType Action;
+               public readonly PredefinedType Task;
+               public readonly PredefinedType TaskGeneric;
+
                public PredefinedTypes (ModuleContainer module)
                {
                        TypedReference = new PredefinedType (module, MemberKind.Struct, "System", "TypedReference");
                        ArgIterator = new PredefinedType (module, MemberKind.Struct, "System", "ArgIterator");
+
                        MarshalByRefObject = new PredefinedType (module, MemberKind.Class, "System", "MarshalByRefObject");
                        RuntimeHelpers = new PredefinedType (module, MemberKind.Class, "System.Runtime.CompilerServices", "RuntimeHelpers");
                        IAsyncResult = new PredefinedType (module, MemberKind.Interface, "System", "IAsyncResult");
@@ -215,9 +252,13 @@ namespace Mono.CSharp
                        NotSupportedException = new PredefinedType (module, MemberKind.Class, "System", "NotSupportedException");
                        RuntimeFieldHandle = new PredefinedType (module, MemberKind.Struct, "System", "RuntimeFieldHandle");
                        RuntimeMethodHandle = new PredefinedType (module, MemberKind.Struct, "System", "RuntimeMethodHandle");
+                       SecurityAction = new PredefinedType (module, MemberKind.Enum, "System.Security.Permissions", "SecurityAction");
+                       Dictionary = new PredefinedType (module, MemberKind.Class, "System.Collections.Generic", "Dictionary", 2);
+                       Hashtable = new PredefinedType (module, MemberKind.Class, "System.Collections", "Hashtable");
 
                        Expression = new PredefinedType (module, MemberKind.Class, "System.Linq.Expressions", "Expression");
                        ExpressionGeneric = new PredefinedType (module, MemberKind.Class, "System.Linq.Expressions", "Expression", 1);
+                       MemberBinding = new PredefinedType (module, MemberKind.Class, "System.Linq.Expressions", "MemberBinding");
                        ParameterExpression = new PredefinedType (module, MemberKind.Class, "System.Linq.Expressions", "ParameterExpression");
                        FieldInfo = new PredefinedType (module, MemberKind.Class, "System.Reflection", "FieldInfo");
                        MethodBase = new PredefinedType (module, MemberKind.Class, "System.Reflection", "MethodBase");
@@ -229,44 +270,271 @@ namespace Mono.CSharp
                        Binder = new PredefinedType (module, MemberKind.Class, "Microsoft.CSharp.RuntimeBinder", "Binder");
                        BinderFlags = new PredefinedType (module, MemberKind.Enum, "Microsoft.CSharp.RuntimeBinder", "CSharpBinderFlags");
 
+                       Action = new PredefinedType (module, MemberKind.Delegate, "System", "Action");
+                       AsyncVoidMethodBuilder = new PredefinedType (module, MemberKind.Struct, "System.Runtime.CompilerServices", "AsyncVoidMethodBuilder");
+                       AsyncTaskMethodBuilder = new PredefinedType (module, MemberKind.Struct, "System.Runtime.CompilerServices", "AsyncTaskMethodBuilder");
+                       AsyncTaskMethodBuilderGeneric = new PredefinedType (module, MemberKind.Struct, "System.Runtime.CompilerServices", "AsyncTaskMethodBuilder", 1);
+                       Task = new PredefinedType (module, MemberKind.Class, "System.Threading.Tasks", "Task");
+                       TaskGeneric = new PredefinedType (module, MemberKind.Class, "System.Threading.Tasks", "Task", 1);
+
                        //
                        // Define types which are used for comparison. It does not matter
                        // if they don't exist as no error report is needed
                        //
-                       TypedReference.Define ();
-                       ArgIterator.Define ();
-                       MarshalByRefObject.Define ();
-                       CharSet.Define ();
-
-                       IEnumerableGeneric.Define ();
-                       IListGeneric.Define ();
-                       ICollectionGeneric.Define ();
-                       IEnumerableGeneric.Define ();
-                       IEnumeratorGeneric.Define ();
-                       Nullable.Define ();
-                       ExpressionGeneric.Define ();
-
-                       // Deal with obsolete static types
-                       // TODO: remove
-                       TypeManager.typed_reference_type = TypedReference.TypeSpec;
-                       TypeManager.arg_iterator_type = ArgIterator.TypeSpec;
-                       TypeManager.mbr_type = MarshalByRefObject.TypeSpec;
-                       TypeManager.generic_ilist_type = IListGeneric.TypeSpec;
-                       TypeManager.generic_icollection_type = ICollectionGeneric.TypeSpec;
-                       TypeManager.generic_ienumerator_type = IEnumeratorGeneric.TypeSpec;
-                       TypeManager.generic_ienumerable_type = IEnumerableGeneric.TypeSpec;
-                       TypeManager.generic_nullable_type = Nullable.TypeSpec;
-                       TypeManager.expression_type = ExpressionGeneric.TypeSpec;
+                       if (TypedReference.Define ())
+                               TypedReference.TypeSpec.IsSpecialRuntimeType = true;
+
+                       if (ArgIterator.Define ())
+                               ArgIterator.TypeSpec.IsSpecialRuntimeType = true;
+
+                       if (IEnumerableGeneric.Define ())
+                               IEnumerableGeneric.TypeSpec.IsGenericIterateInterface = true;
+
+                       if (IListGeneric.Define ())
+                               IListGeneric.TypeSpec.IsGenericIterateInterface = true;
+
+                       if (ICollectionGeneric.Define ())
+                               ICollectionGeneric.TypeSpec.IsGenericIterateInterface = true;
+
+                       if (Nullable.Define ())
+                               Nullable.TypeSpec.IsNullableType = true;
+
+                       if (ExpressionGeneric.Define ())
+                               ExpressionGeneric.TypeSpec.IsExpressionTreeType = true;
+
+                       Task.Define ();
+                       if (TaskGeneric.Define ())
+                               TaskGeneric.TypeSpec.IsGenericTask = true;
+               }
+       }
+
+       class PredefinedMembers
+       {
+               public readonly PredefinedMember<MethodSpec> ActivatorCreateInstance;
+               public readonly PredefinedMember<MethodSpec> AsyncTaskMethodBuilderCreate;
+               public readonly PredefinedMember<MethodSpec> AsyncTaskMethodBuilderSetResult;
+               public readonly PredefinedMember<MethodSpec> AsyncTaskMethodBuilderSetException;
+               public readonly PredefinedMember<PropertySpec> AsyncTaskMethodBuilderTask;
+               public readonly PredefinedMember<MethodSpec> AsyncTaskMethodBuilderGenericCreate;
+               public readonly PredefinedMember<MethodSpec> AsyncTaskMethodBuilderGenericSetResult;
+               public readonly PredefinedMember<MethodSpec> AsyncTaskMethodBuilderGenericSetException;
+               public readonly PredefinedMember<PropertySpec> AsyncTaskMethodBuilderGenericTask;
+               public readonly PredefinedMember<MethodSpec> AsyncVoidMethodBuilderCreate;
+               public readonly PredefinedMember<MethodSpec> AsyncVoidMethodBuilderSetException;
+               public readonly PredefinedMember<MethodSpec> AsyncVoidMethodBuilderSetResult;
+               public readonly PredefinedMember<MethodSpec> DecimalCtor;
+               public readonly PredefinedMember<MethodSpec> DecimalCtorInt;
+               public readonly PredefinedMember<MethodSpec> DecimalCtorLong;
+               public readonly PredefinedMember<MethodSpec> DecimalConstantAttributeCtor;
+               public readonly PredefinedMember<MethodSpec> DefaultMemberAttributeCtor;
+               public readonly PredefinedMember<MethodSpec> DelegateCombine;
+               public readonly PredefinedMember<MethodSpec> DelegateEqual;
+               public readonly PredefinedMember<MethodSpec> DelegateInequal;
+               public readonly PredefinedMember<MethodSpec> DelegateRemove;
+               public readonly PredefinedMember<MethodSpec> DynamicAttributeCtor;
+               public readonly PredefinedMember<MethodSpec> FieldInfoGetFieldFromHandle;
+               public readonly PredefinedMember<MethodSpec> FieldInfoGetFieldFromHandle2;
+               public readonly PredefinedMember<MethodSpec> IDisposableDispose;
+               public readonly PredefinedMember<MethodSpec> IEnumerableGetEnumerator;
+               public readonly PredefinedMember<MethodSpec> InterlockedCompareExchange;
+               public readonly PredefinedMember<MethodSpec> InterlockedCompareExchange_T;
+               public readonly PredefinedMember<MethodSpec> FixedBufferAttributeCtor;
+               public readonly PredefinedMember<MethodSpec> MethodInfoGetMethodFromHandle;
+               public readonly PredefinedMember<MethodSpec> MethodInfoGetMethodFromHandle2;
+               public readonly PredefinedMember<MethodSpec> MonitorEnter;
+               public readonly PredefinedMember<MethodSpec> MonitorEnter_v4;
+               public readonly PredefinedMember<MethodSpec> MonitorExit;
+               public readonly PredefinedMember<PropertySpec> RuntimeCompatibilityWrapNonExceptionThrows;
+               public readonly PredefinedMember<MethodSpec> RuntimeHelpersInitializeArray;
+               public readonly PredefinedMember<PropertySpec> RuntimeHelpersOffsetToStringData;
+               public readonly PredefinedMember<ConstSpec> SecurityActionRequestMinimum;
+               public readonly PredefinedMember<FieldSpec> StringEmpty;
+               public readonly PredefinedMember<MethodSpec> StringEqual;
+               public readonly PredefinedMember<MethodSpec> StringInequal;
+               public readonly PredefinedMember<MethodSpec> StructLayoutAttributeCtor;
+               public readonly PredefinedMember<FieldSpec> StructLayoutCharSet;
+               public readonly PredefinedMember<FieldSpec> StructLayoutSize;
+               public readonly PredefinedMember<MethodSpec> TypeGetTypeFromHandle;
+
+               public PredefinedMembers (ModuleContainer module)
+               {
+                       var types = module.PredefinedTypes;
+                       var atypes = module.PredefinedAttributes;
+                       var btypes = module.Compiler.BuiltinTypes;
+
+                       ActivatorCreateInstance = new PredefinedMember<MethodSpec> (module, types.Activator,
+                               MemberFilter.Method ("CreateInstance", 1, ParametersCompiled.EmptyReadOnlyParameters, null));
+
+                       AsyncTaskMethodBuilderCreate = new PredefinedMember<MethodSpec> (module, types.AsyncTaskMethodBuilder,
+                               MemberFilter.Method ("Create", 0, ParametersCompiled.EmptyReadOnlyParameters, types.AsyncTaskMethodBuilder.TypeSpec));
+
+                       AsyncTaskMethodBuilderSetResult = new PredefinedMember<MethodSpec> (module, types.AsyncTaskMethodBuilder,
+                               MemberFilter.Method ("SetResult", 0, ParametersCompiled.EmptyReadOnlyParameters, btypes.Void));
+
+                       AsyncTaskMethodBuilderSetException = new PredefinedMember<MethodSpec> (module, types.AsyncTaskMethodBuilder,
+                               MemberFilter.Method ("SetException", 0,
+                               ParametersCompiled.CreateFullyResolved (btypes.Exception), btypes.Void));
+
+                       AsyncTaskMethodBuilderTask = new PredefinedMember<PropertySpec> (module, types.AsyncTaskMethodBuilder,
+                               MemberFilter.Property ("Task", null));
+
+                       AsyncTaskMethodBuilderGenericCreate = new PredefinedMember<MethodSpec> (module, types.AsyncTaskMethodBuilderGeneric,
+                               MemberFilter.Method ("Create", 0, ParametersCompiled.EmptyReadOnlyParameters, types.AsyncVoidMethodBuilder.TypeSpec));
+
+                       AsyncTaskMethodBuilderGenericSetResult = new PredefinedMember<MethodSpec> (module, types.AsyncTaskMethodBuilderGeneric,
+                               "SetResult", MemberKind.Method, () => new TypeSpec[] {
+                                               types.AsyncTaskMethodBuilderGeneric.TypeSpec.MemberDefinition.TypeParameters[0]
+                               });
+
+                       AsyncTaskMethodBuilderGenericSetException = new PredefinedMember<MethodSpec> (module, types.AsyncTaskMethodBuilderGeneric,
+                               MemberFilter.Method ("SetException", 0,
+                               ParametersCompiled.CreateFullyResolved (btypes.Exception), btypes.Void));
+
+                       AsyncTaskMethodBuilderGenericTask = new PredefinedMember<PropertySpec> (module, types.AsyncTaskMethodBuilderGeneric,
+                               MemberFilter.Property ("Task", null));
+
+                       AsyncVoidMethodBuilderCreate = new PredefinedMember<MethodSpec> (module, types.AsyncVoidMethodBuilder,
+                               MemberFilter.Method ("Create", 0, ParametersCompiled.EmptyReadOnlyParameters, types.AsyncVoidMethodBuilder.TypeSpec));
+
+                       AsyncVoidMethodBuilderSetException = new PredefinedMember<MethodSpec> (module, types.AsyncVoidMethodBuilder,
+                               MemberFilter.Method ("SetException", 0, null, btypes.Void));
+
+                       AsyncVoidMethodBuilderSetResult = new PredefinedMember<MethodSpec> (module, types.AsyncVoidMethodBuilder,
+                               MemberFilter.Method ("SetResult", 0, ParametersCompiled.EmptyReadOnlyParameters, btypes.Void));
+
+                       DecimalCtor = new PredefinedMember<MethodSpec> (module, btypes.Decimal,
+                               MemberFilter.Constructor (ParametersCompiled.CreateFullyResolved (
+                                       btypes.Int, btypes.Int, btypes.Int, btypes.Bool, btypes.Byte)));
+
+                       DecimalCtorInt = new PredefinedMember<MethodSpec> (module, btypes.Decimal,
+                               MemberFilter.Constructor (ParametersCompiled.CreateFullyResolved (btypes.Int)));
+
+                       DecimalCtorLong = new PredefinedMember<MethodSpec> (module, btypes.Decimal,
+                               MemberFilter.Constructor (ParametersCompiled.CreateFullyResolved (btypes.Long)));
+
+                       DecimalConstantAttributeCtor = new PredefinedMember<MethodSpec> (module, atypes.DecimalConstant,
+                               MemberFilter.Constructor (ParametersCompiled.CreateFullyResolved (
+                                       btypes.Byte, btypes.Byte, btypes.UInt, btypes.UInt, btypes.UInt)));
+
+                       DefaultMemberAttributeCtor = new PredefinedMember<MethodSpec> (module, atypes.DefaultMember,
+                               MemberFilter.Constructor (ParametersCompiled.CreateFullyResolved (btypes.String)));
+
+                       DelegateCombine = new PredefinedMember<MethodSpec> (module, btypes.Delegate, "Combine", btypes.Delegate, btypes.Delegate);
+                       DelegateRemove = new PredefinedMember<MethodSpec> (module, btypes.Delegate, "Remove", btypes.Delegate, btypes.Delegate);
+
+                       DelegateEqual = new PredefinedMember<MethodSpec> (module, btypes.Delegate,
+                               new MemberFilter (Operator.GetMetadataName (Operator.OpType.Equality), 0, MemberKind.Operator, null, btypes.Bool));
+
+                       DelegateInequal = new PredefinedMember<MethodSpec> (module, btypes.Delegate,
+                               new MemberFilter (Operator.GetMetadataName (Operator.OpType.Inequality), 0, MemberKind.Operator, null, btypes.Bool));
+
+                       DynamicAttributeCtor = new PredefinedMember<MethodSpec> (module, atypes.Dynamic,
+                               MemberFilter.Constructor (ParametersCompiled.CreateFullyResolved (
+                                       ArrayContainer.MakeType (module, btypes.Bool))));
+
+                       FieldInfoGetFieldFromHandle = new PredefinedMember<MethodSpec> (module, types.FieldInfo,
+                               "GetFieldFromHandle", MemberKind.Method, types.RuntimeFieldHandle);
+
+                       FieldInfoGetFieldFromHandle2 = new PredefinedMember<MethodSpec> (module, types.FieldInfo,
+                               "GetFieldFromHandle", MemberKind.Method, types.RuntimeFieldHandle, new PredefinedType (btypes.RuntimeTypeHandle));
+
+                       FixedBufferAttributeCtor = new PredefinedMember<MethodSpec> (module, atypes.FixedBuffer,
+                               MemberFilter.Constructor (ParametersCompiled.CreateFullyResolved (btypes.Type, btypes.Int)));
+
+                       IDisposableDispose = new PredefinedMember<MethodSpec> (module, btypes.IDisposable, "Dispose", TypeSpec.EmptyTypes);
+
+                       IEnumerableGetEnumerator = new PredefinedMember<MethodSpec> (module, btypes.IEnumerable,
+                               "GetEnumerator", TypeSpec.EmptyTypes);
+
+                       InterlockedCompareExchange = new PredefinedMember<MethodSpec> (module, types.Interlocked,
+                               MemberFilter.Method ("CompareExchange", 0,
+                                       new ParametersImported (
+                                               new[] {
+                                                               new ParameterData (null, Parameter.Modifier.REF),
+                                                               new ParameterData (null, Parameter.Modifier.NONE),
+                                                               new ParameterData (null, Parameter.Modifier.NONE)
+                                                       },
+                                               new[] {
+                                                               btypes.Int, btypes.Int, btypes.Int
+                                                       },
+                                               false),
+                               btypes.Int));
+
+                       InterlockedCompareExchange_T = new PredefinedMember<MethodSpec> (module, types.Interlocked,
+                               MemberFilter.Method ("CompareExchange", 1,
+                                       new ParametersImported (
+                                               new[] {
+                                                               new ParameterData (null, Parameter.Modifier.REF),
+                                                               new ParameterData (null, Parameter.Modifier.NONE),
+                                                               new ParameterData (null, Parameter.Modifier.NONE)
+                                                       },
+                                               new[] {
+                                                               new TypeParameterSpec (0, null, SpecialConstraint.None, Variance.None, null),
+                                                               new TypeParameterSpec (0, null, SpecialConstraint.None, Variance.None, null),
+                                                               new TypeParameterSpec (0, null, SpecialConstraint.None, Variance.None, null),
+                                                       }, false),
+                                       null));
+
+                       MethodInfoGetMethodFromHandle = new PredefinedMember<MethodSpec> (module, types.MethodBase,
+                               "GetMethodFromHandle", MemberKind.Method, types.RuntimeMethodHandle);
+
+                       MethodInfoGetMethodFromHandle2 = new PredefinedMember<MethodSpec> (module, types.MethodBase,
+                               "GetMethodFromHandle", MemberKind.Method, types.RuntimeMethodHandle, new PredefinedType (btypes.RuntimeTypeHandle));
+
+                       MonitorEnter = new PredefinedMember<MethodSpec> (module, types.Monitor, "Enter", btypes.Object);
+
+                       MonitorEnter_v4 = new PredefinedMember<MethodSpec> (module, types.Monitor,
+                               MemberFilter.Method ("Enter", 0,
+                                       new ParametersImported (new[] {
+                                                       new ParameterData (null, Parameter.Modifier.NONE),
+                                                       new ParameterData (null, Parameter.Modifier.REF)
+                                               },
+                                       new[] {
+                                                       btypes.Object, btypes.Bool
+                                               }, false), null));
+
+                       MonitorExit = new PredefinedMember<MethodSpec> (module, types.Monitor, "Exit", btypes.Object);
+
+                       RuntimeCompatibilityWrapNonExceptionThrows = new PredefinedMember<PropertySpec> (module, atypes.RuntimeCompatibility,
+                               MemberFilter.Property ("WrapNonExceptionThrows", btypes.Bool));
+
+                       RuntimeHelpersInitializeArray = new PredefinedMember<MethodSpec> (module, types.RuntimeHelpers,
+                               "InitializeArray", btypes.Array, btypes.RuntimeFieldHandle);
+
+                       RuntimeHelpersOffsetToStringData = new PredefinedMember<PropertySpec> (module, types.RuntimeHelpers,
+                               MemberFilter.Property ("OffsetToStringData", btypes.Int));
+
+                       SecurityActionRequestMinimum = new PredefinedMember<ConstSpec> (module, types.SecurityAction, "RequestMinimum",
+                               MemberKind.Field, types.SecurityAction);
+
+                       StringEmpty = new PredefinedMember<FieldSpec> (module, btypes.String, MemberFilter.Field ("Empty", btypes.String));
+
+                       StringEqual = new PredefinedMember<MethodSpec> (module, btypes.String,
+                               new MemberFilter (Operator.GetMetadataName (Operator.OpType.Equality), 0, MemberKind.Operator, null, btypes.Bool));
+
+                       StringInequal = new PredefinedMember<MethodSpec> (module, btypes.String,
+                               new MemberFilter (Operator.GetMetadataName (Operator.OpType.Inequality), 0, MemberKind.Operator, null, btypes.Bool));
+
+                       StructLayoutAttributeCtor = new PredefinedMember<MethodSpec> (module, atypes.StructLayout,
+                               MemberFilter.Constructor (ParametersCompiled.CreateFullyResolved (btypes.Short)));
+
+                       StructLayoutCharSet = new PredefinedMember<FieldSpec> (module, atypes.StructLayout, "CharSet",
+                               MemberKind.Field, types.CharSet);
+
+                       StructLayoutSize = new PredefinedMember<FieldSpec> (module, atypes.StructLayout,
+                               MemberFilter.Field ("Size", btypes.Int));
+
+                       TypeGetTypeFromHandle = new PredefinedMember<MethodSpec> (module, btypes.Type, "GetTypeFromHandle", btypes.RuntimeTypeHandle);
                }
        }
 
        public class PredefinedType
        {
-               string name;
-               string ns;
-               int arity;
-               MemberKind kind;
-               ModuleContainer module;
+               readonly string name;
+               readonly string ns;
+               readonly int arity;
+               readonly MemberKind kind;
+               protected readonly ModuleContainer module;
                protected TypeSpec type;
 
                public PredefinedType (ModuleContainer module, MemberKind kind, string ns, string name, int arity)
@@ -283,6 +551,14 @@ namespace Mono.CSharp
                        this.ns = ns;
                }
 
+               public PredefinedType (BuiltinTypeSpec type)
+               {
+                       this.kind = type.Kind;
+                       this.name = type.Name;
+                       this.ns = type.Namespace;
+                       this.type = type;
+               }
+
                #region Properties
 
                public int Arity {
@@ -322,16 +598,8 @@ namespace Mono.CSharp
                        if (type != null)
                                return true;
 
-                       Namespace type_ns = module.GlobalRootNamespace.GetNamespace (ns, true);
-                       var te = type_ns.LookupType (module.Compiler, name, arity, true, Location.Null);
-                       if (te == null)
-                               return false;
-
-                       if (te.Type.Kind != kind)
-                               return false;
-
-                       type = te.Type;
-                       return true;
+                       type = Resolve (module, kind, ns, name, arity, false);
+                       return type != null;
                }
 
                public string GetSignatureForError ()
@@ -339,385 +607,244 @@ namespace Mono.CSharp
                        return ns + "." + name;
                }
 
-               public static TypeSpec Resolve (ModuleContainer module, MemberKind kind, string ns, string name, int arity, Location loc)
+               public static TypeSpec Resolve (ModuleContainer module, MemberKind kind, string ns, string name, int arity)
+               {
+                       return Resolve (module, kind, ns, name, arity, true);
+               }
+
+               public static TypeSpec Resolve (ModuleContainer module, MemberKind kind, string ns, string name, int arity, bool reportErrors)
                {
                        Namespace type_ns = module.GlobalRootNamespace.GetNamespace (ns, true);
-                       var te = type_ns.LookupType (module.Compiler, name, arity, false, Location.Null);
-                       if (te == null) {
-                               module.Compiler.Report.Error (518, loc, "The predefined type `{0}.{1}' is not defined or imported", ns, name);
+                       var found = type_ns.GetAllTypes (name);
+                       if (found == null) {
+                               if (reportErrors)
+                                       module.Compiler.Report.Error (518, "The predefined type `{0}.{1}' is not defined or imported", ns, name);
+
                                return null;
                        }
 
-                       var type = te.Type;
-                       if (type.Kind != kind) {
+                       TypeSpec best_match = null;
+                       foreach (var candidate in found) {
+                               if (candidate.Kind != kind) {
+                                       if (candidate.Kind == MemberKind.Struct && kind == MemberKind.Void && candidate.MemberDefinition is TypeContainer) {
+                                               // Void is declared as struct but we keep it internally as
+                                               // special kind, the swap will be done by caller
+                                       } else {
+                                               continue;
+                                       }
+                               }
+
+                               if (candidate.Arity != arity)
+                                       continue;
+
+                               if ((candidate.Modifiers & Modifiers.INTERNAL) != 0 && !candidate.MemberDefinition.IsInternalAsPublic (module.DeclaringAssembly))
+                                       continue;
+
+                               if (best_match == null) {
+                                       best_match = candidate;
+                                       continue;
+                               }
+
+                               var other_match = best_match;
+                               if (!best_match.MemberDefinition.IsImported &&
+                                       module.Compiler.BuiltinTypes.Object.MemberDefinition.DeclaringAssembly == candidate.MemberDefinition.DeclaringAssembly) {
+                                       best_match = candidate;
+                               }
+
+                               string location;
+                               if (best_match.MemberDefinition is MemberCore) {
+                                       location = ((MemberCore) best_match.MemberDefinition).Location.Name;
+                               } else {
+                                       var assembly = (ImportedAssemblyDefinition) best_match.MemberDefinition.DeclaringAssembly;
+                                       location = Path.GetFileName (assembly.Location);
+                               }
+
+                               module.Compiler.Report.SymbolRelatedToPreviousError (other_match);
+                               module.Compiler.Report.SymbolRelatedToPreviousError (candidate);
+
+                               module.Compiler.Report.Warning (1685, 1,
+                                       "The predefined type `{0}.{1}' is defined multiple times. Using definition from `{2}'",
+                                       ns, name, location);
+
+                               break;
+                       }
+
+                       if (best_match == null && reportErrors) {
+                               Location loc;
+                               if (found[0].MemberDefinition is MemberCore) {
+                                       loc = ((MemberCore) found[0].MemberDefinition).Location;
+                               } else {
+                                       loc = Location.Null;
+                                       module.Compiler.Report.SymbolRelatedToPreviousError (found[0]);
+                               }
+
                                module.Compiler.Report.Error (520, loc, "The predefined type `{0}.{1}' is not declared correctly", ns, name);
-                               return null;
                        }
 
-                       return type;
+                       return best_match;
                }
 
-               public TypeSpec Resolve (Location loc)
+               public TypeSpec Resolve ()
                {
                        if (type == null)
-                               type = Resolve (module, kind, ns, name, arity, loc);
+                               type = Resolve (module, kind, ns, name, arity);
 
                        return type;
                }
        }
 
-       partial class TypeManager {
-       //
-       // A list of core types that the compiler requires or uses
-       //
-       static public BuildinTypeSpec object_type;
-       static public BuildinTypeSpec value_type;
-       static public BuildinTypeSpec string_type;
-       static public BuildinTypeSpec int32_type;
-       static public BuildinTypeSpec uint32_type;
-       static public BuildinTypeSpec int64_type;
-       static public BuildinTypeSpec uint64_type;
-       static public BuildinTypeSpec float_type;
-       static public BuildinTypeSpec double_type;
-       static public BuildinTypeSpec char_type;
-       static public BuildinTypeSpec short_type;
-       static public BuildinTypeSpec decimal_type;
-       static public BuildinTypeSpec bool_type;
-       static public BuildinTypeSpec sbyte_type;
-       static public BuildinTypeSpec byte_type;
-       static public BuildinTypeSpec ushort_type;
-       static public BuildinTypeSpec enum_type;
-       static public BuildinTypeSpec delegate_type;
-       static public BuildinTypeSpec multicast_delegate_type;
-       static public BuildinTypeSpec void_type;
-       static public BuildinTypeSpec array_type;
-       static public BuildinTypeSpec runtime_handle_type;
-       static public BuildinTypeSpec type_type;
-       static public BuildinTypeSpec ienumerator_type;
-       static public BuildinTypeSpec ienumerable_type;
-       static public BuildinTypeSpec idisposable_type;
-       static public BuildinTypeSpec intptr_type;
-       static public BuildinTypeSpec uintptr_type;
-       static public BuildinTypeSpec runtime_field_handle_type;
-       static public BuildinTypeSpec attribute_type;
-       static public BuildinTypeSpec exception_type;
-
-
-       static public TypeSpec typed_reference_type;
-       static public TypeSpec arg_iterator_type;
-       static public TypeSpec mbr_type;
-       static public TypeSpec void_ptr_type;
-       static public TypeSpec generic_ilist_type;
-       static public TypeSpec generic_icollection_type;
-       static public TypeSpec generic_ienumerator_type;
-       static public TypeSpec generic_ienumerable_type;
-       static public TypeSpec generic_nullable_type;
-       static internal TypeSpec expression_type;
-
-       //
-       // These methods are called by code generated by the compiler
-       //
-       static public FieldSpec string_empty;
-       static public MethodSpec system_type_get_type_from_handle;
-       static public MethodSpec bool_movenext_void;
-       static public MethodSpec void_dispose_void;
-       static public MethodSpec void_monitor_enter_object;
-       static public MethodSpec void_monitor_exit_object;
-       static public MethodSpec void_initializearray_array_fieldhandle;
-       static public MethodSpec delegate_combine_delegate_delegate;
-       static public MethodSpec delegate_remove_delegate_delegate;
-       static public PropertySpec int_get_offset_to_string_data;
-       static public MethodSpec int_interlocked_compare_exchange;
-       public static MethodSpec gen_interlocked_compare_exchange;
-       static public PropertySpec ienumerator_getcurrent;
-       public static MethodSpec methodbase_get_type_from_handle;
-       public static MethodSpec methodbase_get_type_from_handle_generic;
-       public static MethodSpec fieldinfo_get_field_from_handle;
-       public static MethodSpec fieldinfo_get_field_from_handle_generic;
-       public static MethodSpec activator_create_instance;
-
-       //
-       // The constructors.
-       //
-       static public MethodSpec void_decimal_ctor_five_args;
-       static public MethodSpec void_decimal_ctor_int_arg;
-       public static MethodSpec void_decimal_ctor_long_arg;
-
-       static TypeManager ()
-       {
-               Reset ();
-       }
-
-       static public void Reset ()
-       {
-//             object_type = null;
-       
-               // TODO: I am really bored by all this static stuff
-               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_interlocked_compare_exchange =
-               gen_interlocked_compare_exchange =
-               methodbase_get_type_from_handle =
-               methodbase_get_type_from_handle_generic =
-               fieldinfo_get_field_from_handle =
-               fieldinfo_get_field_from_handle_generic =
-               activator_create_instance =
-               delegate_combine_delegate_delegate =
-               delegate_remove_delegate_delegate = null;
-
-               int_get_offset_to_string_data =
-               ienumerator_getcurrent = null;
-
-               void_decimal_ctor_five_args =
-               void_decimal_ctor_int_arg =
-               void_decimal_ctor_long_arg = null;
-
-               string_empty = null;
-
-               typed_reference_type = arg_iterator_type = mbr_type =
-               void_ptr_type =
-               generic_ilist_type = generic_icollection_type = generic_ienumerator_type =
-               generic_ienumerable_type = generic_nullable_type = expression_type = null;
-       }
-
-       /// <summary>
-       ///   Returns the C# name of a type if possible, or the full type name otherwise
-       /// </summary>
-       static public string CSharpName (TypeSpec t)
+       class PredefinedMember<T> where T : MemberSpec
        {
-               return t.GetSignatureForError ();
-       }
+               readonly ModuleContainer module;
+               T member;
+               TypeSpec declaring_type;
+               readonly PredefinedType declaring_type_predefined;
+               MemberFilter filter;
+               readonly Func<TypeSpec[]> filter_builder;
 
-       static public string CSharpName (IList<TypeSpec> types)
-       {
-               if (types.Count == 0)
-                       return string.Empty;
+               public PredefinedMember (ModuleContainer module, PredefinedType type, MemberFilter filter)
+               {
+                       this.module = module;
+                       this.declaring_type_predefined = type;
+                       this.filter = filter;
+               }
 
-               StringBuilder sb = new StringBuilder ();
-               for (int i = 0; i < types.Count; ++i) {
-                       if (i > 0)
-                               sb.Append (",");
+               public PredefinedMember (ModuleContainer module, TypeSpec type, MemberFilter filter)
+               {
+                       this.module = module;
+                       this.declaring_type = type;
+                       this.filter = filter;
+               }
 
-                       sb.Append (CSharpName (types [i]));
+               public PredefinedMember (ModuleContainer module, PredefinedType type, string name, params TypeSpec[] types)
+                       : this (module, type, MemberFilter.Method (name, 0, ParametersCompiled.CreateFullyResolved (types), null))
+               {
                }
-               return sb.ToString ();
-       }
 
-       static public string GetFullNameSignature (MemberSpec mi)
-       {
-               return mi.GetSignatureForError ();
-       }
+               public PredefinedMember (ModuleContainer module, PredefinedType type, string name, MemberKind kind, params PredefinedType[] types)
+                       : this (module, type, new MemberFilter (name, 0, kind, null, null))
+               {
+                       filter_builder = () => {
+                               var ptypes = new TypeSpec[types.Length];
+                               for (int i = 0; i < ptypes.Length; ++i) {
+                                       var p = types[i];
+                                       if (!p.Define ())
+                                               return null;
+
+                                       ptypes[i] = p.TypeSpec;
+                               }
+
+                               return ptypes;
+                       };
+               }
 
-       static public string CSharpSignature (MemberSpec mb)
-       {
-               return mb.GetSignatureForError ();
-       }
+               public PredefinedMember (ModuleContainer module, PredefinedType type, string name, MemberKind kind, Func<TypeSpec[]> typesBuilder)
+                       : this (module, type, new MemberFilter (name, 0, kind, null, null))
+               {
+                       filter_builder = typesBuilder;
+               }
 
-       static MemberSpec GetPredefinedMember (TypeSpec t, MemberFilter filter, bool optional, Location loc)
-       {
-               var member = MemberCache.FindMember (t, filter, BindingRestriction.DeclaredOnly);
+               public PredefinedMember (ModuleContainer module, BuiltinTypeSpec type, string name, params TypeSpec[] types)
+                       : this (module, type, MemberFilter.Method (name, 0, ParametersCompiled.CreateFullyResolved (types), null))
+               {
+               }
 
-               if (member != null && member.IsAccessible (InternalType.FakeInternalType))
-                       return member;
+               public T Get ()
+               {
+                       if (member != null)
+                               return member;
 
-               if (optional)
-                       return member;
+                       if (declaring_type == null) {
+                               if (!declaring_type_predefined.Define ())
+                                       return null;
 
-               string method_args = null;
-               if (filter.Parameters != null)
-                       method_args = filter.Parameters.GetSignatureForError ();
+                               declaring_type = declaring_type_predefined.TypeSpec;
+                       }
 
-               RootContext.ToplevelTypes.Compiler.Report.Error (656, loc, "The compiler required member `{0}.{1}{2}' could not be found or is inaccessible",
-                       TypeManager.CSharpName (t), filter.Name, method_args);
+                       if (filter_builder != null) {
+                               var types = filter_builder ();
 
-               return null;
-       }
+                               if (filter.Kind == MemberKind.Field)
+                                       filter = new MemberFilter (filter.Name, filter.Arity, filter.Kind, null, types [0]);
+                               else
+                                       filter = new MemberFilter (filter.Name, filter.Arity, filter.Kind,
+                                               ParametersCompiled.CreateFullyResolved (types), filter.MemberType);
+                       }
 
-       //
-       // Returns the ConstructorInfo for "args"
-       //
-       public static MethodSpec GetPredefinedConstructor (TypeSpec t, Location loc, params TypeSpec [] args)
-       {
-               var pc = ParametersCompiled.CreateFullyResolved (args);
-               return GetPredefinedMember (t, MemberFilter.Constructor (pc), false, loc) as MethodSpec;
-       }
+                       member = MemberCache.FindMember (declaring_type, filter, BindingRestriction.DeclaredOnly) as T;
+                       if (member == null)
+                               return null;
 
-       //
-       // Returns the method specification for a method named `name' defined
-       // in type `t' which takes arguments of types `args'
-       //
-       public static MethodSpec GetPredefinedMethod (TypeSpec t, string name, Location loc, params TypeSpec [] args)
-       {
-               var pc = ParametersCompiled.CreateFullyResolved (args);
-               return GetPredefinedMethod (t, MemberFilter.Method (name, 0, pc, null), false, loc);
-       }
+                       if (!member.IsAccessible (module))
+                               return null;
 
-       public static MethodSpec GetPredefinedMethod (TypeSpec t, MemberFilter filter, Location loc)
-       {
-               return GetPredefinedMethod (t, filter, false, loc);
-       }
+                       return member;
+               }
 
-       public static MethodSpec GetPredefinedMethod (TypeSpec t, MemberFilter filter, bool optional, Location loc)
-       {
-               return GetPredefinedMember (t, filter, optional, loc) as MethodSpec;
-       }
+               public T Resolve (Location loc)
+               {
+                       if (member != null)
+                               return member;
 
-       public static FieldSpec GetPredefinedField (TypeSpec t, string name, Location loc, TypeSpec type)
-       {
-               return GetPredefinedMember (t, MemberFilter.Field (name, type), false, loc) as FieldSpec;
-       }
+                       if (Get () != null)
+                               return member;
 
-       public static PropertySpec GetPredefinedProperty (TypeSpec t, string name, Location loc, TypeSpec type)
-       {
-               return GetPredefinedMember (t, MemberFilter.Property (name, type), false, loc) as PropertySpec;
-       }
+                       if (declaring_type == null) {
+                               if (declaring_type_predefined.Resolve () == null)
+                                       return null;
+                       }
 
-       /// <remarks>
-       ///   The types have to be initialized after the initial
-       ///   population of the type has happened (for example, to
-       ///   bootstrap the corlib.dll
-       /// </remarks>
-       public static bool InitCoreTypes (ModuleContainer module, BuildinTypes buildin)
-       {
-               var ctx = module.Compiler;
-               foreach (var p in buildin.Types) {
-                       var found = PredefinedType.Resolve (module, p.Kind, p.Namespace, p.Name, p.Arity, Location.Null);
-                       if (found == null || found == p)
-                               continue;
-
-                       if (!RootContext.StdLib) {
-                               var ns = module.GlobalRootNamespace.GetNamespace (p.Namespace, false);
-                               ns.ReplaceTypeWithPredefined (found, p);
-
-                               var tc = found.MemberDefinition as TypeContainer;
-                               tc.SetPredefinedSpec (p);
-                               p.SetDefinition (found);
+                       if (filter_builder != null) {
+                               filter = new MemberFilter (filter.Name, filter.Arity, filter.Kind,
+                                       ParametersCompiled.CreateFullyResolved (filter_builder ()), filter.MemberType);
                        }
-               }
 
-               if (InternalType.Dynamic.GetMetaInfo () == null) {
-                       InternalType.Dynamic.SetMetaInfo (object_type.GetMetaInfo ());
+                       string method_args = null;
+                       if (filter.Parameters != null)
+                               method_args = filter.Parameters.GetSignatureForError ();
 
-                       if (object_type.MemberDefinition.IsImported)
-                               InternalType.Dynamic.MemberCache = object_type.MemberCache;
+                       module.Compiler.Report.Error (656, loc, "The compiler required member `{0}.{1}{2}' could not be found or is inaccessible",
+                               declaring_type.GetSignatureForError (), filter.Name, method_args);
 
-                       InternalType.Null.SetMetaInfo (object_type.GetMetaInfo ());
+                       return null;
                }
-
-               return ctx.Report.Errors == 0;
-       }
-
-       //
-       // Initializes optional core types
-       //
-       public static void InitOptionalCoreTypes (CompilerContext ctx)
-       {
-               void_ptr_type = PointerContainer.MakeType (void_type);
-       }
-
-       public static bool IsBuiltinType (TypeSpec t)
-       {
-               if (t == object_type || t == string_type || t == int32_type || t == uint32_type ||
-                   t == int64_type || t == uint64_type || t == float_type || t == double_type ||
-                   t == char_type || t == short_type || t == decimal_type || t == bool_type ||
-                   t == sbyte_type || t == byte_type || t == ushort_type || t == void_type)
-                       return true;
-               else
-                       return false;
-       }
-
-       //
-       // This is like IsBuiltinType, but lacks decimal_type, we should also clean up
-       // the pieces in the code where we use IsBuiltinType and special case decimal_type.
-       // 
-       public static bool IsPrimitiveType (TypeSpec t)
-       {
-               return (t == int32_type || t == uint32_type ||
-                   t == int64_type || t == uint64_type || t == float_type || t == double_type ||
-                   t == char_type || t == short_type || t == bool_type ||
-                   t == sbyte_type || t == byte_type || t == ushort_type);
        }
 
-       // Obsolete
-       public static bool IsDelegateType (TypeSpec t)
-       {
-               return t.IsDelegate;
-       }
-       
-       // Obsolete
-       public static bool IsEnumType (TypeSpec t)
-       {
-               return t.IsEnum;
-       }
+       partial class TypeManager {
 
-       public static bool IsBuiltinOrEnum (TypeSpec t)
+       /// <summary>
+       ///   Returns the C# name of a type if possible, or the full type name otherwise
+       /// </summary>
+       static public string CSharpName (TypeSpec t)
        {
-               if (IsBuiltinType (t))
-                       return true;
-               
-               if (IsEnumType (t))
-                       return true;
-
-               return false;
+               return t.GetSignatureForError ();
        }
 
-       //
-       // Whether a type is unmanaged.  This is used by the unsafe code (25.2)
-       //
-       public static bool IsUnmanagedType (TypeSpec t)
+       static public string CSharpName (IList<TypeSpec> types)
        {
-               var ds = t.MemberDefinition as DeclSpace;
-               if (ds != null)
-                       return ds.IsUnmanagedType ();
-
-               // some builtins that are not unmanaged types
-               if (t == TypeManager.object_type || t == TypeManager.string_type)
-                       return false;
-
-               if (IsBuiltinOrEnum (t))
-                       return true;
-
-               // Someone did the work of checking if the ElementType of t is unmanaged.  Let's not repeat it.
-               if (t.IsPointer)
-                       return IsUnmanagedType (GetElementType (t));
-
-               if (!IsValueType (t))
-                       return false;
+               if (types.Count == 0)
+                       return string.Empty;
 
-               if (t.IsNested && t.DeclaringType.IsGenericOrParentIsGeneric)
-                       return false;
+               StringBuilder sb = new StringBuilder ();
+               for (int i = 0; i < types.Count; ++i) {
+                       if (i > 0)
+                               sb.Append (",");
 
-               return true;
+                       sb.Append (CSharpName (types [i]));
+               }
+               return sb.ToString ();
        }
 
-       //
-       // Null is considered to be a reference type
-       //                      
-       public static bool IsReferenceType (TypeSpec t)
-       {
-               if (t.IsGenericParameter)
-                       return ((TypeParameterSpec) t).IsReferenceType;
-
-               return !t.IsStruct && !IsEnumType (t);
-       }                       
-               
-       public static bool IsValueType (TypeSpec t)
+       static public string GetFullNameSignature (MemberSpec mi)
        {
-               if (t.IsGenericParameter)
-                       return ((TypeParameterSpec) t).IsValueType;
-
-               return t.IsStruct || IsEnumType (t);
+               return mi.GetSignatureForError ();
        }
 
-       public static bool IsStruct (TypeSpec t)
+       static public string CSharpSignature (MemberSpec mb)
        {
-               return t.IsStruct;
+               return mb.GetSignatureForError ();
        }
-
+               
        public static bool IsFamilyAccessible (TypeSpec type, TypeSpec parent)
        {
 //             TypeParameter tparam = LookupTypeParameter (type);
@@ -741,39 +868,20 @@ namespace Mono.CSharp
                return false;
        }
 
-       //
-       // Checks whether `type' is a subclass or nested child of `base_type'.
-       //
-       public static bool IsNestedFamilyAccessible (TypeSpec type, TypeSpec base_type)
-       {
-               do {
-                       if (IsFamilyAccessible (type, base_type))
-                               return true;
-
-                       // Handle nested types.
-                       type = type.DeclaringType;
-               } while (type != null);
-
-               return false;
-       }
-
        //
        // Checks whether `type' is a nested child of `parent'.
        //
-       public static bool IsNestedChildOf (TypeSpec type, TypeSpec parent)
+       public static bool IsNestedChildOf (TypeSpec type, ITypeDefinition parent)
        {
                if (type == null)
                        return false;
 
-               type = type.GetDefinition (); // DropGenericTypeArguments (type);
-               parent = parent.GetDefinition (); // DropGenericTypeArguments (parent);
-
-               if (type == parent)
+               if (type.MemberDefinition == parent)
                        return false;
 
                type = type.DeclaringType;
                while (type != null) {
-                       if (type.GetDefinition () == parent)
+                       if (type.MemberDefinition == parent)
                                return true;
 
                        type = type.DeclaringType;
@@ -782,11 +890,6 @@ namespace Mono.CSharp
                return false;
        }
 
-       public static bool IsSpecialType (TypeSpec t)
-       {
-               return t == arg_iterator_type || t == typed_reference_type;
-       }
-
        public static TypeSpec GetElementType (TypeSpec t)
        {
                return ((ElementTypeSpec)t).Element;
@@ -800,91 +903,20 @@ namespace Mono.CSharp
                return t is ElementTypeSpec;
        }
 
-       static NumberFormatInfo nf_provider = CultureInfo.CurrentCulture.NumberFormat;
-
-       // This is a custom version of Convert.ChangeType() which works
-       // with the TypeBuilder defined types when compiling corlib.
-       public static object ChangeType (object value, TypeSpec targetType, out bool error)
-       {
-               IConvertible convert_value = value as IConvertible;
-               
-               if (convert_value == null){
-                       error = true;
-                       return null;
-               }
-               
-               //
-               // We cannot rely on build-in type conversions as they are
-               // more limited than what C# supports.
-               // See char -> float/decimal/double conversion
-               //
-               error = false;
-               try {
-                       if (targetType == TypeManager.bool_type)
-                               return convert_value.ToBoolean (nf_provider);
-                       if (targetType == TypeManager.byte_type)
-                               return convert_value.ToByte (nf_provider);
-                       if (targetType == TypeManager.char_type)
-                               return convert_value.ToChar (nf_provider);
-                       if (targetType == TypeManager.short_type)
-                               return convert_value.ToInt16 (nf_provider);
-                       if (targetType == TypeManager.int32_type)
-                               return convert_value.ToInt32 (nf_provider);
-                       if (targetType == TypeManager.int64_type)
-                               return convert_value.ToInt64 (nf_provider);
-                       if (targetType == TypeManager.sbyte_type)
-                               return convert_value.ToSByte (nf_provider);
-
-                       if (targetType == TypeManager.decimal_type) {
-                               if (convert_value.GetType () == typeof (char))
-                                       return (decimal) convert_value.ToInt32 (nf_provider);
-                               return convert_value.ToDecimal (nf_provider);
-                       }
-
-                       if (targetType == TypeManager.double_type) {
-                               if (convert_value.GetType () == typeof (char))
-                                       return (double) convert_value.ToInt32 (nf_provider);
-                               return convert_value.ToDouble (nf_provider);
-                       }
-
-                       if (targetType == TypeManager.float_type) {
-                               if (convert_value.GetType () == typeof (char))
-                                       return (float)convert_value.ToInt32 (nf_provider);
-                               return convert_value.ToSingle (nf_provider);
-                       }
-
-                       if (targetType == TypeManager.string_type)
-                               return convert_value.ToString (nf_provider);
-                       if (targetType == TypeManager.ushort_type)
-                               return convert_value.ToUInt16 (nf_provider);
-                       if (targetType == TypeManager.uint32_type)
-                               return convert_value.ToUInt32 (nf_provider);
-                       if (targetType == TypeManager.uint64_type)
-                               return convert_value.ToUInt64 (nf_provider);
-                       if (targetType == TypeManager.object_type)
-                               return value;
-
-                       error = true;
-               } catch {
-                       error = true;
-               }
-               return null;
-       }
-
        /// <summary>
        ///   Utility function that can be used to probe whether a type
        ///   is managed or not.  
        /// </summary>
-       public static bool VerifyUnmanaged (CompilerContext ctx, TypeSpec t, Location loc)
+       public static bool VerifyUnmanaged (ModuleContainer rc, TypeSpec t, Location loc)
        {
-               while (t.IsPointer)
-                       t = GetElementType (t);
-
-               if (IsUnmanagedType (t))
+               if (t.IsUnmanaged)
                        return true;
 
-               ctx.Report.SymbolRelatedToPreviousError (t);
-               ctx.Report.Error (208, loc,
+               while (t.IsPointer)
+                       t = ((ElementTypeSpec) t).Element;
+
+               rc.Compiler.Report.SymbolRelatedToPreviousError (t);
+               rc.Compiler.Report.Error (208, loc,
                        "Cannot take the address of, get the size of, or declare a pointer to a managed type `{0}'",
                        CSharpName (t));
 
@@ -903,12 +935,6 @@ namespace Mono.CSharp
                return type.IsGeneric;
        }
 
-       // TODO: Implement correctly
-       public static bool ContainsGenericParameters (TypeSpec type)
-       {
-               return type.GetMetaInfo ().ContainsGenericParameters;
-       }
-
        public static TypeSpec[] GetTypeArguments (TypeSpec t)
        {
                // TODO: return empty array !!
@@ -923,11 +949,6 @@ namespace Mono.CSharp
        {
                return type == parent || type.MemberDefinition == parent.MemberDefinition;
        }
-
-       public static bool IsNullableType (TypeSpec t)
-       {
-               return generic_nullable_type == t.GetDefinition ();
-       }
 #endregion
 }