Fix Firebird 'make dist' and build
[mono.git] / mcs / gmcs / expression.cs
index 7acc7fa4d2309188311ad622710ffad7db415243..1d35cb53fec2c5cc52974b9b5b05976b2347588c 100644 (file)
@@ -1231,7 +1231,7 @@ namespace Mono.CSharp {
                        eclass = ExprClass.Value;
                        Type etype = expr.Type;
 
-                       if (TypeManager.IsValueType (probe_type)){
+                       if (probe_type.IsValueType) {
                                Report.Error (77, loc, "The as operator must be used with a reference type (`" +
                                              TypeManager.CSharpName (probe_type) + "' is a value type)");
                                return null;
@@ -1349,7 +1349,7 @@ namespace Mono.CSharp {
                        if (expr.Type == target_type)
                                return expr;
 
-                       if (TypeManager.IsEnumType (target_type))
+                       if (TypeManager.IsEnumType (target_type) && TypeManager.EnumToUnderlying (target_type) == expr.Type)
                                return new EnumConstant ((Constant)expr, target_type);
 
                        Expression real_expr = expr;
@@ -2031,7 +2031,7 @@ namespace Mono.CSharp {
                // type, otherwise ConvertImplict() already finds the user-defined conversion for us,
                // so we don't explicitly check for performance reasons.
                //
-               bool DoNumericPromotions (EmitContext ec, Type l, Type r, bool check_user_conv)
+               bool DoNumericPromotions (EmitContext ec, Type l, Type r, Expression lexpr, Expression rexpr, bool check_user_conv)
                {
                        if (IsOfType (ec, l, r, TypeManager.double_type, check_user_conv)){
                                //
@@ -2183,6 +2183,12 @@ namespace Mono.CSharp {
                                left = ForceConversion (ec, left, TypeManager.int32_type);
                                right = ForceConversion (ec, right, TypeManager.int32_type);
 
+                               bool strConv =
+                                       Convert.ImplicitConversionExists (ec, lexpr, TypeManager.string_type) &&
+                                       Convert.ImplicitConversionExists (ec, rexpr, TypeManager.string_type);
+                               if (strConv && left != null && right != null)
+                                       Error_OperatorAmbiguous (loc, oper, l, r);
+
                                type = TypeManager.int32_type;
                        }
 
@@ -2298,7 +2304,7 @@ namespace Mono.CSharp {
                                                return null;
                                        }
 
-                                       left = new BoxedCast (left);
+                                       left = new BoxedCast (left, TypeManager.object_type);
                                        Type = TypeManager.bool_type;
                                        return this;
                                }
@@ -2309,7 +2315,7 @@ namespace Mono.CSharp {
                                                return null;
                                        }
 
-                                       right = new BoxedCast (right);
+                                       right = new BoxedCast (right, TypeManager.object_type);
                                        Type = TypeManager.bool_type;
                                        return this;
                                }
@@ -2753,7 +2759,7 @@ namespace Mono.CSharp {
                        // This will leave left or right set to null if there is an error
                        //
                        bool check_user_conv = is_user_defined (l) && is_user_defined (r);
-                       DoNumericPromotions (ec, l, r, check_user_conv);
+                       DoNumericPromotions (ec, l, r, left, right, check_user_conv);
                        if (left == null || right == null){
                                Error_OperatorCannotBeApplied (loc, OperName (oper), l, r);
                                return null;
@@ -2859,9 +2865,109 @@ namespace Mono.CSharp {
                        if (TypeManager.IsNullableType (left.Type) || TypeManager.IsNullableType (right.Type))
                                return new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
 
+                       // Check CS0652 warning here (before resolving operator).
+                       if (oper == Operator.Equality ||
+                           oper == Operator.Inequality ||
+                           oper == Operator.LessThanOrEqual ||
+                           oper == Operator.LessThan ||
+                           oper == Operator.GreaterThanOrEqual ||
+                           oper == Operator.GreaterThan){
+                               CheckUselessComparison (left as Constant, right.Type);
+                               CheckUselessComparison (right as Constant, left.Type);
+                       }
+
                        return ResolveOperator (ec);
                }
 
+               private void CheckUselessComparison (Constant c, Type type)
+               {
+                       if (c == null || !IsTypeIntegral (type)
+                               || c is StringConstant
+                               || c is BoolConstant
+                               || c is CharConstant
+                               || c is FloatConstant
+                               || c is DoubleConstant
+                               || c is DecimalConstant
+                               )
+                               return;
+
+                       long value = 0;
+
+                       if (c is ULongConstant) {
+                               ulong uvalue = ((ULongConstant) c).Value;
+                               if (uvalue > long.MaxValue) {
+                                       if (type == TypeManager.byte_type ||
+                                           type == TypeManager.sbyte_type ||
+                                           type == TypeManager.short_type ||
+                                           type == TypeManager.ushort_type ||
+                                           type == TypeManager.int32_type ||
+                                           type == TypeManager.uint32_type ||
+                                           type == TypeManager.int64_type)
+                                               WarnUselessComparison (type);
+                                       return;
+                               }
+                               value = (long) uvalue;
+                       }
+                       else if (c is ByteConstant)
+                               value = ((ByteConstant) c).Value;
+                       else if (c is SByteConstant)
+                               value = ((SByteConstant) c).Value;
+                       else if (c is ShortConstant)
+                               value = ((ShortConstant) c).Value;
+                       else if (c is UShortConstant)
+                               value = ((UShortConstant) c).Value;
+                       else if (c is IntConstant)
+                               value = ((IntConstant) c).Value;
+                       else if (c is UIntConstant)
+                               value = ((UIntConstant) c).Value;
+                       else if (c is LongConstant)
+                               value = ((LongConstant) c).Value;
+
+                       if (value != 0) {
+                               if (IsValueOutOfRange (value, type))
+                                       WarnUselessComparison (type);
+                               return;
+                       }
+               }
+
+               private bool IsValueOutOfRange (long value, Type type)
+               {
+                       if (IsTypeUnsigned (type) && value < 0)
+                               return true;
+                       return type == TypeManager.sbyte_type && (value >= 0x80 || value < -0x80) ||
+                               type == TypeManager.byte_type && value >= 0x100 ||
+                               type == TypeManager.short_type && (value >= 0x8000 || value < -0x8000) ||
+                               type == TypeManager.ushort_type && value >= 0x10000 ||
+                               type == TypeManager.int32_type && (value >= 0x80000000 || value < -0x80000000) ||
+                               type == TypeManager.uint32_type && value >= 0x100000000;
+               }
+
+               private static bool IsTypeIntegral (Type type)
+               {
+                       return type == TypeManager.uint64_type ||
+                               type == TypeManager.int64_type ||
+                               type == TypeManager.uint32_type ||
+                               type == TypeManager.int32_type ||
+                               type == TypeManager.ushort_type ||
+                               type == TypeManager.short_type ||
+                               type == TypeManager.sbyte_type ||
+                               type == TypeManager.byte_type;
+               }
+
+               private static bool IsTypeUnsigned (Type type)
+               {
+                       return type == TypeManager.uint64_type ||
+                               type == TypeManager.uint32_type ||
+                               type == TypeManager.ushort_type ||
+                               type == TypeManager.byte_type;
+               }
+
+               private void WarnUselessComparison (Type type)
+               {
+                       Report.Warning (652, 2, loc, "Comparison to integral constant is useless; the constant is outside the range of type `{0}'",
+                               TypeManager.CSharpName (type));
+               }
+
                /// <remarks>
                ///   EmitBranchable is called from Statement.EmitBoolExpression in the
                ///   context of a conditional bool expression.  This function will return
@@ -3650,6 +3756,11 @@ namespace Mono.CSharp {
                                        return null;
                        }
                        
+                       Assign ass = expr as Assign;
+                       if (ass != null && ass.Source is Constant) {
+                               Report.Warning (665, 3, loc, "Assignment in conditional expression is always constant; did you mean to use == instead of = ?");
+                       }
+
                        trueExpr = trueExpr.Resolve (ec);
                        falseExpr = falseExpr.Resolve (ec);
 
@@ -3763,6 +3874,12 @@ namespace Mono.CSharp {
                        }
                }
 
+               public bool VerifyAssigned (EmitContext ec)
+               {
+                       VariableInfo variable_info = local_info.VariableInfo;
+                       return variable_info == null || variable_info.IsAssigned (ec, loc);
+               }
+
                protected Expression DoResolveBase (EmitContext ec, Expression lvalue_right_side)
                {
                        if (local_info == null) {
@@ -3800,7 +3917,7 @@ namespace Mono.CSharp {
                                return e.Resolve (ec);
                        }
 
-                       if ((variable_info != null) && !variable_info.IsAssigned (ec, loc))
+                       if (!VerifyAssigned (ec))
                                return null;
 
                        if (lvalue_right_side == null)
@@ -4092,7 +4209,7 @@ namespace Mono.CSharp {
                {
                        DoResolveBase (ec);
 
-                       if (is_out && ec.DoFlowAnalysis && !IsAssigned (ec, loc))
+                       if (is_out && ec.DoFlowAnalysis && (!ec.OmitStructFlowAnalysis || !vi.TypeInfo.IsStruct) && !IsAssigned (ec, loc))
                                return null;
 
                        return this;
@@ -4892,7 +5009,7 @@ namespace Mono.CSharp {
                                if (a_mod == p_mod ||
                                    (a_mod == Parameter.Modifier.NONE && p_mod == Parameter.Modifier.PARAMS)) {
                                        if (a_mod == Parameter.Modifier.NONE) {
-                                                if (!Convert.ImplicitConversionExists (ec,
+                                                if (!TypeManager.IsEqual (a.Type, pd.ParameterType (i)) && !Convert.ImplicitConversionExists (ec,
                                                                                        a.Expr,
                                                                                        pd.ParameterType (i)))
                                                        return false;
@@ -5049,7 +5166,13 @@ namespace Mono.CSharp {
 
                                        VerifyArgumentsCompat (ec, Arguments, arg_count,
                                                c, false, null, may_fail, loc);
-                                        break;
+
+                                       if (!may_fail && errors == Report.Errors)
+                                               throw new InternalErrorException (
+                                                       "VerifyArgumentsCompat and IsApplicable do not agree; " +
+                                                       "likely reason: ImplicitConversion and ImplicitConversionExists have gone out of sync");
+
+                                       break;
                                }
 
                                if (!may_fail && errors == Report.Errors) {
@@ -5153,7 +5276,6 @@ namespace Mono.CSharp {
                                        method_params = cand_params;
                                }
                        }
-
                        //
                        // Now check that there are no ambiguities i.e the selected method
                        // should be better than all the others
@@ -5434,12 +5556,8 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       if ((method.Attributes & MethodAttributes.SpecialName) != 0){
-                               if (TypeManager.LookupDeclSpace (method.DeclaringType) != null || TypeManager.IsSpecialMethod (method)) {
-                                       Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
-                                               TypeManager.CSharpSignature (method, true));
-                                       return null;
-                               }
+                       if ((method.Attributes & MethodAttributes.SpecialName) != 0 && IsSpecialMethodInvocation (method)) {
+                               return null;
                        }
                        
                        if (mg.InstanceExpression != null)
@@ -5449,6 +5567,32 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               bool IsSpecialMethodInvocation (MethodBase method)
+               {
+                       IMethodData md = TypeManager.GetMethod (method);
+                       if (md != null) {
+                               if (!(md is AbstractPropertyEventMethod) && !(md is Operator))
+                                       return false;
+                       } else {
+                               if (!TypeManager.IsSpecialMethod (method))
+                                       return false;
+
+                               int args = TypeManager.GetParameterData (method).Count;
+                               if (method.Name.StartsWith ("get_") && args > 0)
+                                       return false;
+                               else if (method.Name.StartsWith ("set_") && args > 2)
+                                       return false;
+
+                               // TODO: check operators and events as well ?
+                       }
+
+                       Report.SymbolRelatedToPreviousError (method);
+                       Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
+                               TypeManager.CSharpSignature (method, true));
+       
+                       return true;
+               }
+
                // <summary>
                //   Emits the list of arguments as an array
                // </summary>
@@ -5694,7 +5838,7 @@ namespace Mono.CSharp {
                                if (!omit_args) {
                                        Type t = null;
                                        if (this_call) {
-                                               ig.Emit (OpCodes.Ldarg_0);
+                                               ec.EmitThis ();
                                                t = decl_type;
                                        } else {
                                                Type iexpr_type = instance_expr.Type;
@@ -6086,11 +6230,13 @@ namespace Mono.CSharp {
                        }
 
                        if (type.IsAbstract && type.IsSealed) {
+                               Report.SymbolRelatedToPreviousError (type);
                                Report.Error (712, loc, "Cannot create an instance of the static class `{0}'", TypeManager.CSharpName (type));
                                return null;
                        }
 
                        if (type.IsInterface || type.IsAbstract){
+                               Report.SymbolRelatedToPreviousError (type);
                                Report.Error (144, loc, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager.CSharpName (type));
                                return null;
                        }
@@ -6860,7 +7006,7 @@ namespace Mono.CSharp {
                                        if (e is StringConstant || e is DecimalConstant || !(e is Constant) ||
                                            num_automatic_initializers <= max_automatic_initializers) {
                                                Type etype = e.Type;
-                                               
+
                                                ig.Emit (OpCodes.Dup);
 
                                                for (int idx = 0; idx < dims; idx++) 
@@ -6870,7 +7016,8 @@ namespace Mono.CSharp {
                                                // If we are dealing with a struct, get the
                                                // address of it, so we can store it.
                                                //
-                                               if ((dims == 1) && etype.IsValueType &&
+                                               if ((dims == 1) && 
+                                                   TypeManager.IsValueType (etype) &&
                                                    (!TypeManager.IsBuiltinOrEnum (etype) ||
                                                     etype == TypeManager.decimal_type)) {
                                                        if (e is New){
@@ -7048,7 +7195,7 @@ namespace Mono.CSharp {
                        if (!ResolveBase (ec))
                                return null;
 
-                       if ((variable_info != null) && !variable_info.IsAssigned (ec)) {
+                       if ((variable_info != null) && !(type.IsValueType && ec.OmitStructFlowAnalysis) && !variable_info.IsAssigned (ec)) {
                                Error (188, "The `this' object cannot be used before all of its fields are assigned to");
                                variable_info.SetAssigned (ec);
                                return this;
@@ -7494,7 +7641,7 @@ namespace Mono.CSharp {
                        SimpleName original = expr as SimpleName;
                        Expression new_expr = expr.Resolve (ec,
                                ResolveFlags.VariableOrValue | ResolveFlags.Type |
-                               ResolveFlags.Intermediate | ResolveFlags.DisableFlowAnalysis);
+                               ResolveFlags.Intermediate | ResolveFlags.DisableStructFlowAnalysis);
 
                        if (new_expr == null)
                                return null;
@@ -7571,15 +7718,22 @@ namespace Mono.CSharp {
                                return mg.ResolveGeneric (ec, args);
                        }
 
+                       if (original != null && !TypeManager.IsValueType (expr_type)) {
+                               me = member_lookup as MemberExpr;
+                               if (me != null && me.IsInstance) {
+                                       LocalVariableReference var = new_expr as LocalVariableReference;
+                                       if (var != null && !var.VerifyAssigned (ec))
+                                               return null;
+                               }
+                       }
+
                        // The following DoResolve/DoResolveLValue will do the definite assignment
                        // check.
 
                        if (right_side != null)
-                               member_lookup = member_lookup.DoResolveLValue (ec, right_side);
+                               return member_lookup.DoResolveLValue (ec, right_side);
                        else
-                               member_lookup = member_lookup.DoResolve (ec);
-
-                       return member_lookup;
+                               return member_lookup.DoResolve (ec);
                }
 
                public override Expression DoResolve (EmitContext ec)
@@ -8037,6 +8191,8 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Ldobj, type);
                        } else if (type.IsGenericParameter)
                                ig.Emit (OpCodes.Ldelem_Any, type);
+                       else if (type.IsPointer)
+                               ig.Emit (OpCodes.Ldelem_I);
                        else
                                ig.Emit (OpCodes.Ldelem_Ref);
                }
@@ -8077,7 +8233,9 @@ namespace Mono.CSharp {
                        } else if (t.IsGenericParameter) {
                                has_type_arg = true;
                                return OpCodes.Stelem_Any;
-                       } else
+                       } else if (t.IsPointer)
+                               return OpCodes.Stelem_I;
+                       else
                                return OpCodes.Stelem_Ref;
                }