rev: da1f575431d7ca6cee49846951add2b9b19e807b scopes/src/prover.cpp -rw-r--r-- 138.1 KiB View raw Log this file
da1f575431d7 — Leonard Ritter * prover.cpp: loop merge conflict error now points to loop header as source. Previously the error message was anchored to the return type that precedes the conflicting one. This makes sense for functions where the return type is inferred by the return points, but not for loops where we always know the type (from the header). 13 days ago
                                                                                
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
/*
    The Scopes Compiler Infrastructure
    This file is distributed under the MIT License.
    See LICENSE.md for details.
*/

#include "prover.hpp"
#include "value.hpp"
#include "types.hpp"
#include "qualifiers.hpp"
#include "error.hpp"
#include "stream_expr.hpp"
#include "hash.hpp"
#include "timer.hpp"
#include "gc.hpp"
#include "builtin.hpp"
#include "verify_tools.inc"
#include "dyn_cast.inc"
#include "compiler_flags.hpp"
#include "gen_llvm.hpp"
#include "list.hpp"
#include "expander.hpp"
#include "globals.hpp"
#include "quote.hpp"
#include "anchor.hpp"
#include "scopes/scopes.h"
#include "qualifier.inc"
#include "symbol_enum.inc"
#include "lifetime.hpp"

#include <algorithm>
#include <unordered_set>
#include <deque>

#pragma GCC diagnostic ignored "-Wvla-extension"
#pragma GCC diagnostic ignored "-Wgnu-statement-expression"

#define SCOPES_DEBUG_SYNTAX_EXTEND 0
#define SCOPES_ANNOTATE_TRACKING 1

// the old specializer is at
// https://bitbucket.org/duangle/scopes/raw/dfb69b02546e859b702176c58e92a63de3461d77/src/specializer.cpp

namespace scopes {

#define SCOPES_ARITH_OPS() \
    IARITH_NUW_NSW_OPS(Add) \
    IARITH_NUW_NSW_OPS(Sub) \
    IARITH_NUW_NSW_OPS(Mul) \
    \
    IARITH_OP(SDiv) \
    IARITH_OP(UDiv) \
    IARITH_OP(SRem) \
    IARITH_OP(URem) \
    \
    IARITH_OP(BAnd) \
    IARITH_OP(BOr) \
    IARITH_OP(BXor) \
    \
    IARITH_OP(Shl) \
    IARITH_OP(LShr) \
    IARITH_OP(AShr) \
    \
    FARITH_OP(FAdd) \
    FARITH_OP(FSub) \
    FARITH_OP(FMul) \
    FARITH_OP(FDiv) \
    FARITH_OP(FRem) \
    \
    IUN_OP(BitReverse) IUN_OP(BitCount) IUN_OP(FindMSB) IUN_OP(FindLSB) \
    \
    FUN_OP(FAbs) \
    \
    FUN_OP(FSign) \
    \
    FUN_OP(Radians) FUN_OP(Degrees) \
    FUN_OP(Sin) FUN_OP(Cos) FUN_OP(Tan) \
    FUN_OP(Asin) FUN_OP(Acos) FUN_OP(Atan) \
    FUN_OP(Sinh) FUN_OP(Cosh) FUN_OP(Tanh) \
    FUN_OP(ASinh) FUN_OP(ACosh) FUN_OP(ATanh) \
    FUN_OP(Exp) FUN_OP(Log) FUN_OP(Exp2) FUN_OP(Log2) \
    FUN_OP(Trunc) FUN_OP(Floor) FARITH_OP(Step) \
    FARITH_OP(Atan2) FARITH_OP(Pow) FUN_OP(Sqrt) \
    \
    FTRI_OP(FMix)

//------------------------------------------------------------------------------

namespace FunctionSet {
    struct Hash {
        std::size_t operator()(const Function *s) const {
            std::size_t h = std::hash<Function *>{}(s->frame.unref());
            h = hash2(h, std::hash<Template *>{}(s->original.unref()));
            for (auto arg : s->instance_args) {
                h = hash2(h, std::hash<const Type *>{}(arg));
            }
            return h;
        }
    };

    struct KeyEqual {
        bool operator()( const Function *lhs, const Function *rhs ) const {
            if (lhs->frame != rhs->frame) return false;
            if (lhs->original != rhs->original) return false;
            if (lhs->instance_args.size() != rhs->instance_args.size()) return false;
            for (size_t i = 0; i < lhs->instance_args.size(); ++i) {
                auto lparam = lhs->instance_args[i];
                auto rparam = rhs->instance_args[i];
                if (lparam != rparam) return false;
            }
            return true;
        }
    };
} // namespace FunctionSet

static std::unordered_set<Function *, FunctionSet::Hash, FunctionSet::KeyEqual> functions;

//------------------------------------------------------------------------------

static sc_typecast_func_t g_typecast_handler = nullptr;
void set_typecast_handler(sc_typecast_func_t func) {
    g_typecast_handler = func;
}

static bool has_typecast_handler() {
    return g_typecast_handler != nullptr;
}

static SCOPES_RESULT(TypedValueRef) run_typecast_handler(
    const ASTContext &ctx, const TypedValueRef &value, const Type *T) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    assert(g_typecast_handler);
    T = strip_qualifiers(T);
    auto result = g_typecast_handler(value, T);
    if (!result.ok) {
        SCOPES_RETURN_ERROR(result.except);
    } else {
        auto expr = result._0;
        return SCOPES_GET_RESULT(prove(ctx, expr));
    }
}

static bool typekinds_compatible(TypeKind k1, TypeKind k2) {
    if (k1 == k2) return true;
    if ((k1 == TK_Real) && (k2 == TK_Integer)) return true;
    if ((k1 == TK_Integer) && (k2 == TK_Real)) return true;
    if ((k1 == TK_Vector) && (k2 == TK_Integer)) return true;
    return false;
}

static SCOPES_RESULT(const ReferQualifier *) verify_refer(const Type *T) {
    SCOPES_RESULT_TYPE(const ReferQualifier *);
    auto rq = try_qualifier<ReferQualifier>(T);
    if (!rq) {
        SCOPES_ERROR(ValueMustBeReference, T);
    }
    return rq;
}

static SCOPES_RESULT(void) verify_readable(const ReferQualifier *Q, const Type *T) {
    SCOPES_RESULT_TYPE(void);
    if (!pointer_flags_is_readable(Q->flags)) {
        SCOPES_ERROR(NonReadableReference, T);
    }
    return {};
}

static SCOPES_RESULT(void) verify_writable(const ReferQualifier *Q, const Type *T) {
    SCOPES_RESULT_TYPE(void);
    if (!pointer_flags_is_writable(Q->flags)) {
        SCOPES_ERROR(NonWritableReference, T);
    }
    return {};
}

static SCOPES_RESULT(void) verify_readable(const Type *T) {
    SCOPES_RESULT_TYPE(void);
    auto pi = cast<PointerType>(T);
    if (!pi->is_readable()) {
        SCOPES_ERROR(NonReadablePointer, T);
    }
    return {};
}

static SCOPES_RESULT(void) verify_writable(const Type *T) {
    SCOPES_RESULT_TYPE(void);
    auto pi = cast<PointerType>(T);
    if (!pi->is_writable()) {
        SCOPES_ERROR(NonWritablePointer, T);
    }
    return {};
}

SCOPES_RESULT(ConstRef) nullof(const Type *T) {
    SCOPES_RESULT_TYPE(ConstRef);
    const Type *ST = SCOPES_GET_RESULT(storage_type(T));
    switch(ST->kind()) {
    case TK_Integer: return ConstRef(ConstInt::from(T, 0));
    case TK_Real: return ConstRef(ConstReal::from(T, 0.0));
    case TK_Pointer: return ConstRef(ConstPointer::from(T, nullptr));
    case TK_Array:
    case TK_Matrix: {
        auto at = cast<ArrayLikeType>(ST);
        ConstantPtrs fields;
        if (at->count()) {
            auto elem = SCOPES_GET_RESULT(nullof(at->element_type));
            for (size_t i = 0; i < at->count(); ++i) {
                fields.push_back(elem.unref());
            }
        }
        return ConstRef(ConstAggregate::from(T, fields));
    } break;
    case TK_Vector: {
        auto at = cast<VectorType>(ST);
        ConstantPtrs fields;
        if (at->count()) {
            auto elem = SCOPES_GET_RESULT(nullof(at->element_type));
            for (size_t i = 0; i < at->count(); ++i) {
                fields.push_back(elem.unref());
            }
        }
        return ConstRef(ConstAggregate::from(T, fields));
    } break;
    case TK_Tuple: {
        auto at = cast<TupleType>(ST);
        ConstantPtrs fields;
        for (auto valT : at->values) {
            fields.push_back(SCOPES_GET_RESULT(nullof(valT)).unref());
        }
        return ConstRef(ConstAggregate::from(T, fields));
    } break;
    default: {
        SCOPES_ERROR(CannotCreateConstantOf, T);
    } break;
    }
}

//------------------------------------------------------------------------------

static const ASTContext *ast_context = nullptr;

struct ScopedASTContext {
    ScopedASTContext(const ASTContext &ctx) {
        old_ctx = ast_context;
        ast_context = &ctx;
    }

    ~ScopedASTContext() {
        ast_context = old_ctx;
    }

    const ASTContext *old_ctx;
};

#define SCOPES_ASTCONTEXT(CTX) ScopedASTContext SCOPES_CAT(_scoped_ast_context, __LINE__)(CTX)

//------------------------------------------------------------------------------

ASTContext ASTContext::for_loop(const LoopLabelRef &loop) const {
    return ASTContext(function, frame, loop, except, _break, block);
}

ASTContext ASTContext::for_break(const LabelRef &xbreak) const {
    return ASTContext(function, frame, loop, except, xbreak, block);
}

ASTContext ASTContext::for_try(const LabelRef &except) const {
    return ASTContext(function, frame, loop, except, _break, block);
}

ASTContext ASTContext::with_block(Block &_block) const {
    return ASTContext(function, frame, loop, except, _break, &_block);
}

ASTContext ASTContext::with_frame(const FunctionRef &frame) const {
    return ASTContext(function, frame, loop, except, _break, block);
}

ASTContext::ASTContext() {}

ASTContext::ASTContext(const FunctionRef &_function, const FunctionRef &_frame,
    const LoopLabelRef &_loop, const LabelRef &_except,
    const LabelRef &xbreak, Block *_block) :
    function(_function), frame(_frame), loop(_loop),
    except(_except), _break(xbreak), block(_block) {
}

const Type *ASTContext::fix_merge_type(const Type *T) const {
    if (!is_returning_value(T))
        return T;
    int count = get_argument_count(T);
    Types newtypes;
    newtypes.reserve(count);
    for (int i = 0; i < count; ++i) {
        auto argT = get_argument(T, i);
        if (!is_view(argT) && !is_plain(argT)) {
            argT = unique_type(argT, unique_id());
        }
        newtypes.push_back(argT);
    }
    return arguments_type(newtypes);
}

int ASTContext::unique_id() const {
    return function->unique_id();
}

void ASTContext::move(int id, const ValueRef &mover) const {
    block->move(id);
    function->hint_mover(id, mover);
}

void ASTContext::merge_block(Block &_block) const {
    block->migrate_from(_block);
}

SCOPES_RESULT(void) ASTContext::append(const InstructionRef &value) const {
    //SCOPES_RESULT_TYPE(void);
    assert(block);
    block->append(value);
    //SCOPES_CHECK_RESULT(tag_instruction(*this, value));
    function->try_bind_unique(value);
    return {};
}

SCOPES_RESULT(void) ASTContext::unchecked_append(const InstructionRef &value) const {
    //SCOPES_RESULT_TYPE(void);
    assert(block);
    block->append(value);
    //SCOPES_CHECK_RESULT(tag_instruction(*this, value));
    return {};
}

ASTContext ASTContext::from_function(const FunctionRef &fn) {
    return ASTContext(fn, fn, LoopLabelRef(), LabelRef(), LabelRef(), nullptr);
}

SCOPES_RESULT(InstructionRef) ASTContext::build_deref(const Anchor *anchor,
    const TypedValueRef &value) const {
    SCOPES_RESULT_TYPE(InstructionRef);
    auto op = ref(anchor, RefToPtr::from(value));
    SCOPES_CHECK_RESULT(unchecked_append(op));
    return InstructionRef(anchor, Load::from(op));
}

SCOPES_RESULT(InstructionRef) ASTContext::build_assign(const Anchor *anchor,
    const TypedValueRef &value, const TypedValueRef &target) const {
    SCOPES_RESULT_TYPE(InstructionRef);
    auto op = ref(anchor, RefToPtr::from(target));
    SCOPES_CHECK_RESULT(unchecked_append(op));
    return InstructionRef(anchor, Store::from(value, op));
}

SCOPES_RESULT(InstructionRef) ASTContext::build_getelementref(const Anchor *anchor,
    const TypedValueRef &value, const TypedValues &indices) const {
    SCOPES_RESULT_TYPE(InstructionRef);
    auto op = ref(anchor, RefToPtr::from(value));
    SCOPES_CHECK_RESULT(unchecked_append(op));
    TypedValues idxs;
    idxs.reserve(indices.size() + 1);
    idxs.push_back(ConstInt::from(TYPE_I32, 0));
    for (auto val : indices) {
        idxs.push_back(val);
    }
    auto op2 = ref(anchor, GetElementPtr::from(op, idxs));
    SCOPES_CHECK_RESULT(unchecked_append(op2));
    return InstructionRef(anchor, PtrToRef::from(op2));
}

//------------------------------------------------------------------------------

static SCOPES_RESULT(TypedValueRef) prove_inline(const ASTContext &ctx,
    const Closure *cl, const TypedValues &nodes);

static const Type *merge_single_value_type(const char *context, const Type *T1, const Type *T2) {
    assert(T1);
    assert(T2);
    if (T1 == T2)
        return T1;
    if (is_unique(T1) && is_unique(T2)
        && (strip_unique(T1) == strip_unique(T2))) {
        // return stand-in unique tag
        return unique_type(T1, UnknownUnique);
    }
    auto vq = try_view(T2);
    // is either value a view?
    if ((vq || is_view(T1))
        && (strip_view(T1) == strip_view(T2))) {
        // merge all ids
        return vq?view_type(T1, vq->ids):T1;
    }
    return nullptr;
}

static SCOPES_RESULT(const Type *) merge_value_type(
    const char *context, const Type *T1, const Type *T2,
    const Anchor *A1, const Anchor *A2) {
    SCOPES_RESULT_TYPE(const Type *);
    assert(T2);
    if (!T1)
        return T2;
    if (T1 == T2)
        return T1;
    if (!is_returning(T1))
        return T2;
    if (!is_returning(T2))
        return T1;
    auto count = get_argument_count(T1);
    Types newargs;
    if (get_argument_count(T2) == count) {
        for (int i = 0; i < count; ++i) {
            auto argT1 = get_argument(T1, i);
            auto argT2 = get_argument(T2, i);
            const Type *T = merge_single_value_type(context, argT1, argT2);
            if (!T) {
                SCOPES_ERROR(MergeConflict, context, T1, T2, A1, A2);
            }
            newargs.push_back(T);
        }
        return arguments_type(newargs);
    }
    SCOPES_ERROR(MergeConflict, context, T1, T2, A1, A2);
}

SCOPES_RESULT(TypedValueRef) prove_block(const ASTContext &ctx, Block &block,
    const ValueRef &node, ASTContext &newctx) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    block.set_parent(ctx.block);
    newctx = ctx.with_block(block);
    auto result = SCOPES_GET_RESULT(prove(newctx, node));
    return result;
}

static bool split_return_values(TypedValues &values, const TypedValueRef &value) {
    auto T = value->get_type();
    if (!is_returning(T)) return false;
    auto count = get_argument_count(T);
    for (int i = 0; i < count; ++i) {
        values.push_back(ExtractArgument::from(value, i));
    }
    return true;
}

void map_arguments_to_block(const ASTContext &ctx, const TypedValueRef &src) {
    const Type *T = src->get_type();
    int count = get_argument_count(T);
    for (int i = 0; i < count; ++i) {
        auto argT = get_argument(T, i);
        auto uq = try_unique(argT);
        if (uq) {
            ctx.function->bind_unique(Function::UniqueInfo(ValueIndex(src, i)));
            ctx.block->valid.insert(uq->id);
        }
    }
}

static void write_annotation(const ASTContext &ctx,
    const Anchor *anchor, const String *msg, Values values) {
    values.insert(values.begin(),
        ref(anchor, ConstPointer::string_from(msg)));
    auto expr = ref(anchor,
            CallTemplate::from(
                ref(anchor, ConstInt::builtin_from(Builtin(FN_Annotate))),
                values));
    auto result = prove(ctx, expr);
    if (!result.ok()) {
        print_error(result.assert_error());
        assert(false && "error while annotating");
    }
}

static SCOPES_RESULT(void) verify_valid(const ASTContext &ctx, int id, const char *by) {
    SCOPES_RESULT_TYPE(void);
    if (id == GlobalUnique)
        return {};
    if (!ctx.block->is_valid(id)) {
        auto info = ctx.function->get_unique_info(id);
        SCOPES_ERROR(InaccessibleValue, info.value.get_type(),
            ctx.function->get_best_mover_anchor(id));
    }
    return {};
}

static SCOPES_RESULT(void) verify_valid(const ASTContext &ctx, const IDSet &ids, const char *by) {
    SCOPES_RESULT_TYPE(void);
    for (auto id : ids) {
        SCOPES_CHECK_RESULT(verify_valid(ctx, id, by));
    }
    return {};
}

static SCOPES_RESULT(void) verify_valid(const ASTContext &ctx, const TypedValueRef &val, const char *by) {
    SCOPES_RESULT_TYPE(void);
    auto T = val->get_type();
    if (!is_returning_value(T))
        return {};
    int id = 0;
    if (!ctx.block->is_valid(ValueIndex(val), id)) {
        SCOPES_ERROR(InaccessibleValue, T,
            ctx.function->get_best_mover_anchor(id));
    }
    return {};
}

static SCOPES_RESULT(void) verify_valid(const ASTContext &ctx, const TypedValues &values, const char *by) {
    SCOPES_RESULT_TYPE(void);
    for (auto &&value : values) {
        SCOPES_TRACE_PROVE_ARG_LIFETIME(value);
        SCOPES_CHECK_RESULT(verify_valid(ctx, value, by));
    }
    return {};
}

