dotscope 0.6.0

A high-performance, cross-platform framework for analyzing and reverse engineering .NET PE executables
Documentation
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
//! Central type registry for .NET assembly analysis.
//!
//! This module provides the `TypeRegistry`, a thread-safe, high-performance registry for managing
//! all types within a .NET assembly. It serves as the central hub for type lookup,
//! storage, and cross-reference resolution during metadata analysis.
//!
//! # Key Components
//!
//! - [`TypeRegistry`] - Central registry managing all types in an assembly
//! - [`TypeSource`] - Classification of type origins (current module, external assemblies, etc.)
//! - `SourceRegistry` - Internal management of external type references
//!
//! # Registry Architecture
//!
//! The type registry uses a multi-index approach for efficient type lookup:
//!
//! - **Token-based lookup**: Primary index using metadata tokens
//! - **Name-based lookup**: Secondary indices for full names, simple names, and namespaces
//! - **Source-based lookup**: Types grouped by their origin (assembly, module, etc.)
//! - **Signature cache**: Deduplication using type signature hashes
//!
//! # Thread Safety
//!
//! The registry is designed for high-concurrency scenarios:
//! - Lock-free data structures for primary storage (`SkipMap`)
//! - Concurrent hash maps for indices (`DashMap`)
//! - Atomic operations for token generation
//! - No blocking operations during normal lookup/insertion
//!
//! # Type Sources
//!
//! Types in the registry can originate from various sources:
//! - **Current Module**: Types defined in the assembly being analyzed
//! - **External Assemblies**: Types from referenced assemblies
//! - **Primitive Types**: Built-in CLR types (System.Int32, System.String, etc.)
//! - **External Modules**: Types from module references
//! - **Files**: Types from file references
//!
//! # Examples
//!
//! ## Creating and Using a Registry
//!
//! ```rust,no_run
//! use dotscope::metadata::typesystem::{TypeRegistry, CilType};
//! use dotscope::metadata::identity::AssemblyIdentity;
//! use dotscope::metadata::token::Token;
//!
//! // Create a new registry with primitive types
//! let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
//! let registry = TypeRegistry::new(test_identity)?;
//!
//! // Look up types by name
//! if let Some(string_type) = registry.get_by_fullname("System.String", true) {
//!     println!("Found String type: 0x{:08X}", string_type.token.value());
//! }
//!
//! // Look up by token
//! if let Some(type_def) = registry.get(&Token::new(0x02000001)) {
//!     println!("Type: {}.{}", type_def.namespace, type_def.name);
//! }
//! # Ok::<(), dotscope::Error>(())
//! ```
//!
//! ## Registering New Types
//!
//! ```rust,no_run
//! use dotscope::metadata::typesystem::{TypeRegistry, CilType, TypeSource};
//! use dotscope::metadata::identity::AssemblyIdentity;
//! use dotscope::metadata::token::Token;
//! use std::sync::Arc;
//!
//! # fn example() -> dotscope::Result<()> {
//! let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
//! let registry = TypeRegistry::new(test_identity)?;
//!
//! // Create a new type
//! let new_type = CilType::new(
//!     Token::new(0x02000001),
//!     "MyNamespace".to_string(),
//!     "MyClass".to_string(),
//!     None, // No external reference
//!     None, // No base type yet
//!     0x00100001, // Public class
//!     Arc::new(boxcar::Vec::new()), // Empty fields
//!     Arc::new(boxcar::Vec::new()), // Empty methods
//!     None, // Flavor will be computed
//! );
//!
//! // Register the type
//! registry.insert(&Arc::new(new_type));
//! # Ok(())
//! # }
//! ```
//!
//! ## Type Lookup Patterns
//!
//! The registry provides multiple lookup methods by name, namespace, and token.
//! Each method returns the appropriate collection type for the query.
//!
//! # ECMA-335 Compliance
//!
//! The registry handles all type reference mechanisms defined in ECMA-335:
//! - `TypeDef`, `TypeRef`, and `TypeSpec` tokens
//! - Assembly, Module, and File references
//! - Generic type instantiations
//! - Cross-assembly type resolution

use std::sync::{
    atomic::{AtomicU32, Ordering},
    Arc,
};

use crossbeam_skiplist::SkipMap;
use dashmap::DashMap;

use crate::{
    metadata::{
        identity::AssemblyIdentity,
        signatures::{SignatureMethodSpec, TypeSignature},
        tables::{AssemblyRefRc, FileRc, MethodSpec, ModuleRc, ModuleRefRc, TableId},
        token::Token,
        typesystem::{
            CilFlavor, CilPrimitive, CilPrimitiveKind, CilType, CilTypeRc, CilTypeRef,
            CilTypeReference, PointerSize, TypeSignatureHash,
        },
    },
    Error::TypeNotFound,
    Result,
};

/// Complete type specification for type construction
///
/// This structure contains all the information needed to create a type with full
/// structural identity, enabling proper construction of complex types like generic
/// instances, arrays, and other constructed types.
#[derive(Clone)]
pub struct CompleteTypeSpec {
    /// Optional specific token to assign (None for auto-generation)
    pub token_init: Option<Token>,
    /// The CIL flavor/kind of the type
    pub flavor: CilFlavor,
    /// Type namespace
    pub namespace: String,
    /// Type name
    pub name: String,
    /// Source context (assembly, module, etc.)
    pub source: TypeSource,
    /// Generic arguments for generic instances
    pub generic_args: Option<Vec<CilTypeRc>>,
    /// Base type for derived types
    pub base_type: Option<CilTypeRc>,
    /// TypeAttributes flags (optional, inherited from base type for generic instances)
    pub flags: Option<u32>,
}

impl CompleteTypeSpec {
    /// Check if this specification matches an existing CilType for validation
    ///
    /// This method performs comprehensive structural comparison to determine if an
    /// existing type matches what this specification describes. This is used
    /// for validation and consistency checking during type construction.
    ///
    /// # Comparison Criteria
    /// Types are considered equivalent if they have identical:
    /// - **Basic identity**: Namespace, name, and flavor
    /// - **Source context**: Must originate from the same assembly/module/file  
    /// - **Generic arguments**: Type arguments must be identical (for generic instances)
    /// - **Base type**: Inheritance hierarchy must match (for derived types)
    ///
    /// # Arguments
    /// * `existing_type` - The existing CilType to compare against
    ///
    /// # Returns
    /// `true` if the existing type matches this specification exactly
    pub fn matches(&self, existing_type: &CilType) -> bool {
        // Basic identity check first for performance
        if existing_type.namespace != self.namespace
            || existing_type.name != self.name
            || *existing_type.flavor() != self.flavor
        {
            return false;
        }

        // Check source equivalence
        if !self.source_matches(existing_type) {
            return false;
        }

        // Check base type equivalence
        if !self.base_type_matches(existing_type) {
            return false;
        }

        // Check generic arguments equivalence
        self.generic_args_match(existing_type)
    }

    /// Check if type sources are equivalent
    fn source_matches(&self, existing_type: &CilType) -> bool {
        let ext_ref = existing_type.get_external();

        match (&self.source, ext_ref) {
            (TypeSource::Assembly(_), None) => true,
            (TypeSource::Assembly(_), Some(_)) => false, // Local vs external
            (src, Some(ext_ref)) => {
                match (ext_ref, src) {
                    (CilTypeReference::AssemblyRef(ar), TypeSource::AssemblyRef(tok)) => {
                        ar.token == *tok
                    }
                    (CilTypeReference::ModuleRef(mr), TypeSource::ModuleRef(tok)) => {
                        mr.token == *tok
                    }
                    (CilTypeReference::File(f), TypeSource::File(tok)) => f.token == *tok,
                    _ => true, // Allow different external source types for now
                }
            }
            _ => false, // External vs local
        }
    }

    /// Check if base types match
    fn base_type_matches(&self, existing_type: &CilType) -> bool {
        match (&self.base_type, existing_type.base.get()) {
            (Some(spec_base), Some(type_base)) => {
                match type_base.upgrade() {
                    Some(base_type) => {
                        spec_base.token == base_type.token
                            || spec_base.is_structurally_equivalent(&base_type)
                    }
                    None => false, // Base type reference is dropped
                }
            }
            (None, None) => true, // Both have no base type
            _ => false,           // One has base, one doesn't
        }
    }

    /// Check if generic arguments match
    fn generic_args_match(&self, existing_type: &CilType) -> bool {
        match &self.generic_args {
            Some(spec_args) => {
                // Must have same number of generic arguments
                if spec_args.len() != existing_type.generic_args.count() {
                    return false;
                }

                // Compare each generic argument by token
                for (i, spec_arg) in spec_args.iter().enumerate() {
                    if let Some(type_arg) = existing_type.generic_args.get(i) {
                        if spec_arg.token != type_arg.token {
                            return false;
                        }
                    } else {
                        return false;
                    }
                }
                true
            }
            None => existing_type.generic_args.count() == 0, // No generic args in spec
        }
    }
}

/// Classification of type origins within the .NET assembly ecosystem.
///
/// `TypeSource` identifies where a type is defined, enabling proper resolution
/// of cross-assembly and cross-module type references. This is crucial for
/// handling external dependencies and maintaining proper type identity.
///
/// # Type Resolution
///
/// Different sources require different resolution strategies:
/// - **`CurrentModule`**: Direct access to type definition
/// - **External sources**: Resolution through metadata references
/// - **Primitive**: Built-in CLR types with artificial tokens
///
/// # Examples
///
/// ```rust,no_run
/// use dotscope::metadata::typesystem::TypeSource;
/// use dotscope::metadata::token::Token;
///
/// // Local type
/// let local_source = TypeSource::Unknown;
///
/// // External assembly type
/// let external_source = TypeSource::AssemblyRef(Token::new(0x23000001));
///
/// // Primitive type
/// let primitive_source = TypeSource::Primitive;
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TypeSource {
    /// Type is defined in a specific assembly (by identity)
    Assembly(AssemblyIdentity),
    /// Type is defined in an external module (cross-module reference)
    Module(Token),
    /// Type is defined in an external module reference
    ModuleRef(Token),
    /// Type is defined in an external assembly reference
    AssemblyRef(Token),
    /// Type is defined in an external file reference
    File(Token),
    /// Type is a primitive defined by the CLR runtime
    Primitive,
    /// Type source is not determined or not available
    Unknown,
}

