summaryrefslogtreecommitdiff
path: root/src/debug/ildbsymlib/symwrite.h
blob: 055b8ec21fa48ce1e49a18a17f09f89e88de794e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ===========================================================================
// File: SymWrite.h
//

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

#ifndef SYMWRITE_H_
#define SYMWRITE_H_
#ifdef _MSC_VER
#pragma warning(disable:4786)
#endif

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

#include "cor.h"
#include "umisc.h"
#include "stgpool.h"
#include "safemath.h"

#include <corsym.h>
#include "pdbdata.h"

class SymDocumentWriter;

#if BIGENDIAN
/***
*PUBLIC void VariantSwap
*Purpose:
*  Swap the Variant members
*
*Entry:
*  SrcInBigEndian = whether pvarg is in BIGENDIAN or not
*  pvargDest = Destination variant
*  pvarg = pointer to a VARIANT to swap
*
*Exit:
*  Filled in pvarDest
*
***********************************************************************/
inline HRESULT VariantSwap(bool SrcInBigEndian, VARIANT FAR *pvargDest, VARIANT FAR* pvarg)
{
    if (pvargDest == NULL || pvarg == NULL)
        return E_INVALIDARG;
    VARTYPE vt = VT_EMPTY;

    if (SrcInBigEndian)
    {
        vt = V_VT(pvarg);
    }
    *(UINT32*)pvargDest = VAL32(*(UINT32*)pvarg);
    if (!SrcInBigEndian)
    {
        vt = V_VT(pvargDest);
    }

    switch (vt)
    {
        case VT_EMPTY:
        case VT_NULL:
            // No Value to swap
            break;

            // 1 byte
        case VT_I1:
        case VT_UI1:
            V_I1(pvargDest) = V_I1(pvarg);
            break;

            // 2 bytes
        case VT_I2:
        case VT_UI2:
        case VT_INT:
        case VT_UINT:
        case VT_BOOL:
            V_I2(pvargDest) = VAL16(V_I2(pvarg));
            break;

            // 4 bytes
        case VT_I4:
        case VT_UI4:
        case VT_R4:
            V_I4(pvargDest) = VAL32(V_I4(pvarg));
            break;

            // 8 bytes
        case VT_I8:
        case VT_UI8:
        case VT_R8:
        case VT_DATE:
            V_I8(pvargDest) = VAL64(V_I8(pvarg));
            break;

        case VT_DECIMAL:
            DECIMAL_HI32(V_DECIMAL(pvargDest)) = VAL32(DECIMAL_HI32(V_DECIMAL(pvarg)));
            DECIMAL_LO32(V_DECIMAL(pvargDest)) = VAL32(DECIMAL_LO32(V_DECIMAL(pvarg)));
            DECIMAL_MID32(V_DECIMAL(pvargDest)) = VAL32(DECIMAL_MID32(V_DECIMAL(pvarg)));
            break;

        // These aren't currently supported
        case VT_CY:         //6
        case VT_BSTR:       //8
        case VT_DISPATCH:   //9
        case VT_ERROR:      //10
        case VT_VARIANT:    //12
        case VT_UNKNOWN:    //13
        case VT_VOID:       //24
        case VT_HRESULT:    //25
        case VT_PTR:        //26
        case VT_SAFEARRAY:  //27
        case VT_CARRAY:     //28
        case VT_USERDEFINED://29
        case VT_LPSTR:      //30
        case VT_LPWSTR:     //31
        case VT_FILETIME:   //64
        case VT_BLOB:       //65
        case VT_STREAM:     //66
        case VT_STORAGE:    //67
        case VT_STREAMED_OBJECT:    //68
        case VT_STORED_OBJECT:      //69
        case VT_BLOB_OBJECT:        //70
        case VT_CF:                 //71
        case VT_CLSID:              //72
        default:
            _ASSERTE(!"NYI");
            break;
    }
    return NOERROR;
}
#endif // BIGENDIAN

// Default space sizes for the various arrays. Make it too small in a
// checked build so we exercise the growing code.
#ifdef _DEBUG
#define DEF_LOCAL_SPACE 2
#define DEF_MISC_SPACE  64
#else
#define DEF_LOCAL_SPACE 64
#define DEF_MISC_SPACE  1024
#endif

/* ------------------------------------------------------------------------- *
 * SymVariable struct
 * ------------------------------------------------------------------------- */
