mcs/error recovery: handle errors caused by closing braces after a statement expressi...
[mono.git] / mcs / mcs / typemanager.cs
index 96527624b230e04798c0a2d8e9f917e86850662e..b6243c8cea2eac0ad8d6238dc9821b603470ccaf 100644 (file)
@@ -3,65 +3,67 @@
 //
 // 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 compiler will not work)
+       // All compiler built-in types (they have to exist otherwise the compiler will not work)
        //
-       public class BuildinTypes
+       public class BuiltinTypes
        {
-               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;
+               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 BuildinTypeSpec Dynamic;
+               public readonly BuiltinTypeSpec Dynamic;
 
                // Predefined operators tables
                public readonly Binary.PredefinedOperator[] OperatorsBinaryStandard;
@@ -73,48 +75,48 @@ namespace Mono.CSharp
                public readonly TypeSpec[] BinaryPromotionsTypes;
                public readonly TypeSpec[] SwitchUserTypes;
 
-               readonly BuildinTypeSpec[] types;
+               readonly BuiltinTypeSpec[] types;
 
-               public BuildinTypes ()
+               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.Void, "System", "Void", BuildinTypeSpec.Type.Other);
-                       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.Other);
-                       RuntimeTypeHandle = new BuildinTypeSpec (MemberKind.Struct, "System", "RuntimeTypeHandle", BuildinTypeSpec.Type.Other);
+                       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 BuildinTypeSpec ("dynamic", BuildinTypeSpec.Type.Dynamic);
+                       Dynamic = new BuiltinTypeSpec ("dynamic", BuiltinTypeSpec.Type.Dynamic);
 
                        OperatorsBinaryStandard = Binary.CreateStandardOperatorsTable (this);
                        OperatorsBinaryEquality = Binary.CreateEqualityOperatorsTable (this);
@@ -125,14 +127,14 @@ namespace Mono.CSharp
                        BinaryPromotionsTypes = ConstantFold.CreateBinaryPromotionsTypes (this);
                        SwitchUserTypes = Switch.CreateSwitchUserTypes (this);
 
-                       types = new BuildinTypeSpec[] {
+                       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 };
                }
 
