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
|
/* -----------------------------------------------------------------------------
* See the LICENSE file for information on copyright, usage and redistribution
* of SWIG, and the README file for authors - http://www.swig.org/release.html.
*
* csharp.cxx
*
* C# language module for SWIG.
* ----------------------------------------------------------------------------- */
char cvsroot_csharp_cxx[] = "$Header: /cvsroot/swig/SWIG/Source/Modules/csharp.cxx,v 1.81 2006/03/06 22:50:56 wsfulton Exp $";
#include "swigmod.h"
#include <limits.h> // for INT_MAX
#include "cparse.h"
#include <ctype.h>
class CSHARP : public Language {
static const char *usage;
const String *empty_string;
const String *public_string;
const String *protected_string;
Hash *swig_types_hash;
File *f_runtime;
File *f_header;
File *f_wrappers;
File *f_init;
List *filenames_list;
bool proxy_flag; // Flag for generating proxy classes
bool native_function_flag; // Flag for when wrapping a native function
bool enum_constant_flag; // Flag for when wrapping an enum or constant
bool static_flag; // Flag for when wrapping a static functions or member variables
bool variable_wrapper_flag; // Flag for when wrapping a nonstatic member variable
bool wrapping_member_flag; // Flag for when wrapping a member variable/enum/const
bool global_variable_flag; // Flag for when wrapping a global variable
bool old_variable_names; // Flag for old style variable names in the intermediary class
bool generate_property_declaration_flag; // Flag for generating properties
String *imclass_name; // intermediary class name
String *module_class_name; // module class name
String *imclass_class_code; // intermediary class code
String *proxy_class_def;
String *proxy_class_code;
String *module_class_code;
String *proxy_class_name;
String *variable_name; //Name of a variable being wrapped
String *proxy_class_constants_code;
String *module_class_constants_code;
String *enum_code;
String *dllimport; // DllImport attribute name
String *namespce; // Optional namespace name
String *imclass_imports; //intermediary class imports from %pragma
String *module_imports; //module imports from %pragma
String *imclass_baseclass; //inheritance for intermediary class class from %pragma
String *module_baseclass; //inheritance for module class from %pragma
String *imclass_interfaces; //interfaces for intermediary class class from %pragma
String *module_interfaces; //interfaces for module class from %pragma
String *imclass_class_modifiers; //class modifiers for intermediary class overriden by %pragma
String *module_class_modifiers; //class modifiers for module class overriden by %pragma
String *upcasts_code; //C++ casts for inheritance hierarchies C++ code
String *imclass_cppcasts_code; //C++ casts up inheritance hierarchies intermediary class code
String *destructor_call; //C++ destructor call if any
enum EnumFeature { SimpleEnum, TypeunsafeEnum, TypesafeEnum, ProperEnum };
public:
/* -----------------------------------------------------------------------------
* CSHARP()
* ----------------------------------------------------------------------------- */
CSHARP() :
empty_string(NewString("")),
public_string(NewString("public")),
protected_string(NewString("protected")),
swig_types_hash(NULL),
f_runtime(NULL),
f_header(NULL),
f_wrappers(NULL),
f_init(NULL),
filenames_list(NULL),
proxy_flag(true),
native_function_flag(false),
enum_constant_flag(false),
static_flag(false),
variable_wrapper_flag(false),
wrapping_member_flag(false),
global_variable_flag(false),
old_variable_names (false),
generate_property_declaration_flag(false),
imclass_name(NULL),
module_class_name(NULL),
imclass_class_code(NULL),
proxy_class_def(NULL),
proxy_class_code(NULL),
module_class_code(NULL),
proxy_class_name(NULL),
variable_name(NULL),
proxy_class_constants_code(NULL),
module_class_constants_code(NULL),
enum_code(NULL),
dllimport(NULL),
namespce(NULL),
imclass_imports(NULL),
module_imports(NULL),
imclass_baseclass(NULL),
module_baseclass(NULL),
imclass_interfaces(NULL),
module_interfaces(NULL),
imclass_class_modifiers(NULL),
module_class_modifiers(NULL),
upcasts_code(NULL),
imclass_cppcasts_code(NULL),
destructor_call(NULL)
{
}
/* -----------------------------------------------------------------------------
* getProxyName()
*
* Test to see if a type corresponds to something wrapped with a proxy class
* Return NULL if not otherwise the proxy class name
* ----------------------------------------------------------------------------- */
String *getProxyName(SwigType *t) {
if (proxy_flag) {
Node *n = classLookup(t);
if (n) {
return Getattr(n,"sym:name");
}
}
return NULL;
}
/* ------------------------------------------------------------
* main()
* ------------------------------------------------------------ */
virtual void main(int argc, char *argv[]) {
SWIG_library_directory("csharp");
// Look for certain command line options
for (int i = 1; i < argc; i++) {
if (argv[i]) {
if (strcmp(argv[i],"-dllimport") == 0) {
if (argv[i+1]) {
dllimport = NewString("");
Printf(dllimport, argv[i+1]);
Swig_mark_arg(i);
Swig_mark_arg(i+1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i],"-namespace") == 0) {
if (argv[i+1]) {
namespce = NewString("");
Printf(namespce, argv[i+1]);
Swig_mark_arg(i);
Swig_mark_arg(i+1);
i++;
} else {
Swig_arg_error();
}
} else if ((strcmp(argv[i],"-noproxy") == 0)) {
Swig_mark_arg(i);
proxy_flag = false;
} else if (strcmp(argv[i],"-oldvarnames") == 0) {
Swig_mark_arg(i);
old_variable_names = true;
} else if (strcmp(argv[i],"-help") == 0) {
Printf(stdout,"%s\n", usage);
}
}
}
// Add a symbol to the parser for conditional compilation
Preprocessor_define("SWIGCSHARP 1",0);
// Add typemap definitions
SWIG_typemap_lang("csharp");
SWIG_config_file("csharp.swg");
allow_overloading();
}
/* ---------------------------------------------------------------------
* top()
* --------------------------------------------------------------------- */
virtual int top(Node *n) {
// Get any options set in the module directive
Node* optionsnode = Getattr( Getattr(n,"module"), "options");
if (optionsnode) {
if (Getattr(optionsnode,"imclassname"))
imclass_name = Copy(Getattr(optionsnode,"imclassname"));
}
/* Initialize all of the output files */
String *outfile = Getattr(n,"outfile");
f_runtime = NewFile(outfile,"w");
if (!f_runtime) {
FileErrorDisplay(outfile);
SWIG_exit(EXIT_FAILURE);
}
f_init = NewString("");
f_header = NewString("");
f_wrappers = NewString("");
/* Register file targets with the SWIG file handler */
Swig_register_filebyname("header",f_header);
Swig_register_filebyname("wrapper",f_wrappers);
Swig_register_filebyname("runtime",f_runtime);
Swig_register_filebyname("init",f_init);
swig_types_hash = NewHash();
filenames_list = NewList();
// Make the intermediary class and module class names. The intermediary class name can be set in the module directive.
if (!imclass_name) {
imclass_name = NewStringf("%sPINVOKE", Getattr(n,"name"));
module_class_name = Copy(Getattr(n,"name"));
} else {
// Rename the module name if it is the same as intermediary class name - a backwards compatibility solution
if (Cmp(imclass_name, Getattr(n,"name")) == 0)
module_class_name = NewStringf("%sModule", Getattr(n,"name"));
else
module_class_name = Copy(Getattr(n,"name"));
}
imclass_class_code = NewString("");
proxy_class_def = NewString("");
proxy_class_code = NewString("");
module_class_constants_code = NewString("");
imclass_baseclass = NewString("");
imclass_interfaces = NewString("");
imclass_class_modifiers = NewString("");
module_class_code = NewString("");
module_baseclass = NewString("");
module_interfaces = NewString("");
module_imports = NewString("");
module_class_modifiers = NewString("");
imclass_imports = NewString("");
imclass_cppcasts_code = NewString("");
upcasts_code = NewString("");
if (!namespce) namespce = NewString("");
if (!dllimport) dllimport = Copy(module_class_name);
Swig_banner(f_runtime); // Print the SWIG banner message
String *wrapper_name = NewString("");
Printf(wrapper_name, "CSharp_%%f", imclass_name);
Swig_name_register((char*)"wrapper", Char(wrapper_name));
if (old_variable_names) {
Swig_name_register((char*)"set", (char*)"set_%v");
Swig_name_register((char*)"get", (char*)"get_%v");
}
Delete(wrapper_name);
Printf(f_wrappers,"\n#ifdef __cplusplus\n");
Printf(f_wrappers,"extern \"C\" {\n");
Printf(f_wrappers,"#endif\n\n");
/* Emit code */
Language::top(n);
// Generate the intermediary class
{
String *filen = NewStringf("%s%s.cs", SWIG_output_directory(), imclass_name);
File *f_im = NewFile(filen,"w");
if(!f_im) {
FileErrorDisplay(filen);
SWIG_exit(EXIT_FAILURE);
}
Append(filenames_list, Copy(filen));
Delete(filen); filen = NULL;
// Start writing out the intermediary class file
emitBanner(f_im);
if(Len(namespce) > 0)
Printf(f_im, "namespace %s {\n", namespce);
if(imclass_imports)
Printf(f_im, "%s\n", imclass_imports);
if (Len(imclass_class_modifiers) > 0)
Printf(f_im, "%s ", imclass_class_modifiers);
Printf(f_im, "%s ", imclass_name);
if (imclass_baseclass && *Char(imclass_baseclass))
Printf(f_im, ": %s ", imclass_baseclass);
if (Len(imclass_interfaces) > 0)
Printv(f_im, "implements ", imclass_interfaces, " ", NIL);
Printf(f_im, "{\n");
// Add the intermediary class methods
Replaceall(imclass_class_code, "$module", module_class_name);
Replaceall(imclass_class_code, "$imclassname", imclass_name);
Replaceall(imclass_class_code, "$dllimport", dllimport);
Printv(f_im, imclass_class_code, NIL);
Printv(f_im, imclass_cppcasts_code, NIL);
// Finish off the class
Printf(f_im, "}\n");
Printf(f_im, Len(namespce) > 0 ? "\n}\n" : "");
Close(f_im);
}
// Generate the C# module class
{
String *filen = NewStringf("%s%s.cs", SWIG_output_directory(), module_class_name);
File *f_module = NewFile(filen,"w");
if(!f_module) {
FileErrorDisplay(filen);
SWIG_exit(EXIT_FAILURE);
}
Append(filenames_list, Copy(filen));
Delete(filen); filen = NULL;
// Start writing out the module class file
emitBanner(f_module);
if(Len(namespce) > 0)
Printf(f_module, "namespace %s {\n", namespce);
if(module_imports)
Printf(f_module, "%s\n", module_imports);
if (Len(module_class_modifiers) > 0)
Printf(f_module, "%s ", module_class_modifiers);
Printf(f_module, "%s ", module_class_name);
if (module_baseclass && *Char(module_baseclass))
Printf(f_module, ": %s ", module_baseclass);
if (Len(module_interfaces) > 0)
Printv(f_module, "implements ", module_interfaces, " ", NIL);
Printf(f_module, "{\n");
Replaceall(module_class_code, "$module", module_class_name);
Replaceall(module_class_constants_code, "$module", module_class_name);
Replaceall(module_class_code, "$imclassname", imclass_name);
Replaceall(module_class_constants_code, "$imclassname", imclass_name);
Replaceall(module_class_code, "$dllimport", dllimport);
Replaceall(module_class_constants_code, "$dllimport", dllimport);
// Add the wrapper methods
Printv(f_module, module_class_code, NIL);
// Write out all the global constants
Printv(f_module, module_class_constants_code, NIL);
// Finish off the class
Printf(f_module, "}\n");
Printf(f_module, Len(namespce) > 0 ? "\n}\n" : "");
Close(f_module);
}
if(upcasts_code)
Printv(f_wrappers,upcasts_code,NIL);
Printf(f_wrappers,"#ifdef __cplusplus\n");
Printf(f_wrappers,"}\n");
Printf(f_wrappers,"#endif\n");
// Output a C# type wrapper class for each SWIG type
for (Iterator swig_type = First(swig_types_hash); swig_type.key; swig_type = Next(swig_type)) {
emitTypeWrapperClass(swig_type.key, swig_type.item);
}
// Check for overwriting file problems on filesystems that are case insensitive
Iterator it1;
Iterator it2;
for (it1 = First(filenames_list); it1.item; it1 = Next(it1)) {
String *item1_lower = Swig_string_lower(it1.item);
for (it2 = Next(it1); it2.item; it2 = Next(it2)) {
String *item2_lower = Swig_string_lower(it2.item);
if (it1.item && it2.item) {
if (Strcmp(item1_lower, item2_lower) == 0) {
Swig_warning(WARN_LANG_PORTABILITY_FILENAME, input_file, line_number,
"Portability warning: File %s will be overwritten by %s on case insensitive filesystems such as "
"Windows' FAT32 and NTFS unless the class/module name is renamed\n", it1.item, it2.item);
}
}
Delete(item2_lower);
}
Delete(item1_lower);
}
Delete(swig_types_hash); swig_types_hash = NULL;
Delete(filenames_list); filenames_list = NULL;
Delete(imclass_name); imclass_name = NULL;
Delete(imclass_class_code); imclass_class_code = NULL;
Delete(proxy_class_def); proxy_class_def = NULL;
Delete(proxy_class_code); proxy_class_code = NULL;
Delete(module_class_constants_code); module_class_constants_code = NULL;
Delete(imclass_baseclass); imclass_baseclass = NULL;
Delete(imclass_interfaces); imclass_interfaces = NULL;
Delete(imclass_class_modifiers); imclass_class_modifiers = NULL;
Delete(module_class_name); module_class_name = NULL;
Delete(module_class_code); module_class_code = NULL;
Delete(module_baseclass); module_baseclass = NULL;
Delete(module_interfaces); module_interfaces = NULL;
Delete(module_imports); module_imports = NULL;
Delete(module_class_modifiers); module_class_modifiers = NULL;
Delete(imclass_imports); imclass_imports = NULL;
Delete(imclass_cppcasts_code); imclass_cppcasts_code = NULL;
Delete(upcasts_code); upcasts_code = NULL;
Delete(namespce); namespce = NULL;
/* Close all of the files */
Dump(f_header,f_runtime);
Dump(f_wrappers,f_runtime);
Wrapper_pretty_print(f_init,f_runtime);
Delete(f_header);
Delete(f_wrappers);
Delete(f_init);
Close(f_runtime);
Delete(f_runtime);
return SWIG_OK;
}
/* -----------------------------------------------------------------------------
* emitBanner()
* ----------------------------------------------------------------------------- */
void emitBanner(File *f) {
Printf(f, "/* ----------------------------------------------------------------------------\n");
Printf(f, " * This file was automatically generated by SWIG (http://www.swig.org).\n");
Printf(f, " * Version %s\n", PACKAGE_VERSION);
Printf(f, " *\n");
Printf(f, " * Do not make changes to this file unless you know what you are doing--modify\n");
Printf(f, " * the SWIG interface file instead.\n");
Printf(f, " * ----------------------------------------------------------------------------- */\n\n");
}
/* ----------------------------------------------------------------------
* nativeWrapper()
* ---------------------------------------------------------------------- */
virtual int nativeWrapper(Node *n) {
String *wrapname = Getattr(n,"wrap:name");
if (!addSymbol(wrapname,n)) return SWIG_ERROR;
if (Getattr(n,"type")) {
Swig_save("nativeWrapper",n,"name",NIL);
Setattr(n,"name", wrapname);
native_function_flag = true;
functionWrapper(n);
Swig_restore(n);
native_function_flag = false;
} else {
Printf(stderr,"%s : Line %d. No return type for %%native method %s.\n", input_file, line_number, Getattr(n,"wrap:name"));
}
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* functionWrapper()
* ---------------------------------------------------------------------- */
virtual int functionWrapper(Node *n) {
String *symname = Getattr(n,"sym:name");
SwigType *t = Getattr(n,"type");
ParmList *l = Getattr(n,"parms");
String *tm;
Parm *p;
int i;
String *c_return_type = NewString("");
String *im_return_type = NewString("");
String *cleanup = NewString("");
String *outarg = NewString("");
String *body = NewString("");
String *im_outattribute = 0;
int num_arguments = 0;
int num_required = 0;
bool is_void_return;
String *overloaded_name = getOverloadedName(n);
if (!Getattr(n,"sym:overloaded")) {
if (!addSymbol(Getattr(n,"sym:name"),n)) return SWIG_ERROR;
}
/*
The rest of this function deals with generating the intermediary class wrapper function (that wraps
a c/c++ function) and generating the PInvoke c code. Each C# wrapper function has a
matching PInvoke c function call.
*/
// A new wrapper function object
Wrapper *f = NewWrapper();
// Make a wrapper name for this function
String *wname = Swig_name_wrapper(overloaded_name);
/* Attach the non-standard typemaps to the parameter list. */
Swig_typemap_attach_parms("ctype", l, f);
Swig_typemap_attach_parms("imtype", l, f);
/* Get return types */
if ((tm = Swig_typemap_lookup_new("ctype",n,"",0))) {
String *ctypeout = Getattr(n,"tmap:ctype:out"); // the type in the ctype typemap's out attribute overrides the type in the typemap
if (ctypeout)
tm = ctypeout;
Printf(c_return_type,"%s", tm);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CTYPE_UNDEF, input_file, line_number,
"No ctype typemap defined for %s\n", SwigType_str(t,0));
}
if ((tm = Swig_typemap_lookup_new("imtype",n,"",0))) {
String *imtypeout = Getattr(n,"tmap:imtype:out"); // the type in the imtype typemap's out attribute overrides the type in the typemap
if (imtypeout)
tm = imtypeout;
Printf(im_return_type,"%s", tm);
im_outattribute = Getattr(n,"tmap:imtype:outattributes");
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSTYPE_UNDEF, input_file, line_number,
"No imtype typemap defined for %s\n", SwigType_str(t,0));
}
is_void_return = (Cmp(c_return_type, "void") == 0);
if (!is_void_return)
Wrapper_add_localv(f,"jresult", c_return_type, "jresult",NIL);
Printv(f->def, " SWIGEXPORT ", c_return_type, " SWIGSTDCALL ", wname, "(", NIL);
// Emit all of the local variables for holding arguments.
emit_args(t,l,f);
/* Attach the standard typemaps */
emit_attach_parmmaps(l,f);
// Parameter overloading
Setattr(n,"wrap:parms",l);
Setattr(n,"wrap:name", wname);
// Wrappers not wanted for some methods where the parameters cannot be overloaded in C#
if (Getattr(n,"sym:overloaded")) {
// Emit warnings for the few cases that can't be overloaded in C# and give up on generating wrapper
Swig_overload_check(n);
if (Getattr(n, "overload:ignore"))
return SWIG_OK;
}
Printv(imclass_class_code,
"\n [DllImport(\"", dllimport, "\", EntryPoint=\"CSharp_", overloaded_name, "\")]\n", NIL);
if (im_outattribute)
Printf(imclass_class_code, " %s\n", im_outattribute);
Printf(imclass_class_code, " public static extern %s %s(", im_return_type, overloaded_name);
/* Get number of required and total arguments */
num_arguments = emit_num_arguments(l);
num_required = emit_num_required(l);
int gencomma = 0;
// Now walk the function parameter list and generate code to get arguments
for (i = 0, p=l; i < num_arguments; i++) {
while (checkAttribute(p,"tmap:in:numinputs","0")) {
p = Getattr(p,"tmap:in:next");
}
SwigType *pt = Getattr(p,"type");
String *ln = Getattr(p,"lname");
String *im_param_type = NewString("");
String *c_param_type = NewString("");
String *arg = NewString("");
Printf(arg,"j%s", ln);
/* Get the ctype types of the parameter */
if ((tm = Getattr(p,"tmap:ctype"))) {
Printv(c_param_type, tm, NIL);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CTYPE_UNDEF, input_file, line_number,
"No ctype typemap defined for %s\n", SwigType_str(pt,0));
}
/* Get the intermediary class parameter types of the parameter */
if ((tm = Getattr(p,"tmap:imtype"))) {
String *inattributes = Getattr(p,"tmap:imtype:inattributes");
Printf(im_param_type, "%s%s", inattributes ? (const String *)inattributes : empty_string, tm);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSTYPE_UNDEF, input_file, line_number,
"No imtype typemap defined for %s\n", SwigType_str(pt,0));
}
/* Add parameter to intermediary class method */
if(gencomma) Printf(imclass_class_code, ", ");
Printf(imclass_class_code, "%s %s", im_param_type, arg);
// Add parameter to C function
Printv(f->def, gencomma?", ":"", c_param_type, " ", arg, NIL);
gencomma = 1;
// Get typemap for this argument
if ((tm = Getattr(p,"tmap:in"))) {
canThrow(n, "in", p);
Replaceall(tm,"$source",arg); /* deprecated */
Replaceall(tm,"$target",ln); /* deprecated */
Replaceall(tm,"$arg",arg); /* deprecated? */
Replaceall(tm,"$input", arg);
Setattr(p,"emit:input", arg);
Printf(f->code,"%s\n", tm);
p = Getattr(p,"tmap:in:next");
} else {
Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number,
"Unable to use type %s as a function argument.\n",SwigType_str(pt,0));
p = nextSibling(p);
}
Delete(im_param_type);
Delete(c_param_type);
Delete(arg);
}
/* Insert constraint checking code */
for (p = l; p;) {
if ((tm = Getattr(p,"tmap:check"))) {
canThrow(n, "check", p);
Replaceall(tm,"$target",Getattr(p,"lname")); /* deprecated */
Replaceall(tm,"$arg",Getattr(p,"emit:input")); /* deprecated? */
Replaceall(tm,"$input",Getattr(p,"emit:input"));
Printv(f->code,tm,"\n",NIL);
p = Getattr(p,"tmap:check:next");
} else {
p = nextSibling(p);
}
}
/* Insert cleanup code */
for (p = l; p;) {
if ((tm = Getattr(p,"tmap:freearg"))) {
canThrow(n, "freearg", p);
Replaceall(tm,"$source",Getattr(p,"emit:input")); /* deprecated */
Replaceall(tm,"$arg",Getattr(p,"emit:input")); /* deprecated? */
Replaceall(tm,"$input",Getattr(p,"emit:input"));
Printv(cleanup,tm,"\n",NIL);
p = Getattr(p,"tmap:freearg:next");
} else {
p = nextSibling(p);
}
}
/* Insert argument output code */
for (p = l; p;) {
if ((tm = Getattr(p,"tmap:argout"))) {
canThrow(n, "argout", p);
Replaceall(tm,"$source",Getattr(p,"emit:input")); /* deprecated */
Replaceall(tm,"$target",Getattr(p,"lname")); /* deprecated */
Replaceall(tm,"$arg",Getattr(p,"emit:input")); /* deprecated? */
Replaceall(tm,"$result","jresult");
Replaceall(tm,"$input",Getattr(p,"emit:input"));
Printv(outarg,tm,"\n",NIL);
p = Getattr(p,"tmap:argout:next");
} else {
p = nextSibling(p);
}
}
// Get any C# exception classes in the throws typemap
ParmList *throw_parm_list = NULL;
if ((throw_parm_list = Getattr(n,"throws"))) {
Swig_typemap_attach_parms("throws", throw_parm_list, f);
for (p = throw_parm_list; p; p=nextSibling(p)) {
if ((tm = Getattr(p,"tmap:throws"))) {
canThrow(n, "throws", p);
}
}
}
if (Cmp(nodeType(n), "constant") == 0) {
// Wrapping a constant hack
Swig_save("functionWrapper",n,"wrap:action",NIL);
// below based on Swig_VargetToFunction()
SwigType *ty = Swig_wrapped_var_type(Getattr(n,"type"), use_naturalvar_mode(n));
Setattr(n,"wrap:action", NewStringf("result = (%s) %s;\n", SwigType_lstr(ty,0), Getattr(n, "value")));
}
// Now write code to make the function call
if(!native_function_flag)
emit_action(n,f);
if (Cmp(nodeType(n), "constant") == 0)
Swig_restore(n);
/* Return value if necessary */
String *null_attribute = 0;
if(!native_function_flag) {
if ((tm = Swig_typemap_lookup_new("out",n,"result",0))) {
canThrow(n, "out", n);
Replaceall(tm,"$source", "result"); /* deprecated */
Replaceall(tm,"$target", "jresult"); /* deprecated */
Replaceall(tm,"$result","jresult");
Printf(f->code,"%s", tm);
null_attribute = Getattr(n, "tmap:out:null");
if (Len(tm))
Printf(f->code,"\n");
} else {
Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number,
"Unable to use return type %s in function %s.\n", SwigType_str(t,0), Getattr(n,"name"));
}
}
/* Output argument output code */
Printv(f->code,outarg,NIL);
/* Output cleanup code */
Printv(f->code,cleanup,NIL);
/* Look to see if there is any newfree cleanup code */
if (GetFlag(n,"feature:new")) {
if ((tm = Swig_typemap_lookup_new("newfree",n,"result",0))) {
canThrow(n, "newfree", n);
Replaceall(tm,"$source","result"); /* deprecated */
Printf(f->code,"%s\n",tm);
}
}
/* See if there is any return cleanup code */
if(!native_function_flag) {
if ((tm = Swig_typemap_lookup_new("ret", n, "result", 0))) {
canThrow(n, "ret", n);
Replaceall(tm,"$source","result"); /* deprecated */
Printf(f->code,"%s\n",tm);
}
}
/* Finish C function and intermediary class function definitions */
Printf(imclass_class_code, ")");
Printf(imclass_class_code, ";\n");
Printf(f->def,") {");
if(!is_void_return)
Printv(f->code, " return jresult;\n", NIL);
Printf(f->code, "}\n");
/* Substitute the cleanup code */
Replaceall(f->code,"$cleanup",cleanup);
/* Contract macro modification */
if (Replaceall(f->code, "SWIG_contract_assert(", "SWIG_contract_assert($null, ") > 0) {
Setattr(n,"csharp:canthrow","1");
}
if(!null_attribute)
Replaceall(f->code,"$null","0");
else
Replaceall(f->code,"$null",null_attribute);
/* Dump the function out */
if(!native_function_flag) {
Wrapper_print(f,f_wrappers);
// Handle %csexception which sets the canthrow attribute
if (Getattr(n,"feature:except:canthrow"))
Setattr(n,"csharp:canthrow","1");
// A very simple check (it is not foolproof) to help typemap/feature writers for
// throwing C# exceptions from unmanaged code. It checks for the common methods which
// set a pending C# exception... the 'canthrow' typemap/feature attribute must be set
// so that code which checks for pending exceptions is added in the C# proxy method.
if (!Getattr(n,"csharp:canthrow")) {
if(Strstr(f->code, "SWIG_exception")) {
Swig_warning(WARN_CSHARP_CANTHROW, input_file, line_number,
"Unmanaged code contains a call to SWIG_exception and C# code does not handle pending exceptions via the canthrow attribute.\n");
} else if(Strstr(f->code, "SWIG_CSharpSetPendingException")) {
Swig_warning(WARN_CSHARP_CANTHROW, input_file, line_number,
"Unmanaged code contains a call to a SWIG_CSharpSetPendingException method and C# code does not handle pending exceptions via the canthrow attribute.\n");
}
}
}
if(!(proxy_flag && is_wrapping_class()) && !enum_constant_flag) {
moduleClassFunctionHandler(n);
}
/*
* Generate the proxy class properties for public member variables.
* Not for enums and constants.
*/
if(proxy_flag && wrapping_member_flag && !enum_constant_flag) {
// Capitalize the first letter in the variable in the getter/setter function name
bool getter_flag = Cmp(symname, Swig_name_set(Swig_name_member(proxy_class_name, variable_name))) != 0;
String *getter_setter_name = NewString("");
if(!getter_flag)
Printf(getter_setter_name,"set");
else
Printf(getter_setter_name,"get");
Putc(toupper((int) *Char(variable_name)), getter_setter_name);
Printf(getter_setter_name, "%s", Char(variable_name)+1);
Setattr(n,"proxyfuncname", getter_setter_name);
Setattr(n,"imfuncname", symname);
proxyClassFunctionHandler(n);
Delete(getter_setter_name);
}
Delete(c_return_type);
Delete(im_return_type);
Delete(cleanup);
Delete(outarg);
Delete(body);
Delete(overloaded_name);
DelWrapper(f);
return SWIG_OK;
}
/* -----------------------------------------------------------------------
* variableWrapper()
* ----------------------------------------------------------------------- */
virtual int variableWrapper(Node *n) {
Language::variableWrapper(n);
return SWIG_OK;
}
/* -----------------------------------------------------------------------
* globalvariableHandler()
* ------------------------------------------------------------------------ */
virtual int globalvariableHandler(Node *n) {
generate_property_declaration_flag = true;
variable_name = Getattr(n,"sym:name");
global_variable_flag = true;
int ret = Language::globalvariableHandler(n);
global_variable_flag = false;
generate_property_declaration_flag = false;
Printf(module_class_code, "\n }\n\n");
return ret;
}
/* ----------------------------------------------------------------------
* enumDeclaration()
*
* C/C++ enums can be mapped in one of 4 ways, depending on the cs:enum feature specified:
* 1) Simple enums - simple constant within the proxy class or module class
* 2) Typeunsafe enums - simple constant in a C# class (class named after the c++ enum name)
* 3) Typesafe enum - typesafe enum pattern (class named after the c++ enum name)
* 4) Proper enums - proper C# enum
* Anonymous enums always default to 1)
* ---------------------------------------------------------------------- */
virtual int enumDeclaration(Node *n) {
if (!ImportMode) {
if (getCurrentClass() && (cplus_mode != PUBLIC)) return SWIG_NOWRAP;
enum_code = NewString("");
String *symname = Getattr(n,"sym:name");
String *constants_code = (proxy_flag && is_wrapping_class()) ? proxy_class_constants_code : module_class_constants_code;
EnumFeature enum_feature = decodeEnumFeature(n);
String *typemap_lookup_type = Getattr(n,"name");
if ((enum_feature != SimpleEnum) && symname && typemap_lookup_type) {
// Wrap (non-anonymous) C/C++ enum within a typesafe, typeunsafe or proper C# enum
// Pure C# baseclass and interfaces
const String *pure_baseclass = typemapLookup("csbase", typemap_lookup_type, WARN_NONE);
const String *pure_interfaces = typemapLookup("csinterfaces", typemap_lookup_type, WARN_NONE);
// Class attributes
const String *csattributes = typemapLookup("csattributes", typemap_lookup_type, WARN_NONE);
if (csattributes && *Char(csattributes))
Printf(enum_code, "%s\n", csattributes);
// Emit the enum
Printv(enum_code,
typemapLookup("csclassmodifiers", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers (enum modifiers really)
" ",
symname,
(*Char(pure_baseclass) || *Char(pure_interfaces)) ?
" : " :
"",
pure_baseclass,
((*Char(pure_baseclass)) && *Char(pure_interfaces)) ? // Interfaces
", " :
"",
pure_interfaces,
" {\n",
NIL);
} else {
// Wrap C++ enum with integers - just indicate start of enum with a comment, no comment for anonymous enums of any sort
if (symname && !Getattr(n,"unnamedinstance"))
Printf(constants_code, " // %s \n", symname);
}
// Emit each enum item
Language::enumDeclaration(n);
if ((enum_feature != SimpleEnum) && symname && typemap_lookup_type) {
// Wrap (non-anonymous) C/C++ enum within a typesafe, typeunsafe or proper C# enum
// Finish the enum declaration
// Typemaps are used to generate the enum definition in a similar manner to proxy classes.
Printv(enum_code,
(enum_feature == ProperEnum) ?
"\n" :
typemapLookup("csbody", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CSBODY_UNDEF), // main body of class
typemapLookup("cscode", typemap_lookup_type, WARN_NONE), // extra C# code
"}",
NIL);
Replaceall(enum_code, "$csclassname", symname);
// Substitute $enumvalues - intended usage is for typesafe enums
if (Getattr(n,"enumvalues"))
Replaceall(enum_code, "$enumvalues", Getattr(n,"enumvalues"));
else
Replaceall(enum_code, "$enumvalues", "");
if (proxy_flag && is_wrapping_class()) {
// Enums defined within the C++ class are defined within the proxy class
// Add extra indentation
Replaceall(enum_code, "\n", "\n ");
Replaceall(enum_code, " \n", "\n");
Printv(proxy_class_constants_code, " ", enum_code, "\n\n", NIL);
} else {
// Global enums are defined in their own file
String *filen = NewStringf("%s%s.cs", SWIG_output_directory(), symname);
File *f_enum = NewFile(filen,"w");
if(!f_enum) {
FileErrorDisplay(filen);
SWIG_exit(EXIT_FAILURE);
}
Append(filenames_list, Copy(filen));
Delete(filen); filen = NULL;
// Start writing out the enum file
emitBanner(f_enum);
if(Len(namespce) > 0)
Printf(f_enum, "namespace %s {\n", namespce);
Printv(f_enum,
typemapLookup("csimports", typemap_lookup_type, WARN_NONE), // Import statements
"\n",
enum_code,
"\n",
NIL);
Printf(f_enum, Len(namespce) > 0 ? "\n}\n" : "");
Close(f_enum);
}
} else {
// Wrap C++ enum with simple constant
Printf(enum_code, "\n");
if (proxy_flag && is_wrapping_class())
Printv(proxy_class_constants_code, enum_code, NIL);
else
Printv(module_class_constants_code, enum_code, NIL);
}
Delete(enum_code); enum_code = NULL;
}
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* enumvalueDeclaration()
* ---------------------------------------------------------------------- */
virtual int enumvalueDeclaration(Node *n) {
if (getCurrentClass() && (cplus_mode != PUBLIC)) return SWIG_NOWRAP;
Swig_require("enumvalueDeclaration",n,"*name", "?value",NIL);
String *symname = Getattr(n,"sym:name");
String *value = Getattr(n,"value");
String *name = Getattr(n,"name");
String *tmpValue;
// Strange hack from parent method
if (value)
tmpValue = NewString(value);
else
tmpValue = NewString(name);
// Note that this is used in enumValue() amongst other places
Setattr(n, "value", tmpValue);
{
EnumFeature enum_feature = decodeEnumFeature(parentNode(n));
if ((enum_feature == ProperEnum) && Getattr(parentNode(n),"sym:name") && !Getattr(parentNode(n),"unnamedinstance")) {
// Wrap (non-anonymous) C/C++ enum with a proper C# enum
// Emit the enum item.
if (!Getattr(n,"_last")) // Only the first enum item has this attribute set
Printf(enum_code, ",\n");
Printf(enum_code, " %s", symname);
// Check for the %csconstvalue feature
String *value = Getattr(n,"feature:cs:constvalue");
// Note that the enum value must be a true constant and cannot be set from a PINVOKE call, thus no support for %csconst(0)
value = value ? value : Getattr(n,"enumvalue");
if (value) {
Printf(enum_code, " = %s", value);
}
} else {
// Wrap C/C++ enums with constant integers or use the typesafe enum pattern
const String *parent_name = Getattr(parentNode(n),"name");
String *typemap_lookup_type = parent_name ? Copy(parent_name) : NewString("int");
const String *tm = typemapLookup("cstype", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CSTYPE_UNDEF);
String *return_type = Copy(tm);
Delete(typemap_lookup_type); typemap_lookup_type = NULL;
// The %csconst feature determines how the constant value is obtained
int const_feature_flag = GetFlag(n,"feature:cs:const");
if ((enum_feature == TypesafeEnum) && Getattr(parentNode(n),"sym:name") && !Getattr(parentNode(n),"unnamedinstance")) {
// Wrap (non-anonymouse) enum using the typesafe enum pattern
if (Getattr(n,"enumvalue")) {
String *value = enumValue(n);
Printf(enum_code, " public static readonly %s %s = new %s(\"%s\", %s);\n", return_type, symname, return_type, symname, value);
Delete(value);
} else {
Printf(enum_code, " public static readonly %s %s = new %s(\"%s\");\n", return_type, symname, return_type, symname);
}
} else {
// Simple integer constants
// Note these are always generated for anonymous enums, no matter what enum_feature is specified
// Code generated is the same for SimpleEnum and TypeunsafeEnum -> the class it is generated into is determined later
const char *const_readonly = const_feature_flag ? "const" : "static readonly";
String *value = enumValue(n);
Printf(enum_code, " public %s %s %s = %s;\n", const_readonly, return_type, symname, value);
Delete(value);
}
}
// Add the enum value to the comma separated list being constructed in the enum declaration.
String *enumvalues = Getattr(parentNode(n), "enumvalues");
if (!enumvalues)
Setattr(parentNode(n), "enumvalues", Copy(symname));
else
Printv(enumvalues, ", ", symname, NIL);
}
Delete(tmpValue);
Swig_restore(n);
return SWIG_OK;
}
/* -----------------------------------------------------------------------
* constantWrapper()
* Used for wrapping constants - #define or %constant.
* Also for inline initialised const static primitive type member variables (short, int, double, enums etc).
* C# static const variables are generated for these.
* If the %csconst(1) feature is used then the C constant value is used to initialise the C# const variable.
* If not, a PINVOKE method is generated to get the C constant value for initialisation of the C# const variable.
* However, if the %csconstvalue feature is used, it overrides all other ways to generate the initialisation.
* Also note that this method might be called for wrapping enum items (when the enum is using %csconst(0)).
* ------------------------------------------------------------------------ */
virtual int constantWrapper(Node *n) {
String *symname = Getattr(n,"sym:name");
SwigType *t = Getattr(n,"type");
ParmList *l = Getattr(n,"parms");
String *tm;
String *return_type = NewString("");
String *constants_code = NewString("");
if (!addSymbol(symname,n)) return SWIG_ERROR;
bool is_enum_item = (Cmp(nodeType(n), "enumitem") == 0);
// The %csconst feature determines how the constant value is obtained
int const_feature_flag = GetFlag(n,"feature:cs:const");
/* Adjust the enum type for the Swig_typemap_lookup.
* We want the same jstype typemap for all the enum items so we use the enum type (parent node). */
if (is_enum_item) {
t = Getattr(parentNode(n),"enumtype");
Setattr(n,"type", t);
}
/* Attach the non-standard typemaps to the parameter list. */
Swig_typemap_attach_parms("cstype", l, NULL);
/* Get C# return types */
bool classname_substituted_flag = false;
if ((tm = Swig_typemap_lookup_new("cstype",n,"",0))) {
String *cstypeout = Getattr(n,"tmap:cstype:out"); // the type in the cstype typemap's out attribute overrides the type in the typemap
if (cstypeout)
tm = cstypeout;
classname_substituted_flag = substituteClassname(t, tm);
Printf(return_type, "%s", tm);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF, input_file, line_number,
"No cstype typemap defined for %s\n", SwigType_str(t,0));
}
// Add the stripped quotes back in
String *new_value = NewString("");
Swig_save("constantWrapper",n,"value",NIL);
if(SwigType_type(t) == T_STRING) {
Printf(new_value, "\"%s\"", Copy(Getattr(n, "value")));
Setattr(n, "value", new_value);
}
else if(SwigType_type(t) == T_CHAR) {
Printf(new_value, "\'%s\'", Copy(Getattr(n, "value")));
Setattr(n, "value", new_value);
}
const String *outattributes = Getattr(n,"tmap:cstype:outattributes");
if (outattributes)
Printf(constants_code, " %s\n", outattributes);
const String *itemname = (proxy_flag && wrapping_member_flag) ? variable_name : symname;
Printf(constants_code, " public %s %s %s = ", (const_feature_flag ? "const" : "static readonly"), return_type, itemname);
// Check for the %csconstvalue feature
String *value = Getattr(n,"feature:cs:constvalue");
if (value) {
Printf(constants_code, "%s;\n", value);
} else if (!const_feature_flag) {
// Default enum and constant handling will work with any type of C constant and initialises the C# variable from C through a PINVOKE call.
if(classname_substituted_flag) {
if (SwigType_isenum(t)) {
// This handles wrapping of inline initialised const enum static member variables (not when wrapping enum items - ignored later on)
Printf(constants_code, "(%s)%s.%s();\n", return_type, imclass_name, Swig_name_get(symname));
} else {
// This handles function pointers using the %constant directive
Printf(constants_code, "new %s(%s.%s(), false);\n", return_type, imclass_name, Swig_name_get(symname));
}
} else
Printf(constants_code, "%s.%s();\n", imclass_name, Swig_name_get(symname));
// Each constant and enum value is wrapped with a separate PInvoke function call
SetFlag(n,"feature:immutable");
enum_constant_flag = true;
variableWrapper(n);
enum_constant_flag = false;
} else {
// Alternative constant handling will use the C syntax to make a true C# constant and hope that it compiles as C# code
Printf(constants_code, "%s;\n", Getattr(n,"value"));
}
// Emit the generated code to appropriate place
// Enums only emit the intermediate and PINVOKE methods, so no proxy or module class wrapper methods needed
if (!is_enum_item) {
if(proxy_flag && wrapping_member_flag)
Printv(proxy_class_constants_code, constants_code, NIL);
else
Printv(module_class_constants_code, constants_code, NIL);
}
// Cleanup
Swig_restore(n);
Delete(new_value);
Delete(return_type);
Delete(constants_code);
return SWIG_OK;
}
/* -----------------------------------------------------------------------------
* insertDirective()
* ----------------------------------------------------------------------------- */
virtual int insertDirective(Node *n) {
String *code = Getattr(n,"code");
Replaceall(code, "$module", module_class_name);
Replaceall(code, "$imclassname", imclass_name);
Replaceall(code, "$dllimport", dllimport);
return Language::insertDirective(n);
}
/* -----------------------------------------------------------------------------
* pragmaDirective()
*
* Valid Pragmas:
* imclassbase - base (extends) for the intermediary class
* imclassclassmodifiers - class modifiers for the intermediary class
* imclasscode - text (C# code) is copied verbatim to the intermediary class
* imclassimports - import statements for the intermediary class
* imclassinterfaces - interface (implements) for the intermediary class
*
* modulebase - base (extends) for the module class
* moduleclassmodifiers - class modifiers for the module class
* modulecode - text (C# code) is copied verbatim to the module class
* moduleimports - import statements for the module class
* moduleinterfaces - interface (implements) for the module class
*
* ----------------------------------------------------------------------------- */
virtual int pragmaDirective(Node *n) {
if (!ImportMode) {
String *lang = Getattr(n,"lang");
String *code = Getattr(n,"name");
String *value = Getattr(n,"value");
if(Strcmp(lang, "csharp") == 0) {
String *strvalue = NewString(value);
Replaceall(strvalue,"\\\"", "\"");
if(Strcmp(code, "imclassbase") == 0) {
Delete(imclass_baseclass);
imclass_baseclass = Copy(strvalue);
}
else if(Strcmp(code, "imclassclassmodifiers") == 0) {
Delete(imclass_class_modifiers);
imclass_class_modifiers = Copy(strvalue);
}
else if(Strcmp(code, "imclasscode") == 0) {
Printf(imclass_class_code, "%s\n", strvalue);
}
else if(Strcmp(code, "imclassimports") == 0) {
Delete(imclass_imports);
imclass_imports = Copy(strvalue);
}
else if(Strcmp(code, "imclassinterfaces") == 0) {
Delete(imclass_interfaces);
imclass_interfaces = Copy(strvalue);
}
else if(Strcmp(code, "modulebase") == 0) {
Delete(module_baseclass);
module_baseclass = Copy(strvalue);
}
else if(Strcmp(code, "moduleclassmodifiers") == 0) {
Delete(module_class_modifiers);
module_class_modifiers = Copy(strvalue);
}
else if(Strcmp(code, "modulecode") == 0) {
Printf(module_class_code, "%s\n", strvalue);
}
else if(Strcmp(code, "moduleimports") == 0) {
Delete(module_imports);
module_imports = Copy(strvalue);
}
else if(Strcmp(code, "moduleinterfaces") == 0) {
Delete(module_interfaces);
module_interfaces = Copy(strvalue);
} else {
Printf(stderr,"%s : Line %d. Unrecognized pragma.\n", input_file, line_number);
}
Delete(strvalue);
}
}
return Language::pragmaDirective(n);
}
/* -----------------------------------------------------------------------------
* emitProxyClassDefAndCPPCasts()
* ----------------------------------------------------------------------------- */
void emitProxyClassDefAndCPPCasts(Node *n) {
String *c_classname = SwigType_namestr(Getattr(n,"name"));
String *c_baseclass = NULL;
String *baseclass = NULL;
String *c_baseclassname = NULL;
String *typemap_lookup_type = Getattr(n,"classtypeobj");
/* Deal with inheritance */
List *baselist = Getattr(n,"bases");
if (baselist) {
Iterator base = First(baselist);
while(base.item && GetFlag(base.item,"feature:ignore")) {
base = Next(base);
}
if (base.item) {
c_baseclassname = Getattr(base.item,"name");
baseclass = Copy(getProxyName(c_baseclassname));
if (baseclass)
c_baseclass = SwigType_namestr(Getattr(base.item,"name"));
base = Next(base);
/* Warn about multiple inheritance for additional base class(es) */
while (base.item) {
if (GetFlag(base.item,"feature:ignore")) {
base = Next(base);
continue;
}
String *proxyclassname = SwigType_str(Getattr(n,"classtypeobj"),0);
String *baseclassname = SwigType_str(Getattr(base.item,"name"),0);
Swig_warning(WARN_CSHARP_MULTIPLE_INHERITANCE, input_file, line_number,
"Warning for %s proxy: Base %s ignored. Multiple inheritance is not supported in C#.\n", proxyclassname, baseclassname);
base = Next(base);
}
}
}
bool derived = baseclass && getProxyName(c_baseclassname);
if (!baseclass)
baseclass = NewString("");
// Inheritance from pure C# classes
const String *pure_baseclass = typemapLookup("csbase", typemap_lookup_type, WARN_NONE);
if (Len(pure_baseclass) > 0 && Len(baseclass) > 0) {
Swig_warning(WARN_CSHARP_MULTIPLE_INHERITANCE, input_file, line_number,
"Warning for %s proxy: Base %s ignored. Multiple inheritance is not supported in C#.\n", typemap_lookup_type, pure_baseclass);
pure_baseclass = empty_string;
}
// Pure C# interfaces
const String *pure_interfaces = typemapLookup(derived ? "csinterfaces_derived" : "csinterfaces", typemap_lookup_type, WARN_NONE);
// Start writing the proxy class
Printv(proxy_class_def,
typemapLookup("csimports", typemap_lookup_type, WARN_NONE), // Import statements
"\n",
NIL);
// Class attributes
const String *csattributes = typemapLookup("csattributes", typemap_lookup_type, WARN_NONE);
if (csattributes && *Char(csattributes))
Printf(proxy_class_def, "%s\n", csattributes);
Printv(proxy_class_def,
typemapLookup("csclassmodifiers", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers
" $csclassname", // Class name and base class
(derived || *Char(pure_baseclass) || *Char(pure_interfaces)) ?
" : " :
"",
baseclass, // Note only one of these base classes should ever be set as multiple inheritance is not permissible
pure_baseclass,
((derived || *Char(pure_baseclass)) && *Char(pure_interfaces)) ? // Interfaces
", " :
"",
pure_interfaces,
" {",
derived ?
typemapLookup("csbody_derived", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CSBODY_UNDEF) : // main body of class
typemapLookup("csbody", typemap_lookup_type, WARN_CSHARP_TYPEMAP_CSBODY_UNDEF), // main body of class
NIL);
// C++ destructor is wrapped by the Dispose method
// Note that the method name is specified in a typemap attribute called methodname
String *destruct = NewString("");
const String *tm = NULL;
Node *attributes = NewHash();
String *destruct_methodname = NULL;
if (derived) {
tm = typemapLookup("csdestruct_derived", typemap_lookup_type, WARN_NONE, attributes);
destruct_methodname = Getattr(attributes, "tmap:csdestruct_derived:methodname");
} else {
tm = typemapLookup("csdestruct", typemap_lookup_type, WARN_NONE, attributes);
destruct_methodname = Getattr(attributes, "tmap:csdestruct:methodname");
}
if (!destruct_methodname) {
Swig_error(input_file, line_number,
"No methodname attribute defined in csdestruct%s typemap for %s\n", (derived ? "_derived" : ""), proxy_class_name);
}
// Emit the Finalize and Dispose methods
if (tm) {
// Finalize method
if (*Char(destructor_call)) {
Printv(proxy_class_def,
typemapLookup("csfinalize", typemap_lookup_type, WARN_NONE),
NIL);
}
// Dispose method
Printv(destruct, tm, NIL);
if (*Char(destructor_call))
Replaceall(destruct, "$imcall", destructor_call);
else
Replaceall(destruct, "$imcall", "throw new MethodAccessException(\"C++ destructor does not have public access\")");
if (*Char(destruct))
Printv(proxy_class_def, "\n public ", derived ? "override" : "virtual", " void ", destruct_methodname, "() ", destruct, "\n", NIL);
}
Delete(attributes);
Delete(destruct);
// Emit extra user code
Printv(proxy_class_def,
typemapLookup("cscode", typemap_lookup_type, WARN_NONE), // extra C# code
"\n",
NIL);
// Substitute various strings into the above template
Replaceall(proxy_class_code, "$csclassname", proxy_class_name);
Replaceall(proxy_class_def, "$csclassname", proxy_class_name);
Replaceall(proxy_class_def, "$module", module_class_name);
Replaceall(proxy_class_code, "$module", module_class_name);
Replaceall(proxy_class_def, "$imclassname", imclass_name);
Replaceall(proxy_class_code, "$imclassname", imclass_name);
Replaceall(proxy_class_def, "$dllimport", dllimport);
Replaceall(proxy_class_code, "$dllimport", dllimport);
// Add code to do C++ casting to base class (only for classes in an inheritance hierarchy)
if(derived){
Printv(imclass_cppcasts_code,"\n [DllImport(\"", dllimport, "\", EntryPoint=\"CSharp_", proxy_class_name ,"Upcast", "\")]\n", NIL);
Printf(imclass_cppcasts_code," public static extern IntPtr $csclassnameUpcast(IntPtr objectRef);\n");
Replaceall(imclass_cppcasts_code, "$csclassname", proxy_class_name);
Printv(upcasts_code,
"SWIGEXPORT $cbaseclass * SWIGSTDCALL CSharp_$imclazznameUpcast",
"($cclass *objectRef) {\n",
" return ($cbaseclass *)objectRef;\n"
"}\n",
"\n",
NIL);
Replaceall(upcasts_code, "$cbaseclass", c_baseclass);
Replaceall(upcasts_code, "$imclazzname", proxy_class_name);
Replaceall(upcasts_code, "$cclass", c_classname);
}
Delete(baseclass);
}
/* ----------------------------------------------------------------------
* classHandler()
* ---------------------------------------------------------------------- */
virtual int classHandler(Node *n) {
File *f_proxy = NULL;
if (proxy_flag) {
proxy_class_name = NewString(Getattr(n,"sym:name"));
if (!addSymbol(proxy_class_name,n)) return SWIG_ERROR;
if (Cmp(proxy_class_name, imclass_name) == 0) {
Printf(stderr, "Class name cannot be equal to intermediary class name: %s\n", proxy_class_name);
SWIG_exit(EXIT_FAILURE);
}
if (Cmp(proxy_class_name, module_class_name) == 0) {
Printf(stderr, "Class name cannot be equal to module class name: %s\n", proxy_class_name);
SWIG_exit(EXIT_FAILURE);
}
String *filen = NewStringf("%s%s.cs", SWIG_output_directory(), proxy_class_name);
f_proxy = NewFile(filen,"w");
if(!f_proxy) {
FileErrorDisplay(filen);
SWIG_exit(EXIT_FAILURE);
}
Append(filenames_list, Copy(filen));
Delete(filen); filen = NULL;
// Start writing out the proxy class file
emitBanner(f_proxy);
if(Len(namespce) > 0)
Printf(f_proxy, "namespace %s {\n", namespce);
Clear(proxy_class_def);
Clear(proxy_class_code);
destructor_call = NewString("");
proxy_class_constants_code = NewString("");
}
Language::classHandler(n);
if (proxy_flag) {
emitProxyClassDefAndCPPCasts(n);
Replaceall(proxy_class_def, "$module", module_class_name);
Replaceall(proxy_class_code, "$module", module_class_name);
Replaceall(proxy_class_constants_code, "$module", module_class_name);
Replaceall(proxy_class_def, "$imclassname", imclass_name);
Replaceall(proxy_class_code, "$imclassname", imclass_name);
Replaceall(proxy_class_constants_code, "$imclassname", imclass_name);
Replaceall(proxy_class_def, "$dllimport", dllimport);
Replaceall(proxy_class_code, "$dllimport", dllimport);
Replaceall(proxy_class_constants_code, "$dllimport", dllimport);
Printv(f_proxy, proxy_class_def, proxy_class_code, NIL);
// Write out all the constants
if (Len(proxy_class_constants_code) != 0 )
Printv(f_proxy, proxy_class_constants_code, NIL);
Printf(f_proxy, "}\n");
Printf(f_proxy, Len(namespce) > 0 ? "\n}\n" : "");
Close(f_proxy);
f_proxy = NULL;
Delete(proxy_class_name); proxy_class_name = NULL;
Delete(destructor_call); destructor_call = NULL;
Delete(proxy_class_constants_code); proxy_class_constants_code = NULL;
}
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* memberfunctionHandler()
* ---------------------------------------------------------------------- */
virtual int memberfunctionHandler(Node *n) {
Language::memberfunctionHandler(n);
if (proxy_flag) {
String *overloaded_name = getOverloadedName(n);
String *intermediary_function_name = Swig_name_member(proxy_class_name, overloaded_name);
Setattr(n,"proxyfuncname", Getattr(n, "sym:name"));
Setattr(n,"imfuncname", intermediary_function_name);
proxyClassFunctionHandler(n);
Delete(overloaded_name);
}
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* staticmemberfunctionHandler()
* ---------------------------------------------------------------------- */
virtual int staticmemberfunctionHandler(Node *n) {
static_flag = true;
Language::staticmemberfunctionHandler(n);
if (proxy_flag) {
String *overloaded_name = getOverloadedName(n);
String *intermediary_function_name = Swig_name_member(proxy_class_name, overloaded_name);
Setattr(n,"proxyfuncname", Getattr(n,"sym:name"));
Setattr(n,"imfuncname", intermediary_function_name);
proxyClassFunctionHandler(n);
Delete(overloaded_name);
}
static_flag = false;
return SWIG_OK;
}
/* -----------------------------------------------------------------------------
* proxyClassFunctionHandler()
*
* Function called for creating a C# wrapper function around a c++ function in the
* proxy class. Used for both static and non-static C++ class functions.
* C++ class static functions map to C# static functions.
* Two extra attributes in the Node must be available. These are "proxyfuncname" -
* the name of the C# class proxy function, which in turn will call "imfuncname" -
* the intermediary (PInvoke) function name in the intermediary class.
* ----------------------------------------------------------------------------- */
void proxyClassFunctionHandler(Node *n) {
SwigType *t = Getattr(n,"type");
ParmList *l = Getattr(n,"parms");
String *intermediary_function_name = Getattr(n,"imfuncname");
String *proxy_function_name = Getattr(n,"proxyfuncname");
String *tm;
Parm *p;
Parm *last_parm = 0;
int i;
String *imcall = NewString("");
String *return_type = NewString("");
String *function_code = NewString("");
bool setter_flag = false;
if(!proxy_flag) return;
// Wrappers not wanted for some methods where the parameters cannot be overloaded in C#
if (Getattr(n, "overload:ignore")) return;
if (l) {
if (SwigType_type(Getattr(l,"type")) == T_VOID) {
l = nextSibling(l);
}
}
/* Attach the non-standard typemaps to the parameter list */
Swig_typemap_attach_parms("in", l, NULL);
Swig_typemap_attach_parms("cstype", l, NULL);
Swig_typemap_attach_parms("csin", l, NULL);
/* Get return types */
if ((tm = Swig_typemap_lookup_new("cstype",n,"",0))) {
// Note that in the case of polymorphic (covariant) return types, the method's return type is changed to be the base of the C++ return type
SwigType *covariant = Getattr(n,"covariant");
String *cstypeout = Getattr(n,"tmap:cstype:out"); // the type in the cstype typemap's out attribute overrides the type in the typemap
if (cstypeout)
tm = cstypeout;
substituteClassname(covariant ? covariant : t, tm);
Printf(return_type, "%s", tm);
if (covariant)
Swig_warning(WARN_CSHARP_COVARIANT_RET, input_file, line_number,
"Covariant return types not supported in C#. Proxy method will return %s.\n", SwigType_str(covariant,0));
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF, input_file, line_number,
"No cstype typemap defined for %s\n", SwigType_str(t,0));
}
if(proxy_flag && wrapping_member_flag && !enum_constant_flag) {
// Properties
setter_flag = (Cmp(Getattr(n, "sym:name"), Swig_name_set(Swig_name_member(proxy_class_name, variable_name))) == 0);
}
/* Start generating the proxy function */
const String *outattributes = Getattr(n,"tmap:cstype:outattributes");
if (outattributes)
Printf(function_code, " %s\n", outattributes);
const String *csattributes = Getattr(n,"feature:cs:attributes");
if (csattributes)
Printf(function_code, " %s\n", csattributes);
const String *methodmods = Getattr(n,"feature:cs:methodmodifiers");
if (methodmods) {
Printf(function_code, " %s ", methodmods);
} else {
methodmods = (!is_public(n) ? protected_string : public_string);
Printf(function_code, " %s ", methodmods);
if (Getattr(n,"override"))
Printf(function_code, "override ");
else if (checkAttribute(n, "storage", "virtual"))
Printf(function_code, "virtual ");
if (Getattr(n, "hides"))
Printf(function_code, "new ");
}
if (static_flag)
Printf(function_code, "static ");
Printf(function_code, "%s %s(", return_type, proxy_function_name);
Printv(imcall, imclass_name, ".", intermediary_function_name, "(", NIL);
if (!static_flag)
Printf(imcall, "swigCPtr");
emit_mark_varargs(l);
int gencomma = !static_flag;
/* Output each parameter */
for (i = 0, p=l; p; i++) {
/* Ignored varargs */
if (checkAttribute(p,"varargs:ignore","1")) {
p = nextSibling(p);
continue;
}
/* Ignored parameters */
if (checkAttribute(p,"tmap:in:numinputs","0")) {
p = Getattr(p,"tmap:in:next");
continue;
}
/* Ignore the 'this' argument for variable wrappers */
if (!(variable_wrapper_flag && i==0))
{
SwigType *pt = Getattr(p,"type");
String *param_type = NewString("");
last_parm = p;
/* Get the C# parameter type */
if ((tm = Getattr(p,"tmap:cstype"))) {
substituteClassname(pt, tm);
String *inattributes = Getattr(p,"tmap:cstype:inattributes");
Printf(param_type, "%s%s", inattributes ? (const String *)inattributes : empty_string, tm);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF, input_file, line_number,
"No cstype typemap defined for %s\n", SwigType_str(pt,0));
}
if (gencomma)
Printf(imcall, ", ");
String *arg = makeParameterName(n, p, i, setter_flag);
// Use typemaps to transform type used in C# wrapper function (in proxy class) to type used in PInvoke function (in intermediary class)
if ((tm = Getattr(p,"tmap:csin"))) {
substituteClassname(pt, tm);
Replaceall(tm, "$csinput", arg);
Printv(imcall, tm, NIL);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSIN_UNDEF, input_file, line_number,
"No csin typemap defined for %s\n", SwigType_str(pt,0));
}
/* Add parameter to proxy function */
if (gencomma >= 2)
Printf(function_code, ", ");
gencomma = 2;
Printf(function_code, "%s %s", param_type, arg);
Delete(arg);
Delete(param_type);
}
p = Getattr(p,"tmap:in:next");
}
Printf(imcall, ")");
Printf(function_code, ")");
// Transform return type used in PInvoke function (in intermediary class) to type used in C# wrapper function (in proxy class)
if ((tm = Swig_typemap_lookup_new("csout",n,"",0))) {
if (GetFlag(n,"feature:new"))
Replaceall(tm,"$owner","true");
else
Replaceall(tm,"$owner","false");
substituteClassname(t, tm);
Replaceall(tm, "$imcall", imcall);
excodeSubstitute(n, tm, "csout", n);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number,
"No csout typemap defined for %s\n", SwigType_str(t,0));
}
if(proxy_flag && wrapping_member_flag && !enum_constant_flag) {
// Properties
if (generate_property_declaration_flag) { // Ensure the declaration is generated just once should the property contain both a set and get
// Get the C# variable type - obtained differently depending on whether a setter is required.
String *variable_type = return_type;
if (setter_flag) {
p = last_parm; // (last parameter is the only parameter for properties)
SwigType *pt = Getattr(p,"type");
if ((tm = Getattr(p,"tmap:cstype"))) {
substituteClassname(pt, tm);
variable_type = tm;
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number,
"No csvarin typemap defined for %s\n", SwigType_str(pt,0));
}
}
const String *csattributes = Getattr(n,"feature:cs:attributes");
if (csattributes)
Printf(proxy_class_code, " %s\n", csattributes);
const String *methodmods = Getattr(n,"feature:cs:methodmodifiers");
if (!methodmods)
methodmods = (!is_public(n) ? protected_string : public_string);
Printf(proxy_class_code, " %s %s%s %s {", methodmods, static_flag ? "static " : "", variable_type, variable_name);
}
generate_property_declaration_flag = false;
if(setter_flag) {
// Setter method
Swig_typemap_attach_parms("csvarin", l, NULL);
p = last_parm; // (last parameter is the only parameter for properties)
SwigType *pt = Getattr(p,"type");
if ((tm = Getattr(p,"tmap:csvarin"))) {
substituteClassname(pt, tm);
Replaceall(tm, "$imcall", imcall);
excodeSubstitute(n, tm, "csvarin", p);
Printf(proxy_class_code, "%s", tm);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number,
"No csvarin typemap defined for %s\n", SwigType_str(pt,0));
}
} else {
// Getter method
if ((tm = Swig_typemap_lookup_new("csvarout",n,"",0))) {
if (GetFlag(n,"feature:new"))
Replaceall(tm,"$owner","true");
else
Replaceall(tm,"$owner","false");
substituteClassname(t, tm);
Replaceall(tm, "$imcall", imcall);
excodeSubstitute(n, tm, "csvarout", n);
Printf(proxy_class_code, "%s", tm);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number,
"No csvarout typemap defined for %s\n", SwigType_str(t,0));
}
}
} else {
// Normal function call
Printf(function_code, " %s\n\n", tm ? (const String *)tm : empty_string);
Printv(proxy_class_code, function_code, NIL);
}
Delete(function_code);
Delete(return_type);
Delete(imcall);
}
/* ----------------------------------------------------------------------
* constructorHandler()
* ---------------------------------------------------------------------- */
virtual int constructorHandler(Node *n) {
ParmList *l = Getattr(n,"parms");
String *tm;
Parm *p;
int i;
String *function_code = NewString("");
Language::constructorHandler(n);
// Wrappers not wanted for some methods where the parameters cannot be overloaded in C#
if (Getattr(n, "overload:ignore"))
return SWIG_OK;
if(proxy_flag) {
String *overloaded_name = getOverloadedName(n);
String *mangled_overname = Swig_name_construct(overloaded_name);
String *imcall = NewString("");
const String *csattributes = Getattr(n,"feature:cs:attributes");
if (csattributes)
Printf(function_code, " %s\n", csattributes);
const String *methodmods = Getattr(n,"feature:cs:methodmodifiers");
methodmods = methodmods ? methodmods : (!is_public(n) ? protected_string : public_string);
Printf(function_code, " %s %s(", methodmods, proxy_class_name);
Printv(imcall, imclass_name, ".", mangled_overname, "(", NIL);
/* Attach the non-standard typemaps to the parameter list */
Swig_typemap_attach_parms("in", l, NULL);
Swig_typemap_attach_parms("cstype", l, NULL);
Swig_typemap_attach_parms("csin", l, NULL);
emit_mark_varargs(l);
int gencomma = 0;
/* Output each parameter */
for (i = 0, p=l; p; i++) {
/* Ignored varargs */
if (checkAttribute(p,"varargs:ignore","1")) {
p = nextSibling(p);
continue;
}
/* Ignored parameters */
if (checkAttribute(p,"tmap:in:numinputs","0")) {
p = Getattr(p,"tmap:in:next");
continue;
}
SwigType *pt = Getattr(p,"type");
String *param_type = NewString("");
/* Get the C# parameter type */
if ((tm = Getattr(p,"tmap:cstype"))) {
substituteClassname(pt, tm);
String *inattributes = Getattr(p,"tmap:cstype:inattributes");
Printf(param_type, "%s%s", inattributes ? (const String *)inattributes : empty_string, tm);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF, input_file, line_number,
"No cstype typemap defined for %s\n", SwigType_str(pt,0));
}
if (gencomma)
Printf(imcall, ", ");
String *arg = makeParameterName(n, p, i, false);
// Use typemaps to transform type used in C# wrapper function (in proxy class) to type used in PInvoke function (in intermediary class)
if ((tm = Getattr(p,"tmap:csin"))) {
substituteClassname(pt, tm);
Replaceall(tm, "$csinput", arg);
Printv(imcall, tm, NIL);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSIN_UNDEF, input_file, line_number,
"No csin typemap defined for %s\n", SwigType_str(pt,0));
}
/* Add parameter to proxy function */
if(gencomma)
Printf(function_code, ", ");
Printf(function_code, "%s %s", param_type, arg);
++gencomma;
Delete(arg);
Delete(param_type);
p = Getattr(p,"tmap:in:next");
}
Printf(imcall, ")");
Printf(function_code, ")");
Node *attributes = NewHash();
Printv(function_code, " ", typemapLookup("csconstruct", Getattr(n,"name"), WARN_CSHARP_TYPEMAP_CSCONSTRUCT_UNDEF, attributes), NIL);
Replaceall(function_code, "$imcall", imcall);
excodeSubstitute(n, function_code, "csconstruct", attributes);
Printv(proxy_class_code, function_code, "\n", NIL);
Delete(attributes);
Delete(overloaded_name);
Delete(imcall);
}
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* destructorHandler()
* ---------------------------------------------------------------------- */
virtual int destructorHandler(Node *n) {
Language::destructorHandler(n);
String *symname = Getattr(n,"sym:name");
if(proxy_flag) {
Printv(destructor_call, imclass_name, ".", Swig_name_destroy(symname), "(swigCPtr)", NIL);
}
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* membervariableHandler()
* ---------------------------------------------------------------------- */
virtual int membervariableHandler(Node *n) {
generate_property_declaration_flag = true;
variable_name = Getattr(n,"sym:name");
wrapping_member_flag = true;
variable_wrapper_flag = true;
Language::membervariableHandler(n);
wrapping_member_flag = false;
variable_wrapper_flag = false;
generate_property_declaration_flag = false;
Printf(proxy_class_code, "\n }\n\n");
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* staticmembervariableHandler()
* ---------------------------------------------------------------------- */
virtual int staticmembervariableHandler(Node *n) {
bool static_const_member_flag = (Getattr(n, "value") == 0);
generate_property_declaration_flag = true;
variable_name = Getattr(n,"sym:name");
wrapping_member_flag = true;
static_flag = true;
Language::staticmembervariableHandler(n);
wrapping_member_flag = false;
static_flag = false;
generate_property_declaration_flag = false;
if(static_const_member_flag)
Printf(proxy_class_code, "\n }\n\n");
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* memberconstantHandler()
* ---------------------------------------------------------------------- */
virtual int memberconstantHandler(Node *n) {
variable_name = Getattr(n,"sym:name");
wrapping_member_flag = true;
Language::memberconstantHandler(n);
wrapping_member_flag = false;
return SWIG_OK;
}
/* -----------------------------------------------------------------------------
* getOverloadedName()
* ----------------------------------------------------------------------------- */
String *getOverloadedName(Node *n) {
/* A C# HandleRef is used for all classes in the SWIG intermediary class.
* The intermediary class methods are thus mangled when overloaded to give
* a unique name. */
String *overloaded_name = NewStringf("%s", Getattr(n,"sym:name"));
if (Getattr(n,"sym:overloaded")) {
Printv(overloaded_name, Getattr(n,"sym:overname"), NIL);
}
return overloaded_name;
}
/* -----------------------------------------------------------------------------
* moduleClassFunctionHandler()
* ----------------------------------------------------------------------------- */
void moduleClassFunctionHandler(Node *n) {
SwigType *t = Getattr(n,"type");
ParmList *l = Getattr(n,"parms");
String *tm;
Parm *p;
Parm *last_parm = 0;
int i;
String *imcall = NewString("");
String *return_type = NewString("");
String *function_code = NewString("");
int num_arguments = 0;
int num_required = 0;
String *overloaded_name = getOverloadedName(n);
String *func_name = NULL;
bool setter_flag = false;
if (l) {
if (SwigType_type(Getattr(l,"type")) == T_VOID) {
l = nextSibling(l);
}
}
/* Attach the non-standard typemaps to the parameter list */
Swig_typemap_attach_parms("cstype", l, NULL);
Swig_typemap_attach_parms("csin", l, NULL);
/* Get return types */
if ((tm = Swig_typemap_lookup_new("cstype",n,"",0))) {
String *cstypeout = Getattr(n,"tmap:cstype:out"); // the type in the cstype typemap's out attribute overrides the type in the typemap
if (cstypeout)
tm = cstypeout;
substituteClassname(t, tm);
Printf(return_type, "%s", tm);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF, input_file, line_number,
"No cstype typemap defined for %s\n", SwigType_str(t,0));
}
/* Change function name for global variables */
if (proxy_flag && global_variable_flag) {
// Capitalize the first letter in the variable to create the getter/setter function name
func_name = NewString("");
setter_flag = (Cmp(Getattr(n,"sym:name"), Swig_name_set(variable_name)) == 0);
if(setter_flag)
Printf(func_name,"set");
else
Printf(func_name,"get");
Putc(toupper((int) *Char(variable_name)), func_name);
Printf(func_name, "%s", Char(variable_name)+1);
} else {
func_name = Copy(Getattr(n,"sym:name"));
}
/* Start generating the function */
const String *outattributes = Getattr(n,"tmap:cstype:outattributes");
if (outattributes)
Printf(function_code, " %s\n", outattributes);
const String *csattributes = Getattr(n,"feature:cs:attributes");
if (csattributes)
Printf(function_code, " %s\n", csattributes);
const String *methodmods = Getattr(n,"feature:cs:methodmodifiers");
methodmods = methodmods ? methodmods : (!is_public(n) ? protected_string : public_string);
Printf(function_code, " %s static %s %s(", methodmods, return_type, func_name);
Printv(imcall, imclass_name, ".", overloaded_name, "(", NIL);
/* Get number of required and total arguments */
num_arguments = emit_num_arguments(l);
num_required = emit_num_required(l);
int gencomma = 0;
/* Output each parameter */
for (i = 0, p=l; i < num_arguments; i++) {
/* Ignored parameters */
while (checkAttribute(p,"tmap:in:numinputs","0")) {
p = Getattr(p,"tmap:in:next");
}
SwigType *pt = Getattr(p,"type");
String *param_type = NewString("");
last_parm = p;
/* Get the C# parameter type */
if ((tm = Getattr(p,"tmap:cstype"))) {
substituteClassname(pt, tm);
String *inattributes = Getattr(p,"tmap:cstype:inattributes");
Printf(param_type, "%s%s", inattributes ? (const String *)inattributes : empty_string, tm);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF, input_file, line_number,
"No cstype typemap defined for %s\n", SwigType_str(pt,0));
}
if (gencomma)
Printf(imcall, ", ");
String *arg = makeParameterName(n, p, i, setter_flag);
// Use typemaps to transform type used in C# wrapper function (in proxy class) to type used in PInvoke function (in intermediary class)
if ((tm = Getattr(p,"tmap:csin"))) {
substituteClassname(pt, tm);
Replaceall(tm, "$csinput", arg);
Printv(imcall, tm, NIL);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSIN_UNDEF, input_file, line_number,
"No csin typemap defined for %s\n", SwigType_str(pt,0));
}
/* Add parameter to module class function */
if (gencomma >= 2)
Printf(function_code, ", ");
gencomma = 2;
Printf(function_code, "%s %s", param_type, arg);
p = Getattr(p,"tmap:in:next");
Delete(arg);
Delete(param_type);
}
Printf(imcall, ")");
Printf(function_code, ")");
// Transform return type used in PInvoke function (in intermediary class) to type used in C# wrapper function (in module class)
if ((tm = Swig_typemap_lookup_new("csout",n,"",0))) {
if (GetFlag(n,"feature:new"))
Replaceall(tm,"$owner","true");
else
Replaceall(tm,"$owner","false");
substituteClassname(t, tm);
Replaceall(tm, "$imcall", imcall);
excodeSubstitute(n, tm, "csout", n);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number,
"No csout typemap defined for %s\n", SwigType_str(t,0));
}
if (proxy_flag && global_variable_flag) {
// Properties
if (generate_property_declaration_flag) { // Ensure the declaration is generated just once should the property contain both a set and get
// Get the C# variable type - obtained differently depending on whether a setter is required.
String *variable_type = return_type;
if (setter_flag) {
p = last_parm; // (last parameter is the only parameter for properties)
SwigType *pt = Getattr(p,"type");
if ((tm = Getattr(p,"tmap:cstype"))) {
substituteClassname(pt, tm);
variable_type = tm;
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number,
"No csvarin typemap defined for %s\n", SwigType_str(pt,0));
}
}
const String *csattributes = Getattr(n,"feature:cs:attributes");
if (csattributes)
Printf(module_class_code, " %s\n", csattributes);
const String *methodmods = Getattr(n,"feature:cs:methodmodifiers");
if (!methodmods)
methodmods = (!is_public(n) ? protected_string : public_string);
Printf(module_class_code, " %s static %s %s {", methodmods, variable_type, variable_name);
}
generate_property_declaration_flag = false;
if(setter_flag) {
// Setter method
Swig_typemap_attach_parms("csvarin", l, NULL);
p = last_parm; // (last parameter is the only parameter for properties)
SwigType *pt = Getattr(p,"type");
if ((tm = Getattr(p,"tmap:csvarin"))) {
substituteClassname(pt, tm);
Replaceall(tm, "$imcall", imcall);
excodeSubstitute(n, tm, "csvarin", p);
Printf(module_class_code, "%s", tm);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number,
"No csvarin typemap defined for %s\n", SwigType_str(pt,0));
}
} else {
// Getter method
if ((tm = Swig_typemap_lookup_new("csvarout",n,"",0))) {
if (GetFlag(n,"feature:new"))
Replaceall(tm,"$owner","true");
else
Replaceall(tm,"$owner","false");
substituteClassname(t, tm);
Replaceall(tm, "$imcall", imcall);
excodeSubstitute(n, tm, "csvarout", n);
Printf(module_class_code, "%s", tm);
} else {
Swig_warning(WARN_CSHARP_TYPEMAP_CSOUT_UNDEF, input_file, line_number,
"No csvarout typemap defined for %s\n", SwigType_str(t,0));
}
}
} else {
// Normal function call
Printf(function_code, " %s\n\n", tm ? (const String *)tm : empty_string);
Printv(module_class_code, function_code, NIL);
}
Delete(function_code);
Delete(return_type);
Delete(imcall);
Delete(func_name);
}
/*----------------------------------------------------------------------
* decodeEnumFeature()
* Decode the possible enum features, which are one of:
* %csenum(simple)
* %csenum(typeunsafe) - default
* %csenum(typesafe)
* %csenum(proper)
*--------------------------------------------------------------------*/
EnumFeature decodeEnumFeature(Node *n) {
EnumFeature enum_feature = TypeunsafeEnum;
String *feature = Getattr(n,"feature:cs:enum");
if (feature) {
if (Cmp(feature, "simple") == 0)
enum_feature = SimpleEnum;
else if (Cmp(feature, "typesafe") == 0)
enum_feature = TypesafeEnum;
else if (Cmp(feature, "proper") == 0)
enum_feature = ProperEnum;
}
return enum_feature;
}
/* -----------------------------------------------------------------------
* enumValue()
* This method will return a string with an enum value to use in C# generated
* code. If the %csconst feature is not used, the string will contain the intermediary
* class call to obtain the enum value. The intermediary class and PINVOKE methods to obtain
* the enum value will be generated. Otherwise the C/C++ enum value will be used if there
* is one and hopefully it will compile as C# code - e.g. 20 as in: enum E{e=20};
* The %csconstvalue feature overrides all other ways to generate the constant value.
* The caller must delete memory allocated for the returned string.
* ------------------------------------------------------------------------ */
String *enumValue(Node *n) {
String *symname = Getattr(n,"sym:name");
// Check for the %csconstvalue feature
String *value = Getattr(n,"feature:cs:constvalue");
if (!value) {
// The %csconst feature determines how the constant value is obtained
int const_feature_flag = GetFlag(n,"feature:cs:const");
if (const_feature_flag) {
// Use the C syntax to make a true C# constant and hope that it compiles as C# code
value = Getattr(n,"enumvalue") ? Copy(Getattr(n,"enumvalue")) : Copy(Getattr(n,"enumvalueex"));
} else {
// Get the enumvalue from a PINVOKE call
if (!getCurrentClass() || !cparse_cplusplus) {
// Strange hack to change the name
Setattr(n,"name",Getattr(n,"value")); /* for wrapping of enums in a namespace when emit_action is used */
constantWrapper(n);
value = NewStringf("%s.%s()", imclass_name, Swig_name_get(symname));
} else {
memberconstantHandler(n);
value = NewStringf("%s.%s()", imclass_name, Swig_name_get(Swig_name_member(proxy_class_name, symname)));
}
}
}
return value;
}
/* -----------------------------------------------------------------------------
* getEnumName()
* ----------------------------------------------------------------------------- */
String *getEnumName(SwigType *t) {
Node *enum_name = NULL;
Node *n = enumLookup(t);
if (n) {
String *symname = Getattr(n,"sym:name");
if (symname) {
// Add in class scope when referencing enum if not a global enum
String *scopename_prefix = Swig_scopename_prefix(Getattr(n,"name"));
String *proxyname = 0;
if (scopename_prefix) {
proxyname = getProxyName(scopename_prefix);
}
if (proxyname)
enum_name = NewStringf("%s.%s", proxyname, symname);
else
enum_name = NewStringf("%s", symname);
Delete(scopename_prefix);
}
}
return enum_name;
}
/* -----------------------------------------------------------------------------
* substituteClassname()
*
* Substitute $csclassname with the proxy class name for classes/structs/unions that SWIG knows about.
* Also substitutes enums with enum name.
* Otherwise use the $descriptor name for the C# class name. Note that the $&csclassname substitution
* is the same as a $&descriptor substitution, ie one pointer added to descriptor name.
* Inputs:
* pt - parameter type
* tm - cstype typemap
* Outputs:
* tm - cstype typemap with $csclassname substitution
* Return:
* substitution_performed - flag indicating if a substitution was performed
* ----------------------------------------------------------------------------- */
bool substituteClassname(SwigType *pt, String *tm) {
bool substitution_performed = false;
SwigType *type = Copy(SwigType_typedef_resolve_all(pt));
SwigType *strippedtype = SwigType_strip_qualifiers(type);
if (Strstr(tm, "$csclassname")) {
SwigType *classnametype = Copy(strippedtype);
substituteClassnameSpecialVariable(classnametype, tm, "$csclassname");
substitution_performed = true;
Delete(classnametype);
}
if (Strstr(tm, "$*csclassname")) {
SwigType *classnametype = Copy(strippedtype);
Delete(SwigType_pop(classnametype));
substituteClassnameSpecialVariable(classnametype, tm, "$*csclassname");
substitution_performed = true;
Delete(classnametype);
}
if (Strstr(tm, "$&csclassname")) {
SwigType *classnametype = Copy(strippedtype);
SwigType_add_pointer(classnametype);
substituteClassnameSpecialVariable(classnametype, tm, "$&csclassname");
substitution_performed = true;
Delete(classnametype);
}
Delete(strippedtype);
Delete(type);
return substitution_performed;
}
/* -----------------------------------------------------------------------------
* substituteClassnameSpecialVariable()
* ----------------------------------------------------------------------------- */
void substituteClassnameSpecialVariable(SwigType *classnametype, String *tm, const char *classnamespecialvariable) {
if (SwigType_isenum(classnametype)) {
String *enumname = getEnumName(classnametype);
if (enumname)
Replaceall(tm, classnamespecialvariable, enumname);
else
Replaceall(tm, classnamespecialvariable, NewStringf("int"));
} else {
String *classname = getProxyName(classnametype);
if (classname) {
Replaceall(tm, classnamespecialvariable, classname); // getProxyName() works for pointers to classes too
}
else { // use $descriptor if SWIG does not know anything about this type. Note that any typedefs are resolved.
String *descriptor = NewStringf("SWIGTYPE%s", SwigType_manglestr(classnametype));
Replaceall(tm, classnamespecialvariable, descriptor);
// Add to hash table so that the type wrapper classes can be created later
Setattr(swig_types_hash, descriptor, classnametype);
Delete(descriptor);
}
}
}
/* -----------------------------------------------------------------------------
* makeParameterName()
*
* Inputs:
* n - Node
* p - parameter node
* arg_num - parameter argument number
* setter - set this flag when wrapping member variables
* Return:
* arg - a unique parameter name
* ----------------------------------------------------------------------------- */
String *makeParameterName(Node *n, Parm *p, int arg_num, bool setter) {
String *arg = 0;
String *pn = Getattr(p,"name");
if (setter) {
// Note that in C# property setter names must always be called 'value'
arg = NewString("value");
} else {
// Use C parameter name unless it is a duplicate or an empty parameter name
int count = 0;
ParmList *plist = Getattr(n,"parms");
while (plist) {
if ((Cmp(pn, Getattr(plist,"name")) == 0))
count++;
plist = nextSibling(plist);
}
String *wrn = pn ? Swig_name_warning(p,0,pn,0) : 0;
arg = (!pn || (count > 1) || wrn) ? NewStringf("arg%d",arg_num) : Copy(pn);
}
return arg;
}
/* -----------------------------------------------------------------------------
* emitTypeWrapperClass()
* ----------------------------------------------------------------------------- */
void emitTypeWrapperClass(String *classname, SwigType *type) {
String *swigtype = NewString("");
String *filen = NewStringf("%s%s.cs", SWIG_output_directory(), classname);
File *f_swigtype = NewFile(filen,"w");
if(!f_swigtype) {
FileErrorDisplay(filen);
SWIG_exit(EXIT_FAILURE);
}
Append(filenames_list, Copy(filen));
Delete(filen); filen = NULL;
// Start writing out the type wrapper class file
emitBanner(f_swigtype);
if(Len(namespce) > 0)
Printf(f_swigtype, "namespace %s {\n", namespce);
// Pure C# baseclass and interfaces
const String *pure_baseclass = typemapLookup("csbase", type, WARN_NONE);
const String *pure_interfaces = typemapLookup("csinterfaces", type, WARN_NONE);
// Emit the class
Printv(swigtype,
typemapLookup("csimports", type, WARN_NONE), // Import statements
"\n",
NIL);
// Class attributes
const String *csattributes = typemapLookup("csattributes", type, WARN_NONE);
if (csattributes && *Char(csattributes))
Printf(swigtype, "%s\n", csattributes);
Printv(swigtype,
typemapLookup("csclassmodifiers", type, WARN_CSHARP_TYPEMAP_CLASSMOD_UNDEF), // Class modifiers
" $csclassname", // Class name and base class
(*Char(pure_baseclass) || *Char(pure_interfaces)) ?
" : " :
"",
pure_baseclass,
((*Char(pure_baseclass)) && *Char(pure_interfaces)) ? // Interfaces
", " :
"",
pure_interfaces,
" {",
typemapLookup("csbody", type, WARN_CSHARP_TYPEMAP_CSBODY_UNDEF), // main body of class
typemapLookup("cscode", type, WARN_NONE), // extra C# code
"}\n",
Len(namespce) > 0 ?
"\n}\n" :
"",
NIL);
Replaceall(swigtype, "$csclassname", classname);
Replaceall(swigtype, "$module", module_class_name);
Replaceall(swigtype, "$imclassname", imclass_name);
Replaceall(swigtype, "$dllimport", dllimport);
Printv(f_swigtype, swigtype, NIL);
Close(f_swigtype);
Delete(swigtype);
}
/* -----------------------------------------------------------------------------
* typemapLookup()
* ----------------------------------------------------------------------------- */
const String *typemapLookup(const String *op, String *type, int warning, Node *typemap_attributes=NULL) {
String *tm = NULL;
const String *code = NULL;
if((tm = Swig_typemap_search(op, type, NULL, NULL))) {
code = Getattr(tm,"code");
if (typemap_attributes)
Swig_typemap_attach_kwargs(tm,op,typemap_attributes);
}
if (!code) {
code = empty_string;
if (warning != WARN_NONE)
Swig_warning(warning, input_file, line_number, "No %s typemap defined for %s\n", op, type);
}
return code ? code : empty_string;
}
/* -----------------------------------------------------------------------------
* canThrow()
* Determine whether the code in the typemap can throw a C# exception.
* If so, note it for later when excodeSubstitute() is called.
* ----------------------------------------------------------------------------- */
void canThrow(Node *n, const String *typemap, Node *parameter) {
String *canthrow_attribute = NewStringf("tmap:%s:canthrow", typemap);
String *canthrow = Getattr(parameter,canthrow_attribute);
if (canthrow)
Setattr(n,"csharp:canthrow", "1");
Delete(canthrow_attribute);
}
/* -----------------------------------------------------------------------------
* excodeSubstitute()
* If a method can throw a C# exception, additional exception code is added to
* check for the pending exception so that it can then throw the exception. The
* $excode special variable is replaced by the exception code in the excode
* typemap attribute.
* ----------------------------------------------------------------------------- */
void excodeSubstitute(Node *n, String *code, const String *typemap, Node *parameter) {
String *excode_attribute = NewStringf("tmap:%s:excode", typemap);
String *excode = Getattr(parameter, excode_attribute);
if (Getattr(n,"csharp:canthrow")) {
int count = Replaceall(code, "$excode", excode);
if (count < 1 || !excode) {
Swig_warning(WARN_CSHARP_EXCODE, input_file, line_number,
"C# exception may not be thrown - no $excode or excode attribute in '%s' typemap.\n", typemap);
}
} else {
Replaceall(code, "$excode", empty_string);
}
Delete(excode_attribute);
}
}; /* class CSHARP */
/* -----------------------------------------------------------------------------
* swig_csharp() - Instantiate module
* ----------------------------------------------------------------------------- */
static Language * new_swig_csharp() {
return new CSHARP();
}
extern "C" Language * swig_csharp(void) {
return new_swig_csharp();
}
/* -----------------------------------------------------------------------------
* Static member variables
* ----------------------------------------------------------------------------- */
const char *CSHARP::usage = (char*)"\
C# Options (available with -csharp)\n\
-dllimport <dl> - Override DllImport attribute name to <dl>\n\
-namespace <nm> - Generate wrappers into C# namespace <nm>\n\
-noproxy - Generate the low-level functional interface instead\n\
of proxy classes\n\
-oldvarnames - old intermediary method names for variable wrappers\n\
\n";
|