impl TypeSource {
    /// Returns `true` if this type source refers to an external reference.
    ///
    /// External sources include module references, assembly references, and file references.
    /// Local sources (current assembly) and special sources (primitive, unknown) return `false`.
    #[must_use]
    pub fn is_external(&self) -> bool {
        matches!(
            self,
            TypeSource::ModuleRef(_) | TypeSource::AssemblyRef(_) | TypeSource::File(_)
        )
    }

    /// Returns `true` if this is a primitive type source.
    #[must_use]
    pub fn is_primitive(&self) -> bool {
        matches!(self, TypeSource::Primitive)
    }

    /// Returns `true` if this source is unknown/undetermined.
    #[must_use]
    pub fn is_unknown(&self) -> bool {
        matches!(self, TypeSource::Unknown)
    }

    /// Returns the associated token if this source has one.
    ///
    /// Returns `Some(Token)` for Module, ModuleRef, AssemblyRef, and File variants.
    /// Returns `None` for Assembly, Primitive, and Unknown variants.
    #[must_use]
    pub fn token(&self) -> Option<Token> {
        match self {
            TypeSource::Module(t)
            | TypeSource::ModuleRef(t)
            | TypeSource::AssemblyRef(t)
            | TypeSource::File(t) => Some(*t),
            TypeSource::Assembly(_) | TypeSource::Primitive | TypeSource::Unknown => None,
        }
    }
}

/// Internal registry for tracking external type reference sources.
///
/// `SourceRegistry` maintains weak references to external assemblies, modules,
/// and files to prevent circular reference cycles while enabling proper type
/// resolution. It serves as a lookup table for converting `TypeSource` values
/// back to their corresponding metadata references.
///
/// # Memory Management
///
/// The registry uses reference counting to track external sources without
/// creating strong circular references that could prevent garbage collection.
/// When sources are no longer needed, they can be automatically cleaned up.
///
/// # Thread Safety
///
/// All internal collections use `DashMap` for lock-free concurrent access,
/// making source registration and lookup safe from multiple threads.
struct SourceRegistry {
    /// External modules indexed by their metadata tokens
    modules: DashMap<Token, ModuleRc>,
    /// Module references indexed by their metadata tokens
    module_refs: DashMap<Token, ModuleRefRc>,
    /// Assembly references indexed by their metadata tokens
    assembly_refs: DashMap<Token, AssemblyRefRc>,
    /// File references indexed by their metadata tokens
    files: DashMap<Token, FileRc>,
}

impl SourceRegistry {
    /// Create a new empty source registry.
    ///
    /// Initializes all internal collections as empty, ready to receive
    /// source registrations during metadata loading.
    ///
    /// # Returns
    /// A new `SourceRegistry` with empty collections
    fn new() -> Self {
        SourceRegistry {
            modules: DashMap::new(),
            module_refs: DashMap::new(),
            assembly_refs: DashMap::new(),
            files: DashMap::new(),
        }
    }

    /// Register an external type reference source.
    ///
    /// Stores the external reference and returns a corresponding `TypeSource`
    /// value that can be used for efficient lookups. This method handles all
    /// supported external reference types defined in ECMA-335.
    ///
    /// # Arguments
    /// * `source` - The external type reference to register
    ///
    /// # Returns
    /// A `TypeSource` value for efficient source identification
    ///
    /// # Thread Safety
    ///
    /// This method is thread-safe and can be called concurrently from
    /// multiple threads during metadata loading.
    fn register_source(&self, source: &CilTypeReference) -> TypeSource {
        match source {
            CilTypeReference::Module(module) => {
                self.modules.insert(module.token, module.clone());
                TypeSource::Module(module.token)
            }
            CilTypeReference::ModuleRef(module_ref) => {
                self.module_refs
                    .insert(module_ref.token, module_ref.clone());
                TypeSource::ModuleRef(module_ref.token)
            }
            CilTypeReference::AssemblyRef(assembly_ref) => {
                self.assembly_refs
                    .insert(assembly_ref.token, assembly_ref.clone());
                TypeSource::AssemblyRef(assembly_ref.token)
            }
            CilTypeReference::Assembly(assembly) => {
                TypeSource::Assembly(AssemblyIdentity::from_assembly(assembly))
            }
            CilTypeReference::File(file) => {
                self.files.insert(file.token, file.clone());
                TypeSource::File(file.token)
            }
            _ => TypeSource::Unknown,
        }
    }

    /// Retrieve a type reference from a registered source.
    ///
    /// Converts a `TypeSource` back to its corresponding `CilTypeReference`,
    /// enabling resolution of external type references during analysis.
    ///
    /// # Arguments
    /// * `source` - The type source to look up
    ///
    /// # Returns
    /// * `Some(CilTypeReference)` - The corresponding external reference
    /// * `None` - If source is not external or not found
    ///
    /// # Thread Safety
    ///
    /// This method is thread-safe and lock-free for concurrent access.
    fn get_source(&self, source: &TypeSource) -> Option<CilTypeReference> {
        match source {
            TypeSource::Module(token) => self
                .modules
                .get(token)
                .map(|module| CilTypeReference::Module(module.clone())),
            TypeSource::ModuleRef(token) => self
                .module_refs
                .get(token)
                .map(|moduleref| CilTypeReference::ModuleRef(moduleref.clone())),
            TypeSource::AssemblyRef(token) => self
                .assembly_refs
                .get(token)
                .map(|assemblyref| CilTypeReference::AssemblyRef(assemblyref.clone())),
            TypeSource::File(token) => self
                .files
                .get(token)
                .map(|file| CilTypeReference::File(file.clone())),
            TypeSource::Primitive | TypeSource::Unknown | TypeSource::Assembly(_) => None,
        }
    }
}

/// Central registry for managing all types within a .NET assembly.
///
/// `TypeRegistry` provides thread-safe, high-performance storage and lookup
/// capabilities for all types encountered during metadata analysis. It serves
/// as the authoritative source for type information and handles storage,
/// cross-references, and efficient query operations.
///
/// # Architecture
///
/// The registry uses a multi-layered indexing strategy:
/// - **Primary storage**: Token-based skip list for O(log n) lookups
/// - **Secondary indices**: Hash maps for name-based and source-based queries
/// - **Deduplication**: Signature cache to prevent duplicate type entries
/// - **External references**: Source registry for cross-assembly resolution
///
/// # Concurrency Design
///
/// All operations are designed for high-concurrency scenarios:
/// - Lock-free primary storage using `SkipMap`
/// - Concurrent secondary indices using `DashMap`
/// - Atomic token generation for thread-safe registration
/// - No blocking operations during normal operations
///
/// # Type Identity
///
/// Types are identified using multiple strategies:
/// - **Token identity**: Primary key using metadata tokens
/// - **Name identity**: Full namespace.name qualification
/// - **Source identity**: Origin-based grouping
///
/// # Memory Management
///
/// The registry uses reference counting (`Arc`) to manage type lifetime:
/// - Types can be shared across multiple consumers
/// - Automatic cleanup when no longer referenced
/// - Efficient memory usage through reference counting
///
/// # Examples
///
/// ## Basic Registry Operations
///
/// ```rust,no_run
/// use dotscope::metadata::typesystem::TypeRegistry;
/// use dotscope::metadata::identity::AssemblyIdentity;
///
/// // Create registry with primitive types
/// let identity = AssemblyIdentity::parse("Test, Version=1.0.0.0").unwrap();
/// let registry = TypeRegistry::new(identity)?;
///
/// // Query primitive types
/// if let Some(entry) = registry.get_by_fullname("System.Int32", true) {
///     println!("Found Int32: 0x{:08X}", entry.token.value());
/// }
///
/// // Check registry statistics
/// println!("Total types: {}", registry.len());
/// # Ok::<(), dotscope::Error>(())
/// ```
///
/// # Thread Safety
///
/// The registry is fully thread-safe and optimized for concurrent access:
/// - Multiple threads can perform lookups simultaneously
/// - Registration operations are atomic and consistent
/// - No explicit locking required by consumers
///
/// # Performance Characteristics
///
/// - **Token lookup**: O(log n) using skip list
/// - **Name lookup**: O(1) average using hash indices  
/// - **Registration**: O(log n) + O(1) for indexing
/// - **Memory**: O(n) with reference counting efficiency
pub struct TypeRegistry {
    /// Primary type storage indexed by metadata tokens - uses skip list for O(log n) operations
    types: SkipMap<Token, CilTypeRc>,
    /// Atomic counter for generating unique artificial tokens for new types
    next_token: AtomicU32,
    /// Registry managing external assembly/module/file references
    sources: SourceRegistry,
    /// Identity of the assembly this registry represents
    current_assembly: AssemblyIdentity,
    /// Secondary index: types grouped by their origin source
    types_by_source: DashMap<TypeSource, Vec<Token>>,
    /// Secondary index: types indexed by full name (namespace.name)
    types_by_fullname: DashMap<String, Vec<Token>>,
    /// Secondary index: types indexed by simple name (may have duplicates)
    types_by_name: DashMap<String, Vec<Token>>,
    /// Secondary index: types grouped by namespace
    types_by_namespace: DashMap<String, Vec<Token>>,
    /// Registered external TypeRegistries for cross-assembly type resolution
    /// Maps AssemblyIdentity to external TypeRegistry for cross-assembly lookups
    external_registries: DashMap<AssemblyIdentity, Arc<TypeRegistry>>,
}

impl TypeRegistry {
    /// Create a new type registry with initialized primitive types.
    ///
    /// Constructs a complete type registry with all .NET primitive types
    /// pre-registered and ready for use. The registry starts with artificial
    /// tokens in the `0xF000_0020`+ range for new type registration.
    ///
    /// # Primitive Types
    ///
    /// The following primitive types are automatically registered:
    /// - `System.Void`, `System.Boolean`, `System.Char`
    /// - Integer types: `SByte`, `Byte`, `Int16`, `UInt16`, `Int32`, `UInt32`, `Int64`, `UInt64`
    /// - Floating point: `Single`, `Double`
    /// - Platform types: `IntPtr`, `UIntPtr`
    /// - Reference types: `Object`, `String`
    /// - Special types: `TypedReference`, `ValueType`
    ///
    /// # Returns
    /// * `Ok(TypeRegistry)` - Fully initialized registry with primitive types
    /// * `Err(Error)` - If primitive type initialization fails
    ///
    /// # Errors
    ///
    /// This function will return an error if the primitive type initialization fails,
    /// which could happen due to internal inconsistencies during registry setup.
    ///
    /// # Thread Safety
    ///
    /// The returned registry is fully thread-safe and ready for concurrent use.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use dotscope::metadata::typesystem::TypeRegistry;
    /// use dotscope::metadata::identity::AssemblyIdentity;
    ///
    /// let identity = AssemblyIdentity::parse("Test, Version=1.0.0.0").unwrap();
    /// let registry = TypeRegistry::new(identity)?;
    ///
    /// // Primitive types are immediately available
    /// let string_type = registry.get_by_fullname("System.String", true);
    /// assert!(string_type.is_some());
    /// # Ok::<(), dotscope::Error>(())
    /// ```
    pub fn new(assembly_identity: AssemblyIdentity) -> Result<Self> {
        let registry = TypeRegistry {
            types: SkipMap::new(),
            next_token: AtomicU32::new(0xF000_0020), // Start after reserved primitives
            sources: SourceRegistry::new(),
            current_assembly: assembly_identity,
            types_by_source: DashMap::new(),
            types_by_fullname: DashMap::new(),
            types_by_name: DashMap::new(),
            types_by_namespace: DashMap::new(),
            external_registries: DashMap::new(),
        };

        registry.initialize_primitives()?;
        Ok(registry)
    }

