New tests.
[mono.git] / mcs / mcs / nullable.cs
index 4e8883f0dd856c4d1814bc674984bf16dd37010a..8e2dc682bbc0fbbfc2403f8ed3842c0c87c11dd4 100644 (file)
@@ -14,7 +14,6 @@
 using System;
 using System.Reflection;
 using System.Reflection.Emit;
-using System.Collections;
        
 namespace Mono.CSharp.Nullable
 {
@@ -30,7 +29,7 @@ namespace Mono.CSharp.Nullable
                        eclass = ExprClass.Type;
                }
 
-               public NullableType (Type type, Location loc)
+               public NullableType (TypeSpec type, Location loc)
                        : this (new TypeExpression (type, loc), loc)
                { }
 
@@ -38,68 +37,54 @@ namespace Mono.CSharp.Nullable
                {
                        if (TypeManager.generic_nullable_type == null) {
                                TypeManager.generic_nullable_type = TypeManager.CoreLookupType (ec.Compiler,
-                                       "System", "Nullable`1", Kind.Struct, true);
+                                       "System", "Nullable", 1, MemberKind.Struct, true);
                        }
 
                        TypeArguments args = new TypeArguments (underlying);
                        GenericTypeExpr ctype = new GenericTypeExpr (TypeManager.generic_nullable_type, args, loc);
                        return ctype.ResolveAsTypeTerminal (ec, false);
                }
-
-               public override TypeExpr ResolveAsTypeTerminal (IMemberContext ec, bool silent)
-               {
-                       return ResolveAsBaseTerminal (ec, silent);
-               }               
        }
 
-       public sealed class NullableInfo
+       static class NullableInfo
        {
-               public readonly Type Type;
-               public readonly Type UnderlyingType;
-               public MethodInfo HasValue;
-               public MethodInfo Value;
-               public MethodInfo GetValueOrDefault;
-               public ConstructorInfo Constructor;
-
-               public NullableInfo (Type type)
+               public static MethodSpec GetConstructor (TypeSpec nullableType)
                {
-                       Type = type;
-                       UnderlyingType = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (type) [0]);
-
-                       PropertyInfo has_value_pi = TypeManager.GetPredefinedProperty (type, "HasValue", Location.Null, Type.EmptyTypes);
-                       PropertyInfo value_pi = TypeManager.GetPredefinedProperty (type, "Value", Location.Null, Type.EmptyTypes);
-                       GetValueOrDefault = TypeManager.GetPredefinedMethod (type, "GetValueOrDefault", Location.Null, Type.EmptyTypes);
+                       return TypeManager.GetPredefinedConstructor (nullableType, Location.Null, GetUnderlyingType (nullableType));
+               }
 
-                       HasValue = has_value_pi.GetGetMethod (false);
-                       Value = value_pi.GetGetMethod (false);
+               public static MethodSpec GetHasValue (TypeSpec nullableType)
+               {
+                       return (MethodSpec) MemberCache.FindMember (nullableType,
+                               MemberFilter.Method ("get_HasValue", 0, ParametersCompiled.EmptyReadOnlyParameters, null), BindingRestriction.None);
+               }
 
-                       // When compiling corlib
-                       if (TypeManager.IsBeingCompiled (type)) {
-                               TypeContainer tc = TypeManager.LookupGenericTypeContainer (type);
-                               
-                               // TODO: check for correct overload
-                               Constructor c = ((Constructor) tc.InstanceConstructors [0]);
+               public static MethodSpec GetGetValueOrDefault (TypeSpec nullableType)
+               {
+                       return (MethodSpec) MemberCache.FindMember (nullableType,
+                               MemberFilter.Method ("GetValueOrDefault", 0, ParametersCompiled.EmptyReadOnlyParameters, null), BindingRestriction.None);
+               }
 
-                               Constructor = TypeBuilder.GetConstructor (type, c.ConstructorBuilder);
-                               return;
-                       }
+               public static MethodSpec GetValue (TypeSpec nullableType)
+               {
+                       return (MethodSpec) MemberCache.FindMember (nullableType,
+                               MemberFilter.Method ("get_Value", 0, ParametersCompiled.EmptyReadOnlyParameters, null), BindingRestriction.None);
+               }
 
