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
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
|
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* vim9compile.c: :def and dealing with instructions
*/
#define USING_FLOAT_STUFF
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
#ifdef VMS
# include <float.h>
#endif
#define DEFINE_VIM9_GLOBALS
#include "vim9.h"
// values for ctx_skip
typedef enum {
SKIP_NOT, // condition is a constant, produce code
SKIP_YES, // condition is a constant, do NOT produce code
SKIP_UNKNOWN // condition is not a constant, produce code
} skip_T;
/*
* Chain of jump instructions where the end label needs to be set.
*/
typedef struct endlabel_S endlabel_T;
struct endlabel_S {
endlabel_T *el_next; // chain end_label locations
int el_end_label; // instruction idx where to set end
};
/*
* info specific for the scope of :if / elseif / else
*/
typedef struct {
int is_seen_else;
int is_seen_skip_not; // a block was unconditionally executed
int is_had_return; // every block ends in :return
int is_if_label; // instruction idx at IF or ELSEIF
endlabel_T *is_end_label; // instructions to set end label
} ifscope_T;
/*
* info specific for the scope of :while
*/
typedef struct {
int ws_top_label; // instruction idx at WHILE
endlabel_T *ws_end_label; // instructions to set end
} whilescope_T;
/*
* info specific for the scope of :for
*/
typedef struct {
int fs_top_label; // instruction idx at FOR
endlabel_T *fs_end_label; // break instructions
} forscope_T;
/*
* info specific for the scope of :try
*/
typedef struct {
int ts_try_label; // instruction idx at TRY
endlabel_T *ts_end_label; // jump to :finally or :endtry
int ts_catch_label; // instruction idx of last CATCH
int ts_caught_all; // "catch" without argument encountered
} tryscope_T;
typedef enum {
NO_SCOPE,
IF_SCOPE,
WHILE_SCOPE,
FOR_SCOPE,
TRY_SCOPE,
BLOCK_SCOPE
} scopetype_T;
/*
* Info for one scope, pointed to by "ctx_scope".
*/
typedef struct scope_S scope_T;
struct scope_S {
scope_T *se_outer; // scope containing this one
scopetype_T se_type;
int se_local_count; // ctx_locals.ga_len before scope
skip_T se_skip_save; // ctx_skip before the block
union {
ifscope_T se_if;
whilescope_T se_while;
forscope_T se_for;
tryscope_T se_try;
} se_u;
};
/*
* Entry for "ctx_locals". Used for arguments and local variables.
*/
typedef struct {
char_u *lv_name;
type_T *lv_type;
int lv_idx; // index of the variable on the stack
int lv_from_outer; // nesting level, using ctx_outer scope
int lv_const; // when TRUE cannot be assigned to
int lv_arg; // when TRUE this is an argument
} lvar_T;
// Destination for an assignment or ":unlet" with an index.
typedef enum {
dest_local,
dest_option,
dest_func_option,
dest_env,
dest_global,
dest_buffer,
dest_window,
dest_tab,
dest_vimvar,
dest_script,
dest_reg,
dest_expr,
} assign_dest_T;
// Used by compile_lhs() to store information about the LHS of an assignment
// and one argument of ":unlet" with an index.
typedef struct {
assign_dest_T lhs_dest; // type of destination
char_u *lhs_name; // allocated name excluding the last
// "[expr]" or ".name".
size_t lhs_varlen; // length of the variable without
// "[expr]" or ".name"
char_u *lhs_whole; // allocated name including the last
// "[expr]" or ".name" for :redir
size_t lhs_varlen_total; // length of the variable including
// any "[expr]" or ".name"
char_u *lhs_dest_end; // end of the destination, including
// "[expr]" or ".name".
char_u *lhs_end; // end including any type
int lhs_has_index; // has "[expr]" or ".name"
int lhs_new_local; // create new local variable
int lhs_opt_flags; // for when destination is an option
int lhs_vimvaridx; // for when destination is a v:var
lvar_T lhs_local_lvar; // used for existing local destination
lvar_T lhs_arg_lvar; // used for argument destination
lvar_T *lhs_lvar; // points to destination lvar
int lhs_scriptvar_sid;
int lhs_scriptvar_idx;
int lhs_has_type; // type was specified
type_T *lhs_type;
type_T *lhs_member_type;
int lhs_append; // used by ISN_REDIREND
} lhs_T;
/*
* Context for compiling lines of Vim script.
* Stores info about the local variables and condition stack.
*/
struct cctx_S {
ufunc_T *ctx_ufunc; // current function
int ctx_lnum; // line number in current function
char_u *ctx_line_start; // start of current line or NULL
garray_T ctx_instr; // generated instructions
int ctx_prev_lnum; // line number below previous command, for
// debugging
compiletype_T ctx_compile_type;
garray_T ctx_locals; // currently visible local variables
int ctx_has_closure; // set to one if a closures was created in
// the function
garray_T ctx_imports; // imported items
skip_T ctx_skip;
scope_T *ctx_scope; // current scope, NULL at toplevel
int ctx_had_return; // last seen statement was "return"
cctx_T *ctx_outer; // outer scope for lambda or nested
// function
int ctx_outer_used; // var in ctx_outer was used
garray_T ctx_type_stack; // type of each item on the stack
garray_T *ctx_type_list; // list of pointers to allocated types
int ctx_has_cmdmod; // ISN_CMDMOD was generated
lhs_T ctx_redir_lhs; // LHS for ":redir => var", valid when
// lhs_name is not NULL
};
static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
/*
* Lookup variable "name" in the local scope and return it in "lvar".
* "lvar->lv_from_outer" is incremented accordingly.
* If "lvar" is NULL only check if the variable can be found.
* Return FAIL if not found.
*/
static int
lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
{
int idx;
lvar_T *lvp;
if (len == 0)
return FAIL;
// Find local in current function scope.
for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
{
lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
if (STRNCMP(name, lvp->lv_name, len) == 0
&& STRLEN(lvp->lv_name) == len)
{
if (lvar != NULL)
{
*lvar = *lvp;
lvar->lv_from_outer = 0;
}
return OK;
}
}
// Find local in outer function scope.
if (cctx->ctx_outer != NULL)
{
if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
{
if (lvar != NULL)
{
cctx->ctx_outer_used = TRUE;
++lvar->lv_from_outer;
}
return OK;
}
}
return FAIL;
}
/*
* Lookup an argument in the current function and an enclosing function.
* Returns the argument index in "idxp"
* Returns the argument type in "type"
* Sets "gen_load_outer" to TRUE if found in outer scope.
* Returns OK when found, FAIL otherwise.
*/
static int
arg_exists(
char_u *name,
size_t len,
int *idxp,
type_T **type,
int *gen_load_outer,
cctx_T *cctx)
{
int idx;
char_u *va_name;
if (len == 0)
return FAIL;
for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
{
char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
{
if (idxp != NULL)
{
// Arguments are located above the frame pointer. One further
// if there is a vararg argument
*idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
+ STACK_FRAME_SIZE)
+ (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
if (cctx->ctx_ufunc->uf_arg_types != NULL)
*type = cctx->ctx_ufunc->uf_arg_types[idx];
else
*type = &t_any;
}
return OK;
}
}
va_name = cctx->ctx_ufunc->uf_va_name;
if (va_name != NULL
&& STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
{
if (idxp != NULL)
{
// varargs is always the last argument
*idxp = -STACK_FRAME_SIZE - 1;
*type = cctx->ctx_ufunc->uf_va_type;
}
return OK;
}
if (cctx->ctx_outer != NULL)
{
// Lookup the name for an argument of the outer function.
if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
== OK)
{
if (gen_load_outer != NULL)
++*gen_load_outer;
return OK;
}
}
return FAIL;
}
/*
* Lookup a script-local variable in the current script, possibly defined in a
* block that contains the function "cctx->ctx_ufunc".
* "cctx" is NULL at the script level.
* If "len" is <= 0 "name" must be NUL terminated.
* Return NULL when not found.
*/
static sallvar_T *
find_script_var(char_u *name, size_t len, cctx_T *cctx)
{
scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
hashitem_T *hi;
int cc;
sallvar_T *sav;
sallvar_T *found_sav;
ufunc_T *ufunc;
// Find the list of all script variables with the right name.
if (len > 0)
{
cc = name[len];
name[len] = NUL;
}
hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
if (len > 0)
name[len] = cc;
if (HASHITEM_EMPTY(hi))
return NULL;
sav = HI2SAV(hi);
if (sav->sav_block_id == 0)
// variable defined in the top script scope is always visible
return sav;
if (cctx == NULL)
{
// Not in a function scope, find variable with block id equal to or
// smaller than the current block id.
while (sav != NULL)
{
if (sav->sav_block_id <= si->sn_current_block_id)
break;
sav = sav->sav_next;
}
return sav;
}
// Go over the variables with this name and find one that was visible
// from the function.
ufunc = cctx->ctx_ufunc;
found_sav = sav;
while (sav != NULL)
{
int idx;
// Go over the blocks that this function was defined in. If the
// variable block ID matches it was visible to the function.
for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
return sav;
sav = sav->sav_next;
}
// Not found, assume variable at script level was visible.
return found_sav;
}
/*
* Return TRUE if the script context is Vim9 script.
*/
static int
script_is_vim9()
{
return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
}
/*
* Lookup a variable (without s: prefix) in the current script.
* "cctx" is NULL at the script level.
* Returns OK or FAIL.
*/
static int
script_var_exists(char_u *name, size_t len, cctx_T *cctx)
{
if (current_sctx.sc_sid <= 0)
return FAIL;
if (script_is_vim9())
{
// Check script variables that were visible where the function was
// defined.
if (find_script_var(name, len, cctx) != NULL)
return OK;
}
else
{
hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
dictitem_T *di;
int cc;
// Check script variables that are currently visible
cc = name[len];
name[len] = NUL;
di = find_var_in_ht(ht, 0, name, TRUE);
name[len] = cc;
if (di != NULL)
return OK;
}
return FAIL;
}
/*
* Return TRUE if "name" is a local variable, argument, script variable or
* imported.
*/
static int
variable_exists(char_u *name, size_t len, cctx_T *cctx)
{
return (cctx != NULL
&& (lookup_local(name, len, NULL, cctx) == OK
|| arg_exists(name, len, NULL, NULL, NULL, cctx) == OK))
|| script_var_exists(name, len, cctx) == OK
|| find_imported(name, len, cctx) != NULL;
}
/*
* Return TRUE if "name" is a local variable, argument, script variable,
* imported or function.
*/
static int
item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
{
int is_global;
char_u *p;
if (variable_exists(name, len, cctx))
return TRUE;
// This is similar to what is in lookup_scriptitem():
// Find a function, so that a following "->" works.
// Require "(" or "->" to follow, "Cmd" is a user command while "Cmd()" is
// a function call.
p = skipwhite(name + len);
if (name[len] == '(' || (p[0] == '-' && p[1] == '>'))
{
// Do not check for an internal function, since it might also be a
// valid command, such as ":split" versus "split()".
// Skip "g:" before a function name.
is_global = (name[0] == 'g' && name[1] == ':');
return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
}
return FALSE;
}
/*
* Check if "p[len]" is already defined, either in script "import_sid" or in
* compilation context "cctx". "cctx" is NULL at the script level.
* Does not check the global namespace.
* If "is_arg" is TRUE the error message is for an argument name.
* Return FAIL and give an error if it defined.
*/
int
check_defined(char_u *p, size_t len, cctx_T *cctx, int is_arg)
{
int c = p[len];
ufunc_T *ufunc = NULL;
// underscore argument is OK
if (len == 1 && *p == '_')
return OK;
if (script_var_exists(p, len, cctx) == OK)
{
if (is_arg)
semsg(_(e_argument_already_declared_in_script_str), p);
else
semsg(_(e_variable_already_declared_in_script_str), p);
return FAIL;
}
p[len] = NUL;
if ((cctx != NULL
&& (lookup_local(p, len, NULL, cctx) == OK
|| arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
|| find_imported(p, len, cctx) != NULL
|| (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL)
{
// A local or script-local function can shadow a global function.
if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
&& (!func_is_global(ufunc)
|| (p[0] == 'g' && p[1] == ':'))))
{
if (is_arg)
semsg(_(e_argument_name_shadows_existing_variable_str), p);
else
semsg(_(e_name_already_defined_str), p);
p[len] = c;
return FAIL;
}
}
p[len] = c;
return OK;
}
/////////////////////////////////////////////////////////////////////
// Following generate_ functions expect the caller to call ga_grow().
#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
/*
* Generate an instruction without arguments.
* Returns a pointer to the new instruction, NULL if failed.
*/
static isn_T *
generate_instr(cctx_T *cctx, isntype_T isn_type)
{
garray_T *instr = &cctx->ctx_instr;
isn_T *isn;
RETURN_NULL_IF_SKIP(cctx);
if (GA_GROW_FAILS(instr, 1))
return NULL;
isn = ((isn_T *)instr->ga_data) + instr->ga_len;
isn->isn_type = isn_type;
isn->isn_lnum = cctx->ctx_lnum + 1;
++instr->ga_len;
return isn;
}
/*
* Generate an instruction without arguments.
* "drop" will be removed from the stack.
* Returns a pointer to the new instruction, NULL if failed.
*/
static isn_T *
generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
{
garray_T *stack = &cctx->ctx_type_stack;
RETURN_NULL_IF_SKIP(cctx);
stack->ga_len -= drop;
return generate_instr(cctx, isn_type);
}
/*
* Generate instruction "isn_type" and put "type" on the type stack.
*/
static isn_T *
generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
if ((isn = generate_instr(cctx, isn_type)) == NULL)
return NULL;
if (GA_GROW_FAILS(stack, 1))
return NULL;
((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type;
++stack->ga_len;
return isn;
}
/*
* Generate an ISN_DEBUG instruction.
*/
static isn_T *
generate_instr_debug(cctx_T *cctx)
{
isn_T *isn;
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ cctx->ctx_ufunc->uf_dfunc_idx;
if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
return NULL;
isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
return isn;
}
/*
* If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
* But only for simple types.
* When "tolerant" is TRUE convert most types to string, e.g. a List.
*/
static int
may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
{
isn_T *isn;
isntype_T isntype = ISN_2STRING;
garray_T *stack = &cctx->ctx_type_stack;
type_T **type;
RETURN_OK_IF_SKIP(cctx);
type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
switch ((*type)->tt_type)
{
// nothing to be done
case VAR_STRING: return OK;
// conversion possible
case VAR_SPECIAL:
case VAR_BOOL:
case VAR_NUMBER:
case VAR_FLOAT:
break;
// conversion possible (with runtime check)
case VAR_ANY:
case VAR_UNKNOWN:
isntype = ISN_2STRING_ANY;
break;
// conversion possible when tolerant
case VAR_LIST:
if (tolerant)
{
isntype = ISN_2STRING_ANY;
break;
}
// FALLTHROUGH
// conversion not possible
case VAR_VOID:
case VAR_BLOB:
case VAR_FUNC:
case VAR_PARTIAL:
case VAR_DICT:
case VAR_JOB:
case VAR_CHANNEL:
case VAR_INSTR:
to_string_error((*type)->tt_type);
return FAIL;
}
*type = &t_string;
if ((isn = generate_instr(cctx, isntype)) == NULL)
return FAIL;
isn->isn_arg.tostring.offset = offset;
isn->isn_arg.tostring.tolerant = tolerant;
return OK;
}
static int
check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
{
if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
&& (type2 == VAR_NUMBER || type2 == VAR_FLOAT
|| type2 == VAR_ANY)))
{
if (*op == '+')
emsg(_(e_wrong_argument_type_for_plus));
else
semsg(_(e_char_requires_number_or_float_arguments), *op);
return FAIL;
}
return OK;
}
/*
* Generate instruction for "+". For a list this creates a new list.
*/
static int
generate_add_instr(
cctx_T *cctx,
vartype_T vartype,
type_T *type1,
type_T *type2,
exprtype_T expr_type)
{
garray_T *stack = &cctx->ctx_type_stack;
isn_T *isn = generate_instr_drop(cctx,
vartype == VAR_NUMBER ? ISN_OPNR
: vartype == VAR_LIST ? ISN_ADDLIST
: vartype == VAR_BLOB ? ISN_ADDBLOB
#ifdef FEAT_FLOAT
: vartype == VAR_FLOAT ? ISN_OPFLOAT
#endif
: ISN_OPANY, 1);
if (vartype != VAR_LIST && vartype != VAR_BLOB
&& type1->tt_type != VAR_ANY
&& type2->tt_type != VAR_ANY
&& check_number_or_float(
type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
return FAIL;
if (isn != NULL)
{
if (isn->isn_type == ISN_ADDLIST)
isn->isn_arg.op.op_type = expr_type;
else
isn->isn_arg.op.op_type = EXPR_ADD;
}
// When concatenating two lists with different member types the member type
// becomes "any".
if (vartype == VAR_LIST
&& type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
&& type1->tt_member != type2->tt_member)
(((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any;
return isn == NULL ? FAIL : OK;
}
/*
* Get the type to use for an instruction for an operation on "type1" and
* "type2". If they are matching use a type-specific instruction. Otherwise
* fall back to runtime type checking.
*/
static vartype_T
operator_type(type_T *type1, type_T *type2)
{
if (type1->tt_type == type2->tt_type
&& (type1->tt_type == VAR_NUMBER
|| type1->tt_type == VAR_LIST
#ifdef FEAT_FLOAT
|| type1->tt_type == VAR_FLOAT
#endif
|| type1->tt_type == VAR_BLOB))
return type1->tt_type;
return VAR_ANY;
}
/*
* Generate an instruction with two arguments. The instruction depends on the
* type of the arguments.
*/
static int
generate_two_op(cctx_T *cctx, char_u *op)
{
garray_T *stack = &cctx->ctx_type_stack;
type_T *type1;
type_T *type2;
vartype_T vartype;
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
// Get the known type of the two items on the stack.
type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
vartype = operator_type(type1, type2);
switch (*op)
{
case '+':
if (generate_add_instr(cctx, vartype, type1, type2,
EXPR_COPY) == FAIL)
return FAIL;
break;
case '-':
case '*':
case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
op) == FAIL)
return FAIL;
if (vartype == VAR_NUMBER)
isn = generate_instr_drop(cctx, ISN_OPNR, 1);
#ifdef FEAT_FLOAT
else if (vartype == VAR_FLOAT)
isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
#endif
else
isn = generate_instr_drop(cctx, ISN_OPANY, 1);
if (isn != NULL)
isn->isn_arg.op.op_type = *op == '*'
? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
break;
case '%': if ((type1->tt_type != VAR_ANY
&& type1->tt_type != VAR_NUMBER)
|| (type2->tt_type != VAR_ANY
&& type2->tt_type != VAR_NUMBER))
{
emsg(_(e_percent_requires_number_arguments));
return FAIL;
}
isn = generate_instr_drop(cctx,
vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
if (isn != NULL)
isn->isn_arg.op.op_type = EXPR_REM;
break;
}
// correct type of result
if (vartype == VAR_ANY)
{
type_T *type = &t_any;
#ifdef FEAT_FLOAT
// float+number and number+float results in float
if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
&& (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
type = &t_float;
#endif
((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
}
return OK;
}
/*
* Get the instruction to use for comparing "type1" with "type2"
* Return ISN_DROP when failed.
*/
static isntype_T
get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
{
isntype_T isntype = ISN_DROP;
if (type1 == VAR_UNKNOWN)
type1 = VAR_ANY;
if (type2 == VAR_UNKNOWN)
type2 = VAR_ANY;
if (type1 == type2)
{
switch (type1)
{
case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
case VAR_NUMBER: isntype = ISN_COMPARENR; break;
case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
case VAR_STRING: isntype = ISN_COMPARESTRING; break;
case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
case VAR_LIST: isntype = ISN_COMPARELIST; break;
case VAR_DICT: isntype = ISN_COMPAREDICT; break;
case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
default: isntype = ISN_COMPAREANY; break;
}
}
else if (type1 == VAR_ANY || type2 == VAR_ANY
|| ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
&& (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
isntype = ISN_COMPAREANY;
if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
&& (isntype == ISN_COMPAREBOOL
|| isntype == ISN_COMPARESPECIAL
|| isntype == ISN_COMPARENR
|| isntype == ISN_COMPAREFLOAT))
{
semsg(_(e_cannot_use_str_with_str),
exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
return ISN_DROP;
}
if (isntype == ISN_DROP
|| ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
&& (type1 == VAR_BOOL || type1 == VAR_SPECIAL
|| type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
|| ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
&& exprtype != EXPR_IS && exprtype != EXPR_ISNOT
&& (type1 == VAR_BLOB || type2 == VAR_BLOB
|| type1 == VAR_LIST || type2 == VAR_LIST))))
{
semsg(_(e_cannot_compare_str_with_str),
vartype_name(type1), vartype_name(type2));
return ISN_DROP;
}
return isntype;
}
int
check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
{
if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
return FAIL;
return OK;
}
/*
* Generate an ISN_COMPARE* instruction with a boolean result.
*/
static int
generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
{
isntype_T isntype;
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
vartype_T type1;
vartype_T type2;
RETURN_OK_IF_SKIP(cctx);
// Get the known type of the two items on the stack. If they are matching
// use a type-specific instruction. Otherwise fall back to runtime type
// checking.
type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
isntype = get_compare_isn(exprtype, type1, type2);
if (isntype == ISN_DROP)
return FAIL;
if ((isn = generate_instr(cctx, isntype)) == NULL)
return FAIL;
isn->isn_arg.op.op_type = exprtype;
isn->isn_arg.op.op_ic = ic;
// takes two arguments, puts one bool back
if (stack->ga_len >= 2)
{
--stack->ga_len;
((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
}
return OK;
}
/*
* Generate an ISN_2BOOL instruction.
* "offset" is the offset in the type stack.
*/
static int
generate_2BOOL(cctx_T *cctx, int invert, int offset)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
return FAIL;
isn->isn_arg.tobool.invert = invert;
isn->isn_arg.tobool.offset = offset;
// type becomes bool
((type_T **)stack->ga_data)[stack->ga_len + offset] = &t_bool;
return OK;
}
/*
* Generate an ISN_COND2BOOL instruction.
*/
static int
generate_COND2BOOL(cctx_T *cctx)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
return FAIL;
// type becomes bool
((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
return OK;
}
static int
generate_TYPECHECK(
cctx_T *cctx,
type_T *expected,
int offset,
int argidx)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
return FAIL;
isn->isn_arg.type.ct_type = alloc_type(expected);
isn->isn_arg.type.ct_off = (int8_T)offset;
isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
// type becomes expected
((type_T **)stack->ga_data)[stack->ga_len + offset] = expected;
return OK;
}
static int
generate_SETTYPE(
cctx_T *cctx,
type_T *expected)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
return FAIL;
isn->isn_arg.type.ct_type = alloc_type(expected);
return OK;
}
/*
* Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
* used. Return FALSE if the types will never match.
*/
static int
use_typecheck(type_T *actual, type_T *expected)
{
if (actual->tt_type == VAR_ANY
|| actual->tt_type == VAR_UNKNOWN
|| (actual->tt_type == VAR_FUNC
&& (expected->tt_type == VAR_FUNC
|| expected->tt_type == VAR_PARTIAL)
&& (actual->tt_member == &t_any || actual->tt_argcount < 0)
&& ((actual->tt_member == &t_void)
== (expected->tt_member == &t_void))))
return TRUE;
if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
&& actual->tt_type == expected->tt_type)
// This takes care of a nested list or dict.
return use_typecheck(actual->tt_member, expected->tt_member);
return FALSE;
}
/*
* Check that
* - "actual" matches "expected" type or
* - "actual" is a type that can be "expected" type: add a runtime check; or
* - return FAIL.
* If "actual_is_const" is TRUE then the type won't change at runtime, do not
* generate a TYPECHECK.
*/
static int
need_type_where(
type_T *actual,
type_T *expected,
int offset,
where_T where,
cctx_T *cctx,
int silent,
int actual_is_const)
{
if (expected == &t_bool && actual != &t_bool
&& (actual->tt_flags & TTFLAG_BOOL_OK))
{
// Using "0", "1" or the result of an expression with "&&" or "||" as a
// boolean is OK but requires a conversion.
generate_2BOOL(cctx, FALSE, offset);
return OK;
}
if (check_type(expected, actual, FALSE, where) == OK)
return OK;
// If the actual type can be the expected type add a runtime check.
// If it's a constant a runtime check makes no sense.
if ((!actual_is_const || actual == &t_any)
&& use_typecheck(actual, expected))
{
generate_TYPECHECK(cctx, expected, offset, where.wt_index);
return OK;
}
if (!silent)
type_mismatch_where(expected, actual, where);
return FAIL;
}
int
need_type(
type_T *actual,
type_T *expected,
int offset,
int arg_idx,
cctx_T *cctx,
int silent,
int actual_is_const)
{
where_T where = WHERE_INIT;
where.wt_index = arg_idx;
return need_type_where(actual, expected, offset, where,
cctx, silent, actual_is_const);
}
/*
* Check that the top of the type stack has a type that can be used as a
* condition. Give an error and return FAIL if not.
*/
static int
bool_on_stack(cctx_T *cctx)
{
garray_T *stack = &cctx->ctx_type_stack;
type_T *type;
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (type == &t_bool)
return OK;
if (type == &t_any || type == &t_number || type == &t_number_bool)
// Number 0 and 1 are OK to use as a bool. "any" could also be a bool.
// This requires a runtime type check.
return generate_COND2BOOL(cctx);
return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE);
}
/*
* Generate an ISN_PUSHNR instruction.
*/
static int
generate_PUSHNR(cctx_T *cctx, varnumber_T number)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
return FAIL;
isn->isn_arg.number = number;
if (number == 0 || number == 1)
// A 0 or 1 number can also be used as a bool.
((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool;
return OK;
}
/*
* Generate an ISN_PUSHBOOL instruction.
*/
static int
generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
return FAIL;
isn->isn_arg.number = number;
return OK;
}
/*
* Generate an ISN_PUSHSPEC instruction.
*/
static int
generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
return FAIL;
isn->isn_arg.number = number;
return OK;
}
#ifdef FEAT_FLOAT
/*
* Generate an ISN_PUSHF instruction.
*/
static int
generate_PUSHF(cctx_T *cctx, float_T fnumber)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
return FAIL;
isn->isn_arg.fnumber = fnumber;
return OK;
}
#endif
/*
* Generate an ISN_PUSHS instruction.
* Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
*/
static int
generate_PUSHS(cctx_T *cctx, char_u **str)
{
isn_T *isn;
if (cctx->ctx_skip == SKIP_YES)
{
if (str != NULL)
VIM_CLEAR(*str);
return OK;
}
if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
{
if (str != NULL)
VIM_CLEAR(*str);
return FAIL;
}
isn->isn_arg.string = str == NULL ? NULL : *str;
return OK;
}
/*
* Generate an ISN_PUSHCHANNEL instruction.
* Consumes "channel".
*/
static int
generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
return FAIL;
isn->isn_arg.channel = channel;
return OK;
}
/*
* Generate an ISN_PUSHJOB instruction.
* Consumes "job".
*/
static int
generate_PUSHJOB(cctx_T *cctx, job_T *job)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
return FAIL;
isn->isn_arg.job = job;
return OK;
}
/*
* Generate an ISN_PUSHBLOB instruction.
* Consumes "blob".
*/
static int
generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
return FAIL;
isn->isn_arg.blob = blob;
return OK;
}
/*
* Generate an ISN_PUSHFUNC instruction with name "name".
* Consumes "name".
*/
static int
generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
return FAIL;
isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name);
return OK;
}
/*
* Generate an ISN_GETITEM instruction with "index".
* "with_op" is TRUE for "+=" and other operators, the stack has the current
* value below the list with values.
*/
static int
generate_GETITEM(cctx_T *cctx, int index, int with_op)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
type_T *type = ((type_T **)stack->ga_data)[stack->ga_len
- (with_op ? 2 : 1)];
type_T *item_type = &t_any;
RETURN_OK_IF_SKIP(cctx);
if (type->tt_type != VAR_LIST)
{
// cannot happen, caller has checked the type
emsg(_(e_listreq));
return FAIL;
}
item_type = type->tt_member;
if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
return FAIL;
isn->isn_arg.getitem.gi_index = index;
isn->isn_arg.getitem.gi_with_op = with_op;
// add the item type to the type stack
if (GA_GROW_FAILS(stack, 1))
return FAIL;
((type_T **)stack->ga_data)[stack->ga_len] = item_type;
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_SLICE instruction with "count".
*/
static int
generate_SLICE(cctx_T *cctx, int count)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
return FAIL;
isn->isn_arg.number = count;
return OK;
}
/*
* Generate an ISN_CHECKLEN instruction with "min_len".
*/
static int
generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
return FAIL;
isn->isn_arg.checklen.cl_min_len = min_len;
isn->isn_arg.checklen.cl_more_OK = more_OK;
return OK;
}
/*
* Generate an ISN_STORE instruction.
*/
static int
generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
return FAIL;
if (name != NULL)
isn->isn_arg.string = vim_strsave(name);
else
isn->isn_arg.number = idx;
return OK;
}
/*
* Generate an ISN_STOREOUTER instruction.
*/
static int
generate_STOREOUTER(cctx_T *cctx, int idx, int level)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
return FAIL;
isn->isn_arg.outer.outer_idx = idx;
isn->isn_arg.outer.outer_depth = level;
return OK;
}
/*
* Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
*/
static int
generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
return FAIL;
isn->isn_arg.storenr.stnr_idx = idx;
isn->isn_arg.storenr.stnr_val = value;
return OK;
}
/*
* Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
*/
static int
generate_STOREOPT(
cctx_T *cctx,
isntype_T isn_type,
char_u *name,
int opt_flags)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
return FAIL;
isn->isn_arg.storeopt.so_name = vim_strsave(name);
isn->isn_arg.storeopt.so_flags = opt_flags;
return OK;
}
/*
* Generate an ISN_LOAD or similar instruction.
*/
static int
generate_LOAD(
cctx_T *cctx,
isntype_T isn_type,
int idx,
char_u *name,
type_T *type)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
return FAIL;
if (name != NULL)
isn->isn_arg.string = vim_strsave(name);
else
isn->isn_arg.number = idx;
return OK;
}
/*
* Generate an ISN_LOADOUTER instruction
*/
static int
generate_LOADOUTER(
cctx_T *cctx,
int idx,
int nesting,
type_T *type)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL)
return FAIL;
isn->isn_arg.outer.outer_idx = idx;
isn->isn_arg.outer.outer_depth = nesting;
return OK;
}
/*
* Generate an ISN_LOADV instruction for v:var.
*/
static int
generate_LOADV(
cctx_T *cctx,
char_u *name,
int error)
{
int di_flags;
int vidx = find_vim_var(name, &di_flags);
type_T *type;
RETURN_OK_IF_SKIP(cctx);
if (vidx < 0)
{
if (error)
semsg(_(e_variable_not_found_str), name);
return FAIL;
}
type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
}
/*
* Generate an ISN_UNLET instruction.
*/
static int
generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, isn_type)) == NULL)
return FAIL;
isn->isn_arg.unlet.ul_name = vim_strsave(name);
isn->isn_arg.unlet.ul_forceit = forceit;
return OK;
}
/*
* Generate an ISN_LOCKCONST instruction.
*/
static int
generate_LOCKCONST(cctx_T *cctx)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
return FAIL;
return OK;
}
/*
* Generate an ISN_LOADS instruction.
*/
static int
generate_OLDSCRIPT(
cctx_T *cctx,
isntype_T isn_type,
char_u *name,
int sid,
type_T *type)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if (isn_type == ISN_LOADS)
isn = generate_instr_type(cctx, isn_type, type);
else
isn = generate_instr_drop(cctx, isn_type, 1);
if (isn == NULL)
return FAIL;
isn->isn_arg.loadstore.ls_name = vim_strsave(name);
isn->isn_arg.loadstore.ls_sid = sid;
return OK;
}
/*
* Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
*/
static int
generate_VIM9SCRIPT(
cctx_T *cctx,
isntype_T isn_type,
int sid,
int idx,
type_T *type)
{
isn_T *isn;
scriptref_T *sref;
scriptitem_T *si = SCRIPT_ITEM(sid);
RETURN_OK_IF_SKIP(cctx);
if (isn_type == ISN_LOADSCRIPT)
isn = generate_instr_type(cctx, isn_type, type);
else
isn = generate_instr_drop(cctx, isn_type, 1);
if (isn == NULL)
return FAIL;
// This requires three arguments, which doesn't fit in an instruction, thus
// we need to allocate a struct for this.
sref = ALLOC_ONE(scriptref_T);
if (sref == NULL)
return FAIL;
isn->isn_arg.script.scriptref = sref;
sref->sref_sid = sid;
sref->sref_idx = idx;
sref->sref_seq = si->sn_script_seq;
sref->sref_type = type;
return OK;
}
/*
* Generate an ISN_NEWLIST instruction.
*/
static int
generate_NEWLIST(cctx_T *cctx, int count)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
type_T *type;
type_T *member;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
return FAIL;
isn->isn_arg.number = count;
// get the member type from all the items on the stack.
if (count == 0)
member = &t_unknown;
else
member = get_member_type_from_stack(
((type_T **)stack->ga_data) + stack->ga_len, count, 1,
cctx->ctx_type_list);
type = get_list_type(member, cctx->ctx_type_list);
// drop the value types
stack->ga_len -= count;
// add the list type to the type stack
if (GA_GROW_FAILS(stack, 1))
return FAIL;
((type_T **)stack->ga_data)[stack->ga_len] = type;
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_NEWDICT instruction.
*/
static int
generate_NEWDICT(cctx_T *cctx, int count)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
type_T *type;
type_T *member;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
return FAIL;
isn->isn_arg.number = count;
if (count == 0)
member = &t_void;
else
member = get_member_type_from_stack(
((type_T **)stack->ga_data) + stack->ga_len, count, 2,
cctx->ctx_type_list);
type = get_dict_type(member, cctx->ctx_type_list);
// drop the key and value types
stack->ga_len -= 2 * count;
// add the dict type to the type stack
if (GA_GROW_FAILS(stack, 1))
return FAIL;
((type_T **)stack->ga_data)[stack->ga_len] = type;
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_FUNCREF instruction.
*/
static int
generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
return FAIL;
if (ufunc->uf_def_status == UF_NOT_COMPILED)
isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
else
isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
cctx->ctx_has_closure = 1;
// If the referenced function is a closure, it may use items further up in
// the nested context, including this one.
if (ufunc->uf_flags & FC_CLOSURE)
cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
if (GA_GROW_FAILS(stack, 1))
return FAIL;
((type_T **)stack->ga_data)[stack->ga_len] =
ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_NEWFUNC instruction.
* "lambda_name" and "func_name" must be in allocated memory and will be
* consumed.
*/
static int
generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
{
isn_T *isn;
if (cctx->ctx_skip == SKIP_YES)
{
vim_free(lambda_name);
vim_free(func_name);
return OK;
}
if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
{
vim_free(lambda_name);
vim_free(func_name);
return FAIL;
}
isn->isn_arg.newfunc.nf_lambda = lambda_name;
isn->isn_arg.newfunc.nf_global = func_name;
return OK;
}
/*
* Generate an ISN_DEF instruction: list functions
*/
static int
generate_DEF(cctx_T *cctx, char_u *name, size_t len)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
return FAIL;
if (len > 0)
{
isn->isn_arg.string = vim_strnsave(name, len);
if (isn->isn_arg.string == NULL)
return FAIL;
}
return OK;
}
/*
* Generate an ISN_JUMP instruction.
*/
static int
generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
return FAIL;
isn->isn_arg.jump.jump_when = when;
isn->isn_arg.jump.jump_where = where;
if (when != JUMP_ALWAYS && stack->ga_len > 0)
--stack->ga_len;
return OK;
}
/*
* Generate an ISN_JUMP_IF_ARG_SET instruction.
*/
static int
generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
return FAIL;
isn->isn_arg.jumparg.jump_arg_off = arg_off;
// jump_where is set later
return OK;
}
static int
generate_FOR(cctx_T *cctx, int loop_idx)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
return FAIL;
isn->isn_arg.forloop.for_idx = loop_idx;
if (GA_GROW_FAILS(stack, 1))
return FAIL;
// type doesn't matter, will be stored next
((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_TRYCONT instruction.
*/
static int
generate_TRYCONT(cctx_T *cctx, int levels, int where)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
return FAIL;
isn->isn_arg.trycont.tct_levels = levels;
isn->isn_arg.trycont.tct_where = where;
return OK;
}
/*
* Generate an ISN_BCALL instruction.
* "method_call" is TRUE for "value->method()"
* Return FAIL if the number of arguments is wrong.
*/
static int
generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
int argoff;
type_T **argtypes = NULL;
type_T *shuffled_argtypes[MAX_FUNC_ARGS];
type_T *maptype = NULL;
RETURN_OK_IF_SKIP(cctx);
argoff = check_internal_func(func_idx, argcount);
if (argoff < 0)
return FAIL;
if (method_call && argoff > 1)
{
if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
return FAIL;
isn->isn_arg.shuffle.shfl_item = argcount;
isn->isn_arg.shuffle.shfl_up = argoff - 1;
}
if (argcount > 0)
{
// Check the types of the arguments.
argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount;
if (method_call && argoff > 1)
{
int i;
for (i = 0; i < argcount; ++i)
shuffled_argtypes[i] = (i < argoff - 1)
? argtypes[i + 1]
: (i == argoff - 1) ? argtypes[0] : argtypes[i];
argtypes = shuffled_argtypes;
}
if (internal_func_check_arg_types(argtypes, func_idx, argcount,
cctx) == FAIL)
return FAIL;
if (internal_func_is_map(func_idx))
maptype = *argtypes;
}
if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
return FAIL;
isn->isn_arg.bfunc.cbf_idx = func_idx;
isn->isn_arg.bfunc.cbf_argcount = argcount;
// Drop the argument types and push the return type.
stack->ga_len -= argcount;
if (GA_GROW_FAILS(stack, 1))
return FAIL;
((type_T **)stack->ga_data)[stack->ga_len] =
internal_func_ret_type(func_idx, argcount, argtypes);
++stack->ga_len;
if (maptype != NULL && maptype->tt_member != NULL
&& maptype->tt_member != &t_any)
// Check that map() didn't change the item types.
generate_TYPECHECK(cctx, maptype, -1, 1);
return OK;
}
/*
* Generate an ISN_LISTAPPEND instruction. Works like add().
* Argument count is already checked.
*/
static int
generate_LISTAPPEND(cctx_T *cctx)
{
garray_T *stack = &cctx->ctx_type_stack;
type_T *list_type;
type_T *item_type;
type_T *expected;
// Caller already checked that list_type is a list.
list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
expected = list_type->tt_member;
if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
return FAIL;
--stack->ga_len; // drop the argument
return OK;
}
/*
* Generate an ISN_BLOBAPPEND instruction. Works like add().
* Argument count is already checked.
*/
static int
generate_BLOBAPPEND(cctx_T *cctx)
{
garray_T *stack = &cctx->ctx_type_stack;
type_T *item_type;
// Caller already checked that blob_type is a blob.
item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
return FAIL;
--stack->ga_len; // drop the argument
return OK;
}
/*
* Return TRUE if "ufunc" should be compiled, taking into account whether
* "profile" indicates profiling is to be done.
*/
int
func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
{
switch (ufunc->uf_def_status)
{
case UF_TO_BE_COMPILED:
return TRUE;
case UF_COMPILED:
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ ufunc->uf_dfunc_idx;
switch (compile_type)
{
case CT_PROFILE:
#ifdef FEAT_PROFILE
return dfunc->df_instr_prof == NULL;
#endif
case CT_NONE:
return dfunc->df_instr == NULL;
case CT_DEBUG:
return dfunc->df_instr_debug == NULL;
}
}
case UF_NOT_COMPILED:
case UF_COMPILE_ERROR:
case UF_COMPILING:
break;
}
return FALSE;
}
/*
* Generate an ISN_DCALL or ISN_UCALL instruction.
* Return FAIL if the number of arguments is wrong.
*/
static int
generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
int regular_args = ufunc->uf_args.ga_len;
int argcount = pushed_argcount;
RETURN_OK_IF_SKIP(cctx);
if (argcount > regular_args && !has_varargs(ufunc))
{
semsg(_(e_toomanyarg), printable_func_name(ufunc));
return FAIL;
}
if (argcount < regular_args - ufunc->uf_def_args.ga_len)
{
semsg(_(e_toofewarg), printable_func_name(ufunc));
return FAIL;
}
if (ufunc->uf_def_status != UF_NOT_COMPILED
&& ufunc->uf_def_status != UF_COMPILE_ERROR)
{
int i;
for (i = 0; i < argcount; ++i)
{
type_T *expected;
type_T *actual;
actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
if (actual == &t_special
&& i >= regular_args - ufunc->uf_def_args.ga_len)
{
// assume v:none used for default argument value
continue;
}
if (i < regular_args)
{
if (ufunc->uf_arg_types == NULL)
continue;
expected = ufunc->uf_arg_types[i];
}
else if (ufunc->uf_va_type == NULL
|| ufunc->uf_va_type == &t_list_any)
// possibly a lambda or "...: any"
expected = &t_any;
else
expected = ufunc->uf_va_type->tt_member;
if (need_type(actual, expected, -argcount + i, i + 1, cctx,
TRUE, FALSE) == FAIL)
{
arg_type_mismatch(expected, actual, i + 1);
return FAIL;
}
}
if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
&& compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
COMPILE_TYPE(ufunc), NULL) == FAIL)
return FAIL;
}
if (ufunc->uf_def_status == UF_COMPILE_ERROR)
{
emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
ufunc->uf_name);
return FAIL;
}
if ((isn = generate_instr(cctx,
ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
: ISN_UCALL)) == NULL)
return FAIL;
if (isn->isn_type == ISN_DCALL)
{
isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
isn->isn_arg.dfunc.cdf_argcount = argcount;
}
else
{
// A user function may be deleted and redefined later, can't use the
// ufunc pointer, need to look it up again at runtime.
isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
isn->isn_arg.ufunc.cuf_argcount = argcount;
}
stack->ga_len -= argcount; // drop the arguments
if (GA_GROW_FAILS(stack, 1))
return FAIL;
// add return value
((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_UCALL instruction when the function isn't defined yet.
*/
static int
generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
return FAIL;
isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
isn->isn_arg.ufunc.cuf_argcount = argcount;
stack->ga_len -= argcount; // drop the arguments
if (GA_GROW_FAILS(stack, 1))
return FAIL;
// add return value
((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_PCALL instruction.
* "type" is the type of the FuncRef.
*/
static int
generate_PCALL(
cctx_T *cctx,
int argcount,
char_u *name,
type_T *type,
int at_top)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
type_T *ret_type;
RETURN_OK_IF_SKIP(cctx);
if (type->tt_type == VAR_ANY)
ret_type = &t_any;
else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
{
if (type->tt_argcount != -1)
{
int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
if (argcount < type->tt_min_argcount - varargs)
{
semsg(_(e_toofewarg), name);
return FAIL;
}
if (!varargs && argcount > type->tt_argcount)
{
semsg(_(e_toomanyarg), name);
return FAIL;
}
if (type->tt_args != NULL)
{
int i;
for (i = 0; i < argcount; ++i)
{
int offset = -argcount + i - (at_top ? 0 : 1);
type_T *actual = ((type_T **)stack->ga_data)[
stack->ga_len + offset];
type_T *expected;
if (varargs && i >= type->tt_argcount - 1)
expected = type->tt_args[
type->tt_argcount - 1]->tt_member;
else if (i >= type->tt_min_argcount
&& actual == &t_special)
expected = &t_any;
else
expected = type->tt_args[i];
if (need_type(actual, expected, offset, i + 1,
cctx, TRUE, FALSE) == FAIL)
{
arg_type_mismatch(expected, actual, i + 1);
return FAIL;
}
}
}
}
ret_type = type->tt_member;
if (ret_type == &t_unknown)
// return type not known yet, use a runtime check
ret_type = &t_any;
}
else
{
semsg(_(e_not_callable_type_str), name);
return FAIL;
}
if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
return FAIL;
isn->isn_arg.pfunc.cpf_top = at_top;
isn->isn_arg.pfunc.cpf_argcount = argcount;
stack->ga_len -= argcount; // drop the arguments
// drop the funcref/partial, get back the return value
((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
// If partial is above the arguments it must be cleared and replaced with
// the return value.
if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
return FAIL;
return OK;
}
/*
* Generate an ISN_STRINGMEMBER instruction.
*/
static int
generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
type_T *type;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
return FAIL;
isn->isn_arg.string = vim_strnsave(name, len);
// check for dict type
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (type->tt_type != VAR_DICT && type != &t_any)
{
char *tofree;
semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
name, type_name(type, &tofree));
vim_free(tofree);
return FAIL;
}
// change dict type to dict member type
if (type->tt_type == VAR_DICT)
{
((type_T **)stack->ga_data)[stack->ga_len - 1] =
type->tt_member == &t_unknown ? &t_any : type->tt_member;
}
return OK;
}
/*
* Generate an ISN_ECHO instruction.
*/
static int
generate_ECHO(cctx_T *cctx, int with_white, int count)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
return FAIL;
isn->isn_arg.echo.echo_with_white = with_white;
isn->isn_arg.echo.echo_count = count;
return OK;
}
/*
* Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
*/
static int
generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
{
isn_T *isn;
if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
return FAIL;
isn->isn_arg.number = count;
return OK;
}
/*
* Generate an ISN_PUT instruction.
*/
static int
generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
return FAIL;
isn->isn_arg.put.put_regname = regname;
isn->isn_arg.put.put_lnum = lnum;
return OK;
}
/*
* Generate an EXEC instruction that takes a string argument.
* A copy is made of "line".
*/
static int
generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, isntype)) == NULL)
return FAIL;
isn->isn_arg.string = vim_strsave(line);
return OK;
}
/*
* Generate an EXEC instruction that takes a string argument.
* "str" must be allocated, it is consumed.
*/
static int
generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
{
isn_T *isn;
if (cctx->ctx_skip == SKIP_YES)
{
vim_free(str);
return OK;
}
if ((isn = generate_instr(cctx, isntype)) == NULL)
{
vim_free(str);
return FAIL;
}
isn->isn_arg.string = str;
return OK;
}
static int
generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
return FAIL;
isn->isn_arg.string = vim_strsave(line);
if (GA_GROW_FAILS(stack, 1))
return FAIL;
((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
++stack->ga_len;
return OK;
}
static int
generate_EXECCONCAT(cctx_T *cctx, int count)
{
isn_T *isn;
if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
return FAIL;
isn->isn_arg.number = count;
return OK;
}
/*
* Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
*/
static int
generate_RANGE(cctx_T *cctx, char_u *range)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
return FAIL;
isn->isn_arg.string = range;
if (GA_GROW_FAILS(stack, 1))
return FAIL;
((type_T **)stack->ga_data)[stack->ga_len] = &t_number;
++stack->ga_len;
return OK;
}
static int
generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
return FAIL;
isn->isn_arg.unpack.unp_count = var_count;
isn->isn_arg.unpack.unp_semicolon = semicolon;
return OK;
}
/*
* Generate an instruction for any command modifiers.
*/
static int
generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
{
isn_T *isn;
if (has_cmdmod(cmod, FALSE))
{
cctx->ctx_has_cmdmod = TRUE;
if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
return FAIL;
isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
return FAIL;
mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
// filter program now belongs to the instruction
cmod->cmod_filter_regmatch.regprog = NULL;
}
return OK;
}
static int
generate_undo_cmdmods(cctx_T *cctx)
{
if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
return FAIL;
cctx->ctx_has_cmdmod = FALSE;
return OK;
}
static int
misplaced_cmdmod(cctx_T *cctx)
{
garray_T *instr = &cctx->ctx_instr;
if (cctx->ctx_has_cmdmod
&& ((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type
== ISN_CMDMOD)
{
emsg(_(e_misplaced_command_modifier));
return TRUE;
}
return FALSE;
}
/*
* Get the index of the current instruction.
* This compensates for a preceding ISN_CMDMOD and ISN_PROF_START.
*/
static int
current_instr_idx(cctx_T *cctx)
{
garray_T *instr = &cctx->ctx_instr;
int idx = instr->ga_len;
while (idx > 0)
{
if (cctx->ctx_has_cmdmod && ((isn_T *)instr->ga_data)[idx - 1]
.isn_type == ISN_CMDMOD)
{
--idx;
continue;
}
#ifdef FEAT_PROFILE
if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_PROF_START)
{
--idx;
continue;
}
#endif
if (((isn_T *)instr->ga_data)[idx - 1].isn_type == ISN_DEBUG)
{
--idx;
continue;
}
break;
}
return idx;
}
#ifdef FEAT_PROFILE
static void
may_generate_prof_end(cctx_T *cctx, int prof_lnum)
{
if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
generate_instr(cctx, ISN_PROF_END);
}
#endif
/*
* Reserve space for a local variable.
* Return the variable or NULL if it failed.
*/
static lvar_T *
reserve_local(
cctx_T *cctx,
char_u *name,
size_t len,
int isConst,
type_T *type)
{
lvar_T *lvar;
dfunc_T *dfunc;
if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
{
emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
return NULL;
}
if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
return NULL;
lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
CLEAR_POINTER(lvar);
// Every local variable uses the next entry on the stack. We could re-use
// the last ones when leaving a scope, but then variables used in a closure
// might get overwritten. To keep things simple do not re-use stack
// entries. This is less efficient, but memory is cheap these days.
dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
lvar->lv_idx = dfunc->df_var_names.ga_len;
lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
lvar->lv_const = isConst;
lvar->lv_type = type;
// Remember the name for debugging.
if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
return NULL;
((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
vim_strsave(lvar->lv_name);
++dfunc->df_var_names.ga_len;
return lvar;
}
/*
* Remove local variables above "new_top".
*/
static void
unwind_locals(cctx_T *cctx, int new_top)
{
if (cctx->ctx_locals.ga_len > new_top)
{
int idx;
lvar_T *lvar;
for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
{
lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
vim_free(lvar->lv_name);
}
}
cctx->ctx_locals.ga_len = new_top;
}
/*
* Free all local variables.
*/
static void
free_locals(cctx_T *cctx)
{
unwind_locals(cctx, 0);
ga_clear(&cctx->ctx_locals);
}
/*
* If "check_writable" is ASSIGN_CONST give an error if the variable was
* defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
* error if the variable was defined with :const.
*/
static int
check_item_writable(svar_T *sv, int check_writable, char_u *name)
{
if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
|| (check_writable == ASSIGN_FINAL
&& sv->sv_const == ASSIGN_CONST))
{
semsg(_(e_cannot_change_readonly_variable_str), name);
return FAIL;
}
return OK;
}
/*
* Find "name" in script-local items of script "sid".
* Pass "check_writable" to check_item_writable().
* Returns the index in "sn_var_vals" if found.
* If found but not in "sn_var_vals" returns -1.
* If not found or the variable is not writable returns -2.
*/
int
get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx)
{
hashtab_T *ht;
dictitem_T *di;
scriptitem_T *si = SCRIPT_ITEM(sid);
svar_T *sv;
int idx;
if (!SCRIPT_ID_VALID(sid))
return -1;
if (sid == current_sctx.sc_sid)
{
sallvar_T *sav = find_script_var(name, 0, cctx);
if (sav == NULL)
return -2;
idx = sav->sav_var_vals_idx;
sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
if (check_item_writable(sv, check_writable, name) == FAIL)
return -2;
return idx;
}
// First look the name up in the hashtable.
ht = &SCRIPT_VARS(sid);
di = find_var_in_ht(ht, 0, name, TRUE);
if (di == NULL)
return -2;
// Now find the svar_T index in sn_var_vals.
for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
{
sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
if (sv->sv_tv == &di->di_tv)
{
if (check_item_writable(sv, check_writable, name) == FAIL)
return -2;
return idx;
}
}
return -1;
}
/*
* Find "name" in imported items of the current script or in "cctx" if not
* NULL.
*/
imported_T *
find_imported(char_u *name, size_t len, cctx_T *cctx)
{
int idx;
if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
return NULL;
if (cctx != NULL)
for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
{
imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
+ idx;
if (len == 0 ? STRCMP(name, import->imp_name) == 0
: STRLEN(import->imp_name) == len
&& STRNCMP(name, import->imp_name, len) == 0)
return import;
}
return find_imported_in_script(name, len, current_sctx.sc_sid);
}
imported_T *
find_imported_in_script(char_u *name, size_t len, int sid)
{
scriptitem_T *si;
int idx;
if (!SCRIPT_ID_VALID(sid))
return NULL;
si = SCRIPT_ITEM(sid);
for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
{
imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
if (len == 0 ? STRCMP(name, import->imp_name) == 0
: STRLEN(import->imp_name) == len
&& STRNCMP(name, import->imp_name, len) == 0)
return import;
}
return NULL;
}
/*
* Free all imported variables.
*/
static void
free_imported(cctx_T *cctx)
{
int idx;
for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
{
imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
vim_free(import->imp_name);
}
ga_clear(&cctx->ctx_imports);
}
/*
* Return a pointer to the next line that isn't empty or only contains a
* comment. Skips over white space.
* Returns NULL if there is none.
*/
char_u *
peek_next_line_from_context(cctx_T *cctx)
{
int lnum = cctx->ctx_lnum;
while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
{
char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
char_u *p;
// ignore NULLs inserted for continuation lines
if (line != NULL)
{
p = skipwhite(line);
if (vim9_bad_comment(p))
return NULL;
if (*p != NUL && !vim9_comment_start(p))
return p;
}
}
return NULL;
}
/*
* Called when checking for a following operator at "arg". When the rest of
* the line is empty or only a comment, peek the next line. If there is a next
* line return a pointer to it and set "nextp".
* Otherwise skip over white space.
*/
static char_u *
may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
{
char_u *p = skipwhite(arg);
*nextp = NULL;
if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
{
*nextp = peek_next_line_from_context(cctx);
if (*nextp != NULL)
return *nextp;
}
return p;
}
/*
* Get the next line of the function from "cctx".
* Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
* Returns NULL when at the end.
*/
char_u *
next_line_from_context(cctx_T *cctx, int skip_comment)
{
char_u *line;
do
{
++cctx->ctx_lnum;
if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
{
line = NULL;
break;
}
line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
cctx->ctx_line_start = line;
SOURCING_LNUM = cctx->ctx_lnum + 1;
} while (line == NULL || *skipwhite(line) == NUL
|| (skip_comment && vim9_comment_start(skipwhite(line))));
return line;
}
/*
* Skip over white space at "whitep" and assign to "*arg".
* If "*arg" is at the end of the line, advance to the next line.
* Also when "whitep" points to white space and "*arg" is on a "#".
* Return FAIL if beyond the last line, "*arg" is unmodified then.
*/
static int
may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
{
*arg = skipwhite(whitep);
if (vim9_bad_comment(*arg))
return FAIL;
if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
{
char_u *next = next_line_from_context(cctx, TRUE);
if (next == NULL)
return FAIL;
*arg = skipwhite(next);
}
return OK;
}
/*
* Idem, and give an error when failed.
*/
static int
may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
{
if (may_get_next_line(whitep, arg, cctx) == FAIL)
{
SOURCING_LNUM = cctx->ctx_lnum + 1;
emsg(_(e_line_incomplete));
return FAIL;
}
return OK;
}
// Structure passed between the compile_expr* functions to keep track of
// constants that have been parsed but for which no code was produced yet. If
// possible expressions on these constants are applied at compile time. If
// that is not possible, the code to push the constants needs to be generated
// before other instructions.
// Using 50 should be more than enough of 5 levels of ().
#define PPSIZE 50
typedef struct {
typval_T pp_tv[PPSIZE]; // stack of ppconst constants
int pp_used; // active entries in pp_tv[]
int pp_is_const; // all generated code was constants, used for a
// list or dict with constant members
} ppconst_T;
static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const);
static int compile_expr0(char_u **arg, cctx_T *cctx);
static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
/*
* Generate a PUSH instruction for "tv".
* "tv" will be consumed or cleared.
* Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
*/
static int
generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
{
if (tv != NULL)
{
switch (tv->v_type)
{
case VAR_UNKNOWN:
break;
case VAR_BOOL:
generate_PUSHBOOL(cctx, tv->vval.v_number);
break;
case VAR_SPECIAL:
generate_PUSHSPEC(cctx, tv->vval.v_number);
break;
case VAR_NUMBER:
generate_PUSHNR(cctx, tv->vval.v_number);
break;
#ifdef FEAT_FLOAT
case VAR_FLOAT:
generate_PUSHF(cctx, tv->vval.v_float);
break;
#endif
case VAR_BLOB:
generate_PUSHBLOB(cctx, tv->vval.v_blob);
tv->vval.v_blob = NULL;
break;
case VAR_STRING:
generate_PUSHS(cctx, &tv->vval.v_string);
tv->vval.v_string = NULL;
break;
default:
iemsg("constant type not supported");
clear_tv(tv);
return FAIL;
}
tv->v_type = VAR_UNKNOWN;
}
return OK;
}
/*
* Generate code for any ppconst entries.
*/
static int
generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
{
int i;
int ret = OK;
int save_skip = cctx->ctx_skip;
cctx->ctx_skip = SKIP_NOT;
for (i = 0; i < ppconst->pp_used; ++i)
if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
ret = FAIL;
ppconst->pp_used = 0;
cctx->ctx_skip = save_skip;
return ret;
}
/*
* Check that the last item of "ppconst" is a bool, if there is an item.
*/
static int
check_ppconst_bool(ppconst_T *ppconst)
{
if (ppconst->pp_used > 0)
{
typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
where_T where = WHERE_INIT;
return check_typval_type(&t_bool, tv, where);
}
return OK;
}
/*
* Clear ppconst constants. Used when failing.
*/
static void
clear_ppconst(ppconst_T *ppconst)
{
int i;
for (i = 0; i < ppconst->pp_used; ++i)
clear_tv(&ppconst->pp_tv[i]);
ppconst->pp_used = 0;
}
/*
* Compile getting a member from a list/dict/string/blob. Stack has the
* indexable value and the index or the two indexes of a slice.
* "keeping_dict" is used for dict[func](arg) to pass dict to func.
*/
static int
compile_member(int is_slice, int *keeping_dict, cctx_T *cctx)
{
type_T **typep;
garray_T *stack = &cctx->ctx_type_stack;
vartype_T vartype;
type_T *idxtype;
// We can index a list, dict and blob. If we don't know the type
// we can use the index value type. If we still don't know use an "ANY"
// instruction.
typep = ((type_T **)stack->ga_data) + stack->ga_len
- (is_slice ? 3 : 2);
vartype = (*typep)->tt_type;
idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
// If the index is a string, the variable must be a Dict.
if (*typep == &t_any && idxtype == &t_string)
vartype = VAR_DICT;
if (vartype == VAR_STRING || vartype == VAR_LIST || vartype == VAR_BLOB)
{
if (need_type(idxtype, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
if (is_slice)
{
idxtype = ((type_T **)stack->ga_data)[stack->ga_len - 2];
if (need_type(idxtype, &t_number, -2, 0, cctx,
FALSE, FALSE) == FAIL)
return FAIL;
}
}
if (vartype == VAR_DICT)
{
if (is_slice)
{
emsg(_(e_cannot_slice_dictionary));
return FAIL;
}
if ((*typep)->tt_type == VAR_DICT)
{
*typep = (*typep)->tt_member;
if (*typep == &t_unknown)
// empty dict was used
*typep = &t_any;
}
else
{
if (need_type(*typep, &t_dict_any, -2, 0, cctx,
FALSE, FALSE) == FAIL)
return FAIL;
*typep = &t_any;
}
if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
return FAIL;
if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
return FAIL;
if (keeping_dict != NULL)
*keeping_dict = TRUE;
}
else if (vartype == VAR_STRING)
{
*typep = &t_string;
if ((is_slice
? generate_instr_drop(cctx, ISN_STRSLICE, 2)
: generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL)
return FAIL;
}
else if (vartype == VAR_BLOB)
{
if (is_slice)
{
*typep = &t_blob;
if (generate_instr_drop(cctx, ISN_BLOBSLICE, 2) == FAIL)
return FAIL;
}
else
{
*typep = &t_number;
if (generate_instr_drop(cctx, ISN_BLOBINDEX, 1) == FAIL)
return FAIL;
}
}
else if (vartype == VAR_LIST || *typep == &t_any)
{
if (is_slice)
{
if (generate_instr_drop(cctx,
vartype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE,
2) == FAIL)
return FAIL;
}
else
{
if ((*typep)->tt_type == VAR_LIST)
{
*typep = (*typep)->tt_member;
if (*typep == &t_unknown)
// empty list was used
*typep = &t_any;
}
if (generate_instr_drop(cctx,
vartype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, 1)
== FAIL)
return FAIL;
}
}
else
{
emsg(_(e_string_list_dict_or_blob_required));
return FAIL;
}
return OK;
}
/*
* Generate an instruction to load script-local variable "name", without the
* leading "s:".
* Also finds imported variables.
*/
static int
compile_load_scriptvar(
cctx_T *cctx,
char_u *name, // variable NUL terminated
char_u *start, // start of variable
char_u **end, // end of variable
int error) // when TRUE may give error
{
scriptitem_T *si;
int idx;
imported_T *import;
if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
return FAIL;
si = SCRIPT_ITEM(current_sctx.sc_sid);
idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx);
if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
{
// variable is not in sn_var_vals: old style script.
return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
&t_any);
}
if (idx >= 0)
{
svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
current_sctx.sc_sid, idx, sv->sv_type);
return OK;
}
import = find_imported(name, 0, cctx);
if (import != NULL)
{
if (import->imp_flags & IMP_FLAGS_STAR)
{
char_u *p = skipwhite(*end);
char_u *exp_name;
int cc;
ufunc_T *ufunc;
type_T *type;
// Used "import * as Name", need to lookup the member.
if (*p != '.')
{
semsg(_(e_expected_dot_after_name_str), start);
return FAIL;
}
++p;
if (VIM_ISWHITE(*p))
{
emsg(_(e_no_white_space_allowed_after_dot));
return FAIL;
}
// isolate one name
exp_name = p;
while (eval_isnamec(*p))
++p;
cc = *p;
*p = NUL;
idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
cctx, TRUE);
*p = cc;
p = skipwhite(p);
*end = p;
if (idx < 0)
{
if (*p == '(' && ufunc != NULL)
{
generate_PUSHFUNC(cctx, ufunc->uf_name, import->imp_type);
return OK;
}
return FAIL;
}
generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
import->imp_sid,
idx,
type);
}
else if (import->imp_funcname != NULL)
generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type);
else
generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
import->imp_sid,
import->imp_var_vals_idx,
import->imp_type);
return OK;
}
if (error)
semsg(_(e_item_not_found_str), name);
return FAIL;
}
static int
generate_funcref(cctx_T *cctx, char_u *name)
{
ufunc_T *ufunc = find_func(name, FALSE, cctx);
if (ufunc == NULL)
return FAIL;
// Need to compile any default values to get the argument types.
if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
&& compile_def_function(ufunc, TRUE, COMPILE_TYPE(ufunc), NULL)
== FAIL)
return FAIL;
return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type);
}
/*
* Compile a variable name into a load instruction.
* "end" points to just after the name.
* "is_expr" is TRUE when evaluating an expression, might be a funcref.
* When "error" is FALSE do not give an error when not found.
*/
static int
compile_load(
char_u **arg,
char_u *end_arg,
cctx_T *cctx,
int is_expr,
int error)
{
type_T *type;
char_u *name = NULL;
char_u *end = end_arg;
int res = FAIL;
int prev_called_emsg = called_emsg;
if (*(*arg + 1) == ':')
{
if (end <= *arg + 2)
{
isntype_T isn_type;
// load dictionary of namespace
switch (**arg)
{
case 'g': isn_type = ISN_LOADGDICT; break;
case 'w': isn_type = ISN_LOADWDICT; break;
case 't': isn_type = ISN_LOADTDICT; break;
case 'b': isn_type = ISN_LOADBDICT; break;
default:
semsg(_(e_namespace_not_supported_str), *arg);
goto theend;
}
if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL)
goto theend;
res = OK;
}
else
{
isntype_T isn_type = ISN_DROP;
// load namespaced variable
name = vim_strnsave(*arg + 2, end - (*arg + 2));
if (name == NULL)
return FAIL;
switch (**arg)
{
case 'v': res = generate_LOADV(cctx, name, error);
break;
case 's': if (is_expr && ASCII_ISUPPER(*name)
&& find_func(name, FALSE, cctx) != NULL)
res = generate_funcref(cctx, name);
else
res = compile_load_scriptvar(cctx, name,
NULL, &end, error);
break;
case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
{
if (is_expr && ASCII_ISUPPER(*name)
&& find_func(name, FALSE, cctx) != NULL)
res = generate_funcref(cctx, name);
else
isn_type = ISN_LOADG;
}
else
{
isn_type = ISN_LOADAUTO;
vim_free(name);
name = vim_strnsave(*arg, end - *arg);
if (name == NULL)
return FAIL;
}
break;
case 'w': isn_type = ISN_LOADW; break;
case 't': isn_type = ISN_LOADT; break;
case 'b': isn_type = ISN_LOADB; break;
default: // cannot happen, just in case
semsg(_(e_namespace_not_supported_str), *arg);
goto theend;
}
if (isn_type != ISN_DROP)
{
// Global, Buffer-local, Window-local and Tabpage-local
// variables can be defined later, thus we don't check if it
// exists, give an error at runtime.
res = generate_LOAD(cctx, isn_type, 0, name, &t_any);
}
}
}
else
{
size_t len = end - *arg;
int idx;
int gen_load = FALSE;
int gen_load_outer = 0;
name = vim_strnsave(*arg, end - *arg);
if (name == NULL)
return FAIL;
if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
{
script_autoload(name, FALSE);
res = generate_LOAD(cctx, ISN_LOADAUTO, 0, name, &t_any);
}
else if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx)
== OK)
{
if (gen_load_outer == 0)
gen_load = TRUE;
}
else
{
lvar_T lvar;
if (lookup_local(*arg, len, &lvar, cctx) == OK)
{
type = lvar.lv_type;
idx = lvar.lv_idx;
if (lvar.lv_from_outer != 0)
gen_load_outer = lvar.lv_from_outer;
else
gen_load = TRUE;
}
else
{
// "var" can be script-local even without using "s:" if it
// already exists in a Vim9 script or when it's imported.
if (script_var_exists(*arg, len, cctx) == OK
|| find_imported(name, 0, cctx) != NULL)
res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE);
// When evaluating an expression and the name starts with an
// uppercase letter it can be a user defined function.
// generate_funcref() will fail if the function can't be found.
if (res == FAIL && is_expr && ASCII_ISUPPER(*name))
res = generate_funcref(cctx, name);
}
}
if (gen_load)
res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
if (gen_load_outer > 0)
{
res = generate_LOADOUTER(cctx, idx, gen_load_outer, type);
cctx->ctx_outer_used = TRUE;
}
}
*arg = end;
theend:
if (res == FAIL && error && called_emsg == prev_called_emsg)
semsg(_(e_variable_not_found_str), name);
vim_free(name);
return res;
}
static void
clear_instr_ga(garray_T *gap)
{
int idx;
for (idx = 0; idx < gap->ga_len; ++idx)
delete_instr(((isn_T *)gap->ga_data) + idx);
ga_clear(gap);
}
/*
* Compile a string in a ISN_PUSHS instruction into an ISN_INSTR.
* Returns FAIL if compilation fails.
*/
static int
compile_string(isn_T *isn, cctx_T *cctx)
{
char_u *s = isn->isn_arg.string;
garray_T save_ga = cctx->ctx_instr;
int expr_res;
int trailing_error;
int instr_count;
isn_T *instr = NULL;
// Remove the string type from the stack.
--cctx->ctx_type_stack.ga_len;
// Temporarily reset the list of instructions so that the jump labels are
// correct.
cctx->ctx_instr.ga_len = 0;
cctx->ctx_instr.ga_maxlen = 0;
cctx->ctx_instr.ga_data = NULL;
expr_res = compile_expr0(&s, cctx);
s = skipwhite(s);
trailing_error = *s != NUL;
if (expr_res == FAIL || trailing_error
|| GA_GROW_FAILS(&cctx->ctx_instr, 1))
{
if (trailing_error)
semsg(_(e_trailing_arg), s);
clear_instr_ga(&cctx->ctx_instr);
cctx->ctx_instr = save_ga;
++cctx->ctx_type_stack.ga_len;
return FAIL;
}
// Move the generated instructions into the ISN_INSTR instruction, then
// restore the list of instructions.
instr_count = cctx->ctx_instr.ga_len;
instr = cctx->ctx_instr.ga_data;
instr[instr_count].isn_type = ISN_FINISH;
cctx->ctx_instr = save_ga;
vim_free(isn->isn_arg.string);
isn->isn_type = ISN_INSTR;
isn->isn_arg.instr = instr;
return OK;
}
/*
* Compile the argument expressions.
* "arg" points to just after the "(" and is advanced to after the ")"
*/
static int
compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, int is_searchpair)
{
char_u *p = *arg;
char_u *whitep = *arg;
int must_end = FALSE;
int instr_count;
for (;;)
{
if (may_get_next_line(whitep, &p, cctx) == FAIL)
goto failret;
if (*p == ')')
{
*arg = p + 1;
return OK;
}
if (must_end)
{
semsg(_(e_missing_comma_before_argument_str), p);
return FAIL;
}
instr_count = cctx->ctx_instr.ga_len;
if (compile_expr0(&p, cctx) == FAIL)
return FAIL;
++*argcount;
if (is_searchpair && *argcount == 5
&& cctx->ctx_instr.ga_len == instr_count + 1)
{
isn_T *isn = ((isn_T *)cctx->ctx_instr.ga_data) + instr_count;
// {skip} argument of searchpair() can be compiled if not empty
if (isn->isn_type == ISN_PUSHS && *isn->isn_arg.string != NUL)
compile_string(isn, cctx);
}
if (*p != ',' && *skipwhite(p) == ',')
{
semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
p = skipwhite(p);
}
if (*p == ',')
{
++p;
if (*p != NUL && !VIM_ISWHITE(*p))
semsg(_(e_white_space_required_after_str_str), ",", p - 1);
}
else
must_end = TRUE;
whitep = p;
p = skipwhite(p);
}
failret:
emsg(_(e_missing_close));
return FAIL;
}
/*
* Compile a function call: name(arg1, arg2)
* "arg" points to "name", "arg + varlen" to the "(".
* "argcount_init" is 1 for "value->method()"
* Instructions:
* EVAL arg1
* EVAL arg2
* BCALL / DCALL / UCALL
*/
static int
compile_call(
char_u **arg,
size_t varlen,
cctx_T *cctx,
ppconst_T *ppconst,
int argcount_init)
{
char_u *name = *arg;
char_u *p;
int argcount = argcount_init;
char_u namebuf[100];
char_u fname_buf[FLEN_FIXED + 1];
char_u *tofree = NULL;
int error = FCERR_NONE;
ufunc_T *ufunc = NULL;
int res = FAIL;
int is_autoload;
int is_searchpair;
// We can evaluate "has('name')" at compile time.
// We always evaluate "exists_compiled()" at compile time.
if ((varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
|| (varlen == 15 && STRNCMP(*arg, "exists_compiled", 6) == 0))
{
char_u *s = skipwhite(*arg + varlen + 1);
typval_T argvars[2];
int is_has = **arg == 'h';
argvars[0].v_type = VAR_UNKNOWN;
if (*s == '"')
(void)eval_string(&s, &argvars[0], TRUE);
else if (*s == '\'')
(void)eval_lit_string(&s, &argvars[0], TRUE);
s = skipwhite(s);
if (*s == ')' && argvars[0].v_type == VAR_STRING
&& ((is_has && !dynamic_feature(argvars[0].vval.v_string))
|| !is_has))
{
typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
*arg = s + 1;
argvars[1].v_type = VAR_UNKNOWN;
tv->v_type = VAR_NUMBER;
tv->vval.v_number = 0;
if (is_has)
f_has(argvars, tv);
else
f_exists(argvars, tv);
clear_tv(&argvars[0]);
++ppconst->pp_used;
return OK;
}
clear_tv(&argvars[0]);
if (!is_has)
{
emsg(_(e_argument_of_exists_compiled_must_be_literal_string));
return FAIL;
}
}
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
if (varlen >= sizeof(namebuf))
{
semsg(_(e_name_too_long_str), name);
return FAIL;
}
vim_strncpy(namebuf, *arg, varlen);
name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
// We handle the "skip" argument of searchpair() and searchpairpos()
// differently.
is_searchpair = (varlen == 6 && STRNCMP(*arg, "search", 6) == 0)
|| (varlen == 9 && STRNCMP(*arg, "searchpos", 9) == 0)
|| (varlen == 10 && STRNCMP(*arg, "searchpair", 10) == 0)
|| (varlen == 13 && STRNCMP(*arg, "searchpairpos", 13) == 0);
*arg = skipwhite(*arg + varlen + 1);
if (compile_arguments(arg, cctx, &argcount, is_searchpair) == FAIL)
goto theend;
is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL;
if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload)
{
int idx;
// builtin function
idx = find_internal_func(name);
if (idx >= 0)
{
if (STRCMP(name, "flatten") == 0)
{
emsg(_(e_cannot_use_flatten_in_vim9_script));
goto theend;
}
if (STRCMP(name, "add") == 0 && argcount == 2)
{
garray_T *stack = &cctx->ctx_type_stack;
type_T *type = ((type_T **)stack->ga_data)[
stack->ga_len - 2];
// add() can be compiled to instructions if we know the type
if (type->tt_type == VAR_LIST)
{
// inline "add(list, item)" so that the type can be checked
res = generate_LISTAPPEND(cctx);
idx = -1;
}
else if (type->tt_type == VAR_BLOB)
{
// inline "add(blob, nr)" so that the type can be checked
res = generate_BLOBAPPEND(cctx);
idx = -1;
}
}
if (idx >= 0)
res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
}
else
semsg(_(e_unknownfunc), namebuf);
goto theend;
}
// An argument or local variable can be a function reference, this
// overrules a function name.
if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL
&& arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK)
{
// If we can find the function by name generate the right call.
// Skip global functions here, a local funcref takes precedence.
ufunc = find_func(name, FALSE, cctx);
if (ufunc != NULL && !func_is_global(ufunc))
{
res = generate_CALL(cctx, ufunc, argcount);
goto theend;
}
}
// If the name is a variable, load it and use PCALL.
// Not for g:Func(), we don't know if it is a variable or not.
// Not for eome#Func(), it will be loaded later.
p = namebuf;
if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload
&& compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK)
{
garray_T *stack = &cctx->ctx_type_stack;
type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
goto theend;
}
// If we can find a global function by name generate the right call.
if (ufunc != NULL)
{
res = generate_CALL(cctx, ufunc, argcount);
goto theend;
}
// A global function may be defined only later. Need to figure out at
// runtime. Also handles a FuncRef at runtime.
if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload)
res = generate_UCALL(cctx, name, argcount);
else
semsg(_(e_unknownfunc), namebuf);
theend:
vim_free(tofree);
return res;
}
// like NAMESPACE_CHAR but with 'a' and 'l'.
#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
/*
* Find the end of a variable or function name. Unlike find_name_end() this
* does not recognize magic braces.
* When "use_namespace" is TRUE recognize "b:", "s:", etc.
* Return a pointer to just after the name. Equal to "arg" if there is no
* valid name.
*/
char_u *
to_name_end(char_u *arg, int use_namespace)
{
char_u *p;
// Quick check for valid starting character.
if (!eval_isnamec1(*arg))
return arg;
for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
// Include a namespace such as "s:var" and "v:var". But "n:" is not
// and can be used in slice "[n:]".
if (*p == ':' && (p != arg + 1
|| !use_namespace
|| vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
break;
return p;
}
/*
* Like to_name_end() but also skip over a list or dict constant.
* Also accept "<SNR>123_Func".
* This intentionally does not handle line continuation.
*/
char_u *
to_name_const_end(char_u *arg)
{
char_u *p = arg;
typval_T rettv;
if (STRNCMP(p, "<SNR>", 5) == 0)
p = skipdigits(p + 5);
p = to_name_end(p, TRUE);
if (p == arg && *arg == '[')
{
// Can be "[1, 2, 3]->Func()".
if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
p = arg;
}
return p;
}
/*
* parse a list: [expr, expr]
* "*arg" points to the '['.
* ppconst->pp_is_const is set if all items are a constant.
*/
static int
compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
char_u *p = skipwhite(*arg + 1);
char_u *whitep = *arg + 1;
int count = 0;
int is_const;
int is_all_const = TRUE; // reset when non-const encountered
for (;;)
{
if (may_get_next_line(whitep, &p, cctx) == FAIL)
{
semsg(_(e_list_end), *arg);
return FAIL;
}
if (*p == ',')
{
semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
return FAIL;
}
if (*p == ']')
{
++p;
break;
}
if (compile_expr0_ext(&p, cctx, &is_const) == FAIL)
return FAIL;
if (!is_const)
is_all_const = FALSE;
++count;
if (*p == ',')
{
++p;
if (*p != ']' && !IS_WHITE_OR_NUL(*p))
{
semsg(_(e_white_space_required_after_str_str), ",", p - 1);
return FAIL;
}
}
whitep = p;
p = skipwhite(p);
}
*arg = p;
ppconst->pp_is_const = is_all_const;
return generate_NEWLIST(cctx, count);
}
/*
* Parse a lambda: "(arg, arg) => expr"
* "*arg" points to the '('.
* Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda.
*/
static int
compile_lambda(char_u **arg, cctx_T *cctx)
{
int r;
typval_T rettv;
ufunc_T *ufunc;
evalarg_T evalarg;
init_evalarg(&evalarg);
evalarg.eval_flags = EVAL_EVALUATE;
evalarg.eval_cctx = cctx;
// Get the funcref in "rettv".
r = get_lambda_tv(arg, &rettv, TRUE, &evalarg);
if (r != OK)
{
clear_evalarg(&evalarg, NULL);
return r;
}
// "rettv" will now be a partial referencing the function.
ufunc = rettv.vval.v_partial->pt_func;
++ufunc->uf_refcount;
clear_tv(&rettv);
// Compile it here to get the return type. The return type is optional,
// when it's missing use t_unknown. This is recognized in
// compile_return().
if (ufunc->uf_ret_type->tt_type == VAR_VOID)
ufunc->uf_ret_type = &t_unknown;
compile_def_function(ufunc, FALSE, cctx->ctx_compile_type, cctx);
// When the outer function is compiled for profiling or debugging, the
// lambda may be called without profiling or debugging. Compile it here in
// the right context.
if (cctx->ctx_compile_type == CT_DEBUG
#ifdef FEAT_PROFILE
|| cctx->ctx_compile_type == CT_PROFILE
#endif
)
compile_def_function(ufunc, FALSE, CT_NONE, cctx);
// The last entry in evalarg.eval_tofree_ga is a copy of the last line and
// "*arg" may point into it. Point into the original line to avoid a
// dangling pointer.
if (evalarg.eval_using_cmdline)
{
garray_T *gap = &evalarg.eval_tofree_ga;
size_t off = *arg - ((char_u **)gap->ga_data)[gap->ga_len - 1];
*arg = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]
+ off;
}
clear_evalarg(&evalarg, NULL);
if (ufunc->uf_def_status == UF_COMPILED)
{
// The return type will now be known.
set_function_type(ufunc);
// The function reference count will be 1. When the ISN_FUNCREF
// instruction is deleted the reference count is decremented and the
// function is freed.
return generate_FUNCREF(cctx, ufunc);
}
func_ptr_unref(ufunc);
return FAIL;
}
/*
* Get a lambda and compile it. Uses Vim9 syntax.
*/
int
get_lambda_tv_and_compile(
char_u **arg,
typval_T *rettv,
int types_optional,
evalarg_T *evalarg)
{
int r;
ufunc_T *ufunc;
int save_sc_version = current_sctx.sc_version;
// Get the funcref in "rettv".
current_sctx.sc_version = SCRIPT_VERSION_VIM9;
r = get_lambda_tv(arg, rettv, types_optional, evalarg);
current_sctx.sc_version = save_sc_version;
if (r != OK)
return r;
// "rettv" will now be a partial referencing the function.
ufunc = rettv->vval.v_partial->pt_func;
// Compile it here to get the return type. The return type is optional,
// when it's missing use t_unknown. This is recognized in
// compile_return().
if (ufunc->uf_ret_type == NULL || ufunc->uf_ret_type->tt_type == VAR_VOID)
ufunc->uf_ret_type = &t_unknown;
compile_def_function(ufunc, FALSE, CT_NONE, NULL);
if (ufunc->uf_def_status == UF_COMPILED)
{
// The return type will now be known.
set_function_type(ufunc);
return OK;
}
clear_tv(rettv);
return FAIL;
}
/*
* parse a dict: {key: val, [key]: val}
* "*arg" points to the '{'.
* ppconst->pp_is_const is set if all item values are a constant.
*/
static int
compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
garray_T *instr = &cctx->ctx_instr;
int count = 0;
dict_T *d = dict_alloc();
dictitem_T *item;
char_u *whitep = *arg + 1;
char_u *p;
int is_const;
int is_all_const = TRUE; // reset when non-const encountered
if (d == NULL)
return FAIL;
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
for (;;)
{
char_u *key = NULL;
if (may_get_next_line(whitep, arg, cctx) == FAIL)
{
*arg = NULL;
goto failret;
}
if (**arg == '}')
break;
if (**arg == '[')
{
isn_T *isn;
// {[expr]: value} uses an evaluated key.
*arg = skipwhite(*arg + 1);
if (compile_expr0(arg, cctx) == FAIL)
return FAIL;
isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
if (isn->isn_type == ISN_PUSHNR)
{
char buf[NUMBUFLEN];
// Convert to string at compile time.
vim_snprintf(buf, NUMBUFLEN, "%lld", isn->isn_arg.number);
isn->isn_type = ISN_PUSHS;
isn->isn_arg.string = vim_strsave((char_u *)buf);
}
if (isn->isn_type == ISN_PUSHS)
key = isn->isn_arg.string;
else if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
return FAIL;
*arg = skipwhite(*arg);
if (**arg != ']')
{
emsg(_(e_missing_matching_bracket_after_dict_key));
return FAIL;
}
++*arg;
}
else
{
// {"name": value},
// {'name': value},
// {name: value} use "name" as a literal key
key = get_literal_key(arg);
if (key == NULL)
return FAIL;
if (generate_PUSHS(cctx, &key) == FAIL)
return FAIL;
}
// Check for duplicate keys, if using string keys.
if (key != NULL)
{
item = dict_find(d, key, -1);
if (item != NULL)
{
semsg(_(e_duplicate_key), key);
goto failret;
}
item = dictitem_alloc(key);
if (item != NULL)
{
item->di_tv.v_type = VAR_UNKNOWN;
item->di_tv.v_lock = 0;
if (dict_add(d, item) == FAIL)
dictitem_free(item);
}
}
if (**arg != ':')
{
if (*skipwhite(*arg) == ':')
semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
else
semsg(_(e_missing_dict_colon), *arg);
return FAIL;
}
whitep = *arg + 1;
if (!IS_WHITE_OR_NUL(*whitep))
{
semsg(_(e_white_space_required_after_str_str), ":", *arg);
return FAIL;
}
if (may_get_next_line(whitep, arg, cctx) == FAIL)
{
*arg = NULL;
goto failret;
}
if (compile_expr0_ext(arg, cctx, &is_const) == FAIL)
return FAIL;
if (!is_const)
is_all_const = FALSE;
++count;
whitep = *arg;
if (may_get_next_line(whitep, arg, cctx) == FAIL)
{
*arg = NULL;
goto failret;
}
if (**arg == '}')
break;
if (**arg != ',')
{
semsg(_(e_missing_dict_comma), *arg);
goto failret;
}
if (IS_WHITE_OR_NUL(*whitep))
{
semsg(_(e_no_white_space_allowed_before_str_str), ",", whitep);
return FAIL;
}
whitep = *arg + 1;
if (!IS_WHITE_OR_NUL(*whitep))
{
semsg(_(e_white_space_required_after_str_str), ",", *arg);
return FAIL;
}
*arg = skipwhite(whitep);
}
*arg = *arg + 1;
// Allow for following comment, after at least one space.
p = skipwhite(*arg);
if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
*arg += STRLEN(*arg);
dict_unref(d);
ppconst->pp_is_const = is_all_const;
return generate_NEWDICT(cctx, count);
failret:
if (*arg == NULL)
{
semsg(_(e_missing_dict_end), _("[end of lines]"));
*arg = (char_u *)"";
}
dict_unref(d);
return FAIL;
}
/*
* Compile "&option".
*/
static int
compile_get_option(char_u **arg, cctx_T *cctx)
{
typval_T rettv;
char_u *start = *arg;
int ret;
// parse the option and get the current value to get the type.
rettv.v_type = VAR_UNKNOWN;
ret = eval_option(arg, &rettv, TRUE);
if (ret == OK)
{
// include the '&' in the name, eval_option() expects it.
char_u *name = vim_strnsave(start, *arg - start);
type_T *type = rettv.v_type == VAR_BOOL ? &t_bool
: rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
vim_free(name);
}
clear_tv(&rettv);
return ret;
}
/*
* Compile "$VAR".
*/
static int
compile_get_env(char_u **arg, cctx_T *cctx)
{
char_u *start = *arg;
int len;
int ret;
char_u *name;
++*arg;
len = get_env_len(arg);
if (len == 0)
{
semsg(_(e_syntax_error_at_str), start - 1);
return FAIL;
}
// include the '$' in the name, eval_env_var() expects it.
name = vim_strnsave(start, len + 1);
ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
vim_free(name);
return ret;
}
/*
* Compile "@r".
*/
static int
compile_get_register(char_u **arg, cctx_T *cctx)
{
int ret;
++*arg;
if (**arg == NUL)
{
semsg(_(e_syntax_error_at_str), *arg - 1);
return FAIL;
}
if (!valid_yank_reg(**arg, FALSE))
{
emsg_invreg(**arg);
return FAIL;
}
ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
++*arg;
return ret;
}
/*
* Apply leading '!', '-' and '+' to constant "rettv".
* When "numeric_only" is TRUE do not apply '!'.
*/
static int
apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end)
{
char_u *p = *end;
// this works from end to start
while (p > start)
{
--p;
if (*p == '-' || *p == '+')
{
// only '-' has an effect, for '+' we only check the type
#ifdef FEAT_FLOAT
if (rettv->v_type == VAR_FLOAT)
{
if (*p == '-')
rettv->vval.v_float = -rettv->vval.v_float;
}
else
#endif
{
varnumber_T val;
int error = FALSE;
// tv_get_number_chk() accepts a string, but we don't want that
// here
if (check_not_string(rettv) == FAIL)
return FAIL;
val = tv_get_number_chk(rettv, &error);
clear_tv(rettv);
if (error)
return FAIL;
if (*p == '-')
val = -val;
rettv->v_type = VAR_NUMBER;
rettv->vval.v_number = val;
}
}
else if (numeric_only)
{
++p;
break;
}
else if (*p == '!')
{
int v = tv2bool(rettv);
// '!' is permissive in the type.
clear_tv(rettv);
rettv->v_type = VAR_BOOL;
rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
}
}
*end = p;
return OK;
}
/*
* Recognize v: variables that are constants and set "rettv".
*/
static void
get_vim_constant(char_u **arg, typval_T *rettv)
{
if (STRNCMP(*arg, "v:true", 6) == 0)
{
rettv->v_type = VAR_BOOL;
rettv->vval.v_number = VVAL_TRUE;
*arg += 6;
}
else if (STRNCMP(*arg, "v:false", 7) == 0)
{
rettv->v_type = VAR_BOOL;
rettv->vval.v_number = VVAL_FALSE;
*arg += 7;
}
else if (STRNCMP(*arg, "v:null", 6) == 0)
{
rettv->v_type = VAR_SPECIAL;
rettv->vval.v_number = VVAL_NULL;
*arg += 6;
}
else if (STRNCMP(*arg, "v:none", 6) == 0)
{
rettv->v_type = VAR_SPECIAL;
rettv->vval.v_number = VVAL_NONE;
*arg += 6;
}
}
exprtype_T
get_compare_type(char_u *p, int *len, int *type_is)
{
exprtype_T type = EXPR_UNKNOWN;
int i;
switch (p[0])
{
case '=': if (p[1] == '=')
type = EXPR_EQUAL;
else if (p[1] == '~')
type = EXPR_MATCH;
break;
case '!': if (p[1] == '=')
type = EXPR_NEQUAL;
else if (p[1] == '~')
type = EXPR_NOMATCH;
break;
case '>': if (p[1] != '=')
{
type = EXPR_GREATER;
*len = 1;
}
else
type = EXPR_GEQUAL;
break;
case '<': if (p[1] != '=')
{
type = EXPR_SMALLER;
*len = 1;
}
else
type = EXPR_SEQUAL;
break;
case 'i': if (p[1] == 's')
{
// "is" and "isnot"; but not a prefix of a name
if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
*len = 5;
i = p[*len];
if (!isalnum(i) && i != '_')
{
type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
*type_is = TRUE;
}
}
break;
}
return type;
}
/*
* Skip over an expression, ignoring most errors.
*/
static void
skip_expr_cctx(char_u **arg, cctx_T *cctx)
{
evalarg_T evalarg;
init_evalarg(&evalarg);
evalarg.eval_cctx = cctx;
skip_expr(arg, &evalarg);
clear_evalarg(&evalarg, NULL);
}
/*
* Compile code to apply '-', '+' and '!'.
* When "numeric_only" is TRUE do not apply '!'.
*/
static int
compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end)
{
char_u *p = *end;
// this works from end to start
while (p > start)
{
--p;
while (VIM_ISWHITE(*p))
--p;
if (*p == '-' || *p == '+')
{
int negate = *p == '-';
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
type_T *type;
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (need_type(type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
while (p > start && (p[-1] == '-' || p[-1] == '+'))
{
--p;
if (*p == '-')
negate = !negate;
}
// only '-' has an effect, for '+' we only check the type
if (negate)
{
isn = generate_instr(cctx, ISN_NEGATENR);
if (isn == NULL)
return FAIL;
}
}
else if (numeric_only)
{
++p;
break;
}
else
{
int invert = *p == '!';
while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1])))
{
if (p[-1] == '!')
invert = !invert;
--p;
}
if (generate_2BOOL(cctx, invert, -1) == FAIL)
return FAIL;
}
}
*end = p;
return OK;
}
/*
* Compile "(expression)": recursive!
* Return FAIL/OK.
*/
static int
compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
int ret;
char_u *p = *arg + 1;
if (may_get_next_line_error(p, arg, cctx) == FAIL)
return FAIL;
if (ppconst->pp_used <= PPSIZE - 10)
{
ret = compile_expr1(arg, cctx, ppconst);
}
else
{
// Not enough space in ppconst, flush constants.
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
ret = compile_expr0(arg, cctx);
}
if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
return FAIL;
if (**arg == ')')
++*arg;
else if (ret == OK)
{
emsg(_(e_missing_close));
ret = FAIL;
}
return ret;
}
/*
* Compile whatever comes after "name" or "name()".
* Advances "*arg" only when something was recognized.
*/
static int
compile_subscript(
char_u **arg,
cctx_T *cctx,
char_u *start_leader,
char_u **end_leader,
ppconst_T *ppconst)
{
char_u *name_start = *end_leader;
int keeping_dict = FALSE;
for (;;)
{
char_u *p = skipwhite(*arg);
if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
{
char_u *next = peek_next_line_from_context(cctx);
// If a following line starts with "->{" or "->X" advance to that
// line, so that a line break before "->" is allowed.
// Also if a following line starts with ".x".
if (next != NULL &&
((next[0] == '-' && next[1] == '>'
&& (next[2] == '{'
|| ASCII_ISALPHA(*skipwhite(next + 2))))
|| (next[0] == '.' && eval_isdictc(next[1]))))
{
next = next_line_from_context(cctx, TRUE);
if (next == NULL)
return FAIL;
*arg = next;
p = skipwhite(*arg);
}
}
// Do not skip over white space to find the "(", "execute 'x' (expr)"
// is not a function call.
if (**arg == '(')
{
garray_T *stack = &cctx->ctx_type_stack;
type_T *type;
int argcount = 0;
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
ppconst->pp_is_const = FALSE;
// funcref(arg)
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
*arg = skipwhite(p + 1);
if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
return FAIL;
if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL)
return FAIL;
if (keeping_dict)
{
keeping_dict = FALSE;
if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
return FAIL;
}
}
else if (*p == '-' && p[1] == '>')
{
char_u *pstart = p;
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
ppconst->pp_is_const = FALSE;
// something->method()
// Apply the '!', '-' and '+' first:
// -1.0->func() works like (-1.0)->func()
if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL)
return FAIL;
p += 2;
*arg = skipwhite(p);
// No line break supported right after "->".
if (**arg == '(')
{
int argcount = 1;
garray_T *stack = &cctx->ctx_type_stack;
int type_idx_start = stack->ga_len;
type_T *type;
int expr_isn_start = cctx->ctx_instr.ga_len;
int expr_isn_end;
int arg_isn_count;
// Funcref call: list->(Refs[2])(arg)
// or lambda: list->((arg) => expr)(arg)
//
// Fist compile the function expression.
if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
return FAIL;
// Remember the next instruction index, where the instructions
// for arguments are being written.
expr_isn_end = cctx->ctx_instr.ga_len;
// Compile the arguments.
if (**arg != '(')
{
if (*skipwhite(*arg) == '(')
emsg(_(e_nowhitespace));
else
semsg(_(e_missing_paren), *arg);
return FAIL;
}
*arg = skipwhite(*arg + 1);
if (compile_arguments(arg, cctx, &argcount, FALSE) == FAIL)
return FAIL;
// Move the instructions for the arguments to before the
// instructions of the expression and move the type of the
// expression after the argument types. This is what ISN_PCALL
// expects.
stack = &cctx->ctx_type_stack;
arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
if (arg_isn_count > 0)
{
int expr_isn_count = expr_isn_end - expr_isn_start;
isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
if (isn == NULL)
return FAIL;
mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
+ expr_isn_start,
sizeof(isn_T) * expr_isn_count);
mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
+ expr_isn_start,
((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
sizeof(isn_T) * arg_isn_count);
mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
+ expr_isn_start + arg_isn_count,
isn, sizeof(isn_T) * expr_isn_count);
vim_free(isn);
type = ((type_T **)stack->ga_data)[type_idx_start];
mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
((type_T **)stack->ga_data) + type_idx_start + 1,
sizeof(type_T *)
* (stack->ga_len - type_idx_start - 1));
((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
}
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (generate_PCALL(cctx, argcount, p - 2, type, FALSE) == FAIL)
return FAIL;
}
else
{
// method call: list->method()
p = *arg;
if (!eval_isnamec1(*p))
{
semsg(_(e_trailing_arg), pstart);
return FAIL;
}
if (ASCII_ISALPHA(*p) && p[1] == ':')
p += 2;
for ( ; eval_isnamec(*p); ++p)
;
if (*p != '(')
{
semsg(_(e_missing_paren), *arg);
return FAIL;
}
if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
return FAIL;
}
if (keeping_dict)
{
keeping_dict = FALSE;
if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
return FAIL;
}
}
else if (**arg == '[')
{
int is_slice = FALSE;
// list index: list[123]
// dict member: dict[key]
// string index: text[123]
// blob index: blob[123]
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
ppconst->pp_is_const = FALSE;
++p;
if (may_get_next_line_error(p, arg, cctx) == FAIL)
return FAIL;
if (**arg == ':')
{
// missing first index is equal to zero
generate_PUSHNR(cctx, 0);
}
else
{
if (compile_expr0(arg, cctx) == FAIL)
return FAIL;
if (**arg == ':')
{
semsg(_(e_white_space_required_before_and_after_str_at_str),
":", *arg);
return FAIL;
}
if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
return FAIL;
*arg = skipwhite(*arg);
}
if (**arg == ':')
{
is_slice = TRUE;
++*arg;
if (!IS_WHITE_OR_NUL(**arg) && **arg != ']')
{
semsg(_(e_white_space_required_before_and_after_str_at_str),
":", *arg);
return FAIL;
}
if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
return FAIL;
if (**arg == ']')
// missing second index is equal to end of string
generate_PUSHNR(cctx, -1);
else
{
if (compile_expr0(arg, cctx) == FAIL)
return FAIL;
if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
return FAIL;
*arg = skipwhite(*arg);
}
}
if (**arg != ']')
{
emsg(_(e_missbrac));
return FAIL;
}
*arg = *arg + 1;
if (keeping_dict)
{
keeping_dict = FALSE;
if (generate_instr(cctx, ISN_CLEARDICT) == NULL)
return FAIL;
}
if (compile_member(is_slice, &keeping_dict, cctx) == FAIL)
return FAIL;
}
else if (*p == '.' && p[1] != '.')
{
// dictionary member: dict.name
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
ppconst->pp_is_const = FALSE;
*arg = p + 1;
if (IS_WHITE_OR_NUL(**arg))
{
emsg(_(e_missing_name_after_dot));
return FAIL;
}
p = *arg;
if (eval_isdictc(*p))
while (eval_isnamec(*p))
MB_PTR_ADV(p);
if (p == *arg)
{
semsg(_(e_syntax_error_at_str), *arg);
return FAIL;
}
if (keeping_dict && generate_instr(cctx, ISN_CLEARDICT) == NULL)
return FAIL;
if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
return FAIL;
keeping_dict = TRUE;
*arg = p;
}
else
break;
}
// Turn "dict.Func" into a partial for "Func" bound to "dict".
// This needs to be done at runtime to be able to check the type.
if (keeping_dict && generate_instr(cctx, ISN_USEDICT) == NULL)
return FAIL;
return OK;
}
/*
* Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
* "arg" is advanced until after the expression, skipping white space.
*
* If the value is a constant "ppconst->pp_used" will be non-zero.
* Before instructions are generated, any values in "ppconst" will generated.
*
* This is the compiling equivalent of eval1(), eval2(), etc.
*/
/*
* number number constant
* 0zFFFFFFFF Blob constant
* "string" string constant
* 'string' literal string constant
* &option-name option value
* @r register contents
* identifier variable value
* function() function call
* $VAR environment variable
* (expression) nested expression
* [expr, expr] List
* {key: val, [key]: val} Dictionary
*
* Also handle:
* ! in front logical NOT
* - in front unary minus
* + in front unary plus (ignored)
* trailing (arg) funcref/partial call
* trailing [] subscript in String or List
* trailing .name entry in Dictionary
* trailing ->name() method call
*/
static int
compile_expr7(
char_u **arg,
cctx_T *cctx,
ppconst_T *ppconst)
{
char_u *start_leader, *end_leader;
int ret = OK;
typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
int used_before = ppconst->pp_used;
ppconst->pp_is_const = FALSE;
/*
* Skip '!', '-' and '+' characters. They are handled later.
*/
start_leader = *arg;
if (eval_leader(arg, TRUE) == FAIL)
return FAIL;
end_leader = *arg;
rettv->v_type = VAR_UNKNOWN;
switch (**arg)
{
/*
* Number constant.
*/
case '0': // also for blob starting with 0z
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
return FAIL;
// Apply "-" and "+" just before the number now, right to
// left. Matters especially when "->" follows. Stops at
// '!'.
if (apply_leader(rettv, TRUE,
start_leader, &end_leader) == FAIL)
{
clear_tv(rettv);
return FAIL;
}
break;
/*
* String constant: "string".
*/
case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
return FAIL;
break;
/*
* Literal string constant: 'str''ing'.
*/
case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
return FAIL;
break;
/*
* Constant Vim variable.
*/
case 'v': get_vim_constant(arg, rettv);
ret = NOTDONE;
break;
/*
* "true" constant
*/
case 't': if (STRNCMP(*arg, "true", 4) == 0
&& !eval_isnamec((*arg)[4]))
{
*arg += 4;
rettv->v_type = VAR_BOOL;
rettv->vval.v_number = VVAL_TRUE;
}
else
ret = NOTDONE;
break;
/*
* "false" constant
*/
case 'f': if (STRNCMP(*arg, "false", 5) == 0
&& !eval_isnamec((*arg)[5]))
{
*arg += 5;
rettv->v_type = VAR_BOOL;
rettv->vval.v_number = VVAL_FALSE;
}
else
ret = NOTDONE;
break;
/*
* "null" constant
*/
case 'n': if (STRNCMP(*arg, "null", 4) == 0
&& !eval_isnamec((*arg)[4]))
{
*arg += 4;
rettv->v_type = VAR_SPECIAL;
rettv->vval.v_number = VVAL_NULL;
}
else
ret = NOTDONE;
break;
/*
* List: [expr, expr]
*/
case '[': if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
ret = compile_list(arg, cctx, ppconst);
break;
/*
* Dictionary: {'key': val, 'key': val}
*/
case '{': if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
ret = compile_dict(arg, cctx, ppconst);
break;
/*
* Option value: &name
*/
case '&': if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
ret = compile_get_option(arg, cctx);
break;
/*
* Environment variable: $VAR.
*/
case '$': if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
ret = compile_get_env(arg, cctx);
break;
/*
* Register contents: @r.
*/
case '@': if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
ret = compile_get_register(arg, cctx);
break;
/*
* nested expression: (expression).
* lambda: (arg, arg) => expr
* funcref: (arg, arg) => { statement }
*/
case '(': // if compile_lambda returns NOTDONE then it must be (expr)
ret = compile_lambda(arg, cctx);
if (ret == NOTDONE)
ret = compile_parenthesis(arg, cctx, ppconst);
break;
default: ret = NOTDONE;
break;
}
if (ret == FAIL)
return FAIL;
if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
{
if (cctx->ctx_skip == SKIP_YES)
clear_tv(rettv);
else
// A constant expression can possibly be handled compile time,
// return the value instead of generating code.
++ppconst->pp_used;
}
else if (ret == NOTDONE)
{
char_u *p;
int r;
if (!eval_isnamec1(**arg))
{
if (!vim9_bad_comment(*arg))
{
if (ends_excmd(*skipwhite(*arg)))
semsg(_(e_empty_expression_str), *arg);
else
semsg(_(e_name_expected_str), *arg);
}
return FAIL;
}
// "name" or "name()"
p = to_name_end(*arg, TRUE);
if (p - *arg == (size_t)1 && **arg == '_')
{
emsg(_(e_cannot_use_underscore_here));
return FAIL;
}
if (*p == '(')
{
r = compile_call(arg, p - *arg, cctx, ppconst, 0);
}
else
{
if (cctx->ctx_skip != SKIP_YES
&& generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
r = compile_load(arg, p, cctx, TRUE, TRUE);
}
if (r == FAIL)
return FAIL;
}
// Handle following "[]", ".member", etc.
// Then deal with prefixed '-', '+' and '!', if not done already.
if (compile_subscript(arg, cctx, start_leader, &end_leader,
ppconst) == FAIL)
return FAIL;
if (ppconst->pp_used > 0)
{
// apply the '!', '-' and '+' before the constant
rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL)
return FAIL;
return OK;
}
if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL)
return FAIL;
return OK;
}
/*
* Give the "white on both sides" error, taking the operator from "p[len]".
*/
void
error_white_both(char_u *op, int len)
{
char_u buf[10];
vim_strncpy(buf, op, len);
semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op);
}
/*
* <type>expr7: runtime type check / conversion
*/
static int
compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
type_T *want_type = NULL;
// Recognize <type>
if (**arg == '<' && eval_isnamec1((*arg)[1]))
{
++*arg;
want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
if (want_type == NULL)
return FAIL;
if (**arg != '>')
{
if (*skipwhite(*arg) == '>')
semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
else
emsg(_(e_missing_gt));
return FAIL;
}
++*arg;
if (may_get_next_line_error(*arg, arg, cctx) == FAIL)
return FAIL;
}
if (compile_expr7(arg, cctx, ppconst) == FAIL)
return FAIL;
if (want_type != NULL)
{
garray_T *stack = &cctx->ctx_type_stack;
type_T *actual;
where_T where = WHERE_INIT;
generate_ppconst(cctx, ppconst);
actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (check_type(want_type, actual, FALSE, where) == FAIL)
{
if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
}
}
return OK;
}
/*
* * number multiplication
* / number division
* % number modulo
*/
static int
compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
char_u *op;
char_u *next;
int ppconst_used = ppconst->pp_used;
// get the first expression
if (compile_expr7t(arg, cctx, ppconst) == FAIL)
return FAIL;
/*
* Repeat computing, until no "*", "/" or "%" is following.
*/
for (;;)
{
op = may_peek_next_line(cctx, *arg, &next);
if (*op != '*' && *op != '/' && *op != '%')
break;
if (next != NULL)
{
*arg = next_line_from_context(cctx, TRUE);
op = skipwhite(*arg);
}
if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
{
error_white_both(op, 1);
return FAIL;
}
if (may_get_next_line_error(op + 1, arg, cctx) == FAIL)
return FAIL;
// get the second expression
if (compile_expr7t(arg, cctx, ppconst) == FAIL)
return FAIL;
if (ppconst->pp_used == ppconst_used + 2
&& ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
&& ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
{
typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
varnumber_T res = 0;
int failed = FALSE;
// both are numbers: compute the result
switch (*op)
{
case '*': res = tv1->vval.v_number * tv2->vval.v_number;
break;
case '/': res = num_divide(tv1->vval.v_number,
tv2->vval.v_number, &failed);
break;
case '%': res = num_modulus(tv1->vval.v_number,
tv2->vval.v_number, &failed);
break;
}
if (failed)
return FAIL;
tv1->vval.v_number = res;
--ppconst->pp_used;
}
else
{
generate_ppconst(cctx, ppconst);
generate_two_op(cctx, op);
}
}
return OK;
}
/*
* + number addition or list/blobl concatenation
* - number subtraction
* .. string concatenation
*/
static int
compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
char_u *op;
char_u *next;
int oplen;
int ppconst_used = ppconst->pp_used;
// get the first variable
if (compile_expr6(arg, cctx, ppconst) == FAIL)
return FAIL;
/*
* Repeat computing, until no "+", "-" or ".." is following.
*/
for (;;)
{
op = may_peek_next_line(cctx, *arg, &next);
if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
break;
if (op[0] == op[1] && *op != '.' && next)
// Finding "++" or "--" on the next line is a separate command.
// But ".." is concatenation.
break;
oplen = (*op == '.' ? 2 : 1);
if (next != NULL)
{
*arg = next_line_from_context(cctx, TRUE);
op = skipwhite(*arg);
}
if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
{
error_white_both(op, oplen);
return FAIL;
}
if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL)
return FAIL;
// get the second expression
if (compile_expr6(arg, cctx, ppconst) == FAIL)
return FAIL;
if (ppconst->pp_used == ppconst_used + 2
&& (*op == '.'
? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
&& ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
: (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
&& ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
{
typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
// concat/subtract/add constant numbers
if (*op == '+')
tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
else if (*op == '-')
tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
else
{
// concatenate constant strings
char_u *s1 = tv1->vval.v_string;
char_u *s2 = tv2->vval.v_string;
size_t len1 = STRLEN(s1);
tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
if (tv1->vval.v_string == NULL)
{
clear_ppconst(ppconst);
return FAIL;
}
mch_memmove(tv1->vval.v_string, s1, len1);
STRCPY(tv1->vval.v_string + len1, s2);
vim_free(s1);
vim_free(s2);
}
--ppconst->pp_used;
}
else
{
generate_ppconst(cctx, ppconst);
ppconst->pp_is_const = FALSE;
if (*op == '.')
{
if (may_generate_2STRING(-2, FALSE, cctx) == FAIL
|| may_generate_2STRING(-1, FALSE, cctx) == FAIL)
return FAIL;
generate_instr_drop(cctx, ISN_CONCAT, 1);
}
else
generate_two_op(cctx, op);
}
}
return OK;
}
/*
* expr5a == expr5b
* expr5a =~ expr5b
* expr5a != expr5b
* expr5a !~ expr5b
* expr5a > expr5b
* expr5a >= expr5b
* expr5a < expr5b
* expr5a <= expr5b
* expr5a is expr5b
* expr5a isnot expr5b
*
* Produces instructions:
* EVAL expr5a Push result of "expr5a"
* EVAL expr5b Push result of "expr5b"
* COMPARE one of the compare instructions
*/
static int
compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
exprtype_T type = EXPR_UNKNOWN;
char_u *p;
char_u *next;
int len = 2;
int type_is = FALSE;
int ppconst_used = ppconst->pp_used;
// get the first variable
if (compile_expr5(arg, cctx, ppconst) == FAIL)
return FAIL;
p = may_peek_next_line(cctx, *arg, &next);
type = get_compare_type(p, &len, &type_is);
/*
* If there is a comparative operator, use it.
*/
if (type != EXPR_UNKNOWN)
{
int ic = FALSE; // Default: do not ignore case
if (next != NULL)
{
*arg = next_line_from_context(cctx, TRUE);
p = skipwhite(*arg);
}
if (type_is && (p[len] == '?' || p[len] == '#'))
{
semsg(_(e_invalid_expression_str), *arg);
return FAIL;
}
// extra question mark appended: ignore case
if (p[len] == '?')
{
ic = TRUE;
++len;
}
// extra '#' appended: match case (ignored)
else if (p[len] == '#')
++len;
// nothing appended: match case
if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
{
error_white_both(p, len);
return FAIL;
}
// get the second variable
if (may_get_next_line_error(p + len, arg, cctx) == FAIL)
return FAIL;
if (compile_expr5(arg, cctx, ppconst) == FAIL)
return FAIL;
if (ppconst->pp_used == ppconst_used + 2)
{
typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
int ret;
// Both sides are a constant, compute the result now.
// First check for a valid combination of types, this is more
// strict than typval_compare().
if (check_compare_types(type, tv1, tv2) == FAIL)
ret = FAIL;
else
{
ret = typval_compare(tv1, tv2, type, ic);
tv1->v_type = VAR_BOOL;
tv1->vval.v_number = tv1->vval.v_number
? VVAL_TRUE : VVAL_FALSE;
clear_tv(tv2);
--ppconst->pp_used;
}
return ret;
}
generate_ppconst(cctx, ppconst);
return generate_COMPARE(cctx, type, ic);
}
return OK;
}
static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
/*
* Compile || or &&.
*/
static int
compile_and_or(
char_u **arg,
cctx_T *cctx,
char *op,
ppconst_T *ppconst,
int ppconst_used UNUSED)
{
char_u *next;
char_u *p = may_peek_next_line(cctx, *arg, &next);
int opchar = *op;
if (p[0] == opchar && p[1] == opchar)
{
garray_T *instr = &cctx->ctx_instr;
garray_T end_ga;
int save_skip = cctx->ctx_skip;
/*
* Repeat until there is no following "||" or "&&"
*/
ga_init2(&end_ga, sizeof(int), 10);
while (p[0] == opchar && p[1] == opchar)
{
long start_lnum = SOURCING_LNUM;
long save_sourcing_lnum;
int start_ctx_lnum = cctx->ctx_lnum;
int save_lnum;
int const_used;
int status;
jumpwhen_T jump_when = opchar == '|'
? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE;
if (next != NULL)
{
*arg = next_line_from_context(cctx, TRUE);
p = skipwhite(*arg);
}
if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
{
semsg(_(e_white_space_required_before_and_after_str_at_str),
op, p);
ga_clear(&end_ga);
return FAIL;
}
save_sourcing_lnum = SOURCING_LNUM;
SOURCING_LNUM = start_lnum;
save_lnum = cctx->ctx_lnum;
cctx->ctx_lnum = start_ctx_lnum;
status = check_ppconst_bool(ppconst);
if (status != FAIL)
{
// Use the last ppconst if possible.
if (ppconst->pp_used > 0)
{
typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
int is_true = tv2bool(tv);
if ((is_true && opchar == '|')
|| (!is_true && opchar == '&'))
{
// For "false && expr" and "true || expr" the "expr"
// does not need to be evaluated.
cctx->ctx_skip = SKIP_YES;
clear_tv(tv);
tv->v_type = VAR_BOOL;
tv->vval.v_number = is_true ? VVAL_TRUE : VVAL_FALSE;
}
else
{
// For "true && expr" and "false || expr" only "expr"
// needs to be evaluated.
--ppconst->pp_used;
jump_when = JUMP_NEVER;
}
}
else
{
// Every part must evaluate to a bool.
status = bool_on_stack(cctx);
}
}
if (status != FAIL)
status = ga_grow(&end_ga, 1);
cctx->ctx_lnum = save_lnum;
if (status == FAIL)
{
ga_clear(&end_ga);
return FAIL;
}
if (jump_when != JUMP_NEVER)
{
if (cctx->ctx_skip != SKIP_YES)
{
*(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
++end_ga.ga_len;
}
generate_JUMP(cctx, jump_when, 0);
}
// eval the next expression
SOURCING_LNUM = save_sourcing_lnum;
if (may_get_next_line_error(p + 2, arg, cctx) == FAIL)
{
ga_clear(&end_ga);
return FAIL;
}
const_used = ppconst->pp_used;
if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
: compile_expr4(arg, cctx, ppconst)) == FAIL)
{
ga_clear(&end_ga);
return FAIL;
}
// "0 || 1" results in true, "1 && 0" results in false.
if (ppconst->pp_used == const_used + 1)
{
typval_T *tv = &ppconst->pp_tv[ppconst->pp_used - 1];
if (tv->v_type == VAR_NUMBER
&& (tv->vval.v_number == 1 || tv->vval.v_number == 0))
{
tv->vval.v_number = tv->vval.v_number == 1
? VVAL_TRUE : VVAL_FALSE;
tv->v_type = VAR_BOOL;
}
}
p = may_peek_next_line(cctx, *arg, &next);
}
if (check_ppconst_bool(ppconst) == FAIL)
{
ga_clear(&end_ga);
return FAIL;
}
if (cctx->ctx_skip != SKIP_YES && ppconst->pp_used == 0)
// Every part must evaluate to a bool.
if (bool_on_stack(cctx) == FAIL)
{
ga_clear(&end_ga);
return FAIL;
}
if (end_ga.ga_len > 0)
{
// Fill in the end label in all jumps.
generate_ppconst(cctx, ppconst);
while (end_ga.ga_len > 0)
{
isn_T *isn;
--end_ga.ga_len;
isn = ((isn_T *)instr->ga_data)
+ *(((int *)end_ga.ga_data) + end_ga.ga_len);
isn->isn_arg.jump.jump_where = instr->ga_len;
}
}
ga_clear(&end_ga);
cctx->ctx_skip = save_skip;
}
return OK;
}
/*
* expr4a && expr4a && expr4a logical AND
*
* Produces instructions:
* EVAL expr4a Push result of "expr4a"
* COND2BOOL convert to bool if needed
* JUMP_IF_COND_FALSE end
* EVAL expr4b Push result of "expr4b"
* JUMP_IF_COND_FALSE end
* EVAL expr4c Push result of "expr4c"
* end:
*/
static int
compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
int ppconst_used = ppconst->pp_used;
// get the first variable
if (compile_expr4(arg, cctx, ppconst) == FAIL)
return FAIL;
// || and && work almost the same
return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
}
/*
* expr3a || expr3b || expr3c logical OR
*
* Produces instructions:
* EVAL expr3a Push result of "expr3a"
* COND2BOOL convert to bool if needed
* JUMP_IF_COND_TRUE end
* EVAL expr3b Push result of "expr3b"
* JUMP_IF_COND_TRUE end
* EVAL expr3c Push result of "expr3c"
* end:
*/
static int
compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
int ppconst_used = ppconst->pp_used;
// eval the first expression
if (compile_expr3(arg, cctx, ppconst) == FAIL)
return FAIL;
// || and && work almost the same
return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
}
/*
* Toplevel expression: expr2 ? expr1a : expr1b
* Produces instructions:
* EVAL expr2 Push result of "expr2"
* JUMP_IF_FALSE alt jump if false
* EVAL expr1a
* JUMP_ALWAYS end
* alt: EVAL expr1b
* end:
*
* Toplevel expression: expr2 ?? expr1
* Produces instructions:
* EVAL expr2 Push result of "expr2"
* JUMP_AND_KEEP_IF_TRUE end jump if true
* EVAL expr1
* end:
*/
static int
compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
char_u *p;
int ppconst_used = ppconst->pp_used;
char_u *next;
// Ignore all kinds of errors when not producing code.
if (cctx->ctx_skip == SKIP_YES)
{
skip_expr_cctx(arg, cctx);
return OK;
}
// Evaluate the first expression.
if (compile_expr2(arg, cctx, ppconst) == FAIL)
return FAIL;
p = may_peek_next_line(cctx, *arg, &next);
if (*p == '?')
{
int op_falsy = p[1] == '?';
garray_T *instr = &cctx->ctx_instr;
garray_T *stack = &cctx->ctx_type_stack;
int alt_idx = instr->ga_len;
int end_idx = 0;
isn_T *isn;
type_T *type1 = NULL;
int has_const_expr = FALSE;
int const_value = FALSE;
int save_skip = cctx->ctx_skip;
if (next != NULL)
{
*arg = next_line_from_context(cctx, TRUE);
p = skipwhite(*arg);
}
if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy]))
{
semsg(_(e_white_space_required_before_and_after_str_at_str),
op_falsy ? "??" : "?", p);
return FAIL;
}
if (ppconst->pp_used == ppconst_used + 1)
{
// the condition is a constant, we know whether the ? or the :
// expression is to be evaluated.
has_const_expr = TRUE;
if (op_falsy)
const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
else
{
int error = FALSE;
const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used],
&error);
if (error)
return FAIL;
}
cctx->ctx_skip = save_skip == SKIP_YES ||
(op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT;
if (op_falsy && cctx->ctx_skip == SKIP_YES)
// "left ?? right" and "left" is truthy: produce "left"
generate_ppconst(cctx, ppconst);
else
{
clear_tv(&ppconst->pp_tv[ppconst_used]);
--ppconst->pp_used;
}
}
else
{
generate_ppconst(cctx, ppconst);
if (op_falsy)
end_idx = instr->ga_len;
generate_JUMP(cctx, op_falsy
? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0);
if (op_falsy)
type1 = ((type_T **)stack->ga_data)[stack->ga_len];
}
// evaluate the second expression; any type is accepted
if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL)
return FAIL;
if (compile_expr1(arg, cctx, ppconst) == FAIL)
return FAIL;
if (!has_const_expr)
{
generate_ppconst(cctx, ppconst);
if (!op_falsy)
{
// remember the type and drop it
--stack->ga_len;
type1 = ((type_T **)stack->ga_data)[stack->ga_len];
end_idx = instr->ga_len;
generate_JUMP(cctx, JUMP_ALWAYS, 0);
// jump here from JUMP_IF_FALSE
isn = ((isn_T *)instr->ga_data) + alt_idx;
isn->isn_arg.jump.jump_where = instr->ga_len;
}
}
if (!op_falsy)
{
// Check for the ":".
p = may_peek_next_line(cctx, *arg, &next);
if (*p != ':')
{
emsg(_(e_missing_colon));
return FAIL;
}
if (next != NULL)
{
*arg = next_line_from_context(cctx, TRUE);
p = skipwhite(*arg);
}
if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
{
semsg(_(e_white_space_required_before_and_after_str_at_str),
":", p);
return FAIL;
}
// evaluate the third expression
if (has_const_expr)
cctx->ctx_skip = save_skip == SKIP_YES || const_value
? SKIP_YES : SKIP_NOT;
if (may_get_next_line_error(p + 1, arg, cctx) == FAIL)
return FAIL;
if (compile_expr1(arg, cctx, ppconst) == FAIL)
return FAIL;
}
if (!has_const_expr)
{
type_T **typep;
generate_ppconst(cctx, ppconst);
// If the types differ, the result has a more generic type.
typep = ((type_T **)stack->ga_data) + stack->ga_len - 1;
common_type(type1, *typep, typep, cctx->ctx_type_list);
// jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE
isn = ((isn_T *)instr->ga_data) + end_idx;
isn->isn_arg.jump.jump_where = instr->ga_len;
}
cctx->ctx_skip = save_skip;
}
return OK;
}
/*
* Toplevel expression.
* Sets "is_const" (if not NULL) to indicate the value is a constant.
* Returns OK or FAIL.
*/
static int
compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const)
{
ppconst_T ppconst;
CLEAR_FIELD(ppconst);
if (compile_expr1(arg, cctx, &ppconst) == FAIL)
{
clear_ppconst(&ppconst);
return FAIL;
}
if (is_const != NULL)
*is_const = ppconst.pp_used > 0 || ppconst.pp_is_const;
if (generate_ppconst(cctx, &ppconst) == FAIL)
return FAIL;
return OK;
}
/*
* Toplevel expression.
*/
static int
compile_expr0(char_u **arg, cctx_T *cctx)
{
return compile_expr0_ext(arg, cctx, NULL);
}
/*
* Compile "return [expr]".
* When "legacy" is TRUE evaluate [expr] with legacy syntax
*/
static char_u *
compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
{
char_u *p = arg;
garray_T *stack = &cctx->ctx_type_stack;
type_T *stack_type;
if (*p != NUL && *p != '|' && *p != '\n')
{
if (legacy)
{
int save_flags = cmdmod.cmod_flags;
generate_LEGACY_EVAL(cctx, p);
if (need_type(&t_any, cctx->ctx_ufunc->uf_ret_type, -1,
0, cctx, FALSE, FALSE) == FAIL)
return NULL;
cmdmod.cmod_flags |= CMOD_LEGACY;
(void)skip_expr(&p, NULL);
cmdmod.cmod_flags = save_flags;
}
else
{
// compile return argument into instructions
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
}
if (cctx->ctx_skip != SKIP_YES)
{
// "check_return_type" with uf_ret_type set to &t_unknown is used
// for an inline function without a specified return type. Set the
// return type here.
stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
|| cctx->ctx_ufunc->uf_ret_type == &t_unknown
|| cctx->ctx_ufunc->uf_ret_type == &t_any))
|| (!check_return_type
&& cctx->ctx_ufunc->uf_ret_type == &t_unknown))
{
cctx->ctx_ufunc->uf_ret_type = stack_type;
}
else
{
if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
&& stack_type->tt_type != VAR_VOID
&& stack_type->tt_type != VAR_UNKNOWN)
{
emsg(_(e_returning_value_in_function_without_return_type));
return NULL;
}
if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
0, cctx, FALSE, FALSE) == FAIL)
return NULL;
}
}
}
else
{
// "check_return_type" cannot be TRUE, only used for a lambda which
// always has an argument.
if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
&& cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
{
emsg(_(e_missing_return_value));
return NULL;
}
// No argument, return zero.
generate_PUSHNR(cctx, 0);
}
// Undo any command modifiers.
generate_undo_cmdmods(cctx);
if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
return NULL;
// "return val | endif" is possible
return skipwhite(p);
}
/*
* Get a line from the compilation context, compatible with exarg_T getline().
* Return a pointer to the line in allocated memory.
* Return NULL for end-of-file or some error.
*/
static char_u *
exarg_getline(
int c UNUSED,
void *cookie,
int indent UNUSED,
getline_opt_T options UNUSED)
{
cctx_T *cctx = (cctx_T *)cookie;
char_u *p;
for (;;)
{
if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
return NULL;
++cctx->ctx_lnum;
p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
// Comment lines result in NULL pointers, skip them.
if (p != NULL)
return vim_strsave(p);
}
}
void
fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
{
eap->getline = exarg_getline;
eap->cookie = cctx;
}
/*
* Compile a nested :def command.
*/
static char_u *
compile_nested_function(exarg_T *eap, cctx_T *cctx)
{
int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
char_u *name_start = eap->arg;
char_u *name_end = to_name_end(eap->arg, TRUE);
char_u *lambda_name;
ufunc_T *ufunc;
int r = FAIL;
compiletype_T compile_type;
if (eap->forceit)
{
emsg(_(e_cannot_use_bang_with_nested_def));
return NULL;
}
if (*name_start == '/')
{
name_end = skip_regexp(name_start + 1, '/', TRUE);
if (*name_end == '/')
++name_end;
set_nextcmd(eap, name_end);
}
if (name_end == name_start || *skipwhite(name_end) != '(')
{
if (!ends_excmd2(name_start, name_end))
{
semsg(_(e_invalid_command_str), eap->cmd);
return NULL;
}
// "def" or "def Name": list functions
if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
return NULL;
return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
}
// Only g:Func() can use a namespace.
if (name_start[1] == ':' && !is_global)
{
semsg(_(e_namespace_not_supported_str), name_start);
return NULL;
}
if (check_defined(name_start, name_end - name_start, cctx, FALSE) == FAIL)
return NULL;
eap->arg = name_end;
fill_exarg_from_cctx(eap, cctx);
eap->forceit = FALSE;
// We use the special <Lamba>99 name, but it's not really a lambda.
lambda_name = vim_strsave(get_lambda_name());
if (lambda_name == NULL)
return NULL;
ufunc = define_function(eap, lambda_name);
if (ufunc == NULL)
{
r = eap->skip ? OK : FAIL;
goto theend;
}
// copy over the block scope IDs before compiling
if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
{
int block_depth = cctx->ctx_ufunc->uf_block_depth;
ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
if (ufunc->uf_block_ids != NULL)
{
mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
sizeof(int) * block_depth);
ufunc->uf_block_depth = block_depth;
}
}
compile_type = COMPILE_TYPE(ufunc);
#ifdef FEAT_PROFILE
// If the outer function is profiled, also compile the nested function for
// profiling.
if (cctx->ctx_compile_type == CT_PROFILE)
compile_type = CT_PROFILE;
#endif
if (func_needs_compiling(ufunc, compile_type)
&& compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
{
func_ptr_unref(ufunc);
goto theend;
}
#ifdef FEAT_PROFILE
// When the outer function is compiled for profiling, the nested function
// may be called without profiling. Compile it here in the right context.
if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
compile_def_function(ufunc, FALSE, CT_NONE, cctx);
#endif
if (is_global)
{
char_u *func_name = vim_strnsave(name_start + 2,
name_end - name_start - 2);
if (func_name == NULL)
r = FAIL;
else
{
r = generate_NEWFUNC(cctx, lambda_name, func_name);
lambda_name = NULL;
}
}
else
{
// Define a local variable for the function reference.
lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start,
TRUE, ufunc->uf_func_type);
if (lvar == NULL)
goto theend;
if (generate_FUNCREF(cctx, ufunc) == FAIL)
goto theend;
r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
}
theend:
vim_free(lambda_name);
return r == FAIL ? NULL : (char_u *)"";
}
/*
* Return the length of an assignment operator, or zero if there isn't one.
*/
int
assignment_len(char_u *p, int *heredoc)
{
if (*p == '=')
{
if (p[1] == '<' && p[2] == '<')
{
*heredoc = TRUE;
return 3;
}
return 1;
}
if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
return 2;
if (STRNCMP(p, "..=", 3) == 0)
return 3;
return 0;
}
/*
* Generate the load instruction for "name".
*/
static void
generate_loadvar(
cctx_T *cctx,
assign_dest_T dest,
char_u *name,
lvar_T *lvar,
type_T *type)
{
switch (dest)
{
case dest_option:
case dest_func_option:
generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
break;
case dest_global:
if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
else
generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
break;
case dest_buffer:
generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
break;
case dest_window:
generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
break;
case dest_tab:
generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
break;
case dest_script:
compile_load_scriptvar(cctx,
name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
break;
case dest_env:
// Include $ in the name here
generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
break;
case dest_reg:
generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
break;
case dest_vimvar:
generate_LOADV(cctx, name + 2, TRUE);
break;
case dest_local:
if (lvar->lv_from_outer > 0)
generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
type);
else
generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
break;
case dest_expr:
// list or dict value should already be on the stack.
break;
}
}
/*
* Skip over "[expr]" or ".member".
* Does not check for any errors.
*/
static char_u *
skip_index(char_u *start)
{
char_u *p = start;
if (*p == '[')
{
p = skipwhite(p + 1);
(void)skip_expr(&p, NULL);
p = skipwhite(p);
if (*p == ']')
return p + 1;
return p;
}
// if (*p == '.')
return to_name_end(p + 1, TRUE);
}
void
vim9_declare_error(char_u *name)
{
char *scope = "";
switch (*name)
{
case 'g': scope = _("global"); break;
case 'b': scope = _("buffer"); break;
case 'w': scope = _("window"); break;
case 't': scope = _("tab"); break;
case 'v': scope = "v:"; break;
case '$': semsg(_(e_cannot_declare_an_environment_variable), name);
return;
case '&': semsg(_(e_cannot_declare_an_option), name);
return;
case '@': semsg(_(e_cannot_declare_a_register_str), name);
return;
default: return;
}
semsg(_(e_cannot_declare_a_scope_variable), scope, name);
}
/*
* For one assignment figure out the type of destination. Return it in "dest".
* When not recognized "dest" is not set.
* For an option "option_scope" is set.
* For a v:var "vimvaridx" is set.
* "type" is set to the destination type if known, unchanted otherwise.
* Return FAIL if an error message was given.
*/
static int
get_var_dest(
char_u *name,
assign_dest_T *dest,
int cmdidx,
int *option_scope,
int *vimvaridx,
type_T **type,
cctx_T *cctx)
{
char_u *p;
if (*name == '&')
{
int cc;
long numval;
getoption_T opt_type;
int opt_p_flags;
*dest = dest_option;
if (cmdidx == CMD_final || cmdidx == CMD_const)
{
emsg(_(e_const_option));
return FAIL;
}
p = name;
p = find_option_end(&p, option_scope);
if (p == NULL)
{
// cannot happen?
emsg(_(e_unexpected_characters_in_assignment));
return FAIL;
}
cc = *p;
*p = NUL;
opt_type = get_option_value(skip_option_env_lead(name),
&numval, NULL, &opt_p_flags, *option_scope);
*p = cc;
switch (opt_type)
{
case gov_unknown:
semsg(_(e_unknown_option), name);
return FAIL;
case gov_string:
case gov_hidden_string:
if (opt_p_flags & P_FUNC)
{
// might be a Funcref, check the type later
*type = &t_any;
*dest = dest_func_option;
}
else
{
*type = &t_string;
}
break;
case gov_bool:
case gov_hidden_bool:
*type = &t_bool;
break;
case gov_number:
case gov_hidden_number:
*type = &t_number;
break;
}
}
else if (*name == '$')
{
*dest = dest_env;
*type = &t_string;
}
else if (*name == '@')
{
if (name[1] != '@'
&& (!valid_yank_reg(name[1], FALSE) || name[1] == '.'))
{
emsg_invreg(name[1]);
return FAIL;
}
*dest = dest_reg;
*type = name[1] == '#' ? &t_number_or_string : &t_string;
}
else if (STRNCMP(name, "g:", 2) == 0)
{
*dest = dest_global;
}
else if (STRNCMP(name, "b:", 2) == 0)
{
*dest = dest_buffer;
}
else if (STRNCMP(name, "w:", 2) == 0)
{
*dest = dest_window;
}
else if (STRNCMP(name, "t:", 2) == 0)
{
*dest = dest_tab;
}
else if (STRNCMP(name, "v:", 2) == 0)
{
typval_T *vtv;
int di_flags;
*vimvaridx = find_vim_var(name + 2, &di_flags);
if (*vimvaridx < 0)
{
semsg(_(e_variable_not_found_str), name);
return FAIL;
}
// We use the current value of "sandbox" here, is that OK?
if (var_check_ro(di_flags, name, FALSE))
return FAIL;
*dest = dest_vimvar;
vtv = get_vim_var_tv(*vimvaridx);
*type = typval2type_vimvar(vtv, cctx->ctx_type_list);
}
return OK;
}
/*
* Generate a STORE instruction for "dest", not being "dest_local".
* Return FAIL when out of memory.
*/
static int
generate_store_var(
cctx_T *cctx,
assign_dest_T dest,
int opt_flags,
int vimvaridx,
int scriptvar_idx,
int scriptvar_sid,
type_T *type,
char_u *name)
{
switch (dest)
{
case dest_option:
return generate_STOREOPT(cctx, ISN_STOREOPT,
skip_option_env_lead(name), opt_flags);
case dest_func_option:
return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
skip_option_env_lead(name), opt_flags);
case dest_global:
// include g: with the name, easier to execute that way
return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
? ISN_STOREG : ISN_STOREAUTO, 0, name);
case dest_buffer:
// include b: with the name, easier to execute that way
return generate_STORE(cctx, ISN_STOREB, 0, name);
case dest_window:
// include w: with the name, easier to execute that way
return generate_STORE(cctx, ISN_STOREW, 0, name);
case dest_tab:
// include t: with the name, easier to execute that way
return generate_STORE(cctx, ISN_STORET, 0, name);
case dest_env:
return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
case dest_reg:
return generate_STORE(cctx, ISN_STOREREG,
name[1] == '@' ? '"' : name[1], NULL);
case dest_vimvar:
return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
case dest_script:
if (scriptvar_idx < 0)
// "s:" may be included in the name.
return generate_OLDSCRIPT(cctx, ISN_STORES, name,
scriptvar_sid, type);
return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
scriptvar_sid, scriptvar_idx, type);
case dest_local:
case dest_expr:
// cannot happen
break;
}
return FAIL;
}
static int
generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
{
if (lhs->lhs_dest != dest_local)
return generate_store_var(cctx, lhs->lhs_dest,
lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
lhs->lhs_type, lhs->lhs_name);
if (lhs->lhs_lvar != NULL)
{
garray_T *instr = &cctx->ctx_instr;
isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
// optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
// ISN_STORENR
if (lhs->lhs_lvar->lv_from_outer == 0
&& instr->ga_len == instr_count + 1
&& isn->isn_type == ISN_PUSHNR)
{
varnumber_T val = isn->isn_arg.number;
garray_T *stack = &cctx->ctx_type_stack;
isn->isn_type = ISN_STORENR;
isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
isn->isn_arg.storenr.stnr_val = val;
if (stack->ga_len > 0)
--stack->ga_len;
}
else if (lhs->lhs_lvar->lv_from_outer > 0)
generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
lhs->lhs_lvar->lv_from_outer);
else
generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
}
return OK;
}
static int
is_decl_command(int cmdidx)
{
return cmdidx == CMD_let || cmdidx == CMD_var
|| cmdidx == CMD_final || cmdidx == CMD_const;
}
/*
* Figure out the LHS type and other properties for an assignment or one item
* of ":unlet" with an index.
* Returns OK or FAIL.
*/
static int
compile_lhs(
char_u *var_start,
lhs_T *lhs,
int cmdidx,
int heredoc,
int oplen,
cctx_T *cctx)
{
char_u *var_end;
int is_decl = is_decl_command(cmdidx);
CLEAR_POINTER(lhs);
lhs->lhs_dest = dest_local;
lhs->lhs_vimvaridx = -1;
lhs->lhs_scriptvar_idx = -1;
// "dest_end" is the end of the destination, including "[expr]" or
// ".name".
// "var_end" is the end of the variable/option/etc. name.
lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
if (*var_start == '@')
var_end = var_start + 2;
else
{
// skip over the leading "&", "&l:", "&g:" and "$"
var_end = skip_option_env_lead(var_start);
var_end = to_name_end(var_end, TRUE);
}
// "a: type" is declaring variable "a" with a type, not dict "a:".
if (is_decl && lhs->lhs_dest_end == var_start + 2
&& lhs->lhs_dest_end[-1] == ':')
--lhs->lhs_dest_end;
if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
--var_end;
lhs->lhs_end = lhs->lhs_dest_end;
// compute the length of the destination without "[expr]" or ".name"
lhs->lhs_varlen = var_end - var_start;
lhs->lhs_varlen_total = lhs->lhs_varlen;
lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
if (lhs->lhs_name == NULL)
return FAIL;
if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
// Something follows after the variable: "var[idx]" or "var.key".
lhs->lhs_has_index = TRUE;
if (heredoc)
lhs->lhs_type = &t_list_string;
else
lhs->lhs_type = &t_any;
if (cctx->ctx_skip != SKIP_YES)
{
int declare_error = FALSE;
if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
&lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
&lhs->lhs_type, cctx) == FAIL)
return FAIL;
if (lhs->lhs_dest != dest_local
&& cmdidx != CMD_const && cmdidx != CMD_final)
{
// Specific kind of variable recognized.
declare_error = is_decl;
}
else
{
// No specific kind of variable recognized, just a name.
if (check_reserved_name(lhs->lhs_name) == FAIL)
return FAIL;
if (lookup_local(var_start, lhs->lhs_varlen,
&lhs->lhs_local_lvar, cctx) == OK)
lhs->lhs_lvar = &lhs->lhs_local_lvar;
else
{
CLEAR_FIELD(lhs->lhs_arg_lvar);
if (arg_exists(var_start, lhs->lhs_varlen,
&lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
&lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
{
if (is_decl)
{
semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
return FAIL;
}
lhs->lhs_lvar = &lhs->lhs_arg_lvar;
}
}
if (lhs->lhs_lvar != NULL)
{
if (is_decl)
{
semsg(_(e_variable_already_declared), lhs->lhs_name);
return FAIL;
}
}
else
{
int script_namespace = lhs->lhs_varlen > 1
&& STRNCMP(var_start, "s:", 2) == 0;
int script_var = (script_namespace
? script_var_exists(var_start + 2, lhs->lhs_varlen - 2,
cctx)
: script_var_exists(var_start, lhs->lhs_varlen,
cctx)) == OK;
imported_T *import =
find_imported(var_start, lhs->lhs_varlen, cctx);
if (script_namespace || script_var || import != NULL)
{
char_u *rawname = lhs->lhs_name
+ (lhs->lhs_name[1] == ':' ? 2 : 0);
if (is_decl)
{
if (script_namespace)
semsg(_(e_cannot_declare_script_variable_in_function),
lhs->lhs_name);
else
semsg(_(e_variable_already_declared_in_script_str),
lhs->lhs_name);
return FAIL;
}
else if (cctx->ctx_ufunc->uf_script_ctx_version
== SCRIPT_VERSION_VIM9
&& script_namespace
&& !script_var && import == NULL)
{
semsg(_(e_unknown_variable_str), lhs->lhs_name);
return FAIL;
}
lhs->lhs_dest = dest_script;
// existing script-local variables should have a type
lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
if (import != NULL)
lhs->lhs_scriptvar_sid = import->imp_sid;
if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
{
// Check writable only when no index follows.
lhs->lhs_scriptvar_idx = get_script_item_idx(
lhs->lhs_scriptvar_sid, rawname,
lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST,
cctx);
if (lhs->lhs_scriptvar_idx >= 0)
{
scriptitem_T *si = SCRIPT_ITEM(
lhs->lhs_scriptvar_sid);
svar_T *sv =
((svar_T *)si->sn_var_vals.ga_data)
+ lhs->lhs_scriptvar_idx;
lhs->lhs_type = sv->sv_type;
}
}
}
else if (check_defined(var_start, lhs->lhs_varlen, cctx, FALSE)
== FAIL)
return FAIL;
}
}
if (declare_error)
{
vim9_declare_error(lhs->lhs_name);
return FAIL;
}
}
// handle "a:name" as a name, not index "name" in "a"
if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
var_end = lhs->lhs_dest_end;
if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
{
if (is_decl && *var_end == ':')
{
char_u *p;
// parse optional type: "let var: type = expr"
if (!VIM_ISWHITE(var_end[1]))
{
semsg(_(e_white_space_required_after_str_str), ":", var_end);
return FAIL;
}
p = skipwhite(var_end + 1);
lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
if (lhs->lhs_type == NULL)
return FAIL;
lhs->lhs_has_type = TRUE;
lhs->lhs_end = p;
}
else if (lhs->lhs_lvar != NULL)
lhs->lhs_type = lhs->lhs_lvar->lv_type;
}
if (oplen == 3 && !heredoc
&& lhs->lhs_dest != dest_global
&& !lhs->lhs_has_index
&& lhs->lhs_type->tt_type != VAR_STRING
&& lhs->lhs_type->tt_type != VAR_ANY)
{
emsg(_(e_can_only_concatenate_to_string));
return FAIL;
}
if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
&& cctx->ctx_skip != SKIP_YES)
{
if (oplen > 1 && !heredoc)
{
// +=, /=, etc. require an existing variable
semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name);
return FAIL;
}
if (!is_decl)
{
semsg(_(e_unknown_variable_str), lhs->lhs_name);
return FAIL;
}
// Check the name is valid for a funcref.
if ((lhs->lhs_type->tt_type == VAR_FUNC
|| lhs->lhs_type->tt_type == VAR_PARTIAL)
&& var_wrong_func_name(lhs->lhs_name, TRUE))
return FAIL;
// New local variable.
lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen,
cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type);
if (lhs->lhs_lvar == NULL)
return FAIL;
lhs->lhs_new_local = TRUE;
}
lhs->lhs_member_type = lhs->lhs_type;
if (lhs->lhs_has_index)
{
char_u *after = var_start + lhs->lhs_varlen;
char_u *p;
// Something follows after the variable: "var[idx]" or "var.key".
if (is_decl)
{
emsg(_(e_cannot_use_index_when_declaring_variable));
return FAIL;
}
// Now: var_start[lhs->lhs_varlen] is '[' or '.'
// Only the last index is used below, if there are others
// before it generate code for the expression. Thus for
// "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
for (;;)
{
p = skip_index(after);
if (*p != '[' && *p != '.')
{
lhs->lhs_varlen_total = p - var_start;
break;
}
after = p;
}
if (after > var_start + lhs->lhs_varlen)
{
lhs->lhs_varlen = after - var_start;
lhs->lhs_dest = dest_expr;
// We don't know the type before evaluating the expression,
// use "any" until then.
lhs->lhs_type = &t_any;
}
if (lhs->lhs_type->tt_member == NULL)
lhs->lhs_member_type = &t_any;
else
lhs->lhs_member_type = lhs->lhs_type->tt_member;
}
return OK;
}
/*
* Figure out the LHS and check a few errors.
*/
static int
compile_assign_lhs(
char_u *var_start,
lhs_T *lhs,
int cmdidx,
int is_decl,
int heredoc,
int oplen,
cctx_T *cctx)
{
if (compile_lhs(var_start, lhs, cmdidx, heredoc, oplen, cctx) == FAIL)
return FAIL;
if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
{
semsg(_(e_cannot_assign_to_argument), lhs->lhs_name);
return FAIL;
}
if (!is_decl && lhs->lhs_lvar != NULL
&& lhs->lhs_lvar->lv_const && !lhs->lhs_has_index)
{
semsg(_(e_cannot_assign_to_constant), lhs->lhs_name);
return FAIL;
}
return OK;
}
/*
* Return TRUE if "lhs" has a range index: "[expr : expr]".
*/
static int
has_list_index(char_u *idx_start, cctx_T *cctx)
{
char_u *p = idx_start;
int save_skip;
if (*p != '[')
return FALSE;
p = skipwhite(p + 1);
if (*p == ':')
return TRUE;
save_skip = cctx->ctx_skip;
cctx->ctx_skip = SKIP_YES;
(void)compile_expr0(&p, cctx);
cctx->ctx_skip = save_skip;
return *skipwhite(p) == ':';
}
/*
* For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
* "var.key".
*/
static int
compile_assign_index(
char_u *var_start,
lhs_T *lhs,
int *range,
cctx_T *cctx)
{
size_t varlen = lhs->lhs_varlen;
char_u *p;
int r = OK;
int need_white_before = TRUE;
int empty_second;
p = var_start + varlen;
if (*p == '[')
{
p = skipwhite(p + 1);
if (*p == ':')
{
// empty first index, push zero
r = generate_PUSHNR(cctx, 0);
need_white_before = FALSE;
}
else
r = compile_expr0(&p, cctx);
if (r == OK && *skipwhite(p) == ':')
{
// unlet var[idx : idx]
// blob[idx : idx] = value
*range = TRUE;
p = skipwhite(p);
empty_second = *skipwhite(p + 1) == ']';
if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
|| (!empty_second && !IS_WHITE_OR_NUL(p[1])))
{
semsg(_(e_white_space_required_before_and_after_str_at_str),
":", p);
return FAIL;
}
p = skipwhite(p + 1);
if (*p == ']')
// empty second index, push "none"
r = generate_PUSHSPEC(cctx, VVAL_NONE);
else
r = compile_expr0(&p, cctx);
}
if (r == OK && *skipwhite(p) != ']')
{
// this should not happen
emsg(_(e_missbrac));
r = FAIL;
}
}
else // if (*p == '.')
{
char_u *key_end = to_name_end(p + 1, TRUE);
char_u *key = vim_strnsave(p + 1, key_end - p - 1);
r = generate_PUSHS(cctx, &key);
}
return r;
}
/*
* For a LHS with an index, load the variable to be indexed.
*/
static int
compile_load_lhs(
lhs_T *lhs,
char_u *var_start,
type_T *rhs_type,
cctx_T *cctx)
{
if (lhs->lhs_dest == dest_expr)
{
size_t varlen = lhs->lhs_varlen;
int c = var_start[varlen];
int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
char_u *p = var_start;
garray_T *stack = &cctx->ctx_type_stack;
int res;
// Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
// limit the lines array length to avoid skipping to a following line.
var_start[varlen] = NUL;
cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
res = compile_expr0(&p, cctx);
var_start[varlen] = c;
cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
if (res == FAIL || p != var_start + varlen)
{
// this should not happen
if (res != FAIL)
emsg(_(e_missbrac));
return FAIL;
}
lhs->lhs_type = stack->ga_len == 0 ? &t_void
: ((type_T **)stack->ga_data)[stack->ga_len - 1];
// now we can properly check the type
if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
&& rhs_type != &t_void
&& need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
FALSE, FALSE) == FAIL)
return FAIL;
}
else
generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name,
lhs->lhs_lvar, lhs->lhs_type);
return OK;
}
/*
* Produce code for loading "lhs" and also take care of an index.
* Return OK/FAIL.
*/
static int
compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
{
compile_load_lhs(lhs, var_start, NULL, cctx);
if (lhs->lhs_has_index)
{
int range = FALSE;
// Get member from list or dict. First compile the
// index value.
if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
return FAIL;
if (range)
{
semsg(_(e_cannot_use_range_with_assignment_operator_str),
var_start);
return FAIL;
}
// Get the member.
if (compile_member(FALSE, NULL, cctx) == FAIL)
return FAIL;
}
return OK;
}
/*
* Assignment to a list or dict member, or ":unlet" for the item, using the
* information in "lhs".
* Returns OK or FAIL.
*/
static int
compile_assign_unlet(
char_u *var_start,
lhs_T *lhs,
int is_assign,
type_T *rhs_type,
cctx_T *cctx)
{
vartype_T dest_type;
garray_T *stack = &cctx->ctx_type_stack;
int range = FALSE;
if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
return FAIL;
if (is_assign && range
&& lhs->lhs_type->tt_type != VAR_LIST
&& lhs->lhs_type != &t_blob
&& lhs->lhs_type != &t_any)
{
semsg(_(e_cannot_use_range_with_assignment_str), var_start);
return FAIL;
}
if (lhs->lhs_type == &t_any)
{
// Index on variable of unknown type: check at runtime.
dest_type = VAR_ANY;
}
else
{
dest_type = lhs->lhs_type->tt_type;
if (dest_type == VAR_DICT && range)
{
emsg(e_cannot_use_range_with_dictionary);
return FAIL;
}
if (dest_type == VAR_DICT
&& may_generate_2STRING(-1, FALSE, cctx) == FAIL)
return FAIL;
if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
{
type_T *type;
if (range)
{
type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
if (need_type(type, &t_number,
-1, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
}
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if ((dest_type != VAR_BLOB && type != &t_special)
&& need_type(type, &t_number,
-1, 0, cctx, FALSE, FALSE) == FAIL)
return FAIL;
}
}
// Load the dict or list. On the stack we then have:
// - value (for assignment, not for :unlet)
// - index
// - for [a : b] second index
// - variable
if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
return FAIL;
if (dest_type == VAR_LIST || dest_type == VAR_DICT
|| dest_type == VAR_BLOB || dest_type == VAR_ANY)
{
if (is_assign)
{
if (range)
{
if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
return FAIL;
}
else
{
isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
if (isn == NULL)
return FAIL;
isn->isn_arg.vartype = dest_type;
}
}
else if (range)
{
if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
return FAIL;
}
else
{
if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
return FAIL;
}
}
else
{
emsg(_(e_indexable_type_required));
return FAIL;
}
return OK;
}
/*
* Compile declaration and assignment:
* "let name"
* "var name = expr"
* "final name = expr"
* "const name = expr"
* "name = expr"
* "arg" points to "name".
* "++arg" and "--arg"
* Return NULL for an error.
* Return "arg" if it does not look like a variable list.
*/
static char_u *
compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
{
char_u *var_start;
char_u *p;
char_u *end = arg;
char_u *ret = NULL;
int var_count = 0;
int var_idx;
int semicolon = 0;
int did_generate_slice = FALSE;
garray_T *instr = &cctx->ctx_instr;
garray_T *stack = &cctx->ctx_type_stack;
char_u *op;
int oplen = 0;
int heredoc = FALSE;
int incdec = FALSE;
type_T *rhs_type = &t_any;
char_u *sp;
int is_decl = is_decl_command(cmdidx);
lhs_T lhs;
long start_lnum = SOURCING_LNUM;
// Skip over the "var" or "[var, var]" to get to any "=".
p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
if (p == NULL)
return *arg == '[' ? arg : NULL;
lhs.lhs_name = NULL;
sp = p;
p = skipwhite(p);
op = p;
oplen = assignment_len(p, &heredoc);
if (var_count > 0 && oplen == 0)
// can be something like "[1, 2]->func()"
return arg;
if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen])))
{
error_white_both(op, oplen);
return NULL;
}
if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
{
if (VIM_ISWHITE(eap->cmd[2]))
{
semsg(_(e_no_white_space_allowed_after_str_str),
eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
return NULL;
}
op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
oplen = 2;
incdec = TRUE;
}
if (heredoc)
{
list_T *l;
listitem_T *li;
// [let] varname =<< [trim] {end}
eap->getline = exarg_getline;
eap->cookie = cctx;
l = heredoc_get(eap, op + 3, FALSE);
if (l == NULL)
return NULL;
if (cctx->ctx_skip != SKIP_YES)
{
// Push each line and the create the list.
FOR_ALL_LIST_ITEMS(l, li)
{
generate_PUSHS(cctx, &li->li_tv.vval.v_string);
li->li_tv.vval.v_string = NULL;
}
generate_NEWLIST(cctx, l->lv_len);
}
list_free(l);
p += STRLEN(p);
end = p;
}
else if (var_count > 0)
{
char_u *wp;
// for "[var, var] = expr" evaluate the expression here, loop over the
// list of variables below.
// A line break may follow the "=".
wp = op + oplen;
if (may_get_next_line_error(wp, &p, cctx) == FAIL)
return FAIL;
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
end = p;
if (cctx->ctx_skip != SKIP_YES)
{
type_T *stacktype;
int needed_list_len;
int did_check = FALSE;
stacktype = stack->ga_len == 0 ? &t_void
: ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (stacktype->tt_type == VAR_VOID)
{
emsg(_(e_cannot_use_void_value));
goto theend;
}
if (need_type(stacktype, &t_list_any, -1, 0, cctx,
FALSE, FALSE) == FAIL)
goto theend;
// If a constant list was used we can check the length right here.
needed_list_len = semicolon ? var_count - 1 : var_count;
if (instr->ga_len > 0)
{
isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
if (isn->isn_type == ISN_NEWLIST)
{
did_check = TRUE;
if (semicolon ? isn->isn_arg.number < needed_list_len
: isn->isn_arg.number != needed_list_len)
{
semsg(_(e_expected_nr_items_but_got_nr),
needed_list_len, isn->isn_arg.number);
goto theend;
}
}
}
if (!did_check)
generate_CHECKLEN(cctx, needed_list_len, semicolon);
if (stacktype->tt_member != NULL)
rhs_type = stacktype->tt_member;
}
}
/*
* Loop over variables in "[var, var] = expr".
* For "var = expr" and "let var: type" this is done only once.
*/
if (var_count > 0)
var_start = skipwhite(arg + 1); // skip over the "["
else
var_start = arg;
for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
{
int instr_count = -1;
int save_lnum;
if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
{
// Ignore underscore in "[a, _, b] = list".
if (var_count > 0)
{
var_start = skipwhite(var_start + 2);
continue;
}
emsg(_(e_cannot_use_underscore_here));
goto theend;
}
vim_free(lhs.lhs_name);
/*
* Figure out the LHS type and other properties.
*/
if (compile_assign_lhs(var_start, &lhs, cmdidx,
is_decl, heredoc, oplen, cctx) == FAIL)
goto theend;
if (heredoc)
{
SOURCING_LNUM = start_lnum;
if (lhs.lhs_has_type
&& need_type(&t_list_string, lhs.lhs_type,
-1, 0, cctx, FALSE, FALSE) == FAIL)
goto theend;
}
else
{
if (cctx->ctx_skip == SKIP_YES)
{
if (oplen > 0 && var_count == 0)
{
// skip over the "=" and the expression
p = skipwhite(op + oplen);
(void)compile_expr0(&p, cctx);
}
}
else if (oplen > 0)
{
int is_const = FALSE;
char_u *wp;
// for "+=", "*=", "..=" etc. first load the current value
if (*op != '='
&& compile_load_lhs_with_index(&lhs, var_start,
cctx) == FAIL)
goto theend;
// For "var = expr" evaluate the expression.
if (var_count == 0)
{
int r;
// Compile the expression.
instr_count = instr->ga_len;
if (incdec)
{
r = generate_PUSHNR(cctx, 1);
}
else
{
// Temporarily hide the new local variable here, it is
// not available to this expression.
if (lhs.lhs_new_local)
--cctx->ctx_locals.ga_len;
wp = op + oplen;
if (may_get_next_line_error(wp, &p, cctx) == FAIL)
{
if (lhs.lhs_new_local)
++cctx->ctx_locals.ga_len;
goto theend;
}
r = compile_expr0_ext(&p, cctx, &is_const);
if (lhs.lhs_new_local)
++cctx->ctx_locals.ga_len;
if (r == FAIL)
goto theend;
}
}
else if (semicolon && var_idx == var_count - 1)
{
// For "[var; var] = expr" get the rest of the list
did_generate_slice = TRUE;
if (generate_SLICE(cctx, var_count - 1) == FAIL)
goto theend;
}
else
{
// For "[var, var] = expr" get the "var_idx" item from the
// list.
if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
goto theend;
}
rhs_type = stack->ga_len == 0 ? &t_void
: ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
{
if ((rhs_type->tt_type == VAR_FUNC
|| rhs_type->tt_type == VAR_PARTIAL)
&& !lhs.lhs_has_index
&& var_wrong_func_name(lhs.lhs_name, TRUE))
goto theend;
if (lhs.lhs_new_local && !lhs.lhs_has_type)
{
if (rhs_type->tt_type == VAR_VOID)
{
emsg(_(e_cannot_use_void_value));
goto theend;
}
else
{
// An empty list or dict has a &t_unknown member,
// for a variable that implies &t_any.
if (rhs_type == &t_list_empty)
lhs.lhs_lvar->lv_type = &t_list_any;
else if (rhs_type == &t_dict_empty)
lhs.lhs_lvar->lv_type = &t_dict_any;
else if (rhs_type == &t_unknown)
lhs.lhs_lvar->lv_type = &t_any;
else
lhs.lhs_lvar->lv_type = rhs_type;
}
}
else if (*op == '=')
{
type_T *use_type = lhs.lhs_lvar->lv_type;
where_T where = WHERE_INIT;
// Without operator check type here, otherwise below.
// Use the line number of the assignment.
SOURCING_LNUM = start_lnum;
where.wt_index = var_count > 0 ? var_idx + 1 : 0;
where.wt_variable = var_count > 0;
// If assigning to a list or dict member, use the
// member type. Not for "list[:] =".
if (lhs.lhs_has_index
&& !has_list_index(var_start + lhs.lhs_varlen,
cctx))
use_type = lhs.lhs_member_type;
if (need_type_where(rhs_type, use_type, -1, where,
cctx, FALSE, is_const) == FAIL)
goto theend;
}
}
else
{
type_T *lhs_type = lhs.lhs_member_type;
// Special case: assigning to @# can use a number or a
// string.
// Also: can assign a number to a float.
if ((lhs_type == &t_number_or_string
|| lhs_type == &t_float)
&& rhs_type->tt_type == VAR_NUMBER)
lhs_type = &t_number;
if (*p != '=' && need_type(rhs_type, lhs_type,
-1, 0, cctx, FALSE, FALSE) == FAIL)
goto theend;
}
}
else if (cmdidx == CMD_final)
{
emsg(_(e_final_requires_a_value));
goto theend;
}
else if (cmdidx == CMD_const)
{
emsg(_(e_const_requires_a_value));
goto theend;
}
else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option
|| lhs.lhs_dest == dest_func_option)
{
emsg(_(e_type_or_initialization_required));
goto theend;
}
else
{
// variables are always initialized
if (GA_GROW_FAILS(instr, 1))
goto theend;
switch (lhs.lhs_member_type->tt_type)
{
case VAR_BOOL:
generate_PUSHBOOL(cctx, VVAL_FALSE);
break;
case VAR_FLOAT:
#ifdef FEAT_FLOAT
generate_PUSHF(cctx, 0.0);
#endif
break;
case VAR_STRING:
generate_PUSHS(cctx, NULL);
break;
case VAR_BLOB:
generate_PUSHBLOB(cctx, blob_alloc());
break;
case VAR_FUNC:
generate_PUSHFUNC(cctx, NULL, &t_func_void);
break;
case VAR_LIST:
generate_NEWLIST(cctx, 0);
break;
case VAR_DICT:
generate_NEWDICT(cctx, 0);
break;
case VAR_JOB:
generate_PUSHJOB(cctx, NULL);
break;
case VAR_CHANNEL:
generate_PUSHCHANNEL(cctx, NULL);
break;
case VAR_NUMBER:
case VAR_UNKNOWN:
case VAR_ANY:
case VAR_PARTIAL:
case VAR_VOID:
case VAR_INSTR:
case VAR_SPECIAL: // cannot happen
generate_PUSHNR(cctx, 0);
break;
}
}
if (var_count == 0)
end = p;
}
// no need to parse more when skipping
if (cctx->ctx_skip == SKIP_YES)
break;
if (oplen > 0 && *op != '=')
{
type_T *expected;
type_T *stacktype = NULL;
if (*op == '.')
{
if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
goto theend;
}
else
{
expected = lhs.lhs_member_type;
stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (
#ifdef FEAT_FLOAT
// If variable is float operation with number is OK.
!(expected == &t_float && (stacktype == &t_number
|| stacktype == &t_number_bool)) &&
#endif
need_type(stacktype, expected, -1, 0, cctx,
FALSE, FALSE) == FAIL)
goto theend;
}
if (*op == '.')
{
if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL)
goto theend;
}
else if (*op == '+')
{
if (generate_add_instr(cctx,
operator_type(lhs.lhs_member_type, stacktype),
lhs.lhs_member_type, stacktype,
EXPR_APPEND) == FAIL)
goto theend;
}
else if (generate_two_op(cctx, op) == FAIL)
goto theend;
}
// Use the line number of the assignment for store instruction.
save_lnum = cctx->ctx_lnum;
cctx->ctx_lnum = start_lnum - 1;
if (lhs.lhs_has_index)
{
// Use the info in "lhs" to store the value at the index in the
// list or dict.
if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx)
== FAIL)
{
cctx->ctx_lnum = save_lnum;
goto theend;
}
}
else
{
if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script
|| lhs.lhs_dest == dest_global
|| lhs.lhs_dest == dest_local))
// ":const var": lock the value, but not referenced variables
generate_LOCKCONST(cctx);
if (is_decl
&& (lhs.lhs_type->tt_type == VAR_DICT
|| lhs.lhs_type->tt_type == VAR_LIST)
&& lhs.lhs_type->tt_member != NULL
&& !(lhs.lhs_type->tt_member == &t_any
&& oplen > 0
&& rhs_type != NULL
&& rhs_type->tt_type == lhs.lhs_type->tt_type
&& rhs_type->tt_member != &t_unknown)
&& lhs.lhs_type->tt_member != &t_unknown)
// Set the type in the list or dict, so that it can be checked,
// also in legacy script. Not for "list<any> = val", then the
// type of "val" is used.
generate_SETTYPE(cctx, lhs.lhs_type);
if (generate_store_lhs(cctx, &lhs, instr_count) == FAIL)
{
cctx->ctx_lnum = save_lnum;
goto theend;
}
}
cctx->ctx_lnum = save_lnum;
if (var_idx + 1 < var_count)
var_start = skipwhite(lhs.lhs_end + 1);
}
// For "[var, var] = expr" drop the "expr" value.
// Also for "[var, var; _] = expr".
if (var_count > 0 && (!semicolon || !did_generate_slice))
{
if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
goto theend;
}
ret = skipwhite(end);
theend:
vim_free(lhs.lhs_name);
return ret;
}
/*
* Check for an assignment at "eap->cmd", compile it if found.
* Return NOTDONE if there is none, FAIL for failure, OK if done.
*/
static int
may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
{
char_u *pskip;
char_u *p;
// Assuming the command starts with a variable or function name,
// find what follows.
// Skip over "var.member", "var[idx]" and the like.
// Also "&opt = val", "$ENV = val" and "@r = val".
pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
? eap->cmd + 1 : eap->cmd;
p = to_name_end(pskip, TRUE);
if (p > eap->cmd && *p != NUL)
{
char_u *var_end;
int oplen;
int heredoc;
if (eap->cmd[0] == '@')
var_end = eap->cmd + 2;
else
var_end = find_name_end(pskip, NULL, NULL,
FNE_CHECK_START | FNE_INCL_BR);
oplen = assignment_len(skipwhite(var_end), &heredoc);
if (oplen > 0)
{
size_t len = p - eap->cmd;
// Recognize an assignment if we recognize the variable
// name:
// "g:var = expr"
// "local = expr" where "local" is a local var.
// "script = expr" where "script" is a script-local var.
// "import = expr" where "import" is an imported var
// "&opt = expr"
// "$ENV = expr"
// "@r = expr"
if (*eap->cmd == '&'
|| *eap->cmd == '$'
|| *eap->cmd == '@'
|| ((len) > 2 && eap->cmd[1] == ':')
|| variable_exists(eap->cmd, len, cctx))
{
*line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
if (*line == NULL || *line == eap->cmd)
return FAIL;
return OK;
}
}
}
if (*eap->cmd == '[')
{
// [var, var] = expr
*line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
if (*line == NULL)
return FAIL;
if (*line != eap->cmd)
return OK;
}
return NOTDONE;
}
/*
* Check if "name" can be "unlet".
*/
int
check_vim9_unlet(char_u *name)
{
if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
{
// "unlet s:var" is allowed in legacy script.
if (*name == 's' && !script_is_vim9())
return OK;
semsg(_(e_cannot_unlet_str), name);
return FAIL;
}
return OK;
}
/*
* Callback passed to ex_unletlock().
*/
static int
compile_unlet(
lval_T *lvp,
char_u *name_end,
exarg_T *eap,
int deep UNUSED,
void *coookie)
{
cctx_T *cctx = coookie;
char_u *p = lvp->ll_name;
int cc = *name_end;
int ret = OK;
if (cctx->ctx_skip == SKIP_YES)
return OK;
*name_end = NUL;
if (*p == '$')
{
// :unlet $ENV_VAR
ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
}
else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL)
{
lhs_T lhs;
// This is similar to assigning: lookup the list/dict, compile the
// idx/key. Then instead of storing the value unlet the item.
// unlet {list}[idx]
// unlet {dict}[key] dict.key
//
// Figure out the LHS type and other properties.
//
ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx);
// : unlet an indexed item
if (!lhs.lhs_has_index)
{
iemsg("called compile_lhs() without an index");
ret = FAIL;
}
else
{
// Use the info in "lhs" to unlet the item at the index in the
// list or dict.
ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx);
}
vim_free(lhs.lhs_name);
}
else if (check_vim9_unlet(p) == FAIL)
{
ret = FAIL;
}
else
{
// Normal name. Only supports g:, w:, t: and b: namespaces.
ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
}
*name_end = cc;
return ret;
}
/*
* Callback passed to ex_unletlock().
*/
static int
compile_lock_unlock(
lval_T *lvp,
char_u *name_end,
exarg_T *eap,
int deep UNUSED,
void *coookie)
{
cctx_T *cctx = coookie;
int cc = *name_end;
char_u *p = lvp->ll_name;
int ret = OK;
size_t len;
char_u *buf;
isntype_T isn = ISN_EXEC;
if (cctx->ctx_skip == SKIP_YES)
return OK;
// Cannot use :lockvar and :unlockvar on local variables.
if (p[1] != ':')
{
char_u *end = find_name_end(p, NULL, NULL, FNE_CHECK_START);
if (lookup_local(p, end - p, NULL, cctx) == OK)
{
char_u *s = p;
if (*end != '.' && *end != '[')
{
emsg(_(e_cannot_lock_unlock_local_variable));
return FAIL;
}
// For "d.member" put the local variable on the stack, it will be
// passed to ex_lockvar() indirectly.
if (compile_load(&s, end, cctx, FALSE, FALSE) == FAIL)
return FAIL;
isn = ISN_LOCKUNLOCK;
}
}
// Checking is done at runtime.
*name_end = NUL;
len = name_end - p + 20;
buf = alloc(len);
if (buf == NULL)
ret = FAIL;
else
{
vim_snprintf((char *)buf, len, "%s %s",
eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
p);
ret = generate_EXEC_copy(cctx, isn, buf);
vim_free(buf);
*name_end = cc;
}
return ret;
}
/*
* compile "unlet var", "lock var" and "unlock var"
* "arg" points to "var".
*/
static char_u *
compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
{
ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
cctx);
return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
}
/*
* generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
*/
static int
compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
{
garray_T *instr = &cctx->ctx_instr;
endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
if (endlabel == NULL)
return FAIL;
endlabel->el_next = *el;
*el = endlabel;
endlabel->el_end_label = instr->ga_len;
generate_JUMP(cctx, when, 0);
return OK;
}
static void
compile_fill_jump_to_end(endlabel_T **el, int jump_where, cctx_T *cctx)
{
garray_T *instr = &cctx->ctx_instr;
while (*el != NULL)
{
endlabel_T *cur = (*el);
isn_T *isn;
isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
isn->isn_arg.jump.jump_where = jump_where;
*el = cur->el_next;
vim_free(cur);
}
}
static void
compile_free_jump_to_end(endlabel_T **el)
{
while (*el != NULL)
{
endlabel_T *cur = (*el);
*el = cur->el_next;
vim_free(cur);
}
}
/*
* Create a new scope and set up the generic items.
*/
static scope_T *
new_scope(cctx_T *cctx, scopetype_T type)
{
scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
if (scope == NULL)
return NULL;
scope->se_outer = cctx->ctx_scope;
cctx->ctx_scope = scope;
scope->se_type = type;
scope->se_local_count = cctx->ctx_locals.ga_len;
return scope;
}
/*
* Free the current scope and go back to the outer scope.
*/
static void
drop_scope(cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
if (scope == NULL)
{
iemsg("calling drop_scope() without a scope");
return;
}
cctx->ctx_scope = scope->se_outer;
switch (scope->se_type)
{
case IF_SCOPE:
compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
case FOR_SCOPE:
compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
case WHILE_SCOPE:
compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
case TRY_SCOPE:
compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
case NO_SCOPE:
case BLOCK_SCOPE:
break;
}
vim_free(scope);
}
/*
* compile "if expr"
*
* "if expr" Produces instructions:
* EVAL expr Push result of "expr"
* JUMP_IF_FALSE end
* ... body ...
* end:
*
* "if expr | else" Produces instructions:
* EVAL expr Push result of "expr"
* JUMP_IF_FALSE else
* ... body ...
* JUMP_ALWAYS end
* else:
* ... body ...
* end:
*
* "if expr1 | elseif expr2 | else" Produces instructions:
* EVAL expr Push result of "expr"
* JUMP_IF_FALSE elseif
* ... body ...
* JUMP_ALWAYS end
* elseif:
* EVAL expr Push result of "expr"
* JUMP_IF_FALSE else
* ... body ...
* JUMP_ALWAYS end
* else:
* ... body ...
* end:
*/
static char_u *
compile_if(char_u *arg, cctx_T *cctx)
{
char_u *p = arg;
garray_T *instr = &cctx->ctx_instr;
int instr_count = instr->ga_len;
scope_T *scope;
skip_T skip_save = cctx->ctx_skip;
ppconst_T ppconst;
CLEAR_FIELD(ppconst);
if (compile_expr1(&p, cctx, &ppconst) == FAIL)
{
clear_ppconst(&ppconst);
return NULL;
}
if (!ends_excmd2(arg, skipwhite(p)))
{
semsg(_(e_trailing_arg), p);
return NULL;
}
if (cctx->ctx_skip == SKIP_YES)
clear_ppconst(&ppconst);
else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
{
int error = FALSE;
int v;
// The expression results in a constant.
v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
clear_ppconst(&ppconst);
if (error)
return NULL;
cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
}
else
{
// Not a constant, generate instructions for the expression.
cctx->ctx_skip = SKIP_UNKNOWN;
if (generate_ppconst(cctx, &ppconst) == FAIL)
return NULL;
if (bool_on_stack(cctx) == FAIL)
return NULL;
}
// CMDMOD_REV must come before the jump
generate_undo_cmdmods(cctx);
scope = new_scope(cctx, IF_SCOPE);
if (scope == NULL)
return NULL;
scope->se_skip_save = skip_save;
// "is_had_return" will be reset if any block does not end in :return
scope->se_u.se_if.is_had_return = TRUE;
if (cctx->ctx_skip == SKIP_UNKNOWN)
{
// "where" is set when ":elseif", "else" or ":endif" is found
scope->se_u.se_if.is_if_label = instr->ga_len;
generate_JUMP(cctx, JUMP_IF_FALSE, 0);
}
else
scope->se_u.se_if.is_if_label = -1;
#ifdef FEAT_PROFILE
if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
&& skip_save != SKIP_YES)
{
// generated a profile start, need to generate a profile end, since it
// won't be done after returning
cctx->ctx_skip = SKIP_NOT;
generate_instr(cctx, ISN_PROF_END);
cctx->ctx_skip = SKIP_YES;
}
#endif
return p;
}
static char_u *
compile_elseif(char_u *arg, cctx_T *cctx)
{
char_u *p = arg;
garray_T *instr = &cctx->ctx_instr;
int instr_count;
isn_T *isn;
scope_T *scope = cctx->ctx_scope;
ppconst_T ppconst;
skip_T save_skip = cctx->ctx_skip;
if (scope == NULL || scope->se_type != IF_SCOPE)
{
emsg(_(e_elseif_without_if));
return NULL;
}
unwind_locals(cctx, scope->se_local_count);
if (!cctx->ctx_had_return)
scope->se_u.se_if.is_had_return = FALSE;
if (cctx->ctx_skip == SKIP_NOT)
{
// previous block was executed, this one and following will not
cctx->ctx_skip = SKIP_YES;
scope->se_u.se_if.is_seen_skip_not = TRUE;
}
if (scope->se_u.se_if.is_seen_skip_not)
{
// A previous block was executed, skip over expression and bail out.
// Do not count the "elseif" for profiling and cmdmod
instr->ga_len = current_instr_idx(cctx);
skip_expr_cctx(&p, cctx);
return p;
}
if (cctx->ctx_skip == SKIP_UNKNOWN)
{
int moved_cmdmod = FALSE;
int saved_debug = FALSE;
isn_T debug_isn;
// Move any CMDMOD instruction to after the jump
if (((isn_T *)instr->ga_data)[instr->ga_len - 1].isn_type == ISN_CMDMOD)
{
if (GA_GROW_FAILS(instr, 1))
return NULL;
((isn_T *)instr->ga_data)[instr->ga_len] =
((isn_T *)instr->ga_data)[instr->ga_len - 1];
--instr->ga_len;
moved_cmdmod = TRUE;
}
// Remove the already generated ISN_DEBUG, it is written below the
// ISN_FOR instruction.
if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
&& ((isn_T *)instr->ga_data)[instr->ga_len - 1]
.isn_type == ISN_DEBUG)
{
--instr->ga_len;
debug_isn = ((isn_T *)instr->ga_data)[instr->ga_len];
saved_debug = TRUE;
}
if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
JUMP_ALWAYS, cctx) == FAIL)
return NULL;
// previous "if" or "elseif" jumps here
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
isn->isn_arg.jump.jump_where = instr->ga_len;
if (moved_cmdmod)
++instr->ga_len;
if (saved_debug)
{
// move the debug instruction here
if (GA_GROW_FAILS(instr, 1))
return NULL;
((isn_T *)instr->ga_data)[instr->ga_len] = debug_isn;
++instr->ga_len;
}
}
// compile "expr"; if we know it evaluates to FALSE skip the block
CLEAR_FIELD(ppconst);
if (cctx->ctx_skip == SKIP_YES)
{
cctx->ctx_skip = SKIP_UNKNOWN;
#ifdef FEAT_PROFILE
if (cctx->ctx_compile_type == CT_PROFILE)
// the previous block was skipped, need to profile this line
generate_instr(cctx, ISN_PROF_START);
#endif
if (cctx->ctx_compile_type == CT_DEBUG)
// the previous block was skipped, may want to debug this line
generate_instr_debug(cctx);
}
instr_count = instr->ga_len;
if (compile_expr1(&p, cctx, &ppconst) == FAIL)
{
clear_ppconst(&ppconst);
return NULL;
}
cctx->ctx_skip = save_skip;
if (!ends_excmd2(arg, skipwhite(p)))
{
clear_ppconst(&ppconst);
semsg(_(e_trailing_arg), p);
return NULL;
}
if (scope->se_skip_save == SKIP_YES)
clear_ppconst(&ppconst);
else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
{
int error = FALSE;
int v;
// The expression result is a constant.
v = tv_get_bool_chk(&ppconst.pp_tv[0], &error);
if (error)
{
clear_ppconst(&ppconst);
return NULL;
}
cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES;
clear_ppconst(&ppconst);
scope->se_u.se_if.is_if_label = -1;
}
else
{
// Not a constant, generate instructions for the expression.
cctx->ctx_skip = SKIP_UNKNOWN;
if (generate_ppconst(cctx, &ppconst) == FAIL)
return NULL;
if (bool_on_stack(cctx) == FAIL)
return NULL;
// CMDMOD_REV must come before the jump
generate_undo_cmdmods(cctx);
// "where" is set when ":elseif", "else" or ":endif" is found
scope->se_u.se_if.is_if_label = instr->ga_len;
generate_JUMP(cctx, JUMP_IF_FALSE, 0);
}
return p;
}
static char_u *
compile_else(char_u *arg, cctx_T *cctx)
{
char_u *p = arg;
garray_T *instr = &cctx->ctx_instr;
isn_T *isn;
scope_T *scope = cctx->ctx_scope;
if (scope == NULL || scope->se_type != IF_SCOPE)
{
emsg(_(e_else_without_if));
return NULL;
}
unwind_locals(cctx, scope->se_local_count);
if (!cctx->ctx_had_return)
scope->se_u.se_if.is_had_return = FALSE;
scope->se_u.se_if.is_seen_else = TRUE;
#ifdef FEAT_PROFILE
if (cctx->ctx_compile_type == CT_PROFILE)
{
if (cctx->ctx_skip == SKIP_NOT
&& ((isn_T *)instr->ga_data)[instr->ga_len - 1]
.isn_type == ISN_PROF_START)
// the previous block was executed, do not count "else" for
// profiling
--instr->ga_len;
if (cctx->ctx_skip == SKIP_YES && !scope->se_u.se_if.is_seen_skip_not)
{
// the previous block was not executed, this one will, do count the
// "else" for profiling
cctx->ctx_skip = SKIP_NOT;
generate_instr(cctx, ISN_PROF_END);
generate_instr(cctx, ISN_PROF_START);
cctx->ctx_skip = SKIP_YES;
}
}
#endif
if (!scope->se_u.se_if.is_seen_skip_not && scope->se_skip_save != SKIP_YES)
{
// jump from previous block to the end, unless the else block is empty
if (cctx->ctx_skip == SKIP_UNKNOWN)
{
if (!cctx->ctx_had_return
&& compile_jump_to_end(&scope->se_u.se_if.is_end_label,
JUMP_ALWAYS, cctx) == FAIL)
return NULL;
}
if (cctx->ctx_skip == SKIP_UNKNOWN)
{
if (scope->se_u.se_if.is_if_label >= 0)
{
// previous "if" or "elseif" jumps here
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
isn->isn_arg.jump.jump_where = instr->ga_len;
scope->se_u.se_if.is_if_label = -1;
}
}
if (cctx->ctx_skip != SKIP_UNKNOWN)
cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
}
return p;
}
static char_u *
compile_endif(char_u *arg, cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
ifscope_T *ifscope;
garray_T *instr = &cctx->ctx_instr;
isn_T *isn;
if (misplaced_cmdmod(cctx))
return NULL;
if (scope == NULL || scope->se_type != IF_SCOPE)
{
emsg(_(e_endif_without_if));
return NULL;
}
ifscope = &scope->se_u.se_if;
unwind_locals(cctx, scope->se_local_count);
if (!cctx->ctx_had_return)
ifscope->is_had_return = FALSE;
if (scope->se_u.se_if.is_if_label >= 0)
{
// previous "if" or "elseif" jumps here
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
isn->isn_arg.jump.jump_where = instr->ga_len;
}
// Fill in the "end" label in jumps at the end of the blocks.
compile_fill_jump_to_end(&ifscope->is_end_label, instr->ga_len, cctx);
#ifdef FEAT_PROFILE
// even when skipping we count the endif as executed, unless the block it's
// in is skipped
if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_skip == SKIP_YES
&& scope->se_skip_save != SKIP_YES)
{
cctx->ctx_skip = SKIP_NOT;
generate_instr(cctx, ISN_PROF_START);
}
#endif
cctx->ctx_skip = scope->se_skip_save;
// If all the blocks end in :return and there is an :else then the
// had_return flag is set.
cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
drop_scope(cctx);
return arg;
}
/*
* Compile "for var in expr":
*
* Produces instructions:
* PUSHNR -1
* STORE loop-idx Set index to -1
* EVAL expr result of "expr" on top of stack
* top: FOR loop-idx, end Increment index, use list on bottom of stack
* - if beyond end, jump to "end"
* - otherwise get item from list and push it
* STORE var Store item in "var"
* ... body ...
* JUMP top Jump back to repeat
* end: DROP Drop the result of "expr"
*
* Compile "for [var1, var2] in expr" - as above, but instead of "STORE var":
* UNPACK 2 Split item in 2
* STORE var1 Store item in "var1"
* STORE var2 Store item in "var2"
*/
static char_u *
compile_for(char_u *arg_start, cctx_T *cctx)
{
char_u *arg;
char_u *arg_end;
char_u *name = NULL;
char_u *p;
char_u *wp;
int var_count = 0;
int var_list = FALSE;
int semicolon = FALSE;
size_t varlen;
garray_T *stack = &cctx->ctx_type_stack;
garray_T *instr = &cctx->ctx_instr;
scope_T *scope;
lvar_T *loop_lvar; // loop iteration variable
lvar_T *var_lvar; // variable for "var"
type_T *vartype;
type_T *item_type = &t_any;
int idx;
int prev_lnum = cctx->ctx_prev_lnum;
p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE);
if (p == NULL)
return NULL;
if (var_count == 0)
var_count = 1;
else
var_list = TRUE; // can also be a list of one variable
// consume "in"
wp = p;
if (may_get_next_line_error(wp, &p, cctx) == FAIL)
return NULL;
if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2]))
{
if (*p == ':' && wp != p)
semsg(_(e_no_white_space_allowed_before_colon_str), p);
else
emsg(_(e_missing_in));
return NULL;
}
wp = p + 2;
if (may_get_next_line_error(wp, &p, cctx) == FAIL)
return NULL;
// Remove the already generated ISN_DEBUG, it is written below the ISN_FOR
// instruction.
if (cctx->ctx_compile_type == CT_DEBUG && instr->ga_len > 0
&& ((isn_T *)instr->ga_data)[instr->ga_len - 1]
.isn_type == ISN_DEBUG)
{
--instr->ga_len;
prev_lnum = ((isn_T *)instr->ga_data)[instr->ga_len]
.isn_arg.debug.dbg_break_lnum;
}
scope = new_scope(cctx, FOR_SCOPE);
if (scope == NULL)
return NULL;
// Reserve a variable to store the loop iteration counter and initialize it
// to -1.
loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
if (loop_lvar == NULL)
{
// out of memory
drop_scope(cctx);
return NULL;
}
generate_STORENR(cctx, loop_lvar->lv_idx, -1);
// compile "expr", it remains on the stack until "endfor"
arg = p;
if (compile_expr0(&arg, cctx) == FAIL)
{
drop_scope(cctx);
return NULL;
}
arg_end = arg;
if (cctx->ctx_skip != SKIP_YES)
{
// If we know the type of "var" and it is a not a supported type we can
// give an error now.
vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (vartype->tt_type != VAR_LIST && vartype->tt_type != VAR_STRING
&& vartype->tt_type != VAR_BLOB && vartype->tt_type != VAR_ANY)
{
semsg(_(e_for_loop_on_str_not_supported),
vartype_name(vartype->tt_type));
drop_scope(cctx);
return NULL;
}
if (vartype->tt_type == VAR_STRING)
item_type = &t_string;
else if (vartype->tt_type == VAR_BLOB)
item_type = &t_number;
else if (vartype->tt_type == VAR_LIST
&& vartype->tt_member->tt_type != VAR_ANY)
{
if (!var_list)
item_type = vartype->tt_member;
else if (vartype->tt_member->tt_type == VAR_LIST
&& vartype->tt_member->tt_member->tt_type != VAR_ANY)
item_type = vartype->tt_member->tt_member;
}
// CMDMOD_REV must come before the FOR instruction.
generate_undo_cmdmods(cctx);
// "for_end" is set when ":endfor" is found
scope->se_u.se_for.fs_top_label = current_instr_idx(cctx);
generate_FOR(cctx, loop_lvar->lv_idx);
arg = arg_start;
if (var_list)
{
generate_UNPACK(cctx, var_count, semicolon);
arg = skipwhite(arg + 1); // skip white after '['
// the list item is replaced by a number of items
if (GA_GROW_FAILS(stack, var_count - 1))
{
drop_scope(cctx);
return NULL;
}
--stack->ga_len;
for (idx = 0; idx < var_count; ++idx)
{
((type_T **)stack->ga_data)[stack->ga_len] =
(semicolon && idx == 0) ? vartype : item_type;
++stack->ga_len;
}
}
for (idx = 0; idx < var_count; ++idx)
{
assign_dest_T dest = dest_local;
int opt_flags = 0;
int vimvaridx = -1;
type_T *type = &t_any;
type_T *lhs_type = &t_any;
where_T where = WHERE_INIT;
p = skip_var_one(arg, FALSE);
varlen = p - arg;
name = vim_strnsave(arg, varlen);
if (name == NULL)
goto failed;
if (*p == ':')
{
p = skipwhite(p + 1);
lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
}
// Script var is not supported.
if (get_var_dest(name, &dest, CMD_for, &opt_flags,
&vimvaridx, &type, cctx) == FAIL)
goto failed;
if (dest != dest_local)
{
if (generate_store_var(cctx, dest, opt_flags, vimvaridx,
0, 0, type, name) == FAIL)
goto failed;
}
else if (varlen == 1 && *arg == '_')
{
// Assigning to "_": drop the value.
if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
goto failed;
}
else
{
if (!valid_varname(arg, (int)varlen, FALSE))
goto failed;
if (lookup_local(arg, varlen, NULL, cctx) == OK)
{
semsg(_(e_variable_already_declared), arg);
goto failed;
}
if (STRNCMP(name, "s:", 2) == 0)
{
semsg(_(e_cannot_declare_script_variable_in_function), name);
goto failed;
}
// Reserve a variable to store "var".
where.wt_index = var_list ? idx + 1 : 0;
where.wt_variable = TRUE;
if (lhs_type == &t_any)
lhs_type = item_type;
else if (item_type != &t_unknown
&& (item_type == &t_any
? need_type(item_type, lhs_type,
-1, 0, cctx, FALSE, FALSE)
: check_type(lhs_type, item_type, TRUE, where))
== FAIL)
goto failed;
var_lvar = reserve_local(cctx, arg, varlen, TRUE, lhs_type);
if (var_lvar == NULL)
// out of memory or used as an argument
goto failed;
if (semicolon && idx == var_count - 1)
var_lvar->lv_type = vartype;
else
var_lvar->lv_type = item_type;
generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
}
if (*p == ',' || *p == ';')
++p;
arg = skipwhite(p);
vim_free(name);
}
if (cctx->ctx_compile_type == CT_DEBUG)
{
int save_prev_lnum = cctx->ctx_prev_lnum;
// Add ISN_DEBUG here, so that the loop variables can be inspected.
// Use the prev_lnum from the ISN_DEBUG instruction removed above.
cctx->ctx_prev_lnum = prev_lnum;
generate_instr_debug(cctx);
cctx->ctx_prev_lnum = save_prev_lnum;
}
}
return arg_end;
failed:
vim_free(name);
drop_scope(cctx);
return NULL;
}
/*
* compile "endfor"
*/
static char_u *
compile_endfor(char_u *arg, cctx_T *cctx)
{
garray_T *instr = &cctx->ctx_instr;
scope_T *scope = cctx->ctx_scope;
forscope_T *forscope;
isn_T *isn;
if (misplaced_cmdmod(cctx))
return NULL;
if (scope == NULL || scope->se_type != FOR_SCOPE)
{
emsg(_(e_for));
return NULL;
}
forscope = &scope->se_u.se_for;
cctx->ctx_scope = scope->se_outer;
if (cctx->ctx_skip != SKIP_YES)
{
unwind_locals(cctx, scope->se_local_count);
// At end of ":for" scope jump back to the FOR instruction.
generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
// Fill in the "end" label in the FOR statement so it can jump here.
isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
isn->isn_arg.forloop.for_end = instr->ga_len;
// Fill in the "end" label any BREAK statements
compile_fill_jump_to_end(&forscope->fs_end_label, instr->ga_len, cctx);
// Below the ":for" scope drop the "expr" list from the stack.
if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
return NULL;
}
vim_free(scope);
return arg;
}
/*
* compile "while expr"
*
* Produces instructions:
* top: EVAL expr Push result of "expr"
* JUMP_IF_FALSE end jump if false
* ... body ...
* JUMP top Jump back to repeat
* end:
*
*/
static char_u *
compile_while(char_u *arg, cctx_T *cctx)
{
char_u *p = arg;
scope_T *scope;
scope = new_scope(cctx, WHILE_SCOPE);
if (scope == NULL)
return NULL;
// "endwhile" jumps back here, one before when profiling or using cmdmods
scope->se_u.se_while.ws_top_label = current_instr_idx(cctx);
// compile "expr"
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
if (!ends_excmd2(arg, skipwhite(p)))
{
semsg(_(e_trailing_arg), p);
return NULL;
}
if (cctx->ctx_skip != SKIP_YES)
{
if (bool_on_stack(cctx) == FAIL)
return FAIL;
// CMDMOD_REV must come before the jump
generate_undo_cmdmods(cctx);
// "while_end" is set when ":endwhile" is found
if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
JUMP_IF_FALSE, cctx) == FAIL)
return FAIL;
}
return p;
}
/*
* compile "endwhile"
*/
static char_u *
compile_endwhile(char_u *arg, cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
garray_T *instr = &cctx->ctx_instr;
if (misplaced_cmdmod(cctx))
return NULL;
if (scope == NULL || scope->se_type != WHILE_SCOPE)
{
emsg(_(e_while));
return NULL;
}
cctx->ctx_scope = scope->se_outer;
if (cctx->ctx_skip != SKIP_YES)
{
unwind_locals(cctx, scope->se_local_count);
#ifdef FEAT_PROFILE
// count the endwhile before jumping
may_generate_prof_end(cctx, cctx->ctx_lnum);
#endif
// At end of ":for" scope jump back to the FOR instruction.
generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
// Fill in the "end" label in the WHILE statement so it can jump here.
// And in any jumps for ":break"
compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label,
instr->ga_len, cctx);
}
vim_free(scope);
return arg;
}
/*
* compile "continue"
*/
static char_u *
compile_continue(char_u *arg, cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
int try_scopes = 0;
int loop_label;
for (;;)
{
if (scope == NULL)
{
emsg(_(e_continue));
return NULL;
}
if (scope->se_type == FOR_SCOPE)
{
loop_label = scope->se_u.se_for.fs_top_label;
break;
}
if (scope->se_type == WHILE_SCOPE)
{
loop_label = scope->se_u.se_while.ws_top_label;
break;
}
if (scope->se_type == TRY_SCOPE)
++try_scopes;
scope = scope->se_outer;
}
if (try_scopes > 0)
// Inside one or more try/catch blocks we first need to jump to the
// "finally" or "endtry" to cleanup.
generate_TRYCONT(cctx, try_scopes, loop_label);
else
// Jump back to the FOR or WHILE instruction.
generate_JUMP(cctx, JUMP_ALWAYS, loop_label);
return arg;
}
/*
* compile "break"
*/
static char_u *
compile_break(char_u *arg, cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
endlabel_T **el;
for (;;)
{
if (scope == NULL)
{
emsg(_(e_break));
return NULL;
}
if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
break;
scope = scope->se_outer;
}
// Jump to the end of the FOR or WHILE loop.
if (scope->se_type == FOR_SCOPE)
el = &scope->se_u.se_for.fs_end_label;
else
el = &scope->se_u.se_while.ws_end_label;
if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
return FAIL;
return arg;
}
/*
* compile "{" start of block
*/
static char_u *
compile_block(char_u *arg, cctx_T *cctx)
{
if (new_scope(cctx, BLOCK_SCOPE) == NULL)
return NULL;
return skipwhite(arg + 1);
}
/*
* compile end of block: drop one scope
*/
static void
compile_endblock(cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
cctx->ctx_scope = scope->se_outer;
unwind_locals(cctx, scope->se_local_count);
vim_free(scope);
}
/*
* compile "try"
* Creates a new scope for the try-endtry, pointing to the first catch and
* finally.
* Creates another scope for the "try" block itself.
* TRY instruction sets up exception handling at runtime.
*
* "try"
* TRY -> catch1, -> finally push trystack entry
* ... try block
* "throw {exception}"
* EVAL {exception}
* THROW create exception
* ... try block
* " catch {expr}"
* JUMP -> finally
* catch1: PUSH exception
* EVAL {expr}
* MATCH
* JUMP nomatch -> catch2
* CATCH remove exception
* ... catch block
* " catch"
* JUMP -> finally
* catch2: CATCH remove exception
* ... catch block
* " finally"
* finally:
* ... finally block
* " endtry"
* ENDTRY pop trystack entry, may rethrow
*/
static char_u *
compile_try(char_u *arg, cctx_T *cctx)
{
garray_T *instr = &cctx->ctx_instr;
scope_T *try_scope;
scope_T *scope;
if (misplaced_cmdmod(cctx))
return NULL;
// scope that holds the jumps that go to catch/finally/endtry
try_scope = new_scope(cctx, TRY_SCOPE);
if (try_scope == NULL)
return NULL;
if (cctx->ctx_skip != SKIP_YES)
{
isn_T *isn;
// "try_catch" is set when the first ":catch" is found or when no catch
// is found and ":finally" is found.
// "try_finally" is set when ":finally" is found
// "try_endtry" is set when ":endtry" is found
try_scope->se_u.se_try.ts_try_label = instr->ga_len;
if ((isn = generate_instr(cctx, ISN_TRY)) == NULL)
return NULL;
isn->isn_arg.try.try_ref = ALLOC_CLEAR_ONE(tryref_T);
if (isn->isn_arg.try.try_ref == NULL)
return NULL;
}
// scope for the try block itself
scope = new_scope(cctx, BLOCK_SCOPE);
if (scope == NULL)
return NULL;
return arg;
}
/*
* compile "catch {expr}"
*/
static char_u *
compile_catch(char_u *arg, cctx_T *cctx UNUSED)
{
scope_T *scope = cctx->ctx_scope;
garray_T *instr = &cctx->ctx_instr;
char_u *p;
isn_T *isn;
if (misplaced_cmdmod(cctx))
return NULL;
// end block scope from :try or :catch
if (scope != NULL && scope->se_type == BLOCK_SCOPE)
compile_endblock(cctx);
scope = cctx->ctx_scope;
// Error if not in a :try scope
if (scope == NULL || scope->se_type != TRY_SCOPE)
{
emsg(_(e_catch));
return NULL;
}
if (scope->se_u.se_try.ts_caught_all)
{
emsg(_(e_catch_unreachable_after_catch_all));
return NULL;
}
if (cctx->ctx_skip != SKIP_YES)
{
#ifdef FEAT_PROFILE
// the profile-start should be after the jump
if (cctx->ctx_compile_type == CT_PROFILE
&& instr->ga_len > 0
&& ((isn_T *)instr->ga_data)[instr->ga_len - 1]
.isn_type == ISN_PROF_START)
--instr->ga_len;
#endif
// Jump from end of previous block to :finally or :endtry
if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
JUMP_ALWAYS, cctx) == FAIL)
return NULL;
// End :try or :catch scope: set value in ISN_TRY instruction
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
if (isn->isn_arg.try.try_ref->try_catch == 0)
isn->isn_arg.try.try_ref->try_catch = instr->ga_len;
if (scope->se_u.se_try.ts_catch_label != 0)
{
// Previous catch without match jumps here
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
isn->isn_arg.jump.jump_where = instr->ga_len;
}
#ifdef FEAT_PROFILE
if (cctx->ctx_compile_type == CT_PROFILE)
{
// a "throw" that jumps here needs to be counted
generate_instr(cctx, ISN_PROF_END);
// the "catch" is also counted
generate_instr(cctx, ISN_PROF_START);
}
#endif
if (cctx->ctx_compile_type == CT_DEBUG)
generate_instr_debug(cctx);
}
p = skipwhite(arg);
if (ends_excmd2(arg, p))
{
scope->se_u.se_try.ts_caught_all = TRUE;
scope->se_u.se_try.ts_catch_label = 0;
}
else
{
char_u *end;
char_u *pat;
char_u *tofree = NULL;
int dropped = 0;
int len;
// Push v:exception, push {expr} and MATCH
generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL);
if (*end != *p)
{
semsg(_(e_separator_mismatch_str), p);
vim_free(tofree);
return FAIL;
}
if (tofree == NULL)
len = (int)(end - (p + 1));
else
len = (int)(end - tofree);
pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
vim_free(tofree);
p += len + 2 + dropped;
if (pat == NULL)
return FAIL;
if (generate_PUSHS(cctx, &pat) == FAIL)
return FAIL;
if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
return NULL;
scope->se_u.se_try.ts_catch_label = instr->ga_len;
if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
return NULL;
}
if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL)
return NULL;
if (new_scope(cctx, BLOCK_SCOPE) == NULL)
return NULL;
return p;
}
static char_u *
compile_finally(char_u *arg, cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
garray_T *instr = &cctx->ctx_instr;
isn_T *isn;
int this_instr;
if (misplaced_cmdmod(cctx))
return NULL;
// end block scope from :try or :catch
if (scope != NULL && scope->se_type == BLOCK_SCOPE)
compile_endblock(cctx);
scope = cctx->ctx_scope;
// Error if not in a :try scope
if (scope == NULL || scope->se_type != TRY_SCOPE)
{
emsg(_(e_finally));
return NULL;
}
if (cctx->ctx_skip != SKIP_YES)
{
// End :catch or :finally scope: set value in ISN_TRY instruction
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
if (isn->isn_arg.try.try_ref->try_finally != 0)
{
emsg(_(e_finally_dup));
return NULL;
}
this_instr = instr->ga_len;
#ifdef FEAT_PROFILE
if (cctx->ctx_compile_type == CT_PROFILE
&& ((isn_T *)instr->ga_data)[this_instr - 1]
.isn_type == ISN_PROF_START)
{
// jump to the profile start of the "finally"
--this_instr;
// jump to the profile end above it
if (this_instr > 0 && ((isn_T *)instr->ga_data)[this_instr - 1]
.isn_type == ISN_PROF_END)
--this_instr;
}
#endif
// Fill in the "end" label in jumps at the end of the blocks.
compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
this_instr, cctx);
// If there is no :catch then an exception jumps to :finally.
if (isn->isn_arg.try.try_ref->try_catch == 0)
isn->isn_arg.try.try_ref->try_catch = this_instr;
isn->isn_arg.try.try_ref->try_finally = this_instr;
if (scope->se_u.se_try.ts_catch_label != 0)
{
// Previous catch without match jumps here
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
isn->isn_arg.jump.jump_where = this_instr;
scope->se_u.se_try.ts_catch_label = 0;
}
if (generate_instr(cctx, ISN_FINALLY) == NULL)
return NULL;
}
return arg;
}
static char_u *
compile_endtry(char_u *arg, cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
garray_T *instr = &cctx->ctx_instr;
isn_T *try_isn;
if (misplaced_cmdmod(cctx))
return NULL;
// end block scope from :catch or :finally
if (scope != NULL && scope->se_type == BLOCK_SCOPE)
compile_endblock(cctx);
scope = cctx->ctx_scope;
// Error if not in a :try scope
if (scope == NULL || scope->se_type != TRY_SCOPE)
{
if (scope == NULL)
emsg(_(e_no_endtry));
else if (scope->se_type == WHILE_SCOPE)
emsg(_(e_endwhile));
else if (scope->se_type == FOR_SCOPE)
emsg(_(e_endfor));
else
emsg(_(e_endif));
return NULL;
}
try_isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
if (cctx->ctx_skip != SKIP_YES)
{
if (try_isn->isn_arg.try.try_ref->try_catch == 0
&& try_isn->isn_arg.try.try_ref->try_finally == 0)
{
emsg(_(e_missing_catch_or_finally));
return NULL;
}
#ifdef FEAT_PROFILE
if (cctx->ctx_compile_type == CT_PROFILE
&& ((isn_T *)instr->ga_data)[instr->ga_len - 1]
.isn_type == ISN_PROF_START)
// move the profile start after "endtry" so that it's not counted when
// the exception is rethrown.
--instr->ga_len;
#endif
// Fill in the "end" label in jumps at the end of the blocks, if not
// done by ":finally".
compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label,
instr->ga_len, cctx);
if (scope->se_u.se_try.ts_catch_label != 0)
{
// Last catch without match jumps here
isn_T *isn = ((isn_T *)instr->ga_data)
+ scope->se_u.se_try.ts_catch_label;
isn->isn_arg.jump.jump_where = instr->ga_len;
}
}
compile_endblock(cctx);
if (cctx->ctx_skip != SKIP_YES)
{
// End :catch or :finally scope: set instruction index in ISN_TRY
// instruction
try_isn->isn_arg.try.try_ref->try_endtry = instr->ga_len;
if (cctx->ctx_skip != SKIP_YES
&& generate_instr(cctx, ISN_ENDTRY) == NULL)
return NULL;
#ifdef FEAT_PROFILE
if (cctx->ctx_compile_type == CT_PROFILE)
generate_instr(cctx, ISN_PROF_START);
#endif
}
return arg;
}
/*
* compile "throw {expr}"
*/
static char_u *
compile_throw(char_u *arg, cctx_T *cctx UNUSED)
{
char_u *p = skipwhite(arg);
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
if (cctx->ctx_skip == SKIP_YES)
return p;
if (may_generate_2STRING(-1, FALSE, cctx) == FAIL)
return NULL;
if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
return NULL;
return p;
}
static char_u *
compile_eval(char_u *arg, cctx_T *cctx)
{
char_u *p = arg;
int name_only;
long lnum = SOURCING_LNUM;
// find_ex_command() will consider a variable name an expression, assuming
// that something follows on the next line. Check that something actually
// follows, otherwise it's probably a misplaced command.
name_only = cmd_is_name_only(arg);
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
if (name_only && lnum == SOURCING_LNUM)
{
semsg(_(e_expression_without_effect_str), arg);
return NULL;
}
// drop the result
generate_instr_drop(cctx, ISN_DROP, 1);
return skipwhite(p);
}
/*
* compile "echo expr"
* compile "echomsg expr"
* compile "echoerr expr"
* compile "echoconsole expr"
* compile "execute expr"
*/
static char_u *
compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
{
char_u *p = arg;
char_u *prev = arg;
char_u *expr_start;
int count = 0;
int start_ctx_lnum = cctx->ctx_lnum;
garray_T *stack = &cctx->ctx_type_stack;
type_T *type;
for (;;)
{
if (ends_excmd2(prev, p))
break;
expr_start = p;
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
if (cctx->ctx_skip != SKIP_YES)
{
// check for non-void type
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (type->tt_type == VAR_VOID)
{
semsg(_(e_expression_does_not_result_in_value_str), expr_start);
return NULL;
}
}
++count;
prev = p;
p = skipwhite(p);
}
if (count > 0)
{
long save_lnum = cctx->ctx_lnum;
// Use the line number where the command started.
cctx->ctx_lnum = start_ctx_lnum;
if (cmdidx == CMD_echo || cmdidx == CMD_echon)
generate_ECHO(cctx, cmdidx == CMD_echo, count);
else if (cmdidx == CMD_execute)
generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
else if (cmdidx == CMD_echomsg)
generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
else if (cmdidx == CMD_echoconsole)
generate_MULT_EXPR(cctx, ISN_ECHOCONSOLE, count);
else
generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
cctx->ctx_lnum = save_lnum;
}
return p;
}
/*
* If "eap" has a range that is not a constant generate an ISN_RANGE
* instruction to compute it and return OK.
* Otherwise return FAIL, the caller must deal with any range.
*/
static int
compile_variable_range(exarg_T *eap, cctx_T *cctx)
{
char_u *range_end = skip_range(eap->cmd, TRUE, NULL);
char_u *p = skipdigits(eap->cmd);
if (p == range_end)
return FAIL;
return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd));
}
/*
* :put r
* :put ={expr}
*/
static char_u *
compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx)
{
char_u *line = arg;
linenr_T lnum;
char *errormsg;
int above = eap->forceit;
eap->regname = *line;
if (eap->regname == '=')
{
char_u *p = line + 1;
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
line = p;
}
else if (eap->regname != NUL)
++line;
if (compile_variable_range(eap, cctx) == OK)
{
lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE;
}
else
{
// Either no range or a number.
// "errormsg" will not be set because the range is ADDR_LINES.
if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL)
// cannot happen
return NULL;
if (eap->addr_count == 0)
lnum = -1;
else
lnum = eap->line2;
if (above)
--lnum;
}
generate_PUT(cctx, eap->regname, lnum);
return line;
}
/*
* A command that is not compiled, execute with legacy code.
*/
static char_u *
compile_exec(char_u *line_arg, exarg_T *eap, cctx_T *cctx)
{
char_u *line = line_arg;
char_u *p;
int has_expr = FALSE;
char_u *nextcmd = (char_u *)"";
char_u *tofree = NULL;
char_u *cmd_arg = NULL;
if (cctx->ctx_skip == SKIP_YES)
goto theend;
// If there was a prececing command modifier, drop it and include it in the
// EXEC command.
if (cctx->ctx_has_cmdmod)
{
garray_T *instr = &cctx->ctx_instr;
isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
if (isn->isn_type == ISN_CMDMOD)
{
vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
->cmod_filter_regmatch.regprog);
vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
--instr->ga_len;
cctx->ctx_has_cmdmod = FALSE;
}
}
if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
{
long argt = eap->argt;
int usefilter = FALSE;
has_expr = argt & (EX_XFILE | EX_EXPAND);
// If the command can be followed by a bar, find the bar and truncate
// it, so that the following command can be compiled.
// The '|' is overwritten with a NUL, it is put back below.
if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
&& *eap->arg == '!')
// :w !filter or :r !filter or :r! filter
usefilter = TRUE;
if ((argt & EX_TRLBAR) && !usefilter)
{
eap->argt = argt;
separate_nextcmd(eap);
if (eap->nextcmd != NULL)
nextcmd = eap->nextcmd;
}
else if (eap->cmdidx == CMD_wincmd)
{
p = eap->arg;
if (*p != NUL)
++p;
if (*p == 'g' || *p == Ctrl_G)
++p;
p = skipwhite(p);
if (*p == '|')
{
*p = NUL;
nextcmd = p + 1;
}
}
else if (eap->cmdidx == CMD_command || eap->cmdidx == CMD_autocmd)
{
// If there is a trailing '{' read lines until the '}'
p = eap->arg + STRLEN(eap->arg) - 1;
while (p > eap->arg && VIM_ISWHITE(*p))
--p;
if (*p == '{')
{
exarg_T ea;
int flags; // unused
int start_lnum = SOURCING_LNUM;
CLEAR_FIELD(ea);
ea.arg = eap->arg;
fill_exarg_from_cctx(&ea, cctx);
(void)may_get_cmd_block(&ea, p, &tofree, &flags);
if (tofree != NULL)
{
*p = NUL;
line = concat_str(line, tofree);
if (line == NULL)
goto theend;
vim_free(tofree);
tofree = line;
SOURCING_LNUM = start_lnum;
}
}
}
}
if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
{
// expand filename in "syntax include [@group] filename"
has_expr = TRUE;
eap->arg = skipwhite(eap->arg + 7);
if (*eap->arg == '@')
eap->arg = skiptowhite(eap->arg);
}
if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal)
&& STRLEN(eap->arg) > 4)
{
int delim = *eap->arg;
p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL);
if (*p == delim)
cmd_arg = p + 1;
}
if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed)
cmd_arg = eap->arg;
if (cmd_arg != NULL)
{
exarg_T nea;
CLEAR_FIELD(nea);
nea.cmd = cmd_arg;
p = find_ex_command(&nea, NULL, lookup_scriptitem, NULL);
if (nea.cmdidx < CMD_SIZE)
{
has_expr = excmd_get_argt(nea.cmdidx) & (EX_XFILE | EX_EXPAND);
if (has_expr)
eap->arg = skiptowhite(eap->arg);
}
}
if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
{
int count = 0;
char_u *start = skipwhite(line);
// :cmd xxx`=expr1`yyy`=expr2`zzz
// PUSHS ":cmd xxx"
// eval expr1
// PUSHS "yyy"
// eval expr2
// PUSHS "zzz"
// EXECCONCAT 5
for (;;)
{
if (p > start)
{
char_u *val = vim_strnsave(start, p - start);
generate_PUSHS(cctx, &val);
++count;
}
p += 2;
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
may_generate_2STRING(-1, TRUE, cctx);
++count;
p = skipwhite(p);
if (*p != '`')
{
emsg(_(e_missing_backtick));
return NULL;
}
start = p + 1;
p = (char_u *)strstr((char *)start, "`=");
if (p == NULL)
{
if (*skipwhite(start) != NUL)
{
char_u *val = vim_strsave(start);
generate_PUSHS(cctx, &val);
++count;
}
break;
}
}
generate_EXECCONCAT(cctx, count);
}
else
generate_EXEC_copy(cctx, ISN_EXEC, line);
theend:
if (*nextcmd != NUL)
{
// the parser expects a pointer to the bar, put it back
--nextcmd;
*nextcmd = '|';
}
vim_free(tofree);
return nextcmd;
}
/*
* A script command with heredoc, e.g.
* ruby << EOF
* command
* EOF
* Has been turned into one long line with NL characters by
* get_function_body():
* ruby << EOF<NL> command<NL>EOF
*/
static char_u *
compile_script(char_u *line, cctx_T *cctx)
{
if (cctx->ctx_skip != SKIP_YES)
{
isn_T *isn;
if ((isn = generate_instr(cctx, ISN_EXEC_SPLIT)) == NULL)
return NULL;
isn->isn_arg.string = vim_strsave(line);
}
return (char_u *)"";
}
/*
* :s/pat/repl/
*/
static char_u *
compile_substitute(char_u *arg, exarg_T *eap, cctx_T *cctx)
{
char_u *cmd = eap->arg;
char_u *expr = (char_u *)strstr((char *)cmd, "\\=");
if (expr != NULL)
{
int delimiter = *cmd++;
// There is a \=expr, find it in the substitute part.
cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), NULL, NULL, NULL);
if (cmd[0] == delimiter && cmd[1] == '\\' && cmd[2] == '=')
{
garray_T save_ga = cctx->ctx_instr;
char_u *end;
int expr_res;
int trailing_error;
int instr_count;
isn_T *instr;
isn_T *isn;
cmd += 3;
end = skip_substitute(cmd, delimiter);
// Temporarily reset the list of instructions so that the jump
// labels are correct.
cctx->ctx_instr.ga_len = 0;
cctx->ctx_instr.ga_maxlen = 0;
cctx->ctx_instr.ga_data = NULL;
expr_res = compile_expr0(&cmd, cctx);
if (end[-1] == NUL)
end[-1] = delimiter;
cmd = skipwhite(cmd);
trailing_error = *cmd != delimiter && *cmd != NUL;
if (expr_res == FAIL || trailing_error
|| GA_GROW_FAILS(&cctx->ctx_instr, 1))
{
if (trailing_error)
semsg(_(e_trailing_arg), cmd);
clear_instr_ga(&cctx->ctx_instr);
cctx->ctx_instr = save_ga;
return NULL;
}
// Move the generated instructions into the ISN_SUBSTITUTE
// instructions, then restore the list of instructions before
// adding the ISN_SUBSTITUTE instruction.
instr_count = cctx->ctx_instr.ga_len;
instr = cctx->ctx_instr.ga_data;
instr[instr_count].isn_type = ISN_FINISH;
cctx->ctx_instr = save_ga;
if ((isn = generate_instr(cctx, ISN_SUBSTITUTE)) == NULL)
{
int idx;
for (idx = 0; idx < instr_count; ++idx)
delete_instr(instr + idx);
vim_free(instr);
return NULL;
}
isn->isn_arg.subs.subs_cmd = vim_strsave(arg);
isn->isn_arg.subs.subs_instr = instr;
// skip over flags
if (*end == '&')
++end;
while (ASCII_ISALPHA(*end) || *end == '#')
++end;
return end;
}
}
return compile_exec(arg, eap, cctx);
}
static char_u *
compile_redir(char_u *line, exarg_T *eap, cctx_T *cctx)
{
char_u *arg = eap->arg;
lhs_T *lhs = &cctx->ctx_redir_lhs;
if (lhs->lhs_name != NULL)
{
if (STRNCMP(arg, "END", 3) == 0)
{
if (lhs->lhs_append)
{
// First load the current variable value.
if (compile_load_lhs_with_index(lhs, lhs->lhs_whole,
cctx) == FAIL)
return NULL;
}
// Gets the redirected text and put it on the stack, then store it
// in the variable.
generate_instr_type(cctx, ISN_REDIREND, &t_string);
if (lhs->lhs_append)
generate_instr_drop(cctx, ISN_CONCAT, 1);
if (lhs->lhs_has_index)
{
// Use the info in "lhs" to store the value at the index in the
// list or dict.
if (compile_assign_unlet(lhs->lhs_whole, lhs, TRUE,
&t_string, cctx) == FAIL)
return NULL;
}
else if (generate_store_lhs(cctx, lhs, -1) == FAIL)
return NULL;
VIM_CLEAR(lhs->lhs_name);
VIM_CLEAR(lhs->lhs_whole);
return arg + 3;
}
emsg(_(e_cannot_nest_redir));
return NULL;
}
if (arg[0] == '=' && arg[1] == '>')
{
int append = FALSE;
// redirect to a variable is compiled
arg += 2;
if (*arg == '>')
{
++arg;
append = TRUE;
}
arg = skipwhite(arg);
if (compile_assign_lhs(arg, lhs, CMD_redir,
FALSE, FALSE, 1, cctx) == FAIL)
return NULL;
generate_instr(cctx, ISN_REDIRSTART);
lhs->lhs_append = append;
if (lhs->lhs_has_index)
{
lhs->lhs_whole = vim_strnsave(arg, lhs->lhs_varlen_total);
if (lhs->lhs_whole == NULL)
return NULL;
}
return arg + lhs->lhs_varlen_total;
}
// other redirects are handled like at script level
return compile_exec(line, eap, cctx);
}
#ifdef FEAT_QUICKFIX
static char_u *
compile_cexpr(char_u *line, exarg_T *eap, cctx_T *cctx)
{
isn_T *isn;
char_u *p;
isn = generate_instr(cctx, ISN_CEXPR_AUCMD);
if (isn == NULL)
return NULL;
isn->isn_arg.number = eap->cmdidx;
p = eap->arg;
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
isn = generate_instr(cctx, ISN_CEXPR_CORE);
if (isn == NULL)
return NULL;
isn->isn_arg.cexpr.cexpr_ref = ALLOC_ONE(cexprref_T);
if (isn->isn_arg.cexpr.cexpr_ref == NULL)
return NULL;
isn->isn_arg.cexpr.cexpr_ref->cer_cmdidx = eap->cmdidx;
isn->isn_arg.cexpr.cexpr_ref->cer_forceit = eap->forceit;
isn->isn_arg.cexpr.cexpr_ref->cer_cmdline = vim_strsave(skipwhite(line));
return p;
}
#endif
/*
* Check if the separator for a :global or :substitute command is OK.
*/
int
check_global_and_subst(char_u *cmd, char_u *arg)
{
if (arg == cmd + 1 && vim_strchr((char_u *)":-.", *arg) != NULL)
{
semsg(_(e_separator_not_supported_str), arg);
return FAIL;
}
if (VIM_ISWHITE(cmd[1]))
{
semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
return FAIL;
}
return OK;
}
/*
* Add a function to the list of :def functions.
* This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
*/
static int
add_def_function(ufunc_T *ufunc)
{
dfunc_T *dfunc;
if (def_functions.ga_len == 0)
{
// The first position is not used, so that a zero uf_dfunc_idx means it
// wasn't set.
if (GA_GROW_FAILS(&def_functions, 1))
return FAIL;
++def_functions.ga_len;
}
// Add the function to "def_functions".
if (GA_GROW_FAILS(&def_functions, 1))
return FAIL;
dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
CLEAR_POINTER(dfunc);
dfunc->df_idx = def_functions.ga_len;
ufunc->uf_dfunc_idx = dfunc->df_idx;
dfunc->df_ufunc = ufunc;
dfunc->df_name = vim_strsave(ufunc->uf_name);
ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
++dfunc->df_refcount;
++def_functions.ga_len;
return OK;
}
/*
* After ex_function() has collected all the function lines: parse and compile
* the lines into instructions.
* Adds the function to "def_functions".
* When "check_return_type" is set then set ufunc->uf_ret_type to the type of
* the return statement (used for lambda). When uf_ret_type is already set
* then check that it matches.
* When "profiling" is true add ISN_PROF_START instructions.
* "outer_cctx" is set for a nested function.
* This can be used recursively through compile_lambda(), which may reallocate
* "def_functions".
* Returns OK or FAIL.
*/
int
compile_def_function(
ufunc_T *ufunc,
int check_return_type,
compiletype_T compile_type,
cctx_T *outer_cctx)
{
char_u *line = NULL;
char_u *line_to_free = NULL;
char_u *p;
char *errormsg = NULL; // error message
cctx_T cctx;
garray_T *instr;
int did_emsg_before = did_emsg;
int did_emsg_silent_before = did_emsg_silent;
int ret = FAIL;
sctx_T save_current_sctx = current_sctx;
int save_estack_compiling = estack_compiling;
int save_cmod_flags = cmdmod.cmod_flags;
int do_estack_push;
int new_def_function = FALSE;
#ifdef FEAT_PROFILE
int prof_lnum = -1;
#endif
int debug_lnum = -1;
// When using a function that was compiled before: Free old instructions.
// The index is reused. Otherwise add a new entry in "def_functions".
if (ufunc->uf_dfunc_idx > 0)
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ ufunc->uf_dfunc_idx;
isn_T *instr_dest = NULL;
switch (compile_type)
{
case CT_PROFILE:
#ifdef FEAT_PROFILE
instr_dest = dfunc->df_instr_prof; break;
#endif
case CT_NONE: instr_dest = dfunc->df_instr; break;
case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
}
if (instr_dest != NULL)
// Was compiled in this mode before: Free old instructions.
delete_def_function_contents(dfunc, FALSE);
ga_clear_strings(&dfunc->df_var_names);
}
else
{
if (add_def_function(ufunc) == FAIL)
return FAIL;
new_def_function = TRUE;
}
ufunc->uf_def_status = UF_COMPILING;
CLEAR_FIELD(cctx);
cctx.ctx_compile_type = compile_type;
cctx.ctx_ufunc = ufunc;
cctx.ctx_lnum = -1;
cctx.ctx_outer = outer_cctx;
ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
cctx.ctx_type_list = &ufunc->uf_type_list;
ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
instr = &cctx.ctx_instr;
// Set the context to the function, it may be compiled when called from
// another script. Set the script version to the most modern one.
// The line number will be set in next_line_from_context().
current_sctx = ufunc->uf_script_ctx;
current_sctx.sc_version = SCRIPT_VERSION_VIM9;
// Don't use the flag from ":legacy" here.
cmdmod.cmod_flags &= ~CMOD_LEGACY;
// Make sure error messages are OK.
do_estack_push = !estack_top_is_ufunc(ufunc, 1);
if (do_estack_push)
estack_push_ufunc(ufunc, 1);
estack_compiling = TRUE;
if (ufunc->uf_def_args.ga_len > 0)
{
int count = ufunc->uf_def_args.ga_len;
int first_def_arg = ufunc->uf_args.ga_len - count;
int i;
char_u *arg;
int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
int did_set_arg_type = FALSE;
// Produce instructions for the default values of optional arguments.
SOURCING_LNUM = 0; // line number unknown
for (i = 0; i < count; ++i)
{
garray_T *stack = &cctx.ctx_type_stack;
type_T *val_type;
int arg_idx = first_def_arg + i;
where_T where = WHERE_INIT;
int r;
int jump_instr_idx = instr->ga_len;
isn_T *isn;
// Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
if (generate_JUMP_IF_ARG_SET(&cctx, i - count - off) == FAIL)
goto erret;
// Make sure later arguments are not found.
ufunc->uf_args_visible = arg_idx;
arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
r = compile_expr0(&arg, &cctx);
if (r == FAIL)
goto erret;
// If no type specified use the type of the default value.
// Otherwise check that the default value type matches the
// specified type.
val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
where.wt_index = arg_idx + 1;
if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
{
did_set_arg_type = TRUE;
ufunc->uf_arg_types[arg_idx] = val_type;
}
else if (check_type(ufunc->uf_arg_types[arg_idx], val_type,
TRUE, where) == FAIL)
goto erret;
if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
goto erret;
// set instruction index in JUMP_IF_ARG_SET to here
isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
isn->isn_arg.jumparg.jump_where = instr->ga_len;
}
if (did_set_arg_type)
set_function_type(ufunc);
}
ufunc->uf_args_visible = ufunc->uf_args.ga_len;
/*
* Loop over all the lines of the function and generate instructions.
*/
for (;;)
{
exarg_T ea;
int starts_with_colon = FALSE;
char_u *cmd;
cmdmod_T local_cmdmod;
// Bail out on the first error to avoid a flood of errors and report
// the right line number when inside try/catch.
if (did_emsg_before != did_emsg)
goto erret;
if (line != NULL && *line == '|')
// the line continues after a '|'
++line;
else if (line != NULL && *skipwhite(line) != NUL
&& !(*line == '#' && (line == cctx.ctx_line_start
|| VIM_ISWHITE(line[-1]))))
{
semsg(_(e_trailing_arg), line);
goto erret;
}
else if (line != NULL && vim9_bad_comment(skipwhite(line)))
goto erret;
else
{
line = next_line_from_context(&cctx, FALSE);
if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
{
// beyond the last line
#ifdef FEAT_PROFILE
if (cctx.ctx_skip != SKIP_YES)
may_generate_prof_end(&cctx, prof_lnum);
#endif
break;
}
// Make a copy, splitting off nextcmd and removing trailing spaces
// may change it.
if (line != NULL)
{
line = vim_strsave(line);
vim_free(line_to_free);
line_to_free = line;
}
}
CLEAR_FIELD(ea);
ea.cmdlinep = &line;
ea.cmd = skipwhite(line);
if (*ea.cmd == '#')
{
// "#" starts a comment
line = (char_u *)"";
continue;
}
#ifdef FEAT_PROFILE
if (cctx.ctx_compile_type == CT_PROFILE && cctx.ctx_lnum != prof_lnum
&& cctx.ctx_skip != SKIP_YES)
{
may_generate_prof_end(&cctx, prof_lnum);
prof_lnum = cctx.ctx_lnum;
generate_instr(&cctx, ISN_PROF_START);
}
#endif
if (cctx.ctx_compile_type == CT_DEBUG && cctx.ctx_lnum != debug_lnum
&& cctx.ctx_skip != SKIP_YES)
{
debug_lnum = cctx.ctx_lnum;
generate_instr_debug(&cctx);
}
cctx.ctx_prev_lnum = cctx.ctx_lnum + 1;
// Some things can be recognized by the first character.
switch (*ea.cmd)
{
case '}':
{
// "}" ends a block scope
scopetype_T stype = cctx.ctx_scope == NULL
? NO_SCOPE : cctx.ctx_scope->se_type;
if (stype == BLOCK_SCOPE)
{
compile_endblock(&cctx);
line = ea.cmd;
}
else
{
emsg(_(e_using_rcurly_outside_if_block_scope));
goto erret;
}
if (line != NULL)
line = skipwhite(ea.cmd + 1);
continue;
}
case '{':
// "{" starts a block scope
// "{'a': 1}->func() is something else
if (ends_excmd(*skipwhite(ea.cmd + 1)))
{
line = compile_block(ea.cmd, &cctx);
continue;
}
break;
}
/*
* COMMAND MODIFIERS
*/
cctx.ctx_has_cmdmod = FALSE;
if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE)
== FAIL)
{
if (errormsg != NULL)
goto erret;
// empty line or comment
line = (char_u *)"";
continue;
}
generate_cmdmods(&cctx, &local_cmdmod);
undo_cmdmod(&local_cmdmod);
// Check if there was a colon after the last command modifier or before
// the current position.
for (p = ea.cmd; p >= line; --p)
{
if (*p == ':')
starts_with_colon = TRUE;
if (p < ea.cmd && !VIM_ISWHITE(*p))
break;
}
// Skip ":call" to get to the function name, unless using :legacy
p = ea.cmd;
if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
{
if (checkforcmd(&ea.cmd, "call", 3))
{
if (*ea.cmd == '(')
// not for "call()"
ea.cmd = p;
else
ea.cmd = skipwhite(ea.cmd);
}
if (!starts_with_colon)
{
int assign;
// Check for assignment after command modifiers.
assign = may_compile_assignment(&ea, &line, &cctx);
if (assign == OK)
goto nextline;
if (assign == FAIL)
goto erret;
}
}
/*
* COMMAND after range
* 'text'->func() should not be confused with 'a mark
* "++nr" and "--nr" are eval commands
* in "$ENV->func()" the "$" is not a range
*/
cmd = ea.cmd;
if ((*cmd != '$' || starts_with_colon)
&& (starts_with_colon || !(*cmd == '\''
|| (cmd[0] == cmd[1] && (*cmd == '+' || *cmd == '-')))))
{
ea.cmd = skip_range(ea.cmd, TRUE, NULL);
if (ea.cmd > cmd)
{
if (!starts_with_colon
&& !(local_cmdmod.cmod_flags & CMOD_LEGACY))
{
semsg(_(e_colon_required_before_range_str), cmd);
goto erret;
}
ea.addr_count = 1;
if (ends_excmd2(line, ea.cmd))
{
// A range without a command: jump to the line.
generate_EXEC(&cctx, ISN_EXECRANGE,
vim_strnsave(cmd, ea.cmd - cmd));
line = ea.cmd;
goto nextline;
}
}
}
p = find_ex_command(&ea, NULL,
starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
? NULL : item_exists, &cctx);
if (p == NULL)
{
if (cctx.ctx_skip != SKIP_YES)
emsg(_(e_ambiguous_use_of_user_defined_command));
goto erret;
}
// When using ":legacy cmd" always use compile_exec().
if (local_cmdmod.cmod_flags & CMOD_LEGACY)
{
char_u *start = ea.cmd;
switch (ea.cmdidx)
{
case CMD_if:
case CMD_elseif:
case CMD_else:
case CMD_endif:
case CMD_for:
case CMD_endfor:
case CMD_continue:
case CMD_break:
case CMD_while:
case CMD_endwhile:
case CMD_try:
case CMD_catch:
case CMD_finally:
case CMD_endtry:
semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
goto erret;
default: break;
}
// ":legacy return expr" needs to be handled differently.
if (checkforcmd(&start, "return", 4))
ea.cmdidx = CMD_return;
else
ea.cmdidx = CMD_legacy;
}
if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
{
if (cctx.ctx_skip == SKIP_YES && ea.cmdidx != CMD_eval)
{
line += STRLEN(line);
goto nextline;
}
else if (ea.cmdidx != CMD_eval)
{
// CMD_var cannot happen, compile_assignment() above would be
// used. Most likely an assignment to a non-existing variable.
semsg(_(e_command_not_recognized_str), ea.cmd);
goto erret;
}
}
if (cctx.ctx_had_return
&& ea.cmdidx != CMD_elseif
&& ea.cmdidx != CMD_else
&& ea.cmdidx != CMD_endif
&& ea.cmdidx != CMD_endfor
&& ea.cmdidx != CMD_endwhile
&& ea.cmdidx != CMD_catch
&& ea.cmdidx != CMD_finally
&& ea.cmdidx != CMD_endtry)
{
emsg(_(e_unreachable_code_after_return));
goto erret;
}
p = skipwhite(p);
if (ea.cmdidx != CMD_SIZE
&& ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
{
if (ea.cmdidx >= 0)
ea.argt = excmd_get_argt(ea.cmdidx);
if ((ea.argt & EX_BANG) && *p == '!')
{
ea.forceit = TRUE;
p = skipwhite(p + 1);
}
}
switch (ea.cmdidx)
{
case CMD_def:
case CMD_function:
ea.arg = p;
line = compile_nested_function(&ea, &cctx);
break;
case CMD_return:
line = compile_return(p, check_return_type,
local_cmdmod.cmod_flags & CMOD_LEGACY, &cctx);
cctx.ctx_had_return = TRUE;
break;
case CMD_let:
emsg(_(e_cannot_use_let_in_vim9_script));
break;
case CMD_var:
case CMD_final:
case CMD_const:
case CMD_increment:
case CMD_decrement:
line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
if (line == p)
line = NULL;
break;
case CMD_unlet:
case CMD_unlockvar:
case CMD_lockvar:
line = compile_unletlock(p, &ea, &cctx);
break;
case CMD_import:
emsg(_(e_import_can_only_be_used_in_script));
line = NULL;
break;
case CMD_if:
line = compile_if(p, &cctx);
break;
case CMD_elseif:
line = compile_elseif(p, &cctx);
cctx.ctx_had_return = FALSE;
break;
case CMD_else:
line = compile_else(p, &cctx);
cctx.ctx_had_return = FALSE;
break;
case CMD_endif:
line = compile_endif(p, &cctx);
break;
case CMD_while:
line = compile_while(p, &cctx);
break;
case CMD_endwhile:
line = compile_endwhile(p, &cctx);
cctx.ctx_had_return = FALSE;
break;
case CMD_for:
line = compile_for(p, &cctx);
break;
case CMD_endfor:
line = compile_endfor(p, &cctx);
cctx.ctx_had_return = FALSE;
break;
case CMD_continue:
line = compile_continue(p, &cctx);
break;
case CMD_break:
line = compile_break(p, &cctx);
break;
case CMD_try:
line = compile_try(p, &cctx);
break;
case CMD_catch:
line = compile_catch(p, &cctx);
cctx.ctx_had_return = FALSE;
break;
case CMD_finally:
line = compile_finally(p, &cctx);
cctx.ctx_had_return = FALSE;
break;
case CMD_endtry:
line = compile_endtry(p, &cctx);
cctx.ctx_had_return = FALSE;
break;
case CMD_throw:
line = compile_throw(p, &cctx);
break;
case CMD_eval:
line = compile_eval(p, &cctx);
break;
case CMD_echo:
case CMD_echon:
case CMD_execute:
case CMD_echomsg:
case CMD_echoerr:
case CMD_echoconsole:
line = compile_mult_expr(p, ea.cmdidx, &cctx);
break;
case CMD_put:
ea.cmd = cmd;
line = compile_put(p, &ea, &cctx);
break;
case CMD_substitute:
if (check_global_and_subst(ea.cmd, p) == FAIL)
goto erret;
if (cctx.ctx_skip == SKIP_YES)
line = (char_u *)"";
else
{
ea.arg = p;
line = compile_substitute(line, &ea, &cctx);
}
break;
case CMD_redir:
ea.arg = p;
line = compile_redir(line, &ea, &cctx);
break;
case CMD_cexpr:
case CMD_lexpr:
case CMD_caddexpr:
case CMD_laddexpr:
case CMD_cgetexpr:
case CMD_lgetexpr:
#ifdef FEAT_QUICKFIX
ea.arg = p;
line = compile_cexpr(line, &ea, &cctx);
#else
ex_ni(&ea);
line = NULL;
#endif
break;
case CMD_append:
case CMD_change:
case CMD_insert:
case CMD_k:
case CMD_t:
case CMD_xit:
not_in_vim9(&ea);
goto erret;
case CMD_SIZE:
if (cctx.ctx_skip != SKIP_YES)
{
semsg(_(e_invalid_command_str), ea.cmd);
goto erret;
}
// We don't check for a next command here.
line = (char_u *)"";
break;
case CMD_lua:
case CMD_mzscheme:
case CMD_perl:
case CMD_py3:
case CMD_python3:
case CMD_python:
case CMD_pythonx:
case CMD_ruby:
case CMD_tcl:
ea.arg = p;
if (vim_strchr(line, '\n') == NULL)
line = compile_exec(line, &ea, &cctx);
else
// heredoc lines have been concatenated with NL
// characters in get_function_body()
line = compile_script(line, &cctx);
break;
case CMD_global:
if (check_global_and_subst(ea.cmd, p) == FAIL)
goto erret;
// FALLTHROUGH
default:
// Not recognized, execute with do_cmdline_cmd().
ea.arg = p;
line = compile_exec(line, &ea, &cctx);
break;
}
nextline:
if (line == NULL)
goto erret;
line = skipwhite(line);
// Undo any command modifiers.
generate_undo_cmdmods(&cctx);
if (cctx.ctx_type_stack.ga_len < 0)
{
iemsg("Type stack underflow");
goto erret;
}
}
if (cctx.ctx_scope != NULL)
{
if (cctx.ctx_scope->se_type == IF_SCOPE)
emsg(_(e_endif));
else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
emsg(_(e_endwhile));
else if (cctx.ctx_scope->se_type == FOR_SCOPE)
emsg(_(e_endfor));
else
emsg(_(e_missing_rcurly));
goto erret;
}
if (!cctx.ctx_had_return)
{
if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
ufunc->uf_ret_type = &t_void;
else if (ufunc->uf_ret_type->tt_type != VAR_VOID)
{
emsg(_(e_missing_return_statement));
goto erret;
}
// Return void if there is no return at the end.
generate_instr(&cctx, ISN_RETURN_VOID);
}
// When compiled with ":silent!" and there was an error don't consider the
// function compiled.
if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ ufunc->uf_dfunc_idx;
dfunc->df_deleted = FALSE;
dfunc->df_script_seq = current_sctx.sc_seq;
#ifdef FEAT_PROFILE
if (cctx.ctx_compile_type == CT_PROFILE)
{
dfunc->df_instr_prof = instr->ga_data;
dfunc->df_instr_prof_count = instr->ga_len;
}
else
#endif
if (cctx.ctx_compile_type == CT_DEBUG)
{
dfunc->df_instr_debug = instr->ga_data;
dfunc->df_instr_debug_count = instr->ga_len;
}
else
{
dfunc->df_instr = instr->ga_data;
dfunc->df_instr_count = instr->ga_len;
}
dfunc->df_varcount = dfunc->df_var_names.ga_len;
dfunc->df_has_closure = cctx.ctx_has_closure;
if (cctx.ctx_outer_used)
ufunc->uf_flags |= FC_CLOSURE;
ufunc->uf_def_status = UF_COMPILED;
}
ret = OK;
erret:
if (ufunc->uf_def_status == UF_COMPILING)
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ ufunc->uf_dfunc_idx;
// Compiling aborted, free the generated instructions.
clear_instr_ga(instr);
VIM_CLEAR(dfunc->df_name);
ga_clear_strings(&dfunc->df_var_names);
// If using the last entry in the table and it was added above, we
// might as well remove it.
if (!dfunc->df_deleted && new_def_function
&& ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
{
--def_functions.ga_len;
ufunc->uf_dfunc_idx = 0;
}
ufunc->uf_def_status = UF_COMPILE_ERROR;
while (cctx.ctx_scope != NULL)
drop_scope(&cctx);
if (errormsg != NULL)
emsg(errormsg);
else if (did_emsg == did_emsg_before)
emsg(_(e_compiling_def_function_failed));
}
if (cctx.ctx_redir_lhs.lhs_name != NULL)
{
if (ret == OK)
{
emsg(_(e_missing_redir_end));
ret = FAIL;
}
vim_free(cctx.ctx_redir_lhs.lhs_name);
vim_free(cctx.ctx_redir_lhs.lhs_whole);
}
current_sctx = save_current_sctx;
estack_compiling = save_estack_compiling;
cmdmod.cmod_flags = save_cmod_flags;
if (do_estack_push)
estack_pop();
vim_free(line_to_free);
free_imported(&cctx);
free_locals(&cctx);
ga_clear(&cctx.ctx_type_stack);
return ret;
}
void
set_function_type(ufunc_T *ufunc)
{
int varargs = ufunc->uf_va_name != NULL;
int argcount = ufunc->uf_args.ga_len;
// Create a type for the function, with the return type and any
// argument types.
// A vararg is included in uf_args.ga_len but not in uf_arg_types.
// The type is included in "tt_args".
if (argcount > 0 || varargs)
{
if (ufunc->uf_type_list.ga_itemsize == 0)
ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
argcount, &ufunc->uf_type_list);
// Add argument types to the function type.
if (func_type_add_arg_types(ufunc->uf_func_type,
argcount + varargs,
&ufunc->uf_type_list) == FAIL)
return;
ufunc->uf_func_type->tt_argcount = argcount + varargs;
ufunc->uf_func_type->tt_min_argcount =
argcount - ufunc->uf_def_args.ga_len;
if (ufunc->uf_arg_types == NULL)
{
int i;
// lambda does not have argument types.
for (i = 0; i < argcount; ++i)
ufunc->uf_func_type->tt_args[i] = &t_any;
}
else
mch_memmove(ufunc->uf_func_type->tt_args,
ufunc->uf_arg_types, sizeof(type_T *) * argcount);
if (varargs)
{
ufunc->uf_func_type->tt_args[argcount] =
ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
}
}
else
// No arguments, can use a predefined type.
ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
argcount, &ufunc->uf_type_list);
}
/*
* Delete an instruction, free what it contains.
*/
void
delete_instr(isn_T *isn)
{
switch (isn->isn_type)
{
case ISN_DEF:
case ISN_EXEC:
case ISN_EXECRANGE:
case ISN_EXEC_SPLIT:
case ISN_LEGACY_EVAL:
case ISN_LOADAUTO:
case ISN_LOADB:
case ISN_LOADENV:
case ISN_LOADG:
case ISN_LOADOPT:
case ISN_LOADT:
case ISN_LOADW:
case ISN_LOCKUNLOCK:
case ISN_PUSHEXC:
case ISN_PUSHFUNC:
case ISN_PUSHS:
case ISN_RANGE:
case ISN_STOREAUTO:
case ISN_STOREB:
case ISN_STOREENV:
case ISN_STOREG:
case ISN_STORET:
case ISN_STOREW:
case ISN_STRINGMEMBER:
vim_free(isn->isn_arg.string);
break;
case ISN_SUBSTITUTE:
{
int idx;
isn_T *list = isn->isn_arg.subs.subs_instr;
vim_free(isn->isn_arg.subs.subs_cmd);
for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
delete_instr(list + idx);
vim_free(list);
}
break;
case ISN_INSTR:
{
int idx;
isn_T *list = isn->isn_arg.instr;
for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
delete_instr(list + idx);
vim_free(list);
}
break;
case ISN_LOADS:
case ISN_STORES:
vim_free(isn->isn_arg.loadstore.ls_name);
break;
case ISN_UNLET:
case ISN_UNLETENV:
vim_free(isn->isn_arg.unlet.ul_name);
break;
case ISN_STOREOPT:
case ISN_STOREFUNCOPT:
vim_free(isn->isn_arg.storeopt.so_name);
break;
case ISN_PUSHBLOB: // push blob isn_arg.blob
blob_unref(isn->isn_arg.blob);
break;
case ISN_PUSHJOB:
#ifdef FEAT_JOB_CHANNEL
job_unref(isn->isn_arg.job);
#endif
break;
case ISN_PUSHCHANNEL:
#ifdef FEAT_JOB_CHANNEL
channel_unref(isn->isn_arg.channel);
#endif
break;
case ISN_UCALL:
vim_free(isn->isn_arg.ufunc.cuf_name);
break;
case ISN_FUNCREF:
{
if (isn->isn_arg.funcref.fr_func_name == NULL)
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ isn->isn_arg.funcref.fr_dfunc_idx;
ufunc_T *ufunc = dfunc->df_ufunc;
if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
func_ptr_unref(ufunc);
}
else
{
char_u *name = isn->isn_arg.funcref.fr_func_name;
if (name != NULL)
func_unref(name);
vim_free(isn->isn_arg.funcref.fr_func_name);
}
}
break;
case ISN_DCALL:
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ isn->isn_arg.dfunc.cdf_idx;
if (dfunc->df_ufunc != NULL
&& func_name_refcount(dfunc->df_ufunc->uf_name))
func_ptr_unref(dfunc->df_ufunc);
}
break;
case ISN_NEWFUNC:
{
char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
if (ufunc != NULL)
{
unlink_def_function(ufunc);
func_ptr_unref(ufunc);
}
vim_free(lambda);
vim_free(isn->isn_arg.newfunc.nf_global);
}
break;
case ISN_CHECKTYPE:
case ISN_SETTYPE:
free_type(isn->isn_arg.type.ct_type);
break;
case ISN_CMDMOD:
vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
->cmod_filter_regmatch.regprog);
vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
break;
case ISN_LOADSCRIPT:
case ISN_STORESCRIPT:
vim_free(isn->isn_arg.script.scriptref);
break;
case ISN_TRY:
vim_free(isn->isn_arg.try.try_ref);
break;
case ISN_CEXPR_CORE:
vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
vim_free(isn->isn_arg.cexpr.cexpr_ref);
break;
case ISN_2BOOL:
case ISN_2STRING:
case ISN_2STRING_ANY:
case ISN_ADDBLOB:
case ISN_ADDLIST:
case ISN_ANYINDEX:
case ISN_ANYSLICE:
case ISN_BCALL:
case ISN_BLOBAPPEND:
case ISN_BLOBINDEX:
case ISN_BLOBSLICE:
case ISN_CATCH:
case ISN_CEXPR_AUCMD:
case ISN_CHECKLEN:
case ISN_CHECKNR:
case ISN_CLEARDICT:
case ISN_CMDMOD_REV:
case ISN_COMPAREANY:
case ISN_COMPAREBLOB:
case ISN_COMPAREBOOL:
case ISN_COMPAREDICT:
case ISN_COMPAREFLOAT:
case ISN_COMPAREFUNC:
case ISN_COMPARELIST:
case ISN_COMPARENR:
case ISN_COMPARESPECIAL:
case ISN_COMPARESTRING:
case ISN_CONCAT:
case ISN_COND2BOOL:
case ISN_DEBUG:
case ISN_DROP:
case ISN_ECHO:
case ISN_ECHOCONSOLE:
case ISN_ECHOERR:
case ISN_ECHOMSG:
case ISN_ENDTRY:
case ISN_EXECCONCAT:
case ISN_EXECUTE:
case ISN_FINALLY:
case ISN_FINISH:
case ISN_FOR:
case ISN_GETITEM:
case ISN_JUMP:
case ISN_JUMP_IF_ARG_SET:
case ISN_LISTAPPEND:
case ISN_LISTINDEX:
case ISN_LISTSLICE:
case ISN_LOAD:
case ISN_LOADBDICT:
case ISN_LOADGDICT:
case ISN_LOADOUTER:
case ISN_LOADREG:
case ISN_LOADTDICT:
case ISN_LOADV:
case ISN_LOADWDICT:
case ISN_LOCKCONST:
case ISN_MEMBER:
case ISN_NEGATENR:
case ISN_NEWDICT:
case ISN_NEWLIST:
case ISN_OPANY:
case ISN_OPFLOAT:
case ISN_OPNR:
case ISN_PCALL:
case ISN_PCALL_END:
case ISN_PROF_END:
case ISN_PROF_START:
case ISN_PUSHBOOL:
case ISN_PUSHF:
case ISN_PUSHNR:
case ISN_PUSHSPEC:
case ISN_PUT:
case ISN_REDIREND:
case ISN_REDIRSTART:
case ISN_RETURN:
case ISN_RETURN_VOID:
case ISN_SHUFFLE:
case ISN_SLICE:
case ISN_STORE:
case ISN_STOREINDEX:
case ISN_STORENR:
case ISN_STOREOUTER:
case ISN_STORERANGE:
case ISN_STOREREG:
case ISN_STOREV:
case ISN_STRINDEX:
case ISN_STRSLICE:
case ISN_THROW:
case ISN_TRYCONT:
case ISN_UNLETINDEX:
case ISN_UNLETRANGE:
case ISN_UNPACK:
case ISN_USEDICT:
// nothing allocated
break;
}
}
/*
* Free all instructions for "dfunc" except df_name.
*/
static void
delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
{
int idx;
ga_clear(&dfunc->df_def_args_isn);
ga_clear_strings(&dfunc->df_var_names);
if (dfunc->df_instr != NULL)
{
for (idx = 0; idx < dfunc->df_instr_count; ++idx)
delete_instr(dfunc->df_instr + idx);
VIM_CLEAR(dfunc->df_instr);
dfunc->df_instr = NULL;
}
if (dfunc->df_instr_debug != NULL)
{
for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
delete_instr(dfunc->df_instr_debug + idx);
VIM_CLEAR(dfunc->df_instr_debug);
dfunc->df_instr_debug = NULL;
}
#ifdef FEAT_PROFILE
if (dfunc->df_instr_prof != NULL)
{
for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
delete_instr(dfunc->df_instr_prof + idx);
VIM_CLEAR(dfunc->df_instr_prof);
dfunc->df_instr_prof = NULL;
}
#endif
if (mark_deleted)
dfunc->df_deleted = TRUE;
if (dfunc->df_ufunc != NULL)
dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
}
/*
* When a user function is deleted, clear the contents of any associated def
* function, unless another user function still uses it.
* The position in def_functions can be re-used.
*/
void
unlink_def_function(ufunc_T *ufunc)
{
if (ufunc->uf_dfunc_idx > 0)
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ ufunc->uf_dfunc_idx;
if (--dfunc->df_refcount <= 0)
delete_def_function_contents(dfunc, TRUE);
ufunc->uf_def_status = UF_NOT_COMPILED;
ufunc->uf_dfunc_idx = 0;
if (dfunc->df_ufunc == ufunc)
dfunc->df_ufunc = NULL;
}
}
/*
* Used when a user function refers to an existing dfunc.
*/
void
link_def_function(ufunc_T *ufunc)
{
if (ufunc->uf_dfunc_idx > 0)
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ ufunc->uf_dfunc_idx;
++dfunc->df_refcount;
}
}
#if defined(EXITFREE) || defined(PROTO)
/*
* Free all functions defined with ":def".
*/
void
free_def_functions(void)
{
int idx;
for (idx = 0; idx < def_functions.ga_len; ++idx)
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
delete_def_function_contents(dfunc, TRUE);
vim_free(dfunc->df_name);
}
ga_clear(&def_functions);
}
#endif
#endif // FEAT_EVAL
|