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
|
(*
* Copyright (C)2005-2013 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*)
open Ast
open Type
open Common
type method_name = {
mutable mpath : path;
mutable mname : string;
}
type inline_method = {
iname : string;
iindex : int;
iexpr : texpr;
ihasthis : bool;
iin_block : bool;
iarguments : string list;
ilocals : (string,string) PMap.t;
iinv_locals : (string,string) PMap.t;
}
type context = {
com : Common.context;
ch : out_channel;
buf : Buffer.t;
path : path;
stack : Codegen.stack_context;
mutable nested_loops : int;
mutable inline_index : int;
mutable curclass : tclass;
mutable curmethod : string;
mutable tabs : string;
mutable in_value : string option;
mutable in_loop : bool;
mutable in_block : bool;
mutable in_instance_method : bool;
mutable imports : (string,string list list) Hashtbl.t;
mutable extern_required_paths : (string list * string) list;
mutable extern_classes_with_init : path list;
mutable locals : (string,string) PMap.t;
mutable inv_locals : (string,string) PMap.t;
mutable local_types : t list;
mutable inits : texpr list;
mutable constructor_block : bool;
mutable all_dynamic_methods: method_name list;
mutable dynamic_methods: tclass_field list;
mutable is_call : bool;
mutable cwd : string;
mutable inline_methods : inline_method list;
mutable lib_path : string;
}
let join_class_path path separator =
let result = match fst path, snd path with
| [], s -> s
| el, s -> String.concat separator el ^ separator ^ s in
if (String.contains result '+') then begin
let idx = String.index result '+' in
(String.sub result 0 idx) ^ (String.sub result (idx+1) ((String.length result) - idx -1 ) )
end else
result;;
(* Get a string to represent a type.
The "suffix" will be nothing or "_obj", depending if we want the name of the
pointer class or the pointee (_obj class *)
let rec class_string klass suffix params =
(match klass.cl_path with
(* Array class *)
| ([],"Array") -> (snd klass.cl_path) ^ suffix ^ "<" ^ (String.concat ","
(List.map type_string params) ) ^ " >"
| _ when (match klass.cl_kind with KTypeParameter _ -> true | _ -> false) -> "Dynamic"
| ([],"#Int") -> "/* # */int"
| (["haxe";"io"],"Unsigned_char__") -> "unsigned char"
| ([],"Class") -> "Class"
| ([],"Null") -> (match params with
| [t] ->
(match follow t with
| TInst ({ cl_path = [],"Int" },_)
| TInst ({ cl_path = [],"Float" },_)
| TEnum ({ e_path = [],"Bool" },_) -> "Dynamic"
| _ -> "/*NULL*/" ^ (type_string t) )
| _ -> assert false);
(* Normal class *)
| _ -> (join_class_path klass.cl_path "::") ^ suffix
)
and type_string_suff suffix haxe_type =
(match haxe_type with
| TMono r -> (match !r with None -> "Dynamic" | Some t -> type_string_suff suffix t)
| TAbstract ({ a_path = [],"Int" },[]) -> "int"
| TAbstract ({ a_path = [],"Float" },[]) -> "double"
| TAbstract ({ a_path = [],"Bool" },[]) -> "bool"
| TAbstract ({ a_path = [],"Void" },[]) -> "Void"
| TEnum (enum,params) -> (join_class_path enum.e_path "::") ^ suffix
| TInst (klass,params) -> (class_string klass suffix params)
| TAbstract (abs,params) -> (join_class_path abs.a_path "::") ^ suffix
| TType (type_def,params) ->
(match type_def.t_path with
| [] , "Null" ->
(match params with
| [t] ->
(match follow t with
| TInst ({ cl_path = [],"Int" },_)
| TInst ({ cl_path = [],"Float" },_)
| TEnum ({ e_path = [],"Bool" },_) -> "Dynamic"
| _ -> type_string_suff suffix t)
| _ -> assert false);
| [] , "Array" ->
(match params with
| [t] -> "Array<" ^ (type_string (follow t) ) ^ " >"
| _ -> assert false)
| _ -> type_string_suff suffix (apply_params type_def.t_params params type_def.t_type)
)
| TFun (args,haxe_type) -> "Dynamic"
| TAnon anon -> "Dynamic"
| TDynamic haxe_type -> "Dynamic"
| TLazy func -> type_string_suff suffix ((!func)())
)
and type_string haxe_type =
type_string_suff "" haxe_type;;
let debug_expression expression type_too =
"/* " ^ Type.s_expr_kind expression ^ (if (type_too) then " = " ^ (type_string (follow expression.etype)) else "") ^ " */";;
let rec register_extern_required_path ctx path =
if (List.exists(fun p -> p = path) ctx.extern_classes_with_init) && not (List.exists(fun p -> p = path) ctx.extern_required_paths) then
ctx.extern_required_paths <- path :: ctx.extern_required_paths
let s_expr_expr = Type.s_expr_kind
let s_expr_name e =
s_type (print_context()) (follow e.etype)
let s_type_name t =
s_type (print_context()) t
and start_with s test =
let len = String.length test in
(String.length s > len && String.sub s 0 len = test)
let rec is_uncertain_type t =
match follow t with
| TInst (c, _) -> c.cl_interface
| TMono _ -> true
| TAnon a ->
(match !(a.a_status) with
| Statics _
| EnumStatics _ -> false
| _ -> true)
| TDynamic _ -> true
| _ -> false
let is_uncertain_expr e =
is_uncertain_type e.etype
let rec is_anonym_type t =
match follow t with
| TAnon a ->
(match !(a.a_status) with
| Statics _
| EnumStatics _ -> false
| _ -> true)
| TDynamic _ -> true
| _ -> false
let is_anonym_expr e = is_anonym_type e.etype
let rec is_unknown_type t =
match follow t with
| TMono r ->
(match !r with
| None -> true
| Some t -> is_unknown_type t)
| _ -> false
let is_unknown_expr e = is_unknown_type e.etype
let rec is_string_type t =
match follow t with
| TInst ({cl_path = ([], "String")}, _) -> true
| TAnon a ->
(match !(a.a_status) with
| Statics ({cl_path = ([], "String")}) -> true
| _ -> false)
| TAbstract (a,pl) -> is_string_type (Abstract.get_underlying_type a pl)
| _ -> false
let is_string_expr e = is_string_type e.etype
let to_string ctx e =
let v = alloc_var "__call__" t_dynamic in
let f = mk (TLocal v) t_dynamic e.epos in
mk (TCall (f, [ Codegen.string ctx.com "_hx_string_rec" e.epos; e; Codegen.string ctx.com "" e.epos])) ctx.com.basic.tstring e.epos
let as_string_expr ctx e =
match e.eexpr with
| TConst (TNull) ->
to_string ctx e
| _ when not (is_string_expr e) ->
to_string ctx e
| _ -> e
(* for known String type that could have null value *)
let to_string_null ctx e =
let v = alloc_var "__call__" t_dynamic in
let f = mk (TLocal v) t_dynamic e.epos in
mk (TCall (f, [ Codegen.string ctx.com "_hx_string_or_null" e.epos; e])) ctx.com.basic.tstring e.epos
let as_string_expr ctx e = match e.eexpr with
| TConst (TNull) -> to_string ctx e
| TConst (TString s) -> e
| TBinop (op,_,_) when (is_string_expr e)-> e
| TCall ({eexpr = TField({eexpr = TTypeExpr(TClassDecl {cl_path = ([],"Std")})},FStatic(c,f) )}, [_]) when (f.cf_name="string") -> e
| TCall ({eexpr = TLocal _}, [{eexpr = TConst (TString ("_hx_string_rec" | "_hx_str_or_null"))}]) -> e
| _ when not (is_string_expr e) -> to_string ctx e
| _ -> to_string_null ctx e
let spr ctx s = Buffer.add_string ctx.buf s
let print ctx = Printf.kprintf (fun s -> Buffer.add_string ctx.buf s)
(*--php-prefix - added by skial bainn*)
let prefix_class com name =
match com.php_prefix with
| Some prefix_class (* when not (String.length name <= 2 || String.sub name 0 2 = "__") *) ->
prefix_class ^ name
| _ ->
name
let prefix_init_replace com code =
let r = Str.regexp "php_Boot" in
Str.global_replace r ("php_" ^ (prefix_class com "Boot")) code
let s_path ctx path isextern p =
if isextern then begin
register_extern_required_path ctx path;
snd path
end else begin
(match path with
(*--php-prefix*)
| ([],"List") -> (prefix_class ctx.com "HList")
(*--php-prefix*)
| ([],name) -> (prefix_class ctx.com name)
| (pack,name) ->
(try
(match Hashtbl.find ctx.imports name with
| [p] when p = pack ->
()
| packs ->
if not (List.mem pack packs) then Hashtbl.replace ctx.imports name (pack :: packs))
with Not_found ->
Hashtbl.add ctx.imports name [pack]);
(*--php-prefix*)
String.concat "_" pack ^ "_" ^ (prefix_class ctx.com name))
end
let s_path_haxe path =
match fst path, snd path with
| [], s -> s
| el, s -> String.concat "." el ^ "." ^ s
let escape_bin s =
let b = Buffer.create 0 in
for i = 0 to String.length s - 1 do
match Char.code (String.unsafe_get s i) with
| c when c = Char.code('\\') || c = Char.code('"') || c = Char.code('$') ->
Buffer.add_string b "\\";
Buffer.add_char b (Char.chr c)
| c when c < 32 ->
Buffer.add_string b (Printf.sprintf "\\x%.2X" c)
| c ->
Buffer.add_char b (Char.chr c)
done;
Buffer.contents b
(*
haxe reserved words that match php ones: break, case, class, continue, default, do, else, extends, for, function, if, new, return, static, switch, var, while, interface, implements, public, private, try, catch, throw
*)
(* PHP only (for future use): cfunction, old_function *)
let is_keyword n =
match String.lowercase n with
| "and" | "or" | "xor" | "__file__" | "exception" | "__line__" | "array"
| "as" | "const" | "declare" | "die" | "echo"| "elseif" | "empty"
| "enddeclare" | "endfor" | "endforeach" | "endif" | "endswitch"
| "endwhile" | "eval" | "exit" | "foreach"| "global" | "include"
| "include_once" | "isset" | "list" | "namespace" | "print" | "require" | "require_once"
| "unset" | "use" | "__function__" | "__class__" | "__method__" | "final"
| "php_user_filter" | "protected" | "abstract" | "__set" | "__get" | "__call"
| "clone" | "instanceof" | "break" | "case" | "class" | "continue" | "default" | "do" | "else" | "extends" | "for" | "function" | "if" | "new" | "return" | "static" | "switch" | "var" | "while" | "interface" | "implements" | "public" | "private" | "try" | "catch" | "throw" -> true
| "goto"
| _ -> false
let s_ident n =
let suf = "h" in
if (is_keyword n) then (suf ^ n) else n
let s_ident_field n =
if (is_keyword n) then ("{\"" ^ (escape_bin n) ^ "\"}") else n
let s_ident_local n =
let suf = "h" in
match String.lowercase n with
| "globals" | "_server" | "_get" | "_post" | "_cookie" | "_files"
| "_env" | "_request" | "_session" -> suf ^ n
| _ -> n
let create_directory com ldir =
let atm_path = ref (String.create 0) in
atm_path := com.file;
if not (Sys.file_exists com.file) then (Unix.mkdir com.file 0o755);
(List.iter (fun p -> atm_path := !atm_path ^ "/" ^ p; if not (Sys.file_exists !atm_path) then (Unix.mkdir !atm_path 0o755);) ldir)
let write_resource dir name data =
let rdir = dir ^ "/res" in
if not (Sys.file_exists dir) then Unix.mkdir dir 0o755;
if not (Sys.file_exists rdir) then Unix.mkdir rdir 0o755;
let name = Codegen.escape_res_name name false in
let ch = open_out_bin (rdir ^ "/" ^ name) in
output_string ch data;
close_out ch
let stack_init com use_add =
Codegen.stack_context_init com "GLOBALS['%s']" "GLOBALS['%e']" "__hx__spos" "tmp" use_add null_pos
let init com cwd path def_type =
let rec create acc = function
| [] -> ()
| d :: l ->
let pdir = String.concat "/" (List.rev (d :: acc)) in
if not (Sys.file_exists pdir) then Unix.mkdir pdir 0o755;
create (d :: acc) l
in
let dir = if cwd <> "" then com.file :: (cwd :: fst path) else com.file :: fst path; in
create [] dir;
let filename path =
prefix_class com (match path with
| [], "List" -> "HList";
| _, s -> s) in
(*--php-prefix*)
let ch = open_out (String.concat "/" dir ^ "/" ^ (filename path) ^ (if def_type = 0 then ".class" else if def_type = 1 then ".enum" else if def_type = 2 then ".interface" else ".extern") ^ ".php") in
let imports = Hashtbl.create 0 in
Hashtbl.add imports (snd path) [fst path];
{
com = com;
stack = stack_init com false;
tabs = "";
ch = ch;
path = path;
buf = Buffer.create (1 lsl 14);
in_value = None;
in_loop = false;
in_instance_method = false;
imports = imports;
extern_required_paths = [];
extern_classes_with_init = [];
curclass = null_class;
curmethod = "";
locals = PMap.empty;
inv_locals = PMap.empty;
local_types = [];
inits = [];
constructor_block = false;
dynamic_methods = [];
all_dynamic_methods = [];
is_call = false;
cwd = cwd;
inline_methods = [];
nested_loops = 0;
inline_index = 0;
in_block = false;
lib_path = match com.php_lib with None -> "lib" | Some s -> s;
}
let unsupported msg p = error ("This expression cannot be generated to PHP: " ^ msg) p
let newline ctx =
match Buffer.nth ctx.buf (Buffer.length ctx.buf - 1) with
| '{' | ':' | ' '
| '}' when Buffer.nth ctx.buf (Buffer.length ctx.buf - 2) != '"' ->
print ctx "\n%s" ctx.tabs
| _ ->
print ctx ";\n%s" ctx.tabs
let rec concat ctx s f = function
| [] -> ()
| [x] -> f x
| x :: l ->
f x;
spr ctx s;
concat ctx s f l
let open_block ctx =
let oldt = ctx.tabs in
ctx.tabs <- "\t" ^ ctx.tabs;
(fun() -> ctx.tabs <- oldt)
let parent e =
match e.eexpr with
| TParenthesis _ -> e
| _ -> mk (TParenthesis e) e.etype e.epos
let inc_extern_path ctx path =
let rec slashes n =
if n = 0 then "" else ("../" ^ slashes (n-1))
in
let pre = if ctx.cwd = "" then ctx.lib_path ^ "/" else "" in
match path with
| ([],name) ->
pre ^ (slashes (List.length (fst ctx.path))) ^ (prefix_class ctx.com name) ^ ".extern.php"
| (pack,name) ->
pre ^ (slashes (List.length (fst ctx.path))) ^ String.concat "/" pack ^ "/" ^ (prefix_class ctx.com name) ^ ".extern.php"
let close ctx =
output_string ctx.ch "<?php\n";
List.iter (fun path ->
if path <> ctx.path then output_string ctx.ch ("require_once dirname(__FILE__).'/" ^ (inc_extern_path ctx path) ^ "';\n");
) (List.rev ctx.extern_required_paths);
output_string ctx.ch "\n";
output_string ctx.ch (Buffer.contents ctx.buf);
close_out ctx.ch
let save_locals ctx =
let old = ctx.locals in
let old_inv = ctx.inv_locals in
(fun() -> ctx.locals <- old; ctx.inv_locals <- old_inv)
let define_local ctx l =
let rec loop n =
let name = (if n = 1 then s_ident_local l else s_ident_local (l ^ string_of_int n)) in
if PMap.mem name ctx.inv_locals then
loop (n+1)
else begin
ctx.locals <- PMap.add l name ctx.locals;
ctx.inv_locals <- PMap.add name l ctx.inv_locals;
name
end
in
loop 1
let this ctx =
if ctx.in_value <> None then "$__hx__this" else "$this"
let gen_constant ctx p = function
| TInt i -> print ctx "%ld" i
| TFloat s -> spr ctx s
| TString s ->
print ctx "\"%s\"" (escape_bin s)
| TBool b -> spr ctx (if b then "true" else "false")
| TNull -> spr ctx "null"
| TThis -> spr ctx (this ctx)
| TSuper -> spr ctx "ERROR /* unexpected call to super in gen_constant */"
let arg_is_opt c =
match c with
| Some _ -> true
| None -> false
let s_funarg ctx arg t p o =
let byref = if (String.length arg > 7 && String.sub arg 0 7 = "byref__") then "&" else "" in
print ctx "%s$%s" byref (s_ident_local arg);
if o then spr ctx " = null"
(*
match c with
| _, Some _ -> spr ctx " = null"
| _, None -> ()
*)
let is_in_dynamic_methods ctx e s =
List.exists (fun dm ->
(* TODO: I agree, this is a mess ... but after hours of trials and errors I gave up; maybe in a calmer day *)
((String.concat "." ((fst dm.mpath) @ ["#" ^ (snd dm.mpath)])) ^ "." ^ dm.mname) = (s_type_name e.etype ^ "." ^ s)
) ctx.all_dynamic_methods
let is_dynamic_method f =
(match f.cf_kind with
| Var _ -> true
| Method MethDynamic -> true
| _ -> false)
let fun_block ctx f p =
let e = (match f.tf_expr with { eexpr = TBlock [{ eexpr = TBlock _ } as e] } -> e | e -> e) in
let e = List.fold_left (fun e (v,c) ->
match c with
| None | Some TNull -> e
| Some c -> Type.concat (Codegen.set_default ctx.com v c p) e
) e f.tf_args in
if ctx.com.debug then begin
Codegen.stack_block ctx.stack ctx.curclass ctx.curmethod e
end else
mk_block e
let rec gen_array_args ctx lst =
match lst with
| [] -> ()
| h :: t ->
spr ctx "[";
gen_value ctx h;
spr ctx "]";
gen_array_args ctx t
and gen_call ctx e el =
let rec genargs lst =
(match lst with
| [] -> ()
| h :: [] ->
spr ctx " = ";
gen_value ctx h;
| h :: t ->
spr ctx "[";
gen_value ctx h;
spr ctx "]";
genargs t)
in
match e.eexpr , el with
| TConst TSuper , params ->
(match ctx.curclass.cl_super with
| None -> assert false
| Some (c,_) ->
spr ctx "parent::__construct(";
concat ctx "," (gen_value ctx) params;
spr ctx ")";
);
| TField ({ eexpr = TConst TSuper },f) , params ->
(match ctx.curclass.cl_super with
| None -> assert false
| Some (c,_) ->
print ctx "parent::%s(" (s_ident (field_name f));
concat ctx "," (gen_value ctx) params;
spr ctx ")";
);
| TLocal { v_name = "__set__" }, { eexpr = TConst (TString code) } :: el ->
print ctx "$%s" code;
genargs el;
| TLocal { v_name = "__set__" }, e :: el ->
gen_value ctx e;
genargs el;
| TLocal { v_name = "__setfield__" }, e :: (f :: el) ->
gen_value ctx e;
spr ctx "->{";
gen_value ctx f;
spr ctx "}";
genargs el;
| TLocal { v_name = "__field__" }, e :: ({ eexpr = TConst (TString code) } :: el) ->
gen_value ctx e;
spr ctx "->";
spr ctx code;
gen_array_args ctx el;
| TLocal { v_name = "__field__" }, e :: (f :: el) ->
gen_value ctx e;
spr ctx "->";
gen_value ctx f;
gen_array_args ctx el;
| TLocal { v_name = "__prefix__" }, [] ->
(match ctx.com.php_prefix with
| Some prefix ->
print ctx "\"%s\"" prefix
| None ->
spr ctx "null")
| TLocal { v_name = "__var__" }, { eexpr = TConst (TString code) } :: el ->
print ctx "$%s" code;
gen_array_args ctx el;
| TLocal { v_name = "__var__" }, e :: el ->
gen_value ctx e;
gen_array_args ctx el;
| TLocal { v_name = "__call__" }, { eexpr = TConst (TString code) } :: el ->
spr ctx code;
spr ctx "(";
concat ctx ", " (gen_value ctx) el;
spr ctx ")";
| TLocal { v_name = "__php__" }, [{ eexpr = TConst (TString code) }] ->
(*--php-prefix*)
spr ctx (prefix_init_replace ctx.com code)
| TLocal { v_name = "__php__" }, { eexpr = TConst (TString code); epos = p } :: tl ->
Codegen.interpolate_code ctx.com code tl (spr ctx) (gen_expr ctx) p
| TLocal { v_name = "__instanceof__" }, [e1;{ eexpr = TConst (TString t) }] ->
gen_value ctx e1;
print ctx " instanceof %s" t;
| TLocal { v_name = "__physeq__" }, [e1;e2] ->
spr ctx "(";
gen_value ctx e1;
spr ctx " === ";
gen_value ctx e2;
spr ctx ")"
| TLocal _, []
| TFunction _, []
| TCall _, []
| TParenthesis _, []
| TMeta _, []
| TBlock _, [] ->
ctx.is_call <- true;
spr ctx "call_user_func(";
gen_value ctx e;
ctx.is_call <- false;
spr ctx ")";
| TLocal _, el
| TFunction _, el
| TCall _, el
| TParenthesis _, el
| TMeta _, el
| TBlock _, el ->
ctx.is_call <- true;
spr ctx "call_user_func_array(";
gen_value ctx e;
ctx.is_call <- false;
spr ctx ", array(";
concat ctx ", " (gen_value ctx) el;
spr ctx "))"
(*
| TCall (x,_), el when (match x.eexpr with | TLocal _ -> false | _ -> true) ->
ctx.is_call <- true;
spr ctx "call_user_func_array(";
gen_value ctx e;
ctx.is_call <- false;
spr ctx ", array(";
concat ctx ", " (gen_value ctx) el;
spr ctx "))"
*)
| _ ->
ctx.is_call <- true;
gen_value ctx e;
ctx.is_call <- false;
spr ctx "(";
concat ctx ", " (gen_value ctx) el;
spr ctx ")";
and could_be_string_var s =
s = "length"
and gen_uncertain_string_var ctx s e =
match s with
| "length" ->
spr ctx "_hx_len(";
gen_value ctx e;
spr ctx ")"
| _ ->
gen_field_access ctx true e s;
and gen_string_var ctx s e =
match s with
| "length" ->
spr ctx "strlen(";
gen_value ctx e;
spr ctx ")"
| _ ->
unsupported "gen_string_var " e.epos;
and gen_string_static_call ctx s e el =
match s with
| "fromCharCode" ->
spr ctx "chr(";
concat ctx ", " (gen_value ctx) el;
spr ctx ")";
| _ -> unsupported "gen_string_static_call " e.epos;
and could_be_string_call s =
s = "substr" || s = "substring" || s = "charAt" || s = "charCodeAt" || s = "indexOf" ||
s = "lastIndexOf" || s = "split" || s = "toLowerCase" || s = "toString" || s = "toUpperCase"
and gen_string_call ctx s e el =
match s with
| "substr" ->
spr ctx "_hx_substr(";
gen_value ctx e;
spr ctx ", ";
concat ctx ", " (gen_value ctx) el;
spr ctx ")"
| "substring" ->
spr ctx "_hx_substring(";
gen_value ctx e;
spr ctx ", ";
concat ctx ", " (gen_value ctx) el;
spr ctx ")"
| "charAt" ->
spr ctx "_hx_char_at(";
gen_value ctx e;
spr ctx ", ";
concat ctx ", " (gen_value ctx) el;
spr ctx ")"
| "cca" ->
spr ctx "ord(substr(";
gen_value ctx e;
spr ctx ",";
concat ctx ", " (gen_value ctx) el;
spr ctx ",1))"
| "charCodeAt" ->
spr ctx "_hx_char_code_at(";
gen_value ctx e;
spr ctx ", ";
concat ctx ", " (gen_value ctx) el;
spr ctx ")"
| "indexOf" ->
spr ctx "_hx_index_of(";
gen_value ctx e;
spr ctx ", ";
concat ctx ", " (gen_value ctx) el;
spr ctx ")"
| "lastIndexOf" ->
spr ctx "_hx_last_index_of(";
gen_value ctx e;
spr ctx ", ";
concat ctx ", " (gen_value ctx) el;
spr ctx ")"
| "split" ->
spr ctx "_hx_explode(";
concat ctx ", " (gen_value ctx) el;
spr ctx ", ";
gen_value ctx e;
spr ctx ")"
| "toLowerCase" ->
spr ctx "strtolower(";
gen_value ctx e;
spr ctx ")"
| "toUpperCase" ->
spr ctx "strtoupper(";
gen_value ctx e;
spr ctx ")"
| "toString" ->
gen_value ctx e;
| _ ->
unsupported "gen_string_call" e.epos;
and gen_uncertain_string_call ctx s e el =
spr ctx "_hx_string_call(";
gen_value ctx e;
print ctx ", \"%s\", array(" s;
concat ctx ", " (gen_value ctx) el;
spr ctx "))"
and gen_field_op ctx e =
match e.eexpr with
| TField (f,s) ->
(match follow e.etype with
| TFun _ ->
gen_field_access ctx true f (field_name s)
| _ ->
gen_value_op ctx e)
| _ ->
gen_value_op ctx e
and gen_value_op ctx e =
match e.eexpr with
| TBinop (op,_,_) when op = Ast.OpAnd || op = Ast.OpOr || op = Ast.OpXor ->
gen_value ctx e;
| _ ->
gen_value ctx e
and is_static t =
match follow t with
| TAnon a -> (match !(a.a_status) with
| Statics c -> true
| _ -> false)
| _ -> false
and gen_member_access ctx isvar e s =
match follow e.etype with
| TAnon a ->
(match !(a.a_status) with
| EnumStatics _ ->
print ctx "::%s%s" (if isvar then "$" else "") (s_ident s)
| Statics _ ->
print ctx "::%s%s" (if isvar then "$" else "") (s_ident s)
| _ -> print ctx "->%s" (if isvar then s_ident_field s else s_ident s))
| _ -> print ctx "->%s" (if isvar then s_ident_field s else s_ident s)
and gen_field_access ctx isvar e s =
match e.eexpr with
| TTypeExpr t ->
spr ctx (s_path ctx (t_path t) false e.epos);
gen_member_access ctx isvar e s
| TLocal _ ->
gen_expr ctx e;
print ctx "->%s" (if isvar then s_ident_field s else s_ident s)
| TArray (e1,e2) ->
spr ctx "_hx_array_get(";
gen_value ctx e1;
spr ctx ", ";
gen_value ctx e2;
spr ctx ")";
gen_member_access ctx isvar e s
| TBlock _
| TParenthesis _
| TMeta _
| TObjectDecl _
| TArrayDecl _
| TNew _ ->
spr ctx "_hx_deref(";
ctx.is_call <- false;
gen_value ctx e;
spr ctx ")";
gen_member_access ctx isvar e s
| TCast (ec, _) when (match ec.eexpr with | TNew _ | TArrayDecl _ -> true | _ -> false) ->
spr ctx "_hx_deref(";
ctx.is_call <- false;
gen_value ctx e;
spr ctx ")";
gen_member_access ctx isvar e s
| _ ->
gen_expr ctx e;
gen_member_access ctx isvar e s
and gen_dynamic_function ctx isstatic name f params p =
let old = ctx.in_value in
let old_l = ctx.locals in
let old_li = ctx.inv_locals in
let old_t = ctx.local_types in
ctx.in_value <- None;
ctx.local_types <- List.map snd params @ ctx.local_types;
let byref = if (String.length name > 9 && String.sub name 0 9 = "__byref__") then "&" else "" in
print ctx "function %s%s(" byref name;
concat ctx ", " (fun (v,c) ->
let arg = define_local ctx v.v_name in
s_funarg ctx arg v.v_type p (arg_is_opt c);
) f.tf_args;
spr ctx ") {";
if (List.length f.tf_args) > 0 then begin
if isstatic then
print ctx " return call_user_func_array(self::$%s, array(" name
else
print ctx " return call_user_func_array($this->%s, array(" name;
concat ctx ", " (fun (v,_) ->
spr ctx ("$" ^ v.v_name)
) f.tf_args;
print ctx ")); }";
end else if isstatic then
print ctx " return call_user_func(self::$%s); }" name
else
print ctx " return call_user_func($this->%s); }" name;
newline ctx;
if isstatic then
print ctx "public static $%s = null" name
else
print ctx "public $%s = null" name;
ctx.in_value <- old;
ctx.locals <- old_l;
ctx.inv_locals <- old_li;
ctx.local_types <- old_t
and gen_function ctx name f params p =
let old = ctx.in_value in
let old_l = ctx.locals in
let old_li = ctx.inv_locals in
let old_t = ctx.local_types in
ctx.in_value <- None;
ctx.local_types <- List.map snd params @ ctx.local_types;
let byref = if (String.length name > 9 && String.sub name 0 9 = "__byref__") then "&" else "" in
print ctx "function %s%s(" byref name;
concat ctx ", " (fun (v,o) ->
let arg = define_local ctx v.v_name in
s_funarg ctx arg v.v_type p (arg_is_opt o);
) f.tf_args;
print ctx ") ";
gen_expr ctx (fun_block ctx f p);
ctx.in_value <- old;
ctx.locals <- old_l;
ctx.inv_locals <- old_li;
ctx.local_types <- old_t
and gen_inline_function ctx f hasthis p =
ctx.nested_loops <- ctx.nested_loops - 1;
let old = ctx.in_value in
let old_l = ctx.locals in
let old_li = ctx.inv_locals in
let old_t = ctx.local_types in
ctx.in_value <- Some "closure";
let args a = List.map (fun (v,_) -> v.v_name) a in
let arguments = ref [] in
if hasthis then begin arguments := "this" :: !arguments end;
PMap.iter (fun n _ -> arguments := !arguments @ [n]) old_li;
spr ctx "array(new _hx_lambda(array(";
let c = ref 0 in
List.iter (fun a ->
if !c > 0 then spr ctx ", ";
incr c;
print ctx "&$%s" a;
) (remove_internals !arguments);
spr ctx "), \"";
spr ctx (inline_function ctx (args f.tf_args) hasthis (fun_block ctx f p));
print ctx "\"), 'execute')";
ctx.in_value <- old;
ctx.locals <- old_l;
ctx.inv_locals <- old_li;
ctx.local_types <- old_t;
ctx.nested_loops <- ctx.nested_loops + 1;
and unset_locals ctx old_l =
let lst = ref [] in
PMap.iter (fun n _ ->
if not (PMap.exists n old_l) then
lst := ["$" ^ n] @ !lst;
) ctx.inv_locals;
if (List.length !lst) > 0 then begin
newline ctx;
spr ctx "unset(";
concat ctx "," (fun (s) -> spr ctx s; ) !lst;
spr ctx ")"
end
and gen_while_expr ctx e =
let old_loop = ctx.in_loop in
ctx.in_loop <- true;
let old_nested_loops = ctx.nested_loops in
ctx.nested_loops <- 1;
let old_l = ctx.inv_locals in
let b = save_locals ctx in
(match e.eexpr with
| TBlock (el) ->
List.iter (fun e -> newline ctx; gen_expr ctx e) el;
| _ ->
newline ctx;
gen_expr ctx e);
unset_locals ctx old_l;
b();
ctx.nested_loops <- old_nested_loops;
ctx.in_loop <- old_loop
and gen_tfield ctx e e1 s =
match follow e.etype with
| TFun (args, _) ->
(if ctx.is_call then begin
gen_field_access ctx false e1 s
end else if is_in_dynamic_methods ctx e1 s then begin
gen_field_access ctx true e1 s;
end else begin
let ob ex =
(match ex with
| TTypeExpr t ->
print ctx "\"";
spr ctx (s_path ctx (t_path t) false e1.epos);
print ctx "\""
| _ ->
gen_expr ctx e1) in
spr ctx "(isset(";
gen_field_access ctx true e1 s;
spr ctx ") ? ";
gen_field_access ctx true e1 s;
spr ctx ": array(";
ob e1.eexpr;
print ctx ", \"%s\"))" (s_ident s);
end)
| TMono _ ->
if ctx.is_call then
gen_field_access ctx false e1 s
else
gen_uncertain_string_var ctx s e1
| _ ->
if is_string_expr e1 then
gen_string_var ctx s e1
else if is_uncertain_expr e1 then
gen_uncertain_string_var ctx s e1
else
gen_field_access ctx true e1 s
and gen_expr ctx e =
let in_block = ctx.in_block in
ctx.in_block <- false;
let restore_in_block ctx inb =
if inb then ctx.in_block <- true
in
match e.eexpr with
| TConst c ->
gen_constant ctx e.epos c
| TLocal v ->
spr ctx ("$" ^ (try PMap.find v.v_name ctx.locals with Not_found -> (s_ident_local v.v_name)))
| TArray (e1,e2) ->
(match e1.eexpr with
| TCall _
| TBlock _
| TParenthesis _
| TMeta _
| TArrayDecl _ ->
spr ctx "_hx_array_get(";
gen_value ctx e1;
spr ctx ", ";
gen_value ctx e2;
spr ctx ")";
| TCast (ec, _) when (match ec.eexpr with | TArrayDecl _ | TBlock _ -> true | _ -> false) ->
spr ctx "_hx_array_get(";
gen_value ctx e1;
spr ctx ", ";
gen_value ctx e2;
spr ctx ")";
| _ ->
gen_value ctx e1;
spr ctx "[";
gen_value ctx e2;
spr ctx "]");
| TBinop (op,e1,e2) ->
(* these operators are non-assoc in php, let let's make sure to separate them with parenthesises *)
let non_assoc = function
| (Ast.OpEq | Ast.OpNotEq | Ast.OpGt | Ast.OpGte | Ast.OpLt | Ast.OpLte) -> true
| _ -> false
in
(match e1.eexpr with
| TBinop (op2,_,_) when non_assoc op && non_assoc op2 ->
gen_expr ctx { e with eexpr = TBinop (op,mk (TParenthesis e1) e1.etype e1.epos,e2) }
| _ ->
let leftside e =
(match e.eexpr with
| TArray(te1, te2) ->
gen_value ctx te1;
spr ctx "->a[";
gen_value ctx te2;
spr ctx "]";
| _ ->
gen_field_op ctx e1;) in
let leftsidec e =
(match e.eexpr with
| TArray(te1, te2) ->
gen_value ctx te1;
spr ctx "->a[";
gen_value ctx te2;
spr ctx "]";
| TField (e1,s) ->
gen_field_access ctx true e1 (field_name s)
| _ ->
gen_field_op ctx e1;) in
let leftsidef e =
(match e.eexpr with
| TField (e1,s) ->
gen_field_access ctx true e1 (field_name s)
| _ ->
gen_field_op ctx e1;
) in
(match op with
| Ast.OpMod ->
spr ctx "_hx_mod(";
gen_value_op ctx e1;
spr ctx ", ";
gen_value_op ctx e2;
spr ctx ")";
| Ast.OpAssign ->
(match e1.eexpr with
| TArray(te1, te2) when (match te1.eexpr with | TCall _ | TParenthesis _ -> true | _ -> false) ->
spr ctx "_hx_array_assign(";
gen_value ctx te1;
spr ctx ", ";
gen_value ctx te2;
spr ctx ", ";
gen_value_op ctx e2;
spr ctx ")";
| _ ->
leftsidef e1;
spr ctx " = ";
gen_value_op ctx e2;
)
| Ast.OpAssignOp(Ast.OpAdd) when (is_uncertain_expr e1 && is_uncertain_expr e2) ->
(match e1.eexpr with
| TArray(te1, te2) ->
let t1 = define_local ctx "__hx__t1" in
let t2 = define_local ctx "__hx__t2" in
print ctx "_hx_array_assign($%s = " t1;
gen_value ctx te1;
print ctx ", $%s = " t2;
gen_value ctx te2;
print ctx ", $%s->a[$%s] + " t1 t2;
gen_value_op ctx e2;
spr ctx ")";
| _ ->
leftside e1;
spr ctx " = ";
spr ctx "_hx_add(";
gen_value_op ctx e1;
spr ctx ", ";
gen_value_op ctx e2;
spr ctx ")";
)
| Ast.OpAssignOp(Ast.OpAdd) when (is_string_expr e1 || is_string_expr e2) ->
leftside e1;
spr ctx " .= ";
gen_value_op ctx (as_string_expr ctx e2);
| Ast.OpAssignOp(Ast.OpShl) ->
leftside e1;
spr ctx " <<= ";
gen_value_op ctx e2;
| Ast.OpAssignOp(Ast.OpUShr) ->
leftside e1;
spr ctx " = ";
spr ctx "_hx_shift_right(";
gen_value_op ctx e1;
spr ctx ", ";
gen_value_op ctx e2;
spr ctx ")";
| Ast.OpAssignOp(Ast.OpMod) ->
leftside e1;
spr ctx " = ";
spr ctx "_hx_mod(";
gen_value_op ctx e1;
spr ctx ", ";
gen_value_op ctx e2;
spr ctx ")";
| Ast.OpAssignOp(_) ->
leftsidec e1;
print ctx " %s " (Ast.s_binop op);
gen_value_op ctx e2;
| Ast.OpAdd when (is_uncertain_expr e1 && is_uncertain_expr e2) ->
spr ctx "_hx_add(";
gen_value_op ctx e1;
spr ctx ", ";
gen_value_op ctx e2;
spr ctx ")";
| Ast.OpAdd when (is_string_expr e1 || is_string_expr e2) ->
gen_value_op ctx (as_string_expr ctx e1);
spr ctx " . ";
gen_value_op ctx (as_string_expr ctx e2);
| Ast.OpShl ->
gen_value_op ctx e1;
spr ctx " << ";
gen_value_op ctx e2;
| Ast.OpUShr ->
spr ctx "_hx_shift_right(";
gen_value_op ctx e1;
spr ctx ", ";
gen_value_op ctx e2;
spr ctx ")";
| Ast.OpNotEq
| Ast.OpEq ->
let s_op = if op = Ast.OpNotEq then " != " else " == " in
let s_phop = if op = Ast.OpNotEq then " !== " else " === " in
let se1 = s_expr_name e1 in
let se2 = s_expr_name e2 in
if
e1.eexpr = TConst (TNull)
|| e2.eexpr = TConst (TNull)
then begin
(match e1.eexpr with
| TField (f, s) when is_anonym_expr e1 || is_unknown_expr e1 ->
spr ctx "_hx_field(";
gen_value ctx f;
print ctx ", \"%s\")" (field_name s);
| _ ->
gen_field_op ctx e1;
);
spr ctx s_phop;
(match e2.eexpr with
| TField (f, s) when is_anonym_expr e2 || is_unknown_expr e2 ->
spr ctx "_hx_field(";
gen_value ctx f;
print ctx ", \"%s\")" (field_name s);
| _ ->
gen_field_op ctx e2);
end else if
((se1 = "Int" || se1 = "Null<Int>") && (se2 = "Int" || se2 = "Null<Int>"))
|| ((se1 = "Float" || se1 = "Null<Float>") && (se2 = "Float" || se2 = "Null<Float>"))
then begin
gen_field_op ctx e1;
spr ctx s_phop;
gen_field_op ctx e2;
end else if
((se1 = "Int" || se1 = "Float" || se1 = "Null<Int>" || se1 = "Null<Float>")
&& (se1 = "Int" || se1 = "Float" || se1 = "Null<Int>" || se1 = "Null<Float>"))
|| (is_unknown_expr e1 && is_unknown_expr e2)
|| is_anonym_expr e1
|| is_anonym_expr e2
then begin
if op = Ast.OpNotEq then spr ctx "!";
spr ctx "_hx_equal(";
gen_field_op ctx e1;
spr ctx ", ";
gen_field_op ctx e2;
spr ctx ")";
end else if
(
se1 == se2
|| (match e1.eexpr with | TConst _ | TLocal _ | TArray _ | TNew _ -> true | _ -> false)
|| (match e2.eexpr with | TConst _ | TLocal _ | TArray _ | TNew _ -> true | _ -> false)
|| is_string_expr e1
|| is_string_expr e2
|| is_anonym_expr e1
|| is_anonym_expr e2
|| is_unknown_expr e1
|| is_unknown_expr e2
)
&& (type_string (follow e1.etype)) <> "Dynamic"
&& (type_string (follow e2.etype)) <> "Dynamic"
then begin
gen_field_op ctx e1;
spr ctx s_phop;
gen_field_op ctx e2;
end else begin
let tmp = define_local ctx "_t" in
print ctx "(is_object($%s = " tmp;
gen_field_op ctx e1;
print ctx ") && !($%s instanceof Enum) ? $%s%s" tmp tmp s_phop;
gen_field_op ctx e2;
print ctx " : $%s%s" tmp s_op;
gen_field_op ctx e2;
spr ctx ")";
end
| Ast.OpGt | Ast.OpGte | Ast.OpLt | Ast.OpLte when is_string_expr e1 ->
spr ctx "(strcmp(";
gen_field_op ctx e1;
spr ctx ", ";
gen_field_op ctx e2;
spr ctx ")";
let op_str = match op with
| Ast.OpGt -> ">"
| Ast.OpGte -> ">="
| Ast.OpLt -> "<"
| Ast.OpLte -> "<="
| _ -> assert false
in
print ctx "%s 0)" op_str
| _ ->
leftside e1;
print ctx " %s " (Ast.s_binop op);
gen_value_op ctx e2;
));
| TEnumParameter(e1,_,i) ->
spr ctx "_hx_deref(";
gen_value ctx e1;
spr ctx ")";
print ctx "->params[%d]" i;
| TField (e1,s) ->
gen_tfield ctx e e1 (field_name s)
| TTypeExpr t ->
print ctx "_hx_qtype(\"%s\")" (s_path_haxe (t_path t))
| TParenthesis e ->
(match e.eexpr with
| TParenthesis _
| TReturn _ ->
gen_value ctx e;
| _ ->
spr ctx "(";
gen_value ctx e;
spr ctx ")"
);
| TMeta (_,e) ->
gen_expr ctx e
| TReturn eo ->
(match eo with
| None ->
spr ctx "return"
| Some e when (match follow e.etype with TEnum({ e_path = [],"Void" },[]) | TAbstract ({ a_path = [],"Void" },[]) -> true | _ -> false) ->
gen_value ctx e;
newline ctx;
spr ctx "return"
| Some e ->
(match e.eexpr with
| TThrow _ -> ()
| _ -> spr ctx "return ");
gen_value ctx e;
);
| TBreak ->
if ctx.in_loop then spr ctx "break" else print ctx "break %d" ctx.nested_loops
| TContinue ->
if ctx.in_loop then spr ctx "continue" else print ctx "continue %d" ctx.nested_loops
| TBlock [] ->
spr ctx "{}"
| TBlock el ->
let old_l = ctx.inv_locals in
let b = save_locals ctx in
print ctx "{";
let bend = open_block ctx in
let cb = (
if not ctx.constructor_block then
(fun () -> ())
else begin
ctx.constructor_block <- false;
if List.length ctx.dynamic_methods > 0 then newline ctx else spr ctx " ";
List.iter (fun (f) ->
let name = f.cf_name in
match f.cf_expr with
| Some { eexpr = TFunction fd } ->
print ctx "if(!isset($this->%s)) $this->%s = " name name;
gen_inline_function ctx fd true e.epos;
newline ctx;
| _ -> ()
) ctx.dynamic_methods;
if Codegen.constructor_side_effects e then begin
print ctx "if(!%s::$skip_constructor) {" (s_path ctx (["php"],"Boot") false e.epos);
(fun() -> print ctx "}")
end else
(fun() -> ());
end) in
let remaining = ref (List.length el) in
let build e =
newline ctx;
if (in_block && !remaining = 1) then begin
(match e.eexpr with
| TIf _
| TSwitch _
| TThrow _
| TWhile _
| TFor _
| TTry _
| TBreak
| TBlock _ ->
restore_in_block ctx in_block;
gen_expr ctx e;
unset_locals ctx old_l;
| TReturn (Some e1) ->
(match e1.eexpr with
| TIf _
| TSwitch _
| TThrow _
| TWhile _
| TFor _
| TTry _
| TBlock _ -> ()
| _ ->
spr ctx "return "
);
gen_expr ctx e1;
| _ ->
spr ctx "return ";
gen_value ctx e;
)
end else begin
gen_expr ctx e;
end;
decr remaining;
in
List.iter build el;
if ctx.in_loop then begin
unset_locals ctx old_l;
end;
bend();
newline ctx;
cb();
print ctx "}";
b();
| TFunction f ->
let old = ctx.in_value, ctx.in_loop in
let old_meth = ctx.curmethod in
ctx.in_value <- None;
ctx.in_loop <- false;
ctx.curmethod <- ctx.curmethod ^ "@" ^ string_of_int (Lexer.get_error_line e.epos);
gen_inline_function ctx f false e.epos;
ctx.curmethod <- old_meth;
ctx.in_value <- fst old;
ctx.in_loop <- snd old;
| TCall (ec,el) ->
(match ec.eexpr with
| TArray _ ->
spr ctx "call_user_func_array(";
gen_value ctx ec;
spr ctx ", array(";
concat ctx ", " (gen_value ctx) el;
spr ctx "))";
| TField (ef,s) when is_static ef.etype && is_string_expr ef ->
gen_string_static_call ctx (field_name s) ef el
| TField (ef,s) when is_string_expr ef ->
gen_string_call ctx (field_name s) ef el
| TField (ef,s) when is_anonym_expr ef && could_be_string_call (field_name s) ->
gen_uncertain_string_call ctx (field_name s) ef el
| _ ->
gen_call ctx ec el);
| TArrayDecl el ->
spr ctx "(new _hx_array(array(";
concat ctx ", " (gen_value ctx) el;
spr ctx ")))";
| TThrow e ->
spr ctx "throw new HException(";
gen_value ctx e;
spr ctx ")";
| TVar (v,eo) ->
spr ctx "$";
let restore = save_locals ctx in
let n = define_local ctx v.v_name in
let restore2 = save_locals ctx in
restore();
(match eo with
| None ->
print ctx "%s = null" (s_ident_local n)
| Some e ->
print ctx "%s = " (s_ident_local n);
gen_value ctx e);
restore2()
| TNew (c,_,el) ->
(match c.cl_path, el with
| ([], "String"), _ ->
concat ctx "" (gen_value ctx) el
| ([], "Array"), el ->
spr ctx "new _hx_array(array(";
concat ctx ", " (gen_value ctx) el;
spr ctx "))"
| (_, _), _ ->
print ctx "new %s(" (s_path ctx c.cl_path c.cl_extern e.epos);
let count = ref (-1) in
concat ctx ", " (fun e ->
incr count;
match c.cl_constructor with
| Some f ->
gen_value ctx e;
| _ -> ();
) el;
spr ctx ")")
| TIf (cond,e,eelse) ->
spr ctx "if";
gen_value ctx (parent cond);
spr ctx " ";
restore_in_block ctx in_block;
gen_expr ctx (mk_block e);
(match eelse with
| None -> ()
| Some e when e.eexpr = TConst(TNull) -> ()
| Some e ->
spr ctx " else ";
restore_in_block ctx in_block;
gen_expr ctx (mk_block e));
| TUnop (op,Ast.Prefix,e) ->
(match e.eexpr with
| TArray(te1, te2) ->
(match op with
| Increment ->
spr ctx "_hx_array_increment(";
gen_value ctx te1;
spr ctx ",";
gen_value ctx te2;
spr ctx ")";
| Decrement ->
spr ctx "_hx_array_decrement(";
gen_value ctx te1;
spr ctx ",";
gen_value ctx te2;
spr ctx ")";
| _ ->
spr ctx (Ast.s_unop op);
gen_value ctx te1;
spr ctx "[";
gen_value ctx te2;
spr ctx "]";
);
| TField (e1,s) ->
spr ctx (Ast.s_unop op);
gen_tfield ctx e e1 (field_name s)
| _ ->
spr ctx (Ast.s_unop op);
gen_value ctx e)
| TUnop (op,Ast.Postfix,e) ->
(match e.eexpr with
| TArray(te1, te2) ->
gen_value ctx te1;
spr ctx "->a[";
gen_value ctx te2;
spr ctx "]";
| TField (e1,s) ->
gen_field_access ctx true e1 (field_name s)
| _ ->
gen_value ctx e);
spr ctx (Ast.s_unop op)
| TWhile (cond,e,Ast.NormalWhile) ->
let old = save_locals ctx in
spr ctx "while";
gen_value ctx (parent cond);
spr ctx " {";
let bend = open_block ctx in
gen_while_expr ctx e;
bend();
newline ctx;
spr ctx "}";
old()
| TWhile (cond,e,Ast.DoWhile) ->
let old = save_locals ctx in
spr ctx "do {";
let bend = open_block ctx in
gen_while_expr ctx e;
bend();
newline ctx;
spr ctx "} while";
gen_value ctx (parent cond);
old()
| TObjectDecl fields ->
spr ctx "_hx_anonymous(array(";
concat ctx ", " (fun (f,e) -> print ctx "\"%s\" => " (escape_bin f); gen_value ctx e) fields;
spr ctx "))"
| TFor (v,it,e) ->
let b = save_locals ctx in
let tmp = define_local ctx "__hx__it" in
let v = define_local ctx v.v_name in
(match it.eexpr with
| TCall (e,_) ->
(match e.eexpr with
| TField (e,f) ->
spr ctx "if(null == ";
gen_value ctx e;
spr ctx ") throw new HException('null iterable')";
newline ctx;
| _ ->
());
| _ -> ()
);
print ctx "$%s = " tmp;
gen_value ctx it;
newline ctx;
print ctx "while($%s->hasNext()) {" tmp;
let bend = open_block ctx in
newline ctx;
(* unset loop variable (issue #2900) *)
print ctx "unset($%s)" v;
newline ctx;
print ctx "$%s = $%s->next()" v tmp;
gen_while_expr ctx e;
bend();
newline ctx;
spr ctx "}";
b();
| TTry (e,catchs) ->
spr ctx "try ";
restore_in_block ctx in_block;
gen_expr ctx (mk_block e);
let old = save_locals ctx in
let ex = define_local ctx "__hx__e" in
print ctx "catch(Exception $%s) {" ex;
let bend = open_block ctx in
let first = ref true in
let catchall = ref false in
let evar = define_local ctx "_ex_" in
newline ctx;
print ctx "$%s = ($%s instanceof HException) ? $%s->e : $%s" evar ex ex ex;
old();
List.iter (fun (v,e) ->
let ev = define_local ctx v.v_name in
newline ctx;
let b = save_locals ctx in
if not !first then spr ctx "else ";
(match follow v.v_type with
| TEnum (te,_) -> (match te.e_path with
| [], "Bool" -> print ctx "if(is_bool($%s = $%s))" ev evar
| _ -> print ctx "if(($%s = $%s) instanceof %s)" ev evar (s_path ctx te.e_path te.e_extern e.epos));
restore_in_block ctx in_block;
gen_expr ctx (mk_block e);
| TInst (tc,_) -> (match tc.cl_path with
| [], "Int" -> print ctx "if(is_int($%s = $%s))" ev evar
| [], "Float" -> print ctx "if(is_numeric($%s = $%s))" ev evar
| [], "String" -> print ctx "if(is_string($%s = $%s))" ev evar
| [], "Array" -> print ctx "if(($%s = $%s) instanceof _hx_array)" ev evar
| _ -> print ctx "if(($%s = $%s) instanceof %s)" ev evar (s_path ctx tc.cl_path tc.cl_extern e.epos));
restore_in_block ctx in_block;
gen_expr ctx (mk_block e);
| TAbstract (ta,_) -> (match ta.a_path with
| [], "Int" -> print ctx "if(is_int($%s = $%s))" ev evar
| [], "Float" -> print ctx "if(is_numeric($%s = $%s))" ev evar
| [], "Bool" -> print ctx "if(is_bool($%s = $%s))" ev evar
| _ -> print ctx "if(($%s = $%s) instanceof %s)" ev evar (s_path ctx ta.a_path false e.epos));
restore_in_block ctx in_block;
gen_expr ctx (mk_block e);
| TFun _
| TLazy _
| TType _
| TAnon _ ->
assert false
| TMono _
| TDynamic _ ->
catchall := true;
if not !first then spr ctx "{ ";
print ctx "$%s = $%s" ev evar;
newline ctx;
restore_in_block ctx in_block;
gen_expr ctx (mk_block e);
if not !first then spr ctx "}"
);
b();
first := false;
) catchs;
if not !catchall then
print ctx " else throw $%s;" ex;
bend();
newline ctx;
spr ctx "}"
| TSwitch (e,cases,def) ->
let old_loop = ctx.in_loop in
ctx.in_loop <- false;
ctx.nested_loops <- ctx.nested_loops + 1;
let old = save_locals ctx in
spr ctx "switch";
gen_value ctx (parent e);
spr ctx " {";
newline ctx;
List.iter (fun (el,e2) ->
List.iter (fun e ->
spr ctx "case ";
gen_value ctx e;
spr ctx ":";
) el;
restore_in_block ctx in_block;
gen_expr ctx (mk_block e2);
print ctx "break";
newline ctx;
) cases;
(match def with
| None -> ()
| Some e ->
spr ctx "default:";
restore_in_block ctx in_block;
gen_expr ctx (mk_block e);
print ctx "break";
newline ctx;
);
spr ctx "}";
ctx.nested_loops <- ctx.nested_loops - 1;
ctx.in_loop <- old_loop;
old()
| TCast (e,None) ->
gen_expr ctx e
| TCast (e1,Some t) ->
let mk_texpr = function
| TClassDecl c -> TAnon { a_fields = PMap.empty; a_status = ref (Statics c) }
| TEnumDecl e -> TAnon { a_fields = PMap.empty; a_status = ref (EnumStatics e) }
| TAbstractDecl a -> TAnon { a_fields = PMap.empty; a_status = ref (AbstractStatics a) }
| TTypeDecl _ -> assert false
in
spr ctx "_hx_cast(";
gen_expr ctx e1;
spr ctx ", ";
gen_expr ctx (mk (TTypeExpr t) (mk_texpr t) e1.epos);
spr ctx ")"
and argument_list_from_locals include_this in_var l =
let lst = ref [] in
if (include_this && in_var) then lst := "__hx__this" :: !lst
else if include_this then lst := "this" :: !lst;
PMap.iter (fun n _ ->
lst := !lst @ [n];
) l;
!lst
and remove_internals args =
List.filter (fun a -> a = "__hx__this" || not (start_with a "__hx__")) args;
and inline_block ctx e =
let index = ctx.inline_index in
ctx.inline_index <- ctx.inline_index + 1;
let block = {
iname = (s_path ctx ctx.curclass.cl_path ctx.curclass.cl_extern ctx.curclass.cl_pos) ^ "_" ^ string_of_int index;
iindex = index;
ihasthis = ctx.in_instance_method; (* param this *)
iarguments = [];
iexpr = e;
ilocals = ctx.locals;
iin_block = true;
iinv_locals = ctx.inv_locals;
} in
print ctx "%s(" block.iname;
let in_value = (match ctx.in_value with Some _ -> true | _ -> false) in
(match remove_internals (argument_list_from_locals ctx.in_instance_method in_value ctx.locals) with
| [] -> ()
| l -> print ctx "$%s" (String.concat ", $" l)
);
spr ctx ")";
ctx.inline_methods <- ctx.inline_methods @ [block]
and inline_function ctx args hasthis e =
let index = ctx.inline_index in
ctx.inline_index <- ctx.inline_index + 1;
let block = {
iname = (s_path ctx ctx.curclass.cl_path ctx.curclass.cl_extern ctx.curclass.cl_pos) ^ "_" ^ string_of_int index;
iindex = index;
ihasthis = hasthis; (* param this *)
iarguments = args;
iexpr = e;
ilocals = ctx.locals;
iin_block = false;
iinv_locals = ctx.inv_locals;
} in
ctx.inline_methods <- ctx.inline_methods @ [block];
block.iname
and canbe_ternary_param e =
match e.eexpr with
| TTypeExpr _
| TConst _
| TLocal _
| TField (_,FEnum _)
| TParenthesis _
| TMeta _
| TObjectDecl _
| TArrayDecl _
| TCall _
| TUnop _
| TNew _
| TCast (_, _)
| TBlock [_] ->
true
| TIf (_,e,eelse) ->
cangen_ternary e eelse
| _ ->
false
and cangen_ternary e eelse =
match eelse with
| Some other ->
(canbe_ternary_param e) && (canbe_ternary_param other)
| _ ->
false
and gen_value ctx e =
match e.eexpr with
| TTypeExpr _
| TConst _
| TLocal _
| TArray _
| TBinop _
| TEnumParameter _
| TField _
| TParenthesis _
| TObjectDecl _
| TArrayDecl _
| TCall _
| TUnop _
| TNew _
| TFunction _ ->
gen_expr ctx e
| TMeta (_,e1) ->
gen_value ctx e1
| TBlock [] ->
()
| TCast (e, _)
| TBlock [e] ->
gen_value ctx e
| TIf (cond,e,eelse) when (cangen_ternary e eelse) ->
spr ctx "(";
gen_value ctx cond;
spr ctx " ? ";
gen_value ctx e;
(match eelse with
| Some e ->
spr ctx " : ";
gen_value ctx e
| _ ->());
spr ctx ")";
(*
| TIf (cond,e,eelse) ->
spr ctx "if";
gen_value ctx (parent cond);
spr ctx " ";
restore_in_block ctx in_block;
gen_expr ctx (mk_block e);
(match eelse with
| None -> ()
| Some e when e.eexpr = TConst(TNull) -> ()
| Some e ->
spr ctx " else ";
restore_in_block ctx in_block;
gen_expr ctx (mk_block e));
*)
| TBlock _
| TBreak
| TContinue
| TVar _
| TReturn _
| TWhile _
| TThrow _
| TSwitch _
| TFor _
| TIf _
| TTry _ ->
inline_block ctx e
let rec is_instance_method_defined cls m =
if PMap.exists m cls.cl_fields then
true
else
match cls.cl_super with
| Some (scls, _) ->
is_instance_method_defined scls m
| None ->
false
let is_method_defined ctx m static =
if static then
PMap.exists m ctx.curclass.cl_statics
else
is_instance_method_defined ctx.curclass m
let generate_self_method ctx rights m static setter =
if setter then (
if static then
print ctx "%s function %s($v) { return call_user_func(self::$%s, $v); }" rights (s_ident m) (s_ident m)
else
print ctx "%s function %s($v) { return call_user_func($this->%s, $v); }" rights (s_ident m) (s_ident m)
) else (
if static then
print ctx "%s function %s() { return call_user_func(self::$%s); }" rights (s_ident m) (s_ident m)
else
print ctx "%s function %s() { return call_user_func($this->%s); }" rights (s_ident m) (s_ident m)
);
newline ctx
let gen_assigned_value ctx eo = match eo with
| Some ({eexpr = TConst _} as e) ->
print ctx " = ";
gen_value ctx e
| _ ->
()
let generate_field ctx static f =
if not (is_extern_field f) then
newline ctx;
ctx.locals <- PMap.empty;
ctx.inv_locals <- PMap.empty;
ctx.in_instance_method <- not static;
let rights = if static then "static" else "public" in
let p = ctx.curclass.cl_pos in
match f.cf_expr with
| Some { eexpr = TFunction fd } ->
if f.cf_name = "__construct" then
ctx.curmethod <- "new"
else
ctx.curmethod <- f.cf_name;
spr ctx (rights ^ " ");
if is_dynamic_method f then
gen_dynamic_function ctx static (s_ident f.cf_name) fd f.cf_params p
else
gen_function ctx (s_ident f.cf_name) fd f.cf_params p
| _ ->
if (is_extern_field f) then
()
else if ctx.curclass.cl_interface then
match follow f.cf_type, f.cf_kind with
| TFun (args,r), Method _ ->
print ctx "function %s(" (s_ident f.cf_name);
concat ctx ", " (fun (arg,o,t) ->
s_funarg ctx arg t p o;
) args;
print ctx ")";
| _ -> spr ctx "//"; ()
else if
(match f.cf_kind with
| Var v ->
(match v.v_read, v.v_write with
| AccCall, AccCall ->
let m1 = "get_" ^ f.cf_name in
let m2 = "set_" ^ f.cf_name in
if not (is_method_defined ctx m1 static) then (
generate_self_method ctx rights m1 static false;
print ctx "%s $%s" rights (s_ident m1);
if not (is_method_defined ctx m2 static) then
newline ctx);
if not (is_method_defined ctx m2 static) then (
generate_self_method ctx rights m2 static true;
print ctx "%s $%s" rights (s_ident m2);
newline ctx);
false
| AccCall, _ ->
let m = "get_" ^ f.cf_name in
if not (is_method_defined ctx m static) then generate_self_method ctx rights m static false;
print ctx "%s $%s" rights (s_ident_field f.cf_name);
gen_assigned_value ctx f.cf_expr;
true
| _, AccCall ->
let m = "set_" ^ f.cf_name in
if not (is_method_defined ctx m static) then generate_self_method ctx rights m static true;
print ctx "%s $%s" rights (s_ident_field f.cf_name);
gen_assigned_value ctx f.cf_expr;
true
| _ ->
false)
| _ -> false) then
()
else begin
let name = if static then s_ident f.cf_name else f.cf_name in
if static then
(match f.cf_kind with
| Var _ ->
(match follow f.cf_type with
| TFun _
| TDynamic _ ->
print ctx "static function %s() { $args = func_get_args(); return call_user_func_array(self::$%s, $args); }" name name;
newline ctx;
| _ ->
()
)
| _ ->
()
);
print ctx "%s $%s" rights name;
gen_assigned_value ctx f.cf_expr
end
let generate_static_field_assign ctx path f =
let p = ctx.curclass.cl_pos in
if not ctx.curclass.cl_interface then
(match f.cf_expr with
| None -> ()
| Some e ->
match e.eexpr with
| TConst _ -> ()
| TFunction fd ->
(match f.cf_kind with
| Var _ when
(match follow f.cf_type with
| TFun _
| TDynamic _ ->
true;
| _ ->
false) ->
newline ctx;
print ctx "%s::$%s = " (s_path ctx path false p) (s_ident f.cf_name);
gen_value ctx e
| Method MethDynamic ->
newline ctx;
print ctx "%s::$%s = " (s_path ctx path false p) (s_ident f.cf_name);
gen_value ctx e
| _ -> ())
| _ when is_extern_field f ->
()
| _ ->
newline ctx;
print ctx "%s::$%s = " (s_path ctx path false p) (s_ident f.cf_name);
gen_value ctx e)
let rec super_has_dynamic c =
match c.cl_super with
| None -> false
| Some (csup, _) -> (match csup.cl_dynamic with
| Some _ -> true
| _ -> super_has_dynamic csup)
let generate_inline_method ctx c m =
(match ctx.inline_methods with
| [] -> ()
| h :: t -> ctx.inline_methods <- t
);
ctx.curclass <- c;
let old = save_locals ctx in
ctx.in_value <- Some m.iname;
ctx.in_block <- m.iin_block;
ctx.in_loop <- false;
ctx.locals <- m.ilocals;
ctx.inv_locals <- m.iinv_locals;
newline ctx;
print ctx "function %s(" m.iname;
(* arguments *)
let in_value = (match ctx.in_value with Some _ -> true | _ -> false) in
let arguments = remove_internals (argument_list_from_locals m.ihasthis in_value ctx.locals) in
let arguments = match arguments with
| [h] when h = "this" -> ["__hx__this"]
| h :: t when h = "this" -> "__hx__this" :: t
| _ -> arguments
in
let marguments = List.map (define_local ctx) m.iarguments in
let arguments = (List.map (fun a -> "&$" ^ a) arguments) @ (List.map (fun a -> "$" ^ a) marguments) in
(match arguments with
| [] -> ()
| l -> spr ctx (String.concat ", " arguments)
);
spr ctx ") {";
ctx.nested_loops <- ctx.nested_loops - 1;
let block = open_block ctx in
newline ctx;
gen_expr ctx m.iexpr;
block();
old();
ctx.nested_loops <- ctx.nested_loops + 1;
newline ctx;
spr ctx "}"
let generate_class ctx c =
let requires_constructor = ref true in
ctx.curclass <- c;
ctx.local_types <- List.map snd c.cl_params;
print ctx "%s %s " (if c.cl_interface then "interface" else "class") (s_path ctx c.cl_path c.cl_extern c.cl_pos);
(match c.cl_super with
| None -> ()
| Some (csup,_) ->
requires_constructor := false;
print ctx "extends %s " (s_path ctx csup.cl_path csup.cl_extern c.cl_pos));
let implements = ExtList.List.unique ~cmp:(fun a b -> (fst a).cl_path = (fst b).cl_path) c.cl_implements in
(match implements with
| [] -> ()
| l ->
spr ctx (if c.cl_interface then "extends " else "implements ");
concat ctx ", " (fun (i,_) ->
print ctx "%s" (s_path ctx i.cl_path i.cl_extern c.cl_pos)) l);
spr ctx "{";
let get_dynamic_methods = List.filter is_dynamic_method c.cl_ordered_fields in
if not ctx.curclass.cl_interface then ctx.dynamic_methods <- get_dynamic_methods;
let cl = open_block ctx in
(match c.cl_constructor with
| None ->
if !requires_constructor && not c.cl_interface then begin
newline ctx;
spr ctx "public function __construct(){}"
end;
| Some f ->
let f = { f with
cf_name = "__construct";
cf_public = true;
} in
ctx.constructor_block <- true;
generate_field ctx false f;
);
List.iter (generate_field ctx false) c.cl_ordered_fields;
(match c.cl_dynamic with
| Some _ when not c.cl_interface && not (super_has_dynamic c) ->
newline ctx;
spr ctx "public $__dynamics = array();\n\tpublic function __get($n) {\n\t\tif(isset($this->__dynamics[$n]))\n\t\t\treturn $this->__dynamics[$n];\n\t}\n\tpublic function __set($n, $v) {\n\t\t$this->__dynamics[$n] = $v;\n\t}\n\tpublic function __call($n, $a) {\n\t\tif(isset($this->__dynamics[$n]) && is_callable($this->__dynamics[$n]))\n\t\t\treturn call_user_func_array($this->__dynamics[$n], $a);\n\t\tif('toString' == $n)\n\t\t\treturn $this->__toString();\n\t\tthrow new HException(\"Unable to call <\".$n.\">\");\n\t}"
| Some _
| _ ->
if List.length ctx.dynamic_methods > 0 then begin
newline ctx;
spr ctx "public function __call($m, $a) {\n\t\tif(isset($this->$m) && is_callable($this->$m))\n\t\t\treturn call_user_func_array($this->$m, $a);\n\t\telse if(isset($this->__dynamics[$m]) && is_callable($this->__dynamics[$m]))\n\t\t\treturn call_user_func_array($this->__dynamics[$m], $a);\n\t\telse if('toString' == $m)\n\t\t\treturn $this->__toString();\n\t\telse\n\t\t\tthrow new HException('Unable to call <'.$m.'>');\n\t}";
end;
);
List.iter (generate_field ctx true) c.cl_ordered_statics;
let gen_props props =
String.concat "," (List.map (fun (p,v) -> "\"" ^ p ^ "\" => \"" ^ v ^ "\"") props)
in
let rec fields c =
let list = Codegen.get_properties (c.cl_ordered_statics @ c.cl_ordered_fields) in
match c.cl_super with
| Some (csup, _) ->
list @ fields csup
| None ->
list
in
if not c.cl_interface then (match fields c with
| [] ->
()
| props ->
newline ctx;
print ctx "static $__properties__ = array(%s)" (gen_props props);
);
cl();
newline ctx;
if PMap.exists "__toString" c.cl_fields then
()
else if PMap.exists "toString" c.cl_fields && (not c.cl_interface) && (not c.cl_extern) then begin
print ctx "\tfunction __toString() { return $this->toString(); }";
newline ctx
end else if (not c.cl_interface) && (not c.cl_extern) then begin
print ctx "\tfunction __toString() { return '%s'; }" (s_path_haxe c.cl_path) ;
newline ctx
end;
print ctx "}"
let createmain com e =
let filename = match com.php_front with None -> "index.php" | Some n -> n in
let ctx = {
com = com;
stack = stack_init com false;
tabs = "";
ch = open_out (com.file ^ "/" ^ filename);
path = ([], "");
buf = Buffer.create (1 lsl 14);
in_value = None;
in_loop = false;
in_instance_method = false;
imports = Hashtbl.create 0;
extern_required_paths = [];
extern_classes_with_init = [];
curclass = null_class;
curmethod = "";
locals = PMap.empty;
inv_locals = PMap.empty;
local_types = [];
inits = [];
constructor_block = false;
dynamic_methods = [];
all_dynamic_methods = [];
is_call = false;
cwd = "";
inline_methods = [];
nested_loops = 0;
inline_index = 0;
in_block = false;
lib_path = match com.php_lib with None -> "lib" | Some s -> s;
} in
spr ctx "if(version_compare(PHP_VERSION, '5.1.0', '<')) {
exit('Your current PHP version is: ' . PHP_VERSION . '. Haxe/PHP generates code for version 5.1.0 or later');
}";
newline ctx;
newline ctx;
spr ctx ("require_once dirname(__FILE__).'/" ^ ctx.lib_path ^ "/php/" ^ (prefix_class com "Boot.class.php';\n\n"));
gen_value ctx e;
newline ctx;
spr ctx "\n?>";
close ctx
let generate_main ctx c =
(match c.cl_ordered_statics with
| [{ cf_expr = Some e }] ->
gen_value ctx e;
| _ -> assert false);
newline ctx
let generate_enum ctx e =
ctx.local_types <- List.map snd e.e_params;
let pack = open_block ctx in
let ename = s_path ctx e.e_path e.e_extern e.e_pos in
print ctx "class %s extends Enum {" ename;
PMap.iter (fun _ c ->
newline ctx;
match c.ef_type with
| TFun (args,_) ->
print ctx "public static function %s($" (s_ident c.ef_name);
concat ctx ", $" (fun (a,o,t) ->
spr ctx a;
if o then spr ctx " = null";
) args;
spr ctx ") {";
print ctx " return new %s(\"%s\", %d, array($" ename (s_ident c.ef_name) c.ef_index;
concat ctx ", $" (fun (a,_,_) -> spr ctx a) args;
print ctx ")); }";
| _ ->
print ctx "public static $%s" (s_ident c.ef_name);
) e.e_constrs;
newline ctx;
spr ctx "public static $__constructors = array(";
let first = ref true in
PMap.iter (fun _ c ->
if not !first then spr ctx ", ";
print ctx "%d => '%s'" c.ef_index (s_ident c.ef_name);
first := false;
) e.e_constrs;
spr ctx ")";
newline ctx;
(match Codegen.build_metadata ctx.com (TEnumDecl e) with
| None -> ()
| Some _ ->
spr ctx "public static $__meta__";
newline ctx);
pack();
print ctx "}";
PMap.iter (fun _ c ->
match c.ef_type with
| TFun (args,_) ->
();
| _ ->
newline ctx;
print ctx "%s::$%s = new %s(\"%s\", %d)" ename (s_ident c.ef_name) ename c.ef_name c.ef_index;
) e.e_constrs;
newline ctx;
match Codegen.build_metadata ctx.com (TEnumDecl e) with
| None -> ()
| Some e ->
print ctx "%s::$__meta__ = " ename;
gen_expr ctx e;
newline ctx
let generate com =
let all_dynamic_methods = ref [] in
let extern_classes_with_init = ref [] in
let php_lib_path = (match com.php_lib with None -> "lib" | Some n -> n) in
create_directory com (Str.split (Str.regexp "/") php_lib_path);
(* check for methods with the same name but different case *)
let check_class_fields c =
let lc_names = ref [] in
let special_cases = ["toString"] in
let loop c lst static =
let in_special_cases name =
(List.exists (fun n -> String.lowercase n = name) (special_cases @ List.map (fun f -> f.cf_name) c.cl_overrides))
in
List.iter(fun cf ->
let name = String.lowercase cf.cf_name in
let prefixed_name s = (if s then "s_" else "i_") ^ name in
match cf.cf_kind, cf.cf_expr with
| (Method _, Some e) when not (in_special_cases name) ->
(try
let lc = List.find (fun n ->
let n = snd n in
if static then
(n = (prefixed_name false))
else
((n = (prefixed_name false)) || (n = (prefixed_name true)))
) !lc_names in
unsupported ("method '" ^ (s_type_path c.cl_path) ^ "." ^ cf.cf_name ^ "' already exists here '" ^ (fst lc) ^ "' (different case?)") c.cl_pos
with Not_found ->
lc_names := ((s_type_path c.cl_path) ^ "." ^ cf.cf_name, prefixed_name static) :: !lc_names)
| _ ->
()
) lst
in
let rec _check_class_fields cl =
(match cl.cl_super with
| Some (s,_) -> _check_class_fields s
| _ -> ());
loop cl cl.cl_ordered_statics true;
loop cl cl.cl_ordered_fields false
in
_check_class_fields c
in
List.iter (fun t ->
(match t with
| TClassDecl c ->
check_class_fields c
| TEnumDecl e ->
let e_names = ref [] in
List.iter(fun en -> (
if List.exists (fun n -> n = String.lowercase en) !e_names then
unsupported ("'" ^ en ^ "' constructor exists with different case") e.e_pos
else
e_names := (String.lowercase en) :: !e_names
)) (e.e_names)
| _ -> ())
) com.types;
List.iter (fun t ->
(match t with
| TClassDecl c ->
let dynamic_methods_names lst =
List.map (fun fd -> {
mpath = c.cl_path;
mname = fd.cf_name;
}) (List.filter is_dynamic_method lst)
in
all_dynamic_methods := dynamic_methods_names c.cl_ordered_fields @ !all_dynamic_methods;
if c.cl_extern then
(match c.cl_init with
| Some _ ->
extern_classes_with_init := c.cl_path :: !extern_classes_with_init;
| _ ->
())
else
all_dynamic_methods := dynamic_methods_names c.cl_ordered_statics @ !all_dynamic_methods;
| _ -> ())
) com.types;
List.iter (Codegen.fix_abstract_inheritance com) com.types;
List.iter (fun t ->
(match t with
| TClassDecl c ->
if c.cl_extern then begin
(match c.cl_init with
| None -> ()
| Some e ->
let ctx = init com php_lib_path c.cl_path 3 in
gen_expr ctx e;
newline ctx;
close ctx;
);
end else
let ctx = init com php_lib_path c.cl_path (if c.cl_interface then 2 else 0) in
ctx.extern_classes_with_init <- !extern_classes_with_init;
ctx.all_dynamic_methods <- !all_dynamic_methods;
generate_class ctx c;
(match c.cl_init with
| None -> ()
| Some e ->
newline ctx;
gen_expr ctx e);
List.iter (generate_static_field_assign ctx c.cl_path) c.cl_ordered_statics;
if c.cl_path = (["php"], "Boot") && com.debug then begin
newline ctx;
print ctx "$%s = new _hx_array(array())" ctx.stack.Codegen.stack_var;
newline ctx;
print ctx "$%s = new _hx_array(array())" ctx.stack.Codegen.stack_exc_var;
end;
let rec loop l =
match l with
| [] -> ()
| h :: _ ->
generate_inline_method ctx c h;
loop ctx.inline_methods
in
loop ctx.inline_methods;
newline ctx;
close ctx
| TEnumDecl e ->
if e.e_extern then
()
else
let ctx = init com php_lib_path e.e_path 1 in
generate_enum ctx e;
close ctx
| TTypeDecl _ | TAbstractDecl _ ->
());
) com.types;
(match com.main with
| None -> ()
| Some e -> createmain com e);
Hashtbl.iter (fun name data ->
write_resource com.file name data
) com.resources;
|