-#if MS_COMPATIBLE
-                       if (TypeManager.IsBeingCompiled (UnderlyingType)) {
-                               ConstructorInfo cinfo = TypeManager.DropGenericTypeArguments (type).GetConstructors ()[0];
-                               Constructor = TypeBuilder.GetConstructor (type, cinfo);
-                               return;
-                       }
-#endif
+               public static TypeSpec GetUnderlyingType (TypeSpec nullableType)
+               {
+                       return ((InflatedTypeSpec) nullableType).TypeArguments[0];
+               }
 
-                       Constructor = type.GetConstructor (new Type[] { UnderlyingType });
+               public static bool IsNullableType (TypeSpec type)
+               {
+                       throw new NotImplementedException ("net");
                }
        }
 
        public class Unwrap : Expression, IMemoryLocation, IAssignMethod
        {
                Expression expr;
-               NullableInfo info;
 
                LocalTemporary temp;
                readonly bool useDefaultValue;
@@ -110,8 +95,7 @@ namespace Mono.CSharp.Nullable
                        this.loc = expr.Location;
                        this.useDefaultValue = useDefaultValue;
 
-                       info = new NullableInfo (expr.Type);
-                       type = info.UnderlyingType;
+                       type = NullableInfo.GetUnderlyingType (expr.Type);
                        eclass = expr.eclass;
                }
 
@@ -151,15 +135,15 @@ namespace Mono.CSharp.Nullable
                {
                        Store (ec);
                        if (useDefaultValue)
-                               Invocation.EmitCall (ec, false, this, info.GetValueOrDefault, null, loc);
+                               Invocation.EmitCall (ec, false, this, NullableInfo.GetGetValueOrDefault (expr.Type), null, loc);
                        else
-                               Invocation.EmitCall (ec, false, this, info.Value, null, loc);
+                               Invocation.EmitCall (ec, false, this, NullableInfo.GetValue (expr.Type), null, loc);
                }
 
                public void EmitCheck (EmitContext ec)
                {
                        Store (ec);
-                       Invocation.EmitCall (ec, false, this, info.HasValue, null, loc);
+                       Invocation.EmitCall (ec, false, this, NullableInfo.GetHasValue (expr.Type), null, loc);
                }
 
                public override bool Equals (object obj)
@@ -205,21 +189,10 @@ namespace Mono.CSharp.Nullable
                                LocalVariable.Emit (ec);
                }
 
-#if NET_4_0
                public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
                {
                        return expr.MakeExpression (ctx);
                }
