-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
mdinternalro.cpp
3396 lines (2922 loc) · 126 KB
/
mdinternalro.cpp
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.
// ===========================================================================
// File: MDInternalRO.CPP
//
// Notes:
//
//
// ===========================================================================
#include "stdafx.h"
#include "mdinternalro.h"
#include "metamodelro.h"
#include "liteweightstgdb.h"
#include "corhlpr.h"
#include "../compiler/regmeta.h"
#include "caparser.h"
#ifdef FEATURE_METADATA_INTERNAL_APIS
__checkReturn
HRESULT _FillMDDefaultValue(
BYTE bType,
void const *pValue,
ULONG cbValue,
MDDefaultValue *pMDDefaultValue);
#ifndef DACCESS_COMPILE
__checkReturn
HRESULT TranslateSigHelper( // S_OK or error.
IMDInternalImport *pImport, // [IN] import scope.
IMDInternalImport *pAssemImport, // [IN] import assembly scope.
const void *pbHashValue, // [IN] hash value for the import assembly.
ULONG cbHashValue, // [IN] count of bytes in the hash value.
PCCOR_SIGNATURE pbSigBlob, // [IN] signature in the importing scope
ULONG cbSigBlob, // [IN] count of bytes of signature
IMetaDataAssemblyEmit *pAssemEmit, // [IN] assembly emit scope.
IMetaDataEmit *emit, // [IN] emit interface
CQuickBytes *pqkSigEmit, // [OUT] buffer to hold translated signature
ULONG *pcbSig); // [OUT] count of bytes in the translated signature
#endif //!DACCESS_COMPILE
__checkReturn
HRESULT GetInternalWithRWFormat(
LPVOID pData,
ULONG cbData,
DWORD flags, // [IN] MDInternal_OpenForRead or MDInternal_OpenForENC
REFIID riid, // [in] The interface desired.
void **ppIUnk); // [out] Return interface on success.
// forward declaration
__checkReturn
HRESULT MDApplyEditAndContinue( // S_OK or error.
IMDInternalImport **ppIMD, // [in, out] The metadata to be updated.
IMDInternalImportENC *pDeltaMD); // [in] The delta metadata.
//*****************************************************************************
// Constructor
//*****************************************************************************
MDInternalRO::MDInternalRO()
: m_pMethodSemanticsMap(0),
m_cRefs(1)
{
} // MDInternalRO::MDInternalRO
//*****************************************************************************
// Destructor
//*****************************************************************************
MDInternalRO::~MDInternalRO()
{
m_LiteWeightStgdb.Uninit();
if (m_pMethodSemanticsMap)
delete[] m_pMethodSemanticsMap;
m_pMethodSemanticsMap = 0;
} // MDInternalRO::~MDInternalRO
//*****************************************************************************
// IUnknown
//*****************************************************************************
ULONG MDInternalRO::AddRef()
{
return InterlockedIncrement(&m_cRefs);
} // MDInternalRO::AddRef
ULONG MDInternalRO::Release()
{
ULONG cRef = InterlockedDecrement(&m_cRefs);
if (cRef == 0)
delete this;
return cRef;
} // MDInternalRO::Release
__checkReturn
HRESULT MDInternalRO::QueryInterface(REFIID riid, void **ppUnk)
{
*ppUnk = 0;
if (riid == IID_IUnknown)
*ppUnk = this; // ! QI for IID_IUnknown must return MDInternalRO. ConvertRO2RW() has dependency on this.
else if (riid == IID_IMDInternalImport)
*ppUnk = (IMDInternalImport *)this;
else if (riid == IID_IMDCommon)
*ppUnk = (IMDCommon *)this;
else
return E_NOINTERFACE;
AddRef();
return S_OK;
} // MDInternalRO::QueryInterface
//*****************************************************************************
// Initialize
//*****************************************************************************
__checkReturn
HRESULT MDInternalRO::Init(
LPVOID pData, // points to meta data section in memory
ULONG cbData) // count of bytes in pData
{
m_tdModule = COR_GLOBAL_PARENT_TOKEN;
extern HRESULT _CallInitOnMemHelper(CLiteWeightStgdb<CMiniMd> *pStgdb, ULONG cbData, LPCVOID pData);
return _CallInitOnMemHelper(&m_LiteWeightStgdb, cbData, (BYTE*) pData);
} // MDInternalRO::Init
#ifndef DACCESS_COMPILE
//*****************************************************************************
// Given a scope, determine whether imported from a typelib.
//*****************************************************************************
__checkReturn
HRESULT MDInternalRO::TranslateSigWithScope(
IMDInternalImport* pAssemImport, // [IN] import assembly scope.
const void* pbHashValue, // [IN] hash value for the import assembly.
ULONG cbHashValue, // [IN] count of bytes in the hash value.
PCCOR_SIGNATURE pbSigBlob, // [IN] signature in the importing scope
ULONG cbSigBlob, // [IN] count of bytes of signature
IMetaDataAssemblyEmit* pAssemEmit, // [IN] assembly emit scope.
IMetaDataEmit* emit, // [IN] emit interface
CQuickBytes* pqkSigEmit, // [OUT] buffer to hold translated signature
ULONG* pcbSig) // [OUT] count of bytes in the translated signature
{
return TranslateSigHelper(
this,
pAssemImport,
pbHashValue,
cbHashValue,
pbSigBlob,
cbSigBlob,
pAssemEmit,
emit,
pqkSigEmit,
pcbSig);
} // MDInternalRO::TranslateSigWithScope
#endif // DACCESS_COMPILE
__checkReturn
HRESULT MDInternalRO::GetTypeDefRefTokenInTypeSpec(// return S_FALSE if enclosing type does not have a token
mdTypeSpec tkTypeSpec, // [IN] TypeSpec token to look at
mdToken *tkEnclosedToken) // [OUT] The enclosed type token
{
return m_LiteWeightStgdb.m_MiniMd.GetTypeDefRefTokenInTypeSpec(tkTypeSpec, tkEnclosedToken);
} // MDInternalRO::GetTypeDefRefTokenInTypeSpec
#ifndef DACCESS_COMPILE
//*****************************************************************************
// Given a scope, return the number of tokens in a given table
//*****************************************************************************
ULONG MDInternalRO::GetCountWithTokenKind( // return hresult
DWORD tkKind) // [IN] pass in the kind of token.
{
ULONG ulCount = m_LiteWeightStgdb.m_MiniMd.CommonGetRowCount(tkKind);
if (tkKind == mdtTypeDef)
{
// Remove global typedef from the count of typedefs (and handle the case where there is no global typedef)
if (ulCount > 0)
ulCount--;
}
return ulCount;
} // MDInternalRO::GetCountWithTokenKind
#endif //!DACCESS_COMPILE
//*******************************************************************************
// Enumerator helpers
//*******************************************************************************
//*****************************************************************************
// enumerator init for typedef
//*****************************************************************************
__checkReturn
HRESULT MDInternalRO::EnumTypeDefInit( // return hresult
HENUMInternal *phEnum) // [OUT] buffer to fill for enumerator data
{
HRESULT hr = NOERROR;
_ASSERTE(phEnum);
HENUMInternal::ZeroEnum(phEnum);
phEnum->m_tkKind = mdtTypeDef;
phEnum->m_EnumType = MDSimpleEnum;
phEnum->m_ulCount = m_LiteWeightStgdb.m_MiniMd.getCountTypeDefs();
// Skip over the global model typedef
//
// phEnum->u.m_ulCur : the current rid that is not yet enumerated
// phEnum->u.m_ulStart : the first rid that will be returned by enumerator
// phEnum->u.m_ulEnd : the last rid that will be returned by enumerator
phEnum->u.m_ulStart = phEnum->u.m_ulCur = 2;
phEnum->u.m_ulEnd = phEnum->m_ulCount + 1;
if (phEnum->m_ulCount > 0)
phEnum->m_ulCount --;
return hr;
} // MDInternalRO::EnumTypeDefInit
//*****************************************************************************
// Enumerator init for MethodImpl. The second HENUMInternal* parameter is
// only used for the R/W version of the MetaData.
//*****************************************************************************
__checkReturn
HRESULT MDInternalRO::EnumMethodImplInit( // return hresult
mdTypeDef td, // [IN] TypeDef over which to scope the enumeration.
HENUMInternal *phEnumBody, // [OUT] buffer to fill for enumerator data for MethodBody tokens.
HENUMInternal *phEnumDecl) // [OUT] buffer to fill for enumerator data for MethodDecl tokens.
{
return EnumInit(TBL_MethodImpl << 24, td, phEnumBody);
} // MDInternalRO::EnumMethodImplInit
//*****************************************************************************
// get the number of MethodImpls in a scope
//*****************************************************************************
ULONG MDInternalRO::EnumMethodImplGetCount(
HENUMInternal *phEnumBody, // [IN] MethodBody enumerator.
HENUMInternal *phEnumDecl) // [IN] MethodDecl enumerator.
{
_ASSERTE(phEnumBody && ((phEnumBody->m_tkKind >> 24) == TBL_MethodImpl));
return phEnumBody->m_ulCount;
} // MDInternalRO::EnumMethodImplGetCount
//*****************************************************************************
// enumerator for MethodImpl.
//*****************************************************************************
__checkReturn
HRESULT
MDInternalRO::EnumMethodImplNext( // return hresult
HENUMInternal *phEnumBody, // [IN] input enum for MethodBody
HENUMInternal *phEnumDecl, // [IN] input enum for MethodDecl
mdToken *ptkBody, // [OUT] return token for MethodBody
mdToken *ptkDecl) // [OUT] return token for MethodDecl
{
HRESULT hr;
MethodImplRec *pRecord;
_ASSERTE(phEnumBody && ((phEnumBody->m_tkKind >> 24) == TBL_MethodImpl));
_ASSERTE(ptkBody && ptkDecl);
if (phEnumBody->u.m_ulCur >= phEnumBody->u.m_ulEnd)
{
return S_FALSE;
}
IfFailRet(m_LiteWeightStgdb.m_MiniMd.GetMethodImplRecord(phEnumBody->u.m_ulCur, &pRecord));
*ptkBody = m_LiteWeightStgdb.m_MiniMd.getMethodBodyOfMethodImpl(pRecord);
*ptkDecl = m_LiteWeightStgdb.m_MiniMd.getMethodDeclarationOfMethodImpl(pRecord);
phEnumBody->u.m_ulCur++;
return S_OK;
} // MDInternalRO::EnumMethodImplNext
//*****************************************
// Reset the enumerator to the beginning.
//*****************************************
void MDInternalRO::EnumMethodImplReset(
HENUMInternal *phEnumBody, // [IN] MethodBody enumerator.
HENUMInternal *phEnumDecl) // [IN] MethodDecl enumerator.
{
_ASSERTE(phEnumBody && ((phEnumBody->m_tkKind >> 24) == TBL_MethodImpl));
_ASSERTE(phEnumBody->m_EnumType == MDSimpleEnum);
phEnumBody->u.m_ulCur = phEnumBody->u.m_ulStart;
} // MDInternalRO::EnumMethodImplReset
//*****************************************
// Close the enumerator.
//*****************************************
void MDInternalRO::EnumMethodImplClose(
HENUMInternal *phEnumBody, // [IN] MethodBody enumerator.
HENUMInternal *phEnumDecl) // [IN] MethodDecl enumerator.
{
_ASSERTE(phEnumBody && ((phEnumBody->m_tkKind >> 24) == TBL_MethodImpl));
_ASSERTE(phEnumBody->m_EnumType == MDSimpleEnum);
} // MDInternalRO::EnumMethodImplClose
//******************************************************************************
// enumerator for global functions
//******************************************************************************
__checkReturn
HRESULT MDInternalRO::EnumGlobalFunctionsInit( // return hresult
HENUMInternal *phEnum) // [OUT] buffer to fill for enumerator data
{
return EnumInit(mdtMethodDef, m_tdModule, phEnum);
}
//******************************************************************************
// enumerator for global Fields
//******************************************************************************
__checkReturn
HRESULT MDInternalRO::EnumGlobalFieldsInit( // return hresult
HENUMInternal *phEnum) // [OUT] buffer to fill for enumerator data
{
return EnumInit(mdtFieldDef, m_tdModule, phEnum);
}
//*****************************************
// Enumerator initializer
//*****************************************
__checkReturn
HRESULT MDInternalRO::EnumInit( // return S_FALSE if record not found
DWORD tkKind, // [IN] which table to work on
mdToken tkParent, // [IN] token to scope the search
HENUMInternal *phEnum) // [OUT] the enumerator to fill
{
HRESULT hr = S_OK;
ULONG ulMax = 0;
// Vars for query.
_ASSERTE(phEnum);
HENUMInternal::ZeroEnum(phEnum);
// cache the tkKind and the scope
phEnum->m_tkKind = TypeFromToken(tkKind);
TypeDefRec *pRec;
phEnum->m_EnumType = MDSimpleEnum;
switch (TypeFromToken(tkKind))
{
case mdtFieldDef:
IfFailGo(m_LiteWeightStgdb.m_MiniMd.GetTypeDefRecord(RidFromToken(tkParent), &pRec));
phEnum->u.m_ulStart = m_LiteWeightStgdb.m_MiniMd.getFieldListOfTypeDef(pRec);
IfFailGo(m_LiteWeightStgdb.m_MiniMd.getEndFieldListOfTypeDef(RidFromToken(tkParent), &(phEnum->u.m_ulEnd)));
break;
case mdtMethodDef:
IfFailGo(m_LiteWeightStgdb.m_MiniMd.GetTypeDefRecord(RidFromToken(tkParent), &pRec));
phEnum->u.m_ulStart = m_LiteWeightStgdb.m_MiniMd.getMethodListOfTypeDef(pRec);
IfFailGo(m_LiteWeightStgdb.m_MiniMd.getEndMethodListOfTypeDef(RidFromToken(tkParent), &(phEnum->u.m_ulEnd)));
break;
case mdtGenericParam:
_ASSERTE(TypeFromToken(tkParent) == mdtTypeDef || TypeFromToken(tkParent) == mdtMethodDef);
if (TypeFromToken(tkParent) != mdtTypeDef && TypeFromToken(tkParent) != mdtMethodDef)
IfFailGo(CLDB_E_FILE_CORRUPT);
if (TypeFromToken(tkParent) == mdtTypeDef)
{
IfFailGo(m_LiteWeightStgdb.m_MiniMd.getGenericParamsForTypeDef(
RidFromToken(tkParent),
&phEnum->u.m_ulEnd,
&(phEnum->u.m_ulStart)));
}
else
{
IfFailGo(m_LiteWeightStgdb.m_MiniMd.getGenericParamsForMethodDef(
RidFromToken(tkParent),
&phEnum->u.m_ulEnd,
&(phEnum->u.m_ulStart)));
}
break;
case mdtGenericParamConstraint:
IfFailGo(m_LiteWeightStgdb.m_MiniMd.getGenericParamConstraintsForGenericParam(
RidFromToken(tkParent),
&phEnum->u.m_ulEnd,
&phEnum->u.m_ulStart));
break;
case mdtInterfaceImpl:
IfFailGo(m_LiteWeightStgdb.m_MiniMd.getInterfaceImplsForTypeDef(RidFromToken(tkParent), &phEnum->u.m_ulEnd, &phEnum->u.m_ulStart));
break;
case mdtProperty:
RID ridPropertyMap;
PropertyMapRec *pPropertyMapRec;
// get the starting/ending rid of properties of this typedef
IfFailGo(m_LiteWeightStgdb.m_MiniMd.FindPropertyMapFor(RidFromToken(tkParent), &ridPropertyMap));
if (!InvalidRid(ridPropertyMap))
{
IfFailGo(m_LiteWeightStgdb.m_MiniMd.GetPropertyMapRecord(ridPropertyMap, &pPropertyMapRec));
phEnum->u.m_ulStart = m_LiteWeightStgdb.m_MiniMd.getPropertyListOfPropertyMap(pPropertyMapRec);
IfFailGo(m_LiteWeightStgdb.m_MiniMd.getEndPropertyListOfPropertyMap(ridPropertyMap, &(phEnum->u.m_ulEnd)));
ulMax = m_LiteWeightStgdb.m_MiniMd.getCountProperties() + 1;
if(phEnum->u.m_ulStart == 0) phEnum->u.m_ulStart = 1;
if(phEnum->u.m_ulEnd > ulMax) phEnum->u.m_ulEnd = ulMax;
if(phEnum->u.m_ulStart > phEnum->u.m_ulEnd) phEnum->u.m_ulStart = phEnum->u.m_ulEnd;
}
break;
case mdtEvent:
RID ridEventMap;
EventMapRec *pEventMapRec;
// get the starting/ending rid of events of this typedef
IfFailGo(m_LiteWeightStgdb.m_MiniMd.FindEventMapFor(RidFromToken(tkParent), &ridEventMap));
if (!InvalidRid(ridEventMap))
{
IfFailGo(m_LiteWeightStgdb.m_MiniMd.GetEventMapRecord(ridEventMap, &pEventMapRec));
phEnum->u.m_ulStart = m_LiteWeightStgdb.m_MiniMd.getEventListOfEventMap(pEventMapRec);
IfFailGo(m_LiteWeightStgdb.m_MiniMd.getEndEventListOfEventMap(ridEventMap, &(phEnum->u.m_ulEnd)));
ulMax = m_LiteWeightStgdb.m_MiniMd.getCountEvents() + 1;
if(phEnum->u.m_ulStart == 0) phEnum->u.m_ulStart = 1;
if(phEnum->u.m_ulEnd > ulMax) phEnum->u.m_ulEnd = ulMax;
if(phEnum->u.m_ulStart > phEnum->u.m_ulEnd) phEnum->u.m_ulStart = phEnum->u.m_ulEnd;
}
break;
case mdtParamDef:
_ASSERTE(TypeFromToken(tkParent) == mdtMethodDef);
MethodRec *pMethodRec;
IfFailGo(m_LiteWeightStgdb.m_MiniMd.GetMethodRecord(RidFromToken(tkParent), &pMethodRec));
// figure out the start rid and end rid of the parameter list of this methoddef
phEnum->u.m_ulStart = m_LiteWeightStgdb.m_MiniMd.getParamListOfMethod(pMethodRec);
IfFailGo(m_LiteWeightStgdb.m_MiniMd.getEndParamListOfMethod(RidFromToken(tkParent), &(phEnum->u.m_ulEnd)));
break;
case mdtCustomAttribute:
IfFailGo(m_LiteWeightStgdb.m_MiniMd.getCustomAttributeForToken(tkParent, &phEnum->u.m_ulEnd, &phEnum->u.m_ulStart));
break;
case mdtAssemblyRef:
_ASSERTE(IsNilToken(tkParent));
phEnum->u.m_ulStart = 1;
phEnum->u.m_ulEnd = m_LiteWeightStgdb.m_MiniMd.getCountAssemblyRefs() + 1;
break;
case mdtFile:
_ASSERTE(IsNilToken(tkParent));
phEnum->u.m_ulStart = 1;
phEnum->u.m_ulEnd = m_LiteWeightStgdb.m_MiniMd.getCountFiles() + 1;
break;
case mdtExportedType:
_ASSERTE(IsNilToken(tkParent));
phEnum->u.m_ulStart = 1;
phEnum->u.m_ulEnd = m_LiteWeightStgdb.m_MiniMd.getCountExportedTypes() + 1;
break;
case mdtManifestResource:
_ASSERTE(IsNilToken(tkParent));
phEnum->u.m_ulStart = 1;
phEnum->u.m_ulEnd = m_LiteWeightStgdb.m_MiniMd.getCountManifestResources() + 1;
break;
case mdtModuleRef:
_ASSERTE(IsNilToken(tkParent));
phEnum->u.m_ulStart = 1;
phEnum->u.m_ulEnd = m_LiteWeightStgdb.m_MiniMd.getCountModuleRefs() + 1;
break;
case (TBL_MethodImpl << 24):
_ASSERTE(! IsNilToken(tkParent));
IfFailGo(m_LiteWeightStgdb.m_MiniMd.getMethodImplsForClass(
RidFromToken(tkParent),
&phEnum->u.m_ulEnd,
&phEnum->u.m_ulStart));
break;
default:
_ASSERTE(!"ENUM INIT not implemented for the compressed format!");
IfFailGo(E_NOTIMPL);
break;
}
// If the count is negative, the metadata is corrupted somehow.
if (phEnum->u.m_ulEnd < phEnum->u.m_ulStart)
IfFailGo(CLDB_E_FILE_CORRUPT);
phEnum->m_ulCount = phEnum->u.m_ulEnd - phEnum->u.m_ulStart;
phEnum->u.m_ulCur = phEnum->u.m_ulStart;
ErrExit:
// we are done
return hr;
}
//*****************************************
// Enumerator initializer
//*****************************************
__checkReturn
HRESULT MDInternalRO::EnumAllInit( // return S_FALSE if record not found
DWORD tkKind, // [IN] which table to work on
HENUMInternal *phEnum) // [OUT] the enumerator to fill
{
HRESULT hr = S_OK;
// Vars for query.
_ASSERTE(phEnum);
HENUMInternal::ZeroEnum(phEnum);
// cache the tkKind and the scope
phEnum->m_tkKind = TypeFromToken(tkKind);
phEnum->m_EnumType = MDSimpleEnum;
switch (TypeFromToken(tkKind))
{
case mdtTypeRef:
phEnum->m_ulCount = m_LiteWeightStgdb.m_MiniMd.getCountTypeRefs();
break;
case mdtMemberRef:
phEnum->m_ulCount = m_LiteWeightStgdb.m_MiniMd.getCountMemberRefs();
break;
case mdtSignature:
phEnum->m_ulCount = m_LiteWeightStgdb.m_MiniMd.getCountStandAloneSigs();
break;
case mdtMethodDef:
phEnum->m_ulCount = m_LiteWeightStgdb.m_MiniMd.getCountMethods();
break;
case mdtMethodSpec:
phEnum->m_ulCount = m_LiteWeightStgdb.m_MiniMd.getCountMethodSpecs();
break;
case mdtFieldDef:
phEnum->m_ulCount = m_LiteWeightStgdb.m_MiniMd.getCountFields();
break;
case mdtTypeSpec:
phEnum->m_ulCount = m_LiteWeightStgdb.m_MiniMd.getCountTypeSpecs();
break;
case mdtAssemblyRef:
phEnum->m_ulCount = m_LiteWeightStgdb.m_MiniMd.getCountAssemblyRefs();
break;
case mdtModuleRef:
phEnum->m_ulCount = m_LiteWeightStgdb.m_MiniMd.getCountModuleRefs();
break;
case mdtTypeDef:
phEnum->m_ulCount = m_LiteWeightStgdb.m_MiniMd.getCountTypeDefs();
break;
case mdtFile:
phEnum->m_ulCount = m_LiteWeightStgdb.m_MiniMd.getCountFiles();
break;
case mdtCustomAttribute:
phEnum->m_ulCount = m_LiteWeightStgdb.m_MiniMd.getCountCustomAttributes();
break;
default:
_ASSERTE(!"Bad token kind!");
break;
}
phEnum->u.m_ulStart = phEnum->u.m_ulCur = 1;
phEnum->u.m_ulEnd = phEnum->m_ulCount + 1;
// we are done
return hr;
} // MDInternalRO::EnumAllInit
//*****************************************
// Enumerator initializer for CustomAttributes
//*****************************************
__checkReturn
HRESULT MDInternalRO::EnumCustomAttributeByNameInit(// return S_FALSE if record not found
mdToken tkParent, // [IN] token to scope the search
LPCSTR szName, // [IN] CustomAttribute's name to scope the search
HENUMInternal *phEnum) // [OUT] the enumerator to fill
{
return m_LiteWeightStgdb.m_MiniMd.CommonEnumCustomAttributeByName(tkParent, szName, false, phEnum);
} // MDInternalRO::EnumCustomAttributeByNameInit
//*****************************************
// Nagivator helper to navigate back to the parent token given a token.
// For example, given a memberdef token, it will return the containing typedef.
//
// the mapping is as following:
// ---given child type---------parent type
// mdMethodDef mdTypeDef
// mdFieldDef mdTypeDef
// mdInterfaceImpl mdTypeDef
// mdParam mdMethodDef
// mdProperty mdTypeDef
// mdEvent mdTypeDef
//
//*****************************************
__checkReturn
HRESULT MDInternalRO::GetParentToken(
mdToken tkChild, // [IN] given child token
mdToken *ptkParent) // [OUT] returning parent
{
HRESULT hr = NOERROR;
_ASSERTE(ptkParent);
switch (TypeFromToken(tkChild))
{
case mdtTypeDef:
hr = GetNestedClassProps(tkChild, ptkParent);
// If not found, the *ptkParent has to be left unchanged! (callers depend on that)
if (hr == CLDB_E_RECORD_NOTFOUND)
{
hr = S_OK;
}
break;
case mdtMethodDef:
IfFailRet(m_LiteWeightStgdb.m_MiniMd.FindParentOfMethod(RidFromToken(tkChild), (RID *)ptkParent));
RidToToken(*ptkParent, mdtTypeDef);
break;
case mdtMethodSpec:
{
MethodSpecRec *pRec;
IfFailRet(m_LiteWeightStgdb.m_MiniMd.GetMethodSpecRecord(RidFromToken(tkChild), &pRec));
*ptkParent = m_LiteWeightStgdb.m_MiniMd.getMethodOfMethodSpec(pRec);
break;
}
case mdtFieldDef:
IfFailRet(m_LiteWeightStgdb.m_MiniMd.FindParentOfField(RidFromToken(tkChild), (RID *)ptkParent));
RidToToken(*ptkParent, mdtTypeDef);
break;
case mdtParamDef:
IfFailRet(m_LiteWeightStgdb.m_MiniMd.FindParentOfParam(RidFromToken(tkChild), (RID *)ptkParent));
RidToToken(*ptkParent, mdtMethodDef);
break;
case mdtMemberRef:
{
MemberRefRec *pRec;
IfFailRet(m_LiteWeightStgdb.m_MiniMd.GetMemberRefRecord(RidFromToken(tkChild), &pRec));
*ptkParent = m_LiteWeightStgdb.m_MiniMd.getClassOfMemberRef(pRec);
break;
}
case mdtCustomAttribute:
{
CustomAttributeRec *pRec;
IfFailRet(m_LiteWeightStgdb.m_MiniMd.GetCustomAttributeRecord(RidFromToken(tkChild), &pRec));
*ptkParent = m_LiteWeightStgdb.m_MiniMd.getParentOfCustomAttribute(pRec);
break;
}
case mdtEvent:
hr = m_LiteWeightStgdb.m_MiniMd.FindParentOfEventHelper(tkChild, ptkParent);
break;
case mdtProperty:
hr = m_LiteWeightStgdb.m_MiniMd.FindParentOfPropertyHelper(tkChild, ptkParent);
break;
default:
_ASSERTE(!"NYI: for compressed format!");
break;
}
return hr;
} // MDInternalRO::GetParentToken
//*****************************************************************************
// Get information about a CustomAttribute.
//*****************************************************************************
__checkReturn
HRESULT
MDInternalRO::GetCustomAttributeProps( // S_OK or error.
mdCustomAttribute at, // The attribute.
mdToken *ptkType) // Put attribute type here.
{
HRESULT hr;
_ASSERTE(TypeFromToken(at) == mdtCustomAttribute);
// Do a linear search on compressed version as we do not want to
// depends on ICR.
//
CustomAttributeRec *pCustomAttributeRec;
IfFailRet(m_LiteWeightStgdb.m_MiniMd.GetCustomAttributeRecord(RidFromToken(at), &pCustomAttributeRec));
*ptkType = m_LiteWeightStgdb.m_MiniMd.getTypeOfCustomAttribute(pCustomAttributeRec);
return S_OK;
} // MDInternalRO::GetCustomAttributeProps
//*****************************************************************************
// return custom value
//*****************************************************************************
__checkReturn
HRESULT
MDInternalRO::GetCustomAttributeAsBlob(
mdCustomAttribute cv, // [IN] given custom attribute token
void const **ppBlob, // [OUT] return the pointer to internal blob
ULONG *pcbSize) // [OUT] return the size of the blob
{
HRESULT hr;
_ASSERTE(ppBlob && pcbSize && TypeFromToken(cv) == mdtCustomAttribute);
CustomAttributeRec *pCustomAttributeRec;
IfFailRet(m_LiteWeightStgdb.m_MiniMd.GetCustomAttributeRecord(RidFromToken(cv), &pCustomAttributeRec));
IfFailRet(m_LiteWeightStgdb.m_MiniMd.getValueOfCustomAttribute(pCustomAttributeRec, (const BYTE **)ppBlob, pcbSize));
return S_OK;
} // MDInternalRO::GetCustomAttributeAsBlob
//*****************************************************************************
// Helper function to lookup and retrieve a CustomAttribute.
//*****************************************************************************
__checkReturn
HRESULT MDInternalRO::GetCustomAttributeByName( // S_OK or error.
mdToken tkObj, // [IN] Object with Custom Attribute.
LPCUTF8 szName, // [IN] Name of desired Custom Attribute.
_Outptr_result_bytebuffer_(*pcbData) const void **ppData, // [OUT] Put pointer to data here.
_Out_ ULONG *pcbData) // [OUT] Put size of data here.
{
return m_LiteWeightStgdb.m_MiniMd.CommonGetCustomAttributeByNameEx(tkObj, szName, NULL, ppData, pcbData);
} // MDInternalRO::GetCustomAttributeByName
//*****************************************************************************
// return the name of a custom attribute
//*****************************************************************************
__checkReturn
HRESULT MDInternalRO::GetNameOfCustomAttribute( // S_OK or error.
mdCustomAttribute mdAttribute, // [IN] The Custom Attribute
LPCUTF8 *pszNamespace, // [OUT] Namespace of Custom Attribute.
LPCUTF8 *pszName) // [OUT] Name of Custom Attribute.
{
_ASSERTE(TypeFromToken(mdAttribute) == mdtCustomAttribute);
HRESULT hr = m_LiteWeightStgdb.m_MiniMd.CommonGetNameOfCustomAttribute(RidFromToken(mdAttribute), pszNamespace, pszName);
return (hr == S_FALSE) ? E_FAIL : hr;
} // MDInternalRO::GetNameOfCustomAttribute
//*****************************************************************************
// return scope properties
//*****************************************************************************
__checkReturn
HRESULT
MDInternalRO::GetScopeProps(
LPCSTR *pszName, // [OUT] scope name
GUID *pmvid) // [OUT] version id
{
HRESULT hr;
ModuleRec *pModuleRec;
// there is only one module record
IfFailRet(m_LiteWeightStgdb.m_MiniMd.GetModuleRecord(1, &pModuleRec));
if (pmvid != NULL)
{
IfFailRet(m_LiteWeightStgdb.m_MiniMd.getMvidOfModule(pModuleRec, pmvid));
}
if (pszName != NULL)
{
IfFailRet(m_LiteWeightStgdb.m_MiniMd.getNameOfModule(pModuleRec, pszName));
}
return S_OK;
} // MDInternalRO::GetScopeProps
//*****************************************************************************
// Compare two signatures from the same scope. Varags signatures need to be
// preprocessed so they only contain the fixed part.
//*****************************************************************************
BOOL MDInternalRO::CompareSignatures(PCCOR_SIGNATURE pvFirstSigBlob, // First signature
DWORD cbFirstSigBlob, //
PCCOR_SIGNATURE pvSecondSigBlob, // Second signature
DWORD cbSecondSigBlob, //
void * SigArguments) // No additional arguments required
{
if (cbFirstSigBlob != cbSecondSigBlob || memcmp(pvFirstSigBlob, pvSecondSigBlob, cbSecondSigBlob))
return FALSE;
else
return TRUE;
}
//*****************************************************************************
// Find a given member in a TypeDef (typically a class).
//*****************************************************************************
__checkReturn
HRESULT MDInternalRO::FindMethodDef( // S_OK or error.
mdTypeDef classdef, // The owning class of the member.
LPCSTR szName, // Name of the member in utf8.
PCCOR_SIGNATURE pvSigBlob, // [IN] point to a blob value of COM+ signature
ULONG cbSigBlob, // [IN] count of bytes in the signature blob
mdMethodDef *pmethoddef) // Put MemberDef token here.
{
return FindMethodDefUsingCompare(classdef,
szName,
pvSigBlob,
cbSigBlob,
CompareSignatures,
NULL,
pmethoddef);
}
//*****************************************************************************
// Find a given member in a TypeDef (typically a class).
//*****************************************************************************
__checkReturn
HRESULT MDInternalRO::FindMethodDefUsingCompare( // S_OK or error.
mdTypeDef classdef, // The owning class of the member.
LPCSTR szName, // Name of the member in utf8.
PCCOR_SIGNATURE pvSigBlob, // [IN] point to a blob value of COM+ signature
ULONG cbSigBlob, // [IN] count of bytes in the signature blob
PSIGCOMPARE SigCompare, // [IN] Signature comparison routine
void* pSigArgs, // [IN] Additional arguments passed to signature compare
mdMethodDef *pmethoddef) // Put MemberDef token here.
{
HRESULT hr = NOERROR;
PCCOR_SIGNATURE pvSigTemp = pvSigBlob;
CQuickBytes qbSig;
_ASSERTE(szName && pmethoddef);
// initialize the output parameter
*pmethoddef = mdMethodDefNil;
// check to see if this is a vararg signature
if ( isCallConv(CorSigUncompressCallingConv(pvSigTemp), IMAGE_CEE_CS_CALLCONV_VARARG) )
{
// Get the fix part of VARARG signature
IfFailGo( _GetFixedSigOfVarArg(pvSigBlob, cbSigBlob, &qbSig, &cbSigBlob) );
pvSigBlob = (PCCOR_SIGNATURE) qbSig.Ptr();
}
// Do a linear search on compressed version
//
RID ridMax;
MethodRec *pMethodRec;
LPCUTF8 szCurMethodName;
void const *pvCurMethodSig;
ULONG cbSig;
TypeDefRec *pRec;
RID ridStart;
// get the typedef record
IfFailGo(m_LiteWeightStgdb.m_MiniMd.GetTypeDefRecord(RidFromToken(classdef), &pRec));
// get the range of methoddef rids given the classdef
ridStart = m_LiteWeightStgdb.m_MiniMd.getMethodListOfTypeDef(pRec);
IfFailGo(m_LiteWeightStgdb.m_MiniMd.getEndMethodListOfTypeDef(RidFromToken(classdef), &ridMax));
// loop through each methoddef
for (; ridStart < ridMax; ridStart++)
{
IfFailGo(m_LiteWeightStgdb.m_MiniMd.GetMethodRecord(ridStart, &pMethodRec));
IfFailGo(m_LiteWeightStgdb.m_MiniMd.getNameOfMethod(pMethodRec, &szCurMethodName));
if (strcmp(szCurMethodName, szName) == 0)
{
// name match, now check the signature if specified.
if (cbSigBlob && SigCompare)
{
IfFailGo(m_LiteWeightStgdb.m_MiniMd.getSignatureOfMethod(pMethodRec, (PCCOR_SIGNATURE *)&pvCurMethodSig, &cbSig));
// Signature comparison is required
// Note that if pvSigBlob is vararg, we already preprocess it so that
// it only contains the fix part. Therefore, it still should be an exact
// match!!!.
//
if(SigCompare((PCCOR_SIGNATURE) pvCurMethodSig, cbSig, pvSigBlob, cbSigBlob, pSigArgs) == FALSE)
continue;
}
// Ignore PrivateScope methods.
if (IsMdPrivateScope(m_LiteWeightStgdb.m_MiniMd.getFlagsOfMethod(pMethodRec)))
continue;
// found the match
*pmethoddef = TokenFromRid(ridStart, mdtMethodDef);
goto ErrExit;
}
}
hr = CLDB_E_RECORD_NOTFOUND;
ErrExit:
return hr;
}
//*****************************************************************************
// Find a given param of a Method.
//*****************************************************************************
__checkReturn
HRESULT MDInternalRO::FindParamOfMethod(// S_OK or error.
mdMethodDef md, // [IN] The owning method of the param.
ULONG iSeq, // [IN] The sequence # of the param.
mdParamDef *pparamdef) // [OUT] Put ParamDef token here.
{
HRESULT hr;
ParamRec *pParamRec;
RID ridStart, ridEnd;
_ASSERTE(TypeFromToken(md) == mdtMethodDef && pparamdef);
// get the methoddef record
MethodRec *pMethodRec;
IfFailRet(m_LiteWeightStgdb.m_MiniMd.GetMethodRecord(RidFromToken(md), &pMethodRec));
// figure out the start rid and end rid of the parameter list of this methoddef
ridStart = m_LiteWeightStgdb.m_MiniMd.getParamListOfMethod(pMethodRec);
IfFailRet(m_LiteWeightStgdb.m_MiniMd.getEndParamListOfMethod(RidFromToken(md), &ridEnd));
// Ensure that the paramList is valid. If the count is negative, the metadata
// is corrupted somehow. Thus, return CLDB_E_FILE_CORRUPT.
if (ridEnd < ridStart)
return CLDB_E_FILE_CORRUPT;
// loop through each param
//<TODO>@consider: parameters are sorted by sequence. Maybe a binary search?
//</TODO>
for (; ridStart < ridEnd; ridStart++)
{
IfFailRet(m_LiteWeightStgdb.m_MiniMd.GetParamRecord(ridStart, &pParamRec));
if (iSeq == m_LiteWeightStgdb.m_MiniMd.getSequenceOfParam(pParamRec))
{
// parameter has the sequence number matches what we are looking for
*pparamdef = TokenFromRid(ridStart, mdtParamDef);
return S_OK;
}
}
return CLDB_E_RECORD_NOTFOUND;
}
//*****************************************************************************
// return a pointer which points to meta data's internal string
// return the type name in utf8
//*****************************************************************************
__checkReturn
HRESULT
MDInternalRO::GetNameOfTypeDef( // return hresult
mdTypeDef classdef, // given typedef
LPCSTR* pszname, // pointer to an internal UTF8 string
LPCSTR* psznamespace) // pointer to the namespace.
{
HRESULT hr;
if (pszname != NULL)
{
*pszname = NULL;
}
if (psznamespace != NULL)
{
*psznamespace = NULL;
}
if (TypeFromToken(classdef) == mdtTypeDef)
{
TypeDefRec *pTypeDefRec;
IfFailRet(m_LiteWeightStgdb.m_MiniMd.GetTypeDefRecord(RidFromToken(classdef), &pTypeDefRec));
if (pszname != NULL)
{
IfFailRet(m_LiteWeightStgdb.m_MiniMd.getNameOfTypeDef(pTypeDefRec, pszname));
}
if (psznamespace != NULL)
{
IfFailRet(m_LiteWeightStgdb.m_MiniMd.getNamespaceOfTypeDef(pTypeDefRec, psznamespace));
}
return S_OK;
}
_ASSERTE(!"Invalid argument(s) of GetNameOfTypeDef");
return CLDB_E_INTERNALERROR;
} // MDInternalRO::GetNameOfTypeDef
__checkReturn
HRESULT MDInternalRO::GetIsDualOfTypeDef(// return hresult
mdTypeDef classdef, // given classdef
ULONG *pDual) // [OUT] return dual flag here.
{
ULONG iFace=0; // Iface type.
HRESULT hr; // A result.
hr = GetIfaceTypeOfTypeDef(classdef, &iFace);
if (hr == S_OK)
*pDual = (iFace == ifDual);
else
*pDual = 1;
return hr;
} // MDInternalRO::GetIsDualOfTypeDef
__checkReturn
HRESULT MDInternalRO::GetIfaceTypeOfTypeDef(
mdTypeDef classdef, // [IN] given classdef.