2 // literal.cs: Literal representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
8 // Copyright 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 CreateExpressionTree (EmitContext ec)
46 // HACK: change type to be object
47 type = TypeManager.object_type;
48 return base.CreateExpressionTree (ec);
51 public override Expression DoResolve (EmitContext ec)
56 public override void Emit (EmitContext ec)
58 ec.ig.Emit (OpCodes.Ldnull);
61 public override string ExprClassName {
63 return GetSignatureForError ();
67 public override string GetSignatureForError ()
72 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type t, bool expl)
74 if (TypeManager.IsGenericParameter (t)) {
75 Report.Error(403, loc,
76 "Cannot convert null to the type parameter `{0}' because it could be a value " +
77 "type. Consider using `default ({0})' instead", t.Name);
79 Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
80 TypeManager.CSharpName(t));
84 public override Constant ConvertImplicitly (Type targetType)
86 // FIXME: Null literal should be of object type
87 //if (targetType == TypeManager.object_type)
90 if (targetType.IsPointer)
91 return new EmptyConstantCast (NullPointer.Null, targetType);
93 if (TypeManager.IsReferenceType (targetType))
94 return new EmptyConstantCast (this, targetType);
99 public override object GetValue ()
104 public override Constant Increment ()
106 throw new NotSupportedException ();
109 public override bool IsDefaultValue
114 public override bool IsLiteral {
118 public override bool IsNegative {
119 get { return false; }
122 public override bool IsNull {
126 public override bool IsZeroInteger {
130 public override Constant ConvertExplicitly(bool inCheckedContext, Type target_type)
132 if (!TypeManager.IsValueType (target_type))
133 return new EmptyConstantCast (this, target_type);
140 // A null literal in a pointer context
142 public class NullPointer : NullLiteral {
143 public static readonly NullLiteral Null = new NullPointer ();
145 private NullPointer ():
148 type = TypeManager.object_type;
151 public override void Emit (EmitContext ec)
153 ILGenerator ig = ec.ig;
155 // TODO: why not use Ldnull instead ?
156 ig.Emit (OpCodes.Ldc_I4_0);
157 ig.Emit (OpCodes.Conv_U);
161 public class BoolLiteral : BoolConstant {
162 public BoolLiteral (bool val, Location loc) : base (val, loc)
166 public override Expression DoResolve (EmitContext ec)
168 type = TypeManager.bool_type;
172 public override bool IsLiteral {
177 public class CharLiteral : CharConstant {
178 public CharLiteral (char c, Location loc) : base (c, loc)
182 public override Expression DoResolve (EmitContext ec)
184 type = TypeManager.char_type;
188 public override bool IsLiteral {
193 public class IntLiteral : IntConstant {
194 public IntLiteral (int l, Location loc) : base (l, loc)
198 public override Expression DoResolve (EmitContext ec)
200 type = TypeManager.int32_type;
204 public override Constant ConvertImplicitly (Type type)
207 /// The 0 literal can be converted to an enum value,
209 if (Value == 0 && TypeManager.IsEnumType (type)) {
210 Constant c = ConvertImplicitly (TypeManager.GetEnumUnderlyingType (type));
214 return new EnumConstant (c, type);
216 return base.ConvertImplicitly (type);
219 public override bool IsLiteral {
224 public class UIntLiteral : UIntConstant {
225 public UIntLiteral (uint l, Location loc) : base (l, loc)
229 public override Expression DoResolve (EmitContext ec)
231 type = TypeManager.uint32_type;
235 public override bool IsLiteral {
240 public class LongLiteral : LongConstant {
241 public LongLiteral (long l, Location loc) : base (l, loc)
245 public override Expression DoResolve (EmitContext ec)
247 type = TypeManager.int64_type;
251 public override bool IsLiteral {
256 public class ULongLiteral : ULongConstant {
257 public ULongLiteral (ulong l, Location loc) : base (l, loc)
261 public override Expression DoResolve (EmitContext ec)
263 type = TypeManager.uint64_type;
267 public override bool IsLiteral {
272 public class FloatLiteral : FloatConstant {
274 public FloatLiteral (float f, Location loc) : base (f, loc)
278 public override Expression DoResolve (EmitContext ec)
280 type = TypeManager.float_type;
284 public override bool IsLiteral {
290 public class DoubleLiteral : DoubleConstant {
291 public DoubleLiteral (double d, Location loc) : base (d, loc)
295 public override Expression DoResolve (EmitContext ec)
297 type = TypeManager.double_type;
302 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
304 if (target == TypeManager.float_type) {
305 Error_664 (loc, "float", "f");
309 if (target == TypeManager.decimal_type) {
310 Error_664 (loc, "decimal", "m");
314 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
317 static void Error_664 (Location loc, string type, string suffix)
319 Report.Error (664, loc,
320 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
324 public override bool IsLiteral {
330 public class DecimalLiteral : DecimalConstant {
331 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
335 public override Expression DoResolve (EmitContext ec)
337 type = TypeManager.decimal_type;
341 public override bool IsLiteral {
346 public class StringLiteral : StringConstant {
347 public StringLiteral (string s, Location loc) : base (s, loc)
351 public override Expression DoResolve (EmitContext ec)
353 type = TypeManager.string_type;
358 public override bool IsLiteral {