Merge pull request #5675 from mono/glib-debug-symbols
[mono.git] / mcs / mcs / constant.cs
index 799d81bfac6adb3706ede095c5177c615d7714e4..c16a0aa1c970df6a48d579c56ce5f32731e29ba6 100644 (file)
@@ -60,7 +60,7 @@ namespace Mono.CSharp {
 
                public override void Error_ValueCannotBeConverted (ResolveContext ec, TypeSpec target, bool expl)
                {
-                       if (!expl && IsLiteral && 
+                       if (!expl && IsLiteral && type.BuiltinType != BuiltinTypeSpec.Type.Double &&
                                BuiltinTypeSpec.IsPrimitiveTypeOrDecimal (target) &&
                                BuiltinTypeSpec.IsPrimitiveTypeOrDecimal (type)) {
                                ec.Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
@@ -478,7 +478,7 @@ namespace Mono.CSharp {
                        catch
                        {
                                ec.Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
-                                       GetValue ().ToString (), target.GetSignatureForError ());
+                                       GetValueAsLiteral (), target.GetSignatureForError ());
                        }
                }
 
@@ -522,7 +522,7 @@ namespace Mono.CSharp {
                        return Value ? 1 : 0;
                }
 
-               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
                {
                        enc.Encode (Value);
                }
@@ -573,7 +573,7 @@ namespace Mono.CSharp {
                        Value = v;
                }
 
-               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
                {
                        enc.Encode (Value);
                }
@@ -673,7 +673,7 @@ namespace Mono.CSharp {
                        Value = v;
                }
 
-               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
                {
                        enc.Encode ((ushort) Value);
                }
@@ -801,7 +801,7 @@ namespace Mono.CSharp {
                        Value = v;
                }
 
-               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
                {
                        enc.Encode (Value);
                }
@@ -904,7 +904,7 @@ namespace Mono.CSharp {
                        Value = v;
                }
 
-               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
                {
                        enc.Encode (Value);
                }
@@ -1017,7 +1017,7 @@ namespace Mono.CSharp {
                        Value = v;
                }
 
-               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
                {
                        enc.Encode (Value);
                }
@@ -1126,7 +1126,7 @@ namespace Mono.CSharp {
                        Value = v;
                }
 
-               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
                {
                        enc.Encode (Value);
                }
@@ -1302,7 +1302,7 @@ namespace Mono.CSharp {
                        Value = v;
                }
 
-               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
                {
                        enc.Encode (Value);
                }
@@ -1419,7 +1419,7 @@ namespace Mono.CSharp {
                        Value = v;
                }
 
-               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
                {
                        enc.Encode (Value);
                }
@@ -1550,7 +1550,7 @@ namespace Mono.CSharp {
                        Value = v;
                }
 
-               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
                {
                        enc.Encode (Value);
                }
@@ -1646,23 +1646,35 @@ namespace Mono.CSharp {
        }
 
        public class FloatConstant : Constant {
-               public readonly float Value;
+               //
+               // Store constant value as double because float constant operations
+               // need to work on double value to match JIT
+               //
+               public readonly double DoubleValue;
 
-               public FloatConstant (BuiltinTypes types, float v, Location loc)
+               public FloatConstant (BuiltinTypes types, double v, Location loc)
                        : this (types.Float, v, loc)
                {
                }
 
-               public FloatConstant (TypeSpec type, float v, Location loc)
+               public FloatConstant (TypeSpec type, double v, Location loc)
                        : base (loc)
                {
                        this.type = type;
                        eclass = ExprClass.Value;
 
-                       Value = v;
+                       DoubleValue = v;
                }
 
-               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               public override Constant ConvertImplicitly (TypeSpec type)
+               {
+                       if (type.BuiltinType == BuiltinTypeSpec.Type.Double)
+                               return new DoubleConstant (type, DoubleValue, loc);
+
+                       return base.ConvertImplicitly (type);
+               }
+
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
                {
                        enc.Encode (Value);
                }
@@ -1672,6 +1684,12 @@ namespace Mono.CSharp {
                        ec.Emit (OpCodes.Ldc_R4, Value);
                }
 
+               public float Value {
+                       get {
+                               return (float) DoubleValue;
+                       }
+               }
+
                public override object GetValue ()
                {
                        return Value;
@@ -1679,7 +1697,7 @@ namespace Mono.CSharp {
 
                public override string GetValueAsLiteral ()
                {
-                       return Value.ToString ();
+                       return Value.ToString (CultureInfo.InvariantCulture);
                }
 
                public override long GetValueAsLong ()
@@ -1707,59 +1725,59 @@ namespace Mono.CSharp {
                                        if (Value < byte.MinValue || Value > byte.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
-                               return new ByteConstant (target_type, (byte) Value, Location);
+                               return new ByteConstant (target_type, (byte) DoubleValue, Location);
                        case BuiltinTypeSpec.Type.SByte:
                                if (in_checked_context) {
                                        if (Value < sbyte.MinValue || Value > sbyte.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
-                               return new SByteConstant (target_type, (sbyte) Value, Location);
+                               return new SByteConstant (target_type, (sbyte) DoubleValue, Location);
                        case BuiltinTypeSpec.Type.Short:
                                if (in_checked_context) {
                                        if (Value < short.MinValue || Value > short.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
-                               return new ShortConstant (target_type, (short) Value, Location);
+                               return new ShortConstant (target_type, (short) DoubleValue, Location);
                        case BuiltinTypeSpec.Type.UShort:
                                if (in_checked_context) {
                                        if (Value < ushort.MinValue || Value > ushort.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
-                               return new UShortConstant (target_type, (ushort) Value, Location);
+                               return new UShortConstant (target_type, (ushort) DoubleValue, Location);
                        case BuiltinTypeSpec.Type.Int:
                                if (in_checked_context) {
                                        if (Value < int.MinValue || Value > int.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
-                               return new IntConstant (target_type, (int) Value, Location);
+                               return new IntConstant (target_type, (int) DoubleValue, Location);
                        case BuiltinTypeSpec.Type.UInt:
                                if (in_checked_context) {
                                        if (Value < uint.MinValue || Value > uint.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
-                               return new UIntConstant (target_type, (uint) Value, Location);
+                               return new UIntConstant (target_type, (uint) DoubleValue, Location);
                        case BuiltinTypeSpec.Type.Long:
                                if (in_checked_context) {
                                        if (Value < long.MinValue || Value > long.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
-                               return new LongConstant (target_type, (long) Value, Location);
+                               return new LongConstant (target_type, (long) DoubleValue, Location);
                        case BuiltinTypeSpec.Type.ULong:
                                if (in_checked_context) {
                                        if (Value < ulong.MinValue || Value > ulong.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
-                               return new ULongConstant (target_type, (ulong) Value, Location);
+                               return new ULongConstant (target_type, (ulong) DoubleValue, Location);
                        case BuiltinTypeSpec.Type.Double:
-                               return new DoubleConstant (target_type, (double) Value, Location);
+                               return new DoubleConstant (target_type, DoubleValue, Location);
                        case BuiltinTypeSpec.Type.Char:
                                if (in_checked_context) {
                                        if (Value < (float) char.MinValue || Value > (float) char.MaxValue || float.IsNaN (Value))
                                                throw new OverflowException ();
                                }
-                               return new CharConstant (target_type, (char) Value, Location);
+                               return new CharConstant (target_type, (char) DoubleValue, Location);
                        case BuiltinTypeSpec.Type.Decimal:
-                               return new DecimalConstant (target_type, (decimal) Value, Location);
+                               return new DecimalConstant (target_type, (decimal) DoubleValue, Location);
                        }
 
                        return null;
@@ -1785,7 +1803,7 @@ namespace Mono.CSharp {
                        Value = v;
                }
 
-               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
                {
                        enc.Encode (Value);
                }
@@ -1802,7 +1820,7 @@ namespace Mono.CSharp {
 
                public override string GetValueAsLiteral ()
                {
-                       return Value.ToString ();
+                       return Value.ToString (CultureInfo.InvariantCulture);
                }
 
                public override long GetValueAsLong ()
@@ -2003,7 +2021,7 @@ namespace Mono.CSharp {
 
                public override string GetValueAsLiteral ()
                {
-                       return Value.ToString () + "M";
+                       return Value.ToString (CultureInfo.InvariantCulture) + "M";
                }
 
                public override long GetValueAsLong ()
@@ -2013,8 +2031,6 @@ namespace Mono.CSharp {
        }
 
        public class StringConstant : Constant {
-               public readonly string Value;
-
                public StringConstant (BuiltinTypes types, string s, Location loc)
                        : this (types.String, s, loc)
                {
@@ -2029,6 +2045,13 @@ namespace Mono.CSharp {
                        Value = s;
                }
 
+               protected StringConstant (Location loc)
+                       : base (loc)
+               {
+               }
+
+               public string Value { get; protected set; }
+
                public override object GetValue ()
                {
                        return Value;
@@ -2067,10 +2090,15 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       ec.Emit (OpCodes.Ldstr, Value);
+                       var str = Value;
+                       if (ec.Module.GetResourceStrings != null && !ec.Module.GetResourceStrings.TryGetValue (str, out str)) {
+                               str = Value;
+                       }
+
+                       ec.Emit (OpCodes.Ldstr, str);
                }
 
-               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
                {
                        // cast to object
                        if (type != targetType)
@@ -2111,6 +2139,121 @@ namespace Mono.CSharp {
                }
        }
 
+       class NameOf : StringConstant
+       {
+               readonly SimpleName name;
+
+               public NameOf (SimpleName name)
+                       : base (name.Location)
+               {
+                       this.name = name;
+               }
+
+               protected override Expression DoResolve (ResolveContext rc)
+               {
+                       throw new NotSupportedException ();
+               }
+
+               bool ResolveArgumentExpression (ResolveContext rc, Expression expr)
+               {
+                       var sn = expr as SimpleName;
+                       if (sn != null) {
+                               Value = sn.Name;
+
+                               if (rc.Module.Compiler.Settings.Version < LanguageVersion.V_6)
+                                       rc.Report.FeatureIsNotAvailable (rc.Module.Compiler, Location, "nameof operator");
+
+                               var res = sn.LookupNameExpression (rc, MemberLookupRestrictions.IgnoreAmbiguity | MemberLookupRestrictions.NameOfExcluded);
+                               var me = res as MemberExpr;
+                               if (me != null)
+                                       me.ResolveNameOf (rc, sn);
+
+                               return true;
+                       }
+
+                       var ma = expr as MemberAccess;
+                       if (ma != null) {
+                               var lexpr = ma.LeftExpression;
+                               Expression res;
+
+                               using (rc.Set (ResolveContext.Options.NameOfScope)) {
+                                       res = ma.LookupNameExpression (rc, MemberLookupRestrictions.IgnoreAmbiguity);
+                               }
+
+                               if (res == null) {
+                                       return false;
+                               }
+
+                               if (rc.Module.Compiler.Settings.Version < LanguageVersion.V_6)
+                                       rc.Report.FeatureIsNotAvailable (rc.Module.Compiler, Location, "nameof operator");
+
+                               if (ma is QualifiedAliasMember) {
+                                       rc.Report.Error (8083, loc, "An alias-qualified name is not an expression");
+                                       return false;
+                               }
+
+                               var me = res as MemberExpr;
+                               if (me != null) {
+                                       me.ResolveNameOf (rc, ma);
+                               }
+
+                               //
+                               // LAMESPEC: Why is conditional access not allowed?
+                               //
+                               if (!IsLeftResolvedExpressionValid (ma.LeftExpression) || ma.HasConditionalAccess ()) {
+                                       rc.Report.Error (8082, lexpr.Location, "An argument to nameof operator cannot include sub-expression");
+                                       return false;
+                               }
+
+                               Value = ma.Name;
+                               return true;
+                       }
+
+                       rc.Report.Error (8081, loc, "Expression does not have a name");
+                       return false;
+               }
+
+               static bool IsLeftResolvedExpressionValid (Expression expr)
+               {
+                       var fe = expr as FieldExpr;
+                       if (fe != null) {
+                               return fe.InstanceExpression == null || IsLeftResolvedExpressionValid (fe.InstanceExpression);
+                       }
+
+                       var pe = expr as PropertyExpr;
+                       if (pe != null)
+                               return pe.InstanceExpression == null || IsLeftResolvedExpressionValid (pe.InstanceExpression);
+
+                       var dmb = expr as DynamicMemberBinder;
+                       if (dmb != null) {
+                               return IsLeftResolvedExpressionValid (dmb.Arguments [0].Expr);
+                       }
+
+                       if (expr is ConstantExpr || expr is TypeExpr || expr is NamespaceExpression || expr is VariableReference)
+                               return true;
+
+                       return false;
+               }
+
+               public Expression ResolveOverload (ResolveContext rc, Arguments args)
+               {
+                       if (args == null || args.Count != 1) {
+                               name.Error_NameDoesNotExist (rc);
+                               return null;
+                       }
+
+                       var arg = args [0];
+                       var res = ResolveArgumentExpression (rc, arg.Expr);
+                       if (!res) {
+                               return null;
+                       }
+
+                       type = rc.BuiltinTypes.String;
+                       eclass = ExprClass.Value;
+                       return this;
+               }
+       }
+
        //
        // Null constant can have its own type, think of `default (Foo)'
        //
@@ -2135,7 +2278,7 @@ namespace Mono.CSharp {
                        return base.CreateExpressionTree (ec);
                }
 
-               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+               public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
                {
                        switch (targetType.BuiltinType) {
                        case BuiltinTypeSpec.Type.Object:
@@ -2156,7 +2299,7 @@ namespace Mono.CSharp {
                                break;
                        }
 
-                       base.EncodeAttributeValue (rc, enc, targetType);
+                       base.EncodeAttributeValue (rc, enc, targetType, parameterType);
                }
 
                public override void Emit (EmitContext ec)
@@ -2296,6 +2439,11 @@ namespace Mono.CSharp {
                        }
                }
 
+               public override bool ContainsEmitWithAwait ()
+               {
+                       return side_effect.ContainsEmitWithAwait ();
+               }
+
                public override object GetValue ()
                {
                        return value.GetValue ();
@@ -2323,6 +2471,11 @@ namespace Mono.CSharp {
                        value.EmitSideEffect (ec);
                }
 
+               public override void FlowAnalysis (FlowAnalysisContext fc)
+               {
+                       side_effect.FlowAnalysis (fc);
+               }
+
                public override bool IsDefaultValue {
                        get { return value.IsDefaultValue; }
                }