2008-06-26 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / literal.cs
index 8f4cb6e60d3ba67375602ff23ca5dc1265d3f5bf..bc1482a10dda2b384324fdc29200794652d15f40 100644 (file)
@@ -5,7 +5,7 @@
 //   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
@@ -23,169 +23,128 @@ 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 {
-       }
-
-
-       public abstract class NullConstant : Constant
-       {
-               public NullConstant (Location loc):
+       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 Expression CreateExpressionTree (EmitContext ec)
+               {
+                       // HACK: change type to be object
+                       type = TypeManager.object_type;
+                       return base.CreateExpressionTree (ec);
+               }               
 
-               public override object GetValue ()
+               public override Expression DoResolve (EmitContext ec)
                {
-                       return null;
+                       return this;
                }
 
                public override void Emit (EmitContext ec)
                {
-                       ec.ig.Emit(OpCodes.Ldnull);
+                       ec.ig.Emit (OpCodes.Ldnull);
                }
 
-               public override string GetSignatureForError ()
-               {
-                       return "null";
+               public override string ExprClassName {
+                       get {
+                               return GetSignatureForError ();
+                       }
                }
 
-               public override Constant Increment ()
+               public override string GetSignatureForError ()
                {
-                       throw new NotSupportedException ();
+                       return "null";
                }
 
-               public override bool IsDefaultValue 
+               public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type t, bool expl)
                {
-                       get { return true; }
+                       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 
+               public override Constant ConvertImplicitly (Type targetType)
                {
-                       get { return false; }
-               }
+                       // FIXME: Null literal should be of object type
+                       //if (targetType == TypeManager.object_type)
+                       //      return this;
+               
+                       if (targetType.IsPointer)
+                               return new EmptyConstantCast (NullPointer.Null, targetType);
 
-               public override bool IsZeroInteger 
-               {
-                       get { return true; }
-               }
+                       // Exlude internal compiler types
+                       if (targetType == TypeManager.anonymous_method_type)
+                               return null;
 
-               public override Constant Reduce(bool inCheckedContext, Type target_type)
-               {
-                       if (!TypeManager.IsValueType (target_type))
-                               return new EmptyConstantCast (this, target_type);
+                       if (TypeManager.IsReferenceType (targetType))
+                               return new EmptyConstantCast (this, targetType);
 
                        return null;
                }
 
-               public override Constant ToType(Type targetType)
+               public override object GetValue ()
                {
-                       if (!TypeManager.IsValueType (targetType))
-                               return new EmptyConstantCast (this, targetType);
-
                        return null;
                }
-       }
 
-       //
-       // Represents default(X) when result can be reduced to null
-       //
-       public class NullDefault : EmptyConstantCast
-       {
-               public NullDefault(Constant value, Type type)
-                       : base (value, type)
+               public override Constant Increment ()
                {
+                       throw new NotSupportedException ();
                }
 
-               public override void Error_ValueCannotBeConverted (Location loc, Type target, bool expl)
+               public override bool IsDefaultValue 
                {
-                       base.Error_ValueCannotBeConverted(loc, target, expl);
+                       get { return true; }
                }
-       }
 
-       //
-       // The null Literal constant
-       //
-       public class NullLiteral : NullConstant {
-               public NullLiteral (Location loc):
-                       base (loc)
-               {
+               public override bool IsLiteral {
+                       get { return true; }
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.null_type;
-                       return this;
+               public override bool IsNegative {
+                       get { return false; }
                }
 
-               public override void Error_ValueCannotBeConverted (Location loc, Type t, bool expl)
-               {
-                       if (TypeManager.IsGenericParameter (t)) {
-                               Report.Error(403, loc,
-                                       "Cannot convert null to the type parameter `{0}' becaues 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 IsNull {
+                       get { return true; }
                }
 
-               public override Constant ToType (Type targetType)
-               {
-                       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);
+               public override bool IsZeroInteger {
+                       get { return true; }
+               }
 
-                               Error_ValueCannotBeConverted (loc, targetType, false);
-                               return null;
-                       }
+               public override Constant ConvertExplicitly(bool inCheckedContext, Type target_type)
+               {
+                       if (!TypeManager.IsValueType (target_type))
+                               return new EmptyConstantCast (this, target_type);
 
-                       return base.ToType(targetType);
+                       return null;
                }
-
        }
 
        //
        // A null literal in a pointer context
        //
        public class NullPointer : NullLiteral {
-               public static readonly NullLiteral Null;
-
-               static NullPointer ()
-               {
-                       Null = new NullPointer ();
-               }
+               public static readonly NullLiteral Null = new NullPointer ();
 
                private NullPointer ():
                        base (Location.Null)
@@ -213,6 +172,10 @@ namespace Mono.CSharp {
                        type = TypeManager.bool_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
 
        public class CharLiteral : CharConstant {
@@ -225,6 +188,10 @@ namespace Mono.CSharp {
                        type = TypeManager.char_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
 
        public class IntLiteral : IntConstant {
@@ -237,6 +204,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 {
@@ -249,6 +235,10 @@ namespace Mono.CSharp {
                        type = TypeManager.uint32_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
        
        public class LongLiteral : LongConstant {
@@ -259,9 +249,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 {
@@ -274,6 +267,10 @@ namespace Mono.CSharp {
                        type = TypeManager.uint64_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
        
        public class FloatLiteral : FloatConstant {
@@ -287,6 +284,11 @@ namespace Mono.CSharp {
                        type = TypeManager.float_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
+
        }
 
        public class DoubleLiteral : DoubleConstant {
@@ -301,7 +303,7 @@ namespace Mono.CSharp {
                        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");
@@ -313,7 +315,7 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       base.Error_ValueCannotBeConverted (loc, target, expl);
+                       base.Error_ValueCannotBeConverted (ec, loc, target, expl);
                }
 
                static void Error_664 (Location loc, string type, string suffix)
@@ -322,6 +324,11 @@ namespace Mono.CSharp {
                                "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 {
@@ -334,6 +341,10 @@ namespace Mono.CSharp {
                        type = TypeManager.decimal_type;
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
        }
 
        public class StringLiteral : StringConstant {
@@ -347,5 +358,10 @@ namespace Mono.CSharp {
 
                        return this;
                }
+
+               public override bool IsLiteral {
+                       get { return true; }
+               }
+
        }
 }