winedbg: Add control operators to Thumb2 disassembler.
[wine.git] / dlls / msvcrt / cpp.c
blob8a8af42d2294086a3adb4f5883b7d5ea8582b345
1 /*
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winternl.h"
29 #include "wine/exception.h"
30 #include "wine/debug.h"
31 #include "msvcrt.h"
32 #include "cppexcept.h"
33 #include "mtdll.h"
34 #include "cxx.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
38 typedef exception bad_cast;
39 typedef exception bad_typeid;
40 typedef exception __non_rtti_object;
42 extern const vtable_ptr MSVCRT_exception_vtable;
43 extern const vtable_ptr MSVCRT_bad_typeid_vtable;
44 extern const vtable_ptr MSVCRT_bad_cast_vtable;
45 extern const vtable_ptr MSVCRT___non_rtti_object_vtable;
46 extern const vtable_ptr MSVCRT_type_info_vtable;
48 /* get the vtable pointer for a C++ object */
49 static inline const vtable_ptr *get_vtable( void *obj )
51 return *(const vtable_ptr **)obj;
54 static inline const rtti_object_locator *get_obj_locator( void *cppobj )
56 const vtable_ptr *vtable = get_vtable( cppobj );
57 return (const rtti_object_locator *)vtable[-1];
60 #ifndef __x86_64__
61 static void dump_obj_locator( const rtti_object_locator *ptr )
63 int i;
64 const rtti_object_hierarchy *h = ptr->type_hierarchy;
66 TRACE( "%p: sig=%08x base_offset=%08x flags=%08x type=%p %s hierarchy=%p\n",
67 ptr, ptr->signature, ptr->base_class_offset, ptr->flags,
68 ptr->type_descriptor, dbgstr_type_info(ptr->type_descriptor), ptr->type_hierarchy );
69 TRACE( " hierarchy: sig=%08x attr=%08x len=%d base classes=%p\n",
70 h->signature, h->attributes, h->array_len, h->base_classes );
71 for (i = 0; i < h->array_len; i++)
73 TRACE( " base class %p: num %d off %d,%d,%d attr %08x type %p %s\n",
74 h->base_classes->bases[i],
75 h->base_classes->bases[i]->num_base_classes,
76 h->base_classes->bases[i]->offsets.this_offset,
77 h->base_classes->bases[i]->offsets.vbase_descr,
78 h->base_classes->bases[i]->offsets.vbase_offset,
79 h->base_classes->bases[i]->attributes,
80 h->base_classes->bases[i]->type_descriptor,
81 dbgstr_type_info(h->base_classes->bases[i]->type_descriptor) );
85 #else
87 static void dump_obj_locator( const rtti_object_locator *ptr )
89 int i;
90 char *base = ptr->signature == 0 ? (char*)RtlPcToFileHeader((void*)ptr, (void**)&base) : (char*)ptr - ptr->object_locator;
91 const rtti_object_hierarchy *h = (const rtti_object_hierarchy*)(base + ptr->type_hierarchy);
92 const type_info *type_descriptor = (const type_info*)(base + ptr->type_descriptor);
94 TRACE( "%p: sig=%08x base_offset=%08x flags=%08x type=%p %s hierarchy=%p\n",
95 ptr, ptr->signature, ptr->base_class_offset, ptr->flags,
96 type_descriptor, dbgstr_type_info(type_descriptor), h );
97 TRACE( " hierarchy: sig=%08x attr=%08x len=%d base classes=%p\n",
98 h->signature, h->attributes, h->array_len, base + h->base_classes );
99 for (i = 0; i < h->array_len; i++)
101 const rtti_base_descriptor *bases = (rtti_base_descriptor*)(base +
102 ((const rtti_base_array*)(base + h->base_classes))->bases[i]);
104 TRACE( " base class %p: num %d off %d,%d,%d attr %08x type %p %s\n",
105 bases,
106 bases->num_base_classes,
107 bases->offsets.this_offset,
108 bases->offsets.vbase_descr,
109 bases->offsets.vbase_offset,
110 bases->attributes,
111 base + bases->type_descriptor,
112 dbgstr_type_info((const type_info*)(base + bases->type_descriptor)) );
115 #endif
117 /* Internal common ctor for exception */
118 static void EXCEPTION_ctor(exception *_this, const char** name)
120 _this->vtable = &MSVCRT_exception_vtable;
121 if (*name)
123 unsigned int name_len = strlen(*name) + 1;
124 _this->name = MSVCRT_malloc(name_len);
125 memcpy(_this->name, *name, name_len);
126 _this->do_free = TRUE;
128 else
130 _this->name = NULL;
131 _this->do_free = FALSE;
135 /******************************************************************
136 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
138 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor,8)
139 exception * __thiscall MSVCRT_exception_ctor(exception * _this, const char ** name)
141 TRACE("(%p,%s)\n", _this, *name);
142 EXCEPTION_ctor(_this, name);
143 return _this;
146 /******************************************************************
147 * ??0exception@@QAE@ABQBDH@Z (MSVCRT.@)
149 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor_noalloc,12)
150 exception * __thiscall MSVCRT_exception_ctor_noalloc(exception * _this, char ** name, int noalloc)
152 TRACE("(%p,%s)\n", _this, *name);
153 _this->vtable = &MSVCRT_exception_vtable;
154 _this->name = *name;
155 _this->do_free = FALSE;
156 return _this;
159 /******************************************************************
160 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
162 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor,8)
163 exception * __thiscall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
165 TRACE("(%p,%p)\n", _this, rhs);
167 if (!rhs->do_free)
169 _this->vtable = &MSVCRT_exception_vtable;
170 _this->name = rhs->name;
171 _this->do_free = FALSE;
173 else
174 EXCEPTION_ctor(_this, (const char**)&rhs->name);
175 TRACE("name = %s\n", _this->name);
176 return _this;
179 /******************************************************************
180 * ??0exception@@QAE@XZ (MSVCRT.@)
182 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor,4)
183 exception * __thiscall MSVCRT_exception_default_ctor(exception * _this)
185 static const char* empty = NULL;
187 TRACE("(%p)\n", _this);
188 EXCEPTION_ctor(_this, &empty);
189 return _this;
192 /******************************************************************
193 * ??1exception@@UAE@XZ (MSVCRT.@)
195 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor,4)
196 void __thiscall MSVCRT_exception_dtor(exception * _this)
198 TRACE("(%p)\n", _this);
199 _this->vtable = &MSVCRT_exception_vtable;
200 if (_this->do_free) MSVCRT_free(_this->name);
203 /******************************************************************
204 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
206 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals,8)
207 exception * __thiscall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
209 TRACE("(%p %p)\n", _this, rhs);
210 if (_this != rhs)
212 MSVCRT_exception_dtor(_this);
213 MSVCRT_exception_copy_ctor(_this, rhs);
215 TRACE("name = %s\n", _this->name);
216 return _this;
219 /******************************************************************
220 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
222 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor,8)
223 void * __thiscall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
225 TRACE("(%p %x)\n", _this, flags);
226 if (flags & 2)
228 /* we have an array, with the number of elements stored before the first object */
229 INT_PTR i, *ptr = (INT_PTR *)_this - 1;
231 for (i = *ptr - 1; i >= 0; i--) MSVCRT_exception_dtor(_this + i);
232 MSVCRT_operator_delete(ptr);
234 else
236 MSVCRT_exception_dtor(_this);
237 if (flags & 1) MSVCRT_operator_delete(_this);
239 return _this;
242 /******************************************************************
243 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
245 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor,8)
246 void * __thiscall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
248 TRACE("(%p %x)\n", _this, flags);
249 MSVCRT_exception_dtor(_this);
250 if (flags & 1) MSVCRT_operator_delete(_this);
251 return _this;
254 /******************************************************************
255 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
257 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception,4)
258 const char * __thiscall MSVCRT_what_exception(exception * _this)
260 TRACE("(%p) returning %s\n", _this, _this->name);
261 return _this->name ? _this->name : "Unknown exception";
264 /******************************************************************
265 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
267 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor,8)
268 bad_typeid * __thiscall MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
270 TRACE("(%p %p)\n", _this, rhs);
271 MSVCRT_exception_copy_ctor(_this, rhs);
272 _this->vtable = &MSVCRT_bad_typeid_vtable;
273 return _this;
276 /******************************************************************
277 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
279 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor,8)
280 bad_typeid * __thiscall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
282 TRACE("(%p %s)\n", _this, name);
283 EXCEPTION_ctor(_this, &name);
284 _this->vtable = &MSVCRT_bad_typeid_vtable;
285 return _this;
288 /******************************************************************
289 * ??_Fbad_typeid@@QAEXXZ (MSVCRT.@)
291 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_default_ctor,4)
292 bad_typeid * __thiscall MSVCRT_bad_typeid_default_ctor(bad_typeid * _this)
294 return MSVCRT_bad_typeid_ctor( _this, "bad typeid" );
297 /******************************************************************
298 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
300 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor,4)
301 void __thiscall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
303 TRACE("(%p)\n", _this);
304 MSVCRT_exception_dtor(_this);
307 /******************************************************************
308 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
310 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals,8)
311 bad_typeid * __thiscall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
313 TRACE("(%p %p)\n", _this, rhs);
314 MSVCRT_exception_opequals(_this, rhs);
315 return _this;
318 /******************************************************************
319 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
321 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor,8)
322 void * __thiscall MSVCRT_bad_typeid_vector_dtor(bad_typeid * _this, unsigned int flags)
324 TRACE("(%p %x)\n", _this, flags);
325 if (flags & 2)
327 /* we have an array, with the number of elements stored before the first object */
328 INT_PTR i, *ptr = (INT_PTR *)_this - 1;
330 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_typeid_dtor(_this + i);
331 MSVCRT_operator_delete(ptr);
333 else
335 MSVCRT_bad_typeid_dtor(_this);
336 if (flags & 1) MSVCRT_operator_delete(_this);
338 return _this;
341 /******************************************************************
342 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
344 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor,8)
345 void * __thiscall MSVCRT_bad_typeid_scalar_dtor(bad_typeid * _this, unsigned int flags)
347 TRACE("(%p %x)\n", _this, flags);
348 MSVCRT_bad_typeid_dtor(_this);
349 if (flags & 1) MSVCRT_operator_delete(_this);
350 return _this;
353 /******************************************************************
354 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
356 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor,8)
357 __non_rtti_object * __thiscall MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
358 const __non_rtti_object * rhs)
360 TRACE("(%p %p)\n", _this, rhs);
361 MSVCRT_bad_typeid_copy_ctor(_this, rhs);
362 _this->vtable = &MSVCRT___non_rtti_object_vtable;
363 return _this;
366 /******************************************************************
367 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
369 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor,8)
370 __non_rtti_object * __thiscall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
371 const char * name)
373 TRACE("(%p %s)\n", _this, name);
374 EXCEPTION_ctor(_this, &name);
375 _this->vtable = &MSVCRT___non_rtti_object_vtable;
376 return _this;
379 /******************************************************************
380 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
382 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor,4)
383 void __thiscall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
385 TRACE("(%p)\n", _this);
386 MSVCRT_bad_typeid_dtor(_this);
389 /******************************************************************
390 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
392 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals,8)
393 __non_rtti_object * __thiscall MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
394 const __non_rtti_object *rhs)
396 TRACE("(%p %p)\n", _this, rhs);
397 MSVCRT_bad_typeid_opequals(_this, rhs);
398 return _this;
401 /******************************************************************
402 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
404 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor,8)
405 void * __thiscall MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object * _this, unsigned int flags)
407 TRACE("(%p %x)\n", _this, flags);
408 if (flags & 2)
410 /* we have an array, with the number of elements stored before the first object */
411 INT_PTR i, *ptr = (INT_PTR *)_this - 1;
413 for (i = *ptr - 1; i >= 0; i--) MSVCRT___non_rtti_object_dtor(_this + i);
414 MSVCRT_operator_delete(ptr);
416 else
418 MSVCRT___non_rtti_object_dtor(_this);
419 if (flags & 1) MSVCRT_operator_delete(_this);
421 return _this;
424 /******************************************************************
425 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
427 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor,8)
428 void * __thiscall MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object * _this, unsigned int flags)
430 TRACE("(%p %x)\n", _this, flags);
431 MSVCRT___non_rtti_object_dtor(_this);
432 if (flags & 1) MSVCRT_operator_delete(_this);
433 return _this;
436 /******************************************************************
437 * ??0bad_cast@@AAE@PBQBD@Z (MSVCRT.@)
438 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
440 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor,8)
441 bad_cast * __thiscall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
443 TRACE("(%p %s)\n", _this, *name);
444 EXCEPTION_ctor(_this, name);
445 _this->vtable = &MSVCRT_bad_cast_vtable;
446 return _this;
449 /******************************************************************
450 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
452 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor,8)
453 bad_cast * __thiscall MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
455 TRACE("(%p %p)\n", _this, rhs);
456 MSVCRT_exception_copy_ctor(_this, rhs);
457 _this->vtable = &MSVCRT_bad_cast_vtable;
458 return _this;
461 /******************************************************************
462 * ??0bad_cast@@QAE@PBD@Z (MSVCRT.@)
464 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor_charptr,8)
465 bad_cast * __thiscall MSVCRT_bad_cast_ctor_charptr(bad_cast * _this, const char * name)
467 TRACE("(%p %s)\n", _this, name);
468 EXCEPTION_ctor(_this, &name);
469 _this->vtable = &MSVCRT_bad_cast_vtable;
470 return _this;
473 /******************************************************************
474 * ??_Fbad_cast@@QAEXXZ (MSVCRT.@)
476 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_default_ctor,4)
477 bad_cast * __thiscall MSVCRT_bad_cast_default_ctor(bad_cast * _this)
479 return MSVCRT_bad_cast_ctor_charptr( _this, "bad cast" );
482 /******************************************************************
483 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
485 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor,4)
486 void __thiscall MSVCRT_bad_cast_dtor(bad_cast * _this)
488 TRACE("(%p)\n", _this);
489 MSVCRT_exception_dtor(_this);
492 /******************************************************************
493 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
495 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals,8)
496 bad_cast * __thiscall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
498 TRACE("(%p %p)\n", _this, rhs);
499 MSVCRT_exception_opequals(_this, rhs);
500 return _this;
503 /******************************************************************
504 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
506 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor,8)
507 void * __thiscall MSVCRT_bad_cast_vector_dtor(bad_cast * _this, unsigned int flags)
509 TRACE("(%p %x)\n", _this, flags);
510 if (flags & 2)
512 /* we have an array, with the number of elements stored before the first object */
513 INT_PTR i, *ptr = (INT_PTR *)_this - 1;
515 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_cast_dtor(_this + i);
516 MSVCRT_operator_delete(ptr);
518 else
520 MSVCRT_bad_cast_dtor(_this);
521 if (flags & 1) MSVCRT_operator_delete(_this);
523 return _this;
526 /******************************************************************
527 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
529 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor,8)
530 void * __thiscall MSVCRT_bad_cast_scalar_dtor(bad_cast * _this, unsigned int flags)
532 TRACE("(%p %x)\n", _this, flags);
533 MSVCRT_bad_cast_dtor(_this);
534 if (flags & 1) MSVCRT_operator_delete(_this);
535 return _this;
538 /******************************************************************
539 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
541 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals,8)
542 int __thiscall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
544 int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
545 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
546 return ret;
549 /******************************************************************
550 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
552 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals,8)
553 int __thiscall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
555 int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
556 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
557 return ret;
560 /******************************************************************
561 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
563 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before,8)
564 int __thiscall MSVCRT_type_info_before(type_info * _this, const type_info * rhs)
566 int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
567 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
568 return ret;
571 /******************************************************************
572 * ??1type_info@@UAE@XZ (MSVCRT.@)
574 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor,4)
575 void __thiscall MSVCRT_type_info_dtor(type_info * _this)
577 TRACE("(%p)\n", _this);
578 MSVCRT_free(_this->name);
581 /******************************************************************
582 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
584 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name,4)
585 const char * __thiscall MSVCRT_type_info_name(type_info * _this)
587 if (!_this->name)
589 /* Create and set the demangled name */
590 /* Note: mangled name in type_info struct always starts with a '.', while
591 * it isn't valid for mangled name.
592 * Is this '.' really part of the mangled name, or has it some other meaning ?
594 char* name = __unDName(0, _this->mangled + 1, 0,
595 MSVCRT_malloc, MSVCRT_free, 0x2800);
596 if (name)
598 unsigned int len = strlen(name);
600 /* It seems _unDName may leave blanks at the end of the demangled name */
601 while (len && name[--len] == ' ')
602 name[len] = '\0';
604 if (InterlockedCompareExchangePointer((void**)&_this->name, name, NULL))
606 /* Another thread set this member since we checked above - use it */
607 MSVCRT_free(name);
611 TRACE("(%p) returning %s\n", _this, _this->name);
612 return _this->name;
615 /******************************************************************
616 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
618 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name,4)
619 const char * __thiscall MSVCRT_type_info_raw_name(type_info * _this)
621 TRACE("(%p) returning %s\n", _this, _this->mangled);
622 return _this->mangled;
625 /* Unexported */
626 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor,8)
627 void * __thiscall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
629 TRACE("(%p %x)\n", _this, flags);
630 if (flags & 2)
632 /* we have an array, with the number of elements stored before the first object */
633 INT_PTR i, *ptr = (INT_PTR *)_this - 1;
635 for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
636 MSVCRT_operator_delete(ptr);
638 else
640 MSVCRT_type_info_dtor(_this);
641 if (flags & 1) MSVCRT_operator_delete(_this);
643 return _this;
646 #ifndef __GNUC__
647 void __asm_dummy_vtables(void) {
648 #endif
650 __ASM_VTABLE(type_info,
651 VTABLE_ADD_FUNC(MSVCRT_type_info_vector_dtor));
652 __ASM_VTABLE(exception,
653 VTABLE_ADD_FUNC(MSVCRT_exception_vector_dtor)
654 VTABLE_ADD_FUNC(MSVCRT_what_exception));
655 __ASM_VTABLE(bad_typeid,
656 VTABLE_ADD_FUNC(MSVCRT_bad_typeid_vector_dtor)
657 VTABLE_ADD_FUNC(MSVCRT_what_exception));
658 __ASM_VTABLE(bad_cast,
659 VTABLE_ADD_FUNC(MSVCRT_bad_cast_vector_dtor)
660 VTABLE_ADD_FUNC(MSVCRT_what_exception));
661 __ASM_VTABLE(__non_rtti_object,
662 VTABLE_ADD_FUNC(MSVCRT___non_rtti_object_vector_dtor)
663 VTABLE_ADD_FUNC(MSVCRT_what_exception));
665 #ifndef __GNUC__
667 #endif
669 #define DEFINE_EXCEPTION_TYPE_INFO(name, base_classes, cl1, cl2) \
670 static const cxx_type_info name ## _cxx_type_info = \
672 0, \
673 &name ## _type_info, \
674 { 0, -1, 0 }, \
675 sizeof(name), \
676 (cxx_copy_ctor)THISCALL(MSVCRT_ ## name ## _copy_ctor) \
677 }; \
679 static const cxx_type_info_table name ## _type_info_table = \
681 base_classes+1, \
683 &name ## _cxx_type_info, \
684 cl1, \
685 cl2 \
687 }; \
689 const cxx_exception_type name ## _exception_type = \
691 0, \
692 (void *)THISCALL(MSVCRT_ ## name ## _dtor), \
693 NULL, \
694 &name ## _type_info_table \
697 DEFINE_RTTI_DATA0( type_info, 0, ".?AVtype_info@@" );
698 DEFINE_RTTI_DATA0( exception, 0, ".?AVexception@@" );
699 DEFINE_RTTI_DATA1( bad_typeid, 0, &exception_rtti_base_descriptor, ".?AVbad_typeid@@" );
700 DEFINE_RTTI_DATA1( bad_cast, 0, &exception_rtti_base_descriptor, ".?AVbad_cast@@" );
701 DEFINE_RTTI_DATA2( __non_rtti_object, 0, &bad_typeid_rtti_base_descriptor, &exception_rtti_base_descriptor, ".?AV__non_rtti_object@@" );
703 DEFINE_EXCEPTION_TYPE_INFO( exception, 0, NULL, NULL );
704 DEFINE_EXCEPTION_TYPE_INFO( bad_typeid, 1, &exception_cxx_type_info, NULL );
705 DEFINE_EXCEPTION_TYPE_INFO( bad_cast, 1, &exception_cxx_type_info, NULL );
706 DEFINE_EXCEPTION_TYPE_INFO( __non_rtti_object, 2, &bad_typeid_cxx_type_info, &exception_cxx_type_info );
708 void msvcrt_init_exception(void *base)
710 #ifdef __x86_64__
711 init_type_info_rtti(base);
712 init_exception_rtti(base);
713 init_bad_typeid_rtti(base);
714 init_bad_cast_rtti(base);
715 init___non_rtti_object_rtti(base);
716 #endif
720 /******************************************************************
721 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
723 * Install a handler to be called when terminate() is called.
725 * PARAMS
726 * func [I] Handler function to install
728 * RETURNS
729 * The previously installed handler function, if any.
731 MSVCRT_terminate_function CDECL MSVCRT_set_terminate(MSVCRT_terminate_function func)
733 thread_data_t *data = msvcrt_get_thread_data();
734 MSVCRT_terminate_function previous = data->terminate_handler;
735 TRACE("(%p) returning %p\n",func,previous);
736 data->terminate_handler = func;
737 return previous;
740 /******************************************************************
741 * _get_terminate (MSVCRT.@)
743 MSVCRT_terminate_function CDECL MSVCRT__get_terminate(void)
745 thread_data_t *data = msvcrt_get_thread_data();
746 TRACE("returning %p\n", data->terminate_handler);
747 return data->terminate_handler;
750 /******************************************************************
751 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
753 * Install a handler to be called when unexpected() is called.
755 * PARAMS
756 * func [I] Handler function to install
758 * RETURNS
759 * The previously installed handler function, if any.
761 MSVCRT_unexpected_function CDECL MSVCRT_set_unexpected(MSVCRT_unexpected_function func)
763 thread_data_t *data = msvcrt_get_thread_data();
764 MSVCRT_unexpected_function previous = data->unexpected_handler;
765 TRACE("(%p) returning %p\n",func,previous);
766 data->unexpected_handler = func;
767 return previous;
770 /******************************************************************
771 * _get_unexpected (MSVCRT.@)
773 MSVCRT_unexpected_function CDECL MSVCRT__get_unexpected(void)
775 thread_data_t *data = msvcrt_get_thread_data();
776 TRACE("returning %p\n", data->unexpected_handler);
777 return data->unexpected_handler;
780 /******************************************************************
781 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
783 MSVCRT__se_translator_function CDECL MSVCRT__set_se_translator(MSVCRT__se_translator_function func)
785 thread_data_t *data = msvcrt_get_thread_data();
786 MSVCRT__se_translator_function previous = data->se_translator;
787 TRACE("(%p) returning %p\n",func,previous);
788 data->se_translator = func;
789 return previous;
792 /******************************************************************
793 * ?terminate@@YAXXZ (MSVCRT.@)
795 * Default handler for an unhandled exception.
797 * PARAMS
798 * None.
800 * RETURNS
801 * This function does not return. Either control resumes from any
802 * handler installed by calling set_terminate(), or (by default) abort()
803 * is called.
805 void CDECL MSVCRT_terminate(void)
807 thread_data_t *data = msvcrt_get_thread_data();
808 if (data->terminate_handler) data->terminate_handler();
809 MSVCRT_abort();
812 /******************************************************************
813 * ?unexpected@@YAXXZ (MSVCRT.@)
815 void CDECL MSVCRT_unexpected(void)
817 thread_data_t *data = msvcrt_get_thread_data();
818 if (data->unexpected_handler) data->unexpected_handler();
819 MSVCRT_terminate();
823 /******************************************************************
824 * __RTtypeid (MSVCRT.@)
826 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
828 * PARAMS
829 * cppobj [I] C++ object to get type information for.
831 * RETURNS
832 * Success: A type_info object describing cppobj.
833 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
834 * exception is thrown. If cppobj is NULL, a bad_typeid exception
835 * is thrown. In either case, this function does not return.
837 * NOTES
838 * This function is usually called by compiler generated code as a result
839 * of using one of the C++ dynamic cast statements.
841 #ifndef __x86_64__
842 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
844 const type_info *ret;
846 if (!cppobj)
848 bad_typeid e;
849 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
850 _CxxThrowException( &e, &bad_typeid_exception_type );
851 return NULL;
854 __TRY
856 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
857 ret = obj_locator->type_descriptor;
859 __EXCEPT_PAGE_FAULT
861 __non_rtti_object e;
862 MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
863 _CxxThrowException( &e, &bad_typeid_exception_type );
864 return NULL;
866 __ENDTRY
867 return ret;
870 #else
872 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
874 const type_info *ret;
876 if (!cppobj)
878 bad_typeid e;
879 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
880 _CxxThrowException( &e, &bad_typeid_exception_type );
881 return NULL;
884 __TRY
886 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
887 char *base;
889 if(obj_locator->signature == 0)
890 base = (char*)RtlPcToFileHeader((void*)obj_locator, (void**)&base);
891 else
892 base = (char*)obj_locator - obj_locator->object_locator;
894 ret = (type_info*)(base + obj_locator->type_descriptor);
896 __EXCEPT_PAGE_FAULT
898 __non_rtti_object e;
899 MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
900 _CxxThrowException( &e, &bad_typeid_exception_type );
901 return NULL;
903 __ENDTRY
904 return ret;
906 #endif
908 /******************************************************************
909 * __RTDynamicCast (MSVCRT.@)
911 * Dynamically cast a C++ object to one of its base classes.
913 * PARAMS
914 * cppobj [I] Any C++ object to cast
915 * unknown [I] Reserved, set to 0
916 * src [I] type_info object describing cppobj
917 * dst [I] type_info object describing the base class to cast to
918 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
920 * RETURNS
921 * Success: The address of cppobj, cast to the object described by dst.
922 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
923 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
924 * is thrown and this function does not return.
926 * NOTES
927 * This function is usually called by compiler generated code as a result
928 * of using one of the C++ dynamic cast statements.
930 #ifndef __x86_64__
931 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
932 type_info *src, type_info *dst,
933 int do_throw)
935 void *ret;
937 if (!cppobj) return NULL;
939 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
940 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
942 /* To cast an object at runtime:
943 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
944 * 2.Search for the destination type in the class hierarchy
945 * 3.If destination type is found, return base object address + dest offset
946 * Otherwise, fail the cast
948 * FIXME: the unknown parameter doesn't seem to be used for anything
950 __TRY
952 int i;
953 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
954 const rtti_object_hierarchy *obj_bases = obj_locator->type_hierarchy;
955 const rtti_base_descriptor * const* base_desc = obj_bases->base_classes->bases;
957 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
959 ret = NULL;
960 for (i = 0; i < obj_bases->array_len; i++)
962 const type_info *typ = base_desc[i]->type_descriptor;
964 if (!strcmp(typ->mangled, dst->mangled))
966 /* compute the correct this pointer for that base class */
967 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
968 ret = get_this_pointer( &base_desc[i]->offsets, this_ptr );
969 break;
972 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
973 * to a reference, since references cannot be NULL.
975 if (!ret && do_throw)
977 const char *msg = "Bad dynamic_cast!";
978 bad_cast e;
979 MSVCRT_bad_cast_ctor( &e, &msg );
980 _CxxThrowException( &e, &bad_cast_exception_type );
983 __EXCEPT_PAGE_FAULT
985 __non_rtti_object e;
986 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
987 _CxxThrowException( &e, &bad_typeid_exception_type );
988 return NULL;
990 __ENDTRY
991 return ret;
994 #else
996 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
997 type_info *src, type_info *dst,
998 int do_throw)
1000 void *ret;
1002 if (!cppobj) return NULL;
1004 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
1005 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
1007 __TRY
1009 int i;
1010 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1011 const rtti_object_hierarchy *obj_bases;
1012 const rtti_base_array *base_array;
1013 char *base;
1015 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
1017 if(obj_locator->signature == 0)
1018 base = (char*)RtlPcToFileHeader((void*)obj_locator, (void**)&base);
1019 else
1020 base = (char*)obj_locator - obj_locator->object_locator;
1022 obj_bases = (const rtti_object_hierarchy*)(base + obj_locator->type_hierarchy);
1023 base_array = (const rtti_base_array*)(base + obj_bases->base_classes);
1025 ret = NULL;
1026 for (i = 0; i < obj_bases->array_len; i++)
1028 const rtti_base_descriptor *base_desc = (const rtti_base_descriptor*)(base + base_array->bases[i]);
1029 const type_info *typ = (const type_info*)(base + base_desc->type_descriptor);
1031 if (!strcmp(typ->mangled, dst->mangled))
1033 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
1034 ret = get_this_pointer( &base_desc->offsets, this_ptr );
1035 break;
1038 if (!ret && do_throw)
1040 const char *msg = "Bad dynamic_cast!";
1041 bad_cast e;
1042 MSVCRT_bad_cast_ctor( &e, &msg );
1043 _CxxThrowException( &e, &bad_cast_exception_type );
1046 __EXCEPT_PAGE_FAULT
1048 __non_rtti_object e;
1049 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1050 _CxxThrowException( &e, &bad_typeid_exception_type );
1051 return NULL;
1053 __ENDTRY
1054 return ret;
1056 #endif
1059 /******************************************************************
1060 * __RTCastToVoid (MSVCRT.@)
1062 * Dynamically cast a C++ object to a void*.
1064 * PARAMS
1065 * cppobj [I] The C++ object to cast
1067 * RETURNS
1068 * Success: The base address of the object as a void*.
1069 * Failure: NULL, if cppobj is NULL or has no RTTI.
1071 * NOTES
1072 * This function is usually called by compiler generated code as a result
1073 * of using one of the C++ dynamic cast statements.
1075 void* CDECL MSVCRT___RTCastToVoid(void *cppobj)
1077 void *ret;
1079 if (!cppobj) return NULL;
1081 __TRY
1083 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1084 ret = (char *)cppobj - obj_locator->base_class_offset;
1086 __EXCEPT_PAGE_FAULT
1088 __non_rtti_object e;
1089 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1090 _CxxThrowException( &e, &bad_typeid_exception_type );
1091 return NULL;
1093 __ENDTRY
1094 return ret;
1098 /*********************************************************************
1099 * _CxxThrowException (MSVCRT.@)
1101 void WINAPI _CxxThrowException( exception *object, const cxx_exception_type *type )
1103 ULONG_PTR args[3];
1105 args[0] = CXX_FRAME_MAGIC_VC6;
1106 args[1] = (ULONG_PTR)object;
1107 args[2] = (ULONG_PTR)type;
1108 RaiseException( CXX_EXCEPTION, EH_NONCONTINUABLE, 3, args );