summaryrefslogtreecommitdiffstats
path: root/source/slang/syntax.h
blob: b4d7e146ef24d63005f13e723958e584a59c8779 (plain)
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
#ifndef RASTER_RENDERER_SYNTAX_H
#define RASTER_RENDERER_SYNTAX_H

#include "../core/basic.h"
#include "Lexer.h"
#include "Profile.h"

#include "../../Slang.h"

#include <assert.h>

namespace Slang
{
    class SyntaxVisitor;
    class FunctionSyntaxNode;

    class SyntaxNodeBase : public RefObject
    {
    public:
        CodePosition Position;
    };




    //
    // Other modifiers may have more elaborate data, and so
    // are represented as heap-allocated objects, in a linked
    // list.
    //
    class Modifier : public SyntaxNodeBase
    {
    public:
        // Next modifier in linked list of modifiers on same piece of syntax
        RefPtr<Modifier> next;

        // The token that was used to name this modifier.
        Token nameToken;
    };

#define SIMPLE_MODIFIER(NAME) \
    class NAME##Modifier : public Modifier {}

    SIMPLE_MODIFIER(Uniform);
    SIMPLE_MODIFIER(In);
    SIMPLE_MODIFIER(Out);
    SIMPLE_MODIFIER(Const);
    SIMPLE_MODIFIER(Instance);
    SIMPLE_MODIFIER(Builtin);
    SIMPLE_MODIFIER(Inline);
    SIMPLE_MODIFIER(Public);
    SIMPLE_MODIFIER(Require);
    SIMPLE_MODIFIER(Param);
    SIMPLE_MODIFIER(Extern);
    SIMPLE_MODIFIER(Input);
    SIMPLE_MODIFIER(Transparent);
    SIMPLE_MODIFIER(FromStdLib);
    SIMPLE_MODIFIER(Prefix);
    SIMPLE_MODIFIER(Postfix);
    SIMPLE_MODIFIER(Exported);

#undef SIMPLE_MODIFIER

    enum class IntrinsicOp
    {
        Unknown = 0,
#define INTRINSIC(NAME) NAME,
#include "intrinsic-defs.h"
    };

    IntrinsicOp findIntrinsicOp(char const* name);

    // Base class for modifiers that mark something as "intrinsic"
    // and thus lacking a direct implementation in the language.
    class IntrinsicModifierBase : public Modifier
    {
    };

    // A modifier that marks something as one of a small set of
    // truly intrinsic operations that the compiler knows about
    // directly.
    class IntrinsicOpModifier : public IntrinsicModifierBase
    {
    public:
        // token that names the intrinsic op
        Token opToken;

        // The opcode for the intrinsic operation
        IntrinsicOp op = IntrinsicOp::Unknown;
    };

    // A modifier that marks something as an intrinsic function,
    // for some subset of targets.
    class TargetIntrinsicModifier : public IntrinsicModifierBase
    {
    public:
        // Token that names the target that the operation
        // is an intrisic for.
        Token targetToken;

        // A custom definition for the operation
        Token definitionToken;
    };



    class InOutModifier : public OutModifier {};

    // This is a special sentinel modifier that gets added
    // to the list when we have multiple variable declarations
    // all sharing the same modifiers:
    //
    //     static uniform int a : FOO, *b : register(x0);
    //
    // In this case both `a` and `b` share the syntax
    // for part of their modifier list, but then have
    // their own modifiers as well:
    //
    //     a: SemanticModifier("FOO") --> SharedModifiers --> StaticModifier --> UniformModifier
    //                                 /
    //     b: RegisterModifier("x0")  /
    //
    class SharedModifiers : public Modifier {};

    // A GLSL `layout` modifier
    //
    // We use a distinct modifier for each key that
    // appears within the `layout(...)` construct,
    // and each key might have an optional value token.
    //
    // TODO: We probably want a notion of  "modifier groups"
    // so that we can recover good source location info
    // for modifiers that were part of the same vs.
    // different constructs.
    class GLSLLayoutModifier : public Modifier
    {
    public:
        // THe token used to introduce the modifier is stored
        // as the `nameToken` field.

        // TODO: may want to accept a full expression here
        Token valToken;
    };

    // We divide GLSL `layout` modifiers into those we have parsed
    // (in the sense of having some notion of their semantics), and
    // those we have not.
    class GLSLParsedLayoutModifier      : public GLSLLayoutModifier {};
    class GLSLUnparsedLayoutModifier    : public GLSLLayoutModifier {};

    // Specific cases for known GLSL `layout` modifiers that we need to work with
    class GLSLConstantIDLayoutModifier  : public GLSLParsedLayoutModifier {};
    class GLSLBindingLayoutModifier     : public GLSLParsedLayoutModifier {};
    class GLSLSetLayoutModifier         : public GLSLParsedLayoutModifier {};
    class GLSLLocationLayoutModifier    : public GLSLParsedLayoutModifier {};

    // A catch-all for single-keyword modifiers
    class SimpleModifier : public Modifier {};

    // Some GLSL-specific modifiers
    class GLSLBufferModifier    : public SimpleModifier {};
    class GLSLWriteOnlyModifier : public SimpleModifier {};
    class GLSLReadOnlyModifier  : public SimpleModifier {};
    class GLSLPatchModifier     : public SimpleModifier {};

    // Indicates that this is a variable declaration that corresponds to
    // a parameter block declaration in the source program.
    class ImplicitParameterBlockVariableModifier    : public Modifier {};

    // Indicates that this is a type that corresponds to the element
    // type of a parameter block declaration in the source program.
    class ImplicitParameterBlockElementTypeModifier : public Modifier {};

    // An HLSL semantic
    class HLSLSemantic : public Modifier
    {
    public:
        Token name;
    };


    // An HLSL semantic that affects layout
    class HLSLLayoutSemantic : public HLSLSemantic
    {
    public:
        Token registerName;
        Token componentMask;
    };

    // An HLSL `register` semantic
    class HLSLRegisterSemantic : public HLSLLayoutSemantic
    {
    };

    // TODO(tfoley): `packoffset`
    class HLSLPackOffsetSemantic : public HLSLLayoutSemantic
    {
    };

    // An HLSL semantic that just associated a declaration with a semantic name
    class HLSLSimpleSemantic : public HLSLSemantic
    {
    };

    // GLSL

    // Directives that came in via the preprocessor, but
    // that we need to keep around for later steps
    class GLSLPreprocessorDirective : public Modifier
    {
    };

    // A GLSL `#version` directive
    class GLSLVersionDirective : public GLSLPreprocessorDirective
    {
    public:
        // Token giving the version number to use
        Token versionNumberToken;

        // Optional token giving the sub-profile to be used
        Token glslProfileToken;
    };

    // A GLSL `#extension` directive
    class GLSLExtensionDirective : public GLSLPreprocessorDirective
    {
    public:
        // Token giving the version number to use
        Token extensionNameToken;

        // Optional token giving the sub-profile to be used
        Token dispositionToken;
    };

    class ParameterBlockReflectionName : public Modifier
    {
    public:
        Token nameToken;
    };

    // Helper class for iterating over a list of heap-allocated modifiers
    struct ModifierList
    {
        struct Iterator
        {
            Modifier* current;

            Modifier* operator*()
            {
                return current;
            }

            void operator++()
            {
                current = current->next.Ptr();
            }

            bool operator!=(Iterator other)
            {
                return current != other.current;
            };

            Iterator()
                : current(nullptr)
            {}

            Iterator(Modifier* modifier)
                : current(modifier)
            {}
        };

        ModifierList()
            : modifiers(nullptr)
        {}

        ModifierList(Modifier* modifiers)
            : modifiers(modifiers)
        {}

        Iterator begin() { return Iterator(modifiers); }
        Iterator end() { return Iterator(nullptr); }

        Modifier* modifiers;
    };

    // Helper class for iterating over heap-allocated modifiers
    // of a specific type.
    template<typename T>
    struct FilteredModifierList
    {
        struct Iterator
        {
            Modifier* current;

            T* operator*()
            {
                return (T*)current;
            }

            void operator++()
            {
                current = Adjust(current->next.Ptr());
            }

            bool operator!=(Iterator other)
            {
                return current != other.current;
            };

            Iterator()
                : current(nullptr)
            {}

            Iterator(Modifier* modifier)
                : current(modifier)
            {}
        };

        FilteredModifierList()
            : modifiers(nullptr)
        {}

        FilteredModifierList(Modifier* modifiers)
            : modifiers(Adjust(modifiers))
        {}

        Iterator begin() { return Iterator(modifiers); }
        Iterator end() { return Iterator(nullptr); }

        static Modifier* Adjust(Modifier* modifier)
        {
            Modifier* m = modifier;
            for (;;)
            {
                if (!m) return m;
                if (dynamic_cast<T*>(m)) return m;
                m = m->next.Ptr();
            }
        }

        Modifier* modifiers;
    };

    // A set of modifiers attached to a syntax node
    struct Modifiers
    {
        // The first modifier in the linked list of heap-allocated modifiers
        RefPtr<Modifier> first;

        template<typename T>
        FilteredModifierList<T> getModifiersOfType() { return FilteredModifierList<T>(first.Ptr()); }

        // Find the first modifier of a given type, or return `nullptr` if none is found.
        template<typename T>
        T* findModifier()
        {
            return *getModifiersOfType<T>().begin();
        }

        template<typename T>
        bool hasModifier() { return findModifier<T>() != nullptr; }

        FilteredModifierList<Modifier>::Iterator begin() { return FilteredModifierList<Modifier>::Iterator(first.Ptr()); }
        FilteredModifierList<Modifier>::Iterator end() { return FilteredModifierList<Modifier>::Iterator(nullptr); }
    };


