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"
31 #include "wine/exception.h"
33 #include "wine/debug.h"
34 #include "msvcrt/malloc.h"
35 #include "msvcrt/stdlib.h"
38 #include "cppexcept.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
44 * exception object: base for exception, bad_cast, bad_typeid, __non_rtti_object
48 void* pfn_vector_dtor;
52 typedef struct __exception
54 const exception_vtable *vtable;
55 char *name; /* Name of this exception, always a new copy for each object */
56 int do_free; /* Whether to free 'name' in our dtor */
59 typedef exception bad_cast;
60 typedef exception bad_typeid;
61 typedef exception __non_rtti_object;
63 typedef struct _rtti_base_descriptor
65 type_info *type_descriptor;
67 int base_class_offset;
71 } rtti_base_descriptor;
73 typedef struct _rtti_base_array
75 const rtti_base_descriptor *bases[3]; /* First element is the class itself */
78 typedef struct _rtti_object_hierachy
82 int array_len; /* Size of the array pointed to by 'base_classes' */
83 const rtti_base_array *base_classes;
84 } rtti_object_hierachy;
86 typedef struct _rtti_object_locator
89 int base_class_offset;
91 type_info *type_descriptor;
92 const rtti_object_hierachy *type_hierachy;
93 } rtti_object_locator;
96 #ifdef __i386__ /* thiscall functions are i386-specific */
98 #define DEFINE_THISCALL_WRAPPER(func) \
99 extern void __thiscall_ ## func(); \
100 __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
104 "jmp " __ASM_NAME(#func) )
106 const exception_vtable MSVCRT_exception_vtable;
107 const exception_vtable MSVCRT_bad_typeid_vtable;
108 const exception_vtable MSVCRT_bad_cast_vtable;
109 const exception_vtable MSVCRT___non_rtti_object_vtable;
110 static const exception_vtable MSVCRT_type_info_vtable;
112 /* Internal common ctor for exception */
113 static void WINAPI EXCEPTION_ctor(exception *_this, const char** name)
115 _this->vtable = &MSVCRT_exception_vtable;
118 size_t name_len = strlen(*name) + 1;
119 _this->name = MSVCRT_malloc(name_len);
120 memcpy(_this->name, *name, name_len);
121 _this->do_free = TRUE;
126 _this->do_free = FALSE;
130 /******************************************************************
131 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
133 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor);
134 exception * __stdcall MSVCRT_exception_ctor(exception * _this, const char ** name)
136 TRACE("(%p,%s)\n", _this, *name);
137 EXCEPTION_ctor(_this, name);
141 /******************************************************************
142 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
144 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor);
145 exception * __stdcall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
147 TRACE("(%p,%p)\n", _this, rhs);
151 _this->vtable = &MSVCRT_exception_vtable;
152 _this->name = rhs->name;
153 _this->do_free = FALSE;
156 EXCEPTION_ctor(_this, (const char**)&rhs->name);
157 TRACE("name = %s\n", _this->name);
161 /******************************************************************
162 * ??0exception@@QAE@XZ (MSVCRT.@)
164 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor);
165 exception * __stdcall MSVCRT_exception_default_ctor(exception * _this)
167 static const char* empty = NULL;
169 TRACE("(%p)\n", _this);
170 EXCEPTION_ctor(_this, &empty);
174 /******************************************************************
175 * ??1exception@@UAE@XZ (MSVCRT.@)
177 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor);
178 void __stdcall MSVCRT_exception_dtor(exception * _this)
180 TRACE("(%p)\n", _this);
181 _this->vtable = &MSVCRT_exception_vtable;
182 if (_this->do_free) MSVCRT_free(_this->name);
185 /******************************************************************
186 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
188 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals);
189 exception * __stdcall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
191 TRACE("(%p %p)\n", _this, rhs);
194 MSVCRT_exception_dtor(_this);
195 MSVCRT_exception_copy_ctor(_this, rhs);
197 TRACE("name = %s\n", _this->name);
201 /******************************************************************
202 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
204 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor);
205 void * __stdcall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
207 TRACE("(%p %x)\n", _this, flags);
210 /* we have an array, with the number of elements stored before the first object */
211 int i, *ptr = (int *)_this - 1;
213 for (i = *ptr - 1; i >= 0; i--) MSVCRT_exception_dtor(_this + i);
214 MSVCRT_operator_delete(ptr);
218 MSVCRT_exception_dtor(_this);
219 if (flags & 1) MSVCRT_operator_delete(_this);
224 /******************************************************************
225 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
227 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor);
228 void * __stdcall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
230 TRACE("(%p %x)\n", _this, flags);
231 MSVCRT_exception_dtor(_this);
232 if (flags & 1) MSVCRT_operator_delete(_this);
236 /******************************************************************
237 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
239 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception);
240 const char * __stdcall MSVCRT_what_exception(exception * _this)
242 TRACE("(%p) returning %s\n", _this, _this->name);
243 return _this->name ? _this->name : "Unknown exception";
246 /******************************************************************
247 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
249 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor);
250 bad_typeid * __stdcall MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
252 TRACE("(%p %p)\n", _this, rhs);
253 MSVCRT_exception_copy_ctor(_this, rhs);
254 _this->vtable = &MSVCRT_bad_typeid_vtable;
258 /******************************************************************
259 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
261 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor);
262 bad_typeid * __stdcall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
264 TRACE("(%p %s)\n", _this, name);
265 EXCEPTION_ctor(_this, &name);
266 _this->vtable = &MSVCRT_bad_typeid_vtable;
270 /******************************************************************
271 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
273 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor);
274 void __stdcall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
276 TRACE("(%p)\n", _this);
277 MSVCRT_exception_dtor(_this);
280 /******************************************************************
281 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
283 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals);
284 bad_typeid * __stdcall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
286 TRACE("(%p %p)\n", _this, rhs);
287 MSVCRT_exception_opequals(_this, rhs);
291 /******************************************************************
292 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
294 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor);
295 void * __stdcall MSVCRT_bad_typeid_vector_dtor(bad_typeid * _this, unsigned int flags)
297 TRACE("(%p %x)\n", _this, flags);
300 /* we have an array, with the number of elements stored before the first object */
301 int i, *ptr = (int *)_this - 1;
303 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_typeid_dtor(_this + i);
304 MSVCRT_operator_delete(ptr);
308 MSVCRT_bad_typeid_dtor(_this);
309 if (flags & 1) MSVCRT_operator_delete(_this);
314 /******************************************************************
315 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
317 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor);
318 void * __stdcall MSVCRT_bad_typeid_scalar_dtor(bad_typeid * _this, unsigned int flags)
320 TRACE("(%p %x)\n", _this, flags);
321 MSVCRT_bad_typeid_dtor(_this);
322 if (flags & 1) MSVCRT_operator_delete(_this);
326 /******************************************************************
327 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
329 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor);
330 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
331 const __non_rtti_object * rhs)
333 TRACE("(%p %p)\n", _this, rhs);
334 MSVCRT_bad_typeid_copy_ctor(_this, rhs);
335 _this->vtable = &MSVCRT___non_rtti_object_vtable;
339 /******************************************************************
340 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
342 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor);
343 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
346 TRACE("(%p %s)\n", _this, name);
347 EXCEPTION_ctor(_this, &name);
348 _this->vtable = &MSVCRT___non_rtti_object_vtable;
352 /******************************************************************
353 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
355 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor);
356 void __stdcall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
358 TRACE("(%p)\n", _this);
359 MSVCRT_bad_typeid_dtor(_this);
362 /******************************************************************
363 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
365 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals);
366 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
367 const __non_rtti_object *rhs)
369 TRACE("(%p %p)\n", _this, rhs);
370 MSVCRT_bad_typeid_opequals(_this, rhs);
374 /******************************************************************
375 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
377 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor);
378 void * __stdcall MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object * _this, unsigned int flags)
380 TRACE("(%p %x)\n", _this, flags);
383 /* we have an array, with the number of elements stored before the first object */
384 int i, *ptr = (int *)_this - 1;
386 for (i = *ptr - 1; i >= 0; i--) MSVCRT___non_rtti_object_dtor(_this + i);
387 MSVCRT_operator_delete(ptr);
391 MSVCRT___non_rtti_object_dtor(_this);
392 if (flags & 1) MSVCRT_operator_delete(_this);
397 /******************************************************************
398 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
400 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor);
401 void * __stdcall MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object * _this, unsigned int flags)
403 TRACE("(%p %x)\n", _this, flags);
404 MSVCRT___non_rtti_object_dtor(_this);
405 if (flags & 1) MSVCRT_operator_delete(_this);
409 /******************************************************************
410 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
412 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor);
413 bad_cast * __stdcall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
415 TRACE("(%p %s)\n", _this, *name);
416 EXCEPTION_ctor(_this, name);
417 _this->vtable = &MSVCRT_bad_cast_vtable;
421 /******************************************************************
422 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
424 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor);
425 bad_cast * __stdcall MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
427 TRACE("(%p %p)\n", _this, rhs);
428 MSVCRT_exception_copy_ctor(_this, rhs);
429 _this->vtable = &MSVCRT_bad_cast_vtable;
433 /******************************************************************
434 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
436 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor);
437 void __stdcall MSVCRT_bad_cast_dtor(bad_cast * _this)
439 TRACE("(%p)\n", _this);
440 MSVCRT_exception_dtor(_this);
443 /******************************************************************
444 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
446 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals);
447 bad_cast * __stdcall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
449 TRACE("(%p %p)\n", _this, rhs);
450 MSVCRT_exception_opequals(_this, rhs);
454 /******************************************************************
455 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
457 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor);
458 void * __stdcall MSVCRT_bad_cast_vector_dtor(bad_cast * _this, unsigned int flags)
460 TRACE("(%p %x)\n", _this, flags);
463 /* we have an array, with the number of elements stored before the first object */
464 int i, *ptr = (int *)_this - 1;
466 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_cast_dtor(_this + i);
467 MSVCRT_operator_delete(ptr);
471 MSVCRT_bad_cast_dtor(_this);
472 if (flags & 1) MSVCRT_operator_delete(_this);
477 /******************************************************************
478 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
480 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor);
481 void * __stdcall MSVCRT_bad_cast_scalar_dtor(bad_cast * _this, unsigned int flags)
483 TRACE("(%p %x)\n", _this, flags);
484 MSVCRT_bad_cast_dtor(_this);
485 if (flags & 1) MSVCRT_operator_delete(_this);
489 /******************************************************************
490 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
492 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals);
493 int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
495 int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
496 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
500 /******************************************************************
501 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
503 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals);
504 int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
506 int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
507 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
511 /******************************************************************
512 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
514 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before);
515 int __stdcall MSVCRT_type_info_before(type_info * _this, const type_info * rhs)
517 int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
518 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
522 /******************************************************************
523 * ??1type_info@@UAE@XZ (MSVCRT.@)
525 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor);
526 void __stdcall MSVCRT_type_info_dtor(type_info * _this)
528 TRACE("(%p)\n", _this);
530 MSVCRT_free(_this->name);
533 /******************************************************************
534 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
536 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name);
537 const char * __stdcall MSVCRT_type_info_name(type_info * _this)
541 /* Create and set the demangled name */
542 char* name = MSVCRT___unDName(0, _this->mangled, 0,
543 (MSVCRT_malloc_func)MSVCRT_malloc,
544 (MSVCRT_free_func)MSVCRT_free, 0x2800);
548 unsigned int len = strlen(name);
550 /* It seems _unDName may leave blanks at the end of the demangled name */
551 if (name[len] == ' ')
558 /* Another thread set this member since we checked above - use it */
564 _munlock(_EXIT_LOCK2);
567 TRACE("(%p) returning %s\n", _this, _this->name);
571 /******************************************************************
572 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
574 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name);
575 const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
577 TRACE("(%p) returning %s\n", _this, _this->mangled);
578 return _this->mangled;
582 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor);
583 void * __stdcall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
585 TRACE("(%p %x)\n", _this, flags);
588 /* we have an array, with the number of elements stored before the first object */
589 int i, *ptr = (int *)_this - 1;
591 for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
592 MSVCRT_operator_delete(ptr);
596 MSVCRT_type_info_dtor(_this);
597 if (flags & 1) MSVCRT_operator_delete(_this);
604 const exception_vtable MSVCRT_exception_vtable =
606 __thiscall_MSVCRT_exception_vector_dtor,
607 __thiscall_MSVCRT_what_exception
610 const exception_vtable MSVCRT_bad_typeid_vtable =
612 __thiscall_MSVCRT_bad_typeid_vector_dtor,
613 __thiscall_MSVCRT_what_exception
616 const exception_vtable MSVCRT_bad_cast_vtable =
618 __thiscall_MSVCRT_bad_cast_vector_dtor,
619 __thiscall_MSVCRT_what_exception
622 const exception_vtable MSVCRT___non_rtti_object_vtable =
624 __thiscall_MSVCRT___non_rtti_object_vector_dtor,
625 __thiscall_MSVCRT_what_exception
628 static const exception_vtable MSVCRT_type_info_vtable =
630 __thiscall_MSVCRT_type_info_vector_dtor,
634 /* Static RTTI for exported objects */
636 static type_info exception_type_info =
638 (void*)&MSVCRT_type_info_vtable,
643 static const rtti_base_descriptor exception_rtti_base_descriptor =
645 &exception_type_info,
653 static const rtti_base_array exception_rtti_base_array =
656 &exception_rtti_base_descriptor,
662 static const rtti_object_hierachy exception_type_hierachy =
667 &exception_rtti_base_array
670 static const rtti_object_locator exception_rtti =
675 &exception_type_info,
676 &exception_type_hierachy
679 static const cxx_type_info exception_cxx_type_info =
682 &exception_type_info,
687 (cxx_copy_ctor)__thiscall_MSVCRT_exception_copy_ctor
690 static type_info bad_typeid_type_info =
692 (void*)&MSVCRT_type_info_vtable,
697 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
699 &bad_typeid_type_info,
707 static const rtti_base_array bad_typeid_rtti_base_array =
710 &bad_typeid_rtti_base_descriptor,
711 &exception_rtti_base_descriptor,
716 static const rtti_object_hierachy bad_typeid_type_hierachy =
721 &bad_typeid_rtti_base_array
724 static const rtti_object_locator bad_typeid_rtti =
729 &bad_typeid_type_info,
730 &bad_typeid_type_hierachy
733 static const cxx_type_info bad_typeid_cxx_type_info =
736 &bad_typeid_type_info,
741 (cxx_copy_ctor)__thiscall_MSVCRT_bad_typeid_copy_ctor
744 static type_info bad_cast_type_info =
746 (void*)&MSVCRT_type_info_vtable,
751 static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
761 static const rtti_base_array bad_cast_rtti_base_array =
764 &bad_cast_rtti_base_descriptor,
765 &exception_rtti_base_descriptor,
770 static const rtti_object_hierachy bad_cast_type_hierachy =
775 &bad_cast_rtti_base_array
778 static const rtti_object_locator bad_cast_rtti =
784 &bad_cast_type_hierachy
787 static const cxx_type_info bad_cast_cxx_type_info =
795 (cxx_copy_ctor)__thiscall_MSVCRT_bad_cast_copy_ctor
798 static type_info __non_rtti_object_type_info =
800 (void*)&MSVCRT_type_info_vtable,
802 ".?AV__non_rtti_object@@"
805 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
807 &__non_rtti_object_type_info,
815 static const rtti_base_array __non_rtti_object_rtti_base_array =
818 &__non_rtti_object_rtti_base_descriptor,
819 &bad_typeid_rtti_base_descriptor,
820 &exception_rtti_base_descriptor
824 static const rtti_object_hierachy __non_rtti_object_type_hierachy =
829 &__non_rtti_object_rtti_base_array
832 static const rtti_object_locator __non_rtti_object_rtti =
837 &__non_rtti_object_type_info,
838 &__non_rtti_object_type_hierachy
841 static const cxx_type_info __non_rtti_object_cxx_type_info =
844 &__non_rtti_object_type_info,
849 (cxx_copy_ctor)__thiscall_MSVCRT___non_rtti_object_copy_ctor
852 static type_info type_info_type_info =
854 (void*)&MSVCRT_type_info_vtable,
859 static const rtti_base_descriptor type_info_rtti_base_descriptor =
861 &type_info_type_info,
869 static const rtti_base_array type_info_rtti_base_array =
872 &type_info_rtti_base_descriptor,
878 static const rtti_object_hierachy type_info_type_hierachy =
883 &type_info_rtti_base_array
886 static const rtti_object_locator type_info_rtti =
891 &type_info_type_info,
892 &type_info_type_hierachy
896 * Exception RTTI for cpp objects
898 static const cxx_type_info_table bad_cast_type_info_table =
902 &__non_rtti_object_cxx_type_info,
903 &bad_typeid_cxx_type_info,
904 &exception_cxx_type_info
908 static const cxx_exception_type bad_cast_exception_type =
911 (void*)__thiscall_MSVCRT_bad_cast_dtor,
913 &bad_cast_type_info_table
916 static const cxx_type_info_table bad_typeid_type_info_table =
920 &bad_cast_cxx_type_info,
921 &exception_cxx_type_info,
926 static const cxx_exception_type bad_typeid_exception_type =
929 (void*)__thiscall_MSVCRT_bad_typeid_dtor,
931 &bad_cast_type_info_table
934 static const cxx_exception_type __non_rtti_object_exception_type =
937 (void*)__thiscall_MSVCRT___non_rtti_object_dtor,
939 &bad_typeid_type_info_table
942 #endif /* __i386__ */
945 /******************************************************************
946 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
948 * Install a handler to be called when terminate() is called.
951 * func [I] Handler function to install
954 * The previously installed handler function, if any.
956 terminate_function MSVCRT_set_terminate(terminate_function func)
958 MSVCRT_thread_data *data = msvcrt_get_thread_data();
959 terminate_function previous = data->terminate_handler;
960 TRACE("(%p) returning %p\n",func,previous);
961 data->terminate_handler = func;
965 /******************************************************************
966 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
968 * Install a handler to be called when unexpected() is called.
971 * func [I] Handler function to install
974 * The previously installed handler function, if any.
976 unexpected_function MSVCRT_set_unexpected(unexpected_function func)
978 MSVCRT_thread_data *data = msvcrt_get_thread_data();
979 unexpected_function previous = data->unexpected_handler;
980 TRACE("(%p) returning %p\n",func,previous);
981 data->unexpected_handler = func;
985 /******************************************************************
986 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
988 _se_translator_function MSVCRT__set_se_translator(_se_translator_function func)
990 MSVCRT_thread_data *data = msvcrt_get_thread_data();
991 _se_translator_function previous = data->se_translator;
992 TRACE("(%p) returning %p\n",func,previous);
993 data->se_translator = func;
997 /******************************************************************
998 * ?terminate@@YAXXZ (MSVCRT.@)
1000 * Default handler for an unhandled exception.
1006 * This function does not return. Either control resumes from any
1007 * handler installed by calling set_terminate(), or (by default) abort()
1010 void MSVCRT_terminate(void)
1012 MSVCRT_thread_data *data = msvcrt_get_thread_data();
1013 if (data->terminate_handler) data->terminate_handler();
1017 /******************************************************************
1018 * ?unexpected@@YAXXZ (MSVCRT.@)
1020 void MSVCRT_unexpected(void)
1022 MSVCRT_thread_data *data = msvcrt_get_thread_data();
1023 if (data->unexpected_handler) data->unexpected_handler();
1027 /* Get type info from an object (internal) */
1028 static const rtti_object_locator* RTTI_GetObjectLocator(type_info *cppobj)
1030 const rtti_object_locator *obj_locator = NULL;
1033 const exception_vtable* vtable = (const exception_vtable*)cppobj->vtable;
1035 /* Perhaps this is one of classes we export? */
1036 if (vtable == &MSVCRT_exception_vtable)
1038 TRACE("returning exception_rtti\n");
1039 return &exception_rtti;
1041 else if (vtable == &MSVCRT_bad_typeid_vtable)
1043 TRACE("returning bad_typeid_rtti\n");
1044 return &bad_typeid_rtti;
1046 else if (vtable == &MSVCRT_bad_cast_vtable)
1048 TRACE("returning bad_cast_rtti\n");
1049 return &bad_cast_rtti;
1051 else if (vtable == &MSVCRT___non_rtti_object_vtable)
1053 TRACE("returning __non_rtti_object_rtti\n");
1054 return &__non_rtti_object_rtti;
1056 else if (vtable == &MSVCRT_type_info_vtable)
1058 TRACE("returning type_info_rtti\n");
1059 return &type_info_rtti;
1063 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
1064 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
1065 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
1067 obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
1068 TRACE("returning type_info from vtable (%p)\n", obj_locator);
1074 /******************************************************************
1075 * __RTtypeid (MSVCRT.@)
1077 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1080 * cppobj [I] C++ object to get type information for.
1083 * Success: A type_info object describing cppobj.
1084 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
1085 * exception is thrown. If cppobj is NULL, a bad_typeid exception
1086 * is thrown. In either case, this function does not return.
1089 * This function is usually called by compiler generated code as a result
1090 * of using one of the C++ dynamic cast statements.
1092 type_info* MSVCRT___RTtypeid(type_info *cppobj)
1094 const rtti_object_locator *obj_locator = RTTI_GetObjectLocator(cppobj);
1099 static const char* szNullPtr = "Attempted a typeid of NULL pointer!";
1100 static const char* szBadPtr = "Bad read pointer - no RTTI data!";
1101 const cxx_exception_type *e_type;
1104 /* Throw a bad_typeid or __non_rtti_object exception */
1107 EXCEPTION_ctor(&e, &szNullPtr);
1108 e.vtable = &MSVCRT_bad_typeid_vtable;
1109 e_type = &bad_typeid_exception_type;
1113 EXCEPTION_ctor(&e, &szBadPtr);
1114 e.vtable = &MSVCRT___non_rtti_object_vtable;
1115 e_type = &__non_rtti_object_exception_type;
1118 _CxxThrowException(&e, e_type);
1121 return obj_locator->type_descriptor;
1127 /******************************************************************
1128 * __RTDynamicCast (MSVCRT.@)
1130 * Dynamically cast a C++ object to one of its base classes.
1133 * cppobj [I] Any C++ object to cast
1134 * unknown [I] Reserved, set to 0
1135 * src [I] type_info object describing cppobj
1136 * dst [I] type_info object describing the base class to cast to
1137 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
1140 * Success: The address of cppobj, cast to the object described by dst.
1141 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
1142 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
1143 * is thrown and this function does not return.
1146 * This function is usually called by compiler generated code as a result
1147 * of using one of the C++ dynamic cast statements.
1149 void* MSVCRT___RTDynamicCast(type_info *cppobj, int unknown,
1150 type_info *src, type_info *dst,
1153 const rtti_object_locator *obj_locator;
1155 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
1156 TRACE("(%p,%d,%p,%p,%d)\n", cppobj, unknown, src, dst, do_throw);
1159 obj_locator= RTTI_GetObjectLocator(cppobj);
1161 FIXME("Unknown parameter is non-zero: please report\n");
1163 /* To cast an object at runtime:
1164 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
1165 * 2.Search for the destination type in the class hierarchy
1166 * 3.If destination type is found, return base object address + dest offset
1167 * Otherwise, fail the cast
1172 const rtti_object_hierachy *obj_bases = obj_locator->type_hierachy;
1173 const rtti_base_descriptor **base_desc = obj_bases->base_classes->bases;
1174 int src_offset = obj_locator->base_class_offset, dst_offset = -1;
1176 while (count < obj_bases->array_len)
1178 const type_info *typ = (*base_desc)->type_descriptor;
1180 if (!strcmp(typ->mangled, dst->mangled))
1182 dst_offset = (*base_desc)->base_class_offset;
1188 if (dst_offset >= 0)
1189 return (void*)((unsigned long)cppobj - src_offset + dst_offset);
1193 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
1194 * to a reference, since references cannot be NULL.
1198 static const char* exception_text = "Bad dynamic_cast!";
1201 /* Throw a bad_cast exception */
1202 EXCEPTION_ctor(&e, &exception_text);
1203 e.vtable = &MSVCRT_bad_cast_vtable;
1204 _CxxThrowException(&e, &bad_cast_exception_type);
1212 /******************************************************************
1213 * __RTCastToVoid (MSVCRT.@)
1215 * Dynamically cast a C++ object to a void*.
1218 * cppobj [I] The C++ object to cast
1221 * Success: The base address of the object as a void*.
1222 * Failure: NULL, if cppobj is NULL or has no RTTI.
1225 * This function is usually called by compiler generated code as a result
1226 * of using one of the C++ dynamic cast statements.
1228 void* MSVCRT___RTCastToVoid(type_info *cppobj)
1230 const rtti_object_locator *obj_locator = RTTI_GetObjectLocator(cppobj);
1232 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
1233 TRACE("(%p)\n", cppobj);
1235 /* Casts to void* simply cast to the base object */
1237 return (void*)((unsigned long)cppobj - obj_locator->base_class_offset);