struct SymVariable
{
private:
    UINT32           m_Scope;       // index of parent scope
    UINT32           m_Name;        // index into misc byte array
    ULONG32          m_Attributes;  // Attributes
    UINT32           m_Signature;       // index into misc byte array
    ULONG32          m_SignatureSize;   // Signature size
    ULONG32          m_AddrKind;    // Address Kind
    ULONG32          m_Addr1;       // Additional info
    ULONG32          m_Addr2;
    ULONG32          m_Addr3;
    ULONG32          m_StartOffset; // StartOffset
    ULONG32          m_EndOffset;   // EndOffset
    ULONG32          m_Sequence;
    BOOL             m_IsParam;     // parameter?
    BOOL             m_IsHidden;    // Is not visible to the user

public:
    UINT32 Scope()
    {
        return VAL32(m_Scope);
    }
    void SetScope(UINT32 Scope)
    {
        m_Scope = VAL32(Scope);
    }

    UINT32 Name()
    {
        return VAL32(m_Name);
    }
    void SetName(UINT32 Name)
    {
        m_Name = VAL32(Name);
    }

    ULONG32 Attributes()
    {
        return VAL32(m_Attributes);
    }
    void SetAttributes(ULONG32 Attributes)
    {
        m_Attributes = VAL32(Attributes);
    }

    UINT32 Signature()
    {
        return VAL32(m_Signature);
    }
    void SetSignature(UINT32 Signature)
    {
        m_Signature = VAL32(Signature);
    }
    ULONG32 SignatureSize()
    {
        return VAL32(m_SignatureSize);
    }
    void SetSignatureSize(ULONG32 SignatureSize)
    {
        m_SignatureSize = VAL32(SignatureSize);
    }

    ULONG32 AddrKind()
    {
        return VAL32(m_AddrKind);
    }
    void SetAddrKind(ULONG32 AddrKind)
    {
        m_AddrKind = VAL32(AddrKind);
    }
    ULONG32 Addr1()
    {
        return VAL32(m_Addr1);
    }
    void SetAddr1(ULONG32 Addr1)
    {
        m_Addr1 = VAL32(Addr1);
    }

    ULONG32 Addr2()
    {
        return VAL32(m_Addr2);
    }
    void SetAddr2(ULONG32 Addr2)
    {
        m_Addr2 = VAL32(Addr2);
    }

    ULONG32 Addr3()
    {
        return VAL32(m_Addr3);
    }
    void SetAddr3(ULONG32 Addr3)
    {
        m_Addr3 = VAL32(Addr3);
    }

    ULONG32 StartOffset()
    {
        return VAL32(m_StartOffset);
    }
    void SetStartOffset(ULONG32 StartOffset)
    {
        m_StartOffset = VAL32(StartOffset);
    }
    ULONG32 EndOffset()
    {
        return VAL32(m_EndOffset);
    }
    void SetEndOffset(ULONG EndOffset)
    {
        m_EndOffset = VAL32(EndOffset);
    }
    ULONG32 Sequence()
    {
        return VAL32(m_Sequence);
    }
    void SetSequence(ULONG32 Sequence)
    {
        m_Sequence = VAL32(Sequence);
    }

    BOOL    IsParam()
    {
        return VAL32(m_IsParam);
    }
    void SetIsParam(BOOL IsParam)
    {
        m_IsParam = IsParam;
    }
    BOOL    IsHidden()
    {
        return VAL32(m_IsHidden);
    }
    void SetIsHidden(BOOL IsHidden)
    {
        m_IsHidden = IsHidden;
    }
};

/* ------------------------------------------------------------------------- *
 * SymLexicalScope struct
 * ------------------------------------------------------------------------- */
struct SymLexicalScope
{
private:

    UINT32 m_ParentScope;          // parent index (-1 for no parent)
    ULONG32 m_StartOffset;    // start offset
    ULONG32 m_EndOffset;      // end offset
    BOOL    m_HasChildren;    // scope has children
    BOOL    m_HasVars;        // scope has vars?
public:
    UINT32  ParentScope()
    {
        return VAL32(m_ParentScope);
    }
    void SetParentScope(UINT32 ParentScope)
    {
        m_ParentScope = VAL32(ParentScope);
    }

    ULONG32 StartOffset()
    {
        return VAL32(m_StartOffset);
    }
    void SetStartOffset(ULONG32 StartOffset)
    {
        m_StartOffset = VAL32(StartOffset);
    }
    ULONG32 EndOffset()
    {
        return VAL32(m_EndOffset);
    }
    void SetEndOffset(ULONG32 EndOffset)
    {
        m_EndOffset = VAL32(EndOffset);
    }
    BOOL    HasChildren()
    {
        return m_HasChildren;
    }
    void SetHasChildren(BOOL HasChildren)
    {
        m_HasChildren = HasChildren;
    }
    BOOL    HasVars()
    {
        return m_HasVars;
    }
    void SetHasVars(BOOL HasVars)
    {
        m_HasVars = HasVars;
    }

};

