//
// Author:
// Miguel de Icaza (miguel@ximian.com)
+// Marek Safar (marek.safar@seznam.cz)
//
-// (C) 2001 Ximian, Inc.
+// Copyright 2001 Ximian, Inc.
//
//
// Notice that during parsing we create objects of type Literal, but the
using System.Reflection;
using System.Reflection.Emit;
-//
-// I put System.Null just so we do not have to special case it on
-// TypeManager.CSharpName
-//
-namespace System {
- //
- // Represents the Null Type, just used as a placeholder for the type in NullLiteral
- //
- public class Null {
- }
-}
-
namespace Mono.CSharp {
//
- // The NullType just exists to compare type equality, and for
- // expressions that might have the `null type'
- //
- public class NullType {
- }
-
- //
- // The null Literal constant
+ // The null literal
//
public class NullLiteral : Constant {
public NullLiteral (Location loc):
base (loc)
{
eclass = ExprClass.Value;
+ type = typeof (NullLiteral);
}
-
+
override public string AsString ()
{
- return "null";
+ return GetSignatureForError ();
}
-
- public override object GetValue ()
+
+ public override Expression CreateExpressionTree (EmitContext ec)
{
- return null;
- }
+ // HACK: change type to be object
+ type = TypeManager.object_type;
+ return base.CreateExpressionTree (ec);
+ }
public override Expression DoResolve (EmitContext ec)
{
- type = TypeManager.null_type;
return this;
}
ec.ig.Emit (OpCodes.Ldnull);
}
- public override Constant Increment ()
+ public override string ExprClassName {
+ get {
+ return GetSignatureForError ();
+ }
+ }
+
+ public override string GetSignatureForError ()
{
- throw new NotSupportedException ();
+ return "null";
}
- public override bool IsDefaultValue {
- get {
- return true;
+ public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type t, bool expl)
+ {
+ if (TypeManager.IsGenericParameter (t)) {
+ Report.Error(403, loc,
+ "Cannot convert null to the type parameter `{0}' because it could be a value " +
+ "type. Consider using `default ({0})' instead", t.Name);
+ } else {
+ Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
+ TypeManager.CSharpName(t));
}
}
- public override bool IsNegative {
- get {
- return false;
- }
+ public override Constant ConvertExplicitly (bool inCheckedContext, Type targetType)
+ {
+ if (targetType.IsPointer)
+ return new EmptyConstantCast (new NullPointer (loc), targetType);
+
+ // Exlude internal compiler types
+ if (targetType == TypeManager.anonymous_method_type)
+ return null;
+
+ if (TypeManager.IsReferenceType (targetType))
+ return new EmptyConstantCast (this, targetType);
+
+ return null;
}
- public override bool IsZeroInteger {
- get { return true; }
+ public override Constant ConvertImplicitly (Type targetType)
+ {
+ //
+ // Null literal is of object type
+ //
+ if (targetType == TypeManager.object_type)
+ return this;
+
+ return ConvertExplicitly (false, targetType);
}
- public override string GetSignatureForError()
+ public override object GetValue ()
{
- return "null";
+ return null;
}
- public override void Error_ValueCannotBeConverted (Location loc, Type t, bool expl)
+ public override Constant Increment ()
{
- Report.Error (37, loc, "Cannot convert null to `{0}' because it is a value type",
- TypeManager.CSharpName (t));
+ throw new NotSupportedException ();
}
- public override Constant ToType (Type type, Location loc)
+ public override bool IsDefaultValue
{
- if (!type.IsValueType && !TypeManager.IsEnumType (type))
- return this;
+ get { return true; }
+ }
- return base.ToType (type, loc);
+ public override bool IsLiteral {
+ get { return true; }
}
- public override Constant Reduce(bool inCheckedContext, Type target_type)
- {
- if (!TypeManager.IsValueType (target_type))
- return new NullCast (this, target_type);
+ public override bool IsNegative {
+ get { return false; }
+ }
- return null;
+ public override bool IsNull {
+ get { return true; }
+ }
+
+ public override bool IsZeroInteger {
+ get { return true; }
}
}
// A null literal in a pointer context
//
public class NullPointer : NullLiteral {
- public static readonly NullLiteral Null;
-
- static NullPointer ()
- {
- Null = new NullPointer ();
- }
-
- private NullPointer ():
- base (Location.Null)
+ public NullPointer (Location loc):
+ base (loc)
{
type = TypeManager.object_type;
}
{
ILGenerator ig = ec.ig;
+ //
+ // Emits null pointer
+ //
ig.Emit (OpCodes.Ldc_I4_0);
ig.Emit (OpCodes.Conv_U);
}
type = TypeManager.bool_type;
return this;
}
+
+ public override bool IsLiteral {
+ get { return true; }
+ }
}
public class CharLiteral : CharConstant {
type = TypeManager.char_type;
return this;
}
+
+ public override bool IsLiteral {
+ get { return true; }
+ }
}
public class IntLiteral : IntConstant {
type = TypeManager.int32_type;
return this;
}
+
+ public override Constant ConvertImplicitly (Type type)
+ {
+ ///
+ /// The 0 literal can be converted to an enum value,
+ ///
+ if (Value == 0 && TypeManager.IsEnumType (type)) {
+ Constant c = ConvertImplicitly (TypeManager.GetEnumUnderlyingType (type));
+ if (c == null)
+ return null;
+
+ return new EnumConstant (c, type);
+ }
+ return base.ConvertImplicitly (type);
+ }
+
+ public override bool IsLiteral {
+ get { return true; }
+ }
}
public class UIntLiteral : UIntConstant {
type = TypeManager.uint32_type;
return this;
}
+
+ public override bool IsLiteral {
+ get { return true; }
+ }
}
public class LongLiteral : LongConstant {
public override Expression DoResolve (EmitContext ec)
{
type = TypeManager.int64_type;
-
return this;
}
+
+ public override bool IsLiteral {
+ get { return true; }
+ }
}
public class ULongLiteral : ULongConstant {
type = TypeManager.uint64_type;
return this;
}
+
+ public override bool IsLiteral {
+ get { return true; }
+ }
}
public class FloatLiteral : FloatConstant {
type = TypeManager.float_type;
return this;
}
+
+ public override bool IsLiteral {
+ get { return true; }
+ }
+
}
public class DoubleLiteral : DoubleConstant {
return this;
}
- public override void Error_ValueCannotBeConverted (Location loc, Type target, bool expl)
+ public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
{
if (target == TypeManager.float_type) {
Error_664 (loc, "float", "f");
return;
}
- base.Error_ValueCannotBeConverted (loc, target, expl);
+ base.Error_ValueCannotBeConverted (ec, loc, target, expl);
}
static void Error_664 (Location loc, string type, string suffix)
"Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
type, suffix);
}
+
+ public override bool IsLiteral {
+ get { return true; }
+ }
+
}
public class DecimalLiteral : DecimalConstant {
type = TypeManager.decimal_type;
return this;
}
+
+ public override bool IsLiteral {
+ get { return true; }
+ }
}
public class StringLiteral : StringConstant {
return this;
}
+
+ public override bool IsLiteral {
+ get { return true; }
+ }
+
}
}