2010-01-07 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / literal.cs
index c7aae9204b0a775c51648643e8ade9d926fb42ca..f371812c6268f0a5fba0704f4aed6a06d7d4a44b 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,120 +23,76 @@ 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'
-       //
-       public class NullType {
-       }
-
+       // The null literal
        //
-       // The null Literal constant
+       // Note: C# specification null-literal is NullLiteral of NullType type
        //
-       public class NullLiteral : Constant {
-               public NullLiteral (Location loc):
-                       base (loc)
-               {
-                       eclass = ExprClass.Value;
-               }
-               
-               override public string AsString ()
-               {
-                       return "null";
-               }
-
-               public override object GetValue ()
-               {
-                       return null;
-               }
-
-               public override Expression DoResolve (EmitContext ec)
+       public class NullLiteral : NullConstant
+       {
+               //
+               // Default type of null is an object
+               //
+               public NullLiteral (Location loc)
+                       : base (typeof (NullLiteral), loc)
                {
-                       type = TypeManager.null_type;
-                       return this;
                }
 
-               public override void Emit (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       ec.ig.Emit (OpCodes.Ldnull);
-               }
+                       // Optimized version, also avoids referencing literal internal type
+                       Arguments args = new Arguments (1);
+                       args.Add (new Argument (this));
+                       return CreateExpressionFactoryCall (ec, "Constant", args);
+               }               
 
-               public override Constant Increment ()
+               public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, Type t, bool expl)
                {
-                       throw new NotSupportedException ();
-               }
-
-               public override bool IsDefaultValue {
-                       get {
-                               return true;
+                       if (TypeManager.IsGenericParameter (t)) {
+                               ec.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;
                        }
-               }
 
-               public override bool IsNegative {
-                       get {
-                               return false;
+                       if (TypeManager.IsValueType (t)) {
+                               ec.Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
+                                       TypeManager.CSharpName(t));
+                               return;
                        }
-               }
 
-               public override bool IsZeroInteger {
-                       get { return true; }
+                       base.Error_ValueCannotBeConverted (ec, loc, t, expl);
                }
 
-               public override string GetSignatureForError()
+               public override Constant ConvertImplicitly (ResolveContext rc, Type targetType)
                {
-                       return "null";
-               }
+                       //
+                       // Null literal is of object type
+                       //
+                       if (targetType == TypeManager.object_type)
+                               return this;
 
-               public override void Error_ValueCannotBeConverted (Location loc, Type t, bool expl)
-               {
-                       Report.Error (37, loc, "Cannot convert null to `{0}' because it is a value type",
-                               TypeManager.CSharpName (t));
+                       return base.ConvertImplicitly (rc, targetType);
                }
 
-               public override Constant ToType (Type type, Location loc)
-               {
-                       if (!type.IsValueType && !TypeManager.IsEnumType (type))
-                               return this;
-
-                       return base.ToType (type, loc);
+               public override bool IsLiteral {
+                       get { return true; }
                }
 
-               public override Constant Reduce(EmitContext ec, Type target_type)
+               public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
                {
-                       if (!TypeManager.IsValueType (target_type))
-                               return new NullCast (this, target_type);
-
-                       return null;
+                       return System.Linq.Expressions.Expression.Constant (null);
                }
        }
 
        //
        // 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)
+       class NullPointer : NullLiteral {
+               public NullPointer (Location loc):
+                       base (loc)
                {
                        type = TypeManager.object_type;
                }
@@ -144,6 +101,9 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
                                
+                       //
+                       // Emits null pointer
+                       //
                        ig.Emit (OpCodes.Ldc_I4_0);
                        ig.Emit (OpCodes.Conv_U);
                }
@@ -154,10 +114,8 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.bool_type;
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
        }
 
@@ -166,10 +124,8 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.char_type;
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
        }
 
@@ -178,10 +134,24 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               public override Constant ConvertImplicitly (ResolveContext rc, Type type)
                {
-                       type = TypeManager.int32_type;
-                       return this;
+                       //
+                       // The 0 literal can be converted to an enum value
+                       //
+                       if (Value == 0 && TypeManager.IsEnumType (type)) {
+                               Constant c = ConvertImplicitly (rc, TypeManager.GetEnumUnderlyingType (type));
+                               if (c == null)
+                                       return null;
+
+                               return new EnumConstant (c, type).Resolve (rc);
+                       }
+
+                       return base.ConvertImplicitly (rc, type);
+               }
+
+               public override bool IsLiteral {
+                       get { return true; }
                }
        }
 
@@ -190,10 +160,8 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.uint32_type;
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
        }
        
@@ -202,11 +170,8 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.int64_type;
-
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
        }
 
@@ -215,10 +180,8 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.uint64_type;
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
        }
        
@@ -228,11 +191,10 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.float_type;
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
+
        }
 
        public class DoubleLiteral : DoubleConstant {
@@ -240,12 +202,32 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, Type target, bool expl)
                {
-                       type = TypeManager.double_type;
+                       if (target == TypeManager.float_type) {
+                               Error_664 (ec, loc, "float", "f");
+                               return;
+                       }
+
+                       if (target == TypeManager.decimal_type) {
+                               Error_664 (ec, loc, "decimal", "m");
+                               return;
+                       }
 
-                       return this;
+                       base.Error_ValueCannotBeConverted (ec, loc, target, expl);
                }
+
+               static void Error_664 (ResolveContext ec, Location loc, string type, string suffix)
+               {
+                       ec.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 {
@@ -253,10 +235,8 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.decimal_type;
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
        }
 
@@ -265,11 +245,9 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.string_type;
-
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
+
        }
 }