1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
//! Defines the `resymgen` YAML format and its programmatic representation, the [`SymGen`] struct.

pub mod cursor;
pub use cursor::{BlockCursor, SymGenCursor};

use std::any;
use std::borrow::Cow;
use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::fmt::{self, Display, Formatter};
use std::io::{self, Read, Write};
use std::iter;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::slice::SliceIndex;

use regex::{Captures, Regex};
use serde::{Deserialize, Serialize};
use serde_yaml;
use syn::{self, LitStr};

use super::error::{Error, Result, SubregionError};
use super::types::*;

/// Specifies how integers should be formatted during serialization.
#[derive(Clone, Copy)]
pub enum IntFormat {
    Decimal,
    Hexadecimal,
}

/// Information about a [`Block`] to be propagated down to the block's contents.
struct BlockContext {
    version_order: Option<HashMap<String, u64>>,
}

fn option_vec_is_empty<T>(opt: &Option<Vec<T>>) -> bool {
    match opt {
        None => true,
        Some(v) => v.is_empty(),
    }
}

/// A symbol in a `resymgen` symbol table, with some metadata.
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Symbol {
    /// The symbol name.
    pub name: String,
    /// Aliases for the symbol.
    #[serde(skip_serializing_if = "option_vec_is_empty")]
    pub aliases: Option<Vec<String>>,
    /// The starting address of the symbol in memory.
    pub address: MaybeVersionDep<Linkable>,
    /// The length of the symbol in memory (in bytes).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub length: Option<MaybeVersionDep<Uint>>,
    /// A description of the symbol.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

/// Combines possibly version-dependent `addrs` and `opt_len` into a single `MaybeVersionDep`
/// with the data (addr, opt_len). Assumes `addrs` and `opt_len` have the same `Version` key space.
fn zip_addr_len<T>(
    addrs: &MaybeVersionDep<T>,
    opt_len: Option<&MaybeVersionDep<Uint>>,
) -> MaybeVersionDep<(T, Option<Uint>)>
where
    T: Clone,
{
    match addrs {
        MaybeVersionDep::ByVersion(addr) => {
            let version_dep = match opt_len {
                Some(len) => addr
                    .iter()
                    .map(|(v, a)| (v.clone(), (a.clone(), len.get_native(Some(v)).copied())))
                    .collect(),
                None => addr
                    .iter()
                    .map(|(v, a)| (v.clone(), (a.clone(), None)))
                    .collect(),
            };
            MaybeVersionDep::ByVersion(version_dep)
        }
        MaybeVersionDep::Common(addr) => match opt_len {
            Some(lens) => match lens {
                MaybeVersionDep::Common(len) => MaybeVersionDep::Common((addr.clone(), Some(*len))),
                // If we don't have explicit versions for addr but do for length,
                // use the versions from length as a best-effort output.
                MaybeVersionDep::ByVersion(len) => MaybeVersionDep::ByVersion(
                    len.iter()
                        .map(|(v, &l)| (v.clone(), (addr.clone(), Some(l))))
                        .collect(),
                ),
            },
            None => MaybeVersionDep::Common((addr.clone(), None)),
        },
    }
}

impl Symbol {
    /// Initializes the [`Symbol`] with a given `ctx`.
    fn init(&mut self, ctx: &BlockContext) {
        self.address.init(&ctx.version_order);
        if let Some(l) = &mut self.length {
            l.init(&ctx.version_order);
        }
    }
    /// Coerces the [`Symbol`]'s address and length fields to be [`ByVersion`].
    ///
    /// If either field is [`Common`], it will be expanded with the versions in `all_versions`.
    ///
    /// [`ByVersion`]: MaybeVersionDep::ByVersion
    /// [`Common`]: MaybeVersionDep::Common
    pub fn expand_versions(&mut self, all_versions: &[Version]) {
        self.address.expand_versions(all_versions);
        if let Some(len) = &mut self.length {
            len.expand_versions(all_versions);
        }
    }
    /// Gets the extents occupied by the [`Symbol`], possibly by version, represented as
    /// address-length pairs.
    ///
    /// If the optional `all_versions` is provided, the returned extents are guaranteed to be
    /// [`ByVersion`].
    ///
    /// [`ByVersion`]: MaybeVersionDep::ByVersion
    pub fn extents(
        &self,
        all_versions: Option<&[Version]>,
    ) -> MaybeVersionDep<(Linkable, Option<Uint>)> {
        match all_versions {
            // Always realize the address with all versions if possible
            Some(versions) => zip_addr_len(
                &MaybeVersionDep::ByVersion(self.address.by_version(versions)),
                self.length.as_ref(),
            ),
            None => zip_addr_len(&self.address, self.length.as_ref()),
        }
    }
    /// Returns an [`Iterator`] over references to all the [`Symbol`]'s names.
    ///
    /// The [`Symbol`]'s primary name is yielded first, then any aliases.
    pub fn iter_names(&self) -> impl Iterator<Item = &str> {
        iter::once(self.name.deref()).chain(self.iter_aliases())
    }
    /// Returns an [`Iterator`] over references to all the [`Symbol`]'s aliases.
    pub fn iter_aliases(&self) -> impl Iterator<Item = &str> {
        self.aliases
            .as_deref()
            .unwrap_or_default()
            .iter()
            .map(|s| s.deref())
    }
}

impl Sort for Symbol {
    // Note: only applies to the address values. Version keys will always be sorted.
    fn sort(&mut self) {
        if let Some(aliases) = &mut self.aliases {
            aliases.sort();
        }
        self.address.sort();
    }
}

impl PartialOrd for Symbol {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Symbol {
    fn cmp(&self, other: &Self) -> Ordering {
        self.address.cmp(&other.address)
    }
}

/// A concrete realization of a [`Symbol`] for some [`Version`] (which can be [`None`]).
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize)]
pub struct RealizedSymbol<'a> {
    pub name: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aliases: Option<&'a [String]>,
    pub address: Uint,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub length: Option<Uint>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<&'a str>,
}

/// Wraps an [`Iterator`] over [`Symbol`]s to yield a stream of [`RealizedSymbol`]s.
pub struct RealizedSymbolIter<'v, 's, I>
where
    I: Iterator<Item = &'s Symbol>,
{
    version: Option<&'v Version>,
    symbols: I,
    cur: Option<(&'s Symbol, LinkableIter<'s>, Option<&'s Uint>)>,
}

impl<'v, 's, I> Iterator for RealizedSymbolIter<'v, 's, I>
where
    I: Iterator<Item = &'s Symbol>,
{
    type Item = RealizedSymbol<'s>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            while self.cur.is_none() {
                // Try to fill cur
                match self.symbols.next() {
                    Some(symbol) => {
                        if let Some(address) = symbol.address.get(self.version) {
                            // This symbol can be realized; store it as cur
                            self.cur = Some((
                                symbol,
                                address.iter(),
                                symbol.length.as_ref().and_then(|l| l.get(self.version)),
                            ));
                        }
                    }
                    None => return None, // Out of symbols; we are done
                }
            }
            // cur is filled; yield realized symbols
            let (symbol, mut addrs, len) = self.cur.take().expect("self.cur is None?");
            if let Some(&a) = addrs.next() {
                // Still addresses to yield; put cur back for the next yield
                self.cur = Some((symbol, addrs, len));
                return Some(RealizedSymbol {
                    name: &symbol.name,
                    aliases: symbol.aliases.as_deref(),
                    address: a,
                    length: len.copied(),
                    description: symbol.description.as_deref(),
                });
            }
            // cur is depleted; don't put it back and get a new one next loop
        }
    }
}

/// Implementers of [`Realize`] can produce iterators over [`RealizedSymbol`]s
/// for arbitrary [`Version`]s.
pub trait Realize<'s> {
    type Iter: Iterator<Item = &'s Symbol>;

    /// Returns a [`RealizedSymbolIter`] for the given `version`.
    fn realize<'v>(self, version: Option<&'v Version>) -> RealizedSymbolIter<'v, 's, Self::Iter>;
}

impl<'s, I> Realize<'s> for I
where
    I: Iterator<Item = &'s Symbol>,
{
    type Iter = I;

    fn realize<'v>(self, version: Option<&'v Version>) -> RealizedSymbolIter<'v, 's, Self::Iter> {
        RealizedSymbolIter {
            version,
            symbols: self,
            cur: None,
        }
    }
}

/// A list of [`Symbol`]s.
///
/// Implements a similar accessor interface to [`Vec<Symbol>`].
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct SymbolList(Vec<Symbol>);

impl SymbolList {
    /// Initializes the [`SymbolList`] with a given `ctx`.
    fn init(&mut self, ctx: &BlockContext) {
        for symbol in self.0.iter_mut() {
            symbol.init(ctx);
        }
    }
    /// Expands the versions of all the [`Symbol`]s contained within the [`SymbolList`].
    ///
    /// See [`Symbol::expand_versions()`].
    pub fn expand_versions(&mut self, all_versions: &[Version]) {
        for symbol in self.0.iter_mut() {
            symbol.expand_versions(all_versions);
        }
    }

    pub fn get<I>(&self, index: I) -> Option<&<I as SliceIndex<[Symbol]>>::Output>
    where
        I: SliceIndex<[Symbol]>,
    {
        self.0.get(index)
    }
    pub fn get_mut<I>(&mut self, index: I) -> Option<&mut <I as SliceIndex<[Symbol]>>::Output>
    where
        I: SliceIndex<[Symbol]>,
    {
        self.0.get_mut(index)
    }
    /// Returns a mutable reference to a symbol, without doing bounds checking.
    ///
    /// # Safety
    /// Calling this method with an out-of-bounds index is undefined behavior.
    pub unsafe fn get_unchecked_mut<I>(
        &mut self,
        index: I,
    ) -> &mut <I as SliceIndex<[Symbol]>>::Output
    where
        I: SliceIndex<[Symbol]>,
    {
        self.0.get_unchecked_mut(index)
    }
    pub fn iter(&self) -> impl Iterator<Item = &Symbol> {
        self.0.iter()
    }
    pub fn len(&self) -> usize {
        self.0.len()
    }
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
    pub fn push(&mut self, value: Symbol) {
        self.0.push(value)
    }
    pub fn append(&mut self, other: &mut SymbolList) {
        self.0.append(&mut other.0)
    }
}

impl Deref for SymbolList {
    type Target = [Symbol];

    fn deref(&self) -> &Self::Target {
        self.0.deref()
    }
}

impl<const N: usize> From<[Symbol; N]> for SymbolList {
    fn from(arr: [Symbol; N]) -> Self {
        SymbolList(Vec::from(arr))
    }
}

/// A symbol in a [`SymbolList`], potentially augmented by additional addresses for sorting
#[derive(Debug, PartialEq, Eq, Clone)]
struct SortSymbol {
    /// The symbol.
    symbol: Symbol,
    /// Addresses temporarily assigned while the parent SymbolList is being sorted.
    sort_address: Option<VersionDep<Linkable>>,
}

impl SortSymbol {
    fn get_native(&self, native_key: &Version) -> Option<&Linkable> {
        if let Some(addr) = &self.sort_address {
            return addr.get_native(native_key);
        } else if let MaybeVersionDep::ByVersion(addr) = &self.symbol.address {
            return addr.get_native(native_key);
        }
        None
    }
    fn contains_key_native(&self, native_key: &Version) -> bool {
        self.get_native(native_key).is_some()
    }
    fn insert_native(&mut self, native_key: Version, value: Linkable) -> Option<Linkable> {
        if let Some(addr) = &mut self.sort_address {
            addr.insert_native(native_key, value)
        } else if let MaybeVersionDep::ByVersion(addr) = &self.symbol.address {
            let mut addr = addr.clone();
            let old = addr.insert_native(native_key, value);
            self.sort_address = Some(addr);
            old
        } else {
            self.sort_address = Some([(native_key, value)].into());
            None
        }
    }
}

