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

//#CLRRuntimeHostInternal_GetImageVersionString
// External implementation of call to code:ICLRRuntimeHostInternal::GetImageVersionString.
// Implemented in clr.dll and mscordbi.dll.
HRESULT 
CLRRuntimeHostInternal_GetImageVersionString(
  __out_ecount(*pcchBuffer) 
    LPWSTR  wszBuffer, 
    DWORD * pcchBuffer);

//----------------------------------------------------------------------------------------------------
// The name prefixes used by WinMD to hide/unhide WinRT and CLR versions of RuntimeClasses.
//----------------------------------------------------------------------------------------------------

static const char s_szWinRTPrefix[] = "<WinRT>";
//static const size_t s_ncWinRTPrefix = sizeof(s_szWinRTPrefix) - 1;

static const char s_szCLRPrefix[] = "<CLR>"; 
static const size_t s_ncCLRPrefix = sizeof(s_szCLRPrefix) - 1;

// the public key token of the ecma key used by some framework assemblies (mscorlib, system, etc).
// note that some framework assemblies use a different key: b03f5f7f11d50a3a
static const BYTE s_pbFrameworkPublicKeyToken[] = {0xB7,0x7A,0x5C,0x56,0x19,0x34,0xE0,0x89};


//-----------------------------------------------------------------------------------------------------
// Returns:
//    S_OK:    if WinMD adapter should be used.
//    S_FALSE: if not
//-----------------------------------------------------------------------------------------------------
HRESULT CheckIfWinMDAdapterNeeded(IMDCommon *pRawMDCommon)
{
    HRESULT hr;
    _ASSERTE(pRawMDCommon != NULL);

    LPCSTR szMetadataVersionString = NULL;

#ifndef DACCESS_COMPILE
#ifdef _DEBUG
    //---------------------------------------------------------------------------------------------------------
    // set COMPLUS_INTERNAL_MD_WinMD_AssertOnIllegalUsage=1
    //
    //    to turn the WinMD adapter off universally.
    //---------------------------------------------------------------------------------------------------------
    if (CLRConfig::GetConfigValue(CLRConfig::INTERNAL_MD_WinMD_Disable))
    {
        hr = S_FALSE;
        goto ErrExit;
    }
#endif //_DEBUG
#endif

    //---------------------------------------------------------------------------------------------------------
    // This is the real check: activate WinMD based on metadata version string.
    //---------------------------------------------------------------------------------------------------------
    static const LPCSTR g_szWindowsRuntimeVersion = "WindowsRuntime ";
    IfFailGo(pRawMDCommon->GetVersionString(&szMetadataVersionString));
    if (0 == strncmp(szMetadataVersionString, g_szWindowsRuntimeVersion, strlen(g_szWindowsRuntimeVersion)))
    {
        hr = S_OK;
        goto ErrExit;
    }
    else
    {
        hr = S_FALSE;
        goto ErrExit;
    }

  ErrExit:
    return hr;
}


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

//
// Factory for WinMDAdapters. Caller must use "delete" to destroy adapter when done.
//
/*static*/ HRESULT WinMDAdapter::Create(IMDCommon *pRawMDCommon, /*[out]*/ WinMDAdapter **ppAdapter)
{
    HRESULT hr;
    LPWSTR        wszCorVersion = NULL;
    WinMDAdapter* pNewAdapter = NULL;
    ULONG numAssemblyRefs = 0;
    const char *szClrPortion = NULL;

    *ppAdapter = NULL;

    pNewAdapter = new (nothrow) WinMDAdapter(pRawMDCommon);
    if (!pNewAdapter)
    {
        IfFailGo(E_OUTOFMEMORY);
    }

    //------------------------------------------------------------------------------------------------
    // Create a stored string to hold our phantom metadata version string.
    //------------------------------------------------------------------------------------------------
    LPCSTR szVersion;
    IfFailGo(pRawMDCommon->GetVersionString(&szVersion));
    szClrPortion = strchr(szVersion, ';');
    if (szClrPortion)
    {
        pNewAdapter->m_scenario = kWinMDExp;
        szClrPortion++;
        
        // skip the "CLR" prefix if present
        if ((szClrPortion[0] == 'c' || szClrPortion[0] == 'C') &&
            (szClrPortion[1] == 'l' || szClrPortion[1] == 'L') &&
            (szClrPortion[2] == 'r' || szClrPortion[2] == 'R'))
        {
            szClrPortion += 3;
        }
        while (szClrPortion[0] == ' ')
        {
            szClrPortion++;
        }

        size_t ncClrPortion = strlen(szClrPortion);
        pNewAdapter->m_pRedirectedVersionString = new (nothrow) char[ncClrPortion + 1];
        IfNullGo(pNewAdapter->m_pRedirectedVersionString);
        memcpy(pNewAdapter->m_pRedirectedVersionString, szClrPortion, ncClrPortion + 1);
    }
    else
    {
        pNewAdapter->m_scenario = kWinMDNormal;
#ifndef DACCESS_COMPILE
        WCHAR wszCorVersion[_MAX_PATH];
        DWORD cchWszCorVersion = _countof(wszCorVersion);
        IfFailGo(CLRRuntimeHostInternal_GetImageVersionString (wszCorVersion, &cchWszCorVersion));
        MAKE_UTF8PTR_FROMWIDE_NOTHROW(szCorVersion, wszCorVersion);
        IfNullGo(szCorVersion);
        size_t nch = strlen(szCorVersion) + 1;
        pNewAdapter->m_pRedirectedVersionString = new (nothrow) char[nch];
        IfNullGo(pNewAdapter->m_pRedirectedVersionString);
        memcpy(pNewAdapter->m_pRedirectedVersionString, szCorVersion, nch);
#else
        pNewAdapter->m_pRedirectedVersionString = new (nothrow) char[1];
        pNewAdapter->m_pRedirectedVersionString[0] = 0;
#endif
    }


    //------------------------------------------------------------------------------------------------
    // Find an assemblyRef to mscorlib (required to exist in .winmd files precisely to make the adapter's job easier.
    //------------------------------------------------------------------------------------------------
    numAssemblyRefs = pNewAdapter->m_pRawMetaModelCommonRO->CommonGetRowCount(mdtAssemblyRef);
    pNewAdapter->m_assemblyRefMscorlib = 0;
    pNewAdapter->m_fReferencesMscorlibV4 = FALSE;
    for (ULONG rid = 1; rid <= numAssemblyRefs; rid++)
    {
        mdAssemblyRef mdar = mdtAssemblyRef|rid;
        LPCSTR arefName;
        USHORT usMajorVersion;
        IfFailGo(pNewAdapter->m_pRawMetaModelCommonRO->CommonGetAssemblyRefProps(mdar, &usMajorVersion, NULL, NULL, NULL, NULL, NULL, NULL, &arefName, NULL, NULL, NULL));
        
        // We check for legacy Core library name since Windows.winmd references mscorlib and not System.Private.CoreLib
        if (0 == strcmp(arefName, LegacyCoreLibName_A))
        {
            pNewAdapter->m_assemblyRefMscorlib = mdar;

            if (usMajorVersion == 4)
            {
                // Older WinMDExp used to incorrectly generate an assemblyRef to 4.0.0.0 mscorlib.
                // We use this flag to implement back-compat quirks.
                pNewAdapter->m_fReferencesMscorlibV4 = TRUE;
            }

            break;
        }
    }
    if (pNewAdapter->m_assemblyRefMscorlib == 0)
    {
        // .winmd files are required to have an assemblyRef to mscorlib.
        IfFailGo(COR_E_BADIMAGEFORMAT);
    }


    //------------------------------------------------------------------------------------------------
    // All initialization tasks done. 
    //------------------------------------------------------------------------------------------------
    *ppAdapter = pNewAdapter;
    hr = S_OK;

  ErrExit:
    delete wszCorVersion;
    if (FAILED(hr))
        delete pNewAdapter;
    return hr;
}


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

WinMDAdapter::WinMDAdapter(IMDCommon *pRawMDCommon)
  : m_typeRefTreatmentMemoTable(pRawMDCommon->GetMetaModelCommonRO()->CommonGetRowCount(mdtTypeRef), kTrNotYetInitialized)
  , m_typeDefTreatmentMemoTable(pRawMDCommon->GetMetaModelCommonRO()->CommonGetRowCount(mdtTypeDef), kTdNotYetInitialized)
  , m_methodDefTreatmentMemoTable(pRawMDCommon->GetMetaModelCommonRO()->CommonGetRowCount(mdtMethodDef), kMdNotYetInitialized)
  , m_redirectedCABlobsMemoTable(pRawMDCommon->GetMetaModelCommonRO()->CommonGetRowCount(mdtCustomAttribute), NULL)
  , m_redirectedMethodDefSigMemoTable(pRawMDCommon->GetMetaModelCommonRO()->CommonGetRowCount(mdtMethodDef), NULL)
  , m_redirectedFieldDefSigMemoTable(pRawMDCommon->GetMetaModelCommonRO()->CommonGetRowCount(mdtFieldDef), NULL)
  , m_redirectedMemberRefSigMemoTable(pRawMDCommon->GetMetaModelCommonRO()->CommonGetRowCount(mdtMemberRef), NULL)
  , m_redirectedPropertySigMemoTable(pRawMDCommon->GetMetaModelCommonRO()->CommonGetRowCount(mdtProperty), NULL)
  , m_redirectedTypeSpecSigMemoTable(pRawMDCommon->GetMetaModelCommonRO()->CommonGetRowCount(mdtTypeSpec), NULL)
  , m_redirectedMethodSpecSigMemoTable(pRawMDCommon->GetMetaModelCommonRO()->CommonGetRowCount(mdtMethodSpec), NULL)
  , m_mangledTypeNameTable(pRawMDCommon->GetMetaModelCommonRO()->CommonGetRowCount(mdtTypeDef), NULL)
  , m_extraAssemblyRefCount(-1)
{
    m_rawAssemblyRefCount = pRawMDCommon->GetMetaModelCommonRO()->CommonGetRowCount(mdtAssemblyRef);
    m_pRedirectedVersionString = NULL;
    m_pRawMetaModelCommonRO = pRawMDCommon->GetMetaModelCommonRO();
}

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

WinMDAdapter::~WinMDAdapter()
{
    delete m_pRedirectedVersionString;
}


//------------------------------------------------------------------------------
// Hides projected jupiter struct helper class & interfaces

struct HiddenWinRTTypeInfo
{
    LPCSTR szWinRTNamespace;
    LPCSTR szWinRTName;
};

#define DEFINE_PROJECTED_JUPITER_STRUCT(szWinRTNamespace, szWinRTName, szClrNamespace, szClrName, nClrAssemblyIndex, nContractAssemblyIndex, WinRTRedirectedTypeIndex, ClrRedirectedTypeIndex, fieldSizes) \
    {                                       \
        szWinRTNamespace,                   \
        szWinRTName "Helper"                \
    },

#define DEFINE_HIDDEN_WINRT_TYPE(szWinRTNamespace, szWinRTName) \
    {                                       \
        szWinRTNamespace,                   \
        szWinRTName                         \
    },

#define DEFINE_PROJECTED_TYPE(szWinRTNS, szWinRTName, szClrNS, szClrName, nClrAsmIdx, nContractAsmIdx, nWinRTIndex, nClrIndex, nWinMDTypeKind)

static const HiddenWinRTTypeInfo g_rgHiddenWinRTTypes[] = 
{
    #include "WinRTProjectedTypes.h"
};

#undef DEFINE_PROJECTED_JUPITER_STRUCT
#undef DEFINE_HIDDEN_WINRT_TYPE
#undef DEFINE_PROJECTED_TYPE

// Whether the WinRT type should be hidden from managed code
// Example: helper class for projected jupiter structs 
// (helper class interfaces are already private by default)
BOOL WinMDAdapter::IsHiddenWinRTType(LPCSTR szWinRTNamespace, LPCSTR szWinRTName)
{
    _ASSERTE(szWinRTNamespace && szWinRTName);
    
    for (UINT i = 0; i < _countof(g_rgHiddenWinRTTypes); i++)
    {
        if (0 == strcmp(szWinRTNamespace, g_rgHiddenWinRTTypes[i].szWinRTNamespace))
        {
            if (0 == strcmp(szWinRTName, g_rgHiddenWinRTTypes[i].szWinRTName))
            {
                return TRUE;
            }
        }
    }
    
    return FALSE;    
}

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

struct RedirectedTypeInfo
{
    LPCSTR szWinRTNamespace;
    LPCSTR szWinRTName;
    LPCSTR szWinRTFullName;
    LPCWSTR wszWinRTFullName;
    LPCSTR szClrNamespace;
    LPCSTR szClrName;
    LPCSTR szClrFullName;
    LPCWSTR wszClrFullName;
    const WinMDAdapter::FrameworkAssemblyIndex nClrAssemblyIndex;
    const WinMDAdapter::ContractAssemblyIndex nContractAssemblyIndex;
    const WinMDAdapter::WinMDTypeKind nTypeKind;
#ifdef _DEBUG
    // Indexes for verification of constants RedirectedTypeIndex_*
    const UINT    dbg_nIndex1;
    const UINT    dbg_nIndex2;
#endif
};

