2002-06-15 Rafael Teixeira <rafaelteixeirabr@hotmail.com>
[mono.git] / mcs / mbas / constant.cs
index f7dbefdba93a07b27d49d99f5ac516303234264e..2bfdfc031f5a8f6eb889daeffcb7a677dbca3ffa 100644 (file)
-//\r
-// constant.cs: Constants.\r
-//\r
-// Author:\r
-//   Miguel de Icaza (miguel@ximian.com)\r
-//\r
-// (C) 2001 Ximian, Inc.\r
-//\r
-//\r
-\r
-namespace Mono.CSharp {\r
-\r
-       using System;\r
-       using System.Reflection.Emit;\r
-\r
-       /// <summary>\r
-       ///   Base class for constants and literals.\r
-       /// </summary>\r
-       public abstract class Constant : Expression {\r
-               /// <remarks>\r
-               ///   This is different from ToString in that ToString\r
-               ///   is supposed to be there for debugging purposes,\r
-               ///   and is not guaranteed to be useful for anything else,\r
-               ///   AsString() will provide something that can be used\r
-               ///   for round-tripping C# code.  Maybe it can be used\r
-               ///   for IL assembly as well.\r
-               /// </remarks>\r
-               public abstract string AsString ();\r
-\r
-               override public string ToString ()\r
-               {\r
-                       return this.GetType ().Name + " (" + AsString () + ")";\r
-               }\r
-\r
-               /// <summary>\r
-               ///  This is used to obtain the actual value of the literal\r
-               ///  cast into an object.\r
-               /// </summary>\r
-               public abstract object GetValue ();\r
-\r
-               /// <summary>\r
-               ///   Constants are always born in a fully resolved state\r
-               /// </summary>\r
-               public override Expression DoResolve (EmitContext ec)\r
-               {\r
-                       return this;\r
-               }\r
-\r
-               //\r
-               // The various ToXXXX conversion functions are used by the constant\r
-               // folding evaluator.   A null value is returned if the conversion is\r
-               // not possible.   \r
-               //\r
-               // Note: not all the patterns for catching `implicit_conv' are the same.\r
-               // some implicit conversions can never be performed between two types\r
-               // even if the conversion would be lossless (for example short to uint),\r
-               // but some conversions are explicitly permitted by the standard provided\r
-               // that there will be no loss of information (for example, int to uint).\r
-               //\r
-               public DoubleConstant ToDouble (Location loc)\r
-               {\r
-                       DoubleConstant c = ConvertToDouble ();\r
-\r
-                       if (c == null)\r
-                               Error_CannotConvertImplicit (loc, Type, TypeManager.double_type);\r
-\r
-                       return c;\r
-               }\r
-\r
-               public FloatConstant ToFloat (Location loc)\r
-               {\r
-                       FloatConstant c = ConvertToFloat ();\r
-\r
-                       if (c == null)\r
-                               Error_CannotConvertImplicit (loc, Type, TypeManager.float_type);\r
-\r
-                       return c;\r
-               }\r
-\r
-               public ULongConstant ToULong (Location loc)\r
-               {\r
-                       ULongConstant c = ConvertToULong ();\r
-\r
-                       if (c == null)\r
-                               Error_CannotConvertImplicit (loc, Type, TypeManager.uint64_type);\r
-\r
-                       return c;\r
-               }\r
-\r
-               public LongConstant ToLong (Location loc)\r
-               {\r
-                       LongConstant c = ConvertToLong ();\r
-\r
-                       if (c == null)\r
-                               Error_CannotConvertImplicit (loc, Type, TypeManager.int64_type);\r
-\r
-                       return c;\r
-               }\r
-               \r
-               public UIntConstant ToUInt (Location loc)\r
-               {\r
-                       UIntConstant c = ConvertToUInt ();\r
-\r
-                       if (c == null)\r
-                               Error_CannotConvertImplicit (loc, Type, TypeManager.uint32_type);\r
-\r
-                       return c;\r
-               }\r
-\r
-               public IntConstant ToInt (Location loc)\r
-               {\r
-                       IntConstant c = ConvertToInt ();\r
-\r
-                       if (c == null)\r
-                               Error_CannotConvertImplicit (loc, Type, TypeManager.int32_type);\r
-\r
-                       return c;\r
-               }\r
-               \r
-               public virtual DoubleConstant ConvertToDouble ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public virtual FloatConstant ConvertToFloat ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public virtual ULongConstant ConvertToULong ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public virtual LongConstant ConvertToLong ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public virtual UIntConstant ConvertToUInt ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public virtual IntConstant ConvertToInt ()\r
-               {\r
-                       return null;\r
-               }\r
-       }\r
-       \r
-       public class BoolConstant : Constant {\r
-               public readonly bool Value;\r
-               \r
-               public BoolConstant (bool val)\r
-               {\r
-                       type = TypeManager.bool_type;\r
-                       eclass = ExprClass.Value;\r
-\r
-                       Value = val;\r
-               }\r
-\r
-               override public string AsString ()\r
-               {\r
-                       return Value ? "true" : "false";\r
-               }\r
-\r
-               public override object GetValue ()\r
-               {\r
-                       return (object) Value;\r
-               }\r
-                               \r
-               \r
-               public override void Emit (EmitContext ec)\r
-               {\r
-                       if (Value)\r
-                               ec.ig.Emit (OpCodes.Ldc_I4_1);\r
-                       else\r
-                               ec.ig.Emit (OpCodes.Ldc_I4_0);\r
-               }\r
-       }\r
-\r
-       public class ByteConstant : Constant {\r
-               public readonly byte Value;\r
-\r
-               public ByteConstant (byte v)\r
-               {\r
-                       type = TypeManager.byte_type;\r
-                       eclass = ExprClass.Value;\r
-                       Value = v;\r
-               }\r
-\r
-               public override void Emit (EmitContext ec)\r
-               {\r
-                       IntLiteral.EmitInt (ec.ig, Value);\r
-               }\r
-\r
-               public override string AsString ()\r
-               {\r
-                       return Value.ToString ();\r
-               }\r
-\r
-               public override object GetValue ()\r
-               {\r
-                       return Value;\r
-               }\r
-\r
-               public override DoubleConstant ConvertToDouble ()\r
-               {\r
-                       return new DoubleConstant (Value);\r
-               }\r
-\r
-               public override FloatConstant ConvertToFloat ()\r
-               {\r
-                       return new FloatConstant (Value);\r
-               }\r
-\r
-               public override ULongConstant ConvertToULong ()\r
-               {\r
-                       return new ULongConstant (Value);\r
-               }\r
-\r
-               public override LongConstant ConvertToLong ()\r
-               {\r
-                       return new LongConstant (Value);\r
-               }\r
-\r
-               public override UIntConstant ConvertToUInt ()\r
-               {\r
-                       return new UIntConstant (Value);\r
-               }\r
-\r
-               public override IntConstant ConvertToInt ()\r
-               {\r
-                       return new IntConstant (Value);\r
-               }\r
-       }\r
-\r
-       public class CharConstant : Constant {\r
-               public readonly char Value;\r
-\r
-               public CharConstant (char v)\r
-               {\r
-                       type = TypeManager.char_type;\r
-                       eclass = ExprClass.Value;\r
-                       Value = v;\r
-               }\r
-\r
-               public override void Emit (EmitContext ec)\r
-               {\r
-                       IntLiteral.EmitInt (ec.ig, Value);\r
-               }\r
-\r
-               static public string descape (char c)\r
-               {\r
-                       switch (c){\r
-                       case '\a':\r
-                               return "\\a"; \r
-                       case '\b':\r
-                               return "\\b"; \r
-                       case '\n':\r
-                               return "\\n"; \r
-                       case '\t':\r
-                               return "\\t"; \r
-                       case '\v':\r
-                               return "\\v"; \r
-                       case '\r':\r
-                               return "\\r"; \r
-                       case '\\':\r
-                               return "\\\\";\r
-                       case '\f':\r
-                               return "\\f"; \r
-                       case '\0':\r
-                               return "\\0"; \r
-                       case '"':\r
-                               return "\\\""; \r
-                       case '\'':\r
-                               return "\\\'"; \r
-                       }\r
-                       return c.ToString ();\r
-               }\r
-\r
-               public override string AsString ()\r
-               {\r
-                       return "\"" + descape (Value) + "\"";\r
-               }\r
-\r
-               public override object GetValue ()\r
-               {\r
-                       return Value;\r
-               }\r
-\r
-               public override DoubleConstant ConvertToDouble ()\r
-               {\r
-                       return new DoubleConstant (Value);\r
-               }\r
-\r
-               public override FloatConstant ConvertToFloat ()\r
-               {\r
-                       return new FloatConstant (Value);\r
-               }\r
-\r
-               public override ULongConstant ConvertToULong ()\r
-               {\r
-                       return new ULongConstant (Value);\r
-               }\r
-\r
-               public override LongConstant ConvertToLong ()\r
-               {\r
-                       return new LongConstant (Value);\r
-               }\r
-\r
-               public override UIntConstant ConvertToUInt ()\r
-               {\r
-                       return new UIntConstant (Value);\r
-               }\r
-\r
-               public override IntConstant ConvertToInt ()\r
-               {\r
-                       return new IntConstant (Value);\r
-               }\r
-       }\r
-\r
-       public class SByteConstant : Constant {\r
-               public readonly sbyte Value;\r
-\r
-               public SByteConstant (sbyte v)\r
-               {\r
-                       type = TypeManager.sbyte_type;\r
-                       eclass = ExprClass.Value;\r
-                       Value = v;\r
-               }\r
-\r
-               public override void Emit (EmitContext ec)\r
-               {\r
-                       IntLiteral.EmitInt (ec.ig, Value);\r
-               }\r
-\r
-               public override string AsString ()\r
-               {\r
-                       return Value.ToString ();\r
-               }\r
-\r
-               public override object GetValue ()\r
-               {\r
-                       return Value;\r
-               }\r
-\r
-               public override DoubleConstant ConvertToDouble ()\r
-               {\r
-                       return new DoubleConstant (Value);\r
-               }\r
-\r
-               public override FloatConstant ConvertToFloat ()\r
-               {\r
-                       return new FloatConstant (Value);\r
-               }\r
-\r
-               public override ULongConstant ConvertToULong ()\r
-               {\r
-                       if (Value >= 0)\r
-                               return new ULongConstant ((ulong) Value);\r
-                       \r
-                       return null;\r
-               }\r
-\r
-               public override LongConstant ConvertToLong ()\r
-               {\r
-                       return new LongConstant (Value);\r
-               }\r
-\r
-               public override UIntConstant ConvertToUInt ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public override IntConstant ConvertToInt ()\r
-               {\r
-                       return new IntConstant (Value);\r
-               }\r
-       }\r
-\r
-       public class ShortConstant : Constant {\r
-               public readonly short Value;\r
-\r
-               public ShortConstant (short v)\r
-               {\r
-                       type = TypeManager.short_type;\r
-                       eclass = ExprClass.Value;\r
-                       Value = v;\r
-               }\r
-\r
-               public override void Emit (EmitContext ec)\r
-               {\r
-                       IntLiteral.EmitInt (ec.ig, Value);\r
-               }\r
-\r
-               public override string AsString ()\r
-               {\r
-                       return Value.ToString ();\r
-               }\r
-\r
-               public override object GetValue ()\r
-               {\r
-                       return Value;\r
-               }\r
-\r
-               public override DoubleConstant ConvertToDouble ()\r
-               {\r
-                       return new DoubleConstant (Value);\r
-               }\r
-\r
-               public override FloatConstant ConvertToFloat ()\r
-               {\r
-                       return new FloatConstant (Value);\r
-               }\r
-\r
-               public override ULongConstant ConvertToULong ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public override LongConstant ConvertToLong ()\r
-               {\r
-                       return new LongConstant (Value);\r
-               }\r
-\r
-               public override UIntConstant ConvertToUInt ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public override IntConstant ConvertToInt ()\r
-               {\r
-                       return new IntConstant (Value);\r
-               }\r
-       }\r
-\r
-       public class UShortConstant : Constant {\r
-               public readonly ushort Value;\r
-\r
-               public UShortConstant (ushort v)\r
-               {\r
-                       type = TypeManager.ushort_type;\r
-                       eclass = ExprClass.Value;\r
-                       Value = v;\r
-               }\r
-\r
-               public override void Emit (EmitContext ec)\r
-               {\r
-                       IntLiteral.EmitInt (ec.ig, Value);\r
-               }\r
-\r
-               public override string AsString ()\r
-               {\r
-                       return Value.ToString ();\r
-               }\r
-\r
-               public override object GetValue ()\r
-               {\r
-                       return Value;\r
-               }\r
-\r
-               public override DoubleConstant ConvertToDouble ()\r
-               {\r
-                       return new DoubleConstant (Value);\r
-               }\r
-\r
-               public override FloatConstant ConvertToFloat ()\r
-               {\r
-                       return new FloatConstant (Value);\r
-               }\r
-\r
-               public override ULongConstant ConvertToULong ()\r
-               {\r
-                       return new ULongConstant (Value);\r
-               }\r
-\r
-               public override LongConstant ConvertToLong ()\r
-               {\r
-                       return new LongConstant (Value);\r
-               }\r
-\r
-               public override UIntConstant ConvertToUInt ()\r
-               {\r
-                       return new UIntConstant (Value);\r
-               }\r
-\r
-               public override IntConstant ConvertToInt ()\r
-               {\r
-                       return new IntConstant (Value);\r
-               }\r
-       }\r
-\r
-       public class IntConstant : Constant {\r
-               public readonly int Value;\r
-\r
-               public IntConstant (int v)\r
-               {\r
-                       type = TypeManager.int32_type;\r
-                       eclass = ExprClass.Value;\r
-                       Value = v;\r
-               }\r
-\r
-               static public void EmitInt (ILGenerator ig, int i)\r
-               {\r
-                       switch (i){\r
-                       case -1:\r
-                               ig.Emit (OpCodes.Ldc_I4_M1);\r
-                               break;\r
-                               \r
-                       case 0:\r
-                               ig.Emit (OpCodes.Ldc_I4_0);\r
-                               break;\r
-                               \r
-                       case 1:\r
-                               ig.Emit (OpCodes.Ldc_I4_1);\r
-                               break;\r
-                               \r
-                       case 2:\r
-                               ig.Emit (OpCodes.Ldc_I4_2);\r
-                               break;\r
-                               \r
-                       case 3:\r
-                               ig.Emit (OpCodes.Ldc_I4_3);\r
-                               break;\r
-                               \r
-                       case 4:\r
-                               ig.Emit (OpCodes.Ldc_I4_4);\r
-                               break;\r
-                               \r
-                       case 5:\r
-                               ig.Emit (OpCodes.Ldc_I4_5);\r
-                               break;\r
-                               \r
-                       case 6:\r
-                               ig.Emit (OpCodes.Ldc_I4_6);\r
-                               break;\r
-                               \r
-                       case 7:\r
-                               ig.Emit (OpCodes.Ldc_I4_7);\r
-                               break;\r
-                               \r
-                       case 8:\r
-                               ig.Emit (OpCodes.Ldc_I4_8);\r
-                               break;\r
-\r
-                       default:\r
-                               if (i >= -128 && i <= 127){\r
-                                       ig.Emit (OpCodes.Ldc_I4_S, (sbyte) i);\r
-                               } else\r
-                                       ig.Emit (OpCodes.Ldc_I4, i);\r
-                               break;\r
-                       }\r
-               }\r
-\r
-               public override void Emit (EmitContext ec)\r
-               {\r
-                       EmitInt (ec.ig, Value);\r
-               }\r
-\r
-               public override string AsString ()\r
-               {\r
-                       return Value.ToString ();\r
-               }\r
-\r
-               public override object GetValue ()\r
-               {\r
-                       return Value;\r
-               }\r
-\r
-               public override DoubleConstant ConvertToDouble ()\r
-               {\r
-                       return new DoubleConstant (Value);\r
-               }\r
-\r
-               public override FloatConstant ConvertToFloat ()\r
-               {\r
-                       return new FloatConstant (Value);\r
-               }\r
-\r
-               public override ULongConstant ConvertToULong ()\r
-               {\r
-                       if (Value < 0)\r
-                               return null;\r
-\r
-                       return new ULongConstant ((ulong) Value);\r
-               }\r
-\r
-               public override LongConstant ConvertToLong ()\r
-               {\r
-                       return new LongConstant (Value);\r
-               }\r
-\r
-               public override UIntConstant ConvertToUInt ()\r
-               {\r
-                       if (Value < 0)\r
-                               return null;\r
-\r
-                       return new UIntConstant ((uint) Value);\r
-               }\r
-\r
-               public override IntConstant ConvertToInt ()\r
-               {\r
-                       return this;\r
-               }\r
-       }\r
-\r
-       public class UIntConstant : Constant {\r
-               public readonly uint Value;\r
-\r
-               public UIntConstant (uint v)\r
-               {\r
-                       type = TypeManager.uint32_type;\r
-                       eclass = ExprClass.Value;\r
-                       Value = v;\r
-               }\r
-\r
-               public override void Emit (EmitContext ec)\r
-               {\r
-                       IntLiteral.EmitInt (ec.ig, unchecked ((int) Value));\r
-               }\r
-\r
-               public override string AsString ()\r
-               {\r
-                       return Value.ToString ();\r
-               }\r
-\r
-               public override object GetValue ()\r
-               {\r
-                       return Value;\r
-               }\r
-\r
-               public override DoubleConstant ConvertToDouble ()\r
-               {\r
-                       return new DoubleConstant (Value);\r
-               }\r
-\r
-               public override FloatConstant ConvertToFloat ()\r
-               {\r
-                       return new FloatConstant (Value);\r
-               }\r
-\r
-               public override ULongConstant ConvertToULong ()\r
-               {\r
-                       return new ULongConstant (Value);\r
-               }\r
-\r
-               public override LongConstant ConvertToLong ()\r
-               {\r
-                       return new LongConstant (Value);\r
-               }\r
-\r
-               public override UIntConstant ConvertToUInt ()\r
-               {\r
-                       return this;\r
-               }\r
-\r
-               public override IntConstant ConvertToInt ()\r
-               {\r
-                       return null;\r
-               }\r
-       }\r
-\r
-       public class LongConstant : Constant {\r
-               public readonly long Value;\r
-\r
-               public LongConstant (long v)\r
-               {\r
-                       type = TypeManager.int64_type;\r
-                       eclass = ExprClass.Value;\r
-                       Value = v;\r
-               }\r
-\r
-               public override void Emit (EmitContext ec)\r
-               {\r
-                       ILGenerator ig = ec.ig;\r
-\r
-                       EmitLong (ig, Value);\r
-               }\r
-\r
-               static public void EmitLong (ILGenerator ig, long l)\r
-               {\r
-                       ig.Emit (OpCodes.Ldc_I8, l);\r
-               }\r
-\r
-               public override string AsString ()\r
-               {\r
-                       return Value.ToString ();\r
-               }\r
-\r
-               public override object GetValue ()\r
-               {\r
-                       return Value;\r
-               }\r
-\r
-               public override DoubleConstant ConvertToDouble ()\r
-               {\r
-                       return new DoubleConstant (Value);\r
-               }\r
-\r
-               public override FloatConstant ConvertToFloat ()\r
-               {\r
-                       return new FloatConstant (Value);\r
-               }\r
-\r
-               public override ULongConstant ConvertToULong ()\r
-               {\r
-                       if (Value < 0)\r
-                               return null;\r
-                       \r
-                       return new ULongConstant ((ulong) Value);\r
-               }\r
-\r
-               public override LongConstant ConvertToLong ()\r
-               {\r
-                       return this;\r
-               }\r
-\r
-               public override UIntConstant ConvertToUInt ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public override IntConstant ConvertToInt ()\r
-               {\r
-                       return null;\r
-               }\r
-       }\r
-\r
-       public class ULongConstant : Constant {\r
-               public readonly ulong Value;\r
-\r
-               public ULongConstant (ulong v)\r
-               {\r
-                       type = TypeManager.uint64_type;\r
-                       eclass = ExprClass.Value;\r
-                       Value = v;\r
-               }\r
-\r
-               public override void Emit (EmitContext ec)\r
-               {\r
-                       ILGenerator ig = ec.ig;\r
-\r
-                       LongLiteral.EmitLong (ig, unchecked ((long) Value));\r
-               }\r
-\r
-               public override string AsString ()\r
-               {\r
-                       return Value.ToString ();\r
-               }\r
-\r
-               public override object GetValue ()\r
-               {\r
-                       return Value;\r
-               }\r
-\r
-               public override DoubleConstant ConvertToDouble ()\r
-               {\r
-                       return new DoubleConstant (Value);\r
-               }\r
-\r
-               public override FloatConstant ConvertToFloat ()\r
-               {\r
-                       return new FloatConstant (Value);\r
-               }\r
-\r
-               public override ULongConstant ConvertToULong ()\r
-               {\r
-                       return this;\r
-               }\r
-\r
-               public override LongConstant ConvertToLong ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public override UIntConstant ConvertToUInt ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public override IntConstant ConvertToInt ()\r
-               {\r
-                       return null;\r
-               }\r
-       }\r
-\r
-       public class FloatConstant : Constant {\r
-               public readonly float Value;\r
-\r
-               public FloatConstant (float v)\r
-               {\r
-                       type = TypeManager.float_type;\r
-                       eclass = ExprClass.Value;\r
-                       Value = v;\r
-               }\r
-\r
-               public override void Emit (EmitContext ec)\r
-               {\r
-                       ec.ig.Emit (OpCodes.Ldc_R4, Value);\r
-               }\r
-\r
-               public override string AsString ()\r
-               {\r
-                       return Value.ToString ();\r
-               }\r
-\r
-               public override object GetValue ()\r
-               {\r
-                       return Value;\r
-               }\r
-\r
-               public override DoubleConstant ConvertToDouble ()\r
-               {\r
-                       return new DoubleConstant (Value);\r
-               }\r
-\r
-               public override FloatConstant ConvertToFloat ()\r
-               {\r
-                       return this;\r
-               }\r
-\r
-               public override LongConstant ConvertToLong ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public override UIntConstant ConvertToUInt ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public override IntConstant ConvertToInt ()\r
-               {\r
-                       return null;\r
-               }\r
-       }\r
-\r
-       public class DoubleConstant : Constant {\r
-               public readonly double Value;\r
-\r
-               public DoubleConstant (double v)\r
-               {\r
-                       type = TypeManager.double_type;\r
-                       eclass = ExprClass.Value;\r
-                       Value = v;\r
-               }\r
-\r
-               public override void Emit (EmitContext ec)\r
-               {\r
-                       ec.ig.Emit (OpCodes.Ldc_R8, Value);\r
-               }\r
-\r
-               public override string AsString ()\r
-               {\r
-                       return Value.ToString ();\r
-               }\r
-\r
-               public override object GetValue ()\r
-               {\r
-                       return Value;\r
-               }\r
-\r
-               public override DoubleConstant ConvertToDouble ()\r
-               {\r
-                       return this;\r
-               }\r
-\r
-               public override FloatConstant ConvertToFloat ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public override ULongConstant ConvertToULong ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public override LongConstant ConvertToLong ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public override UIntConstant ConvertToUInt ()\r
-               {\r
-                       return null;\r
-               }\r
-\r
-               public override IntConstant ConvertToInt ()\r
-               {\r
-                       return null;\r
-               }\r
-       }\r
-\r
-       public class DecimalConstant : Constant {\r
-               public readonly decimal Value;\r
-\r
-               public DecimalConstant (decimal d)\r
-               {\r
-                       type = TypeManager.decimal_type;\r
-                       eclass = ExprClass.Value;\r
-                       Value = d;\r
-               }\r
-\r
-               override public string AsString ()\r
-               {\r
-                       return Value.ToString ();\r
-               }\r
-\r
-               public override object GetValue ()\r
-               {\r
-                       return (object) Value;\r
-               }\r
-\r
-               public override void Emit (EmitContext ec)\r
-               {\r
-                       throw new Exception ("Implement me");\r
-               }\r
-       }\r
-\r
-       public class StringConstant : Constant {\r
-               public readonly string Value;\r
-\r
-               public StringConstant (string s)\r
-               {\r
-                       type = TypeManager.string_type;\r
-                       eclass = ExprClass.Value;\r
-                       Value = s;\r
-               }\r
-\r
-               // FIXME: Escape the string.\r
-               override public string AsString ()\r
-               {\r
-                       return "\"" + Value + "\"";\r
-               }\r
-\r
-               public override object GetValue ()\r
-               {\r
-                       return Value;\r
-               }\r
-               \r
-               public override void Emit (EmitContext ec)\r
-               {\r
-                       ec.ig.Emit (OpCodes.Ldstr, Value);\r
-               }\r
-       }\r
-\r
-}\r
-\r
-\r
+//
+// constant.cs: Constants.
+//
+// Author:
+//   Miguel de Icaza (miguel@ximian.com)
+//
+// (C) 2001 Ximian, Inc.
+//
+//
+
+namespace Mono.CSharp {
+
+       using System;
+       using System.Reflection.Emit;
+
+       /// <summary>
+       ///   Base class for constants and literals.
+       /// </summary>
+       public abstract class Constant : Expression {
+               /// <remarks>
+               ///   This is different from ToString in that ToString
+               ///   is supposed to be there for debugging purposes,
+               ///   and is not guaranteed to be useful for anything else,
+               ///   AsString() will provide something that can be used
+               ///   for round-tripping C# code.  Maybe it can be used
+               ///   for IL assembly as well.
+               /// </remarks>
+               public abstract string AsString ();
+
+               override public string ToString ()
+               {
+                       return this.GetType ().Name + " (" + AsString () + ")";
+               }
+
+               /// <summary>
+               ///  This is used to obtain the actual value of the literal
+               ///  cast into an object.
+               /// </summary>
+               public abstract object GetValue ();
+
+               /// <summary>
+               ///   Constants are always born in a fully resolved state
+               /// </summary>
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       return this;
+               }
+
+               //
+               // The various ToXXXX conversion functions are used by the constant
+               // folding evaluator.   A null value is returned if the conversion is
+               // not possible.   
+               //
+               // Note: not all the patterns for catching `implicit_conv' are the same.
+               // some implicit conversions can never be performed between two types
+               // even if the conversion would be lossless (for example short to uint),
+               // but some conversions are explicitly permitted by the standard provided
+               // that there will be no loss of information (for example, int to uint).
+               //
+               public DoubleConstant ToDouble (Location loc)
+               {
+                       DoubleConstant c = ConvertToDouble ();
+
+                       if (c == null)
+                               Error_CannotConvertImplicit (loc, Type, TypeManager.double_type);
+
+                       return c;
+               }
+
+               public FloatConstant ToFloat (Location loc)
+               {
+                       FloatConstant c = ConvertToFloat ();
+
+                       if (c == null)
+                               Error_CannotConvertImplicit (loc, Type, TypeManager.float_type);
+
+                       return c;
+               }
+
+               public ULongConstant ToULong (Location loc)
+               {
+                       ULongConstant c = ConvertToULong ();
+
+                       if (c == null)
+                               Error_CannotConvertImplicit (loc, Type, TypeManager.uint64_type);
+
+                       return c;
+               }
+
+               public LongConstant ToLong (Location loc)
+               {
+                       LongConstant c = ConvertToLong ();
+
+                       if (c == null)
+                               Error_CannotConvertImplicit (loc, Type, TypeManager.int64_type);
+
+                       return c;
+               }
+               
+               public UIntConstant ToUInt (Location loc)
+               {
+                       UIntConstant c = ConvertToUInt ();
+
+                       if (c == null)
+                               Error_CannotConvertImplicit (loc, Type, TypeManager.uint32_type);
+
+                       return c;
+               }
+
+               public IntConstant ToInt (Location loc)
+               {
+                       IntConstant c = ConvertToInt ();
+
+                       if (c == null)
+                               Error_CannotConvertImplicit (loc, Type, TypeManager.int32_type);
+
+                       return c;
+               }
+               
+               public virtual DoubleConstant ConvertToDouble ()
+               {
+                       return null;
+               }
+
+               public virtual FloatConstant ConvertToFloat ()
+               {
+                       return null;
+               }
+
+               public virtual ULongConstant ConvertToULong ()
+               {
+                       return null;
+               }
+
+               public virtual LongConstant ConvertToLong ()
+               {
+                       return null;
+               }
+
+               public virtual UIntConstant ConvertToUInt ()
+               {
+                       return null;
+               }
+
+               public virtual IntConstant ConvertToInt ()
+               {
+                       return null;
+               }
+       }
+       
+       public class BoolConstant : Constant {
+               public readonly bool Value;
+               
+               public BoolConstant (bool val)
+               {
+                       type = TypeManager.bool_type;
+                       eclass = ExprClass.Value;
+
+                       Value = val;
+               }
+
+               override public string AsString ()
+               {
+                       return Value ? "true" : "false";
+               }
+
+               public override object GetValue ()
+               {
+                       return (object) Value;
+               }
+                               
+               
+               public override void Emit (EmitContext ec)
+               {
+                       if (Value)
+                               ec.ig.Emit (OpCodes.Ldc_I4_1);
+                       else
+                               ec.ig.Emit (OpCodes.Ldc_I4_0);
+               }
+       }
+
+       public class ByteConstant : Constant {
+               public readonly byte Value;
+
+               public ByteConstant (byte v)
+               {
+                       type = TypeManager.byte_type;
+                       eclass = ExprClass.Value;
+                       Value = v;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       IntLiteral.EmitInt (ec.ig, Value);
+               }
+
+               public override string AsString ()
+               {
+                       return Value.ToString ();
+               }
+
+               public override object GetValue ()
+               {
+                       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 ()
+               {
+                       return new LongConstant (Value);
+               }
+
+               public override UIntConstant ConvertToUInt ()
+               {
+                       return new UIntConstant (Value);
+               }
+
+               public override IntConstant ConvertToInt ()
+               {
+                       return new IntConstant (Value);
+               }
+       }
+
+       public class CharConstant : Constant {
+               public readonly char Value;
+
+               public CharConstant (char v)
+               {
+                       type = TypeManager.char_type;
+                       eclass = ExprClass.Value;
+                       Value = v;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       IntLiteral.EmitInt (ec.ig, Value);
+               }
+
+               static public string descape (char c)
+               {
+                       switch (c){
+                       case '\a':
+                               return "\\a"; 
+                       case '\b':
+                               return "\\b"; 
+                       case '\n':
+                               return "\\n"; 
+                       case '\t':
+                               return "\\t"; 
+                       case '\v':
+                               return "\\v"; 
+                       case '\r':
+                               return "\\r"; 
+                       case '\\':
+                               return "\\\\";
+                       case '\f':
+                               return "\\f"; 
+                       case '\0':
+                               return "\\0"; 
+                       case '"':
+                               return "\\\""; 
+                       case '\'':
+                               return "\\\'"; 
+                       }
+                       return c.ToString ();
+               }
+
+               public override string AsString ()
+               {
+                       return "\"" + descape (Value) + "\"";
+               }
+
+               public override object GetValue ()
+               {
+                       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 ()
+               {
+                       return new LongConstant (Value);
+               }
+
+               public override UIntConstant ConvertToUInt ()
+               {
+                       return new UIntConstant (Value);
+               }
+
+               public override IntConstant ConvertToInt ()
+               {
+                       return new IntConstant (Value);
+               }
+       }
+
+       public class SByteConstant : Constant {
+               public readonly sbyte Value;
+
+               public SByteConstant (sbyte v)
+               {
+                       type = TypeManager.sbyte_type;
+                       eclass = ExprClass.Value;
+                       Value = v;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       IntLiteral.EmitInt (ec.ig, Value);
+               }
+
+               public override string AsString ()
+               {
+                       return Value.ToString ();
+               }
+
+               public override object GetValue ()
+               {
+                       return Value;
+               }
+
+               public override DoubleConstant ConvertToDouble ()
+               {
+                       return new DoubleConstant (Value);
+               }
+
+               public override FloatConstant ConvertToFloat ()
+               {
+                       return new FloatConstant (Value);
+               }
+
+               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 class ShortConstant : Constant {
+               public readonly short Value;
+
+               public ShortConstant (short v)
+               {
+                       type = TypeManager.short_type;
+                       eclass = ExprClass.Value;
+                       Value = v;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       IntLiteral.EmitInt (ec.ig, Value);
+               }
+
+               public override string AsString ()
+               {
+                       return Value.ToString ();
+               }
+
+               public override object GetValue ()
+               {
+                       return Value;
+               }
+
+               public override DoubleConstant ConvertToDouble ()
+               {
+                       return new DoubleConstant (Value);
+               }
+
+               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 class UShortConstant : Constant {
+               public readonly ushort Value;
+
+               public UShortConstant (ushort v)
+               {
+                       type = TypeManager.ushort_type;
+                       eclass = ExprClass.Value;
+                       Value = v;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       IntLiteral.EmitInt (ec.ig, Value);
+               }
+
+               public override string AsString ()
+               {
+                       return Value.ToString ();
+               }
+
+               public override object GetValue ()
+               {
+                       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 ()
+               {
+                       return new LongConstant (Value);
+               }
+
+               public override UIntConstant ConvertToUInt ()
+               {
+                       return new UIntConstant (Value);
+               }
+
+               public override IntConstant ConvertToInt ()
+               {
+                       return new IntConstant (Value);
+               }
+       }
+
+       public class IntConstant : Constant {
+               public readonly int Value;
+
+               public IntConstant (int v)
+               {
+                       type = TypeManager.int32_type;
+                       eclass = ExprClass.Value;
+                       Value = v;
+               }
+
+               static public void EmitInt (ILGenerator ig, int i)
+               {
+                       switch (i){
+                       case -1:
+                               ig.Emit (OpCodes.Ldc_I4_M1);
+                               break;
+                               
+                       case 0:
+                               ig.Emit (OpCodes.Ldc_I4_0);
+                               break;
+                               
+                       case 1:
+                               ig.Emit (OpCodes.Ldc_I4_1);
+                               break;
+                               
+                       case 2:
+                               ig.Emit (OpCodes.Ldc_I4_2);
+                               break;
+                               
+                       case 3:
+                               ig.Emit (OpCodes.Ldc_I4_3);
+                               break;
+                               
+                       case 4:
+                               ig.Emit (OpCodes.Ldc_I4_4);
+                               break;
+                               
+                       case 5:
+                               ig.Emit (OpCodes.Ldc_I4_5);
+                               break;
+                               
+                       case 6:
+                               ig.Emit (OpCodes.Ldc_I4_6);
+                               break;
+                               
+                       case 7:
+                               ig.Emit (OpCodes.Ldc_I4_7);
+                               break;
+                               
+                       case 8:
+                               ig.Emit (OpCodes.Ldc_I4_8);
+                               break;
+
+                       default:
+                               if (i >= -128 && i <= 127){
+                                       ig.Emit (OpCodes.Ldc_I4_S, (sbyte) i);
+                               } else
+                                       ig.Emit (OpCodes.Ldc_I4, i);
+                               break;
+                       }
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       EmitInt (ec.ig, Value);
+               }
+
+               public override string AsString ()
+               {
+                       return Value.ToString ();
+               }
+
+               public override object GetValue ()
+               {
+                       return Value;
+               }
+
+               public override DoubleConstant ConvertToDouble ()
+               {
+                       return new DoubleConstant (Value);
+               }
+
+               public override FloatConstant ConvertToFloat ()
+               {
+                       return new FloatConstant (Value);
+               }
+
+               public override ULongConstant ConvertToULong ()
+               {
+                       if (Value < 0)
+                               return null;
+
+                       return new ULongConstant ((ulong) Value);
+               }
+
+               public override LongConstant ConvertToLong ()
+               {
+                       return new LongConstant (Value);
+               }
+
+               public override UIntConstant ConvertToUInt ()
+               {
+                       if (Value < 0)
+                               return null;
+
+                       return new UIntConstant ((uint) Value);
+               }
+
+               public override IntConstant ConvertToInt ()
+               {
+                       return this;
+               }
+       }
+
+       public class UIntConstant : Constant {
+               public readonly uint Value;
+
+               public UIntConstant (uint v)
+               {
+                       type = TypeManager.uint32_type;
+                       eclass = ExprClass.Value;
+                       Value = v;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       IntLiteral.EmitInt (ec.ig, unchecked ((int) Value));
+               }
+
+               public override string AsString ()
+               {
+                       return Value.ToString ();
+               }
+
+               public override object GetValue ()
+               {
+                       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 ()
+               {
+                       return new LongConstant (Value);
+               }
+
+               public override UIntConstant ConvertToUInt ()
+               {
+                       return this;
+               }
+
+               public override IntConstant ConvertToInt ()
+               {
+                       return null;
+               }
+       }
+
+       public class LongConstant : Constant {
+               public readonly long Value;
+
+               public LongConstant (long v)
+               {
+                       type = TypeManager.int64_type;
+                       eclass = ExprClass.Value;
+                       Value = v;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       ILGenerator ig = ec.ig;
+
+                       EmitLong (ig, Value);
+               }
+
+               static public void EmitLong (ILGenerator ig, long l)
+               {
+                       ig.Emit (OpCodes.Ldc_I8, l);
+               }
+
+               public override string AsString ()
+               {
+                       return Value.ToString ();
+               }
+
+               public override object GetValue ()
+               {
+                       return Value;
+               }
+
+               public override DoubleConstant ConvertToDouble ()
+               {
+                       return new DoubleConstant (Value);
+               }
+
+               public override FloatConstant ConvertToFloat ()
+               {
+                       return new FloatConstant (Value);
+               }
+
+               public override ULongConstant ConvertToULong ()
+               {
+                       if (Value < 0)
+                               return null;
+                       
+                       return new ULongConstant ((ulong) Value);
+               }
+
+               public override LongConstant ConvertToLong ()
+               {
+                       return this;
+               }
+
+               public override UIntConstant ConvertToUInt ()
+               {
+                       return null;
+               }
+
+               public override IntConstant ConvertToInt ()
+               {
+                       return null;
+               }
+       }
+
+       public class ULongConstant : Constant {
+               public readonly ulong Value;
+
+               public ULongConstant (ulong v)
+               {
+                       type = TypeManager.uint64_type;
+                       eclass = ExprClass.Value;
+                       Value = v;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       ILGenerator ig = ec.ig;
+
+                       LongLiteral.EmitLong (ig, unchecked ((long) Value));
+               }
+
+               public override string AsString ()
+               {
+                       return Value.ToString ();
+               }
+
+               public override object GetValue ()
+               {
+                       return Value;
+               }
+
+               public override DoubleConstant ConvertToDouble ()
+               {
+                       return new DoubleConstant (Value);
+               }
+
+               public override FloatConstant ConvertToFloat ()
+               {
+                       return new FloatConstant (Value);
+               }
+
+               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 class FloatConstant : Constant {
+               public readonly float Value;
+
+               public FloatConstant (float v)
+               {
+                       type = TypeManager.float_type;
+                       eclass = ExprClass.Value;
+                       Value = v;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       ec.ig.Emit (OpCodes.Ldc_R4, Value);
+               }
+
+               public override string AsString ()
+               {
+                       return Value.ToString ();
+               }
+
+               public override object GetValue ()
+               {
+                       return Value;
+               }
+
+               public override DoubleConstant ConvertToDouble ()
+               {
+                       return new DoubleConstant (Value);
+               }
+
+               public override FloatConstant ConvertToFloat ()
+               {
+                       return this;
+               }
+
+               public override LongConstant ConvertToLong ()
+               {
+                       return null;
+               }
+
+               public override UIntConstant ConvertToUInt ()
+               {
+                       return null;
+               }
+
+               public override IntConstant ConvertToInt ()
+               {
+                       return null;
+               }
+       }
+
+       public class DoubleConstant : Constant {
+               public readonly double Value;
+
+               public DoubleConstant (double v)
+               {
+                       type = TypeManager.double_type;
+                       eclass = ExprClass.Value;
+                       Value = v;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       ec.ig.Emit (OpCodes.Ldc_R8, Value);
+               }
+
+               public override string AsString ()
+               {
+                       return Value.ToString ();
+               }
+
+               public override object GetValue ()
+               {
+                       return Value;
+               }
+
+               public override DoubleConstant ConvertToDouble ()
+               {
+                       return this;
+               }
+
+               public override FloatConstant ConvertToFloat ()
+               {
+                       return null;
+               }
+
+               public override ULongConstant ConvertToULong ()
+               {
+                       return null;
+               }
+
+               public override LongConstant ConvertToLong ()
+               {
+                       return null;
+               }
+
+               public override UIntConstant ConvertToUInt ()
+               {
+                       return null;
+               }
+
+               public override IntConstant ConvertToInt ()
+               {
+                       return null;
+               }
+       }
+
+       public class DecimalConstant : Constant {
+               public readonly decimal Value;
+
+               public DecimalConstant (decimal d)
+               {
+                       type = TypeManager.decimal_type;
+                       eclass = ExprClass.Value;
+                       Value = d;
+               }
+
+               override public string AsString ()
+               {
+                       return Value.ToString ();
+               }
+
+               public override object GetValue ()
+               {
+                       return (object) Value;
+               }
+
+               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;
+                       
+                       IntConstant.EmitInt (ig, words [0]);
+                       IntConstant.EmitInt (ig, words [1]);
+                       IntConstant.EmitInt (ig, words [2]);
+
+                       // sign
+                       IntConstant.EmitInt (ig, words [3] >> 31);
+
+                       // power
+                       IntConstant.EmitInt (ig, (words [3] >> 16) & 0xff);
+
+                       ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_five_args);
+               }
+       }
+
+       public class StringConstant : Constant {
+               public readonly string Value;
+
+               public StringConstant (string s)
+               {
+                       type = TypeManager.string_type;
+                       eclass = ExprClass.Value;
+                       Value = s;
+               }
+
+               // FIXME: Escape the string.
+               override public string AsString ()
+               {
+                       return "\"" + Value + "\"";
+               }
+
+               public override object GetValue ()
+               {
+                       return Value;
+               }
+               
+               public override void Emit (EmitContext ec)
+               {
+                       ec.ig.Emit (OpCodes.Ldstr, Value);
+               }
+       }
+
+}
+
+