summaryrefslogtreecommitdiff
path: root/src/vm/typeparse.h
blob: 66cbd86282959e8da4dbcd911a0f238165c4c280 (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
// 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.
// ---------------------------------------------------------------------------
// typeparse.h
// ---------------------------------------------------------------------------
//

//

 
#ifndef TYPEPARSE_H
#define TYPEPARSE_H

#include "common.h"
#include "class.h"
#include "typehandle.h"

// To work around a warning about redefining "TypeName" include the file
// that defines Windows.UI.Xaml.Interop.TypeName now.
#ifdef FEATURE_COMINTEROP
#include <windows.ui.xaml.interop.h>
#endif

//#define TYPE_NAME_RESERVED_CHAR W(",[]&*+\\")

bool inline IsTypeNameReservedChar(WCHAR ch)
{
    LIMITED_METHOD_CONTRACT;

    switch (ch)
    {
    case W(','):
    case W('['):
    case W(']'):
    case W('&'):
    case W('*'):
    case W('+'):
    case W('\\'):
        return true;

    default:
        return false;
    }
}


DomainAssembly * LoadDomainAssembly(
    SString *  psszAssemblySpec, 
    Assembly * pRequestingAssembly, 
    ICLRPrivBinder * pPrivHostBinder,
    BOOL       bThrowIfNotFound, 
    BOOL       bIntrospectionOnly, 
    SString *  pssOuterTypeName);

class TypeNameFactory : public ITypeNameFactory
{    
public:
    static HRESULT CreateObject(REFIID riid, void **ppUnk);
    
public:
    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppUnk);
    virtual ULONG STDMETHODCALLTYPE AddRef() { LIMITED_METHOD_CONTRACT; m_count++; return m_count; }
    virtual ULONG STDMETHODCALLTYPE Release() { LIMITED_METHOD_CONTRACT; SUPPORTS_DAC_HOST_ONLY; m_count--; ULONG count = m_count; if (count == 0) delete this; return count; }

public:
    virtual HRESULT STDMETHODCALLTYPE ParseTypeName(LPCWSTR szName, DWORD* pError, ITypeName** ppTypeName);
    virtual HRESULT STDMETHODCALLTYPE GetTypeNameBuilder(ITypeNameBuilder** ppTypeBuilder);

public:
    TypeNameFactory() : m_count(0)
    {
        WRAPPER_NO_CONTRACT;
        SString::Startup();
    }

    virtual ~TypeNameFactory() {}
        
private:
    DWORD m_count;
};

class TypeName : public ITypeName
{
private:
    template<typename PRODUCT>
    class Factory
    {
    public:
        const static DWORD MAX_PRODUCT = 4;
        
    public:
        Factory() : m_cProduct(0), m_next(NULL) { LIMITED_METHOD_CONTRACT; }
        ~Factory()
        {
            CONTRACTL
            {
                NOTHROW;
                SO_TOLERANT;
            }
            CONTRACTL_END;
            VALIDATE_BACKOUT_STACK_CONSUMPTION;               

            if (m_next) 
                delete m_next; 
          } 

        PRODUCT* Create()
            { WRAPPER_NO_CONTRACT; if (m_cProduct == (INT32)MAX_PRODUCT) return GetNext()->Create(); return &m_product[m_cProduct++]; }

    private:
        Factory* GetNext() { if (!m_next) m_next = new Factory<PRODUCT>(); return m_next; }   

    private:
        PRODUCT m_product[MAX_PRODUCT];
        INT32 m_cProduct;
        Factory* m_next;
    };
    friend class TypeName::Factory<TypeName>;
    friend class TypeNameBuilder;

private:
    class TypeNameParser
    {
        TypeNameParser(LPCWSTR szTypeName, TypeName* pTypeName, DWORD* pError) 
        {
            CONTRACTL
            {
                THROWS;
                GC_NOTRIGGER;
                MODE_ANY;
            }
            CONTRACTL_END;

            if (szTypeName == NULL)
            {
                szTypeName = W("");
            }

            m_currentToken = TypeNameEmpty;
            m_nextToken = TypeNameEmpty;

            *pError = (DWORD)-1;
            m_pTypeName = pTypeName;
            m_sszTypeName = szTypeName;
            m_currentItr = m_itr = m_sszTypeName; 

            if (!START())
                *pError = (DWORD)(m_currentItr - m_sszTypeName) - 1;
        }

    private:
        friend class TypeName;
        