static SCOPES_RESULT(void) build_view(
    const ASTContext &ctx, const Anchor *anchor, TypedValueRef &val) {
    SCOPES_RESULT_TYPE(void);
    auto T = val->get_type();
    auto uq = try_unique(T);
    if (uq) {
        assert(ctx.block->is_valid(ValueIndex(val)));
        auto retT = view_type(T, {});
        auto call = ref(anchor, Cast::from(CastBitcast, val, retT));
        SCOPES_CHECK_RESULT(ctx.append(call));
        val = call;
    }
    return {};
}

static SCOPES_RESULT(TypedValueRef) build_drop(const ASTContext &ctx,
    const Anchor *anchor, const ValueIndex &arg) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    // generate destructor
    auto argT = arg.get_type();
    IDSet ids;
    auto uq = try_unique(argT);
    if (uq) {
        ids.insert(uq->id);
    } else {
        auto vq = try_view(argT);
        if (vq) {
            ids = vq->ids;
        }
    }
    argT = strip_qualifiers(argT);
    ValueRef handler;
    if (!argT->lookup(SYM_DropHandler, handler)) {
        #if SCOPES_ANNOTATE_TRACKING
        StyledString ss;
        ss.out << "forget";
        write_annotation(ctx, anchor, ss.str(), {
            ref(anchor, ConstPointer::type_from(arg.get_type())) });
        #endif
        return ref(anchor, ArgumentList::from({}));
    } else {
        #if SCOPES_ANNOTATE_TRACKING
        StyledString ss;
        ss.out << "destruct";
        write_annotation(ctx, anchor, ss.str(), {
            ref(anchor, ConstPointer::type_from(arg.get_type())) });
        #endif
        auto val = ref(anchor, ExtractArgument::from(arg.value, arg.index));
        SCOPES_CHECK_RESULT(build_view(ctx, anchor, val));
        auto expr =
            ref(anchor, CallTemplate::from(handler, { val }));
        auto result = SCOPES_GET_RESULT(prove(ctx, expr));
        auto RT = result->get_type();
        if (!is_returning(RT) || is_returning_value(RT)) {
            SCOPES_ERROR(DropReturnsArguments);
        }
        SCOPES_CHECK_RESULT(verify_valid(ctx, ids, "drop"));
        return result;
    }
}

static SCOPES_RESULT(void) drop_value(const ASTContext &ctx,
    const ValueRef &mover, const ValueIndex &arg) {
    SCOPES_RESULT_TYPE(void);
    if (ctx.block->is_valid(arg)) {
        auto anchor = mover.anchor();
        SCOPES_CHECK_RESULT(build_drop(ctx, anchor, arg));
        auto argT = arg.get_type();
        int id = get_unique(argT)->id;
        #if 0
        if (needs_autofree(argT)) {
            build_free(ctx, anchor, ref(anchor, ExtractArgument::from(arg.value, arg.index)));
        }
        #endif
        ctx.move(id, ref(anchor, mover));
    }
    return {};
}

static SCOPES_RESULT(void) build_move(
    const ASTContext &ctx, const ValueRef &mover, TypedValueRef &val) {
    SCOPES_RESULT_TYPE(void);
    assert(ctx.block->is_valid(ValueIndex(val)));
    auto anchor = mover.anchor();
    auto T = val->get_type();
    auto uq = get_unique(T);
    auto retT = unique_type(T, ctx.unique_id());
    auto call = ref(anchor, Cast::from(CastBitcast, val, retT));
    SCOPES_CHECK_RESULT(ctx.append(call));
    ctx.move(uq->id, mover);
    val = call;
    return {};
}

static SCOPES_RESULT(void) validate_pass_block(const ASTContext &ctx, const Block &src) {
    SCOPES_RESULT_TYPE(void);
    // see if pass deleted any values
    IDSet deleted = difference_idset(ctx.block->valid, src.valid);
    if (!deleted.empty()) {
        int id = *deleted.begin();
        auto info = ctx.function->get_unique_info(id);
        assert (info.get_depth() < src.depth);
        SCOPES_ERROR(SwitchPassMovedValue, info.value.get_type());
    }
    return {};
}

static void merge_back_valid(const ASTContext &ctx, IDSet &valid, const ValueRef &mover) {
    IDSet deleted = difference_idset(ctx.block->valid, valid);
    for (auto id : deleted) {
        ctx.move(id, mover);
        #if SCOPES_ANNOTATE_TRACKING
        StyledString ss;
        ss.out << "merge-forgetting " << id;
        write_annotation(ctx, unknown_anchor(), ss.str(), {});
        #endif
    }
}

// from a parent set of valid values, only keep the ones in both sets
static void collect_valid_values(const ASTContext &ctx, IDSet &valid) {
    valid = intersect_idset(valid, ctx.block->valid);
}

// from a parent set of valid values, only keep the ones in both sets
static void collect_valid_function_values(const ASTContext &ctx) {
    ctx.function->valid = intersect_idset(ctx.function->valid, ctx.block->valid);
}

// check if all merge view arguments are still valid
static SCOPES_RESULT(void) validate_merge_values(const IDSet &valid, const TypedValues &values) {
    SCOPES_RESULT_TYPE(void);
    for (auto &&value : values) {
        auto vq = try_view(value->get_type());
        if (!vq) continue;
        for (auto id : vq->ids) {
            if (id == GlobalUnique)
                continue;
            if (!valid.count(id)) {
                SCOPES_ERROR(ViewExitingScope, value->get_type());
            }
        }
    }
    return {};
}

static void sort_drop_ids(const IDSet &invalid, IDs &drop_ids) {
    drop_ids.reserve(invalid.size());
    for (auto id : invalid) {
        drop_ids.push_back(id);
    }
    // drop newest IDs first
    std::sort(drop_ids.rbegin(), drop_ids.rend());
}

static SCOPES_RESULT(void) drop_values(const ASTContext &ctx,
    const ValueRef &mover, const IDSet &todrop) {
    SCOPES_RESULT_TYPE(void);
    IDs drop_ids;
    drop_ids.reserve(todrop.size());
    sort_drop_ids(todrop, drop_ids);
    auto anchor = mover.anchor();
    for (auto &&id : drop_ids) {
        if (!ctx.block->is_valid(id)) // already moved
            continue;
        #if SCOPES_ANNOTATE_TRACKING
        StyledString ss;
        ss.out << "dropping " << id;
        write_annotation(ctx, anchor, ss.str(), {});
        #endif
        auto info = ctx.function->get_unique_info(id);
        SCOPES_CHECK_RESULT(drop_value(ctx, mover, info.value));
    }
    return {};
}

static SCOPES_RESULT(void) move_merge_values(const ASTContext &ctx,
    const ValueRef &mover, int retdepth, TypedValues &values, const char *by) {
    SCOPES_RESULT_TYPE(void);
    assert(retdepth >= 0);
    IDSet saved;
    for (auto &&value : values) {
        SCOPES_CHECK_RESULT(verify_valid(ctx, value, by));
        auto T = value->get_type();
        auto uq = try_unique(T);
        if (uq) {
            auto info = ctx.function->get_unique_info(uq->id);
            int depth = info.get_depth();
            if (depth <= retdepth) {
                // must move
                SCOPES_CHECK_RESULT(build_move(ctx, mover, value));
                T = value->get_type();
                uq = get_unique(T);
            }
            // must save
            saved.insert(uq->id);
            continue;
        }
        auto vq = try_view(T);
        if (vq) {
            for (auto &&id : vq->ids) {
                if (id == GlobalUnique)
                    continue;
                auto info = ctx.function->get_unique_info(id);
                int depth = info.get_depth();
                if (depth > retdepth) {
                    // cannot move id of value that is going to be deleted
                    SCOPES_ERROR(ViewExitingScope, value->get_type());
                } else {
                    // will still be live
                }
            }
            continue;
        }
    }

    {
        // auto-drop all locally valid uniques
        Block *block = ctx.block;
        assert(block);
        IDSet valid = block->valid;
        IDSet todrop;
        for (auto id : valid) {
            if (saved.count(id))
                continue;
            auto info = ctx.function->get_unique_info(id);
            if (info.get_depth() <= retdepth)
                continue;
            todrop.insert(id);
        }
        SCOPES_CHECK_RESULT(drop_values(ctx, mover, todrop));
    }

    return {};
}

static SCOPES_RESULT(TypedValueRef) move_single_merge_value(const ASTContext &ctx,
    int retdepth, TypedValueRef result, const char *by) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    TypedValues values;
    if (is_returning_value(result->get_type())
        && split_return_values(values, result)) {
        SCOPES_CHECK_RESULT(move_merge_values(ctx, result, retdepth, values, by));
        result = ref(result.anchor(), ArgumentList::from(values));
    } else {
        SCOPES_CHECK_RESULT(move_merge_values(ctx, result, retdepth, values, by));
    }
    return result;
}

// must be called before the return type is computed
// don't forget to call merge_back_invalid(...) when the label has been added
SCOPES_RESULT(void) finalize_merges(const ASTContext &ctx,
    const LabelRef &label, IDSet &valid, const char *by) {
    SCOPES_RESULT_TYPE(void);
    valid = ctx.block->valid;
    // patch merges
    int retdepth = label->body.depth - 1;
    for (auto merge : label->merges) {
        auto mctx = ctx.with_block(*merge->block);
        assert(merge->block);
        SCOPES_CHECK_RESULT(move_merge_values(mctx, merge, retdepth,
            merge->values, by));
        collect_valid_values(mctx, valid);
    }
    // deleted values
    IDSet deleted = difference_idset(ctx.block->valid, valid);
    for (auto merge : label->merges) {
        auto mctx = ctx.with_block(*merge->block);
        SCOPES_CHECK_RESULT(validate_merge_values(valid, merge->values));
        // values to drop: deleted values which are still alive in merge block
        IDSet todrop = intersect_idset(deleted, merge->block->valid);
        SCOPES_CHECK_RESULT(drop_values(mctx, merge, todrop));
    }
    return {};
}

SCOPES_RESULT(void) finalize_repeats(const ASTContext &ctx,
    const LoopLabelRef &label, const char *by) {
    SCOPES_RESULT_TYPE(void);
    IDSet valid = ctx.block->valid;
    // patch repeats
    int retdepth = label->body.depth - 1;
    for (auto merge : label->repeats) {
        auto mctx = ctx.with_block(*merge->block);
        assert(merge->block);
        SCOPES_CHECK_RESULT(move_merge_values(mctx,
            merge, retdepth, merge->values, by));
        collect_valid_values(mctx, valid);
    }
    IDSet deleted = difference_idset(ctx.block->valid, valid);
    if (!deleted.empty()) {
        // parent values were deleted, which we can't repeat
        int id = *deleted.begin();
        auto info = ctx.function->get_unique_info(id);
        SCOPES_ERROR(LoopMovedValue, info.value.get_type());
    }
    for (auto merge : label->repeats) {
        SCOPES_CHECK_RESULT(validate_merge_values(valid, merge->values));
    }
    return {};
}

SCOPES_RESULT(void) finalize_returns_raises(const ASTContext &ctx) {
    SCOPES_RESULT_TYPE(void);
    const IDSet &valid = ctx.function->valid;
    IDSet deleted = difference_idset(
        ctx.function->original_valid, valid);
    for (auto merge : ctx.function->returns) {
        auto mctx = ctx.with_block(*merge->block);
        SCOPES_CHECK_RESULT(validate_merge_values(valid, merge->values));
        IDSet todrop = intersect_idset(deleted, merge->block->valid);
        SCOPES_CHECK_RESULT(drop_values(mctx, merge, todrop));
    }
    for (auto merge : ctx.function->raises) {
        auto mctx = ctx.with_block(*merge->block);
        SCOPES_CHECK_RESULT(validate_merge_values(valid, merge->values));
        IDSet todrop = intersect_idset(deleted, merge->block->valid);
        SCOPES_CHECK_RESULT(drop_values(mctx, merge, todrop));
    }
    return {};
}

static SCOPES_RESULT(TypedValueRef) make_merge1(const ASTContext &ctx, const Anchor *anchor, const LabelRef &label, const TypedValues &values) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    assert(label);

    auto newmerge = ref(anchor, Merge::from(label, values));

    SCOPES_CHECK_RESULT(ctx.append(newmerge));
    label->merges.push_back(newmerge);

    return TypedValueRef(newmerge);
}

static SCOPES_RESULT(TypedValueRef) make_merge(const ASTContext &ctx, const Anchor *anchor, const LabelRef &label, const TypedValueRef &value) {
    TypedValues results;
    if (split_return_values(results, value)) {
        return make_merge1(ctx, anchor, label, results);
    } else {
        return value;
    }
}

static SCOPES_RESULT(TypedValueRef) make_repeat1(const ASTContext &ctx, const Anchor *anchor, const LoopLabelRef &label, const TypedValues &values) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    assert(label);

    auto newrepeat = ref(anchor, Repeat::from(label, values));

    SCOPES_CHECK_RESULT(ctx.append(newrepeat));
    label->repeats.push_back(newrepeat);

    return TypedValueRef(newrepeat);
}


static SCOPES_RESULT(TypedValueRef) make_repeat(const ASTContext &ctx, const Anchor *anchor, const LoopLabelRef &label, const TypedValueRef &value) {
    TypedValues results;
    if (split_return_values(results, value)) {
        return make_repeat1(ctx, anchor, label, results);
    } else {
        return value;
    }
}

static SCOPES_RESULT(void) build_deref_automove(
    const ASTContext &ctx, const ValueRef &mover, TypedValueRef &val);

static SCOPES_RESULT(TypedValueRef) make_return1(
    const ASTContext &ctx, const ValueRef &mover, TypedValues values) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    assert(ctx.block);
    assert(ctx.function);
    auto anchor = mover.anchor();

    for (size_t i = 0; i < values.size(); ++i) {
        auto T = values[i]->get_type();
        auto rq = try_qualifier<ReferQualifier>(T);
        if (rq && (rq->storage_class == SYM_SPIRV_StorageClassFunction)) {
            SCOPES_CHECK_RESULT(build_deref_automove(ctx, mover, values[i]));
        }
    }

    SCOPES_CHECK_RESULT(move_merge_values(ctx, mover, 0, values, "return"));
    collect_valid_function_values(ctx);

    auto newreturn = ref(anchor, Return::from(values));

    SCOPES_CHECK_RESULT(ctx.append(newreturn));
    ctx.function->returns.push_back(newreturn);
    return TypedValueRef(newreturn);
}

static SCOPES_RESULT(TypedValueRef) make_return(const ASTContext &ctx,
    const ValueRef &mover, const TypedValueRef &value) {
    TypedValues results;
    if (split_return_values(results, value)) {
        return make_return1(ctx, mover, results);
    } else {
        return value;
    }
}

static SCOPES_RESULT(TypedValueRef) make_raise1(
    const ASTContext &ctx, const ValueRef &mover, TypedValues values) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    auto anchor = mover.anchor();
    if (ctx.except) {
        if (ctx.except->label_kind == LK_ExceptAll) {
            values.clear();
        }
        return make_merge1(ctx, anchor, ctx.except, values);
    } else {
        SCOPES_CHECK_RESULT(move_merge_values(ctx, mover, 0, values, "raise"));
        collect_valid_function_values(ctx);

        auto newraise = ref(anchor, Raise::from(values));

        SCOPES_CHECK_RESULT(ctx.append(newraise));
        ctx.function->raises.push_back(newraise);
        return TypedValueRef(newraise);
    }
}

static SCOPES_RESULT(TypedValueRef) make_raise(const ASTContext &ctx,
    const ValueRef &mover, const TypedValueRef &value) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    auto anchor = mover.anchor();
    #if 1
    if (ctx.block->tag_traceback) {
        if (value->get_type() == TYPE_Error) {
            // add info
            auto tracecall = ref(anchor, Call::from(empty_arguments_type(),
                g_sc_error_append_calltrace, {
                value, ConstAggregate::ast_from(value)
            }));
            SCOPES_CHECK_RESULT(ctx.append(tracecall));
        }
    }
    #endif
    TypedValues results;
    if (split_return_values(results, value)) {
        return make_raise1(ctx, mover, results);
    } else {
        return value;
    }
}

static SCOPES_RESULT(TypedValueRef) prove_LabelTemplate(const ASTContext &ctx, const LabelTemplateRef &node) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    LabelRef label = ref(node.anchor(), Label::from(node->label_kind, node->name));
    assert(ctx.frame);
    assert(ctx.block);
    ctx.frame->bind(node, label);
    TypedValueRef result;
    const char *by = "label merge";
    ASTContext labelctx;
    switch (label->label_kind) {
    case LK_ExceptAll:
    case LK_Except: {
        by = "exception";
        result = SCOPES_GET_RESULT(
            prove_block(ctx.for_try(label), label->body, node->value, labelctx));
    } break;
    case LK_Break: {
        by = "break";
        result = SCOPES_GET_RESULT(
            prove_block(ctx.for_break(label), label->body, node->value, labelctx));
    } break;
    case LK_Try:
        by = "try block";
    default: {
        result = SCOPES_GET_RESULT(
            prove_block(ctx, label->body, node->value, labelctx));
    } break;
    }
    assert(result);
    if (label->merges.empty()) {
        // label does not need a merge label
        assert(ctx.block);
        result = SCOPES_GET_RESULT(
            move_single_merge_value(labelctx, ctx.block->depth, result, by));
        ctx.merge_block(label->body);
        return result;
    } else {
        SCOPES_CHECK_RESULT(make_merge(labelctx, result.anchor(), label, result));
        #if 1
        if (label->body.empty()) {
            StyledStream ss;
            stream_value(ss, label);
            stream_value(ss, node);
            stream_value(ss, result);
        }
        #endif
        assert(!label->body.empty());
        IDSet valid;
        SCOPES_CHECK_RESULT(finalize_merges(ctx, label, valid, by));
        const Type *rtype = nullptr;
        const Anchor *last_anchor = result.anchor();
        for (auto merge : label->merges) {
            rtype = SCOPES_GET_RESULT(merge_value_type(by,
                rtype, arguments_type_from_typed_values(merge->values),
                last_anchor, merge.anchor()));
            last_anchor = merge.anchor();
        }
        rtype = ctx.fix_merge_type(rtype);
        label->change_type(rtype);
        merge_back_valid(ctx, valid, label);
        SCOPES_CHECK_RESULT(ctx.append(label));
        return TypedValueRef(label);
    }
}

