**** Merged r36987 from MCS ****
[mono.git] / mcs / gmcs / constant.cs
index bb73f8e4b06093f48e9510152148261c5e8c57d0..73de93f06f273af931fca1d08ccc103eaea55966 100755 (executable)
@@ -116,6 +116,21 @@ namespace Mono.CSharp {
 
                        return c;
                }
+
+               public DecimalConstant ToDecimal (Location loc)
+               {
+                       DecimalConstant c = ConvertToDecimal ();
+
+                       if (c == null)
+                               Convert.Error_CannotConvertType (loc, Type, TypeManager.decimal_type);
+
+                       return c;
+               }
+
+               public virtual DecimalConstant ConvertToDecimal ()
+               {
+                       return null;
+               }
                
                public virtual DoubleConstant ConvertToDouble ()
                {
@@ -147,6 +162,10 @@ namespace Mono.CSharp {
                        return null;
                }
                
+               public abstract bool IsNegative {
+                       get;
+               }
+
                //
                // Returns true iff 1) the stack type of this is one of Object, 
                // int32, int64 and 2) this == 0 or this == null.
@@ -186,6 +205,12 @@ namespace Mono.CSharp {
                                ec.ig.Emit (OpCodes.Ldc_I4_0);
                }
                
+               public override bool IsNegative {
+                       get {
+                               return false;
+                       }
+               }
+       
                public override bool IsZeroInteger {
                        get { return Value == false; }
                }
@@ -246,6 +271,12 @@ namespace Mono.CSharp {
                        return new IntConstant (Value);
                }
                
+               public override bool IsNegative {
+                       get {
+                               return false;
+                       }
+               }
+
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
@@ -335,6 +366,12 @@ namespace Mono.CSharp {
                        return new IntConstant (Value);
                }
                
+               public override bool IsNegative {
+                       get {
+                               return false;
+                       }
+               }
+
                public override bool IsZeroInteger {
                        get { return Value == '\0'; }
                }
@@ -398,6 +435,12 @@ namespace Mono.CSharp {
                        return new IntConstant (Value);
                }
                
+               public override bool IsNegative {
+                       get {
+                               return Value < 0;
+                       }
+               }
+               
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
@@ -461,6 +504,12 @@ namespace Mono.CSharp {
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
+
+               public override bool IsNegative {
+                       get {
+                               return Value < 0;
+                       }
+               }
        }
 
        public class UShortConstant : Constant {
@@ -518,6 +567,12 @@ namespace Mono.CSharp {
                        return new IntConstant (Value);
                }
                
+               public override bool IsNegative {
+                       get {
+                               return false;
+                       }
+               }
+       
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
@@ -600,6 +655,11 @@ namespace Mono.CSharp {
                        return Value;
                }
 
+               public override DecimalConstant ConvertToDecimal()
+               {
+                       return new DecimalConstant (Value);
+               }
+
                public override DoubleConstant ConvertToDouble ()
                {
                        return new DoubleConstant (Value);
@@ -636,6 +696,12 @@ namespace Mono.CSharp {
                        return this;
                }
                
+               public override bool IsNegative {
+                       get {
+                               return Value < 0;
+                       }
+               }
+
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
@@ -696,6 +762,12 @@ namespace Mono.CSharp {
                        return null;
                }
                
+               public override bool IsNegative {
+                       get {
+                               return false;
+                       }
+               }
+
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
@@ -771,6 +843,12 @@ namespace Mono.CSharp {
                        return null;
                }
                
+               public override bool IsNegative {
+                       get {
+                               return Value < 0;
+                       }
+               }
+
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
@@ -833,6 +911,12 @@ namespace Mono.CSharp {
                        return null;
                }
                
+               public override bool IsNegative {
+                       get {
+                               return false;
+                       }
+               }
+
                public override bool IsZeroInteger {
                        get { return Value == 0; }
                }
@@ -887,6 +971,12 @@ namespace Mono.CSharp {
                {
                        return null;
                }
+
+               public override bool IsNegative {
+                       get {
+                               return Value < 0;
+                       }
+               }
        }
 
        public class DoubleConstant : Constant {
@@ -943,6 +1033,12 @@ namespace Mono.CSharp {
                {
                        return null;
                }
+
+               public override bool IsNegative {
+                       get {
+                               return Value < 0;
+                       }
+               }
        }
 
        public class DecimalConstant : Constant {
@@ -967,15 +1063,24 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
+                       ILGenerator ig = ec.ig;
+
                        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;
+                       }
+
                        
                        //
                        // FIXME: we could optimize this, and call a better 
                        // constructor
                        //
 
-                       ILGenerator ig = ec.ig;
-                       
                        IntConstant.EmitInt (ig, words [0]);
                        IntConstant.EmitInt (ig, words [1]);
                        IntConstant.EmitInt (ig, words [2]);
@@ -984,10 +1089,16 @@ 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 bool IsNegative {
+                       get {
+                               return Value < 0;
+                       }
+               }
        }
 
        public class StringConstant : Constant {
@@ -1018,6 +1129,12 @@ namespace Mono.CSharp {
                        else
                                ec.ig.Emit (OpCodes.Ldstr, Value);
                }
+
+               public override bool IsNegative {
+                       get {
+                               return false;
+                       }
+               }
        }
 
 }