Initial set of Ward sgen annotations (#5705)
[mono.git] / mcs / mcs / literal.cs
index b23d0004c0e692572c3b89bf037eff9faf679c39..1af2d0c9e379ecb600379156bfe84dbf1ab67bb9 100644 (file)
@@ -6,6 +6,7 @@
 //   Marek Safar (marek.safar@seznam.cz)
 //
 // Copyright 2001 Ximian, Inc.
+// Copyright 2011 Xamarin 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;
+#endif
 
-namespace Mono.CSharp {
+namespace Mono.CSharp
+{
+       public interface ILiteralConstant
+       {
+#if FULL_AST
+               char[] ParsedValue { get; set; }
+#endif
+       }
 
        //
        // The null literal
        //
        // Note: C# specification null-literal is NullLiteral of NullType type
        //
-       class NullLiteral : Constant
+       public class NullLiteral : NullConstant
        {
                //
                // Default type of null is an object
                //
-               public NullLiteral (Location loc):
-                       this (typeof (NullLiteral), loc)
-               {
-               }
-
-               //
-               // Null can have its own type, think of default (Foo)
-               //
-               public NullLiteral (Type type, Location loc)
-                       : base (loc)
-               {
-                       eclass = ExprClass.Value;
-                       this.type = type;
-               }
-
-               override public string AsString ()
-               {
-                       return GetSignatureForError ();
-               }
-               
-               public override Expression CreateExpressionTree (EmitContext ec)
-               {
-                       // HACK: avoid referencing mcs internal type
-                       if (type == typeof (NullLiteral))
-                               type = TypeManager.object_type;
-
-                       return base.CreateExpressionTree (ec);
-               }               
-
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       return this;
-               }
-
-               public override void Emit (EmitContext ec)
-               {
-                       ec.ig.Emit (OpCodes.Ldnull);
-
-#if GMCS_SOURCE
-                       // Only to make verifier happy
-                       if (TypeManager.IsGenericParameter (type))
-                               ec.ig.Emit (OpCodes.Unbox_Any, type);
-#endif
-               }
-
-               public override string ExprClassName {
-                       get {
-                               return GetSignatureForError ();
-                       }
-               }
-
-               public override string GetSignatureForError ()
+               public NullLiteral (Location loc)
+                       : base (InternalType.NullLiteral, loc)
                {
-                       return "null";
                }
 
-               public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type t, bool expl)
+               public override void Error_ValueCannotBeConverted (ResolveContext ec, TypeSpec 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);
                                return;
                        }
 
-                       if (TypeManager.IsValueType (t)) {
-                               Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
-                                       TypeManager.CSharpName(t));
+                       if (TypeSpec.IsValueType (t)) {
+                               ec.Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
+                                       t.GetSignatureForError ());
                                return;
                        }
 
-                       base.Error_ValueCannotBeConverted (ec, loc, t, expl);
+                       base.Error_ValueCannotBeConverted (ec, t, expl);
                }
 
-               public override Constant ConvertExplicitly (bool inCheckedContext, Type targetType)
+               public override string GetValueAsLiteral ()
                {
-                       if (targetType.IsPointer) {
-                               if (type == TypeManager.null_type || this is NullPointer)
-                                       return new EmptyConstantCast (new NullPointer (loc), targetType);
-
-                               return null;
-                       }
-
-                       // Exlude internal compiler types
-                       if (targetType == InternalType.AnonymousMethod)
-                               return null;
-
-                       if (type != TypeManager.null_type && !Convert.ImplicitStandardConversionExists (this, targetType))
-                               return null;
-
-                       if (TypeManager.IsReferenceType (targetType))
-                               return new NullLiteral (targetType, loc);
-
-                       if (TypeManager.IsNullableType (targetType))
-                               return Nullable.LiftedNull.Create (targetType, loc);
-
-                       return null;
+                       return "null";
                }
 
-               public override Constant ConvertImplicitly (Type targetType)
-               {
-                       //
-                       // Null literal is of object type
-                       //
-                       if (targetType == TypeManager.object_type)
-                               return this;
-
-                       return ConvertExplicitly (false, targetType);
+               public override bool IsLiteral {
+                       get { return true; }
                }
 
-               public override object GetValue ()
+               public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
                {
-                       return null;
+                       return System.Linq.Expressions.Expression.Constant (null);
                }
+       }
 