-#endif
-
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       type = storey.MutateType (type);
-                       info.Constructor = storey.MutateConstructor (info.Constructor);
-                       info.HasValue = storey.MutateGenericMethod (info.HasValue);
-                       info.GetValueOrDefault = storey.MutateGenericMethod (info.GetValueOrDefault);
-                       info.Value = storey.MutateGenericMethod (info.Value);
-               }
 
                public void AddressOf (EmitContext ec, AddressOp mode)
                {
@@ -236,7 +209,7 @@ namespace Mono.CSharp.Nullable
                LocalTemporary LocalVariable {
                        get {
                                if (temp == null)
-                                       temp = new LocalTemporary (info.Type);
+                                       temp = new LocalTemporary (expr.Type);
                                return temp;
                        }
                }
@@ -252,22 +225,20 @@ namespace Mono.CSharp.Nullable
                public void EmitAssign (EmitContext ec, Expression source,
                                        bool leave_copy, bool prepare_for_load)
                {
-                       InternalWrap wrap = new InternalWrap (source, info, loc);
+                       InternalWrap wrap = new InternalWrap (source, expr.Type, loc);
                        ((IAssignMethod) expr).EmitAssign (ec, wrap, leave_copy, false);
                }
 
-               protected class InternalWrap : Expression
+               class InternalWrap : Expression
                {
                        public Expression expr;
-                       public NullableInfo info;
 
-                       public InternalWrap (Expression expr, NullableInfo info, Location loc)
+                       public InternalWrap (Expression expr, TypeSpec type, Location loc)
                        {
                                this.expr = expr;
-                               this.info = info;
                                this.loc = loc;
+                               this.type = type;
 
-                               type = info.Type;
                                eclass = ExprClass.Value;
                        }
 
@@ -284,26 +255,19 @@ namespace Mono.CSharp.Nullable
                        public override void Emit (EmitContext ec)
                        {
                                expr.Emit (ec);
-                               ec.ig.Emit (OpCodes.Newobj, info.Constructor);
+                               ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
                        }
                }
        }
 
        public class Wrap : TypeCast
        {
-               readonly NullableInfo info;
-
-               protected Wrap (Expression expr, Type type)
+               private Wrap (Expression expr, TypeSpec type)
                        : base (expr, type)
                {
-                       info = new NullableInfo (type);
                        eclass = ExprClass.Value;
                }
 
-               public Expression Child {
-                       get { return child; }
-               }
-
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        TypeCast child_cast = child as TypeCast;
@@ -315,13 +279,13 @@ namespace Mono.CSharp.Nullable
                        return base.CreateExpressionTree (ec);
                }
 
-               public static Expression Create (Expression expr, Type type)
+               public static Expression Create (Expression expr, TypeSpec type)
                {
                        //
                        // Avoid unwraping and wraping of the same type
                        //
                        Unwrap unwrap = expr as Unwrap;
-                       if (unwrap != null && TypeManager.IsEqual (expr.Type, TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (type) [0])))
+                       if (unwrap != null && TypeManager.IsEqual (expr.Type, NullableInfo.GetUnderlyingType (type)))
                                return unwrap.Original;
                
                        return new Wrap (expr, type);
@@ -330,7 +294,7 @@ namespace Mono.CSharp.Nullable
                public override void Emit (EmitContext ec)
                {
                        child.Emit (ec);
-                       ec.ig.Emit (OpCodes.Newobj, info.Constructor);
+                       ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
                }
        }
 
@@ -339,13 +303,13 @@ namespace Mono.CSharp.Nullable
        //
        public class LiftedNull : NullConstant, IMemoryLocation
        {
-               private LiftedNull (Type nullable_type, Location loc)
+               private LiftedNull (TypeSpec nullable_type, Location loc)
                        : base (nullable_type, loc)
                {
                        eclass = ExprClass.Value;
                }
 
-               public static Constant Create (Type nullable, Location loc)
+               public static Constant Create (TypeSpec nullable, Location loc)
                {
                        return new LiftedNull (nullable, loc);
                }
@@ -364,7 +328,7 @@ namespace Mono.CSharp.Nullable
                        LocalTemporary value_target = new LocalTemporary (type);
 
                        value_target.AddressOf (ec, AddressOp.Store);
-                       ec.ig.Emit (OpCodes.Initobj, type);
+                       ec.Emit (OpCodes.Initobj, type);
                        value_target.Emit (ec);
                }
 
@@ -373,7 +337,7 @@ namespace Mono.CSharp.Nullable
                        LocalTemporary value_target = new LocalTemporary (type);
                                
                        value_target.AddressOf (ec, AddressOp.Store);
-                       ec.ig.Emit (OpCodes.Initobj, type);
+                       ec.Emit (OpCodes.Initobj, type);
                        ((IMemoryLocation) value_target).AddressOf (ec, Mode);
                }
        }
@@ -386,7 +350,7 @@ namespace Mono.CSharp.Nullable
                Expression expr, null_value;
                Unwrap unwrap;
 
