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 string GetValueAsLiteral ()
68 public override bool IsLiteral {
72 public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
74 return System.Linq.Expressions.Expression.Constant (null);
79 // A null literal in a pointer context
81 class NullPointer : NullLiteral {
82 public NullPointer (Location loc):
85 type = TypeManager.object_type;
88 public override void Emit (EmitContext ec)
93 ec.Emit (OpCodes.Ldc_I4_0);
94 ec.Emit (OpCodes.Conv_U);
98 public class BoolLiteral : BoolConstant {
99 public BoolLiteral (bool val, Location loc) : base (val, loc)
103 public override bool IsLiteral {
108 public class CharLiteral : CharConstant {
109 public CharLiteral (char c, Location loc) : base (c, loc)
113 public override bool IsLiteral {
118 public class IntLiteral : IntConstant {
119 public IntLiteral (int l, Location loc) : base (l, loc)
123 public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
126 // The 0 literal can be converted to an enum value
128 if (Value == 0 && TypeManager.IsEnumType (type)) {
129 Constant c = ConvertImplicitly (rc, EnumSpec.GetUnderlyingType (type));
133 return new EnumConstant (c, type).Resolve (rc);
136 return base.ConvertImplicitly (rc, type);
139 public override bool IsLiteral {
144 public class UIntLiteral : UIntConstant {
145 public UIntLiteral (uint l, Location loc) : base (l, loc)
149 public override bool IsLiteral {
154 public class LongLiteral : LongConstant {
155 public LongLiteral (long l, Location loc) : base (l, loc)
159 public override bool IsLiteral {
164 public class ULongLiteral : ULongConstant {
165 public ULongLiteral (ulong l, Location loc) : base (l, loc)
169 public override bool IsLiteral {
174 public class FloatLiteral : FloatConstant {
176 public FloatLiteral (float f, Location loc) : base (f, loc)
180 public override bool IsLiteral {
186 public class DoubleLiteral : DoubleConstant {
187 public DoubleLiteral (double d, Location loc) : base (d, loc)
191 public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
193 if (target == TypeManager.float_type) {
194 Error_664 (ec, loc, "float", "f");
198 if (target == TypeManager.decimal_type) {
199 Error_664 (ec, loc, "decimal", "m");
203 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
206 static void Error_664 (ResolveContext ec, Location loc, string type, string suffix)
208 ec.Report.Error (664, loc,
209 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
213 public override bool IsLiteral {
219 public class DecimalLiteral : DecimalConstant {
220 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
224 public override bool IsLiteral {
229 public class StringLiteral : StringConstant {
230 public StringLiteral (string s, Location loc) : base (s, loc)
234 public override bool IsLiteral {