Better messages than a throw
[mono.git] / mcs / mcs / cfold.cs
old mode 100755 (executable)
new mode 100644 (file)
index 12999ec..9465eaa
@@ -134,6 +134,12 @@ namespace Mono.CSharp {
                                }
 
                                return;
+                       } else if (left is DecimalConstant || right is DecimalConstant) {
+                               if (!(left is DecimalConstant))
+                                       left = left.ToDecimal (loc);
+                               else if (!(right is DecimalConstant))
+                                       right = right.ToDecimal (loc);
+                               return;
                        } else if (left is EnumConstant || right is EnumConstant){
                                //
                                // If either operand is an enum constant, the other one must
@@ -165,6 +171,8 @@ namespace Mono.CSharp {
                                        left = ((EnumConstant) left).Child;
                                if (right is EnumConstant)
                                        right = ((EnumConstant) right).Child;
+
+                               DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
                                return;
 
                        } else {
@@ -221,7 +229,7 @@ namespace Mono.CSharp {
                                DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
                                if (left == null || right == null)
                                        return null;
-                               
+
                                if (left is IntConstant){
                                        IntConstant v;
                                        int res = ((IntConstant) left).Value | ((IntConstant) right).Value;
@@ -523,8 +531,19 @@ namespace Mono.CSharp {
                                                                         ((IntConstant) right).Value);
 
                                                result = new IntConstant (res);
+                                       } else if (left is DecimalConstant) {
+                                               decimal res;
+
+                                               if (ec.ConstantCheckState)
+                                                       res = checked (((DecimalConstant) left).Value +
+                                                               ((DecimalConstant) right).Value);
+                                               else
+                                                       res = unchecked (((DecimalConstant) left).Value +
+                                                               ((DecimalConstant) right).Value);
+
+                                               result = new DecimalConstant (res);
                                        } else {
-                                               throw new Exception ( "Unexepected input: " + left);
+                                               throw new Exception ( "Unexepected addition input: " + left);
                                        }
                                } catch (OverflowException){
                                        Error_CompileTimeOverflow (loc);
@@ -630,8 +649,19 @@ namespace Mono.CSharp {
                                                                         ((IntConstant) right).Value);
 
                                                result = new IntConstant (res);
+                                       } else if (left is DecimalConstant) {
+                                               decimal res;
+
+                                               if (ec.ConstantCheckState)
+                                                       res = checked (((DecimalConstant) left).Value -
+                                                               ((DecimalConstant) right).Value);
+                                               else
+                                                       res = unchecked (((DecimalConstant) left).Value -
+                                                               ((DecimalConstant) right).Value);
+
+                                               return new DecimalConstant (res);
                                        } else {
-                                               throw new Exception ( "Unexepected input: " + left);
+                                               throw new Exception ( "Unexepected subtraction input: " + left);
                                        }
                                } catch (OverflowException){
                                        Error_CompileTimeOverflow (loc);
@@ -652,10 +682,10 @@ namespace Mono.CSharp {
                                                
                                                if (ec.ConstantCheckState)
                                                        res = checked (((DoubleConstant) left).Value *
-                                                                      ((DoubleConstant) right).Value);
+                                                               ((DoubleConstant) right).Value);
                                                else
                                                        res = unchecked (((DoubleConstant) left).Value *
-                                                                        ((DoubleConstant) right).Value);
+                                                               ((DoubleConstant) right).Value);
                                                
                                                return new DoubleConstant (res);
                                        } else if (left is FloatConstant){
@@ -663,10 +693,10 @@ namespace Mono.CSharp {
                                                
                                                if (ec.ConstantCheckState)
                                                        res = checked (((FloatConstant) left).Value *
-                                                                      ((FloatConstant) right).Value);
+                                                               ((FloatConstant) right).Value);
                                                else
                                                        res = unchecked (((FloatConstant) left).Value *
-                                                                        ((FloatConstant) right).Value);
+                                                               ((FloatConstant) right).Value);
                                                
                                                return new FloatConstant (res);
                                        } else if (left is ULongConstant){
@@ -674,10 +704,10 @@ namespace Mono.CSharp {
                                                
                                                if (ec.ConstantCheckState)
                                                        res = checked (((ULongConstant) left).Value *
-                                                                      ((ULongConstant) right).Value);
+                                                               ((ULongConstant) right).Value);
                                                else
                                                        res = unchecked (((ULongConstant) left).Value *
-                                                                        ((ULongConstant) right).Value);
+                                                               ((ULongConstant) right).Value);
                                                
                                                return new ULongConstant (res);
                                        } else if (left is LongConstant){
@@ -685,10 +715,10 @@ namespace Mono.CSharp {
                                                
                                                if (ec.ConstantCheckState)
                                                        res = checked (((LongConstant) left).Value *
-                                                                      ((LongConstant) right).Value);
+                                                               ((LongConstant) right).Value);
                                                else
                                                        res = unchecked (((LongConstant) left).Value *
-                                                                        ((LongConstant) right).Value);
+                                                               ((LongConstant) right).Value);
                                                
                                                return new LongConstant (res);
                                        } else if (left is UIntConstant){
@@ -696,10 +726,10 @@ namespace Mono.CSharp {
                                                
                                                if (ec.ConstantCheckState)
                                                        res = checked (((UIntConstant) left).Value *
-                                                                      ((UIntConstant) right).Value);
+                                                               ((UIntConstant) right).Value);
                                                else
                                                        res = unchecked (((UIntConstant) left).Value *
-                                                                        ((UIntConstant) right).Value);
+                                                               ((UIntConstant) right).Value);
                                                
                                                return new UIntConstant (res);
                                        } else if (left is IntConstant){
@@ -707,14 +737,25 @@ namespace Mono.CSharp {
 
                                                if (ec.ConstantCheckState)
                                                        res = checked (((IntConstant) left).Value *
-                                                                      ((IntConstant) right).Value);
+                                                               ((IntConstant) right).Value);
                                                else
                                                        res = unchecked (((IntConstant) left).Value *
-                                                                        ((IntConstant) right).Value);
+                                                               ((IntConstant) right).Value);
 
                                                return new IntConstant (res);
