summaryrefslogtreecommitdiff
path: root/src/vm/methodtable.inl
blob: c762512bf1684fd550f205f7c86bc87a56334948 (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
// 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.
//
// File: methodtable.inl
//


//

//
// ============================================================================

#ifndef _METHODTABLE_INL_ 
#define _METHODTABLE_INL_

#include "methodtable.h"
#include "genericdict.h"
#include "threadstatics.h"

//==========================================================================================
inline PTR_EEClass MethodTable::GetClass_NoLogging()
{
    LIMITED_METHOD_DAC_CONTRACT;

#ifdef _DEBUG 
    LowBits lowBits = union_getLowBits(m_pCanonMT);
    if (lowBits == UNION_EECLASS)
    {
        return PTR_EEClass(m_pCanonMT);
    }
    else if (lowBits == UNION_METHODTABLE)
    {
        // pointer to canonical MethodTable.
        TADDR canonicalMethodTable = union_getPointer(m_pCanonMT);
        return PTR_EEClass(PTR_MethodTable(canonicalMethodTable)->m_pCanonMT);
    }
#ifdef FEATURE_PREJIT
    else if (lowBits == UNION_INDIRECTION)
    {
        // pointer to indirection cell that points to canonical MethodTable
        TADDR canonicalMethodTable = *PTR_TADDR(union_getPointer(m_pCanonMT));
        return PTR_EEClass(PTR_MethodTable(canonicalMethodTable)->m_pCanonMT);
    }
#endif
#ifdef DACCESS_COMPILE
    // Minidumps don't guarantee that every member of every class will be able to work here.
#else
    _ASSERTE(!"Malformed m_pEEClass in MethodTable");
#endif
    return NULL;

#else

    TADDR addr = m_pCanonMT;

    if ((addr & 2) == 0)
    {
        // pointer to EEClass
        return PTR_EEClass(addr);
    }

#ifdef FEATURE_PREJIT
    if ((addr & 1) != 0)
    {
        // pointer to indirection cell that points to canonical MethodTable
        TADDR canonicalMethodTable = *PTR_TADDR(addr - 3);
        return PTR_EEClass(PTR_MethodTable(canonicalMethodTable)->m_pCanonMT);
    }
#endif

    // pointer to canonical MethodTable.
    return PTR_EEClass(PTR_MethodTable(addr - 2)->m_pCanonMT);
#endif
}

//==========================================================================================
inline PTR_EEClass MethodTable::GetClass()
{
    LIMITED_METHOD_DAC_CONTRACT;

    _ASSERTE_IMPL(!IsAsyncPinType());
    _ASSERTE_IMPL(GetClass_NoLogging() != NULL);

    g_IBCLogger.LogEEClassAndMethodTableAccess(this);
    return GetClass_NoLogging();
}

//==========================================================================================
inline Assembly * MethodTable::GetAssembly()
{
    WRAPPER_NO_CONTRACT;
    return GetModule()->GetAssembly();
}

//==========================================================================================
// DO NOT ADD ANY ASSERTS OR ANY OTHER CODE TO THIS METHOD.
// DO NOT USE THIS METHOD.
// Yes folks, for better or worse the debugger pokes supposed object addresses
// to try to see if objects are valid, possibly firing an AccessViolation or
// worse.  Thus it is "correct" behaviour for this to AV, and incorrect
// behaviour for it to assert if called on an invalid pointer.
inline PTR_EEClass MethodTable::GetClassWithPossibleAV()
{
    CANNOT_HAVE_CONTRACT;
    SUPPORTS_DAC;
    return GetClass_NoLogging();
}

//==========================================================================================
inline BOOL MethodTable::IsClassPointerValid()
{
    WRAPPER_NO_CONTRACT;
    SUPPORTS_DAC;

    LowBits lowBits = union_getLowBits(m_pCanonMT);
    if (lowBits == UNION_EECLASS)
    {
        return (m_pEEClass != NULL);
    }
    else if (lowBits == UNION_METHODTABLE)
    {
        // pointer to canonical MethodTable.
        TADDR canonicalMethodTable = union_getPointer(m_pCanonMT);
        return (PTR_MethodTable(canonicalMethodTable)->m_pEEClass != NULL);
    }
#ifdef FEATURE_PREJIT
    else if (lowBits == UNION_INDIRECTION)
    {
        // pointer to indirection cell that points to canonical MethodTable
        TADDR canonicalMethodTable = *PTR_TADDR(union_getPointer(m_pCanonMT));
        if (CORCOMPILE_IS_POINTER_TAGGED(canonicalMethodTable))
            return FALSE;
        return (PTR_MethodTable(canonicalMethodTable)->m_pEEClass != NULL);
    }
#endif
    _ASSERTE(!"Malformed m_pEEClass in MethodTable");
    return FALSE;
}

//==========================================================================================
// Does this immediate item live in an NGEN module?
inline BOOL MethodTable::IsZapped()
{
    LIMITED_METHOD_DAC_CONTRACT;

#ifdef FEATURE_PREJIT
    return GetFlag(enum_flag_IsZapped);
#else
    return FALSE;
#endif
}

//==========================================================================================
// For types that are part of an ngen-ed assembly this gets the
// Module* that contains this methodtable.
inline PTR_Module MethodTable::GetZapModule()
{
    LIMITED_METHOD_DAC_CONTRACT;

    PTR_Module zapModule = NULL;
    if (IsZapped())
    {
        zapModule = m_pLoaderModule;
    }

    return zapModule;
}

//==========================================================================================
inline PTR_Module MethodTable::GetLoaderModule() 
{
    LIMITED_METHOD_DAC_CONTRACT;
    return m_pLoaderModule;
}

inline PTR_LoaderAllocator MethodTable::GetLoaderAllocator() 
{
    LIMITED_METHOD_DAC_CONTRACT;
    return GetLoaderModule()->GetLoaderAllocator();
}



#ifndef DACCESS_COMPILE 
//==========================================================================================
inline void MethodTable::SetLoaderModule(Module* pModule) 
{
    WRAPPER_NO_CONTRACT;
    m_pLoaderModule = pModule;
}

inline void MethodTable::SetLoaderAllocator(LoaderAllocator* pAllocator) 
{
    LIMITED_METHOD_CONTRACT;
    _ASSERTE(pAllocator == GetLoaderAllocator());

    if (pAllocator->Id()->IsCollectible())
    {
        SetFlag(enum_flag_Collectible);
    }
}

#endif

//==========================================================================================
inline WORD MethodTable::GetNumNonVirtualSlots()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return HasNonVirtualSlots() ? GetClass()->GetNumNonVirtualSlots() : 0;
}        

//==========================================================================================
inline WORD MethodTable::GetNumInstanceFields()
{
    WRAPPER_NO_CONTRACT;
    return (GetClass()->GetNumInstanceFields());
}

//==========================================================================================
inline WORD MethodTable::GetNumStaticFields()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return (GetClass()->GetNumStaticFields());
}

//==========================================================================================
inline WORD MethodTable::GetNumThreadStaticFields()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return (GetClass()->GetNumThreadStaticFields());
}

//==========================================================================================
inline DWORD MethodTable::GetNumInstanceFieldBytes()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return(GetBaseSize() - GetClass()->GetBaseSizePadding());
}

