rev: eeadb6082f44956592d11d79c659cb265ac9d9d3 scopes/src/value.cpp -rw-r--r-- 67.6 KiB View raw Log this file
eeadb6082f44 — Leonard Ritter * SPIR-V: fixed unstructured branch validation error for `if/elseif/else` construct 9 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
/*
    The Scopes Compiler Infrastructure
    This file is distributed under the MIT License.
    See LICENSE.md for details.
*/

#include "value.hpp"
#include "error.hpp"
#include "scope.hpp"
#include "types.hpp"
#include "stream_expr.hpp"
#include "dyn_cast.inc"
#include "qualifier.inc"
#include "qualifiers.hpp"
#include "hash.hpp"
#include "anchor.hpp"
#include "prover.hpp"

#include <assert.h>
#include <unordered_set>

namespace scopes {

const char *get_value_kind_name(ValueKind kind) {
    switch(kind) {
#define T(NAME, BNAME, CLASS) \
    case NAME: return BNAME;
SCOPES_VALUE_KIND()
#undef T
    default: return "???";
    }
}

const char *get_value_class_name(ValueKind kind) {
    switch(kind) {
#define T(NAME, BNAME, CLASS) \
    case NAME: return #CLASS;
SCOPES_VALUE_KIND()
#undef T
    default: return "???";
    }
}

#define T(CLASS) \
    const Anchor *CLASS::def_anchor() const { \
        if (!_def_anchor) return unknown_anchor(); \
        return _def_anchor; \
    } \
    void CLASS::set_def_anchor(const Anchor *anchor) { \
        _def_anchor = anchor; \
    } \


SCOPES_DEFINED_VALUES()
#undef T

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

std::size_t ValueIndex::Hash::operator()(const ValueIndex & s) const {
    return hash2(std::hash<Value *>{}(s.value.unref()), s.index);
}

ValueIndex::ValueIndex(const TypedValueRef &_value, int _index)
    : value(_value), index(_index) {
repeat:
    switch(value->kind()) {
    case VK_Keyed: {
        value = value.cast<Keyed>()->value;
        goto repeat;
    } break;
    case VK_ArgumentList: {
        auto al = value.cast<ArgumentList>();
        if (index < al->values.size()) {
            value = al->values[index];
            index = 0;
            goto repeat;
        } else {
            assert(false);
        }
    } break;
    case VK_ExtractArgument: {
        auto ea = value.cast<ExtractArgument>();
        if (index == 0) {
            value = ea->value;
            index = ea->index;
            goto repeat;
        } else {
            assert(false);
        }
    } break;
    default: break;
    }
}

bool ValueIndex::operator ==(const ValueIndex &other) const {
    return value.unref() == other.value.unref() && index == other.index;
}

const Type *ValueIndex::get_type() const {
    return get_argument(value->get_type(), index);
}

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

KeyedTemplate::KeyedTemplate(Symbol _key, const ValueRef &node)
    : UntypedValue(VK_KeyedTemplate), key(_key), value(node)
{
}

ValueRef KeyedTemplate::from(Symbol key, ValueRef node) {
    assert(node);
    if (node.isa<TypedValue>()) {
        return Keyed::from(key, node.cast<TypedValue>());
    } else {
        if (node.isa<KeyedTemplate>()) {
            node = node.cast<KeyedTemplate>()->value;
        }
        return ref(node.anchor(), new KeyedTemplate(key, node));
    }
}

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

Keyed::Keyed(const Type *type, Symbol _key, const TypedValueRef &node)
    : TypedValue(VK_Keyed, type), key(_key), value(node)
{
}

TypedValueRef Keyed::from(Symbol key, TypedValueRef node) {
    assert(node);
    if (node.isa<Keyed>()) {
        node = node.cast<Keyed>()->value;
    }
    auto T = node->get_type();
    auto NT = key_type(key, T);
    if (T == NT)
        return node;
    if (node.isa<Pure>()) {
        return PureCast::from(NT, node.cast<Pure>());
    } else {
        return ref(node.anchor(), new Keyed(NT, key, node));
    }
}

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

// when joining two argument lists, all but the last argument can be flattened

ArgumentListTemplate::ArgumentListTemplate(const Values &values)
    : UntypedValue(VK_ArgumentListTemplate), _values(values) {
}

/*
bool ArgumentListTemplate::is_constant() const {
    for (auto val : _values) {
        assert(val);
        if (!val.isa<Const>())
            return false;
    }
    return true;
}
*/

bool ArgumentListTemplate::empty() const {
    return _values.empty();
}

const Values &ArgumentListTemplate::values() const {
    return _values;
}

ValueRef ArgumentListTemplate::from(const Values &values) {
    if (values.size() == 1) {
        return values[0];
    }
    bool is_typed = true;
    for (auto value : values) {
        if (value.isa<UntypedValue>()) {
            is_typed = false;
            break;
        }
    }
    if (is_typed) {
        // all values are typed - promote to ArgumentList
        TypedValues typed_values;
        typed_values.reserve(values.size());
        for (auto value : values) {
            typed_values.push_back(value.cast<TypedValue>());
        }
        return ref(unknown_anchor(), ArgumentList::from(typed_values));
    } else {
        #if 0
        assert(!values.empty());
        Values newvalues;
        newvalues.reserve(values.size());
        int count = (int)values.size() - 1;
        // truncate all but last argument
        for (int i = 0; i < count; ++i) {
            auto &&arg = values[i];
            if (arg.isa<ArgumentListTemplate>() || arg.isa<ArgumentList>()) {
                newvalues.push_back(ref(arg.anchor(), ExtractArgumentTemplate::from(arg, 0)));
            } else {
                newvalues.push_back(arg);
            }
        }
        auto &&arg = values.back();
        /*if (arg.isa<ArgumentListTemplate>()) {
            for (auto &&val : (arg.cast<ArgumentListTemplate>())->values()) {
                newvalues.push_back(val);
            }
        } else if (arg.isa<ArgumentList>()) {
            auto al = arg.cast<ArgumentList>();
            for (auto &&val : al->values) {
                newvalues.push_back(val);
            }
        } else*/ {
            newvalues.push_back(arg);
        }
        #endif
        return ref(unknown_anchor(), new ArgumentListTemplate(values));
    }
}

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

static TypedValueRef _empty_argument_list;

const Type *arguments_type_from_typed_values(const TypedValues &_values) {
    Types types;
    for (auto value : _values) {
        types.push_back(value->get_type());
    }
    return arguments_type(types);
}

ArgumentList::ArgumentList(const TypedValues &_values)
    : TypedValue(VK_ArgumentList, arguments_type_from_typed_values(_values)) {
    values.reserve(get_argument_count(get_type()));
    int idx = 1;
    for (auto value : _values) {
        if (value.isa<ArgumentList>()) {
            auto al = value.cast<ArgumentList>();
            for (auto &&value : al->values) {
                values.push_back(value);
                if (idx != _values.size())
                    break;
            }
        } else {
            values.push_back(value);
        }
        idx++;
    }
    assert(get_argument_count(get_type()) == values.size());
}

bool ArgumentList::is_constant() const {
    for (auto val : values) {
        assert(val);
        if (!val.isa<Const>())
            return false;
    }
    return true;
}

TypedValueRef ArgumentList::from(const TypedValues &values) {
    if (values.size() == 0) {
        if (!_empty_argument_list)
            _empty_argument_list = ref(unknown_anchor(), new ArgumentList(values));
        return _empty_argument_list;
    } else if (values.size() == 1) {
        return values[0];
    }
    return ref(unknown_anchor(), new ArgumentList(values));
}

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

ExtractArgumentTemplate::ExtractArgumentTemplate(const ValueRef &_value, int _index, bool _vararg)
    : UntypedValue(VK_ExtractArgumentTemplate), index(_index), value(_value), vararg(_vararg) {
    assert(index >= 0);
}

ValueRef ExtractArgumentTemplate::from(
    const ValueRef &_value, int index, bool vararg) {
    ValueRef value = _value;
//repeat:
    if (value.isa<TypedValue>()) {
        if (vararg) {
            return ExtractArgument::variadic_from(value.cast<TypedValue>(), index);
        } else {
            return ExtractArgument::from(value.cast<TypedValue>(), index);
        }
    }
    // doesn't work right: we might be un-singling mrv's
    /* else if (value.isa<ArgumentListTemplate>()) {
        auto al = value.cast<ArgumentListTemplate>();
        auto &&values = al->values();
        if (vararg) {
            if ((index == 0) || values.empty()) {
                return value;
            }
        } else {
            if (values.empty()) {
                return ref(value.anchor(), ConstAggregate::none_from());
            }
            if ((index + 1) < values.size()) {
                // we can extract directly
                return values[index];
            }
            auto lastkind = values.back()->kind();
            if ((lastkind == VK_ArgumentListTemplate) || (lastkind == VK_ArgumentList)) {
                // continue searching in next list
                value = values.back();
                index = index + 1 - (int)values.size();
                goto repeat;
            }
        }
    } else if (!vararg && value.isa<ExtractArgumentTemplate>()) {
        auto eat = value.cast<ExtractArgumentTemplate>();
        if (eat->vararg) {
            value = eat->value;
            index += eat->index;
            goto repeat;
        } else if (index == 0) {
            return value;
        } else {
            return ref(value.anchor(), ConstAggregate::none_from());
        }
    }
    */
    return ref(value.anchor(),
        new ExtractArgumentTemplate(value, index, vararg));
}

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

ExtractArgument::ExtractArgument(const Type *type, const TypedValueRef &_value, int _index)
    : TypedValue(VK_ExtractArgument, type),
        index(_index), value(_value) {
}

TypedValueRef ExtractArgument::variadic_from(const TypedValueRef &value, int index) {
    if (!index) return value;
    auto T = value->get_type();
    if (!is_returning(T)) return value;
    int count = get_argument_count(T);
    TypedValues values;
    if (value.isa<ArgumentList>()) {
        auto al = value.cast<ArgumentList>();
        for (int i = index; i < count; ++i) {
            values.push_back(al->values[i]);
        }
    } else {
        for (int i = index; i < count; ++i) {
            //auto argT = get_argument(T, i);
            values.push_back(from(value, i));
        }
    }
    return ref(value.anchor(), ArgumentList::from(values));
}

TypedValueRef ExtractArgument::from(const TypedValueRef &value, int index) {
    auto T = value->get_type();
    if (!is_returning(T)) return value;
    int count = get_argument_count(T);
    if (index >= count)
        return ref(value.anchor(), ConstAggregate::none_from());
    if (value.isa<ArgumentList>()) {
        auto al = value.cast<ArgumentList>();
        assert (index < al->values.size());
        return al->values[index];
    } else if ((count == 1) && (index == 0)) {
        return value;
    } else {
        auto argT = get_argument(T, index);
        return ref(value.anchor(),
            new ExtractArgument(argT, value, index));
    }
}

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

Template::Template(Symbol _name, const ParameterTemplates &_params, const ValueRef &_value)
    : UntypedValue(VK_Template),
        name(_name), params(_params), value(_value),
        _is_inline(false), docstring(nullptr),
        recursion(0) {
    int index = 0;
    for (auto param : params) {
        param->set_owner(ref(unknown_anchor(), this), index++);
    }
}

bool Template::is_forward_decl() const {
    return !value;
}

void Template::set_inline() {
    _is_inline = true;
}

bool Template::is_inline() const {
    return _is_inline;
}

bool Template::is_hidden() const {
    return is_inline() && name == SYM_HiddenInline;
}

void Template::append_param(const ParameterTemplateRef &sym) {
    sym->set_owner(ref(unknown_anchor(), this), params.size());
    params.push_back(sym);
}

TemplateRef Template::from(Symbol name,
    const ParameterTemplates &params, const ValueRef &value) {
    return ref(unknown_anchor(), new Template(name, params, value));
}

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

Function::UniqueInfo::UniqueInfo(const ValueIndex& _value)
    : value(_value) {
}

int Function::UniqueInfo::get_depth() const {
    return value.value->get_depth();
}

Function::Function(Symbol _name, const Parameters &_params)
    : Pure(VK_Function, TYPE_Unknown),
        name(_name), params(_params),
        docstring(nullptr),
        frame(FunctionRef()),
        boundary(FunctionRef()),
        original(TemplateRef()),
        label(LabelRef()),
        complete(false),
        nextid(FirstUniquePrivate),
        returning_hint(TYPE_NoReturn),
        raising_hint(TYPE_NoReturn),
        returning_anchor(nullptr),
        raising_anchor(nullptr) {
    body.depth = 1;
    int index = 0;
    for (auto param : params) {
        param->set_owner(ref(unknown_anchor(), this), index++);
    }
}

const Function::UniqueInfo &Function::get_unique_info(int id) const {
    assert(id != 0);
    assert(id != GlobalUnique);
    assert(id < nextid);
    auto it = uniques.find(id);
    assert(it != uniques.end());
    return it->second;
}

int Function::unique_id() {
    return nextid++;
}

void Function::try_bind_unique(const TypedValueRef &value) {
    auto T = value->get_type();
    int count = get_argument_count(T);
    for (int i = 0; i < count; ++i) {
        auto argT = get_argument(T, i);
        if (is_unique(argT)) {
            bind_unique(UniqueInfo(ValueIndex(value, i)));
        }
    }
}

void Function::bind_unique(const UniqueInfo &info) {
    auto uq = get_unique(info.value.get_type());
    auto result = uniques.insert({uq->id, info});
    if (!result.second) {
        StyledStream ss;
        ss << "internal error: duplicate unique " << uq->id;
        ss << " (was " << result.first->second.value;
        ss << ", is " << info.value << ")" << std::endl;
    }
    assert(result.second);
}

bool Function::key_equal(const Function *other) const {
    return this == other;
}

std::size_t Function::hash() const {
    return std::hash<const Function *>{}(this);
}

bool Function::is_typed() const {
    assert(_type);
    return _type != TYPE_Unknown;
}

void Function::set_type(const Type *type) {
    assert(!is_typed());
    assert(type);
    _type = type;
}

void Function::change_type(const Type *type) {
    assert(is_typed());
    assert(type);
    _type = type;
}

const Anchor *Function::get_best_mover_anchor(int id) {
    auto it = movers.find(id);
    if (it != movers.end()) {
        return it->second.anchor();
    } else {
        auto info = get_unique_info(id);
        return info.value.value.anchor();
    }
}

void Function::hint_mover(int id, const ValueRef &where) {
    auto result = movers.insert({ id, where });
    if (!result.second)
        result.second = where;
}

void Function::build_valids() {
    assert(valid.empty());
    // add uniques
    for (auto sym : params) {
        auto T = sym->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) {
                assert(uq->id);
                bind_unique(UniqueInfo(ValueIndex(sym, i)));
                valid.insert(uq->id);
            }
        }
    }
    // add views
    for (auto sym : params) {
        auto T = sym->get_type();
        int count = get_argument_count(T);
        for (int i = 0; i < count; ++i) {
            auto argT = get_argument(T, i);
            auto vq = try_view(argT);
            if (vq) {
                for (auto id : vq->ids) {
                    assert(id);
                    if (!valid.count(id)) {
                        auto result = uniques.insert({id,
                            UniqueInfo(ValueIndex(sym, i))});
                        assert(result.second);
                        valid.insert(id);
                    }
                }
            }
        }
    }
    original_valid = valid;
}