impl PartialOrd for SortSymbol {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for SortSymbol {
    fn cmp(&self, other: &Self) -> Ordering {
        // Use the sort_address field if present, otherwise fall back to the normal address field
        match &self.sort_address {
            Some(self_sort_addr) => match &other.sort_address {
                Some(other_sort_addr) => self_sort_addr.cmp(other_sort_addr),
                None => {
                    MaybeVersionDep::ByVersion(self_sort_addr.clone()).cmp(&other.symbol.address)
                }
            },
            None => match &other.sort_address {
                Some(other_sort_addr) => self
                    .symbol
                    .address
                    .cmp(&MaybeVersionDep::ByVersion(other_sort_addr.clone())),
                None => self.symbol.address.cmp(&other.symbol.address),
            },
        }
    }
}

/// A disjoint set of closed numeric ranges
#[derive(Debug, PartialEq, Eq)]
struct RangeSet(Vec<(Uint, Uint)>);

impl From<Vec<(Uint, Uint)>> for RangeSet {
    fn from(mut ranges: Vec<(Uint, Uint)>) -> Self {
        ranges.retain(|r| r.0 <= r.1);
        if ranges.is_empty() {
            return Self(ranges);
        }
        ranges.sort_unstable();

        let mut rangeset = Vec::with_capacity(ranges.len());
        let mut range_iter = ranges.into_iter();
        // Buffer the current range so that we can coalesce if needed before pushing
        let mut current_range = range_iter.next().unwrap();
        for r in range_iter {
            // Note: can't just check (r.0 - current_range.1) as i64 <= 1 because of integer
            // underflow.
            if r.0 <= current_range.1 || (r.0 - current_range.1 == 1) {
                // The new range's left endpoint overlaps with or borders current_range, so we can
                // combine it with current_range.
                current_range.1 = current_range.1.max(r.1);
            } else {
                rangeset.push(current_range);
                current_range = r;
            }
        }
        rangeset.push(current_range);
        Self(rangeset)
    }
}

impl RangeSet {
    fn contains(&self, val: Uint) -> bool {
        self.0
            .binary_search_by(|r| {
                if let Ordering::Greater = r.0.cmp(&val) {
                    Ordering::Greater
                } else if let Ordering::Less = r.1.cmp(&val) {
                    Ordering::Less
                } else {
                    Ordering::Equal
                }
            })
            .is_ok()
    }
}

impl Sort for SymbolList {
    fn sort(&mut self) {
        // Sort each individual symbol's contents, and gather a sorted list of all versions
        // inferred from the symbol contents
        let mut all_versions = BTreeSet::new();
        for symbol in self.0.iter_mut() {
            symbol.sort();

            for v in symbol.address.versions() {
                all_versions.insert(v);
            }
        }
        let all_versions: Vec<_> = all_versions.into_iter().cloned().collect();

        // Move symbols over to an auxiliary array, since we need to be able to augment the symbol
        // data for sorting purposes
        let mut sort_list: Vec<SortSymbol> = Vec::with_capacity(self.len());
        for symbol in self.0.drain(..) {
            sort_list.push(SortSymbol {
                symbol,
                sort_address: None,
            });
        }

        // Realize Common addresses for sorting purposes, if possible. Comparison between
        // Common/ByVersion variants is not consistent/transitive if the ByVersion variant
        // is missing some versions, and realization prevents such intransitivity.
        for ss in sort_list.iter_mut() {
            if ss.symbol.address.is_common() && !all_versions.is_empty() {
                ss.sort_address = Some(ss.symbol.address.by_version(&all_versions));
            }
        }

        // First pass: naive lexicographic sort.
        sort_list.sort();

        // The following block performs a more sophisticated sorting algorithm for symbols with
        // versioned addresses.
        //
        // # Motivation
        // A naive lexicographic sort has limitations when some symbols are missing addresses
        // for certain versions. For example, say you have a list like this:
        // ```yml
        // - name: a
        //   address:
        //     v1: 0
        //     v2: 1
        // - name: b
        //   address:
        //     v1: 10
        //     v2: 11
        // - name: c
        //   address:
        //     v2: 2
        // ```
        // The naive sort will leave "c" at the end, even though it should really be in between
        // "a" and "b", based on the v2 value.
        //
        // However, it's not always possible to unambiguously order symbols with missing addresses.
        // For example, say you have a list like this:
        // ```yml
        // - name: a
        //   address:
        //     v1: 0
        //     v2: 10
        // - name: b
        //   address:
        //     v1: 1
        //     v2: 2
        // - name: c
        //   address:
        //     v2: 5
        // ```
        // Here, it's not clear whether "c" should come before "a", or after "b". A good sorting
        // algorithm should be able to detect such situations, and leave these symbols at the end
        // in these cases.
        //
        // # Algorithm Overview
        // Sorting happens over multiple passes, one for each version, in order (with the first
        // pass being the naive sort in the previous line). The passes accumulate, and by the last
        // pass, the list will be fully sorted. In each pass (for a version "v"):
        //
        // 1. Of the symbols with addresses for version "v" but not for prior versions, determine
        // which can be unambigously resorted.
        // 2. Of the good symbols in step 1, figure out where to relocate them, and then do so.
        //
        // See subsequent comments for more detail.
        let mut first_unsorted_idx = 0;
        for (pass_idx, vpair) in all_versions.windows(2).enumerate() {
            let vsorted = &vpair[0]; // the previous version, which a pass was already done for
            let v = &vpair[1]; // the currrent version, which this pass is focused on
                               // all previous versions for which a pass was already done for
            let all_vsorted = &all_versions[..=pass_idx];

            // Find the first symbol (that isn't already sorted) whose address set doesn't have
            // vsorted. This is the first unsorted symbol.
            for ss in sort_list.iter().skip(first_unsorted_idx) {
                if ss.contains_key_native(vsorted) {
                    first_unsorted_idx += 1;
                } else {
                    break;
                }
            }
            // Everything is already sorted; nothing to do
            if first_unsorted_idx == sort_list.len() {
                break;
            }

            // Search for sort violations among the vsorted-sorted symbols with v addresses,
            // and fill in v addresses if missing. A sort violation for version v is when the
            // sequence of v addresses (the ordering of which is controlled by addresses from prior
            // versions) is not in sorted order. For example:
            //
            // v2: 1, 2, 3, 4, 5, 3, 6, 4, 8, 12, 11
            //                    ^     ^         ^
            //                     sort violations
            //
            // What we care about is the range of values that are "passed through" when sorting
            // order is violated. In the above example, we pass from 5 -> 3, 6 -> 4, and 12 -> 11.
            // This means that we can't sort any currently unsorted symbol with a v address in the
            // ranges [3, 5], [4, 6], or [11, 12].
            let mut prev_val = Uint::MIN;
            let mut contested_ranges = Vec::new();
            for ss in sort_list.iter_mut().take(first_unsorted_idx) {
                if let Some(cur_val) = ss.get_native(v).map(|val| val.cmp_key()) {
                    if cur_val < prev_val {
                        contested_ranges.push((cur_val, prev_val));
                    }
                    prev_val = cur_val;
                } else {
                    // We need to fill in an artificial v address so the binary search in the
                    // next step works properly. We can just use prev_val to maintain the existing
                    // order.
                    ss.insert_native(v.clone(), prev_val.into());
                }
            }
            let contested_ranges = RangeSet::from(contested_ranges);

            // Go through each of the unsorted symbols (with v addresses but not vsorted addresses)
            // and try to assign fake addresses for all the addresses in all_vsorted, such that the
            // symbols will end up appropriately sorted.
            let (sorted_slice, unsorted_slice) = sort_list.split_at_mut(first_unsorted_idx);
            for ss in unsorted_slice.iter_mut() {
                if let Some(cur_val) = ss.get_native(v).map(|val| val.cmp_key()) {
                    first_unsorted_idx += 1; // this just saves us some work in the next pass

                    if contested_ranges.contains(cur_val) {
                        // This symbol is in a contested range...we can't sort it, so just skip
                        continue;
                    }

                    // Search for the first fully sorted symbol (had a vsorted address) with a
                    // version v address that exceeds that of the current unsorted symbol. This
                    // is the sorted symbol we want to insert the unsorted symbol in front of.
                    let idx =
                        sorted_slice.partition_point(|ss| {
                            ss.get_native(v).expect(
                            "SymbolList::Sort reference symbol does not have reference value?",
                        ).cmp_key() <= cur_val
                        });
                    // If idx == sorted_slice.len(), there's nothing to do; the current unsorted
                    // symbol comes after all the currently sorted symbols and should stay at the
                    // end of the list.
                    if idx < sorted_slice.len() {
                        // # Safety
                        // We just checked that idx < sorted_slice.len()
                        let ref_ss = unsafe { sorted_slice.get_unchecked(idx) };
                        // Copy the values for the all_vsorted version from the matched sorted
                        // symbol to the current unsorted symbol. Since the version v value for
                        // the current unsorted symbol is less than that of the matched sorted
                        // symbol by construction, this ensures that the current unsorted symbol
                        // will end up directly in front of the sorted symbol when we resort the
                        // list.
                        for vother in all_vsorted.iter() {
                            // ref_ss must have a value for v, but not necessarily for the vother's
                            // before it, since it could've been skipped on previous iterations due
                            // to contested ranges.
                            if let Some(vother_val) = ref_ss.get_native(vother) {
                                ss.insert_native(vother.clone(), vother_val.cmp_key().into());
                            }
                        }
                    }
                } else {
                    // This symbol doesn't have a v address. Since sort_list was already
                    // pre-sorted, none of the later symbols will either. This pass is finished.
                    break;
                }
            }

            // Next pass: now that we've added new sort_addresses, redo the lexicographic sort
            // to put the symbols with version v addresses but not vsorted addresses in order
            sort_list.sort();
        }

        // Transfer the fully sorted symbols back from the auxiliary array
        for ss in sort_list.into_iter() {
            self.0.push(ss.symbol);
        }
    }
}

/// A contiguous block of [`Symbol`]s in a `resymgen` symbol table, with some metadata.
///
/// Like its consituent [`Symbol`]s, a [`Block`] contains an address, a length, and a description.
/// Unlike [`Symbol`]s, the `length` field is required. [`Block`]s can also contain a list of all
/// relevant [`Version`]s, which may or may not be used by the [`Symbol`]s it contains.
///
/// Every [`Block`] contains two separate [`SymbolList`]s: one for function symbols, and one for
/// data symbols.
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Block {
    // Metadata
    /// List of [`Version`]s relevant to the block.
    #[serde(skip_serializing_if = "option_vec_is_empty")]
    pub versions: Option<Vec<Version>>,
    /// The starting address of the block in memory.
    pub address: MaybeVersionDep<Uint>,
    /// The length of the block in memory (in bytes).
    pub length: MaybeVersionDep<Uint>,
    /// A description of the block.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    // Symbols
    /// List of subregions.
    #[serde(skip_serializing_if = "option_vec_is_empty")]
    pub subregions: Option<Vec<Subregion>>,
    /// List of function symbols.
    pub functions: SymbolList,
    /// List of data symbols.
    pub data: SymbolList,
}

impl Block {
    /// Gets a [`BlockContext`] associated with the [`Block`].
    fn get_context(&self) -> BlockContext {
        BlockContext {
            version_order: OrdString::get_order_map(self.versions.as_deref()),
        }
    }
    /// Initializes the [`Block`]'s contents using its version list.
    fn init(&mut self) {
        let ctx = self.get_context();

        // Init metadata
        if let Some(vers) = &mut self.versions {
            for v in vers {
                v.init(&ctx.version_order);
            }
        }
        self.address.init(&ctx.version_order);
        self.length.init(&ctx.version_order);

        // Init symbols
        self.functions.init(&ctx);
        self.data.init(&ctx);
    }
    /// Recursively resolves the contents of all [`Subregion`]s in the [`Block`].
    ///
    /// [`Subregion`]s are read from files using `file_opener`, with file paths based on the root
    /// directory specified by `dir_path`.
    pub fn resolve_subregions<P, R, F>(&mut self, dir_path: P, file_opener: F) -> Result<()>
    where
        P: AsRef<Path>,
        R: Read,
        F: Fn(&Path) -> io::Result<R> + Copy,
    {
        if let Some(subregions) = &mut self.subregions {
            for s in subregions.iter_mut() {
                s.resolve(&dir_path, file_opener)?;
                // Recursively resolve
                let subdir_path = dir_path.as_ref().join(Subregion::subregion_dir(&s.name));
                // Explicitly block symlinks, which could lead to infinite recursion.
                // If the path itself is invalid, just carry on and let file_opener deal with it.
                // Note that the documentation on is_symlink() is a bit ambiguous, but this method
                // (at least on Unix) will still follow symlinks on the path to get to the file,
                // it just won't follow the file's link if the file itself is a symlink.
                if subdir_path.is_symlink() {
                    return Err(Error::Subregion(SubregionError::Symlink(subdir_path)));
                }
                s.contents
                    .as_mut()
                    .expect("subregion not resolved after Subregion::resolve()")
                    .resolve_subregions(&subdir_path, file_opener)?;
            }
        }
        Ok(())
    }
    /// Moves all symbols within [`Subregion`]s into the [`Block`]'s main symbol lists, destroying
    /// the [`Subregion`]s in the process.
    pub fn collapse_subregions(&mut self) {
        if let Some(subregions) = self.subregions.take() {
            for s in subregions {
                if let Some(mut symgen) = s.contents {
                    // Recursively collapse
                    symgen.collapse_subregions();
                    for blocks in symgen.blocks_mut() {
                        self.functions.append(&mut blocks.functions);
                        self.data.append(&mut blocks.data);
                    }
                }
            }
        }
    }
    /// Gets the extent occupied by the [`Block`], possibly by version, represented as
    /// address-length pairs.
    pub fn extent(&self) -> MaybeVersionDep<(Uint, Option<Uint>)> {
        let mut versions: Vec<Version> = Vec::new();
        if let Some(v) = &self.versions {
            versions.extend(v.iter().cloned());
        }
        for v in self.address.versions().chain(self.length.versions()) {
            if !versions.contains(v) {
                versions.push(v.clone());
            }
        }
        if !versions.is_empty() {
            // Always realize the address with all versions (including inferred ones) if possible
            zip_addr_len(
                &MaybeVersionDep::ByVersion(self.address.by_version(&versions)),
                Some(&self.length),
            )
        } else {
            zip_addr_len(&self.address, Some(&self.length))
        }
    }
    /// Expands the versions of all the addresses and lengths contained within the [`Block`]
    /// (both in its metadata and the [`Symbol`]s it contains).
    ///
    /// See [`Symbol::expand_versions()`].
    pub fn expand_versions(&mut self) {
        // Note: this function does not try to infer versions from address/length because, unlike
        // with extent(), this function actually CHANGES Common to ByVersion, which sort of loses
        // information (because Common is purely generic).
        if let Some(vers) = &self.versions {
            self.address.expand_versions(vers);
            self.length.expand_versions(vers);
            self.functions.expand_versions(vers);
            self.data.expand_versions(vers);
        }
    }
    /// Looks up a [`Version`] in the [`Block`] by name.
    pub fn version(&self, name: &str) -> Option<&Version> {
        self.versions
            .as_ref()
            .and_then(|vs| vs.iter().find(|v| v.name() == name))
    }
    /// Returns a combined iterator over both function and data symbols in the [`Block`].
    pub fn iter(&self) -> impl Iterator<Item = &Symbol> {
        self.functions.iter().chain(self.data.iter())
    }
    /// Returns a combined iterator over both function and data symbols in the [`Block`], realized
    /// for the [`Version`] corresponding to `version_name`.
    pub fn iter_realized(&self, version_name: &str) -> impl Iterator<Item = RealizedSymbol> + '_ {
        let version = self.version(version_name);
        self.iter().realize(version)
    }
    /// Returns an iterator over function symbols in the [`Block`], realized for the [`Version`]
    /// corresponding to `version_name`.
    pub fn functions_realized(
        &self,
        version_name: &str,
    ) -> impl Iterator<Item = RealizedSymbol> + '_ {
        let version = self.version(version_name);
        self.functions.iter().realize(version)
    }
    /// Returns an iterator over data symbols in the [`Block`], realized for the [`Version`]
    /// corresponding to `version_name`.
    pub fn data_realized(&self, version_name: &str) -> impl Iterator<Item = RealizedSymbol> + '_ {
        let version = self.version(version_name);
        self.data.iter().realize(version)
    }

    /// Returns a [`BlockCursor`] for this [`Block`] with the given block name and file path.
    pub fn cursor<'s, 'p>(&'s self, name: &'s str, path: &'p Path) -> BlockCursor<'s, 'p> {
        BlockCursor::new(self, name, Cow::Borrowed(path))
    }
}

