summaryrefslogtreecommitdiff
path: root/src/ilasm/typar.hpp
blob: ad5cb0e9978b1deaa786b0dbd721138c818006b5 (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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. 
//
/**************************************************************************/
/* a type parameter list */

#ifndef TYPAR_H
#define TYPAR_H
#include "binstr.h"

extern unsigned int g_uCodePage;

class TyParDescr
{
public:
    TyParDescr()
    {
        m_pbsBounds = NULL;
        m_wzName = NULL;
        m_dwAttrs = 0;
    };
    ~TyParDescr()
    {
        delete m_pbsBounds;
        delete [] m_wzName;
        m_lstCA.RESET(true);
    };
    void Init(BinStr* bounds, LPCUTF8 name, DWORD attrs)
    {
        m_pbsBounds = bounds;
        ULONG               cTemp = (ULONG)strlen(name)+1;
        WCHAR *pwzName;
        m_wzName = pwzName = new WCHAR[cTemp];
        if(pwzName)
        {
            memset(pwzName,0,sizeof(WCHAR)*cTemp);
            WszMultiByteToWideChar(g_uCodePage,0,name,-1,pwzName,cTemp);
        }
        m_dwAttrs = attrs;
    };
    BinStr* Bounds() { return m_pbsBounds; };
    LPCWSTR Name() { return m_wzName; };
    DWORD   Attrs() { return m_dwAttrs; };
    CustomDescrList* CAList() { return &m_lstCA; };
private:
    BinStr* m_pbsBounds;
    LPCWSTR m_wzName;
    DWORD   m_dwAttrs;
    CustomDescrList m_lstCA;
};

class TyParList {
public:
	TyParList(DWORD a, BinStr* b, LPCUTF8 n, TyParList* nx = NULL) 
    { 
        bound  = (b == NULL) ? new BinStr() : b;
        bound->appendInt32(0); // zero terminator
		attrs = a; name = n; next = nx;
	};
	~TyParList() 
    { 
        if(bound) delete bound;
		if (next) delete next; 
	};
    int Count()
    {
        TyParList* tp = this;
        int n;
        for(n = 1; (tp = tp->next) != NULL; n++);
        return n;
    };
    int IndexOf(LPCUTF8 name)
    {
        TyParList* tp = this;
        int n;
        int ret = -1;
        for(n=0; tp != NULL; n++, tp = tp->next)
        {
            if(tp->name == NULL)
            {
                if(name == NULL) ret = n;
            }
            else
            {
                if(name == NULL) continue;
                if(0 == strcmp(name,tp->name)) ret = n;
            }
        }
        return ret;
    };

#ifdef _PREFAST_
#pragma warning(push)
#pragma warning(disable:6211) // "Leaking memory 'b' due to an exception. Consider using a local catch block to clean up memory"
#endif /*_PREFAST_ */

    int ToArray(BinStr ***bounds, LPCWSTR** names, DWORD **attrs)
    {   
        int n = Count();
        BinStr **b = new BinStr* [n];
        LPCWSTR *nam = new LPCWSTR [n];
        DWORD *attr = attrs ? new DWORD [n] : NULL;
        TyParList *tp = this;
        int i = 0;
        while (tp)
        {
            ULONG               cTemp = (ULONG)strlen(tp->name)+1;
            WCHAR*              wzDllName = new WCHAR [cTemp];
            // Convert name to UNICODE
            memset(wzDllName,0,sizeof(WCHAR)*cTemp);
            WszMultiByteToWideChar(g_uCodePage,0,tp->name,-1,wzDllName,cTemp);
            nam[i] = (LPCWSTR)wzDllName;
            b[i] = tp->bound;
            if (attr) 
                attr[i] = tp->attrs;
            tp->bound = 0; // to avoid deletion by destructor
            i++;
            tp = tp->next;
        }
        *bounds = b;
        *names = nam;          
        if (attrs)
            *attrs = attr;
        return n;
    };

#ifdef _PREFAST_
#pragma warning(pop)
#endif /*_PREFAST_*/

    int ToArray(TyParDescr **ppTPD)
    {
        int n = Count();
        TyParDescr *pTPD = NULL;
        if(n)
        {
            pTPD = new TyParDescr[n];
            if(pTPD)
            {
                int i = 0;
                TyParList *tp = this;
                while (tp)
                {
                    pTPD[i].Init(tp->bound,tp->name,tp->attrs);
                    tp->bound = 0; // to avoid deletion by destructor
                    i++;
                    tp = tp->next;
                }
            }
        }
        *ppTPD = pTPD;
        return n;
    };
    TyParList* Next() { return next; };
    BinStr* Bound() { return bound; };
private:
	BinStr* bound;
    LPCUTF8 name;
    TyParList* next;
    DWORD   attrs;
};

typedef TyParList* pTyParList;

#endif