void Function::append_param(const ParameterRef &sym) {
    sym->set_owner(ref(unknown_anchor(), this), params.size());
    params.push_back(sym);
}

TypedValueRef Function::resolve_local(const ValueRef &node) const {
    auto it = map.find(node.unref());
    if (it == map.end())
        return TypedValueRef();
    return it->second;
}

TypedValueRef Function::unsafe_resolve(const ValueRef &node) const {
    auto fn = this;
    while (fn) {
        auto val = fn->resolve_local(node);
        if (val) return val;
        fn = fn->frame.unref();
    }
    return TypedValueRef();
}

SCOPES_RESULT(TypedValueRef) Function::resolve(const ValueRef &node, const FunctionRef &_boundary) const {
    SCOPES_RESULT_TYPE(TypedValueRef)
    auto fn = this;
    while (fn) {
        auto val = fn->resolve_local(node);
        if (val) {
            if ((fn->boundary != _boundary) && !val->is_accessible()) {
                SCOPES_TRACE_PROVE_ARG(node);
                SCOPES_ERROR(VariableOutOfScope, val->get_type(), val.anchor());
            }
            return val;
        }
        fn = fn->frame.unref();
    }
    return TypedValueRef();
}

void Function::bind(const ValueRef &oldnode, const TypedValueRef &newnode) {
    auto it = map.insert({oldnode.unref(), newnode});
    if (!it.second) {
        it.first->second = newnode;
    }
}

FunctionRef Function::from(Symbol name,
    const Parameters &params) {
    return ref(unknown_anchor(), new Function(name, params));
}

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