    /// Get the current assembly identity.
    pub fn current_assembly(&self) -> AssemblyIdentity {
        self.current_assembly.clone()
    }

    /// Get the TypeSource for the current assembly.
    pub fn current_assembly_source(&self) -> TypeSource {
        TypeSource::Assembly(self.current_assembly.clone())
    }

    /// Get the next available token and increment the counter
    fn next_token(&self) -> Token {
        let next_token = self.next_token.fetch_add(1, Ordering::Relaxed);
        if next_token == 0xFFFF_FFFF {
            // We're out of tokens - this should never happen in practice
            debug_assert!(
                false,
                "We ran out of tokens and are going overwrite existing ones"
            );
            self.next_token.store(0xF100_0000, Ordering::Relaxed);
        }

        Token::new(next_token)
    }

    /// Initialize primitive types in the registry
    fn initialize_primitives(&self) -> Result<()> {
        for primitive in [
            CilPrimitive::new(CilPrimitiveKind::Void),
            CilPrimitive::new(CilPrimitiveKind::Boolean),
            CilPrimitive::new(CilPrimitiveKind::Char),
            CilPrimitive::new(CilPrimitiveKind::I1),
            CilPrimitive::new(CilPrimitiveKind::U1),
            CilPrimitive::new(CilPrimitiveKind::I2),
            CilPrimitive::new(CilPrimitiveKind::U2),
            CilPrimitive::new(CilPrimitiveKind::I4),
            CilPrimitive::new(CilPrimitiveKind::U4),
            CilPrimitive::new(CilPrimitiveKind::I8),
            CilPrimitive::new(CilPrimitiveKind::U8),
            CilPrimitive::new(CilPrimitiveKind::R4),
            CilPrimitive::new(CilPrimitiveKind::R8),
            CilPrimitive::new(CilPrimitiveKind::I),
            CilPrimitive::new(CilPrimitiveKind::U),
            CilPrimitive::new(CilPrimitiveKind::Object),
            CilPrimitive::new(CilPrimitiveKind::String),
            CilPrimitive::new(CilPrimitiveKind::TypedReference),
            CilPrimitive::new(CilPrimitiveKind::ValueType),
            CilPrimitive::new(CilPrimitiveKind::Var),
            CilPrimitive::new(CilPrimitiveKind::MVar),
            CilPrimitive::new(CilPrimitiveKind::Null),
        ] {
            let token = primitive.token();
            let flavor = primitive.to_flavor();

            let new_type = Arc::new(CilType::new(
                token,
                primitive.namespace().to_string(),
                primitive.name().to_string(),
                None,
                None,
                0,
                Arc::new(boxcar::Vec::new()),
                Arc::new(boxcar::Vec::new()),
                Some(flavor),
            ));

            self.register_type_internal(&new_type, TypeSource::Primitive);
        }

        // Set up base type relationships
        let object_token = CilPrimitive::new(CilPrimitiveKind::Object).token();
        let value_type_token = CilPrimitive::new(CilPrimitiveKind::ValueType).token();

        // All value types extend System.ValueType
        for primitive in [
            CilPrimitive::new(CilPrimitiveKind::Void),
            CilPrimitive::new(CilPrimitiveKind::Boolean),
            CilPrimitive::new(CilPrimitiveKind::Char),
            CilPrimitive::new(CilPrimitiveKind::I1),
            CilPrimitive::new(CilPrimitiveKind::U1),
            CilPrimitive::new(CilPrimitiveKind::I2),
            CilPrimitive::new(CilPrimitiveKind::U2),
            CilPrimitive::new(CilPrimitiveKind::I4),
            CilPrimitive::new(CilPrimitiveKind::U4),
            CilPrimitive::new(CilPrimitiveKind::I8),
            CilPrimitive::new(CilPrimitiveKind::U8),
            CilPrimitive::new(CilPrimitiveKind::R4),
            CilPrimitive::new(CilPrimitiveKind::R8),
            CilPrimitive::new(CilPrimitiveKind::I),
            CilPrimitive::new(CilPrimitiveKind::U),
        ] {
            let type_token = primitive.token();
            if let (Some(type_rc), Some(value_type_rc)) = (
                self.types.get(&type_token),
                self.types.get(&value_type_token),
            ) {
                type_rc
                    .value()
                    .base
                    .set(value_type_rc.value().clone().into())
                    .map_err(|_| malformed_error!("Type base already set"))?;
            }
        }

        // System.ValueType itself extends System.Object
        if let (Some(value_type_rc), Some(object_rc)) = (
            self.types.get(&value_type_token),
            self.types.get(&object_token),
        ) {
            value_type_rc
                .value()
                .base
                .set(object_rc.value().clone().into())
                .map_err(|_| malformed_error!("ValueType base already set"))?;
        }

        // System.String extends System.Object
        if let (Some(string_rc), Some(object_rc)) = (
            self.types
                .get(&CilPrimitive::new(CilPrimitiveKind::String).token()),
            self.types.get(&object_token),
        ) {
            string_rc
                .value()
                .base
                .set(object_rc.value().clone().into())
                .map_err(|_| malformed_error!("String base already set"))?;
        }

        Ok(())
    }

    /// Register a new type in all the lookup tables
    ///
    /// ## Arguments
    /// * `type_rc`     - The type instance
    /// * `source`      - The the source of the type
    fn register_type_internal(&self, type_rc: &CilTypeRc, source: TypeSource) {
        let token = type_rc.token;
        if self.types.contains_key(&token) {
            return;
        }

        self.types.insert(token, type_rc.clone());

        self.types_by_source
            .entry(source)
            .or_default()
            .push(type_rc.token);

        if !type_rc.namespace.is_empty() {
            self.types_by_namespace
                .entry(type_rc.namespace.clone())
                .or_default()
                .push(type_rc.token);
        }

        self.types_by_name
            .entry(type_rc.name.clone())
            .or_default()
            .push(type_rc.token);

        self.types_by_fullname
            .entry(type_rc.fullname())
            .or_default()
            .push(type_rc.token);
    }

    /// Insert a `CilType` into the registry
    ///
    /// ## Arguments
    /// * '`new_type`' - The type to register
    pub fn insert(&self, new_type: &CilTypeRc) {
        let source = match new_type.get_external() {
            Some(external_source) => self.register_source(external_source),
            None => TypeSource::Assembly(self.current_assembly.clone()),
        };

        self.register_type_internal(new_type, source);
    }

    /// Create a new empty type with the next available token
    ///
    /// # Errors
    /// Returns an error if the type cannot be created or inserted into the registry.
    pub fn create_type_empty(&self) -> Result<CilTypeRc> {
        let token = self.next_token();

        let new_type = Arc::new(CilType::new(
            token,
            String::new(),
            String::new(),
            None,
            None,
            0,
            Arc::new(boxcar::Vec::new()),
            Arc::new(boxcar::Vec::new()),
            None,
        ));

        self.types.insert(token, new_type.clone());
        Ok(new_type)
    }

    /// Create a new type with a specific flavor
    ///
    /// ## Arguments
    /// * 'flavor' - The flavor to set for the new type
    ///
    /// # Errors
    /// Returns an error if the type cannot be created or inserted into the registry.
    pub fn create_type_with_flavor(&self, flavor: CilFlavor) -> Result<CilTypeRc> {
        let token = self.next_token();

        let new_type = Arc::new(CilType::new(
            token,
            String::new(),
            String::new(),
            None,
            None,
            0,
            Arc::new(boxcar::Vec::new()),
            Arc::new(boxcar::Vec::new()),
            Some(flavor),
        ));

        self.types.insert(token, new_type.clone());
        Ok(new_type)
    }

    /// Get a primitive type by its `CilPrimitive` enum value
    ///
    /// ## Arguments
    /// * 'primitive' - The kind of primitive to look up
    ///
    /// # Errors
    /// Returns an error if the primitive type is not found in the registry.
    pub fn get_primitive(&self, primitive: CilPrimitiveKind) -> Result<CilTypeRc> {
        match self.types.get(&primitive.token()) {
            Some(res) => Ok(res.value().clone()),
            None => Err(TypeNotFound(primitive.token())),
        }
    }

    /// Look up a type by its metadata token.
    ///
    /// Performs the primary lookup operation using the token-based index.
    /// This is the most efficient lookup method with O(log n) complexity.
    ///
    /// # Arguments
    /// * `token` - The metadata token to look up
    ///
    /// # Returns
    /// * `Some(CilTypeRc)` - The type if found
    /// * `None` - If no type exists with the given token
    ///
    /// # Thread Safety
    ///
    /// This method is thread-safe and lock-free for concurrent access.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use dotscope::metadata::{typesystem::TypeRegistry, token::Token};
    ///
    /// # fn example(registry: &TypeRegistry) {
    /// if let Some(type_def) = registry.get(&Token::new(0x02000001)) {
    ///     println!("Found type: {}.{}", type_def.namespace, type_def.name);
    /// }
    /// # }
    /// ```
    pub fn get(&self, token: &Token) -> Option<CilTypeRc> {
        self.types.get(token).map(|entry| entry.value().clone())
    }

    /// Returns the `<Module>` type which contains global fields and methods.
    ///
    /// The `<Module>` type is always TypeDef RID 1 in .NET assemblies. It contains
    /// global methods, the module static constructor (.cctor), and nested types
    /// injected by obfuscators.
    ///
    /// # Returns
    ///
    /// * `Some(CilTypeRc)` - The module type
    /// * `None` - If the module type doesn't exist (malformed assembly)
    #[must_use]
    pub fn module_type(&self) -> Option<CilTypeRc> {
        self.get(&Token::new(0x0200_0001))
    }