/* ------------------------------------------------------------------------- *
 * SymUsingNamespace struct
 * ------------------------------------------------------------------------- */
struct SymUsingNamespace
{
private:

    UINT32  m_ParentScope;  // index of parent scope
    UINT32  m_Name;         // Index of name
public:
    UINT32  ParentScope()
    {
        return VAL32(m_ParentScope);
    }
    void SetParentScope(UINT32 ParentScope)
    {
        m_ParentScope = VAL32(ParentScope);
    }
    UINT32  Name()
    {
        return VAL32(m_Name);
    }
    void SetName(UINT32 Name)
    {
        m_Name = VAL32(Name);
    }
};

/* ------------------------------------------------------------------------- *
 * SymConstant struct
 * ------------------------------------------------------------------------- */
struct SymConstant
{
private:

    VARIANT m_Value;   // Constant Value
    UINT32 m_ParentScope;   // Parent scope
    UINT32 m_Name;          // Name index
    UINT32 m_Signature;     // Signature index
    ULONG32 m_SignatureSize;// Signature size
    UINT32 m_ValueBstr; // If the variant is a bstr, store the string

public:
    UINT32  ParentScope()
    {
        return VAL32(m_ParentScope);
    }
    void SetParentScope(UINT32 ParentScope)
    {
        m_ParentScope = VAL32(ParentScope);
    }
    UINT32  Name()
    {
        return VAL32(m_Name);
    }
    void SetName(UINT32 Name)
    {
        m_Name = VAL32(Name);
    }
    UINT32 Signature()
    {
        return VAL32(m_Signature);
    }
    void SetSignature(UINT32 Signature)
    {
        m_Signature = VAL32(Signature);
    }
    ULONG32 SignatureSize()
    {
        return VAL32(m_SignatureSize);
    }
    void SetSignatureSize(ULONG32 SignatureSize)
    {
        m_SignatureSize = VAL32(SignatureSize);
    }
    VARIANT Value(UINT32 *pValueBstr)
    {   
        *pValueBstr = VAL32(m_ValueBstr);
#if BIGENDIAN
        VARIANT VariantValue;
        VariantInit(&VariantValue);
        // VT_BSTR's are dealt with ValueBStr
        if (m_ValueBstr)
        {
            V_VT(&VariantValue) = VT_BSTR;
        }
        else
        {
            VariantSwap(false, &VariantValue, &m_Value);
        }
        return VariantValue;
#else   
        return m_Value;
#endif
    }
    void SetValue(VARIANT VariantValue, UINT32 ValueBstr)
    {
        m_Value = VariantValue;
        m_ValueBstr = VAL32(ValueBstr);
#if BIGENDIAN
        // VT_BSTR's are dealt with ValueBStr
        if (m_ValueBstr)
        {
            V_VT(&m_Value) = VAL16(VT_BSTR);
        }
        else
        {
            VariantSwap(true, &m_Value, &VariantValue);
        }
#endif
    }
};

/* ------------------------------------------------------------------------- *
 * SymMethodInfo struct
 * ------------------------------------------------------------------------- */
struct SymMethodInfo
{
private:

    mdMethodDef     m_MethodToken;    // Method token

    // Start/End Entries into the respective tables
    // End values are extents - one past the last index (and so may actually be an index off
    // the end of the array).  Start may equal end if the method has none of the item.
    UINT32          m_StartScopes;
    UINT32          m_EndScopes;
    UINT32          m_StartVars;      
    UINT32          m_EndVars;
    UINT32          m_StartUsing;
    UINT32          m_EndUsing;
    UINT32          m_StartConstant;
    UINT32          m_EndConstant;
    UINT32          m_StartDocuments;
    UINT32          m_EndDocuments;
    UINT32          m_StartSequencePoints;
    UINT32          m_EndSequencePoints;

public:
    static int __cdecl compareMethods(const void *elem1, const void *elem2 );