static const Type *pointer_for_global_type(const Type *type, size_t flags, Symbol storage_class) {
    size_t ptrflags = 0;
    if (storage_class == SYM_SPIRV_StorageClassUniformConstant) {
        // regular uniforms are always readonly, even though the underlying sampler may have other flags
    } else {
        if (flags & GF_NonWritable)
            ptrflags |= PTF_NonWritable;
        else if (flags & GF_NonReadable)
            ptrflags |= PTF_NonReadable;
    }
    return pointer_type(type, ptrflags, storage_class);
}

Global::Global(const Type *type, Symbol _name, size_t _flags, Symbol _storage_class)
    : Pure(VK_Global, pointer_for_global_type(type, _flags, _storage_class)),
        element_type(type), name(_name), flags(_flags), storage_class(_storage_class) {
}

bool Global::key_equal(const Global *other) const {
    return this == other;
}

std::size_t Global::hash() const {
    return std::hash<const Global *>{}(this);
}

GlobalRef Global::from(const Type *type, Symbol name, size_t flags, Symbol storage_class) {
    if ((storage_class == SYM_SPIRV_StorageClassUniform)
        && !(flags & GF_BufferBlock)) {
        flags |= GF_Block;
    }
    return ref(unknown_anchor(), new Global(type, name, flags, storage_class));
}

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

template<typename T>
struct ConstSet {
    struct Hash {
        std::size_t operator()(const T *k) const {
            return k->hash();
        }
    };

    struct Equal {
        std::size_t operator()(const T *self, const T *other) const {
            return self->key_equal(other);
        }
    };

    std::unordered_set<T *, Hash, Equal> map;

    template<typename ... Args>
    TValueRef<T> from(Args ... args) {
        T key(args ...);
        auto it = map.find(&key);
        if (it != map.end()) {
            return ref(unknown_anchor(), *it);
        }
        auto val = new T(args ...);
        map.insert(val);
        return ref(unknown_anchor(), val);
    }
};

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

static ConstSet<GlobalString> globalstrings;

GlobalString::GlobalString(const char *_data, size_t _count)
    : Pure(VK_GlobalString,
        refer_type(
            array_type(TYPE_I8, _count).assert_ok(),
            PTF_NonWritable,
            SYM_SPIRV_StorageClassPrivate)),
        value(_data, _count) {
}

bool GlobalString::key_equal(const GlobalString *other) const {
    return value == other->value;
}

std::size_t GlobalString::hash() const {
    return hash_bytes(value.data(), value.size());
}

GlobalStringRef GlobalString::from(const char *_data, size_t _count) {
    return globalstrings.from(_data, _count);
}

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

PureCast::PureCast(const Type *type, const PureRef &_value)
    : Pure(VK_PureCast, type), value(_value) {}

bool PureCast::key_equal(const PureCast *other) const {
    return get_type() == other->get_type()
        && value == other->value;
}

std::size_t PureCast::hash() const {
    return value->hash();
}

PureRef PureCast::from(const Type *type, PureRef value) {
    if (value.isa<PureCast>()) {
        value = value.cast<PureCast>()->value;
    }
    if (value->get_type() == type)
        return value;
    if (value.isa<Undef>()) {
        return Undef::from(type);
    } else if (value.isa<Const>()
        && (storage_kind(type) == storage_kind(value->get_type()))) {
        ConstRef result;
        switch (value->kind()) {
        case VK_ConstInt: {
            auto node = value.cast<ConstInt>();
            result = ConstInt::from(type, node->words);
        } break;
        case VK_ConstReal: {
            auto node = value.cast<ConstReal>();
            result = ConstReal::from(type, node->value);
        } break;
        case VK_ConstAggregate: {
            auto node = value.cast<ConstAggregate>();
            result = ConstAggregate::from(type, node->values);
        } break;
        case VK_ConstPointer: {
            auto node = value.cast<ConstPointer>();
            result = ConstPointer::from(type, node->value);
        } break;
        default:
            assert (false && "unknown const kind");
            break;
        }
        return PureRef(value.anchor(), result);
    } else {
        return ref(value.anchor(), new PureCast(type, value));
    }
}

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

bool Undef::key_equal(const Undef *other) const {
    return get_type() == other->get_type();
}

std::size_t Undef::hash() const {
    return std::hash<const Type *>{}(get_type());
}

Undef::Undef(const Type *_type)
    : Pure(VK_Undef, _type) {
}

UndefRef Undef::from(const Type *type) {
    return ref(unknown_anchor(), new Undef(type));
}

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

Block::Block()
    : depth(-1), insert_index(0), tag_traceback(true), terminator(InstructionRef())
{}

bool Block::is_valid(const IDSet &ids) const {
    int _id = 0;
    return is_valid(ids, _id);
}

bool Block::is_valid(const ValueIndex &value) const {
    int _id = 0;
    return is_valid(value, _id);
}

bool Block::is_valid(const ValueIndex &value, int &_id) const {
    auto T = value.get_type();
    auto vq = try_view(T);
    if (vq) {
        return is_valid(vq->ids, _id);
    }
    auto uq = try_unique(T);
    if (uq) {
        if (!is_valid(uq->id)) {
            _id = uq->id;
            return false;
        }
    }
    return true;
}

bool Block::is_valid(const IDSet &ids, int &_id) const {
    for (auto id : ids) {
        if (!is_valid(id)) {
            _id = id;
            return false;
        }
    }
    return true;
}

bool Block::is_valid(int id) const {
    return (id == GlobalUnique)?true:valid.count(id);
}

void Block::move(int id) {
    assert(is_valid(id));
    valid.erase(id);
}

bool Block::is_terminated() const {
    return terminator.unref() != nullptr;
}

void Block::set_parent(Block *_parent) {
    assert(depth < 0);
    assert(_parent);
    depth = _parent->depth + 1;
    tag_traceback = _parent->tag_traceback;
    // copy valids from parent
    if (valid.empty()) {
        valid = _parent->valid;
    } else {
        // block has some preconfigured values
        valid = union_idset(valid, _parent->valid);
    }
}

void Block::clear() {
    body.clear();
    valid.clear();
    terminator = InstructionRef();
}

void Block::migrate_from(Block &source) {
    for (auto arg : source.body) {
        arg->block = nullptr;
        append(arg);
    }
    if (source.terminator) {
        terminator = source.terminator;
        terminator->block = this;
    }
    // equivalent to removing all values no longer valid, and adding
    // all values that have since been added
    valid = source.valid;
    source.clear();
}

bool Block::empty() const {
    return body.empty() && !terminator;
}

void Block::insert_at(int index) {
    assert((index >= 0) && (index <= body.size()));
    insert_index = index;
}

void Block::insert_at_end() {
    insert_index = body.size();
}

void Block::append(const InstructionRef &node) {
    assert(!node->block);
    node->block = this;
    if (!is_returning(node->get_type())) {
        assert(!terminator);
        assert(insert_index == body.size());
        terminator = node;
    } else {
        auto T = node->get_type();
        auto count = get_argument_count(T);
        for (int i = 0; i < count; ++i) {
            auto uq = try_unique(get_argument(T, i));
            if (uq) {
                assert(uq->id);
                valid.insert(uq->id);
            }
        }
        body.insert(body.begin() + insert_index, node);
        insert_index++;
    }
}

Block::DataMap &Block::get_channel(Symbol name) {
    DataMap *&map = channels[name];
    if (!map) {
        map = new DataMap();
    }
    return *map;
}

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

Expression::Expression(const Values &_body, const ValueRef &_value)
    : UntypedValue(VK_Expression), body(_body), value(_value), scoped(true) {
}

void Expression::append(const ValueRef &node) {
    assert(node);
    if (value && !value.isa<Pure>()) {
        body.push_back(value);
    }
    value = node;
}

ExpressionRef Expression::scoped_from(const Values &nodes, const ValueRef &value) {
    return ref(unknown_anchor(), new Expression(nodes, value));
}

ExpressionRef Expression::unscoped_from(const Values &nodes, const ValueRef &value) {
    auto expr = new Expression(nodes, value);
    expr->scoped = false;
    return ref(unknown_anchor(), expr);
}

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

CondBr::CondBr(const TypedValueRef &_cond)
    : Instruction(VK_CondBr, TYPE_NoReturn), cond(_cond)
{}

CondBrRef CondBr::from(const TypedValueRef &cond) {
    validate_instruction(cond);
    return ref(unknown_anchor(), new CondBr(cond));
}

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

CondTemplate::CondTemplate(
    const ValueRef &_cond, const ValueRef &_then_value, const ValueRef &_else_value)
    : UntypedValue(VK_CondTemplate),
    cond(_cond), then_value(_then_value), else_value(_else_value) {
}

