Fix #77628.
[mono.git] / mcs / mcs / literal.cs
old mode 100755 (executable)
new mode 100644 (file)
index 842bbe6..c7aae92
@@ -22,20 +22,35 @@ 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 constant
+       //
        public class NullLiteral : Constant {
-               public static readonly NullLiteral Null;
-               
-               static NullLiteral ()
-               {
-                       Null = new NullLiteral ();
-               }
-                       
-               public NullLiteral ()
+               public NullLiteral (Location loc):
+                       base (loc)
                {
-                       if (Null != null)
-                               throw new Exception ("More than one null has been created!");
+                       eclass = ExprClass.Value;
                }
                
                override public string AsString ()
@@ -50,7 +65,7 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       type = TypeManager.object_type;
+                       type = TypeManager.null_type;
                        return this;
                }
 
@@ -58,10 +73,84 @@ namespace Mono.CSharp {
                {
                        ec.ig.Emit (OpCodes.Ldnull);
                }
+
+               public override Constant Increment ()
+               {
+                       throw new NotSupportedException ();
+               }
+
+               public override bool IsDefaultValue {
+                       get {
+                               return true;
+                       }
+               }
+
+               public override bool IsNegative {
+                       get {
+                               return false;
+                       }
+               }
+
+               public override bool IsZeroInteger {
+                       get { return true; }
+               }
+
+               public override string GetSignatureForError()
+               {
+                       return "null";
+               }
+
+               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));
+               }
+
+               public override Constant ToType (Type type, Location loc)
+               {
+                       if (!type.IsValueType && !TypeManager.IsEnumType (type))
+                               return this;
+
+                       return base.ToType (type, loc);
+               }
+
+               public override Constant Reduce(EmitContext ec, Type target_type)
+               {
+                       if (!TypeManager.IsValueType (target_type))
+                               return new NullCast (this, target_type);
+
+                       return 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)
+               {
+                       type = TypeManager.object_type;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       ILGenerator ig = ec.ig;
+                               
+                       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)
                {
                }
 
@@ -73,7 +162,7 @@ namespace Mono.CSharp {
        }
 
        public class CharLiteral : CharConstant {
-               public CharLiteral (char c) : base (c)
+               public CharLiteral (char c, Location loc) : base (c, loc)
                {
                }
 
@@ -85,7 +174,7 @@ namespace Mono.CSharp {
        }
 
        public class IntLiteral : IntConstant {
-               public IntLiteral (int l) : base (l)
+               public IntLiteral (int l, Location loc) : base (l, loc)
                {
                }
 
@@ -97,7 +186,7 @@ namespace Mono.CSharp {
        }
 
        public class UIntLiteral : UIntConstant {
-               public UIntLiteral (uint l) : base (l)
+               public UIntLiteral (uint l, Location loc) : base (l, loc)
                {
                }
 
@@ -109,7 +198,7 @@ namespace Mono.CSharp {
        }
        
        public class LongLiteral : LongConstant {
-               public LongLiteral (long l) : base (l)
+               public LongLiteral (long l, Location loc) : base (l, loc)
                {
                }
 
@@ -122,7 +211,7 @@ namespace Mono.CSharp {
        }
 
        public class ULongLiteral : ULongConstant {
-               public ULongLiteral (ulong l) : base (l)
+               public ULongLiteral (ulong l, Location loc) : base (l, loc)
                {
                }
 
@@ -135,7 +224,7 @@ namespace Mono.CSharp {
        
        public class FloatLiteral : FloatConstant {
                
-               public FloatLiteral (float f) : base (f)
+               public FloatLiteral (float f, Location loc) : base (f, loc)
                {
                }
 
@@ -147,7 +236,7 @@ namespace Mono.CSharp {
        }
 
        public class DoubleLiteral : DoubleConstant {
-               public DoubleLiteral (double d) : base (d)
+               public DoubleLiteral (double d, Location loc) : base (d, loc)
                {
                }
 
@@ -160,7 +249,7 @@ namespace Mono.CSharp {
        }
 
        public class DecimalLiteral : DecimalConstant {
-               public DecimalLiteral (decimal d) : base (d)
+               public DecimalLiteral (decimal d, Location loc) : base (d, loc)
                {
                }
 
@@ -169,15 +258,10 @@ namespace Mono.CSharp {
                        type = TypeManager.decimal_type;
                        return this;
                }
-
-               public override void Emit (EmitContext ec)
-               {
-                       throw new Exception ("Implement me");
-               }
        }
 
        public class StringLiteral : StringConstant {
-               public StringLiteral (string s) : base (s)
+               public StringLiteral (string s, Location loc) : base (s, loc)
                {
                }