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 ig.Emit (OpCodes.Ldc_I8, l);
285 public class ULongLiteral : Literal {
286 public readonly ulong Value;
288 public ULongLiteral (ulong l)
293 override public string AsString ()
295 return Value.ToString ();
298 public override Expression DoResolve (TypeContainer tc)
300 type = TypeManager.uint64_type;
305 public override void Emit (EmitContext ec)
307 ILGenerator ig = ec.ig;
309 LongLiteral.EmitLong (ig, unchecked ((long) Value));
313 public class FloatLiteral : Literal {
314 public readonly float Value;
316 public FloatLiteral (float f)
321 override public string AsString ()
323 return Value.ToString ();
326 public override Expression DoResolve (TypeContainer tc)
328 type = TypeManager.float_type;
333 public override void Emit (EmitContext ec)
335 ec.ig.Emit (OpCodes.Ldc_R4, Value);
339 public class DoubleLiteral : Literal {
340 public readonly double Value;
342 public DoubleLiteral (double d)
347 override public string AsString ()
349 return Value.ToString ();
352 public override Expression DoResolve (TypeContainer tc)
354 type = TypeManager.double_type;
359 public override void Emit (EmitContext ec)
361 ec.ig.Emit (OpCodes.Ldc_R8, Value);
365 public class DecimalLiteral : Literal {
366 public readonly decimal Value;
368 public DecimalLiteral (decimal d)
373 override public string AsString ()
375 return Value.ToString ();
378 public override Expression DoResolve (TypeContainer tc)
380 type = TypeManager.decimal_type;
385 public override void Emit (EmitContext ec)
387 throw new Exception ("Implement me");
391 public class StringLiteral : Literal {
394 public StringLiteral (string s)
399 // FIXME: Escape the string.
400 override public string AsString ()
402 return "\"" + s + "\"";
405 public override Expression DoResolve (TypeContainer tc)
407 type = TypeManager.string_type;
412 public override void Emit (EmitContext ec)
414 ec.ig.Emit (OpCodes.Ldstr, s);