This repository was archived by the owner on Apr 27, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathtm.cpp
More file actions
1229 lines (969 loc) · 31.5 KB
/
Copy pathtm.cpp
File metadata and controls
1229 lines (969 loc) · 31.5 KB
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
//////////////////////////////////////////////////////////////////////////////
// Type Map implementation
#include "pdbimpl.h"
#include "dbiimpl.h"
#include "output.h"
#include <stdio.h>
#include <malloc.h>
#define IS_ST_LEAF(p) (MapLeafStToSz(((lfEasy*)p)->leaf) != ((lfEasy*)p)->leaf)
#define SZ_NAME(p, n) (IS_ST_LEAF(p) ? szForSt((ST)p->n) : (SZ)p->n)
#define NEW_SZ_NAME(p, n) (IS_ST_LEAF(p) ? szCopySt((ST)p->n) : szCopy((SZ)p->n))
#ifdef _DEBUG
BOOL rgbEnableDiagnostic[20];
#endif
TM::TM(PDB1* ppdb1To_, DBI1* pdbi1To_, TPI* ptpiTo_, TPI* pipiTo_)
: ppdb1To(ppdb1To_), pdbi1To(pdbi1To_), ptpiTo(ptpiTo_), pipiTo(pipiTo_),
mptiti(0), mpidid(0), m_fCorruptedFromTypePool(false), m_imod(imodNil) {}
BOOL TM::fInit(TI tiMin_, TI tiMac_, TI idMin_, TI idMac_)
{
tiMin = tiMin_;
tiMac = tiMac_;
dassert(tiMin <= tiMac);
ctiFrom = tiMac - tiMin;
if (ctiFrom && !(mptiti = new (zeroed) TI[ctiFrom])) {
ppdb1To->setOOMError();
return FALSE;
}
idMin = idMin_;
idMac = idMac_;
dassert(idMin <= idMac);
cidFrom = idMac - idMin;
if (cidFrom && !(mpidid = new (zeroed) TI[cidFrom])) {
ppdb1To->setOOMError();
return FALSE;
}
return TRUE;
}
void TM::endMod()
{
// do nothing: by default, TMs outlive modules
}
void TM::endDBI()
{
delete this;
}
TM::~TM()
{
if (mptiti) {
delete [] mptiti;
}
if (mpidid) {
delete [] mpidid;
}
}
TMEQTS::TMEQTS(PDB1* ppdb1To_, DBI1* pdbi1To_, TPI* ptpiTo_, TPI* pipiTo_)
: TM(ppdb1To_, pdbi1To_, ptpiTo_, pipiTo_)
{
instrumentation(pdbi1To->info.cTMTS++);
}
BOOL TMEQTS::fInit()
{
// to/from are equivalent in a TMEQTS
TI tiMin_ = ptpiTo->QueryTiMin();
TI tiMac_ = ptpiTo->QueryTiMac();
assert(ptpiTo->SupportQueryTiForUDT());
TI idMin_ = tiNil;
TI idMac_ = tiNil;
if (pipiTo != NULL) {
idMin_ = pipiTo->QueryTiMin();
idMac_ = pipiTo->QueryTiMac();
assert(pipiTo->SupportQueryTiForUDT());
}
// initialize rest, but we don't need a mapping in the base class,
// so indicate it through giving it no types to map.
return TM::fInit(tiMin_, tiMin_, idMin_, idMin_);
}
TMTS::TMTS(PDB1* ppdb1To_, DBI1* pdbi1To_, TPI* ptpiTo_, TPI* pipiTo_)
: TM(ppdb1To_, pdbi1To_, ptpiTo_, pipiTo_), ppdbFrom(0), pdbiFrom(0), ptpiFrom(0), pipiFrom(0)
{
instrumentation(pdbi1To->info.cTMTS++);
}
BOOL TMTS::fInit(PDB* ppdbFrom_)
{
dassert(ppdbFrom_);
// given ppdbFrom, open ptpiFrom and pipiFrom
ppdbFrom = ppdbFrom_;
if (!ppdbFrom->OpenTpi(pdbRead pdbGetRecordsOnly, &ptpiFrom)) {
return FALSE;
}
TI tiMin_ = ptpiFrom->QueryTiMin();
TI tiMac_ = ptpiFrom->QueryTiMac();
TI idMin_ = tiNil;
TI idMac_ = tiNil;
if (ppdbFrom->OpenIpi(pdbRead pdbGetRecordsOnly, &pipiFrom)) {
idMin_ = pipiFrom->QueryTiMin();
idMac_ = pipiFrom->QueryTiMac();
}
else {
// It is okay so long as it is an old verioned PDB.
}
// initialize rest
return TM::fInit(tiMin_, tiMac_, idMin_, idMac_);
}
void TMTS::ClosePdbFrom()
{
if (mptiti) {
delete [] mptiti;
}
if (mpidid) {
delete [] mpidid;
}
if (ptpiFrom) {
ptpiFrom->Close();
}
if (pipiFrom) {
pipiFrom->Close();
}
if (ppdbFrom) {
ppdbFrom->Close();
ppdbFrom = NULL;
}
}
TMTS::~TMTS()
{
// xfer Src from the "from" pdb to the "to" pdb
if (ppdbFrom) {
ppdb1To->CopySrc(ppdbFrom);
}
if (ptpiFrom) {
ptpiFrom->Close();
}
if (pipiFrom) {
pipiFrom->Close();
}
if (pdbiFrom) {
pdbiFrom->Close();
}
if (ppdbFrom) {
ppdbFrom->Close();
}
}
TMR::TMR(PDB1* ppdb1To_, DBI1* pdbi1To_, TPI* ptpiTo_, TPI* pipiTo_)
: TM(ppdb1To_, pdbi1To_, ptpiTo_, pipiTo_), m_ptmpct(0), mptiptype(0), pbTypes(0), pwti(0)
{
instrumentation(pdbi1To->info.cTMR++);
m_fIsTmpctOwner = false;
signature = 0;
}
BOOL TMR::fInit(PB pbTypes_, CB cb, SZ szModule, ULONG sigTypes)
{
PTYPE ptypeMin = (PTYPE)pbTypes_;
PTYPE ptypeMax = (PTYPE)(pbTypes_ + cb);
TI tiMin_ = CV_FIRST_NONPRIM;
bool fPrecomp = false;
lfPreComp * plfPreComp = NULL;
f16bitTypes = sigTypes < CV_SIGNATURE_C11;
if (f16bitTypes) {
if (!WidenTi::fCreate(pwti, cb >> 4)) {
ppdb1To->setOOMError();
return FALSE;
}
// set up for checking the PCT use; we have to get a 32-bit
// version of the lfPreComp_16t record if so.
if (cb > 0) {
PTYPE p = PTYPE(pwti->pTypeWidenTi(0, PB(ptypeMin)));
if (SZLEAFIDX(p->leaf) == LF_PRECOMP) {
plfPreComp = (lfPreComp*)&p->leaf;
fPrecomp = TRUE;
}
}
}
// check for PCT use, 32-bit type only; 16-bit case handled above
if (fPrecomp || (fPrecomp = (cb > 0 && SZLEAFIDX(ptypeMin->leaf) == LF_PRECOMP))) {
if (!plfPreComp)
plfPreComp = (lfPreComp*)&ptypeMin->leaf;
assert(plfPreComp->start == ::tiMin);
// now skip the LF_PRECOMP record and advance tiMin_ over the TIs in the PCT
ptypeMin = (PTYPE)pbEndType(ptypeMin);
tiMin_ += plfPreComp->count;
}
// count types and check for PCT definition
TI tiPreComp = tiNil;
TI tiMac_ = tiMin_;
PTYPE ptypeEndPreComp = 0;
PTYPE ptype;
for (ptype = ptypeMin; ptype < ptypeMax; ptype = (PTYPE)pbEndType(ptype)) {
if (SZLEAFIDX(ptype->leaf) == LF_ENDPRECOMP) {
tiPreComp = tiMac_;
ptypeEndPreComp = ptype;
}
++tiMac_;
}
if (tiPreComp != tiNil) {
// This module is a PCT. Create a TMPCT containing the types up to the
// LF_PRECOMP record. Adjust things to subsequently create a TMR which
// uses the TMPCT just as if this module were just another PCT use.
if (fPrecomp) {
// a pct referencing another pct is not supported - issue warning
// to recompile -Zi or build pdb:none
ppdb1To->setLastError(EC_NOT_IMPLEMENTED, szModule);
return FALSE;
}
m_fIsTmpctOwner = true;
CB cbPreComp = CB(PB(ptypeEndPreComp) - pbTypes_); // REVIEW:WIN64 CAST
lfEndPreComp* pepc = (lfEndPreComp*)&ptypeEndPreComp->leaf;
if (!(m_ptmpct = new TMPCT(ppdb1To, pdbi1To, ptpiTo, pipiTo))) {
ppdb1To->setOOMError();
return FALSE;
}
// Initialize the TMPCT and add it to the OTM list with a temporary
// name (the module name from the linker). We will update it later
// when processing the symbols for this object file.
signature = pepc->signature;
if (!m_ptmpct->fInit(pbTypes_, cbPreComp, szModule, sigTypes) ||
!pdbi1To->fAddTmpct(pepc, tiPreComp, szModule, m_ptmpct))
{
return FALSE;
}
pbTypes_ = pbEndType(ptypeEndPreComp);
ptypeMin = (PTYPE)pbTypes_;
tiMin_ = tiPreComp + 1;
}
// had to defer finding precomp tmr until we look for an endprecomp
// this is to generate a consistent link error regardless of module link order
PTYPE ptypePreComp = PTYPE( ((PB)plfPreComp) - sizeof (ptypePreComp->len) );
// we do not need to go after the TMPCT if we are the owner of it; we have it
// already in m_ptmpct if so.
assert(implies(fPrecomp, !IsTmpctOwner()));
assert(implies(IsTmpctOwner(), m_ptmpct != NULL));
if (fPrecomp && !pdbi1To->fGetTmpct(ptypePreComp, &m_ptmpct)) {
SZ szErr = NEW_SZ_NAME(plfPreComp, name);
ppdb1To->setLastError(EC_PRECOMP_REQUIRED, szErr);
freeSz(szErr);
return FALSE;
}
// initialize base
if (!TM::fInit(tiMin_, tiMac_, tiNil, tiNil)) {
return FALSE;
}
// establish mptiptype
if (!(mptiptype = new PTYPE[ctiFrom])) {
ppdb1To->setOOMError();
return FALSE;
}
// save a copy of *pbTypes_ to isolate us from client memory usage
if (f16bitTypes) {
dassert(pwti);
// we need to go through all of the types and count up how
// much space to allocate for the expanded records
cb = 0;
TI tiT = tiMin;
for (ptype = ptypeMin;
ptype < ptypeMax;
ptype = PTYPE(pbEndType(ptype)), tiT++) {
// this call will widen and cache the record if appropriate.
PTYPE ptypeT = PTYPE(pwti->pTypeWidenTi(tiT, PB(ptype)));
if (ptypeT) {
cb += CB(cbAlign(cbForType(ptypeT)));
}
else {
ppdb1To->setOOMError();
return FALSE;
}
}
tiT = tiMin;
if (!(pbTypes = new BYTE[cb])) {
ppdb1To->setOOMError();
return FALSE;
}
assert((int_ptr_t(pbTypes) & 3) == 0);
PB pbTypesT = pbTypes;
for (ptype = ptypeMin;
ptype < ptypeMax;
ptype = PTYPE(pbEndType(ptype)), tiT++) {
// pick up the cached records
PTYPE ptypeT = PTYPE(pwti->pTypeWidenTi(tiT, PB(ptype)));
if (ptypeT) {
CB cbT = cbForType(ptypeT);
assert(pbTypesT < pbTypes + cb);
assert(pbTypesT + cbT <= pbTypes + cb);
memcpy(pbTypesT, PB(ptypeT), cbT);
// pad out the record and be sure to update the len field.
CB cbPad = cbInsertAlign(pbTypesT + cbT, cbT);
// advance the destination buffer pointer and adjust
// the destination type record length field.
PTYPE(pbTypesT)->len += ushort(cbPad);
pbTypesT += cbT + cbPad;
assert(((PTYPE(pbTypesT - (cbT + cbPad))->len + 2) & 3) == 0);
assert(pbTypesT <= pbTypes + cb);
}
else {
dassert(FALSE); // should never happen, since we have
// prescanned the types above.
}
}
pwti->release();
pwti = NULL;
}
else
{
cb = CB(PB(ptypeMax) - PB(ptypeMin)); // REVIEW:WIN64 CAST
if (!(pbTypes = new BYTE[cb])) {
ppdb1To->setOOMError();
return FALSE;
}
memcpy(pbTypes, (PB)ptypeMin, cb);
}
cbTypes = cb;
TI ti = tiMin;
NameMap *pNameMap = NULL;
if (ppdb1To->FMinimal()) {
if (!pdbi1To->GetNameMap(true, &pNameMap)) {
ppdb1To->setOOMError();
return FALSE;
}
}
for (ptype = (PTYPE)pbTypes, ptypeMax = (PTYPE)(pbTypes + cb);
ptype < ptypeMax;
ptype = (PTYPE)pbEndType(ptype)) {
mptiptype[indexBias(ti, false)] = ptype;
if (ptypeForTi(ti, false) != ptype) {
ppdb1To->setCorruptError();
return FALSE;
}
if (ptype->leaf == LF_UDT_SRC_LINE) {
lfUdtSrcLine *plf = (lfUdtSrcLine *) &ptype->leaf;
if (!mptiUdtIdRec.add(plf->type, ptype)) {
ppdb1To->setOOMError();
return FALSE;
}
}
if (ppdb1To->FMinimal()) {
PB pb = (PB) ptype;
if (REC::fIsDefnUdt(pb)) {
SZ szName = szUDTName(pb);
if (REC::fIsLocalDefnUdtWithUniqueName(pb)) {
szName = szName + strlen(szName) + 1;
}
NI ni;
if (!pNameMap->getNiUTF8(szName, &ni) || !mpnitiUdt.add(ni, ti)) {
ppdb1To->setOOMError();
return FALSE;
}
}
}
++ti;
}
return TRUE;
}
void TMR::endMod()
{
delete this;
}
void TMR::endDBI()
{
dassert(FALSE);
}
TMR::~TMR()
{
if (pbTypes) {
delete [] pbTypes;
}
if (mptiptype) {
delete [] mptiptype;
}
if (pwti) {
pwti->release();
}
}
TMPCT::TMPCT(PDB1* ppdb1To_, DBI1* pdbi1To_, TPI* ptpiTo_, TPI* pipiTo_)
: TMR(ppdb1To_, pdbi1To_, ptpiTo_, pipiTo_)
{
m_fIsTmpct = true;
instrumentation(pdbi1To->info.cTMR--);
instrumentation(pdbi1To->info.cTMPCT++);
}
BOOL TMPCT::fInit(PB pbTypes_, CB cb, SZ szModule, ULONG sig)
{
return TMR::fInit(pbTypes_, cb, szModule, sig);
}
void TMPCT::endMod()
{
dassert(FALSE);
}
void TMPCT::endDBI()
{
delete this;
}
TMPCT::~TMPCT()
{
}
debug(void dumpType(PTYPE ptype););
const CB cbTypeBuf = 256; // this buffer will be large enough for most records
// Update rti (a TI reference with the TI of an equivalent record in ptpiTo,
// and return TRUE if successful.
//
BOOL TM::fMapRti(IMOD imod, TI& rti, bool fID)
{
MTS_PROTECT(m_cs);
TI tiOrig = rti;
instrumentation(pdbi1To->info.cTypesMapped++);
if (fID && (pipiTo == NULL)) {
ppdb1To->setLastError(EC_FORMAT);
return FALSE;
}
bool fDefnUdt = false;
if (!fMapRti(rti, fID, 0, &fDefnUdt)) {
return FALSE;
}
// For a non fwdref UDT, convert its corresponding LF_UDT_SRC_LINE
// to LF_UDT_MOD_SRC_LINE, and then write it into PDB.
if (fID || !fDefnUdt || (pipiTo == NULL)) {
// Don't bother if it is not a UDT definition or this PDB has no ID stream.
return TRUE;
}
// Check whether source string has already been added into string table.
DWORD dwLine = 0;
TI srcId = 0;
NI ni = 0;
if (!QuerySrcIdLineForUDT(tiOrig, srcId, dwLine)) {
// It is OK if we can't get source info on this UDT definition.
return TRUE;
}
if (!m_mpSrcIdToNi.map(srcId, &ni)) {
char *szSrc = NULL;
if (!QuerySrcLineForUDT(tiOrig, &szSrc, &dwLine)) {
// It is OK if we can't get source/line on this UDT definition.
return TRUE;
}
// Add the source string into PDB's string table.
NameMap *pNameMap = NULL;
if (!pdbi1To->GetNameMap(true, &pNameMap)) {
ppdb1To->setOOMError();
return FALSE;
}
if (pdbi1To->cSrcMapEntries() > 0) {
USES_STACKBUFFER(MAX_PATH * sizeof(wchar_t));
wchar_t *wszSrc = GetSZUnicodeFromSZUTF8(szSrc);
Buffer bufMappedName;
if (pdbi1To->fSrcMap(wszSrc, bufMappedName)) {
wszSrc = reinterpret_cast<wchar_t *>(bufMappedName.Start());
if (!pNameMap->getNiW(wszSrc, &ni)) {
ppdb1To->setOOMError();
return FALSE;
}
}
}
if ((ni == 0) && !pNameMap->getNiUTF8(szSrc, &ni)) {
ppdb1To->setOOMError();
return FALSE;
}
delete [] szSrc;
if (!m_mpSrcIdToNi.add(srcId, ni)) {
ppdb1To->setOOMError();
return FALSE;
}
}
// Compose LF_UDT_MOD_SRC_LINE record and write it into PDB.
BYTE rgb[sizeof(unsigned short) + sizeof(lfUdtModSrcLine)] = {0};
*((unsigned short *) rgb) = sizeof(lfUdtModSrcLine);
lfUdtModSrcLine *plf = (lfUdtModSrcLine *) (rgb + sizeof(unsigned short));
plf->leaf = LF_UDT_MOD_SRC_LINE;
plf->imod = imod + 1;
plf->type = rti;
plf->src = ni;
plf->line = dwLine;
TI tiDummy = 0;
return pipiTo->QueryTiForCVRecord(rgb, &tiDummy);
}
inline BOOL fGlobalUDTDecl(PTYPE ptype)
{
assert(!fNeedsSzConversion(ptype));
unsigned leaf = ptype->leaf;
if ((leaf >= LF_CLASS) && (leaf <= LF_ENUM)) {
void * pv = &ptype->leaf;
return (leaf <= LF_UNION) ?
((lfClass*)pv)->property.fwdref && !((lfClass*)pv)->property.scoped :
((lfEnum*)pv)->property.fwdref && !((lfEnum*)pv)->property.scoped;
}
return FALSE;
}
PTYPE TMEQTS::ptypeForTi(TI ti, bool fID) const
{
assert(isValidIndex(ti, fID) && !CV_IS_PRIMITIVE(ti));
PB pb = NULL;
TPI* p = fID ? pipiTo : ptpiTo;
verify(p->QueryPbCVRecordForTi(ti, &pb));
return PTYPE(pb);
}
BOOL TMEQTS::QuerySrcLineForUDT(TI tiUdt, char **psz, DWORD *pLine)
{
assert(isValidIndex(tiUdt, false) && !CV_IS_PRIMITIVE(tiUdt));
return ((TPI1 *) pipiTo)->QueryModSrcLineForUDT(tiUdt, NULL, psz, pLine);
}
BOOL TMEQTS::QuerySrcIdLineForUDT(TI tiUdt, TI& srcId, DWORD& line)
{
assert(isValidIndex(tiUdt, false) && !CV_IS_PRIMITIVE(tiUdt));
return ((TPI1 *) pipiTo)->QuerySrcIdLineForUDT(tiUdt, srcId, line);
}
PTYPE TMTS::ptypeForTi(TI ti, bool fID) const
{
dassert(isValidIndex(ti, fID) && !CV_IS_PRIMITIVE(ti));
PB pb = NULL;
TPI* p = fID ? pipiFrom : ptpiFrom;
verify(p->QueryPbCVRecordForTi(ti, &pb));
return (PTYPE) pb;
}
BOOL TMTS::QuerySrcLineForUDT(TI tiUdt, char **psz, DWORD *pLine)
{
assert(isValidIndex(tiUdt, false) && !CV_IS_PRIMITIVE(tiUdt));
if (pipiFrom == NULL) {
return FALSE;
}
return ((TPI1 *) pipiFrom)->QueryModSrcLineForUDT(tiUdt, NULL, psz, pLine);
}
BOOL TMTS::QuerySrcIdLineForUDT(TI tiUdt, TI& srcId, DWORD& line)
{
assert(isValidIndex(tiUdt, false) && !CV_IS_PRIMITIVE(tiUdt));
if (pipiFrom == NULL) {
return FALSE;
}
return ((TPI1 *) pipiFrom)->QuerySrcIdLineForUDT(tiUdt, srcId, line);
}
// Update rti (a TI reference for a type record stored in a TypeServer) with
// the TI of an equivalent record in ptpiTo, and return TRUE if successful.
//
BOOL TMTS::fMapRti(TI& rti, bool fID, int depth, bool *pfDefnUdt)
{
expect(isValidIndex(rti, fID));
instrumentation(pdbi1To->info.cTypesMappedRecursively++);
// return immediately if TI is primitive, is bogus, or has already been mapped
if (CV_IS_PRIMITIVE(rti)) {
return TRUE;
}
if (!isValidIndex(rti, fID)) {
// Leave a flag that this pool has some corruption.
// REVIEW: later, we may want to provide some more data here
//
fCorruptedFromTypePool() = true;
rti = tiNil;
return TRUE;
}
if (rtiMapped(rti, fID) != tiNil) {
rti = rtiMapped(rti, fID);
return TRUE;
}
// read type record from the 'from' TypeServer or ID server
TPI* pFrom = fID ? pipiFrom : ptpiFrom;
SafeStackAllocator<1024> allocator;
CB cb = 0;
if (!pFrom->QueryCVRecordForTi(rti, NULL, &cb)) {
return FALSE;
}
// alloc a buffer
PTYPE ptype = reinterpret_cast<PTYPE>(allocator.Alloc<BYTE>(cb));
if (NULL == ptype) {
ppdb1To->setOOMError();
return FALSE;
}
if (!pFrom->QueryCVRecordForTi(rti, (PB)ptype, &cb)) {
return FALSE;
}
// recursively map all TIs within the type record into the 'to' TypeServer
#if _DEBUG // {
#if !defined( PDB_LIBRARY )
if (rgbEnableDiagnostic[0]) {
OutputInit();
StdOutPrintf(L"%d< %04X ", depth, rti);
dumpType(ptype);
}
#endif
#endif // }
for (TypeTiIter tii(ptype); tii.next(); ) {
if (!TMTS::fMapRti(tii.rti(), tii.fId(), depth + 1, NULL)) {
return FALSE;
}
}
if ((pfDefnUdt != NULL) && !fID) {
*pfDefnUdt = REC::fIsDefnUdt((PB) ptype);
}
// find TI for resulting record within the 'to' TypeServer
TPI* pTo = fID ? pipiTo : ptpiTo;
instrumentation(pdbi1To->info.cTypesQueried++);
instrumentation(TI tiMacWas = pTo->QueryTiMac());
TI tiTo;
if (!pTo->QueryTiForCVRecord((PB)ptype, &tiTo) &&
(ppdb1To->QueryLastErrorExW(NULL, 0) != EC_OUT_OF_TI)) {
return FALSE; // if we run out of type indices keep trying to pack
}
instrumentation(pdbi1To->info.cTypesAdded += (tiMacWas != pTo->QueryTiMac()));
#if _DEBUG // {
#if !defined( PDB_LIBRARY )
if (rgbEnableDiagnostic[0]) {
StdOutPrintf(L"%d> %04X->%04X ", depth, rti, tiTo);
dumpType(ptype);
}
#endif
#endif // }
// update rti and maps
rti = rtiMapped(rti, fID) = tiTo;
return TRUE;
}
inline CV_prop_t getPropOfUDTType(PTYPE ptype)
{
switch (SZLEAFIDX(ptype->leaf)) {
case LF_CLASS:
case LF_STRUCTURE:
case LF_UNION:
return ((lfClass*) (&ptype->leaf))->property;
case LF_ENUM:
return ((lfEnum*) (&ptype->leaf))->property;
case LF_ALIAS:
{
CV_prop_t propT = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
assert(!propT.fwdref);
return propT;
}
};
// return a property with fwdref set so that it will
// flagged as a decl, not a defn.
CV_prop_t prop = { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 };
assert(prop.fwdref);
return prop;
}
inline BOOL fUDTDefn(PTYPE ptype)
{
return !getPropOfUDTType(ptype).fwdref;
}
// make sure that the conditions we require for a quick check
// for UDT type records holds true.
cassert(LF_CLASS+1 == LF_STRUCTURE);
cassert(LF_STRUCTURE+1 == LF_UNION);
cassert(LF_UNION+1 == LF_ENUM);
inline bool fIsUDT(unsigned leaf)
{
return SZLEAFIDX(leaf) == LF_ALIAS || (SZLEAFIDX(leaf) >= LF_CLASS && SZLEAFIDX(leaf) <= LF_ENUM);
}
namespace _tm {
struct UnnamedTag {
const char * sz;
size_t cb;
};
}
BOOL fUDTAnon(PTYPE ptype)
{
static const _tm::UnnamedTag utag[] = {
{ "::<unnamed-tag>", 15 },
{ "::__unnamed", 11 }
};
assert(!fNeedsSzConversion(ptype));
UTFSZ sz = szUDTName(PB(ptype));
if (sz) {
size_t cbSz = 0;
for (unsigned iutag = 0; iutag < _countof(utag); iutag++) {
if (strcmp(sz, utag[iutag].sz + 2) == 0) {
return TRUE;
}
if (cbSz == 0) {
cbSz = strlen(sz);
}
if (cbSz >= utag[iutag].cb) {
// can only be part of composite class name ie foo::`utag[iut].sz'
// check the last cb bytes for a match of utag[iut].sz.
char *pchMax = sz + cbSz;
if (memcmp(utag[iutag].sz, pchMax - utag[iutag].cb, utag[iutag].cb) == 0) {
return TRUE;
}
}
}
}
return FALSE;
}
// do the UDT refs when we have from and to type server equivalence.
//
BOOL TMEQTS::fMapRti(TI& rti, bool fID, int depth, bool *pfDefnUdt)
{
instrumentation(pdbi1To->info.cTypesMappedRecursively++);
// return immediately if TI is primitive or has already been mapped
if (CV_IS_PRIMITIVE(rti))
return TRUE;
if (!isValidIndex(rti, fID)) {
// Leave a flag that this pool has some corruption.
// REVIEW: later, we may want to provide some more data here
//
fCorruptedFromTypePool() = true;
rti = tiNil;
return TRUE;
}
PTYPE ptype = ptypeForTi(rti, fID);
for (TypeTiIter tii(ptype); tii.next(); ) {
if (!fMapRti(tii.rti(), tii.fId(), depth + 1, NULL)) {
return FALSE;
}
}
if (!fID) {
if (pfDefnUdt != NULL) {
*pfDefnUdt = REC::fIsDefnUdt((PB) ptype);
}
if (REC::fIsGlobalDefnUdt((PB) ptype) || REC::fIsLocalDefnUdtWithUniqueName((PB) ptype)) {
// The type record should have already existed in the PDB.
// But in case that the PDB contains multiple definitions
// of this UDT, we have to promote this definition to the
// head of the hash chain.
TI tiTo;
if (!ptpiTo->QueryTiForCVRecord((PB)ptype, &tiTo) || (tiTo != rti)) {
ppdb1To->setLastError(EC_CORRUPT);
return FALSE;
}
}
}
return TRUE;
}
// Update rti (a TI reference for a type record stored directly in this
// module's types records) with the TI of an equivalent record in ptpiTo,
// and return TRUE if successful.
//
BOOL TMR::fMapRti(TI& rti, bool fID, int depth, bool *pfDefnUdt)
{
bool f = (depth == 0) && (m_ptmpct != NULL);
MTS_PROTECT_COND((f ? m_ptmpct->m_cs : m_cs), f);
expect(isValidIndex(rti, false));
instrumentation(pdbi1To->info.cTypesMappedRecursively++);
// return if TI is primitive
if (CV_IS_PRIMITIVE(rti)) {
return TRUE;
}
if (!isValidIndex(rti, false)) {
// Leave a flag that this pool has some corruption.
// REVIEW: later, we may want to provide some more data here
//
fCorruptedFromTypePool() = true;
rti = tiNil;
return TRUE;
}
// return if the type has been mapped already or if it contains a cycle
TI& rtiMap = rtiMapped(rti, false);
if (rtiMap != tiNil) {
rti = rtiMap;
return TRUE;
}
PTYPE ptype = ptypeForTi(rti, false);
#if _DEBUG // {
#if !defined( PDB_LIBRARY )
if (rgbEnableDiagnostic[0]) {
StdOutPrintf(L"%d<%04X ", depth, rti);
dumpType(ptype);
}
#endif
#endif // }
// masm generated functions which return themselves, sigh
if (ptype->leaf == LF_PROCEDURE) {
lfProc* pproc = (lfProc*)&ptype->leaf;
if (pproc->rvtype == rti)
pproc->rvtype = T_VOID;
}
// Recursively map all TIs within the type record into the 'to' TypeServer
// (overwriting in place the TI fields of this type record).
// (Before we recurse, mark this type as T_NOTTRANS so we won't stack
// overflow if the type graph happens to contain a cycle.)
rtiMap = T_NOTTRANS;
for (TypeTiIter tii(ptype); tii.next(); )
if (!TMR::fMapRti(tii.rti(), tii.fId(), depth + 1, NULL))
return FALSE;
if ((pfDefnUdt != NULL) && !fID) {
*pfDefnUdt = REC::fIsDefnUdt((PB) ptype);
}
// find TI for resulting record within the 'to' TypeServer
TPI* pTo = fID ? pipiTo : ptpiTo;
instrumentation(pdbi1To->info.cTypesQueried++);
instrumentation(TI tiMacWas = pTo->QueryTiMac());
TI tiTo;
if (!pTo->QueryTiForCVRecord((PB)ptype, &tiTo) &&
(ppdb1To->QueryLastErrorExW(NULL, 0) != EC_OUT_OF_TI))
return FALSE; // if we run out of type indices keep trying to pack
instrumentation(pdbi1To->info.cTypesAdded += (tiMacWas != pTo->QueryTiMac()));
#if _DEBUG // {
#if !defined( PDB_LIBRARY )
if (rgbEnableDiagnostic[0]) {
StdOutPrintf(L"%d>%04X->%04X ", depth, rti, tiTo);
dumpType(ptype);
}
#endif
#endif // }
// update rti and maps
rti = rtiMapped(rti, false) = tiTo;
return TRUE;
}
inline TI& TM::rtiMapped(TI ti, bool fID) const
{
dassert(isValidIndex(ti, fID));