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 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899
|
/* Deal with I/O statements & related stuff.
Copyright (C) 2000-2022 Free Software Foundation, Inc.
Contributed by Andy Vaught
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "options.h"
#include "gfortran.h"
#include "match.h"
#include "parse.h"
#include "constructor.h"
gfc_st_label
format_asterisk = {0, NULL, NULL, -1, ST_LABEL_FORMAT, ST_LABEL_FORMAT, NULL,
0, {NULL, NULL}, NULL};
typedef struct
{
const char *name, *spec, *value;
bt type;
}
io_tag;
static const io_tag
tag_readonly = {"READONLY", " readonly", NULL, BT_UNKNOWN },
tag_shared = {"SHARE", " shared", NULL, BT_UNKNOWN },
tag_noshared = {"SHARE", " noshared", NULL, BT_UNKNOWN },
tag_e_share = {"SHARE", " share =", " %e", BT_CHARACTER },
tag_v_share = {"SHARE", " share =", " %v", BT_CHARACTER },
tag_cc = {"CARRIAGECONTROL", " carriagecontrol =", " %e",
BT_CHARACTER },
tag_v_cc = {"CARRIAGECONTROL", " carriagecontrol =", " %v",
BT_CHARACTER },
tag_file = {"FILE", " file =", " %e", BT_CHARACTER },
tag_status = {"STATUS", " status =", " %e", BT_CHARACTER},
tag_e_access = {"ACCESS", " access =", " %e", BT_CHARACTER},
tag_e_form = {"FORM", " form =", " %e", BT_CHARACTER},
tag_e_recl = {"RECL", " recl =", " %e", BT_INTEGER},
tag_e_blank = {"BLANK", " blank =", " %e", BT_CHARACTER},
tag_e_position = {"POSITION", " position =", " %e", BT_CHARACTER},
tag_e_action = {"ACTION", " action =", " %e", BT_CHARACTER},
tag_e_delim = {"DELIM", " delim =", " %e", BT_CHARACTER},
tag_e_pad = {"PAD", " pad =", " %e", BT_CHARACTER},
tag_e_decimal = {"DECIMAL", " decimal =", " %e", BT_CHARACTER},
tag_e_encoding = {"ENCODING", " encoding =", " %e", BT_CHARACTER},
tag_e_async = {"ASYNCHRONOUS", " asynchronous =", " %e", BT_CHARACTER},
tag_e_round = {"ROUND", " round =", " %e", BT_CHARACTER},
tag_e_sign = {"SIGN", " sign =", " %e", BT_CHARACTER},
tag_unit = {"UNIT", " unit =", " %e", BT_INTEGER},
tag_advance = {"ADVANCE", " advance =", " %e", BT_CHARACTER},
tag_rec = {"REC", " rec =", " %e", BT_INTEGER},
tag_spos = {"POSITION", " pos =", " %e", BT_INTEGER},
tag_format = {"FORMAT", NULL, NULL, BT_CHARACTER},
tag_iomsg = {"IOMSG", " iomsg =", " %e", BT_CHARACTER},
tag_iostat = {"IOSTAT", " iostat =", " %v", BT_INTEGER},
tag_size = {"SIZE", " size =", " %v", BT_INTEGER},
tag_exist = {"EXIST", " exist =", " %v", BT_LOGICAL},
tag_opened = {"OPENED", " opened =", " %v", BT_LOGICAL},
tag_named = {"NAMED", " named =", " %v", BT_LOGICAL},
tag_name = {"NAME", " name =", " %v", BT_CHARACTER},
tag_number = {"NUMBER", " number =", " %v", BT_INTEGER},
tag_s_access = {"ACCESS", " access =", " %v", BT_CHARACTER},
tag_sequential = {"SEQUENTIAL", " sequential =", " %v", BT_CHARACTER},
tag_direct = {"DIRECT", " direct =", " %v", BT_CHARACTER},
tag_s_form = {"FORM", " form =", " %v", BT_CHARACTER},
tag_formatted = {"FORMATTED", " formatted =", " %v", BT_CHARACTER},
tag_unformatted = {"UNFORMATTED", " unformatted =", " %v", BT_CHARACTER},
tag_s_recl = {"RECL", " recl =", " %v", BT_INTEGER},
tag_nextrec = {"NEXTREC", " nextrec =", " %v", BT_INTEGER},
tag_s_blank = {"BLANK", " blank =", " %v", BT_CHARACTER},
tag_s_position = {"POSITION", " position =", " %v", BT_CHARACTER},
tag_s_action = {"ACTION", " action =", " %v", BT_CHARACTER},
tag_read = {"READ", " read =", " %v", BT_CHARACTER},
tag_write = {"WRITE", " write =", " %v", BT_CHARACTER},
tag_readwrite = {"READWRITE", " readwrite =", " %v", BT_CHARACTER},
tag_s_delim = {"DELIM", " delim =", " %v", BT_CHARACTER},
tag_s_pad = {"PAD", " pad =", " %v", BT_CHARACTER},
tag_s_decimal = {"DECIMAL", " decimal =", " %v", BT_CHARACTER},
tag_s_encoding = {"ENCODING", " encoding =", " %v", BT_CHARACTER},
tag_s_async = {"ASYNCHRONOUS", " asynchronous =", " %v", BT_CHARACTER},
tag_s_round = {"ROUND", " round =", " %v", BT_CHARACTER},
tag_s_sign = {"SIGN", " sign =", " %v", BT_CHARACTER},
tag_iolength = {"IOLENGTH", " iolength =", " %v", BT_INTEGER},
tag_convert = {"CONVERT", " convert =", " %e", BT_CHARACTER},
tag_strm_out = {"POS", " pos =", " %v", BT_INTEGER},
tag_err = {"ERR", " err =", " %l", BT_UNKNOWN},
tag_end = {"END", " end =", " %l", BT_UNKNOWN},
tag_eor = {"EOR", " eor =", " %l", BT_UNKNOWN},
tag_id = {"ID", " id =", " %v", BT_INTEGER},
tag_pending = {"PENDING", " pending =", " %v", BT_LOGICAL},
tag_newunit = {"NEWUNIT", " newunit =", " %v", BT_INTEGER},
tag_s_iqstream = {"STREAM", " stream =", " %v", BT_CHARACTER};
static gfc_dt *current_dt;
#define RESOLVE_TAG(x, y) if (!resolve_tag (x, y)) return false;
/**************** Fortran 95 FORMAT parser *****************/
/* FORMAT tokens returned by format_lex(). */
enum format_token
{
FMT_NONE, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_LPAREN,
FMT_RPAREN, FMT_X, FMT_SIGN, FMT_BLANK, FMT_CHAR, FMT_P, FMT_IBOZ, FMT_F,
FMT_E, FMT_EN, FMT_ES, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END,
FMT_ERROR, FMT_DC, FMT_DP, FMT_T, FMT_TR, FMT_TL, FMT_STAR, FMT_RC,
FMT_RD, FMT_RN, FMT_RP, FMT_RU, FMT_RZ, FMT_DT
};
/* Local variables for checking format strings. The saved_token is
used to back up by a single format token during the parsing
process. */
static gfc_char_t *format_string;
static int format_string_pos;
static int format_length, use_last_char;
static char error_element;
static locus format_locus;
static format_token saved_token;
static enum
{ MODE_STRING, MODE_FORMAT, MODE_COPY }
mode;
/* Return the next character in the format string. */
static char
next_char (gfc_instring in_string)
{
static gfc_char_t c;
if (use_last_char)
{
use_last_char = 0;
return c;
}
format_length++;
if (mode == MODE_STRING)
c = *format_string++;
else
{
c = gfc_next_char_literal (in_string);
if (c == '\n')
c = '\0';
}
if (flag_backslash && c == '\\')
{
locus old_locus = gfc_current_locus;
if (gfc_match_special_char (&c) == MATCH_NO)
gfc_current_locus = old_locus;
if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
gfc_warning (0, "Extension: backslash character at %C");
}
if (mode == MODE_COPY)
*format_string++ = c;
if (mode != MODE_STRING)
format_locus = gfc_current_locus;
format_string_pos++;
c = gfc_wide_toupper (c);
return c;
}
/* Back up one character position. Only works once. */
static void
unget_char (void)
{
use_last_char = 1;
}
/* Eat up the spaces and return a character. */
static char
next_char_not_space ()
{
char c;
do
{
error_element = c = next_char (NONSTRING);
if (c == '\t')
gfc_warning (OPT_Wtabs, "Nonconforming tab character in format at %C");
}
while (gfc_is_whitespace (c));
return c;
}
static int value = 0;
/* Simple lexical analyzer for getting the next token in a FORMAT
statement. */
static format_token
format_lex (void)
{
format_token token;
char c, delim;
int zflag;
int negative_flag;
if (saved_token != FMT_NONE)
{
token = saved_token;
saved_token = FMT_NONE;
return token;
}
c = next_char_not_space ();
negative_flag = 0;
switch (c)
{
case '-':
negative_flag = 1;
/* Falls through. */
case '+':
c = next_char_not_space ();
if (!ISDIGIT (c))
{
token = FMT_UNKNOWN;
break;
}
value = c - '0';
do
{
c = next_char_not_space ();
if (ISDIGIT (c))
value = 10 * value + c - '0';
}
while (ISDIGIT (c));
unget_char ();
if (negative_flag)
value = -value;
token = FMT_SIGNED_INT;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
zflag = (c == '0');
value = c - '0';
do
{
c = next_char_not_space ();
if (ISDIGIT (c))
{
value = 10 * value + c - '0';
if (c != '0')
zflag = 0;
}
}
while (ISDIGIT (c));
unget_char ();
token = zflag ? FMT_ZERO : FMT_POSINT;
break;
case '.':
token = FMT_PERIOD;
break;
case ',':
token = FMT_COMMA;
break;
case ':':
token = FMT_COLON;
break;
case '/':
token = FMT_SLASH;
break;
case '$':
token = FMT_DOLLAR;
break;
case 'T':
c = next_char_not_space ();
switch (c)
{
case 'L':
token = FMT_TL;
break;
case 'R':
token = FMT_TR;
break;
default:
token = FMT_T;
unget_char ();
}
break;
case '(':
token = FMT_LPAREN;
break;
case ')':
token = FMT_RPAREN;
break;
case 'X':
token = FMT_X;
break;
case 'S':
c = next_char_not_space ();
if (c != 'P' && c != 'S')
unget_char ();
token = FMT_SIGN;
break;
case 'B':
c = next_char_not_space ();
if (c == 'N' || c == 'Z')
token = FMT_BLANK;
else
{
unget_char ();
token = FMT_IBOZ;
}
break;
case '\'':
case '"':
delim = c;
value = 0;
for (;;)
{
c = next_char (INSTRING_WARN);
if (c == '\0')
{
token = FMT_END;
break;
}
if (c == delim)
{
c = next_char (NONSTRING);
if (c == '\0')
{
token = FMT_END;
break;
}
if (c != delim)
{
unget_char ();
token = FMT_CHAR;
break;
}
}
value++;
}
break;
case 'P':
token = FMT_P;
break;
case 'I':
case 'O':
case 'Z':
token = FMT_IBOZ;
break;
case 'F':
token = FMT_F;
break;
case 'E':
c = next_char_not_space ();
if (c == 'N' )
token = FMT_EN;
else if (c == 'S')
token = FMT_ES;
else
{
token = FMT_E;
unget_char ();
}
break;
case 'G':
token = FMT_G;
break;
case 'H':
token = FMT_H;
break;
case 'L':
token = FMT_L;
break;
case 'A':
token = FMT_A;
break;
case 'D':
c = next_char_not_space ();
if (c == 'P')
{
if (!gfc_notify_std (GFC_STD_F2003, "DP format "
"specifier not allowed at %C"))
return FMT_ERROR;
token = FMT_DP;
}
else if (c == 'C')
{
if (!gfc_notify_std (GFC_STD_F2003, "DC format "
"specifier not allowed at %C"))
return FMT_ERROR;
token = FMT_DC;
}
else if (c == 'T')
{
if (!gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DT format "
"specifier not allowed at %C"))
return FMT_ERROR;
token = FMT_DT;
c = next_char_not_space ();
if (c == '\'' || c == '"')
{
delim = c;
value = 0;
for (;;)
{
c = next_char (INSTRING_WARN);
if (c == '\0')
{
token = FMT_END;
break;
}
if (c == delim)
{
c = next_char (NONSTRING);
if (c == '\0')
{
token = FMT_END;
break;
}
if (c == '/')
{
token = FMT_SLASH;
break;
}
if (c == delim)
continue;
unget_char ();
break;
}
}
}
else if (c == '/')
{
token = FMT_SLASH;
break;
}
else
unget_char ();
}
else
{
token = FMT_D;
unget_char ();
}
break;
case 'R':
c = next_char_not_space ();
switch (c)
{
case 'C':
token = FMT_RC;
break;
case 'D':
token = FMT_RD;
break;
case 'N':
token = FMT_RN;
break;
case 'P':
token = FMT_RP;
break;
case 'U':
token = FMT_RU;
break;
case 'Z':
token = FMT_RZ;
break;
default:
token = FMT_UNKNOWN;
unget_char ();
break;
}
break;
case '\0':
token = FMT_END;
break;
case '*':
token = FMT_STAR;
break;
default:
token = FMT_UNKNOWN;
break;
}
return token;
}
static const char *
token_to_string (format_token t)
{
switch (t)
{
case FMT_D:
return "D";
case FMT_G:
return "G";
case FMT_E:
return "E";
case FMT_EN:
return "EN";
case FMT_ES:
return "ES";
default:
return "";
}
}
/* Check a format statement. The format string, either from a FORMAT
statement or a constant in an I/O statement has already been parsed
by itself, and we are checking it for validity. The dual origin
means that the warning message is a little less than great. */
static bool
check_format (bool is_input)
{
const char *posint_required
= G_("Positive width required in format string at %L");
const char *nonneg_required
= G_("Nonnegative width required in format string at %L");
const char *unexpected_element
= G_("Unexpected element %qc in format string at %L");
const char *unexpected_end
= G_("Unexpected end of format string in format string at %L");
const char *zero_width
= G_("Zero width in format descriptor in format string at %L");
const char *error = NULL;
format_token t, u;
int level;
int repeat;
bool rv;
use_last_char = 0;
saved_token = FMT_NONE;
level = 0;
repeat = 0;
rv = true;
format_string_pos = 0;
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_LPAREN)
{
error = G_("Missing leading left parenthesis in format string at %L");
goto syntax;
}
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t == FMT_RPAREN)
goto finished; /* Empty format is legal */
saved_token = t;
format_item:
/* In this state, the next thing has to be a format item. */
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
format_item_1:
switch (t)
{
case FMT_STAR:
repeat = -1;
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t == FMT_LPAREN)
{
level++;
goto format_item;
}
error = G_("Left parenthesis required after %<*%> in format string "
"at %L");
goto syntax;
case FMT_POSINT:
repeat = value;
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t == FMT_LPAREN)
{
level++;
goto format_item;
}
if (t == FMT_SLASH)
goto optional_comma;
goto data_desc;
case FMT_LPAREN:
level++;
goto format_item;
case FMT_SIGNED_INT:
case FMT_ZERO:
/* Signed integer can only precede a P format. */
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_P)
{
error = G_("Expected P edit descriptor in format string at %L");
goto syntax;
}
goto data_desc;
case FMT_P:
/* P requires a prior number. */
error = G_("P descriptor requires leading scale factor in format "
"string at %L");
goto syntax;
case FMT_X:
/* X requires a prior number if we're being pedantic. */
if (mode != MODE_FORMAT)
format_locus.nextc += format_string_pos;
if (!gfc_notify_std (GFC_STD_GNU, "X descriptor requires leading "
"space count at %L", &format_locus))
return false;
goto between_desc;
case FMT_SIGN:
case FMT_BLANK:
case FMT_DP:
case FMT_DC:
case FMT_RC:
case FMT_RD:
case FMT_RN:
case FMT_RP:
case FMT_RU:
case FMT_RZ:
goto between_desc;
case FMT_CHAR:
goto extension_optional_comma;
case FMT_COLON:
case FMT_SLASH:
goto optional_comma;
case FMT_DOLLAR:
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (!gfc_notify_std (GFC_STD_GNU, "$ descriptor at %L", &format_locus))
return false;
if (t != FMT_RPAREN || level > 0)
{
gfc_warning (0, "$ should be the last specifier in format at %L",
&format_locus);
goto optional_comma_1;
}
goto finished;
case FMT_T:
case FMT_TL:
case FMT_TR:
case FMT_IBOZ:
case FMT_F:
case FMT_E:
case FMT_EN:
case FMT_ES:
case FMT_G:
case FMT_L:
case FMT_A:
case FMT_D:
case FMT_H:
case FMT_DT:
goto data_desc;
case FMT_END:
error = unexpected_end;
goto syntax;
case FMT_RPAREN:
if (flag_dec_blank_format_item)
goto finished;
else
{
error = G_("Missing item in format string at %L");
goto syntax;
}
default:
error = unexpected_element;
goto syntax;
}
data_desc:
/* In this state, t must currently be a data descriptor.
Deal with things that can/must follow the descriptor. */
switch (t)
{
case FMT_SIGN:
case FMT_BLANK:
case FMT_DP:
case FMT_DC:
case FMT_X:
break;
case FMT_P:
/* No comma after P allowed only for F, E, EN, ES, D, or G.
10.1.1 (1). */
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (!(gfc_option.allow_std & GFC_STD_F2003) && t != FMT_COMMA
&& t != FMT_F && t != FMT_E && t != FMT_EN && t != FMT_ES
&& t != FMT_D && t != FMT_G && t != FMT_RPAREN && t != FMT_SLASH)
{
error = G_("Comma required after P descriptor in format string "
"at %L");
goto syntax;
}
if (t != FMT_COMMA)
{
if (t == FMT_POSINT)
{
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
}
if (t != FMT_F && t != FMT_E && t != FMT_EN && t != FMT_ES
&& t != FMT_D && t != FMT_G && t != FMT_RPAREN && t != FMT_SLASH)
{
error = G_("Comma required after P descriptor in format string "
"at %L");
goto syntax;
}
}
saved_token = t;
goto optional_comma;
case FMT_T:
case FMT_TL:
case FMT_TR:
t = format_lex ();
if (t != FMT_POSINT)
{
error = G_("Positive width required with T descriptor in format "
"string at %L");
goto syntax;
}
break;
case FMT_L:
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t == FMT_POSINT)
break;
if (mode != MODE_FORMAT)
format_locus.nextc += format_string_pos;
if (t == FMT_ZERO)
{
switch (gfc_notification_std (GFC_STD_GNU))
{
case WARNING:
gfc_warning (0, "Extension: Zero width after L "
"descriptor at %L", &format_locus);
break;
case ERROR:
gfc_error ("Extension: Zero width after L "
"descriptor at %L", &format_locus);
goto fail;
case SILENT:
break;
default:
gcc_unreachable ();
}
}
else
{
saved_token = t;
gfc_notify_std (GFC_STD_GNU, "Missing positive width after "
"L descriptor at %L", &format_locus);
}
break;
case FMT_A:
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t == FMT_ZERO)
{
error = zero_width;
goto syntax;
}
if (t != FMT_POSINT)
saved_token = t;
break;
case FMT_D:
case FMT_E:
case FMT_G:
case FMT_EN:
case FMT_ES:
u = format_lex ();
if (t == FMT_G && u == FMT_ZERO)
{
if (is_input)
{
error = zero_width;
goto syntax;
}
if (!gfc_notify_std (GFC_STD_F2008, "%<G0%> in format at %L",
&format_locus))
return false;
u = format_lex ();
if (u != FMT_PERIOD)
{
saved_token = u;
break;
}
u = format_lex ();
if (u != FMT_POSINT)
{
error = posint_required;
goto syntax;
}
u = format_lex ();
if (u == FMT_E)
{
error = G_("E specifier not allowed with g0 descriptor in "
"format string at %L");
goto syntax;
}
saved_token = u;
break;
}
if (u != FMT_POSINT)
{
if (flag_dec)
{
if (flag_dec_format_defaults)
{
/* Assume a default width based on the variable size. */
saved_token = u;
break;
}
else
{
gfc_error ("Positive width required in format "
"specifier %s at %L", token_to_string (t),
&format_locus);
saved_token = u;
goto fail;
}
}
format_locus.nextc += format_string_pos;
if (!gfc_notify_std (GFC_STD_F2018,
"positive width required at %L",
&format_locus))
{
saved_token = u;
goto fail;
}
if (flag_dec_format_defaults)
{
/* Assume a default width based on the variable size. */
saved_token = u;
break;
}
}
u = format_lex ();
if (u == FMT_ERROR)
goto fail;
if (u != FMT_PERIOD)
{
/* Warn if -std=legacy, otherwise error. */
format_locus.nextc += format_string_pos;
if (gfc_option.warn_std != 0)
{
gfc_error ("Period required in format "
"specifier %s at %L", token_to_string (t),
&format_locus);
saved_token = u;
goto fail;
}
else
gfc_warning (0, "Period required in format "
"specifier %s at %L", token_to_string (t),
&format_locus);
/* If we go to finished, we need to unwind this
before the next round. */
format_locus.nextc -= format_string_pos;
saved_token = u;
break;
}
u = format_lex ();
if (u == FMT_ERROR)
goto fail;
if (u != FMT_ZERO && u != FMT_POSINT)
{
error = nonneg_required;
goto syntax;
}
if (t == FMT_D)
break;
/* Look for optional exponent. */
u = format_lex ();
if (u == FMT_ERROR)
goto fail;
if (u != FMT_E)
saved_token = u;
else
{
u = format_lex ();
if (u == FMT_ERROR)
goto fail;
if (u != FMT_POSINT)
{
if (u == FMT_ZERO)
{
if (!gfc_notify_std (GFC_STD_F2018,
"Positive exponent width required in "
"format string at %L", &format_locus))
{
saved_token = u;
goto fail;
}
}
else
{
error = G_("Positive exponent width required in format "
"string at %L");
goto syntax;
}
}
}
break;
case FMT_DT:
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
switch (t)
{
case FMT_RPAREN:
level--;
if (level < 0)
goto finished;
goto between_desc;
case FMT_COMMA:
goto format_item;
case FMT_COLON:
goto format_item_1;
case FMT_LPAREN:
dtio_vlist:
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_POSINT)
{
error = posint_required;
goto syntax;
}
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t == FMT_COMMA)
goto dtio_vlist;
if (t != FMT_RPAREN)
{
error = G_("Right parenthesis expected at %C in format string "
"at %L");
goto syntax;
}
goto between_desc;
default:
error = unexpected_element;
goto syntax;
}
break;
case FMT_F:
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_ZERO && t != FMT_POSINT)
{
if (flag_dec_format_defaults)
{
/* Assume the default width is expected here and continue lexing. */
value = 0; /* It doesn't matter what we set the value to here. */
saved_token = t;
break;
}
error = nonneg_required;
goto syntax;
}
else if (is_input && t == FMT_ZERO)
{
error = posint_required;
goto syntax;
}
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_PERIOD)
{
/* Warn if -std=legacy, otherwise error. */
if (gfc_option.warn_std != 0)
{
error = G_("Period required in format specifier in format "
"string at %L");
goto syntax;
}
if (mode != MODE_FORMAT)
format_locus.nextc += format_string_pos;
gfc_warning (0, "Period required in format specifier at %L",
&format_locus);
saved_token = t;
break;
}
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_ZERO && t != FMT_POSINT)
{
error = nonneg_required;
goto syntax;
}
break;
case FMT_H:
if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
{
if (mode != MODE_FORMAT)
format_locus.nextc += format_string_pos;
gfc_warning (0, "The H format specifier at %L is"
" a Fortran 95 deleted feature", &format_locus);
}
if (mode == MODE_STRING)
{
format_string += value;
format_length -= value;
format_string_pos += repeat;
}
else
{
while (repeat >0)
{
next_char (INSTRING_WARN);
repeat -- ;
}
}
break;
case FMT_IBOZ:
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_ZERO && t != FMT_POSINT)
{
if (flag_dec_format_defaults)
{
/* Assume the default width is expected here and continue lexing. */
value = 0; /* It doesn't matter what we set the value to here. */
saved_token = t;
}
else
{
error = nonneg_required;
goto syntax;
}
}
else if (is_input && t == FMT_ZERO)
{
error = posint_required;
goto syntax;
}
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_PERIOD)
saved_token = t;
else
{
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_ZERO && t != FMT_POSINT)
{
error = nonneg_required;
goto syntax;
}
}
break;
default:
error = unexpected_element;
goto syntax;
}
between_desc:
/* Between a descriptor and what comes next. */
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
switch (t)
{
case FMT_COMMA:
goto format_item;
case FMT_RPAREN:
level--;
if (level < 0)
goto finished;
goto between_desc;
case FMT_COLON:
case FMT_SLASH:
goto optional_comma;
case FMT_END:
error = unexpected_end;
goto syntax;
default:
if (mode != MODE_FORMAT)
format_locus.nextc += format_string_pos - 1;
if (!gfc_notify_std (GFC_STD_GNU, "Missing comma at %L", &format_locus))
return false;
/* If we do not actually return a failure, we need to unwind this
before the next round. */
if (mode != MODE_FORMAT)
format_locus.nextc -= format_string_pos;
goto format_item_1;
}
optional_comma:
/* Optional comma is a weird between state where we've just finished
reading a colon, slash, dollar or P descriptor. */
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
optional_comma_1:
switch (t)
{
case FMT_COMMA:
break;
case FMT_RPAREN:
level--;
if (level < 0)
goto finished;
goto between_desc;
default:
/* Assume that we have another format item. */
saved_token = t;
break;
}
goto format_item;
extension_optional_comma:
/* As a GNU extension, permit a missing comma after a string literal. */
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
switch (t)
{
case FMT_COMMA:
break;
case FMT_RPAREN:
level--;
if (level < 0)
goto finished;
goto between_desc;
case FMT_COLON:
case FMT_SLASH:
goto optional_comma;
case FMT_END:
error = unexpected_end;
goto syntax;
default:
if (mode != MODE_FORMAT)
format_locus.nextc += format_string_pos;
if (!gfc_notify_std (GFC_STD_GNU, "Missing comma at %L", &format_locus))
return false;
/* If we do not actually return a failure, we need to unwind this
before the next round. */
if (mode != MODE_FORMAT)
format_locus.nextc -= format_string_pos;
saved_token = t;
break;
}
goto format_item;
syntax:
if (mode != MODE_FORMAT)
format_locus.nextc += format_string_pos;
if (error == unexpected_element)
gfc_error (error, error_element, &format_locus);
else
gfc_error (error, &format_locus);
fail:
rv = false;
finished:
return rv;
}
/* Given an expression node that is a constant string, see if it looks
like a format string. */
static bool
check_format_string (gfc_expr *e, bool is_input)
{
bool rv;
int i;
if (!e || e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
return true;
mode = MODE_STRING;
format_string = e->value.character.string;
/* More elaborate measures are needed to show where a problem is within a
format string that has been calculated, but that's probably not worth the
effort. */
format_locus = e->where;
rv = check_format (is_input);
/* check for extraneous characters at the end of an otherwise valid format
string, like '(A10,I3)F5'
start at the end and move back to the last character processed,
spaces are OK */
if (rv && e->value.character.length > format_string_pos)
for (i=e->value.character.length-1;i>format_string_pos-1;i--)
if (e->value.character.string[i] != ' ')
{
format_locus.nextc += format_length + 1;
gfc_warning (0,
"Extraneous characters in format at %L", &format_locus);
break;
}
return rv;
}
/************ Fortran I/O statement matchers *************/
/* Match a FORMAT statement. This amounts to actually parsing the
format descriptors in order to correctly locate the end of the
format string. */
match
gfc_match_format (void)
{
gfc_expr *e;
locus start;
if (gfc_current_ns->proc_name
&& gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
{
gfc_error ("Format statement in module main block at %C");
return MATCH_ERROR;
}
/* Before parsing the rest of a FORMAT statement, check F2008:c1206. */
if ((gfc_current_state () == COMP_FUNCTION
|| gfc_current_state () == COMP_SUBROUTINE)
&& gfc_state_stack->previous->state == COMP_INTERFACE)
{
gfc_error ("FORMAT statement at %C cannot appear within an INTERFACE");
return MATCH_ERROR;
}
if (gfc_statement_label == NULL)
{
gfc_error ("Missing format label at %C");
return MATCH_ERROR;
}
gfc_gobble_whitespace ();
mode = MODE_FORMAT;
format_length = 0;
start = gfc_current_locus;
if (!check_format (false))
return MATCH_ERROR;
if (gfc_match_eos () != MATCH_YES)
{
gfc_syntax_error (ST_FORMAT);
return MATCH_ERROR;
}
/* The label doesn't get created until after the statement is done
being matched, so we have to leave the string for later. */
gfc_current_locus = start; /* Back to the beginning */
new_st.loc = start;
new_st.op = EXEC_NOP;
e = gfc_get_character_expr (gfc_default_character_kind, &start,
NULL, format_length);
format_string = e->value.character.string;
gfc_statement_label->format = e;
mode = MODE_COPY;
check_format (false); /* Guaranteed to succeed */
gfc_match_eos (); /* Guaranteed to succeed */
return MATCH_YES;
}
/* Match an expression I/O tag of some sort. */
static match
match_etag (const io_tag *tag, gfc_expr **v)
{
gfc_expr *result;
match m;
m = gfc_match (tag->spec);
if (m != MATCH_YES)
return m;
m = gfc_match (tag->value, &result);
if (m != MATCH_YES)
{
gfc_error ("Invalid value for %s specification at %C", tag->name);
return MATCH_ERROR;
}
if (*v != NULL)
{
gfc_error ("Duplicate %s specification at %C", tag->name);
gfc_free_expr (result);
return MATCH_ERROR;
}
*v = result;
return MATCH_YES;
}
/* Match a variable I/O tag of some sort. */
static match
match_vtag (const io_tag *tag, gfc_expr **v)
{
gfc_expr *result;
match m;
m = gfc_match (tag->spec);
if (m != MATCH_YES)
return m;
m = gfc_match (tag->value, &result);
if (m != MATCH_YES)
{
gfc_error ("Invalid value for %s specification at %C", tag->name);
return MATCH_ERROR;
}
if (*v != NULL)
{
gfc_error ("Duplicate %s specification at %C", tag->name);
gfc_free_expr (result);
return MATCH_ERROR;
}
if (result->symtree)
{
bool impure;
if (result->symtree->n.sym->attr.intent == INTENT_IN)
{
gfc_error ("Variable %s cannot be INTENT(IN) at %C", tag->name);
gfc_free_expr (result);
return MATCH_ERROR;
}
impure = gfc_impure_variable (result->symtree->n.sym);
if (impure && gfc_pure (NULL))
{
gfc_error ("Variable %s cannot be assigned in PURE procedure at %C",
tag->name);
gfc_free_expr (result);
return MATCH_ERROR;
}
if (impure)
gfc_unset_implicit_pure (NULL);
}
*v = result;
return MATCH_YES;
}
/* Match I/O tags that cause variables to become redefined. */
static match
match_out_tag (const io_tag *tag, gfc_expr **result)
{
match m;
m = match_vtag (tag, result);
if (m == MATCH_YES)
{
if ((*result)->symtree)
gfc_check_do_variable ((*result)->symtree);
if ((*result)->expr_type == EXPR_CONSTANT)
{
gfc_error ("Expecting a variable at %L", &(*result)->where);
return MATCH_ERROR;
}
}
return m;
}
/* Match a label I/O tag. */
static match
match_ltag (const io_tag *tag, gfc_st_label ** label)
{
match m;
gfc_st_label *old;
old = *label;
m = gfc_match (tag->spec);
if (m != MATCH_YES)
return m;
m = gfc_match (tag->value, label);
if (m != MATCH_YES)
{
gfc_error ("Invalid value for %s specification at %C", tag->name);
return MATCH_ERROR;
}
if (old)
{
gfc_error ("Duplicate %s label specification at %C", tag->name);
return MATCH_ERROR;
}
if (!gfc_reference_st_label (*label, ST_LABEL_TARGET))
return MATCH_ERROR;
return m;
}
/* Match a tag using match_etag, but only if -fdec is enabled. */
static match
match_dec_etag (const io_tag *tag, gfc_expr **e)
{
match m = match_etag (tag, e);
if (flag_dec && m != MATCH_NO)
return m;
else if (m != MATCH_NO)
{
gfc_error ("%s at %C is a DEC extension, enable with "
"%<-fdec%>", tag->name);
return MATCH_ERROR;
}
return m;
}
/* Match a tag using match_vtag, but only if -fdec is enabled. */
static match
match_dec_vtag (const io_tag *tag, gfc_expr **e)
{
match m = match_vtag(tag, e);
if (flag_dec && m != MATCH_NO)
return m;
else if (m != MATCH_NO)
{
gfc_error ("%s at %C is a DEC extension, enable with "
"%<-fdec%>", tag->name);
return MATCH_ERROR;
}
return m;
}
/* Match a DEC I/O flag tag - a tag with no expression such as READONLY. */
static match
match_dec_ftag (const io_tag *tag, gfc_open *o)
{
match m;
m = gfc_match (tag->spec);
if (m != MATCH_YES)
return m;
if (!flag_dec)
{
gfc_error ("%s at %C is a DEC extension, enable with "
"%<-fdec%>", tag->name);
return MATCH_ERROR;
}
/* Just set the READONLY flag, which we use at runtime to avoid delete on
close. */
if (tag == &tag_readonly)
{
o->readonly |= 1;
return MATCH_YES;
}
/* Interpret SHARED as SHARE='DENYNONE' (read lock). */
else if (tag == &tag_shared)
{
if (o->share != NULL)
{
gfc_error ("Duplicate %s specification at %C", tag->name);
return MATCH_ERROR;
}
o->share = gfc_get_character_expr (gfc_default_character_kind,
&gfc_current_locus, "denynone", 8);
return MATCH_YES;
}
/* Interpret NOSHARED as SHARE='DENYRW' (exclusive lock). */
else if (tag == &tag_noshared)
{
if (o->share != NULL)
{
gfc_error ("Duplicate %s specification at %C", tag->name);
return MATCH_ERROR;
}
o->share = gfc_get_character_expr (gfc_default_character_kind,
&gfc_current_locus, "denyrw", 6);
return MATCH_YES;
}
/* We handle all DEC tags above. */
gcc_unreachable ();
}
/* Resolution of the FORMAT tag, to be called from resolve_tag. */
static bool
resolve_tag_format (gfc_expr *e)
{
if (e->expr_type == EXPR_CONSTANT
&& (e->ts.type != BT_CHARACTER
|| e->ts.kind != gfc_default_character_kind))
{
gfc_error ("Constant expression in FORMAT tag at %L must be "
"of type default CHARACTER", &e->where);
return false;
}
/* Concatenate a constant character array into a single character
expression. */
if ((e->expr_type == EXPR_ARRAY || e->rank > 0)
&& e->ts.type == BT_CHARACTER
&& gfc_is_constant_expr (e))
{
if (e->expr_type == EXPR_VARIABLE
&& e->symtree->n.sym->attr.flavor == FL_PARAMETER)
gfc_simplify_expr (e, 1);
if (e->expr_type == EXPR_ARRAY)
{
gfc_constructor *c;
gfc_charlen_t n, len;
gfc_expr *r;
gfc_char_t *dest, *src;
if (e->value.constructor == NULL)
{
gfc_error ("FORMAT tag at %L cannot be a zero-sized array",
&e->where);
return false;
}
n = 0;
c = gfc_constructor_first (e->value.constructor);
len = c->expr->value.character.length;
for ( ; c; c = gfc_constructor_next (c))
n += len;
r = gfc_get_character_expr (e->ts.kind, &e->where, NULL, n);
dest = r->value.character.string;
for (c = gfc_constructor_first (e->value.constructor);
c; c = gfc_constructor_next (c))
{
src = c->expr->value.character.string;
for (gfc_charlen_t i = 0 ; i < len; i++)
*dest++ = *src++;
}
gfc_replace_expr (e, r);
return true;
}
}
/* If e's rank is zero and e is not an element of an array, it should be
of integer or character type. The integer variable should be
ASSIGNED. */
if (e->rank == 0
&& (e->expr_type != EXPR_VARIABLE
|| e->symtree == NULL
|| e->symtree->n.sym->as == NULL
|| e->symtree->n.sym->as->rank == 0))
{
if ((e->ts.type != BT_CHARACTER
|| e->ts.kind != gfc_default_character_kind)
&& e->ts.type != BT_INTEGER)
{
gfc_error ("FORMAT tag at %L must be of type default-kind CHARACTER "
"or of INTEGER", &e->where);
return false;
}
else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
{
if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGNED variable in "
"FORMAT tag at %L", &e->where))
return false;
if (e->symtree->n.sym->attr.assign != 1)
{
gfc_error ("Variable %qs at %L has not been assigned a "
"format label", e->symtree->n.sym->name, &e->where);
return false;
}
}
else if (e->ts.type == BT_INTEGER)
{
gfc_error ("Scalar %qs in FORMAT tag at %L is not an ASSIGNED "
"variable", gfc_basic_typename (e->ts.type), &e->where);
return false;
}
return true;
}
/* If rank is nonzero and type is not character, we allow it under GFC_STD_LEGACY.
It may be assigned an Hollerith constant. */
if (e->ts.type != BT_CHARACTER)
{
if (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS
|| e->ts.type == BT_VOID || e->ts.type == BT_UNKNOWN)
{
gfc_error ("Non-character non-Hollerith in FORMAT tag at %L",
&e->where);
return false;
}
if (!gfc_notify_std (GFC_STD_LEGACY, "Non-character in FORMAT tag "
"at %L", &e->where))
return false;
if (e->rank == 0 && e->symtree->n.sym->as->type == AS_ASSUMED_SHAPE)
{
gfc_error ("Non-character assumed shape array element in FORMAT"
" tag at %L", &e->where);
return false;
}
if (e->rank == 0 && e->symtree->n.sym->as->type == AS_ASSUMED_SIZE)
{
gfc_error ("Non-character assumed size array element in FORMAT"
" tag at %L", &e->where);
return false;
}
if (e->rank == 0 && e->symtree->n.sym->attr.pointer)
{
gfc_error ("Non-character pointer array element in FORMAT tag at %L",
&e->where);
return false;
}
}
return true;
}
/* Do expression resolution and type-checking on an expression tag. */
static bool
resolve_tag (const io_tag *tag, gfc_expr *e)
{
if (e == NULL)
return true;
if (!gfc_resolve_expr (e))
return false;
if (tag == &tag_format)
return resolve_tag_format (e);
if (e->ts.type != tag->type)
{
gfc_error ("%s tag at %L must be of type %s", tag->name,
&e->where, gfc_basic_typename (tag->type));
return false;
}
if (e->ts.type == BT_CHARACTER && e->ts.kind != gfc_default_character_kind)
{
gfc_error ("%s tag at %L must be a character string of default kind",
tag->name, &e->where);
return false;
}
if (e->rank != 0)
{
gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
return false;
}
if (tag == &tag_iomsg)
{
if (!gfc_notify_std (GFC_STD_F2003, "IOMSG tag at %L", &e->where))
return false;
}
if ((tag == &tag_iostat || tag == &tag_size || tag == &tag_iolength
|| tag == &tag_number || tag == &tag_nextrec || tag == &tag_s_recl)
&& e->ts.kind != gfc_default_integer_kind)
{
if (!gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
"INTEGER in %s tag at %L", tag->name, &e->where))
return false;
}
if (e->ts.kind != gfc_default_logical_kind &&
(tag == &tag_exist || tag == &tag_named || tag == &tag_opened
|| tag == &tag_pending))
{
if (!gfc_notify_std (GFC_STD_F2008, "Non-default LOGICAL kind "
"in %s tag at %L", tag->name, &e->where))
return false;
}
if (tag == &tag_newunit)
{
if (!gfc_notify_std (GFC_STD_F2008, "NEWUNIT specifier at %L",
&e->where))
return false;
}
/* NEWUNIT, IOSTAT, SIZE and IOMSG are variable definition contexts. */
if (tag == &tag_newunit || tag == &tag_iostat
|| tag == &tag_size || tag == &tag_iomsg)
{
char context[64];
sprintf (context, _("%s tag"), tag->name);
if (!gfc_check_vardef_context (e, false, false, false, context))
return false;
}
if (tag == &tag_convert)
{
if (!gfc_notify_std (GFC_STD_GNU, "CONVERT tag at %L", &e->where))
return false;
}
return true;
}
/* Match a single tag of an OPEN statement. */
static match
match_open_element (gfc_open *open)
{
match m;
m = match_etag (&tag_e_async, &open->asynchronous);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_unit, &open->unit);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_iomsg, &open->iomsg);
if (m != MATCH_NO)
return m;
m = match_out_tag (&tag_iostat, &open->iostat);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_file, &open->file);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_status, &open->status);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_access, &open->access);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_form, &open->form);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_recl, &open->recl);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_blank, &open->blank);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_position, &open->position);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_action, &open->action);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_delim, &open->delim);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_pad, &open->pad);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_decimal, &open->decimal);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_encoding, &open->encoding);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_round, &open->round);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_sign, &open->sign);
if (m != MATCH_NO)
return m;
m = match_ltag (&tag_err, &open->err);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_convert, &open->convert);
if (m != MATCH_NO)
return m;
m = match_out_tag (&tag_newunit, &open->newunit);
if (m != MATCH_NO)
return m;
/* The following are extensions enabled with -fdec. */
m = match_dec_etag (&tag_e_share, &open->share);
if (m != MATCH_NO)
return m;
m = match_dec_etag (&tag_cc, &open->cc);
if (m != MATCH_NO)
return m;
m = match_dec_ftag (&tag_readonly, open);
if (m != MATCH_NO)
return m;
m = match_dec_ftag (&tag_shared, open);
if (m != MATCH_NO)
return m;
m = match_dec_ftag (&tag_noshared, open);
if (m != MATCH_NO)
return m;
return MATCH_NO;
}
/* Free the gfc_open structure and all the expressions it contains. */
void
gfc_free_open (gfc_open *open)
{
if (open == NULL)
return;
gfc_free_expr (open->unit);
gfc_free_expr (open->iomsg);
gfc_free_expr (open->iostat);
gfc_free_expr (open->file);
gfc_free_expr (open->status);
gfc_free_expr (open->access);
gfc_free_expr (open->form);
gfc_free_expr (open->recl);
gfc_free_expr (open->blank);
gfc_free_expr (open->position);
gfc_free_expr (open->action);
gfc_free_expr (open->delim);
gfc_free_expr (open->pad);
gfc_free_expr (open->decimal);
gfc_free_expr (open->encoding);
gfc_free_expr (open->round);
gfc_free_expr (open->sign);
gfc_free_expr (open->convert);
gfc_free_expr (open->asynchronous);
gfc_free_expr (open->newunit);
gfc_free_expr (open->share);
gfc_free_expr (open->cc);
free (open);
}
static int
compare_to_allowed_values (const char *specifier, const char *allowed[],
const char *allowed_f2003[],
const char *allowed_gnu[], gfc_char_t *value,
const char *statement, bool warn, locus *where,
int *num = NULL);
static bool
check_open_constraints (gfc_open *open, locus *where);
/* Resolve everything in a gfc_open structure. */
bool
gfc_resolve_open (gfc_open *open, locus *where)
{
RESOLVE_TAG (&tag_unit, open->unit);
RESOLVE_TAG (&tag_iomsg, open->iomsg);
RESOLVE_TAG (&tag_iostat, open->iostat);
RESOLVE_TAG (&tag_file, open->file);
RESOLVE_TAG (&tag_status, open->status);
RESOLVE_TAG (&tag_e_access, open->access);
RESOLVE_TAG (&tag_e_form, open->form);
RESOLVE_TAG (&tag_e_recl, open->recl);
RESOLVE_TAG (&tag_e_blank, open->blank);
RESOLVE_TAG (&tag_e_position, open->position);
RESOLVE_TAG (&tag_e_action, open->action);
RESOLVE_TAG (&tag_e_delim, open->delim);
RESOLVE_TAG (&tag_e_pad, open->pad);
RESOLVE_TAG (&tag_e_decimal, open->decimal);
RESOLVE_TAG (&tag_e_encoding, open->encoding);
RESOLVE_TAG (&tag_e_async, open->asynchronous);
RESOLVE_TAG (&tag_e_round, open->round);
RESOLVE_TAG (&tag_e_sign, open->sign);
RESOLVE_TAG (&tag_convert, open->convert);
RESOLVE_TAG (&tag_newunit, open->newunit);
RESOLVE_TAG (&tag_e_share, open->share);
RESOLVE_TAG (&tag_cc, open->cc);
if (!gfc_reference_st_label (open->err, ST_LABEL_TARGET))
return false;
return check_open_constraints (open, where);
}
/* Check if a given value for a SPECIFIER is either in the list of values
allowed in F95 or F2003, issuing an error message and returning a zero
value if it is not allowed. */
static int
compare_to_allowed_values (const char *specifier, const char *allowed[],
const char *allowed_f2003[],
const char *allowed_gnu[], gfc_char_t *value,
const char *statement, bool warn, locus *where,
int *num)
{
int i;
unsigned int len;
len = gfc_wide_strlen (value);
if (len > 0)
{
for (len--; len > 0; len--)
if (value[len] != ' ')
break;
len++;
}
for (i = 0; allowed[i]; i++)
if (len == strlen (allowed[i])
&& gfc_wide_strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
{
if (num)
*num = i;
return 1;
}
if (!where)
where = &gfc_current_locus;
for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
if (len == strlen (allowed_f2003[i])
&& gfc_wide_strncasecmp (value, allowed_f2003[i],
strlen (allowed_f2003[i])) == 0)
{
notification n = gfc_notification_std (GFC_STD_F2003);
if (n == WARNING || (warn && n == ERROR))
{
gfc_warning (0, "Fortran 2003: %s specifier in %s statement at %L "
"has value %qs", specifier, statement, where,
allowed_f2003[i]);
return 1;
}
else
if (n == ERROR)
{
gfc_notify_std (GFC_STD_F2003, "%s specifier in "
"%s statement at %L has value %qs", specifier,
statement, where, allowed_f2003[i]);
return 0;
}
/* n == SILENT */
return 1;
}
for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
if (len == strlen (allowed_gnu[i])
&& gfc_wide_strncasecmp (value, allowed_gnu[i],
strlen (allowed_gnu[i])) == 0)
{
notification n = gfc_notification_std (GFC_STD_GNU);
if (n == WARNING || (warn && n == ERROR))
{
gfc_warning (0, "Extension: %s specifier in %s statement at %L "
"has value %qs", specifier, statement, where,
allowed_gnu[i]);
return 1;
}
else
if (n == ERROR)
{
gfc_notify_std (GFC_STD_GNU, "%s specifier in "
"%s statement at %L has value %qs", specifier,
statement, where, allowed_gnu[i]);
return 0;
}
/* n == SILENT */
return 1;
}
if (warn)
{
char *s = gfc_widechar_to_char (value, -1);
gfc_warning (0,
"%s specifier in %s statement at %L has invalid value %qs",
specifier, statement, where, s);
free (s);
return 1;
}
else
{
char *s = gfc_widechar_to_char (value, -1);
gfc_error ("%s specifier in %s statement at %L has invalid value %qs",
specifier, statement, where, s);
free (s);
return 0;
}
}
/* Check constraints on the OPEN statement.
Similar to check_io_constraints for data transfer statements.
At this point all tags have already been resolved via resolve_tag, which,
among other things, verifies that BT_CHARACTER tags are of default kind. */
static bool
check_open_constraints (gfc_open *open, locus *where)
{
#define warn_or_error(...) \
{ \
if (warn) \
gfc_warning (0, __VA_ARGS__); \
else \
{ \
gfc_error (__VA_ARGS__); \
return false; \
} \
}
bool warn = (open->err || open->iostat) ? true : false;
/* Checks on the ACCESS specifier. */
if (open->access && open->access->expr_type == EXPR_CONSTANT)
{
static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
static const char *access_f2003[] = { "STREAM", NULL };
static const char *access_gnu[] = { "APPEND", NULL };
if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
access_gnu,
open->access->value.character.string,
"OPEN", warn, &open->access->where))
return false;
}
/* Checks on the ACTION specifier. */
if (open->action && open->action->expr_type == EXPR_CONSTANT)
{
gfc_char_t *str = open->action->value.character.string;
static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
str, "OPEN", warn, &open->action->where))
return false;
/* With READONLY, only allow ACTION='READ'. */
if (open->readonly && (gfc_wide_strlen (str) != 4
|| gfc_wide_strncasecmp (str, "READ", 4) != 0))
{
gfc_error ("ACTION type conflicts with READONLY specifier at %L",
&open->action->where);
return false;
}
}
/* If we see READONLY and no ACTION, set ACTION='READ'. */
else if (open->readonly && open->action == NULL)
{
open->action = gfc_get_character_expr (gfc_default_character_kind,
&gfc_current_locus, "read", 4);
}
/* Checks on the ASYNCHRONOUS specifier. */
if (open->asynchronous)
{
if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS= at %L "
"not allowed in Fortran 95",
&open->asynchronous->where))
return false;
if (open->asynchronous->expr_type == EXPR_CONSTANT)
{
static const char * asynchronous[] = { "YES", "NO", NULL };
if (!compare_to_allowed_values ("ASYNCHRONOUS", asynchronous,
NULL, NULL, open->asynchronous->value.character.string,
"OPEN", warn, &open->asynchronous->where))
return false;
}
}
/* Checks on the BLANK specifier. */
if (open->blank)
{
if (!gfc_notify_std (GFC_STD_F2003, "BLANK= at %L "
"not allowed in Fortran 95", &open->blank->where))
return false;
if (open->blank->expr_type == EXPR_CONSTANT)
{
static const char *blank[] = { "ZERO", "NULL", NULL };
if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
open->blank->value.character.string,
"OPEN", warn, &open->blank->where))
return false;
}
}
/* Checks on the CARRIAGECONTROL specifier. */
if (open->cc && open->cc->expr_type == EXPR_CONSTANT)
{
static const char *cc[] = { "LIST", "FORTRAN", "NONE", NULL };
if (!compare_to_allowed_values ("CARRIAGECONTROL", cc, NULL, NULL,
open->cc->value.character.string,
"OPEN", warn, &open->cc->where))
return false;
}
/* Checks on the DECIMAL specifier. */
if (open->decimal)
{
if (!gfc_notify_std (GFC_STD_F2003, "DECIMAL= at %L "
"not allowed in Fortran 95", &open->decimal->where))
return false;
if (open->decimal->expr_type == EXPR_CONSTANT)
{
static const char * decimal[] = { "COMMA", "POINT", NULL };
if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
open->decimal->value.character.string,
"OPEN", warn, &open->decimal->where))
return false;
}
}
/* Checks on the DELIM specifier. */
if (open->delim)
{
if (open->delim->expr_type == EXPR_CONSTANT)
{
static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
open->delim->value.character.string,
"OPEN", warn, &open->delim->where))
return false;
}
}
/* Checks on the ENCODING specifier. */
if (open->encoding)
{
if (!gfc_notify_std (GFC_STD_F2003, "ENCODING= at %L "
"not allowed in Fortran 95", &open->encoding->where))
return false;
if (open->encoding->expr_type == EXPR_CONSTANT)
{
static const char * encoding[] = { "DEFAULT", "UTF-8", NULL };
if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
open->encoding->value.character.string,
"OPEN", warn, &open->encoding->where))
return false;
}
}
/* Checks on the FORM specifier. */
if (open->form && open->form->expr_type == EXPR_CONSTANT)
{
static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
open->form->value.character.string,
"OPEN", warn, &open->form->where))
return false;
}
/* Checks on the PAD specifier. */
if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
{
static const char *pad[] = { "YES", "NO", NULL };
if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
open->pad->value.character.string,
"OPEN", warn, &open->pad->where))
return false;
}
/* Checks on the POSITION specifier. */
if (open->position && open->position->expr_type == EXPR_CONSTANT)
{
static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
open->position->value.character.string,
"OPEN", warn, &open->position->where))
return false;
}
/* Checks on the ROUND specifier. */
if (open->round)
{
if (!gfc_notify_std (GFC_STD_F2003, "ROUND= at %L "
"not allowed in Fortran 95", &open->round->where))
return false;
if (open->round->expr_type == EXPR_CONSTANT)
{
static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
"COMPATIBLE", "PROCESSOR_DEFINED",
NULL };
if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
open->round->value.character.string,
"OPEN", warn, &open->round->where))
return false;
}
}
/* Checks on the SHARE specifier. */
if (open->share && open->share->expr_type == EXPR_CONSTANT)
{
static const char *share[] = { "DENYNONE", "DENYRW", NULL };
if (!compare_to_allowed_values ("SHARE", share, NULL, NULL,
open->share->value.character.string,
"OPEN", warn, &open->share->where))
return false;
}
/* Checks on the SIGN specifier. */
if (open->sign)
{
if (!gfc_notify_std (GFC_STD_F2003, "SIGN= at %L "
"not allowed in Fortran 95", &open->sign->where))
return false;
if (open->sign->expr_type == EXPR_CONSTANT)
{
static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
NULL };
if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
open->sign->value.character.string,
"OPEN", warn, &open->sign->where))
return false;
}
}
/* Checks on the RECL specifier. */
if (open->recl && open->recl->expr_type == EXPR_CONSTANT
&& open->recl->ts.type == BT_INTEGER
&& mpz_sgn (open->recl->value.integer) != 1)
{
warn_or_error (G_("RECL in OPEN statement at %L must be positive"),
&open->recl->where);
}
/* Checks on the STATUS specifier. */
if (open->status && open->status->expr_type == EXPR_CONSTANT)
{
static const char *status[] = { "OLD", "NEW", "SCRATCH",
"REPLACE", "UNKNOWN", NULL };
if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
open->status->value.character.string,
"OPEN", warn, &open->status->where))
return false;
/* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
the FILE= specifier shall appear. */
if (open->file == NULL
&& (gfc_wide_strncasecmp (open->status->value.character.string,
"replace", 7) == 0
|| gfc_wide_strncasecmp (open->status->value.character.string,
"new", 3) == 0))
{
char *s = gfc_widechar_to_char (open->status->value.character.string,
-1);
warn_or_error (G_("The STATUS specified in OPEN statement at %L is "
"%qs and no FILE specifier is present"),
&open->status->where, s);
free (s);
}
/* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
the FILE= specifier shall not appear. */
if (gfc_wide_strncasecmp (open->status->value.character.string,
"scratch", 7) == 0 && open->file)
{
warn_or_error (G_("The STATUS specified in OPEN statement at %L "
"cannot have the value SCRATCH if a FILE specifier "
"is present"), &open->status->where);
}
}
/* Checks on NEWUNIT specifier. */
if (open->newunit)
{
if (open->unit)
{
gfc_error ("UNIT specifier not allowed with NEWUNIT at %L",
&open->newunit->where);
return false;
}
if (!open->file &&
(!open->status ||
(open->status->expr_type == EXPR_CONSTANT
&& gfc_wide_strncasecmp (open->status->value.character.string,
"scratch", 7) != 0)))
{
gfc_error ("NEWUNIT specifier must have FILE= "
"or STATUS='scratch' at %L", &open->newunit->where);
return false;
}
}
else if (!open->unit)
{
gfc_error ("OPEN statement at %L must have UNIT or NEWUNIT specified",
where);
return false;
}
/* Things that are not allowed for unformatted I/O. */
if (open->form && open->form->expr_type == EXPR_CONSTANT
&& (open->delim || open->decimal || open->encoding || open->round
|| open->sign || open->pad || open->blank)
&& gfc_wide_strncasecmp (open->form->value.character.string,
"unformatted", 11) == 0)
{
locus *loc;
const char *spec;
if (open->delim)
{
loc = &open->delim->where;
spec = "DELIM ";
}
else if (open->pad)
{
loc = &open->pad->where;
spec = "PAD ";
}
else if (open->blank)
{
loc = &open->blank->where;
spec = "BLANK ";
}
else
{
loc = where;
spec = "";
}
warn_or_error (G_("%s specifier at %L not allowed in OPEN statement for "
"unformatted I/O"), spec, loc);
}
if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
&& gfc_wide_strncasecmp (open->access->value.character.string,
"stream", 6) == 0)
{
warn_or_error (G_("RECL specifier not allowed in OPEN statement at %L for "
"stream I/O"), &open->recl->where);
}
if (open->position
&& open->access && open->access->expr_type == EXPR_CONSTANT
&& !(gfc_wide_strncasecmp (open->access->value.character.string,
"sequential", 10) == 0
|| gfc_wide_strncasecmp (open->access->value.character.string,
"stream", 6) == 0
|| gfc_wide_strncasecmp (open->access->value.character.string,
"append", 6) == 0))
{
warn_or_error (G_("POSITION specifier in OPEN statement at %L only allowed "
"for stream or sequential ACCESS"), &open->position->where);
}
return true;
#undef warn_or_error
}
/* Match an OPEN statement. */
match
gfc_match_open (void)
{
gfc_open *open;
match m;
m = gfc_match_char ('(');
if (m == MATCH_NO)
return m;
open = XCNEW (gfc_open);
m = match_open_element (open);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
{
m = gfc_match_expr (&open->unit);
if (m == MATCH_ERROR)
goto cleanup;
}
for (;;)
{
if (gfc_match_char (')') == MATCH_YES)
break;
if (gfc_match_char (',') != MATCH_YES)
goto syntax;
m = match_open_element (open);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
goto syntax;
}
if (gfc_match_eos () == MATCH_NO)
goto syntax;
if (gfc_pure (NULL))
{
gfc_error ("OPEN statement not allowed in PURE procedure at %C");
goto cleanup;
}
gfc_unset_implicit_pure (NULL);
new_st.op = EXEC_OPEN;
new_st.ext.open = open;
return MATCH_YES;
syntax:
gfc_syntax_error (ST_OPEN);
cleanup:
gfc_free_open (open);
return MATCH_ERROR;
}
/* Free a gfc_close structure an all its expressions. */
void
gfc_free_close (gfc_close *close)
{
if (close == NULL)
return;
gfc_free_expr (close->unit);
gfc_free_expr (close->iomsg);
gfc_free_expr (close->iostat);
gfc_free_expr (close->status);
free (close);
}
/* Match elements of a CLOSE statement. */
static match
match_close_element (gfc_close *close)
{
match m;
m = match_etag (&tag_unit, &close->unit);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_status, &close->status);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_iomsg, &close->iomsg);
if (m != MATCH_NO)
return m;
m = match_out_tag (&tag_iostat, &close->iostat);
if (m != MATCH_NO)
return m;
m = match_ltag (&tag_err, &close->err);
if (m != MATCH_NO)
return m;
return MATCH_NO;
}
/* Match a CLOSE statement. */
match
gfc_match_close (void)
{
gfc_close *close;
match m;
m = gfc_match_char ('(');
if (m == MATCH_NO)
return m;
close = XCNEW (gfc_close);
m = match_close_element (close);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
{
m = gfc_match_expr (&close->unit);
if (m == MATCH_NO)
goto syntax;
if (m == MATCH_ERROR)
goto cleanup;
}
for (;;)
{
if (gfc_match_char (')') == MATCH_YES)
break;
if (gfc_match_char (',') != MATCH_YES)
goto syntax;
m = match_close_element (close);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
goto syntax;
}
if (gfc_match_eos () == MATCH_NO)
goto syntax;
if (gfc_pure (NULL))
{
gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
goto cleanup;
}
gfc_unset_implicit_pure (NULL);
new_st.op = EXEC_CLOSE;
new_st.ext.close = close;
return MATCH_YES;
syntax:
gfc_syntax_error (ST_CLOSE);
cleanup:
gfc_free_close (close);
return MATCH_ERROR;
}
static bool
check_close_constraints (gfc_close *close, locus *where)
{
bool warn = (close->iostat || close->err) ? true : false;
if (close->unit == NULL)
{
gfc_error ("CLOSE statement at %L requires a UNIT number", where);
return false;
}
if (close->unit->expr_type == EXPR_CONSTANT
&& close->unit->ts.type == BT_INTEGER
&& mpz_sgn (close->unit->value.integer) < 0)
{
gfc_error ("UNIT number in CLOSE statement at %L must be non-negative",
&close->unit->where);
}
/* Checks on the STATUS specifier. */
if (close->status && close->status->expr_type == EXPR_CONSTANT)
{
static const char *status[] = { "KEEP", "DELETE", NULL };
if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
close->status->value.character.string,
"CLOSE", warn, &close->status->where))
return false;
}
return true;
}
/* Resolve everything in a gfc_close structure. */
bool
gfc_resolve_close (gfc_close *close, locus *where)
{
RESOLVE_TAG (&tag_unit, close->unit);
RESOLVE_TAG (&tag_iomsg, close->iomsg);
RESOLVE_TAG (&tag_iostat, close->iostat);
RESOLVE_TAG (&tag_status, close->status);
if (!gfc_reference_st_label (close->err, ST_LABEL_TARGET))
return false;
return check_close_constraints (close, where);
}
/* Free a gfc_filepos structure. */
void
gfc_free_filepos (gfc_filepos *fp)
{
gfc_free_expr (fp->unit);
gfc_free_expr (fp->iomsg);
gfc_free_expr (fp->iostat);
free (fp);
}
/* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
static match
match_file_element (gfc_filepos *fp)
{
match m;
m = match_etag (&tag_unit, &fp->unit);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_iomsg, &fp->iomsg);
if (m != MATCH_NO)
return m;
m = match_out_tag (&tag_iostat, &fp->iostat);
if (m != MATCH_NO)
return m;
m = match_ltag (&tag_err, &fp->err);
if (m != MATCH_NO)
return m;
return MATCH_NO;
}
/* Match the second half of the file-positioning statements, REWIND,
BACKSPACE, ENDFILE, or the FLUSH statement. */
static match
match_filepos (gfc_statement st, gfc_exec_op op)
{
gfc_filepos *fp;
match m;
fp = XCNEW (gfc_filepos);
if (gfc_match_char ('(') == MATCH_NO)
{
m = gfc_match_expr (&fp->unit);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
goto syntax;
goto done;
}
m = match_file_element (fp);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
{
m = gfc_match_expr (&fp->unit);
if (m == MATCH_ERROR || m == MATCH_NO)
goto syntax;
}
for (;;)
{
if (gfc_match_char (')') == MATCH_YES)
break;
if (gfc_match_char (',') != MATCH_YES)
goto syntax;
m = match_file_element (fp);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
goto syntax;
}
done:
if (gfc_match_eos () != MATCH_YES)
goto syntax;
if (gfc_pure (NULL))
{
gfc_error ("%s statement not allowed in PURE procedure at %C",
gfc_ascii_statement (st));
goto cleanup;
}
gfc_unset_implicit_pure (NULL);
new_st.op = op;
new_st.ext.filepos = fp;
return MATCH_YES;
syntax:
gfc_syntax_error (st);
cleanup:
gfc_free_filepos (fp);
return MATCH_ERROR;
}
bool
gfc_resolve_filepos (gfc_filepos *fp, locus *where)
{
RESOLVE_TAG (&tag_unit, fp->unit);
RESOLVE_TAG (&tag_iostat, fp->iostat);
RESOLVE_TAG (&tag_iomsg, fp->iomsg);
if (!fp->unit && (fp->iostat || fp->iomsg || fp->err))
{
gfc_error ("UNIT number missing in statement at %L", where);
return false;
}
if (!gfc_reference_st_label (fp->err, ST_LABEL_TARGET))
return false;
if (fp->unit->expr_type == EXPR_CONSTANT
&& fp->unit->ts.type == BT_INTEGER
&& mpz_sgn (fp->unit->value.integer) < 0)
{
gfc_error ("UNIT number in statement at %L must be non-negative",
&fp->unit->where);
return false;
}
return true;
}
/* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
and the FLUSH statement. */
match
gfc_match_endfile (void)
{
return match_filepos (ST_END_FILE, EXEC_ENDFILE);
}
match
gfc_match_backspace (void)
{
return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
}
match
gfc_match_rewind (void)
{
return match_filepos (ST_REWIND, EXEC_REWIND);
}
match
gfc_match_flush (void)
{
if (!gfc_notify_std (GFC_STD_F2003, "FLUSH statement at %C"))
return MATCH_ERROR;
return match_filepos (ST_FLUSH, EXEC_FLUSH);
}
/******************** Data Transfer Statements *********************/
/* Return a default unit number. */
static gfc_expr *
default_unit (io_kind k)
{
int unit;
if (k == M_READ)
unit = 5;
else
unit = 6;
return gfc_get_int_expr (gfc_default_integer_kind, NULL, unit);
}
/* Match a unit specification for a data transfer statement. */
static match
match_dt_unit (io_kind k, gfc_dt *dt)
{
gfc_expr *e;
char c;
if (gfc_match_char ('*') == MATCH_YES)
{
if (dt->io_unit != NULL)
goto conflict;
dt->io_unit = default_unit (k);
c = gfc_peek_ascii_char ();
if (c == ')')
gfc_error_now ("Missing format with default unit at %C");
return MATCH_YES;
}
if (gfc_match_expr (&e) == MATCH_YES)
{
if (dt->io_unit != NULL)
{
gfc_free_expr (e);
goto conflict;
}
dt->io_unit = e;
return MATCH_YES;
}
return MATCH_NO;
conflict:
gfc_error ("Duplicate UNIT specification at %C");
return MATCH_ERROR;
}
/* Match a format specification. */
static match
match_dt_format (gfc_dt *dt)
{
locus where;
gfc_expr *e;
gfc_st_label *label;
match m;
where = gfc_current_locus;
if (gfc_match_char ('*') == MATCH_YES)
{
if (dt->format_expr != NULL || dt->format_label != NULL)
goto conflict;
dt->format_label = &format_asterisk;
return MATCH_YES;
}
if ((m = gfc_match_st_label (&label)) == MATCH_YES)
{
char c;
/* Need to check if the format label is actually either an operand
to a user-defined operator or is a kind type parameter. That is,
print 2.ip.8 ! .ip. is a user-defined operator return CHARACTER.
print 1_'(I0)', i ! 1_'(I0)' is a default character string. */
gfc_gobble_whitespace ();
c = gfc_peek_ascii_char ();
if (c == '.' || c == '_')
gfc_current_locus = where;
else
{
if (dt->format_expr != NULL || dt->format_label != NULL)
{
gfc_free_st_label (label);
goto conflict;
}
if (!gfc_reference_st_label (label, ST_LABEL_FORMAT))
return MATCH_ERROR;
dt->format_label = label;
return MATCH_YES;
}
}
else if (m == MATCH_ERROR)
/* The label was zero or too large. Emit the correct diagnosis. */
return MATCH_ERROR;
if (gfc_match_expr (&e) == MATCH_YES)
{
if (dt->format_expr != NULL || dt->format_label != NULL)
{
gfc_free_expr (e);
goto conflict;
}
dt->format_expr = e;
return MATCH_YES;
}
gfc_current_locus = where; /* The only case where we have to restore */
return MATCH_NO;
conflict:
gfc_error ("Duplicate format specification at %C");
return MATCH_ERROR;
}
/* Check for formatted read and write DTIO procedures. */
static bool
dtio_procs_present (gfc_symbol *sym, io_kind k)
{
gfc_symbol *derived;
if (sym && sym->ts.u.derived)
{
if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
derived = CLASS_DATA (sym)->ts.u.derived;
else if (sym->ts.type == BT_DERIVED)
derived = sym->ts.u.derived;
else
return false;
if ((k == M_WRITE || k == M_PRINT) &&
(gfc_find_specific_dtio_proc (derived, true, true) != NULL))
return true;
if ((k == M_READ) &&
(gfc_find_specific_dtio_proc (derived, false, true) != NULL))
return true;
}
return false;
}
/* Traverse a namelist that is part of a READ statement to make sure
that none of the variables in the namelist are INTENT(IN). Returns
nonzero if we find such a variable. */
static int
check_namelist (gfc_symbol *sym)
{
gfc_namelist *p;
for (p = sym->namelist; p; p = p->next)
if (p->sym->attr.intent == INTENT_IN)
{
gfc_error ("Symbol %qs in namelist %qs is INTENT(IN) at %C",
p->sym->name, sym->name);
return 1;
}
return 0;
}
/* Match a single data transfer element. */
static match
match_dt_element (io_kind k, gfc_dt *dt)
{
char name[GFC_MAX_SYMBOL_LEN + 1];
gfc_symbol *sym;
match m;
if (gfc_match (" unit =") == MATCH_YES)
{
m = match_dt_unit (k, dt);
if (m != MATCH_NO)
return m;
}
if (gfc_match (" fmt =") == MATCH_YES)
{
m = match_dt_format (dt);
if (m != MATCH_NO)
return m;
}
if (gfc_match (" nml = %n", name) == MATCH_YES)
{
if (dt->namelist != NULL)
{
gfc_error ("Duplicate NML specification at %C");
return MATCH_ERROR;
}
if (gfc_find_symbol (name, NULL, 1, &sym))
return MATCH_ERROR;
if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
{
gfc_error ("Symbol %qs at %C must be a NAMELIST group name",
sym != NULL ? sym->name : name);
return MATCH_ERROR;
}
dt->namelist = sym;
if (k == M_READ && check_namelist (sym))
return MATCH_ERROR;
return MATCH_YES;
}
m = match_etag (&tag_e_async, &dt->asynchronous);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_blank, &dt->blank);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_delim, &dt->delim);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_pad, &dt->pad);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_sign, &dt->sign);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_round, &dt->round);
if (m != MATCH_NO)
return m;
m = match_out_tag (&tag_id, &dt->id);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_decimal, &dt->decimal);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_rec, &dt->rec);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_spos, &dt->pos);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_iomsg, &dt->iomsg);
if (m != MATCH_NO)
return m;
m = match_out_tag (&tag_iostat, &dt->iostat);
if (m != MATCH_NO)
return m;
m = match_ltag (&tag_err, &dt->err);
if (m == MATCH_YES)
dt->err_where = gfc_current_locus;
if (m != MATCH_NO)
return m;
m = match_etag (&tag_advance, &dt->advance);
if (m != MATCH_NO)
return m;
m = match_out_tag (&tag_size, &dt->size);
if (m != MATCH_NO)
return m;
m = match_ltag (&tag_end, &dt->end);
if (m == MATCH_YES)
{
if (k == M_WRITE)
{
gfc_error ("END tag at %C not allowed in output statement");
return MATCH_ERROR;
}
dt->end_where = gfc_current_locus;
}
if (m != MATCH_NO)
return m;
m = match_ltag (&tag_eor, &dt->eor);
if (m == MATCH_YES)
dt->eor_where = gfc_current_locus;
if (m != MATCH_NO)
return m;
return MATCH_NO;
}
/* Free a data transfer structure and everything below it. */
void
gfc_free_dt (gfc_dt *dt)
{
if (dt == NULL)
return;
gfc_free_expr (dt->io_unit);
gfc_free_expr (dt->format_expr);
gfc_free_expr (dt->rec);
gfc_free_expr (dt->advance);
gfc_free_expr (dt->iomsg);
gfc_free_expr (dt->iostat);
gfc_free_expr (dt->size);
gfc_free_expr (dt->pad);
gfc_free_expr (dt->delim);
gfc_free_expr (dt->sign);
gfc_free_expr (dt->round);
gfc_free_expr (dt->blank);
gfc_free_expr (dt->decimal);
gfc_free_expr (dt->pos);
gfc_free_expr (dt->dt_io_kind);
/* dt->extra_comma is a link to dt_io_kind if it is set. */
free (dt);
}
static const char *
io_kind_name (io_kind k);
static bool
check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
locus *spec_end);
/* Resolve everything in a gfc_dt structure. */
bool
gfc_resolve_dt (gfc_code *dt_code, gfc_dt *dt, locus *loc)
{
gfc_expr *e;
io_kind k;
/* This is set in any case. */
gcc_assert (dt->dt_io_kind);
k = dt->dt_io_kind->value.iokind;
RESOLVE_TAG (&tag_format, dt->format_expr);
RESOLVE_TAG (&tag_rec, dt->rec);
RESOLVE_TAG (&tag_spos, dt->pos);
RESOLVE_TAG (&tag_advance, dt->advance);
RESOLVE_TAG (&tag_id, dt->id);
RESOLVE_TAG (&tag_iomsg, dt->iomsg);
RESOLVE_TAG (&tag_iostat, dt->iostat);
RESOLVE_TAG (&tag_size, dt->size);
RESOLVE_TAG (&tag_e_pad, dt->pad);
RESOLVE_TAG (&tag_e_delim, dt->delim);
RESOLVE_TAG (&tag_e_sign, dt->sign);
RESOLVE_TAG (&tag_e_round, dt->round);
RESOLVE_TAG (&tag_e_blank, dt->blank);
RESOLVE_TAG (&tag_e_decimal, dt->decimal);
RESOLVE_TAG (&tag_e_async, dt->asynchronous);
/* Check I/O constraints.
To validate NAMELIST we need to check if we were also given an I/O list,
which is stored in code->block->next with op EXEC_TRANSFER.
Note that the I/O list was already resolved from resolve_transfer. */
gfc_code *io_code = NULL;
if (dt_code && dt_code->block && dt_code->block->next
&& dt_code->block->next->op == EXEC_TRANSFER)
io_code = dt_code->block->next;
if (!check_io_constraints (k, dt, io_code, loc))
return false;
e = dt->io_unit;
if (e == NULL)
{
gfc_error ("UNIT not specified at %L", loc);
return false;
}
if (e->symtree && e->symtree->n.sym->attr.flavor == FL_PARAMETER
&& e->ts.type == BT_CHARACTER)
{
gfc_error ("UNIT specification at %L must "
"not be a character PARAMETER", &e->where);
return false;
}
if (gfc_resolve_expr (e)
&& (e->ts.type != BT_INTEGER
&& (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_VARIABLE)))
{
/* If there is no extra comma signifying the "format" form of the IO
statement, then this must be an error. */
if (!dt->extra_comma)
{
gfc_error ("UNIT specification at %L must be an INTEGER expression "
"or a CHARACTER variable", &e->where);
return false;
}
else
{
/* At this point, we have an extra comma. If io_unit has arrived as
type character, we assume its really the "format" form of the I/O
statement. We set the io_unit to the default unit and format to
the character expression. See F95 Standard section 9.4. */
if (e->ts.type == BT_CHARACTER && (k == M_READ || k == M_PRINT))
{
dt->format_expr = dt->io_unit;
dt->io_unit = default_unit (k);
/* Nullify this pointer now so that a warning/error is not
triggered below for the "Extension". */
dt->extra_comma = NULL;
}
if (k == M_WRITE)
{
gfc_error ("Invalid form of WRITE statement at %L, UNIT required",
&dt->extra_comma->where);
return false;
}
}
}
if (e->ts.type == BT_CHARACTER)
{
if (gfc_has_vector_index (e))
{
gfc_error ("Internal unit with vector subscript at %L", &e->where);
return false;
}
/* If we are writing, make sure the internal unit can be changed. */
gcc_assert (k != M_PRINT);
if (k == M_WRITE
&& !gfc_check_vardef_context (e, false, false, false,
_("internal unit in WRITE")))
return false;
}
if (e->rank && e->ts.type != BT_CHARACTER)
{
gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
return false;
}
if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_INTEGER
&& mpz_sgn (e->value.integer) < 0)
{
gfc_error ("UNIT number in statement at %L must be non-negative",
&e->where);
return false;
}
/* If we are reading and have a namelist, check that all namelist symbols
can appear in a variable definition context. */
if (dt->namelist)
{
gfc_namelist* n;
for (n = dt->namelist->namelist; n; n = n->next)
{
gfc_expr* e;
bool t;
if (k == M_READ)
{
e = gfc_get_variable_expr (gfc_find_sym_in_symtree (n->sym));
t = gfc_check_vardef_context (e, false, false, false, NULL);
gfc_free_expr (e);
if (!t)
{
gfc_error ("NAMELIST %qs in READ statement at %L contains"
" the symbol %qs which may not appear in a"
" variable definition context",
dt->namelist->name, loc, n->sym->name);
return false;
}
}
t = dtio_procs_present (n->sym, k);
if (n->sym->ts.type == BT_CLASS && !t)
{
gfc_error ("NAMELIST object %qs in namelist %qs at %L is "
"polymorphic and requires a defined input/output "
"procedure", n->sym->name, dt->namelist->name, loc);
return false;
}
if ((n->sym->ts.type == BT_DERIVED)
&& (n->sym->ts.u.derived->attr.alloc_comp
|| n->sym->ts.u.derived->attr.pointer_comp))
{
if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
"namelist %qs at %L with ALLOCATABLE "
"or POINTER components", n->sym->name,
dt->namelist->name, loc))
return false;
if (!t)
{
gfc_error ("NAMELIST object %qs in namelist %qs at %L has "
"ALLOCATABLE or POINTER components and thus requires "
"a defined input/output procedure", n->sym->name,
dt->namelist->name, loc);
return false;
}
}
}
}
if (dt->extra_comma
&& !gfc_notify_std (GFC_STD_LEGACY, "Comma before i/o item list at %L",
&dt->extra_comma->where))
return false;
if (dt->err)
{
if (!gfc_reference_st_label (dt->err, ST_LABEL_TARGET))
return false;
if (dt->err->defined == ST_LABEL_UNKNOWN)
{
gfc_error ("ERR tag label %d at %L not defined",
dt->err->value, &dt->err_where);
return false;
}
}
if (dt->end)
{
if (!gfc_reference_st_label (dt->end, ST_LABEL_TARGET))
return false;
if (dt->end->defined == ST_LABEL_UNKNOWN)
{
gfc_error ("END tag label %d at %L not defined",
dt->end->value, &dt->end_where);
return false;
}
}
if (dt->eor)
{
if (!gfc_reference_st_label (dt->eor, ST_LABEL_TARGET))
return false;
if (dt->eor->defined == ST_LABEL_UNKNOWN)
{
gfc_error ("EOR tag label %d at %L not defined",
dt->eor->value, &dt->eor_where);
return false;
}
}
/* Check the format label actually exists. */
if (dt->format_label && dt->format_label != &format_asterisk
&& dt->format_label->defined == ST_LABEL_UNKNOWN)
{
gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
loc);
return false;
}
return true;
}
/* Given an io_kind, return its name. */
static const char *
io_kind_name (io_kind k)
{
const char *name;
switch (k)
{
case M_READ:
name = "READ";
break;
case M_WRITE:
name = "WRITE";
break;
case M_PRINT:
name = "PRINT";
break;
case M_INQUIRE:
name = "INQUIRE";
break;
default:
gfc_internal_error ("io_kind_name(): bad I/O-kind");
}
return name;
}
/* Match an IO iteration statement of the form:
( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
which is equivalent to a single IO element. This function is
mutually recursive with match_io_element(). */
static match match_io_element (io_kind, gfc_code **);
static match
match_io_iterator (io_kind k, gfc_code **result)
{
gfc_code *head, *tail, *new_code;
gfc_iterator *iter;
locus old_loc;
match m;
int n;
iter = NULL;
head = NULL;
old_loc = gfc_current_locus;
if (gfc_match_char ('(') != MATCH_YES)
return MATCH_NO;
m = match_io_element (k, &head);
tail = head;
if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
{
m = MATCH_NO;
goto cleanup;
}
/* Can't be anything but an IO iterator. Build a list. */
iter = gfc_get_iterator ();
for (n = 1;; n++)
{
m = gfc_match_iterator (iter, 0);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_YES)
{
gfc_check_do_variable (iter->var->symtree);
break;
}
m = match_io_element (k, &new_code);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
{
if (n > 2)
goto syntax;
goto cleanup;
}
tail = gfc_append_code (tail, new_code);
if (gfc_match_char (',') != MATCH_YES)
{
if (n > 2)
goto syntax;
m = MATCH_NO;
goto cleanup;
}
}
if (gfc_match_char (')') != MATCH_YES)
goto syntax;
new_code = gfc_get_code (EXEC_DO);
new_code->ext.iterator = iter;
new_code->block = gfc_get_code (EXEC_DO);
new_code->block->next = head;
*result = new_code;
return MATCH_YES;
syntax:
gfc_error ("Syntax error in I/O iterator at %C");
m = MATCH_ERROR;
cleanup:
gfc_free_iterator (iter, 1);
gfc_free_statements (head);
gfc_current_locus = old_loc;
return m;
}
/* Match a single element of an IO list, which is either a single
expression or an IO Iterator. */
static match
match_io_element (io_kind k, gfc_code **cpp)
{
gfc_expr *expr;
gfc_code *cp;
match m;
expr = NULL;
m = match_io_iterator (k, cpp);
if (m == MATCH_YES)
return MATCH_YES;
if (k == M_READ)
{
m = gfc_match_variable (&expr, 0);
if (m == MATCH_NO)
{
gfc_error ("Expecting variable in READ statement at %C");
m = MATCH_ERROR;
}
if (m == MATCH_YES && expr->expr_type == EXPR_CONSTANT)
{
gfc_error ("Expecting variable or io-implied-do in READ statement "
"at %L", &expr->where);
m = MATCH_ERROR;
}
if (m == MATCH_YES
&& expr->expr_type == EXPR_VARIABLE
&& expr->symtree->n.sym->attr.external)
{
gfc_error ("Expecting variable or io-implied-do at %L",
&expr->where);
m = MATCH_ERROR;
}
}
else
{
m = gfc_match_expr (&expr);
if (m == MATCH_NO)
gfc_error ("Expected expression in %s statement at %C",
io_kind_name (k));
if (m == MATCH_YES && expr->ts.type == BT_BOZ)
{
if (gfc_invalid_boz (G_("BOZ literal constant at %L cannot appear in"
" an output IO list"), &gfc_current_locus))
return MATCH_ERROR;
if (!gfc_boz2int (expr, gfc_max_integer_kind))
return MATCH_ERROR;
};
}
if (m == MATCH_YES && k == M_READ && gfc_check_do_variable (expr->symtree))
m = MATCH_ERROR;
if (m != MATCH_YES)
{
gfc_free_expr (expr);
return MATCH_ERROR;
}
cp = gfc_get_code (EXEC_TRANSFER);
cp->expr1 = expr;
if (k != M_INQUIRE)
cp->ext.dt = current_dt;
*cpp = cp;
return MATCH_YES;
}
/* Match an I/O list, building gfc_code structures as we go. */
static match
match_io_list (io_kind k, gfc_code **head_p)
{
gfc_code *head, *tail, *new_code;
match m;
*head_p = head = tail = NULL;
if (gfc_match_eos () == MATCH_YES)
return MATCH_YES;
for (;;)
{
m = match_io_element (k, &new_code);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
goto syntax;
tail = gfc_append_code (tail, new_code);
if (head == NULL)
head = new_code;
if (gfc_match_eos () == MATCH_YES)
break;
if (gfc_match_char (',') != MATCH_YES)
goto syntax;
}
*head_p = head;
return MATCH_YES;
syntax:
gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
cleanup:
gfc_free_statements (head);
return MATCH_ERROR;
}
/* Attach the data transfer end node. */
static void
terminate_io (gfc_code *io_code)
{
gfc_code *c;
if (io_code == NULL)
io_code = new_st.block;
c = gfc_get_code (EXEC_DT_END);
/* Point to structure that is already there */
c->ext.dt = new_st.ext.dt;
gfc_append_code (io_code, c);
}
/* Check the constraints for a data transfer statement. The majority of the
constraints appearing in 9.4 of the standard appear here.
Tag expressions are already resolved by resolve_tag, which includes
verifying the type, that they are scalar, and verifying that BT_CHARACTER
tags are of default kind. */
static bool
check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
locus *spec_end)
{
#define io_constraint(condition, msg, arg)\
if (condition) \
{\
if ((arg)->lb != NULL)\
gfc_error ((msg), (arg));\
else\
gfc_error ((msg), spec_end);\
return false;\
}
gfc_expr *expr;
gfc_symbol *sym = NULL;
bool warn, unformatted;
warn = (dt->err || dt->iostat) ? true : false;
unformatted = dt->format_expr == NULL && dt->format_label == NULL
&& dt->namelist == NULL;
expr = dt->io_unit;
if (expr && expr->expr_type == EXPR_VARIABLE
&& expr->ts.type == BT_CHARACTER)
{
sym = expr->symtree->n.sym;
io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
"Internal file at %L must not be INTENT(IN)",
&expr->where);
io_constraint (gfc_has_vector_index (dt->io_unit),
"Internal file incompatible with vector subscript at %L",
&expr->where);
io_constraint (dt->rec != NULL,
"REC tag at %L is incompatible with internal file",
&dt->rec->where);
io_constraint (dt->pos != NULL,
"POS tag at %L is incompatible with internal file",
&dt->pos->where);
io_constraint (unformatted,
"Unformatted I/O not allowed with internal unit at %L",
&dt->io_unit->where);
io_constraint (dt->asynchronous != NULL,
"ASYNCHRONOUS tag at %L not allowed with internal file",
&dt->asynchronous->where);
if (dt->namelist != NULL)
{
if (!gfc_notify_std (GFC_STD_F2003, "Internal file at %L with "
"namelist", &expr->where))
return false;
}
io_constraint (dt->advance != NULL,
"ADVANCE tag at %L is incompatible with internal file",
&dt->advance->where);
}
if (expr && expr->ts.type != BT_CHARACTER)
{
if (gfc_pure (NULL) && (k == M_READ || k == M_WRITE))
{
gfc_error ("IO UNIT in %s statement at %L must be "
"an internal file in a PURE procedure",
io_kind_name (k), &expr->where);
return false;
}
if (k == M_READ || k == M_WRITE)
gfc_unset_implicit_pure (NULL);
}
if (dt->asynchronous)
{
int num = -1;
static const char * asynchronous[] = { "YES", "NO", NULL };
/* Note: gfc_reduce_init_expr reports an error if not init-expr. */
if (!gfc_reduce_init_expr (dt->asynchronous))
return false;
if (!compare_to_allowed_values
("ASYNCHRONOUS", asynchronous, NULL, NULL,
dt->asynchronous->value.character.string,
io_kind_name (k), warn, &dt->asynchronous->where, &num))
return false;
gcc_checking_assert (num != -1);
/* For "YES", mark related symbols as asynchronous. */
if (num == 0)
{
/* SIZE variable. */
if (dt->size)
dt->size->symtree->n.sym->attr.asynchronous = 1;
/* Variables in a NAMELIST. */
if (dt->namelist)
for (gfc_namelist *nl = dt->namelist->namelist; nl; nl = nl->next)
nl->sym->attr.asynchronous = 1;
/* Variables in an I/O list. */
for (gfc_code *xfer = io_code; xfer && xfer->op == EXEC_TRANSFER;
xfer = xfer->next)
{
gfc_expr *expr = xfer->expr1;
while (expr != NULL && expr->expr_type == EXPR_OP
&& expr->value.op.op == INTRINSIC_PARENTHESES)
expr = expr->value.op.op1;
if (expr && expr->expr_type == EXPR_VARIABLE)
expr->symtree->n.sym->attr.asynchronous = 1;
}
}
}
if (dt->id)
{
bool not_yes
= !dt->asynchronous
|| gfc_wide_strlen (dt->asynchronous->value.character.string) != 3
|| gfc_wide_strncasecmp (dt->asynchronous->value.character.string,
"yes", 3) != 0;
io_constraint (not_yes,
"ID= specifier at %L must be with ASYNCHRONOUS='yes' "
"specifier", &dt->id->where);
}
if (dt->decimal)
{
if (!gfc_notify_std (GFC_STD_F2003, "DECIMAL= at %L "
"not allowed in Fortran 95", &dt->decimal->where))
return false;
if (dt->decimal->expr_type == EXPR_CONSTANT)
{
static const char * decimal[] = { "COMMA", "POINT", NULL };
if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
dt->decimal->value.character.string,
io_kind_name (k), warn,
&dt->decimal->where))
return false;
io_constraint (unformatted,
"the DECIMAL= specifier at %L must be with an "
"explicit format expression", &dt->decimal->where);
}
}
if (dt->blank)
{
if (!gfc_notify_std (GFC_STD_F2003, "BLANK= at %L "
"not allowed in Fortran 95", &dt->blank->where))
return false;
if (dt->blank->expr_type == EXPR_CONSTANT)
{
static const char * blank[] = { "NULL", "ZERO", NULL };
if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
dt->blank->value.character.string,
io_kind_name (k), warn,
&dt->blank->where))
return false;
io_constraint (unformatted,
"the BLANK= specifier at %L must be with an "
"explicit format expression", &dt->blank->where);
}
}
if (dt->pad)
{
if (!gfc_notify_std (GFC_STD_F2003, "PAD= at %L "
"not allowed in Fortran 95", &dt->pad->where))
return false;
if (dt->pad->expr_type == EXPR_CONSTANT)
{
static const char * pad[] = { "YES", "NO", NULL };
if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
dt->pad->value.character.string,
io_kind_name (k), warn,
&dt->pad->where))
return false;
io_constraint (unformatted,
"the PAD= specifier at %L must be with an "
"explicit format expression", &dt->pad->where);
}
}
if (dt->round)
{
if (!gfc_notify_std (GFC_STD_F2003, "ROUND= at %L "
"not allowed in Fortran 95", &dt->round->where))
return false;
if (dt->round->expr_type == EXPR_CONSTANT)
{
static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
"COMPATIBLE", "PROCESSOR_DEFINED",
NULL };
if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
dt->round->value.character.string,
io_kind_name (k), warn,
&dt->round->where))
return false;
}
}
if (dt->sign)
{
/* When implemented, change the following to use gfc_notify_std F2003.
if (gfc_notify_std (GFC_STD_F2003, "SIGN= at %L "
"not allowed in Fortran 95", &dt->sign->where) == false)
return false; */
if (dt->sign->expr_type == EXPR_CONSTANT)
{
static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
NULL };
if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
dt->sign->value.character.string,
io_kind_name (k), warn, &dt->sign->where))
return false;
io_constraint (unformatted,
"SIGN= specifier at %L must be with an "
"explicit format expression", &dt->sign->where);
io_constraint (k == M_READ,
"SIGN= specifier at %L not allowed in a "
"READ statement", &dt->sign->where);
}
}
if (dt->delim)
{
if (!gfc_notify_std (GFC_STD_F2003, "DELIM= at %L "
"not allowed in Fortran 95", &dt->delim->where))
return false;
if (dt->delim->expr_type == EXPR_CONSTANT)
{
static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
dt->delim->value.character.string,
io_kind_name (k), warn,
&dt->delim->where))
return false;
io_constraint (k == M_READ,
"DELIM= specifier at %L not allowed in a "
"READ statement", &dt->delim->where);
io_constraint (dt->format_label != &format_asterisk
&& dt->namelist == NULL,
"DELIM= specifier at %L must have FMT=*",
&dt->delim->where);
io_constraint (unformatted && dt->namelist == NULL,
"DELIM= specifier at %L must be with FMT=* or "
"NML= specifier", &dt->delim->where);
}
}
if (dt->namelist)
{
io_constraint (io_code && dt->namelist,
"NAMELIST cannot be followed by IO-list at %L",
&io_code->loc);
io_constraint (dt->format_expr,
"IO spec-list cannot contain both NAMELIST group name "
"and format specification at %L",
&dt->format_expr->where);
io_constraint (dt->format_label,
"IO spec-list cannot contain both NAMELIST group name "
"and format label at %L", spec_end);
io_constraint (dt->rec,
"NAMELIST IO is not allowed with a REC= specifier "
"at %L", &dt->rec->where);
io_constraint (dt->advance,
"NAMELIST IO is not allowed with a ADVANCE= specifier "
"at %L", &dt->advance->where);
}
if (dt->rec)
{
io_constraint (dt->end,
"An END tag is not allowed with a "
"REC= specifier at %L", &dt->end_where);
io_constraint (dt->format_label == &format_asterisk,
"FMT=* is not allowed with a REC= specifier "
"at %L", spec_end);
io_constraint (dt->pos,
"POS= is not allowed with REC= specifier "
"at %L", &dt->pos->where);
}
if (dt->advance)
{
int not_yes, not_no;
expr = dt->advance;
io_constraint (dt->format_label == &format_asterisk,
"List directed format(*) is not allowed with a "
"ADVANCE= specifier at %L.", &expr->where);
io_constraint (unformatted,
"the ADVANCE= specifier at %L must appear with an "
"explicit format expression", &expr->where);
if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
{
const gfc_char_t *advance = expr->value.character.string;
not_no = gfc_wide_strlen (advance) != 2
|| gfc_wide_strncasecmp (advance, "no", 2) != 0;
not_yes = gfc_wide_strlen (advance) != 3
|| gfc_wide_strncasecmp (advance, "yes", 3) != 0;
}
else
{
not_no = 0;
not_yes = 0;
}
io_constraint (not_no && not_yes,
"ADVANCE= specifier at %L must have value = "
"YES or NO.", &expr->where);
io_constraint (dt->size && not_no && k == M_READ,
"SIZE tag at %L requires an ADVANCE = %<NO%>",
&dt->size->where);
io_constraint (dt->eor && not_no && k == M_READ,
"EOR tag at %L requires an ADVANCE = %<NO%>",
&dt->eor_where);
}
if (k != M_READ)
{
io_constraint (dt->end, "END tag not allowed with output at %L",
&dt->end_where);
io_constraint (dt->eor, "EOR tag not allowed with output at %L",
&dt->eor_where);
io_constraint (dt->blank,
"BLANK= specifier not allowed with output at %L",
&dt->blank->where);
io_constraint (dt->pad, "PAD= specifier not allowed with output at %L",
&dt->pad->where);
io_constraint (dt->size, "SIZE= specifier not allowed with output at %L",
&dt->size->where);
}
else
{
io_constraint (dt->size && dt->advance == NULL,
"SIZE tag at %L requires an ADVANCE tag",
&dt->size->where);
io_constraint (dt->eor && dt->advance == NULL,
"EOR tag at %L requires an ADVANCE tag",
&dt->eor_where);
}
return true;
#undef io_constraint
}
/* Match a READ, WRITE or PRINT statement. */
static match
match_io (io_kind k)
{
char name[GFC_MAX_SYMBOL_LEN + 1];
gfc_code *io_code;
gfc_symbol *sym;
int comma_flag;
locus where;
locus control;
gfc_dt *dt;
match m;
where = gfc_current_locus;
comma_flag = 0;
current_dt = dt = XCNEW (gfc_dt);
m = gfc_match_char ('(');
if (m == MATCH_NO)
{
where = gfc_current_locus;
if (k == M_WRITE)
goto syntax;
else if (k == M_PRINT)
{
/* Treat the non-standard case of PRINT namelist. */
if ((gfc_current_form == FORM_FIXED || gfc_peek_ascii_char () == ' ')
&& gfc_match_name (name) == MATCH_YES)
{
gfc_find_symbol (name, NULL, 1, &sym);
if (sym && sym->attr.flavor == FL_NAMELIST)
{
if (!gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
"%C is an extension"))
{
m = MATCH_ERROR;
goto cleanup;
}
dt->io_unit = default_unit (k);
dt->namelist = sym;
goto get_io_list;
}
else
gfc_current_locus = where;
}
if (gfc_match_char ('*') == MATCH_YES
&& gfc_match_char(',') == MATCH_YES)
{
locus where2 = gfc_current_locus;
if (gfc_match_eos () == MATCH_YES)
{
gfc_current_locus = where2;
gfc_error ("Comma after * at %C not allowed without I/O list");
m = MATCH_ERROR;
goto cleanup;
}
else
gfc_current_locus = where;
}
else
gfc_current_locus = where;
}
if (gfc_current_form == FORM_FREE)
{
char c = gfc_peek_ascii_char ();
if (c != ' ' && c != '*' && c != '\'' && c != '"')
{
m = MATCH_NO;
goto cleanup;
}
}
m = match_dt_format (dt);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
goto syntax;
comma_flag = 1;
dt->io_unit = default_unit (k);
goto get_io_list;
}
else
{
/* Before issuing an error for a malformed 'print (1,*)' type of
error, check for a default-char-expr of the form ('(I0)'). */
if (m == MATCH_YES)
{
control = gfc_current_locus;
if (k == M_PRINT)
{
/* Reset current locus to get the initial '(' in an expression. */
gfc_current_locus = where;
dt->format_expr = NULL;
m = match_dt_format (dt);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO || dt->format_expr == NULL)
goto syntax;
comma_flag = 1;
dt->io_unit = default_unit (k);
goto get_io_list;
}
if (k == M_READ)
{
/* Commit any pending symbols now so that when we undo
symbols later we wont lose them. */
gfc_commit_symbols ();
/* Reset current locus to get the initial '(' in an expression. */
gfc_current_locus = where;
dt->format_expr = NULL;
m = gfc_match_expr (&dt->format_expr);
if (m == MATCH_YES)
{
if (dt->format_expr
&& dt->format_expr->ts.type == BT_CHARACTER)
{
comma_flag = 1;
dt->io_unit = default_unit (k);
goto get_io_list;
}
else
{
gfc_free_expr (dt->format_expr);
dt->format_expr = NULL;
gfc_current_locus = control;
}
}
else
{
gfc_clear_error ();
gfc_undo_symbols ();
gfc_free_expr (dt->format_expr);
dt->format_expr = NULL;
gfc_current_locus = control;
}
}
}
}
/* Match a control list */
if (match_dt_element (k, dt) == MATCH_YES)
goto next;
if (match_dt_unit (k, dt) != MATCH_YES)
goto loop;
if (gfc_match_char (')') == MATCH_YES)
goto get_io_list;
if (gfc_match_char (',') != MATCH_YES)
goto syntax;
m = match_dt_element (k, dt);
if (m == MATCH_YES)
goto next;
if (m == MATCH_ERROR)
goto cleanup;
m = match_dt_format (dt);
if (m == MATCH_YES)
goto next;
if (m == MATCH_ERROR)
goto cleanup;
where = gfc_current_locus;
m = gfc_match_name (name);
if (m == MATCH_YES)
{
gfc_find_symbol (name, NULL, 1, &sym);
if (sym && sym->attr.flavor == FL_NAMELIST)
{
dt->namelist = sym;
if (k == M_READ && check_namelist (sym))
{
m = MATCH_ERROR;
goto cleanup;
}
goto next;
}
}
gfc_current_locus = where;
goto loop; /* No matches, try regular elements */
next:
if (gfc_match_char (')') == MATCH_YES)
goto get_io_list;
if (gfc_match_char (',') != MATCH_YES)
goto syntax;
loop:
for (;;)
{
m = match_dt_element (k, dt);
if (m == MATCH_NO)
goto syntax;
if (m == MATCH_ERROR)
goto cleanup;
if (gfc_match_char (')') == MATCH_YES)
break;
if (gfc_match_char (',') != MATCH_YES)
goto syntax;
}
get_io_list:
/* Save the IO kind for later use. */
dt->dt_io_kind = gfc_get_iokind_expr (&gfc_current_locus, k);
/* Optional leading comma (non-standard). We use a gfc_expr structure here
to save the locus. This is used later when resolving transfer statements
that might have a format expression without unit number. */
if (!comma_flag && gfc_match_char (',') == MATCH_YES)
dt->extra_comma = dt->dt_io_kind;
io_code = NULL;
if (gfc_match_eos () != MATCH_YES)
{
if (comma_flag && gfc_match_char (',') != MATCH_YES)
{
gfc_error ("Expected comma in I/O list at %C");
m = MATCH_ERROR;
goto cleanup;
}
m = match_io_list (k, &io_code);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
goto syntax;
}
/* See if we want to use defaults for missing exponents in real transfers
and other DEC runtime extensions. */
if (flag_dec_format_defaults)
dt->dec_ext = 1;
/* Check the format string now. */
if (dt->format_expr
&& (!gfc_simplify_expr (dt->format_expr, 0)
|| !check_format_string (dt->format_expr, k == M_READ)))
return MATCH_ERROR;
new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
new_st.ext.dt = dt;
new_st.block = gfc_get_code (new_st.op);
new_st.block->next = io_code;
terminate_io (io_code);
return MATCH_YES;
syntax:
gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
m = MATCH_ERROR;
cleanup:
gfc_free_dt (dt);
return m;
}
match
gfc_match_read (void)
{
return match_io (M_READ);
}
match
gfc_match_write (void)
{
return match_io (M_WRITE);
}
match
gfc_match_print (void)
{
match m;
m = match_io (M_PRINT);
if (m != MATCH_YES)
return m;
if (gfc_pure (NULL))
{
gfc_error ("PRINT statement at %C not allowed within PURE procedure");
return MATCH_ERROR;
}
gfc_unset_implicit_pure (NULL);
return MATCH_YES;
}
/* Free a gfc_inquire structure. */
void
gfc_free_inquire (gfc_inquire *inquire)
{
if (inquire == NULL)
return;
gfc_free_expr (inquire->unit);
gfc_free_expr (inquire->file);
gfc_free_expr (inquire->iomsg);
gfc_free_expr (inquire->iostat);
gfc_free_expr (inquire->exist);
gfc_free_expr (inquire->opened);
gfc_free_expr (inquire->number);
gfc_free_expr (inquire->named);
gfc_free_expr (inquire->name);
gfc_free_expr (inquire->access);
gfc_free_expr (inquire->sequential);
gfc_free_expr (inquire->direct);
gfc_free_expr (inquire->form);
gfc_free_expr (inquire->formatted);
gfc_free_expr (inquire->unformatted);
gfc_free_expr (inquire->recl);
gfc_free_expr (inquire->nextrec);
gfc_free_expr (inquire->blank);
gfc_free_expr (inquire->position);
gfc_free_expr (inquire->action);
gfc_free_expr (inquire->read);
gfc_free_expr (inquire->write);
gfc_free_expr (inquire->readwrite);
gfc_free_expr (inquire->delim);
gfc_free_expr (inquire->encoding);
gfc_free_expr (inquire->pad);
gfc_free_expr (inquire->iolength);
gfc_free_expr (inquire->convert);
gfc_free_expr (inquire->strm_pos);
gfc_free_expr (inquire->asynchronous);
gfc_free_expr (inquire->decimal);
gfc_free_expr (inquire->pending);
gfc_free_expr (inquire->id);
gfc_free_expr (inquire->sign);
gfc_free_expr (inquire->size);
gfc_free_expr (inquire->round);
gfc_free_expr (inquire->share);
gfc_free_expr (inquire->cc);
free (inquire);
}
/* Match an element of an INQUIRE statement. */
#define RETM if (m != MATCH_NO) return m;
static match
match_inquire_element (gfc_inquire *inquire)
{
match m;
m = match_etag (&tag_unit, &inquire->unit);
RETM m = match_etag (&tag_file, &inquire->file);
RETM m = match_ltag (&tag_err, &inquire->err);
RETM m = match_etag (&tag_iomsg, &inquire->iomsg);
RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
RETM m = match_vtag (&tag_exist, &inquire->exist);
RETM m = match_vtag (&tag_opened, &inquire->opened);
RETM m = match_vtag (&tag_named, &inquire->named);
RETM m = match_vtag (&tag_name, &inquire->name);
RETM m = match_out_tag (&tag_number, &inquire->number);
RETM m = match_vtag (&tag_s_access, &inquire->access);
RETM m = match_vtag (&tag_sequential, &inquire->sequential);
RETM m = match_vtag (&tag_direct, &inquire->direct);
RETM m = match_vtag (&tag_s_form, &inquire->form);
RETM m = match_vtag (&tag_formatted, &inquire->formatted);
RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
RETM m = match_vtag (&tag_s_blank, &inquire->blank);
RETM m = match_vtag (&tag_s_position, &inquire->position);
RETM m = match_vtag (&tag_s_action, &inquire->action);
RETM m = match_vtag (&tag_read, &inquire->read);
RETM m = match_vtag (&tag_write, &inquire->write);
RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
RETM m = match_vtag (&tag_s_async, &inquire->asynchronous);
RETM m = match_vtag (&tag_s_delim, &inquire->delim);
RETM m = match_vtag (&tag_s_decimal, &inquire->decimal);
RETM m = match_out_tag (&tag_size, &inquire->size);
RETM m = match_vtag (&tag_s_encoding, &inquire->encoding);
RETM m = match_vtag (&tag_s_round, &inquire->round);
RETM m = match_vtag (&tag_s_sign, &inquire->sign);
RETM m = match_vtag (&tag_s_pad, &inquire->pad);
RETM m = match_out_tag (&tag_iolength, &inquire->iolength);
RETM m = match_vtag (&tag_convert, &inquire->convert);
RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
RETM m = match_vtag (&tag_pending, &inquire->pending);
RETM m = match_vtag (&tag_id, &inquire->id);
RETM m = match_vtag (&tag_s_iqstream, &inquire->iqstream);
RETM m = match_dec_vtag (&tag_v_share, &inquire->share);
RETM m = match_dec_vtag (&tag_v_cc, &inquire->cc);
RETM return MATCH_NO;
}
#undef RETM
match
gfc_match_inquire (void)
{
gfc_inquire *inquire;
gfc_code *code;
match m;
locus loc;
m = gfc_match_char ('(');
if (m == MATCH_NO)
return m;
inquire = XCNEW (gfc_inquire);
loc = gfc_current_locus;
m = match_inquire_element (inquire);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
{
m = gfc_match_expr (&inquire->unit);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
goto syntax;
}
/* See if we have the IOLENGTH form of the inquire statement. */
if (inquire->iolength != NULL)
{
if (gfc_match_char (')') != MATCH_YES)
goto syntax;
m = match_io_list (M_INQUIRE, &code);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
goto syntax;
for (gfc_code *c = code; c; c = c->next)
if (c->expr1 && c->expr1->expr_type == EXPR_FUNCTION
&& c->expr1->symtree && c->expr1->symtree->n.sym->attr.function
&& !c->expr1->symtree->n.sym->attr.external
&& strcmp (c->expr1->symtree->name, "null") == 0)
{
gfc_error ("NULL() near %L cannot appear in INQUIRE statement",
&c->expr1->where);
goto cleanup;
}
new_st.op = EXEC_IOLENGTH;
new_st.expr1 = inquire->iolength;
new_st.ext.inquire = inquire;
if (gfc_pure (NULL))
{
gfc_free_statements (code);
gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
return MATCH_ERROR;
}
gfc_unset_implicit_pure (NULL);
new_st.block = gfc_get_code (EXEC_IOLENGTH);
terminate_io (code);
new_st.block->next = code;
return MATCH_YES;
}
/* At this point, we have the non-IOLENGTH inquire statement. */
for (;;)
{
if (gfc_match_char (')') == MATCH_YES)
break;
if (gfc_match_char (',') != MATCH_YES)
goto syntax;
m = match_inquire_element (inquire);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
goto syntax;
if (inquire->iolength != NULL)
{
gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
goto cleanup;
}
}
if (gfc_match_eos () != MATCH_YES)
goto syntax;
if (inquire->unit != NULL && inquire->file != NULL)
{
gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
"UNIT specifiers", &loc);
goto cleanup;
}
if (inquire->unit == NULL && inquire->file == NULL)
{
gfc_error ("INQUIRE statement at %L requires either FILE or "
"UNIT specifier", &loc);
goto cleanup;
}
if (inquire->unit != NULL && inquire->unit->expr_type == EXPR_CONSTANT
&& inquire->unit->ts.type == BT_INTEGER
&& ((mpz_get_si (inquire->unit->value.integer) == GFC_INTERNAL_UNIT4)
|| (mpz_get_si (inquire->unit->value.integer) == GFC_INTERNAL_UNIT)))
{
gfc_error ("UNIT number in INQUIRE statement at %L cannot "
"be %d", &loc, (int) mpz_get_si (inquire->unit->value.integer));
goto cleanup;
}
if (gfc_pure (NULL))
{
gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
goto cleanup;
}
gfc_unset_implicit_pure (NULL);
if (inquire->id != NULL && inquire->pending == NULL)
{
gfc_error ("INQUIRE statement at %L requires a PENDING= specifier with "
"the ID= specifier", &loc);
goto cleanup;
}
new_st.op = EXEC_INQUIRE;
new_st.ext.inquire = inquire;
return MATCH_YES;
syntax:
gfc_syntax_error (ST_INQUIRE);
cleanup:
gfc_free_inquire (inquire);
return MATCH_ERROR;
}
/* Resolve everything in a gfc_inquire structure. */
bool
gfc_resolve_inquire (gfc_inquire *inquire)
{
RESOLVE_TAG (&tag_unit, inquire->unit);
RESOLVE_TAG (&tag_file, inquire->file);
RESOLVE_TAG (&tag_id, inquire->id);
/* For INQUIRE, all tags except FILE, ID and UNIT are variable definition
contexts. Thus, use an extended RESOLVE_TAG macro for that. */
#define INQUIRE_RESOLVE_TAG(tag, expr) \
RESOLVE_TAG (tag, expr); \
if (expr) \
{ \
char context[64]; \
sprintf (context, _("%s tag with INQUIRE"), (tag)->name); \
if (gfc_check_vardef_context ((expr), false, false, false, \
context) == false) \
return false; \
}
INQUIRE_RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
INQUIRE_RESOLVE_TAG (&tag_iostat, inquire->iostat);
INQUIRE_RESOLVE_TAG (&tag_exist, inquire->exist);
INQUIRE_RESOLVE_TAG (&tag_opened, inquire->opened);
INQUIRE_RESOLVE_TAG (&tag_number, inquire->number);
INQUIRE_RESOLVE_TAG (&tag_named, inquire->named);
INQUIRE_RESOLVE_TAG (&tag_name, inquire->name);
INQUIRE_RESOLVE_TAG (&tag_s_access, inquire->access);
INQUIRE_RESOLVE_TAG (&tag_sequential, inquire->sequential);
INQUIRE_RESOLVE_TAG (&tag_direct, inquire->direct);
INQUIRE_RESOLVE_TAG (&tag_s_form, inquire->form);
INQUIRE_RESOLVE_TAG (&tag_formatted, inquire->formatted);
INQUIRE_RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
INQUIRE_RESOLVE_TAG (&tag_s_recl, inquire->recl);
INQUIRE_RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
INQUIRE_RESOLVE_TAG (&tag_s_blank, inquire->blank);
INQUIRE_RESOLVE_TAG (&tag_s_position, inquire->position);
INQUIRE_RESOLVE_TAG (&tag_s_action, inquire->action);
INQUIRE_RESOLVE_TAG (&tag_read, inquire->read);
INQUIRE_RESOLVE_TAG (&tag_write, inquire->write);
INQUIRE_RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
INQUIRE_RESOLVE_TAG (&tag_s_delim, inquire->delim);
INQUIRE_RESOLVE_TAG (&tag_s_pad, inquire->pad);
INQUIRE_RESOLVE_TAG (&tag_s_encoding, inquire->encoding);
INQUIRE_RESOLVE_TAG (&tag_s_round, inquire->round);
INQUIRE_RESOLVE_TAG (&tag_iolength, inquire->iolength);
INQUIRE_RESOLVE_TAG (&tag_convert, inquire->convert);
INQUIRE_RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
INQUIRE_RESOLVE_TAG (&tag_s_async, inquire->asynchronous);
INQUIRE_RESOLVE_TAG (&tag_s_sign, inquire->sign);
INQUIRE_RESOLVE_TAG (&tag_s_round, inquire->round);
INQUIRE_RESOLVE_TAG (&tag_pending, inquire->pending);
INQUIRE_RESOLVE_TAG (&tag_size, inquire->size);
INQUIRE_RESOLVE_TAG (&tag_s_decimal, inquire->decimal);
INQUIRE_RESOLVE_TAG (&tag_s_iqstream, inquire->iqstream);
INQUIRE_RESOLVE_TAG (&tag_v_share, inquire->share);
INQUIRE_RESOLVE_TAG (&tag_v_cc, inquire->cc);
#undef INQUIRE_RESOLVE_TAG
if (!gfc_reference_st_label (inquire->err, ST_LABEL_TARGET))
return false;
return true;
}
void
gfc_free_wait (gfc_wait *wait)
{
if (wait == NULL)
return;
gfc_free_expr (wait->unit);
gfc_free_expr (wait->iostat);
gfc_free_expr (wait->iomsg);
gfc_free_expr (wait->id);
free (wait);
}
bool
gfc_resolve_wait (gfc_wait *wait)
{
RESOLVE_TAG (&tag_unit, wait->unit);
RESOLVE_TAG (&tag_iomsg, wait->iomsg);
RESOLVE_TAG (&tag_iostat, wait->iostat);
RESOLVE_TAG (&tag_id, wait->id);
if (!gfc_reference_st_label (wait->err, ST_LABEL_TARGET))
return false;
if (!gfc_reference_st_label (wait->end, ST_LABEL_TARGET))
return false;
return true;
}
/* Match an element of a WAIT statement. */
#define RETM if (m != MATCH_NO) return m;
static match
match_wait_element (gfc_wait *wait)
{
match m;
m = match_etag (&tag_unit, &wait->unit);
RETM m = match_ltag (&tag_err, &wait->err);
RETM m = match_ltag (&tag_end, &wait->end);
RETM m = match_ltag (&tag_eor, &wait->eor);
RETM m = match_etag (&tag_iomsg, &wait->iomsg);
RETM m = match_out_tag (&tag_iostat, &wait->iostat);
RETM m = match_etag (&tag_id, &wait->id);
RETM return MATCH_NO;
}
#undef RETM
match
gfc_match_wait (void)
{
gfc_wait *wait;
match m;
m = gfc_match_char ('(');
if (m == MATCH_NO)
return m;
wait = XCNEW (gfc_wait);
m = match_wait_element (wait);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
{
m = gfc_match_expr (&wait->unit);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
goto syntax;
}
for (;;)
{
if (gfc_match_char (')') == MATCH_YES)
break;
if (gfc_match_char (',') != MATCH_YES)
goto syntax;
m = match_wait_element (wait);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
goto syntax;
}
if (!gfc_notify_std (GFC_STD_F2003, "WAIT at %C "
"not allowed in Fortran 95"))
goto cleanup;
if (gfc_pure (NULL))
{
gfc_error ("WAIT statement not allowed in PURE procedure at %C");
goto cleanup;
}
gfc_unset_implicit_pure (NULL);
new_st.op = EXEC_WAIT;
new_st.ext.wait = wait;
return MATCH_YES;
syntax:
gfc_syntax_error (ST_WAIT);
cleanup:
gfc_free_wait (wait);
return MATCH_ERROR;
}
|