+                                       } else if (left is DecimalConstant) {
+                                               decimal res;
+
+                                               if (ec.ConstantCheckState)
+                                                       res = checked (((DecimalConstant) left).Value *
+                                                               ((DecimalConstant) right).Value);
+                                               else
+                                                       res = unchecked (((DecimalConstant) left).Value *
+                                                               ((DecimalConstant) right).Value);
+
+                                               return new DecimalConstant (res);
                                        } else {
-                                               throw new Exception ( "Unexepected input: " + left);
+                                               throw new Exception ( "Unexepected multiply input: " + left);
                                        }
                                } catch (OverflowException){
                                        Error_CompileTimeOverflow (loc);
@@ -732,10 +773,10 @@ namespace Mono.CSharp {
                                                
                                                if (ec.ConstantCheckState)
                                                        res = checked (((DoubleConstant) left).Value /
-                                                                      ((DoubleConstant) right).Value);
+                                                               ((DoubleConstant) right).Value);
                                                else
                                                        res = unchecked (((DoubleConstant) left).Value /
-                                                                        ((DoubleConstant) right).Value);
+                                                               ((DoubleConstant) right).Value);
                                                
                                                return new DoubleConstant (res);
                                        } else if (left is FloatConstant){
@@ -743,10 +784,10 @@ namespace Mono.CSharp {
                                                
                                                if (ec.ConstantCheckState)
                                                        res = checked (((FloatConstant) left).Value /
-                                                                      ((FloatConstant) right).Value);
+                                                               ((FloatConstant) right).Value);
                                                else
                                                        res = unchecked (((FloatConstant) left).Value /
-                                                                        ((FloatConstant) right).Value);
+                                                               ((FloatConstant) right).Value);
                                                
                                                return new FloatConstant (res);
                                        } else if (left is ULongConstant){
@@ -754,10 +795,10 @@ namespace Mono.CSharp {
                                                
                                                if (ec.ConstantCheckState)
                                                        res = checked (((ULongConstant) left).Value /
-                                                                      ((ULongConstant) right).Value);
+                                                               ((ULongConstant) right).Value);
                                                else
                                                        res = unchecked (((ULongConstant) left).Value /
-                                                                        ((ULongConstant) right).Value);
+                                                               ((ULongConstant) right).Value);
                                                
                                                return new ULongConstant (res);
                                        } else if (left is LongConstant){
@@ -765,10 +806,10 @@ namespace Mono.CSharp {
                                                
                                                if (ec.ConstantCheckState)
                                                        res = checked (((LongConstant) left).Value /
-                                                                      ((LongConstant) right).Value);
+                                                               ((LongConstant) right).Value);
                                                else
                                                        res = unchecked (((LongConstant) left).Value /
-                                                                        ((LongConstant) right).Value);
+                                                               ((LongConstant) right).Value);
                                                
                                                return new LongConstant (res);
                                        } else if (left is UIntConstant){
@@ -776,10 +817,10 @@ namespace Mono.CSharp {
                                                
                                                if (ec.ConstantCheckState)
                                                        res = checked (((UIntConstant) left).Value /
-                                                                      ((UIntConstant) right).Value);
+                                                               ((UIntConstant) right).Value);
                                                else
                                                        res = unchecked (((UIntConstant) left).Value /
-                                                                        ((UIntConstant) right).Value);
+                                                               ((UIntConstant) right).Value);
                                                
                                                return new UIntConstant (res);
                                        } else if (left is IntConstant){
@@ -787,14 +828,25 @@ namespace Mono.CSharp {
 
                                                if (ec.ConstantCheckState)
                                                        res = checked (((IntConstant) left).Value /
-                                                                      ((IntConstant) right).Value);
+                                                               ((IntConstant) right).Value);
                                                else
                                                        res = unchecked (((IntConstant) left).Value /
-                                                                        ((IntConstant) right).Value);
+                                                               ((IntConstant) right).Value);
 
                                                return new IntConstant (res);
+                                       } else if (left is DecimalConstant) {
+                                               decimal res;
+
+                                               if (ec.ConstantCheckState)
+                                                       res = checked (((DecimalConstant) left).Value /
+                                                               ((DecimalConstant) right).Value);
+                                               else
+                                                       res = unchecked (((DecimalConstant) left).Value /
+                                                               ((DecimalConstant) right).Value);
+
+                                               return new DecimalConstant (res);
                                        } else {
-                                               throw new Exception ( "Unexepected input: " + left);
+                                               throw new Exception ( "Unexepected division input: " + left);
                                        }
                                } catch (OverflowException){
                                        Error_CompileTimeOverflow (loc);
@@ -878,7 +930,7 @@ namespace Mono.CSharp {
 
                                                return new IntConstant (res);
                                        } else {
-                                               throw new Exception ( "Unexepected input: " + left);
+                                               throw new Exception ( "Unexepected modulus input: " + left);
                                        }
                                } catch (DivideByZeroException){
                                        Report.Error (020, loc, "Division by constant zero");