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.NullLiteral, 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);
78 public class BoolLiteral : BoolConstant {
79 public BoolLiteral (BuildinTypes types, bool val, Location loc)
80 : base (types, val, loc)
84 public override bool IsLiteral {
89 public class CharLiteral : CharConstant {
90 public CharLiteral (BuildinTypes types, char c, Location loc)
91 : base (types, c, loc)
95 public override bool IsLiteral {
100 public class IntLiteral : IntConstant {
101 public IntLiteral (BuildinTypes types, int l, Location loc)
102 : base (types, l, loc)
106 public override Constant ConvertImplicitly (TypeSpec type)
109 // The 0 literal can be converted to an enum value
111 if (Value == 0 && TypeManager.IsEnumType (type)) {
112 Constant c = ConvertImplicitly (EnumSpec.GetUnderlyingType (type));
116 return new EnumConstant (c, type);
119 return base.ConvertImplicitly (type);
122 public override bool IsLiteral {
127 public class UIntLiteral : UIntConstant {
128 public UIntLiteral (BuildinTypes types, uint l, Location loc)
129 : base (types, l, loc)
133 public override bool IsLiteral {
138 public class LongLiteral : LongConstant {
139 public LongLiteral (BuildinTypes types, long l, Location loc)
140 : base (types, l, loc)
144 public override bool IsLiteral {
149 public class ULongLiteral : ULongConstant {
150 public ULongLiteral (BuildinTypes types, ulong l, Location loc)
151 : base (types, l, loc)
155 public override bool IsLiteral {
160 public class FloatLiteral : FloatConstant {
162 public FloatLiteral (BuildinTypes types, float f, Location loc)
163 : base (types, f, loc)
167 public override bool IsLiteral {
173 public class DoubleLiteral : DoubleConstant {
174 public DoubleLiteral (BuildinTypes types, double d, Location loc)
175 : base (types, d, loc)
179 public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
181 if (target.BuildinType == BuildinTypeSpec.Type.Float) {
182 Error_664 (ec, loc, "float", "f");
186 if (target.BuildinType == BuildinTypeSpec.Type.Decimal) {
187 Error_664 (ec, loc, "decimal", "m");
191 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
194 static void Error_664 (ResolveContext ec, Location loc, string type, string suffix)
196 ec.Report.Error (664, loc,
197 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
201 public override bool IsLiteral {
207 public class DecimalLiteral : DecimalConstant {
208 public DecimalLiteral (BuildinTypes types, decimal d, Location loc)
209 : base (types, d, loc)
213 public override bool IsLiteral {
218 public class StringLiteral : StringConstant {
219 public StringLiteral (BuildinTypes types, string s, Location loc)
220 : base (types, s, loc)
224 public override bool IsLiteral {