static SCOPES_RESULT(TypedValueRef) prove_Expression(const ASTContext &ctx,
    const ExpressionRef &expr) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    int count = (int)expr->body.size();
    if (expr->scoped) {
        Block block;
        block.set_parent(ctx.block);
        ASTContext subctx = ctx.with_block(block);
        for (int i = 0; i < count; ++i) {
            auto newsrc = SCOPES_GET_RESULT(prove(subctx, expr->body[i]));
            if (!is_returning(newsrc->get_type())) {
                SCOPES_TRACE_PROVE_ARG(expr->body[i]);
                SCOPES_ERROR(PrematureReturnFromExpression);
            }
        }
        TypedValueRef result;
        if (expr->value) {
            result = SCOPES_GET_RESULT(prove(subctx, expr->value));
        } else {
            result = ref(expr.anchor(), ArgumentList::from({}));
        }
        result = SCOPES_GET_RESULT(
            move_single_merge_value(subctx, block.depth - 1, result,
                "expression block"));
        ctx.merge_block(block);
        return result;
    } else {
        for (int i = 0; i < count; ++i) {
            auto newsrc = SCOPES_GET_RESULT(prove(ctx, expr->body[i]));
            if (!is_returning(newsrc->get_type())) {
                SCOPES_TRACE_PROVE_ARG(expr->body[i]);
                SCOPES_ERROR(PrematureReturnFromExpression);
            }
        }
        if (!expr->value)
            return ref(expr.anchor(), ArgumentList::from({}));
        return SCOPES_GET_RESULT(prove(ctx, expr->value));
    }
}

static int find_key(const Symbols &symbols, Symbol key) {
    for (int i = 0; i < symbols.size(); ++i) {
        if (symbols[i] == key)
            return i;
    }
    return -1;
}

SCOPES_RESULT(void) map_keyed_arguments(const Anchor *anchor, const TypedValueRef &callee,
    TypedValues &outargs, const TypedValues &values, const Symbols &symbols, bool varargs) {
    SCOPES_RESULT_TYPE(void);
    outargs.reserve(values.size());
    std::vector<bool> mapped;
    mapped.reserve(values.size());
    size_t next_index = 0;
    for (size_t i = 0; i < values.size(); ++i) {
        TypedValueRef arg = values[i];
        auto kt = type_key(arg->get_type());
        Symbol key = kt._0;
        int index = -1;
        if (key == SYM_Unnamed) {
            // argument without key

            // find next argument that is unmapped
            while ((next_index < mapped.size()) && mapped[next_index])
                next_index++;
            // fill up argument slots until index
            while (mapped.size() <= next_index) {
                mapped.push_back(false);
                outargs.push_back(TypedValueRef());
            }
            index = next_index;
            next_index++;
        } else {
            // find desired parameter index of key
            auto ki = find_key(symbols, key);
            if (ki >= 0) {
                // parameter with key exists
                // fill up argument slots until index
                while (mapped.size() <= (size_t)ki) {
                    mapped.push_back(false);
                    outargs.push_back(TypedValueRef());
                }
                if (mapped[ki]) {
                    SCOPES_TRACE_PROVE_PARAM_MAP(callee);
                    SCOPES_ERROR(DuplicateParameterKey, key);
                }
                index = ki;
                // strip key from value
                arg = ref(anchor, Keyed::from(SYM_Unnamed, arg));
            } else if (varargs) {
                // no parameter with that name, but we accept varargs
                while (mapped.size() < symbols.size()) {
                    mapped.push_back(false);
                    outargs.push_back(TypedValueRef());
                }
                index = (int)outargs.size();
                mapped.push_back(false);
                outargs.push_back(TypedValueRef());
            } else {
                SCOPES_TRACE_PROVE_PARAM_MAP(callee);
                SCOPES_ERROR(UnknownParameterKey, key, symbols);
            }
        }
        mapped[index] = true;
        outargs[index] = arg;
    }
    TypedValueRef noneval;
    for (size_t i = 0; i < outargs.size(); ++i) {
        if (!outargs[i]) {
            if (!noneval) {
                noneval = ref(anchor, ConstAggregate::none_from());
            }
            outargs[i] = noneval;
        }
    }
    return {};
}

static SCOPES_RESULT(TypedValueRef) ensure_tracked(TypedValueRef value) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    auto T = value->get_type();
    if (is_plain(T))
        return value;
    auto uq = try_unique(T);
    if (uq) return value;
    auto vq = try_view(T);
    if (vq) return value;
    if (value.isa<Pure>()) {
        return TypedValueRef(ref(value.anchor(),
            PureCast::from(view_type(T, { GlobalUnique }),
            value.cast<Pure>())));
    }
    SCOPES_ERROR(UntrackedType, T);
}

// used by ArgumentList & Call
static SCOPES_RESULT(TypedValueRef) prove_arguments(
    const ASTContext &ctx, TypedValues &outargs, const Values &values) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    int count = (int)values.size();
    for (int i = 0; i < count; ++i) {
        auto value = SCOPES_GET_RESULT(prove(ctx, values[i]));
        const Type *T = value->get_type();
        if (!is_returning(T)) {
            return value;
        }
        if (is_arguments_type(T)) {
            if ((i + 1) == count) {
                // last argument is appended in full
                int valcount = get_argument_count(T);
                for (int j = 0; j < valcount; ++j) {
                    auto elem = ExtractArgument::from(value, j);
                    {
                        SCOPES_TRACE_PROVE_ARG(elem);
                        elem = SCOPES_GET_RESULT(ensure_tracked(elem));
                    }
                    outargs.push_back(elem);
                }
                break;
            } else {
                value = ExtractArgument::from(value, 0);
            }
        }
        {
            SCOPES_TRACE_PROVE_ARG(value);
            value = SCOPES_GET_RESULT(ensure_tracked(value));
        }
        outargs.push_back(ref(values[i].anchor(), value));
    }
    return TypedValueRef();
}

static SCOPES_RESULT(TypedValueRef) prove_ArgumentListTemplate(const ASTContext &ctx, const ArgumentListTemplateRef &nlist) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    TypedValues values;
    TypedValueRef noret = SCOPES_GET_RESULT(prove_arguments(ctx, values, nlist->values()));
    if (noret) {
        return noret;
    }
    return ref(nlist.anchor(), ArgumentList::from(values));
}

static SCOPES_RESULT(TypedValueRef) prove_ExtractArgumentTemplate(
    const ASTContext &ctx, const ExtractArgumentTemplateRef &node) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    auto value = SCOPES_GET_RESULT(prove(ctx, node->value));
    assert(node->index >= 0);
    if (node->vararg)
        return ref(node.anchor(), ExtractArgument::variadic_from(value, node->index));
    else
        return ref(node.anchor(), ExtractArgument::from(value, node->index));
}

static SCOPES_RESULT(TypedValueRef) prove_Loop(const ASTContext &ctx, const LoopRef &loop) {
    SCOPES_RESULT_TYPE(TypedValueRef);

    TypedValues init_values;
    auto noret = SCOPES_GET_RESULT(
        prove_arguments(ctx, init_values, { loop->init }));
    if (noret) return noret;

    Types loop_types;
    for (auto &&value : init_values) {
        SCOPES_CHECK_RESULT(verify_valid(ctx, value, "loop init"));
        auto T = value->get_type();
        auto uq = try_unique(T);
        if (uq) {
            ctx.move(uq->id, loop);
            // move into loop
            T = unique_type(T, ctx.unique_id());
        }
        loop_types.push_back(T);
    }

    auto loopargs = ref(loop.anchor(), LoopLabelArguments::from(
        arguments_type(loop_types)));

    LoopLabelRef newloop = ref(loop.anchor(),
        LoopLabel::from(init_values, loopargs));
    // anchor loop to the local block to avoid it landing in the wrong place
    // ctx.append(newloop);
    map_arguments_to_block(ctx.with_block(newloop->body), loopargs);
    ctx.frame->bind(loop->args, newloop->args);

    auto subctx = ctx.for_loop(newloop);
    ASTContext newctx;
    auto result = SCOPES_GET_RESULT(prove_block(subctx, newloop->body, loop->value, newctx));
    //auto rtype = result->get_type();
    SCOPES_CHECK_RESULT(make_repeat(newctx, result.anchor(), newloop, result));

    SCOPES_CHECK_RESULT(finalize_repeats(ctx, newloop, "loop repeat"));
    SCOPES_TRACE_PROVE_EXPR(loop);

    const Type *rtype = newloop->args->get_type();
    for (auto repeat : newloop->repeats) {
        SCOPES_CHECK_RESULT(merge_value_type("loop repeat", rtype,
            arguments_type_from_typed_values(repeat->values),
            newloop.anchor(), repeat.anchor()));
    }

    return TypedValueRef(newloop);
}

static SCOPES_RESULT(TypedValueRef) prove_LoopArguments(const ASTContext &ctx, const ValueRef &node) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    SCOPES_TRACE_PROVE_PARAM_MAP(node);
    SCOPES_ERROR(UnboundValue, node);
}
static SCOPES_RESULT(TypedValueRef) prove_ParameterTemplate(const ASTContext &ctx, const ValueRef &node) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    SCOPES_TRACE_PROVE_PARAM_MAP(node);
    SCOPES_ERROR(UnboundValue, node);
}

const Type *try_get_const_type(const ValueRef &node) {
    if (node.isa<Const>())
        return node.cast<Const>()->get_type();
    return TYPE_Unknown;
}

const String *try_extract_string(const ValueRef &node) {
    auto ptr = node.dyn_cast<ConstPointer>();
    if (ptr && (ptr->get_type() == TYPE_String))
        return (const String *)ptr->value;
    return nullptr;
}

static SCOPES_RESULT(TypedValueRef) prove_MergeTemplate(const ASTContext &ctx, const MergeTemplateRef &node) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    TypedValueRef label = SCOPES_GET_RESULT(ctx.frame->resolve(node->label, ctx.function));
    if (!label.isa<Label>()) {
        SCOPES_ERROR(LabelExpected, label->get_type());
    }
    TypedValueRef value = SCOPES_GET_RESULT(prove(ctx, node->value));
    if (!is_returning(value->get_type()))
        return value;
    return make_merge(ctx, node.anchor(), label.cast<Label>(), value);
}

bool is_value_stage_constant(const ValueRef &value) {
    return value.isa<Pure>() && (value.cast<Pure>()->get_type() != TYPE_ASTMacro);
}

static SCOPES_RESULT(TypedValueRef) prove_CompileStage(const ASTContext &ctx, const CompileStageRef &sx) {
    SCOPES_RESULT_TYPE(TypedValueRef);

    auto anchor = sx->anchor;
    auto scope = sx->env;

    assert(scope);
    auto parent = sc_scope_get_parent(scope);
    auto docstr = sc_scope_module_docstring(scope);

    const Scope *newscope = nullptr;
    if (parent) {
        newscope = sc_scope_new_subscope_with_docstring(parent, docstr);
    } else {
        newscope = sc_scope_new_with_docstring(docstr);
    }

    auto block = ref(anchor, Expression::unscoped_from());
    //StyledStream ss;
    {
        auto it = sc_scope_next_deleted(scope, -1);
        while (it._1 != -1) {
            // generate deletions
            newscope = sc_scope_unbind(newscope, it._0);
            it = sc_scope_next_deleted(scope, it._1);
        }
    }
    // generate constant insertions
    auto it = sc_scope_next(scope, -1);
    while (it._2 != -1) {
        // generate insertions
        auto key = it._0;
        auto untyped_value = it._1;
        auto keydocstr = sc_scope_docstring(scope, key);
        auto value = SCOPES_GET_RESULT(prove(ctx, untyped_value));

        if (is_value_stage_constant(value)) {
            if (sc_string_count(keydocstr))
                newscope = sc_scope_bind_with_docstring(newscope, key, value, keydocstr);
            else
                newscope = sc_scope_bind(newscope, key, value);
        }
        it = sc_scope_next(scope, it._2);
    }

    // generate variable insertions
    ValueRef tmp = ref(anchor, ConstPointer::scope_from(newscope));
    it = sc_scope_next(scope, -1);
    while (it._2 != -1) {
        // generate insertions
        auto key = it._0;
        auto untyped_value = it._1;
        auto keydocstr = sc_scope_docstring(scope, key);
        auto value = SCOPES_GET_RESULT(prove(ctx, untyped_value));

        if (!is_value_stage_constant(value)) {
            //ss << "assigning " << key << " " << value << std::endl;
            //ss << "assigning " << key << std::endl;

            auto value_anchor = value.anchor();

            int argc = sc_argcount(value);
            auto vkey = ref(anchor, Quote::from(key));
            if (argc == 1) {
                if (sc_string_count(keydocstr)) {
                    tmp = ref(anchor,
                        CallTemplate::from(g_sc_scope_bind_with_docstring, { tmp,  vkey,
                            ref(value_anchor, Quote::from(value)),
                            ref(anchor, ConstPointer::string_from(keydocstr))
                        }));
                } else {
                    tmp = ref(anchor,
                        CallTemplate::from(g_sc_scope_bind, { tmp,  vkey,
                            ref(value_anchor, Quote::from(value))
                        }));
                }
            } else {
                Values newvalues;
                newvalues.reserve(argc);
                for (int i = 0; i < argc; ++i) {
                    auto arg = sc_getarg(value, i);
                    auto arg_anchor = arg.anchor();
                    if (sc_value_type(arg) == TYPE_ValueRef) {
                        newvalues.push_back(
                            ref(arg_anchor, Quote::from(ref(arg_anchor, Quote::from(arg)))));
                    } else {
                        newvalues.push_back(
                            ref(arg_anchor, Quote::from(arg)));
                    }
                }
                auto outargs = build_quoted_argument_list(anchor, newvalues);
                if (sc_string_count(keydocstr)) {
                    tmp = ref(anchor,
                        CallTemplate::from(g_sc_scope_bind_with_docstring, { tmp, vkey, outargs,
                            ref(anchor, ConstPointer::string_from(keydocstr)) }));
                } else {
                    tmp = ref(anchor,
                        CallTemplate::from(g_sc_scope_bind, { tmp, vkey, outargs }));
                }
            }
            block->append(tmp);
        }

        it = sc_scope_next(scope, it._2);
    }
    //ss << "===" << std::endl;

    tmp = ref(anchor, CallTemplate::from(g_sc_scope_new_subscope, { tmp }));
    block->append(
        ref(anchor, CallTemplate::from(g_bitcast, {
            ref(anchor, CallTemplate::from(g_sc_eval_stage, {
                ref(anchor, ConstPointer::anchor_from(anchor)),
                ref(anchor, ConstPointer::list_from(sx->next)),
                tmp })),
            ref(anchor, ConstPointer::type_from(TYPE_CompileStage))
        }))
    );
    //StyledStream ss;
    //stream_ast(ss, block, StreamASTFormat());
    return prove(ctx, block);
}

static SCOPES_RESULT(TypedValueRef) prove_KeyedTemplate(const ASTContext &ctx,
    const KeyedTemplateRef &keyed) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    auto value = SCOPES_GET_RESULT(prove(ctx, keyed->value));
    //assert(!value.isa<ArgumentList>());
    return ref(keyed.anchor(), Keyed::from(keyed->key, value));
}

template<typename T, ValueKind kind>
static SCOPES_RESULT(TValueRef<T>) extract_constant(const ValueRef &value) {
    SCOPES_RESULT_TYPE(TValueRef<T>);
    auto constval = value.dyn_cast<T>();
    if (!constval) {
        SCOPES_ERROR(ConstantValueKindMismatch,
            kind, value->kind());
    }
    return constval;
}

SCOPES_RESULT(ConstRef) extract_constant(const ValueRef &value) {
    SCOPES_RESULT_TYPE(ConstRef);
    auto constval = value.dyn_cast<Const>();
    if (!constval) {
        SCOPES_ERROR(ConstantExpected, value->kind());
    }
    return constval;
}

template<typename T>
static SCOPES_RESULT(TValueRef<T>) extract_typed_constant(const Type *want, ValueRef value) {
    SCOPES_RESULT_TYPE(TValueRef<T>);
    const Type *TT = nullptr;
    if (value.isa<PureCast>()) {
        TT = value.cast<PureCast>()->get_type();
        value = ref(value.anchor(), value.cast<PureCast>()->value);
    }
    auto constval = value.dyn_cast<T>();
    if (!constval) {
        SCOPES_ERROR(TypedConstantValueKindMismatch, want, value->kind());
    }
    if (!TT) {
        TT = constval->get_type();
    }
    SCOPES_CHECK_RESULT(verify(TT, want));
    return constval;
}

SCOPES_RESULT(GlobalRef) extract_global_constant(ValueRef value) {
    SCOPES_RESULT_TYPE(GlobalRef);
    if (value.isa<PureCast>()) {
        value = ref(value.anchor(), value.cast<PureCast>()->value);
    }
    auto constval = value.dyn_cast<Global>();
    if (!constval) {
        SCOPES_ERROR(ConstantValueKindMismatch, VK_Global, value->kind());
    }
    return constval;
}

SCOPES_RESULT(const Type *) extract_type_constant(const ValueRef &value) {
    SCOPES_RESULT_TYPE(const Type *);
    ConstPointerRef x = SCOPES_GET_RESULT(extract_typed_constant<ConstPointer>(TYPE_Type, value));
    return (const Type *)x->value;
}

SCOPES_RESULT(const Closure *) extract_closure_constant(const ValueRef &value) {
    SCOPES_RESULT_TYPE(const Closure *);
    ConstPointerRef x = SCOPES_GET_RESULT(extract_typed_constant<ConstPointer>(TYPE_Closure, value));
    return (const Closure *)x->value;
}

SCOPES_RESULT(FunctionRef) extract_function_constant(const ValueRef &value) {
    return extract_constant<Function, VK_Function>(value);
}

SCOPES_RESULT(TemplateRef) extract_template_constant(const ValueRef &value) {
    return extract_constant<Template, VK_Template>(value);
}

SCOPES_RESULT(sc_ast_macro_func_t) extract_astmacro_constant(const ValueRef &value) {
    SCOPES_RESULT_TYPE(sc_ast_macro_func_t);
    ConstPointerRef x = SCOPES_GET_RESULT(extract_typed_constant<ConstPointer>(TYPE_ASTMacro, value));
    return (sc_ast_macro_func_t)x->value;
}

SCOPES_RESULT(const List *) extract_list_constant(const ValueRef &value) {
    SCOPES_RESULT_TYPE(const List *);
    ConstPointerRef x = SCOPES_GET_RESULT(extract_typed_constant<ConstPointer>(TYPE_List, value));
    return (const List *)x->value;
}

SCOPES_RESULT(const String *) extract_string_constant(const ValueRef &value) {
    SCOPES_RESULT_TYPE(const String *);
    ConstPointerRef x = SCOPES_GET_RESULT(extract_typed_constant<ConstPointer>(TYPE_String, value));
    return (const String *)x->value;
}

SCOPES_RESULT(Builtin) extract_builtin_constant(const ValueRef &value) {
    SCOPES_RESULT_TYPE(Builtin);
    ConstIntRef x = SCOPES_GET_RESULT(extract_typed_constant<ConstInt>(TYPE_Builtin, value));
    return Builtin((KnownSymbol)x->value());
}

