Fix compilation error
[mono.git] / mcs / mcs / constant.cs
index ead8ce025fe297af35f1947a3b041cab2ebe36f0..c5a38e79439a419930ac5aaca968989b7cad69db 100644 (file)
@@ -41,6 +41,11 @@ namespace Mono.CSharp {
 
                public override bool GetAttributableValue (Type valueType, out object value)
                {
+                       if (valueType == TypeManager.object_type) {
+                               value = GetTypedValue ();
+                               return true;
+                       }
+
                        Constant c = ImplicitConversionRequired (valueType, loc);
                        if (c == null) {
                                value = null;
@@ -74,7 +79,7 @@ namespace Mono.CSharp {
                {
                        Constant c = ConvertImplicitly (type);
                        if (c == null)
-                               Error_ValueCannotBeConverted (loc, type, false);
+                               Error_ValueCannotBeConverted (null, loc, type, false);
                        return c;
                }
 
@@ -83,9 +88,6 @@ namespace Mono.CSharp {
                        if (this.type == type)
                                return this;
 
-                       if (type == TypeManager.object_type)
-                               return new EmptyConstantCast (this, type);
-
                        if (Convert.ImplicitNumericConversion (this, type) == null) 
                                return null;
 
@@ -147,49 +149,6 @@ namespace Mono.CSharp {
                        throw new Exception ("Unknown type for constant (" + t +
                                        "), details: " + v);
                }
-
-               protected static void CheckRange (bool inCheckedContext, ulong value, ulong max)
-               {
-                       if (!inCheckedContext)
-                               return;
-
-                       if (value > max)
-                               throw new OverflowException ();
-               }
-
-               protected static void CheckRange (bool inCheckedContext, double value, long min, long max)
-               {
-                       if (!inCheckedContext)
-                               return;
-
-                       if (((value < min) || (value > max)))
-                               throw new OverflowException ();
-
-                       if (double.IsNaN (value))
-                               throw new OverflowException ();
-               }
-
-               protected static void CheckRange (bool inCheckedContext, double value, ulong min, ulong max)
-               {
-                       if (!inCheckedContext)
-                               return;
-
-                       if (((value < min) || (value > max)))
-                               throw new OverflowException ();
-
-                       if (double.IsNaN (value))
-                               throw new OverflowException ();
-               }
-
-               protected static void CheckUnsigned (bool inCheckedContext, long value)
-               {
-                       if (!inCheckedContext)
-                               return;
-
-                       if (value < 0)
-                               throw new OverflowException ();
-               }
-
                /// <summary>
                /// Maybe ConvertTo name is better. It tries to convert `this' constant to target_type.
                /// It throws OverflowException 
@@ -257,6 +216,11 @@ namespace Mono.CSharp {
                public virtual bool IsZeroInteger {
                        get { return false; }
                }
+
+               protected override void CloneTo (CloneContext clonectx, Expression target)
+               {
+                       // CloneTo: Nothing, we do not keep any state on this expression
+               }
        }
 
        public abstract class IntegralConstant : Constant {
@@ -265,11 +229,11 @@ namespace Mono.CSharp {
                {
                }
 
-               public override void Error_ValueCannotBeConverted (Location loc, Type target, bool expl)
+               public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
                {
                        try {
                                ConvertExplicitly (true, target);
-                               base.Error_ValueCannotBeConverted (loc, target, expl);
+                               base.Error_ValueCannotBeConverted (ec, loc, target, expl);
                        }
                        catch
                        {
@@ -388,7 +352,10 @@ namespace Mono.CSharp {
                public override Constant ConvertExplicitly (bool inCheckedContext, Type target_type)
                {
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type)
@@ -496,15 +463,24 @@ namespace Mono.CSharp {
                public override Constant ConvertExplicitly (bool inCheckedContext, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (inCheckedContext, Value, Byte.MinValue, Byte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < Byte.MinValue || Value > Byte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type) {
-                               CheckRange (inCheckedContext, Value, Int16.MinValue, Int16.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value > Int16.MaxValue)
+                                               throw new OverflowException ();
+                               }                                       
                                return new ShortConstant ((short) Value, Location);
                        }
                        if (target_type == TypeManager.int32_type)
@@ -577,23 +553,27 @@ namespace Mono.CSharp {
                public override Constant ConvertExplicitly (bool inCheckedContext, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckUnsigned (inCheckedContext, Value);
+                               if (inCheckedContext && Value < 0)
+                                       throw new OverflowException ();
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type)
                                return new ShortConstant ((short) Value, Location);
                        if (target_type == TypeManager.ushort_type) {
-                               CheckUnsigned (inCheckedContext, Value);
+                               if (inCheckedContext && Value < 0)
+                                       throw new OverflowException ();
                                return new UShortConstant ((ushort) Value, Location);
                        } if (target_type == TypeManager.int32_type)
                                  return new IntConstant ((int) Value, Location);
                        if (target_type == TypeManager.uint32_type) {
-                               CheckUnsigned (inCheckedContext, Value);
+                               if (inCheckedContext && Value < 0)
+                                       throw new OverflowException ();
                                return new UIntConstant ((uint) Value, Location);
                        } if (target_type == TypeManager.int64_type)
                                  return new LongConstant ((long) Value, Location);
                        if (target_type == TypeManager.uint64_type) {
-                               CheckUnsigned (inCheckedContext, Value);
+                               if (inCheckedContext && Value < 0)
+                                       throw new OverflowException ();
                                return new ULongConstant ((ulong) Value, Location);
                        }
                        if (target_type == TypeManager.float_type)
@@ -601,7 +581,8 @@ namespace Mono.CSharp {
                        if (target_type == TypeManager.double_type)
                                return new DoubleConstant ((double) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckUnsigned (inCheckedContext, Value);
+                               if (inCheckedContext && Value < 0)
+                                       throw new OverflowException ();
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -662,27 +643,37 @@ namespace Mono.CSharp {
                public override Constant ConvertExplicitly (bool inCheckedContext, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (inCheckedContext, Value, Byte.MinValue, Byte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < Byte.MinValue || Value > Byte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < SByte.MinValue || Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.ushort_type) {
-                               CheckUnsigned (inCheckedContext, Value);
+                               if (inCheckedContext && Value < 0)
+                                       throw new OverflowException ();
+                               
                                return new UShortConstant ((ushort) Value, Location);
                        }
                        if (target_type == TypeManager.int32_type)
                                return new IntConstant ((int) Value, Location);
                        if (target_type == TypeManager.uint32_type) {
-                               CheckUnsigned (inCheckedContext, Value);
+                               if (inCheckedContext && Value < 0)
+                                       throw new OverflowException ();
                                return new UIntConstant ((uint) Value, Location);
                        }
                        if (target_type == TypeManager.int64_type)
                                return new LongConstant ((long) Value, Location);
                        if (target_type == TypeManager.uint64_type) {
-                               CheckUnsigned (inCheckedContext, Value);
+                               if (inCheckedContext && Value < 0)
+                                       throw new OverflowException ();
                                return new ULongConstant ((ulong) Value, Location);
                        }
                        if (target_type == TypeManager.float_type)
@@ -690,7 +681,10 @@ namespace Mono.CSharp {
                        if (target_type == TypeManager.double_type)
                                return new DoubleConstant ((double) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckRange (inCheckedContext, Value, Char.MinValue, Char.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < Char.MinValue)
+                                               throw new OverflowException ();
+                               }
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -751,15 +745,24 @@ namespace Mono.CSharp {
                public override Constant ConvertExplicitly (bool inCheckedContext, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (inCheckedContext, Value, Byte.MinValue, Byte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value > Byte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type) {
-                               CheckRange (inCheckedContext, Value, Int16.MinValue, Int16.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value > Int16.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ShortConstant ((short) Value, Location);
                        }
                        if (target_type == TypeManager.int32_type)
@@ -775,7 +778,10 @@ namespace Mono.CSharp {
                        if (target_type == TypeManager.double_type)
                                return new DoubleConstant ((double) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckRange (inCheckedContext, Value, Char.MinValue, Char.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value > Char.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -887,29 +893,45 @@ namespace Mono.CSharp {
                public override Constant ConvertExplicitly (bool inCheckedContext, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (inCheckedContext, Value, Byte.MinValue, Byte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < Byte.MinValue || Value > Byte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < SByte.MinValue || Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type) {
-                               CheckRange (inCheckedContext, Value, Int16.MinValue, Int16.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < Int16.MinValue || Value > Int16.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ShortConstant ((short) Value, Location);
                        }
                        if (target_type == TypeManager.ushort_type) {
-                               CheckRange (inCheckedContext, Value, UInt16.MinValue, UInt16.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < UInt16.MinValue || Value > UInt16.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new UShortConstant ((ushort) Value, Location);
                        }
                        if (target_type == TypeManager.uint32_type) {
-                               CheckRange (inCheckedContext, Value, UInt32.MinValue, UInt32.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < UInt32.MinValue)
+                                               throw new OverflowException ();
+                               }
                                return new UIntConstant ((uint) Value, Location);
                        }
                        if (target_type == TypeManager.int64_type)
                                return new LongConstant ((long) Value, Location);
                        if (target_type == TypeManager.uint64_type) {
-                               CheckUnsigned (inCheckedContext, Value);
+                               if (inCheckedContext && Value < 0)
+                                       throw new OverflowException ();
                                return new ULongConstant ((ulong) Value, Location);
                        }
                        if (target_type == TypeManager.float_type)
@@ -917,7 +939,10 @@ namespace Mono.CSharp {
                        if (target_type == TypeManager.double_type)
                                return new DoubleConstant ((double) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckRange (inCheckedContext, Value, Char.MinValue, Char.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < Char.MinValue || Value > Char.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -925,6 +950,62 @@ namespace Mono.CSharp {
 
                        return null;
                }
+
+               public override Constant ConvertImplicitly (Type type)
+               {
+                       if (this.type == type)
+                               return this;
+
+                       Constant c = TryImplicitIntConversion (type);
+                       if (c != null)
+                               return c;
+
+                       return base.ConvertImplicitly (type);
+               }
+
+               /// <summary>
+               ///   Attempts to perform an implicit constant conversion of the IntConstant
+               ///   into a different data type using casts (See Implicit Constant
+               ///   Expression Conversions)
+               /// </summary>
+               Constant TryImplicitIntConversion (Type target_type)
+               {
+                       if (target_type == TypeManager.sbyte_type) {
+                               if (Value >= SByte.MinValue && Value <= SByte.MaxValue)
+                                       return new SByteConstant ((sbyte) Value, loc);
+                       } 
+                       else if (target_type == TypeManager.byte_type) {
+                               if (Value >= Byte.MinValue && Value <= Byte.MaxValue)
+                                       return new ByteConstant ((byte) Value, loc);
+                       } 
+                       else if (target_type == TypeManager.short_type) {
+                               if (Value >= Int16.MinValue && Value <= Int16.MaxValue)
+                                       return new ShortConstant ((short) Value, loc);
+                       } 
+                       else if (target_type == TypeManager.ushort_type) {
+                               if (Value >= UInt16.MinValue && Value <= UInt16.MaxValue)
+                                       return new UShortConstant ((ushort) Value, loc);
+                       } 
+                       else if (target_type == TypeManager.uint32_type) {
+                               if (Value >= 0)
+                                       return new UIntConstant ((uint) Value, loc);
+                       } 
+                       else if (target_type == TypeManager.uint64_type) {
+                               //
+                               // we can optimize this case: a positive int32
+                               // always fits on a uint64.  But we need an opcode
+                               // to do it.
+                               //
+                               if (Value >= 0)
+                                       return new ULongConstant ((ulong) Value, loc);
+                       } 
+                       else if (target_type == TypeManager.double_type)
+                               return new DoubleConstant ((double) Value, loc);
+                       else if (target_type == TypeManager.float_type)
+                               return new FloatConstant ((float) Value, loc);
+
+                       return null;
+               }
        }
 
        public class UIntConstant : IntegralConstant {
@@ -977,23 +1058,38 @@ namespace Mono.CSharp {
                public override Constant ConvertExplicitly (bool inCheckedContext, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (inCheckedContext, Value, Char.MinValue, Char.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < Char.MinValue || Value > Char.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type) {
-                               CheckRange (inCheckedContext, Value, Int16.MinValue, Int16.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value > Int16.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ShortConstant ((short) Value, Location);
                        }
                        if (target_type == TypeManager.ushort_type) {
-                               CheckRange (inCheckedContext, Value, UInt16.MinValue, UInt16.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < UInt16.MinValue || Value > UInt16.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new UShortConstant ((ushort) Value, Location);
                        }
                        if (target_type == TypeManager.int32_type) {
-                               CheckRange (inCheckedContext, Value, Int32.MinValue, Int32.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value > Int32.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new IntConstant ((int) Value, Location);
                        }
                        if (target_type == TypeManager.int64_type)
@@ -1005,7 +1101,10 @@ namespace Mono.CSharp {
                        if (target_type == TypeManager.double_type)
                                return new DoubleConstant ((double) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckRange (inCheckedContext, Value, Char.MinValue, Char.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < Char.MinValue || Value > Char.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -1076,31 +1175,50 @@ namespace Mono.CSharp {
                public override Constant ConvertExplicitly (bool inCheckedContext, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (inCheckedContext, Value, Byte.MinValue, Byte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < Byte.MinValue || Value > Byte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < SByte.MinValue || Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type) {
-                               CheckRange (inCheckedContext, Value, Int16.MinValue, Int16.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < Int16.MinValue || Value > Int16.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ShortConstant ((short) Value, Location);
                        }
                        if (target_type == TypeManager.ushort_type) {
-                               CheckRange (inCheckedContext, Value, UInt16.MinValue, UInt16.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < UInt16.MinValue || Value > UInt16.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new UShortConstant ((ushort) Value, Location);
                        }
                        if (target_type == TypeManager.int32_type) {
-                               CheckRange (inCheckedContext, Value, Int32.MinValue, Int32.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < Int32.MinValue || Value > Int32.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new IntConstant ((int) Value, Location);
                        }
                        if (target_type == TypeManager.uint32_type) {
-                               CheckRange (inCheckedContext, Value, UInt32.MinValue, UInt32.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < UInt32.MinValue || Value > UInt32.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new UIntConstant ((uint) Value, Location);
                        }
                        if (target_type == TypeManager.uint64_type) {
-                               CheckUnsigned (inCheckedContext, Value);
+                               if (inCheckedContext && Value < 0)
+                                       throw new OverflowException ();
                                return new ULongConstant ((ulong) Value, Location);
                        }
                        if (target_type == TypeManager.float_type)
@@ -1108,7 +1226,10 @@ namespace Mono.CSharp {
                        if (target_type == TypeManager.double_type)
                                return new DoubleConstant ((double) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckRange (inCheckedContext, Value, Char.MinValue, Char.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < Char.MinValue || Value > Char.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -1117,6 +1238,14 @@ namespace Mono.CSharp {
                        return null;
                }
 
+               public override Constant ConvertImplicitly (Type type)
+               {
+                       if (Value >= 0 && type == TypeManager.uint64_type) {
+                               return new ULongConstant ((ulong) Value, loc);
+                       }
+
+                       return base.ConvertImplicitly (type);
+               }
        }
 
        public class ULongConstant : IntegralConstant {
@@ -1171,31 +1300,38 @@ namespace Mono.CSharp {
                public override Constant ConvertExplicitly (bool inCheckedContext, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (inCheckedContext, Value, Byte.MaxValue);
+                               if (inCheckedContext && Value > Byte.MaxValue)
+                                       throw new OverflowException ();
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (inCheckedContext, Value, (ulong) SByte.MaxValue);
+                               if (inCheckedContext && Value > ((ulong) SByte.MaxValue))
+                                       throw new OverflowException ();
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type) {
-                               CheckRange (inCheckedContext, Value, (ulong) Int16.MaxValue);
+                               if (inCheckedContext && Value > ((ulong) Int16.MaxValue))
+                                       throw new OverflowException ();
                                return new ShortConstant ((short) Value, Location);
                        }
                        if (target_type == TypeManager.ushort_type) {
-                               CheckRange (inCheckedContext, Value, UInt16.MaxValue);
+                               if (inCheckedContext && Value > UInt16.MaxValue)
+                                       throw new OverflowException ();
                                return new UShortConstant ((ushort) Value, Location);
                        }
                        if (target_type == TypeManager.int32_type) {
-                               CheckRange (inCheckedContext, Value, Int32.MaxValue);
+                               if (inCheckedContext && Value > UInt32.MaxValue)
+                                       throw new OverflowException ();
                                return new IntConstant ((int) Value, Location);
                        }
                        if (target_type == TypeManager.uint32_type) {
-                               CheckRange (inCheckedContext, Value, UInt32.MaxValue);
+                               if  (inCheckedContext && Value > UInt32.MaxValue)
+                                       throw new OverflowException ();
                                return new UIntConstant ((uint) Value, Location);
                        }
                        if (target_type == TypeManager.int64_type) {
-                               CheckRange (inCheckedContext, Value, (ulong) Int64.MaxValue);
+                               if (inCheckedContext && Value > Int64.MaxValue)
+                                       throw new OverflowException ();
                                return new LongConstant ((long) Value, Location);
                        }
                        if (target_type == TypeManager.float_type)
@@ -1203,7 +1339,8 @@ namespace Mono.CSharp {
                        if (target_type == TypeManager.double_type)
                                return new DoubleConstant ((double) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckRange (inCheckedContext, Value, Char.MaxValue);
+                               if (inCheckedContext && Value > Char.MaxValue)
+                                       throw new OverflowException ();
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -1260,41 +1397,68 @@ namespace Mono.CSharp {
                public override Constant ConvertExplicitly (bool inCheckedContext, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (inCheckedContext, Value, byte.MinValue, byte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < byte.MinValue || Value > byte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (inCheckedContext, Value,  sbyte.MinValue, sbyte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value <  sbyte.MinValue || Value > sbyte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type) {
-                               CheckRange (inCheckedContext, Value, short.MinValue, short.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < short.MinValue || Value > short.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ShortConstant ((short) Value, Location);
                        }
                        if (target_type == TypeManager.ushort_type) {
-                               CheckRange (inCheckedContext, Value, ushort.MinValue, ushort.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < ushort.MinValue || Value > ushort.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new UShortConstant ((ushort) Value, Location);
                        }
                        if (target_type == TypeManager.int32_type) {
-                               CheckRange (inCheckedContext, Value, int.MinValue, int.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < int.MinValue || Value > int.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new IntConstant ((int) Value, Location);
                        }
                        if (target_type == TypeManager.uint32_type) {
-                               CheckRange (inCheckedContext, Value, uint.MinValue, uint.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < uint.MinValue || Value > uint.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new UIntConstant ((uint) Value, Location);
                        }
                        if (target_type == TypeManager.int64_type) {
-                               CheckRange (inCheckedContext, Value, long.MinValue, long.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < long.MinValue || Value > long.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new LongConstant ((long) Value, Location);
                        }
                        if (target_type == TypeManager.uint64_type) {
-                               CheckRange (inCheckedContext, Value, ulong.MinValue, ulong.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < ulong.MinValue || Value > ulong.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ULongConstant ((ulong) Value, Location);
                        }
                        if (target_type == TypeManager.double_type)
                                return new DoubleConstant ((double) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckRange (inCheckedContext, Value, char.MinValue, char.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < (float) char.MinValue || Value > (float) char.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -1351,41 +1515,68 @@ namespace Mono.CSharp {
                public override Constant ConvertExplicitly (bool inCheckedContext, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (inCheckedContext, Value, Byte.MinValue, Byte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < Byte.MinValue || Value > Byte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < SByte.MinValue || Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type) {
-                               CheckRange (inCheckedContext, Value, short.MinValue, short.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < short.MinValue || Value > short.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ShortConstant ((short) Value, Location);
                        }
                        if (target_type == TypeManager.ushort_type) {
-                               CheckRange (inCheckedContext, Value, ushort.MinValue, ushort.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < ushort.MinValue || Value > ushort.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new UShortConstant ((ushort) Value, Location);
                        }
                        if (target_type == TypeManager.int32_type) {
-                               CheckRange (inCheckedContext, Value, int.MinValue, int.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < int.MinValue || Value > int.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new IntConstant ((int) Value, Location);
                        }
                        if (target_type == TypeManager.uint32_type) {
-                               CheckRange (inCheckedContext, Value, uint.MinValue, uint.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < uint.MinValue || Value > uint.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new UIntConstant ((uint) Value, Location);
                        }
                        if (target_type == TypeManager.int64_type) {
-                               CheckRange (inCheckedContext, Value, long.MinValue, long.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < long.MinValue || Value > long.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new LongConstant ((long) Value, Location);
                        }
                        if (target_type == TypeManager.uint64_type) {
-                               CheckRange (inCheckedContext, Value, ulong.MinValue, ulong.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < ulong.MinValue || Value > ulong.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ULongConstant ((ulong) Value, Location);
                        }
                        if (target_type == TypeManager.float_type)
                                return new FloatConstant ((float) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckRange (inCheckedContext, Value, char.MinValue, char.MaxValue);
+                               if (inCheckedContext){
+                                       if (Value < (double) char.MinValue || Value > (double) char.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)