    /// Returns the token of the module static constructor (.cctor) if it exists.
    ///
    /// The module `.cctor` is defined on the `<Module>` type (TypeDef RID 1).
    /// This method is commonly used to find initialization code that needs
    /// to be analyzed or neutralized during deobfuscation.
    ///
    /// # Returns
    ///
    /// * `Some(Token)` - The token of the module `.cctor` method
    /// * `None` - If the module type doesn't exist or has no `.cctor`
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use dotscope::metadata::typesystem::TypeRegistry;
    ///
    /// # fn example(registry: &TypeRegistry) {
    /// if let Some(cctor_token) = registry.module_cctor() {
    ///     println!("Module .cctor found: {:?}", cctor_token);
    /// }
    /// # }
    /// ```
    #[must_use]
    pub fn module_cctor(&self) -> Option<Token> {
        self.module_type().and_then(|module_type| {
            module_type.methods.iter().find_map(|(_, method_ref)| {
                method_ref
                    .upgrade()
                    .and_then(|m| if m.is_cctor() { Some(m.token) } else { None })
            })
        })
    }

    /// Look up a type by its source and qualified name.
    ///
    /// Performs a targeted lookup for types from a specific source with
    /// exact namespace and name matching. This is useful for resolving
    /// external type references where the source is known.
    ///
    /// # Arguments
    /// * `source` - The origin source of the type
    /// * `namespace` - The namespace of the type (can be empty)
    /// * `name` - The exact name of the type
    ///
    /// # Returns
    /// * `Some(CilTypeRc)` - The first matching type from the specified source
    /// * `None` - If no matching type is found in the source
    ///
    /// # Performance
    ///
    /// This method combines source filtering with name lookup for efficient
    /// resolution of external type references.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use dotscope::metadata::typesystem::{TypeRegistry, TypeSource};
    /// use dotscope::metadata::token::Token;
    ///
    /// # fn example(registry: &TypeRegistry) {
    /// let external_source = TypeSource::AssemblyRef(Token::new(0x23000001));
    /// if let Some(type_def) = registry.get_by_source_and_name(
    ///     &external_source,
    ///     "System",
    ///     "String"
    /// ) {
    ///     println!("Found external String type");
    /// }
    /// # }
    /// ```
    pub fn get_by_source_and_name(
        &self,
        source: &TypeSource,
        namespace: &str,
        name: &str,
    ) -> Option<CilTypeRc> {
        let fullname = if namespace.is_empty() {
            name.to_string()
        } else {
            format!("{namespace}.{name}")
        };

        if let Some(tokens) = self.types_by_source.get(source) {
            for &token in tokens.value() {
                if let Some(type_rc) = self.types.get(&token) {
                    if type_rc.value().namespace == namespace && type_rc.value().name == name {
                        return Some(type_rc.value().clone());
                    }
                }
            }
        }

        if let Some(tokens) = self.types_by_fullname.get(&fullname) {
            if let Some(&token) = tokens.first() {
                return self.types.get(&token).map(|res| res.value().clone());
            }
        }

        None
    }

    /// Get all types within a specific namespace.
    ///
    /// Returns all types that belong to the specified namespace, regardless
    /// of their source or other characteristics. This is useful for namespace
    /// exploration and type discovery operations.
    ///
    /// # Arguments
    /// * `namespace` - The namespace to search for (case-sensitive)
    ///
    /// # Returns
    /// A vector of all types in the specified namespace. The vector may be
    /// empty if no types exist in the namespace.
    ///
    /// # Performance
    ///
    /// This operation is O(1) for namespace lookup plus O(n) for type
    /// resolution where n is the number of types in the namespace.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use dotscope::metadata::typesystem::TypeRegistry;
    ///
    /// # fn example(registry: &TypeRegistry) {
    /// // Get all System types
    /// let system_types = registry.get_by_namespace("System");
    /// for type_def in system_types {
    ///     println!("System type: {}", type_def.name);
    /// }
    ///
    /// // Get types in global namespace
    /// let global_types = registry.get_by_namespace("");
    /// # }
    /// ```
    pub fn get_by_namespace(&self, namespace: &str) -> Vec<CilTypeRc> {
        if let Some(tokens) = self.types_by_namespace.get(namespace) {
            tokens
                .iter()
                .filter_map(|token| self.types.get(token).map(|entry| entry.value().clone()))
                .collect()
        } else {
            Vec::new()
        }
    }

    /// Get all types with a specific simple name across all namespaces.
    ///
    /// Returns all types that have the specified name, regardless of their
    /// namespace. This can return multiple types if the same name exists
    /// in different namespaces (e.g., multiple "List" types).
    ///
    /// # Arguments
    /// * `name` - The simple name to search for (case-sensitive)
    ///
    /// # Returns
    /// A vector of all types with the specified name. Types from different
    /// namespaces will be included. The vector may be empty if no types
    /// with the name exist.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use dotscope::metadata::typesystem::TypeRegistry;
    ///
    /// # fn example(registry: &TypeRegistry) {
    /// // Find all "List" types (may find System.Collections.List,
    /// // System.Collections.Generic.List, custom List types, etc.)
    /// let list_types = registry.get_by_name("List");
    /// for type_def in list_types {
    ///     println!("List type: {}.{}", type_def.namespace, type_def.name);
    /// }
    /// # }
    /// ```
    pub fn get_by_name(&self, name: &str) -> Vec<CilTypeRc> {
        if let Some(tokens) = self.types_by_name.get(name) {
            tokens
                .iter()
                .filter_map(|token| self.types.get(token).map(|entry| entry.value().clone()))
                .collect()
        } else {
            Vec::new()
        }
    }

    /// Get types by their fully qualified name (namespace.name).
    ///
    /// Returns all types that exactly match the specified fully qualified name.
    /// This is the most precise name-based lookup method and typically returns
    /// at most one type (unless there are duplicate definitions).
    ///
    /// # Arguments
    /// * `fullname` - The fully qualified name in "namespace.name" format
    /// * `external` - Whether to search external registries if not found locally
    ///
    /// # Returns
    /// * `Some(CilTypeRc)` - The first TypeDef found
    /// * `None` - If no TypeDef with the name is found
    ///
    /// # Name Format
    ///
    /// The fullname should be in the format:
    /// - "Namespace.TypeName" for namespaced types
    /// - "`TypeName`" for types in the global namespace
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use dotscope::metadata::typesystem::TypeRegistry;
    ///
    /// # fn example(registry: &TypeRegistry) {
    /// // Find the specific System.String type (false = local only)
    /// if let Some(string_type) = registry.get_by_fullname("System.String", false) {
    ///     println!("Found System.String: 0x{:08X}", string_type.token.value());
    /// }
    ///
    /// // Find including external types
    /// let global_type = registry.get_by_fullname("GlobalType", true);
    /// # }
    /// ```
    pub fn get_by_fullname(&self, fullname: &str, external: bool) -> Option<CilTypeRc> {
        if let Some(tokens) = self.types_by_fullname.get(fullname) {
            for token in tokens.value() {
                if let Some(entry) = self.types.get(token) {
                    let type_rc = entry.value().clone();
                    // Accept TypeDef (0x02), TypeSpec (0x1B), and artificial types (0xF0)
                    if type_rc.token.is_table(TableId::TypeDef)
                        || type_rc.token.is_table(TableId::TypeSpec)
                        || type_rc.token.table() == 0xF0
                    {
                        return Some(type_rc);
                    }
                }
            }
        } else {
            // Fallback: try suffix matching for nested types
            // This handles cases where TypeRef has incomplete name (e.g., "DynamicPartitionEnumerator_Abstract`2")
            // but TypeDef has complete name (e.g., "Partitioner/DynamicPartitionEnumerator_Abstract`2")
            let mut candidates = Vec::new();
            for key_entry in &self.types_by_fullname {
                let key = key_entry.key();
                let tokens = key_entry.value();

                // Check if this key ends with our target fullname (handling nested types)
                if key.ends_with(fullname) && key != fullname {
                    // Additional check: ensure it's a proper nested type match (contains '/')
                    if key.contains('/') {
                        // Check tokens for TypeDef or TypeSpec
                        for token in tokens {
                            if let Some(entry) = self.types.get(token) {
                                let type_rc = entry.value().clone();
                                if type_rc.token.is_table(TableId::TypeDef)
                                    || type_rc.token.is_table(TableId::TypeSpec)
                                {
                                    candidates.push(type_rc);
                                    break; // Take first TypeDef/TypeSpec found
                                }
                            }
                        }
                    }
                }
            }

            // Return first candidate (could be enhanced with disambiguation logic)
            if let Some(candidate) = candidates.first() {
                return Some(candidate.clone());
            }
        }

        if external {
            for external_registry_entry in &self.external_registries {
                let external_registry = external_registry_entry.value();
                if let Some(external_type) = external_registry.get_by_fullname(fullname, false) {
                    return Some(external_type);
                }
            }
        }
        None
    }

    /// Get all TypeDefs by fully qualified name.
    ///
    /// # Arguments
    /// * `fullname` - The fully qualified name in "namespace.name" format  
    /// * `include_external` - Whether to search external registries
    ///
    /// # Returns
    /// * `Vec<CilTypeRc>` - All TypeDefs found with the given name
    pub fn get_by_fullname_list(&self, fullname: &str, include_external: bool) -> Vec<CilTypeRc> {
        let mut typedef_matches = Vec::new();

        if let Some(tokens) = self.types_by_fullname.get(fullname) {
            for token in tokens.value() {
                if let Some(entry) = self.types.get(token) {
                    let type_rc = entry.value().clone();
                    if type_rc.token.table() == 0xF0
                        || (include_external && type_rc.token.is_table(TableId::TypeDef))
                    {
                        typedef_matches.push(type_rc);
                    }
                }
            }
        }

        if include_external {
            for external_registry_entry in &self.external_registries {
                let external_registry = external_registry_entry.value();
                let external_types = external_registry.get_by_fullname_list(fullname, false);
                typedef_matches.extend(external_types);
            }
        }

        typedef_matches
    }

