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 abstract class NullConstant : Constant
50 public NullConstant (Location loc):
53 eclass = ExprClass.Value;
56 override public string AsString ()
61 public override object GetValue ()
66 public override void Emit (EmitContext ec)
68 ec.ig.Emit(OpCodes.Ldnull);
71 public override string GetSignatureForError ()
76 public override Constant Increment ()
78 throw new NotSupportedException ();
81 public override bool IsDefaultValue
86 public override bool IsNegative
91 public override bool IsZeroInteger
96 public override Constant Reduce(bool inCheckedContext, Type target_type)
98 if (!TypeManager.IsValueType (target_type))
99 return new EmptyConstantCast (this, target_type);
104 public override Constant ToType(Type targetType)
106 if (!TypeManager.IsValueType (targetType))
107 return new EmptyConstantCast (this, targetType);
114 // Represents default(X) when result can be reduced to null
116 public class NullDefault : EmptyConstantCast
118 public NullDefault(Constant value, Type type)
123 public override void Error_ValueCannotBeConverted (Location loc, Type target, bool expl)
125 base.Error_ValueCannotBeConverted(loc, target, expl);
130 // The null Literal constant
132 public class NullLiteral : NullConstant {
133 public NullLiteral (Location loc):
138 public override Expression DoResolve (EmitContext ec)
140 type = TypeManager.null_type;
144 public override void Error_ValueCannotBeConverted (Location loc, Type t, bool expl)
146 if (TypeManager.IsGenericParameter (t)) {
147 Report.Error(403, loc,
148 "Cannot convert null to the type parameter `{0}' becaues it could be a value " +
149 "type. Consider using `default ({0})' instead", t.Name);
151 Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
152 TypeManager.CSharpName(t));
156 public override Constant ToType (Type targetType)
158 if (targetType.IsPointer)
159 return new EmptyConstantCast (NullPointer.Null, targetType);
161 if (TypeManager.IsGenericParameter(targetType)) {
162 GenericConstraints gc = null;
165 gc = TypeManager.GetTypeParameterConstraints(targetType);
167 if (gc != null && gc.IsReferenceType)
168 return new EmptyConstantCast (this, targetType);
170 Error_ValueCannotBeConverted (loc, targetType, false);
174 return base.ToType(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;
242 public class UIntLiteral : UIntConstant {
243 public UIntLiteral (uint l, Location loc) : base (l, loc)
247 public override Expression DoResolve (EmitContext ec)
249 type = TypeManager.uint32_type;
254 public class LongLiteral : LongConstant {
255 public LongLiteral (long l, Location loc) : base (l, loc)
259 public override Expression DoResolve (EmitContext ec)
261 type = TypeManager.int64_type;
267 public class ULongLiteral : ULongConstant {
268 public ULongLiteral (ulong l, Location loc) : base (l, loc)
272 public override Expression DoResolve (EmitContext ec)
274 type = TypeManager.uint64_type;
279 public class FloatLiteral : FloatConstant {
281 public FloatLiteral (float f, Location loc) : base (f, loc)
285 public override Expression DoResolve (EmitContext ec)
287 type = TypeManager.float_type;
292 public class DoubleLiteral : DoubleConstant {
293 public DoubleLiteral (double d, Location loc) : base (d, loc)
297 public override Expression DoResolve (EmitContext ec)
299 type = TypeManager.double_type;
304 public override void Error_ValueCannotBeConverted (Location loc, Type target, bool expl)
306 if (target == TypeManager.float_type) {
307 Error_664 (loc, "float", "f");
311 if (target == TypeManager.decimal_type) {
312 Error_664 (loc, "decimal", "m");
316 base.Error_ValueCannotBeConverted (loc, target, expl);
319 static void Error_664 (Location loc, string type, string suffix)
321 Report.Error (664, loc,
322 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
327 public class DecimalLiteral : DecimalConstant {
328 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
332 public override Expression DoResolve (EmitContext ec)
334 type = TypeManager.decimal_type;
339 public class StringLiteral : StringConstant {
340 public StringLiteral (string s, Location loc) : base (s, loc)
344 public override Expression DoResolve (EmitContext ec)
346 type = TypeManager.string_type;