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 // Note: C# specification null-literal is NullLiteral of NullType type
33 public class NullLiteral : NullConstant
36 // Default type of null is an object
38 public NullLiteral (Location loc)
39 : base (InternalType.Null, loc)
43 public override Expression CreateExpressionTree (ResolveContext ec)
45 // Optimized version, also avoids referencing literal internal type
46 Arguments args = new Arguments (1);
47 args.Add (new Argument (this));
48 return CreateExpressionFactoryCall (ec, "Constant", args);
51 public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec t, bool expl)
53 if (t.IsGenericParameter) {
54 ec.Report.Error(403, loc,
55 "Cannot convert null to the type parameter `{0}' because it could be a value " +
56 "type. Consider using `default ({0})' instead", t.Name);
60 if (TypeManager.IsValueType (t)) {
61 ec.Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
62 TypeManager.CSharpName(t));
66 base.Error_ValueCannotBeConverted (ec, loc, t, expl);
69 public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec targetType)
72 // Null literal is of object type
74 if (targetType == TypeManager.object_type)
77 return base.ConvertImplicitly (rc, targetType);
80 public override bool IsLiteral {
84 public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
86 return System.Linq.Expressions.Expression.Constant (null);
91 // A null literal in a pointer context
93 class NullPointer : NullLiteral {
94 public NullPointer (Location loc):
97 type = TypeManager.object_type;
100 public override void Emit (EmitContext ec)
103 // Emits null pointer
105 ec.Emit (OpCodes.Ldc_I4_0);
106 ec.Emit (OpCodes.Conv_U);
110 public class BoolLiteral : BoolConstant {
111 public BoolLiteral (bool val, Location loc) : base (val, loc)
115 public override bool IsLiteral {
120 public class CharLiteral : CharConstant {
121 public CharLiteral (char c, Location loc) : base (c, loc)
125 public override bool IsLiteral {
130 public class IntLiteral : IntConstant {
131 public IntLiteral (int l, Location loc) : base (l, loc)
135 public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
138 // The 0 literal can be converted to an enum value
140 if (Value == 0 && TypeManager.IsEnumType (type)) {
141 Constant c = ConvertImplicitly (rc, EnumSpec.GetUnderlyingType (type));
145 return new EnumConstant (c, type).Resolve (rc);
148 return base.ConvertImplicitly (rc, type);
151 public override bool IsLiteral {
156 public class UIntLiteral : UIntConstant {
157 public UIntLiteral (uint l, Location loc) : base (l, loc)
161 public override bool IsLiteral {
166 public class LongLiteral : LongConstant {
167 public LongLiteral (long l, Location loc) : base (l, loc)
171 public override bool IsLiteral {
176 public class ULongLiteral : ULongConstant {
177 public ULongLiteral (ulong l, Location loc) : base (l, loc)
181 public override bool IsLiteral {
186 public class FloatLiteral : FloatConstant {
188 public FloatLiteral (float f, Location loc) : base (f, loc)
192 public override bool IsLiteral {
198 public class DoubleLiteral : DoubleConstant {
199 public DoubleLiteral (double d, Location loc) : base (d, loc)
203 public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
205 if (target == TypeManager.float_type) {
206 Error_664 (ec, loc, "float", "f");
210 if (target == TypeManager.decimal_type) {
211 Error_664 (ec, loc, "decimal", "m");
215 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
218 static void Error_664 (ResolveContext ec, Location loc, string type, string suffix)
220 ec.Report.Error (664, loc,
221 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
225 public override bool IsLiteral {
231 public class DecimalLiteral : DecimalConstant {
232 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
236 public override bool IsLiteral {
241 public class StringLiteral : StringConstant {
242 public StringLiteral (string s, Location loc) : base (s, loc)
246 public override bool IsLiteral {