-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
metamodelrw.h
1399 lines (1194 loc) · 55.8 KB
/
metamodelrw.h
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//*****************************************************************************
// MetaModelRW.h -- header file for Read/Write compressed COM+ metadata.
//
//
// Used by Emitters and by E&C.
//
//*****************************************************************************
#ifndef _METAMODELRW_H_
#define _METAMODELRW_H_
#if _MSC_VER >= 1100
# pragma once
#endif
#include "metamodel.h" // Base classes for the MetaModel.
#include "metadatahash.h"
#include "rwutil.h"
#include "shash.h"
#include "../heaps/export.h"
#include "../tables/export.h"
#include<minipal/utils.h>
struct HENUMInternal;
#ifdef FEATURE_METADATA_CUSTOM_DATA_SOURCE
struct IMDCustomDataSource;
#endif
// ENUM for marking bit
enum
{
InvalidMarkedBit = 0x00000000,
ModuleMarkedBit = 0x00000001,
TypeRefMarkedBit = 0x00000002,
TypeDefMarkedBit = 0x00000004,
FieldMarkedBit = 0x00000008,
MethodMarkedBit = 0x00000010,
ParamMarkedBit = 0x00000020,
MemberRefMarkedBit = 0x00000040,
CustomAttributeMarkedBit = 0x00000080,
DeclSecurityMarkedBit = 0x00000100,
SignatureMarkedBit = 0x00000200,
EventMarkedBit = 0x00000400,
PropertyMarkedBit = 0x00000800,
MethodImplMarkedBit = 0x00001000,
ModuleRefMarkedBit = 0x00002000,
TypeSpecMarkedBit = 0x00004000,
InterfaceImplMarkedBit = 0x00008000,
AssemblyRefMarkedBit = 0x00010000,
MethodSpecMarkedBit = 0x00020000,
};
// entry for marking UserString
struct FilterUserStringEntry
{
DWORD m_tkString;
bool m_fMarked;
};
class FilterTable : public CDynArray<DWORD>
{
public:
FilterTable() { m_daUserStringMarker = NULL; }
~FilterTable();
__checkReturn FORCEINLINE HRESULT MarkTypeRef(mdToken tk) { return MarkToken(tk, TypeRefMarkedBit); }
__checkReturn FORCEINLINE HRESULT MarkTypeDef(mdToken tk) { return MarkToken(tk, TypeDefMarkedBit); }
__checkReturn FORCEINLINE HRESULT MarkField(mdToken tk) { return MarkToken(tk, FieldMarkedBit); }
__checkReturn FORCEINLINE HRESULT MarkMethod(mdToken tk) { return MarkToken(tk, MethodMarkedBit); }
__checkReturn FORCEINLINE HRESULT MarkParam(mdToken tk) { return MarkToken(tk, ParamMarkedBit); }
__checkReturn FORCEINLINE HRESULT MarkMemberRef(mdToken tk) { return MarkToken(tk, MemberRefMarkedBit); }
__checkReturn FORCEINLINE HRESULT MarkCustomAttribute(mdToken tk) { return MarkToken(tk, CustomAttributeMarkedBit); }
__checkReturn FORCEINLINE HRESULT MarkDeclSecurity(mdToken tk) { return MarkToken(tk, DeclSecurityMarkedBit); }
__checkReturn FORCEINLINE HRESULT MarkSignature(mdToken tk) { return MarkToken(tk, SignatureMarkedBit); }
__checkReturn FORCEINLINE HRESULT MarkEvent(mdToken tk) { return MarkToken(tk, EventMarkedBit); }
__checkReturn FORCEINLINE HRESULT MarkProperty(mdToken tk) { return MarkToken(tk, PropertyMarkedBit); }
__checkReturn FORCEINLINE HRESULT MarkMethodImpl(RID rid)
{
return MarkToken(TokenFromRid(rid, TBL_MethodImpl << 24), MethodImplMarkedBit);
}
__checkReturn FORCEINLINE HRESULT MarkModuleRef(mdToken tk) { return MarkToken(tk, ModuleRefMarkedBit); }
__checkReturn FORCEINLINE HRESULT MarkTypeSpec(mdToken tk) { return MarkToken(tk, TypeSpecMarkedBit); }
__checkReturn FORCEINLINE HRESULT MarkInterfaceImpl(mdToken tk) { return MarkToken(tk, InterfaceImplMarkedBit); }
__checkReturn FORCEINLINE HRESULT MarkAssemblyRef(mdToken tk) { return MarkToken(tk, AssemblyRefMarkedBit); }
__checkReturn FORCEINLINE HRESULT MarkMethodSpec(mdToken tk) { return MarkToken(tk, MethodSpecMarkedBit); }
// It may look inconsistent but it is because taht UserString an offset to the heap.
// We don't want to grow the FilterTable to the size of the UserString heap.
// So we use the heap's marking system instead...
//
__checkReturn HRESULT MarkUserString(mdString str);
__checkReturn HRESULT MarkNewUserString(mdString str);
FORCEINLINE bool IsTypeRefMarked(mdToken tk) { return IsTokenMarked(tk, TypeRefMarkedBit); }
FORCEINLINE bool IsTypeDefMarked(mdToken tk) { return IsTokenMarked(tk, TypeDefMarkedBit); }
FORCEINLINE bool IsFieldMarked(mdToken tk) { return IsTokenMarked(tk, FieldMarkedBit); }
FORCEINLINE bool IsMethodMarked(mdToken tk) { return IsTokenMarked(tk, MethodMarkedBit); }
FORCEINLINE bool IsParamMarked(mdToken tk) { return IsTokenMarked(tk, ParamMarkedBit); }
FORCEINLINE bool IsMemberRefMarked(mdToken tk) { return IsTokenMarked(tk, MemberRefMarkedBit); }
FORCEINLINE bool IsCustomAttributeMarked(mdToken tk) { return IsTokenMarked(tk, CustomAttributeMarkedBit); }
FORCEINLINE bool IsDeclSecurityMarked(mdToken tk) { return IsTokenMarked(tk, DeclSecurityMarkedBit); }
FORCEINLINE bool IsSignatureMarked(mdToken tk) { return IsTokenMarked(tk, SignatureMarkedBit); }
FORCEINLINE bool IsEventMarked(mdToken tk) { return IsTokenMarked(tk, EventMarkedBit); }
FORCEINLINE bool IsPropertyMarked(mdToken tk) { return IsTokenMarked(tk, PropertyMarkedBit); }
FORCEINLINE bool IsMethodImplMarked(RID rid)
{
return IsTokenMarked(TokenFromRid(rid, TBL_MethodImpl << 24), MethodImplMarkedBit);
}
FORCEINLINE bool IsModuleRefMarked(mdToken tk) { return IsTokenMarked(tk, ModuleRefMarkedBit); }
FORCEINLINE bool IsTypeSpecMarked(mdToken tk) { return IsTokenMarked(tk, TypeSpecMarkedBit); }
FORCEINLINE bool IsInterfaceImplMarked(mdToken tk){ return IsTokenMarked(tk, InterfaceImplMarkedBit); }
FORCEINLINE bool IsAssemblyRefMarked(mdToken tk){ return IsTokenMarked(tk, AssemblyRefMarkedBit); }
FORCEINLINE bool IsMethodSpecMarked(mdToken tk){ return IsTokenMarked(tk, MethodSpecMarkedBit); }
bool IsUserStringMarked(mdString str);
__checkReturn HRESULT UnmarkAll(CMiniMdRW *pMiniMd, ULONG ulSize);
__checkReturn HRESULT MarkAll(CMiniMdRW *pMiniMd, ULONG ulSize);
bool IsTokenMarked(mdToken);
__checkReturn FORCEINLINE HRESULT UnmarkTypeDef(mdToken tk) { return UnmarkToken(tk, TypeDefMarkedBit); }
__checkReturn FORCEINLINE HRESULT UnmarkField(mdToken tk) { return UnmarkToken(tk, FieldMarkedBit); }
__checkReturn FORCEINLINE HRESULT UnmarkMethod(mdToken tk) { return UnmarkToken(tk, MethodMarkedBit); }
__checkReturn FORCEINLINE HRESULT UnmarkCustomAttribute(mdToken tk) { return UnmarkToken(tk, CustomAttributeMarkedBit); }
private:
CDynArray<FilterUserStringEntry> *m_daUserStringMarker;
bool IsTokenMarked(mdToken tk, DWORD bitMarked);
__checkReturn HRESULT MarkToken(mdToken tk, DWORD bit);
__checkReturn HRESULT UnmarkToken(mdToken tk, DWORD bit);
}; // class FilterTable : public CDynArray<DWORD>
class CMiniMdRW;
//*****************************************************************************
// This class is used to keep a list of RID. This list of RID can be sorted
// base on the m_ixCol's value of the m_ixTbl table.
//*****************************************************************************
class VirtualSort
{
public:
void Init(ULONG ixTbl, ULONG ixCol, CMiniMdRW *pMiniMd);
void Uninit();
TOKENMAP *m_pMap; // RID for m_ixTbl table. Sorted by on the ixCol
bool m_isMapValid;
ULONG m_ixTbl; // Table this is a sorter for.
ULONG m_ixCol; // Key column in the table.
CMiniMdRW *m_pMiniMd; // The MiniMd with the data.
__checkReturn
HRESULT Sort();
private:
mdToken m_tkBuf;
__checkReturn
HRESULT SortRange(int iLeft, int iRight);
public:
__checkReturn
HRESULT Compare(
RID iLeft, // First item to compare.
RID iRight, // Second item to compare.
int *pnResult); // -1, 0, or 1
private:
FORCEINLINE void Swap(
RID iFirst,
RID iSecond)
{
if ( iFirst == iSecond ) return;
m_tkBuf = *(m_pMap->Get(iFirst));
*(m_pMap->Get(iFirst)) = *(m_pMap->Get(iSecond));
*(m_pMap->Get(iSecond)) = m_tkBuf;
}
}; // class VirtualSort
class ReorderData
{
public:
typedef enum
{
MinReorderBucketType=0, // bucket# shouldn't be less than this value
Undefined=0, // use this for initialization
Duplicate=1, // duplicate string
ProfileData=2, // bucket# for IBC data
PublicData=3, // bucket# for public data
OtherData=4, // bucket# for other data
NonPublicData=5, // bucket# for non-public data
MaxReorderBucketType=255 // bucket# shouldn't exceeed this value
} ReorderBucketType;
};
typedef CMetaDataHashBase CMemberRefHash;
typedef CMetaDataHashBase CLookUpHash;
class MDTOKENMAP;
class MDInternalRW;
class UTSemReadWrite;
template <class MiniMd> class CLiteWeightStgdb;
//*****************************************************************************
// Read/Write MiniMd.
//*****************************************************************************
class CMiniMdRW : public CMiniMdTemplate<CMiniMdRW>
{
public:
friend class CLiteWeightStgdb<CMiniMdRW>;
friend class CLiteWeightStgdbRW;
friend class CMiniMdTemplate<CMiniMdRW>;
friend class CQuickSortMiniMdRW;
friend class VirtualSort;
friend class MDInternalRW;
friend class RegMeta;
friend class FilterTable;
friend class ImportHelper;
friend class VerifyLayoutsMD;
CMiniMdRW();
~CMiniMdRW();
__checkReturn
HRESULT InitNew();
__checkReturn
HRESULT InitOnMem(const void *pBuf, ULONG ulBufLen, int bReadOnly);
__checkReturn
HRESULT PostInit(int iLevel);
__checkReturn
HRESULT InitPoolOnMem(int iPool, void *pbData, ULONG cbData, int bReadOnly);
__checkReturn
HRESULT InitOnRO(CMiniMd *pMd, int bReadOnly);
#ifdef FEATURE_METADATA_CUSTOM_DATA_SOURCE
__checkReturn
HRESULT InitOnCustomDataSource(IMDCustomDataSource* pDataSource);
#endif
__checkReturn
HRESULT ConvertToRW();
__checkReturn
HRESULT GetSaveSize(
CorSaveSize fSave,
UINT32 *pcbSize,
DWORD *pbCompressed,
MetaDataReorderingOptions reorderingOptions = NoReordering);
int IsPoolEmpty(int iPool);
__checkReturn
HRESULT GetPoolSaveSize(int iPool, UINT32 *pcbSize);
__checkReturn
HRESULT SaveTablesToStream(IStream *pIStream, MetaDataReorderingOptions reorderingOptions);
__checkReturn
HRESULT SavePoolToStream(int iPool, IStream *pIStream);
__checkReturn
HRESULT SaveDone();
__checkReturn
HRESULT SetHandler(IUnknown *pIUnk);
__checkReturn
HRESULT SetOption(OptionValue *pOptionValue);
__checkReturn
HRESULT GetOption(OptionValue *pOptionValue);
static ULONG GetTableForToken(mdToken tkn);
static mdToken GetTokenForTable(ULONG ixTbl);
FORCEINLINE static ULONG TblFromRecId(ULONG ul) { return (ul >> 24)&0x7f; }
FORCEINLINE static ULONG RidFromRecId(ULONG ul) { return ul & 0xffffff; }
FORCEINLINE static ULONG RecIdFromRid(ULONG rid, ULONG ixTbl) { return rid | ((ixTbl|0x80) << 24); }
FORCEINLINE static int IsRecId(ULONG ul) { return (ul & 0x80000000) != 0;}
// Place in every API function before doing any allocations.
__checkReturn
FORCEINLINE HRESULT PreUpdate()
{
if (m_eGrow == eg_grow)
{
return ExpandTables();
}
return S_OK;
}
__checkReturn
HRESULT AddRecord(
UINT32 nTableIndex,
void **ppRow,
RID *pRid);
__checkReturn
FORCEINLINE HRESULT PutCol(ULONG ixTbl, ULONG ixCol, void *pRecord, ULONG uVal)
{ _ASSERTE(ixTbl < TBL_COUNT); _ASSERTE(ixCol < m_TableDefs[ixTbl].m_cCols);
return PutCol(m_TableDefs[ixTbl].m_pColDefs[ixCol], pRecord, uVal);
} // HRESULT CMiniMdRW::PutCol()
__checkReturn
HRESULT PutString(ULONG ixTbl, ULONG ixCol, void *pRecord, LPCSTR szString);
__checkReturn
HRESULT PutStringW(ULONG ixTbl, ULONG ixCol, void *pRecord, LPCWSTR wszString);
__checkReturn
HRESULT PutGuid(ULONG ixTbl, ULONG ixCol, void *pRecord, REFGUID guid);
__checkReturn
HRESULT ChangeMvid(REFGUID newMvid);
__checkReturn
HRESULT PutToken(ULONG ixTbl, ULONG ixCol, void *pRecord, mdToken tk);
__checkReturn
HRESULT PutBlob(ULONG ixTbl, ULONG ixCol, void *pRecord, const void *pvData, ULONG cbData);
__checkReturn
HRESULT PutUserString(MetaData::DataBlob data, UINT32 *pnIndex)
{ return m_UserStringHeap.AddBlob(data, pnIndex); }
ULONG GetCol(ULONG ixTbl, ULONG ixCol, void *pRecord);
mdToken GetToken(ULONG ixTbl, ULONG ixCol, void *pRecord);
// Add a record to a table, and return a typed XXXRec *.
// #undef AddTblRecord
#define AddTblRecord(tbl) \
__checkReturn HRESULT Add##tbl##Record(tbl##Rec **ppRow, RID *pnRowIndex) \
{ return AddRecord(TBL_##tbl, reinterpret_cast<void **>(ppRow), pnRowIndex); }
AddTblRecord(Module)
AddTblRecord(TypeRef)
__checkReturn HRESULT AddTypeDefRecord( // Specialized implementation.
TypeDefRec **ppRow,
RID *pnRowIndex);
AddTblRecord(Field)
__checkReturn HRESULT AddMethodRecord( // Specialized implementation.
MethodRec **ppRow,
RID *pnRowIndex);
AddTblRecord(Param)
AddTblRecord(InterfaceImpl)
AddTblRecord(MemberRef)
AddTblRecord(Constant)
AddTblRecord(CustomAttribute)
AddTblRecord(FieldMarshal)
AddTblRecord(DeclSecurity)
AddTblRecord(ClassLayout)
AddTblRecord(FieldLayout)
AddTblRecord(StandAloneSig)
__checkReturn HRESULT AddEventMapRecord( // Specialized implementation.
EventMapRec **ppRow,
RID *pnRowIndex);
AddTblRecord(Event)
__checkReturn HRESULT AddPropertyMapRecord( // Specialized implementation.
PropertyMapRec **ppRow,
RID *pnRowIndex);
AddTblRecord(Property)
AddTblRecord(MethodSemantics)
AddTblRecord(MethodImpl)
AddTblRecord(ModuleRef)
AddTblRecord(FieldPtr)
AddTblRecord(MethodPtr)
AddTblRecord(ParamPtr)
AddTblRecord(PropertyPtr)
AddTblRecord(EventPtr)
AddTblRecord(ENCLog)
AddTblRecord(TypeSpec)
AddTblRecord(ImplMap)
AddTblRecord(ENCMap)
AddTblRecord(FieldRVA)
// Assembly Tables.
AddTblRecord(Assembly)
AddTblRecord(AssemblyProcessor)
AddTblRecord(AssemblyOS)
AddTblRecord(AssemblyRef)
AddTblRecord(AssemblyRefProcessor)
AddTblRecord(AssemblyRefOS)
AddTblRecord(File)
AddTblRecord(ExportedType)
AddTblRecord(ManifestResource)
AddTblRecord(NestedClass)
AddTblRecord(GenericParam)
AddTblRecord(MethodSpec)
AddTblRecord(GenericParamConstraint)
#ifdef FEATURE_METADATA_EMIT_PORTABLE_PDB
AddTblRecord(Document)
AddTblRecord(MethodDebugInformation)
AddTblRecord(LocalScope)
AddTblRecord(LocalVariable)
AddTblRecord(LocalConstant)
AddTblRecord(ImportScope)
// TODO:
// AddTblRecord(StateMachineMethod)
// AddTblRecord(CustomDebugInformation)
#endif // FEATURE_METADATA_EMIT_PORTABLE_PDB
// Specialized AddXxxToYyy() functions.
__checkReturn HRESULT AddMethodToTypeDef(RID td, RID md);
__checkReturn HRESULT AddFieldToTypeDef(RID td, RID md);
__checkReturn HRESULT AddParamToMethod(RID md, RID pd);
__checkReturn HRESULT AddPropertyToPropertyMap(RID pmd, RID pd);
__checkReturn HRESULT AddEventToEventMap(ULONG emd, RID ed);
// does the MiniMdRW has the indirect tables, such as FieldPtr, MethodPtr
FORCEINLINE int HasIndirectTable(ULONG ix)
{ if (g_PtrTableIxs[ix].m_ixtbl < TBL_COUNT) return GetCountRecs(g_PtrTableIxs[ix].m_ixtbl); return 0;}
FORCEINLINE int IsVsMapValid(ULONG ixTbl)
{ _ASSERTE(ixTbl<TBL_COUNT); return (m_pVS[ixTbl] && m_pVS[ixTbl]->m_isMapValid); }
// translate index returned by getMethodListOfTypeDef to a rid into Method table
__checkReturn
FORCEINLINE HRESULT GetMethodRid(ULONG index, RID *pRid)
{
HRESULT hr;
if (HasIndirectTable(TBL_Method))
{
MethodPtrRec *pMethodPtrRecord;
IfFailGo(GetMethodPtrRecord(index, &pMethodPtrRecord));
*pRid = getMethodOfMethodPtr(pMethodPtrRecord);
}
else
{
*pRid = index;
}
return S_OK;
ErrExit:
*pRid = 0;
return hr;
}
// translate index returned by getFieldListOfTypeDef to a rid into Field table
__checkReturn
FORCEINLINE HRESULT GetFieldRid(ULONG index, RID *pRid)
{
HRESULT hr;
if (HasIndirectTable(TBL_Field))
{
FieldPtrRec *pFieldPtrRecord;
IfFailGo(GetFieldPtrRecord(index, &pFieldPtrRecord));
*pRid = getFieldOfFieldPtr(pFieldPtrRecord);
}
else
{
*pRid = index;
}
return S_OK;
ErrExit:
*pRid = 0;
return hr;
}
// translate index returned by getParamListOfMethod to a rid into Param table
__checkReturn
FORCEINLINE HRESULT GetParamRid(ULONG index, RID *pRid)
{
HRESULT hr;
if (HasIndirectTable(TBL_Param))
{
ParamPtrRec *pParamPtrRecord;
IfFailGo(GetParamPtrRecord(index, &pParamPtrRecord));
*pRid = getParamOfParamPtr(pParamPtrRecord);
}
else
{
*pRid = index;
}
return S_OK;
ErrExit:
*pRid = 0;
return hr;
}
// translate index returned by getEventListOfEventMap to a rid into Event table
__checkReturn
FORCEINLINE HRESULT GetEventRid(ULONG index, RID *pRid)
{
HRESULT hr;
if (HasIndirectTable(TBL_Event))
{
EventPtrRec *pEventPtrRecord;
IfFailGo(GetEventPtrRecord(index, &pEventPtrRecord));
*pRid = getEventOfEventPtr(pEventPtrRecord);
}
else
{
*pRid = index;
}
return S_OK;
ErrExit:
*pRid = 0;
return hr;
}
// translate index returned by getPropertyListOfPropertyMap to a rid into Property table
__checkReturn
FORCEINLINE HRESULT GetPropertyRid(ULONG index, RID *pRid)
{
HRESULT hr;
if (HasIndirectTable(TBL_Property))
{
PropertyPtrRec *pPropertyPtrRecord;
IfFailGo(GetPropertyPtrRecord(index, &pPropertyPtrRecord));
*pRid = getPropertyOfPropertyPtr(pPropertyPtrRecord);
}
else
{
*pRid = index;
}
return S_OK;
ErrExit:
*pRid = 0;
return hr;
}
// Convert a pseudo-RID from a Virtual Sort into a real RID.
FORCEINLINE ULONG GetRidFromVirtualSort(ULONG ixTbl, ULONG index)
{ return IsVsMapValid(ixTbl) ? *(m_pVS[ixTbl]->m_pMap->Get(index)) : index; }
// Index returned by GetInterfaceImplForTypeDef. It could be index to VirtualSort table
// or directly to InterfaceImpl
FORCEINLINE ULONG GetInterfaceImplRid(ULONG index)
{ return GetRidFromVirtualSort(TBL_InterfaceImpl, index); }
// Index returned by GetGenericParamForToken. It could be index to VirtualSort table
// or directly to GenericParam
FORCEINLINE ULONG GetGenericParamRid(ULONG index)
{ return GetRidFromVirtualSort(TBL_GenericParam, index); }
// Index returned by GetGenericParamConstraintForToken. It could be index to VirtualSort table
// or directly to GenericParamConstraint
FORCEINLINE ULONG GetGenericParamConstraintRid(ULONG index)
{ return GetRidFromVirtualSort(TBL_GenericParamConstraint, index); }
// Index returned by GetDeclSecurityForToken. It could be index to VirtualSort table
// or directly to DeclSecurity
FORCEINLINE ULONG GetDeclSecurityRid(ULONG index)
{ return GetRidFromVirtualSort(TBL_DeclSecurity, index); }
// Index returned by GetCustomAttributeForToken. It could be index to VirtualSort table
// or directly to CustomAttribute
FORCEINLINE ULONG GetCustomAttributeRid(ULONG index)
{ return GetRidFromVirtualSort(TBL_CustomAttribute, index); }
// add method, field, property, event, param to the map table
__checkReturn HRESULT AddMethodToLookUpTable(mdMethodDef md, mdTypeDef td);
__checkReturn HRESULT AddFieldToLookUpTable(mdFieldDef fd, mdTypeDef td);
__checkReturn HRESULT AddPropertyToLookUpTable(mdProperty pr, mdTypeDef td);
__checkReturn HRESULT AddEventToLookUpTable(mdEvent ev, mdTypeDef td);
__checkReturn HRESULT AddParamToLookUpTable(mdParamDef pd, mdMethodDef md);
// look up the parent of method, field, property, event, or param
__checkReturn HRESULT FindParentOfMethodHelper(mdMethodDef md, mdTypeDef *ptd);
__checkReturn HRESULT FindParentOfFieldHelper(mdFieldDef fd, mdTypeDef *ptd);
__checkReturn HRESULT FindParentOfPropertyHelper(mdProperty pr, mdTypeDef *ptd);
__checkReturn HRESULT FindParentOfEventHelper(mdEvent ev, mdTypeDef *ptd);
__checkReturn HRESULT FindParentOfParamHelper(mdParamDef pd, mdMethodDef *pmd);
bool IsMemberDefHashPresent() { return m_pMemberDefHash != NULL; }
// Result of hash search
enum HashSearchResult
{
Found, // Item was found.
NotFound, // Item not found.
NoTable // Table hasn't been built.
};
// Create MemberRef hash table.
__checkReturn
HRESULT CreateMemberRefHash();
// Add a new MemberRef to the hash table.
__checkReturn
HRESULT AddMemberRefToHash( // Return code.
mdMemberRef mr); // Token of new MemberRef.
// If the hash is built, search for the item. Ignore token *ptkMemberRef.
HashSearchResult FindMemberRefFromHash(
mdToken tkParent, // Parent token.
LPCUTF8 szName, // Name of item.
PCCOR_SIGNATURE pvSigBlob, // Signature.
ULONG cbSigBlob, // Size of signature.
mdMemberRef * ptkMemberRef); // IN: Ignored token. OUT: Return if found.
//*************************************************************************
// Check a given mr token to see if this one is a match.
//*************************************************************************
__checkReturn
HRESULT CompareMemberRefs( // S_OK match, S_FALSE no match.
mdMemberRef mr, // Token to check.
mdToken tkPar, // Parent token.
LPCUTF8 szNameUtf8, // Name of item.
PCCOR_SIGNATURE pvSigBlob, // Signature.
ULONG cbSigBlob); // Size of signature.
// Add a new MemberDef to the hash table.
__checkReturn
HRESULT AddMemberDefToHash(
mdToken tkMember, // Token of new def. It can be MethodDef or FieldDef
mdToken tkParent); // Parent token.
// Create MemberDef Hash
__checkReturn
HRESULT CreateMemberDefHash();
// If the hash is built, search for the item. Ignore token *ptkMember.
HashSearchResult FindMemberDefFromHash(
mdToken tkParent, // Parent token.
LPCUTF8 szName, // Name of item.
PCCOR_SIGNATURE pvSigBlob, // Signature.
ULONG cbSigBlob, // Size of signature.
mdToken * ptkMember); // IN: Ignored token. OUT: Return if found. It can be MethodDef or FieldDef
//*************************************************************************
// Check a given Method/Field token to see if this one is a match.
//*************************************************************************
__checkReturn
HRESULT CompareMemberDefs( // S_OK match, S_FALSE no match.
mdToken tkMember, // Token to check. It can be MethodDef or FieldDef
mdToken tkParent, // Parent token recorded in the hash entry
mdToken tkPar, // Parent token.
LPCUTF8 szNameUtf8, // Name of item.
PCCOR_SIGNATURE pvSigBlob, // Signature.
ULONG cbSigBlob); // Size of signature.
//*************************************************************************
// Add a new CustomAttributes to the hash table.
//*************************************************************************
__checkReturn
HRESULT AddCustomAttributesToHash( // Return code.
mdCustomAttribute cv) // Token of new guy.
{ return GenericAddToHash(TBL_CustomAttribute, CustomAttributeRec::COL_Parent, RidFromToken(cv)); }
inline ULONG HashMemberRef(mdToken tkPar, LPCUTF8 szName)
{
ULONG l = HashBytes((const BYTE *) &tkPar, sizeof(mdToken)) + HashStringA(szName);
return (l);
}
inline ULONG HashMemberDef(mdToken tkPar, LPCUTF8 szName)
{
return HashMemberRef(tkPar, szName);
}
// helper to calculate the hash value given a token
inline ULONG HashCustomAttribute(mdToken tkObject)
{
return HashToken(tkObject);
}
DAC_ALIGNAS(CMiniMdTemplate<CMiniMdRW>) // Align the first member to the alignment of the base class
CMemberRefHash *m_pMemberRefHash;
// Hash table for Methods and Fields
CMemberDefHash *m_pMemberDefHash;
// helper to calculate the hash value given a pair of tokens
inline ULONG HashToken(mdToken tkObject)
{
ULONG l = HashBytes((const BYTE *) &tkObject, sizeof(mdToken));
return (l);
}
//*************************************************************************
// Add a new FieldMarhsal Rid to the hash table.
//*************************************************************************
__checkReturn
HRESULT AddFieldMarshalToHash( // Return code.
RID rid) // Token of new guy.
{ return GenericAddToHash(TBL_FieldMarshal, FieldMarshalRec::COL_Parent, rid); }
//*************************************************************************
// Add a new Constant Rid to the hash table.
//*************************************************************************
__checkReturn
HRESULT AddConstantToHash( // Return code.
RID rid) // Token of new guy.
{ return GenericAddToHash(TBL_Constant, ConstantRec::COL_Parent, rid); }
//*************************************************************************
// Add a new MethodSemantics Rid to the hash table.
//*************************************************************************
__checkReturn
HRESULT AddMethodSemanticsToHash( // Return code.
RID rid) // Token of new guy.
{ return GenericAddToHash(TBL_MethodSemantics, MethodSemanticsRec::COL_Association, rid); }
//*************************************************************************
// Add a new ClassLayout Rid to the hash table.
//*************************************************************************
__checkReturn
HRESULT AddClassLayoutToHash( // Return code.
RID rid) // Token of new guy.
{ return GenericAddToHash(TBL_ClassLayout, ClassLayoutRec::COL_Parent, rid); }
//*************************************************************************
// Add a new FieldLayout Rid to the hash table.
//*************************************************************************
__checkReturn
HRESULT AddFieldLayoutToHash( // Return code.
RID rid) // Token of new guy.
{ return GenericAddToHash(TBL_FieldLayout, FieldLayoutRec::COL_Field, rid); }
//*************************************************************************
// Add a new ImplMap Rid to the hash table.
//*************************************************************************
__checkReturn
HRESULT AddImplMapToHash( // Return code.
RID rid) // Token of new guy.
{ return GenericAddToHash(TBL_ImplMap, ImplMapRec::COL_MemberForwarded, rid); }
//*************************************************************************
// Add a new FieldRVA Rid to the hash table.
//*************************************************************************
__checkReturn
HRESULT AddFieldRVAToHash( // Return code.
RID rid) // Token of new guy.
{ return GenericAddToHash(TBL_FieldRVA, FieldRVARec::COL_Field, rid); }
//*************************************************************************
// Add a new nested class Rid to the hash table.
//*************************************************************************
__checkReturn
HRESULT AddNestedClassToHash( // Return code.
RID rid) // Token of new guy.
{ return GenericAddToHash(TBL_NestedClass, NestedClassRec::COL_NestedClass, rid); }
//*************************************************************************
// Add a new MethodImpl Rid to the hash table.
//*************************************************************************
__checkReturn
HRESULT AddMethodImplToHash( // Return code.
RID rid) // Token of new guy.
{ return GenericAddToHash(TBL_MethodImpl, MethodImplRec::COL_Class, rid); }
//*************************************************************************
// Build a hash table for the specified table if the size exceed the thresholds.
//*************************************************************************
__checkReturn
HRESULT GenericBuildHashTable( // Return code.
ULONG ixTbl, // Table with hash
ULONG ixCol); // col that we hash.
//*************************************************************************
// Add a rid from a table into a hash
//*************************************************************************
__checkReturn
HRESULT GenericAddToHash( // Return code.
ULONG ixTbl, // Table with hash
ULONG ixCol, // col that we hash.
RID rid); // new row of the table.
//*************************************************************************
// Add a rid from a table into a hash
//*************************************************************************
__checkReturn
HRESULT GenericFindWithHash( // Return code.
ULONG ixTbl, // Table with hash
ULONG ixCol, // col that we hash.
mdToken tkTarget, // token to be find in the hash
RID *pFoundRid);
// look up hash table for tokenless tables.
// They are constant, FieldMarshal, MethodSemantics, ClassLayout, FieldLayout, ImplMap, FieldRVA, NestedClass, and MethodImpl
CLookUpHash * m_pLookUpHashes[TBL_COUNT];
//*************************************************************************
// Hash for named items.
//*************************************************************************
__checkReturn
HRESULT AddNamedItemToHash( // Return code.
ULONG ixTbl, // Table with the new item.
mdToken tk, // Token of new guy.
LPCUTF8 szName, // Name of item.
mdToken tkParent); // Token of parent, if any.
HashSearchResult FindNamedItemFromHash(
ULONG ixTbl, // Table with the item.
LPCUTF8 szName, // Name of item.
mdToken tkParent, // Token of parent, if any.
mdToken * ptk); // Return if found.
__checkReturn
HRESULT CompareNamedItems( // S_OK match, S_FALSE no match.
ULONG ixTbl, // Table with the item.
mdToken tk, // Token to check.
LPCUTF8 szName, // Name of item.
mdToken tkParent); // Token of parent, if any.
FORCEINLINE ULONG HashNamedItem(mdToken tkPar, LPCUTF8 szName)
{ return HashBytes((const BYTE *) &tkPar, sizeof(mdToken)) + HashStringA(szName); }
CMetaDataHashBase *m_pNamedItemHash;
//*****************************************************************************
// IMetaModelCommon - RW specific versions for some of the functions.
//*****************************************************************************
__checkReturn
virtual HRESULT CommonGetEnclosingClassOfTypeDef(
mdTypeDef td,
mdTypeDef *ptkEnclosingTypeDef)
{
_ASSERTE(ptkEnclosingTypeDef != NULL);
HRESULT hr;
NestedClassRec *pRec;
RID iRec;
IfFailRet(FindNestedClassHelper(td, &iRec));
if (iRec == 0)
{
*ptkEnclosingTypeDef = mdTypeDefNil;
return S_OK;
}
IfFailRet(GetNestedClassRecord(iRec, &pRec));
*ptkEnclosingTypeDef = getEnclosingClassOfNestedClass(pRec);
return S_OK;
}
__checkReturn
HRESULT CommonEnumCustomAttributeByName( // S_OK or error.
mdToken tkObj, // [IN] Object with Custom Attribute.
LPCUTF8 szName, // [IN] Name of desired Custom Attribute.
bool fStopAtFirstFind, // [IN] just find the first one
HENUMInternal* phEnum); // enumerator to fill up
__checkReturn
HRESULT CommonGetCustomAttributeByNameEx( // S_OK or error.
mdToken tkObj, // [IN] Object with Custom Attribute.
LPCUTF8 szName, // [IN] Name of desired Custom Attribute.
mdCustomAttribute *ptkCA, // [OUT] put custom attribute token here
const void **ppData, // [OUT] Put pointer to data here.
ULONG *pcbData); // [OUT] Put size of data here.
//*****************************************************************************
// Find helper for a constant.
//*****************************************************************************
__checkReturn
HRESULT FindConstantHelper( // return index to the constant table
mdToken tkParent, // Parent token. Can be ParamDef, FieldDef, or Property.
RID *pFoundRid);
//*****************************************************************************
// Find helper for a FieldMarshal.
//*****************************************************************************
__checkReturn
HRESULT FindFieldMarshalHelper( // return index to the field marshal table
mdToken tkParent, // Parent token. Can be a FieldDef or ParamDef.
RID *pFoundRid);
//*****************************************************************************
// Find helper for a method semantics.
//*****************************************************************************
__checkReturn
HRESULT FindMethodSemanticsHelper( // return HRESULT
mdToken tkAssociate, // Event or property token
HENUMInternal *phEnum); // fill in the enum
//*****************************************************************************
// Find helper for a method semantics given a associate and semantics.
// This will look up methodsemantics based on its status!
// Return CLDB_E_RECORD_NOTFOUND if cannot find the matching one
//*****************************************************************************
__checkReturn
HRESULT FindAssociateHelper(// return HRESULT
mdToken tkAssociate, // Event or property token
DWORD dwSemantics, // [IN] given a associate semantics(setter, getter, testdefault, reset)
RID *pRid); // [OUT] return matching row index here
//*****************************************************************************
// Find helper for a MethodImpl.
//*****************************************************************************
__checkReturn
HRESULT FindMethodImplHelper(// return HRESULT
mdTypeDef td, // TypeDef token for the Class.
HENUMInternal *phEnum); // fill in the enum
//*****************************************************************************
// Find helper for a GenericParams
//*****************************************************************************
__checkReturn
HRESULT FindGenericParamHelper( // Return HRESULT
mdToken tkOwner, // Token for the GenericParams' owner
HENUMInternal *phEnum); // Fill in the enum.
//*****************************************************************************
// Find helper for a Generic Constraints
//*****************************************************************************
__checkReturn
HRESULT FindGenericParamConstraintHelper( // Return HRESULT
mdGenericParam tkParam, // Token for the GenericParam
HENUMInternal *phEnum); // Fill in the enum.
//*****************************************************************************
// Find helper for a ClassLayout.
//*****************************************************************************
__checkReturn
HRESULT FindClassLayoutHelper( // return index to the ClassLayout table
mdTypeDef tkParent, // Parent token.
RID *pFoundRid);
//*****************************************************************************
// Find helper for a FieldLayout.
//*****************************************************************************
__checkReturn
HRESULT FindFieldLayoutHelper( // return index to the FieldLayout table
mdFieldDef tkField, // Token for the field.
RID *pFoundRid);
//*****************************************************************************
// Find helper for a ImplMap.
//*****************************************************************************
__checkReturn
HRESULT FindImplMapHelper( // return index to the constant table
mdToken tk, // Member forwarded token.
RID *pFoundRid);
//*****************************************************************************
// Find helper for a FieldRVA.
//*****************************************************************************
__checkReturn
HRESULT FindFieldRVAHelper( // return index to the FieldRVA table
mdFieldDef tkField, // Token for the field.
RID *pFoundRid);
//*****************************************************************************
// Find helper for a NestedClass.
//*****************************************************************************
__checkReturn
HRESULT FindNestedClassHelper( // return index to the NestedClass table
mdTypeDef tkClass, // Token for the NestedClass.
RID *pFoundRid);
//*****************************************************************************
// IMPORTANT!!!!!!!! Use these set of functions if you are dealing with RW rather
// getInterfaceImplsForTypeDef, getDeclSecurityForToken, etc.
// The following functions can deal with these tables when they are not sorted and
// build the VirtualSort tables for quick lookup.
//*****************************************************************************
__checkReturn
HRESULT GetInterfaceImplsForTypeDef(mdTypeDef td, RID *pRidStart, RID *pRidEnd = 0)
{
return LookUpTableByCol( RidFromToken(td), m_pVS[TBL_InterfaceImpl], pRidStart, pRidEnd);
}
__checkReturn
HRESULT GetGenericParamsForToken(mdToken tk, RID *pRidStart, RID *pRidEnd = 0)
{
return LookUpTableByCol(
encodeToken(RidFromToken(tk), TypeFromToken(tk), mdtTypeOrMethodDef, ARRAY_SIZE(mdtTypeOrMethodDef)),
m_pVS[TBL_GenericParam], pRidStart, pRidEnd);
}
__checkReturn
HRESULT GetGenericParamConstraintsForToken(mdToken tk, RID *pRidStart, RID *pRidEnd = 0)
{
return LookUpTableByCol( RidFromToken(tk),
m_pVS[TBL_GenericParamConstraint], pRidStart, pRidEnd);
}
__checkReturn
HRESULT GetMethodSpecsForToken(mdToken tk, RID *pRidStart, RID *pRidEnd = 0)
{
return LookUpTableByCol(
encodeToken(RidFromToken(tk), TypeFromToken(tk), mdtMethodDefOrRef, ARRAY_SIZE(mdtMethodDefOrRef)),
m_pVS[TBL_MethodSpec], pRidStart, pRidEnd);
}
__checkReturn
HRESULT GetDeclSecurityForToken(mdToken tk, RID *pRidStart, RID *pRidEnd = 0)
{
return LookUpTableByCol(
encodeToken(RidFromToken(tk), TypeFromToken(tk), mdtHasDeclSecurity, ARRAY_SIZE(mdtHasDeclSecurity)),
m_pVS[TBL_DeclSecurity],
pRidStart,
pRidEnd);
}
__checkReturn
HRESULT GetCustomAttributeForToken(mdToken tk, RID *pRidStart, RID *pRidEnd = 0)
{
return LookUpTableByCol(
encodeToken(RidFromToken(tk), TypeFromToken(tk), mdtHasCustomAttribute, ARRAY_SIZE(mdtHasCustomAttribute)),
m_pVS[TBL_CustomAttribute],
pRidStart,
pRidEnd);
}
__checkReturn
FORCEINLINE HRESULT GetUserString(ULONG nIndex, MetaData::DataBlob *pData)
{ return m_UserStringHeap.GetBlob(nIndex, pData); }
// Gets user string (*Data) at index (nIndex) and fills the index (*pnNextIndex) of the next user string
// in the heap.
// Returns S_OK and fills the string (*pData) and the next index (*pnNextIndex).
// Returns S_FALSE if the index (nIndex) is not valid user string index.
// Returns error code otherwise.
// Clears *pData and sets *pnNextIndex to 0 on error or S_FALSE.