1977afe135b666b9f55cfe1b5287cbada6c65bc2
[reactos.git] / reactos / lib / sdk / crt / except / cpp.c
1 /*
2 * msvcrt.dll C++ objects
3 *
4 * Copyright 2000 Jon Griffiths
5 * Copyright 2003, 2004 Alexandre Julliard
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "wine/config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "winternl.h"
31 #include "wine/exception.h"
32 #include "winnt.h"
33 #include "excpt.h"
34 #include "wine/debug.h"
35 #include <malloc.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38
39 #include <internal/wine/msvcrt.h>
40 #include <internal/wine/cppexcept.h>
41 #include <internal/mtdll.h>
42
43 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
44
45 typedef exception bad_cast;
46 typedef exception bad_typeid;
47 typedef exception __non_rtti_object;
48
49 typedef struct _rtti_base_descriptor
50 {
51 const type_info *type_descriptor;
52 int num_base_classes;
53 this_ptr_offsets offsets; /* offsets for computing the this pointer */
54 unsigned int attributes;
55 } rtti_base_descriptor;
56
57 typedef struct _rtti_base_array
58 {
59 const rtti_base_descriptor *bases[3]; /* First element is the class itself */
60 } rtti_base_array;
61
62 typedef struct _rtti_object_hierarchy
63 {
64 unsigned int signature;
65 unsigned int attributes;
66 int array_len; /* Size of the array pointed to by 'base_classes' */
67 const rtti_base_array *base_classes;
68 } rtti_object_hierarchy;
69
70 typedef struct _rtti_object_locator
71 {
72 unsigned int signature;
73 int base_class_offset;
74 unsigned int flags;
75 const type_info *type_descriptor;
76 const rtti_object_hierarchy *type_hierarchy;
77 } rtti_object_locator;
78
79
80 #ifdef __i386__ /* thiscall functions are i386-specific */
81
82 #define THISCALL(func) __thiscall_ ## func
83 #define THISCALL_NAME(func) __ASM_NAME("__thiscall_" #func)
84 #define DEFINE_THISCALL_WRAPPER(func) \
85 extern void THISCALL(func)(); \
86 __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
87 "popl %eax\n\t" \
88 "pushl %ecx\n\t" \
89 "pushl %eax\n\t" \
90 "jmp " __ASM_NAME(#func) )
91 #else /* __i386__ */
92
93 #define THISCALL(func) func
94 #define THISCALL_NAME(func) __ASM_NAME(#func)
95 #define DEFINE_THISCALL_WRAPPER(func) /* nothing */
96
97 #endif /* __i386__ */
98
99 extern const vtable_ptr MSVCRT_exception_vtable;
100 extern const vtable_ptr MSVCRT_bad_typeid_vtable;
101 extern const vtable_ptr MSVCRT_bad_cast_vtable;
102 extern const vtable_ptr MSVCRT___non_rtti_object_vtable;
103 extern const vtable_ptr MSVCRT_type_info_vtable;
104
105 /* get the vtable pointer for a C++ object */
106 static inline const vtable_ptr *get_vtable( void *obj )
107 {
108 return *(const vtable_ptr **)obj;
109 }
110
111 static inline const rtti_object_locator *get_obj_locator( void *cppobj )
112 {
113 const vtable_ptr *vtable = get_vtable( cppobj );
114 return (const rtti_object_locator *)vtable[-1];
115 }
116
117 static void dump_obj_locator( const rtti_object_locator *ptr )
118 {
119 int i;
120 const rtti_object_hierarchy *h = ptr->type_hierarchy;
121
122 TRACE( "%p: sig=%08x base_offset=%08x flags=%08x type=%p %s hierarchy=%p\n",
123 ptr, ptr->signature, ptr->base_class_offset, ptr->flags,
124 ptr->type_descriptor, dbgstr_type_info(ptr->type_descriptor), ptr->type_hierarchy );
125 TRACE( " hierarchy: sig=%08x attr=%08x len=%d base classes=%p\n",
126 h->signature, h->attributes, h->array_len, h->base_classes );
127 for (i = 0; i < h->array_len; i++)
128 {
129 TRACE( " base class %p: num %d off %d,%d,%d attr %08x type %p %s\n",
130 h->base_classes->bases[i],
131 h->base_classes->bases[i]->num_base_classes,
132 h->base_classes->bases[i]->offsets.this_offset,
133 h->base_classes->bases[i]->offsets.vbase_descr,
134 h->base_classes->bases[i]->offsets.vbase_offset,
135 h->base_classes->bases[i]->attributes,
136 h->base_classes->bases[i]->type_descriptor,
137 dbgstr_type_info(h->base_classes->bases[i]->type_descriptor) );
138 }
139 }
140
141 /* Internal common ctor for exception */
142 static void WINAPI EXCEPTION_ctor(exception *_this, const char** name)
143 {
144 _this->vtable = &MSVCRT_exception_vtable;
145 if (*name)
146 {
147 size_t name_len = strlen(*name) + 1;
148 _this->name = MSVCRT_malloc(name_len);
149 memcpy(_this->name, *name, name_len);
150 _this->do_free = TRUE;
151 }
152 else
153 {
154 _this->name = NULL;
155 _this->do_free = FALSE;
156 }
157 }
158
159 /******************************************************************
160 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
161 */
162 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor)
163 exception * __stdcall MSVCRT_exception_ctor(exception * _this, const char ** name)
164 {
165 TRACE("(%p,%s)\n", _this, *name);
166 EXCEPTION_ctor(_this, name);
167 return _this;
168 }
169
170 /******************************************************************
171 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
172 */
173 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor)
174 exception * __stdcall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
175 {
176 TRACE("(%p,%p)\n", _this, rhs);
177
178 if (!rhs->do_free)
179 {
180 _this->vtable = &MSVCRT_exception_vtable;
181 _this->name = rhs->name;
182 _this->do_free = FALSE;
183 }
184 else
185 EXCEPTION_ctor(_this, (const char**)&rhs->name);
186 TRACE("name = %s\n", _this->name);
187 return _this;
188 }
189
190 /******************************************************************
191 * ??0exception@@QAE@XZ (MSVCRT.@)
192 */
193 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor)
194 exception * __stdcall MSVCRT_exception_default_ctor(exception * _this)
195 {
196 static const char* empty = NULL;
197
198 TRACE("(%p)\n", _this);
199 EXCEPTION_ctor(_this, &empty);
200 return _this;
201 }
202
203 /******************************************************************
204 * ??1exception@@UAE@XZ (MSVCRT.@)
205 */
206 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor)
207 void __stdcall MSVCRT_exception_dtor(exception * _this)
208 {
209 TRACE("(%p)\n", _this);
210 _this->vtable = &MSVCRT_exception_vtable;
211 if (_this->do_free) MSVCRT_free(_this->name);
212 }
213
214 /******************************************************************
215 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
216 */
217 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals)
218 exception * __stdcall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
219 {
220 TRACE("(%p %p)\n", _this, rhs);
221 if (_this != rhs)
222 {
223 MSVCRT_exception_dtor(_this);
224 MSVCRT_exception_copy_ctor(_this, rhs);
225 }
226 TRACE("name = %s\n", _this->name);
227 return _this;
228 }
229
230 /******************************************************************
231 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
232 */
233 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor)
234 void * __stdcall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
235 {
236 TRACE("(%p %x)\n", _this, flags);
237 if (flags & 2)
238 {
239 /* we have an array, with the number of elements stored before the first object */
240 int i, *ptr = (int *)_this - 1;
241
242 for (i = *ptr - 1; i >= 0; i--) MSVCRT_exception_dtor(_this + i);
243 MSVCRT_operator_delete(ptr);
244 }
245 else
246 {
247 MSVCRT_exception_dtor(_this);
248 if (flags & 1) MSVCRT_operator_delete(_this);
249 }
250 return _this;
251 }
252
253 /******************************************************************
254 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
255 */
256 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor)
257 void * __stdcall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
258 {
259 TRACE("(%p %x)\n", _this, flags);
260 MSVCRT_exception_dtor(_this);
261 if (flags & 1) MSVCRT_operator_delete(_this);
262 return _this;
263 }
264
265 /******************************************************************
266 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
267 */
268 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception)
269 const char * __stdcall MSVCRT_what_exception(exception * _this)
270 {
271 TRACE("(%p) returning %s\n", _this, _this->name);
272 return _this->name ? _this->name : "Unknown exception";
273 }
274
275 /******************************************************************
276 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
277 */
278 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor)
279 bad_typeid * __stdcall MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
280 {
281 TRACE("(%p %p)\n", _this, rhs);
282 MSVCRT_exception_copy_ctor(_this, rhs);
283 _this->vtable = &MSVCRT_bad_typeid_vtable;
284 return _this;
285 }
286
287 /******************************************************************
288 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
289 */
290 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor)
291 bad_typeid * __stdcall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
292 {
293 TRACE("(%p %s)\n", _this, name);
294 EXCEPTION_ctor(_this, &name);
295 _this->vtable = &MSVCRT_bad_typeid_vtable;
296 return _this;
297 }
298
299 /******************************************************************
300 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
301 */
302 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor)
303 void __stdcall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
304 {
305 TRACE("(%p)\n", _this);
306 MSVCRT_exception_dtor(_this);
307 }
308
309 /******************************************************************
310 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
311 */
312 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals)
313 bad_typeid * __stdcall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
314 {
315 TRACE("(%p %p)\n", _this, rhs);
316 MSVCRT_exception_opequals(_this, rhs);
317 return _this;
318 }
319
320 /******************************************************************
321 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
322 */
323 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor)
324 void * __stdcall MSVCRT_bad_typeid_vector_dtor(bad_typeid * _this, unsigned int flags)
325 {
326 TRACE("(%p %x)\n", _this, flags);
327 if (flags & 2)
328 {
329 /* we have an array, with the number of elements stored before the first object */
330 int i, *ptr = (int *)_this - 1;
331
332 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_typeid_dtor(_this + i);
333 MSVCRT_operator_delete(ptr);
334 }
335 else
336 {
337 MSVCRT_bad_typeid_dtor(_this);
338 if (flags & 1) MSVCRT_operator_delete(_this);
339 }
340 return _this;
341 }
342
343 /******************************************************************
344 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
345 */
346 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor)
347 void * __stdcall MSVCRT_bad_typeid_scalar_dtor(bad_typeid * _this, unsigned int flags)
348 {
349 TRACE("(%p %x)\n", _this, flags);
350 MSVCRT_bad_typeid_dtor(_this);
351 if (flags & 1) MSVCRT_operator_delete(_this);
352 return _this;
353 }
354
355 /******************************************************************
356 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
357 */
358 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor)
359 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
360 const __non_rtti_object * rhs)
361 {
362 TRACE("(%p %p)\n", _this, rhs);
363 MSVCRT_bad_typeid_copy_ctor(_this, rhs);
364 _this->vtable = &MSVCRT___non_rtti_object_vtable;
365 return _this;
366 }
367
368 /******************************************************************
369 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
370 */
371 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor)
372 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
373 const char * name)
374 {
375 TRACE("(%p %s)\n", _this, name);
376 EXCEPTION_ctor(_this, &name);
377 _this->vtable = &MSVCRT___non_rtti_object_vtable;
378 return _this;
379 }
380
381 /******************************************************************
382 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
383 */
384 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor)
385 void __stdcall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
386 {
387 TRACE("(%p)\n", _this);
388 MSVCRT_bad_typeid_dtor(_this);
389 }
390
391 /******************************************************************
392 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
393 */
394 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals)
395 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
396 const __non_rtti_object *rhs)
397 {
398 TRACE("(%p %p)\n", _this, rhs);
399 MSVCRT_bad_typeid_opequals(_this, rhs);
400 return _this;
401 }
402
403 /******************************************************************
404 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
405 */
406 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor)
407 void * __stdcall MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object * _this, unsigned int flags)
408 {
409 TRACE("(%p %x)\n", _this, flags);
410 if (flags & 2)
411 {
412 /* we have an array, with the number of elements stored before the first object */
413 int i, *ptr = (int *)_this - 1;
414
415 for (i = *ptr - 1; i >= 0; i--) MSVCRT___non_rtti_object_dtor(_this + i);
416 MSVCRT_operator_delete(ptr);
417 }
418 else
419 {
420 MSVCRT___non_rtti_object_dtor(_this);
421 if (flags & 1) MSVCRT_operator_delete(_this);
422 }
423 return _this;
424 }
425
426 /******************************************************************
427 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
428 */
429 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor)
430 void * __stdcall MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object * _this, unsigned int flags)
431 {
432 TRACE("(%p %x)\n", _this, flags);
433 MSVCRT___non_rtti_object_dtor(_this);
434 if (flags & 1) MSVCRT_operator_delete(_this);
435 return _this;
436 }
437
438 /******************************************************************
439 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
440 */
441 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor)
442 bad_cast * __stdcall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
443 {
444 TRACE("(%p %s)\n", _this, *name);
445 EXCEPTION_ctor(_this, name);
446 _this->vtable = &MSVCRT_bad_cast_vtable;
447 return _this;
448 }
449
450 /******************************************************************
451 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
452 */
453 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor)
454 bad_cast * __stdcall MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
455 {
456 TRACE("(%p %p)\n", _this, rhs);
457 MSVCRT_exception_copy_ctor(_this, rhs);
458 _this->vtable = &MSVCRT_bad_cast_vtable;
459 return _this;
460 }
461
462 /******************************************************************
463 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
464 */
465 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor)
466 void __stdcall MSVCRT_bad_cast_dtor(bad_cast * _this)
467 {
468 TRACE("(%p)\n", _this);
469 MSVCRT_exception_dtor(_this);
470 }
471
472 /******************************************************************
473 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
474 */
475 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals)
476 bad_cast * __stdcall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
477 {
478 TRACE("(%p %p)\n", _this, rhs);
479 MSVCRT_exception_opequals(_this, rhs);
480 return _this;
481 }
482
483 /******************************************************************
484 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
485 */
486 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor)
487 void * __stdcall MSVCRT_bad_cast_vector_dtor(bad_cast * _this, unsigned int flags)
488 {
489 TRACE("(%p %x)\n", _this, flags);
490 if (flags & 2)
491 {
492 /* we have an array, with the number of elements stored before the first object */
493 int i, *ptr = (int *)_this - 1;
494
495 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_cast_dtor(_this + i);
496 MSVCRT_operator_delete(ptr);
497 }
498 else
499 {
500 MSVCRT_bad_cast_dtor(_this);
501 if (flags & 1) MSVCRT_operator_delete(_this);
502 }
503 return _this;
504 }
505
506 /******************************************************************
507 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
508 */
509 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor)
510 void * __stdcall MSVCRT_bad_cast_scalar_dtor(bad_cast * _this, unsigned int flags)
511 {
512 TRACE("(%p %x)\n", _this, flags);
513 MSVCRT_bad_cast_dtor(_this);
514 if (flags & 1) MSVCRT_operator_delete(_this);
515 return _this;
516 }
517
518 /******************************************************************
519 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
520 */
521 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals)
522 int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
523 {
524 int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
525 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
526 return ret;
527 }
528
529 /******************************************************************
530 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
531 */
532 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals)
533 int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
534 {
535 int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
536 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
537 return ret;
538 }
539
540 /******************************************************************
541 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
542 */
543 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before)
544 int __stdcall MSVCRT_type_info_before(type_info * _this, const type_info * rhs)
545 {
546 int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
547 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
548 return ret;
549 }
550
551 /******************************************************************
552 * ??1type_info@@UAE@XZ (MSVCRT.@)
553 */
554 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor)
555 void __stdcall MSVCRT_type_info_dtor(type_info * _this)
556 {
557 TRACE("(%p)\n", _this);
558 MSVCRT_free(_this->name);
559 }
560
561 /******************************************************************
562 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
563 */
564 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name)
565 const char * __stdcall MSVCRT_type_info_name(type_info * _this)
566 {
567 if (!_this->name)
568 {
569 /* Create and set the demangled name */
570 /* Nota: mangled name in type_info struct always start with a '.', while
571 * it isn't valid for mangled name.
572 * Is this '.' really part of the mangled name, or has it some other meaning ?
573 */
574 char* name = __unDName(0, _this->mangled + 1, 0,
575 MSVCRT_malloc, MSVCRT_free, 0x2800);
576
577 if (name)
578 {
579 unsigned int len = strlen(name);
580
581 /* It seems _unDName may leave blanks at the end of the demangled name */
582 while (len && name[--len] == ' ')
583 name[len] = '\0';
584
585 _mlock(_EXIT_LOCK2);
586
587 if (_this->name)
588 {
589 /* Another thread set this member since we checked above - use it */
590 MSVCRT_free(name);
591 }
592 else
593 _this->name = name;
594
595 _munlock(_EXIT_LOCK2);
596 }
597 }
598 TRACE("(%p) returning %s\n", _this, _this->name);
599 return _this->name;
600 }
601
602 /******************************************************************
603 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
604 */
605 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name)
606 const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
607 {
608 TRACE("(%p) returning %s\n", _this, _this->mangled);
609 return _this->mangled;
610 }
611
612 /* Unexported */
613 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor)
614 void * __stdcall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
615 {
616 TRACE("(%p %x)\n", _this, flags);
617 if (flags & 2)
618 {
619 /* we have an array, with the number of elements stored before the first object */
620 int i, *ptr = (int *)_this - 1;
621
622 for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
623 MSVCRT_operator_delete(ptr);
624 }
625 else
626 {
627 MSVCRT_type_info_dtor(_this);
628 if (flags & 1) MSVCRT_operator_delete(_this);
629 }
630 return _this;
631 }
632
633 /* vtables */
634 #ifdef _WIN64
635
636 #define __ASM_VTABLE(name,funcs) \
637 __asm__(".data\n" \
638 "\t.align 8\n" \
639 "\t.quad " __ASM_NAME(#name "_rtti") "\n" \
640 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
641 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
642 "\t.quad " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
643 funcs "\n\t.text");
644
645 #define __ASM_EXCEPTION_VTABLE(name) \
646 __ASM_VTABLE(name, "\t.quad " THISCALL_NAME(MSVCRT_what_exception) )
647
648 #else
649
650 #define __ASM_VTABLE(name,funcs) \
651 __asm__(".data\n" \
652 "\t.align 4\n" \
653 "\t.long " __ASM_NAME(#name "_rtti") "\n" \
654 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
655 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
656 "\t.long " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
657 funcs "\n\t.text");
658
659 #define __ASM_EXCEPTION_VTABLE(name) \
660 __ASM_VTABLE(name, "\t.long " THISCALL_NAME(MSVCRT_what_exception) )
661
662 #endif /* _WIN64 */
663
664 #ifndef __GNUC__
665 void __asm_dummy_vtables(void) {
666 #endif
667
668 __ASM_VTABLE(type_info,"")
669 __ASM_EXCEPTION_VTABLE(exception)
670 __ASM_EXCEPTION_VTABLE(bad_typeid)
671 __ASM_EXCEPTION_VTABLE(bad_cast)
672 __ASM_EXCEPTION_VTABLE(__non_rtti_object)
673
674 #ifndef __GNUC__
675 }
676 #endif
677
678 /* Static RTTI for exported objects */
679
680 static const type_info exception_type_info =
681 {
682 &MSVCRT_type_info_vtable,
683 NULL,
684 ".?AVexception@@"
685 };
686
687 static const rtti_base_descriptor exception_rtti_base_descriptor =
688 {
689 &exception_type_info,
690 0,
691 { 0, -1, 0 },
692 0
693 };
694
695 static const rtti_base_array exception_rtti_base_array =
696 {
697 {
698 &exception_rtti_base_descriptor,
699 NULL,
700 NULL
701 }
702 };
703
704 static const rtti_object_hierarchy exception_type_hierarchy =
705 {
706 0,
707 0,
708 1,
709 &exception_rtti_base_array
710 };
711
712 const rtti_object_locator exception_rtti =
713 {
714 0,
715 0,
716 0,
717 &exception_type_info,
718 &exception_type_hierarchy
719 };
720
721 static const cxx_type_info exception_cxx_type_info =
722 {
723 0,
724 &exception_type_info,
725 { 0, -1, 0 },
726 sizeof(exception),
727 (cxx_copy_ctor)THISCALL(MSVCRT_exception_copy_ctor)
728 };
729
730 static const type_info bad_typeid_type_info =
731 {
732 &MSVCRT_type_info_vtable,
733 NULL,
734 ".?AVbad_typeid@@"
735 };
736
737 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
738 {
739 &bad_typeid_type_info,
740 1,
741 { 0, -1, 0 },
742 0
743 };
744
745 static const rtti_base_array bad_typeid_rtti_base_array =
746 {
747 {
748 &bad_typeid_rtti_base_descriptor,
749 &exception_rtti_base_descriptor,
750 NULL
751 }
752 };
753
754 static const rtti_object_hierarchy bad_typeid_type_hierarchy =
755 {
756 0,
757 0,
758 2,
759 &bad_typeid_rtti_base_array
760 };
761
762 const rtti_object_locator bad_typeid_rtti =
763 {
764 0,
765 0,
766 0,
767 &bad_typeid_type_info,
768 &bad_typeid_type_hierarchy
769 };
770
771 static const cxx_type_info bad_typeid_cxx_type_info =
772 {
773 0,
774 &bad_typeid_type_info,
775 { 0, -1, 0 },
776 sizeof(exception),
777 (cxx_copy_ctor)THISCALL(MSVCRT_bad_typeid_copy_ctor)
778 };
779
780 static const type_info bad_cast_type_info =
781 {
782 &MSVCRT_type_info_vtable,
783 NULL,
784 ".?AVbad_cast@@"
785 };
786
787 static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
788 {
789 &bad_cast_type_info,
790 1,
791 { 0, -1, 0 },
792 0
793 };
794
795 static const rtti_base_array bad_cast_rtti_base_array =
796 {
797 {
798 &bad_cast_rtti_base_descriptor,
799 &exception_rtti_base_descriptor,
800 NULL
801 }
802 };
803
804 static const rtti_object_hierarchy bad_cast_type_hierarchy =
805 {
806 0,
807 0,
808 2,
809 &bad_cast_rtti_base_array
810 };
811
812 const rtti_object_locator bad_cast_rtti =
813 {
814 0,
815 0,
816 0,
817 &bad_cast_type_info,
818 &bad_cast_type_hierarchy
819 };
820
821 static const cxx_type_info bad_cast_cxx_type_info =
822 {
823 0,
824 &bad_cast_type_info,
825 { 0, -1, 0 },
826 sizeof(exception),
827 (cxx_copy_ctor)THISCALL(MSVCRT_bad_cast_copy_ctor)
828 };
829
830 static const type_info __non_rtti_object_type_info =
831 {
832 &MSVCRT_type_info_vtable,
833 NULL,
834 ".?AV__non_rtti_object@@"
835 };
836
837 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
838 {
839 &__non_rtti_object_type_info,
840 2,
841 { 0, -1, 0 },
842 0
843 };
844
845 static const rtti_base_array __non_rtti_object_rtti_base_array =
846 {
847 {
848 &__non_rtti_object_rtti_base_descriptor,
849 &bad_typeid_rtti_base_descriptor,
850 &exception_rtti_base_descriptor
851 }
852 };
853
854 static const rtti_object_hierarchy __non_rtti_object_type_hierarchy =
855 {
856 0,
857 0,
858 3,
859 &__non_rtti_object_rtti_base_array
860 };
861
862 const rtti_object_locator __non_rtti_object_rtti =
863 {
864 0,
865 0,
866 0,
867 &__non_rtti_object_type_info,
868 &__non_rtti_object_type_hierarchy
869 };
870
871 static const cxx_type_info __non_rtti_object_cxx_type_info =
872 {
873 0,
874 &__non_rtti_object_type_info,
875 { 0, -1, 0 },
876 sizeof(exception),
877 (cxx_copy_ctor)THISCALL(MSVCRT___non_rtti_object_copy_ctor)
878 };
879
880 static const type_info type_info_type_info =
881 {
882 &MSVCRT_type_info_vtable,
883 NULL,
884 ".?AVtype_info@@"
885 };
886
887 static const rtti_base_descriptor type_info_rtti_base_descriptor =
888 {
889 &type_info_type_info,
890 0,
891 { 0, -1, 0 },
892 0
893 };
894
895 static const rtti_base_array type_info_rtti_base_array =
896 {
897 {
898 &type_info_rtti_base_descriptor,
899 NULL,
900 NULL
901 }
902 };
903
904 static const rtti_object_hierarchy type_info_type_hierarchy =
905 {
906 0,
907 0,
908 1,
909 &type_info_rtti_base_array
910 };
911
912 const rtti_object_locator type_info_rtti =
913 {
914 0,
915 0,
916 0,
917 &type_info_type_info,
918 &type_info_type_hierarchy
919 };
920
921 /*
922 * Exception RTTI for cpp objects
923 */
924 static const cxx_type_info_table bad_cast_type_info_table =
925 {
926 3,
927 {
928 &__non_rtti_object_cxx_type_info,
929 &bad_typeid_cxx_type_info,
930 &exception_cxx_type_info
931 }
932 };
933
934 static const cxx_exception_type bad_cast_exception_type =
935 {
936 0,
937 (void*)THISCALL(MSVCRT_bad_cast_dtor),
938 NULL,
939 &bad_cast_type_info_table
940 };
941
942 static const cxx_type_info_table bad_typeid_type_info_table =
943 {
944 2,
945 {
946 &bad_cast_cxx_type_info,
947 &exception_cxx_type_info,
948 NULL
949 }
950 };
951
952 static const cxx_exception_type bad_typeid_exception_type =
953 {
954 0,
955 (void*)THISCALL(MSVCRT_bad_typeid_dtor),
956 NULL,
957 &bad_cast_type_info_table
958 };
959
960 static const cxx_exception_type __non_rtti_object_exception_type =
961 {
962 0,
963 (void*)THISCALL(MSVCRT___non_rtti_object_dtor),
964 NULL,
965 &bad_typeid_type_info_table
966 };
967
968
969 /******************************************************************
970 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
971 *
972 * Install a handler to be called when terminate() is called.
973 *
974 * PARAMS
975 * func [I] Handler function to install
976 *
977 * RETURNS
978 * The previously installed handler function, if any.
979 */
980 terminate_function CDECL MSVCRT_set_terminate(terminate_function func)
981 {
982 MSVCRT_thread_data *data = msvcrt_get_thread_data();
983 terminate_function previous = data->terminate_handler;
984 TRACE("(%p) returning %p\n",func,previous);
985 data->terminate_handler = func;
986 return previous;
987 }
988
989 /******************************************************************
990 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
991 *
992 * Install a handler to be called when unexpected() is called.
993 *
994 * PARAMS
995 * func [I] Handler function to install
996 *
997 * RETURNS
998 * The previously installed handler function, if any.
999 */
1000 unexpected_function CDECL MSVCRT_set_unexpected(unexpected_function func)
1001 {
1002 MSVCRT_thread_data *data = msvcrt_get_thread_data();
1003 unexpected_function previous = data->unexpected_handler;
1004 TRACE("(%p) returning %p\n",func,previous);
1005 data->unexpected_handler = func;
1006 return previous;
1007 }
1008
1009 /******************************************************************
1010 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
1011 */
1012 _se_translator_function CDECL MSVCRT__set_se_translator(_se_translator_function func)
1013 {
1014 MSVCRT_thread_data *data = msvcrt_get_thread_data();
1015 _se_translator_function previous = data->se_translator;
1016 TRACE("(%p) returning %p\n",func,previous);
1017 data->se_translator = func;
1018 return previous;
1019 }
1020
1021 /******************************************************************
1022 * ?terminate@@YAXXZ (MSVCRT.@)
1023 *
1024 * Default handler for an unhandled exception.
1025 *
1026 * PARAMS
1027 * None.
1028 *
1029 * RETURNS
1030 * This function does not return. Either control resumes from any
1031 * handler installed by calling set_terminate(), or (by default) abort()
1032 * is called.
1033 */
1034 void CDECL MSVCRT_terminate(void)
1035 {
1036 MSVCRT_thread_data *data = msvcrt_get_thread_data();
1037 if (data->terminate_handler) data->terminate_handler();
1038 abort();
1039 }
1040
1041 /******************************************************************
1042 * ?unexpected@@YAXXZ (MSVCRT.@)
1043 */
1044 void CDECL MSVCRT_unexpected(void)
1045 {
1046 MSVCRT_thread_data *data = msvcrt_get_thread_data();
1047 if (data->unexpected_handler) data->unexpected_handler();
1048 MSVCRT_terminate();
1049 }
1050
1051
1052 /******************************************************************
1053 * __RTtypeid (MSVCRT.@)
1054 *
1055 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1056 *
1057 * PARAMS
1058 * cppobj [I] C++ object to get type information for.
1059 *
1060 * RETURNS
1061 * Success: A type_info object describing cppobj.
1062 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
1063 * exception is thrown. If cppobj is NULL, a bad_typeid exception
1064 * is thrown. In either case, this function does not return.
1065 *
1066 * NOTES
1067 * This function is usually called by compiler generated code as a result
1068 * of using one of the C++ dynamic cast statements.
1069 */
1070 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
1071 {
1072 const type_info *ret;
1073
1074 if (!cppobj)
1075 {
1076 bad_typeid e;
1077 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
1078 _CxxThrowException( &e, &bad_typeid_exception_type );
1079 return NULL;
1080 }
1081
1082 __TRY
1083 {
1084 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1085 ret = obj_locator->type_descriptor;
1086 }
1087 __EXCEPT_PAGE_FAULT
1088 {
1089 __non_rtti_object e;
1090 MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
1091 _CxxThrowException( &e, &bad_typeid_exception_type );
1092 return NULL;
1093 }
1094 __ENDTRY
1095 return ret;
1096 }
1097
1098 /******************************************************************
1099 * __RTDynamicCast (MSVCRT.@)
1100 *
1101 * Dynamically cast a C++ object to one of its base classes.
1102 *
1103 * PARAMS
1104 * cppobj [I] Any C++ object to cast
1105 * unknown [I] Reserved, set to 0
1106 * src [I] type_info object describing cppobj
1107 * dst [I] type_info object describing the base class to cast to
1108 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
1109 *
1110 * RETURNS
1111 * Success: The address of cppobj, cast to the object described by dst.
1112 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
1113 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
1114 * is thrown and this function does not return.
1115 *
1116 * NOTES
1117 * This function is usually called by compiler generated code as a result
1118 * of using one of the C++ dynamic cast statements.
1119 */
1120 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
1121 type_info *src, type_info *dst,
1122 int do_throw)
1123 {
1124 void *ret;
1125
1126 if (!cppobj) return NULL;
1127
1128 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
1129 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
1130
1131 /* To cast an object at runtime:
1132 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
1133 * 2.Search for the destination type in the class hierarchy
1134 * 3.If destination type is found, return base object address + dest offset
1135 * Otherwise, fail the cast
1136 *
1137 * FIXME: the unknown parameter doesn't seem to be used for anything
1138 */
1139 __TRY
1140 {
1141 int i;
1142 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1143 const rtti_object_hierarchy *obj_bases = obj_locator->type_hierarchy;
1144 const rtti_base_descriptor * const* base_desc = obj_bases->base_classes->bases;
1145
1146 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
1147
1148 ret = NULL;
1149 for (i = 0; i < obj_bases->array_len; i++)
1150 {
1151 const type_info *typ = base_desc[i]->type_descriptor;
1152
1153 if (!strcmp(typ->mangled, dst->mangled))
1154 {
1155 /* compute the correct this pointer for that base class */
1156 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
1157 ret = get_this_pointer( &base_desc[i]->offsets, this_ptr );
1158 break;
1159 }
1160 }
1161 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
1162 * to a reference, since references cannot be NULL.
1163 */
1164 if (!ret && do_throw)
1165 {
1166 const char *msg = "Bad dynamic_cast!";
1167 bad_cast e;
1168 MSVCRT_bad_cast_ctor( &e, &msg );
1169 _CxxThrowException( &e, &bad_cast_exception_type );
1170 }
1171 }
1172 __EXCEPT_PAGE_FAULT
1173 {
1174 __non_rtti_object e;
1175 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1176 _CxxThrowException( &e, &bad_typeid_exception_type );
1177 return NULL;
1178 }
1179 __ENDTRY
1180 return ret;
1181 }
1182
1183
1184 /******************************************************************
1185 * __RTCastToVoid (MSVCRT.@)
1186 *
1187 * Dynamically cast a C++ object to a void*.
1188 *
1189 * PARAMS
1190 * cppobj [I] The C++ object to cast
1191 *
1192 * RETURNS
1193 * Success: The base address of the object as a void*.
1194 * Failure: NULL, if cppobj is NULL or has no RTTI.
1195 *
1196 * NOTES
1197 * This function is usually called by compiler generated code as a result
1198 * of using one of the C++ dynamic cast statements.
1199 */
1200 void* CDECL MSVCRT___RTCastToVoid(void *cppobj)
1201 {
1202 void *ret;
1203
1204 if (!cppobj) return NULL;
1205
1206 __TRY
1207 {
1208 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1209 ret = (char *)cppobj - obj_locator->base_class_offset;
1210 }
1211 __EXCEPT_PAGE_FAULT
1212 {
1213 __non_rtti_object e;
1214 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1215 _CxxThrowException( &e, &bad_typeid_exception_type );
1216 return NULL;
1217 }
1218 __ENDTRY
1219 return ret;
1220 }