//==========================================================================================
inline WORD MethodTable::GetNumIntroducedInstanceFields()
{
    LIMITED_METHOD_DAC_CONTRACT;

    WORD wNumFields = GetNumInstanceFields();

    MethodTable * pParentMT = GetParentMethodTable();
    if (pParentMT != NULL)
    {
        WORD wNumParentFields = pParentMT->GetNumInstanceFields();

        // If this assert fires, then our bookkeeping is bad. Perhaps we incremented the count
        // of fields on the base class w/o incrementing the count in the derived class. (EnC scenarios).
        _ASSERTE(wNumFields >= wNumParentFields);

        wNumFields -= wNumParentFields;
    }

    return(wNumFields);
}

//==========================================================================================
inline DWORD MethodTable::GetAlignedNumInstanceFieldBytes()
{
    WRAPPER_NO_CONTRACT;
    return((GetNumInstanceFieldBytes() + 3) & (~3));
}

//==========================================================================================
inline PTR_FieldDesc MethodTable::GetApproxFieldDescListRaw()
{
    WRAPPER_NO_CONTRACT;
    // Careful about using this method. If it's possible that fields may have been added via EnC, then
    // must use the FieldDescIterator as any fields added via EnC won't be in the raw list

    return GetClass()->GetFieldDescList();
}

#ifdef FEATURE_COMINTEROP
//==========================================================================================
inline DWORD MethodTable::IsComClassInterface()
{
    WRAPPER_NO_CONTRACT;
    return GetClass()->IsComClassInterface();
}

//==========================================================================================
inline DWORD MethodTable::IsComImport()
{
    WRAPPER_NO_CONTRACT;
    return GetClass()->IsComImport();
}

//==========================================================================================
// Sparse VTables.   These require a SparseVTableMap in the EEClass in
// order to record how the CLR's vtable slots map across to COM
// Interop slots.
//
inline int MethodTable::IsSparseForCOMInterop()
{
    WRAPPER_NO_CONTRACT;
    return GetClass()->IsSparseForCOMInterop();
}

//==========================================================================================
inline int MethodTable::IsComEventItfType()
{
    WRAPPER_NO_CONTRACT;
    _ASSERTE(GetClass());
    return GetClass()->IsComEventItfType();
}

#endif // FEATURE_COMINTEROP

//==========================================================================================
inline DWORD MethodTable::GetAttrClass()
{
    WRAPPER_NO_CONTRACT;
    return GetClass()->GetAttrClass();
}

//==========================================================================================
inline BOOL MethodTable::IsSerializable()
{
    WRAPPER_NO_CONTRACT;
    return GetClass()->IsSerializable();
}

//==========================================================================================
inline BOOL MethodTable::SupportsGenericInterop(TypeHandle::InteropKind interopKind,
                        MethodTable::Mode mode /*= modeAll*/)
{
    LIMITED_METHOD_CONTRACT;

#ifdef FEATURE_COMINTEROP
    return ((IsInterface() || IsDelegate()) &&    // interface or delegate
            HasInstantiation() &&                 // generic
            !IsSharedByGenericInstantiations() && // unshared
            !ContainsGenericVariables() &&        // closed over concrete types
            // defined in .winmd or one of the redirected mscorlib interfaces
            ((((mode & modeProjected) != 0) && IsProjectedFromWinRT()) ||
             (((mode & modeRedirected) != 0) && (IsWinRTRedirectedInterface(interopKind) || IsWinRTRedirectedDelegate()))));
#else // FEATURE_COMINTEROP
    return FALSE;
#endif // FEATURE_COMINTEROP
}


//==========================================================================================
inline BOOL MethodTable::IsNotTightlyPacked()
{
    WRAPPER_NO_CONTRACT;
    return GetClass()->IsNotTightlyPacked();
}

//==========================================================================================
inline BOOL MethodTable::HasFieldsWhichMustBeInited()
{
    WRAPPER_NO_CONTRACT;
    return GetClass()->HasFieldsWhichMustBeInited();
}

//==========================================================================================
inline BOOL MethodTable::SupportsAutoNGen()
{
    LIMITED_METHOD_CONTRACT;
    return FALSE;
}

//==========================================================================================
inline BOOL MethodTable::RunCCTorAsIfNGenImageExists()
{
    LIMITED_METHOD_CONTRACT;
#ifdef FEATURE_CORESYSTEM
    return TRUE; // On our coresystem builds we will always be using triton in the customer scenario.
#else
    return FALSE;
#endif
}

//==========================================================================================
inline BOOL MethodTable::IsAbstract()
{
    WRAPPER_NO_CONTRACT;
    return GetClass()->IsAbstract();
}

//==========================================================================================

#ifdef FEATURE_COMINTEROP
//==========================================================================================
inline void MethodTable::SetHasGuidInfo()
{
    LIMITED_METHOD_CONTRACT;
    _ASSERTE(IsInterface() || (HasCCWTemplate() && IsDelegate()));

    // for delegates, having CCW template implies having GUID info
    if (IsInterface())
        SetFlag(enum_flag_IfInterfaceThenHasGuidInfo);
}

//==========================================================================================
inline BOOL MethodTable::HasGuidInfo()
{
    LIMITED_METHOD_DAC_CONTRACT;

    if (IsInterface())
        return GetFlag(enum_flag_IfInterfaceThenHasGuidInfo);

    // HasCCWTemplate() is intentionally checked first here to avoid hitting 
    // g_pMulticastDelegateClass == NULL inside IsDelegate() during startup
    return HasCCWTemplate() && IsDelegate();
}

//==========================================================================================
// True IFF the type has a GUID explicitly assigned to it (including WinRT generic interfaces
// where the GUID is computed).
inline BOOL MethodTable::HasExplicitGuid()
{
    CONTRACTL {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        SUPPORTS_DAC;
    } CONTRACTL_END;

    GUID guid;
    GetGuid(&guid, FALSE);
    return (guid != GUID_NULL);
}

//==========================================================================================
inline void MethodTable::SetHasCCWTemplate()
{
    LIMITED_METHOD_CONTRACT;
    SetFlag(enum_flag_HasCCWTemplate);
}

//==========================================================================================
inline BOOL MethodTable::HasCCWTemplate()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return GetFlag(enum_flag_HasCCWTemplate);
}

//==========================================================================================
inline void MethodTable::SetHasRCWPerTypeData()
{
    LIMITED_METHOD_CONTRACT;
    SetFlag(enum_flag_HasRCWPerTypeData);
}

//==========================================================================================
inline BOOL MethodTable::HasRCWPerTypeData()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return GetFlag(enum_flag_HasRCWPerTypeData);
}

//==========================================================================================
// Get the GUID used for WinRT interop
//   * if the type is not a WinRT type or a redirected interfae return FALSE
inline BOOL MethodTable::GetGuidForWinRT(GUID *pGuid)
{
    CONTRACTL {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        SUPPORTS_DAC;
    } CONTRACTL_END;

    BOOL bRes = FALSE;
    if ((IsProjectedFromWinRT() && !HasInstantiation()) || 
        (SupportsGenericInterop(TypeHandle::Interop_NativeToManaged) && IsLegalNonArrayWinRTType()))
    {
        bRes = SUCCEEDED(GetGuidNoThrow(pGuid, TRUE, FALSE));
    }

    return bRes;
}


