2006-08-13 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / mbas / constant.cs
index 4030080a0b273504b59c05e496bd60cad8b91fb6..d42d674c74f5ef4fc83f3b648c56b70c24c38253 100644 (file)
@@ -8,7 +8,7 @@
 //
 //
 
-namespace Mono.CSharp {
+namespace Mono.MonoBASIC {
 
        using System;
        using System.Reflection.Emit;
@@ -117,6 +117,22 @@ namespace Mono.CSharp {
                        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.CSharp {
                {
                        return new FloatConstant (Value);
                }
+               
+               public override DecimalConstant ConvertToDecimal ()
+               {
+                       return new DecimalConstant (Value);
+               }
 
                public override ULongConstant ConvertToULong ()
                {
@@ -680,12 +701,7 @@ namespace Mono.CSharp {
 
                static public void EmitLong (ILGenerator ig, long l)
                {
-                       if ((l >> 32) == 0){
-                               IntLiteral.EmitInt (ig, unchecked ((int) l));
-                               ig.Emit (OpCodes.Conv_U8);
-                       } else {
-                               ig.Emit (OpCodes.Ldc_I8, l);
-                       }
+                       ig.Emit (OpCodes.Ldc_I8, l);
                }
 
                public override string AsString ()
@@ -873,7 +889,12 @@ namespace Mono.CSharp {
 
                public override FloatConstant ConvertToFloat ()
                {
-                       return null;
+                       return new FloatConstant ((float) Value);
+               }
+
+               public override DecimalConstant ConvertToDecimal ()
+               {
+                       return new DecimalConstant ((decimal) Value);
                }
 
                public override ULongConstant ConvertToULong ()
@@ -919,27 +940,39 @@ namespace Mono.CSharp {
 
                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 {
@@ -969,6 +1002,31 @@ namespace Mono.CSharp {
                }
        }
 
-}
+       public class DateConstant : Constant {
+               public readonly DateTime Value;
 
+               public DateConstant (DateTime s)
+               {
+                       type = TypeManager.date_type;
+                       eclass = ExprClass.Value;
+                       Value = s;
+               }
 
+               override public string AsString ()
+               {
+                       return "#" + Value.ToString() + "#";
+               }
+
+               public override object GetValue ()
+               {
+                       return Value;
+               }
+               
+               public override void Emit (EmitContext ec)
+               {
+                       ec.ig.Emit (OpCodes.Ldc_I8, Value.Ticks);
+                       ec.ig.Emit (OpCodes.Newobj, TypeManager.void_datetime_ctor_ticks_arg);
+               }
+       }
+
+}