impl PartialOrd for Block {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Block {
    fn cmp(&self, other: &Self) -> Ordering {
        self.address.cmp(&other.address)
    }
}

impl Sort for Block {
    fn sort(&mut self) {
        if let Some(subregions) = &mut self.subregions {
            subregions.sort();
            for s in subregions {
                if let Some(contents) = &mut s.contents {
                    contents.sort();
                }
            }
        }
        self.functions.sort();
        self.data.sort();
    }
}

/// A programmatic representation of the `resymgen` YAML format.
///
/// At its core, a [`SymGen`] is just a mapping between block names and [`Block`]s, along with
/// convenient methods for manipulating the data within those [`Block`]s.
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct SymGen(BTreeMap<OrdString, Block>);

impl SymGen {
    /// Initializes all the block names and [`Block`]s within the [`SymGen`].
    pub fn init(&mut self) {
        // Get entries sorted by (block, name)
        let mut sorted_blocks: Vec<_> = self.0.iter().map(|(name, block)| (block, name)).collect();
        sorted_blocks.sort();
        // Might as well clone the names here because we'll need to do it later anyway
        let names: Vec<_> = sorted_blocks
            .into_iter()
            .map(|(_, name)| name.clone())
            .collect();
        let name_order = OrdString::get_order_map(Some(&names));

        // Remove each element, init keys and values, and reinsert
        for mut name in names {
            let mut block = self.0.remove(&name).unwrap();
            name.init(&name_order);
            block.init();
            self.0.insert(name, block);
        }
    }
    /// Reads an uninitialized [`SymGen`] from `rdr`.
    pub fn read_no_init<R: Read>(rdr: R) -> Result<SymGen> {
        serde_yaml::from_reader(rdr).map_err(Error::Yaml)
    }
    /// Reads a [`SymGen`] from `rdr`. The returned [`SymGen`] will be initialized.
    pub fn read<R: Read>(rdr: R) -> Result<SymGen> {
        let mut symgen: SymGen = SymGen::read_no_init(rdr)?;
        symgen.init();
        Ok(symgen)
    }
    /// Reads a [`SymGen`] from `rdr`. The returned [`SymGen`] will be initialized and sorted.
    ///
    /// [`Block`]s and their contained [`Symbol`]s are sorted by address. For version-dependent
    /// addresses, comparison is lexicographic in [`Version`] order.
    pub fn read_sorted<R: Read>(rdr: R) -> Result<SymGen> {
        let mut symgen = SymGen::read(rdr)?;
        symgen.sort();
        Ok(symgen)
    }
    /// Converts target fields in a `resymgen` YAML string with some inline operation,
    /// via line-by-line text processing.
    ///
    /// `F` injects modified lines into the final YAML string accumulator, based on the given line
    /// to be modified and the current indentation level, and returns a success flag.
    ///
    /// This is kind of a hack. Might be worth investigating whether it's easy to mod `yaml-rust`
    /// and `serde-yaml` to serialize in the desired format directly, rather than doing it via
    /// post-processing. But this is serviceable for now.
    fn convert_fields_inline<F, const N: usize>(
        yaml: &str,
        field_prefixes: [&str; N],
        convert: F,
    ) -> String
    where
        F: Fn(&mut String, &str, usize) -> bool,
    {
        // If under a target field, the level of whitespace for the field so we can tell when
        // the field has ended.
        let mut field_whitespace_level: Option<usize> = None;
        let mut converted_yaml = String::with_capacity(yaml.len());
        for line in yaml.lines() {
            // Strip out hyphens so that the first field in a SymbolList entry we'll still match
            // properly against it. Currently this is always be "name" (see Symbol), but this
            // is pretty easy to do and makes things less fragile if we change the field order.
            // Note that the strings in the Block version list cannot look like a YAML map
            // key (identifier followed by a colon) without being quoted, so we're safe from
            // user-defined versions in this case.
            let trimmed = line.trim_start().trim_start_matches('-').trim_start();
            // Safe to subtract usize here since trimmed.len() <= line.len()
            let whitespace_level = line.len() - trimmed.len();

            if let Some(l) = field_whitespace_level {
                if whitespace_level <= l {
                    // Field has ended; stop converting for now.
                    field_whitespace_level = None;
                }
            }

            if field_whitespace_level.is_none() {
                // Not currently matching a target field; look for a new one.
                //
                // Checking whitespace_level > 0 prevents matching against user-specified block
                // names that happen to start with the target field prefixes. This works because
                // yaml-rust doesn't indent top-level keys. In the unlikely event that this changes
                // we'd need to keep track of the whitespace level of the current top-level key
                // instead of assuming it's 0.
                //
                // YAML supports multiline strings in various forms
                // (https://stackoverflow.com/questions/3790454/how-do-i-break-a-string-in-yaml-over-multiple-lines),
                // but yaml-rust always emits string map values on a single line right after the
                // key (the symgen format doesn't use complex keys)
                // - map values: https://github.com/chyh1990/yaml-rust/blob/4fffe95cddbcf444f8a3f080364caf16a6c11ca6/src/emitter.rs#L230
                // - strings: https://github.com/chyh1990/yaml-rust/blob/4fffe95cddbcf444f8a3f080364caf16a6c11ca6/src/emitter.rs#L156
                // so we don't need to worry about the case where a user-specified string matching
                // target field prefixes.
                //
                // The only other place with arbitrary keys is the MaybeVersionDep's under the
                // target field keys, but since we should match the parent key first and enter
                // conversion mode before seeing the version keys, this shouldn't be a problem.
                if whitespace_level > 0
                    && field_prefixes
                        .iter()
                        .any(|prefix| trimmed.starts_with(prefix))
                {
                    field_whitespace_level = Some(whitespace_level);
                }
            };

            let mut success = false;
            if let Some(indent) = field_whitespace_level {
                // In a target field; convert subsequent lines.
                success = convert(&mut converted_yaml, line, indent);
            }
            if !success {
                // No conversion happened. Add the line unmodified
                converted_yaml.push_str(line);
            }
            // Add the newline back in. Note: Rust has standardized on '\n' for newlines on all platforms.
            // - https://doc.rust-lang.org/std/macro.println.html
            // - https://stackoverflow.com/questions/66450942/in-rust-is-there-a-way-to-make-literal-newlines-in-r-using-windows-c
            // Anyway, newer versions of Notepad support Unix line endings :D.
            // - https://devblogs.microsoft.com/commandline/extended-eol-in-notepad/
            converted_yaml.push('\n');
        }
        converted_yaml
    }
    /// Converts all integer values in a `resymgen` YAML string from decimal to hexadecimal.
    fn convert_dec_to_hex(yaml: &str) -> String {
        let re_int = Regex::new(r"\b\d+\b").unwrap();
        SymGen::convert_fields_inline(
            yaml,
            ["address:", "length:"],
            |converted_yaml, line, indent| {
                // Skip past any colons. This prevents us from replacing "numbers" that appear
                // within quoted version string keys, and we never expect to see any colons
                // after the key-value separator. Even if there's no colon, we can still skip
                // past the whitespace for free, since we have that stored already anyway.
                let start_idx = line.rfind(':').unwrap_or(indent);
                let converted = re_int.replace_all(&line[start_idx..], |caps: &Captures| {
                    let int = caps[0].parse::<Uint>().unwrap_or_else(|_| {
                        panic!(
                            "Could not parse {} as {}",
                            &caps[0],
                            any::type_name::<Uint>()
                        )
                    });
                    format!("{:#X}", int)
                });
                converted_yaml.push_str(&line[..start_idx]);
                converted_yaml.push_str(&converted);
                true
            },
        )
    }
    /// Converts all multiline description strings in a `resymgen` YAML string to block scalar
    /// format, for readability.
    fn convert_multiline_desc_to_block_scalar(yaml: &str) -> String {
        SymGen::convert_fields_inline(yaml, ["description:"], |converted_yaml, line, indent| {
            const SUB_INDENT: usize = 2;
            let start_idx;
            let contents;
            if let Some(idx) = line.find('"') {
                start_idx = idx;
                contents = Cow::Borrowed(&line[start_idx..]);
            } else if let Some(colon) = line.find(':') {
                if let Some(i) = line[colon + 1..].find(|c: char| !c.is_ascii_whitespace()) {
                    start_idx = colon + 1 + i;
                    // Manually add quotes so it can be parsed as a Rust string literal
                    contents = Cow::Owned(format!("\"{}\"", &line[start_idx..]));
                } else {
                    return false;
                }
            } else {
                return false;
            }
            if let Ok(l) = syn::parse_str::<LitStr>(&contents) {
                // Only convert multiline strings
                if l.value().trim_end().lines().count() > 1 {
                    converted_yaml.push_str(&line[..start_idx]);
                    converted_yaml.push_str("|-"); // There's no reason to have trailing newlines
                    for desc_ln in l.value().trim_end().lines() {
                        converted_yaml.push('\n');
                        for _ in 0..indent + SUB_INDENT {
                            converted_yaml.push(' ');
                        }
                        converted_yaml.push_str(desc_ln);
                    }
                    return true;
                }
            }
            false
        })
    }
    /// Writes the [`SymGen`] data to `writer` in `resymgen` YAML format.
    ///
    /// Integers will be written with the given `int_format`.
    pub fn write<W: Write>(&self, mut writer: W, int_format: IntFormat) -> Result<()> {
        // I don't expect these YAML files to be too big to fit in memory, so it's easier and
        // faster to keep the serialized data in memory for processing. And anyway,
        // serde_yaml::from_reader already uses read_to_end()
        // (https://github.com/dtolnay/serde-yaml/blob/644be1654d382627c4419f613e300c5e4df3650f/src/de.rs#L141)
        // so this shouldn't be much worse. If it ever becomes an issue (like with merging huge
        // files or something) this can be refactored to use another intermediate tempfile and a
        // BufReader/BufWriter or something.
        let mut yaml = serde_yaml::to_string(self).map_err(Error::Yaml)?;
        // yaml-rust's built-in behavior is to dump integers in decimal
        // (https://github.com/chyh1990/yaml-rust/blob/4fffe95cddbcf444f8a3f080364caf16a6c11ca6/src/emitter.rs#L173)
        // so writing in hex format requires further processing.
        if let IntFormat::Hexadecimal = int_format {
            yaml = SymGen::convert_dec_to_hex(&yaml);
        }
        yaml = SymGen::convert_multiline_desc_to_block_scalar(&yaml);

        // Skip past the unsightly "---" document-start that serde_yaml inserts (or rather
        // yaml-rust: https://github.com/chyh1990/yaml-rust/blob/4fffe95cddbcf444f8a3f080364caf16a6c11ca6/src/emitter.rs#L135).
        // We aren't using any YAML directives, we only ever serialize one object/document, and
        // serde_yaml doesn't support deserializing multiple documents anyway, so it's totally
        // optional.
        let yaml_bytes = yaml
            .strip_prefix("---")
            .unwrap_or(&yaml)
            .trim_start()
            .as_bytes();
        writer.write_all(yaml_bytes).map_err(Error::Io)
    }
    /// Writes the [`SymGen`] data to a [`String`] in `resymgen` YAML format.
    ///
    /// Integers will be written with the given `int_format`.
    pub fn write_to_str(&self, int_format: IntFormat) -> Result<String> {
        let mut bytes = Vec::<u8>::new();
        self.write(&mut bytes, int_format)?;
        String::from_utf8(bytes).map_err(Error::FromUtf8)
    }

