Remove redundant TARGET_JVMs
[mono.git] / mcs / mcs / constant.cs
old mode 100755 (executable)
new mode 100644 (file)
index 7fdcfc4..5dae2a6
@@ -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,12 +39,34 @@ namespace Mono.CSharp {
                        return this.GetType ().Name + " (" + AsString () + ")";
                }
 
+               public override bool GetAttributableValue (Type value_type, out object value)
+               {
+                       if (value_type == TypeManager.object_type) {
+                               value = GetTypedValue ();
+                               return true;
+                       }
+
+                       Constant c = ImplicitConversionRequired (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.
                /// </summary>
                public abstract object GetValue ();
 
+               public virtual object GetTypedValue ()
+               {
+                       return GetValue ();
+               }
+
                /// <summary>
                ///   Constants are always born in a fully resolved state
                /// </summary>
@@ -46,122 +75,136 @@ 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)
+               public Constant ImplicitConversionRequired (Type type, Location loc)
                {
-                       DoubleConstant c = ConvertToDouble ();
-
+                       Constant c = ConvertImplicitly (type);
                        if (c == null)
-                               Convert.Error_CannotImplicitConversion (loc, Type, TypeManager.double_type);
-
+                               Error_ValueCannotBeConverted (null, loc, type, false);
                        return c;
                }
 
-               public FloatConstant ToFloat (Location loc)
+               public virtual Constant ConvertImplicitly (Type type)
                {
-                       FloatConstant c = ConvertToFloat ();
-
-                       if (c == null)
-                               Convert.Error_CannotImplicitConversion (loc, Type, TypeManager.float_type);
+                       if (this.type == type)
+                               return this;
 
-                       return c;
-               }
-
-               public ULongConstant ToULong (Location loc)
-               {
-                       ULongConstant c = ConvertToULong ();
+                       if (Convert.ImplicitNumericConversion (this, type) == null) 
+                               return null;
 
-                       if (c == null)
-                               Convert.Error_CannotImplicitConversion (loc, Type, TypeManager.uint64_type);
+                       bool fail;                      
+                       object constant_value = TypeManager.ChangeType (GetValue (), type, out fail);
+                       if (fail){
+                               //
+                               // We should always catch the error before this is ever
+                               // reached, by calling Convert.ImplicitStandardConversionExists
+                               //
+                               throw new InternalErrorException ("Missing constant conversion between `{0}' and `{1}'",
+                                 TypeManager.CSharpName (Type), TypeManager.CSharpName (type));
+                       }
 
-                       return c;
+                       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);
                }
+               /// <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);
 
-               public LongConstant ToLong (Location loc)
-               {
-                       LongConstant c = ConvertToLong ();
-
-                       if (c == null)
-                               Convert.Error_CannotImplicitConversion (loc, Type, TypeManager.int64_type);
-
-                       return c;
-               }
-               
-               public UIntConstant ToUInt (Location loc)
+               /// <summary>
+               ///   Attempts to do a compile-time folding of a constant cast.
+               /// </summary>
+               public Constant TryReduce (EmitContext ec, Type target_type, Location loc)
                {
-                       UIntConstant c = ConvertToUInt ();
-
-                       if (c == null)
-                               Convert.Error_CannotImplicitConversion (loc, Type, TypeManager.uint32_type);
-
-                       return c;
+                       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));
+                               return null;
+                       }
                }
 
-               public IntConstant ToInt (Location loc)
+               Constant TryReduce (EmitContext ec, Type target_type)
                {
-                       IntConstant c = ConvertToInt ();
-
-                       if (c == null)
-                               Convert.Error_CannotImplicitConversion (loc, Type, TypeManager.int32_type);
-
-                       return c;
-               }
+                       if (Type == target_type)
+                               return this;
 