    /// Gets the byte size of a field for FieldRVA initialization.
    ///
    /// This method looks up a field by its token and determines the size of its data
    /// based on the field's type signature. This is primarily used for pre-populating
    /// static fields from FieldRVA data in the PE file.
    ///
    /// # Arguments
    ///
    /// * `field_token` - The metadata token of the field (table 0x04)
    ///
    /// # Returns
    ///
    /// * `Some(size)` - For primitive types with known sizes (I1=1, I2=2, I4=4, I8=8, etc.)
    /// * `Some(class_size)` - For value types with explicit ClassLayout size
    /// * `None` - For arrays, reference types, or types without known sizes
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use dotscope::metadata::typesystem::{TypeRegistry, PointerSize};
    /// use dotscope::metadata::token::Token;
    ///
    /// # fn example(registry: &TypeRegistry) {
    /// let field_token = Token::new(0x04000001);
    /// if let Some(size) = registry.get_field_byte_size(&field_token, PointerSize::Bit64) {
    ///     println!("Field size: {} bytes", size);
    /// }
    /// # }
    /// ```
    #[must_use]
    pub fn get_field_byte_size(&self, field_token: &Token, ptr_size: PointerSize) -> Option<usize> {
        // Search through all types to find the field
        for entry in self {
            let type_ref = entry.value();
            // Check if this type owns the field
            for (_, field) in type_ref.fields.iter() {
                if field.token == *field_token {
                    // Found the field, get its type signature
                    let sig = &field.signature;

                    // First try to get size from primitive type
                    if let Some(size) = sig.base.byte_size(ptr_size) {
                        return Some(size);
                    }

                    // For value types, try to look up class size
                    if let TypeSignature::ValueType(vt_token) = &sig.base {
                        if let Some(value_type) = self.get(vt_token) {
                            if let Some(class_size) = value_type.class_size.get() {
                                if *class_size > 0 {
                                    return Some(*class_size as usize);
                                }
                            }
                        }
                    }

                    return None;
                }
            }
        }

        None
    }

    /// Gets the type signature of a field by its metadata token.
    ///
    /// Searches through all types to find the field with the given token
    /// and returns its type signature.
    ///
    /// # Arguments
    ///
    /// * `field_token` - The metadata token of the field to look up
    ///
    /// # Returns
    ///
    /// The field's type signature if found, `None` otherwise.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use dotscope::metadata::typesystem::TypeRegistry;
    /// use dotscope::metadata::token::Token;
    ///
    /// # fn example(registry: &TypeRegistry) {
    /// let field_token = Token::new(0x04000001);
    /// if let Some(sig) = registry.get_field_signature(&field_token) {
    ///     println!("Field type: {:?}", sig);
    /// }
    /// # }
    /// ```
    #[must_use]
    pub fn get_field_signature(&self, field_token: &Token) -> Option<TypeSignature> {
        // Search through all types to find the field
        for entry in self {
            let type_ref = entry.value();
            // Check if this type owns the field
            for (_, field) in type_ref.fields.iter() {
                if field.token == *field_token {
                    return Some(field.signature.base.clone());
                }
            }
        }

        None
    }

    /// Returns the index of a field within a type's field list.
    ///
    /// This is used for accessing fields in value types (structs) stored inline.
    /// The index corresponds to the position in `EmValue::ValueType::fields`.
    ///
    /// # Arguments
    ///
    /// * `type_token` - Token of the type containing the field.
    /// * `field_token` - Token of the field to find.
    ///
    /// # Returns
    ///
    /// The zero-based index of the field within the type's field list,
    /// or `None` if the field is not found in the type.
    ///
    /// # Example
    ///
    /// ```text
    /// // For struct CFGCtx { uint A; uint B; uint C; uint D; }
    /// // get_field_index_in_type(CFGCtx, CFGCtx::A) returns Some(0)
    /// // get_field_index_in_type(CFGCtx, CFGCtx::D) returns Some(3)
    /// ```
    #[must_use]
    pub fn get_field_index_in_type(
        &self,
        type_token: &Token,
        field_token: &Token,
    ) -> Option<usize> {
        // Get the type
        let type_entry = self.get(type_token)?;

        // Find the field index within the type's field list
        for (index, (_, field)) in type_entry.fields.iter().enumerate() {
            if field.token == *field_token {
                return Some(index);
            }
        }

        None
    }

    /// Register a source entity to enable resolving references to it
    ///
    /// ## Arguments
    /// * 'source' - The source of the type to register
    pub fn register_source(&self, source: &CilTypeReference) -> TypeSource {
        self.sources.register_source(source)
    }

    /// Get a source reference by its id
    ///
    /// ## Arguments
    /// * 'source' - The source of the type to look for
    pub fn get_source_reference(&self, source: &TypeSource) -> Option<CilTypeReference> {
        self.sources.get_source(source)
    }

    /// This method creates types with complete structural information upfront, enabling
    /// proper type construction that considers the full type identity including generic arguments,
    /// base types, and other distinguishing characteristics.
    ///
    /// ## Arguments
    /// * `spec` - Complete type specification including all distinguishing information
    ///
    /// ## Errors
    /// Returns an error if type construction fails due to invalid specifications
    /// or if required dependencies cannot be resolved.
    pub fn get_or_create_type(&self, spec: &CompleteTypeSpec) -> Result<CilTypeRc> {
        let token = if let Some(init_token) = spec.token_init {
            init_token
        } else {
            self.next_token()
        };

        if let Some(existing) = self.types.get(&token) {
            return Ok(existing.value().clone());
        }

        let flags = spec.flags.unwrap_or(0);
        let new_type = Arc::new(CilType::new(
            token,
            spec.namespace.clone(),
            spec.name.clone(),
            self.get_source_reference(&spec.source),
            None,
            flags,
            Arc::new(boxcar::Vec::new()),
            Arc::new(boxcar::Vec::new()),
            Some(spec.flavor.clone()),
        ));

        Self::configure_type_from_spec(&new_type, spec)?;

        self.register_type_internal(&new_type, spec.source.clone());

        Ok(new_type)
    }

    /// Calculate hash for complete type specification with enhanced collision resistance
    fn calculate_complete_type_hash(spec: &CompleteTypeSpec) -> u64 {
        let mut hash_builder = TypeSignatureHash::new()
            .add_flavor(&spec.flavor)
            .add_fullname(&spec.namespace, &spec.name)
            .add_source(&spec.source);

        // Include generic arguments with enhanced entropy
        if let Some(generic_args) = &spec.generic_args {
            hash_builder = hash_builder
                .add_component(&generic_args.len())
                .add_component(&"generic_args_marker"); // Add distinguishing marker

            for (index, arg) in generic_args.iter().enumerate() {
                // Include position to prevent order-independent collisions
                hash_builder = hash_builder.add_component(&index).add_token(&arg.token);

                // Include additional type characteristics for better distinction
                hash_builder = hash_builder
                    .add_fullname(&arg.namespace, &arg.name)
                    .add_flavor(arg.flavor());
            }
        } else {
            // Add explicit marker for non-generic types to distinguish from empty generic args
            hash_builder = hash_builder.add_component(&"non_generic_marker");
        }

        // Include base type with enhanced information
        if let Some(base_type) = &spec.base_type {
            hash_builder = hash_builder
                .add_component(&"base_type_marker")
                .add_token(&base_type.token)
                .add_fullname(&base_type.namespace, &base_type.name)
                .add_flavor(base_type.flavor());
        } else {
            // Add explicit marker for types without base type
            hash_builder = hash_builder.add_component(&"no_base_type_marker");
        }

        hash_builder.finalize()
    }

    /// Configure type according to complete specification
    fn configure_type_from_spec(type_ref: &CilTypeRc, spec: &CompleteTypeSpec) -> Result<()> {
        // Set base type if specified
        if let Some(base_type) = &spec.base_type {
            if type_ref.base.get().is_none() {
                type_ref
                    .base
                    .set(CilTypeRef::from(base_type.clone()))
                    .map_err(|_| malformed_error!("Base type already set"))?;
            }
        }

        // Configure generic arguments if specified
        if let Some(generic_args) = &spec.generic_args {
            for (index, arg_type) in generic_args.iter().enumerate() {
                let rid = u32::try_from(index)
                    .map_err(|_| malformed_error!("Generic argument index too large"))?
                    + 1;
                let token_value = 0x2B00_0000_u32
                    .checked_add(
                        u32::try_from(index)
                            .map_err(|_| malformed_error!("Generic argument index too large"))?,
                    )
                    .and_then(|v| v.checked_add(1))
                    .ok_or_else(|| malformed_error!("Token value overflow"))?;

                let method_spec = Arc::new(MethodSpec {
                    rid,
                    token: Token::new(token_value),
                    offset: 0,
                    method: CilTypeReference::None,
                    instantiation: SignatureMethodSpec {
                        generic_args: vec![],
                    },
                    custom_attributes: Arc::new(boxcar::Vec::new()),
                    generic_args: {
                        let type_ref_list = Arc::new(boxcar::Vec::with_capacity(1));
                        type_ref_list.push(arg_type.clone().into());
                        type_ref_list
                    },
                });
                type_ref.generic_args.push(method_spec);
            }
        }

        Ok(())
    }

    /// Count of types in the registry
    pub fn len(&self) -> usize {
        self.types.len()
    }

    /// Check if the registry is empty
    pub fn is_empty(&self) -> bool {
        self.types.is_empty()
    }