#endif // FEATURE_COMINTEROP

//==========================================================================================
// The following two methods produce correct results only if this type is
// marked Serializable (verified by assert in checked builds) and the field
// in question was introduced in this type (the index is the FieldDesc
// index).
inline BOOL MethodTable::IsFieldNotSerialized(DWORD dwFieldIndex)
{
    LIMITED_METHOD_CONTRACT;
    _ASSERTE(IsSerializable());
    return FALSE;
}

//==========================================================================================
inline BOOL MethodTable::IsFieldOptionallySerialized(DWORD dwFieldIndex)
{
    LIMITED_METHOD_CONTRACT;
    _ASSERTE(IsSerializable());
    return FALSE;
}

//==========================================================================================
// Is pParentMT System.Enum? (Cannot be called before System.Enum is loaded.)
inline BOOL MethodTable::IsEnum()
{
    LIMITED_METHOD_DAC_CONTRACT;

    // We should not be calling this before our parent method table pointer
    // is valid .
    _ASSERTE_IMPL(IsParentMethodTablePointerValid());

    PTR_MethodTable pParentMT = GetParentMethodTable();

    // Make sure that we are not using this method during startup
    _ASSERTE(g_pEnumClass != NULL);

    return (pParentMT == g_pEnumClass);
}

//==========================================================================================
// Is pParentMT either System.ValueType or System.Enum?
inline BOOL MethodTable::IsValueType()
{
    LIMITED_METHOD_DAC_CONTRACT;

    g_IBCLogger.LogMethodTableAccess(this);

    return GetFlag(enum_flag_Category_ValueType_Mask) == enum_flag_Category_ValueType;
}

//==========================================================================================
inline CorElementType MethodTable::GetArrayElementType()
{
    WRAPPER_NO_CONTRACT;

    _ASSERTE (IsArray());
    return dac_cast<PTR_ArrayClass>(GetClass())->GetArrayElementType();
}

//==========================================================================================
inline DWORD MethodTable::GetRank()
{
    LIMITED_METHOD_DAC_CONTRACT;

    _ASSERTE (IsArray());
    if (GetFlag(enum_flag_Category_IfArrayThenSzArray))
        return 1;  // ELEMENT_TYPE_SZARRAY
    else
        return dac_cast<PTR_ArrayClass>(GetClass())->GetRank();
}

//==========================================================================================
inline BOOL MethodTable::IsTruePrimitive()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return GetFlag(enum_flag_Category_Mask) == enum_flag_Category_TruePrimitive;
}

//==========================================================================================
inline void MethodTable::SetIsTruePrimitive()
{
    LIMITED_METHOD_DAC_CONTRACT;
    SetFlag(enum_flag_Category_TruePrimitive);
}

//==========================================================================================
inline BOOL MethodTable::IsBlittable()
{
    WRAPPER_NO_CONTRACT;
#ifndef DACCESS_COMPILE 
    _ASSERTE(GetClass());
    return GetClass()->IsBlittable();
#else // DACCESS_COMPILE
    DacNotImpl();
    return false;
#endif // DACCESS_COMPILE
}

//==========================================================================================
inline BOOL MethodTable::HasClassConstructor()
{
    WRAPPER_NO_CONTRACT;
    return GetFlag(enum_flag_HasCctor);
}

//==========================================================================================
inline void MethodTable::SetHasClassConstructor()
{
    WRAPPER_NO_CONTRACT;
    return SetFlag(enum_flag_HasCctor);
}

//==========================================================================================
inline WORD MethodTable::GetClassConstructorSlot()
{
    WRAPPER_NO_CONTRACT;
    _ASSERTE(HasClassConstructor());

    // The class constructor slot is the first non-vtable slot
    return GetNumVirtuals();
}

//==========================================================================================
inline BOOL MethodTable::HasDefaultConstructor()
{
    WRAPPER_NO_CONTRACT;
    return GetFlag(enum_flag_HasDefaultCtor);
}

//==========================================================================================
inline void MethodTable::SetHasDefaultConstructor()
{
    WRAPPER_NO_CONTRACT;
    return SetFlag(enum_flag_HasDefaultCtor);
}

//==========================================================================================
inline WORD MethodTable::GetDefaultConstructorSlot()
{
    WRAPPER_NO_CONTRACT;
    _ASSERTE(HasDefaultConstructor());

    // The default ctor slot is right after cctor slot if there is one
    return GetNumVirtuals() + (HasClassConstructor() ? 1 : 0);
}

//==========================================================================================
inline BOOL MethodTable::HasLayout()
{
    WRAPPER_NO_CONTRACT;
    _ASSERTE(GetClass());
    return GetClass()->HasLayout();
}

//==========================================================================================
inline MethodDesc* MethodTable::GetMethodDescForSlot(DWORD slot)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        SO_TOLERANT;
        MODE_ANY;
    }
    CONTRACTL_END;

    PCODE pCode = GetRestoredSlot(slot);

    // This is an optimization that we can take advantage of if we're trying
    // to get the MethodDesc for an interface virtual, since their slots
    // always point to the stub.
    if (IsInterface() && slot < GetNumVirtuals())
    {
        return MethodDesc::GetMethodDescFromStubAddr(pCode);
    }

    return MethodTable::GetMethodDescForSlotAddress(pCode);
}

#ifndef DACCESS_COMPILE 

//==========================================================================================
inline INT32 MethodTable::MethodIterator::GetNumMethods() const
{
    LIMITED_METHOD_CONTRACT;
    //  assert that number of methods hasn't changed during the iteration
    CONSISTENCY_CHECK( m_pMethodData->GetNumMethods() == static_cast< UINT32 >( m_iMethods ) );
    return m_iMethods;
}

//==========================================================================================
// Returns TRUE if it's valid to request data from the current position
inline BOOL MethodTable::MethodIterator::IsValid() const
{
    LIMITED_METHOD_CONTRACT;
    return m_iCur >= 0 && m_iCur < GetNumMethods();
}

//==========================================================================================
inline BOOL MethodTable::MethodIterator::MoveTo(UINT32 idx)
{
    LIMITED_METHOD_CONTRACT;
    m_iCur = (INT32)idx;
    return IsValid();
}

//==========================================================================================
inline BOOL MethodTable::MethodIterator::Prev()
{
    WRAPPER_NO_CONTRACT;
    if (IsValid())
        --m_iCur;
    return (IsValid());
}

//==========================================================================================
inline BOOL MethodTable::MethodIterator::Next()
{
    WRAPPER_NO_CONTRACT;
    if (IsValid())
        ++m_iCur;
    return (IsValid());
}

//==========================================================================================
inline void MethodTable::MethodIterator::MoveToBegin()
{
    WRAPPER_NO_CONTRACT;
    m_iCur = 0;
}

//==========================================================================================
inline void MethodTable::MethodIterator::MoveToEnd()
{
    WRAPPER_NO_CONTRACT;
    m_iCur = GetNumMethods() - 1;
}

//==========================================================================================
inline UINT32 MethodTable::MethodIterator::GetSlotNumber() const {
    LIMITED_METHOD_CONTRACT;
    CONSISTENCY_CHECK(IsValid());
    return (UINT32)m_iCur;
}

