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);
96 if (TypeManager.IsNullableType (targetType))
97 return Nullable.LiftedNull.Create (targetType, loc);
102 public override Constant ConvertImplicitly (Type targetType)
105 // Null literal is of object type
107 if (targetType == TypeManager.object_type)
110 return ConvertExplicitly (false, targetType);
113 public override object GetValue ()
118 public override Constant Increment ()
120 throw new NotSupportedException ();
123 public override bool IsDefaultValue
128 public override bool IsLiteral {
132 public override bool IsNegative {
133 get { return false; }
136 public override bool IsNull {
140 public override bool IsZeroInteger {
146 // A null literal in a pointer context
148 public class NullPointer : NullLiteral {
149 public NullPointer (Location loc):
152 type = TypeManager.object_type;
155 public override void Emit (EmitContext ec)
157 ILGenerator ig = ec.ig;
160 // Emits null pointer
162 ig.Emit (OpCodes.Ldc_I4_0);
163 ig.Emit (OpCodes.Conv_U);
167 public class BoolLiteral : BoolConstant {
168 public BoolLiteral (bool val, Location loc) : base (val, loc)
172 public override Expression DoResolve (EmitContext ec)
174 type = TypeManager.bool_type;
178 public override bool IsLiteral {
183 public class CharLiteral : CharConstant {
184 public CharLiteral (char c, Location loc) : base (c, loc)
188 public override Expression DoResolve (EmitContext ec)
190 type = TypeManager.char_type;
194 public override bool IsLiteral {
199 public class IntLiteral : IntConstant {
200 public IntLiteral (int l, Location loc) : base (l, loc)
204 public override Expression DoResolve (EmitContext ec)
206 type = TypeManager.int32_type;
210 public override Constant ConvertImplicitly (Type type)
213 /// The 0 literal can be converted to an enum value,
215 if (Value == 0 && TypeManager.IsEnumType (type)) {
216 Constant c = ConvertImplicitly (TypeManager.GetEnumUnderlyingType (type));
220 return new EnumConstant (c, type);
222 return base.ConvertImplicitly (type);
225 public override bool IsLiteral {
230 public class UIntLiteral : UIntConstant {
231 public UIntLiteral (uint l, Location loc) : base (l, loc)
235 public override Expression DoResolve (EmitContext ec)
237 type = TypeManager.uint32_type;
241 public override bool IsLiteral {
246 public class LongLiteral : LongConstant {
247 public LongLiteral (long l, Location loc) : base (l, loc)
251 public override Expression DoResolve (EmitContext ec)
253 type = TypeManager.int64_type;
257 public override bool IsLiteral {
262 public class ULongLiteral : ULongConstant {
263 public ULongLiteral (ulong l, Location loc) : base (l, loc)
267 public override Expression DoResolve (EmitContext ec)
269 type = TypeManager.uint64_type;
273 public override bool IsLiteral {
278 public class FloatLiteral : FloatConstant {
280 public FloatLiteral (float f, Location loc) : base (f, loc)
284 public override Expression DoResolve (EmitContext ec)
286 type = TypeManager.float_type;
290 public override bool IsLiteral {
296 public class DoubleLiteral : DoubleConstant {
297 public DoubleLiteral (double d, Location loc) : base (d, loc)
301 public override Expression DoResolve (EmitContext ec)
303 type = TypeManager.double_type;
308 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
310 if (target == TypeManager.float_type) {
311 Error_664 (loc, "float", "f");
315 if (target == TypeManager.decimal_type) {
316 Error_664 (loc, "decimal", "m");
320 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
323 static void Error_664 (Location loc, string type, string suffix)
325 Report.Error (664, loc,
326 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
330 public override bool IsLiteral {
336 public class DecimalLiteral : DecimalConstant {
337 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
341 public override Expression DoResolve (EmitContext ec)
343 type = TypeManager.decimal_type;
347 public override bool IsLiteral {
352 public class StringLiteral : StringConstant {
353 public StringLiteral (string s, Location loc) : base (s, loc)
357 public override Expression DoResolve (EmitContext ec)
359 type = TypeManager.string_type;
364 public override bool IsLiteral {