2 // literal.cs: Literal representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
11 using System.Reflection;
12 using System.Reflection.Emit;
14 namespace Mono.CSharp {
17 /// Base class for literals
19 public abstract class Literal : Constant {
20 static public string descape (char c)
54 public class NullLiteral : Literal {
55 public static readonly NullLiteral Null;
59 Null = new NullLiteral ();
65 throw new Exception ("More than one null has been created!");
68 override public string AsString ()
73 public override object GetValue ()
78 public override Expression DoResolve (EmitContext ec)
80 type = TypeManager.object_type;
81 eclass = ExprClass.Value;
85 public override void Emit (EmitContext ec)
87 ec.ig.Emit (OpCodes.Ldnull);
91 public class BoolLiteral : Literal {
92 public readonly bool Value;
94 public BoolLiteral (bool val)
99 override public string AsString ()
101 return Value ? "true" : "false";
104 public override object GetValue ()
106 return (object) Value;
110 public override Expression DoResolve (EmitContext ec)
112 type = TypeManager.bool_type;
117 public override void Emit (EmitContext ec)
120 ec.ig.Emit (OpCodes.Ldc_I4_1);
122 ec.ig.Emit (OpCodes.Ldc_I4_0);
126 public class CharLiteral : Literal {
129 public CharLiteral (char c)
134 override public string AsString ()
136 return "\"" + descape (c) + "\"";
139 public override object GetValue ()
144 public override Expression DoResolve (EmitContext ec)
146 type = TypeManager.char_type;
151 public override void Emit (EmitContext ec)
153 IntLiteral.EmitInt (ec.ig, c);
157 public class IntLiteral : Literal {
158 public readonly int Value;
160 public IntLiteral (int l)
165 override public string AsString ()
167 return Value.ToString ();
170 public override object GetValue ()
172 if (Value <= System.Int32.MaxValue &&
173 Value >= System.Int32.MinValue)
174 return (object) Value;
179 public override Expression DoResolve (EmitContext ec)
181 type = TypeManager.int32_type;
186 public override void Emit (EmitContext ec)
188 ILGenerator ig = ec.ig;
193 static public void EmitInt (ILGenerator ig, int i)
197 ig.Emit (OpCodes.Ldc_I4_M1);
201 ig.Emit (OpCodes.Ldc_I4_0);
205 ig.Emit (OpCodes.Ldc_I4_1);
209 ig.Emit (OpCodes.Ldc_I4_2);
213 ig.Emit (OpCodes.Ldc_I4_3);
217 ig.Emit (OpCodes.Ldc_I4_4);
221 ig.Emit (OpCodes.Ldc_I4_5);
225 ig.Emit (OpCodes.Ldc_I4_6);
229 ig.Emit (OpCodes.Ldc_I4_7);
233 ig.Emit (OpCodes.Ldc_I4_8);
237 if (i > 0 && i < 127){
238 ig.Emit (OpCodes.Ldc_I4_S, (sbyte) i);
240 ig.Emit (OpCodes.Ldc_I4, i);
246 public class UIntLiteral : Literal {
247 public readonly uint Value;
249 public UIntLiteral (uint l)
254 override public string AsString ()
256 return Value.ToString ();
259 public override object GetValue ()
261 if (Value <= System.UInt32.MaxValue &&
262 Value >= System.UInt32.MinValue)
263 return (object) Value;
269 public override Expression DoResolve (EmitContext ec)
271 type = TypeManager.uint32_type;
276 public override void Emit (EmitContext ec)
278 ILGenerator ig = ec.ig;
280 IntLiteral.EmitInt (ig, unchecked ((int) Value));
285 public class LongLiteral : Literal {
286 public readonly long Value;
288 public LongLiteral (long l)
293 override public string AsString ()
295 return Value.ToString ();
298 public override object GetValue ()
300 if (Value <= System.Int64.MaxValue &&
301 Value >= System.Int64.MinValue)
302 return (object) Value;
307 public override Expression DoResolve (EmitContext ec)
309 type = TypeManager.int64_type;
314 public override void Emit (EmitContext ec)
316 ILGenerator ig = ec.ig;
318 EmitLong (ig, Value);
321 static public void EmitLong (ILGenerator ig, long l)
323 ig.Emit (OpCodes.Ldc_I8, l);
327 public class ULongLiteral : Literal {
328 public readonly ulong Value;
330 public ULongLiteral (ulong l)
335 override public string AsString ()
337 return Value.ToString ();
340 public override object GetValue ()
342 if (Value <= System.UInt64.MaxValue &&
343 Value >= System.UInt64.MinValue)
344 return (object) Value;
349 public override Expression DoResolve (EmitContext ec)
351 type = TypeManager.uint64_type;
356 public override void Emit (EmitContext ec)
358 ILGenerator ig = ec.ig;
360 LongLiteral.EmitLong (ig, unchecked ((long) Value));
364 public class FloatLiteral : Literal {
365 public readonly float Value;
367 public FloatLiteral (float f)
372 override public string AsString ()
374 return Value.ToString ();
377 public override object GetValue ()
379 return (object) Value;
382 public override Expression DoResolve (EmitContext ec)
384 type = TypeManager.float_type;
389 public override void Emit (EmitContext ec)
391 ec.ig.Emit (OpCodes.Ldc_R4, Value);
395 public class DoubleLiteral : Literal {
396 public readonly double Value;
398 public DoubleLiteral (double d)
403 override public string AsString ()
405 return Value.ToString ();
408 public override object GetValue ()
410 return (object) Value;
413 public override Expression DoResolve (EmitContext ec)
415 type = TypeManager.double_type;
420 public override void Emit (EmitContext ec)
422 ec.ig.Emit (OpCodes.Ldc_R8, Value);
426 public class DecimalLiteral : Literal {
427 public readonly decimal Value;
429 public DecimalLiteral (decimal d)
434 override public string AsString ()
436 return Value.ToString ();
439 public override object GetValue ()
441 return (object) Value;
444 public override Expression DoResolve (EmitContext ec)
446 type = TypeManager.decimal_type;
451 public override void Emit (EmitContext ec)
453 throw new Exception ("Implement me");
457 public class StringLiteral : Literal {
458 public readonly string Value;
460 public StringLiteral (string s)
465 // FIXME: Escape the string.
466 override public string AsString ()
468 return "\"" + Value + "\"";
471 public override object GetValue ()
473 return (object) Value;
476 public override Expression DoResolve (EmitContext ec)
478 type = TypeManager.string_type;
483 public override void Emit (EmitContext ec)
485 ec.ig.Emit (OpCodes.Ldstr, Value);