//==========================================================================================
inline UINT32 MethodTable::MethodIterator::GetImplSlotNumber() const {
    WRAPPER_NO_CONTRACT;
    CONSISTENCY_CHECK(IsValid());
    return (UINT32)m_pMethodData->GetImplSlotNumber(m_iCur);
}

//==========================================================================================
inline BOOL MethodTable::MethodIterator::IsVirtual() const {
    LIMITED_METHOD_CONTRACT;
    CONSISTENCY_CHECK(IsValid());
    return m_iCur < (INT32)(GetNumVirtuals());
}

//==========================================================================================
inline UINT32 MethodTable::MethodIterator::GetNumVirtuals() const {
    LIMITED_METHOD_CONTRACT;
    return m_pMethodData->GetNumVirtuals();;
}

//==========================================================================================
inline DispatchSlot MethodTable::MethodIterator::GetTarget() const {
    LIMITED_METHOD_CONTRACT;
    CONSISTENCY_CHECK(IsValid());
    return m_pMethodData->GetImplSlot(m_iCur);
}

//==========================================================================================
inline MethodDesc *MethodTable::MethodIterator::GetMethodDesc() const {
    LIMITED_METHOD_CONTRACT;
    CONSISTENCY_CHECK(IsValid());
    MethodDesc *pMD = m_pMethodData->GetImplMethodDesc(m_iCur);
    CONSISTENCY_CHECK(CheckPointer(pMD));
    return pMD;
}

//==========================================================================================
inline MethodDesc *MethodTable::MethodIterator::GetDeclMethodDesc() const {
    LIMITED_METHOD_CONTRACT;
    CONSISTENCY_CHECK(IsValid());
    MethodDesc *pMD = m_pMethodData->GetDeclMethodDesc(m_iCur);
    CONSISTENCY_CHECK(CheckPointer(pMD));
    CONSISTENCY_CHECK(pMD->GetSlot() == GetSlotNumber());
    return pMD;
}

#endif // DACCESS_COMPILE 

//==========================================================================================
// Non-canonical types share the method bodies with the canonical type. So the canonical
// type can be said to own the method bodies. Hence, by default, IntroducedMethodIterator
// only lets you iterate methods of the canonical type. You have to pass in
// restrictToCanonicalTypes=FALSE to iterate methods through a non-canonical type.

inline MethodTable::IntroducedMethodIterator::IntroducedMethodIterator(
        MethodTable *pMT, 
        BOOL restrictToCanonicalTypes /* = TRUE */ )
{
    WRAPPER_NO_CONTRACT;
    CONSISTENCY_CHECK(pMT->IsCanonicalMethodTable() || !restrictToCanonicalTypes);

    SetChunk(pMT->GetClass()->GetChunks());
}

//==========================================================================================
FORCEINLINE BOOL MethodTable::IntroducedMethodIterator::Next()
{
    WRAPPER_NO_CONTRACT;
    CONSISTENCY_CHECK(IsValid());

    // Check whether the next MethodDesc is still within the bounds of the current chunk
    TADDR pNext = dac_cast<TADDR>(m_pMethodDesc) + m_pMethodDesc->SizeOf();

    if (pNext < m_pChunkEnd)
    {
        // Just skip to the next method in the same chunk
        m_pMethodDesc = PTR_MethodDesc(pNext);
    }
    else
    {
        _ASSERTE(pNext == m_pChunkEnd);

        // We have walked all the methods in the current chunk. Move on
        // to the next chunk.
        SetChunk(m_pChunk->GetNextChunk());
    }

    return IsValid();
}

//==========================================================================================
inline BOOL MethodTable::IntroducedMethodIterator::IsValid() const
{
    LIMITED_METHOD_CONTRACT;
    return m_pMethodDesc != NULL;
}

//==========================================================================================
inline MethodDesc * MethodTable::IntroducedMethodIterator::GetMethodDesc() const
{
    WRAPPER_NO_CONTRACT;
    CONSISTENCY_CHECK(IsValid());
    return m_pMethodDesc;
}

//==========================================================================================
inline DWORD MethodTable::GetIndexOfVtableIndirection(DWORD slotNum)
{
    LIMITED_METHOD_DAC_CONTRACT;
    _ASSERTE((1 << VTABLE_SLOTS_PER_CHUNK_LOG2) == VTABLE_SLOTS_PER_CHUNK);

    return slotNum >> VTABLE_SLOTS_PER_CHUNK_LOG2;
}

//==========================================================================================
inline DWORD MethodTable::GetStartSlotForVtableIndirection(UINT32 indirectionIndex, DWORD wNumVirtuals)
{
    LIMITED_METHOD_DAC_CONTRACT;
    _ASSERTE(indirectionIndex < GetNumVtableIndirections(wNumVirtuals));

    return indirectionIndex * VTABLE_SLOTS_PER_CHUNK;
}

//==========================================================================================
inline DWORD MethodTable::GetEndSlotForVtableIndirection(UINT32 indirectionIndex, DWORD wNumVirtuals)
{
    LIMITED_METHOD_DAC_CONTRACT;
    _ASSERTE(indirectionIndex < GetNumVtableIndirections(wNumVirtuals));

    DWORD end = (indirectionIndex + 1) * VTABLE_SLOTS_PER_CHUNK;

    if (end > wNumVirtuals)
    {
        end = wNumVirtuals;
    }

    return end;
}

//==========================================================================================
inline UINT32 MethodTable::GetIndexAfterVtableIndirection(UINT32 slotNum)
{
    LIMITED_METHOD_DAC_CONTRACT;
    _ASSERTE((1 << VTABLE_SLOTS_PER_CHUNK_LOG2) == VTABLE_SLOTS_PER_CHUNK);

    return (slotNum & (VTABLE_SLOTS_PER_CHUNK - 1));
}

//==========================================================================================
inline DWORD MethodTable::GetNumVtableIndirections(DWORD wNumVirtuals)
{
    LIMITED_METHOD_DAC_CONTRACT;
    _ASSERTE((1 << VTABLE_SLOTS_PER_CHUNK_LOG2) == VTABLE_SLOTS_PER_CHUNK);

    return (wNumVirtuals + (VTABLE_SLOTS_PER_CHUNK - 1)) >> VTABLE_SLOTS_PER_CHUNK_LOG2;
}

//==========================================================================================
inline PTR_PTR_PCODE MethodTable::GetVtableIndirections()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return dac_cast<PTR_PTR_PCODE>(dac_cast<TADDR>(this) + sizeof(MethodTable));
}

//==========================================================================================
inline DWORD MethodTable::GetNumVtableIndirections()
{
    WRAPPER_NO_CONTRACT;

    return GetNumVtableIndirections(GetNumVirtuals_NoLogging());
}

//==========================================================================================
inline MethodTable::VtableIndirectionSlotIterator::VtableIndirectionSlotIterator(MethodTable *pMT)
  : m_pSlot(pMT->GetVtableIndirections()),
    m_i((DWORD) -1),
    m_count(pMT->GetNumVtableIndirections()),
    m_pMT(pMT)
{
    WRAPPER_NO_CONTRACT;
}

//==========================================================================================
inline MethodTable::VtableIndirectionSlotIterator::VtableIndirectionSlotIterator(MethodTable *pMT, DWORD index)
  : m_pSlot(pMT->GetVtableIndirections() + index),
    m_i(index),
    m_count(pMT->GetNumVtableIndirections()),
    m_pMT(pMT)
{
    WRAPPER_NO_CONTRACT;
    PRECONDITION(index != (DWORD) -1 && index < m_count);
}

