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
|
/* This file is part of the nesC compiler.
This file is derived from RC and the GNU C Compiler. It is thus
Copyright (C) 1987, 88, 89, 92-7, 1998 Free Software Foundation, Inc.
Copyright (C) 2000-2001 The Regents of the University of California.
Changes for nesC are
Copyright (C) 2002 Intel Corporation
The attached "nesC" software is provided to you under the terms and
conditions of the GNU General Public License Version 2 as published by the
Free Software Foundation.
nesC 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 nesC; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include "parser.h"
#include "types.h"
#include "constants.h"
#include "c-parse.h"
#include "machine.h"
#include "nesc-semantics.h"
#include "nesc-xml.h"
#include "AST_utils.h"
#include <stddef.h>
#include <stdarg.h>
struct type
{
enum { tk_primitive, tk_complex, tk_tagged, tk_error, tk_void,
tk_pointer, tk_function, tk_array, tk_iref, tk_variable,
tk_cref } kind;
type_quals qualifiers;
enum { nx_no, nx_base, nx_derived } network;
data_declaration combiner, basedecl, typedefdecl;
/* size is not used for aggregate types
(as the values may be discovered after the type is created)
or for arrays (as they may be arrays of aggregates)
alignment is used for aggregates and arrays only if it is non-zero
(indicating an alignment overridden by an attribute)
*/
cval size;
cval alignment;
bool user_align;
union {
/* tk_primtive and tk_complex.
The order reflects promotion order (for an arbitrary machine,
see common_primitive_type) */
enum { /* The elements of this enum must be ordered as follows:
- all integral types before tp_first_floating
- do not change the floating type order
- the integral types are ordered by "rank" (see c9x std) and
unsignedness (unsigned > signed) (common_primitive_type
relies on this order)
- The tp_[u]int<n> types must be before tp_char (for the
assert in common_primitive_type). These types are only
used when the corresponding size is not available amongst
short/int/long/long long
If this frontend followed c9x, these types would always exist
and be special integer types distinct from the regular ones
(see the rank stuff), but we're following gcc so they
aren't. If this changes, common_primitive_type,
default_conversion and type_default_conversion need revising.
*/
tp_error,
tp_int2, tp_uint2, tp_int4, tp_uint4, tp_int8, tp_uint8,
tp_char,
tp_signed_char, tp_unsigned_char,
tp_short, tp_unsigned_short,
tp_int, tp_unsigned_int,
tp_long, tp_unsigned_long,
tp_long_long, tp_unsigned_long_long,
/* Used as the rep type of enums whose constants are derived
from template arguments and whose size is hence unknown.
The unknown int type has the highest rank (its unsignedness
is unknown, we assume its signed). */
tp_unknown_int,
tp_first_floating,
tp_float = tp_first_floating, tp_double, tp_long_double,
/* Like tp_unknown_int, but might be a real or integer */
tp_unknown_number,
tp_last
} primitive;
/* tk_tagged */
tag_declaration tag;
/* tk_pointer */
type pointsto;
/* tk_function */
struct {
type returns;
typelist argtypes;
bool varargs;
bool oldstyle;
enum { tkf_c, tkf_event, tkf_command, tkf_task, tkf_generic } fkind;
} fn;
/* tk_array */
struct {
type arrayof;
expression size;
/* If size is a known constant, it is guaranteed >= 0 */
} array;
/* tk_iref */
data_declaration iref;
/* tk_cref */
data_declaration cref;
/* tk_variable */
data_declaration tdecl;
} u;
};
#ifdef RC_ADJUST
static void rc_update_type(struct type *old, struct type *new)
{
regionid base = regionidof(old);
switch (old->kind)
{
case tk_tagged:
RC_UPDATE(base, old->u.tag, new->u.tag);
break;
case tk_pointer:
RC_UPDATE(base, old->u.pointsto, new->u.pointsto);
break;
case tk_function:
RC_UPDATE(base, old->u.fn.returns, new->u.fn.returns);
RC_UPDATE(base, old->u.fn.argtypes, new->u.fn.argtypes);
break;
case tk_array:
RC_UPDATE(base, old->u.array.arrayof, new->u.array.arrayof);
RC_UPDATE(base, old->u.array.size, new->u.array.size);
break;
default:
break;
}
}
static size_t rc_adjust_type(void *x, int by)
{
struct type *p = x;
RC_ADJUST_PREAMBLE;
switch (p->kind)
{
case tk_tagged:
RC_ADJUST(p->u.tag, by);
break;
case tk_pointer:
RC_ADJUST(p->u.pointsto, by);
break;
case tk_function:
RC_ADJUST(p->u.fn.returns, by);
RC_ADJUST(p->u.fn.argtypes, by);
break;
case tk_array:
RC_ADJUST(p->u.array.arrayof, by);
RC_ADJUST(p->u.array.size, by);
break;
default:
break;
}
return sizeof *p;
}
#endif
static region types_region;
static type primitive_types[tp_last];
static type complex_types[tp_last];
type float_type, double_type, long_double_type,
int_type, unsigned_int_type, long_type, unsigned_long_type,
long_long_type, unsigned_long_long_type, short_type, unsigned_short_type,
char_type, char_array_type, wchar_type, wchar_array_type,
unsigned_char_type, signed_char_type, void_type, ptr_void_type,
const_ptr_void_type,
size_t_type, ptrdiff_t_type, intptr_type,
int2_type, uint2_type, int4_type, uint4_type, int8_type, uint8_type,
unknown_int_type, unknown_number_type, error_type;
static type copy_type(type t)
{
type nt = ralloc(types_region, struct type);
*nt = *t;
return nt;
}
static type new_type(int kind)
{
type nt = ralloc(types_region, struct type);
nt->kind = kind;
/*nt->qualifiers = 0;
nt->network = nx_no;
nt->user_align = FALSE;
nt->combiner = nt->basedecl = nt->typedefdecl = NULL;*/
nt->size = nt->alignment = cval_top;
return nt;
}
/* Return the 'complex t' version of basic type t (one of the integral or
floating-point types) */
type make_complex_type(type t)
{
assert(t->kind == tk_primitive);
return qualify_type1(complex_types[t->u.primitive], t);
}
/* Return the base type of complex type t (one of the integral or
floating-point types) */
type make_base_type(type t)
{
assert(t->kind == tk_complex);
return primitive_types[t->u.primitive];
}
/* Return the type t with it's qualifiers set to tq (old qualifiers are
ignored). This is illegal for function types. For arrays, the qualifiers
get pushed down to the base type. */
type make_qualified_type(type t, type_quals qualifiers)
{
/* Push const or volatile down to base type */
if (t->kind == tk_array)
return make_array_type(make_qualified_type(t->u.array.arrayof, qualifiers),
t->u.array.size);
else
{
type nt = copy_type(t);
nt->qualifiers = qualifiers;
return nt;
}
}
/* Return type 'pointer to t' (unqualified) */
type make_pointer_type(type t)
{
type nt = new_type(tk_pointer);
nt->u.pointsto = t;
/* ASSUME: all pointers are the same */
nt->size = make_type_cval(target->tptr.size);
nt->alignment = make_type_cval(target->tptr.align);
return nt;
}
/* Return type 'array [size] of t'. size is optional */
type make_array_type(type t, expression size)
{
type nt = new_type(tk_array);
nt->u.array.arrayof = t;
nt->u.array.size = size;
nt->network = t->network != nx_no ? nx_derived : nx_no;
return nt;
}
/* Return type 'function with argument types argtypes returning t'.
If oldstyle is true, this is an oldstyle function type and
argtypes is NULL */
type make_function_type(type t, typelist argtypes, bool varargs,
bool oldstyle)
{
type nt = new_type(tk_function);
nt->u.fn.fkind = tkf_c;
nt->u.fn.returns = t;
nt->u.fn.argtypes = argtypes;
nt->u.fn.varargs = varargs;
nt->u.fn.oldstyle = oldstyle;
nt->size = nt->alignment = make_type_cval(1);
return nt;
}
type build_function_type(region r, type returns, ...)
{
va_list args;
typelist argtypes;
va_start(args, returns);
argtypes = new_typelist(r);
for (;;)
{
type onearg = va_arg(args, type);
if (!onearg)
break;
typelist_append(argtypes, onearg);
}
return make_function_type(returns, argtypes, FALSE, FALSE);
}
/* Return the tagged type whose declaration is d */
type make_tagged_type(tag_declaration d)
{
type nt = new_type(tk_tagged);
nt->u.tag = d;
if (d->kind == kind_nx_struct_ref || d->kind == kind_nx_union_ref)
nt->network = nx_derived;
return nt;
}
bool type_network(type t)
{
return t->network != nx_no;
}
/* Make the single instance of pk, with specified size and alignment
(Note that we may make copies if this single instance for different alignments)
Requires: must be called at most once for each pk, from types_init
*/
static type make_primitive0(int pk, cval size, cval alignment,
cval complex_size)
{
type nt = new_type(tk_primitive), ct;
nt->u.primitive = pk;
nt->size = size;
nt->alignment = alignment;
primitive_types[pk] = nt;
ct = new_type(tk_complex);
ct->u.primitive = pk;
ct->size = complex_size; /* can't compute as types not available yet */
ct->alignment = nt->alignment; /* ASSUME: alignof(complex t) == alignof(t) */
complex_types[pk] = ct;
return nt;
}
static type make_primitive(int pk, int size, int alignment)
{
return make_primitive0(pk, make_type_cval(size), make_type_cval(alignment),
make_type_cval(size * 2));
}
static type make_unknown_primitive(int pk)
{
return make_primitive0(pk, cval_unknown_number, cval_unknown_number,
cval_unknown_number);
}
static type lookup_primitive(int default_kind, int size, int alignment,
bool isunsigned)
{
int i;
for (i = tp_signed_char; i < tp_unknown_int; i++)
if (cval_uint_value(primitive_types[i]->size) == size &&
type_unsigned(primitive_types[i]) == isunsigned)
return primitive_types[i];
return make_primitive(default_kind, size, alignment);
}
static type lookup_float(int size)
{
int i;
for (i = tp_first_floating; i < tp_unknown_number; i++)
if (cval_uint_value(primitive_types[i]->size) == size)
return primitive_types[i];
return error_type;
}
/* Return the integral type of size 'size', unsigned if 'isunsigned' is true */
type type_for_size(cval size, bool isunsigned)
{
type t;
if (cval_isunknown(size))
return unknown_int_type;
t = lookup_primitive(tp_error, cval_sint_value(size), 0, isunsigned);
assert(t->u.primitive != tp_error);
return t;
}
static type type_for_size_int(int size, bool isunsigned)
{
type t = lookup_primitive(tp_error, size, 0, isunsigned);
assert(t->u.primitive != tp_error);
return t;
}
type type_for_cval(cval c, bool isunsigned)
{
int i;
if (cval_isunknown(c))
return unknown_int_type;
for (i = tp_signed_char; i < tp_unknown_int; i++)
if (type_unsigned(primitive_types[i]) == isunsigned &&
cval_inrange(c, primitive_types[i]))
return primitive_types[i];
return NULL;
}
void init_types(void)
{
types_region = newregion();
float_type = make_primitive
(tp_float, target->tfloat.size, target->tfloat.align);
double_type = make_primitive
(tp_double, target->tdouble.size, target->tdouble.align);
long_double_type = make_primitive
(tp_long_double, target->tlong_double.size, target->tlong_double.align);
short_type = make_primitive
(tp_short, target->tshort.size, target->tshort.align);
unsigned_short_type = make_primitive
(tp_unsigned_short, target->tshort.size, target->tshort.align);
int_type = make_primitive
(tp_int, target->tint.size, target->tint.align);
unsigned_int_type = make_primitive
(tp_unsigned_int, target->tint.size, target->tint.align);
long_type = make_primitive
(tp_long, target->tlong.size, target->tlong.align);
unsigned_long_type = make_primitive
(tp_unsigned_long, target->tlong.size, target->tlong.align);
long_long_type = make_primitive
(tp_long_long, target->tlong_long.size, target->tlong_long.align);
unsigned_long_long_type = make_primitive
(tp_unsigned_long_long, target->tlong_long.size, target->tlong_long.align);
signed_char_type = make_primitive(tp_signed_char, 1, target->int1_align);
unsigned_char_type = make_primitive(tp_unsigned_char, 1, target->int1_align);
char_type = make_primitive(tp_char, 1, target->int1_align);
int2_type = lookup_primitive(tp_int2, 2, target->int2_align, FALSE);
uint2_type = lookup_primitive(tp_uint2, 2, target->int2_align, TRUE);
int4_type = lookup_primitive(tp_int4, 4, target->int4_align, FALSE);
uint4_type = lookup_primitive(tp_uint4, 4, target->int4_align, TRUE);
int8_type = lookup_primitive(tp_int8, 8, target->int8_align, FALSE);
uint8_type = lookup_primitive(tp_uint8, 8, target->int8_align, TRUE);
unknown_int_type = make_unknown_primitive(tp_unknown_int);
unknown_number_type = make_unknown_primitive(tp_unknown_number);
char_array_type = make_array_type(char_type, NULL);
error_type = new_type(tk_error);
error_type->size = error_type->alignment = make_type_cval(1);
void_type = new_type(tk_void);
void_type->size = void_type->alignment = make_type_cval(1);
ptr_void_type = make_pointer_type(void_type);
const_ptr_void_type =
make_pointer_type(make_qualified_type(void_type, const_qualifier));
wchar_type = type_for_size_int(target->wchar_t_size, !target->wchar_t_signed);
wchar_array_type = make_array_type(wchar_type, NULL);
size_t_type = type_for_size_int(target->size_t_size, TRUE);
ptrdiff_t_type = type_for_size_int(target->tptr.size, FALSE);
intptr_type = type_for_size_int(target->tptr.size, TRUE);
}
struct typelist
{
region sameregion r;
struct typelist_element *sameregion first;
};
struct typelist_element
{
struct typelist_element *sameregion next;
type t;
};
typelist new_typelist(region r)
{
typelist tl = ralloc(r, struct typelist);
tl->r = r;
tl->first = NULL;
return tl;
}
void typelist_append(typelist tl, type t)
{
struct typelist_element *nte = ralloc(tl->r, struct typelist_element);
struct typelist_element *sameregion *last;
nte->t = t;
last = &tl->first;
while (*last)
last = &(*last)->next;
*last = nte;
}
bool empty_typelist(typelist tl)
{
return tl->first == NULL;
}
void typelist_scan(typelist tl, typelist_scanner *scanner)
{
*scanner = tl->first;
}
type typelist_next(typelist_scanner *scanner)
{
type t;
if (!*scanner)
return NULL;
t = (*scanner)->t;
*scanner = (*scanner)->next;
return t;
}
type_quals type_qualifiers(type t)
{
/* Arrays don't have qualifiers */
while (t->kind == tk_array)
t = t->u.array.arrayof;
return t->qualifiers;
}
#define Q(name, kind, tq, val) \
bool type_ ## name(type t) \
{ \
return (type_qualifiers(t) & tq) != 0; \
}
#include "qualifiers.h"
#undef Q
bool type_transparent(type t)
{
return (type_qualifiers(t) & transparent_qualifier) != 0;
}
bool type_readonly(type t)
{
return type_const(t) || (type_tagged(t) && type_tag(t)->fields_const);
}
bool type_integral(type t) /* Does not include enum's */
{
return t->kind == tk_primitive && t->u.primitive < tp_first_floating;
}
bool type_smallerthanint(type t)
{
return t->kind == tk_primitive && t->u.primitive < tp_unknown_int &&
cval_intcompare(t->size, int_type->size) < 0;
}
bool type_unsigned(type t)
{
if (t->kind == tk_primitive)
switch (t->u.primitive)
{
case tp_char: return !flag_signed_char;
case tp_unsigned_char:
case tp_unsigned_short:
case tp_unsigned_int:
case tp_unsigned_long:
case tp_unsigned_long_long:
case tp_uint2: case tp_uint4: case tp_uint8:
return TRUE;
default: break;
}
return FALSE;
}
bool type_floating(type t)
{
return t->kind == tk_primitive && t->u.primitive >= tp_first_floating &&
t->u.primitive < tp_unknown_number;
}
bool type_plain_char(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_char;
}
bool type_signed_char(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_signed_char;
}
bool type_unsigned_char(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_unsigned_char;
}
bool type_short(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_short;
}
bool type_unsigned_short(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_unsigned_short;
}
bool type_int(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_int;
}
bool type_unsigned_int(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_unsigned_int;
}
bool type_long(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_long;
}
bool type_unsigned_long(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_unsigned_long;
}
bool type_long_long(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_long_long;
}
bool type_unsigned_long_long(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_unsigned_long_long;
}
bool type_unknown_int(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_unknown_int;
}
bool type_float(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_float;
}
bool type_double(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_double;
}
bool type_long_double(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_long_double;
}
bool type_unknown_number(type t)
{
return t->kind == tk_primitive && t->u.primitive == tp_unknown_number;
}
bool type_unknown(type t) /* unknown_int or unknown_number */
{
return type_unknown_int(t) || type_unknown_number(t);
}
bool type_char(type t)
{
return t->kind == tk_primitive &&
(t->u.primitive == tp_char || t->u.primitive == tp_unsigned_char ||
t->u.primitive == tp_signed_char);
}
bool type_void(type t)
{
return t->kind == tk_void;
}
bool type_function(type t)
{
return t->kind == tk_function && t->u.fn.fkind == tkf_c;
}
bool type_array(type t)
{
return t->kind == tk_array;
}
bool type_pointer(type t)
{
return t->kind == tk_pointer;
}
bool type_complex(type t)
{
return t->kind == tk_complex;
}
bool type_enum(type t)
{
return t->kind == tk_tagged && t->u.tag->kind == kind_enum_ref;
}
bool type_tagged(type t)
{
return t->kind == tk_tagged;
}
bool type_struct(type t)
{
return t->kind == tk_tagged &&
(t->u.tag->kind == kind_struct_ref || t->u.tag->kind == kind_nx_struct_ref);
}
bool type_attribute(type t)
{
return t->kind == tk_tagged && t->u.tag->kind == kind_attribute_ref;
}
bool type_union(type t)
{
return t->kind == tk_tagged &&
(t->u.tag->kind == kind_union_ref || t->u.tag->kind == kind_nx_union_ref);
}
type type_function_return_type(type t)
{
assert(t->kind == tk_function);
return t->u.fn.returns;
}
typelist type_function_arguments(type t)
{
assert(t->kind == tk_function);
return t->u.fn.argtypes;
}
bool type_function_varargs(type t)
{
assert(t->kind == tk_function);
return t->u.fn.varargs;
}
bool type_function_oldstyle(type t)
{
assert(t->kind == tk_function);
return t->u.fn.oldstyle;
}
type type_points_to(type t)
{
assert(t->kind == tk_pointer);
return t->u.pointsto;
}
type type_array_of(type t)
{
assert(t->kind == tk_array);
return t->u.array.arrayof;
}
type type_array_of_base(type t)
{
while (t->kind == tk_array)
t = t->u.array.arrayof;
return t;
}
expression type_array_size(type t)
{
assert(t->kind == tk_array);
return t->u.array.size;
}
cval type_array_size_cval(type t)
/* Returns: number of elements in array type t if known, cval_top otherwise */
{
known_cst s;
if (t->u.array.size && (s = t->u.array.size->cst) &&
(constant_integral(s) || constant_unknown(s)))
return cval_cast(s->cval, size_t_type);
return cval_top;
}
tag_declaration type_tag(type t)
{
assert(t->kind == tk_tagged);
return t->u.tag;
}
bool type_incomplete(type t)
{
return t->kind == tk_void ||
(t->kind == tk_tagged && !t->u.tag->defined) ||
(t->kind == tk_array &&
(type_incomplete(t->u.array.arrayof) || !t->u.array.size));
}
/* Return TRUE if two type lists are equal */
static bool type_lists_equal(typelist al1, typelist al2)
{
struct typelist_element *args1 = al1->first, *args2 = al2->first;
for (;;)
{
if (args1 == 0 && args2 == 0)
return TRUE;
/* If one list is shorter than the other,
they fail to match. */
if (args1 == 0 || args2 == 0)
return FALSE;
if (!type_equal(args1->t, args2->t))
return FALSE;
args1 = args1->next;
args2 = args2->next;
}
}
/* Return TRUE if two function types F1 and F2 are equal. */
bool function_equal(type t1, type t2)
{
/* Function kinds and return types must match */
if (!(t1->u.fn.fkind == t2->u.fn.fkind &&
type_equal(t1->u.fn.returns, t2->u.fn.returns)))
return FALSE;
if (!t1->u.fn.oldstyle && !t2->u.fn.oldstyle)
return t1->u.fn.varargs == t2->u.fn.varargs &&
type_lists_equal(t1->u.fn.argtypes, t2->u.fn.argtypes);
else
return t1->u.fn.oldstyle == t2->u.fn.oldstyle;
}
bool type_equal(type t1, type t2)
{
return t1->qualifiers == t2->qualifiers && type_equal_unqualified(t1, t2);
}
static bool array_sizes_match(type t1, type t2)
/* Return: TRUE if we think the array sizes of t1, t2 match
*/
{
known_cst s1 = t1->u.array.size ? t1->u.array.size->cst : NULL;
known_cst s2 = t2->u.array.size ? t2->u.array.size->cst : NULL;
// Non-constant array sizes match everything
// Unknown array sizes match everything too
// (XXX: we should check again after instantiation, but we'll leave
// that to the backend C compiler)
if (!s1 || !constant_integral(s1) || !s2 || !constant_integral(s2))
return TRUE;
return cval_intcompare(s1->cval, s2->cval) == 0;
}
bool type_equal_unqualified(type t1, type t2)
{
if (t1 == error_type || t2 == error_type)
return TRUE;
/* The unknown types are actually not equal to themselves... */
if (type_unknown_int(t1) || type_unknown_number(t1))
return FALSE;
/* Short-circuit easy case */
if (t1 == t2)
return TRUE;
/* Different classes of types can't be compatible. */
if (t1->kind != t2->kind)
return FALSE;
/* sameregion, traditional and parentptr qualifiers must always match */
if ((t1->qualifiers & ~(const_qualifier | volatile_qualifier | restrict_qualifier | transparent_qualifier)) !=
(t2->qualifiers & ~(const_qualifier | volatile_qualifier | restrict_qualifier | transparent_qualifier)))
return FALSE;
/* Network base types are identified by their declaration */
if (type_network_base_type(t1) || type_network_base_type(t2))
return t1->basedecl == t2->basedecl;
switch (t1->kind)
{
case tk_primitive: case tk_complex:
return t1->u.primitive == t2->u.primitive;
case tk_void:
return TRUE;
case tk_tagged:
return t1->u.tag == t2->u.tag;
case tk_pointer:
return type_equal(t1->u.pointsto, t2->u.pointsto);
case tk_function:
return function_equal(t1, t2);
case tk_array:
return type_equal(t1->u.array.arrayof, t2->u.array.arrayof) &&
array_sizes_match(t1, t2);
case tk_variable:
return t1->u.tdecl == t2->u.tdecl;
default: assert(0); return FALSE;
}
}
/* Return TRUE if T is not affected by default promotions. */
bool type_self_promoting(type t)
{
if (t->kind != tk_primitive)
return TRUE;
switch (t->u.primitive)
{
case tp_float: case tp_char: case tp_unsigned_char: case tp_signed_char:
case tp_short: case tp_unsigned_short:
return FALSE;
default:
return TRUE;
}
}
/* Return TRUE if function type FNTYPE specifies a fixed number of parameters
and none of their types is affected by default promotions.
TRUE for oldstyle functions */
bool self_promoting_args(type fntype)
{
struct typelist_element *parms;
if (type_function_varargs(fntype))
return FALSE;
if (type_function_oldstyle(fntype))
return TRUE;
for (parms = type_function_arguments(fntype)->first; parms;
parms = parms->next)
if (!type_self_promoting(parms->t))
return FALSE;
return TRUE;
}
/* Allow wait (union {union wait *u; int *i})
and wait (union wait *) to be compatible. */
static bool weird_parameter_match(type t1, type t2)
{
tag_declaration t1decl;
if (type_union(t1)
&& (!(t1decl = type_tag(t1))->name || t1decl->transparent_union)
&& type_size_cc(t1) && type_size_cc(t2))
{
cval s1 = type_size(t1), s2 = type_size(t2);
/* We don't do this for types of unknown size */
if (cval_isinteger(s1) && cval_isinteger(s2) &&
cval_intcompare(s1, s2) == 0)
{
field_declaration field;
for (field = t1decl->fieldlist; field; field = field->next)
if (type_compatible(field->type, t2))
return TRUE;
}
}
return FALSE;
}
static type weird_common_parameter(type t1, type t2)
{
/* Given wait (union {union wait *u; int *i} *)
and wait (union wait *),
prefer union wait * as type of parm. */
if (weird_parameter_match(t1, t2))
{
if (pedantic)
pedwarn("function types not truly compatible in ANSI C");
return t2;
}
return NULL;
}
/* Check two lists of types for compatibility,
returning 0 for incompatible, 1 for compatible,
or 2 for compatible with warning. */
static int type_lists_compatible(typelist al1, typelist al2)
{
/* 1 if no need for warning yet, 2 if warning cause has been seen. */
int val = 1;
int newval = 0;
struct typelist_element *args1 = al1->first, *args2 = al2->first;
while (1)
{
if (args1 == 0 && args2 == 0)
return val;
/* If one list is shorter than the other,
they fail to match. */
if (args1 == 0 || args2 == 0)
return 0;
if (!(newval = type_compatible_unqualified(args1->t, args2->t)))
{
if (!weird_parameter_match(args1->t, args2->t) &&
!weird_parameter_match(args2->t, args1->t))
return 0;
}
/* type_compatible said ok, but record if it said to warn. */
if (newval > val)
val = newval;
args1 = args1->next;
args2 = args2->next;
}
}
/* Return 1 if two function types F1 and F2 are compatible.
If either type specifies no argument types,
the other must specify a fixed number of self-promoting arg types.
Otherwise, if one type specifies only the number of arguments,
the other must specify that number of self-promoting arg types.
Otherwise, the argument types must match. */
int function_compatible(type t1, type t2)
{
typelist args1, args2;
/* 1 if no need for warning yet, 2 if warning cause has been seen. */
int val = 1;
/* Kinds and return types must match */
if (!(t1->u.fn.fkind == t2->u.fn.fkind &&
type_compatible(t1->u.fn.returns, t2->u.fn.returns)))
return 0;
args1 = t1->u.fn.argtypes;
args2 = t2->u.fn.argtypes;
/* An unspecified parmlist matches any specified parmlist
whose argument types don't need default promotions. */
if (t1->u.fn.oldstyle)
{
if (args2 && !self_promoting_args(t2))
return 0;
#if 0
/* If one of these types comes from a non-prototype fn definition,
compare that with the other type's arglist.
If they don't match, ask for a warning (but no error). */
if (TYPE_ACTUAL_ARG_TYPES (f1)
&& 1 != type_lists_compatible(args2, TYPE_ACTUAL_ARG_TYPES (f1)))
val = 2;
#endif
return val;
}
if (t2->u.fn.oldstyle)
{
if (args1 && !self_promoting_args(t1))
return 0;
#if 0
if (TYPE_ACTUAL_ARG_TYPES (f2)
&& 1 != type_lists_compatible(args1, TYPE_ACTUAL_ARG_TYPES (f2)))
val = 2;
#endif
return val;
}
if (t1->u.fn.varargs != t2->u.fn.varargs)
return 0;
/* Both types have argument lists: compare them and propagate results. */
return type_lists_compatible(args1, args2);
}
static bool interface_equal(nesc_declaration i1, nesc_declaration i2)
/* Returns: TRUE if the interface types are equal, i.e., they have the
same type arguments to the same base interface
*/
{
type_parm_decl p1 = CAST(type_parm_decl, i1->parameters),
p2 = CAST(type_parm_decl, i2->parameters);
if (original_component(i1) != original_component(i2))
return FALSE;
while (p1 && p2)
{
if (!type_equal(p1->ddecl->type, p2->ddecl->type))
return FALSE;
p1 = CAST(type_parm_decl, p1->next);
p2 = CAST(type_parm_decl, p2->next);
}
return p1 == NULL && p2 == NULL; /* or p1 == p2 ;-) */
}
bool type_compatible_unqualified(type t1, type t2)
{
if (t1 == error_type || t2 == error_type)
return 1;
/* The unknown types are actually not compatible with themselves... */
if (type_unknown_int(t1) || type_unknown_number(t1))
return FALSE;
/* Short-circuit easy case */
if (t1 == t2)
return 1;
/* Different classes of types can't be compatible. */
if (t1->kind != t2->kind)
return 0;
#if 0
/* The combiners must match too */
if (t1->combiner != t2->combiner)
return 0;
#endif
/* sameregion, traditional and parentptr qualifiers must always match */
if ((t1->qualifiers & ~(const_qualifier | volatile_qualifier | restrict_qualifier | transparent_qualifier)) !=
(t2->qualifiers & ~(const_qualifier | volatile_qualifier | restrict_qualifier | transparent_qualifier)))
return 0;
/* Network base types are identified by their declaration */
if (type_network_base_type(t1) || type_network_base_type(t2))
return t1->basedecl == t2->basedecl;
switch (t1->kind)
{
case tk_primitive: case tk_complex:
return t1->u.primitive == t2->u.primitive;
case tk_void:
return 1;
case tk_tagged:
return t1->u.tag == t2->u.tag;
case tk_pointer:
return type_compatible(t1->u.pointsto, t2->u.pointsto);
case tk_function:
return function_compatible(t1, t2);
case tk_array:
return type_compatible(t1->u.array.arrayof, t2->u.array.arrayof) &&
array_sizes_match(t1, t2);
case tk_iref:
return interface_equal(t1->u.iref->itype, t2->u.iref->itype);
case tk_cref:
return t1->u.cref == t2->u.cref;
case tk_variable:
return t1->u.tdecl == t2->u.tdecl;
default: assert(0); return 0;
}
}
bool type_compatible(type t1, type t2)
{
/* Qualifiers must match. */
/* GCC appears to allow changes to restrict (see /usr/include/sys/stat.h
and the decls/defs of stat/lstat in redhat linux 7) */
/* XXX: investigate more. */
if ((t1->qualifiers & ~restrict_qualifier) != (t2->qualifiers & ~restrict_qualifier))
return 0;
else
return type_compatible_unqualified(t1, t2);
}
type qualify_type1(type t, type t1)
{
return make_qualified_type(t, type_qualifiers(t1));
}
type qualify_type2(type t, type t1, type t2)
{
return make_qualified_type(t, type_qualifiers(t1) | type_qualifiers(t2));
}
type align_type(type t, cval new_alignment)
{
type nt = copy_type(t);
nt->alignment = new_alignment;
nt->user_align = TRUE;
return nt;
}
bool type_realigned(type t)
{
return t->user_align;
}
static int common_primitive_type(type t1, type t2)
{
int pk1 = t1->u.primitive, pk2 = t2->u.primitive;
int pk = pk1 < pk2 ? pk2 : pk1;
/* Note: the results of this function depend on the relative sizes of
char, short, int, long and long long. The results are only correct
for a version of C that has types that match those set by types_init
(which uses those from the C compiler that compiles this file)
*/
if (pk1 < tp_first_floating && pk2 < tp_first_floating)
{
largest_uint s1, s2;
/* The simple cases */
if (pk1 == tp_unknown_int || pk2 == tp_unknown_int)
return tp_unknown_int;
s1 = cval_uint_value(t1->size);
s2 = cval_uint_value(t2->size);
if (s1 < s2)
return pk2;
else if (s1 > s2)
return pk1;
/* The pain starts, see 6.3.1.8 of c9x */
/* If the sizes are the same, then we can't have a tp_[u]int<n> or a
tp_char/short/int/long/etc pair (as we only have tp_[u]int<n> if there
is no corresponding integer type of the same size. So we can compare rank
by comparing pk1 and pk2 */
assert(!((pk1 < tp_char && pk2 >= tp_char) ||
(pk1 >= tp_char && pk2 < tp_char)));
/* the higher rank wins, and if either of the types is unsigned, the
result is unsigned (thus unsigned short + int == unsigned int if
sizeof(short) == sizeof(int) */
if ((type_unsigned(t1) || type_unsigned(t2)) && !type_unsigned(primitive_types[pk]))
/* A bit inefficient, admittedly */
pk = make_unsigned_type(primitive_types[pk])->u.primitive;
return pk;
}
/* Floating point types follow the order specified in the enum and win
over all integral types. This includes unknown_number winning over
everybody. */
return pk;
}
/* Return the common type of two types.
This is used in two cases:
- when type_compatible(t1, t2) is true, to pick a merged type for
declarations
- to merge two types that are compatible in some expressions (e.g. ?:,
arithmetic) */
type common_type(type t1, type t2)
{
type rtype;
data_declaration combiner;
/* Save time if the two types are the same. */
if (t1 == t2)
return t1;
/* If one type is nonsense, use the other. */
if (t1 == error_type)
return t2;
if (t2 == error_type)
return t1;
combiner = NULL;
if (t1->combiner == t2->combiner)
combiner = t1->combiner;
/* Special enum handling: if same enum, just merge qualifiers. otherwise
treat an enum type as the unsigned integer type of the same width.
Note that gcc does not check for the same enum. Some weird behaviour...
(see enum2.c)
*/
if (type_enum(t1))
{
if (type_equal_unqualified(t1, t2))
return make_combiner_type(qualify_type2(t1, t1, t2), combiner);
t1 = qualify_type1(type_for_size(type_size(t1), TRUE), t1);
}
if (type_enum(t2))
t2 = qualify_type1(type_for_size(type_size(t1), TRUE), t2);
/* If one type is complex, form the common type of the non-complex
components, then make that complex. */
if (type_complex(t1) || type_complex(t2))
{
int pk = common_primitive_type(t1, t2);
assert((t1->kind == tk_primitive || t1->kind == tk_complex) &&
(t2->kind == tk_primitive || t2->kind == tk_complex));
rtype = complex_types[pk];
}
else
switch (t1->kind)
{
case tk_primitive:
/* We need to preserve equivalent network base types because
common_type is used for redeclarations */
if (type_network_base_type(t1) && type_network_base_type(t2) &&
t1->basedecl == t2->basedecl)
rtype = make_qualified_type(t1, 0);
else
{
int pk = common_primitive_type(t1, t2);
assert(t2->kind == tk_primitive);
rtype = primitive_types[pk];
break;
}
case tk_void: case tk_tagged: case tk_variable:
rtype = t1;
break;
case tk_pointer:
rtype = make_pointer_type(common_type(t1->u.pointsto, t2->u.pointsto));
break;
case tk_array:
{
/* Merge the element types, and have a size if either arg has one. */
type element_type =
common_type(t1->u.array.arrayof, t2->u.array.arrayof);
expression size1 = t1->u.array.size, size2 = t2->u.array.size;
rtype = make_array_type(element_type, size1 ? size1 : size2);
break;
}
case tk_function:
/* Function types: prefer the one that specified arg types.
If both do, merge the arg types. Also merge the return types. */
{
type valtype = common_type(t1->u.fn.returns, t2->u.fn.returns);
typelist args;
bool oldstyle, varargs;
if (t1->u.fn.oldstyle && t2->u.fn.oldstyle)
{
args = NULL;
oldstyle = TRUE;
varargs = FALSE;
}
else if (t1->u.fn.oldstyle)
{
args = t2->u.fn.argtypes;
oldstyle = FALSE;
varargs = t2->u.fn.varargs;
}
else if (t2->u.fn.oldstyle)
{
args = t1->u.fn.argtypes;
oldstyle = FALSE;
varargs = t1->u.fn.varargs;
}
else
{
/* If both args specify argument types, we must merge the two
lists, argument by argument. */
struct typelist_element *args1 = t1->u.fn.argtypes->first;
struct typelist_element *args2 = t2->u.fn.argtypes->first;
type argtype;
oldstyle = FALSE;
varargs = t1->u.fn.varargs;
args = new_typelist(t1->u.fn.argtypes->r);
while (args1)
{
(void) ((argtype = weird_common_parameter(args1->t, args2->t)) ||
(argtype = weird_common_parameter(args2->t, args1->t)) ||
(argtype = common_type(args1->t, args2->t)));
typelist_append(args, argtype);
args1 = args1->next;
args2 = args2->next;
}
}
rtype = make_function_type(valtype, args, varargs, oldstyle);
/* Hack up the function kind */
rtype->u.fn.fkind = t1->u.fn.fkind;
break;
}
default:
assert(0); return NULL;
}
rtype = qualify_type2(rtype, t1, t2);
return make_combiner_type(rtype, combiner);
}
type type_base(type t)
{
while (t->kind == tk_array)
t = t->u.array.arrayof;
return t;
}
bool type_integer(type t)
{
return type_integral(t) || type_enum(t) ||
(type_variable(t) && type_variable_decl(t)->typevar_kind == typevar_integer);
}
bool type_real(type t)
{
return type_integer(t) || type_floating(t) || type_unknown_number(t) ||
(type_variable(t) && type_variable_decl(t)->typevar_kind == typevar_number);
}
bool type_arithmetic(type t)
{
return type_real(t) || type_complex(t);
}
bool type_scalar(type t)
{
return type_arithmetic(t) || type_pointer(t);
}
bool type_aggregate(type t)
{
return type_struct(t) || type_union(t) || type_attribute(t);
}
type make_unsigned_type(type t)
{
if (t->kind != tk_primitive)
return t;
switch (t->u.primitive)
{
case tp_char: case tp_signed_char:
return qualify_type1(unsigned_char_type, t);
case tp_short: return qualify_type1(unsigned_short_type, t);
case tp_int: return qualify_type1(unsigned_int_type, t);
case tp_long: return qualify_type1(unsigned_long_type, t);
case tp_long_long: return qualify_type1(unsigned_long_long_type, t);
case tp_int2: return qualify_type1(uint2_type, t);
case tp_int4: return qualify_type1(uint4_type, t);
case tp_int8: return qualify_type1(uint8_type, t);
default: break;
}
assert(type_unknown_int(t) || type_unknown_number(t) || type_unsigned(t));
return t;
}
static type_element rid2ast(region r, location loc, int keyword, type_element rest)
{
type_element ast = CAST(type_element, new_rid(r, loc, keyword));
ast->next = CAST(node, rest);
return ast;
}
static type_element qualifier2ast(region r, location loc, int keyword, type_element rest)
{
type_element ast = CAST(type_element, new_qualifier(r, loc, keyword));
ast->next = CAST(node, rest);
return ast;
}
static type_element qualifiers2ast(region r, location loc, type_quals quals,
type_element rest)
{
if (quals & volatile_qualifier)
rest = qualifier2ast(r, loc, volatile_qualifier, rest);
if (quals & const_qualifier)
rest = qualifier2ast(r, loc, const_qualifier, rest);
return rest;
}
static type_element primitive2ast(region r, location loc, int primitive,
type_element rest)
{
bool isunsigned = FALSE;
int keyword;
switch (primitive)
{
case tp_unsigned_char:
isunsigned = TRUE;
case tp_char:
keyword = RID_CHAR;
break;
case tp_signed_char:
return rid2ast(r, loc, RID_SIGNED, rid2ast(r, loc, RID_CHAR, rest));
case tp_unsigned_short:
isunsigned = TRUE;
case tp_short:
keyword = RID_SHORT;
break;
case tp_unsigned_int:
isunsigned = TRUE;
case tp_int:
keyword = RID_INT;
break;
case tp_unsigned_long:
isunsigned = TRUE;
case tp_long:
keyword = RID_LONG;
break;
case tp_unsigned_long_long:
isunsigned = TRUE;
case tp_long_long:
keyword = RID_LONG;
rest = rid2ast(r, loc, RID_LONG, rest);
break;
case tp_float:
keyword = RID_FLOAT;
break;
case tp_double:
keyword = RID_DOUBLE;
break;
case tp_long_double:
keyword = RID_DOUBLE;
rest = rid2ast(r, loc, RID_LONG, rest);
break;
default:
assert(0);
keyword = RID_INT; break;
}
rest = rid2ast(r, loc, keyword, rest);
if (isunsigned)
rest = rid2ast(r, loc, RID_UNSIGNED, rest);
return rest;
}
#define UNNAMED_STRUCT_PREFIX "__nesc_unnamed"
void name_tag(tag_declaration tag)
{
/* Name unnamed structs, unions or enums */
if (!tag->name)
{
static long nextid = 4242;
char tagname[sizeof(UNNAMED_STRUCT_PREFIX) + 20];
sprintf(tagname, UNNAMED_STRUCT_PREFIX "%ld", nextid++);
tag->definition->word1 = new_word(parse_region, dummy_location,
str2cstring(parse_region, tagname));
tag->name = tag->definition->word1->cstring.data;
}
}
static type_element tag2ast(region r, location loc, tag_declaration tag,
type_element rest)
{
tag_ref tr;
name_tag(tag);
tr = newkind_tag_ref(r, tag->kind, loc,
new_word(r, loc, str2cstring(r, tag->name)),
NULL, NULL, FALSE);
tr->tdecl = tag;
tr->next = CAST(node, rest);
/* creating a type naming a tag that shadows another is tricky as
the placement of the type affects its meaning.
The current use of type2ast is for declaring temporaries. These are
placed at the start of the closest enclosing block. This is correct as
long as the temporary does not rely on a tag declared in the same
block that shadows a tag in an enclosing scope (as the temporary would
then erroneously refer to the enclosing tag). The simplest check which
detects this situation is any use of a shadowed tag (which I am
assuming are very rare anyway).
The correct solution is that if the type of temporary x refers to tags
t1...tn declared in the current block, then x should be placed just
before the declaration of the latest of ti, the tag in t1...tn which
was declared latest, and the declaration of x should be preceded by
'struct/union/enum ti;'. This is somewhat painful.
To avoid any problems, compile with error_shadow = warn_shadow = 1
(this is what RC does) */
assert(!tag->shadowed);
return CAST(type_element, tr);
}
static type_element typevar2ast(region r, location loc, data_declaration tvar,
type_element rest)
{
type_element tname;
/* Unlike with tags, we don't need to worry about whether the type variable
is shadowed, as type variables are always replaced by their value in
generated code...
If we were to re-output nesC code, we could run into problems. */
tname = CAST(type_element, new_typename(r, loc, tvar));
tname->next = CAST(node, rest);
return tname;
}
static declaration parameter2ast(region r, location loc, type t)
{
variable_decl vd;
data_decl dd;
declarator tdeclarator;
type_element tmodifiers;
/* Build AST for the declaration */
type2ast(r, loc, t, NULL, &tdeclarator, &tmodifiers);
vd = new_variable_decl(r, loc, tdeclarator, NULL, NULL, NULL, NULL);
vd->declared_type = t;
dd = new_data_decl(r, loc, tmodifiers, CAST(declaration, vd));
return CAST(declaration, dd);
}
/* Build AST nodes such that "MODIFIERS D" represents the declaration of
"T INSIDE", at location loc, allocating in region r */
void type2ast(region r, location loc, type t, declarator inside,
declarator *d, type_element *modifiers)
{
/* XXX: De-recursify */
type_element qualifiers = qualifiers2ast(r, loc, t->qualifiers, NULL);
/* A network base type uses its typedef name (it is effectively a new
type, not a typedef) */
if (type_network_base_type(t))
{
typename tname = new_typename(r, loc, t->basedecl);
tname->next = CAST(node, qualifiers);
*d = inside;
*modifiers = CAST(type_element, tname);
return;
}
switch (t->kind)
{
case tk_primitive:
*modifiers = primitive2ast(r, loc, t->u.primitive, qualifiers);
*d = inside;
break;
case tk_complex:
*modifiers =
rid2ast(r, loc, RID_COMPLEX,
primitive2ast(r, loc, t->u.primitive, qualifiers));
*d = inside;
break;
case tk_tagged:
*modifiers = tag2ast(r, loc, t->u.tag, qualifiers);
*d = inside;
break;
case tk_void:
*modifiers = rid2ast(r, loc, RID_VOID, qualifiers);
*d = inside;
break;
case tk_pointer:
if (qualifiers)
inside = CAST(declarator,
new_qualified_declarator(r, loc, inside, qualifiers));
inside = CAST(declarator,
new_pointer_declarator(r, loc, inside));
type2ast(r, loc, t->u.pointsto, inside, d, modifiers);
break;
case tk_array:
assert(qualifiers == NULL);
inside = CAST(declarator,
new_array_declarator(r, loc, inside, t->u.array.size));
type2ast(r, loc, t->u.array.arrayof, inside, d, modifiers);
break;
case tk_function: {
declaration parms;
assert(t->u.fn.fkind == tkf_c); /* XXX: not done for nesC stuff yet
(not needed yet) */
/* XXX: doesn't rebuild fn qualifiers. Are we generating C here
or not ? */
/* XXX: Should build environment for parameters */
if (t->u.fn.oldstyle)
parms = NULL;
else if (empty_typelist(t->u.fn.argtypes))
parms = parameter2ast(r, loc, void_type);
else
{
struct typelist_element *args = t->u.fn.argtypes->first;
declaration *lastparm = &parms;
while (args)
{
*lastparm = parameter2ast(r, loc, args->t);
lastparm = (declaration *)&(*lastparm)->next;
args = args->next;
}
if (t->u.fn.varargs)
{
*lastparm = CAST(declaration, new_ellipsis_decl(r, loc));
lastparm = (declaration *)&(*lastparm)->next;
}
*lastparm = NULL;
}
inside = CAST(declarator,
new_function_declarator(r, loc, inside, parms, NULL, NULL, NULL));
type2ast(r, loc, t->u.fn.returns, inside, d, modifiers);
break;
}
case tk_variable:
*modifiers = typevar2ast(r, loc, t->u.tdecl, qualifiers);
*d = inside;
break;
default: assert(0); break;
}
}
bool type_contains_pointers(type t)
{
field_declaration field;
if (type_pointer(t))
return TRUE;
if (type_array(t))
return type_contains_pointers(type_array_of(t));
if (!type_aggregate(t))
return FALSE;
for (field = type_tag(t)->fieldlist; field; field = field->next)
if (type_contains_pointers(field->type))
return TRUE;
return FALSE;
}
bool type_contains_union_with_pointers(type t)
{
field_declaration field;
if (type_array(t))
return type_contains_union_with_pointers(type_array_of(t));
if (type_union(t))
return type_contains_pointers(t);
if (!type_struct(t))
return FALSE;
for (field = type_tag(t)->fieldlist; field; field = field->next)
if (type_contains_union_with_pointers(field->type))
return TRUE;
return FALSE;
}
type type_default_conversion(type from)
{
if (type_enum(from))
from = type_tag(from)->reptype;
if (type_smallerthanint(from))
{
/* Traditionally, unsignedness is preserved in default promotions. */
if (flag_traditional && type_unsigned(from))
return unsigned_int_type;
else
return int_type;
}
/* Note: if we had a type of same size as int, but of lesser rank, we should be
returning one of int/unsigned int here, but we don't support that kind of
type */
if (flag_traditional && !flag_allow_single_precision && type_float(from))
return double_type;
if (type_function(from))
return make_pointer_type(from);
if (type_array(from))
return make_pointer_type(type_array_of(from));
if (type_variable(from))
{
data_declaration vdecl = type_variable_decl(from);
switch (vdecl->typevar_kind)
{
case typevar_integer: return unknown_int_type;
case typevar_number: return unknown_number_type;
default: break;
}
}
return from;
}
/* would be called type_default_function_array_conversion in gcc 3.x */
type type_default_conversion_for_assignment(type from)
{
if (type_array(from) || type_function(from))
return type_default_conversion(from);
else
return from;
}
type function_call_type(function_call fcall)
{
type fntype = fcall->arg1->type;
if (type_pointer(fntype))
fntype = type_points_to(fntype);
assert(type_functional(fntype));
return fntype;
}
cval type_size(type t)
/* Requires: type_size_cc(t)
Returns: size of type t in a cval. The cval is either unknown (for types
derived in some way from template arguments) or an unsigned number
*/
{
assert(type_size_cc(t));
if (type_tagged(t))
return t->u.tag->size;
if (type_array(t))
return cval_times(type_array_size_cval(t), type_size(t->u.array.arrayof));
return t->size;
}
largest_uint type_size_int(type t)
/* Requires: type_size_cc(t) && cval_isinteger(type_size(t))
(i.e., t not variable or unknown size)
Returns: size of t
*/
{
return cval_uint_value(type_size(t));
}
cval type_alignment(type t)
{
assert(type_has_size(t));
if (!cval_istop(t->alignment)) /* Possibly overridden alignment */
return t->alignment;
if (type_tagged(t))
return t->u.tag->alignment;
if (type_array(t))
return type_alignment(t->u.array.arrayof);
/* We should never get here (all types have non-zero alignment) */
assert(0);
return t->alignment;
}
/* True if the sizeof of t is a compile-time constant (known or unknown)
*/
bool type_size_cc(type t)
{
if (!type_has_size(t))
return FALSE;
if (type_tagged(t))
return !cval_istop(t->u.tag->size);
if (type_array(t))
return !cval_istop(type_array_size_cval(t)) &&
type_size_cc(t->u.array.arrayof);
return TRUE;
}
bool type_has_size(type t)
{
/* Similar, but not identical to, type_incomplete */
return type_void(t) || !type_incomplete(t);
}
char *qualifier_name(type_quals q)
{
switch (q)
{
default: abort(); return NULL;
#define Q(name, kind, tq, val) case tq: return #name;
#include "qualifiers.h"
#undef Q
}
}
/* nesc types */
bool type_command(type t)
{
return t->kind == tk_function && t->u.fn.fkind == tkf_command;
}
bool type_event(type t)
{
return t->kind == tk_function && t->u.fn.fkind == tkf_event;
}
bool type_task(type t)
{
return t->kind == tk_function && t->u.fn.fkind == tkf_task;
}
bool type_generic(type t)
{
return t->kind == tk_function && t->u.fn.fkind == tkf_generic;
}
type make_command_type(type t, typelist argtypes, bool varargs)
{
type newt = make_function_type(t, argtypes, varargs, FALSE);
newt->u.fn.fkind = tkf_command;
return newt;
}
type make_event_type(type t, typelist argtypes, bool varargs)
{
type newt = make_function_type(t, argtypes, varargs, FALSE);
newt->u.fn.fkind = tkf_event;
return newt;
}
type make_task_type(type t, typelist argtypes, bool varargs)
{
type newt = make_function_type(t, argtypes, varargs, FALSE);
newt->u.fn.fkind = tkf_task;
return newt;
}
type make_generic_type(type t, typelist argtypes)
{
type newt = make_function_type(t, argtypes, FALSE, FALSE);
newt->u.fn.fkind = tkf_generic;
return newt;
}
type make_interface_type(data_declaration itype)
{
type nt = new_type(tk_iref);
nt->u.iref = itype;
/* These are not yet stored, but I'll assume they might be like
pointers some day... */
/* ASSUME: all pointers are the same */
nt->size = make_type_cval(target->tptr.size);
nt->alignment = make_type_cval(target->tptr.align);
return nt;
}
bool type_interface(type t)
{
return t->kind == tk_iref;
}
data_declaration type_iref(type t)
{
assert(type_interface(t));
return t->u.iref;
}
type make_component_type(data_declaration ctype)
{
type nt = new_type(tk_cref);
nt->u.cref = ctype;
/* These are not yet stored, but I'll assume they might be like
pointers some day... */
/* ASSUME: all pointers are the same */
nt->size = make_type_cval(target->tptr.size);
nt->alignment = make_type_cval(target->tptr.align);
return nt;
}
bool type_component(type t)
{
return t->kind == tk_cref;
}
data_declaration type_cref(type t)
{
assert(type_component(t));
return t->u.cref;
}
bool type_functional(type t)
{
return t->kind == tk_function && t->u.fn.fkind != tkf_generic;
}
type make_combiner_type(type t, data_declaration combiner)
{
type nt;
if (!combiner)
return t;
nt = copy_type(t);
nt->combiner = combiner;
return nt;
}
data_declaration type_combiner(type t)
{
return t->combiner;
}
void set_typedef_type(data_declaration def, bool network)
/* Requires: def->kind == decl_typedef
Effects: Sets def's type to remember the typedef it comes from
If network is true, the type becomes a network base type
*/
{
type nt = copy_type(def->type);
if (network)
{
nt->network = nx_base;
nt->alignment = make_type_cval(1);
nt->basedecl = def;
}
nt->typedefdecl = def;
def->type = nt;
}
data_declaration type_typedef(type t)
/* Returns: the typedef t comes from, or NULL if none
*/
{
return t->typedefdecl;
}
data_declaration type_networkdef(type t)
/* Requires: type_network_base_type(t)
Returns: the network base type definition for t
*/
{
assert(type_network_base_type(t));
return t->basedecl;
}
bool type_network_base_type(type t)
{
return t->network == nx_base;
}
type type_network_platform_type(type t)
/* Requires: type_network_base_type(t)
Returns: t's non-network base type
*/
{
return type_networkdef(t)->basetype;
}
/* Type variables */
type make_variable_type(data_declaration tdecl)
/* Requires: tdecl->kind == decl_typedef.
*/
{
type nt = new_type(tk_variable);
nt->u.tdecl = tdecl;
/* Type variables have unknown size and alignment */
nt->size = nt->alignment = cval_unknown_number;
return nt;
}
bool type_variable(type t)
{
return t->kind == tk_variable;
}
data_declaration type_variable_decl(type t)
{
assert(type_variable(t));
return t->u.tdecl;
}
typelist instantiate_typelist(typelist old)
/* Returns: An instantiated copy of typelist old, allocated in the same
region
*/
{
typelist new = new_typelist(old->r);
struct typelist_element *scan = old->first;
while (scan)
{
typelist_append(new, instantiate_type(scan->t));
scan = scan->next;
}
return new;
}
type instantiate_type(type t)
/* Effects: Instantiate a type with type variables based on the instantiation
of the variables and tag declarations. These are found in
type_variable_decl(vartype)->instantiation->type for variables
type_tag(tagtype)->instantiation for tags
Returns: The instantiated type
*/
{
type newt = NULL;
/* Instantiating an unknown type is not possible (we don't know what
type to produce) */
assert(!type_unknown(t));
switch (t->kind)
{
case tk_tagged:
if (t->u.tag->instantiation)
newt = make_tagged_type(t->u.tag->instantiation);
break;
case tk_pointer:
newt = make_pointer_type(instantiate_type(t->u.pointsto));
break;
case tk_function: {
type ret = instantiate_type(t->u.fn.returns);
typelist args = NULL;
if (t->u.fn.argtypes)
args = instantiate_typelist(t->u.fn.argtypes);
newt = make_function_type(ret, args, t->u.fn.varargs, t->u.fn.oldstyle);
newt->u.fn.fkind = t->u.fn.fkind;
break;
}
case tk_array:
newt = make_array_type(instantiate_type(t->u.array.arrayof),
!t->u.array.size ? NULL :
CAST(expression, t->u.array.size->instantiation));
break;
case tk_iref:
if (t->u.iref->instantiation)
newt = make_interface_type(t->u.iref->instantiation);
break;
case tk_cref:
if (t->u.cref->instantiation)
newt = make_component_type(t->u.cref->instantiation);
break;
case tk_variable:
if (t->u.tdecl->instantiation)
newt = t->u.tdecl->instantiation->type;
break;
default:
break;
}
if (newt)
return make_qualified_type(newt, t->qualifiers);
else
return t;
}
static char *primname[] = {
NULL, /* error */
"int16_t",
"uint16_t",
"int32_t",
"uint32_t",
"int64_t",
"uint64_t",
"char",
"signed char",
"unsigned char",
"short",
"unsigned short",
"int",
"unsigned int",
"long",
"unsigned long",
"long long",
"unsigned long long",
"unknown int",
"float",
"double",
"long double",
"unknown number"
};
static const char *rconcat(region r, const char *s1, const char *s2)
{
int l = strlen(s1) + strlen(s2) + 1;
char *s = rstralloc(r, l);
strcpy(s, s1);
strcat(s, s2);
return s;
}
static const char *add_qualifiers(region r, type_quals qs, const char *to)
{
type_quals q;
for (q = 1; q < last_qualifier; q <<= 1)
if (qs & q)
{
to = rconcat(r, " ", to);
to = rconcat(r, qualifier_name(q), to);
}
return to;
}
static const char *add_parens(region r, const char *to)
{
to = rconcat(r, "(", to);
to = rconcat(r, to, ")");
return to;
}
static void split_type_name(region r, type t, const char **prefix,
const char **decls, bool decls_is_star)
{
const char *basic;
switch (t->kind)
{
case tk_primitive:
basic = primname[t->u.primitive];
basic = add_qualifiers(r, t->qualifiers, basic);
break;
case tk_complex:
basic = rconcat(r, "complex ", primname[t->u.primitive]);
basic = add_qualifiers(r, t->qualifiers, basic);
break;
case tk_void:
basic = "void";
basic = add_qualifiers(r, t->qualifiers, basic);
break;
case tk_pointer:
*decls = add_qualifiers(r, t->qualifiers, *decls);
*decls = rconcat(r, "*", *decls);
split_type_name(r, t->u.pointsto, &basic, decls, TRUE);
break;
case tk_array:
/* can't have qualifiers here - see make_qualified_type */
if (decls_is_star)
*decls = add_parens(r, *decls);
*decls = rconcat(r, *decls, "[]");
split_type_name(r, t->u.array.arrayof, &basic, decls, FALSE);
break;
case tk_function: {
const char *args= "";
if (!t->u.fn.oldstyle)
{
typelist_scanner scanargs;
type argt;
bool first = TRUE;
typelist_scan(t->u.fn.argtypes, &scanargs);
while ((argt = typelist_next(&scanargs)))
{
if (!first)
args = rconcat(r, args, ", ");
args = rconcat(r, args, type_name(r, argt));
first = FALSE;
}
if (t->u.fn.varargs)
args = rconcat(r, args, ", ...");
}
if (decls_is_star)
*decls = add_parens(r, *decls);
if (t->qualifiers)
/* This isn't legal C syntax, but seems the reasonable rep */
*decls = add_parens(r, add_qualifiers(r, t->qualifiers, *decls));
*decls = rconcat(r, *decls, add_parens(r, args));
split_type_name(r, t->u.fn.returns, &basic, decls, FALSE);
break;
}
case tk_tagged: {
tag_declaration tdecl = t->u.tag;
basic = rconcat(r, tagkind_name(tdecl->kind), " ");
if (tdecl->container)
{
basic = rconcat(r, basic, tdecl->container->name);
basic = rconcat(r, basic, ".");
}
if (tdecl->name)
basic = rconcat(r, basic, tdecl->name);
else
basic = rconcat(r, basic, "/*anon*/");
basic = add_qualifiers(r, t->qualifiers, basic);
break;
}
default: /* for bugs, tk_error tk_iref tk_cref */
basic = "error";
break;
}
*prefix = basic;
}
const char *type_name(region r, type t)
{
const char *prefix, *decls;
decls = "";
split_type_name(r, t, &prefix, &decls, FALSE);
if (decls[0])
return rconcat(r, prefix, rconcat(r, " ", decls));
else
return prefix;
}
static void nxml_typedef(data_declaration tdef)
{
xindent();
xstartline();
xml_tag("typename");
nxml_ddecl_ref(tdef);
xml_pop();
xnewline();
xunindent();
}
void nxml_type(type t)
{
type_quals quals = type_qualifiers(t) & (const_qualifier | volatile_qualifier | restrict_qualifier);
data_declaration tdef = type_typedef(t);
xstartline();
if (quals)
{
indentedtag_start("type-qualified");
#define Q(name, kind, tq, val) \
if (quals & val) xml_attr_noval(# name);
#include "qualifiers.h"
#undef Q
xml_tag_end();
xnewline();
if (tdef)
{
nxml_typedef(tdef);
tdef = NULL;
}
}
switch (t->kind)
{
case tk_primitive:
if (t->u.primitive < tp_first_floating)
xml_tag_start("type-int");
else
xml_tag_start("type-float");
xml_attr("cname", primname[t->u.primitive]);
xml_attr_bool("unsigned", type_unsigned(t));
break;
case tk_complex:
if (t->u.primitive < tp_first_floating)
{
xml_tag_start("type-complex-int");
xml_attr_bool("unsigned", type_unsigned(primitive_types[t->u.primitive]));
}
else
xml_tag_start("type-complex-float");
xml_attr("cname", primname[t->u.primitive]);
break;
case tk_void:
xml_tag_start("type-void");
break;
case tk_pointer:
xml_tag_start("type-pointer");
break;
case tk_array:
xml_tag_start("type-array");
xml_attr_cval("elements", type_array_size_cval(t));
break;
case tk_function:
xml_tag_start("type-function");
xml_attr_bool("oldstyle", t->u.fn.oldstyle);
xml_attr_bool("varargs", t->u.fn.varargs);
break;
case tk_tagged:
xml_tag_start("type-tag");
break;
case tk_iref:
xml_tag_start("type-interface");
break;
case tk_cref:
xml_tag_start("type-component");
break;
case tk_variable:
xml_tag_start("type-var");
break;
default: /* for bugs */
xml_tag_start("type-error");
break;
}
xml_attr_cval("size", type_size_cc(t) ? type_size(t) : cval_top);
xml_attr_cval("alignment", type_has_size(t) ? type_alignment(t) : cval_top);
if (t->network == nx_base)
xml_attr("network", t->basedecl->name);
switch (t->kind)
{
default:
xml_tag_end();
break;
case tk_pointer:
xml_tag_end();
xnewline(); xindent();
nxml_type(t->u.pointsto);
xunindent();
break;
case tk_array:
xml_tag_end();
xnewline(); xindent();
nxml_type(t->u.array.arrayof);
xunindent();
break;
case tk_function:
xml_tag_end();
xnewline(); xindent();
nxml_type(t->u.fn.returns);
if (!t->u.fn.oldstyle)
nxml_typelist("function-parameters", t->u.fn.argtypes);
xunindent();
break;
case tk_tagged:
xml_tag_end();
nxml_tdecl_ref(t->u.tag);
break;
case tk_iref:
xml_tag_end();
nxml_ddecl_ref(t->u.iref);
break;
case tk_cref:
xml_tag_end();
nxml_ddecl_ref(t->u.cref);
break;
case tk_variable:
xml_tag_end();
nxml_ddecl_ref(t->u.tdecl);
break;
}
if (tdef)
nxml_typedef(tdef);
xml_pop();
xnewline();
if (quals)
indentedtag_pop();
}
void nxml_typelist(const char *name, typelist types)
{
typelist_scanner scantypes;
type t;
indentedtag(name);
typelist_scan(types, &scantypes);
while ((t = typelist_next(&scantypes)))
nxml_type(t);
indentedtag_pop();
}
enum {
TYPE_HASH_PRIMITIVE = 1,
TYPE_HASH_COMPLEX = TYPE_HASH_PRIMITIVE + tp_last,
TYPE_HASH_ERROR = TYPE_HASH_COMPLEX + tp_last,
TYPE_HASH_VOID,
TYPE_HASH_FUNCTION
};
/* Return a hash value to distinguish types. Note that we are much
less careful here than in type_equal, since hash collisions only
effect performance. */
unsigned long type_hash(type t)
{
switch (t->kind)
{
case tk_error:
return TYPE_HASH_ERROR;
case tk_primitive:
return TYPE_HASH_PRIMITIVE + t->u.primitive;
case tk_complex:
return TYPE_HASH_COMPLEX + t->u.primitive;
case tk_void:
return TYPE_HASH_VOID;
case tk_tagged:
return hash_ptr(t->u.tag);
case tk_pointer:
return (type_hash(t->u.pointsto) << 1) ^ 0x51353157;
case tk_function:
return TYPE_HASH_FUNCTION;
case tk_array:
return (type_hash(t->u.array.arrayof) << 1) ^ 0x40142453 ;
default:
assert(0); return 0;
}
}
/* True if an assignment to type child could modify a value of type parent
according to the ANSI C rules.
Note: child cannot be an array type (assignments to arrays do not exist
in C)
*/
bool type_contains(type parent, type child)
{
assert(!type_array(child));
/* Short-circuit easy case */
if (parent == child)
return TRUE;
/* Char writes can be used on any value */
if (type_char(child))
return TRUE;
switch (parent->kind)
{
default:
/* for primitive, void, function */
return type_equal_unqualified(parent, child);
case tk_complex:
/* true if child is primitive or complex and same base primitive type */
return (child->kind == tk_primitive || child->kind == tk_complex) &&
parent->u.primitive == child->u.primitive;
case tk_tagged: {
/* Same tags -> yes. Otherwise, for structs, unions: true if parent
has a field type that contains child */
field_declaration field;
if (child->kind == tk_tagged && parent->u.tag == child->u.tag)
return TRUE;
if (parent->u.tag->kind == kind_enum_ref)
return FALSE;
for (field = parent->u.tag->fieldlist; field; field = field->next)
if (type_contains(field->type, child))
return TRUE;
return FALSE;
}
case tk_pointer:
/* base types must match, but can have different qualifiers
(this is different from type_equal) */
return
child->kind == tk_pointer &&
type_equal_unqualified(parent->u.pointsto, child->u.pointsto);
case tk_array: {
type base_parent = parent;
while (base_parent->kind == tk_array)
base_parent = base_parent->u.array.arrayof;
return type_contains(base_parent, child);
}
}
}
bool type_charstar(type t)
{
return type_pointer(t) && type_char(type_points_to(t));
}
bool type_chararray(type t, bool no_size_allowed)
{
return t == char_array_type || /* check for easy, common case first */
(type_array(t) && type_char(type_array_of(t)) &&
!(no_size_allowed && type_array_size(t)));
}
bool type_wchararray(type t, bool no_size_allowed)
{
return t == wchar_array_type || /* check for easy, common case first */
(type_array(t) && type_equal(wchar_type, type_array_of(t)) &&
!(no_size_allowed && type_array_size(t)));
}
/* See gcc's machmode.def for the source of this mode data. This is a very
simplified form.
*/
typedef enum {
m_int, m_float, m_cint, m_cfloat
} machmode_t;
type type_for_mode(const char *mode, bool isunsigned)
/* Returns: type (unsigned if 'unsigned' is TRUE) corresponding to the
specified mode
*/
{
int i;
static struct { char *name; size_t s; machmode_t m; } modes[] = {
{ "byte", 1, m_int },
{ "word", 0, m_int },
{ "pointer", 0, m_int },
{ "QI", 1, m_int },
{ "HI", 2, m_int },
{ "SI", 4, m_int },
{ "DI", 8, m_int },
{ "TI", 16, m_int },
{ "OI", 32, m_int },
{ "QF", 1, m_float },
{ "HF", 2, m_float },
{ "TQF", 3, m_float },
{ "SF", 4, m_float },
{ "DF", 8, m_float },
{ "XF", 12, m_float },
{ "TF", 16, m_float },
{ "QC", 1, m_cfloat },
{ "HC", 2, m_cfloat },
{ "SC", 4, m_cfloat },
{ "DC", 8, m_cfloat },
{ "XC", 12, m_cfloat },
{ "TC", 16, m_cfloat },
{ "CQI", 1, m_cint },
{ "CHI", 2, m_cint },
{ "CSI", 4, m_cint, },
{ "CDI", 8, m_int },
{ "CTI", 16, m_cint },
{ "COI", 32, m_cint }
};
modes[1].s = target->word_size;
modes[2].s = target->tptr.size;
for (i = 0; i < sizeof modes / sizeof *modes; i++)
if (is_attr_name(mode, modes[i].name))
{
type t;
switch (modes[i].m)
{
case m_int: case m_cint:
t = lookup_primitive(tp_error, modes[i].s, 0, isunsigned);
break;
case m_float: case m_cfloat:
t = lookup_float(modes[i].s);
break;
}
if (t->u.primitive == tp_error)
return NULL;
if (modes[i].m == m_cint || modes[i].m == m_cfloat)
t = make_complex_type(t);
return t;
}
return NULL;
}
|