-               public override Constant Increment ()
+       public class BoolLiteral : BoolConstant, ILiteralConstant
+       {
+               public BoolLiteral (BuiltinTypes types, bool val, Location loc)
+                       : base (types, val, loc)
                {
-                       throw new NotSupportedException ();
-               }
-
-               public override bool IsDefaultValue {
-                       get { return true; }
                }
 
                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 void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       type = storey.MutateType (type);
-               }
-       }
-
-       //
-       // A null literal in a pointer context
-       //
-       class NullPointer : NullLiteral {
-               public NullPointer (Location loc):
-                       base (loc)
-               {
-                       type = TypeManager.object_type;
-               }
+#if FULL_AST
+               public char[] ParsedValue { get; set; }
+#endif
 
-               public override void Emit (EmitContext ec)
+               public override object Accept (StructuralVisitor visitor)
                {
-                       ILGenerator ig = ec.ig;
-                               
-                       //
-                       // Emits null pointer
-                       //
-                       ig.Emit (OpCodes.Ldc_I4_0);
-                       ig.Emit (OpCodes.Conv_U);
+                       return visitor.Visit (this);
                }
        }
 
-       public class BoolLiteral : BoolConstant {
-               public BoolLiteral (bool val, Location loc) : base (val, loc)
-               {
-               }
-
-               public override Expression DoResolve (EmitContext ec)
+       public class CharLiteral : CharConstant, ILiteralConstant
+       {
+               public CharLiteral (BuiltinTypes types, char c, Location loc)
+                       : base (types, c, loc)
                {
-                       type = TypeManager.bool_type;
-                       return this;
                }
 
                public override bool IsLiteral {
                        get { return true; }
                }
-       }
 
-       public class CharLiteral : CharConstant {
-               public CharLiteral (char c, Location loc) : base (c, loc)
-               {
-               }
+#if FULL_AST
+               public char[] ParsedValue { get; set; }
+#endif
 
-               public override Expression DoResolve (EmitContext ec)
+               public override object Accept (StructuralVisitor visitor)
                {
-                       type = TypeManager.char_type;
-                       return this;
-               }
-
-               public override bool IsLiteral {
-                       get { return true; }
+                       return visitor.Visit (this);
                }
        }
 
-       public class IntLiteral : IntConstant {
-               public IntLiteral (int l, Location loc) : base (l, loc)
-               {
-               }
-
-               public override Expression DoResolve (EmitContext ec)
+       public class IntLiteral : IntConstant, ILiteralConstant
+       {
+               public IntLiteral (BuiltinTypes types, int l, Location loc)
+                       : base (types, l, loc)
                {
-                       type = TypeManager.int32_type;
-                       return this;
                }
 
-               public override Constant ConvertImplicitly (Type type)
+               public override Constant ConvertImplicitly (TypeSpec type)
                {
-                       ///
-                       /// The 0 literal can be converted to an enum value,
-                       ///
-                       if (Value == 0 && TypeManager.IsEnumType (type)) {
-                               Constant c = ConvertImplicitly (TypeManager.GetEnumUnderlyingType (type));
+                       //
+                       // The 0 literal can be converted to an enum value
+                       //
+                       if (Value == 0 && type.IsEnum) {
+                               Constant c = ConvertImplicitly (EnumSpec.GetUnderlyingType (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 {
-               public UIntLiteral (uint l, Location loc) : base (l, loc)
+#if FULL_AST
+               public char[] ParsedValue { get; set; }
+#endif
+
+               public override object Accept (StructuralVisitor visitor)
                {
+                       return visitor.Visit (this);
                }
+       }
 
-               public override Expression DoResolve (EmitContext ec)
+       public class UIntLiteral : UIntConstant, ILiteralConstant
+       {
+               public UIntLiteral (BuiltinTypes types, uint l, Location loc)
+                       : base (types, l, loc)
                {
-                       type = TypeManager.uint32_type;
-                       return this;
                }
 
                public override bool IsLiteral {
                        get { return true; }
                }
-       }
-       
-       public class LongLiteral : LongConstant {
-               public LongLiteral (long l, Location loc) : base (l, loc)
+
+#if FULL_AST
+               public char[] ParsedValue { get; set; }
+#endif
+
+               public override object Accept (StructuralVisitor visitor)
                {
+                       return visitor.Visit (this);
                }
+       }
 
-               public override Expression DoResolve (EmitContext ec)
+       public class LongLiteral : LongConstant, ILiteralConstant
+       {
+               public LongLiteral (BuiltinTypes types, long l, Location loc)
+                       : base (types, l, loc)
                {
-                       type = TypeManager.int64_type;
-                       return this;
                }
 
                public override bool IsLiteral {
                        get { return true; }
                }
-       }
 
-       public class ULongLiteral : ULongConstant {
-               public ULongLiteral (ulong l, Location loc) : base (l, loc)
+#if FULL_AST
+               public char[] ParsedValue { get; set; }
+#endif
+
+               public override object Accept (StructuralVisitor visitor)
                {
+                       return visitor.Visit (this);
                }
+       }
 
-               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.uint64_type;
-                       return this;
                }
 
                public override bool IsLiteral {
                        get { return true; }
                }
-       }
-       
-       public class FloatLiteral : FloatConstant {
-               
-               public FloatLiteral (float f, Location loc) : base (f, loc)
+
+#if FULL_AST
+               public char[] ParsedValue { get; set; }
+#endif
+
+               public override object Accept (StructuralVisitor visitor)
                {
+                       return visitor.Visit (this);
                }
+       }
 
-               public override Expression DoResolve (EmitContext ec)
+       public class FloatLiteral : FloatConstant, ILiteralConstant
+       {
+               public FloatLiteral (BuiltinTypes types, float f, Location loc)
+                       : base (types, f, loc)
                {
-                       type = TypeManager.float_type;
-                       return this;
                }
 
                public override bool IsLiteral {
                        get { return true; }
                }
 
-       }
+#if FULL_AST
+               public char[] ParsedValue { get; set; }
+#endif
 
-       public class DoubleLiteral : DoubleConstant {
-               public DoubleLiteral (double d, Location loc) : base (d, loc)
+               public override object Accept (StructuralVisitor visitor)
                {
+                       return visitor.Visit (this);
                }
+       }
 
-               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.double_type;
-
-                       return this;
                }
 
-               public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
+               public override void Error_ValueCannotBeConverted (ResolveContext ec, TypeSpec target, bool expl)
                {
-                       if (target == TypeManager.float_type) {
-                               Error_664 (loc, "float", "f");
+                       if (target.BuiltinType == BuiltinTypeSpec.Type.Float) {
+                               Error_664 (ec, loc, "float", "f");
                                return;
                        }
 
-                       if (target == TypeManager.decimal_type) {
-                               Error_664 (loc, "decimal", "m");
+                       if (target.BuiltinType == BuiltinTypeSpec.Type.Decimal) {
+                               Error_664 (ec, loc, "decimal", "m");
                                return;
                        }
 
-                       base.Error_ValueCannotBeConverted (ec, loc, target, expl);
+                       base.Error_ValueCannotBeConverted (ec, 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);
                }
@@ -370,39 +279,55 @@ namespace Mono.CSharp {
                        get { return true; }
                }
 
-       }
+#if FULL_AST
+               public char[] ParsedValue { get; set; }
+#endif
 
-       public class DecimalLiteral : DecimalConstant {
-               public DecimalLiteral (decimal d, Location loc) : base (d, loc)
+               public override object Accept (StructuralVisitor visitor)
                {
+                       return visitor.Visit (this);
                }
+       }
 
-               public override Expression DoResolve (EmitContext ec)
+       public class DecimalLiteral : DecimalConstant, ILiteralConstant
+       {
+               public DecimalLiteral (BuiltinTypes types, decimal d, Location loc)
+                       : base (types, d, loc)
                {
-                       type = TypeManager.decimal_type;
-                       return this;
                }
 
                public override bool IsLiteral {
                        get { return true; }
                }
-       }
 
-       public class StringLiteral : StringConstant {
-               public StringLiteral (string s, Location loc) : base (s, loc)
+#if FULL_AST
+               public char[] ParsedValue { get; set; }
+#endif
+
+               public override object Accept (StructuralVisitor visitor)
                {
+                       return visitor.Visit (this);
                }
+       }
 
-               public override Expression DoResolve (EmitContext ec)
+       public class StringLiteral : StringConstant, ILiteralConstant
+       {
+               public StringLiteral (BuiltinTypes types, string s, Location loc)
+                       : base (types, s, loc)
                {
-                       type = TypeManager.string_type;
-
-                       return this;
                }
 
                public override bool IsLiteral {
                        get { return true; }
                }
 
+#if FULL_AST
+               public char[] ParsedValue { get; set; }
+#endif
+
+               public override object Accept (StructuralVisitor visitor)
+               {
+                       return visitor.Visit (this);
+               }
        }
 }