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);
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 (BuildinTypes types, bool val, Location loc)
100 : base (types, val, loc)
104 public override bool IsLiteral {
109 public class CharLiteral : CharConstant {
110 public CharLiteral (BuildinTypes types, char c, Location loc)
111 : base (types, c, loc)
115 public override bool IsLiteral {
120 public class IntLiteral : IntConstant {
121 public IntLiteral (BuildinTypes types, int l, Location loc)
122 : base (types, l, loc)
126 public override Constant ConvertImplicitly (TypeSpec type)
129 // The 0 literal can be converted to an enum value
131 if (Value == 0 && TypeManager.IsEnumType (type)) {
132 Constant c = ConvertImplicitly (EnumSpec.GetUnderlyingType (type));
136 return new EnumConstant (c, type);
139 return base.ConvertImplicitly (type);
142 public override bool IsLiteral {
147 public class UIntLiteral : UIntConstant {
148 public UIntLiteral (BuildinTypes types, uint l, Location loc)
149 : base (types, l, loc)
153 public override bool IsLiteral {
158 public class LongLiteral : LongConstant {
159 public LongLiteral (BuildinTypes types, long l, Location loc)
160 : base (types, l, loc)
164 public override bool IsLiteral {
169 public class ULongLiteral : ULongConstant {
170 public ULongLiteral (BuildinTypes types, ulong l, Location loc)
171 : base (types, l, loc)
175 public override bool IsLiteral {
180 public class FloatLiteral : FloatConstant {
182 public FloatLiteral (BuildinTypes types, float f, Location loc)
183 : base (types, f, loc)
187 public override bool IsLiteral {
193 public class DoubleLiteral : DoubleConstant {
194 public DoubleLiteral (BuildinTypes types, double d, Location loc)
195 : base (types, d, loc)
199 public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
201 if (target.BuildinType == BuildinTypeSpec.Type.Float) {
202 Error_664 (ec, loc, "float", "f");
206 if (target.BuildinType == BuildinTypeSpec.Type.Decimal) {
207 Error_664 (ec, loc, "decimal", "m");
211 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
214 static void Error_664 (ResolveContext ec, Location loc, string type, string suffix)
216 ec.Report.Error (664, loc,
217 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
221 public override bool IsLiteral {
227 public class DecimalLiteral : DecimalConstant {
228 public DecimalLiteral (BuildinTypes types, decimal d, Location loc)
229 : base (types, d, loc)
233 public override bool IsLiteral {
238 public class StringLiteral : StringConstant {
239 public StringLiteral (BuildinTypes types, string s, Location loc)
240 : base (types, s, loc)
244 public override bool IsLiteral {