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;
15 public abstract class Literal : Expression {
17 // This is different from ToString in that ToString
18 // is supposed to be there for debugging purposes,
19 // and is not guarantee to be useful for anything else,
20 // AsString() will provide something that can be used
21 // for round-tripping C# code. Maybe it can be used
22 // for IL assembly as well.
24 public abstract string AsString ();
26 override public string ToString ()
32 // This is used to obtain the actual value of the literal
33 // cast into an object.
35 public abstract object GetValue ();
37 static public string descape (char c)
68 eclass = ExprClass.Value;
72 public class NullLiteral : Literal {
77 override public string AsString ()
82 public override object GetValue ()
87 public override Expression DoResolve (EmitContext ec)
89 type = TypeManager.object_type;
90 eclass = ExprClass.Value;
94 public override void Emit (EmitContext ec)
96 ec.ig.Emit (OpCodes.Ldnull);
100 public class BoolLiteral : Literal {
103 public BoolLiteral (bool val)
108 override public string AsString ()
110 return val ? "true" : "false";
113 public override object GetValue ()
119 public override Expression DoResolve (EmitContext ec)
121 type = TypeManager.bool_type;
126 public override void Emit (EmitContext ec)
129 ec.ig.Emit (OpCodes.Ldc_I4_1);
131 ec.ig.Emit (OpCodes.Ldc_I4_0);
135 public class CharLiteral : Literal {
138 public CharLiteral (char c)
143 override public string AsString ()
145 return "\"" + descape (c) + "\"";
148 public override object GetValue ()
153 public override Expression DoResolve (EmitContext ec)
155 type = TypeManager.char_type;
160 public override void Emit (EmitContext ec)
162 IntLiteral.EmitInt (ec.ig, c);
166 public class IntLiteral : Literal {
167 public readonly int Value;
169 public IntLiteral (int l)
174 override public string AsString ()
176 return Value.ToString ();
179 public override object GetValue ()
181 return (object) Value;
184 public override Expression DoResolve (EmitContext ec)
186 type = TypeManager.int32_type;
191 public override void Emit (EmitContext ec)
193 ILGenerator ig = ec.ig;
198 static public void EmitInt (ILGenerator ig, int i)
202 ig.Emit (OpCodes.Ldc_I4_M1);
206 ig.Emit (OpCodes.Ldc_I4_0);
210 ig.Emit (OpCodes.Ldc_I4_1);
214 ig.Emit (OpCodes.Ldc_I4_2);
218 ig.Emit (OpCodes.Ldc_I4_3);
222 ig.Emit (OpCodes.Ldc_I4_4);
226 ig.Emit (OpCodes.Ldc_I4_5);
230 ig.Emit (OpCodes.Ldc_I4_6);
234 ig.Emit (OpCodes.Ldc_I4_7);
238 ig.Emit (OpCodes.Ldc_I4_8);
242 if (i > 0 && i < 127){
243 ig.Emit (OpCodes.Ldc_I4_S, (sbyte) i);
245 ig.Emit (OpCodes.Ldc_I4, i);
251 public class UIntLiteral : Literal {
252 public readonly uint Value;
254 public UIntLiteral (uint l)
259 override public string AsString ()
261 return Value.ToString ();
264 public override object GetValue ()
266 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 return (object) Value;
303 public override Expression DoResolve (EmitContext ec)
305 type = TypeManager.int64_type;
310 public override void Emit (EmitContext ec)
312 ILGenerator ig = ec.ig;
314 EmitLong (ig, Value);
317 static public void EmitLong (ILGenerator ig, long l)
319 ig.Emit (OpCodes.Ldc_I8, l);
323 public class ULongLiteral : Literal {
324 public readonly ulong Value;
326 public ULongLiteral (ulong l)
331 override public string AsString ()
333 return Value.ToString ();
336 public override object GetValue ()
338 return (object) Value;
341 public override Expression DoResolve (EmitContext ec)
343 type = TypeManager.uint64_type;
348 public override void Emit (EmitContext ec)
350 ILGenerator ig = ec.ig;
352 LongLiteral.EmitLong (ig, unchecked ((long) Value));
356 public class FloatLiteral : Literal {
357 public readonly float Value;
359 public FloatLiteral (float f)
364 override public string AsString ()
366 return Value.ToString ();
369 public override object GetValue ()
371 return (object) Value;
374 public override Expression DoResolve (EmitContext ec)
376 type = TypeManager.float_type;
381 public override void Emit (EmitContext ec)
383 ec.ig.Emit (OpCodes.Ldc_R4, Value);
387 public class DoubleLiteral : Literal {
388 public readonly double Value;
390 public DoubleLiteral (double d)
395 override public string AsString ()
397 return Value.ToString ();
400 public override object GetValue ()
402 return (object) Value;
405 public override Expression DoResolve (EmitContext ec)
407 type = TypeManager.double_type;
412 public override void Emit (EmitContext ec)
414 ec.ig.Emit (OpCodes.Ldc_R8, Value);
418 public class DecimalLiteral : Literal {
419 public readonly decimal Value;
421 public DecimalLiteral (decimal d)
426 override public string AsString ()
428 return Value.ToString ();
431 public override object GetValue ()
433 return (object) Value;
436 public override Expression DoResolve (EmitContext ec)
438 type = TypeManager.decimal_type;
443 public override void Emit (EmitContext ec)
445 throw new Exception ("Implement me");
449 public class StringLiteral : Literal {
452 public StringLiteral (string s)
457 // FIXME: Escape the string.
458 override public string AsString ()
460 return "\"" + s + "\"";
463 public override object GetValue ()
468 public override Expression DoResolve (EmitContext ec)
470 type = TypeManager.string_type;
475 public override void Emit (EmitContext ec)
477 ec.ig.Emit (OpCodes.Ldstr, s);