    /// Recursively resolves the contents of all [`Subregion`]s in all [`Block`]s within the
    /// [`SymGen`].
    ///
    /// [`Subregion`]s are read from files using `file_opener`, with file paths based on the root
    /// directory specified by `dir_path`.
    pub fn resolve_subregions<P, R, F>(&mut self, dir_path: P, file_opener: F) -> Result<()>
    where
        P: AsRef<Path>,
        R: Read,
        F: Fn(&Path) -> io::Result<R> + Copy,
    {
        for block in self.0.values_mut() {
            block.resolve_subregions(&dir_path, file_opener)?;
        }
        Ok(())
    }
    /// Moves all symbols within [`Subregion`]s into their parent [`Block`]s' main symbol lists,
    /// destroying the [`Subregion`]s in the process.
    pub fn collapse_subregions(&mut self) {
        for block in self.0.values_mut() {
            block.collapse_subregions();
        }
    }

    /// Expands the versions of all the addresses and lengths contained within the [`SymGen`]
    /// (in all the contained [`Block`]s).
    ///
    /// See [`Block::expand_versions()`].
    pub fn expand_versions(&mut self) {
        for block in self.0.values_mut() {
            block.expand_versions();
        }
    }
    /// Gets a reference to the [`OrdString`] key in the [`SymGen`] corresponding to `block_name`,
    /// if present.
    pub fn block_key(&self, block_name: &str) -> Option<&OrdString> {
        self.0.keys().find(|k| k.val == block_name)
    }

    /// Gets a reference to the [`Block`] associated with `key`, if present.
    pub fn get(&self, key: &OrdString) -> Option<&Block> {
        self.0.get(key)
    }
    /// Gets a mutable reference to the [`Block`] associated with `key`, if present.
    pub fn get_mut(&mut self, key: &OrdString) -> Option<&mut Block> {
        self.0.get_mut(key)
    }
    /// Inserts the [`Block`] contained by `value` into the [`SymGen`], keyed by `key`.
    ///
    /// If the [`SymGen`] already had a [`Block`] keyed by `key`, the old [`Block`] is returned.
    pub fn insert(&mut self, key: OrdString, value: Block) -> Option<Block> {
        self.0.insert(key, value)
    }

    /// Returns an [`Iterator`] over references to (block name, [`Block`]) pairs in the [`SymGen`].
    pub fn iter(&self) -> impl Iterator<Item = (&OrdString, &Block)> {
        self.0.iter()
    }
    /// Returns an [`Iterator`] over mutable references to (block name, [`Block`]) pairs in the
    /// [`SymGen`].
    pub fn iter_mut(&mut self) -> impl Iterator<Item = (&OrdString, &mut Block)> {
        self.0.iter_mut()
    }
    /// Returns an [`Iterator`] over references to [`Block`]s in the [`SymGen`].
    pub fn blocks(&self) -> impl Iterator<Item = &Block> {
        self.0.values()
    }
    /// Returns an [`Iterator`] over mutable references to [`Block`]s in the [`SymGen`].
    pub fn blocks_mut(&mut self) -> impl Iterator<Item = &mut Block> {
        self.0.values_mut()
    }
    /// Returns a flat [`Iterator`] over references to the [`Symbol`]s contained within every
    /// [`Block`] in the [`SymGen`].
    pub fn symbols(&self) -> impl Iterator<Item = &Symbol> {
        self.blocks().flat_map(|b| b.iter())
    }
    /// Returns a flat [`Iterator`] over all symbols contained within every [`Block`] in
    /// the [`SymGen`], realized for the [`Version`] corresponding to `version_name`.
    pub fn symbols_realized(
        &self,
        version_name: &str,
    ) -> impl Iterator<Item = RealizedSymbol> + '_ {
        let v = String::from(version_name);
        self.blocks().flat_map(move |b| b.iter_realized(&v))
    }
    /// Returns a flat [`Iterator`] over all function symbols contained within every [`Block`] in
    /// the [`SymGen`], realized for the [`Version`] corresponding to `version_name`.
    pub fn functions_realized(
        &self,
        version_name: &str,
    ) -> impl Iterator<Item = RealizedSymbol> + '_ {
        let v = String::from(version_name);
        self.blocks().flat_map(move |b| b.functions_realized(&v))
    }
    /// Returns a flat [`Iterator`] over all data symbols contained within every [`Block`] in
    /// the [`SymGen`], realized for the [`Version`] corresponding to `version_name`.
    pub fn data_realized(&self, version_name: &str) -> impl Iterator<Item = RealizedSymbol> + '_ {
        let v = String::from(version_name);
        self.blocks().flat_map(move |b| b.data_realized(&v))
    }

    /// Returns a [`SymGenCursor`] for this [`SymGen`] with the given file path.
    pub fn cursor<'s, 'p>(&'s self, path: &'p Path) -> SymGenCursor<'s, 'p> {
        SymGenCursor::new(self, Cow::Borrowed(path))
    }
}

impl<const N: usize> From<[(OrdString, Block); N]> for SymGen {
    fn from(arr: [(OrdString, Block); N]) -> Self {
        SymGen(BTreeMap::from(arr))
    }
}

impl Display for SymGen {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let string = self.write_to_str(IntFormat::Hexadecimal);
        match string {
            Ok(s) => write!(f, "{}", s),
            Err(e) => write!(f, "{}", e),
        }
    }
}

impl Sort for SymGen {
    // Note: only applies to the blocks. As a BTreeMap, block keys will always be sorted.
    fn sort(&mut self) {
        // Sort each block
        for block in self.0.values_mut() {
            block.sort();
        }
    }
}

/// A subsidiary [`SymGen`] (a collection of named [`Block`]s) nested within a parent [`Block`].
///
/// A minimal [`Subregion`] consists of just a file name, which may or may not correspond to a
/// valid file. A [`Subregion`] can be "resolved" by associating a concrete [`SymGen`] with it,
/// typically by reading the contents of a file corresponding to the [`Subregion`]'s name.
/// The contents of a resolved [`Subregion`] are logically grouped together, but are ultimately
/// owned by the parent [`Block`].
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Subregion {
    pub name: PathBuf,
    // This doesn't actually need to be a Box to compile, but it's more space-efficient for the
    // common case of an unresolved subregion. It also allows for the null pointer optimization.
    #[serde(skip)]
    pub contents: Option<Box<SymGen>>,
}

impl Subregion {
    /// Get the canonical directory containing the subregion files for a given parent file path.
    pub fn subregion_dir<P: AsRef<Path>>(filepath: P) -> PathBuf {
        filepath.as_ref().with_extension("")
    }

    /// Whether this [`Subregion`] is associated with a concrete [`SymGen`].
    pub fn is_resolved(&self) -> bool {
        self.contents.is_some()
    }

