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
|
/*
* $Id: c.c 689 2008-12-13 21:17:36Z elliotth $
*
* Copyright (c) 1996-2003, Darren Hiebert
*
* This source code is released for free distribution under the terms of the
* GNU General Public License.
*
* This module contains functions for parsing and scanning C, C++ and Java
* source files.
*/
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
#include <string.h>
#include <setjmp.h>
#include "debug.h"
#include "entry.h"
#include "get.h"
#include "keyword.h"
#include "options.h"
#include "parse.h"
#include "read.h"
#include "routines.h"
/*
* MACROS
*/
#define activeToken(st) ((st)->token [(int) (st)->tokenIndex])
#define parentDecl(st) ((st)->parent == NULL ? \
DECL_NONE : (st)->parent->declaration)
#define isType(token,t) (boolean) ((token)->type == (t))
#define insideEnumBody(st) ((st)->parent == NULL ? FALSE : \
(boolean) ((st)->parent->declaration == DECL_ENUM))
#define isExternCDecl(st,c) (boolean) ((c) == STRING_SYMBOL && \
! (st)->haveQualifyingName && (st)->scope == SCOPE_EXTERN)
#define isOneOf(c,s) (boolean) (strchr ((s), (c)) != NULL)
#define isHighChar(c) ((c) != EOF && (unsigned char)(c) >= 0xc0)
/*
* DATA DECLARATIONS
*/
enum { NumTokens = 15 };
typedef enum eException {
ExceptionNone, ExceptionEOF, ExceptionFormattingError,
ExceptionBraceFormattingError
} exception_t;
/* Used to specify type of keyword.
*/
typedef enum eKeywordId {
KEYWORD_NONE = -1,
KEYWORD_ATTRIBUTE, KEYWORD_ABSTRACT,
KEYWORD_BOOLEAN, KEYWORD_BYTE, KEYWORD_BAD_STATE, KEYWORD_BAD_TRANS,
KEYWORD_BIND, KEYWORD_BIND_VAR, KEYWORD_BIT,
KEYWORD_CASE, KEYWORD_CATCH, KEYWORD_CHAR, KEYWORD_CLASS, KEYWORD_CONST, KEYWORD_CONSTEXPR,
KEYWORD_CONSTRAINT, KEYWORD_COVERAGE_BLOCK, KEYWORD_COVERAGE_DEF,
KEYWORD_DEFAULT, KEYWORD_DELEGATE, KEYWORD_DELETE, KEYWORD_DO,
KEYWORD_DOUBLE,
KEYWORD_ELSE, KEYWORD_ENUM, KEYWORD_EXPLICIT, KEYWORD_EXTERN,
KEYWORD_EXTENDS, KEYWORD_EVENT,
KEYWORD_FINAL, KEYWORD_FLOAT, KEYWORD_FOR, KEYWORD_FOREACH,
KEYWORD_FRIEND, KEYWORD_FUNCTION,
KEYWORD_GOTO,
KEYWORD_IF, KEYWORD_IMPLEMENTS, KEYWORD_IMPORT, KEYWORD_INLINE, KEYWORD_INT,
KEYWORD_INOUT, KEYWORD_INPUT, KEYWORD_INTEGER, KEYWORD_INTERFACE,
KEYWORD_INTERNAL,
KEYWORD_LOCAL, KEYWORD_LONG,
KEYWORD_M_BAD_STATE, KEYWORD_M_BAD_TRANS, KEYWORD_M_STATE, KEYWORD_M_TRANS,
KEYWORD_MUTABLE,
KEYWORD_NAMESPACE, KEYWORD_NEW, KEYWORD_NEWCOV, KEYWORD_NATIVE, KEYWORD_NOEXCEPT,
KEYWORD_OPERATOR, KEYWORD_OUTPUT, KEYWORD_OVERLOAD, KEYWORD_OVERRIDE,
KEYWORD_PACKED, KEYWORD_PORT, KEYWORD_PACKAGE, KEYWORD_PRIVATE,
KEYWORD_PROGRAM, KEYWORD_PROTECTED, KEYWORD_PUBLIC,
KEYWORD_REGISTER, KEYWORD_RETURN,
KEYWORD_SHADOW, KEYWORD_STATE,
KEYWORD_SHORT, KEYWORD_SIGNED, KEYWORD_STATIC, KEYWORD_STATIC_ASSERT, KEYWORD_STRING,
KEYWORD_STRUCT, KEYWORD_SWITCH, KEYWORD_SYNCHRONIZED,
KEYWORD_TASK, KEYWORD_TEMPLATE, KEYWORD_THIS, KEYWORD_THROW,
KEYWORD_THROWS, KEYWORD_TRANSIENT, KEYWORD_TRANS, KEYWORD_TRANSITION,
KEYWORD_TRY, KEYWORD_TYPEDEF, KEYWORD_TYPENAME,
KEYWORD_UINT, KEYWORD_ULONG, KEYWORD_UNION, KEYWORD_UNSIGNED, KEYWORD_USHORT,
KEYWORD_USING,
KEYWORD_VIRTUAL, KEYWORD_VOID, KEYWORD_VOLATILE,
KEYWORD_WCHAR_T, KEYWORD_WHILE
} keywordId;
/* Used to determine whether keyword is valid for the current language and
* what its ID is.
*/
typedef struct sKeywordDesc {
const char *name;
keywordId id;
short isValid [5]; /* indicates languages for which kw is valid */
} keywordDesc;
/* Used for reporting the type of object parsed by nextToken ().
*/
typedef enum eTokenType {
TOKEN_NONE, /* none */
TOKEN_ARGS, /* a parenthetical pair and its contents */
TOKEN_BRACE_CLOSE,
TOKEN_BRACE_OPEN,
TOKEN_COLON, /* the colon character */
TOKEN_COMMA, /* the comma character */
TOKEN_DOUBLE_COLON, /* double colon indicates nested-name-specifier */
TOKEN_KEYWORD,
TOKEN_NAME, /* an unknown name */
TOKEN_PACKAGE, /* a Java package name */
TOKEN_PAREN_NAME, /* a single name in parentheses */
TOKEN_SEMICOLON, /* the semicolon character */
TOKEN_SPEC, /* a storage class specifier, qualifier, type, etc. */
TOKEN_STAR, /* pointer * detection */
TOKEN_AMPERSAND, /* ampersand & detection */
TOKEN_COUNT
} tokenType;
/* This describes the scoping of the current statement.
*/
typedef enum eTagScope {
SCOPE_GLOBAL, /* no storage class specified */
SCOPE_STATIC, /* static storage class */
SCOPE_EXTERN, /* external storage class */
SCOPE_FRIEND, /* declares access only */
SCOPE_TYPEDEF, /* scoping depends upon context */
SCOPE_COUNT
} tagScope;
typedef enum eDeclaration {
DECL_NONE,
DECL_BASE, /* base type (default) */
DECL_CLASS,
DECL_ENUM,
DECL_EVENT,
DECL_FUNCTION,
DECL_IGNORE, /* non-taggable "declaration" */
DECL_INTERFACE,
DECL_NAMESPACE,
DECL_NOMANGLE, /* C++ name demangling block */
DECL_PACKAGE,
DECL_PROGRAM, /* Vera program */
DECL_STRUCT,
DECL_TASK, /* Vera task */
DECL_UNION,
DECL_COUNT
} declType;
typedef enum eVisibilityType {
ACCESS_UNDEFINED,
ACCESS_LOCAL,
ACCESS_PRIVATE,
ACCESS_PROTECTED,
ACCESS_PUBLIC,
ACCESS_DEFAULT, /* Java-specific */
ACCESS_COUNT
} accessType;
/* Information about the parent class of a member (if any).
*/
typedef struct sMemberInfo {
accessType access; /* access of current statement */
accessType accessDefault; /* access default for current statement */
} memberInfo;
typedef struct sTokenInfo {
tokenType type;
keywordId keyword;
vString* name; /* the name of the token */
unsigned long lineNumber; /* line number of tag */
fpos_t filePosition; /* file position of line containing name */
} tokenInfo;
typedef enum eImplementation {
IMP_DEFAULT,
IMP_ABSTRACT,
IMP_VIRTUAL,
IMP_PURE_VIRTUAL,
IMP_COUNT
} impType;
/* Describes the statement currently undergoing analysis.
*/
typedef struct sStatementInfo {
tagScope scope;
declType declaration; /* specifier associated with TOKEN_SPEC */
boolean gotName; /* was a name parsed yet? */
boolean haveQualifyingName; /* do we have a name we are considering? */
boolean gotParenName; /* was a name inside parentheses parsed yet? */
boolean gotArgs; /* was a list of parameters parsed yet? */
boolean isPointer; /* is 'name' a pointer? */
boolean inFunction; /* are we inside of a function? */
boolean assignment; /* have we handled an '='? */
boolean notVariable; /* has a variable declaration been disqualified ? */
impType implementation; /* abstract or concrete implementation? */
unsigned int tokenIndex; /* currently active token */
tokenInfo* token [(int) NumTokens];
tokenInfo* context; /* accumulated scope of current statement */
tokenInfo* blockName; /* name of current block */
memberInfo member; /* information regarding parent class/struct */
vString* parentClasses; /* parent classes */
struct sStatementInfo *parent; /* statement we are nested within */
} statementInfo;
/* Describes the type of tag being generated.
*/
typedef enum eTagType {
TAG_UNDEFINED,
TAG_CLASS, /* class name */
TAG_ENUM, /* enumeration name */
TAG_ENUMERATOR, /* enumerator (enumeration value) */
TAG_EVENT, /* event */
TAG_FIELD, /* field (Java) */
TAG_FUNCTION, /* function definition */
TAG_INTERFACE, /* interface declaration */
TAG_LOCAL, /* local variable definition */
TAG_MEMBER, /* structure, class or interface member */
TAG_METHOD, /* method declaration */
TAG_NAMESPACE, /* namespace name */
TAG_PACKAGE, /* package name */
TAG_PROGRAM, /* program name */
TAG_PROPERTY, /* property name */
TAG_PROTOTYPE, /* function prototype or declaration */
TAG_STRUCT, /* structure name */
TAG_TASK, /* task name */
TAG_TYPEDEF, /* typedef name */
TAG_UNION, /* union name */
TAG_VARIABLE, /* variable definition */
TAG_EXTERN_VAR, /* external variable declaration */
TAG_COUNT /* must be last */
} tagType;
typedef struct sParenInfo {
boolean isPointer;
boolean isParamList;
boolean isKnrParamList;
boolean isNameCandidate;
boolean invalidContents;
boolean nestedArgs;
unsigned int parameterCount;
} parenInfo;
/*
* DATA DEFINITIONS
*/
static jmp_buf Exception;
static langType Lang_c;
static langType Lang_cpp;
static langType Lang_csharp;
static langType Lang_java;
static langType Lang_vera;
static vString *Signature;
static boolean CollectingSignature;
static vString *ReturnType;
/* Number used to uniquely identify anonymous structs and unions. */
static int AnonymousID = 0;
/* Used to index into the CKinds table. */
typedef enum {
CK_UNDEFINED = -1,
CK_CLASS, CK_DEFINE, CK_ENUMERATOR, CK_FUNCTION,
CK_ENUMERATION, CK_LOCAL, CK_MEMBER, CK_NAMESPACE, CK_PROTOTYPE,
CK_STRUCT, CK_TYPEDEF, CK_UNION, CK_VARIABLE,
CK_EXTERN_VARIABLE
} cKind;
static kindOption CKinds [] = {
{ TRUE, 'c', "class", "classes"},
{ TRUE, 'd', "macro", "macro definitions"},
{ TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
{ TRUE, 'f', "function", "function definitions"},
{ TRUE, 'g', "enum", "enumeration names"},
{ FALSE, 'l', "local", "local variables"},
{ TRUE, 'm', "member", "class, struct, and union members"},
{ TRUE, 'n', "namespace", "namespaces"},
{ FALSE, 'p', "prototype", "function prototypes"},
{ TRUE, 's', "struct", "structure names"},
{ TRUE, 't', "typedef", "typedefs"},
{ TRUE, 'u', "union", "union names"},
{ TRUE, 'v', "variable", "variable definitions"},
{ FALSE, 'x', "externvar", "external and forward variable declarations"},
};
typedef enum {
CSK_UNDEFINED = -1,
CSK_CLASS, CSK_DEFINE, CSK_ENUMERATOR, CSK_EVENT, CSK_FIELD,
CSK_ENUMERATION, CSK_INTERFACE, CSK_LOCAL, CSK_METHOD,
CSK_NAMESPACE, CSK_PROPERTY, CSK_STRUCT, CSK_TYPEDEF
} csharpKind;
static kindOption CsharpKinds [] = {
{ TRUE, 'c', "class", "classes"},
{ TRUE, 'd', "macro", "macro definitions"},
{ TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
{ TRUE, 'E', "event", "events"},
{ TRUE, 'f', "field", "fields"},
{ TRUE, 'g', "enum", "enumeration names"},
{ TRUE, 'i', "interface", "interfaces"},
{ FALSE, 'l', "local", "local variables"},
{ TRUE, 'm', "method", "methods"},
{ TRUE, 'n', "namespace", "namespaces"},
{ TRUE, 'p', "property", "properties"},
{ TRUE, 's', "struct", "structure names"},
{ TRUE, 't', "typedef", "typedefs"},
};
/* Used to index into the JavaKinds table. */
typedef enum {
JK_UNDEFINED = -1,
JK_CLASS, JK_ENUM_CONSTANT, JK_FIELD, JK_ENUM, JK_INTERFACE,
JK_LOCAL, JK_METHOD, JK_PACKAGE, JK_ACCESS, JK_CLASS_PREFIX
} javaKind;
static kindOption JavaKinds [] = {
{ TRUE, 'c', "class", "classes"},
{ TRUE, 'e', "enum constant", "enum constants"},
{ TRUE, 'f', "field", "fields"},
{ TRUE, 'g', "enum", "enum types"},
{ TRUE, 'i', "interface", "interfaces"},
{ FALSE, 'l', "local", "local variables"},
{ TRUE, 'm', "method", "methods"},
{ TRUE, 'p', "package", "packages"},
};
/* Used to index into the VeraKinds table. */
typedef enum {
VK_UNDEFINED = -1,
VK_CLASS, VK_DEFINE, VK_ENUMERATOR, VK_FUNCTION,
VK_ENUMERATION, VK_LOCAL, VK_MEMBER, VK_PROGRAM, VK_PROTOTYPE,
VK_TASK, VK_TYPEDEF, VK_VARIABLE,
VK_EXTERN_VARIABLE
} veraKind;
static kindOption VeraKinds [] = {
{ TRUE, 'c', "class", "classes"},
{ TRUE, 'd', "macro", "macro definitions"},
{ TRUE, 'e', "enumerator", "enumerators (values inside an enumeration)"},
{ TRUE, 'f', "function", "function definitions"},
{ TRUE, 'g', "enum", "enumeration names"},
{ FALSE, 'l', "local", "local variables"},
{ TRUE, 'm', "member", "class, struct, and union members"},
{ TRUE, 'p', "program", "programs"},
{ FALSE, 'P', "prototype", "function prototypes"},
{ TRUE, 't', "task", "tasks"},
{ TRUE, 'T', "typedef", "typedefs"},
{ TRUE, 'v', "variable", "variable definitions"},
{ FALSE, 'x', "externvar", "external variable declarations"}
};
static const keywordDesc KeywordTable [] = {
/* C++ */
/* ANSI C | C# Java */
/* | | | | Vera */
/* keyword keyword ID | | | | | */
{ "__attribute__", KEYWORD_ATTRIBUTE, { 1, 1, 1, 0, 0 } },
{ "abstract", KEYWORD_ABSTRACT, { 0, 0, 1, 1, 0 } },
{ "bad_state", KEYWORD_BAD_STATE, { 0, 0, 0, 0, 1 } },
{ "bad_trans", KEYWORD_BAD_TRANS, { 0, 0, 0, 0, 1 } },
{ "bind", KEYWORD_BIND, { 0, 0, 0, 0, 1 } },
{ "bind_var", KEYWORD_BIND_VAR, { 0, 0, 0, 0, 1 } },
{ "bit", KEYWORD_BIT, { 0, 0, 0, 0, 1 } },
{ "boolean", KEYWORD_BOOLEAN, { 0, 0, 0, 1, 0 } },
{ "byte", KEYWORD_BYTE, { 0, 0, 0, 1, 0 } },
{ "case", KEYWORD_CASE, { 1, 1, 1, 1, 0 } },
{ "catch", KEYWORD_CATCH, { 0, 1, 1, 0, 0 } },
{ "char", KEYWORD_CHAR, { 1, 1, 1, 1, 0 } },
{ "class", KEYWORD_CLASS, { 0, 1, 1, 1, 1 } },
{ "const", KEYWORD_CONST, { 1, 1, 1, 1, 0 } },
{ "constexpr", KEYWORD_CONSTEXPR, { 0, 1, 0, 0, 0 } },
{ "constraint", KEYWORD_CONSTRAINT, { 0, 0, 0, 0, 1 } },
{ "coverage_block", KEYWORD_COVERAGE_BLOCK, { 0, 0, 0, 0, 1 } },
{ "coverage_def", KEYWORD_COVERAGE_DEF, { 0, 0, 0, 0, 1 } },
{ "do", KEYWORD_DO, { 1, 1, 1, 1, 0 } },
{ "default", KEYWORD_DEFAULT, { 1, 1, 1, 1, 0 } },
{ "delegate", KEYWORD_DELEGATE, { 0, 0, 1, 0, 0 } },
{ "delete", KEYWORD_DELETE, { 0, 1, 0, 0, 0 } },
{ "double", KEYWORD_DOUBLE, { 1, 1, 1, 1, 0 } },
{ "else", KEYWORD_ELSE, { 1, 1, 1, 1, 0 } },
{ "enum", KEYWORD_ENUM, { 1, 1, 1, 1, 1 } },
{ "event", KEYWORD_EVENT, { 0, 0, 1, 0, 1 } },
{ "explicit", KEYWORD_EXPLICIT, { 0, 1, 1, 0, 0 } },
{ "extends", KEYWORD_EXTENDS, { 0, 0, 0, 1, 1 } },
{ "extern", KEYWORD_EXTERN, { 1, 1, 1, 0, 1 } },
{ "final", KEYWORD_FINAL, { 0, 0, 0, 1, 0 } },
{ "float", KEYWORD_FLOAT, { 1, 1, 1, 1, 0 } },
{ "for", KEYWORD_FOR, { 1, 1, 1, 1, 0 } },
{ "foreach", KEYWORD_FOREACH, { 0, 0, 1, 0, 0 } },
{ "friend", KEYWORD_FRIEND, { 0, 1, 0, 0, 0 } },
{ "function", KEYWORD_FUNCTION, { 0, 0, 0, 0, 1 } },
{ "goto", KEYWORD_GOTO, { 1, 1, 1, 1, 0 } },
{ "if", KEYWORD_IF, { 1, 1, 1, 1, 0 } },
{ "implements", KEYWORD_IMPLEMENTS, { 0, 0, 0, 1, 0 } },
{ "import", KEYWORD_IMPORT, { 0, 0, 0, 1, 0 } },
{ "inline", KEYWORD_INLINE, { 0, 1, 0, 0, 0 } },
{ "inout", KEYWORD_INOUT, { 0, 0, 0, 0, 1 } },
{ "input", KEYWORD_INPUT, { 0, 0, 0, 0, 1 } },
{ "int", KEYWORD_INT, { 1, 1, 1, 1, 0 } },
{ "integer", KEYWORD_INTEGER, { 0, 0, 0, 0, 1 } },
{ "interface", KEYWORD_INTERFACE, { 0, 0, 1, 1, 1 } },
{ "internal", KEYWORD_INTERNAL, { 0, 0, 1, 0, 0 } },
{ "local", KEYWORD_LOCAL, { 0, 0, 0, 0, 1 } },
{ "long", KEYWORD_LONG, { 1, 1, 1, 1, 0 } },
{ "m_bad_state", KEYWORD_M_BAD_STATE, { 0, 0, 0, 0, 1 } },
{ "m_bad_trans", KEYWORD_M_BAD_TRANS, { 0, 0, 0, 0, 1 } },
{ "m_state", KEYWORD_M_STATE, { 0, 0, 0, 0, 1 } },
{ "m_trans", KEYWORD_M_TRANS, { 0, 0, 0, 0, 1 } },
{ "mutable", KEYWORD_MUTABLE, { 0, 1, 0, 0, 0 } },
{ "namespace", KEYWORD_NAMESPACE, { 0, 1, 1, 0, 0 } },
{ "native", KEYWORD_NATIVE, { 0, 0, 0, 1, 0 } },
{ "new", KEYWORD_NEW, { 0, 1, 1, 1, 0 } },
{ "newcov", KEYWORD_NEWCOV, { 0, 0, 0, 0, 1 } },
{ "noexcept", KEYWORD_NOEXCEPT, { 0, 1, 0, 0, 0 } },
{ "operator", KEYWORD_OPERATOR, { 0, 1, 1, 0, 0 } },
{ "output", KEYWORD_OUTPUT, { 0, 0, 0, 0, 1 } },
{ "overload", KEYWORD_OVERLOAD, { 0, 1, 0, 0, 0 } },
{ "override", KEYWORD_OVERRIDE, { 0, 0, 1, 0, 0 } },
{ "package", KEYWORD_PACKAGE, { 0, 0, 0, 1, 0 } },
{ "packed", KEYWORD_PACKED, { 0, 0, 0, 0, 1 } },
{ "port", KEYWORD_PORT, { 0, 0, 0, 0, 1 } },
{ "private", KEYWORD_PRIVATE, { 0, 1, 1, 1, 0 } },
{ "program", KEYWORD_PROGRAM, { 0, 0, 0, 0, 1 } },
{ "protected", KEYWORD_PROTECTED, { 0, 1, 1, 1, 1 } },
{ "public", KEYWORD_PUBLIC, { 0, 1, 1, 1, 1 } },
{ "register", KEYWORD_REGISTER, { 1, 1, 0, 0, 0 } },
{ "return", KEYWORD_RETURN, { 1, 1, 1, 1, 0 } },
{ "shadow", KEYWORD_SHADOW, { 0, 0, 0, 0, 1 } },
{ "short", KEYWORD_SHORT, { 1, 1, 1, 1, 0 } },
{ "signed", KEYWORD_SIGNED, { 1, 1, 0, 0, 0 } },
{ "state", KEYWORD_STATE, { 0, 0, 0, 0, 1 } },
{ "static", KEYWORD_STATIC, { 1, 1, 1, 1, 1 } },
{ "static_assert", KEYWORD_STATIC_ASSERT, { 0, 1, 0, 0, 0} },
{ "string", KEYWORD_STRING, { 0, 0, 1, 0, 1 } },
{ "struct", KEYWORD_STRUCT, { 1, 1, 1, 0, 0 } },
{ "switch", KEYWORD_SWITCH, { 1, 1, 1, 1, 0 } },
{ "synchronized", KEYWORD_SYNCHRONIZED, { 0, 0, 0, 1, 0 } },
{ "task", KEYWORD_TASK, { 0, 0, 0, 0, 1 } },
{ "template", KEYWORD_TEMPLATE, { 0, 1, 0, 0, 0 } },
{ "this", KEYWORD_THIS, { 0, 1, 1, 1, 0 } },
{ "throw", KEYWORD_THROW, { 0, 1, 1, 1, 0 } },
{ "throws", KEYWORD_THROWS, { 0, 0, 0, 1, 0 } },
{ "trans", KEYWORD_TRANS, { 0, 0, 0, 0, 1 } },
{ "transition", KEYWORD_TRANSITION, { 0, 0, 0, 0, 1 } },
{ "transient", KEYWORD_TRANSIENT, { 0, 0, 0, 1, 0 } },
{ "try", KEYWORD_TRY, { 0, 1, 1, 0, 0 } },
{ "typedef", KEYWORD_TYPEDEF, { 1, 1, 1, 0, 1 } },
{ "typename", KEYWORD_TYPENAME, { 0, 1, 0, 0, 0 } },
{ "uint", KEYWORD_UINT, { 0, 0, 1, 0, 0 } },
{ "ulong", KEYWORD_ULONG, { 0, 0, 1, 0, 0 } },
{ "union", KEYWORD_UNION, { 1, 1, 0, 0, 0 } },
{ "unsigned", KEYWORD_UNSIGNED, { 1, 1, 1, 0, 0 } },
{ "ushort", KEYWORD_USHORT, { 0, 0, 1, 0, 0 } },
{ "using", KEYWORD_USING, { 0, 1, 1, 0, 0 } },
{ "virtual", KEYWORD_VIRTUAL, { 0, 1, 1, 0, 1 } },
{ "void", KEYWORD_VOID, { 1, 1, 1, 1, 1 } },
{ "volatile", KEYWORD_VOLATILE, { 1, 1, 1, 1, 0 } },
{ "wchar_t", KEYWORD_WCHAR_T, { 1, 1, 1, 0, 0 } },
{ "while", KEYWORD_WHILE, { 1, 1, 1, 1, 0 } }
};
/*
* FUNCTION PROTOTYPES
*/
static void createTags (const unsigned int nestLevel, statementInfo *const parent);
/*
* FUNCTION DEFINITIONS
*/
extern boolean includingDefineTags (void)
{
return CKinds [CK_DEFINE].enabled;
}
/*
* Token management
*/
static void initToken (tokenInfo* const token)
{
token->type = TOKEN_NONE;
token->keyword = KEYWORD_NONE;
token->lineNumber = getSourceLineNumber ();
token->filePosition = getInputFilePosition ();
vStringClear (token->name);
}
static void advanceToken (statementInfo* const st)
{
if (st->tokenIndex >= (unsigned int) NumTokens - 1)
st->tokenIndex = 0;
else
++st->tokenIndex;
initToken (st->token [st->tokenIndex]);
}
static tokenInfo *prevToken (const statementInfo *const st, unsigned int n)
{
unsigned int tokenIndex;
unsigned int num = (unsigned int) NumTokens;
Assert (n < num);
tokenIndex = (st->tokenIndex + num - n) % num;
return st->token [tokenIndex];
}
static void setToken (statementInfo *const st, const tokenType type)
{
tokenInfo *token;
token = activeToken (st);
initToken (token);
token->type = type;
}
static void retardToken (statementInfo *const st)
{
if (st->tokenIndex == 0)
st->tokenIndex = (unsigned int) NumTokens - 1;
else
--st->tokenIndex;
setToken (st, TOKEN_NONE);
}
static tokenInfo *newToken (void)
{
tokenInfo *const token = xMalloc (1, tokenInfo);
token->name = vStringNew ();
initToken (token);
return token;
}
static void deleteToken (tokenInfo *const token)
{
if (token != NULL)
{
vStringDelete (token->name);
eFree (token);
}
}
static const char *accessString (const accessType access)
{
static const char *const names [] = {
"?", "local", "private", "protected", "public", "default"
};
Assert (sizeof (names) / sizeof (names [0]) == ACCESS_COUNT);
Assert ((int) access < ACCESS_COUNT);
return names [(int) access];
}
static const char *implementationString (const impType imp)
{
static const char *const names [] ={
"?", "abstract", "virtual", "pure virtual"
};
Assert (sizeof (names) / sizeof (names [0]) == IMP_COUNT);
Assert ((int) imp < IMP_COUNT);
return names [(int) imp];
}
/*
* Debugging functions
*/
#ifdef DEBUG
#define boolString(c) ((c) ? "TRUE" : "FALSE")
static const char *tokenString (const tokenType type)
{
static const char *const names [] = {
"none", "args", "}", "{", "colon", "comma", "double colon", "keyword",
"name", "package", "paren-name", "semicolon", "specifier", "star", "ampersand"
};
Assert (sizeof (names) / sizeof (names [0]) == TOKEN_COUNT);
Assert ((int) type < TOKEN_COUNT);
return names [(int) type];
}
static const char *scopeString (const tagScope scope)
{
static const char *const names [] = {
"global", "static", "extern", "friend", "typedef"
};
Assert (sizeof (names) / sizeof (names [0]) == SCOPE_COUNT);
Assert ((int) scope < SCOPE_COUNT);
return names [(int) scope];
}
static const char *declString (const declType declaration)
{
static const char *const names [] = {
"?", "base", "class", "enum", "event", "function", "ignore",
"interface", "namespace", "no mangle", "package", "program",
"struct", "task", "union",
};
Assert (sizeof (names) / sizeof (names [0]) == DECL_COUNT);
Assert ((int) declaration < DECL_COUNT);
return names [(int) declaration];
}
static const char *keywordString (const keywordId keyword)
{
const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
const char *name = "none";
size_t i;
for (i = 0 ; i < count ; ++i)
{
const keywordDesc *p = &KeywordTable [i];
if (p->id == keyword)
{
name = p->name;
break;
}
}
return name;
}
static void __unused__ pt (tokenInfo *const token)
{
if (isType (token, TOKEN_NAME))
printf ("type: %-12s: %-13s line: %lu\n",
tokenString (token->type), vStringValue (token->name),
token->lineNumber);
else if (isType (token, TOKEN_KEYWORD))
printf ("type: %-12s: %-13s line: %lu\n",
tokenString (token->type), keywordString (token->keyword),
token->lineNumber);
else
printf ("type: %-12s line: %lu\n",
tokenString (token->type), token->lineNumber);
}
static void __unused__ ps (statementInfo *const st)
{
unsigned int i;
printf ("scope: %s decl: %s gotName: %s gotParenName: %s isPointer: %s\n",
scopeString (st->scope), declString (st->declaration),
boolString (st->gotName), boolString (st->gotParenName), boolString (st->isPointer));
printf ("haveQualifyingName: %s\n", boolString (st->haveQualifyingName));
printf ("access: %s default: %s\n", accessString (st->member.access),
accessString (st->member.accessDefault));
printf ("active token : ");
pt (activeToken (st));
for (i = 1 ; i < (unsigned int) NumTokens ; ++i)
{
printf ("prev %u : ", i);
pt (prevToken (st, i));
}
printf ("context: ");
pt (st->context);
}
#endif
/*
* Statement management
*/
static boolean isContextualKeyword (const tokenInfo *const token)
{
boolean result;
switch (token->keyword)
{
case KEYWORD_CLASS:
case KEYWORD_ENUM:
case KEYWORD_INTERFACE:
case KEYWORD_NAMESPACE:
case KEYWORD_STRUCT:
case KEYWORD_UNION:
result = TRUE;
break;
default: result = FALSE; break;
}
return result;
}
static boolean isContextualStatement (const statementInfo *const st)
{
boolean result = FALSE;
if (st != NULL) switch (st->declaration)
{
case DECL_CLASS:
case DECL_ENUM:
case DECL_INTERFACE:
case DECL_NAMESPACE:
case DECL_STRUCT:
case DECL_UNION:
result = TRUE;
break;
default: result = FALSE; break;
}
return result;
}
static boolean isMember (const statementInfo *const st)
{
boolean result;
if (isType (st->context, TOKEN_NAME))
result = TRUE;
else
result = (boolean)
(st->parent != NULL && isContextualStatement (st->parent));
return result;
}
static void initMemberInfo (statementInfo *const st)
{
accessType accessDefault = ACCESS_UNDEFINED;
if (st->parent != NULL) switch (st->parent->declaration)
{
case DECL_ENUM:
accessDefault = (isLanguage (Lang_java) ? ACCESS_PUBLIC : ACCESS_UNDEFINED);
break;
case DECL_NAMESPACE:
accessDefault = ACCESS_UNDEFINED;
break;
case DECL_CLASS:
if (isLanguage (Lang_java))
accessDefault = ACCESS_DEFAULT;
else
accessDefault = ACCESS_PRIVATE;
break;
case DECL_INTERFACE:
case DECL_STRUCT:
case DECL_UNION:
accessDefault = ACCESS_PUBLIC;
break;
default: break;
}
st->member.accessDefault = accessDefault;
st->member.access = accessDefault;
}
static void reinitStatement (statementInfo *const st, const boolean partial)
{
unsigned int i;
if (! partial)
{
st->scope = SCOPE_GLOBAL;
if (isContextualStatement (st->parent))
st->declaration = DECL_BASE;
else
st->declaration = DECL_NONE;
}
st->gotParenName = FALSE;
st->isPointer = FALSE;
st->inFunction = FALSE;
st->assignment = FALSE;
st->notVariable = FALSE;
st->implementation = IMP_DEFAULT;
st->gotArgs = FALSE;
st->gotName = FALSE;
st->haveQualifyingName = FALSE;
st->tokenIndex = 0;
if (st->parent != NULL)
st->inFunction = st->parent->inFunction;
for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
initToken (st->token [i]);
initToken (st->context);
/* Keep the block name, so that a variable following after a comma will
* still have the structure name.
*/
if (! partial)
initToken (st->blockName);
vStringClear (st->parentClasses);
/* Init member info.
*/
if (! partial)
st->member.access = st->member.accessDefault;
}
static void initStatement (statementInfo *const st, statementInfo *const parent)
{
st->parent = parent;
initMemberInfo (st);
reinitStatement (st, FALSE);
}
/*
* Tag generation functions
*/
static cKind cTagKind (const tagType type)
{
cKind result = CK_UNDEFINED;
switch (type)
{
case TAG_CLASS: result = CK_CLASS; break;
case TAG_ENUM: result = CK_ENUMERATION; break;
case TAG_ENUMERATOR: result = CK_ENUMERATOR; break;
case TAG_FUNCTION: result = CK_FUNCTION; break;
case TAG_LOCAL: result = CK_LOCAL; break;
case TAG_MEMBER: result = CK_MEMBER; break;
case TAG_NAMESPACE: result = CK_NAMESPACE; break;
case TAG_PROTOTYPE: result = CK_PROTOTYPE; break;
case TAG_STRUCT: result = CK_STRUCT; break;
case TAG_TYPEDEF: result = CK_TYPEDEF; break;
case TAG_UNION: result = CK_UNION; break;
case TAG_VARIABLE: result = CK_VARIABLE; break;
case TAG_EXTERN_VAR: result = CK_EXTERN_VARIABLE; break;
default: Assert ("Bad C tag type" == NULL); break;
}
return result;
}
static csharpKind csharpTagKind (const tagType type)
{
csharpKind result = CSK_UNDEFINED;
switch (type)
{
case TAG_CLASS: result = CSK_CLASS; break;
case TAG_ENUM: result = CSK_ENUMERATION; break;
case TAG_ENUMERATOR: result = CSK_ENUMERATOR; break;
case TAG_EVENT: result = CSK_EVENT; break;
case TAG_FIELD: result = CSK_FIELD ; break;
case TAG_INTERFACE: result = CSK_INTERFACE; break;
case TAG_LOCAL: result = CSK_LOCAL; break;
case TAG_METHOD: result = CSK_METHOD; break;
case TAG_NAMESPACE: result = CSK_NAMESPACE; break;
case TAG_PROPERTY: result = CSK_PROPERTY; break;
case TAG_STRUCT: result = CSK_STRUCT; break;
case TAG_TYPEDEF: result = CSK_TYPEDEF; break;
default: Assert ("Bad C# tag type" == NULL); break;
}
return result;
}
static javaKind javaTagKind (const tagType type)
{
javaKind result = JK_UNDEFINED;
switch (type)
{
case TAG_CLASS: result = JK_CLASS; break;
case TAG_ENUM: result = JK_ENUM; break;
case TAG_ENUMERATOR: result = JK_ENUM_CONSTANT; break;
case TAG_FIELD: result = JK_FIELD; break;
case TAG_INTERFACE: result = JK_INTERFACE; break;
case TAG_LOCAL: result = JK_LOCAL; break;
case TAG_METHOD: result = JK_METHOD; break;
case TAG_PACKAGE: result = JK_PACKAGE; break;
default: Assert ("Bad Java tag type" == NULL); break;
}
return result;
}
static veraKind veraTagKind (const tagType type) {
veraKind result = VK_UNDEFINED;
switch (type)
{
case TAG_CLASS: result = VK_CLASS; break;
case TAG_ENUM: result = VK_ENUMERATION; break;
case TAG_ENUMERATOR: result = VK_ENUMERATOR; break;
case TAG_FUNCTION: result = VK_FUNCTION; break;
case TAG_LOCAL: result = VK_LOCAL; break;
case TAG_MEMBER: result = VK_MEMBER; break;
case TAG_PROGRAM: result = VK_PROGRAM; break;
case TAG_PROTOTYPE: result = VK_PROTOTYPE; break;
case TAG_TASK: result = VK_TASK; break;
case TAG_TYPEDEF: result = VK_TYPEDEF; break;
case TAG_VARIABLE: result = VK_VARIABLE; break;
case TAG_EXTERN_VAR: result = VK_EXTERN_VARIABLE; break;
default: Assert ("Bad Vera tag type" == NULL); break;
}
return result;
}
static const char *tagName (const tagType type)
{
const char* result;
if (isLanguage (Lang_csharp))
result = CsharpKinds [csharpTagKind (type)].name;
else if (isLanguage (Lang_java))
result = JavaKinds [javaTagKind (type)].name;
else if (isLanguage (Lang_vera))
result = VeraKinds [veraTagKind (type)].name;
else
result = CKinds [cTagKind (type)].name;
return result;
}
static int tagLetter (const tagType type)
{
int result;
if (isLanguage (Lang_csharp))
result = CsharpKinds [csharpTagKind (type)].letter;
else if (isLanguage (Lang_java))
result = JavaKinds [javaTagKind (type)].letter;
else if (isLanguage (Lang_vera))
result = VeraKinds [veraTagKind (type)].letter;
else
result = CKinds [cTagKind (type)].letter;
return result;
}
static boolean includeTag (const tagType type, const boolean isFileScope)
{
boolean result;
if (isFileScope && ! Option.include.fileScope)
result = FALSE;
else if (isLanguage (Lang_csharp))
result = CsharpKinds [csharpTagKind (type)].enabled;
else if (isLanguage (Lang_java))
result = JavaKinds [javaTagKind (type)].enabled;
else if (isLanguage (Lang_vera))
result = VeraKinds [veraTagKind (type)].enabled;
else
result = CKinds [cTagKind (type)].enabled;
return result;
}
static tagType declToTagType (const declType declaration)
{
tagType type = TAG_UNDEFINED;
switch (declaration)
{
case DECL_CLASS: type = TAG_CLASS; break;
case DECL_ENUM: type = TAG_ENUM; break;
case DECL_EVENT: type = TAG_EVENT; break;
case DECL_FUNCTION: type = TAG_FUNCTION; break;
case DECL_INTERFACE: type = TAG_INTERFACE; break;
case DECL_NAMESPACE: type = TAG_NAMESPACE; break;
case DECL_PROGRAM: type = TAG_PROGRAM; break;
case DECL_TASK: type = TAG_TASK; break;
case DECL_STRUCT: type = TAG_STRUCT; break;
case DECL_UNION: type = TAG_UNION; break;
default: Assert ("Unexpected declaration" == NULL); break;
}
return type;
}
static const char* accessField (const statementInfo *const st)
{
const char* result = NULL;
if (isLanguage (Lang_cpp) && st->scope == SCOPE_FRIEND)
result = "friend";
else if (st->member.access != ACCESS_UNDEFINED)
result = accessString (st->member.access);
return result;
}
static void addContextSeparator (vString *const scope)
{
if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
vStringCatS (scope, "::");
else if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
vStringCatS (scope, ".");
}
static void addOtherFields (tagEntryInfo* const tag, const tagType type,
const statementInfo *const st,
vString *const scope, vString *const typeRef)
{
/* For selected tag types, append an extension flag designating the
* parent object in which the tag is defined.
*/
switch (type)
{
default: break;
case TAG_FUNCTION:
case TAG_METHOD:
case TAG_PROTOTYPE:
if (vStringLength (Signature) > 0)
{
tag->extensionFields.signature = vStringValue (Signature);
}
if (vStringLength (ReturnType) > 0)
{
tag->extensionFields.returnType = vStringValue (ReturnType);
}
case TAG_CLASS:
case TAG_ENUM:
case TAG_ENUMERATOR:
case TAG_EVENT:
case TAG_FIELD:
case TAG_INTERFACE:
case TAG_MEMBER:
case TAG_NAMESPACE:
case TAG_PROPERTY:
case TAG_STRUCT:
case TAG_TASK:
case TAG_TYPEDEF:
case TAG_UNION:
if (vStringLength (scope) > 0 &&
(isMember (st) || st->parent->declaration == DECL_NAMESPACE))
{
if (isType (st->context, TOKEN_NAME))
tag->extensionFields.scope [0] = tagName (TAG_CLASS);
else
tag->extensionFields.scope [0] =
tagName (declToTagType (parentDecl (st)));
tag->extensionFields.scope [1] = vStringValue (scope);
}
if ((type == TAG_CLASS || type == TAG_INTERFACE ||
type == TAG_STRUCT) && vStringLength (st->parentClasses) > 0)
{
tag->extensionFields.inheritance =
vStringValue (st->parentClasses);
}
if (st->implementation != IMP_DEFAULT &&
(isLanguage (Lang_cpp) || isLanguage (Lang_csharp) ||
isLanguage (Lang_java)))
{
tag->extensionFields.implementation =
implementationString (st->implementation);
}
if (isMember (st))
{
tag->extensionFields.access = accessField (st);
}
break;
}
/* Add typename info, type of the tag and name of struct/union/etc. */
if ((type == TAG_TYPEDEF || type == TAG_VARIABLE || type == TAG_MEMBER)
&& isContextualStatement(st))
{
char *p;
tag->extensionFields.typeRef [0] =
tagName (declToTagType (st->declaration));
p = vStringValue (st->blockName->name);
/* If there was no {} block get the name from the token before the
* name (current token is ';' or ',', previous token is the name).
*/
if (p == NULL || *p == '\0')
{
tokenInfo *const prev2 = prevToken (st, 2);
if (isType (prev2, TOKEN_NAME))
p = vStringValue (prev2->name);
}
/* Prepend the scope name if there is one. */
if (vStringLength (scope) > 0)
{
vStringCopy(typeRef, scope);
addContextSeparator (typeRef);
vStringCatS(typeRef, p);
p = vStringValue (typeRef);
}
tag->extensionFields.typeRef [1] = p;
}
}
static void findScopeHierarchy (vString *const string,
const statementInfo *const st)
{
vStringClear (string);
if (isType (st->context, TOKEN_NAME))
vStringCopy (string, st->context->name);
if (st->parent != NULL)
{
vString *temp = vStringNew ();
const statementInfo *s;
for (s = st->parent ; s != NULL ; s = s->parent)
{
if (isContextualStatement (s) ||
s->declaration == DECL_NAMESPACE ||
s->declaration == DECL_PROGRAM)
{
vStringCopy (temp, string);
vStringClear (string);
Assert (isType (s->blockName, TOKEN_NAME));
if (isType (s->context, TOKEN_NAME) &&
vStringLength (s->context->name) > 0)
{
vStringCat (string, s->context->name);
addContextSeparator (string);
}
vStringCat (string, s->blockName->name);
if (vStringLength (temp) > 0)
addContextSeparator (string);
vStringCat (string, temp);
}
}
vStringDelete (temp);
}
}
static void makeExtraTagEntry (const tagType type, tagEntryInfo *const e,
vString *const scope)
{
if (Option.include.qualifiedTags &&
scope != NULL && vStringLength (scope) > 0)
{
vString *const scopedName = vStringNew ();
if (type != TAG_ENUMERATOR)
vStringCopy (scopedName, scope);
else
{
/* remove last component (i.e. enumeration name) from scope */
const char* const sc = vStringValue (scope);
const char* colon = strrchr (sc, ':');
if (colon != NULL)
{
while (*colon == ':' && colon > sc)
--colon;
vStringNCopy (scopedName, scope, colon + 1 - sc);
}
}
if (vStringLength (scopedName) > 0)
{
addContextSeparator (scopedName);
vStringCatS (scopedName, e->name);
e->name = vStringValue (scopedName);
makeTagEntry (e);
}
vStringDelete (scopedName);
}
}
static void makeTag (const tokenInfo *const token,
const statementInfo *const st,
boolean isFileScope, const tagType type)
{
/* Nothing is really of file scope when it appears in a header file.
*/
isFileScope = (boolean) (isFileScope && ! isHeaderFile ());
if (isType (token, TOKEN_NAME) && vStringLength (token->name) > 0 &&
includeTag (type, isFileScope))
{
vString *scope = vStringNew ();
/* Use "typeRef" to store the typename from addOtherFields() until
* it's used in makeTagEntry().
*/
vString *typeRef = vStringNew ();
tagEntryInfo e;
initTagEntry (&e, vStringValue (token->name));
e.lineNumber = token->lineNumber;
e.filePosition = token->filePosition;
e.isFileScope = isFileScope;
e.kindName = tagName (type);
e.kind = tagLetter (type);
findScopeHierarchy (scope, st);
addOtherFields (&e, type, st, scope, typeRef);
makeTagEntry (&e);
makeExtraTagEntry (type, &e, scope);
vStringDelete (scope);
vStringDelete (typeRef);
}
}
static boolean isValidTypeSpecifier (const declType declaration)
{
boolean result;
switch (declaration)
{
case DECL_BASE:
case DECL_CLASS:
case DECL_ENUM:
case DECL_EVENT:
case DECL_STRUCT:
case DECL_UNION:
result = TRUE;
break;
default:
result = FALSE;
break;
}
return result;
}
static void qualifyEnumeratorTag (const statementInfo *const st,
const tokenInfo *const nameToken)
{
if (isType (nameToken, TOKEN_NAME))
makeTag (nameToken, st, TRUE, TAG_ENUMERATOR);
}
static void qualifyFunctionTag (const statementInfo *const st,
const tokenInfo *const nameToken)
{
if (isType (nameToken, TOKEN_NAME))
{
tagType type;
const boolean isFileScope =
(boolean) (st->member.access == ACCESS_PRIVATE ||
(!isMember (st) && st->scope == SCOPE_STATIC));
if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
type = TAG_METHOD;
else if (isLanguage (Lang_vera) && st->declaration == DECL_TASK)
type = TAG_TASK;
else
type = TAG_FUNCTION;
makeTag (nameToken, st, isFileScope, type);
}
}
static void qualifyFunctionDeclTag (const statementInfo *const st,
const tokenInfo *const nameToken)
{
if (! isType (nameToken, TOKEN_NAME))
;
else if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
qualifyFunctionTag (st, nameToken);
else if (st->scope == SCOPE_TYPEDEF)
makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
else if (isValidTypeSpecifier (st->declaration) && ! isLanguage (Lang_csharp))
makeTag (nameToken, st, TRUE, TAG_PROTOTYPE);
}
static void qualifyCompoundTag (const statementInfo *const st,
const tokenInfo *const nameToken)
{
if (isType (nameToken, TOKEN_NAME))
{
const tagType type = declToTagType (st->declaration);
const boolean fileScoped = (boolean)
(!(isLanguage (Lang_java) ||
isLanguage (Lang_csharp) ||
isLanguage (Lang_vera)));
if (type != TAG_UNDEFINED)
makeTag (nameToken, st, fileScoped, type);
}
}
static void qualifyBlockTag (statementInfo *const st,
const tokenInfo *const nameToken)
{
switch (st->declaration)
{
case DECL_CLASS:
case DECL_ENUM:
case DECL_INTERFACE:
case DECL_NAMESPACE:
case DECL_PROGRAM:
case DECL_STRUCT:
case DECL_UNION:
qualifyCompoundTag (st, nameToken);
break;
default: break;
}
}
static void qualifyVariableTag (const statementInfo *const st,
const tokenInfo *const nameToken)
{
/* We have to watch that we do not interpret a declaration of the
* form "struct tag;" as a variable definition. In such a case, the
* token preceding the name will be a keyword.
*/
if (! isType (nameToken, TOKEN_NAME))
;
else if (st->scope == SCOPE_TYPEDEF)
makeTag (nameToken, st, TRUE, TAG_TYPEDEF);
else if (st->declaration == DECL_EVENT)
makeTag (nameToken, st, (boolean) (st->member.access == ACCESS_PRIVATE),
TAG_EVENT);
else if (st->declaration == DECL_PACKAGE)
makeTag (nameToken, st, FALSE, TAG_PACKAGE);
else if (isValidTypeSpecifier (st->declaration))
{
if (st->notVariable)
;
else if (isMember (st))
{
if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
makeTag (nameToken, st,
(boolean) (st->member.access == ACCESS_PRIVATE), TAG_FIELD);
else if (st->scope == SCOPE_GLOBAL || st->scope == SCOPE_STATIC)
makeTag (nameToken, st, TRUE, TAG_MEMBER);
}
else
{
if (st->scope == SCOPE_EXTERN || ! st->haveQualifyingName)
makeTag (nameToken, st, FALSE, TAG_EXTERN_VAR);
else if (st->inFunction)
makeTag (nameToken, st, (boolean) (st->scope == SCOPE_STATIC),
TAG_LOCAL);
else
makeTag (nameToken, st, (boolean) (st->scope == SCOPE_STATIC),
TAG_VARIABLE);
}
}
}
/*
* Parsing functions
*/
static int skipToOneOf (const char *const chars)
{
int c;
do
c = cppGetc ();
while (c != EOF && c != '\0' && strchr (chars, c) == NULL);
return c;
}
/* Skip to the next non-white character.
*/
static int skipToNonWhite (void)
{
boolean found = FALSE;
int c;
#if 0
do
c = cppGetc ();
while (isspace (c));
#else
while (1)
{
c = cppGetc ();
if (isspace (c))
found = TRUE;
else
break;
}
if (CollectingSignature && found)
vStringPut (Signature, ' ');
#endif
return c;
}
/* Skips to the next brace in column 1. This is intended for cases where
* preprocessor constructs result in unbalanced braces.
*/
static void skipToFormattedBraceMatch (void)
{
int c, next;
c = cppGetc ();
next = cppGetc ();
while (c != EOF && (c != '\n' || next != '}'))
{
c = next;
next = cppGetc ();
}
}
/* Skip to the matching character indicated by the pair string. If skipping
* to a matching brace and any brace is found within a different level of a
* #if conditional statement while brace formatting is in effect, we skip to
* the brace matched by its formatting. It is assumed that we have already
* read the character which starts the group (i.e. the first character of
* "pair").
*/
static void skipToMatch (const char *const pair)
{
const boolean braceMatching = (boolean) (strcmp ("{}", pair) == 0);
const boolean braceFormatting = (boolean) (isBraceFormat () && braceMatching);
const unsigned int initialLevel = getDirectiveNestLevel ();
const int begin = pair [0], end = pair [1];
const unsigned long inputLineNumber = getInputLineNumber ();
int matchLevel = 1;
int c = '\0';
while (matchLevel > 0 && (c = skipToNonWhite ()) != EOF)
{
if (CollectingSignature)
vStringPut (Signature, c);
if (c == begin)
{
// watch out for '<<' in template arguments
int x = cppGetc ();
if(c == '<' && x == '<') {
// we've found a << - do nothing except record the signature
if (CollectingSignature)
vStringPut(Signature, x);
} else {
cppUngetc (x);
++matchLevel;
if (braceFormatting && getDirectiveNestLevel () != initialLevel)
{
skipToFormattedBraceMatch ();
break;
}
}
}
else if (c == end)
{
// don't care if you find a '>>' (the important thing is closing the brackets)
int x = cppGetc ();
cppUngetc (x);
--matchLevel;
if (braceFormatting && getDirectiveNestLevel () != initialLevel)
{
skipToFormattedBraceMatch ();
break;
}
}
/* early out if matching "<>" and we encounter a ";" or "{" to mitigate
* match problems with C++ generics containing a static expression like
* foo<X<Y> bar;
* normally neither ";" nor "{" could appear inside "<>" anyway. */
else if (isLanguage (Lang_cpp) && begin == '<' &&
(c == ';' || c == '{'))
{
cppUngetc (c);
break;
}
}
if (c == EOF)
{
verbose ("%s: failed to find match for '%c' at line %lu\n",
getInputFileName (), begin, inputLineNumber);
if (braceMatching)
longjmp (Exception, (int) ExceptionBraceFormattingError);
else
longjmp (Exception, (int) ExceptionFormattingError);
}
}
static void skipParens (void)
{
const int c = skipToNonWhite ();
if (c == '(')
skipToMatch ("()");
else
cppUngetc (c);
}
static void skipBraces (void)
{
const int c = skipToNonWhite ();
if (c == '{')
skipToMatch ("{}");
else
cppUngetc (c);
}
static keywordId analyzeKeyword (const char *const name)
{
const keywordId id = (keywordId) lookupKeyword (name, getSourceLanguage ());
return id;
}
static void analyzeIdentifier (tokenInfo *const token)
{
char *const name = vStringValue (token->name);
const char *replacement = NULL;
boolean parensToo = FALSE;
if (isLanguage (Lang_java) ||
! isIgnoreToken (name, &parensToo, &replacement))
{
if (replacement != NULL)
token->keyword = analyzeKeyword (replacement);
else
token->keyword = analyzeKeyword (vStringValue (token->name));
if (token->keyword == KEYWORD_NONE)
token->type = TOKEN_NAME;
else
token->type = TOKEN_KEYWORD;
}
else
{
initToken (token);
if (parensToo)
{
int c = skipToNonWhite ();
if (c == '(')
skipToMatch ("()");
}
}
}
static void readIdentifier (tokenInfo *const token, const int firstChar)
{
vString *const name = token->name;
int c = firstChar;
boolean first = TRUE;
initToken (token);
/* Bug #1585745: strangely, C++ destructors allow whitespace between
* the ~ and the class name. */
if (isLanguage (Lang_cpp) && firstChar == '~')
{
vStringPut (name, c);
c = skipToNonWhite ();
}
do
{
vStringPut (name, c);
if (CollectingSignature)
{
if (!first)
vStringPut (Signature, c);
first = FALSE;
}
c = cppGetc ();
} while (isident (c) || ((isLanguage (Lang_java) || isLanguage (Lang_csharp)) && (isHighChar (c) || c == '.')));
vStringTerminate (name);
cppUngetc (c); /* unget non-identifier character */
analyzeIdentifier (token);
}
static void readPackageName (tokenInfo *const token, const int firstChar)
{
vString *const name = token->name;
int c = firstChar;
initToken (token);
while (isident (c) || c == '.')
{
vStringPut (name, c);
c = cppGetc ();
}
vStringTerminate (name);
cppUngetc (c); /* unget non-package character */
}
static void readPackageOrNamespace (statementInfo *const st, const declType declaration)
{
st->declaration = declaration;
if (declaration == DECL_NAMESPACE && !isLanguage (Lang_csharp))
{
/* In C++ a namespace is specified one level at a time. */
return;
}
else
{
/* In C#, a namespace can also be specified like a Java package name. */
tokenInfo *const token = activeToken (st);
Assert (isType (token, TOKEN_KEYWORD));
readPackageName (token, skipToNonWhite ());
token->type = TOKEN_NAME;
st->gotName = TRUE;
st->haveQualifyingName = TRUE;
}
}
static void processName (statementInfo *const st)
{
Assert (isType (activeToken (st), TOKEN_NAME));
if (st->gotName && st->declaration == DECL_NONE)
st->declaration = DECL_BASE;
st->gotName = TRUE;
st->haveQualifyingName = TRUE;
}
static void readOperator (statementInfo *const st)
{
const char *const acceptable = "+-*/%^&|~!=<>,[]";
const tokenInfo* const prev = prevToken (st,1);
tokenInfo *const token = activeToken (st);
vString *const name = token->name;
int c = skipToNonWhite ();
/* When we arrive here, we have the keyword "operator" in 'name'.
*/
if (isType (prev, TOKEN_KEYWORD) && (prev->keyword == KEYWORD_ENUM ||
prev->keyword == KEYWORD_STRUCT || prev->keyword == KEYWORD_UNION))
; /* ignore "operator" keyword if preceded by these keywords */
else if (c == '(')
{
/* Verify whether this is a valid function call (i.e. "()") operator.
*/
if (cppGetc () == ')')
{
vStringPut (name, ' '); /* always separate operator from keyword */
c = skipToNonWhite ();
if (c == '(')
vStringCatS (name, "()");
}
else
{
skipToMatch ("()");
c = cppGetc ();
}
}
else if (isident1 (c))
{
/* Handle "new" and "delete" operators, and conversion functions
* (per 13.3.1.1.2 [2] of the C++ spec).
*/
boolean whiteSpace = TRUE; /* default causes insertion of space */
do
{
if (isspace (c))
whiteSpace = TRUE;
else
{
if (whiteSpace)
{
vStringPut (name, ' ');
whiteSpace = FALSE;
}
vStringPut (name, c);
}
c = cppGetc ();
} while (! isOneOf (c, "(;") && c != EOF);
vStringTerminate (name);
}
else if (isOneOf (c, acceptable))
{
vStringPut (name, ' '); /* always separate operator from keyword */
do
{
vStringPut (name, c);
c = cppGetc ();
} while (isOneOf (c, acceptable));
vStringTerminate (name);
}
cppUngetc (c);
token->type = TOKEN_NAME;
token->keyword = KEYWORD_NONE;
processName (st);
}
static void copyToken (tokenInfo *const dest, const tokenInfo *const src)
{
dest->type = src->type;
dest->keyword = src->keyword;
dest->filePosition = src->filePosition;
dest->lineNumber = src->lineNumber;
vStringCopy (dest->name, src->name);
}
static void setAccess (statementInfo *const st, const accessType access)
{
if (isMember (st))
{
if (isLanguage (Lang_cpp))
{
int c = skipToNonWhite ();
if (c == ':')
reinitStatement (st, FALSE);
else
cppUngetc (c);
st->member.accessDefault = access;
}
st->member.access = access;
}
}
static void discardTypeList (tokenInfo *const token)
{
int c = skipToNonWhite ();
while (isident1 (c))
{
readIdentifier (token, c);
c = skipToNonWhite ();
if (c == '.' || c == ',')
c = skipToNonWhite ();
}
cppUngetc (c);
}
static void addParentClass (statementInfo *const st, tokenInfo *const token)
{
if (vStringLength (token->name) > 0 &&
vStringLength (st->parentClasses) > 0)
{
vStringPut (st->parentClasses, ',');
}
vStringCat (st->parentClasses, token->name);
}
static void readParents (statementInfo *const st, const int qualifier)
{
tokenInfo *const token = newToken ();
tokenInfo *const parent = newToken ();
int c;
do
{
c = skipToNonWhite ();
if (isident1 (c))
{
readIdentifier (token, c);
if (isType (token, TOKEN_NAME))
vStringCat (parent->name, token->name);
else
{
addParentClass (st, parent);
initToken (parent);
}
}
else if (c == qualifier)
vStringPut (parent->name, c);
else if (c == '<')
skipToMatch ("<>");
else if (isType (token, TOKEN_NAME))
{
addParentClass (st, parent);
initToken (parent);
}
} while (c != '{' && c != EOF);
cppUngetc (c);
deleteToken (parent);
deleteToken (token);
}
static void skipStatement (statementInfo *const st)
{
st->declaration = DECL_IGNORE;
skipToOneOf (";");
}
static void processInterface (statementInfo *const st)
{
st->declaration = DECL_INTERFACE;
}
static void checkIsClassEnum (statementInfo *const st, const declType decl)
{
if (! isLanguage (Lang_cpp) || st->declaration != DECL_ENUM)
st->declaration = decl;
}
static void processToken (tokenInfo *const token, statementInfo *const st)
{
switch (token->keyword) /* is it a reserved word? */
{
default: break;
case KEYWORD_NONE: processName (st); break;
case KEYWORD_ABSTRACT: st->implementation = IMP_ABSTRACT; break;
case KEYWORD_ATTRIBUTE:
case KEYWORD_TYPENAME:
case KEYWORD_INLINE: skipParens (); initToken (token); break;
case KEYWORD_BIND: st->declaration = DECL_BASE; break;
case KEYWORD_BIT: st->declaration = DECL_BASE; break;
case KEYWORD_CATCH: skipParens (); skipBraces (); break;
case KEYWORD_CHAR: st->declaration = DECL_BASE; break;
case KEYWORD_CLASS: checkIsClassEnum (st, DECL_CLASS); break;
case KEYWORD_CONST: st->declaration = DECL_BASE; break;
case KEYWORD_CONSTEXPR: st->declaration = DECL_BASE; break;
case KEYWORD_DOUBLE: st->declaration = DECL_BASE; break;
case KEYWORD_ENUM: st->declaration = DECL_ENUM; break;
case KEYWORD_EXTENDS: readParents (st, '.');
setToken (st, TOKEN_NONE); break;
case KEYWORD_FLOAT: st->declaration = DECL_BASE; break;
case KEYWORD_FUNCTION: st->declaration = DECL_BASE; break;
case KEYWORD_FRIEND: st->scope = SCOPE_FRIEND; break;
case KEYWORD_GOTO: skipStatement (st); break;
case KEYWORD_IMPLEMENTS:readParents (st, '.');
setToken (st, TOKEN_NONE); break;
case KEYWORD_IMPORT: skipStatement (st); break;
case KEYWORD_INT: st->declaration = DECL_BASE; break;
case KEYWORD_INTEGER: st->declaration = DECL_BASE; break;
case KEYWORD_INTERFACE: processInterface (st); break;
case KEYWORD_LOCAL: setAccess (st, ACCESS_LOCAL); break;
case KEYWORD_LONG: st->declaration = DECL_BASE; break;
case KEYWORD_OPERATOR: readOperator (st); break;
case KEYWORD_PRIVATE: setAccess (st, ACCESS_PRIVATE); break;
case KEYWORD_PROGRAM: st->declaration = DECL_PROGRAM; break;
case KEYWORD_PROTECTED: setAccess (st, ACCESS_PROTECTED); break;
case KEYWORD_PUBLIC: setAccess (st, ACCESS_PUBLIC); break;
case KEYWORD_RETURN: skipStatement (st); break;
case KEYWORD_SHORT: st->declaration = DECL_BASE; break;
case KEYWORD_SIGNED: st->declaration = DECL_BASE; break;
case KEYWORD_STATIC_ASSERT: skipParens(); break;
case KEYWORD_STRING: st->declaration = DECL_BASE; break;
case KEYWORD_STRUCT: checkIsClassEnum (st, DECL_STRUCT); break;
case KEYWORD_TASK: st->declaration = DECL_TASK; break;
case KEYWORD_THROWS: discardTypeList (token); break;
case KEYWORD_UNION: st->declaration = DECL_UNION; break;
case KEYWORD_UNSIGNED: st->declaration = DECL_BASE; break;
case KEYWORD_USING: skipStatement (st); break;
case KEYWORD_VOID: st->declaration = DECL_BASE; break;
case KEYWORD_VOLATILE: st->declaration = DECL_BASE; break;
case KEYWORD_VIRTUAL: st->implementation = IMP_VIRTUAL; break;
case KEYWORD_WCHAR_T: st->declaration = DECL_BASE; break;
case KEYWORD_NAMESPACE: readPackageOrNamespace (st, DECL_NAMESPACE); break;
case KEYWORD_PACKAGE: readPackageOrNamespace (st, DECL_PACKAGE); break;
case KEYWORD_EVENT:
if (isLanguage (Lang_csharp))
st->declaration = DECL_EVENT;
break;
case KEYWORD_TYPEDEF:
reinitStatement (st, FALSE);
st->scope = SCOPE_TYPEDEF;
break;
case KEYWORD_EXTERN:
if (! isLanguage (Lang_csharp) || !st->gotName)
{
reinitStatement (st, FALSE);
st->scope = SCOPE_EXTERN;
st->declaration = DECL_BASE;
}
break;
case KEYWORD_STATIC:
if (! (isLanguage (Lang_java) || isLanguage (Lang_csharp)))
{
reinitStatement (st, FALSE);
st->scope = SCOPE_STATIC;
st->declaration = DECL_BASE;
}
break;
case KEYWORD_FOR:
case KEYWORD_FOREACH:
case KEYWORD_IF:
case KEYWORD_SWITCH:
case KEYWORD_WHILE:
{
int c = skipToNonWhite ();
if (c == '(')
skipToMatch ("()");
break;
}
}
}
/*
* Parenthesis handling functions
*/
static void restartStatement (statementInfo *const st)
{
tokenInfo *const save = newToken ();
tokenInfo *token = activeToken (st);
copyToken (save, token);
DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>");)
reinitStatement (st, FALSE);
token = activeToken (st);
copyToken (token, save);
deleteToken (save);
processToken (token, st);
}
/* Skips over a the mem-initializer-list of a ctor-initializer, defined as:
*
* mem-initializer-list:
* mem-initializer, mem-initializer-list
*
* mem-initializer:
* [::] [nested-name-spec] class-name (...)
* identifier
*/
static void skipMemIntializerList (tokenInfo *const token)
{
int c;
do
{
c = skipToNonWhite ();
while (isident1 (c) || c == ':')
{
if (c != ':')
readIdentifier (token, c);
c = skipToNonWhite ();
}
if (c == '<')
{
skipToMatch ("<>");
c = skipToNonWhite ();
}
if (c == '(')
{
skipToMatch ("()");
c = skipToNonWhite ();
}
} while (c == ',');
cppUngetc (c);
}
static void skipMacro (statementInfo *const st)
{
tokenInfo *const prev2 = prevToken (st, 2);
if (isType (prev2, TOKEN_NAME))
retardToken (st);
skipToMatch ("()");
}
/* Skips over characters following the parameter list. This will be either
* non-ANSI style function declarations or C++ stuff. Our choices:
*
* C (K&R):
* int func ();
* int func (one, two) int one; float two; {...}
* C (ANSI):
* int func (int one, float two);
* int func (int one, float two) {...}
* C++:
* int foo (...) [const|volatile] [throw (...)];
* int foo (...) [const|volatile] [throw (...)] [ctor-initializer] {...}
* int foo (...) [const|volatile] [throw (...)] try [ctor-initializer] {...}
* catch (...) {...}
*/
static boolean skipPostArgumentStuff (
statementInfo *const st, parenInfo *const info)
{
tokenInfo *const token = activeToken (st);
unsigned int parameters = info->parameterCount;
unsigned int elementCount = 0;
boolean restart = FALSE;
boolean end = FALSE;
int c = skipToNonWhite ();
do
{
switch (c)
{
case ')': break;
case ':': skipMemIntializerList (token);break; /* ctor-initializer */
case '[': skipToMatch ("[]"); break;
case '=': cppUngetc (c); end = TRUE; break;
case '{': cppUngetc (c); end = TRUE; break;
case '}': cppUngetc (c); end = TRUE; break;
case '(':
if (elementCount > 0)
++elementCount;
skipToMatch ("()");
break;
case ';':
if (parameters == 0 || elementCount < 2)
{
cppUngetc (c);
end = TRUE;
}
else if (--parameters == 0)
end = TRUE;
break;
default:
if (isident1 (c))
{
readIdentifier (token, c);
switch (token->keyword)
{
case KEYWORD_ATTRIBUTE: skipParens (); break;
case KEYWORD_THROW: skipParens (); break;
case KEYWORD_TRY: break;
case KEYWORD_CONST:
case KEYWORD_VOLATILE:
if (vStringLength (Signature) > 0)
{
vStringPut (Signature, ' ');
vStringCat (Signature, token->name);
}
break;
case KEYWORD_CATCH:
case KEYWORD_CLASS:
case KEYWORD_EXPLICIT:
case KEYWORD_EXTERN:
case KEYWORD_FRIEND:
case KEYWORD_INLINE:
case KEYWORD_MUTABLE:
case KEYWORD_NAMESPACE:
case KEYWORD_NEW:
case KEYWORD_NEWCOV:
case KEYWORD_NOEXCEPT:
case KEYWORD_OPERATOR:
case KEYWORD_OVERLOAD:
case KEYWORD_PRIVATE:
case KEYWORD_PROTECTED:
case KEYWORD_PUBLIC:
case KEYWORD_STATIC:
case KEYWORD_TEMPLATE:
case KEYWORD_TYPEDEF:
case KEYWORD_TYPENAME:
case KEYWORD_USING:
case KEYWORD_VIRTUAL:
/* Never allowed within parameter declarations. */
restart = TRUE;
end = TRUE;
break;
default:
/* "override" and "final" are only keywords in the declaration of a virtual
* member function, so need to be handled specially, not as keywords */
if (isLanguage(Lang_cpp) && isType (token, TOKEN_NAME) &&
(strcmp ("override", vStringValue (token->name)) == 0 ||
strcmp ("final", vStringValue (token->name)) == 0))
;
else if (isType (token, TOKEN_NONE))
;
else if (info->isKnrParamList && info->parameterCount > 0)
++elementCount;
else
{
/* If we encounter any other identifier immediately
* following an empty parameter list, this is almost
* certainly one of those Microsoft macro "thingies"
* that the automatic source code generation sticks
* in. Terminate the current statement.
*/
restart = TRUE;
end = TRUE;
}
break;
}
}
}
if (! end)
{
c = skipToNonWhite ();
if (c == EOF)
end = TRUE;
}
} while (! end);
if (restart)
restartStatement (st);
else
setToken (st, TOKEN_NONE);
return (boolean) (c != EOF);
}
static void skipJavaThrows (statementInfo *const st)
{
tokenInfo *const token = activeToken (st);
int c = skipToNonWhite ();
if (isident1 (c))
{
readIdentifier (token, c);
if (token->keyword == KEYWORD_THROWS)
{
do
{
c = skipToNonWhite ();
if (isident1 (c))
{
readIdentifier (token, c);
c = skipToNonWhite ();
}
} while (c == '.' || c == ',');
}
}
cppUngetc (c);
setToken (st, TOKEN_NONE);
}
static void analyzePostParens (statementInfo *const st, parenInfo *const info)
{
const unsigned long inputLineNumber = getInputLineNumber ();
int c = skipToNonWhite ();
cppUngetc (c);
if (isOneOf (c, "{;,="))
;
else if (isLanguage (Lang_java))
skipJavaThrows (st);
else
{
if (! skipPostArgumentStuff (st, info))
{
verbose (
"%s: confusing argument declarations beginning at line %lu\n",
getInputFileName (), inputLineNumber);
longjmp (Exception, (int) ExceptionFormattingError);
}
}
}
static boolean languageSupportsGenerics (void)
{
return (boolean) (isLanguage (Lang_cpp) || isLanguage (Lang_csharp) ||
isLanguage (Lang_java));
}
static void processAngleBracket (void)
{
int c = cppGetc ();
if (c == '>') {
/* already found match for template */
} else if (languageSupportsGenerics () && c != '<' && c != '=') {
/* this is a template */
cppUngetc (c);
skipToMatch ("<>");
} else if (c == '<') {
/* skip "<<" or "<<=". */
c = cppGetc ();
if (c != '=') {
cppUngetc (c);
}
} else {
cppUngetc (c);
}
}
static void parseJavaAnnotation (statementInfo *const st)
{
/*
* @Override
* @Target(ElementType.METHOD)
* @SuppressWarnings(value = "unchecked")
*
* But watch out for "@interface"!
*/
tokenInfo *const token = activeToken (st);
int c = skipToNonWhite ();
readIdentifier (token, c);
if (token->keyword == KEYWORD_INTERFACE)
{
/* Oops. This was actually "@interface" defining a new annotation. */
processInterface (st);
}
else
{
/* Bug #1691412: skip any annotation arguments. */
skipParens ();
}
}
static void parseReturnType (statementInfo *const st)
{
int i;
int lower_bound;
int upper_bound;
tokenInfo * finding_tok;
/* FIXME TODO: if java language must be supported then impement this here
* removing the current FIXME */
if (!isLanguage (Lang_c) && !isLanguage (Lang_cpp))
{
return;
}
vStringClear (ReturnType);
finding_tok = prevToken (st, 1);
if (isType (finding_tok, TOKEN_NONE))
return;
finding_tok = prevToken (st, 2);
if (finding_tok->type == TOKEN_DOUBLE_COLON)
{
/* get the total number of double colons */
int j;
int num_colons = 0;
/* we already are at 2nd token */
/* the +=2 means that colons are usually found at even places */
for (j = 2; j < NumTokens; j+=2)
{
tokenInfo *curr_tok;
curr_tok = prevToken (st, j);
if (curr_tok->type == TOKEN_DOUBLE_COLON)
num_colons++;
else
break;
}
/*printf ("FOUND colons %d\n", num_colons);*/
lower_bound = 2 * num_colons + 1;
}
else
lower_bound = 1;
upper_bound = -1;
for (i = 0; i < NumTokens; i++) {
tokenInfo *curr_tok;
curr_tok = prevToken (st, i);
if (curr_tok->type == TOKEN_BRACE_CLOSE || curr_tok->type == TOKEN_BRACE_OPEN) {
upper_bound = i - 1;
break;
}
}
if (upper_bound < 0) {
upper_bound = NumTokens - 1;
}
for (i = upper_bound; i > lower_bound; i--)
{
tokenInfo * curr_tok;
curr_tok = prevToken (st, i);
switch (curr_tok->type)
{
case TOKEN_PAREN_NAME:
case TOKEN_NONE:
continue;
break;
case TOKEN_DOUBLE_COLON:
/* usually C++ class scope */
vStringCatS (ReturnType, "::");
break;
case TOKEN_STAR:
/* pointers */
vStringPut (ReturnType, '*');
break;
case TOKEN_AMPERSAND:
/* references */
vStringPut (ReturnType, '&');
break;
default:
vStringCat (ReturnType, curr_tok->name);
if (curr_tok->type == TOKEN_KEYWORD) {
vStringPut (ReturnType, ' ');
}
break;
}
}
/* clear any white space from the front */
vStringStripLeading (ReturnType);
/* .. and from the tail too */
vStringStripTrailing (ReturnType);
/* put and end marker */
vStringTerminate (ReturnType);
/*/
printf ("~~~~~ statement ---->\n");
ps (st);
printf ("NumTokens: %d\n", NumTokens);
printf ("FOUND ReturnType: %s\n", vStringValue (ReturnType));
printf ("<~~~~~\n");
//*/
}
static int parseParens (statementInfo *const st, parenInfo *const info)
{
tokenInfo *const token = activeToken (st);
unsigned int identifierCount = 0;
unsigned int depth = 1;
boolean firstChar = TRUE;
int nextChar = '\0';
CollectingSignature = TRUE;
vStringClear (Signature);
vStringPut (Signature, '(');
info->parameterCount = 1;
do
{
int c = skipToNonWhite ();
vStringPut (Signature, c);
switch (c)
{
case '&':
case '*':
info->isPointer = TRUE;
info->isKnrParamList = FALSE;
if (identifierCount == 0)
info->isParamList = FALSE;
initToken (token);
break;
case ':':
info->isKnrParamList = FALSE;
break;
case '.':
info->isNameCandidate = FALSE;
c = cppGetc ();
if (c != '.')
{
cppUngetc (c);
info->isKnrParamList = FALSE;
}
else
{
c = cppGetc ();
if (c != '.')
{
cppUngetc (c);
info->isKnrParamList = FALSE;
}
else
vStringCatS (Signature, "..."); /* variable arg list */
}
break;
case ',':
info->isNameCandidate = FALSE;
if (info->isKnrParamList)
{
++info->parameterCount;
identifierCount = 0;
}
break;
case '=':
info->isKnrParamList = FALSE;
info->isNameCandidate = FALSE;
if (firstChar)
{
info->isParamList = FALSE;
skipMacro (st);
depth = 0;
}
break;
case '[':
info->isKnrParamList = FALSE;
skipToMatch ("[]");
break;
case '<':
info->isKnrParamList = FALSE;
processAngleBracket ();
break;
case ')':
if (firstChar)
info->parameterCount = 0;
--depth;
break;
case '(':
info->isKnrParamList = FALSE;
if (firstChar)
{
info->isNameCandidate = FALSE;
cppUngetc (c);
vStringClear (Signature);
skipMacro (st);
depth = 0;
vStringChop (Signature);
}
else if (isType (token, TOKEN_PAREN_NAME))
{
c = skipToNonWhite ();
if (c == '*') /* check for function pointer */
{
skipToMatch ("()");
c = skipToNonWhite ();
if (c == '(')
skipToMatch ("()");
else
cppUngetc (c);
}
else
{
cppUngetc (c);
cppUngetc ('(');
info->nestedArgs = TRUE;
}
}
else
++depth;
break;
default:
if (c == '@' && isLanguage (Lang_java))
{
parseJavaAnnotation(st);
}
else if (isident1 (c))
{
if (++identifierCount > 1)
info->isKnrParamList = FALSE;
readIdentifier (token, c);
if (isType (token, TOKEN_NAME) && info->isNameCandidate)
token->type = TOKEN_PAREN_NAME;
else if (isType (token, TOKEN_KEYWORD))
{
if (token->keyword != KEYWORD_CONST &&
token->keyword != KEYWORD_VOLATILE)
{
info->isKnrParamList = FALSE;
info->isNameCandidate = FALSE;
}
}
}
else
{
info->isParamList = FALSE;
info->isKnrParamList = FALSE;
info->isNameCandidate = FALSE;
info->invalidContents = TRUE;
}
break;
}
firstChar = FALSE;
} while (! info->nestedArgs && depth > 0 &&
(info->isKnrParamList || info->isNameCandidate));
if (! info->nestedArgs) while (depth > 0)
{
skipToMatch ("()");
--depth;
}
if (! info->isNameCandidate)
initToken (token);
vStringTerminate (Signature);
if (info->isKnrParamList)
vStringClear (Signature);
CollectingSignature = FALSE;
return nextChar;
}
static void initParenInfo (parenInfo *const info)
{
info->isPointer = FALSE;
info->isParamList = TRUE;
info->isKnrParamList = isLanguage (Lang_c);
info->isNameCandidate = TRUE;
info->invalidContents = FALSE;
info->nestedArgs = FALSE;
info->parameterCount = 0;
}
static void analyzeParens (statementInfo *const st)
{
tokenInfo *const prev = prevToken (st, 1);
if (st->inFunction && ! st->assignment)
st->notVariable = TRUE;
if (! isType (prev, TOKEN_NONE)) /* in case of ignored enclosing macros */
{
tokenInfo *const token = activeToken (st);
parenInfo info;
int c;
initParenInfo (&info);
parseParens (st, &info);
parseReturnType (st);
c = skipToNonWhite ();
cppUngetc (c);
if (info.invalidContents)
reinitStatement (st, FALSE);
else if (info.isNameCandidate && isType (token, TOKEN_PAREN_NAME) &&
! st->gotParenName &&
(! info.isParamList || ! st->haveQualifyingName ||
c == '(' ||
(c == '=' && st->implementation != IMP_VIRTUAL) ||
(st->declaration == DECL_NONE && isOneOf (c, ",;"))))
{
token->type = TOKEN_NAME;
processName (st);
st->gotParenName = TRUE;
if (! (c == '(' && info.nestedArgs))
st->isPointer = info.isPointer;
}
else if (! st->gotArgs && info.isParamList)
{
st->gotArgs = TRUE;
setToken (st, TOKEN_ARGS);
advanceToken (st);
if (st->scope != SCOPE_TYPEDEF)
analyzePostParens (st, &info);
}
else
setToken (st, TOKEN_NONE);
}
}
/*
* Token parsing functions
*/
static void addContext (statementInfo *const st, const tokenInfo* const token)
{
if (isType (token, TOKEN_NAME))
{
if (vStringLength (st->context->name) > 0)
{
if (isLanguage (Lang_c) || isLanguage (Lang_cpp))
vStringCatS (st->context->name, "::");
else if (isLanguage (Lang_java) || isLanguage (Lang_csharp))
vStringCatS (st->context->name, ".");
}
vStringCat (st->context->name, token->name);
st->context->type = TOKEN_NAME;
}
}
static boolean inheritingDeclaration (declType decl)
{
/* C# supports inheritance for enums. C++0x will too, but not yet. */
if (decl == DECL_ENUM)
{
return (boolean) (isLanguage (Lang_csharp));
}
return (boolean) (
decl == DECL_CLASS ||
decl == DECL_STRUCT ||
decl == DECL_INTERFACE);
}
static void processColon (statementInfo *const st)
{
int c = (isLanguage (Lang_cpp) ? cppGetc () : skipToNonWhite ());
const boolean doubleColon = (boolean) (c == ':');
if (doubleColon)
{
setToken (st, TOKEN_DOUBLE_COLON);
st->haveQualifyingName = FALSE;
}
else
{
cppUngetc (c);
if ((isLanguage (Lang_cpp) || isLanguage (Lang_csharp)) &&
inheritingDeclaration (st->declaration))
{
readParents (st, ':');
}
else if (parentDecl (st) == DECL_STRUCT)
{
c = skipToOneOf (",;");
if (c == ',')
setToken (st, TOKEN_COMMA);
else if (c == ';')
setToken (st, TOKEN_SEMICOLON);
}
else if (isLanguage (Lang_cpp) && st->declaration == DECL_ENUM)
{
/* skip enum's base type */
c = skipToOneOf ("{;");
if (c == '{')
setToken (st, TOKEN_BRACE_OPEN);
else if (c == ';')
setToken (st, TOKEN_SEMICOLON);
}
else
{
const tokenInfo *const prev = prevToken (st, 1);
const tokenInfo *const prev2 = prevToken (st, 2);
if (prev->keyword == KEYWORD_DEFAULT ||
prev2->keyword == KEYWORD_CASE ||
st->parent != NULL)
{
reinitStatement (st, FALSE);
}
}
}
}
/* Skips over any initializing value which may follow an '=' character in a
* variable definition.
*/
static int skipInitializer (statementInfo *const st)
{
boolean done = FALSE;
int c;
while (! done)
{
c = skipToNonWhite ();
if (c == EOF)
longjmp (Exception, (int) ExceptionFormattingError);
else switch (c)
{
case ',':
case ';': done = TRUE; break;
case '0':
if (st->implementation == IMP_VIRTUAL)
st->implementation = IMP_PURE_VIRTUAL;
break;
case '[': skipToMatch ("[]"); break;
case '(': skipToMatch ("()"); break;
case '{': skipToMatch ("{}"); break;
case '<': processAngleBracket(); break;
case '}':
if (insideEnumBody (st))
done = TRUE;
else if (! isBraceFormat ())
{
verbose ("%s: unexpected closing brace at line %lu\n",
getInputFileName (), getInputLineNumber ());
longjmp (Exception, (int) ExceptionBraceFormattingError);
}
break;
default: break;
}
}
return c;
}
static void processInitializer (statementInfo *const st)
{
const boolean inEnumBody = insideEnumBody (st);
int c = cppGetc ();
if (c != '=')
{
cppUngetc (c);
c = skipInitializer (st);
st->assignment = TRUE;
if (c == ';')
setToken (st, TOKEN_SEMICOLON);
else if (c == ',')
setToken (st, TOKEN_COMMA);
else if (c == '}' && inEnumBody)
{
cppUngetc (c);
setToken (st, TOKEN_COMMA);
}
if (st->scope == SCOPE_EXTERN)
st->scope = SCOPE_GLOBAL;
}
}
static void parseIdentifier (statementInfo *const st, const int c)
{
tokenInfo *const token = activeToken (st);
readIdentifier (token, c);
if (! isType (token, TOKEN_NONE))
processToken (token, st);
}
static void parseGeneralToken (statementInfo *const st, const int c)
{
const tokenInfo *const prev = prevToken (st, 1);
if (isident1 (c) || (isLanguage (Lang_java) && isHighChar (c)))
{
parseIdentifier (st, c);
if (isType (st->context, TOKEN_NAME) &&
isType (activeToken (st), TOKEN_NAME) && isType (prev, TOKEN_NAME))
{
initToken (st->context);
}
}
else if (c == '.' || c == '-')
{
if (! st->assignment)
st->notVariable = TRUE;
if (c == '-')
{
int c2 = cppGetc ();
if (c2 != '>')
cppUngetc (c2);
}
}
else if (c == '!' || c == '>')
{
int c2 = cppGetc ();
if (c2 != '=')
cppUngetc (c2);
}
else if (c == '@' && isLanguage (Lang_java))
{
parseJavaAnnotation (st);
}
else if (isExternCDecl (st, c))
{
st->declaration = DECL_NOMANGLE;
st->scope = SCOPE_GLOBAL;
}
}
/* Reads characters from the pre-processor and assembles tokens, setting
* the current statement state.
*/
static void nextToken (statementInfo *const st)
{
tokenInfo *token;
do
{
int c = skipToNonWhite ();
switch (c)
{
case EOF: longjmp (Exception, (int) ExceptionEOF); break;
/* analyze functions and co */
case '(': analyzeParens (st); break;
case '<': processAngleBracket (); break;
case '*':
st->haveQualifyingName = FALSE;
setToken (st, TOKEN_STAR);
break;
case '&': setToken (st, TOKEN_AMPERSAND); break;
case ',': setToken (st, TOKEN_COMMA); break;
case ':': processColon (st); break;
case ';': setToken (st, TOKEN_SEMICOLON); break;
case '=': processInitializer (st); break;
case '[': skipToMatch ("[]"); break;
case '{': setToken (st, TOKEN_BRACE_OPEN); break;
case '}': setToken (st, TOKEN_BRACE_CLOSE); break;
default: parseGeneralToken (st, c); break;
}
token = activeToken (st);
} while (isType (token, TOKEN_NONE));
}
/*
* Scanning support functions
*/
static statementInfo *CurrentStatement = NULL;
static statementInfo *newStatement (statementInfo *const parent)
{
statementInfo *const st = xMalloc (1, statementInfo);
unsigned int i;
for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
st->token [i] = newToken ();
st->context = newToken ();
st->blockName = newToken ();
st->parentClasses = vStringNew ();
initStatement (st, parent);
CurrentStatement = st;
return st;
}
static void deleteStatement (void)
{
statementInfo *const st = CurrentStatement;
statementInfo *const parent = st->parent;
unsigned int i;
for (i = 0 ; i < (unsigned int) NumTokens ; ++i)
{
deleteToken (st->token [i]); st->token [i] = NULL;
}
deleteToken (st->blockName); st->blockName = NULL;
deleteToken (st->context); st->context = NULL;
vStringDelete (st->parentClasses); st->parentClasses = NULL;
eFree (st);
CurrentStatement = parent;
}
static void deleteAllStatements (void)
{
while (CurrentStatement != NULL)
deleteStatement ();
}
static boolean isStatementEnd (const statementInfo *const st)
{
const tokenInfo *const token = activeToken (st);
boolean isEnd;
if (isType (token, TOKEN_SEMICOLON))
isEnd = TRUE;
else if (isType (token, TOKEN_BRACE_CLOSE))
/* Java and C# do not require semicolons to end a block. Neither do C++
* namespaces. All other blocks require a semicolon to terminate them.
*/
isEnd = (boolean) (isLanguage (Lang_java) || isLanguage (Lang_csharp) ||
! isContextualStatement (st));
else
isEnd = FALSE;
return isEnd;
}
static void checkStatementEnd (statementInfo *const st)
{
const tokenInfo *const token = activeToken (st);
if (isType (token, TOKEN_COMMA))
reinitStatement (st, TRUE);
else if (isStatementEnd (st))
{
DebugStatement ( if (debug (DEBUG_PARSE)) printf ("<ES>"); )
reinitStatement (st, FALSE);
cppEndStatement ();
}
else
{
cppBeginStatement ();
advanceToken (st);
}
}
static void nest (statementInfo *const st, const unsigned int nestLevel)
{
switch (st->declaration)
{
case DECL_CLASS:
case DECL_ENUM:
case DECL_INTERFACE:
case DECL_NAMESPACE:
case DECL_NOMANGLE:
case DECL_STRUCT:
case DECL_UNION:
createTags (nestLevel, st);
break;
case DECL_FUNCTION:
case DECL_TASK:
st->inFunction = TRUE;
/* fall through */
default:
if (includeTag (TAG_LOCAL, FALSE))
createTags (nestLevel, st);
else
skipToMatch ("{}");
break;
}
advanceToken (st);
setToken (st, TOKEN_BRACE_CLOSE);
}
static void tagCheck (statementInfo *const st)
{
const tokenInfo *const token = activeToken (st);
const tokenInfo *const prev = prevToken (st, 1);
const tokenInfo *const prev2 = prevToken (st, 2);
switch (token->type)
{
case TOKEN_NAME:
if (insideEnumBody (st))
qualifyEnumeratorTag (st, token);
break;
#if 0
case TOKEN_PACKAGE:
if (st->haveQualifyingName)
makeTag (token, st, FALSE, TAG_PACKAGE);
break;
#endif
case TOKEN_BRACE_OPEN:
if (isType (prev, TOKEN_ARGS))
{
if (st->haveQualifyingName)
{
if (! isLanguage (Lang_vera))
st->declaration = DECL_FUNCTION;
if (isType (prev2, TOKEN_NAME))
copyToken (st->blockName, prev2);
qualifyFunctionTag (st, prev2);
}
}
else if (isContextualStatement (st) ||
st->declaration == DECL_NAMESPACE ||
st->declaration == DECL_PROGRAM)
{
tokenInfo *name_token = (tokenInfo *)prev;
/* C++ 11 allows class <name> final { ... } */
if (isLanguage (Lang_cpp) && isType (prev, TOKEN_NAME) &&
strcmp("final", vStringValue(prev->name)) == 0 &&
isType(prev2, TOKEN_NAME))
{
name_token = (tokenInfo *)prev2;
copyToken (st->blockName, name_token);
}
else if (isType (name_token, TOKEN_NAME))
{
copyToken (st->blockName, prev);
}
else
{
/* For an anonymous struct or union we use a unique ID
* a number, so that the members can be found.
*/
char buf [20]; /* length of "_anon" + digits + null */
sprintf (buf, "__anon%d", ++AnonymousID);
vStringCopyS (st->blockName->name, buf);
st->blockName->type = TOKEN_NAME;
st->blockName->keyword = KEYWORD_NONE;
}
qualifyBlockTag (st, prev);
}
else if (isLanguage (Lang_csharp))
makeTag (prev, st, FALSE, TAG_PROPERTY);
break;
case TOKEN_SEMICOLON:
case TOKEN_COMMA:
if (insideEnumBody (st))
;
else if (isType (prev, TOKEN_NAME))
{
if (isContextualKeyword (prev2))
makeTag (prev, st, TRUE, TAG_EXTERN_VAR);
else
qualifyVariableTag (st, prev);
}
else if (isType (prev, TOKEN_ARGS) && isType (prev2, TOKEN_NAME))
{
if (st->isPointer)
qualifyVariableTag (st, prev2);
else
qualifyFunctionDeclTag (st, prev2);
}
if (isLanguage (Lang_java) && token->type == TOKEN_SEMICOLON && insideEnumBody (st))
{
/* In Java, after an initial enum-like part,
* a semicolon introduces a class-like part.
* See Bug #1730485 for the full rationale. */
st->parent->declaration = DECL_CLASS;
}
break;
default: break;
}
}
/* Parses the current file and decides whether to write out and tags that
* are discovered.
*/
static void createTags (const unsigned int nestLevel,
statementInfo *const parent)
{
statementInfo *const st = newStatement (parent);
DebugStatement ( if (nestLevel > 0) debugParseNest (TRUE, nestLevel); )
while (TRUE)
{
tokenInfo *token;
nextToken (st);
token = activeToken (st);
if (isType (token, TOKEN_BRACE_CLOSE))
{
if (nestLevel > 0)
break;
else
{
verbose ("%s: unexpected closing brace at line %lu\n",
getInputFileName (), getInputLineNumber ());
longjmp (Exception, (int) ExceptionBraceFormattingError);
}
}
else if (isType (token, TOKEN_DOUBLE_COLON))
{
addContext (st, prevToken (st, 1));
advanceToken (st);
}
else
{
tagCheck (st);
if (isType (token, TOKEN_BRACE_OPEN))
nest (st, nestLevel + 1);
checkStatementEnd (st);
}
}
deleteStatement ();
DebugStatement ( if (nestLevel > 0) debugParseNest (FALSE, nestLevel - 1); )
}
static boolean findCTags (const unsigned int passCount)
{
exception_t exception;
boolean retry;
Assert (passCount < 3);
cppInit ((boolean) (passCount > 1), isLanguage (Lang_csharp));
Signature = vStringNew ();
ReturnType = vStringNew ();
exception = (exception_t) setjmp (Exception);
retry = FALSE;
if (exception == ExceptionNone)
createTags (0, NULL);
else
{
deleteAllStatements ();
if (exception == ExceptionBraceFormattingError && passCount == 1)
{
retry = TRUE;
verbose ("%s: retrying file with fallback brace matching algorithm\n",
getInputFileName ());
}
}
vStringDelete (Signature);
vStringDelete (ReturnType);
cppTerminate ();
return retry;
}
static void buildKeywordHash (const langType language, unsigned int idx)
{
const size_t count = sizeof (KeywordTable) / sizeof (KeywordTable [0]);
size_t i;
for (i = 0 ; i < count ; ++i)
{
const keywordDesc* const p = &KeywordTable [i];
if (p->isValid [idx])
addKeyword (p->name, language, (int) p->id);
}
}
static void initializeCParser (const langType language)
{
Lang_c = language;
buildKeywordHash (language, 0);
}
static void initializeCppParser (const langType language)
{
Lang_cpp = language;
buildKeywordHash (language, 1);
}
static void initializeCsharpParser (const langType language)
{
Lang_csharp = language;
buildKeywordHash (language, 2);
}
static void initializeJavaParser (const langType language)
{
Lang_java = language;
buildKeywordHash (language, 3);
}
static void initializeVeraParser (const langType language)
{
Lang_vera = language;
buildKeywordHash (language, 4);
}
extern parserDefinition* CParser (void)
{
static const char *const extensions [] = { "c", NULL };
parserDefinition* def = parserNew ("C");
def->kinds = CKinds;
def->kindCount = KIND_COUNT (CKinds);
def->extensions = extensions;
def->parser2 = findCTags;
def->initialize = initializeCParser;
return def;
}
extern parserDefinition* CppParser (void)
{
static const char *const extensions [] = {
"c++", "cc", "cp", "cpp", "cxx", "h", "h++", "hh", "hp", "hpp", "hxx",
#ifndef CASE_INSENSITIVE_FILENAMES
"C", "H",
#endif
NULL
};
parserDefinition* def = parserNew ("C++");
def->kinds = CKinds;
def->kindCount = KIND_COUNT (CKinds);
def->extensions = extensions;
def->parser2 = findCTags;
def->initialize = initializeCppParser;
return def;
}
extern parserDefinition* CsharpParser (void)
{
static const char *const extensions [] = { "cs", NULL };
parserDefinition* def = parserNew ("C#");
def->kinds = CsharpKinds;
def->kindCount = KIND_COUNT (CsharpKinds);
def->extensions = extensions;
def->parser2 = findCTags;
def->initialize = initializeCsharpParser;
return def;
}
extern parserDefinition* JavaParser (void)
{
static const char *const extensions [] = { "java", NULL };
parserDefinition* def = parserNew ("Java");
def->kinds = JavaKinds;
def->kindCount = KIND_COUNT (JavaKinds);
def->extensions = extensions;
def->parser2 = findCTags;
def->initialize = initializeJavaParser;
return def;
}
extern parserDefinition* VeraParser (void)
{
static const char *const extensions [] = { "vr", "vri", "vrh", NULL };
parserDefinition* def = parserNew ("Vera");
def->kinds = VeraKinds;
def->kindCount = KIND_COUNT (VeraKinds);
def->extensions = extensions;
def->parser2 = findCTags;
def->initialize = initializeVeraParser;
return def;
}
/* vi:set tabstop=4 shiftwidth=4 noexpandtab: */
|