1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
|
//
// cfold.cs: Constant Folding
//
// Author:
// Miguel de Icaza (miguel@ximian.com)
// Marek Safar (marek.safar@seznam.cz)
//
// Copyright 2002, 2003 Ximian, Inc.
// Copyright 2003-2011, Novell, Inc.
//
using System;
namespace Mono.CSharp {
public static class ConstantFold
{
public static TypeSpec[] CreateBinaryPromotionsTypes (BuiltinTypes types)
{
return new TypeSpec[] {
types.Decimal, types.Double, types.Float,
types.ULong, types.Long, types.UInt
};
}
//
// Performs the numeric promotions on the left and right expresions
// 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:
//
// TODO: BinaryFold should be called as an optimization step only,
// error checking here is weak
//
static bool DoBinaryNumericPromotions (ResolveContext rc, ref Constant left, ref Constant right)
{
TypeSpec ltype = left.Type;
TypeSpec rtype = right.Type;
foreach (TypeSpec t in rc.BuiltinTypes.BinaryPromotionsTypes) {
if (t == ltype)
return t == rtype || ConvertPromotion (rc, ref right, ref left, t);
if (t == rtype)
return t == ltype || ConvertPromotion (rc, ref left, ref right, t);
}
left = left.ConvertImplicitly (rc.BuiltinTypes.Int);
right = right.ConvertImplicitly (rc.BuiltinTypes.Int);
return left != null && right != null;
}
static bool ConvertPromotion (ResolveContext rc, ref Constant prim, ref Constant second, TypeSpec type)
{
Constant c = prim.ConvertImplicitly (type);
if (c != null) {
prim = c;
return true;
}
if (type.BuiltinType == BuiltinTypeSpec.Type.UInt) {
type = rc.BuiltinTypes.Long;
prim = prim.ConvertImplicitly (type);
second = second.ConvertImplicitly (type);
return prim != null && second != null;
}
return false;
}
internal static void Error_CompileTimeOverflow (ResolveContext rc, Location loc)
{
rc.Report.Error (220, loc, "The operation overflows at compile time in checked mode");
}
/// <summary>
/// Constant expression folder for binary operations.
///
/// Returns null if the expression can not be folded.
/// </summary>
static public Constant BinaryFold (ResolveContext 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) {
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) {
result = BinaryFold (ec, oper, left, ((SideEffectConstant) right).value, loc);
if (result == null)
return null;
return new SideEffectConstant (result, right, loc);
}
TypeSpec lt = left.Type;
TypeSpec rt = right.Type;
bool bool_res;
if (lt.BuiltinType == BuiltinTypeSpec.Type.Bool && lt == rt) {
bool lv = (bool) left.GetValue ();
bool rv = (bool) right.GetValue ();
switch (oper) {
case Binary.Operator.BitwiseAnd:
case Binary.Operator.LogicalAnd:
return new BoolConstant (ec.BuiltinTypes, lv && rv, left.Location);
case Binary.Operator.BitwiseOr:
case Binary.Operator.LogicalOr:
return new BoolConstant (ec.BuiltinTypes, lv || rv, left.Location);
case Binary.Operator.ExclusiveOr:
return new BoolConstant (ec.BuiltinTypes, lv ^ rv, left.Location);
case Binary.Operator.Equality:
return new BoolConstant (ec.BuiltinTypes, lv == rv, left.Location);
case Binary.Operator.Inequality:
return new BoolConstant (ec.BuiltinTypes, lv != rv, left.Location);
}
return null;
}
//
// During an enum evaluation, none of the rules are valid
// Not sure whether it is bug in csc or in documentation
//
if (ec.HasSet (ResolveContext.Options.EnumScope)){
if (left is EnumConstant)
left = ((EnumConstant) left).Child;
if (right is EnumConstant)
right = ((EnumConstant) right).Child;
} else if (left is EnumConstant && rt == lt) {
switch (oper){
///
/// E operator |(E x, E y);
/// E operator &(E x, E y);
/// E operator ^(E x, E y);
///
case Binary.Operator.BitwiseOr:
case Binary.Operator.BitwiseAnd:
case Binary.Operator.ExclusiveOr:
result = BinaryFold (ec, oper, ((EnumConstant)left).Child, ((EnumConstant)right).Child, loc);
if (result != null)
result = result.Reduce (ec, lt);
return result;
///
/// U operator -(E x, E y);
///
case Binary.Operator.Subtraction:
result = BinaryFold (ec, oper, ((EnumConstant)left).Child, ((EnumConstant)right).Child, loc);
if (result != null)
result = result.Reduce (ec, EnumSpec.GetUnderlyingType (lt));
return result;
///
/// bool operator ==(E x, E y);
/// bool operator !=(E x, E y);
/// bool operator <(E x, E y);
/// bool operator >(E x, E y);
/// bool operator <=(E x, E y);
/// bool operator >=(E x, E y);
///
case Binary.Operator.Equality:
case Binary.Operator.Inequality:
case Binary.Operator.LessThan:
case Binary.Operator.GreaterThan:
case Binary.Operator.LessThanOrEqual:
case Binary.Operator.GreaterThanOrEqual:
return BinaryFold(ec, oper, ((EnumConstant)left).Child, ((EnumConstant)right).Child, loc);
}
return null;
}
switch (oper){
case Binary.Operator.BitwiseOr:
//
// bool? operator |(bool? x, bool? y);
//
if ((lt.BuiltinType == BuiltinTypeSpec.Type.Bool && right is NullLiteral) ||
(rt.BuiltinType == BuiltinTypeSpec.Type.Bool && left is NullLiteral)) {
var b = new Binary (oper, left, right).ResolveOperator (ec);
// false | null => null
// null | false => null
if ((right is NullLiteral && left.IsDefaultValue) || (left is NullLiteral && right.IsDefaultValue))
return Nullable.LiftedNull.CreateFromExpression (ec, b);
// true | null => true
// null | true => true
return ReducedExpression.Create (new BoolConstant (ec.BuiltinTypes, true, loc), b);
}
if (!DoBinaryNumericPromotions (ec, ref left, ref right))
return null;
if (left is IntConstant){
int res = ((IntConstant) left).Value | ((IntConstant) right).Value;
return new IntConstant (ec.BuiltinTypes, res, left.Location);
}
if (left is UIntConstant){
uint res = ((UIntConstant)left).Value | ((UIntConstant)right).Value;
return new UIntConstant (ec.BuiltinTypes, res, left.Location);
}
if (left is LongConstant){
long res = ((LongConstant)left).Value | ((LongConstant)right).Value;
return new LongConstant (ec.BuiltinTypes, res, left.Location);
}
if (left is ULongConstant){
ulong res = ((ULongConstant)left).Value |
((ULongConstant)right).Value;
return new ULongConstant (ec.BuiltinTypes, res, left.Location);
}
break;
case Binary.Operator.BitwiseAnd:
//
// bool? operator &(bool? x, bool? y);
//
if ((lt.BuiltinType == BuiltinTypeSpec.Type.Bool && right is NullLiteral) ||
(rt.BuiltinType == BuiltinTypeSpec.Type.Bool && left is NullLiteral)) {
var b = new Binary (oper, left, right).ResolveOperator (ec);
// false & null => false
// null & false => false
if ((right is NullLiteral && left.IsDefaultValue) || (left is NullLiteral && right.IsDefaultValue))
return ReducedExpression.Create (new BoolConstant (ec.BuiltinTypes, false, loc), b);
// true & null => null
// null & true => null
return Nullable.LiftedNull.CreateFromExpression (ec, b);
}
if (!DoBinaryNumericPromotions (ec, ref left, ref right))
return null;
///
/// int operator &(int x, int y);
/// uint operator &(uint x, uint y);
/// long operator &(long x, long y);
/// ulong operator &(ulong x, ulong y);
///
if (left is IntConstant){
int res = ((IntConstant) left).Value & ((IntConstant) right).Value;
return new IntConstant (ec.BuiltinTypes, res, left.Location);
}
if (left is UIntConstant){
uint res = ((UIntConstant)left).Value & ((UIntConstant)right).Value;
return new UIntConstant (ec.BuiltinTypes, res, left.Location);
}
if (left is LongConstant){
long res = ((LongConstant)left).Value & ((LongConstant)right).Value;
return new LongConstant (ec.BuiltinTypes, res, left.Location);
}
if (left is ULongConstant){
ulong res = ((ULongConstant)left).Value &
((ULongConstant)right).Value;
return new ULongConstant (ec.BuiltinTypes, res, left.Location);
}
break;
case Binary.Operator.ExclusiveOr:
if (!DoBinaryNumericPromotions (ec, ref left, ref right))
return null;
if (left is IntConstant){
int res = ((IntConstant) left).Value ^ ((IntConstant) right).Value;
return new IntConstant (ec.BuiltinTypes, res, left.Location);
}
if (left is UIntConstant){
uint res = ((UIntConstant)left).Value ^ ((UIntConstant)right).Value;
return new UIntConstant (ec.BuiltinTypes, res, left.Location);
}
if (left is LongConstant){
long res = ((LongConstant)left).Value ^ ((LongConstant)right).Value;
return new LongConstant (ec.BuiltinTypes, res, left.Location);
}
if (left is ULongConstant){
ulong res = ((ULongConstant)left).Value ^
((ULongConstant)right).Value;
return new ULongConstant (ec.BuiltinTypes, res, left.Location);
}
break;
case Binary.Operator.Addition:
//
// If both sides are strings, then concatenate
//
// string operator + (string x, string y)
//
if (lt.BuiltinType == BuiltinTypeSpec.Type.String || rt.BuiltinType == BuiltinTypeSpec.Type.String){
if (lt == rt)
return new StringConstant (ec.BuiltinTypes, (string)left.GetValue () + (string)right.GetValue (),
left.Location);
if (lt == InternalType.NullLiteral)
return new StringConstant (ec.BuiltinTypes, "" + right.GetValue (), left.Location);
if (rt == InternalType.NullLiteral)
return new StringConstant (ec.BuiltinTypes, left.GetValue () + "", left.Location);
return null;
}
//
// string operator + (string x, object y)
//
if (lt == InternalType.NullLiteral) {
if (rt.BuiltinType == BuiltinTypeSpec.Type.Object)
return new StringConstant (ec.BuiltinTypes, "" + right.GetValue (), left.Location);
if (lt == rt) {
ec.Report.Error (34, loc, "Operator `{0}' is ambiguous on operands of type `{1}' and `{2}'",
"+", lt.GetSignatureForError (), rt.GetSignatureForError ());
return null;
}
return right;
}
//
// string operator + (object x, string y)
//
if (rt == InternalType.NullLiteral) {
if (lt.BuiltinType == BuiltinTypeSpec.Type.Object)
return new StringConstant (ec.BuiltinTypes, right.GetValue () + "", left.Location);
return left;
}
//
// handle "E operator + (E x, U y)"
// handle "E operator + (Y y, E x)"
//
EnumConstant lc = left as EnumConstant;
EnumConstant rc = right as EnumConstant;
if (lc != null || rc != null){
if (lc == null) {
lc = rc;
lt = lc.Type;
right = left;
}
// U has to be implicitly convetible to E.base
right = right.ConvertImplicitly (lc.Child.Type);
if (right == null)
return null;
result = BinaryFold (ec, oper, lc.Child, right, loc);
if (result == null)
return null;
result = result.Reduce (ec, lt);
if (result == null)
return null;
return new EnumConstant (result, lt);
}
if (!DoBinaryNumericPromotions (ec, ref left, ref right))
return null;
try {
if (left is DoubleConstant){
double res;
if (ec.ConstantCheckState)
res = checked (((DoubleConstant) left).Value +
((DoubleConstant) right).Value);
else
res = unchecked (((DoubleConstant) left).Value +
((DoubleConstant) right).Value);
return new DoubleConstant (ec.BuiltinTypes, res, left.Location);
}
if (left is FloatConstant){
float res;
if (ec.ConstantCheckState)
res = checked (((FloatConstant) left).Value +
((FloatConstant) right).Value);
else
res = unchecked (((FloatConstant) left).Value +
((FloatConstant) right).Value);
result = new FloatConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is ULongConstant){
ulong res;
if (ec.ConstantCheckState)
res = checked (((ULongConstant) left).Value +
((ULongConstant) right).Value);
else
res = unchecked (((ULongConstant) left).Value +
((ULongConstant) right).Value);
result = new ULongConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is LongConstant){
long res;
if (ec.ConstantCheckState)
res = checked (((LongConstant) left).Value +
((LongConstant) right).Value);
else
res = unchecked (((LongConstant) left).Value +
((LongConstant) right).Value);
result = new LongConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is UIntConstant){
uint res;
if (ec.ConstantCheckState)
res = checked (((UIntConstant) left).Value +
((UIntConstant) right).Value);
else
res = unchecked (((UIntConstant) left).Value +
((UIntConstant) right).Value);
result = new UIntConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is IntConstant){
int res;
if (ec.ConstantCheckState)
res = checked (((IntConstant) left).Value +
((IntConstant) right).Value);
else
res = unchecked (((IntConstant) left).Value +
((IntConstant) right).Value);
result = new IntConstant (ec.BuiltinTypes, res, left.Location);
} 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 (ec.BuiltinTypes, res, left.Location);
}
} catch (OverflowException){
Error_CompileTimeOverflow (ec, loc);
}
return result;
case Binary.Operator.Subtraction:
//
// handle "E operator - (E x, U y)"
// handle "E operator - (Y y, E x)"
//
lc = left as EnumConstant;
rc = right as EnumConstant;
if (lc != null || rc != null){
if (lc == null) {
lc = rc;
lt = lc.Type;
right = left;
}
// U has to be implicitly convetible to E.base
right = right.ConvertImplicitly (lc.Child.Type);
if (right == null)
return null;
result = BinaryFold (ec, oper, lc.Child, right, loc);
if (result == null)
return null;
result = result.Reduce (ec, lt);
if (result == null)
return null;
return new EnumConstant (result, lt);
}
if (left is NullLiteral && right is NullLiteral) {
var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
lifted_int.ResolveAsType (ec);
return (Constant) new Binary (oper, lifted_int, right).ResolveOperator (ec);
}
if (!DoBinaryNumericPromotions (ec, ref left, ref right))
return null;
try {
if (left is DoubleConstant){
double res;
if (ec.ConstantCheckState)
res = checked (((DoubleConstant) left).Value -
((DoubleConstant) right).Value);
else
res = unchecked (((DoubleConstant) left).Value -
((DoubleConstant) right).Value);
result = new DoubleConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is FloatConstant){
float res;
if (ec.ConstantCheckState)
res = checked (((FloatConstant) left).Value -
((FloatConstant) right).Value);
else
res = unchecked (((FloatConstant) left).Value -
((FloatConstant) right).Value);
result = new FloatConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is ULongConstant){
ulong res;
if (ec.ConstantCheckState)
res = checked (((ULongConstant) left).Value -
((ULongConstant) right).Value);
else
res = unchecked (((ULongConstant) left).Value -
((ULongConstant) right).Value);
result = new ULongConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is LongConstant){
long res;
if (ec.ConstantCheckState)
res = checked (((LongConstant) left).Value -
((LongConstant) right).Value);
else
res = unchecked (((LongConstant) left).Value -
((LongConstant) right).Value);
result = new LongConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is UIntConstant){
uint res;
if (ec.ConstantCheckState)
res = checked (((UIntConstant) left).Value -
((UIntConstant) right).Value);
else
res = unchecked (((UIntConstant) left).Value -
((UIntConstant) right).Value);
result = new UIntConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is IntConstant){
int res;
if (ec.ConstantCheckState)
res = checked (((IntConstant) left).Value -
((IntConstant) right).Value);
else
res = unchecked (((IntConstant) left).Value -
((IntConstant) right).Value);
result = new IntConstant (ec.BuiltinTypes, res, left.Location);
} 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 (ec.BuiltinTypes, res, left.Location);
} else {
throw new Exception ( "Unexepected subtraction input: " + left);
}
} catch (OverflowException){
Error_CompileTimeOverflow (ec, loc);
}
return result;
case Binary.Operator.Multiply:
if (left is NullLiteral && right is NullLiteral) {
var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
lifted_int.ResolveAsType (ec);
return (Constant) new Binary (oper, lifted_int, right).ResolveOperator (ec);
}
if (!DoBinaryNumericPromotions (ec, ref left, ref right))
return null;
try {
if (left is DoubleConstant){
double res;
if (ec.ConstantCheckState)
res = checked (((DoubleConstant) left).Value *
((DoubleConstant) right).Value);
else
res = unchecked (((DoubleConstant) left).Value *
((DoubleConstant) right).Value);
return new DoubleConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is FloatConstant){
float res;
if (ec.ConstantCheckState)
res = checked (((FloatConstant) left).Value *
((FloatConstant) right).Value);
else
res = unchecked (((FloatConstant) left).Value *
((FloatConstant) right).Value);
return new FloatConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is ULongConstant){
ulong res;
if (ec.ConstantCheckState)
res = checked (((ULongConstant) left).Value *
((ULongConstant) right).Value);
else
res = unchecked (((ULongConstant) left).Value *
((ULongConstant) right).Value);
return new ULongConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is LongConstant){
long res;
if (ec.ConstantCheckState)
res = checked (((LongConstant) left).Value *
((LongConstant) right).Value);
else
res = unchecked (((LongConstant) left).Value *
((LongConstant) right).Value);
return new LongConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is UIntConstant){
uint res;
if (ec.ConstantCheckState)
res = checked (((UIntConstant) left).Value *
((UIntConstant) right).Value);
else
res = unchecked (((UIntConstant) left).Value *
((UIntConstant) right).Value);
return new UIntConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is IntConstant){
int res;
if (ec.ConstantCheckState)
res = checked (((IntConstant) left).Value *
((IntConstant) right).Value);
else
res = unchecked (((IntConstant) left).Value *
((IntConstant) right).Value);
return new IntConstant (ec.BuiltinTypes, res, left.Location);
} 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 (ec.BuiltinTypes, res, left.Location);
} else {
throw new Exception ( "Unexepected multiply input: " + left);
}
} catch (OverflowException){
Error_CompileTimeOverflow (ec, loc);
}
break;
case Binary.Operator.Division:
if (left is NullLiteral && right is NullLiteral) {
var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
lifted_int.ResolveAsType (ec);
return (Constant) new Binary (oper, lifted_int, right).ResolveOperator (ec);
}
if (!DoBinaryNumericPromotions (ec, ref left, ref right))
return null;
try {
if (left is DoubleConstant){
double res;
if (ec.ConstantCheckState)
res = checked (((DoubleConstant) left).Value /
((DoubleConstant) right).Value);
else
res = unchecked (((DoubleConstant) left).Value /
((DoubleConstant) right).Value);
return new DoubleConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is FloatConstant){
float res;
if (ec.ConstantCheckState)
res = checked (((FloatConstant) left).Value /
((FloatConstant) right).Value);
else
res = unchecked (((FloatConstant) left).Value /
((FloatConstant) right).Value);
return new FloatConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is ULongConstant){
ulong res;
if (ec.ConstantCheckState)
res = checked (((ULongConstant) left).Value /
((ULongConstant) right).Value);
else
res = unchecked (((ULongConstant) left).Value /
((ULongConstant) right).Value);
return new ULongConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is LongConstant){
long res;
if (ec.ConstantCheckState)
res = checked (((LongConstant) left).Value /
((LongConstant) right).Value);
else
res = unchecked (((LongConstant) left).Value /
((LongConstant) right).Value);
return new LongConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is UIntConstant){
uint res;
if (ec.ConstantCheckState)
res = checked (((UIntConstant) left).Value /
((UIntConstant) right).Value);
else
res = unchecked (((UIntConstant) left).Value /
((UIntConstant) right).Value);
return new UIntConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is IntConstant){
int res;
if (ec.ConstantCheckState)
res = checked (((IntConstant) left).Value /
((IntConstant) right).Value);
else
res = unchecked (((IntConstant) left).Value /
((IntConstant) right).Value);
return new IntConstant (ec.BuiltinTypes, res, left.Location);
} 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 (ec.BuiltinTypes, res, left.Location);
} else {
throw new Exception ( "Unexepected division input: " + left);
}
} catch (OverflowException){
Error_CompileTimeOverflow (ec, loc);
} catch (DivideByZeroException) {
ec.Report.Error (20, loc, "Division by constant zero");
}
break;
case Binary.Operator.Modulus:
if (left is NullLiteral && right is NullLiteral) {
var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
lifted_int.ResolveAsType (ec);
return (Constant) new Binary (oper, lifted_int, right).ResolveOperator (ec);
}
if (!DoBinaryNumericPromotions (ec, ref left, ref right))
return null;
try {
if (left is DoubleConstant){
double res;
if (ec.ConstantCheckState)
res = checked (((DoubleConstant) left).Value %
((DoubleConstant) right).Value);
else
res = unchecked (((DoubleConstant) left).Value %
((DoubleConstant) right).Value);
return new DoubleConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is FloatConstant){
float res;
if (ec.ConstantCheckState)
res = checked (((FloatConstant) left).Value %
((FloatConstant) right).Value);
else
res = unchecked (((FloatConstant) left).Value %
((FloatConstant) right).Value);
return new FloatConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is ULongConstant){
ulong res;
if (ec.ConstantCheckState)
res = checked (((ULongConstant) left).Value %
((ULongConstant) right).Value);
else
res = unchecked (((ULongConstant) left).Value %
((ULongConstant) right).Value);
return new ULongConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is LongConstant){
long res;
if (ec.ConstantCheckState)
res = checked (((LongConstant) left).Value %
((LongConstant) right).Value);
else
res = unchecked (((LongConstant) left).Value %
((LongConstant) right).Value);
return new LongConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is UIntConstant){
uint res;
if (ec.ConstantCheckState)
res = checked (((UIntConstant) left).Value %
((UIntConstant) right).Value);
else
res = unchecked (((UIntConstant) left).Value %
((UIntConstant) right).Value);
return new UIntConstant (ec.BuiltinTypes, res, left.Location);
} else if (left is IntConstant){
int res;
if (ec.ConstantCheckState)
res = checked (((IntConstant) left).Value %
((IntConstant) right).Value);
else
res = unchecked (((IntConstant) left).Value %
((IntConstant) right).Value);
return new IntConstant (ec.BuiltinTypes, res, left.Location);
} else {
throw new Exception ( "Unexepected modulus input: " + left);
}
} catch (DivideByZeroException){
ec.Report.Error (20, loc, "Division by constant zero");
} catch (OverflowException){
Error_CompileTimeOverflow (ec, loc);
}
break;
//
// There is no overflow checking on left shift
//
case Binary.Operator.LeftShift:
if (left is NullLiteral && right is NullLiteral) {
var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
lifted_int.ResolveAsType (ec);
return (Constant) new Binary (oper, lifted_int, right).ResolveOperator (ec);
}
IntConstant ic = right.ConvertImplicitly (ec.BuiltinTypes.Int) as IntConstant;
if (ic == null){
Binary.Error_OperatorCannotBeApplied (ec, left, right, oper, loc);
return null;
}
int lshift_val = ic.Value;
switch (left.Type.BuiltinType) {
case BuiltinTypeSpec.Type.ULong:
return new ULongConstant (ec.BuiltinTypes, ((ULongConstant) left).Value << lshift_val, left.Location);
case BuiltinTypeSpec.Type.Long:
return new LongConstant (ec.BuiltinTypes, ((LongConstant) left).Value << lshift_val, left.Location);
case BuiltinTypeSpec.Type.UInt:
return new UIntConstant (ec.BuiltinTypes, ((UIntConstant) left).Value << lshift_val, left.Location);
}
// null << value => null
if (left is NullLiteral)
return (Constant) new Binary (oper, left, right).ResolveOperator (ec);
left = left.ConvertImplicitly (ec.BuiltinTypes.Int);
if (left.Type.BuiltinType == BuiltinTypeSpec.Type.Int)
return new IntConstant (ec.BuiltinTypes, ((IntConstant) left).Value << lshift_val, left.Location);
Binary.Error_OperatorCannotBeApplied (ec, left, right, oper, loc);
break;
//
// There is no overflow checking on right shift
//
case Binary.Operator.RightShift:
if (left is NullLiteral && right is NullLiteral) {
var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
lifted_int.ResolveAsType (ec);
return (Constant) new Binary (oper, lifted_int, right).ResolveOperator (ec);
}
IntConstant sic = right.ConvertImplicitly (ec.BuiltinTypes.Int) as IntConstant;
if (sic == null){
Binary.Error_OperatorCannotBeApplied (ec, left, right, oper, loc); ;
return null;
}
int rshift_val = sic.Value;
switch (left.Type.BuiltinType) {
case BuiltinTypeSpec.Type.ULong:
return new ULongConstant (ec.BuiltinTypes, ((ULongConstant) left).Value >> rshift_val, left.Location);
case BuiltinTypeSpec.Type.Long:
return new LongConstant (ec.BuiltinTypes, ((LongConstant) left).Value >> rshift_val, left.Location);
case BuiltinTypeSpec.Type.UInt:
return new UIntConstant (ec.BuiltinTypes, ((UIntConstant) left).Value >> rshift_val, left.Location);
}
// null >> value => null
if (left is NullLiteral)
return (Constant) new Binary (oper, left, right).ResolveOperator (ec);
left = left.ConvertImplicitly (ec.BuiltinTypes.Int);
if (left.Type.BuiltinType == BuiltinTypeSpec.Type.Int)
return new IntConstant (ec.BuiltinTypes, ((IntConstant) left).Value >> rshift_val, left.Location);
Binary.Error_OperatorCannotBeApplied (ec, left, right, oper, loc);
break;
case Binary.Operator.Equality:
if (TypeSpec.IsReferenceType (lt) && TypeSpec.IsReferenceType (rt) ||
(left is Nullable.LiftedNull && right.IsNull) ||
(right is Nullable.LiftedNull && left.IsNull)) {
if (left.IsNull || right.IsNull) {
return ReducedExpression.Create (
new BoolConstant (ec.BuiltinTypes, left.IsNull == right.IsNull, left.Location),
new Binary (oper, left, right));
}
if (left is StringConstant && right is StringConstant)
return new BoolConstant (ec.BuiltinTypes,
((StringConstant) left).Value == ((StringConstant) right).Value, left.Location);
return null;
}
if (!DoBinaryNumericPromotions (ec, ref left, ref right))
return null;
bool_res = false;
if (left is DoubleConstant)
bool_res = ((DoubleConstant) left).Value ==
((DoubleConstant) right).Value;
else if (left is FloatConstant)
bool_res = ((FloatConstant) left).Value ==
((FloatConstant) right).Value;
else if (left is ULongConstant)
bool_res = ((ULongConstant) left).Value ==
((ULongConstant) right).Value;
else if (left is LongConstant)
bool_res = ((LongConstant) left).Value ==
((LongConstant) right).Value;
else if (left is UIntConstant)
bool_res = ((UIntConstant) left).Value ==
((UIntConstant) right).Value;
else if (left is IntConstant)
bool_res = ((IntConstant) left).Value ==
((IntConstant) right).Value;
else
return null;
return new BoolConstant (ec.BuiltinTypes, bool_res, left.Location);
case Binary.Operator.Inequality:
if (TypeSpec.IsReferenceType (lt) && TypeSpec.IsReferenceType (rt) ||
(left is Nullable.LiftedNull && right.IsNull) ||
(right is Nullable.LiftedNull && left.IsNull)) {
if (left.IsNull || right.IsNull) {
return ReducedExpression.Create (
new BoolConstant (ec.BuiltinTypes, left.IsNull != right.IsNull, left.Location),
new Binary (oper, left, right));
}
if (left is StringConstant && right is StringConstant)
return new BoolConstant (ec.BuiltinTypes,
((StringConstant) left).Value != ((StringConstant) right).Value, left.Location);
return null;
}
if (!DoBinaryNumericPromotions (ec, ref left, ref right))
return null;
bool_res = false;
if (left is DoubleConstant)
bool_res = ((DoubleConstant) left).Value !=
((DoubleConstant) right).Value;
else if (left is FloatConstant)
bool_res = ((FloatConstant) left).Value !=
((FloatConstant) right).Value;
else if (left is ULongConstant)
bool_res = ((ULongConstant) left).Value !=
((ULongConstant) right).Value;
else if (left is LongConstant)
bool_res = ((LongConstant) left).Value !=
((LongConstant) right).Value;
else if (left is UIntConstant)
bool_res = ((UIntConstant) left).Value !=
((UIntConstant) right).Value;
else if (left is IntConstant)
bool_res = ((IntConstant) left).Value !=
((IntConstant) right).Value;
else
return null;
return new BoolConstant (ec.BuiltinTypes, bool_res, left.Location);
case Binary.Operator.LessThan:
if (right is NullLiteral) {
if (left is NullLiteral) {
var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
lifted_int.ResolveAsType (ec);
return (Constant) new Binary (oper, lifted_int, right).ResolveOperator (ec);
}
}
if (!DoBinaryNumericPromotions (ec, ref left, ref right))
return null;
bool_res = false;
if (left is DoubleConstant)
bool_res = ((DoubleConstant) left).Value <
((DoubleConstant) right).Value;
else if (left is FloatConstant)
bool_res = ((FloatConstant) left).Value <
((FloatConstant) right).Value;
else if (left is ULongConstant)
bool_res = ((ULongConstant) left).Value <
((ULongConstant) right).Value;
else if (left is LongConstant)
bool_res = ((LongConstant) left).Value <
((LongConstant) right).Value;
else if (left is UIntConstant)
bool_res = ((UIntConstant) left).Value <
((UIntConstant) right).Value;
else if (left is IntConstant)
bool_res = ((IntConstant) left).Value <
((IntConstant) right).Value;
else
return null;
return new BoolConstant (ec.BuiltinTypes, bool_res, left.Location);
case Binary.Operator.GreaterThan:
if (right is NullLiteral) {
if (left is NullLiteral) {
var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
lifted_int.ResolveAsType (ec);
return (Constant) new Binary (oper, lifted_int, right).ResolveOperator (ec);
}
}
if (!DoBinaryNumericPromotions (ec, ref left, ref right))
return null;
bool_res = false;
if (left is DoubleConstant)
bool_res = ((DoubleConstant) left).Value >
((DoubleConstant) right).Value;
else if (left is FloatConstant)
bool_res = ((FloatConstant) left).Value >
((FloatConstant) right).Value;
else if (left is ULongConstant)
bool_res = ((ULongConstant) left).Value >
((ULongConstant) right).Value;
else if (left is LongConstant)
bool_res = ((LongConstant) left).Value >
((LongConstant) right).Value;
else if (left is UIntConstant)
bool_res = ((UIntConstant) left).Value >
((UIntConstant) right).Value;
else if (left is IntConstant)
bool_res = ((IntConstant) left).Value >
((IntConstant) right).Value;
else
return null;
return new BoolConstant (ec.BuiltinTypes, bool_res, left.Location);
case Binary.Operator.GreaterThanOrEqual:
if (right is NullLiteral) {
if (left is NullLiteral) {
var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
lifted_int.ResolveAsType (ec);
return (Constant) new Binary (oper, lifted_int, right).ResolveOperator (ec);
}
}
if (!DoBinaryNumericPromotions (ec, ref left, ref right))
return null;
bool_res = false;
if (left is DoubleConstant)
bool_res = ((DoubleConstant) left).Value >=
((DoubleConstant) right).Value;
else if (left is FloatConstant)
bool_res = ((FloatConstant) left).Value >=
((FloatConstant) right).Value;
else if (left is ULongConstant)
bool_res = ((ULongConstant) left).Value >=
((ULongConstant) right).Value;
else if (left is LongConstant)
bool_res = ((LongConstant) left).Value >=
((LongConstant) right).Value;
else if (left is UIntConstant)
bool_res = ((UIntConstant) left).Value >=
((UIntConstant) right).Value;
else if (left is IntConstant)
bool_res = ((IntConstant) left).Value >=
((IntConstant) right).Value;
else
return null;
return new BoolConstant (ec.BuiltinTypes, bool_res, left.Location);
case Binary.Operator.LessThanOrEqual:
if (right is NullLiteral) {
if (left is NullLiteral) {
var lifted_int = new Nullable.NullableType (ec.BuiltinTypes.Int, loc);
lifted_int.ResolveAsType (ec);
return (Constant) new Binary (oper, lifted_int, right).ResolveOperator (ec);
}
}
if (!DoBinaryNumericPromotions (ec, ref left, ref right))
return null;
bool_res = false;
if (left is DoubleConstant)
bool_res = ((DoubleConstant) left).Value <=
((DoubleConstant) right).Value;
else if (left is FloatConstant)
bool_res = ((FloatConstant) left).Value <=
((FloatConstant) right).Value;
else if (left is ULongConstant)
bool_res = ((ULongConstant) left).Value <=
((ULongConstant) right).Value;
else if (left is LongConstant)
bool_res = ((LongConstant) left).Value <=
((LongConstant) right).Value;
else if (left is UIntConstant)
bool_res = ((UIntConstant) left).Value <=
((UIntConstant) right).Value;
else if (left is IntConstant)
bool_res = ((IntConstant) left).Value <=
((IntConstant) right).Value;
else
return null;
return new BoolConstant (ec.BuiltinTypes, bool_res, left.Location);
}
return null;
}
}
}
|