offsets array is the size of the wine data format so there is no need
[wine.git] / dlls / msvcrt / cpp.c
blob245d483397803226e6f1ac8b981e789e41155f5e
1 /*
2 * msvcrt.dll C++ objects
4 * Copyright 2000 Jon Griffiths
5 * Copyright 2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "winternl.h"
31 #include "wine/exception.h"
32 #include "excpt.h"
33 #include "wine/debug.h"
34 #include "msvcrt.h"
35 #include "cppexcept.h"
36 #include "mtdll.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
41 * exception object: base for exception, bad_cast, bad_typeid, __non_rtti_object
43 typedef struct
45 void* pfn_vector_dtor;
46 void* pfn_what;
47 } exception_vtable;
49 typedef struct __exception
51 const exception_vtable *vtable;
52 char *name; /* Name of this exception, always a new copy for each object */
53 int do_free; /* Whether to free 'name' in our dtor */
54 } exception;
56 typedef exception bad_cast;
57 typedef exception bad_typeid;
58 typedef exception __non_rtti_object;
60 typedef struct _rtti_base_descriptor
62 type_info *type_descriptor;
63 int num_base_classes;
64 int base_class_offset;
65 unsigned int flags;
66 int unknown1;
67 int unknown2;
68 } rtti_base_descriptor;
70 typedef struct _rtti_base_array
72 const rtti_base_descriptor *bases[3]; /* First element is the class itself */
73 } rtti_base_array;
75 typedef struct _rtti_object_hierachy
77 int unknown1;
78 int unknown2;
79 int array_len; /* Size of the array pointed to by 'base_classes' */
80 const rtti_base_array *base_classes;
81 } rtti_object_hierachy;
83 typedef struct _rtti_object_locator
85 int unknown1;
86 int base_class_offset;
87 unsigned int flags;
88 type_info *type_descriptor;
89 const rtti_object_hierachy *type_hierachy;
90 } rtti_object_locator;
93 #ifdef __i386__ /* thiscall functions are i386-specific */
95 #define DEFINE_THISCALL_WRAPPER(func) \
96 extern void __thiscall_ ## func(); \
97 __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
98 "popl %eax\n\t" \
99 "pushl %ecx\n\t" \
100 "pushl %eax\n\t" \
101 "jmp " __ASM_NAME(#func) )
103 const exception_vtable MSVCRT_exception_vtable;
104 const exception_vtable MSVCRT_bad_typeid_vtable;
105 const exception_vtable MSVCRT_bad_cast_vtable;
106 const exception_vtable MSVCRT___non_rtti_object_vtable;
107 static const exception_vtable MSVCRT_type_info_vtable;
109 /* Internal common ctor for exception */
110 static void WINAPI EXCEPTION_ctor(exception *_this, const char** name)
112 _this->vtable = &MSVCRT_exception_vtable;
113 if (*name)
115 size_t name_len = strlen(*name) + 1;
116 _this->name = MSVCRT_malloc(name_len);
117 memcpy(_this->name, *name, name_len);
118 _this->do_free = TRUE;
120 else
122 _this->name = NULL;
123 _this->do_free = FALSE;
127 /******************************************************************
128 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
130 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor);
131 exception * __stdcall MSVCRT_exception_ctor(exception * _this, const char ** name)
133 TRACE("(%p,%s)\n", _this, *name);
134 EXCEPTION_ctor(_this, name);
135 return _this;
138 /******************************************************************
139 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
141 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor);
142 exception * __stdcall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
144 TRACE("(%p,%p)\n", _this, rhs);
146 if (!rhs->do_free)
148 _this->vtable = &MSVCRT_exception_vtable;
149 _this->name = rhs->name;
150 _this->do_free = FALSE;
152 else
153 EXCEPTION_ctor(_this, (const char**)&rhs->name);
154 TRACE("name = %s\n", _this->name);
155 return _this;
158 /******************************************************************
159 * ??0exception@@QAE@XZ (MSVCRT.@)
161 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor);
162 exception * __stdcall MSVCRT_exception_default_ctor(exception * _this)
164 static const char* empty = NULL;
166 TRACE("(%p)\n", _this);
167 EXCEPTION_ctor(_this, &empty);
168 return _this;
171 /******************************************************************
172 * ??1exception@@UAE@XZ (MSVCRT.@)
174 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor);
175 void __stdcall MSVCRT_exception_dtor(exception * _this)
177 TRACE("(%p)\n", _this);
178 _this->vtable = &MSVCRT_exception_vtable;
179 if (_this->do_free) MSVCRT_free(_this->name);
182 /******************************************************************
183 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
185 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals);
186 exception * __stdcall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
188 TRACE("(%p %p)\n", _this, rhs);
189 if (_this != rhs)
191 MSVCRT_exception_dtor(_this);
192 MSVCRT_exception_copy_ctor(_this, rhs);
194 TRACE("name = %s\n", _this->name);
195 return _this;
198 /******************************************************************
199 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
201 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor);
202 void * __stdcall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
204 TRACE("(%p %x)\n", _this, flags);
205 if (flags & 2)
207 /* we have an array, with the number of elements stored before the first object */
208 int i, *ptr = (int *)_this - 1;
210 for (i = *ptr - 1; i >= 0; i--) MSVCRT_exception_dtor(_this + i);
211 MSVCRT_operator_delete(ptr);
213 else
215 MSVCRT_exception_dtor(_this);
216 if (flags & 1) MSVCRT_operator_delete(_this);
218 return _this;
221 /******************************************************************
222 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
224 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor);
225 void * __stdcall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
227 TRACE("(%p %x)\n", _this, flags);
228 MSVCRT_exception_dtor(_this);
229 if (flags & 1) MSVCRT_operator_delete(_this);
230 return _this;
233 /******************************************************************
234 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
236 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception);
237 const char * __stdcall MSVCRT_what_exception(exception * _this)
239 TRACE("(%p) returning %s\n", _this, _this->name);
240 return _this->name ? _this->name : "Unknown exception";
243 /******************************************************************
244 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
246 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor);
247 bad_typeid * __stdcall MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
249 TRACE("(%p %p)\n", _this, rhs);
250 MSVCRT_exception_copy_ctor(_this, rhs);
251 _this->vtable = &MSVCRT_bad_typeid_vtable;
252 return _this;
255 /******************************************************************
256 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
258 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor);
259 bad_typeid * __stdcall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
261 TRACE("(%p %s)\n", _this, name);
262 EXCEPTION_ctor(_this, &name);
263 _this->vtable = &MSVCRT_bad_typeid_vtable;
264 return _this;
267 /******************************************************************
268 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
270 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor);
271 void __stdcall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
273 TRACE("(%p)\n", _this);
274 MSVCRT_exception_dtor(_this);
277 /******************************************************************
278 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
280 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals);
281 bad_typeid * __stdcall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
283 TRACE("(%p %p)\n", _this, rhs);
284 MSVCRT_exception_opequals(_this, rhs);
285 return _this;
288 /******************************************************************
289 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
291 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor);
292 void * __stdcall MSVCRT_bad_typeid_vector_dtor(bad_typeid * _this, unsigned int flags)
294 TRACE("(%p %x)\n", _this, flags);
295 if (flags & 2)
297 /* we have an array, with the number of elements stored before the first object */
298 int i, *ptr = (int *)_this - 1;
300 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_typeid_dtor(_this + i);
301 MSVCRT_operator_delete(ptr);
303 else
305 MSVCRT_bad_typeid_dtor(_this);
306 if (flags & 1) MSVCRT_operator_delete(_this);
308 return _this;
311 /******************************************************************
312 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
314 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor);
315 void * __stdcall MSVCRT_bad_typeid_scalar_dtor(bad_typeid * _this, unsigned int flags)
317 TRACE("(%p %x)\n", _this, flags);
318 MSVCRT_bad_typeid_dtor(_this);
319 if (flags & 1) MSVCRT_operator_delete(_this);
320 return _this;
323 /******************************************************************
324 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
326 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor);
327 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
328 const __non_rtti_object * rhs)
330 TRACE("(%p %p)\n", _this, rhs);
331 MSVCRT_bad_typeid_copy_ctor(_this, rhs);
332 _this->vtable = &MSVCRT___non_rtti_object_vtable;
333 return _this;
336 /******************************************************************
337 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
339 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor);
340 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
341 const char * name)
343 TRACE("(%p %s)\n", _this, name);
344 EXCEPTION_ctor(_this, &name);
345 _this->vtable = &MSVCRT___non_rtti_object_vtable;
346 return _this;
349 /******************************************************************
350 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
352 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor);
353 void __stdcall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
355 TRACE("(%p)\n", _this);
356 MSVCRT_bad_typeid_dtor(_this);
359 /******************************************************************
360 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
362 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals);
363 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
364 const __non_rtti_object *rhs)
366 TRACE("(%p %p)\n", _this, rhs);
367 MSVCRT_bad_typeid_opequals(_this, rhs);
368 return _this;
371 /******************************************************************
372 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
374 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor);
375 void * __stdcall MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object * _this, unsigned int flags)
377 TRACE("(%p %x)\n", _this, flags);
378 if (flags & 2)
380 /* we have an array, with the number of elements stored before the first object */
381 int i, *ptr = (int *)_this - 1;
383 for (i = *ptr - 1; i >= 0; i--) MSVCRT___non_rtti_object_dtor(_this + i);
384 MSVCRT_operator_delete(ptr);
386 else
388 MSVCRT___non_rtti_object_dtor(_this);
389 if (flags & 1) MSVCRT_operator_delete(_this);
391 return _this;
394 /******************************************************************
395 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
397 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor);
398 void * __stdcall MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object * _this, unsigned int flags)
400 TRACE("(%p %x)\n", _this, flags);
401 MSVCRT___non_rtti_object_dtor(_this);
402 if (flags & 1) MSVCRT_operator_delete(_this);
403 return _this;
406 /******************************************************************
407 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
409 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor);
410 bad_cast * __stdcall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
412 TRACE("(%p %s)\n", _this, *name);
413 EXCEPTION_ctor(_this, name);
414 _this->vtable = &MSVCRT_bad_cast_vtable;
415 return _this;
418 /******************************************************************
419 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
421 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor);
422 bad_cast * __stdcall MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
424 TRACE("(%p %p)\n", _this, rhs);
425 MSVCRT_exception_copy_ctor(_this, rhs);
426 _this->vtable = &MSVCRT_bad_cast_vtable;
427 return _this;
430 /******************************************************************
431 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
433 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor);
434 void __stdcall MSVCRT_bad_cast_dtor(bad_cast * _this)
436 TRACE("(%p)\n", _this);
437 MSVCRT_exception_dtor(_this);
440 /******************************************************************
441 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
443 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals);
444 bad_cast * __stdcall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
446 TRACE("(%p %p)\n", _this, rhs);
447 MSVCRT_exception_opequals(_this, rhs);
448 return _this;
451 /******************************************************************
452 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
454 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor);
455 void * __stdcall MSVCRT_bad_cast_vector_dtor(bad_cast * _this, unsigned int flags)
457 TRACE("(%p %x)\n", _this, flags);
458 if (flags & 2)
460 /* we have an array, with the number of elements stored before the first object */
461 int i, *ptr = (int *)_this - 1;
463 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_cast_dtor(_this + i);
464 MSVCRT_operator_delete(ptr);
466 else
468 MSVCRT_bad_cast_dtor(_this);
469 if (flags & 1) MSVCRT_operator_delete(_this);
471 return _this;
474 /******************************************************************
475 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
477 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor);
478 void * __stdcall MSVCRT_bad_cast_scalar_dtor(bad_cast * _this, unsigned int flags)
480 TRACE("(%p %x)\n", _this, flags);
481 MSVCRT_bad_cast_dtor(_this);
482 if (flags & 1) MSVCRT_operator_delete(_this);
483 return _this;
486 /******************************************************************
487 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
489 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals);
490 int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
492 int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
493 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
494 return ret;
497 /******************************************************************
498 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
500 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals);
501 int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
503 int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
504 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
505 return ret;
508 /******************************************************************
509 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
511 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before);
512 int __stdcall MSVCRT_type_info_before(type_info * _this, const type_info * rhs)
514 int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
515 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
516 return ret;
519 /******************************************************************
520 * ??1type_info@@UAE@XZ (MSVCRT.@)
522 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor);
523 void __stdcall MSVCRT_type_info_dtor(type_info * _this)
525 TRACE("(%p)\n", _this);
526 if (_this->name)
527 MSVCRT_free(_this->name);
530 /******************************************************************
531 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
533 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name);
534 const char * __stdcall MSVCRT_type_info_name(type_info * _this)
536 if (!_this->name)
538 /* Create and set the demangled name */
539 char* name = __unDName(0, _this->mangled, 0,
540 (malloc_func_t)MSVCRT_malloc,
541 (free_func_t)MSVCRT_free, 0x2800);
543 if (name)
545 unsigned int len = strlen(name);
547 /* It seems _unDName may leave blanks at the end of the demangled name */
548 if (name[len] == ' ')
549 name[len] = '\0';
551 _mlock(_EXIT_LOCK2);
553 if (_this->name)
555 /* Another thread set this member since we checked above - use it */
556 MSVCRT_free(name);
558 else
559 _this->name = name;
561 _munlock(_EXIT_LOCK2);
564 TRACE("(%p) returning %s\n", _this, _this->name);
565 return _this->name;
568 /******************************************************************
569 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
571 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name);
572 const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
574 TRACE("(%p) returning %s\n", _this, _this->mangled);
575 return _this->mangled;
578 /* Unexported */
579 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor);
580 void * __stdcall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
582 TRACE("(%p %x)\n", _this, flags);
583 if (flags & 2)
585 /* we have an array, with the number of elements stored before the first object */
586 int i, *ptr = (int *)_this - 1;
588 for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
589 MSVCRT_operator_delete(ptr);
591 else
593 MSVCRT_type_info_dtor(_this);
594 if (flags & 1) MSVCRT_operator_delete(_this);
596 return _this;
599 /* vtables */
601 const exception_vtable MSVCRT_exception_vtable =
603 __thiscall_MSVCRT_exception_vector_dtor,
604 __thiscall_MSVCRT_what_exception
607 const exception_vtable MSVCRT_bad_typeid_vtable =
609 __thiscall_MSVCRT_bad_typeid_vector_dtor,
610 __thiscall_MSVCRT_what_exception
613 const exception_vtable MSVCRT_bad_cast_vtable =
615 __thiscall_MSVCRT_bad_cast_vector_dtor,
616 __thiscall_MSVCRT_what_exception
619 const exception_vtable MSVCRT___non_rtti_object_vtable =
621 __thiscall_MSVCRT___non_rtti_object_vector_dtor,
622 __thiscall_MSVCRT_what_exception
625 static const exception_vtable MSVCRT_type_info_vtable =
627 __thiscall_MSVCRT_type_info_vector_dtor,
628 NULL
631 /* Static RTTI for exported objects */
633 static type_info exception_type_info =
635 (void*)&MSVCRT_type_info_vtable,
636 NULL,
637 ".?AVexception@@"
640 static const rtti_base_descriptor exception_rtti_base_descriptor =
642 &exception_type_info,
650 static const rtti_base_array exception_rtti_base_array =
653 &exception_rtti_base_descriptor,
654 NULL,
655 NULL
659 static const rtti_object_hierachy exception_type_hierachy =
664 &exception_rtti_base_array
667 static const rtti_object_locator exception_rtti =
672 &exception_type_info,
673 &exception_type_hierachy
676 static const cxx_type_info exception_cxx_type_info =
679 &exception_type_info,
683 sizeof(exception),
684 (cxx_copy_ctor)__thiscall_MSVCRT_exception_copy_ctor
687 static type_info bad_typeid_type_info =
689 (void*)&MSVCRT_type_info_vtable,
690 NULL,
691 ".?AVbad_typeid@@"
694 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
696 &bad_typeid_type_info,
699 0xffffffff,
704 static const rtti_base_array bad_typeid_rtti_base_array =
707 &bad_typeid_rtti_base_descriptor,
708 &exception_rtti_base_descriptor,
709 NULL
713 static const rtti_object_hierachy bad_typeid_type_hierachy =
718 &bad_typeid_rtti_base_array
721 static const rtti_object_locator bad_typeid_rtti =
726 &bad_typeid_type_info,
727 &bad_typeid_type_hierachy
730 static const cxx_type_info bad_typeid_cxx_type_info =
733 &bad_typeid_type_info,
737 sizeof(exception),
738 (cxx_copy_ctor)__thiscall_MSVCRT_bad_typeid_copy_ctor
741 static type_info bad_cast_type_info =
743 (void*)&MSVCRT_type_info_vtable,
744 NULL,
745 ".?AVbad_cast@@"
748 static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
750 &bad_cast_type_info,
753 0xffffffff,
758 static const rtti_base_array bad_cast_rtti_base_array =
761 &bad_cast_rtti_base_descriptor,
762 &exception_rtti_base_descriptor,
763 NULL
767 static const rtti_object_hierachy bad_cast_type_hierachy =
772 &bad_cast_rtti_base_array
775 static const rtti_object_locator bad_cast_rtti =
780 &bad_cast_type_info,
781 &bad_cast_type_hierachy
784 static const cxx_type_info bad_cast_cxx_type_info =
787 &bad_cast_type_info,
791 sizeof(exception),
792 (cxx_copy_ctor)__thiscall_MSVCRT_bad_cast_copy_ctor
795 static type_info __non_rtti_object_type_info =
797 (void*)&MSVCRT_type_info_vtable,
798 NULL,
799 ".?AV__non_rtti_object@@"
802 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
804 &__non_rtti_object_type_info,
807 0xffffffff,
812 static const rtti_base_array __non_rtti_object_rtti_base_array =
815 &__non_rtti_object_rtti_base_descriptor,
816 &bad_typeid_rtti_base_descriptor,
817 &exception_rtti_base_descriptor
821 static const rtti_object_hierachy __non_rtti_object_type_hierachy =
826 &__non_rtti_object_rtti_base_array
829 static const rtti_object_locator __non_rtti_object_rtti =
834 &__non_rtti_object_type_info,
835 &__non_rtti_object_type_hierachy
838 static const cxx_type_info __non_rtti_object_cxx_type_info =
841 &__non_rtti_object_type_info,
845 sizeof(exception),
846 (cxx_copy_ctor)__thiscall_MSVCRT___non_rtti_object_copy_ctor
849 static type_info type_info_type_info =
851 (void*)&MSVCRT_type_info_vtable,
852 NULL,
853 ".?AVtype_info@@"
856 static const rtti_base_descriptor type_info_rtti_base_descriptor =
858 &type_info_type_info,
861 0xffffffff,
866 static const rtti_base_array type_info_rtti_base_array =
869 &type_info_rtti_base_descriptor,
870 NULL,
871 NULL
875 static const rtti_object_hierachy type_info_type_hierachy =
880 &type_info_rtti_base_array
883 static const rtti_object_locator type_info_rtti =
888 &type_info_type_info,
889 &type_info_type_hierachy
893 * Exception RTTI for cpp objects
895 static const cxx_type_info_table bad_cast_type_info_table =
899 &__non_rtti_object_cxx_type_info,
900 &bad_typeid_cxx_type_info,
901 &exception_cxx_type_info
905 static const cxx_exception_type bad_cast_exception_type =
908 (void*)__thiscall_MSVCRT_bad_cast_dtor,
909 NULL,
910 &bad_cast_type_info_table
913 static const cxx_type_info_table bad_typeid_type_info_table =
917 &bad_cast_cxx_type_info,
918 &exception_cxx_type_info,
919 NULL
923 static const cxx_exception_type bad_typeid_exception_type =
926 (void*)__thiscall_MSVCRT_bad_typeid_dtor,
927 NULL,
928 &bad_cast_type_info_table
931 static const cxx_exception_type __non_rtti_object_exception_type =
934 (void*)__thiscall_MSVCRT___non_rtti_object_dtor,
935 NULL,
936 &bad_typeid_type_info_table
939 #endif /* __i386__ */
942 /******************************************************************
943 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
945 * Install a handler to be called when terminate() is called.
947 * PARAMS
948 * func [I] Handler function to install
950 * RETURNS
951 * The previously installed handler function, if any.
953 MSVCRT_terminate_function MSVCRT_set_terminate(MSVCRT_terminate_function func)
955 thread_data_t *data = msvcrt_get_thread_data();
956 MSVCRT_terminate_function previous = data->terminate_handler;
957 TRACE("(%p) returning %p\n",func,previous);
958 data->terminate_handler = func;
959 return previous;
962 /******************************************************************
963 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
965 * Install a handler to be called when unexpected() is called.
967 * PARAMS
968 * func [I] Handler function to install
970 * RETURNS
971 * The previously installed handler function, if any.
973 MSVCRT_unexpected_function MSVCRT_set_unexpected(MSVCRT_unexpected_function func)
975 thread_data_t *data = msvcrt_get_thread_data();
976 MSVCRT_unexpected_function previous = data->unexpected_handler;
977 TRACE("(%p) returning %p\n",func,previous);
978 data->unexpected_handler = func;
979 return previous;
982 /******************************************************************
983 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
985 MSVCRT__se_translator_function MSVCRT__set_se_translator(MSVCRT__se_translator_function func)
987 thread_data_t *data = msvcrt_get_thread_data();
988 MSVCRT__se_translator_function previous = data->se_translator;
989 TRACE("(%p) returning %p\n",func,previous);
990 data->se_translator = func;
991 return previous;
994 /******************************************************************
995 * ?terminate@@YAXXZ (MSVCRT.@)
997 * Default handler for an unhandled exception.
999 * PARAMS
1000 * None.
1002 * RETURNS
1003 * This function does not return. Either control resumes from any
1004 * handler installed by calling set_terminate(), or (by default) abort()
1005 * is called.
1007 void MSVCRT_terminate(void)
1009 thread_data_t *data = msvcrt_get_thread_data();
1010 if (data->terminate_handler) data->terminate_handler();
1011 MSVCRT_abort();
1014 /******************************************************************
1015 * ?unexpected@@YAXXZ (MSVCRT.@)
1017 void MSVCRT_unexpected(void)
1019 thread_data_t *data = msvcrt_get_thread_data();
1020 if (data->unexpected_handler) data->unexpected_handler();
1021 MSVCRT_terminate();
1024 /* Get type info from an object (internal) */
1025 static const rtti_object_locator* RTTI_GetObjectLocator(type_info *cppobj)
1027 const rtti_object_locator *obj_locator = NULL;
1029 #ifdef __i386__
1030 const exception_vtable* vtable = (const exception_vtable*)cppobj->vtable;
1032 /* Perhaps this is one of classes we export? */
1033 if (vtable == &MSVCRT_exception_vtable)
1035 TRACE("returning exception_rtti\n");
1036 return &exception_rtti;
1038 else if (vtable == &MSVCRT_bad_typeid_vtable)
1040 TRACE("returning bad_typeid_rtti\n");
1041 return &bad_typeid_rtti;
1043 else if (vtable == &MSVCRT_bad_cast_vtable)
1045 TRACE("returning bad_cast_rtti\n");
1046 return &bad_cast_rtti;
1048 else if (vtable == &MSVCRT___non_rtti_object_vtable)
1050 TRACE("returning __non_rtti_object_rtti\n");
1051 return &__non_rtti_object_rtti;
1053 else if (vtable == &MSVCRT_type_info_vtable)
1055 TRACE("returning type_info_rtti\n");
1056 return &type_info_rtti;
1058 #endif
1060 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
1061 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
1062 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
1064 obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
1065 TRACE("returning type_info from vtable (%p)\n", obj_locator);
1068 return obj_locator;
1071 /******************************************************************
1072 * __RTtypeid (MSVCRT.@)
1074 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1076 * PARAMS
1077 * cppobj [I] C++ object to get type information for.
1079 * RETURNS
1080 * Success: A type_info object describing cppobj.
1081 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
1082 * exception is thrown. If cppobj is NULL, a bad_typeid exception
1083 * is thrown. In either case, this function does not return.
1085 * NOTES
1086 * This function is usually called by compiler generated code as a result
1087 * of using one of the C++ dynamic cast statements.
1089 type_info* MSVCRT___RTtypeid(type_info *cppobj)
1091 const rtti_object_locator *obj_locator = RTTI_GetObjectLocator(cppobj);
1093 #ifdef __i386__
1094 if (!obj_locator)
1096 static const char* szNullPtr = "Attempted a typeid of NULL pointer!";
1097 static const char* szBadPtr = "Bad read pointer - no RTTI data!";
1098 const cxx_exception_type *e_type;
1099 exception e;
1101 /* Throw a bad_typeid or __non_rtti_object exception */
1102 if (!cppobj)
1104 EXCEPTION_ctor(&e, &szNullPtr);
1105 e.vtable = &MSVCRT_bad_typeid_vtable;
1106 e_type = &bad_typeid_exception_type;
1108 else
1110 EXCEPTION_ctor(&e, &szBadPtr);
1111 e.vtable = &MSVCRT___non_rtti_object_vtable;
1112 e_type = &__non_rtti_object_exception_type;
1115 _CxxThrowException(&e, e_type);
1116 DebugBreak();
1118 return obj_locator->type_descriptor;
1119 #else
1120 return NULL;
1121 #endif
1124 /******************************************************************
1125 * __RTDynamicCast (MSVCRT.@)
1127 * Dynamically cast a C++ object to one of its base classes.
1129 * PARAMS
1130 * cppobj [I] Any C++ object to cast
1131 * unknown [I] Reserved, set to 0
1132 * src [I] type_info object describing cppobj
1133 * dst [I] type_info object describing the base class to cast to
1134 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
1136 * RETURNS
1137 * Success: The address of cppobj, cast to the object described by dst.
1138 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
1139 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
1140 * is thrown and this function does not return.
1142 * NOTES
1143 * This function is usually called by compiler generated code as a result
1144 * of using one of the C++ dynamic cast statements.
1146 void* MSVCRT___RTDynamicCast(type_info *cppobj, int unknown,
1147 type_info *src, type_info *dst,
1148 int do_throw)
1150 const rtti_object_locator *obj_locator;
1152 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
1153 TRACE("(%p,%d,%p,%p,%d)\n", cppobj, unknown, src, dst, do_throw);
1154 if (!cppobj)
1155 return 0;
1156 obj_locator= RTTI_GetObjectLocator(cppobj);
1157 if (unknown)
1158 FIXME("Unknown parameter is non-zero: please report\n");
1160 /* To cast an object at runtime:
1161 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
1162 * 2.Search for the destination type in the class hierarchy
1163 * 3.If destination type is found, return base object address + dest offset
1164 * Otherwise, fail the cast
1166 if (obj_locator)
1168 int count = 0;
1169 const rtti_object_hierachy *obj_bases = obj_locator->type_hierachy;
1170 const rtti_base_descriptor **base_desc = obj_bases->base_classes->bases;
1171 int src_offset = obj_locator->base_class_offset, dst_offset = -1;
1173 while (count < obj_bases->array_len)
1175 const type_info *typ = (*base_desc)->type_descriptor;
1177 if (!strcmp(typ->mangled, dst->mangled))
1179 dst_offset = (*base_desc)->base_class_offset;
1180 break;
1182 base_desc++;
1183 count++;
1185 if (dst_offset >= 0)
1186 return (void*)((unsigned long)cppobj - src_offset + dst_offset);
1189 #ifdef __i386__
1190 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
1191 * to a reference, since references cannot be NULL.
1193 if (do_throw)
1195 static const char* exception_text = "Bad dynamic_cast!";
1196 exception e;
1198 /* Throw a bad_cast exception */
1199 EXCEPTION_ctor(&e, &exception_text);
1200 e.vtable = &MSVCRT_bad_cast_vtable;
1201 _CxxThrowException(&e, &bad_cast_exception_type);
1202 DebugBreak();
1204 #endif
1205 return NULL;
1209 /******************************************************************
1210 * __RTCastToVoid (MSVCRT.@)
1212 * Dynamically cast a C++ object to a void*.
1214 * PARAMS
1215 * cppobj [I] The C++ object to cast
1217 * RETURNS
1218 * Success: The base address of the object as a void*.
1219 * Failure: NULL, if cppobj is NULL or has no RTTI.
1221 * NOTES
1222 * This function is usually called by compiler generated code as a result
1223 * of using one of the C++ dynamic cast statements.
1225 void* MSVCRT___RTCastToVoid(type_info *cppobj)
1227 const rtti_object_locator *obj_locator = RTTI_GetObjectLocator(cppobj);
1229 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
1230 TRACE("(%p)\n", cppobj);
1232 /* Casts to void* simply cast to the base object */
1233 if (obj_locator)
1234 return (void*)((unsigned long)cppobj - obj_locator->base_class_offset);
1235 return NULL;