summaryrefslogtreecommitdiff
path: root/plugins/pdb-ng/src/type_parser.rs
blob: 7805cbc45b5586431839a79a4f3db05573f51fb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
// Copyright 2022-2026 Vector 35 Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::HashMap;
use std::sync::OnceLock;

use crate::struct_grouper::group_structure;
use crate::PDBParserInstance;
use anyhow::{anyhow, Result};
use binaryninja::architecture::Architecture;
use binaryninja::calling_convention::{CallingConvention, CoreCallingConvention};
use binaryninja::confidence::{Conf, MAX_CONFIDENCE};
use binaryninja::platform::Platform;
use binaryninja::rc::Ref;
use binaryninja::types::{
    BaseStructure, EnumerationBuilder, EnumerationMember, FunctionParameter, MemberAccess,
    MemberScope, NamedTypeReference, NamedTypeReferenceClass, ReturnValue, StructureBuilder,
    StructureMember, StructureType, Type, TypeBuilder, TypeClass, ValueLocation,
    ValueLocationComponent,
};
use binaryninja::variable::{Variable, VariableSourceType};
use pdb::Error::UnimplementedTypeKind;
use pdb::{
    ArgumentList, ArrayType, BaseClassType, BitfieldType, ClassKind, ClassType, EnumerateType,
    EnumerationType, FallibleIterator, FieldAttributes, FieldList, FunctionAttributes, Indirection,
    ItemFinder, MemberFunctionType, MemberType, MethodList, MethodType, ModifierType, NestedType,
    OverloadedMethodType, PointerMode, PointerType, PrimitiveKind, PrimitiveType, ProcedureType,
    Source, StaticMemberType, TypeData, TypeIndex, UnionType, Variant, VirtualBaseClassType,
    VirtualFunctionTablePointerType, VirtualFunctionTableType, VirtualTableShapeType,
};
use regex::Regex;

static BUILTIN_NAMES: &[&str] = &[
    "size_t",
    "ssize_t",
    "ptrdiff_t",
    "wchar_t",
    "wchar16",
    "wchar32",
    "bool",
];
// const VOID_RETURN_CONFIDENCE: u8 = 16;

/// Function types
#[derive(Debug, Clone)]
pub struct ParsedProcedureType {
    /// Interpreted type of the method, with thisptr, __return, etc
    pub method_type: Ref<Type>,
    /// Base method type right outta the pdb with no frills
    pub raw_method_type: Ref<Type>,
}

/// Bitfield member type, if we ever get around to implementing these
#[derive(Debug, Clone)]
pub struct ParsedBitfieldType {
    /// Size in bits
    pub size: u64,
    /// Bit offset in the current bitfield set
    pub position: u64,
    /// Underlying type of the whole bitfield set
    pub ty: Ref<Type>,
}

/// Parsed member of a class/structure, basically just binaryninja::StructureMember but with bitfields :(
#[derive(Debug, Clone)]
pub struct ParsedMember {
    /// Member type
    pub ty: Conf<Ref<Type>>,
    /// Member name
    pub name: String,
    /// Offset in structure (bytes)
    pub offset: u64,
    /// Access flags
    pub access: MemberAccess,
    /// Scope doesn't really mean anything in binja
    pub scope: MemberScope,
    /// Bitfield size, if this is in a bitfield. Mainly you should just be checking for Some()
    pub bitfield_size: Option<u64>,
    /// Bit offset, if this is in a bitfield. Mainly you should just be checking for Some()
    pub bitfield_position: Option<u64>,
}

/// Parsed named method of a class
#[derive(Debug, Clone)]
pub struct ParsedMethod {
    /// Attributes from pdb-rs
    pub attributes: FieldAttributes,
    /// Name of method
    pub name: String,
    /// Type of the method + class info
    pub method_type: ParsedMemberFunction,
    /// Offset in class's virtual table, if virtual
    pub vtable_offset: Option<usize>,
}

/// One entry in a list of parsed methods? This is just here so overloaded methods have a struct to use
#[derive(Debug, Clone)]
pub struct ParsedMethodListEntry {
    /// Attributes from pdb-rs
    pub attributes: FieldAttributes,
    /// Type of the method + class info
    pub method_type: ParsedMemberFunction,
    /// Offset in class's virtual table, if virtual
    pub vtable_offset: Option<usize>,
}

/// Parsed member function type info
#[derive(Debug, Clone)]
pub struct ParsedMemberFunction {
    /// Attributes from pdb-rs
    pub attributes: FunctionAttributes,
    /// Parent class's name
    pub class_name: String,
    /// Interpreted type of the method, with thisptr, __return, etc
    pub method_type: Ref<Type>,
    /// Base method type right outta the pdb with no frills
    pub raw_method_type: Ref<Type>,
    /// Type of thisptr object, if relevant
    pub this_pointer_type: Option<Ref<Type>>,
    /// Adjust to thisptr at start, for virtual bases or something
    pub this_adjustment: usize,
}

/// Virtual base class, c++ nightmare fuel
#[derive(Debug, Clone)]
pub struct VirtualBaseClass {
    /// Base class name
    pub base_name: String,
    /// Base class type
    pub base_type: Ref<Type>,
    /// Offset in this class where the base's fields are located
    pub base_offset: u64,
    /// Type of vbtable, probably
    pub base_table_type: Ref<Type>,
    /// Offset of this base in the vbtable
    pub base_table_offset: u64,
}

/// Mega enum of all the different types of types we can parse
#[derive(Debug, Clone)]
pub enum ParsedType {
    /// No info other than type data
    Bare(Ref<Type>),
    /// Named fully parsed class/enum/union/etc type
    Named(String, Ref<Type>),
    /// Function procedure
    Procedure(ParsedProcedureType),
    /// Bitfield entries
    BitfieldType(ParsedBitfieldType),
    /// A list of members for a structure / union
    FieldList(Vec<ParsedType>),
    /// One member in a structure/union
    Member(ParsedMember),
    /// Base class name and offset details
    BaseClass(String, StructureMember),
    /// One member in an enumeration
    Enumerate(EnumerationMember),
    /// List of arguments to a function
    ArgumentList(Vec<FunctionParameter>),
    /// Parsed member function type info
    MemberFunction(ParsedMemberFunction),
    /// Parsed named method of a class
    Method(ParsedMethod),
    /// List of all the methods in a class
    MethodList(Vec<ParsedMethodListEntry>),
    /// (Name, Overloads) equivalent to ParsedMethod
    OverloadedMethod(String, Vec<ParsedMethodListEntry>),
    /// Virtual table shape straight outta pdb-rs
    VTableShape(Vec<u8>),
    /// Also virtual table shape, but you want a pointer this time
    VTablePointer(Vec<u8>),
    /// Virtual base class, c++ nightmare fuel
    VBaseClass(VirtualBaseClass),
}

#[allow(non_camel_case_types)]
#[derive(Debug)]
pub enum CV_call_t {
    NEAR_C = 1,
    FAR_C = 2,
    NEAR_PASCAL = 3,
    FAR_PASCAL = 4,
    NEAR_FAST = 5,
    FAR_FAST = 6,
    SKIPPED = 7,
    NEAR_STD = 8,
    FAR_STD = 9,
    NEAR_SYS = 10,
    FAR_SYS = 11,
    THISCALL = 12,
    MIPSCALL = 13,
    GENERIC = 14,
    ALPHACALL = 15,
    PPCCALL = 16,
    SHCALL = 17,
    ARMCALL = 18,
    AM33CALL = 19,
    TRICALL = 20,
    SH5CALL = 21,
    M32RCALL = 22,
    ALWAYS_INLINED = 23,
    NEAR_VECTOR = 24,
    RESERVED = 25,
}

impl TryFrom<u8> for CV_call_t {
    type Error = anyhow::Error;

    fn try_from(value: u8) -> Result<Self> {
        match value {
            0 => Err(anyhow!("Empty calling convention")),
            1 => Ok(Self::NEAR_C),
            2 => Ok(Self::FAR_C),
            3 => Ok(Self::NEAR_PASCAL),
            4 => Ok(Self::FAR_PASCAL),
            5 => Ok(Self::NEAR_FAST),
            6 => Ok(Self::FAR_FAST),
            7 => Ok(Self::SKIPPED),
            8 => Ok(Self::NEAR_STD),
            9 => Ok(Self::FAR_STD),
            10 => Ok(Self::NEAR_SYS),
            11 => Ok(Self::FAR_SYS),
            12 => Ok(Self::THISCALL),
            13 => Ok(Self::MIPSCALL),
            14 => Ok(Self::GENERIC),
            15 => Ok(Self::ALPHACALL),
            16 => Ok(Self::PPCCALL),
            17 => Ok(Self::SHCALL),
            18 => Ok(Self::ARMCALL),
            19 => Ok(Self::AM33CALL),
            20 => Ok(Self::TRICALL),
            21 => Ok(Self::SH5CALL),
            22 => Ok(Self::M32RCALL),
            23 => Ok(Self::ALWAYS_INLINED),
            24 => Ok(Self::NEAR_VECTOR),
            25 => Ok(Self::RESERVED),
            e => Err(anyhow!("Unknown CV_call_t convention {}", e)),
        }
    }
}

