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 public class NullLiteral : Constant {
32 public NullLiteral (Location loc):
35 eclass = ExprClass.Value;
36 type = typeof (NullLiteral);
39 override public string AsString ()
41 return GetSignatureForError ();
44 public override Expression CreateExpressionTree (EmitContext ec)
46 type = TypeManager.object_type;
47 return base.CreateExpressionTree (ec);
50 public override Expression DoResolve (EmitContext ec)
55 public override void Emit (EmitContext ec)
57 ec.ig.Emit (OpCodes.Ldnull);
60 public override string ExprClassName {
62 return GetSignatureForError ();
66 public override string GetSignatureForError ()
71 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type t, bool expl)
73 if (TypeManager.IsGenericParameter (t)) {
74 Report.Error(403, loc,
75 "Cannot convert null to the type parameter `{0}' because it could be a value " +
76 "type. Consider using `default ({0})' instead", t.Name);
78 Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
79 TypeManager.CSharpName(t));
83 public override Constant ConvertImplicitly (Type targetType)
85 if (targetType.IsPointer)
86 return new EmptyConstantCast (NullPointer.Null, targetType);
88 if (TypeManager.IsReferenceType (targetType))
89 return new EmptyConstantCast (this, targetType);
94 public override object GetValue ()
99 public override Constant Increment ()
101 throw new NotSupportedException ();
104 public override bool IsDefaultValue
109 public override bool IsLiteral {
113 public override bool IsNegative {
114 get { return false; }
117 public override bool IsNull {
121 public override bool IsZeroInteger {
125 public override Constant ConvertExplicitly(bool inCheckedContext, Type target_type)
127 if (!TypeManager.IsValueType (target_type))
128 return new EmptyConstantCast (this, target_type);
135 // A null literal in a pointer context
137 public class NullPointer : NullLiteral {
138 public static readonly NullLiteral Null = new NullPointer ();
140 private NullPointer ():
143 type = TypeManager.object_type;
146 public override void Emit (EmitContext ec)
148 ILGenerator ig = ec.ig;
150 // TODO: why not use Ldnull instead ?
151 ig.Emit (OpCodes.Ldc_I4_0);
152 ig.Emit (OpCodes.Conv_U);
156 public class BoolLiteral : BoolConstant {
157 public BoolLiteral (bool val, Location loc) : base (val, loc)
161 public override Expression DoResolve (EmitContext ec)
163 type = TypeManager.bool_type;
167 public override bool IsLiteral {
172 public class CharLiteral : CharConstant {
173 public CharLiteral (char c, Location loc) : base (c, loc)
177 public override Expression DoResolve (EmitContext ec)
179 type = TypeManager.char_type;
183 public override bool IsLiteral {
188 public class IntLiteral : IntConstant {
189 public IntLiteral (int l, Location loc) : base (l, loc)
193 public override Expression DoResolve (EmitContext ec)
195 type = TypeManager.int32_type;
199 public override Constant ConvertImplicitly (Type type)
202 /// The 0 literal can be converted to an enum value,
204 if (Value == 0 && TypeManager.IsEnumType (type)) {
205 Constant c = ConvertImplicitly (TypeManager.GetEnumUnderlyingType (type));
209 return new EnumConstant (c, type);
211 return base.ConvertImplicitly (type);
214 public override bool IsLiteral {
219 public class UIntLiteral : UIntConstant {
220 public UIntLiteral (uint l, Location loc) : base (l, loc)
224 public override Expression DoResolve (EmitContext ec)
226 type = TypeManager.uint32_type;
230 public override bool IsLiteral {
235 public class LongLiteral : LongConstant {
236 public LongLiteral (long l, Location loc) : base (l, loc)
240 public override Expression DoResolve (EmitContext ec)
242 type = TypeManager.int64_type;
246 public override bool IsLiteral {
251 public class ULongLiteral : ULongConstant {
252 public ULongLiteral (ulong l, Location loc) : base (l, loc)
256 public override Expression DoResolve (EmitContext ec)
258 type = TypeManager.uint64_type;
262 public override bool IsLiteral {
267 public class FloatLiteral : FloatConstant {
269 public FloatLiteral (float f, Location loc) : base (f, loc)
273 public override Expression DoResolve (EmitContext ec)
275 type = TypeManager.float_type;
279 public override bool IsLiteral {
285 public class DoubleLiteral : DoubleConstant {
286 public DoubleLiteral (double d, Location loc) : base (d, loc)
290 public override Expression DoResolve (EmitContext ec)
292 type = TypeManager.double_type;
297 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
299 if (target == TypeManager.float_type) {
300 Error_664 (loc, "float", "f");
304 if (target == TypeManager.decimal_type) {
305 Error_664 (loc, "decimal", "m");
309 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
312 static void Error_664 (Location loc, string type, string suffix)
314 Report.Error (664, loc,
315 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
319 public override bool IsLiteral {
325 public class DecimalLiteral : DecimalConstant {
326 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
330 public override Expression DoResolve (EmitContext ec)
332 type = TypeManager.decimal_type;
336 public override bool IsLiteral {
341 public class StringLiteral : StringConstant {
342 public StringLiteral (string s, Location loc) : base (s, loc)
346 public override Expression DoResolve (EmitContext ec)
348 type = TypeManager.string_type;
353 public override bool IsLiteral {