    private:
        typedef enum {
            //
            // TOKENS
            //
            TypeNameEmpty               = 0x8000,
            TypeNameIdentifier          = 0x0001,
            TypeNamePostIdentifier      = 0x0002,
            TypeNameOpenSqBracket       = 0x0004,
            TypeNameCloseSqBracket      = 0x0008,
            TypeNameComma               = 0x0010,
            TypeNamePlus                = 0x0020,
            TypeNameAstrix              = 0x0040,
            TypeNameAmperstand          = 0x0080,
            TypeNameBackSlash           = 0x0100,
            TypeNameEnd                 = 0x4000,

            //
            // 1 TOKEN LOOK AHEAD 
            //
            TypeNameNAME                = TypeNameIdentifier,
            TypeNameNESTNAME            = TypeNameIdentifier,
            TypeNameASSEMSPEC           = TypeNameIdentifier, 
            TypeNameGENPARAM            = TypeNameOpenSqBracket | TypeNameEmpty,
            TypeNameFULLNAME            = TypeNameNAME,
            TypeNameAQN                 = TypeNameFULLNAME | TypeNameEnd,
            TypeNameASSEMBLYSPEC        = TypeNameIdentifier,
            TypeNameGENARG              = TypeNameOpenSqBracket | TypeNameFULLNAME,
            TypeNameGENARGS             = TypeNameGENARG,
            TypeNameEAQN                = TypeNameIdentifier,
            TypeNameEASSEMSPEC          = TypeNameIdentifier,
            TypeNameARRAY               = TypeNameOpenSqBracket,
            TypeNameQUALIFIER           = TypeNameAmperstand | TypeNameAstrix | TypeNameARRAY | TypeNameEmpty,
            TypeNameRANK                = TypeNameComma | TypeNameEmpty,            
        } TypeNameTokens;

        typedef enum {
            TypeNameNone                = 0x00,
            TypeNameId                  = 0x01,
            TypeNameFusionName          = 0x02,
            TypeNameEmbeddedFusionName  = 0x03,
        } TypeNameIdentifiers;

    //
    // LEXIFIER 
    //
    private:
        TypeNameTokens LexAToken(BOOL ignorePlus = FALSE);
        BOOL GetIdentifier(SString* sszId, TypeNameIdentifiers identiferType);
        void NextToken()  { WRAPPER_NO_CONTRACT; m_currentToken = m_nextToken; m_currentItr = m_itr; m_nextToken = LexAToken(); }
        BOOL NextTokenIs(TypeNameTokens token) { LIMITED_METHOD_CONTRACT; return !!(m_nextToken & token); }
        BOOL TokenIs(TypeNameTokens token) { LIMITED_METHOD_CONTRACT; return !!(m_currentToken & token); }
        BOOL TokenIs(int token) { LIMITED_METHOD_CONTRACT; return TokenIs((TypeNameTokens)token); }
        
    //
    // PRODUCTIONS
    //
    private: 
        BOOL START();
        
        BOOL AQN();
        // /* empty */
        // FULLNAME ',' ASSEMSPEC
        // FULLNAME
      
        BOOL ASSEMSPEC();
        // fusionName
        
        BOOL FULLNAME();
        // NAME GENPARAMS QUALIFIER
        
        BOOL GENPARAMS();
        // *empty*
        // '[' GENARGS ']'        
        
        BOOL GENARGS();
        // GENARG
        // GENARG ',' GENARGS
                
        BOOL GENARG();
        // '[' EAQN ']'
        // FULLNAME
        
        BOOL EAQN();
        // FULLNAME ',' EASSEMSPEC
        // FULLNAME
        
        BOOL EASSEMSPEC();
        // embededFusionName
        
        BOOL QUALIFIER();
        // *empty*
        // '&'
        // *' QUALIFIER
        // ARRAY QUALIFIER
        
        BOOL ARRAY();
        // '[' RANK ']'
        // '[' '*' ']'
        
        BOOL RANK(DWORD* pdwRank);
        // *empty*
        // ',' RANK
        
        BOOL NAME();
        // id
        // id '+' NESTNAME
        
        BOOL NESTNAME();
        // id
        // id '+' NESTNAME
  
    public:
        void MakeRotorHappy() { WRAPPER_NO_CONTRACT; }
    
    private:
        TypeName* m_pTypeName;
        LPCWSTR m_sszTypeName;
        LPCWSTR m_itr;
        LPCWSTR m_currentItr;
        TypeNameTokens m_currentToken;
        TypeNameTokens m_nextToken;
    };
    friend class TypeName::TypeNameParser;
    
public:
    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppUnk);
    virtual ULONG STDMETHODCALLTYPE AddRef();
    virtual ULONG STDMETHODCALLTYPE Release();

