wpcap: Handle different layout of the native packet header structure on 32-bit.
[wine.git] / dlls / msvcp90 / cxx.h
blob8037d7def568183d2153caa0503325a7ed0fcee3
1 /*
2 * Copyright 2012 Piotr Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "windef.h"
20 #include "winternl.h"
21 #include "rtlsupportapi.h"
22 #include "wine/asm.h"
24 #ifdef __i386__
25 #undef RTTI_USE_RVA
26 #else
27 #define RTTI_USE_RVA 1
28 #endif
30 #ifdef _WIN64
32 #define VTABLE_ADD_FUNC(name) "\t.quad " THISCALL_NAME(name) "\n"
34 #define __ASM_VTABLE(name,funcs) \
35 __asm__(".data\n" \
36 "\t.balign 8\n" \
37 "\t.quad " __ASM_NAME(#name "_rtti") "\n" \
38 "\t.globl " __ASM_NAME(#name "_vtable") "\n" \
39 __ASM_NAME(#name "_vtable") ":\n" \
40 funcs "\n\t.text")
42 #else
44 #define VTABLE_ADD_FUNC(name) "\t.long " THISCALL_NAME(name) "\n"
46 #define __ASM_VTABLE(name,funcs) \
47 __asm__(".data\n" \
48 "\t.balign 4\n" \
49 "\t.long " __ASM_NAME(#name "_rtti") "\n" \
50 "\t.globl " __ASM_NAME(#name "_vtable") "\n" \
51 __ASM_NAME(#name "_vtable") ":\n" \
52 funcs "\n\t.text")
54 #endif /* _WIN64 */
56 #ifndef RTTI_USE_RVA
58 #define DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
59 static type_info name ## _type_info = { \
60 &type_info_vtable, \
61 NULL, \
62 mangled_name \
63 }; \
65 static const rtti_base_descriptor name ## _rtti_base_descriptor = { \
66 &name ##_type_info, \
67 base_classes_no, \
68 { 0, -1, 0}, \
69 64 \
72 #define DEFINE_RTTI_DATA(name, off, base_classes_no, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, mangled_name) \
73 DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
75 static const rtti_base_array name ## _rtti_base_array = { \
76 { \
77 &name ## _rtti_base_descriptor, \
78 cl1, \
79 cl2, \
80 cl3, \
81 cl4, \
82 cl5, \
83 cl6, \
84 cl7, \
85 cl8, \
86 cl9, \
87 } \
88 }; \
90 static const rtti_object_hierarchy name ## _hierarchy = { \
91 0, \
92 0, \
93 base_classes_no+1, \
94 &name ## _rtti_base_array \
95 }; \
97 const rtti_object_locator name ## _rtti = { \
98 0, \
99 off, \
100 0, \
101 &name ## _type_info, \
102 &name ## _hierarchy \
105 #define DEFINE_CXX_TYPE_INFO(type) \
106 static const cxx_type_info type ## _cxx_type_info = { \
107 0, \
108 & type ##_type_info, \
109 { 0, -1, 0 }, \
110 sizeof(type), \
111 THISCALL(type ##_copy_ctor) \
114 #define DEFINE_CXX_DATA(type, base_no, cl1, cl2, cl3, cl4, dtor) \
115 DEFINE_CXX_TYPE_INFO(type) \
117 static const cxx_type_info_table type ## _cxx_type_table = { \
118 base_no+1, \
120 & type ## _cxx_type_info, \
121 cl1, \
122 cl2, \
123 cl3, \
124 cl4 \
126 }; \
128 static const cxx_exception_type type ## _exception_type = { \
129 0, \
130 THISCALL(dtor), \
131 NULL, \
132 & type ## _cxx_type_table \
135 #else
137 #define __DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
138 static type_info name ## _type_info = { \
139 &type_info_vtable, \
140 NULL, \
141 mangled_name \
142 }; \
144 static rtti_base_descriptor name ## _rtti_base_descriptor = { \
145 0xdeadbeef, \
146 base_classes_no, \
147 { 0, -1, 0}, \
148 64 \
151 #define DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
152 __DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
154 static void init_ ## name ## _rtti(char *base) \
156 name ## _rtti_base_descriptor.type_descriptor = (char*)&name ## _type_info - base; \
159 #define DEFINE_RTTI_DATA(name, off, base_classes_no, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, mangled_name) \
160 __DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
162 static rtti_base_array name ## _rtti_base_array = { \
164 0xdeadbeef, \
165 0xdeadbeef, \
166 0xdeadbeef, \
167 0xdeadbeef, \
168 0xdeadbeef, \
169 0xdeadbeef, \
170 0xdeadbeef, \
171 0xdeadbeef, \
172 0xdeadbeef, \
173 0xdeadbeef, \
175 }; \
177 static rtti_object_hierarchy name ## _hierarchy = { \
178 0, \
179 0, \
180 base_classes_no+1, \
181 0xdeadbeef \
182 }; \
184 rtti_object_locator name ## _rtti = { \
185 1, \
186 off, \
187 0, \
188 0xdeadbeef, \
189 0xdeadbeef, \
190 0xdeadbeef \
193 static void init_ ## name ## _rtti(char *base) \
195 name ## _rtti_base_descriptor.type_descriptor = (char*)&name ## _type_info - base; \
196 name ## _rtti_base_array.bases[0] = (char*)&name ## _rtti_base_descriptor - base; \
197 name ## _rtti_base_array.bases[1] = (char*)cl1 - base; \
198 name ## _rtti_base_array.bases[2] = (char*)cl2 - base; \
199 name ## _rtti_base_array.bases[3] = (char*)cl3 - base; \
200 name ## _rtti_base_array.bases[4] = (char*)cl4 - base; \
201 name ## _rtti_base_array.bases[5] = (char*)cl5 - base; \
202 name ## _rtti_base_array.bases[6] = (char*)cl6 - base; \
203 name ## _rtti_base_array.bases[7] = (char*)cl7 - base; \
204 name ## _rtti_base_array.bases[8] = (char*)cl8 - base; \
205 name ## _rtti_base_array.bases[9] = (char*)cl9 - base; \
206 name ## _hierarchy.base_classes = (char*)&name ## _rtti_base_array - base; \
207 name ## _rtti.type_descriptor = (char*)&name ## _type_info - base; \
208 name ## _rtti.type_hierarchy = (char*)&name ## _hierarchy - base; \
209 name ## _rtti.object_locator = (char*)&name ## _rtti - base; \
212 #define DEFINE_CXX_TYPE_INFO(type) \
213 static cxx_type_info type ## _cxx_type_info = { \
214 0, \
215 0xdeadbeef, \
216 { 0, -1, 0 }, \
217 sizeof(type), \
218 0xdeadbeef \
219 }; \
221 static void init_ ## type ## _cxx_type_info(char *base) \
223 type ## _cxx_type_info.type_info = (char *)&type ## _type_info - base; \
224 type ## _cxx_type_info.copy_ctor = (char *)type ## _copy_ctor - base; \
227 #define DEFINE_CXX_DATA(type, base_no, cl1, cl2, cl3, cl4, dtor) \
229 DEFINE_CXX_TYPE_INFO(type) \
231 static cxx_type_info_table type ## _cxx_type_table = { \
232 base_no+1, \
234 0xdeadbeef, \
235 0xdeadbeef, \
236 0xdeadbeef, \
237 0xdeadbeef, \
238 0xdeadbeef \
240 }; \
242 static cxx_exception_type type ##_exception_type = { \
243 0, \
244 0xdeadbeef, \
245 0, \
246 0xdeadbeef \
247 }; \
249 static void init_ ## type ## _cxx(char *base) \
251 init_ ## type ## _cxx_type_info(base); \
252 type ## _cxx_type_table.info[0] = (char *)&type ## _cxx_type_info - base; \
253 type ## _cxx_type_table.info[1] = (char *)cl1 - base; \
254 type ## _cxx_type_table.info[2] = (char *)cl2 - base; \
255 type ## _cxx_type_table.info[3] = (char *)cl3 - base; \
256 type ## _cxx_type_table.info[4] = (char *)cl4 - base; \
257 type ## _exception_type.destructor = (char *)dtor - base; \
258 type ## _exception_type.type_info_table = (char *)&type ## _cxx_type_table - base; \
261 #endif
263 #define DEFINE_RTTI_DATA0(name, off, mangled_name) \
264 DEFINE_RTTI_DATA(name, off, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, mangled_name)
265 #define DEFINE_RTTI_DATA1(name, off, cl1, mangled_name) \
266 DEFINE_RTTI_DATA(name, off, 1, cl1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, mangled_name)
267 #define DEFINE_RTTI_DATA2(name, off, cl1, cl2, mangled_name) \
268 DEFINE_RTTI_DATA(name, off, 2, cl1, cl2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, mangled_name)
269 #define DEFINE_RTTI_DATA3(name, off, cl1, cl2, cl3, mangled_name) \
270 DEFINE_RTTI_DATA(name, off, 3, cl1, cl2, cl3, NULL, NULL, NULL, NULL, NULL, NULL, mangled_name)
271 #define DEFINE_RTTI_DATA4(name, off, cl1, cl2, cl3, cl4, mangled_name) \
272 DEFINE_RTTI_DATA(name, off, 4, cl1, cl2, cl3, cl4, NULL, NULL, NULL, NULL, NULL, mangled_name)
273 #define DEFINE_RTTI_DATA5(name, off, cl1, cl2, cl3, cl4, cl5, mangled_name) \
274 DEFINE_RTTI_DATA(name, off, 5, cl1, cl2, cl3, cl4, cl5, NULL, NULL, NULL, NULL, mangled_name)
275 #define DEFINE_RTTI_DATA8(name, off, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, mangled_name) \
276 DEFINE_RTTI_DATA(name, off, 8, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, NULL, mangled_name)
277 #define DEFINE_RTTI_DATA9(name, off, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, mangled_name) \
278 DEFINE_RTTI_DATA(name, off, 9, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, mangled_name)
280 #define DEFINE_CXX_DATA0(name, dtor) \
281 DEFINE_CXX_DATA(name, 0, NULL, NULL, NULL, NULL, dtor)
282 #define DEFINE_CXX_DATA1(name, cl1, dtor) \
283 DEFINE_CXX_DATA(name, 1, cl1, NULL, NULL, NULL, dtor)
284 #define DEFINE_CXX_DATA2(name, cl1, cl2, dtor) \
285 DEFINE_CXX_DATA(name, 2, cl1, cl2, NULL, NULL, dtor)
286 #define DEFINE_CXX_DATA3(name, cl1, cl2, cl3, dtor) \
287 DEFINE_CXX_DATA(name, 3, cl1, cl2, cl3, NULL, dtor)
288 #define DEFINE_CXX_DATA4(name, cl1, cl2, cl3, cl4, dtor) \
289 DEFINE_CXX_DATA(name, 4, cl1, cl2, cl3, cl4, dtor)
291 #ifdef __ASM_USE_THISCALL_WRAPPER
293 #define CALL_VTBL_FUNC(this, off, ret, type, args) ((ret (WINAPI*)type)&vtbl_wrapper_##off)args
295 extern void *vtbl_wrapper_0;
296 extern void *vtbl_wrapper_4;
297 extern void *vtbl_wrapper_8;
298 extern void *vtbl_wrapper_12;
299 extern void *vtbl_wrapper_16;
300 extern void *vtbl_wrapper_20;
301 extern void *vtbl_wrapper_24;
302 extern void *vtbl_wrapper_28;
303 extern void *vtbl_wrapper_32;
304 extern void *vtbl_wrapper_36;
305 extern void *vtbl_wrapper_40;
306 extern void *vtbl_wrapper_44;
307 extern void *vtbl_wrapper_48;
308 extern void *vtbl_wrapper_52;
309 extern void *vtbl_wrapper_56;
311 #else
313 #define CALL_VTBL_FUNC(this, off, ret, type, args) ((ret (__thiscall***)type)this)[0][off/4]args
315 #endif
317 /* exception object */
318 typedef void (*vtable_ptr)(void);
319 typedef struct __exception
321 const vtable_ptr *vtable;
322 char *name; /* Name of this exception, always a new copy for each object */
323 int do_free; /* Whether to free 'name' in our dtor */
324 } exception;
326 /* rtti */
327 typedef struct __type_info
329 const vtable_ptr *vtable;
330 char *name; /* Unmangled name, allocated lazily */
331 char mangled[128]; /* Variable length, but we declare it large enough for static RTTI */
332 } type_info;
334 /* offsets for computing the this pointer */
335 typedef struct
337 int this_offset; /* offset of base class this pointer from start of object */
338 int vbase_descr; /* offset of virtual base class descriptor */
339 int vbase_offset; /* offset of this pointer offset in virtual base class descriptor */
340 } this_ptr_offsets;
342 #ifndef RTTI_USE_RVA
344 typedef struct _rtti_base_descriptor
346 const type_info *type_descriptor;
347 int num_base_classes;
348 this_ptr_offsets offsets; /* offsets for computing the this pointer */
349 unsigned int attributes;
350 } rtti_base_descriptor;
352 typedef struct _rtti_base_array
354 const rtti_base_descriptor *bases[10]; /* First element is the class itself */
355 } rtti_base_array;
357 typedef struct _rtti_object_hierarchy
359 unsigned int signature;
360 unsigned int attributes;
361 int array_len; /* Size of the array pointed to by 'base_classes' */
362 const rtti_base_array *base_classes;
363 } rtti_object_hierarchy;
365 typedef struct _rtti_object_locator
367 unsigned int signature;
368 int base_class_offset;
369 unsigned int flags;
370 const type_info *type_descriptor;
371 const rtti_object_hierarchy *type_hierarchy;
372 } rtti_object_locator;
374 typedef struct
376 UINT flags;
377 const type_info *type_info;
378 this_ptr_offsets offsets;
379 unsigned int size;
380 void *copy_ctor;
381 } cxx_type_info;
383 typedef struct
385 UINT count;
386 const cxx_type_info *info[5];
387 } cxx_type_info_table;
389 typedef struct
391 UINT flags;
392 void *destructor;
393 void *custom_handler;
394 const cxx_type_info_table *type_info_table;
395 } cxx_exception_type;
397 #else
399 typedef struct
401 unsigned int type_descriptor;
402 int num_base_classes;
403 this_ptr_offsets offsets; /* offsets for computing the this pointer */
404 unsigned int attributes;
405 } rtti_base_descriptor;
407 typedef struct
409 unsigned int bases[10]; /* First element is the class itself */
410 } rtti_base_array;
412 typedef struct
414 unsigned int signature;
415 unsigned int attributes;
416 int array_len; /* Size of the array pointed to by 'base_classes' */
417 unsigned int base_classes;
418 } rtti_object_hierarchy;
420 typedef struct
422 unsigned int signature;
423 int base_class_offset;
424 unsigned int flags;
425 unsigned int type_descriptor;
426 unsigned int type_hierarchy;
427 unsigned int object_locator;
428 } rtti_object_locator;
430 typedef struct
432 UINT flags;
433 unsigned int type_info;
434 this_ptr_offsets offsets;
435 unsigned int size;
436 unsigned int copy_ctor;
437 } cxx_type_info;
439 typedef struct
441 UINT count;
442 unsigned int info[5];
443 } cxx_type_info_table;
445 typedef struct
447 UINT flags;
448 unsigned int destructor;
449 unsigned int custom_handler;
450 unsigned int type_info_table;
451 } cxx_exception_type;
453 #endif
455 extern const vtable_ptr type_info_vtable;
457 #ifdef RTTI_USE_RVA
459 static inline uintptr_t rtti_rva_base( const void *ptr )
461 void *base;
462 return (uintptr_t)RtlPcToFileHeader( (void *)ptr, &base );
465 static inline void *rtti_rva( unsigned int rva, uintptr_t base )
467 return (void *)(base + rva);
470 #else
472 static inline uintptr_t rtti_rva_base( const void *ptr )
474 return 0;
477 static inline void *rtti_rva( const void *ptr, uintptr_t base )
479 return (void *)ptr;
482 #endif
484 #define CREATE_TYPE_INFO_VTABLE \
485 DEFINE_THISCALL_WRAPPER(type_info_vector_dtor,8) \
486 void * __thiscall type_info_vector_dtor(type_info * _this, unsigned int flags) \
488 if (flags & 2) \
490 /* we have an array, with the number of elements stored before the first object */ \
491 INT_PTR i, *ptr = (INT_PTR *)_this - 1; \
493 for (i = *ptr - 1; i >= 0; i--) free(_this[i].name); \
494 free(ptr); \
496 else \
498 free(_this->name); \
499 if (flags & 1) free(_this); \
501 return _this; \
504 DEFINE_RTTI_DATA0( type_info, 0, ".?AVtype_info@@" ) \
506 __ASM_BLOCK_BEGIN(type_info_vtables) \
507 __ASM_VTABLE(type_info, \
508 VTABLE_ADD_FUNC(type_info_vector_dtor)); \
509 __ASM_BLOCK_END