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
|
/*
* The parse tree transformation module for SIP.
*
* Copyright (c) 2005
* Riverbank Computing Limited <info@riverbankcomputing.co.uk>
*
* This file is part of SIP.
*
* This copy of SIP is licensed for use under the terms of the SIP License
* Agreement. See the file LICENSE for more details.
*
* SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include "sip.h"
#define pyToBasicInt(t) ((t) == short_type || (t) == ushort_type || \
(t) == enum_type || (t) == bool_type || \
(t) == int_type || (t) == uint_type || \
(t) == long_type || (t) == ulong_type)
#define pyToInt(t) (pyToBasicInt(t) || (t) == cint_type)
#define pyToDouble(t) (pyToBasicInt(t) || \
(t) == float_type || (t) == cfloat_type || \
(t) == double_type || (t) == cdouble_type)
static int supportedType(classDef *,overDef *,argDef *,int);
static int sameOverload(overDef *od1,overDef *od2);
static int sameVirtualHandler(virtHandlerDef *vhd1,virtHandlerDef *vhd2);
static int isSubClass(classDef *cc,classDef *pc);
static void ensureInput(classDef *,overDef *,argDef *);
static void defaultInput(argDef *);
static void defaultOutput(classDef *,overDef *,argDef *);
static void assignClassNrs(sipSpec *,moduleDef *,nodeDef *);
static void assignEnumNrs(sipSpec *pt);
static void positionClass(classDef *);
static void addNodeToParent(nodeDef *,classDef *);
static void addAutoOverload(sipSpec *,classDef *,overDef *);
static void ifaceFileIsUsed(ifaceFileDef *,argDef *);
static void ifaceFilesAreUsed(ifaceFileDef *,overDef *);
static void ifaceFilesAreUsedByMethod(classDef *,memberDef *);
static void ifaceFilesAreUsedByOther(sipSpec *pt,overDef *od);
static void scopeDefaultValue(sipSpec *,classDef *,argDef *);
static void setHierarchy(sipSpec *,classDef *,classDef *,classList **);
static void transformCtors(sipSpec *,classDef *);
static void addDefaultCopyCtor(classDef *);
static void transformOverloads(sipSpec *,classDef *,overDef *);
static void transformVariableList(sipSpec *);
static void transformMappedTypes(sipSpec *);
static void getVisibleMembers(sipSpec *,classDef *);
static void getVirtuals(sipSpec *pt,classDef *cd);
static void getClassVirtuals(classDef *,classDef *);
static void transformTypedefs(sipSpec *pt);
static void resolveMappedTypeTypes(sipSpec *,mappedTypeDef *);
static void resolveCtorTypes(sipSpec *,classDef *,ctorDef *);
static void resolveFuncTypes(sipSpec *,classDef *,overDef *);
static void resolvePySigTypes(sipSpec *,classDef *,overDef *,signatureDef *,
int);
static void resolveVariableType(sipSpec *,varDef *);
static void fatalNoDefinedType(scopedNameDef *);
static void getBaseType(sipSpec *,classDef *,argDef *);
static void searchScope(sipSpec *,classDef *,scopedNameDef *,argDef *);
static void searchMappedTypes(sipSpec *,scopedNameDef *,argDef *);
static void searchTypedefs(sipSpec *,scopedNameDef *,argDef *);
static void searchEnums(sipSpec *,scopedNameDef *,argDef *);
static void searchClasses(sipSpec *,scopedNameDef *,argDef *);
static void appendToMRO(mroDef *,mroDef ***,classDef *);
static void filterVirtualHandlers(sipSpec *pt,moduleDef *mod);
static ifaceFileDef *getIfaceFile(argDef *ad);
/*
* Transform the parse tree.
*/
void transform(sipSpec *pt)
{
int nr;
moduleListDef *mld;
classDef *cd, *rev, **tail;
classList *newl;
overDef *od;
mappedTypeDef *mtd;
if (pt -> module -> name == NULL)
fatal("No %%Module has been specified for the module\n");
/*
* The class list has the main module's classes at the front and the
* ones from the module at the most nested %Import at the end. This
* affects some of the following algorithms, eg. when assigning class
* numbers. We have to have consistency whenever a module is used. To
* achieve this we reverse the order of the classes.
*/
rev = NULL;
cd = pt -> classes;
while (cd != NULL)
{
classDef *next = cd -> next;
cd -> next = rev;
rev = cd;
cd = next;
}
pt -> classes = rev;
/*
* Give each module directly imported from the main module a number
* that corresponds to its index in the main module's import table.
*/
nr = 0;
for (mld = pt -> module -> imports; mld != NULL; mld = mld -> next)
mld -> module -> modulenr = nr++;
/* Check each class has been defined. */
for (cd = pt -> classes; cd != NULL; cd = cd -> next)
if (cd -> iff -> module == NULL)
{
fatalScopedName(cd -> iff -> fqcname);
fatal(" has not been defined\n");
}
/*
* Set the super-class hierarchy for each class and re-order the list
* of classes so that no class appears before a super class or an
* enclosing scope class.
*/
newl = NULL;
for (cd = pt -> classes; cd != NULL; cd = cd -> next)
setHierarchy(pt,cd,cd,&newl);
/* Replace the old list with the new one. */
tail = &pt -> classes;
while (newl != NULL)
{
classList *cl = newl;
*tail = cl -> cd;
tail = &cl -> cd -> next;
newl = cl -> next;
free(cl);
}
*tail = NULL;
/* Transform typedefs, variables and global functions. */
transformTypedefs(pt);
transformVariableList(pt);
transformOverloads(pt,NULL,pt -> overs);
/* Transform class ctors and functions. */
if (!pt -> genc)
for (cd = pt -> classes; cd != NULL; cd = cd -> next)
{
transformCtors(pt,cd);
transformOverloads(pt,cd,cd -> overs);
}
/* Transform mapped types based on templates. */
transformMappedTypes(pt);
/* Add default ctors now that the argument types are resolved. */
if (!pt -> genc)
for (cd = pt -> classes; cd != NULL; cd = cd -> next)
if (!isOpaque(cd))
addDefaultCopyCtor(cd);
/*
* Go through each class and add it to it's defining module's tree of
* classes. The tree reflects the namespace hierarchy.
*/
for (cd = pt -> classes; cd != NULL; cd = cd -> next)
addNodeToParent(&cd -> iff -> module -> root,cd);
for (cd = pt -> classes; cd != NULL; cd = cd -> next)
positionClass(cd);
/*
* Assign module specific class numbers for this module and any we
* import directly.
*/
assignClassNrs(pt,pt -> module,&pt -> module -> root);
for (mld = pt -> module -> imports; mld != NULL; mld = mld -> next)
assignClassNrs(pt,mld -> module,&mld -> module -> root);
/* Assign module specific enum numbers for all enums. */
assignEnumNrs(pt);
/* Add any automatically generated methods. */
for (cd = pt -> classes; cd != NULL; cd = cd -> next)
for (od = cd -> overs; od != NULL; od = od -> next)
if (isAutoGen(od))
addAutoOverload(pt,cd,od);
/* Allocate mapped types numbers. */
for (mtd = pt -> mappedtypes; mtd != NULL; mtd = mtd -> next)
mtd -> mappednr = mtd -> iff -> module -> nrmappedtypes++;
/* Generate the different class views. */
for (cd = pt -> classes; cd != NULL; cd = cd -> next)
{
ifaceFileDef *iff = cd -> iff;
if (iff -> type == class_iface)
{
/* Get the list of visible member functions. */
getVisibleMembers(pt,cd);
/* Get the virtual members. */
if (isComplex(cd))
getVirtuals(pt,cd);
}
else if (iff -> type == namespace_iface && iff -> module == pt -> module)
{
memberDef *md;
for (md = cd -> members; md != NULL; md = md -> next)
ifaceFilesAreUsedByMethod(cd,md);
}
}
/*
* In case there are any global functions that need external interface
* files.
*/
for (od = pt -> overs; od != NULL; od = od -> next)
ifaceFilesAreUsedByOther(pt,od);
/*
* Remove redundant virtual handlers. It's important that earlier,
* ie. those at the deepest level of %Import, are done first.
*/
for (mld = pt -> module -> imports; mld != NULL; mld = mld -> next)
filterVirtualHandlers(pt,mld -> module);
filterVirtualHandlers(pt,pt -> module);
}
/*
* Go through the virtual handlers filtering those can duplicate earlier ones.
* Make sure each virtual is numbered within its module, and according to their
* position in the list (ignoring duplicates).
*/
static void filterVirtualHandlers(sipSpec *pt,moduleDef *mod)
{
virtHandlerDef *vhd;
for (vhd = mod -> virthandlers; vhd != NULL; vhd = vhd -> next)
{
virtHandlerDef *best, *best_thismod, *hd;
best = best_thismod = NULL;
/*
* If this has handwritten code then we will want to use it.
* Otherwise, look for a handler in earlier modules.
*/
if (vhd -> virtcode == NULL)
{
moduleListDef *mld;
for (mld = pt -> module -> imports; mld != NULL && mld -> module != mod; mld = mld -> next)
{
for (hd = mld -> module -> virthandlers; hd != NULL; hd = hd -> next)
if (sameVirtualHandler(vhd,hd))
{
best = hd;
break;
}
/*
* No need to check later modules as this will
* either be the right one, or a duplicate of
* the right one.
*/
if (best != NULL)
break;
}
}
/*
* Find the best candidate in this module in case we want to
* give it our handwritten code.
*/
for (hd = mod -> virthandlers; hd != vhd; hd = hd -> next)
if (sameVirtualHandler(vhd,hd))
{
best_thismod = hd;
break;
}
/*
* We don't use this one if it doesn't have virtual code and
* there is an alternative, or if it does have virtual code and
* there is already an alternative in the same module which
* doesn't have virtual code.
*/
if ((vhd -> virtcode == NULL && (best != NULL || best_thismod != NULL)) ||
(vhd -> virtcode != NULL && best_thismod != NULL && best_thismod -> virtcode == NULL))
{
virtHandlerDef *saved;
/*
* If the alternative is in the same module and we
* have virtual code then give it to the alternative.
* Note that there is a bug here. If there are three
* handlers, the first without code and the second and
* third with code then which code is transfered to the
* first is down to luck. We should really only
* transfer code to methods that are known to be
* re-implementations - just having the same signature
* isn't enough.
*/
if (best_thismod != NULL)
{
if (best_thismod -> virtcode == NULL && vhd -> virtcode != NULL)
{
best_thismod -> virtcode = vhd -> virtcode;
resetIsDuplicateVH(best_thismod);
}
best = best_thismod;
}
/* Use the better one in place of this one. */
saved = vhd -> next;
*vhd = *best;
setIsDuplicateVH(vhd);
vhd -> next = saved;
}
else
vhd -> virthandlernr = mod -> nrvirthandlers++;
}
}
/*
* Add an overload that is automatically generated (typically by Qt's moc).
*/
static void addAutoOverload(sipSpec *pt,classDef *autocd,overDef *autood)
{
classDef *cd;
/* Find every class that has this one in its hierarchy. */
for (cd = pt -> classes; cd != NULL; cd = cd -> next)
{
mroDef *mro;
if (cd == autocd)
continue;
for (mro = cd -> mro; mro != NULL; mro = mro -> next)
if (mro -> cd == autocd)
{
memberDef *md;
overDef *od;
/* Another overload may already exist. */
for (md = cd -> members; md != NULL; md = md -> next)
if (md -> pyname == autood -> common -> pyname)
break;
if (md == NULL)
{
md = sipMalloc(sizeof (memberDef));
md -> pyname = autood -> common -> pyname;
md -> memberflags = autood -> common -> memberflags;
md -> slot = autood -> common -> slot;
md -> module = cd -> iff -> module;
md -> next = cd -> members;
cd -> members = md;
}
od = sipMalloc(sizeof (overDef));
*od = *autood;
od -> common = md;
od -> next = cd -> overs;
cd -> overs = od;
resetIsAutoGen(od);
if (cd -> iff -> module == pt -> module)
setIsUsedName(md -> pyname);
break;
}
}
}
/*
* Set the complete hierarchy for a class.
*/
static void setHierarchy(sipSpec *pt,classDef *base,classDef *cd,
classList **head)
{
mroDef **tailp = &cd -> mro;
/* See if it has already been done. */
if (cd -> mro != NULL)
return;
if (cd -> ecd != NULL)
setHierarchy(pt,base,cd -> ecd,head);
if (cd -> iff -> type == class_iface)
{
classList *cl;
/* The first thing is itself. */
appendToMRO(cd -> mro,&tailp,cd);
if (cd -> convtosubcode != NULL)
cd -> subbase = cd;
/* Now do it's superclasses. */
for (cl = cd -> supers; cl != NULL; cl = cl -> next)
{
mroDef *mro;
/*
* Make sure the super-class's hierarchy has been done.
*/
setHierarchy(pt,base,cl -> cd,head);
/* Append the super-classes hierarchy. */
for (mro = cl -> cd -> mro; mro != NULL; mro = mro -> next)
{
appendToMRO(cd -> mro,&tailp,mro -> cd);
/* Ripple through the complex flag. */
if (isComplex(mro -> cd))
setIsComplex(cd);
/*
* Ensure that the sub-class base class is the
* furthest up the hierarchy.
*/
if (mro -> cd -> subbase != NULL)
cd -> subbase = mro -> cd -> subbase;
}
}
}
/* We can't derive from classes with private dtors. */
if (isPrivateDtor(cd))
resetIsComplex(cd);
/* Add it to the new list. */
appendToClassList(head,cd);
}
/*
* Append a class definition to an mro list
*/
static void appendToMRO(mroDef *head,mroDef ***tailp,classDef *cd)
{
mroDef *mro, *new;
new = sipMalloc(sizeof (mroDef));
new -> cd = cd;
new -> mroflags = 0;
new -> next = NULL;
/* See if it is a duplicate. */
for (mro = head; mro != NULL; mro = mro -> next)
if (mro -> cd == cd)
{
setIsDuplicateSuper(new);
if (!isDuplicateSuper(mro))
setHasDuplicateSuper(mro);
break;
}
/* Append to the list and update the tail pointer. */
**tailp = new;
*tailp = &new -> next;
}
/*
* Get the base types for all typedefs in the current module.
*/
static void transformTypedefs(sipSpec *pt)
{
typedefDef *td;
for (td = pt -> typedefs; td != NULL; td = td -> next)
if (td -> module == pt -> module)
getBaseType(pt, td -> ecd, &td -> type);
}
/*
* Transform the data types for mapped types based on a template.
*/
static void transformMappedTypes(sipSpec *pt)
{
mappedTypeDef *mt;
for (mt = pt -> mappedtypes; mt != NULL; mt = mt -> next)
{
/* Nothing to do if this isn't template based. */
if (mt -> type.atype == template_type)
resolveMappedTypeTypes(pt,mt);
}
}
/*
* Transform the data types for a list of ctors.
*/
static void transformCtors(sipSpec *pt,classDef *cd)
{
ctorDef *ct;
for (ct = cd -> ctors; ct != NULL; ct = ct -> next)
resolveCtorTypes(pt,cd,ct);
}
/*
* Add a default copy ctor is required.
*/
static void addDefaultCopyCtor(classDef *cd)
{
ctorDef *copyct;
mroDef *mro;
/* See if there is a private copy ctor in the hierarchy. */
copyct = NULL;
for (mro = cd -> mro; mro != NULL; mro = mro -> next)
{
ctorDef *ct;
if (isDuplicateSuper(mro))
continue;
for (ct = mro -> cd -> ctors; ct != NULL; ct = ct -> next)
{
argDef *ad = &ct -> pysig.args[0];
/* See if is a copy ctor. */
if (ct -> pysig.nrArgs != 1 || ad -> nrderefs != 0 ||
!isReference(ad) || ad -> atype != class_type ||
ad -> u.cd != mro -> cd)
continue;
/* Stop now if the copy ctor is private. */
if (isPrivateCtor(ct))
return;
/*
* Remember if it's in the class we are dealing with.
*/
if (mro == cd -> mro)
copyct = ct;
break;
}
}
if (copyct == NULL)
{
ctorDef **tailp;
/* Create a default public copy ctor. */
copyct = sipMalloc(sizeof (ctorDef));
copyct -> ctorflags = SECT_IS_PUBLIC;
copyct -> pysig.nrArgs = 1;
copyct -> pysig.args[0].atype = class_type;
copyct -> pysig.args[0].u.cd = cd;
copyct -> pysig.args[0].argflags = (ARG_IS_REF | ARG_IS_CONST | ARG_IN);
copyct -> pysig.args[0].nrderefs = 0;
copyct -> pysig.args[0].defval = NULL;
copyct -> cppsig = ©ct -> pysig;
copyct -> exceptions = NULL;
copyct -> methodcode = NULL;
copyct -> prehook = NULL;
copyct -> posthook = NULL;
copyct -> next = NULL;
/* Append it to the list. */
for (tailp = &cd -> ctors; *tailp != NULL; tailp = &(*tailp) -> next)
;
*tailp = copyct;
}
}
/*
* Transform the data types for a list of overloads.
*/
static void transformOverloads(sipSpec *pt,classDef *scope,overDef *overs)
{
overDef *od;
for (od = overs; od != NULL; od = od -> next)
resolveFuncTypes(pt,scope,od);
}
/*
* Transform the data types for the variables.
*/
static void transformVariableList(sipSpec *pt)
{
varDef *vd;
for (vd = pt -> vars; vd != NULL; vd = vd -> next)
resolveVariableType(pt,vd);
}
/*
* Set the list of visible member functions for a class.
*/
static void getVisibleMembers(sipSpec *pt,classDef *cd)
{
mroDef *mro;
cd -> visible = NULL;
for (mro = cd -> mro; mro != NULL; mro = mro -> next)
{
memberDef *md;
classDef *mrocd;
if (isDuplicateSuper(mro))
continue;
mrocd = mro -> cd;
/*
* If the base class is in the main module, see if it needs to
* publish any protected enums.
*/
if (cd -> iff -> module == pt -> module)
{
enumDef *ed;
for (ed = pt -> enums; ed != NULL; ed = ed -> next)
{
/* Skip unless we are the publisher. */
if (ed -> pcd != mrocd)
continue;
/*
* If we are not in the main module then the
* base class must take over as the publisher.
*/
if (mrocd -> iff -> module != pt -> module)
ed -> pcd = cd;
}
}
for (md = mrocd -> members; md != NULL; md = md -> next)
{
visibleList *vl;
if (md -> slot != no_slot)
{
if (mrocd == cd)
ifaceFilesAreUsedByMethod(cd,md);
continue;
}
/*
* See if it is already in the list. This has the
* desired side effect of eliminating any functions
* that have an implementation closer to this class in
* the hierarchy. This is the only reason to define
* private functions.
*/
for (vl = cd -> visible; vl != NULL; vl = vl -> next)
if (vl -> m -> pyname == md -> pyname)
break;
/* See if it is a new member function. */
if (vl == NULL)
{
overDef *od;
/* We probably want the name. */
setIsUsedName(md -> pyname);
vl = sipMalloc(sizeof (visibleList));
vl -> m = md;
vl -> cd = mrocd;
vl -> next = cd -> visible;
cd -> visible = vl;
for (od = mrocd -> overs; od != NULL; od = od -> next)
if (od -> common == md)
ifaceFilesAreUsed(cd -> iff,od);
}
}
}
}
/*
* Get all the virtuals for a particular class.
*/
static void getVirtuals(sipSpec *pt,classDef *cd)
{
mroDef *mro;
for (mro = cd -> mro; mro != NULL; mro = mro -> next)
{
if (isDuplicateSuper(mro))
continue;
getClassVirtuals(cd,mro -> cd);
}
/*
* If this class is defined in the main module make sure we get the API
* files for all the visible virtuals.
*/
if (cd -> iff -> module == pt -> module)
{
virtOverDef *vod;
for (vod = cd -> vmembers; vod != NULL; vod = vod -> next)
{
/* Make sure we get the name. */
setIsUsedName(vod -> o.common -> pyname);
ifaceFilesAreUsed(cd -> iff,&vod -> o);
}
}
}
/*
* Get the list of visible virtual functions for a class.
*/
static void getClassVirtuals(classDef *base,classDef *cd)
{
overDef *od;
for (od = cd -> overs; od != NULL; od = od -> next)
{
virtOverDef **tailp, *vod;
if (!isVirtual(od) || isPrivate(od))
continue;
/*
* See if a virtual of this name and signature is already in
* the list.
*/
for (tailp = &base -> vmembers; (vod = *tailp) != NULL; tailp = &vod -> next)
if (strcmp(vod -> o.cppname,od -> cppname) == 0 && sameOverload(&vod -> o,od))
break;
if (vod == NULL)
{
/*
* See if there is a non-virtual reimplementation
* nearer in the class hierarchy.
*/
mroDef *mro;
classDef *scope = NULL;
overDef *eod;
for (mro = base -> mro; mro -> cd != cd; mro = mro -> next)
{
if (isDuplicateSuper(mro))
continue;
/*
* Ignore classes that are on a different
* branch of the class hierarchy.
*/
if (!isSubClass(mro -> cd,cd))
continue;
for (eod = mro -> cd -> overs; eod != NULL; eod = eod -> next)
if (strcmp(eod -> cppname,od -> cppname) == 0 && sameSignature(eod -> cppsig,od -> cppsig,TRUE) && isConst(eod) == isConst(od) && !isAbstract(eod))
{
scope = mro -> cd;
break;
}
if (scope != NULL)
break;
}
vod = sipMalloc(sizeof (virtOverDef));
vod -> o = *od;
vod -> scope = (scope != NULL ? scope : cd);
vod -> next = NULL;
*tailp = vod;
/*
* If there was a nearer reimplementation then we use
* its protection and abstract flags.
*/
if (scope != NULL)
{
vod -> o.overflags &= ~(SECT_MASK | OVER_IS_ABSTRACT);
vod -> o.overflags |= (SECT_MASK | OVER_IS_ABSTRACT) & eod -> overflags;
}
}
}
}
/*
* Return TRUE is a class is derived from another.
*/
static int isSubClass(classDef *cc,classDef *pc)
{
mroDef *mro;
/*
* In other words, does the parent class appear in the child class's
* MRO list.
*/
for (mro = cc -> mro; mro != NULL; mro = mro -> next)
if (mro -> cd == pc)
return TRUE;
return FALSE;
}
/*
* Resolve the types of a mapped type based on a template.
*/
static void resolveMappedTypeTypes(sipSpec *pt,mappedTypeDef *mt)
{
int a;
templateDef *td = mt -> type.u.td;
for (a = 0; a < td -> types.nrArgs; ++a)
{
getBaseType(pt,NULL,&td -> types.args[a]);
ifaceFileIsUsed(mt -> iff,&td -> types.args[a]);
}
}
/*
* Resolve the types of a ctor.
*/
static void resolveCtorTypes(sipSpec *pt,classDef *scope,ctorDef *ct)
{
int a;
/* Handle any C++ signature. */
if (ct -> cppsig != &ct -> pysig)
for (a = 0; a < ct -> cppsig -> nrArgs; ++a)
getBaseType(pt,scope,&ct -> cppsig -> args[a]);
/* Handle the Python signature. */
for (a = 0; a < ct -> pysig.nrArgs; ++a)
{
argDef *ad = &ct -> pysig.args[a];
getBaseType(pt,scope,ad);
if (!supportedType(scope,NULL,ad,FALSE) && (ct -> cppsig == &ct -> pysig || ct -> methodcode == NULL))
{
fatalScopedName(scope -> iff -> fqcname);
fatal(" unsupported ctor argument type - provide %%MethodCode and a C++ signature\n");
}
ifaceFileIsUsed(scope -> iff,ad);
scopeDefaultValue(pt,scope,ad);
}
}
/*
* Resolve the types of a function.
*/
static void resolveFuncTypes(sipSpec *pt,classDef *scope,overDef *od)
{
argDef *res;
/* Handle any C++ signature. */
if (od -> cppsig != &od -> pysig)
{
int a;
getBaseType(pt,scope,&od -> cppsig -> result);
for (a = 0; a < od -> cppsig -> nrArgs; ++a)
getBaseType(pt,scope,&od -> cppsig -> args[a]);
}
/* Handle the Python signature. */
resolvePySigTypes(pt,scope,od,&od -> pysig,isSignal(od));
/* These slots must return int. */
res = &od -> pysig.result;
if (od -> common -> slot == len_slot ||
od -> common -> slot == cmp_slot ||
od -> common -> slot == contains_slot ||
od -> common -> slot == nonzero_slot)
if (res -> atype != int_type || res -> nrderefs != 0 ||
isReference(res) || isConstArg(res))
fatal("%s slots must return int\n",od -> common -> pyname -> text);
/* These slots must return void. */
if (od -> common -> slot == setitem_slot ||
od -> common -> slot == delitem_slot)
if (res -> atype != void_type || res -> nrderefs != 0 ||
isReference(res) || isConstArg(res))
fatal("%s slots must return void\n",od -> common -> pyname -> text);
}
/*
* Resolve the types of a Python signature.
*/
static void resolvePySigTypes(sipSpec *pt,classDef *scope,overDef *od,
signatureDef *pysig,int issignal)
{
int a;
argDef *res = &pysig -> result;
if (res -> atype != void_type || res -> nrderefs != 0)
{
if (issignal)
{
if (scope != NULL)
{
fatalScopedName(scope -> iff -> fqcname);
fatal("::");
}
fatal("%s() signals must return void\n",od -> cppname);
}
getBaseType(pt,scope,res);
/* Results must be simple. */
if (!supportedType(scope,od,res,FALSE) && (od -> cppsig == &od -> pysig || od -> methodcode == NULL))
{
if (scope != NULL)
{
fatalScopedName(scope -> iff -> fqcname);
fatal("::");
}
fatal("%s() unsupported function return type - provide %%MethodCode and a %s signature\n",od -> cppname,(pt -> genc ? "C" : "C++"));
}
}
for (a = 0; a < pysig -> nrArgs; ++a)
{
argDef *ad = &pysig -> args[a];
getBaseType(pt,scope,ad);
if (ad -> atype == slotcon_type)
resolvePySigTypes(pt,scope,od,ad -> u.sa,TRUE);
/*
* Note signal arguments are restricted in their types because
* we don't (yet) support handwritten code for them.
*/
if (issignal)
{
if (!supportedType(scope,od,ad,FALSE))
{
if (scope != NULL)
{
fatalScopedName(scope -> iff -> fqcname);
fatal("::");
}
fatal("%s() unsupported signal argument type\n");
}
}
else if (!supportedType(scope,od,ad,TRUE) && (od -> cppsig == &od -> pysig || od -> methodcode == NULL || (isVirtual(od) && od -> virthandler -> virtcode == NULL)))
{
if (scope != NULL)
{
fatalScopedName(scope -> iff -> fqcname);
fatal("::");
}
if (isVirtual(od))
fatal("%s() unsupported function argument type - provide %%Method code, a valid %%VirtualCatcherCode and a valid C++ signature\n",od -> cppname);
fatal("%s() unsupported function argument type - provide %%Method code and a valid %s signature\n",od -> cppname,(pt -> genc ? "C" : "C++"));
}
if (scope != NULL)
scopeDefaultValue(pt,scope,ad);
}
}
/*
* Resolve the type of a variable.
*/
static void resolveVariableType(sipSpec *pt,varDef *vd)
{
int bad = TRUE;
argDef *vtype = &vd -> type;
getBaseType(pt,vd -> ecd,vtype);
switch (vtype -> atype)
{
case mapped_type:
case class_type:
/* Class, Class & and Class * are supported. */
if (vtype -> nrderefs <= 1)
bad = FALSE;
break;
case ustring_type:
case string_type:
/* (unsigned) char, (unsigned) char * are supported. */
if (!isReference(vtype) && vtype -> nrderefs <= 1)
bad = FALSE;
break;
case float_type:
case double_type:
case enum_type:
case bool_type:
case ushort_type:
case short_type:
case uint_type:
case cint_type:
case int_type:
case ulong_type:
case long_type:
case pyobject_type:
/* These are supported without pointers or references. */
if (!isReference(vtype) && vtype -> nrderefs == 0)
bad = FALSE;
break;
case struct_type:
case void_type:
/* A simple pointer is supported. */
if (!isReference(vtype) && vtype -> nrderefs == 1)
bad = FALSE;
break;
}
if (bad)
{
fatalScopedName(vd -> fqcname);
fatal(" has an unsupported type\n");
}
if (vtype -> atype != class_type && vd -> accessfunc != NULL)
{
fatalScopedName(vd -> fqcname);
fatal(" has %%AccessCode but isn't a class instance\n");
}
if (vd -> ecd != NULL)
ifaceFileIsUsed(vd -> ecd -> iff,vtype);
/*
* Instance variables or static class variables (unless they are
* constants) need a handler.
*/
if (vd -> ecd != NULL && vd -> accessfunc == NULL &&
(!isStaticVar(vd) || vtype -> nrderefs != 0 || !isConstArg(vtype)))
{
setNeedsHandler(vd);
setHasVarHandlers(vd -> ecd);
}
}
/*
* See if a type is supported by the generated code.
*/
static int supportedType(classDef *cd,overDef *od,argDef *ad,int outputs)
{
switch (ad -> atype)
{
case signal_type:
case slot_type:
case rxcon_type:
case rxdis_type:
case slotcon_type:
case slotdis_type:
case qobject_type:
/* These can only appear in argument lists without * or &. */
ensureInput(cd,od,ad);
return TRUE;
case ustring_type:
case string_type:
if (isReference(ad))
{
if (outputs && ad -> nrderefs <= 1)
{
defaultOutput(cd,od,ad);
return TRUE;
}
}
else if (ad -> nrderefs == 0)
{
ensureInput(cd,od,ad);
return TRUE;
}
else if (ad -> nrderefs == 1)
{
if (outputs)
defaultInput(ad);
else
ensureInput(cd,od,ad);
return TRUE;
}
else if (ad -> nrderefs == 2 && outputs)
{
defaultOutput(cd,od,ad);
return TRUE;
}
break;
case float_type:
case double_type:
case enum_type:
case bool_type:
case ushort_type:
case short_type:
case uint_type:
case cint_type:
case int_type:
case ulong_type:
case long_type:
case pyobject_type:
case pytuple_type:
case pylist_type:
case pydict_type:
case pycallable_type:
case pyslice_type:
if (isReference(ad))
{
if (ad -> nrderefs == 0 && outputs)
{
defaultOutput(cd,od,ad);
return TRUE;
}
}
else if (ad -> nrderefs == 0)
{
ensureInput(cd,od,ad);
return TRUE;
}
else if (ad -> nrderefs == 1 && outputs)
{
defaultOutput(cd,od,ad);
return TRUE;
}
break;
case mapped_type:
case class_type:
if (isReference(ad))
{
if (ad -> nrderefs == 0)
{
defaultInput(ad);
return TRUE;
}
else if (ad -> nrderefs == 1 && outputs)
{
defaultOutput(cd,od,ad);
return TRUE;
}
}
else if (ad -> nrderefs == 0)
{
ensureInput(cd,od,ad);
return TRUE;
}
else if (ad -> nrderefs == 1)
{
if (outputs)
defaultInput(ad);
else
ensureInput(cd,od,ad);
return TRUE;
}
else if (ad -> nrderefs == 2 && outputs)
{
defaultOutput(cd,od,ad);
return TRUE;
}
break;
case struct_type:
case void_type:
if (isReference(ad))
{
if (ad -> nrderefs == 1 && outputs)
{
defaultOutput(cd,od,ad);
return TRUE;
}
}
else if (ad -> nrderefs == 1)
{
ensureInput(cd,od,ad);
return TRUE;
}
else if (ad -> nrderefs == 2 && outputs)
{
defaultOutput(cd,od,ad);
return TRUE;
}
break;
}
/* Unsupported if we got this far. */
return FALSE;
}
/*
* Ensure the direction of an argument is an input.
*/
static void ensureInput(classDef *cd,overDef *od,argDef *ad)
{
if (isOutArg(ad))
{
if (cd != NULL)
{
fatalScopedName(cd -> iff -> fqcname);
fatal("::");
}
if (od != NULL)
fatal("%s",od -> cppname);
fatal("() invalid argument type for /Out/\n");
}
setIsInArg(ad);
}
/*
* Default the direction of an argument to an input.
*/
static void defaultInput(argDef *ad)
{
if (!isInArg(ad) && !isOutArg(ad))
setIsInArg(ad);
}
/*
* Default the direction of an argument to an output unless the argument is
* const.
*/
static void defaultOutput(classDef *cd,overDef *od,argDef *ad)
{
if (isOutArg(ad))
{
if (isConstArg(ad))
{
if (cd != NULL)
{
fatalScopedName(cd -> iff -> fqcname);
fatal("::");
}
if (od != NULL)
fatal("%s",od -> cppname);
fatal("() const argument cannot have /Out/ specified\n");
}
}
else if (!isInArg(ad))
if (isConstArg(ad))
setIsInArg(ad);
else
setIsOutArg(ad);
}
/*
* Put a scoped name to stderr.
*/
void fatalScopedName(scopedNameDef *snd)
{
while (snd != NULL)
{
fatal("%s",snd -> name);
snd = snd -> next;
if (snd != NULL)
fatal("::");
}
}
/*
* Compare two overloads and return TRUE if they are the same.
*/
static int sameOverload(overDef *od1,overDef *od2)
{
/* They must both be const, or both not. */
if (isConst(od1) != isConst(od2))
return FALSE;
return sameSignature(&od1 -> pysig,&od2 -> pysig,TRUE);
}
/*
* Compare two virtual handlers and return TRUE if they are the same.
*/
static int sameVirtualHandler(virtHandlerDef *vhd1,virtHandlerDef *vhd2)
{
if (isTransferVH(vhd1) != isTransferVH(vhd2))
return FALSE;
if (!sameArgType(&vhd1 -> sd -> result,&vhd2 -> sd -> result,TRUE))
return FALSE;
return sameSignature(vhd1 -> sd,vhd2 -> sd,TRUE);
}
/*
* Compare two signatures and return TRUE if they are the same.
*/
int sameSignature(signatureDef *sd1,signatureDef *sd2,int strict)
{
int a;
if (strict)
{
/* The number of arguments must be the same. */
if (sd1 -> nrArgs != sd2 -> nrArgs)
return FALSE;
}
else
{
int na1, na2;
/* We only count the compulsory arguments. */
na1 = 0;
for (a = 0; a < sd1 -> nrArgs; ++a)
{
if (sd1 -> args[a].defval != NULL)
break;
++na1;
}
na2 = 0;
for (a = 0; a < sd2 -> nrArgs; ++a)
{
if (sd2 -> args[a].defval != NULL)
break;
++na2;
}
if (na1 != na2)
return FALSE;
}
/* The arguments must be the same. */
for (a = 0; a < sd1 -> nrArgs; ++a)
{
if (!strict && sd1 -> args[a].defval != NULL)
break;
if (!sameArgType(&sd1 -> args[a],&sd2 -> args[a],strict))
return FALSE;
}
/* Must be the same if we've got this far. */
return TRUE;
}
/*
* Compare two argument types and return TRUE if they are the same.
*/
int sameArgType(argDef *a1,argDef *a2,int strict)
{
/* The indirection and the references must be the same. */
if (isReference(a1) != isReference(a2) || a1 -> nrderefs != a2 -> nrderefs)
return FALSE;
/* The const should be the same. */
if (isConstArg(a1) != isConstArg(a2))
return FALSE;
/*
* In non-strict, just check if they are subject to Python conversions.
*/
if (!strict)
if ((pyToInt(a1 -> atype) && pyToInt(a2 -> atype)) ||
(pyToDouble(a1 -> atype) && pyToDouble(a2 -> atype)))
return TRUE;
return sameBaseType(a1,a2,strict);
}
/*
* Compare two basic types and return TRUE if they are the same.
*/
int sameBaseType(argDef *a1,argDef *a2,int strict)
{
/* The types must be the same. */
if (a1 -> atype != a2 ->atype)
return FALSE;
switch (a1 -> atype)
{
case class_type:
if (a1 -> u.cd != a2 -> u.cd)
return FALSE;
break;
case enum_type:
if (strict && a1 -> u.ed != a2 -> u.ed)
return FALSE;
break;
case slotcon_type:
case slotdis_type:
if (!sameSignature(a1 -> u.sa,a2 -> u.sa,strict))
return FALSE;
break;
case template_type:
{
int a;
templateDef *td1, *td2;
td1 = a1 -> u.td;
td2 = a2 -> u.td;
if (!sameScopedName(td1 -> fqname,td2 -> fqname) != 0 ||
td1 -> types.nrArgs != td2 -> types.nrArgs)
return FALSE;
for (a = 0; a < td1 -> types.nrArgs; ++a)
if (!sameBaseType(&td1 -> types.args[a],&td2 -> types.args[a],strict))
return FALSE;
break;
}
case struct_type:
if (!sameScopedName(a1 -> u.sname,a2 -> u.sname) != 0)
return FALSE;
break;
case defined_type:
if (!sameScopedName(a1 -> u.snd,a2 -> u.snd))
return FALSE;
break;
case mapped_type:
if (a1 -> u.mtd != a2 -> u.mtd)
return FALSE;
break;
}
/* Must be the same if we've got this far. */
return TRUE;
}
/*
* Return TRUE if two scoped names are the same.
*/
int sameScopedName(scopedNameDef *snd1,scopedNameDef *snd2)
{
while (snd1 != NULL && snd2 != NULL && strcmp(snd1 -> name,snd2 -> name) == 0)
{
snd1 = snd1 -> next;
snd2 = snd2 -> next;
}
return (snd1 == NULL && snd2 == NULL);
}
/*
* Add an explicit scope to the default value of an argument if possible.
*/
static void scopeDefaultValue(sipSpec *pt,classDef *cd,argDef *ad)
{
valueDef *vd, **tailp, *newvd;
/*
* We do a quick check to see if we need to do anything. This means
* we can limit the times we need to copy the default value. It needs
* to be copied because it will be shared by class versions that have
* been created on the fly and it may need to be scoped differently for
* each of those versions.
*/
for (vd = ad -> defval; vd != NULL; vd = vd -> next)
if (vd -> vtype == scoped_value && vd -> u.vscp -> next == NULL)
break;
if (vd == NULL)
return;
/*
* It's not certain that we will do anything, but we assume we will and
* start copying.
*/
newvd = NULL;
tailp = &newvd;
for (vd = ad -> defval; vd != NULL; vd = vd -> next)
{
mroDef *mro;
scopedNameDef *origname;
valueDef *new;
/* Make the copy. */
new = sipMalloc(sizeof (valueDef));
*new = *vd;
*tailp = new;
tailp = &new -> next;
/*
* Skip this part of the expression if it isn't a named value
* or it already has a scope.
*/
if (vd -> vtype != scoped_value || vd -> u.vscp -> next != NULL)
continue;
/*
* Search the class hierarchy for an enum value with the same
* name. If we don't find one, leave it as it is (the compiler
* will find out if this is a problem).
*/
origname = vd -> u.vscp;
for (mro = cd -> mro; mro != NULL; mro = mro -> next)
{
enumDef *ed;
if (isDuplicateSuper(mro))
continue;
for (ed = pt -> enums; ed != NULL; ed = ed -> next)
{
enumMemberDef *emd;
if (ed -> ecd != mro -> cd)
continue;
for (emd = ed -> members; emd != NULL; emd = emd -> next)
if (strcmp(emd -> cname,origname -> name) == 0)
{
scopedNameDef *snd;
/*
* Take the scope from the
* class that the enum was
* defined in.
*/
snd = copyScopedName(mro -> cd -> iff -> fqcname);
appendScopedName(&snd,origname);
new -> u.vscp = snd;
/* Nothing more to do. */
break;
}
if (emd != NULL)
break;
}
if (ed != NULL)
break;
}
}
ad -> defval = newvd;
}
/*
* Make sure a type is a base type.
*/
static void getBaseType(sipSpec *pt,classDef *defscope,argDef *type)
{
/* Loop until we've got to a base type. */
while (type -> atype == defined_type)
{
scopedNameDef *snd = type -> u.snd;
type -> atype = no_type;
if (defscope != NULL)
searchScope(pt,defscope,snd,type);
if (type -> atype == no_type)
searchMappedTypes(pt,snd,type);
if (type -> atype == no_type)
searchTypedefs(pt,snd,type);
if (type -> atype == no_type)
searchEnums(pt,snd,type);
if (type -> atype == no_type)
searchClasses(pt,snd,type);
if (type -> atype == no_type)
fatalNoDefinedType(snd);
}
/* Get the base of type of any slot arguments. */
if (type -> atype == slotcon_type || type -> atype == slotdis_type)
{
int sa;
for (sa = 0; sa < type -> u.sa -> nrArgs; ++sa)
getBaseType(pt,defscope,&type -> u.sa -> args[sa]);
}
/* Replace the base type if it has been mapped. */
if (type -> atype == struct_type || type -> atype == template_type)
searchMappedTypes(pt,NULL,type);
}
/*
* Search for a name in a scope and return the corresponding type.
*/
static void searchScope(sipSpec *pt,classDef *scope,scopedNameDef *snd,
argDef *ad)
{
scopedNameDef *tmpsnd = NULL;
mroDef *mro;
for (mro = scope -> mro; mro != NULL; mro = mro -> next)
{
if (isDuplicateSuper(mro))
continue;
/* Append the name to the scope and see if it exists. */
tmpsnd = copyScopedName(classFQCName(mro -> cd));
appendScopedName(&tmpsnd,copyScopedName(snd));
searchMappedTypes(pt,tmpsnd,ad);
if (ad -> atype != no_type)
break;
searchTypedefs(pt,tmpsnd,ad);
if (ad -> atype != no_type)
break;
searchEnums(pt,tmpsnd,ad);
if (ad -> atype != no_type)
break;
searchClasses(pt,tmpsnd,ad);
if (ad -> atype != no_type)
break;
freeScopedName(tmpsnd);
tmpsnd = NULL;
}
if (tmpsnd != NULL)
freeScopedName(tmpsnd);
}
/*
* Search the mapped types for a name and return the type.
*/
static void searchMappedTypes(sipSpec *pt,scopedNameDef *snd,argDef *ad)
{
mappedTypeDef *mtd;
scopedNameDef *oname;
/* Patch back to defined types so we can use sameBaseType(). */
if (snd != NULL)
{
oname = ad -> u.snd;
ad -> u.snd = snd;
ad -> atype = defined_type;
}
for (mtd = pt -> mappedtypes; mtd != NULL; mtd = mtd -> next)
if (sameBaseType(ad,&mtd -> type,TRUE))
{
/* Copy the type. */
ad -> atype = mapped_type;
ad -> u.mtd = mtd;
return;
}
/* Restore because we didn't find anything. */
if (snd != NULL)
{
ad -> u.snd = oname;
ad -> atype = no_type;
}
}
/*
* Search the typedefs for a name and return the type.
*/
static void searchTypedefs(sipSpec *pt,scopedNameDef *snd,argDef *ad)
{
typedefDef *td;
for (td = pt -> typedefs; td != NULL; td = td -> next)
if (sameScopedName(td -> fqname,snd))
{
/* Copy the type. */
ad -> atype = td -> type.atype;
ad -> argflags |= td -> type.argflags;
ad -> nrderefs += td -> type.nrderefs;
ad -> u = td -> type.u;
break;
}
}
/*
* Search the enums for a name and return the type.
*/
static void searchEnums(sipSpec *pt,scopedNameDef *snd,argDef *ad)
{
enumDef *ed;
for (ed = pt -> enums; ed != NULL; ed = ed -> next)
{
if (ed -> fqcname == NULL)
continue;
if (sameScopedName(ed -> fqcname,snd))
{
ad -> atype = enum_type;
ad -> u.ed = ed;
break;
}
}
}
/*
* Search the classes for a name and return the type.
*/
static void searchClasses(sipSpec *pt,scopedNameDef *snd,argDef *ad)
{
classDef *cd;
for (cd = pt -> classes; cd != NULL; cd = cd -> next)
if (sameScopedName(classFQCName(cd),snd))
{
ad -> atype = class_type;
ad -> u.cd = cd;
break;
}
}
/*
* Print an error message describing an undefined type to stderr and terminate.
*/
static void fatalNoDefinedType(scopedNameDef *snd)
{
fatalScopedName(snd);
fatal(" is undefined\n");
}
/*
* Make sure all external interface files for all other functions of a module
* are used.
*/
static void ifaceFilesAreUsedByOther(sipSpec *pt,overDef *od)
{
int a;
ifaceFileDef *iff;
if ((iff = getIfaceFile(&od -> pysig.result)) != NULL && iff -> module != pt -> module)
addToUsedList(&pt -> used,iff);
for (a = 0; a < od -> pysig.nrArgs; ++a)
if ((iff = getIfaceFile(&od -> pysig.args[a])) != NULL && iff -> module != pt -> module)
addToUsedList(&pt -> used,iff);
}
/*
* Make sure all interface files for all overloads of a method are used.
*/
static void ifaceFilesAreUsedByMethod(classDef *cd,memberDef *md)
{
overDef *od;
for (od = cd -> overs; od != NULL; od = od -> next)
if (od -> common == md)
ifaceFilesAreUsed(cd -> iff,od);
}
/*
* Make sure all interface files for a signature are used.
*/
static void ifaceFilesAreUsed(ifaceFileDef *iff,overDef *od)
{
int a;
ifaceFileIsUsed(iff,&od -> pysig.result);
for (a = 0; a < od -> pysig.nrArgs; ++a)
ifaceFileIsUsed(iff,&od -> pysig.args[a]);
}
/*
* If a type has an interface file then add it to the appropriate list of used
* interface files so that the header file is #included in the generated code.
*/
static void ifaceFileIsUsed(ifaceFileDef *iff,argDef *ad)
{
ifaceFileDef *usediff;
if ((usediff = getIfaceFile(ad)) != NULL && usediff != iff)
addToUsedList(&iff -> used,usediff);
}
/*
* Return the interface file for a type, or NULL if it doesn't have one.
*/
static ifaceFileDef *getIfaceFile(argDef *ad)
{
ifaceFileDef *iff;
switch (ad -> atype)
{
case class_type:
iff = ad -> u.cd -> iff;
break;
case mapped_type:
iff = ad -> u.mtd -> iff;
break;
case enum_type:
if (ad -> u.ed -> fqcname != NULL && ad -> u.ed -> ecd != NULL)
{
iff = ad -> u.ed -> ecd -> iff;
break;
}
/* Drop through. */
default:
iff = NULL;
}
return iff;
}
/*
* Position a class so that it is after all its super-classes.
*/
static void positionClass(classDef *cd)
{
classList *cl;
/* See if it has already been done. */
if (cd -> node -> ordered)
return;
for (cl = cd -> supers; cl != NULL; cl = cl -> next)
{
nodeDef **ndp, *nd1, *nd2, *rp;
/* Ignore super-classes from different modules. */
if (cl -> cd -> iff -> module != cd -> iff -> module)
continue;
/* Make sure the super-class is positioned. */
positionClass(cl -> cd);
/*
* Find ancestors of the two that are siblings (ie. they have a
* common parent).
*/
rp = &cd -> iff -> module -> root;
for (nd1 = cd -> node; nd1 != rp; nd1 = nd1 -> parent)
{
for (nd2 = cl -> cd -> node; nd2 != rp; nd2 = nd2 -> parent)
if (nd1 -> parent == nd2 -> parent)
break;
if (nd2 != rp)
break;
}
/*
* The first node must appear after the second in the common
* parent's list of children.
*/
for (ndp = &nd1 -> parent -> child; *ndp != NULL; ndp = &(*ndp) -> next)
{
nodeDef *nd = *ndp;
if (nd == nd2)
break;
if (nd == nd1)
{
/* Remove this one from the list. */
*ndp = nd -> next;
/* Find the super-class ancestor. */
while (*ndp != nd2)
ndp = &(*ndp) -> next;
/*
* Put this one back after the super-class
* ancestor.
*/
nd -> next = (*ndp) -> next;
(*ndp) -> next = nd;
break;
}
}
}
cd -> node -> ordered = TRUE;
}
/*
* Make sure a class is in the namespace tree.
*/
static void addNodeToParent(nodeDef *root,classDef *cd)
{
nodeDef *nd, *parent;
/* Skip classes already in the tree. */
if (cd -> node != NULL)
return;
/* Add this child to the parent. */
nd = sipMalloc(sizeof (nodeDef));
nd -> ordered = FALSE;
nd -> cd = cd;
nd -> child = NULL;
/* Get the address of the parent node. */
if (cd -> ecd == NULL)
parent = root;
else
{
/* Make sure the parent is in the tree. */
addNodeToParent(root,cd -> ecd);
parent = cd -> ecd -> node;
}
nd -> parent = parent;
/* Insert this at the head of the parent's children. */
nd -> next = parent -> child;
parent -> child = nd;
/* Remember where we are in the tree. */
cd -> node = nd;
}
/*
* Assign the module specific class number for a class and all it's children.
*/
static void assignClassNrs(sipSpec *pt,moduleDef *mod,nodeDef *nd)
{
classDef *cd;
nodeDef *cnd;
/* Assign the class if it's not the root. */
if ((cd = nd -> cd) != NULL)
{
cd -> classnr = mod -> nrclasses++;
/*
* If we find a class defined in the main module called
* QObject, assume it's Qt.
*/
if (mod == pt -> module && strcmp(classBaseName(cd),"QObject") == 0)
pt -> qobjclass = cd -> classnr;
}
/* Assign all it's children. */
for (cnd = nd -> child; cnd != NULL; cnd = cnd -> next)
assignClassNrs(pt,mod,cnd);
}
/*
* Assign the module specific enum number for all named enums.
*/
static void assignEnumNrs(sipSpec *pt)
{
enumDef *ed;
for (ed = pt -> enums; ed != NULL; ed = ed -> next)
if (ed -> fqcname != NULL)
ed -> enumnr = ed -> module -> nrenums++;
}
|