    /// Returns an iterator over all types in the registry
    pub fn iter(&self) -> crossbeam_skiplist::map::Iter<'_, Token, CilTypeRc> {
        self.types.iter()
    }

    /// Get all types in the registry
    pub fn all_types(&self) -> Vec<CilTypeRc> {
        self.types
            .iter()
            .map(|entry| entry.value().clone())
            .collect()
    }

    /// Get types from a specific source
    ///
    /// ## Arguments
    /// * 'source' - The source of the types to look for
    pub fn types_from_source(&self, source: &TypeSource) -> Vec<CilTypeRc> {
        if let Some(tokens) = self.types_by_source.get(source) {
            tokens
                .iter()
                .filter_map(|token| self.types.get(token).map(|entry| entry.value().clone()))
                .collect()
        } else {
            Vec::new()
        }
    }

    /// Link another TypeRegistry for cross-assembly type resolution.
    ///
    /// This enables the registry to search other assemblies' type registries when
    /// a type cannot be found locally. This is essential for resolving TypeRef
    /// tokens that reference external assemblies.
    ///
    /// # Arguments
    /// * `assembly_identity` - The identity of the external assembly
    /// * `registry` - The TypeRegistry from the external assembly
    ///
    /// # Thread Safety
    /// This method is thread-safe and can be called concurrently.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use dotscope::metadata::{
    ///     identity::AssemblyIdentity,
    ///     typesystem::TypeRegistry,
    /// };
    /// use std::sync::Arc;
    ///
    /// # fn example() -> dotscope::Result<()> {
    /// let main_identity = AssemblyIdentity::parse("Main, Version=1.0.0.0")?;
    /// let main_registry = TypeRegistry::new(main_identity)?;
    /// let ext_identity = AssemblyIdentity::parse("mscorlib, Version=4.0.0.0")?;
    /// let external_registry = Arc::new(TypeRegistry::new(ext_identity.clone())?);
    /// let external_identity = ext_identity;
    ///
    /// main_registry.registry_link(external_identity, external_registry);
    ///
    /// // Now main_registry can resolve types from the external assembly
    /// # Ok(())
    /// # }
    /// ```
    pub fn registry_link(&self, assembly_identity: AssemblyIdentity, registry: Arc<TypeRegistry>) {
        self.external_registries.insert(assembly_identity, registry);
    }

    /// Unlink a TypeRegistry from cross-assembly type resolution.
    ///
    /// # Arguments
    /// * `assembly_identity` - The identity of the assembly to unlink
    ///
    /// # Returns
    /// The removed TypeRegistry if it existed, None otherwise
    pub fn registry_unlink(
        &self,
        assembly_identity: &AssemblyIdentity,
    ) -> Option<Arc<TypeRegistry>> {
        self.external_registries
            .remove(assembly_identity)
            .map(|(_, registry)| registry)
    }

    /// Get a type by fully qualified name across all registries.
    ///
    /// This is a convenience method that calls get_by_fullname_first with include_external=true.
    ///
    /// # Arguments  
    /// * `fullname` - The fully qualified name in "namespace.name" format
    ///
    /// # Returns
    /// * `Some(CilTypeRc)` - The first TypeDef found
    /// * `None` - If no TypeDef with the name is found in any registry
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use dotscope::metadata::typesystem::TypeRegistry;
    ///
    /// # fn example(registry: &TypeRegistry) {
    /// if let Some(string_type) = registry.resolve_type_global("System.String") {
    ///     println!("Resolved System.String: 0x{:08X}", string_type.token.value());
    ///     // This will be a TypeDef if available
    /// }
    /// # }
    /// ```
    pub fn resolve_type_global(&self, fullname: &str) -> Option<CilTypeRc> {
        self.get_by_fullname(fullname, true)
    }

    /// Get all registered external assembly identities.
    ///
    /// # Returns
    /// A vector of all assembly identities that have registered TypeRegistries
    pub fn external_assemblies(&self) -> Vec<AssemblyIdentity> {
        self.external_registries
            .iter()
            .map(|entry| entry.key().clone())
            .collect()
    }

    /// Get an external TypeRegistry by assembly identity.
    ///
    /// # Arguments
    /// * `assembly_identity` - The identity of the external assembly
    ///
    /// # Returns  
    /// * `Some(Arc<TypeRegistry>)` - The external registry if registered
    /// * `None` - If no registry is registered for the assembly
    pub fn get_external_registry(
        &self,
        assembly_identity: &AssemblyIdentity,
    ) -> Option<Arc<TypeRegistry>> {
        self.external_registries
            .get(assembly_identity)
            .map(|entry| entry.value().clone())
    }

    /// Count of registered external registries.
    ///
    /// # Returns
    /// The number of external TypeRegistries currently registered
    pub fn external_registry_count(&self) -> usize {
        self.external_registries.len()
    }

    /// Replace TypeRef registry entries to point to resolved TypeDef.
    ///
    /// This method updates the registry's lookup tables so that the TypeRef token
    /// now points to the resolved TypeDef from another assembly, instead of the
    /// original TypeRef. The TypeRef token remains as the lookup key.
    ///
    /// # Arguments
    /// * `typeref_token` - The TypeRef token to redirect
    /// * `resolved_typedef` - The resolved TypeDef to point to
    ///
    /// # Returns
    /// * `true` if the replacement was successful
    /// * `false` if the TypeRef token was not found in this registry
    pub fn redirect_typeref_to_typedef(
        &self,
        typeref_token: Token,
        resolved_typedef: &CilTypeRc,
    ) -> bool {
        // Get the original TypeRef to clean up its secondary index entries
        let original_typeref = if let Some(entry) = self.types.get(&typeref_token) {
            entry.value().clone()
        } else {
            return false; // TypeRef not found
        };

        // Redirect the TypeRef token in all secondary indexes to use the TypeDef's metadata
        // Remove TypeRef from its original indexes (old metadata)
        if let Some(external) = original_typeref.get_external() {
            let source = self.register_source(external);
            if let Some(mut list) = self.types_by_source.get_mut(&source) {
                list.retain(|&token| token != typeref_token);
            }
        } else {
            let current_source = self.current_assembly_source();
            if let Some(mut list) = self.types_by_source.get_mut(&current_source) {
                list.retain(|&token| token != typeref_token);
            }
        }

        if !original_typeref.namespace.is_empty() {
            if let Some(mut list) = self.types_by_namespace.get_mut(&original_typeref.namespace) {
                list.retain(|&token| token != typeref_token);
            }
        }

        if let Some(mut list) = self.types_by_name.get_mut(&original_typeref.name) {
            list.retain(|&token| token != typeref_token);
        }

        let old_fullname = original_typeref.fullname();
        if let Some(mut list) = self.types_by_fullname.get_mut(&old_fullname) {
            list.retain(|&token| token != typeref_token);
        }

        // Add TypeRef token to the TypeDef's indexes (new metadata)
        if let Some(external) = resolved_typedef.get_external() {
            let source = self.register_source(external);
            self.types_by_source
                .entry(source)
                .or_default()
                .push(typeref_token);
        }

        if !resolved_typedef.namespace.is_empty() {
            self.types_by_namespace
                .entry(resolved_typedef.namespace.clone())
                .or_default()
                .push(typeref_token);
        }

        self.types_by_name
            .entry(resolved_typedef.name.clone())
            .or_default()
            .push(typeref_token);

        self.types_by_fullname
            .entry(resolved_typedef.fullname())
            .or_default()
            .push(typeref_token);

        self.types.insert(typeref_token, resolved_typedef.clone());
        true
    }

    /// Build the fullname lookup table after structural relationships are established.
    ///
    /// This method should be called after all structural relationships (like nested classes)
    /// have been established, so that types can be looked up by their final hierarchical names.
    /// It populates the `types_by_fullname` index which is used by lookup methods.
    ///
    /// # Usage
    ///
    /// This should be called once per assembly after:
    /// - All TypeDef, TypeRef, TypeSpec entries are loaded
    /// - All NestedClass relationships are applied
    /// - Before InheritanceResolver runs (which needs to look up nested types)
    pub fn build_fullnames(&self) {
        self.types_by_fullname.clear();

        for entry in &self.types {
            let type_rc = entry.value();
            let current_fullname = type_rc.fullname();

            self.types_by_fullname
                .entry(current_fullname)
                .or_default()
                .push(type_rc.token);
        }
    }
}

impl<'a> IntoIterator for &'a TypeRegistry {
    type Item = crossbeam_skiplist::map::Entry<'a, Token, CilTypeRc>;
    type IntoIter = crossbeam_skiplist::map::Iter<'a, Token, CilTypeRc>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

#[cfg(test)]
mod tests {
    use uguid::guid;

    use super::*;
    use crate::metadata::tables::{AssemblyRef, AssemblyRefHash, File, Module, ModuleRef};

    #[test]
    fn test_registry_primitives() {
        let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
        let registry = TypeRegistry::new(test_identity).unwrap();

        let bool_type = registry.get_primitive(CilPrimitiveKind::Boolean).unwrap();
        assert_eq!(bool_type.name, "Boolean");
        assert_eq!(bool_type.namespace, "System");

        let int_type = registry.get_primitive(CilPrimitiveKind::I4).unwrap();
        assert_eq!(int_type.name, "Int32");
        assert_eq!(int_type.namespace, "System");

        let object_type = registry.get_primitive(CilPrimitiveKind::Object).unwrap();
        let string_type = registry.get_primitive(CilPrimitiveKind::String).unwrap();

        assert_eq!(
            string_type.base.get().unwrap().token().unwrap(),
            object_type.token
        );

        let value_type = registry.get_primitive(CilPrimitiveKind::ValueType).unwrap();
        assert_eq!(
            value_type.base.get().unwrap().token().unwrap(),
            object_type.token
        );

        assert_eq!(
            int_type.base.get().unwrap().token().unwrap(),
            value_type.token
        );

        let all_primitives = [
            CilPrimitiveKind::Void,
            CilPrimitiveKind::Boolean,
            CilPrimitiveKind::Char,
            CilPrimitiveKind::I1,
            CilPrimitiveKind::U1,
            CilPrimitiveKind::I2,
            CilPrimitiveKind::U2,
            CilPrimitiveKind::I4,
            CilPrimitiveKind::U4,
            CilPrimitiveKind::I8,
            CilPrimitiveKind::U8,
            CilPrimitiveKind::R4,
            CilPrimitiveKind::R8,
            CilPrimitiveKind::I,
            CilPrimitiveKind::U,
            CilPrimitiveKind::Object,
            CilPrimitiveKind::String,
            CilPrimitiveKind::TypedReference,
            CilPrimitiveKind::ValueType,
            CilPrimitiveKind::Var,
            CilPrimitiveKind::MVar,
            CilPrimitiveKind::Null,
        ];

        for primitive in all_primitives.iter() {
            let prim_type = registry.get_primitive(*primitive);
            assert!(prim_type.is_ok(), "Failed to get primitive: {primitive:?}");
        }
    }

    #[test]
    fn test_create_and_lookup() {
        let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
        let registry = TypeRegistry::new(test_identity).unwrap();

        let list_type = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::Class,
                namespace: "System.Collections.Generic".to_string(),
                name: "List`1".to_string(),
                source: TypeSource::Unknown,
                generic_args: None,
                base_type: None,
                flags: None,
            })
            .unwrap();

        assert_eq!(list_type.name, "List`1");
        assert_eq!(list_type.namespace, "System.Collections.Generic");

