2 // literal.cs: Literal representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
10 // Notice that during parsing we create objects of type Literal, but the
11 // types are not loaded (thats why the Resolve method has to assign the
12 // type at that point).
14 // Literals differ from the constants in that we know we encountered them
15 // as a literal in the source code (and some extra rules apply there) and
16 // they have to be resolved (since during parsing we have not loaded the
17 // types yet) while constants are created only after types have been loaded
18 // and are fully resolved when born.
22 using System.Reflection;
23 using System.Reflection.Emit;
26 // I put System.Null just so we do not have to special case it on
27 // TypeManager.CSharpName
31 // Represents the Null Type, just used as a placeholder for the type in NullLiteral
37 namespace Mono.CSharp {
40 // Unlike C#, VB.NET doesn't understand "signed byte",
41 // "unsigned short", "unsigned int" and "unsigned long" as
42 // primitive types. The NotDefinedAsPrimitiveType just exists
43 // to accomodate this fact.
45 public class NotDefinedAsPrimitiveType {
49 // The NullType just exists to compare type equality, and for
50 // expressions that might have the `null type'
52 public class NullType {
56 // The null Literal constant
58 public class NullLiteral : Constant {
59 public static readonly NullLiteral Null;
63 Null = new NullLiteral ();
68 eclass = ExprClass.Value;
71 override public string AsString ()
76 public override object GetValue ()
81 public override Expression DoResolve (EmitContext ec)
83 type = TypeManager.null_type;
87 public override void Emit (EmitContext ec)
89 ec.ig.Emit (OpCodes.Ldnull);
92 public override CharConstant ConvertToChar ()
94 return new CharConstant ((char) 0);
97 public override BoolConstant ConvertToBoolean ()
99 return new BoolConstant (false);
102 public override ByteConstant ConvertToByte ()
104 return new ByteConstant ((byte) 0);
107 public override ShortConstant ConvertToShort ()
109 return new ShortConstant ((short) 0);
112 public override DecimalConstant ConvertToDecimal ()
114 return new DecimalConstant ((decimal) 0);
117 public override DoubleConstant ConvertToDouble ()
119 return new DoubleConstant ((double) 0);
122 public override FloatConstant ConvertToFloat ()
124 return new FloatConstant ((float) 0);
127 public override LongConstant ConvertToLong ()
129 return new LongConstant ((long) 0);
132 public override IntConstant ConvertToInt ()
134 return new IntConstant (0);
137 public override bool IsNegative {
143 public override bool IsZeroInteger {
149 // A null literal in a pointer context
151 public class NullPointer : NullLiteral {
152 public new static readonly NullLiteral Null;
154 static NullPointer ()
156 Null = new NullPointer ();
159 private NullPointer ()
161 type = TypeManager.object_type;
164 public override void Emit (EmitContext ec)
166 ILGenerator ig = ec.ig;
168 ig.Emit (OpCodes.Ldc_I4_0);
169 ig.Emit (OpCodes.Conv_U);
173 public class BoolLiteral : BoolConstant {
174 public BoolLiteral (bool val) : base (val)
178 public override Expression DoResolve (EmitContext ec)
180 type = TypeManager.bool_type;
185 public class CharLiteral : CharConstant {
186 public CharLiteral (char c) : base (c)
190 public override Expression DoResolve (EmitContext ec)
192 type = TypeManager.char_type;
197 public class IntLiteral : IntConstant {
198 public static IntLiteral One, Zero;
202 Zero = new IntLiteral (0);
203 One = new IntLiteral (1);
206 public IntLiteral (int l) : base (l)
210 public override Expression DoResolve (EmitContext ec)
212 type = TypeManager.int32_type;
217 public class UIntLiteral : UIntConstant {
218 public UIntLiteral (uint l) : base (l)
222 public override Expression DoResolve (EmitContext ec)
224 type = TypeManager.uint32_type;
229 public class LongLiteral : LongConstant {
230 public LongLiteral (long l) : base (l)
234 public override Expression DoResolve (EmitContext ec)
236 type = TypeManager.int64_type;
242 public class ULongLiteral : ULongConstant {
243 public ULongLiteral (ulong l) : base (l)
247 public override Expression DoResolve (EmitContext ec)
249 type = TypeManager.uint64_type;
254 public class FloatLiteral : FloatConstant {
256 public FloatLiteral (float f) : base (f)
260 public override Expression DoResolve (EmitContext ec)
262 type = TypeManager.float_type;
267 public class DoubleLiteral : DoubleConstant {
268 public DoubleLiteral (double d) : base (d)
272 public override Expression DoResolve (EmitContext ec)
274 type = TypeManager.double_type;
280 public class DecimalLiteral : DecimalConstant {
281 public DecimalLiteral (decimal d) : base (d)
285 public override Expression DoResolve (EmitContext ec)
287 type = TypeManager.decimal_type;
292 public class StringLiteral : StringConstant {
293 public StringLiteral (string s) : base (s)
297 public override Expression DoResolve (EmitContext ec)
299 type = TypeManager.string_type;
308 public class DateLiteral : DateConstant {
309 public DateLiteral (DateTime s) : base (s)
313 public override Expression DoResolve (EmitContext ec)
315 type = TypeManager.date_type;