2 * msvcrt.dll C++ objects
4 * Copyright 2000 Jon Griffiths
5 * Copyright 2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
26 #include "wine/exception.h"
28 #include "wine/debug.h"
29 #include "msvcrt/malloc.h"
30 #include "msvcrt/stdlib.h"
33 #include "cppexcept.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
39 * exception object: base for exception, bad_cast, bad_typeid, __non_rtti_object
43 void* pfn_vector_dtor;
47 typedef struct __exception
49 const exception_vtable *vtable;
50 char *name; /* Name of this exception, always a new copy for each object */
51 int do_free; /* Whether to free 'name' in our dtor */
54 typedef exception bad_cast;
55 typedef exception bad_typeid;
56 typedef exception __non_rtti_object;
58 typedef struct _rtti_base_descriptor
60 type_info *type_descriptor;
62 int base_class_offset;
66 } rtti_base_descriptor;
68 typedef struct _rtti_base_array
70 const rtti_base_descriptor *bases[3]; /* First element is the class itself */
73 typedef struct _rtti_object_hierachy
77 int array_len; /* Size of the array pointed to by 'base_classes' */
78 const rtti_base_array *base_classes;
79 } rtti_object_hierachy;
81 typedef struct _rtti_object_locator
84 int base_class_offset;
86 type_info *type_descriptor;
87 const rtti_object_hierachy *type_hierachy;
88 } rtti_object_locator;
91 #ifdef __i386__ /* thiscall functions are i386-specific */
93 #define DEFINE_THISCALL_WRAPPER(func) \
94 extern void __thiscall_ ## func(); \
95 __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
99 "jmp " __ASM_NAME(#func) )
101 const exception_vtable MSVCRT_exception_vtable;
102 const exception_vtable MSVCRT_bad_typeid_vtable;
103 const exception_vtable MSVCRT_bad_cast_vtable;
104 const exception_vtable MSVCRT___non_rtti_object_vtable;
105 static const exception_vtable MSVCRT_type_info_vtable;
107 /* Internal common ctor for exception */
108 static void WINAPI EXCEPTION_ctor(exception *_this, const char** name)
110 _this->vtable = &MSVCRT_exception_vtable;
113 size_t name_len = strlen(*name) + 1;
114 _this->name = MSVCRT_malloc(name_len);
115 memcpy(_this->name, *name, name_len);
116 _this->do_free = TRUE;
121 _this->do_free = FALSE;
125 /******************************************************************
126 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
128 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor);
129 exception * __stdcall MSVCRT_exception_ctor(exception * _this, const char ** name)
131 TRACE("(%p,%s)\n", _this, *name);
132 EXCEPTION_ctor(_this, name);
136 /******************************************************************
137 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
139 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor);
140 exception * __stdcall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
142 TRACE("(%p,%p)\n", _this, rhs);
146 _this->vtable = &MSVCRT_exception_vtable;
147 _this->name = rhs->name;
148 _this->do_free = FALSE;
151 EXCEPTION_ctor(_this, (const char**)&rhs->name);
152 TRACE("name = %s\n", _this->name);
156 /******************************************************************
157 * ??0exception@@QAE@XZ (MSVCRT.@)
159 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor);
160 exception * __stdcall MSVCRT_exception_default_ctor(exception * _this)
162 static const char* empty = NULL;
164 TRACE("(%p)\n", _this);
165 EXCEPTION_ctor(_this, &empty);
169 /******************************************************************
170 * ??1exception@@UAE@XZ (MSVCRT.@)
172 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor);
173 void __stdcall MSVCRT_exception_dtor(exception * _this)
175 TRACE("(%p)\n", _this);
176 _this->vtable = &MSVCRT_exception_vtable;
177 if (_this->do_free) MSVCRT_free(_this->name);
180 /******************************************************************
181 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
183 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals);
184 exception * __stdcall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
186 TRACE("(%p %p)\n", _this, rhs);
189 MSVCRT_exception_dtor(_this);
190 MSVCRT_exception_copy_ctor(_this, rhs);
192 TRACE("name = %s\n", _this->name);
196 /******************************************************************
197 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
199 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor);
200 void * __stdcall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
202 TRACE("(%p %x)\n", _this, flags);
205 /* we have an array, with the number of elements stored before the first object */
206 int i, *ptr = (int *)_this - 1;
208 for (i = *ptr - 1; i >= 0; i--) MSVCRT_exception_dtor(_this + i);
209 MSVCRT_operator_delete(ptr);
213 MSVCRT_exception_dtor(_this);
214 if (flags & 1) MSVCRT_operator_delete(_this);
219 /******************************************************************
220 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
222 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor);
223 void * __stdcall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
225 TRACE("(%p %x)\n", _this, flags);
226 MSVCRT_exception_dtor(_this);
227 if (flags & 1) MSVCRT_operator_delete(_this);
231 /******************************************************************
232 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
234 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception);
235 const char * __stdcall MSVCRT_what_exception(exception * _this)
237 TRACE("(%p) returning %s\n", _this, _this->name);
238 return _this->name ? _this->name : "Unknown exception";
241 /******************************************************************
242 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
244 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor);
245 bad_typeid * __stdcall MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
247 TRACE("(%p %p)\n", _this, rhs);
248 MSVCRT_exception_copy_ctor(_this, rhs);
249 _this->vtable = &MSVCRT_bad_typeid_vtable;
253 /******************************************************************
254 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
256 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor);
257 bad_typeid * __stdcall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
259 TRACE("(%p %s)\n", _this, name);
260 EXCEPTION_ctor(_this, &name);
261 _this->vtable = &MSVCRT_bad_typeid_vtable;
265 /******************************************************************
266 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
268 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor);
269 void __stdcall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
271 TRACE("(%p)\n", _this);
272 MSVCRT_exception_dtor(_this);
275 /******************************************************************
276 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
278 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals);
279 bad_typeid * __stdcall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
281 TRACE("(%p %p)\n", _this, rhs);
282 MSVCRT_exception_opequals(_this, rhs);
286 /******************************************************************
287 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
289 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor);
290 void * __stdcall MSVCRT_bad_typeid_vector_dtor(bad_typeid * _this, unsigned int flags)
292 TRACE("(%p %x)\n", _this, flags);
295 /* we have an array, with the number of elements stored before the first object */
296 int i, *ptr = (int *)_this - 1;
298 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_typeid_dtor(_this + i);
299 MSVCRT_operator_delete(ptr);
303 MSVCRT_bad_typeid_dtor(_this);
304 if (flags & 1) MSVCRT_operator_delete(_this);
309 /******************************************************************
310 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
312 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor);
313 void * __stdcall MSVCRT_bad_typeid_scalar_dtor(bad_typeid * _this, unsigned int flags)
315 TRACE("(%p %x)\n", _this, flags);
316 MSVCRT_bad_typeid_dtor(_this);
317 if (flags & 1) MSVCRT_operator_delete(_this);
321 /******************************************************************
322 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
324 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor);
325 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
326 const __non_rtti_object * rhs)
328 TRACE("(%p %p)\n", _this, rhs);
329 MSVCRT_bad_typeid_copy_ctor(_this, rhs);
330 _this->vtable = &MSVCRT___non_rtti_object_vtable;
334 /******************************************************************
335 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
337 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor);
338 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
341 TRACE("(%p %s)\n", _this, name);
342 EXCEPTION_ctor(_this, &name);
343 _this->vtable = &MSVCRT___non_rtti_object_vtable;
347 /******************************************************************
348 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
350 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor);
351 void __stdcall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
353 TRACE("(%p)\n", _this);
354 MSVCRT_bad_typeid_dtor(_this);
357 /******************************************************************
358 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
360 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals);
361 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
362 const __non_rtti_object *rhs)
364 TRACE("(%p %p)\n", _this, rhs);
365 MSVCRT_bad_typeid_opequals(_this, rhs);
369 /******************************************************************
370 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
372 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor);
373 void * __stdcall MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object * _this, unsigned int flags)
375 TRACE("(%p %x)\n", _this, flags);
378 /* we have an array, with the number of elements stored before the first object */
379 int i, *ptr = (int *)_this - 1;
381 for (i = *ptr - 1; i >= 0; i--) MSVCRT___non_rtti_object_dtor(_this + i);
382 MSVCRT_operator_delete(ptr);
386 MSVCRT___non_rtti_object_dtor(_this);
387 if (flags & 1) MSVCRT_operator_delete(_this);
392 /******************************************************************
393 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
395 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor);
396 void * __stdcall MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object * _this, unsigned int flags)
398 TRACE("(%p %x)\n", _this, flags);
399 MSVCRT___non_rtti_object_dtor(_this);
400 if (flags & 1) MSVCRT_operator_delete(_this);
404 /******************************************************************
405 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
407 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor);
408 bad_cast * __stdcall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
410 TRACE("(%p %s)\n", _this, *name);
411 EXCEPTION_ctor(_this, name);
412 _this->vtable = &MSVCRT_bad_cast_vtable;
416 /******************************************************************
417 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
419 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor);
420 bad_cast * __stdcall MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
422 TRACE("(%p %p)\n", _this, rhs);
423 MSVCRT_exception_copy_ctor(_this, rhs);
424 _this->vtable = &MSVCRT_bad_cast_vtable;
428 /******************************************************************
429 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
431 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor);
432 void __stdcall MSVCRT_bad_cast_dtor(bad_cast * _this)
434 TRACE("(%p)\n", _this);
435 MSVCRT_exception_dtor(_this);
438 /******************************************************************
439 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
441 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals);
442 bad_cast * __stdcall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
444 TRACE("(%p %p)\n", _this, rhs);
445 MSVCRT_exception_opequals(_this, rhs);
449 /******************************************************************
450 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
452 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor);
453 void * __stdcall MSVCRT_bad_cast_vector_dtor(bad_cast * _this, unsigned int flags)
455 TRACE("(%p %x)\n", _this, flags);
458 /* we have an array, with the number of elements stored before the first object */
459 int i, *ptr = (int *)_this - 1;
461 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_cast_dtor(_this + i);
462 MSVCRT_operator_delete(ptr);
466 MSVCRT_bad_cast_dtor(_this);
467 if (flags & 1) MSVCRT_operator_delete(_this);
472 /******************************************************************
473 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
475 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor);
476 void * __stdcall MSVCRT_bad_cast_scalar_dtor(bad_cast * _this, unsigned int flags)
478 TRACE("(%p %x)\n", _this, flags);
479 MSVCRT_bad_cast_dtor(_this);
480 if (flags & 1) MSVCRT_operator_delete(_this);
484 /******************************************************************
485 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
487 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals);
488 int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
490 int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
491 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
495 /******************************************************************
496 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
498 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals);
499 int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
501 int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
502 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
506 /******************************************************************
507 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
509 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before);
510 int __stdcall MSVCRT_type_info_before(type_info * _this, const type_info * rhs)
512 int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
513 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
517 /******************************************************************
518 * ??1type_info@@UAE@XZ (MSVCRT.@)
520 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor);
521 void __stdcall MSVCRT_type_info_dtor(type_info * _this)
523 TRACE("(%p)\n", _this);
525 MSVCRT_free(_this->name);
528 /******************************************************************
529 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
531 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name);
532 const char * __stdcall MSVCRT_type_info_name(type_info * _this)
536 /* Create and set the demangled name */
537 char* name = MSVCRT___unDName(0, _this->mangled, 0,
538 (MSVCRT_malloc_func)MSVCRT_malloc,
539 (MSVCRT_free_func)MSVCRT_free, 0x2800);
543 unsigned int len = strlen(name);
545 /* It seems _unDName may leave blanks at the end of the demangled name */
546 if (name[len] == ' ')
553 /* Another thread set this member since we checked above - use it */
559 _munlock(_EXIT_LOCK2);
562 TRACE("(%p) returning %s\n", _this, _this->name);
566 /******************************************************************
567 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
569 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name);
570 const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
572 TRACE("(%p) returning %s\n", _this, _this->mangled);
573 return _this->mangled;
577 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor);
578 void * __stdcall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
580 TRACE("(%p %x)\n", _this, flags);
583 /* we have an array, with the number of elements stored before the first object */
584 int i, *ptr = (int *)_this - 1;
586 for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
587 MSVCRT_operator_delete(ptr);
591 MSVCRT_type_info_dtor(_this);
592 if (flags & 1) MSVCRT_operator_delete(_this);
599 const exception_vtable MSVCRT_exception_vtable =
601 __thiscall_MSVCRT_exception_vector_dtor,
602 __thiscall_MSVCRT_what_exception
605 const exception_vtable MSVCRT_bad_typeid_vtable =
607 __thiscall_MSVCRT_bad_typeid_vector_dtor,
608 __thiscall_MSVCRT_what_exception
611 const exception_vtable MSVCRT_bad_cast_vtable =
613 __thiscall_MSVCRT_bad_cast_vector_dtor,
614 __thiscall_MSVCRT_what_exception
617 const exception_vtable MSVCRT___non_rtti_object_vtable =
619 __thiscall_MSVCRT___non_rtti_object_vector_dtor,
620 __thiscall_MSVCRT_what_exception
623 static const exception_vtable MSVCRT_type_info_vtable =
625 __thiscall_MSVCRT_type_info_vector_dtor,
629 /* Static RTTI for exported objects */
631 static type_info exception_type_info =
633 (void*)&MSVCRT_type_info_vtable,
638 static const rtti_base_descriptor exception_rtti_base_descriptor =
640 &exception_type_info,
648 static const rtti_base_array exception_rtti_base_array =
651 &exception_rtti_base_descriptor,
657 static const rtti_object_hierachy exception_type_hierachy =
662 &exception_rtti_base_array
665 static const rtti_object_locator exception_rtti =
670 &exception_type_info,
671 &exception_type_hierachy
674 static const cxx_type_info exception_cxx_type_info =
677 &exception_type_info,
682 (cxx_copy_ctor)__thiscall_MSVCRT_exception_copy_ctor
685 static type_info bad_typeid_type_info =
687 (void*)&MSVCRT_type_info_vtable,
692 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
694 &bad_typeid_type_info,
702 static const rtti_base_array bad_typeid_rtti_base_array =
705 &bad_typeid_rtti_base_descriptor,
706 &exception_rtti_base_descriptor,
711 static const rtti_object_hierachy bad_typeid_type_hierachy =
716 &bad_typeid_rtti_base_array
719 static const rtti_object_locator bad_typeid_rtti =
724 &bad_typeid_type_info,
725 &bad_typeid_type_hierachy
728 static const cxx_type_info bad_typeid_cxx_type_info =
731 &bad_typeid_type_info,
736 (cxx_copy_ctor)__thiscall_MSVCRT_bad_typeid_copy_ctor
739 static type_info bad_cast_type_info =
741 (void*)&MSVCRT_type_info_vtable,
746 static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
756 static const rtti_base_array bad_cast_rtti_base_array =
759 &bad_cast_rtti_base_descriptor,
760 &exception_rtti_base_descriptor,
765 static const rtti_object_hierachy bad_cast_type_hierachy =
770 &bad_cast_rtti_base_array
773 static const rtti_object_locator bad_cast_rtti =
779 &bad_cast_type_hierachy
782 static const cxx_type_info bad_cast_cxx_type_info =
790 (cxx_copy_ctor)__thiscall_MSVCRT_bad_cast_copy_ctor
793 static type_info __non_rtti_object_type_info =
795 (void*)&MSVCRT_type_info_vtable,
797 ".?AV__non_rtti_object@@"
800 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
802 &__non_rtti_object_type_info,
810 static const rtti_base_array __non_rtti_object_rtti_base_array =
813 &__non_rtti_object_rtti_base_descriptor,
814 &bad_typeid_rtti_base_descriptor,
815 &exception_rtti_base_descriptor
819 static const rtti_object_hierachy __non_rtti_object_type_hierachy =
824 &__non_rtti_object_rtti_base_array
827 static const rtti_object_locator __non_rtti_object_rtti =
832 &__non_rtti_object_type_info,
833 &__non_rtti_object_type_hierachy
836 static const cxx_type_info __non_rtti_object_cxx_type_info =
839 &__non_rtti_object_type_info,
844 (cxx_copy_ctor)__thiscall_MSVCRT___non_rtti_object_copy_ctor
847 static type_info type_info_type_info =
849 (void*)&MSVCRT_type_info_vtable,
854 static const rtti_base_descriptor type_info_rtti_base_descriptor =
856 &type_info_type_info,
864 static const rtti_base_array type_info_rtti_base_array =
867 &type_info_rtti_base_descriptor,
873 static const rtti_object_hierachy type_info_type_hierachy =
878 &type_info_rtti_base_array
881 static const rtti_object_locator type_info_rtti =
886 &type_info_type_info,
887 &type_info_type_hierachy
891 * Exception RTTI for cpp objects
893 static const cxx_type_info_table bad_cast_type_info_table =
897 &__non_rtti_object_cxx_type_info,
898 &bad_typeid_cxx_type_info,
899 &exception_cxx_type_info
903 static const cxx_exception_type bad_cast_exception_type =
906 (void*)__thiscall_MSVCRT_bad_cast_dtor,
908 &bad_cast_type_info_table
911 static const cxx_type_info_table bad_typeid_type_info_table =
915 &bad_cast_cxx_type_info,
916 &exception_cxx_type_info,
921 static const cxx_exception_type bad_typeid_exception_type =
924 (void*)__thiscall_MSVCRT_bad_typeid_dtor,
926 &bad_cast_type_info_table
929 static const cxx_exception_type __non_rtti_object_exception_type =
932 (void*)__thiscall_MSVCRT___non_rtti_object_dtor,
934 &bad_typeid_type_info_table
937 #endif /* __i386__ */
940 /******************************************************************
941 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
943 * Install a handler to be called when terminate() is called.
946 * func [I] Handler function to install
949 * The previously installed handler function, if any.
951 terminate_function MSVCRT_set_terminate(terminate_function func)
953 MSVCRT_thread_data *data = msvcrt_get_thread_data();
954 terminate_function previous = data->terminate_handler;
955 TRACE("(%p) returning %p\n",func,previous);
956 data->terminate_handler = func;
960 /******************************************************************
961 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
963 * Install a handler to be called when unexpected() is called.
966 * func [I] Handler function to install
969 * The previously installed handler function, if any.
971 unexpected_function MSVCRT_set_unexpected(unexpected_function func)
973 MSVCRT_thread_data *data = msvcrt_get_thread_data();
974 unexpected_function previous = data->unexpected_handler;
975 TRACE("(%p) returning %p\n",func,previous);
976 data->unexpected_handler = func;
980 /******************************************************************
981 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
983 _se_translator_function MSVCRT__set_se_translator(_se_translator_function func)
985 MSVCRT_thread_data *data = msvcrt_get_thread_data();
986 _se_translator_function previous = data->se_translator;
987 TRACE("(%p) returning %p\n",func,previous);
988 data->se_translator = func;
992 /******************************************************************
993 * ?terminate@@YAXXZ (MSVCRT.@)
995 * Default handler for an unhandled exception.
1001 * This function does not return. Either control resumes from any
1002 * handler installed by calling set_terminate(), or (by default) abort()
1005 void MSVCRT_terminate(void)
1007 MSVCRT_thread_data *data = msvcrt_get_thread_data();
1008 if (data->terminate_handler) data->terminate_handler();
1012 /******************************************************************
1013 * ?unexpected@@YAXXZ (MSVCRT.@)
1015 void MSVCRT_unexpected(void)
1017 MSVCRT_thread_data *data = msvcrt_get_thread_data();
1018 if (data->unexpected_handler) data->unexpected_handler();
1022 /* Get type info from an object (internal) */
1023 static const rtti_object_locator* RTTI_GetObjectLocator(type_info *cppobj)
1025 const rtti_object_locator *obj_locator = NULL;
1028 const exception_vtable* vtable = (const exception_vtable*)cppobj->vtable;
1030 /* Perhaps this is one of classes we export? */
1031 if (vtable == &MSVCRT_exception_vtable)
1033 TRACE("returning exception_rtti\n");
1034 return &exception_rtti;
1036 else if (vtable == &MSVCRT_bad_typeid_vtable)
1038 TRACE("returning bad_typeid_rtti\n");
1039 return &bad_typeid_rtti;
1041 else if (vtable == &MSVCRT_bad_cast_vtable)
1043 TRACE("returning bad_cast_rtti\n");
1044 return &bad_cast_rtti;
1046 else if (vtable == &MSVCRT___non_rtti_object_vtable)
1048 TRACE("returning __non_rtti_object_rtti\n");
1049 return &__non_rtti_object_rtti;
1051 else if (vtable == &MSVCRT_type_info_vtable)
1053 TRACE("returning type_info_rtti\n");
1054 return &type_info_rtti;
1058 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
1059 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
1060 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
1062 obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
1063 TRACE("returning type_info from vtable (%p)\n", obj_locator);
1069 /******************************************************************
1070 * __RTtypeid (MSVCRT.@)
1072 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1075 * cppobj [I] C++ object to get type information for.
1078 * Success: A type_info object describing cppobj.
1079 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
1080 * exception is thrown. If cppobj is NULL, a bad_typeid exception
1081 * is thrown. In either case, this function does not return.
1084 * This function is usually called by compiler generated code as a result
1085 * of using one of the C++ dynamic cast statements.
1087 type_info* MSVCRT___RTtypeid(type_info *cppobj)
1089 const rtti_object_locator *obj_locator = RTTI_GetObjectLocator(cppobj);
1094 static const char* szNullPtr = "Attempted a typeid of NULL pointer!";
1095 static const char* szBadPtr = "Bad read pointer - no RTTI data!";
1096 const cxx_exception_type *e_type;
1099 /* Throw a bad_typeid or __non_rtti_object exception */
1102 EXCEPTION_ctor(&e, &szNullPtr);
1103 e.vtable = &MSVCRT_bad_typeid_vtable;
1104 e_type = &bad_typeid_exception_type;
1108 EXCEPTION_ctor(&e, &szBadPtr);
1109 e.vtable = &MSVCRT___non_rtti_object_vtable;
1110 e_type = &__non_rtti_object_exception_type;
1113 _CxxThrowException(&e, e_type);
1116 return obj_locator->type_descriptor;
1122 /******************************************************************
1123 * __RTDynamicCast (MSVCRT.@)
1125 * Dynamically cast a C++ object to one of its base classes.
1128 * cppobj [I] Any C++ object to cast
1129 * unknown [I] Reserved, set to 0
1130 * src [I] type_info object describing cppobj
1131 * dst [I] type_info object describing the base class to cast to
1132 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
1135 * Success: The address of cppobj, cast to the object described by dst.
1136 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
1137 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
1138 * is thrown and this function does not return.
1141 * This function is usually called by compiler generated code as a result
1142 * of using one of the C++ dynamic cast statements.
1144 void* MSVCRT___RTDynamicCast(type_info *cppobj, int unknown,
1145 type_info *src, type_info *dst,
1148 const rtti_object_locator *obj_locator = RTTI_GetObjectLocator(cppobj);
1150 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
1151 TRACE("(%p,%d,%p,%p,%d)\n", cppobj, unknown, src, dst, do_throw);
1154 FIXME("Unknown parameter is non-zero: please report\n");
1156 /* To cast an object at runtime:
1157 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
1158 * 2.Search for the destination type in the class heirachy
1159 * 3.If destination type is found, return base object address + dest offset
1160 * Otherwise, fail the cast
1165 const rtti_object_hierachy *obj_bases = obj_locator->type_hierachy;
1166 const rtti_base_descriptor **base_desc = obj_bases->base_classes->bases;
1167 int src_offset = obj_locator->base_class_offset, dst_offset = -1;
1169 while (count < obj_bases->array_len)
1171 const type_info *typ = (*base_desc)->type_descriptor;
1173 if (!strcmp(typ->mangled, dst->mangled))
1175 dst_offset = (*base_desc)->base_class_offset;
1181 if (dst_offset >= 0)
1182 return (void*)((unsigned long)cppobj - src_offset + dst_offset);
1186 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
1187 * to a reference, since references cannot be NULL.
1191 static const char* exception_text = "Bad dynamic_cast!";
1194 /* Throw a bad_cast exception */
1195 EXCEPTION_ctor(&e, &exception_text);
1196 e.vtable = &MSVCRT_bad_cast_vtable;
1197 _CxxThrowException(&e, &bad_cast_exception_type);
1205 /******************************************************************
1206 * __RTCastToVoid (MSVCRT.@)
1208 * Dynamically cast a C++ object to a void*.
1211 * cppobj [I] The C++ object to cast
1214 * Success: The base address of the object as a void*.
1215 * Failure: NULL, if cppobj is NULL or has no RTTI.
1218 * This function is usually called by compiler generated code as a result
1219 * of using one of the C++ dynamic cast statements.
1221 void* MSVCRT___RTCastToVoid(type_info *cppobj)
1223 const rtti_object_locator *obj_locator = RTTI_GetObjectLocator(cppobj);
1225 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
1226 TRACE("(%p)\n", cppobj);
1228 /* Casts to void* simply cast to the base object */
1230 return (void*)((unsigned long)cppobj - obj_locator->base_class_offset);