Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TypeBuilder.cs « TypeLoader « Runtime « Internal « src « System.Private.TypeLoader « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 271ec8cef413e3bac392a44acd047bdb6014cf32 (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
// 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.


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime;
using System.Text;

using System.Reflection.Runtime.General;

using Internal.Runtime.Augments;
using Internal.Runtime.CompilerServices;

using Internal.Metadata.NativeFormat;
using Internal.NativeFormat;
using Internal.TypeSystem;
using Internal.TypeSystem.NativeFormat;
using Internal.TypeSystem.NoMetadata;

namespace Internal.Runtime.TypeLoader
{
    using DynamicGenericsRegistrationData = TypeLoaderEnvironment.DynamicGenericsRegistrationData;
    using GenericTypeEntry = TypeLoaderEnvironment.GenericTypeEntry;
    using TypeEntryToRegister = TypeLoaderEnvironment.TypeEntryToRegister;
    using GenericMethodEntry = TypeLoaderEnvironment.GenericMethodEntry;
    using HandleBasedGenericTypeLookup = TypeLoaderEnvironment.HandleBasedGenericTypeLookup;
    using DefTypeBasedGenericTypeLookup = TypeLoaderEnvironment.DefTypeBasedGenericTypeLookup;
    using HandleBasedGenericMethodLookup = TypeLoaderEnvironment.HandleBasedGenericMethodLookup;
    using MethodDescBasedGenericMethodLookup = TypeLoaderEnvironment.MethodDescBasedGenericMethodLookup;
    using ThunkKind = CallConverterThunk.ThunkKind;
    using VTableSlotMapper = TypeBuilderState.VTableSlotMapper;

    internal static class LowLevelListExtensions
    {
        public static void Expand<T>(this LowLevelList<T> list, int count)
        {
            if (list.Capacity < count)
                list.Capacity = count;

            while (list.Count < count)
                list.Add(default(T));
        }

        public static bool HasSetBits(this LowLevelList<bool> list)
        {
            for (int index = 0; index < list.Count; index++)
            {
                if (list[index])
                    return true;
            }

            return false;
        }
    }

    [Flags]
    internal enum FieldLoadState
    {
        None = 0,
        Instance = 1,
        Statics = 2,
    }

    public static class TypeBuilderApi
    {
        public static void ResolveMultipleCells(GenericDictionaryCell [] cells, out IntPtr[] fixups)
        {
            TypeBuilder.ResolveMultipleCells(cells, out fixups);
        }
    }


    internal class TypeBuilder
    {
        public TypeBuilder()
        {
            TypeLoaderEnvironment.Instance.VerifyTypeLoaderLockHeld();
        }

        private const int MinimumValueTypeSize = 0x1;

        /// <summary>
        /// The StaticClassConstructionContext for a type is encoded in the negative space
        /// of the NonGCStatic fields of a type.
        /// </summary>
        public static unsafe readonly int ClassConstructorOffset = -sizeof(System.Runtime.CompilerServices.StaticClassConstructionContext);

        private LowLevelList<TypeDesc> _typesThatNeedTypeHandles = new LowLevelList<TypeDesc>();

        private LowLevelList<InstantiatedMethod> _methodsThatNeedDictionaries = new LowLevelList<InstantiatedMethod>();

        private LowLevelList<TypeDesc> _typesThatNeedPreparation = null;

        private Object _epoch = new Object();

#if DEBUG
        private bool _finalTypeBuilding = false;
#endif

        // Helper exception to abort type building if we do not find the generic type template
        internal class MissingTemplateException : Exception
        {
        }


        private bool CheckAllHandlesValidForMethod(MethodDesc method)
        {
            if (!method.OwningType.RetrieveRuntimeTypeHandleIfPossible())
                return false;

            for (int i = 0; i < method.Instantiation.Length; i++)
                if (!method.Instantiation[i].RetrieveRuntimeTypeHandleIfPossible())
                    return false;

            return true;
        }

        internal bool RetrieveExactFunctionPointerIfPossible(MethodDesc method, out IntPtr result)
        {
            result = IntPtr.Zero;

            if (!method.IsNonSharableMethod || !CheckAllHandlesValidForMethod(method))
                return false;

            RuntimeTypeHandle[] genMethodArgs = method.Instantiation.Length > 0 ? new RuntimeTypeHandle[method.Instantiation.Length] : Empty<RuntimeTypeHandle>.Array;
            for (int i = 0; i < method.Instantiation.Length; i++)
                genMethodArgs[i] = method.Instantiation[i].RuntimeTypeHandle;

            return TypeLoaderEnvironment.Instance.TryLookupExactMethodPointerForComponents(method.OwningType.RuntimeTypeHandle, method.NameAndSignature, genMethodArgs, out result);
        }

        internal bool RetrieveMethodDictionaryIfPossible(InstantiatedMethod method)
        {
            if (method.RuntimeMethodDictionary != IntPtr.Zero)
                return true;

            bool allHandlesValid = CheckAllHandlesValidForMethod(method);

            TypeLoaderLogger.WriteLine("Looking for method dictionary for method " + method.ToString() + " ... " + (allHandlesValid ? "(All type arg handles valid)" : ""));

            IntPtr methodDictionary;

            if ((allHandlesValid && TypeLoaderEnvironment.Instance.TryLookupGenericMethodDictionaryForComponents(new HandleBasedGenericMethodLookup(method), out methodDictionary)) ||
                 (!allHandlesValid && TypeLoaderEnvironment.Instance.TryLookupGenericMethodDictionaryForComponents(new MethodDescBasedGenericMethodLookup(method), out methodDictionary)))
            {
                TypeLoaderLogger.WriteLine("Found DICT = " + methodDictionary.LowLevelToString() + " for method " + method.ToString());
                method.AssociateWithRuntimeMethodDictionary(methodDictionary);
                return true;
            }

            return false;
        }

        /// <summary>
        /// Register the type for preparation. The preparation will be done once the current type is prepared.
        /// This is the prefered way to get a dependent type prepared because of it avoids issues with cycles and recursion.
        /// </summary>
        public void RegisterForPreparation(TypeDesc type)
        {
            TypeLoaderLogger.WriteLine("Register for preparation " + type.ToString() + " ...");

            // If this type has type handle, do nothing and return
            if (type.RetrieveRuntimeTypeHandleIfPossible())
                return;

            var state = type.GetOrCreateTypeBuilderState();

            // If this type was already inspected, do nothing and return.
            if (state.NeedsTypeHandle)
                return;

            state.NeedsTypeHandle = true;

            if (type.IsCanonicalSubtype(CanonicalFormKind.Any))
                return;

            if (_typesThatNeedPreparation == null)
                _typesThatNeedPreparation = new LowLevelList<TypeDesc>();

            _typesThatNeedPreparation.Add(type);
        }

        /// <summary>
        /// Collects all dependencies that need to be created in order to create
        /// the method that was passed in.
        /// </summary>
        public void PrepareMethod(MethodDesc method)
        {
            TypeLoaderLogger.WriteLine("Preparing method " + method.ToString() + " ...");

            RegisterForPreparation(method.OwningType);

            if (method.Instantiation.Length == 0)
                return;

            InstantiatedMethod genericMethod = (InstantiatedMethod)method;

            if (RetrieveMethodDictionaryIfPossible(genericMethod))
                return;

            // If this method was already inspected, do nothing and return
            if (genericMethod.NeedsDictionary)
                return;

            genericMethod.NeedsDictionary = true;

            if (genericMethod.IsCanonicalMethod(CanonicalFormKind.Any))
                return;

            _methodsThatNeedDictionaries.Add(genericMethod);

            foreach (var type in genericMethod.Instantiation)
                RegisterForPreparation(type);

            ParseNativeLayoutInfo(genericMethod);
        }

        private void InsertIntoNeedsTypeHandleList(TypeBuilderState state, TypeDesc type)
        {
            if ((type is DefType) || (type is ArrayType) || (type is PointerType) || (type is ByRefType))
            {
                _typesThatNeedTypeHandles.Add(type);
            }
        }

        /// <summary>
        /// Collects all dependencies that need to be created in order to create
        /// the type that was passed in.
        /// </summary>
        internal void PrepareType(TypeDesc type)
        {
            TypeLoaderLogger.WriteLine("Preparing type " + type.ToString() + " ...");

            TypeBuilderState state = type.GetTypeBuilderStateIfExist();
            bool hasTypeHandle = type.RetrieveRuntimeTypeHandleIfPossible();

            // If this type has type handle, do nothing and return unless we should prepare even in the presence of a type handle
            if (hasTypeHandle)
                return;

            if (state == null)
                state = type.GetOrCreateTypeBuilderState();

            // If this type was already prepared, do nothing unless we are re-preparing it for the purpose of loading the field layout
            if (state.HasBeenPrepared)
            {
                return;
            }

            state.HasBeenPrepared = true;
            state.NeedsTypeHandle = true;

            if (!hasTypeHandle)
            {
                InsertIntoNeedsTypeHandleList(state, type);
            }

            bool noExtraPreparation = false; // Set this to true for types which don't need other types to be prepared. I.e GenericTypeDefinitions

            if (type is DefType)
            {
                DefType typeAsDefType = (DefType)type;

                if (typeAsDefType.HasInstantiation)
                {
                    if (typeAsDefType.IsTypeDefinition)
                    {
                        noExtraPreparation = true;
                    }
                    else
                    {
                        // This call to ComputeTemplate will find the native layout info for the type, and the template
                        // For metadata loaded types, a template will not exist, but we may find the NativeLayout describing the generic dictionary
                        typeAsDefType.ComputeTemplate(state, false);

                        Debug.Assert(state.TemplateType == null || (state.TemplateType is DefType && !state.TemplateType.RuntimeTypeHandle.IsNull()));

                        // Collect dependencies

                        // We need the instantiation arguments to register a generic type
                        foreach (var instArg in typeAsDefType.Instantiation)
                            RegisterForPreparation(instArg);

                        // We need the type definition to register a generic type
                        if (type.GetTypeDefinition() is MetadataType)
                            RegisterForPreparation(type.GetTypeDefinition());

                        ParseNativeLayoutInfo(state, type);
                    }
                }

                if (!noExtraPreparation)
                    state.PrepareStaticGCLayout();
            }
            else if (type is ParameterizedType)
            {
                PrepareType(((ParameterizedType)type).ParameterType);

                if (type is ArrayType)
                {
                    ArrayType typeAsArrayType = (ArrayType)type;

                    if (typeAsArrayType.IsSzArray && !typeAsArrayType.ElementType.IsPointer)
                    {
                        typeAsArrayType.ComputeTemplate(state);
                        Debug.Assert(state.TemplateType != null && state.TemplateType is ArrayType && !state.TemplateType.RuntimeTypeHandle.IsNull());

                        ParseNativeLayoutInfo(state, type);
                    }
                    else
                    {
                        Debug.Assert(typeAsArrayType.IsMdArray || typeAsArrayType.ElementType.IsPointer);
                    }

                    // Assert that non-valuetypes are considered to have pointer size
                    Debug.Assert(typeAsArrayType.ParameterType.IsValueType || state.ComponentSize == IntPtr.Size);
                }
            }
            else
            {
                Debug.Assert(false);
            }

            // Need to prepare the base type first since it is used to compute interfaces
            if (!noExtraPreparation)
            {
                PrepareBaseTypeAndDictionaries(type);
                PrepareRuntimeInterfaces(type);

                TypeLoaderLogger.WriteLine("Layout for type " + type.ToString() + " complete." +
                    " IsHFA = " + (state.IsHFA ? "true" : "false") +
                    " Type size = " + (state.TypeSize.HasValue ? state.TypeSize.Value.LowLevelToString() : "UNDEF") +
                    " Fields size = " + (state.UnalignedTypeSize.HasValue ? state.UnalignedTypeSize.Value.LowLevelToString() : "UNDEF") +
                    " Type alignment = " + (state.FieldAlignment.HasValue ? state.FieldAlignment.Value.LowLevelToString() : "UNDEF"));

                if (state.TemplateType != null && state.TemplateType.IsCanonicalSubtype(CanonicalFormKind.Universal))
                {
                    state.VTableSlotsMapping = new VTableSlotMapper(state.TemplateType.RuntimeTypeHandle.GetNumVtableSlots());
                    ComputeVTableLayout(type, state.TemplateType, state);
                }
            }
        }

        /// <summary>
        /// Recursively triggers preparation for a type's runtime interfaces
        /// </summary>
        private void PrepareRuntimeInterfaces(TypeDesc type)
        {
            // Prepare all the interfaces that might be used. (This can be a superset of the
            // interfaces explicitly in the NativeLayout.)
            foreach (DefType interfaceType in type.RuntimeInterfaces)
            {
                PrepareType(interfaceType);
            }
        }

        /// <summary>
        /// Triggers preparation for a type's base types
        /// </summary>
        private void PrepareBaseTypeAndDictionaries(TypeDesc type)
        {
            DefType baseType = type.BaseType;
            if (baseType == null)
                return;

            PrepareType(baseType);
        }

        private void ProcessTypesNeedingPreparation()
        {
            // Process the pending types
            while (_typesThatNeedPreparation != null)
            {
                var pendingTypes = _typesThatNeedPreparation;
                _typesThatNeedPreparation = null;

                for (int i = 0; i < pendingTypes.Count; i++)
                    PrepareType(pendingTypes[i]);
            }
        }

        private GenericDictionaryCell[] GetGenericMethodDictionaryCellsForMetadataBasedLoad(InstantiatedMethod method, InstantiatedMethod nonTemplateMethod)
        {
#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
            uint r2rNativeLayoutInfoToken;
            GenericDictionaryCell[] cells = null;
            NativeFormatModuleInfo r2rNativeLayoutModuleInfo;

            if ((new TemplateLocator()).TryGetMetadataNativeLayout(nonTemplateMethod, out r2rNativeLayoutModuleInfo, out r2rNativeLayoutInfoToken))
            {
                // ReadyToRun dictionary parsing
                NativeReader readyToRunReader = TypeLoaderEnvironment.Instance.GetNativeLayoutInfoReader(r2rNativeLayoutModuleInfo.Handle);
                var readyToRunInfoParser = new NativeParser(readyToRunReader, r2rNativeLayoutInfoToken);

                // A null readyToRunInfoParser is a valid situation to end up in
                // This can happen if either we have exact code for a method, or if
                // we are going to use the universal generic implementation.
                // In both of those cases, we do not have any generic dictionary cells
                // to put into the dictionary
                if (!readyToRunInfoParser.IsNull)
                {
                    NativeFormatMetadataUnit nativeMetadataUnit = method.Context.ResolveMetadataUnit(r2rNativeLayoutModuleInfo);
                    FixupCellMetadataResolver resolver = new FixupCellMetadataResolver(nativeMetadataUnit, nonTemplateMethod);
                    cells = GenericDictionaryCell.BuildDictionaryFromMetadataTokensAndContext(this, readyToRunInfoParser, nativeMetadataUnit, resolver);
                }
            }

            return cells;
#else
            return null;
#endif
        }

        internal void ParseNativeLayoutInfo(InstantiatedMethod method)
        {
            TypeLoaderLogger.WriteLine("Parsing NativeLayoutInfo for method " + method.ToString() + " ...");

            Debug.Assert(method.Dictionary == null);

            InstantiatedMethod nonTemplateMethod = method;

            // Templates are always non-unboxing stubs
            if (method.UnboxingStub)
            {
                // Strip unboxing stub, note the first parameter which is false
                nonTemplateMethod = (InstantiatedMethod)method.Context.ResolveGenericMethodInstantiation(false, (DefType)method.OwningType, method.NameAndSignature, method.Instantiation, IntPtr.Zero, false);
            }

            uint nativeLayoutInfoToken;
            NativeFormatModuleInfo nativeLayoutModule;
            MethodDesc templateMethod = (new TemplateLocator()).TryGetGenericMethodTemplate(nonTemplateMethod, out nativeLayoutModule, out nativeLayoutInfoToken);

            // If the templateMethod found in the static image is missing or universal, see if the R2R layout
            // can provide something more specific.
            if ((templateMethod == null) || templateMethod.IsCanonicalMethod(CanonicalFormKind.Universal))
            {
                GenericDictionaryCell[] cells = GetGenericMethodDictionaryCellsForMetadataBasedLoad(method, nonTemplateMethod);

                if (cells != null)
                {
                    method.SetGenericDictionary(new GenericMethodDictionary(cells));
                    return;
                }

                if (templateMethod == null)
                {
                    // In this case we were looking for the r2r template to create the dictionary, but
                    // there isn't one. This implies that we don't need a Canon specific dictionary
                    // so just generate something empty
                    method.SetGenericDictionary(new GenericMethodDictionary(Array.Empty<GenericDictionaryCell>()));
                    return;
                }
            }

            // Ensure that if this method is non-shareable from a normal canonical perspective, then
            // its template MUST be a universal canonical template method
            Debug.Assert(!method.IsNonSharableMethod || (method.IsNonSharableMethod && templateMethod.IsCanonicalMethod(CanonicalFormKind.Universal)));

            NativeReader nativeLayoutInfoReader = TypeLoaderEnvironment.Instance.GetNativeLayoutInfoReader(nativeLayoutModule.Handle);

            var methodInfoParser = new NativeParser(nativeLayoutInfoReader, nativeLayoutInfoToken);
            var context = new NativeLayoutInfoLoadContext
            {
                _typeSystemContext = method.Context,
                _typeArgumentHandles = method.OwningType.Instantiation,
                _methodArgumentHandles = method.Instantiation,
                _module = nativeLayoutModule
            };

            BagElementKind kind;
            while ((kind = methodInfoParser.GetBagElementKind()) != BagElementKind.End)
            {
                switch (kind)
                {
                    case BagElementKind.DictionaryLayout:
                        TypeLoaderLogger.WriteLine("Found BagElementKind.DictionaryLayout");
                        method.SetGenericDictionary(new GenericMethodDictionary(GenericDictionaryCell.BuildDictionary(this, context, methodInfoParser.GetParserFromRelativeOffset())));
                        break;

                    default:
                        Debug.Fail("Unexpected BagElementKind for generic method with name " + method.NameAndSignature.Name + "! Only BagElementKind.DictionaryLayout should appear.");
                        throw new BadImageFormatException();
                }
            }

            if (method.Dictionary == null)
                method.SetGenericDictionary(new GenericMethodDictionary(Array.Empty<GenericDictionaryCell>()));
        }

        internal void ParseNativeLayoutInfo(TypeBuilderState state, TypeDesc type)
        {
            TypeLoaderLogger.WriteLine("Parsing NativeLayoutInfo for type " + type.ToString() + " ...");

            bool isTemplateUniversalCanon = false;
            if (state.TemplateType != null)
            {
                isTemplateUniversalCanon = state.TemplateType.IsCanonicalSubtype(CanonicalFormKind.Universal);
            }

            // If we found the universal template, see if there is a ReadyToRun dictionary description available.
            // If so, use that, otherwise, run down the template type loader path with the universal template
            if ((state.TemplateType == null) || isTemplateUniversalCanon)
            {
#if PROJECTN
                // CanonAlike types do not get dictionaries
                if ((state.TemplateType == null) && (type.IsConstructedOverType(type.Context.CanonAlikeTypeArray)))
                    return;
#endif

                // ReadyToRun case - Native Layout is just the dictionary
                NativeParser readyToRunInfoParser = state.GetParserForReadyToRunNativeLayoutInfo();
                GenericDictionaryCell[] cells = null;

                // A null readyToRunInfoParser is a valid situation to end up in
                // This can happen if either we have exact code for the method on a type, or if
                // we are going to use the universal generic implementation.
                // In both of those cases, we do not have any generic dictionary cells
                // to put into the dictionary
                if (!readyToRunInfoParser.IsNull)
                {
#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
                    NativeFormatMetadataUnit nativeMetadataUnit = type.Context.ResolveMetadataUnit(state.R2RNativeLayoutInfo.Module);
                    FixupCellMetadataResolver resolver = new FixupCellMetadataResolver(nativeMetadataUnit, type);
                    cells = GenericDictionaryCell.BuildDictionaryFromMetadataTokensAndContext(this, readyToRunInfoParser, nativeMetadataUnit, resolver);
#endif
                }
                state.Dictionary = cells != null ? new GenericTypeDictionary(cells) : null;

                if (state.TemplateType == null)
                    return;
            }

            NativeParser typeInfoParser = state.GetParserForNativeLayoutInfo();
            NativeLayoutInfoLoadContext context = state.NativeLayoutInfo.LoadContext;

            NativeParser baseTypeParser = new NativeParser();

            int nonGcDataSize = 0;
            int gcDataSize = 0;
            int threadDataSize = 0;
            bool staticSizesMeaningful = (type is DefType) // Is type permitted to have static fields
                                    && !isTemplateUniversalCanon; // Non-universal templates always specify their statics sizes 
                                                                  // if the size can be greater than 0

            int baseTypeSize = 0;
            bool checkBaseTypeSize = false;

            BagElementKind kind;
            while ((kind = typeInfoParser.GetBagElementKind()) != BagElementKind.End)
            {
                switch (kind)
                {
                    case BagElementKind.BaseType:
                        TypeLoaderLogger.WriteLine("Found BagElementKind.BaseType");
                        Debug.Assert(baseTypeParser.IsNull);
                        baseTypeParser = typeInfoParser.GetParserFromRelativeOffset();
                        break;

                    case BagElementKind.BaseTypeSize:
                        TypeLoaderLogger.WriteLine("Found BagElementKind.BaseTypeSize");
                        Debug.Assert(state.TemplateType.IsCanonicalSubtype(CanonicalFormKind.Universal));
                        baseTypeSize = checked((int)typeInfoParser.GetUnsigned());
                        break;

                    case BagElementKind.ImplementedInterfaces:
                        TypeLoaderLogger.WriteLine("Found BagElementKind.ImplementedInterfaces");
                        // Interface handling is done entirely in NativeLayoutInterfacesAlgorithm
                        typeInfoParser.GetUnsigned();
                        break;

                    case BagElementKind.TypeFlags:
                        {
                            TypeLoaderLogger.WriteLine("Found BagElementKind.TypeFlags");
                            Internal.NativeFormat.TypeFlags flags = (Internal.NativeFormat.TypeFlags)typeInfoParser.GetUnsigned();
                            Debug.Assert(state.HasStaticConstructor == ((flags & Internal.NativeFormat.TypeFlags.HasClassConstructor) != 0));
                        }
                        break;

                    case BagElementKind.ClassConstructorPointer:
                        TypeLoaderLogger.WriteLine("Found BagElementKind.ClassConstructorPointer");
                        state.ClassConstructorPointer = context.GetGCStaticInfo(typeInfoParser.GetUnsigned());
                        break;

                    case BagElementKind.NonGcStaticDataSize:
                        TypeLoaderLogger.WriteLine("Found BagElementKind.NonGcStaticDataSize");
                        // Use checked typecast to int to ensure there aren't any overflows/truncations (size value used in allocation of memory later)
                        nonGcDataSize = checked((int)typeInfoParser.GetUnsigned());
                        Debug.Assert(staticSizesMeaningful);
                        break;

                    case BagElementKind.GcStaticDataSize:
                        TypeLoaderLogger.WriteLine("Found BagElementKind.GcStaticDataSize");
                        // Use checked typecast to int to ensure there aren't any overflows/truncations (size value used in allocation of memory later)
                        gcDataSize = checked((int)typeInfoParser.GetUnsigned());
                        Debug.Assert(staticSizesMeaningful);
                        break;

                    case BagElementKind.ThreadStaticDataSize:
                        TypeLoaderLogger.WriteLine("Found BagElementKind.ThreadStaticDataSize");
                        // Use checked typecast to int to ensure there aren't any overflows/truncations (size value used in allocation of memory later)
                        threadDataSize = checked((int)typeInfoParser.GetUnsigned());
                        Debug.Assert(staticSizesMeaningful);
                        break;

                    case BagElementKind.GcStaticDesc:
                        TypeLoaderLogger.WriteLine("Found BagElementKind.GcStaticDesc");
                        state.GcStaticDesc = context.GetGCStaticInfo(typeInfoParser.GetUnsigned());
                        break;

                    case BagElementKind.ThreadStaticDesc:
                        TypeLoaderLogger.WriteLine("Found BagElementKind.ThreadStaticDesc");
                        state.ThreadStaticDesc = context.GetGCStaticInfo(typeInfoParser.GetUnsigned());
                        break;

                    case BagElementKind.GenericVarianceInfo:
                        TypeLoaderLogger.WriteLine("Found BagElementKind.GenericVarianceInfo");
                        NativeParser varianceInfoParser = typeInfoParser.GetParserFromRelativeOffset();
                        state.GenericVarianceFlags = new int[varianceInfoParser.GetSequenceCount()];
                        for (int i = 0; i < state.GenericVarianceFlags.Length; i++)
                            state.GenericVarianceFlags[i] = checked((int)varianceInfoParser.GetUnsigned());
                        break;

                    case BagElementKind.FieldLayout:
                        TypeLoaderLogger.WriteLine("Found BagElementKind.FieldLayout");
                        typeInfoParser.SkipInteger(); // Handled in type layout algorithm
                        break;

                    case BagElementKind.VTableMethodSignatures:
                        TypeLoaderLogger.WriteLine("Found BagElementKind.VTableMethodSignatures");
                        ParseVTableMethodSignatures(state, context, typeInfoParser.GetParserFromRelativeOffset());
                        break;

                    case BagElementKind.SealedVTableEntries:
                        TypeLoaderLogger.WriteLine("Found BagElementKind.SealedVTableEntries");
                        state.NumSealedVTableEntries = typeInfoParser.GetUnsigned();
                        break;

                    case BagElementKind.DictionaryLayout:
                        TypeLoaderLogger.WriteLine("Found BagElementKind.DictionaryLayout");
                        Debug.Assert(!isTemplateUniversalCanon, "Universal template nativelayout do not have DictionaryLayout");

#if PROJECTN
                        if (type.IsConstructedOverType(type.Context.CanonAlikeTypeArray))
                        {
                            TypeLoaderLogger.WriteLine("Type is CanonAlike, skip generation of dictionary");
                            typeInfoParser.SkipInteger();
                            break;
                        }
#endif

                        Debug.Assert(state.Dictionary == null);
                        if (!state.TemplateType.RetrieveRuntimeTypeHandleIfPossible())
                        {
                            TypeLoaderLogger.WriteLine("ERROR: failed to get type handle for template type " + state.TemplateType.ToString());
                            throw new TypeBuilder.MissingTemplateException();
                        }
                        state.Dictionary = new GenericTypeDictionary(GenericDictionaryCell.BuildDictionary(this, context, typeInfoParser.GetParserFromRelativeOffset()));
                        break;

                    default:
                        TypeLoaderLogger.WriteLine("Found unknown BagElementKind: " + ((int)kind).LowLevelToString());
                        typeInfoParser.SkipInteger();
                        break;
                }
            }

            if (staticSizesMeaningful)
            {
                Debug.Assert((state.NonGcDataSize + (state.HasStaticConstructor ? TypeBuilder.ClassConstructorOffset : 0)) == nonGcDataSize);
                Debug.Assert(state.GcDataSize == gcDataSize);
                Debug.Assert(state.ThreadDataSize == threadDataSize);
            }

#if GENERICS_FORCE_USG
            if (isTemplateUniversalCanon && type.CanShareNormalGenericCode())
            {
                // Even in the GENERICS_FORCE_USG stress mode today, codegen will generate calls to normal-canonical target methods whenever possible.
                // Given that we use universal template types to build the dynamic EETypes, these dynamic types will end up with NULL dictionary 
                // entries, causing the normal-canonical code sharing to fail.
                // To fix this problem, we will load the generic dictionary from the non-universal template type, and build a generic dictionary out of
                // it for the dynamic type, and store that dictionary pointer in the dynamic EEType's structure.
                TypeBuilderState tempState = new TypeBuilderState();
                tempState.NativeLayoutInfo = new NativeLayoutInfo();
                state.NonUniversalTemplateType = tempState.TemplateType = type.Context.TemplateLookup.TryGetNonUniversalTypeTemplate(type, ref tempState.NativeLayoutInfo);
                if (tempState.TemplateType != null)
                {
                    Debug.Assert(!tempState.TemplateType.IsCanonicalSubtype(CanonicalFormKind.UniversalCanonLookup));
                    NativeParser nonUniversalTypeInfoParser = GetNativeLayoutInfoParser(type, ref tempState.NativeLayoutInfo);
                    NativeParser dictionaryLayoutParser = nonUniversalTypeInfoParser.GetParserForBagElementKind(BagElementKind.DictionaryLayout);
                    if (!dictionaryLayoutParser.IsNull)
                        state.Dictionary = new GenericTypeDictionary(GenericDictionaryCell.BuildDictionary(this, context, dictionaryLayoutParser));

                    // Get the non-universal GCDesc pointers, so we can compare them the ones we will dynamically construct for the type
                    // and verify they are equal (This is an easy and predictable way of validation for the GCDescs creation logic in the stress mode)
                    GetNonUniversalGCDescPointers(type, state, tempState);
                }
            }
#endif
            type.ParseBaseType(context, baseTypeParser);

            // Assert that parsed base type size matches the BaseTypeSize that we calculated.
            Debug.Assert(!checkBaseTypeSize || state.BaseTypeSize == baseTypeSize);
        }

        private void ParseVTableMethodSignatures(TypeBuilderState state, NativeLayoutInfoLoadContext nativeLayoutInfoLoadContext, NativeParser methodSignaturesParser)
        {
            TypeDesc type = state.TypeBeingBuilt;
            if (methodSignaturesParser.IsNull)
                return;

            // Processing vtable method signatures is only meaningful in the context of universal generics only
            Debug.Assert(state.TemplateType != null && state.TemplateType.IsCanonicalSubtype(CanonicalFormKind.Universal));

            uint numSignatures = methodSignaturesParser.GetUnsigned();

            state.VTableMethodSignatures = new TypeBuilderState.VTableLayoutInfo[numSignatures];

            for (int i = 0; i < numSignatures; i++)
            {
                state.VTableMethodSignatures[i] = new TypeBuilderState.VTableLayoutInfo();

                uint slot = methodSignaturesParser.GetUnsigned();
                state.VTableMethodSignatures[i].VTableSlot = (slot >> 1);
                if ((slot & 1) == 1)
                {
                    state.VTableMethodSignatures[i].IsSealedVTableSlot = true;
                    state.NumSealedVTableMethodSignatures++;
                }

                NativeParser sigParser = methodSignaturesParser.GetParserFromRelativeOffset();
                state.VTableMethodSignatures[i].MethodSignature = RuntimeSignature.CreateFromNativeLayoutSignature(nativeLayoutInfoLoadContext._module.Handle, sigParser.Offset);
            }
        }

        private unsafe void ComputeVTableLayout(TypeDesc currentType, TypeDesc currentTemplateType, TypeBuilderState targetTypeState)
        {
            TypeDesc baseType = GetBaseTypeThatIsCorrectForMDArrays(currentType);
            TypeDesc baseTemplateType = GetBaseTypeUsingRuntimeTypeHandle(currentTemplateType);

            Debug.Assert((baseType == null && baseTemplateType == null) || (baseType != null && baseTemplateType != null));

            // Compute the vtable layout for the current type starting with base types first
            if (baseType != null)
                ComputeVTableLayout(baseType, baseTemplateType, targetTypeState);

            currentTemplateType.RetrieveRuntimeTypeHandleIfPossible();
            Debug.Assert(!currentTemplateType.RuntimeTypeHandle.IsNull());
            Debug.Assert(baseTemplateType == null || !baseTemplateType.RuntimeTypeHandle.IsNull());

            // The m_usNumVtableSlots field on EETypes includes the count of vtable slots of the base type,
            // so make sure we don't count that twice!
            int currentVtableIndex = baseTemplateType == null ? 0 : baseTemplateType.RuntimeTypeHandle.GetNumVtableSlots();

            IntPtr dictionarySlotInVtable = IntPtr.Zero;

            if (currentType.IsGeneric())
            {
                if (!currentType.CanShareNormalGenericCode() && currentTemplateType.IsCanonicalSubtype(CanonicalFormKind.Universal))
                {
                    // We are building a type that cannot share code with normal canonical types, so the type has to have
                    // the same vtable layout as non-shared generics, meaning no dictionary pointer in the vtable.
                    // We use universal canonical template types to build such types. Universal canonical types have 'NULL'
                    // dictionary pointers in their vtables, so we'll start copying the vtable entries right after that
                    // dictionary slot (dictionaries are accessed/used at runtime in a different way, not through the vtable 
                    // dictionary pointer for such types).
                    currentVtableIndex++;
                }
                else if (currentType.CanShareNormalGenericCode())
                {
                    // In the case of a normal canonical type in their base class hierarchy,
                    // we need to keep track of its dictionary slot in the vtable mapping, and try to 
                    // copy its value values directly from its template type vtable.
                    // Two possible cases:
                    //      1)  The template type is a normal canonical type. In this case, the dictionary value
                    //          in the vtable slot of the template is NULL, but that's ok because this case is 
                    //          correctly handled anyways by the FinishBaseTypeAndDictionaries() API.
                    //      2)  The template type is NOT a canonical type. In this case, the dictionary value
                    //          in the vtable slot of the template is not null, and we keep track of it in the
                    //          VTableSlotsMapping so we can copy it to the dynamic type after creation.
                    //          This corner case is not handled by FinishBaseTypeAndDictionaries(), so we track it
                    //          here.
                    // Examples:
                    //      1) Derived<T,U> : Base<U>, instantiated over [int,string]
                    //      2) Derived<__Universal> : BaseClass, and BaseClass : BaseBaseClass<object>
                    //      3) Derived<__Universal> : BaseClass<object>
                    Debug.Assert(currentTemplateType != null && !currentTemplateType.RuntimeTypeHandle.IsNull());

                    IntPtr* pTemplateVtable = (IntPtr*)((byte*)(currentTemplateType.RuntimeTypeHandle.ToEETypePtr()) + sizeof(EEType));
                    dictionarySlotInVtable = pTemplateVtable[currentVtableIndex];
                }
            }
            else if (currentType is ArrayType)
            {
                if (currentTemplateType.IsCanonicalSubtype(CanonicalFormKind.Universal))
                {
                    TypeDesc canonicalElementType = currentType.Context.ConvertToCanon(((ArrayType)currentType).ElementType, CanonicalFormKind.Specific);
                    bool quickIsNotCanonical = canonicalElementType == ((ArrayType)currentType).ElementType;

                    Debug.Assert(quickIsNotCanonical == !canonicalElementType.IsCanonicalSubtype(CanonicalFormKind.Any));

                    if (quickIsNotCanonical)
                    {
                        // We are building a type that cannot share code with normal canonical types, so the type has to have
                        // the same vtable layout as non-shared generics, meaning no dictionary pointer in the vtable.
                        // We use universal canonical template types to build such types. Universal canonical types have 'NULL'
                        // dictionary pointers in their vtables, so we'll start copying the vtable entries right after that
                        // dictionary slot (dictionaries are accessed/used at runtime in a different way, not through the vtable 
                        // dictionary pointer for such types).
                        currentVtableIndex++;
                    }
                }
            }

            // Map vtable entries from target type's template type
            int numVtableSlotsOnCurrentTemplateType = currentTemplateType.RuntimeTypeHandle.GetNumVtableSlots();
            for (; currentVtableIndex < numVtableSlotsOnCurrentTemplateType; currentVtableIndex++)
            {
                targetTypeState.VTableSlotsMapping.AddMapping(
                    currentVtableIndex,
                    targetTypeState.VTableSlotsMapping.NumSlotMappings,
                    dictionarySlotInVtable);

                // Reset dictionarySlotInVtable (only one dictionary slot in vtable per type)
                dictionarySlotInVtable = IntPtr.Zero;
            }

            // Sanity check: vtable of the dynamic type should be equal or smaller than the vtable of the template type
            Debug.Assert(targetTypeState.VTableSlotsMapping.NumSlotMappings <= numVtableSlotsOnCurrentTemplateType);
        }

        /// <summary>
        /// Wraps information about how a type is laid out into one package.  Types may have been laid out by
        /// TypeBuilder (which means they have a gc bitfield), or they could be types that were laid out by NUTC
        /// (which means we only have a GCDesc for them).  This struct wraps both of those possibilities into
        /// one package to be able to write that layout to another bitfield we are constructing.  (This is for
        /// struct fields.)
        /// </summary>
        internal unsafe struct GCLayout
        {
            private LowLevelList<bool> _bitfield;
            private unsafe void* _gcdesc;
            private int _size;
            private bool _isReferenceTypeGCLayout;

            public static GCLayout None { get { return new GCLayout(); } }
            public static GCLayout SingleReference { get; } = new GCLayout(new LowLevelList<bool>(new bool[1] { true }), false);

            public bool IsNone { get { return _bitfield == null && _gcdesc == null; } }

            public GCLayout(LowLevelList<bool> bitfield, bool isReferenceTypeGCLayout)
            {
                Debug.Assert(bitfield != null);

                _bitfield = bitfield;
                _gcdesc = null;
                _size = 0;
                _isReferenceTypeGCLayout = isReferenceTypeGCLayout;
            }

            public GCLayout(RuntimeTypeHandle rtth)
            {
                EEType* eeType = rtth.ToEETypePtr();
                Debug.Assert(eeType != null);

                _bitfield = null;
                _isReferenceTypeGCLayout = false; // This field is only used for the LowLevelList<bool> path
                _gcdesc = eeType->HasGCPointers ? (void**)eeType - 1 : null;
                _size = (int)eeType->BaseSize;
            }

            /// <summary>
            /// Writes this layout to the given bitfield.
            /// </summary>
            /// <param name="bitfield">The bitfield to write a layout to (may be null, at which
            /// point it will be created and assigned).</param>
            /// <param name="offset">The offset at which we need to write the bitfield.</param>
            public void WriteToBitfield(LowLevelList<bool> bitfield, int offset)
            {
                if (bitfield == null)
                    throw new ArgumentNullException(nameof(bitfield));

                if (IsNone)
                    return;

                // Ensure exactly one of these two are set.
                Debug.Assert(_gcdesc != null ^ _bitfield != null);

                if (_bitfield != null)
                    MergeBitfields(bitfield, offset);
                else
                    WriteGCDescToBitfield(bitfield, offset);
            }

            private unsafe void WriteGCDescToBitfield(LowLevelList<bool> bitfield, int offset)
            {
                int startIndex = offset / IntPtr.Size;

                void** ptr = (void**)_gcdesc;
                Debug.Assert(_gcdesc != null);

                // Number of series
                int count = (int)*ptr-- - 1;
                Debug.Assert(count >= 0);

                // Ensure capacity for the values we are about to write
                int capacity = startIndex + _size / IntPtr.Size - 2;
                bitfield.Expand(capacity);

                while (count-- >= 0)
                {
                    int offs = (int)*ptr-- / IntPtr.Size - 1;
                    int len = ((int)*ptr-- + _size) / IntPtr.Size;

                    Debug.Assert(len > 0);
                    Debug.Assert(offs >= 0);

                    for (int i = 0; i < len; i++)
                        bitfield[startIndex + offs + i] = true;
                }
            }

            private void MergeBitfields(LowLevelList<bool> outputBitfield, int offset)
            {
                int startIndex = offset / IntPtr.Size;

                // These routines represent the GC layout after the EEType pointer
                // in an object, but the LowLevelList<bool> bitfield logically contains 
                // the EETypepointer if it is describing a reference type. So, skip the
                // first value.
                int itemsToSkip = _isReferenceTypeGCLayout ? 1 : 0;

                // Assert that we only skip a non-reported pointer.
                Debug.Assert(itemsToSkip == 0 || _bitfield[0] == false);

                // Ensure capacity for the values we are about to write
                int capacity = startIndex + _bitfield.Count - itemsToSkip;
                outputBitfield.Expand(capacity);


                for (int i = itemsToSkip; i < _bitfield.Count; i++)
                {
                    // We should never overwrite a TRUE value in the table.
                    Debug.Assert(!outputBitfield[startIndex + i - itemsToSkip] || _bitfield[i]);

                    outputBitfield[startIndex + i - itemsToSkip] = _bitfield[i];
                }
            }
        }

#if GENERICS_FORCE_USG
        private unsafe void GetNonUniversalGCDescPointers(TypeDesc type, TypeBuilderState state, TypeBuilderState tempNonUniversalState)
        {
            NativeParser nonUniversalTypeInfoParser = GetNativeLayoutInfoParser(type, ref tempNonUniversalState.NativeLayoutInfo);
            NativeLayoutInfoLoadContext context = tempNonUniversalState.NativeLayoutInfo.LoadContext;
            
            uint beginOffset = nonUniversalTypeInfoParser.Offset;
            uint? staticGCDescId = nonUniversalTypeInfoParser.GetUnsignedForBagElementKind(BagElementKind.GcStaticDesc);

            nonUniversalTypeInfoParser.Offset = beginOffset;
            uint? threadStaticGCDescId = nonUniversalTypeInfoParser.GetUnsignedForBagElementKind(BagElementKind.ThreadStaticDesc);

            if(staticGCDescId.HasValue)
                state.NonUniversalStaticGCDesc = context.GetStaticInfo(staticGCDescId.Value);

            if (threadStaticGCDescId.HasValue)
                state.NonUniversalThreadStaticGCDesc = context.GetStaticInfo(threadStaticGCDescId.Value);

            state.NonUniversalInstanceGCDescSize = RuntimeAugments.GetGCDescSize(tempNonUniversalState.TemplateType.RuntimeTypeHandle);
            if (state.NonUniversalInstanceGCDescSize > 0)
                state.NonUniversalInstanceGCDesc = new IntPtr(((byte*)tempNonUniversalState.TemplateType.RuntimeTypeHandle.ToIntPtr().ToPointer()) - 1);
        }
#endif

        private unsafe void AllocateRuntimeType(TypeDesc type)
        {
            TypeBuilderState state = type.GetTypeBuilderState();

            Debug.Assert(type is DefType || type is ArrayType || type is PointerType || type is ByRefType);

            if (state.ThreadDataSize != 0)
                state.ThreadStaticOffset = TypeLoaderEnvironment.Instance.GetNextThreadStaticsOffsetValue();

            RuntimeTypeHandle rtt = EETypeCreator.CreateEEType(type, state);

            if (state.ThreadDataSize != 0)
                TypeLoaderEnvironment.Instance.RegisterDynamicThreadStaticsInfo(state.HalfBakedRuntimeTypeHandle, state.ThreadStaticOffset, state.ThreadDataSize);

            TypeLoaderLogger.WriteLine("Allocated new type " + type.ToString() + " with hashcode value = 0x" + type.GetHashCode().LowLevelToString() + " with eetype = " + rtt.ToIntPtr().LowLevelToString() + " of size " + rtt.ToEETypePtr()->BaseSize.LowLevelToString());
        }

        private void AllocateRuntimeMethodDictionary(InstantiatedMethod method)
        {
            Debug.Assert(method.RuntimeMethodDictionary == IntPtr.Zero && method.Dictionary != null);

            IntPtr rmd = method.Dictionary.Allocate();
            method.AssociateWithRuntimeMethodDictionary(rmd);

            TypeLoaderLogger.WriteLine("Allocated new method dictionary for method " + method.ToString() + " @ " + rmd.LowLevelToString());
        }

        private RuntimeTypeHandle[] GetGenericContextOfBaseType(DefType type, int vtableMethodSlot)
        {
            DefType baseType = type.BaseType;
            Debug.Assert(baseType == null || !GetRuntimeTypeHandle(baseType).IsNull());
            Debug.Assert(vtableMethodSlot < GetRuntimeTypeHandle(type).GetNumVtableSlots());

            int numBaseTypeVtableSlots = baseType == null ? 0 : GetRuntimeTypeHandle(baseType).GetNumVtableSlots();

            if (vtableMethodSlot < numBaseTypeVtableSlots)
                return GetGenericContextOfBaseType(baseType, vtableMethodSlot);
            else
                return GetRuntimeTypeHandles(type.Instantiation);
        }

        private unsafe void FinishVTableCallingConverterThunks(TypeDesc type, TypeBuilderState state)
        {
            Debug.Assert(state.TemplateType.IsCanonicalSubtype(CanonicalFormKind.Universal));

            if (state.VTableMethodSignatures == null || state.VTableMethodSignatures.Length == 0)
                return;

            int numVtableSlots = GetRuntimeTypeHandle(type).GetNumVtableSlots();
            IntPtr* vtableCells = (IntPtr*)((byte*)GetRuntimeTypeHandle(type).ToIntPtr() + sizeof(EEType));
            Debug.Assert((state.VTableMethodSignatures.Length - state.NumSealedVTableMethodSignatures) <= numVtableSlots);

            TypeDesc baseType = type.BaseType;
            int numBaseTypeVtableSlots = GetRuntimeTypeHandle(baseType).GetNumVtableSlots();

            // Generic context
            RuntimeTypeHandle[] typeArgs = Empty<RuntimeTypeHandle>.Array;

            if (type is DefType)
                typeArgs = GetRuntimeTypeHandles(((DefType)type).Instantiation);
            else if (type is ArrayType)
                typeArgs = GetRuntimeTypeHandles(new Instantiation(new TypeDesc[] { ((ArrayType)type).ElementType }));

            for (int i = 0; i < state.VTableMethodSignatures.Length; i++)
            {
                RuntimeTypeHandle[] typeArgsToUse = typeArgs;

                int vtableSlotInDynamicType = -1;
                if (!state.VTableMethodSignatures[i].IsSealedVTableSlot)
                {
                    vtableSlotInDynamicType = state.VTableSlotsMapping.GetVTableSlotInTargetType((int)state.VTableMethodSignatures[i].VTableSlot);
                    Debug.Assert(vtableSlotInDynamicType != -1);

                    if (vtableSlotInDynamicType < numBaseTypeVtableSlots)
                    {
                        // Vtable method  from the vtable portion of a base type. Use generic context of the basetype defining the vtable slot.
                        // We should never reach here for array types (the vtable entries of the System.Array basetype should never need a converter).
                        Debug.Assert(type is DefType);
                        typeArgsToUse = GetGenericContextOfBaseType((DefType)type, vtableSlotInDynamicType);
                    }
                }

                IntPtr originalFunctionPointerFromVTable = state.VTableMethodSignatures[i].IsSealedVTableSlot ?
                    ((IntPtr*)state.HalfBakedSealedVTable)[state.VTableMethodSignatures[i].VTableSlot] :
                    vtableCells[vtableSlotInDynamicType];

                IntPtr thunkPtr = CallConverterThunk.MakeThunk(
                    ThunkKind.StandardToGeneric,
                    originalFunctionPointerFromVTable,
                    state.VTableMethodSignatures[i].MethodSignature,
                    IntPtr.Zero,                                        // No instantiating arg for non-generic instance methods
                    typeArgsToUse,
                    Empty<RuntimeTypeHandle>.Array);                    // No GVMs in vtables, no no method args

                if (state.VTableMethodSignatures[i].IsSealedVTableSlot)
                {
                    // Patch the sealed vtable entry to point to the calling converter thunk
                    Debug.Assert(state.VTableMethodSignatures[i].VTableSlot < state.NumSealedVTableEntries && state.HalfBakedSealedVTable != IntPtr.Zero);
                    ((IntPtr*)state.HalfBakedSealedVTable)[state.VTableMethodSignatures[i].VTableSlot] = thunkPtr;
                }
                else
                {
                    // Patch the vtable entry to point to the calling converter thunk
                    Debug.Assert(vtableSlotInDynamicType < numVtableSlots && vtableCells != null);
                    vtableCells[vtableSlotInDynamicType] = thunkPtr;
                }
            }
        }

        //
        // Returns either the registered type handle or half-baked type handle. This method should be only called
        // during final phase of type building.
        //
        public RuntimeTypeHandle GetRuntimeTypeHandle(TypeDesc type)
        {
#if DEBUG
            Debug.Assert(_finalTypeBuilding);
#endif

            var rtth = type.RuntimeTypeHandle;
            if (!rtth.IsNull())
                return rtth;

            rtth = type.GetTypeBuilderState().HalfBakedRuntimeTypeHandle;
            Debug.Assert(!rtth.IsNull());
            return rtth;
        }

        public RuntimeTypeHandle[] GetRuntimeTypeHandles(Instantiation types)
        {
            if (types.Length == 0)
                return Array.Empty<RuntimeTypeHandle>();

            RuntimeTypeHandle[] result = new RuntimeTypeHandle[types.Length];
            for (int i = 0; i < types.Length; i++)
                result[i] = GetRuntimeTypeHandle(types[i]);
            return result;
        }

        public static DefType GetBaseTypeUsingRuntimeTypeHandle(TypeDesc type)
        {
            type.RetrieveRuntimeTypeHandleIfPossible();
            unsafe
            {
                RuntimeTypeHandle thBaseTypeTemplate = type.RuntimeTypeHandle.ToEETypePtr()->BaseType->ToRuntimeTypeHandle();
                if (thBaseTypeTemplate.IsNull())
                    return null;

                return (DefType)type.Context.ResolveRuntimeTypeHandle(thBaseTypeTemplate);
            }
        }

        public static DefType GetBaseTypeThatIsCorrectForMDArrays(TypeDesc type)
        {
            if (type.BaseType == type.Context.GetWellKnownType(WellKnownType.Array))
            {
                // Use the type from the template, the metadata we have will be inaccurate for multidimensional
                // arrays, as we hide the MDArray infrastructure from the metadata.
                TypeDesc template = type.ComputeTemplate(false);
                return GetBaseTypeUsingRuntimeTypeHandle(template ?? type);
            }

            return type.BaseType;
        }

        private void FinishInterfaces(TypeDesc type, TypeBuilderState state)
        {
            DefType[] interfaces = state.RuntimeInterfaces;
            if (interfaces != null)
            {
                for (int i = 0; i < interfaces.Length; i++)
                {
                    state.HalfBakedRuntimeTypeHandle.SetInterface(i, GetRuntimeTypeHandle(interfaces[i]));
                }
            }
        }

        unsafe private void FinishTypeDictionary(TypeDesc type, TypeBuilderState state)
        {
            if (state.Dictionary != null)
            {
                // First, update the dictionary slot in the type's vtable to point to the created dictionary when applicable
                Debug.Assert(state.HalfBakedDictionary != IntPtr.Zero);

                int dictionarySlot = EETypeCreator.GetDictionarySlotInVTable(type);
                if (dictionarySlot >= 0)
                {
                    state.HalfBakedRuntimeTypeHandle.SetDictionary(dictionarySlot, state.HalfBakedDictionary);
                }
                else
                {
                    // Dictionary shouldn't be in the vtable of the type
                    Debug.Assert(!type.CanShareNormalGenericCode());
                }

                TypeLoaderLogger.WriteLine("Setting dictionary entries for type " + type.ToString() + " @ " + state.HalfBakedDictionary.LowLevelToString());
                state.Dictionary.Finish(this);
            }
        }

        unsafe private void FinishMethodDictionary(InstantiatedMethod method)
        {
            Debug.Assert(method.Dictionary != null);

            TypeLoaderLogger.WriteLine("Setting dictionary entries for method " + method.ToString() + " @ " + method.RuntimeMethodDictionary.LowLevelToString());
            method.Dictionary.Finish(this);
        }

        unsafe private void FinishClassConstructor(TypeDesc type, TypeBuilderState state)
        {
            if (!state.HasStaticConstructor)
                return;

            IntPtr canonicalClassConstructorFunctionPointer = IntPtr.Zero; // Pointer to canonical static method to serve as cctor
            IntPtr exactClassConstructorFunctionPointer = IntPtr.Zero; // Exact pointer. Takes priority over canonical pointer

            if (state.TemplateType == null)
            {
                if (!type.HasInstantiation)
                {
                    // Non-Generic ReadyToRun types in their current state already have their static field region setup
                    // with the class constructor initialized.
                    return;
                }
                else
                {
                    // For generic types, we need to do the metadata lookup and then resolve to a function pointer.
                    MethodDesc staticConstructor = type.GetStaticConstructor();
                    IntPtr staticCctor;
                    IntPtr unused1;
                    TypeLoaderEnvironment.MethodAddressType addressType;
                    if (!TypeLoaderEnvironment.TryGetMethodAddressFromMethodDesc(staticConstructor, out staticCctor, out unused1, out addressType))
                    {
                        Environment.FailFast("Unable to find class constructor method address for type:" + type.ToString());
                    }
                    Debug.Assert(unused1 == IntPtr.Zero);

                    switch (addressType)
                    {
                        case TypeLoaderEnvironment.MethodAddressType.Exact:
                            // If we have an exact match, put it in the slot directly
                            // and return as we don't want to make this into a fat function pointer
                            exactClassConstructorFunctionPointer = staticCctor;
                            break;

                        case TypeLoaderEnvironment.MethodAddressType.Canonical:
                        case TypeLoaderEnvironment.MethodAddressType.UniversalCanonical:
                            // If we have a canonical method, setup for generating a fat function pointer
                            canonicalClassConstructorFunctionPointer = staticCctor;
                            break;

                        default:
                            Environment.FailFast("Invalid MethodAddressType during ClassConstructor discovery");
                            return;
                    }
                }
            }
            else if (state.ClassConstructorPointer.HasValue)
            {
                canonicalClassConstructorFunctionPointer = state.ClassConstructorPointer.Value;
            }
            else
            {
                // Lookup the non-GC static data for the template type, and use the class constructor context offset to locate the class constructor's
                // fat pointer within the non-GC static data.
                IntPtr templateTypeStaticData = TypeLoaderEnvironment.Instance.TryGetNonGcStaticFieldData(GetRuntimeTypeHandle(state.TemplateType));
                Debug.Assert(templateTypeStaticData != IntPtr.Zero);
                IntPtr* templateTypeClassConstructorSlotPointer = (IntPtr*)((byte*)*((IntPtr*)templateTypeStaticData) + ClassConstructorOffset);
                IntPtr templateTypeClassConstructorFatFunctionPointer = templateTypeClassConstructorFatFunctionPointer = *templateTypeClassConstructorSlotPointer;

                // Crack the fat function pointer into the raw class constructor method pointer and the generic type dictionary.
                Debug.Assert(FunctionPointerOps.IsGenericMethodPointer(templateTypeClassConstructorFatFunctionPointer));
                GenericMethodDescriptor* templateTypeGenericMethodDescriptor = FunctionPointerOps.ConvertToGenericDescriptor(templateTypeClassConstructorFatFunctionPointer);
                Debug.Assert(templateTypeGenericMethodDescriptor != null);
                canonicalClassConstructorFunctionPointer = templateTypeGenericMethodDescriptor->MethodFunctionPointer;
            }

            IntPtr generatedTypeStaticData = GetRuntimeTypeHandle(type).ToEETypePtr()->DynamicNonGcStaticsData;
            IntPtr* generatedTypeClassConstructorSlotPointer = (IntPtr*)((byte*)*((IntPtr*)generatedTypeStaticData) + ClassConstructorOffset);

            if (exactClassConstructorFunctionPointer != IntPtr.Zero)
            {
                // We have an exact pointer, not a canonical match
                // Just set the pointer and return. No need for a fat pointer
                *generatedTypeClassConstructorSlotPointer = exactClassConstructorFunctionPointer;
                return;
            }

            // If we reach here, classConstructorFunctionPointer points at a canonical method, that needs to be converted into 
            // a fat function pointer so that the calli in the ClassConstructorRunner will work properly
            Debug.Assert(canonicalClassConstructorFunctionPointer != IntPtr.Zero);

            // Use the template type's class constructor method pointer and this type's generic type dictionary to generate a new fat pointer,
            // and save that fat pointer back to this type's class constructor context offset within the non-GC static data.
            IntPtr instantiationArgument = GetRuntimeTypeHandle(type).ToIntPtr();
            IntPtr generatedTypeClassConstructorFatFunctionPointer = FunctionPointerOps.GetGenericMethodFunctionPointer(canonicalClassConstructorFunctionPointer, instantiationArgument);
            *generatedTypeClassConstructorSlotPointer = generatedTypeClassConstructorFatFunctionPointer;
        }

        private void CopyDictionaryFromTypeToAppropriateSlotInDerivedType(TypeDesc baseType, TypeBuilderState derivedTypeState)
        {
            var baseTypeState = baseType.GetOrCreateTypeBuilderState();

            if (baseTypeState.HasDictionaryInVTable)
            {
                RuntimeTypeHandle baseTypeHandle = GetRuntimeTypeHandle(baseType);

                // If the basetype is currently being created by the TypeBuilder, we need to get its dictionary pointer from the 
                // TypeBuilder state (at this point, the dictionary has not yet been set on the baseTypeHandle). If
                // the basetype is not a dynamic type, or has previously been dynamically allocated in the past, the TypeBuilder
                // state will have a null dictionary pointer, in which case we need to read it directly from the basetype's vtable
                IntPtr dictionaryEntry = baseTypeState.HalfBakedDictionary;
                if (dictionaryEntry == IntPtr.Zero)
                    dictionaryEntry = baseTypeHandle.GetDictionary();
                Debug.Assert(dictionaryEntry != IntPtr.Zero);

                // Compute the vtable slot for the dictionary entry to set
                int dictionarySlot = EETypeCreator.GetDictionarySlotInVTable(baseType);
                Debug.Assert(dictionarySlot >= 0);

                derivedTypeState.HalfBakedRuntimeTypeHandle.SetDictionary(dictionarySlot, dictionaryEntry);
                TypeLoaderLogger.WriteLine("Setting basetype " + baseType.ToString() + " dictionary on type " + derivedTypeState.TypeBeingBuilt.ToString());
            }
        }

        private void FinishBaseTypeAndDictionaries(TypeDesc type, TypeBuilderState state)
        {
            DefType baseType = GetBaseTypeThatIsCorrectForMDArrays(type);
            state.HalfBakedRuntimeTypeHandle.SetBaseType(baseType == null ? default(RuntimeTypeHandle) : GetRuntimeTypeHandle(baseType));

            if (baseType == null)
                return;

            // Update every dictionary in type hierarchy with copy from base type
            while (baseType != null)
            {
                CopyDictionaryFromTypeToAppropriateSlotInDerivedType(baseType, state);
                baseType = baseType.BaseType;
            }
        }

        private void FinishRuntimeType(TypeDesc type)
        {
            TypeLoaderLogger.WriteLine("Finishing type " + type.ToString() + " ...");

            var state = type.GetTypeBuilderState();

            if (type is DefType)
            {
                DefType typeAsDefType = (DefType)type;

                if (type.HasInstantiation)
                {
                    // Type definitions don't need any further finishing once created by the EETypeCreator
                    if (type.IsTypeDefinition)
                        return;

                    state.HalfBakedRuntimeTypeHandle.SetGenericDefinition(GetRuntimeTypeHandle(typeAsDefType.GetTypeDefinition()));
                    Instantiation instantiation = typeAsDefType.Instantiation;
                    for (int argIndex = 0; argIndex < instantiation.Length; argIndex++)
                        state.HalfBakedRuntimeTypeHandle.SetGenericArgument(argIndex, GetRuntimeTypeHandle(instantiation[argIndex]));
                }

                FinishBaseTypeAndDictionaries(type, state);

                FinishInterfaces(type, state);

                FinishTypeDictionary(type, state);

                FinishClassConstructor(type, state);

                // For types that were allocated from universal canonical templates, patch their vtables with
                // pointers to calling convention conversion thunks
                if (state.TemplateType != null && state.TemplateType.IsCanonicalSubtype(CanonicalFormKind.Universal))
                    FinishVTableCallingConverterThunks(type, state);

                if (RuntimeAugments.IsNullable(state.HalfBakedRuntimeTypeHandle))
                {
                    Debug.Assert(typeAsDefType.Instantiation.Length == 1);
                    state.HalfBakedRuntimeTypeHandle.SetNullableType(GetRuntimeTypeHandle(typeAsDefType.Instantiation[0]));
                }
            }
            else if (type is ParameterizedType)
            {
                if (type is ArrayType)
                {
                    ArrayType typeAsSzArrayType = (ArrayType)type;

                    state.HalfBakedRuntimeTypeHandle.SetRelatedParameterType(GetRuntimeTypeHandle(typeAsSzArrayType.ElementType));

                    state.HalfBakedRuntimeTypeHandle.SetComponentSize(state.ComponentSize.Value);

                    FinishInterfaces(type, state);

                    if (typeAsSzArrayType.IsSzArray && !typeAsSzArrayType.ElementType.IsPointer)
                    {
                        FinishTypeDictionary(type, state);

                        // For types that were allocated from universal canonical templates, patch their vtables with
                        // pointers to calling convention conversion thunks
                        if (state.TemplateType != null && state.TemplateType.IsCanonicalSubtype(CanonicalFormKind.Universal))
                            FinishVTableCallingConverterThunks(type, state);
                    }
                }
                else if (type is PointerType)
                {
                    state.HalfBakedRuntimeTypeHandle.SetRelatedParameterType(GetRuntimeTypeHandle(((PointerType)type).ParameterType));

                    // Nothing else to do for pointer types
                }
                else if (type is ByRefType)
                {
                    state.HalfBakedRuntimeTypeHandle.SetRelatedParameterType(GetRuntimeTypeHandle(((ByRefType)type).ParameterType));

                    // We used a pointer type for the template because they're similar enough. Adjust this to be a ByRef.
                    unsafe { Debug.Assert(state.HalfBakedRuntimeTypeHandle.ToEETypePtr()->ParameterizedTypeShape == ParameterizedTypeShapeConstants.Pointer); }
                    state.HalfBakedRuntimeTypeHandle.SetParameterizedTypeShape(ParameterizedTypeShapeConstants.ByRef);
                }
            }
            else
            {
                Debug.Assert(false);
            }
        }

        private IEnumerable<TypeEntryToRegister> TypesToRegister()
        {
            for (int i = 0; i < _typesThatNeedTypeHandles.Count; i++)
            {
                DefType typeAsDefType = _typesThatNeedTypeHandles[i] as DefType;
                if (typeAsDefType == null)
                    continue;

                if (typeAsDefType.HasInstantiation && !typeAsDefType.IsTypeDefinition)
                {
                    yield return new TypeEntryToRegister
                    {
                        GenericTypeEntry = new GenericTypeEntry
                        {
                            _genericTypeDefinitionHandle = GetRuntimeTypeHandle(typeAsDefType.GetTypeDefinition()),
                            _genericTypeArgumentHandles = GetRuntimeTypeHandles(typeAsDefType.Instantiation),
                            _instantiatedTypeHandle = typeAsDefType.GetTypeBuilderState().HalfBakedRuntimeTypeHandle
                        }
                    };
                }
                else
                {
                    yield return new TypeEntryToRegister
                    {
                        MetadataDefinitionType = (MetadataType)typeAsDefType
                    };
                }
            }
        }

        private IEnumerable<GenericMethodEntry> MethodsToRegister()
        {
            for (int i = 0; i < _methodsThatNeedDictionaries.Count; i++)
            {
                InstantiatedMethod method = _methodsThatNeedDictionaries[i];
                yield return new GenericMethodEntry
                {
                    _declaringTypeHandle = GetRuntimeTypeHandle(method.OwningType),
                    _genericMethodArgumentHandles = GetRuntimeTypeHandles(method.Instantiation),
                    _methodNameAndSignature = method.NameAndSignature,
                    _methodDictionary = method.RuntimeMethodDictionary
                };
            }
        }

        private void RegisterGenericTypesAndMethods()
        {
            int typesToRegisterCount = 0;
            for (int i = 0; i < _typesThatNeedTypeHandles.Count; i++)
            {
                DefType typeAsDefType;
                if ((typeAsDefType = _typesThatNeedTypeHandles[i] as DefType) != null)
                    typesToRegisterCount++;
            }

            DynamicGenericsRegistrationData registrationData = new DynamicGenericsRegistrationData
            {
                TypesToRegisterCount = typesToRegisterCount,
                TypesToRegister = (typesToRegisterCount != 0) ? TypesToRegister() : null,
                MethodsToRegisterCount = _methodsThatNeedDictionaries.Count,
                MethodsToRegister = (_methodsThatNeedDictionaries.Count != 0) ? MethodsToRegister() : null,
            };
            TypeLoaderEnvironment.Instance.RegisterDynamicGenericTypesAndMethods(registrationData);
        }

        /// <summary>
        /// Publish generic type / method information to the data buffer read by the debugger. This supports
        /// debugging dynamically created types / methods
        /// </summary>
        private void RegisterDebugDataForTypesAndMethods()
        {
            for (int i = 0; i < _typesThatNeedTypeHandles.Count; i++)
            {
                DefType typeAsDefType;
                if ((typeAsDefType = _typesThatNeedTypeHandles[i] as DefType) != null)
                {
                    SerializedDebugData.RegisterDebugDataForType(this, typeAsDefType, typeAsDefType.GetTypeBuilderState());
                }
            }

            for (int i = 0; i < _methodsThatNeedDictionaries.Count; i++)
            {
                SerializedDebugData.RegisterDebugDataForMethod(this, _methodsThatNeedDictionaries[i]);
            }
        }

        private void FinishTypeAndMethodBuilding()
        {
            // Once we start allocating EETypes and dictionaries, the only accepted failure is OOM.
            // TODO: Error handling - on retry, restart where we failed last time? The current implementation is leaking on OOM.

#if DEBUG
            _finalTypeBuilding = true;
#endif

            // At this point we know all types that need EETypes. Allocate all EETypes so that we can start building
            // their contents.
            for (int i = 0; i < _typesThatNeedTypeHandles.Count; i++)
            {
                AllocateRuntimeType(_typesThatNeedTypeHandles[i]);
            }

            for (int i = 0; i < _methodsThatNeedDictionaries.Count; i++)
            {
                AllocateRuntimeMethodDictionary(_methodsThatNeedDictionaries[i]);
            }

            // Do not add more type phases here. Instead, read the required information from the TypeDesc or TypeBuilderState.

            // Fill in content of all EETypes
            for (int i = 0; i < _typesThatNeedTypeHandles.Count; i++)
            {
                FinishRuntimeType(_typesThatNeedTypeHandles[i]);
            }

            for (int i = 0; i < _methodsThatNeedDictionaries.Count; i++)
            {
                FinishMethodDictionary(_methodsThatNeedDictionaries[i]);
            }

            RegisterDebugDataForTypesAndMethods();

            int newArrayTypesCount = 0;
            int newPointerTypesCount = 0;
            int newByRefTypesCount = 0;
            int[] mdArrayNewTypesCount = null;

            for (int i = 0; i < _typesThatNeedTypeHandles.Count; i++)
            {
                ParameterizedType typeAsParameterizedType = _typesThatNeedTypeHandles[i] as ParameterizedType;
                if (typeAsParameterizedType == null)
                    continue;

                if (typeAsParameterizedType.IsSzArray)
                    newArrayTypesCount++;
                else if (typeAsParameterizedType.IsPointer)
                    newPointerTypesCount++;
                else if (typeAsParameterizedType.IsByRef)
                    newByRefTypesCount++;
                else if (typeAsParameterizedType.IsMdArray)
                {
                    if (mdArrayNewTypesCount == null)
                        mdArrayNewTypesCount = new int[MDArray.MaxRank + 1];
                    mdArrayNewTypesCount[((ArrayType)typeAsParameterizedType).Rank]++;
                }
            }
            // Reserve space in array/pointer cache's so that the actual adding can be fault-free.
            var szArrayCache = TypeSystemContext.GetArrayTypesCache(false, -1);
            szArrayCache.Reserve(szArrayCache.Count + newArrayTypesCount);

            // 
            if (mdArrayNewTypesCount != null)
            {
                for (int i = 0; i < mdArrayNewTypesCount.Length; i++)
                {
                    if (mdArrayNewTypesCount[i] == 0)
                        continue;

                    var mdArrayCache = TypeSystemContext.GetArrayTypesCache(true, i);
                    mdArrayCache.Reserve(mdArrayCache.Count + mdArrayNewTypesCount[i]);
                }
            }

            TypeSystemContext.PointerTypesCache.Reserve(TypeSystemContext.PointerTypesCache.Count + newPointerTypesCount);
            TypeSystemContext.ByRefTypesCache.Reserve(TypeSystemContext.ByRefTypesCache.Count + newByRefTypesCount);

            // Finally, register all generic types and methods atomically with the runtime
            RegisterGenericTypesAndMethods();


            for (int i = 0; i < _typesThatNeedTypeHandles.Count; i++)
            {
                _typesThatNeedTypeHandles[i].SetRuntimeTypeHandleUnsafe(_typesThatNeedTypeHandles[i].GetTypeBuilderState().HalfBakedRuntimeTypeHandle);

                TypeLoaderLogger.WriteLine("Successfully Registered type " + _typesThatNeedTypeHandles[i].ToString() + ".");
            }

            // Save all constructed array and pointer types to the types cache
            for (int i = 0; i < _typesThatNeedTypeHandles.Count; i++)
            {
                ParameterizedType typeAsParameterizedType = _typesThatNeedTypeHandles[i] as ParameterizedType;
                if (typeAsParameterizedType == null)
                    continue;

                Debug.Assert(!typeAsParameterizedType.RuntimeTypeHandle.IsNull());
                Debug.Assert(!typeAsParameterizedType.ParameterType.RuntimeTypeHandle.IsNull());

                if (typeAsParameterizedType.IsMdArray)
                    TypeSystemContext.GetArrayTypesCache(true, ((ArrayType)typeAsParameterizedType).Rank).AddOrGetExisting(typeAsParameterizedType.RuntimeTypeHandle);
                else if (typeAsParameterizedType.IsSzArray)
                    TypeSystemContext.GetArrayTypesCache(false, -1).AddOrGetExisting(typeAsParameterizedType.RuntimeTypeHandle);
                else if (typeAsParameterizedType.IsByRef)
                {
                    unsafe
                    {
                        Debug.Assert(typeAsParameterizedType.RuntimeTypeHandle.ToEETypePtr()->IsByRefType);
                    }
                    TypeSystemContext.ByRefTypesCache.AddOrGetExisting(typeAsParameterizedType.RuntimeTypeHandle);
                }
                else
                {
                    Debug.Assert(typeAsParameterizedType is PointerType);
                    unsafe
                    {
                        Debug.Assert(typeAsParameterizedType.RuntimeTypeHandle.ToEETypePtr()->IsPointerType);
                    }
                    TypeSystemContext.PointerTypesCache.AddOrGetExisting(typeAsParameterizedType.RuntimeTypeHandle);
                }
            }
        }

        internal void BuildType(TypeDesc type)
        {
            TypeLoaderLogger.WriteLine("Dynamically allocating new type for " + type.ToString());

            // Construct a new type along with all the dependencies that are needed to create interface lists,
            // generic dictionaries, etc.

            // Start by collecting all dependencies we need to create in order to create this type.
            PrepareType(type);

            // Process the pending types
            ProcessTypesNeedingPreparation();

            FinishTypeAndMethodBuilding();
        }

        internal bool TryComputeFieldOffset(DefType declaringType, uint fieldOrdinal, out int fieldOffset)
        {
            fieldOffset = int.MinValue;

            TypeLoaderLogger.WriteLine("Computing offset of field #" + fieldOrdinal.LowLevelToString() + " on type " + declaringType.ToString());

            // Get the computed field offset result
            LayoutInt layoutFieldOffset = declaringType.GetFieldByNativeLayoutOrdinal(fieldOrdinal).Offset;
            if (layoutFieldOffset.IsIndeterminate)
            {
                fieldOffset = 0;
                return false;
            }
            fieldOffset = layoutFieldOffset.AsInt;
            return true;
        }

        private void BuildMethod(InstantiatedMethod method)
        {
            TypeLoaderLogger.WriteLine("Dynamically allocating new method instantiation for " + method.ToString());

            // Start by collecting all dependencies we need to create in order to create this method.
            PrepareMethod(method);

            // Process the pending types
            ProcessTypesNeedingPreparation();

            FinishTypeAndMethodBuilding();
        }

        private static DefType GetExactDeclaringType(DefType srcDefType, DefType dstDefType)
        {
            while (srcDefType != null)
            {
                if (srcDefType.HasSameTypeDefinition(dstDefType))
                    return srcDefType;

                srcDefType = srcDefType.BaseType;
            }

            Debug.Assert(false);
            return null;
        }

        //
        // This method is used by the lazy generic lookup. It resolves the signature of the runtime artifact in the given instantiation context.
        // 
        private unsafe IntPtr BuildGenericLookupTarget(TypeSystemContext typeSystemContext, IntPtr context, IntPtr signature, out IntPtr auxResult)
        {
            TypeLoaderLogger.WriteLine("BuildGenericLookupTarget for " + context.LowLevelToString() + "/" + signature.LowLevelToString());

            TypeManagerHandle typeManager;
            NativeReader reader;
            uint offset;

#if PROJECTN
            // If the system module is compiled with as a type manager, all modules are compiled as such
            if (!ModuleList.Instance.SystemModule.Handle.IsTypeManager)
            {
                IntPtr moduleHandle = RuntimeAugments.GetOSModuleFromPointer(signature);
                typeManager = new TypeManagerHandle(moduleHandle);
                reader = TypeLoaderEnvironment.Instance.GetNativeLayoutInfoReader(typeManager);
                offset = reader.AddressToOffset(signature);
            }
            else
#endif
            {
                // The first is a pointer that points to the TypeManager indirection cell.
                // The second is the offset into the native layout info blob in that TypeManager, where the native signature is encoded.
                IntPtr** lazySignature = (IntPtr**)signature.ToPointer();
                typeManager = new TypeManagerHandle(lazySignature[0][0]);
                offset = checked((uint)new IntPtr(lazySignature[1]).ToInt32());
                reader = TypeLoaderEnvironment.Instance.GetNativeLayoutInfoReader(typeManager);
            }

            NativeParser parser = new NativeParser(reader, offset);

            GenericContextKind contextKind = (GenericContextKind)parser.GetUnsigned();

            NativeFormatModuleInfo moduleInfo = ModuleList.Instance.GetModuleInfoByHandle(typeManager);

            NativeLayoutInfoLoadContext nlilContext = new NativeLayoutInfoLoadContext();
            nlilContext._module = moduleInfo;
            nlilContext._typeSystemContext = typeSystemContext;

#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
            NativeFormatMetadataUnit metadataUnit = null;

            if (moduleInfo.ModuleType == ModuleType.ReadyToRun)
                metadataUnit = typeSystemContext.ResolveMetadataUnit(moduleInfo);
#endif

            if ((contextKind & GenericContextKind.FromMethodHiddenArg) != 0)
            {
                RuntimeTypeHandle declaringTypeHandle;
                MethodNameAndSignature nameAndSignature;
                RuntimeTypeHandle[] genericMethodArgHandles;
                bool success = TypeLoaderEnvironment.Instance.TryGetGenericMethodComponents(context, out declaringTypeHandle, out nameAndSignature, out genericMethodArgHandles);
                Debug.Assert(success);

                if (RuntimeAugments.IsGenericType(declaringTypeHandle))
                {
                    DefType declaringType = (DefType)typeSystemContext.ResolveRuntimeTypeHandle(declaringTypeHandle);
                    nlilContext._typeArgumentHandles = declaringType.Instantiation;
                }

                nlilContext._methodArgumentHandles = typeSystemContext.ResolveRuntimeTypeHandles(genericMethodArgHandles);
            }
            else
            {
                TypeDesc typeContext = typeSystemContext.ResolveRuntimeTypeHandle(RuntimeAugments.CreateRuntimeTypeHandle(context));

                if (typeContext is DefType)
                {
                    nlilContext._typeArgumentHandles = ((DefType)typeContext).Instantiation;
                }
                else if (typeContext is ArrayType)
                {
                    nlilContext._typeArgumentHandles = new Instantiation(new TypeDesc[] { ((ArrayType)typeContext).ElementType });
                }
                else
                {
                    Debug.Assert(false);
                }

                if ((contextKind & GenericContextKind.HasDeclaringType) != 0)
                {
                    // No need to deal with arrays - arrays can't have declaring type

                    TypeDesc declaringType;

                    if (moduleInfo.ModuleType == ModuleType.Eager)
                    {
                        declaringType = nlilContext.GetType(ref parser);
                    }
                    else
                    {
                        Debug.Assert(moduleInfo.ModuleType == ModuleType.ReadyToRun);
#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
                        uint typeToken = parser.GetUnsigned();
                        declaringType = metadataUnit.GetType(((int)typeToken).AsHandle());
#else
                        Environment.FailFast("Ready to Run module type?");
                        declaringType = null;
#endif
                    }

                    DefType actualContext = GetExactDeclaringType((DefType)typeContext, (DefType)declaringType);

                    nlilContext._typeArgumentHandles = actualContext.Instantiation;
                }
            }

            if ((contextKind & GenericContextKind.NeedsUSGContext) != 0)
            {
                IntPtr genericDictionary;
                auxResult = IntPtr.Zero;

                // There is a cache in place so that this function doesn't get called much, but we still need a registration store,
                // so we don't leak allocated contexts
                if (TypeLoaderEnvironment.Instance.TryLookupConstructedLazyDictionaryForContext(context, signature, out genericDictionary))
                {
                    return genericDictionary;
                }

                GenericTypeDictionary ucgDict;

                if (moduleInfo.ModuleType == ModuleType.Eager)
                {
                    ucgDict = new GenericTypeDictionary(GenericDictionaryCell.BuildDictionary(this, nlilContext, parser));
                }
                else
                {
#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
                    Debug.Assert(moduleInfo.ModuleType == ModuleType.ReadyToRun);
                    FixupCellMetadataResolver metadataResolver = new FixupCellMetadataResolver(metadataUnit, nlilContext);
                    ucgDict = new GenericTypeDictionary(GenericDictionaryCell.BuildDictionaryFromMetadataTokensAndContext(this, parser, metadataUnit, metadataResolver));
#else
                    Environment.FailFast("Ready to Run module type?");
                    ucgDict = null;
#endif
                }
                genericDictionary = ucgDict.Allocate();

                // Process the pending types
                ProcessTypesNeedingPreparation();

                FinishTypeAndMethodBuilding();

                ucgDict.Finish(this);

                TypeLoaderEnvironment.Instance.RegisterConstructedLazyDictionaryForContext(context, signature, genericDictionary);
                return genericDictionary;
            }
            else
            {
                GenericDictionaryCell cell;

                if (moduleInfo.ModuleType == ModuleType.Eager)
                {
                    cell = GenericDictionaryCell.ParseAndCreateCell(
                        nlilContext,
                        ref parser);
                }
                else
                {
                    Debug.Assert(moduleInfo.ModuleType == ModuleType.ReadyToRun);
#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
                    MetadataFixupKind fixupKind = (MetadataFixupKind)parser.GetUInt8();
                    Internal.Metadata.NativeFormat.Handle token = parser.GetUnsigned().AsHandle();
                    Internal.Metadata.NativeFormat.Handle token2 = default(Internal.Metadata.NativeFormat.Handle);

                    switch (fixupKind)
                    {
                        case MetadataFixupKind.GenericConstrainedMethod:
                        case MetadataFixupKind.NonGenericConstrainedMethod:
                        case MetadataFixupKind.NonGenericDirectConstrainedMethod:
                            token2 = parser.GetUnsigned().AsHandle();
                            break;
                    }

                    FixupCellMetadataResolver resolver = new FixupCellMetadataResolver(metadataUnit, nlilContext);
                    cell = GenericDictionaryCell.CreateCellFromFixupKindAndToken(fixupKind, resolver, token, token2);
#else
                    Environment.FailFast("Ready to Run module type?");
                    cell = null;
#endif
                }

                cell.Prepare(this);

                // Process the pending types
                ProcessTypesNeedingPreparation();

                FinishTypeAndMethodBuilding();

                IntPtr dictionaryCell = cell.CreateLazyLookupCell(this, out auxResult);

                return dictionaryCell;
            }
        }

        //
        // This method is used to build the floating portion of a generic dictionary.
        // 
        private unsafe IntPtr BuildFloatingDictionary(TypeSystemContext typeSystemContext, IntPtr context, bool isTypeContext, IntPtr fixedDictionary, out bool isNewlyAllocatedDictionary)
        {
            isNewlyAllocatedDictionary = true;

            NativeParser nativeLayoutParser;
            NativeLayoutInfoLoadContext nlilContext;

            if (isTypeContext)
            {
                TypeDesc typeContext = typeSystemContext.ResolveRuntimeTypeHandle(*(RuntimeTypeHandle*)&context);

                TypeLoaderLogger.WriteLine("Building floating dictionary layout for type " + typeContext.ToString() + "...");

                // We should only perform updates to floating dictionaries for types that share normal canonical code
                Debug.Assert(typeContext.CanShareNormalGenericCode());

                // Computing the template will throw if no template is found.
                typeContext.ComputeTemplate();

                TypeBuilderState state = typeContext.GetOrCreateTypeBuilderState();
                nativeLayoutParser = state.GetParserForNativeLayoutInfo();
                nlilContext = state.NativeLayoutInfo.LoadContext;
            }
            else
            {
                RuntimeTypeHandle declaringTypeHandle;
                MethodNameAndSignature nameAndSignature;
                RuntimeTypeHandle[] genericMethodArgHandles;
                bool success = TypeLoaderEnvironment.Instance.TryGetGenericMethodComponents(context, out declaringTypeHandle, out nameAndSignature, out genericMethodArgHandles);
                Debug.Assert(success);

                DefType declaringType = (DefType)typeSystemContext.ResolveRuntimeTypeHandle(declaringTypeHandle);
                InstantiatedMethod methodContext = (InstantiatedMethod)typeSystemContext.ResolveGenericMethodInstantiation(
                    false, 
                    declaringType, 
                    nameAndSignature, 
                    typeSystemContext.ResolveRuntimeTypeHandles(genericMethodArgHandles), 
                    IntPtr.Zero, 
                    false);

                TypeLoaderLogger.WriteLine("Building floating dictionary layout for method " + methodContext.ToString() + "...");

                // We should only perform updates to floating dictionaries for gemeric methods that share normal canonical code
                Debug.Assert(!methodContext.IsNonSharableMethod);

                uint nativeLayoutInfoToken;
                NativeFormatModuleInfo nativeLayoutModule;
                MethodDesc templateMethod = (new TemplateLocator()).TryGetGenericMethodTemplate(methodContext, out nativeLayoutModule, out nativeLayoutInfoToken);
                if (templateMethod == null)
                    throw new TypeBuilder.MissingTemplateException();

                NativeReader nativeLayoutInfoReader = TypeLoaderEnvironment.Instance.GetNativeLayoutInfoReader(nativeLayoutModule.Handle);

                nativeLayoutParser = new NativeParser(nativeLayoutInfoReader, nativeLayoutInfoToken);
                nlilContext = new NativeLayoutInfoLoadContext
                {
                    _typeSystemContext = methodContext.Context,
                    _typeArgumentHandles = methodContext.OwningType.Instantiation,
                    _methodArgumentHandles = methodContext.Instantiation,
                    _module = nativeLayoutModule
                };
            }

            NativeParser dictionaryLayoutParser = nativeLayoutParser.GetParserForBagElementKind(BagElementKind.DictionaryLayout);
            if (dictionaryLayoutParser.IsNull)
                return IntPtr.Zero;

            int floatingVersionCellIndex, floatingVersionInLayout;
            GenericDictionaryCell[] floatingCells = GenericDictionaryCell.BuildFloatingDictionary(this, nlilContext, dictionaryLayoutParser, out floatingVersionCellIndex, out floatingVersionInLayout);
            if (floatingCells == null)
                return IntPtr.Zero;

            // If the floating section is already constructed, then return. This means we are beaten by another thread.
            if (*((IntPtr*)fixedDictionary) != IntPtr.Zero)
            {
                isNewlyAllocatedDictionary = false;
                return *((IntPtr*)fixedDictionary);
            }

            GenericTypeDictionary floatingDict = new GenericTypeDictionary(floatingCells);

            IntPtr result = floatingDict.Allocate();

            ProcessTypesNeedingPreparation();

            FinishTypeAndMethodBuilding();

            floatingDict.Finish(this);

            return result;
        }

        public static bool TryBuildGenericType(RuntimeTypeHandle genericTypeDefinitionHandle, RuntimeTypeHandle[] genericTypeArgumentHandles, out RuntimeTypeHandle runtimeTypeHandle)
        {
            Debug.Assert(!genericTypeDefinitionHandle.IsNull() && genericTypeArgumentHandles != null && genericTypeArgumentHandles.Length > 0);

            try
            {
                TypeSystemContext context = TypeSystemContextFactory.Create();

                DefType genericDef = (DefType)context.ResolveRuntimeTypeHandle(genericTypeDefinitionHandle);
                Instantiation genericArgs = context.ResolveRuntimeTypeHandles(genericTypeArgumentHandles);
                DefType typeBeingLoaded = context.ResolveGenericInstantiation(genericDef, genericArgs);

                new TypeBuilder().BuildType(typeBeingLoaded);

                runtimeTypeHandle = typeBeingLoaded.RuntimeTypeHandle;
                Debug.Assert(!runtimeTypeHandle.IsNull());

                // Recycle the context only if we succesfully built the type. The state may be partially initialized otherwise.
                TypeSystemContextFactory.Recycle(context);

                return true;
            }
            catch (MissingTemplateException)
            {
                runtimeTypeHandle = default(RuntimeTypeHandle);
                return false;
            }
        }

        public static bool TryBuildArrayType(RuntimeTypeHandle elementTypeHandle, bool isMdArray, int rank, out RuntimeTypeHandle arrayTypeHandle)
        {
            try
            {
                TypeSystemContext context = TypeSystemContextFactory.Create();

                TypeDesc elementType = context.ResolveRuntimeTypeHandle(elementTypeHandle);
                ArrayType arrayType = (ArrayType)context.GetArrayType(elementType, !isMdArray ? -1 : rank);

                new TypeBuilder().BuildType(arrayType);

                arrayTypeHandle = arrayType.RuntimeTypeHandle;
                Debug.Assert(!arrayTypeHandle.IsNull());

                // Recycle the context only if we succesfully built the type. The state may be partially initialized otherwise.
                TypeSystemContextFactory.Recycle(context);

                return true;
            }
            catch (MissingTemplateException)
            {
                arrayTypeHandle = default(RuntimeTypeHandle);
                return false;
            }
        }

        public static bool TryBuildPointerType(RuntimeTypeHandle pointeeTypeHandle, out RuntimeTypeHandle pointerTypeHandle)
        {
            if (!TypeSystemContext.PointerTypesCache.TryGetValue(pointeeTypeHandle, out pointerTypeHandle))
            {
                TypeSystemContext context = TypeSystemContextFactory.Create();
                TypeDesc pointerType = context.GetPointerType(context.ResolveRuntimeTypeHandle(pointeeTypeHandle));
                pointerTypeHandle = EETypeCreator.CreatePointerEEType((uint)pointerType.GetHashCode(), pointeeTypeHandle, pointerType);
                unsafe
                {
                    Debug.Assert(pointerTypeHandle.ToEETypePtr()->IsPointerType);
                }
                TypeSystemContext.PointerTypesCache.AddOrGetExisting(pointerTypeHandle);

                // Recycle the context only if we succesfully built the type. The state may be partially initialized otherwise.
                TypeSystemContextFactory.Recycle(context);
            }

            return true;
        }

        public static bool TryBuildByRefType(RuntimeTypeHandle pointeeTypeHandle, out RuntimeTypeHandle byRefTypeHandle)
        {
            if (!TypeSystemContext.ByRefTypesCache.TryGetValue(pointeeTypeHandle, out byRefTypeHandle))
            {
                TypeSystemContext context = TypeSystemContextFactory.Create();
                TypeDesc byRefType = context.GetByRefType(context.ResolveRuntimeTypeHandle(pointeeTypeHandle));
                byRefTypeHandle = EETypeCreator.CreateByRefEEType((uint)byRefType.GetHashCode(), pointeeTypeHandle, byRefType);
                unsafe
                {
                    Debug.Assert(byRefTypeHandle.ToEETypePtr()->IsByRefType);
                }
                TypeSystemContext.ByRefTypesCache.AddOrGetExisting(byRefTypeHandle);

                // Recycle the context only if we succesfully built the type. The state may be partially initialized otherwise.
                TypeSystemContextFactory.Recycle(context);
            }

            return true;
        }

        public static bool TryBuildGenericMethod(RuntimeTypeHandle declaringTypeHandle, RuntimeTypeHandle[] genericMethodArgHandles, MethodNameAndSignature methodNameAndSignature, out IntPtr methodDictionary)
        {
            TypeSystemContext context = TypeSystemContextFactory.Create();

            DefType declaringType = (DefType)context.ResolveRuntimeTypeHandle(declaringTypeHandle);
            InstantiatedMethod methodBeingLoaded = (InstantiatedMethod)context.ResolveGenericMethodInstantiation(false, declaringType, methodNameAndSignature, context.ResolveRuntimeTypeHandles(genericMethodArgHandles), IntPtr.Zero, false);

            bool success = TryBuildGenericMethod(methodBeingLoaded, out methodDictionary);

            // Recycle the context only if we succesfully built the method. The state may be partially initialized otherwise.
            if (success)
                TypeSystemContextFactory.Recycle(context);

            return success;
        }

        internal static bool TryBuildGenericMethod(InstantiatedMethod methodBeingLoaded, out IntPtr methodDictionary)
        {
            try
            {
                new TypeBuilder().BuildMethod(methodBeingLoaded);

                methodDictionary = methodBeingLoaded.RuntimeMethodDictionary;
                Debug.Assert(methodDictionary != IntPtr.Zero);

                return true;
            }
            catch (MissingTemplateException)
            {
                methodDictionary = IntPtr.Zero;
                return false;
            }
        }

        private void ResolveSingleCell_Worker(GenericDictionaryCell cell, out IntPtr fixupResolution)
        {
            cell.Prepare(this);

            // Process the pending types
            ProcessTypesNeedingPreparation();
            FinishTypeAndMethodBuilding();

            // At this stage the pointer we need is accessible via a call to Create on the prepared cell
            fixupResolution = cell.Create(this);
        }

        private void ResolveMultipleCells_Worker(GenericDictionaryCell[] cells, out IntPtr[] fixups)
        {
            foreach (var cell in cells)
            {
                cell.Prepare(this);
            }

            // Process the pending types
            ProcessTypesNeedingPreparation();
            FinishTypeAndMethodBuilding();

            // At this stage the pointer we need is accessible via a call to Create on the prepared cell
            fixups = new IntPtr[cells.Length];
            for (int i = 0; i < fixups.Length; i++)
                fixups[i] = cells[i].Create(this);
        }

#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
        private void ResolveSingleMetadataFixup(NativeFormatMetadataUnit module, Handle token, MetadataFixupKind fixupKind, out IntPtr fixupResolution)
        {
            FixupCellMetadataResolver metadata = new FixupCellMetadataResolver(module);

            // Allocate a cell object to represent the fixup, and prepare it
            GenericDictionaryCell cell = GenericDictionaryCell.CreateCellFromFixupKindAndToken(fixupKind, metadata, token, default(Handle));
            ResolveSingleCell_Worker(cell, out fixupResolution);
        }

        public static bool TryResolveSingleMetadataFixup(NativeFormatModuleInfo module, int metadataToken, MetadataFixupKind fixupKind, out IntPtr fixupResolution)
        {
            TypeSystemContext context = TypeSystemContextFactory.Create();

            NativeFormatMetadataUnit metadataUnit = context.ResolveMetadataUnit(module);
            new TypeBuilder().ResolveSingleMetadataFixup(metadataUnit, metadataToken.AsHandle(), fixupKind, out fixupResolution);

            TypeSystemContextFactory.Recycle(context);

            return true;
        }

        public static void ResolveSingleTypeDefinition(QTypeDefinition qTypeDefinition, out IntPtr typeHandle)
        {
            TypeSystemContext context = TypeSystemContextFactory.Create();

            TypeDesc type = context.GetTypeDescFromQHandle(qTypeDefinition);
            GenericDictionaryCell cell = GenericDictionaryCell.CreateTypeHandleCell(type);

            new TypeBuilder().ResolveSingleCell_Worker(cell, out typeHandle);

            TypeSystemContextFactory.Recycle(context);
        }
#endif

        internal static void ResolveSingleCell(GenericDictionaryCell cell, out IntPtr fixupResolution)
        {
            new TypeBuilder().ResolveSingleCell_Worker(cell, out fixupResolution);
        }

        public static void ResolveMultipleCells(GenericDictionaryCell [] cells, out IntPtr[] fixups)
        {
            new TypeBuilder().ResolveMultipleCells_Worker(cells, out fixups);
        }

        public static IntPtr BuildGenericLookupTarget(IntPtr typeContext, IntPtr signature, out IntPtr auxResult)
        {
            try
            {
                TypeSystemContext context = TypeSystemContextFactory.Create();

                IntPtr ret = new TypeBuilder().BuildGenericLookupTarget(context, typeContext, signature, out auxResult);

                TypeSystemContextFactory.Recycle(context);

                return ret;
            }
            catch (MissingTemplateException e)
            {
                // This should not ever happen. The static compiler should ensure that the templates are always
                // available for types and methods referenced by lazy dictionary lookups
                Environment.FailFast("MissingTemplateException thrown during lazy generic lookup", e);

                auxResult = IntPtr.Zero;
                return IntPtr.Zero;
            }
        }

        public static bool TryGetFieldOffset(RuntimeTypeHandle declaringTypeHandle, uint fieldOrdinal, out int fieldOffset)
        {
            try
            {
                TypeSystemContext context = TypeSystemContextFactory.Create();

                DefType declaringType = (DefType)context.ResolveRuntimeTypeHandle(declaringTypeHandle);
                Debug.Assert(declaringType.HasInstantiation);

                bool success = new TypeBuilder().TryComputeFieldOffset(declaringType, fieldOrdinal, out fieldOffset);

                TypeSystemContextFactory.Recycle(context);

                return success;
            }
            catch (MissingTemplateException)
            {
                fieldOffset = int.MinValue;
                return false;
            }
        }

        internal static bool TryGetDelegateInvokeMethodSignature(RuntimeTypeHandle delegateTypeHandle, out RuntimeSignature signature)
        {
            signature = default(RuntimeSignature);
            bool success = false;

            TypeSystemContext context = TypeSystemContextFactory.Create();

            DefType delegateType = (DefType)context.ResolveRuntimeTypeHandle(delegateTypeHandle);
            Debug.Assert(delegateType.HasInstantiation);

            NativeLayoutInfoLoadContext loadContext;
            NativeLayoutInfo universalLayoutInfo;
            NativeParser parser = delegateType.GetOrCreateTypeBuilderState().GetParserForUniversalNativeLayoutInfo(out loadContext, out universalLayoutInfo);
            if (!parser.IsNull)
            {
                NativeParser sigParser = parser.GetParserForBagElementKind(BagElementKind.DelegateInvokeSignature);
                if (!sigParser.IsNull)
                {
                    signature = RuntimeSignature.CreateFromNativeLayoutSignature(universalLayoutInfo.Module.Handle, sigParser.Offset);
                    success = true;
                }
            }

            TypeSystemContextFactory.Recycle(context);

            return success;
        }

        //
        // This method is used to build the floating portion of a generic dictionary.
        // 
        internal static IntPtr TryBuildFloatingDictionary(IntPtr context, bool isTypeContext, IntPtr fixedDictionary, out bool isNewlyAllocatedDictionary)
        {
            isNewlyAllocatedDictionary = true;

            try
            {
                TypeSystemContext typeSystemContext = TypeSystemContextFactory.Create();

                IntPtr ret = new TypeBuilder().BuildFloatingDictionary(typeSystemContext, context, isTypeContext, fixedDictionary, out isNewlyAllocatedDictionary);

                TypeSystemContextFactory.Recycle(typeSystemContext);

                return ret;
            }
            catch (MissingTemplateException e)
            {
                // This should not ever happen. The static compiler should ensure that the templates are always
                // available for types and methods that have floating dictionaries
                Environment.FailFast("MissingTemplateException thrown during dictionary update", e);

                return IntPtr.Zero;
            }
        }
    }
}