-               public Lifted (Expression expr, Unwrap unwrap, Type type)
+               public Lifted (Expression expr, Unwrap unwrap, TypeSpec type)
                {
                        this.expr = expr;
                        this.unwrap = unwrap;
@@ -394,7 +358,7 @@ namespace Mono.CSharp.Nullable
                        this.type = type;
                }
 
-               public Lifted (Expression expr, Expression unwrap, Type type)
+               public Lifted (Expression expr, Expression unwrap, TypeSpec type)
                        : this (expr, unwrap as Unwrap, type)
                {
                }
@@ -435,20 +399,19 @@ namespace Mono.CSharp.Nullable
 
                public override void Emit (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-                       Label is_null_label = ig.DefineLabel ();
-                       Label end_label = ig.DefineLabel ();
+                       Label is_null_label = ec.DefineLabel ();
+                       Label end_label = ec.DefineLabel ();
 
                        unwrap.EmitCheck (ec);
-                       ig.Emit (OpCodes.Brfalse, is_null_label);
+                       ec.Emit (OpCodes.Brfalse, is_null_label);
 
                        expr.Emit (ec);
 
-                       ig.Emit (OpCodes.Br, end_label);
-                       ig.MarkLabel (is_null_label);
+                       ec.Emit (OpCodes.Br, end_label);
+                       ec.MarkLabel (is_null_label);
 
                        null_value.Emit (ec);
-                       ig.MarkLabel (end_label);
+                       ec.MarkLabel (end_label);
                }
 
                public void AddressOf (EmitContext ec, AddressOp mode)
@@ -462,8 +425,8 @@ namespace Mono.CSharp.Nullable
                Unwrap unwrap;
                Expression user_operator;
 
-               public LiftedUnaryOperator (Unary.Operator op, Expression expr)
-                       : base (op, expr)
+               public LiftedUnaryOperator (Unary.Operator op, Expression expr, Location loc)
+                       : base (op, expr, loc)
                {
                }
 
@@ -485,9 +448,6 @@ namespace Mono.CSharp.Nullable
 
                protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        unwrap = Unwrap.Create (Expr, false);
                        if (unwrap == null)
                                return null;
@@ -510,28 +470,25 @@ namespace Mono.CSharp.Nullable
 
                public override void Emit (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-                       Label is_null_label = ig.DefineLabel ();
-                       Label end_label = ig.DefineLabel ();
+                       Label is_null_label = ec.DefineLabel ();
+                       Label end_label = ec.DefineLabel ();
 
                        unwrap.EmitCheck (ec);
-                       ig.Emit (OpCodes.Brfalse, is_null_label);
-
-                       NullableInfo ni = new NullableInfo (type);
+                       ec.Emit (OpCodes.Brfalse, is_null_label);
 
                        if (user_operator != null) {
                                user_operator.Emit (ec);
                        } else {
-                               EmitOperator (ec, ni.UnderlyingType);
+                               EmitOperator (ec, NullableInfo.GetUnderlyingType (type));
                        }
 
-                       ig.Emit (OpCodes.Newobj, ni.Constructor);
-                       ig.Emit (OpCodes.Br_S, end_label);
+                       ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
+                       ec.Emit (OpCodes.Br_S, end_label);
 
-                       ig.MarkLabel (is_null_label);
+                       ec.MarkLabel (is_null_label);
                        LiftedNull.Create (type, loc).Emit (ec);
 
-                       ig.MarkLabel (end_label);
+                       ec.MarkLabel (end_label);
                }
 
                Expression LiftExpression (ResolveContext ec, Expression expr)
@@ -579,13 +536,11 @@ namespace Mono.CSharp.Nullable
                bool left_null_lifted, right_null_lifted;
                Expression left_orig, right_orig;
                Expression user_operator;
-               ConstructorInfo wrap_ctor;
+               MethodSpec wrap_ctor;
 
-               public LiftedBinaryOperator (Binary.Operator op, Expression left, Expression right,
-                                            Location loc)
-                       : base (op, left, right)
+               public LiftedBinaryOperator (Binary.Operator op, Expression left, Expression right, Location loc)
+                       : base (op, left, right, loc)
                {
-                       this.loc = loc;
                }
 
                public override Expression CreateExpressionTree (ResolveContext ec)