//==========================================================================================
inline BOOL MethodTable::VtableIndirectionSlotIterator::Next()
{
    LIMITED_METHOD_DAC_CONTRACT;
    PRECONDITION(!Finished());
    if (m_i != (DWORD) -1)
        m_pSlot++;
    return (++m_i < m_count);
}

//==========================================================================================
inline BOOL MethodTable::VtableIndirectionSlotIterator::Finished()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return (m_i == m_count);
}

//==========================================================================================
inline DWORD MethodTable::VtableIndirectionSlotIterator::GetIndex()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return m_i;
}

//==========================================================================================
inline DWORD MethodTable::VtableIndirectionSlotIterator::GetOffsetFromMethodTable()
{
    WRAPPER_NO_CONTRACT;
    PRECONDITION(m_i != (DWORD) -1 && m_i < m_count);
    
    return GetVtableOffset() + sizeof(PTR_PCODE) * m_i;
}

//==========================================================================================
inline PTR_PCODE MethodTable::VtableIndirectionSlotIterator::GetIndirectionSlot()
{
    LIMITED_METHOD_DAC_CONTRACT;
    PRECONDITION(m_i != (DWORD) -1 && m_i < m_count);

    return *m_pSlot;
}

//==========================================================================================
#ifndef DACCESS_COMPILE
inline void MethodTable::VtableIndirectionSlotIterator::SetIndirectionSlot(PTR_PCODE pChunk)
{
    LIMITED_METHOD_CONTRACT;
    *m_pSlot = pChunk;
}
#endif

//==========================================================================================
inline DWORD MethodTable::VtableIndirectionSlotIterator::GetStartSlot()
{
    WRAPPER_NO_CONTRACT;
    PRECONDITION(m_i != (DWORD) -1 && m_i < m_count);

    return GetStartSlotForVtableIndirection(m_i, m_pMT->GetNumVirtuals());
}

//==========================================================================================
inline DWORD MethodTable::VtableIndirectionSlotIterator::GetEndSlot()
{
    WRAPPER_NO_CONTRACT;
    PRECONDITION(m_i != (DWORD) -1 && m_i < m_count);

    return GetEndSlotForVtableIndirection(m_i, m_pMT->GetNumVirtuals());
}

//==========================================================================================
inline DWORD MethodTable::VtableIndirectionSlotIterator::GetNumSlots()
{
    WRAPPER_NO_CONTRACT;

    return GetEndSlot() - GetStartSlot();
}

//==========================================================================================
inline DWORD MethodTable::VtableIndirectionSlotIterator::GetSize()
{
    WRAPPER_NO_CONTRACT;

    return GetNumSlots() * sizeof(PCODE);
}

//==========================================================================================
// Create a new iterator over the vtable indirection slots
// The iterator starts just before the first item
inline MethodTable::VtableIndirectionSlotIterator MethodTable::IterateVtableIndirectionSlots()
{
    WRAPPER_NO_CONTRACT;
    return VtableIndirectionSlotIterator(this);
}

//==========================================================================================
// Create a new iterator over the vtable indirection slots, starting at the index specified
inline MethodTable::VtableIndirectionSlotIterator MethodTable::IterateVtableIndirectionSlotsFrom(DWORD index)
{
    WRAPPER_NO_CONTRACT;
    return VtableIndirectionSlotIterator(this, index);
}

#ifndef DACCESS_COMPILE
#ifdef FEATURE_COMINTEROP

//==========================================================================================
inline ComCallWrapperTemplate *MethodTable::GetComCallWrapperTemplate()
{
    LIMITED_METHOD_CONTRACT;
    if (HasCCWTemplate())
    {
        return *GetCCWTemplatePtr();
    }
    return GetClass()->GetComCallWrapperTemplate();
}

//==========================================================================================
inline BOOL MethodTable::SetComCallWrapperTemplate(ComCallWrapperTemplate *pTemplate)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

    if (HasCCWTemplate())
    {
        TypeHandle th(this);
        g_IBCLogger.LogTypeMethodTableWriteableAccess(&th);
        return (InterlockedCompareExchangeT(EnsureWritablePages(GetCCWTemplatePtr()), pTemplate, NULL) == NULL);
    }
    g_IBCLogger.LogEEClassCOWTableAccess(this);
    return GetClass_NoLogging()->SetComCallWrapperTemplate(pTemplate);
}

#ifdef FEATURE_COMINTEROP_UNMANAGED_ACTIVATION
//==========================================================================================
inline ClassFactoryBase *MethodTable::GetComClassFactory()
{
    LIMITED_METHOD_CONTRACT;
    return GetClass()->GetComClassFactory();
}

//==========================================================================================
inline BOOL MethodTable::SetComClassFactory(ClassFactoryBase *pFactory)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

    g_IBCLogger.LogEEClassCOWTableAccess(this);
    return GetClass_NoLogging()->SetComClassFactory(pFactory);
}
#endif // FEATURE_COMINTEROP_UNMANAGED_ACTIVATION
#endif // FEATURE_COMINTEROP
#endif // DACCESS_COMPILE

#ifdef FEATURE_COMINTEROP
//==========================================================================================
inline BOOL MethodTable::IsProjectedFromWinRT()
{
    LIMITED_METHOD_DAC_CONTRACT;
    _ASSERTE(GetClass());
    return GetClass()->IsProjectedFromWinRT();
}

//==========================================================================================
inline BOOL MethodTable::IsExportedToWinRT()
{
    LIMITED_METHOD_DAC_CONTRACT;
    _ASSERTE(GetClass());
    return GetClass()->IsExportedToWinRT();
}

//==========================================================================================
inline BOOL MethodTable::IsWinRTDelegate()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return (IsProjectedFromWinRT() && IsDelegate()) || IsWinRTRedirectedDelegate();
}

#else // FEATURE_COMINTEROP

//==========================================================================================
inline BOOL MethodTable::IsProjectedFromWinRT()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return FALSE;
}

//==========================================================================================
inline BOOL MethodTable::IsExportedToWinRT()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return FALSE;
}

//==========================================================================================
inline BOOL MethodTable::IsWinRTDelegate()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return FALSE;
}

#endif // FEATURE_COMINTEROP

//==========================================================================================
inline UINT32 MethodTable::GetNativeSize()
{
    LIMITED_METHOD_CONTRACT;
    _ASSERTE(GetClass());
    return GetClass()->GetNativeSize();
}

//==========================================================================================
inline PTR_MethodTable MethodTable::GetCanonicalMethodTable()
{
    LIMITED_METHOD_DAC_CONTRACT;

#ifdef _DEBUG
    LowBits lowBits = union_getLowBits(m_pCanonMT);
    if (lowBits == UNION_EECLASS)
    {
        return dac_cast<PTR_MethodTable>(this);
    }
    else if (lowBits == UNION_METHODTABLE)
    {
        // pointer to canonical MethodTable.
        return PTR_MethodTable(union_getPointer(m_pCanonMT));
    }
#ifdef FEATURE_PREJIT
    else if (lowBits == UNION_INDIRECTION)
    {
        return PTR_MethodTable(*PTR_TADDR(union_getPointer(m_pCanonMT)));
    }
#endif
    _ASSERTE(!"Malformed m_pCanonMT in MethodTable");
    return NULL;
#else
    TADDR addr = m_pCanonMT;

    if ((addr & 2) == 0)
        return dac_cast<PTR_MethodTable>(this);

#ifdef FEATURE_PREJIT
    if ((addr & 1) != 0)
        return PTR_MethodTable(*PTR_TADDR(addr - 3));
#endif

    return PTR_MethodTable(addr - 2);
#endif
}