public:
    virtual HRESULT STDMETHODCALLTYPE GetNameCount(DWORD* pCount);
    virtual HRESULT STDMETHODCALLTYPE GetNames(DWORD count, BSTR* rgbszNames, DWORD* pFetched);
    virtual HRESULT STDMETHODCALLTYPE GetTypeArgumentCount(DWORD* pCount);
    virtual HRESULT STDMETHODCALLTYPE GetTypeArguments(DWORD count, ITypeName** rgpArguments, DWORD* pFetched);
    virtual HRESULT STDMETHODCALLTYPE GetModifierLength(DWORD* pCount);
    virtual HRESULT STDMETHODCALLTYPE GetModifiers(DWORD count, DWORD* rgModifiers, DWORD* pFetched);
    virtual HRESULT STDMETHODCALLTYPE GetAssemblyName(BSTR* rgbszAssemblyNames);
    
public:
    TypeName(LPCWSTR szTypeName, DWORD* pError) : m_bIsGenericArgument(FALSE), m_count(0) 
    {
        CONTRACTL
        {
            THROWS;
            GC_NOTRIGGER;
            MODE_ANY;
        }
        CONTRACTL_END;
        TypeNameParser parser(szTypeName, this, pError); 
        parser.MakeRotorHappy(); 
    }

    virtual ~TypeName();
    
public:
#ifndef CROSSGEN_COMPILE
    static void QCALLTYPE QCreateTypeNameParser (LPCWSTR wszTypeName, QCall::ObjectHandleOnStack pNames, BOOL throwOnError);
    static void QCALLTYPE QReleaseTypeNameParser(TypeName * pTypeName);
    static void QCALLTYPE QGetNames             (TypeName * pTypeName, QCall::ObjectHandleOnStack pNames);
    static void QCALLTYPE QGetTypeArguments     (TypeName * pTypeName, QCall::ObjectHandleOnStack pTypeArguments);
    static void QCALLTYPE QGetModifiers         (TypeName * pTypeName, QCall::ObjectHandleOnStack pModifiers);
    static void QCALLTYPE QGetAssemblyName      (TypeName * pTypeName, QCall::StringHandleOnStack pAssemblyName);
#endif //CROSSGEN_COMPILE

    //-------------------------------------------------------------------------------------------
    // Retrieves a type from an assembly. It requires the caller to know which assembly
    // the type is in.
    //-------------------------------------------------------------------------------------------
    static TypeHandle GetTypeFromAssembly(LPCWSTR szTypeName, Assembly *pAssembly, BOOL bThrowIfNotFound = TRUE);

    TypeHandle GetTypeFromAsm(BOOL bForIntrospection);

    //-------------------------------------------------------------------------------------------
    // Retrieves a type. Will assert if the name is not fully qualified.
    //-------------------------------------------------------------------------------------------
    static TypeHandle GetTypeFromAsmQualifiedName(LPCWSTR szFullyQualifiedName, BOOL bForIntrospection);


    //-------------------------------------------------------------------------------------------
    // This version is used for resolving types named in custom attributes such as those used
    // for interop. Thus, it follows a well-known multistage set of rules for determining which
    // assembly the type is in. It will also enforce that the requesting assembly has access
    // rights to the type being loaded.
    //
    // The search logic is:
    //
    //    if szTypeName is ASM-qualified, only that assembly will be searched.
    //    if szTypeName is not ASM-qualified, we will search for the types in the following order:
    //       - in pRequestingAssembly (if not NULL). pRequestingAssembly is the assembly that contained
    //         the custom attribute from which the typename was derived.
    //       - in mscorlib.dll
    //       - raise an AssemblyResolveEvent() in the current appdomain
    //
    // pRequestingAssembly may be NULL. In that case, the "visibility" check will simply check that
    // the loaded type has public access.
    //
    //--------------------------------------------------------------------------------------------
    static TypeHandle GetTypeUsingCASearchRules(LPCUTF8 szTypeName, Assembly *pRequestingAssembly, BOOL *pfTypeNameWasQualified = NULL, BOOL bDoVisibilityChecks = TRUE);
    static TypeHandle GetTypeUsingCASearchRules(LPCWSTR szTypeName, Assembly *pRequestingAssembly, BOOL *pfTypeNameWasQualified = NULL, BOOL bDoVisibilityChecks = TRUE);


    //--------------------------------------------------------------------------------------------------------------
    // This everything-but-the-kitchen-sink version is what used to be called "GetType()". It exposes all the
    // funky knobs needed for implementing the specific requirements of the managed Type.GetType() apis and friends.
    // Really that knowledge shouldn't even be embedded in the TypeParse class at all but for now, we'll
    // settle for giving this entrypoint a really ugly name so that only the two FCALL's that really need it will call
    // it.
    //--------------------------------------------------------------------------------------------------------------
    static TypeHandle GetTypeManaged(
        LPCWSTR szTypeName, 
        DomainAssembly* pAssemblyGetType,
        BOOL bThrowIfNotFound,
        BOOL bIgnoreCase,
        BOOL bIntrospectionOnly,
        BOOL bProhibitAssemblyQualifiedName,
        StackCrawlMark* pStackMark,
        BOOL bLoadTypeFromPartialNameHack,
        OBJECTREF *pKeepAlive,
        ICLRPrivBinder * pPrivHostBinder = nullptr);
    
    
