Check whether parent struct field is assigned when assigning to child field. Fixes...
[mono.git] / mcs / mcs / literal.cs
index 42762cb2c0ba2d50bf330cbe02bdfeb660741657..b2efb35c1a95d74106eca17124da57f0bdd9f5a0 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
 // and are fully resolved when born.
 //
 
-using System;
-using System.Reflection;
+#if STATIC
+using IKVM.Reflection.Emit;
+#else
 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 {
+#endif
+
+namespace Mono.CSharp
+{
+       public interface ILiteralConstant
+       {
+#if FULL_AST
+               char[] ParsedValue { get; set; }
+#endif
        }
-}
-       
-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 {
-       }
-
+       // Note: C# specification null-literal is NullLiteral of NullType type
        //
-       // The null Literal constant
-       //
-       public class NullLiteral : Constant {
-               public static readonly NullLiteral Null;
-
-               static NullLiteral ()
+       public class NullLiteral : NullConstant
+       {
+               //
+               // Default type of null is an object
+               //
+               public NullLiteral (Location loc)
+                       : base (InternalType.NullLiteral, loc)
                {
-                       Null = new NullLiteral ();
                }
-                       
-               public NullLiteral ()
+
+               public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec t, bool expl)
                {
-                       eclass = ExprClass.Value;
+                       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);
+                               return;
+                       }
+
+                       if (TypeSpec.IsValueType (t)) {
+                               ec.Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
+                                       TypeManager.CSharpName(t));
+                               return;
+                       }
+
+                       base.Error_ValueCannotBeConverted (ec, loc, t, expl);
                }
-               
-               override public string AsString ()
+
+               public override string GetValueAsLiteral ()
                {
                        return "null";
                }
 