//==========================================================================================
inline TADDR MethodTable::GetCanonicalMethodTableFixup()
{
    LIMITED_METHOD_DAC_CONTRACT;

#ifdef FEATURE_PREJIT
    LowBits lowBits = union_getLowBits(m_pCanonMT);
    if (lowBits == UNION_INDIRECTION)
    {
        // pointer to canonical MethodTable.
        return *PTR_TADDR(union_getPointer(m_pCanonMT));
    }
    else
#endif
    {
        return NULL;
    }
}

//==========================================================================================
#ifndef DACCESS_COMPILE
FORCEINLINE BOOL MethodTable::IsEquivalentTo(MethodTable *pOtherMT COMMA_INDEBUG(TypeHandlePairList *pVisited /*= NULL*/))
{
    WRAPPER_NO_CONTRACT;

    if (this == pOtherMT)
        return TRUE;

#ifdef FEATURE_TYPEEQUIVALENCE
    // bail early for normal types
    if (!HasTypeEquivalence() || !pOtherMT->HasTypeEquivalence())
        return FALSE;

    if (IsEquivalentTo_Worker(pOtherMT COMMA_INDEBUG(pVisited)))
        return TRUE;
#endif // FEATURE_TYPEEQUIVALENCE

    return FALSE;
}
#endif

//==========================================================================================
inline IMDInternalImport* MethodTable::GetMDImport()
{
    LIMITED_METHOD_CONTRACT;
    return GetModule()->GetMDImport();
}

//==========================================================================================
inline BOOL MethodTable::IsSealed()
{
    LIMITED_METHOD_CONTRACT;
    return GetClass()->IsSealed();
}

//==========================================================================================
inline BOOL MethodTable::IsManagedSequential()
{
    LIMITED_METHOD_CONTRACT;
    return GetClass()->IsManagedSequential();
}

//==========================================================================================
inline BOOL MethodTable::HasExplicitSize()
{
    LIMITED_METHOD_CONTRACT;
    return GetClass()->HasExplicitSize();
}

//==========================================================================================
inline DWORD MethodTable::GetPerInstInfoSize()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return GetNumDicts() * sizeof(TypeHandle*);
}

//==========================================================================================
inline EEClassLayoutInfo *MethodTable::GetLayoutInfo()
{
    LIMITED_METHOD_CONTRACT;
    PRECONDITION(HasLayout());
    return GetClass()->GetLayoutInfo();
}

//==========================================================================================
// These come after the pointers to the generic dictionaries (if any)
inline DWORD MethodTable::GetInterfaceMapSize()
{
    LIMITED_METHOD_DAC_CONTRACT;

    DWORD cbIMap = GetNumInterfaces() * sizeof(InterfaceInfo_t);
#ifdef FEATURE_COMINTEROP 
    cbIMap += (HasDynamicInterfaceMap() ? sizeof(DWORD_PTR) : 0);
#endif
    return cbIMap;
}

//==========================================================================================
// These are the generic dictionaries themselves and are come after
//  the interface map.  In principle they need not be inline in the method table.
inline DWORD MethodTable::GetInstAndDictSize()
{
    LIMITED_METHOD_DAC_CONTRACT;

    if (!HasInstantiation())
        return 0;
    else
        return DictionaryLayout::GetFirstDictionaryBucketSize(GetNumGenericArgs(), GetClass()->GetDictionaryLayout());
}

//==========================================================================================
inline BOOL MethodTable::IsSharedByGenericInstantiations()
{
    LIMITED_METHOD_DAC_CONTRACT;

    g_IBCLogger.LogMethodTableAccess(this);

    return TestFlagWithMask(enum_flag_GenericsMask, enum_flag_GenericsMask_SharedInst);
}

//==========================================================================================
inline BOOL MethodTable::IsCanonicalMethodTable()
{
    LIMITED_METHOD_DAC_CONTRACT;

    return (union_getLowBits(m_pCanonMT) == UNION_EECLASS);
}

//==========================================================================================
FORCEINLINE BOOL MethodTable::HasInstantiation()
{
    LIMITED_METHOD_DAC_CONTRACT;

    // Generics flags cannot be expressed in terms of GetFlag()
    return !TestFlagWithMask(enum_flag_GenericsMask, enum_flag_GenericsMask_NonGeneric);
}

//==========================================================================================
inline void MethodTable::SetHasInstantiation(BOOL fTypicalInstantiation, BOOL fSharedByGenericInstantiations)
{
    LIMITED_METHOD_CONTRACT;
    _ASSERTE(!IsStringOrArray());
    SetFlag(fTypicalInstantiation ? enum_flag_GenericsMask_TypicalInst :
        (fSharedByGenericInstantiations ? enum_flag_GenericsMask_SharedInst : enum_flag_GenericsMask_GenericInst));
}
//==========================================================================================
inline BOOL MethodTable::IsGenericTypeDefinition()
{
    LIMITED_METHOD_DAC_CONTRACT;

    // Generics flags cannot be expressed in terms of GetFlag()
    return TestFlagWithMask(enum_flag_GenericsMask, enum_flag_GenericsMask_TypicalInst);
}

//==========================================================================================
inline PTR_InterfaceInfo MethodTable::GetInterfaceMap()
{
    LIMITED_METHOD_DAC_CONTRACT;

    return dac_cast<PTR_InterfaceInfo>(m_pMultipurposeSlot2); // m_pInterfaceMap
}

//==========================================================================================
FORCEINLINE TADDR MethodTable::GetMultipurposeSlotPtr(WFLAGS2_ENUM flag, const BYTE * offsets)
{
    LIMITED_METHOD_DAC_CONTRACT;

    _ASSERTE(GetFlag(flag));

    DWORD offset = offsets[GetFlag((WFLAGS2_ENUM)(flag - 1))];

    if (offset >= sizeof(MethodTable)) {
        offset += GetNumVtableIndirections() * sizeof(PTR_PCODE);
    }

    return dac_cast<TADDR>(this) + offset;
}

//==========================================================================================
// This method is dependent on the declared order of optional members
// If you add or remove an optional member or reorder them please change this method
FORCEINLINE DWORD MethodTable::GetOffsetOfOptionalMember(OptionalMemberId id)
{
    LIMITED_METHOD_CONTRACT;

    DWORD offset = c_OptionalMembersStartOffsets[GetFlag(enum_flag_MultipurposeSlotsMask)];

    offset += GetNumVtableIndirections() * sizeof(PTR_PCODE);

#undef METHODTABLE_OPTIONAL_MEMBER
#define METHODTABLE_OPTIONAL_MEMBER(NAME, TYPE, GETTER) \
    if (id == OptionalMember_##NAME) { \
        return offset; \
    } \
    C_ASSERT(sizeof(TYPE) % sizeof(UINT_PTR) == 0); /* To insure proper alignment */ \
    if (Has##NAME()) { \
        offset += sizeof(TYPE); \
    }

    METHODTABLE_OPTIONAL_MEMBERS()

    _ASSERTE(!"Wrong optional member" || id == OptionalMember_Count);
    return offset;
}

