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@ABQBDH@Z (MSVCRT.@)
165 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor_noalloc,12)
166 exception * __stdcall MSVCRT_exception_ctor_noalloc(exception * _this, char ** name, int noalloc)
168 TRACE("(%p,%s)\n", _this, *name);
169 _this->vtable = &MSVCRT_exception_vtable;
171 _this->do_free = FALSE;
175 /******************************************************************
176 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
178 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor,8)
179 exception * __stdcall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
181 TRACE("(%p,%p)\n", _this, rhs);
185 _this->vtable = &MSVCRT_exception_vtable;
186 _this->name = rhs->name;
187 _this->do_free = FALSE;
190 EXCEPTION_ctor(_this, (const char**)&rhs->name);
191 TRACE("name = %s\n", _this->name);
195 /******************************************************************
196 * ??0exception@@QAE@XZ (MSVCRT.@)
198 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor,4)
199 exception * __stdcall MSVCRT_exception_default_ctor(exception * _this)
201 static const char* empty = NULL;
203 TRACE("(%p)\n", _this);
204 EXCEPTION_ctor(_this, &empty);
208 /******************************************************************
209 * ??1exception@@UAE@XZ (MSVCRT.@)
211 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor,4)
212 void __stdcall MSVCRT_exception_dtor(exception * _this)
214 TRACE("(%p)\n", _this);
215 _this->vtable = &MSVCRT_exception_vtable;
216 if (_this->do_free) MSVCRT_free(_this->name);
219 /******************************************************************
220 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
222 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals,8)
223 exception * __stdcall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
225 TRACE("(%p %p)\n", _this, rhs);
228 MSVCRT_exception_dtor(_this);
229 MSVCRT_exception_copy_ctor(_this, rhs);
231 TRACE("name = %s\n", _this->name);
235 /******************************************************************
236 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
238 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor,8)
239 void * __stdcall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
241 TRACE("(%p %x)\n", _this, flags);
244 /* we have an array, with the number of elements stored before the first object */
245 int i, *ptr = (int *)_this - 1;
247 for (i = *ptr - 1; i >= 0; i--) MSVCRT_exception_dtor(_this + i);
248 MSVCRT_operator_delete(ptr);
252 MSVCRT_exception_dtor(_this);
253 if (flags & 1) MSVCRT_operator_delete(_this);
258 /******************************************************************
259 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
261 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor,8)
262 void * __stdcall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
264 TRACE("(%p %x)\n", _this, flags);
265 MSVCRT_exception_dtor(_this);
266 if (flags & 1) MSVCRT_operator_delete(_this);
270 /******************************************************************
271 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
273 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception,4)
274 const char * __stdcall MSVCRT_what_exception(exception * _this)
276 TRACE("(%p) returning %s\n", _this, _this->name);
277 return _this->name ? _this->name : "Unknown exception";
280 /******************************************************************
281 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
283 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor,8)
284 bad_typeid * __stdcall MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
286 TRACE("(%p %p)\n", _this, rhs);
287 MSVCRT_exception_copy_ctor(_this, rhs);
288 _this->vtable = &MSVCRT_bad_typeid_vtable;
292 /******************************************************************
293 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
295 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor,8)
296 bad_typeid * __stdcall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
298 TRACE("(%p %s)\n", _this, name);
299 EXCEPTION_ctor(_this, &name);
300 _this->vtable = &MSVCRT_bad_typeid_vtable;
304 /******************************************************************
305 * ??_Fbad_typeid@@QAEXXZ (MSVCRT.@)
307 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_default_ctor,4)
308 bad_typeid * __stdcall MSVCRT_bad_typeid_default_ctor(bad_typeid * _this)
310 return MSVCRT_bad_typeid_ctor( _this, "bad typeid" );
313 /******************************************************************
314 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
316 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor,4)
317 void __stdcall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
319 TRACE("(%p)\n", _this);
320 MSVCRT_exception_dtor(_this);
323 /******************************************************************
324 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
326 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals,8)
327 bad_typeid * __stdcall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
329 TRACE("(%p %p)\n", _this, rhs);
330 MSVCRT_exception_opequals(_this, rhs);
334 /******************************************************************
335 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
337 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor,8)
338 void * __stdcall MSVCRT_bad_typeid_vector_dtor(bad_typeid * _this, unsigned int flags)
340 TRACE("(%p %x)\n", _this, flags);
343 /* we have an array, with the number of elements stored before the first object */
344 int i, *ptr = (int *)_this - 1;
346 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_typeid_dtor(_this + i);
347 MSVCRT_operator_delete(ptr);
351 MSVCRT_bad_typeid_dtor(_this);
352 if (flags & 1) MSVCRT_operator_delete(_this);
357 /******************************************************************
358 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
360 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor,8)
361 void * __stdcall MSVCRT_bad_typeid_scalar_dtor(bad_typeid * _this, unsigned int flags)
363 TRACE("(%p %x)\n", _this, flags);
364 MSVCRT_bad_typeid_dtor(_this);
365 if (flags & 1) MSVCRT_operator_delete(_this);
369 /******************************************************************
370 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
372 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor,8)
373 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
374 const __non_rtti_object * rhs)
376 TRACE("(%p %p)\n", _this, rhs);
377 MSVCRT_bad_typeid_copy_ctor(_this, rhs);
378 _this->vtable = &MSVCRT___non_rtti_object_vtable;
382 /******************************************************************
383 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
385 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor,8)
386 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
389 TRACE("(%p %s)\n", _this, name);
390 EXCEPTION_ctor(_this, &name);
391 _this->vtable = &MSVCRT___non_rtti_object_vtable;
395 /******************************************************************
396 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
398 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor,4)
399 void __stdcall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
401 TRACE("(%p)\n", _this);
402 MSVCRT_bad_typeid_dtor(_this);
405 /******************************************************************
406 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
408 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals,8)
409 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
410 const __non_rtti_object *rhs)
412 TRACE("(%p %p)\n", _this, rhs);
413 MSVCRT_bad_typeid_opequals(_this, rhs);
417 /******************************************************************
418 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
420 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor,8)
421 void * __stdcall MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object * _this, unsigned int flags)
423 TRACE("(%p %x)\n", _this, flags);
426 /* we have an array, with the number of elements stored before the first object */
427 int i, *ptr = (int *)_this - 1;
429 for (i = *ptr - 1; i >= 0; i--) MSVCRT___non_rtti_object_dtor(_this + i);
430 MSVCRT_operator_delete(ptr);
434 MSVCRT___non_rtti_object_dtor(_this);
435 if (flags & 1) MSVCRT_operator_delete(_this);
440 /******************************************************************
441 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
443 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor,8)
444 void * __stdcall MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object * _this, unsigned int flags)
446 TRACE("(%p %x)\n", _this, flags);
447 MSVCRT___non_rtti_object_dtor(_this);
448 if (flags & 1) MSVCRT_operator_delete(_this);
452 /******************************************************************
453 * ??0bad_cast@@AAE@PBQBD@Z (MSVCRT.@)
454 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
456 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor,8)
457 bad_cast * __stdcall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
459 TRACE("(%p %s)\n", _this, *name);
460 EXCEPTION_ctor(_this, name);
461 _this->vtable = &MSVCRT_bad_cast_vtable;
465 /******************************************************************
466 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
468 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor,8)
469 bad_cast * __stdcall MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
471 TRACE("(%p %p)\n", _this, rhs);
472 MSVCRT_exception_copy_ctor(_this, rhs);
473 _this->vtable = &MSVCRT_bad_cast_vtable;
477 /******************************************************************
478 * ??0bad_cast@@QAE@PBD@Z (MSVCRT.@)
480 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor_charptr,8)
481 bad_cast * __stdcall MSVCRT_bad_cast_ctor_charptr(bad_cast * _this, const char * name)
483 TRACE("(%p %s)\n", _this, name);
484 EXCEPTION_ctor(_this, &name);
485 _this->vtable = &MSVCRT_bad_cast_vtable;
489 /******************************************************************
490 * ??_Fbad_cast@@QAEXXZ (MSVCRT.@)
492 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_default_ctor,4)
493 bad_cast * __stdcall MSVCRT_bad_cast_default_ctor(bad_cast * _this)
495 return MSVCRT_bad_cast_ctor_charptr( _this, "bad cast" );
498 /******************************************************************
499 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
501 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor,4)
502 void __stdcall MSVCRT_bad_cast_dtor(bad_cast * _this)
504 TRACE("(%p)\n", _this);
505 MSVCRT_exception_dtor(_this);
508 /******************************************************************
509 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
511 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals,8)
512 bad_cast * __stdcall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
514 TRACE("(%p %p)\n", _this, rhs);
515 MSVCRT_exception_opequals(_this, rhs);
519 /******************************************************************
520 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
522 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor,8)
523 void * __stdcall MSVCRT_bad_cast_vector_dtor(bad_cast * _this, unsigned int flags)
525 TRACE("(%p %x)\n", _this, flags);
528 /* we have an array, with the number of elements stored before the first object */
529 int i, *ptr = (int *)_this - 1;
531 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_cast_dtor(_this + i);
532 MSVCRT_operator_delete(ptr);
536 MSVCRT_bad_cast_dtor(_this);
537 if (flags & 1) MSVCRT_operator_delete(_this);
542 /******************************************************************
543 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
545 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor,8)
546 void * __stdcall MSVCRT_bad_cast_scalar_dtor(bad_cast * _this, unsigned int flags)
548 TRACE("(%p %x)\n", _this, flags);
549 MSVCRT_bad_cast_dtor(_this);
550 if (flags & 1) MSVCRT_operator_delete(_this);
554 /******************************************************************
555 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
557 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals,8)
558 int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
560 int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
561 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
565 /******************************************************************
566 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
568 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals,8)
569 int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
571 int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
572 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
576 /******************************************************************
577 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
579 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before,8)
580 int __stdcall MSVCRT_type_info_before(type_info * _this, const type_info * rhs)
582 int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
583 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
587 /******************************************************************
588 * ??1type_info@@UAE@XZ (MSVCRT.@)
590 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor,4)
591 void __stdcall MSVCRT_type_info_dtor(type_info * _this)
593 TRACE("(%p)\n", _this);
594 MSVCRT_free(_this->name);
597 /******************************************************************
598 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
600 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name,4)
601 const char * __stdcall MSVCRT_type_info_name(type_info * _this)
605 /* Create and set the demangled name */
606 /* Nota: mangled name in type_info struct always start with a '.', while
607 * it isn't valid for mangled name.
608 * Is this '.' really part of the mangled name, or has it some other meaning ?
610 char* name = __unDName(0, _this->mangled + 1, 0,
611 MSVCRT_malloc, MSVCRT_free, 0x2800);
615 unsigned int len = strlen(name);
617 /* It seems _unDName may leave blanks at the end of the demangled name */
618 while (len && name[--len] == ' ')
625 /* Another thread set this member since we checked above - use it */
631 _munlock(_EXIT_LOCK2);
634 TRACE("(%p) returning %s\n", _this, _this->name);
638 /******************************************************************
639 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
641 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name,4)
642 const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
644 TRACE("(%p) returning %s\n", _this, _this->mangled);
645 return _this->mangled;
649 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor,8)
650 void * __stdcall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
652 TRACE("(%p %x)\n", _this, flags);
655 /* we have an array, with the number of elements stored before the first object */
656 int i, *ptr = (int *)_this - 1;
658 for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
659 MSVCRT_operator_delete(ptr);
663 MSVCRT_type_info_dtor(_this);
664 if (flags & 1) MSVCRT_operator_delete(_this);
673 #define __ASM_VTABLE(name,funcs) \
676 "\t.quad " __ASM_NAME(#name "_rtti") "\n" \
677 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
678 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
679 "\t.quad " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
682 #define __ASM_EXCEPTION_VTABLE(name) \
683 __ASM_VTABLE(name, "\t.quad " THISCALL_NAME(MSVCRT_what_exception) )
687 #define __ASM_VTABLE(name,funcs) \
690 "\t.long " __ASM_NAME(#name "_rtti") "\n" \
691 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
692 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
693 "\t.long " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
696 #define __ASM_EXCEPTION_VTABLE(name) \
697 __ASM_VTABLE(name, "\t.long " THISCALL_NAME(MSVCRT_what_exception) )
702 void __asm_dummy_vtables(void) {
705 __ASM_VTABLE(type_info,"")
706 __ASM_EXCEPTION_VTABLE(exception)
707 __ASM_EXCEPTION_VTABLE(bad_typeid)
708 __ASM_EXCEPTION_VTABLE(bad_cast)
709 __ASM_EXCEPTION_VTABLE(__non_rtti_object)
715 /* Static RTTI for exported objects */
717 static const type_info exception_type_info =
719 &MSVCRT_type_info_vtable,
724 static const rtti_base_descriptor exception_rtti_base_descriptor =
726 &exception_type_info,
732 static const rtti_base_array exception_rtti_base_array =
735 &exception_rtti_base_descriptor,
741 static const rtti_object_hierarchy exception_type_hierarchy =
746 &exception_rtti_base_array
749 const rtti_object_locator exception_rtti =
754 &exception_type_info,
755 &exception_type_hierarchy
758 static const cxx_type_info exception_cxx_type_info =
761 &exception_type_info,
764 (cxx_copy_ctor)THISCALL(MSVCRT_exception_copy_ctor)
767 static const type_info bad_typeid_type_info =
769 &MSVCRT_type_info_vtable,
774 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
776 &bad_typeid_type_info,
782 static const rtti_base_array bad_typeid_rtti_base_array =
785 &bad_typeid_rtti_base_descriptor,
786 &exception_rtti_base_descriptor,
791 static const rtti_object_hierarchy bad_typeid_type_hierarchy =
796 &bad_typeid_rtti_base_array
799 const rtti_object_locator bad_typeid_rtti =
804 &bad_typeid_type_info,
805 &bad_typeid_type_hierarchy
808 static const cxx_type_info bad_typeid_cxx_type_info =
811 &bad_typeid_type_info,
814 (cxx_copy_ctor)THISCALL(MSVCRT_bad_typeid_copy_ctor)
817 static const type_info bad_cast_type_info =
819 &MSVCRT_type_info_vtable,
824 static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
832 static const rtti_base_array bad_cast_rtti_base_array =
835 &bad_cast_rtti_base_descriptor,
836 &exception_rtti_base_descriptor,
841 static const rtti_object_hierarchy bad_cast_type_hierarchy =
846 &bad_cast_rtti_base_array
849 const rtti_object_locator bad_cast_rtti =
855 &bad_cast_type_hierarchy
858 static const cxx_type_info bad_cast_cxx_type_info =
864 (cxx_copy_ctor)THISCALL(MSVCRT_bad_cast_copy_ctor)
867 static const type_info __non_rtti_object_type_info =
869 &MSVCRT_type_info_vtable,
871 ".?AV__non_rtti_object@@"
874 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
876 &__non_rtti_object_type_info,
882 static const rtti_base_array __non_rtti_object_rtti_base_array =
885 &__non_rtti_object_rtti_base_descriptor,
886 &bad_typeid_rtti_base_descriptor,
887 &exception_rtti_base_descriptor
891 static const rtti_object_hierarchy __non_rtti_object_type_hierarchy =
896 &__non_rtti_object_rtti_base_array
899 const rtti_object_locator __non_rtti_object_rtti =
904 &__non_rtti_object_type_info,
905 &__non_rtti_object_type_hierarchy
908 static const cxx_type_info __non_rtti_object_cxx_type_info =
911 &__non_rtti_object_type_info,
914 (cxx_copy_ctor)THISCALL(MSVCRT___non_rtti_object_copy_ctor)
917 static const type_info type_info_type_info =
919 &MSVCRT_type_info_vtable,
924 static const rtti_base_descriptor type_info_rtti_base_descriptor =
926 &type_info_type_info,
932 static const rtti_base_array type_info_rtti_base_array =
935 &type_info_rtti_base_descriptor,
941 static const rtti_object_hierarchy type_info_type_hierarchy =
946 &type_info_rtti_base_array
949 const rtti_object_locator type_info_rtti =
954 &type_info_type_info,
955 &type_info_type_hierarchy
959 * Exception RTTI for cpp objects
961 static const cxx_type_info_table bad_cast_type_info_table =
965 &__non_rtti_object_cxx_type_info,
966 &bad_typeid_cxx_type_info,
967 &exception_cxx_type_info
971 static const cxx_exception_type bad_cast_exception_type =
974 (void*)THISCALL(MSVCRT_bad_cast_dtor),
976 &bad_cast_type_info_table
979 static const cxx_type_info_table bad_typeid_type_info_table =
983 &bad_cast_cxx_type_info,
984 &exception_cxx_type_info,
989 static const cxx_exception_type bad_typeid_exception_type =
992 (void*)THISCALL(MSVCRT_bad_typeid_dtor),
994 &bad_cast_type_info_table
997 static const cxx_exception_type __non_rtti_object_exception_type =
1000 (void*)THISCALL(MSVCRT___non_rtti_object_dtor),
1002 &bad_typeid_type_info_table
1006 /******************************************************************
1007 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
1009 * Install a handler to be called when terminate() is called.
1012 * func [I] Handler function to install
1015 * The previously installed handler function, if any.
1017 MSVCRT_terminate_function CDECL MSVCRT_set_terminate(MSVCRT_terminate_function func)
1019 thread_data_t *data = msvcrt_get_thread_data();
1020 MSVCRT_terminate_function previous = data->terminate_handler;
1021 TRACE("(%p) returning %p\n",func,previous);
1022 data->terminate_handler = func;
1026 /******************************************************************
1027 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
1029 * Install a handler to be called when unexpected() is called.
1032 * func [I] Handler function to install
1035 * The previously installed handler function, if any.
1037 MSVCRT_unexpected_function CDECL MSVCRT_set_unexpected(MSVCRT_unexpected_function func)
1039 thread_data_t *data = msvcrt_get_thread_data();
1040 MSVCRT_unexpected_function previous = data->unexpected_handler;
1041 TRACE("(%p) returning %p\n",func,previous);
1042 data->unexpected_handler = func;
1046 /******************************************************************
1047 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
1049 MSVCRT__se_translator_function CDECL MSVCRT__set_se_translator(MSVCRT__se_translator_function func)
1051 thread_data_t *data = msvcrt_get_thread_data();
1052 MSVCRT__se_translator_function previous = data->se_translator;
1053 TRACE("(%p) returning %p\n",func,previous);
1054 data->se_translator = func;
1058 /******************************************************************
1059 * ?terminate@@YAXXZ (MSVCRT.@)
1061 * Default handler for an unhandled exception.
1067 * This function does not return. Either control resumes from any
1068 * handler installed by calling set_terminate(), or (by default) abort()
1071 void CDECL MSVCRT_terminate(void)
1073 thread_data_t *data = msvcrt_get_thread_data();
1074 if (data->terminate_handler) data->terminate_handler();
1078 /******************************************************************
1079 * ?unexpected@@YAXXZ (MSVCRT.@)
1081 void CDECL MSVCRT_unexpected(void)
1083 thread_data_t *data = msvcrt_get_thread_data();
1084 if (data->unexpected_handler) data->unexpected_handler();
1089 /******************************************************************
1090 * __RTtypeid (MSVCRT.@)
1092 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1095 * cppobj [I] C++ object to get type information for.
1098 * Success: A type_info object describing cppobj.
1099 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
1100 * exception is thrown. If cppobj is NULL, a bad_typeid exception
1101 * is thrown. In either case, this function does not return.
1104 * This function is usually called by compiler generated code as a result
1105 * of using one of the C++ dynamic cast statements.
1107 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
1109 const type_info *ret;
1114 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
1115 _CxxThrowException( &e, &bad_typeid_exception_type );
1121 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1122 ret = obj_locator->type_descriptor;
1126 __non_rtti_object e;
1127 MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
1128 _CxxThrowException( &e, &bad_typeid_exception_type );
1135 /******************************************************************
1136 * __RTDynamicCast (MSVCRT.@)
1138 * Dynamically cast a C++ object to one of its base classes.
1141 * cppobj [I] Any C++ object to cast
1142 * unknown [I] Reserved, set to 0
1143 * src [I] type_info object describing cppobj
1144 * dst [I] type_info object describing the base class to cast to
1145 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
1148 * Success: The address of cppobj, cast to the object described by dst.
1149 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
1150 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
1151 * is thrown and this function does not return.
1154 * This function is usually called by compiler generated code as a result
1155 * of using one of the C++ dynamic cast statements.
1157 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
1158 type_info *src, type_info *dst,
1163 if (!cppobj) return NULL;
1165 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
1166 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
1168 /* To cast an object at runtime:
1169 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
1170 * 2.Search for the destination type in the class hierarchy
1171 * 3.If destination type is found, return base object address + dest offset
1172 * Otherwise, fail the cast
1174 * FIXME: the unknown parameter doesn't seem to be used for anything
1179 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1180 const rtti_object_hierarchy *obj_bases = obj_locator->type_hierarchy;
1181 const rtti_base_descriptor * const* base_desc = obj_bases->base_classes->bases;
1183 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
1186 for (i = 0; i < obj_bases->array_len; i++)
1188 const type_info *typ = base_desc[i]->type_descriptor;
1190 if (!strcmp(typ->mangled, dst->mangled))
1192 /* compute the correct this pointer for that base class */
1193 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
1194 ret = get_this_pointer( &base_desc[i]->offsets, this_ptr );
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.
1201 if (!ret && do_throw)
1203 const char *msg = "Bad dynamic_cast!";
1205 MSVCRT_bad_cast_ctor( &e, &msg );
1206 _CxxThrowException( &e, &bad_cast_exception_type );
1211 __non_rtti_object e;
1212 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1213 _CxxThrowException( &e, &bad_typeid_exception_type );
1221 /******************************************************************
1222 * __RTCastToVoid (MSVCRT.@)
1224 * Dynamically cast a C++ object to a void*.
1227 * cppobj [I] The C++ object to cast
1230 * Success: The base address of the object as a void*.
1231 * Failure: NULL, if cppobj is NULL or has no RTTI.
1234 * This function is usually called by compiler generated code as a result
1235 * of using one of the C++ dynamic cast statements.
1237 void* CDECL MSVCRT___RTCastToVoid(void *cppobj)
1241 if (!cppobj) return NULL;
1245 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1246 ret = (char *)cppobj - obj_locator->base_class_offset;
1250 __non_rtti_object e;
1251 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1252 _CxxThrowException( &e, &bad_typeid_exception_type );