2009-03-23 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / constant.cs
index 76c2c5667a8574a363f163ea62e0f84844823968..f141c8211105fc0be72f92192d4cf406b97669a7 100644 (file)
@@ -5,14 +5,15 @@
 //   Miguel de Icaza (miguel@ximian.com)
 //   Marek Safar (marek.safar@seznam.cz)
 //
-// (C) 2001 Ximian, Inc.
-//
+// Copyright 2001-2003 Ximian, Inc.
+// Copyright 2003-2008 Novell, Inc.
 //
 
 namespace Mono.CSharp {
 
        using System;
        using System.Reflection.Emit;
+       using System.Collections;
 
        /// <summary>
        ///   Base class for constants and literals.
@@ -39,6 +40,23 @@ namespace Mono.CSharp {
                        return this.GetType ().Name + " (" + AsString () + ")";
                }
 
+               public override bool GetAttributableValue (EmitContext ec, Type value_type, out object value)
+               {
+                       if (value_type == TypeManager.object_type) {
+                               value = GetTypedValue ();
+                               return true;
+                       }
+
+                       Constant c = ImplicitConversionRequired (ec, value_type, 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.
@@ -58,182 +76,108 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               //
-               // The various ToXXXX conversion functions are used by the constant
-               // folding evaluator.   A null value is returned if the conversion is
-               // not possible.   
-               //
-               // Note: not all the patterns for catching `implicit_conv' are the same.
-               // some implicit conversions can never be performed between two types
-               // even if the conversion would be lossless (for example short to uint),
-               // but some conversions are explicitly permitted by the standard provided
-               // that there will be no loss of information (for example, int to uint).
-               //
-               public DoubleConstant ToDouble (Location loc)
-               {
-                       DoubleConstant c = ConvertToDouble ();
-
-                       if (c == null)
-                               Error_ValueCannotBeConverted (loc, TypeManager.double_type, false);
-
-                       return c;
-               }
-
-               public FloatConstant ToFloat (Location loc)
-               {
-                       FloatConstant c = ConvertToFloat ();
-
-                       if (c == null)
-                               Error_ValueCannotBeConverted (loc, TypeManager.float_type, false);
-
-                       return c;
-               }
-
-               public ULongConstant ToULong (Location loc)
-               {
-                       ULongConstant c = ConvertToULong ();
-
-                       if (c == null)
-                               Error_ValueCannotBeConverted (loc, TypeManager.uint64_type, false);
-
-                       return c;
-               }
-
-               public LongConstant ToLong (Location loc)
-               {
-                       LongConstant c = ConvertToLong ();
-
-                       if (c == null)
-                               Error_ValueCannotBeConverted (loc, TypeManager.int64_type, false);
-
-                       return c;
-               }
-               
-               public UIntConstant ToUInt (Location loc)
-               {
-                       UIntConstant c = ConvertToUInt ();
-
-                       if (c == null)
-                               Error_ValueCannotBeConverted (loc, TypeManager.uint32_type, false);
-
-                       return c;
-               }
-
-               public IntConstant ToInt (Location loc)
+               public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
                {
-                       IntConstant c = ConvertToInt ();
-
-                       if (c == null)
-                               Error_ValueCannotBeConverted (loc, TypeManager.int32_type, false);
-
-                       return c;
+                       if (!expl && IsLiteral && 
+                               (TypeManager.IsPrimitiveType (target) || type == TypeManager.decimal_type) &&
+                               (TypeManager.IsPrimitiveType (type) || type == TypeManager.decimal_type)) {
+                               Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
+                                       AsString (), TypeManager.CSharpName (target));
+                       } else {
+                               base.Error_ValueCannotBeConverted (ec, loc, target, expl);
+                       }
                }
 
-               public DecimalConstant ToDecimal (Location loc)
+               public Constant ImplicitConversionRequired (EmitContext ec, Type type, Location loc)
                {
-                       DecimalConstant c = ConvertToDecimal ();
-
+                       Constant c = ConvertImplicitly (type);
                        if (c == null)
-                               Error_ValueCannotBeConverted (loc, TypeManager.decimal_type, false);
-
+                               Error_ValueCannotBeConverted (ec, loc, type, false);
                        return c;
                }
 
-               public virtual Constant ToType (Type type, Location loc)
+               public virtual Constant ConvertImplicitly (Type type)
                {
-                       if (Type == type)
-                               return this;
-
-                       if (type == TypeManager.object_type)
+                       if (this.type == type)
                                return this;
 
-                       if (!Convert.ImplicitStandardConversionExists (Convert.ConstantEC, this, type)){
-                               Error_ValueCannotBeConverted (loc, type, false);
+                       if (Convert.ImplicitNumericConversion (this, type) == null) 
                                return null;
-                       }
-
-                       // Special-case: The 0 literal can be converted to an enum value,
-                       // and ImplicitStandardConversionExists will return true in that case.
-                       if (IsZeroInteger && Type == TypeManager.int32_type && TypeManager.IsEnumType (type)) {
-                               return new EnumConstant (this, type);
-                       }
 
                        bool fail;                      
                        object constant_value = TypeManager.ChangeType (GetValue (), type, out fail);
                        if (fail){
-                               Error_ValueCannotBeConverted (loc, type, false);
-                               
                                //
                                // We should always catch the error before this is ever
                                // reached, by calling Convert.ImplicitStandardConversionExists
                                //
-                               throw new Exception (
-                                       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, loc);
-                       else if (type == TypeManager.uint32_type)
-                               retval = new UIntConstant ((uint) constant_value, loc);
-                       else if (type == TypeManager.int64_type)
-                               retval = new LongConstant ((long) constant_value, loc);
-                       else if (type == TypeManager.uint64_type)
-                               retval = new ULongConstant ((ulong) constant_value, loc);
-                       else if (type == TypeManager.float_type)
-                               retval = new FloatConstant ((float) constant_value, loc);
-                       else if (type == TypeManager.double_type)
-                               retval = new DoubleConstant ((double) constant_value, loc);
-                       else if (type == TypeManager.string_type)
-                               retval = new StringConstant ((string) constant_value, loc);
-                       else if (type == TypeManager.short_type)
-                               retval = new ShortConstant ((short) constant_value, loc);
-                       else if (type == TypeManager.ushort_type)
-                               retval = new UShortConstant ((ushort) constant_value, loc);
-                       else if (type == TypeManager.sbyte_type)
-                               retval = new SByteConstant ((sbyte) constant_value, loc);
-                       else if (type == TypeManager.byte_type)
-                               retval = new ByteConstant ((byte) constant_value, loc);
-                       else if (type == TypeManager.char_type)
-                               retval = new CharConstant ((char) constant_value, loc);
-                       else if (type == TypeManager.bool_type)
-                               retval = new BoolConstant ((bool) constant_value, loc);
-                       else if (type == TypeManager.decimal_type)
-                               retval = new DecimalConstant ((decimal) constant_value, loc);
-                       else
-                               throw new Exception ("LookupConstantValue: Unhandled constant type: " + type);
-                       
-                       return retval;
-               }
-
-               protected void CheckRange (EmitContext ec, ulong value, Type type, ulong max)
-               {
-                       if (!ec.ConstantCheckState)
-                               return;
-
-                       if (value > max)
-                               throw new OverflowException ();
-               }
-
-               protected void CheckRange (EmitContext ec, double value, Type type, long min, long max)
-               {
-                       if (!ec.ConstantCheckState)
-                               return;
-
-                       if (((value < min) || (value > max)))
-                               throw new OverflowException ();
+                               throw new InternalErrorException ("Missing constant conversion between `{0}' and `{1}'",
+                                 TypeManager.CSharpName (Type), TypeManager.CSharpName (type));
+                       }
+
+                       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.GetEnumUnderlyingType (t);
+                               return new EnumConstant (CreateConstant (real_type, v, loc), t);
+                       } 
+                       if (v == null && !TypeManager.IsValueType (t))
+                               return new EmptyConstantCast (new NullLiteral (loc), t);
+
+                       throw new Exception ("Unknown type for constant (" + t +
+                                       "), details: " + v);
+               }
+
+               public override Expression CreateExpressionTree (EmitContext ec)
+               {
+                       ArrayList args = new ArrayList (2);
+                       args.Add (new Argument (this));
+                       args.Add (new Argument (
+                               new TypeOf (new TypeExpression (type, loc), loc)));
+
+                       return CreateExpressionFactoryCall ("Constant", args);
                }
 
-               protected void CheckUnsigned (EmitContext ec, long value, Type type)
-               {
-                       if (!ec.ConstantCheckState)
-                               return;
-
-                       if (value < 0)
-                               throw new OverflowException ();
-               }
 
-               public abstract Constant Reduce (EmitContext ec, Type target_type);
+               /// <summary>
+               /// Maybe ConvertTo name is better. It tries to convert `this' constant to target_type.
+               /// It throws OverflowException 
+               /// </summary>
+               // DON'T CALL THIS METHOD DIRECTLY AS IT DOES NOT HANDLE ENUMS
+               public abstract Constant ConvertExplicitly (bool in_checked_context, Type target_type);
 
                /// <summary>
                ///   Attempts to do a compile-time folding of a constant cast.
@@ -241,14 +185,17 @@ namespace Mono.CSharp {
                public Constant TryReduce (EmitContext ec, Type target_type, Location loc)
                {
                        try {
-                               return  TryReduce (ec, target_type);
+                               return TryReduce (ec, target_type);
                        }
                        catch (OverflowException) {
-                               if (ec.ConstantCheckState) {
+                               if (ec.ConstantCheckState) {                            
                                        Report.Error (221, loc, "Constant value `{0}' cannot be converted to a `{1}' (use `unchecked' syntax to override)",
                                                GetValue ().ToString (), TypeManager.CSharpName (target_type));
+                               } else {
+                                       Error_ValueCannotBeConverted (ec, loc, target_type, false);
                                }
-                               return null;
+
+                               return New.Constantify (target_type);
                        }
                }
 
@@ -258,68 +205,86 @@ namespace Mono.CSharp {
                                return this;
 
                        if (TypeManager.IsEnumType (target_type)) {
-                               Constant c = TryReduce (ec, TypeManager.EnumToUnderlying (target_type));
+                               Constant c = TryReduce (ec, TypeManager.GetEnumUnderlyingType (target_type));
                                if (c == null)
                                        return null;
 
                                return new EnumConstant (c, target_type);
                        }
 
-                       return Reduce (ec, target_type);
+                       return ConvertExplicitly (ec.ConstantCheckState, target_type);
                }
 
-
-               public virtual DecimalConstant ConvertToDecimal ()
-               {
-                       return null;
-               }
+               public abstract Constant Increment ();
                
-               public virtual DoubleConstant ConvertToDouble ()
+               /// <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)
                {
-                       return null;
+                       if (type == Type)
+                               return IsDefaultValue;
+
+                       return this is NullLiteral;
                }
 
-               public virtual FloatConstant ConvertToFloat ()
-               {
-                       return null;
+               public abstract bool IsDefaultValue {
+                       get;
                }
 
-               public virtual ULongConstant ConvertToULong ()
-               {
-                       return null;
+               public abstract bool IsNegative {
+                       get;
                }
 
-               public virtual LongConstant ConvertToLong ()
-               {
-                       return null;
+               //
+               // When constant is declared as literal
+               //
+               public virtual bool IsLiteral {
+                       get { return false; }
+               }
+
+               //
+               // Returns true iff 1) the stack type of this is one of Object, 
+               // int32, int64 and 2) this == 0 or this == null.
+               //
+               public virtual bool IsZeroInteger {
+                       get { return false; }
                }
 
-               public virtual UIntConstant ConvertToUInt ()
+               public override void EmitSideEffect (EmitContext ec)
                {
-                       return null;
+                       // do nothing
                }
 
-               public virtual IntConstant ConvertToInt ()
+               protected override void CloneTo (CloneContext clonectx, Expression target)
                {
-                       return null;
+                       // CloneTo: Nothing, we do not keep any state on this expression
                }
 
-               public abstract Constant Increment ();
-               
-               public abstract bool IsDefaultValue {
-                       get;
+               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+               {
+                       // A constant cannot be of generic type
                }
+       }
 
-               public abstract bool IsNegative {
-                       get;
+       public abstract class IntegralConstant : Constant {
+               protected IntegralConstant (Location loc) :
+                       base (loc)
+               {
                }
 
-               //
-               // Returns true iff 1) the stack type of this is one of Object, 
-               // int32, int64 and 2) this == 0 or this == null.
-               //
-               public virtual bool IsZeroInteger {
-                       get { return false; }
+               public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
+               {
+                       try {
+                               ConvertExplicitly (true, target);
+                               base.Error_ValueCannotBeConverted (ec, loc, target, expl);
+                       }
+                       catch
+                       {
+                               Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
+                                       GetValue ().ToString (), TypeManager.CSharpName (target));
+                       }
                }
        }
        
@@ -375,14 +340,14 @@ namespace Mono.CSharp {
                        get { return Value == false; }
                }
 
-               public override Constant Reduce (EmitContext ec, Type target_type)
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
                        return null;
                }
 
        }
 
-       public class ByteConstant : Constant {
+       public class ByteConstant : IntegralConstant {
                public readonly byte Value;
 
                public ByteConstant (byte v, Location loc):
@@ -408,36 +373,6 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value, loc);
-               }
-
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return new FloatConstant (Value, loc);
-               }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       return new ULongConstant (Value, loc);
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return new LongConstant (Value, loc);
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return new UIntConstant (Value, loc);
-               }
-
-               public override IntConstant ConvertToInt ()
-               {
-                       return new IntConstant (Value, loc);
-               }
-
                public override Constant Increment ()
                {
                        return new ByteConstant (checked ((byte)(Value + 1)), loc);
@@ -459,10 +394,13 @@ namespace Mono.CSharp {
                        get { return Value == 0; }
                }
 
-               public override Constant Reduce (EmitContext ec, Type target_type)
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (ec, Value, target_type, SByte.MinValue, SByte.MaxValue);
+                               if (in_checked_context){
+                                       if (Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type)
@@ -546,36 +484,6 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value, loc);
-               }
-
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return new FloatConstant (Value, loc);
-               }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       return new ULongConstant (Value, loc);
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return new LongConstant (Value, loc);
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return new UIntConstant (Value, loc);
-               }
-
-               public override IntConstant ConvertToInt ()
-               {
-                       return new IntConstant (Value, loc);
-               }
-
                public override Constant Increment ()
                {
                        return new CharConstant (checked ((char)(Value + 1)), loc);
@@ -597,18 +505,27 @@ namespace Mono.CSharp {
                        get { return Value == '\0'; }
                }
 
-               public override Constant Reduce (EmitContext ec, Type target_type)
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (ec, Value, target_type, Byte.MinValue, Byte.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < Byte.MinValue || Value > Byte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (ec, Value, target_type, SByte.MinValue, SByte.MaxValue);
+                               if (in_checked_context){
+                                       if (Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type) {
-                               CheckRange (ec, Value, target_type, Int16.MinValue, Int16.MaxValue);
+                               if (in_checked_context){
+                                       if (Value > Int16.MaxValue)
+                                               throw new OverflowException ();
+                               }                                       
                                return new ShortConstant ((short) Value, Location);
                        }
                        if (target_type == TypeManager.int32_type)
@@ -631,7 +548,7 @@ namespace Mono.CSharp {
 
        }
 
-       public class SByteConstant : Constant {
+       public class SByteConstant : IntegralConstant {
                public readonly sbyte Value;
 
                public SByteConstant (sbyte v, Location loc):
@@ -657,39 +574,6 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value, loc);
-               }
-
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return new FloatConstant (Value, loc);
-               }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       if (Value >= 0)
-                               return new ULongConstant ((ulong) Value, loc);
-                       
-                       return null;
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return new LongConstant (Value, loc);
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return null;
-               }
-
-               public override IntConstant ConvertToInt ()
-               {
-                       return new IntConstant (Value, loc);
-               }
-
                public override Constant Increment ()
                {
                    return new SByteConstant (checked((sbyte)(Value + 1)), loc);
@@ -711,26 +595,30 @@ namespace Mono.CSharp {
                        get { return Value == 0; }
                }
 
-               public override Constant Reduce (EmitContext ec, Type target_type)
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckUnsigned (ec, Value, target_type);
+                               if (in_checked_context && 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 (ec, Value, target_type);
+                               if (in_checked_context && 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 (ec, Value, target_type);
+                               if (in_checked_context && 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 (ec, Value, target_type);
+                               if (in_checked_context && Value < 0)
+                                       throw new OverflowException ();
                                return new ULongConstant ((ulong) Value, Location);
                        }
                        if (target_type == TypeManager.float_type)
@@ -738,7 +626,8 @@ namespace Mono.CSharp {
                        if (target_type == TypeManager.double_type)
                                return new DoubleConstant ((double) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckUnsigned (ec, Value, target_type);
+                               if (in_checked_context && Value < 0)
+                                       throw new OverflowException ();
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -749,7 +638,7 @@ namespace Mono.CSharp {
 
        }
 
-       public class ShortConstant : Constant {
+       public class ShortConstant : IntegralConstant {
                public readonly short Value;
 
                public ShortConstant (short v, Location loc):
@@ -775,36 +664,6 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value, loc);
-               }
-
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return new FloatConstant (Value, loc);
-               }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       return null;
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return new LongConstant (Value, loc);
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return null;
-               }
-
-               public override IntConstant ConvertToInt ()
-               {
-                       return new IntConstant (Value, loc);
-               }
-
                public override Constant Increment ()
                {
                        return new ShortConstant (checked((short)(Value + 1)), loc);
@@ -826,30 +685,40 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Constant Reduce (EmitContext ec, Type target_type)
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (ec, Value, target_type, Byte.MinValue, Byte.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < Byte.MinValue || Value > Byte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (ec, Value, target_type, SByte.MinValue, SByte.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < SByte.MinValue || Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.ushort_type) {
-                               CheckUnsigned (ec, Value, target_type);
+                               if (in_checked_context && 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 (ec, Value, target_type);
+                               if (in_checked_context && 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 (ec, Value, target_type);
+                               if (in_checked_context && Value < 0)
+                                       throw new OverflowException ();
                                return new ULongConstant ((ulong) Value, Location);
                        }
                        if (target_type == TypeManager.float_type)
@@ -857,7 +726,10 @@ namespace Mono.CSharp {
                        if (target_type == TypeManager.double_type)
                                return new DoubleConstant ((double) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckRange (ec, Value, target_type, Char.MinValue, Char.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < Char.MinValue)
+                                               throw new OverflowException ();
+                               }
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -868,7 +740,7 @@ namespace Mono.CSharp {
 
        }
 
-       public class UShortConstant : Constant {
+       public class UShortConstant : IntegralConstant {
                public readonly ushort Value;
 
                public UShortConstant (ushort v, Location loc):
@@ -893,36 +765,6 @@ namespace Mono.CSharp {
                {
                        return Value;
                }
-
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value, loc);
-               }
-
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return new FloatConstant (Value, loc);
-               }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       return new ULongConstant (Value, loc);
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return new LongConstant (Value, loc);
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return new UIntConstant (Value, loc);
-               }
-
-               public override IntConstant ConvertToInt ()
-               {
-                       return new IntConstant (Value, loc);
-               }
        
                public override Constant Increment ()
                {
@@ -945,18 +787,27 @@ namespace Mono.CSharp {
                        get { return Value == 0; }
                }
 
-               public override Constant Reduce (EmitContext ec, Type target_type)
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (ec, Value, target_type, Byte.MinValue, Byte.MaxValue);
+                               if (in_checked_context){
+                                       if (Value > Byte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (ec, Value, target_type, SByte.MinValue, SByte.MaxValue);
+                               if (in_checked_context){
+                                       if (Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type) {
-                               CheckRange (ec, Value, target_type, Int16.MinValue, Int16.MaxValue);
+                               if (in_checked_context){
+                                       if (Value > Int16.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ShortConstant ((short) Value, Location);
                        }
                        if (target_type == TypeManager.int32_type)
@@ -972,7 +823,10 @@ namespace Mono.CSharp {
                        if (target_type == TypeManager.double_type)
                                return new DoubleConstant ((double) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckRange (ec, Value, target_type, Char.MinValue, Char.MaxValue);
+                               if (in_checked_context){
+                                       if (Value > Char.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -982,7 +836,7 @@ namespace Mono.CSharp {
                }
        }
 
-       public class IntConstant : Constant {
+       public class IntConstant : IntegralConstant {
                public readonly int Value;
 
                public IntConstant (int v, Location loc):
@@ -1060,94 +914,69 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DecimalConstant ConvertToDecimal()
+               public override Constant Increment ()
                {
-                       return new DecimalConstant (Value, loc);
+                       return new IntConstant (checked(Value + 1), loc);
                }
 
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value, loc);
+               public override bool IsDefaultValue {
+                       get {
+                               return Value == 0;
+                       }
                }
-
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return new FloatConstant (Value, loc);
-               }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       if (Value < 0)
-                               return null;
-
-                       return new ULongConstant ((ulong) Value, loc);
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return new LongConstant (Value, loc);
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       if (Value < 0)
-                               return null;
-
-                       return new UIntConstant ((uint) Value, loc);
-               }
-
-               public override IntConstant ConvertToInt ()
-               {
-                       return this;
-               }
-
-               public override Constant Increment ()
-               {
-                       return new IntConstant (checked(Value + 1), loc);
-               }
-
-               public override bool IsDefaultValue {
-                       get {
-                               return Value == 0;
-                       }
-               }
-               
-               public override bool IsNegative {
-                       get {
-                               return Value < 0;
-                       }
+               
+               public override bool IsNegative {
+                       get {
+                               return Value < 0;
+                       }
                }
 
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
 
-               public override Constant Reduce (EmitContext ec, Type target_type)
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (ec, Value, target_type, Byte.MinValue, Byte.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < Byte.MinValue || Value > Byte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (ec, Value, target_type, SByte.MinValue, SByte.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < SByte.MinValue || Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type) {
-                               CheckRange (ec, Value, target_type, Int16.MinValue, Int16.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < Int16.MinValue || Value > Int16.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ShortConstant ((short) Value, Location);
                        }
                        if (target_type == TypeManager.ushort_type) {
-                               CheckRange (ec, Value, target_type, UInt16.MinValue, UInt16.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < UInt16.MinValue || Value > UInt16.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new UShortConstant ((ushort) Value, Location);
                        }
                        if (target_type == TypeManager.uint32_type) {
-                               CheckRange (ec, Value, target_type, Int32.MinValue, Int32.MaxValue);
+                               if (in_checked_context){
+                                       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 (ec, Value, target_type);
+                               if (in_checked_context && Value < 0)
+                                       throw new OverflowException ();
                                return new ULongConstant ((ulong) Value, Location);
                        }
                        if (target_type == TypeManager.float_type)
@@ -1155,7 +984,10 @@ namespace Mono.CSharp {
                        if (target_type == TypeManager.double_type)
                                return new DoubleConstant ((double) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckRange (ec, Value, target_type, Char.MinValue, Char.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < Char.MinValue || Value > Char.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -1163,9 +995,65 @@ 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 : Constant {
+       public class UIntConstant : IntegralConstant {
                public readonly uint Value;
 
                public UIntConstant (uint v, Location loc):
@@ -1191,36 +1079,6 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value, loc);
-               }
-
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return new FloatConstant (Value, loc);
-               }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       return new ULongConstant (Value, loc);
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return new LongConstant (Value, loc);
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return this;
-               }
-
-               public override IntConstant ConvertToInt ()
-               {
-                       return null;
-               }
-       
                public override Constant Increment ()
                {
                        return new UIntConstant (checked(Value + 1), loc);
@@ -1242,26 +1100,41 @@ namespace Mono.CSharp {
                        get { return Value == 0; }
                }
 
-               public override Constant Reduce (EmitContext ec, Type target_type)
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (ec, Value, target_type, Char.MinValue, Char.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < Char.MinValue || Value > Char.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (ec, Value, target_type, SByte.MinValue, SByte.MaxValue);
+                               if (in_checked_context){
+                                       if (Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type) {
-                               CheckRange (ec, Value, target_type, Int16.MinValue, Int16.MaxValue);
+                               if (in_checked_context){
+                                       if (Value > Int16.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ShortConstant ((short) Value, Location);
                        }
                        if (target_type == TypeManager.ushort_type) {
-                               CheckRange (ec, Value, target_type, UInt16.MinValue, UInt16.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < UInt16.MinValue || Value > UInt16.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new UShortConstant ((ushort) Value, Location);
                        }
                        if (target_type == TypeManager.int32_type) {
-                               CheckRange (ec, Value, target_type, Int32.MinValue, Int32.MaxValue);
+                               if (in_checked_context){
+                                       if (Value > Int32.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new IntConstant ((int) Value, Location);
                        }
                        if (target_type == TypeManager.int64_type)
@@ -1273,7 +1146,10 @@ namespace Mono.CSharp {
                        if (target_type == TypeManager.double_type)
                                return new DoubleConstant ((double) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckRange (ec, Value, target_type, Char.MinValue, Char.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < Char.MinValue || Value > Char.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -1284,7 +1160,7 @@ namespace Mono.CSharp {
 
        }
 
-       public class LongConstant : Constant {
+       public class LongConstant : IntegralConstant {
                public readonly long Value;
 
                public LongConstant (long v, Location loc):
@@ -1297,19 +1173,24 @@ 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_I8);
+                               return;
+                       }
+
+                       if (l >= 0 && l <= uint.MaxValue) {
                                IntLiteral.EmitInt (ig, unchecked ((int) l));
                                ig.Emit (OpCodes.Conv_U8);
-                       } else {
-                               ig.Emit (OpCodes.Ldc_I8, l);
+                               return;
                        }
+                       
+                       ig.Emit (OpCodes.Ldc_I8, l);
                }
 
                public override string AsString ()
@@ -1322,39 +1203,6 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value, loc);
-               }
-
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return new FloatConstant (Value, loc);
-               }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       if (Value < 0)
-                               return null;
-                       
-                       return new ULongConstant ((ulong) Value, loc);
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return this;
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return null;
-               }
-
-               public override IntConstant ConvertToInt ()
-               {
-                       return null;
-               }
-
                public override Constant Increment ()
                {
                        return new LongConstant (checked(Value + 1), loc);
@@ -1376,34 +1224,53 @@ namespace Mono.CSharp {
                        get { return Value == 0; }
                }
 
-               public override Constant Reduce (EmitContext ec, Type target_type)
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (ec, Value, target_type, Byte.MinValue, Byte.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < Byte.MinValue || Value > Byte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (ec, Value, target_type, SByte.MinValue, SByte.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < SByte.MinValue || Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type) {
-                               CheckRange (ec, Value, target_type, Int16.MinValue, Int16.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < Int16.MinValue || Value > Int16.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new ShortConstant ((short) Value, Location);
                        }
                        if (target_type == TypeManager.ushort_type) {
-                               CheckRange (ec, Value, target_type, UInt16.MinValue, UInt16.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < UInt16.MinValue || Value > UInt16.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new UShortConstant ((ushort) Value, Location);
                        }
                        if (target_type == TypeManager.int32_type) {
-                               CheckRange (ec, Value, target_type, Int32.MinValue, Int32.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < Int32.MinValue || Value > Int32.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new IntConstant ((int) Value, Location);
                        }
                        if (target_type == TypeManager.uint32_type) {
-                               CheckRange (ec, Value, target_type, UInt32.MinValue, UInt32.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < UInt32.MinValue || Value > UInt32.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new UIntConstant ((uint) Value, Location);
                        }
                        if (target_type == TypeManager.uint64_type) {
-                               CheckUnsigned (ec, Value, target_type);
+                               if (in_checked_context && Value < 0)
+                                       throw new OverflowException ();
                                return new ULongConstant ((ulong) Value, Location);
                        }
                        if (target_type == TypeManager.float_type)
@@ -1411,7 +1278,10 @@ namespace Mono.CSharp {
                        if (target_type == TypeManager.double_type)
                                return new DoubleConstant ((double) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckRange (ec, Value, target_type, Char.MinValue, Char.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < Char.MinValue || Value > Char.MaxValue)
+                                               throw new OverflowException ();
+                               }
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -1420,9 +1290,17 @@ 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 : Constant {
+       public class ULongConstant : IntegralConstant {
                public readonly ulong Value;
 
                public ULongConstant (ulong v, Location loc):
@@ -1450,36 +1328,6 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value, loc);
-               }
-
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return new FloatConstant (Value, loc);
-               }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       return this;
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return null;
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return null;
-               }
-
-               public override IntConstant ConvertToInt ()
-               {
-                       return null;
-               }
-
                public override Constant Increment ()
                {
                        return new ULongConstant (checked(Value + 1), loc);
@@ -1501,34 +1349,41 @@ namespace Mono.CSharp {
                        get { return Value == 0; }
                }
 
-               public override Constant Reduce (EmitContext ec, Type target_type)
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (ec, Value, target_type, Byte.MaxValue);
+                               if (in_checked_context && Value > Byte.MaxValue)
+                                       throw new OverflowException ();
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (ec, Value, target_type, (ulong) SByte.MaxValue);
+                               if (in_checked_context && Value > ((ulong) SByte.MaxValue))
+                                       throw new OverflowException ();
                                return new SByteConstant ((sbyte) Value, Location);
                        }
                        if (target_type == TypeManager.short_type) {
-                               CheckRange (ec, Value, target_type, (ulong) Int16.MaxValue);
+                               if (in_checked_context && Value > ((ulong) Int16.MaxValue))
+                                       throw new OverflowException ();
                                return new ShortConstant ((short) Value, Location);
                        }
                        if (target_type == TypeManager.ushort_type) {
-                               CheckRange (ec, Value, target_type, UInt16.MaxValue);
+                               if (in_checked_context && Value > UInt16.MaxValue)
+                                       throw new OverflowException ();
                                return new UShortConstant ((ushort) Value, Location);
                        }
                        if (target_type == TypeManager.int32_type) {
-                               CheckRange (ec, Value, target_type, Int32.MaxValue);
+                               if (in_checked_context && Value > UInt32.MaxValue)
+                                       throw new OverflowException ();
                                return new IntConstant ((int) Value, Location);
                        }
                        if (target_type == TypeManager.uint32_type) {
-                               CheckRange (ec, Value, target_type, UInt32.MaxValue);
+                               if  (in_checked_context && Value > UInt32.MaxValue)
+                                       throw new OverflowException ();
                                return new UIntConstant ((uint) Value, Location);
                        }
                        if (target_type == TypeManager.int64_type) {
-                               CheckRange (ec, Value, target_type, (ulong) Int64.MaxValue);
+                               if (in_checked_context && Value > Int64.MaxValue)
+                                       throw new OverflowException ();
                                return new LongConstant ((long) Value, Location);
                        }
                        if (target_type == TypeManager.float_type)
@@ -1536,7 +1391,8 @@ namespace Mono.CSharp {
                        if (target_type == TypeManager.double_type)
                                return new DoubleConstant ((double) Value, Location);
                        if (target_type == TypeManager.char_type) {
-                               CheckRange (ec, Value, target_type, Char.MaxValue);
+                               if (in_checked_context && Value > Char.MaxValue)
+                                       throw new OverflowException ();
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -1548,7 +1404,7 @@ namespace Mono.CSharp {
        }
 
        public class FloatConstant : Constant {
-               public readonly float Value;
+               public float Value;
 
                public FloatConstant (float v, Location loc):
                        base (loc)
@@ -1573,31 +1429,6 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value, loc);
-               }
-
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return this;
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return null;
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return null;
-               }
-
-               public override IntConstant ConvertToInt ()
-               {
-                       return null;
-               }
-
                public override Constant Increment ()
                {
                        return new FloatConstant (checked(Value + 1), loc);
@@ -1615,28 +1446,73 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Constant Reduce (EmitContext ec, Type target_type)
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
-                       if (target_type == TypeManager.byte_type)
+                       if (target_type == TypeManager.byte_type) {
+                               if (in_checked_context){
+                                       if (Value < byte.MinValue || Value > byte.MaxValue || float.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
-                       if (target_type == TypeManager.sbyte_type)
+                       }
+                       if (target_type == TypeManager.sbyte_type) {
+                               if (in_checked_context){
+                                       if (Value < sbyte.MinValue || Value > sbyte.MaxValue || float.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
-                       if (target_type == TypeManager.short_type)
+                       }
+                       if (target_type == TypeManager.short_type) {
+                               if (in_checked_context){
+                                       if (Value < short.MinValue || Value > short.MaxValue || float.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new ShortConstant ((short) Value, Location);
-                       if (target_type == TypeManager.ushort_type)
+                       }
+                       if (target_type == TypeManager.ushort_type) {
+                               if (in_checked_context){
+                                       if (Value < ushort.MinValue || Value > ushort.MaxValue || float.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new UShortConstant ((ushort) Value, Location);
-                       if (target_type == TypeManager.int32_type)
+                       }
+                       if (target_type == TypeManager.int32_type) {
+                               if (in_checked_context){
+                                       if (Value < int.MinValue || Value > int.MaxValue || float.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new IntConstant ((int) Value, Location);
-                       if (target_type == TypeManager.uint32_type)
+                       }
+                       if (target_type == TypeManager.uint32_type) {
+                               if (in_checked_context){
+                                       if (Value < uint.MinValue || Value > uint.MaxValue || float.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new UIntConstant ((uint) Value, Location);
-                       if (target_type == TypeManager.int64_type)
+                       }
+                       if (target_type == TypeManager.int64_type) {
+                               if (in_checked_context){
+                                       if (Value < long.MinValue || Value > long.MaxValue || float.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new LongConstant ((long) Value, Location);
-                       if (target_type == TypeManager.uint64_type)
+                       }
+                       if (target_type == TypeManager.uint64_type) {
+                               if (in_checked_context){
+                                       if (Value < ulong.MinValue || Value > ulong.MaxValue || float.IsNaN (Value))
+                                               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)
+                       if (target_type == TypeManager.char_type) {
+                               if (in_checked_context){
+                                       if (Value < (float) char.MinValue || Value > (float) char.MaxValue || float.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new CharConstant ((char) Value, Location);
+                       }
                        if (target_type == TypeManager.decimal_type)
                                return new DecimalConstant ((decimal) Value, Location);
 
@@ -1646,7 +1522,7 @@ namespace Mono.CSharp {
        }
 
        public class DoubleConstant : Constant {
-               public readonly double Value;
+               public double Value;
 
                public DoubleConstant (double v, Location loc):
                        base (loc)
@@ -1671,36 +1547,6 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return this;
-               }
-
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return null;
-               }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       return null;
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return null;
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return null;
-               }
-
-               public override IntConstant ConvertToInt ()
-               {
-                       return null;
-               }
-
                public override Constant Increment ()
                {
                        return new DoubleConstant (checked(Value + 1), loc);
@@ -1718,32 +1564,71 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Constant Reduce (EmitContext ec, Type target_type)
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
                        if (target_type == TypeManager.byte_type) {
-                               CheckRange (ec, Value, target_type, Byte.MinValue, Byte.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < Byte.MinValue || Value > Byte.MaxValue || double.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new ByteConstant ((byte) Value, Location);
                        }
                        if (target_type == TypeManager.sbyte_type) {
-                               CheckRange (ec, Value, target_type, SByte.MinValue, SByte.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < SByte.MinValue || Value > SByte.MaxValue || double.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new SByteConstant ((sbyte) Value, Location);
                        }
-                       if (target_type == TypeManager.short_type)
+                       if (target_type == TypeManager.short_type) {
+                               if (in_checked_context){
+                                       if (Value < short.MinValue || Value > short.MaxValue || double.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new ShortConstant ((short) Value, Location);
-                       if (target_type == TypeManager.ushort_type)
+                       }
+                       if (target_type == TypeManager.ushort_type) {
+                               if (in_checked_context){
+                                       if (Value < ushort.MinValue || Value > ushort.MaxValue || double.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new UShortConstant ((ushort) Value, Location);
-                       if (target_type == TypeManager.int32_type)
+                       }
+                       if (target_type == TypeManager.int32_type) {
+                               if (in_checked_context){
+                                       if (Value < int.MinValue || Value > int.MaxValue || double.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new IntConstant ((int) Value, Location);
-                       if (target_type == TypeManager.uint32_type)
+                       }
+                       if (target_type == TypeManager.uint32_type) {
+                               if (in_checked_context){
+                                       if (Value < uint.MinValue || Value > uint.MaxValue || double.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new UIntConstant ((uint) Value, Location);
-                       if (target_type == TypeManager.int64_type)
+                       }
+                       if (target_type == TypeManager.int64_type) {
+                               if (in_checked_context){
+                                       if (Value < long.MinValue || Value > long.MaxValue || double.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new LongConstant ((long) Value, Location);
-                       if (target_type == TypeManager.uint64_type)
+                       }
+                       if (target_type == TypeManager.uint64_type) {
+                               if (in_checked_context){
+                                       if (Value < ulong.MinValue || Value > ulong.MaxValue || double.IsNaN (Value))
+                                               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 (ec, Value, target_type, char.MinValue, char.MaxValue);
+                               if (in_checked_context){
+                                       if (Value < (double) char.MinValue || Value > (double) char.MaxValue || double.IsNaN (Value))
+                                               throw new OverflowException ();
+                               }
                                return new CharConstant ((char) Value, Location);
                        }
                        if (target_type == TypeManager.decimal_type)
@@ -1767,7 +1652,7 @@ namespace Mono.CSharp {
 
                override public string AsString ()
                {
-                       return Value.ToString ();
+                       return Value.ToString () + "M";
                }
 
                public override object GetValue ()
@@ -1784,6 +1669,14 @@ namespace Mono.CSharp {
 
                        if (power == 0 && Value <= int.MaxValue && Value >= int.MinValue)
                        {
+                               if (TypeManager.void_decimal_ctor_int_arg == null) {
+                                       TypeManager.void_decimal_ctor_int_arg = TypeManager.GetPredefinedConstructor (
+                                               TypeManager.decimal_type, loc, TypeManager.int32_type);
+
+                                       if (TypeManager.void_decimal_ctor_int_arg == null)
+                                               return;
+                               }
+
                                IntConstant.EmitInt (ig, (int)Value);
                                ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_int_arg);
                                return;
@@ -1805,6 +1698,15 @@ namespace Mono.CSharp {
                        // power
                        IntConstant.EmitInt (ig, power);
 
+                       if (TypeManager.void_decimal_ctor_five_args == null) {
+                               TypeManager.void_decimal_ctor_five_args = TypeManager.GetPredefinedConstructor (
+                                       TypeManager.decimal_type, loc, TypeManager.int32_type, TypeManager.int32_type,
+                                       TypeManager.int32_type, TypeManager.bool_type, TypeManager.byte_type);
+
+                               if (TypeManager.void_decimal_ctor_five_args == null)
+                                       return;
+                       }
+
                        ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_five_args);
                }
 
@@ -1825,7 +1727,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Constant Reduce (EmitContext ec, Type target_type)
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
                        if (target_type == TypeManager.sbyte_type)
                                return new SByteConstant ((sbyte)Value, loc);
@@ -1879,10 +1781,26 @@ namespace Mono.CSharp {
                
                public override void Emit (EmitContext ec)
                {
-                       if (Value == null)
+                       if (Value == null) {
                                ec.ig.Emit (OpCodes.Ldnull);
-                       else
-                               ec.ig.Emit (OpCodes.Ldstr, Value);
+                               return;
+                       }
+
+                       //
+                       // Use string.Empty for both literals and constants even if
+                       // it's not allowed at language level
+                       //
+                       if (Value.Length == 0 && RootContext.Optimize && ec.TypeContainer.TypeBuilder != TypeManager.string_type) {                     
+                               if (TypeManager.string_empty == null)
+                                       TypeManager.string_empty = TypeManager.GetPredefinedField (TypeManager.string_type, "Empty", loc);
+
+                               if (TypeManager.string_empty != null) {
+                                       ec.ig.Emit (OpCodes.Ldsfld, TypeManager.string_empty);
+                                       return;
+                               }
+                       }
+
+                       ec.ig.Emit (OpCodes.Ldstr, Value);
                }
 
                public override Constant Increment ()
@@ -1902,12 +1820,75 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Constant Reduce (EmitContext ec, Type target_type)
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
                        return null;
                }
        }
 
-}
+       /// <summary>
+       ///   The value is constant, but when emitted has a side effect.  This is
+       ///   used by BitwiseAnd to ensure that the second expression is invoked
+       ///   regardless of the value of the left side.  
+       /// </summary>
+       
+       public class SideEffectConstant : Constant {
+               public Constant value;
+               Expression side_effect;
+               
+               public SideEffectConstant (Constant value, Expression side_effect, Location loc) : base (loc)
+               {
+                       this.value = value;
+                       while (side_effect is SideEffectConstant)
+                               side_effect = ((SideEffectConstant) side_effect).side_effect;
+                       this.side_effect = side_effect;
+                       eclass = ExprClass.Value;
+                       type = value.Type;
+               }
+
+               public override string AsString ()
+               {
+                       return value.AsString ();
+               }
+
+               public override object GetValue ()
+               {
+                       return value.GetValue ();
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       side_effect.EmitSideEffect (ec);
+                       value.Emit (ec);
+               }
+
+               public override void EmitSideEffect (EmitContext ec)
+               {
+                       side_effect.EmitSideEffect (ec);
+                       value.EmitSideEffect (ec);
+               }
 
+               public override bool IsDefaultValue {
+                       get { return value.IsDefaultValue; }
+               }
 
+               public override Constant Increment ()
+               {
+                       throw new NotSupportedException ();
+               }
+               
+               public override bool IsNegative {
+                       get { return value.IsNegative; }
+               }
+
+               public override bool IsZeroInteger {
+                       get { return value.IsZeroInteger; }
+               }
+
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+               {
+                       Constant new_value = value.ConvertExplicitly (in_checked_context, target_type);
+                       return new_value == null ? null : new SideEffectConstant (new_value, side_effect, new_value.Location);
+               }
+       }
+}