//==========================================================================================
// this is not the pretties function however I got bitten pretty hard by the computation 
// of the allocation size of a MethodTable done "by hand" in few places.
// Essentially the idea is that this is going to centralize computation of size for optional
// members so the next morons that need to add an optional member will look at this function
// and hopefully be less exposed to code doing size computation behind their back
inline DWORD MethodTable::GetOptionalMembersAllocationSize(DWORD dwMultipurposeSlotsMask,
                                                           BOOL needsRemotableMethodInfo,
                                                           BOOL needsGenericsStaticsInfo,
                                                           BOOL needsGuidInfo,
                                                           BOOL needsCCWTemplate,
                                                           BOOL needsRCWPerTypeData,
                                                           BOOL needsRemotingVtsInfo,
                                                           BOOL needsContextStatic,
                                                           BOOL needsTokenOverflow)
{
    LIMITED_METHOD_CONTRACT;

    DWORD size = c_OptionalMembersStartOffsets[dwMultipurposeSlotsMask] - sizeof(MethodTable);

    if (needsRemotableMethodInfo)
        size += sizeof(UINT_PTR);
    if (needsGenericsStaticsInfo)
        size += sizeof(GenericsStaticsInfo);
    if (needsGuidInfo)
        size += sizeof(UINT_PTR);
    if (needsCCWTemplate)
        size += sizeof(UINT_PTR);
    if (needsRCWPerTypeData)
        size += sizeof(UINT_PTR);
    if (needsRemotingVtsInfo)
        size += sizeof(UINT_PTR);
    if (needsContextStatic)
        size += sizeof(UINT_PTR);
    if (dwMultipurposeSlotsMask & enum_flag_HasInterfaceMap)
        size += sizeof(UINT_PTR);
    if (needsTokenOverflow)
        size += sizeof(UINT_PTR);

    return size;
}

inline DWORD MethodTable::GetOptionalMembersSize()
{
    WRAPPER_NO_CONTRACT;

    return GetEndOffsetOfOptionalMembers() - GetStartOffsetOfOptionalMembers();
}

#ifndef DACCESS_COMPILE

//==========================================================================================
inline PTR_BYTE MethodTable::GetNonGCStaticsBasePointer()
{
    WRAPPER_NO_CONTRACT;
    return GetDomainLocalModule()->GetNonGCStaticsBasePointer(this);
}

//==========================================================================================
inline PTR_BYTE MethodTable::GetGCStaticsBasePointer()
{
    WRAPPER_NO_CONTRACT;
    return GetDomainLocalModule()->GetGCStaticsBasePointer(this);
}

//==========================================================================================
inline PTR_BYTE MethodTable::GetNonGCThreadStaticsBasePointer()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

    // Get the current thread
    PTR_Thread pThread = dac_cast<PTR_Thread>(GetThread());

    // Get the current module's ModuleIndex
    ModuleIndex index = GetModuleForStatics()->GetModuleIndex();
    
    PTR_ThreadLocalBlock pTLB = ThreadStatics::GetCurrentTLBIfExists(pThread, NULL);
    if (pTLB == NULL)
        return NULL;

    PTR_ThreadLocalModule pTLM = pTLB->GetTLMIfExists(index);
    if (pTLM == NULL)
        return NULL;

    return pTLM->GetNonGCStaticsBasePointer(this);
}

//==========================================================================================
inline PTR_BYTE MethodTable::GetGCThreadStaticsBasePointer()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    // Get the current thread
    PTR_Thread pThread = dac_cast<PTR_Thread>(GetThread());

    // Get the current module's ModuleIndex
    ModuleIndex index = GetModuleForStatics()->GetModuleIndex();
    
    PTR_ThreadLocalBlock pTLB = ThreadStatics::GetCurrentTLBIfExists(pThread, NULL);
    if (pTLB == NULL)
        return NULL;

    PTR_ThreadLocalModule pTLM = pTLB->GetTLMIfExists(index);
    if (pTLM == NULL)
        return NULL;

    return pTLM->GetGCStaticsBasePointer(this);
}

#endif //!DACCESS_COMPILE

//==========================================================================================
inline PTR_BYTE MethodTable::GetNonGCThreadStaticsBasePointer(PTR_Thread pThread, PTR_AppDomain pDomain)
{
    LIMITED_METHOD_DAC_CONTRACT;

    // Get the current module's ModuleIndex
    ModuleIndex index = GetModuleForStatics()->GetModuleIndex();
    
    PTR_ThreadLocalBlock pTLB = ThreadStatics::GetCurrentTLBIfExists(pThread, pDomain);
    if (pTLB == NULL)
        return NULL;

    PTR_ThreadLocalModule pTLM = pTLB->GetTLMIfExists(index);
    if (pTLM == NULL)
        return NULL;

    return pTLM->GetNonGCStaticsBasePointer(this);
}

//==========================================================================================
inline PTR_BYTE MethodTable::GetGCThreadStaticsBasePointer(PTR_Thread pThread, PTR_AppDomain pDomain)
{
    LIMITED_METHOD_DAC_CONTRACT;

    // Get the current module's ModuleIndex
    ModuleIndex index = GetModuleForStatics()->GetModuleIndex();
    
    PTR_ThreadLocalBlock pTLB = ThreadStatics::GetCurrentTLBIfExists(pThread, pDomain);
    if (pTLB == NULL)
        return NULL;

    PTR_ThreadLocalModule pTLM = pTLB->GetTLMIfExists(index);
    if (pTLM == NULL)
        return NULL;

    return pTLM->GetGCStaticsBasePointer(this);
}

//==========================================================================================
inline PTR_DomainLocalModule MethodTable::GetDomainLocalModule(AppDomain * pAppDomain)
{
    WRAPPER_NO_CONTRACT;
    return GetModuleForStatics()->GetDomainLocalModule(pAppDomain);
}

#ifndef DACCESS_COMPILE
//==========================================================================================
inline PTR_DomainLocalModule MethodTable::GetDomainLocalModule()
{
    WRAPPER_NO_CONTRACT;
    return GetModuleForStatics()->GetDomainLocalModule();
}
#endif //!DACCESS_COMPILE

//==========================================================================================
inline OBJECTREF MethodTable::AllocateNoChecks()
{
    CONTRACTL
    {
        MODE_COOPERATIVE;
        GC_TRIGGERS;
        THROWS;
    }
    CONTRACTL_END;

    // we know an instance of this class already exists in the same appdomain
    // therefore, some checks become redundant.
    // this currently only happens for Delegate.Combine
    CONSISTENCY_CHECK(IsRestored_NoLogging());

    CONSISTENCY_CHECK(CheckInstanceActivated());

    return AllocateObject(this);
}


//==========================================================================================
inline DWORD MethodTable::GetClassIndex()
{
    WRAPPER_NO_CONTRACT;
    return GetClassIndexFromToken(GetCl());
}

#ifndef DACCESS_COMPILE
//==========================================================================================
// unbox src into dest, making sure src is of the correct type.