@@ -604,7 +559,7 @@ namespace Mono.CSharp.Nullable
                Expression CreateNullConstant (ResolveContext ec, Expression expr)
                {
                        // FIXME: Handle side effect constants
-                       Constant c = new BoolConstant (Oper == Operator.Inequality, loc);
+                       Constant c = new BoolConstant (Oper == Operator.Inequality, loc).Resolve (ec);
 
                        if ((Oper & Operator.EqualityMask) != 0) {
                                ec.Report.Warning (472, 2, loc, "The result of comparing value type `{0}' with null is `{1}'",
@@ -619,9 +574,6 @@ namespace Mono.CSharp.Nullable
 
                protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        if ((Oper & Operator.LogicalMask) != 0) {
                                Error_OperatorCannotBeApplied (ec, left, right);
                                return null;
@@ -665,23 +617,21 @@ namespace Mono.CSharp.Nullable
 
                void EmitBitwiseBoolean (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-
-                       Label load_left = ig.DefineLabel ();
-                       Label load_right = ig.DefineLabel ();
-                       Label end_label = ig.DefineLabel ();
+                       Label load_left = ec.DefineLabel ();
+                       Label load_right = ec.DefineLabel ();
+                       Label end_label = ec.DefineLabel ();
 
                        left_unwrap.Emit (ec);
-                       ig.Emit (OpCodes.Brtrue_S, load_right);
+                       ec.Emit (OpCodes.Brtrue_S, load_right);
 
                        right_unwrap.Emit (ec);
-                       ig.Emit (OpCodes.Brtrue_S, load_left);
+                       ec.Emit (OpCodes.Brtrue_S, load_left);
 
                        left_unwrap.EmitCheck (ec);
-                       ig.Emit (OpCodes.Brfalse_S, load_right);
+                       ec.Emit (OpCodes.Brfalse_S, load_right);
 
                        // load left
-                       ig.MarkLabel (load_left);
+                       ec.MarkLabel (load_left);
 
                        if (Oper == Operator.BitwiseAnd) {
                                left_unwrap.Load (ec);
@@ -689,13 +639,13 @@ namespace Mono.CSharp.Nullable
                                right_unwrap.Load (ec);
                                right_unwrap = left_unwrap;
                        }
-                       ig.Emit (OpCodes.Br_S, end_label);
+                       ec.Emit (OpCodes.Br_S, end_label);
 
                        // load right
-                       ig.MarkLabel (load_right);
+                       ec.MarkLabel (load_right);
                        right_unwrap.Load (ec);
 
-                       ig.MarkLabel (end_label);
+                       ec.MarkLabel (end_label);
                }
 
                //
@@ -703,16 +653,14 @@ namespace Mono.CSharp.Nullable
                //
                void EmitEquality (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-
                        //
                        // Either left or right is null
                        //
                        if (left_unwrap != null && (right_null_lifted || right.IsNull)) {
                                left_unwrap.EmitCheck (ec);
                                if (Oper == Binary.Operator.Equality) {
-                                       ig.Emit (OpCodes.Ldc_I4_0);
-                                       ig.Emit (OpCodes.Ceq);
+                                       ec.Emit (OpCodes.Ldc_I4_0);
+                                       ec.Emit (OpCodes.Ceq);
                                }
                                return;
                        }
@@ -720,23 +668,23 @@ namespace Mono.CSharp.Nullable
                        if (right_unwrap != null && (left_null_lifted || left.IsNull)) {
                                right_unwrap.EmitCheck (ec);
                                if (Oper == Binary.Operator.Equality) {
-                                       ig.Emit (OpCodes.Ldc_I4_0);
-                                       ig.Emit (OpCodes.Ceq);
+                                       ec.Emit (OpCodes.Ldc_I4_0);
+                                       ec.Emit (OpCodes.Ceq);
                                }
                                return;
                        }
 
-                       Label dissimilar_label = ig.DefineLabel ();
-                       Label end_label = ig.DefineLabel ();
+                       Label dissimilar_label = ec.DefineLabel ();
+                       Label end_label = ec.DefineLabel ();
 
                        if (user_operator != null) {
                                user_operator.Emit (ec);
-                               ig.Emit (Oper == Operator.Equality ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, dissimilar_label);
+                               ec.Emit (Oper == Operator.Equality ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, dissimilar_label);
                        } else {
                                left.Emit (ec);
                                right.Emit (ec);
 
-                               ig.Emit (OpCodes.Bne_Un_S, dissimilar_label);
+                               ec.Emit (OpCodes.Bne_Un_S, dissimilar_label);
                        }
 
                        if (left_unwrap != null)
@@ -747,31 +695,31 @@ namespace Mono.CSharp.Nullable
 
                        if (left_unwrap != null && right_unwrap != null) {
                                if (Oper == Operator.Inequality)
-                                       ig.Emit (OpCodes.Xor);
+                                       ec.Emit (OpCodes.Xor);
                                else
-                                       ig.Emit (OpCodes.Ceq);
+                                       ec.Emit (OpCodes.Ceq);
                        } else {
                                if (Oper == Operator.Inequality) {
-                                       ig.Emit (OpCodes.Ldc_I4_0);
-                                       ig.Emit (OpCodes.Ceq);
+                                       ec.Emit (OpCodes.Ldc_I4_0);
+                                       ec.Emit (OpCodes.Ceq);
                                }
                        }
 
-                       ig.Emit (OpCodes.Br_S, end_label);
+                       ec.Emit (OpCodes.Br_S, end_label);
 
-                       ig.MarkLabel (dissimilar_label);
+                       ec.MarkLabel (dissimilar_label);
                        if (Oper == Operator.Inequality)
-                               ig.Emit (OpCodes.Ldc_I4_1);
+                               ec.Emit (OpCodes.Ldc_I4_1);
                        else
-                               ig.Emit (OpCodes.Ldc_I4_0);
+                               ec.Emit (OpCodes.Ldc_I4_0);
 
-                       ig.MarkLabel (end_label);
+                       ec.MarkLabel (end_label);
                }
                
                public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
                {
                        Emit (ec);
-                       ec.ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
+                       ec.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
                }                       
 
                public override void Emit (EmitContext ec)
@@ -792,14 +740,12 @@ namespace Mono.CSharp.Nullable
                                return;
                        }
 
-                       ILGenerator ig = ec.ig;
-
-                       Label is_null_label = ig.DefineLabel ();
-                       Label end_label = ig.DefineLabel ();
+                       Label is_null_label = ec.DefineLabel ();
+                       Label end_label = ec.DefineLabel ();
 
                        if (left_unwrap != null) {
                                left_unwrap.EmitCheck (ec);
-                               ig.Emit (OpCodes.Brfalse, is_null_label);
+                               ec.Emit (OpCodes.Brfalse, is_null_label);
                        }
 
                        //
@@ -807,27 +753,27 @@ namespace Mono.CSharp.Nullable
                        //
                        if (right_unwrap != null && !left.Equals (right)) {
                                right_unwrap.EmitCheck (ec);
-                               ig.Emit (OpCodes.Brfalse, is_null_label);
+                               ec.Emit (OpCodes.Brfalse, is_null_label);
                        }
 
                        EmitOperator (ec, left.Type);
 
                        if (wrap_ctor != null)
-                               ig.Emit (OpCodes.Newobj, wrap_ctor);
+                               ec.Emit (OpCodes.Newobj, wrap_ctor);
 
-                       ig.Emit (OpCodes.Br_S, end_label);
-                       ig.MarkLabel (is_null_label);
+                       ec.Emit (OpCodes.Br_S, end_label);
+                       ec.MarkLabel (is_null_label);
 
                        if ((Oper & Operator.ComparisonMask) != 0) {
-                               ig.Emit (OpCodes.Ldc_I4_0);
+                               ec.Emit (OpCodes.Ldc_I4_0);
                        } else {
                                LiftedNull.Create (type, loc).Emit (ec);
                        }
 
-                       ig.MarkLabel (end_label);
+                       ec.MarkLabel (end_label);
                }
 
-               protected override void EmitOperator (EmitContext ec, Type l)
+               protected override void EmitOperator (EmitContext ec, TypeSpec l)
                {
                        if (user_operator != null) {
                                user_operator.Emit (ec);
@@ -835,7 +781,7 @@ namespace Mono.CSharp.Nullable
                        }
 
                        if (TypeManager.IsNullableType (l))
-                               l = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (l) [0]);
+                               l = TypeManager.GetTypeArguments (l) [0];
 
                        base.EmitOperator (ec, l);
                }
@@ -884,7 +830,7 @@ namespace Mono.CSharp.Nullable
                                if (lifted_type == null)
                                        return null;
 
-                               wrap_ctor = new NullableInfo (lifted_type.Type).Constructor;
+                               wrap_ctor = NullableInfo.GetConstructor (lifted_type.Type);
                                type = res_expr.Type = lifted_type.Type;
                        }
 