    mdMethodDef     MethodToken()
    {
        return VAL32(m_MethodToken);
    }
    void SetMethodToken(mdMethodDef MethodToken)
    {
        m_MethodToken = VAL32(MethodToken);
    }
    UINT32  StartScopes() 
    { 
        return VAL32(m_StartScopes);
    }
    void SetStartScopes(UINT32 StartScopes)
    {
        m_StartScopes = VAL32(StartScopes);
    }
    UINT32  EndScopes()
    {
        return VAL32(m_EndScopes);
    }
    void SetEndScopes(UINT32 EndScopes)
    {
        m_EndScopes = VAL32(EndScopes);
    }
    UINT32 StartVars()
    {
        return VAL32(m_StartVars);
    }
    void SetStartVars(UINT32 StartVars)
    {
        m_StartVars = VAL32(StartVars);
    }
    UINT32 EndVars()
    {
        return VAL32(m_EndVars);
    }
    void SetEndVars(UINT32 EndVars)
    {
        m_EndVars = VAL32(EndVars);
    }
    UINT32 StartUsing()
    {
        return VAL32(m_StartUsing);
    }
    void SetStartUsing(UINT32 StartUsing)
    {
        m_StartUsing = VAL32(StartUsing);
    }
    UINT32 EndUsing()
    {
        return VAL32(m_EndUsing);
    }
    void SetEndUsing(UINT32 EndUsing)
    {
        m_EndUsing = VAL32(EndUsing);
    }
    UINT32 StartConstant()
    {
        return VAL32(m_StartConstant);
    }
    void SetStartConstant(UINT32 StartConstant)
    {
        m_StartConstant = VAL32(StartConstant);
    }
    UINT32 EndConstant()
    {
        return VAL32(m_EndConstant);
    }
    void SetEndConstant(UINT32 EndConstant)
    {
        m_EndConstant = VAL32(EndConstant);
    }
    UINT32 StartDocuments()
    {
        return VAL32(m_StartDocuments);
    }
    void SetStartDocuments(UINT32 StartDocuments)
    {
        m_StartDocuments = VAL32(StartDocuments);
    }
    UINT32 EndDocuments()
    {
        return VAL32(m_EndDocuments);
    }
    void SetEndDocuments(UINT32 EndDocuments)
    {
        m_EndDocuments = VAL32(EndDocuments);
    }
    UINT32 StartSequencePoints()
    {
        return VAL32(m_StartSequencePoints);
    }
    void SetStartSequencePoints(UINT32 StartSequencePoints)
    {
        m_StartSequencePoints = VAL32(StartSequencePoints);
    }
    UINT32 EndSequencePoints()
    {
        return VAL32(m_EndSequencePoints);
    }
    void SetEndSequencePoints(UINT32 EndSequencePoints)
    {
        m_EndSequencePoints = VAL32(EndSequencePoints);
    }
};

/* ------------------------------------------------------------------------- *
 * SymMap struct
 * ------------------------------------------------------------------------- */
struct SymMap
{
    mdMethodDef     m_MethodToken;    // New Method token
    UINT32          MethodEntry;      // Method Entry
};

/* ------------------------------------------------------------------------- *
 * SequencePoint struct
 * ------------------------------------------------------------------------- */
struct SequencePoint {

private:

    DWORD   m_Offset;
    DWORD   m_StartLine;
    DWORD   m_StartColumn;
    DWORD   m_EndLine;
    DWORD   m_EndColumn;
    DWORD   m_Document;

public:
    bool IsWithin(ULONG32 line, ULONG32 column);
    bool IsWithinLineOnly(ULONG32 line);
    bool IsGreaterThan(ULONG32 line, ULONG32 column);
    bool IsLessThan(ULONG32 line, ULONG32 column);
    bool IsUserLine();
    static int __cdecl compareAuxLines(const void *elem1, const void *elem2 );

    DWORD Offset()
    {
        return VAL32(m_Offset);
    }
    void SetOffset(DWORD Offset)
    {
        m_Offset = VAL32(Offset);
    }
    DWORD StartLine()
    {
        return VAL32(m_StartLine);
    }
    void SetStartLine(DWORD StartLine)
    {
        m_StartLine = VAL32(StartLine);
    }

    DWORD StartColumn()
    {
        return VAL32(m_StartColumn);
    }
    void SetStartColumn(DWORD StartColumn)
    {
        m_StartColumn = VAL32(StartColumn);
    }

    DWORD EndLine()
    {
        return VAL32(m_EndLine);
    }
    void SetEndLine(DWORD EndLine)
    {
        m_EndLine = VAL32(EndLine);
    }
    DWORD EndColumn()
    {
        return VAL32(m_EndColumn);
    }
    void SetEndColumn(DWORD EndColumn)
    {
        m_EndColumn = VAL32(EndColumn);
    }
    DWORD Document()
    {
        return VAL32(m_Document);
    }
    void SetDocument(DWORD Document)
    {
        m_Document = VAL32(Document);
    }
};