public:
    SString* GetAssembly() { WRAPPER_NO_CONTRACT; return &m_assembly; }
    
private:
    TypeName() : m_bIsGenericArgument(FALSE), m_count(0) { LIMITED_METHOD_CONTRACT; }    
    TypeName* AddGenericArgument();
    
    SString* AddName() 
    { 
        CONTRACTL
        {
            THROWS;
            GC_NOTRIGGER;
            MODE_ANY;
        }
        CONTRACTL_END;
        
        return m_names.AppendEx(m_nestNameFactory.Create()); 
    }

    SArray<SString*>& GetNames() { WRAPPER_NO_CONTRACT; return m_names; }
    SArray<TypeName*>& GetGenericArguments() { WRAPPER_NO_CONTRACT; return m_genericArguments; }
    SArray<DWORD>& GetSignature() { WRAPPER_NO_CONTRACT; return m_signature; }
    void SetByRef() { WRAPPER_NO_CONTRACT; m_signature.Append(ELEMENT_TYPE_BYREF); }
    void SetPointer() { WRAPPER_NO_CONTRACT;  m_signature.Append(ELEMENT_TYPE_PTR); }
    void SetSzArray() { WRAPPER_NO_CONTRACT; m_signature.Append(ELEMENT_TYPE_SZARRAY); }
    
    void SetArray(DWORD rank) 
    { 
        CONTRACTL
        {
            THROWS;
            GC_NOTRIGGER;
            MODE_ANY;
        }
        CONTRACTL_END;
        m_signature.Append(ELEMENT_TYPE_ARRAY); 
        m_signature.Append(rank); 
    } 
    
    SString* ToString(SString* pBuf, BOOL bAssemblySpec = FALSE, BOOL bSignature = FALSE, BOOL bGenericArguments = FALSE);
        
private:
    //----------------------------------------------------------------------------------------------------------------
    // This is the "uber" GetType() that all public GetType() funnels through. It's main job is to figure out which
    // Assembly to load the type from and then invoke GetTypeHaveAssembly.
    //
    // It's got a highly baroque interface partly for historical reasons and partly because it's the uber-function
    // for all of the possible GetTypes.
    //----------------------------------------------------------------------------------------------------------------
    TypeHandle GetTypeWorker(
        BOOL bThrowIfNotFound, 
        BOOL bIgnoreCase, 
        BOOL bIntrospectionOnly, 
        Assembly* pAssemblyGetType,

        BOOL fEnableCASearchRules,  
                                    
        BOOL bProhibitAssemblyQualifiedName,
                                    
        StackCrawlMark* pStackMark, 
        Assembly* pRequestingAssembly, 
        ICLRPrivBinder * pPrivHostBinder,
        BOOL bLoadTypeFromPartialNameHack,
        OBJECTREF *pKeepAlive);    

    //----------------------------------------------------------------------------------------------------------------
    // These functions are the ones that actually loads the type once we've pinned down the Assembly it's in.
    //----------------------------------------------------------------------------------------------------------------
    TypeHandle GetTypeHaveAssembly(Assembly* pAssembly, BOOL bThrowIfNotFound, BOOL bIgnoreCase, OBJECTREF *pKeepAlive)
    {
        return GetTypeHaveAssemblyHelper(pAssembly, bThrowIfNotFound, bIgnoreCase, pKeepAlive, TRUE);
    }
    TypeHandle GetTypeHaveAssemblyHelper(Assembly* pAssembly, BOOL bThrowIfNotFound, BOOL bIgnoreCase, OBJECTREF *pKeepAlive, BOOL bRecurse);
    SAFEHANDLE GetSafeHandle();

private:
    BOOL m_bIsGenericArgument;
    DWORD m_count;
    InlineSArray<DWORD, 128> m_signature;
    InlineSArray<TypeName*, 16> m_genericArguments;
    InlineSArray<SString*, 16> m_names;
    InlineSString<128> m_assembly;
    Factory<InlineSString<128> > m_nestNameFactory;
};

#endif