CondTemplateRef CondTemplate::from(
    const ValueRef &cond, const ValueRef &then_value, const ValueRef &else_value) {
    return ref(unknown_anchor(), new CondTemplate(cond, then_value, else_value));
}

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

SwitchTemplate::SwitchTemplate(const ValueRef &_expr, const Cases &_cases)
    : UntypedValue(VK_SwitchTemplate), expr(_expr), cases(_cases)
{}

SwitchTemplateRef SwitchTemplate::from(const ValueRef &expr, const Cases &cases) {
    return ref(unknown_anchor(), new SwitchTemplate(expr, cases));
}

void SwitchTemplate::append_case(const ValueRef &literal, const ValueRef &value) {
    assert(literal);
    assert(value);
    Case _case;
    _case.anchor = value.anchor();
    _case.kind = CK_Case;
    _case.literal = literal;
    _case.value = value;
    cases.push_back(_case);
}

void SwitchTemplate::append_pass(const ValueRef &literal, const ValueRef &value) {
    assert(literal);
    assert(value);
    Case _case;
    _case.anchor = value.anchor();
    _case.kind = CK_Pass;
    _case.literal = literal;
    _case.value = value;
    cases.push_back(_case);
}

void SwitchTemplate::append_do(const ValueRef &value) {
    assert(value);
    Case _case;
    _case.anchor = value.anchor();
    _case.kind = CK_Do;
    _case.value = value;
    cases.push_back(_case);
}

void SwitchTemplate::append_default(const ValueRef &value) {
    assert(value);
    Case _case;
    _case.anchor = value.anchor();
    _case.kind = CK_Default;
    _case.value = value;
    cases.push_back(_case);
}

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

Switch::Switch(const TypedValueRef &_expr, const Cases &_cases)
    : Instruction(VK_Switch, TYPE_NoReturn), expr(_expr), cases(_cases)
{}

SwitchRef Switch::from(const TypedValueRef &expr, const Cases &cases) {
    validate_instruction(expr);
    return ref(unknown_anchor(), new Switch(expr, cases));
}

Switch::Case &Switch::append_pass(const Anchor *anchor, const ConstIntRef &literal) {
    assert(literal);
    Case *_case = new Case();
    _case->anchor = anchor;
    _case->kind = CK_Pass;
    _case->literal = literal;
    cases.push_back(_case);
    return *cases.back();
}

Switch::Case &Switch::append_default(const Anchor *anchor) {
    Case *_case = new Case();
    _case->anchor = anchor;
    _case->kind = CK_Default;
    cases.push_back(_case);
    return *cases.back();
}

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

ParameterTemplate::ParameterTemplate(Symbol _name, bool _variadic)
    : UntypedValue(VK_ParameterTemplate), name(_name), variadic(_variadic),
        owner(TemplateRef()), index(-1) {
}

ParameterTemplateRef ParameterTemplate::from(Symbol name) {
    return ref(unknown_anchor(), new ParameterTemplate(name, false));
}

ParameterTemplateRef ParameterTemplate::variadic_from(Symbol name) {
    return ref(unknown_anchor(), new ParameterTemplate(name, true));
}

bool ParameterTemplate::is_variadic() const {
    return variadic;
}

void ParameterTemplate::set_owner(const TemplateRef &_owner, int _index) {
    assert(!owner);
    owner = _owner;
    index = _index;
}

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

Parameter::Parameter(Symbol _name, const Type *_type)
    : TypedValue(VK_Parameter, _type), name(_name),
        owner(FunctionRef()), block(nullptr), index(-1) {
}

ParameterRef Parameter::from(Symbol name, const Type *type) {
    return ref(unknown_anchor(), new Parameter(name, type));
}

void Parameter::set_owner(const FunctionRef &_owner, int _index) {
    assert(!owner);
    owner = _owner;
    index = _index;
}

void Parameter::retype(const Type *T) {
    _type = T;
}

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

LoopArguments::LoopArguments(const LoopRef &_loop)
    : UntypedValue(VK_LoopArguments), loop(_loop) {}

LoopArgumentsRef LoopArguments::from(const LoopRef &loop) {
    return ref(unknown_anchor(), new LoopArguments(loop));
}

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

LoopLabelArguments::LoopLabelArguments(const Type *type)
    : TypedValue(VK_LoopLabelArguments, type), loop(LoopLabelRef()) {}

LoopLabelArgumentsRef LoopLabelArguments::from(const Type *type) {
    return ref(unknown_anchor(), new LoopLabelArguments(type));
}

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

Exception::Exception(const Type *type)
    : TypedValue(VK_Exception, type) {
}

ExceptionRef Exception::from(const Type *type) {
    return ref(unknown_anchor(), new Exception(type));
}

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

CallTemplate::CallTemplate(const ValueRef &_callee, const Values &_args)
    : UntypedValue(VK_CallTemplate), callee(_callee), args(_args), flags(0) {
}

bool CallTemplate::is_rawcall() const {
    return flags & CF_RawCall;
}

void CallTemplate::set_rawcall() {
    flags |= CF_RawCall;
}

CallTemplateRef CallTemplate::from(const ValueRef &callee, const Values &args) {
    return ref(unknown_anchor(), new CallTemplate(callee, args));
}

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

static const Type *get_cmp_type(const Type *T) {
    return bool_op_return_type(T).assert_ok();
}

ICmp::ICmp(ICmpKind _cmp_kind, const TypedValueRef &_value1, const TypedValueRef &_value2)
    : Instruction(VK_ICmp, get_cmp_type(_value1->get_type())), cmp_kind(_cmp_kind), value1(_value1), value2(_value2) {}
ICmpRef ICmp::from(ICmpKind cmp_kind, const TypedValueRef &value1, const TypedValueRef &value2) {
    return ref(unknown_anchor(), new ICmp(cmp_kind, value1, value2));
}

FCmp::FCmp(FCmpKind _cmp_kind, const TypedValueRef &_value1, const TypedValueRef &_value2)
    : Instruction(VK_FCmp, get_cmp_type(_value1->get_type())), cmp_kind(_cmp_kind), value1(_value1), value2(_value2) {}
FCmpRef FCmp::from(FCmpKind cmp_kind, const TypedValueRef &value1, const TypedValueRef &value2) {
    return ref(unknown_anchor(), new FCmp(cmp_kind, value1, value2));
}

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

static const Type *get_unop_type(UnOpKind _op, const Type *T) {
    switch(_op) {
    case UnOpLength: {
        auto ST = storage_type(T).assert_ok();
        if (isa<VectorType>(ST)) {
            return cast<VectorType>(ST)->element_type;
        }
    } break;
    default: break;
    }
    return T;
}

UnOp::UnOp(UnOpKind _op, const TypedValueRef &_value)
    : Instruction(VK_UnOp, get_unop_type(_op, _value->get_type())), op(_op), value(_value) {}
UnOpRef UnOp::from(UnOpKind op, const TypedValueRef &value) {
    return ref(unknown_anchor(), new UnOp(op, value));
}

BinOp::BinOp(BinOpKind _op, const TypedValueRef &_value1, const TypedValueRef &_value2)
    : Instruction(VK_BinOp, _value1->get_type()), op(_op), value1(_value1), value2(_value2) {}
BinOpRef BinOp::from(BinOpKind op, const TypedValueRef &value1, const TypedValueRef &value2) {
    return ref(unknown_anchor(), new BinOp(op, value1, value2));
}

TriOp::TriOp(TriOpKind _op, const TypedValueRef &_value1, const TypedValueRef &_value2, const TypedValueRef &_value3)
    : Instruction(VK_TriOp, _value1->get_type()), op(_op), value1(_value1), value2(_value2), value3(_value3) {}
TriOpRef TriOp::from(TriOpKind op, const TypedValueRef &value1, const TypedValueRef &value2, const TypedValueRef &value3) {
    return ref(unknown_anchor(), new TriOp(op, value1, value2, value3));
}

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

static const ImageType *sampler_image_type(const Type *T) {
    auto ST = storage_type(T).assert_ok();
    if (ST->kind() == TK_SampledImage) {
        auto sit = cast<SampledImageType>(ST);
        ST = storage_type(sit->type).assert_ok();
    }
    return cast<ImageType>(ST);
}

static const Type *sampler_comp_type(const Type *T) {
    return sampler_image_type(T)->type;
}

Sample::Sample(const TypedValueRef &_sampler, const TypedValueRef &_coords, const std::vector<Option> &_options)
    : Instruction(VK_Sample, sampler_comp_type(_sampler->get_type())), sampler(_sampler), coords(_coords), options(_options) {}