SCOPES_RESULT(Symbol) extract_symbol_constant(const ValueRef &value) {
    SCOPES_RESULT_TYPE(Symbol);
    ConstIntRef x = SCOPES_GET_RESULT(extract_typed_constant<ConstInt>(TYPE_Symbol, value));
    return Symbol::wrap(x->value());
}

SCOPES_RESULT(uint64_t) extract_integer_constant(const ValueRef &value) {
    SCOPES_RESULT_TYPE(uint64_t);
    auto val = extract_constant<ConstInt, VK_ConstInt>(value);
    ConstIntRef x = SCOPES_GET_RESULT(val);
    return x->value();
}

SCOPES_RESULT(ConstAggregateRef) extract_vector_constant(const ValueRef &value) {
    return extract_constant<ConstAggregate, VK_ConstAggregate>(value);
}

SCOPES_RESULT(const Type *) bool_op_return_type(const Type *T) {
    SCOPES_RESULT_TYPE(const Type *);
    T = SCOPES_GET_RESULT(storage_type(T));
    if (T->kind() == TK_Vector) {
        auto vi = cast<VectorType>(T);
        return vector_type(TYPE_Bool, vi->_count);
    } else {
        return TYPE_Bool;
    }
}

static SCOPES_RESULT(void) verify_integer_ops(const Type *x) {
    SCOPES_RESULT_TYPE(void);
    return verify_integer_vector(SCOPES_GET_RESULT(storage_type(x)));
}

static SCOPES_RESULT(void) verify_real_ops(const Type *x) {
    SCOPES_RESULT_TYPE(void);
    return verify_real_vector(SCOPES_GET_RESULT(storage_type(x)));
}

static SCOPES_RESULT(void) verify_integer_ops(const Type *a, const Type *b) {
    SCOPES_RESULT_TYPE(void);
    SCOPES_CHECK_RESULT(verify_integer_vector(SCOPES_GET_RESULT(storage_type(a))));
    return verify(a, b);
}

static SCOPES_RESULT(void) verify_real_ops(const Type *a, const Type *b) {
    SCOPES_RESULT_TYPE(void);
    SCOPES_CHECK_RESULT(verify_real_vector(SCOPES_GET_RESULT(storage_type(a))));
    return verify(a, b);
}

static SCOPES_RESULT(void) verify_real_ops(const Type *a, const Type *b, const Type *c) {
    SCOPES_RESULT_TYPE(void);
    SCOPES_CHECK_RESULT(verify_real_vector(SCOPES_GET_RESULT(storage_type(a))));
    SCOPES_CHECK_RESULT(verify(a, b));
    return verify(a, c);
}

static const Type *unique_result_type(const ASTContext &ctx, const Type *T) {
    T = strip_lifetime(T);
    if (!is_plain(T)) {
        return unique_type(T, ctx.unique_id());
    }
    return T;
}

static void collect_view_argument(const ASTContext &ctx, TypedValueRef &arg, IDSet &ids) {
    auto T = arg->get_type();
    if (is_unique(T)) {
        build_view(ctx, arg.anchor(), arg).assert_ok();
        T = arg->get_type();
        assert(is_view(T));
    }
    auto vq = try_view(T);
    if (vq) {
        ids = union_idset(ids, vq->ids);
    } else {
        if (!is_plain(T)) {
            StyledStream ss;
            ss << "internal error: trying to view " << arg << " but it is untracked" << std::endl;
        }
        assert(is_plain(T));
    }
}

static const Type *view_result_type(const Type *T, const IDSet &ids) {
    T = strip_lifetime(T);
    if (ids.empty()) {
        if (!is_plain(T)) {
            StyledStream ss;
            ss << "internal error: " << T << " has no views" << std::endl;
        }
        assert(is_plain(T));
        return T;
    }
    return view_type(T, ids);
}

static const Type *view_result_type(const ASTContext &ctx, const Type *T,
    TypedValueRef &arg1) {
    IDSet ids;
    collect_view_argument(ctx, arg1, ids);
    return view_result_type(T, ids);
}

static const Type *view_result_type(const ASTContext &ctx, const Type *T,
    TypedValueRef &arg1, TypedValueRef &arg2) {
    IDSet ids;
    collect_view_argument(ctx, arg1, ids);
    collect_view_argument(ctx, arg2, ids);
    return view_result_type(T, ids);
}

static const Type *view_result_type(const ASTContext &ctx, const Type *T,
    TypedValueRef &arg1, TypedValueRef &arg2, TypedValueRef &arg3) {
    IDSet ids;
    collect_view_argument(ctx, arg1, ids);
    collect_view_argument(ctx, arg2, ids);
    collect_view_argument(ctx, arg3, ids);
    return view_result_type(T, ids);
}

static SCOPES_RESULT(void) build_tobool (
    const ASTContext &ctx, const Anchor *anchor, TypedValueRef &val) {
    SCOPES_RESULT_TYPE(void);
    auto T = strip_qualifiers(val->get_type());
    if ((T != TYPE_Bool) && has_typecast_handler()) {
        val = SCOPES_GET_RESULT(run_typecast_handler(ctx, ref(anchor, val), TYPE_Bool));
    }
    return {};
}

static SCOPES_RESULT(void) build_deref(
    const ASTContext &ctx, const Anchor *anchor, TypedValueRef &val) {
    SCOPES_RESULT_TYPE(void);
    auto T = val->get_type();
    auto rq = try_qualifier<ReferQualifier>(T);
    if (rq) {
        SCOPES_CHECK_RESULT(verify_readable(rq, T));
        auto retT = strip_qualifier<ReferQualifier>(T);
        retT = view_result_type(ctx, retT, val);
        auto call = SCOPES_GET_RESULT(ctx.build_deref(anchor, val));
        call->hack_change_value(retT);
        SCOPES_CHECK_RESULT(ctx.append(call));
        val = call;
        return {};
    }
    return {};
}

static SCOPES_RESULT(void) build_deref_move(
    const ASTContext &ctx, const ValueRef &mover, TypedValueRef &val) {
    SCOPES_RESULT_TYPE(void);
    auto anchor = mover.anchor();
    auto T = val->get_type();
    if (!is_plain(T)) {
        auto uq = try_unique(T);
        if (!uq) {
            SCOPES_ERROR(UniqueValueExpected, T);
        }
    }
    auto rq = try_qualifier<ReferQualifier>(T);
    if (rq) {
        SCOPES_CHECK_RESULT(verify_readable(rq, T));
        auto retT = strip_qualifier<ReferQualifier>(T);
        auto call = SCOPES_GET_RESULT(ctx.build_deref(anchor, val));
        call->hack_change_value(unique_result_type(ctx, retT));
        SCOPES_CHECK_RESULT(ctx.append(call));
        auto uq = try_unique(T);
        if (uq) {
            ctx.move(uq->id, mover);
        }
        val = call;
    }
    return {};
}

static SCOPES_RESULT(void) build_deref_automove(
    const ASTContext &ctx, const ValueRef &mover, TypedValueRef &val) {
    SCOPES_RESULT_TYPE(void);
    auto anchor = mover.anchor();
    auto T = val->get_type();
    auto rq = try_qualifier<ReferQualifier>(T);
    if (rq) {
        SCOPES_CHECK_RESULT(verify_readable(rq, T));
        auto retT = strip_qualifier<ReferQualifier>(T);
        const Type *rtype = nullptr;
        if (is_unique(T)) {
            rtype = unique_result_type(ctx, retT);
        } else if (is_view(T)) {
            rtype = view_result_type(ctx, retT, val);
        } else {
            rtype = retT;
        }
        auto call = SCOPES_GET_RESULT(ctx.build_deref(anchor, val));
        call->hack_change_value(rtype);
        SCOPES_CHECK_RESULT(ctx.append(call));
        auto uq = try_unique(T);
        if (uq) {
            ctx.move(uq->id, mover);
        }
        val = call;
    }
    return {};
}

#define CHECKARGS(MINARGS, MAXARGS) \
    SCOPES_CHECK_RESULT((checkargs<MINARGS, MAXARGS>(argcount)))
#define UNIQUETYPE1(ARGT) unique_result_type(ctx, ARGT)
#define UNIQUETYPE2(ARGT1, ARGT2) arguments_type({ \
    unique_result_type(ctx, ARGT1), \
    unique_result_type(ctx, ARGT2)})
#define VIEWTYPE1(ARGT, ...) view_result_type(ctx, ARGT, __VA_ARGS__)
#define DEREF(NAME) \
        SCOPES_CHECK_RESULT(build_deref(ctx, call.anchor(), NAME));
#define MOVE_DEREF(NAME) \
        SCOPES_CHECK_RESULT(build_deref_move(ctx, call, NAME));
#define AUTOMOVE_DEREF(NAME) \
        SCOPES_CHECK_RESULT(build_deref_automove(ctx, call, NAME));
#define READ_VALUE(NAME) \
        assert(argn < argcount); \
        auto && NAME = values[argn++]; \
        DEREF(NAME);
#define READ_NODEREF_VALUE(NAME) \
        assert(argn < argcount); \
        auto && NAME = values[argn++];
#define READ_NODEREF_TYPEOF(NAME) \
        assert(argn < argcount); \
        auto &&_ ## NAME = values[argn++]; \
        const Type *NAME = _ ## NAME->get_type();
