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 ExprClassName {
74 return GetSignatureForError ();
78 public override string GetSignatureForError ()
83 public override Constant Increment ()
85 throw new NotSupportedException ();
88 public override bool IsDefaultValue
93 public override bool IsNegative
98 public override bool IsZeroInteger
103 public override Constant ConvertExplicitly(bool inCheckedContext, Type target_type)
105 if (!TypeManager.IsValueType (target_type))
106 return new EmptyConstantCast (this, target_type);
111 public override Constant ConvertImplicitly (Type targetType)
113 if (!TypeManager.IsValueType (targetType))
114 return new EmptyConstantCast (this, targetType);
121 // Represents default(X) when result can be reduced to null
123 public class NullDefault : EmptyConstantCast
125 public NullDefault(Constant value, Type type)
130 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
132 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
137 // The null Literal constant
139 public class NullLiteral : NullConstant {
140 public NullLiteral (Location loc):
145 public override Expression DoResolve (EmitContext ec)
147 type = TypeManager.null_type;
151 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type t, bool expl)
153 if (TypeManager.IsGenericParameter (t)) {
154 Report.Error(403, loc,
155 "Cannot convert null to the type parameter `{0}' because it could be a value " +
156 "type. Consider using `default ({0})' instead", t.Name);
158 Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
159 TypeManager.CSharpName(t));
163 public override Constant ConvertImplicitly (Type targetType)
165 if (targetType.IsPointer)
166 return new EmptyConstantCast (NullPointer.Null, targetType);
168 if (TypeManager.IsGenericParameter(targetType)) {
169 GenericConstraints gc = null;
172 gc = TypeManager.GetTypeParameterConstraints(targetType);
174 if (gc != null && gc.IsReferenceType)
175 return new EmptyConstantCast (this, targetType);
180 return base.ConvertImplicitly(targetType);
186 // A null literal in a pointer context
188 public class NullPointer : NullLiteral {
189 public static readonly NullLiteral Null;
191 static NullPointer ()
193 Null = new NullPointer ();
196 private NullPointer ():
199 type = TypeManager.object_type;
202 public override void Emit (EmitContext ec)
204 ILGenerator ig = ec.ig;
206 // TODO: why not use Ldnull instead ?
207 ig.Emit (OpCodes.Ldc_I4_0);
208 ig.Emit (OpCodes.Conv_U);
212 public class BoolLiteral : BoolConstant {
213 public BoolLiteral (bool val, Location loc) : base (val, loc)
217 public override Expression DoResolve (EmitContext ec)
219 type = TypeManager.bool_type;
224 public class CharLiteral : CharConstant {
225 public CharLiteral (char c, Location loc) : base (c, loc)
229 public override Expression DoResolve (EmitContext ec)
231 type = TypeManager.char_type;
236 public class IntLiteral : IntConstant {
237 public IntLiteral (int l, Location loc) : base (l, loc)
241 public override Expression DoResolve (EmitContext ec)
243 type = TypeManager.int32_type;
247 public override Constant ConvertImplicitly (Type type)
250 /// The 0 literal can be converted to an enum value,
252 if (Value == 0 && TypeManager.IsEnumType (type)) {
253 Constant c = ConvertImplicitly (TypeManager.EnumToUnderlying (type));
257 return new EnumConstant (c, type);
259 return base.ConvertImplicitly (type);
264 public class UIntLiteral : UIntConstant {
265 public UIntLiteral (uint l, Location loc) : base (l, loc)
269 public override Expression DoResolve (EmitContext ec)
271 type = TypeManager.uint32_type;
276 public class LongLiteral : LongConstant {
277 public LongLiteral (long l, Location loc) : base (l, loc)
281 public override Expression DoResolve (EmitContext ec)
283 type = TypeManager.int64_type;
289 public class ULongLiteral : ULongConstant {
290 public ULongLiteral (ulong l, Location loc) : base (l, loc)
294 public override Expression DoResolve (EmitContext ec)
296 type = TypeManager.uint64_type;
301 public class FloatLiteral : FloatConstant {
303 public FloatLiteral (float f, Location loc) : base (f, loc)
307 public override Expression DoResolve (EmitContext ec)
309 type = TypeManager.float_type;
314 public class DoubleLiteral : DoubleConstant {
315 public DoubleLiteral (double d, Location loc) : base (d, loc)
319 public override Expression DoResolve (EmitContext ec)
321 type = TypeManager.double_type;
326 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
328 if (target == TypeManager.float_type) {
329 Error_664 (loc, "float", "f");
333 if (target == TypeManager.decimal_type) {
334 Error_664 (loc, "decimal", "m");
338 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
341 static void Error_664 (Location loc, string type, string suffix)
343 Report.Error (664, loc,
344 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
349 public class DecimalLiteral : DecimalConstant {
350 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
354 public override Expression DoResolve (EmitContext ec)
356 type = TypeManager.decimal_type;
361 public class StringLiteral : StringConstant {
362 public StringLiteral (string s, Location loc) : base (s, loc)
366 public override Expression DoResolve (EmitContext ec)
368 type = TypeManager.string_type;