    /// Resolve this [`Subregion`] with the contents of a file.
    ///
    /// The file is read using `file_opener`, with the file path derived from the directory
    /// specified by `dir_path` and the [`Subregion`]'s name.
    pub fn resolve<P, R, F>(&mut self, dir_path: P, file_opener: F) -> Result<()>
    where
        P: AsRef<Path>,
        R: Read,
        F: Fn(&Path) -> io::Result<R> + Copy,
    {
        if self.name.components().count() != 1 {
            return Err(Error::Subregion(SubregionError::InvalidPath(
                self.name.clone(),
            )));
        }
        let filepath = dir_path.as_ref().join(&self.name);
        let rdr = file_opener(&filepath).map_err(|e| {
            Error::Subregion(SubregionError::SymGen((
                filepath.clone(),
                Box::new(Error::Io(e)),
            )))
        })?;
        self.contents = Some(Box::new(SymGen::read(rdr).map_err(|e| {
            Error::Subregion(SubregionError::SymGen((filepath.clone(), Box::new(e))))
        })?));
        Ok(())
    }
    /// Unresolves this [`Subregion`] by discarding its contents, if any.
    pub fn unresolve(&mut self) {
        self.contents = None;
    }
}

impl<P> From<P> for Subregion
where
    P: AsRef<Path>,
{
    fn from(val: P) -> Self {
        // unresolved subregion
        Subregion {
            name: val.as_ref().to_owned(),
            contents: None,
        }
    }
}

impl<P> PartialEq<P> for Subregion
where
    P: AsRef<Path>,
{
    fn eq(&self, other: &P) -> bool {
        self.name == other.as_ref()
    }
}

impl PartialOrd for Subregion {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Subregion {
    fn cmp(&self, other: &Self) -> Ordering {
        self.name.cmp(&other.name)
    }
}

#[cfg(test)]
pub mod test_utils {
    use super::*;
    use std::collections::HashMap;
    use std::io;
    use std::path::{Path, PathBuf};