@@ -911,13 +857,13 @@ namespace Mono.CSharp.Nullable
                                // Value types and null comparison
                                //
                                if (left_unwrap == null || (Oper & Operator.RelationalMask) != 0)
-                                       return CreateNullConstant (ec, left_orig).Resolve (ec);
+                                       return CreateNullConstant (ec, left_orig);
                        }
 
                        return res_expr;
                }
 
-               protected override Expression ResolveOperatorPredefined (ResolveContext ec, Binary.PredefinedOperator [] operators, bool primitives_only, Type enum_type)
+               protected override Expression ResolveOperatorPredefined (ResolveContext ec, Binary.PredefinedOperator [] operators, bool primitives_only, TypeSpec enum_type)
                {
                        Expression e = base.ResolveOperatorPredefined (ec, operators, primitives_only, enum_type);
 
@@ -939,7 +885,7 @@ namespace Mono.CSharp.Nullable
                        return e;
                }
 
-               protected override Expression ResolveUserOperator (ResolveContext ec, Type l, Type r)
+               protected override Expression ResolveUserOperator (ResolveContext ec, TypeSpec l, TypeSpec r)
                {
                        Expression expr = base.ResolveUserOperator (ec, l, r);
                        if (expr == null)
@@ -998,7 +944,7 @@ namespace Mono.CSharp.Nullable
                        if (left.eclass == ExprClass.MethodGroup)
                                return null;
 
-                       Type ltype = left.Type;
+                       TypeSpec ltype = left.Type;
 
                        //
                        // If left is a nullable type and an implicit conversion exists from right to underlying type of left,
@@ -1028,7 +974,7 @@ namespace Mono.CSharp.Nullable
                                        // Reduce (left ?? null) to left OR (null-constant ?? right) to right
                                        //
                                        if (right.IsNull || lc != null)
-                                               return ReducedExpression.Create (lc != null ? right : left, this).Resolve (ec);
+                                               return ReducedExpression.Create (lc != null ? right : left, this);
 
                                        right = Convert.ImplicitConversion (ec, right, ltype, loc);
                                        type = left.Type;
@@ -1038,7 +984,7 @@ namespace Mono.CSharp.Nullable
                                return null;
                        }
 
