2 // constant.cs: Constants.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
11 namespace Mono.CSharp {
14 using System.Reflection.Emit;
17 /// Base class for constants and literals.
19 public abstract class Constant : Expression {
23 eclass = ExprClass.Value;
27 /// This is different from ToString in that ToString
28 /// is supposed to be there for debugging purposes,
29 /// and is not guaranteed to be useful for anything else,
30 /// AsString() will provide something that can be used
31 /// for round-tripping C# code. Maybe it can be used
32 /// for IL assembly as well.
34 public abstract string AsString ();
36 override public string ToString ()
42 /// This is used to obtain the actual value of the literal
43 /// cast into an object.
45 public abstract object GetValue ();
48 /// Constants are always born in a fully resolved state
50 public override Expression DoResolve (EmitContext ec)
56 public class BoolConstant : Constant {
57 public readonly bool Value;
59 public BoolConstant (bool val)
61 type = TypeManager.bool_type;
66 override public string AsString ()
68 return Value ? "true" : "false";
71 public override object GetValue ()
73 return (object) Value;
77 public override void Emit (EmitContext ec)
80 ec.ig.Emit (OpCodes.Ldc_I4_1);
82 ec.ig.Emit (OpCodes.Ldc_I4_0);
86 public class ByteConstant : Constant {
87 public readonly byte Value;
89 public ByteConstant (byte v)
91 type = TypeManager.byte_type;
95 public override void Emit (EmitContext ec)
97 IntLiteral.EmitInt (ec.ig, Value);
100 public override string AsString ()
102 return Value.ToString ();
105 public override object GetValue ()
111 public class CharConstant : Constant {
112 public readonly char Value;
114 public CharConstant (char v)
116 type = TypeManager.char_type;
120 public override void Emit (EmitContext ec)
122 IntLiteral.EmitInt (ec.ig, Value);
125 static public string descape (char c)
151 return c.ToString ();
154 public override string AsString ()
156 return "\"" + descape (Value) + "\"";
159 public override object GetValue ()
165 public class SByteConstant : Constant {
166 public readonly sbyte Value;
168 public SByteConstant (sbyte v)
170 type = TypeManager.sbyte_type;
174 public override void Emit (EmitContext ec)
176 IntLiteral.EmitInt (ec.ig, Value);
179 public override string AsString ()
181 return Value.ToString ();
184 public override object GetValue ()
190 public class ShortConstant : Constant {
191 public readonly short Value;
193 public ShortConstant (short v)
195 type = TypeManager.short_type;
199 public override void Emit (EmitContext ec)
201 IntLiteral.EmitInt (ec.ig, Value);
204 public override string AsString ()
206 return Value.ToString ();
209 public override object GetValue ()
215 public class UShortConstant : Constant {
216 public readonly ushort Value;
218 public UShortConstant (ushort v)
220 type = TypeManager.ushort_type;
224 public override void Emit (EmitContext ec)
226 IntLiteral.EmitInt (ec.ig, Value);
229 public override string AsString ()
231 return Value.ToString ();
234 public override object GetValue ()
240 public class IntConstant : Constant {
241 public readonly int Value;
243 public IntConstant (int v)
245 type = TypeManager.int32_type;
249 static public void EmitInt (ILGenerator ig, int i)
253 ig.Emit (OpCodes.Ldc_I4_M1);
257 ig.Emit (OpCodes.Ldc_I4_0);
261 ig.Emit (OpCodes.Ldc_I4_1);
265 ig.Emit (OpCodes.Ldc_I4_2);
269 ig.Emit (OpCodes.Ldc_I4_3);
273 ig.Emit (OpCodes.Ldc_I4_4);
277 ig.Emit (OpCodes.Ldc_I4_5);
281 ig.Emit (OpCodes.Ldc_I4_6);
285 ig.Emit (OpCodes.Ldc_I4_7);
289 ig.Emit (OpCodes.Ldc_I4_8);
293 if (i >= -128 && i <= 127){
294 ig.Emit (OpCodes.Ldc_I4_S, (sbyte) i);
296 ig.Emit (OpCodes.Ldc_I4, i);
301 public override void Emit (EmitContext ec)
303 EmitInt (ec.ig, Value);
306 public override string AsString ()
308 return Value.ToString ();
311 public override object GetValue ()
317 public class UIntConstant : Constant {
318 public readonly uint Value;
320 public UIntConstant (uint v)
322 type = TypeManager.uint32_type;
326 public override void Emit (EmitContext ec)
328 IntLiteral.EmitInt (ec.ig, unchecked ((int) Value));
331 public override string AsString ()
333 return Value.ToString ();
336 public override object GetValue ()
342 public class LongConstant : Constant {
343 public readonly long Value;
345 public LongConstant (long v)
347 type = TypeManager.int64_type;
351 public override void Emit (EmitContext ec)
353 ILGenerator ig = ec.ig;
355 EmitLong (ig, Value);
358 static public void EmitLong (ILGenerator ig, long l)
360 ig.Emit (OpCodes.Ldc_I8, l);
363 public override string AsString ()
365 return Value.ToString ();
368 public override object GetValue ()
374 public class ULongConstant : Constant {
375 public readonly ulong Value;
377 public ULongConstant (ulong v)
379 type = TypeManager.uint64_type;
383 public override void Emit (EmitContext ec)
385 ILGenerator ig = ec.ig;
387 LongLiteral.EmitLong (ig, unchecked ((long) Value));
390 public override string AsString ()
392 return Value.ToString ();
395 public override object GetValue ()
401 public class FloatConstant : Constant {
402 public readonly float Value;
404 public FloatConstant (float v)
406 type = TypeManager.float_type;
410 public override void Emit (EmitContext ec)
412 ec.ig.Emit (OpCodes.Ldc_R4, Value);
415 public override string AsString ()
417 return Value.ToString ();
420 public override object GetValue ()
426 public class DoubleConstant : Constant {
427 public readonly double Value;
429 public DoubleConstant (double v)
431 type = TypeManager.double_type;
435 public override void Emit (EmitContext ec)
437 ec.ig.Emit (OpCodes.Ldc_R8, Value);
440 public override string AsString ()
442 return Value.ToString ();
445 public override object GetValue ()
451 public class DecimalConstant : Constant {
452 public readonly decimal Value;
454 public DecimalConstant (decimal d)
456 type = TypeManager.decimal_type;
460 override public string AsString ()
462 return Value.ToString ();
465 public override object GetValue ()
467 return (object) Value;
470 public override void Emit (EmitContext ec)
472 throw new Exception ("Implement me");
476 public class StringConstant : Constant {
477 public readonly string Value;
479 public StringConstant (string s)
481 type = TypeManager.string_type;
485 // FIXME: Escape the string.
486 override public string AsString ()
488 return "\"" + Value + "\"";
491 public override object GetValue ()
496 public override void Emit (EmitContext ec)
498 ec.ig.Emit (OpCodes.Ldstr, Value);