comctl32/tests: Use BOOL type where appropriate.
[wine.git] / dlls / msvcrt / cpp.c
bloba3ee6b9632761a49d51d9eeccf560ddf6b604611
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 ? 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, UNDNAME_NO_ARGUMENTS | UNDNAME_32_BIT_DECODE);
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_RTTI_DATA0( type_info, 0, ".?AVtype_info@@" )
670 DEFINE_RTTI_DATA0( exception, 0, ".?AVexception@@" )
671 DEFINE_RTTI_DATA1( bad_typeid, 0, &exception_rtti_base_descriptor, ".?AVbad_typeid@@" )
672 DEFINE_RTTI_DATA1( bad_cast, 0, &exception_rtti_base_descriptor, ".?AVbad_cast@@" )
673 DEFINE_RTTI_DATA2( __non_rtti_object, 0, &bad_typeid_rtti_base_descriptor, &exception_rtti_base_descriptor, ".?AV__non_rtti_object@@" )
675 DEFINE_EXCEPTION_TYPE_INFO( exception, 0, NULL, NULL )
676 DEFINE_EXCEPTION_TYPE_INFO( bad_typeid, 1, &exception_cxx_type_info, NULL )
677 DEFINE_EXCEPTION_TYPE_INFO( bad_cast, 1, &exception_cxx_type_info, NULL )
678 DEFINE_EXCEPTION_TYPE_INFO( __non_rtti_object, 2, &bad_typeid_cxx_type_info, &exception_cxx_type_info )
680 void msvcrt_init_exception(void *base)
682 #ifdef __x86_64__
683 init_type_info_rtti(base);
684 init_exception_rtti(base);
685 init_bad_typeid_rtti(base);
686 init_bad_cast_rtti(base);
687 init___non_rtti_object_rtti(base);
689 init_exception_cxx(base);
690 init_bad_typeid_cxx(base);
691 init_bad_cast_cxx(base);
692 init___non_rtti_object_cxx(base);
693 #endif
697 /******************************************************************
698 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
700 * Install a handler to be called when terminate() is called.
702 * PARAMS
703 * func [I] Handler function to install
705 * RETURNS
706 * The previously installed handler function, if any.
708 MSVCRT_terminate_function CDECL MSVCRT_set_terminate(MSVCRT_terminate_function func)
710 thread_data_t *data = msvcrt_get_thread_data();
711 MSVCRT_terminate_function previous = data->terminate_handler;
712 TRACE("(%p) returning %p\n",func,previous);
713 data->terminate_handler = func;
714 return previous;
717 /******************************************************************
718 * _get_terminate (MSVCRT.@)
720 MSVCRT_terminate_function CDECL MSVCRT__get_terminate(void)
722 thread_data_t *data = msvcrt_get_thread_data();
723 TRACE("returning %p\n", data->terminate_handler);
724 return data->terminate_handler;
727 /******************************************************************
728 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
730 * Install a handler to be called when unexpected() is called.
732 * PARAMS
733 * func [I] Handler function to install
735 * RETURNS
736 * The previously installed handler function, if any.
738 MSVCRT_unexpected_function CDECL MSVCRT_set_unexpected(MSVCRT_unexpected_function func)
740 thread_data_t *data = msvcrt_get_thread_data();
741 MSVCRT_unexpected_function previous = data->unexpected_handler;
742 TRACE("(%p) returning %p\n",func,previous);
743 data->unexpected_handler = func;
744 return previous;
747 /******************************************************************
748 * _get_unexpected (MSVCRT.@)
750 MSVCRT_unexpected_function CDECL MSVCRT__get_unexpected(void)
752 thread_data_t *data = msvcrt_get_thread_data();
753 TRACE("returning %p\n", data->unexpected_handler);
754 return data->unexpected_handler;
757 /******************************************************************
758 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
760 MSVCRT__se_translator_function CDECL MSVCRT__set_se_translator(MSVCRT__se_translator_function func)
762 thread_data_t *data = msvcrt_get_thread_data();
763 MSVCRT__se_translator_function previous = data->se_translator;
764 TRACE("(%p) returning %p\n",func,previous);
765 data->se_translator = func;
766 return previous;
769 /******************************************************************
770 * ?terminate@@YAXXZ (MSVCRT.@)
772 * Default handler for an unhandled exception.
774 * PARAMS
775 * None.
777 * RETURNS
778 * This function does not return. Either control resumes from any
779 * handler installed by calling set_terminate(), or (by default) abort()
780 * is called.
782 void CDECL MSVCRT_terminate(void)
784 thread_data_t *data = msvcrt_get_thread_data();
785 if (data->terminate_handler) data->terminate_handler();
786 MSVCRT_abort();
789 /******************************************************************
790 * ?unexpected@@YAXXZ (MSVCRT.@)
792 void CDECL MSVCRT_unexpected(void)
794 thread_data_t *data = msvcrt_get_thread_data();
795 if (data->unexpected_handler) data->unexpected_handler();
796 MSVCRT_terminate();
800 /******************************************************************
801 * __RTtypeid (MSVCRT.@)
803 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
805 * PARAMS
806 * cppobj [I] C++ object to get type information for.
808 * RETURNS
809 * Success: A type_info object describing cppobj.
810 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
811 * exception is thrown. If cppobj is NULL, a bad_typeid exception
812 * is thrown. In either case, this function does not return.
814 * NOTES
815 * This function is usually called by compiler generated code as a result
816 * of using one of the C++ dynamic cast statements.
818 #ifndef __x86_64__
819 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
821 const type_info *ret;
823 if (!cppobj)
825 bad_typeid e;
826 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
827 _CxxThrowException( &e, &bad_typeid_exception_type );
828 return NULL;
831 __TRY
833 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
834 ret = obj_locator->type_descriptor;
836 __EXCEPT_PAGE_FAULT
838 __non_rtti_object e;
839 MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
840 _CxxThrowException( &e, &bad_typeid_exception_type );
841 return NULL;
843 __ENDTRY
844 return ret;
847 #else
849 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
851 const type_info *ret;
853 if (!cppobj)
855 bad_typeid e;
856 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
857 _CxxThrowException( &e, &bad_typeid_exception_type );
858 return NULL;
861 __TRY
863 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
864 char *base;
866 if(obj_locator->signature == 0)
867 base = RtlPcToFileHeader((void*)obj_locator, (void**)&base);
868 else
869 base = (char*)obj_locator - obj_locator->object_locator;
871 ret = (type_info*)(base + obj_locator->type_descriptor);
873 __EXCEPT_PAGE_FAULT
875 __non_rtti_object e;
876 MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
877 _CxxThrowException( &e, &bad_typeid_exception_type );
878 return NULL;
880 __ENDTRY
881 return ret;
883 #endif
885 /******************************************************************
886 * __RTDynamicCast (MSVCRT.@)
888 * Dynamically cast a C++ object to one of its base classes.
890 * PARAMS
891 * cppobj [I] Any C++ object to cast
892 * unknown [I] Reserved, set to 0
893 * src [I] type_info object describing cppobj
894 * dst [I] type_info object describing the base class to cast to
895 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
897 * RETURNS
898 * Success: The address of cppobj, cast to the object described by dst.
899 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
900 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
901 * is thrown and this function does not return.
903 * NOTES
904 * This function is usually called by compiler generated code as a result
905 * of using one of the C++ dynamic cast statements.
907 #ifndef __x86_64__
908 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
909 type_info *src, type_info *dst,
910 int do_throw)
912 void *ret;
914 if (!cppobj) return NULL;
916 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
917 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
919 /* To cast an object at runtime:
920 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
921 * 2.Search for the destination type in the class hierarchy
922 * 3.If destination type is found, return base object address + dest offset
923 * Otherwise, fail the cast
925 * FIXME: the unknown parameter doesn't seem to be used for anything
927 __TRY
929 int i;
930 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
931 const rtti_object_hierarchy *obj_bases = obj_locator->type_hierarchy;
932 const rtti_base_descriptor * const* base_desc = obj_bases->base_classes->bases;
934 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
936 ret = NULL;
937 for (i = 0; i < obj_bases->array_len; i++)
939 const type_info *typ = base_desc[i]->type_descriptor;
941 if (!strcmp(typ->mangled, dst->mangled))
943 /* compute the correct this pointer for that base class */
944 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
945 ret = get_this_pointer( &base_desc[i]->offsets, this_ptr );
946 break;
949 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
950 * to a reference, since references cannot be NULL.
952 if (!ret && do_throw)
954 const char *msg = "Bad dynamic_cast!";
955 bad_cast e;
956 MSVCRT_bad_cast_ctor( &e, &msg );
957 _CxxThrowException( &e, &bad_cast_exception_type );
960 __EXCEPT_PAGE_FAULT
962 __non_rtti_object e;
963 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
964 _CxxThrowException( &e, &bad_typeid_exception_type );
965 return NULL;
967 __ENDTRY
968 return ret;
971 #else
973 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
974 type_info *src, type_info *dst,
975 int do_throw)
977 void *ret;
979 if (!cppobj) return NULL;
981 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
982 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
984 __TRY
986 int i;
987 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
988 const rtti_object_hierarchy *obj_bases;
989 const rtti_base_array *base_array;
990 char *base;
992 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
994 if(obj_locator->signature == 0)
995 base = RtlPcToFileHeader((void*)obj_locator, (void**)&base);
996 else
997 base = (char*)obj_locator - obj_locator->object_locator;
999 obj_bases = (const rtti_object_hierarchy*)(base + obj_locator->type_hierarchy);
1000 base_array = (const rtti_base_array*)(base + obj_bases->base_classes);
1002 ret = NULL;
1003 for (i = 0; i < obj_bases->array_len; i++)
1005 const rtti_base_descriptor *base_desc = (const rtti_base_descriptor*)(base + base_array->bases[i]);
1006 const type_info *typ = (const type_info*)(base + base_desc->type_descriptor);
1008 if (!strcmp(typ->mangled, dst->mangled))
1010 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
1011 ret = get_this_pointer( &base_desc->offsets, this_ptr );
1012 break;
1015 if (!ret && do_throw)
1017 const char *msg = "Bad dynamic_cast!";
1018 bad_cast e;
1019 MSVCRT_bad_cast_ctor( &e, &msg );
1020 _CxxThrowException( &e, &bad_cast_exception_type );
1023 __EXCEPT_PAGE_FAULT
1025 __non_rtti_object e;
1026 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1027 _CxxThrowException( &e, &bad_typeid_exception_type );
1028 return NULL;
1030 __ENDTRY
1031 return ret;
1033 #endif
1036 /******************************************************************
1037 * __RTCastToVoid (MSVCRT.@)
1039 * Dynamically cast a C++ object to a void*.
1041 * PARAMS
1042 * cppobj [I] The C++ object to cast
1044 * RETURNS
1045 * Success: The base address of the object as a void*.
1046 * Failure: NULL, if cppobj is NULL or has no RTTI.
1048 * NOTES
1049 * This function is usually called by compiler generated code as a result
1050 * of using one of the C++ dynamic cast statements.
1052 void* CDECL MSVCRT___RTCastToVoid(void *cppobj)
1054 void *ret;
1056 if (!cppobj) return NULL;
1058 __TRY
1060 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1061 ret = (char *)cppobj - obj_locator->base_class_offset;
1063 __EXCEPT_PAGE_FAULT
1065 __non_rtti_object e;
1066 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1067 _CxxThrowException( &e, &bad_typeid_exception_type );
1068 return NULL;
1070 __ENDTRY
1071 return ret;
1075 /*********************************************************************
1076 * _CxxThrowException (MSVCRT.@)
1078 void WINAPI _CxxThrowException( exception *object, const cxx_exception_type *type )
1080 ULONG_PTR args[3];
1082 args[0] = CXX_FRAME_MAGIC_VC6;
1083 args[1] = (ULONG_PTR)object;
1084 args[2] = (ULONG_PTR)type;
1085 RaiseException( CXX_EXCEPTION, EH_NONCONTINUABLE, 3, args );