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);
215 ig.Emit (OpCodes.Ldc_I4_S, i);
217 ig.Emit (OpCodes.Ldc_I4, i);
223 public class LongLiteral : Literal {
224 public readonly long Value;
226 public LongLiteral (long l)
231 override public string AsString ()
233 return Value.ToString ();
236 public override Expression Resolve (TypeContainer tc)
238 type = TypeManager.int64_type;
243 public override void Emit (EmitContext ec)
245 ILGenerator ig = ec.ig;
247 EmitLong (ig, Value);
250 static public void EmitLong (ILGenerator ig, long l)
252 if (l >= -1 || l < Int32.MaxValue)
253 IntLiteral.EmitInt (ig, (int) l);
255 ig.Emit (OpCodes.Ldc_I8, l);
259 public class FloatLiteral : Literal {
260 public readonly float Value;
262 public FloatLiteral (float f)
267 override public string AsString ()
269 return Value.ToString ();
272 public override Expression Resolve (TypeContainer tc)
274 type = TypeManager.float_type;
279 public override void Emit (EmitContext ec)
281 ec.ig.Emit (OpCodes.Ldc_R4, Value);
285 public class DoubleLiteral : Literal {
286 public readonly double Value;
288 public DoubleLiteral (double d)
293 override public string AsString ()
295 return Value.ToString ();
298 public override Expression Resolve (TypeContainer tc)
300 type = TypeManager.double_type;
305 public override void Emit (EmitContext ec)
307 ec.ig.Emit (OpCodes.Ldc_R8, Value);
311 public class DecimalLiteral : Literal {
314 public DecimalLiteral (decimal d)
319 override public string AsString ()
321 return d.ToString ();
324 public override Expression Resolve (TypeContainer tc)
326 type = TypeManager.decimal_type;
331 public override void Emit (EmitContext ec)
336 public class StringLiteral : Literal {
339 public StringLiteral (string s)
344 // FIXME: Escape the string.
345 override public string AsString ()
347 return "\"" + s + "\"";
350 public override Expression Resolve (TypeContainer tc)
352 type = TypeManager.string_type;
357 public override void Emit (EmitContext ec)
359 ec.ig.Emit (OpCodes.Ldstr, s);