/// This is all done in the parser instance namespace because the lifetimes are impossible to
/// wrangle otherwise.
impl<'a, S: Source<'a> + 'a> PDBParserInstance<'a, S> {
    /// Parse all the types in a pdb
    pub fn parse_types(
        &mut self,
        progress: Box<dyn Fn(usize, usize) -> Result<()> + '_>,
    ) -> Result<()> {
        // Hack: This is needed for primitive types but it's not defined in the pdb itself
        self.named_types
            .insert("HRESULT".into(), Type::int(4, true));

        let type_information = self.pdb.type_information()?;
        let mut finder = type_information.finder();

        let mut type_count = 0;

        // Do an initial pass on the types to find the full indexes for named types
        // In case something like an array needs to reference them before they're fully defined
        let mut prepass_types = type_information.iter();
        while let Some(ty) = prepass_types.next()? {
            type_count += 1;
            finder.update(&prepass_types);
            match ty.parse() {
                Ok(TypeData::Class(data)) => {
                    if !data.properties.forward_reference() {
                        self.full_type_indices.insert(
                            data.unique_name
                                .unwrap_or(data.name)
                                .to_string()
                                .to_string(),
                            ty.index(),
                        );
                    }
                }
                Ok(TypeData::Enumeration(data)) => {
                    if !data.properties.forward_reference() {
                        self.full_type_indices.insert(
                            data.unique_name
                                .unwrap_or(data.name)
                                .to_string()
                                .to_string(),
                            ty.index(),
                        );
                    }
                }
                Ok(TypeData::Union(data)) => {
                    if !data.properties.forward_reference() {
                        self.full_type_indices.insert(
                            data.unique_name
                                .unwrap_or(data.name)
                                .to_string()
                                .to_string(),
                            ty.index(),
                        );
                    }
                }
                _ => {}
            }
        }

        self.log(|| "Now parsing named types");

        // Parse the types we care about, so that recursion gives us parent relationships for free
        let mut types = type_information.iter();
        let mut i = 0;
        while let Some(ty) = types.next()? {
            i += 1;
            (progress)(i, type_count * 2)?;

            match ty.parse() {
                Ok(TypeData::Class(_)) | Ok(TypeData::Enumeration(_)) | Ok(TypeData::Union(_)) => {
                    self.handle_type_index(ty.index(), &mut finder)?;
                }
                _ => {}
            }

            assert!(self.namespace_stack.is_empty());
            assert!(self.type_stack.is_empty());
        }

        self.log(|| "Now parsing unused floating types");

        // Parse the rest because symbols often use them
        let mut postpass_types = type_information.iter();
        while let Some(ty) = postpass_types.next()? {
            i += 1;
            (progress)(i, type_count * 2)?;

            self.handle_type_index(ty.index(), &mut finder)?;
        }

        self.log(|| "Now adding all unreferenced named types");
        // Any referenced named types that are only forward-declared will cause missing type references,
        // so create empty types for those here.
        for parsed in self.indexed_types.values() {
            match parsed {
                ParsedType::Bare(ty) if ty.type_class() == TypeClass::NamedTypeReferenceClass => {
                    // See if we have this type
                    let name = ty
                        .get_named_type_reference()
                        .ok_or(anyhow!("expected ntr"))?
                        .name()
                        .to_string();
                    if Self::is_name_anonymous(&name) {
                        continue;
                    }
                    if self.named_types.contains_key(&name) {
                        continue;
                    }
                    // If the bv has this type, DebugInfo will just update us to reference it
                    if self.bv.type_by_name(name.to_owned()).is_some() {
                        continue;
                    }

                    self.log(|| format!("Got undefined but referenced named type: {}", &name));
                    let type_class = ty
                        .get_named_type_reference()
                        .ok_or(anyhow!("expected ntr"))?
                        .class();

                    let bare_type = match type_class {
                        NamedTypeReferenceClass::ClassNamedTypeClass => Type::structure(
                            StructureBuilder::new()
                                .structure_type(StructureType::ClassStructureType)
                                .finalize()
                                .as_ref(),
                        ),
                        // Missing typedefs are just going to become structures
                        NamedTypeReferenceClass::UnknownNamedTypeClass
                        | NamedTypeReferenceClass::TypedefNamedTypeClass
                        | NamedTypeReferenceClass::StructNamedTypeClass => {
                            Type::structure(StructureBuilder::new().finalize().as_ref())
                        }
                        NamedTypeReferenceClass::UnionNamedTypeClass => Type::structure(
                            StructureBuilder::new()
                                .structure_type(StructureType::UnionStructureType)
                                .finalize()
                                .as_ref(),
                        ),
                        NamedTypeReferenceClass::EnumNamedTypeClass => Type::enumeration(
                            EnumerationBuilder::new().finalize().as_ref(),
                            self.arch.default_integer_size().try_into()?,
                            false,
                        ),
                    };

                    self.log(|| format!("Bare type created: {} {}", &name, &bare_type));
                    self.named_types.insert(name, bare_type);
                }
                _ => {}
            }
        }

        // Cleanup a couple builtin names
        for &name in BUILTIN_NAMES {
            if self.named_types.contains_key(name) {
                self.named_types.remove(name);
                self.log(|| format!("Remove builtin type {}", name));
            }
        }

        static MEM: OnceLock<Regex> = OnceLock::new();
        let uint_regex = MEM.get_or_init(|| Regex::new(r"u?int\d+_t").unwrap());

        let float_regex = MEM.get_or_init(|| Regex::new(r"float\d+").unwrap());

        let mut remove_names = vec![];
        for name in self.named_types.keys() {
            let name_str = name.to_string();
            if uint_regex.is_match(&name_str) {
                remove_names.push(name.clone());
            }
            if float_regex.is_match(&name_str) {
                remove_names.push(name.clone());
            }
        }
        for name in remove_names {
            self.named_types.remove(&name);
            self.log(|| format!("Remove builtin type {}", &name));
        }

        Ok(())
    }

    /// Lookup a type in the parsed types by its index (ie for a procedure)
    pub(crate) fn lookup_type(
        &self,
        index: &TypeIndex,
        fancy_procs: bool,
    ) -> Result<Option<Ref<Type>>> {
        match self.indexed_types.get(index) {
            Some(ParsedType::Bare(ty)) => Ok(Some(ty.clone())),
            Some(ParsedType::Named(name, ty)) => {
                Ok(Some(Type::named_type_from_type(name.clone(), ty)))
            }
            Some(ParsedType::Procedure(ParsedProcedureType {
                method_type,
                raw_method_type,
            })) => {
                if fancy_procs {
                    Ok(Some(method_type.clone()))
                } else {
                    Ok(Some(raw_method_type.clone()))
                }
            }
            Some(ParsedType::MemberFunction(ParsedMemberFunction {
                method_type,
                raw_method_type,
                ..
            })) => {
                if fancy_procs {
                    Ok(Some(method_type.clone()))
                } else {
                    Ok(Some(raw_method_type.clone()))
                }
            }
            Some(ParsedType::Member(ParsedMember { ty, .. })) => Ok(Some(ty.contents.clone())),
            _ => Ok(None),
        }
    }

