Merge pull request #9562 from AndyAyersMS/FixMagicTypeEquality
[platform/upstream/coreclr.git] / src / vm / mscorlib.cpp
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 //
5
6
7 // 
8 // This file defines tables for references between VM and mscorlib.
9 //
10 // When compiling crossgen, this file is compiled with the FEATURE_XXX define settings matching the target.
11 // It allows us to strip features (e.g. refection only load) from crossgen without stripping them from the target.
12 //
13
14 #ifdef CROSSGEN_MSCORLIB
15 // Use minimal set of headers for crossgen
16 #include "windows.h"
17 #include "corinfo.h"
18 #else
19 #include "common.h"
20 #include "ecall.h"
21 #endif // CROSSGEN_MSCORLIB
22
23 #ifndef CROSSGEN_COMPILE
24 //
25 // Headers for all ECall entrypoints
26 //
27 #include "arraynative.h"
28 #include "stringnative.h"
29 #include "stringbuffer.h"
30 #include "objectnative.h"
31 #include "comdelegate.h"
32 #include "customattribute.h"
33 #include "comdynamic.h"
34 #include "commethodrental.h"
35 #include "nlsinfo.h"
36 #include "calendardata.h"
37 #include "commodule.h"
38 #include "marshalnative.h"
39 #include "system.h"
40 #include "comutilnative.h"
41 #include "comsynchronizable.h"
42 #include "floatdouble.h"
43 #include "floatsingle.h"
44 #include "decimal.h"
45 #include "currency.h"
46 #include "comdatetime.h"
47 #include "comisolatedstorage.h"
48 #include "number.h"
49 #include "compatibilityswitch.h"
50 #ifdef FEATURE_REMOTING
51 #include "remotingnative.h"
52 #include "message.h"
53 #include "stackbuildersink.h"
54 #endif
55 #include "debugdebugger.h"
56 #include "assemblyname.hpp"
57 #include "assemblynative.hpp"
58 #include "rwlock.h"
59 #include "comthreadpool.h"
60 #include "comwaithandle.h"
61 #include "nativeoverlapped.h"
62
63 #include "proftoeeinterfaceimpl.h"
64
65 #include "appdomainnative.hpp"
66 #include "arrayhelpers.h"
67 #include "runtimehandles.h"
68 #include "reflectioninvocation.h"
69 #include "managedmdimport.hpp"
70 #include "synchronizationcontextnative.h"
71 #include "commemoryfailpoint.h"
72 #include "typestring.h"
73 #include "comdependenthandle.h"
74 #include "weakreferencenative.h"
75 #include "varargsnative.h"
76
77 #ifdef MDA_SUPPORTED 
78 #include "mdaassistants.h"
79 #endif
80
81 #include "coverage.h"
82
83 #ifdef FEATURE_COMINTEROP
84 #include "variant.h"
85 #include "oavariant.h"
86 #include "registration.h"
87 #include "mngstdinterfaces.h"
88 #include "extensibleclassfactory.h"
89 #endif // FEATURE_COMINTEROP
90
91 #include "stubhelpers.h"
92 #include "ilmarshalers.h"
93
94 #ifdef FEATURE_MULTICOREJIT
95 #include "multicorejit.h"
96 #endif
97
98 #ifdef FEATURE_COMINTEROP
99 #include "clrprivtypecachereflectiononlywinrt.h"
100 #endif
101
102 #ifdef FEATURE_COMINTEROP
103 #include "windowsruntimebufferhelper.h"
104 #endif
105
106 #if defined(FEATURE_EVENTSOURCE_XPLAT)
107 #include "nativeeventsource.h"
108 #endif //defined(FEATURE_EVENTSOURCE_XPLAT)
109
110 #endif // CROSSGEN_MSCORLIB
111
112
113 #ifdef CROSSGEN_MSCORLIB
114
115 ///////////////////////////////////////////////////////////////////////////////
116 //
117 // Duplicate definitions of constants and datastructures required to define the tables
118 //
119
120 #define NumItems(s) (sizeof(s) / sizeof(s[0]))
121
122 #define GetEEFuncEntryPoint(pfn) 0x1001
123
124 enum {
125     FCFuncFlag_EndOfArray   = 0x01,
126     FCFuncFlag_HasSignature = 0x02,
127     FCFuncFlag_Unreferenced = 0x04, // Suppress unused fcall check
128     FCFuncFlag_QCall        = 0x08, // QCall - mscorlib.dll to mscorwks.dll transition implemented as PInvoke
129 };
130
131 struct ECClass
132 {
133     LPCSTR      m_szClassName;
134     LPCSTR      m_szNameSpace;
135     const LPVOID *  m_pECFunc;
136 };
137
138 struct HardCodedMetaSig
139 {
140     const BYTE* m_pMetaSig; // metasig prefixed with INT8 length:
141                             // length > 0 - resolved, lenght < 0 - has unresolved type references
142 };
143
144 enum BinderClassID
145 {
146 #define TYPEINFO(e,ns,c,s,g,ia,ip,if,im,gv)   CLASS__ ## e,
147 #include "cortypeinfo.h"
148 #undef TYPEINFO
149
150 #define DEFINE_CLASS(i,n,s)         CLASS__ ## i,
151 #include "mscorlib.h"
152
153     CLASS__MSCORLIB_COUNT,
154
155     CLASS__VOID     = CLASS__ELEMENT_TYPE_VOID,
156     CLASS__BOOLEAN  = CLASS__ELEMENT_TYPE_BOOLEAN,
157     CLASS__CHAR     = CLASS__ELEMENT_TYPE_CHAR,
158     CLASS__BYTE     = CLASS__ELEMENT_TYPE_U1,
159     CLASS__SBYTE    = CLASS__ELEMENT_TYPE_I1,
160     CLASS__INT16    = CLASS__ELEMENT_TYPE_I2,
161     CLASS__UINT16   = CLASS__ELEMENT_TYPE_U2,
162     CLASS__INT32    = CLASS__ELEMENT_TYPE_I4,
163     CLASS__UINT32   = CLASS__ELEMENT_TYPE_U4,
164     CLASS__INT64    = CLASS__ELEMENT_TYPE_I8,
165     CLASS__UINT64   = CLASS__ELEMENT_TYPE_U8,
166     CLASS__SINGLE   = CLASS__ELEMENT_TYPE_R4,
167     CLASS__DOUBLE   = CLASS__ELEMENT_TYPE_R8,
168     CLASS__STRING   = CLASS__ELEMENT_TYPE_STRING,
169     CLASS__TYPED_REFERENCE = CLASS__ELEMENT_TYPE_TYPEDBYREF,
170     CLASS__INTPTR   = CLASS__ELEMENT_TYPE_I,
171     CLASS__UINTPTR  = CLASS__ELEMENT_TYPE_U,
172     CLASS__OBJECT   = CLASS__ELEMENT_TYPE_OBJECT
173 };
174
175 struct MscorlibClassDescription
176 {
177     LPCSTR  nameSpace;
178     LPCSTR  name;
179 };
180
181 struct MscorlibMethodDescription
182 {
183     BinderClassID classID;
184     LPCSTR  name;
185     const HardCodedMetaSig * sig;
186 };
187
188 struct MscorlibFieldDescription
189 {
190     BinderClassID classID;
191     LPCSTR  name;
192 };
193
194 #endif // CROSSGEN_MSCORLIB
195
196
197 #ifdef CROSSGEN_MSCORLIB
198 // When compiling crossgen this namespace creates the second version of the tables than matches the target
199 namespace CrossGenMscorlib {
200 #endif
201
202
203 ///////////////////////////////////////////////////////////////////////////////
204 //
205 // Hardcoded Meta-Sig
206 //
207
208 //
209 // Helper enum with metasig lengths
210 //
211 // iterate over the metasig recursing into the complex types
212 #define DEFINE_METASIG(body)            body,
213 #define METASIG_ATOM(x)                 + 1
214 #define METASIG_RECURSE                 1
215 #define SM(varname, args, retval)       gsigl_SM_ ## varname = 1 + 1 retval args
216 #define IM(varname, args, retval)       gsigl_IM_ ## varname = 1 + 1 retval args
217 #define GM(varname, conv, n, args, retval) gsigl_GM_ ## varname = 1 + 1 + 1 + retval args
218 #define Fld(varname, val)               gsigl_Fld_ ## varname = 1 val
219 enum _gsigl {
220 #include "metasig.h"
221 };
222
223 //
224 // Helper enum with metasig argcount
225 //
226 // iterate over the metasig without recursing into the complex types
227 #define DEFINE_METASIG(body)            body,
228 #define METASIG_ATOM(x)                 + 1
229 #define METASIG_RECURSE                 0
230 #define SM(varname, args, retval)       gsigc_SM_ ## varname = 0 args
231 #define IM(varname, args, retval)       gsigc_IM_ ## varname = 0 args
232 #define GM(varname, conv, n, args, retval) gsigc_GM_ ## varname = 0 args
233 #define Fld(varname, val)               gsigc_Fld_ ## varname = 0
234 enum _gsigc {
235 #include "metasig.h"
236 };
237
238
239 //
240 // The actual array with the hardcoded metasig:
241 //
242 // There are 3 variations of the macros for Fields, Static Methods and Instance Methods.
243 //
244 // Each of them has 2 flavors: one for the fully baked signatures, and the other 
245 // for the signatures with unresolved type references
246 //
247 // The signatures with unresolved type references are marked with negative size, 
248 // and the pointer to them is non-const because of it will be overwritten with
249 // the pointer to the resolved signature at runtime.
250 //
251
252 #define DEFINE_METASIG(body)            body
253 #define DEFINE_METASIG_T(body)          _##body
254 #define METASIG_ATOM(x)                 x,
255 #define METASIG_RECURSE                 1
256
257 // define gsig_ ## varname before gsige_ ## varname to give a hint to the compiler about the desired layout
258
259 #define SM(varname, args, retval) extern const BYTE gsige_SM_ ## varname[];     \
260     const HardCodedMetaSig gsig_SM_ ## varname = { gsige_SM_ ## varname };      \
261     const BYTE gsige_SM_ ## varname[] = { gsigl_SM_ ## varname,                 \
262         IMAGE_CEE_CS_CALLCONV_DEFAULT, gsigc_SM_ ## varname, retval args };
263
264 #define IM(varname, args, retval) extern const BYTE gsige_IM_ ## varname[];     \
265     const HardCodedMetaSig gsig_IM_ ## varname = { gsige_IM_ ## varname };      \
266     const BYTE gsige_IM_ ## varname[] = { gsigl_IM_ ## varname,                 \
267         IMAGE_CEE_CS_CALLCONV_HASTHIS, gsigc_IM_ ## varname, retval args };
268
269 #define GM(varname, conv, n, args, retval) extern const BYTE gsige_GM_ ## varname[];    \
270     const HardCodedMetaSig gsig_GM_ ## varname = { gsige_GM_ ## varname };              \
271     const BYTE gsige_GM_ ## varname[] = { gsigl_GM_ ## varname,                         \
272         conv | IMAGE_CEE_CS_CALLCONV_GENERIC, n, gsigc_GM_ ## varname, retval args };
273
274 #define Fld(varname, val) extern const BYTE gsige_Fld_ ## varname[];            \
275     const HardCodedMetaSig gsig_Fld_ ## varname = { gsige_Fld_ ## varname };    \
276     const BYTE gsige_Fld_ ## varname[] = { gsigl_Fld_ ## varname,               \
277         IMAGE_CEE_CS_CALLCONV_FIELD, val };
278
279 #define _SM(varname, args, retval) extern const BYTE gsige_SM_ ## varname[];    \
280     HardCodedMetaSig gsig_SM_ ## varname = { gsige_SM_ ## varname };            \
281     const BYTE gsige_SM_ ## varname[] = { (BYTE) -gsigl_SM_ ## varname,         \
282         IMAGE_CEE_CS_CALLCONV_DEFAULT, gsigc_SM_ ## varname, retval args };
283
284 #define _IM(varname, args, retval) extern const BYTE gsige_IM_ ## varname[];    \
285     HardCodedMetaSig gsig_IM_ ## varname = { gsige_IM_ ## varname };            \
286     const BYTE gsige_IM_ ## varname[] = { (BYTE) -gsigl_IM_ ## varname,         \
287         IMAGE_CEE_CS_CALLCONV_HASTHIS, gsigc_IM_ ## varname, retval args };
288
289 #define _Fld(varname, val) extern const BYTE gsige_Fld_ ## varname[];           \
290     HardCodedMetaSig gsig_Fld_ ## varname = { gsige_Fld_ ## varname };          \
291     const BYTE gsige_Fld_ ## varname[] = { (BYTE) -gsigl_Fld_ ## varname,       \
292         IMAGE_CEE_CS_CALLCONV_FIELD, val };
293
294 #include "metasig.h"
295
296 #undef _SM
297 #undef _IM
298 #undef _Fld
299
300
301
302 #ifdef _DEBUG
303
304 //
305 // Make sure DEFINE_METASIG is used for signatures that do not reference other types
306 //
307 // counts number of type references in the signature and C_ASSERTs that 
308 // it is zero. An assertion failure results in error C2118: negative subscript.
309 #define DEFINE_METASIG(body)            body
310 #define DEFINE_METASIG_T(body)
311 #define METASIG_BODY(varname, types)    C_ASSERT(types 0 == 0);
312 #define METASIG_ATOM(x)                 0+
313 #define METASIG_RECURSE                 1
314 #define C(x)                            1+
315 #define g(x)                            1+
316 #include "metasig.h"
317
318 //
319 // Make sure DEFINE_METASIG_T is used only for signatures that reference 
320 // other types.
321 //
322 // counts number of type references in the signature and C_ASSERTs that 
323 // it is non zero. An assertion failure results in error C2118: negative subscript.
324 #define DEFINE_METASIG(body)
325 #define DEFINE_METASIG_T(body)          body
326 #define METASIG_BODY(varname, types)    C_ASSERT(types 0 != 0);
327 #define METASIG_ATOM(x)                 0+
328 #define METASIG_RECURSE                 1
329 #define C(x)                            1+
330 #define g(x)                            1+
331 #include "metasig.h"
332
333 #endif
334
335
336
337
338
339 ///////////////////////////////////////////////////////////////////////////////
340 //
341 // Mscorlib binder
342 //
343
344 // Extern definitions so that binder.cpp can see these tables
345 extern const MscorlibClassDescription c_rgMscorlibClassDescriptions[];
346 extern const USHORT c_nMscorlibClassDescriptions;
347
348 extern const MscorlibMethodDescription c_rgMscorlibMethodDescriptions[];
349 extern const USHORT c_nMscorlibMethodDescriptions;
350
351 extern const MscorlibFieldDescription c_rgMscorlibFieldDescriptions[];
352 extern const USHORT c_nMscorlibFieldDescriptions;
353
354 const MscorlibClassDescription c_rgMscorlibClassDescriptions[] =
355 {
356     #define TYPEINFO(e,ns,c,s,g,ia,ip,if,im,gv)   { ns, c },
357     #include "cortypeinfo.h"
358     #undef TYPEINFO
359     
360     #define DEFINE_CLASS(i,n,s)        { g_ ## n ## NS, # s },
361     #include "namespace.h"
362     #include "mscorlib.h"
363     
364     // Include all exception types here that are defined in mscorlib.  Omit exceptions defined elsewhere.
365     #define DEFINE_EXCEPTION(ns, reKind, bHRformessage, ...) { ns , # reKind },
366     #define DEFINE_EXCEPTION_HR_WINRT_ONLY(ns, reKind, ...)
367     #define DEFINE_EXCEPTION_IN_OTHER_FX_ASSEMBLY(ns, reKind, assemblySimpleName, publicKeyToken, bHRformessage, ...)
368     #include "rexcep.h"
369 };
370 const USHORT c_nMscorlibClassDescriptions = NumItems(c_rgMscorlibClassDescriptions);
371
372 #define gsig_NoSig (*(HardCodedMetaSig *)NULL)
373
374 const MscorlibMethodDescription c_rgMscorlibMethodDescriptions[] =
375 {
376     #define DEFINE_METHOD(c,i,s,g)          { CLASS__ ## c , # s, & gsig_ ## g },
377     #include "mscorlib.h"
378 };
379 const USHORT c_nMscorlibMethodDescriptions = NumItems(c_rgMscorlibMethodDescriptions) + 1;
380
381 const MscorlibFieldDescription c_rgMscorlibFieldDescriptions[] =
382 {
383     #define DEFINE_FIELD(c,i,s)           { CLASS__ ## c , # s },
384     #include "mscorlib.h"
385 };
386 const USHORT c_nMscorlibFieldDescriptions = NumItems(c_rgMscorlibFieldDescriptions) + 1;
387
388
389
390
391
392 ///////////////////////////////////////////////////////////////////////////////
393 //
394 // ECalls
395 //
396
397 // When compiling crossgen, we only need the target version of the ecall tables
398 #if !defined(CROSSGEN_COMPILE) || defined(CROSSGEN_MSCORLIB)
399
400 #ifdef CROSSGEN_COMPILE
401
402 #define QCFuncElement(name,impl) \
403     FCFuncFlag_QCall + FCFuncFlags(CORINFO_INTRINSIC_Illegal, ECall::InvalidDynamicFCallId), NULL, (LPVOID)name,
404
405 #define FCFuncFlags(intrinsicID, dynamicID) \
406     (BYTE*)( (((BYTE)intrinsicID) << 16) )
407
408 #else
409
410 #define QCFuncElement(name,impl) \
411     FCFuncFlag_QCall + FCFuncFlags(CORINFO_INTRINSIC_Illegal, ECall::InvalidDynamicFCallId), (LPVOID)(impl), (LPVOID)name,
412
413 #define FCFuncFlags(intrinsicID, dynamicID) \
414     (BYTE*)( (((BYTE)intrinsicID) << 16) + (((BYTE)dynamicID) << 24) )
415
416 #endif
417
418 #define FCFuncElement(name, impl) FCFuncFlags(CORINFO_INTRINSIC_Illegal, ECall::InvalidDynamicFCallId), \
419     (LPVOID)GetEEFuncEntryPoint(impl), (LPVOID)name,
420
421 #define FCFuncElementSig(name,sig,impl) \
422     FCFuncFlag_HasSignature + FCFuncElement(name, impl) (LPVOID)sig,
423
424 #define FCIntrinsic(name,impl,intrinsicID) FCFuncFlags(intrinsicID, ECall::InvalidDynamicFCallId), \
425     (LPVOID)GetEEFuncEntryPoint(impl), (LPVOID)name,
426
427 #define FCIntrinsicSig(name,sig,impl,intrinsicID) \
428     FCFuncFlag_HasSignature + FCIntrinsic(name,impl,intrinsicID) (LPVOID)sig,
429
430 #define FCDynamic(name,intrinsicID,dynamicID) FCFuncFlags(intrinsicID, dynamicID), \
431     NULL, (LPVOID)name,
432
433 #define FCDynamicSig(name,sig,intrinsicID,dynamicID) \
434     FCFuncFlag_HasSignature + FCDynamic(name,intrinsicID,dynamicID) (LPVOID)sig,
435
436 #define FCUnreferenced FCFuncFlag_Unreferenced +
437
438 #define FCFuncStart(name) static const LPVOID name[] = {
439 #define FCFuncEnd() FCFuncFlag_EndOfArray + FCFuncFlags(CORINFO_INTRINSIC_Illegal, ECall::InvalidDynamicFCallId) };
440
441 #include "ecalllist.h"
442
443
444 // Extern definitions so that ecall.cpp can see these tables
445 extern const ECClass c_rgECClasses[];
446 extern const int c_nECClasses;
447
448 const ECClass c_rgECClasses[] =
449 {
450 #define FCClassElement(name,namespace,funcs) {name, namespace, funcs},
451 #include "ecalllist.h"
452 };  // c_rgECClasses[]
453
454 const int c_nECClasses = NumItems(c_rgECClasses);
455
456 #endif // !CROSSGEN_COMPILE && CROSSGEN_MSCORLIB
457
458
459 #ifdef CROSSGEN_MSCORLIB
460 }; // namespace CrossGenMscorlib
461 #endif