/* ------------------------------------------------------------------------- *
 * DocumentInfo struct
 * ------------------------------------------------------------------------- */
typedef struct DocumentInfo {

private:

    GUID                m_Language;
    GUID                m_LanguageVendor;
    GUID                m_DocumentType;
    GUID                m_AlgorithmId;
    DWORD               m_CheckSumSize;
    UINT32              m_CheckSumEntry;
    UINT32              m_SourceSize;
    UINT32              m_SourceEntry;
    UINT32              m_UrlEntry;
    SymDocumentWriter * m_pDocumentWriter;

public:

    GUID Language()
    {
        GUID TmpGuid = m_Language;
        SwapGuid(&TmpGuid);
        return TmpGuid;
    }
    void SetLanguage(GUID Language)
    {
        SwapGuid(&Language);
        m_Language = Language;
    }
    GUID LanguageVendor()
    {
        GUID TmpGuid = m_LanguageVendor;
        SwapGuid(&TmpGuid);
        return TmpGuid;
    }
    void SetLanguageVendor(GUID LanguageVendor)
    {
        SwapGuid(&LanguageVendor);
        m_LanguageVendor = LanguageVendor;
    }
    GUID DocumentType()
    {
        GUID TmpGuid = m_DocumentType;
        SwapGuid(&TmpGuid);
        return TmpGuid;
    }
    void SetDocumentType(GUID DocumentType)
    {
        SwapGuid(&DocumentType);
        m_DocumentType = DocumentType;
    }

    // Set the pointer to the SymDocumentWriter instance corresponding to this instance of DocumentInfo
    // An argument of NULL will call Release
    void SetDocumentWriter(SymDocumentWriter * pDoc);

    // get the associated SymDocumentWriter
    SymDocumentWriter * DocumentWriter()
    {
        return m_pDocumentWriter;
    }

    GUID AlgorithmId()
    {
        GUID TmpGuid = m_AlgorithmId;
        SwapGuid(&TmpGuid);
        return TmpGuid;
    }
    void SetAlgorithmId(GUID AlgorithmId)
    {
        SwapGuid(&AlgorithmId);
        m_AlgorithmId = AlgorithmId;
    }

    DWORD CheckSumSize()
    {
        return VAL32(m_CheckSumSize);
    }
    void SetCheckSymSize(DWORD CheckSumSize)
    {
        m_CheckSumSize = VAL32(CheckSumSize);
    }
    UINT32 CheckSumEntry()
    {
        return VAL32(m_CheckSumEntry);
    }
    void SetCheckSumEntry(UINT32 CheckSumEntry)
    {
        m_CheckSumEntry = VAL32(CheckSumEntry);
    }
    UINT32 SourceSize()
    {
        return VAL32(m_SourceSize);
    }
    void SetSourceSize(UINT32 SourceSize)
    {
        m_SourceSize = VAL32(SourceSize);
    }
    UINT32 SourceEntry()
    {
        return VAL32(m_SourceEntry);
    }
    void SetSourceEntry(UINT32 SourceEntry)
    {
        m_SourceEntry = VAL32(SourceEntry);
    }
    UINT32 UrlEntry()
    {
        return VAL32(m_UrlEntry);
    }
    void SetUrlEntry(UINT32 UrlEntry)
    {
        m_UrlEntry = VAL32(UrlEntry);
    }

} DocumentInfo; 

template <class T>
class ArrayStorage
{
public:

    ArrayStorage( int initialSize = 0 )
        : m_spaceSize(0),  m_instanceCount( 0 ), m_array( NULL )
    {
        grow( initialSize );
    }
    ~ArrayStorage()
    {
        
        if ( m_array )
            DELETEARRAY(m_array);
        m_array = NULL;
        m_spaceSize = 0;
        m_instanceCount = 0;
    }
    T* next() 
    {
        if( !grow ( m_instanceCount ) ) 
            return NULL;
        _ASSERTE( m_instanceCount < m_spaceSize );
        return &m_array[ m_instanceCount++ ];
    }
    bool grab(UINT32 n, UINT32 * pIndex) 
    {
        S_UINT32 newSize = S_UINT32(m_instanceCount) + S_UINT32(n);
        if (newSize.IsOverflow())
            return false;
        if (!grow(newSize.Value())) 
            return false;
        _ASSERTE( m_instanceCount < m_spaceSize );
        *pIndex = m_instanceCount;
        m_instanceCount += n;
        return true;
    }

