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)
61 public class NullLiteral : Literal {
66 override public string AsString ()
71 public override Expression Resolve (TypeContainer tc)
73 eclass = ExprClass.Value;
74 type = TypeManager.object_type;
79 public override void Emit (EmitContext ec)
81 ec.ig.Emit (OpCodes.Ldnull);
85 public class BoolLiteral : Literal {
86 static Type bool_type = Type.GetType ("System.Bool");
89 public BoolLiteral (bool val)
94 override public string AsString ()
96 return val ? "true" : "false";
99 public override Expression Resolve (TypeContainer tc)
101 eclass = ExprClass.Value;
107 public override void Emit (EmitContext ec)
110 ec.ig.Emit (OpCodes.Ldc_I4_1);
112 ec.ig.Emit (OpCodes.Ldc_I4_0);
116 public class CharLiteral : Literal {
119 public CharLiteral (char c)
124 override public string AsString ()
126 return "\"" + descape (c) + "\"";
129 public override Expression Resolve (TypeContainer tc)
131 eclass = ExprClass.Value;
132 type = TypeManager.char_type;
137 public override void Emit (EmitContext ec)
139 IntLiteral.EmitInt (ec.ig, c);
143 public class IntLiteral : Literal {
146 public IntLiteral (int l)
151 override public string AsString ()
153 return i.ToString ();
156 public override Expression Resolve (TypeContainer tc)
158 eclass = ExprClass.Value;
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);
216 ig.Emit (OpCodes.Ldc_I4_S, i);
218 ig.Emit (OpCodes.Ldc_I4, i);
224 public class FloatLiteral : Literal {
227 public FloatLiteral (float f)
232 override public string AsString ()
234 return f.ToString ();
237 public override Expression Resolve (TypeContainer tc)
239 eclass = ExprClass.Value;
240 type = TypeManager.float_type;
245 public override void Emit (EmitContext ec)
247 ec.ig.Emit (OpCodes.Ldc_R4, f);
251 public class DoubleLiteral : Literal {
254 public DoubleLiteral (double d)
259 override public string AsString ()
261 return d.ToString ();
264 public override Expression Resolve (TypeContainer tc)
266 eclass = ExprClass.Value;
267 type = TypeManager.double_type;
272 public override void Emit (EmitContext ec)
274 ec.ig.Emit (OpCodes.Ldc_R8, d);
278 public class DecimalLiteral : Literal {
281 public DecimalLiteral (decimal d)
286 override public string AsString ()
288 return d.ToString ();
291 public override Expression Resolve (TypeContainer tc)
293 eclass = ExprClass.Value;
294 type = TypeManager.decimal_type;
299 public override void Emit (EmitContext ec)
304 public class StringLiteral : Literal {
307 public StringLiteral (string s)
312 // FIXME: Escape the string.
313 override public string AsString ()
315 return "\"" + s + "\"";
318 public override Expression Resolve (TypeContainer tc)
320 eclass = ExprClass.Value;
321 type = TypeManager.string_type;
326 public override void Emit (EmitContext ec)
328 ec.ig.Emit (OpCodes.Ldstr, s);