-                       Type rtype = right.Type;
+                       TypeSpec rtype = right.Type;
                        if (!Convert.ImplicitConversionExists (ec, unwrap != null ? unwrap : left, rtype) || right.eclass == ExprClass.MethodGroup)
                                return null;
 
@@ -1046,7 +992,7 @@ namespace Mono.CSharp.Nullable
                        // Reduce (null ?? right) to right
                        //
                        if (left.IsNull)
-                               return ReducedExpression.Create (right, this).Resolve (ec);
+                               return ReducedExpression.Create (right, this);
 
                        left = Convert.ImplicitConversion (ec, unwrap != null ? unwrap : left, rtype, loc);
                        type = rtype;
@@ -1055,9 +1001,6 @@ namespace Mono.CSharp.Nullable
 
                protected override Expression DoResolve (ResolveContext ec)
                {
-                       if (eclass != ExprClass.Invalid)
-                               return this;
-
                        left = left.Resolve (ec);
                        right = right.Resolve (ec);
 
@@ -1077,42 +1020,33 @@ namespace Mono.CSharp.Nullable
 
                public override void Emit (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-
-                       Label end_label = ig.DefineLabel ();
+                       Label end_label = ec.DefineLabel ();
 
                        if (unwrap != null) {
-                               Label is_null_label = ig.DefineLabel ();
+                               Label is_null_label = ec.DefineLabel ();
 
                                unwrap.EmitCheck (ec);
-                               ig.Emit (OpCodes.Brfalse, is_null_label);
+                               ec.Emit (OpCodes.Brfalse, is_null_label);
 
                                left.Emit (ec);
-                               ig.Emit (OpCodes.Br, end_label);
+                               ec.Emit (OpCodes.Br, end_label);
 
-                               ig.MarkLabel (is_null_label);
+                               ec.MarkLabel (is_null_label);
                                right.Emit (ec);
 
-                               ig.MarkLabel (end_label);
+                               ec.MarkLabel (end_label);
                                return;
                        }
 
                        left.Emit (ec);
 
-                       ig.Emit (OpCodes.Dup);
-                       ig.Emit (OpCodes.Brtrue, end_label);
+                       ec.Emit (OpCodes.Dup);
+                       ec.Emit (OpCodes.Brtrue, end_label);
 
-                       ig.Emit (OpCodes.Pop);
+                       ec.Emit (OpCodes.Pop);
                        right.Emit (ec);
 
-                       ig.MarkLabel (end_label);
-               }
-
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       left.MutateHoistedGenericType (storey);
-                       right.MutateHoistedGenericType (storey);
-                       type = storey.MutateType (type);
+                       ec.MarkLabel (end_label);
                }
 
                protected override void CloneTo (CloneContext clonectx, Expression t)
