2006-12-03 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / mcs / constant.cs
index bee3c077a038d7628a34d1f508082a2558471dfd..8ea18f3c74d842f9d4d26334ac75c89826222d9d 100644 (file)
@@ -3,6 +3,7 @@
 //
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
+//   Marek Safar (marek.safar@seznam.cz)
 //
 // (C) 2001 Ximian, Inc.
 //
@@ -17,6 +18,12 @@ namespace Mono.CSharp {
        ///   Base class for constants and literals.
        /// </summary>
        public abstract class Constant : Expression {
+
+               protected Constant (Location loc)
+               {
+                       this.loc = loc;
+               }
+
                /// <remarks>
                ///   This is different from ToString in that ToString
                ///   is supposed to be there for debugging purposes,
@@ -32,6 +39,18 @@ namespace Mono.CSharp {
                        return this.GetType ().Name + " (" + AsString () + ")";
                }
 
+               public override bool GetAttributableValue (Type valueType, out object value)
+               {
+                       Constant c = ImplicitConversionRequired (valueType, loc);
+                       if (c == null) {
+                               value = null;
+                               return false;
+                       }
+
+                       value = c.GetTypedValue ();
+                       return true;
+               }
+
                /// <summary>
                ///  This is used to obtain the actual value of the literal
                ///  cast into an object.
@@ -51,17 +70,14 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               public override void Error_ValueCannotBeConverted (Location loc, Type t)
+               public Constant ImplicitConversionRequired (Type type, Location loc)
                {
-                       // string is not real constant
-                       if (type == TypeManager.string_type)
-                               base.Error_ValueCannotBeConverted (loc, t);
-                       else
-                               Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
-                                       AsString (), TypeManager.CSharpName (t));
+                       Constant c = ToType (type);
+                       if (c == null)
+                               Error_ValueCannotBeConverted (loc, type, false);
+                       return c;
                }
 
-
                //
                // The various ToXXXX conversion functions are used by the constant
                // folding evaluator.   A null value is returned if the conversion is
@@ -78,7 +94,7 @@ namespace Mono.CSharp {
                        DoubleConstant c = ConvertToDouble ();
 
                        if (c == null)
-                               Convert.Error_CannotImplicitConversion (loc, Type, TypeManager.double_type);
+                               Error_ValueCannotBeConverted (loc, TypeManager.double_type, false);
 
                        return c;
                }
@@ -88,7 +104,7 @@ namespace Mono.CSharp {
                        FloatConstant c = ConvertToFloat ();
 
                        if (c == null)
-                               Convert.Error_CannotImplicitConversion (loc, Type, TypeManager.float_type);
+                               Error_ValueCannotBeConverted (loc, TypeManager.float_type, false);
 
                        return c;
                }
@@ -98,7 +114,7 @@ namespace Mono.CSharp {
                        ULongConstant c = ConvertToULong ();
 
                        if (c == null)
-                               Convert.Error_CannotImplicitConversion (loc, Type, TypeManager.uint64_type);
+                               Error_ValueCannotBeConverted (loc, TypeManager.uint64_type, false);
 
                        return c;
                }
@@ -108,7 +124,7 @@ namespace Mono.CSharp {
                        LongConstant c = ConvertToLong ();
 
                        if (c == null)
-                               Convert.Error_CannotImplicitConversion (loc, Type, TypeManager.int64_type);
+                               Error_ValueCannotBeConverted (loc, TypeManager.int64_type, false);
 
                        return c;
                }
@@ -118,7 +134,7 @@ namespace Mono.CSharp {
                        UIntConstant c = ConvertToUInt ();
 
                        if (c == null)
-                               Convert.Error_CannotImplicitConversion (loc, Type, TypeManager.uint32_type);
+                               Error_ValueCannotBeConverted (loc, TypeManager.uint32_type, false);
 
                        return c;
                }
@@ -128,7 +144,7 @@ namespace Mono.CSharp {
                        IntConstant c = ConvertToInt ();
 
                        if (c == null)
-                               Convert.Error_CannotImplicitConversion (loc, Type, TypeManager.int32_type);
+                               Error_ValueCannotBeConverted (loc, TypeManager.int32_type, false);
 
                        return c;
                }
@@ -138,12 +154,12 @@ namespace Mono.CSharp {
                        DecimalConstant c = ConvertToDecimal ();
 
                        if (c == null)
-                               Convert.Error_CannotConvertType (loc, Type, TypeManager.decimal_type);
+                               Error_ValueCannotBeConverted (loc, TypeManager.decimal_type, false);
 
                        return c;
                }
 
-               public virtual Constant ToType (Type type, Location loc)
+               public virtual Constant ToType (Type type)
                {
                        if (Type == type)
                                return this;
@@ -151,8 +167,7 @@ namespace Mono.CSharp {
                        if (type == TypeManager.object_type)
                                return this;
 
-                       if (!Convert.ImplicitStandardConversionExists (Convert.ConstantEC, this, type)){
-                               Error_ValueCannotBeConverted (loc, type);
+                       if (!Convert.ImplicitStandardConversionExists (this, type)){
                                return null;
                        }
 
@@ -165,8 +180,6 @@ namespace Mono.CSharp {
                        bool fail;                      
                        object constant_value = TypeManager.ChangeType (GetValue (), type, out fail);
                        if (fail){
-                               Convert.Error_CannotImplicitConversion (loc, Type, type);
-                               
                                //
                                // We should always catch the error before this is ever
                                // reached, by calling Convert.ImplicitStandardConversionExists
@@ -175,41 +188,134 @@ namespace Mono.CSharp {
                                        String.Format ("LookupConstantValue: This should never be reached {0} {1}", Type, type));
                        }
 
-                       Constant retval;
-                       if (type == TypeManager.int32_type)
-                               retval = new IntConstant ((int) constant_value);
-                       else if (type == TypeManager.uint32_type)
-                               retval = new UIntConstant ((uint) constant_value);
-                       else if (type == TypeManager.int64_type)
-                               retval = new LongConstant ((long) constant_value);
-                       else if (type == TypeManager.uint64_type)
-                               retval = new ULongConstant ((ulong) constant_value);
-                       else if (type == TypeManager.float_type)
-                               retval = new FloatConstant ((float) constant_value);
-                       else if (type == TypeManager.double_type)
-                               retval = new DoubleConstant ((double) constant_value);
-                       else if (type == TypeManager.string_type)
-                               retval = new StringConstant ((string) constant_value);
-                       else if (type == TypeManager.short_type)
-                               retval = new ShortConstant ((short) constant_value);
-                       else if (type == TypeManager.ushort_type)
-                               retval = new UShortConstant ((ushort) constant_value);
-                       else if (type == TypeManager.sbyte_type)
-                               retval = new SByteConstant ((sbyte) constant_value);
-                       else if (type == TypeManager.byte_type)
-                               retval = new ByteConstant ((byte) constant_value);
-                       else if (type == TypeManager.char_type)
-                               retval = new CharConstant ((char) constant_value);
-                       else if (type == TypeManager.bool_type)
-                               retval = new BoolConstant ((bool) constant_value);
-                       else if (type == TypeManager.decimal_type)
-                               retval = new DecimalConstant ((decimal) constant_value);
-                       else
-                               throw new Exception ("LookupConstantValue: Unhandled constant type: " + type);
-                       
-                       return retval;
+                       return CreateConstant (type, constant_value, loc);
+               }
+
+               ///  Returns a constant instance based on Type
+               ///  The returned value is already resolved.
+               public static Constant CreateConstant (Type t, object v, Location loc)
+               {
+                       if (t == TypeManager.int32_type)
+                               return new IntConstant ((int) v, loc);
+                       if (t == TypeManager.string_type)
+                               return new StringConstant ((string) v, loc);
+                       if (t == TypeManager.uint32_type)
+                               return new UIntConstant ((uint) v, loc);
+                       if (t == TypeManager.int64_type)
+                               return new LongConstant ((long) v, loc);
+                       if (t == TypeManager.uint64_type)
+                               return new ULongConstant ((ulong) v, loc);
+                       if (t == TypeManager.float_type)
+                               return new FloatConstant ((float) v, loc);
+                       if (t == TypeManager.double_type)
+                               return new DoubleConstant ((double) v, loc);
+                       if (t == TypeManager.short_type)
+                               return new ShortConstant ((short)v, loc);
+                       if (t == TypeManager.ushort_type)
+                               return new UShortConstant ((ushort)v, loc);
+                       if (t == TypeManager.sbyte_type)
+                               return new SByteConstant ((sbyte)v, loc);
+                       if (t == TypeManager.byte_type)
+                               return new ByteConstant ((byte)v, loc);
+                       if (t == TypeManager.char_type)
+                               return new CharConstant ((char)v, loc);
+                       if (t == TypeManager.bool_type)
+                               return new BoolConstant ((bool) v, loc);
+                       if (t == TypeManager.decimal_type)
+                               return new DecimalConstant ((decimal) v, loc);
+                       if (TypeManager.IsEnumType (t)) {
+                               Type real_type = TypeManager.TypeToCoreType (v.GetType ());
+                               if (real_type == t)
+                                       real_type = System.Enum.GetUnderlyingType (real_type);
+                               return new EnumConstant (CreateConstant (real_type, v, loc), t);
+                       } 
+                       if (v == null && !TypeManager.IsValueType (t))
+                               return new EmptyConstantCast (new NullConstant (loc), t);
+
+                       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 
+               /// </summary>
+               public abstract Constant Reduce (bool inCheckedContext, Type target_type);
+
+               /// <summary>
+               ///   Attempts to do a compile-time folding of a constant cast.
+               /// </summary>
+               public Constant TryReduce (EmitContext ec, Type target_type, Location loc)
+               {
+                       try {
+                               return  TryReduce (ec, target_type);
+                       }
+                       catch (OverflowException) {
+                               Report.Error (221, loc, "Constant value `{0}' cannot be converted to a `{1}' (use `unchecked' syntax to override)",
+                                       GetValue ().ToString (), TypeManager.CSharpName (target_type));
+                               throw;
+                       }
+               }
+
+               Constant TryReduce (EmitContext ec, Type target_type)
+               {
+                       if (Type == target_type)
+                               return this;
+
+                       if (TypeManager.IsEnumType (target_type)) {
+                               Constant c = TryReduce (ec, TypeManager.EnumToUnderlying (target_type));
+                               if (c == null)
+                                       return null;
+
+                               return new EnumConstant (c, target_type);
+                       }
+
+                       return Reduce (ec.ConstantCheckState, target_type);
                }
 
+
                public virtual DecimalConstant ConvertToDecimal ()
                {
                        return null;
@@ -247,6 +353,18 @@ namespace Mono.CSharp {
 
                public abstract Constant Increment ();
                
+               /// <summary>
+               /// Need to pass type as the constant can require a boxing
+               /// and in such case no optimization is possible
+               /// </summary>
+               public bool IsDefaultInitializer (Type type)
+               {
+                       if (type == Type)
+                               return IsDefaultValue;
+
+                       return Type == TypeManager.null_type;
+               }
+
                public abstract bool IsDefaultValue {
                        get;
                }
@@ -263,11 +381,32 @@ namespace Mono.CSharp {
                        get { return false; }
                }
        }
+
+       public abstract class IntegralConstant : Constant {
+               protected IntegralConstant (Location loc) :
+                       base (loc)
+               {
+               }
+
+               public override void Error_ValueCannotBeConverted (Location loc, Type target, bool expl)
+               {
+                       try {
+                               Reduce (true, target);
+                               base.Error_ValueCannotBeConverted (loc, target, expl);
+                       }
+                       catch
+                       {
+                               Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
+                                       GetValue ().ToString (), TypeManager.CSharpName (target));
+                       }
+               }
+       }
        
        public class BoolConstant : Constant {
                public readonly bool Value;
                
-               public BoolConstant (bool val)
+               public BoolConstant (bool val, Location loc):
+                       base (loc)
                {
                        type = TypeManager.bool_type;
                        eclass = ExprClass.Value;
@@ -314,12 +453,19 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == false; }
                }
+
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               {
+                       return null;
+               }
+
        }
 
-       public class ByteConstant : Constant {
+       public class ByteConstant : IntegralConstant {
                public readonly byte Value;
 
-               public ByteConstant (byte v)
+               public ByteConstant (byte v, Location loc):
+                       base (loc)
                {
                        type = TypeManager.byte_type;
                        eclass = ExprClass.Value;
@@ -343,37 +489,37 @@ namespace Mono.CSharp {
 
                public override DoubleConstant ConvertToDouble ()
                {
-                       return new DoubleConstant (Value);
+                       return new DoubleConstant (Value, loc);
                }
 
                public override FloatConstant ConvertToFloat ()
                {
-                       return new FloatConstant (Value);
+                       return new FloatConstant (Value, loc);
                }
 
                public override ULongConstant ConvertToULong ()
                {
-                       return new ULongConstant (Value);
+                       return new ULongConstant (Value, loc);
                }
 
                public override LongConstant ConvertToLong ()
                {
-                       return new LongConstant (Value);
+                       return new LongConstant (Value, loc);
                }
 
                public override UIntConstant ConvertToUInt ()
                {
-                       return new UIntConstant (Value);
+                       return new UIntConstant (Value, loc);
                }
 
                public override IntConstant ConvertToInt ()
                {
-                       return new IntConstant (Value);
+                       return new IntConstant (Value, loc);
                }
 
                public override Constant Increment ()
                {
-                       return new ByteConstant (checked ((byte)(Value + 1)));
+                       return new ByteConstant (checked ((byte)(Value + 1)), loc);
                }
 
                public override bool IsDefaultValue {
@@ -391,12 +537,44 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
+
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               {
+                       if (target_type == TypeManager.sbyte_type) {
+                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               return new SByteConstant ((sbyte) Value, Location);
+                       }
+                       if (target_type == TypeManager.short_type)
+                               return new ShortConstant ((short) Value, Location);
+                       if (target_type == TypeManager.ushort_type)
+                               return new UShortConstant ((ushort) Value, Location);
+                       if (target_type == TypeManager.int32_type)
+                               return new IntConstant ((int) Value, Location);
+                       if (target_type == TypeManager.uint32_type)
+                               return new UIntConstant ((uint) Value, Location);
+                       if (target_type == TypeManager.int64_type)
+                               return new LongConstant ((long) Value, Location);
+                       if (target_type == TypeManager.uint64_type)
+                               return new ULongConstant ((ulong) Value, Location);
+                       if (target_type == TypeManager.float_type)
+                               return new FloatConstant ((float) Value, Location);
+                       if (target_type == TypeManager.double_type)
+                               return new DoubleConstant ((double) Value, Location);
+                       if (target_type == TypeManager.char_type)
+                               return new CharConstant ((char) Value, Location);
+                       if (target_type == TypeManager.decimal_type)
+                               return new DecimalConstant ((decimal) Value, Location);
+
+                       return null;
+               }
+
        }
 
        public class CharConstant : Constant {
                public readonly char Value;
 
-               public CharConstant (char v)
+               public CharConstant (char v, Location loc):
+                       base (loc)
                {
                        type = TypeManager.char_type;
                        eclass = ExprClass.Value;
@@ -449,37 +627,37 @@ namespace Mono.CSharp {
 
                public override DoubleConstant ConvertToDouble ()
                {
-                       return new DoubleConstant (Value);
+                       return new DoubleConstant (Value, loc);
                }
 
                public override FloatConstant ConvertToFloat ()
                {
-                       return new FloatConstant (Value);
+                       return new FloatConstant (Value, loc);
                }
 
                public override ULongConstant ConvertToULong ()
                {
-                       return new ULongConstant (Value);
+                       return new ULongConstant (Value, loc);
                }
 
                public override LongConstant ConvertToLong ()
                {
-                       return new LongConstant (Value);
+                       return new LongConstant (Value, loc);
                }
 
                public override UIntConstant ConvertToUInt ()
                {
-                       return new UIntConstant (Value);
+                       return new UIntConstant (Value, loc);
                }
 
                public override IntConstant ConvertToInt ()
                {
-                       return new IntConstant (Value);
+                       return new IntConstant (Value, loc);
                }
 
                public override Constant Increment ()
                {
-                       return new CharConstant (checked ((char)(Value + 1)));
+                       return new CharConstant (checked ((char)(Value + 1)), loc);
                }
                
                public override bool IsDefaultValue {
@@ -497,12 +675,46 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == '\0'; }
                }
+
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_type) {
+                               CheckRange (inCheckedContext, Value, Byte.MinValue, Byte.MaxValue);
+                               return new ByteConstant ((byte) Value, Location);
+                       }
+                       if (target_type == TypeManager.sbyte_type) {
+                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               return new SByteConstant ((sbyte) Value, Location);
+                       }
+                       if (target_type == TypeManager.short_type) {
+                               CheckRange (inCheckedContext, Value, Int16.MinValue, Int16.MaxValue);
+                               return new ShortConstant ((short) Value, Location);
+                       }
+                       if (target_type == TypeManager.int32_type)
+                               return new IntConstant ((int) Value, Location);
+                       if (target_type == TypeManager.uint32_type)
+                               return new UIntConstant ((uint) Value, Location);
+                       if (target_type == TypeManager.int64_type)
+                               return new LongConstant ((long) Value, Location);
+                       if (target_type == TypeManager.uint64_type)
+                               return new ULongConstant ((ulong) Value, Location);
+                       if (target_type == TypeManager.float_type)
+                               return new FloatConstant ((float) Value, Location);
+                       if (target_type == TypeManager.double_type)
+                               return new DoubleConstant ((double) Value, Location);
+                       if (target_type == TypeManager.decimal_type)
+                               return new DecimalConstant ((decimal) Value, Location);
+
+                       return null;
+               }
+
        }
 
-       public class SByteConstant : Constant {
+       public class SByteConstant : IntegralConstant {
                public readonly sbyte Value;
 
-               public SByteConstant (sbyte v)
+               public SByteConstant (sbyte v, Location loc):
+                       base (loc)
                {
                        type = TypeManager.sbyte_type;
                        eclass = ExprClass.Value;
@@ -526,25 +738,25 @@ namespace Mono.CSharp {
 
                public override DoubleConstant ConvertToDouble ()
                {
-                       return new DoubleConstant (Value);
+                       return new DoubleConstant (Value, loc);
                }
 
                public override FloatConstant ConvertToFloat ()
                {
-                       return new FloatConstant (Value);
+                       return new FloatConstant (Value, loc);
                }
 
                public override ULongConstant ConvertToULong ()
                {
                        if (Value >= 0)
-                               return new ULongConstant ((ulong) Value);
+                               return new ULongConstant ((ulong) Value, loc);
                        
                        return null;
                }
 
                public override LongConstant ConvertToLong ()
                {
-                       return new LongConstant (Value);
+                       return new LongConstant (Value, loc);
                }
 
                public override UIntConstant ConvertToUInt ()
@@ -554,12 +766,12 @@ namespace Mono.CSharp {
 
                public override IntConstant ConvertToInt ()
                {
-                       return new IntConstant (Value);
+                       return new IntConstant (Value, loc);
                }
 
                public override Constant Increment ()
                {
-                   return new SByteConstant (checked((sbyte)(Value + 1)));
+                   return new SByteConstant (checked((sbyte)(Value + 1)), loc);
                }
 
                public override bool IsDefaultValue {
@@ -577,12 +789,50 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
+
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_type) {
+                               CheckUnsigned (inCheckedContext, Value);
+                               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);
+                               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);
+                               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);
+                               return new ULongConstant ((ulong) Value, Location);
+                       }
+                       if (target_type == TypeManager.float_type)
+                               return new FloatConstant ((float) Value, Location);
+                       if (target_type == TypeManager.double_type)
+                               return new DoubleConstant ((double) Value, Location);
+                       if (target_type == TypeManager.char_type) {
+                               CheckUnsigned (inCheckedContext, Value);
+                               return new CharConstant ((char) Value, Location);
+                       }
+                       if (target_type == TypeManager.decimal_type)
+                               return new DecimalConstant ((decimal) Value, Location);
+
+                       return null;
+               }
+
        }
 
-       public class ShortConstant : Constant {
+       public class ShortConstant : IntegralConstant {
                public readonly short Value;
 
-               public ShortConstant (short v)
+               public ShortConstant (short v, Location loc):
+                       base (loc)
                {
                        type = TypeManager.short_type;
                        eclass = ExprClass.Value;
@@ -606,12 +856,12 @@ namespace Mono.CSharp {
 
                public override DoubleConstant ConvertToDouble ()
                {
-                       return new DoubleConstant (Value);
+                       return new DoubleConstant (Value, loc);
                }
 
                public override FloatConstant ConvertToFloat ()
                {
-                       return new FloatConstant (Value);
+                       return new FloatConstant (Value, loc);
                }
 
                public override ULongConstant ConvertToULong ()
@@ -621,7 +871,7 @@ namespace Mono.CSharp {
 
                public override LongConstant ConvertToLong ()
                {
-                       return new LongConstant (Value);
+                       return new LongConstant (Value, loc);
                }
 
                public override UIntConstant ConvertToUInt ()
@@ -631,12 +881,12 @@ namespace Mono.CSharp {
 
                public override IntConstant ConvertToInt ()
                {
-                       return new IntConstant (Value);
+                       return new IntConstant (Value, loc);
                }
 
                public override Constant Increment ()
                {
-                       return new ShortConstant (checked((short)(Value + 1)));
+                       return new ShortConstant (checked((short)(Value + 1)), loc);
                }
 
                public override bool IsDefaultValue {
@@ -654,12 +904,54 @@ namespace Mono.CSharp {
                                return Value < 0;
                        }
                }
+
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_type) {
+                               CheckRange (inCheckedContext, Value, Byte.MinValue, Byte.MaxValue);
+                               return new ByteConstant ((byte) Value, Location);
+                       }
+                       if (target_type == TypeManager.sbyte_type) {
+                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               return new SByteConstant ((sbyte) Value, Location);
+                       }
+                       if (target_type == TypeManager.ushort_type) {
+                               CheckUnsigned (inCheckedContext, Value);
+                               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);
+                               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);
+                               return new ULongConstant ((ulong) Value, Location);
+                       }
+                       if (target_type == TypeManager.float_type)
+                               return new FloatConstant ((float) 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);
+                               return new CharConstant ((char) Value, Location);
+                       }
+                       if (target_type == TypeManager.decimal_type)
+                               return new DecimalConstant ((decimal) Value, Location);
+
+                       return null;
+               }
+
        }
 
-       public class UShortConstant : Constant {
+       public class UShortConstant : IntegralConstant {
                public readonly ushort Value;
 
-               public UShortConstant (ushort v)
+               public UShortConstant (ushort v, Location loc):
+                       base (loc)
                {
                        type = TypeManager.ushort_type;
                        eclass = ExprClass.Value;
@@ -683,37 +975,37 @@ namespace Mono.CSharp {
 
                public override DoubleConstant ConvertToDouble ()
                {
-                       return new DoubleConstant (Value);
+                       return new DoubleConstant (Value, loc);
                }
 
                public override FloatConstant ConvertToFloat ()
                {
-                       return new FloatConstant (Value);
+                       return new FloatConstant (Value, loc);
                }
 
                public override ULongConstant ConvertToULong ()
                {
-                       return new ULongConstant (Value);
+                       return new ULongConstant (Value, loc);
                }
 
                public override LongConstant ConvertToLong ()
                {
-                       return new LongConstant (Value);
+                       return new LongConstant (Value, loc);
                }
 
                public override UIntConstant ConvertToUInt ()
                {
-                       return new UIntConstant (Value);
+                       return new UIntConstant (Value, loc);
                }
 
                public override IntConstant ConvertToInt ()
                {
-                       return new IntConstant (Value);
+                       return new IntConstant (Value, loc);
                }
        
                public override Constant Increment ()
                {
-                       return new UShortConstant (checked((ushort)(Value + 1)));
+                       return new UShortConstant (checked((ushort)(Value + 1)), loc);
                }
 
                public override bool IsDefaultValue {
@@ -731,12 +1023,49 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
+
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_type) {
+                               CheckRange (inCheckedContext, Value, Byte.MinValue, Byte.MaxValue);
+                               return new ByteConstant ((byte) Value, Location);
+                       }
+                       if (target_type == TypeManager.sbyte_type) {
+                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               return new SByteConstant ((sbyte) Value, Location);
+                       }
+                       if (target_type == TypeManager.short_type) {
+                               CheckRange (inCheckedContext, Value, Int16.MinValue, Int16.MaxValue);
+                               return new ShortConstant ((short) Value, Location);
+                       }
+                       if (target_type == TypeManager.int32_type)
+                               return new IntConstant ((int) Value, Location);
+                       if (target_type == TypeManager.uint32_type)
+                               return new UIntConstant ((uint) Value, Location);
+                       if (target_type == TypeManager.int64_type)
+                               return new LongConstant ((long) Value, Location);
+                       if (target_type == TypeManager.uint64_type)
+                               return new ULongConstant ((ulong) Value, Location);
+                       if (target_type == TypeManager.float_type)
+                               return new FloatConstant ((float) 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);
+                               return new CharConstant ((char) Value, Location);
+                       }
+                       if (target_type == TypeManager.decimal_type)
+                               return new DecimalConstant ((decimal) Value, Location);
+
+                       return null;
+               }
        }
 
-       public class IntConstant : Constant {
+       public class IntConstant : IntegralConstant {
                public readonly int Value;
 
-               public IntConstant (int v)
+               public IntConstant (int v, Location loc):
+                       base (loc)
                {
                        type = TypeManager.int32_type;
                        eclass = ExprClass.Value;
@@ -812,17 +1141,17 @@ namespace Mono.CSharp {
 
                public override DecimalConstant ConvertToDecimal()
                {
-                       return new DecimalConstant (Value);
+                       return new DecimalConstant (Value, loc);
                }
 
                public override DoubleConstant ConvertToDouble ()
                {
-                       return new DoubleConstant (Value);
+                       return new DoubleConstant (Value, loc);
                }
 
                public override FloatConstant ConvertToFloat ()
                {
-                       return new FloatConstant (Value);
+                       return new FloatConstant (Value, loc);
                }
 
                public override ULongConstant ConvertToULong ()
@@ -830,12 +1159,12 @@ namespace Mono.CSharp {
                        if (Value < 0)
                                return null;
 
-                       return new ULongConstant ((ulong) Value);
+                       return new ULongConstant ((ulong) Value, loc);
                }
 
                public override LongConstant ConvertToLong ()
                {
-                       return new LongConstant (Value);
+                       return new LongConstant (Value, loc);
                }
 
                public override UIntConstant ConvertToUInt ()
@@ -843,7 +1172,7 @@ namespace Mono.CSharp {
                        if (Value < 0)
                                return null;
 
-                       return new UIntConstant ((uint) Value);
+                       return new UIntConstant ((uint) Value, loc);
                }
 
                public override IntConstant ConvertToInt ()
@@ -853,7 +1182,7 @@ namespace Mono.CSharp {
 
                public override Constant Increment ()
                {
-                       return new IntConstant (checked(Value + 1));
+                       return new IntConstant (checked(Value + 1), loc);
                }
 
                public override bool IsDefaultValue {
@@ -871,12 +1200,55 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
+
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_type) {
+                               CheckRange (inCheckedContext, Value, Byte.MinValue, Byte.MaxValue);
+                               return new ByteConstant ((byte) Value, Location);
+                       }
+                       if (target_type == TypeManager.sbyte_type) {
+                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               return new SByteConstant ((sbyte) Value, Location);
+                       }
+                       if (target_type == TypeManager.short_type) {
+                               CheckRange (inCheckedContext, Value, Int16.MinValue, Int16.MaxValue);
+                               return new ShortConstant ((short) Value, Location);
+                       }
+                       if (target_type == TypeManager.ushort_type) {
+                               CheckRange (inCheckedContext, Value, UInt16.MinValue, UInt16.MaxValue);
+                               return new UShortConstant ((ushort) Value, Location);
+                       }
+                       if (target_type == TypeManager.uint32_type) {
+                               CheckRange (inCheckedContext, Value, UInt32.MinValue, UInt32.MaxValue);
+                               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);
+                               return new ULongConstant ((ulong) Value, Location);
+                       }
+                       if (target_type == TypeManager.float_type)
+                               return new FloatConstant ((float) 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);
+                               return new CharConstant ((char) Value, Location);
+                       }
+                       if (target_type == TypeManager.decimal_type)
+                               return new DecimalConstant ((decimal) Value, Location);
+
+                       return null;
+               }
        }
 
-       public class UIntConstant : Constant {
+       public class UIntConstant : IntegralConstant {
                public readonly uint Value;
 
-               public UIntConstant (uint v)
+               public UIntConstant (uint v, Location loc):
+                       base (loc)
                {
                        type = TypeManager.uint32_type;
                        eclass = ExprClass.Value;
@@ -900,22 +1272,22 @@ namespace Mono.CSharp {
 
                public override DoubleConstant ConvertToDouble ()
                {
-                       return new DoubleConstant (Value);
+                       return new DoubleConstant (Value, loc);
                }
 
                public override FloatConstant ConvertToFloat ()
                {
-                       return new FloatConstant (Value);
+                       return new FloatConstant (Value, loc);
                }
 
                public override ULongConstant ConvertToULong ()
                {
-                       return new ULongConstant (Value);
+                       return new ULongConstant (Value, loc);
                }
 
                public override LongConstant ConvertToLong ()
                {
-                       return new LongConstant (Value);
+                       return new LongConstant (Value, loc);
                }
 
                public override UIntConstant ConvertToUInt ()
@@ -930,7 +1302,7 @@ namespace Mono.CSharp {
        
                public override Constant Increment ()
                {
-                       return new UIntConstant (checked(Value + 1));
+                       return new UIntConstant (checked(Value + 1), loc);
                }
        
                public override bool IsDefaultValue {
@@ -948,12 +1320,54 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
+
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_type) {
+                               CheckRange (inCheckedContext, Value, Char.MinValue, Char.MaxValue);
+                               return new ByteConstant ((byte) Value, Location);
+                       }
+                       if (target_type == TypeManager.sbyte_type) {
+                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               return new SByteConstant ((sbyte) Value, Location);
+                       }
+                       if (target_type == TypeManager.short_type) {
+                               CheckRange (inCheckedContext, Value, Int16.MinValue, Int16.MaxValue);
+                               return new ShortConstant ((short) Value, Location);
+                       }
+                       if (target_type == TypeManager.ushort_type) {
+                               CheckRange (inCheckedContext, Value, UInt16.MinValue, UInt16.MaxValue);
+                               return new UShortConstant ((ushort) Value, Location);
+                       }
+                       if (target_type == TypeManager.int32_type) {
+                               CheckRange (inCheckedContext, Value, Int32.MinValue, Int32.MaxValue);
+                               return new IntConstant ((int) Value, Location);
+                       }
+                       if (target_type == TypeManager.int64_type)
+                               return new LongConstant ((long) Value, Location);
+                       if (target_type == TypeManager.uint64_type)
+                               return new ULongConstant ((ulong) Value, Location);
+                       if (target_type == TypeManager.float_type)
+                               return new FloatConstant ((float) 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);
+                               return new CharConstant ((char) Value, Location);
+                       }
+                       if (target_type == TypeManager.decimal_type)
+                               return new DecimalConstant ((decimal) Value, Location);
+
+                       return null;
+               }
+
        }
 
-       public class LongConstant : Constant {
+       public class LongConstant : IntegralConstant {
                public readonly long Value;
 
-               public LongConstant (long v)
+               public LongConstant (long v, Location loc):
+                       base (loc)
                {
                        type = TypeManager.int64_type;
                        eclass = ExprClass.Value;
@@ -962,19 +1376,17 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-
-                       EmitLong (ig, Value);
+                       EmitLong (ec.ig, Value);
                }
 
                static public void EmitLong (ILGenerator ig, long l)
                {
-                       if ((l >> 32) == 0){
+                       if (l >= int.MinValue && l <= int.MaxValue) {
                                IntLiteral.EmitInt (ig, unchecked ((int) l));
-                               ig.Emit (OpCodes.Conv_U8);
-                       } else {
-                               ig.Emit (OpCodes.Ldc_I8, l);
+                               ig.Emit (OpCodes.Conv_I8);
+                               return;
                        }
+                       ig.Emit (OpCodes.Ldc_I8, l);
                }
 
                public override string AsString ()
@@ -989,12 +1401,12 @@ namespace Mono.CSharp {
 
                public override DoubleConstant ConvertToDouble ()
                {
-                       return new DoubleConstant (Value);
+                       return new DoubleConstant (Value, loc);
                }
 
                public override FloatConstant ConvertToFloat ()
                {
-                       return new FloatConstant (Value);
+                       return new FloatConstant (Value, loc);
                }
 
                public override ULongConstant ConvertToULong ()
@@ -1002,7 +1414,7 @@ namespace Mono.CSharp {
                        if (Value < 0)
                                return null;
                        
-                       return new ULongConstant ((ulong) Value);
+                       return new ULongConstant ((ulong) Value, loc);
                }
 
                public override LongConstant ConvertToLong ()
@@ -1022,7 +1434,7 @@ namespace Mono.CSharp {
 
                public override Constant Increment ()
                {
-                       return new LongConstant (checked(Value + 1));
+                       return new LongConstant (checked(Value + 1), loc);
                }
                
                public override bool IsDefaultValue {
@@ -1040,12 +1452,58 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
+
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_type) {
+                               CheckRange (inCheckedContext, Value, Byte.MinValue, Byte.MaxValue);
+                               return new ByteConstant ((byte) Value, Location);
+                       }
+                       if (target_type == TypeManager.sbyte_type) {
+                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               return new SByteConstant ((sbyte) Value, Location);
+                       }
+                       if (target_type == TypeManager.short_type) {
+                               CheckRange (inCheckedContext, Value, Int16.MinValue, Int16.MaxValue);
+                               return new ShortConstant ((short) Value, Location);
+                       }
+                       if (target_type == TypeManager.ushort_type) {
+                               CheckRange (inCheckedContext, Value, UInt16.MinValue, UInt16.MaxValue);
+                               return new UShortConstant ((ushort) Value, Location);
+                       }
+                       if (target_type == TypeManager.int32_type) {
+                               CheckRange (inCheckedContext, Value, Int32.MinValue, Int32.MaxValue);
+                               return new IntConstant ((int) Value, Location);
+                       }
+                       if (target_type == TypeManager.uint32_type) {
+                               CheckRange (inCheckedContext, Value, UInt32.MinValue, UInt32.MaxValue);
+                               return new UIntConstant ((uint) Value, Location);
+                       }
+                       if (target_type == TypeManager.uint64_type) {
+                               CheckUnsigned (inCheckedContext, Value);
+                               return new ULongConstant ((ulong) Value, Location);
+                       }
+                       if (target_type == TypeManager.float_type)
+                               return new FloatConstant ((float) 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);
+                               return new CharConstant ((char) Value, Location);
+                       }
+                       if (target_type == TypeManager.decimal_type)
+                               return new DecimalConstant ((decimal) Value, Location);
+
+                       return null;
+               }
+
        }
 
-       public class ULongConstant : Constant {
+       public class ULongConstant : IntegralConstant {
                public readonly ulong Value;
 
-               public ULongConstant (ulong v)
+               public ULongConstant (ulong v, Location loc):
+                       base (loc)
                {
                        type = TypeManager.uint64_type;
                        eclass = ExprClass.Value;
@@ -1071,12 +1529,12 @@ namespace Mono.CSharp {
 
                public override DoubleConstant ConvertToDouble ()
                {
-                       return new DoubleConstant (Value);
+                       return new DoubleConstant (Value, loc);
                }
 
                public override FloatConstant ConvertToFloat ()
                {
-                       return new FloatConstant (Value);
+                       return new FloatConstant (Value, loc);
                }
 
                public override ULongConstant ConvertToULong ()
@@ -1101,7 +1559,7 @@ namespace Mono.CSharp {
 
                public override Constant Increment ()
                {
-                       return new ULongConstant (checked(Value + 1));
+                       return new ULongConstant (checked(Value + 1), loc);
                }
 
                public override bool IsDefaultValue {
@@ -1119,12 +1577,58 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
+
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_type) {
+                               CheckRange (inCheckedContext, Value, Byte.MaxValue);
+                               return new ByteConstant ((byte) Value, Location);
+                       }
+                       if (target_type == TypeManager.sbyte_type) {
+                               CheckRange (inCheckedContext, Value, (ulong) SByte.MaxValue);
+                               return new SByteConstant ((sbyte) Value, Location);
+                       }
+                       if (target_type == TypeManager.short_type) {
+                               CheckRange (inCheckedContext, Value, (ulong) Int16.MaxValue);
+                               return new ShortConstant ((short) Value, Location);
+                       }
+                       if (target_type == TypeManager.ushort_type) {
+                               CheckRange (inCheckedContext, Value, UInt16.MaxValue);
+                               return new UShortConstant ((ushort) Value, Location);
+                       }
+                       if (target_type == TypeManager.int32_type) {
+                               CheckRange (inCheckedContext, Value, Int32.MaxValue);
+                               return new IntConstant ((int) Value, Location);
+                       }
+                       if (target_type == TypeManager.uint32_type) {
+                               CheckRange (inCheckedContext, Value, UInt32.MaxValue);
+                               return new UIntConstant ((uint) Value, Location);
+                       }
+                       if (target_type == TypeManager.int64_type) {
+                               CheckRange (inCheckedContext, Value, (ulong) Int64.MaxValue);
+                               return new LongConstant ((long) Value, Location);
+                       }
+                       if (target_type == TypeManager.float_type)
+                               return new FloatConstant ((float) Value, Location);
+                       if (target_type == TypeManager.double_type)
+                               return new DoubleConstant ((double) Value, Location);
+                       if (target_type == TypeManager.char_type) {
+                               CheckRange (inCheckedContext, Value, Char.MaxValue);
+                               return new CharConstant ((char) Value, Location);
+                       }
+                       if (target_type == TypeManager.decimal_type)
+                               return new DecimalConstant ((decimal) Value, Location);
+
+                       return null;
+               }
+
        }
 
        public class FloatConstant : Constant {
-               public readonly float Value;
+               public float Value;
 
-               public FloatConstant (float v)
+               public FloatConstant (float v, Location loc):
+                       base (loc)
                {
                        type = TypeManager.float_type;
                        eclass = ExprClass.Value;
@@ -1148,7 +1652,7 @@ namespace Mono.CSharp {
 
                public override DoubleConstant ConvertToDouble ()
                {
-                       return new DoubleConstant (Value);
+                       return new DoubleConstant (Value, loc);
                }
 
                public override FloatConstant ConvertToFloat ()
@@ -1173,7 +1677,7 @@ namespace Mono.CSharp {
 
                public override Constant Increment ()
                {
-                       return new FloatConstant (checked(Value + 1));
+                       return new FloatConstant (checked(Value + 1), loc);
                }
 
                public override bool IsDefaultValue {
@@ -1187,12 +1691,60 @@ namespace Mono.CSharp {
                                return Value < 0;
                        }
                }
+
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_type) {
+                               CheckRange (inCheckedContext, Value, byte.MinValue, byte.MaxValue);
+                               return new ByteConstant ((byte) Value, Location);
+                       }
+                       if (target_type == TypeManager.sbyte_type) {
+                               CheckRange (inCheckedContext, Value,  sbyte.MinValue, sbyte.MaxValue);
+                               return new SByteConstant ((sbyte) Value, Location);
+                       }
+                       if (target_type == TypeManager.short_type) {
+                               CheckRange (inCheckedContext, Value, short.MinValue, short.MaxValue);
+                               return new ShortConstant ((short) Value, Location);
+                       }
+                       if (target_type == TypeManager.ushort_type) {
+                               CheckRange (inCheckedContext, Value, ushort.MinValue, ushort.MaxValue);
+                               return new UShortConstant ((ushort) Value, Location);
+                       }
+                       if (target_type == TypeManager.int32_type) {
+                               CheckRange (inCheckedContext, Value, int.MinValue, int.MaxValue);
+                               return new IntConstant ((int) Value, Location);
+                       }
+                       if (target_type == TypeManager.uint32_type) {
+                               CheckRange (inCheckedContext, Value, uint.MinValue, uint.MaxValue);
+                               return new UIntConstant ((uint) Value, Location);
+                       }
+                       if (target_type == TypeManager.int64_type) {
+                               CheckRange (inCheckedContext, Value, long.MinValue, long.MaxValue);
+                               return new LongConstant ((long) Value, Location);
+                       }
+                       if (target_type == TypeManager.uint64_type) {
+                               CheckRange (inCheckedContext, Value, ulong.MinValue, ulong.MaxValue);
+                               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);
+                               return new CharConstant ((char) Value, Location);
+                       }
+                       if (target_type == TypeManager.decimal_type)
+                               return new DecimalConstant ((decimal) Value, Location);
+
+                       return null;
+               }
+
        }
 
        public class DoubleConstant : Constant {
-               public readonly double Value;
+               public double Value;
 
-               public DoubleConstant (double v)
+               public DoubleConstant (double v, Location loc):
+                       base (loc)
                {
                        type = TypeManager.double_type;
                        eclass = ExprClass.Value;
@@ -1246,7 +1798,7 @@ namespace Mono.CSharp {
 
                public override Constant Increment ()
                {
-                       return new DoubleConstant (checked(Value + 1));
+                       return new DoubleConstant (checked(Value + 1), loc);
                }
 
                public override bool IsDefaultValue {
@@ -1260,12 +1812,60 @@ namespace Mono.CSharp {
                                return Value < 0;
                        }
                }
+
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_type) {
+                               CheckRange (inCheckedContext, Value, Byte.MinValue, Byte.MaxValue);
+                               return new ByteConstant ((byte) Value, Location);
+                       }
+                       if (target_type == TypeManager.sbyte_type) {
+                               CheckRange (inCheckedContext, Value, SByte.MinValue, SByte.MaxValue);
+                               return new SByteConstant ((sbyte) Value, Location);
+                       }
+                       if (target_type == TypeManager.short_type) {
+                               CheckRange (inCheckedContext, Value, short.MinValue, short.MaxValue);
+                               return new ShortConstant ((short) Value, Location);
+                       }
+                       if (target_type == TypeManager.ushort_type) {
+                               CheckRange (inCheckedContext, Value, ushort.MinValue, ushort.MaxValue);
+                               return new UShortConstant ((ushort) Value, Location);
+                       }
+                       if (target_type == TypeManager.int32_type) {
+                               CheckRange (inCheckedContext, Value, int.MinValue, int.MaxValue);
+                               return new IntConstant ((int) Value, Location);
+                       }
+                       if (target_type == TypeManager.uint32_type) {
+                               CheckRange (inCheckedContext, Value, uint.MinValue, uint.MaxValue);
+                               return new UIntConstant ((uint) Value, Location);
+                       }
+                       if (target_type == TypeManager.int64_type) {
+                               CheckRange (inCheckedContext, Value, long.MinValue, long.MaxValue);
+                               return new LongConstant ((long) Value, Location);
+                       }
+                       if (target_type == TypeManager.uint64_type) {
+                               CheckRange (inCheckedContext, Value, ulong.MinValue, ulong.MaxValue);
+                               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);
+                               return new CharConstant ((char) Value, Location);
+                       }
+                       if (target_type == TypeManager.decimal_type)
+                               return new DecimalConstant ((decimal) Value, Location);
+
+                       return null;
+               }
+
        }
 
        public class DecimalConstant : Constant {
                public readonly decimal Value;
 
-               public DecimalConstant (decimal d)
+               public DecimalConstant (decimal d, Location loc):
+                       base (loc)
                {
                        type = TypeManager.decimal_type;
                        eclass = ExprClass.Value;
@@ -1317,7 +1917,7 @@ namespace Mono.CSharp {
 
                public override Constant Increment ()
                {
-                       return new DecimalConstant (checked (Value + 1));
+                       return new DecimalConstant (checked (Value + 1), loc);
                }
 
                public override bool IsDefaultValue {
@@ -1331,12 +1931,42 @@ namespace Mono.CSharp {
                                return Value < 0;
                        }
                }
+
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               {
+                       if (target_type == TypeManager.sbyte_type)
+                               return new SByteConstant ((sbyte)Value, loc);
+                       if (target_type == TypeManager.byte_type)
+                               return new ByteConstant ((byte)Value, loc);
+                       if (target_type == TypeManager.short_type)
+                               return new ShortConstant ((short)Value, loc);
+                       if (target_type == TypeManager.ushort_type)
+                               return new UShortConstant ((ushort)Value, loc);
+                       if (target_type == TypeManager.int32_type)
+                               return new IntConstant ((int)Value, loc);
+                       if (target_type == TypeManager.uint32_type)
+                               return new UIntConstant ((uint)Value, loc);
+                       if (target_type == TypeManager.int64_type)
+                               return new LongConstant ((long)Value, loc);
+                       if (target_type == TypeManager.uint64_type)
+                               return new ULongConstant ((ulong)Value, loc);
+                       if (target_type == TypeManager.char_type)
+                               return new CharConstant ((char)Value, loc);
+                       if (target_type == TypeManager.float_type)
+                               return new FloatConstant ((float)Value, loc);
+                       if (target_type == TypeManager.double_type)
+                               return new DoubleConstant ((double)Value, loc);
+
+                       return null;
+               }
+
        }
 
        public class StringConstant : Constant {
                public readonly string Value;
 
-               public StringConstant (string s)
+               public StringConstant (string s, Location loc):
+                       base (loc)
                {
                        type = TypeManager.string_type;
                        eclass = ExprClass.Value;
@@ -1378,6 +2008,11 @@ namespace Mono.CSharp {
                                return false;
                        }
                }
+
+               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               {
+                       return null;
+               }
        }
 
 }