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;
30 public interface ILiteralConstant
33 char[] ParsedValue { get; set; }
40 // Note: C# specification null-literal is NullLiteral of NullType type
42 public class NullLiteral : NullConstant
45 // Default type of null is an object
47 public NullLiteral (Location loc)
48 : base (InternalType.NullLiteral, loc)
52 public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec t, bool expl)
54 if (t.IsGenericParameter) {
55 ec.Report.Error(403, loc,
56 "Cannot convert null to the type parameter `{0}' because it could be a value " +
57 "type. Consider using `default ({0})' instead", t.Name);
61 if (TypeSpec.IsValueType (t)) {
62 ec.Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
63 TypeManager.CSharpName(t));
67 base.Error_ValueCannotBeConverted (ec, loc, t, expl);
70 public override string GetValueAsLiteral ()
75 public override bool IsLiteral {
79 public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
81 return System.Linq.Expressions.Expression.Constant (null);
85 public class BoolLiteral : BoolConstant, ILiteralConstant
87 public BoolLiteral (BuiltinTypes types, bool val, Location loc)
88 : base (types, val, loc)
92 public override bool IsLiteral {
97 char[] ILiteralConstant.ParsedValue { get; set; }
101 public class CharLiteral : CharConstant, ILiteralConstant
103 public CharLiteral (BuiltinTypes types, char c, Location loc)
104 : base (types, c, loc)
108 public override bool IsLiteral {
113 char[] ILiteralConstant.ParsedValue { get; set; }
117 public class IntLiteral : IntConstant, ILiteralConstant
119 public IntLiteral (BuiltinTypes types, int l, Location loc)
120 : base (types, l, loc)
124 public override Constant ConvertImplicitly (TypeSpec type)
127 // The 0 literal can be converted to an enum value
129 if (Value == 0 && TypeManager.IsEnumType (type)) {
130 Constant c = ConvertImplicitly (EnumSpec.GetUnderlyingType (type));
134 return new EnumConstant (c, type);
137 return base.ConvertImplicitly (type);
140 public override bool IsLiteral {
145 char[] ILiteralConstant.ParsedValue { get; set; }
149 public class UIntLiteral : UIntConstant, ILiteralConstant
151 public UIntLiteral (BuiltinTypes types, uint l, Location loc)
152 : base (types, l, loc)
156 public override bool IsLiteral {
161 char[] ILiteralConstant.ParsedValue { get; set; }
165 public class LongLiteral : LongConstant, ILiteralConstant
167 public LongLiteral (BuiltinTypes types, long l, Location loc)
168 : base (types, l, loc)
172 public override bool IsLiteral {
177 char[] ILiteralConstant.ParsedValue { get; set; }
181 public class ULongLiteral : ULongConstant, ILiteralConstant
183 public ULongLiteral (BuiltinTypes types, ulong l, Location loc)
184 : base (types, l, loc)
188 public override bool IsLiteral {
193 char[] ILiteralConstant.ParsedValue { get; set; }
197 public class FloatLiteral : FloatConstant, ILiteralConstant
199 public FloatLiteral (BuiltinTypes types, float f, Location loc)
200 : base (types, f, loc)
204 public override bool IsLiteral {
209 char[] ILiteralConstant.ParsedValue { get; set; }
213 public class DoubleLiteral : DoubleConstant, ILiteralConstant
215 public DoubleLiteral (BuiltinTypes types, double d, Location loc)
216 : base (types, d, loc)
220 public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
222 if (target.BuiltinType == BuiltinTypeSpec.Type.Float) {
223 Error_664 (ec, loc, "float", "f");
227 if (target.BuiltinType == BuiltinTypeSpec.Type.Decimal) {
228 Error_664 (ec, loc, "decimal", "m");
232 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
235 static void Error_664 (ResolveContext ec, Location loc, string type, string suffix)
237 ec.Report.Error (664, loc,
238 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
242 public override bool IsLiteral {
247 char[] ILiteralConstant.ParsedValue { get; set; }
251 public class DecimalLiteral : DecimalConstant, ILiteralConstant
253 public DecimalLiteral (BuiltinTypes types, decimal d, Location loc)
254 : base (types, d, loc)
258 public override bool IsLiteral {
263 char[] ILiteralConstant.ParsedValue { get; set; }
267 public class StringLiteral : StringConstant, ILiteralConstant
269 public StringLiteral (BuiltinTypes types, string s, Location loc)
270 : base (types, s, loc)
274 public override bool IsLiteral {
279 char[] ILiteralConstant.ParsedValue { get; set; }