#define READ_TYPEOF(NAME) \
        assert(argn < argcount); \
        auto &&_ ## NAME = values[argn++]; \
        DEREF(_ ## NAME); \
        const Type *NAME = _ ## NAME->get_type();
#define READ_AUTOMOVE_TYPEOF(NAME) \
        assert(argn < argcount); \
        auto &&_ ## NAME = values[argn++]; \
        AUTOMOVE_DEREF(_ ## NAME); \
        const Type *NAME = _ ## NAME->get_type();
#define READ_NODEREF_STORAGETYPEOF(NAME) \
        assert(argn < argcount); \
        auto &&_ ## NAME = values[argn++]; \
        const Type *typeof_ ## NAME = _ ## NAME->get_type(); \
        const Type *NAME = SCOPES_GET_RESULT(storage_type(typeof_ ## NAME));
#define READ_STORAGETYPEOF(NAME) \
        assert(argn < argcount); \
        auto &&_ ## NAME = values[argn++]; \
        DEREF(_ ## NAME); \
        const Type *typeof_ ## NAME = _ ## NAME->get_type(); \
        const Type *NAME = SCOPES_GET_RESULT(storage_type(typeof_ ## NAME));
#define READ_AUTOMOVE_STORAGETYPEOF(NAME) \
        assert(argn < argcount); \
        auto &&_ ## NAME = values[argn++]; \
        AUTOMOVE_DEREF(_ ## NAME); \
        const Type *typeof_ ## NAME = _ ## NAME->get_type(); \
        const Type *NAME = SCOPES_GET_RESULT(storage_type(typeof_ ## NAME));
#define READ_MOVE_STORAGETYPEOF(NAME) \
        assert(argn < argcount); \
        auto &&_ ## NAME = values[argn++]; \
        MOVE_DEREF(_ ## NAME); \
        const Type *typeof_ ## NAME = _ ## NAME->get_type(); \
        const Type *NAME = SCOPES_GET_RESULT(storage_type(typeof_ ## NAME));
#define READ_INT_CONST(NAME) \
        assert(argn < argcount); \
        auto &&_ ## NAME = values[argn++]; \
        auto NAME = SCOPES_GET_RESULT(extract_integer_constant(_ ## NAME));
#define READ_TYPE_CONST(NAME) \
        assert(argn < argcount); \
        auto &&_ ## NAME = values[argn++]; \
        auto NAME = SCOPES_GET_RESULT(extract_type_constant(_ ## NAME));
#define READ_BUILTIN_CONST(NAME) \
        assert(argn < argcount); \
        auto &&_ ## NAME = values[argn++]; \
        auto NAME = SCOPES_GET_RESULT(extract_builtin_constant(_ ## NAME));
#define READ_VECTOR_CONST(NAME) \
        assert(argn < argcount); \
        auto NAME = SCOPES_GET_RESULT(extract_vector_constant(values[argn++]));
#define READ_SYMBOL_CONST(NAME) \
        assert(argn < argcount); \
        auto &&_ ## NAME = values[argn++]; \
        auto NAME = SCOPES_GET_RESULT(extract_symbol_constant(_ ## NAME));

static const Type *canonical_return_type(const FunctionRef &fn, const Type *rettype,
    bool is_except = false) {
    if (!is_returning_value(rettype))
        return rettype;
    std::unordered_map<int, int> idmap;
    Types rettypes;
    int acount = get_argument_count(rettype);
    for (int i = 0; i < acount; ++i) {
        auto T = get_argument(rettype, i);
        auto uq = try_qualifier<UniqueQualifier>(T);
        if (uq) {
            int newid = (is_except?FirstUniqueError:FirstUniqueOutput) - i;
            idmap.insert({uq->id, newid});
            T = unique_type(T, newid);
        }
        rettypes.push_back(T);
    }
    for (int i = 0; i < acount; ++i) {
        auto &&T = rettypes[i];
        auto vq = try_qualifier<ViewQualifier>(T);
        if (vq) {
            IDSet ids;
            for (auto id : vq->ids) {
                auto it = idmap.find(id);
                if (it != idmap.end()) {
                    ids.insert(it->second);
                } else {
                    ids.insert(id);
                }
            }
            T = view_type(strip_qualifier<ViewQualifier>(T), ids);
        }
    }
    return arguments_type(rettypes);
}

static SCOPES_RESULT(const Type *) get_function_type(const FunctionRef &fn) {
    SCOPES_RESULT_TYPE(const Type *);

    //const Block *block = &fn->body;

    Types params;
    for (int i = 0; i < fn->params.size(); ++i) {
        auto param = fn->params[i];
        auto T = param->get_type();
        auto uq = try_unique(T);
        if (uq && fn->valid.count(uq->id)) {
            // has this parameter been spent? if not, it's a view
            T = view_type(T, {});
            param->retype(T);
        }
        params.push_back(T);
    }

    const Type *rettype = fn->returning_hint;
    const Anchor *last_anchor =
        fn->returning_anchor?fn->returning_anchor:fn.anchor();
    for (auto _return : fn->returns) {
        rettype = SCOPES_GET_RESULT(merge_value_type("return", rettype,
            arguments_type_from_typed_values(_return->values),
            last_anchor, _return.anchor()));
        last_anchor = _return.anchor();
    }
    rettype = canonical_return_type(fn, rettype);
    const Type *raisetype = fn->raising_hint;
    last_anchor =
        fn->raising_anchor?fn->raising_anchor:fn.anchor();
    for (auto _raise : fn->raises) {
        raisetype = SCOPES_GET_RESULT(merge_value_type("raise", raisetype,
            arguments_type_from_typed_values(_raise->values),
            last_anchor, _raise.anchor()));
        last_anchor = _raise.anchor();
    }
    raisetype = canonical_return_type(fn, raisetype, true);

    return native_opaque_pointer_type(raising_function_type(raisetype, rettype, params));
}

static SCOPES_RESULT(const Type *) ensure_function_type(const FunctionRef &fn) {
    SCOPES_RESULT_TYPE(const Type *);
    const Type *fT = SCOPES_GET_RESULT(get_function_type(fn));
    if (fn->is_typed()) {
        if (fT != fn->get_type()) {
            SCOPES_ERROR(RecursiveFunctionChangedType, fn->get_type(), fT);
        }
    } else {
        fn->set_type(fT);
    }
    return fT;
}

static void keys_from_function_type(Symbols &keys, const FunctionType *ft) {
    for (auto T : ft->argument_types) {
        keys.push_back(type_key(T)._0);
    }
}

static bool keys_from_parameters(Symbols &keys, const ParameterTemplates &params) {
    bool vararg = false;
    for (auto param : params) {
        if (param->variadic) {
            vararg = true;
        } else {
            keys.push_back(param->name);
        }
    }
    return vararg;
}

SCOPES_RESULT(const Type *) ptr_to_ref(const Type *T) {
    SCOPES_RESULT_TYPE(const Type *);
    T = SCOPES_GET_RESULT(storage_type(T));
    SCOPES_CHECK_RESULT(verify_kind<TK_Pointer>(T));
    auto pt = cast<PointerType>(T);
    return refer_type(
        pt->element_type, pt->flags, pt->storage_class);
}

SCOPES_RESULT(const Type *) ref_to_ptr(const Type *T) {
    SCOPES_RESULT_TYPE(const Type *);
    auto rq = SCOPES_GET_RESULT(verify_refer(T));
    return copy_qualifiers(
        rq->get_pointer_type(strip_qualifiers(T)),
        strip_qualifier<ReferQualifier>(T));
}

//template<typename T>
SCOPES_RESULT(void) sanitize_tuple_index(const Anchor *anchor, const Type *ST,
    const TupleType *type, uint64_t &arg, TypedValueRef &_arg) {
    SCOPES_RESULT_TYPE(void);
    if (_arg->get_type() == TYPE_Symbol) {
        auto sym = Symbol::wrap(arg);
        size_t idx = type->field_index(sym);
        if (idx == (size_t)-1) {
            SCOPES_ERROR(UnknownTupleField, sym, ST);
        }
        // rewrite field
        arg = idx;
        _arg = ref(anchor, ConstInt::from(TYPE_I32, idx));
    } else if (_arg->get_type() != TYPE_I32) {
        _arg = ref(anchor, ConstInt::from(TYPE_I32, arg));
    }
    return {};
}

// if it's legal to make a starting copy of this value
static bool is_template_like_constant(const TypedValueRef &value) {
    if (value.isa<Undef>()) return true;
    if (value.isa<Const>()) return true;
    return false;
}

const Type *remap_unique_return_arguments(
    const ASTContext &ctx, ID2SetMap &idmap, const Type *rt) {
    if (is_returning_value(rt)) {
        // remap return type
        int acount = get_argument_count(rt);
        Types rettypes;
        rettypes.reserve(acount);
        for (int i = 0; i < acount; ++i) {
            const Type *argT = get_argument(rt, i);
            auto uq = try_unique(argT);
            if (uq) {
                auto newid = ctx.unique_id();
                argT = unique_type(argT, newid);
                map_unique_id(idmap, uq->id, newid);
            } else {
                auto vq = try_view(argT);
                if (vq) {
                    IDSet newids;
                    for (auto vid : vq->ids) {
                        if (vid == GlobalUnique) {
                            newids.insert(GlobalUnique);
                        } else {
                            auto it = idmap.find(vid);
                            assert(it != idmap.end());
                            for (auto destid : it->second) {
                                newids.insert(destid);
                            }
                        }
                    }
                    argT = view_type(strip_view(argT), newids);
                }
            }
            rettypes.push_back(argT);
        }
        rt = arguments_type(rettypes);
    }
    return rt;
}

static SCOPES_RESULT(TypedValueRef) prove_CallTemplate(
    const ASTContext &ctx, const CallTemplateRef &call) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    SCOPES_TRACE_PROVE_EXPR(call);
    //auto callee_anchor = call->callee.anchor();
    //const Anchor *anchor = get_best_anchor(call);
    TypedValueRef callee = ref(call->callee.anchor(),
        SCOPES_GET_RESULT(prove(ctx, call->callee)));
    TypedValues values;
    auto noret = SCOPES_GET_RESULT(prove_arguments(ctx, values, call->args));
    if (noret) return noret;
    bool rawcall = call->is_rawcall();
    int redirections = 0;
repeat:
    SCOPES_CHECK_RESULT(verify_valid(ctx, callee, "callee"));
    const Type *T = strip_qualifiers(callee->get_type());
    if (!rawcall) {
        assert(redirections < 16);
        ValueRef dest;
        if (T->lookup_call_handler(dest)) {
            values.insert(values.begin(), callee);
            callee = SCOPES_GET_RESULT(prove(ctx, dest));
            //call = ref(dest.anchor(), call);
            redirections++;
            goto repeat;
        }
    }
    DEREF(callee);
    if (is_function_pointer(T)) {
        TypedValues args;
        Symbols keys;
        keys_from_function_type(keys, extract_function_type(T));
        SCOPES_CHECK_RESULT(map_keyed_arguments(call.anchor(), callee, args, values, keys, false));
        values = args;
        SCOPES_CHECK_RESULT(verify_valid(ctx, values, "function call"));
    } else if (T == TYPE_Closure) {
        const Closure *cl = SCOPES_GET_RESULT((extract_closure_constant(callee)));
        {
            TypedValues args;
            Symbols keys;
            bool vararg = keys_from_parameters(keys, cl->func->params);
            SCOPES_CHECK_RESULT(map_keyed_arguments(call.anchor(),
                ref(cl->func.anchor(), callee),
                args, values, keys, vararg));
            values = args;
        }
        if (cl->func->is_inline()) {
            return SCOPES_GET_RESULT(prove_inline(ctx, cl, values));
        } else {
            SCOPES_CHECK_RESULT(verify_valid(ctx, values, "call"));
            Types types;
            for (auto &&arg : values) {
                auto AT = arg->get_type();
                if (is_opaque(AT)) {
                    SCOPES_TRACE_PROVE_ARG(arg);
                    SCOPES_ERROR(OpaqueType, AT);
                }
                types.push_back(arg->get_type());
            }
            callee = SCOPES_GET_RESULT(prove(
                ref(callee.anchor(), cl->frame),
                ref(callee.anchor(), cl->func),
                types));
            FunctionRef f = callee.cast<Function>();
            if (f->complete) {
                T = callee->get_type();
            } else {
                T = SCOPES_GET_RESULT(ensure_function_type(f));
            }
            //goto repeat;
        }
    } else if (T == TYPE_ASTMacro) {
        auto fptr = SCOPES_GET_RESULT(extract_astmacro_constant(callee));
        assert(fptr);
        SCOPES_ASTCONTEXT(ctx);
        auto result = fptr(ref(call.anchor(), ArgumentList::from(values)));
        if (result.ok) {
            ValueRef value = result._0;
            if (!value) {
                SCOPES_ERROR(SpiceMacroReturnedNull);
            }
            //value = ref(call.anchor(), result._0);
            return SCOPES_GET_RESULT(prove(ctx, value));
        } else {
            SCOPES_RETURN_TRACE_ERROR(result.except);
        }
    } else if (T == TYPE_Builtin) {
        //SCOPES_CHECK_RESULT(anycl.verify(TYPE_Builtin));
        Builtin b = SCOPES_GET_RESULT(extract_builtin_constant(callee));
        size_t argcount = values.size();
        size_t argn = 0;
        if (b.value() == FN_IsValid) {
            CHECKARGS(1, 1);
            bool valid = ctx.block->is_valid(ValueIndex(values[0]));
            return TypedValueRef(call.anchor(), ConstInt::from(TYPE_Bool, valid));
        }
        if (b != FN_IsDropped) {
            SCOPES_CHECK_RESULT(verify_valid(ctx, values, "builtin call"));
        }
        switch(b.value()) {
        /*** DEBUGGING ***/
        case FN_DumpUniques: {
            StyledStream ss(SCOPES_CERR);
            ss << call.anchor() << " dump-uniques:";
            for (auto id : ctx.block->valid) {
                ss << " ";
                ss << id;
                auto info = ctx.function->get_unique_info(id);
                ss << "[" << info.get_depth() << "](" << info.value << ")";
            }
            ss << std::endl;
            return ref(call.anchor(), ArgumentList::from(values));
        } break;
        case FN_Dump: {
            StyledStream ss(SCOPES_CERR);
            ss << call.anchor() << " dump:";
            for (auto arg : values) {
                ss << " ";
                stream_value(ss, arg, StreamValueFormat::singleline());
            }
            ss << std::endl;
            return ref(call.anchor(), ArgumentList::from(values));
        } break;
        case FN_DumpDebug: {
            StyledStream ss(SCOPES_CERR);
            ss << call.anchor() << " dump-debug:";
            for (auto arg : values) {
                ss << std::endl;
                stream_value(ss, arg, StreamValueFormat::debug());
            }
            ss << std::endl;
            return ref(call.anchor(), ArgumentList::from(values));
        } break;
        case FN_DumpAST: {
            StyledStream ss(SCOPES_CERR);
            ss << call.anchor() << " dump-spice:";
            for (auto arg : values) {
                ss << std::endl;
                stream_value(ss, arg);
            }
            ss << std::endl;
            return ref(call.anchor(), ArgumentList::from(values));
        } break;
        case FN_DumpTemplate: {
            StyledStream ss(SCOPES_CERR);
            ss << call.anchor() << " dump-template:";
            for (auto arg : call->args) {
                ss << std::endl;
                stream_value(ss, arg);
            }
            return ref(call.anchor(), ArgumentList::from(values));
        } break;
        /*** ANNOTATION ***/
        case FN_Annotate: {
            // takes any kind of argument
            return TypedValueRef(call.anchor(), Annotate::from(values));
        } break;
        case FN_HideTraceback: {
            CHECKARGS(0, 0);
            ctx.block->tag_traceback = false;
            return ref(call.anchor(), ArgumentList::from({}));
        } break;
        /*** VIEW INFERENCE ***/
        case FN_Move: {
            CHECKARGS(1, 1);
            READ_NODEREF_TYPEOF(X);
            if (is_template_like_constant(_X)) return _X;
            auto uq = try_unique(X);
            if (!uq) {
                SCOPES_ERROR(UniqueValueExpected, _X->get_type());
            }
            SCOPES_CHECK_RESULT(build_move(ctx, call, _X));
            return _X;
        } break;
        case FN_View: {
            CHECKARGS(0, -1);
            while (argn < argcount) {
                READ_NODEREF_TYPEOF(X);
                auto uq = try_unique(X);
                if (!uq) {
                    // no effect
                    continue;
                }
                SCOPES_CHECK_RESULT(build_view(ctx, call.anchor(), _X));
            }
            return ref(call.anchor(), ArgumentList::from(values));
        } break;
        case FN_Dupe: {
            CHECKARGS(1, 1);
            READ_NODEREF_TYPEOF(X);
            const Type *DestT = strip_lifetime(X);
            auto op = Cast::from(CastBitcast, _X, DestT);
            if (is_plain(X)) {
                return TypedValueRef(call.anchor(), op);
            } else {
                auto uq = try_unique(X);
                if (uq) {
                    ctx.move(uq->id, call);
                }
                op->hack_change_value(UNIQUETYPE1(op->get_type()));
                return TypedValueRef(call.anchor(), op);
            }
        } break;
        case SYM_DropHandler: {
            CHECKARGS(1, 1);
            READ_NODEREF_TYPEOF(X);
            (void)X;
            return SCOPES_GET_RESULT(build_drop(ctx, call.anchor(), _X));
        } break;
        case FN_Lose: {
            CHECKARGS(1, 1);
            READ_NODEREF_TYPEOF(X);
            if (!is_plain(X)) {
                auto uq = try_unique(X);
                if (!uq) {
                    SCOPES_ERROR(UniqueValueExpected, _X->get_type());
                }
                ctx.move(uq->id, call);
            }
            return ref(call.anchor(), ArgumentList::from({}));
        } break;
        case FN_IsDropped: {
            CHECKARGS(1, 1);
            bool valid = ctx.block->is_valid(ValueIndex(values[0]));
            return TypedValueRef(ref(call.anchor(), ConstInt::from(TYPE_Bool, !valid)));
        } break;
        case FN_Viewing: {
            for (size_t i = 0; i < values.size(); ++i) {
                READ_NODEREF_VALUE(value);
                auto param = value.dyn_cast<Parameter>();
                if (!param) {
                    SCOPES_ERROR(ValueKindMismatch, VK_Parameter, value->kind());
                }
                auto T = param->get_type();
                if (!is_plain(T)) {
                    param->retype(view_type(param->get_type(), {}));
                }
            }
            return ref(call.anchor(), ArgumentList::from({}));
        } break;
        case FN_Returning: {
            Types types;
            for (size_t i = 0; i < values.size(); ++i) {
                READ_TYPE_CONST(T);
                types.push_back(T);
            }
            auto rtype = arguments_type(types);
            auto fn = ctx.function;
            const Anchor *last_anchor =
                fn->returning_anchor?fn->returning_anchor:fn.anchor();
            fn->returning_hint = SCOPES_GET_RESULT(merge_value_type("returning",
                fn->returning_hint, rtype, last_anchor, call.anchor()));
            fn->returning_anchor = call.anchor();
            return ref(call.anchor(), ArgumentList::from({}));
        } break;
        case FN_Raising: {
            Types types;
            for (size_t i = 0; i < values.size(); ++i) {
                READ_TYPE_CONST(T);
                types.push_back(T);
            }
            auto rtype = arguments_type(types);
            auto fn = ctx.function;
            const Anchor *last_anchor =
                fn->raising_anchor?fn->raising_anchor:fn.anchor();
            fn->raising_hint = SCOPES_GET_RESULT(merge_value_type("raising",
                fn->raising_hint, rtype, last_anchor, call.anchor()));
            fn->raising_anchor = call.anchor();
            return ref(call.anchor(), ArgumentList::from({}));
        } break;
        /*** ARGUMENTS ***/
        case FN_VaCountOf: {
            return TypedValueRef(call.anchor(), ConstInt::from(TYPE_I32, argcount));
        } break;
        case FN_NullOf: {
            CHECKARGS(1, 1);
            READ_TYPE_CONST(T);
            return TypedValueRef(call.anchor(), SCOPES_GET_RESULT(nullof(T)));
        } break;
        case FN_Undef: {
            CHECKARGS(1, 1);
            READ_TYPE_CONST(T);
            return TypedValueRef(call.anchor(), Undef::from(T));
        } break;
        case FN_TypeOf: {
            CHECKARGS(1, 1);
            READ_NODEREF_TYPEOF(A);
            return TypedValueRef(call.anchor(),
                ConstPointer::type_from(strip_qualifiers(A)));
        } break;
        case KW_Forward: {
            return ref(call.anchor(), ArgumentList::from(values));
        } break;
        /*** SPIR-V FORMS ***/
        case FN_Sample: {
            CHECKARGS(2, -1);
            READ_STORAGETYPEOF(ST);
            READ_STORAGETYPEOF(coords);
            (void)coords;
            if (ST->kind() == TK_SampledImage) {
                auto sit = cast<SampledImageType>(ST);
                ST = SCOPES_GET_RESULT(storage_type(sit->type));
            }
            SCOPES_CHECK_RESULT(verify_kind<TK_Image>(ST));
            Sample::Options opts;
            opts.reserve(argcount);
            while (argn < argcount) {
                READ_VALUE(val);
                Symbol key = type_key(val->get_type())._0;
                opts.push_back(Sample::Option(key, val));
            }
            auto op = Sample::from(_ST, _coords, opts);
            op->hack_change_value(VIEWTYPE1(op->get_type(), _ST));
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_ImageQuerySize: {
            CHECKARGS(1, -1);
            READ_STORAGETYPEOF(ST);
            if (ST->kind() == TK_SampledImage) {
                auto sit = cast<SampledImageType>(ST);
                ST = SCOPES_GET_RESULT(storage_type(sit->type));
            }
            SCOPES_CHECK_RESULT(verify_kind<TK_Image>(ST));
            TypedValueRef lod;
            while (argn < argcount) {
                READ_VALUE(val);
                Symbol key = type_key(val->get_type())._0;
                if (key == SYM_SPIRV_ImageOperandLod) {
                    lod = val;
                }
            }
            auto op = ImageQuerySize::from(_ST, lod);
            op->hack_change_value(VIEWTYPE1(op->get_type(), _ST));
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_ImageQueryLod: {
            CHECKARGS(2, 2);
            READ_STORAGETYPEOF(ST);
            READ_STORAGETYPEOF(coords);
            (void)coords;
            if (ST->kind() == TK_SampledImage) {
                auto sit = cast<SampledImageType>(ST);
                ST = SCOPES_GET_RESULT(storage_type(sit->type));
            }
            SCOPES_CHECK_RESULT(verify_kind<TK_Image>(ST));
            auto op = ImageQueryLod::from(_ST, _coords);
            op->hack_change_value(VIEWTYPE1(op->get_type(), _ST));
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_ImageQueryLevels:
        case FN_ImageQuerySamples: {
            CHECKARGS(1, 1);
            READ_STORAGETYPEOF(ST);
            if (ST->kind() == TK_SampledImage) {
                auto sit = cast<SampledImageType>(ST);
                ST = SCOPES_GET_RESULT(storage_type(sit->type));
            }
            SCOPES_CHECK_RESULT(verify_kind<TK_Image>(ST));
            TypedValueRef op;
            if (b.value() == FN_ImageQueryLevels) {
                op = TypedValueRef(call.anchor(), ImageQueryLevels::from(_ST));
            } else {
                op = TypedValueRef(call.anchor(), ImageQuerySamples::from(_ST));
            }
            op->hack_change_value(VIEWTYPE1(op->get_type(), _ST));
            return op;
        } break;
        case FN_ImageRead: {
            CHECKARGS(2, 2);
            READ_STORAGETYPEOF(ST);
            READ_STORAGETYPEOF(coords);
            (void)coords;
            SCOPES_CHECK_RESULT(verify_kind<TK_Image>(ST));
            auto op = ImageRead::from(_ST, _coords);
            op->hack_change_value(VIEWTYPE1(op->get_type(), _ST));
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_ImageWrite: {
            CHECKARGS(3, 3);
            READ_STORAGETYPEOF(ST);
            READ_STORAGETYPEOF(coords);
            (void)coords;
            READ_STORAGETYPEOF(texel);
            (void)texel;
            SCOPES_CHECK_RESULT(verify_kind<TK_Image>(ST));
            auto op = ImageWrite::from(_ST, _coords, _texel);
            return TypedValueRef(call.anchor(), op);
        } break;
        case SFXFN_ExecutionMode: {
            CHECKARGS(1, 4);
            READ_SYMBOL_CONST(sym);
            switch(sym.value()) {
            #define T(NAME) \
                case SYM_SPIRV_ExecutionMode ## NAME: break;
                B_SPIRV_EXECUTION_MODE()
            #undef T
                default:
                    SCOPES_ERROR(UnsupportedExecutionMode, sym);
                    break;
            }
            int c = 0;
            int v[3] = { -1, -1, -1 };
            for (size_t i = 1; i < values.size(); ++i) {
                READ_INT_CONST(x);
                v[c++] = x;
                if (c == 3) break;
            }
            return TypedValueRef(call.anchor(),
                ExecutionMode::from(sym, v[0], v[1], v[2]));
        } break;
        /*** terminators ***/
        case SFXFN_Discard: {
            CHECKARGS(0, 0);
            return TypedValueRef(call.anchor(), Discard::from());
        } break;
        case SFXFN_Unreachable: {
            CHECKARGS(0, 0);
            return TypedValueRef(call.anchor(), Unreachable::from());
        } break;
        case KW_Return: {
            if (ctx.frame->label) {
                assert(ctx.frame->original && ctx.frame->original->is_inline());
                // generate a merge
                return make_merge1(ctx, call.anchor(), ctx.frame->label, values);
            } else {
                assert(!ctx.frame->original || !ctx.frame->original->is_inline()
                    || ctx.frame->original->is_hidden());
                // generate a return
                return SCOPES_GET_RESULT(make_return1(ctx, call, values));
            }
        } break;
        case KW_Raise: {
            assert(ctx.frame);
            return SCOPES_GET_RESULT(make_raise1(ctx, call, values));
        } break;
        case KW_Break: {
            if (!ctx._break) {
                SCOPES_ERROR(BreakOutsideLoop);
            }
            return make_merge1(ctx, call.anchor(), ctx._break, values);
        } break;
        case KW_Repeat: {
            if (!ctx.loop) {
                SCOPES_ERROR(RepeatOutsideLoop);
            }
            return make_repeat1(ctx, call.anchor(), ctx.loop, values);
        } break;
        /*** MISC ***/
        case OP_Tertiary: {
            CHECKARGS(3, 3);
            READ_STORAGETYPEOF(T1);
            READ_TYPEOF(T2);
            READ_TYPEOF(T3);
            {
                SCOPES_TRACE_PROVE_ARG(_T1);
                SCOPES_CHECK_RESULT(verify_bool_vector(T1));
            }
            {
                SCOPES_TRACE_PROVE_ARG(_T3);
                SCOPES_CHECK_RESULT(verify(T2, T3));
            }
            if (T1->kind() == TK_Vector) {
                SCOPES_TRACE_PROVE_ARG(_T2);
                auto ST2 = SCOPES_GET_RESULT(storage_type(T2));
                SCOPES_CHECK_RESULT(verify_vector_sizes(T1, ST2));
            }
            auto op = Select::from(_T1, _T2, _T3);
            op->hack_change_value(VIEWTYPE1(op->get_type(), _T1, _T2, _T3));
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_Bitcast: {
            CHECKARGS(2, 2);
            READ_NODEREF_TYPEOF(SrcT);
            READ_TYPE_CONST(DestT);
            if (SrcT == DestT) {
                return _SrcT;
            } else {
                //DEREF(_SrcT);
                const Type *SSrcT = SCOPES_GET_RESULT(storage_type(SrcT));
                const Type *SDestT = SCOPES_GET_RESULT(storage_type(DestT));
                if (!typekinds_compatible(SSrcT->kind(), SDestT->kind())) {
                    SCOPES_ERROR(CastCategoryError, SrcT, DestT);
                }
                if (SSrcT != SDestT) {
                    switch (SSrcT->kind()) {
                    case TK_Array:
                    case TK_Matrix:
                    //case TK_Vector:
                    case TK_Tuple: {
                        SCOPES_ERROR(CastIncompatibleAggregateType, SSrcT);
                    } break;
                    default: break;
                    }
                }
                if (size_of(SSrcT).assert_ok() != size_of(SDestT).assert_ok()) {
                    SCOPES_ERROR(CastSizeError, SrcT, DestT);
                }

                DestT = strip_qualifiers(DestT);
                bool target_is_plain = is_plain(DestT);

                if (is_view(SrcT)) {
                    DestT = view_result_type(ctx, DestT, _SrcT);
                } else if (!target_is_plain) {
                    DestT = unique_result_type(ctx, DestT);
                }

                auto uq = try_unique(SrcT);
                if (uq) {
                    ctx.move(uq->id, call);
                }

                auto rq = try_qualifier<ReferQualifier>(SrcT);
                if (rq) {
                    DestT = qualify(DestT, { rq });
                }
                //_DestT = ref(_DestT.anchor(), ConstPointer::type_from(DestT));
                if (_SrcT.isa<Pure>() && target_is_plain) {
                    return TypedValueRef(ref(call.anchor(),
                        PureCast::from(DestT, _SrcT.cast<Pure>())));
                } else {
                    // DestT is already converted
                    return TypedValueRef(ref(call.anchor(),
                        Cast::from(CastBitcast, _SrcT, DestT)));
                }
            }
        } break;
        case FN_IntToPtr: {
            CHECKARGS(2, 2);
            READ_STORAGETYPEOF(T);
            READ_TYPE_CONST(DestT);
            SCOPES_CHECK_RESULT(verify_integer(T));
            SCOPES_CHECK_RESULT((verify_kind<TK_Pointer>(SCOPES_GET_RESULT(storage_type(DestT)))));
            return TypedValueRef(call.anchor(), Cast::from(CastIntToPtr, _T, VIEWTYPE1(DestT, _T)));
        } break;
        case FN_PtrToInt: {
            CHECKARGS(2, 2);
            READ_STORAGETYPEOF(T);
            READ_TYPE_CONST(DestT);
            SCOPES_CHECK_RESULT(verify_kind<TK_Pointer>(T));
            SCOPES_CHECK_RESULT(verify_integer(SCOPES_GET_RESULT(storage_type(DestT))));
            return TypedValueRef(call.anchor(), Cast::from(CastPtrToInt, _T, VIEWTYPE1(DestT, _T)));
        } break;
        case FN_ITrunc: {
            CHECKARGS(2, 2);
            READ_STORAGETYPEOF(T);
            READ_TYPE_CONST(DestT);
            SCOPES_CHECK_RESULT(verify_integer(T));
            SCOPES_CHECK_RESULT(verify_integer(SCOPES_GET_RESULT(storage_type(DestT))));
            return TypedValueRef(call.anchor(), Cast::from(CastITrunc, _T, VIEWTYPE1(DestT, _T)));
        } break;
        case FN_FPTrunc: {
            CHECKARGS(2, 2);
            READ_STORAGETYPEOF(T);
            READ_TYPE_CONST(DestT);
            SCOPES_CHECK_RESULT(verify_real(T));
            SCOPES_CHECK_RESULT(verify_real(SCOPES_GET_RESULT(storage_type(DestT))));
            if (cast<RealType>(T)->width < cast<RealType>(DestT)->width) {
                SCOPES_ERROR(InvalidOperands, b, T, DestT);
            }
            return TypedValueRef(call.anchor(), Cast::from(CastFPTrunc, _T, VIEWTYPE1(DestT, _T)));
        } break;
        case FN_FPExt: {
            CHECKARGS(2, 2);
            READ_STORAGETYPEOF(T);
            READ_TYPE_CONST(DestT);
            SCOPES_CHECK_RESULT(verify_real(T));
            SCOPES_CHECK_RESULT(verify_real(SCOPES_GET_RESULT(storage_type(DestT))));
            if (cast<RealType>(T)->width > cast<RealType>(DestT)->width) {
                SCOPES_ERROR(InvalidOperands, b, T, DestT);
            }
            return TypedValueRef(call.anchor(), Cast::from(CastFPExt, _T, VIEWTYPE1(DestT, _T)));
        } break;
        case FN_FPToUI:
        case FN_FPToSI: {
            CHECKARGS(2, 2);
            READ_STORAGETYPEOF(T);
            READ_TYPE_CONST(DestT);
            SCOPES_CHECK_RESULT(verify_real_ops(T));
            auto ST = SCOPES_GET_RESULT(storage_type(DestT));
            SCOPES_CHECK_RESULT(verify_integer_ops(ST));
            SCOPES_CHECK_RESULT(verify_vector_sizes(T, ST));
            #if 0
            if ((T != TYPE_F32) && (T != TYPE_F64)) {
                SCOPES_ERROR(InvalidOperands, b, T, DestT);
            }
            #endif
            if (b.value() == FN_FPToUI) {
                return TypedValueRef(call.anchor(), Cast::from(CastFPToUI, _T, VIEWTYPE1(DestT, _T)));
            } else {
                return TypedValueRef(call.anchor(), Cast::from(CastFPToSI, _T, VIEWTYPE1(DestT, _T)));
            }
        } break;
        case FN_UIToFP:
        case FN_SIToFP: {
            CHECKARGS(2, 2);
            READ_STORAGETYPEOF(T);
            READ_TYPE_CONST(DestT);
            auto ST = SCOPES_GET_RESULT(storage_type(DestT));
            SCOPES_CHECK_RESULT(verify_integer_ops(T));
            SCOPES_CHECK_RESULT(verify_real_ops(ST));
            SCOPES_CHECK_RESULT(verify_vector_sizes(T, ST));
            #if 0
            if ((DestT != TYPE_F32) && (DestT != TYPE_F64)) {
                SCOPES_ERROR(InvalidOperands, b, T, DestT);
            }
            #endif
            if (b.value() == FN_UIToFP) {
                return TypedValueRef(call.anchor(), Cast::from(CastUIToFP, _T, VIEWTYPE1(DestT, _T)));
            } else {
                return TypedValueRef(call.anchor(), Cast::from(CastSIToFP, _T, VIEWTYPE1(DestT, _T)));
            }
        } break;
        case FN_ZExt:
        case FN_SExt: {
            CHECKARGS(2, 2);
            READ_STORAGETYPEOF(T);
            READ_TYPE_CONST(DestT);
            SCOPES_CHECK_RESULT(verify_integer(T));
            SCOPES_CHECK_RESULT(verify_integer(SCOPES_GET_RESULT(storage_type(DestT))));
            if (b.value() == FN_ZExt) {
                return TypedValueRef(call.anchor(), Cast::from(CastZExt, _T, VIEWTYPE1(DestT, _T)));
            } else {
                return TypedValueRef(call.anchor(), Cast::from(CastSExt, _T, VIEWTYPE1(DestT, _T)));
            }
        } break;
        case FN_ExtractElement: {
            CHECKARGS(2, 2);
            READ_NODEREF_STORAGETYPEOF(T);
            READ_STORAGETYPEOF(idx);
            SCOPES_CHECK_RESULT(verify_kind<TK_Vector>(T));
            SCOPES_CHECK_RESULT(verify_integer(idx));
            auto rq = try_qualifier<ReferQualifier>(typeof_T);
            if (rq) {
                auto op = TypedValueRef(call.anchor(),
                    SCOPES_GET_RESULT(ctx.build_getelementref(call.anchor(), _T, { _idx })));
                const Type *retT = view_result_type(ctx,
                    qualify(op->get_type(), { rq }), _T, _idx);
                op->hack_change_value(retT);
                return op;
            } else {
                auto op = TypedValueRef(call.anchor(),
                    ExtractElement::from(_T, _idx));
                op->hack_change_value(VIEWTYPE1(op->get_type(), _T, _idx));
                return op;
            }
        } break;
        case FN_InsertElement: {
            CHECKARGS(3, 3);
            READ_STORAGETYPEOF(T);
            READ_STORAGETYPEOF(ET);
            READ_STORAGETYPEOF(idx);
            SCOPES_CHECK_RESULT(verify_integer(idx));
            SCOPES_CHECK_RESULT(verify_kind<TK_Vector>(T));
            auto vi = cast<VectorType>(T);
            SCOPES_CHECK_RESULT(verify(SCOPES_GET_RESULT(storage_type(vi->element_type)), ET));
            auto op = TypedValueRef(call.anchor(),
                InsertElement::from(_T, _ET, _idx));
            op->hack_change_value(VIEWTYPE1(op->get_type(), _T, _ET, _idx));
            return op;
        } break;
        case FN_ShuffleVector: {
            CHECKARGS(3, 3);
            READ_STORAGETYPEOF(TV1);
            READ_STORAGETYPEOF(TV2);
            READ_VECTOR_CONST(mask);
            const Type *TMask = mask->get_type();
            SCOPES_CHECK_RESULT(verify_kind<TK_Vector>(TV1));
            SCOPES_CHECK_RESULT(verify_kind<TK_Vector>(TV2));
            SCOPES_CHECK_RESULT(verify_kind<TK_Vector>(TMask));
            SCOPES_CHECK_RESULT(verify(TV1, TV2));
            auto vi = cast<VectorType>(TV1);
            auto mask_vi = cast<VectorType>(TMask);
            SCOPES_CHECK_RESULT(verify(TYPE_I32, mask_vi->element_type));
            size_t incount = vi->count() * 2;
            size_t outcount = mask_vi->count();
            std::vector<uint32_t> outmask;
            outmask.reserve(outcount);
            for (size_t i = 0; i < outcount; ++i) {
                auto k = cast<ConstInt>(mask->values[i])->msw();
                SCOPES_CHECK_RESULT(verify_range(k, incount));
                outmask.push_back(k);
            }
            auto op = TypedValueRef(call.anchor(),
                ShuffleVector::from(_TV1, _TV2, outmask));
            op->hack_change_value(VIEWTYPE1(op->get_type(), _TV1, _TV2));
            return op;
        } break;
        case FN_Length: {
            CHECKARGS(1, 1);
            READ_STORAGETYPEOF(T);
            SCOPES_CHECK_RESULT(verify_real_vector(T));
            auto op = UnOp::from(UnOpLength, _T);
            op->hack_change_value(VIEWTYPE1(op->get_type(), _T));
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_Normalize: {
            CHECKARGS(1, 1);
            READ_TYPEOF(A);
            SCOPES_CHECK_RESULT(verify_real_ops(A));
            auto op = UnOp::from(UnOpNormalize, _A);
            op->hack_change_value(VIEWTYPE1(op->get_type(), _A));
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_Cross: {
            CHECKARGS(2, 2);
            READ_STORAGETYPEOF(A);
            READ_TYPEOF(B);
            SCOPES_CHECK_RESULT(verify_real_vector(A, 3));
            SCOPES_CHECK_RESULT(verify(typeof_A, B));
            auto op = BinOp::from(BinOpCross, _A, _B);
            op->hack_change_value(VIEWTYPE1(op->get_type(), _A));
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_ExtractValue: {
            CHECKARGS(2, 2);
            READ_NODEREF_STORAGETYPEOF(T);
            READ_STORAGETYPEOF(idx);
            uint64_t iidx = -1ull;
            switch(T->kind()) {
            case TK_Array:
            case TK_Matrix: {
                auto ai = cast<ArrayLikeType>(T);
                auto rq = try_qualifier<ReferQualifier>(typeof_T);
                if (rq) {
                    SCOPES_CHECK_RESULT(verify_integer(idx));
                } else {
                    // index must be constant
                    iidx = SCOPES_GET_RESULT(extract_integer_constant(_idx));
                    // only check for sized arrays
                    SCOPES_GET_RESULT(ai->type_at_index(iidx));
                }
            } break;
            case TK_Tuple: {
                iidx = SCOPES_GET_RESULT(extract_integer_constant(_idx));
                auto ti = cast<TupleType>(T);
                SCOPES_CHECK_RESULT(sanitize_tuple_index(call.anchor(), T, ti, iidx, _idx));
                SCOPES_GET_RESULT(ti->type_at_index(iidx));
            } break;
            default: {
                SCOPES_ERROR(InvalidArgumentTypeForBuiltin, b, T);
            } break;
            }
            auto rq = try_qualifier<ReferQualifier>(typeof_T);
            if (rq) {
                auto op = TypedValueRef(call.anchor(),
                    SCOPES_GET_RESULT(ctx.build_getelementref(call.anchor(), _T, { _idx })));
                auto retT = view_result_type(ctx, qualify(op->get_type(), { rq }), _T);
                op->hack_change_value(retT);
                return op;
            } else {
                assert(iidx != -1ull);
                auto op = ExtractValue::from(_T, iidx);
                op->hack_change_value(VIEWTYPE1(op->get_type(), _T));
                return TypedValueRef(call.anchor(), op);
            }
        } break;
        case FN_InsertValue: {
            CHECKARGS(3, 3);
            READ_AUTOMOVE_TYPEOF(AT);
            READ_AUTOMOVE_STORAGETYPEOF(ET);
            READ_INT_CONST(idx);
            bool movable = false;
            if (is_template_like_constant(_AT)) {
                movable = is_movable(typeof_ET);
            } else {
                movable = is_movable(AT);
                if (movable != is_movable(typeof_ET)) {
                    // borrow both arguments if one isn't movable
                    SCOPES_ERROR(MovableTypeMismatch, AT, typeof_ET);
                }
            }
            auto T = SCOPES_GET_RESULT(storage_type(AT));
            switch(T->kind()) {
            case TK_Array:
            case TK_Matrix: {
                auto ai = cast<ArrayLikeType>(T);
                SCOPES_CHECK_RESULT(verify(SCOPES_GET_RESULT(storage_type(SCOPES_GET_RESULT(ai->type_at_index(idx)))), ET));
            } break;
            case TK_Tuple: {
                auto ti = cast<TupleType>(T);
                SCOPES_CHECK_RESULT(verify(SCOPES_GET_RESULT(storage_type(SCOPES_GET_RESULT(ti->type_at_index(idx)))), ET));
            } break;
            default: {
                SCOPES_ERROR(InvalidArgumentTypeForBuiltin, b, T);
            } break;
            }
            auto op = InsertValue::from(_AT, _ET, idx);
            if (movable) {
                auto uq_AT = try_unique(AT);
                if (uq_AT) ctx.move(uq_AT->id, call);
                auto uq_ET = try_unique(typeof_ET);
                if (uq_ET) ctx.move(uq_ET->id, call);
                op->hack_change_value(UNIQUETYPE1(op->get_type()));
            } else {
                op->hack_change_value(VIEWTYPE1(op->get_type(), _AT, _ET));
            }
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_GetElementRef:
        case FN_GetElementPtr: {
            CHECKARGS(2, -1);
            const Type *T;
            bool is_ref = (b.value() == FN_GetElementRef);
            TypedValueRef dep;
            const Type *Tptr = nullptr;
            if (is_ref) {
                READ_NODEREF_TYPEOF(argT);
                T = Tptr = SCOPES_GET_RESULT(ref_to_ptr(argT));
                dep = _argT;
            } else {
                READ_STORAGETYPEOF(argT);
                T = argT;
                dep = _argT;
            }
            SCOPES_CHECK_RESULT(verify_kind<TK_Pointer>(T));
            auto pi = cast<PointerType>(T);
            T = pi->element_type;
            TypedValues indices;
            indices.reserve(argcount);
            if (!is_ref) {
                // first argument is pointer offset
                // not applicable to references
                READ_STORAGETYPEOF(arg);
                SCOPES_CHECK_RESULT(verify_integer(arg));
                indices.push_back(_arg);
            }
            while (argn < argcount) {
                const Type *ST = SCOPES_GET_RESULT(storage_type(T));
                switch(ST->kind()) {
                case TK_Array:
                case TK_Matrix: {
                    auto ai = cast<ArrayLikeType>(ST);
                    T = ai->element_type;
                    READ_STORAGETYPEOF(arg);
                    SCOPES_CHECK_RESULT(verify_integer(arg));
                    indices.push_back(_arg);
                } break;
                case TK_Tuple: {
                    auto ti = cast<TupleType>(ST);
                    READ_INT_CONST(arg);
                    SCOPES_CHECK_RESULT(sanitize_tuple_index(call.anchor(), ST, ti, arg, _arg));
                    T = SCOPES_GET_RESULT(ti->type_at_index(arg));
                    indices.push_back(ConstInt::from(TYPE_I32, arg));
                } break;
                default: {
                    SCOPES_ERROR(InvalidArgumentTypeForBuiltin, b, T);
                } break;
                }
            }
            T = pointer_type(T, pi->flags, pi->storage_class);
            TypedValueRef op;
            if (is_ref) {
                assert(Tptr);
                op = SCOPES_GET_RESULT(ctx.build_getelementref(call.anchor(), dep, indices));
            } else {
                op = GetElementPtr::from(dep, indices);
            }
            op->hack_change_value(VIEWTYPE1(op->get_type(), dep));
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_Deref: {
            CHECKARGS(1, 1);
            READ_AUTOMOVE_TYPEOF(T);
            (void)T;
            return _T;
        } break;
        case FN_Assign: {
            CHECKARGS(2, 2);
            READ_MOVE_STORAGETYPEOF(ElemT);
            READ_NODEREF_STORAGETYPEOF(DestT);
            {
                SCOPES_TRACE_PROVE_ARG(_DestT);
                auto rq = SCOPES_GET_RESULT(verify_refer(typeof_DestT));
                if (rq) {
                    SCOPES_CHECK_RESULT(verify_writable(rq, typeof_DestT));
                }
            }
            typeof_DestT = strip_qualifier<ReferQualifier>(typeof_DestT);
            //strip_qualifiers(ElemT);
            //strip_qualifiers(DestT);

            SCOPES_CHECK_RESULT(verify(ElemT, DestT));

            if (!is_plain(typeof_ElemT)) {
                auto uq = try_unique(typeof_ElemT);
                if (!uq) {
                    SCOPES_ERROR(UniqueValueExpected, _ElemT->get_type());
                }
                ctx.move(uq->id, call);
            }
            return TypedValueRef(SCOPES_GET_RESULT(ctx.build_assign(call.anchor(), _ElemT, _DestT)));
        } break;
        case FN_PtrToRef: {
            CHECKARGS(1, 1);
            READ_AUTOMOVE_TYPEOF(T);
            auto NT = SCOPES_GET_RESULT(ptr_to_ref(T));
            if (_T.isa<Pure>()) {
                if (!is_plain(T)) {
                    NT = view_type(NT, { GlobalUnique });
                }
                return TypedValueRef(
                    ref(call.anchor(), PureCast::from(NT, _T.cast<Pure>())));
            } else {
                auto uq = try_unique(T);
                if (uq) {
                    ctx.move(uq->id, call);
                }
                auto op = PtrToRef::from(_T);
                if (is_unique(T)) {
                    op->hack_change_value(UNIQUETYPE1(op->get_type()));
                } else {
                    op->hack_change_value(VIEWTYPE1(op->get_type(), _T));
                }
                return TypedValueRef(call.anchor(), op);
            }
        } break;
        case FN_RefToPtr: {
            CHECKARGS(1, 1);
            READ_NODEREF_TYPEOF(T);
            auto NT = SCOPES_GET_RESULT(ref_to_ptr(T));
            if (is_plain(T) && _T.isa<Pure>()) {
                return TypedValueRef(
                    ref(call.anchor(), PureCast::from(NT, _T.cast<Pure>())));
            } else {
                auto uq = try_unique(T);
                if (uq) {
                    ctx.move(uq->id, call);
                }
                auto op = RefToPtr::from(_T);
                if (is_unique(T)) {
                    op->hack_change_value(UNIQUETYPE1(op->get_type()));
                } else {
                    op->hack_change_value(VIEWTYPE1(op->get_type(), _T));
                }
                return TypedValueRef(call.anchor(), op);
            }
        } break;
        case FN_VolatileLoad:
        case FN_Load: {
            CHECKARGS(1, 1);
            READ_STORAGETYPEOF(T);
            SCOPES_CHECK_RESULT(verify_kind<TK_Pointer>(T));
            SCOPES_CHECK_RESULT(verify_readable(T));
            auto op = Load::from(_T, b.value() == FN_VolatileLoad);
            op->hack_change_value(VIEWTYPE1(op->get_type(), _T));
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_VolatileStore:
        case FN_Store: {
            CHECKARGS(2, 2);
            READ_STORAGETYPEOF(ElemT);
            READ_STORAGETYPEOF(DestT);
            SCOPES_CHECK_RESULT(verify_kind<TK_Pointer>(DestT));
            SCOPES_CHECK_RESULT(verify_writable(DestT));
            auto pi = cast<PointerType>(DestT);
            SCOPES_CHECK_RESULT(
                verify(SCOPES_GET_RESULT(storage_type(pi->element_type)), ElemT));
            if (!is_plain(typeof_DestT)) {
                auto uq = try_unique(typeof_ElemT);
                if (!uq) {
                    SCOPES_ERROR(UniqueValueExpected, _ElemT->get_type());
                }
                ctx.move(uq->id, call);
            }
            auto op = Store::from(_ElemT, _DestT, b.value() == FN_VolatileStore);
            return TypedValueRef(call.anchor(), op);
        } break;
        case OP_CmpXchg: {
            CHECKARGS(3, 3);
            READ_STORAGETYPEOF(DestT);
            READ_STORAGETYPEOF(Cmp);
            READ_STORAGETYPEOF(ElemT);
            SCOPES_CHECK_RESULT(verify_kind<TK_Pointer>(DestT));
            SCOPES_CHECK_RESULT(verify_readable(DestT));
            SCOPES_CHECK_RESULT(verify_writable(DestT));
            auto pi = cast<PointerType>(DestT);
            auto ET = SCOPES_GET_RESULT(storage_type(pi->element_type));
            SCOPES_CHECK_RESULT(verify(ET, Cmp));
            SCOPES_CHECK_RESULT(verify(ET, ElemT));
            if (!is_plain(typeof_DestT)) {
                auto uq = try_unique(typeof_ElemT);
                if (!uq) {
                    SCOPES_ERROR(UniqueValueExpected, _ElemT->get_type());
                }
                ctx.move(uq->id, call);
            }
            auto op = CmpXchg::from(_DestT, _Cmp, _ElemT);
            auto T = op->get_type();
            op->hack_change_value(UNIQUETYPE2(get_argument(T, 0), get_argument(T, 1)));
            return TypedValueRef(call.anchor(), op);
        } break;
        case OP_AtomicRMW: {
            CHECKARGS(3, 3);
            READ_BUILTIN_CONST(Op);
            READ_STORAGETYPEOF(DestT);
            READ_STORAGETYPEOF(ElemT);
            SCOPES_CHECK_RESULT(verify_kind<TK_Pointer>(DestT));
            SCOPES_CHECK_RESULT(verify_readable(DestT));
            SCOPES_CHECK_RESULT(verify_writable(DestT));
            auto pi = cast<PointerType>(DestT);
            auto ET = SCOPES_GET_RESULT(storage_type(pi->element_type));
            SCOPES_CHECK_RESULT(verify(ET, ElemT));
            if (!is_plain(typeof_DestT)) {
                auto uq = try_unique(typeof_ElemT);
                if (!uq) {
                    SCOPES_ERROR(UniqueValueExpected, _ElemT->get_type());
                }
                ctx.move(uq->id, call);
            }
            AtomicRMWOpKind opkind;
            switch(Op.value()) {
            #define T(NAME, OPNAME, BNAME) \
                case OPNAME: opkind = NAME; break;
                SCOPES_ATOMICRMW_OP_KIND()
            #undef T
                default: {
                    SCOPES_ERROR(UnsupportedBuiltin, Op);
                } break;
            }
            auto op = AtomicRMW::from(opkind, _DestT, _ElemT);
            op->hack_change_value(UNIQUETYPE1(op->get_type()));
            return TypedValueRef(call.anchor(), op);
        } break;
        case OP_Barrier: {
            CHECKARGS(1, 1);
            READ_INT_CONST(kind);
            auto op = Barrier::from((BarrierKind)kind);
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_Alloca: {
            CHECKARGS(1, 1);
            READ_TYPE_CONST(T);
            auto ST = strip_qualifiers(T);
            auto op = Alloca::from(ST);
            auto vq = try_view(T);
            if (vq) {
                op->hack_change_value(view_type(op->get_type(), vq->ids));
            } else {
                op->hack_change_value(UNIQUETYPE1(op->get_type()));
            }
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_AllocaArray: {
            CHECKARGS(2, 2);
            READ_TYPE_CONST(T);
            READ_STORAGETYPEOF(size);
            SCOPES_CHECK_RESULT(verify_integer(size));
            //auto ST = strip_qualifiers(T);
            auto op = Alloca::from(T, _size);
            auto vq = try_view(T);
            if (vq) {
                op->hack_change_value(view_type(op->get_type(), vq->ids));
            } else {
                op->hack_change_value(UNIQUETYPE1(op->get_type()));
            }
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_Malloc: {
            CHECKARGS(1, 1);
            READ_TYPE_CONST(T);
            //auto ST = strip_qualifiers(T);
            auto op = Malloc::from(T);
            auto vq = try_view(T);
            if (vq) {
                op->hack_change_value(view_type(op->get_type(), vq->ids));
            } else {
                op->hack_change_value(UNIQUETYPE1(op->get_type()));
            }
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_MallocArray: {
            CHECKARGS(2, 2);
            READ_TYPE_CONST(T);
            READ_STORAGETYPEOF(size);
            SCOPES_CHECK_RESULT(verify_integer(size));
            //auto ST = strip_qualifiers(T);
            auto op = Malloc::from(T, _size);
            auto vq = try_view(T);
            if (vq) {
                op->hack_change_value(view_type(op->get_type(), vq->ids));
            } else {
                op->hack_change_value(UNIQUETYPE1(op->get_type()));
            }
            return TypedValueRef(call.anchor(), op);
        } break;
        case FN_Free: {
            CHECKARGS(1, 1);
            READ_AUTOMOVE_STORAGETYPEOF(T);
            SCOPES_CHECK_RESULT(verify_kind<TK_Pointer>(T));
            //SCOPES_CHECK_RESULT(verify_writable(T));

            if (cast<PointerType>(T)->storage_class != SYM_Unnamed) {
                SCOPES_ERROR(InvalidArgumentTypeForBuiltin, b, T);
            }
            if (!is_plain(T)) {
                auto uq = try_unique(T);
                if (uq) {
                    ctx.move(uq->id, call);
                }
            }
            return TypedValueRef(call.anchor(), Free::from(_T));
        } break;
        case OP_ICmpEQ:
        case OP_ICmpNE:
        case OP_ICmpUGT:
        case OP_ICmpUGE:
        case OP_ICmpULT:
        case OP_ICmpULE:
        case OP_ICmpSGT:
        case OP_ICmpSGE:
        case OP_ICmpSLT:
        case OP_ICmpSLE: {
            CHECKARGS(2, 2);
            READ_TYPEOF(A); READ_TYPEOF(B);
            SCOPES_CHECK_RESULT(verify_integer_ops(A, B));
            ICmpKind pred;
            switch(b.value()) {
            #define T(NAME, BNAME) case OP_ ## NAME: pred = NAME; break;
            SCOPES_ICMP_KIND()
            #undef T
            default: assert(false); break;
            }
            auto op = ICmp::from(pred, _A, _B);
            //op->hack_change_value(VIEWTYPE1(op->get_type(), _A, _B));
            return TypedValueRef(call.anchor(), op);
        } break;
        case OP_FCmpOEQ:
        case OP_FCmpONE:
        case OP_FCmpORD:
        case OP_FCmpOGT:
        case OP_FCmpOGE:
        case OP_FCmpOLT:
        case OP_FCmpOLE:
        case OP_FCmpUEQ:
        case OP_FCmpUNE:
        case OP_FCmpUNO:
        case OP_FCmpUGT:
        case OP_FCmpUGE:
        case OP_FCmpULT:
        case OP_FCmpULE: {
            CHECKARGS(2, 2);
            READ_TYPEOF(A); READ_TYPEOF(B);
            SCOPES_CHECK_RESULT(verify_real_ops(A, B));
            FCmpKind pred;
            switch(b.value()) {
            #define T(NAME, BNAME) case OP_ ## NAME: pred = NAME; break;
            SCOPES_FCMP_KIND()
            #undef T
            default: assert(false); break;
            }
            auto op = FCmp::from(pred, _A, _B);
            //op->hack_change_value(VIEWTYPE1(op->get_type(), _A, _B));
            return TypedValueRef(call.anchor(), op);
        } break;
#define IARITH_NUW_NSW_OPS(NAME) \
        case OP_ ## NAME: \
        case OP_ ## NAME ## NUW: \
        case OP_ ## NAME ## NSW: { \
            CHECKARGS(2, 2); \
            READ_TYPEOF(A); READ_TYPEOF(B); \
            SCOPES_CHECK_RESULT(verify_integer_ops(A, B)); \
            BinOpKind opkind = (BinOpKind)0; \
            switch(b.value()) { \
            case OP_ ## NAME: opkind = BinOp ## NAME; break; \
            case OP_ ## NAME ## NUW: opkind = BinOp ## NAME ## NUW; break; \
            case OP_ ## NAME ## NSW: opkind = BinOp ## NAME ## NSW; break; \
            default: break; \
            } \
            auto op = BinOp::from(opkind, _A, _B); \
            op->hack_change_value(VIEWTYPE1(op->get_type(), _A, _B)); \
            return TypedValueRef(call.anchor(), op); \
        } break;
#define IARITH_OP(NAME) \
        case OP_ ## NAME: { \
            CHECKARGS(2, 2); \
            READ_TYPEOF(A); READ_TYPEOF(B); \
            SCOPES_CHECK_RESULT(verify_integer_ops(A, B)); \
            auto op = BinOp::from(BinOp ## NAME, _A, _B); \
            op->hack_change_value(VIEWTYPE1(op->get_type(), _A, _B)); \
            return TypedValueRef(call.anchor(), op); \
        } break;
#define FARITH_OP(NAME) \
        case OP_ ## NAME: { \
            CHECKARGS(2, 2); \
            READ_TYPEOF(A); READ_TYPEOF(B); \
            SCOPES_CHECK_RESULT(verify_real_ops(A, B)); \
            auto op = BinOp::from(BinOp ## NAME, _A, _B); \
            op->hack_change_value(VIEWTYPE1(op->get_type(), _A, _B)); \
            return TypedValueRef(call.anchor(), op); \
        } break;
#define FTRI_OP(NAME) \
        case OP_ ## NAME: { \
            CHECKARGS(3, 3); \
            READ_TYPEOF(A); READ_TYPEOF(B); READ_TYPEOF(C); \
            SCOPES_CHECK_RESULT(verify_real_ops(A, B, C)); \
            auto op = TriOp::from(TriOp ## NAME, _A, _B, _C); \
            op->hack_change_value(VIEWTYPE1(op->get_type(), _A, _B, _C)); \
            return TypedValueRef(call.anchor(), op); \
        } break;
#define IUN_OP(NAME) \
        case OP_ ## NAME: { \
            CHECKARGS(1, 1); \
            READ_TYPEOF(A); \
            SCOPES_CHECK_RESULT(verify_integer_ops(A)); \
            auto op = UnOp::from(UnOp ## NAME, _A); \
            op->hack_change_value(VIEWTYPE1(op->get_type(), _A)); \
            return TypedValueRef(call.anchor(), op); \
        } break;
#define FUN_OP(NAME) \
        case OP_ ## NAME: { \
            CHECKARGS(1, 1); \
            READ_TYPEOF(A); \
            SCOPES_CHECK_RESULT(verify_real_ops(A)); \
            auto op = UnOp::from(UnOp ## NAME, _A); \
            op->hack_change_value(VIEWTYPE1(op->get_type(), _A)); \
            return TypedValueRef(call.anchor(), op); \
        } break;
        SCOPES_ARITH_OPS()

#undef IARITH_NUW_NSW_OPS
#undef IARITH_OP
#undef FARITH_OP
#undef IUN_OP
#undef FUN_OP
#undef FTRI_OP
        default: {
            SCOPES_TRACE_PROVE_ARG(callee);

            SCOPES_ERROR(UnsupportedBuiltin, b);
        } break;
        }

        return ref(call.anchor(), ArgumentList::from({}));
    }
    if (!is_function_pointer(T)) {
        SCOPES_ERROR(InvalidCallee, callee->get_type());
    }
    const FunctionType *aft = extract_function_type(T);
    const FunctionType *ft = aft->strip_annotations();
    int numargs = (int)ft->argument_types.size();
    bool variadic = ft->vararg();
    if ((!variadic && (values.size() != numargs))
        || (variadic && (values.size() < numargs))) {
        if (values.size() > numargs) {
            SCOPES_ERROR(TooManyFunctionArguments, ft, values.size());
        } else if (values.size() < numargs) {
            SCOPES_ERROR(NotEnoughFunctionArguments, ft, values.size());
        } else {
            assert(false && "how did we get here?");
        }
    }
    // verify_function_argument_signature
    for (int i = 0; i < numargs; ++i) {
        SCOPES_TRACE_PROVE_ARG(values[i]);
        const Type *Ta = values[i]->get_type();
        const Type *Tb = ft->argument_types[i];
        if (is_reference(Ta) && !is_reference(Tb)) {
            SCOPES_CHECK_RESULT(build_deref(ctx, call.anchor(), values[i]));
            Ta = values[i]->get_type();
        }
        if (is_view(Tb)) {
            if (!is_view(Ta)) {
                SCOPES_CHECK_RESULT(build_view(ctx, call.anchor(), values[i]));
                Ta = values[i]->get_type();
                if (!is_view(Ta)) {
                    SCOPES_ERROR(ParameterTypeMismatch, Tb, Ta);
                }
            }
            Tb = strip_view(Tb);
            Ta = strip_view(Ta);
        } else if (is_unique(Tb)) {
            if (!is_view(Ta)) {
                assert(is_unique(Ta));
                Tb = strip_unique(Tb);
                Ta = strip_unique(Ta);
            }
        } else if (is_plain(Tb)) {
            //Tb = strip_lifetime(Tb);
            Ta = strip_lifetime(Ta);
        }
        Ta = strip_qualifier<KeyQualifier>(Ta);
        if (types_compatible(Tb, Ta)) {
            continue;
        }
        SCOPES_ERROR(ParameterTypeMismatch, Tb, Ta);
    }
    // ensure variadic parameters aren't references
    for (int i = numargs; i < values.size(); ++i) {
        DEREF(values[i]);
    }

    // build id map
    ID2SetMap idmap;
    idmap.reserve(numargs);
    // first map uniques
    for (int i = 0; i < numargs; ++i) {
        const Type *paramT = ft->argument_types[i];
        if (is_unique(paramT)) {
            const Type *argT = values[i]->get_type();
            auto paramu = get_unique(paramT);
            auto argu = get_unique(argT);
            // argument will be moved into the function
            ctx.move(argu->id, call);
            map_unique_id(idmap, paramu->id, argu->id);
        }
    }
    // then map views
    for (int i = 0; i < numargs; ++i) {
        const Type *paramT = ft->argument_types[i];
        if (is_view(paramT)) {
            const Type *argT = values[i]->get_type();
            if (!is_view(argT)) {
                StyledStream ss;
                ss << paramT << " != " << argT << std::endl;
            }
            auto paramv = get_view(paramT);
            auto argv = get_view(argT);
            for (auto id : paramv->sorted_ids) {
                auto it = idmap.find(id);
                if (it != idmap.end())
                    continue;
                assert(!argv->ids.empty());
                // unseen view id, map to argument ids
                for (auto vid : argv->ids) {
                    map_unique_id(idmap, id, vid);
                }
            }
        }
    }
    //const Type *art = aft->return_type;
    const Type *rt = remap_unique_return_arguments(ctx, idmap, ft->return_type);
    CallRef newcall = ref(call.anchor(), Call::from(rt, callee, values));
    //newcall->set_def_anchor(call->def_anchor());
    if (ft->has_exception()) {
        // todo: remap exception type
        newcall->except_body.set_parent(ctx.block);
        auto exceptctx = ctx.with_block(newcall->except_body);
        const Type *et = remap_unique_return_arguments(exceptctx, idmap, ft->except_type);
        auto exc = ref(call.anchor(), Exception::from(et));

        map_arguments_to_block(exceptctx, exc);
        newcall->except = exc;

        SCOPES_CHECK_RESULT(make_raise(exceptctx, call, exc));
    }

    #if 1
    // hack: rewrite valuerefs returned by globals matching sc_*_new
    //       to use the anchor of the calling expression
    //
    //       this could also be performed by wrapper macros inside the language
    if (rt == TYPE_ValueRef) {
        if (callee.isa<Global>()) {
            auto g = callee.cast<Global>();
            const char *name = g->name.name()->data;
            auto sz = g->name.name()->count;
            if (sz >= 7) {
                if (name[0] == 's' && name[1] == 'c' && name[2] == '_'
                    && name[sz-4] == '_' && name[sz-3] == 'n' && name[sz-2] == 'e' && name[sz-1] == 'w') {
                    SCOPES_CHECK_RESULT(ctx.append(newcall));
                    auto anchor = call.anchor();
                    // convert to valueref
                    newcall = ref(anchor, Call::from(TYPE_ValueRef,
                        g_sc_valueref_tag, {
                        ref(anchor, ConstPointer::anchor_from(anchor)),
                        newcall
                    }));
                }
            }
        }
    }
    #endif
    return TypedValueRef(newcall);
}

static LabelRef make_merge_label(
    const ASTContext &ctx, const Anchor *anchor) {
    LabelRef merge_label = ref(anchor, Label::from(LK_BranchMerge));
    merge_label->body.set_parent(ctx.block);
    return merge_label;
}

static SCOPES_RESULT(TypedValueRef) finalize_merge_label(const ASTContext &ctx,
    const LabelRef &merge_label, const char *by) {
    SCOPES_RESULT_TYPE(TypedValueRef);

    auto _void = empty_arguments_type();
    const Type *rtype = nullptr;
    for (auto merge : merge_label->merges) {
        if (merge->values.empty()) {
            rtype = _void;
            break;
        }
    }
    if (rtype == _void) {
        // ensure all merges return nothing
        for (auto merge : merge_label->merges) {
            merge->values.clear();
        }
    }

    IDSet valid;
    SCOPES_CHECK_RESULT(finalize_merges(ctx, merge_label, valid, by));
    if (!rtype) {
        const Anchor *last_anchor = merge_label.anchor();
        for (auto merge : merge_label->merges) {
            rtype = SCOPES_GET_RESULT(merge_value_type(by, rtype,
                arguments_type_from_typed_values(merge->values),
                last_anchor, merge.anchor()));
            last_anchor = merge.anchor();
        }
        rtype = ctx.fix_merge_type(rtype);
    }
    merge_label->change_type(rtype);
    merge_back_valid(ctx, valid, merge_label);
    SCOPES_CHECK_RESULT(ctx.append(merge_label));

    return TypedValueRef(merge_label);
}

static SCOPES_RESULT(TypedValueRef) prove_SwitchTemplate(const ASTContext &ctx,
    const SwitchTemplateRef &node) {
    SCOPES_RESULT_TYPE(TypedValueRef);

    auto newexpr = SCOPES_GET_RESULT(prove(ctx, node->expr));
    newexpr = ref(newexpr.anchor(), ExtractArgument::from(newexpr, 0));
    SCOPES_CHECK_RESULT(build_deref(ctx, newexpr.anchor(), newexpr));

    const Type *casetype = newexpr->get_type();
    const Anchor *last_anchor = newexpr.anchor();

    auto _switch = ref(node.anchor(), Switch::from(newexpr));

    LabelRef merge_label = make_merge_label(ctx, node.anchor());
    merge_label->splitpoints.insert(_switch.unref());

    ASTContext subctx = ctx.with_block(merge_label->body);
    SCOPES_CHECK_RESULT(subctx.append(_switch));

    Switch::Case *defaultcase = nullptr;
    Switch::Case *passcase = nullptr;

    for (auto &&_case : node->cases) {
        SCOPES_TRACE_PROVE_EXPR(ref(_case.anchor, g_none));
        Switch::Case *newcase = nullptr;
        if (_case.kind == CK_Default) {
            if (defaultcase) {
                SCOPES_ERROR(DuplicateSwitchDefaultCase);
            }
            newcase = &_switch->append_default(_case.anchor);
            defaultcase = newcase;
            passcase = nullptr;
        } else if (_case.kind == CK_Do) {
            if (!passcase) {
                SCOPES_ERROR(DoWithoutPass);
            }
            newcase = passcase;
            passcase = nullptr;
        } else {
            if (passcase && (_case.kind == CK_Case)) {
                SCOPES_ERROR(UnclosedPass);
            }
            auto newlit = SCOPES_GET_RESULT(prove(ctx, _case.literal));
            if ((newlit->get_type() != casetype) && has_typecast_handler()) {
                newlit = SCOPES_GET_RESULT(run_typecast_handler(ctx, newlit, casetype));
            }
            if (!newlit.isa<ConstInt>()) {
                SCOPES_ERROR(ValueKindMismatch, VK_ConstInt, newlit->kind());
            }
            casetype = SCOPES_GET_RESULT(
                merge_value_type("switch case literal", casetype, newlit->get_type(),
                last_anchor, newlit.anchor()));
            newcase = &_switch->append_pass(_case.anchor, newlit.cast<ConstInt>());
        }
        assert(_case.value);
        ASTContext newctx;
        TypedValueRef newvalue;
        if (_case.kind == CK_Do) {
            // append to last case
            newctx = ctx.with_block(newcase->body);
            newvalue = SCOPES_GET_RESULT(prove(newctx, _case.value));
        } else {
            newvalue = SCOPES_GET_RESULT(prove_block(subctx, newcase->body, _case.value, newctx));
        }
        if (_case.kind == CK_Pass) {
            passcase = newcase;
            SCOPES_CHECK_RESULT(validate_pass_block(subctx, newcase->body));
        } else {
            if (is_returning(newvalue->get_type())) {
                SCOPES_CHECK_RESULT(make_merge(newctx, newvalue.anchor(), merge_label, newvalue));
            }
        }
    }

    if (!defaultcase) {
        SCOPES_ERROR(MissingDefaultCase);
    }

    if (merge_label->merges.empty()) {
        // none of the paths are returning
        // cases do not need a merge label
        assert(ctx.block);
        ctx.merge_block(merge_label->body);
        return TypedValueRef(_switch);
    }

    return finalize_merge_label(ctx, merge_label, "switch case");
}

static SCOPES_RESULT(TypedValueRef) prove_CondTemplate(const ASTContext &ctx, const CondTemplateRef &node) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    LabelRef merge_label = make_merge_label(ctx, node.anchor());
    ASTContext subctx = ctx.with_block(merge_label->body);

    //assert(clause.anchor);
    //SCOPES_ANCHOR(clause.anchor);
    TypedValueRef newcond = SCOPES_GET_RESULT(prove(subctx, node->cond));
    {
        SCOPES_TRACE_PROVE_ARG(newcond);
        newcond = ref(newcond.anchor(),
            ExtractArgument::from(newcond, 0));
        SCOPES_CHECK_RESULT(build_tobool(subctx, node->cond.anchor(), newcond));
        SCOPES_CHECK_RESULT(build_deref_automove(subctx, newcond, newcond));
        auto condT = strip_qualifiers(newcond->get_type());
        if (condT != TYPE_Bool) {
            SCOPES_ERROR(ConditionNotBool, newcond->get_type());
        }
    }
    CondBrRef condbr = ref(newcond.anchor(), CondBr::from(newcond));
    merge_label->splitpoints.insert(condbr.unref());
    condbr->then_body.set_parent(&merge_label->body);
    condbr->else_body.set_parent(&merge_label->body);
    auto thenctx = subctx.with_block(condbr->then_body);
    auto thenresult = SCOPES_GET_RESULT(prove(thenctx, node->then_value));
    if (is_returning(thenresult->get_type())) {
        SCOPES_CHECK_RESULT(make_merge(thenctx, thenresult.anchor(), merge_label, thenresult));
    }
    merge_label->body.append(condbr);

    subctx = subctx.with_block(condbr->else_body);

    auto elseresult = SCOPES_GET_RESULT(prove(subctx, node->else_value));
    if (is_returning(elseresult->get_type())) {
        ASTContext elsectx = ctx.with_block(condbr->else_body);
        SCOPES_CHECK_RESULT(make_merge(elsectx, node.anchor(), merge_label, elseresult));
    }

    if (merge_label->merges.empty()) {
        // none of the paths are returning
        // conditions do not need a merge label
        assert(ctx.block);
        ctx.merge_block(merge_label->body);
        return TypedValueRef(condbr);
    }

    return finalize_merge_label(ctx, merge_label, "branch");
}

static SCOPES_RESULT(TypedValueRef) prove_Template(const ASTContext &ctx, const TemplateRef &_template) {
    FunctionRef frame = ctx.frame;
    assert(frame);
    return TypedValueRef(_template.anchor(), ConstPointer::closure_from(
        Closure::from(_template, frame)));
}

static SCOPES_RESULT(TypedValueRef) prove_Quote(const ASTContext &ctx, const QuoteRef &node) {
    //StyledStream ss;
    //ss << "before quote" << std::endl;
    //stream_ast(ss, node, StreamASTFormat());
    return quote(ctx, node->value);
    //ss << "after quote" << std::endl;
    //stream_ast(ss, value, StreamASTFormat());
}

static SCOPES_RESULT(TypedValueRef) prove_Unquote(const ASTContext &ctx, const UnquoteRef &node) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    SCOPES_ERROR(UnexpectedValueKind, node->kind());
}

SCOPES_RESULT(TypedValueRef) prove(const ASTContext &ctx, const ValueRef &node) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    assert(node);
    TypedValueRef result = SCOPES_GET_RESULT(ctx.frame->resolve(node, ctx.function));
    if (!result) {
        if (node.isa<TypedValue>()) {
            result = node.cast<TypedValue>();
        } else {
            // we shouldn't set an anchor here because sometimes the parent context
            // is more indicative than the node position
            //SCOPES_CHECK_RESULT(verify_stack());
            switch(node->kind()) {
#define T(NAME, BNAME, CLASS) \
            case NAME: result = SCOPES_GET_RESULT(prove_ ## CLASS(ctx, node.cast<CLASS>())); break;
            SCOPES_UNTYPED_VALUE_KIND()
#undef T
            default: assert(false);
            }
            assert(result);
            ctx.frame->bind(node, result);
            assert (ctx.block);
            #if 0
            if (!result->is_typed()) {
                StyledStream ss;
                stream_ast(ss, result, StreamASTFormat());
            }
            #endif
            if (result.isa<Instruction>()) {
                auto instr = result.cast<Instruction>();
                if (!instr->block) {
                    SCOPES_CHECK_RESULT(ctx.append(instr));
                }
            }

        }
    }
    return result;
}

// used by inlined functions
static SCOPES_RESULT(void) prove_inline_arguments(const ASTContext &ctx,
    const ParameterTemplates &params, const TypedValues &tmpargs) {
    SCOPES_RESULT_TYPE(void);
    int count = (int)params.size();
    for (int i = 0; i < count; ++i) {
        auto oldsym = params[i];
        TypedValueRef newval;
        if (oldsym->is_variadic()) {
            if ((i + 1) < count) {
                SCOPES_ERROR(VariadicParameterNotLast);
            }
            if ((i + 1) == (int)tmpargs.size()) {
                newval = tmpargs[i];
            } else {
                TypedValues args;
                for (int j = i; j < tmpargs.size(); ++j) {
                    args.push_back(tmpargs[j]);
                }
                newval = ref(oldsym.anchor(), ArgumentList::from(args));
            }
        } else if (i < tmpargs.size()) {
            newval = tmpargs[i];
        } else {
            newval = ref(oldsym.anchor(),
                ConstAggregate::none_from());
        }
        ctx.frame->bind(oldsym, newval);
    }
    return {};
}

static SCOPES_RESULT(TypedValueRef) prove_inline_body(const ASTContext &ctx,
    const Closure *cl, const TypedValues &nodes) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    auto frame = cl->frame;
    auto func = cl->func;
    SCOPES_TRACE_PROVE_TEMPLATE(func);
    Timer sum_prove_time(TIMER_Specialize);
    if (func->is_forward_decl()) {
        SCOPES_ERROR(CannotProveForwardDeclaration);
    }
    assert(func);
    auto anchor = func.anchor();
    //int count = (int)func->params.size();
    FunctionRef fn = ref(anchor, Function::from(func->name, {}));
    //fn->set_def_anchor(anchor);
    fn->original = ref(anchor, func);
    fn->frame = ref(frame.anchor(), frame);
    LabelRef label;
    if (!func->is_hidden()) {
        label = ref(anchor, Label::from(LK_Inline, func->name));
        fn->label = label;
    }
    fn->boundary = ctx.function;

    // inlines may escape caller loops
    ASTContext subctx = ctx.with_frame(fn);
    SCOPES_CHECK_RESULT(prove_inline_arguments(subctx, func->params, nodes));
    if (label) {
        ASTContext bodyctx;
        TypedValueRef result_value = SCOPES_GET_RESULT(
            prove_block(subctx, label->body, func->value, bodyctx));
        if (label->merges.empty()) {
            // label does not need a merge label
            assert(ctx.block);
            /* result_value = SCOPES_GET_RESULT(
                move_single_merge_value(bodyctx, ctx.block->depth,
                    result_value, "inline return")); */
            ctx.merge_block(label->body);
            return result_value;
        } else {
            if (is_returning(result_value->get_type())) {
                SCOPES_CHECK_RESULT(make_merge(bodyctx, result_value.anchor(), label, result_value));
            }
            IDSet valid;
            SCOPES_CHECK_RESULT(finalize_merges(ctx, label, valid, "inline return"));
            const Type *rtype = nullptr;
            const Anchor *last_anchor = result_value.anchor();
            for (auto merge : label->merges) {
                rtype = SCOPES_GET_RESULT(merge_value_type("inline return merge",
                    rtype,
                    arguments_type_from_typed_values(merge->values),
                    last_anchor, merge.anchor()));
                last_anchor = merge.anchor();
            }
            rtype = ctx.fix_merge_type(rtype);
            label->change_type(rtype);
            merge_back_valid(ctx, valid, label);
            SCOPES_CHECK_RESULT(ctx.append(label));
            return TypedValueRef(label);
        }
    } else {
        fn->label = ctx.frame->label;
        return prove(subctx, func->value);
    }
}

SCOPES_RESULT(TypedValueRef) prove_inline(const ASTContext &ctx,
    const Closure *cl, const TypedValues &nodes) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    auto func = cl->func;
    if (func->recursion >= SCOPES_MAX_RECURSIONS) {
        SCOPES_ERROR(RecursionOverflow, func->recursion);
    }
    func->recursion++;
    auto result = prove_inline_body(ctx, cl, nodes);
    func->recursion--;
    return result;
}

static SCOPES_RESULT(FunctionRef) prove_body(
    const FunctionRef &frame, const TemplateRef &func, Types types) {
    SCOPES_RESULT_TYPE(FunctionRef);
    Timer sum_prove_time(TIMER_Specialize);
    assert(func);
    canonicalize_argument_types(types);
    Function key(func->name, {});
    key.original = func;
    key.frame = frame;
    key.instance_args = types;
    auto it = functions.find(&key);
    if (it != functions.end())
        return ref(func.anchor(), *it);
    SCOPES_TRACE_PROVE_TEMPLATE(func);
    if (func->is_forward_decl()) {
        SCOPES_ERROR(CannotProveForwardDeclaration);
    }
    int count = (int)func->params.size();
    FunctionRef fn = ref(func.anchor(), Function::from(func->name, {}));
    fn->original = func;
    fn->frame = frame;
    fn->instance_args = types;
    fn->boundary = fn;
    for (int i = 0; i < count; ++i) {
        auto oldparam = func->params[i];
        if (oldparam->is_variadic()) {
            if ((i + 1) < count) {
                SCOPES_ERROR(VariadicParameterNotLast);
            }
            if ((i + 1) == (int)types.size()) {
                auto newparam = ref(oldparam.anchor(),
                    Parameter::from(oldparam->name, types[i]));
                fn->append_param(newparam);
                fn->bind(oldparam, newparam);
            } else {
                Types vtypes;
                TypedValues args;
                for (int j = i; j < types.size(); ++j) {
                    vtypes.push_back(types[j]);
                    auto newparam = ref(oldparam.anchor(),
                        Parameter::from(oldparam->name, types[j]));
                    fn->append_param(newparam);
                    args.push_back(newparam);
                }
                fn->bind(oldparam, ref(oldparam.anchor(),
                    ArgumentList::from(args)));
            }
        } else {
            const Type *T = TYPE_Nothing;
            if (i < types.size()) {
                T = types[i];
            }
            #if 0
            if (oldparam->is_typed()) {
                SCOPES_ANCHOR(oldparam->anchor());
                SCOPES_CHECK_RESULT(verify(oldparam->get_type(), T));
            }
            #endif
            auto newparam = ref(oldparam.anchor(),
                Parameter::from(oldparam->name, T));
            fn->append_param(newparam);
            fn->bind(oldparam, newparam);
        }
    }
    fn->build_valids();
    functions.insert(fn.unref());

    ASTContext fnctx = ASTContext::from_function(fn);
    ASTContext bodyctx = fnctx.with_block(fn->body);
    fn->body.valid = fn->valid;
    auto expr = SCOPES_GET_RESULT(prove(bodyctx, func->value));
    SCOPES_CHECK_RESULT(make_return(bodyctx, expr, expr));
    SCOPES_CHECK_RESULT(ensure_function_type(fn));
    SCOPES_CHECK_RESULT(finalize_returns_raises(bodyctx));
    //SCOPES_CHECK_RESULT(track(fnctx));
    fn->complete = true;
    return fn;
}

SCOPES_RESULT(FunctionRef) prove(const FunctionRef &frame, const TemplateRef &func, const Types &types) {
    SCOPES_RESULT_TYPE(FunctionRef);
    if (func->recursion >= SCOPES_MAX_RECURSIONS) {
        SCOPES_ERROR(RecursionOverflow, func->recursion);
    }
    func->recursion++;
    auto result = prove_body(frame, func, types);
    func->recursion--;
    return result;
}

SCOPES_RESULT(TypedValueRef) prove(const ValueRef &node) {
    SCOPES_RESULT_TYPE(TypedValueRef);
    if (!ast_context) {
        // fake one
        FunctionRef fn = ref(node.anchor(), Function::from(SYM_Unnamed, {}));
        fn->boundary = fn;
        ASTContext fnctx = ASTContext::from_function(fn);
        ASTContext bodyctx = fnctx.with_block(fn->body);
        auto result = SCOPES_GET_RESULT(prove(bodyctx, node));
        if (!result.isa<Pure>()) {
            SCOPES_ERROR(ResultMustBePure);
        }
        return result;
    } else {
        return prove(*ast_context, node);
    }
}

//------------------------------------------------------------------------------

} // namespace scopes