2 * msvcrt.dll C++ objects
4 * Copyright 2000 Jon Griffiths
5 * Copyright 2003, 2004 Alexandre Julliard
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.
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.
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
23 #include "wine/port.h"
29 #include "wine/exception.h"
30 #include "wine/debug.h"
32 #include "cppexcept.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
37 typedef exception bad_cast;
38 typedef exception bad_typeid;
39 typedef exception __non_rtti_object;
41 typedef struct _rtti_base_descriptor
43 const type_info *type_descriptor;
45 this_ptr_offsets offsets; /* offsets for computing the this pointer */
46 unsigned int attributes;
47 } rtti_base_descriptor;
49 typedef struct _rtti_base_array
51 const rtti_base_descriptor *bases[3]; /* First element is the class itself */
54 typedef struct _rtti_object_hierarchy
56 unsigned int signature;
57 unsigned int attributes;
58 int array_len; /* Size of the array pointed to by 'base_classes' */
59 const rtti_base_array *base_classes;
60 } rtti_object_hierarchy;
62 typedef struct _rtti_object_locator
64 unsigned int signature;
65 int base_class_offset;
67 const type_info *type_descriptor;
68 const rtti_object_hierarchy *type_hierarchy;
69 } rtti_object_locator;
72 #ifdef __i386__ /* thiscall functions are i386-specific */
74 #define THISCALL(func) __thiscall_ ## func
75 #define THISCALL_NAME(func) __ASM_NAME("__thiscall_" #func)
76 #define DEFINE_THISCALL_WRAPPER(func,args) \
77 extern void THISCALL(func)(void); \
78 __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
82 "jmp " __ASM_NAME(#func) __ASM_STDCALL(args) )
85 #define THISCALL(func) func
86 #define THISCALL_NAME(func) __ASM_NAME(#func)
87 #define DEFINE_THISCALL_WRAPPER(func,args) /* nothing */
91 extern const vtable_ptr MSVCRT_exception_vtable;
92 extern const vtable_ptr MSVCRT_bad_typeid_vtable;
93 extern const vtable_ptr MSVCRT_bad_cast_vtable;
94 extern const vtable_ptr MSVCRT___non_rtti_object_vtable;
95 extern const vtable_ptr MSVCRT_type_info_vtable;
97 /* get the vtable pointer for a C++ object */
98 static inline const vtable_ptr *get_vtable( void *obj )
100 return *(const vtable_ptr **)obj;
103 static inline const rtti_object_locator *get_obj_locator( void *cppobj )
105 const vtable_ptr *vtable = get_vtable( cppobj );
106 return (const rtti_object_locator *)vtable[-1];
109 static void dump_obj_locator( const rtti_object_locator *ptr )
112 const rtti_object_hierarchy *h = ptr->type_hierarchy;
114 TRACE( "%p: sig=%08x base_offset=%08x flags=%08x type=%p %s hierarchy=%p\n",
115 ptr, ptr->signature, ptr->base_class_offset, ptr->flags,
116 ptr->type_descriptor, dbgstr_type_info(ptr->type_descriptor), ptr->type_hierarchy );
117 TRACE( " hierarchy: sig=%08x attr=%08x len=%d base classes=%p\n",
118 h->signature, h->attributes, h->array_len, h->base_classes );
119 for (i = 0; i < h->array_len; i++)
121 TRACE( " base class %p: num %d off %d,%d,%d attr %08x type %p %s\n",
122 h->base_classes->bases[i],
123 h->base_classes->bases[i]->num_base_classes,
124 h->base_classes->bases[i]->offsets.this_offset,
125 h->base_classes->bases[i]->offsets.vbase_descr,
126 h->base_classes->bases[i]->offsets.vbase_offset,
127 h->base_classes->bases[i]->attributes,
128 h->base_classes->bases[i]->type_descriptor,
129 dbgstr_type_info(h->base_classes->bases[i]->type_descriptor) );
133 /* Internal common ctor for exception */
134 static void EXCEPTION_ctor(exception *_this, const char** name)
136 _this->vtable = &MSVCRT_exception_vtable;
139 unsigned int name_len = strlen(*name) + 1;
140 _this->name = MSVCRT_malloc(name_len);
141 memcpy(_this->name, *name, name_len);
142 _this->do_free = TRUE;
147 _this->do_free = FALSE;
151 /******************************************************************
152 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
154 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor,8)
155 exception * __stdcall MSVCRT_exception_ctor(exception * _this, const char ** name)
157 TRACE("(%p,%s)\n", _this, *name);
158 EXCEPTION_ctor(_this, name);
162 /******************************************************************
163 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
165 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor,8)
166 exception * __stdcall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
168 TRACE("(%p,%p)\n", _this, rhs);
172 _this->vtable = &MSVCRT_exception_vtable;
173 _this->name = rhs->name;
174 _this->do_free = FALSE;
177 EXCEPTION_ctor(_this, (const char**)&rhs->name);
178 TRACE("name = %s\n", _this->name);
182 /******************************************************************
183 * ??0exception@@QAE@XZ (MSVCRT.@)
185 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor,4)
186 exception * __stdcall MSVCRT_exception_default_ctor(exception * _this)
188 static const char* empty = NULL;
190 TRACE("(%p)\n", _this);
191 EXCEPTION_ctor(_this, &empty);
195 /******************************************************************
196 * ??1exception@@UAE@XZ (MSVCRT.@)
198 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor,4)
199 void __stdcall MSVCRT_exception_dtor(exception * _this)
201 TRACE("(%p)\n", _this);
202 _this->vtable = &MSVCRT_exception_vtable;
203 if (_this->do_free) MSVCRT_free(_this->name);
206 /******************************************************************
207 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
209 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals,8)
210 exception * __stdcall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
212 TRACE("(%p %p)\n", _this, rhs);
215 MSVCRT_exception_dtor(_this);
216 MSVCRT_exception_copy_ctor(_this, rhs);
218 TRACE("name = %s\n", _this->name);
222 /******************************************************************
223 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
225 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor,8)
226 void * __stdcall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
228 TRACE("(%p %x)\n", _this, flags);
231 /* we have an array, with the number of elements stored before the first object */
232 int i, *ptr = (int *)_this - 1;
234 for (i = *ptr - 1; i >= 0; i--) MSVCRT_exception_dtor(_this + i);
235 MSVCRT_operator_delete(ptr);
239 MSVCRT_exception_dtor(_this);
240 if (flags & 1) MSVCRT_operator_delete(_this);
245 /******************************************************************
246 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
248 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor,8)
249 void * __stdcall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
251 TRACE("(%p %x)\n", _this, flags);
252 MSVCRT_exception_dtor(_this);
253 if (flags & 1) MSVCRT_operator_delete(_this);
257 /******************************************************************
258 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
260 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception,4)
261 const char * __stdcall MSVCRT_what_exception(exception * _this)
263 TRACE("(%p) returning %s\n", _this, _this->name);
264 return _this->name ? _this->name : "Unknown exception";
267 /******************************************************************
268 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
270 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor,8)
271 bad_typeid * __stdcall MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
273 TRACE("(%p %p)\n", _this, rhs);
274 MSVCRT_exception_copy_ctor(_this, rhs);
275 _this->vtable = &MSVCRT_bad_typeid_vtable;
279 /******************************************************************
280 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
282 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor,8)
283 bad_typeid * __stdcall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
285 TRACE("(%p %s)\n", _this, name);
286 EXCEPTION_ctor(_this, &name);
287 _this->vtable = &MSVCRT_bad_typeid_vtable;
291 /******************************************************************
292 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
294 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor,4)
295 void __stdcall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
297 TRACE("(%p)\n", _this);
298 MSVCRT_exception_dtor(_this);
301 /******************************************************************
302 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
304 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals,8)
305 bad_typeid * __stdcall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
307 TRACE("(%p %p)\n", _this, rhs);
308 MSVCRT_exception_opequals(_this, rhs);
312 /******************************************************************
313 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
315 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor,8)
316 void * __stdcall MSVCRT_bad_typeid_vector_dtor(bad_typeid * _this, unsigned int flags)
318 TRACE("(%p %x)\n", _this, flags);
321 /* we have an array, with the number of elements stored before the first object */
322 int i, *ptr = (int *)_this - 1;
324 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_typeid_dtor(_this + i);
325 MSVCRT_operator_delete(ptr);
329 MSVCRT_bad_typeid_dtor(_this);
330 if (flags & 1) MSVCRT_operator_delete(_this);
335 /******************************************************************
336 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
338 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor,8)
339 void * __stdcall MSVCRT_bad_typeid_scalar_dtor(bad_typeid * _this, unsigned int flags)
341 TRACE("(%p %x)\n", _this, flags);
342 MSVCRT_bad_typeid_dtor(_this);
343 if (flags & 1) MSVCRT_operator_delete(_this);
347 /******************************************************************
348 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
350 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor,8)
351 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
352 const __non_rtti_object * rhs)
354 TRACE("(%p %p)\n", _this, rhs);
355 MSVCRT_bad_typeid_copy_ctor(_this, rhs);
356 _this->vtable = &MSVCRT___non_rtti_object_vtable;
360 /******************************************************************
361 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
363 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor,8)
364 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
367 TRACE("(%p %s)\n", _this, name);
368 EXCEPTION_ctor(_this, &name);
369 _this->vtable = &MSVCRT___non_rtti_object_vtable;
373 /******************************************************************
374 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
376 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor,4)
377 void __stdcall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
379 TRACE("(%p)\n", _this);
380 MSVCRT_bad_typeid_dtor(_this);
383 /******************************************************************
384 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
386 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals,8)
387 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
388 const __non_rtti_object *rhs)
390 TRACE("(%p %p)\n", _this, rhs);
391 MSVCRT_bad_typeid_opequals(_this, rhs);
395 /******************************************************************
396 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
398 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor,8)
399 void * __stdcall MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object * _this, unsigned int flags)
401 TRACE("(%p %x)\n", _this, flags);
404 /* we have an array, with the number of elements stored before the first object */
405 int i, *ptr = (int *)_this - 1;
407 for (i = *ptr - 1; i >= 0; i--) MSVCRT___non_rtti_object_dtor(_this + i);
408 MSVCRT_operator_delete(ptr);
412 MSVCRT___non_rtti_object_dtor(_this);
413 if (flags & 1) MSVCRT_operator_delete(_this);
418 /******************************************************************
419 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
421 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor,8)
422 void * __stdcall MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object * _this, unsigned int flags)
424 TRACE("(%p %x)\n", _this, flags);
425 MSVCRT___non_rtti_object_dtor(_this);
426 if (flags & 1) MSVCRT_operator_delete(_this);
430 /******************************************************************
431 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
433 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor,8)
434 bad_cast * __stdcall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
436 TRACE("(%p %s)\n", _this, *name);
437 EXCEPTION_ctor(_this, name);
438 _this->vtable = &MSVCRT_bad_cast_vtable;
442 /******************************************************************
443 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
445 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor,8)
446 bad_cast * __stdcall MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
448 TRACE("(%p %p)\n", _this, rhs);
449 MSVCRT_exception_copy_ctor(_this, rhs);
450 _this->vtable = &MSVCRT_bad_cast_vtable;
454 /******************************************************************
455 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
457 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor,4)
458 void __stdcall MSVCRT_bad_cast_dtor(bad_cast * _this)
460 TRACE("(%p)\n", _this);
461 MSVCRT_exception_dtor(_this);
464 /******************************************************************
465 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
467 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals,8)
468 bad_cast * __stdcall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
470 TRACE("(%p %p)\n", _this, rhs);
471 MSVCRT_exception_opequals(_this, rhs);
475 /******************************************************************
476 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
478 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor,8)
479 void * __stdcall MSVCRT_bad_cast_vector_dtor(bad_cast * _this, unsigned int flags)
481 TRACE("(%p %x)\n", _this, flags);
484 /* we have an array, with the number of elements stored before the first object */
485 int i, *ptr = (int *)_this - 1;
487 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_cast_dtor(_this + i);
488 MSVCRT_operator_delete(ptr);
492 MSVCRT_bad_cast_dtor(_this);
493 if (flags & 1) MSVCRT_operator_delete(_this);
498 /******************************************************************
499 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
501 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor,8)
502 void * __stdcall MSVCRT_bad_cast_scalar_dtor(bad_cast * _this, unsigned int flags)
504 TRACE("(%p %x)\n", _this, flags);
505 MSVCRT_bad_cast_dtor(_this);
506 if (flags & 1) MSVCRT_operator_delete(_this);
510 /******************************************************************
511 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
513 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals,8)
514 int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
516 int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
517 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
521 /******************************************************************
522 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
524 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals,8)
525 int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
527 int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
528 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
532 /******************************************************************
533 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
535 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before,8)
536 int __stdcall MSVCRT_type_info_before(type_info * _this, const type_info * rhs)
538 int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
539 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
543 /******************************************************************
544 * ??1type_info@@UAE@XZ (MSVCRT.@)
546 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor,4)
547 void __stdcall MSVCRT_type_info_dtor(type_info * _this)
549 TRACE("(%p)\n", _this);
550 MSVCRT_free(_this->name);
553 /******************************************************************
554 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
556 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name,4)
557 const char * __stdcall MSVCRT_type_info_name(type_info * _this)
561 /* Create and set the demangled name */
562 /* Nota: mangled name in type_info struct always start with a '.', while
563 * it isn't valid for mangled name.
564 * Is this '.' really part of the mangled name, or has it some other meaning ?
566 char* name = __unDName(0, _this->mangled + 1, 0,
567 MSVCRT_malloc, MSVCRT_free, 0x2800);
571 unsigned int len = strlen(name);
573 /* It seems _unDName may leave blanks at the end of the demangled name */
574 while (len && name[--len] == ' ')
581 /* Another thread set this member since we checked above - use it */
587 _munlock(_EXIT_LOCK2);
590 TRACE("(%p) returning %s\n", _this, _this->name);
594 /******************************************************************
595 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
597 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name,4)
598 const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
600 TRACE("(%p) returning %s\n", _this, _this->mangled);
601 return _this->mangled;
605 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor,8)
606 void * __stdcall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
608 TRACE("(%p %x)\n", _this, flags);
611 /* we have an array, with the number of elements stored before the first object */
612 int i, *ptr = (int *)_this - 1;
614 for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
615 MSVCRT_operator_delete(ptr);
619 MSVCRT_type_info_dtor(_this);
620 if (flags & 1) MSVCRT_operator_delete(_this);
629 #define __ASM_VTABLE(name,funcs) \
632 "\t.quad " __ASM_NAME(#name "_rtti") "\n" \
633 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
634 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
635 "\t.quad " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
638 #define __ASM_EXCEPTION_VTABLE(name) \
639 __ASM_VTABLE(name, "\t.quad " THISCALL_NAME(MSVCRT_what_exception) )
643 #define __ASM_VTABLE(name,funcs) \
646 "\t.long " __ASM_NAME(#name "_rtti") "\n" \
647 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
648 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
649 "\t.long " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
652 #define __ASM_EXCEPTION_VTABLE(name) \
653 __ASM_VTABLE(name, "\t.long " THISCALL_NAME(MSVCRT_what_exception) )
658 void __asm_dummy_vtables(void) {
661 __ASM_VTABLE(type_info,"")
662 __ASM_EXCEPTION_VTABLE(exception)
663 __ASM_EXCEPTION_VTABLE(bad_typeid)
664 __ASM_EXCEPTION_VTABLE(bad_cast)
665 __ASM_EXCEPTION_VTABLE(__non_rtti_object)
671 /* Static RTTI for exported objects */
673 static const type_info exception_type_info =
675 &MSVCRT_type_info_vtable,
680 static const rtti_base_descriptor exception_rtti_base_descriptor =
682 &exception_type_info,
688 static const rtti_base_array exception_rtti_base_array =
691 &exception_rtti_base_descriptor,
697 static const rtti_object_hierarchy exception_type_hierarchy =
702 &exception_rtti_base_array
705 const rtti_object_locator exception_rtti =
710 &exception_type_info,
711 &exception_type_hierarchy
714 static const cxx_type_info exception_cxx_type_info =
717 &exception_type_info,
720 (cxx_copy_ctor)THISCALL(MSVCRT_exception_copy_ctor)
723 static const type_info bad_typeid_type_info =
725 &MSVCRT_type_info_vtable,
730 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
732 &bad_typeid_type_info,
738 static const rtti_base_array bad_typeid_rtti_base_array =
741 &bad_typeid_rtti_base_descriptor,
742 &exception_rtti_base_descriptor,
747 static const rtti_object_hierarchy bad_typeid_type_hierarchy =
752 &bad_typeid_rtti_base_array
755 const rtti_object_locator bad_typeid_rtti =
760 &bad_typeid_type_info,
761 &bad_typeid_type_hierarchy
764 static const cxx_type_info bad_typeid_cxx_type_info =
767 &bad_typeid_type_info,
770 (cxx_copy_ctor)THISCALL(MSVCRT_bad_typeid_copy_ctor)
773 static const type_info bad_cast_type_info =
775 &MSVCRT_type_info_vtable,
780 static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
788 static const rtti_base_array bad_cast_rtti_base_array =
791 &bad_cast_rtti_base_descriptor,
792 &exception_rtti_base_descriptor,
797 static const rtti_object_hierarchy bad_cast_type_hierarchy =
802 &bad_cast_rtti_base_array
805 const rtti_object_locator bad_cast_rtti =
811 &bad_cast_type_hierarchy
814 static const cxx_type_info bad_cast_cxx_type_info =
820 (cxx_copy_ctor)THISCALL(MSVCRT_bad_cast_copy_ctor)
823 static const type_info __non_rtti_object_type_info =
825 &MSVCRT_type_info_vtable,
827 ".?AV__non_rtti_object@@"
830 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
832 &__non_rtti_object_type_info,
838 static const rtti_base_array __non_rtti_object_rtti_base_array =
841 &__non_rtti_object_rtti_base_descriptor,
842 &bad_typeid_rtti_base_descriptor,
843 &exception_rtti_base_descriptor
847 static const rtti_object_hierarchy __non_rtti_object_type_hierarchy =
852 &__non_rtti_object_rtti_base_array
855 const rtti_object_locator __non_rtti_object_rtti =
860 &__non_rtti_object_type_info,
861 &__non_rtti_object_type_hierarchy
864 static const cxx_type_info __non_rtti_object_cxx_type_info =
867 &__non_rtti_object_type_info,
870 (cxx_copy_ctor)THISCALL(MSVCRT___non_rtti_object_copy_ctor)
873 static const type_info type_info_type_info =
875 &MSVCRT_type_info_vtable,
880 static const rtti_base_descriptor type_info_rtti_base_descriptor =
882 &type_info_type_info,
888 static const rtti_base_array type_info_rtti_base_array =
891 &type_info_rtti_base_descriptor,
897 static const rtti_object_hierarchy type_info_type_hierarchy =
902 &type_info_rtti_base_array
905 const rtti_object_locator type_info_rtti =
910 &type_info_type_info,
911 &type_info_type_hierarchy
915 * Exception RTTI for cpp objects
917 static const cxx_type_info_table bad_cast_type_info_table =
921 &__non_rtti_object_cxx_type_info,
922 &bad_typeid_cxx_type_info,
923 &exception_cxx_type_info
927 static const cxx_exception_type bad_cast_exception_type =
930 (void*)THISCALL(MSVCRT_bad_cast_dtor),
932 &bad_cast_type_info_table
935 static const cxx_type_info_table bad_typeid_type_info_table =
939 &bad_cast_cxx_type_info,
940 &exception_cxx_type_info,
945 static const cxx_exception_type bad_typeid_exception_type =
948 (void*)THISCALL(MSVCRT_bad_typeid_dtor),
950 &bad_cast_type_info_table
953 static const cxx_exception_type __non_rtti_object_exception_type =
956 (void*)THISCALL(MSVCRT___non_rtti_object_dtor),
958 &bad_typeid_type_info_table
962 /******************************************************************
963 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
965 * Install a handler to be called when terminate() is called.
968 * func [I] Handler function to install
971 * The previously installed handler function, if any.
973 MSVCRT_terminate_function CDECL MSVCRT_set_terminate(MSVCRT_terminate_function func)
975 thread_data_t *data = msvcrt_get_thread_data();
976 MSVCRT_terminate_function previous = data->terminate_handler;
977 TRACE("(%p) returning %p\n",func,previous);
978 data->terminate_handler = func;
982 /******************************************************************
983 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
985 * Install a handler to be called when unexpected() is called.
988 * func [I] Handler function to install
991 * The previously installed handler function, if any.
993 MSVCRT_unexpected_function CDECL MSVCRT_set_unexpected(MSVCRT_unexpected_function func)
995 thread_data_t *data = msvcrt_get_thread_data();
996 MSVCRT_unexpected_function previous = data->unexpected_handler;
997 TRACE("(%p) returning %p\n",func,previous);
998 data->unexpected_handler = func;
1002 /******************************************************************
1003 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
1005 MSVCRT__se_translator_function CDECL MSVCRT__set_se_translator(MSVCRT__se_translator_function func)
1007 thread_data_t *data = msvcrt_get_thread_data();
1008 MSVCRT__se_translator_function previous = data->se_translator;
1009 TRACE("(%p) returning %p\n",func,previous);
1010 data->se_translator = func;
1014 /******************************************************************
1015 * ?terminate@@YAXXZ (MSVCRT.@)
1017 * Default handler for an unhandled exception.
1023 * This function does not return. Either control resumes from any
1024 * handler installed by calling set_terminate(), or (by default) abort()
1027 void CDECL MSVCRT_terminate(void)
1029 thread_data_t *data = msvcrt_get_thread_data();
1030 if (data->terminate_handler) data->terminate_handler();
1034 /******************************************************************
1035 * ?unexpected@@YAXXZ (MSVCRT.@)
1037 void CDECL MSVCRT_unexpected(void)
1039 thread_data_t *data = msvcrt_get_thread_data();
1040 if (data->unexpected_handler) data->unexpected_handler();
1045 /******************************************************************
1046 * __RTtypeid (MSVCRT.@)
1048 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1051 * cppobj [I] C++ object to get type information for.
1054 * Success: A type_info object describing cppobj.
1055 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
1056 * exception is thrown. If cppobj is NULL, a bad_typeid exception
1057 * is thrown. In either case, this function does not return.
1060 * This function is usually called by compiler generated code as a result
1061 * of using one of the C++ dynamic cast statements.
1063 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
1065 const type_info *ret;
1070 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
1071 _CxxThrowException( &e, &bad_typeid_exception_type );
1077 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1078 ret = obj_locator->type_descriptor;
1082 __non_rtti_object e;
1083 MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
1084 _CxxThrowException( &e, &bad_typeid_exception_type );
1091 /******************************************************************
1092 * __RTDynamicCast (MSVCRT.@)
1094 * Dynamically cast a C++ object to one of its base classes.
1097 * cppobj [I] Any C++ object to cast
1098 * unknown [I] Reserved, set to 0
1099 * src [I] type_info object describing cppobj
1100 * dst [I] type_info object describing the base class to cast to
1101 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
1104 * Success: The address of cppobj, cast to the object described by dst.
1105 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
1106 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
1107 * is thrown and this function does not return.
1110 * This function is usually called by compiler generated code as a result
1111 * of using one of the C++ dynamic cast statements.
1113 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
1114 type_info *src, type_info *dst,
1119 if (!cppobj) return NULL;
1121 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
1122 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
1124 /* To cast an object at runtime:
1125 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
1126 * 2.Search for the destination type in the class hierarchy
1127 * 3.If destination type is found, return base object address + dest offset
1128 * Otherwise, fail the cast
1130 * FIXME: the unknown parameter doesn't seem to be used for anything
1135 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1136 const rtti_object_hierarchy *obj_bases = obj_locator->type_hierarchy;
1137 const rtti_base_descriptor * const* base_desc = obj_bases->base_classes->bases;
1139 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
1142 for (i = 0; i < obj_bases->array_len; i++)
1144 const type_info *typ = base_desc[i]->type_descriptor;
1146 if (!strcmp(typ->mangled, dst->mangled))
1148 /* compute the correct this pointer for that base class */
1149 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
1150 ret = get_this_pointer( &base_desc[i]->offsets, this_ptr );
1154 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
1155 * to a reference, since references cannot be NULL.
1157 if (!ret && do_throw)
1159 const char *msg = "Bad dynamic_cast!";
1161 MSVCRT_bad_cast_ctor( &e, &msg );
1162 _CxxThrowException( &e, &bad_cast_exception_type );
1167 __non_rtti_object e;
1168 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1169 _CxxThrowException( &e, &bad_typeid_exception_type );
1177 /******************************************************************
1178 * __RTCastToVoid (MSVCRT.@)
1180 * Dynamically cast a C++ object to a void*.
1183 * cppobj [I] The C++ object to cast
1186 * Success: The base address of the object as a void*.
1187 * Failure: NULL, if cppobj is NULL or has no RTTI.
1190 * This function is usually called by compiler generated code as a result
1191 * of using one of the C++ dynamic cast statements.
1193 void* CDECL MSVCRT___RTCastToVoid(void *cppobj)
1197 if (!cppobj) return NULL;
1201 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1202 ret = (char *)cppobj - obj_locator->base_class_offset;
1206 __non_rtti_object e;
1207 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1208 _CxxThrowException( &e, &bad_typeid_exception_type );