2 // literal.cs: Literal representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
8 // (C) 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 IsNegative
111 get { return false; }
114 public override bool IsNull {
118 public override bool IsZeroInteger
123 public override Constant ConvertExplicitly(bool inCheckedContext, Type target_type)
125 if (!TypeManager.IsValueType (target_type))
126 return new EmptyConstantCast (this, target_type);
133 // A null literal in a pointer context
135 public class NullPointer : NullLiteral {
136 public static readonly NullLiteral Null = new NullPointer ();
138 private NullPointer ():
141 type = TypeManager.object_type;
144 public override void Emit (EmitContext ec)
146 ILGenerator ig = ec.ig;
148 // TODO: why not use Ldnull instead ?
149 ig.Emit (OpCodes.Ldc_I4_0);
150 ig.Emit (OpCodes.Conv_U);
154 public class BoolLiteral : BoolConstant {
155 public BoolLiteral (bool val, Location loc) : base (val, loc)
159 public override Expression DoResolve (EmitContext ec)
161 type = TypeManager.bool_type;
166 public class CharLiteral : CharConstant {
167 public CharLiteral (char c, Location loc) : base (c, loc)
171 public override Expression DoResolve (EmitContext ec)
173 type = TypeManager.char_type;
178 public class IntLiteral : IntConstant {
179 public IntLiteral (int l, Location loc) : base (l, loc)
183 public override Expression DoResolve (EmitContext ec)
185 type = TypeManager.int32_type;
189 public override Constant ConvertImplicitly (Type type)
192 /// The 0 literal can be converted to an enum value,
194 if (Value == 0 && TypeManager.IsEnumType (type)) {
195 Constant c = ConvertImplicitly (TypeManager.GetEnumUnderlyingType (type));
199 return new EnumConstant (c, type);
201 return base.ConvertImplicitly (type);
206 public class UIntLiteral : UIntConstant {
207 public UIntLiteral (uint l, Location loc) : base (l, loc)
211 public override Expression DoResolve (EmitContext ec)
213 type = TypeManager.uint32_type;
218 public class LongLiteral : LongConstant {
219 public LongLiteral (long l, Location loc) : base (l, loc)
223 public override Expression DoResolve (EmitContext ec)
225 type = TypeManager.int64_type;
231 public class ULongLiteral : ULongConstant {
232 public ULongLiteral (ulong l, Location loc) : base (l, loc)
236 public override Expression DoResolve (EmitContext ec)
238 type = TypeManager.uint64_type;
243 public class FloatLiteral : FloatConstant {
245 public FloatLiteral (float f, Location loc) : base (f, loc)
249 public override Expression DoResolve (EmitContext ec)
251 type = TypeManager.float_type;
256 public class DoubleLiteral : DoubleConstant {
257 public DoubleLiteral (double d, Location loc) : base (d, loc)
261 public override Expression DoResolve (EmitContext ec)
263 type = TypeManager.double_type;
268 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
270 if (target == TypeManager.float_type) {
271 Error_664 (loc, "float", "f");
275 if (target == TypeManager.decimal_type) {
276 Error_664 (loc, "decimal", "m");
280 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
283 static void Error_664 (Location loc, string type, string suffix)
285 Report.Error (664, loc,
286 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
291 public class DecimalLiteral : DecimalConstant {
292 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
296 public override Expression DoResolve (EmitContext ec)
298 type = TypeManager.decimal_type;
303 public class StringLiteral : StringConstant {
304 public StringLiteral (string s, Location loc) : base (s, loc)
308 public override Expression DoResolve (EmitContext ec)
310 type = TypeManager.string_type;