SampleRef Sample::from(const TypedValueRef &sampler, const TypedValueRef &coords, const std::vector<Option> &options) {
    return ref(unknown_anchor(), new Sample(sampler, coords, options));
}

static const Type *sampler_comp_size_type(const Type *T) {
    auto it = sampler_image_type(T);
    int comps = 0;
    switch(it->dim.value()) {
    case SYM_SPIRV_Dim1D:
    case SYM_SPIRV_DimBuffer:
        comps = 1;
        break;
    case SYM_SPIRV_Dim2D:
    case SYM_SPIRV_DimCube:
    case SYM_SPIRV_DimRect:
    case SYM_SPIRV_DimSubpassData:
        comps = 2;
        break;
    case SYM_SPIRV_Dim3D:
        comps = 3;
        break;
    default: assert(false); break;
    }
    if (it->arrayed) {
        comps++;
    }
    const Type *retT = TYPE_I32;
    if (comps != 1) {
        return vector_type(TYPE_I32, comps).assert_ok();
    } else {
        return retT;
    }
}

ImageQuerySize::ImageQuerySize(const TypedValueRef &_sampler, const TypedValueRef &_lod)
    : Instruction(VK_ImageQuerySize, sampler_comp_size_type(_sampler->get_type())), sampler(_sampler), lod(_lod) {}
bool ImageQuerySize::has_lod() const {
    return lod;
}
ImageQuerySizeRef ImageQuerySize::from(const TypedValueRef &sampler) {
    return ref(unknown_anchor(), new ImageQuerySize(sampler, TypedValueRef()));
}
ImageQuerySizeRef ImageQuerySize::from(const TypedValueRef &sampler, const TypedValueRef &lod) {
    return ref(unknown_anchor(), new ImageQuerySize(sampler, lod));
}

ImageQueryLod::ImageQueryLod(const TypedValueRef &_sampler, const TypedValueRef &_coords)
    : Instruction(VK_ImageQueryLod, vector_type(TYPE_F32, 2).assert_ok()), sampler(_sampler), coords(_coords) {}
ImageQueryLodRef ImageQueryLod::from(const TypedValueRef &sampler, const TypedValueRef &coords) {
    return ref(unknown_anchor(), new ImageQueryLod(sampler, coords));
}

ImageQueryLevels::ImageQueryLevels(const TypedValueRef &_sampler)
    : Instruction(VK_ImageQueryLevels, TYPE_I32), sampler(_sampler) {}
ImageQueryLevelsRef ImageQueryLevels::from(const TypedValueRef &sampler) {
    return ref(unknown_anchor(), new ImageQueryLevels(sampler));
}

ImageQuerySamples::ImageQuerySamples(const TypedValueRef &_sampler)
    : Instruction(VK_ImageQuerySamples, TYPE_I32), sampler(_sampler) {}
ImageQuerySamplesRef ImageQuerySamples::from(const TypedValueRef &sampler) {
    return ref(unknown_anchor(), new ImageQuerySamples(sampler));
}

ImageRead::ImageRead(const TypedValueRef &_image, const TypedValueRef &_coords)
    : Instruction(VK_ImageRead, sampler_comp_type(_image->get_type())), image(_image), coords(_coords) {}
ImageReadRef ImageRead::from(const TypedValueRef &image, const TypedValueRef &coords) {
    return ref(unknown_anchor(), new ImageRead(image, coords));
}

ImageWrite::ImageWrite(const TypedValueRef &_image, const TypedValueRef &_coords, const TypedValueRef &_texel)
    : Instruction(VK_ImageWrite, empty_arguments_type()), image(_image), coords(_coords), texel(_texel) {}
ImageWriteRef ImageWrite::from(const TypedValueRef &image, const TypedValueRef &coords, const TypedValueRef &texel) {
    return ref(unknown_anchor(), new ImageWrite(image, coords, texel));
}

ExecutionMode::ExecutionMode(Symbol _mode, int v0, int v1, int v2)
    : Instruction(VK_ExecutionMode, empty_arguments_type()), mode(_mode) {
    values[0] = v0;
    values[1] = v1;
    values[2] = v2;
}
ExecutionModeRef ExecutionMode::from(Symbol mode) {
    return ref(unknown_anchor(), new ExecutionMode(mode,-1,-1,-1));
}
ExecutionModeRef ExecutionMode::from(Symbol mode, int v0) {
    return ref(unknown_anchor(), new ExecutionMode(mode,v0,-1,-1));
}
ExecutionModeRef ExecutionMode::from(Symbol mode, int v0, int v1) {
    return ref(unknown_anchor(), new ExecutionMode(mode,v0,v1,-1));
}
ExecutionModeRef ExecutionMode::from(Symbol mode, int v0, int v1, int v2) {
    return ref(unknown_anchor(), new ExecutionMode(mode,v0,v1,v2));
}

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

Annotate::Annotate(const TypedValues &_values)
    : Instruction(VK_Annotate, empty_arguments_type()), values(_values) {}
AnnotateRef Annotate::from(const TypedValues &values) {
    return ref(unknown_anchor(), new Annotate(values));
}

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

Select::Select(const TypedValueRef &_cond,
    const TypedValueRef &_value1, const TypedValueRef &_value2)
    : Instruction(VK_Select, _value2->get_type()), cond(_cond), value1(_value1), value2(_value2)
{}

SelectRef Select::from(const TypedValueRef &cond,
    const TypedValueRef &value1, const TypedValueRef &value2) {
    return ref(unknown_anchor(), new Select(cond, value1, value2));
}

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

static const Type *get_element_pointer_type(const Type *T, const TypedValues &indices) {
    T = storage_type(T).assert_ok();
    assert(isa<PointerType>(T));
    auto pi = cast<PointerType>(T);
    T = pi->element_type;
    int count = indices.size();
    for (int i = 1; i < count; ++i) {
        const Type *ST = storage_type(T).assert_ok();
        switch(ST->kind()) {
        case TK_Array:
        case TK_Matrix: {
            auto ai = cast<ArrayLikeType>(ST);
            T = ai->element_type;
        } break;
        case TK_Vector: {
            auto vi = cast<VectorType>(ST);
            T = vi->element_type;
        } break;
        case TK_Tuple: {
            auto ti = cast<TupleType>(ST);
            assert(indices[i].isa<ConstInt>());
            auto idx = indices[i].cast<ConstInt>()->msw();
            T = type_key(ti->type_at_index(idx).assert_ok())._1;
        } break;
        default: {
            assert(false);
        } break;
        }
    }
    return pointer_type(T, pi->flags, pi->storage_class);
}

GetElementPtr::GetElementPtr(const TypedValueRef &_value, const TypedValues &_indices)
    : Instruction(VK_GetElementPtr, get_element_pointer_type(_value->get_type(), _indices)),
    value(_value), indices(_indices) {}

GetElementPtrRef GetElementPtr::from(const TypedValueRef &value, const TypedValues &indices) {
    return ref(unknown_anchor(), new GetElementPtr(value, indices));
}

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

static const Type *value_type_at_index(const Type *T, int index) {
    T = storage_type(T).assert_ok();
    switch(T->kind()) {
    case TK_Pointer: return cast<PointerType>(T)->element_type;
    case TK_Array:
    case TK_Matrix:
        return cast<ArrayLikeType>(T)->element_type;
    case TK_Vector: return cast<VectorType>(T)->element_type;
    case TK_Tuple: return type_key(cast<TupleType>(T)->type_at_index(index).assert_ok())._1;
    default: {
        assert(false);
        return nullptr;
    } break;
    }
}

ExtractValue::ExtractValue(const TypedValueRef &_value, uint32_t _index)
    : Instruction(VK_ExtractValue,
        value_type_at_index(_value->get_type(), _index)),
    value(_value), index(_index) {}

TypedValueRef ExtractValue::from(const TypedValueRef &value, uint32_t index) {
    if (value.isa<ConstAggregate>()
        && is_plain(value->get_type())) {
        return get_field(value.cast<ConstAggregate>(), index);
    } else {
        return ref(unknown_anchor(), new ExtractValue(value, index));
    }
}

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

InsertValue::InsertValue(const TypedValueRef &_value, const TypedValueRef &_element, uint32_t _index)
    : Instruction(VK_InsertValue, _value->get_type()),
        value(_value), element(_element), index(_index) {}

TypedValueRef InsertValue::from(const TypedValueRef &value, const TypedValueRef &element, uint32_t index) {
    if (value.isa<ConstAggregate>()
        && element.isa<Const>()
        && is_plain(value->get_type())
        && is_plain(element->get_type())) {
        return set_field(value.cast<ConstAggregate>(), element.cast<Const>(), index);
    } else {
        return ref(unknown_anchor(), new InsertValue(value, element, index));
    }
}

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