@@ -1136,8 +1070,6 @@ namespace Mono.CSharp.Nullable
                        this.expr = expr;
                        this.Mode = mode;
                        this.loc = loc;
-
-                       eclass = ExprClass.Value;
                }
 
                public override Expression CreateExpressionTree (ResolveContext ec)
@@ -1155,35 +1087,36 @@ namespace Mono.CSharp.Nullable
                        if (unwrap == null)
                                return null;
 
-                       underlying = (UnaryMutator) new UnaryMutator (Mode, unwrap).Resolve (ec);
+                       underlying = (UnaryMutator) new UnaryMutator (Mode, unwrap, loc).Resolve (ec);
                        if (underlying == null)
                                return null;
 
+
+                       eclass = ExprClass.Value;
                        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 ();
+                       Label is_null_label = ec.DefineLabel ();
+                       Label end_label = ec.DefineLabel ();
 
                        unwrap.EmitCheck (ec);
-                       ig.Emit (OpCodes.Brfalse, is_null_label);
+                       ec.Emit (OpCodes.Brfalse, is_null_label);
 
                        if (is_expr) {
                                underlying.Emit (ec);
-                               ig.Emit (OpCodes.Br_S, end_label);
+                               ec.Emit (OpCodes.Br_S, end_label);
                        } else {
                                underlying.EmitStatement (ec);
                        }
 
-                       ig.MarkLabel (is_null_label);
+                       ec.MarkLabel (is_null_label);
                        if (is_expr)
                                LiftedNull.Create (type, loc).Emit (ec);
 
-                       ig.MarkLabel (end_label);
+                       ec.MarkLabel (end_label);
                }
 
                public override void Emit (EmitContext ec)