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 /* Nota: mangled name in type_info struct always start 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);
617 unsigned int len = strlen(name);
619 /* It seems _unDName may leave blanks at the end of the demangled name */
620 while (len && name[--len] == ' ')
627 /* Another thread set this member since we checked above - use it */
633 _munlock(_EXIT_LOCK2);
636 TRACE("(%p) returning %s\n", _this, _this->name);
640 /******************************************************************
641 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
643 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name,4)
644 const char * __thiscall MSVCRT_type_info_raw_name(type_info * _this)
646 TRACE("(%p) returning %s\n", _this, _this->mangled);
647 return _this->mangled;
651 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor,8)
652 void * __thiscall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
654 TRACE("(%p %x)\n", _this, flags);
657 /* we have an array, with the number of elements stored before the first object */
658 int i, *ptr = (int *)_this - 1;
660 for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
661 MSVCRT_operator_delete(ptr);
665 MSVCRT_type_info_dtor(_this);
666 if (flags & 1) MSVCRT_operator_delete(_this);
675 #define __ASM_VTABLE(name,funcs) \
678 "\t.quad " __ASM_NAME(#name "_rtti") "\n" \
679 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
680 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
681 "\t.quad " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
684 #define __ASM_EXCEPTION_VTABLE(name) \
685 __ASM_VTABLE(name, "\t.quad " THISCALL_NAME(MSVCRT_what_exception) )
689 #define __ASM_VTABLE(name,funcs) \
692 "\t.long " __ASM_NAME(#name "_rtti") "\n" \
693 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
694 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
695 "\t.long " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
698 #define __ASM_EXCEPTION_VTABLE(name) \
699 __ASM_VTABLE(name, "\t.long " THISCALL_NAME(MSVCRT_what_exception) )
704 void __asm_dummy_vtables(void) {
707 __ASM_VTABLE(type_info,"")
708 __ASM_EXCEPTION_VTABLE(exception)
709 __ASM_EXCEPTION_VTABLE(bad_typeid)
710 __ASM_EXCEPTION_VTABLE(bad_cast)
711 __ASM_EXCEPTION_VTABLE(__non_rtti_object)
717 /* Static RTTI for exported objects */
719 static const type_info exception_type_info =
721 &MSVCRT_type_info_vtable,
726 static const rtti_base_descriptor exception_rtti_base_descriptor =
728 &exception_type_info,
734 static const rtti_base_array exception_rtti_base_array =
737 &exception_rtti_base_descriptor,
743 static const rtti_object_hierarchy exception_type_hierarchy =
748 &exception_rtti_base_array
751 const rtti_object_locator exception_rtti =
756 &exception_type_info,
757 &exception_type_hierarchy
760 static const cxx_type_info exception_cxx_type_info =
763 &exception_type_info,
766 (cxx_copy_ctor)THISCALL(MSVCRT_exception_copy_ctor)
769 static const type_info bad_typeid_type_info =
771 &MSVCRT_type_info_vtable,
776 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
778 &bad_typeid_type_info,
784 static const rtti_base_array bad_typeid_rtti_base_array =
787 &bad_typeid_rtti_base_descriptor,
788 &exception_rtti_base_descriptor,
793 static const rtti_object_hierarchy bad_typeid_type_hierarchy =
798 &bad_typeid_rtti_base_array
801 const rtti_object_locator bad_typeid_rtti =
806 &bad_typeid_type_info,
807 &bad_typeid_type_hierarchy
810 static const cxx_type_info bad_typeid_cxx_type_info =
813 &bad_typeid_type_info,
816 (cxx_copy_ctor)THISCALL(MSVCRT_bad_typeid_copy_ctor)
819 static const type_info bad_cast_type_info =
821 &MSVCRT_type_info_vtable,
826 static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
834 static const rtti_base_array bad_cast_rtti_base_array =
837 &bad_cast_rtti_base_descriptor,
838 &exception_rtti_base_descriptor,
843 static const rtti_object_hierarchy bad_cast_type_hierarchy =
848 &bad_cast_rtti_base_array
851 const rtti_object_locator bad_cast_rtti =
857 &bad_cast_type_hierarchy
860 static const cxx_type_info bad_cast_cxx_type_info =
866 (cxx_copy_ctor)THISCALL(MSVCRT_bad_cast_copy_ctor)
869 static const type_info __non_rtti_object_type_info =
871 &MSVCRT_type_info_vtable,
873 ".?AV__non_rtti_object@@"
876 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
878 &__non_rtti_object_type_info,
884 static const rtti_base_array __non_rtti_object_rtti_base_array =
887 &__non_rtti_object_rtti_base_descriptor,
888 &bad_typeid_rtti_base_descriptor,
889 &exception_rtti_base_descriptor
893 static const rtti_object_hierarchy __non_rtti_object_type_hierarchy =
898 &__non_rtti_object_rtti_base_array
901 const rtti_object_locator __non_rtti_object_rtti =
906 &__non_rtti_object_type_info,
907 &__non_rtti_object_type_hierarchy
910 static const cxx_type_info __non_rtti_object_cxx_type_info =
913 &__non_rtti_object_type_info,
916 (cxx_copy_ctor)THISCALL(MSVCRT___non_rtti_object_copy_ctor)
919 static const type_info type_info_type_info =
921 &MSVCRT_type_info_vtable,
926 static const rtti_base_descriptor type_info_rtti_base_descriptor =
928 &type_info_type_info,
934 static const rtti_base_array type_info_rtti_base_array =
937 &type_info_rtti_base_descriptor,
943 static const rtti_object_hierarchy type_info_type_hierarchy =
948 &type_info_rtti_base_array
951 const rtti_object_locator type_info_rtti =
956 &type_info_type_info,
957 &type_info_type_hierarchy
961 * Exception RTTI for cpp objects
963 static const cxx_type_info_table bad_cast_type_info_table =
967 &__non_rtti_object_cxx_type_info,
968 &bad_typeid_cxx_type_info,
969 &exception_cxx_type_info
973 static const cxx_exception_type bad_cast_exception_type =
976 (void*)THISCALL(MSVCRT_bad_cast_dtor),
978 &bad_cast_type_info_table
981 static const cxx_type_info_table bad_typeid_type_info_table =
985 &bad_cast_cxx_type_info,
986 &exception_cxx_type_info,
991 static const cxx_exception_type bad_typeid_exception_type =
994 (void*)THISCALL(MSVCRT_bad_typeid_dtor),
996 &bad_cast_type_info_table
999 static const cxx_exception_type __non_rtti_object_exception_type =
1002 (void*)THISCALL(MSVCRT___non_rtti_object_dtor),
1004 &bad_typeid_type_info_table
1008 /******************************************************************
1009 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
1011 * Install a handler to be called when terminate() is called.
1014 * func [I] Handler function to install
1017 * The previously installed handler function, if any.
1019 MSVCRT_terminate_function CDECL MSVCRT_set_terminate(MSVCRT_terminate_function func)
1021 thread_data_t *data = msvcrt_get_thread_data();
1022 MSVCRT_terminate_function previous = data->terminate_handler;
1023 TRACE("(%p) returning %p\n",func,previous);
1024 data->terminate_handler = func;
1028 /******************************************************************
1029 * _get_terminate (MSVCRT.@)
1031 MSVCRT_terminate_function CDECL MSVCRT__get_terminate(void)
1033 thread_data_t *data = msvcrt_get_thread_data();
1034 TRACE("returning %p\n", data->terminate_handler);
1035 return data->terminate_handler;
1038 /******************************************************************
1039 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
1041 * Install a handler to be called when unexpected() is called.
1044 * func [I] Handler function to install
1047 * The previously installed handler function, if any.
1049 MSVCRT_unexpected_function CDECL MSVCRT_set_unexpected(MSVCRT_unexpected_function func)
1051 thread_data_t *data = msvcrt_get_thread_data();
1052 MSVCRT_unexpected_function previous = data->unexpected_handler;
1053 TRACE("(%p) returning %p\n",func,previous);
1054 data->unexpected_handler = func;
1058 /******************************************************************
1059 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
1061 MSVCRT__se_translator_function CDECL MSVCRT__set_se_translator(MSVCRT__se_translator_function func)
1063 thread_data_t *data = msvcrt_get_thread_data();
1064 MSVCRT__se_translator_function previous = data->se_translator;
1065 TRACE("(%p) returning %p\n",func,previous);
1066 data->se_translator = func;
1070 /******************************************************************
1071 * ?terminate@@YAXXZ (MSVCRT.@)
1073 * Default handler for an unhandled exception.
1079 * This function does not return. Either control resumes from any
1080 * handler installed by calling set_terminate(), or (by default) abort()
1083 void CDECL MSVCRT_terminate(void)
1085 thread_data_t *data = msvcrt_get_thread_data();
1086 if (data->terminate_handler) data->terminate_handler();
1090 /******************************************************************
1091 * ?unexpected@@YAXXZ (MSVCRT.@)
1093 void CDECL MSVCRT_unexpected(void)
1095 thread_data_t *data = msvcrt_get_thread_data();
1096 if (data->unexpected_handler) data->unexpected_handler();
1101 /******************************************************************
1102 * __RTtypeid (MSVCRT.@)
1104 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1107 * cppobj [I] C++ object to get type information for.
1110 * Success: A type_info object describing cppobj.
1111 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
1112 * exception is thrown. If cppobj is NULL, a bad_typeid exception
1113 * is thrown. In either case, this function does not return.
1116 * This function is usually called by compiler generated code as a result
1117 * of using one of the C++ dynamic cast statements.
1119 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
1121 const type_info *ret;
1126 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
1127 _CxxThrowException( &e, &bad_typeid_exception_type );
1133 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1134 ret = obj_locator->type_descriptor;
1138 __non_rtti_object e;
1139 MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
1140 _CxxThrowException( &e, &bad_typeid_exception_type );
1147 /******************************************************************
1148 * __RTDynamicCast (MSVCRT.@)
1150 * Dynamically cast a C++ object to one of its base classes.
1153 * cppobj [I] Any C++ object to cast
1154 * unknown [I] Reserved, set to 0
1155 * src [I] type_info object describing cppobj
1156 * dst [I] type_info object describing the base class to cast to
1157 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
1160 * Success: The address of cppobj, cast to the object described by dst.
1161 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
1162 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
1163 * is thrown and this function does not return.
1166 * This function is usually called by compiler generated code as a result
1167 * of using one of the C++ dynamic cast statements.
1169 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
1170 type_info *src, type_info *dst,
1175 if (!cppobj) return NULL;
1177 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
1178 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
1180 /* To cast an object at runtime:
1181 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
1182 * 2.Search for the destination type in the class hierarchy
1183 * 3.If destination type is found, return base object address + dest offset
1184 * Otherwise, fail the cast
1186 * FIXME: the unknown parameter doesn't seem to be used for anything
1191 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1192 const rtti_object_hierarchy *obj_bases = obj_locator->type_hierarchy;
1193 const rtti_base_descriptor * const* base_desc = obj_bases->base_classes->bases;
1195 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
1198 for (i = 0; i < obj_bases->array_len; i++)
1200 const type_info *typ = base_desc[i]->type_descriptor;
1202 if (!strcmp(typ->mangled, dst->mangled))
1204 /* compute the correct this pointer for that base class */
1205 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
1206 ret = get_this_pointer( &base_desc[i]->offsets, this_ptr );
1210 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
1211 * to a reference, since references cannot be NULL.
1213 if (!ret && do_throw)
1215 const char *msg = "Bad dynamic_cast!";
1217 MSVCRT_bad_cast_ctor( &e, &msg );
1218 _CxxThrowException( &e, &bad_cast_exception_type );
1223 __non_rtti_object e;
1224 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1225 _CxxThrowException( &e, &bad_typeid_exception_type );
1233 /******************************************************************
1234 * __RTCastToVoid (MSVCRT.@)
1236 * Dynamically cast a C++ object to a void*.
1239 * cppobj [I] The C++ object to cast
1242 * Success: The base address of the object as a void*.
1243 * Failure: NULL, if cppobj is NULL or has no RTTI.
1246 * This function is usually called by compiler generated code as a result
1247 * of using one of the C++ dynamic cast statements.
1249 void* CDECL MSVCRT___RTCastToVoid(void *cppobj)
1253 if (!cppobj) return NULL;
1257 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1258 ret = (char *)cppobj - obj_locator->base_class_offset;
1262 __non_rtti_object e;
1263 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1264 _CxxThrowException( &e, &bad_typeid_exception_type );