    T& operator[]( UINT32 i ) {
        _ASSERTE( i < m_instanceCount );
        if (i >= m_instanceCount)
        {
            // Help mitigate the impact of buffer overflow
            // Fail fast with a null-reference AV
            return *(static_cast<T*>(0)) ;
        }
        return m_array[ i ];
    }
    void reset() {
        m_instanceCount = 0;
    }
    UINT32 size() {
        return m_spaceSize;
    }
    UINT32 count() {
        return m_instanceCount;
    }

    UINT32      m_spaceSize;     // Total size of array in elements
    UINT32      m_instanceCount;   // total T's in the file
    T          *m_array;         // array of T's
private:
    bool grow( UINT32 n )
    {
        if (n >= m_spaceSize)
        {
            // Make a new, bigger array.
            UINT32 newSpaceSize;

            if (n == 0)
                newSpaceSize = DEF_LOCAL_SPACE;
            else
                newSpaceSize = max( m_spaceSize * 2, n);

            // Make sure we're not asking for more than 4GB of bytes to ensure no integer-overflow attacks are possible
            S_UINT32 newBytes = S_UINT32(newSpaceSize) * S_UINT32(sizeof(T));
            if (newBytes.IsOverflow())
                return false;

            T *newTs;
            newTs = NEW(T[newSpaceSize]);
            if ( newTs == NULL )
                return false;

            // Copy over the old Ts.
            memcpy(newTs, m_array,
                   sizeof(T) * m_spaceSize);

            // Delete the old Ts.
            DELETEARRAY(m_array);

            // Hang onto the new array.
            m_array = newTs;
            m_spaceSize = newSpaceSize;
        }
        return true;
    }

};

typedef struct MethodInfo {

    ArrayStorage<SymMethodInfo> m_methods;    // Methods information
    ArrayStorage<SymLexicalScope> m_scopes;   // Scope information for the method
    ArrayStorage<SymVariable> m_vars;         // Variables
    ArrayStorage<SymUsingNamespace> m_usings; // using/imports
    ArrayStorage<SymConstant> m_constants;    // Constants
    ArrayStorage<DocumentInfo> m_documents;   // Document Source Format
    ArrayStorage<SequencePoint>  m_auxSequencePoints;  // Sequence Points
    // Array of various bytes (variable signature, etc)
    ArrayStorage<BYTE>  m_bytes;


public:

  MethodInfo() :
      m_bytes( DEF_MISC_SPACE )
  {
  }
} MethodInfo;

/* ------------------------------------------------------------------------- *
 * SymWriter class
 * ------------------------------------------------------------------------- */

class SymWriter : public ISymUnmanagedWriter3
{
public:
    SymWriter();
    virtual ~SymWriter();

    //-----------------------------------------------------------
    // IUnknown support
    //-----------------------------------------------------------
    ULONG STDMETHODCALLTYPE AddRef()
    {
        return (InterlockedIncrement((LONG *) &m_refCount));
    }

    ULONG STDMETHODCALLTYPE Release()
    {
        // Note that this must be thread-safe - it may be invoked on the finalizer thread
        LONG refCount = InterlockedDecrement((LONG *) &m_refCount);
        if (refCount == 0)
            DELETE(this);

        return (refCount);
    }
    COM_METHOD QueryInterface(REFIID riid, void **ppInterface);

