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;
27 // I put System.Null just so we do not have to special case it on
28 // TypeManager.CSharpName
32 // Represents the Null Type, just used as a placeholder for the type in NullLiteral
38 namespace Mono.CSharp {
41 // The NullType just exists to compare type equality, and for
42 // expressions that might have the `null type'
44 public class NullType {
48 public class NullConstant : Constant
50 public NullConstant (Location loc):
53 eclass = ExprClass.Value;
54 type = TypeManager.null_type;
57 override public string AsString ()
62 public override object GetValue ()
67 public override void Emit (EmitContext ec)
69 ec.ig.Emit(OpCodes.Ldnull);
72 public override string GetSignatureForError ()
77 public override Constant Increment ()
79 throw new NotSupportedException ();
82 public override bool IsDefaultValue
87 public override bool IsNegative
92 public override bool IsZeroInteger
97 public override Constant ConvertExplicitly(bool inCheckedContext, Type target_type)
99 if (!TypeManager.IsValueType (target_type))
100 return new EmptyConstantCast (this, target_type);
105 public override Constant ConvertImplicitly (Type targetType)
107 if (!TypeManager.IsValueType (targetType))
108 return new EmptyConstantCast (this, targetType);
115 // Represents default(X) when result can be reduced to null
117 public class NullDefault : EmptyConstantCast
119 public NullDefault(Constant value, Type type)
124 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
126 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
131 // The null Literal constant
133 public class NullLiteral : NullConstant {
134 public NullLiteral (Location loc):
139 public override Expression DoResolve (EmitContext ec)
141 type = TypeManager.null_type;
145 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type t, bool expl)
147 if (TypeManager.IsGenericParameter (t)) {
148 Report.Error(403, loc,
149 "Cannot convert null to the type parameter `{0}' because it could be a value " +
150 "type. Consider using `default ({0})' instead", t.Name);
152 Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
153 TypeManager.CSharpName(t));
157 public override Constant ConvertImplicitly (Type targetType)
159 if (targetType.IsPointer)
160 return new EmptyConstantCast (NullPointer.Null, targetType);
162 if (TypeManager.IsGenericParameter(targetType)) {
163 GenericConstraints gc = null;
166 gc = TypeManager.GetTypeParameterConstraints(targetType);
168 if (gc != null && gc.IsReferenceType)
169 return new EmptyConstantCast (this, targetType);
174 return base.ConvertImplicitly(targetType);
180 // A null literal in a pointer context
182 public class NullPointer : NullLiteral {
183 public static readonly NullLiteral Null;
185 static NullPointer ()
187 Null = new NullPointer ();
190 private NullPointer ():
193 type = TypeManager.object_type;
196 public override void Emit (EmitContext ec)
198 ILGenerator ig = ec.ig;
200 // TODO: why not use Ldnull instead ?
201 ig.Emit (OpCodes.Ldc_I4_0);
202 ig.Emit (OpCodes.Conv_U);
206 public class BoolLiteral : BoolConstant {
207 public BoolLiteral (bool val, Location loc) : base (val, loc)
211 public override Expression DoResolve (EmitContext ec)
213 type = TypeManager.bool_type;
218 public class CharLiteral : CharConstant {
219 public CharLiteral (char c, Location loc) : base (c, loc)
223 public override Expression DoResolve (EmitContext ec)
225 type = TypeManager.char_type;
230 public class IntLiteral : IntConstant {
231 public IntLiteral (int l, Location loc) : base (l, loc)
235 public override Expression DoResolve (EmitContext ec)
237 type = TypeManager.int32_type;
241 public override Constant ConvertImplicitly (Type type)
244 /// The 0 literal can be converted to an enum value,
246 if (Value == 0 && TypeManager.IsEnumType (type)) {
247 Constant c = ConvertImplicitly (TypeManager.EnumToUnderlying (type));
251 return new EnumConstant (c, type);
253 return base.ConvertImplicitly (type);
258 public class UIntLiteral : UIntConstant {
259 public UIntLiteral (uint l, Location loc) : base (l, loc)
263 public override Expression DoResolve (EmitContext ec)
265 type = TypeManager.uint32_type;
270 public class LongLiteral : LongConstant {
271 public LongLiteral (long l, Location loc) : base (l, loc)
275 public override Expression DoResolve (EmitContext ec)
277 type = TypeManager.int64_type;
283 public class ULongLiteral : ULongConstant {
284 public ULongLiteral (ulong l, Location loc) : base (l, loc)
288 public override Expression DoResolve (EmitContext ec)
290 type = TypeManager.uint64_type;
295 public class FloatLiteral : FloatConstant {
297 public FloatLiteral (float f, Location loc) : base (f, loc)
301 public override Expression DoResolve (EmitContext ec)
303 type = TypeManager.float_type;
308 public class DoubleLiteral : DoubleConstant {
309 public DoubleLiteral (double d, Location loc) : base (d, loc)
313 public override Expression DoResolve (EmitContext ec)
315 type = TypeManager.double_type;
320 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
322 if (target == TypeManager.float_type) {
323 Error_664 (loc, "float", "f");
327 if (target == TypeManager.decimal_type) {
328 Error_664 (loc, "decimal", "m");
332 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
335 static void Error_664 (Location loc, string type, string suffix)
337 Report.Error (664, loc,
338 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
343 public class DecimalLiteral : DecimalConstant {
344 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
348 public override Expression DoResolve (EmitContext ec)
350 type = TypeManager.decimal_type;
355 public class StringLiteral : StringConstant {
356 public StringLiteral (string s, Location loc) : base (s, loc)
360 public override Expression DoResolve (EmitContext ec)
362 type = TypeManager.string_type;