summaryrefslogtreecommitdiff
path: root/src/vm/typectxt.cpp
blob: e2d6abaa95cc287327a716d7e4ae691daa6aca7d (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
// 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.
//
// typectxt.cpp
//

//
// Simple struct to record the data necessary to interpret ELEMENT_TYPE_VAR
// and ELEMENT_TYPE_MVAR within pieces of metadata, in particular within 
// signatures parsed by MetaSig and SigPointer.
//



#include "common.h"
#include "method.hpp"
#include "typehandle.h"
#include "field.h"



void SigTypeContext::InitTypeContext(MethodDesc *md, Instantiation exactClassInst, Instantiation exactMethodInst, SigTypeContext *pRes)
{
    LIMITED_METHOD_CONTRACT;
    STATIC_CONTRACT_SO_TOLERANT;
    MethodTable *pMT = md->GetMethodTable();

    if (pMT->IsArray())
    {
        pRes->m_classInst = exactClassInst.IsEmpty() ? pMT->GetClassOrArrayInstantiation() : exactClassInst;
    }
    else
    {
        pRes->m_classInst = exactClassInst;
    }
    pRes->m_methodInst = exactMethodInst;
}

void SigTypeContext::InitTypeContext(MethodDesc *md, SigTypeContext *pRes)
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        FORBID_FAULT;
        SO_TOLERANT;
        SUPPORTS_DAC;
 
        PRECONDITION(CheckPointer(md));
    } CONTRACTL_END;
 
    MethodTable *pMT = md->GetMethodTable();
    if (pMT->IsArray())
    {
        pRes->m_classInst = pMT->GetClassOrArrayInstantiation();
    }
    else
    {
        pRes->m_classInst = pMT->GetInstantiation();
    }
    pRes->m_methodInst = md->GetMethodInstantiation();
}
 
void SigTypeContext::InitTypeContext(MethodDesc *md, TypeHandle declaringType, SigTypeContext *pRes)
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        FORBID_FAULT;
        SO_TOLERANT;
        SUPPORTS_DAC;
 
        PRECONDITION(CheckPointer(md));
    } CONTRACTL_END;
 
    if (declaringType.IsNull())
    {
        SigTypeContext::InitTypeContext(md, pRes);
    }
    else 
    {
        MethodTable *pMDMT = md->GetMethodTable();
        if (pMDMT->IsArray())
        {
            pRes->m_classInst = declaringType.GetClassOrArrayInstantiation();
        }
        else 
        {
            pRes->m_classInst = declaringType.GetInstantiationOfParentClass(pMDMT);
        }
        pRes->m_methodInst = md->GetMethodInstantiation();
    }
}
 
#ifndef DACCESS_COMPILE
TypeHandle GetDeclaringMethodTableFromTypeVarTypeDesc(TypeVarTypeDesc *pTypeVar, MethodDesc *pMD)
{
    LIMITED_METHOD_CONTRACT;

    // This currently should only happen in cases where we've already loaded the constraints.
    // Currently, the only known case where use this code is reflection over methods exposed on a TypeVariable.
    _ASSERTE(pTypeVar->ConstraintsLoaded());

    if (pTypeVar->ConstraintsLoaded())
    {
        DWORD cConstraints;
        TypeHandle *pTypeHandles = pTypeVar->GetCachedConstraints(&cConstraints);
        for (DWORD iConstraint = 0; iConstraint < cConstraints; iConstraint++)
        {
            if (pTypeHandles[iConstraint].IsGenericVariable())
            {
                TypeHandle th = GetDeclaringMethodTableFromTypeVarTypeDesc(pTypeHandles[iConstraint].AsGenericVariable(), pMD);
                if (!th.IsNull())
                    return th;
            }
            else
            {
                MethodTable *pMT = pTypeHandles[iConstraint].GetMethodTable();
                while (pMT != NULL)
                {
                    if (pMT == pMD->GetMethodTable())
                    {
                        return TypeHandle(pMT);
                    }

                    pMT = pMT->GetParentMethodTable();
                }
            }
        }
    }
    return TypeHandle();
}

void SigTypeContext::InitTypeContext(MethodDesc *md, TypeHandle declaringType, Instantiation exactMethodInst, SigTypeContext *pRes)
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        FORBID_FAULT;
        SO_TOLERANT;
 
        PRECONDITION(CheckPointer(md));
    } CONTRACTL_END;
 
    if (declaringType.IsNull())
    {
        SigTypeContext::InitTypeContext(md, pRes);
    }
    else 
    {
        // <TODO> factor this with the work above </TODO>
        if (declaringType.IsGenericVariable())
        {
            declaringType = GetDeclaringMethodTableFromTypeVarTypeDesc(declaringType.AsGenericVariable(), md);
        }

        if (declaringType.IsNull())
        {
            SigTypeContext::InitTypeContext(md, pRes);
        }
        else
        {
            MethodTable *pMDMT = md->GetMethodTable();
            if (pMDMT->IsArray())
            {
                pRes->m_classInst = declaringType.GetClassOrArrayInstantiation();
            }
            else 
            {
                pRes->m_classInst = declaringType.GetInstantiationOfParentClass(pMDMT);
            }
        }
    }
    pRes->m_methodInst = !exactMethodInst.IsEmpty() ? exactMethodInst : md->GetMethodInstantiation();
}
#endif // !DACCESS_COMPILE

