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);
171 Error_ValueCannotBeConverted (null, loc, targetType, false);
175 return base.ConvertImplicitly(targetType);
181 // A null literal in a pointer context
183 public class NullPointer : NullLiteral {
184 public static readonly NullLiteral Null;
186 static NullPointer ()
188 Null = new NullPointer ();
191 private NullPointer ():
194 type = TypeManager.object_type;
197 public override void Emit (EmitContext ec)
199 ILGenerator ig = ec.ig;
201 // TODO: why not use Ldnull instead ?
202 ig.Emit (OpCodes.Ldc_I4_0);
203 ig.Emit (OpCodes.Conv_U);
207 public class BoolLiteral : BoolConstant {
208 public BoolLiteral (bool val, Location loc) : base (val, loc)
212 public override Expression DoResolve (EmitContext ec)
214 type = TypeManager.bool_type;
219 public class CharLiteral : CharConstant {
220 public CharLiteral (char c, Location loc) : base (c, loc)
224 public override Expression DoResolve (EmitContext ec)
226 type = TypeManager.char_type;
231 public class IntLiteral : IntConstant {
232 public IntLiteral (int l, Location loc) : base (l, loc)
236 public override Expression DoResolve (EmitContext ec)
238 type = TypeManager.int32_type;
242 public override Constant ConvertImplicitly (Type type)
245 /// The 0 literal can be converted to an enum value,
247 if (Value == 0 && TypeManager.IsEnumType (type)) {
248 Constant c = ConvertImplicitly (TypeManager.EnumToUnderlying (type));
252 return new EnumConstant (c, type);
254 return base.ConvertImplicitly (type);
259 public class UIntLiteral : UIntConstant {
260 public UIntLiteral (uint l, Location loc) : base (l, loc)
264 public override Expression DoResolve (EmitContext ec)
266 type = TypeManager.uint32_type;
271 public class LongLiteral : LongConstant {
272 public LongLiteral (long l, Location loc) : base (l, loc)
276 public override Expression DoResolve (EmitContext ec)
278 type = TypeManager.int64_type;
284 public class ULongLiteral : ULongConstant {
285 public ULongLiteral (ulong l, Location loc) : base (l, loc)
289 public override Expression DoResolve (EmitContext ec)
291 type = TypeManager.uint64_type;
296 public class FloatLiteral : FloatConstant {
298 public FloatLiteral (float f, Location loc) : base (f, loc)
302 public override Expression DoResolve (EmitContext ec)
304 type = TypeManager.float_type;
309 public class DoubleLiteral : DoubleConstant {
310 public DoubleLiteral (double d, Location loc) : base (d, loc)
314 public override Expression DoResolve (EmitContext ec)
316 type = TypeManager.double_type;
321 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
323 if (target == TypeManager.float_type) {
324 Error_664 (loc, "float", "f");
328 if (target == TypeManager.decimal_type) {
329 Error_664 (loc, "decimal", "m");
333 base.Error_ValueCannotBeConverted (ec, loc, target, expl);
336 static void Error_664 (Location loc, string type, string suffix)
338 Report.Error (664, loc,
339 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
344 public class DecimalLiteral : DecimalConstant {
345 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
349 public override Expression DoResolve (EmitContext ec)
351 type = TypeManager.decimal_type;
356 public class StringLiteral : StringConstant {
357 public StringLiteral (string s, Location loc) : base (s, loc)
361 public override Expression DoResolve (EmitContext ec)
363 type = TypeManager.string_type;