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 // The NullType just exists to compare type equality, and for
41 // expressions that might have the `null type'
43 public class NullType {
47 // The null Literal constant
49 public class NullLiteral : Constant {
50 public static readonly NullLiteral Null;
54 Null = new NullLiteral ();
59 eclass = ExprClass.Value;
62 override public string AsString ()
67 public override object GetValue ()
72 public override Expression DoResolve (EmitContext ec)
74 type = TypeManager.null_type;
78 public override void Emit (EmitContext ec)
80 ec.ig.Emit (OpCodes.Ldnull);
83 public override Constant Increment ()
85 throw new NotSupportedException ();
88 public override bool IsDefaultValue {
94 public override bool IsNegative {
100 public override bool IsZeroInteger {
104 public override string GetSignatureForError()
109 public override void Error_ValueCannotBeConverted (Location loc, Type t)
111 Report.Error (37, loc, "Cannot convert null to `{0}' because it is a value type",
112 TypeManager.CSharpName (t));
115 public override Constant ToType (Type type, Location loc)
117 if (!type.IsValueType && !TypeManager.IsEnumType (type))
118 return NullLiteral.Null;
120 return base.ToType (type, loc);
126 // A null literal in a pointer context
128 public class NullPointer : NullLiteral {
129 public new static readonly NullLiteral Null;
131 static NullPointer ()
133 Null = new NullPointer ();
136 private NullPointer ()
138 type = TypeManager.object_type;
141 public override void Emit (EmitContext ec)
143 ILGenerator ig = ec.ig;
145 ig.Emit (OpCodes.Ldc_I4_0);
146 ig.Emit (OpCodes.Conv_U);
150 public class BoolLiteral : BoolConstant {
151 public BoolLiteral (bool val) : base (val)
155 public override Expression DoResolve (EmitContext ec)
157 type = TypeManager.bool_type;
162 public class CharLiteral : CharConstant {
163 public CharLiteral (char c) : base (c)
167 public override Expression DoResolve (EmitContext ec)
169 type = TypeManager.char_type;
174 public class IntLiteral : IntConstant {
175 public static IntLiteral One, Zero;
179 Zero = new IntLiteral (0);
180 One = new IntLiteral (1);
183 public IntLiteral (int l) : base (l)
187 public override Expression DoResolve (EmitContext ec)
189 type = TypeManager.int32_type;
194 public class UIntLiteral : UIntConstant {
195 public UIntLiteral (uint l) : base (l)
199 public override Expression DoResolve (EmitContext ec)
201 type = TypeManager.uint32_type;
206 public class LongLiteral : LongConstant {
207 public LongLiteral (long l) : base (l)
211 public override Expression DoResolve (EmitContext ec)
213 type = TypeManager.int64_type;
219 public class ULongLiteral : ULongConstant {
220 public ULongLiteral (ulong l) : base (l)
224 public override Expression DoResolve (EmitContext ec)
226 type = TypeManager.uint64_type;
231 public class FloatLiteral : FloatConstant {
233 public FloatLiteral (float f) : base (f)
237 public override Expression DoResolve (EmitContext ec)
239 type = TypeManager.float_type;
244 public class DoubleLiteral : DoubleConstant {
245 public DoubleLiteral (double d) : base (d)
249 public override Expression DoResolve (EmitContext ec)
251 type = TypeManager.double_type;
257 public class DecimalLiteral : DecimalConstant {
258 public DecimalLiteral (decimal d) : base (d)
262 public override Expression DoResolve (EmitContext ec)
264 type = TypeManager.decimal_type;
269 public class StringLiteral : StringConstant {
270 public StringLiteral (string s) : base (s)
274 public override Expression DoResolve (EmitContext ec)
276 type = TypeManager.string_type;