ExtractElement::ExtractElement(const TypedValueRef &_value, const TypedValueRef &_index)
    : Instruction(VK_ExtractElement, value_type_at_index(_value->get_type(), 0)), value(_value), index(_index) {}
TypedValueRef ExtractElement::from(const TypedValueRef &value, const TypedValueRef &index) {
    if (value.isa<ConstAggregate>()
        && index.isa<ConstInt>()
        && is_plain(value->get_type())) {
        return get_field(value.cast<ConstAggregate>(), index.cast<ConstInt>()->msw());
    } else {
        return ref(unknown_anchor(), new ExtractElement(value, index));
    }
}

InsertElement::InsertElement(const TypedValueRef &_value, const TypedValueRef &_element, const TypedValueRef &_index)
    : Instruction(VK_InsertElement, _value->get_type()), value(_value), element(_element), index(_index) {}
TypedValueRef InsertElement::from(const TypedValueRef &value, const TypedValueRef &element, const TypedValueRef &index) {
    if (value.isa<ConstAggregate>()
        && element.isa<Const>()
        && index.isa<ConstInt>()
        && is_plain(value->get_type())
        && is_plain(element->get_type())) {
        return set_field(value.cast<ConstAggregate>(), element.cast<Const>(), index.cast<ConstInt>()->msw());
    } else {
        return ref(unknown_anchor(), new InsertElement(value, element, index));
    }
}

ShuffleVector::ShuffleVector(const TypedValueRef &_v1, const TypedValueRef &_v2, const std::vector<uint32_t> &_mask)
    : Instruction(VK_ShuffleVector, vector_type(value_type_at_index(_v1->get_type(), 0), _mask.size()).assert_ok()),
    v1(_v1), v2(_v2), mask(_mask) {}
ShuffleVectorRef ShuffleVector::from(const TypedValueRef &v1, const TypedValueRef &v2, const std::vector<uint32_t> &mask) {
    return ref(unknown_anchor(), new ShuffleVector(v1, v2, mask));
}

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

Alloca::Alloca(const Type *T, const TypedValueRef &_count)
    : Instruction(VK_Alloca, local_pointer_type(T)), type(T), count(_count) {}
AllocaRef Alloca::from(const Type *T) {
    return ref(unknown_anchor(), new Alloca(T, TypedValueRef()));
}
AllocaRef Alloca::from(const Type *T, const TypedValueRef &count) {
    return ref(unknown_anchor(), new Alloca(T, count));
}
bool Alloca::is_array() const {
    return count;
}

Malloc::Malloc(const Type *T, const TypedValueRef &_count)
    : Instruction(VK_Malloc, native_pointer_type(T)), type(T), count(_count) {}
MallocRef Malloc::from(const Type *T) {
    return ref(unknown_anchor(), new Malloc(T, TypedValueRef()));
}
MallocRef Malloc::from(const Type *T, const TypedValueRef &count) {
    return ref(unknown_anchor(), new Malloc(T, count));
}
bool Malloc::is_array() const {
    return count;
}

Free::Free(const TypedValueRef &_value)
    : Instruction(VK_Free, empty_arguments_type()), value(_value) {}
FreeRef Free::from(const TypedValueRef &value) {
    return ref(unknown_anchor(), new Free(value));
}

Load::Load(const TypedValueRef &_value, bool _is_volatile)
    : Instruction(VK_Load, value_type_at_index(_value->get_type(),0)), value(_value), is_volatile(_is_volatile) {}
LoadRef Load::from(const TypedValueRef &value, bool is_volatile) {
    return ref(unknown_anchor(), new Load(value, is_volatile));
}

Store::Store(const TypedValueRef &_value, const TypedValueRef &_target, bool _is_volatile)
    : Instruction(VK_Store, empty_arguments_type()), value(_value), target(_target), is_volatile(_is_volatile) {}
StoreRef Store::from(const TypedValueRef &value, const TypedValueRef &target, bool is_volatile) {
    return ref(unknown_anchor(), new Store(value, target, is_volatile));
}

AtomicRMW::AtomicRMW(AtomicRMWOpKind _op, const TypedValueRef &_target, const TypedValueRef &_value)
    : Instruction(VK_AtomicRMW, value_type_at_index(_target->get_type(),0)), op(_op), target(_target), value(_value) {}
AtomicRMWRef AtomicRMW::from(AtomicRMWOpKind op, const TypedValueRef &target, const TypedValueRef &value) {
    return ref(unknown_anchor(), new AtomicRMW(op, target, value));
}

CmpXchg::CmpXchg(const TypedValueRef &_target, const TypedValueRef &_cmp, const TypedValueRef &_value)
    : Instruction(VK_CmpXchg,
        arguments_type({value_type_at_index(_target->get_type(),0), TYPE_Bool})),
        target(_target), cmp(_cmp), value(_value) {}
CmpXchgRef CmpXchg::from(const TypedValueRef &target, const TypedValueRef &cmp, const TypedValueRef &value) {
    return ref(unknown_anchor(), new CmpXchg(target, cmp, value));
}

Barrier::Barrier(BarrierKind _kind)
    : Instruction(VK_Barrier, empty_arguments_type()), kind(_kind) {}
BarrierRef Barrier::from(BarrierKind kind) {
    return ref(unknown_anchor(), new Barrier(kind));
}

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

CastRef PtrToRef::from(const TypedValueRef &value) {
    return Cast::from(CastPtrToRef, value, ptr_to_ref(value->get_type()).assert_ok());
}

CastRef RefToPtr::from(const TypedValueRef &value) {
    return Cast::from(CastRefToPtr, value, ref_to_ptr(value->get_type()).assert_ok());
}

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

Call::Call(const Type *type, const TypedValueRef &_callee, const TypedValues &_args)
    : Instruction(VK_Call, type), callee(_callee), args(_args),
        except(ExceptionRef()) {
}

CallRef Call::from(const Type *type, const TypedValueRef &callee, const TypedValues &args) {
    validate_instruction(callee);
    validate_instructions(args);
    return ref(unknown_anchor(), new Call(type, callee, args));
}

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

Cast::Cast(CastKind _op, const TypedValueRef &_value, const Type *_type)
    : Instruction(VK_Cast, _type), op(_op), value(_value) {}

CastRef Cast::from(CastKind op, const TypedValueRef &value, const Type *type) {
    return ref(unknown_anchor(), new Cast(op, value,  type));
}

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

LoopLabel::LoopLabel(const TypedValues &_init, const LoopLabelArgumentsRef &_args)
    : Instruction(VK_LoopLabel, TYPE_NoReturn), init(_init), args(_args) {
    assert(args);
    args->loop = ref(unknown_anchor(), this);
}

LoopLabelRef LoopLabel::from(const TypedValues &init, const LoopLabelArgumentsRef &args) {
    return ref(unknown_anchor(), new LoopLabel(init, args));
}

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

Loop::Loop(const ValueRef &_init, const ValueRef &_value)
    : UntypedValue(VK_Loop), init(_init), value(_value) {
    args = ref(unknown_anchor(), LoopArguments::from(ref(unknown_anchor(), this)));
}

LoopRef Loop::from(const ValueRef &init, const ValueRef &value) {
    return ref(unknown_anchor(), new Loop(init, value));
}

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

Label::Label(LabelKind _kind, Symbol _name)
    : Instruction(VK_Label, empty_arguments_type()), name(_name), label_kind(_kind) {}

LabelRef Label::from(LabelKind kind, Symbol name) {
    return ref(unknown_anchor(), new Label(kind, name));
}

void Label::change_type(const Type *type) {
    assert(type);
    _type = type;
}

const char *get_label_kind_name(LabelKind kind) {
    switch(kind) {
    #define T(NAME, BNAME) \
        case NAME: return BNAME;
    SCOPES_LABEL_KIND()
    #undef T
    default: return "???";
    }
}

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

LabelTemplate::LabelTemplate(LabelKind _kind, Symbol _name, const ValueRef &_value)
    : UntypedValue(VK_LabelTemplate), name(_name), value(_value), label_kind(_kind) {}

LabelTemplateRef LabelTemplate::from(LabelKind kind, Symbol name, const ValueRef &value) {
    return ref(unknown_anchor(), new LabelTemplate(kind, name, value));
}

LabelTemplateRef LabelTemplate::try_from(const ValueRef &value) {
    return ref(unknown_anchor(), new LabelTemplate(LK_Try, KW_Try, value));
}
LabelTemplateRef LabelTemplate::except_from(const ValueRef &value) {
    return ref(unknown_anchor(), new LabelTemplate(LK_Except, KW_Except, value));
}