-               public DecimalConstant ToDecimal (Location loc)
-               {
-                       DecimalConstant c = ConvertToDecimal ();
+                       if (TypeManager.IsEnumType (target_type)) {
+                               Constant c = TryReduce (ec, TypeManager.EnumToUnderlying (target_type));
+                               if (c == null)
+                                       return null;
 
-                       if (c == null)
-                               Convert.Error_CannotConvertType (loc, Type, TypeManager.decimal_type);
+                               return new EnumConstant (c, target_type);
+                       }
 
-                       return c;
+                       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;
 
-               public virtual FloatConstant ConvertToFloat ()
-               {
-                       return null;
+                       return Type == TypeManager.null_type;
                }
 
-               public virtual ULongConstant ConvertToULong ()
-               {
-                       return null;
-               }
-
-               public virtual LongConstant ConvertToLong ()
-               {
-                       return null;
-               }
-
-               public virtual UIntConstant ConvertToUInt ()
-               {
-                       return null;
+               public abstract bool IsDefaultValue {
+                       get;
                }
 
-               public virtual IntConstant ConvertToInt ()
-               {
-                       return null;
-               }
-               
                public abstract bool IsNegative {
                        get;
                }
@@ -173,12 +216,38 @@ namespace Mono.CSharp {
                public virtual bool IsZeroInteger {
                        get { return false; }
                }
+
+               protected override void CloneTo (CloneContext clonectx, Expression target)
+               {
+                       // CloneTo: Nothing, we do not keep any state on this expression
+               }
+       }
+
+       public abstract class IntegralConstant : Constant {
+               protected IntegralConstant (Location loc) :
+                       base (loc)
+               {
+               }
+
+               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));
+                       }
+               }
        }
        
        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;
@@ -204,7 +273,18 @@ namespace Mono.CSharp {
                        else
                                ec.ig.Emit (OpCodes.Ldc_I4_0);
                }
+
+               public override Constant Increment ()
+               {
+                       throw new NotSupportedException ();
+               }
        
+               public override bool IsDefaultValue {
+                       get {
+                               return !Value;
+                       }
+               }
+
                public override bool IsNegative {
                        get {
                                return false;
@@ -214,12 +294,19 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == false; }
                }
+
+               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)
+               public ByteConstant (byte v, Location loc):
+                       base (loc)
                {
                        type = TypeManager.byte_type;
                        eclass = ExprClass.Value;
@@ -241,34 +328,15 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value);
-               }
-
-               public override FloatConstant ConvertToFloat ()
+               public override Constant Increment ()
                {
-                       return new FloatConstant (Value);
+                       return new ByteConstant (checked ((byte)(Value + 1)), loc);
                }
 
