2006-08-13 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / mbas / constant.cs
index 9d3ea886cd6456d680a19fd35026d517b0dc1c1f..d42d674c74f5ef4fc83f3b648c56b70c24c38253 100644 (file)
@@ -117,6 +117,22 @@ namespace Mono.MonoBASIC {
                        return c;
                }
                
+               public DecimalConstant ToDecimal (Location loc)
+                {
+                        DecimalConstant c = ConvertToDecimal ();
+
+                        if (c == null)
+                                Error_CannotConvertImplicit (loc, Type, TypeManager.decimal_type);
+
+                        return c;
+                }
+
+                public virtual DecimalConstant ConvertToDecimal ()
+                {
+                        return null;
+                }
+
+               
                public virtual DoubleConstant ConvertToDouble ()
                {
                        return null;
@@ -577,6 +593,11 @@ namespace Mono.MonoBASIC {
                {
                        return new FloatConstant (Value);
                }
+               
+               public override DecimalConstant ConvertToDecimal ()
+               {
+                       return new DecimalConstant (Value);
+               }
 
                public override ULongConstant ConvertToULong ()
                {
@@ -868,7 +889,12 @@ namespace Mono.MonoBASIC {
 
                public override FloatConstant ConvertToFloat ()
                {
-                       return null;
+                       return new FloatConstant ((float) Value);
+               }
+
+               public override DecimalConstant ConvertToDecimal ()
+               {
+                       return new DecimalConstant ((decimal) Value);
                }
 
                public override ULongConstant ConvertToULong ()
@@ -914,27 +940,39 @@ namespace Mono.MonoBASIC {
 
                public override void Emit (EmitContext ec)
                {
-                       int [] words = Decimal.GetBits (Value);
-                       
-                       //
-                       // FIXME: we could optimize this, and call a better 
-                       // constructor
-                       //
+                        ILGenerator ig = ec.ig;
 
-                       ILGenerator ig = ec.ig;
                        
-                       IntConstant.EmitInt (ig, words [0]);
+                        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
+                        //
+
+                        IntConstant.EmitInt (ig, words [0]);
                        IntConstant.EmitInt (ig, words [1]);
-                       IntConstant.EmitInt (ig, words [2]);
+                        IntConstant.EmitInt (ig, words [2]);
 
-                       // sign
-                       IntConstant.EmitInt (ig, words [3] >> 31);
+                        // sign
+                        IntConstant.EmitInt (ig, words [3] >> 31);
 
-                       // power
-                       IntConstant.EmitInt (ig, (words [3] >> 16) & 0xff);
+                        // power
+                        IntConstant.EmitInt (ig, power);
+
+                        ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_five_args);
+                }
 
-                       ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_five_args);
-               }
        }
 
        public class StringConstant : Constant {