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 IKVM.Reflection.Emit;
25 using System.Reflection.Emit;
28 namespace Mono.CSharp {
33 // Note: C# specification null-literal is NullLiteral of NullType type
35 public class NullLiteral : NullConstant
38 // Default type of null is an object
40 public NullLiteral (Location loc)
41 : base (InternalType.Null, loc)
45 public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec t, bool expl)
47 if (t.IsGenericParameter) {
48 ec.Report.Error(403, loc,
49 "Cannot convert null to the type parameter `{0}' because it could be a value " +
50 "type. Consider using `default ({0})' instead", t.Name);
54 if (TypeManager.IsValueType (t)) {
55 ec.Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
56 TypeManager.CSharpName(t));
60 base.Error_ValueCannotBeConverted (ec, loc, t, expl);
63 public override bool IsLiteral {
67 public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
69 return System.Linq.Expressions.Expression.Constant (null);
74 // A null literal in a pointer context
76 class NullPointer : NullLiteral {
77 public NullPointer (Location loc):
80 type = TypeManager.object_type;
83 public override void Emit (EmitContext ec)
88 ec.Emit (OpCodes.Ldc_I4_0);
89 ec.Emit (OpCodes.Conv_U);
93 public class BoolLiteral : BoolConstant {
94 public BoolLiteral (bool val, Location loc) : base (val, loc)
98 public override bool IsLiteral {
103 public class CharLiteral : CharConstant {
104 public CharLiteral (char c, Location loc) : base (c, loc)
108 public override bool IsLiteral {
113 public class IntLiteral : IntConstant {
114 public IntLiteral (int l, Location loc) : base (l, loc)
118 public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
121 // The 0 literal can be converted to an enum value
123 if (Value == 0 && TypeManager.IsEnumType (type)) {
124 Constant c = ConvertImplicitly (rc, EnumSpec.GetUnderlyingType (type));
128 return new EnumConstant (c, type).Resolve (rc);
131 return base.ConvertImplicitly (rc, type);
134 public override bool IsLiteral {
139 public class UIntLiteral : UIntConstant {
140 public UIntLiteral (uint l, Location loc) : base (l, loc)
144 public override bool IsLiteral {
149 public class LongLiteral : LongConstant {
150 public LongLiteral (long l, Location loc) : base (l, loc)
154 public override bool IsLiteral {
159 public class ULongLiteral : ULongConstant {
160 public ULongLiteral (ulong l, Location loc) : base (l, loc)
164 public override bool IsLiteral {
169 public class FloatLiteral : FloatConstant {
171 public FloatLiteral (float f, Location loc) : base (f, loc)
175 public override bool IsLiteral {
181 public class DoubleLiteral : DoubleConstant {
182 public DoubleLiteral (double d, Location loc) : base (d, loc)
186 public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
188 if (target == TypeManager.float_type) {
189 Error_664 (ec, loc, "float", "f");
193 if (target == TypeManager.decimal_type) {
194 Error_664 (ec, loc, "decimal", "m");
198 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
201 static void Error_664 (ResolveContext ec, Location loc, string type, string suffix)
203 ec.Report.Error (664, loc,
204 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
208 public override bool IsLiteral {
214 public class DecimalLiteral : DecimalConstant {
215 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
219 public override bool IsLiteral {
224 public class StringLiteral : StringConstant {
225 public StringLiteral (string s, Location loc) : base (s, loc)
229 public override bool IsLiteral {