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 Resolve (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 Resolve (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 Resolve (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 Resolve (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 Resolve (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 Resolve (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 FloatLiteral : Literal {
289 public readonly float Value;
291 public FloatLiteral (float f)
296 override public string AsString ()
298 return Value.ToString ();
301 public override Expression Resolve (TypeContainer tc)
303 type = TypeManager.float_type;
308 public override void Emit (EmitContext ec)
310 ec.ig.Emit (OpCodes.Ldc_R4, Value);
314 public class DoubleLiteral : Literal {
315 public readonly double Value;
317 public DoubleLiteral (double d)
322 override public string AsString ()
324 return Value.ToString ();
327 public override Expression Resolve (TypeContainer tc)
329 type = TypeManager.double_type;
334 public override void Emit (EmitContext ec)
336 ec.ig.Emit (OpCodes.Ldc_R8, Value);
340 public class DecimalLiteral : Literal {
341 public readonly decimal Value;
343 public DecimalLiteral (decimal d)
348 override public string AsString ()
350 return Value.ToString ();
353 public override Expression Resolve (TypeContainer tc)
355 type = TypeManager.decimal_type;
360 public override void Emit (EmitContext ec)
362 throw new Exception ("Implement me");
366 public class StringLiteral : Literal {
369 public StringLiteral (string s)
374 // FIXME: Escape the string.
375 override public string AsString ()
377 return "\"" + s + "\"";
380 public override Expression Resolve (TypeContainer tc)
382 type = TypeManager.string_type;
387 public override void Emit (EmitContext ec)
389 ec.ig.Emit (OpCodes.Ldstr, s);