2009-03-17 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / literal.cs
index 639e5368c81699ea34af5cfb32eab4d7e8c5ceb3..1565dda2daf6e624114cec017f5b0fe6996ee9e4 100644 (file)
@@ -3,8 +3,9 @@
 //
 // 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
@@ -22,113 +23,170 @@ using System;
 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'
+       // The null literal
        //
-       public class NullType {
-       }
-
+       // Note: C# specification null-literal is NullLiteral of NullType type
        //
-       // The null Literal constant
-       //
-       public class NullLiteral : Constant {
+       class NullLiteral : Constant
+       {
+               //
+               // Default type of null is an object
+               //
                public NullLiteral (Location loc):
-                       base (loc)
+                       this (typeof (NullLiteral), loc)
                {
-                       eclass = ExprClass.Value;
                }
-               
-               override public string AsString ()
+
+               //
+               // Null can have its own type, think of default (Foo)
+               //
+               public NullLiteral (Type type, Location loc)
+                       : base (loc)
                {
-                       return "null";
+                       eclass = ExprClass.Value;
+                       this.type = type;
                }
 
-               public override object GetValue ()
+               override public string AsString ()
                {
-                       return null;
+                       return GetSignatureForError ();
                }
+               
+               public override Expression CreateExpressionTree (EmitContext ec)
+               {
+                       // HACK: avoid referencing mcs internal type
+                       if (type == typeof (NullLiteral))
+                               type = TypeManager.object_type;
+
+                       return base.CreateExpressionTree (ec);
+               }               
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       type = TypeManager.null_type;
                        return this;
                }
 
                public override void Emit (EmitContext ec)
                {
                        ec.ig.Emit (OpCodes.Ldnull);
-               }
 
-               public override Constant Increment ()
-               {
-                       throw new NotSupportedException ();
+#if GMCS_SOURCE
+                       // Only to make verifier happy
+                       if (TypeManager.IsGenericParameter (type))
+                               ec.ig.Emit (OpCodes.Unbox_Any, type);
+#endif
                }
 
-               public override bool IsDefaultValue {
+               public override string ExprClassName {
                        get {
-                               return true;
+                               return GetSignatureForError ();
                        }
                }
 
-               public override bool IsNegative {
-                       get {
-                               return false;
+               public override string GetSignatureForError ()
+               {
+                       return "null";
+               }
+
+               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);
+                               return;
+                       }
+
+                       if (TypeManager.IsValueType (t)) {
+                               Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
+                                       TypeManager.CSharpName(t));
+                               return;
                        }
+
+                       base.Error_ValueCannotBeConverted (ec, loc, t, expl);
                }
 
-               public override bool IsZeroInteger {
-                       get { return true; }
+               public override Constant ConvertExplicitly (bool inCheckedContext, Type targetType)
+               {
+                       if (targetType.IsPointer) {
+                               if (type == TypeManager.null_type || this is NullPointer)
+                                       return new EmptyConstantCast (new NullPointer (loc), targetType);
+
+                               return null;
+                       }
+
+                       // Exlude internal compiler types
+                       if (targetType == TypeManager.anonymous_method_type)
+                               return null;
+
+                       if (type != TypeManager.null_type && !Convert.ImplicitStandardConversionExists (this, targetType))
+                               return null;
+
+                       if (TypeManager.IsReferenceType (targetType))
+                               return new NullLiteral (targetType, loc);
+
+                       if (TypeManager.IsNullableType (targetType))
+                               return Nullable.LiftedNull.Create (targetType, loc);
+
+                       return null;
                }
 
-               public override string GetSignatureForError()
+               public override Constant ConvertImplicitly (Type targetType)
                {
-                       return "null";
+                       //
+                       // Null literal is of object type
+                       //
+                       if (targetType == TypeManager.object_type)
+                               return this;
+
+                       return ConvertExplicitly (false, targetType);
                }
 
-               public override void Error_ValueCannotBeConverted (Location loc, Type t, bool expl)
+               public override object GetValue ()
                {
-                       Report.Error (37, loc, "Cannot convert null to `{0}' because it is a value type",
-                               TypeManager.CSharpName (t));
+                       return null;
                }
 
-               public override Constant ToType (Type type, Location loc)
+               public override Constant Increment ()
                {
-                       if (!type.IsValueType && !TypeManager.IsEnumType (type))
-                               return this;
+                       throw new NotSupportedException ();
+               }
 
-                       return base.ToType (type, loc);
+               public override bool IsDefaultValue {
+                       get { return true; }
                }
 
-       }
+               public override bool IsLiteral {
+                       get { return true; }
+               }
 
-       //
-       // A null literal in a pointer context
-       //
-       public class NullPointer : NullLiteral {
-               public static readonly NullLiteral Null;
+               public override bool IsNegative {
+                       get { return false; }
+               }
+
+               public override bool IsNull {
+                       get { return true; }
+               }
 
-               static NullPointer ()
+               public override bool IsZeroInteger {
+                       get { return true; }
+               }
+               
+               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
-                       Null = new NullPointer ();
+                       type = storey.MutateType (type);
                }
+       }
 
-               private NullPointer ():
-                       base (Location.Null)
+       //
+       // A null literal in a pointer context
+       //
+       class NullPointer : NullLiteral {
+               public NullPointer (Location loc):
+                       base (loc)
                {
                        type = TypeManager.object_type;
                }
@@ -137,6 +195,9 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
                                
+                       //
+                       // Emits null pointer
+                       //
                        ig.Emit (OpCodes.Ldc_I4_0);
                        ig.Emit (OpCodes.Conv_U);
                }
@@ -152,6 +213,10 @@ namespace Mono.CSharp {
                        type = TypeManager.bool_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
 
        public class CharLiteral : CharConstant {
@@ -164,6 +229,10 @@ namespace Mono.CSharp {
                        type = TypeManager.char_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
 
        public class IntLiteral : IntConstant {
@@ -176,6 +245,25 @@ 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.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 {
@@ -188,6 +276,10 @@ namespace Mono.CSharp {
                        type = TypeManager.uint32_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
        
        public class LongLiteral : LongConstant {
@@ -198,9 +290,12 @@ namespace Mono.CSharp {
                public override Expression DoResolve (EmitContext ec)
                {
                        type = TypeManager.int64_type;
-
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
 
        public class ULongLiteral : ULongConstant {
@@ -213,6 +308,10 @@ namespace Mono.CSharp {
                        type = TypeManager.uint64_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
        
        public class FloatLiteral : FloatConstant {
@@ -226,6 +325,11 @@ namespace Mono.CSharp {
                        type = TypeManager.float_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
+
        }
 
        public class DoubleLiteral : DoubleConstant {
@@ -239,6 +343,33 @@ 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 override bool IsLiteral {
+                       get { return true; }
+               }
+
        }
 
        public class DecimalLiteral : DecimalConstant {
@@ -251,6 +382,10 @@ namespace Mono.CSharp {
                        type = TypeManager.decimal_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
 
        public class StringLiteral : StringConstant {
@@ -264,5 +399,10 @@ namespace Mono.CSharp {
 
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
+
        }
 }