    //-----------------------------------------------------------
    // ISymUnmanagedWriter
    //-----------------------------------------------------------
    COM_METHOD DefineDocument(const WCHAR *url,
                              const GUID *language,
                              const GUID *languageVendor,
                              const GUID *documentType,
                              ISymUnmanagedDocumentWriter **pRetVal);
    COM_METHOD SetUserEntryPoint(mdMethodDef entryMethod);
    COM_METHOD OpenMethod(mdMethodDef method);
    COM_METHOD CloseMethod();
    COM_METHOD DefineSequencePoints(ISymUnmanagedDocumentWriter *document,
                                    ULONG32 spCount,
                                    ULONG32 offsets[],
                                    ULONG32 lines[],
                                    ULONG32 columns[],
                                    ULONG32 endLines[],
                                    ULONG32 encColumns[]);
    COM_METHOD OpenScope(ULONG32 startOffset, ULONG32 *scopeID);
    COM_METHOD CloseScope(ULONG32 endOffset);
    COM_METHOD SetScopeRange(ULONG32 scopeID, ULONG32 startOffset, ULONG32 endOffset);
    COM_METHOD DefineLocalVariable(const WCHAR *name,
                                   ULONG32 attributes,
                                   ULONG32 cSig,
                                   BYTE signature[],
                                   ULONG32 addrKind,
                                   ULONG32 addr1, ULONG32 addr2, ULONG32 addr3,
                                   ULONG32 startOffset, ULONG32 endOffset);
    COM_METHOD DefineParameter(const WCHAR *name,
                               ULONG32 attributes,
                               ULONG32 sequence,
                               ULONG32 addrKind,
                               ULONG32 addr1, ULONG32 addr2, ULONG32 addr3);
    COM_METHOD DefineField(mdTypeDef parent,
                           const WCHAR *name,
                           ULONG32 attributes,
                           ULONG32 cSig,
                           BYTE signature[],
                           ULONG32 addrKind,
                           ULONG32 addr1, ULONG32 addr2, ULONG32 addr3);
    COM_METHOD DefineGlobalVariable(const WCHAR *name,
                                    ULONG32 attributes,
                                    ULONG32 cSig,
                                    BYTE signature[],
                                    ULONG32 addrKind,
                                    ULONG32 addr1, ULONG32 addr2, ULONG32 addr3);
    COM_METHOD Close();
    COM_METHOD SetSymAttribute(mdToken parent,
                               const WCHAR *name,
                               ULONG32 cData,
                               BYTE data[]);
    COM_METHOD OpenNamespace(const WCHAR *name);
    COM_METHOD CloseNamespace();
    COM_METHOD UsingNamespace(const WCHAR *fullName);
    COM_METHOD SetMethodSourceRange(ISymUnmanagedDocumentWriter *startDoc,
                                    ULONG32 startLine,
                                    ULONG32 startColumn,
                                    ISymUnmanagedDocumentWriter *endDoc,
                                    ULONG32 endLine,
                                    ULONG32 endColumn);
    COM_METHOD GetDebugCVInfo(DWORD cData,
                           DWORD *pcData,
                           BYTE data[]);

    COM_METHOD Initialize(IUnknown *emitter,
                        const WCHAR *filename,
                        IStream *pIStream,
                        BOOL fFullBuild);

    COM_METHOD Initialize2(IUnknown *emitter,
                        const WCHAR *pdbTempPath,   // location to write pdb file
                        IStream *pIStream,
                        BOOL fFullBuild,
                        const WCHAR *pdbFinalPath); // location exe should contain for pdb file
                        
    COM_METHOD GetDebugInfo(IMAGE_DEBUG_DIRECTORY *pIDD,
                         DWORD cData,
                         DWORD *pcData,
                         BYTE data[]);

    COM_METHOD RemapToken(mdToken oldToken,
                          mdToken newToken);

    COM_METHOD DefineConstant(const WCHAR __RPC_FAR *name,
                        VARIANT value,
                        ULONG32 cSig,
                        unsigned char __RPC_FAR signature[  ]);
    
    COM_METHOD Abort(void);

    //-----------------------------------------------------------
    // ISymUnmanagedWriter2
    //-----------------------------------------------------------
    COM_METHOD DefineLocalVariable2(const WCHAR *name,
                        ULONG32 attributes,
                        mdSignature sigToken,
                        ULONG32 addrKind,
                        ULONG32 addr1,
                        ULONG32 addr2,
                        ULONG32 addr3,
                        ULONG32 startOffset,
                        ULONG32 endOffset);

    COM_METHOD DefineGlobalVariable2(const WCHAR *name,
                        ULONG32 attributes,
                        mdSignature sigToken,
                        ULONG32 addrKind,
                        ULONG32 addr1,
                        ULONG32 addr2,
                        ULONG32 addr3);

    COM_METHOD DefineConstant2(const WCHAR *name,
                        VARIANT value,
                        mdSignature sigToken);

    //-----------------------------------------------------------
    // ISymUnmanagedWriter3
    //-----------------------------------------------------------

    COM_METHOD OpenMethod2(mdMethodDef method,
                        ULONG32 isect,
                        ULONG32 offset);

    COM_METHOD Commit();

    //-----------------------------------------------------------
    // Methods not exposed via a COM interface.
    //-----------------------------------------------------------

    static HRESULT NewSymWriter(REFIID clsid, void** ppObj);
    HRESULT SetDocumentCheckSum(
            UINT32 DocumentEntry,
            GUID  AlgorithmId,
            DWORD CheckSumSize,
            BYTE* pCheckSum);
    HRESULT SetDocumentSrc(UINT32 DocumentEntry,
            DWORD SourceSize,
            BYTE* pSource);

