condition tests passes
[mono.git] / mcs / mcs / literal.cs
index c7aae9204b0a775c51648643e8ade9d926fb42ca..aba51e60b12b3492188c96d505837f0725bd5bc0 100644 (file)
@@ -3,6 +3,7 @@
 //
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
+//   Marek Safar (marek.safar@seznam.cz)
 //
 // (C) 2001 Ximian, Inc.
 //
@@ -43,14 +44,14 @@ namespace Mono.CSharp {
        public class NullType {
        }
 
-       //
-       // The null Literal constant
-       //
-       public class NullLiteral : Constant {
-               public NullLiteral (Location loc):
+
+       public class NullConstant : Constant
+       {
+               public NullConstant (Location loc):
                        base (loc)
                {
                        eclass = ExprClass.Value;
+                       type = TypeManager.null_type;
                }
                
                override public string AsString ()
@@ -63,15 +64,20 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               public override void Emit (EmitContext ec)
                {
-                       type = TypeManager.null_type;
-                       return this;
+                       ec.ig.Emit(OpCodes.Ldnull);
+               }
+               
+               public override string ExprClassName {
+                       get {
+                               return GetSignatureForError ();
+                       }
                }
 
-               public override void Emit (EmitContext ec)
+               public override string GetSignatureForError ()
                {
-                       ec.ig.Emit (OpCodes.Ldnull);
+                       return "null";
                }
 
                public override Constant Increment ()
@@ -79,61 +85,113 @@ namespace Mono.CSharp {
                        throw new NotSupportedException ();
                }
 
-               public override bool IsDefaultValue {
-                       get {
-                               return true;
-                       }
+               public override bool IsDefaultValue 
+               {
+                       get { return true; }
                }
 
-               public override bool IsNegative {
-                       get {
-                               return false;
-                       }
+               public override bool IsNegative 
+               {
+                       get { return false; }
                }
 
-               public override bool IsZeroInteger {
+               public override bool IsNull {
                        get { return true; }
                }
 
-               public override string GetSignatureForError()
+               public override bool IsZeroInteger 
                {
-                       return "null";
+                       get { return true; }
                }
 
-               public override void Error_ValueCannotBeConverted (Location loc, Type t, bool expl)
+               public override Constant ConvertExplicitly(bool inCheckedContext, Type target_type)
                {
-                       Report.Error (37, loc, "Cannot convert null to `{0}' because it is a value type",
-                               TypeManager.CSharpName (t));
+                       if (!TypeManager.IsValueType (target_type))
+                               return new EmptyConstantCast (this, target_type);
+
+                       return null;
                }
 
-               public override Constant ToType (Type type, Location loc)
+               public override Constant ConvertImplicitly (Type targetType)
                {
-                       if (!type.IsValueType && !TypeManager.IsEnumType (type))
-                               return this;
+                       if (!TypeManager.IsValueType (targetType))
+                               return new EmptyConstantCast (this, targetType);
 
-                       return base.ToType (type, loc);
+                       return null;
                }
+       }
 
-               public override Constant Reduce(EmitContext ec, Type target_type)
+       //
+       // Represents default(X) when result can be reduced to null
+       //
+       public class NullDefault : EmptyConstantCast
+       {
+               public NullDefault(Constant value, Type type)
+                       : base (value, type)
                {
-                       if (!TypeManager.IsValueType (target_type))
-                               return new NullCast (this, target_type);
+               }
 
-                       return null;
+               public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
+               {
+                       base.Error_ValueCannotBeConverted (ec, loc, target, expl);
                }
        }
 
        //
-       // A null literal in a pointer context
+       // The null Literal constant
        //
-       public class NullPointer : NullLiteral {
-               public static readonly NullLiteral Null;
+       public class NullLiteral : NullConstant {
+               public NullLiteral (Location loc):
+                       base (loc)
+               {
+               }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       type = TypeManager.null_type;
+                       return this;
+               }
+
+               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));
+                       }
+               }
 
-               static NullPointer ()
+               public override Constant ConvertImplicitly (Type targetType)
                {
-                       Null = new NullPointer ();
+                       if (targetType.IsPointer)
+                               return new EmptyConstantCast (NullPointer.Null, targetType);
+
+                       if (TypeManager.IsGenericParameter(targetType)) {
+                               GenericConstraints gc = null;
+
+#if GMCS_SOURCE
+                               gc = TypeManager.GetTypeParameterConstraints(targetType);
+#endif
+                               if (gc != null && gc.IsReferenceType)
+                                       return new EmptyConstantCast (this, targetType);
+
+                               return null;
+                       }
+
+                       return base.ConvertImplicitly(targetType);
                }
 
+       }
+
+       //
+       // A null literal in a pointer context
+       //
+       public class NullPointer : NullLiteral {
+               public static readonly NullLiteral Null = new NullPointer ();
+
                private NullPointer ():
                        base (Location.Null)
                {
@@ -144,6 +202,7 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
                                
+                       // TODO: why not use Ldnull instead ?
                        ig.Emit (OpCodes.Ldc_I4_0);
                        ig.Emit (OpCodes.Conv_U);
                }
@@ -183,6 +242,22 @@ namespace Mono.CSharp {
                        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.EnumToUnderlying (type));
+                               if (c == null)
+                                       return null;
+
+                               return new EnumConstant (c, type);
+                       }
+                       return base.ConvertImplicitly (type);
+               }
+
        }
 
        public class UIntLiteral : UIntConstant {
@@ -246,6 +321,28 @@ namespace Mono.CSharp {
 
                        return this;
                }
+
+               public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
+               {
+                       if (target == TypeManager.float_type) {
+                               Error_664 (loc, "float", "f");
+                               return;
+                       }
+
+                       if (target == TypeManager.decimal_type) {
+                               Error_664 (loc, "decimal", "m");
+                               return;
+                       }
+
+                       base.Error_ValueCannotBeConverted (ec, loc, target, expl);
+               }
+
+               static void Error_664 (Location loc, string type, string suffix)
+               {
+                       Report.Error (664, loc,
+                               "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
+                               type, suffix);
+               }
        }
 
        public class DecimalLiteral : DecimalConstant {