2005-01-19 Sureshkumar T <tsureshkumar@novell.com>
[mono.git] / mcs / gmcs / expression.cs
old mode 100755 (executable)
new mode 100644 (file)
index 828f0fe..a056929
@@ -217,6 +217,10 @@ namespace Mono.CSharp {
                                e = new IntConstant (-((ShortConstant) expr).Value);
                        else if (expr is UShortConstant)
                                e = new IntConstant (-((UShortConstant) expr).Value);
+                       else if (expr is SByteConstant)
+                               e = new IntConstant (-((SByteConstant) expr).Value);
+                       else if (expr is ByteConstant)
+                               e = new IntConstant (-((ByteConstant) expr).Value);
                        return e;
                }
 
@@ -237,7 +241,7 @@ namespace Mono.CSharp {
                                
                        case Operator.UnaryNegation:
                                result = TryReduceNegative (e);
-                               return true;
+                               return result != null;
                                
                        case Operator.LogicalNot:
                                if (expr_type != TypeManager.bool_type) {
@@ -1162,6 +1166,9 @@ namespace Mono.CSharp {
 
                                warning_always_matches = true;
                        } else if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
+                               if (etype.IsGenericParameter)
+                                       expr = new BoxedCast (expr, etype);
+
                                //
                                // Second case: explicit reference convresion
                                //
@@ -1174,15 +1181,15 @@ namespace Mono.CSharp {
                                warning_never_matches = true;
                        }
                        
-                               if (warning_always_matches)
+                       if (warning_always_matches)
                                Warning (183, "The given expression is always of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
-                               else if (warning_never_matches){
-                                       if (!(probe_type.IsInterface || expr.Type.IsInterface))
+                       else if (warning_never_matches){
+                               if (!(probe_type.IsInterface || expr.Type.IsInterface))
                                        Warning (184, "The given expression is never of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
                        }
 
                        return this;
-               }                               
+               }
        }
 
        /// <summary>
@@ -1240,6 +1247,9 @@ namespace Mono.CSharp {
                        }
 
                        if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
+                               if (etype.IsGenericParameter)
+                                       expr = new BoxedCast (expr, etype);
+
                                do_isinst = true;
                                return this;
                        }
@@ -2705,16 +2715,27 @@ namespace Mono.CSharp {
                                }
                        } else
                                left = left.Resolve (ec);
-                       right = right.Resolve (ec);
 
-                       if (left == null || right == null)
+                       if (left == null)
+                               return null;
+
+                       Constant lc = left as Constant;
+                       if (lc != null && lc.Type == TypeManager.bool_type && 
+                               ((oper == Operator.LogicalAnd && (bool)lc.GetValue () == false) ||
+                                (oper == Operator.LogicalOr && (bool)lc.GetValue () == true))) {
+
+                               // TODO: make a sence to resolve unreachable expression as we do for statement
+                               Report.Warning (429, 4, loc, "Unreachable expression code detected");
+                               return left;
+                       }
+
+                       right = right.Resolve (ec);
+                       if (right == null)
                                return null;
 
                        eclass = ExprClass.Value;
 
                        Constant rc = right as Constant;