    enum class BaseType
    {
        // Note(tfoley): These are ordered in terms of promotion rank, so be vareful when messing with this

        Void = 0,
        Bool,
        Int,
        UInt,
        UInt64,
        Float,
        Double,
#if 0
        Texture2D = 48,
        TextureCube = 49,
        Texture2DArray = 50,
        Texture2DShadow = 51,
        TextureCubeShadow = 52,
        Texture2DArrayShadow = 53,
        Texture3D = 54,
        SamplerState = 4096, SamplerComparisonState = 4097,
        Error = 16384,
#endif
    };

    class Decl;
    class StructSyntaxNode;
    class BasicExpressionType;
    class ArrayExpressionType;
    class TypeDefDecl;
    class DeclRefType;
    class NamedExpressionType;
    class TypeType;
    class GenericDeclRefType; 
    class VectorExpressionType;
    class MatrixExpressionType;
    class ArithmeticExpressionType;
    class GenericDecl;
    class Substitutions;
    class TextureType;
    class SamplerStateType;

    // A compile-time constant value (usually a type)
    class Val : public RefObject
    {
    public:
        // construct a new value by applying a set of parameter
        // substitutions to this one
        RefPtr<Val> Substitute(Substitutions* subst);

        // Lower-level interface for substition. Like the basic
        // `Substitute` above, but also takes a by-reference
        // integer parameter that should be incremented when
        // returning a modified value (this can help the caller
        // decide whether they need to do anything).
        virtual RefPtr<Val> SubstituteImpl(Substitutions* subst, int* ioDiff);

        virtual bool EqualsVal(Val* val) = 0;
        virtual String ToString() = 0;
        virtual int GetHashCode() = 0;
        bool operator == (const Val & v)
        {
            return EqualsVal(const_cast<Val*>(&v));
        }
    };

    // A compile-time integer (may not have a specific concrete value)
    class IntVal : public Val
    {
    };

    // Try to extract a simple integer value from an `IntVal`.
    // This fill assert-fail if the object doesn't represent a literal value.
    IntegerLiteralValue GetIntVal(RefPtr<IntVal> val);

    // Trivial case of a value that is just a constant integer
    class ConstantIntVal : public IntVal
    {
    public:
        IntegerLiteralValue value;

        ConstantIntVal(IntegerLiteralValue value)
            : value(value)
        {}

        virtual bool EqualsVal(Val* val) override;
        virtual String ToString() override;
        virtual int GetHashCode() override;
    };

    // TODO(tfoley): classes for more general compile-time integers,
    // including references to template parameters

    // A type, representing a classifier for some term in the AST.
    //
    // Types can include "sugar" in that they may refer to a
    // `typedef` which gives them a good name when printed as
    // part of diagnostic messages.
    //
    // In order to operation on types, though, we often want
    // to look past any sugar, and operate on an underlying
    // "canonical" type. The reprsentation caches a pointer to
    // a canonical type on every type, so we can easily
    // operate on the raw representation when needed.
    class ExpressionType : public Val
    {
    public:
        static RefPtr<ExpressionType> Error;
        static RefPtr<ExpressionType> initializerListType;
        static RefPtr<ExpressionType> Overloaded;

        static Dictionary<int, RefPtr<ExpressionType>> sBuiltinTypes;
        static Dictionary<String, Decl*> sMagicDecls;

        // Note: just exists to make sure we can clean up
        // canonical types we create along the way
        static List<RefPtr<ExpressionType>> sCanonicalTypes;



        static ExpressionType* GetBool();
        static ExpressionType* GetFloat();
        static ExpressionType* getDoubleType();
        static ExpressionType* GetInt();
        static ExpressionType* GetUInt();
        static ExpressionType* GetVoid();
        static ExpressionType* getInitializerListType();
        static ExpressionType* GetError();

    public:
        virtual String ToString() = 0;

        bool Equals(ExpressionType * type);
        bool Equals(RefPtr<ExpressionType> type);

        bool IsVectorType() { return As<VectorExpressionType>() != nullptr; }
        bool IsArray() { return As<ArrayExpressionType>() != nullptr; }

        template<typename T>
        T* As()
        {
            return dynamic_cast<T*>(GetCanonicalType());
        }

        // Convenience/legacy wrappers for `As<>`
        ArithmeticExpressionType * AsArithmeticType() { return As<ArithmeticExpressionType>(); }
        BasicExpressionType * AsBasicType() { return As<BasicExpressionType>(); }
        VectorExpressionType * AsVectorType() { return As<VectorExpressionType>(); }
        MatrixExpressionType * AsMatrixType() { return As<MatrixExpressionType>(); }
        ArrayExpressionType * AsArrayType() { return As<ArrayExpressionType>(); }

        DeclRefType* AsDeclRefType() { return As<DeclRefType>(); }

        NamedExpressionType* AsNamedType();

        bool IsTextureOrSampler();
        bool IsTexture() { return As<TextureType>() != nullptr; }
        bool IsSampler() { return As<SamplerStateType>() != nullptr; }
        bool IsStruct();
        bool IsClass();
        static void Init();
        static void Finalize();
        ExpressionType* GetCanonicalType();

        virtual RefPtr<Val> SubstituteImpl(Substitutions* subst, int* ioDiff) override;

        virtual bool EqualsVal(Val* val) override;
    protected:
        virtual bool EqualsImpl(ExpressionType * type) = 0;

        virtual ExpressionType* CreateCanonicalType() = 0;
        ExpressionType* canonicalType = nullptr;
    };

    // A substitution represents a binding of certain
    // type-level variables to concrete argument values
    class Substitutions : public RefObject
    {
    public:
        // The generic declaration that defines the
        // parametesr we are binding to arguments
        GenericDecl*	genericDecl;

        // The actual values of the arguments
        List<RefPtr<Val>> args;

        // Any further substitutions, relating to outer generic declarations
        RefPtr<Substitutions> outer;

        // Apply a set of substitutions to the bindings in this substitution
        RefPtr<Substitutions> SubstituteImpl(Substitutions* subst, int* ioDiff);

        // Check if these are equivalent substitutiosn to another set
        bool Equals(Substitutions* subst);
        bool operator == (const Substitutions & subst)
        {
            return Equals(const_cast<Substitutions*>(&subst));
        }
        int GetHashCode() const
        {
            int rs = 0;
            for (auto && v : args)
            {
                rs ^= v->GetHashCode();
                rs *= 16777619;
            }
            return rs;
        }
    };

