2009-03-17 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / cfold.cs
index 3db17fd8a2c567070e130399ba98535f627c28bc..a23a20a122cf186f500363663fb6e52710d62706 100644 (file)
@@ -5,41 +5,60 @@
 //   Miguel de Icaza (miguel@ximian.com)
 //   Marek Safar (marek.safar@seznam.cz)
 //
-// (C) 2002, 2003 Ximian, Inc.
-//
-
+// Copyright 2002, 2003 Ximian, Inc.
+// Copyright 2003-2008, Novell, Inc.
+// 
 using System;
 
 namespace Mono.CSharp {
 
        public class ConstantFold {
 
-               static Type[] binary_promotions = new Type[] { 
+               public static readonly Type[] binary_promotions = new Type[] { 
                        TypeManager.decimal_type, TypeManager.double_type, TypeManager.float_type,
                        TypeManager.uint64_type, TypeManager.int64_type, TypeManager.uint32_type };
 
                //
                // Performs the numeric promotions on the left and right expresions
-               // and desposits the results on `lc' and `rc'.
+               // and deposits the results on `lc' and `rc'.
                //
                // On success, the types of `lc' and `rc' on output will always match,
                // and the pair will be one of:
                //
-               static void DoBinaryNumericPromotions (ref Constant left, ref Constant right)
+               static bool DoBinaryNumericPromotions (ref Constant left, ref Constant right)
                {
                        Type ltype = left.Type;
                        Type rtype = right.Type;
 
                        foreach (Type t in binary_promotions) {
-                               if (t == ltype || t == rtype) {
-                                       left = left.ConvertImplicitly (t);
-                                       right = right.ConvertImplicitly (t);
-                                       return;
-                               }
+                               if (t == ltype)
+                                       return t == rtype || ConvertPromotion (ref right, ref left, t);
+
+                               if (t == rtype)
+                                       return t == ltype || ConvertPromotion (ref left, ref right, t);
                        }
 
                        left = left.ConvertImplicitly (TypeManager.int32_type);
                        right = right.ConvertImplicitly (TypeManager.int32_type);
+                       return left != null && right != null;
+               }
+
+               static bool ConvertPromotion (ref Constant prim, ref Constant second, Type type)
+               {
+                       Constant c = prim.ConvertImplicitly (type);
+                       if (c != null) {
+                               prim = c;
+                               return true;
+                       }
+
+                       if (type == TypeManager.uint32_type) {
+                               type = TypeManager.int64_type;
+                               prim = prim.ConvertImplicitly (type);
+                               second = second.ConvertImplicitly (type);
+                               return prim != null && second != null;
+                       }
+
+                       return false;
                }
 
                internal static void Error_CompileTimeOverflow (Location loc)
@@ -55,26 +74,35 @@ namespace Mono.CSharp {
                static public Constant BinaryFold (EmitContext ec, Binary.Operator oper,
                                                     Constant left, Constant right, Location loc)
                {
+                       Constant result = null;
+
                        if (left is EmptyConstantCast)
                                return BinaryFold (ec, oper, ((EmptyConstantCast)left).child, right, loc);
 
-                       if (left is SideEffectConstant)
-                               return BinaryFold (ec, oper, ((SideEffectConstant) left).left, right, loc);
+                       if (left is SideEffectConstant) {
+                               result = BinaryFold (ec, oper, ((SideEffectConstant) left).value, right, loc);
+                               if (result == null)
+                                       return null;
+                               return new SideEffectConstant (result, left, loc);
+                       }
 
                        if (right is EmptyConstantCast)
                                return BinaryFold (ec, oper, left, ((EmptyConstantCast)right).child, loc);
 
-                       if (right is SideEffectConstant)
-                               return BinaryFold (ec, oper, left, ((SideEffectConstant) right).left, loc);
+                       if (right is SideEffectConstant) {
+                               result = BinaryFold (ec, oper, left, ((SideEffectConstant) right).value, loc);
+                               if (result == null)
+                                       return null;
+                               return new SideEffectConstant (result, right, loc);
+                       }
 
                        Type lt = left.Type;
                        Type rt = right.Type;
                        bool bool_res;
-                       Constant result = null;
 
                        if (lt == TypeManager.bool_type && lt == rt) {
-                               bool lv = ((BoolConstant) left ).Value;
-                               bool rv = ((BoolConstant) right).Value;
+                               bool lv = (bool) left.GetValue ();
+                               bool rv = (bool) right.GetValue ();                     
                                switch (oper) {
                                case Binary.Operator.BitwiseAnd:
                                case Binary.Operator.LogicalAnd:
@@ -143,8 +171,7 @@ namespace Mono.CSharp {
 
                        switch (oper){
                        case Binary.Operator.BitwiseOr:
-                               DoBinaryNumericPromotions (ref left, ref right);
-                               if (left == null || right == null)
+                               if (!DoBinaryNumericPromotions (ref left, ref right))
                                        return null;
 
                                if (left is IntConstant){
@@ -171,8 +198,7 @@ namespace Mono.CSharp {
                                break;
                                
                        case Binary.Operator.BitwiseAnd:
-                               DoBinaryNumericPromotions (ref left, ref right);
-                               if (left == null || right == null)
+                               if (!DoBinaryNumericPromotions (ref left, ref right))
                                        return null;
                                
                                ///
@@ -202,8 +228,7 @@ namespace Mono.CSharp {
                                break;
 
                        case Binary.Operator.ExclusiveOr:
-                               DoBinaryNumericPromotions (ref left, ref right);
-                               if (left == null || right == null)
+                               if (!DoBinaryNumericPromotions (ref left, ref right))
                                        return null;
                                
                                if (left is IntConstant){
@@ -229,16 +254,21 @@ namespace Mono.CSharp {
                                break;
 
                        case Binary.Operator.Addition:
+                               if (lt == TypeManager.null_type)
+                                       return right;
+
+                               if (rt == TypeManager.null_type)
+                                       return left;
+
                                //
                                // If both sides are strings, then concatenate, if
                                // one is a string, and the other is not, then defer
                                // to runtime concatenation
                                //
                                if (lt == TypeManager.string_type || rt == TypeManager.string_type){
-                                       if (lt == TypeManager.string_type && rt == TypeManager.string_type)
-                                               return new StringConstant (
-                                                       ((StringConstant) left).Value +
-                                                       ((StringConstant) right).Value, left.Location);
+                                       if (lt == rt)
+                                               return new StringConstant ((string)left.GetValue () + (string)right.GetValue (),
+                                                       left.Location);
                                        
                                        return null;
                                }
@@ -272,8 +302,7 @@ namespace Mono.CSharp {
                                        return new EnumConstant (result, lt);
                                }
 
-                               DoBinaryNumericPromotions (ref left, ref right);
-                               if (left == null || right == null)
+                               if (!DoBinaryNumericPromotions (ref left, ref right))
                                        return null;
 
                                try {
@@ -355,8 +384,6 @@ namespace Mono.CSharp {
                                                                ((DecimalConstant) right).Value);
 
                                                result = new DecimalConstant (res, left.Location);
-                                       } else {
-                                               throw new Exception ( "Unexepected addition input: " + left);
                                        }
                                } catch (OverflowException){
                                        Error_CompileTimeOverflow (loc);
@@ -394,8 +421,7 @@ namespace Mono.CSharp {
                                        return new EnumConstant (result, lt);
                                }
 
-                               DoBinaryNumericPromotions (ref left, ref right);
-                               if (left == null || right == null)
+                               if (!DoBinaryNumericPromotions (ref left, ref right))
                                        return null;
 
                                try {
@@ -486,8 +512,7 @@ namespace Mono.CSharp {
                                return result;
                                
                        case Binary.Operator.Multiply:
-                               DoBinaryNumericPromotions (ref left, ref right);
-                               if (left == null || right == null)
+                               if (!DoBinaryNumericPromotions (ref left, ref right))
                                        return null;
 
                                try {
@@ -577,8 +602,7 @@ namespace Mono.CSharp {
                                break;
 
                        case Binary.Operator.Division:
-                               DoBinaryNumericPromotions (ref left, ref right);
-                               if (left == null || right == null)
+                               if (!DoBinaryNumericPromotions (ref left, ref right))
                                        return null;
 
                                try {
@@ -672,8 +696,7 @@ namespace Mono.CSharp {
                                break;
                                
                        case Binary.Operator.Modulus:
-                               DoBinaryNumericPromotions (ref left, ref right);
-                               if (left == null || right == null)
+                               if (!DoBinaryNumericPromotions (ref left, ref right))
                                        return null;
 
                                try {
@@ -759,7 +782,7 @@ namespace Mono.CSharp {
                        case Binary.Operator.LeftShift:
                                IntConstant ic = right.ConvertImplicitly (TypeManager.int32_type) as IntConstant;
                                if (ic == null){
-                                       Binary.Error_OperatorCannotBeApplied (loc, "<<", lt, rt);
+                                       Binary.Error_OperatorCannotBeApplied (left, right, oper, loc);
                                        return null;
                                }
 
@@ -775,7 +798,7 @@ namespace Mono.CSharp {
                                if (left.Type == TypeManager.int32_type)
                                        return new IntConstant (((IntConstant)left).Value << lshift_val, left.Location);
 
-                               Binary.Error_OperatorCannotBeApplied (loc, "<<", lt, rt);
+                               Binary.Error_OperatorCannotBeApplied (left, right, oper, loc);
                                break;
 
                                //
@@ -784,7 +807,7 @@ namespace Mono.CSharp {
                        case Binary.Operator.RightShift:
                                IntConstant sic = right.ConvertImplicitly (TypeManager.int32_type) as IntConstant;
                                if (sic == null){
-                                       Binary.Error_OperatorCannotBeApplied (loc, ">>", lt, rt);
+                                       Binary.Error_OperatorCannotBeApplied (left, right, oper, loc); ;
                                        return null;
                                }
                                int rshift_val = sic.Value;
@@ -799,18 +822,18 @@ namespace Mono.CSharp {
                                if (left.Type == TypeManager.int32_type)
                                        return new IntConstant (((IntConstant)left).Value >> rshift_val, left.Location);
 
-                               Binary.Error_OperatorCannotBeApplied (loc, ">>", lt, rt);
+                               Binary.Error_OperatorCannotBeApplied (left, right, oper, loc);
                                break;
 
                        case Binary.Operator.Equality:
-                               if (left is NullConstant){
-                                       if (right is NullConstant)
+                               if (left is NullLiteral){
+                                       if (right is NullLiteral)
                                                return new BoolConstant (true, left.Location);
                                        else if (right is StringConstant)
                                                return new BoolConstant (
                                                        ((StringConstant) right).Value == null, left.Location);
-                               } else if (right is NullConstant){
-                                       if (left is NullConstant)
+                               } else if (right is NullLiteral) {
+                                       if (left is NullLiteral)
                                                return new BoolConstant (true, left.Location);
                                        else if (left is StringConstant)
                                                return new BoolConstant (
@@ -823,8 +846,7 @@ namespace Mono.CSharp {
                                        
                                }
 
-                               DoBinaryNumericPromotions (ref left, ref right);
-                               if (left == null || right == null)
+                               if (!DoBinaryNumericPromotions (ref left, ref right))
                                        return null;
 
                                bool_res = false;
@@ -852,14 +874,14 @@ namespace Mono.CSharp {
                                return new BoolConstant (bool_res, left.Location);
 
                        case Binary.Operator.Inequality:
-                               if (left is NullConstant){
-                                       if (right is NullConstant)
+                               if (left is NullLiteral) {
+                                       if (right is NullLiteral)
                                                return new BoolConstant (false, left.Location);
                                        else if (right is StringConstant)
                                                return new BoolConstant (
                                                        ((StringConstant) right).Value != null, left.Location);
-                               } else if (right is NullConstant){
-                                       if (left is NullConstant)
+                               } else if (right is NullLiteral) {
+                                       if (left is NullLiteral)
                                                return new BoolConstant (false, left.Location);
                                        else if (left is StringConstant)
                                                return new BoolConstant (
@@ -872,8 +894,7 @@ namespace Mono.CSharp {
                                        
                                }
 
-                               DoBinaryNumericPromotions (ref left, ref right);
-                               if (left == null || right == null)
+                               if (!DoBinaryNumericPromotions (ref left, ref right))
                                        return null;
 
                                bool_res = false;
@@ -901,8 +922,7 @@ namespace Mono.CSharp {
                                return new BoolConstant (bool_res, left.Location);
 
                        case Binary.Operator.LessThan:
-                               DoBinaryNumericPromotions (ref left, ref right);
-                               if (left == null || right == null)
+                               if (!DoBinaryNumericPromotions (ref left, ref right))
                                        return null;
 
                                bool_res = false;
@@ -930,8 +950,7 @@ namespace Mono.CSharp {
                                return new BoolConstant (bool_res, left.Location);
                                
                        case Binary.Operator.GreaterThan:
-                               DoBinaryNumericPromotions (ref left, ref right);
-                               if (left == null || right == null)
+                               if (!DoBinaryNumericPromotions (ref left, ref right))
                                        return null;
 
                                bool_res = false;
@@ -959,8 +978,7 @@ namespace Mono.CSharp {
                                return new BoolConstant (bool_res, left.Location);
 
                        case Binary.Operator.GreaterThanOrEqual:
-                               DoBinaryNumericPromotions (ref left, ref right);
-                               if (left == null || right == null)
+                               if (!DoBinaryNumericPromotions (ref left, ref right))
                                        return null;
 
                                bool_res = false;
@@ -988,8 +1006,7 @@ namespace Mono.CSharp {
                                return new BoolConstant (bool_res, left.Location);
 
                        case Binary.Operator.LessThanOrEqual:
-                               DoBinaryNumericPromotions (ref left, ref right);
-                               if (left == null || right == null)
+                               if (!DoBinaryNumericPromotions (ref left, ref right))
                                        return null;
 
                                bool_res = false;