#define DEFINE_PROJECTED_TYPE(szWinRTNS, szWinRTName, szClrNS, szClrName, nClrAsmIdx, nContractAsmIdx, nWinRTIndex, nClrIndex, nWinMDTypeKind) \
    { \
        szWinRTNS, \
        szWinRTName, \
        szWinRTNS "." szWinRTName, \
        L##szWinRTNS W(".") L##szWinRTName, \
        szClrNS, \
        szClrName, \
        szClrNS "." szClrName, \
        L##szClrNS W(".") L##szClrName, \
    	WinMDAdapter::FrameworkAssembly_ ## nClrAsmIdx, \
        WinMDAdapter::ContractAssembly_ ## nContractAsmIdx, \
        WinMDAdapter::WinMDTypeKind_ ## nWinMDTypeKind \
        COMMA_INDEBUG(WinMDAdapter::RedirectedTypeIndex_ ## nWinRTIndex) \
        COMMA_INDEBUG(WinMDAdapter::RedirectedTypeIndex_ ## nClrIndex) \
    },

static const RedirectedTypeInfo 
g_rgRedirectedTypes[WinMDAdapter::RedirectedTypeIndex_Count] = 
{
#include "WinRTProjectedTypes.h"


};
#undef SCAT
#undef DEFINE_PROJECTED_TYPE

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

/*static*/ BOOL WinMDAdapter::ConvertWellKnownTypeNameFromWinRTToClr(LPCSTR *pszNamespace, LPCSTR *pszName)
{
    return ConvertWellKnownTypeNameFromWinRTToClr(pszNamespace, pszName, NULL);
}


// Maps well-known WinRT typenames to CLR typename. If the incoming name is not a well-known WinRT typename,
// this function leaves *pszNamespace and *pszName alone and returns FALSE. Otherwise, it overwrites
// them with pointers to const strings, returns the index into the rewrite table in *pIndex and returns TRUE.
//
// (Note: munged names from WinMDExp are not "well-known" names. Since the idea of munging them is to hide them,
// this function will not transform such names. Not to mention, it would be hard to return such strings
// in a function that no error return mechanism.)
//
/*static*/ BOOL WinMDAdapter::ConvertWellKnownTypeNameFromWinRTToClr(LPCSTR *pszNamespace, LPCSTR *pszName, UINT *pIndex)
{
    _ASSERTE(pszNamespace && pszName);
    for (UINT i = 0; i < RedirectedTypeIndex_Count; i++)
    {
        _ASSERTE(g_rgRedirectedTypes[i].dbg_nIndex1 == i);
        _ASSERTE(g_rgRedirectedTypes[i].dbg_nIndex2 == i);
        
        if (0 == strcmp(*pszNamespace, g_rgRedirectedTypes[i].szWinRTNamespace))
        {
            if (0 == strcmp(*pszName, g_rgRedirectedTypes[i].szWinRTName))
            {
                *pszNamespace = g_rgRedirectedTypes[i].szClrNamespace;
                *pszName      = g_rgRedirectedTypes[i].szClrName;
                if (pIndex)
                    *pIndex = i;
                return TRUE;
            }
        }
    }
    return FALSE;
}

/*static*/ BOOL WinMDAdapter::ConvertWellKnownFullTypeNameFromWinRTToClr(LPCWSTR *pwszFullName, RedirectedTypeIndex *pIndex)
{
    _ASSERTE(pwszFullName);
    for (UINT i = 0; i < RedirectedTypeIndex_Count; i++)
    {
        _ASSERTE(g_rgRedirectedTypes[i].dbg_nIndex1 == i);
        _ASSERTE(g_rgRedirectedTypes[i].dbg_nIndex2 == i);
        
        if (0 == wcscmp(*pwszFullName, g_rgRedirectedTypes[i].wszWinRTFullName))
        {
            *pwszFullName = g_rgRedirectedTypes[i].wszClrFullName;
             if (pIndex)
                *pIndex = static_cast<RedirectedTypeIndex>(i);
            return TRUE;
        }
    }
    return FALSE;
}

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

// Maps well-known CLR typenames to WinRT typename. If the incoming name is not a well-known CLR typename,
// this function leaves *pszNamespace and *pszName alone and returns FALSE. Otherwise, it overwrites
// them with pointers to const strings and returns TRUE.
//
// (Note: munged names from WinMDExp are not "well-known" names. Since the idea of munging them is to hide them,
// this function will not transform such names. Not to mention, it would be hard to return such strings
// in a function that no error return mechanism.)
//
/*static*/ BOOL WinMDAdapter::ConvertWellKnownTypeNameFromClrToWinRT(LPCSTR *pszFullName)
{
    _ASSERTE(pszFullName);
   
    for (UINT i = 0; i < RedirectedTypeIndex_Count; i++)
    {
        if (0 == strcmp(g_rgRedirectedTypes[i].szClrFullName, *pszFullName))
        {
            *pszFullName = g_rgRedirectedTypes[i].szWinRTFullName;
            return TRUE;
        }
    }
    return FALSE;
}

/*static*/ BOOL WinMDAdapter::ConvertWellKnownTypeNameFromClrToWinRT(LPCSTR *pszNamespace, LPCSTR *pszName)
{
    _ASSERTE(pszNamespace);
    _ASSERTE(pszName);
   
    for (UINT i = 0; i < RedirectedTypeIndex_Count; i++)
    {
        if (0 == strcmp(g_rgRedirectedTypes[i].szClrNamespace, *pszNamespace) &&
            0 == strcmp(g_rgRedirectedTypes[i].szClrName, *pszName))
        {
            *pszNamespace   = g_rgRedirectedTypes[i].szWinRTNamespace;
            *pszName        = g_rgRedirectedTypes[i].szWinRTName;
            
            return TRUE;
        }
    }
    return FALSE;
}

//---------------------------------------------------------------------------------------
// 
// Returns names of redirected type 'index'.
// 
//static
void 
WinMDAdapter::GetRedirectedTypeInfo(
    RedirectedTypeIndex index, 
    LPCSTR *            pszClrNamespace, 
    LPCSTR *            pszClrName, 
    LPCSTR *            pszFullWinRTName,
    FrameworkAssemblyIndex * pFrameworkAssemblyIdx,
    ContractAssemblyIndex * pContractAssemblyIdx,
    WinMDTypeKind *     pWinMDTypeKind)
{
    _ASSERTE(index < RedirectedTypeIndex_Count);

    if (pszClrName != nullptr)
    {
        *pszClrName       = g_rgRedirectedTypes[index].szClrName;
    }
    if (pszClrNamespace != nullptr)
    {
        *pszClrNamespace  = g_rgRedirectedTypes[index].szClrNamespace;
    }
    if (pszFullWinRTName != nullptr)
    {
        *pszFullWinRTName = g_rgRedirectedTypes[index].szWinRTFullName;
    }	
    if (pFrameworkAssemblyIdx != nullptr)
    {
        *pFrameworkAssemblyIdx = g_rgRedirectedTypes[index].nClrAssemblyIndex;
    }
    if (pContractAssemblyIdx != nullptr)
    {
        *pContractAssemblyIdx = g_rgRedirectedTypes[index].nContractAssemblyIndex;
    }
    if (pWinMDTypeKind != nullptr)
    {
        *pWinMDTypeKind   = g_rgRedirectedTypes[index].nTypeKind;
    }
}

//---------------------------------------------------------------------------------------
// 
// Returns WinRT name of redirected type 'index'.
// 
//static 
LPCWSTR 
WinMDAdapter::GetRedirectedTypeFullWinRTName(
    RedirectedTypeIndex index)
{
    _ASSERTE(index < RedirectedTypeIndex_Count);
    
    return g_rgRedirectedTypes[index].wszWinRTFullName;
}

//---------------------------------------------------------------------------------------
// 
// Returns CLR name of redirected type 'index'.
// 
//static 
LPCSTR 
WinMDAdapter::GetRedirectedTypeFullCLRName(
    RedirectedTypeIndex index)
{
    _ASSERTE(index < RedirectedTypeIndex_Count);
    
    return g_rgRedirectedTypes[index].szClrFullName;
}

//------------------------------------------------------------------------------
// 
// Get TypeRefTreatment value for a TypeRef
// 
HRESULT 
    WinMDAdapter::GetTypeDefTreatment(
    mdTypeDef tkTypeDef, 
    ULONG *   pTypeDefTreatment)
{
    HRESULT hr;

    _ASSERTE(pTypeDefTreatment != NULL);
    _ASSERTE(TypeFromToken(tkTypeDef) == mdtTypeDef);

    ULONG typeDefIndex = RidFromToken(tkTypeDef) - 1;
    ULONG treatment;
    IfFailGo(m_typeDefTreatmentMemoTable.GetEntry(typeDefIndex, &treatment));
    if (hr == S_FALSE)
    {
        LPCSTR  szNamespace;
        LPCSTR  szName;
        ULONG   dwFlags;
        mdToken extends;
        IfFailGo(m_pRawMetaModelCommonRO->CommonGetTypeDefProps(tkTypeDef, &szNamespace, &szName, &dwFlags, &extends, NULL));
        treatment = kTdOther;
        ULONG baseTypeRefTreatment = kTrNotYetInitialized;

        // We only want to treat this type special if it has metadata that was encoded as WinRT metadata
        if (IsTdWindowsRuntime(dwFlags))
        {
            // We need to know whether the typeDef is an Enum/Struct/Delegate/Attribute
            if (TypeFromToken(extends) == mdtTypeRef)
            {
                IfFailGo(GetTypeRefTreatment(extends, &baseTypeRefTreatment));
            }
            // Force tdWindowsRuntime flag on (@todo: WinMDExp already does this on its own - if AppModel could do so, we could get rid of this code)
            if (m_scenario == kWinMDNormal)
            {
                BOOL isAttribute = FALSE;
                if (TypeFromToken(extends) == mdtTypeRef)
                {
                    ULONG treatment;
                    IfFailGo(GetTypeRefTreatment(extends, &treatment));
                    if (treatment == kTrSystemAttribute)
                    {
                        isAttribute = TRUE;
                    }
                }

                if (!isAttribute)
                {
                    treatment = kTdNormalNonAttribute;
                }
                else
                {
                    treatment = kTdNormalAttribute;
                }
            }

            // Windows.Foundation.WinMD: Hide any type defs that define well-known types that we'd redirect to mscorlib equivalents.
            LPCSTR szTempNamespace = szNamespace;
            LPCSTR szTempName      = szName;
            if ((treatment != kTdOther) && ConvertWellKnownTypeNameFromWinRTToClr(&szTempNamespace, &szTempName))
            {
                if (treatment == kTdNormalNonAttribute)
                    treatment = kTdRedirectedToCLRType;
                else
                    treatment = kTdRedirectedToCLRAttribute;
            }
            else
            {
                // WinMDExp emits two versions of RuntimeClasses and Enums:
                //
                //    public class Foo {}            // the WinRT reference class
                //    internal class <CLR>Foo {}     // the implementation class that we want WinRT consumers to ignore
                //
                // The adapter's job is to undo WinMDExp's transformations. I.e. turn the above into:
                //
                //    internal class <WinRT>Foo {}   // the WinRT reference class that we want CLR consumers to ignore
                //    public class Foo {}            // the implementation class
                //
                // We only add the <WinRT> prefix here since the WinRT view is the only view that is marked tdWindowsRuntime
                // De-mangling the CLR name is done below.
                if (m_scenario == kWinMDExp && !IsTdNested(dwFlags) && IsTdPublic(dwFlags) && !IsTdInterface(dwFlags))
                {
                    switch (baseTypeRefTreatment)
                    {
                        case kTrSystemDelegate:
                        case kTrSystemAttribute:
                        case kTrSystemValueType:
                        {
                            // Delegates, Attributes, and Structs have only one version
                            break;
                        }

                        case kTrSystemEnum:
                        {
                            if (m_fReferencesMscorlibV4 && !IsTdSpecialName(dwFlags))
                            {
                                // This is a back-compat quirk. Enums exported with an older WinMDExp have only one version
                                // not marked with tdSpecialName. These enums should *not* be mangled and flipped to private.
                                break;
                            }
                            // fall-thru
                        }
                        default:
                        {
                            // Prepend "<WinRT>" and flip the visibility to private
                            treatment = kTdPrefixWinRTName;
                        }
                    }
                }
            }

            // Scan through Custom Attributes on type, looking for interesting bits. We only need to do this for RuntimeClasses. (The if check below is conservative.)
            if (!IsTdInterface(dwFlags) && ((treatment == kTdPrefixWinRTName) || (treatment == kTdNormalNonAttribute)))
            {
                HRESULT hrCA;
                hrCA = m_pRawMetaModelCommonRO->CommonGetCustomAttributeByNameEx(tkTypeDef, "Windows.UI.Xaml.TreatAsAbstractComposableClassAttribute", NULL, NULL, NULL);
                if (hrCA == S_OK)
                {
                    treatment |= kTdMarkAbstractFlag;
                }
            }

            if ((treatment == kTdNormalNonAttribute || treatment == kTdNormalAttribute) &&  // native WinMD, not redirected
                IsHiddenWinRTType(szTempNamespace, szTempName)      // hidden type
                )
            {
                // Hide those WinRT types. Examples of those WinRT types that we need to hide are Jupiter struct helpers
                treatment |= kTdMarkInternalFlag;
            }
        }
        else if (m_scenario == kWinMDExp && !IsTdNested(dwFlags))
        {
            // <CLR> implementation classes are not marked tdWindowsRuntime, but still need to be modified
            // by the adapter.  See if we have one of those here.
            LPCSTR szUnmangledName;
            IfFailGo(CheckIfClrImplementationType(szName, dwFlags, &szUnmangledName));
            if (hr == S_OK)
            {
                treatment = kTdUnmangleWinRTName;
            }
        }

        if (baseTypeRefTreatment == kTrSystemEnum)
        {
            // The typeDef is an Enum. We need to store the treatment.
            treatment |= kTdEnum;
        }

        IfFailGo(m_typeDefTreatmentMemoTable.InitEntry(typeDefIndex, &treatment));
    }
    _ASSERTE(treatment != kTdNotYetInitialized);

    *pTypeDefTreatment = treatment;
    hr = S_OK;

ErrExit:
    return hr;
} // WinMDAdapter::GetTypeDefTreatment

//------------------------------------------------------------------------------
// Returns renamed typedefs
HRESULT WinMDAdapter::GetTypeDefProps(mdTypeDef    tkTypeDef,           // [IN] given typedef
    LPCUTF8     *pszNamespace,        // [OUT] return typedef namespace
    LPCUTF8     *pszName,             // [OUT] return typedef name
    DWORD       *pdwFlags,            // [OUT] return typedef flags                                    
    mdToken     *ptkExtends           // [OUT] Put base class TypeDef/TypeRef here.
    )
{
    HRESULT hr;

    _ASSERTE(TypeFromToken(tkTypeDef) == mdtTypeDef);

    LPCSTR  szNamespace;
    LPCSTR  szName;
    ULONG   dwFlags;
    mdToken extends;
    ULONG treatment;
    IfFailGo(m_pRawMetaModelCommonRO->CommonGetTypeDefProps(tkTypeDef, &szNamespace, &szName, &dwFlags, &extends, NULL));
    IfFailGo(GetTypeDefTreatment(tkTypeDef, &treatment));

    switch (treatment & kTdTreatmentMask)
    {
    case kTdOther:
        break;

    case kTdNormalNonAttribute:
        // Force tdWindowsRuntime flag on (@todo: WinMDExp already does this on its own - if AppModel could do so, we could get rid of this code)
        dwFlags |= tdWindowsRuntime | tdImport;
        break;

    case kTdNormalAttribute:
        // Attribute types don't really exist, so we don't want to allow derivation from them either
        dwFlags |= tdWindowsRuntime | tdSealed;
        break;

    case kTdUnmangleWinRTName:
        szName = szName + s_ncCLRPrefix;
        dwFlags |= tdPublic;
        dwFlags &= ~tdSpecialName;
        break;

    case kTdPrefixWinRTName:
        {
            // Prepend "<WinRT>" and flip the visibility to private
            LPCSTR szPrefixedName;
            ULONG index = RidFromToken(tkTypeDef) - 1;
            IfFailGo(m_mangledTypeNameTable.GetEntry(index, &szPrefixedName));
            if (hr == S_FALSE)
            {
                IfFailGo(CreatePrefixedName(s_szWinRTPrefix, szName, &szPrefixedName));
                IfFailGo(m_mangledTypeNameTable.InitEntry(index, &szPrefixedName));
            }
            szName = szPrefixedName;
            dwFlags &= ~tdPublic;
            dwFlags |= tdImport;
            break;
        }

    case kTdRedirectedToCLRType:
        dwFlags &= ~tdPublic;
        dwFlags |= tdImport;
        break;

    case kTdRedirectedToCLRAttribute:
        dwFlags &= ~tdPublic;
        break;

        default:
            UNREACHABLE();
    }
    
    if ((treatment & kTdMarkAbstractFlag) == kTdMarkAbstractFlag)
    {
        dwFlags |= tdAbstract;
    }    
    if ((treatment & kTdMarkInternalFlag) == kTdMarkInternalFlag)
    {
        dwFlags &= ~tdPublic;
    }    

    if (pszNamespace)
        *pszNamespace = szNamespace;
    if (pszName)
        *pszName = szName;
    if (pdwFlags)
        *pdwFlags = dwFlags;
    if (ptkExtends)
        *ptkExtends = extends;
    
    hr = S_OK;

 ErrExit:
    return hr;
}

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

// Find TypeDef by name
HRESULT WinMDAdapter::FindTypeDef(
    LPCSTR      szTypeDefNamespace, // [IN] Namespace for the TypeDef.
    LPCSTR      szTypeDefName,      // [IN] Name of the TypeDef.
    mdToken     tkEnclosingClass,   // [IN] TypeDef/TypeRef of enclosing class.
    mdTypeDef * ptkTypeDef          // [OUT] return typedef
)
{
    HRESULT hr = S_OK;
    
    _ASSERTE((szTypeDefName != NULL) && (ptkTypeDef != NULL));
    _ASSERTE((TypeFromToken(tkEnclosingClass) == mdtTypeRef) || 
             (TypeFromToken(tkEnclosingClass) == mdtTypeDef) || 
             IsNilToken(tkEnclosingClass));
    
    // initialize the output parameter
    *ptkTypeDef = mdTypeDefNil;
    
    // Treat no namespace as empty string.
    if (szTypeDefNamespace == NULL)
        szTypeDefNamespace = "";
    
    // Do a linear search
    ULONG        cTypeDefRecs = m_pRawMetaModelCommonRO->CommonGetRowCount(mdtTypeDef);
    
    // Get TypeDef of the tkEnclosingClass passed in
    if (TypeFromToken(tkEnclosingClass) == mdtTypeRef)
    {
        LPCUTF8  szNamespace;
        LPCUTF8  szName;
        mdToken  tkResolutionScope;

        IfFailRet(this->GetTypeRefProps(tkEnclosingClass, &szNamespace, &szName, &tkResolutionScope));
        // Update tkEnclosingClass to TypeDef
        IfFailRet(this->FindTypeDef(
                    szNamespace, 
                    szName, 
                    (TypeFromToken(tkResolutionScope) == mdtTypeRef) ? tkResolutionScope : mdTokenNil, 
                    &tkEnclosingClass));
        _ASSERTE(TypeFromToken(tkEnclosingClass) == mdtTypeDef);
    }
    
    // Search for the TypeDef
    for (ULONG i = 1; i <= cTypeDefRecs; i++)
    {
        LPCUTF8   szNamespace;
        LPCUTF8   szName;
        ULONG     dwFlags;

        mdTypeDef tkTypeDefCandidate = TokenFromRid(i, mdtTypeDef);

        IfFailRet(this->GetTypeDefProps(tkTypeDefCandidate, &szNamespace, &szName, &dwFlags, NULL));
        if (!IsTdNested(dwFlags) && !IsNilToken(tkEnclosingClass))
        {
            // If the class is not Nested and EnclosingClass passed in is not nil
            continue;
        }
        else if (IsTdNested(dwFlags) && IsNilToken(tkEnclosingClass))
        {
            // If the class is nested and EnclosingClass passed is nil
            continue;
        }
        else if (!IsNilToken(tkEnclosingClass))
        {
            _ASSERTE(TypeFromToken(tkEnclosingClass) == mdtTypeDef);

            mdTypeDef enclosingTypeDef;
            IfFailRet(m_pRawMetaModelCommonRO->CommonGetEnclosingClassOfTypeDef(tkTypeDefCandidate, &enclosingTypeDef));
            if (enclosingTypeDef != tkEnclosingClass)
            {
                // Type was not nested by tkEnclosingClass
                continue;     
            }
        }
        
        if (strcmp(szTypeDefName, szName) == 0)
        {
            if (strcmp(szTypeDefNamespace, szNamespace) == 0)
            {
                *ptkTypeDef = TokenFromRid(i, mdtTypeDef);
                return S_OK;
            }
        }
    }
    // Cannot find the TypeDef by name
    return CLDB_E_RECORD_NOTFOUND;
}

//------------------------------------------------------------------------------
// 
// Modifies TypeRef names and resolution scope.
// 
HRESULT 
WinMDAdapter::GetTypeRefProps(
    mdTypeRef tkTypeRef,
    LPCSTR *  pszNamespace, 
    LPCSTR *  pszName, 
    mdToken * ptkResolutionScope)
{

    HRESULT hr;
    ULONG   treatment;
    ULONG treatmentClass;
    IfFailGo(GetTypeRefTreatment(tkTypeRef, &treatment));
    _ASSERTE(treatment != kTrNotYetInitialized);

    treatmentClass = treatment & kTrClassMask;
    if (treatmentClass == kTrClassWellKnownRedirected)
    {
        ULONG nRewritePairIndex = treatment & ~kTrClassMask;
        if (pszNamespace != NULL)
            *pszNamespace = g_rgRedirectedTypes[nRewritePairIndex].szClrNamespace;
        if (pszName != NULL)
            *pszName = g_rgRedirectedTypes[nRewritePairIndex].szClrName;
        if (ptkResolutionScope != NULL)
        {
            ContractAssemblyIndex assemblyIndex  = g_rgRedirectedTypes[nRewritePairIndex].nContractAssemblyIndex;
            _ASSERTE(assemblyIndex < ContractAssembly_Count);
            *ptkResolutionScope = mdtAssemblyRef | (m_rawAssemblyRefCount + assemblyIndex + 1);
        }
    }
    else
    {
        _ASSERTE(treatmentClass == kTrClassMisc);
        switch (treatment)
        {
            case kTrNoRewriteNeeded:
            case kTrSystemValueType:
            case kTrSystemEnum:
                IfFailGo(m_pRawMetaModelCommonRO->CommonGetTypeRefProps(tkTypeRef, pszNamespace, pszName, ptkResolutionScope));
                break;
    
            case kTrSystemDelegate:
                if (pszNamespace != NULL)       *pszNamespace = "System";
                if (pszName != NULL)            *pszName = "MulticastDelegate";
                if (ptkResolutionScope) *ptkResolutionScope = mdtAssemblyRef | (m_rawAssemblyRefCount + ContractAssembly_SystemRuntime + 1);
                break;
    
            case kTrSystemAttribute:
                if (pszNamespace != NULL)       *pszNamespace = "System";
                if (pszName != NULL)            *pszName = "Attribute";                
                if (ptkResolutionScope != NULL) *ptkResolutionScope = mdtAssemblyRef | (m_rawAssemblyRefCount + ContractAssembly_SystemRuntime + 1);
                break;
    
            default:
                _ASSERTE(!"Unknown treatment value.");
                break;
    
        }
    }
    hr = S_OK;
    
ErrExit:
    return hr;
} // WinMDAdapter::GetTypeRefProps

//------------------------------------------------------------------------------
// 
// Get TypeRefTreatment value for a TypeRef
// 
HRESULT 
WinMDAdapter::GetTypeRefTreatment(
    mdTypeRef tkTypeRef, 
    ULONG *   pTypeRefTreatment)
{
    HRESULT hr;

    _ASSERTE(pTypeRefTreatment != NULL);
    _ASSERTE(TypeFromToken(tkTypeRef) == mdtTypeRef);
    
    ULONG typeRefIndex = RidFromToken(tkTypeRef) - 1;
    ULONG treatment;
    IfFailGo(m_typeRefTreatmentMemoTable.GetEntry(typeRefIndex, &treatment));
    if (hr == S_FALSE)
    {
        LPCSTR szFromNamespace;
        LPCSTR szFromName;
        mdToken tkResolutionScope;
        IfFailGo(m_pRawMetaModelCommonRO->CommonGetTypeRefProps(tkTypeRef, &szFromNamespace, &szFromName, &tkResolutionScope));
        treatment = kTrNoRewriteNeeded;
        UINT redirIndex;
        if (ConvertWellKnownTypeNameFromWinRTToClr(&szFromNamespace, &szFromName, &redirIndex))
        {
            treatment = kTrClassWellKnownRedirected | redirIndex;
        }
        else
        {
            if (0 == strcmp(szFromNamespace, "System"))
            {
                if (0 == strcmp(szFromName, "MulticastDelegate"))
                {
                    treatment = kTrSystemDelegate;
                }
                else if (0 == strcmp(szFromName, "Attribute"))
                {
                    treatment = kTrSystemAttribute;
                }
                else if (0 == strcmp(szFromName, "Enum"))
                {
                    treatment = kTrSystemEnum;
                }
                else if (0 == strcmp(szFromName, "ValueType"))
                {
                    treatment = kTrSystemValueType;
                }
            }
        }

        // Note that we intentionally do not mangle names of TypeRef's pointing to mangled TypeDef's
        // in the same module. This allows WinMDExp to generate "conditional TypeRef's", i.e. references
        // to the WinMD view or CLR view, depending on whether the adapter is on.

        IfFailGo(m_typeRefTreatmentMemoTable.InitEntry(typeRefIndex, &treatment));
    }
    _ASSERTE(treatment != kTrNotYetInitialized);
    
    *pTypeRefTreatment = treatment;
    hr = S_OK;
    
ErrExit:
    return hr;
} // WinMDAdapter::GetTypeRefTreatment


//------------------------------------------------------------------------------
// 
// Get TypeRef's index in array code:g_rgRedirectedTypes.
// Returns S_OK if TypeRef is redirected and fills its index (*pIndex).
// Returns S_FALSE if type is not well known redirected type (*pIndex is not initialized).

// 
HRESULT 
WinMDAdapter::GetTypeRefRedirectedInfo(
    mdTypeRef             tkTypeRef, 
    RedirectedTypeIndex * pIndex)
{
    _ASSERTE(TypeFromToken(tkTypeRef) == mdtTypeRef);
    _ASSERTE(pIndex != NULL);
    
    HRESULT hr;
    ULONG   treatment;
    ULONG treatmentClass;
    IfFailGo(GetTypeRefTreatment(tkTypeRef, &treatment));
    
    treatmentClass = treatment & kTrClassMask;
    if (treatmentClass == kTrClassWellKnownRedirected)
    {
        *pIndex = (RedirectedTypeIndex)(treatment & ~kTrClassMask);
        _ASSERTE(*pIndex < RedirectedTypeIndex_Count);
        hr = S_OK;
    }
    else
    {
        // Do not initialize *pIndex
        hr = S_FALSE;
    }
    
ErrExit:
    return hr;
}

//------------------------------------------------------------------------------
// 
// Finds a typeref by its (transformed) name
// 
HRESULT WinMDAdapter::FindTypeRef(  // S_OK or error.
        LPCSTR      szNamespace,            // [IN] Namespace for the TypeRef.
        LPCSTR      szName,                 // [IN] Name of the TypeRef.
        mdToken     tkResolutionScope,      // [IN] Resolution Scope fo the TypeRef.
        mdTypeRef   *ptk)                   // [OUT] TypeRef token returned.
{
    HRESULT     hr;

    _ASSERTE(szName);  // Crash on NULL pszName (just like the real metadata importer)
    _ASSERTE(ptk);     // Crash on NULL ptk (just like the real metadata importer)


    // initialize the output parameter
    *ptk = mdTypeRefNil;
    
    // Treat no namespace as empty string.
    if (!szNamespace)
        szNamespace = "";

    ULONG       cTypeRefRecs = m_pRawMetaModelCommonRO->CommonGetRowCount(mdtTypeRef);
    
    for (ULONG i = 1; i <= cTypeRefRecs; i++)
    {
        LPCUTF8     szNamespaceTmp;
        LPCUTF8     szNameTmp;
        mdToken     tkRes;

        mdTypeRef tr = TokenFromRid(i, mdtTypeRef);
        IfFailGo(GetTypeRefProps(tr, &szNamespaceTmp, &szNameTmp, &tkRes));
        if (IsNilToken(tkRes))
        {
            if (!IsNilToken(tkResolutionScope))
                continue;
        }
        else if (tkRes != tkResolutionScope)
            continue;

        if (strcmp(szNamespace, szNamespaceTmp))
            continue;

        if (!strcmp(szNameTmp, szName))
        {
            *ptk = tr;
            hr = S_OK;
            goto ErrExit;
        }
    }

    // cannot find the typedef
    hr = CLDB_E_RECORD_NOTFOUND;
ErrExit:
    return hr;
}

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

HRESULT WinMDAdapter::ModifyExportedTypeName(
    mdExportedType tkExportedType,     // [IN] exportedType token
    LPCSTR     *pszNamespace,          // [IN,OUT,OPTIONAL] namespace to modify
    LPCSTR     *pszName                // [IN,OUT,OPTIONAL] name to modify
)
{
    _ASSERTE(TypeFromToken(tkExportedType) == mdtExportedType);

    if (m_scenario == kWinMDExp)
    {
        if (pszName && 0 == strncmp(*pszName, s_szCLRPrefix, s_ncCLRPrefix))
        {
            (*pszName) += s_ncCLRPrefix;
        }
    }
    return S_OK;
}

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

// We must optionaly add an assembly ref for System.Numerics.Vectors.dll since this assembly is not available
// on downlevel platforms. 
//
// This function assumes that System.Numerics.Vectors.dll is the last assembly that
// we add so if we find a reference then we return ContractAssembly_Count otherwise we return 
// ContractAssembly_Count - 1. 
int WinMDAdapter::GetExtraAssemblyRefCount()
{
    HRESULT hr;

    if (m_extraAssemblyRefCount == -1)
    {
        mdAssemblyRef tkSystemNumericsVectors = TokenFromRid(m_rawAssemblyRefCount + ContractAssembly_SystemNumericsVectors + 1, mdtAssemblyRef);
        ULONG cTypeRefRecs = m_pRawMetaModelCommonRO->CommonGetRowCount(mdtTypeRef);
        BOOL systemNumericsVectorsTypeFound = FALSE;

        for (ULONG i = 1; i <= cTypeRefRecs; i++)
        {
            mdToken tkResolutionScope;
            mdTypeRef tkTypeRef = TokenFromRid(i, mdtTypeRef);

            // Get the resolution scope(AssemblyRef) token for the type. GetTypeRefProps does the type redirection.
            IfFailGo(GetTypeRefProps(tkTypeRef, nullptr, nullptr, &tkResolutionScope));

            if (tkResolutionScope == tkSystemNumericsVectors)
            {
                systemNumericsVectorsTypeFound = TRUE;
                break;
            }
        }

        if (systemNumericsVectorsTypeFound)
        {
            m_extraAssemblyRefCount = ContractAssembly_Count;
        }
        else
        {
            m_extraAssemblyRefCount = ContractAssembly_Count - 1;
        }
    }

ErrExit:
    if (m_extraAssemblyRefCount == -1)
    {
        // Setting m_extraAssemblyRefCount to ContractAssembly_Count so that this function returns a stable value and
        // that if there is a System.Numerics type ref that it does not have a dangling assembly ref
        m_extraAssemblyRefCount = ContractAssembly_Count; 
    }

    return m_extraAssemblyRefCount;
}

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

/*static*/
void WinMDAdapter::GetExtraAssemblyRefProps(FrameworkAssemblyIndex index,
                                            LPCSTR* ppName,
                                            AssemblyMetaDataInternal* pContext,
                                            PCBYTE * ppPublicKeytoken,
                                            DWORD* pTokenLength,
                                            DWORD* pdwFlags)
{
    _ASSERTE(index >= 0 && index < FrameworkAssembly_Count);
    _ASSERTE(index != FrameworkAssembly_Mscorlib);

    if (ppName)
    {
        *ppName = GetExtraAssemblyRefNameFromIndex((FrameworkAssemblyIndex)index);
    }

    if (pContext)
    {
        ::memset(pContext, 0, sizeof(AssemblyMetaDataInternal));

        pContext->usMajorVersion = VER_ASSEMBLYMAJORVERSION;
        pContext->usMinorVersion = VER_ASSEMBLYMINORVERSION;
        pContext->usBuildNumber = VER_ASSEMBLYBUILD;
        pContext->usRevisionNumber = VER_ASSEMBLYBUILD_QFE;

        pContext->szLocale = "";
    }

    if (ppPublicKeytoken)
    {
        if (index == FrameworkAssembly_Mscorlib)
        {
            *ppPublicKeytoken = g_rbTheSilverlightPlatformKeyToken;
            *pTokenLength = sizeof(g_rbTheSilverlightPlatformKeyToken);
        }
        else
        {
            if (index == FrameworkAssembly_SystemNumericsVectors || index == FrameworkAssembly_SystemRuntime || index == FrameworkAssembly_SystemObjectModel)
            {
                *ppPublicKeytoken = s_pbContractPublicKeyToken;
                *pTokenLength = sizeof(s_pbContractPublicKeyToken);
            }
            else
            {
                *ppPublicKeytoken = s_pbFrameworkPublicKeyToken;
                *pTokenLength = sizeof(s_pbFrameworkPublicKeyToken);
            }
        }
    }

    if (pdwFlags)
    {
        // ppPublicKeytoken contains the public key token, not the whole key.
        *pdwFlags = 0;
    }
}

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

HRESULT WinMDAdapter::FindExportedType(
    LPCUTF8           szNamespace,           // [IN] expected namespace
    LPCUTF8           szName,                // [IN] expected name
    mdToken           tkEnclosingType,       // [IN] expected tkEnclosingType
    mdExportedType   *ptkExportedType  // [OUT] ExportedType token returned.
)
{
    HRESULT hr;

    _ASSERTE(szName && ptkExportedType);

    // Set NULL namespace to empty string.
    if (!szNamespace)
        szNamespace = "";

    // Set output to Nil.
    *ptkExportedType = mdTokenNil;

    ULONG ulCount = m_pRawMetaModelCommonRO->CommonGetRowCount(mdtExportedType);
    while (ulCount)
    {
        mdExportedType tkCandidateExportedType = TokenFromRid(ulCount--, mdtExportedType);

        LPCSTR      szCandidateNamespace;
        LPCSTR      szCandidateName;
        mdToken     tkCandidateImpl;

        IfFailRet(m_pRawMetaModelCommonRO->CommonGetExportedTypeProps(tkCandidateExportedType, &szCandidateNamespace, &szCandidateName, &tkCandidateImpl));
        IfFailRet(this->ModifyExportedTypeName(tkCandidateExportedType, &szCandidateNamespace, &szCandidateName));
        // Handle the case of nested vs. non-nested classes.
        if (TypeFromToken(tkCandidateImpl) == mdtExportedType && !IsNilToken(tkCandidateImpl))
        {
            // Current ExportedType being looked at is a nested type, so
            // comparing the implementation token.
            if (tkCandidateImpl != tkEnclosingType)
                continue;
        }
        else if (TypeFromToken(tkEnclosingType) == mdtExportedType &&
                 !IsNilToken(tkEnclosingType))
        {
            // ExportedType passed in is nested but the current ExportedType is not.
            continue;
        }

        if (0 != strcmp(szName, szCandidateName))
            continue;

        if (0 != strcmp(szNamespace, szCandidateNamespace))
            continue;

        *ptkExportedType = tkCandidateExportedType;
        return S_OK;
    }
    return CLDB_E_RECORD_NOTFOUND;
}


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

// Modifies methodDef flags and RVA
HRESULT WinMDAdapter::ModifyMethodProps(mdMethodDef tkMethodDef, /*[in, out]*/ DWORD *pdwAttr, /* [in,out] */ DWORD *pdwImplFlags, /* [in,out] */ ULONG *pulRVA, /* [in, out] */ LPCSTR *pszName)
{
    HRESULT hr;
    _ASSERTE(TypeFromToken(tkMethodDef) == mdtMethodDef);

    ULONG mdTreatment;
    DWORD dwAttr;
    DWORD dwImplFlags;
    ULONG ulRVA;
    IfFailGo(GetMethodDefTreatment(tkMethodDef, &mdTreatment));

    dwAttr = pdwAttr ? *pdwAttr: 0;
    dwImplFlags = pdwImplFlags ? *pdwImplFlags : 0;
    ulRVA = pulRVA ? *pulRVA : 0; 

    switch (mdTreatment & kMdTreatmentMask)
    {
        case kMdInterface:
            // Method is declared on an interface
            dwImplFlags |= miRuntime|miInternalCall;
            break;

        case kMdDelegate:
            // Method is declared on a delegate
            dwAttr &= ~mdMemberAccessMask;
            dwAttr |= mdPublic;
            ulRVA = 0;
            dwImplFlags |= miRuntime;
            break;

        case kMdAttribute:
            // Method is declared on an attribute
            ulRVA = 0;
            dwImplFlags |= miRuntime|miInternalCall;
            break;

        case kMdImplementation:
            // CLR implementation class. Needs no adjustment.
            break;

        case kMdHiddenImpl:
            dwAttr &= ~mdMemberAccessMask;
            dwAttr |= mdPrivate;
            // fall-through

        case kMdOther:

            // All other cases
            ulRVA = 0;
            dwImplFlags |= miRuntime|miInternalCall;

            if (mdTreatment & kMdMarkAbstractFlag)
            {
                dwAttr |= mdAbstract;
            }

            if (mdTreatment & kMdMarkPublicFlag)
            {
                dwAttr &= ~mdMemberAccessMask;
                dwAttr |= mdPublic;
            }

            break;

        case kMdRenameToDisposeMethod:
            ulRVA = 0;
            dwImplFlags |= miRuntime|miInternalCall;
            if(pszName)
            {
                *pszName = "Dispose";
            }
            break;
            
        default:
            UNREACHABLE();

    }

    dwAttr |= mdHideBySig;

    if (pdwAttr)
        (*pdwAttr) = dwAttr;
    if (pdwImplFlags)
        (*pdwImplFlags) = dwImplFlags;
    if (pulRVA)
        *pulRVA = ulRVA;
    hr = S_OK;

  ErrExit:
    return hr;
}

HRESULT WinMDAdapter::ModifyFieldProps (mdToken tkField, mdToken tkParent, LPCSTR szFieldName, DWORD *pdwFlags)
{
    _ASSERTE(szFieldName != NULL);

    HRESULT hr;
    if (pdwFlags && IsFdPrivate(*pdwFlags) && (0 == strcmp(szFieldName, "value__")))
    {
        ULONG treatment;
        BOOL isEnum = FALSE;
        if(TypeFromToken(tkParent) == mdtTypeDef)
        {
            IfFailGo(GetTypeDefTreatment(tkParent, &treatment));

            if ((treatment & kTdEnum) == kTdEnum)
            {
                isEnum = TRUE;
            }
        }

        if (isEnum)
        {
            // We have found the value__ field of the enum.
            // We need to change its flags from private to public
            *pdwFlags = (*pdwFlags & ~fdPrivate) | fdPublic;
        }
    }

    hr = S_OK;

ErrExit:
    return hr;
}


HRESULT WinMDAdapter::ModifyFieldDefProps (mdFieldDef tkFielddDef, DWORD *pdwFlags)
{
    HRESULT hr;

    if (pdwFlags)
    {
        mdTypeDef tkParent;
        LPCSTR szName;
        IfFailGo(m_pRawMetaModelCommonRO->CommonGetFieldDefProps(tkFielddDef, &tkParent, &szName, pdwFlags));
        IfFailGo(ModifyFieldProps(tkFielddDef, tkParent, szName, pdwFlags));
    }

    hr = S_OK;
ErrExit:
    return hr;
}

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

HRESULT WinMDAdapter::ModifyMemberProps(mdToken tkMember, /*[in, out]*/ DWORD *pdwAttr, /* [in,out] */ DWORD *pdwImplFlags, /* [in,out] */ ULONG *pulRVA, LPCSTR *pszNewName)
{
    HRESULT hr; 
    switch(TypeFromToken(tkMember))
    {
    case mdtMethodDef: IfFailGo(ModifyMethodProps(tkMember, pdwAttr, pdwImplFlags, pulRVA, pszNewName));
        break;

    case mdtMemberRef:
    {
        //
        // We need to rename the MemberRef for IClosable.Close as well
        // so that the MethodImpl for Dispose method can correctly be shown as IDisposable.Dispose
        // instead of IDisposable.Close
        //
        UINT nIndex = WinMDAdapter::RedirectedTypeIndex_Invalid;
        if (pszNewName && 
            CheckIfMethodImplImplementsARedirectedInterface(tkMember, &nIndex) == S_OK &&
            nIndex == WinMDAdapter::RedirectedTypeIndex_Windows_Foundation_IClosable)
        {
            *pszNewName = "Dispose";
        }
    }
    break;

    case mdtFieldDef: 
        IfFailGo(ModifyFieldDefProps(tkMember, pdwAttr));
        break;
    }

    hr = S_OK;
ErrExit:
    return hr;
}

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

// Get MethodTreatment value for a methodDef
HRESULT WinMDAdapter::GetMethodDefTreatment(mdMethodDef tkMethodDef, ULONG *ppMethodDefTreatment)
{
    HRESULT hr;

    _ASSERTE(TypeFromToken(tkMethodDef) == mdtMethodDef);
    ULONG index = RidFromToken(tkMethodDef) - 1;

    // Thread-safety: No lock is needed to update this table as we're monotonically advancing a kMdNotYetInitialized to
    //    some other fixed byte. The work to decide this value is idempotent and side-effect free so
    //    there's no harm if two threads do it concurrently. 
    ULONG mdTreatment;
    IfFailGo(m_methodDefTreatmentMemoTable.GetEntry(index, &mdTreatment));
    if (hr == S_FALSE)
    {
        mdTypeDef declaringTypeDef;
        IfFailGo(m_pRawMetaModelCommonRO->FindParentOfMethodHelper(tkMethodDef, &declaringTypeDef));
        ULONG firstMethodRid;
        IfFailGo(m_pRawMetaModelCommonRO->CommonGetTypeDefProps(declaringTypeDef, NULL, NULL, NULL, NULL, &firstMethodRid));
        IfFailGo(ComputeMethodDefTreatment(tkMethodDef, declaringTypeDef, &mdTreatment));
        _ASSERTE(mdTreatment != kMdNotYetInitialized);
        IfFailGo(m_methodDefTreatmentMemoTable.InitEntry(index, &mdTreatment));

        // Since the mdTreatment is only a function of the declaring type in most cases, cache it for all the declared method since
        // we took the time to look up the declaring type. Do enough validity checks to avoid corrupting the heap
        // or table but otherwise, swallow validity errors as this is just an optimization.
        // Methods on RuntimeClasses need to get treatment data per method.
        if (((mdTreatment & kMdTreatmentMask) != kMdOther) && 
            ((mdTreatment & kMdTreatmentMask) != kMdHiddenImpl) &&
            ((mdTreatment & kMdTreatmentMask) != kMdRenameToDisposeMethod))
        {
            const ULONG methodDefCount = m_pRawMetaModelCommonRO->CommonGetRowCount(mdtMethodDef);
            const ULONG startIndex = RidFromToken(firstMethodRid) - 1;
            if (startIndex < methodDefCount)
            {
                const ULONG typeDefCount = m_pRawMetaModelCommonRO->CommonGetRowCount(mdtTypeDef);
                if (RidFromToken(declaringTypeDef) < typeDefCount)
                {
                    ULONG stopMethodRid;
                    if (S_OK == m_pRawMetaModelCommonRO->CommonGetTypeDefProps(declaringTypeDef + 1, NULL, NULL, NULL, NULL, &stopMethodRid))
                    {
                        ULONG stopIndex = RidFromToken(stopMethodRid) - 1;
                        if (startIndex < methodDefCount && stopIndex <= methodDefCount)
                        {
                            ULONG walkIndex = startIndex;
                            while (walkIndex < stopIndex)
                            {
                                IfFailGo(m_methodDefTreatmentMemoTable.InitEntry(walkIndex++, &mdTreatment));
                            }
                        }
                    }
                }
                else
                {
                    // This was final typeDef - blast the mdTreatment into the rest of the table.
                    for (ULONG i = startIndex; i < methodDefCount; i++)
                    {
                        IfFailGo(m_methodDefTreatmentMemoTable.InitEntry(i, &mdTreatment));
                    }
                }
            }
        }
    }

    *ppMethodDefTreatment = mdTreatment;
    hr = S_OK;

  ErrExit:
    return hr;
}

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

// Compute MethodTreatment value for a methodDef (unlike GetMethodDefTreatment, this
//  does not cache.)
//
HRESULT WinMDAdapter::ComputeMethodDefTreatment(mdMethodDef tkMethodDef, mdTypeDef tkDeclaringTypeDef, ULONG *ppMethodDefTreatment)
{
    HRESULT hr;

    BYTE mdTreatment = kMdImplementation;

    LPCSTR  szDeclaringTypeName;
    DWORD   parentTdAttr;
    mdToken extends;
    IfFailGo(m_pRawMetaModelCommonRO->CommonGetTypeDefProps(tkDeclaringTypeDef, NULL, &szDeclaringTypeName, &parentTdAttr, &extends, NULL));

    // We only want to treat this method special if it has metadata exposed to WinRT
    if (IsTdWindowsRuntime(parentTdAttr))
    {
        LPCSTR szUnmangledName;
        IfFailGo(CheckIfClrImplementationType(szDeclaringTypeName, parentTdAttr, &szUnmangledName));
        if (hr == S_OK)
        {
            mdTreatment = kMdImplementation;
        }
        else if (IsTdNested(parentTdAttr))
        {
            // nested types are implementation
            mdTreatment = kMdImplementation;
        }
        else if (parentTdAttr & tdInterface)
        {
            // Method is declared on an interface.
            mdTreatment = kMdInterface;
        }
        else if (m_scenario == kWinMDExp && (parentTdAttr & tdPublic) == 0)
        {
            // internal classes generated by WinMDExp are implementation
            mdTreatment = kMdImplementation;
        }
        else
        {
            mdTreatment = kMdOther;

            if (TypeFromToken(extends) == mdtTypeRef)
            {
                ULONG trTreatment;
                IfFailGo(GetTypeRefTreatment(extends, &trTreatment));
                if (trTreatment == kTrSystemDelegate)
                {
                    // Method is declared on a delegate
                    mdTreatment = kMdDelegate;
                }
                else if (trTreatment == kTrSystemAttribute)
                {
                    // Method is declared on a attribute
                    mdTreatment = kMdAttribute;
                }
            }
        }
    }
    if (mdTreatment == kMdOther)
    {
        // we want to hide the method if it implements only redirected interfaces
        // Also we want to check if the methodImpl is IClosable.Close then we will change the name.
        bool fSeenRedirectedInterfaces = false;
        bool fSeenNonRedirectedInterfaces = false;
        
        bool isIClosableCloseMethod = false;
        
        mdToken tkMethodImplFirst;
        ULONG count;
        mdTypeRef mtTypeRef;
        IfFailGo(m_pRawMetaModelCommonRO->CommonGetMethodImpls(tkDeclaringTypeDef, &tkMethodImplFirst, &count));
        for (ULONG i = 0; i < count; i++)
        {
            mdToken tkMethodImpl = tkMethodImplFirst + i;
            mdToken tkBody, tkDecl;
            IfFailGo(m_pRawMetaModelCommonRO->CommonGetMethodImplProps(tkMethodImpl, &tkBody, &tkDecl));

            if (tkBody == tkMethodDef)
            {
                // See if this MethodImpl implements a redirected interface
                UINT nIndex;
                IfFailGo(CheckIfMethodImplImplementsARedirectedInterface(tkDecl, &nIndex));
                if (hr == S_FALSE)
                {
                    // Now we know this implements a non-redirected interface
                    // But we need to keep looking, just in case we got a MethodImpl that implements
                    // the IClosable.Close method and needs to be renamed
                    fSeenNonRedirectedInterfaces = true;
                }
                else if (SUCCEEDED(hr))
                {
                    fSeenRedirectedInterfaces = true;
                    if (nIndex == WinMDAdapter::RedirectedTypeIndex_Windows_Foundation_IClosable)
                    {
                        // This method implements IClosable.Close 
                        // Let's rename it to Dispose later
                        // Once we know this implements IClosable.Close, we are done looking as we know 
                        // we won't hide it
                        isIClosableCloseMethod = true;
                        break;
                    }
                }
            }
        }

        if (isIClosableCloseMethod)
        {
            // Rename IClosable.Close to Dispose
            mdTreatment = kMdRenameToDisposeMethod;
        }
        else if (fSeenRedirectedInterfaces && !fSeenNonRedirectedInterfaces)
        {
            // Only hide if all the interfaces implemented are redirected
            mdTreatment = kMdHiddenImpl;
        }
    }

    // If treatment is other, then this is a non-managed WinRT runtime class definition. Find out about various bits that we apply via attrubtes and name parsing.
    if (mdTreatment == kMdOther)
    {
        // Scan through Custom Attributes on type, looking for interesting bits.
        HRESULT hrCA;
        hrCA = m_pRawMetaModelCommonRO->CommonGetCustomAttributeByNameEx(tkMethodDef, "Windows.UI.Xaml.TreatAsPublicMethodAttribute", NULL, NULL, NULL);
        if (hrCA == S_OK)
        {
            mdTreatment |= kMdMarkPublicFlag;
        }


        hrCA = m_pRawMetaModelCommonRO->CommonGetCustomAttributeByNameEx(tkMethodDef, "Windows.UI.Xaml.TreatAsAbstractMethodAttribute", NULL, NULL, NULL);
        if (hrCA == S_OK)
        {
            mdTreatment |= kMdMarkAbstractFlag;
        }

        LPCSTR szName;
        DWORD dwFlags;
        IfFailRet(m_pRawMetaModelCommonRO->CommonGetMethodDefProps(tkMethodDef, &szName, &dwFlags, NULL, NULL));
    }
    *ppMethodDefTreatment = mdTreatment;
    hr = S_OK;

ErrExit:
    return hr;
}

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

//
// Finds a CA by its (transformed) name
//
HRESULT WinMDAdapter::GetCustomAttributeByName( // S_OK or error.
    mdToken            tkObj,      // [IN] Object with Custom Attribute.
    LPCUTF8            szName,     // [IN] Name of desired Custom Attribute.
    mdCustomAttribute *ptkCA,      // [OUT] Put custom attribute token here
    const void       **ppData,     // [OUT] Put pointer to data here.
    ULONG             *pcbData)    // [OUT] Put size of data here.
{
    HRESULT hr;
    _ASSERTE(szName);

    if (ConvertWellKnownTypeNameFromClrToWinRT(&szName))
    {
        mdCustomAttribute tkCA;
        IfFailGo(m_pRawMetaModelCommonRO->CommonGetCustomAttributeByNameEx(tkObj, szName, &tkCA, NULL, NULL));
        if (hr == S_FALSE)
            goto ErrExit;
        if (ptkCA)
            *ptkCA = tkCA;
        IfFailGo(GetCustomAttributeBlob(tkCA, ppData, pcbData));
    }
    else
    {
        IfFailGo(m_pRawMetaModelCommonRO->CommonGetCustomAttributeByNameEx(tkObj, szName, ptkCA, ppData, pcbData));
    }
  ErrExit:
    return hr;
}

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

//
// Modify CA blobs
//
HRESULT WinMDAdapter::GetCustomAttributeBlob(
    mdCustomAttribute tkCA,
    const void  **ppData,         // [OUT] Put pointer to data here.
    ULONG        *pcbData)        // [OUT] Put size of data here.
{
    HRESULT hr;

    _ASSERTE(TypeFromToken(tkCA) == mdtCustomAttribute);
    ULONG index = RidFromToken(tkCA) - 1;

    // If someone already queried this CA, use the previous result.
    CABlob * pCABlob;
    IfFailGo(m_redirectedCABlobsMemoTable.GetEntry(index, &pCABlob));
    if (hr == S_FALSE)
    {
        // No, we're the first. Initialize the entry (keeping in mind we may be racing with other threads.)
        pCABlob = CABlob::NOREDIRECT;
        mdToken tkOwner;
        mdToken tkCtor;
        IfFailGo(m_pRawMetaModelCommonRO->CommonGetCustomAttributeProps(tkCA, &tkOwner, &tkCtor, NULL, NULL));
        if (TypeFromToken(tkOwner) == mdtTypeDef)   // AttributeUsageAttribute only goes on a typeDef, so if the owner isn't a typeDef, no point in going further.
        {
            if (TypeFromToken(tkCtor) == mdtMemberRef)  // REX has promised to use a memberRef (not Def) here.
            {
                mdToken tkCtorType;
                IfFailGo(m_pRawMetaModelCommonRO->CommonGetMemberRefProps(tkCtor, &tkCtorType));
                if (TypeFromToken(tkCtorType) == mdtTypeRef)  // REX has promised to use a typeRef (not a Def, or heavens forbid, a Spec) here
                {
                    RedirectedTypeIndex redirectedTypeIndex;
                    IfFailGo(GetTypeRefRedirectedInfo(tkCtorType, &redirectedTypeIndex));
                    _ASSERTE((hr == S_OK) || (hr == S_FALSE));
                    
                    if ((hr == S_OK) && (redirectedTypeIndex == RedirectedTypeIndex_Windows_Foundation_Metadata_AttributeUsageAttribute))
                    {
                        // We found a Windows.Foundation.Metadata.AttributeUsageAttribute. The TypeRef redirection already makes this 
                        // look like a System.AttributeUsageAttribute. Must munge the blob so that it matches the CLR expections.
                        BOOL  allowMultiple;
                        DWORD clrTargetValue;
                        IfFailGo(TranslateWinMDAttributeUsageAttribute(tkOwner, &clrTargetValue, &allowMultiple));
                        if (hr == S_OK)
                        {
                            CABlob *pNewCABlob = NULL;
                            IfFailGo(CreateClrAttributeUsageAttributeCABlob(clrTargetValue, allowMultiple, &pNewCABlob));
                            pCABlob = pNewCABlob;
                        }
                    }
                }
            }
        }

        IfFailGo(m_redirectedCABlobsMemoTable.InitEntry(index, &pCABlob));
    }

    _ASSERTE(pCABlob != NULL);
    const void *pData;
    ULONG      cbData;
    if (pCABlob == CABlob::NOREDIRECT)
    {
        // The normal case: don't rewrite the blob.
        IfFailGo(m_pRawMetaModelCommonRO->CommonGetCustomAttributeProps(tkCA, NULL, NULL, &pData, &cbData));
    }
    else
    {
        // The special cases: Blob was rewritten, return the rewritten blob.
        pData = pCABlob->data;
        cbData = pCABlob->cbBlob;
    }
    if (ppData)
        *ppData = pData;
    if (pcbData)
        *pcbData = cbData;

    hr = S_OK;
  ErrExit:
    return hr;
}

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

// Note: This method will look in a cache for the reinterpreted signature, but does not add any values to 
// the cache or do any work on failure.  If we can't find it then it returns S_FALSE.
HRESULT WinMDAdapter::GetCachedSigForToken(
    mdToken          token,             // [IN] given token
    MemoTable<SigData*, SigData::Destroy> &memoTable, // [IN] the MemoTable to use
    ULONG           *pcbSigBlob,        // [OUT] count of bytes in the signature blob
    PCCOR_SIGNATURE *ppSig,             // [OUT] new signature
    BOOL            *pfPassThrough      // [OUT] did the cache say we don't need to reinterpret this sig?
)
{
    _ASSERTE(pfPassThrough != NULL);

    HRESULT hr;
    
    ULONG index = RidFromToken(token) - 1;

    // If someone already queried this method signature, use the previous result.
    SigData *pSigData = NULL;
    IfFailGo(memoTable.GetEntry(index, &pSigData));
    if (hr == S_FALSE)
    {
        *pfPassThrough = FALSE;
        return S_FALSE;
    }

    _ASSERTE(pSigData != NULL);

    if (pSigData == SigData::NOREDIRECT)
    {
        // The normal case: don't rewrite the signature.
        *pfPassThrough = TRUE;
        return S_FALSE;
    }
    else
    {
        *pfPassThrough = FALSE;
        // The signature was rewritten, return the rewritten sig.
        if (ppSig)
            *ppSig = (PCCOR_SIGNATURE) pSigData->data;
        if (pcbSigBlob)
            *pcbSigBlob = pSigData->cbSig;
    }

    hr = S_OK;

  ErrExit:
    return hr;
}

//------------------------------------------------------------------------------
// static
HRESULT WinMDAdapter::InsertCachedSigForToken(
    mdToken          token,             // [IN] given token
    MemoTable<SigData*, SigData::Destroy> &memoTable, // [IN] the MemoTable to use
    SigData        **ppSigData          // [IN, OUT] new signature or SigData::NOREDIRECT if the signature didn't need to be reparsed,
)                                       // will be updated with another (but identical) SigData* if this thread lost the race
{
    _ASSERTE(ppSigData != NULL && *ppSigData != NULL);

    HRESULT hr;
    ULONG index = RidFromToken(token) - 1;

    IfFailGo(memoTable.InitEntry(index, ppSigData));
    hr = S_OK;

  ErrExit:
    return hr;
}

//------------------------------------------------------------------------------
static HRESULT FinalizeSignatureRewrite(
    SigBuilder      & newSig,
    BOOL              fChangedSig,
    WinMDAdapter::SigData **ppSigData
    DEBUG_ARG(ULONG cbOrigSigBlob)
    )
{
    // Make sure we didn't lose anything, since we wrote out the full signature.
    ULONG  cbNewSigLen;
    BYTE * pNewSigBytes = (BYTE *) newSig.GetSignature(&cbNewSigLen);
    _ASSERTE(cbNewSigLen == cbOrigSigBlob);  // Didn't lose any data nor add anything

    // Set the output SigData appropriately.
    if (fChangedSig)
    {
        *ppSigData = WinMDAdapter::SigData::Create(cbNewSigLen, pNewSigBytes);
        
        if (*ppSigData == NULL)
        {
            return E_OUTOFMEMORY;
        }
    }
    else
    {
        *ppSigData = WinMDAdapter::SigData::NOREDIRECT;
    }
    return S_OK;
}

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

// Purpose: Translate method signatures containing classes that we're projecting as value types, and vice versa.
// Example: ELEMENT_TYPE_CLASS [IReference<T>] to ELEMENT_TYPE_VALUETYPE [Nullable<T>]
// Example: ELEMENT_TYPE_VALUETYPE [Windows.Foundation.HResult] to ELEMENT_TYPE_CLASS [Exception]
HRESULT WinMDAdapter::ReinterpretMethodSignature(
    ULONG             cbOrigSigBlob,    // [IN] count of bytes in the original signature blob
    PCCOR_SIGNATURE   pOrigSig,         // [IN] original signature
    SigData         **ppSigData         // [OUT] new signature or SigData::NOREDIRECT
)
{
    _ASSERTE(pOrigSig != NULL);
    _ASSERTE(ppSigData != NULL);

    HRESULT hr;
    
    // @REVISIT_TODO: Need to allocate memory here.  We cannot take a lock though (such as any lock needed by 'new'), or we can get 
    // into deadlocks from profilers that need to inspect metadata to walk the stack.  Needs some help from some loader/ngen experts.

    BOOL fChangedSig = FALSE;

    // The following implements MethodDef signature parsing, per ECMA CLI spec, section 23.2.1.
    SigParser sigParser(pOrigSig, cbOrigSigBlob);
    SigBuilder newSig(cbOrigSigBlob);   // We will not change the signature size, just modify it a bit (E_T_CLASS <-> E_T_VALUETYPE)
    
    // Read calling convention info - Note: Calling convention is always one byte
    ULONG callingConvention;
    IfFailGo(sigParser.GetCallingConvInfo(&callingConvention));
    _ASSERTE((callingConvention & 0xff) == callingConvention);
    newSig.AppendByte((BYTE)callingConvention);

    // If it is generic, read the generic parameter count
    if ((callingConvention & CORINFO_CALLCONV_GENERIC) != 0)
    {
        ULONG genericArgsCount;
        IfFailGo(sigParser.GetData(&genericArgsCount));
        newSig.AppendData(genericArgsCount);
    }
    
    // Read number of locals / method parameters
    ULONG cParameters;
    IfFailGo(sigParser.GetData(&cParameters));
    newSig.AppendData(cParameters);
    
    if (callingConvention != CORINFO_CALLCONV_LOCAL_SIG)
    {
        // Read return type
        IfFailGo(RewriteTypeInSignature(&sigParser, &newSig, &fChangedSig));
    }

    // Visit each local / parameter
    for (ULONG i = 0; i < cParameters; i++)
    {
        IfFailGo(RewriteTypeInSignature(&sigParser, &newSig, &fChangedSig));
    }

    IfFailGo(FinalizeSignatureRewrite(newSig, fChangedSig, ppSigData DEBUG_ARG(cbOrigSigBlob)));
    return S_OK;
    
ErrExit:
    Debug_ReportError("Couldn't parse a signature in WinMDAdapter::ReinterpretMethodSignature!");
    return hr;
} // WinMDAdapter::ReinterpretMethodSignature


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

// Purpose: Translate FieldDef signatures containing classes that we're projecting as value types, and vice versa.
// Example: ELEMENT_TYPE_VALUETYPE [Windows.Foundation.HResult] to ELEMENT_TYPE_CLASS [Exception]
HRESULT WinMDAdapter::ReinterpretFieldSignature(
    ULONG             cbOrigSigBlob,    // [IN] count of bytes in the original signature blob
    PCCOR_SIGNATURE   pOrigSig,         // [IN] original signature
    SigData         **ppSigData         // [OUT] new signature or SigData::NOREDIRECT
)
{
    _ASSERTE(pOrigSig != NULL);
    _ASSERTE(ppSigData != NULL);

    HRESULT hr = S_OK;
    BOOL fChangedSig = FALSE;
    
    // The following implements FieldDef signature parsing, per ECMA CLI spec, section 23.2.4.
    // Format is FIELD [custom modifiers]* Type
    SigParser sigParser(pOrigSig, cbOrigSigBlob);
    SigBuilder newSig(cbOrigSigBlob);   // We will not change the signature size, just modify it a bit (E_T_CLASS <-> E_T_VALUETYPE)
    
    // Read calling convention info - this should be IMAGE_CEE_CS_CALLCONV_FIELD.
    ULONG callingConvention;
    IfFailGo(sigParser.GetCallingConvInfo(&callingConvention));
    _ASSERTE((callingConvention & 0xff) == callingConvention);
    _ASSERTE(callingConvention == IMAGE_CEE_CS_CALLCONV_FIELD);
    newSig.AppendByte((BYTE)callingConvention);

    // Rewrite field type
    IfFailGo(RewriteTypeInSignature(&sigParser, &newSig, &fChangedSig));

    IfFailGo(FinalizeSignatureRewrite(newSig, fChangedSig, ppSigData DEBUG_ARG(cbOrigSigBlob)));
    return S_OK;
    
ErrExit:
    Debug_ReportError("Couldn't parse a signature in WinMDAdapter::ReinterpretFieldSignature!");
    return hr;
} // WinMDAdapter::ReinterpretFieldSignature


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

// Purpose: Translate TypeSpec signatures containing classes that we're projecting as value types, and vice versa.
HRESULT WinMDAdapter::ReinterpretTypeSpecSignature(
    ULONG             cbOrigSigBlob,    // [IN] count of bytes in the original signature blob
    PCCOR_SIGNATURE   pOrigSig,         // [IN] original signature
    SigData         **ppSigData         // [OUT] new signature or SigData::NOREDIRECT
)
{
    _ASSERTE(pOrigSig != NULL);
    _ASSERTE(ppSigData != NULL);

    HRESULT hr = S_OK;
    BOOL fChangedSig = FALSE;

    // The following implements TypeSpec signature parsing, per ECMA CLI spec, section 23.2.14.
    // Format is [custom modifiers]* Type
    SigParser sigParser(pOrigSig, cbOrigSigBlob);
    SigBuilder newSig(cbOrigSigBlob);   // We will not change the signature size, just modify it a bit (E_T_CLASS <-> E_T_VALUETYPE)
    
    // Rewrite the type
    IfFailGo(RewriteTypeInSignature(&sigParser, &newSig, &fChangedSig));

    IfFailGo(FinalizeSignatureRewrite(newSig, fChangedSig, ppSigData DEBUG_ARG(cbOrigSigBlob)));
    return S_OK;
    
ErrExit:
    Debug_ReportError("Couldn't parse a signature in WinMDAdapter::ReinterpretTypeSpecSignature!");
    return hr;
} // WinMDAdapter::ReinterpretTypeSpecSignature


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

// Purpose: Translate MethodSpec signatures containing classes that we're projecting as value types, and vice versa.
HRESULT WinMDAdapter::ReinterpretMethodSpecSignature(
    ULONG             cbOrigSigBlob,    // [IN] count of bytes in the original signature blob
    PCCOR_SIGNATURE   pOrigSig,         // [IN] original signature
    SigData         **ppSigData         // [OUT] new signature or SigData::NOREDIRECT
)
{
    _ASSERTE(pOrigSig != NULL);
    _ASSERTE(ppSigData != NULL);

    HRESULT hr = S_OK;
    BOOL fChangedSig = FALSE;

    // The following implements MethodSpec signature parsing, per ECMA CLI spec, section 23.2.15.
    // Format is GENERICINST GenArgCount Type+
    SigParser sigParser(pOrigSig, cbOrigSigBlob);
    SigBuilder newSig(cbOrigSigBlob);   // We will not change the signature size, just modify it a bit (E_T_CLASS <-> E_T_VALUETYPE)
    
    // Read calling convention info - this should be IMAGE_CEE_CS_CALLCONV_GENERICINST.
    ULONG callingConvention;
    IfFailGo(sigParser.GetCallingConvInfo(&callingConvention));
    _ASSERTE((callingConvention & 0xff) == callingConvention);
    _ASSERTE(callingConvention == IMAGE_CEE_CS_CALLCONV_GENERICINST);
    newSig.AppendByte((BYTE)callingConvention);

    // Read number of generic arguments
    ULONG cArguments;
    IfFailGo(sigParser.GetData(&cArguments));
    newSig.AppendData(cArguments);

    // Rewrite each argument
    for (ULONG i = 0; i < cArguments; i++)
    {
        IfFailGo(RewriteTypeInSignature(&sigParser, &newSig, &fChangedSig));
    }

    IfFailGo(FinalizeSignatureRewrite(newSig, fChangedSig, ppSigData DEBUG_ARG(cbOrigSigBlob)));
    return S_OK;
    
ErrExit:
    Debug_ReportError("Couldn't parse a signature in WinMDAdapter::ReinterpretMethodSpecSignature!");
    return hr;
} // WinMDAdapter::ReinterpretMethodSpecSignature


//------------------------------------------------------------------------------
// 
// We expose some WinRT types to managed as CLR types, while changing reference type <-> value type:
//  E_T_CLASS Windows.Foundation.IReference<T>                  ---> E_T_VALUETYPE System.Nullable<T>
//  E_T_CLASS Windows.Foundation.Collections.IKeyValuePair<U,V> ---> E_T_VALUETYPE System.Collections.Generic.KeyValuePair<U,V>
//  E_T_VALUETYPE Windows.UI.Xaml.Interop.TypeName              ---> E_T_CLASS System.Type
//  E_T_VALUETYPE Windows.Foundation.HResult                    ---> E_T_CLASS System.Exception
HRESULT WinMDAdapter::RewriteTypeInSignature(
    SigParser *  pSigParser, 
    SigBuilder * pSigBuilder, 
    BOOL *       pfChangedSig)
{
    HRESULT hr;
    
    BYTE elementType;
    IfFailGo(pSigParser->GetByte(&elementType));

    switch (elementType)
    {
    // Simple types with no additional data in the signature
    case ELEMENT_TYPE_VOID:
    case ELEMENT_TYPE_BOOLEAN:
    case ELEMENT_TYPE_CHAR:
    case ELEMENT_TYPE_I1:
    case ELEMENT_TYPE_U1:
    case ELEMENT_TYPE_I2:
    case ELEMENT_TYPE_U2:
    case ELEMENT_TYPE_I4:
    case ELEMENT_TYPE_U4:
    case ELEMENT_TYPE_I8:
    case ELEMENT_TYPE_U8:
    case ELEMENT_TYPE_R4:
    case ELEMENT_TYPE_R8:
    case ELEMENT_TYPE_STRING:
    case ELEMENT_TYPE_I:
    case ELEMENT_TYPE_U:
    case ELEMENT_TYPE_OBJECT:
    case ELEMENT_TYPE_TYPEDBYREF:     // TYPEDREF  (it takes no args) a typed reference to some other type
        {
            pSigBuilder->AppendByte(elementType);
            break;
        }

        // Read a token
    case ELEMENT_TYPE_CLASS:
        {
            mdToken token;
            IfFailGo(pSigParser->GetToken(&token));
            
            if (TypeFromToken(token) == mdtTypeRef)
            {
                RedirectedTypeIndex nRedirectedTypeIndex;
                IfFailGo(this->GetTypeRefRedirectedInfo(token, &nRedirectedTypeIndex));
                _ASSERTE((hr == S_OK) || (hr == S_FALSE));
                
                if (hr == S_OK)
                {   // TypeRef is well known redirectetd type (with index in array code:g_rgRedirectedTypes)
                    if (nRedirectedTypeIndex == RedirectedTypeIndex_Windows_Foundation_IReference ||
                        nRedirectedTypeIndex == RedirectedTypeIndex_Windows_Foundation_Collections_IKeyValuePair)
                    {   // The TypeRef name was changed to System.Nullable or System.Collections.Generic.KeyValuePair`2 (value type, not class)
                        elementType = ELEMENT_TYPE_VALUETYPE;
                        *pfChangedSig = TRUE;
                    }
                }
                // We do not want to return S_FALSE
                hr = S_OK;
            }
            
            pSigBuilder->AppendByte(elementType);
            pSigBuilder->AppendToken(token);
            
            break;
        }
    case ELEMENT_TYPE_VALUETYPE:
        {
            mdToken token;
            IfFailGo(pSigParser->GetToken(&token));
            
            if (TypeFromToken(token) == mdtTypeRef)
            {
                RedirectedTypeIndex nRedirectedTypeIndex;
                IfFailGo(this->GetTypeRefRedirectedInfo(token, &nRedirectedTypeIndex));
                _ASSERTE((hr == S_OK) || (hr == S_FALSE));
                
                if (hr == S_OK)
                {   // TypeRef is well known redirectetd type (with index in array code:g_rgRedirectedTypes)
                    if (nRedirectedTypeIndex == RedirectedTypeIndex_Windows_UI_Xaml_Interop_TypeName ||
                        nRedirectedTypeIndex == RedirectedTypeIndex_Windows_Foundation_HResult)
                    {
                        // TypeIdentifier and HResult are all value types in winmd and are mapped to reference
                        // types in CLR: Type, and Exception.
                        elementType = ELEMENT_TYPE_CLASS;
                        *pfChangedSig = TRUE;
                    }
                }
                // We do not want to return S_FALSE
                hr = S_OK;
            }
            
            pSigBuilder->AppendByte(elementType);
            pSigBuilder->AppendToken(token);
            
            break;
        }
        
        // Read a type
    case ELEMENT_TYPE_SZARRAY:  // SZARRAY <type>
    case ELEMENT_TYPE_PTR:      // PTR <type>
    case ELEMENT_TYPE_BYREF:    // BYREF <type>
    case ELEMENT_TYPE_SENTINEL: // sentinel for VARARGS ("..." in the parameter list), it behaves as prefix to next arg
        {
            pSigBuilder->AppendByte(elementType);
            
            IfFailGo(RewriteTypeInSignature(pSigParser, pSigBuilder, pfChangedSig));
            break;
        }

        // Read a token then a type.  That type could be another custom modifier.
    case ELEMENT_TYPE_CMOD_REQD:     // required C modifier : E_T_CMOD_REQD <mdTypeRef/mdTypeDef> [followed by a type, or another custom modifier]
    case ELEMENT_TYPE_CMOD_OPT:      // optional C modifier : E_T_CMOD_OPT <mdTypeRef/mdTypeDef>
        {
            pSigBuilder->AppendByte(elementType);
            
            mdToken token;
            IfFailGo(pSigParser->GetToken(&token));
            pSigBuilder->AppendToken(token);
            
            // Process next type or custom modifier
            IfFailGo(RewriteTypeInSignature(pSigParser, pSigBuilder, pfChangedSig));
            break;
        }

        // Read a number
    case ELEMENT_TYPE_VAR:     // a class type variable VAR <number>
    case ELEMENT_TYPE_MVAR:    // a method type variable MVAR <number>
        {
            pSigBuilder->AppendByte(elementType);
            
            ULONG number;
            IfFailGo(pSigParser->GetData(&number));
            pSigBuilder->AppendData(number);
            break;
        }

    case ELEMENT_TYPE_ARRAY:     // MDARRAY <type> <rank> <bcount> <bound1> ... <lbcount> <lb1> ...
        {
            pSigBuilder->AppendByte(elementType);
            
            // Read array type
            IfFailGo(RewriteTypeInSignature(pSigParser, pSigBuilder, pfChangedSig));
            
            // Read rank
            ULONG rank;
            IfFailGo(pSigParser->GetData(&rank));
            pSigBuilder->AppendData(rank);

            // If rank is 0, then there's nothing else in the array signature
            if (rank != 0)
            {
                // Read number of dimension sizes
                ULONG cDimensionSizes;
                IfFailGo(pSigParser->GetData(&cDimensionSizes));
                pSigBuilder->AppendData(cDimensionSizes);
                
                // Read all dimension sizes
                for (ULONG i = 0; i < cDimensionSizes; i++)
                {
                    ULONG dimensionSize;
                    IfFailGo(pSigParser->GetData(&dimensionSize));
                    pSigBuilder->AppendData(dimensionSize);
                }
                
                // Read number of lower bounds
                ULONG cLowerBounds;
                IfFailGo(pSigParser->GetData(&cLowerBounds));
                pSigBuilder->AppendData(cLowerBounds);

                // Read all lower bounds
                for (ULONG i = 0; i < cLowerBounds; i++)
                {
                    ULONG lowerBound;
                    IfFailGo(pSigParser->GetData(&lowerBound));
                    pSigBuilder->AppendData(lowerBound);
                }
            }
            break;
        }

    case ELEMENT_TYPE_GENERICINST:     // GENERICINST <generic type> <argCnt> <arg1> ... <argn>
        {
            pSigBuilder->AppendByte(elementType);
            
            // Read the generic type
            IfFailGo(RewriteTypeInSignature(pSigParser, pSigBuilder, pfChangedSig));
            
            // Read arg count
            ULONG cGenericTypeArguments;
            IfFailGo(pSigParser->GetData(&cGenericTypeArguments));
            pSigBuilder->AppendData(cGenericTypeArguments);
            
            // Read each type argument
            for (ULONG i = 0; i < cGenericTypeArguments; i++)
            {
                IfFailGo(RewriteTypeInSignature(pSigParser, pSigBuilder, pfChangedSig));
            }
            break;
        }
        
    case ELEMENT_TYPE_FNPTR:     // FNPTR <complete sig for the function including calling convention>
        /*
        // FNPTR is not supported in C#/VB, thefore this is not a main scenario.
        // This implementation was late during .NET 4.5, but may be useful in future releases when we decide to support more languages for managed WinMD implementation.
        {
            pSigBuilder->AppendByte(elementType);
            
            // Read calling convention
            DWORD callingConvention;
            IfFailGo(pSigParser->GetData(&callingConvention));
            pSigBuilder->AppendData(callingConvention);
            
            // Read arg count
            ULONG cArgs;
            IfFailGo(pSigParser->GetData(&cArgs));
            pSigBuilder->AppendData(cArgs);
            
            // Read return argument
            IfFailGo(RewriteTypeInSignature(pSigParser, pSigBuilder, pfChangedSig));
            
            // Read each argument
            for (ULONG i = 0; i < cArgs; i++)
            {
                IfFailGo(RewriteTypeInSignature(pSigParser, pSigBuilder, pfChangedSig));
            }
            break;
        }
        */
        Debug_ReportError("ELEMENT_TYPE_FNPTR signature parsing in WinMD Adapter is NYI.");
        IfFailGo(E_FAIL);
        
    case ELEMENT_TYPE_END:
    case ELEMENT_TYPE_INTERNAL:     // INTERNAL <typehandle>  (Only in ngen images, but not reachable from MetaData - no sig rewriting)
    case ELEMENT_TYPE_PINNED:       // PINNED <type>, used only in LocalSig (no sig rewriting)
        Debug_ReportError("Unexpected CorElementType in a signature.  Sig parsing failing.");
        IfFailGo(E_FAIL);
        
    default:
        Debug_ReportError("Unknown CorElementType.");
        IfFailGo(E_FAIL);
    }
    _ASSERTE(hr == S_OK);
    return hr;
    
ErrExit:
    Debug_ReportError("Sig parsing failed.");
    return hr;
} // WinMDAdapter::RewriteTypeInSignature

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



//---------------------------------------------------------------------------------
// Windows.Foundation.Metadata.AttributeTarget and System.AttributeTarget enum
// define different bits for everything (@todo: Be nice to change that before we ship.)
// Do the conversion here.
//---------------------------------------------------------------------------------
static DWORD ConvertToClrAttributeTarget(DWORD winRTTarget)
{
    struct AttributeTargetsPair
    {
        DWORD WinRTValue;
        DWORD ClrValue;
    };
    
    static const AttributeTargetsPair s_attributeTargetPairs[] =
    {
#define DEFINE_PROJECTED_TYPE(a,b,c,d,e,f,g,h,i)
#define DEFINE_PROJECTED_ATTRIBUTETARGETS_VALUE(winrt, clr) { winrt, clr },
#include "WinRTProjectedTypes.h"
#undef DEFINE_PROJECTED_ATTRIBUTETARGETS_VALUES
#undef DEFINE_PROJECTED_TYPE
    };

    if (winRTTarget == 0xffffffff /* Windows.Foundation.Metadata.AttributeTargets.All */)
        return 0x00007fff;  /* System.AttributeTargets.All */
    DWORD clrTarget = 0;
    for (UINT i = 0; i < sizeof(s_attributeTargetPairs)/sizeof(*s_attributeTargetPairs); i++)
    {
        if (winRTTarget & s_attributeTargetPairs[i].WinRTValue)
        {
            clrTarget |= s_attributeTargetPairs[i].ClrValue;
        }
    }
    return clrTarget;
}


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


// Search a typeDef for WF.AttributeUsageAttribute and WF.AllowMultipleAttribute, and compute
//
//     *pClrTargetValue - the equivalent System.AttributeTargets enum value
//     *pAllowMultiple  - where multiple instances of the CA are allowed.
//
// Returns:
//     S_OK:     a WF.AttributeUsageAttribute CA exists (this is the one that we will rewrite)
//     S_FALSE:  no WF.AttributeUsageAttribute CA exists
//
HRESULT WinMDAdapter::TranslateWinMDAttributeUsageAttribute(mdTypeDef tkTypeDefOfCA, /*[out]*/ DWORD *pClrTargetValue, /*[out]*/ BOOL *pAllowMultiple)
{
    HRESULT hr;
    _ASSERTE(TypeFromToken(tkTypeDefOfCA) == mdtTypeDef);
    _ASSERTE(pClrTargetValue != NULL);
    _ASSERTE(pAllowMultiple != NULL);

    const BYTE *pbWFUsageBlob;
    ULONG       cbWFUsageBlob;
    IfFailGo(m_pRawMetaModelCommonRO->CommonGetCustomAttributeByName(tkTypeDefOfCA, "Windows.Foundation.Metadata.AttributeUsageAttribute", (const void **)&pbWFUsageBlob, &cbWFUsageBlob));
    if (hr == S_FALSE)
        goto ErrExit;
    // Expected blob format:
    //    01 00        - Fixed prolog for CA's
    //    xx xx xx xx  - The Windows.Foundation.Metadata.AttributeTarget value
    //    00 00        - Indicates 0 name/value pairs following.
    if (cbWFUsageBlob != 2 + sizeof(DWORD) + 2)  
    {
        IfFailGo(COR_E_BADIMAGEFORMAT);
    }

    {
        DWORD wfTargetValue = *(DWORD*)(pbWFUsageBlob + 2);
        *pClrTargetValue = ConvertToClrAttributeTarget(wfTargetValue);
    }
    
    // add AttributeTargets.Method, AttributeTargets.Constructor , AttributeTargets.Property, and AttributeTargets.Event if this is the VersionAttribute
    LPCSTR  szNamespace;
    LPCSTR  szName;
    IfFailGo(m_pRawMetaModelCommonRO->CommonGetTypeDefProps(tkTypeDefOfCA, &szNamespace, &szName, NULL, NULL, NULL));

    if ((strcmp(szName, "VersionAttribute") == 0 || strcmp(szName, "DeprecatedAttribute") == 0) && strcmp(szNamespace, "Windows.Foundation.Metadata") == 0)
    {
        *pClrTargetValue |= 0x2E0;
    }

    const BYTE *pbWFAllowMultipleBlob;
    ULONG       cbWFAllowMultipleBlob;
    IfFailGo(m_pRawMetaModelCommonRO->CommonGetCustomAttributeByName(tkTypeDefOfCA, "Windows.Foundation.Metadata.AllowMultipleAttribute", (const void **)&pbWFAllowMultipleBlob, &cbWFAllowMultipleBlob));
    *pAllowMultiple = (hr == S_OK);
    hr = S_OK;

  ErrExit:
    return hr;
}

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

/*static*/ HRESULT WinMDAdapter::CreateClrAttributeUsageAttributeCABlob(DWORD clrTargetValue, BOOL allowMultiple, CABlob **ppCABlob)
{
    // Emit the blob format corresponding to:
    //    [System.AttributeUsage(System.AttributeTargets.xx, AllowMultiple=yy)]
    //
    //    01 00         - Fixed prolog for CA's
    //    xx xx xx xx   - The System.AttributeTarget value
    //    01 00         - Indicates 1 name/value pair following.
    //    54            - SERIALIZATION_TYPE_PROPERTY
    //    02            - ELEMENT_TYPE_BOOLEAN
    //    0d            - strlen("AllowMultiple") - 1
    //    41 6c ... 65  - "A" "l" "l" "o" "w" "M" "u" "l" "t" "i" "p" "l" "e"
    //    yy            - The boolean selection for "AllowMultiple"

    BYTE blob[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x54, 0x02, 0x0D, 0x41, 0x6C, 0x6C, 0x6F, 0x77, 0x4D, 0x75, 0x6C, 0x74, 0x69, 0x70, 0x6C, 0x65, 0x00 };
    blob[sizeof(blob) - 1] = allowMultiple ? 1 : 0;
    *((DWORD*)(blob+2)) = clrTargetValue;
    CABlob *pCABlob;
    IfNullRet(pCABlob = CABlob::Create(blob, sizeof(blob)));
    *ppCABlob = pCABlob;
    return S_OK;
}


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

// Gets Guid from Windows.Foundation.Metadata.Guid
HRESULT WinMDAdapter::GetItemGuid(mdToken tkObj, CLSID *pGuid)
{
    HRESULT hr;
    _ASSERTE(pGuid);
    *pGuid = GUID_NULL;
    const BYTE *pBlob;
    ULONG cbBlob;
    IfFailGo(m_pRawMetaModelCommonRO->CommonGetCustomAttributeByName(tkObj, "Windows.Foundation.Metadata.GuidAttribute", (const void**)&pBlob, &cbBlob));
    if (hr == S_OK)
    {
        if (cbBlob == 2 + sizeof(GUID) + 2)
        {
            memcpy(pGuid, pBlob + 2, sizeof(GUID));
            hr = S_OK;
            goto ErrExit;
        }
    }
    hr = S_FALSE;
  ErrExit:
    return hr;

}


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

//----------------------------------------------------------------------------
// Gets filtered methodImpl list and adds it to an existing DynamicArray enum.
//
// This is used to hide implementations of methods on redirected interfaces after
// we've replaced them with their CLR counterparts in the interface implementation list.
//
// Each filtered methodImpl adds two elements to the enum: the body and decl values
// in that order.
//----------------------------------------------------------------------------
HRESULT WinMDAdapter::AddMethodImplsToEnum(mdTypeDef tkTypeDef, HENUMInternal *henum)
{
    _ASSERTE(henum != NULL);
    _ASSERTE(henum->m_EnumType == MDDynamicArrayEnum);
    _ASSERTE(TypeFromToken(tkTypeDef) == mdtTypeDef);

    HRESULT hr;

    mdToken tkMethodImplFirst;
    ULONG count;
    IfFailGo(m_pRawMetaModelCommonRO->CommonGetMethodImpls(tkTypeDef, &tkMethodImplFirst, &count));
    for (ULONG i = 0; i < count; i++)
    {
        mdToken tkMethodImpl = tkMethodImplFirst + i;
        mdToken tkBody, tkDecl;
        IfFailGo(m_pRawMetaModelCommonRO->CommonGetMethodImplProps(tkMethodImpl, &tkBody, &tkDecl));
        UINT nIndex;
        IfFailGo(CheckIfMethodImplImplementsARedirectedInterface(tkDecl, &nIndex));
        if (hr == S_FALSE || 
            (SUCCEEDED(hr) && nIndex == WinMDAdapter::RedirectedTypeIndex_Windows_Foundation_IClosable))
        {
            // Keep MethodImpl for IClosable methods and non-redirected interfaces
            IfFailGo(HENUMInternal::AddElementToEnum(henum, tkBody));
            IfFailGo(HENUMInternal::AddElementToEnum(henum, tkDecl));
        }
    }

    hr = S_OK;
  ErrExit:
    return hr;
}

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

// S_OK if this is a CLR implementation type that was mangled and hidden by WinMDExp
//
// Logically, this function takes a mdTypeDef, but since the caller has already extracted the
// row data for other purposes, we'll take the row data.
HRESULT WinMDAdapter::CheckIfClrImplementationType(LPCSTR szName, DWORD dwAttr, LPCSTR *pszUnmangledName)
{
    if (m_scenario != kWinMDExp)
        return S_FALSE;                                       // Input file not produced by WinMDExp
    if (IsTdNested(dwAttr))
        return S_FALSE;                                       // Type is nested in another type
    if ((dwAttr & (tdPublic|tdSpecialName)) != tdSpecialName)
        return S_FALSE;                                       // Type public or not SpecialName
    if (0 != strncmp(szName, s_szCLRPrefix, s_ncCLRPrefix))
        return S_FALSE;                                       // Name does not begin with "<CLR>"

    // Ran out of reasons.
    *pszUnmangledName = szName + s_ncCLRPrefix;
    return S_OK;
}


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

//-------------------------------------------------------------------------------------
// Returns: S_OK if tkDecl of a methodImpl is a reference to a method on a redirected interface.
//-------------------------------------------------------------------------------------
HRESULT WinMDAdapter::CheckIfMethodImplImplementsARedirectedInterface(mdToken tkDecl, UINT *pIndex)
{

    HRESULT hr;
    if (TypeFromToken(tkDecl) != mdtMemberRef)
        return S_FALSE;   // REX will always use memberRef and typeRefs to refer to redirected interfaces, even if in same module.

    mdToken tkParent;
    IfFailRet(m_pRawMetaModelCommonRO->CommonGetMemberRefProps(tkDecl, &tkParent));

    mdTypeRef tkTypeRef;
    if (TypeFromToken(tkParent) == mdtTypeRef)
    {
        tkTypeRef = tkParent;
    }
    else if (TypeFromToken(tkParent) == mdtTypeSpec)
    {
        PCCOR_SIGNATURE pvSig;
        ULONG cbSig;
        IfFailRet(m_pRawMetaModelCommonRO->CommonGetTypeSpecProps(tkParent, &pvSig, &cbSig));
        static const BYTE expectedSigStart[] = {ELEMENT_TYPE_GENERICINST, ELEMENT_TYPE_CLASS};
        if (cbSig < sizeof(expectedSigStart) + 1)
            return S_FALSE;
        if (0 != memcmp(pvSig, expectedSigStart, sizeof(expectedSigStart)))
            return S_FALSE;
        const BYTE *pCodedToken = pvSig + sizeof(expectedSigStart);
        if (cbSig < sizeof(expectedSigStart) + CorSigUncompressedDataSize(pCodedToken))
            return S_FALSE;
        mdToken genericType = CorSigUncompressToken(/*modifies*/pCodedToken);
        if (TypeFromToken(genericType) != mdtTypeRef)
            return S_FALSE;
        tkTypeRef = genericType;
    }
    else
    {
        return S_FALSE;
    }

    ULONG treatment;
    IfFailRet(GetTypeRefTreatment(tkTypeRef, &treatment));
    if ((treatment & kTrClassMask) != kTrClassWellKnownRedirected)
        return S_FALSE;

    if (pIndex)
        *pIndex = (treatment & ~kTrClassMask);

    return S_OK;
}

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

/*static*/ HRESULT WinMDAdapter::CreatePrefixedName(LPCSTR szPrefix, LPCSTR szName, LPCSTR *ppOut)
{
    // This can cause allocations (thus entering the host) during a profiler stackwalk.
    // But we're ok since we're not supporting SQL/F1 profiling with WinMDs. FUTURE:
    // Would be nice to eliminate allocations on stack walks regardless.
    PERMANENT_CONTRACT_VIOLATION(HostViolation, ReasonUnsupportedForSQLF1Profiling);

    size_t ncPrefix = strlen(szPrefix);
    size_t ncName = strlen(szName);
    if (ncPrefix + ncName < ncPrefix || ncPrefix + ncName + 1 < ncPrefix + ncName)
        return E_OUTOFMEMORY;
    LPSTR szResult = new (nothrow) char[ncPrefix + ncName + 1];
    IfNullRet(szResult);
    memcpy(szResult, szPrefix, ncPrefix);
    memcpy(szResult + ncPrefix, szName, ncName);
    szResult[ncPrefix + ncName] = '\0';
    *ppOut = szResult;
    return S_OK;
}

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

// Sentinel value in m_redirectedCABlobsMemoTable table. Means "do no blob rewriting. Return the one from the underlying importer."
/*static*/ WinMDAdapter::CABlob * const WinMDAdapter::CABlob::NOREDIRECT = ((WinMDAdapter::CABlob *)(0x1));

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

/*static*/ WinMDAdapter::CABlob* WinMDAdapter::CABlob::Create(const BYTE *pBlob, ULONG cbBlob)
{
    // This can cause allocations (thus entering the host) during a profiler stackwalk.
    // But we're ok since we're not supporting SQL/F1 profiling with WinMDs. FUTURE:
    // Would be nice to eliminate allocations on stack walks regardless.
    PERMANENT_CONTRACT_VIOLATION(HostViolation, ReasonUnsupportedForSQLF1Profiling);

    size_t cbAlloc = sizeof(CABlob) + cbBlob;  // This overestimates the needed size a bit - no biggie
    if (cbAlloc < sizeof(CABlob))
        return NULL;

    CABlob *pNewCABlob = (CABlob*)(new (nothrow) BYTE[cbAlloc]);
    if (!pNewCABlob)
        return NULL;

    pNewCABlob->cbBlob = cbBlob;
    memcpy(pNewCABlob->data, pBlob, cbBlob);
    return pNewCABlob;
}

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

/*static*/ void WinMDAdapter::CABlob::Destroy(WinMDAdapter::CABlob *pCABlob)
{
    if (pCABlob != CABlob::NOREDIRECT)
        delete [] (BYTE*)pCABlob;
}

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

// Sentinel value in m_redirectedMethodSigMemoTable or m_redirectedFieldMemoTable. 
// Means "do no signature rewriting. Return the one from the underlying importer."
/*static*/ WinMDAdapter::SigData * const WinMDAdapter::SigData::NOREDIRECT = ((WinMDAdapter::SigData *)(0x1));

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

/*static*/ WinMDAdapter::SigData* WinMDAdapter::SigData::Create(ULONG cbSig, PCCOR_SIGNATURE pSig)
{
    // This can cause allocations (thus entering the host) during a profiler stackwalk.
    // But we're ok since we're not supporting SQL/F1 profiling with WinMDs. FUTURE:
    // Would be nice to eliminate allocations on stack walks regardless.
    PERMANENT_CONTRACT_VIOLATION(HostViolation, ReasonUnsupportedForSQLF1Profiling);

    _ASSERTE(pSig != NULL);
    size_t cbAlloc = sizeof(SigData) + cbSig;  // This overestimates the needed size a bit - no biggie
    if (cbAlloc < sizeof(SigData))
        return NULL;

    SigData *pNewSigData = (SigData*)(new (nothrow) BYTE[cbAlloc]);
    if (!pNewSigData)
        return NULL;

    pNewSigData->cbSig = cbSig;
    memcpy(pNewSigData->data, pSig, cbSig);
    return pNewSigData;
}

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

/*static*/ void WinMDAdapter::SigData::Destroy(WinMDAdapter::SigData *pSigData)
{
    if (pSigData != SigData::NOREDIRECT)
        delete [] (BYTE*)pSigData;
}

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

//-----------------------------------------------------------------------------------------------------
// S_OK if pUnknown is really a WinMD wrapper. This is just a polite way of asking "is it bad to
//   to static cast pUnknown to RegMeta/MDInternalRO." 
//-----------------------------------------------------------------------------------------------------
HRESULT CheckIfImportingWinMD(IUnknown *pUnknown)
{
    IUnknown *pIgnore = NULL;
    HRESULT hr = pUnknown->QueryInterface(IID_IWinMDImport, (void**)&pIgnore);
    if (hr == S_OK)
    {
        pIgnore->Release();
    }
    if (hr == E_NOINTERFACE)
    {
        hr = S_FALSE;
    }
    return hr;
}


//-----------------------------------------------------------------------------------------------------
// E_NOTIMPL if pUnknown is really a WinMD wrapper.
//-----------------------------------------------------------------------------------------------------
HRESULT VerifyNotWinMDHelper(IUnknown *pUnknown
#ifdef _DEBUG
                            ,LPCSTR    assertMsg
                            ,LPCSTR    file
                            ,int       line
#endif //_DEBUG
                            )
{
    HRESULT hr = CheckIfImportingWinMD(pUnknown);
    if (FAILED(hr))
        return hr;
    if (hr == S_FALSE)
    {
        return S_OK;
    }
#ifdef _DEBUG
#ifndef DACCESS_COMPILE
    if (CLRConfig::GetConfigValue(CLRConfig::INTERNAL_MD_WinMD_AssertOnIllegalUsage))
    {
        DbgAssertDialog(file, line, assertMsg);
    }
#endif 
#endif
    return E_NOTIMPL;
}