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 ()
31 static public string descape (char c)
62 eclass = ExprClass.Value;
66 public class NullLiteral : Literal {
71 override public string AsString ()
76 public override Expression DoResolve (TypeContainer tc)
78 type = TypeManager.object_type;
82 public override void Emit (EmitContext ec)
84 ec.ig.Emit (OpCodes.Ldnull);
88 public class BoolLiteral : Literal {
91 public BoolLiteral (bool val)
96 override public string AsString ()
98 return val ? "true" : "false";
101 public override Expression DoResolve (TypeContainer tc)
103 type = TypeManager.bool_type;
108 public override void Emit (EmitContext ec)
111 ec.ig.Emit (OpCodes.Ldc_I4_1);
113 ec.ig.Emit (OpCodes.Ldc_I4_0);
117 public class CharLiteral : Literal {
120 public CharLiteral (char c)
125 override public string AsString ()
127 return "\"" + descape (c) + "\"";
130 public override Expression DoResolve (TypeContainer tc)
132 type = TypeManager.char_type;
137 public override void Emit (EmitContext ec)
139 IntLiteral.EmitInt (ec.ig, c);
143 public class IntLiteral : Literal {
144 public readonly int Value;
146 public IntLiteral (int l)
151 override public string AsString ()
153 return Value.ToString ();
156 public override Expression DoResolve (TypeContainer tc)
158 type = TypeManager.int32_type;
163 public override void Emit (EmitContext ec)
165 ILGenerator ig = ec.ig;
170 static public void EmitInt (ILGenerator ig, int i)
174 ig.Emit (OpCodes.Ldc_I4_M1);
178 ig.Emit (OpCodes.Ldc_I4_0);
182 ig.Emit (OpCodes.Ldc_I4_1);
186 ig.Emit (OpCodes.Ldc_I4_2);
190 ig.Emit (OpCodes.Ldc_I4_3);
194 ig.Emit (OpCodes.Ldc_I4_4);
198 ig.Emit (OpCodes.Ldc_I4_5);
202 ig.Emit (OpCodes.Ldc_I4_6);
206 ig.Emit (OpCodes.Ldc_I4_7);
210 ig.Emit (OpCodes.Ldc_I4_8);
214 if (i > 0 && i < 127){
215 ig.Emit (OpCodes.Ldc_I4_S, (sbyte) i);
217 ig.Emit (OpCodes.Ldc_I4, i);
223 public class UIntLiteral : Literal {
224 public readonly uint Value;
226 public UIntLiteral (uint l)
231 override public string AsString ()
233 return Value.ToString ();
236 public override Expression DoResolve (TypeContainer tc)
238 type = TypeManager.uint32_type;
243 public override void Emit (EmitContext ec)
245 ILGenerator ig = ec.ig;
247 IntLiteral.EmitInt (ig, unchecked ((int) Value));
252 public class LongLiteral : Literal {
253 public readonly long Value;
255 public LongLiteral (long l)
260 override public string AsString ()
262 return Value.ToString ();
265 public override Expression DoResolve (TypeContainer tc)
267 type = TypeManager.int64_type;
272 public override void Emit (EmitContext ec)
274 ILGenerator ig = ec.ig;
276 EmitLong (ig, Value);
279 static public void EmitLong (ILGenerator ig, long l)
281 if (l >= -1 || l < Int32.MaxValue)
282 IntLiteral.EmitInt (ig, (int) l);
284 ig.Emit (OpCodes.Ldc_I8, l);
288 public class ULongLiteral : Literal {
289 public readonly ulong Value;
291 public ULongLiteral (ulong l)
296 override public string AsString ()
298 return Value.ToString ();
301 public override Expression DoResolve (TypeContainer tc)
303 type = TypeManager.uint64_type;
308 public override void Emit (EmitContext ec)
310 ILGenerator ig = ec.ig;
312 LongLiteral.EmitLong (ig, unchecked ((long) Value));
316 public class FloatLiteral : Literal {
317 public readonly float Value;
319 public FloatLiteral (float f)
324 override public string AsString ()
326 return Value.ToString ();
329 public override Expression DoResolve (TypeContainer tc)
331 type = TypeManager.float_type;
336 public override void Emit (EmitContext ec)
338 ec.ig.Emit (OpCodes.Ldc_R4, Value);
342 public class DoubleLiteral : Literal {
343 public readonly double Value;
345 public DoubleLiteral (double d)
350 override public string AsString ()
352 return Value.ToString ();
355 public override Expression DoResolve (TypeContainer tc)
357 type = TypeManager.double_type;
362 public override void Emit (EmitContext ec)
364 ec.ig.Emit (OpCodes.Ldc_R8, Value);
368 public class DecimalLiteral : Literal {
369 public readonly decimal Value;
371 public DecimalLiteral (decimal d)
376 override public string AsString ()
378 return Value.ToString ();
381 public override Expression DoResolve (TypeContainer tc)
383 type = TypeManager.decimal_type;
388 public override void Emit (EmitContext ec)
390 throw new Exception ("Implement me");
394 public class StringLiteral : Literal {
397 public StringLiteral (string s)
402 // FIXME: Escape the string.
403 override public string AsString ()
405 return "\"" + s + "\"";
408 public override Expression DoResolve (TypeContainer tc)
410 type = TypeManager.string_type;
415 public override void Emit (EmitContext ec)
417 ec.ig.Emit (OpCodes.Ldstr, s);