1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608
|
//------------------------------------------------------------------------------
// <copyright file="XmlIlVisitor.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Schema;
using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Xml.Xsl;
using System.Xml.Xsl.Qil;
using System.Xml.Xsl.Runtime;
namespace System.Xml.Xsl.IlGen {
using TypeFactory = System.Xml.Xsl.XmlQueryTypeFactory;
using Res = System.Xml.Utils.Res;
/// <summary>
/// Creates Msil code for an entire QilExpression graph. Code is generated in one of two modes: push or
/// pull. In push mode, code is generated to push the values in an iterator to the XmlWriter
/// interface. In pull mode, the values in an iterator are stored in a physical location such as
/// the stack or a local variable by an iterator. The iterator is passive, and will just wait for
/// a caller to pull the data and/or instruct the iterator to enumerate the next value.
/// </summary>
internal class XmlILVisitor : QilVisitor {
private QilExpression qil;
private GenerateHelper helper;
private IteratorDescriptor iterCurr, iterNested;
private int indexId;
//-----------------------------------------------
// Entry
//-----------------------------------------------
/// <summary>
/// Visits the specified QilExpression graph and generates MSIL code.
/// </summary>
public void Visit(QilExpression qil, GenerateHelper helper, MethodInfo methRoot) {
this.qil = qil;
this.helper = helper;
this.iterNested = null;
this.indexId = 0;
// Prepare each global parameter and global variable to be visited
PrepareGlobalValues(qil.GlobalParameterList);
PrepareGlobalValues(qil.GlobalVariableList);
// Visit each global parameter and global variable
VisitGlobalValues(qil.GlobalParameterList);
VisitGlobalValues(qil.GlobalVariableList);
// Build each function
foreach (QilFunction ndFunc in qil.FunctionList) {
// Visit each parameter and the function body
Function(ndFunc);
}
// Build the root expression
this.helper.MethodBegin(methRoot, null, true);
StartNestedIterator(qil.Root);
Visit(qil.Root);
Debug.Assert(this.iterCurr.Storage.Location == ItemLocation.None, "Root expression should have been pushed to the writer.");
EndNestedIterator(qil.Root);
this.helper.MethodEnd();
}
/// <summary>
/// Create IteratorDescriptor for each global value. This pre-visit is necessary because a global early
/// in the list may reference a global later in the list and therefore expect its IteratorDescriptor to already
/// be initialized.
/// </summary>
private void PrepareGlobalValues(QilList globalIterators) {
MethodInfo methGlobal;
IteratorDescriptor iterInfo;
foreach (QilIterator iter in globalIterators) {
Debug.Assert(iter.NodeType == QilNodeType.Let || iter.NodeType == QilNodeType.Parameter);
// Get metadata for method which computes this global's value
methGlobal = XmlILAnnotation.Write(iter).FunctionBinding;
Debug.Assert(methGlobal != null, "Metadata for global value should have already been computed");
// Create an IteratorDescriptor for this global value
iterInfo = new IteratorDescriptor(this.helper);
// Iterator items will be stored in a global location
iterInfo.Storage = StorageDescriptor.Global(methGlobal, GetItemStorageType(iter), !iter.XmlType.IsSingleton);
// Associate IteratorDescriptor with parameter
XmlILAnnotation.Write(iter).CachedIteratorDescriptor = iterInfo;
}
}
/// <summary>
/// Visit each global variable or parameter. Create a IteratorDescriptor for each global value. Generate code for
/// default values.
/// </summary>
private void VisitGlobalValues(QilList globalIterators) {
MethodInfo methGlobal;
Label lblGetGlobal, lblComputeGlobal;
bool isCached;
int idxValue;
foreach (QilIterator iter in globalIterators) {
QilParameter param = iter as QilParameter;
// Get MethodInfo for method that computes the value of this global
methGlobal = XmlILAnnotation.Write(iter).CachedIteratorDescriptor.Storage.GlobalLocation;
isCached = !iter.XmlType.IsSingleton;
// Notify the StaticDataManager of the new global value
idxValue = this.helper.StaticData.DeclareGlobalValue(iter.DebugName);
// Generate code for this method
this.helper.MethodBegin(methGlobal, iter.SourceLine, false);
lblGetGlobal = this.helper.DefineLabel();
lblComputeGlobal = this.helper.DefineLabel();
// if (runtime.IsGlobalComputed(idx)) goto LabelGetGlobal;
this.helper.LoadQueryRuntime();
this.helper.LoadInteger(idxValue);
this.helper.Call(XmlILMethods.GlobalComputed);
this.helper.Emit(OpCodes.Brtrue, lblGetGlobal);
// Compute value of global value
StartNestedIterator(iter);
if (param != null) {
Debug.Assert(iter.XmlType == TypeFactory.ItemS, "IlGen currently only supports parameters of type item*.");
// param = runtime.ExternalContext.GetParameter(localName, namespaceUri);
// if (param == null) goto LabelComputeGlobal;
LocalBuilder locParam = this.helper.DeclareLocal("$$$param", typeof(object));
this.helper.CallGetParameter(param.Name.LocalName, param.Name.NamespaceUri);
this.helper.Emit(OpCodes.Stloc, locParam);
this.helper.Emit(OpCodes.Ldloc, locParam);
this.helper.Emit(OpCodes.Brfalse, lblComputeGlobal);
// runtime.SetGlobalValue(idxValue, runtime.ChangeTypeXsltResult(idxType, value));
// Ensure that the storage type of the parameter corresponds to static type
this.helper.LoadQueryRuntime();
this.helper.LoadInteger(idxValue);
this.helper.LoadQueryRuntime();
this.helper.LoadInteger(this.helper.StaticData.DeclareXmlType(XmlQueryTypeFactory.ItemS));
this.helper.Emit(OpCodes.Ldloc, locParam);
this.helper.Call(XmlILMethods.ChangeTypeXsltResult);
this.helper.CallSetGlobalValue(typeof(object));
// goto LabelGetGlobal;
this.helper.EmitUnconditionalBranch(OpCodes.Br, lblGetGlobal);
}
// LabelComputeGlobal:
this.helper.MarkLabel(lblComputeGlobal);
if (iter.Binding != null) {
// runtime.SetGlobalValue(idxValue, (object) value);
this.helper.LoadQueryRuntime();
this.helper.LoadInteger(idxValue);
// Compute value of global value
NestedVisitEnsureStack(iter.Binding, GetItemStorageType(iter), isCached);
this.helper.CallSetGlobalValue(GetStorageType(iter));
}
else {
// Throw exception, as there is no default value for this parameter
// XmlQueryRuntime.ThrowException("...");
Debug.Assert(iter.NodeType == QilNodeType.Parameter, "Only parameters may not have a default value");
this.helper.LoadQueryRuntime();
this.helper.Emit(OpCodes.Ldstr, Res.GetString(Res.XmlIl_UnknownParam, new string[] {param.Name.LocalName, param.Name.NamespaceUri}));
this.helper.Call(XmlILMethods.ThrowException);
}
EndNestedIterator(iter);
// LabelGetGlobal:
// return (T) runtime.GetGlobalValue(idxValue);
this.helper.MarkLabel(lblGetGlobal);
this.helper.CallGetGlobalValue(idxValue, GetStorageType(iter));
this.helper.MethodEnd();
}
}
/// <summary>
/// Generate code for the specified function.
/// </summary>
private void Function(QilFunction ndFunc) {
MethodInfo methFunc;
int paramId;
IteratorDescriptor iterInfo;
bool useWriter;
// Annotate each function parameter with a IteratorDescriptor
foreach (QilIterator iter in ndFunc.Arguments) {
Debug.Assert(iter.NodeType == QilNodeType.Parameter);
// Create an IteratorDescriptor for this parameter
iterInfo = new IteratorDescriptor(this.helper);
// Add one to parameter index, as 0th parameter is always "this"
paramId = XmlILAnnotation.Write(iter).ArgumentPosition + 1;
// The ParameterInfo for each argument should be set as its location
iterInfo.Storage = StorageDescriptor.Parameter(paramId, GetItemStorageType(iter), !iter.XmlType.IsSingleton);
// Associate IteratorDescriptor with Let iterator
XmlILAnnotation.Write(iter).CachedIteratorDescriptor = iterInfo;
}
methFunc = XmlILAnnotation.Write(ndFunc).FunctionBinding;
useWriter = (XmlILConstructInfo.Read(ndFunc).ConstructMethod == XmlILConstructMethod.Writer);
// Generate query code from QilExpression tree
this.helper.MethodBegin(methFunc, ndFunc.SourceLine, useWriter);
foreach (QilIterator iter in ndFunc.Arguments) {
// DebugInfo: Sequence point just before generating code for the bound expression
if (this.qil.IsDebug && iter.SourceLine != null)
this.helper.DebugSequencePoint(iter.SourceLine);
// Calculate default value of this parameter
if (iter.Binding != null) {
Debug.Assert(iter.XmlType == TypeFactory.ItemS, "IlGen currently only supports default values in parameters of type item*.");
paramId = (iter.Annotation as XmlILAnnotation).ArgumentPosition + 1;
// runtime.MatchesXmlType(param, XmlTypeCode.QName);
Label lblLocalComputed = this.helper.DefineLabel();
this.helper.LoadQueryRuntime();
this.helper.LoadParameter(paramId);
this.helper.LoadInteger((int)XmlTypeCode.QName);
this.helper.Call(XmlILMethods.SeqMatchesCode);
this.helper.Emit(OpCodes.Brfalse, lblLocalComputed);
// Compute default value of this parameter
StartNestedIterator(iter);
NestedVisitEnsureStack(iter.Binding, GetItemStorageType(iter), /*isCached:*/!iter.XmlType.IsSingleton);
EndNestedIterator(iter);
this.helper.SetParameter(paramId);
this.helper.MarkLabel(lblLocalComputed);
}
}
StartNestedIterator(ndFunc);
// If function did not push results to writer, then function will return value(s) (rather than void)
if (useWriter)
NestedVisit(ndFunc.Definition);
else
NestedVisitEnsureStack(ndFunc.Definition, GetItemStorageType(ndFunc), !ndFunc.XmlType.IsSingleton);
EndNestedIterator(ndFunc);
this.helper.MethodEnd();
}
//-----------------------------------------------
// QilVisitor
//-----------------------------------------------
/// <summary>
/// Generate a query plan for the QilExpression subgraph.
/// </summary>
protected override QilNode Visit(QilNode nd) {
if (nd == null)
return null;
// DebugInfo: Sequence point just before generating code for this expression
if (this.qil.IsDebug && nd.SourceLine != null && !(nd is QilIterator))
this.helper.DebugSequencePoint(nd.SourceLine);
// Expressions are constructed using one of several possible methods
switch (XmlILConstructInfo.Read(nd).ConstructMethod) {
case XmlILConstructMethod.WriterThenIterator:
// Push results of expression to cached writer; then iterate over cached results
NestedConstruction(nd);
break;
case XmlILConstructMethod.IteratorThenWriter:
// Iterate over items in the sequence; send items to writer
CopySequence(nd);
break;
case XmlILConstructMethod.Iterator:
Debug.Assert(nd.XmlType.IsSingleton || CachesResult(nd) || this.iterCurr.HasLabelNext,
"When generating code for a non-singleton expression, LabelNext must be defined.");
goto default;
default:
// Allow base internal class to dispatch to correct Visit method
base.Visit(nd);
break;
}
return nd;
}
/// <summary>
/// VisitChildren should never be called.
/// </summary>
protected override QilNode VisitChildren(QilNode parent) {
Debug.Fail("Visit" + parent.NodeType + " should never be called");
return parent;
}
/// <summary>
/// Generate code to cache a sequence of items that are pushed to output.
/// </summary>
private void NestedConstruction(QilNode nd) {
// Start nested construction of a sequence of items
this.helper.CallStartSequenceConstruction();
// Allow base internal class to dispatch to correct Visit method
base.Visit(nd);
// Get the result sequence
this.helper.CallEndSequenceConstruction();
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(XPathItem), true);
}
/// <summary>
/// Iterate over items produced by the "nd" expression and copy each item to output.
/// </summary>
private void CopySequence(QilNode nd) {
XmlQueryType typ = nd.XmlType;
bool hasOnEnd;
Label lblOnEnd;
StartWriterLoop(nd, out hasOnEnd, out lblOnEnd);
if (typ.IsSingleton) {
// Always write atomic values via XmlQueryOutput
this.helper.LoadQueryOutput();
// Allow base internal class to dispatch to correct Visit method
base.Visit(nd);
this.iterCurr.EnsureItemStorageType(nd.XmlType, typeof(XPathItem));
}
else {
// Allow base internal class to dispatch to correct Visit method
base.Visit(nd);
this.iterCurr.EnsureItemStorageType(nd.XmlType, typeof(XPathItem));
// Save any stack values in a temporary local
this.iterCurr.EnsureNoStackNoCache("$$$copyTemp");
this.helper.LoadQueryOutput();
}
// Write value to output
this.iterCurr.EnsureStackNoCache();
this.helper.Call(XmlILMethods.WriteItem);
EndWriterLoop(nd, hasOnEnd, lblOnEnd);
}
/// <summary>
/// Generate code for QilNodeType.DataSource.
/// </summary>
/// <remarks>
/// Generates code to retrieve a document using the XmlResolver.
/// </remarks>
protected override QilNode VisitDataSource(QilDataSource ndSrc) {
LocalBuilder locNav;
// XPathNavigator navDoc = runtime.ExternalContext.GetEntity(uri)
this.helper.LoadQueryContext();
NestedVisitEnsureStack(ndSrc.Name);
NestedVisitEnsureStack(ndSrc.BaseUri);
this.helper.Call(XmlILMethods.GetDataSource);
locNav = this.helper.DeclareLocal("$$$navDoc", typeof(XPathNavigator));
this.helper.Emit(OpCodes.Stloc, locNav);
// if (navDoc == null) goto LabelNextCtxt;
this.helper.Emit(OpCodes.Ldloc, locNav);
this.helper.Emit(OpCodes.Brfalse, this.iterCurr.GetLabelNext());
this.iterCurr.Storage = StorageDescriptor.Local(locNav, typeof(XPathNavigator), false);
return ndSrc;
}
/// <summary>
/// Generate code for QilNodeType.Nop.
/// </summary>
protected override QilNode VisitNop(QilUnary ndNop) {
return Visit(ndNop.Child);
}
/// <summary>
/// Generate code for QilNodeType.OptimizeBarrier.
/// </summary>
protected override QilNode VisitOptimizeBarrier(QilUnary ndBarrier) {
return Visit(ndBarrier.Child);
}
/// <summary>
/// Generate code for QilNodeType.Error.
/// </summary>
protected override QilNode VisitError(QilUnary ndErr) {
// XmlQueryRuntime.ThrowException(strErr);
this.helper.LoadQueryRuntime();
NestedVisitEnsureStack(ndErr.Child);
this.helper.Call(XmlILMethods.ThrowException);
if (XmlILConstructInfo.Read(ndErr).ConstructMethod == XmlILConstructMethod.Writer) {
this.iterCurr.Storage = StorageDescriptor.None();
}
else {
// Push dummy value so that Location is not None and IL rules are met
this.helper.Emit(OpCodes.Ldnull);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(XPathItem), false);
}
return ndErr;
}
/// <summary>
/// Generate code for QilNodeType.Warning.
/// </summary>
protected override QilNode VisitWarning(QilUnary ndWarning) {
// runtime.SendMessage(strWarning);
this.helper.LoadQueryRuntime();
NestedVisitEnsureStack(ndWarning.Child);
this.helper.Call(XmlILMethods.SendMessage);
if (XmlILConstructInfo.Read(ndWarning).ConstructMethod == XmlILConstructMethod.Writer)
this.iterCurr.Storage = StorageDescriptor.None();
else
VisitEmpty(ndWarning);
return ndWarning;
}
/// <summary>
/// Generate code for QilNodeType.True.
/// </summary>
/// <remarks>
/// BranchingContext.OnFalse context: [nothing]
/// BranchingContext.OnTrue context: goto LabelParent;
/// BranchingContext.None context: push true();
/// </remarks>
protected override QilNode VisitTrue(QilNode ndTrue) {
if (this.iterCurr.CurrentBranchingContext != BranchingContext.None) {
// Make sure there's an IL code path to both the true and false branches in order to avoid dead
// code which can cause IL verification errors.
this.helper.EmitUnconditionalBranch(this.iterCurr.CurrentBranchingContext == BranchingContext.OnTrue ?
OpCodes.Brtrue : OpCodes.Brfalse, this.iterCurr.LabelBranch);
this.iterCurr.Storage = StorageDescriptor.None();
}
else {
// Push boolean result onto the stack
this.helper.LoadBoolean(true);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(bool), false);
}
return ndTrue;
}
/// <summary>
/// Generate code for QilNodeType.False.
/// </summary>
/// <remarks>
/// BranchingContext.OnFalse context: goto LabelParent;
/// BranchingContext.OnTrue context: [nothing]
/// BranchingContext.None context: push false();
/// </remarks>
protected override QilNode VisitFalse(QilNode ndFalse) {
if (this.iterCurr.CurrentBranchingContext != BranchingContext.None) {
// Make sure there's an IL code path to both the true and false branches in order to avoid dead
// code which can cause IL verification errors.
this.helper.EmitUnconditionalBranch(this.iterCurr.CurrentBranchingContext == BranchingContext.OnFalse ?
OpCodes.Brtrue : OpCodes.Brfalse, this.iterCurr.LabelBranch);
this.iterCurr.Storage = StorageDescriptor.None();
}
else {
// Push boolean result onto the stack
this.helper.LoadBoolean(false);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(bool), false);
}
return ndFalse;
}
/// <summary>
/// Generate code for QilNodeType.LiteralString.
/// </summary>
protected override QilNode VisitLiteralString(QilLiteral ndStr) {
this.helper.Emit(OpCodes.Ldstr, (string) ndStr);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(string), false);
return ndStr;
}
/// <summary>
/// Generate code for QilNodeType.LiteralInt32.
/// </summary>
protected override QilNode VisitLiteralInt32(QilLiteral ndInt) {
this.helper.LoadInteger((int) ndInt);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(int), false);
return ndInt;
}
/// <summary>
/// Generate code for QilNodeType.LiteralInt64.
/// </summary>
protected override QilNode VisitLiteralInt64(QilLiteral ndLong) {
this.helper.Emit(OpCodes.Ldc_I8, (long) ndLong);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(long), false);
return ndLong;
}
/// <summary>
/// Generate code for QilNodeType.LiteralDouble.
/// </summary>
protected override QilNode VisitLiteralDouble(QilLiteral ndDbl) {
this.helper.Emit(OpCodes.Ldc_R8, (double) ndDbl);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(double), false);
return ndDbl;
}
/// <summary>
/// Generate code for QilNodeType.LiteralDecimal.
/// </summary>
protected override QilNode VisitLiteralDecimal(QilLiteral ndDec) {
this.helper.ConstructLiteralDecimal((decimal) ndDec);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(decimal), false);
return ndDec;
}
/// <summary>
/// Generate code for QilNodeType.LiteralQName.
/// </summary>
protected override QilNode VisitLiteralQName(QilName ndQName) {
this.helper.ConstructLiteralQName(ndQName.LocalName, ndQName.NamespaceUri);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(XmlQualifiedName), false);
return ndQName;
}
/// <summary>
/// Generate code for QilNodeType.And.
/// </summary>
/// <remarks>
/// BranchingContext.OnFalse context: (expr1) and (expr2)
/// ==> if (!expr1) goto LabelParent;
/// if (!expr2) goto LabelParent;
///
/// BranchingContext.OnTrue context: (expr1) and (expr2)
/// ==> if (!expr1) goto LabelTemp;
/// if (expr1) goto LabelParent;
/// LabelTemp:
///
/// BranchingContext.None context: (expr1) and (expr2)
/// ==> if (!expr1) goto LabelTemp;
/// if (!expr1) goto LabelTemp;
/// push true();
/// goto LabelSkip;
/// LabelTemp:
/// push false();
/// LabelSkip:
///
/// </remarks>
protected override QilNode VisitAnd(QilBinary ndAnd) {
IteratorDescriptor iterParent = this.iterCurr;
Label lblOnFalse;
// Visit left branch
StartNestedIterator(ndAnd.Left);
lblOnFalse = StartConjunctiveTests(iterParent.CurrentBranchingContext, iterParent.LabelBranch);
Visit(ndAnd.Left);
EndNestedIterator(ndAnd.Left);
// Visit right branch
StartNestedIterator(ndAnd.Right);
StartLastConjunctiveTest(iterParent.CurrentBranchingContext, iterParent.LabelBranch, lblOnFalse);
Visit(ndAnd.Right);
EndNestedIterator(ndAnd.Right);
// End And expression
EndConjunctiveTests(iterParent.CurrentBranchingContext, iterParent.LabelBranch, lblOnFalse);
return ndAnd;
}
/// <summary>
/// Fixup branching context for all but the last test in a conjunctive (Logical And) expression.
/// Return a temporary label which will be passed to StartLastAndBranch() and EndAndBranch().
/// </summary>
private Label StartConjunctiveTests(BranchingContext brctxt, Label lblBranch) {
Label lblOnFalse;
switch (brctxt) {
case BranchingContext.OnFalse:
// If condition evaluates to false, branch to false label
this.iterCurr.SetBranching(BranchingContext.OnFalse, lblBranch);
return lblBranch;
default:
// If condition evaluates to false:
// 1. Jump to new false label that will be fixed just beyond the second condition
// 2. Or, jump to code that pushes "false"
lblOnFalse = this.helper.DefineLabel();
this.iterCurr.SetBranching(BranchingContext.OnFalse, lblOnFalse);
return lblOnFalse;
}
}
/// <summary>
/// Fixup branching context for the last test in a conjunctive (Logical And) expression.
/// </summary>
private void StartLastConjunctiveTest(BranchingContext brctxt, Label lblBranch, Label lblOnFalse) {
switch (brctxt) {
case BranchingContext.OnTrue:
// If last condition evaluates to true, branch to true label
this.iterCurr.SetBranching(BranchingContext.OnTrue, lblBranch);
break;
default:
// If last condition evalutes to false, branch to false label
// Else fall through to true code path
this.iterCurr.SetBranching(BranchingContext.OnFalse, lblOnFalse);
break;
}
}
/// <summary>
/// Anchor any remaining labels.
/// </summary>
private void EndConjunctiveTests(BranchingContext brctxt, Label lblBranch, Label lblOnFalse) {
switch (brctxt) {
case BranchingContext.OnTrue:
// Anchor false label
this.helper.MarkLabel(lblOnFalse);
goto case BranchingContext.OnFalse;
case BranchingContext.OnFalse:
this.iterCurr.Storage = StorageDescriptor.None();
break;
case BranchingContext.None:
// Convert branch targets into push of true/false
this.helper.ConvBranchToBool(lblOnFalse, false);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(bool), false);
break;
}
}
/// <summary>
/// Generate code for QilNodeType.Or.
/// </summary>
/// <remarks>
/// BranchingContext.OnFalse context: (expr1) or (expr2)
/// ==> if (expr1) goto LabelTemp;
/// if (!expr2) goto LabelParent;
/// LabelTemp:
///
/// BranchingContext.OnTrue context: (expr1) or (expr2)
/// ==> if (expr1) goto LabelParent;
/// if (expr1) goto LabelParent;
///
/// BranchingContext.None context: (expr1) or (expr2)
/// ==> if (expr1) goto LabelTemp;
/// if (expr1) goto LabelTemp;
/// push false();
/// goto LabelSkip;
/// LabelTemp:
/// push true();
/// LabelSkip:
///
/// </remarks>
protected override QilNode VisitOr(QilBinary ndOr) {
Label lblTemp = new Label();
// Visit left branch
switch (this.iterCurr.CurrentBranchingContext) {
case BranchingContext.OnFalse:
// If left condition evaluates to true, jump to new label that will be fixed
// just beyond the second condition
lblTemp = this.helper.DefineLabel();
NestedVisitWithBranch(ndOr.Left, BranchingContext.OnTrue, lblTemp);
break;
case BranchingContext.OnTrue:
// If left condition evaluates to true, branch to true label
NestedVisitWithBranch(ndOr.Left, BranchingContext.OnTrue, this.iterCurr.LabelBranch);
break;
default:
// If left condition evalutes to true, jump to code that pushes "true"
Debug.Assert(this.iterCurr.CurrentBranchingContext == BranchingContext.None);
lblTemp = this.helper.DefineLabel();
NestedVisitWithBranch(ndOr.Left, BranchingContext.OnTrue, lblTemp);
break;
}
// Visit right branch
switch (this.iterCurr.CurrentBranchingContext) {
case BranchingContext.OnFalse:
// If right condition evaluates to false, branch to false label
NestedVisitWithBranch(ndOr.Right, BranchingContext.OnFalse, this.iterCurr.LabelBranch);
break;
case BranchingContext.OnTrue:
// If right condition evaluates to true, branch to true label
NestedVisitWithBranch(ndOr.Right, BranchingContext.OnTrue, this.iterCurr.LabelBranch);
break;
default:
// If right condition evalutes to true, jump to code that pushes "true".
// Otherwise, if both conditions evaluate to false, fall through code path
// will push "false".
NestedVisitWithBranch(ndOr.Right, BranchingContext.OnTrue, lblTemp);
break;
}
switch (this.iterCurr.CurrentBranchingContext) {
case BranchingContext.OnFalse:
// Anchor true label
this.helper.MarkLabel(lblTemp);
goto case BranchingContext.OnTrue;
case BranchingContext.OnTrue:
this.iterCurr.Storage = StorageDescriptor.None();
break;
case BranchingContext.None:
// Convert branch targets into push of true/false
this.helper.ConvBranchToBool(lblTemp, true);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(bool), false);
break;
}
return ndOr;
}
/// <summary>
/// Generate code for QilNodeType.Not.
/// </summary>
/// <remarks>
/// BranchingContext.OnFalse context: not(expr1)
/// ==> if (expr1) goto LabelParent;
///
/// BranchingContext.OnTrue context: not(expr1)
/// ==> if (!expr1) goto LabelParent;
///
/// BranchingContext.None context: not(expr1)
/// ==> if (expr1) goto LabelTemp;
/// push false();
/// goto LabelSkip;
/// LabelTemp:
/// push true();
/// LabelSkip:
///
/// </remarks>
protected override QilNode VisitNot(QilUnary ndNot) {
Label lblTemp = new Label();
// Visit operand
// Reverse branch types
switch (this.iterCurr.CurrentBranchingContext) {
case BranchingContext.OnFalse:
NestedVisitWithBranch(ndNot.Child, BranchingContext.OnTrue, this.iterCurr.LabelBranch);
break;
case BranchingContext.OnTrue:
NestedVisitWithBranch(ndNot.Child, BranchingContext.OnFalse, this.iterCurr.LabelBranch);
break;
default:
// Replace boolean argument on top of stack with its inverse
Debug.Assert(this.iterCurr.CurrentBranchingContext == BranchingContext.None);
lblTemp = this.helper.DefineLabel();
NestedVisitWithBranch(ndNot.Child, BranchingContext.OnTrue, lblTemp);
break;
}
if (this.iterCurr.CurrentBranchingContext == BranchingContext.None) {
// If condition evaluates to true, then jump to code that pushes false
this.helper.ConvBranchToBool(lblTemp, false);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(bool), false);
}
else {
this.iterCurr.Storage = StorageDescriptor.None();
}
return ndNot;
}
/// <summary>
/// Generate code for QilNodeType.Conditional.
/// </summary>
protected override QilNode VisitConditional(QilTernary ndCond) {
XmlILConstructInfo info = XmlILConstructInfo.Read(ndCond);
if (info.ConstructMethod == XmlILConstructMethod.Writer) {
Label lblFalse, lblDone;
// Evaluate if test
lblFalse = this.helper.DefineLabel();
NestedVisitWithBranch(ndCond.Left, BranchingContext.OnFalse, lblFalse);
// Generate true branch code
NestedVisit(ndCond.Center);
// Generate false branch code. If false branch is the empty list,
if (ndCond.Right.NodeType == QilNodeType.Sequence && ndCond.Right.Count == 0) {
// Then generate simplified code that doesn't contain a false branch
this.helper.MarkLabel(lblFalse);
NestedVisit(ndCond.Right);
}
else {
// Jump past false branch
lblDone = this.helper.DefineLabel();
this.helper.EmitUnconditionalBranch(OpCodes.Br, lblDone);
// Generate false branch code
this.helper.MarkLabel(lblFalse);
NestedVisit(ndCond.Right);
this.helper.MarkLabel(lblDone);
}
this.iterCurr.Storage = StorageDescriptor.None();
}
else {
IteratorDescriptor iterInfoTrue;
LocalBuilder locBool = null, locCond = null;
Label lblFalse, lblDone, lblNext;
Type itemStorageType = GetItemStorageType(ndCond);
Debug.Assert(info.ConstructMethod == XmlILConstructMethod.Iterator);
// Evaluate conditional test -- save boolean result in boolResult
Debug.Assert(ndCond.Left.XmlType.TypeCode == XmlTypeCode.Boolean);
lblFalse = this.helper.DefineLabel();
if (ndCond.XmlType.IsSingleton) {
// if (!bool-expr) goto LabelFalse;
NestedVisitWithBranch(ndCond.Left, BranchingContext.OnFalse, lblFalse);
}
else {
// CondType itemCond;
// int boolResult = bool-expr;
locCond = this.helper.DeclareLocal("$$$cond", itemStorageType);
locBool = this.helper.DeclareLocal("$$$boolResult", typeof(bool));
NestedVisitEnsureLocal(ndCond.Left, locBool);
// if (!boolResult) goto LabelFalse;
this.helper.Emit(OpCodes.Ldloc, locBool);
this.helper.Emit(OpCodes.Brfalse, lblFalse);
}
// Generate code for true branch
ConditionalBranch(ndCond.Center, itemStorageType, locCond);
iterInfoTrue = this.iterNested;
// goto LabelDone;
lblDone = this.helper.DefineLabel();
this.helper.EmitUnconditionalBranch(OpCodes.Br, lblDone);
// Generate code for false branch
// LabelFalse:
this.helper.MarkLabel(lblFalse);
ConditionalBranch(ndCond.Right, itemStorageType, locCond);
// If conditional is not cardinality one, then need to iterate through all values
if (!ndCond.XmlType.IsSingleton) {
Debug.Assert(!ndCond.Center.XmlType.IsSingleton || !ndCond.Right.XmlType.IsSingleton);
// IL's rules do not allow OpCodes.Br here
// goto LabelDone;
this.helper.EmitUnconditionalBranch(OpCodes.Brtrue, lblDone);
// LabelNext:
lblNext = this.helper.DefineLabel();
this.helper.MarkLabel(lblNext);
// if (boolResult) goto LabelNextTrue else goto LabelNextFalse;
this.helper.Emit(OpCodes.Ldloc, locBool);
this.helper.Emit(OpCodes.Brtrue, iterInfoTrue.GetLabelNext());
this.helper.EmitUnconditionalBranch(OpCodes.Br, this.iterNested.GetLabelNext());
this.iterCurr.SetIterator(lblNext, StorageDescriptor.Local(locCond, itemStorageType, false));
}
// LabelDone:
this.helper.MarkLabel(lblDone);
}
return ndCond;
}
/// <summary>
/// Generate code for one of the branches of QilNodeType.Conditional.
/// </summary>
private void ConditionalBranch(QilNode ndBranch, Type itemStorageType, LocalBuilder locResult) {
if (locResult == null) {
Debug.Assert(ndBranch.XmlType.IsSingleton, "Conditional must produce a singleton");
// If in a branching context, then inherit branch target from parent context
if (this.iterCurr.IsBranching) {
Debug.Assert(itemStorageType == typeof(bool));
NestedVisitWithBranch(ndBranch, this.iterCurr.CurrentBranchingContext, this.iterCurr.LabelBranch);
}
else {
NestedVisitEnsureStack(ndBranch, itemStorageType, false);
}
}
else {
// Link nested iterator to parent conditional's iterator
NestedVisit(ndBranch, this.iterCurr.GetLabelNext());
this.iterCurr.EnsureItemStorageType(ndBranch.XmlType, itemStorageType);
this.iterCurr.EnsureLocalNoCache(locResult);
}
}
/// <summary>
/// Generate code for QilNodeType.Choice.
/// </summary>
protected override QilNode VisitChoice(QilChoice ndChoice) {
QilNode ndBranches;
Label[] switchLabels;
Label lblOtherwise, lblDone;
int regBranches, idx;
Debug.Assert(XmlILConstructInfo.Read(ndChoice).PushToWriterFirst);
// Evaluate the expression
NestedVisit(ndChoice.Expression);
// Generate switching code
ndBranches = ndChoice.Branches;
regBranches = ndBranches.Count - 1;
switchLabels = new Label[regBranches];
for (idx = 0; idx < regBranches; idx++)
switchLabels[idx] = this.helper.DefineLabel();
lblOtherwise = this.helper.DefineLabel();
lblDone = this.helper.DefineLabel();
// switch (value)
// case 0: goto Label[0];
// ...
// case N-1: goto Label[N-1];
// default: goto LabelOtherwise;
this.helper.Emit(OpCodes.Switch, switchLabels);
this.helper.EmitUnconditionalBranch(OpCodes.Br, lblOtherwise);
for (idx = 0; idx < regBranches; idx++) {
// Label[i]:
this.helper.MarkLabel(switchLabels[idx]);
// Generate regular branch code
NestedVisit(ndBranches[idx]);
// goto LabelDone
this.helper.EmitUnconditionalBranch(OpCodes.Br, lblDone);
}
// LabelOtherwise:
this.helper.MarkLabel(lblOtherwise);
// Generate otherwise branch code
NestedVisit(ndBranches[idx]);
// LabelDone:
this.helper.MarkLabel(lblDone);
this.iterCurr.Storage = StorageDescriptor.None();
return ndChoice;
}
/// <summary>
/// Generate code for QilNodeType.Length.
/// </summary>
/// <remarks>
/// int length = 0;
/// foreach (item in expr)
/// length++;
/// </remarks>
protected override QilNode VisitLength(QilUnary ndSetLen) {
Label lblOnEnd = this.helper.DefineLabel();
OptimizerPatterns patt = OptimizerPatterns.Read(ndSetLen);
if (CachesResult(ndSetLen.Child)) {
NestedVisitEnsureStack(ndSetLen.Child);
this.helper.CallCacheCount(this.iterNested.Storage.ItemStorageType);
}
else {
// length = 0;
this.helper.Emit(OpCodes.Ldc_I4_0);
StartNestedIterator(ndSetLen.Child, lblOnEnd);
// foreach (item in expr) {
Visit(ndSetLen.Child);
// Pop values of SetLength expression from the stack if necessary
this.iterCurr.EnsureNoCache();
this.iterCurr.DiscardStack();
// length++;
this.helper.Emit(OpCodes.Ldc_I4_1);
this.helper.Emit(OpCodes.Add);
if (patt.MatchesPattern(OptimizerPatternName.MaxPosition)) {
// Short-circuit rest of loop if max position has been exceeded
this.helper.Emit(OpCodes.Dup);
this.helper.LoadInteger((int) patt.GetArgument(OptimizerPatternArgument.MaxPosition));
this.helper.Emit(OpCodes.Bgt, lblOnEnd);
}
// }
this.iterCurr.LoopToEnd(lblOnEnd);
EndNestedIterator(ndSetLen.Child);
}
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(int), false);
return ndSetLen;
}
/// <summary>
/// Find physical query plan for QilNodeType.Sequence.
/// </summary>
protected override QilNode VisitSequence(QilList ndSeq) {
if (XmlILConstructInfo.Read(ndSeq).ConstructMethod == XmlILConstructMethod.Writer) {
// Push each item in the list to output
foreach (QilNode nd in ndSeq)
NestedVisit(nd);
}
else {
// Empty sequence is special case
if (ndSeq.Count == 0)
VisitEmpty(ndSeq);
else
Sequence(ndSeq);
}
return ndSeq;
}
/// <summary>
/// Generate code for the empty sequence.
/// </summary>
private void VisitEmpty(QilNode nd) {
Debug.Assert(XmlILConstructInfo.Read(nd).PullFromIteratorFirst, "VisitEmpty should only be called if items are iterated");
// IL's rules prevent OpCodes.Br here
// Empty sequence
this.helper.EmitUnconditionalBranch(OpCodes.Brtrue, this.iterCurr.GetLabelNext());
// Push dummy value so that Location is not None and IL rules are met
this.helper.Emit(OpCodes.Ldnull);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(XPathItem), false);
}
/// <summary>
/// Generate code for QilNodeType.Sequence, when sort-merging to retain document order is not necessary.
/// </summary>
private void Sequence(QilList ndSeq) {
LocalBuilder locIdx, locList;
Label lblStart, lblNext, lblOnEnd = new Label();
Label[] arrSwitchLabels;
int i;
Type itemStorageType = GetItemStorageType(ndSeq);
Debug.Assert(XmlILConstructInfo.Read(ndSeq).ConstructMethod == XmlILConstructMethod.Iterator, "This method should only be called if items in list are pulled from a code iterator.");
// Singleton list is a special case if in addition to the singleton there are warnings or errors which should be executed
if (ndSeq.XmlType.IsSingleton) {
foreach (QilNode nd in ndSeq) {
// Generate nested iterator's code
if (nd.XmlType.IsSingleton) {
NestedVisitEnsureStack(nd);
}
else {
lblOnEnd = this.helper.DefineLabel();
NestedVisit(nd, lblOnEnd);
this.iterCurr.DiscardStack();
this.helper.MarkLabel(lblOnEnd);
}
}
this.iterCurr.Storage = StorageDescriptor.Stack(itemStorageType, false);
}
else {
// Type itemList;
// int idxList;
locList = this.helper.DeclareLocal("$$$itemList", itemStorageType);
locIdx = this.helper.DeclareLocal("$$$idxList", typeof(int));
arrSwitchLabels = new Label[ndSeq.Count];
lblStart = this.helper.DefineLabel();
for (i = 0; i < ndSeq.Count; i++) {
// LabelOnEnd[i - 1]:
// When previous nested iterator is exhausted, it should jump to this (the next) iterator
if (i != 0)
this.helper.MarkLabel(lblOnEnd);
// Create new LabelOnEnd for all but the last iterator, which jumps back to parent iterator when exhausted
if (i == ndSeq.Count - 1)
lblOnEnd = this.iterCurr.GetLabelNext();
else
lblOnEnd = this.helper.DefineLabel();
// idxList = [i];
this.helper.LoadInteger(i);
this.helper.Emit(OpCodes.Stloc, locIdx);
// Generate nested iterator's code
NestedVisit(ndSeq[i], lblOnEnd);
// Result of list should be saved to a common type and location
this.iterCurr.EnsureItemStorageType(ndSeq[i].XmlType, itemStorageType);
this.iterCurr.EnsureLocalNoCache(locList);
// Switch statement will jump to nested iterator's LabelNext
arrSwitchLabels[i] = this.iterNested.GetLabelNext();
// IL's rules prevent OpCodes.Br here
// goto LabelStart;
this.helper.EmitUnconditionalBranch(OpCodes.Brtrue, lblStart);
}
// LabelNext:
lblNext = this.helper.DefineLabel();
this.helper.MarkLabel(lblNext);
// switch (idxList)
// case 0: goto LabelNext1;
// ...
// case N-1: goto LabelNext[N];
this.helper.Emit(OpCodes.Ldloc, locIdx);
this.helper.Emit(OpCodes.Switch, arrSwitchLabels);
// LabelStart:
this.helper.MarkLabel(lblStart);
this.iterCurr.SetIterator(lblNext, StorageDescriptor.Local(locList, itemStorageType, false));
}
}
/// <summary>
/// Generate code for QilNodeType.Union.
/// </summary>
protected override QilNode VisitUnion(QilBinary ndUnion) {
return CreateSetIterator(ndUnion, "$$$iterUnion", typeof(UnionIterator), XmlILMethods.UnionCreate, XmlILMethods.UnionNext);
}
/// <summary>
/// Generate code for QilNodeType.Intersection.
/// </summary>
protected override QilNode VisitIntersection(QilBinary ndInter) {
return CreateSetIterator(ndInter, "$$$iterInter", typeof(IntersectIterator), XmlILMethods.InterCreate, XmlILMethods.InterNext);
}
/// <summary>
/// Generate code for QilNodeType.Difference.
/// </summary>
protected override QilNode VisitDifference(QilBinary ndDiff) {
return CreateSetIterator(ndDiff, "$$$iterDiff", typeof(DifferenceIterator), XmlILMethods.DiffCreate, XmlILMethods.DiffNext);
}
/// <summary>
/// Generate code to combine nodes from two nested iterators using Union, Intersection, or Difference semantics.
/// </summary>
private QilNode CreateSetIterator(QilBinary ndSet, string iterName, Type iterType, MethodInfo methCreate, MethodInfo methNext) {
LocalBuilder locIter, locNav;
Label lblNext, lblCall, lblNextLeft, lblNextRight, lblInitRight;
// SetIterator iterSet;
// XPathNavigator navSet;
locIter = this.helper.DeclareLocal(iterName, iterType);
locNav = this.helper.DeclareLocal("$$$navSet", typeof(XPathNavigator));
// iterSet.Create(runtime);
this.helper.Emit(OpCodes.Ldloca, locIter);
this.helper.LoadQueryRuntime();
this.helper.Call(methCreate);
// Define labels that will be used
lblNext = this.helper.DefineLabel();
lblCall = this.helper.DefineLabel();
lblInitRight = this.helper.DefineLabel();
// Generate left nested iterator. When it is empty, it will branch to lblNext.
// goto LabelCall;
NestedVisit(ndSet.Left, lblNext);
lblNextLeft = this.iterNested.GetLabelNext();
this.iterCurr.EnsureLocal(locNav);
this.helper.EmitUnconditionalBranch(OpCodes.Brtrue, lblCall);
// Generate right nested iterator. When it is empty, it will branch to lblNext.
// LabelInitRight:
// goto LabelCall;
this.helper.MarkLabel(lblInitRight);
NestedVisit(ndSet.Right, lblNext);
lblNextRight = this.iterNested.GetLabelNext();
this.iterCurr.EnsureLocal(locNav);
this.helper.EmitUnconditionalBranch(OpCodes.Brtrue, lblCall);
// LabelNext:
this.helper.MarkLabel(lblNext);
this.helper.Emit(OpCodes.Ldnull);
this.helper.Emit(OpCodes.Stloc, locNav);
// LabelCall:
// switch (iterSet.MoveNext(nestedNested)) {
// case SetIteratorResult.NoMoreNodes: goto LabelNextCtxt;
// case SetIteratorResult.InitRightIterator: goto LabelInitRight;
// case SetIteratorResult.NeedLeftNode: goto LabelNextLeft;
// case SetIteratorResult.NeedRightNode: goto LabelNextRight;
// }
this.helper.MarkLabel(lblCall);
this.helper.Emit(OpCodes.Ldloca, locIter);
this.helper.Emit(OpCodes.Ldloc, locNav);
this.helper.Call(methNext);
// If this iterator always returns a single node, then NoMoreNodes will never be returned
// Don't expose Next label if this iterator always returns a single node
if (ndSet.XmlType.IsSingleton) {
this.helper.Emit(OpCodes.Switch, new Label[] {lblInitRight, lblNextLeft, lblNextRight});
this.iterCurr.Storage = StorageDescriptor.Current(locIter, typeof(XPathNavigator));
}
else {
this.helper.Emit(OpCodes.Switch, new Label[] {this.iterCurr.GetLabelNext(), lblInitRight, lblNextLeft, lblNextRight});
this.iterCurr.SetIterator(lblNext, StorageDescriptor.Current(locIter, typeof(XPathNavigator)));
}
return ndSet;
}
/// <summary>
/// Generate code for QilNodeType.Average.
/// </summary>
protected override QilNode VisitAverage(QilUnary ndAvg) {
XmlILStorageMethods meths = XmlILMethods.StorageMethods[GetItemStorageType(ndAvg)];
return CreateAggregator(ndAvg, "$$$aggAvg", meths, meths.AggAvg, meths.AggAvgResult);
}
/// <summary>
/// Generate code for QilNodeType.Sum.
/// </summary>
protected override QilNode VisitSum(QilUnary ndSum) {
XmlILStorageMethods meths = XmlILMethods.StorageMethods[GetItemStorageType(ndSum)];
return CreateAggregator(ndSum, "$$$aggSum", meths, meths.AggSum, meths.AggSumResult);
}
/// <summary>
/// Generate code for QilNodeType.Minimum.
/// </summary>
protected override QilNode VisitMinimum(QilUnary ndMin) {
XmlILStorageMethods meths = XmlILMethods.StorageMethods[GetItemStorageType(ndMin)];
return CreateAggregator(ndMin, "$$$aggMin", meths, meths.AggMin, meths.AggMinResult);
}
/// <summary>
/// Generate code for QilNodeType.Maximum.
/// </summary>
protected override QilNode VisitMaximum(QilUnary ndMax) {
XmlILStorageMethods meths = XmlILMethods.StorageMethods[GetItemStorageType(ndMax)];
return CreateAggregator(ndMax, "$$$aggMax", meths, meths.AggMax, meths.AggMaxResult);
}
/// <summary>
/// Generate code for QilNodeType.Sum, QilNodeType.Average, QilNodeType.Minimum, and QilNodeType.Maximum.
/// </summary>
private QilNode CreateAggregator(QilUnary ndAgg, string aggName, XmlILStorageMethods methods, MethodInfo methAgg, MethodInfo methResult) {
Label lblOnEnd = this.helper.DefineLabel();
Type typAgg = methAgg.DeclaringType;
LocalBuilder locAgg;
// Aggregate agg;
// agg.Create();
locAgg = this.helper.DeclareLocal(aggName, typAgg);
this.helper.Emit(OpCodes.Ldloca, locAgg);
this.helper.Call(methods.AggCreate);
// foreach (num in expr) {
StartNestedIterator(ndAgg.Child, lblOnEnd);
this.helper.Emit(OpCodes.Ldloca, locAgg);
Visit(ndAgg.Child);
// agg.Aggregate(num);
this.iterCurr.EnsureStackNoCache();
this.iterCurr.EnsureItemStorageType(ndAgg.XmlType, GetItemStorageType(ndAgg));
this.helper.Call(methAgg);
this.helper.Emit(OpCodes.Ldloca, locAgg);
// }
this.iterCurr.LoopToEnd(lblOnEnd);
// End nested iterator
EndNestedIterator(ndAgg.Child);
// If aggregate might be empty sequence, then generate code to handle this possibility
if (ndAgg.XmlType.MaybeEmpty) {
// if (agg.IsEmpty) goto LabelNextCtxt;
this.helper.Call(methods.AggIsEmpty);
this.helper.Emit(OpCodes.Brtrue, this.iterCurr.GetLabelNext());
this.helper.Emit(OpCodes.Ldloca, locAgg);
}
// result = agg.Result;
this.helper.Call(methResult);
this.iterCurr.Storage = StorageDescriptor.Stack(GetItemStorageType(ndAgg), false);
return ndAgg;
}
/// <summary>
/// Generate code for QilNodeType.Negate.
/// </summary>
protected override QilNode VisitNegate(QilUnary ndNeg) {
NestedVisitEnsureStack(ndNeg.Child);
this.helper.CallArithmeticOp(QilNodeType.Negate, ndNeg.XmlType.TypeCode);
this.iterCurr.Storage = StorageDescriptor.Stack(GetItemStorageType(ndNeg), false);
return ndNeg;
}
/// <summary>
/// Generate code for QilNodeType.Add.
/// </summary>
protected override QilNode VisitAdd(QilBinary ndPlus) {
return ArithmeticOp(ndPlus);
}
/// <summary>
/// Generate code for QilNodeType.Subtract.
/// </summary>
protected override QilNode VisitSubtract(QilBinary ndMinus) {
return ArithmeticOp(ndMinus);
}
/// <summary>
/// Generate code for QilNodeType.Multiply.
/// </summary>
protected override QilNode VisitMultiply(QilBinary ndMul) {
return ArithmeticOp(ndMul);
}
/// <summary>
/// Generate code for QilNodeType.Divide.
/// </summary>
protected override QilNode VisitDivide(QilBinary ndDiv) {
return ArithmeticOp(ndDiv);
}
/// <summary>
/// Generate code for QilNodeType.Modulo.
/// </summary>
protected override QilNode VisitModulo(QilBinary ndMod) {
return ArithmeticOp(ndMod);
}
/// <summary>
/// Generate code for two-argument arithmetic operations.
/// </summary>
private QilNode ArithmeticOp(QilBinary ndOp) {
NestedVisitEnsureStack(ndOp.Left, ndOp.Right);
this.helper.CallArithmeticOp(ndOp.NodeType, ndOp.XmlType.TypeCode);
this.iterCurr.Storage = StorageDescriptor.Stack(GetItemStorageType(ndOp), false);
return ndOp;
}
/// <summary>
/// Generate code for QilNodeType.StrLength.
/// </summary>
protected override QilNode VisitStrLength(QilUnary ndLen) {
NestedVisitEnsureStack(ndLen.Child);
this.helper.Call(XmlILMethods.StrLen);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(int), false);
return ndLen;
}
/// <summary>
/// Generate code for QilNodeType.StrConcat.
/// </summary>
protected override QilNode VisitStrConcat(QilStrConcat ndStrConcat) {
LocalBuilder locStringConcat;
bool fasterConcat;
QilNode delimiter;
QilNode listStrings;
Debug.Assert(!ndStrConcat.Values.XmlType.IsSingleton, "Optimizer should have folded StrConcat of a singleton value");
// Get delimiter (assuming it's not the empty string)
delimiter = ndStrConcat.Delimiter;
if (delimiter.NodeType == QilNodeType.LiteralString && ((string) (QilLiteral) delimiter).Length == 0) {
delimiter = null;
}
listStrings = ndStrConcat.Values;
if (listStrings.NodeType == QilNodeType.Sequence && listStrings.Count < 5) {
// Faster concat possible only if cardinality can be guaranteed at compile-time and there's no delimiter
fasterConcat = true;
foreach (QilNode ndStr in listStrings) {
if (!ndStr.XmlType.IsSingleton)
fasterConcat = false;
}
}
else {
// If more than 4 strings, array will need to be built
fasterConcat = false;
}
if (fasterConcat) {
foreach (QilNode ndStr in listStrings)
NestedVisitEnsureStack(ndStr);
this.helper.CallConcatStrings(listStrings.Count);
}
else {
// Create StringConcat helper internal class
locStringConcat = this.helper.DeclareLocal("$$$strcat", typeof(StringConcat));
this.helper.Emit(OpCodes.Ldloca, locStringConcat);
this.helper.Call(XmlILMethods.StrCatClear);
// Set delimiter, if it's not empty string
if (delimiter != null) {
this.helper.Emit(OpCodes.Ldloca, locStringConcat);
NestedVisitEnsureStack(delimiter);
this.helper.Call(XmlILMethods.StrCatDelim);
}
this.helper.Emit(OpCodes.Ldloca, locStringConcat);
if (listStrings.NodeType == QilNodeType.Sequence) {
foreach (QilNode ndStr in listStrings)
GenerateConcat(ndStr, locStringConcat);
}
else {
GenerateConcat(listStrings, locStringConcat);
}
// Push resulting string onto stack
this.helper.Call(XmlILMethods.StrCatResult);
}
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(string), false);
return ndStrConcat;
}
/// <summary>
/// Generate code to concatenate string values returned by expression "ndStr" using the StringConcat helper class.
/// </summary>
private void GenerateConcat(QilNode ndStr, LocalBuilder locStringConcat) {
Label lblOnEnd;
// str = each string;
lblOnEnd = this.helper.DefineLabel();
StartNestedIterator(ndStr, lblOnEnd);
Visit(ndStr);
// strcat.Concat(str);
this.iterCurr.EnsureStackNoCache();
this.iterCurr.EnsureItemStorageType(ndStr.XmlType, typeof(string));
this.helper.Call(XmlILMethods.StrCatCat);
this.helper.Emit(OpCodes.Ldloca, locStringConcat);
// Get next string
// goto LabelNext;
// LabelOnEnd:
this.iterCurr.LoopToEnd(lblOnEnd);
// End nested iterator
EndNestedIterator(ndStr);
}
/// <summary>
/// Generate code for QilNodeType.StrParseQName.
/// </summary>
protected override QilNode VisitStrParseQName(QilBinary ndParsedTagName) {
VisitStrParseQName(ndParsedTagName, false);
return ndParsedTagName;
}
/// <summary>
/// Generate code for QilNodeType.StrParseQName.
/// </summary>
private void VisitStrParseQName(QilBinary ndParsedTagName, bool preservePrefix) {
// If QName prefix should be preserved, then don't create an XmlQualifiedName, which discards the prefix
if (!preservePrefix)
this.helper.LoadQueryRuntime();
// Push (possibly computed) tag name onto the stack
NestedVisitEnsureStack(ndParsedTagName.Left);
// If type of second parameter is string,
if (ndParsedTagName.Right.XmlType.TypeCode == XmlTypeCode.String) {
// Then push (possibly computed) namespace onto the stack
Debug.Assert(ndParsedTagName.Right.XmlType.IsSingleton);
NestedVisitEnsureStack(ndParsedTagName.Right);
if (!preservePrefix)
this.helper.CallParseTagName(GenerateNameType.TagNameAndNamespace);
}
else {
// Else push index of set of prefix mappings to use in resolving the prefix
if (ndParsedTagName.Right.NodeType == QilNodeType.Sequence)
this.helper.LoadInteger(this.helper.StaticData.DeclarePrefixMappings(ndParsedTagName.Right));
else
this.helper.LoadInteger(this.helper.StaticData.DeclarePrefixMappings(new QilNode[] {ndParsedTagName.Right}));
// If QName prefix should be preserved, then don't create an XmlQualifiedName, which discards the prefix
if (!preservePrefix)
this.helper.CallParseTagName(GenerateNameType.TagNameAndMappings);
}
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(XmlQualifiedName), false);
}
/// <summary>
/// Generate code for QilNodeType.Ne.
/// </summary>
protected override QilNode VisitNe(QilBinary ndNe) {
Compare(ndNe);
return ndNe;
}
/// <summary>
/// Generate code for QilNodeType.Eq.
/// </summary>
protected override QilNode VisitEq(QilBinary ndEq) {
Compare(ndEq);
return ndEq;
}
/// <summary>
/// Generate code for QilNodeType.Gt.
/// </summary>
protected override QilNode VisitGt(QilBinary ndGt) {
Compare(ndGt);
return ndGt;
}
/// <summary>
/// Generate code for QilNodeType.Ne.
/// </summary>
protected override QilNode VisitGe(QilBinary ndGe) {
Compare(ndGe);
return ndGe;
}
/// <summary>
/// Generate code for QilNodeType.Lt.
/// </summary>
protected override QilNode VisitLt(QilBinary ndLt) {
Compare(ndLt);
return ndLt;
}
/// <summary>
/// Generate code for QilNodeType.Le.
/// </summary>
protected override QilNode VisitLe(QilBinary ndLe) {
Compare(ndLe);
return ndLe;
}
/// <summary>
/// Generate code for comparison operations.
/// </summary>
private void Compare(QilBinary ndComp) {
QilNodeType relOp = ndComp.NodeType;
XmlTypeCode code;
Debug.Assert(ndComp.Left.XmlType.IsAtomicValue && ndComp.Right.XmlType.IsAtomicValue, "Operands to compare must be atomic values.");
Debug.Assert(ndComp.Left.XmlType.IsSingleton && ndComp.Right.XmlType.IsSingleton, "Operands to compare must be cardinality one.");
Debug.Assert(ndComp.Left.XmlType == ndComp.Right.XmlType, "Operands to compare may not be heterogenous.");
if (relOp == QilNodeType.Eq || relOp == QilNodeType.Ne) {
// Generate better code for certain special cases
if (TryZeroCompare(relOp, ndComp.Left, ndComp.Right))
return;
if (TryZeroCompare(relOp, ndComp.Right, ndComp.Left))
return;
if (TryNameCompare(relOp, ndComp.Left, ndComp.Right))
return;
if (TryNameCompare(relOp, ndComp.Right, ndComp.Left))
return;
}
// Push two operands onto the stack
NestedVisitEnsureStack(ndComp.Left, ndComp.Right);
// Perform comparison
code = ndComp.Left.XmlType.TypeCode;
switch (code) {
case XmlTypeCode.String:
case XmlTypeCode.Decimal:
case XmlTypeCode.QName:
if (relOp == QilNodeType.Eq || relOp == QilNodeType.Ne) {
this.helper.CallCompareEquals(code);
// If relOp is Eq, then branch to true label or push "true" if Equals function returns true (non-zero)
// If relOp is Ne, then branch to true label or push "true" if Equals function returns false (zero)
ZeroCompare((relOp == QilNodeType.Eq) ? QilNodeType.Ne : QilNodeType.Eq, true);
}
else {
Debug.Assert(code != XmlTypeCode.QName, "QName values do not support the " + relOp + " operation");
// Push -1, 0, or 1 onto the stack depending upon the result of the comparison
this.helper.CallCompare(code);
// Compare result to 0 (e.g. Ge is >= 0)
this.helper.Emit(OpCodes.Ldc_I4_0);
ClrCompare(relOp, code);
}
break;
case XmlTypeCode.Integer:
case XmlTypeCode.Int:
case XmlTypeCode.Boolean:
case XmlTypeCode.Double:
ClrCompare(relOp, code);
break;
default:
Debug.Fail("Comparisons for datatype " + code + " are invalid.");
break;
}
}
/// <summary>
/// Generate code for QilNodeType.VisitIs.
/// </summary>
protected override QilNode VisitIs(QilBinary ndIs) {
// Generate code to push arguments onto stack
NestedVisitEnsureStack(ndIs.Left, ndIs.Right);
this.helper.Call(XmlILMethods.NavSamePos);
// navThis.IsSamePosition(navThat);
ZeroCompare(QilNodeType.Ne, true);
return ndIs;
}
/// <summary>
/// Generate code for QilNodeType.VisitBefore.
/// </summary>
protected override QilNode VisitBefore(QilBinary ndBefore) {
ComparePosition(ndBefore);
return ndBefore;
}
/// <summary>
/// Generate code for QilNodeType.VisitAfter.
/// </summary>
protected override QilNode VisitAfter(QilBinary ndAfter) {
ComparePosition(ndAfter);
return ndAfter;
}
/// <summary>
/// Generate code for QilNodeType.VisitBefore and QilNodeType.VisitAfter.
/// </summary>
private void ComparePosition(QilBinary ndComp) {
// Generate code to push arguments onto stack
this.helper.LoadQueryRuntime();
NestedVisitEnsureStack(ndComp.Left, ndComp.Right);
this.helper.Call(XmlILMethods.CompPos);
// XmlQueryRuntime.ComparePosition(navThis, navThat) < 0;
this.helper.LoadInteger(0);
ClrCompare(ndComp.NodeType == QilNodeType.Before ? QilNodeType.Lt : QilNodeType.Gt, XmlTypeCode.String);
}
/// <summary>
/// Generate code for a QilNodeType.For.
/// </summary>
protected override QilNode VisitFor(QilIterator ndFor) {
IteratorDescriptor iterInfo;
// Reference saved location
iterInfo = XmlILAnnotation.Write(ndFor).CachedIteratorDescriptor;
this.iterCurr.Storage = iterInfo.Storage;
// If the iterator is a reference to a global variable or parameter,
if (this.iterCurr.Storage.Location == ItemLocation.Global) {
// Then compute global value and push it onto the stack
this.iterCurr.EnsureStack();
}
return ndFor;
}
/// <summary>
/// Generate code for a QilNodeType.Let.
/// </summary>
protected override QilNode VisitLet(QilIterator ndLet) {
// Same as For
return VisitFor(ndLet);
}
/// <summary>
/// Generate code for a QilNodeType.Parameter.
/// </summary>
protected override QilNode VisitParameter(QilParameter ndParameter) {
// Same as For
return VisitFor(ndParameter);
}
/// <summary>
/// Generate code for a QilNodeType.Loop.
/// </summary>
protected override QilNode VisitLoop(QilLoop ndLoop) {
bool hasOnEnd;
Label lblOnEnd;
StartWriterLoop(ndLoop, out hasOnEnd, out lblOnEnd);
StartBinding(ndLoop.Variable);
// Unnest loop body as part of the current iterator
Visit(ndLoop.Body);
EndBinding(ndLoop.Variable);
EndWriterLoop(ndLoop, hasOnEnd, lblOnEnd);
return ndLoop;
}
/// <summary>
/// Generate code for a QilNodeType.Filter.
/// </summary>
protected override QilNode VisitFilter(QilLoop ndFilter) {
// Handle any special-case patterns that are rooted at Filter
if (HandleFilterPatterns(ndFilter))
return ndFilter;
StartBinding(ndFilter.Variable);
// Result of filter is the sequence bound to the iterator
this.iterCurr.SetIterator(this.iterNested);
// If filter is false, skip the current item
StartNestedIterator(ndFilter.Body);
this.iterCurr.SetBranching(BranchingContext.OnFalse, this.iterCurr.ParentIterator.GetLabelNext());
Visit(ndFilter.Body);
EndNestedIterator(ndFilter.Body);
EndBinding(ndFilter.Variable);
return ndFilter;
}
/// <summary>
/// There are a number of path patterns that can be rooted at Filter nodes. Determine whether one of these patterns
/// has been previously matched on "ndFilter". If so, generate code for the pattern and return true. Otherwise, just
/// return false.
/// </summary>
private bool HandleFilterPatterns(QilLoop ndFilter) {
OptimizerPatterns patt = OptimizerPatterns.Read(ndFilter);
LocalBuilder locIter;
XmlNodeKindFlags kinds;
QilName name;
QilNode input, step;
bool isFilterElements;
// Handle FilterElements and FilterContentKind patterns
isFilterElements = patt.MatchesPattern(OptimizerPatternName.FilterElements);
if (isFilterElements || patt.MatchesPattern(OptimizerPatternName.FilterContentKind)) {
if (isFilterElements) {
// FilterElements pattern, so Kind = Element and Name = Argument
kinds = XmlNodeKindFlags.Element;
name = (QilName) patt.GetArgument(OptimizerPatternArgument.ElementQName);
}
else {
// FilterKindTest pattern, so Kind = Argument and Name = null
kinds = ((XmlQueryType) patt.GetArgument(OptimizerPatternArgument.KindTestType)).NodeKinds;
name = null;
}
step = (QilNode) patt.GetArgument(OptimizerPatternArgument.StepNode);
input = (QilNode) patt.GetArgument(OptimizerPatternArgument.StepInput);
switch (step.NodeType) {
case QilNodeType.Content:
if (isFilterElements) {
// Iterator iter;
locIter = this.helper.DeclareLocal("$$$iterElemContent", typeof(ElementContentIterator));
// iter.Create(navCtxt, locName, ns);
this.helper.Emit(OpCodes.Ldloca, locIter);
NestedVisitEnsureStack(input);
this.helper.CallGetAtomizedName(this.helper.StaticData.DeclareName(name.LocalName));
this.helper.CallGetAtomizedName(this.helper.StaticData.DeclareName(name.NamespaceUri));
this.helper.Call(XmlILMethods.ElemContentCreate);
GenerateSimpleIterator(typeof(XPathNavigator), locIter, XmlILMethods.ElemContentNext);
}
else {
if (kinds == XmlNodeKindFlags.Content) {
CreateSimpleIterator(input, "$$$iterContent", typeof(ContentIterator), XmlILMethods.ContentCreate, XmlILMethods.ContentNext);
}
else {
// Iterator iter;
locIter = this.helper.DeclareLocal("$$$iterContent", typeof(NodeKindContentIterator));
// iter.Create(navCtxt, nodeType);
this.helper.Emit(OpCodes.Ldloca, locIter);
NestedVisitEnsureStack(input);
this.helper.LoadInteger((int) QilXmlToXPathNodeType(kinds));
this.helper.Call(XmlILMethods.KindContentCreate);
GenerateSimpleIterator(typeof(XPathNavigator), locIter, XmlILMethods.KindContentNext);
}
}
return true;
case QilNodeType.Parent:
CreateFilteredIterator(input, "$$$iterPar", typeof(ParentIterator), XmlILMethods.ParentCreate, XmlILMethods.ParentNext,
kinds, name, TriState.Unknown, null);
return true;
case QilNodeType.Ancestor:
case QilNodeType.AncestorOrSelf:
CreateFilteredIterator(input, "$$$iterAnc", typeof(AncestorIterator), XmlILMethods.AncCreate, XmlILMethods.AncNext,
kinds, name, (step.NodeType == QilNodeType.Ancestor) ? TriState.False : TriState.True, null);
return true;
case QilNodeType.Descendant:
case QilNodeType.DescendantOrSelf:
CreateFilteredIterator(input, "$$$iterDesc", typeof(DescendantIterator), XmlILMethods.DescCreate, XmlILMethods.DescNext,
kinds, name, (step.NodeType == QilNodeType.Descendant) ? TriState.False : TriState.True, null);
return true;
case QilNodeType.Preceding:
CreateFilteredIterator(input, "$$$iterPrec", typeof(PrecedingIterator), XmlILMethods.PrecCreate, XmlILMethods.PrecNext,
kinds, name, TriState.Unknown, null);
return true;
case QilNodeType.FollowingSibling:
CreateFilteredIterator(input, "$$$iterFollSib", typeof(FollowingSiblingIterator), XmlILMethods.FollSibCreate, XmlILMethods.FollSibNext,
kinds, name, TriState.Unknown, null);
return true;
case QilNodeType.PrecedingSibling:
CreateFilteredIterator(input, "$$$iterPreSib", typeof(PrecedingSiblingIterator), XmlILMethods.PreSibCreate, XmlILMethods.PreSibNext,
kinds, name, TriState.Unknown, null);
return true;
case QilNodeType.NodeRange:
CreateFilteredIterator(input, "$$$iterRange", typeof(NodeRangeIterator), XmlILMethods.NodeRangeCreate, XmlILMethods.NodeRangeNext,
kinds, name, TriState.Unknown, ((QilBinary) step).Right);
return true;
case QilNodeType.XPathFollowing:
CreateFilteredIterator(input, "$$$iterFoll", typeof(XPathFollowingIterator), XmlILMethods.XPFollCreate, XmlILMethods.XPFollNext,
kinds, name, TriState.Unknown, null);
return true;
case QilNodeType.XPathPreceding:
CreateFilteredIterator(input, "$$$iterPrec", typeof(XPathPrecedingIterator), XmlILMethods.XPPrecCreate, XmlILMethods.XPPrecNext,
kinds, name, TriState.Unknown, null);
return true;
default:
Debug.Assert(false, "Pattern " + step.NodeType + " should have been handled.");
break;
}
}
else if (patt.MatchesPattern(OptimizerPatternName.FilterAttributeKind)) {
// Handle FilterAttributeKind pattern
input = (QilNode) patt.GetArgument(OptimizerPatternArgument.StepInput);
CreateSimpleIterator(input, "$$$iterAttr", typeof(AttributeIterator), XmlILMethods.AttrCreate, XmlILMethods.AttrNext);
return true;
}
else if (patt.MatchesPattern(OptimizerPatternName.EqualityIndex)) {
// Handle EqualityIndex pattern
Label lblOnEnd = this.helper.DefineLabel();
Label lblLookup = this.helper.DefineLabel();
QilIterator nodes = (QilIterator) patt.GetArgument(OptimizerPatternArgument.IndexedNodes);
QilNode keys = (QilNode) patt.GetArgument(OptimizerPatternArgument.KeyExpression);
// XmlILIndex index;
// if (runtime.FindIndex(navCtxt, indexId, out index)) goto LabelLookup;
LocalBuilder locIndex = this.helper.DeclareLocal("$$$index", typeof(XmlILIndex));
this.helper.LoadQueryRuntime();
this.helper.Emit(OpCodes.Ldarg_1);
this.helper.LoadInteger(this.indexId);
this.helper.Emit(OpCodes.Ldloca, locIndex);
this.helper.Call(XmlILMethods.FindIndex);
this.helper.Emit(OpCodes.Brtrue, lblLookup);
// runtime.AddNewIndex(navCtxt, indexId, [build index]);
this.helper.LoadQueryRuntime();
this.helper.Emit(OpCodes.Ldarg_1);
this.helper.LoadInteger(this.indexId);
this.helper.Emit(OpCodes.Ldloc, locIndex);
// Generate code to iterate over the the nodes which are being indexed ($iterNodes in the pattern)
StartNestedIterator(nodes, lblOnEnd);
StartBinding(nodes);
// Generate code to iterate over the keys for each node ($bindingKeys in the pattern)
Visit(keys);
// index.Add(key, value);
this.iterCurr.EnsureStackNoCache();
VisitFor(nodes);
this.iterCurr.EnsureStackNoCache();
this.iterCurr.EnsureItemStorageType(nodes.XmlType, typeof(XPathNavigator));
this.helper.Call(XmlILMethods.IndexAdd);
this.helper.Emit(OpCodes.Ldloc, locIndex);
// LabelOnEnd:
this.iterCurr.LoopToEnd(lblOnEnd);
EndBinding(nodes);
EndNestedIterator(nodes);
// runtime.AddNewIndex(navCtxt, indexId, [build index]);
this.helper.Call(XmlILMethods.AddNewIndex);
// LabelLookup:
// results = index.Lookup(keyValue);
this.helper.MarkLabel(lblLookup);
this.helper.Emit(OpCodes.Ldloc, locIndex);
this.helper.Emit(OpCodes.Ldarg_2);
this.helper.Call(XmlILMethods.IndexLookup);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(XPathNavigator), true);
this.indexId++;
return true;
}
return false;
}
/// <summary>
/// Generate code for a Let, For, or Parameter iterator. Bind iterated value to a variable.
/// </summary>
private void StartBinding(QilIterator ndIter) {
OptimizerPatterns patt = OptimizerPatterns.Read(ndIter);
Debug.Assert(ndIter != null);
// DebugInfo: Sequence point just before generating code for the bound expression
if (this.qil.IsDebug && ndIter.SourceLine != null)
this.helper.DebugSequencePoint(ndIter.SourceLine);
// Treat cardinality one Let iterators as if they were For iterators (no nesting necessary)
if (ndIter.NodeType == QilNodeType.For || ndIter.XmlType.IsSingleton) {
StartForBinding(ndIter, patt);
}
else {
Debug.Assert(ndIter.NodeType == QilNodeType.Let || ndIter.NodeType == QilNodeType.Parameter);
Debug.Assert(!patt.MatchesPattern(OptimizerPatternName.IsPositional));
// Bind Let values (nested iterator) to variable
StartLetBinding(ndIter);
}
// Attach IteratorDescriptor to the iterator
XmlILAnnotation.Write(ndIter).CachedIteratorDescriptor = this.iterNested;
}
/// <summary>
/// Bind values produced by the "ndFor" expression to a non-stack location that can later
/// be referenced.
/// </summary>
private void StartForBinding(QilIterator ndFor, OptimizerPatterns patt) {
LocalBuilder locPos = null;
Debug.Assert(ndFor.XmlType.IsSingleton);
// For expression iterator will be unnested as part of parent iterator
if (this.iterCurr.HasLabelNext)
StartNestedIterator(ndFor.Binding, this.iterCurr.GetLabelNext());
else
StartNestedIterator(ndFor.Binding);
if (patt.MatchesPattern(OptimizerPatternName.IsPositional)) {
// Need to track loop index so initialize it to 0 before starting loop
locPos = this.helper.DeclareLocal("$$$pos", typeof(int));
this.helper.Emit(OpCodes.Ldc_I4_0);
this.helper.Emit(OpCodes.Stloc, locPos);
}
// Allow base internal class to dispatch based on QilExpression node type
Visit(ndFor.Binding);
// DebugInfo: Open variable scope
// DebugInfo: Ensure that for variable is stored in a local and tag it with the user-defined name
if (this.qil.IsDebug && ndFor.DebugName != null) {
this.helper.DebugStartScope();
// Ensure that values are stored in a local variable with a user-defined name
this.iterCurr.EnsureLocalNoCache("$$$for");
this.iterCurr.Storage.LocalLocation.SetLocalSymInfo(ndFor.DebugName);
}
else {
// Ensure that values are not stored on the stack
this.iterCurr.EnsureNoStackNoCache("$$$for");
}
if (patt.MatchesPattern(OptimizerPatternName.IsPositional)) {
// Increment position
this.helper.Emit(OpCodes.Ldloc, locPos);
this.helper.Emit(OpCodes.Ldc_I4_1);
this.helper.Emit(OpCodes.Add);
this.helper.Emit(OpCodes.Stloc, locPos);
if (patt.MatchesPattern(OptimizerPatternName.MaxPosition)) {
// Short-circuit rest of loop if max position has already been reached
this.helper.Emit(OpCodes.Ldloc, locPos);
this.helper.LoadInteger((int) patt.GetArgument(OptimizerPatternArgument.MaxPosition));
this.helper.Emit(OpCodes.Bgt, this.iterCurr.ParentIterator.GetLabelNext());
}
this.iterCurr.LocalPosition = locPos;
}
EndNestedIterator(ndFor.Binding);
this.iterCurr.SetIterator(this.iterNested);
}
/// <summary>
/// Bind values in the "ndLet" expression to a non-stack location that can later be referenced.
/// </summary>
public void StartLetBinding(QilIterator ndLet) {
Debug.Assert(!ndLet.XmlType.IsSingleton);
// Construct nested iterator
StartNestedIterator(ndLet);
// Allow base internal class to dispatch based on QilExpression node type
NestedVisit(ndLet.Binding, GetItemStorageType(ndLet), !ndLet.XmlType.IsSingleton);
// DebugInfo: Open variable scope
// DebugInfo: Ensure that for variable is stored in a local and tag it with the user-defined name
if (this.qil.IsDebug && ndLet.DebugName != null) {
this.helper.DebugStartScope();
// Ensure that cache is stored in a local variable with a user-defined name
this.iterCurr.EnsureLocal("$$$cache");
this.iterCurr.Storage.LocalLocation.SetLocalSymInfo(ndLet.DebugName);
}
else {
// Ensure that cache is not stored on the stack
this.iterCurr.EnsureNoStack("$$$cache");
}
EndNestedIterator(ndLet);
}
/// <summary>
/// Mark iterator variables as out-of-scope.
/// </summary>
private void EndBinding(QilIterator ndIter) {
Debug.Assert(ndIter != null);
// Variables go out of scope here
if (this.qil.IsDebug && ndIter.DebugName != null)
this.helper.DebugEndScope();
}
/// <summary>
/// Generate code for QilNodeType.PositionOf.
/// </summary>
protected override QilNode VisitPositionOf(QilUnary ndPos) {
QilIterator ndIter = ndPos.Child as QilIterator;
LocalBuilder locPos;
Debug.Assert(ndIter.NodeType == QilNodeType.For);
locPos = XmlILAnnotation.Write(ndIter).CachedIteratorDescriptor.LocalPosition;
Debug.Assert(locPos != null);
this.iterCurr.Storage = StorageDescriptor.Local(locPos, typeof(int), false);
return ndPos;
}
/// <summary>
/// Generate code for QilNodeType.Sort.
/// </summary>
protected override QilNode VisitSort(QilLoop ndSort) {
Type itemStorageType = GetItemStorageType(ndSort);
LocalBuilder locCache, locKeys;
Label lblOnEndSort = this.helper.DefineLabel();
Debug.Assert(ndSort.Variable.NodeType == QilNodeType.For);
// XmlQuerySequence<T> cache;
// cache = XmlQuerySequence.CreateOrReuse(cache);
XmlILStorageMethods methods = XmlILMethods.StorageMethods[itemStorageType];
locCache = this.helper.DeclareLocal("$$$cache", methods.SeqType);
this.helper.Emit(OpCodes.Ldloc, locCache);
this.helper.CallToken(methods.SeqReuse);
this.helper.Emit(OpCodes.Stloc, locCache);
this.helper.Emit(OpCodes.Ldloc, locCache);
// XmlSortKeyAccumulator keys;
// keys.Create(runtime);
locKeys = this.helper.DeclareLocal("$$$keys", typeof(XmlSortKeyAccumulator));
this.helper.Emit(OpCodes.Ldloca, locKeys);
this.helper.Call(XmlILMethods.SortKeyCreate);
// Construct nested iterator
// foreach (item in sort-expr) {
StartNestedIterator(ndSort.Variable, lblOnEndSort);
StartBinding(ndSort.Variable);
Debug.Assert(!this.iterNested.Storage.IsCached);
// cache.Add(item);
this.iterCurr.EnsureStackNoCache();
this.iterCurr.EnsureItemStorageType(ndSort.Variable.XmlType, GetItemStorageType(ndSort.Variable));
this.helper.Call(methods.SeqAdd);
this.helper.Emit(OpCodes.Ldloca, locKeys);
// Add keys to accumulator (there may be several keys)
foreach (QilSortKey ndKey in ndSort.Body)
VisitSortKey(ndKey, locKeys);
// keys.FinishSortKeys();
this.helper.Call(XmlILMethods.SortKeyFinish);
// }
this.helper.Emit(OpCodes.Ldloc, locCache);
this.iterCurr.LoopToEnd(lblOnEndSort);
// Remove cache reference from stack
this.helper.Emit(OpCodes.Pop);
// cache.SortByKeys(keys.Keys);
this.helper.Emit(OpCodes.Ldloc, locCache);
this.helper.Emit(OpCodes.Ldloca, locKeys);
this.helper.Call(XmlILMethods.SortKeyKeys);
this.helper.Call(methods.SeqSortByKeys);
// End nested iterator
this.iterCurr.Storage = StorageDescriptor.Local(locCache, itemStorageType, true);
EndBinding(ndSort.Variable);
EndNestedIterator(ndSort.Variable);
this.iterCurr.SetIterator(this.iterNested);
return ndSort;
}
/// <summary>
/// Generate code to add a (value, collation) sort key to the XmlSortKeyAccumulator.
/// </summary>
private void VisitSortKey(QilSortKey ndKey, LocalBuilder locKeys) {
Label lblOnEndKey;
Debug.Assert(ndKey.Key.XmlType.IsAtomicValue, "Sort key must be an atomic value.");
// Push collation onto the stack
this.helper.Emit(OpCodes.Ldloca, locKeys);
if (ndKey.Collation.NodeType == QilNodeType.LiteralString) {
// collation = runtime.GetCollation(idx);
this.helper.CallGetCollation(this.helper.StaticData.DeclareCollation((string) (QilLiteral) ndKey.Collation));
}
else {
// collation = runtime.CreateCollation(str);
this.helper.LoadQueryRuntime();
NestedVisitEnsureStack(ndKey.Collation);
this.helper.Call(XmlILMethods.CreateCollation);
}
if (ndKey.XmlType.IsSingleton) {
NestedVisitEnsureStack(ndKey.Key);
// keys.AddSortKey(collation, value);
this.helper.AddSortKey(ndKey.Key.XmlType);
}
else {
lblOnEndKey = this.helper.DefineLabel();
StartNestedIterator(ndKey.Key, lblOnEndKey);
Visit(ndKey.Key);
this.iterCurr.EnsureStackNoCache();
this.iterCurr.EnsureItemStorageType(ndKey.Key.XmlType, GetItemStorageType(ndKey.Key));
// Non-empty sort key
// keys.AddSortKey(collation, value);
this.helper.AddSortKey(ndKey.Key.XmlType);
// goto LabelDone;
// LabelOnEnd:
Label lblDone = this.helper.DefineLabel();
this.helper.EmitUnconditionalBranch(OpCodes.Br_S, lblDone);
this.helper.MarkLabel(lblOnEndKey);
// Empty sequence key
// keys.AddSortKey(collation);
this.helper.AddSortKey(null);
this.helper.MarkLabel(lblDone);
EndNestedIterator(ndKey.Key);
}
}
/// <summary>
/// Generate code for for QilNodeType.DocOrderDistinct.
/// </summary>
protected override QilNode VisitDocOrderDistinct(QilUnary ndDod) {
// DocOrderDistinct applied to a singleton is a no-op
if (ndDod.XmlType.IsSingleton)
return Visit(ndDod.Child);
// Handle any special-case patterns that are rooted at DocOrderDistinct
if (HandleDodPatterns(ndDod))
return ndDod;
// Sort results of child expression by document order and remove duplicate nodes
// cache = runtime.DocOrderDistinct(cache);
this.helper.LoadQueryRuntime();
NestedVisitEnsureCache(ndDod.Child, typeof(XPathNavigator));
this.iterCurr.EnsureStack();
this.helper.Call(XmlILMethods.DocOrder);
return ndDod;
}
/// <summary>
/// There are a number of path patterns that can be rooted at DocOrderDistinct nodes. Determine whether one of these
/// patterns has been previously matched on "ndDod". If so, generate code for the pattern and return true. Otherwise,
/// just return false.
/// </summary>
private bool HandleDodPatterns(QilUnary ndDod) {
OptimizerPatterns pattDod = OptimizerPatterns.Read(ndDod);
XmlNodeKindFlags kinds;
QilName name;
QilNode input, step;
bool isJoinAndDod;
// Handle JoinAndDod and DodReverse patterns
isJoinAndDod = pattDod.MatchesPattern(OptimizerPatternName.JoinAndDod);
if (isJoinAndDod || pattDod.MatchesPattern(OptimizerPatternName.DodReverse)) {
OptimizerPatterns pattStep = OptimizerPatterns.Read((QilNode) pattDod.GetArgument(OptimizerPatternArgument.DodStep));
if (pattStep.MatchesPattern(OptimizerPatternName.FilterElements)) {
// FilterElements pattern, so Kind = Element and Name = Argument
kinds = XmlNodeKindFlags.Element;
name = (QilName) pattStep.GetArgument(OptimizerPatternArgument.ElementQName);
}
else if (pattStep.MatchesPattern(OptimizerPatternName.FilterContentKind)) {
// FilterKindTest pattern, so Kind = Argument and Name = null
kinds = ((XmlQueryType) pattStep.GetArgument(OptimizerPatternArgument.KindTestType)).NodeKinds;
name = null;
}
else {
Debug.Assert(pattStep.MatchesPattern(OptimizerPatternName.Axis), "Dod patterns should only match if step is FilterElements or FilterKindTest or Axis");
kinds = ((ndDod.XmlType.NodeKinds & XmlNodeKindFlags.Attribute) != 0) ? XmlNodeKindFlags.Any : XmlNodeKindFlags.Content;
name = null;
}
step = (QilNode) pattStep.GetArgument(OptimizerPatternArgument.StepNode);
if (isJoinAndDod) {
switch (step.NodeType) {
case QilNodeType.Content:
CreateContainerIterator(ndDod, "$$$iterContent", typeof(ContentMergeIterator), XmlILMethods.ContentMergeCreate, XmlILMethods.ContentMergeNext,
kinds, name, TriState.Unknown);
return true;
case QilNodeType.Descendant:
case QilNodeType.DescendantOrSelf:
CreateContainerIterator(ndDod, "$$$iterDesc", typeof(DescendantMergeIterator), XmlILMethods.DescMergeCreate, XmlILMethods.DescMergeNext,
kinds, name, (step.NodeType == QilNodeType.Descendant) ? TriState.False : TriState.True);
return true;
case QilNodeType.XPathFollowing:
CreateContainerIterator(ndDod, "$$$iterFoll", typeof(XPathFollowingMergeIterator), XmlILMethods.XPFollMergeCreate, XmlILMethods.XPFollMergeNext,
kinds, name, TriState.Unknown);
return true;
case QilNodeType.FollowingSibling:
CreateContainerIterator(ndDod, "$$$iterFollSib", typeof(FollowingSiblingMergeIterator), XmlILMethods.FollSibMergeCreate, XmlILMethods.FollSibMergeNext,
kinds, name, TriState.Unknown);
return true;
case QilNodeType.XPathPreceding:
CreateContainerIterator(ndDod, "$$$iterPrec", typeof(XPathPrecedingMergeIterator), XmlILMethods.XPPrecMergeCreate, XmlILMethods.XPPrecMergeNext,
kinds, name, TriState.Unknown);
return true;
default:
Debug.Assert(false, "Pattern " + step.NodeType + " should have been handled.");
break;
}
}
else {
input = (QilNode) pattStep.GetArgument(OptimizerPatternArgument.StepInput);
switch (step.NodeType) {
case QilNodeType.Ancestor:
case QilNodeType.AncestorOrSelf:
CreateFilteredIterator(input, "$$$iterAnc", typeof(AncestorDocOrderIterator), XmlILMethods.AncDOCreate, XmlILMethods.AncDONext,
kinds, name, (step.NodeType == QilNodeType.Ancestor) ? TriState.False : TriState.True, null);
return true;
case QilNodeType.PrecedingSibling:
CreateFilteredIterator(input, "$$$iterPreSib", typeof(PrecedingSiblingDocOrderIterator), XmlILMethods.PreSibDOCreate, XmlILMethods.PreSibDONext,
kinds, name, TriState.Unknown, null);
return true;
case QilNodeType.XPathPreceding:
CreateFilteredIterator(input, "$$$iterPrec", typeof(XPathPrecedingDocOrderIterator), XmlILMethods.XPPrecDOCreate, XmlILMethods.XPPrecDONext,
kinds, name, TriState.Unknown, null);
return true;
default:
Debug.Assert(false, "Pattern " + step.NodeType + " should have been handled.");
break;
}
}
}
else if (pattDod.MatchesPattern(OptimizerPatternName.DodMerge)) {
// DodSequenceMerge dodMerge;
LocalBuilder locMerge = this.helper.DeclareLocal("$$$dodMerge", typeof(DodSequenceMerge));
Label lblOnEnd = this.helper.DefineLabel();
// dodMerge.Create(runtime);
this.helper.Emit(OpCodes.Ldloca, locMerge);
this.helper.LoadQueryRuntime();
this.helper.Call(XmlILMethods.DodMergeCreate);
this.helper.Emit(OpCodes.Ldloca, locMerge);
StartNestedIterator(ndDod.Child, lblOnEnd);
// foreach (seq in expr) {
Visit(ndDod.Child);
// dodMerge.AddSequence(seq);
Debug.Assert(this.iterCurr.Storage.IsCached, "DodMerge pattern should only be matched when cached sequences are returned from loop");
this.iterCurr.EnsureStack();
this.helper.Call(XmlILMethods.DodMergeAdd);
this.helper.Emit(OpCodes.Ldloca, locMerge);
// }
this.iterCurr.LoopToEnd(lblOnEnd);
EndNestedIterator(ndDod.Child);
// mergedSequence = dodMerge.MergeSequences();
this.helper.Call(XmlILMethods.DodMergeSeq);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(XPathNavigator), true);
return true;
}
return false;
}
/// <summary>
/// Generate code for for QilNodeType.Invoke.
/// </summary>
protected override QilNode VisitInvoke(QilInvoke ndInvoke) {
QilFunction ndFunc = ndInvoke.Function;
MethodInfo methInfo = XmlILAnnotation.Write(ndFunc).FunctionBinding;
bool useWriter = (XmlILConstructInfo.Read(ndFunc).ConstructMethod == XmlILConstructMethod.Writer);
Debug.Assert(!XmlILConstructInfo.Read(ndInvoke).PushToWriterFirst || useWriter);
// Push XmlQueryRuntime onto the stack as the first parameter
this.helper.LoadQueryRuntime();
// Generate code to push each Invoke argument onto the stack
for (int iArg = 0; iArg < ndInvoke.Arguments.Count; iArg++) {
QilNode ndActualArg = ndInvoke.Arguments[iArg];
QilNode ndFormalArg = ndInvoke.Function.Arguments[iArg];
NestedVisitEnsureStack(ndActualArg, GetItemStorageType(ndFormalArg), !ndFormalArg.XmlType.IsSingleton);
}
// Check whether this call should compiled using the .tailcall instruction
if (OptimizerPatterns.Read(ndInvoke).MatchesPattern(OptimizerPatternName.TailCall))
this.helper.TailCall(methInfo);
else
this.helper.Call(methInfo);
// If function's results are not pushed to Writer,
if (!useWriter) {
// Return value is on the stack; ensure it has the correct storage type
this.iterCurr.Storage = StorageDescriptor.Stack(GetItemStorageType(ndInvoke), !ndInvoke.XmlType.IsSingleton);
}
else {
this.iterCurr.Storage = StorageDescriptor.None();
}
return ndInvoke;
}
/// <summary>
/// Generate code for for QilNodeType.Content.
/// </summary>
protected override QilNode VisitContent(QilUnary ndContent) {
CreateSimpleIterator(ndContent.Child, "$$$iterAttrContent", typeof(AttributeContentIterator), XmlILMethods.AttrContentCreate, XmlILMethods.AttrContentNext);
return ndContent;
}
/// <summary>
/// Generate code for for QilNodeType.Attribute.
/// </summary>
protected override QilNode VisitAttribute(QilBinary ndAttr) {
QilName ndName = ndAttr.Right as QilName;
Debug.Assert(ndName != null, "Attribute node must have a literal QName as its second argument");
// XPathNavigator navAttr;
LocalBuilder locNav = this.helper.DeclareLocal("$$$navAttr", typeof(XPathNavigator));
// navAttr = SyncToNavigator(navAttr, navCtxt);
SyncToNavigator(locNav, ndAttr.Left);
// if (!navAttr.MoveToAttribute(localName, namespaceUri)) goto LabelNextCtxt;
this.helper.Emit(OpCodes.Ldloc, locNav);
this.helper.CallGetAtomizedName(this.helper.StaticData.DeclareName(ndName.LocalName));
this.helper.CallGetAtomizedName(this.helper.StaticData.DeclareName(ndName.NamespaceUri));
this.helper.Call(XmlILMethods.NavMoveAttr);
this.helper.Emit(OpCodes.Brfalse, this.iterCurr.GetLabelNext());
this.iterCurr.Storage = StorageDescriptor.Local(locNav, typeof(XPathNavigator), false);
return ndAttr;
}
/// <summary>
/// Generate code for for QilNodeType.Parent.
/// </summary>
protected override QilNode VisitParent(QilUnary ndParent) {
// XPathNavigator navParent;
LocalBuilder locNav = this.helper.DeclareLocal("$$$navParent", typeof(XPathNavigator));
// navParent = SyncToNavigator(navParent, navCtxt);
SyncToNavigator(locNav, ndParent.Child);
// if (!navParent.MoveToParent()) goto LabelNextCtxt;
this.helper.Emit(OpCodes.Ldloc, locNav);
this.helper.Call(XmlILMethods.NavMoveParent);
this.helper.Emit(OpCodes.Brfalse, this.iterCurr.GetLabelNext());
this.iterCurr.Storage = StorageDescriptor.Local(locNav, typeof(XPathNavigator), false);
return ndParent;
}
/// <summary>
/// Generate code for for QilNodeType.Root.
/// </summary>
protected override QilNode VisitRoot(QilUnary ndRoot) {
// XPathNavigator navRoot;
LocalBuilder locNav = this.helper.DeclareLocal("$$$navRoot", typeof(XPathNavigator));
// navRoot = SyncToNavigator(navRoot, navCtxt);
SyncToNavigator(locNav, ndRoot.Child);
// navRoot.MoveToRoot();
this.helper.Emit(OpCodes.Ldloc, locNav);
this.helper.Call(XmlILMethods.NavMoveRoot);
this.iterCurr.Storage = StorageDescriptor.Local(locNav, typeof(XPathNavigator), false);
return ndRoot;
}
/// <summary>
/// Generate code for QilNodeType.XmlContext.
/// </summary>
/// <remarks>
/// Generates code to retrieve the default document using the XmlResolver.
/// </remarks>
protected override QilNode VisitXmlContext(QilNode ndCtxt) {
// runtime.ExternalContext.DefaultDataSource
this.helper.LoadQueryContext();
this.helper.Call(XmlILMethods.GetDefaultDataSource);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(XPathNavigator), false);
return ndCtxt;
}
/// <summary>
/// Find physical query plan for QilNodeType.Descendant.
/// </summary>
protected override QilNode VisitDescendant(QilUnary ndDesc) {
CreateFilteredIterator(ndDesc.Child, "$$$iterDesc", typeof(DescendantIterator), XmlILMethods.DescCreate, XmlILMethods.DescNext,
XmlNodeKindFlags.Any, null, TriState.False, null);
return ndDesc;
}
/// <summary>
/// Generate code for for QilNodeType.DescendantOrSelf.
/// </summary>
protected override QilNode VisitDescendantOrSelf(QilUnary ndDesc) {
CreateFilteredIterator(ndDesc.Child, "$$$iterDesc", typeof(DescendantIterator), XmlILMethods.DescCreate, XmlILMethods.DescNext,
XmlNodeKindFlags.Any, null, TriState.True, null);
return ndDesc;
}
/// <summary>
/// Find physical query plan for QilNodeType.Ancestor.
/// </summary>
protected override QilNode VisitAncestor(QilUnary ndAnc) {
CreateFilteredIterator(ndAnc.Child, "$$$iterAnc", typeof(AncestorIterator), XmlILMethods.AncCreate, XmlILMethods.AncNext,
XmlNodeKindFlags.Any, null, TriState.False, null);
return ndAnc;
}
/// <summary>
/// Find physical query plan for QilNodeType.AncestorOrSelf.
/// </summary>
protected override QilNode VisitAncestorOrSelf(QilUnary ndAnc) {
CreateFilteredIterator(ndAnc.Child, "$$$iterAnc", typeof(AncestorIterator), XmlILMethods.AncCreate, XmlILMethods.AncNext,
XmlNodeKindFlags.Any, null, TriState.True, null);
return ndAnc;
}
/// <summary>
/// Find physical query plan for QilNodeType.Preceding.
/// </summary>
protected override QilNode VisitPreceding(QilUnary ndPrec) {
CreateFilteredIterator(ndPrec.Child, "$$$iterPrec", typeof(PrecedingIterator), XmlILMethods.PrecCreate, XmlILMethods.PrecNext,
XmlNodeKindFlags.Any, null, TriState.Unknown, null);
return ndPrec;
}
/// <summary>
/// Find physical query plan for QilNodeType.FollowingSibling.
/// </summary>
protected override QilNode VisitFollowingSibling(QilUnary ndFollSib) {
CreateFilteredIterator(ndFollSib.Child, "$$$iterFollSib", typeof(FollowingSiblingIterator), XmlILMethods.FollSibCreate, XmlILMethods.FollSibNext,
XmlNodeKindFlags.Any, null, TriState.Unknown, null);
return ndFollSib;
}
/// <summary>
/// Find physical query plan for QilNodeType.PrecedingSibling.
/// </summary>
protected override QilNode VisitPrecedingSibling(QilUnary ndPreSib) {
CreateFilteredIterator(ndPreSib.Child, "$$$iterPreSib", typeof(PrecedingSiblingIterator), XmlILMethods.PreSibCreate, XmlILMethods.PreSibNext,
XmlNodeKindFlags.Any, null, TriState.Unknown, null);
return ndPreSib;
}
/// <summary>
/// Find physical query plan for QilNodeType.NodeRange.
/// </summary>
protected override QilNode VisitNodeRange(QilBinary ndRange) {
CreateFilteredIterator(ndRange.Left, "$$$iterRange", typeof(NodeRangeIterator), XmlILMethods.NodeRangeCreate, XmlILMethods.NodeRangeNext,
XmlNodeKindFlags.Any, null, TriState.Unknown, ndRange.Right);
return ndRange;
}
/// <summary>
/// Generate code for for QilNodeType.Deref.
/// </summary>
protected override QilNode VisitDeref(QilBinary ndDeref) {
// IdIterator iterId;
LocalBuilder locIter = this.helper.DeclareLocal("$$$iterId", typeof(IdIterator));
// iterId.Create(navCtxt, value);
this.helper.Emit(OpCodes.Ldloca, locIter);
NestedVisitEnsureStack(ndDeref.Left);
NestedVisitEnsureStack(ndDeref.Right);
this.helper.Call(XmlILMethods.IdCreate);
GenerateSimpleIterator(typeof(XPathNavigator), locIter, XmlILMethods.IdNext);
return ndDeref;
}
/// <summary>
/// Generate code for QilNodeType.ElementCtor.
/// </summary>
protected override QilNode VisitElementCtor(QilBinary ndElem) {
XmlILConstructInfo info = XmlILConstructInfo.Read(ndElem);
bool callChk;
GenerateNameType nameType;
Debug.Assert(XmlILConstructInfo.Read(ndElem).PushToWriterFirst, "Element contruction should always be pushed to writer.");
// Runtime checks must be made in the following cases:
// 1. Xml state is not known at compile-time, or is illegal
// 2. Element's namespace must be declared
// 3. Element's attributes might be duplicates of one another, or namespaces might follow attributes
callChk = CheckWithinContent(info) || !info.IsNamespaceInScope || ElementCachesAttributes(info);
// If it is not known whether element content was output, then make this check at run-time
if (XmlILConstructInfo.Read(ndElem.Right).FinalStates == PossibleXmlStates.Any)
callChk = true;
// If runtime state after EndElement is called is not known, then call XmlQueryOutput.WriteEndElementChk
if (info.FinalStates == PossibleXmlStates.Any)
callChk = true;
// If WriteStartElementChk will *not* be called, then code must be generated to ensure valid state transitions
if (!callChk)
BeforeStartChecks(ndElem);
// Generate call to WriteStartElement
nameType = LoadNameAndType(XPathNodeType.Element, ndElem.Left, true, callChk);
this.helper.CallWriteStartElement(nameType, callChk);
// Recursively construct content
NestedVisit(ndElem.Right);
// If runtime state is guaranteed to be EnumAttrs, and an element is being constructed, call XmlQueryOutput.StartElementContent
if (XmlILConstructInfo.Read(ndElem.Right).FinalStates == PossibleXmlStates.EnumAttrs && !callChk)
this.helper.CallStartElementContent();
// Generate call to WriteEndElement
nameType = LoadNameAndType(XPathNodeType.Element, ndElem.Left, false, callChk);
this.helper.CallWriteEndElement(nameType, callChk);
if (!callChk)
AfterEndChecks(ndElem);
this.iterCurr.Storage = StorageDescriptor.None();
return ndElem;
}
/// <summary>
/// Generate code for QilNodeType.AttributeCtor.
/// </summary>
protected override QilNode VisitAttributeCtor(QilBinary ndAttr) {
XmlILConstructInfo info = XmlILConstructInfo.Read(ndAttr);
bool callChk;
GenerateNameType nameType;
Debug.Assert(XmlILConstructInfo.Read(ndAttr).PushToWriterFirst, "Attribute construction should always be pushed to writer.");
// Runtime checks must be made in the following cases:
// 1. Xml state is not known at compile-time, or is illegal
// 2. Attribute's namespace must be declared
callChk = CheckEnumAttrs(info) || !info.IsNamespaceInScope;
// If WriteStartAttributeChk will *not* be called, then code must be generated to ensure well-formedness
// and track namespace scope.
if (!callChk)
BeforeStartChecks(ndAttr);
// Generate call to WriteStartAttribute
nameType = LoadNameAndType(XPathNodeType.Attribute, ndAttr.Left, true, callChk);
this.helper.CallWriteStartAttribute(nameType, callChk);
// Recursively construct content
NestedVisit(ndAttr.Right);
// Generate call to WriteEndAttribute
this.helper.CallWriteEndAttribute(callChk);
if (!callChk)
AfterEndChecks(ndAttr);
this.iterCurr.Storage = StorageDescriptor.None();
return ndAttr;
}
/// <summary>
/// Generate code for QilNodeType.CommentCtor.
/// </summary>
protected override QilNode VisitCommentCtor(QilUnary ndComment) {
Debug.Assert(XmlILConstructInfo.Read(ndComment).PushToWriterFirst, "Comment construction should always be pushed to writer.");
// Always call XmlQueryOutput.WriteStartComment
this.helper.CallWriteStartComment();
// Recursively construct content
NestedVisit(ndComment.Child);
// Always call XmlQueryOutput.WriteEndComment
this.helper.CallWriteEndComment();
this.iterCurr.Storage = StorageDescriptor.None();
return ndComment;
}
/// <summary>
/// Generate code for QilNodeType.PICtor.
/// </summary>
protected override QilNode VisitPICtor(QilBinary ndPI) {
Debug.Assert(XmlILConstructInfo.Read(ndPI).PushToWriterFirst, "PI construction should always be pushed to writer.");
// Always call XmlQueryOutput.WriteStartPI
this.helper.LoadQueryOutput();
NestedVisitEnsureStack(ndPI.Left);
this.helper.CallWriteStartPI();
// Recursively construct content
NestedVisit(ndPI.Right);
// Always call XmlQueryOutput.WriteEndPI
this.helper.CallWriteEndPI();
this.iterCurr.Storage = StorageDescriptor.None();
return ndPI;
}
/// <summary>
/// Generate code for QilNodeType.TextCtor.
/// </summary>
protected override QilNode VisitTextCtor(QilUnary ndText) {
return VisitTextCtor(ndText, false);
}
/// <summary>
/// Generate code for QilNodeType.RawTextCtor.
/// </summary>
protected override QilNode VisitRawTextCtor(QilUnary ndText) {
return VisitTextCtor(ndText, true);
}
/// <summary>
/// Generate code for QilNodeType.TextCtor and QilNodeType.RawTextCtor.
/// </summary>
private QilNode VisitTextCtor(QilUnary ndText, bool disableOutputEscaping) {
XmlILConstructInfo info = XmlILConstructInfo.Read(ndText);
bool callChk;
Debug.Assert(info.PushToWriterFirst, "Text construction should always be pushed to writer.");
// Write out text in different contexts (within attribute, within element, within comment, etc.)
switch (info.InitialStates) {
case PossibleXmlStates.WithinAttr:
case PossibleXmlStates.WithinComment:
case PossibleXmlStates.WithinPI:
callChk = false;
break;
default:
callChk = CheckWithinContent(info);
break;
}
if (!callChk)
BeforeStartChecks(ndText);
this.helper.LoadQueryOutput();
// Push string value of text onto IL stack
NestedVisitEnsureStack(ndText.Child);
// Write out text in different contexts (within attribute, within element, within comment, etc.)
switch (info.InitialStates) {
case PossibleXmlStates.WithinAttr:
// Ignore hints when writing out attribute text
this.helper.CallWriteString(false, callChk);
break;
case PossibleXmlStates.WithinComment:
// Call XmlQueryOutput.WriteCommentString
this.helper.Call(XmlILMethods.CommentText);
break;
case PossibleXmlStates.WithinPI:
// Call XmlQueryOutput.WriteProcessingInstructionString
this.helper.Call(XmlILMethods.PIText);
break;
default:
// Call XmlQueryOutput.WriteTextBlockChk, XmlQueryOutput.WriteTextBlockNoEntities, or XmlQueryOutput.WriteTextBlock
this.helper.CallWriteString(disableOutputEscaping, callChk);
break;
}
if (!callChk)
AfterEndChecks(ndText);
this.iterCurr.Storage = StorageDescriptor.None();
return ndText;
}
/// <summary>
/// Generate code for QilNodeType.DocumentCtor.
/// </summary>
protected override QilNode VisitDocumentCtor(QilUnary ndDoc) {
Debug.Assert(XmlILConstructInfo.Read(ndDoc).PushToWriterFirst, "Document root construction should always be pushed to writer.");
// Generate call to XmlQueryOutput.WriteStartRootChk
this.helper.CallWriteStartRoot();
// Recursively construct content
NestedVisit(ndDoc.Child);
// Generate call to XmlQueryOutput.WriteEndRootChk
this.helper.CallWriteEndRoot();
this.iterCurr.Storage = StorageDescriptor.None();
return ndDoc;
}
/// <summary>
/// Generate code for QilNodeType.NamespaceDecl.
/// </summary>
protected override QilNode VisitNamespaceDecl(QilBinary ndNmsp) {
XmlILConstructInfo info = XmlILConstructInfo.Read(ndNmsp);
bool callChk;
Debug.Assert(info.PushToWriterFirst, "Namespace construction should always be pushed to writer.");
// Runtime checks must be made in the following cases:
// 1. Xml state is not known at compile-time, or is illegal
// 2. Namespaces might be added to element after attributes have already been added
callChk = CheckEnumAttrs(info) || MightHaveNamespacesAfterAttributes(info);
// If WriteNamespaceDeclarationChk will *not* be called, then code must be generated to ensure well-formedness
// and track namespace scope.
if (!callChk)
BeforeStartChecks(ndNmsp);
this.helper.LoadQueryOutput();
// Recursively construct prefix and ns
NestedVisitEnsureStack(ndNmsp.Left);
NestedVisitEnsureStack(ndNmsp.Right);
// Generate call to WriteNamespaceDecl
this.helper.CallWriteNamespaceDecl(callChk);
if (!callChk)
AfterEndChecks(ndNmsp);
this.iterCurr.Storage = StorageDescriptor.None();
return ndNmsp;
}
/// <summary>
/// Generate code for for QilNodeType.RtfCtor.
/// </summary>
protected override QilNode VisitRtfCtor(QilBinary ndRtf) {
OptimizerPatterns patt = OptimizerPatterns.Read(ndRtf);
string baseUri = (string) (QilLiteral) ndRtf.Right;
if (patt.MatchesPattern(OptimizerPatternName.SingleTextRtf)) {
// Special-case Rtf containing a root node and a single text node child
this.helper.LoadQueryRuntime();
NestedVisitEnsureStack((QilNode) patt.GetArgument(OptimizerPatternArgument.RtfText));
this.helper.Emit(OpCodes.Ldstr, baseUri);
this.helper.Call(XmlILMethods.RtfConstr);
}
else {
// Start nested construction of an Rtf
this.helper.CallStartRtfConstruction(baseUri);
// Write content of Rtf to writer
NestedVisit(ndRtf.Left);
// Get the result Rtf
this.helper.CallEndRtfConstruction();
}
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(XPathNavigator), false);
return ndRtf;
}
/// <summary>
/// Generate code for QilNodeType.NameOf.
/// </summary>
protected override QilNode VisitNameOf(QilUnary ndName) {
return VisitNodeProperty(ndName);
}
/// <summary>
/// Generate code for QilNodeType.LocalNameOf.
/// </summary>
protected override QilNode VisitLocalNameOf(QilUnary ndName) {
return VisitNodeProperty(ndName);
}
/// <summary>
/// Generate code for QilNodeType.NamespaceUriOf.
/// </summary>
protected override QilNode VisitNamespaceUriOf(QilUnary ndName) {
return VisitNodeProperty(ndName);
}
/// <summary>
/// Generate code for QilNodeType.PrefixOf.
/// </summary>
protected override QilNode VisitPrefixOf(QilUnary ndName) {
return VisitNodeProperty(ndName);
}
/// <summary>
/// Generate code to push the local name, namespace uri, or qname of the context navigator.
/// </summary>
private QilNode VisitNodeProperty(QilUnary ndProp) {
// Generate code to push argument onto stack
NestedVisitEnsureStack(ndProp.Child);
switch (ndProp.NodeType) {
case QilNodeType.NameOf:
// push new XmlQualifiedName(navigator.LocalName, navigator.NamespaceURI);
this.helper.Emit(OpCodes.Dup);
this.helper.Call(XmlILMethods.NavLocalName);
this.helper.Call(XmlILMethods.NavNmsp);
this.helper.Construct(XmlILConstructors.QName);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(XmlQualifiedName), false);
break;
case QilNodeType.LocalNameOf:
// push navigator.Name;
this.helper.Call(XmlILMethods.NavLocalName);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(string), false);
break;
case QilNodeType.NamespaceUriOf:
// push navigator.NamespaceURI;
this.helper.Call(XmlILMethods.NavNmsp);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(string), false);
break;
case QilNodeType.PrefixOf:
// push navigator.Prefix;
this.helper.Call(XmlILMethods.NavPrefix);
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(string), false);
break;
default:
Debug.Assert(false);
break;
}
return ndProp;
}
/// <summary>
/// Find physical query plan for QilNodeType.TypeAssert.
/// </summary>
protected override QilNode VisitTypeAssert(QilTargetType ndTypeAssert) {
if (!ndTypeAssert.Source.XmlType.IsSingleton && ndTypeAssert.XmlType.IsSingleton && !this.iterCurr.HasLabelNext) {
// This case occurs when a non-singleton expression is treated as cardinality One.
// The trouble is that the expression will branch to an end label when it's done iterating, so
// an end label must be provided. But there is no next label in the current iteration context,
// so we've got to create a dummy label instead (IL requires it). This creates an infinite loop,
// but since it's known statically that the expression is cardinality One, this branch will never
// be taken.
Label lblDummy = this.helper.DefineLabel();
this.helper.MarkLabel(lblDummy);
NestedVisit(ndTypeAssert.Source, lblDummy);
}
else {
// Generate code for child expression
Visit(ndTypeAssert.Source);
}
this.iterCurr.EnsureItemStorageType(ndTypeAssert.Source.XmlType, GetItemStorageType(ndTypeAssert));
return ndTypeAssert;
}
/// <summary>
/// Generate code for QilNodeType.IsType.
/// </summary>
protected override QilNode VisitIsType(QilTargetType ndIsType) {
XmlQueryType typDerived, typBase;
XmlTypeCode codeBase;
typDerived = ndIsType.Source.XmlType;
typBase = ndIsType.TargetType;
Debug.Assert(!typDerived.NeverSubtypeOf(typBase), "Normalizer should have eliminated IsType where source can never be a subtype of destination type.");
// Special Case: Test whether singleton item is a Node
if (typDerived.IsSingleton && (object) typBase == (object) TypeFactory.Node) {
NestedVisitEnsureStack(ndIsType.Source);
Debug.Assert(this.iterCurr.Storage.ItemStorageType == typeof(XPathItem), "If !IsNode, then storage type should be Item");
// if (item.IsNode op true) goto LabelBranch;
this.helper.Call(XmlILMethods.ItemIsNode);
ZeroCompare(QilNodeType.Ne, true);
return ndIsType;
}
// Special Case: Source value is a singleton Node, and we're testing whether it is an Element, Attribute, PI, etc.
if (MatchesNodeKinds(ndIsType, typDerived, typBase))
return ndIsType;
// Special Case: XmlTypeCode is sufficient to describe destination type
if ((object) typBase == (object) TypeFactory.Double) codeBase = XmlTypeCode.Double;
else if ((object) typBase == (object) TypeFactory.String) codeBase = XmlTypeCode.String;
else if ((object) typBase == (object) TypeFactory.Boolean) codeBase = XmlTypeCode.Boolean;
else if ((object) typBase == (object) TypeFactory.Node) codeBase = XmlTypeCode.Node;
else codeBase = XmlTypeCode.None;
if (codeBase != XmlTypeCode.None) {
// if (runtime.MatchesXmlType(value, code) op true) goto LabelBranch;
this.helper.LoadQueryRuntime();
NestedVisitEnsureStack(ndIsType.Source, typeof(XPathItem), !typDerived.IsSingleton);
this.helper.LoadInteger((int) codeBase);
this.helper.Call(typDerived.IsSingleton ? XmlILMethods.ItemMatchesCode : XmlILMethods.SeqMatchesCode);
ZeroCompare(QilNodeType.Ne, true);
return ndIsType;
}
// if (runtime.MatchesXmlType(value, idxType) op true) goto LabelBranch;
this.helper.LoadQueryRuntime();
NestedVisitEnsureStack(ndIsType.Source, typeof(XPathItem), !typDerived.IsSingleton);
this.helper.LoadInteger(this.helper.StaticData.DeclareXmlType(typBase));
this.helper.Call(typDerived.IsSingleton ? XmlILMethods.ItemMatchesType : XmlILMethods.SeqMatchesType);
ZeroCompare(QilNodeType.Ne, true);
return ndIsType;
}
/// <summary>
/// Faster code can be generated if type test is just a node kind test. If this special case is detected, then generate code and return true.
/// Otherwise, return false, and a call to MatchesXmlType will be generated instead.
/// </summary>
private bool MatchesNodeKinds(QilTargetType ndIsType, XmlQueryType typDerived, XmlQueryType typBase) {
XmlNodeKindFlags kinds;
bool allowKinds = true;
XPathNodeType kindsRuntime;
int kindsUnion;
// If not checking whether typDerived is some kind of singleton node, then fallback to MatchesXmlType
if (!typBase.IsNode || !typBase.IsSingleton)
return false;
// If typDerived is not statically guaranteed to be a singleton node (and not an rtf), then fallback to MatchesXmlType
if (!typDerived.IsNode || !typDerived.IsSingleton || !typDerived.IsNotRtf)
return false;
// Now we are guaranteed that typDerived is a node, and typBase is a node, so check node kinds
// Ensure that typBase is only composed of kind-test prime types (no name-test, no schema-test, etc.)
kinds = XmlNodeKindFlags.None;
foreach (XmlQueryType typItem in typBase) {
if ((object) typItem == (object) TypeFactory.Element) kinds |= XmlNodeKindFlags.Element;
else if ((object) typItem == (object) TypeFactory.Attribute) kinds |= XmlNodeKindFlags.Attribute;
else if ((object) typItem == (object) TypeFactory.Text) kinds |= XmlNodeKindFlags.Text;
else if ((object) typItem == (object) TypeFactory.Document) kinds |= XmlNodeKindFlags.Document;
else if ((object) typItem == (object) TypeFactory.Comment) kinds |= XmlNodeKindFlags.Comment;
else if ((object) typItem == (object) TypeFactory.PI) kinds |= XmlNodeKindFlags.PI;
else if ((object) typItem == (object) TypeFactory.Namespace) kinds |= XmlNodeKindFlags.Namespace;
else return false;
}
Debug.Assert((typDerived.NodeKinds & kinds) != XmlNodeKindFlags.None, "Normalizer should have taken care of case where node kinds are disjoint.");
kinds = typDerived.NodeKinds & kinds;
// Attempt to allow or disallow exactly one kind
if (!Bits.ExactlyOne((uint) kinds)) {
// Not possible to allow one kind, so try to disallow one kind
kinds = ~kinds & XmlNodeKindFlags.Any;
allowKinds = !allowKinds;
}
switch (kinds) {
case XmlNodeKindFlags.Element: kindsRuntime = XPathNodeType.Element; break;
case XmlNodeKindFlags.Attribute: kindsRuntime = XPathNodeType.Attribute; break;
case XmlNodeKindFlags.Namespace: kindsRuntime = XPathNodeType.Namespace; break;
case XmlNodeKindFlags.PI: kindsRuntime = XPathNodeType.ProcessingInstruction; break;
case XmlNodeKindFlags.Comment: kindsRuntime = XPathNodeType.Comment; break;
case XmlNodeKindFlags.Document: kindsRuntime = XPathNodeType.Root; break;
default:
// Union of several types (when testing for Text, we need to test for Whitespace as well)
// if (((1 << navigator.NodeType) & nodesDisallow) op 0) goto LabelBranch;
this.helper.Emit(OpCodes.Ldc_I4_1);
kindsRuntime = XPathNodeType.All;
break;
}
// Push navigator.NodeType onto the stack
NestedVisitEnsureStack(ndIsType.Source);
this.helper.Call(XmlILMethods.NavType);
if (kindsRuntime == XPathNodeType.All) {
// if (((1 << navigator.NodeType) & kindsUnion) op 0) goto LabelBranch;
this.helper.Emit(OpCodes.Shl);
kindsUnion = 0;
if ((kinds & XmlNodeKindFlags.Document) != 0) kindsUnion |= (1 << (int) XPathNodeType.Root);
if ((kinds & XmlNodeKindFlags.Element) != 0) kindsUnion |= (1 << (int) XPathNodeType.Element);
if ((kinds & XmlNodeKindFlags.Attribute) != 0) kindsUnion |= (1 << (int) XPathNodeType.Attribute);
if ((kinds & XmlNodeKindFlags.Text) != 0) kindsUnion |= (1 << (int) (int) XPathNodeType.Text) |
(1 << (int) (int) XPathNodeType.SignificantWhitespace) |
(1 << (int) (int) XPathNodeType.Whitespace);
if ((kinds & XmlNodeKindFlags.Comment) != 0) kindsUnion |= (1 << (int) XPathNodeType.Comment);
if ((kinds & XmlNodeKindFlags.PI) != 0) kindsUnion |= (1 << (int) XPathNodeType.ProcessingInstruction);
if ((kinds & XmlNodeKindFlags.Namespace) != 0) kindsUnion |= (1 << (int) XPathNodeType.Namespace);
this.helper.LoadInteger(kindsUnion);
this.helper.Emit(OpCodes.And);
ZeroCompare(allowKinds ? QilNodeType.Ne : QilNodeType.Eq, false);
}
else {
// if (navigator.NodeType op runtimeItem) goto LabelBranch;
this.helper.LoadInteger((int) kindsRuntime);
ClrCompare(allowKinds ? QilNodeType.Eq : QilNodeType.Ne, XmlTypeCode.Int);
}
return true;
}
/// <summary>
/// Generate code for QilNodeType.IsEmpty.
/// </summary>
/// <remarks>
/// BranchingContext.OnFalse context: is-empty(expr)
/// ==> foreach (item in expr)
/// goto LabelBranch;
///
/// BranchingContext.OnTrue context: is-empty(expr)
/// ==> foreach (item in expr)
/// break;
/// ...
/// LabelOnEnd: (called if foreach is empty)
/// goto LabelBranch;
///
/// BranchingContext.None context: is-empty(expr)
/// ==> foreach (item in expr)
/// break;
/// push true();
/// ...
/// LabelOnEnd: (called if foreach is empty)
/// push false();
/// </remarks>
protected override QilNode VisitIsEmpty(QilUnary ndIsEmpty) {
Label lblTrue;
// If the child expression returns a cached result,
if (CachesResult(ndIsEmpty.Child)) {
// Then get the count directly from the cache
NestedVisitEnsureStack(ndIsEmpty.Child);
this.helper.CallCacheCount(this.iterNested.Storage.ItemStorageType);
switch (this.iterCurr.CurrentBranchingContext) {
case BranchingContext.OnFalse:
// Take false path if count != 0
this.helper.TestAndBranch(0, this.iterCurr.LabelBranch, OpCodes.Bne_Un);
break;
case BranchingContext.OnTrue:
// Take true path if count == 0
this.helper.TestAndBranch(0, this.iterCurr.LabelBranch, OpCodes.Beq);
break;
default:
Debug.Assert(this.iterCurr.CurrentBranchingContext == BranchingContext.None);
// if (count == 0) goto LabelTrue;
lblTrue = this.helper.DefineLabel();
this.helper.Emit(OpCodes.Brfalse_S, lblTrue);
// Convert branch targets into push of true/false
this.helper.ConvBranchToBool(lblTrue, true);
break;
}
}
else {
Label lblOnEnd = this.helper.DefineLabel();
IteratorDescriptor iterParent = this.iterCurr;
// Forward any LabelOnEnd jumps to LabelBranch if BranchingContext.OnTrue
if (iterParent.CurrentBranchingContext == BranchingContext.OnTrue)
StartNestedIterator(ndIsEmpty.Child, this.iterCurr.LabelBranch);
else
StartNestedIterator(ndIsEmpty.Child, lblOnEnd);
Visit(ndIsEmpty.Child);
// Pop value of IsEmpty expression from the stack if necessary
this.iterCurr.EnsureNoCache();
this.iterCurr.DiscardStack();
switch (iterParent.CurrentBranchingContext) {
case BranchingContext.OnFalse:
// Reverse polarity of iterator
this.helper.EmitUnconditionalBranch(OpCodes.Br, iterParent.LabelBranch);
this.helper.MarkLabel(lblOnEnd);
break;
case BranchingContext.OnTrue:
// Nothing to do
break;
case BranchingContext.None:
// Convert branch targets into push of true/false
this.helper.ConvBranchToBool(lblOnEnd, true);
break;
}
// End nested iterator
EndNestedIterator(ndIsEmpty.Child);
}
if (this.iterCurr.IsBranching)
this.iterCurr.Storage = StorageDescriptor.None();
else
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(bool), false);
return ndIsEmpty;
}
/// <summary>
/// Generate code for QilNodeType.XPathNodeValue.
/// </summary>
protected override QilNode VisitXPathNodeValue(QilUnary ndVal) {
Label lblOnEnd, lblDone;
Debug.Assert(ndVal.Child.XmlType.IsNode, "XPathNodeValue node may only be applied to a sequence of Nodes.");
// If the expression is a singleton,
if (ndVal.Child.XmlType.IsSingleton) {
// Then generate code to push expresion result onto the stack
NestedVisitEnsureStack(ndVal.Child, typeof(XPathNavigator), false);
// navigator.Value;
this.helper.Call(XmlILMethods.Value);
}
else {
lblOnEnd = this.helper.DefineLabel();
// Construct nested iterator and iterate over results
StartNestedIterator(ndVal.Child, lblOnEnd);
Visit(ndVal.Child);
this.iterCurr.EnsureStackNoCache();
// navigator.Value;
this.helper.Call(XmlILMethods.Value);
// Handle empty sequence by pushing empty string onto the stack
lblDone = this.helper.DefineLabel();
this.helper.EmitUnconditionalBranch(OpCodes.Br, lblDone);
this.helper.MarkLabel(lblOnEnd);
this.helper.Emit(OpCodes.Ldstr, "");
this.helper.MarkLabel(lblDone);
// End nested iterator
EndNestedIterator(ndVal.Child);
}
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(string), false);
return ndVal;
}
/// <summary>
/// Find physical query plan for QilNodeType.XPathFollowing.
/// </summary>
protected override QilNode VisitXPathFollowing(QilUnary ndFoll) {
CreateFilteredIterator(ndFoll.Child, "$$$iterFoll", typeof(XPathFollowingIterator), XmlILMethods.XPFollCreate, XmlILMethods.XPFollNext,
XmlNodeKindFlags.Any, null, TriState.Unknown, null);
return ndFoll;
}
/// <summary>
/// Find physical query plan for QilNodeType.XPathPreceding.
/// </summary>
protected override QilNode VisitXPathPreceding(QilUnary ndPrec) {
CreateFilteredIterator(ndPrec.Child, "$$$iterPrec", typeof(XPathPrecedingIterator), XmlILMethods.XPPrecCreate, XmlILMethods.XPPrecNext,
XmlNodeKindFlags.Any, null, TriState.Unknown, null);
return ndPrec;
}
/// <summary>
/// Find physical query plan for QilNodeType.XPathNamespace.
/// </summary>
protected override QilNode VisitXPathNamespace(QilUnary ndNmsp) {
CreateSimpleIterator(ndNmsp.Child, "$$$iterNmsp", typeof(NamespaceIterator), XmlILMethods.NmspCreate, XmlILMethods.NmspNext);
return ndNmsp;
}
/// <summary>
/// Generate code for QilNodeType.XsltGenerateId.
/// </summary>
protected override QilNode VisitXsltGenerateId(QilUnary ndGenId) {
Label lblOnEnd, lblDone;
this.helper.LoadQueryRuntime();
// If the expression is a singleton,
if (ndGenId.Child.XmlType.IsSingleton) {
// Then generate code to push expresion result onto the stack
NestedVisitEnsureStack(ndGenId.Child, typeof(XPathNavigator), false);
// runtime.GenerateId(value);
this.helper.Call(XmlILMethods.GenId);
}
else {
lblOnEnd = this.helper.DefineLabel();
// Construct nested iterator and iterate over results
StartNestedIterator(ndGenId.Child, lblOnEnd);
Visit(ndGenId.Child);
this.iterCurr.EnsureStackNoCache();
this.iterCurr.EnsureItemStorageType(ndGenId.Child.XmlType, typeof(XPathNavigator));
// runtime.GenerateId(value);
this.helper.Call(XmlILMethods.GenId);
// Handle empty sequence by pushing empty string onto the stack
lblDone = this.helper.DefineLabel();
this.helper.EmitUnconditionalBranch(OpCodes.Br, lblDone);
this.helper.MarkLabel(lblOnEnd);
this.helper.Emit(OpCodes.Pop);
this.helper.Emit(OpCodes.Ldstr, "");
this.helper.MarkLabel(lblDone);
// End nested iterator
EndNestedIterator(ndGenId.Child);
}
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(string), false);
return ndGenId;
}
/// <summary>
/// Generate code for for QilNodeType.XsltInvokeLateBound.
/// </summary>
protected override QilNode VisitXsltInvokeLateBound(QilInvokeLateBound ndInvoke) {
LocalBuilder locArgs = this.helper.DeclareLocal("$$$args", typeof(IList<XPathItem>[]));
QilName ndName = (QilName) ndInvoke.Name;
Debug.Assert(XmlILConstructInfo.Read(ndInvoke).ConstructMethod != XmlILConstructMethod.Writer);
// runtime.ExternalContext.InvokeXsltLateBoundFunction(name, ns, args);
this.helper.LoadQueryContext();
this.helper.Emit(OpCodes.Ldstr, ndName.LocalName);
this.helper.Emit(OpCodes.Ldstr, ndName.NamespaceUri);
// args = new IList<XPathItem>[argCount];
this.helper.LoadInteger(ndInvoke.Arguments.Count);
this.helper.Emit(OpCodes.Newarr, typeof(IList<XPathItem>));
this.helper.Emit(OpCodes.Stloc, locArgs);
for (int iArg = 0; iArg < ndInvoke.Arguments.Count; iArg++) {
QilNode ndArg = ndInvoke.Arguments[iArg];
// args[0] = arg0;
// ...
// args[N] = argN;
this.helper.Emit(OpCodes.Ldloc, locArgs);
this.helper.LoadInteger(iArg);
this.helper.Emit(OpCodes.Ldelema, typeof(IList<XPathItem>));
NestedVisitEnsureCache(ndArg, typeof(XPathItem));
this.iterCurr.EnsureStack();
this.helper.Emit(OpCodes.Stobj, typeof(IList<XPathItem>));
}
this.helper.Emit(OpCodes.Ldloc, locArgs);
this.helper.Call(XmlILMethods.InvokeXsltLate);
// Returned item sequence is on the stack
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(XPathItem), true);
return ndInvoke;
}
/// <summary>
/// Generate code for for QilNodeType.XsltInvokeEarlyBound.
/// </summary>
protected override QilNode VisitXsltInvokeEarlyBound(QilInvokeEarlyBound ndInvoke) {
QilName ndName = ndInvoke.Name;
XmlExtensionFunction extFunc;
Type clrTypeRetSrc, clrTypeRetDst;
// Retrieve metadata from the extension function
extFunc = new XmlExtensionFunction(ndName.LocalName, ndName.NamespaceUri, ndInvoke.ClrMethod);
clrTypeRetSrc = extFunc.ClrReturnType;
clrTypeRetDst = GetStorageType(ndInvoke);
// Prepare to call runtime.ChangeTypeXsltResult
if (clrTypeRetSrc != clrTypeRetDst && !ndInvoke.XmlType.IsEmpty) {
this.helper.LoadQueryRuntime();
this.helper.LoadInteger(this.helper.StaticData.DeclareXmlType(ndInvoke.XmlType));
}
// If this is not a static method, then get the instance object
if (!extFunc.Method.IsStatic) {
// Special-case the XsltLibrary object
if (ndName.NamespaceUri.Length == 0)
this.helper.LoadXsltLibrary();
else
this.helper.CallGetEarlyBoundObject(this.helper.StaticData.DeclareEarlyBound(ndName.NamespaceUri, extFunc.Method.DeclaringType), extFunc.Method.DeclaringType);
}
// Generate code to push each Invoke argument onto the stack
for (int iArg = 0; iArg < ndInvoke.Arguments.Count; iArg++) {
QilNode ndActualArg;
XmlQueryType xmlTypeFormalArg;
Type clrTypeActualArg, clrTypeFormalArg;
ndActualArg = ndInvoke.Arguments[iArg];
// Infer Xml type and Clr type of formal argument
xmlTypeFormalArg = extFunc.GetXmlArgumentType(iArg);
clrTypeFormalArg = extFunc.GetClrArgumentType(iArg);
Debug.Assert(ndActualArg.XmlType.IsSubtypeOf(xmlTypeFormalArg), "Xml type of actual arg must be a subtype of the Xml type of the formal arg");
// Use different conversion rules for internal Xslt libraries. If the actual argument is
// stored using Clr type T, then library must use type T, XPathItem, IList<T>, or IList<XPathItem>.
// If the actual argument is stored using Clr type IList<T>, then library must use type
// IList<T> or IList<XPathItem>. This is to ensure that there will not be unnecessary
// conversions that take place when calling into an internal library.
if (ndName.NamespaceUri.Length == 0) {
Type itemType = GetItemStorageType(ndActualArg);
if (clrTypeFormalArg == XmlILMethods.StorageMethods[itemType].IListType) {
// Formal type is IList<T>
NestedVisitEnsureStack(ndActualArg, itemType, true);
}
else if (clrTypeFormalArg == XmlILMethods.StorageMethods[typeof(XPathItem)].IListType) {
// Formal type is IList<XPathItem>
NestedVisitEnsureStack(ndActualArg, typeof(XPathItem), true);
}
else if ((ndActualArg.XmlType.IsSingleton && clrTypeFormalArg == itemType) || ndActualArg.XmlType.TypeCode == XmlTypeCode.None) {
// Formal type is T
NestedVisitEnsureStack(ndActualArg, clrTypeFormalArg, false);
}
else if (ndActualArg.XmlType.IsSingleton && clrTypeFormalArg == typeof(XPathItem)) {
// Formal type is XPathItem
NestedVisitEnsureStack(ndActualArg, typeof(XPathItem), false);
}
else
Debug.Fail("Internal Xslt library may not use parameters of type " + clrTypeFormalArg);
}
else {
// There is an implicit upcast to the Xml type of the formal argument. This can change the Clr storage type.
clrTypeActualArg = GetStorageType(xmlTypeFormalArg);
// If the formal Clr type is typeof(object) or if it is not a supertype of the actual Clr type, then call ChangeTypeXsltArgument
if (xmlTypeFormalArg.TypeCode == XmlTypeCode.Item || !clrTypeFormalArg.IsAssignableFrom(clrTypeActualArg)) {
// (clrTypeFormalArg) runtime.ChangeTypeXsltArgument(xmlTypeFormalArg, (object) value, clrTypeFormalArg);
this.helper.LoadQueryRuntime();
this.helper.LoadInteger(this.helper.StaticData.DeclareXmlType(xmlTypeFormalArg));
NestedVisitEnsureStack(ndActualArg, GetItemStorageType(xmlTypeFormalArg), !xmlTypeFormalArg.IsSingleton);
this.helper.TreatAs(clrTypeActualArg, typeof(object));
this.helper.LoadType(clrTypeFormalArg);
this.helper.Call(XmlILMethods.ChangeTypeXsltArg);
this.helper.TreatAs(typeof(object), clrTypeFormalArg);
}
else {
NestedVisitEnsureStack(ndActualArg, GetItemStorageType(xmlTypeFormalArg), !xmlTypeFormalArg.IsSingleton);
}
}
}
// Invoke the target method
this.helper.Call(extFunc.Method);
// Return value is on the stack; convert it to canonical ILGen storage type
if (ndInvoke.XmlType.IsEmpty) {
this.helper.Emit(OpCodes.Ldsfld, XmlILMethods.StorageMethods[typeof(XPathItem)].SeqEmpty);
}
else if (clrTypeRetSrc != clrTypeRetDst) {
// (T) runtime.ChangeTypeXsltResult(idxType, (object) value);
this.helper.TreatAs(clrTypeRetSrc, typeof(object));
this.helper.Call(XmlILMethods.ChangeTypeXsltResult);
this.helper.TreatAs(typeof(object), clrTypeRetDst);
}
else if (ndName.NamespaceUri.Length != 0 && !clrTypeRetSrc.IsValueType){
// Check for null if a user-defined extension function returns a reference type
Label lblSkip = this.helper.DefineLabel();
this.helper.Emit(OpCodes.Dup);
this.helper.Emit(OpCodes.Brtrue, lblSkip);
this.helper.LoadQueryRuntime();
this.helper.Emit(OpCodes.Ldstr, Res.GetString(Res.Xslt_ItemNull));
this.helper.Call(XmlILMethods.ThrowException);
this.helper.MarkLabel(lblSkip);
}
this.iterCurr.Storage = StorageDescriptor.Stack(GetItemStorageType(ndInvoke), !ndInvoke.XmlType.IsSingleton);
return ndInvoke;
}
/// <summary>
/// Generate code for QilNodeType.XsltCopy.
/// </summary>
protected override QilNode VisitXsltCopy(QilBinary ndCopy) {
Label lblSkipContent = this.helper.DefineLabel();
Debug.Assert(XmlILConstructInfo.Read(ndCopy).PushToWriterFirst);
// if (!xwrtChk.StartCopyChk(navCopy)) goto LabelSkipContent;
this.helper.LoadQueryOutput();
NestedVisitEnsureStack(ndCopy.Left);
Debug.Assert(ndCopy.Left.XmlType.IsNode);
this.helper.Call(XmlILMethods.StartCopy);
this.helper.Emit(OpCodes.Brfalse, lblSkipContent);
// Recursively construct content
NestedVisit(ndCopy.Right);
// xwrtChk.EndCopyChk(navCopy);
this.helper.LoadQueryOutput();
NestedVisitEnsureStack(ndCopy.Left);
Debug.Assert(ndCopy.Left.XmlType.IsNode);
this.helper.Call(XmlILMethods.EndCopy);
// LabelSkipContent:
this.helper.MarkLabel(lblSkipContent);
this.iterCurr.Storage = StorageDescriptor.None();
return ndCopy;
}
/// <summary>
/// Generate code for QilNodeType.XsltCopyOf.
/// </summary>
protected override QilNode VisitXsltCopyOf(QilUnary ndCopyOf) {
Debug.Assert(XmlILConstructInfo.Read(ndCopyOf).PushToWriterFirst, "XsltCopyOf should always be pushed to writer.");
this.helper.LoadQueryOutput();
// XmlQueryOutput.XsltCopyOf(navigator);
NestedVisitEnsureStack(ndCopyOf.Child);
this.helper.Call(XmlILMethods.CopyOf);
this.iterCurr.Storage = StorageDescriptor.None();
return ndCopyOf;
}
/// <summary>
/// Generate code for QilNodeType.XsltConvert.
/// </summary>
protected override QilNode VisitXsltConvert(QilTargetType ndConv) {
XmlQueryType typSrc, typDst;
MethodInfo meth;
typSrc = ndConv.Source.XmlType;
typDst = ndConv.TargetType;
if (GetXsltConvertMethod(typSrc, typDst, out meth)) {
NestedVisitEnsureStack(ndConv.Source);
}
else {
// If a conversion could not be found, then convert the source expression to item or item* and try again
NestedVisitEnsureStack(ndConv.Source, typeof(XPathItem), !typSrc.IsSingleton);
if (!GetXsltConvertMethod(typSrc.IsSingleton ? TypeFactory.Item : TypeFactory.ItemS, typDst, out meth))
Debug.Fail("Conversion from " + ndConv.Source.XmlType + " to " + ndConv.TargetType + " is not supported.");
}
// XsltConvert.XXXToYYY(value);
if (meth != null)
this.helper.Call(meth);
this.iterCurr.Storage = StorageDescriptor.Stack(GetItemStorageType(typDst), !typDst.IsSingleton);
return ndConv;
}
/// <summary>
/// Get the XsltConvert method that converts from "typSrc" to "typDst". Return false if no
/// such method exists. This conversion matrix should match the one in XsltConvert.ExternalValueToExternalValue.
/// </summary>
private bool GetXsltConvertMethod(XmlQueryType typSrc, XmlQueryType typDst, out MethodInfo meth) {
meth = null;
// Note, Ref.Equals is OK to use here, since we will always fall back to Item or Item* in the
// case where the source or destination types do not match the static types exposed on the
// XmlQueryTypeFactory. This is bad for perf if it accidentally occurs, but the results
// should still be correct.
// => xs:boolean
if ((object) typDst == (object) TypeFactory.BooleanX) {
if ((object) typSrc == (object) TypeFactory.Item) meth = XmlILMethods.ItemToBool;
else if ((object) typSrc == (object) TypeFactory.ItemS) meth = XmlILMethods.ItemsToBool;
}
// => xs:dateTime
else if ((object) typDst == (object) TypeFactory.DateTimeX) {
if ((object) typSrc == (object) TypeFactory.StringX) meth = XmlILMethods.StrToDT;
}
// => xs:decimal
else if ((object) typDst == (object) TypeFactory.DecimalX) {
if ((object) typSrc == (object) TypeFactory.DoubleX) meth = XmlILMethods.DblToDec;
}
// => xs:double
else if ((object) typDst == (object) TypeFactory.DoubleX) {
if ((object) typSrc == (object) TypeFactory.DecimalX) meth = XmlILMethods.DecToDbl;
else if ((object) typSrc == (object) TypeFactory.IntX) meth = XmlILMethods.IntToDbl;
else if ((object) typSrc == (object) TypeFactory.Item) meth = XmlILMethods.ItemToDbl;
else if ((object) typSrc == (object) TypeFactory.ItemS) meth = XmlILMethods.ItemsToDbl;
else if ((object) typSrc == (object) TypeFactory.LongX) meth = XmlILMethods.LngToDbl;
else if ((object) typSrc == (object) TypeFactory.StringX) meth = XmlILMethods.StrToDbl;
}
// => xs:int
else if ((object) typDst == (object) TypeFactory.IntX) {
if ((object) typSrc == (object) TypeFactory.DoubleX) meth = XmlILMethods.DblToInt;
}
// => xs:long
else if ((object) typDst == (object) TypeFactory.LongX) {
if ((object) typSrc == (object) TypeFactory.DoubleX) meth = XmlILMethods.DblToLng;
}
// => node
else if ((object) typDst == (object) TypeFactory.NodeNotRtf) {
if ((object) typSrc == (object) TypeFactory.Item) meth = XmlILMethods.ItemToNode;
else if ((object) typSrc == (object) TypeFactory.ItemS) meth = XmlILMethods.ItemsToNode;
}
// => node*
else if ((object) typDst == (object) TypeFactory.NodeSDod ||
(object) typDst == (object) TypeFactory.NodeNotRtfS) {
if ((object) typSrc == (object) TypeFactory.Item) meth = XmlILMethods.ItemToNodes;
else if ((object) typSrc == (object) TypeFactory.ItemS) meth = XmlILMethods.ItemsToNodes;
}
// => xs:string
else if ((object) typDst == (object) TypeFactory.StringX) {
if ((object) typSrc == (object) TypeFactory.DateTimeX) meth = XmlILMethods.DTToStr;
else if ((object) typSrc == (object) TypeFactory.DoubleX) meth = XmlILMethods.DblToStr;
else if ((object) typSrc == (object) TypeFactory.Item) meth = XmlILMethods.ItemToStr;
else if ((object) typSrc == (object) TypeFactory.ItemS) meth = XmlILMethods.ItemsToStr;
}
return meth != null;
}
//-----------------------------------------------
// Helper methods
//-----------------------------------------------
/// <summary>
/// Ensure that the "locNav" navigator is positioned to the context node "ndCtxt".
/// </summary>
private void SyncToNavigator(LocalBuilder locNav, QilNode ndCtxt) {
this.helper.Emit(OpCodes.Ldloc, locNav);
NestedVisitEnsureStack(ndCtxt);
this.helper.CallSyncToNavigator();
this.helper.Emit(OpCodes.Stloc, locNav);
}
/// <summary>
/// Generate boiler-plate code to create a simple Xml iterator.
/// </summary>
/// <remarks>
/// Iterator iter;
/// iter.Create(navCtxt);
/// LabelNext:
/// if (!iter.MoveNext())
/// goto LabelNextCtxt;
/// </remarks>
private void CreateSimpleIterator(QilNode ndCtxt, string iterName, Type iterType, MethodInfo methCreate, MethodInfo methNext) {
// Iterator iter;
LocalBuilder locIter = this.helper.DeclareLocal(iterName, iterType);
// iter.Create(navCtxt);
this.helper.Emit(OpCodes.Ldloca, locIter);
NestedVisitEnsureStack(ndCtxt);
this.helper.Call(methCreate);
GenerateSimpleIterator(typeof(XPathNavigator), locIter, methNext);
}
/// <summary>
/// Generate boiler-plate code to create an Xml iterator that uses an XmlNavigatorFilter to filter items.
/// </summary>
/// <remarks>
/// Iterator iter;
/// iter.Create(navCtxt, filter [, orSelf] [, navEnd]);
/// LabelNext:
/// if (!iter.MoveNext())
/// goto LabelNextCtxt;
/// </remarks>
private void CreateFilteredIterator(QilNode ndCtxt, string iterName, Type iterType, MethodInfo methCreate, MethodInfo methNext,
XmlNodeKindFlags kinds, QilName ndName, TriState orSelf, QilNode ndEnd) {
// Iterator iter;
LocalBuilder locIter = this.helper.DeclareLocal(iterName, iterType);
// iter.Create(navCtxt, filter [, orSelf], [, navEnd]);
this.helper.Emit(OpCodes.Ldloca, locIter);
NestedVisitEnsureStack(ndCtxt);
LoadSelectFilter(kinds, ndName);
if (orSelf != TriState.Unknown)
this.helper.LoadBoolean(orSelf == TriState.True);
if (ndEnd != null)
NestedVisitEnsureStack(ndEnd);
this.helper.Call(methCreate);
GenerateSimpleIterator(typeof(XPathNavigator), locIter, methNext);
}
/// <summary>
/// Generate boiler-plate code to create an Xml iterator that controls a nested iterator.
/// </summary>
/// <remarks>
/// Iterator iter;
/// iter.Create(filter [, orSelf]);
/// ...nested iterator...
/// navInput = nestedNested;
/// goto LabelCall;
/// LabelNext:
/// navInput = null;
/// LabelCall:
/// switch (iter.MoveNext(navInput)) {
/// case IteratorState.NoMoreNodes: goto LabelNextCtxt;
/// case IteratorState.NextInputNode: goto LabelNextNested;
/// }
/// </remarks>
private void CreateContainerIterator(QilUnary ndDod, string iterName, Type iterType, MethodInfo methCreate, MethodInfo methNext,
XmlNodeKindFlags kinds, QilName ndName, TriState orSelf) {
// Iterator iter;
LocalBuilder locIter = this.helper.DeclareLocal(iterName, iterType);
Label lblOnEndNested;
QilLoop ndLoop = (QilLoop) ndDod.Child;
Debug.Assert(ndDod.NodeType == QilNodeType.DocOrderDistinct && ndLoop != null);
// iter.Create(filter [, orSelf]);
this.helper.Emit(OpCodes.Ldloca, locIter);
LoadSelectFilter(kinds, ndName);
if (orSelf != TriState.Unknown)
this.helper.LoadBoolean(orSelf == TriState.True);
this.helper.Call(methCreate);
// Generate nested iterator (branch to lblOnEndNested when iteration is complete)
lblOnEndNested = this.helper.DefineLabel();
StartNestedIterator(ndLoop, lblOnEndNested);
StartBinding(ndLoop.Variable);
EndBinding(ndLoop.Variable);
EndNestedIterator(ndLoop.Variable);
this.iterCurr.Storage = this.iterNested.Storage;
GenerateContainerIterator(ndDod, locIter, lblOnEndNested, methNext, typeof(XPathNavigator));
}
/// <summary>
/// Generate boiler-plate code that calls MoveNext on a simple Xml iterator. Iterator should have already been
/// created by calling code.
/// </summary>
/// <remarks>
/// ...
/// LabelNext:
/// if (!iter.MoveNext())
/// goto LabelNextCtxt;
/// </remarks>
private void GenerateSimpleIterator(Type itemStorageType, LocalBuilder locIter, MethodInfo methNext) {
Label lblNext;
// LabelNext:
lblNext = this.helper.DefineLabel();
this.helper.MarkLabel(lblNext);
// if (!iter.MoveNext()) goto LabelNextCtxt;
this.helper.Emit(OpCodes.Ldloca, locIter);
this.helper.Call(methNext);
this.helper.Emit(OpCodes.Brfalse, this.iterCurr.GetLabelNext());
this.iterCurr.SetIterator(lblNext, StorageDescriptor.Current(locIter, itemStorageType));
}
/// <summary>
/// Generate boiler-plate code that calls MoveNext on an Xml iterator that controls a nested iterator. Iterator should
/// have already been created by calling code.
/// </summary>
/// <remarks>
/// ...
/// goto LabelCall;
/// LabelNext:
/// navCtxt = null;
/// LabelCall:
/// switch (iter.MoveNext(navCtxt)) {
/// case IteratorState.NoMoreNodes: goto LabelNextCtxt;
/// case IteratorState.NextInputNode: goto LabelNextNested;
/// }
/// </remarks>
private void GenerateContainerIterator(QilNode nd, LocalBuilder locIter, Label lblOnEndNested,
MethodInfo methNext, Type itemStorageType) {
Label lblCall;
// Define labels that will be used
lblCall = this.helper.DefineLabel();
// iter.MoveNext(input);
// goto LabelCall;
this.iterCurr.EnsureNoStackNoCache(nd.XmlType.IsNode ? "$$$navInput" : "$$$itemInput");
this.helper.Emit(OpCodes.Ldloca, locIter);
this.iterCurr.PushValue();
this.helper.EmitUnconditionalBranch(OpCodes.Br, lblCall);
// LabelNext:
// iterSet.MoveNext(null);
this.helper.MarkLabel(lblOnEndNested);
this.helper.Emit(OpCodes.Ldloca, locIter);
this.helper.Emit(OpCodes.Ldnull);
// LabelCall:
// result = iter.MoveNext(input);
this.helper.MarkLabel(lblCall);
this.helper.Call(methNext);
// If this iterator always returns a single node, then NoMoreNodes will never be returned
if (nd.XmlType.IsSingleton) {
// if (result == IteratorResult.NeedInputNode) goto LabelNextInput;
this.helper.LoadInteger((int) IteratorResult.NeedInputNode);
this.helper.Emit(OpCodes.Beq, this.iterNested.GetLabelNext());
this.iterCurr.Storage = StorageDescriptor.Current(locIter, itemStorageType);
}
else {
// switch (iter.MoveNext(input)) {
// case IteratorResult.NoMoreNodes: goto LabelNextCtxt;
// case IteratorResult.NeedInputNode: goto LabelNextInput;
// }
this.helper.Emit(OpCodes.Switch, new Label[] {this.iterCurr.GetLabelNext(), this.iterNested.GetLabelNext()});
this.iterCurr.SetIterator(lblOnEndNested, StorageDescriptor.Current(locIter, itemStorageType));
}
}
/// <summary>
/// Load XmlQueryOutput, load a name (computed or literal) and load an index to an Xml schema type.
/// Return an enumeration that specifies what kind of name was loaded.
/// </summary>
private GenerateNameType LoadNameAndType(XPathNodeType nodeType, QilNode ndName, bool isStart, bool callChk) {
QilName ndLiteralName;
string prefix, localName, ns;
GenerateNameType nameType;
Debug.Assert(ndName.XmlType.TypeCode == XmlTypeCode.QName, "Element or attribute name must have QName type.");
this.helper.LoadQueryOutput();
// 0. Default is to pop names off stack
nameType = GenerateNameType.StackName;
// 1. Literal names
if (ndName.NodeType == QilNodeType.LiteralQName) {
// If checks need to be made on End construction, then always pop names from stack
if (isStart || !callChk) {
ndLiteralName = ndName as QilName;
prefix = ndLiteralName.Prefix;
localName = ndLiteralName.LocalName;
ns = ndLiteralName.NamespaceUri;
// Check local name, namespace parts in debug code
Debug.Assert(ValidateNames.ValidateName(prefix, localName, ns, nodeType, ValidateNames.Flags.AllExceptPrefixMapping));
// If the namespace is empty,
if (ndLiteralName.NamespaceUri.Length == 0) {
// Then always call method on XmlQueryOutput
this.helper.Emit(OpCodes.Ldstr, ndLiteralName.LocalName);
return GenerateNameType.LiteralLocalName;
}
// If prefix is not valid for the node type,
if (!ValidateNames.ValidateName(prefix, localName, ns, nodeType, ValidateNames.Flags.CheckPrefixMapping)) {
// Then construct a new prefix at run-time
if (isStart) {
this.helper.Emit(OpCodes.Ldstr, localName);
this.helper.Emit(OpCodes.Ldstr, ns);
this.helper.Construct(XmlILConstructors.QName);
nameType = GenerateNameType.QName;
}
}
else {
// Push string parts
this.helper.Emit(OpCodes.Ldstr, prefix);
this.helper.Emit(OpCodes.Ldstr, localName);
this.helper.Emit(OpCodes.Ldstr, ns);
nameType = GenerateNameType.LiteralName;
}
}
}
else {
if (isStart) {
// 2. Copied names
if (ndName.NodeType == QilNodeType.NameOf) {
// Preserve prefix of source node, so just push navigator onto stack
NestedVisitEnsureStack((ndName as QilUnary).Child);
nameType = GenerateNameType.CopiedName;
}
// 3. Parsed tag names (foo:bar)
else if (ndName.NodeType == QilNodeType.StrParseQName) {
// Preserve prefix from parsed tag name
VisitStrParseQName(ndName as QilBinary, true);
// Type of name depends upon data-type of name argument
if ((ndName as QilBinary).Right.XmlType.TypeCode == XmlTypeCode.String)
nameType = GenerateNameType.TagNameAndNamespace;
else
nameType = GenerateNameType.TagNameAndMappings;
}
// 4. Other computed qnames
else {
// Push XmlQualifiedName onto the stack
NestedVisitEnsureStack(ndName);
nameType = GenerateNameType.QName;
}
}
}
return nameType;
}
/// <summary>
/// If the first argument is a constant value that evaluates to zero, then a more optimal instruction sequence
/// can be generated that does not have to push the zero onto the stack. Instead, a Brfalse or Brtrue instruction
/// can be used.
/// </summary>
private bool TryZeroCompare(QilNodeType relOp, QilNode ndFirst, QilNode ndSecond) {
Debug.Assert(relOp == QilNodeType.Eq || relOp == QilNodeType.Ne);
switch (ndFirst.NodeType) {
case QilNodeType.LiteralInt64:
if ((int) (QilLiteral) ndFirst != 0) return false;
break;
case QilNodeType.LiteralInt32:
if ((int) (QilLiteral) ndFirst != 0) return false;
break;
case QilNodeType.False:
break;
case QilNodeType.True:
// Inverse of QilNodeType.False
relOp = (relOp == QilNodeType.Eq) ? QilNodeType.Ne : QilNodeType.Eq;
break;
default:
return false;
}
// Generate code to push second argument on stack
NestedVisitEnsureStack(ndSecond);
// Generate comparison code -- op == 0 or op != 0
ZeroCompare(relOp, ndSecond.XmlType.TypeCode == XmlTypeCode.Boolean);
return true;
}
/// <summary>
/// If the comparison involves a qname, then perform comparison using atoms and return true.
/// Otherwise, return false (caller will perform comparison).
/// </summary>
private bool TryNameCompare(QilNodeType relOp, QilNode ndFirst, QilNode ndSecond) {
Debug.Assert(relOp == QilNodeType.Eq || relOp == QilNodeType.Ne);
if (ndFirst.NodeType == QilNodeType.NameOf) {
switch (ndSecond.NodeType) {
case QilNodeType.NameOf:
case QilNodeType.LiteralQName: {
this.helper.LoadQueryRuntime();
// Push left navigator onto the stack
NestedVisitEnsureStack((ndFirst as QilUnary).Child);
// Push the local name and namespace uri of the right argument onto the stack
if (ndSecond.NodeType == QilNodeType.LiteralQName) {
QilName ndName = ndSecond as QilName;
this.helper.LoadInteger(this.helper.StaticData.DeclareName(ndName.LocalName));
this.helper.LoadInteger(this.helper.StaticData.DeclareName(ndName.NamespaceUri));
// push runtime.IsQNameEqual(navigator, localName, namespaceUri)
this.helper.Call(XmlILMethods.QNameEqualLit);
}
else {
// Generate code to locate the navigator argument of NameOf operator
Debug.Assert(ndSecond.NodeType == QilNodeType.NameOf);
NestedVisitEnsureStack(ndSecond);
// push runtime.IsQNameEqual(nav1, nav2)
this.helper.Call(XmlILMethods.QNameEqualNav);
}
// Branch based on boolean result or push boolean value
ZeroCompare((relOp == QilNodeType.Eq) ? QilNodeType.Ne : QilNodeType.Eq, true);
return true;
}
}
}
// Caller must perform comparison
return false;
}
/// <summary>
/// For QilExpression types that map directly to CLR primitive types, the built-in CLR comparison operators can
/// be used to perform the specified relational operation.
/// </summary>
private void ClrCompare(QilNodeType relOp, XmlTypeCode code) {
OpCode opcode;
Label lblTrue;
switch (this.iterCurr.CurrentBranchingContext) {
case BranchingContext.OnFalse:
// Reverse the comparison operator
// Use Bxx_Un OpCodes to handle NaN case for double and single types
if (code == XmlTypeCode.Double || code == XmlTypeCode.Float) {
switch (relOp) {
case QilNodeType.Gt: opcode = OpCodes.Ble_Un; break;
case QilNodeType.Ge: opcode = OpCodes.Blt_Un; break;
case QilNodeType.Lt: opcode = OpCodes.Bge_Un; break;
case QilNodeType.Le: opcode = OpCodes.Bgt_Un; break;
case QilNodeType.Eq: opcode = OpCodes.Bne_Un; break;
case QilNodeType.Ne: opcode = OpCodes.Beq; break;
default: Debug.Assert(false); opcode = OpCodes.Nop; break;
}
}
else {
switch (relOp)
{
case QilNodeType.Gt: opcode = OpCodes.Ble; break;
case QilNodeType.Ge: opcode = OpCodes.Blt; break;
case QilNodeType.Lt: opcode = OpCodes.Bge; break;
case QilNodeType.Le: opcode = OpCodes.Bgt; break;
case QilNodeType.Eq: opcode = OpCodes.Bne_Un; break;
case QilNodeType.Ne: opcode = OpCodes.Beq; break;
default: Debug.Assert(false); opcode = OpCodes.Nop; break;
}
}
this.helper.Emit(opcode, this.iterCurr.LabelBranch);
this.iterCurr.Storage = StorageDescriptor.None();
break;
case BranchingContext.OnTrue:
switch (relOp) {
case QilNodeType.Gt: opcode = OpCodes.Bgt; break;
case QilNodeType.Ge: opcode = OpCodes.Bge; break;
case QilNodeType.Lt: opcode = OpCodes.Blt; break;
case QilNodeType.Le: opcode = OpCodes.Ble; break;
case QilNodeType.Eq: opcode = OpCodes.Beq; break;
case QilNodeType.Ne: opcode = OpCodes.Bne_Un; break;
default: Debug.Assert(false); opcode = OpCodes.Nop; break;
}
this.helper.Emit(opcode, this.iterCurr.LabelBranch);
this.iterCurr.Storage = StorageDescriptor.None();
break;
default:
Debug.Assert(this.iterCurr.CurrentBranchingContext == BranchingContext.None);
switch (relOp) {
case QilNodeType.Gt: this.helper.Emit(OpCodes.Cgt); break;
case QilNodeType.Lt: this.helper.Emit(OpCodes.Clt); break;
case QilNodeType.Eq: this.helper.Emit(OpCodes.Ceq); break;
default:
switch (relOp) {
case QilNodeType.Ge: opcode = OpCodes.Bge_S; break;
case QilNodeType.Le: opcode = OpCodes.Ble_S; break;
case QilNodeType.Ne: opcode = OpCodes.Bne_Un_S; break;
default: Debug.Assert(false); opcode = OpCodes.Nop; break;
}
// Push "true" if comparison succeeds, "false" otherwise
lblTrue = this.helper.DefineLabel();
this.helper.Emit(opcode, lblTrue);
this.helper.ConvBranchToBool(lblTrue, true);
break;
}
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(bool), false);
break;
}
}
/// <summary>
/// Generate code to compare the top stack value to 0 by using the Brfalse or Brtrue instructions,
/// which avoid pushing zero onto the stack. Both of these instructions test for null/zero/false.
/// </summary>
private void ZeroCompare(QilNodeType relOp, bool isBoolVal) {
Label lblTrue;
Debug.Assert(relOp == QilNodeType.Eq || relOp == QilNodeType.Ne);
// Test to determine if top stack value is zero (if relOp is Eq) or is not zero (if relOp is Ne)
switch (this.iterCurr.CurrentBranchingContext) {
case BranchingContext.OnTrue:
// If relOp is Eq, jump to true label if top value is zero (Brfalse)
// If relOp is Ne, jump to true label if top value is non-zero (Brtrue)
this.helper.Emit((relOp == QilNodeType.Eq) ? OpCodes.Brfalse : OpCodes.Brtrue, this.iterCurr.LabelBranch);
this.iterCurr.Storage = StorageDescriptor.None();
break;
case BranchingContext.OnFalse:
// If relOp is Eq, jump to false label if top value is non-zero (Brtrue)
// If relOp is Ne, jump to false label if top value is zero (Brfalse)
this.helper.Emit((relOp == QilNodeType.Eq) ? OpCodes.Brtrue : OpCodes.Brfalse, this.iterCurr.LabelBranch);
this.iterCurr.Storage = StorageDescriptor.None();
break;
default:
Debug.Assert(this.iterCurr.CurrentBranchingContext == BranchingContext.None);
// Since (boolval != 0) = boolval, value on top of the stack is already correct
if (!isBoolVal || relOp == QilNodeType.Eq) {
// If relOp is Eq, push "true" if top value is zero, "false" otherwise
// If relOp is Ne, push "true" if top value is non-zero, "false" otherwise
lblTrue = this.helper.DefineLabel();
this.helper.Emit((relOp == QilNodeType.Eq) ? OpCodes.Brfalse : OpCodes.Brtrue, lblTrue);
this.helper.ConvBranchToBool(lblTrue, true);
}
this.iterCurr.Storage = StorageDescriptor.Stack(typeof(bool), false);
break;
}
}
/// <summary>
/// Construction within a loop is starting. If transition from non-Any to Any state occurs, then ensure
/// that runtime state will be set.
/// </summary>
private void StartWriterLoop(QilNode nd, out bool hasOnEnd, out Label lblOnEnd) {
XmlILConstructInfo info = XmlILConstructInfo.Read(nd);
// By default, do not create a new iteration label
hasOnEnd = false;
lblOnEnd = new Label();
// If loop is not involved in Xml construction, or if loop returns exactly one value, then do nothing
if (!info.PushToWriterLast || nd.XmlType.IsSingleton)
return;
if (!this.iterCurr.HasLabelNext) {
// Iterate until all items are constructed
hasOnEnd = true;
lblOnEnd = this.helper.DefineLabel();
this.iterCurr.SetIterator(lblOnEnd, StorageDescriptor.None());
}
}
/// <summary>
/// Construction within a loop is ending. If transition from non-Any to Any state occurs, then ensure that
/// runtime state will be set.
/// </summary>
private void EndWriterLoop(QilNode nd, bool hasOnEnd, Label lblOnEnd) {
XmlILConstructInfo info = XmlILConstructInfo.Read(nd);
// If loop is not involved in Xml construction, then do nothing
if (!info.PushToWriterLast)
return;
// Since results of construction were pushed to writer, there are no values to return
this.iterCurr.Storage = StorageDescriptor.None();
// If loop returns exactly one value, then do nothing further
if (nd.XmlType.IsSingleton)
return;
if (hasOnEnd) {
// Loop over all items in the list, sending each to the output writer
this.iterCurr.LoopToEnd(lblOnEnd);
}
}
/// <summary>
/// Returns true if the specified node's owner element might have local namespaces added to it
/// after attributes have already been added.
/// </summary>
private bool MightHaveNamespacesAfterAttributes(XmlILConstructInfo info) {
// Get parent element
if (info != null)
info = info.ParentElementInfo;
// If a parent element has not been statically identified, then assume that the runtime
// element will have namespaces added after attributes.
if (info == null)
return true;
return info.MightHaveNamespacesAfterAttributes;
}
/// <summary>
/// Returns true if the specified element should cache attributes.
/// </summary>
private bool ElementCachesAttributes(XmlILConstructInfo info) {
// Attributes will be cached if namespaces might be constructed after the attributes
return info.MightHaveDuplicateAttributes || info.MightHaveNamespacesAfterAttributes;
}
/// <summary>
/// This method is called before calling any WriteEnd??? method. It generates code to perform runtime
/// construction checks separately. This should only be called if the XmlQueryOutput::StartElementChk
/// method will *not* be called.
/// </summary>
private void BeforeStartChecks(QilNode ndCtor) {
switch (XmlILConstructInfo.Read(ndCtor).InitialStates) {
case PossibleXmlStates.WithinSequence:
// If runtime state is guaranteed to be WithinSequence, then call XmlQueryOutput.StartTree
this.helper.CallStartTree(QilConstructorToNodeType(ndCtor.NodeType));
break;
case PossibleXmlStates.EnumAttrs:
switch (ndCtor.NodeType) {
case QilNodeType.ElementCtor:
case QilNodeType.TextCtor:
case QilNodeType.RawTextCtor:
case QilNodeType.PICtor:
case QilNodeType.CommentCtor:
// If runtime state is guaranteed to be EnumAttrs, and content is being constructed, call
// XmlQueryOutput.StartElementContent
this.helper.CallStartElementContent();
break;
}
break;
}
}
/// <summary>
/// This method is called after calling any WriteEnd??? method. It generates code to perform runtime
/// construction checks separately. This should only be called if the XmlQueryOutput::EndElementChk
/// method will *not* be called.
/// </summary>
private void AfterEndChecks(QilNode ndCtor) {
if (XmlILConstructInfo.Read(ndCtor).FinalStates == PossibleXmlStates.WithinSequence) {
// If final runtime state is guaranteed to be WithinSequence, then call XmlQueryOutput.StartTree
this.helper.CallEndTree();
}
}
/// <summary>
/// Return true if a runtime check needs to be made in order to transition into the WithinContent state.
/// </summary>
private bool CheckWithinContent(XmlILConstructInfo info) {
switch (info.InitialStates) {
case PossibleXmlStates.WithinSequence:
case PossibleXmlStates.EnumAttrs:
case PossibleXmlStates.WithinContent:
// Transition to WithinContent can be ensured at compile-time
return false;
}
return true;
}
/// <summary>
/// Return true if a runtime check needs to be made in order to transition into the EnumAttrs state.
/// </summary>
private bool CheckEnumAttrs(XmlILConstructInfo info) {
switch (info.InitialStates) {
case PossibleXmlStates.WithinSequence:
case PossibleXmlStates.EnumAttrs:
// Transition to EnumAttrs can be ensured at compile-time
return false;
}
return true;
}
/// <summary>
/// Map the XmlNodeKindFlags enumeration into the XPathNodeType enumeration.
/// </summary>
private XPathNodeType QilXmlToXPathNodeType(XmlNodeKindFlags xmlTypes) {
switch (xmlTypes) {
case XmlNodeKindFlags.Element: return XPathNodeType.Element;
case XmlNodeKindFlags.Attribute: return XPathNodeType.Attribute;
case XmlNodeKindFlags.Text: return XPathNodeType.Text;
case XmlNodeKindFlags.Comment: return XPathNodeType.Comment;
}
Debug.Assert(xmlTypes == XmlNodeKindFlags.PI);
return XPathNodeType.ProcessingInstruction;
}
/// <summary>
/// Map a QilExpression constructor type into the XPathNodeType enumeration.
/// </summary>
private XPathNodeType QilConstructorToNodeType(QilNodeType typ) {
switch (typ) {
case QilNodeType.DocumentCtor: return XPathNodeType.Root;
case QilNodeType.ElementCtor: return XPathNodeType.Element;
case QilNodeType.TextCtor: return XPathNodeType.Text;
case QilNodeType.RawTextCtor: return XPathNodeType.Text;
case QilNodeType.PICtor: return XPathNodeType.ProcessingInstruction;
case QilNodeType.CommentCtor: return XPathNodeType.Comment;
case QilNodeType.AttributeCtor: return XPathNodeType.Attribute;
case QilNodeType.NamespaceDecl: return XPathNodeType.Namespace;
}
Debug.Assert(false, "Cannot map QilNodeType " + typ + " to an XPathNodeType");
return XPathNodeType.All;
}
/// <summary>
/// Load an XmlNavigatorFilter that matches only the specified name and types onto the stack.
/// </summary>
private void LoadSelectFilter(XmlNodeKindFlags xmlTypes, QilName ndName) {
if (ndName != null) {
// Push NameFilter
Debug.Assert(xmlTypes == XmlNodeKindFlags.Element);
this.helper.CallGetNameFilter(this.helper.StaticData.DeclareNameFilter(ndName.LocalName, ndName.NamespaceUri));
}
else {
// Either type cannot be a union, or else it must be >= union of all Content types
bool isXmlTypeUnion = IsNodeTypeUnion(xmlTypes);
Debug.Assert(!isXmlTypeUnion || (xmlTypes & XmlNodeKindFlags.Content) == XmlNodeKindFlags.Content);
if (isXmlTypeUnion) {
if ((xmlTypes & XmlNodeKindFlags.Attribute) != 0) {
// Union includes attributes, so allow all node kinds
this.helper.CallGetTypeFilter(XPathNodeType.All);
}
else {
// Filter attributes
this.helper.CallGetTypeFilter(XPathNodeType.Attribute);
}
}
else {
// Filter nodes of all but one type
this.helper.CallGetTypeFilter(QilXmlToXPathNodeType(xmlTypes));
}
}
}
/// <summary>
/// Return true if more than one node type is set.
/// </summary>
private static bool IsNodeTypeUnion(XmlNodeKindFlags xmlTypes) {
return ((int) xmlTypes & ((int) xmlTypes - 1)) != 0;
}
/// <summary>
/// Start construction of a new nested iterator. If this.iterCurr == null, then the new iterator
/// is a top-level, or root iterator. Otherwise, the new iterator will be nested within the
/// current iterator.
/// </summary>
private void StartNestedIterator(QilNode nd) {
IteratorDescriptor iterParent = this.iterCurr;
// Create a new, nested iterator
if (iterParent == null) {
// Create a "root" iterator info that has no parernt
this.iterCurr = new IteratorDescriptor(this.helper);
}
else {
// Create a nested iterator
this.iterCurr = new IteratorDescriptor(iterParent);
}
this.iterNested = null;
}
/// <summary>
/// Calls StartNestedIterator(nd) and also sets up the nested iterator to branch to "lblOnEnd" when iteration
/// is complete.
/// </summary>
private void StartNestedIterator(QilNode nd, Label lblOnEnd) {
StartNestedIterator(nd);
this.iterCurr.SetIterator(lblOnEnd, StorageDescriptor.None());
}
/// <summary>
/// End construction of the current iterator.
/// </summary>
private void EndNestedIterator(QilNode nd) {
Debug.Assert(this.iterCurr.Storage.Location == ItemLocation.None ||
this.iterCurr.Storage.ItemStorageType == GetItemStorageType(nd) ||
this.iterCurr.Storage.ItemStorageType == typeof(XPathItem) ||
nd.XmlType.TypeCode == XmlTypeCode.None,
"QilNodeType " + nd.NodeType + " cannot be stored using type " + this.iterCurr.Storage.ItemStorageType + ".");
// If the nested iterator was constructed in branching mode,
if (this.iterCurr.IsBranching) {
// Then if branching hasn't already taken place, do so now
if (this.iterCurr.Storage.Location != ItemLocation.None) {
this.iterCurr.EnsureItemStorageType(nd.XmlType, typeof(bool));
this.iterCurr.EnsureStackNoCache();
if (this.iterCurr.CurrentBranchingContext == BranchingContext.OnTrue)
this.helper.Emit(OpCodes.Brtrue, this.iterCurr.LabelBranch);
else
this.helper.Emit(OpCodes.Brfalse, this.iterCurr.LabelBranch);
this.iterCurr.Storage = StorageDescriptor.None();
}
}
// Save current iterator as nested iterator
this.iterNested = this.iterCurr;
// Update current iterator to be parent iterator
this.iterCurr = this.iterCurr.ParentIterator;
}
/// <summary>
/// Recursively generate code to iterate over the results of the "nd" expression. If "nd" is pushed
/// to the writer, then there are no results. If "nd" is a singleton expression and isCached is false,
/// then generate code to construct the singleton. Otherwise, cache the sequence in an XmlQuerySequence
/// object. Ensure that all items are converted to the specified "itemStorageType".
/// </summary>
private void NestedVisit(QilNode nd, Type itemStorageType, bool isCached) {
if (XmlILConstructInfo.Read(nd).PushToWriterLast) {
// Push results to output, so nothing is left to store
StartNestedIterator(nd);
Visit(nd);
EndNestedIterator(nd);
this.iterCurr.Storage = StorageDescriptor.None();
}
else if (!isCached && nd.XmlType.IsSingleton) {
// Storage of result will be a non-cached singleton
StartNestedIterator(nd);
Visit(nd);
this.iterCurr.EnsureNoCache();
this.iterCurr.EnsureItemStorageType(nd.XmlType, itemStorageType);
EndNestedIterator(nd);
this.iterCurr.Storage = this.iterNested.Storage;
}
else {
NestedVisitEnsureCache(nd, itemStorageType);
}
}
/// <summary>
/// Calls NestedVisit(QilNode, Type, bool), storing result in the default storage type for "nd".
/// </summary>
private void NestedVisit(QilNode nd) {
NestedVisit(nd, GetItemStorageType(nd), !nd.XmlType.IsSingleton);
}
/// <summary>
/// Recursively generate code to iterate over the results of the "nd" expression. When the expression
/// has been fully iterated, it will jump to "lblOnEnd".
/// </summary>
private void NestedVisit(QilNode nd, Label lblOnEnd) {
Debug.Assert(!XmlILConstructInfo.Read(nd).PushToWriterLast);
StartNestedIterator(nd, lblOnEnd);
Visit(nd);
this.iterCurr.EnsureNoCache();
this.iterCurr.EnsureItemStorageType(nd.XmlType, GetItemStorageType(nd));
EndNestedIterator(nd);
this.iterCurr.Storage = this.iterNested.Storage;
}
/// <summary>
/// Call NestedVisit(QilNode) and ensure that result is pushed onto the IL stack.
/// </summary>
private void NestedVisitEnsureStack(QilNode nd) {
Debug.Assert(!XmlILConstructInfo.Read(nd).PushToWriterLast);
NestedVisit(nd);
this.iterCurr.EnsureStack();
}
/// <summary>
/// Generate code for both QilExpression nodes and ensure that each result is pushed onto the IL stack.
/// </summary>
private void NestedVisitEnsureStack(QilNode ndLeft, QilNode ndRight) {
NestedVisitEnsureStack(ndLeft);
NestedVisitEnsureStack(ndRight);
}
/// <summary>
/// Call NestedVisit(QilNode, Type, bool) and ensure that result is pushed onto the IL stack.
/// </summary>
private void NestedVisitEnsureStack(QilNode nd, Type itemStorageType, bool isCached) {
Debug.Assert(!XmlILConstructInfo.Read(nd).PushToWriterLast);
NestedVisit(nd, itemStorageType, isCached);
this.iterCurr.EnsureStack();
}
/// <summary>
/// Call NestedVisit(QilNode) and ensure that result is stored in local variable "loc".
/// </summary>
private void NestedVisitEnsureLocal(QilNode nd, LocalBuilder loc) {
Debug.Assert(!XmlILConstructInfo.Read(nd).PushToWriterLast);
NestedVisit(nd);
this.iterCurr.EnsureLocal(loc);
}
/// <summary>
/// Start a nested iterator in a branching context and recursively generate code for the specified QilExpression node.
/// </summary>
private void NestedVisitWithBranch(QilNode nd, BranchingContext brctxt, Label lblBranch) {
Debug.Assert(nd.XmlType.IsSingleton && !XmlILConstructInfo.Read(nd).PushToWriterLast);
StartNestedIterator(nd);
this.iterCurr.SetBranching(brctxt, lblBranch);
Visit(nd);
EndNestedIterator(nd);
this.iterCurr.Storage = StorageDescriptor.None();
}
/// <summary>
/// Generate code for the QilExpression node and ensure that results are fully cached as an XmlQuerySequence. All results
/// should be converted to "itemStorageType" before being added to the cache.
/// </summary>
private void NestedVisitEnsureCache(QilNode nd, Type itemStorageType) {
Debug.Assert(!XmlILConstructInfo.Read(nd).PushToWriterLast);
bool cachesResult = CachesResult(nd);
LocalBuilder locCache;
Label lblOnEnd = this.helper.DefineLabel();
Type cacheType;
XmlILStorageMethods methods;
// If bound expression will already be cached correctly, then don't create an XmlQuerySequence
if (cachesResult) {
StartNestedIterator(nd);
Visit(nd);
EndNestedIterator(nd);
this.iterCurr.Storage = this.iterNested.Storage;
Debug.Assert(this.iterCurr.Storage.IsCached, "Expression result should be cached. CachesResult() might have a bug in it.");
// If type of items in the cache matches "itemStorageType", then done
if (this.iterCurr.Storage.ItemStorageType == itemStorageType)
return;
// If the cache has navigators in it, or if converting to a cache of navigators, then EnsureItemStorageType
// can directly convert without needing to create a new cache.
if (this.iterCurr.Storage.ItemStorageType == typeof(XPathNavigator) || itemStorageType == typeof(XPathNavigator)) {
this.iterCurr.EnsureItemStorageType(nd.XmlType, itemStorageType);
return;
}
this.iterCurr.EnsureNoStack("$$$cacheResult");
}
// Always store navigators in XmlQueryNodeSequence (which implements IList<XPathItem>)
cacheType = (GetItemStorageType(nd) == typeof(XPathNavigator)) ? typeof(XPathNavigator) : itemStorageType;
// XmlQuerySequence<T> cache;
methods = XmlILMethods.StorageMethods[cacheType];
locCache = this.helper.DeclareLocal("$$$cache", methods.SeqType);
this.helper.Emit(OpCodes.Ldloc, locCache);
// Special case non-navigator singletons to use overload of CreateOrReuse
if (nd.XmlType.IsSingleton) {
// cache = XmlQuerySequence.CreateOrReuse(cache, item);
NestedVisitEnsureStack(nd, cacheType, false);
this.helper.CallToken(methods.SeqReuseSgl);
this.helper.Emit(OpCodes.Stloc, locCache);
}
else {
// XmlQuerySequence<T> cache;
// cache = XmlQuerySequence.CreateOrReuse(cache);
this.helper.CallToken(methods.SeqReuse);
this.helper.Emit(OpCodes.Stloc, locCache);
this.helper.Emit(OpCodes.Ldloc, locCache);
StartNestedIterator(nd, lblOnEnd);
if (cachesResult)
this.iterCurr.Storage = this.iterCurr.ParentIterator.Storage;
else
Visit(nd);
// cache.Add(item);
this.iterCurr.EnsureItemStorageType(nd.XmlType, cacheType);
this.iterCurr.EnsureStackNoCache();
this.helper.Call(methods.SeqAdd);
this.helper.Emit(OpCodes.Ldloc, locCache);
// }
this.iterCurr.LoopToEnd(lblOnEnd);
EndNestedIterator(nd);
// Remove cache reference from stack
this.helper.Emit(OpCodes.Pop);
}
this.iterCurr.Storage = StorageDescriptor.Local(locCache, itemStorageType, true);
}
/// <summary>
/// Returns true if the specified QilExpression node type is *guaranteed* to cache its results in an XmlQuerySequence,
/// where items in the cache are stored using the default storage type.
/// </summary>
private bool CachesResult(QilNode nd) {
OptimizerPatterns patt;
switch (nd.NodeType) {
case QilNodeType.Let:
case QilNodeType.Parameter:
case QilNodeType.Invoke:
case QilNodeType.XsltInvokeLateBound:
case QilNodeType.XsltInvokeEarlyBound:
return !nd.XmlType.IsSingleton;
case QilNodeType.Filter:
// EqualityIndex pattern caches results
patt = OptimizerPatterns.Read(nd);
return patt.MatchesPattern(OptimizerPatternName.EqualityIndex);
case QilNodeType.DocOrderDistinct:
if (nd.XmlType.IsSingleton)
return false;
// JoinAndDod and DodReverse patterns don't cache results
patt = OptimizerPatterns.Read(nd);
return !patt.MatchesPattern(OptimizerPatternName.JoinAndDod) && !patt.MatchesPattern(OptimizerPatternName.DodReverse);
case QilNodeType.TypeAssert:
QilTargetType ndTypeAssert = (QilTargetType) nd;
// Check if TypeAssert would be no-op
return CachesResult(ndTypeAssert.Source) && GetItemStorageType(ndTypeAssert.Source) == GetItemStorageType(ndTypeAssert);
}
return false;
}
/// <summary>
/// Shortcut call to XmlILTypeHelper.GetStorageType.
/// </summary>
private Type GetStorageType(QilNode nd) {
return XmlILTypeHelper.GetStorageType(nd.XmlType);
}
/// <summary>
/// Shortcut call to XmlILTypeHelper.GetStorageType.
/// </summary>
private Type GetStorageType(XmlQueryType typ) {
return XmlILTypeHelper.GetStorageType(typ);
}
/// <summary>
/// Shortcut call to XmlILTypeHelper.GetStorageType, using an expression's prime type.
/// </summary>
private Type GetItemStorageType(QilNode nd) {
return XmlILTypeHelper.GetStorageType(nd.XmlType.Prime);
}
/// <summary>
/// Shortcut call to XmlILTypeHelper.GetStorageType, using the prime type.
/// </summary>
private Type GetItemStorageType(XmlQueryType typ) {
return XmlILTypeHelper.GetStorageType(typ.Prime);
}
}
}
|