Nothing to see here
[mono.git] / mcs / mcs / generic.cs
index e6fcb7fbc0c940299d49ff799d7eac7e98fe65c6..fa210289ecf3efe896b27a4421e47b52f5cfb926 100644 (file)
@@ -5,10 +5,10 @@
 //          Miguel de Icaza (miguel@ximian.com)
 //          Marek Safar (marek.safar@gmail.com)
 //
-// Licensed under the terms of the GNU GPL
+// Dual licensed under the terms of the MIT X11 or GNU GPL
 //
-// (C) 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
-// (C) 2004 Novell, Inc
+// Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
+// Copyright 2004-2008 Novell, Inc
 //
 using System;
 using System.Reflection;
@@ -182,7 +182,7 @@ namespace Mono.CSharp {
                        if (resolved)
                                return true;
 
-                       iface_constraints = new ArrayList ();
+                       iface_constraints = new ArrayList (2);  // TODO: Too expensive allocation
                        type_param_constraints = new ArrayList ();
 
                        foreach (object obj in constraints) {
@@ -243,8 +243,7 @@ namespace Mono.CSharp {
                                if ((expr == null) || (expr.Type == null))
                                        return false;
 
-                               // TODO: It's aleady done in ResolveAsBaseTerminal
-                               if (!ec.GenericDeclContainer.AsAccessible (fn.Type, ec.GenericDeclContainer.ModFlags)) {
+                               if (!ec.GenericDeclContainer.IsAccessibleAs (fn.Type)) {
                                        Report.SymbolRelatedToPreviousError (fn.Type);
                                        Report.Error (703, loc,
                                                "Inconsistent accessibility: constraint type `{0}' is less accessible than `{1}'",
@@ -257,11 +256,10 @@ namespace Mono.CSharp {
                                        type_param_constraints.Add (expr);
                                else if (expr.IsInterface)
                                        iface_constraints.Add (expr);
-                               else if (class_constraint != null) {
+                               else if (class_constraint != null || iface_constraints.Count != 0) {
                                        Report.Error (406, loc,
-                                                     "`{0}': the class constraint for `{1}' " +
-                                                     "must come before any other constraints.",
-                                                     expr.Name, name);
+                                               "The class type constraint `{0}' must be listed before any other constraints. Consider moving type constraint to the beginning of the constraint list",
+                                               expr.GetSignatureForError ());
                                        return false;
                                } else if (HasReferenceTypeConstraint || HasValueTypeConstraint) {
                                        Report.Error (450, loc, "`{0}': cannot specify both " +
@@ -346,26 +344,52 @@ namespace Mono.CSharp {
                        else
                                effective_base_type = TypeManager.object_type;
 
+                       if ((attrs & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0)
+                               attrs |= GenericParameterAttributes.DefaultConstructorConstraint;
+
                        resolved = true;
                        return true;
                }
 
-               bool CheckTypeParameterConstraints (TypeParameter tparam, Hashtable seen)
+               bool CheckTypeParameterConstraints (TypeParameter tparam, ref TypeExpr prevConstraint, ArrayList seen)
                {
-                       seen.Add (tparam, true);
+                       seen.Add (tparam);
 
                        Constraints constraints = tparam.Constraints;
                        if (constraints == null)
                                return true;
 
                        if (constraints.HasValueTypeConstraint) {
-                               Report.Error (456, loc, "Type parameter `{0}' has " +
-                                             "the `struct' constraint, so it cannot " +
-                                             "be used as a constraint for `{1}'",
-                                             tparam.Name, name);
+                               Report.Error (456, loc,
+                                       "Type parameter `{0}' has the `struct' constraint, so it cannot be used as a constraint for `{1}'",
+                                       tparam.Name, name);
                                return false;
                        }
 
+                       //
+                       //  Checks whether there are no conflicts between type parameter constraints
+                       //
+                       //   class Foo<T, U>
+                       //      where T : A
+                       //      where U : A, B  // A and B are not convertible
+                       //
+                       if (constraints.HasClassConstraint) {
+                               if (prevConstraint != null) {
+                                       Type t2 = constraints.ClassConstraint;
+                                       TypeExpr e2 = constraints.class_constraint;
+
+                                       if (!Convert.ImplicitReferenceConversionExists (prevConstraint, t2) &&
+                                               !Convert.ImplicitReferenceConversionExists (e2, prevConstraint.Type)) {
+                                               Report.Error (455, loc,
+                                                       "Type parameter `{0}' inherits conflicting constraints `{1}' and `{2}'",
+                                                       name, TypeManager.CSharpName (prevConstraint.Type), TypeManager.CSharpName (t2));
+                                               return false;
+                                       }
+                               }
+
+                               prevConstraint = constraints.class_constraint;
+                       }
+
                        if (constraints.type_param_constraints == null)
                                return true;
 
@@ -373,11 +397,11 @@ namespace Mono.CSharp {
                                if (seen.Contains (expr.TypeParameter)) {
                                        Report.Error (454, loc, "Circular constraint " +
                                                      "dependency involving `{0}' and `{1}'",
-                                                     tparam.Name, expr.Name);
+                                                     tparam.Name, expr.GetSignatureForError ());
                                        return false;
                                }
 
-                               if (!CheckTypeParameterConstraints (expr.TypeParameter, seen))
+                               if (!CheckTypeParameterConstraints (expr.TypeParameter, ref prevConstraint, seen))
                                        return false;
                        }
 
@@ -403,10 +427,14 @@ namespace Mono.CSharp {
                                        return false;
                        }
 
-                       foreach (TypeParameterExpr expr in type_param_constraints) {
-                               Hashtable seen = new Hashtable ();
-                               if (!CheckTypeParameterConstraints (expr.TypeParameter, seen))
-                                       return false;
+                       if (type_param_constraints.Count != 0) {
+                               ArrayList seen = new ArrayList ();
+                               TypeExpr prev_constraint = class_constraint;
+                               foreach (TypeParameterExpr expr in type_param_constraints) {
+                                       if (!CheckTypeParameterConstraints (expr.TypeParameter, ref prev_constraint, seen))
+                                               return false;
+                                       seen.Clear ();
+                               }
                        }
 
                        for (int i = 0; i < iface_constraints.Count; ++i) {
@@ -426,66 +454,6 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               /// <summary>
-               ///   Check whether there are no conflicts in our type parameter constraints.
-               ///
-               ///   This is an example:
-               ///
-               ///   class Foo<T,U>
-               ///      where T : class
-               ///      where U : T, struct
-               /// </summary>
-               public bool CheckDependencies ()
-               {
-                       foreach (TypeParameterExpr expr in type_param_constraints) {
-                               if (!CheckDependencies (expr.TypeParameter))
-                                       return false;
-                       }
-
-                       return true;
-               }
-
-               bool CheckDependencies (TypeParameter tparam)
-               {
-                       Constraints constraints = tparam.Constraints;
-                       if (constraints == null)
-                               return true;
-
-                       if (HasValueTypeConstraint && constraints.HasClassConstraint) {
-                               Report.Error (455, loc, "Type parameter `{0}' inherits " +
-                                             "conflicting constraints `{1}' and `{2}'",
-                                             name, TypeManager.CSharpName (constraints.ClassConstraint),
-                                             "System.ValueType");
-                               return false;
-                       }
-
-                       if (HasClassConstraint && constraints.HasClassConstraint) {
-                               Type t1 = ClassConstraint;
-                               TypeExpr e1 = class_constraint;
-                               Type t2 = constraints.ClassConstraint;
-                               TypeExpr e2 = constraints.class_constraint;
-
-                               if (!Convert.ImplicitReferenceConversionExists (e1, t2) &&
-                                   !Convert.ImplicitReferenceConversionExists (e2, t1)) {
-                                       Report.Error (455, loc,
-                                                     "Type parameter `{0}' inherits " +
-                                                     "conflicting constraints `{1}' and `{2}'",
-                                                     name, TypeManager.CSharpName (t1), TypeManager.CSharpName (t2));
-                                       return false;
-                               }
-                       }
-
-                       if (constraints.type_param_constraints == null)
-                               return true;
-
-                       foreach (TypeParameterExpr expr in constraints.type_param_constraints) {
-                               if (!CheckDependencies (expr.TypeParameter))
-                                       return false;
-                       }
-
-                       return true;
-               }
-
                public override GenericParameterAttributes Attributes {
                        get { return attrs; }
                }
@@ -536,7 +504,7 @@ namespace Mono.CSharp {
                ///   method.  To do that, we're called on each of the implementing method's
                ///   type parameters.
                /// </summary>
-               public bool CheckInterfaceMethod (GenericConstraints gc)
+               public bool AreEqual (GenericConstraints gc)
                {
                        if (gc.Attributes != attrs)
                                return false;
@@ -605,11 +573,12 @@ namespace Mono.CSharp {
        /// </summary>
        public class TypeParameter : MemberCore, IMemberContainer
        {
+               static readonly string[] attribute_target = new string [] { "type parameter" };
+               
                string name;
                DeclSpace decl;
                GenericConstraints gc;
                Constraints constraints;
-               Location loc;
                GenericTypeParameterBuilder type;
                MemberCache member_cache;
 
@@ -620,7 +589,6 @@ namespace Mono.CSharp {
                        this.name = name;
                        this.decl = decl;
                        this.constraints = constraints;
-                       this.loc = loc;
                }
 
                public GenericConstraints GenericConstraints {
@@ -730,7 +698,7 @@ namespace Mono.CSharp {
 
                        if (implementing != null) {
                                if (is_override && (constraints != null)) {
-                                       Report.Error (460, loc,
+                                       Report.Error (460, Location,
                                                "`{0}': Cannot specify constraints for overrides or explicit interface implementation methods",
                                                TypeManager.CSharpSignature (builder));
                                        return false;
@@ -751,7 +719,7 @@ namespace Mono.CSharp {
                                if (constraints != null) {
                                        if (temp_gc == null)
                                                ok = false;
-                                       else if (!constraints.CheckInterfaceMethod (gc))
+                                       else if (!constraints.AreEqual (gc))
                                                ok = false;
                                } else {
                                        if (!is_override && (temp_gc != null))
@@ -762,7 +730,7 @@ namespace Mono.CSharp {
                                        Report.SymbolRelatedToPreviousError (implementing);
 
                                        Report.Error (
-                                               425, loc, "The constraints for type " +
+                                               425, Location, "The constraints for type " +
                                                "parameter `{0}' of method `{1}' must match " +
                                                "the constraints for type parameter `{2}' " +
                                                "of interface method `{3}'. Consider using " +
@@ -786,8 +754,8 @@ namespace Mono.CSharp {
                        if (gc == null)
                                return true;
 
-                       if (gc.HasClassConstraint)
-                               type.SetBaseTypeConstraint (gc.ClassConstraint);
+                       if (gc.HasClassConstraint || gc.HasValueTypeConstraint)
+                               type.SetBaseTypeConstraint (gc.EffectiveBaseClass);
 
                        type.SetInterfaceConstraints (gc.InterfaceConstraints);
                        type.SetGenericParameterAttributes (gc.Attributes);
@@ -796,23 +764,6 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               /// <summary>
-               ///   Check whether there are no conflicts in our type parameter constraints.
-               ///
-               ///   This is an example:
-               ///
-               ///   class Foo<T,U>
-               ///      where T : class
-               ///      where U : T, struct
-               /// </summary>
-               public bool CheckDependencies ()
-               {
-                       if (constraints != null)
-                               return constraints.CheckDependencies ();
-
-                       return true;
-               }
-
                /// <summary>
                ///   This is called for each part of a partial generic type definition.
                ///
@@ -835,7 +786,7 @@ namespace Mono.CSharp {
                                return false;
 
                        if (constraints != null) 
-                               return constraints.CheckInterfaceMethod (new_constraints);
+                               return constraints.AreEqual (new_constraints);
 
                        constraints = new_constraints;
                        return true;
@@ -873,13 +824,13 @@ namespace Mono.CSharp {
 
                public override AttributeTargets AttributeTargets {
                        get {
-                               return (AttributeTargets) AttributeTargets.GenericParameter;
+                               return AttributeTargets.GenericParameter;
                        }
                }
 
                public override string[] ValidAttributeTargets {
                        get {
-                               return new string [] { "type parameter" };
+                               return attribute_target;
                        }
                }
 
@@ -909,7 +860,7 @@ namespace Mono.CSharp {
 
                MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
                {
-                       return FindMembers (mt, bf, null, null);
+                       throw new NotSupportedException ();
                }
 
                public MemberCache MemberCache {
@@ -1046,7 +997,7 @@ namespace Mono.CSharp {
                                if (t == null)
                                        return null;
                                if (t.IsGenericParameter)
-                                       return dargs [t.GenericParameterPosition];
+                                       return t.GenericParameterPosition < dargs.Length ? dargs [t.GenericParameterPosition] : t;
                                if (t.IsGenericType) {
                                        Type[] args = t.GetGenericArguments ();
                                        Type[] inflated = new Type [args.Length];
@@ -1089,18 +1040,6 @@ namespace Mono.CSharp {
        public class TypeParameterExpr : TypeExpr {
                TypeParameter type_parameter;
 
-               public override string Name {
-                       get {
-                               return type_parameter.Name;
-                       }
-               }
-
-               public override string FullName {
-                       get {
-                               return type_parameter.Name;
-                       }
-               }
-
                public TypeParameter TypeParameter {
                        get {
                                return type_parameter;
@@ -1115,8 +1054,13 @@ namespace Mono.CSharp {
 
                protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
                {
-                       type = type_parameter.Type;
+                       throw new NotSupportedException ();
+               }
 
+               public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
+               {
+                       type = type_parameter.Type;
+                       eclass = ExprClass.TypeParameter;
                        return this;
                }
 
@@ -1128,11 +1072,6 @@ namespace Mono.CSharp {
                {
                        return true;
                }
-
-               public void Error_CannotUseAsUnmanagedType (Location loc)
-               {
-                       Report.Error (-203, loc, "Can not use type parameter as unmanaged type");
-               }
        }
 
        /// <summary>
@@ -1145,7 +1084,6 @@ namespace Mono.CSharp {
                Type[] atypes;
                int dimension;
                bool has_type_args;
-               bool created;
                
                public TypeArguments (Location loc)
                {
@@ -1167,17 +1105,11 @@ namespace Mono.CSharp {
 
                public void Add (Expression type)
                {
-                       if (created)
-                               throw new InvalidOperationException ();
-
                        args.Add (type);
                }
 
                public void Add (TypeArguments new_args)
                {
-                       if (created)
-                               throw new InvalidOperationException ();
-
                        args.AddRange (new_args.args);
                }
 
@@ -1197,14 +1129,15 @@ namespace Mono.CSharp {
                                        continue;
                                }
                                SimpleName sn = args [i] as SimpleName;
-                               if (sn != null) {
+                               if (sn != null && !sn.HasTypeArguments) {
                                        ret [i] = new TypeParameterName (sn.Name, null, sn.Location);
                                        continue;
                                }
 
-                               Report.Error (81, Location, "Type parameter declaration " +
-                                             "must be an identifier not a type");
-                               return null;
+                               Expression expr = (Expression) args [i];
+                               // TODO: Wrong location
+                               Report.Error (81, Location, "Type parameter declaration must be an identifier not a type");
+                               ret [i] = new TypeParameterName (expr.GetSignatureForError (), null, expr.Location);
                        }
                        return ret;
                }
@@ -1300,9 +1233,10 @@ namespace Mono.CSharp {
                                        return false;
                                }
 
-                               if (te.Type.IsPointer) {
-                                       Report.Error (306, Location, "The type `{0}' may not be used " +
-                                                         "as a type argument", TypeManager.CSharpName (te.Type));
+                               if (te.Type.IsPointer || TypeManager.IsSpecialType (te.Type)) {
+                                       Report.Error (306, Location,
+                                               "The type `{0}' may not be used as a type argument",
+                                               TypeManager.CSharpName (te.Type));
                                        return false;
                                }
 
@@ -1345,7 +1279,6 @@ namespace Mono.CSharp {
        ///   An instantiation of a generic type.
        /// </summary>  
        public class ConstructedType : TypeExpr {
-               string full_name;
                FullNamedExpression name;
                TypeArguments args;
                Type[] gen_params, atypes;
@@ -1360,26 +1293,6 @@ namespace Mono.CSharp {
                        this.name = fname;
                        this.args = args;
 
-                       eclass = ExprClass.Type;
-                       full_name = name + "<" + args.ToString () + ">";
-               }
-
-               protected ConstructedType (TypeArguments args, Location l)
-               {
-                       loc = l;
-                       this.args = args;
-
-                       eclass = ExprClass.Type;
-               }
-
-               protected ConstructedType (TypeParameter[] type_params, Location l)
-               {
-                       loc = l;
-
-                       args = new TypeArguments (l);
-                       foreach (TypeParameter type_param in type_params)
-                               args.Add (new TypeParameterExpr (type_param, l));
-
                        eclass = ExprClass.Type;
                }
 
@@ -1387,12 +1300,16 @@ namespace Mono.CSharp {
                ///   This is used to construct the `this' type inside a generic type definition.
                /// </summary>
                public ConstructedType (Type t, TypeParameter[] type_params, Location l)
-                       : this (type_params, l)
                {
                        gt = t.GetGenericTypeDefinition ();
 
+                       args = new TypeArguments (l);
+                       foreach (TypeParameter type_param in type_params)
+                               args.Add (new TypeParameterExpr (type_param, l));
+
+                       this.loc = l;
                        this.name = new TypeExpression (gt, l);
-                       full_name = gt.FullName + "<" + args.ToString () + ">";
+                       eclass = ExprClass.Type;
                }
 
                /// <summary>
@@ -1401,12 +1318,10 @@ namespace Mono.CSharp {
                ///   generic type.
                /// </summary>          
                public ConstructedType (Type t, TypeArguments args, Location l)
-                       : this (args, l)
+                       : this ((FullNamedExpression)null, args, l)
                {
                        gt = t.GetGenericTypeDefinition ();
-
                        this.name = new TypeExpression (gt, l);
-                       full_name = gt.FullName + "<" + args.ToString () + ">";
                }
 
                public TypeArguments TypeArguments {
@@ -1415,7 +1330,7 @@ namespace Mono.CSharp {
 
                public override string GetSignatureForError ()
                {
-                       return TypeManager.RemoveGenericArity (gt.FullName) + "<" + args.GetSignatureForError () + ">";
+                       return TypeManager.CSharpName (type);
                }
 
                protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
@@ -1450,7 +1365,7 @@ namespace Mono.CSharp {
                        Type t = name.Type;
 
                        if (t == null) {
-                               Report.Error (246, loc, "Cannot find type `{0}'<...>", Name);
+                               Report.Error (246, loc, "Cannot find type `{0}'<...>", GetSignatureForError ());
                                return false;
                        }
 
@@ -1504,14 +1419,14 @@ namespace Mono.CSharp {
                        return ds.CheckAccessLevel (gt);
                }
 
-               public override bool AsAccessible (DeclSpace ds, int flags)
+               public override bool AsAccessible (DeclSpace ds)
                {
                        foreach (Type t in atypes) {
-                               if (!ds.AsAccessible (t, flags))
+                               if (!ds.IsAccessibleAs (t))
                                        return false;
                        }
 
-                       return ds.AsAccessible (gt, flags);
+                       return ds.IsAccessibleAs (gt);
                }
 
                public override bool IsClass {
@@ -1546,18 +1461,6 @@ namespace Mono.CSharp {
                {
                        return base.GetHashCode ();
                }
-
-               public override string Name {
-                       get {
-                               return full_name;
-                       }
-               }
-
-               public override string FullName {
-                       get {
-                               return full_name;
-                       }
-               }
        }
 
        public abstract class ConstraintChecker
@@ -1717,12 +1620,36 @@ namespace Mono.CSharp {
                        if (Convert.ImplicitStandardConversionExists (expr, ctype))
                                return true;
 
-                       Error_TypeMustBeConvertible (expr.Type, ctype, ptype);
+                       Report_SymbolRelatedToPreviousError ();
+                       Report.SymbolRelatedToPreviousError (expr.Type);
+
+                       if (TypeManager.IsNullableType (expr.Type) && ctype.IsInterface) {
+                               Report.Error (313, loc,
+                                       "The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. " +
+                                       "The nullable type `{0}' never satisfies interface constraint of type `{3}'",
+                                       TypeManager.CSharpName (expr.Type), TypeManager.CSharpName (ptype),
+                                       GetSignatureForError (), TypeManager.CSharpName (ctype));
+                       } else {
+                               Report.Error (309, loc,
+                                       "The type `{0}' must be convertible to `{1}' in order to " +
+                                       "use it as parameter `{2}' in the generic type or method `{3}'",
+                                       TypeManager.CSharpName (expr.Type), TypeManager.CSharpName (ctype),
+                                       TypeManager.CSharpName (ptype), GetSignatureForError ());
+                       }
                        return false;
                }
 
-               bool HasDefaultConstructor (Type atype)
+               static bool HasDefaultConstructor (Type atype)
                {
+                       TypeParameter tparam = TypeManager.LookupTypeParameter (atype);
+                       if (tparam != null) {
+                               if (tparam.GenericConstraints == null)
+                                       return false;
+                                               
+                               return tparam.GenericConstraints.HasConstructorConstraint || 
+                                       tparam.GenericConstraints.HasValueTypeConstraint;
+                       }
+               
                        if (atype.IsAbstract)
                                return false;
 
@@ -1748,25 +1675,16 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       TypeParameter tparam = TypeManager.LookupTypeParameter (atype);
-                       if (tparam != null) {
-                               if (tparam.GenericConstraints == null)
-                                       return false;
-                               else
-                                       return tparam.GenericConstraints.HasConstructorConstraint;
-                       }
-
-                       MemberList list = TypeManager.FindMembers (
-                               atype, MemberTypes.Constructor,
-                               BindingFlags.Public | BindingFlags.Instance |
-                               BindingFlags.DeclaredOnly, null, null);
+                       MemberInfo [] list = TypeManager.MemberLookup (null, null, atype, MemberTypes.Constructor,
+                               BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
+                               ConstructorInfo.ConstructorName, null);
 
-                       if (atype.IsAbstract || (list == null))
+                       if (list == null)
                                return false;
 
                        foreach (MethodBase mb in list) {
-                               ParameterData pd = TypeManager.GetParameterData (mb);
-                               if ((pd.Count == 0) && mb.IsPublic && !mb.IsStatic)
+                               AParametersCollection pd = TypeManager.GetParameterData (mb);
+                               if (pd.Count == 0)
                                        return true;
                        }
 
@@ -1776,17 +1694,6 @@ namespace Mono.CSharp {
                protected abstract string GetSignatureForError ();
                protected abstract void Report_SymbolRelatedToPreviousError ();
 
-               void Error_TypeMustBeConvertible (Type atype, Type gc, Type ptype)
-               {
-                       Report_SymbolRelatedToPreviousError ();
-                       Report.SymbolRelatedToPreviousError (atype);
-                       Report.Error (309, loc, 
-                                     "The type `{0}' must be convertible to `{1}' in order to " +
-                                     "use it as parameter `{2}' in the generic type or method `{3}'",
-                                     TypeManager.CSharpName (atype), TypeManager.CSharpName (gc),
-                                     TypeManager.CSharpName (ptype), GetSignatureForError ());
-               }
-
                public static bool CheckConstraints (EmitContext ec, MethodBase definition,
                                                     MethodBase instantiated, Location loc)
                {
@@ -1856,11 +1763,11 @@ namespace Mono.CSharp {
        /// </summary>
        public class GenericMethod : DeclSpace
        {
-               Expression return_type;
+               FullNamedExpression return_type;
                Parameters parameters;
 
                public GenericMethod (NamespaceEntry ns, DeclSpace parent, MemberName name,
-                                     Expression return_type, Parameters parameters)
+                                     FullNamedExpression return_type, Parameters parameters)
                        : base (ns, parent, name, null)
                {
                        this.return_type = return_type;
@@ -1885,7 +1792,7 @@ namespace Mono.CSharp {
                ///   Define and resolve the type parameters.
                ///   We're called from Method.Define().
                /// </summary>
-               public bool Define (MethodBuilder mb, ToplevelBlock block)
+               public bool Define (MethodBuilder mb)
                {
                        TypeParameterName[] names = MemberName.TypeArguments.GetDeclarations ();
                        string[] snames = new string [names.Length];
@@ -1932,11 +1839,8 @@ namespace Mono.CSharp {
                                            ec, mb, implementing, is_override))
                                        return false;
 
-                       bool ok = true;
-                       foreach (Parameter p in parameters.FixedParameters){
-                               if (!p.Resolve (ec))
-                                       ok = false;
-                       }
+                       bool ok = parameters.Resolve (ec);
+
                        if ((return_type != null) && (return_type.ResolveAsTypeTerminal (ec, false) == null))
                                ok = false;
 
@@ -1952,11 +1856,6 @@ namespace Mono.CSharp {
                                OptAttributes.Emit ();
                }
 
-               public override bool DefineMembers ()
-               {
-                       return true;
-               }
-
                public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
                                                        MemberFilter filter, object criteria)
                {
@@ -1990,85 +1889,10 @@ namespace Mono.CSharp {
                }
        }
 
-       public class NullableType : TypeExpr
-       {
-               Expression underlying;
-
-               public NullableType (Expression underlying, Location l)
-               {
-                       this.underlying = underlying;
-                       loc = l;
-
-                       eclass = ExprClass.Type;
-               }
-
-               public NullableType (Type type, Location loc)
-                       : this (new TypeExpression (type, loc), loc)
-               { }
-
-               public override string Name {
-                       get { return underlying.ToString () + "?"; }
-               }
-
-               public override string FullName {
-                       get { return underlying.ToString () + "?"; }
-               }
-
-               protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
-               {
-                       TypeArguments args = new TypeArguments (loc);
-                       args.Add (underlying);
-
-                       ConstructedType ctype = new ConstructedType (TypeManager.generic_nullable_type, args, loc);
-                       return ctype.ResolveAsTypeTerminal (ec, false);
-               }
-       }
-
        public partial class TypeManager
        {
-               //
-               // A list of core types that the compiler requires or uses
-               //
                static public Type activator_type;
-               static public Type generic_ilist_type;
-               static public Type generic_icollection_type;
-               static public Type generic_ienumerator_type;
-               static public Type generic_ienumerable_type;
-               static public Type generic_nullable_type;
-
-               //
-               // These methods are called by code generated by the compiler
-               //
-               static public MethodInfo activator_create_instance;
-
-               static void InitGenericCoreTypes ()
-               {
-                       activator_type = CoreLookupType ("System", "Activator");
-
-                       generic_ilist_type = CoreLookupType (
-                               "System.Collections.Generic", "IList", 1);
-                       generic_icollection_type = CoreLookupType (
-                               "System.Collections.Generic", "ICollection", 1);
-                       generic_ienumerator_type = CoreLookupType (
-                               "System.Collections.Generic", "IEnumerator", 1);
-                       generic_ienumerable_type = CoreLookupType (
-                               "System.Collections.Generic", "IEnumerable", 1);
-                       generic_nullable_type = CoreLookupType (
-                               "System", "Nullable", 1);
-               }
-
-               static void InitGenericCodeHelpers ()
-               {
-                       // Activator
-                       activator_create_instance = GetMethod (
-                               activator_type, "CreateInstance", Type.EmptyTypes);
-               }
-
-               static Type CoreLookupType (string ns, string name, int arity)
-               {
-                       return CoreLookupType (ns, MemberName.MakeName (name, arity));
-               }
-
+       
                public static TypeContainer LookupGenericTypeContainer (Type t)
                {
                        t = DropGenericTypeArguments (t);
@@ -2201,76 +2025,6 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               /// <summary>
-               ///   Type inference.  Try to infer the type arguments from the params method
-               ///   `method', which is invoked with the arguments `arguments'.  This is used
-               ///   when resolving an Invocation or a DelegateInvocation and the user
-               ///   did not explicitly specify type arguments.
-               /// </summary>
-               public static bool InferParamsTypeArguments (EmitContext ec, ArrayList arguments,
-                                                            ref MethodBase method)
-               {
-                       if (!TypeManager.IsGenericMethod (method))
-                               return true;
-
-                       // if there are no arguments, there's no way to infer the type-arguments
-                       if (arguments == null || arguments.Count == 0)
-                               return false;
-
-                       ParameterData pd = TypeManager.GetParameterData (method);
-                       int pd_count = pd.Count;
-                       int arg_count = arguments.Count;
-
-                       if (pd_count == 0)
-                               return false;
-
-                       if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
-                               return false;
-
-                       if (pd_count - 1 > arg_count)
-                               return false;
-
-                       Type[] method_args = method.GetGenericArguments ();
-                       Type[] inferred_types = new Type [method_args.Length];
-
-                       //
-                       // If we have come this far, the case which
-                       // remains is when the number of parameters is
-                       // less than or equal to the argument count.
-                       //
-                       for (int i = 0; i < pd_count - 1; ++i) {
-                               Argument a = (Argument) arguments [i];
-
-                               if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
-                                       continue;
-
-                               Type pt = pd.ParameterType (i);
-                               Type at = a.Type;
-
-                               if (!TypeInferenceV2.UnifyType (pt, at, inferred_types))
-                                       return false;
-                       }
-
-                       Type element_type = TypeManager.GetElementType (pd.ParameterType (pd_count - 1));
-
-                       for (int i = pd_count - 1; i < arg_count; i++) {
-                               Argument a = (Argument) arguments [i];
-
-                               if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
-                                       continue;
-
-                               if (!TypeInferenceV2.UnifyType (element_type, a.Type, inferred_types))
-                                       return false;
-                       }
-
-                       for (int i = 0; i < inferred_types.Length; i++)
-                               if (inferred_types [i] == null)
-                                       return false;
-
-                       method = ((MethodInfo)method).MakeGenericMethod (inferred_types);
-                       return true;
-               }
-       
                /// <summary>
                ///   Type inference.  Try to infer the type arguments from `method',
                ///   which is invoked with the arguments `arguments'.  This is used
@@ -2296,7 +2050,7 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Type inference.
                /// </summary>
-               public static bool InferTypeArguments (ParameterData apd,
+               public static bool InferTypeArguments (AParametersCollection apd,
                                                       ref MethodBase method)
                {
                        if (!TypeManager.IsGenericMethod (method))
@@ -2354,7 +2108,7 @@ namespace Mono.CSharp {
 
                public override Type[] InferDelegateArguments (MethodBase method)
                {
-                       ParameterData pd = TypeManager.GetParameterData (method);
+                       AParametersCollection pd = TypeManager.GetParameterData (method);
                        if (arg_count != pd.Count)
                                return null;
 
@@ -2365,7 +2119,7 @@ namespace Mono.CSharp {
                        Type[] arg_types = (Type[])arguments.ToArray (typeof (Type));
 
                        for (int i = 0; i < arg_count; i++) {
-                               param_types[i] = pd.ParameterType (i);
+                               param_types[i] = pd.Types [i];
                        }
 
                        if (!InferTypeArguments (param_types, arg_types, inferred_types))
@@ -2376,7 +2130,7 @@ namespace Mono.CSharp {
 
                public override Type[] InferMethodArguments (EmitContext ec, MethodBase method)
                {
-                       ParameterData pd = TypeManager.GetParameterData (method);
+                       AParametersCollection pd = TypeManager.GetParameterData (method);
                        Type[] method_generic_args = method.GetGenericArguments ();
                        Type [] inferred_types = new Type [method_generic_args.Length];
                        Type[] arg_types = new Type [pd.Count];
@@ -2390,12 +2144,12 @@ namespace Mono.CSharp {
                                if (a.Expr is NullLiteral || a.Expr is MethodGroupExpr || a.Expr is AnonymousMethodExpression)
                                        continue;
 
-                               if (!TypeInferenceV2.UnifyType (pd.ParameterType (i), a.Type, inferred_types))
+                               if (!TypeInferenceV2.UnifyType (pd.Types [i], a.Type, inferred_types))
                                        return null;
                        }
 
                        if (pd.HasParams) {
-                               Type element_type = TypeManager.GetElementType (pd.ParameterType (a_count));
+                               Type element_type = TypeManager.GetElementType (pd.Types [a_count]);
                                for (int i = a_count; i < arg_count; i++) {
                                        Argument a = (Argument) arguments [i];
                                        if (a.Expr is NullLiteral || a.Expr is MethodGroupExpr || a.Expr is AnonymousMethodExpression)
@@ -2537,7 +2291,7 @@ namespace Mono.CSharp {
 
                public override Type[] InferDelegateArguments (MethodBase method)
                {
-                       ParameterData pd = TypeManager.GetParameterData (method);
+                       AParametersCollection pd = TypeManager.GetParameterData (method);
                        if (arg_count != pd.Count)
                                return null;
 
@@ -2567,7 +2321,7 @@ namespace Mono.CSharp {
                        if (!context.UnfixedVariableExists)
                                return Type.EmptyTypes;
 
-                       ParameterData pd = TypeManager.GetParameterData (method);
+                       AParametersCollection pd = TypeManager.GetParameterData (method);
                        if (!InferInPhases (ec, context, pd))
                                return null;
 
@@ -2577,15 +2331,35 @@ namespace Mono.CSharp {
                //
                // Implements method type arguments inference
                //
-               bool InferInPhases (EmitContext ec, TypeInferenceContext tic, ParameterData methodParameters)
+               bool InferInPhases (EmitContext ec, TypeInferenceContext tic, AParametersCollection methodParameters)
                {
+                       int params_arguments_start;
+                       if (methodParameters.HasParams) {
+                               params_arguments_start = methodParameters.Count - 1;
+                       } else {
+                               params_arguments_start = arg_count;
+                       }
+
+                       Type [] ptypes = methodParameters.Types;
+                       
                        //
                        // The first inference phase
                        //
+                       Type method_parameter = null;
                        for (int i = 0; i < arg_count; i++) {
-                               Type method_parameter = methodParameters.ParameterType (i);
+                               Argument a = (Argument) arguments [i];
+                               
+                               if (i < params_arguments_start) {
+                                       method_parameter = methodParameters.Types [i];
+                               } else if (i == params_arguments_start) {
+                                       if (arg_count == params_arguments_start + 1 && TypeManager.HasElementType (a.Type))
+                                               method_parameter = methodParameters.Types [params_arguments_start];
+                                       else
+                                               method_parameter = TypeManager.GetElementType (methodParameters.Types [params_arguments_start]);
 
-                               Argument a = (Argument) arguments[i];
+                                       ptypes = (Type[]) ptypes.Clone ();
+                                       ptypes [i] = method_parameter;
+                               }
 
                                //
                                // When a lambda expression, an anonymous method
@@ -2598,7 +2372,7 @@ namespace Mono.CSharp {
                                        continue;
                                }
 
-                               if (a.Expr.Type == TypeManager.null_type)
+                               if (a.Expr is NullLiteral)
                                        continue;
 
                                //
@@ -2612,16 +2386,16 @@ namespace Mono.CSharp {
                        // we don't need to call it in cycle
                        //
                        bool fixed_any = false;
-                       if (!tic.FixIndependentTypeArguments (methodParameters, ref fixed_any))
+                       if (!tic.FixIndependentTypeArguments (ptypes, ref fixed_any))
                                return false;
 
-                       return DoSecondPhase (ec, tic, methodParameters, !fixed_any);
+                       return DoSecondPhase (ec, tic, ptypes, !fixed_any);
                }
 
-               bool DoSecondPhase (EmitContext ec, TypeInferenceContext tic, ParameterData methodParameters, bool fixDependent)
+               bool DoSecondPhase (EmitContext ec, TypeInferenceContext tic, Type[] methodParameters, bool fixDependent)
                {
                        bool fixed_any = false;
-                       if (fixDependent && !tic.FixDependentTypes (methodParameters, ref fixed_any))
+                       if (fixDependent && !tic.FixDependentTypes (ref fixed_any))
                                return false;
 
                        // If no further unfixed type variables exist, type inference succeeds
@@ -2635,9 +2409,16 @@ namespace Mono.CSharp {
                        // contain unfixed type variables but the input types do not,
                        // an output type inference is made
                        for (int i = 0; i < arg_count; i++) {
-                               Type t_i = methodParameters.ParameterType (i);
-                               if (!TypeManager.IsDelegateType (t_i))
-                                       continue;
+                               
+                               // Align params arguments
+                               Type t_i = methodParameters [i >= methodParameters.Length ? methodParameters.Length - 1: i];
+                               
+                               if (!TypeManager.IsDelegateType (t_i)) {
+                                       if (TypeManager.DropGenericTypeArguments (t_i) != TypeManager.expression_type)
+                                               continue;
+
+                                       t_i = t_i.GetGenericArguments () [0];
+                               }
 
                                MethodInfo mi = Delegate.GetInvokeMethod (t_i, t_i);
                                Type rtype = mi.ReturnType;
@@ -2662,6 +2443,7 @@ namespace Mono.CSharp {
                readonly Type[] unfixed_types;
                readonly Type[] fixed_types;
                readonly ArrayList[] bounds;
+               bool failed;
                
                public TypeInferenceContext (Type[] typeArguments)
                {
@@ -2690,17 +2472,31 @@ namespace Mono.CSharp {
 
                void AddToBounds (Type t, int index)
                {
-                       ArrayList a = bounds[index];
+                       //
+                       // Some types cannot be used as type arguments
+                       //
+                       if (t == TypeManager.void_type || t.IsPointer)
+                               return;
+
+                       ArrayList a = bounds [index];
                        if (a == null) {
                                a = new ArrayList ();
-                               a.Add (t);
-                               bounds[index] = a;
-                               return;
+                               bounds [index] = a;
+                       } else {
+                               if (a.Contains (t))
+                                       return;
                        }
 
-                       if (a.Contains (t))
-                               return;
-
+                       //
+                       // SPEC: does not cover type inference using constraints
+                       //
+                       //if (TypeManager.IsGenericParameter (t)) {
+                       //    GenericConstraints constraints = TypeManager.GetTypeParameterConstraints (t);
+                       //    if (constraints != null) {
+                       //        //if (constraints.EffectiveBaseClass != null)
+                       //        //    t = constraints.EffectiveBaseClass;
+                       //    }
+                       //}
                        a.Add (t);
                }
                
@@ -2776,7 +2572,7 @@ namespace Mono.CSharp {
                // a, There is at least one type variable Xj that depends on Xi
                // b, Xi has a non-empty set of bounds
                // 
-               public bool FixDependentTypes (ParameterData methodParameters, ref bool fixed_any)
+               public bool FixDependentTypes (ref bool fixed_any)
                {
                        for (int i = 0; i < unfixed_types.Length; ++i) {
                                if (unfixed_types[i] == null)
@@ -2797,15 +2593,20 @@ namespace Mono.CSharp {
                //
                // All unfixed type variables Xi which depend on no Xj are fixed
                //
-               public bool FixIndependentTypeArguments (ParameterData methodParameters, ref bool fixed_any)
+               public bool FixIndependentTypeArguments (Type[] methodParameters, ref bool fixed_any)
                {
                        ArrayList types_to_fix = new ArrayList (unfixed_types);
-                       foreach (Type t in methodParameters.Types) {
+                       for (int i = 0; i < methodParameters.Length; ++i) {
+                               Type t = methodParameters[i];
                                if (t.IsGenericParameter)
                                        continue;
 
-                               if (!TypeManager.IsDelegateType (t))
-                                       continue;
+                               if (!TypeManager.IsDelegateType (t)) {
+                                       if (TypeManager.DropGenericTypeArguments (t) != TypeManager.expression_type)
+                                               continue;
+
+                                       t = t.GetGenericArguments () [0];
+                               }
 
                                MethodInfo invoke = Delegate.GetInvokeMethod (t, t);
                                Type rtype = invoke.ReturnType;
@@ -2846,6 +2647,9 @@ namespace Mono.CSharp {
                        if (unfixed_types[i] == null)
                                throw new InternalErrorException ("Type argument has been already fixed");
 
+                       if (failed)
+                               return false;
+
                        ArrayList candidates = (ArrayList)bounds [i];
                        if (candidates == null)
                                return false;
@@ -2856,27 +2660,33 @@ namespace Mono.CSharp {
                                return true;
                        }
 
-                       // TODO: Review, I think it is still wrong
-                       Type best_candidate = null;
-                       for (int ci = 0; ci < candidates.Count; ++ci) {
-                               TypeExpr candidate = new TypeExpression ((Type)candidates[ci], Location.Null);
-                               bool failed = false;
-                               for (int cii = 0; cii < candidates.Count; ++cii) {
+                       //
+                       // Determines a unique type from which there is
+                       // a standard implicit conversion to all the other
+                       // candidate types.
+                       //
+                       Type best_candidate = null;
+                       int cii;
+                       int candidates_count = candidates.Count;
+                       for (int ci = 0; ci < candidates_count; ++ci) {
+                               Type candidate = (Type)candidates [ci];
+                               for (cii = 0; cii < candidates_count; ++cii) {
                                        if (cii == ci)
                                                continue;
 
-                                       if (!Convert.ImplicitStandardConversionExists (candidate, (Type)candidates[cii])) {
-                                               failed = true;
+                                       if (!Convert.ImplicitConversionExists (null,
+                                               new TypeExpression ((Type)candidates [cii], Location.Null), candidate)) {
+                                               break;
                                        }
                                }
 
-                               if (failed)
+                               if (cii != candidates_count)
                                        continue;
 
                                if (best_candidate != null)
                                        return false;
 
-                               best_candidate = candidate.Type;
+                               best_candidate = candidate;
                        }
 
                        if (best_candidate == null)
@@ -2892,8 +2702,15 @@ namespace Mono.CSharp {
                //
                public Type InflateGenericArgument (Type parameter)
                {
-                       if (parameter.IsGenericParameter)
+                       if (parameter.IsGenericParameter) {
+                               //
+                               // Inflate method generic argument (MVAR) only
+                               //
+                               if (parameter.DeclaringMethod == null)
+                                       return parameter;
+
                                return fixed_types [parameter.GenericParameterPosition];
+                       }
 
                        if (parameter.IsGenericType) {
                                Type [] parameter_targs = parameter.GetGenericArguments ();
@@ -2931,7 +2748,7 @@ namespace Mono.CSharp {
                        }
 
                        // All generic input arguments have to be fixed
-                       ParameterData d_parameters = TypeManager.GetParameterData (invoke);
+                       AParametersCollection d_parameters = TypeManager.GetParameterData (invoke);
                        return AllTypesAreFixed (d_parameters.Types);
                }
                
@@ -2959,9 +2776,12 @@ namespace Mono.CSharp {
                //
                public int LowerBoundInference (Type u, Type v)
                {
-                       // Remove ref, out modifiers
-                       if (v.IsByRef)
-                               v = v.GetElementType ();
+                       // If V is one of the unfixed type arguments
+                       int pos = IsUnfixed (v);
+                       if (pos != -1) {
+                               AddToBounds (u, pos);
+                               return 1;
+                       }                       
 
                        // If U is an array type
                        if (u.IsArray) {
@@ -3019,6 +2839,8 @@ namespace Mono.CSharp {
                                u_candidates.AddRange (TypeManager.GetInterfaces (u));
 
                                Type open_v = v.GetGenericTypeDefinition ();
+                               Type [] unique_candidate_targs = null;
+                               Type [] ga_v = v.GetGenericArguments ();                        
                                foreach (Type u_candidate in u_candidates) {
                                        if (!u_candidate.IsGenericType || u_candidate.IsGenericTypeDefinition)
                                                continue;
@@ -3026,24 +2848,38 @@ namespace Mono.CSharp {
                                        if (TypeManager.DropGenericTypeArguments (u_candidate) != open_v)
                                                continue;
 
-                                       Type [] ga_u = u_candidate.GetGenericArguments ();
-                                       Type [] ga_v = v.GetGenericArguments ();
-                                       int score = 0;
-                                       for (int i = 0; i < ga_u.Length; ++i)
-                                               score += ExactInference (ga_u [i], ga_v [i]);
+                                       //
+                                       // The unique set of types U1..Uk means that if we have an interface C<T>,
+                                       // class U: C<int>, C<long> then no type inference is made when inferring
+                                       // from U to C<T> because T could be int or long
+                                       //
+                                       if (unique_candidate_targs != null) {
+                                               Type[] second_unique_candidate_targs = u_candidate.GetGenericArguments ();
+                                               if (TypeManager.IsEqual (unique_candidate_targs, second_unique_candidate_targs)) {
+                                                       unique_candidate_targs = second_unique_candidate_targs;
+                                                       continue;
+                                               }
+                                               
+                                               //
+                                               // This should always cause type inference failure
+                                               //
+                                               failed = true;
+                                               return 1;
+                                       }
 
-                                       return score > 0 ? 1 : 0;
+                                       unique_candidate_targs = u_candidate.GetGenericArguments ();
                                }
-                               return 0;
-                       }
 
-                       // If V is one of the unfixed type arguments
-                       int pos = IsUnfixed (v);
-                       if (pos == -1)
-                               return 0;
+                               if (unique_candidate_targs != null) {
+                                       int score = 0;
+                                       for (int i = 0; i < unique_candidate_targs.Length; ++i)
+                                               if (ExactInference (unique_candidate_targs [i], ga_v [i]) == 0)
+                                                       ++score;
+                                       return score;
+                               }
+                       }
 
-                       AddToBounds (u, pos);
-                       return 1;
+                       return 0;
                }
 
                //
@@ -3058,7 +2894,7 @@ namespace Mono.CSharp {
                                MethodInfo invoke = Delegate.GetInvokeMethod (t, t);
 
                                if (rt == null) {
-                                       ParameterData pd = TypeManager.GetParameterData (invoke);
+                                       AParametersCollection pd = TypeManager.GetParameterData (invoke);
                                        return ame.Parameters.Count == pd.Count ? 1 : 0;
                                }
 
@@ -3071,13 +2907,37 @@ namespace Mono.CSharp {
                                return LowerBoundInference (rt, rtype) + 1;
                        }
 
+                       //
+                       // if E is a method group and T is a delegate type or expression tree type
+                       // return type Tb with parameter types T1..Tk and return type Tb, and overload
+                       // resolution of E with the types T1..Tk yields a single method with return type U,
+                       // then a lower-bound inference is made from U for Tb.
+                       //
                        if (e is MethodGroupExpr) {
+                               // TODO: Or expression tree
+                               if (!TypeManager.IsDelegateType (t))
+                                       return 0;
+
                                MethodInfo invoke = Delegate.GetInvokeMethod (t, t);
                                Type rtype = invoke.ReturnType;
+#if MS_COMPATIBLE
+                               // Blablabla, because reflection does not work with dynamic types
+                               Type [] g_args = t.GetGenericArguments ();
+                               rtype = g_args [rtype.GenericParameterPosition];
+#endif
+
                                if (!TypeManager.IsGenericType (rtype))
                                        return 0;
-                               
+
+                               MethodGroupExpr mg = (MethodGroupExpr) e;
+                               ArrayList args = DelegateCreation.CreateDelegateMethodArguments (invoke, e.Location);
+                               mg = mg.OverloadResolve (ec, ref args, true, e.Location);
+                               if (mg == null)
+                                       return 0;
+
+                               // TODO: What should happen when return type is of generic type ?
                                throw new NotImplementedException ();
+//                             return LowerBoundInference (null, rtype) + 1;
                        }
 
                        //
@@ -3113,898 +2973,4 @@ namespace Mono.CSharp {
                        }
                }
        }
-
-       public abstract class Nullable
-       {
-               public sealed class NullableInfo
-               {
-                       public readonly Type Type;
-                       public readonly Type UnderlyingType;
-                       public readonly MethodInfo HasValue;
-                       public readonly MethodInfo Value;
-                       public readonly ConstructorInfo Constructor;
-
-                       public NullableInfo (Type type)
-                       {
-                               Type = type;
-                               UnderlyingType = TypeManager.GetTypeArguments (type) [0];
-
-                               PropertyInfo has_value_pi = TypeManager.GetProperty (type, "HasValue");
-                               PropertyInfo value_pi = TypeManager.GetProperty (type, "Value");
-
-                               HasValue = has_value_pi.GetGetMethod (false);
-                               Value = value_pi.GetGetMethod (false);
-                               Constructor = type.GetConstructor (new Type[] { UnderlyingType });
-                       }
-               }
-               
-               public class HasValue : Expression
-               {
-                       Expression expr;
-                       NullableInfo info;
-
-                       private HasValue (Expression expr)
-                       {
-                               this.expr = expr;
-                       }
-                       
-                       public static Expression Create (Expression expr, EmitContext ec)
-                       {
-                               return new HasValue (expr).Resolve (ec);
-                       }
-
-                       public override void Emit (EmitContext ec)
-                       {
-                               ((IMemoryLocation) expr).AddressOf (ec, AddressOp.LoadStore);
-                               ec.ig.EmitCall (OpCodes.Call, info.HasValue, null);
-                       }
-
-                       public override Expression DoResolve (EmitContext ec)
-                       {
-                               this.info = new NullableInfo (expr.Type);
-
-                               type = TypeManager.bool_type;
-                               eclass = expr.eclass;
-                               return this;
-                       }
-               }               
-
-               public class Unwrap : Expression, IMemoryLocation, IAssignMethod
-               {
-                       Expression expr;
-                       NullableInfo info;
-
-                       LocalTemporary temp;
-                       bool has_temp;
-
-                       protected Unwrap (Expression expr)
-                       {
-                               this.expr = expr;
-                               this.loc = expr.Location;
-                       }
-
-                       public static Unwrap Create (Expression expr, EmitContext ec)
-                       {
-                               return new Unwrap (expr).Resolve (ec) as Unwrap;
-                       }
-
-                       public override Expression DoResolve (EmitContext ec)
-                       {
-                               expr = expr.Resolve (ec);
-                               if (expr == null)
-                                       return null;
-
-                               temp = new LocalTemporary (expr.Type);
-
-                               info = new NullableInfo (expr.Type);
-                               type = info.UnderlyingType;
-                               eclass = expr.eclass;
-                               return this;
-                       }
-
-                       public override void Emit (EmitContext ec)
-                       {
-                               AddressOf (ec, AddressOp.LoadStore);
-                               ec.ig.EmitCall (OpCodes.Call, info.Value, null);
-                       }
-
-                       public void EmitCheck (EmitContext ec)
-                       {
-                               AddressOf (ec, AddressOp.LoadStore);
-                               ec.ig.EmitCall (OpCodes.Call, info.HasValue, null);
-                       }
-
-                       public void Store (EmitContext ec)
-                       {
-                               create_temp (ec);
-                       }
-
-                       void create_temp (EmitContext ec)
-                       {
-                               if ((temp != null) && !has_temp) {
-                                       expr.Emit (ec);
-                                       temp.Store (ec);
-                                       has_temp = true;
-                               }
-                       }
-
-                       public void AddressOf (EmitContext ec, AddressOp mode)
-                       {
-                               create_temp (ec);
-                               if (temp != null)
-                                       temp.AddressOf (ec, AddressOp.LoadStore);
-                               else
-                                       ((IMemoryLocation) expr).AddressOf (ec, AddressOp.LoadStore);
-                       }
-
-                       public void Emit (EmitContext ec, bool leave_copy)
-                       {
-                               create_temp (ec);
-                               if (leave_copy) {
-                                       if (temp != null)
-                                               temp.Emit (ec);
-                                       else
-                                               expr.Emit (ec);
-                               }
-
-                               Emit (ec);
-                       }
-
-                       public void EmitAssign (EmitContext ec, Expression source,
-                                               bool leave_copy, bool prepare_for_load)
-                       {
-                               InternalWrap wrap = new InternalWrap (source, info, loc);
-                               ((IAssignMethod) expr).EmitAssign (ec, wrap, leave_copy, false);
-                       }
-
-                       protected class InternalWrap : Expression
-                       {
-                               public Expression expr;
-                               public NullableInfo info;
-
-                               public InternalWrap (Expression expr, NullableInfo info, Location loc)
-                               {
-                                       this.expr = expr;
-                                       this.info = info;
-                                       this.loc = loc;
-
-                                       type = info.Type;
-                                       eclass = ExprClass.Value;
-                               }
-
-                               public override Expression DoResolve (EmitContext ec)
-                               {
-                                       return this;
-                               }
-
-                               public override void Emit (EmitContext ec)
-                               {
-                                       expr.Emit (ec);
-                                       ec.ig.Emit (OpCodes.Newobj, info.Constructor);
-                               }
-                       }
-               }
-
-               public class Wrap : Expression
-               {
-                       Expression expr;
-                       NullableInfo info;
-
-                       protected Wrap (Expression expr)
-                       {
-                               this.expr = expr;
-                               this.loc = expr.Location;
-                       }
-
-                       public static Wrap Create (Expression expr, EmitContext ec)
-                       {
-                               return new Wrap (expr).Resolve (ec) as Wrap;
-                       }
-
-                       public override Expression DoResolve (EmitContext ec)
-                       {
-                               expr = expr.Resolve (ec);
-                               if (expr == null)
-                                       return null;
-
-                               TypeExpr target_type = new NullableType (expr.Type, loc);
-                               target_type = target_type.ResolveAsTypeTerminal (ec, false);
-                               if (target_type == null)
-                                       return null;
-
-                               type = target_type.Type;
-                               info = new NullableInfo (type);
-                               eclass = ExprClass.Value;
-                               return this;
-                       }
-
-                       public override void Emit (EmitContext ec)
-                       {
-                               expr.Emit (ec);
-                               ec.ig.Emit (OpCodes.Newobj, info.Constructor);
-                       }
-               }
-
-               public class NullableLiteral : NullLiteral, IMemoryLocation {
-                       public NullableLiteral (Type target_type, Location loc)
-                               : base (loc)
-                       {
-                               this.type = target_type;
-
-                               eclass = ExprClass.Value;
-                       }
-               
-                       public override Expression DoResolve (EmitContext ec)
-                       {
-                               return this;
-                       }
-
-                       public override void Emit (EmitContext ec)
-                       {
-                               LocalTemporary value_target = new LocalTemporary (type);
-
-                               value_target.AddressOf (ec, AddressOp.Store);
-                               ec.ig.Emit (OpCodes.Initobj, type);
-                               value_target.Emit (ec);
-                       }
-
-                       public void AddressOf (EmitContext ec, AddressOp Mode)
-                       {
-                               LocalTemporary value_target = new LocalTemporary (type);
-                                       
-                               value_target.AddressOf (ec, AddressOp.Store);
-                               ec.ig.Emit (OpCodes.Initobj, type);
-                               ((IMemoryLocation) value_target).AddressOf (ec, Mode);
-                       }
-               }
-
-               public abstract class Lifted : Expression, IMemoryLocation
-               {
-                       Expression expr, underlying, wrap, null_value;
-                       Unwrap unwrap;
-
-                       protected Lifted (Expression expr, Location loc)
-                       {
-                               this.expr = expr;
-                               this.loc = loc;
-                       }
-
-                       public override Expression DoResolve (EmitContext ec)
-                       {
-                               expr = expr.Resolve (ec);
-                               if (expr == null)
-                                       return null;
-
-                               unwrap = Unwrap.Create (expr, ec);
-                               if (unwrap == null)
-                                       return null;
-
-                               underlying = ResolveUnderlying (unwrap, ec);
-                               if (underlying == null)
-                                       return null;
-
-                               wrap = Wrap.Create (underlying, ec);
-                               if (wrap == null)
-                                       return null;
-
-                               null_value = new NullableLiteral (wrap.Type, loc).Resolve (ec);
-                               if (null_value == null)
-                                       return null;
-
-                               type = wrap.Type;
-                               eclass = ExprClass.Value;
-                               return this;
-                       }
-
-                       protected abstract Expression ResolveUnderlying (Expression unwrap, EmitContext ec);
-
-                       public override void Emit (EmitContext ec)
-                       {
-                               ILGenerator ig = ec.ig;
-                               Label is_null_label = ig.DefineLabel ();
-                               Label end_label = ig.DefineLabel ();
-
-                               unwrap.EmitCheck (ec);
-                               ig.Emit (OpCodes.Brfalse, is_null_label);
-
-                               wrap.Emit (ec);
-                               ig.Emit (OpCodes.Br, end_label);
-
-                               ig.MarkLabel (is_null_label);
-                               null_value.Emit (ec);
-
-                               ig.MarkLabel (end_label);
-                       }
-
-                       public void AddressOf (EmitContext ec, AddressOp mode)
-                       {
-                               unwrap.AddressOf (ec, mode);
-                       }
-               }
-
-               public class LiftedConversion : Lifted
-               {
-                       public readonly bool IsUser;
-                       public readonly bool IsExplicit;
-                       public readonly Type TargetType;
-
-                       public LiftedConversion (Expression expr, Type target_type, bool is_user,
-                                                bool is_explicit, Location loc)
-                               : base (expr, loc)
-                       {
-                               this.IsUser = is_user;
-                               this.IsExplicit = is_explicit;
-                               this.TargetType = target_type;
-                       }
-
-                       protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
-                       {
-                               Type type = TypeManager.GetTypeArguments (TargetType) [0];
-
-                               if (IsUser) {
-                                       return Convert.UserDefinedConversion (ec, unwrap, type, loc, IsExplicit);
-                               } else {
-                                       if (IsExplicit)
-                                               return Convert.ExplicitConversion (ec, unwrap, type, loc);
-                                       else
-                                               return Convert.ImplicitConversion (ec, unwrap, type, loc);
-                               }
-                       }
-               }
-
-               public class LiftedUnaryOperator : Lifted
-               {
-                       public readonly Unary.Operator Oper;
-
-                       public LiftedUnaryOperator (Unary.Operator op, Expression expr, Location loc)
-                               : base (expr, loc)
-                       {
-                               this.Oper = op;
-                       }
-
-                       protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
-                       {
-                               return new Unary (Oper, unwrap, loc);
-                       }
-               }
-
-               public class LiftedConditional : Lifted
-               {
-                       Expression true_expr, false_expr;
-
-                       public LiftedConditional (Expression expr, Expression true_expr, Expression false_expr,
-                                                 Location loc)
-                               : base (expr, loc)
-                       {
-                               this.true_expr = true_expr;
-                               this.false_expr = false_expr;
-                       }
-
-                       protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
-                       {
-                               return new Conditional (unwrap, true_expr, false_expr);
-                       }
-               }
-
-               public class LiftedBinaryOperator : Binary
-               {
-                       Expression underlying, null_value, bool_wrap;
-                       Unwrap left_unwrap, right_unwrap;
-                       bool is_equality, is_comparision, is_boolean;
-
-                       public LiftedBinaryOperator (Binary.Operator op, Expression left, Expression right,
-                                                    Location loc)
-                               : base (op, left, right)
-                       {
-                               this.loc = loc;
-                       }
-
-                       public override Expression DoResolve (EmitContext ec)
-                       {
-                               if ((Oper == Binary.Operator.LogicalAnd) ||
-                                   (Oper == Binary.Operator.LogicalOr)) {
-                                       Error_OperatorCannotBeApplied ();
-                                       return null;
-                               }
-                               
-                               if (TypeManager.IsNullableType (left.Type)) {
-                                       left = left_unwrap = Unwrap.Create (left, ec);
-                                       if (left == null)
-                                               return null;
-                               }
-
-                               if (TypeManager.IsNullableType (right.Type)) {
-                                       right = right_unwrap = Unwrap.Create (right, ec);
-                                       if (right == null)
-                                               return null;
-                               }
-
-                               if (((Oper == Binary.Operator.BitwiseAnd) || (Oper == Binary.Operator.BitwiseOr)) &&
-                                   ((left.Type == TypeManager.bool_type) && (right.Type == TypeManager.bool_type))) {
-                                       Expression empty = new EmptyExpression (TypeManager.bool_type);
-                                       bool_wrap = Wrap.Create (empty, ec);
-                                       null_value = new NullableLiteral (bool_wrap.Type, loc).Resolve (ec);
-
-                                       type = bool_wrap.Type;
-                                       is_boolean = true;
-                               } else if ((Oper == Binary.Operator.Equality) || (Oper == Binary.Operator.Inequality)) {
-                                       if (!(left is NullLiteral) && !(right is NullLiteral)) {
-                                               underlying = new Binary (Oper, left, right).Resolve (ec);
-                                               if (underlying == null)
-                                                       return null;
-                                       }
-
-                                       type = TypeManager.bool_type;
-                                       is_equality = true;
-                               } else if ((Oper == Binary.Operator.LessThan) ||
-                                          (Oper == Binary.Operator.GreaterThan) ||
-                                          (Oper == Binary.Operator.LessThanOrEqual) ||
-                                          (Oper == Binary.Operator.GreaterThanOrEqual)) {
-                                       underlying = new Binary (Oper, left, right).Resolve (ec);
-                                       if (underlying == null)
-                                               return null;
-
-                                       type = TypeManager.bool_type;
-                                       is_comparision = true;
-                               } else {
-                                       underlying = new Binary (Oper, left, right).Resolve (ec);
-                                       if (underlying == null)
-                                               return null;
-
-                                       underlying = Wrap.Create (underlying, ec);
-                                       if (underlying == null)
-                                               return null;
-
-                                       type = underlying.Type;
-                                       null_value = new NullableLiteral (type, loc).Resolve (ec);
-                               }
-
-                               eclass = ExprClass.Value;
-                               return this;
-                       }
-
-                       void EmitBoolean (EmitContext ec)
-                       {
-                               ILGenerator ig = ec.ig;
-
-                               Label left_is_null_label = ig.DefineLabel ();
-                               Label right_is_null_label = ig.DefineLabel ();
-                               Label is_null_label = ig.DefineLabel ();
-                               Label wrap_label = ig.DefineLabel ();
-                               Label end_label = ig.DefineLabel ();
-
-                               if (left_unwrap != null) {
-                                       left_unwrap.EmitCheck (ec);
-                                       ig.Emit (OpCodes.Brfalse, left_is_null_label);
-                               }
-
-                               left.Emit (ec);
-                               ig.Emit (OpCodes.Dup);
-                               if ((Oper == Binary.Operator.BitwiseOr) || (Oper == Binary.Operator.LogicalOr))
-                                       ig.Emit (OpCodes.Brtrue, wrap_label);
-                               else
-                                       ig.Emit (OpCodes.Brfalse, wrap_label);
-
-                               if (right_unwrap != null) {
-                                       right_unwrap.EmitCheck (ec);
-                                       ig.Emit (OpCodes.Brfalse, right_is_null_label);
-                               }
-
-                               if ((Oper == Binary.Operator.LogicalAnd) || (Oper == Binary.Operator.LogicalOr))
-                                       ig.Emit (OpCodes.Pop);
-
-                               right.Emit (ec);
-                               if (Oper == Binary.Operator.BitwiseOr)
-                                       ig.Emit (OpCodes.Or);
-                               else if (Oper == Binary.Operator.BitwiseAnd)
-                                       ig.Emit (OpCodes.And);
-                               ig.Emit (OpCodes.Br, wrap_label);
-
-                               ig.MarkLabel (left_is_null_label);
-                               if (right_unwrap != null) {
-                                       right_unwrap.EmitCheck (ec);
-                                       ig.Emit (OpCodes.Brfalse, is_null_label);
-                               }
-
-                               right.Emit (ec);
-                               ig.Emit (OpCodes.Dup);
-                               if ((Oper == Binary.Operator.BitwiseOr) || (Oper == Binary.Operator.LogicalOr))
-                                       ig.Emit (OpCodes.Brtrue, wrap_label);
-                               else
-                                       ig.Emit (OpCodes.Brfalse, wrap_label);
-
-                               ig.MarkLabel (right_is_null_label);
-                               ig.Emit (OpCodes.Pop);
-                               ig.MarkLabel (is_null_label);
-                               null_value.Emit (ec);
-                               ig.Emit (OpCodes.Br, end_label);
-
-                               ig.MarkLabel (wrap_label);
-                               ig.Emit (OpCodes.Nop);
-                               bool_wrap.Emit (ec);
-                               ig.Emit (OpCodes.Nop);
-
-                               ig.MarkLabel (end_label);
-                       }
-
-                       void EmitEquality (EmitContext ec)
-                       {
-                               ILGenerator ig = ec.ig;
-
-                               // Given 'X? x;' for any value type X: 'x != null' is the same as 'x.HasValue'
-                               if (left is NullLiteral) {
-                                       if (right_unwrap == null)
-                                               throw new InternalErrorException ();
-                                       right_unwrap.EmitCheck (ec);
-                                       if (Oper == Binary.Operator.Equality) {
-                                               ig.Emit (OpCodes.Ldc_I4_0);
-                                               ig.Emit (OpCodes.Ceq);
-                                       }
-                                       return;
-                               }
-
-                               if (right is NullLiteral) {
-                                       if (left_unwrap == null)
-                                               throw new InternalErrorException ();
-                                       left_unwrap.EmitCheck (ec);
-                                       if (Oper == Binary.Operator.Equality) {
-                                               ig.Emit (OpCodes.Ldc_I4_0);
-                                               ig.Emit (OpCodes.Ceq);
-                                       }
-                                       return;
-                               }
-
-                               Label both_have_value_label = ig.DefineLabel ();
-                               Label end_label = ig.DefineLabel ();
-
-                               if (left_unwrap != null && right_unwrap != null) {
-                                       Label dissimilar_label = ig.DefineLabel ();
-
-                                       left_unwrap.EmitCheck (ec);
-                                       ig.Emit (OpCodes.Dup);
-                                       right_unwrap.EmitCheck (ec);
-                                       ig.Emit (OpCodes.Bne_Un, dissimilar_label);
-
-                                       ig.Emit (OpCodes.Brtrue, both_have_value_label);
-
-                                       // both are null
-                                       if (Oper == Binary.Operator.Equality)
-                                               ig.Emit (OpCodes.Ldc_I4_1);
-                                       else
-                                               ig.Emit (OpCodes.Ldc_I4_0);
-                                       ig.Emit (OpCodes.Br, end_label);
-
-                                       ig.MarkLabel (dissimilar_label);
-                                       ig.Emit (OpCodes.Pop);
-                               } else if (left_unwrap != null) {
-                                       left_unwrap.EmitCheck (ec);
-                                       ig.Emit (OpCodes.Brtrue, both_have_value_label);
-                               } else if (right_unwrap != null) {
-                                       right_unwrap.EmitCheck (ec);
-                                       ig.Emit (OpCodes.Brtrue, both_have_value_label);
-                               } else {
-                                       throw new InternalErrorException ("shouldn't get here");
-                               }
-
-                               // one is null while the other isn't
-                               if (Oper == Binary.Operator.Equality)
-                                       ig.Emit (OpCodes.Ldc_I4_0);
-                               else
-                                       ig.Emit (OpCodes.Ldc_I4_1);
-                               ig.Emit (OpCodes.Br, end_label);
-
-                               ig.MarkLabel (both_have_value_label);
-                               underlying.Emit (ec);
-
-                               ig.MarkLabel (end_label);
-                       }
-
-                       void EmitComparision (EmitContext ec)
-                       {
-                               ILGenerator ig = ec.ig;
-
-                               Label is_null_label = ig.DefineLabel ();
-                               Label end_label = ig.DefineLabel ();
-
-                               if (left_unwrap != null) {
-                                       left_unwrap.EmitCheck (ec);
-                                       ig.Emit (OpCodes.Brfalse, is_null_label);
-                               }
-
-                               if (right_unwrap != null) {
-                                       right_unwrap.EmitCheck (ec);
-                                       ig.Emit (OpCodes.Brfalse, is_null_label);
-                               }
-
-                               underlying.Emit (ec);
-                               ig.Emit (OpCodes.Br, end_label);
-
-                               ig.MarkLabel (is_null_label);
-                               ig.Emit (OpCodes.Ldc_I4_0);
-
-                               ig.MarkLabel (end_label);
-                       }
-                       
-                       public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
-                       {
-                               Emit (ec);
-                               ec.ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
-                       }                       
-
-                       public override void Emit (EmitContext ec)
-                       {
-                               if (left_unwrap != null)
-                                       left_unwrap.Store (ec);
-                               if (right_unwrap != null)
-                                       right_unwrap.Store (ec);
-
-                               if (is_boolean) {
-                                       EmitBoolean (ec);
-                                       return;
-                               } else if (is_equality) {
-                                       EmitEquality (ec);
-                                       return;
-                               } else if (is_comparision) {
-                                       EmitComparision (ec);
-                                       return;
-                               }
-
-                               ILGenerator ig = ec.ig;
-
-                               Label is_null_label = ig.DefineLabel ();
-                               Label end_label = ig.DefineLabel ();
-
-                               if (left_unwrap != null) {
-                                       left_unwrap.EmitCheck (ec);
-                                       ig.Emit (OpCodes.Brfalse, is_null_label);
-                               }
-
-                               if (right_unwrap != null) {
-                                       right_unwrap.EmitCheck (ec);
-                                       ig.Emit (OpCodes.Brfalse, is_null_label);
-                               }
-
-                               underlying.Emit (ec);
-                               ig.Emit (OpCodes.Br, end_label);
-
-                               ig.MarkLabel (is_null_label);
-                               null_value.Emit (ec);
-
-                               ig.MarkLabel (end_label);
-                       }
-               }
-
-               public class OperatorTrueOrFalse : Expression
-               {
-                       public readonly bool IsTrue;
-
-                       Expression expr;
-                       Unwrap unwrap;
-
-                       public OperatorTrueOrFalse (Expression expr, bool is_true, Location loc)
-                       {
-                               this.IsTrue = is_true;
-                               this.expr = expr;
-                               this.loc = loc;
-                       }
-
-                       public override Expression DoResolve (EmitContext ec)
-                       {
-                               unwrap = Unwrap.Create (expr, ec);
-                               if (unwrap == null)
-                                       return null;
-
-                               if (unwrap.Type != TypeManager.bool_type)
-                                       return null;
-
-                               type = TypeManager.bool_type;
-                               eclass = ExprClass.Value;
-                               return this;
-                       }
-
-                       public override void Emit (EmitContext ec)
-                       {
-                               ILGenerator ig = ec.ig;
-
-                               Label is_null_label = ig.DefineLabel ();
-                               Label end_label = ig.DefineLabel ();
-
-                               unwrap.EmitCheck (ec);
-                               ig.Emit (OpCodes.Brfalse, is_null_label);
-
-                               unwrap.Emit (ec);
-                               if (!IsTrue) {
-                                       ig.Emit (OpCodes.Ldc_I4_0);
-                                       ig.Emit (OpCodes.Ceq);
-                               }
-                               ig.Emit (OpCodes.Br, end_label);
-
-                               ig.MarkLabel (is_null_label);
-                               ig.Emit (OpCodes.Ldc_I4_0);
-
-                               ig.MarkLabel (end_label);
-                       }
-               }
-
-               public class NullCoalescingOperator : Expression
-               {
-                       Expression left, right;
-                       Expression expr;
-                       Unwrap unwrap;
-
-                       public NullCoalescingOperator (Expression left, Expression right, Location loc)
-                       {
-                               this.left = left;
-                               this.right = right;
-                               this.loc = loc;
-
-                               eclass = ExprClass.Value;
-                       }
-
-                       public override Expression DoResolve (EmitContext ec)
-                       {
-                               if (type != null)
-                                       return this;
-
-                               left = left.Resolve (ec);
-                               if (left == null)
-                                       return null;
-
-                               right = right.Resolve (ec);
-                               if (right == null)
-                                       return null;
-
-                               Type ltype = left.Type, rtype = right.Type;
-
-                               if (!TypeManager.IsNullableType (ltype) && ltype.IsValueType) {
-                                       Binary.Error_OperatorCannotBeApplied (loc, "??", ltype, rtype);
-                                       return null;
-                               }
-
-                               if (TypeManager.IsNullableType (ltype)) {
-                                       NullableInfo info = new NullableInfo (ltype);
-
-                                       unwrap = Unwrap.Create (left, ec);
-                                       if (unwrap == null)
-                                               return null;
-
-                                       expr = Convert.ImplicitConversion (ec, right, info.UnderlyingType, loc);
-                                       if (expr != null) {
-                                               left = unwrap;
-                                               type = expr.Type;
-                                               return this;
-                                       }
-                               }
-
-                               expr = Convert.ImplicitConversion (ec, right, ltype, loc);
-                               if (expr != null) {
-                                       type = expr.Type;
-                                       return this;
-                               }
-
-                               Expression left_null = unwrap != null ? unwrap : left;
-                               expr = Convert.ImplicitConversion (ec, left_null, rtype, loc);
-                               if (expr != null) {
-                                       left = expr;
-                                       expr = right;
-                                       type = rtype;
-                                       return this;
-                               }
-
-                               Binary.Error_OperatorCannotBeApplied (loc, "??", ltype, rtype);
-                               return null;
-                       }
-
-                       public override void Emit (EmitContext ec)
-                       {
-                               ILGenerator ig = ec.ig;
-
-                               Label is_null_label = ig.DefineLabel ();
-                               Label end_label = ig.DefineLabel ();
-
-                               if (unwrap != null) {
-                                       unwrap.EmitCheck (ec);
-                                       ig.Emit (OpCodes.Brfalse, is_null_label);
-
-                                       left.Emit (ec);
-                                       ig.Emit (OpCodes.Br, end_label);
-
-                                       ig.MarkLabel (is_null_label);
-                                       expr.Emit (ec);
-
-                                       ig.MarkLabel (end_label);
-                               } else {
-                                       left.Emit (ec);
-                                       ig.Emit (OpCodes.Dup);
-                                       ig.Emit (OpCodes.Brtrue, end_label);
-
-                                       ig.MarkLabel (is_null_label);
-
-                                       ig.Emit (OpCodes.Pop);
-                                       expr.Emit (ec);
-
-                                       ig.MarkLabel (end_label);
-                               }
-                       }
-
-                       protected override void CloneTo (CloneContext clonectx, Expression t)
-                       {
-                               NullCoalescingOperator target = (NullCoalescingOperator) t;
-
-                               target.left = left.Clone (clonectx);
-                               target.right = right.Clone (clonectx);
-                       }
-               }
-
-               public class LiftedUnaryMutator : ExpressionStatement
-               {
-                       public readonly UnaryMutator.Mode Mode;
-                       Expression expr, null_value;
-                       UnaryMutator underlying;
-                       Unwrap unwrap;
-
-                       public LiftedUnaryMutator (UnaryMutator.Mode mode, Expression expr, Location loc)
-                       {
-                               this.expr = expr;
-                               this.Mode = mode;
-                               this.loc = loc;
-
-                               eclass = ExprClass.Value;
-                       }
-
-                       public override Expression DoResolve (EmitContext ec)
-                       {
-                               expr = expr.Resolve (ec);
-                               if (expr == null)
-                                       return null;
-
-                               unwrap = Unwrap.Create (expr, ec);
-                               if (unwrap == null)
-                                       return null;
-
-                               underlying = (UnaryMutator) new UnaryMutator (Mode, unwrap, loc).Resolve (ec);
-                               if (underlying == null)
-                                       return null;
-
-                               null_value = new NullableLiteral (expr.Type, loc).Resolve (ec);
-                               if (null_value == null)
-                                       return null;
-
-                               type = expr.Type;
-                               return this;
-                       }
-
-                       void DoEmit (EmitContext ec, bool is_expr)
-                       {
-                               ILGenerator ig = ec.ig;
-                               Label is_null_label = ig.DefineLabel ();
-                               Label end_label = ig.DefineLabel ();
-
-                               unwrap.EmitCheck (ec);
-                               ig.Emit (OpCodes.Brfalse, is_null_label);
-
-                               if (is_expr)
-                                       underlying.Emit (ec);
-                               else
-                                       underlying.EmitStatement (ec);
-                               ig.Emit (OpCodes.Br, end_label);
-
-                               ig.MarkLabel (is_null_label);
-                               if (is_expr)
-                                       null_value.Emit (ec);
-
-                               ig.MarkLabel (end_label);
-                       }
-
-                       public override void Emit (EmitContext ec)
-                       {
-                               DoEmit (ec, true);
-                       }
-
-                       public override void EmitStatement (EmitContext ec)
-                       {
-                               DoEmit (ec, false);
-                       }
-               }
-       }
 }