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

TypeBuilderState.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: b83174f60cda047cbdda6494438be4428d0d41b6 (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
// 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.Text;

using Internal.Runtime;
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
{
    internal struct NativeLayoutInfo
    {
        public uint Offset;
        public NativeFormatModuleInfo Module;
        public NativeReader Reader;
        public NativeLayoutInfoLoadContext LoadContext;
    }

    //
    // TypeBuilder per-Type state. It is attached to each TypeDesc that gets involved in type building.
    //
    internal class TypeBuilderState
    {
        internal class VTableLayoutInfo
        {
            public uint VTableSlot;
            public RuntimeSignature MethodSignature;
            public bool IsSealedVTableSlot;
        }

        internal class VTableSlotMapper
        {
            private int[] _slotMap;
            private IntPtr[] _dictionarySlots;
            private int _numMappingsAssigned;

            public VTableSlotMapper(int numVtableSlotsInTemplateType)
            {
                _slotMap = new int[numVtableSlotsInTemplateType];
                _dictionarySlots = new IntPtr[numVtableSlotsInTemplateType];
                _numMappingsAssigned = 0;
                for (int i = 0; i < numVtableSlotsInTemplateType; i++)
                {
                    _slotMap[i] = -1;
                    _dictionarySlots[i] = IntPtr.Zero;
                }
            }
            public void AddMapping(int vtableSlotInTemplateType, int vtableSlotInTargetType, IntPtr dictionaryValueInSlot)
            {
                Debug.Assert(_numMappingsAssigned < _slotMap.Length);
                _slotMap[vtableSlotInTemplateType] = vtableSlotInTargetType;
                _dictionarySlots[vtableSlotInTemplateType] = dictionaryValueInSlot;
                _numMappingsAssigned++;
            }
            public int GetVTableSlotInTargetType(int vtableSlotInTemplateType)
            {
                Debug.Assert(vtableSlotInTemplateType < _slotMap.Length);
                return _slotMap[vtableSlotInTemplateType];
            }
            public bool IsDictionarySlot(int vtableSlotInTemplateType, out IntPtr dictionaryPtrValue)
            {
                Debug.Assert(vtableSlotInTemplateType < _dictionarySlots.Length);
                dictionaryPtrValue = _dictionarySlots[vtableSlotInTemplateType];
                return _dictionarySlots[vtableSlotInTemplateType] != IntPtr.Zero;
            }
            public int NumSlotMappings
            {
                get { return _numMappingsAssigned; }
            }
        }

        public TypeBuilderState(TypeDesc typeBeingBuilt)
        {
            TypeBeingBuilt = typeBeingBuilt;
        }

        public readonly TypeDesc TypeBeingBuilt;

        //
        // We cache and try to reuse the most recently used TypeSystemContext. The TypeSystemContext is used by not just type builder itself,
        // but also in several random other places in reflection. There can be multiple ResolutionContexts in flight at any given moment.
        // This check ensures that the RuntimeTypeHandle cache in the current resolution context is refreshed in case there were new
        // types built using a different TypeSystemContext in the meantime.
        // NOTE: For correctness, this value must be recomputed every time the context is recycled. This requires flushing the TypeBuilderState
        // from each type that has one if context is recycled.
        //
        public bool AttemptedAndFailedToRetrieveTypeHandle = false;

        public bool NeedsTypeHandle;
        public bool HasBeenPrepared;

        public RuntimeTypeHandle HalfBakedRuntimeTypeHandle;
        public IntPtr HalfBakedDictionary;
        public IntPtr HalfBakedSealedVTable;

        private bool _templateComputed;
        private bool _nativeLayoutTokenComputed;
        private TypeDesc _templateType;

        public TypeDesc TemplateType
        {
            get
            {
                if (!_templateComputed)
                {
                    // Multidimensional arrays and szarrays of pointers don't implement generic interfaces and are special cases. They use
                    // typeof(object[,]) as their template.
                    if (TypeBeingBuilt.IsMdArray || (TypeBeingBuilt.IsSzArray && ((ArrayType)TypeBeingBuilt).ElementType.IsPointer))
                    {
                        _templateType = TypeBeingBuilt.Context.ResolveRuntimeTypeHandle(typeof(object[,]).TypeHandle);
                        _templateTypeLoaderNativeLayout = false;
                        _nativeLayoutComputed = _nativeLayoutTokenComputed = _templateComputed = true;

                        return _templateType;
                    }

                    // Locate the template type and native layout info
                    _templateType = TypeBeingBuilt.Context.TemplateLookup.TryGetTypeTemplate(TypeBeingBuilt, ref _nativeLayoutInfo);
                    Debug.Assert(_templateType == null || !_templateType.RuntimeTypeHandle.IsNull());

                    _templateTypeLoaderNativeLayout = true;
                    _templateComputed = true;
                    if ((_templateType != null) && !_templateType.IsCanonicalSubtype(CanonicalFormKind.Universal))
                        _nativeLayoutTokenComputed = true;
                }

                return _templateType;
            }
        }

        private bool _nativeLayoutComputed = false;
        private bool _templateTypeLoaderNativeLayout = false;
        private bool _readyToRunNativeLayout = false;

        private NativeLayoutInfo _nativeLayoutInfo;
        private NativeLayoutInfo _r2rnativeLayoutInfo;

        private void EnsureNativeLayoutInfoComputed()
        {
            if (!_nativeLayoutComputed)
            {
                if (!_nativeLayoutTokenComputed)
                {
                    if (!_templateComputed)
                    {
                        // Attempt to compute native layout through as a non-ReadyToRun template
                        object temp = this.TemplateType;
                    }
                    if (!_nativeLayoutTokenComputed)
                    {
                        TypeBeingBuilt.Context.TemplateLookup.TryGetMetadataNativeLayout(TypeBeingBuilt, out _r2rnativeLayoutInfo.Module, out _r2rnativeLayoutInfo.Offset);

                        if (_r2rnativeLayoutInfo.Module != null)
                            _readyToRunNativeLayout = true;
                    }
                    _nativeLayoutTokenComputed = true;
                }

                if (_nativeLayoutInfo.Module != null)
                {
                    FinishInitNativeLayoutInfo(TypeBeingBuilt, ref _nativeLayoutInfo);
                }

                if (_r2rnativeLayoutInfo.Module != null)
                {
                    FinishInitNativeLayoutInfo(TypeBeingBuilt, ref _r2rnativeLayoutInfo);
                }

                _nativeLayoutComputed = true;
            }
        }

        /// <summary>
        /// Initialize the Reader and LoadContext fields of the native layout info
        /// </summary>
        /// <param name="type"></param>
        /// <param name="nativeLayoutInfo"></param>
        private static void FinishInitNativeLayoutInfo(TypeDesc type, ref NativeLayoutInfo nativeLayoutInfo)
        {
            var nativeLayoutInfoLoadContext = new NativeLayoutInfoLoadContext();

            nativeLayoutInfoLoadContext._typeSystemContext = type.Context;
            nativeLayoutInfoLoadContext._module = nativeLayoutInfo.Module;

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

            nativeLayoutInfoLoadContext._methodArgumentHandles = new Instantiation(null);

            nativeLayoutInfo.Reader = TypeLoaderEnvironment.Instance.GetNativeLayoutInfoReader(nativeLayoutInfo.Module.Handle);
            nativeLayoutInfo.LoadContext = nativeLayoutInfoLoadContext;
        }

        public NativeLayoutInfo NativeLayoutInfo
        {
            get
            {
                EnsureNativeLayoutInfoComputed();
                return _nativeLayoutInfo;
            }
        }

        public NativeLayoutInfo R2RNativeLayoutInfo
        {
            get
            {
                EnsureNativeLayoutInfoComputed();
                return _r2rnativeLayoutInfo;
            }
        }

        public NativeParser GetParserForNativeLayoutInfo()
        {
            EnsureNativeLayoutInfoComputed();
            if (_templateTypeLoaderNativeLayout)
                return new NativeParser(_nativeLayoutInfo.Reader, _nativeLayoutInfo.Offset);
            else
                return default(NativeParser);
        }

        public NativeParser GetParserForReadyToRunNativeLayoutInfo()
        {
            EnsureNativeLayoutInfoComputed();
            if (_readyToRunNativeLayout)
                return new NativeParser(_r2rnativeLayoutInfo.Reader, _r2rnativeLayoutInfo.Offset);
            else
                return default(NativeParser);
        }

        public NativeParser GetParserForUniversalNativeLayoutInfo(out NativeLayoutInfoLoadContext universalLayoutLoadContext, out NativeLayoutInfo universalLayoutInfo)
        {
            universalLayoutInfo = new NativeLayoutInfo();
            universalLayoutLoadContext = null;
            TypeDesc universalTemplate = TypeBeingBuilt.Context.TemplateLookup.TryGetUniversalTypeTemplate(TypeBeingBuilt, ref universalLayoutInfo);
            if (universalTemplate == null)
                return new NativeParser();

            FinishInitNativeLayoutInfo(TypeBeingBuilt, ref universalLayoutInfo);
            universalLayoutLoadContext = universalLayoutInfo.LoadContext;
            return new NativeParser(universalLayoutInfo.Reader, universalLayoutInfo.Offset);
        }

        // RuntimeInterfaces is the full list of interfaces that the type implements. It can include private internal implementation 
        // detail interfaces that nothing is known about.
        public DefType[] RuntimeInterfaces
        {
            get
            {
                // Generic Type Definitions have no runtime interfaces
                if (TypeBeingBuilt.IsGenericDefinition)
                    return null;

                return TypeBeingBuilt.RuntimeInterfaces;
            }
        }

        private bool? _hasDictionarySlotInVTable;
        private bool ComputeHasDictionarySlotInVTable()
        {
            if (!TypeBeingBuilt.IsGeneric() && !(TypeBeingBuilt is ArrayType))
                return false;

            // Generic interfaces always have a dictionary slot
            if (TypeBeingBuilt.IsInterface)
                return true;

            return TypeBeingBuilt.CanShareNormalGenericCode();
        }

        public bool HasDictionarySlotInVTable
        {
            get
            {
                if (_hasDictionarySlotInVTable == null)
                {
                    _hasDictionarySlotInVTable = ComputeHasDictionarySlotInVTable();
                }
                return _hasDictionarySlotInVTable.Value;
            }
        }

        private bool? _hasDictionaryInVTable;
        private bool ComputeHasDictionaryInVTable()
        {
            if (!HasDictionarySlotInVTable)
                return false;

            if (TypeBeingBuilt.RetrieveRuntimeTypeHandleIfPossible())
            {
                // Type was already constructed
                return TypeBeingBuilt.RuntimeTypeHandle.GetDictionary() != IntPtr.Zero;
            }
            else
            {
                // Type is being newly constructed
                if (TemplateType != null)
                {
                    NativeParser parser = GetParserForNativeLayoutInfo();
                    // Template type loader case
#if GENERICS_FORCE_USG
                    bool isTemplateUniversalCanon = state.TemplateType.IsCanonicalSubtype(CanonicalFormKind.UniversalCanonLookup);
                    if (isTemplateUniversalCanon && type.CanShareNormalGenericCode())
                    {
                        TypeBuilderState tempState = new TypeBuilderState();
                        tempState.NativeLayoutInfo = new NativeLayoutInfo();
                        tempState.TemplateType = type.Context.TemplateLookup.TryGetNonUniversalTypeTemplate(type, ref tempState.NativeLayoutInfo);
                        if (tempState.TemplateType != null)
                        {
                            Debug.Assert(!tempState.TemplateType.IsCanonicalSubtype(CanonicalFormKind.UniversalCanonLookup));
                            parser = GetNativeLayoutInfoParser(type, ref tempState.NativeLayoutInfo);
                        }
                    }
#endif
                    var dictionaryLayoutParser = parser.GetParserForBagElementKind(BagElementKind.DictionaryLayout);

                    return !dictionaryLayoutParser.IsNull;
                }
                else
                {
                    NativeParser parser = GetParserForReadyToRunNativeLayoutInfo();
                    // ReadyToRun case
                    // Dictionary is directly encoded instead of the NativeLayout being a collection of bags
                    if (parser.IsNull)
                        return false;

                    // First unsigned value in the native layout is the number of dictionary entries
                    return parser.GetUnsigned() != 0;
                }
            }
        }

        public bool HasDictionaryInVTable
        {
            get
            {
                if (_hasDictionaryInVTable == null)
                    _hasDictionaryInVTable = ComputeHasDictionaryInVTable();
                return _hasDictionaryInVTable.Value;
            }
        }

        private ushort? _numVTableSlots;
        private ushort ComputeNumVTableSlots()
        {
            if (TypeBeingBuilt.RetrieveRuntimeTypeHandleIfPossible())
            {
                unsafe
                {
                    return TypeBeingBuilt.RuntimeTypeHandle.ToEETypePtr()->NumVtableSlots;
                }
            }
            else
            {
                TypeDesc templateType = TypeBeingBuilt.ComputeTemplate(false);
                if (templateType != null)
                {
                    // Template type loader case
                    if (VTableSlotsMapping != null)
                    {
                        return checked((ushort)VTableSlotsMapping.NumSlotMappings);
                    }
                    else
                    {
                        unsafe
                        {
                            if (TypeBeingBuilt.IsMdArray || (TypeBeingBuilt.IsSzArray && ((ArrayType)TypeBeingBuilt).ElementType.IsPointer))
                            {
                                // MDArray types and pointer arrays have the same vtable as the System.Array type they "derive" from.
                                // They do not implement the generic interfaces that make this interesting for normal arrays.
                                return TypeBeingBuilt.BaseType.GetRuntimeTypeHandle().ToEETypePtr()->NumVtableSlots;
                            }
                            else
                            {
                                // This should only happen for non-universal templates
                                Debug.Assert(TypeBeingBuilt.IsTemplateCanonical());

                                // Canonical template type loader case
                                return templateType.GetRuntimeTypeHandle().ToEETypePtr()->NumVtableSlots;
                            }
                        }
                    }
                }
                else
                {
                    // Metadata based type loading.

                    // Generic Type Definitions have no actual vtable entries
                    if (TypeBeingBuilt.IsGenericDefinition)
                        return 0;

                    // We have at least as many slots as exist on the base type.

                    ushort numVTableSlots = 0;
                    checked
                    {
                        if (TypeBeingBuilt.BaseType != null)
                        {
                            numVTableSlots = TypeBeingBuilt.BaseType.GetOrCreateTypeBuilderState().NumVTableSlots;
                        }
                        else
                        {
                            // Generic interfaces have a dictionary slot
                            if (TypeBeingBuilt.IsInterface && TypeBeingBuilt.HasInstantiation)
                                numVTableSlots = 1;
                        }

                        // Interfaces have actual vtable slots
                        if (TypeBeingBuilt.IsInterface)
                            return numVTableSlots;

                        foreach (MethodDesc method in TypeBeingBuilt.GetMethods())
                        {
#if SUPPORTS_NATIVE_METADATA_TYPE_LOADING
                            if (LazyVTableResolver.MethodDefinesVTableSlot(method))
                                numVTableSlots++;
#else
                            Environment.FailFast("metadata type loader required");
#endif
                        }

                        if (HasDictionarySlotInVTable)
                            numVTableSlots++;
                    }

                    return numVTableSlots;
                }
            }
        }

        public ushort NumVTableSlots
        {
            get
            {
                if (_numVTableSlots == null)
                    _numVTableSlots = ComputeNumVTableSlots();

                return _numVTableSlots.Value;
            }
        }


        public GenericTypeDictionary Dictionary;

        public int NonGcDataSize
        {
            get
            {
                DefType defType = TypeBeingBuilt as DefType;

                // The NonGCStatic fields hold the class constructor data if it exists in the negative space
                // of the memory region. The ClassConstructorOffset is negative, so it must be negated to 
                // determine the extra space that is used.

                if (defType != null)
                {
                    return defType.NonGCStaticFieldSize.AsInt - (HasStaticConstructor ? TypeBuilder.ClassConstructorOffset : 0);
                }
                else
                {
                    return -(HasStaticConstructor ? TypeBuilder.ClassConstructorOffset : 0);
                }
            }
        }

        public int GcDataSize
        {
            get
            {
                DefType defType = TypeBeingBuilt as DefType;
                if (defType != null)
                {
                    return defType.GCStaticFieldSize.AsInt;
                }
                else
                {
                    return 0;
                }
            }
        }

        public int ThreadDataSize
        {
            get
            {
                DefType defType = TypeBeingBuilt as DefType;
                if (defType != null && !defType.IsGenericDefinition)
                {
                    return defType.ThreadGcStaticFieldSize.AsInt;
                }
                else
                {
                    // Non-DefType's and GenericEETypeDefinitions do not have static fields of any form
                    return 0;
                }
            }
        }

        public bool HasStaticConstructor
        {
            get { return TypeBeingBuilt.HasStaticConstructor; }
        }

        public IntPtr? ClassConstructorPointer;
        public IntPtr GcStaticDesc;
        public IntPtr GcStaticEEType;
        public IntPtr ThreadStaticDesc;
        public bool AllocatedStaticGCDesc;
        public bool AllocatedThreadStaticGCDesc;
        public uint ThreadStaticOffset;
        public uint NumSealedVTableEntries;
        public int[] GenericVarianceFlags;

        // Sentinel static to allow us to initialize _instanceLayout to something
        // and then detect that InstanceGCLayout should return null
        private static LowLevelList<bool> s_emptyLayout = new LowLevelList<bool>();

        private LowLevelList<bool> _instanceGCLayout;

        /// <summary>
        /// The instance gc layout of a dynamically laid out type.
        /// null if one of the following is true
        ///     1) For an array type:
        ///         - the type is a reference array
        ///     2) For a generic type:
        ///         - the type has no GC instance fields
        ///         - the type already has a type handle
        ///         - the type has a non-universal canonical template
        ///         - the type has already been constructed
        ///    
        /// If the type is a valuetype array, this is the layout of the valuetype held in the array if the type has GC reference fields
        /// Otherwise, it is the layout of the fields in the type.
        /// </summary>
        public LowLevelList<bool> InstanceGCLayout
        {
            get
            {
                if (_instanceGCLayout == null)
                {
                    LowLevelList<bool> instanceGCLayout = null;

                    if (TypeBeingBuilt is ArrayType)
                    {
                        if (!IsArrayOfReferenceTypes)
                        {
                            ArrayType arrayType = (ArrayType)TypeBeingBuilt;
                            TypeBuilder.GCLayout elementGcLayout = GetFieldGCLayout(arrayType.ElementType);
                            if (!elementGcLayout.IsNone)
                            {
                                instanceGCLayout = new LowLevelList<bool>();
                                elementGcLayout.WriteToBitfield(instanceGCLayout, 0);
                                _instanceGCLayout = instanceGCLayout;
                            }
                        }
                        else
                        {
                            // Array of reference type returns null
                            _instanceGCLayout = s_emptyLayout;
                        }
                    }
                    else if (TypeBeingBuilt.RetrieveRuntimeTypeHandleIfPossible() ||
                             TypeBeingBuilt.IsTemplateCanonical() ||
                             (TypeBeingBuilt is PointerType) ||
                             (TypeBeingBuilt is ByRefType))
                    {
                        _instanceGCLayout = s_emptyLayout;
                    }
                    else
                    {
                        // Generic Type Definitions have no gc layout
                        if (!(TypeBeingBuilt.IsGenericDefinition))
                        {
                            // Copy in from base type
                            if (!TypeBeingBuilt.IsValueType && (TypeBeingBuilt.BaseType != null))
                            {
                                DefType baseType = TypeBeingBuilt.BaseType;

                                // Capture the gc layout from the base type
                                TypeBuilder.GCLayout baseTypeLayout = GetInstanceGCLayout(baseType);
                                if (!baseTypeLayout.IsNone)
                                {
                                    instanceGCLayout = new LowLevelList<bool>();
                                    baseTypeLayout.WriteToBitfield(instanceGCLayout, IntPtr.Size /* account for the EEType pointer */);
                                }
                            }

                            foreach (FieldDesc field in GetFieldsForGCLayout())
                            {
                                if (field.IsStatic)
                                    continue;

                                if (field.IsLiteral)
                                    continue;

                                TypeBuilder.GCLayout fieldGcLayout = GetFieldGCLayout(field.FieldType);
                                if (!fieldGcLayout.IsNone)
                                {
                                    if (instanceGCLayout == null)
                                        instanceGCLayout = new LowLevelList<bool>();

                                    fieldGcLayout.WriteToBitfield(instanceGCLayout, field.Offset.AsInt);
                                }
                            }

                            if ((instanceGCLayout != null) && instanceGCLayout.HasSetBits())
                            {
                                // When bits are set in the instance GC layout, it implies that the type contains GC refs,
                                // which implies that the type size is pointer-aligned.  In this case consumers assume that
                                // the type size can be computed by multiplying the bitfield size by the pointer size.  If
                                // necessary, expand the bitfield to ensure that this invariant holds.

                                // Valuetypes with gc fields must be aligned on at least pointer boundaries
                                Debug.Assert(!TypeBeingBuilt.IsValueType || (FieldAlignment.Value >= TypeBeingBuilt.Context.Target.PointerSize));
                                // Valuetypes with gc fields must have a type size which is aligned on an IntPtr boundary.
                                Debug.Assert(!TypeBeingBuilt.IsValueType || ((TypeSize.Value & (IntPtr.Size - 1)) == 0));

                                int impliedBitCount = (TypeSize.Value + IntPtr.Size - 1) / IntPtr.Size;
                                Debug.Assert(instanceGCLayout.Count <= impliedBitCount);
                                instanceGCLayout.Expand(impliedBitCount);
                                Debug.Assert(instanceGCLayout.Count == impliedBitCount);
                            }
                        }

                        if (instanceGCLayout == null)
                            _instanceGCLayout = s_emptyLayout;
                        else
                            _instanceGCLayout = instanceGCLayout;
                    }
                }

                if (_instanceGCLayout == s_emptyLayout)
                    return null;
                else
                    return _instanceGCLayout;
            }
        }


        public LowLevelList<bool> StaticGCLayout;
        public LowLevelList<bool> ThreadStaticGCLayout;

        private bool _staticGCLayoutPrepared;

        /// <summary>
        /// Prepare the StaticGCLayout/ThreadStaticGCLayout/GcStaticDesc/ThreadStaticDesc fields by
        /// reading native layout or metadata as appropriate. This method should only be called for types which
        /// are actually to be created.
        /// </summary>
        public void PrepareStaticGCLayout()
        {
            if (!_staticGCLayoutPrepared)
            {
                _staticGCLayoutPrepared = true;
                DefType defType = TypeBeingBuilt as DefType;

                if (defType == null)
                {
                    // Array/pointer types do not have static fields
                }
                else if (defType.IsTemplateCanonical())
                {
                    // Canonical templates get their layout directly from the NativeLayoutInfo. 
                    // Parse it and pull that info out here.

                    NativeParser typeInfoParser = GetParserForNativeLayoutInfo();

                    BagElementKind kind;
                    while ((kind = typeInfoParser.GetBagElementKind()) != BagElementKind.End)
                    {
                        switch (kind)
                        {
                            case BagElementKind.GcStaticDesc:
                                GcStaticDesc = NativeLayoutInfo.LoadContext.GetGCStaticInfo(typeInfoParser.GetUnsigned());
                                break;

                            case BagElementKind.ThreadStaticDesc:
                                ThreadStaticDesc = NativeLayoutInfo.LoadContext.GetGCStaticInfo(typeInfoParser.GetUnsigned());
                                break;

                            case BagElementKind.GcStaticEEType:
                                GcStaticEEType = NativeLayoutInfo.LoadContext.GetGCStaticInfo(typeInfoParser.GetUnsigned());
                                break;

                            default:
                                typeInfoParser.SkipInteger();
                                break;
                        }
                    }
                }
                else
                {
                    // Compute GC layout boolean array from field information.
                    IEnumerable<FieldDesc> fields = GetFieldsForGCLayout();
                    LowLevelList<bool> threadStaticLayout = null;
                    LowLevelList<bool> gcStaticLayout = null;

                    foreach (FieldDesc field in fields)
                    {
                        if (!field.IsStatic)
                            continue;

                        if (field.IsLiteral)
                            continue;

                        LowLevelList<bool> gcLayoutInfo = null;
                        if (field.IsThreadStatic)
                        {
                            if (threadStaticLayout == null)
                                threadStaticLayout = new LowLevelList<bool>();
                            gcLayoutInfo = threadStaticLayout;
                        }
                        else if (field.HasGCStaticBase)
                        {
                            if (gcStaticLayout == null)
                                gcStaticLayout = new LowLevelList<bool>();
                            gcLayoutInfo = gcStaticLayout;
                        }
                        else
                        {
                            // Non-GC static  no need to record information
                            continue;
                        }

                        TypeBuilder.GCLayout fieldGcLayout = GetFieldGCLayout(field.FieldType);
                        fieldGcLayout.WriteToBitfield(gcLayoutInfo, field.Offset.AsInt);
                    }

                    if (gcStaticLayout != null && gcStaticLayout.Count > 0)
                        StaticGCLayout = gcStaticLayout;

                    if (threadStaticLayout != null && threadStaticLayout.Count > 0)
                        ThreadStaticGCLayout = threadStaticLayout;
                }
            }
        }

        /// <summary>
        /// Get an enumerable list of the fields used for dynamic gc layout calculation.
        /// </summary>
        private IEnumerable<FieldDesc> GetFieldsForGCLayout()
        {
            DefType defType = (DefType)TypeBeingBuilt;

            IEnumerable<FieldDesc> fields;

            if (defType.ComputeTemplate(false) != null)
            {
                // we have native layout and a template. Use the NativeLayoutFields as that is the only complete
                // description of the fields available. (There may be metadata fields, but those aren't guaranteed
                // to be a complete set of fields due to reflection reduction.
                NativeLayoutFieldAlgorithm.EnsureFieldLayoutLoadedForGenericType(defType);
                fields = defType.NativeLayoutFields;
            }
            else
            {
                // The metadata case. We're loading the type from regular metadata, so use the regular metadata fields
                fields = defType.GetFields();
            }

            return fields;
        }

        // Get the GC layout of a type. Handles pre-created, universal template, and non-universal template cases
        // Only to be used for getting the instance layout of non-valuetypes.
        /// <summary>
        /// Get the GC layout of a type. Handles pre-created, universal template, and non-universal template cases
        /// Only to be used for getting the instance layout of non-valuetypes that are used as base types
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private unsafe TypeBuilder.GCLayout GetInstanceGCLayout(TypeDesc type)
        {
            Debug.Assert(!type.IsCanonicalSubtype(CanonicalFormKind.Any));
            Debug.Assert(!type.IsValueType);

            if (type.RetrieveRuntimeTypeHandleIfPossible())
            {
                return new TypeBuilder.GCLayout(type.RuntimeTypeHandle);
            }

            if (type.IsTemplateCanonical())
            {
                var templateType = type.ComputeTemplate();
                bool success = templateType.RetrieveRuntimeTypeHandleIfPossible();
                Debug.Assert(success && !templateType.RuntimeTypeHandle.IsNull());

                return new TypeBuilder.GCLayout(templateType.RuntimeTypeHandle);
            }
            else
            {
                TypeBuilderState state = type.GetOrCreateTypeBuilderState();
                if (state.InstanceGCLayout == null)
                    return TypeBuilder.GCLayout.None;
                else
                    return new TypeBuilder.GCLayout(state.InstanceGCLayout, true);
            }
        }

        /// <summary>
        /// Get the GC layout of a type when used as a field.
        /// NOTE: if the fieldtype is a reference type, this function will return GCLayout.None
        ///       Consumers of the api must handle that special case.
        /// </summary>
        private unsafe TypeBuilder.GCLayout GetFieldGCLayout(TypeDesc fieldType)
        {
            if (!fieldType.IsValueType)
            {
                if (fieldType.IsPointer)
                    return TypeBuilder.GCLayout.None;
                else
                    return TypeBuilder.GCLayout.SingleReference;
            }

            // Is this a type that already exists? If so, get its gclayout from the EEType directly
            if (fieldType.RetrieveRuntimeTypeHandleIfPossible())
            {
                return new TypeBuilder.GCLayout(fieldType.RuntimeTypeHandle);
            }

            // The type of the field must be a valuetype that is dynamically being constructed

            if (fieldType.IsTemplateCanonical())
            {
                // Pull the GC Desc from the canonical instantiation
                TypeDesc templateType = fieldType.ComputeTemplate();
                bool success = templateType.RetrieveRuntimeTypeHandleIfPossible();
                Debug.Assert(success);
                return new TypeBuilder.GCLayout(templateType.RuntimeTypeHandle);
            }
            else
            {
                // Use the type builder state's computed InstanceGCLayout
                var instanceGCLayout = fieldType.GetOrCreateTypeBuilderState().InstanceGCLayout;
                if (instanceGCLayout == null)
                    return TypeBuilder.GCLayout.None;

                return new TypeBuilder.GCLayout(instanceGCLayout, false /* Always represents a valuetype as the reference type case
                                                                           is handled above with the GCLayout.SingleReference return */);
            }
        }


        public bool IsArrayOfReferenceTypes
        {
            get
            {
                ArrayType typeAsArrayType = TypeBeingBuilt as ArrayType;
                if (typeAsArrayType != null)
                    return !typeAsArrayType.ParameterType.IsValueType && !typeAsArrayType.ParameterType.IsPointer;
                else
                    return false;
            }
        }

        // Rank for arrays, -1 is used for an SzArray, and a positive number for a multidimensional array.
        public int? ArrayRank
        {
            get
            {
                if (!TypeBeingBuilt.IsArray)
                    return null;
                else if (TypeBeingBuilt.IsSzArray)
                    return -1;
                else
                {
                    Debug.Assert(TypeBeingBuilt.IsMdArray);
                    return ((ArrayType)TypeBeingBuilt).Rank;
                }
            }
        }

        public int? BaseTypeSize
        {
            get
            {
                if (TypeBeingBuilt.BaseType == null)
                {
                    return null;
                }
                else
                {
                    return TypeBeingBuilt.BaseType.InstanceByteCountUnaligned.AsInt;
                }
            }
        }

        public int? TypeSize
        {
            get
            {
                DefType defType = TypeBeingBuilt as DefType;
                if (defType != null)
                {
                    // Generic Type Definition EETypes do not have size
                    if (defType.IsGenericDefinition)
                        return null;

                    if (defType.IsValueType)
                    {
                        return defType.InstanceFieldSize.AsInt;
                    }
                    else
                    {
                        if (defType.IsInterface)
                            return IntPtr.Size;

                        return defType.InstanceByteCountUnaligned.AsInt;
                    }
                }
                else if (TypeBeingBuilt is ArrayType)
                {
                    int basicArraySize = TypeBeingBuilt.BaseType.InstanceByteCountUnaligned.AsInt;
                    if (TypeBeingBuilt.IsMdArray)
                    {
                        // MD Arrays are arranged like normal arrays, but they also have 2 int's per rank for the individual dimension loBounds and range.
                        basicArraySize += ((ArrayType)TypeBeingBuilt).Rank * sizeof(int) * 2;
                    }
                    return basicArraySize;
                }
                else
                {
                    return null;
                }
            }
        }

        public int? UnalignedTypeSize
        {
            get
            {
                DefType defType = TypeBeingBuilt as DefType;
                if (defType != null)
                {
                    return defType.InstanceByteCountUnaligned.AsInt;
                }
                else if (TypeBeingBuilt is ArrayType)
                {
                    // Arrays use the same algorithm for TypeSize as for UnalignedTypeSize
                    return TypeSize;
                }
                else
                {
                    return 0;
                }
            }
        }

        public int? FieldAlignment
        {
            get
            {
                if (TypeBeingBuilt is DefType)
                {
                    return checked((ushort)((DefType)TypeBeingBuilt).InstanceFieldAlignment.AsInt);
                }
                else if (TypeBeingBuilt is ArrayType)
                {
                    ArrayType arrayType = (ArrayType)TypeBeingBuilt;

                    if (arrayType.ElementType is DefType)
                    {
                        return checked((ushort)((DefType)arrayType.ElementType).InstanceFieldAlignment.AsInt);
                    }
                    else
                    {
                        return (ushort)arrayType.Context.Target.PointerSize;
                    }
                }
                else if (TypeBeingBuilt is PointerType || TypeBeingBuilt is ByRefType)
                {
                    return (ushort)TypeBeingBuilt.Context.Target.PointerSize;
                }
                else
                {
                    return null;
                }
            }
        }

        public ushort? ComponentSize
        {
            get
            {
                ArrayType arrayType = TypeBeingBuilt as ArrayType;
                if (arrayType != null)
                {
                    if (arrayType.ElementType is DefType)
                    {
                        uint size = (uint)((DefType)arrayType.ElementType).InstanceFieldSize.AsInt;

                        if (size > ArrayTypesConstants.MaxSizeForValueClassInArray && arrayType.ElementType.IsValueType)
                            ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadValueClassTooLarge, arrayType.ElementType);

                        return checked((ushort)size);
                    }
                    else
                    {
                        return (ushort)arrayType.Context.Target.PointerSize;
                    }
                }
                else
                {
                    return null;
                }
            }
        }

        public uint NullableValueOffset
        {
            get
            {
                if (!TypeBeingBuilt.IsNullable)
                    return 0;

                if (TypeBeingBuilt.IsTemplateCanonical())
                {
                    // Pull the GC Desc from the canonical instantiation
                    TypeDesc templateType = TypeBeingBuilt.ComputeTemplate();
                    bool success = templateType.RetrieveRuntimeTypeHandleIfPossible();
                    Debug.Assert(success);
                    unsafe
                    {
                        return templateType.RuntimeTypeHandle.ToEETypePtr()->NullableValueOffset;
                    }
                }
                else
                {
                    int fieldCount = 0;
                    uint nullableValueOffset = 0;

                    foreach (FieldDesc f in GetFieldsForGCLayout())
                    {
                        if (fieldCount == 1)
                        {
                            nullableValueOffset = checked((uint)f.Offset.AsInt);
                        }
                        fieldCount++;
                    }

                    // Nullable<T> only has two fields. HasValue and Value
                    Debug.Assert(fieldCount == 2);
                    return nullableValueOffset;
                }
            }
        }

        public bool IsHFA
        {
            get
            {
#if ARM
                if (TypeBeingBuilt is DefType)
                {
                    return ((DefType)TypeBeingBuilt).IsHfa;
                }
                else
                {
                    return false;
                }
#else
                // On Non-ARM platforms, HFA'ness is not encoded in the EEType as it doesn't effect ABI
                return false;
#endif
            }
        }

        public VTableLayoutInfo[] VTableMethodSignatures;
        public int NumSealedVTableMethodSignatures;

        public VTableSlotMapper VTableSlotsMapping;

#if GENERICS_FORCE_USG
        public TypeDesc NonUniversalTemplateType;
        public int NonUniversalInstanceGCDescSize;
        public IntPtr NonUniversalInstanceGCDesc;
        public IntPtr NonUniversalStaticGCDesc;
        public IntPtr NonUniversalThreadStaticGCDesc;
#endif
    }
}