2 // literal.cs: Literal representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
8 // (C) 2001 Ximian, Inc.
11 // Notice that during parsing we create objects of type Literal, but the
12 // types are not loaded (thats why the Resolve method has to assign the
13 // type at that point).
15 // Literals differ from the constants in that we know we encountered them
16 // as a literal in the source code (and some extra rules apply there) and
17 // they have to be resolved (since during parsing we have not loaded the
18 // types yet) while constants are created only after types have been loaded
19 // and are fully resolved when born.
23 using System.Reflection;
24 using System.Reflection.Emit;
26 namespace Mono.CSharp {
31 public class NullLiteral : Constant {
32 public NullLiteral (Location loc):
35 eclass = ExprClass.Value;
36 type = typeof (NullLiteral);
39 override public string AsString ()
41 return GetSignatureForError ();
44 public override Expression DoResolve (EmitContext ec)
49 public override void Emit (EmitContext ec)
51 ec.ig.Emit (OpCodes.Ldnull);
54 public override string ExprClassName {
56 return GetSignatureForError ();
60 public override string GetSignatureForError ()
65 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type t, bool expl)
67 if (TypeManager.IsGenericParameter (t)) {
68 Report.Error(403, loc,
69 "Cannot convert null to the type parameter `{0}' because it could be a value " +
70 "type. Consider using `default ({0})' instead", t.Name);
72 Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
73 TypeManager.CSharpName(t));
77 public override Constant ConvertImplicitly (Type targetType)
79 if (targetType.IsPointer)
80 return new EmptyConstantCast (NullPointer.Null, targetType);
82 if (TypeManager.IsReferenceType (targetType))
83 return new EmptyConstantCast (this, targetType);
88 public override object GetValue ()
93 public override Constant Increment ()
95 throw new NotSupportedException ();
98 public override bool IsDefaultValue
103 public override bool IsNegative
105 get { return false; }
108 public override bool IsNull {
112 public override bool IsZeroInteger
117 public override Constant ConvertExplicitly(bool inCheckedContext, Type target_type)
119 if (!TypeManager.IsValueType (target_type))
120 return new EmptyConstantCast (this, target_type);
127 // A null literal in a pointer context
129 public class NullPointer : NullLiteral {
130 public static readonly NullLiteral Null = new NullPointer ();
132 private NullPointer ():
135 type = TypeManager.object_type;
138 public override void Emit (EmitContext ec)
140 ILGenerator ig = ec.ig;
142 // TODO: why not use Ldnull instead ?
143 ig.Emit (OpCodes.Ldc_I4_0);
144 ig.Emit (OpCodes.Conv_U);
148 public class BoolLiteral : BoolConstant {
149 public BoolLiteral (bool val, Location loc) : base (val, loc)
153 public override Expression DoResolve (EmitContext ec)
155 type = TypeManager.bool_type;
160 public class CharLiteral : CharConstant {
161 public CharLiteral (char c, Location loc) : base (c, loc)
165 public override Expression DoResolve (EmitContext ec)
167 type = TypeManager.char_type;
172 public class IntLiteral : IntConstant {
173 public IntLiteral (int l, Location loc) : base (l, loc)
177 public override Expression DoResolve (EmitContext ec)
179 type = TypeManager.int32_type;
183 public override Constant ConvertImplicitly (Type type)
186 /// The 0 literal can be converted to an enum value,
188 if (Value == 0 && TypeManager.IsEnumType (type)) {
189 Constant c = ConvertImplicitly (TypeManager.EnumToUnderlying (type));
193 return new EnumConstant (c, type);
195 return base.ConvertImplicitly (type);
200 public class UIntLiteral : UIntConstant {
201 public UIntLiteral (uint l, Location loc) : base (l, loc)
205 public override Expression DoResolve (EmitContext ec)
207 type = TypeManager.uint32_type;
212 public class LongLiteral : LongConstant {
213 public LongLiteral (long l, Location loc) : base (l, loc)
217 public override Expression DoResolve (EmitContext ec)
219 type = TypeManager.int64_type;
225 public class ULongLiteral : ULongConstant {
226 public ULongLiteral (ulong l, Location loc) : base (l, loc)
230 public override Expression DoResolve (EmitContext ec)
232 type = TypeManager.uint64_type;
237 public class FloatLiteral : FloatConstant {
239 public FloatLiteral (float f, Location loc) : base (f, loc)
243 public override Expression DoResolve (EmitContext ec)
245 type = TypeManager.float_type;
250 public class DoubleLiteral : DoubleConstant {
251 public DoubleLiteral (double d, Location loc) : base (d, loc)
255 public override Expression DoResolve (EmitContext ec)
257 type = TypeManager.double_type;
262 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
264 if (target == TypeManager.float_type) {
265 Error_664 (loc, "float", "f");
269 if (target == TypeManager.decimal_type) {
270 Error_664 (loc, "decimal", "m");
274 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
277 static void Error_664 (Location loc, string type, string suffix)
279 Report.Error (664, loc,
280 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
285 public class DecimalLiteral : DecimalConstant {
286 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
290 public override Expression DoResolve (EmitContext ec)
292 type = TypeManager.decimal_type;
297 public class StringLiteral : StringConstant {
298 public StringLiteral (string s, Location loc) : base (s, loc)
302 public override Expression DoResolve (EmitContext ec)
304 type = TypeManager.string_type;