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"
35 #include "cppexcept.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
41 * exception object: base for exception, bad_cast, bad_typeid, __non_rtti_object
45 void* pfn_vector_dtor;
49 typedef struct __exception
51 const exception_vtable *vtable;
52 char *name; /* Name of this exception, always a new copy for each object */
53 int do_free; /* Whether to free 'name' in our dtor */
56 typedef exception bad_cast;
57 typedef exception bad_typeid;
58 typedef exception __non_rtti_object;
60 typedef struct _rtti_base_descriptor
62 type_info *type_descriptor;
64 this_ptr_offsets offsets; /* offsets for computing the this pointer */
65 unsigned int attributes;
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_hierarchy
75 unsigned int signature;
76 unsigned int attributes;
77 int array_len; /* Size of the array pointed to by 'base_classes' */
78 const rtti_base_array *base_classes;
79 } rtti_object_hierarchy;
81 typedef struct _rtti_object_locator
83 unsigned int signature;
84 int base_class_offset;
86 type_info *type_descriptor;
87 const rtti_object_hierarchy *type_hierarchy;
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 static void dump_obj_locator( const rtti_object_locator *ptr )
110 const rtti_object_hierarchy *h = ptr->type_hierarchy;
112 TRACE( "%p: sig=%08x base_offset=%08x flags=%08x type=%p %s hierarchy=%p\n",
113 ptr, ptr->signature, ptr->base_class_offset, ptr->flags,
114 ptr->type_descriptor, dbgstr_type_info(ptr->type_descriptor), ptr->type_hierarchy );
115 TRACE( " hierarchy: sig=%08x attr=%08x len=%d base classes=%p\n",
116 h->signature, h->attributes, h->array_len, h->base_classes );
117 for (i = 0; i < h->array_len; i++)
119 TRACE( " base class %p: num %d off %d,%d,%d attr %08x type %p %s\n",
120 h->base_classes->bases[i],
121 h->base_classes->bases[i]->num_base_classes,
122 h->base_classes->bases[i]->offsets.this_offset,
123 h->base_classes->bases[i]->offsets.vbase_descr,
124 h->base_classes->bases[i]->offsets.vbase_offset,
125 h->base_classes->bases[i]->attributes,
126 h->base_classes->bases[i]->type_descriptor,
127 dbgstr_type_info(h->base_classes->bases[i]->type_descriptor) );
131 /* Internal common ctor for exception */
132 static void WINAPI EXCEPTION_ctor(exception *_this, const char** name)
134 _this->vtable = &MSVCRT_exception_vtable;
137 size_t name_len = strlen(*name) + 1;
138 _this->name = MSVCRT_malloc(name_len);
139 memcpy(_this->name, *name, name_len);
140 _this->do_free = TRUE;
145 _this->do_free = FALSE;
149 /******************************************************************
150 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
152 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor);
153 exception * __stdcall MSVCRT_exception_ctor(exception * _this, const char ** name)
155 TRACE("(%p,%s)\n", _this, *name);
156 EXCEPTION_ctor(_this, name);
160 /******************************************************************
161 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
163 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor);
164 exception * __stdcall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
166 TRACE("(%p,%p)\n", _this, rhs);
170 _this->vtable = &MSVCRT_exception_vtable;
171 _this->name = rhs->name;
172 _this->do_free = FALSE;
175 EXCEPTION_ctor(_this, (const char**)&rhs->name);
176 TRACE("name = %s\n", _this->name);
180 /******************************************************************
181 * ??0exception@@QAE@XZ (MSVCRT.@)
183 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor);
184 exception * __stdcall MSVCRT_exception_default_ctor(exception * _this)
186 static const char* empty = NULL;
188 TRACE("(%p)\n", _this);
189 EXCEPTION_ctor(_this, &empty);
193 /******************************************************************
194 * ??1exception@@UAE@XZ (MSVCRT.@)
196 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor);
197 void __stdcall MSVCRT_exception_dtor(exception * _this)
199 TRACE("(%p)\n", _this);
200 _this->vtable = &MSVCRT_exception_vtable;
201 if (_this->do_free) MSVCRT_free(_this->name);
204 /******************************************************************
205 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
207 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals);
208 exception * __stdcall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
210 TRACE("(%p %p)\n", _this, rhs);
213 MSVCRT_exception_dtor(_this);
214 MSVCRT_exception_copy_ctor(_this, rhs);
216 TRACE("name = %s\n", _this->name);
220 /******************************************************************
221 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
223 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor);
224 void * __stdcall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
226 TRACE("(%p %x)\n", _this, flags);
229 /* we have an array, with the number of elements stored before the first object */
230 int i, *ptr = (int *)_this - 1;
232 for (i = *ptr - 1; i >= 0; i--) MSVCRT_exception_dtor(_this + i);
233 MSVCRT_operator_delete(ptr);
237 MSVCRT_exception_dtor(_this);
238 if (flags & 1) MSVCRT_operator_delete(_this);
243 /******************************************************************
244 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
246 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor);
247 void * __stdcall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
249 TRACE("(%p %x)\n", _this, flags);
250 MSVCRT_exception_dtor(_this);
251 if (flags & 1) MSVCRT_operator_delete(_this);
255 /******************************************************************
256 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
258 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception);
259 const char * __stdcall MSVCRT_what_exception(exception * _this)
261 TRACE("(%p) returning %s\n", _this, _this->name);
262 return _this->name ? _this->name : "Unknown exception";
265 /******************************************************************
266 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
268 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor);
269 bad_typeid * __stdcall MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
271 TRACE("(%p %p)\n", _this, rhs);
272 MSVCRT_exception_copy_ctor(_this, rhs);
273 _this->vtable = &MSVCRT_bad_typeid_vtable;
277 /******************************************************************
278 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
280 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor);
281 bad_typeid * __stdcall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
283 TRACE("(%p %s)\n", _this, name);
284 EXCEPTION_ctor(_this, &name);
285 _this->vtable = &MSVCRT_bad_typeid_vtable;
289 /******************************************************************
290 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
292 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor);
293 void __stdcall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
295 TRACE("(%p)\n", _this);
296 MSVCRT_exception_dtor(_this);
299 /******************************************************************
300 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
302 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals);
303 bad_typeid * __stdcall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
305 TRACE("(%p %p)\n", _this, rhs);
306 MSVCRT_exception_opequals(_this, rhs);
310 /******************************************************************
311 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
313 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor);
314 void * __stdcall MSVCRT_bad_typeid_vector_dtor(bad_typeid * _this, unsigned int flags)
316 TRACE("(%p %x)\n", _this, flags);
319 /* we have an array, with the number of elements stored before the first object */
320 int i, *ptr = (int *)_this - 1;
322 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_typeid_dtor(_this + i);
323 MSVCRT_operator_delete(ptr);
327 MSVCRT_bad_typeid_dtor(_this);
328 if (flags & 1) MSVCRT_operator_delete(_this);
333 /******************************************************************
334 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
336 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor);
337 void * __stdcall MSVCRT_bad_typeid_scalar_dtor(bad_typeid * _this, unsigned int flags)
339 TRACE("(%p %x)\n", _this, flags);
340 MSVCRT_bad_typeid_dtor(_this);
341 if (flags & 1) MSVCRT_operator_delete(_this);
345 /******************************************************************
346 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
348 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor);
349 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
350 const __non_rtti_object * rhs)
352 TRACE("(%p %p)\n", _this, rhs);
353 MSVCRT_bad_typeid_copy_ctor(_this, rhs);
354 _this->vtable = &MSVCRT___non_rtti_object_vtable;
358 /******************************************************************
359 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
361 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor);
362 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
365 TRACE("(%p %s)\n", _this, name);
366 EXCEPTION_ctor(_this, &name);
367 _this->vtable = &MSVCRT___non_rtti_object_vtable;
371 /******************************************************************
372 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
374 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor);
375 void __stdcall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
377 TRACE("(%p)\n", _this);
378 MSVCRT_bad_typeid_dtor(_this);
381 /******************************************************************
382 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
384 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals);
385 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
386 const __non_rtti_object *rhs)
388 TRACE("(%p %p)\n", _this, rhs);
389 MSVCRT_bad_typeid_opequals(_this, rhs);
393 /******************************************************************
394 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
396 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor);
397 void * __stdcall MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object * _this, unsigned int flags)
399 TRACE("(%p %x)\n", _this, flags);
402 /* we have an array, with the number of elements stored before the first object */
403 int i, *ptr = (int *)_this - 1;
405 for (i = *ptr - 1; i >= 0; i--) MSVCRT___non_rtti_object_dtor(_this + i);
406 MSVCRT_operator_delete(ptr);
410 MSVCRT___non_rtti_object_dtor(_this);
411 if (flags & 1) MSVCRT_operator_delete(_this);
416 /******************************************************************
417 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
419 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor);
420 void * __stdcall MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object * _this, unsigned int flags)
422 TRACE("(%p %x)\n", _this, flags);
423 MSVCRT___non_rtti_object_dtor(_this);
424 if (flags & 1) MSVCRT_operator_delete(_this);
428 /******************************************************************
429 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
431 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor);
432 bad_cast * __stdcall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
434 TRACE("(%p %s)\n", _this, *name);
435 EXCEPTION_ctor(_this, name);
436 _this->vtable = &MSVCRT_bad_cast_vtable;
440 /******************************************************************
441 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
443 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor);
444 bad_cast * __stdcall MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
446 TRACE("(%p %p)\n", _this, rhs);
447 MSVCRT_exception_copy_ctor(_this, rhs);
448 _this->vtable = &MSVCRT_bad_cast_vtable;
452 /******************************************************************
453 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
455 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor);
456 void __stdcall MSVCRT_bad_cast_dtor(bad_cast * _this)
458 TRACE("(%p)\n", _this);
459 MSVCRT_exception_dtor(_this);
462 /******************************************************************
463 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
465 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals);
466 bad_cast * __stdcall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
468 TRACE("(%p %p)\n", _this, rhs);
469 MSVCRT_exception_opequals(_this, rhs);
473 /******************************************************************
474 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
476 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor);
477 void * __stdcall MSVCRT_bad_cast_vector_dtor(bad_cast * _this, unsigned int flags)
479 TRACE("(%p %x)\n", _this, flags);
482 /* we have an array, with the number of elements stored before the first object */
483 int i, *ptr = (int *)_this - 1;
485 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_cast_dtor(_this + i);
486 MSVCRT_operator_delete(ptr);
490 MSVCRT_bad_cast_dtor(_this);
491 if (flags & 1) MSVCRT_operator_delete(_this);
496 /******************************************************************
497 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
499 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor);
500 void * __stdcall MSVCRT_bad_cast_scalar_dtor(bad_cast * _this, unsigned int flags)
502 TRACE("(%p %x)\n", _this, flags);
503 MSVCRT_bad_cast_dtor(_this);
504 if (flags & 1) MSVCRT_operator_delete(_this);
508 /******************************************************************
509 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
511 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals);
512 int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
514 int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
515 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
519 /******************************************************************
520 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
522 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals);
523 int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
525 int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
526 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
530 /******************************************************************
531 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
533 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before);
534 int __stdcall MSVCRT_type_info_before(type_info * _this, const type_info * rhs)
536 int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
537 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
541 /******************************************************************
542 * ??1type_info@@UAE@XZ (MSVCRT.@)
544 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor);
545 void __stdcall MSVCRT_type_info_dtor(type_info * _this)
547 TRACE("(%p)\n", _this);
549 MSVCRT_free(_this->name);
552 /******************************************************************
553 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
555 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name);
556 const char * __stdcall MSVCRT_type_info_name(type_info * _this)
560 /* Create and set the demangled name */
561 char* name = __unDName(0, _this->mangled, 0,
562 (malloc_func_t)MSVCRT_malloc,
563 (free_func_t)MSVCRT_free, 0x2800);
567 unsigned int len = strlen(name);
569 /* It seems _unDName may leave blanks at the end of the demangled name */
570 if (name[len] == ' ')
577 /* Another thread set this member since we checked above - use it */
583 _munlock(_EXIT_LOCK2);
586 TRACE("(%p) returning %s\n", _this, _this->name);
590 /******************************************************************
591 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
593 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name);
594 const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
596 TRACE("(%p) returning %s\n", _this, _this->mangled);
597 return _this->mangled;
601 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor);
602 void * __stdcall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
604 TRACE("(%p %x)\n", _this, flags);
607 /* we have an array, with the number of elements stored before the first object */
608 int i, *ptr = (int *)_this - 1;
610 for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
611 MSVCRT_operator_delete(ptr);
615 MSVCRT_type_info_dtor(_this);
616 if (flags & 1) MSVCRT_operator_delete(_this);
623 const exception_vtable MSVCRT_exception_vtable =
625 __thiscall_MSVCRT_exception_vector_dtor,
626 __thiscall_MSVCRT_what_exception
629 const exception_vtable MSVCRT_bad_typeid_vtable =
631 __thiscall_MSVCRT_bad_typeid_vector_dtor,
632 __thiscall_MSVCRT_what_exception
635 const exception_vtable MSVCRT_bad_cast_vtable =
637 __thiscall_MSVCRT_bad_cast_vector_dtor,
638 __thiscall_MSVCRT_what_exception
641 const exception_vtable MSVCRT___non_rtti_object_vtable =
643 __thiscall_MSVCRT___non_rtti_object_vector_dtor,
644 __thiscall_MSVCRT_what_exception
647 static const exception_vtable MSVCRT_type_info_vtable =
649 __thiscall_MSVCRT_type_info_vector_dtor,
653 /* Static RTTI for exported objects */
655 static type_info exception_type_info =
657 (void*)&MSVCRT_type_info_vtable,
662 static const rtti_base_descriptor exception_rtti_base_descriptor =
664 &exception_type_info,
670 static const rtti_base_array exception_rtti_base_array =
673 &exception_rtti_base_descriptor,
679 static const rtti_object_hierarchy exception_type_hierarchy =
684 &exception_rtti_base_array
687 static const rtti_object_locator exception_rtti =
692 &exception_type_info,
693 &exception_type_hierarchy
696 static const cxx_type_info exception_cxx_type_info =
699 &exception_type_info,
702 (cxx_copy_ctor)__thiscall_MSVCRT_exception_copy_ctor
705 static type_info bad_typeid_type_info =
707 (void*)&MSVCRT_type_info_vtable,
712 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
714 &bad_typeid_type_info,
720 static const rtti_base_array bad_typeid_rtti_base_array =
723 &bad_typeid_rtti_base_descriptor,
724 &exception_rtti_base_descriptor,
729 static const rtti_object_hierarchy bad_typeid_type_hierarchy =
734 &bad_typeid_rtti_base_array
737 static const rtti_object_locator bad_typeid_rtti =
742 &bad_typeid_type_info,
743 &bad_typeid_type_hierarchy
746 static const cxx_type_info bad_typeid_cxx_type_info =
749 &bad_typeid_type_info,
752 (cxx_copy_ctor)__thiscall_MSVCRT_bad_typeid_copy_ctor
755 static type_info bad_cast_type_info =
757 (void*)&MSVCRT_type_info_vtable,
762 static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
770 static const rtti_base_array bad_cast_rtti_base_array =
773 &bad_cast_rtti_base_descriptor,
774 &exception_rtti_base_descriptor,
779 static const rtti_object_hierarchy bad_cast_type_hierarchy =
784 &bad_cast_rtti_base_array
787 static const rtti_object_locator bad_cast_rtti =
793 &bad_cast_type_hierarchy
796 static const cxx_type_info bad_cast_cxx_type_info =
802 (cxx_copy_ctor)__thiscall_MSVCRT_bad_cast_copy_ctor
805 static type_info __non_rtti_object_type_info =
807 (void*)&MSVCRT_type_info_vtable,
809 ".?AV__non_rtti_object@@"
812 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
814 &__non_rtti_object_type_info,
820 static const rtti_base_array __non_rtti_object_rtti_base_array =
823 &__non_rtti_object_rtti_base_descriptor,
824 &bad_typeid_rtti_base_descriptor,
825 &exception_rtti_base_descriptor
829 static const rtti_object_hierarchy __non_rtti_object_type_hierarchy =
834 &__non_rtti_object_rtti_base_array
837 static const rtti_object_locator __non_rtti_object_rtti =
842 &__non_rtti_object_type_info,
843 &__non_rtti_object_type_hierarchy
846 static const cxx_type_info __non_rtti_object_cxx_type_info =
849 &__non_rtti_object_type_info,
852 (cxx_copy_ctor)__thiscall_MSVCRT___non_rtti_object_copy_ctor
855 static type_info type_info_type_info =
857 (void*)&MSVCRT_type_info_vtable,
862 static const rtti_base_descriptor type_info_rtti_base_descriptor =
864 &type_info_type_info,
870 static const rtti_base_array type_info_rtti_base_array =
873 &type_info_rtti_base_descriptor,
879 static const rtti_object_hierarchy type_info_type_hierarchy =
884 &type_info_rtti_base_array
887 static const rtti_object_locator type_info_rtti =
892 &type_info_type_info,
893 &type_info_type_hierarchy
897 * Exception RTTI for cpp objects
899 static const cxx_type_info_table bad_cast_type_info_table =
903 &__non_rtti_object_cxx_type_info,
904 &bad_typeid_cxx_type_info,
905 &exception_cxx_type_info
909 static const cxx_exception_type bad_cast_exception_type =
912 (void*)__thiscall_MSVCRT_bad_cast_dtor,
914 &bad_cast_type_info_table
917 static const cxx_type_info_table bad_typeid_type_info_table =
921 &bad_cast_cxx_type_info,
922 &exception_cxx_type_info,
927 static const cxx_exception_type bad_typeid_exception_type =
930 (void*)__thiscall_MSVCRT_bad_typeid_dtor,
932 &bad_cast_type_info_table
935 static const cxx_exception_type __non_rtti_object_exception_type =
938 (void*)__thiscall_MSVCRT___non_rtti_object_dtor,
940 &bad_typeid_type_info_table
943 #endif /* __i386__ */
946 /******************************************************************
947 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
949 * Install a handler to be called when terminate() is called.
952 * func [I] Handler function to install
955 * The previously installed handler function, if any.
957 MSVCRT_terminate_function MSVCRT_set_terminate(MSVCRT_terminate_function func)
959 thread_data_t *data = msvcrt_get_thread_data();
960 MSVCRT_terminate_function previous = data->terminate_handler;
961 TRACE("(%p) returning %p\n",func,previous);
962 data->terminate_handler = func;
966 /******************************************************************
967 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
969 * Install a handler to be called when unexpected() is called.
972 * func [I] Handler function to install
975 * The previously installed handler function, if any.
977 MSVCRT_unexpected_function MSVCRT_set_unexpected(MSVCRT_unexpected_function func)
979 thread_data_t *data = msvcrt_get_thread_data();
980 MSVCRT_unexpected_function previous = data->unexpected_handler;
981 TRACE("(%p) returning %p\n",func,previous);
982 data->unexpected_handler = func;
986 /******************************************************************
987 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
989 MSVCRT__se_translator_function MSVCRT__set_se_translator(MSVCRT__se_translator_function func)
991 thread_data_t *data = msvcrt_get_thread_data();
992 MSVCRT__se_translator_function previous = data->se_translator;
993 TRACE("(%p) returning %p\n",func,previous);
994 data->se_translator = func;
998 /******************************************************************
999 * ?terminate@@YAXXZ (MSVCRT.@)
1001 * Default handler for an unhandled exception.
1007 * This function does not return. Either control resumes from any
1008 * handler installed by calling set_terminate(), or (by default) abort()
1011 void MSVCRT_terminate(void)
1013 thread_data_t *data = msvcrt_get_thread_data();
1014 if (data->terminate_handler) data->terminate_handler();
1018 /******************************************************************
1019 * ?unexpected@@YAXXZ (MSVCRT.@)
1021 void MSVCRT_unexpected(void)
1023 thread_data_t *data = msvcrt_get_thread_data();
1024 if (data->unexpected_handler) data->unexpected_handler();
1028 /* Get type info from an object (internal) */
1029 static const rtti_object_locator* RTTI_GetObjectLocator(type_info *cppobj)
1031 const rtti_object_locator *obj_locator = NULL;
1034 const exception_vtable* vtable = (const exception_vtable*)cppobj->vtable;
1036 /* Perhaps this is one of classes we export? */
1037 if (vtable == &MSVCRT_exception_vtable)
1039 TRACE("returning exception_rtti\n");
1040 return &exception_rtti;
1042 else if (vtable == &MSVCRT_bad_typeid_vtable)
1044 TRACE("returning bad_typeid_rtti\n");
1045 return &bad_typeid_rtti;
1047 else if (vtable == &MSVCRT_bad_cast_vtable)
1049 TRACE("returning bad_cast_rtti\n");
1050 return &bad_cast_rtti;
1052 else if (vtable == &MSVCRT___non_rtti_object_vtable)
1054 TRACE("returning __non_rtti_object_rtti\n");
1055 return &__non_rtti_object_rtti;
1057 else if (vtable == &MSVCRT_type_info_vtable)
1059 TRACE("returning type_info_rtti\n");
1060 return &type_info_rtti;
1064 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
1065 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
1066 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
1068 obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
1069 TRACE("returning type_info from vtable (%p)\n", obj_locator);
1075 /******************************************************************
1076 * __RTtypeid (MSVCRT.@)
1078 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1081 * cppobj [I] C++ object to get type information for.
1084 * Success: A type_info object describing cppobj.
1085 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
1086 * exception is thrown. If cppobj is NULL, a bad_typeid exception
1087 * is thrown. In either case, this function does not return.
1090 * This function is usually called by compiler generated code as a result
1091 * of using one of the C++ dynamic cast statements.
1093 type_info* MSVCRT___RTtypeid(type_info *cppobj)
1095 const rtti_object_locator *obj_locator = RTTI_GetObjectLocator(cppobj);
1100 static const char* szNullPtr = "Attempted a typeid of NULL pointer!";
1101 static const char* szBadPtr = "Bad read pointer - no RTTI data!";
1102 const cxx_exception_type *e_type;
1105 /* Throw a bad_typeid or __non_rtti_object exception */
1108 EXCEPTION_ctor(&e, &szNullPtr);
1109 e.vtable = &MSVCRT_bad_typeid_vtable;
1110 e_type = &bad_typeid_exception_type;
1114 EXCEPTION_ctor(&e, &szBadPtr);
1115 e.vtable = &MSVCRT___non_rtti_object_vtable;
1116 e_type = &__non_rtti_object_exception_type;
1119 _CxxThrowException(&e, e_type);
1122 return obj_locator->type_descriptor;
1128 /******************************************************************
1129 * __RTDynamicCast (MSVCRT.@)
1131 * Dynamically cast a C++ object to one of its base classes.
1134 * cppobj [I] Any C++ object to cast
1135 * unknown [I] Reserved, set to 0
1136 * src [I] type_info object describing cppobj
1137 * dst [I] type_info object describing the base class to cast to
1138 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
1141 * Success: The address of cppobj, cast to the object described by dst.
1142 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
1143 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
1144 * is thrown and this function does not return.
1147 * This function is usually called by compiler generated code as a result
1148 * of using one of the C++ dynamic cast statements.
1150 void* MSVCRT___RTDynamicCast(type_info *cppobj, int unknown,
1151 type_info *src, type_info *dst,
1154 const rtti_object_locator *obj_locator;
1156 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
1157 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
1158 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
1161 obj_locator= RTTI_GetObjectLocator(cppobj);
1163 FIXME("Unknown parameter is non-zero: please report\n");
1165 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
1167 /* To cast an object at runtime:
1168 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
1169 * 2.Search for the destination type in the class hierarchy
1170 * 3.If destination type is found, return base object address + dest offset
1171 * Otherwise, fail the cast
1176 const rtti_object_hierarchy *obj_bases = obj_locator->type_hierarchy;
1177 const rtti_base_descriptor **base_desc = obj_bases->base_classes->bases;
1178 int src_offset = obj_locator->base_class_offset, dst_offset = -1;
1180 while (count < obj_bases->array_len)
1182 const type_info *typ = (*base_desc)->type_descriptor;
1184 if (!strcmp(typ->mangled, dst->mangled))
1186 /* compute the correct this pointer for that base class */
1187 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
1188 return get_this_pointer( &(*base_desc)->offsets, this_ptr );
1193 if (dst_offset >= 0)
1194 return (void*)((unsigned long)cppobj - src_offset + dst_offset);
1198 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
1199 * to a reference, since references cannot be NULL.
1203 static const char* exception_text = "Bad dynamic_cast!";
1206 /* Throw a bad_cast exception */
1207 EXCEPTION_ctor(&e, &exception_text);
1208 e.vtable = &MSVCRT_bad_cast_vtable;
1209 _CxxThrowException(&e, &bad_cast_exception_type);
1217 /******************************************************************
1218 * __RTCastToVoid (MSVCRT.@)
1220 * Dynamically cast a C++ object to a void*.
1223 * cppobj [I] The C++ object to cast
1226 * Success: The base address of the object as a void*.
1227 * Failure: NULL, if cppobj is NULL or has no RTTI.
1230 * This function is usually called by compiler generated code as a result
1231 * of using one of the C++ dynamic cast statements.
1233 void* MSVCRT___RTCastToVoid(type_info *cppobj)
1235 const rtti_object_locator *obj_locator = RTTI_GetObjectLocator(cppobj);
1237 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
1238 TRACE("(%p)\n", cppobj);
1240 /* Casts to void* simply cast to the base object */
1242 return (void*)((unsigned long)cppobj - obj_locator->base_class_offset);