Rework gc descriptor to include size information in more cases.
[mono.git] / mcs / mcs / cfold.cs
index 51bbbd1c54000797e89755f840da58701002c5a5..7580124ceead35f46cd9af384fdc17b2fe99dcb8 100644 (file)
@@ -6,17 +6,21 @@
 //   Marek Safar (marek.safar@seznam.cz)
 //
 // Copyright 2002, 2003 Ximian, Inc.
-// Copyright 2003-2008, Novell, Inc.
+// Copyright 2003-2011, Novell, Inc.
 // 
 using System;
 
 namespace Mono.CSharp {
 
-       public class ConstantFold {
-
-               public static readonly Type[] binary_promotions = new Type[] { 
-                       TypeManager.decimal_type, TypeManager.double_type, TypeManager.float_type,
-                       TypeManager.uint64_type, TypeManager.int64_type, TypeManager.uint32_type };
+       public static class ConstantFold
+       {
+               public static TypeSpec[] CreateBinaryPromotionsTypes (BuiltinTypes types)
+               {
+                       return new TypeSpec[] { 
+                               types.Decimal, types.Double, types.Float,
+                               types.ULong, types.Long, types.UInt
+                       };
+               }
 
                //
                // Performs the numeric promotions on the left and right expresions
@@ -30,10 +34,10 @@ namespace Mono.CSharp {
                //              
                static bool DoBinaryNumericPromotions (ResolveContext rc, ref Constant left, ref Constant right)
                {
-                       Type ltype = left.Type;
-                       Type rtype = right.Type;
+                       TypeSpec ltype = left.Type;
+                       TypeSpec rtype = right.Type;
 
-                       foreach (Type t in binary_promotions) {
+                       foreach (TypeSpec t in rc.BuiltinTypes.BinaryPromotionsTypes) {
                                if (t == ltype)
                                        return t == rtype || ConvertPromotion (rc, ref right, ref left, t);
 
@@ -41,23 +45,23 @@ namespace Mono.CSharp {
                                        return t == ltype || ConvertPromotion (rc, ref left, ref right, t);
                        }
 
-                       left = left.ConvertImplicitly (rc, TypeManager.int32_type);
-                       right = right.ConvertImplicitly (rc, TypeManager.int32_type);
+                       left = left.ConvertImplicitly (rc.BuiltinTypes.Int);
+                       right = right.ConvertImplicitly (rc.BuiltinTypes.Int);
                        return left != null && right != null;
                }
 
-               static bool ConvertPromotion (ResolveContext rc, ref Constant prim, ref Constant second, Type type)
+               static bool ConvertPromotion (ResolveContext rc, ref Constant prim, ref Constant second, TypeSpec type)
                {
-                       Constant c = prim.ConvertImplicitly (rc, type);
+                       Constant c = prim.ConvertImplicitly (type);
                        if (c != null) {
                                prim = c;
                                return true;
                        }
 
-                       if (type == TypeManager.uint32_type) {
-                               type = TypeManager.int64_type;
-                               prim = prim.ConvertImplicitly (rc, type);
-                               second = second.ConvertImplicitly (rc, type);
+                       if (type.BuiltinType == BuiltinTypeSpec.Type.UInt) {
+                               type = rc.BuiltinTypes.Long;
+                               prim = prim.ConvertImplicitly (type);
+                               second = second.ConvertImplicitly (type);
                                return prim != null && second != null;
                        }
 
@@ -99,26 +103,26 @@ namespace Mono.CSharp {
                                return new SideEffectConstant (result, right, loc);
                        }
 
-                       Type lt = left.Type;
-                       Type rt = right.Type;
+                       TypeSpec lt = left.Type;
+                       TypeSpec rt = right.Type;
                        bool bool_res;
 
-                       if (lt == TypeManager.bool_type && lt == rt) {
+                       if (lt.BuiltinType == BuiltinTypeSpec.Type.Bool && lt == rt) {
                                bool lv = (bool) left.GetValue ();
                                bool rv = (bool) right.GetValue ();                     
                                switch (oper) {
                                case Binary.Operator.BitwiseAnd:
                                case Binary.Operator.LogicalAnd:
-                                       return new BoolConstant (lv && rv, left.Location);
+                                       return new BoolConstant (ec.BuiltinTypes, lv && rv, left.Location);
                                case Binary.Operator.BitwiseOr:
                                case Binary.Operator.LogicalOr:
-                                       return new BoolConstant (lv || rv, left.Location);
+                                       return new BoolConstant (ec.BuiltinTypes, lv || rv, left.Location);
                                case Binary.Operator.ExclusiveOr:
-                                       return new BoolConstant (lv ^ rv, left.Location);
+                                       return new BoolConstant (ec.BuiltinTypes, lv ^ rv, left.Location);
                                case Binary.Operator.Equality:
-                                       return new BoolConstant (lv == rv, left.Location);
+                                       return new BoolConstant (ec.BuiltinTypes, lv == rv, left.Location);
                                case Binary.Operator.Inequality:
-                                       return new BoolConstant (lv != rv, left.Location);
+                                       return new BoolConstant (ec.BuiltinTypes, lv != rv, left.Location);
                                }
                                return null;
                        }
@@ -145,7 +149,7 @@ namespace Mono.CSharp {
                                        case Binary.Operator.ExclusiveOr:
                                                result = BinaryFold (ec, oper, ((EnumConstant)left).Child, ((EnumConstant)right).Child, loc);
                                                if (result != null)
-                                                       result = result.Resolve (ec).TryReduce (ec, lt, loc);
+                                                       result = result.TryReduce (ec, lt, loc);
                                                return result;
 
                                        ///
@@ -154,7 +158,7 @@ namespace Mono.CSharp {
                                        case Binary.Operator.Subtraction:
                                                result = BinaryFold (ec, oper, ((EnumConstant)left).Child, ((EnumConstant)right).Child, loc);
                                                if (result != null)
-                                                       result = result.Resolve (ec).TryReduce (ec, ((EnumConstant)left).Child.Type, loc);
+                                                       result = result.TryReduce (ec, EnumSpec.GetUnderlyingType (lt), loc);
                                                return result;
 
                                        ///
@@ -178,33 +182,67 @@ namespace Mono.CSharp {
 
                        switch (oper){
                        case Binary.Operator.BitwiseOr:
+                               //
+                               // bool? operator &(bool? x, bool? y);
+                               //
+                               if ((lt.BuiltinType == BuiltinTypeSpec.Type.Bool && right is NullLiteral) ||
+                                       (rt.BuiltinType == BuiltinTypeSpec.Type.Bool && left is NullLiteral)) {
+                                       var b = new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
+
+                                       // false | null => null
+                                       // null | false => null
+                                       if ((right is NullLiteral && left.IsDefaultValue) || (left is NullLiteral && right.IsDefaultValue))
+                                               return Nullable.LiftedNull.CreateFromExpression (ec, b);
+
+                                       // true | null => true
+                                       // null | true => true
+                                       return ReducedExpression.Create (new BoolConstant (ec.BuiltinTypes, true, loc), b);                                     
+                               }
+
                                if (!DoBinaryNumericPromotions (ec, ref left, ref right))
                                        return null;
 
                                if (left is IntConstant){
                                        int res = ((IntConstant) left).Value | ((IntConstant) right).Value;
-                                       
-                                       return new IntConstant (res, left.Location);
+
+                                       return new IntConstant (ec.BuiltinTypes, res, left.Location);
                                }
                                if (left is UIntConstant){
                                        uint res = ((UIntConstant)left).Value | ((UIntConstant)right).Value;
-                                       
-                                       return new UIntConstant (res, left.Location);
+
+                                       return new UIntConstant (ec.BuiltinTypes, res, left.Location);
                                }
                                if (left is LongConstant){
                                        long res = ((LongConstant)left).Value | ((LongConstant)right).Value;
-                                       
-                                       return new LongConstant (res, left.Location);
+
+                                       return new LongConstant (ec.BuiltinTypes, res, left.Location);
                                }
                                if (left is ULongConstant){
                                        ulong res = ((ULongConstant)left).Value |
                                                ((ULongConstant)right).Value;
-                                       
-                                       return new ULongConstant (res, left.Location);
+
+                                       return new ULongConstant (ec.BuiltinTypes, res, left.Location);
                                }
                                break;
                                
                        case Binary.Operator.BitwiseAnd:
+                               //
+                               // bool? operator &(bool? x, bool? y);
+                               //
+                               if ((lt.BuiltinType == BuiltinTypeSpec.Type.Bool && right is NullLiteral) ||
+                                       (rt.BuiltinType == BuiltinTypeSpec.Type.Bool && left is NullLiteral)) {
+                                       var b = new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
+
+                                       // false & null => false
+                                       // null & false => false
+                                       if ((right is NullLiteral && left.IsDefaultValue) || (left is NullLiteral && right.IsDefaultValue))
+                                               return ReducedExpression.Create (new BoolConstant (ec.BuiltinTypes, false, loc), b);
+
+                                       // true & null => null
+                                       // null & true => null
+                                       return Nullable.LiftedNull.CreateFromExpression (ec, b);
+                               }
+
                                if (!DoBinaryNumericPromotions (ec, ref left, ref right))
                                        return null;
                                
@@ -216,21 +254,21 @@ namespace Mono.CSharp {
                                ///
                                if (left is IntConstant){
                                        int res = ((IntConstant) left).Value & ((IntConstant) right).Value;
-                                       return new IntConstant (res, left.Location);
+                                       return new IntConstant (ec.BuiltinTypes, res, left.Location);
                                }
                                if (left is UIntConstant){
                                        uint res = ((UIntConstant)left).Value & ((UIntConstant)right).Value;
-                                       return new UIntConstant (res, left.Location);
+                                       return new UIntConstant (ec.BuiltinTypes, res, left.Location);
                                }
                                if (left is LongConstant){
                                        long res = ((LongConstant)left).Value & ((LongConstant)right).Value;
-                                       return new LongConstant (res, left.Location);
+                                       return new LongConstant (ec.BuiltinTypes, res, left.Location);
                                }
                                if (left is ULongConstant){
                                        ulong res = ((ULongConstant)left).Value &
                                                ((ULongConstant)right).Value;
-                                       
-                                       return new ULongConstant (res, left.Location);
+
+                                       return new ULongConstant (ec.BuiltinTypes, res, left.Location);
                                }
                                break;
 
@@ -240,31 +278,31 @@ namespace Mono.CSharp {
                                
                                if (left is IntConstant){
                                        int res = ((IntConstant) left).Value ^ ((IntConstant) right).Value;
-                                       return new IntConstant (res, left.Location);
+                                       return new IntConstant (ec.BuiltinTypes, res, left.Location);
                                }
                                if (left is UIntConstant){
                                        uint res = ((UIntConstant)left).Value ^ ((UIntConstant)right).Value;
-                                       
-                                       return  new UIntConstant (res, left.Location);
+
+                                       return new UIntConstant (ec.BuiltinTypes, res, left.Location);
                                }
                                if (left is LongConstant){
                                        long res = ((LongConstant)left).Value ^ ((LongConstant)right).Value;
-                                       
-                                       return new LongConstant (res, left.Location);
+
+                                       return new LongConstant (ec.BuiltinTypes, res, left.Location);
                                }
                                if (left is ULongConstant){
                                        ulong res = ((ULongConstant)left).Value ^
                                                ((ULongConstant)right).Value;
-                                       
-                                       return new ULongConstant (res, left.Location);
+
+                                       return new ULongConstant (ec.BuiltinTypes, res, left.Location);
                                }
                                break;
 
                        case Binary.Operator.Addition:
-                               if (lt == TypeManager.null_type)
+                               if (lt == InternalType.NullLiteral)
                                        return right;
 
-                               if (rt == TypeManager.null_type)
+                               if (rt == InternalType.NullLiteral)
                                        return left;
 
                                //
@@ -272,9 +310,9 @@ namespace Mono.CSharp {
                                // one is a string, and the other is not, then defer
                                // to runtime concatenation
                                //
-                               if (lt == TypeManager.string_type || rt == TypeManager.string_type){
+                               if (lt.BuiltinType == BuiltinTypeSpec.Type.String || rt.BuiltinType == BuiltinTypeSpec.Type.String){
                                        if (lt == rt)
-                                               return new StringConstant ((string)left.GetValue () + (string)right.GetValue (),
+                                               return new StringConstant (ec.BuiltinTypes, (string)left.GetValue () + (string)right.GetValue (),
                                                        left.Location);
                                        
                                        return null;
@@ -294,7 +332,7 @@ namespace Mono.CSharp {
                                        }
 
                                        // U has to be implicitly convetible to E.base
-                                       right = right.ConvertImplicitly (ec, lc.Child.Type);
+                                       right = right.ConvertImplicitly (lc.Child.Type);
                                        if (right == null)
                                                return null;
 
@@ -302,7 +340,7 @@ namespace Mono.CSharp {
                                        if (result == null)
                                                return null;
 
-                                       result = result.Resolve (ec).TryReduce (ec, lt, loc);
+                                       result = result.TryReduce (ec, lt, loc);
                                        if (result == null)
                                                return null;
 
@@ -322,8 +360,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((DoubleConstant) left).Value +
                                                                         ((DoubleConstant) right).Value);
-                                               
-                                               return new DoubleConstant (res, left.Location);
+
+                                               return new DoubleConstant (ec.BuiltinTypes, res, left.Location);
                                        }
                                        if (left is FloatConstant){
                                                float res;
@@ -334,8 +372,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((FloatConstant) left).Value +
                                                                         ((FloatConstant) right).Value);
-                                               
-                                               result = new FloatConstant (res, left.Location);
+
+                                               result = new FloatConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is ULongConstant){
                                                ulong res;
                                                
@@ -346,7 +384,7 @@ namespace Mono.CSharp {
                                                        res = unchecked (((ULongConstant) left).Value +
                                                                         ((ULongConstant) right).Value);
 
-                                               result = new ULongConstant (res, left.Location);
+                                               result = new ULongConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is LongConstant){
                                                long res;
                                                
@@ -356,8 +394,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((LongConstant) left).Value +
                                                                         ((LongConstant) right).Value);
-                                               
-                                               result = new LongConstant (res, left.Location);
+
+                                               result = new LongConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is UIntConstant){
                                                uint res;
                                                
@@ -367,8 +405,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((UIntConstant) left).Value +
                                                                         ((UIntConstant) right).Value);
-                                               
-                                               result = new UIntConstant (res, left.Location);
+
+                                               result = new UIntConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is IntConstant){
                                                int res;
 
@@ -379,7 +417,7 @@ namespace Mono.CSharp {
                                                        res = unchecked (((IntConstant) left).Value +
                                                                         ((IntConstant) right).Value);
 
-                                               result = new IntConstant (res, left.Location);
+                                               result = new IntConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is DecimalConstant) {
                                                decimal res;
 
@@ -390,7 +428,7 @@ namespace Mono.CSharp {
                                                        res = unchecked (((DecimalConstant) left).Value +
                                                                ((DecimalConstant) right).Value);
 
-                                               result = new DecimalConstant (res, left.Location);
+                                               result = new DecimalConstant (ec.BuiltinTypes, res, left.Location);
                                        }
                                } catch (OverflowException){
                                        Error_CompileTimeOverflow (ec, loc);
@@ -413,7 +451,7 @@ namespace Mono.CSharp {
                                        }
 
                                        // U has to be implicitly convetible to E.base
-                                       right = right.ConvertImplicitly (ec, lc.Child.Type);
+                                       right = right.ConvertImplicitly (lc.Child.Type);
                                        if (right == null)
                                                return null;
 
@@ -421,13 +459,19 @@ namespace Mono.CSharp {
                                        if (result == null)
                                                return null;
 
-                                       result = result.Resolve (ec).TryReduce (ec, lt, loc);
+                                       result = result.TryReduce (ec, lt, loc);
                                        if (result == null)
                                                return null;
 
                                        return new EnumConstant (result, lt);
                                }
 
+                               if (left is NullLiteral && right is NullLiteral) {
+                                       var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
+                                       lifted_int.ResolveAsType (ec);
+                                       return (Constant) new Nullable.LiftedBinaryOperator (oper, lifted_int, right, loc).Resolve (ec);
+                               }
+
                                if (!DoBinaryNumericPromotions (ec, ref left, ref right))
                                        return null;
 
@@ -441,8 +485,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((DoubleConstant) left).Value -
                                                                         ((DoubleConstant) right).Value);
-                                               
-                                               result = new DoubleConstant (res, left.Location);
+
+                                               result = new DoubleConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is FloatConstant){
                                                float res;
                                                
@@ -452,8 +496,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((FloatConstant) left).Value -
                                                                         ((FloatConstant) right).Value);
-                                               
-                                               result = new FloatConstant (res, left.Location);
+
+                                               result = new FloatConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is ULongConstant){
                                                ulong res;
                                                
@@ -463,8 +507,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((ULongConstant) left).Value -
                                                                         ((ULongConstant) right).Value);
-                                               
-                                               result = new ULongConstant (res, left.Location);
+
+                                               result = new ULongConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is LongConstant){
                                                long res;
                                                
@@ -474,8 +518,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((LongConstant) left).Value -
                                                                         ((LongConstant) right).Value);
-                                               
-                                               result = new LongConstant (res, left.Location);
+
+                                               result = new LongConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is UIntConstant){
                                                uint res;
                                                
@@ -485,8 +529,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((UIntConstant) left).Value -
                                                                         ((UIntConstant) right).Value);
-                                               
-                                               result = new UIntConstant (res, left.Location);
+
+                                               result = new UIntConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is IntConstant){
                                                int res;
 
@@ -497,7 +541,7 @@ namespace Mono.CSharp {
                                                        res = unchecked (((IntConstant) left).Value -
                                                                         ((IntConstant) right).Value);
 
-                                               result = new IntConstant (res, left.Location);
+                                               result = new IntConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is DecimalConstant) {
                                                decimal res;
 
@@ -508,7 +552,7 @@ namespace Mono.CSharp {
                                                        res = unchecked (((DecimalConstant) left).Value -
                                                                ((DecimalConstant) right).Value);
 
-                                               return new DecimalConstant (res, left.Location);
+                                               return new DecimalConstant (ec.BuiltinTypes, res, left.Location);
                                        } else {
                                                throw new Exception ( "Unexepected subtraction input: " + left);
                                        }
@@ -519,6 +563,12 @@ namespace Mono.CSharp {
                                return result;
                                
                        case Binary.Operator.Multiply:
+                               if (left is NullLiteral && right is NullLiteral) {
+                                       var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
+                                       lifted_int.ResolveAsType (ec);
+                                       return (Constant) new Nullable.LiftedBinaryOperator (oper, lifted_int, right, loc).Resolve (ec);
+                               }
+
                                if (!DoBinaryNumericPromotions (ec, ref left, ref right))
                                        return null;
 
@@ -532,8 +582,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((DoubleConstant) left).Value *
                                                                ((DoubleConstant) right).Value);
-                                               
-                                               return new DoubleConstant (res, left.Location);
+
+                                               return new DoubleConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is FloatConstant){
                                                float res;
                                                
@@ -543,8 +593,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((FloatConstant) left).Value *
                                                                ((FloatConstant) right).Value);
-                                               
-                                               return new FloatConstant (res, left.Location);
+
+                                               return new FloatConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is ULongConstant){
                                                ulong res;
                                                
@@ -554,8 +604,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((ULongConstant) left).Value *
                                                                ((ULongConstant) right).Value);
-                                               
-                                               return new ULongConstant (res, left.Location);
+
+                                               return new ULongConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is LongConstant){
                                                long res;
                                                
@@ -565,8 +615,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((LongConstant) left).Value *
                                                                ((LongConstant) right).Value);
-                                               
-                                               return new LongConstant (res, left.Location);
+
+                                               return new LongConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is UIntConstant){
                                                uint res;
                                                
@@ -576,8 +626,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((UIntConstant) left).Value *
                                                                ((UIntConstant) right).Value);
-                                               
-                                               return new UIntConstant (res, left.Location);
+
+                                               return new UIntConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is IntConstant){
                                                int res;
 
@@ -588,7 +638,7 @@ namespace Mono.CSharp {
                                                        res = unchecked (((IntConstant) left).Value *
                                                                ((IntConstant) right).Value);
 
-                                               return new IntConstant (res, left.Location);
+                                               return new IntConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is DecimalConstant) {
                                                decimal res;
 
@@ -599,7 +649,7 @@ namespace Mono.CSharp {
                                                        res = unchecked (((DecimalConstant) left).Value *
                                                                ((DecimalConstant) right).Value);
 
-                                               return new DecimalConstant (res, left.Location);
+                                               return new DecimalConstant (ec.BuiltinTypes, res, left.Location);
                                        } else {
                                                throw new Exception ( "Unexepected multiply input: " + left);
                                        }
@@ -609,6 +659,12 @@ namespace Mono.CSharp {
                                break;
 
                        case Binary.Operator.Division:
+                               if (left is NullLiteral && right is NullLiteral) {
+                                       var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
+                                       lifted_int.ResolveAsType (ec);
+                                       return (Constant) new Nullable.LiftedBinaryOperator (oper, lifted_int, right, loc).Resolve (ec);
+                               }
+
                                if (!DoBinaryNumericPromotions (ec, ref left, ref right))
                                        return null;
 
@@ -622,8 +678,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((DoubleConstant) left).Value /
                                                                ((DoubleConstant) right).Value);
-                                               
-                                               return new DoubleConstant (res, left.Location);
+
+                                               return new DoubleConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is FloatConstant){
                                                float res;
                                                
@@ -633,8 +689,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((FloatConstant) left).Value /
                                                                ((FloatConstant) right).Value);
-                                               
-                                               return new FloatConstant (res, left.Location);
+
+                                               return new FloatConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is ULongConstant){
                                                ulong res;
                                                
@@ -644,8 +700,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((ULongConstant) left).Value /
                                                                ((ULongConstant) right).Value);
-                                               
-                                               return new ULongConstant (res, left.Location);
+
+                                               return new ULongConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is LongConstant){
                                                long res;
                                                
@@ -655,8 +711,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((LongConstant) left).Value /
                                                                ((LongConstant) right).Value);
-                                               
-                                               return new LongConstant (res, left.Location);
+
+                                               return new LongConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is UIntConstant){
                                                uint res;
                                                
@@ -666,8 +722,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((UIntConstant) left).Value /
                                                                ((UIntConstant) right).Value);
-                                               
-                                               return new UIntConstant (res, left.Location);
+
+                                               return new UIntConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is IntConstant){
                                                int res;
 
@@ -678,7 +734,7 @@ namespace Mono.CSharp {
                                                        res = unchecked (((IntConstant) left).Value /
                                                                ((IntConstant) right).Value);
 
-                                               return new IntConstant (res, left.Location);
+                                               return new IntConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is DecimalConstant) {
                                                decimal res;
 
@@ -689,7 +745,7 @@ namespace Mono.CSharp {
                                                        res = unchecked (((DecimalConstant) left).Value /
                                                                ((DecimalConstant) right).Value);
 
-                                               return new DecimalConstant (res, left.Location);
+                                               return new DecimalConstant (ec.BuiltinTypes, res, left.Location);
                                        } else {
                                                throw new Exception ( "Unexepected division input: " + left);
                                        }
@@ -703,6 +759,12 @@ namespace Mono.CSharp {
                                break;
                                
                        case Binary.Operator.Modulus:
+                               if (left is NullLiteral && right is NullLiteral) {
+                                       var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
+                                       lifted_int.ResolveAsType (ec);
+                                       return (Constant) new Nullable.LiftedBinaryOperator (oper, lifted_int, right, loc).Resolve (ec);
+                               }
+
                                if (!DoBinaryNumericPromotions (ec, ref left, ref right))
                                        return null;
 
@@ -716,8 +778,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((DoubleConstant) left).Value %
                                                                         ((DoubleConstant) right).Value);
-                                               
-                                               return new DoubleConstant (res, left.Location);
+
+                                               return new DoubleConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is FloatConstant){
                                                float res;
                                                
@@ -727,8 +789,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((FloatConstant) left).Value %
                                                                         ((FloatConstant) right).Value);
-                                               
-                                               return new FloatConstant (res, left.Location);
+
+                                               return new FloatConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is ULongConstant){
                                                ulong res;
                                                
@@ -738,8 +800,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((ULongConstant) left).Value %
                                                                         ((ULongConstant) right).Value);
-                                               
-                                               return new ULongConstant (res, left.Location);
+
+                                               return new ULongConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is LongConstant){
                                                long res;
                                                
@@ -749,8 +811,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((LongConstant) left).Value %
                                                                         ((LongConstant) right).Value);
-                                               
-                                               return new LongConstant (res, left.Location);
+
+                                               return new LongConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is UIntConstant){
                                                uint res;
                                                
@@ -760,8 +822,8 @@ namespace Mono.CSharp {
                                                else
                                                        res = unchecked (((UIntConstant) left).Value %
                                                                         ((UIntConstant) right).Value);
-                                               
-                                               return new UIntConstant (res, left.Location);
+
+                                               return new UIntConstant (ec.BuiltinTypes, res, left.Location);
                                        } else if (left is IntConstant){
                                                int res;
 
@@ -772,7 +834,7 @@ namespace Mono.CSharp {
                                                        res = unchecked (((IntConstant) left).Value %
                                                                         ((IntConstant) right).Value);
 
-                                               return new IntConstant (res, left.Location);
+                                               return new IntConstant (ec.BuiltinTypes, res, left.Location);
                                        } else {
                                                throw new Exception ( "Unexepected modulus input: " + left);
                                        }
@@ -787,23 +849,35 @@ namespace Mono.CSharp {
                                // There is no overflow checking on left shift
                                //
                        case Binary.Operator.LeftShift:
-                               IntConstant ic = right.ConvertImplicitly (ec, TypeManager.int32_type) as IntConstant;
+                               if (left is NullLiteral && right is NullLiteral) {
+                                       var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
+                                       lifted_int.ResolveAsType (ec);
+                                       return (Constant) new Nullable.LiftedBinaryOperator (oper, lifted_int, right, loc).Resolve (ec);
+                               }
+
+                               IntConstant ic = right.ConvertImplicitly (ec.BuiltinTypes.Int) as IntConstant;
                                if (ic == null){
                                        Binary.Error_OperatorCannotBeApplied (ec, left, right, oper, loc);
                                        return null;
                                }
 
                                int lshift_val = ic.Value;
-                               if (left.Type == TypeManager.uint64_type)
-                                       return new ULongConstant (((ULongConstant)left).Value << lshift_val, left.Location);
-                               if (left.Type == TypeManager.int64_type)
-                                       return new LongConstant (((LongConstant)left).Value << lshift_val, left.Location);
-                               if (left.Type == TypeManager.uint32_type)
-                                       return new UIntConstant (((UIntConstant)left).Value << lshift_val, left.Location);
+                               switch (left.Type.BuiltinType) {
+                               case BuiltinTypeSpec.Type.ULong:
+                                       return new ULongConstant (ec.BuiltinTypes, ((ULongConstant) left).Value << lshift_val, left.Location);
+                               case BuiltinTypeSpec.Type.Long:
+                                       return new LongConstant (ec.BuiltinTypes, ((LongConstant) left).Value << lshift_val, left.Location);
+                               case BuiltinTypeSpec.Type.UInt:
+                                       return new UIntConstant (ec.BuiltinTypes, ((UIntConstant) left).Value << lshift_val, left.Location);
+                               }
 
-                               left = left.ConvertImplicitly (ec, TypeManager.int32_type);
-                               if (left.Type == TypeManager.int32_type)
-                                       return new IntConstant (((IntConstant)left).Value << lshift_val, left.Location);
+                               // null << value => null
+                               if (left is NullLiteral)
+                                       return (Constant) new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
+
+                               left = left.ConvertImplicitly (ec.BuiltinTypes.Int);
+                               if (left.Type.BuiltinType == BuiltinTypeSpec.Type.Int)
+                                       return new IntConstant (ec.BuiltinTypes, ((IntConstant) left).Value << lshift_val, left.Location);
 
                                Binary.Error_OperatorCannotBeApplied (ec, left, right, oper, loc);
                                break;
@@ -812,36 +886,50 @@ namespace Mono.CSharp {
                                // There is no overflow checking on right shift
                                //
                        case Binary.Operator.RightShift:
-                               IntConstant sic = right.ConvertImplicitly (ec, TypeManager.int32_type) as IntConstant;
+                               if (left is NullLiteral && right is NullLiteral) {
+                                       var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
+                                       lifted_int.ResolveAsType (ec);
+                                       return (Constant) new Nullable.LiftedBinaryOperator (oper, lifted_int, right, loc).Resolve (ec);
+                               }
+
+                               IntConstant sic = right.ConvertImplicitly (ec.BuiltinTypes.Int) as IntConstant;
                                if (sic == null){
                                        Binary.Error_OperatorCannotBeApplied (ec, left, right, oper, loc); ;
                                        return null;
                                }
                                int rshift_val = sic.Value;
-                               if (left.Type == TypeManager.uint64_type)
-                                       return new ULongConstant (((ULongConstant)left).Value >> rshift_val, left.Location);
-                               if (left.Type == TypeManager.int64_type)
-                                       return new LongConstant (((LongConstant)left).Value >> rshift_val, left.Location);
-                               if (left.Type == TypeManager.uint32_type)
-                                       return new UIntConstant (((UIntConstant)left).Value >> rshift_val, left.Location);
+                               switch (left.Type.BuiltinType) {
+                               case BuiltinTypeSpec.Type.ULong:
+                                       return new ULongConstant (ec.BuiltinTypes, ((ULongConstant) left).Value >> rshift_val, left.Location);
+                               case BuiltinTypeSpec.Type.Long:
+                                       return new LongConstant (ec.BuiltinTypes, ((LongConstant) left).Value >> rshift_val, left.Location);
+                               case BuiltinTypeSpec.Type.UInt:
+                                       return new UIntConstant (ec.BuiltinTypes, ((UIntConstant) left).Value >> rshift_val, left.Location);
+                               }
+
+                               // null >> value => null
+                               if (left is NullLiteral)
+                                       return (Constant) new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
 
-                               left = left.ConvertImplicitly (ec, TypeManager.int32_type);
-                               if (left.Type == TypeManager.int32_type)
-                                       return new IntConstant (((IntConstant)left).Value >> rshift_val, left.Location);
+                               left = left.ConvertImplicitly (ec.BuiltinTypes.Int);
+                               if (left.Type.BuiltinType == BuiltinTypeSpec.Type.Int)
+                                       return new IntConstant (ec.BuiltinTypes, ((IntConstant) left).Value >> rshift_val, left.Location);
 
                                Binary.Error_OperatorCannotBeApplied (ec, left, right, oper, loc);
                                break;
 
                        case Binary.Operator.Equality:
-                               if (TypeManager.IsReferenceType (lt) && TypeManager.IsReferenceType (lt)) {
+                               if (TypeSpec.IsReferenceType (lt) && TypeSpec.IsReferenceType (rt) ||
+                                       (left is Nullable.LiftedNull && right.IsNull) ||
+                                       (right is Nullable.LiftedNull && left.IsNull)) {
                                        if (left.IsNull || right.IsNull) {
                                                return ReducedExpression.Create (
-                                                       new BoolConstant (left.IsNull == right.IsNull, left.Location).Resolve (ec),
-                                                       new Binary (oper, left, right));
+                                                       new BoolConstant (ec.BuiltinTypes, left.IsNull == right.IsNull, left.Location),
+                                                       new Binary (oper, left, right, loc));
                                        }
 
                                        if (left is StringConstant && right is StringConstant)
-                                               return new BoolConstant (
+                                               return new BoolConstant (ec.BuiltinTypes,
                                                        ((StringConstant) left).Value == ((StringConstant) right).Value, left.Location);
 
                                        return null;
@@ -872,18 +960,20 @@ namespace Mono.CSharp {
                                else
                                        return null;
 
-                               return new BoolConstant (bool_res, left.Location);
+                               return new BoolConstant (ec.BuiltinTypes, bool_res, left.Location);
 
                        case Binary.Operator.Inequality:
-                               if (TypeManager.IsReferenceType (lt) && TypeManager.IsReferenceType (lt)) {
+                               if (TypeSpec.IsReferenceType (lt) && TypeSpec.IsReferenceType (rt) ||
+                                       (left is Nullable.LiftedNull && right.IsNull) ||
+                                       (right is Nullable.LiftedNull && left.IsNull)) {
                                        if (left.IsNull || right.IsNull) {
                                                return ReducedExpression.Create (
-                                                       new BoolConstant (left.IsNull != right.IsNull, left.Location).Resolve (ec),
-                                                       new Binary (oper, left, right));
+                                                       new BoolConstant (ec.BuiltinTypes, left.IsNull != right.IsNull, left.Location),
+                                                       new Binary (oper, left, right, loc));
                                        }
 
                                        if (left is StringConstant && right is StringConstant)
-                                               return new BoolConstant (
+                                               return new BoolConstant (ec.BuiltinTypes,
                                                        ((StringConstant) left).Value != ((StringConstant) right).Value, left.Location);
 
                                        return null;
@@ -914,9 +1004,21 @@ namespace Mono.CSharp {
                                else
                                        return null;
 
-                               return new BoolConstant (bool_res, left.Location);
+                               return new BoolConstant (ec.BuiltinTypes, bool_res, left.Location);
 
                        case Binary.Operator.LessThan:
+                               if (right is NullLiteral) {
+                                       if (left is NullLiteral) {
+                                               var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
+                                               lifted_int.ResolveAsType (ec);
+                                               return (Constant) new Nullable.LiftedBinaryOperator (oper, lifted_int, right, loc).Resolve (ec);
+                                       }
+
+                                       if (left is Nullable.LiftedNull) {
+                                               return (Constant) new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
+                                       }
+                               }
+
                                if (!DoBinaryNumericPromotions (ec, ref left, ref right))
                                        return null;
 
@@ -942,9 +1044,21 @@ namespace Mono.CSharp {
                                else
                                        return null;
 
-                               return new BoolConstant (bool_res, left.Location);
+                               return new BoolConstant (ec.BuiltinTypes, bool_res, left.Location);
                                
                        case Binary.Operator.GreaterThan:
+                               if (right is NullLiteral) {
+                                       if (left is NullLiteral) {
+                                               var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
+                                               lifted_int.ResolveAsType (ec);
+                                               return (Constant) new Nullable.LiftedBinaryOperator (oper, lifted_int, right, loc).Resolve (ec);
+                                       }
+
+                                       if (left is Nullable.LiftedNull) {
+                                               return (Constant) new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
+                                       }
+                               }
+
                                if (!DoBinaryNumericPromotions (ec, ref left, ref right))
                                        return null;
 
@@ -970,9 +1084,21 @@ namespace Mono.CSharp {
                                else
                                        return null;
 
-                               return new BoolConstant (bool_res, left.Location);
+                               return new BoolConstant (ec.BuiltinTypes, bool_res, left.Location);
 
                        case Binary.Operator.GreaterThanOrEqual:
+                               if (right is NullLiteral) {
+                                       if (left is NullLiteral) {
+                                               var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
+                                               lifted_int.ResolveAsType (ec);
+                                               return (Constant) new Nullable.LiftedBinaryOperator (oper, lifted_int, right, loc).Resolve (ec);
+                                       }
+
+                                       if (left is Nullable.LiftedNull) {
+                                               return (Constant) new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
+                                       }
+                               }
+
                                if (!DoBinaryNumericPromotions (ec, ref left, ref right))
                                        return null;
 
@@ -998,9 +1124,21 @@ namespace Mono.CSharp {
                                else
                                        return null;
 
-                               return new BoolConstant (bool_res, left.Location);
+                               return new BoolConstant (ec.BuiltinTypes, bool_res, left.Location);
 
                        case Binary.Operator.LessThanOrEqual:
+                               if (right is NullLiteral) {
+                                       if (left is NullLiteral) {
+                                               var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
+                                               lifted_int.ResolveAsType (ec);
+                                               return (Constant) new Nullable.LiftedBinaryOperator (oper, lifted_int, right, loc).Resolve (ec);
+                                       }
+
+                                       if (left is Nullable.LiftedNull) {
+                                               return (Constant) new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
+                                       }
+                               }
+
                                if (!DoBinaryNumericPromotions (ec, ref left, ref right))
                                        return null;
 
@@ -1026,7 +1164,7 @@ namespace Mono.CSharp {
                                else
                                        return null;
 
-                               return new BoolConstant (bool_res, left.Location);
+                               return new BoolConstant (ec.BuiltinTypes, bool_res, left.Location);
                        }
                                        
                        return null;