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 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
|
/* exprtype.c -- propagates datatype thru expressions.
Copyright (c) 2001 by Robert K. Moniot.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Acknowledgement: the above permission notice is what is known
as the "MIT License."
*/
/* I. */
/* $Id: exprtype.c,v 1.15 2005/02/07 00:38:01 moniot Exp $
Routines to propagate datatype through expressions.
binexpr_type() Yields result type of binary expression.
unexpr_type() Yields result type of unary expression.
assignment_stmt_type() Checks assignment statement type.
func_ref_expr(id,args,result) Forms token for a function invocation.
primary_id_expr() Forms token for primary which is an identifier.
stmt_fun_arg_cmp(t1,t2) Checks agreement between stmt func args.
int int_power(x,n) Computes x**n for value propagation.
init_typesizes(wdsize) Sets standard type sizes
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "ftnchek.h"
#define EXPRTYPE
#include "symtab.h"
#include "symutils.h"
#include "tokdefs.h"
PROTO(PRIVATE char* sized_typename,( int type, long size ));
PROTO(PRIVATE void report_mismatch,( const Token *term1, const Token *op, const Token *term2 ));
PROTO(PRIVATE void report_type,( const Token *t ));
PROTO(PRIVATE int int_power,( int x, int n ));
PROTO(PRIVATE int eval_intrins,( IntrinsInfo *defn, Token *args ));
/* shorthand for datatypes. must match those in symtab.h */
/* N.B. Also, the fact that type_DEFAULT=0 is assumed in size
propagation code. */
#define E 0 /* Error for invalid type combos */
#define I 1
#define R 2
#define D 3
#define C 4
#define Z 5
#define L 6
#define S 7
#define H 8
#define NumT (H+1) /* number of types in tables below */
#define W 10 /* Warning for nonstandard type combos: W>NumT */
/* for + - / * ** ANSI book pp. 6-5,6-6 */
/* Mixed double+complex = double complex with
warning, double + double complex is OK */
PRIVATE unsigned char arith_expr_type[NumT][NumT]={
/*E I R D C Z L S H */
{ E, E, E, E, E, E, E, E, E }, /* E */
{ E, I, R, D, C, Z, E, E, E }, /* I */
{ E, R, R, D, C, Z, E, E, E }, /* R */
{ E, D, D, D,W+Z, Z, E, E, E }, /* D */
{ E, C, C,W+Z, C, Z, E, E, E }, /* C */
{ E, Z, Z, Z, Z, Z, E, E, E }, /* Z */
{ E, E, E, E, E, E, E, E, E }, /* L */
{ E, E, E, E, E, E, E, E, E }, /* S */
{ E, E, E, E, E, E, E, E, E } /* H */
};
/* for relops. Corresponds to arith type table
except that nonstandard comparisons of like
types have warning, not error. */
PRIVATE unsigned char rel_expr_type[NumT][NumT]={
/*E I R D C Z L S H */
{ E, E, E, E, E, E, E, E, E }, /* E */
{ E, L, L, L, L, L, E, E,W+L }, /* I */
{ E, L, L, L, L, L, E, E, E }, /* R */
{ E, L, L, L,W+L, L, E, E, E }, /* D */
{ E, L, L,W+L, L, L, E, E, E }, /* C */
{ E, L, L, L, L, L, E, E, E }, /* Z */
{ E, E, E, E, E, E,W+L, E,W+L }, /* L */
{ E, E, E, E, E, E, E, L, E }, /* S */
{ E,W+L, E, E, E, E,W+L, E,W+L } /* H */
};
/* Result of assignment: lvalue = expr. Here rows
correspond to type of lvalue, columns to type
of expr */
PRIVATE unsigned char assignment_type[NumT][NumT]={
/*E I R D C Z L S H */
{ E, E, E, E, E, E, E, E, E }, /* E */
{ E, I, I, I, I, I, E, E,W+I }, /* I */
{ E, R, R, R, R, R, E, E,W+R }, /* R */
{ E, D, D, D, D, D, E, E,W+D }, /* D */
{ E, C, C, C, C, C, E, E,W+C }, /* C */
{ E, Z, Z, Z, Z, Z, E, E,W+Z }, /* Z */
{ E, E, E, E, E, E, L, E,W+L }, /* L */
{ E, E, E, E, E, E, E, S, E }, /* S */
{ E, E, E, E, E, E, E, E, E } /* H not possible for lvalue */
};
#define INTRINS_ARGS (opclass == ',') /* Flag to modify behavior of binexpr_type */
/* Routine used in printing diagnostics: returns string "type" for
unsized objects, "type*size" for explicitly sized things. Due
to use of local static variable, cannot be invoked twice in the
same expression. */
PRIVATE char*
#if HAVE_STDC
sized_typename(int type, long int size)
#else /* K&R style */
sized_typename(type,size)
int type; long size;
#endif /* HAVE_STDC */
{
static char strbuf[]="type*000000"; /* template */
static char *char_unk="char*(?)";
static char *char_adj="char*(*)";
if(size == size_DEFAULT) {
return type_name[type]; /* no explicit size */
}
else {
if(type != S || size > 0) {
(void)sprintf(strbuf,"%4s*%ld", /* type*size */
type_name[type],
size%1000000);
}
else { /* handle special character size codes */
if(size == size_ADJUSTABLE)
return char_adj;
else /*size_UNKNOWN*/
return char_unk;
}
}
return strbuf;
}
void
init_typesizes(VOID)
/* Only executes once. Thus cannot change wordsize
after processing starts. */
{
static int trapdoor=FALSE, ptr_trapdoor=FALSE;
if(trapdoor) {
if(given_wordsize != local_wordsize) {
(void)fprintf(stderr,
"\nSorry-Cannot change wordsize after processing starts");
}
given_wordsize = local_wordsize;
}
else {
trapdoor = TRUE;
local_wordsize = given_wordsize;
if(given_wordsize != 0) {
if(given_wordsize != BpW) {
type_size[I] = type_size[R] = type_size[L] = (BYTE)given_wordsize;
type_size[D] = type_size[C] = (BYTE)(2*given_wordsize);
type_size[Z] = (BYTE)(4*given_wordsize);
}
}
}
/* Cray pointer size is set separately */
if(ptr_trapdoor) {
if(given_ptrsize != local_ptrsize) {
(void)fprintf(stderr,
"\nSorry-Cannot change pointer size after processing starts");
}
given_ptrsize = local_ptrsize;
}
else {
ptr_trapdoor = TRUE;
local_ptrsize = given_ptrsize;
}
}
/* this routine propagates type in binary expressions */
void
#if HAVE_STDC
binexpr_type(Token *term1, Token *op, Token *term2, Token *result)
#else /* K&R style */
binexpr_type(term1,op,term2,result)
Token *term1, *op, *term2, *result;
#endif /* HAVE_STDC */
{
int opclass = op->tclass,
type1 = datatype_of(term1->TOK_type),
type2 = datatype_of(term2->TOK_type),
result_type;
long
size1 = term1->size,
size2 = term2->size,
result_size;
int I_logop_I=FALSE; /* for f90_mixed_type warning */
if( ! is_computational_type(type1) ) {
if( misc_warn ) {
syntax_error(term1->line_num,term1->col_num,
"numeric or character quantity expected:");
report_type(term1);
}
result_type = E;
}
else if( ! is_computational_type(type2) ) {
if( misc_warn ) {
syntax_error(term2->line_num,term2->col_num,
"numeric or character quantity expected:");
report_type(term2);
}
result_type = E;
}
else {
switch(opclass) {
/* arithmetic operators: use lookup table */
case '+':
case '-':
case '*':
case '/':
case tok_power:
result_type = (unsigned)arith_expr_type[type1][type2];
break;
/* relational operators: use lookup table */
case tok_relop:
result_type = (unsigned)rel_expr_type[type1][type2];
break;
/* logical operators: operands should be
logical, but allow integers with a
warning. */
case tok_AND:
case tok_OR:
case tok_EQV:
case tok_NEQV:
if(type1 == L && type2 == L)
result_type = L;
else if(type1 == I && type2 == I) {
result_type = W+I;
I_logop_I = TRUE;
}
else
result_type = E;
break;
/* // operator: operands must be strings */
case tok_concat:
if(type1 == S && type2 == S)
result_type = S;
else
result_type = E;
break;
/* Intrinsic function argument list: no promotion
across type categories. Accept matching type
categories: size match will be checked later. */
case ',': /* INTRINS_ARGS */
if( type_category[type1] != type_category[type2] )
result_type = E;
else if(type1 == S)
result_type = S;
else
result_type = (unsigned)arith_expr_type[type1][type2];
break;
default:
oops_message(OOPS_NONFATAL,
op->line_num,op->col_num,
"operator unknown: type not propagated");
result_type = type1;
break;
}
if( (type1 != E && type2 != E) ) {
if( result_type == E) {
if(INTRINS_ARGS) {
syntax_error(op->line_num,op->col_num,
"type mismatch between intrinsic function arguments:");
report_mismatch(term1,op,term2);
}
else if( misc_warn ) {
syntax_error(op->line_num,op->col_num,
"operands cannot be combined in expression:");
report_mismatch(term1,op,term2);
}
}
else if(result_type >= W) { /* W result */
/* F90 warning suppressed for numeric exprs */
if(f77_mixed_expr ||
(f90_mixed_expr && ((type1>=L || type2>=L) || I_logop_I)) ) {
nonstandard(op->line_num,op->col_num,f90_mixed_expr,0);
msg_tail(": incompatible type combination in expression:");
report_mismatch(term1,op,term2);
}
result_type -= W;
}
/* Obscure standard rule 6.2.2. We have to look
for IN_ASSIGN flag in any of 3 places, since
it gets prematurely turned off in
fun_or_substr_handle production if one of
operands is a substring expression. */
else if( f77_mixed_expr &&
opclass == tok_concat &&
!is_true(IN_ASSIGN,
(term1->TOK_flags|op->TOK_flags|term2->TOK_flags)) &&
((size1==size_ADJUSTABLE && !is_true(CONST_EXPR,term1->TOK_flags))
|| (size2==size_ADJUSTABLE && !is_true(CONST_EXPR,term2->TOK_flags))) ) {
nonstandard(op->line_num,op->col_num,0,0);
msg_tail(": adjustable size cannot be concatenated here");
}
}
}
/* Figure out the size of result */
result_size = size_DEFAULT;
if(result_type != E ) { /* Error type gets DEFAULT size */
if(opclass == tok_concat) { /* string//string yields sum of lengths */
if(size1 == size_UNKNOWN || size2 == size_UNKNOWN)
result_size = size_UNKNOWN;
else
if(size1 == size_ADJUSTABLE || size2 == size_ADJUSTABLE)
result_size = size_ADJUSTABLE;
else {
result_size = size1 + size2;
if(port_long_string && result_size > 255)
nonportable(op->line_num,op->col_num,
"character expression length exceeds 255");
}
}
/* DEFAULT op DEFAULT always yields DEFAULT. So need
to handle only explicitly sized expressions,
except intrinsic arglists, where no promotion
of plain real to dble or plain complex to dcpx,
and check for promotions of real types.
*/
else if(INTRINS_ARGS?
(type1 != type2 ||
(type1 == type2 && is_numeric_type(type1) &&
(size1 != size_DEFAULT || size2 != size_DEFAULT))) :
((size1 != size_DEFAULT || size2 != size_DEFAULT) ||
(trunc_promotion &&
is_float_type(type1) && is_float_type(type2))))
{
/* Local variables for convenience.
N.B. Use tc1/2,ls1/2 for tests,
t1/2,s1/2 for assigning result.
*/
int t1,t2; /* sorted types: t1 <= t2. */
long s1,s2; /* sizes of t1 and t2. */
int tc1,tc2; /* type categories: D->R and Z->C */
long ls1,ls2; /* local sizes = declared size else type_size */
int defsize1,defsize2; /* flags for default size */
/* Sort so that t1 <= t2 */
if(type1 <= type2) {
t1 = type1; s1 = size1;
t2 = type2; s2 = size2;
}
else {
t1 = type2; s1 = size2;
t2 = type1; s2 = size1;
}
/* Assign type categories and local sizes */
tc1 = type_category[t1];
tc2 = type_category[t2];
defsize1 = (s1 == size_DEFAULT);
defsize2 = (s2 == size_DEFAULT);
ls1 = (defsize1? type_size[t1]: s1);
ls2 = (defsize2? type_size[t2]: s2);
#ifdef DEBUG_EXPRTYPE
if(debug_latest)
(void)fprintf(list_fd,"\nt1=%s s1=%d ls1=%d t2=%s s2=%d ls2=%d",
type_name[t1],s1,ls1, type_name[t2], s2, ls2);
#endif
if(tc1 == tc2) {/* same type category */
/* Intrins args: size promotion illegal */
if(INTRINS_ARGS && ls1 != ls2) {
syntax_error(op->line_num,op->col_num,
"precision mismatch in intrinsic argument list:");
report_mismatch(term1,op,term2);
}
/* Give -port warning if e.g. plain I+I*2
(variables only) */
else if(port_mixed_size || local_wordsize==0) {
if(defsize1 != defsize2
&& !is_true(CONST_EXPR,term1->TOK_flags)
&& !is_true(CONST_EXPR,term2->TOK_flags))
{
nonportable(op->line_num,op->col_num,
INTRINS_ARGS?"intrinsic argument list":"expr");
msg_tail("mixes default and explicit");
msg_tail((is_numeric_type(t1)&&is_numeric_type(t2))?
"precision":"size");
msg_tail("operands:");
report_mismatch(term1,op,term2);
}
}
/* If same type category, use the larger of the two sizes if
both declared. If only one size declared, use the
larger of the declared size and the default size.
If result is equal in size to default, use size_DEFAULT.
*/
if(ls1 > ls2) {
result_size = s1;
}
else if(ls2 > ls1) {
result_size = s2;
}
else /*ls1 == ls2*/{
if(!defsize1 && !defsize2)
result_size = s1; /* otherwise DEFAULT */
}
}/* end(tc1==tc2) */
else /* tc1!=tc2 */ {
/* Differing type categories: only two cases. */
/* Case 1: I + R|D|C|Z
Result: size of dominant type */
if(tc1 == I) {
result_size = s2;
}
/* Case 2: R|D + C|Z
Result: larger of C|Z and 2*size of R|D */
else {
if(ls2 >= 2*ls1)
result_size = s2;
else
result_size = 2*s1; /* 2*size_DEFAULT = 0 is still DEFAULT */
}
}/* end tc1 != tc2 */
/* change D or Z to default size or else
to explicitly sized R or C
*/
if(result_type == D || result_type == Z) {
if(result_size != size_DEFAULT
&& result_size != type_size[result_type])
result_type = (result_type==D)?R:C;
else
result_size = size_DEFAULT;
}
/* Give -trunc warning if a real or
complex type is promoted to double. */
if(trunc_promotion && !INTRINS_ARGS && is_float_type(t1) ) {
/* First clause checks R+R size agreement */
if( (type_category[result_type] == R && ls1 != ls2)
/* Second clause checks R+C and C+C */
|| (type_category[result_type] == C &&
(type_category[t1] == R? ls2 != 2*ls1 : ls2 != ls1)) ){
warning(op->line_num,op->col_num,
"promotion may not give desired precision:");
report_mismatch(term1,op,term2);
}
/* For comparisons we have R==R, C==C, or R==C. */
if( opclass == tok_relop &&
(type_category[t1] == type_category[t2]? ls2 != ls1:ls2 != 2*ls1) ) {
warning(op->line_num,op->col_num,
"comparison mixes terms of different precision:");
report_mismatch(term1,op,term2);
}
}
}/*end if(non-DEFAULT sizes)*/
/* Result of compare gets DEFAULT size. However, we want to
go thru the above code for relops in case of type/size
mismatches that should be reported. Here for relops we
replace any result_size that got set above by DEFAULT size.
*/
if( opclass == tok_relop) {
result_size = size_DEFAULT;
}
}/*end if(result_type != E)*/
#ifdef DEBUG_EXPRTYPE
if(debug_latest) {
(void)fprintf(list_fd,"\nsize of %s %c",sized_typename(type1,size1),
ispunct(opclass)?opclass:'~');
(void)fprintf(list_fd," %s = ",sized_typename(type2,size2));
(void)fprintf(list_fd,"%s",sized_typename(result_type,result_size));
}
#endif
result->TOK_type = type_byte(class_VAR, result_type);
result->TOK_flags = 0; /* clear all flags */
result->size = result_size;
/* Keep track of constant expressions */
if( is_true(CONST_EXPR,term1->TOK_flags)
&& is_true(CONST_EXPR,term2->TOK_flags)
&& !(opclass==tok_power && type2!=I) ) { /* exclude **REAL */
make_true(CONST_EXPR,result->TOK_flags);
}
/* Parameter expressions are like constant exprs
except we bend the rules to allow intrinsic functions
and **REAL */
if( is_true(PARAMETER_EXPR,term1->TOK_flags)
&& is_true(PARAMETER_EXPR,term2->TOK_flags) ) {
make_true(PARAMETER_EXPR,result->TOK_flags);
}
/* Keep track of dimension bound expressions */
if( is_true(DIM_BOUND_EXPR,term1->TOK_flags)
&& is_true(DIM_BOUND_EXPR,term2->TOK_flags) ) {
make_true(DIM_BOUND_EXPR,result->TOK_flags);
}
if( is_true(EVALUATED_EXPR,term1->TOK_flags)
&& is_true(EVALUATED_EXPR,term2->TOK_flags) ) {
make_true(EVALUATED_EXPR,result->TOK_flags);
}
#ifdef DEBUG_EXPRTYPE
if(debug_latest)
(void)fprintf(list_fd,"\nconst param eval: (%d %d %d) %s (%d %d %d) = (%d %d %d)",
is_true(CONST_EXPR,term1->TOK_flags),
is_true(PARAMETER_EXPR,term1->TOK_flags),
is_true(EVALUATED_EXPR,term1->TOK_flags),
operator->src_text,
is_true(CONST_EXPR,term2->TOK_flags),
is_true(PARAMETER_EXPR,term2->TOK_flags),
is_true(EVALUATED_EXPR,term2->TOK_flags),
is_true(CONST_EXPR,result->TOK_flags),
is_true(PARAMETER_EXPR,result->TOK_flags),
is_true(EVALUATED_EXPR,result->TOK_flags));
#endif
if(! INTRINS_ARGS) { /* Remaining steps only applicable to exprs */
/* Remember if integer division was used */
if(result_type == type_INTEGER &&
(opclass == '/' ||
(is_true(INT_QUOTIENT_EXPR,term1->TOK_flags) ||
is_true(INT_QUOTIENT_EXPR,term2->TOK_flags))) ) {
make_true(INT_QUOTIENT_EXPR,result->TOK_flags);
}
/* Issue warning if integer expr involving division is
later converted to any real type, or if it is used
as an exponent. */
if( is_true(INT_QUOTIENT_EXPR,term1->TOK_flags)
|| is_true(INT_QUOTIENT_EXPR,term2->TOK_flags) ) {
int r=result_type;
if(r == type_LOGICAL) /* relational tests are equivalent */
r = arith_expr_type[type1][type2]; /* to subtraction */
if(opclass == tok_power && is_true(INT_QUOTIENT_EXPR,term2->TOK_flags) ) {
if(trunc_int_div_exponent) {
warning(op->line_num,op->col_num,
"integer quotient expr");
msg_expr_tree(term2);
msg_tail("used in exponent");
}
if( ! is_true(INT_QUOTIENT_EXPR,term1->TOK_flags) )
make_false(INT_QUOTIENT_EXPR,result->TOK_flags);
}
else if( r == type_REAL || r == type_DP || r == type_COMPLEX) {
if(trunc_int_div_real) {
warning(op->line_num,op->col_num,
"integer quotient expr");
msg_expr_tree(is_true(INT_QUOTIENT_EXPR,term1->TOK_flags)?
term1:term2);
msg_tail(" converted to real");
}
}
}
/* If either term is an identifier, set use flag */
if(is_true(ID_EXPR,term1->TOK_flags))
use_variable(term1);
if(is_true(ID_EXPR,term2->TOK_flags))
use_variable(term2);
/* Propagate the value of integer constant expressions */
if(is_true(EVALUATED_EXPR,result->TOK_flags)) {
if(result_type == type_INTEGER) { /* Only ints propagated */
int a = int_expr_value(term1),
b = int_expr_value(term2),
c;
switch(opclass) {
case '+': c = a+b; break;
case '-': c = a-b; break;
case '*': c = a*b; break;
case '/': if(b == 0) {
if(misc_warn) {
syntax_error(term2->line_num,term2->col_num,
"division by zero attempted");
}
c = 0;
}
else {
c = a/b;
}
break;
case tok_power: c = int_power(a,b); break;
case tok_AND: c = a&b; break;
case tok_OR: c = a|b; break;
case tok_EQV: c = ~(a^b); break;
case tok_NEQV: c = a^b; break;
default:
oops_message(OOPS_NONFATAL,
op->line_num,op->col_num,
"invalid int expr operator");
c = 0; break;
}
make_true(EVALUATED_EXPR,result->TOK_flags);
result->value.integer = c; /* Result goes into token value */
/* Integer division (including i**neg)
that yields 0 is suspicious. */
if(trunc_int_div_zero)
if(c==0 && (opclass=='/' || opclass==tok_power)) {
warning(op->line_num,op->col_num,
"integer const expr yields result of 0");
}
}
}
/* Also nonconstant**neg is 0 unless
nonconstant=1 */
else if(trunc_int_neg_power)
if(result_type == type_INTEGER && opclass == tok_power
&& is_true(EVALUATED_EXPR,term2->TOK_flags)
&& int_expr_value(term2) < 0) {
warning(op->line_num,op->col_num,
"integer to negative power usually yields 0");
}
}/* end if !INTRINS_ARGS */
}/*binexpr_type*/
/* this routine propagates type in unary expressions */
void
#if HAVE_STDC
unexpr_type(Token *op, Token *term1, Token *result)
#else /* K&R style */
unexpr_type(op,term1,result)
Token *term1, *op, *result;
#endif /* HAVE_STDC */
{
int opclass = op->tclass,
type1 = datatype_of(term1->TOK_type),
result_type;
if( ! is_computational_type(type1) ) {
if( misc_warn ) {
syntax_error(term1->line_num,term1->col_num,
"numeric quantity expected:");
report_type(term1);
}
result_type = E;
}
else {
switch(opclass) {
/* arith operators: use diagonal of lookup table */
case '+':
case '-':
result_type = arith_expr_type[type1][type1];
break;
/* NOT: operand should be
logical, but allow integers with a
warning. */
case tok_NOT:
if(type1 == L)
result_type = L;
else if(type1 == I)
result_type = W+I;
else
result_type = E;
break;
default:
oops_message(OOPS_NONFATAL,
op->line_num,op->col_num,
"unary operator type not propagated");
result_type = type1;
break;
}
if( type1 != E ) {
if( result_type == E) {
if( misc_warn ) {
syntax_error(op->line_num,op->col_num,
"expression incompatible with operator:");
msg_tail(op->src_text);
msg_tail("used with");
report_type(term1);
}
}
else if(result_type >= W) {
if(f77_mixed_expr || f90_mixed_expr) {
nonstandard(op->line_num,op->col_num,f90_mixed_expr,0);
msg_tail(": incompatible type used with operator:");
msg_tail(op->src_text);
msg_tail("used with");
report_type(term1);
}
result_type -= W;
}
}
}
result->TOK_type = type_byte(class_VAR, result_type);
result->TOK_flags = 0; /* clear all flags */
result->size = term1->size; /* result is same size as operand */
/* Keep track of constant expressions */
copy_flag(CONST_EXPR,result->TOK_flags,term1->TOK_flags);
copy_flag(PARAMETER_EXPR,result->TOK_flags,term1->TOK_flags);
copy_flag(DIM_BOUND_EXPR,result->TOK_flags,term1->TOK_flags);
/* Remember if integer division was used */
if(result_type == type_INTEGER)
copy_flag(INT_QUOTIENT_EXPR,result->TOK_flags,term1->TOK_flags);
if(is_true(ID_EXPR,term1->TOK_flags))
use_variable(term1);
/* Propagate the value of integer constant expressions */
if(is_true(EVALUATED_EXPR,term1->TOK_flags)) {
if(result_type == type_INTEGER) { /* Only ints propagated */
int a = int_expr_value(term1),
c;
switch(opclass) {
case '+': c = a; break;
case '-': c = -a; break;
case tok_NOT: c = ~a; break;
default: oops_message(OOPS_NONFATAL,
op->line_num,op->col_num,
"invalid int expr operator");
c = 0; break;
}
make_true(EVALUATED_EXPR,result->TOK_flags);
result->value.integer = c; /* Result goes into token value */
}
}
}
/* this routine checks type and size match in assignment statements
and in parameter assignments */
void
#if HAVE_STDC
assignment_stmt_type(Token *term1, Token *equals, Token *term2)
#else /* K&R style */
assignment_stmt_type(term1,equals,term2)
Token *term1, *equals, *term2;
#endif /* HAVE_STDC */
{
int type1 = datatype_of(term1->TOK_type),
type2 = datatype_of(term2->TOK_type),
result_type;
if( ! is_computational_type(type1) ) {
if( misc_warn ) {
syntax_error(term1->line_num,term1->col_num,
"numeric or character quantity expected:");
report_type(term1);
}
result_type = E;
}
else if( ! is_computational_type(type2) ) {
if( misc_warn ) {
syntax_error(term2->line_num,term2->col_num,
"numeric or character quantity expected:");
report_type(term2);
}
result_type = E;
}
else {
result_type = (unsigned)assignment_type[type1][type2];
if( (type1 != E && type2 != E) ) {
if( result_type == E) {
if( misc_warn ) {
syntax_error(equals->line_num,equals->col_num,
"type mismatch:");
report_type(term2);
msg_tail("assigned to");
report_type(term1);
}
}
else {
if(result_type >= W) { /* W result */
if(f77_mixed_expr || f90_mixed_expr) {
nonstandard(equals->line_num,equals->col_num,f90_mixed_expr,0);
msg_tail(": incompatible type combination:");
report_type(term2);
msg_tail("assigned to");
report_type(term1);
}
result_type -= W;
}
/* Watch for truncation to lower precision type */
if(trunc_precision ||
port_mixed_size || local_wordsize==0) {
long size1 = term1->size;
long size2 = term2->size;
int type_trunc=FALSE, /* flags for kind of truncation */
size_trunc=FALSE,
mixed_size=FALSE,
promotion=FALSE,
trunc_warn,mixed_warn;
if(size1 == size_DEFAULT && size2 == size_DEFAULT) {
type_trunc = ( is_numeric_type(type1) &&
is_numeric_type(type2) &&
(type1 < type2 ||
/* C = D truncates precision of D */
(type1 == C && type2 == D)) );
/* Watch for promotions also */
if(type_category[type2] == R) {
if(type_category[type1] == R) /* R|D = R|D */
promotion = (type1 > type2);
else if(type_category[type1] == C) /* C|Z = R|D */
promotion =
((int)type_size[type1] > 2*(int)type_size[type2]);
}
else if(type_category[type2] == C) /* any = C|Z */
promotion = (type1 > type2);
}
else if(type1 == S) { /* character strings */
if(size1>0 && size2>0) /* ignore ADJUSTABLE and UNKNOWN */
size_trunc = size1 < size2;
} else {
int tc1,tc2;/* type categories: D->R, Z->C, H->I */
int ls1,ls2;/* local sizes */
/* Assign type categories and local sizes */
tc1 = type_category[type1];
tc2 = type_category[type2];
ls1 = size1; if(ls1 == size_DEFAULT) ls1 = type_size[type1];
ls2 = size2; if(ls2 == size_DEFAULT) ls2 = type_size[type2];
/* type truncation: any numeric type category
to a lower category. */
type_trunc = ( /***is_numeric_type(type1) &&
is_numeric_type(type2) &&***/
tc1 < tc2 );
/* size truncation: assigned to smaller
local size. For C = R correct test is
Csize < 2*Rsize */
if(tc1 == C && tc2 == R) {
size_trunc = (ls1 < ls2*2);
promotion = (ls1 > ls2*2);
}
else {
/* Suppress size truncation warning if rhs
is a literal constant that is sure to fit.
For logicals this is always the case; for
integers we use a suitable threshold.
*/
if( (size_trunc = (ls1 < ls2)) &&
is_true(LIT_CONST,term2->TOK_flags) ){
switch(tc2) {
case L:
size_trunc = FALSE;
break;
case I:
if( term2->value.integer <= SMALL_INT_VALUE )
size_trunc = FALSE;
break;
}
}
promotion = ((tc2 == R || tc2 == C) && (ls1 > ls2));
}
/* mixed size: default size assigned to
declared size of like type category
or vice-versa. -port only, and superseded
by truncation warning if any. */
mixed_size = (tc1 == tc2) &&
(size1==size_DEFAULT ||
(size2==size_DEFAULT &&
!is_true(CONST_EXPR,term2->TOK_flags)));
}
/* Under -trunc, report type truncation or size
truncation. Say "possibly" if -nowordsize.
Also report promotions under -trunc.
If no truncation warning given and under -port,
report mixed assignment */
#ifdef DEBUG_EXPRTYPE
#define TorF(x) ((x)?"":"no")
if(debug_latest) {
(void)fprintf(list_fd,"\nassign %s =",sized_typename(type1,size1));
(void)fprintf(list_fd," %s : ",sized_typename(type2,size2));
(void)fprintf(list_fd,"%s type %s size %s mixed",
TorF(type_trunc),
TorF(size_trunc),
TorF(mixed_size));
}
#endif
trunc_warn = (trunc_promotion && promotion) ||
(trunc_type_demotion && type_trunc) ||
(trunc_size_demotion && size_trunc);
mixed_warn = ((port_mixed_size || local_wordsize==0) &&
mixed_size);
if( trunc_warn ) {
warning(equals->line_num,equals->col_num,"");
report_type(term2);
if(trunc_warn && !type_trunc && mixed_size
&& local_wordsize == 0)
msg_tail("possibly");
if(promotion)
msg_tail("promoted to");
else
msg_tail("truncated to");
report_type(term1);
if(promotion)
msg_tail(": may not give desired precision");
}
else if(mixed_warn) {
nonportable(equals->line_num,equals->col_num,
"mixed default and explicit");
msg_tail((is_numeric_type(type1)&&is_numeric_type(type2))?
"precision":"size");
msg_tail("items:");
report_type(term2);
msg_tail("assigned to");
report_type(term1);
}
}
}/*end else (result_type != E)*/
}/*end if (type1,type2 != E)*/
}/*end else (is_computational_type(type2))*/
/* Issue warning if integer expr involving division is
later converted to any real type. */
if(trunc_int_div_real)
if( is_true(INT_QUOTIENT_EXPR,term2->TOK_flags) ) {
int r=result_type;
if( r == type_REAL || r == type_DP || r == type_COMPLEX) {
warning(equals->line_num,equals->col_num,
"integer quotient expr");
msg_expr_tree(term2);
msg_tail(" converted to real");
}
}
if(is_true(ID_EXPR,term2->TOK_flags))
use_variable(term2);
use_lvalue(term1);
}
void
check_initializer_type(Token *assignee_list, Token *equals, Token *expr_list)
{
Token *t;
if( expr_list->next_token == (Token*)NULL ) {
t = expr_list; /* simple token, not a list */
}
else {
/* token lists are built in reverse, so
restore to order in source statement */
t = expr_list->next_token = reverse_tokenlist(expr_list->next_token);
}
/* Go thru list, checking match.
At this time, assignee can only be a single
variable
*/
while( t!=NULL ) {
assignment_stmt_type(assignee_list,t,t);
t = t->next_token;
}
}
/* Make an expression-token for a function invocation */
void
#if HAVE_STDC
func_ref_expr(Token *id, Token *args, Token *result)
#else /* K&R style */
func_ref_expr(id,args,result)
Token *id,*args,*result;
#endif /* HAVE_STDC */
{
Lsymtab *symt;
IntrinsInfo *defn;
int rettype, retsize;
symt = hashtab[id->value.integer].loc_symtab;
if( symt->intrinsic ) {
defn = symt->info.intrins_info;
/* Intrinsic functions: type stored in info field */
rettype = defn->result_type;
retsize = size_DEFAULT;
if( defn->intrins_flags & I_QUAD ) { /* Quad intrinsic */
/* These are either R*16 or X*32 */
retsize = ((rettype==type_QUAD)? size_QUAD: size_CQUAD);
}
/* Generic Intrinsic functions: use propagated arg type */
if(rettype == type_GENERIC) {
if(args->next_token == NULL) {
rettype = type_UNDECL;
retsize = size_DEFAULT;
}
else {
#ifdef OLDSTUFF
rettype = args->next_token->TOK_type;
retsize = args->next_token->size;
#else
rettype = args->TOK_type;
retsize = args->size;
#endif
}
/* special case: REAL(integer|[d]real) -> real */
if((defn->intrins_flags&I_SP_R) &&
(rettype != type_COMPLEX) && (rettype != type_DCOMPLEX)) {
rettype = type_REAL;
retsize = size_DEFAULT;
}
/* special cases: */
/* ABS([d]complex) -> [d]real */
/* IMAG([d]complex) -> [d]real */
/* REAL([d]complex) -> [d]real */
if(rettype == type_COMPLEX && (defn->intrins_flags&I_C_TO_R)) {
rettype = type_REAL;
retsize = retsize/2;
}
if(rettype == type_DCOMPLEX &&(defn->intrins_flags&I_C_TO_R)) {
rettype = type_DP;
retsize = size_DEFAULT;
}
}
else { /* non-generic */
/* special case: CHAR(code): size=1 */
if(defn->intrins_flags&I_CHAR) {
retsize = 1;
}
}
}
else { /* non-intrinsic */
rettype = get_type(symt);
retsize = get_size(symt,rettype);
}
/* referencing function makes it no longer a class_SUBPROGRAM
but an expression. */
#ifndef TOK_type
result->tclass = id->tclass;
#endif
result->tsubclass = 0;
result->TOK_type = type_byte(class_VAR,rettype);
#ifndef TOK_flags
result->TOK_flags = 0; /* clear all flags */
#endif
result->size = retsize;
result->next_token = (Token *)NULL;
#ifdef DEBUG_EXPRTYPE
if(debug_latest) {
(void)fprintf(list_fd,"\n%sFunction %s() = %s",
symt->intrinsic?"Intrinsic ":"",
symt->name,sized_typename(rettype,retsize));
}
#endif
/* If intrinsic and all arguments are PARAMETER_EXPRs,
then result is one too. */
if( symt->intrinsic ) {
/* Evaluate intrinsic if result is
integer, the args are const (except for
LEN), and a handler is defined.
*/
if(rettype == type_INTEGER &&
(defn->intrins_flags&I_EVALUATED) )
{
result->value.integer = eval_intrins(defn,args);
/* Evaluation routines can affect the flags */
copy_flag(EVALUATED_EXPR,result->TOK_flags,args->TOK_flags);
}
copy_flag(PARAMETER_EXPR,result->TOK_flags,args->TOK_flags);
#ifdef DEBUG_EXPRTYPE
if(debug_latest) {
(void)fprintf(list_fd,"\n%s(...) ",defn->name);
if(is_true(EVALUATED_EXPR,args->TOK_flags))
(void)fprintf(list_fd,"=%d",result->value.integer);
else
(void)fprintf(list_fd,"not evaluated");
(void)fprintf(list_fd,": const param eval=(%d %d %d)",
is_true(CONST_EXPR,result->TOK_flags),
is_true(PARAMETER_EXPR,result->TOK_flags),
is_true(EVALUATED_EXPR,result->TOK_flags));
}
#endif
}
}/*func_ref_expr*/
/* Make an expression-token for primary consisting of
a symbolic name */
void
#if HAVE_STDC
primary_id_expr(Token *id, Token *primary)
#else /* K&R style */
primary_id_expr(id,primary)
Token *id,*primary;
#endif /* HAVE_STDC */
{
Lsymtab *symt;
int id_type;
symt = hashtab[id->value.integer].loc_symtab;
id_type=get_type(symt);
#ifndef TOK_type
primary->tclass = id->tclass;
#endif
primary->tsubclass = 0;
primary->TOK_type = type_byte(storage_class_of(symt->type),id_type);
#ifndef TOK_flags
primary->TOK_flags = 0;
#endif
primary->size =get_size(symt,id_type);
primary->left_token = (Token *) NULL;
make_true(ID_EXPR,primary->TOK_flags);
if( storage_class_of(symt->type) == class_VAR) {
if(symt->parameter) {
make_true(CONST_EXPR,primary->TOK_flags);
make_true(PARAMETER_EXPR,primary->TOK_flags);
make_true(EVALUATED_EXPR,primary->TOK_flags);
}
else {
make_true(LVALUE_EXPR,primary->TOK_flags);
}
if(symt->active_do_var) {
make_true(DO_VARIABLE,primary->TOK_flags);
}
if(symt->array_var)
make_true(ARRAY_ID_EXPR,primary->TOK_flags);
if(symt->set_flag || symt->common_var || symt->parameter
|| symt->argument)
make_true(SET_FLAG,primary->TOK_flags);
if(symt->assigned_flag)
make_true(ASSIGNED_FLAG,primary->TOK_flags);
if(symt->used_before_set)
make_true(USED_BEFORE_SET,primary->TOK_flags);
}
else if(storage_class_of(symt->type) == class_STMT_FUNCTION) {
make_true(STMT_FUNCTION_EXPR,primary->TOK_flags);
}
#ifdef DEBUG_PARSER
if(debug_parser){
(void)fprintf(list_fd,"\nprimary %s: TOK_type=0x%x TOK_flags=0x%x",
symt->name,primary->TOK_type,primary->TOK_flags);
}
#endif
}/*primary_id_expr*/
int
#if HAVE_STDC
intrins_arg_cmp(IntrinsInfo *defn, Token *t)
/* Definition */
/* Argument */
#else /* K&R style */
intrins_arg_cmp(defn,t)
IntrinsInfo *defn; /* Definition */
Token *t; /* Argument */
#endif /* HAVE_STDC */
{
int defn_types=defn->arg_type;
int a_type = datatype_of(t->TOK_type);
int type_OK;
/* Check for argument type mismatch.
*/
type_OK = ( (1<<a_type) & defn_types );
if(! type_OK) {
int ct;/* compatible type */
/* Accept compatible types if
sizes agree, e.g. DSQRT(REAL*8).
The macros check the two cases and
set ct to the compatible type.
*/
#define EXCEPTION1 (a_type==type_REAL && ((1<<(ct=type_DP))&defn_types))
#define EXCEPTION2 (a_type==type_COMPLEX&&((1<<(ct=type_DCOMPLEX))&defn_types))
if(!( (EXCEPTION1||EXCEPTION2) && t->size==type_size[ct] )){
syntax_error(t->line_num,t->col_num,
"illegal argument data type for intrinsic function");
msg_tail(defn->name);
msg_tail(":");
report_type(t);
}
else {
if(port_mixed_size || local_wordsize==0) {
nonportable(t->line_num,t->col_num,
"argument precision may not be correct for intrinsic function");
msg_tail(defn->name);
msg_tail(":");
report_type(t);
}
type_OK = TRUE; /* Acceptable after all */
}
}/* end if(! type_OK) */
/* Quad intrinsics need a special check
to verify that real or cplx arg size is right.
*/
else if(defn->intrins_flags & I_QARG) {
if(t->size != ((a_type==type_REAL)? size_QUAD: size_CQUAD)) {
syntax_error(t->line_num,t->col_num,
"illegal argument data type for intrinsic function");
msg_tail(defn->name);
msg_tail(":");
report_type(t);
}
}
return type_OK;
}/*intrins_arg_cmp*/
/* Check agreement between statement function
dummy (t1) and actual (t2) args. At this
time, checks only class, type and size,
not arrayness. */
void
#if HAVE_STDC
stmt_fun_arg_cmp(const Lsymtab *symt, const Token *d_arg, const Token *a_arg)
#else /* K&R style */
stmt_fun_arg_cmp(symt,d_arg,a_arg)
Lsymtab *symt;
Token *d_arg,*a_arg;
#endif /* HAVE_STDC */
{
int d_class = class_VAR,
a_class = storage_class_of(a_arg->TOK_type),
d_type = datatype_of(d_arg->TOK_type),
a_type = datatype_of(a_arg->TOK_type),
d_size = d_arg->size,
a_size = a_arg->size,
d_defsize = (d_size == size_DEFAULT),
a_defsize = (a_size == size_DEFAULT);
int d_cmptype= (d_type==type_HOLLERITH && a_type!=type_STRING)?
a_type:type_category[d_type];
int a_cmptype= (a_type==type_HOLLERITH && d_type!=type_STRING)?
d_type:type_category[a_type];
if(!(port_mixed_size || local_wordsize==0)) {
if(d_defsize)
d_size = type_size[d_type];
if(a_defsize)
a_size = type_size[a_type];
}
if(d_size < 0 || a_size < 0) { /* char size_ADJUSTABLE or UNKNOWN */
d_size = a_size = size_DEFAULT; /* suppress warnings on size */
d_defsize = a_defsize = TRUE; /* these are not used at present */
}
if(d_class != a_class || d_cmptype != a_cmptype ||
(d_type == type_STRING? d_size > a_size: d_size != a_size) ) {
syntax_error(a_arg->line_num,a_arg->col_num,
"argument mismatch in stmt function");
msg_tail(symt->name); /* Give the stmt func name */
msg_tail(": dummy");
report_type(d_arg); /* Dummy arg type */
msg_tail("vs actual");
report_type(a_arg);
}
}/*stmt_fun_arg_cmp*/
/* Routine to document the types of
two terms and their operator */
PRIVATE void
#if HAVE_STDC
report_mismatch(const Token *term1, const Token *op, const Token *term2)
#else /* K&R style */
report_mismatch(term1,op,term2)
Token *term1,*op,*term2;
#endif /* HAVE_STDC */
{
report_type(term1);
msg_tail(op->src_text);
report_type(term2);
}
/* Routine to document the type
of a token, with its name if it
has one. */
PRIVATE void
#if HAVE_STDC
report_type(const Token *t)
#else /* K&R style */
report_type(t)
Token *t;
#endif /* HAVE_STDC */
{
msg_tail(sized_typename((int)datatype_of(t->TOK_type),t->size));
if(is_true(ID_EXPR,t->TOK_flags))
msg_tail(hashtab[t->value.integer].name);
else if(is_true(LIT_CONST,t->TOK_flags)) {
msg_tail("const");
msg_expr_tree(t);
}
else {
msg_tail("expr");
msg_expr_tree(t);
}
}
int
#if HAVE_STDC
substring_size(Token *id, Token *limits)
#else /* K&R style */
substring_size(id,limits)
Token *id,*limits;
#endif /* HAVE_STDC */
{
int id_type,id_len;
int startindex,endindex,substr_len;
#ifdef DEBUG_EXPRTREES
Lsymtab *symt = hashtab[id->value.integer].loc_symtab;
#endif
/*** id_type=get_type(symt); **/
id_type = datatype_of(id->TOK_type);
substr_len=size_UNKNOWN;
if(id_type != type_STRING) {
syntax_error(id->line_num,id->col_num,
"string variable expected");
}
else {
id_len = id->size;
#ifdef DEBUG_EXPRTREES
if(debug_latest) {
fprintf(list_fd,"\nSubstring %s :: ",symt->name);
print_expr_list(limits);
}
#endif
/* fortran.y stores (startindex:endindex) in
TOK_start, Tok_end */
startindex = limits->TOK_start;
endindex = limits->TOK_end;
if(startindex != size_UNKNOWN && endindex != size_UNKNOWN) {
/* Check limits unless endindex=0 */
if( startindex > endindex && endindex > 0 ) {
syntax_error(limits->line_num,limits->col_num,
"invalid substring limits");
}
else {
if(endindex == 0) /* 0 means it was (startindex: ) */
endindex=id_len;
substr_len = endindex-startindex+1;
if(id_len > 0 && substr_len > id_len)
syntax_error(limits->line_num,limits->col_num,
"substring size exceeds string size");
}
}
}
return substr_len;
}
/* Integer power: uses recursion x**n = (x**(n/2))**2 */
PRIVATE int
#if HAVE_STDC
int_power(int x, int n)
#else /* K&R style */
int_power(x,n)
int x,n;
#endif /* HAVE_STDC */
{
int temp;
/* Order of tests puts commonest cases first */
if(n > 1) {
temp = int_power(x,n>>1);
temp *= temp;
if(n&1) return temp*x; /* Odd n */
else return temp; /* Even n */
}
else if(n == 1) return x;
else if(n < 0) return 1/int_power(x,-n); /* Usually 0 */
else return 1;
}
/* Intrinsic function handlers */
PROTO(PRIVATE int ii_abs,( Token *args ));
PROTO(PRIVATE int ii_dim,( Token *args ));
PROTO(PRIVATE int ii_ichar,( Token *args ));
PROTO(PRIVATE int ii_index,( Token *args ));
PROTO(PRIVATE int ii_len,( Token *args ));
PROTO(PRIVATE int ii_max,( Token *args ));
PROTO(PRIVATE int ii_min,( Token *args ));
PROTO(PRIVATE int ii_mod,( Token *args ));
PROTO(PRIVATE int ii_sign,( Token *args ));
/* Array of pointers to functions for evaluating integer-valued intrinsic
functions. The order matches definitions of I_ABS thru I_INDEX in
symtab.h */
PROTO(PRIVATE int (*ii_fun[]),( Token *args ))
={
NULL,
ii_abs,
ii_sign,
ii_dim,
ii_mod,
ii_max,
ii_min,
ii_ichar,
ii_len,
ii_index,
};
PRIVATE int
#if HAVE_STDC
eval_intrins(IntrinsInfo *defn, Token *args)
#else /* K&R style */
eval_intrins(defn,args)
IntrinsInfo *defn;
Token *args;
#endif /* HAVE_STDC */
{
intrins_flags_t fun_num;
fun_num = (defn->intrins_flags & I_EVALUATED);
/* Args must be evaluated, except for LEN */
if( (is_true(EVALUATED_EXPR,args->TOK_flags) || fun_num==I_LEN) &&
fun_num > 0 && fun_num < (sizeof(ii_fun)/sizeof(ii_fun[0])) ) {
return (*ii_fun[fun_num])(args);
}
else {
#ifdef DEBUG_EXPRTYPE
if(debug_latest)
(void)fprintf(list_fd,"\nIntrinsic %s not handled",defn->name);
make_false(EVALUATED_EXPR,args->TOK_flags);
#endif
return 0;
}
}
PRIVATE int
#if HAVE_STDC
ii_abs(Token *args)
#else /* K&R style */
ii_abs(args)
Token *args;
#endif /* HAVE_STDC */
{
Token *t;
int val, result=0;
t = args->next_token;
if(t->TOK_type != type_INTEGER) {/* wrong arg type: message given elsewhere */
make_false(EVALUATED_EXPR,args->TOK_flags);
}
else {
val = int_expr_value(t);
result = (val >= 0? val: -val);
}
return result;
}
PRIVATE int
#if HAVE_STDC
ii_sign(Token *args) /* SIGN(value,sign) */
#else /* K&R style */
ii_sign(args) /* SIGN(value,sign) */
Token *args;
#endif /* HAVE_STDC */
{
Token *t1,*t2;
int val1,val2, result=0;
t1 = args->next_token;
t2 = t1->next_token;
if(t2 == NULL || t1->TOK_type != type_INTEGER
|| t2->TOK_type != type_INTEGER) {/* wrong arg type: message given elswr */
make_false(EVALUATED_EXPR,args->TOK_flags);
}
else {
val1 = int_expr_value(t1);
if(val1 < 0) val1 = -val1;
val2 = int_expr_value(t2);
result = (val2 >= 0? val1: -val1);
}
return result;
}
PRIVATE int
#if HAVE_STDC
ii_dim(Token *args) /* DIM(int,int) */
#else /* K&R style */
ii_dim(args) /* DIM(int,int) */
Token *args;
#endif /* HAVE_STDC */
{
Token *t1,*t2;
int val, result=0;
t1 = args->next_token;
t2 = t1->next_token;
if(t2 == NULL || t1->TOK_type != type_INTEGER
|| t2->TOK_type != type_INTEGER) {/* wrong arg type: message given elswr */
make_false(EVALUATED_EXPR,args->TOK_flags);
}
else {
val = int_expr_value(t1)-int_expr_value(t2);
result = (val >= 0? val: 0);
}
return result;
}
PRIVATE int
#if HAVE_STDC
ii_mod(Token *args) /* MOD(int,int) */
#else /* K&R style */
ii_mod(args) /* MOD(int,int) */
Token *args;
#endif /* HAVE_STDC */
{
Token *t1,*t2;
int val1,val2,quotient, result=0;
t1 = args->next_token;
t2 = t1->next_token;
if(t2 == NULL || t1->TOK_type != type_INTEGER
|| t2->TOK_type != type_INTEGER) {/* wrong arg type: message given elswr */
make_false(EVALUATED_EXPR,args->TOK_flags);
}
else {
val1 = int_expr_value(t1);
val2 = int_expr_value(t2);
if((val1 < 0) == (val2 < 0)) {
quotient = val1/val2; /* Both positive or both negative*/
}
else {
quotient = -(-val1/val2); /* Unlike signs */
}
result = val1 - quotient*val2;
}
return result;
}
PRIVATE int
#if HAVE_STDC
ii_max(Token *args) /* MAX(int,int,...) */
#else /* K&R style */
ii_max(args) /* MAX(int,int,...) */
Token *args;
#endif /* HAVE_STDC */
{
Token *t=args;
int val,result=0,n=0;
#ifdef DEBUG_EXPRTYPE
if(debug_latest)
(void)fprintf(list_fd,"\nEvaluating MAX(");
#endif
while( (t=t->next_token) != NULL) {
if(t->TOK_type != type_INTEGER) {/* wrong arg type: message given elswr */
make_false(EVALUATED_EXPR,args->TOK_flags);
break;
}
else {
val = int_expr_value(t);
if(n++ == 0 || val > result)
result = val;
#ifdef DEBUG_EXPRTYPE
if(debug_latest)
(void)fprintf(list_fd,"%d ",val);
#endif
}
}
#ifdef DEBUG_EXPRTYPE
if(debug_latest)
(void)fprintf(list_fd,") = %d",result);
#endif
return result;
}
PRIVATE int
#if HAVE_STDC
ii_min(Token *args) /* MIN(int,int,...) */
#else /* K&R style */
ii_min(args) /* MIN(int,int,...) */
Token *args;
#endif /* HAVE_STDC */
{
Token *t=args;
int val,result=0,n=0;
while( (t=t->next_token) != NULL) {
if(t->TOK_type != type_INTEGER) {/* wrong arg type: message given elswr */
make_false(EVALUATED_EXPR,args->TOK_flags);
break;
}
else {
val = int_expr_value(t);
if(n++ == 0 || val < result)
result = val;
}
}
return result;
}
PRIVATE int
#if HAVE_STDC
ii_ichar(Token *args) /* ICHAR(string) */
#else /* K&R style */
ii_ichar(args) /* ICHAR(string) */
Token *args;
#endif /* HAVE_STDC */
{
Token *t=args->next_token;
if(t->TOK_type != type_STRING || !is_true(LIT_CONST,t->TOK_flags)) {
make_false(EVALUATED_EXPR,args->TOK_flags);
}
else {
return t->value.string[0]; /* Processor collating sequence is used */
}
return 0;
}
PRIVATE int
#if HAVE_STDC
ii_len(Token *args) /* LEN(string) */
#else /* K&R style */
ii_len(args) /* LEN(string) */
Token *args;
#endif /* HAVE_STDC */
{
Token *t=args->next_token;
int val,result=0;
/* Set the PARAMETER_EXPR flag since LEN of string does
not require contents to be known */
if( t->TOK_type == type_STRING && (val = t->size) > 0 ) {
make_true(PARAMETER_EXPR,args->TOK_flags);
make_true(EVALUATED_EXPR,args->TOK_flags);
result = val;
}
else { /* nonstring or adjustable or unknown */
make_false(PARAMETER_EXPR,args->TOK_flags);
make_false(EVALUATED_EXPR,args->TOK_flags);
}
return result;
}
PRIVATE int
#if HAVE_STDC
ii_index(Token *args) /* INDEX(str1,str2) */
#else /* K&R style */
ii_index(args) /* INDEX(str1,str2) */
Token *args;
#endif /* HAVE_STDC */
{
Token *t1,*t2;
t1=args->next_token;
t2=t1->next_token;
if(t2 == NULL || t1->TOK_type != type_STRING
|| t2->TOK_type != type_STRING
|| !is_true(LIT_CONST,t1->TOK_flags) || !is_true(LIT_CONST,t2->TOK_flags)) {
make_false(EVALUATED_EXPR,args->TOK_flags);
}
else {
int i;
char *s1=t1->value.string;
char *s2=t2->value.string;
int n1=strlen(s1), n2=strlen(s2);
for(i=1; n1 > 0 && n1 >= n2; i++,s1++,n1--) {
if(strncmp(s1,s2,n2) == 0)
return i;
}
}
return 0;
}
/* Undefine special macros */
#undef E
#undef I
#undef R
#undef D
#undef C
#undef L
#undef S
#undef H
#undef W
|