    /// Lookup a type in the parsed types and get a confidence value for it too
    pub(crate) fn lookup_type_conf(
        &self,
        index: &TypeIndex,
        fancy_procs: bool,
    ) -> Result<Option<Conf<Ref<Type>>>> {
        match self.lookup_type(index, fancy_procs)? {
            Some(ty) if ty.type_class() == TypeClass::VoidTypeClass => Ok(Some(Conf::new(ty, 0))),
            Some(ty) => {
                let mut confidence = MAX_CONFIDENCE;

                // Extra check here for void(void) functions, they should get minimum confidence since this
                // is the signature PDB uses when it doesn't actually know the signature
                if ty.type_class() == TypeClass::FunctionTypeClass {
                    if let Some(ret) = ty.return_value() {
                        if ret.contents.type_class() == TypeClass::VoidTypeClass {
                            if let Some(params) = ty.parameters() {
                                if params.len() == 0 {
                                    confidence = 0;
                                }
                            }
                        }
                    }
                }

                // Also array of bare function pointers (often seen in vtables)
                // These should not be marked confidently, as they don't actually know
                // the types of their contents

                if ty.type_class() == TypeClass::ArrayTypeClass {
                    if let Some(ptr) = ty.element_type() {
                        if ptr.contents.type_class() == TypeClass::PointerTypeClass {
                            if let Some(fun) = ptr.contents.target() {
                                if fun.contents.type_class() == TypeClass::FunctionTypeClass
                                    && fun
                                        .contents
                                        .parameters()
                                        .map(|pars| pars.len())
                                        .unwrap_or(0)
                                        == 0
                                {
                                    if let Some(ret) = fun.contents.return_value() {
                                        if ret.contents.type_class() == TypeClass::VoidTypeClass {
                                            confidence = 0;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                Ok(Some(Conf::new(ty, confidence)))
            }
            None => Ok(None),
        }
    }

    /// Parse and return a type by its index, used as lookup-or-parse
    fn handle_type_index(
        &mut self,
        ty: TypeIndex,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<&ParsedType>> {
        if let None = self.indexed_types.get(&ty) {
            self.log(|| format!("Parsing Type {:x?} ", ty));

            match finder.find(ty).and_then(|item| item.parse()) {
                Ok(data) => {
                    self.type_stack.push(ty);
                    let handled = self.handle_type(&data, finder);
                    self.type_stack.pop();

                    match handled {
                        Ok(Some(parsed)) => {
                            self.log(|| format!("Type {} parsed into: {:?}", ty, parsed));
                            match &*parsed {
                                ParsedType::Named(name, parsed) => {
                                    // PDB does this thing where anonymous inner types are represented as
                                    // some_type::<anonymous-tag>
                                    if !Self::is_name_anonymous(name) {
                                        if let Some(_old) =
                                            self.named_types.insert(name.clone(), parsed.clone())
                                        {
                                            tracing::warn!("Found two types both named `{}`, only one will be used.", name);
                                        }
                                    }
                                }
                                _ => {}
                            }
                            self.indexed_types.insert(ty, *parsed);
                        }
                        e => {
                            self.log(|| format!("Error parsing type {}: {:x?}", ty, e));
                        }
                    }
                }
                Err(UnimplementedTypeKind(k)) if k != 0 => {
                    tracing::warn!("Not parsing unimplemented type {}: kind {:x?}", ty, k);
                }
                Err(e) => {
                    self.log(|| format!("Could not parse type: {}: {}", ty, e));
                }
            };
        }

        Ok(self.indexed_types.get(&ty))
    }

    /// Parse a new type's data
    fn handle_type(
        &mut self,
        data: &TypeData,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        match data {
            TypeData::Primitive(data) => Ok(self.handle_primitive_type(data, finder)?),
            TypeData::Class(data) => Ok(self.handle_class_type(data, finder)?),
            TypeData::Member(data) => Ok(self.handle_member_type(data, finder)?),
            TypeData::MemberFunction(data) => Ok(self.handle_member_function_type(data, finder)?),
            TypeData::OverloadedMethod(data) => {
                Ok(self.handle_overloaded_method_type(data, finder)?)
            }
            TypeData::Method(data) => Ok(self.handle_method_type(data, finder)?),
            TypeData::StaticMember(data) => Ok(self.handle_static_member_type(data, finder)?),
            TypeData::Nested(data) => Ok(self.handle_nested_type(data, finder)?),
            TypeData::BaseClass(data) => Ok(self.handle_base_class_type(data, finder)?),
            TypeData::VirtualBaseClass(data) => {
                Ok(self.handle_virtual_base_class_type(data, finder)?)
            }
            TypeData::VirtualFunctionTable(data) => {
                Ok(self.handle_virtual_function_table_type(data, finder)?)
            }
            TypeData::VirtualTableShape(data) => {
                Ok(self.handle_virtual_table_shape_type(data, finder)?)
            }
            TypeData::VirtualFunctionTablePointer(data) => {
                Ok(self.handle_virtual_function_table_pointer_type(data, finder)?)
            }
            TypeData::Procedure(data) => Ok(self.handle_procedure_type(data, finder)?),
            TypeData::Pointer(data) => Ok(self.handle_pointer_type(data, finder)?),
            TypeData::Modifier(data) => Ok(self.handle_modifier_type(data, finder)?),
            TypeData::Enumeration(data) => Ok(self.handle_enumeration_type(data, finder)?),
            TypeData::Enumerate(data) => Ok(self.handle_enumerate_type(data, finder)?),
            TypeData::Array(data) => Ok(self.handle_array_type(data, finder)?),
            TypeData::Union(data) => Ok(self.handle_union_type(data, finder)?),
            TypeData::Bitfield(data) => Ok(self.handle_bitfield_type(data, finder)?),
            TypeData::FieldList(data) => Ok(self.handle_field_list_type(data, finder)?),
            TypeData::ArgumentList(data) => Ok(self.handle_argument_list_type(data, finder)?),
            TypeData::MethodList(data) => Ok(self.handle_method_list_type(data, finder)?),
            _ => Err(anyhow!("Unknown typedata")),
        }
    }

    /// Get the raw (mangled) name out of a type, if possible
    fn type_data_to_raw_name(data: &TypeData) -> Option<String> {
        match data {
            TypeData::Class(data) => Some(
                data.unique_name
                    .unwrap_or(data.name)
                    .to_string()
                    .to_string(),
            ),
            TypeData::Enumeration(data) => Some(
                data.unique_name
                    .unwrap_or(data.name)
                    .to_string()
                    .to_string(),
            ),
            TypeData::Union(data) => Some(
                data.unique_name
                    .unwrap_or(data.name)
                    .to_string()
                    .to_string(),
            ),
            _ => None,
        }
    }

    fn handle_primitive_type(
        &mut self,
        data: &PrimitiveType,
        _finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got Primitive type: {:x?}", data));
        let base = match data.kind {
            PrimitiveKind::NoType => Ok(Type::void()),
            PrimitiveKind::Void => Ok(Type::void()),
            PrimitiveKind::Char => Ok(Type::int(1, true)),
            PrimitiveKind::UChar => Ok(Type::int(1, false)),
            PrimitiveKind::RChar => Ok(Type::int(1, true)),
            PrimitiveKind::WChar => Ok(Type::wide_char(2)),
            PrimitiveKind::RChar16 => Ok(Type::wide_char(2)),
            PrimitiveKind::RChar32 => Ok(Type::wide_char(4)),
            PrimitiveKind::I8 => Ok(Type::int(1, true)),
            PrimitiveKind::U8 => Ok(Type::int(1, false)),
            PrimitiveKind::Short => Ok(Type::int(2, true)),
            PrimitiveKind::UShort => Ok(Type::int(2, false)),
            PrimitiveKind::I16 => Ok(Type::int(2, true)),
            PrimitiveKind::U16 => Ok(Type::int(2, false)),
            PrimitiveKind::Long => Ok(Type::int(4, true)),
            PrimitiveKind::ULong => Ok(Type::int(4, false)),
            PrimitiveKind::I32 => Ok(Type::int(4, true)),
            PrimitiveKind::U32 => Ok(Type::int(4, false)),
            PrimitiveKind::Quad => Ok(Type::int(8, true)),
            PrimitiveKind::UQuad => Ok(Type::int(8, false)),
            PrimitiveKind::I64 => Ok(Type::int(8, true)),
            PrimitiveKind::U64 => Ok(Type::int(8, false)),
            PrimitiveKind::Octa => Ok(Type::int(16, true)),
            PrimitiveKind::UOcta => Ok(Type::int(16, false)),
            PrimitiveKind::I128 => Ok(Type::int(16, true)),
            PrimitiveKind::U128 => Ok(Type::int(16, false)),
            PrimitiveKind::F16 => Ok(Type::float(2)),
            PrimitiveKind::F32 => Ok(Type::float(4)),
            PrimitiveKind::F32PP => Ok(Type::float(4)),
            PrimitiveKind::F48 => Ok(Type::float(6)),
            PrimitiveKind::F64 => Ok(Type::float(8)),
            PrimitiveKind::F80 => Ok(Type::float(10)),
            PrimitiveKind::F128 => Ok(Type::float(16)),
            PrimitiveKind::Complex32 => Err(anyhow!("Complex32 unimplmented")),
            PrimitiveKind::Complex64 => Err(anyhow!("Complex64 unimplmented")),
            PrimitiveKind::Complex80 => Err(anyhow!("Complex80 unimplmented")),
            PrimitiveKind::Complex128 => Err(anyhow!("Complex128 unimplmented")),
            PrimitiveKind::Bool8 => Ok(Type::int(1, false)),
            PrimitiveKind::Bool16 => Ok(Type::int(2, false)),
            PrimitiveKind::Bool32 => Ok(Type::int(4, false)),
            PrimitiveKind::Bool64 => Ok(Type::int(8, false)),
            // Hack: this isn't always defined
            PrimitiveKind::HRESULT => Ok(Type::named_type_from_type(
                "HRESULT",
                Type::int(4, true).as_ref(),
            )),
            _ => Err(anyhow!("Unknown type unimplmented")),
        }?;

        // TODO: Pointer suffix is not exposed
        match data.indirection {
            Some(Indirection::Near16) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
                &self.arch, &base,
            ))))),
            Some(Indirection::Far16) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
                &self.arch, &base,
            ))))),
            Some(Indirection::Huge16) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
                &self.arch, &base,
            ))))),
            Some(Indirection::Near32) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
                &self.arch, &base,
            ))))),
            Some(Indirection::Far32) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
                &self.arch, &base,
            ))))),
            Some(Indirection::Near64) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
                &self.arch, &base,
            ))))),
            Some(Indirection::Near128) => Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
                &self.arch, &base,
            ))))),
            None => Ok(Some(Box::new(ParsedType::Bare(base)))),
        }
    }

    fn handle_class_type(
        &mut self,
        data: &ClassType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got Class type: {:x?}", data));

        let raw_class_name = data.name.to_string();
        let class_name = raw_class_name.to_string();

        self.log(|| format!("Named: {}", class_name));

        if data.properties.forward_reference() {
            // Try and find it first
            if let Some(existing) = self.named_types.get(&class_name) {
                return Ok(Some(Box::new(ParsedType::Bare(
                    Type::named_type_from_type(class_name, existing),
                ))));
            }

            let ntr_class = match data.kind {
                ClassKind::Class => NamedTypeReferenceClass::ClassNamedTypeClass,
                ClassKind::Struct => NamedTypeReferenceClass::StructNamedTypeClass,
                ClassKind::Interface => NamedTypeReferenceClass::StructNamedTypeClass,
            };
            return Ok(Some(Box::new(ParsedType::Bare(Type::named_type(
                &NamedTypeReference::new(ntr_class, class_name),
            )))));
        }

        let struct_kind = match &data.kind {
            ClassKind::Class => StructureType::ClassStructureType,
            ClassKind::Struct => StructureType::StructStructureType,
            ClassKind::Interface => StructureType::StructStructureType,
        };

        let mut structure = StructureBuilder::new();
        structure.structure_type(struct_kind);
        structure.width(data.size);
        structure.packed(data.properties.packed());

        if let Some(fields) = data.fields {
            self.namespace_stack.push(class_name.clone());
            let success = self.parse_structure_fields(&mut structure, fields, finder);
            self.namespace_stack.pop();
            let _ = success?;
        }

        let new_type = Type::structure(structure.finalize().as_ref());
        Ok(Some(Box::new(ParsedType::Named(class_name, new_type))))
    }

    /// Handle all the structure field parsing for a given field list, putting the fields into a struct
    fn parse_structure_fields(
        &mut self,
        structure: &mut StructureBuilder,
        fields: TypeIndex,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<()> {
        let mut base_classes = vec![];
        let mut virt_methods = HashMap::new();
        let mut non_virt_methods = Vec::new();

        let mut members = vec![];

        match self.handle_type_index(fields, finder)? {
            Some(ParsedType::FieldList(fields)) => {
                for field in fields {
                    match field {
                        ParsedType::Member(member) => {
                            members.push(member.clone());
                        }
                        b @ ParsedType::BaseClass(..) => {
                            base_classes.push(b.clone());
                        }
                        b @ ParsedType::VBaseClass(..) => {
                            base_classes.push(b.clone());
                        }
                        ParsedType::Named(..) => {}
                        ParsedType::VTablePointer(_vt) => {}
                        ParsedType::Method(method) => {
                            if let Some(offset) = method.vtable_offset {
                                virt_methods.insert(
                                    offset,
                                    (method.name.clone(), method.method_type.clone()),
                                );
                            } else {
                                non_virt_methods
                                    .push((method.name.clone(), method.method_type.clone()));
                            }
                        }
                        ParsedType::OverloadedMethod(name, methods) => {
                            for method in methods {
                                if let Some(offset) = method.vtable_offset {
                                    virt_methods
                                        .insert(offset, (name.clone(), method.method_type.clone()));
                                }
                            }
                        }
                        f => {
                            return Err(anyhow!("Unexpected field type {:?}", f));
                        }
                    }
                }
            }
            Some(_) => {
                return Err(anyhow!(
                    "Structure fields list did not parse into member list?"
                ));
            }
            // No fields?
            None => {}
        }

        group_structure(
            &format!(
                "`{}`",
                self.namespace_stack
                    .last()
                    .ok_or_else(|| anyhow!("Expected class in ns stack"))?
            ),
            &members,
            structure,
        )?;

        let mut bases = vec![];

        for base_class in &base_classes {
            match base_class {
                ParsedType::BaseClass(name, base) => {
                    let ntr_class = match self.named_types.get(name) {
                        Some(ty) if ty.type_class() == TypeClass::StructureTypeClass => {
                            match ty.get_structure() {
                                Some(str)
                                    if str.structure_type()
                                        == StructureType::StructStructureType =>
                                {
                                    NamedTypeReferenceClass::StructNamedTypeClass
                                }
                                Some(str)
                                    if str.structure_type()
                                        == StructureType::ClassStructureType =>
                                {
                                    NamedTypeReferenceClass::ClassNamedTypeClass
                                }
                                _ => NamedTypeReferenceClass::StructNamedTypeClass,
                            }
                        }
                        _ => NamedTypeReferenceClass::StructNamedTypeClass,
                    };
                    bases.push(BaseStructure::new(
                        NamedTypeReference::new(ntr_class, name.clone()),
                        base.offset,
                        base.ty.contents.width(),
                    ));
                }
                ParsedType::VBaseClass(VirtualBaseClass {
                    base_name,
                    base_type,
                    base_offset,
                    ..
                }) => {
                    let ntr_class = match self.named_types.get(base_name) {
                        Some(ty) if ty.type_class() == TypeClass::StructureTypeClass => {
                            match ty.get_structure() {
                                Some(str)
                                    if str.structure_type()
                                        == StructureType::StructStructureType =>
                                {
                                    NamedTypeReferenceClass::StructNamedTypeClass
                                }
                                Some(str)
                                    if str.structure_type()
                                        == StructureType::ClassStructureType =>
                                {
                                    NamedTypeReferenceClass::ClassNamedTypeClass
                                }
                                _ => NamedTypeReferenceClass::StructNamedTypeClass,
                            }
                        }
                        _ => NamedTypeReferenceClass::StructNamedTypeClass,
                    };
                    bases.push(BaseStructure::new(
                        NamedTypeReference::new(ntr_class, base_name.clone()),
                        *base_offset,
                        base_type.width(),
                    ));
                    tracing::warn!(
                        "Class `{}` uses virtual inheritance. Type information may be inaccurate.",
                        self.namespace_stack
                            .last()
                            .ok_or_else(|| anyhow!("Expected class in ns stack"))?
                    );
                }
                e => return Err(anyhow!("Unexpected base class type: {:x?}", e)),
            }
        }

        if bases.len() > 1 {
            tracing::warn!(
                "Class `{}` has multiple base classes. Type information may be inaccurate.",
                self.namespace_stack
                    .last()
                    .ok_or_else(|| anyhow!("Expected class in ns stack"))?
            );
        }
        structure.base_structures(&bases);

        if self.settings.get_bool_with_opts(
            "pdb.features.generateVTables",
            &mut self.settings_query_opts,
        ) && !virt_methods.is_empty()
        {
            let mut vt = StructureBuilder::new();

            let mut vt_bases = vec![];

            for base_class in &base_classes {
                match base_class {
                    ParsedType::BaseClass(base_name, _base_type) => {
                        let vt_base_name = format!("{}::VTable", base_name);

                        match self.named_types.get(&vt_base_name) {
                            Some(vt_base_type)
                                if vt_base_type.type_class() == TypeClass::StructureTypeClass =>
                            {
                                let ntr_class =
                                    if vt_base_type.type_class() == TypeClass::StructureTypeClass {
                                        match vt_base_type.get_structure() {
                                            Some(str)
                                                if str.structure_type()
                                                    == StructureType::StructStructureType =>
                                            {
                                                NamedTypeReferenceClass::StructNamedTypeClass
                                            }
                                            Some(str)
                                                if str.structure_type()
                                                    == StructureType::ClassStructureType =>
                                            {
                                                NamedTypeReferenceClass::ClassNamedTypeClass
                                            }
                                            _ => NamedTypeReferenceClass::StructNamedTypeClass,
                                        }
                                    } else {
                                        NamedTypeReferenceClass::StructNamedTypeClass
                                    };
                                vt_bases.push(BaseStructure::new(
                                    NamedTypeReference::new(ntr_class, vt_base_name),
                                    0,
                                    vt_base_type.width(),
                                ));
                            }
                            e @ Some(_) => {
                                return Err(anyhow!("Unexpected vtable base class: {:?}", e))
                            }
                            None => {
                                // Parent might just not have a vtable
                            }
                        }
                    }
                    ParsedType::VBaseClass(_vbase) => {}
                    e => return Err(anyhow!("Unexpected base class type: {:x?}", e)),
                }
            }

            let mut min_width = 0;
            for base in &vt_bases {
                min_width = min_width.max(base.width);
            }

            vt.base_structures(&vt_bases);
            vt.propagates_data_var_refs(true);

            for (offset, (name, method)) in virt_methods {
                vt.insert(
                    &Conf::new(
                        Type::pointer(&self.arch, &Conf::new(method.method_type, MAX_CONFIDENCE)),
                        MAX_CONFIDENCE,
                    ),
                    &name,
                    offset as u64,
                    true,
                    MemberAccess::PublicAccess,
                    MemberScope::NoScope,
                );
                min_width = min_width.max((offset + self.arch.address_size()) as u64);
            }

            vt.width(min_width);

            let vt_type = Type::structure(vt.finalize().as_ref());
            // Need to insert a new named type for the vtable
            if self.namespace_stack.is_empty() {
                return Err(anyhow!("Expected class in ns stack"));
            }

            let class_name = self.namespace_stack.last().cloned().unwrap_or_default();
            let vt_name = format!("{}::VTable", class_name);
            self.named_types.insert(vt_name.clone(), vt_type.clone());

            let vt_pointer = Type::pointer(
                &self.arch,
                &Conf::new(
                    Type::named_type_from_type(vt_name, vt_type.as_ref()),
                    MAX_CONFIDENCE,
                ),
            );

            structure.insert(
                &Conf::new(vt_pointer, MAX_CONFIDENCE),
                "vtable",
                0,
                true,
                MemberAccess::PublicAccess,
                MemberScope::NoScope,
            );
        }

        Ok(())
    }

    fn handle_member_type(
        &mut self,
        data: &MemberType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got Member type: {:x?}", data));

        let member_name = data.name.to_string();
        let member_offset = data.offset;
        let member_attrs = data.attributes;

        let access = match member_attrs.access() {
            1 /* CV_private */ => MemberAccess::PrivateAccess,
            2 /* CV_protected */ => MemberAccess::ProtectedAccess,
            3 /* CV_public */ => MemberAccess::PublicAccess,
            _ => return Err(anyhow!("Unknown access"))
        };

        let scope = MemberScope::NoScope;

        match self.try_type_index_to_bare(data.field_type, finder, true)? {
            Some(ty) => Ok(Some(Box::new(ParsedType::Member(ParsedMember {
                ty: Conf::new(ty, MAX_CONFIDENCE),
                name: member_name.into_owned(),
                offset: member_offset,
                access,
                scope,
                bitfield_position: None,
                bitfield_size: None,
            })))),
            None => match self.handle_type_index(data.field_type, finder)? {
                Some(ParsedType::BitfieldType(bitfield)) => {
                    Ok(Some(Box::new(ParsedType::Member(ParsedMember {
                        ty: Conf::new(bitfield.ty.clone(), MAX_CONFIDENCE),
                        name: member_name.into_owned(),
                        offset: member_offset,
                        access,
                        scope,
                        bitfield_position: Some(bitfield.position),
                        bitfield_size: Some(bitfield.size),
                    }))))
                }
                e => Err(anyhow!("Unexpected member type: {:x?}", e)),
            },
        }
    }

    fn handle_member_function_type(
        &mut self,
        data: &MemberFunctionType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got MemberFunction type: {:x?}", data));
        let return_type = self.type_index_to_bare(data.return_type, finder, false)?;

        let class_name = match self.handle_type_index(data.class_type, finder)? {
            Some(ParsedType::Bare(ty)) if ty.type_class() == TypeClass::NamedTypeReferenceClass => {
                ty.get_named_type_reference()
                    .ok_or(anyhow!("Expected NTR to have NTR"))?
                    .name()
                    .to_string()
            }
            e => return Err(anyhow!("Unexpected class type: {:x?}", e)),
        };

        let this_pointer_type = if let Some(this_pointer_type) = data.this_pointer_type {
            match self.handle_type_index(this_pointer_type, finder)? {
                Some(ParsedType::Bare(ty)) => Some(ty.clone()),
                e => return Err(anyhow!("Unexpected this pointer type: {:x?}", e)),
            }
        } else {
            None
        };

        let mut arguments = match self.handle_type_index(data.argument_list, finder)? {
            Some(ParsedType::ArgumentList(args)) => args.clone(),
            e => return Err(anyhow!("Unexpected argument list type: {:x?}", e)),
        };

        // It looks like pdb stores varargs by having the final argument be void
        let mut is_varargs = false;
        if let Some(last) = arguments.pop() {
            if last.ty.contents.as_ref().type_class() == TypeClass::VoidTypeClass {
                is_varargs = true;
            } else {
                arguments.push(last);
            }
        }

        let mut fancy_return_value = ReturnValue {
            ty: Conf::new(return_type.clone(), MAX_CONFIDENCE),
            location: None,
        };
        let mut fancy_arguments = arguments.clone();

        let convention = self
            .cv_call_t_to_calling_convention(data.attributes.calling_convention())
            .map(|cc| Conf::new(cc, MAX_CONFIDENCE))
            .unwrap_or({
                if is_varargs {
                    Conf::new(self.cdecl_cc.clone(), MAX_CONFIDENCE)
                } else if this_pointer_type.is_some() {
                    Conf::new(self.thiscall_cc.clone(), MAX_CONFIDENCE)
                } else {
                    Conf::new(self.default_cc.clone(), 16)
                }
            });

        if data.attributes.cxx_return_udt()
            || !self.can_fit_in_register(data.return_type, finder, true)
        {
            // Return UDT??
            // This probably means the return value got pushed to the stack
            if self.settings.get_bool_with_opts(
                "pdb.features.passStructuresByValue",
                &mut self.settings_query_opts,
            ) {
                fancy_return_value.location =
                    self.indirect_return_value_location(&convention, &fancy_return_value);
            } else {
                fancy_return_value.ty = Conf::new(
                    Type::pointer(&self.arch, &return_type.clone()),
                    MAX_CONFIDENCE,
                );
                fancy_arguments.insert(
                    0,
                    FunctionParameter::new(
                        fancy_return_value.ty.clone(),
                        "__return".to_string(),
                        None,
                    ),
                );
            }
        }

        if let Some(this_ptr) = &this_pointer_type {
            self.insert_this_pointer(&mut fancy_arguments, this_ptr.clone())?;
        }

        let func = Type::function_with_opts(
            &Conf::new(return_type, MAX_CONFIDENCE),
            arguments.as_slice(),
            is_varargs,
            convention.clone(),
            Conf::new(0, 0),
        );

        let fancy_func = Type::function_with_opts(
            fancy_return_value,
            fancy_arguments.as_slice(),
            is_varargs,
            convention,
            Conf::new(0, 0),
        );

        Ok(Some(Box::new(ParsedType::MemberFunction(
            ParsedMemberFunction {
                attributes: data.attributes,
                class_name,
                method_type: fancy_func,
                raw_method_type: func,
                this_pointer_type,
                this_adjustment: data.this_adjustment as usize,
            },
        ))))
    }

    fn handle_overloaded_method_type(
        &mut self,
        data: &OverloadedMethodType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got OverloadedMethod type: {:x?}", data));
        // This is just a MethodList in disguise
        let method_list = match self.handle_type_index(data.method_list, finder)? {
            Some(ParsedType::MethodList(list)) => list.clone(),
            e => return Err(anyhow!("Unexpected method list type: {:x?}", e)),
        };

        Ok(Some(Box::new(ParsedType::OverloadedMethod(
            data.name.to_string().to_string(),
            method_list,
        ))))
    }

    fn handle_method_type(
        &mut self,
        data: &MethodType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got Method type: {:x?}", data));

        let member_function = match self.handle_type_index(data.method_type, finder)? {
            Some(ParsedType::MemberFunction(func)) => func.clone(),
            e => return Err(anyhow!("Unexpected method type {:?}", e)),
        };

        Ok(Some(Box::new(ParsedType::Method(ParsedMethod {
            attributes: data.attributes,
            name: data.name.to_string().to_string(),
            method_type: member_function,
            vtable_offset: data.vtable_offset.map(|o| o as usize),
        }))))
    }

    fn handle_static_member_type(
        &mut self,
        data: &StaticMemberType,
        _finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got StaticMember type: {:x?}", data));
        // TODO: Not handling these
        Ok(None)
    }

    fn handle_nested_type(
        &mut self,
        data: &NestedType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got Nested type: {:x?}", data));
        let mut class_name_ns = self.namespace_stack.clone();
        class_name_ns.push(data.name.to_string().to_string());
        let ty = self.type_index_to_bare(data.nested_type, finder, false)?;
        Ok(Some(Box::new(ParsedType::Named(
            class_name_ns.join("::"),
            ty,
        ))))
    }

    fn handle_base_class_type(
        &mut self,
        data: &BaseClassType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got BaseClass type: {:x?}", data));

        let base_offset = data.offset;
        let base_attrs = data.attributes;

        let (member_name, t) = match self.handle_type_index(data.base_class, finder)? {
            Some(ParsedType::Named(n, t)) => (n.clone(), t.clone()),
            Some(ParsedType::Bare(t)) if t.type_class() == TypeClass::NamedTypeReferenceClass => {
                let name = t
                    .get_named_type_reference()
                    .ok_or(anyhow!("Expected NTR to have NTR"))?
                    .name()
                    .to_string();
                (name, t.clone())
            }
            e => return Err(anyhow!("Unexpected base class type: {:x?}", e)),
        };

        // Try to resolve the full base type
        let resolved_type = match self.try_type_index_to_bare(data.base_class, finder, true)? {
            Some(ty) => Type::named_type_from_type(&member_name, ty.as_ref()),
            None => t.clone(),
        };

        let access = match base_attrs.access() {
            1 /* CV_private */ => MemberAccess::PrivateAccess,
            2 /* CV_protected */ => MemberAccess::ProtectedAccess,
            3 /* CV_public */ => MemberAccess::PublicAccess,
            _ => return Err(anyhow!("Unknown access"))
        };

        let scope = MemberScope::NoScope;
        Ok(Some(Box::new(ParsedType::BaseClass(
            member_name.clone(),
            StructureMember::new(
                Conf::new(resolved_type, MAX_CONFIDENCE),
                member_name,
                base_offset as u64,
                access,
                scope,
            ),
        ))))
    }

    fn handle_virtual_base_class_type(
        &mut self,
        data: &VirtualBaseClassType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got VirtualBaseClass type: {:x?}", data));

        let (n, ty) = match self.handle_type_index(data.base_class, finder)? {
            Some(ParsedType::Named(n, t)) => (n.clone(), t.clone()),
            Some(ParsedType::Bare(t)) if t.type_class() == TypeClass::NamedTypeReferenceClass => {
                let name = t
                    .get_named_type_reference()
                    .ok_or(anyhow!("Expected NTR to have NTR"))?
                    .name()
                    .to_string();
                (name, t.clone())
            }
            e => return Err(anyhow!("Unexpected base class type: {:x?}", e)),
        };

        // In addition to the base class, we also have a vbtable
        let vbptr_type = match self.handle_type_index(data.base_pointer, finder)? {
            Some(ParsedType::Bare(t)) => t.clone(),
            e => return Err(anyhow!("Unexpected virtual base pointer type: {:x?}", e)),
        };

        Ok(Some(Box::new(ParsedType::VBaseClass(VirtualBaseClass {
            base_name: n,
            base_type: ty,
            base_offset: data.base_pointer_offset as u64,
            base_table_type: vbptr_type,
            base_table_offset: data.virtual_base_offset as u64,
        }))))
    }

    fn handle_virtual_function_table_type(
        &mut self,
        data: &VirtualFunctionTableType,
        _finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got VirtualFunctionTableType type: {:x?}", data));
        Err(anyhow!("VirtualFunctionTableType unimplemented"))
    }

    fn handle_virtual_table_shape_type(
        &mut self,
        data: &VirtualTableShapeType,
        _finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got VirtualTableShapeType type: {:x?}", data));
        Ok(Some(Box::new(ParsedType::VTableShape(
            data.descriptors.clone(),
        ))))
    }

    fn handle_virtual_function_table_pointer_type(
        &mut self,
        data: &VirtualFunctionTablePointerType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got VirtualFunctionTablePointer type: {:x?}", data));
        let shape = match self.handle_type_index(data.table, finder)? {
            Some(ParsedType::VTablePointer(shape)) => shape.clone(),
            e => {
                return Err(anyhow!(
                    "Could not parse virtual function table pointer type: {:x?}",
                    e
                ))
            }
        };

        Ok(Some(Box::new(ParsedType::VTablePointer(shape))))
    }

    fn handle_procedure_type(
        &mut self,
        data: &ProcedureType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got Procedure type: {:x?}", data));
        let return_type = if let Some(return_type_index) = data.return_type {
            self.try_type_index_to_bare(return_type_index, finder, false)?
        } else {
            None
        }
        .map(|r| Conf::new(r, MAX_CONFIDENCE))
        .unwrap_or(Conf::new(Type::void(), 0));

        let mut arguments = match self.handle_type_index(data.argument_list, finder)? {
            Some(ParsedType::ArgumentList(args)) => args.clone(),
            e => return Err(anyhow!("Unexpected argument list type: {:x?}", e)),
        };

        // It looks like pdb stores varargs by having the final argument be void
        let mut is_varargs = false;
        if let Some(last) = arguments.pop() {
            if last.ty.contents.as_ref().type_class() == TypeClass::VoidTypeClass {
                is_varargs = true;
            } else {
                arguments.push(last);
            }
        }

        let mut fancy_return_value = ReturnValue {
            ty: return_type.clone(),
            location: None,
        };
        let mut fancy_arguments = arguments.clone();

        let convention = self
            .cv_call_t_to_calling_convention(data.attributes.calling_convention())
            .map(|cc| Conf::new(cc, MAX_CONFIDENCE))
            .unwrap_or(Conf::new(self.default_cc.clone(), 0));
        self.log(|| format!("Convention: {:?}", convention));

        let mut return_stacky = data.attributes.cxx_return_udt();
        if let Some(return_type_index) = data.return_type {
            return_stacky |= !self.can_fit_in_register(return_type_index, finder, true);
        }
        if return_stacky {
            // Stack return via a pointer in the first parameter
            if self.settings.get_bool_with_opts(
                "pdb.features.passStructuresByValue",
                &mut self.settings_query_opts,
            ) {
                fancy_return_value.location =
                    self.indirect_return_value_location(&convention, &fancy_return_value);
            } else {
                fancy_return_value.ty = Conf::new(
                    Type::pointer(&self.arch, &return_type.clone()),
                    MAX_CONFIDENCE,
                );
                fancy_arguments.insert(
                    0,
                    FunctionParameter::new(
                        fancy_return_value.ty.clone(),
                        "__return".to_string(),
                        None,
                    ),
                );
            }
        }

        let func = Type::function_with_opts(
            &return_type,
            arguments.as_slice(),
            is_varargs,
            convention.clone(),
            Conf::new(0, 0),
        );

        let fancy_func = Type::function_with_opts(
            fancy_return_value,
            fancy_arguments.as_slice(),
            is_varargs,
            convention,
            Conf::new(0, 0),
        );

        Ok(Some(Box::new(ParsedType::Procedure(ParsedProcedureType {
            method_type: fancy_func,
            raw_method_type: func,
        }))))
    }

    fn handle_pointer_type(
        &mut self,
        data: &PointerType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got Pointer type: {:x?}", data));
        let base = match self.try_type_index_to_bare(data.underlying_type, finder, false)? {
            Some(ty) => Some(ty.clone()),
            None => match self.handle_type_index(data.underlying_type, finder)? {
                Some(ParsedType::VTableShape(descriptors)) => {
                    return Ok(Some(Box::new(ParsedType::VTablePointer(
                        descriptors.clone(),
                    ))));
                }
                _ => None,
            },
        };

        if let Some(base) = base {
            Ok(Some(Box::new(ParsedType::Bare(Type::pointer(
                &self.arch, &base,
            )))))
        } else {
            Ok(None)
        }
    }

    fn handle_modifier_type(
        &mut self,
        data: &ModifierType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got Modifier type: {:x?}", data));
        let base = self.try_type_index_to_bare(data.underlying_type, finder, false)?;

        if let Some(base) = base {
            let builder = TypeBuilder::new(base.as_ref());
            builder.set_const(data.constant);
            builder.set_volatile(data.volatile);
            Ok(Some(Box::new(ParsedType::Bare(builder.finalize()))))
        } else {
            Ok(None)
        }
    }

    fn handle_enumeration_type(
        &mut self,
        data: &EnumerationType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got Enumeration type: {:x?}", data));

        let raw_enum_name = data.name.to_string();
        let enum_name = raw_enum_name.to_string();
        self.log(|| format!("Named: {}", enum_name));

        if data.properties.forward_reference() {
            // Try and find it first
            if let Some(existing) = self.named_types.get(&enum_name) {
                return Ok(Some(Box::new(ParsedType::Bare(
                    Type::named_type_from_type(enum_name, existing),
                ))));
            }

            let ntr_class = NamedTypeReferenceClass::EnumNamedTypeClass;
            return Ok(Some(Box::new(ParsedType::Bare(Type::named_type(
                &NamedTypeReference::new(ntr_class, enum_name),
            )))));
        }

        let mut enumeration = EnumerationBuilder::new();

        match self.handle_type_index(data.fields, finder)? {
            Some(ParsedType::FieldList(fields)) => {
                for field in fields {
                    match field {
                        ParsedType::Enumerate(member) => {
                            enumeration.insert(&member.name, member.value);
                        }
                        e => return Err(anyhow!("Unexpected enumerate member: {:?}", e)),
                    }
                }
            }
            // No fields?
            None => {}
            e => return Err(anyhow!("Unexpected enumeration field list: {:?}", e)),
        }

        let underlying = match self.handle_type_index(data.underlying_type, finder)? {
            Some(ParsedType::Bare(ty)) => ty.clone(),
            e => return Err(anyhow!("Making enumeration from unexpected type: {:x?}", e)),
        };

        let new_type = Type::enumeration(
            enumeration.finalize().as_ref(),
            // TODO: This looks bad, look at the comment in [`Type::width`].
            (underlying.width() as usize).try_into()?,
            underlying.is_signed().contents,
        );

        Ok(Some(Box::new(ParsedType::Named(enum_name, new_type))))
    }

    fn handle_enumerate_type(
        &mut self,
        data: &EnumerateType,
        _finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got Enumerate type: {:x?}", data));
        Ok(Some(Box::new(ParsedType::Enumerate(EnumerationMember {
            name: data.name.to_string().to_string(),
            value: match data.value {
                Variant::U8(v) => v as u64,
                Variant::U16(v) => v as u64,
                Variant::U32(v) => v as u64,
                Variant::U64(v) => v as u64,
                Variant::I8(v) => (v as i64) as u64,
                Variant::I16(v) => (v as i64) as u64,
                Variant::I32(v) => (v as i64) as u64,
                Variant::I64(v) => (v as i64) as u64,
            },
            default: false,
        }))))
    }

    fn handle_array_type(
        &mut self,
        data: &ArrayType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got Array type: {:x?}", data));
        // PDB stores array sizes as TOTAL bytes not element count
        // So we need to look up the original type's size to know how many there are
        let base = self.try_type_index_to_bare(data.element_type, finder, true)?;

        if let Some(base) = base {
            let mut new_type = base;
            if new_type.width() == 0 {
                if new_type.width() == 0 {
                    return Err(anyhow!(
                        "Cannot calculate array of 0-size elements: {}",
                        new_type
                    ));
                }
            }

            let mut counts = data
                .dimensions
                .iter()
                .map(|t| *t as u64)
                .collect::<Vec<_>>();
            for i in 0..counts.len() {
                for j in i..counts.len() {
                    if counts[j] % new_type.width() != 0 {
                        return Err(anyhow!(
                            "Array stride {} is not a multiple of element {} size {}",
                            counts[j],
                            new_type,
                            new_type.width()
                        ));
                    }
                    counts[j] /= new_type.width();
                }

                new_type = Type::array(&new_type, counts[i]);
            }

            Ok(Some(Box::new(ParsedType::Bare(new_type))))
        } else {
            Ok(None)
        }
    }

    fn handle_union_type(
        &mut self,
        data: &UnionType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got Union type: {:x?}", data));

        let raw_union_name = data.name.to_string();
        let union_name = raw_union_name.to_string();
        self.log(|| format!("Named: {}", union_name));

        if data.properties.forward_reference() {
            // Try and find it first
            if let Some(existing) = self.named_types.get(&union_name) {
                return Ok(Some(Box::new(ParsedType::Bare(
                    Type::named_type_from_type(union_name, existing),
                ))));
            }

            let ntr_class = NamedTypeReferenceClass::UnionNamedTypeClass;
            return Ok(Some(Box::new(ParsedType::Bare(Type::named_type(
                &NamedTypeReference::new(ntr_class, union_name),
            )))));
        }

        let mut structure = StructureBuilder::new();
        structure.structure_type(StructureType::UnionStructureType);
        structure.width(data.size);

        self.namespace_stack.push(union_name.to_string());
        let _success = self.parse_union_fields(&mut structure, data.fields, finder);
        self.namespace_stack.pop();

        let new_type = Type::structure(structure.finalize().as_ref());
        Ok(Some(Box::new(ParsedType::Named(union_name, new_type))))
    }

    /// Parse the fields in a union's field list
    fn parse_union_fields(
        &mut self,
        structure: &mut StructureBuilder,
        fields: TypeIndex,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<()> {
        let mut union_groups = vec![];
        let mut last_union_group = u64::MAX;

        match self.handle_type_index(fields, finder) {
            Ok(Some(ParsedType::FieldList(fields))) => {
                for field in fields {
                    match field {
                        ParsedType::Member(member) => {
                            if member.offset <= last_union_group {
                                union_groups.push(vec![]);
                            }
                            last_union_group = member.offset;
                            union_groups
                                .last_mut()
                                .expect("invariant")
                                .push(member.clone());
                        }
                        ParsedType::Method(..) => {}
                        ParsedType::Named(..) => {}
                        e => return Err(anyhow!("Unexpected union member type {:?}", e)),
                    }
                }
            }
            e => return Err(anyhow!("Unexpected union field list type {:?}", e)),
        }

        for (i, group) in union_groups.into_iter().enumerate() {
            if group.len() == 1 {
                structure.insert(
                    &group[0].ty,
                    &group[0].name,
                    group[0].offset,
                    false,
                    group[0].access,
                    group[0].scope,
                );
            } else {
                let mut inner_struct = StructureBuilder::new();
                for member in group {
                    inner_struct.insert(
                        &member.ty,
                        &member.name,
                        member.offset,
                        false,
                        member.access,
                        member.scope,
                    );
                }
                structure.insert(
                    &Conf::new(
                        Type::structure(inner_struct.finalize().as_ref()),
                        MAX_CONFIDENCE,
                    ),
                    &format!("__inner{:x}", i),
                    0,
                    false,
                    MemberAccess::PublicAccess,
                    MemberScope::NoScope,
                );
            }
        }

        Ok(())
    }

    fn handle_bitfield_type(
        &mut self,
        data: &BitfieldType,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got Bitfield type: {:x?}", data));
        Ok(self
            .try_type_index_to_bare(data.underlying_type, finder, true)?
            .map(|ty| {
                Box::new(ParsedType::BitfieldType(ParsedBitfieldType {
                    size: data.length as u64,
                    position: data.position as u64,
                    ty,
                }))
            }))
    }

    fn handle_field_list_type(
        &mut self,
        data: &FieldList,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got FieldList type: {:x?}", data));

        let mut fields = vec![];
        for (i, field) in data.fields.iter().enumerate() {
            match self.handle_type(field, finder)? {
                Some(f) => {
                    self.log(|| format!("Inner field {} parsed into {:?}", i, f));
                    fields.push(*f);
                }
                None => {
                    self.log(|| format!("Inner field {} parsed into None", i));
                }
            }
        }

        if let Some(cont) = data.continuation {
            match self.handle_type_index(cont, finder)? {
                Some(ParsedType::FieldList(cont_fields)) => {
                    fields.extend(cont_fields.clone());
                }
                None => {}
                f => {
                    return Err(anyhow!("Unexpected field list continuation {:?}", f));
                }
            }
        }
        Ok(Some(Box::new(ParsedType::FieldList(fields))))
    }

    fn handle_argument_list_type(
        &mut self,
        data: &ArgumentList,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got ArgumentList type: {:x?}", data));
        let mut args = vec![];
        for &arg in data.arguments.iter() {
            match self.try_type_index_to_bare(arg, finder, false)? {
                Some(ty) => {
                    // On x86_32, structures are stored on the stack directly
                    // On x64, they are put into pointers if they are not a int size
                    if self.arch.address_size() == 4
                        || Self::size_can_fit_in_register(ty.width())
                        || self.settings.get_bool_with_opts(
                            "pdb.features.passStructuresByValue",
                            &mut self.settings_query_opts,
                        )
                    {
                        args.push(FunctionParameter::new(
                            Conf::new(ty.clone(), MAX_CONFIDENCE),
                            "".to_string(),
                            None,
                        ));
                    } else {
                        args.push(FunctionParameter::new(
                            Conf::new(Type::pointer(self.arch.as_ref(), &ty), MAX_CONFIDENCE),
                            "".to_string(),
                            None,
                        ));
                    }
                }
                e => {
                    return Err(anyhow!("Unexpected argument type {:?}", e));
                }
            }
        }
        Ok(Some(Box::new(ParsedType::ArgumentList(args))))
    }

    fn handle_method_list_type(
        &mut self,
        data: &MethodList,
        finder: &mut ItemFinder<TypeIndex>,
    ) -> Result<Option<Box<ParsedType>>> {
        self.log(|| format!("Got MethodList type: {:x?}", data));

        let mut list = vec![];
        for method in &data.methods {
            match self.handle_type_index(method.method_type, finder)? {
                Some(ParsedType::MemberFunction(func)) => {
                    list.push(ParsedMethodListEntry {
                        attributes: method.attributes,
                        method_type: func.clone(),
                        vtable_offset: method.vtable_offset.map(|o| o as usize),
                    });
                }
                e => return Err(anyhow!("Unexpected method list entry: {:?}", e)),
            }
        }

        Ok(Some(Box::new(ParsedType::MethodList(list))))
    }

    /// Given a type index, get a bare binja type (or fail if not found)
    /// Optionally, set fully_resolve to true to parse and get the real type back in the case of NTRs
    fn type_index_to_bare(
        &mut self,
        index: TypeIndex,
        finder: &mut ItemFinder<TypeIndex>,
        fully_resolve: bool,
    ) -> Result<Ref<Type>> {
        match self.try_type_index_to_bare(index, finder, fully_resolve)? {
            Some(ty) => Ok(ty),
            None => Err(anyhow!("Unresolved expected type {:?}", index)),
        }
    }

    /// Given a type index, try to get a bare binja type
    /// Optionally, set fully_resolve to true to parse and get the real type back in the case of NTRs
    fn try_type_index_to_bare(
        &mut self,
        index: TypeIndex,
        finder: &mut ItemFinder<TypeIndex>,
        fully_resolve: bool,
    ) -> Result<Option<Ref<Type>>> {
        let (mut type_, inner) = match self.handle_type_index(index, finder)? {
            Some(ParsedType::Bare(ty)) => (ty.clone(), None),
            Some(ParsedType::Named(name, ty)) => (
                Type::named_type_from_type(name.to_owned(), ty),
                Some(ty.clone()),
            ),
            Some(ParsedType::Procedure(ParsedProcedureType { method_type, .. })) => {
                (method_type.clone(), Some(method_type.clone()))
            }
            Some(ParsedType::MemberFunction(ParsedMemberFunction { method_type, .. })) => {
                (method_type.clone(), Some(method_type.clone()))
            }
            Some(ParsedType::Member(ParsedMember { ty, .. })) => {
                (ty.contents.clone(), Some(ty.contents.clone()))
            }
            _ => return Ok(None),
        };

        if type_.type_class() == TypeClass::NamedTypeReferenceClass {
            if type_.width() == 0 {
                // Replace empty NTR with fully parsed NTR, if we can
                let name = type_
                    .get_named_type_reference()
                    .ok_or(anyhow!("expected ntr"))?
                    .name()
                    .to_string();
                if let Some(full_ntr) = self.named_types.get(&name) {
                    type_ = Type::named_type_from_type(name, full_ntr.as_ref());
                }
            }
        }

        if !fully_resolve {
            return Ok(Some(type_));
        }

        if type_.type_class() == TypeClass::NamedTypeReferenceClass {
            if type_.width() == 0 {
                // Look up raw name of this type
                if let Ok(raw) = finder.find(index) {
                    if let Ok(parsed) = raw.parse() {
                        // Have to use raw name here because self.full_type_indices uses raw name
                        // for some reason
                        if let Some(raw_name) = Self::type_data_to_raw_name(&parsed) {
                            if let Some(&full_index) = self.full_type_indices.get(&raw_name) {
                                if self
                                    .type_stack
                                    .iter()
                                    .find(|&&idx| idx == full_index)
                                    .is_none()
                                {
                                    if full_index != index {
                                        return self.try_type_index_to_bare(
                                            full_index,
                                            finder,
                                            fully_resolve,
                                        );
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        if type_.type_class() == TypeClass::NamedTypeReferenceClass {
            // PDB does this thing where anonymous inner types are represented as
            // some_type::<anonymous-tag>
            let name = type_
                .get_named_type_reference()
                .ok_or(anyhow!("expected ntr"))?
                .name()
                .to_string();
            if Self::is_name_anonymous(&name) {
                if let Some(inner) = inner.as_ref() {
                    type_ = inner.clone();
                } else {
                    // Look up raw name of this type
                    if let Ok(raw) = finder.find(index) {
                        if let Ok(parsed) = raw.parse() {
                            // Have to use raw name here because self.full_type_indices uses raw name
                            // for some reason
                            if let Some(raw_name) = Self::type_data_to_raw_name(&parsed) {
                                if let Some(&full_index) = self.full_type_indices.get(&raw_name) {
                                    if self
                                        .type_stack
                                        .iter()
                                        .find(|&&idx| idx == full_index)
                                        .is_none()
                                    {
                                        if full_index != index {
                                            return self.try_type_index_to_bare(
                                                full_index,
                                                finder,
                                                fully_resolve,
                                            );
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        Ok(Some(type_))
    }

    /// Is this name one of the stupid microsoft unnamed type names
    fn is_name_anonymous(name: &String) -> bool {
        match name.split("::").last() {
            Some(item) if item == "<anonymous-tag>" => true,
            Some(item) if item.contains("<unnamed-") => true,
            _ => false,
        }
    }

    /// Find a calling convention in the platform
    pub(crate) fn find_calling_convention(
        platform: &Platform,
        name: &str,
    ) -> Option<Ref<CoreCallingConvention>> {
        platform
            .calling_conventions()
            .iter()
            .find(|c| c.name().as_str() == name)
            .map(|g| g.clone())
    }

    /// Convert pdb calling convention enum to binja
    fn cv_call_t_to_calling_convention(&self, cv: u8) -> Option<Ref<CoreCallingConvention>> {
        match CV_call_t::try_from(cv) {
            Ok(CV_call_t::NEAR_FAST) | Ok(CV_call_t::FAR_FAST) => {
                self.platform.get_fastcall_calling_convention()
            }
            Ok(CV_call_t::NEAR_STD) | Ok(CV_call_t::FAR_STD) => {
                self.platform.get_stdcall_calling_convention()
            }
            Ok(CV_call_t::NEAR_C) | Ok(CV_call_t::FAR_C) => {
                self.platform.get_cdecl_calling_convention()
            }
            Ok(CV_call_t::THISCALL) => {
                Self::find_calling_convention(self.platform.as_ref(), "thiscall")
            }
            Ok(CV_call_t::NEAR_PASCAL) | Ok(CV_call_t::FAR_PASCAL) => {
                Self::find_calling_convention(self.platform.as_ref(), "pascal")
            }
            Ok(CV_call_t::NEAR_SYS) | Ok(CV_call_t::FAR_SYS) => {
                Self::find_calling_convention(self.platform.as_ref(), "sys")
            }
            Ok(CV_call_t::MIPSCALL) => {
                Self::find_calling_convention(self.platform.as_ref(), "mipscall")
            }
            Ok(CV_call_t::ALPHACALL) => {
                Self::find_calling_convention(self.platform.as_ref(), "alphacall")
            }
            Ok(CV_call_t::PPCCALL) => {
                Self::find_calling_convention(self.platform.as_ref(), "ppccall")
            }
            Ok(CV_call_t::SHCALL) => {
                Self::find_calling_convention(self.platform.as_ref(), "shcall")
            }
            Ok(CV_call_t::ARMCALL) => {
                Self::find_calling_convention(self.platform.as_ref(), "armcall")
            }
            Ok(CV_call_t::AM33CALL) => {
                Self::find_calling_convention(self.platform.as_ref(), "am33call")
            }
            Ok(CV_call_t::TRICALL) => {
                Self::find_calling_convention(self.platform.as_ref(), "tricall")
            }
            Ok(CV_call_t::SH5CALL) => {
                Self::find_calling_convention(self.platform.as_ref(), "sh5call")
            }
            Ok(CV_call_t::M32RCALL) => {
                Self::find_calling_convention(self.platform.as_ref(), "m32rcall")
            }
            Ok(CV_call_t::NEAR_VECTOR) => {
                Self::find_calling_convention(self.platform.as_ref(), "vectorcall")
            }
            _ => None,
        }
    }

    /// Insert an argument for the thisptr in a function param list
    fn insert_this_pointer(
        &self,
        parameters: &mut Vec<FunctionParameter>,
        this_type: Ref<Type>,
    ) -> Result<()> {
        parameters.insert(
            0,
            FunctionParameter::new(
                Conf::new(this_type, MAX_CONFIDENCE),
                "this".to_string(),
                None,
            ),
        );

        Ok(())
    }

    /// Does this type get returned in rax? Or should we put it on the stack?
    pub fn can_fit_in_register(
        &mut self,
        index: TypeIndex,
        finder: &mut ItemFinder<TypeIndex>,
        treat_references_like_pointers: bool,
    ) -> bool {
        // TLDR "This is impossible so we're making a best-guess"
        // GET READY OKAY

        // "A scalar return value that can fit into 64 bits, including the __m64 type, is returned
        // through RAX. Non-scalar types including floats, doubles, and vector types such as __m128,
        // __m128i, __m128d are returned in XMM0. The state of unused bits in the value returned
        // in RAX or XMM0 is undefined.

        // "User-defined types can be returned by value from global functions and static member
        // functions. To return a user-defined type by value in RAX, it must have a length of
        // 1, 2, 4, 8, 16, 32, or 64 bits. It must also have no user-defined constructor, destructor,
        // or copy assignment operator. It can have no private or protected non-static data members,
        // and no non-static data members of reference type. It can't have base classes or virtual
        // functions. And, it can only have data members that also meet these requirements.
        // (This definition is essentially the same as a C++03 POD type. Because the definition has
        // changed in the C++11 standard, we don't recommend using std::is_pod for this test.)
        // Otherwise, the caller must allocate memory for the return value and pass a pointer to it
        // as the first argument. The remaining arguments are then shifted one argument to the right.
        // The same pointer must be returned by the callee in RAX."

        // - length of 1, 2, 4, 8, 16, 32, or 64 bits
        // - no user-defined constructor
        // - no user-defined destructor
        // - no user-defined copy assignment operator
        // - no private data members
        // - no protected data members
        // - no reference data members
        // - no base classes
        // - no virtual functions

        // This one is incorrect, so we're not including it:
        // - all members meet these requirements
        // https://godbolt.org/z/hsTxrxq9c extremely cool

        // Are we going to implement all of this?
        // No? We're just going to do something close and leave it to the users to figure out the rest
        // There's no way I'm digging through all nonsense

        // Glenn: "Never mind this got deleted... MS has done it again"
        // After a quick GitHub discussion (https://github.com/MicrosoftDocs/cpp-docs/issues/4152)
        // I've determined this is unknowable.
        // Microsoft does it again!!!!

        if let Some(&returnable) = self.type_default_returnable.get(&index) {
            returnable
        } else {
            let returnable =
                self.can_fit_in_register_impl(index, finder, treat_references_like_pointers);
            self.log(|| format!("Type {} is default returnable: {}", index, returnable));
            self.type_default_returnable.insert(index, returnable);
            returnable
        }
    }

    fn size_can_fit_in_register(size: u64) -> bool {
        matches!(size, 0 | 1 | 2 | 4 | 8)
    }

    // Memoized... because this has gotta be real slow
    fn can_fit_in_register_impl(
        &mut self,
        index: TypeIndex,
        finder: &mut ItemFinder<TypeIndex>,
        treat_references_like_pointers: bool,
    ) -> bool {
        let ty = match finder.find(index) {
            Ok(item) => match item.parse() {
                Ok(ty) => ty,
                Err(_) => return false,
            },
            Err(_) => return false,
        };

        fn get_fields<'a>(
            index: TypeIndex,
            finder: &mut ItemFinder<'a, TypeIndex>,
        ) -> Result<Vec<TypeData<'a>>> {
            match finder.find(index).and_then(|fields| fields.parse()) {
                Ok(TypeData::FieldList(fields)) => {
                    if let Some(cont) = fields.continuation {
                        Ok(fields
                            .fields
                            .into_iter()
                            .chain(get_fields(cont, finder)?)
                            .collect::<Vec<_>>())
                    } else {
                        Ok(fields.fields)
                    }
                }
                _ => Err(anyhow!("can't lookup fields")),
            }
        }

        match ty {
            TypeData::Primitive(_) => true,
            TypeData::Pointer(p) => match p.attributes.pointer_mode() {
                PointerMode::Pointer => true,
                PointerMode::Member => true,
                PointerMode::MemberFunction => true,
                // - no reference data members
                PointerMode::LValueReference => treat_references_like_pointers,
                PointerMode::RValueReference => treat_references_like_pointers,
            },
            TypeData::Array(a) => {
                Self::size_can_fit_in_register(*a.dimensions.last().unwrap_or(&0) as u64)
                    && self.can_fit_in_register(a.element_type, finder, false)
            }
            TypeData::Modifier(m) => {
                self.can_fit_in_register(m.underlying_type, finder, treat_references_like_pointers)
            }
            TypeData::Enumeration(e) => self.can_fit_in_register(e.underlying_type, finder, false),
            TypeData::Class(c) => {
                if c.properties.forward_reference() {
                    if let Some(raw_name) = c.unique_name {
                        if let Some(&full) = self
                            .full_type_indices
                            .get(&raw_name.to_string().to_string())
                        {
                            return self.can_fit_in_register(
                                full,
                                finder,
                                treat_references_like_pointers,
                            );
                        }
                    }
                    // Can't look up, assume not
                    return false;
                }

                // - length of 1, 2, 4, 8, 16, 32, or 64 bits
                if !Self::size_can_fit_in_register(c.size) {
                    return false;
                }

                // - no user-defined constructor
                // - no user-defined destructor
                // - no user-defined copy assignment operator
                if c.properties.constructors() || c.properties.overloaded_assignment() {
                    return false;
                }

                // - no base classes
                if c.derived_from.is_some() {
                    return false;
                }
                // - no virtual functions
                if c.vtable_shape.is_some() {
                    return false;
                }

                let fields = if let Some(fields_idx) = c.fields {
                    if let Ok(fields) = get_fields(fields_idx, finder) {
                        fields
                    } else {
                        return false;
                    }
                } else {
                    // No fields?
                    return true;
                };

                for field in fields {
                    match field {
                        TypeData::Member(m) => {
                            // - no private data members
                            // - no protected data members
                            if m.attributes.access() == 1 || m.attributes.access() == 2 {
                                return false;
                            }
                        }
                        TypeData::OverloadedMethod(m) => {
                            match finder.find(m.method_list).and_then(|l| l.parse()) {
                                Ok(TypeData::MethodList(list)) => {
                                    for m in list.methods {
                                        // - no virtual functions
                                        if m.attributes.is_virtual() {
                                            return false;
                                        }
                                    }
                                }
                                _ => return false,
                            }
                        }
                        TypeData::Method(m) => {
                            // - no virtual functions
                            if m.attributes.is_virtual() {
                                return false;
                            }
                        }
                        // - no base classes
                        TypeData::BaseClass(_) => return false,
                        TypeData::VirtualBaseClass(_) => return false,
                        TypeData::VirtualFunctionTable(_) => return false,
                        TypeData::VirtualTableShape(_) => return false,
                        TypeData::VirtualFunctionTablePointer(_) => return false,
                        _ => {}
                    }
                }
                true
            }
            TypeData::Union(u) => {
                if u.properties.forward_reference() {
                    if let Some(raw_name) = u.unique_name {
                        if let Some(&full) = self
                            .full_type_indices
                            .get(&raw_name.to_string().to_string())
                        {
                            return self.can_fit_in_register(
                                full,
                                finder,
                                treat_references_like_pointers,
                            );
                        }
                    }
                    // Can't look up, assume not
                    return false;
                }

                // - length of 1, 2, 4, 8, 16, 32, or 64 bits
                if !Self::size_can_fit_in_register(u.size) {
                    return false;
                }

                // - no user-defined constructor
                // - no user-defined destructor
                // - no user-defined copy assignment operator
                if u.properties.constructors() || u.properties.overloaded_assignment() {
                    return false;
                }

                let fields = if let Ok(fields) = get_fields(u.fields, finder) {
                    fields
                } else {
                    return false;
                };

                for field in fields {
                    match field {
                        TypeData::Member(m) => {
                            // - no private data members
                            // - no protected data members
                            if m.attributes.access() == 1 || m.attributes.access() == 2 {
                                return false;
                            }
                        }
                        TypeData::OverloadedMethod(m) => {
                            match finder.find(m.method_list).and_then(|l| l.parse()) {
                                Ok(TypeData::MethodList(list)) => {
                                    for m in list.methods {
                                        // - no virtual functions
                                        if m.attributes.is_virtual() {
                                            return false;
                                        }
                                    }
                                }
                                _ => return false,
                            }
                        }
                        TypeData::Method(m) => {
                            // - no virtual functions
                            if m.attributes.is_virtual() {
                                return false;
                            }
                        }
                        // - no base classes
                        TypeData::BaseClass(_) => return false,
                        TypeData::VirtualBaseClass(_) => return false,
                        TypeData::VirtualFunctionTable(_) => return false,
                        TypeData::VirtualTableShape(_) => return false,
                        TypeData::VirtualFunctionTablePointer(_) => return false,
                        _ => {}
                    }
                }
                true
            }
            _ => false,
        }
    }

    /// Determines if there is a non-default location for an indirect return value and returns
    /// the location if there is one.
    fn indirect_return_value_location(
        &self,
        convention: &Conf<Ref<CoreCallingConvention>>,
        return_value: &ReturnValue,
    ) -> Option<Conf<ValueLocation>> {
        // Non-POD data types are always returned as indirect values. The calling convention
        // may not know this and try to place them in registers, so check the calling convention
        // to see if it wants to pass indirectly.
        // TODO: The structures themselves should have some kind of non-POD attribute so
        // that the calling convention can determine this by default
        let default_return_location = convention
            .contents
            .return_value_location(Some(self.bv), &return_value);
        if default_return_location.indirect {
            None
        } else {
            let variable = if let Some(reg) = convention.contents.int_arg_registers().get(0) {
                Variable::new(
                    VariableSourceType::RegisterVariableSourceType,
                    0,
                    reg.0 as i64,
                )
            } else {
                Variable::new(
                    VariableSourceType::StackVariableSourceType,
                    0,
                    self.arch.address_size() as i64,
                )
            };
            Some(Conf::new(
                ValueLocation {
                    components: vec![ValueLocationComponent {
                        variable,
                        offset: 0,
                        size: Some(return_value.ty.contents.width()),
                    }],
                    indirect: true,
                    returned_pointer: convention.contents.return_int_reg().map(|reg| {
                        Variable::new(
                            VariableSourceType::RegisterVariableSourceType,
                            0,
                            reg.0 as i64,
                        )
                    }),
                },
                MAX_CONFIDENCE,
            ))
        }
    }
}