summaryrefslogtreecommitdiff
path: root/src/vm/customattribute.h
blob: f2e5ee9c1b5b466df1ca3fb6b387feaf88ce0b89 (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
// 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.


#ifndef _CUSTOMATTRIBUTE_H_
#define _CUSTOMATTRIBUTE_H_

#include "fcall.h"
#include "../md/compiler/custattr.h"

struct CustomAttributeType;
struct CustomAttributeValue;
struct CustomAttributeArgument;
struct CustomAttributeNamedArgument;

typedef Array<CustomAttributeArgument> CaArgArray;
typedef Array<CustomAttributeNamedArgument> CaNamedArgArray;
typedef Array<CustomAttributeValue> CaValueArray;

typedef DPTR(CaArgArray) PTR_CaArgArray;
typedef DPTR(CaNamedArgArray) PTR_CaNamedArgArray;
typedef DPTR(CaValueArray) PTR_CaValueArray;

#ifdef USE_CHECKED_OBJECTREFS
typedef REF<CaArgArray> CaArgArrayREF;
typedef REF<CaNamedArgArray> CaNamedArgArrayREF;
typedef REF<CaValueArray> CaValueArrayREF;
#else
typedef PTR_CaArgArray CaArgArrayREF;
typedef PTR_CaNamedArgArray CaNamedArgArrayREF;
typedef PTR_CaValueArray CaValueArrayREF;
#endif


#include <pshpack1.h>
struct CustomAttributeType
{
    STRINGREF m_enumName;
    CorSerializationType m_tag;
    CorSerializationType m_enumType;
    CorSerializationType m_arrayType;    
    CorSerializationType m_padding;
};

struct CustomAttributeValue
{
#ifdef _WIN64
    // refs come before longs on win64
    CaValueArrayREF     m_value;
    STRINGREF           m_enumOrTypeName;
    INT64               m_rawValue;
#else
    // longs come before refs on x86
    INT64               m_rawValue;
    CaValueArrayREF     m_value;
    STRINGREF           m_enumOrTypeName;
#endif
    CustomAttributeType m_type;
#if defined(FEATURE_64BIT_ALIGNMENT)
    DWORD m_padding;
#endif
};

struct CustomAttributeArgument
{
    CustomAttributeType m_type;
#if (!defined(_WIN64) && (DATA_ALIGNMENT > 4)) || defined(FEATURE_64BIT_ALIGNMENT)
    DWORD m_padding;
#endif
    CustomAttributeValue m_value;
};

struct CustomAttributeNamedArgument
{
    STRINGREF m_argumentName;
    CorSerializationType m_propertyOrField;
    CorSerializationType m_padding;
#if !defined(_WIN64) && (DATA_ALIGNMENT > 4)
    DWORD m_padding2;
#endif
    CustomAttributeType m_type;
#if !defined(_WIN64) && (DATA_ALIGNMENT > 4)
    DWORD m_padding3;
#endif
    CustomAttributeValue m_value;
};
#include <poppack.h>


typedef struct {
    STRINGREF string;
    CaValueArrayREF array;
} CustomAttributeManagedValues;

typedef Factory< SArray<CaValue> > CaValueArrayFactory;

class Attribute
{
public:
    static FCDECL5(VOID, ParseAttributeArguments, 
        void* pCa, 
        INT32 cCa, 
        CaArgArrayREF* ppCustomAttributeArguments, 
        CaNamedArgArrayREF* ppCustomAttributeNamedArguments,
        AssemblyBaseObject* pAssemblyUNSAFE);

private:
    static HRESULT ParseAttributeArgumentValues(
        void* pCa,
        INT32 cCa,
        CaValueArrayFactory* pCaValueArrayFactory,
        CaArg* pCaArgs,
        COUNT_T cArgs,
        CaNamedArg* pCaNamedArgs,
        COUNT_T cNamedArgs,
        DomainAssembly* pDomainAssembly);

    static HRESULT ParseCaValue(
        CustomAttributeParser &ca,
        CaValue* pCaArg,
        CaType* pCaParam,
        CaValueArrayFactory* pCaValueArrayFactory,
        DomainAssembly* pDomainAssembly);

    static HRESULT ParseCaCtorArgs(
        CustomAttributeParser &ca,
        CaArg* pArgs,
        ULONG cArgs,
        CaValueArrayFactory* pCaValueArrayFactory,
        DomainAssembly* pDomainAssembly);

    static HRESULT ParseCaNamedArgs(
        CustomAttributeParser &ca, // The Custom Attribute blob.
        CaNamedArg *pNamedParams,  // Array of argument descriptors.
        ULONG cNamedParams,
        CaValueArrayFactory* pCaValueArrayFactory,
        DomainAssembly* pDomainAssembly);

    static HRESULT InitCaType(
        CustomAttributeType* pType,
        Factory<SString>* pSstringFactory,
        Factory<StackScratchBuffer>* pStackScratchBufferFactory,
        CaType* pCaType);

    static HRESULT ParseCaType(
        CustomAttributeParser &ca,
        CaType* pCaType,
        DomainAssembly* pDomainAssembly,
        StackSString* ss = NULL);

    static TypeHandle GetTypeForEnum(
        LPCUTF8 szEnumName,
        COUNT_T cbEnumName,
        DomainAssembly* pDomainAssembly);

    static void SetBlittableCaValue(
        CustomAttributeValue* pVal,
        CaValue* pCaVal,
        BOOL* pbAllBlittableCa);

    static void SetManagedValue(
        CustomAttributeManagedValues gc,
        CustomAttributeValue* pValue);

    static CustomAttributeManagedValues GetManagedCaValue(CaValue* pCaVal);
};

class COMCustomAttribute
{
public:

    // custom attributes utility functions
    static FCDECL5(VOID, ParseAttributeUsageAttribute, PVOID pData, ULONG cData, ULONG* pTargets, CLR_BOOL* pInherited, CLR_BOOL* pAllowMultiple);
    static FCDECL6(LPVOID, CreateCaObject, ReflectModuleBaseObject* pAttributedModuleUNSAFE, ReflectClassBaseObject* pCaTypeUNSAFE, ReflectMethodObject *pMethodUNSAFE, BYTE** ppBlob, BYTE* pEndBlob, INT32* pcNamedArgs);
    static FCDECL7(void, GetPropertyOrFieldData, ReflectModuleBaseObject *pModuleUNSAFE, BYTE** ppBlobStart, BYTE* pBlobEnd, STRINGREF* pName, CLR_BOOL* pbIsProperty, OBJECTREF* pType, OBJECTREF* value);
    static FCDECL4(VOID, GetSecurityAttributes, ReflectModuleBaseObject *pModuleUNSAFE, DWORD tkToken, CLR_BOOL fAssembly, PTRARRAYREF* ppArray);

private:

    static TypeHandle GetTypeHandleFromBlob(
        Assembly *pCtorAssembly,
        CorSerializationType objType, 
        BYTE **pBlob, 
        const BYTE *endBlob,
        Module *pModule);

    static ARG_SLOT GetDataFromBlob(
        Assembly *pCtorAssembly,
        CorSerializationType type, 
        TypeHandle th, 
        BYTE **pBlob, 
        const BYTE *endBlob, 
        Module *pModule, 
        BOOL *bObjectCreated);

    static void ReadArray(
        Assembly *pCtorAssembly,
        CorSerializationType arrayType, 
        int size, 
        TypeHandle th,
        BYTE **pBlob, 
        const BYTE *endBlob, 
        Module *pModule,
        BASEARRAYREF *pArray);

    static int GetStringSize(
        BYTE **pBlob,
        const BYTE *endBlob);

    template < typename T >
    static BOOL CopyArrayVAL(
        BASEARRAYREF pArray,
        int nElements,
        BYTE **pBlob,
        const BYTE *endBlob);
};

#endif