inline BOOL MethodTable::UnBoxInto(void *dest, OBJECTREF src) 
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        SO_TOLERANT;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    if (Nullable::IsNullableType(TypeHandle(this)))
        return Nullable::UnBoxNoGC(dest, src, this);
    else  
    {
        if (src == NULL || src->GetMethodTable() != this)
            return FALSE;

        CopyValueClass(dest, src->UnBox(), this, src->GetAppDomain());
    }
    return TRUE;
}

//==========================================================================================
// unbox src into argument, making sure src is of the correct type.

inline BOOL MethodTable::UnBoxIntoArg(ArgDestination *argDest, OBJECTREF src)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        SO_TOLERANT;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    if (Nullable::IsNullableType(TypeHandle(this)))
        return Nullable::UnBoxIntoArgNoGC(argDest, src, this);
    else  
    {
        if (src == NULL || src->GetMethodTable() != this)
            return FALSE;

        CopyValueClassArg(argDest, src->UnBox(), this, src->GetAppDomain(), 0);
    }
    return TRUE;
}

//==========================================================================================
// unbox src into dest, No checks are done

inline void MethodTable::UnBoxIntoUnchecked(void *dest, OBJECTREF src) 
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        SO_TOLERANT;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    if (Nullable::IsNullableType(TypeHandle(this))) {
        BOOL ret;
        ret = Nullable::UnBoxNoGC(dest, src, this);
        _ASSERTE(ret);
    }
    else  
    {
        _ASSERTE(src->GetMethodTable()->GetNumInstanceFieldBytes() == GetNumInstanceFieldBytes());

        CopyValueClass(dest, src->UnBox(), this, src->GetAppDomain());
    }
}
#endif
//==========================================================================================
__forceinline TypeHandle::CastResult MethodTable::CanCastToClassOrInterfaceNoGC(MethodTable *pTargetMT)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        INSTANCE_CHECK;
        SO_TOLERANT;
        PRECONDITION(CheckPointer(pTargetMT));
        PRECONDITION(!pTargetMT->IsArray());
    }
    CONTRACTL_END

    if (pTargetMT->IsInterface())
        return CanCastToInterfaceNoGC(pTargetMT);
    else
        return CanCastToClassNoGC(pTargetMT);
}

//==========================================================================================
inline BOOL MethodTable::CanCastToClassOrInterface(MethodTable *pTargetMT, TypeHandlePairList *pVisited)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        INSTANCE_CHECK;
        PRECONDITION(CheckPointer(pTargetMT));
        PRECONDITION(!pTargetMT->IsArray());
        PRECONDITION(IsRestored_NoLogging());
    }
    CONTRACTL_END

    if (pTargetMT->IsInterface())
        return CanCastToInterface(pTargetMT, pVisited);
    else
        return CanCastToClass(pTargetMT, pVisited);
}

//==========================================================================================
FORCEINLINE PTR_Module MethodTable::GetGenericsStaticsModuleAndID(DWORD * pID)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        SO_TOLERANT;
        SUPPORTS_DAC;
    }
    CONTRACTL_END

    _ASSERTE(HasGenericsStaticsInfo());

#ifdef FEATURE_PREJIT
    // This is performance sensitive codepath inlined into JIT helpers. Test the flag directly without
    // checking IsStringOrArray() first. IsStringOrArray() will always be false here.
    _ASSERTE(!IsStringOrArray());
    if (m_dwFlags & enum_flag_StaticsMask_IfGenericsThenCrossModule)
    {
        CrossModuleGenericsStaticsInfo *pInfo = m_pWriteableData->GetCrossModuleGenericsStaticsInfo();
        _ASSERTE(FitsIn<DWORD>(pInfo->m_DynamicTypeID) || pInfo->m_DynamicTypeID == (SIZE_T)-1);
        *pID = static_cast<DWORD>(pInfo->m_DynamicTypeID);
        return pInfo->m_pModuleForStatics;
    }
#endif // FEATURE_PREJIT

    _ASSERTE(FitsIn<DWORD>(GetGenericsStaticsInfo()->m_DynamicTypeID) || GetGenericsStaticsInfo()->m_DynamicTypeID == (SIZE_T)-1);
    *pID = static_cast<DWORD>(GetGenericsStaticsInfo()->m_DynamicTypeID);
    return GetLoaderModule();
}

//==========================================================================================
inline OBJECTHANDLE MethodTable::GetLoaderAllocatorObjectHandle()
{
    LIMITED_METHOD_CONTRACT;
    return GetLoaderAllocator()->GetLoaderAllocatorObjectHandle();
}

//==========================================================================================
FORCEINLINE OBJECTREF MethodTable::GetManagedClassObjectIfExists() 
{
    LIMITED_METHOD_CONTRACT;

    // Logging will be done by the slow path
    LOADERHANDLE handle = GetWriteableData_NoLogging()->GetExposedClassObjectHandle();

    OBJECTREF retVal;

    // GET_LOADERHANDLE_VALUE_FAST macro is inlined here to let us give hint to the compiler
    // when the return value is not null.
    if (!LoaderAllocator::GetHandleValueFast(handle, &retVal) &&
        !GetLoaderAllocator()->GetHandleValueFastPhase2(handle, &retVal))
    {
        return NULL;
    }

    // Only code:MethodTable::GetManagedClassObject sets m_pExposedClassObject and it insures that 
    // remoted objects and arrays don't get in.  
    _ASSERTE(!IsArray() && !IsTransparentProxy());

    COMPILER_ASSUME(retVal != NULL);
    return retVal;
}

//==========================================================================================
inline void MethodTable::SetIsArray(CorElementType arrayType, CorElementType elementType)
{
    STANDARD_VM_CONTRACT;

    DWORD category = enum_flag_Category_Array;
    if (arrayType == ELEMENT_TYPE_SZARRAY)
        category |= enum_flag_Category_IfArrayThenSzArray;

    _ASSERTE((m_dwFlags & enum_flag_Category_Mask) == 0);
    m_dwFlags |= category;

    _ASSERTE(GetInternalCorElementType() == arrayType);
}

//==========================================================================================
FORCEINLINE BOOL MethodTable::ImplementsInterfaceInline(MethodTable *pInterface)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        SO_TOLERANT;
        PRECONDITION(pInterface->IsInterface()); // class we are looking up should be an interface
    }
    CONTRACTL_END;

    //
    // Inline InterfaceMapIterator here for performance reasons
    //

    DWORD numInterfaces = GetNumInterfaces();
    if (numInterfaces == 0)
        return FALSE;

    InterfaceInfo_t *pInfo = GetInterfaceMap();

    do
    {
        if (pInfo->GetMethodTable() == pInterface)
        {
            // Extensible RCW's need to be handled specially because they can have interfaces
            // in their map that are added at runtime. These interfaces will have a start offset
            // of -1 to indicate this. We cannot take for granted that every instance of this
            // COM object has this interface so FindInterface on these interfaces is made to fail.
            //
            // However, we are only considering the statically available slots here
            // (m_wNumInterface doesn't contain the dynamic slots), so we can safely
            // ignore this detail.
            return TRUE;
        }
        pInfo++;
    }
    while (--numInterfaces);

    return FALSE;
}

#endif // !_METHODTABLE_INL_