Better messages than a throw
[mono.git] / mcs / mcs / cfold.cs
old mode 100755 (executable)
new mode 100644 (file)
index a87f836..9465eaa
@@ -128,9 +128,18 @@ namespace Mono.CSharp {
                                if (other is SByteConstant || other is ShortConstant || ic != null){
                                        left = left.ToLong (loc);
                                        right = right.ToLong (loc);
+                               } else {
+                                       left = left.ToUInt (loc);
+                                       right = left.ToUInt (loc);
                                }
 
                                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
@@ -162,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 {
@@ -218,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;
@@ -520,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);
@@ -627,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);
@@ -649,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){
@@ -660,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){
@@ -671,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){
@@ -682,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){
@@ -693,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){
@@ -704,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);
@@ -729,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){
@@ -740,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){
@@ -751,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){
@@ -762,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){
@@ -773,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){
@@ -784,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);
@@ -875,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");
@@ -967,6 +1022,19 @@ namespace Mono.CSharp {
                                                ((BoolConstant) right).Value);
                                
                                }
+                               if (left is NullLiteral){
+                                       if (right is NullLiteral)
+                                               return new BoolConstant (true);
+                                       else if (right is StringConstant)
+                                               return new BoolConstant (
+                                                       ((StringConstant) right).Value == null);
+                               } else if (right is NullLiteral){
+                                       if (left is NullLiteral)
+                                               return new BoolConstant (true);
+                                       else if (left is StringConstant)
+                                               return new BoolConstant (
+                                                       ((StringConstant) left).Value == null);
+                               }
                                if (left is StringConstant && right is StringConstant){
                                        return new BoolConstant (
                                                ((StringConstant) left).Value ==
@@ -1008,6 +1076,19 @@ namespace Mono.CSharp {
                                                ((BoolConstant) left).Value !=
                                                ((BoolConstant) right).Value);
                                }
+                               if (left is NullLiteral){
+                                       if (right is NullLiteral)
+                                               return new BoolConstant (false);
+                                       else if (right is StringConstant)
+                                               return new BoolConstant (
+                                                       ((StringConstant) right).Value != null);
+                               } else if (right is NullLiteral){
+                                       if (left is NullLiteral)
+                                               return new BoolConstant (false);
+                                       else if (left is StringConstant)
+                                               return new BoolConstant (
+                                                       ((StringConstant) left).Value != null);
+                               }
                                if (left is StringConstant && right is StringConstant){
                                        return new BoolConstant (
                                                ((StringConstant) left).Value !=