        let found = registry.get_by_name("List`1");
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].token, list_type.token);

        let found = registry.get_by_namespace("System.Collections.Generic");
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].token, list_type.token);

        let found = registry.get_by_fullname_list("System.Collections.Generic.List`1", false);
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].token, list_type.token);

        let found = registry.get(&list_type.token);
        assert!(found.is_some());
        assert_eq!(found.unwrap().token, list_type.token);

        let found = registry.get_by_source_and_name(
            &TypeSource::Unknown,
            "System.Collections.Generic",
            "List`1",
        );
        assert!(found.is_some());
        assert_eq!(found.unwrap().token, list_type.token);
    }

    #[test]
    fn test_multiple_types_with_same_name() {
        let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
        let registry = TypeRegistry::new(test_identity).unwrap();

        let point1 = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::ValueType,
                namespace: "System.Drawing".to_string(),
                name: "Point".to_string(),
                source: TypeSource::Unknown,
                generic_args: None,
                base_type: None,
                flags: None,
            })
            .unwrap();

        let point2 = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::ValueType,
                namespace: "System.Windows".to_string(),
                name: "Point".to_string(),
                source: TypeSource::Unknown,
                generic_args: None,
                base_type: None,
                flags: None,
            })
            .unwrap();

        assert_ne!(point1.token, point2.token);

        let found = registry.get_by_name("Point");
        assert_eq!(found.len(), 2);

        let found = registry.get_by_fullname_list("System.Drawing.Point", false);
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].token, point1.token);

        let found = registry.get_by_fullname_list("System.Windows.Point", false);
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].token, point2.token);
    }

    #[test]
    fn test_create_type_empty() {
        let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
        let registry = TypeRegistry::new(test_identity).unwrap();

        let empty_type = registry.create_type_empty().unwrap();

        assert_eq!(empty_type.namespace, "");
        assert_eq!(empty_type.name, "");
        assert!(matches!(*empty_type.flavor(), CilFlavor::Class)); // Empty types default to Class with lazy evaluation
    }

    #[test]
    fn test_create_type_with_flavor() {
        let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
        let registry = TypeRegistry::new(test_identity).unwrap();

        let class_type = registry.create_type_with_flavor(CilFlavor::Class).unwrap();

        assert_eq!(class_type.namespace, "");
        assert_eq!(class_type.name, "");
        assert!(matches!(*class_type.flavor(), CilFlavor::Class));
    }

    #[test]
    fn test_insert() {
        let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
        let registry = TypeRegistry::new(test_identity.clone()).unwrap();

        let token = Token::new(0x01000123);
        let new_type = Arc::new(CilType::new(
            token,
            "MyNamespace".to_string(),
            "MyClass".to_string(),
            None,
            None,
            0,
            Arc::new(boxcar::Vec::new()),
            Arc::new(boxcar::Vec::new()),
            Some(CilFlavor::Class),
        ));

        registry.insert(&new_type);

        let found = registry.get(&token);
        assert!(found.is_some());
        assert_eq!(found.unwrap().token, token);

        registry.insert(&new_type);

        let user_types = registry.types_from_source(&TypeSource::Assembly(test_identity));
        assert_eq!(user_types.len(), 1);
    }

    #[test]
    fn test_source_registry() {
        let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
        let registry = TypeRegistry::new(test_identity).unwrap();

        let module = Arc::new(Module {
            token: Token::new(0x00000001),
            name: "MainModule".to_string(),
            mvid: guid!("01234567-89ab-cdef-0123-456789abcdef"),
            encid: None,
            rid: 1,
            offset: 1,
            generation: 0,
            encbaseid: None,
            imports: Vec::new(),
            custom_attributes: Arc::new(boxcar::Vec::new()),
        });

        let module_ref = Arc::new(ModuleRef {
            token: Token::new(0x1A000001),
            name: "ReferenceModule".to_string(),
            rid: 0,
            offset: 0,
            custom_attributes: Arc::new(boxcar::Vec::new()),
        });

        let assembly_ref = Arc::new(AssemblyRef {
            token: Token::new(0x23000001),
            flags: 0,
            name: "ReferenceAssembly".to_string(),
            culture: Some("".to_string()),
            rid: 0,
            offset: 0,
            major_version: 1,
            minor_version: 0,
            build_number: 0,
            revision_number: 1,
            identifier: None,
            hash: None,
            os_platform_id: AtomicU32::new(0),
            os_major_version: AtomicU32::new(0),
            os_minor_version: AtomicU32::new(0),
            processor: AtomicU32::new(0),
            custom_attributes: Arc::new(boxcar::Vec::new()),
        });

        let file = Arc::new(File {
            token: Token::new(0x26000001),
            flags: 0,
            name: "ExternalFile.dll".to_string(),
            rid: 0,
            offset: 0,
            hash_value: AssemblyRefHash::new(&[0xCC, 0xCC]).unwrap(),
            custom_attributes: Arc::new(boxcar::Vec::new()),
        });

        let module_source = registry.register_source(&CilTypeReference::Module(module.clone()));
        let module_ref_source =
            registry.register_source(&CilTypeReference::ModuleRef(module_ref.clone()));
        let assembly_ref_source =
            registry.register_source(&CilTypeReference::AssemblyRef(assembly_ref.clone()));
        let file_source = registry.register_source(&CilTypeReference::File(file.clone()));

        assert!(matches!(module_source, TypeSource::Module(_)));
        assert!(matches!(module_ref_source, TypeSource::ModuleRef(_)));
        assert!(matches!(assembly_ref_source, TypeSource::AssemblyRef(_)));
        assert!(matches!(file_source, TypeSource::File(_)));

        if let TypeSource::Module(token) = module_source {
            if let CilTypeReference::Module(ref m) =
                registry.get_source_reference(&module_source).unwrap()
            {
                assert_eq!(m.token, token);
            } else {
                panic!("Expected Module reference");
            }
        }

        if let TypeSource::ModuleRef(token) = module_ref_source {
            if let CilTypeReference::ModuleRef(ref m) =
                registry.get_source_reference(&module_ref_source).unwrap()
            {
                assert_eq!(m.token, token);
            } else {
                panic!("Expected ModuleRef reference");
            }
        }

        if let TypeSource::AssemblyRef(token) = assembly_ref_source {
            if let CilTypeReference::AssemblyRef(ref a) =
                registry.get_source_reference(&assembly_ref_source).unwrap()
            {
                assert_eq!(a.token, token);
            } else {
                panic!("Expected AssemblyRef reference");
            }
        }

        if let TypeSource::File(token) = file_source {
            if let CilTypeReference::File(ref f) =
                registry.get_source_reference(&file_source).unwrap()
            {
                assert_eq!(f.token, token);
            } else {
                panic!("Expected File reference");
            }
        }

        let type1 = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::Class,
                namespace: "System.Collections".to_string(),
                name: "ArrayList".to_string(),
                source: TypeSource::Unknown,
                generic_args: None,
                base_type: None,
                flags: None,
            })
            .unwrap();

        let type2 = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::Class,
                namespace: "System.Collections".to_string(),
                name: "ArrayList".to_string(),
                source: module_ref_source.clone(),
                generic_args: None,
                base_type: None,
                flags: None,
            })
            .unwrap();

        let type3 = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::Class,
                namespace: "System.Collections".to_string(),
                name: "ArrayList".to_string(),
                source: assembly_ref_source.clone(),
                generic_args: None,
                base_type: None,
                flags: None,
            })
            .unwrap();

        assert_ne!(type1.token, type2.token);
        assert_ne!(type1.token, type3.token);
        assert_ne!(type2.token, type3.token);

        let types_from_module_ref = registry.types_from_source(&module_ref_source);
        assert_eq!(types_from_module_ref.len(), 1);
        assert_eq!(types_from_module_ref[0].token, type2.token);

        let types_from_assembly_ref = registry.types_from_source(&assembly_ref_source);
        assert_eq!(types_from_assembly_ref.len(), 1);
        assert_eq!(types_from_assembly_ref[0].token, type3.token);
    }

    #[test]
    fn test_registry_count_and_all_types() {
        let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
        let registry = TypeRegistry::new(test_identity).unwrap();

        let initial_count = registry.len();

        let _ = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::Class,
                namespace: "MyNamespace".to_string(),
                name: "MyClass1".to_string(),
                source: TypeSource::Unknown,
                generic_args: None,
                base_type: None,
                flags: None,
            })
            .unwrap();

        let _ = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::Class,
                namespace: "MyNamespace".to_string(),
                name: "MyClass2".to_string(),
                source: TypeSource::Unknown,
                generic_args: None,
                base_type: None,
                flags: None,
            })
            .unwrap();

        assert_eq!(registry.len(), initial_count + 2);

        let all_types = registry.all_types();
        assert!(all_types.len() >= initial_count + 2);

        let class1_count = all_types
            .iter()
            .filter(|t| t.name == "MyClass1" && t.namespace == "MyNamespace")
            .count();

        let class2_count = all_types
            .iter()
            .filter(|t| t.name == "MyClass2" && t.namespace == "MyNamespace")
            .count();

        assert_eq!(class1_count, 1);
        assert_eq!(class2_count, 1);
    }

    #[test]
    fn test_type_signature_hash() {
        let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
        let registry = TypeRegistry::new(test_identity).unwrap();

        let source1 = TypeSource::Unknown;
        let source2 = TypeSource::AssemblyRef(Token::new(0x23000001));

        let type1 = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::Class,
                namespace: "System.Collections".to_string(),
                name: "ArrayList".to_string(),
                source: source1,
                generic_args: None,
                base_type: None,
                flags: None,
            })
            .unwrap();

        let type2 = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::Class,
                namespace: "System.Collections".to_string(),
                name: "ArrayList".to_string(),
                source: source2,
                generic_args: None,
                base_type: None,
                flags: None,
            })
            .unwrap();

        assert_ne!(type1.token, type2.token);
    }

    #[test]
    fn test_class_vs_generic_instance_hash_debug() {
        // Debug test to investigate hash collision between Class and GenericInstance
        let class_hash = TypeSignatureHash::new()
            .add_flavor(&CilFlavor::Class)
            .add_fullname("System.Collections.Generic", "Dictionary`2")
            .add_source(&TypeSource::Unknown)
            .finalize();

        let generic_instance_hash = TypeSignatureHash::new()
            .add_flavor(&CilFlavor::GenericInstance)
            .add_fullname("System.Collections.Generic", "Dictionary`2")
            .add_source(&TypeSource::Unknown)
            .finalize();

        // They should be different
        assert_ne!(
            class_hash, generic_instance_hash,
            "Class and GenericInstance with same name should have different hashes"
        );
    }

    #[test]
    fn test_enhanced_generic_instance_creation() {
        let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
        let registry = TypeRegistry::new(test_identity).unwrap();

        // Create primitive types for generic arguments
        let string_type = registry.get_primitive(CilPrimitiveKind::String).unwrap();
        let int_type = registry.get_primitive(CilPrimitiveKind::I4).unwrap();
        let _object_type = registry.get_primitive(CilPrimitiveKind::Object).unwrap();

        // Test context-aware creation for generic instances
        let list_string_1 = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::GenericInstance,
                namespace: "System.Collections.Generic".to_string(),
                name: "List`1".to_string(),
                source: TypeSource::Unknown,
                generic_args: Some(vec![string_type.clone()]),
                base_type: None,
                flags: None,
            })
            .unwrap();

        let list_string_2 = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::GenericInstance,
                namespace: "System.Collections.Generic".to_string(),
                name: "List`1".to_string(),
                source: TypeSource::Unknown,
                generic_args: Some(vec![string_type.clone()]),
                base_type: None,
                flags: None,
            })
            .unwrap();

        let list_int = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::GenericInstance,
                namespace: "System.Collections.Generic".to_string(),
                name: "List`1".to_string(),
                source: TypeSource::Unknown,
                generic_args: Some(vec![int_type.clone()]),
                base_type: None,
                flags: None,
            })
            .unwrap();

        // Since deduplication is disabled, these should be different instances
        assert_ne!(
            list_string_1.token, list_string_2.token,
            "Without deduplication, tokens should be different"
        );
        assert!(
            !Arc::ptr_eq(&list_string_1, &list_string_2),
            "Without deduplication, instances should be different"
        );

        assert!(
            !list_string_1.is_structurally_equivalent(&list_int),
            "List<string> and List<int> should NOT be structurally equivalent"
        );

        // Basic type identity checks
        assert_eq!(list_string_1.namespace, "System.Collections.Generic");
        assert_eq!(list_string_1.name, "List`1");
        assert!(matches!(
            *list_string_1.flavor(),
            CilFlavor::GenericInstance
        ));
    }

    #[test]
    fn test_token_generation() {
        let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
        let registry = TypeRegistry::new(test_identity).unwrap();

        let token1 = registry.create_type_empty().unwrap().token;
        let token2 = registry.create_type_empty().unwrap().token;
        let token3 = registry.create_type_empty().unwrap().token;

        assert_eq!(token2.value(), token1.value() + 1);
        assert_eq!(token3.value(), token2.value() + 1);
    }

    #[test]
    fn test_get_and_lookup_methods() {
        let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
        let registry = TypeRegistry::new(test_identity).unwrap();

        let bad_token = Token::new(0x01999999);
        assert!(registry.get(&bad_token).is_none());

        let bad_name = registry.get_by_name("DoesNotExist");
        assert!(bad_name.is_empty());

        let bad_namespace = registry.get_by_namespace("NonExistent.Namespace");
        assert!(bad_namespace.is_empty());

        let bad_fullname = registry.get_by_fullname_list("NonExistent.Namespace.Type", false);
        assert!(bad_fullname.is_empty());

        let bad_source_name =
            registry.get_by_source_and_name(&TypeSource::Unknown, "NonExistent.Namespace", "Type");
        assert!(bad_source_name.is_none());
    }

    #[test]
    fn test_improved_hash_collision_resistance() {
        let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
        let registry = TypeRegistry::new(test_identity).unwrap();

        // Test cases that would collide with old XOR-based approach
        let types = [
            ("System", "String", TypeSource::Unknown),
            ("System", "Object", TypeSource::Unknown),
            ("System.Collections", "ArrayList", TypeSource::Unknown),
            ("System.Collections.Generic", "List`1", TypeSource::Unknown),
            (
                "MyApp",
                "Helper",
                TypeSource::AssemblyRef(Token::new(0x23000001)),
            ),
            (
                "MyApp",
                "Helper",
                TypeSource::AssemblyRef(Token::new(0x23000002)),
            ),
        ];

        let mut created_types = Vec::new();
        for (namespace, name, source) in &types {
            let type_ref = registry
                .get_or_create_type(&CompleteTypeSpec {
                    token_init: None,
                    flavor: CilFlavor::Class,
                    namespace: namespace.to_string(),
                    name: name.to_string(),
                    source: source.clone(),
                    generic_args: None,
                    base_type: None,
                    flags: None,
                })
                .unwrap();
            created_types.push(type_ref);
        }

        // All types should be unique (each request creates a new type instance)
        assert_eq!(created_types.len(), types.len());

        // Each type should have a unique token
        let mut tokens = std::collections::HashSet::new();
        for type_ref in &created_types {
            assert!(
                tokens.insert(type_ref.token),
                "Duplicate token found: {:?}",
                type_ref.token
            );
        }

        // Verify that identical requests create different instances (no deduplication)
        let duplicate_request = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::Class,
                namespace: "System".to_string(),
                name: "String".to_string(),
                source: TypeSource::Unknown,
                generic_args: None,
                base_type: None,
                flags: None,
            })
            .unwrap();

        assert_ne!(
            duplicate_request.token, created_types[0].token,
            "Each request should create a unique type instance"
        );
    }

    #[test]
    fn test_hash_different_flavors() {
        let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
        let registry = TypeRegistry::new(test_identity).unwrap();

        // Same name/namespace, different flavors should create different types
        let class_type = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::Class,
                namespace: "MyNamespace".to_string(),
                name: "MyType".to_string(),
                source: TypeSource::Unknown,
                generic_args: None,
                base_type: None,
                flags: None,
            })
            .unwrap();

        let interface_type = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::Interface,
                namespace: "MyNamespace".to_string(),
                name: "MyType".to_string(),
                source: TypeSource::Unknown,
                generic_args: None,
                base_type: None,
                flags: None,
            })
            .unwrap();

        let value_type = registry
            .get_or_create_type(&CompleteTypeSpec {
                token_init: None,
                flavor: CilFlavor::ValueType,
                namespace: "MyNamespace".to_string(),
                name: "MyType".to_string(),
                source: TypeSource::Unknown,
                generic_args: None,
                base_type: None,
                flags: None,
            })
            .unwrap();

        // All should have different tokens
        assert_ne!(class_type.token, interface_type.token);
        assert_ne!(class_type.token, value_type.token);
        assert_ne!(interface_type.token, value_type.token);

        // Verify flavors are correct
        assert_eq!(*class_type.flavor(), CilFlavor::Class);
        assert_eq!(*interface_type.flavor(), CilFlavor::Interface);
        assert_eq!(*value_type.flavor(), CilFlavor::ValueType);
    }

    #[test]
    fn test_hash_collision_chain_functionality() {
        let test_identity = AssemblyIdentity::parse("TestAssembly, Version=1.0.0.0").unwrap();
        let registry = TypeRegistry::new(test_identity).unwrap();

        // Force potential hash collision by creating many similar types
        let similar_types = [
            "Type1", "Type2", "Type3", "Type4", "Type5", "Type11", "Type12", "Type13", "Type14",
            "Type15", "TypeA", "TypeB", "TypeC", "TypeD", "TypeE",
        ];

        let mut created_tokens = std::collections::HashSet::new();

        for type_name in &similar_types {
            let type_ref = registry
                .get_or_create_type(&CompleteTypeSpec {
                    token_init: None,
                    flavor: CilFlavor::Class,
                    namespace: "TestNamespace".to_string(),
                    name: type_name.to_string(),
                    source: TypeSource::Unknown,
                    generic_args: None,
                    base_type: None,
                    flags: None,
                })
                .unwrap();

            // Each type should get a unique token
            assert!(
                created_tokens.insert(type_ref.token),
                "Token collision for type: {}",
                type_name
            );

            // Verify the type can be retrieved correctly
            assert_eq!(type_ref.name, *type_name);
            assert_eq!(type_ref.namespace, "TestNamespace");
        }

        // All types should be distinct
        assert_eq!(created_tokens.len(), similar_types.len());
    }

    #[test]
    fn test_signature_hash_ordering_independence() {
        // Test that hash function is sensitive to parameter order
        // This was a problem with the old XOR approach

        let hash1 = TypeSignatureHash::new()
            .add_fullname("System", "String")
            .add_source(&TypeSource::Unknown)
            .add_flavor(&CilFlavor::Class)
            .finalize();

        let hash2 = TypeSignatureHash::new()
            .add_flavor(&CilFlavor::Class)
            .add_fullname("System", "String")
            .add_source(&TypeSource::Unknown)
            .finalize();

        let hash3 = TypeSignatureHash::new()
            .add_source(&TypeSource::Unknown)
            .add_flavor(&CilFlavor::Class)
            .add_fullname("System", "String")
            .finalize();

        // Different orders should produce different hashes (order sensitivity)
        assert_ne!(hash1, hash2, "Hash should be order-sensitive");
        assert_ne!(hash1, hash3, "Hash should be order-sensitive");
        assert_ne!(hash2, hash3, "Hash should be order-sensitive");
    }

    #[test]
    fn test_signature_hash_component_uniqueness() {
        // Test that different components produce different hashes

        let base_hash = TypeSignatureHash::new()
            .add_fullname("System", "String")
            .add_source(&TypeSource::Unknown)
            .finalize();

        let class_hash = TypeSignatureHash::new()
            .add_fullname("System", "String")
            .add_source(&TypeSource::Unknown)
            .add_flavor(&CilFlavor::Class)
            .finalize();

        let interface_hash = TypeSignatureHash::new()
            .add_fullname("System", "String")
            .add_source(&TypeSource::Unknown)
            .add_flavor(&CilFlavor::Interface)
            .finalize();

        let different_source_hash = TypeSignatureHash::new()
            .add_fullname("System", "String")
            .add_source(&TypeSource::AssemblyRef(Token::new(0x23000001)))
            .add_flavor(&CilFlavor::Class)
            .finalize();

        // All should be different
        assert_ne!(base_hash, class_hash);
        assert_ne!(class_hash, interface_hash);
        assert_ne!(class_hash, different_source_hash);
        assert_ne!(interface_hash, different_source_hash);
    }

    #[test]
    fn test_class_vs_generic_instance_hash_collision() {
        // This is the specific collision that breaks the test
        let class_hash = TypeSignatureHash::new()
            .add_flavor(&CilFlavor::Class)
            .add_fullname("System.Collections.Generic", "Dictionary`2")
            .add_source(&TypeSource::Unknown)
            .finalize();

        let generic_instance_hash = TypeSignatureHash::new()
            .add_flavor(&CilFlavor::GenericInstance)
            .add_fullname("System.Collections.Generic", "Dictionary`2")
            .add_source(&TypeSource::Unknown)
            .finalize();

        // These should be different for proper type distinction
        assert_ne!(
            class_hash, generic_instance_hash,
            "CRITICAL: Class and GenericInstance are generating hash collisions! \
             This proves the original hash collision issue still exists."
        );
    }
}