LabelTemplateRef LabelTemplate::except_all_from(const ValueRef &value) {
    return ref(unknown_anchor(), new LabelTemplate(LK_ExceptAll, KW_Else, value));
}

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

bool Pure::classof(const Value *T) {
    switch(T->kind()) {
#define T(NAME, BNAME, CLASS) \
    case NAME:
SCOPES_PURE_VALUE_KIND()
#undef T
        return true;
    default: return false;
    }
}

Pure::Pure(ValueKind _kind, const Type *type)
    : TypedValue(_kind, type) {
}

bool Pure::key_equal(const Pure *other) const {
    if (kind() != other->kind())
        return false;
    switch(kind()) {
    #define T(KIND, NAME, CLASS) \
        case KIND: \
            return cast<CLASS>(this)->key_equal(cast<CLASS>(other));
    SCOPES_PURE_VALUE_KIND()
    #undef T
    default: assert(false); return false;
    }
}

std::size_t Pure::hash() const {
    switch(kind()) {
    #define T(KIND, NAME, CLASS) \
        case KIND: \
            return hash2(std::hash<int>{}(kind()), cast<CLASS>(this)->hash());
            //return cast<CLASS>(this)->hash();
    SCOPES_PURE_VALUE_KIND()
    #undef T
    default: assert(false); return 0;
    }
}

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

bool Const::classof(const Value *T) {
    switch(T->kind()) {
#define T(NAME, BNAME, CLASS) \
    case NAME:
SCOPES_CONST_VALUE_KIND()
#undef T
        return true;
    default: return false;
    }
}

Const::Const(ValueKind _kind, const Type *type)
    : Pure(_kind, type) {
}

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

static ConstSet<ConstInt> constints;

ConstInt::ConstInt(const Type *type, const std::vector<uint64_t> &_value)
    : Const(VK_ConstInt, type), words(_value) {
    assert(!_value.empty());
}

bool ConstInt::key_equal(const ConstInt *other) const {
    if (get_type() != other->get_type())
        return false;
    if (words.size() != other->words.size())
        return false;
    for (size_t i = 0; i < words.size(); ++i) {
        if (words[i] != other->words[i])
            return false;
    }
    return true;
}

std::size_t ConstInt::hash() const {
    auto h = std::hash<const Type *>{}(get_type());
    for (size_t i = 0; i < words.size(); ++i) {
        h = hash2(h, std::hash<uint64_t>{}(words[i]));
    }
    return h;
}

uint64_t ConstInt::value() const {
    return words[0];
}

uint64_t ConstInt::msw() const {
    return words.back();
}

ConstIntRef ConstInt::from(const Type *type, std::vector<uint64_t> values) {
    auto T = cast<IntegerType>(storage_type(type).assert_ok());
    // truncate value
    auto w = T->width;
    auto issigned = T->issigned;
    auto numwords = (w + 63) / 64;
    if (values.size() > numwords) {
        // truncate
        values.resize(numwords);
    } else {
        // pad
        uint64_t val = (!issigned || (int64_t(values.back()) >= 0ll))?0ull:-1ull;
        while (values.size() < numwords) {
            values.push_back(val);
        }
    }
    assert(values.size() == numwords);
    if (w % 64 != 0) {
        w = w % 64;
        // mask tail
        auto &&value = values[numwords - 1];
        if (issigned) {
            int64_t intval = value;
            int shift = 64 - w;
            intval = (intval << shift) >> shift;
            value = intval;
        } else {
            value = value & ~(-1ull << w);
        }
    }
    return constints.from(type, values);
}

ConstIntRef ConstInt::from(const Type *type, uint64_t value) {
    std::vector<uint64_t> values = { value };
    return from(type, values);
}

ConstIntRef ConstInt::symbol_from(Symbol value) {
    return from(TYPE_Symbol, value.value());
}

ConstIntRef ConstInt::builtin_from(Builtin value) {
    return from(TYPE_Builtin, value.value());
}

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

static ConstSet<ConstReal> constreals;

ConstReal::ConstReal(const Type *type, double _value)
    : Const(VK_ConstReal, type), value(_value) {}

bool ConstReal::key_equal(const ConstReal *other) const {
    return get_type() == other->get_type()
        && value == other->value;
}

std::size_t ConstReal::hash() const {
    return hash2(std::hash<const Type *>{}(get_type()), std::hash<double>{}(value));
}

ConstRealRef ConstReal::from(const Type *type, double value) {
    return constreals.from(type, value);
}

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

static ConstSet<ConstAggregate> constaggs;

ConstAggregate::ConstAggregate(const Type *type, const ConstantPtrs &_fields)
    : Const(VK_ConstAggregate, type), values(_fields) {
    uint64_t h = std::hash<const Type *>{}(get_type());
    for (int i = 0; i < values.size(); ++i) {
        h = hash2(h, values[i]->hash());
    }
    _hash = h;
}

bool ConstAggregate::key_equal(const ConstAggregate *other) const {
    if (get_type() != other->get_type())
        return false;
    for (int i = 0; i < values.size(); ++i) {
        auto a = values[i];
        auto b = other->values[i];
        if (a != b)
            return false;
    }
    return true;
}

std::size_t ConstAggregate::hash() const {
    return _hash;
}

ConstAggregateRef ConstAggregate::from(const Type *type, const ConstantPtrs &fields) {
    return constaggs.from(type, fields);
}

ConstAggregateRef ConstAggregate::none_from() {
    return from(TYPE_Nothing, {});
}

ConstAggregateRef ConstAggregate::ast_from(const ValueRef &node) {
    auto ptr = ConstPointer::from(TYPE__Value, node.unref()).unref();
    return from(TYPE_ValueRef, { ptr, ConstPointer::anchor_from(node.anchor()).unref() });
}

ConstRef get_field(const ConstAggregateRef &value, uint32_t i) {
    auto VT = value_type_at_index(value->get_type(), i);
    return PureCast::from(VT, ref(value.anchor(), value->values[i])).cast<Const>();
}

ConstRef set_field(const ConstAggregateRef &value, const ConstRef &element, uint32_t i) {
    ConstantPtrs values = value->values;
    assert (i < values.size());
    values[i] = element.unref();
    return ConstAggregate::from(value->get_type(), values);
}

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

static ConstSet<ConstPointer> constptrs;

ConstPointer::ConstPointer(const Type *type, const void *_pointer)
    : Const(VK_ConstPointer, type), value(_pointer) {}

bool ConstPointer::key_equal(const ConstPointer *other) const {
    if (get_type() != other->get_type())
        return false;
    return value == other->value;
}

std::size_t ConstPointer::hash() const {
    return hash2(std::hash<const Type *>{}(get_type()), std::hash<const void *>{}(value));
}

ConstPointerRef ConstPointer::from(const Type *type, const void *pointer) {
    return constptrs.from(type, pointer);
}

ConstPointerRef ConstPointer::type_from(const Type *type) {
    return from(TYPE_Type, type);
}

ConstPointerRef ConstPointer::closure_from(const Closure *closure) {
    return from(TYPE_Closure, closure);
}

ConstPointerRef ConstPointer::string_from(const String *str) {
    return from(TYPE_String, str);
}

ConstPointerRef ConstPointer::list_from(const List *list) {
    return from(TYPE_List, list);
}

ConstPointerRef ConstPointer::scope_from(const Scope *scope) {
    return from(TYPE_Scope, scope);
}

ConstPointerRef ConstPointer::anchor_from(const Anchor *anchor) {
    return from(TYPE_Anchor, anchor);
}

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

Repeat::Repeat(const LoopLabelRef &_loop, const TypedValues &values)
    : Terminator(VK_Repeat, values), loop(_loop) {
}

RepeatRef Repeat::from(const LoopLabelRef &loop, const TypedValues &values) {
    return ref(unknown_anchor(), new Repeat(loop, values));
}

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

Return::Return(const TypedValues &values)
    : Terminator(VK_Return, values) {
}

ReturnRef Return::from(const TypedValues &values) {
    return ref(unknown_anchor(), new Return(values));
}

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

Merge::Merge(const LabelRef &_label, const TypedValues &values)
    : Terminator(VK_Merge, values), label(_label) {
}

MergeRef Merge::from(const LabelRef &label, const TypedValues &values) {
    return ref(unknown_anchor(), new Merge(label, values));
}

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

MergeTemplate::MergeTemplate(const LabelTemplateRef &_label, const ValueRef &_value)
    : UntypedValue(VK_MergeTemplate), label(_label), value(_value) {}