    class SyntaxNode : public SyntaxNodeBase
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) = 0;
    };

    class ContainerDecl;
    class SpecializeModifier;

    // Represents how much checking has been applied to a declaration.
    enum class DeclCheckState : uint8_t
    {
        // The declaration has been parsed, but not checked
        Unchecked,

        // We are in the process of checking the declaration "header"
        // (those parts of the declaration needed in order to
        // reference it)
        CheckingHeader,

        // We are done checking the declaration header.
        CheckedHeader,

        // We have checked the declaration fully.
        Checked,
    };

    // A syntax node which can have modifiers appled
    class ModifiableSyntaxNode : public SyntaxNode
    {
    public:
        Modifiers modifiers;

        template<typename T>
        FilteredModifierList<T> GetModifiersOfType() { return FilteredModifierList<T>(modifiers.first.Ptr()); }

        // Find the first modifier of a given type, or return `nullptr` if none is found.
        template<typename T>
        T* FindModifier()
        {
            return *GetModifiersOfType<T>().begin();
        }

        template<typename T>
        bool HasModifier() { return FindModifier<T>() != nullptr; }
    };

    void addModifier(
        RefPtr<ModifiableSyntaxNode>    syntax,
        RefPtr<Modifier>                modifier);


    // An intermediate type to represent either a single declaration, or a group of declarations
    class DeclBase : public ModifiableSyntaxNode
    {
    public:
    };

    class Decl : public DeclBase
    {
    public:
        ContainerDecl*  ParentDecl = nullptr;

        Token Name;
        String const& getName() { return Name.Content; }
        Token const& getNameToken() { return Name; }


        DeclCheckState checkState = DeclCheckState::Unchecked;

        // The next declaration defined in the same container with the same name
        Decl* nextInContainerWithSameName = nullptr;

        bool IsChecked(DeclCheckState state) { return checkState >= state; }
        void SetCheckState(DeclCheckState state)
        {
            assert(state >= checkState);
            checkState = state;
        }
    };

    struct QualType
    {
        RefPtr<ExpressionType>	type;
        bool					IsLeftValue;

        QualType()
            : IsLeftValue(false)
        {}

        QualType(ExpressionType* type)
            : type(type)
            , IsLeftValue(false)
        {}

        ExpressionType* Ptr() { return type.Ptr(); }

        operator RefPtr<ExpressionType>() { return type; }
        RefPtr<ExpressionType> operator->() { return type; }
    };

    class ExpressionSyntaxNode : public SyntaxNode
    {
    public:
        QualType Type;
        ExpressionSyntaxNode()
        {}
    };




    // A reference to a declaration, which may include
    // substitutions for generic parameters.
    struct DeclRefBase
    {
        typedef Decl DeclType;

        // The underlying declaration
        Decl* decl = nullptr;
        Decl* getDecl() const { return decl; }

        // Optionally, a chain of substititions to perform
        RefPtr<Substitutions> substitutions;

        DeclRefBase()
        {}

        DeclRefBase(Decl* decl, RefPtr<Substitutions> substitutions)
            : decl(decl)
            , substitutions(substitutions)
        {}

        // Apply substitutions to a type or ddeclaration
        RefPtr<ExpressionType> Substitute(RefPtr<ExpressionType> type) const;

        DeclRefBase Substitute(DeclRefBase declRef) const;

        // Apply substitutions to an expression
        RefPtr<ExpressionSyntaxNode> Substitute(RefPtr<ExpressionSyntaxNode> expr) const;

        // Apply substitutions to this declaration reference
        DeclRefBase SubstituteImpl(Substitutions* subst, int* ioDiff);

        // Check if this is an equivalent declaration reference to another
        bool Equals(DeclRefBase const& declRef) const;
        bool operator == (const DeclRefBase& other) const
        {
            return Equals(other);
        }

        // Convenience accessors for common properties of declarations
        String const& GetName() const;
        DeclRefBase GetParent() const;

        int GetHashCode() const;
    };

    template<typename T>
    struct DeclRef : DeclRefBase
    {
        typedef T DeclType;

        DeclRef()
        {}

        DeclRef(T* decl, RefPtr<Substitutions> substitutions)
            : DeclRefBase(decl, substitutions)
        {}

        template <typename U>
        DeclRef(DeclRef<U> const& other,
            typename EnableIf<IsConvertible<T*, U*>::Value, void>::type* = 0)
            : DeclRefBase(other.decl, other.substitutions)
        {
        }

        // "dynamic cast" to a more specific declaration reference type
        template<typename T>
        DeclRef<T> As() const
        {
            DeclRef<T> result;
            result.decl = dynamic_cast<T*>(decl);
            result.substitutions = substitutions;
            return result;
        }

        T* getDecl() const
        {
            return (T*)decl;
        }

        operator T*() const
        {
            return getDecl();
        }

        //
        static DeclRef<T> unsafeInit(DeclRefBase const& declRef)
        {
            return DeclRef<T>((T*) declRef.decl, declRef.substitutions);
        }

        RefPtr<ExpressionType> Substitute(RefPtr<ExpressionType> type) const
        {
            return DeclRefBase::Substitute(type);
        }
        RefPtr<ExpressionSyntaxNode> Substitute(RefPtr<ExpressionSyntaxNode> expr) const
        {
            return DeclRefBase::Substitute(expr);
        }

        // Apply substitutions to a type or ddeclaration
        template<typename U>
        DeclRef<U> Substitute(DeclRef<U> declRef) const
        {
            return DeclRef<U>::unsafeInit(DeclRefBase::Substitute(declRef));
        }

        // Apply substitutions to this declaration reference
        DeclRef<T> SubstituteImpl(Substitutions* subst, int* ioDiff)
        {
            return DeclRef<T>::unsafeInit(DeclRefBase::SubstituteImpl(subst, ioDiff));
        }

        DeclRef<ContainerDecl> GetParent() const
        {
            return DeclRef<ContainerDecl>::unsafeInit(DeclRefBase::GetParent());
        }

    };

    // The type of a reference to an overloaded name
    class OverloadGroupType : public ExpressionType
    {
    public:
        virtual String ToString() override;

    protected:
        virtual bool EqualsImpl(ExpressionType * type) override;
        virtual ExpressionType* CreateCanonicalType() override;
        virtual int GetHashCode() override;
    };

    // The type of an initializer-list expression (before it has
    // been coerced to some other type)
    class InitializerListType : public ExpressionType
    {
    public:
        virtual String ToString() override;

    protected:
        virtual bool EqualsImpl(ExpressionType * type) override;
        virtual ExpressionType* CreateCanonicalType() override;
        virtual int GetHashCode() override;
    };

    // The type of an expression that was erroneous
    class ErrorType : public ExpressionType
    {
    public:
        virtual String ToString() override;

    protected:
        virtual bool EqualsImpl(ExpressionType * type) override;
        virtual ExpressionType* CreateCanonicalType() override;
        virtual int GetHashCode() override;
    };

    // A type that takes the form of a reference to some declaration
    class DeclRefType : public ExpressionType
    {
    public:
        DeclRef<Decl> declRef;

        virtual String ToString() override;
        virtual RefPtr<Val> SubstituteImpl(Substitutions* subst, int* ioDiff) override;

        static DeclRefType* Create(DeclRef<Decl> declRef);

    protected:
        DeclRefType()
        {}
        DeclRefType(DeclRef<Decl> declRef)
            : declRef(declRef)
        {}
        virtual int GetHashCode() override;
        virtual bool EqualsImpl(ExpressionType * type) override;
        virtual ExpressionType* CreateCanonicalType() override;
    };

    // Base class for types that can be used in arithmetic expressions
    class ArithmeticExpressionType : public DeclRefType
    {
    public:
        virtual BasicExpressionType* GetScalarType() = 0;
    };

    class FunctionDeclBase;

    class BasicExpressionType : public ArithmeticExpressionType
    {
    public:
        BaseType BaseType;

        BasicExpressionType()
        {
            BaseType = Slang::BaseType::Int;
        }
        BasicExpressionType(Slang::BaseType baseType)
        {
            BaseType = baseType;
        }
        virtual Slang::String ToString() override;
    protected:
        virtual BasicExpressionType* GetScalarType() override;
        virtual bool EqualsImpl(ExpressionType * type) override;
        virtual ExpressionType* CreateCanonicalType() override;
    };


    class TextureTypeBase : public DeclRefType
    {
    public:
        // The type that results from fetching an element from this texture
        RefPtr<ExpressionType> elementType;

        // Bits representing the kind of texture type we are looking at
        // (e.g., `Texture2DMS` vs. `TextureCubeArray`)
        typedef uint16_t Flavor;
        Flavor flavor;

        enum
        {
            // Mask for the overall "shape" of the texture
            ShapeMask		= SLANG_RESOURCE_BASE_SHAPE_MASK,

            // Flag for whether the shape has "array-ness"
            ArrayFlag		= SLANG_TEXTURE_ARRAY_FLAG,

            // Whether or not the texture stores multiple samples per pixel
            MultisampleFlag	= SLANG_TEXTURE_MULTISAMPLE_FLAG,

            // Whether or not this is a shadow texture
            //
            // TODO(tfoley): is this even meaningful/used?
            // ShadowFlag		= 0x80, 
        };

        enum Shape : uint8_t
        {
            Shape1D			= SLANG_TEXTURE_1D,
            Shape2D			= SLANG_TEXTURE_2D,
            Shape3D			= SLANG_TEXTURE_3D,
            ShapeCube		= SLANG_TEXTURE_CUBE,

            Shape1DArray	= Shape1D | ArrayFlag,
            Shape2DArray	= Shape2D | ArrayFlag,
            // No Shape3DArray
            ShapeCubeArray	= ShapeCube | ArrayFlag,
        };
            

        Shape GetBaseShape() const { return Shape(flavor & ShapeMask); }
        bool isArray() const { return (flavor & ArrayFlag) != 0; }
        bool isMultisample() const { return (flavor & MultisampleFlag) != 0; }
//            bool isShadow() const { return (flavor & ShadowFlag) != 0; }

        SlangResourceShape getShape() const { return flavor & 0xFF; }
        SlangResourceAccess getAccess() const { return (flavor >> 8) & 0xFF; }

        TextureTypeBase(
            Flavor flavor,
            RefPtr<ExpressionType> elementType)
            : elementType(elementType)
            , flavor(flavor)
        {}
    };

    class TextureType : public TextureTypeBase
    {
    public:
        TextureType(
            Flavor flavor,
            RefPtr<ExpressionType> elementType)
            : TextureTypeBase(flavor, elementType)
        {}
    };

    // This is a base type for texture/sampler pairs,
    // as they exist in, e.g., GLSL
    class TextureSamplerType : public TextureTypeBase
    {
    public:
        TextureSamplerType(
            Flavor flavor,
            RefPtr<ExpressionType> elementType)
            : TextureTypeBase(flavor, elementType)
        {}
    };

    // This is a base type for `image*` types, as they exist in GLSL
    class GLSLImageType : public TextureTypeBase
    {
    public:
        GLSLImageType(
            Flavor flavor,
            RefPtr<ExpressionType> elementType)
            : TextureTypeBase(flavor, elementType)
        {}
    };

    class SamplerStateType : public DeclRefType
    {
    public:
        // What flavor of sampler state is this
        enum class Flavor : uint8_t
        {
            SamplerState,
            SamplerComparisonState,
        };
        Flavor flavor;
    };

    // Other cases of generic types known to the compiler
    class BuiltinGenericType : public DeclRefType
    {
    public:
        RefPtr<ExpressionType> elementType;
    };

    // Types that behave like pointers, in that they can be
    // dereferenced (implicitly) to access members defined
    // in the element type.
    class PointerLikeType : public BuiltinGenericType
    {};

    // Generic types used in existing Slang code
    // TODO(tfoley): check that these are actually working right...
    class PatchType : public PointerLikeType {};
    class StorageBufferType : public BuiltinGenericType {};
    class UniformBufferType : public PointerLikeType {};
    class PackedBufferType : public BuiltinGenericType {};

    // HLSL buffer-type resources

    class HLSLBufferType : public BuiltinGenericType {};
    class HLSLRWBufferType : public BuiltinGenericType {};
    class HLSLStructuredBufferType : public BuiltinGenericType {};
    class HLSLRWStructuredBufferType : public BuiltinGenericType {};

    class UntypedBufferResourceType : public DeclRefType {};
    class HLSLByteAddressBufferType : public UntypedBufferResourceType {};
    class HLSLRWByteAddressBufferType : public UntypedBufferResourceType {};

    class HLSLAppendStructuredBufferType : public BuiltinGenericType {};
    class HLSLConsumeStructuredBufferType : public BuiltinGenericType {};

    class HLSLPatchType : public DeclRefType
    {
    public:
        ExpressionType* getElementType();
        IntVal*         getElementCount();
    };

    class HLSLInputPatchType : public HLSLPatchType {};
    class HLSLOutputPatchType : public HLSLPatchType {};

    // HLSL geometry shader output stream types

    class HLSLStreamOutputType : public BuiltinGenericType {};
    class HLSLPointStreamType : public HLSLStreamOutputType {};
    class HLSLLineStreamType : public HLSLStreamOutputType {};
    class HLSLTriangleStreamType : public HLSLStreamOutputType {};

    //
    class GLSLInputAttachmentType : public DeclRefType {};

    // Base class for types used when desugaring parameter block
    // declarations, includeing HLSL `cbuffer` or GLSL `uniform` blocks.
    class ParameterBlockType : public PointerLikeType {};

    class UniformParameterBlockType : public ParameterBlockType {};
    class VaryingParameterBlockType : public ParameterBlockType {};

    // Type for HLSL `cbuffer` declarations, and `ConstantBuffer<T>`
    // ALso used for GLSL `uniform` blocks.
    class ConstantBufferType : public UniformParameterBlockType {};

    // Type for HLSL `tbuffer` declarations, and `TextureBuffer<T>`
    class TextureBufferType : public UniformParameterBlockType {};

    // Type for GLSL `in` and `out` blocks
    class GLSLInputParameterBlockType : public VaryingParameterBlockType {};
    class GLSLOutputParameterBlockType : public VaryingParameterBlockType {};

    // Type for GLLSL `buffer` blocks
    class GLSLShaderStorageBufferType : public UniformParameterBlockType {};

    class ArrayExpressionType : public ExpressionType
    {
    public:
        RefPtr<ExpressionType> BaseType;
        RefPtr<IntVal> ArrayLength;
        virtual Slang::String ToString() override;
    protected:
        virtual bool EqualsImpl(ExpressionType * type) override;
        virtual ExpressionType* CreateCanonicalType() override;
        virtual int GetHashCode() override;
    };

    // The "type" of an expression that resolves to a type.
    // For example, in the expression `float(2)` the sub-expression,
    // `float` would have the type `TypeType(float)`.
    class TypeType : public ExpressionType
    {
    public:
        TypeType(RefPtr<ExpressionType> type)
            : type(type)
        {}

        // The type that this is the type of...
        RefPtr<ExpressionType> type;


        virtual String ToString() override;

    protected:
        virtual bool EqualsImpl(ExpressionType * type) override;
        virtual ExpressionType* CreateCanonicalType() override;
        virtual int GetHashCode() override;
    };

    class GenericDecl;

    // A vector type, e.g., `vector<T,N>`
    class VectorExpressionType : public ArithmeticExpressionType
    {
    public:
#if 0
        VectorExpressionType(
            RefPtr<ExpressionType>	elementType,
            RefPtr<IntVal>			elementCount)
            : elementType(elementType)
            , elementCount(elementCount)
        {}
#endif

        // The type of vector elements.
        // As an invariant, this should be a basic type or an alias.
        RefPtr<ExpressionType>	elementType;

        // The number of elements
        RefPtr<IntVal>			elementCount;

        virtual String ToString() override;

    protected:
        virtual BasicExpressionType* GetScalarType() override;
    };

    // A matrix type, e.g., `matrix<T,R,C>`
    class MatrixExpressionType : public ArithmeticExpressionType
    {
    public:
        // TODO: consider adding these back for convenience,
        // with a way to initialize them on-demand from the
        // real storage (which is in the `DeclRefType`
#if 0
        // The type of vector elements.
        // As an invariant, this should be a basic type or an alias.
        RefPtr<ExpressionType>			elementType;

        // The type of the matrix rows
        RefPtr<VectorExpressionType>	rowType;

        // The number of rows and columns
        RefPtr<IntVal>					rowCount;
        RefPtr<IntVal>					colCount;
#endif
        ExpressionType* getElementType();
        IntVal*         getRowCount();
        IntVal*         getColumnCount();


        virtual String ToString() override;

    protected:
        virtual BasicExpressionType* GetScalarType() override;
    };

    inline BaseType GetVectorBaseType(VectorExpressionType* vecType) {
        return vecType->elementType->AsBasicType()->BaseType;
    }

    inline int GetVectorSize(VectorExpressionType* vecType)
    {
        auto constantVal = vecType->elementCount.As<ConstantIntVal>();
        if (constantVal)
            return (int) constantVal->value;
        // TODO: what to do in this case?
        return 0;
    }

    class ContainerDecl;


    // A group of declarations that should be treated as a unit
    class DeclGroup : public DeclBase
    {
    public:
        List<RefPtr<Decl>> decls;

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    template<typename T>
    struct FilteredMemberList
    {
        typedef RefPtr<Decl> Element;

        FilteredMemberList()
            : mBegin(NULL)
            , mEnd(NULL)
        {}

        explicit FilteredMemberList(
            List<Element> const& list)
            : mBegin(Adjust(list.begin(), list.end()))
            , mEnd(list.end())
        {}

        struct Iterator
        {
            Element* mCursor;
            Element* mEnd;

            bool operator!=(Iterator const& other)
            {
                return mCursor != other.mCursor;
            }

            void operator++()
            {
                mCursor = Adjust(mCursor + 1, mEnd);
            }

            RefPtr<T>& operator*()
            {
                return *(RefPtr<T>*)mCursor;
            }
        };

        Iterator begin()
        {
            Iterator iter = { mBegin, mEnd };
            return iter;
        }

        Iterator end()
        {
            Iterator iter = { mEnd, mEnd };
            return iter;
        }

        static Element* Adjust(Element* cursor, Element* end)
        {
            while (cursor != end)
            {
                if ((*cursor).As<T>())
                    return cursor;
                cursor++;
            }
            return cursor;
        }

        // TODO(tfoley): It is ugly to have these.
        // We should probably fix the call sites instead.
        RefPtr<T>& First() { return *begin(); }
        int Count()
        {
            int count = 0;
            for (auto iter : (*this))
            {
                (void)iter;
                count++;
            }
            return count;
        }

        List<RefPtr<T>> ToArray()
        {
            List<RefPtr<T>> result;
            for (auto element : (*this))
            {
                result.Add(element);
            }
            return result;
        }

        Element* mBegin;
        Element* mEnd;
    };

    struct TransparentMemberInfo
    {
        // The declaration of the transparent member
        Decl*	decl;
    };

    // A "container" decl is a parent to other declarations
    class ContainerDecl : public Decl
    {
    public:
        List<RefPtr<Decl>> Members;

        template<typename T>
        FilteredMemberList<T> getMembersOfType()
        {
            return FilteredMemberList<T>(Members);
        }


        // Dictionary for looking up members by name.
        // This is built on demand before performing lookup.
        Dictionary<String, Decl*> memberDictionary;

        // Whether the `memberDictionary` is valid.
        // Should be set to `false` if any members get added/remoed.
        bool memberDictionaryIsValid = false;

        // A list of transparent members, to be used in lookup
        // Note: this is only valid if `memberDictionaryIsValid` is true
        List<TransparentMemberInfo> transparentMembers;
    };

    template<typename T>
    struct FilteredMemberRefList
    {
        List<RefPtr<Decl>> const&	decls;
        RefPtr<Substitutions>		substitutions;

        FilteredMemberRefList(
            List<RefPtr<Decl>> const&	decls,
            RefPtr<Substitutions>		substitutions)
            : decls(decls)
            , substitutions(substitutions)
        {}

        int Count() const
        {
            int count = 0;
            for (auto d : *this)
                count++;
            return count;
        }

        List<DeclRef<T>> ToArray() const
        {
            List<DeclRef<T>> result;
            for (auto d : *this)
                result.Add(d);
            return result;
        }

        struct Iterator
        {
            FilteredMemberRefList const* list;
            RefPtr<Decl>* ptr;
            RefPtr<Decl>* end;

            Iterator() : list(nullptr), ptr(nullptr) {}
            Iterator(
                FilteredMemberRefList const* list,
                RefPtr<Decl>* ptr,
                RefPtr<Decl>* end)
                : list(list)
                , ptr(ptr)
                , end(end)
            {}

            bool operator!=(Iterator other)
            {
                return ptr != other.ptr;
            }

            void operator++()
            {
                ptr = list->Adjust(ptr + 1, end);
            }

            DeclRef<T> operator*()
            {
                return DeclRef<T>((T*) ptr->Ptr(), list->substitutions);
            }
        };

        Iterator begin() const { return Iterator(this, Adjust(decls.begin(), decls.end()), decls.end()); }
        Iterator end() const { return Iterator(this, decls.end(), decls.end()); }

        RefPtr<Decl>* Adjust(RefPtr<Decl>* ptr, RefPtr<Decl>* end) const
        {
            while (ptr != end)
            {
                DeclRef<Decl> declRef(ptr->Ptr(), substitutions);
                if (declRef.As<T>())
                    return ptr;
                ptr++;
            }
            return end;
        }
    };

    inline FilteredMemberRefList<Decl> getMembers(DeclRef<ContainerDecl> const& declRef)
    {
        return FilteredMemberRefList<Decl>(declRef.getDecl()->Members, declRef.substitutions);
    }

    template<typename T>
    inline FilteredMemberRefList<T> getMembersOfType(DeclRef<ContainerDecl> const& declRef)
    {
        return FilteredMemberRefList<T>(declRef.getDecl()->Members, declRef.substitutions);
    }

    //
    // Type Expressions
    //

    // A "type expression" is a term that we expect to resolve to a type during checking.
    // We store both the original syntax and the resolved type here.
    struct TypeExp
    {
        TypeExp() {}
        TypeExp(TypeExp const& other)
            : exp(other.exp)
            , type(other.type)
        {}
        explicit TypeExp(RefPtr<ExpressionSyntaxNode> exp)
            : exp(exp)
        {}
        TypeExp(RefPtr<ExpressionSyntaxNode> exp, RefPtr<ExpressionType> type)
            : exp(exp)
            , type(type)
        {}

        RefPtr<ExpressionSyntaxNode> exp;
        RefPtr<ExpressionType> type;

        bool Equals(ExpressionType* other) {
            return type->Equals(other);
        }
        bool Equals(RefPtr<ExpressionType> other) {
            return type->Equals(other.Ptr());
        }
        ExpressionType* Ptr() { return type.Ptr(); }
        operator ExpressionType*()
        {
            return type;
        }
        ExpressionType* operator->() { return Ptr(); }

        TypeExp Accept(SyntaxVisitor* visitor);
    };


    //
    // Declarations
    //

    // Base class for all variable-like declarations
    class VarDeclBase : public Decl
    {
    public:
        // Type of the variable
        TypeExp Type;

        ExpressionType* getType() { return Type.type.Ptr(); }

        // Initializer expression (optional)
        RefPtr<ExpressionSyntaxNode> Expr;
    };

    inline RefPtr<ExpressionType> GetType(DeclRef<VarDeclBase> const& declRef)
    {
        return declRef.Substitute(declRef.getDecl()->Type.Ptr());
    }

    inline RefPtr<ExpressionSyntaxNode> getInitExpr(DeclRef<VarDeclBase> const& declRef)
    {
        return declRef.Substitute(declRef.getDecl()->Expr);
    }

    // A field of a `struct` type
    class StructField : public VarDeclBase
    {
    public:
        StructField()
        {}
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // An `AggTypeDeclBase` captures the shared functionality
    // between true aggregate type declarations and extension
    // declarations:
    //
    // - Both can container members (they are `ContainerDecl`s)
    // - Both can have declared bases
    // - Both expose a `this` variable in their body
    //
    class AggTypeDeclBase : public ContainerDecl
    {
    public:
    };

    // An extension to apply to an existing type
    class ExtensionDecl : public AggTypeDeclBase
    {
    public:
        TypeExp targetType;

        // next extension attached to the same nominal type
        ExtensionDecl* nextCandidateExtension = nullptr;


        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };


    inline RefPtr<ExpressionType> GetTargetType(DeclRef<ExtensionDecl> const& declRef)
    {
        return declRef.Substitute(declRef.getDecl()->targetType.Ptr());
    }

    // Declaration of a type that represents some sort of aggregate
    class AggTypeDecl : public AggTypeDeclBase
    {
    public:

        // extensions that might apply to this declaration
        ExtensionDecl* candidateExtensions = nullptr;
        FilteredMemberList<StructField> GetFields()
        {
            return getMembersOfType<StructField>();
        }
        StructField* FindField(String name)
        {
            for (auto field : GetFields())
            {
                if (field->Name.Content == name)
                    return field.Ptr();
            }
            return nullptr;
        }
        int FindFieldIndex(String name)
        {
            int index = 0;
            for (auto field : GetFields())
            {
                if (field->Name.Content == name)
                    return index;
                index++;
            }
            return -1;
        }
    };

    inline ExtensionDecl* GetCandidateExtensions(DeclRef<AggTypeDecl> const& declRef)
    {
        return declRef.getDecl()->candidateExtensions;
    }

    class StructSyntaxNode : public AggTypeDecl
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    inline FilteredMemberRefList<StructField> GetFields(DeclRef<StructSyntaxNode> const& declRef)
    {
        return getMembersOfType<StructField>(declRef);
    }

    class ClassSyntaxNode : public AggTypeDecl
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // An interface which other types can conform to
    class InterfaceDecl : public AggTypeDecl
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // A kind of pseudo-member that represents an explicit
    // or implicit inheritance relationship.
    //
    class InheritanceDecl : public Decl
    {
    public:
        // The type expression as written
        TypeExp base;

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    inline RefPtr<ExpressionType> getBaseType(DeclRef<InheritanceDecl> const& declRef)
    {
        return declRef.Substitute(declRef.getDecl()->base.type);
    }

    // TODO: may eventually need sub-classes for explicit/direct vs. implicit/indirect inheritance


    // A declaration that represents a simple (non-aggregate) type
    class SimpleTypeDecl : public Decl
    {
    };

    // A `typedef` declaration
    class TypeDefDecl : public SimpleTypeDecl
    {
    public:
        TypeExp Type;

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    inline RefPtr<ExpressionType> GetType(DeclRef<TypeDefDecl> const& declRef)
    {
        return declRef.Substitute(declRef.getDecl()->Type.Ptr());
    }

    // A type alias of some kind (e.g., via `typedef`)
    class NamedExpressionType : public ExpressionType
    {
    public:
        NamedExpressionType(DeclRef<TypeDefDecl> declRef)
            : declRef(declRef)
        {}

        DeclRef<TypeDefDecl> declRef;

        virtual String ToString() override;

    protected:
        virtual bool EqualsImpl(ExpressionType * type) override;
        virtual ExpressionType* CreateCanonicalType() override;
        virtual int GetHashCode() override;
    };


    class StatementSyntaxNode : public ModifiableSyntaxNode
    {
    public:
    };

    // A scope for local declarations (e.g., as part of a statement)
    class ScopeDecl : public ContainerDecl
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class ScopeStmt : public StatementSyntaxNode
    {
    public:
        RefPtr<ScopeDecl> scopeDecl;
    };

    class BlockStatementSyntaxNode : public ScopeStmt
    {
    public:
        List<RefPtr<StatementSyntaxNode>> Statements;
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class UnparsedStmt : public StatementSyntaxNode
    {
    public:
        // The tokens that were contained between `{` and `}`
        List<Token> tokens;

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class ParameterSyntaxNode : public VarDeclBase
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // Base class for things that have parameter lists and can thus be applied to arguments ("called")
    class CallableDecl : public ContainerDecl
    {
    public:
        FilteredMemberList<ParameterSyntaxNode> GetParameters()
        {
            return getMembersOfType<ParameterSyntaxNode>();
        }
        TypeExp ReturnType;
    };

    inline RefPtr<ExpressionType> GetResultType(DeclRef<CallableDecl> const& declRef)
    {
        return declRef.Substitute(declRef.getDecl()->ReturnType.type.Ptr());
    }

    inline FilteredMemberRefList<ParameterSyntaxNode> GetParameters(DeclRef<CallableDecl> const& declRef)
    {
        return getMembersOfType<ParameterSyntaxNode>(declRef);
    }

    // Base class for callable things that may also have a body that is evaluated to produce their result
    class FunctionDeclBase : public CallableDecl
    {
    public:
        RefPtr<StatementSyntaxNode> Body;
    };

    // Function types are currently used for references to symbols that name
    // either ordinary functions, or "component functions."
    // We do not directly store a representation of the type, and instead
    // use a reference to the symbol to stand in for its logical type
    class FuncType : public ExpressionType
    {
    public:
        DeclRef<CallableDecl> declRef;

        virtual String ToString() override;
    protected:
        virtual bool EqualsImpl(ExpressionType * type) override;
        virtual ExpressionType* CreateCanonicalType() override;
        virtual int GetHashCode() override;
    };

    // A constructor/initializer to create instances of a type
    class ConstructorDecl : public FunctionDeclBase
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // A subscript operation used to index instances of a type
    class SubscriptDecl : public CallableDecl
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // An "accessor" for a subscript or property
    class AccessorDecl : public FunctionDeclBase
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class GetterDecl : public AccessorDecl
    {
    };

    class SetterDecl : public AccessorDecl
    {
    };

    //

    class FunctionSyntaxNode : public FunctionDeclBase
    {
    public:
        String InternalName;
        bool IsInline() { return HasModifier<InlineModifier>(); }
        bool IsExtern() { return HasModifier<ExternModifier>(); }
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
        FunctionSyntaxNode()
        {
        }
    };

    struct Scope : public RefObject
    {
        // The parent of this scope (where lookup should go if nothing is found locally)
        RefPtr<Scope>           parent;

        // The next sibling of this scope (a peer for lookup)
        RefPtr<Scope>           nextSibling;

        // The container to use for lookup
        //
        // Note(tfoley): This is kept as an unowned pointer
        // so that a scope can't keep parts of the AST alive,
        // but the opposite it allowed.
        ContainerDecl*          containerDecl;
    };

    // Base class for expressions that will reference declarations
    class DeclRefExpr : public ExpressionSyntaxNode
    {
    public:
        // The scope in which to perform lookup
        RefPtr<Scope>   scope;

        // The declaration of the symbol being referenced
        DeclRef<Decl> declRef;

        // The name of the symbol being referenced
        String name;
    };

    class VarExpressionSyntaxNode : public DeclRefExpr
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // Masks to be applied when lookup up declarations
    enum class LookupMask : uint8_t
    {
        Type = 0x1,
        Function = 0x2,
        Value = 0x4,

        All = Type | Function | Value,
    };

    // Represents one item found during lookup
    struct LookupResultItem
    {
        // Sometimes lookup finds an item, but there were additional
        // "hops" taken to reach it. We need to remember these steps
        // so that if/when we consturct a full expression we generate
        // appropriate AST nodes for all the steps.
        //
        // We build up a list of these "breadcrumbs" while doing
        // lookup, and store them alongside each item found.
        class Breadcrumb : public RefObject
        {
        public:
            enum class Kind
            {
                Member, // A member was references
                Deref, // A value with pointer(-like) type was dereferenced
            };

            Kind kind;
            DeclRef<Decl> declRef;
            RefPtr<Breadcrumb> next;

            Breadcrumb(Kind kind, DeclRef<Decl> declRef, RefPtr<Breadcrumb> next)
                : kind(kind)
                , declRef(declRef)
                , next(next)
            {}
        };

        // A properly-specialized reference to the declaration that was found.
        DeclRef<Decl> declRef;

        // Any breadcrumbs needed in order to turn that declaration
        // reference into a well-formed expression.
        //
        // This is unused in the simple case where a declaration
        // is being referenced directly (rather than through
        // transparent members).
        RefPtr<Breadcrumb> breadcrumbs;

        LookupResultItem() = default;
        explicit LookupResultItem(DeclRef<Decl> declRef)
            : declRef(declRef)
        {}
        LookupResultItem(DeclRef<Decl> declRef, RefPtr<Breadcrumb> breadcrumbs)
            : declRef(declRef)
            , breadcrumbs(breadcrumbs)
        {}
    };


    // Result of looking up a name in some lexical/semantic environment.
    // Can be used to enumerate all the declarations matching that name,
    // in the case where the result is overloaded.
    struct LookupResult
    {
        // The one item that was found, in the smple case
        LookupResultItem item;

        // All of the items that were found, in the complex case.
        // Note: if there was no overloading, then this list isn't
        // used at all, to avoid allocation.
        List<LookupResultItem> items;

        // Was at least one result found?
        bool isValid() const { return item.declRef.getDecl() != nullptr; }

        bool isOverloaded() const { return items.Count() > 1; }
    };

    class SemanticsVisitor;

    struct LookupRequest
    {
        SemanticsVisitor*   semantics   = nullptr;

        RefPtr<Scope>       scope       = nullptr;
        RefPtr<Scope>       endScope    = nullptr;

        LookupMask          mask        = LookupMask::All;
    };

    // An expression that references an overloaded set of declarations
    // having the same name.
    class OverloadedExpr : public ExpressionSyntaxNode
    {
    public:
        // Optional: the base expression is this overloaded result
        // arose from a member-reference expression.
        RefPtr<ExpressionSyntaxNode> base;

        // The lookup result that was ambiguous
        LookupResult lookupResult2;

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class ConstantExpressionSyntaxNode : public ExpressionSyntaxNode
    {
    public:
        Token token;

        enum class ConstantType
        {
            Int,
            Bool,
            Float,
            String,
        };
        ConstantType ConstType;
        union
        {
            IntegerLiteralValue         integerValue;
            FloatingPointLiteralValue   floatingPointValue;
        };
        String stringValue;
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    enum class Operator
    {
        Neg, Not, BitNot, PreInc, PreDec, PostInc, PostDec,
        Mul, Div, Mod,
        Add, Sub,
        Lsh, Rsh,
        Eql, Neq, Greater, Less, Geq, Leq,
        BitAnd, BitXor, BitOr,
        And,
        Or,
        Sequence,
        Select,
        Assign = 200, AddAssign, SubAssign, MulAssign, DivAssign, ModAssign,
        LshAssign, RshAssign, OrAssign, AndAssign, XorAssign,
    };
    String GetOperatorFunctionName(Operator op);
    String OperatorToString(Operator op);

    // An initializer list, e.g. `{ 1, 2, 3 }`
    class InitializerListExpr : public ExpressionSyntaxNode
    {
    public:
        List<RefPtr<ExpressionSyntaxNode>> args;

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // A base expression being applied to arguments: covers
    // both ordinary `()` function calls and `<>` generic application
    class AppExprBase : public ExpressionSyntaxNode
    {
    public:
        RefPtr<ExpressionSyntaxNode> FunctionExpr;
        List<RefPtr<ExpressionSyntaxNode>> Arguments;
    };


    class InvokeExpressionSyntaxNode : public AppExprBase
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class OperatorExpressionSyntaxNode : public InvokeExpressionSyntaxNode
    {
    public:
//            Operator Operator;
//            void SetOperator(RefPtr<Scope> scope, Slang::Operator op);
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class InfixExpr   : public OperatorExpressionSyntaxNode {};
    class PrefixExpr  : public OperatorExpressionSyntaxNode {};
    class PostfixExpr : public OperatorExpressionSyntaxNode {};

    class IndexExpressionSyntaxNode : public ExpressionSyntaxNode
    {
    public:
        RefPtr<ExpressionSyntaxNode> BaseExpression;
        RefPtr<ExpressionSyntaxNode> IndexExpression;
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class MemberExpressionSyntaxNode : public DeclRefExpr
    {
    public:
        RefPtr<ExpressionSyntaxNode> BaseExpression;
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class SwizzleExpr : public ExpressionSyntaxNode
    {
    public:
        RefPtr<ExpressionSyntaxNode> base;
        int elementCount;
        int elementIndices[4];

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // A dereference of a pointer or pointer-like type
    class DerefExpr : public ExpressionSyntaxNode
    {
    public:
        RefPtr<ExpressionSyntaxNode> base;

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class TypeCastExpressionSyntaxNode : public ExpressionSyntaxNode
    {
    public:
        TypeExp TargetType;
        RefPtr<ExpressionSyntaxNode> Expression;
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class SelectExpressionSyntaxNode : public OperatorExpressionSyntaxNode
    {
    public:
    };


    class EmptyStatementSyntaxNode : public StatementSyntaxNode
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class DiscardStatementSyntaxNode : public StatementSyntaxNode
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    struct Variable : public VarDeclBase
    {
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class VarDeclrStatementSyntaxNode : public StatementSyntaxNode
    {
    public:
        RefPtr<DeclBase> decl;
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // A "module" of code (essentiately, a single translation unit)
    // that provides a scope for some number of declarations.
    class ProgramSyntaxNode : public ContainerDecl
    {
    public:
        // Access members of specific types
        FilteredMemberList<FunctionSyntaxNode> GetFunctions()
        {
            return getMembersOfType<FunctionSyntaxNode>();
        }

        FilteredMemberList<ClassSyntaxNode> GetClasses()
        {
            return getMembersOfType<ClassSyntaxNode>();
        }
        FilteredMemberList<StructSyntaxNode> GetStructs()
        {
            return getMembersOfType<StructSyntaxNode>();
        }
        FilteredMemberList<TypeDefDecl> GetTypeDefs()
        {
            return getMembersOfType<TypeDefDecl>();
        }

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

            class ImportDecl : public Decl
    {
    public:
        // The name of the module we are trying to import
        Token nameToken;

        // The scope that we want to import into
        RefPtr<Scope> scope;

        // The module that actually got imported
        RefPtr<ProgramSyntaxNode>   importedModuleDecl;

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };


    class IfStatementSyntaxNode : public StatementSyntaxNode
    {
    public:
        RefPtr<ExpressionSyntaxNode> Predicate;
        RefPtr<StatementSyntaxNode> PositiveStatement;
        RefPtr<StatementSyntaxNode> NegativeStatement;
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // A statement that can be escaped with a `break`
    class BreakableStmt : public ScopeStmt
    {};

    class SwitchStmt : public BreakableStmt
    {
    public:
        RefPtr<ExpressionSyntaxNode> condition;
        RefPtr<StatementSyntaxNode> body;

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // A statement that is expected to appear lexically nested inside
    // some other construct, and thus needs to keep track of the
    // outer statement that it is associated with...
    class ChildStmt : public StatementSyntaxNode
    {
    public:
        StatementSyntaxNode* parentStmt = nullptr;
    };

    // a `case` or `default` statement inside a `switch`
    //
    // Note(tfoley): A correct AST for a C-like language would treat
    // these as a labelled statement, and so they would contain a
    // sub-statement. I'm leaving that out for now for simplicity.
    class CaseStmtBase : public ChildStmt
    {
    public:
    };

    // a `case` statement inside a `switch`
    class CaseStmt : public CaseStmtBase
    {
    public:
        RefPtr<ExpressionSyntaxNode> expr;

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // a `default` statement inside a `switch`
    class DefaultStmt : public CaseStmtBase
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // A statement that represents a loop, and can thus be escaped with a `continue`
    class LoopStmt : public BreakableStmt
    {};

    class ForStatementSyntaxNode : public LoopStmt
    {
    public:
        RefPtr<StatementSyntaxNode> InitialStatement;
        RefPtr<ExpressionSyntaxNode> SideEffectExpression, PredicateExpression;
        RefPtr<StatementSyntaxNode> Statement;
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class WhileStatementSyntaxNode : public LoopStmt
    {
    public:
        RefPtr<ExpressionSyntaxNode> Predicate;
        RefPtr<StatementSyntaxNode> Statement;
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class DoWhileStatementSyntaxNode : public LoopStmt
    {
    public:
        RefPtr<StatementSyntaxNode> Statement;
        RefPtr<ExpressionSyntaxNode> Predicate;
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // The case of child statements that do control flow relative
    // to their parent statement.
    class JumpStmt : public ChildStmt
    {
    public:
        StatementSyntaxNode* parentStmt = nullptr;
    };

    class BreakStatementSyntaxNode : public JumpStmt
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class ContinueStatementSyntaxNode : public JumpStmt
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class ReturnStatementSyntaxNode : public StatementSyntaxNode
    {
    public:
        RefPtr<ExpressionSyntaxNode> Expression;
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    class ExpressionStatementSyntaxNode : public StatementSyntaxNode
    {
    public:
        RefPtr<ExpressionSyntaxNode> Expression;
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // Note(tfoley): Moved this further down in the file because it depends on
    // `ExpressionSyntaxNode` and a forward reference just isn't good enough
    // for `RefPtr`.
    //
    class GenericAppExpr : public AppExprBase
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // An expression representing re-use of the syntax for a type in more
    // than once conceptually-distinct declaration
    class SharedTypeExpr : public ExpressionSyntaxNode
    {
    public:
        // The underlying type expression that we want to share
        TypeExp base;

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };


    // A modifier that indicates a built-in base type (e.g., `float`)
    class BuiltinTypeModifier : public Modifier
    {
    public:
        BaseType tag;
    };

    // A modifier that indicates a built-in type that isn't a base type (e.g., `vector`)
    //
    // TODO(tfoley): This deserves a better name than "magic"
    class MagicTypeModifier : public Modifier
    {
    public:
        String name;
        uint32_t tag;
    };

    // Modifiers that affect the storage layout for matrices
    class MatrixLayoutModifier : public Modifier {};

    // Modifiers that specify row- and column-major layout, respectively
    class RowMajorLayoutModifier : public MatrixLayoutModifier {};
    class ColumnMajorLayoutModifier : public MatrixLayoutModifier {};

    // The HLSL flavor of those modifiers
    class HLSLRowMajorLayoutModifier : public RowMajorLayoutModifier {};
    class HLSLColumnMajorLayoutModifier : public ColumnMajorLayoutModifier {};

    // The GLSL flavor of those modifiers
    //
    // Note(tfoley): The GLSL versions of these modifiers are "backwards"
    // in the sense that when a GLSL programmer requests row-major layout,
    // we actually interpret that as requesting column-major. This makes
    // sense because we interpret matrix conventions backwards from how
    // GLSL specifies them.
    class GLSLRowMajorLayoutModifier : public ColumnMajorLayoutModifier {};
    class GLSLColumnMajorLayoutModifier : public RowMajorLayoutModifier {};

    // More HLSL Keyword

    // HLSL `nointerpolation` modifier
    class HLSLNoInterpolationModifier : public Modifier {};

    // HLSL `linear` modifier
    class HLSLLinearModifier : public Modifier {};

    // HLSL `sample` modifier
    class HLSLSampleModifier : public Modifier {};

    // HLSL `centroid` modifier
    class HLSLCentroidModifier : public Modifier {};

    // HLSL `precise` modifier
    class HLSLPreciseModifier : public Modifier {};

    // HLSL `shared` modifier (which is used by the effect system,
    // and shouldn't be confused with `groupshared`)
    class HLSLEffectSharedModifier : public Modifier {};

    // HLSL `groupshared` modifier
    class HLSLGroupSharedModifier : public Modifier {};

    // HLSL `static` modifier (probably doesn't need to be
    // treated as HLSL-specific)
    class HLSLStaticModifier : public Modifier {};

    // HLSL `uniform` modifier (distinct meaning from GLSL
    // use of the keyword)
    class HLSLUniformModifier : public Modifier {};

    // HLSL `volatile` modifier (ignored)
    class HLSLVolatileModifier : public Modifier {};

    // An HLSL `[name(arg0, ...)]` style attribute.
    class HLSLAttribute : public Modifier
    {
    public:
        Token nameToken;
        List<RefPtr<ExpressionSyntaxNode>> args;
    };
        
    // An HLSL `[name(...)]` attribute that hasn't undergone
    // any semantic analysis.
    // After analysis, this might be transformed into a more specific case.
    class HLSLUncheckedAttribute : public HLSLAttribute
    {
    public:
    };

    // An HLSL `[numthreads(x,y,z)]` attribute
    class HLSLNumThreadsAttribute : public HLSLAttribute
    {
    public:
        // The number of threads to use along each axis
        int32_t x;
        int32_t y;
        int32_t z;
    };

    // HLSL modifiers for geometry shader input topology
    class HLSLGeometryShaderInputPrimitiveTypeModifier : public Modifier {};
    class HLSLPointModifier         : public HLSLGeometryShaderInputPrimitiveTypeModifier {};
    class HLSLLineModifier          : public HLSLGeometryShaderInputPrimitiveTypeModifier {};
    class HLSLTriangleModifier      : public HLSLGeometryShaderInputPrimitiveTypeModifier {};
    class HLSLLineAdjModifier       : public HLSLGeometryShaderInputPrimitiveTypeModifier {};
    class HLSLTriangleAdjModifier   : public HLSLGeometryShaderInputPrimitiveTypeModifier {};

    //

    // A generic declaration, parameterized on types/values
    class GenericDecl : public ContainerDecl
    {
    public:
        // The decl that is genericized...
        RefPtr<Decl> inner;

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    inline Decl* GetInner(DeclRef<GenericDecl> const& declRef)
    {
        // TODO: Should really return a `DeclRef<Decl>` for the inner
        // declaration, and not just a raw pointer
        return declRef.getDecl()->inner.Ptr();
    }

    // The "type" of an expression that names a generic declaration.
    class GenericDeclRefType : public ExpressionType
    {
    public:
        GenericDeclRefType(DeclRef<GenericDecl> declRef)
            : declRef(declRef)
        {}

        DeclRef<GenericDecl> declRef;
        DeclRef<GenericDecl> const& GetDeclRef() const { return declRef; }

        virtual String ToString() override;

    protected:
        virtual bool EqualsImpl(ExpressionType * type) override;
        virtual int GetHashCode() override;
        virtual ExpressionType* CreateCanonicalType() override;
    };



    class GenericTypeParamDecl : public SimpleTypeDecl
    {
    public:
        // The bound for the type parameter represents a trait that any
        // type used as this parameter must conform to
//            TypeExp bound;

        // The "initializer" for the parameter represents a default value
        TypeExp initType;

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // A constraint placed as part of a generic declaration
    class GenericTypeConstraintDecl : public Decl
    {
    public:
        // A type constraint like `T : U` is constraining `T` to be "below" `U`
        // on a lattice of types. This may not be a subtyping relationship
        // per se, but it makes sense to use that terminology here, so we
        // think of these fields as the sub-type and sup-ertype, respectively.
        TypeExp sub;
        TypeExp sup;

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    inline RefPtr<ExpressionType> GetSub(DeclRef<GenericTypeConstraintDecl> const& declRef)
    {
        return declRef.Substitute(declRef.getDecl()->sub.Ptr());
    }

    inline RefPtr<ExpressionType> GetSup(DeclRef<GenericTypeConstraintDecl> const& declRef)
    {
        return declRef.Substitute(declRef.getDecl()->sup.Ptr());
    }

    class GenericValueParamDecl : public VarDeclBase
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // The logical "value" of a rererence to a generic value parameter
    class GenericParamIntVal : public IntVal
    {
    public:
        DeclRef<VarDeclBase> declRef;

        GenericParamIntVal(DeclRef<VarDeclBase> declRef)
            : declRef(declRef)
        {}

        virtual bool EqualsVal(Val* val) override;
        virtual String ToString() override;
        virtual int GetHashCode() override;
        virtual RefPtr<Val> SubstituteImpl(Substitutions* subst, int* ioDiff) override;
    };

    // Declaration of a user-defined modifier
    class ModifierDecl : public Decl
    {
    public:
        // The name of the C++ class to instantiate
        // (this is a reference to a class in the compiler source code,
        // and not the user's source code)
        Token classNameToken;

        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    // An empty declaration (which might still have modifiers attached).
    //
    // An empty declaration is uncommon in HLSL, but
    // in GLSL it is often used at the global scope
    // to declare metadata that logically belongs
    // to the entry point, e.g.:
    //
    //     layout(local_size_x = 16) in;
    //
    class EmptyDecl : public Decl
    {
    public:
        virtual RefPtr<SyntaxNode> Accept(SyntaxVisitor * visitor) override;
    };

    //

    class SyntaxVisitor : public RefObject
    {
    protected:
        DiagnosticSink * sink = nullptr;
        DiagnosticSink* getSink() { return sink; }

        SourceLanguage sourceLanguage = SourceLanguage::Unknown;
    public:
        void setSourceLanguage(SourceLanguage language)
        {
            sourceLanguage = language;
        }

        SyntaxVisitor(DiagnosticSink * sink)
            : sink(sink)
        {}
        virtual ~SyntaxVisitor()
        {
        }

        virtual RefPtr<ProgramSyntaxNode> VisitProgram(ProgramSyntaxNode* program)
        {
            for (auto & m : program->Members)
                m = m->Accept(this).As<Decl>();
            return program;
        }

        virtual void visitImportDecl(ImportDecl * decl) = 0;

        virtual RefPtr<FunctionSyntaxNode> VisitFunction(FunctionSyntaxNode* func)
        {
            func->ReturnType = func->ReturnType.Accept(this);
            for (auto & member : func->Members)
                member = member->Accept(this).As<Decl>();
            if (func->Body)
                func->Body = func->Body->Accept(this).As<BlockStatementSyntaxNode>();
            return func;
        }
        virtual RefPtr<ScopeDecl> VisitScopeDecl(ScopeDecl* decl)
        {
            // By default don't visit children, because they will always
            // be encountered in the ordinary flow of the corresponding statement.
            return decl;
        }
        virtual RefPtr<StructSyntaxNode> VisitStruct(StructSyntaxNode * s)
        {
            for (auto & f : s->Members)
                f = f->Accept(this).As<Decl>();
            return s;
        }
        virtual RefPtr<ClassSyntaxNode> VisitClass(ClassSyntaxNode * s)
        {
            for (auto & f : s->Members)
                f = f->Accept(this).As<Decl>();
            return s;
        }
        virtual RefPtr<GenericDecl> VisitGenericDecl(GenericDecl * decl)
        {
            for (auto & m : decl->Members)
                m = m->Accept(this).As<Decl>();
            decl->inner = decl->inner->Accept(this).As<Decl>();
            return decl;
        }
        virtual RefPtr<TypeDefDecl> VisitTypeDefDecl(TypeDefDecl* decl)
        {
            decl->Type = decl->Type.Accept(this);
            return decl;
        }
        virtual RefPtr<StatementSyntaxNode> VisitDiscardStatement(DiscardStatementSyntaxNode * stmt)
        {
            return stmt;
        }
        virtual RefPtr<StructField> VisitStructField(StructField * f)
        {
            f->Type = f->Type.Accept(this);
            return f;
        }
        virtual RefPtr<StatementSyntaxNode> VisitBlockStatement(BlockStatementSyntaxNode* stmt)
        {
            for (auto & s : stmt->Statements)
                s = s->Accept(this).As<StatementSyntaxNode>();
            return stmt;
        }
        virtual RefPtr<StatementSyntaxNode> VisitBreakStatement(BreakStatementSyntaxNode* stmt)
        {
            return stmt;
        }
        virtual RefPtr<StatementSyntaxNode> VisitContinueStatement(ContinueStatementSyntaxNode* stmt)
        {
            return stmt;
        }

        virtual RefPtr<StatementSyntaxNode> VisitDoWhileStatement(DoWhileStatementSyntaxNode* stmt)
        {
            if (stmt->Predicate)
                stmt->Predicate = stmt->Predicate->Accept(this).As<ExpressionSyntaxNode>();
            if (stmt->Statement)
                stmt->Statement = stmt->Statement->Accept(this).As<StatementSyntaxNode>();
            return stmt;
        }
        virtual RefPtr<StatementSyntaxNode> VisitEmptyStatement(EmptyStatementSyntaxNode* stmt)
        {
            return stmt;
        }
        virtual RefPtr<StatementSyntaxNode> VisitForStatement(ForStatementSyntaxNode* stmt)
        {
            if (stmt->InitialStatement)
                stmt->InitialStatement = stmt->InitialStatement->Accept(this).As<StatementSyntaxNode>();
            if (stmt->PredicateExpression)
                stmt->PredicateExpression = stmt->PredicateExpression->Accept(this).As<ExpressionSyntaxNode>();
            if (stmt->SideEffectExpression)
                stmt->SideEffectExpression = stmt->SideEffectExpression->Accept(this).As<ExpressionSyntaxNode>();
            if (stmt->Statement)
                stmt->Statement = stmt->Statement->Accept(this).As<StatementSyntaxNode>();
            return stmt;
        }
        virtual RefPtr<StatementSyntaxNode> VisitIfStatement(IfStatementSyntaxNode* stmt)
        {
            if (stmt->Predicate)
                stmt->Predicate = stmt->Predicate->Accept(this).As<ExpressionSyntaxNode>();
            if (stmt->PositiveStatement)
                stmt->PositiveStatement = stmt->PositiveStatement->Accept(this).As<StatementSyntaxNode>();
            if (stmt->NegativeStatement)
                stmt->NegativeStatement = stmt->NegativeStatement->Accept(this).As<StatementSyntaxNode>();
            return stmt;
        }
        virtual RefPtr<SwitchStmt> VisitSwitchStmt(SwitchStmt* stmt)
        {
            if (stmt->condition)
                stmt->condition = stmt->condition->Accept(this).As<ExpressionSyntaxNode>();
            if (stmt->body)
                stmt->body = stmt->body->Accept(this).As<BlockStatementSyntaxNode>();
            return stmt;
        }
        virtual RefPtr<CaseStmt> VisitCaseStmt(CaseStmt* stmt)
        {
            if (stmt->expr)
                stmt->expr = stmt->expr->Accept(this).As<ExpressionSyntaxNode>();
            return stmt;
        }
        virtual RefPtr<DefaultStmt> VisitDefaultStmt(DefaultStmt* stmt)
        {
            return stmt;
        }
        virtual RefPtr<StatementSyntaxNode> VisitReturnStatement(ReturnStatementSyntaxNode* stmt)
        {
            if (stmt->Expression)
                stmt->Expression = stmt->Expression->Accept(this).As<ExpressionSyntaxNode>();
            return stmt;
        }
        virtual RefPtr<StatementSyntaxNode> VisitVarDeclrStatement(VarDeclrStatementSyntaxNode* stmt)
        {
            stmt->decl = stmt->decl->Accept(this).As<DeclBase>();
            return stmt;
        }
        virtual RefPtr<StatementSyntaxNode> VisitWhileStatement(WhileStatementSyntaxNode* stmt)
        {
            if (stmt->Predicate)
                stmt->Predicate = stmt->Predicate->Accept(this).As<ExpressionSyntaxNode>();
            if (stmt->Statement)
                stmt->Statement = stmt->Statement->Accept(this).As<StatementSyntaxNode>();
            return stmt;
        }
        virtual RefPtr<StatementSyntaxNode> VisitExpressionStatement(ExpressionStatementSyntaxNode* stmt)
        {
            if (stmt->Expression)
                stmt->Expression = stmt->Expression->Accept(this).As<ExpressionSyntaxNode>();
            return stmt;
        }

        virtual RefPtr<ExpressionSyntaxNode> VisitOperatorExpression(OperatorExpressionSyntaxNode* expr)
        {
            for (auto && child : expr->Arguments)
                child->Accept(this);
            return expr;
        }
        virtual RefPtr<ExpressionSyntaxNode> VisitConstantExpression(ConstantExpressionSyntaxNode* expr)
        {
            return expr;
        }
        virtual RefPtr<ExpressionSyntaxNode> VisitIndexExpression(IndexExpressionSyntaxNode* expr)
        {
            if (expr->BaseExpression)
                expr->BaseExpression = expr->BaseExpression->Accept(this).As<ExpressionSyntaxNode>();
            if (expr->IndexExpression)
                expr->IndexExpression = expr->IndexExpression->Accept(this).As<ExpressionSyntaxNode>();
            return expr;
        }
        virtual RefPtr<ExpressionSyntaxNode> VisitMemberExpression(MemberExpressionSyntaxNode * stmt)
        {
            if (stmt->BaseExpression)
                stmt->BaseExpression = stmt->BaseExpression->Accept(this).As<ExpressionSyntaxNode>();
            return stmt;
        }
        virtual RefPtr<ExpressionSyntaxNode> VisitSwizzleExpression(SwizzleExpr * expr)
        {
            if (expr->base)
                expr->base->Accept(this);
            return expr;
        }
        virtual RefPtr<ExpressionSyntaxNode> VisitInvokeExpression(InvokeExpressionSyntaxNode* stmt)
        {
            stmt->FunctionExpr->Accept(this);
            for (auto & arg : stmt->Arguments)
                arg = arg->Accept(this).As<ExpressionSyntaxNode>();
            return stmt;
        }
        virtual RefPtr<ExpressionSyntaxNode> VisitTypeCastExpression(TypeCastExpressionSyntaxNode * stmt)
        {
            if (stmt->Expression)
                stmt->Expression = stmt->Expression->Accept(this).As<ExpressionSyntaxNode>();
            return stmt->Expression;
        }
        virtual RefPtr<ExpressionSyntaxNode> VisitVarExpression(VarExpressionSyntaxNode* expr)
        {
            return expr;
        }

        virtual RefPtr<ParameterSyntaxNode> VisitParameter(ParameterSyntaxNode* param)
        {
            return param;
        }
        virtual RefPtr<ExpressionSyntaxNode> VisitGenericApp(GenericAppExpr* type)
        {
            return type;
        }

        virtual RefPtr<Variable> VisitDeclrVariable(Variable* dclr)
        {
            if (dclr->Expr)
                dclr->Expr = dclr->Expr->Accept(this).As<ExpressionSyntaxNode>();
            return dclr;
        }

        virtual TypeExp VisitTypeExp(TypeExp const& typeExp)
        {
            TypeExp result = typeExp;
            result.exp = typeExp.exp->Accept(this).As<ExpressionSyntaxNode>();
            if (auto typeType = result.exp->Type.type.As<TypeType>())
            {
                result.type = typeType->type;
            }
            return result;
        }

        virtual void VisitExtensionDecl(ExtensionDecl* /*decl*/)
        {}

        virtual void VisitConstructorDecl(ConstructorDecl* /*decl*/)
        {}

        virtual void visitSubscriptDecl(SubscriptDecl* decl) = 0;

        virtual void visitAccessorDecl(AccessorDecl* decl) = 0;

        virtual void visitInterfaceDecl(InterfaceDecl* /*decl*/) = 0;

        virtual void visitInheritanceDecl(InheritanceDecl* /*decl*/) = 0;

        virtual RefPtr<ExpressionSyntaxNode> VisitSharedTypeExpr(SharedTypeExpr* typeExpr)
        {
            return typeExpr;
        }

        virtual void VisitDeclGroup(DeclGroup* declGroup)
        {
            for (auto decl : declGroup->decls)
            {
                decl->Accept(this);
            }
        }

        virtual RefPtr<ExpressionSyntaxNode> visitInitializerListExpr(InitializerListExpr* expr) = 0;
    };

    // Note(tfoley): These logically belong to `ExpressionType`,
    // but order-of-declaration stuff makes that tricky
    //
    // TODO(tfoley): These should really belong to the compilation context!
    //
    void RegisterBuiltinDecl(
        RefPtr<Decl>                decl,
        RefPtr<BuiltinTypeModifier> modifier);
    void RegisterMagicDecl(
        RefPtr<Decl>                decl,
        RefPtr<MagicTypeModifier>   modifier);

    // Look up a magic declaration by its name
    RefPtr<Decl> findMagicDecl(
        String const& name);

    // Create an instance of a syntax class by name
    SyntaxNodeBase* createInstanceOfSyntaxClassByName(
        String const&   name);

} // namespace Slang

#endif