-               public override object GetValue ()
-               {
-                       return null;
+               public override bool IsLiteral {
+                       get { return true; }
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
                {
-                       type = TypeManager.null_type;
-                       return this;
+                       return System.Linq.Expressions.Expression.Constant (null);
                }
+       }
 
-               public override void Emit (EmitContext ec)
+       public class BoolLiteral : BoolConstant, ILiteralConstant
+       {
+               public BoolLiteral (BuiltinTypes types, bool val, Location loc)
+                       : base (types, val, loc)
                {
-                       ec.ig.Emit (OpCodes.Ldnull);
                }
 
-               public override bool IsDefaultValue {
-                       get {
-                               return true;
-                       }
-               }
-
-               public override bool IsNegative {
-                       get {
-                               return false;
-                       }
-               }
-
-               public override bool IsZeroInteger {
+               public override bool IsLiteral {
                        get { return true; }
                }
-       }
 
-       //
-       // A null literal in a pointer context
-       //
-       public class NullPointer : NullLiteral {
-               public new static readonly NullLiteral Null;
+#if FULL_AST
+               char[] ILiteralConstant.ParsedValue { get; set; }
+#endif
+       }
 
-               static NullPointer ()
+       public class CharLiteral : CharConstant, ILiteralConstant
+       {
+               public CharLiteral (BuiltinTypes types, char c, Location loc)
+                       : base (types, c, loc)
                {
-                       Null = new NullPointer ();
                }
 
-               private NullPointer ()
-               {
-                       type = TypeManager.object_type;
+               public override bool IsLiteral {
+                       get { return true; }
                }
 
-               public override void Emit (EmitContext ec)
-               {
-                       ILGenerator ig = ec.ig;
-                               
-                       ig.Emit (OpCodes.Ldc_I4_0);
-                       ig.Emit (OpCodes.Conv_U);
-               }
+#if FULL_AST
+               char[] ILiteralConstant.ParsedValue { get; set; }
+#endif
        }
 
-       public class BoolLiteral : BoolConstant {
-               public BoolLiteral (bool val) : base (val)
+       public class IntLiteral : IntConstant, ILiteralConstant
+       {
+               public IntLiteral (BuiltinTypes types, int l, Location loc)
+                       : base (types, l, loc)
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               public override Constant ConvertImplicitly (TypeSpec type)
                {
-                       type = TypeManager.bool_type;
-                       return this;
-               }
-       }
+                       //
+                       // The 0 literal can be converted to an enum value
+                       //
+                       if (Value == 0 && TypeManager.IsEnumType (type)) {
+                               Constant c = ConvertImplicitly (EnumSpec.GetUnderlyingType (type));
+                               if (c == null)
+                                       return null;
+
+                               return new EnumConstant (c, type);
+                       }
 
-       public class CharLiteral : CharConstant {
-               public CharLiteral (char c) : base (c)
-               {
+                       return base.ConvertImplicitly (type);
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.char_type;
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
+
+#if FULL_AST
+               char[] ILiteralConstant.ParsedValue { get; set; }
+#endif
        }
 
-       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 class UIntLiteral : UIntConstant, ILiteralConstant
+       {
+               public UIntLiteral (BuiltinTypes types, uint l, Location loc)
+                       : base (types, l, loc)
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.int32_type;
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
+
+#if FULL_AST
+               char[] ILiteralConstant.ParsedValue { get; set; }
+#endif
        }
 
-       public class UIntLiteral : UIntConstant {
-               public UIntLiteral (uint l) : base (l)
+       public class LongLiteral : LongConstant, ILiteralConstant
+       {
+               public LongLiteral (BuiltinTypes types, long l, Location loc)
+                       : base (types, l, loc)
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.uint32_type;
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
+
+#if FULL_AST
+               char[] ILiteralConstant.ParsedValue { get; set; }
+#endif
        }
-       
-       public class LongLiteral : LongConstant {
-               public LongLiteral (long l) : base (l)
-               {
-               }
 
-               public override Expression DoResolve (EmitContext ec)
+       public class ULongLiteral : ULongConstant, ILiteralConstant
+       {
+               public ULongLiteral (BuiltinTypes types, ulong l, Location loc)
+                       : base (types, l, loc)
                {
-                       type = TypeManager.int64_type;
+               }
 
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
+
+#if FULL_AST
+               char[] ILiteralConstant.ParsedValue { get; set; }
+#endif
        }
 
-       public class ULongLiteral : ULongConstant {
-               public ULongLiteral (ulong l) : base (l)
+       public class FloatLiteral : FloatConstant, ILiteralConstant
+       {
+               public FloatLiteral (BuiltinTypes types, float f, Location loc)
+                       : base (types, f, loc)
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.uint64_type;
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
+
+#if FULL_AST
+               char[] ILiteralConstant.ParsedValue { get; set; }
+#endif
        }
-       
-       public class FloatLiteral : FloatConstant {
-               
-               public FloatLiteral (float f) : base (f)
-               {
-               }
 
-               public override Expression DoResolve (EmitContext ec)
+       public class DoubleLiteral : DoubleConstant, ILiteralConstant
+       {
+               public DoubleLiteral (BuiltinTypes types, double d, Location loc)
+                       : base (types, d, loc)
                {
-                       type = TypeManager.float_type;
-                       return this;
                }
-       }
 
-       public class DoubleLiteral : DoubleConstant {
-               public DoubleLiteral (double d) : base (d)
+               public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
                {
+                       if (target.BuiltinType == BuiltinTypeSpec.Type.Float) {
+                               Error_664 (ec, loc, "float", "f");
+                               return;
+                       }
+
+                       if (target.BuiltinType == BuiltinTypeSpec.Type.Decimal) {
+                               Error_664 (ec, loc, "decimal", "m");
+                               return;
+                       }
+
+                       base.Error_ValueCannotBeConverted (ec, loc, target, expl);
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               static void Error_664 (ResolveContext ec, Location loc, string type, string suffix)
                {
-                       type = TypeManager.double_type;
+                       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);
+               }
 
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
+
+#if FULL_AST
+               char[] ILiteralConstant.ParsedValue { get; set; }
+#endif
        }
 
-       public class DecimalLiteral : DecimalConstant {
-               public DecimalLiteral (decimal d) : base (d)
+       public class DecimalLiteral : DecimalConstant, ILiteralConstant
+       {
+               public DecimalLiteral (BuiltinTypes types, decimal d, Location loc)
+                       : base (types, d, loc)
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.decimal_type;
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
+
+#if FULL_AST
+               char[] ILiteralConstant.ParsedValue { get; set; }
+#endif
        }
 
-       public class StringLiteral : StringConstant {
-               public StringLiteral (string s) : base (s)
+       public class StringLiteral : StringConstant, ILiteralConstant
+       {
+               public StringLiteral (BuiltinTypes types, string s, Location loc)
+                       : base (types, s, loc)
                {
                }
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       type = TypeManager.string_type;
-
-                       return this;
+               public override bool IsLiteral {
+                       get { return true; }
                }
+
+#if FULL_AST
+               char[] ILiteralConstant.ParsedValue { get; set; }
+#endif
        }
 }