-               public BuildinTypeSpec[] AllTypes {
+               public BuiltinTypeSpec[] AllTypes {
                        get {
                                return types;
                        }
@@ -142,14 +144,14 @@ namespace Mono.CSharp
                {
                        var ctx = module.Compiler;
                        foreach (var p in types) {
-                               var found = PredefinedType.Resolve (module, p.Kind, p.Namespace, p.Name, p.Arity, Location.Null);
+                               var found = PredefinedType.Resolve (module, p.Kind, p.Namespace, p.Name, p.Arity);
                                if (found == null || found == p)
                                        continue;
 
-                               var tc = found.MemberDefinition as TypeContainer;
+                               var tc = found.MemberDefinition as TypeDefinition;
                                if (tc != null) {
                                        var ns = module.GlobalRootNamespace.GetNamespace (p.Namespace, false);
-                                       ns.ReplaceTypeWithPredefined (found, p);
+                                       ns.SetBuiltinType (p);
 
                                        tc.SetPredefinedSpec (p);
                                        p.SetDefinition (found);
@@ -174,6 +176,7 @@ namespace Mono.CSharp
        class PredefinedTypes
        {
                public readonly PredefinedType ArgIterator;
+               public readonly PredefinedType TypedReference;
                public readonly PredefinedType MarshalByRefObject;
                public readonly PredefinedType RuntimeHelpers;
                public readonly PredefinedType IAsyncResult;
@@ -206,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
@@ -215,9 +219,19 @@ 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)
                {
-                       var TypedReference = new PredefinedType (module, MemberKind.Struct, "System", "TypedReference");
+                       TypedReference = new PredefinedType (module, MemberKind.Struct, "System", "TypedReference");
                        ArgIterator = new PredefinedType (module, MemberKind.Struct, "System", "ArgIterator");
 
                        MarshalByRefObject = new PredefinedType (module, MemberKind.Class, "System", "MarshalByRefObject");
@@ -244,6 +258,7 @@ namespace Mono.CSharp
 
                        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");
@@ -255,6 +270,13 @@ 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
@@ -279,12 +301,27 @@ namespace Mono.CSharp
 
                        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;
@@ -316,7 +353,6 @@ namespace Mono.CSharp
                public readonly PredefinedMember<MethodSpec> StringInequal;
                public readonly PredefinedMember<MethodSpec> StructLayoutAttributeCtor;
                public readonly PredefinedMember<FieldSpec> StructLayoutCharSet;
-               public readonly PredefinedMember<FieldSpec> StructLayoutPack;
                public readonly PredefinedMember<FieldSpec> StructLayoutSize;
                public readonly PredefinedMember<MethodSpec> TypeGetTypeFromHandle;
 
@@ -324,11 +360,48 @@ namespace Mono.CSharp
                {
                        var types = module.PredefinedTypes;
                        var atypes = module.PredefinedAttributes;
-                       var btypes = module.Compiler.BuildinTypes;
+                       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)));
@@ -448,9 +521,6 @@ namespace Mono.CSharp
                        StructLayoutCharSet = new PredefinedMember<FieldSpec> (module, atypes.StructLayout, "CharSet",
                                MemberKind.Field, types.CharSet);
 
-                       StructLayoutPack = new PredefinedMember<FieldSpec> (module, atypes.StructLayout,
-                               MemberFilter.Field ("Pack", btypes.Int));
-
                        StructLayoutSize = new PredefinedMember<FieldSpec> (module, atypes.StructLayout,
                                MemberFilter.Field ("Size", btypes.Int));
 
@@ -481,7 +551,7 @@ namespace Mono.CSharp
                        this.ns = ns;
                }
 
-               public PredefinedType (BuildinTypeSpec type)
+               public PredefinedType (BuiltinTypeSpec type)
                {
                        this.kind = type.Kind;
                        this.name = type.Name;
@@ -528,14 +598,8 @@ namespace Mono.CSharp
                        if (type != null)
                                return true;
 
-                       Namespace type_ns = module.GlobalRootNamespace.GetNamespace (ns, true);
-                       var te = type_ns.LookupType (module, name, arity, true, Location.Null);
-                       if (te == null || 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 ()
@@ -543,33 +607,87 @@ 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, 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) {
-                               if (type.Kind == MemberKind.Struct && kind == MemberKind.Void && type.MemberDefinition is TypeContainer) {
-                                       // Void is declared as struct but we keep it internally as
-                                       // special kind, the swap will be done by caller
+                       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 {
-                                       module.Compiler.Report.Error (520, loc, "The predefined type `{0}.{1}' is not declared correctly", ns, name);
-                                       return null;
+                                       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;
                        }
 
-                       return type;
+                       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 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;
                }
@@ -581,8 +699,8 @@ namespace Mono.CSharp
                T member;
                TypeSpec declaring_type;
                readonly PredefinedType declaring_type_predefined;
-               readonly PredefinedType[] parameters_predefined;
                MemberFilter filter;
+               readonly Func<TypeSpec[]> filter_builder;
 
                public PredefinedMember (ModuleContainer module, PredefinedType type, MemberFilter filter)
                {
@@ -606,10 +724,27 @@ namespace Mono.CSharp
                public PredefinedMember (ModuleContainer module, PredefinedType type, string name, MemberKind kind, params PredefinedType[] types)
                        : this (module, type, new MemberFilter (name, 0, kind, null, null))
                {
-                       parameters_predefined = types;
+                       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;
+                       };
+               }
+
+               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;
                }
 
-               public PredefinedMember (ModuleContainer module, BuildinTypeSpec type, string name, params TypeSpec[] types)
+               public PredefinedMember (ModuleContainer module, BuiltinTypeSpec type, string name, params TypeSpec[] types)
                        : this (module, type, MemberFilter.Method (name, 0, ParametersCompiled.CreateFullyResolved (types), null))
                {
                }
@@ -626,20 +761,14 @@ namespace Mono.CSharp
                                declaring_type = declaring_type_predefined.TypeSpec;
                        }
 
-                       if (parameters_predefined != null) {
-                               TypeSpec[] types = new TypeSpec [parameters_predefined.Length];
-                               for (int i = 0; i < types.Length; ++i) {
-                                       var p = parameters_predefined [i];
-                                       if (!p.Define ())
-                                               return null;
-
-                                       types[i] = p.TypeSpec;
-                               }
+                       if (filter_builder != null) {
+                               var types = filter_builder ();
 
                                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);
+                                       filter = new MemberFilter (filter.Name, filter.Arity, filter.Kind,
+                                               ParametersCompiled.CreateFullyResolved (types), filter.MemberType);
                        }
 
                        member = MemberCache.FindMember (declaring_type, filter, BindingRestriction.DeclaredOnly) as T;
@@ -661,20 +790,13 @@ namespace Mono.CSharp
                                return member;
 
                        if (declaring_type == null) {
-                               if (declaring_type_predefined.Resolve (loc) == null)
+                               if (declaring_type_predefined.Resolve () == null)
                                        return null;
                        }
 
-                       if (parameters_predefined != null) {
-                               TypeSpec[] types = new TypeSpec[parameters_predefined.Length];
-                               for (int i = 0; i < types.Length; ++i) {
-                                       var p = parameters_predefined[i];
-                                       types[i] = p.Resolve (loc);
-                                       if (types[i] == null)
-                                               return null;
-                               }
-
-                               filter = new MemberFilter (filter.Name, filter.Arity, filter.Kind, ParametersCompiled.CreateFullyResolved (types), filter.MemberType);
+                       if (filter_builder != null) {
+                               filter = new MemberFilter (filter.Name, filter.Arity, filter.Kind,
+                                       ParametersCompiled.CreateFullyResolved (filter_builder ()), filter.MemberType);
                        }
 
                        string method_args = null;
@@ -722,63 +844,7 @@ namespace Mono.CSharp
        {
                return mb.GetSignatureForError ();
        }
-
-       // Obsolete
-       public static bool IsDelegateType (TypeSpec t)
-       {
-               return t.IsDelegate;
-       }
-       
-       // Obsolete
-       public static bool IsEnumType (TypeSpec t)
-       {
-               return t.IsEnum;
-       }
-
-       //
-       // Whether a type is unmanaged.  This is used by the unsafe code (25.2)
-       //
-       public static bool IsUnmanagedType (TypeSpec t)
-       {
-               var ds = t.MemberDefinition as DeclSpace;
-               if (ds != null)
-                       return ds.IsUnmanagedType ();
-
-               if (t.Kind == MemberKind.Void)
-                       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 (t.IsNested && t.DeclaringType.IsGenericOrParentIsGeneric)
-                       return false;
-
-               return true;
-       }
-
-       //
-       // 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)
-       {
-               if (t.IsGenericParameter)
-                       return ((TypeParameterSpec) t).IsValueType;
-
-               return t.IsStruct || IsEnumType (t);
-       }
-
        public static bool IsFamilyAccessible (TypeSpec type, TypeSpec parent)
        {
 //             TypeParameter tparam = LookupTypeParameter (type);
@@ -843,12 +909,12 @@ namespace Mono.CSharp
        /// </summary>
        public static bool VerifyUnmanaged (ModuleContainer rc, TypeSpec t, Location loc)
        {
-               while (t.IsPointer)
-                       t = GetElementType (t);
-
-               if (IsUnmanagedType (t))
+               if (t.IsUnmanaged)
                        return true;
 
+               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}'",