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 ConvertExplicitly (bool inCheckedContext, Type targetType)
86 if (targetType.IsPointer)
87 return new EmptyConstantCast (new NullPointer (loc), targetType);
89 // Exlude internal compiler types
90 if (targetType == TypeManager.anonymous_method_type)
93 if (TypeManager.IsReferenceType (targetType))
94 return new EmptyConstantCast (this, targetType);
99 public override Constant ConvertImplicitly (Type targetType)
102 // Null literal is of object type
104 if (targetType == TypeManager.object_type)
107 return ConvertExplicitly (false, targetType);
110 public override object GetValue ()
115 public override Constant Increment ()
117 throw new NotSupportedException ();
120 public override bool IsDefaultValue
125 public override bool IsLiteral {
129 public override bool IsNegative {
130 get { return false; }
133 public override bool IsNull {
137 public override bool IsZeroInteger {
143 // A null literal in a pointer context
145 public class NullPointer : NullLiteral {
146 public NullPointer (Location loc):
149 type = TypeManager.object_type;
152 public override void Emit (EmitContext ec)
154 ILGenerator ig = ec.ig;
157 // Emits null pointer
159 ig.Emit (OpCodes.Ldc_I4_0);
160 ig.Emit (OpCodes.Conv_U);
164 public class BoolLiteral : BoolConstant {
165 public BoolLiteral (bool val, Location loc) : base (val, loc)
169 public override Expression DoResolve (EmitContext ec)
171 type = TypeManager.bool_type;
175 public override bool IsLiteral {
180 public class CharLiteral : CharConstant {
181 public CharLiteral (char c, Location loc) : base (c, loc)
185 public override Expression DoResolve (EmitContext ec)
187 type = TypeManager.char_type;
191 public override bool IsLiteral {
196 public class IntLiteral : IntConstant {
197 public IntLiteral (int l, Location loc) : base (l, loc)
201 public override Expression DoResolve (EmitContext ec)
203 type = TypeManager.int32_type;
207 public override Constant ConvertImplicitly (Type type)
210 /// The 0 literal can be converted to an enum value,
212 if (Value == 0 && TypeManager.IsEnumType (type)) {
213 Constant c = ConvertImplicitly (TypeManager.GetEnumUnderlyingType (type));
217 return new EnumConstant (c, type);
219 return base.ConvertImplicitly (type);
222 public override bool IsLiteral {
227 public class UIntLiteral : UIntConstant {
228 public UIntLiteral (uint l, Location loc) : base (l, loc)
232 public override Expression DoResolve (EmitContext ec)
234 type = TypeManager.uint32_type;
238 public override bool IsLiteral {
243 public class LongLiteral : LongConstant {
244 public LongLiteral (long l, Location loc) : base (l, loc)
248 public override Expression DoResolve (EmitContext ec)
250 type = TypeManager.int64_type;
254 public override bool IsLiteral {
259 public class ULongLiteral : ULongConstant {
260 public ULongLiteral (ulong l, Location loc) : base (l, loc)
264 public override Expression DoResolve (EmitContext ec)
266 type = TypeManager.uint64_type;
270 public override bool IsLiteral {
275 public class FloatLiteral : FloatConstant {
277 public FloatLiteral (float f, Location loc) : base (f, loc)
281 public override Expression DoResolve (EmitContext ec)
283 type = TypeManager.float_type;
287 public override bool IsLiteral {
293 public class DoubleLiteral : DoubleConstant {
294 public DoubleLiteral (double d, Location loc) : base (d, loc)
298 public override Expression DoResolve (EmitContext ec)
300 type = TypeManager.double_type;
305 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
307 if (target == TypeManager.float_type) {
308 Error_664 (loc, "float", "f");
312 if (target == TypeManager.decimal_type) {
313 Error_664 (loc, "decimal", "m");
317 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
320 static void Error_664 (Location loc, string type, string suffix)
322 Report.Error (664, loc,
323 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
327 public override bool IsLiteral {
333 public class DecimalLiteral : DecimalConstant {
334 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
338 public override Expression DoResolve (EmitContext ec)
340 type = TypeManager.decimal_type;
344 public override bool IsLiteral {
349 public class StringLiteral : StringConstant {
350 public StringLiteral (string s, Location loc) : base (s, loc)
354 public override Expression DoResolve (EmitContext ec)
356 type = TypeManager.string_type;
361 public override bool IsLiteral {