-               public override ULongConstant ConvertToULong ()
-               {
-                       return new ULongConstant (Value);
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return new LongConstant (Value);
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return new UIntConstant (Value);
-               }
-
-               public override IntConstant ConvertToInt ()
-               {
-                       return new IntConstant (Value);
+               public override bool IsDefaultValue {
+                       get {
+                               return Value == 0;
+                       }
                }
 
                public override bool IsNegative {
@@ -280,12 +348,47 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
+
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+               {
+                       if (target_type == TypeManager.sbyte_type) {
+                               if (in_checked_context){
+                                       if (Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               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;
@@ -336,36 +439,17 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value);
-               }
-
-               public override FloatConstant ConvertToFloat ()
+               public override Constant Increment ()
                {
-                       return new FloatConstant (Value);
+                       return new CharConstant (checked ((char)(Value + 1)), loc);
                }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       return new ULongConstant (Value);
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return new LongConstant (Value);
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return new UIntConstant (Value);
+               
+               public override bool IsDefaultValue {
+                       get {
+                               return Value == 0;
+                       }
                }
 
-               public override IntConstant ConvertToInt ()
-               {
-                       return new IntConstant (Value);
-               }
-               
                public override bool IsNegative {
                        get {
                                return false;
@@ -375,12 +459,55 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == '\0'; }
                }
+
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_type) {
+                               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) {
+                               if (in_checked_context){
+                                       if (Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new SByteConstant ((sbyte) Value, Location);
+                       }
+                       if (target_type == TypeManager.short_type) {
+                               if (in_checked_context){
+                                       if (Value > Int16.MaxValue)
+                                               throw new OverflowException ();
+                               }                                       
+                               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;
@@ -402,37 +529,15 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value);
-               }
-
-               public override FloatConstant ConvertToFloat ()
+               public override Constant Increment ()
                {
-                       return new FloatConstant (Value);
+                   return new SByteConstant (checked((sbyte)(Value + 1)), loc);
                }
 
-               public override ULongConstant ConvertToULong ()
-               {
-                       if (Value >= 0)
-                               return new ULongConstant ((ulong) Value);
-                       
-                       return null;
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return new LongConstant (Value);
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return null;
-               }
-
-               public override IntConstant ConvertToInt ()
-               {
-                       return new IntConstant (Value);
+               public override bool IsDefaultValue {
+                       get {
+                               return Value == 0;
+                       }
                }
 
                public override bool IsNegative {
@@ -444,12 +549,55 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
+
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_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) {
+                               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) {
+                               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) {
+                               if (in_checked_context && Value < 0)
+                                       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.double_type)
+                               return new DoubleConstant ((double) Value, Location);
+                       if (target_type == TypeManager.char_type) {
+                               if (in_checked_context && Value < 0)
+                                       throw new OverflowException ();
+                               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;
@@ -471,34 +619,15 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
+               public override Constant Increment ()
                {
-                       return new DoubleConstant (Value);
+                       return new ShortConstant (checked((short)(Value + 1)), loc);
                }
 
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return new FloatConstant (Value);
-               }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       return null;
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return new LongConstant (Value);
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return null;
-               }
-
-               public override IntConstant ConvertToInt ()
-               {
-                       return new IntConstant (Value);
+               public override bool IsDefaultValue {
+                       get {
+                               return Value == 0;
+                       }
                }
                
                public override bool IsZeroInteger {
@@ -510,12 +639,67 @@ namespace Mono.CSharp {
                                return Value < 0;
                        }
                }
+
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_type) {
+                               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) {
+                               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) {
+                               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) {
+                               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) {
+                               if (in_checked_context && Value < 0)
+                                       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.double_type)
+                               return new DoubleConstant ((double) Value, Location);
+                       if (target_type == TypeManager.char_type) {
+                               if (in_checked_context){
+                                       if (Value < Char.MinValue)
+                                               throw new OverflowException ();
+                               }
+                               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;
@@ -536,37 +720,18 @@ namespace Mono.CSharp {
                {
                        return Value;
                }
-
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value);
-               }
-
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return new FloatConstant (Value);
-               }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       return new ULongConstant (Value);
-               }
-
-               public override LongConstant ConvertToLong ()
+       
+               public override Constant Increment ()
                {
-                       return new LongConstant (Value);
+                       return new UShortConstant (checked((ushort)(Value + 1)), loc);
                }
 
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return new UIntConstant (Value);
+               public override bool IsDefaultValue {
+                       get {
+                               return Value == 0;
+                       }
                }
 
-               public override IntConstant ConvertToInt ()
-               {
-                       return new IntConstant (Value);
-               }
-       
                public override bool IsNegative {
                        get {
                                return false;
@@ -576,12 +741,61 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
+
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_type) {
+                               if (in_checked_context){
+                                       if (Value > Byte.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new ByteConstant ((byte) Value, Location);
+                       }
+                       if (target_type == TypeManager.sbyte_type) {
+                               if (in_checked_context){
+                                       if (Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new SByteConstant ((sbyte) Value, Location);
+                       }
+                       if (target_type == TypeManager.short_type) {
+                               if (in_checked_context){
+                                       if (Value > Int16.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               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) {
+                               if (in_checked_context){
+                                       if (Value > Char.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               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;
@@ -655,62 +869,150 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DecimalConstant ConvertToDecimal()
+               public override Constant Increment ()
                {
-                       return new DecimalConstant (Value);
+                       return new IntConstant (checked(Value + 1), loc);
                }
 
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value);
+               public override bool IsDefaultValue {
+                       get {
+                               return Value == 0;
+                       }
+               }
+               
+               public override bool IsNegative {
+                       get {
+                               return Value < 0;
+                       }
                }
 
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return new FloatConstant (Value);
+               public override bool IsZeroInteger {
+                       get { return Value == 0; }
                }
 
-               public override ULongConstant ConvertToULong ()
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
-                       if (Value < 0)
-                               return null;
+                       if (target_type == TypeManager.byte_type) {
+                               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) {
+                               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) {
+                               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) {
+                               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) {
+                               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) {
+                               if (in_checked_context && Value < 0)
+                                       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.double_type)
+                               return new DoubleConstant ((double) Value, Location);
+                       if (target_type == TypeManager.char_type) {
+                               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)
+                               return new DecimalConstant ((decimal) Value, Location);
 
-                       return new ULongConstant ((ulong) Value);
+                       return null;
                }
 
-               public override LongConstant ConvertToLong ()
+               public override Constant ConvertImplicitly (Type type)
                {
-                       return new LongConstant (Value);
-               }
+                       if (this.type == type)
+                               return this;
 
-               public override UIntConstant ConvertToUInt ()
-               {
-                       if (Value < 0)
-                               return null;
+                       Constant c = TryImplicitIntConversion (type);
+                       if (c != null)
+                               return c;
 
-                       return new UIntConstant ((uint) Value);
+                       return base.ConvertImplicitly (type);
                }
 
-               public override IntConstant ConvertToInt ()
-               {
-                       return this;
-               }
-               
-               public override bool IsNegative {
-                       get {
-                               return Value < 0;
-                       }
-               }
+               /// <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);
 
-               public override bool IsZeroInteger {
-                       get { return Value == 0; }
+                       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;
@@ -732,36 +1034,17 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
+               public override Constant Increment ()
                {
-                       return new DoubleConstant (Value);
+                       return new UIntConstant (checked(Value + 1), loc);
                }
-
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return new FloatConstant (Value);
-               }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       return new ULongConstant (Value);
-               }
-
-               public override LongConstant ConvertToLong ()
-               {
-                       return new LongConstant (Value);
-               }
-
-               public override UIntConstant ConvertToUInt ()
-               {
-                       return this;
+       
+               public override bool IsDefaultValue {
+                       get {
+                               return Value == 0;
+                       }
                }
 
-               public override IntConstant ConvertToInt ()
-               {
-                       return null;
-               }
-               
                public override bool IsNegative {
                        get {
                                return false;
@@ -771,12 +1054,72 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
+
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_type) {
+                               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) {
+                               if (in_checked_context){
+                                       if (Value > SByte.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new SByteConstant ((sbyte) Value, Location);
+                       }
+                       if (target_type == TypeManager.short_type) {
+                               if (in_checked_context){
+                                       if (Value > Int16.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new ShortConstant ((short) Value, Location);
+                       }
+                       if (target_type == TypeManager.ushort_type) {
+                               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) {
+                               if (in_checked_context){
+                                       if (Value > Int32.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               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) {
+                               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)
+                               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;
@@ -785,19 +1128,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 ()
@@ -810,54 +1151,108 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
+               public override Constant Increment ()
                {
-                       return new DoubleConstant (Value);
+                       return new LongConstant (checked(Value + 1), loc);
                }
-
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return new FloatConstant (Value);
+               
+               public override bool IsDefaultValue {
+                       get {
+                               return Value == 0;
+                       }
                }
 
-               public override ULongConstant ConvertToULong ()
-               {
-                       if (Value < 0)
-                               return null;
-                       
-                       return new ULongConstant ((ulong) Value);
+               public override bool IsNegative {
+                       get {
+                               return Value < 0;
+                       }
                }
 
-               public override LongConstant ConvertToLong ()
-               {
-                       return this;
+               public override bool IsZeroInteger {
+                       get { return Value == 0; }
                }
 
-               public override UIntConstant ConvertToUInt ()
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
+                       if (target_type == TypeManager.byte_type) {
+                               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) {
+                               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) {
+                               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) {
+                               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) {
+                               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) {
+                               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) {
+                               if (in_checked_context && Value < 0)
+                                       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.double_type)
+                               return new DoubleConstant ((double) Value, Location);
+                       if (target_type == TypeManager.char_type) {
+                               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)
+                               return new DecimalConstant ((decimal) Value, Location);
+
                        return null;
                }
 
-               public override IntConstant ConvertToInt ()
+               public override Constant ConvertImplicitly (Type type)
                {
-                       return null;
-               }
-               
-               public override bool IsNegative {
-                       get {
-                               return Value < 0;
+                       if (Value >= 0 && type == TypeManager.uint64_type) {
+                               return new ULongConstant ((ulong) Value, loc);
                        }
-               }
 
-               public override bool IsZeroInteger {
-                       get { return Value == 0; }
+                       return base.ConvertImplicitly (type);
                }
        }
 
-       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;
@@ -881,34 +1276,15 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
-               {
-                       return new DoubleConstant (Value);
-               }
-
-               public override FloatConstant ConvertToFloat ()
+               public override Constant Increment ()
                {
-                       return new FloatConstant (Value);
+                       return new ULongConstant (checked(Value + 1), 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 bool IsDefaultValue {
+                       get {
+                               return Value == 0;
+                       }
                }
 
                public override bool IsNegative {
@@ -920,12 +1296,66 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
+
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+               {
+                       if (target_type == TypeManager.byte_type) {
+                               if (in_checked_context && Value > Byte.MaxValue)
+                                       throw new OverflowException ();
+                               return new ByteConstant ((byte) Value, Location);
+                       }
+                       if (target_type == TypeManager.sbyte_type) {
+                               if (in_checked_context && Value > ((ulong) SByte.MaxValue))
+                                       throw new OverflowException ();
+                               return new SByteConstant ((sbyte) Value, Location);
+                       }
+                       if (target_type == TypeManager.short_type) {
+                               if (in_checked_context && Value > ((ulong) Int16.MaxValue))
+                                       throw new OverflowException ();
+                               return new ShortConstant ((short) Value, Location);
+                       }
+                       if (target_type == TypeManager.ushort_type) {
+                               if (in_checked_context && Value > UInt16.MaxValue)
+                                       throw new OverflowException ();
+                               return new UShortConstant ((ushort) Value, Location);
+                       }
+                       if (target_type == TypeManager.int32_type) {
+                               if (in_checked_context && Value > UInt32.MaxValue)
+                                       throw new OverflowException ();
+                               return new IntConstant ((int) Value, Location);
+                       }
+                       if (target_type == TypeManager.uint32_type) {
+                               if  (in_checked_context && Value > UInt32.MaxValue)
+                                       throw new OverflowException ();
+                               return new UIntConstant ((uint) Value, Location);
+                       }
+                       if (target_type == TypeManager.int64_type) {
+                               if (in_checked_context && Value > Int64.MaxValue)
+                                       throw new OverflowException ();
+                               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) {
+                               if (in_checked_context && Value > Char.MaxValue)
+                                       throw new OverflowException ();
+                               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;
@@ -947,42 +1377,103 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
+               public override Constant Increment ()
                {
-                       return new DoubleConstant (Value);
+                       return new FloatConstant (checked(Value + 1), loc);
                }
 
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return this;
+               public override bool IsDefaultValue {
+                       get {
+                               return Value == 0;
+                       }
                }
 
-               public override LongConstant ConvertToLong ()
-               {
-                       return null;
+               public override bool IsNegative {
+                       get {
+                               return Value < 0;
+                       }
                }
 
-               public override UIntConstant ConvertToUInt ()
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
-                       return null;
-               }
+                       if (target_type == TypeManager.byte_type) {
+                               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) {
+                               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) {
+                               if (in_checked_context){
+                                       if (Value < short.MinValue || Value > short.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new ShortConstant ((short) Value, Location);
+                       }
+                       if (target_type == TypeManager.ushort_type) {
+                               if (in_checked_context){
+                                       if (Value < ushort.MinValue || Value > ushort.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new UShortConstant ((ushort) Value, Location);
+                       }
+                       if (target_type == TypeManager.int32_type) {
+                               if (in_checked_context){
+                                       if (Value < int.MinValue || Value > int.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new IntConstant ((int) Value, Location);
+                       }
+                       if (target_type == TypeManager.uint32_type) {
+                               if (in_checked_context){
+                                       if (Value < uint.MinValue || Value > uint.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new UIntConstant ((uint) Value, Location);
+                       }
+                       if (target_type == TypeManager.int64_type) {
+                               if (in_checked_context){
+                                       if (Value < long.MinValue || Value > long.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new LongConstant ((long) Value, Location);
+                       }
+                       if (target_type == TypeManager.uint64_type) {
+                               if (in_checked_context){
+                                       if (Value < ulong.MinValue || Value > ulong.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new ULongConstant ((ulong) Value, Location);
+                       }
+                       if (target_type == TypeManager.double_type)
+                               return new DoubleConstant ((double) Value, Location);
+                       if (target_type == TypeManager.char_type) {
+                               if (in_checked_context){
+                                       if (Value < (float) char.MinValue || Value > (float) char.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new CharConstant ((char) Value, Location);
+                       }
+                       if (target_type == TypeManager.decimal_type)
+                               return new DecimalConstant ((decimal) Value, Location);
 
-               public override IntConstant ConvertToInt ()
-               {
                        return null;
                }
 
-               public override bool IsNegative {
-                       get {
-                               return Value < 0;
-                       }
-               }
        }
 
        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;
@@ -1004,47 +1495,103 @@ namespace Mono.CSharp {
                        return Value;
                }
 
-               public override DoubleConstant ConvertToDouble ()
+               public override Constant Increment ()
                {
-                       return this;
+                       return new DoubleConstant (checked(Value + 1), loc);
                }
 
-               public override FloatConstant ConvertToFloat ()
-               {
-                       return null;
-               }
-
-               public override ULongConstant ConvertToULong ()
-               {
-                       return null;
+               public override bool IsDefaultValue {
+                       get {
+                               return Value == 0;
+                       }
                }
 
-               public override LongConstant ConvertToLong ()
-               {
-                       return null;
+               public override bool IsNegative {
+                       get {
+                               return Value < 0;
+                       }
                }
 
-               public override UIntConstant ConvertToUInt ()
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
                {
-                       return null;
-               }
+                       if (target_type == TypeManager.byte_type) {
+                               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) {
+                               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) {
+                               if (in_checked_context){
+                                       if (Value < short.MinValue || Value > short.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new ShortConstant ((short) Value, Location);
+                       }
+                       if (target_type == TypeManager.ushort_type) {
+                               if (in_checked_context){
+                                       if (Value < ushort.MinValue || Value > ushort.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new UShortConstant ((ushort) Value, Location);
+                       }
+                       if (target_type == TypeManager.int32_type) {
+                               if (in_checked_context){
+                                       if (Value < int.MinValue || Value > int.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new IntConstant ((int) Value, Location);
+                       }
+                       if (target_type == TypeManager.uint32_type) {
+                               if (in_checked_context){
+                                       if (Value < uint.MinValue || Value > uint.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new UIntConstant ((uint) Value, Location);
+                       }
+                       if (target_type == TypeManager.int64_type) {
+                               if (in_checked_context){
+                                       if (Value < long.MinValue || Value > long.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new LongConstant ((long) Value, Location);
+                       }
+                       if (target_type == TypeManager.uint64_type) {
+                               if (in_checked_context){
+                                       if (Value < ulong.MinValue || Value > ulong.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new ULongConstant ((ulong) Value, Location);
+                       }
+                       if (target_type == TypeManager.float_type)
+                               return new FloatConstant ((float) Value, Location);
+                       if (target_type == TypeManager.char_type) {
+                               if (in_checked_context){
+                                       if (Value < (double) char.MinValue || Value > (double) char.MaxValue)
+                                               throw new OverflowException ();
+                               }
+                               return new CharConstant ((char) Value, Location);
+                       }
+                       if (target_type == TypeManager.decimal_type)
+                               return new DecimalConstant ((decimal) Value, Location);
 
-               public override IntConstant ConvertToInt ()
-               {
                        return null;
                }
 
-               public override bool IsNegative {
-                       get {
-                               return Value < 0;
-                       }
-               }
        }
 
        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;
@@ -1065,14 +1612,16 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
 
-                       if (Value <= int.MaxValue && Value >= int.MinValue)
+                       int [] words = Decimal.GetBits (Value);
+                       int power = (words [3] >> 16) & 0xff;
+
+                       if (power == 0 && Value <= int.MaxValue && Value >= int.MinValue)
                        {
                                IntConstant.EmitInt (ig, (int)Value);
                                ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_int_arg);
                                return;
                        }
 
-                       int [] words = Decimal.GetBits (Value);
                        
                        //
                        // FIXME: we could optimize this, and call a better 
@@ -1087,22 +1636,63 @@ namespace Mono.CSharp {
                        IntConstant.EmitInt (ig, words [3] >> 31);
 
                        // power
-                       IntConstant.EmitInt (ig, (words [3] >> 16) & 0xff);
+                       IntConstant.EmitInt (ig, power);
 
                        ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_five_args);
                }
 
+               public override Constant Increment ()
+               {
+                       return new DecimalConstant (checked (Value + 1), loc);
+               }
+
+               public override bool IsDefaultValue {
+                       get {
+                               return Value == 0;
+                       }
+               }
+
                public override bool IsNegative {
                        get {
                                return Value < 0;
                        }
                }
+
+               public override Constant ConvertExplicitly (bool in_checked_context, 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;
@@ -1128,11 +1718,27 @@ namespace Mono.CSharp {
                                ec.ig.Emit (OpCodes.Ldstr, Value);
                }
 
+               public override Constant Increment ()
+               {
+                       throw new NotSupportedException ();
+               }
+
+               public override bool IsDefaultValue {
+                       get {
+                               return Value == null;
+                       }
+               }
+
                public override bool IsNegative {
                        get {
                                return false;
                        }
                }
+
+               public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
+               {
+                       return null;
+               }
        }
 
 }