    pub fn get_symgen_with_subregions<P: AsRef<Path>>(
        root: &str,
        subregions: &[(P, &str)],
    ) -> SymGen {
        let mut symgen = SymGen::read(root.as_bytes()).expect("Failed to read SymGen");
        let root_dir = Path::new(file!());
        let file_map: HashMap<PathBuf, String> = subregions
            .iter()
            .map(|(p, s)| (root_dir.join(p.as_ref()), s.to_string()))
            .collect();
        symgen
            .resolve_subregions(root_dir, |p| {
                file_map
                    .get(p)
                    .map(|s| s.as_bytes())
                    .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, p.to_string_lossy()))
            })
            .expect("Failed to resolve subregions");
        symgen
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(test)]
    mod symbol_tests {
        use super::*;

        #[test]
        fn test_init_sort() {
            let versions = ["SI", "imperial", "natural"];
            let version_order = OrdString::get_order_map(Some(&versions));
            let ctx = BlockContext { version_order };

            let mut symbol = Symbol {
                name: "c".to_string(),
                aliases: Some(vec!["speed_of_light".to_string(), "lightspeed".to_string()]),
                address: MaybeVersionDep::ByVersion(
                    [
                        ("SI".into(), Linkable::from([1080000000, 299792458])),
                        ("natural".into(), Linkable::from(1)),
                        ("imperial".into(), Linkable::from([671000000, 186000])),
                    ]
                    .into(),
                ),
                length: Some(MaybeVersionDep::ByVersion(
                    [
                        ("natural".into(), 200),
                        ("imperial".into(), 300),
                        ("SI".into(), 100),
                    ]
                    .into(),
                )),
                description: Some("the speed of light".to_string()),
            };
            symbol.init(&ctx);
            symbol.sort();

            assert_eq!(
                &symbol,
                &Symbol {
                    name: "c".to_string(),
                    aliases: Some(vec!["lightspeed".to_string(), "speed_of_light".to_string()]),
                    address: MaybeVersionDep::ByVersion(
                        [
                            (("SI", 0).into(), Linkable::from([299792458, 1080000000])),
                            (("imperial", 1).into(), Linkable::from([186000, 671000000])),
                            (("natural", 2).into(), Linkable::from(1)),
                        ]
                        .into()
                    ),
                    length: Some(MaybeVersionDep::ByVersion(
                        [
                            (("SI", 0).into(), 100),
                            (("imperial", 1).into(), 300),
                            (("natural", 2).into(), 200),
                        ]
                        .into()
                    )),
                    description: Some("the speed of light".to_string()),
                }
            );
        }

        #[test]
        fn test_expand_versions() {
            let versions = [
                Version::from(("NA", 0)),
                Version::from(("EU", 1)),
                Version::from(("JP", 2)),
            ];
            let version_order = OrdString::get_order_map(Some(&versions));
            let ctx = BlockContext { version_order };
            let address = MaybeVersionDep::ByVersion(
                [
                    (versions[0].clone(), Linkable::from(0x2100000)),
                    (versions[1].clone(), Linkable::from(0x2100c00)),
                ]
                .into(),
            );
            let mut function = Symbol {
                name: "function".to_string(),
                aliases: None,
                address: address.clone(),
                length: Some(MaybeVersionDep::Common(0x100)),
                description: None,
            };
            function.init(&ctx);
            function.sort();
            function.expand_versions(&versions);

            assert_eq!(
                &function,
                &Symbol {
                    name: "function".to_string(),
                    aliases: None,
                    address: address.clone(),
                    length: Some(MaybeVersionDep::ByVersion(
                        [
                            (versions[0].clone(), 0x100),
                            (versions[1].clone(), 0x100),
                            (versions[2].clone(), 0x100),
                        ]
                        .into()
                    )),
                    description: None,
                }
            )
        }

        #[test]
        fn test_extents() {
            let versions = [
                Version::from(("NA", 0)),
                Version::from(("EU", 1)),
                Version::from(("JP", 1)),
            ];
            let function1 = Symbol {
                name: "function1".to_string(),
                aliases: None,
                address: MaybeVersionDep::ByVersion(
                    [
                        (versions[0].clone(), Linkable::from([0x2100000, 0x2100100])),
                        (versions[1].clone(), Linkable::from(0x2100c00)),
                    ]
                    .into(),
                ),
                length: Some(MaybeVersionDep::ByVersion(
                    [(versions[0].clone(), 0x100), (versions[2].clone(), 0x200)].into(),
                )),
                description: None,
            };
            let expected_extents = MaybeVersionDep::ByVersion(
                [
                    (
                        versions[0].clone(),
                        (Linkable::from([0x2100000, 0x2100100]), Some(0x100)),
                    ),
                    (versions[1].clone(), (Linkable::from(0x2100c00), None)),
                ]
                .into(),
            );
            assert_eq!(&function1.extents(Some(&versions)), &expected_extents);
            assert_eq!(&function1.extents(None), &expected_extents);

            // Weird edge case where we have versions for length but not address
            let function2 = Symbol {
                name: "function2".to_string(),
                aliases: None,
                address: MaybeVersionDep::Common(Linkable::from(0x2100000)),
                length: Some(MaybeVersionDep::ByVersion(
                    [(versions[0].clone(), 0x100), (versions[1].clone(), 0x200)].into(),
                )),
                description: None,
            };
            assert_eq!(
                &function2.extents(Some(&versions)),
                &MaybeVersionDep::ByVersion(
                    [
                        (
                            versions[0].clone(),
                            (Linkable::from(0x2100000), Some(0x100)),
                        ),
                        (
                            versions[1].clone(),
                            (Linkable::from(0x2100000), Some(0x200))
                        ),
                        (versions[2].clone(), (Linkable::from(0x2100000), None)),
                    ]
                    .into(),
                )
            );
            assert_eq!(
                &function2.extents(None),
                &MaybeVersionDep::ByVersion(
                    [
                        (
                            versions[0].clone(),
                            (Linkable::from(0x2100000), Some(0x100)),
                        ),
                        (
                            versions[1].clone(),
                            (Linkable::from(0x2100000), Some(0x200))
                        ),
                        // version[2] is missing!
                    ]
                    .into(),
                )
            );
        }

        #[test]
        fn test_cmp() {
            let versions = ["NA", "EU", "JP"];
            let version_order = OrdString::get_order_map(Some(&versions));
            let ctx = BlockContext { version_order };

            let mut function1 = Symbol {
                name: "function1".to_string(),
                aliases: None,
                address: MaybeVersionDep::ByVersion(
                    [
                        ("NA".into(), Linkable::from(0x2100000)),
                        ("EU".into(), Linkable::from(0x2100c00)),
                    ]
                    .into(),
                ),
                length: None,
                description: None,
            };
            function1.init(&ctx);
            function1.sort();

            let mut function2 = Symbol {
                name: "function2".to_string(),
                aliases: None,
                address: MaybeVersionDep::ByVersion(
                    [
                        ("NA".into(), Linkable::from(0x2101000)),
                        ("EU".into(), Linkable::from(0x2101c00)),
                        ("JP".into(), Linkable::from(0x2100e00)),
                    ]
                    .into(),
                ),
                length: None,
                description: None,
            };
            function2.init(&ctx);
            function2.sort();

            assert_eq!(function1.cmp(&function2), Ordering::Less)
        }
    }

    /// Returns a tuple of (uninited version list, final version list, uninited block addresses,
    /// final block addresses, uninited SymbolList, final SymbolList)
    fn get_block_data() -> (
        Vec<Version>,
        Vec<Version>,
        MaybeVersionDep<Uint>,
        MaybeVersionDep<Uint>,
        SymbolList,
        SymbolList,
    ) {
        (
            vec!["NA".into(), "EU".into()],
            vec![("NA", 0).into(), ("EU", 1).into()],
            MaybeVersionDep::ByVersion([("NA".into(), 0x2000000), ("EU".into(), 0x2000004)].into()),
            MaybeVersionDep::ByVersion(
                [(("NA", 0).into(), 0x2000000), (("EU", 1).into(), 0x2000004)].into(),
            ),
            SymbolList::from([
                Symbol {
                    name: "function2".to_string(),
                    aliases: None,
                    address: MaybeVersionDep::Common(Linkable::from([0x2101000, 0x2101100])),
                    length: None,
                    description: None,
                },
                Symbol {
                    name: "function1".to_string(),
                    aliases: Some(vec!["function1_alias".to_string()]),
                    address: MaybeVersionDep::ByVersion(
                        [
                            ("EU".into(), Linkable::from(0x2100c00)),
                            ("NA".into(), Linkable::from([0x2100100, 0x2100000])),
                        ]
                        .into(),
                    ),
                    length: Some(MaybeVersionDep::Common(0x100)),
                    description: None,
                },
            ]),
            SymbolList::from([
                Symbol {
                    name: "function1".to_string(),
                    aliases: Some(vec!["function1_alias".to_string()]),
                    address: MaybeVersionDep::ByVersion(
                        [
                            (("NA", 0).into(), Linkable::from([0x2100000, 0x2100100])),
                            (("EU", 1).into(), Linkable::from(0x2100c00)),
                        ]
                        .into(),
                    ),
                    length: Some(MaybeVersionDep::Common(0x100)),
                    description: None,
                },
                Symbol {
                    name: "function2".to_string(),
                    aliases: None,
                    address: MaybeVersionDep::Common(Linkable::from([0x2101000, 0x2101100])),
                    length: None,
                    description: None,
                },
            ]),
        )
    }

    #[cfg(test)]
    mod symbol_list_tests {
        use super::*;

        #[test]
        fn test_init_sort() {
            let (versions, _, _, _, mut list, final_list) = get_block_data();
            let version_order = OrdString::get_order_map(Some(&versions));
            let ctx = BlockContext { version_order };

            list.init(&ctx);
            list.sort();

            assert_eq!(&list, &final_list);
        }

        #[test]
        fn test_rangeset_from() {
            let rangeset = RangeSet::from(vec![
                (11, 19),
                (10, 20),   // fully subsume (11, 19)
                (15, 18),   // fully within (10, 20)
                (5, 12),    // extend (10, 20) leftwards
                (15, 30),   // extend (5, 20) rightwards
                (40, 50),   // disjoint
                (100, 200), // disjoint
                (1, 55),    // fully subsume (5, 30) + (40, 50)
                (56, 60),   // extend (1, 55) rightwards
            ]);
            assert_eq!(&rangeset, &RangeSet(vec![(1, 60), (100, 200),]));
        }

        #[test]
        fn test_rangeset_contains() {
            let rangeset = RangeSet(vec![(1, 60), (100, 200)]);
            assert!(!rangeset.contains(0));
            assert!(rangeset.contains(1));
            assert!(rangeset.contains(30));
            assert!(rangeset.contains(60));
            assert!(!rangeset.contains(75));
            assert!(rangeset.contains(100));
            assert!(rangeset.contains(150));
            assert!(rangeset.contains(200));
            assert!(!rangeset.contains(300));
        }

        fn make_symbol_list<const N: usize>(
            list: [(&str, MaybeVersionDep<Linkable>); N],
        ) -> SymbolList {
            let mut versions = BTreeSet::new();
            for i in list.iter() {
                for v in i.1.versions() {
                    versions.insert(v);
                }
            }
            let versions: Vec<_> = versions.into_iter().collect();
            let version_order = OrdString::get_order_map(Some(&versions));
            let ctx = BlockContext { version_order };

            let mut slist = SymbolList(
                list.iter()
                    .map(|i| Symbol {
                        name: i.0.to_string(),
                        aliases: None,
                        address: i.1.clone(),
                        length: None,
                        description: None,
                    })
                    .collect(),
            );
            slist.init(&ctx);
            slist
        }

        fn assert_sort_order<const N: usize>(
            list: [(&str, MaybeVersionDep<Linkable>); N],
            list_sorted: [(&str, MaybeVersionDep<Linkable>); N],
        ) {
            let mut list = make_symbol_list(list);
            let list_sorted = make_symbol_list(list_sorted);
            list.sort();
            assert_eq!(&list, &list_sorted);
        }

        #[test]
        fn test_sort_common() {
            assert_sort_order(
                [
                    ("symbol3", MaybeVersionDep::Common(3.into())),
                    ("symbol1", MaybeVersionDep::Common(1.into())),
                    ("symbol2", MaybeVersionDep::Common(2.into())),
                ],
                [
                    ("symbol1", MaybeVersionDep::Common(1.into())),
                    ("symbol2", MaybeVersionDep::Common(2.into())),
                    ("symbol3", MaybeVersionDep::Common(3.into())),
                ],
            );
        }

        #[test]
        fn test_sort_multipass() {
            assert_sort_order(
                [
                    (
                        "symbol1",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 0.into()),
                                ("v2".into(), 1.into()),
                                ("v3".into(), 2.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol3",
                        MaybeVersionDep::ByVersion(
                            [("v1".into(), 10.into()), ("v2".into(), 11.into())].into(),
                        ),
                    ),
                    (
                        "symbol5",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 20.into()),
                                ("v2".into(), 21.into()),
                                ("v3".into(), 22.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol0",
                        MaybeVersionDep::ByVersion([("v2".into(), 0.into())].into()),
                    ),
                    (
                        "symbol6",
                        MaybeVersionDep::ByVersion([("v2".into(), 100.into())].into()),
                    ),
                    (
                        "symbol2",
                        MaybeVersionDep::ByVersion([("v2".into(), 1.into())].into()),
                    ),
                    (
                        "symbol4",
                        MaybeVersionDep::ByVersion([("v3".into(), 15.into())].into()),
                    ),
                ],
                [
                    (
                        "symbol0",
                        MaybeVersionDep::ByVersion([("v2".into(), 0.into())].into()),
                    ),
                    (
                        "symbol1",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 0.into()),
                                ("v2".into(), 1.into()),
                                ("v3".into(), 2.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol2",
                        MaybeVersionDep::ByVersion([("v2".into(), 1.into())].into()),
                    ),
                    (
                        "symbol3",
                        MaybeVersionDep::ByVersion(
                            [("v1".into(), 10.into()), ("v2".into(), 11.into())].into(),
                        ),
                    ),
                    (
                        "symbol4",
                        MaybeVersionDep::ByVersion([("v3".into(), 15.into())].into()),
                    ),
                    (
                        "symbol5",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 20.into()),
                                ("v2".into(), 21.into()),
                                ("v3".into(), 22.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol6",
                        MaybeVersionDep::ByVersion([("v2".into(), 100.into())].into()),
                    ),
                ],
            );
        }

        #[test]
        fn test_sort_multipass_conflict() {
            assert_sort_order(
                [
                    (
                        "symbol1",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 0.into()),
                                ("v2".into(), 1.into()),
                                ("v3".into(), 2.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol2",
                        MaybeVersionDep::ByVersion(
                            [("v1".into(), 10.into()), ("v2".into(), 0.into())].into(),
                        ),
                    ),
                    (
                        "symbol4",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 20.into()),
                                ("v2".into(), 21.into()),
                                ("v3".into(), 22.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol5",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 30.into()),
                                ("v2".into(), 20.into()),
                                ("v3".into(), 32.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol6",
                        MaybeVersionDep::ByVersion([("v2".into(), 1.into())].into()),
                    ),
                    (
                        "symbol3a",
                        MaybeVersionDep::ByVersion([("v2".into(), 10.into())].into()),
                    ),
                    (
                        "symbol3b",
                        MaybeVersionDep::ByVersion([("v3".into(), 15.into())].into()),
                    ),
                    (
                        "symbol7",
                        MaybeVersionDep::ByVersion([("v2".into(), 20.into())].into()),
                    ),
                ],
                [
                    (
                        "symbol1",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 0.into()),
                                ("v2".into(), 1.into()),
                                ("v3".into(), 2.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol2",
                        MaybeVersionDep::ByVersion(
                            [("v1".into(), 10.into()), ("v2".into(), 0.into())].into(),
                        ),
                    ),
                    (
                        "symbol3a",
                        MaybeVersionDep::ByVersion([("v2".into(), 10.into())].into()),
                    ),
                    (
                        "symbol3b",
                        MaybeVersionDep::ByVersion([("v3".into(), 15.into())].into()),
                    ),
                    (
                        "symbol4",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 20.into()),
                                ("v2".into(), 21.into()),
                                ("v3".into(), 22.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol5",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 30.into()),
                                ("v2".into(), 20.into()),
                                ("v3".into(), 32.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol6",
                        // Conflicts with symbol1/symbol2
                        MaybeVersionDep::ByVersion([("v2".into(), 1.into())].into()),
                    ),
                    (
                        "symbol7",
                        // Conflicts with symbol4/symbol5
                        MaybeVersionDep::ByVersion([("v2".into(), 20.into())].into()),
                    ),
                ],
            );
        }

        #[test]
        fn test_sort_multipass_conflict_cascade() {
            assert_sort_order(
                [
                    (
                        "symbol1",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 0.into()),
                                ("v2".into(), 1.into()),
                                ("v3".into(), 2.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol3",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 10.into()),
                                ("v2".into(), 0.into()),
                                ("v3".into(), 12.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol5",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 20.into()),
                                ("v2".into(), 21.into()),
                                ("v3".into(), 22.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol6",
                        MaybeVersionDep::ByVersion(
                            [("v2".into(), 1.into()), ("v3".into(), 13.into())].into(),
                        ),
                    ),
                    (
                        "symbol4",
                        MaybeVersionDep::ByVersion([("v2".into(), 10.into())].into()),
                    ),
                    (
                        "symbol7",
                        MaybeVersionDep::ByVersion([("v3".into(), 15.into())].into()),
                    ),
                    (
                        "symbol2",
                        MaybeVersionDep::ByVersion([("v3".into(), 10.into())].into()),
                    ),
                ],
                [
                    (
                        "symbol1",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 0.into()),
                                ("v2".into(), 1.into()),
                                ("v3".into(), 2.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol2",
                        MaybeVersionDep::ByVersion([("v3".into(), 10.into())].into()),
                    ),
                    (
                        "symbol3",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 10.into()),
                                ("v2".into(), 0.into()),
                                ("v3".into(), 12.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol4",
                        MaybeVersionDep::ByVersion([("v2".into(), 10.into())].into()),
                    ),
                    (
                        "symbol5",
                        MaybeVersionDep::ByVersion(
                            [
                                ("v1".into(), 20.into()),
                                ("v2".into(), 21.into()),
                                ("v3".into(), 22.into()),
                            ]
                            .into(),
                        ),
                    ),
                    (
                        "symbol6",
                        // Conflicts with symbol1/symbol3
                        MaybeVersionDep::ByVersion(
                            [("v2".into(), 1.into()), ("v3".into(), 13.into())].into(),
                        ),
                    ),
                    (
                        "symbol7",
                        // Conflicts with symbol5/symbol6
                        MaybeVersionDep::ByVersion([("v3".into(), 15.into())].into()),
                    ),
                ],
            );
        }

        #[test]
        fn test_iter_realize() {
            let (_, versions, _, _, _, list) = get_block_data();

            let mut iter0 = list.iter().realize(Some(&versions[0]));
            let function1_aliases = ["function1_alias".to_string()];
            let exp0 = [
                RealizedSymbol {
                    name: &"function1",
                    aliases: Some(&function1_aliases),
                    address: 0x2100000,
                    length: Some(0x100),
                    description: None,
                },
                RealizedSymbol {
                    name: &"function1",
                    aliases: Some(&function1_aliases),
                    address: 0x2100100,
                    length: Some(0x100),
                    description: None,
                },
                RealizedSymbol {
                    name: &"function2",
                    aliases: None,
                    address: 0x2101000,
                    length: None,
                    description: None,
                },
                RealizedSymbol {
                    name: &"function2",
                    aliases: None,
                    address: 0x2101100,
                    length: None,
                    description: None,
                },
            ];
            for e in exp0.iter() {
                assert_eq!(iter0.next().as_ref(), Some(e));
            }
            assert_eq!(iter0.next(), None);

            let mut iter1 = list.iter().realize(Some(&versions[1]));
            let function1_aliases = ["function1_alias".to_string()];
            let exp1 = [
                RealizedSymbol {
                    name: &"function1",
                    aliases: Some(&function1_aliases),
                    address: 0x2100c00,
                    length: Some(0x100),
                    description: None,
                },
                RealizedSymbol {
                    name: &"function2",
                    aliases: None,
                    address: 0x2101000,
                    length: None,
                    description: None,
                },
                RealizedSymbol {
                    name: &"function2",
                    aliases: None,
                    address: 0x2101100,
                    length: None,
                    description: None,
                },
            ];
            for e in exp1.iter() {
                assert_eq!(iter1.next().as_ref(), Some(e));
            }
            assert_eq!(iter1.next(), None);
        }

        #[test]
        fn test_iter_realize_with_none() {
            let (_, _, _, _, _, list) = get_block_data();
            let mut iter = list.iter().realize(None);
            let exp = [
                RealizedSymbol {
                    name: &"function2",
                    aliases: None,
                    address: 0x2101000,
                    length: None,
                    description: None,
                },
                RealizedSymbol {
                    name: &"function2",
                    aliases: None,
                    address: 0x2101100,
                    length: None,
                    description: None,
                },
            ];
            for e in exp.iter() {
                assert_eq!(iter.next().as_ref(), Some(e));
            }
            assert_eq!(iter.next(), None);
        }
    }

    #[cfg(test)]
    mod block_tests {
        use super::*;

        fn get_sorted_block() -> Block {
            let (versions, _, addresses, _, symbols, _) = get_block_data();

            let mut block = Block {
                versions: Some(versions),
                address: addresses.clone(),
                length: addresses.clone(),
                description: None,
                subregions: None,
                functions: symbols.clone(),
                data: symbols.clone(),
            };
            block.init();
            block.sort();
            block
        }

        #[test]
        fn test_init_sort() {
            let mut block = get_sorted_block();
            // Add some subregions manually
            block.subregions = Some(vec!["subregion2".into(), "subregion1".into()]);
            block.sort();

            let (_, final_versions, _, final_addresses, _, final_symbols) = get_block_data();
            let mut final_subregions = block.subregions.clone().unwrap();
            final_subregions.sort();
            assert_eq!(
                &block,
                &Block {
                    versions: Some(final_versions),
                    address: final_addresses.clone(),
                    length: final_addresses.clone(),
                    description: None,
                    subregions: Some(final_subregions.clone()),
                    functions: final_symbols.clone(),
                    data: final_symbols.clone(),
                }
            )
        }

        #[test]
        fn test_expand_versions() {
            let mut block = get_sorted_block();
            let versions = block.versions.clone();
            let description = block.description.clone();
            let address = block.address.clone();
            let length = block.length.clone();
            let expanded_symbols = SymbolList::from([
                Symbol {
                    name: "function1".to_string(),
                    aliases: Some(vec!["function1_alias".to_string()]),
                    address: MaybeVersionDep::ByVersion(
                        [
                            (("NA", 0).into(), Linkable::from([0x2100000, 0x2100100])),
                            (("EU", 1).into(), Linkable::from(0x2100c00)),
                        ]
                        .into(),
                    ),
                    length: Some(MaybeVersionDep::ByVersion(
                        [(("NA", 0).into(), 0x100), (("EU", 1).into(), 0x100)].into(),
                    )),
                    description: None,
                },
                Symbol {
                    name: "function2".to_string(),
                    aliases: None,
                    address: MaybeVersionDep::ByVersion(
                        [
                            (("NA", 0).into(), Linkable::from([0x2101000, 0x2101100])),
                            (("EU", 1).into(), Linkable::from([0x2101000, 0x2101100])),
                        ]
                        .into(),
                    ),
                    length: None,
                    description: None,
                },
            ]);
            block.expand_versions();
            assert_eq!(
                &block,
                &Block {
                    versions,
                    address,
                    length,
                    description,
                    subregions: None,
                    functions: expanded_symbols.clone(),
                    data: expanded_symbols.clone(),
                }
            )
        }

        #[test]
        fn test_extent() {
            let block = get_sorted_block();
            assert_eq!(
                &block.extent(),
                &MaybeVersionDep::ByVersion(
                    [
                        (("NA", 0).into(), (0x2000000, Some(0x2000000))),
                        (("EU", 1).into(), (0x2000004, Some(0x2000004))),
                    ]
                    .into()
                )
            );
        }

        #[test]
        fn test_iter() {
            let block = get_sorted_block();
            let (_, _, _, _, _, final_symbols) = get_block_data();
            let mut block_iter = block.iter();
            for e in final_symbols.iter().chain(final_symbols.iter()) {
                assert_eq!(block_iter.next(), Some(e));
            }
            assert_eq!(block_iter.next(), None);
        }

        #[test]
        fn test_iter_realized() {
            let block = get_sorted_block();
            let mut iter = block.iter_realized("NA");
            let mut function_iter = block.functions_realized("NA");
            let mut data_iter = block.data_realized("NA");
            let function1_aliases = ["function1_alias".to_string()];
            let exp = [
                RealizedSymbol {
                    name: &"function1",
                    aliases: Some(&function1_aliases),
                    address: 0x2100000,
                    length: Some(0x100),
                    description: None,
                },
                RealizedSymbol {
                    name: &"function1",
                    aliases: Some(&function1_aliases),
                    address: 0x2100100,
                    length: Some(0x100),
                    description: None,
                },
                RealizedSymbol {
                    name: &"function2",
                    aliases: None,
                    address: 0x2101000,
                    length: None,
                    description: None,
                },
                RealizedSymbol {
                    name: &"function2",
                    aliases: None,
                    address: 0x2101100,
                    length: None,
                    description: None,
                },
            ];
            for e in exp.iter().chain(exp.iter()) {
                assert_eq!(iter.next().as_ref(), Some(e));
            }
            assert_eq!(iter.next(), None);

            // realizing function and data individually should work pretty much the same
            for e in exp.iter() {
                assert_eq!(function_iter.next().as_ref(), Some(e));
            }
            assert_eq!(function_iter.next(), None);

            for e in exp.iter() {
                assert_eq!(data_iter.next().as_ref(), Some(e));
            }
            assert_eq!(data_iter.next(), None);
        }

        #[test]
        fn test_iter_realized_missing_key() {
            let block = get_sorted_block();
            let mut iter = block.iter_realized("JP");
            // Should still yield the Common info.
            let exp = [
                RealizedSymbol {
                    name: &"function2",
                    aliases: None,
                    address: 0x2101000,
                    length: None,
                    description: None,
                },
                RealizedSymbol {
                    name: &"function2",
                    aliases: None,
                    address: 0x2101100,
                    length: None,
                    description: None,
                },
            ];
            for e in exp.iter().chain(exp.iter()) {
                assert_eq!(iter.next().as_ref(), Some(e));
            }
            assert_eq!(iter.next(), None);
        }
    }

    #[cfg(test)]
    mod symgen_tests {
        use super::*;

        /// Returns a tuple of (symgen string, inited+sorted SymGen)
        fn get_symgen_data() -> (String, SymGen) {
            (
                String::from(
                    r#"main:
  versions:
    - v1
    - v2
  address:
    v1: 0x2000000
    v2: 0x2000000
  length:
    v1: 0x100000
    v2: 0x100004
  description: foo
  functions:
    - name: fn1
      aliases:
        - fn1_alias
      address:
        v1: 0x2001000
        v2: 0x2002000
      length: 0x1000
      description: |-
        multi
        line
        description
    - name: fn2
      address:
        v1:
          - 0x2002000
          - 0x2003000
        v2: 0x2003000
      description: baz
  data:
    - name: SOME_DATA
      address:
        v1: 0x2000000
        v2: 0x2000000
      length:
        v1: 0x1000
        v2: 0x2000
      description: foo bar baz
other:
  address: 0x2100000
  length: 0x100000
  functions:
    - name: fn3
      address: 0x2100000
  data: []
"#,
                ),
                SymGen::from([
                    (
                        ("main", 0).into(),
                        Block {
                            versions: Some(vec![("v1", 0).into(), ("v2", 1).into()]),
                            address: MaybeVersionDep::ByVersion(
                                [(("v1", 0).into(), 0x2000000), (("v2", 1).into(), 0x2000000)]
                                    .into(),
                            ),
                            length: MaybeVersionDep::ByVersion(
                                [(("v1", 0).into(), 0x100000), (("v2", 1).into(), 0x100004)].into(),
                            ),
                            description: Some("foo".to_string()),
                            subregions: None,
                            functions: [
                                Symbol {
                                    name: "fn1".to_string(),
                                    aliases: Some(vec!["fn1_alias".to_string()]),
                                    address: MaybeVersionDep::ByVersion(
                                        [
                                            (("v1", 0).into(), 0x2001000.into()),
                                            (("v2", 1).into(), 0x2002000.into()),
                                        ]
                                        .into(),
                                    ),
                                    length: Some(MaybeVersionDep::Common(0x1000)),
                                    description: Some("multi\nline\ndescription".to_string()),
                                },
                                Symbol {
                                    name: "fn2".to_string(),
                                    aliases: None,
                                    address: MaybeVersionDep::ByVersion(
                                        [
                                            (("v1", 0).into(), [0x2002000, 0x2003000].into()),
                                            (("v2", 1).into(), 0x2003000.into()),
                                        ]
                                        .into(),
                                    ),
                                    length: None,
                                    description: Some("baz".to_string()),
                                },
                            ]
                            .into(),
                            data: [Symbol {
                                name: "SOME_DATA".to_string(),
                                aliases: None,
                                address: MaybeVersionDep::ByVersion(
                                    [
                                        (("v1", 0).into(), 0x2000000.into()),
                                        (("v2", 1).into(), 0x2000000.into()),
                                    ]
                                    .into(),
                                ),
                                length: Some(MaybeVersionDep::ByVersion(
                                    [(("v1", 0).into(), 0x1000), (("v2", 1).into(), 0x2000)].into(),
                                )),
                                description: Some("foo bar baz".to_string()),
                            }]
                            .into(),
                        },
                    ),
                    (
                        ("other", 1).into(),
                        Block {
                            versions: None,
                            address: MaybeVersionDep::Common(0x2100000),
                            length: MaybeVersionDep::Common(0x100000),
                            description: None,
                            subregions: None,
                            functions: [Symbol {
                                name: "fn3".to_string(),
                                aliases: None,
                                address: MaybeVersionDep::Common(0x2100000.into()),
                                length: None,
                                description: None,
                            }]
                            .into(),
                            data: [].into(),
                        },
                    ),
                ]),
            )
        }

        /// Same as get_symgen_data(), but with 64-bit data
        fn get_symgen_data_64bit() -> (String, SymGen) {
            (
                String::from(
                    r#"main:
  versions:
    - v1
    - v2
  address:
    v1: 0x2000000FF
    v2: 0x2000000FF
  length:
    v1: 0x100000FF
    v2: 0x100004FF
  description: foo
  functions:
    - name: fn1
      address:
        v1: 0x2001000FF
        v2: 0x2002000FF
      length: 0x1000
      description: |-
        multi
        line
        description
    - name: fn2
      address:
        v1:
          - 0x2002000FF
          - 0x2003000FF
        v2: 0x2003000FF
      description: baz
  data:
    - name: SOME_DATA
      address:
        v1: 0x2000000FF
        v2: 0x2000000FF
      length:
        v1: 0x1000
        v2: 0x2000
      description: foo bar baz
other:
  address: 0x2100000FFFF
  length: 0x100000FFFF
  functions:
    - name: fn3
      address: 0x2100000FFFF
  data: []
"#,
                ),
                SymGen::from([
                    (
                        ("main", 0).into(),
                        Block {
                            versions: Some(vec![("v1", 0).into(), ("v2", 1).into()]),
                            address: MaybeVersionDep::ByVersion(
                                [
                                    (("v1", 0).into(), 0x2000000FF),
                                    (("v2", 1).into(), 0x2000000FF),
                                ]
                                .into(),
                            ),
                            length: MaybeVersionDep::ByVersion(
                                [
                                    (("v1", 0).into(), 0x100000FF),
                                    (("v2", 1).into(), 0x100004FF),
                                ]
                                .into(),
                            ),
                            description: Some("foo".to_string()),
                            subregions: None,
                            functions: [
                                Symbol {
                                    name: "fn1".to_string(),
                                    aliases: None,
                                    address: MaybeVersionDep::ByVersion(
                                        [
                                            (("v1", 0).into(), 0x2001000FF.into()),
                                            (("v2", 1).into(), 0x2002000FF.into()),
                                        ]
                                        .into(),
                                    ),
                                    length: Some(MaybeVersionDep::Common(0x1000)),
                                    description: Some("multi\nline\ndescription".to_string()),
                                },
                                Symbol {
                                    name: "fn2".to_string(),
                                    aliases: None,
                                    address: MaybeVersionDep::ByVersion(
                                        [
                                            (("v1", 0).into(), [0x2002000FF, 0x2003000FF].into()),
                                            (("v2", 1).into(), 0x2003000FF.into()),
                                        ]
                                        .into(),
                                    ),
                                    length: None,
                                    description: Some("baz".to_string()),
                                },
                            ]
                            .into(),
                            data: [Symbol {
                                name: "SOME_DATA".to_string(),
                                aliases: None,
                                address: MaybeVersionDep::ByVersion(
                                    [
                                        (("v1", 0).into(), 0x2000000FF.into()),
                                        (("v2", 1).into(), 0x2000000FF.into()),
                                    ]
                                    .into(),
                                ),
                                length: Some(MaybeVersionDep::ByVersion(
                                    [(("v1", 0).into(), 0x1000), (("v2", 1).into(), 0x2000)].into(),
                                )),
                                description: Some("foo bar baz".to_string()),
                            }]
                            .into(),
                        },
                    ),
                    (
                        ("other", 1).into(),
                        Block {
                            versions: None,
                            address: MaybeVersionDep::Common(0x2100000FFFF),
                            length: MaybeVersionDep::Common(0x100000FFFF),
                            description: None,
                            subregions: None,
                            functions: [Symbol {
                                name: "fn3".to_string(),
                                aliases: None,
                                address: MaybeVersionDep::Common(0x2100000FFFF.into()),
                                length: None,
                                description: None,
                            }]
                            .into(),
                            data: [].into(),
                        },
                    ),
                ]),
            )
        }

        fn read_test_template<F: FnOnce() -> (String, SymGen)>(get_data: F) {
            let (input, expected) = get_data();
            let obj = SymGen::read(input.as_bytes()).expect("Read failed");
            assert_eq!(&obj, &expected);
        }

        #[test]
        fn test_read() {
            read_test_template(get_symgen_data);
        }

        #[test]
        fn test_read_64bit() {
            read_test_template(get_symgen_data_64bit);
        }

        fn write_test_template<F: FnOnce() -> (String, SymGen)>(get_data: F) {
            let (expected, input) = get_data();
            let yaml = input
                .write_to_str(IntFormat::Hexadecimal)
                .expect("Write failed");
            assert_eq!(&yaml, &expected);
        }

        #[test]
        fn test_write() {
            write_test_template(get_symgen_data);
        }

        #[test]
        fn test_write_64bit() {
            write_test_template(get_symgen_data_64bit);
        }

        #[test]
        fn test_expand_versions() {
            let (_, mut symgen) = get_symgen_data();
            let main_block_key = symgen.block_key("main").unwrap().clone();
            let other_block_key = symgen.block_key("other").unwrap().clone();
            let mut main_block = symgen.get(&main_block_key).unwrap().clone();
            let other_block = symgen.get(&other_block_key).unwrap().clone();
            main_block.expand_versions();
            // other_block doesn't have a version list so expanding shouldn't do anything
            symgen.expand_versions();
            assert_eq!(
                &symgen,
                &SymGen::from([(main_block_key, main_block), (other_block_key, other_block)])
            );
        }

        #[test]
        fn test_block_key() {
            let (_, symgen) = get_symgen_data();
            assert_eq!(symgen.block_key("main"), Some(&("main", 0).into()));
            assert_eq!(symgen.block_key("other"), Some(&("other", 1).into()));
            assert_eq!(symgen.block_key("not_a_block_name"), None);
        }

        #[test]
        fn test_iter() {
            let (_, symgen) = get_symgen_data();
            let main_block_key = symgen.block_key("main").unwrap();
            let other_block_key = symgen.block_key("other").unwrap();
            let main_block = symgen.get(main_block_key).unwrap();
            let other_block = symgen.get(other_block_key).unwrap();

            let mut iter = symgen.iter();
            assert_eq!(iter.next(), Some((main_block_key, main_block)));
            assert_eq!(iter.next(), Some((other_block_key, other_block)));
            assert_eq!(iter.next(), None);
        }

        #[test]
        fn test_symbols_realized() {
            let (_, symgen) = get_symgen_data();
            let version_str = "v1";
            let fn1_aliases = ["fn1_alias".to_string()];
            let functions_main_exp = [
                RealizedSymbol {
                    name: &"fn1",
                    aliases: Some(&fn1_aliases),
                    address: 0x2001000,
                    length: Some(0x1000),
                    description: Some(&"multi\nline\ndescription"),
                },
                RealizedSymbol {
                    name: &"fn2",
                    aliases: None,
                    address: 0x2002000,
                    length: None,
                    description: Some(&"baz"),
                },
                RealizedSymbol {
                    name: &"fn2",
                    aliases: None,
                    address: 0x2003000,
                    length: None,
                    description: Some(&"baz"),
                },
            ];
            let data_main_exp = [RealizedSymbol {
                name: &"SOME_DATA",
                aliases: None,
                address: 0x2000000,
                length: Some(0x1000),
                description: Some(&"foo bar baz"),
            }];
            let functions_other_exp = [RealizedSymbol {
                name: &"fn3",
                aliases: None,
                address: 0x2100000,
                length: None,
                description: None,
            }];

            let mut iter = symgen.symbols_realized(version_str);
            for e in functions_main_exp
                .iter()
                .chain(data_main_exp.iter())
                .chain(functions_other_exp.iter())
            {
                assert_eq!(iter.next().as_ref(), Some(e));
            }
            assert_eq!(iter.next(), None);

            // These should work basically the same
            let mut functions_iter = symgen.functions_realized(version_str);
            for e in functions_main_exp.iter().chain(functions_other_exp.iter()) {
                assert_eq!(functions_iter.next().as_ref(), Some(e));
            }
            assert_eq!(functions_iter.next(), None);

            let mut data_iter = symgen.data_realized(version_str);
            for e in data_main_exp.iter() {
                assert_eq!(data_iter.next().as_ref(), Some(e));
            }
            assert_eq!(data_iter.next(), None);
        }
    }

    #[cfg(test)]
    mod subregion_tests {
        use super::*;

        #[test]
        fn test_subregion_dir() {
            assert_eq!(Subregion::subregion_dir("test.yml"), Path::new("test"));
            assert_eq!(
                Subregion::subregion_dir("path/to/test.yml"),
                Path::new("path/to/test")
            );
            assert_eq!(
                Subregion::subregion_dir("/abs/path/to/test.yml"),
                Path::new("/abs/path/to/test")
            );
        }

        fn get_basic_subregion<P: AsRef<Path>>(name: P) -> (Subregion, String) {
            let text = format!(
                r#"{}:
                address: 0x0
                length: 0x100
                functions: []
                data: []
                "#,
                Subregion::subregion_dir(&name).display()
            );
            let sub = Subregion {
                name: name.as_ref().to_owned(),
                contents: Some(Box::new(
                    SymGen::read(text.as_bytes()).expect("Failed to read SymGen"),
                )),
            };
            (sub, text)
        }

        fn get_parent_subregion<P: AsRef<Path>>(
            name: P,
            subregions: &[(P, Subregion)],
        ) -> (Subregion, String) {
            let text = format!(
                r#"{}:
                address: 0x0
                length: 0x100
                subregions: {:?}
                functions: []
                data: []
                "#,
                Subregion::subregion_dir(&name).display(),
                subregions
                    .iter()
                    .map(|(p, _)| p.as_ref().display())
                    .collect::<Vec<_>>()
            );
            let mut sub = Subregion {
                name: name.as_ref().to_owned(),
                contents: Some(Box::new(
                    SymGen::read(text.as_bytes()).expect("Failed to read SymGen"),
                )),
            };
            sub.contents
                .as_mut()
                .unwrap()
                .blocks_mut()
                .next()
                .unwrap()
                .subregions = Some(subregions.iter().map(|(_, s)| s.clone()).collect());
            (sub, text)
        }

        #[test]
        fn test_resolve() {
            let name = "sub.yml";
            let (resolved, text) = get_basic_subregion(name);
            let mut subregion = Subregion::from(name);
            assert!(!subregion.is_resolved());
            subregion
                .resolve("", |_| Ok(text.as_bytes()))
                .expect("Failed to resolve subregion");
            assert!(subregion.is_resolved());
            assert_eq!(&subregion, &resolved);
        }

        #[test]
        fn test_invalid_path() {
            let mut subregion = Subregion::from("dir/sub.yml");
            let res = subregion.resolve("", |_| Ok("".as_bytes()));
            assert!(matches!(
                res,
                Err(Error::Subregion(SubregionError::InvalidPath(_)))
            ));
        }

        #[test]
        fn test_recursive_resolve_subregions() {
            let (name1, name2, name3) = ("sub1.yml", "sub2.yml", "sub3.yml");
            let mut symgen = SymGen::read(
                format!(
                    r#"main:
                    address: 0x0
                    length: 0x100
                    subregions:
                      - {}
                      - {}
                    functions: []
                    data: []
                    "#,
                    name1, name2
                )
                .as_bytes(),
            )
            .expect("Failed to read SymGen");
            let (sub1, text1) = get_basic_subregion(name1);
            let (sub3, text3) = get_basic_subregion(name3);
            let (sub2, text2) = get_parent_subregion(name2, &[(name3, sub3)]);
            // Use this source file path as the root_dir in order to ensure that none of the test
            // subregion paths are actually real, and thus that the recursive symlink check will
            // never be set off. Technically this depends on the working directory when the test
            // binary is run, but this should be a good enough safeguard...
            let root_dir = Path::new(file!());
            let file_map: HashMap<PathBuf, String> = [
                (root_dir.join(name1), text1),
                (root_dir.join(name2), text2),
                (
                    root_dir.join(Subregion::subregion_dir(name2)).join(name3),
                    text3,
                ),
            ]
            .into();

            symgen
                .resolve_subregions(root_dir, |p| {
                    file_map
                        .get(p)
                        .map(|s| s.as_bytes())
                        .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, p.to_string_lossy()))
                })
                .expect("Failed to resolve subregions");

            let block = symgen.blocks().next().unwrap();
            let block_subregions: Vec<&Subregion> = block
                .subregions
                .as_ref()
                .expect("Block has no subregions?")
                .iter()
                .collect();
            assert_eq!(block_subregions[0], &sub1);
            assert_eq!(block_subregions[1], &sub2);
        }

        #[test]
        fn test_recursive_collapse_subregions() {
            let (name1, name2, name3) = ("sub1.yml", "sub2.yml", "sub3.yml");
            let mut symgen = SymGen::read(
                format!(
                    r#"main:
                    address: 0x0
                    length: 0x100
                    subregions:
                      - {}
                      - {}
                    functions:
                      - name: fn0
                        address: 0x0
                    data: []
                    "#,
                    name1, name2
                )
                .as_bytes(),
            )
            .expect("Failed to read SymGen");
            let text1 = r#"sub1:
                address: 0x0
                length: 0x100
                functions: []
                data:
                  - name: data1
                    address: 0x10
                    length: 0x4
                "#;
            let text2 = r#"sub2:
                address: 0x0
                length: 0x100
                subregions:
                  - sub3.yml
                functions:
                  - name: fn2
                    address: 0x8
                data:
                  - name: data2
                    address: 0x20
                    length: 0x4
                "#;
            let text3 = r#"sub3:
                address: 0x0
                length: 0x100
                functions:
                  - name: fn3
                    address: 0xC
                data:
                  - name: data3
                    address: 0x30
                    length: 0x4
                "#;

            let collapsed_symgen = SymGen::read(
                r#"main:
                address: 0x0
                length: 0x100
                functions:
                  - name: fn0
                    address: 0x0
                  - name: fn2
                    address: 0x8
                  - name: fn3
                    address: 0xC
                data:
                  - name: data1
                    address: 0x10
                    length: 0x4
                  - name: data2
                    address: 0x20
                    length: 0x4
                  - name: data3
                    address: 0x30
                    length: 0x4
                "#
                .as_bytes(),
            )
            .expect("Failed to read SymGen");

            let root_dir = Path::new(file!());
            let file_map: HashMap<PathBuf, String> = [
                (root_dir.join(name1), text1.to_owned()),
                (root_dir.join(name2), text2.to_owned()),
                (
                    root_dir.join(Subregion::subregion_dir(name2)).join(name3),
                    text3.to_owned(),
                ),
            ]
            .into();

            symgen
                .resolve_subregions(root_dir, |p| {
                    file_map
                        .get(p)
                        .map(|s| s.as_bytes())
                        .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, p.to_string_lossy()))
                })
                .expect("Failed to resolve subregions");

            symgen.collapse_subregions();
            assert_eq!(&symgen, &collapsed_symgen);
        }
    }
}