[xbuild] Fix bug #674337.
[mono.git] / mcs / mcs / literal.cs
index 77582d130afa3d1106ac96de87026e6a616ce81c..2ef541d1cbb880435fb995f574b1c150e0227cf5 100644 (file)
 // and are fully resolved when born.
 //
 
-using System;
-using System.Reflection;
+#if STATIC
+using IKVM.Reflection.Emit;
+#else
 using System.Reflection.Emit;
+#endif
 
 namespace Mono.CSharp {
 
        //
        // The null literal
        //
-       public class NullLiteral : Constant {
-               public NullLiteral (Location loc):
-                       base (loc)
-               {
-                       eclass = ExprClass.Value;
-                       type = typeof (NullLiteral);
-               }
-
-               override public string AsString ()
-               {
-                       return GetSignatureForError ();
-               }
-               
-               public override Expression CreateExpressionTree (EmitContext ec)
-               {
-                       type = TypeManager.object_type;
-                       return base.CreateExpressionTree (ec);
-               }               
-
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       return this;
-               }
-
-               public override void Emit (EmitContext ec)
+       // Note: C# specification null-literal is NullLiteral of NullType type
+       //
+       public class NullLiteral : NullConstant
+       {
+               //
+               // Default type of null is an object
+               //
+               public NullLiteral (Location loc)
+                       : base (InternalType.Null, loc)
                {
-                       ec.ig.Emit (OpCodes.Ldnull);
-               }
-
-               public override string ExprClassName {
-                       get {
-                               return GetSignatureForError ();
-                       }
                }
 
-               public override string GetSignatureForError ()
+               public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec t, bool expl)
                {
-                       return "null";
-               }
-
-               public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type t, bool expl)
-               {
-                       if (TypeManager.IsGenericParameter (t)) {
-                               Report.Error(403, loc,
+                       if (t.IsGenericParameter) {
+                               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);
-                       } else {
-                               Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
-                                       TypeManager.CSharpName(t));
+                               return;
                        }
-               }
-
-               public override Constant ConvertImplicitly (Type targetType)
-               {
-                       if (targetType.IsPointer)
-                               return new EmptyConstantCast (NullPointer.Null, targetType);
-
-                       if (TypeManager.IsReferenceType (targetType))
-                               return new EmptyConstantCast (this, targetType);
 
-                       return null;
-               }
-
-               public override object GetValue ()
-               {
-                       return null;
-               }
+                       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 Constant Increment ()
-               {
-                       throw new NotSupportedException ();
+                       base.Error_ValueCannotBeConverted (ec, loc, t, expl);
                }
 
-               public override bool IsDefaultValue 
+               public override string GetValueAsLiteral ()
                {
-                       get { return true; }
+                       return "null";
                }
 
                public override bool IsLiteral {
                        get { return true; }
                }
 
-               public override bool IsNegative {
-                       get { return false; }
-               }
-
-               public override bool IsNull {
-                       get { return true; }
-               }
-
-               public override bool IsZeroInteger {
-                       get { return true; }
-               }
-
-               public override Constant ConvertExplicitly(bool inCheckedContext, Type target_type)
+               public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
                {
-                       if (!TypeManager.IsValueType (target_type))
-                               return new EmptyConstantCast (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 = new NullPointer ();
-
-               private NullPointer ():
-                       base (Location.Null)
+       class NullPointer : NullLiteral {
+               public NullPointer (Location loc):
+                       base (loc)
                {
                        type = TypeManager.object_type;
                }
 
                public override void Emit (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-                               
-                       // TODO: why not use Ldnull instead ?
-                       ig.Emit (OpCodes.Ldc_I4_0);
-                       ig.Emit (OpCodes.Conv_U);
+                       //
+                       // Emits null pointer
+                       //
+                       ec.Emit (OpCodes.Ldc_I4_0);
+                       ec.Emit (OpCodes.Conv_U);
                }
        }
 
@@ -158,12 +100,6 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.bool_type;
-                       return this;
-               }
-
                public override bool IsLiteral {
                        get { return true; }
                }
@@ -174,12 +110,6 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.char_type;
-                       return this;
-               }
-
                public override bool IsLiteral {
                        get { return true; }
                }
@@ -190,25 +120,20 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.int32_type;
-                       return this;
-               }
-
-               public override Constant ConvertImplicitly (Type type)
+               public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
                {
-                       ///
-                       /// The 0 literal can be converted to an enum value,
-                       ///
+                       //
+                       // The 0 literal can be converted to an enum value
+                       //
                        if (Value == 0 && TypeManager.IsEnumType (type)) {
-                               Constant c = ConvertImplicitly (TypeManager.GetEnumUnderlyingType (type));
+                               Constant c = ConvertImplicitly (rc, EnumSpec.GetUnderlyingType (type));
                                if (c == null)
                                        return null;
 
-                               return new EnumConstant (c, type);
+                               return new EnumConstant (c, type).Resolve (rc);
                        }
-                       return base.ConvertImplicitly (type);
+
+                       return base.ConvertImplicitly (rc, type);
                }
 
                public override bool IsLiteral {
@@ -221,12 +146,6 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.uint32_type;
-                       return this;
-               }
-
                public override bool IsLiteral {
                        get { return true; }
                }
@@ -237,12 +156,6 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.int64_type;
-                       return this;
-               }
-
                public override bool IsLiteral {
                        get { return true; }
                }
@@ -253,12 +166,6 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.uint64_type;
-                       return this;
-               }
-
                public override bool IsLiteral {
                        get { return true; }
                }
@@ -270,12 +177,6 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.float_type;
-                       return this;
-               }
-
                public override bool IsLiteral {
                        get { return true; }
                }
@@ -287,31 +188,24 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.double_type;
-
-                       return this;
-               }
-
-               public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
+               public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
                {
                        if (target == TypeManager.float_type) {
-                               Error_664 (loc, "float", "f");
+                               Error_664 (ec, loc, "float", "f");
                                return;
                        }
 
                        if (target == TypeManager.decimal_type) {
-                               Error_664 (loc, "decimal", "m");
+                               Error_664 (ec, loc, "decimal", "m");
                                return;
                        }
 
                        base.Error_ValueCannotBeConverted (ec, loc, target, expl);
                }
 
-               static void Error_664 (Location loc, string type, string suffix)
+               static void Error_664 (ResolveContext ec, Location loc, string type, string suffix)
                {
-                       Report.Error (664, loc,
+                       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);
                }
@@ -327,12 +221,6 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.decimal_type;
-                       return this;
-               }
-
                public override bool IsLiteral {
                        get { return true; }
                }
@@ -343,13 +231,6 @@ namespace Mono.CSharp {
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.string_type;
-
-                       return this;
-               }
-
                public override bool IsLiteral {
                        get { return true; }
                }