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 (EmitContext ec)
78 type = TypeManager.object_type;
79 eclass = ExprClass.Value;
83 public override void Emit (EmitContext ec)
85 ec.ig.Emit (OpCodes.Ldnull);
89 public class BoolLiteral : Literal {
92 public BoolLiteral (bool val)
97 override public string AsString ()
99 return val ? "true" : "false";
102 public override Expression DoResolve (EmitContext ec)
104 type = TypeManager.bool_type;
109 public override void Emit (EmitContext ec)
112 ec.ig.Emit (OpCodes.Ldc_I4_1);
114 ec.ig.Emit (OpCodes.Ldc_I4_0);
118 public class CharLiteral : Literal {
121 public CharLiteral (char c)
126 override public string AsString ()
128 return "\"" + descape (c) + "\"";
131 public override Expression DoResolve (EmitContext ec)
133 type = TypeManager.char_type;
138 public override void Emit (EmitContext ec)
140 IntLiteral.EmitInt (ec.ig, c);
144 public class IntLiteral : Literal {
145 public readonly int Value;
147 public IntLiteral (int l)
152 override public string AsString ()
154 return Value.ToString ();
157 public override Expression DoResolve (EmitContext ec)
159 type = TypeManager.int32_type;
164 public override void Emit (EmitContext ec)
166 ILGenerator ig = ec.ig;
171 static public void EmitInt (ILGenerator ig, int i)
175 ig.Emit (OpCodes.Ldc_I4_M1);
179 ig.Emit (OpCodes.Ldc_I4_0);
183 ig.Emit (OpCodes.Ldc_I4_1);
187 ig.Emit (OpCodes.Ldc_I4_2);
191 ig.Emit (OpCodes.Ldc_I4_3);
195 ig.Emit (OpCodes.Ldc_I4_4);
199 ig.Emit (OpCodes.Ldc_I4_5);
203 ig.Emit (OpCodes.Ldc_I4_6);
207 ig.Emit (OpCodes.Ldc_I4_7);
211 ig.Emit (OpCodes.Ldc_I4_8);
215 if (i > 0 && i < 127){
216 ig.Emit (OpCodes.Ldc_I4_S, (sbyte) i);
218 ig.Emit (OpCodes.Ldc_I4, i);
224 public class UIntLiteral : Literal {
225 public readonly uint Value;
227 public UIntLiteral (uint l)
232 override public string AsString ()
234 return Value.ToString ();
237 public override Expression DoResolve (EmitContext ec)
239 type = TypeManager.uint32_type;
244 public override void Emit (EmitContext ec)
246 ILGenerator ig = ec.ig;
248 IntLiteral.EmitInt (ig, unchecked ((int) Value));
253 public class LongLiteral : Literal {
254 public readonly long Value;
256 public LongLiteral (long l)
261 override public string AsString ()
263 return Value.ToString ();
266 public override Expression DoResolve (EmitContext ec)
268 type = TypeManager.int64_type;
273 public override void Emit (EmitContext ec)
275 ILGenerator ig = ec.ig;
277 EmitLong (ig, Value);
280 static public void EmitLong (ILGenerator ig, long l)
282 ig.Emit (OpCodes.Ldc_I8, l);
286 public class ULongLiteral : Literal {
287 public readonly ulong Value;
289 public ULongLiteral (ulong l)
294 override public string AsString ()
296 return Value.ToString ();
299 public override Expression DoResolve (EmitContext ec)
301 type = TypeManager.uint64_type;
306 public override void Emit (EmitContext ec)
308 ILGenerator ig = ec.ig;
310 LongLiteral.EmitLong (ig, unchecked ((long) Value));
314 public class FloatLiteral : Literal {
315 public readonly float Value;
317 public FloatLiteral (float f)
322 override public string AsString ()
324 return Value.ToString ();
327 public override Expression DoResolve (EmitContext ec)
329 type = TypeManager.float_type;
334 public override void Emit (EmitContext ec)
336 ec.ig.Emit (OpCodes.Ldc_R4, Value);
340 public class DoubleLiteral : Literal {
341 public readonly double Value;
343 public DoubleLiteral (double d)
348 override public string AsString ()
350 return Value.ToString ();
353 public override Expression DoResolve (EmitContext ec)
355 type = TypeManager.double_type;
360 public override void Emit (EmitContext ec)
362 ec.ig.Emit (OpCodes.Ldc_R8, Value);
366 public class DecimalLiteral : Literal {
367 public readonly decimal Value;
369 public DecimalLiteral (decimal d)
374 override public string AsString ()
376 return Value.ToString ();
379 public override Expression DoResolve (EmitContext ec)
381 type = TypeManager.decimal_type;
386 public override void Emit (EmitContext ec)
388 throw new Exception ("Implement me");
392 public class StringLiteral : Literal {
395 public StringLiteral (string s)
400 // FIXME: Escape the string.
401 override public string AsString ()
403 return "\"" + s + "\"";
406 public override Expression DoResolve (EmitContext ec)
408 type = TypeManager.string_type;
413 public override void Emit (EmitContext ec)
415 ec.ig.Emit (OpCodes.Ldstr, s);