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 Reduce(bool inCheckedContext, Type target_type)
99 if (!TypeManager.IsValueType (target_type))
100 return new EmptyConstantCast (this, target_type);
105 public override Constant ToType(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 (Location loc, Type target, bool expl)
126 base.Error_ValueCannotBeConverted(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 (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 ToType (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 (loc, targetType, false);
175 return base.ToType(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;
243 public class UIntLiteral : UIntConstant {
244 public UIntLiteral (uint l, Location loc) : base (l, loc)
248 public override Expression DoResolve (EmitContext ec)
250 type = TypeManager.uint32_type;
255 public class LongLiteral : LongConstant {
256 public LongLiteral (long l, Location loc) : base (l, loc)
260 public override Expression DoResolve (EmitContext ec)
262 type = TypeManager.int64_type;
268 public class ULongLiteral : ULongConstant {
269 public ULongLiteral (ulong l, Location loc) : base (l, loc)
273 public override Expression DoResolve (EmitContext ec)
275 type = TypeManager.uint64_type;
280 public class FloatLiteral : FloatConstant {
282 public FloatLiteral (float f, Location loc) : base (f, loc)
286 public override Expression DoResolve (EmitContext ec)
288 type = TypeManager.float_type;
293 public class DoubleLiteral : DoubleConstant {
294 public DoubleLiteral (double d, Location loc) : base (d, loc)
298 public override Expression DoResolve (EmitContext ec)
300 type = TypeManager.double_type;
305 public override void Error_ValueCannotBeConverted (Location loc, Type target, bool expl)
307 if (target == TypeManager.float_type) {
308 Error_664 (loc, "float", "f");
312 if (target == TypeManager.decimal_type) {
313 Error_664 (loc, "decimal", "m");
317 base.Error_ValueCannotBeConverted (loc, target, expl);
320 static void Error_664 (Location loc, string type, string suffix)
322 Report.Error (664, loc,
323 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
328 public class DecimalLiteral : DecimalConstant {
329 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
333 public override Expression DoResolve (EmitContext ec)
335 type = TypeManager.decimal_type;
340 public class StringLiteral : StringConstant {
341 public StringLiteral (string s, Location loc) : base (s, loc)
345 public override Expression DoResolve (EmitContext ec)
347 type = TypeManager.string_type;