    COM_METHOD Write(void *pData, DWORD SizeOfData);
    COM_METHOD WriteStringPool();
    COM_METHOD WritePDB();

    COM_METHOD Initialize(const WCHAR *szFilename, IStream *pIStream);

    void SetFullPathName(const WCHAR *szFullPathName)
    {
        
    }

private:
    // Helper API for CloserScope
    COM_METHOD CloseScopeInternal(ULONG32 endOffset);
    HRESULT GetOrCreateDocument(
        const WCHAR *wcsUrl,          // Document name
        const GUID *pLanguage,        // What Language we're compiling
        const GUID *pLanguageVendor,  // What vendor
        const GUID *pDocumentType,    // Type
        ISymUnmanagedDocumentWriter **ppRetVal // [out] Created DocumentWriter
    );
    HRESULT CreateDocument(
        const WCHAR *wcsUrl,          // Document name
        const GUID *pLanguage,        // What Language we're compiling
        const GUID *pLanguageVendor,  // What vendor
        const GUID *pDocumentType,    // Type
        ISymUnmanagedDocumentWriter **ppRetVal // [out] Created DocumentWriter
    );


    //-----------------------------------------------------------
    // Data members
    //-----------------------------------------------------------
private:
    UINT32      m_refCount; // AddRef/Release

    mdMethodDef         m_openMethodToken;
    mdMethodDef         m_LargestMethodToken;
    SymMethodInfo     * m_pmethod;

    // index of currently open scope
    UINT32 m_currentScope;

    // special scope "index" meaning there is no such scope
    static const UINT32 k_noScope = (UINT32)-1;

    // maximum scope end offset seen so far in this method
    ULONG32 m_maxScopeEnd;

    MethodInfo m_MethodInfo;
    ArrayStorage<SymMap> m_MethodMap;    // Methods information

    // Symbol File Name
    WCHAR m_szPath[ _MAX_PATH ];
    // File Handle
    HANDLE m_hFile;
    // Stream we're storing into if asked to.
    IStream* m_pIStream;

    // StringPool we use to store the string into
    StgStringPool *m_pStringPool;

    // Project level symbol information
    PDBInfo ModuleLevelInfo;

    bool                m_closed;       // Have we closed the file yet?
    bool                m_sortLines;    // sort the line for current method
    bool                m_sortMethodEntries; // Sort the method entries


};

/* ------------------------------------------------------------------------- *
 * SymDocumentWriter class
 * ------------------------------------------------------------------------- */

class SymDocumentWriter : public ISymUnmanagedDocumentWriter
{
public:
    SymDocumentWriter(UINT32 DocumentEntry,
                      SymWriter  *pEmitter);

    virtual ~SymDocumentWriter();

    //-----------------------------------------------------------
    // IUnknown support
    //-----------------------------------------------------------
    ULONG STDMETHODCALLTYPE AddRef()
    {
        return (InterlockedIncrement((LONG *) &m_refCount));
    }

    ULONG STDMETHODCALLTYPE Release()
    {
        // Note that this must be thread-safe - it may be invoked on the finalizer thread
        LONG refCount = InterlockedDecrement((LONG *) &m_refCount);
        if (refCount == 0)
            DELETE(this);

        return (refCount);
    }
    COM_METHOD QueryInterface(REFIID riid, void **ppInterface);

    //-----------------------------------------------------------
    // ISymUnmanagedDocumentWriter
    //-----------------------------------------------------------
    COM_METHOD SetSource(ULONG32 sourceSize, BYTE source[]);
    COM_METHOD SetCheckSum(GUID algorithmId,
                           ULONG32 checkSumSize, BYTE checkSum[]);

    //-----------------------------------------------------------
    // Methods not exposed via a COM interface.
    //-----------------------------------------------------------
    //
    // Commit the doc to the pdb
    //
    UINT32 GetDocumentEntry()
    {
        return m_DocumentEntry;
    }

    //-----------------------------------------------------------
    // Data members
    //-----------------------------------------------------------
private:
    UINT32 m_refCount; // AddRef/Release
    UINT32 m_DocumentEntry; // Entry into the documents array
    SymWriter *m_pEmitter;  // Associated SymWriter
};

// Debug Info
struct RSDSI                       // RSDS debug info
{
    DWORD   dwSig;                 // RSDS
    GUID    guidSig;
    DWORD   age;
    char    szPDB[0];  // followed by a zero-terminated UTF8 file name
};

#endif /* SYMWRITE_H_ */