-                       Constant lc = left as Constant;
-
                        if (rc != null & lc != null){
                                Expression e = ConstantFold.BinaryFold (
                                        ec, oper, lc, rc, loc);
@@ -4594,7 +4615,7 @@ namespace Mono.CSharp {
                                                      bool do_varargs, ref MethodBase candidate)
                {
                        if (!me.HasTypeArguments &&
-                           !InferParamsTypeArguments (ec, arguments, ref candidate))
+                           !TypeManager.InferParamsTypeArguments (ec, arguments, ref candidate))
                                return false;
 
                        return IsParamsMethodApplicable (
@@ -4694,7 +4715,7 @@ namespace Mono.CSharp {
                                          ref MethodBase candidate)
                {
                        if (!me.HasTypeArguments &&
-                           !InferTypeArguments (ec, arguments, ref candidate))
+                           !TypeManager.InferTypeArguments (ec, arguments, ref candidate))
                                return false;
 
                        return IsApplicable (ec, arguments, arg_count, candidate);
@@ -4862,7 +4883,7 @@ namespace Mono.CSharp {
                                        if (pd.Count != arg_count)
                                                continue;
 
-                                       if (!InferTypeArguments (ec, Arguments, ref c))
+                                       if (!TypeManager.InferTypeArguments (ec, Arguments, ref c))
                                                continue;
 
                                        VerifyArgumentsCompat (ec, Arguments, arg_count,
@@ -4882,7 +4903,7 @@ namespace Mono.CSharp {
                                                if (pd.Count != arg_count)
                                                        continue;
 
-                                               if (InferTypeArguments (ec, Arguments, ref c))
+                                               if (TypeManager.InferTypeArguments (ec, Arguments, ref c))
                                                        continue;
 
                                                Report.Error (
@@ -5134,281 +5155,6 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               static bool InferType (Type pt, Type at, ref Type[] infered)
-               {
-                       if (pt.IsGenericParameter && (pt.DeclaringMethod != null)) {
-                               int pos = pt.GenericParameterPosition;
-
-                               if (infered [pos] == null) {
-                                       Type check = at;
-                                       while (check.IsArray)
-                                               check = check.GetElementType ();
-
-                                       if (pt == check)
-                                               return false;
-
-                                       infered [pos] = at;
-                                       return true;
-                               }
-
-                               if (infered [pos] != at)
-                                       return false;
-
-                               return true;
-                       }
-
-                       if (!pt.ContainsGenericParameters)
-                               return true;
-
-                       if (at.IsArray) {
-                               if (!pt.IsArray ||
-                                   (at.GetArrayRank () != pt.GetArrayRank ()))
-                                       return false;
-
-                               return InferType (pt.GetElementType (), at.GetElementType (),
-                                                 ref infered);
-                       }
-
-                       if (pt.IsArray) {
-                               if (!at.IsArray ||
-                                   (pt.GetArrayRank () != at.GetArrayRank ()))
-                                       return false;
-
-                               return InferType (pt.GetElementType (), at.GetElementType (),
-                                                 ref infered);
-                       }
-
-                       if (pt.IsByRef && at.IsByRef)
-                               return InferType (pt.GetElementType (), at.GetElementType (),
-                                                 ref infered);
-
-                       ArrayList list = new ArrayList ();
-                       if (at.IsGenericInstance)
-                               list.Add (at);
-                       else {
-                               for (Type bt = at.BaseType; bt != null; bt = bt.BaseType)
-                                       list.Add (bt);
-
-                               list.AddRange (TypeManager.GetInterfaces (at));
-                       }
-
-                       bool found_one = false;
-
-                       foreach (Type type in list) {
-                               if (!type.IsGenericInstance)
-                                       continue;
-
-                               Type[] infered_types = new Type [infered.Length];
-
-                               if (!InferGenericInstance (pt, type, infered_types))
-                                       continue;
-
-                               for (int i = 0; i < infered_types.Length; i++) {
-                                       if (infered [i] == null) {
-                                               infered [i] = infered_types [i];
-                                               continue;
-                                       }
-
-                                       if (infered [i] != infered_types [i])
-                                               return false;
-                               }
-
-                               found_one = true;
-                       }
-
-                       return found_one;
-               }
-
-               static bool InferGenericInstance (Type pt, Type at, Type[] infered_types)
-               {
-                       Type[] at_args = at.GetGenericArguments ();
-                       Type[] pt_args = pt.GetGenericArguments ();
-
-                       if (at_args.Length != pt_args.Length)
-                               return false;
-
-                       for (int i = 0; i < at_args.Length; i++) {
-                               if (!InferType (pt_args [i], at_args [i], ref infered_types))
-                                       return false;
-                       }
-
-                       for (int i = 0; i < infered_types.Length; i++) {
-                               if (infered_types [i] == null)
-                                       return false;
-                       }
-
-                       return true;
-               }
-
-               static bool InferParamsTypeArguments (EmitContext ec, ArrayList arguments,
-                                                     ref MethodBase method)
-               {
-                       if ((arguments == null) || !TypeManager.IsGenericMethod (method))
-                               return true;
-
-                       int arg_count;
-                       
-                       if (arguments == null)
-                               arg_count = 0;
-                       else
-                               arg_count = arguments.Count;
-                       
-                       ParameterData pd = GetParameterData (method);
-
-                       int pd_count = pd.Count;
-
-                       if (pd_count == 0)
-                               return false;
-                       
-                       if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
-                               return false;
-                       
-                       if (pd_count - 1 > arg_count)
-                               return false;
-                       
-                       if (pd_count == 1 && arg_count == 0)
-                               return true;
-
-                       Type[] method_args = method.GetGenericArguments ();
-                       Type[] infered_types = new Type [method_args.Length];
-
-                       //
-                       // If we have come this far, the case which
-                       // remains is when the number of parameters is
-                       // less than or equal to the argument count.
-                       //
-                       for (int i = 0; i < pd_count - 1; ++i) {
-                               Argument a = (Argument) arguments [i];
-
-                               if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
-                                       continue;
-
-                               Type pt = pd.ParameterType (i);
-                               Type at = a.Type;
-
-                               if (!InferType (pt, at, ref infered_types))
-                                       return false;
-                       }
-
-                       Type element_type = TypeManager.GetElementType (pd.ParameterType (pd_count - 1));
-
-                       for (int i = pd_count - 1; i < arg_count; i++) {
-                               Argument a = (Argument) arguments [i];
-
-                               if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
-                                       continue;
-
-                               if (!InferType (element_type, a.Type, ref infered_types))
-                                       return false;
-                       }
-
-                       for (int i = 0; i < infered_types.Length; i++)
-                               if (infered_types [i] == null)
-                                       return false;
-
-                       method = method.BindGenericParameters (infered_types);
-                       return true;
-               }
-
-               public static bool InferTypeArguments (Type[] param_types, Type[] arg_types,
-                                                      ref Type[] infered_types)
-               {
-                       if (infered_types == null)
-                               return false;
-
-                       for (int i = 0; i < arg_types.Length; i++) {
-                               if (arg_types [i] == null)
-                                       continue;
-
-                               if (!InferType (param_types [i], arg_types [i], ref infered_types))
-                                       return false;
-                       }
-
-                       for (int i = 0; i < infered_types.Length; i++)
-                               if (infered_types [i] == null)
-                                       return false;
-
-                       return true;
-               }
-
-               static bool InferTypeArguments (EmitContext ec, ArrayList arguments,
-                                               ref MethodBase method)
-               {
-                       if (!TypeManager.IsGenericMethod (method))
-                               return true;
-
-                       int arg_count;
-                       if (arguments != null)
-                               arg_count = arguments.Count;
-                       else
-                               arg_count = 0;
-
-                       ParameterData pd = GetParameterData (method);
-                       if (arg_count != pd.Count)
-                               return false;
-
-                       Type[] method_args = method.GetGenericArguments ();
-
-                       bool is_open = false;
-                       for (int i = 0; i < method_args.Length; i++) {
-                               if (method_args [i].IsGenericParameter) {
-                                       is_open = true;
-                                       break;
-                               }
-                       }
-                       if (!is_open)
-                               return true;
-
-                       Type[] infered_types = new Type [method_args.Length];
-
-                       Type[] param_types = new Type [pd.Count];
-                       Type[] arg_types = new Type [pd.Count];
-
-                       for (int i = 0; i < arg_count; i++) {
-                               param_types [i] = pd.ParameterType (i);
-
-                               Argument a = (Argument) arguments [i];
-                               if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
-                                       continue;
-
-                               arg_types [i] = a.Type;
-                       }
-
-                       if (!InferTypeArguments (param_types, arg_types, ref infered_types))
-                               return false;
-
-                       method = method.BindGenericParameters (infered_types);
-                       return true;
-               }
-
-               public static bool InferTypeArguments (EmitContext ec, ParameterData apd,
-                                                      ref MethodBase method)
-               {
-                       if (!TypeManager.IsGenericMethod (method))
-                               return true;
-
-                       ParameterData pd = GetParameterData (method);
-                       if (apd.Count != pd.Count)
-                               return false;
-
-                       Type[] method_args = method.GetGenericArguments ();
-                       Type[] infered_types = new Type [method_args.Length];
-
-                       Type[] param_types = new Type [pd.Count];
-                       Type[] arg_types = new Type [pd.Count];
-
-                       for (int i = 0; i < apd.Count; i++) {
-                               param_types [i] = pd.ParameterType (i);
-                               arg_types [i] = apd.ParameterType (i);
-                       }
-
-                       if (!InferTypeArguments (param_types, arg_types, ref infered_types))
-                               return false;
-
-                       method = method.BindGenericParameters (infered_types);
-                       return true;
-               }
-
                public override Expression DoResolve (EmitContext ec)
                {
                        //