MergeTemplateRef MergeTemplate::from(const LabelTemplateRef &label, const ValueRef &value) {
    return ref(unknown_anchor(), new MergeTemplate(label, value));
}

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

Raise::Raise(const TypedValues &values)
    : Terminator(VK_Raise, values) {
}

RaiseRef Raise::from(const TypedValues &values) {
    return ref(unknown_anchor(), new Raise(values));
}

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

Unreachable::Unreachable()
    : Terminator(VK_Unreachable, {}) {}
UnreachableRef Unreachable::from() {
    return ref(unknown_anchor(), new Unreachable());
}

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

Discard::Discard()
    : Terminator(VK_Discard, {}) {}
DiscardRef Discard::from() {
    return ref(unknown_anchor(), new Discard());
}

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

Quote::Quote(const ValueRef &_value)
    : UntypedValue(VK_Quote), value(_value) {
}

QuoteRef Quote::from(const ValueRef &value) {
    return ref(unknown_anchor(), new Quote(value));
}

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

Unquote::Unquote(const ValueRef &_value)
    : UntypedValue(VK_Unquote), value(_value) {
}

UnquoteRef Unquote::from(const ValueRef &value) {
    return ref(unknown_anchor(), new Unquote(value));
}

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

CompileStage::CompileStage(const Anchor *_anchor, const List *_next, const Scope *_env)
    : UntypedValue(VK_CompileStage), anchor(_anchor), next(_next), env(_env) {
}

CompileStageRef CompileStage::from(const Anchor *anchor, const List *next, const Scope *env) {
    return ref(unknown_anchor(), new CompileStage(anchor, next, env));
}

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

ValueKind Value::kind() const { return _kind; }

Value::Value(ValueKind kind)
    : _kind(kind) {
}

bool Value::is_accessible() const {
    switch(kind()) {
    case VK_ArgumentList: {
        auto al = cast<ArgumentList>(this);
        for (auto val : al->values) {
            assert(val);
            if (!val->is_accessible())
                return false;
        }
        return true;
    } break;
    default: break;
    }
    return isa<Pure>(this);
}

int Value::get_depth() const {
    if (isa<Instruction>(this)) {
        auto instr = cast<Instruction>(this);
        const Block *block = instr->block;
        assert(block);
        return block->depth;
    } else if (isa<LoopLabelArguments>(this)) {
        auto lla = cast<LoopLabelArguments>(this);
        assert(lla->loop);
        return lla->loop->body.depth;
    } else {
        return 0;
    }
}

#define T(NAME, BNAME, CLASS) \
    bool CLASS::classof(const Value *T) { \
        return T->kind() == NAME; \
    }
SCOPES_VALUE_KIND()
#undef T

const Anchor *get_best_anchor(const ValueRef &value) {
    const Anchor *def_anchor = unknown_anchor();
#define T(CLASS) \
    if (value.isa<CLASS>()) { \
        def_anchor = value.cast<CLASS>()->def_anchor(); \
    } else
    SCOPES_DEFINED_VALUES()
#undef T
    {};
    if (!def_anchor->is_boring()) {
        return def_anchor;
    }
    return value.anchor();
}

void set_best_anchor(const ValueRef &value, const Anchor *anchor) {
#define T(CLASS) \
    if (value.isa<CLASS>()) { \
        auto val = value.cast<CLASS>(); \
        val->set_def_anchor(anchor); \
    } else
    SCOPES_DEFINED_VALUES()
#undef T
    {};
}

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

UntypedValue::UntypedValue(ValueKind _kind)
    : Value(_kind)
{}

bool UntypedValue::classof(const Value *T) {
    switch(T->kind()) {
#define T(NAME, BNAME, CLASS) \
    case NAME:
SCOPES_UNTYPED_VALUE_KIND()
#undef T
        return true;
    default: return false;
    }
}

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

bool TypedValue::classof(const Value *T) {
    switch(T->kind()) {
#define T(NAME, BNAME, CLASS) \
    case NAME: return true;
SCOPES_TYPED_VALUE_KIND()
#undef T
    default: return false;
    }
}

TypedValue::TypedValue(ValueKind _kind, const Type *type)
    : Value(_kind), _type(type) {
    assert(_type);
}

void TypedValue::hack_change_value(const Type *T) {
    assert(T);
    if (_type != T) {
        assert(!Const::classof(this)); // must not retype constants
        _type = T;
    }
}

const Type *TypedValue::get_type() const {
    if (!_type) {
        StyledStream ss;
        ss << get_value_class_name(kind()) << std::endl;
    }
    assert(_type);
    return _type;
}

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

Instruction::Instruction(ValueKind _kind, const Type *type)
    : TypedValue(_kind, type), name(SYM_Unnamed), block(nullptr) {
}

bool Instruction::classof(const Value *T) {
    switch(T->kind()) {
#define T(NAME, BNAME, CLASS) \
    case NAME:
SCOPES_INSTRUCTION_VALUE_KIND()
#undef T
        return true;
    default: return false;
    }
}

void validate_instruction(const TypedValueRef &value) {
#if 0
    assert(value);
    if (value.isa<Instruction>()) {
        auto instr = value.cast<Instruction>();
        assert(instr->block);
    }
#endif
}

void validate_instructions(const TypedValues &values) {
#if 0
    for (auto value : values) {
        validate_instruction(value);
    }
#endif
}

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

bool Terminator::classof(const Value *T) {
    switch(T->kind()) {
#define T(NAME, BNAME, CLASS) \
    case NAME:
SCOPES_TERMINATOR_VALUE_KIND()
#undef T
        return true;
    default: return false;
    }
}

Terminator::Terminator(ValueKind _kind, const TypedValues &_values)
    : Instruction(_kind, TYPE_NoReturn), values(_values)
{
    validate_instructions(_values);
}

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

namespace ClosureSet {

struct Hash {
    std::size_t operator()(const Closure *k) const {
        return k->hash();
    }
};

struct Equal {
    std::size_t operator()(const Closure *self, const Closure *other) const {
        return self->key_equal(other);
    }
};

} // namespace ClosureSet

static std::unordered_set<Closure *, ClosureSet::Hash, ClosureSet::Equal> closures;

Closure::Closure(const TemplateRef &_func, const FunctionRef &_frame) :
    func(_func), frame(_frame) {}

bool Closure::key_equal(const Closure *other) const {
    return (func == other->func) && (frame == other->frame);
}

std::size_t Closure::hash() const {
    return hash2(
        std::hash<Template *>{}(func.unref()),
        std::hash<Function *>{}(frame.unref()));
}

Closure *Closure::from(const TemplateRef &func, const FunctionRef &frame) {
    Closure cl(func, frame);
    auto it = closures.find(&cl);
    if (it != closures.end()) {
        return *it;
    }
    Closure *result = new Closure(func, frame);
    closures.insert(result);
    return result;
}

StyledStream &Closure::stream(StyledStream &ost) const {
    ost << Style_Comment << "<" << Style_None;
    if (frame)
        ost << Style_Symbol << frame->name.name()->data << "λ" << (void *)frame.unref() << Style_None;
    ost << Style_Comment << "::" << Style_None
        << Style_Symbol << func->name.name()->data << "λ" << (void *)func.unref() << Style_None
        << Style_Comment << ">" << Style_None;
    return ost;
}

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

StyledStream& operator<<(StyledStream& ss, const Closure *closure) {
    closure->stream(ss);
    return ss;
}

StyledStream& operator<<(StyledStream& ost, Value *node) {
    ost << const_cast<const Value *>(node);
    return ost;
}

StyledStream& operator<<(StyledStream& ost, const Value *node) {
    ost << Style_Keyword << get_value_class_name(node->kind()) << Style_None;
    ost << "$";
    stream_address(ost, node);
    if (isa<TypedValue>(node)) {
        ost << Style_Operator << ":" << Style_None << cast<TypedValue>(node)->get_type();
    }
    return ost;
}

StyledStream& operator<<(StyledStream& ost, TypedValue *node) {
    ost << (Value *)node;
    return ost;
}

StyledStream& operator<<(StyledStream& ost, const TypedValue *node) {
    ost << (const Value *)node;
    return ost;
}

StyledStream& operator<<(StyledStream& ost, const ValueIndex &arg) {
    ost << arg.value;
    if (get_argument_count(arg.value->get_type()) != 1) {
        ost << Style_Operator << "@" << Style_None << arg.index;
        ost << Style_Operator << ":" << Style_None << arg.get_type();
    }
    return ost;
}

StyledStream& operator<<(StyledStream& ost, ValueIndex &arg) {
    ost << const_cast<const ValueIndex &>(arg);
    return ost;
}

} // namespace scopes