Flush
[mono.git] / mcs / mcs / literal.cs
old mode 100755 (executable)
new mode 100644 (file)
index ac8a49e..77a8832
@@ -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,58 +23,115 @@ 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 null literal
+       //
        public class NullLiteral : Constant {
-               public static readonly NullLiteral Null;
-
-               static NullLiteral ()
+               public NullLiteral (Location loc):
+                       base (loc)
                {
-                       Null = new NullLiteral ();
+                       eclass = ExprClass.Value;
+                       type = typeof (NullLiteral);
                }
-                       
-               public NullLiteral ()
+
+               override public string AsString ()
                {
-                       eclass = ExprClass.Value;
+                       return GetSignatureForError ();
                }
                
-               override public string AsString ()
+               public override Expression CreateExpressionTree (EmitContext ec)
+               {
+                       // HACK: change type to be object
+                       type = TypeManager.object_type;
+                       return base.CreateExpressionTree (ec);
+               }               
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       return this;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       ec.ig.Emit (OpCodes.Ldnull);
+               }
+
+               public override string ExprClassName {
+                       get {
+                               return GetSignatureForError ();
+                       }
+               }
+
+               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);
+                       } else {
+                               Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
+                                       TypeManager.CSharpName(t));
+                       }
+               }
+
+               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 Constant ConvertImplicitly (Type targetType)
+               {
+                       //
+                       // Null literal is of object type
+                       //
+                       if (targetType == TypeManager.object_type)
+                               return this;
+
+                       return ConvertExplicitly (false, targetType);
+               }
+
                public override object GetValue ()
                {
                        return null;
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               public override Constant Increment ()
                {
-                       type = typeof (System.Null); 
-                       return this;
+                       throw new NotSupportedException ();
                }
 
-               public override void Emit (EmitContext ec)
+               public override bool IsDefaultValue 
                {
-                       ec.ig.Emit (OpCodes.Ldnull);
+                       get { return true; }
                }
-               
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
+
                public override bool IsNegative {
-                       get {
-                               return false;
-                       }
+                       get { return false; }
+               }
+
+               public override bool IsNull {
+                       get { return true; }
                }
 
                public override bool IsZeroInteger {
@@ -85,14 +143,8 @@ namespace Mono.CSharp {
        // A null literal in a pointer context
        //
        public class NullPointer : NullLiteral {
-               public new static readonly NullLiteral Null;
-
-               static NullPointer ()
-               {
-                       Null = new NullPointer ();
-               }
-
-               private NullPointer ()
+               public NullPointer (Location loc):
+                       base (loc)
                {
                        type = TypeManager.object_type;
                }
@@ -101,13 +153,16 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
                                
+                       //
+                       // Emits null pointer
+                       //
                        ig.Emit (OpCodes.Ldc_I4_0);
                        ig.Emit (OpCodes.Conv_U);
                }
        }
 
        public class BoolLiteral : BoolConstant {
-               public BoolLiteral (bool val) : base (val)
+               public BoolLiteral (bool val, Location loc) : base (val, loc)
                {
                }
 
@@ -116,10 +171,14 @@ namespace Mono.CSharp {
                        type = TypeManager.bool_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
 
        public class CharLiteral : CharConstant {
-               public CharLiteral (char c) : base (c)
+               public CharLiteral (char c, Location loc) : base (c, loc)
                {
                }
 
@@ -128,18 +187,14 @@ namespace Mono.CSharp {
                        type = TypeManager.char_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
 
        public class IntLiteral : IntConstant {
-               public static IntLiteral One, Zero;
-               
-               static IntLiteral ()
-               {
-                       Zero = new IntLiteral (0);
-                       One = new IntLiteral (1);
-               }
-               
-               public IntLiteral (int l) : base (l)
+               public IntLiteral (int l, Location loc) : base (l, loc)
                {
                }
 
@@ -148,10 +203,29 @@ 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 {
-               public UIntLiteral (uint l) : base (l)
+               public UIntLiteral (uint l, Location loc) : base (l, loc)
                {
                }
 
@@ -160,23 +234,30 @@ namespace Mono.CSharp {
                        type = TypeManager.uint32_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
        
        public class LongLiteral : LongConstant {
-               public LongLiteral (long l) : base (l)
+               public LongLiteral (long l, Location loc) : base (l, loc)
                {
                }
 
                public override Expression DoResolve (EmitContext ec)
                {
                        type = TypeManager.int64_type;
-
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
 
        public class ULongLiteral : ULongConstant {
-               public ULongLiteral (ulong l) : base (l)
+               public ULongLiteral (ulong l, Location loc) : base (l, loc)
                {
                }
 
@@ -185,11 +266,15 @@ namespace Mono.CSharp {
                        type = TypeManager.uint64_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
        
        public class FloatLiteral : FloatConstant {
                
-               public FloatLiteral (float f) : base (f)
+               public FloatLiteral (float f, Location loc) : base (f, loc)
                {
                }
 
@@ -198,10 +283,15 @@ namespace Mono.CSharp {
                        type = TypeManager.float_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
+
        }
 
        public class DoubleLiteral : DoubleConstant {
-               public DoubleLiteral (double d) : base (d)
+               public DoubleLiteral (double d, Location loc) : base (d, loc)
                {
                }
 
@@ -211,10 +301,37 @@ 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 {
-               public DecimalLiteral (decimal d) : base (d)
+               public DecimalLiteral (decimal d, Location loc) : base (d, loc)
                {
                }
 
@@ -223,10 +340,14 @@ namespace Mono.CSharp {
                        type = TypeManager.decimal_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
 
        public class StringLiteral : StringConstant {
-               public StringLiteral (string s) : base (s)
+               public StringLiteral (string s, Location loc) : base (s, loc)
                {
                }
 
@@ -236,5 +357,10 @@ namespace Mono.CSharp {
 
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
+
        }
 }