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 __thiscall __stdcall
77 #define DEFINE_THISCALL_WRAPPER(func,args) \
78 extern void THISCALL(func)(void); \
79 __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
83 "jmp " __ASM_NAME(#func) __ASM_STDCALL(args) )
86 #define THISCALL(func) func
87 #define THISCALL_NAME(func) __ASM_NAME(#func)
88 #define __thiscall __cdecl
89 #define DEFINE_THISCALL_WRAPPER(func,args) /* nothing */
93 extern const vtable_ptr MSVCRT_exception_vtable;
94 extern const vtable_ptr MSVCRT_bad_typeid_vtable;
95 extern const vtable_ptr MSVCRT_bad_cast_vtable;
96 extern const vtable_ptr MSVCRT___non_rtti_object_vtable;
97 extern const vtable_ptr MSVCRT_type_info_vtable;
99 /* get the vtable pointer for a C++ object */
100 static inline const vtable_ptr *get_vtable( void *obj )
102 return *(const vtable_ptr **)obj;
105 static inline const rtti_object_locator *get_obj_locator( void *cppobj )
107 const vtable_ptr *vtable = get_vtable( cppobj );
108 return (const rtti_object_locator *)vtable[-1];
111 static void dump_obj_locator( const rtti_object_locator *ptr )
114 const rtti_object_hierarchy *h = ptr->type_hierarchy;
116 TRACE( "%p: sig=%08x base_offset=%08x flags=%08x type=%p %s hierarchy=%p\n",
117 ptr, ptr->signature, ptr->base_class_offset, ptr->flags,
118 ptr->type_descriptor, dbgstr_type_info(ptr->type_descriptor), ptr->type_hierarchy );
119 TRACE( " hierarchy: sig=%08x attr=%08x len=%d base classes=%p\n",
120 h->signature, h->attributes, h->array_len, h->base_classes );
121 for (i = 0; i < h->array_len; i++)
123 TRACE( " base class %p: num %d off %d,%d,%d attr %08x type %p %s\n",
124 h->base_classes->bases[i],
125 h->base_classes->bases[i]->num_base_classes,
126 h->base_classes->bases[i]->offsets.this_offset,
127 h->base_classes->bases[i]->offsets.vbase_descr,
128 h->base_classes->bases[i]->offsets.vbase_offset,
129 h->base_classes->bases[i]->attributes,
130 h->base_classes->bases[i]->type_descriptor,
131 dbgstr_type_info(h->base_classes->bases[i]->type_descriptor) );
135 /* Internal common ctor for exception */
136 static void EXCEPTION_ctor(exception *_this, const char** name)
138 _this->vtable = &MSVCRT_exception_vtable;
141 unsigned int name_len = strlen(*name) + 1;
142 _this->name = MSVCRT_malloc(name_len);
143 memcpy(_this->name, *name, name_len);
144 _this->do_free = TRUE;
149 _this->do_free = FALSE;
153 /******************************************************************
154 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
156 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor,8)
157 exception * __thiscall MSVCRT_exception_ctor(exception * _this, const char ** name)
159 TRACE("(%p,%s)\n", _this, *name);
160 EXCEPTION_ctor(_this, name);
164 /******************************************************************
165 * ??0exception@@QAE@ABQBDH@Z (MSVCRT.@)
167 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor_noalloc,12)
168 exception * __thiscall MSVCRT_exception_ctor_noalloc(exception * _this, char ** name, int noalloc)
170 TRACE("(%p,%s)\n", _this, *name);
171 _this->vtable = &MSVCRT_exception_vtable;
173 _this->do_free = FALSE;
177 /******************************************************************
178 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
180 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor,8)
181 exception * __thiscall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
183 TRACE("(%p,%p)\n", _this, rhs);
187 _this->vtable = &MSVCRT_exception_vtable;
188 _this->name = rhs->name;
189 _this->do_free = FALSE;
192 EXCEPTION_ctor(_this, (const char**)&rhs->name);
193 TRACE("name = %s\n", _this->name);
197 /******************************************************************
198 * ??0exception@@QAE@XZ (MSVCRT.@)
200 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor,4)
201 exception * __thiscall MSVCRT_exception_default_ctor(exception * _this)
203 static const char* empty = NULL;
205 TRACE("(%p)\n", _this);
206 EXCEPTION_ctor(_this, &empty);
210 /******************************************************************
211 * ??1exception@@UAE@XZ (MSVCRT.@)
213 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor,4)
214 void __thiscall MSVCRT_exception_dtor(exception * _this)
216 TRACE("(%p)\n", _this);
217 _this->vtable = &MSVCRT_exception_vtable;
218 if (_this->do_free) MSVCRT_free(_this->name);
221 /******************************************************************
222 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
224 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals,8)
225 exception * __thiscall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
227 TRACE("(%p %p)\n", _this, rhs);
230 MSVCRT_exception_dtor(_this);
231 MSVCRT_exception_copy_ctor(_this, rhs);
233 TRACE("name = %s\n", _this->name);
237 /******************************************************************
238 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
240 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor,8)
241 void * __thiscall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
243 TRACE("(%p %x)\n", _this, flags);
246 /* we have an array, with the number of elements stored before the first object */
247 int i, *ptr = (int *)_this - 1;
249 for (i = *ptr - 1; i >= 0; i--) MSVCRT_exception_dtor(_this + i);
250 MSVCRT_operator_delete(ptr);
254 MSVCRT_exception_dtor(_this);
255 if (flags & 1) MSVCRT_operator_delete(_this);
260 /******************************************************************
261 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
263 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor,8)
264 void * __thiscall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
266 TRACE("(%p %x)\n", _this, flags);
267 MSVCRT_exception_dtor(_this);
268 if (flags & 1) MSVCRT_operator_delete(_this);
272 /******************************************************************
273 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
275 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception,4)
276 const char * __thiscall MSVCRT_what_exception(exception * _this)
278 TRACE("(%p) returning %s\n", _this, _this->name);
279 return _this->name ? _this->name : "Unknown exception";
282 /******************************************************************
283 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
285 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor,8)
286 bad_typeid * __thiscall MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
288 TRACE("(%p %p)\n", _this, rhs);
289 MSVCRT_exception_copy_ctor(_this, rhs);
290 _this->vtable = &MSVCRT_bad_typeid_vtable;
294 /******************************************************************
295 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
297 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor,8)
298 bad_typeid * __thiscall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
300 TRACE("(%p %s)\n", _this, name);
301 EXCEPTION_ctor(_this, &name);
302 _this->vtable = &MSVCRT_bad_typeid_vtable;
306 /******************************************************************
307 * ??_Fbad_typeid@@QAEXXZ (MSVCRT.@)
309 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_default_ctor,4)
310 bad_typeid * __thiscall MSVCRT_bad_typeid_default_ctor(bad_typeid * _this)
312 return MSVCRT_bad_typeid_ctor( _this, "bad typeid" );
315 /******************************************************************
316 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
318 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor,4)
319 void __thiscall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
321 TRACE("(%p)\n", _this);
322 MSVCRT_exception_dtor(_this);
325 /******************************************************************
326 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
328 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals,8)
329 bad_typeid * __thiscall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
331 TRACE("(%p %p)\n", _this, rhs);
332 MSVCRT_exception_opequals(_this, rhs);
336 /******************************************************************
337 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
339 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor,8)
340 void * __thiscall MSVCRT_bad_typeid_vector_dtor(bad_typeid * _this, unsigned int flags)
342 TRACE("(%p %x)\n", _this, flags);
345 /* we have an array, with the number of elements stored before the first object */
346 int i, *ptr = (int *)_this - 1;
348 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_typeid_dtor(_this + i);
349 MSVCRT_operator_delete(ptr);
353 MSVCRT_bad_typeid_dtor(_this);
354 if (flags & 1) MSVCRT_operator_delete(_this);
359 /******************************************************************
360 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
362 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor,8)
363 void * __thiscall MSVCRT_bad_typeid_scalar_dtor(bad_typeid * _this, unsigned int flags)
365 TRACE("(%p %x)\n", _this, flags);
366 MSVCRT_bad_typeid_dtor(_this);
367 if (flags & 1) MSVCRT_operator_delete(_this);
371 /******************************************************************
372 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
374 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor,8)
375 __non_rtti_object * __thiscall MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
376 const __non_rtti_object * rhs)
378 TRACE("(%p %p)\n", _this, rhs);
379 MSVCRT_bad_typeid_copy_ctor(_this, rhs);
380 _this->vtable = &MSVCRT___non_rtti_object_vtable;
384 /******************************************************************
385 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
387 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor,8)
388 __non_rtti_object * __thiscall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
391 TRACE("(%p %s)\n", _this, name);
392 EXCEPTION_ctor(_this, &name);
393 _this->vtable = &MSVCRT___non_rtti_object_vtable;
397 /******************************************************************
398 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
400 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor,4)
401 void __thiscall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
403 TRACE("(%p)\n", _this);
404 MSVCRT_bad_typeid_dtor(_this);
407 /******************************************************************
408 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
410 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals,8)
411 __non_rtti_object * __thiscall MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
412 const __non_rtti_object *rhs)
414 TRACE("(%p %p)\n", _this, rhs);
415 MSVCRT_bad_typeid_opequals(_this, rhs);
419 /******************************************************************
420 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
422 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor,8)
423 void * __thiscall MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object * _this, unsigned int flags)
425 TRACE("(%p %x)\n", _this, flags);
428 /* we have an array, with the number of elements stored before the first object */
429 int i, *ptr = (int *)_this - 1;
431 for (i = *ptr - 1; i >= 0; i--) MSVCRT___non_rtti_object_dtor(_this + i);
432 MSVCRT_operator_delete(ptr);
436 MSVCRT___non_rtti_object_dtor(_this);
437 if (flags & 1) MSVCRT_operator_delete(_this);
442 /******************************************************************
443 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
445 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor,8)
446 void * __thiscall MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object * _this, unsigned int flags)
448 TRACE("(%p %x)\n", _this, flags);
449 MSVCRT___non_rtti_object_dtor(_this);
450 if (flags & 1) MSVCRT_operator_delete(_this);
454 /******************************************************************
455 * ??0bad_cast@@AAE@PBQBD@Z (MSVCRT.@)
456 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
458 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor,8)
459 bad_cast * __thiscall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
461 TRACE("(%p %s)\n", _this, *name);
462 EXCEPTION_ctor(_this, name);
463 _this->vtable = &MSVCRT_bad_cast_vtable;
467 /******************************************************************
468 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
470 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor,8)
471 bad_cast * __thiscall MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
473 TRACE("(%p %p)\n", _this, rhs);
474 MSVCRT_exception_copy_ctor(_this, rhs);
475 _this->vtable = &MSVCRT_bad_cast_vtable;
479 /******************************************************************
480 * ??0bad_cast@@QAE@PBD@Z (MSVCRT.@)
482 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor_charptr,8)
483 bad_cast * __thiscall MSVCRT_bad_cast_ctor_charptr(bad_cast * _this, const char * name)
485 TRACE("(%p %s)\n", _this, name);
486 EXCEPTION_ctor(_this, &name);
487 _this->vtable = &MSVCRT_bad_cast_vtable;
491 /******************************************************************
492 * ??_Fbad_cast@@QAEXXZ (MSVCRT.@)
494 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_default_ctor,4)
495 bad_cast * __thiscall MSVCRT_bad_cast_default_ctor(bad_cast * _this)
497 return MSVCRT_bad_cast_ctor_charptr( _this, "bad cast" );
500 /******************************************************************
501 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
503 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor,4)
504 void __thiscall MSVCRT_bad_cast_dtor(bad_cast * _this)
506 TRACE("(%p)\n", _this);
507 MSVCRT_exception_dtor(_this);
510 /******************************************************************
511 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
513 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals,8)
514 bad_cast * __thiscall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
516 TRACE("(%p %p)\n", _this, rhs);
517 MSVCRT_exception_opequals(_this, rhs);
521 /******************************************************************
522 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
524 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor,8)
525 void * __thiscall MSVCRT_bad_cast_vector_dtor(bad_cast * _this, unsigned int flags)
527 TRACE("(%p %x)\n", _this, flags);
530 /* we have an array, with the number of elements stored before the first object */
531 int i, *ptr = (int *)_this - 1;
533 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_cast_dtor(_this + i);
534 MSVCRT_operator_delete(ptr);
538 MSVCRT_bad_cast_dtor(_this);
539 if (flags & 1) MSVCRT_operator_delete(_this);
544 /******************************************************************
545 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
547 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor,8)
548 void * __thiscall MSVCRT_bad_cast_scalar_dtor(bad_cast * _this, unsigned int flags)
550 TRACE("(%p %x)\n", _this, flags);
551 MSVCRT_bad_cast_dtor(_this);
552 if (flags & 1) MSVCRT_operator_delete(_this);
556 /******************************************************************
557 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
559 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals,8)
560 int __thiscall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
562 int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
563 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
567 /******************************************************************
568 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
570 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals,8)
571 int __thiscall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
573 int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
574 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
578 /******************************************************************
579 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
581 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before,8)
582 int __thiscall MSVCRT_type_info_before(type_info * _this, const type_info * rhs)
584 int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
585 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
589 /******************************************************************
590 * ??1type_info@@UAE@XZ (MSVCRT.@)
592 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor,4)
593 void __thiscall MSVCRT_type_info_dtor(type_info * _this)
595 TRACE("(%p)\n", _this);
596 MSVCRT_free(_this->name);
599 /******************************************************************
600 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
602 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name,4)
603 const char * __thiscall MSVCRT_type_info_name(type_info * _this)
607 /* Create and set the demangled name */
608 /* Note: mangled name in type_info struct always starts with a '.', while
609 * it isn't valid for mangled name.
610 * Is this '.' really part of the mangled name, or has it some other meaning ?
612 char* name = __unDName(0, _this->mangled + 1, 0,
613 MSVCRT_malloc, MSVCRT_free, 0x2800);
616 unsigned int len = strlen(name);
618 /* It seems _unDName may leave blanks at the end of the demangled name */
619 while (len && name[--len] == ' ')
622 if (InterlockedCompareExchangePointer((void**)&_this->name, name, NULL))
624 /* Another thread set this member since we checked above - use it */
629 TRACE("(%p) returning %s\n", _this, _this->name);
633 /******************************************************************
634 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
636 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name,4)
637 const char * __thiscall MSVCRT_type_info_raw_name(type_info * _this)
639 TRACE("(%p) returning %s\n", _this, _this->mangled);
640 return _this->mangled;
644 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor,8)
645 void * __thiscall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
647 TRACE("(%p %x)\n", _this, flags);
650 /* we have an array, with the number of elements stored before the first object */
651 int i, *ptr = (int *)_this - 1;
653 for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
654 MSVCRT_operator_delete(ptr);
658 MSVCRT_type_info_dtor(_this);
659 if (flags & 1) MSVCRT_operator_delete(_this);
668 #define __ASM_VTABLE(name,funcs) \
671 "\t.quad " __ASM_NAME(#name "_rtti") "\n" \
672 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
673 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
674 "\t.quad " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
677 #define __ASM_EXCEPTION_VTABLE(name) \
678 __ASM_VTABLE(name, "\t.quad " THISCALL_NAME(MSVCRT_what_exception) )
682 #define __ASM_VTABLE(name,funcs) \
685 "\t.long " __ASM_NAME(#name "_rtti") "\n" \
686 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
687 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
688 "\t.long " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
691 #define __ASM_EXCEPTION_VTABLE(name) \
692 __ASM_VTABLE(name, "\t.long " THISCALL_NAME(MSVCRT_what_exception) )
697 void __asm_dummy_vtables(void) {
700 __ASM_VTABLE(type_info,"")
701 __ASM_EXCEPTION_VTABLE(exception)
702 __ASM_EXCEPTION_VTABLE(bad_typeid)
703 __ASM_EXCEPTION_VTABLE(bad_cast)
704 __ASM_EXCEPTION_VTABLE(__non_rtti_object)
710 /* Static RTTI for exported objects */
712 static const type_info exception_type_info =
714 &MSVCRT_type_info_vtable,
719 static const rtti_base_descriptor exception_rtti_base_descriptor =
721 &exception_type_info,
727 static const rtti_base_array exception_rtti_base_array =
730 &exception_rtti_base_descriptor,
736 static const rtti_object_hierarchy exception_type_hierarchy =
741 &exception_rtti_base_array
744 const rtti_object_locator exception_rtti =
749 &exception_type_info,
750 &exception_type_hierarchy
753 static const cxx_type_info exception_cxx_type_info =
756 &exception_type_info,
759 (cxx_copy_ctor)THISCALL(MSVCRT_exception_copy_ctor)
762 static const type_info bad_typeid_type_info =
764 &MSVCRT_type_info_vtable,
769 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
771 &bad_typeid_type_info,
777 static const rtti_base_array bad_typeid_rtti_base_array =
780 &bad_typeid_rtti_base_descriptor,
781 &exception_rtti_base_descriptor,
786 static const rtti_object_hierarchy bad_typeid_type_hierarchy =
791 &bad_typeid_rtti_base_array
794 const rtti_object_locator bad_typeid_rtti =
799 &bad_typeid_type_info,
800 &bad_typeid_type_hierarchy
803 static const cxx_type_info bad_typeid_cxx_type_info =
806 &bad_typeid_type_info,
809 (cxx_copy_ctor)THISCALL(MSVCRT_bad_typeid_copy_ctor)
812 static const type_info bad_cast_type_info =
814 &MSVCRT_type_info_vtable,
819 static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
827 static const rtti_base_array bad_cast_rtti_base_array =
830 &bad_cast_rtti_base_descriptor,
831 &exception_rtti_base_descriptor,
836 static const rtti_object_hierarchy bad_cast_type_hierarchy =
841 &bad_cast_rtti_base_array
844 const rtti_object_locator bad_cast_rtti =
850 &bad_cast_type_hierarchy
853 static const cxx_type_info bad_cast_cxx_type_info =
859 (cxx_copy_ctor)THISCALL(MSVCRT_bad_cast_copy_ctor)
862 static const type_info __non_rtti_object_type_info =
864 &MSVCRT_type_info_vtable,
866 ".?AV__non_rtti_object@@"
869 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
871 &__non_rtti_object_type_info,
877 static const rtti_base_array __non_rtti_object_rtti_base_array =
880 &__non_rtti_object_rtti_base_descriptor,
881 &bad_typeid_rtti_base_descriptor,
882 &exception_rtti_base_descriptor
886 static const rtti_object_hierarchy __non_rtti_object_type_hierarchy =
891 &__non_rtti_object_rtti_base_array
894 const rtti_object_locator __non_rtti_object_rtti =
899 &__non_rtti_object_type_info,
900 &__non_rtti_object_type_hierarchy
903 static const cxx_type_info __non_rtti_object_cxx_type_info =
906 &__non_rtti_object_type_info,
909 (cxx_copy_ctor)THISCALL(MSVCRT___non_rtti_object_copy_ctor)
912 static const type_info type_info_type_info =
914 &MSVCRT_type_info_vtable,
919 static const rtti_base_descriptor type_info_rtti_base_descriptor =
921 &type_info_type_info,
927 static const rtti_base_array type_info_rtti_base_array =
930 &type_info_rtti_base_descriptor,
936 static const rtti_object_hierarchy type_info_type_hierarchy =
941 &type_info_rtti_base_array
944 const rtti_object_locator type_info_rtti =
949 &type_info_type_info,
950 &type_info_type_hierarchy
954 * Exception RTTI for cpp objects
956 static const cxx_type_info_table bad_cast_type_info_table =
960 &__non_rtti_object_cxx_type_info,
961 &bad_typeid_cxx_type_info,
962 &exception_cxx_type_info
966 static const cxx_exception_type bad_cast_exception_type =
969 (void*)THISCALL(MSVCRT_bad_cast_dtor),
971 &bad_cast_type_info_table
974 static const cxx_type_info_table bad_typeid_type_info_table =
978 &bad_cast_cxx_type_info,
979 &exception_cxx_type_info,
984 static const cxx_exception_type bad_typeid_exception_type =
987 (void*)THISCALL(MSVCRT_bad_typeid_dtor),
989 &bad_cast_type_info_table
992 static const cxx_exception_type __non_rtti_object_exception_type =
995 (void*)THISCALL(MSVCRT___non_rtti_object_dtor),
997 &bad_typeid_type_info_table
1001 /******************************************************************
1002 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
1004 * Install a handler to be called when terminate() is called.
1007 * func [I] Handler function to install
1010 * The previously installed handler function, if any.
1012 MSVCRT_terminate_function CDECL MSVCRT_set_terminate(MSVCRT_terminate_function func)
1014 thread_data_t *data = msvcrt_get_thread_data();
1015 MSVCRT_terminate_function previous = data->terminate_handler;
1016 TRACE("(%p) returning %p\n",func,previous);
1017 data->terminate_handler = func;
1021 /******************************************************************
1022 * _get_terminate (MSVCRT.@)
1024 MSVCRT_terminate_function CDECL MSVCRT__get_terminate(void)
1026 thread_data_t *data = msvcrt_get_thread_data();
1027 TRACE("returning %p\n", data->terminate_handler);
1028 return data->terminate_handler;
1031 /******************************************************************
1032 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
1034 * Install a handler to be called when unexpected() is called.
1037 * func [I] Handler function to install
1040 * The previously installed handler function, if any.
1042 MSVCRT_unexpected_function CDECL MSVCRT_set_unexpected(MSVCRT_unexpected_function func)
1044 thread_data_t *data = msvcrt_get_thread_data();
1045 MSVCRT_unexpected_function previous = data->unexpected_handler;
1046 TRACE("(%p) returning %p\n",func,previous);
1047 data->unexpected_handler = func;
1051 /******************************************************************
1052 * _get_unexpected (MSVCRT.@)
1054 MSVCRT_unexpected_function CDECL MSVCRT__get_unexpected(void)
1056 thread_data_t *data = msvcrt_get_thread_data();
1057 TRACE("returning %p\n", data->unexpected_handler);
1058 return data->unexpected_handler;
1061 /******************************************************************
1062 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
1064 MSVCRT__se_translator_function CDECL MSVCRT__set_se_translator(MSVCRT__se_translator_function func)
1066 thread_data_t *data = msvcrt_get_thread_data();
1067 MSVCRT__se_translator_function previous = data->se_translator;
1068 TRACE("(%p) returning %p\n",func,previous);
1069 data->se_translator = func;
1073 /******************************************************************
1074 * ?terminate@@YAXXZ (MSVCRT.@)
1076 * Default handler for an unhandled exception.
1082 * This function does not return. Either control resumes from any
1083 * handler installed by calling set_terminate(), or (by default) abort()
1086 void CDECL MSVCRT_terminate(void)
1088 thread_data_t *data = msvcrt_get_thread_data();
1089 if (data->terminate_handler) data->terminate_handler();
1093 /******************************************************************
1094 * ?unexpected@@YAXXZ (MSVCRT.@)
1096 void CDECL MSVCRT_unexpected(void)
1098 thread_data_t *data = msvcrt_get_thread_data();
1099 if (data->unexpected_handler) data->unexpected_handler();
1104 /******************************************************************
1105 * __RTtypeid (MSVCRT.@)
1107 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1110 * cppobj [I] C++ object to get type information for.
1113 * Success: A type_info object describing cppobj.
1114 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
1115 * exception is thrown. If cppobj is NULL, a bad_typeid exception
1116 * is thrown. In either case, this function does not return.
1119 * This function is usually called by compiler generated code as a result
1120 * of using one of the C++ dynamic cast statements.
1122 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
1124 const type_info *ret;
1129 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
1130 _CxxThrowException( &e, &bad_typeid_exception_type );
1136 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1137 ret = obj_locator->type_descriptor;
1141 __non_rtti_object e;
1142 MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
1143 _CxxThrowException( &e, &bad_typeid_exception_type );
1150 /******************************************************************
1151 * __RTDynamicCast (MSVCRT.@)
1153 * Dynamically cast a C++ object to one of its base classes.
1156 * cppobj [I] Any C++ object to cast
1157 * unknown [I] Reserved, set to 0
1158 * src [I] type_info object describing cppobj
1159 * dst [I] type_info object describing the base class to cast to
1160 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
1163 * Success: The address of cppobj, cast to the object described by dst.
1164 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
1165 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
1166 * is thrown and this function does not return.
1169 * This function is usually called by compiler generated code as a result
1170 * of using one of the C++ dynamic cast statements.
1172 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
1173 type_info *src, type_info *dst,
1178 if (!cppobj) return NULL;
1180 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
1181 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
1183 /* To cast an object at runtime:
1184 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
1185 * 2.Search for the destination type in the class hierarchy
1186 * 3.If destination type is found, return base object address + dest offset
1187 * Otherwise, fail the cast
1189 * FIXME: the unknown parameter doesn't seem to be used for anything
1194 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1195 const rtti_object_hierarchy *obj_bases = obj_locator->type_hierarchy;
1196 const rtti_base_descriptor * const* base_desc = obj_bases->base_classes->bases;
1198 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
1201 for (i = 0; i < obj_bases->array_len; i++)
1203 const type_info *typ = base_desc[i]->type_descriptor;
1205 if (!strcmp(typ->mangled, dst->mangled))
1207 /* compute the correct this pointer for that base class */
1208 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
1209 ret = get_this_pointer( &base_desc[i]->offsets, this_ptr );
1213 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
1214 * to a reference, since references cannot be NULL.
1216 if (!ret && do_throw)
1218 const char *msg = "Bad dynamic_cast!";
1220 MSVCRT_bad_cast_ctor( &e, &msg );
1221 _CxxThrowException( &e, &bad_cast_exception_type );
1226 __non_rtti_object e;
1227 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1228 _CxxThrowException( &e, &bad_typeid_exception_type );
1236 /******************************************************************
1237 * __RTCastToVoid (MSVCRT.@)
1239 * Dynamically cast a C++ object to a void*.
1242 * cppobj [I] The C++ object to cast
1245 * Success: The base address of the object as a void*.
1246 * Failure: NULL, if cppobj is NULL or has no RTTI.
1249 * This function is usually called by compiler generated code as a result
1250 * of using one of the C++ dynamic cast statements.
1252 void* CDECL MSVCRT___RTCastToVoid(void *cppobj)
1256 if (!cppobj) return NULL;
1260 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1261 ret = (char *)cppobj - obj_locator->base_class_offset;
1265 __non_rtti_object e;
1266 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1267 _CxxThrowException( &e, &bad_typeid_exception_type );