void SigTypeContext::InitTypeContext(FieldDesc *pFD, TypeHandle declaringType, SigTypeContext *pRes)
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        FORBID_FAULT;
        SO_TOLERANT;
 
        PRECONDITION(CheckPointer(declaringType, NULL_OK));
        PRECONDITION(CheckPointer(pFD));
    } CONTRACTL_END;
    LIMITED_METHOD_CONTRACT;
    InitTypeContext(pFD->GetExactClassInstantiation(declaringType),Instantiation(), pRes);
}

 
void SigTypeContext::InitTypeContext(TypeHandle th, SigTypeContext *pRes)
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        FORBID_FAULT;
        SO_TOLERANT;
    } CONTRACTL_END;

    if (th.IsNull())
    {
        InitTypeContext(pRes);
    }
    else if (th.GetMethodTable()->IsArray())
    {
        InitTypeContext(th.GetMethodTable()->GetClassOrArrayInstantiation(), Instantiation(), pRes);
    }
    else
    {
        InitTypeContext(th.GetInstantiation(), Instantiation(), pRes);
    }
}


const SigTypeContext * SigTypeContext::GetOptionalTypeContext(MethodDesc *md, TypeHandle declaringType, SigTypeContext *pRes)
{ 
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        SO_TOLERANT;
        MODE_ANY;
    }
    CONTRACTL_END;
    
    _ASSERTE(md);
    if (md->HasClassOrMethodInstantiation()  || md->GetMethodTable()->IsArray())
    {
        SigTypeContext::InitTypeContext(md, declaringType,pRes);
        return pRes;
    }
    else
    {
        _ASSERTE(pRes->m_classInst.IsEmpty());
        _ASSERTE(pRes->m_methodInst.IsEmpty());
        return NULL;
    }
}

const SigTypeContext * SigTypeContext::GetOptionalTypeContext(TypeHandle th, SigTypeContext *pRes)
{ 
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        SO_TOLERANT;
        MODE_ANY;
    }
    CONTRACTL_END;
    
    _ASSERTE (!th.IsNull());
    if (th.HasInstantiation() || th.GetMethodTable()->IsArray())
    { 
        SigTypeContext::InitTypeContext(th,pRes);
        return pRes;
    }
    else
    {
        // It should already have been null-initialized when allocated on the stack.
        _ASSERTE(pRes->m_classInst.IsEmpty());
        _ASSERTE(pRes->m_methodInst.IsEmpty());
        return NULL;
    }
}

BOOL SigTypeContext::IsValidTypeOnlyInstantiationOf(const SigTypeContext *pCtxTypicalMethodInstantiation, const SigTypeContext *pCtxTypeOnlyInstantiation)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        SO_TOLERANT;
        MODE_ANY;
    }
    CONTRACTL_END;

    // Compare class inst counts
    if (pCtxTypicalMethodInstantiation->m_classInst.GetNumArgs() != pCtxTypeOnlyInstantiation->m_classInst.GetNumArgs())
        return FALSE;

    // Compare method inst counts
    if (pCtxTypicalMethodInstantiation->m_methodInst.GetNumArgs() != pCtxTypeOnlyInstantiation->m_methodInst.GetNumArgs())
        return FALSE;

    DWORD i;

    // Ensure that no type variables are part of the instantiation of the generic type
    for (i = 0; i < pCtxTypicalMethodInstantiation->m_classInst.GetNumArgs(); i++) {
        if (pCtxTypeOnlyInstantiation->m_classInst[i].IsGenericVariable())
            return FALSE;
    }

    // Compare method inst values to ensure they represent the same generic method parameters
    for (i = 0; i < pCtxTypicalMethodInstantiation->m_methodInst.GetNumArgs(); i++) {
        _ASSERTE(pCtxTypicalMethodInstantiation->m_methodInst[i].IsGenericVariable());

        if (pCtxTypicalMethodInstantiation->m_methodInst[i] != pCtxTypeOnlyInstantiation->m_methodInst[i])
            return FALSE;
    }

    return TRUE;
}

BOOL SigTypeContext::Equal(const SigTypeContext *pCtx1, const SigTypeContext *pCtx2)
{
    WRAPPER_NO_CONTRACT;

    // Compare class inst counts
    if (pCtx1->m_classInst.GetNumArgs() != pCtx2->m_classInst.GetNumArgs())
        return FALSE;

    // Compare method inst counts
    if (pCtx1->m_methodInst.GetNumArgs() != pCtx2->m_methodInst.GetNumArgs())
        return FALSE;

    DWORD i;

    // Compare class inst values
    for (i = 0; i < pCtx1->m_classInst.GetNumArgs(); i++) {
        if (pCtx1->m_classInst[i] != pCtx2->m_classInst[i])
            return FALSE;
    }

    // Compare method inst values
    for (i = 0; i < pCtx1->m_methodInst.GetNumArgs(); i++) {
        if (pCtx1->m_methodInst[i] != pCtx2->m_methodInst[i])
            return FALSE;
    }

    return TRUE;
}