wined3d: Check the results of SetPixelFormat.
[wine.git] / dlls / msvcrt / cpp.c
blob045bab26cc99e806f9b329fc36a75cd1bc5f5953
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"
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
37 typedef exception bad_cast;
38 typedef exception bad_typeid;
39 typedef exception __non_rtti_object;
41 typedef struct _rtti_base_descriptor
43 const type_info *type_descriptor;
44 int num_base_classes;
45 this_ptr_offsets offsets; /* offsets for computing the this pointer */
46 unsigned int attributes;
47 } rtti_base_descriptor;
49 typedef struct _rtti_base_array
51 const rtti_base_descriptor *bases[3]; /* First element is the class itself */
52 } rtti_base_array;
54 typedef struct _rtti_object_hierarchy
56 unsigned int signature;
57 unsigned int attributes;
58 int array_len; /* Size of the array pointed to by 'base_classes' */
59 const rtti_base_array *base_classes;
60 } rtti_object_hierarchy;
62 typedef struct _rtti_object_locator
64 unsigned int signature;
65 int base_class_offset;
66 unsigned int flags;
67 const type_info *type_descriptor;
68 const rtti_object_hierarchy *type_hierarchy;
69 } rtti_object_locator;
72 #ifdef __i386__ /* thiscall functions are i386-specific */
74 #define THISCALL(func) __thiscall_ ## func
75 #define THISCALL_NAME(func) __ASM_NAME("__thiscall_" #func)
76 #define DEFINE_THISCALL_WRAPPER(func) \
77 extern void THISCALL(func)(); \
78 __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
79 "popl %eax\n\t" \
80 "pushl %ecx\n\t" \
81 "pushl %eax\n\t" \
82 "jmp " __ASM_NAME(#func) )
83 #else /* __i386__ */
85 #define THISCALL(func) func
86 #define THISCALL_NAME(func) __ASM_NAME(#func)
87 #define DEFINE_THISCALL_WRAPPER(func) /* nothing */
89 #endif /* __i386__ */
91 extern const vtable_ptr MSVCRT_exception_vtable;
92 extern const vtable_ptr MSVCRT_bad_typeid_vtable;
93 extern const vtable_ptr MSVCRT_bad_cast_vtable;
94 extern const vtable_ptr MSVCRT___non_rtti_object_vtable;
95 extern const vtable_ptr MSVCRT_type_info_vtable;
97 /* get the vtable pointer for a C++ object */
98 static inline const vtable_ptr *get_vtable( void *obj )
100 return *(const vtable_ptr **)obj;
103 static inline const rtti_object_locator *get_obj_locator( void *cppobj )
105 const vtable_ptr *vtable = get_vtable( cppobj );
106 return (const rtti_object_locator *)vtable[-1];
109 static void dump_obj_locator( const rtti_object_locator *ptr )
111 int i;
112 const rtti_object_hierarchy *h = ptr->type_hierarchy;
114 TRACE( "%p: sig=%08x base_offset=%08x flags=%08x type=%p %s hierarchy=%p\n",
115 ptr, ptr->signature, ptr->base_class_offset, ptr->flags,
116 ptr->type_descriptor, dbgstr_type_info(ptr->type_descriptor), ptr->type_hierarchy );
117 TRACE( " hierarchy: sig=%08x attr=%08x len=%d base classes=%p\n",
118 h->signature, h->attributes, h->array_len, h->base_classes );
119 for (i = 0; i < h->array_len; i++)
121 TRACE( " base class %p: num %d off %d,%d,%d attr %08x type %p %s\n",
122 h->base_classes->bases[i],
123 h->base_classes->bases[i]->num_base_classes,
124 h->base_classes->bases[i]->offsets.this_offset,
125 h->base_classes->bases[i]->offsets.vbase_descr,
126 h->base_classes->bases[i]->offsets.vbase_offset,
127 h->base_classes->bases[i]->attributes,
128 h->base_classes->bases[i]->type_descriptor,
129 dbgstr_type_info(h->base_classes->bases[i]->type_descriptor) );
133 /* Internal common ctor for exception */
134 static void WINAPI EXCEPTION_ctor(exception *_this, const char** name)
136 _this->vtable = &MSVCRT_exception_vtable;
137 if (*name)
139 size_t name_len = strlen(*name) + 1;
140 _this->name = MSVCRT_malloc(name_len);
141 memcpy(_this->name, *name, name_len);
142 _this->do_free = TRUE;
144 else
146 _this->name = NULL;
147 _this->do_free = FALSE;
151 /******************************************************************
152 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
154 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor)
155 exception * __stdcall MSVCRT_exception_ctor(exception * _this, const char ** name)
157 TRACE("(%p,%s)\n", _this, *name);
158 EXCEPTION_ctor(_this, name);
159 return _this;
162 /******************************************************************
163 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
165 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor)
166 exception * __stdcall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
168 TRACE("(%p,%p)\n", _this, rhs);
170 if (!rhs->do_free)
172 _this->vtable = &MSVCRT_exception_vtable;
173 _this->name = rhs->name;
174 _this->do_free = FALSE;
176 else
177 EXCEPTION_ctor(_this, (const char**)&rhs->name);
178 TRACE("name = %s\n", _this->name);
179 return _this;
182 /******************************************************************
183 * ??0exception@@QAE@XZ (MSVCRT.@)
185 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor)
186 exception * __stdcall MSVCRT_exception_default_ctor(exception * _this)
188 static const char* empty = NULL;
190 TRACE("(%p)\n", _this);
191 EXCEPTION_ctor(_this, &empty);
192 return _this;
195 /******************************************************************
196 * ??1exception@@UAE@XZ (MSVCRT.@)
198 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor)
199 void __stdcall MSVCRT_exception_dtor(exception * _this)
201 TRACE("(%p)\n", _this);
202 _this->vtable = &MSVCRT_exception_vtable;
203 if (_this->do_free) MSVCRT_free(_this->name);
206 /******************************************************************
207 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
209 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals)
210 exception * __stdcall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
212 TRACE("(%p %p)\n", _this, rhs);
213 if (_this != rhs)
215 MSVCRT_exception_dtor(_this);
216 MSVCRT_exception_copy_ctor(_this, rhs);
218 TRACE("name = %s\n", _this->name);
219 return _this;
222 /******************************************************************
223 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
225 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor)
226 void * __stdcall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
228 TRACE("(%p %x)\n", _this, flags);
229 if (flags & 2)
231 /* we have an array, with the number of elements stored before the first object */
232 int i, *ptr = (int *)_this - 1;
234 for (i = *ptr - 1; i >= 0; i--) MSVCRT_exception_dtor(_this + i);
235 MSVCRT_operator_delete(ptr);
237 else
239 MSVCRT_exception_dtor(_this);
240 if (flags & 1) MSVCRT_operator_delete(_this);
242 return _this;
245 /******************************************************************
246 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
248 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor)
249 void * __stdcall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
251 TRACE("(%p %x)\n", _this, flags);
252 MSVCRT_exception_dtor(_this);
253 if (flags & 1) MSVCRT_operator_delete(_this);
254 return _this;
257 /******************************************************************
258 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
260 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception)
261 const char * __stdcall MSVCRT_what_exception(exception * _this)
263 TRACE("(%p) returning %s\n", _this, _this->name);
264 return _this->name ? _this->name : "Unknown exception";
267 /******************************************************************
268 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
270 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor)
271 bad_typeid * __stdcall MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
273 TRACE("(%p %p)\n", _this, rhs);
274 MSVCRT_exception_copy_ctor(_this, rhs);
275 _this->vtable = &MSVCRT_bad_typeid_vtable;
276 return _this;
279 /******************************************************************
280 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
282 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor)
283 bad_typeid * __stdcall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
285 TRACE("(%p %s)\n", _this, name);
286 EXCEPTION_ctor(_this, &name);
287 _this->vtable = &MSVCRT_bad_typeid_vtable;
288 return _this;
291 /******************************************************************
292 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
294 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor)
295 void __stdcall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
297 TRACE("(%p)\n", _this);
298 MSVCRT_exception_dtor(_this);
301 /******************************************************************
302 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
304 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals)
305 bad_typeid * __stdcall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
307 TRACE("(%p %p)\n", _this, rhs);
308 MSVCRT_exception_opequals(_this, rhs);
309 return _this;
312 /******************************************************************
313 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
315 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor)
316 void * __stdcall MSVCRT_bad_typeid_vector_dtor(bad_typeid * _this, unsigned int flags)
318 TRACE("(%p %x)\n", _this, flags);
319 if (flags & 2)
321 /* we have an array, with the number of elements stored before the first object */
322 int i, *ptr = (int *)_this - 1;
324 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_typeid_dtor(_this + i);
325 MSVCRT_operator_delete(ptr);
327 else
329 MSVCRT_bad_typeid_dtor(_this);
330 if (flags & 1) MSVCRT_operator_delete(_this);
332 return _this;
335 /******************************************************************
336 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
338 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor)
339 void * __stdcall MSVCRT_bad_typeid_scalar_dtor(bad_typeid * _this, unsigned int flags)
341 TRACE("(%p %x)\n", _this, flags);
342 MSVCRT_bad_typeid_dtor(_this);
343 if (flags & 1) MSVCRT_operator_delete(_this);
344 return _this;
347 /******************************************************************
348 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
350 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor)
351 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
352 const __non_rtti_object * rhs)
354 TRACE("(%p %p)\n", _this, rhs);
355 MSVCRT_bad_typeid_copy_ctor(_this, rhs);
356 _this->vtable = &MSVCRT___non_rtti_object_vtable;
357 return _this;
360 /******************************************************************
361 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
363 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor)
364 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
365 const char * name)
367 TRACE("(%p %s)\n", _this, name);
368 EXCEPTION_ctor(_this, &name);
369 _this->vtable = &MSVCRT___non_rtti_object_vtable;
370 return _this;
373 /******************************************************************
374 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
376 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor)
377 void __stdcall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
379 TRACE("(%p)\n", _this);
380 MSVCRT_bad_typeid_dtor(_this);
383 /******************************************************************
384 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
386 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals)
387 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
388 const __non_rtti_object *rhs)
390 TRACE("(%p %p)\n", _this, rhs);
391 MSVCRT_bad_typeid_opequals(_this, rhs);
392 return _this;
395 /******************************************************************
396 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
398 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor)
399 void * __stdcall MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object * _this, unsigned int flags)
401 TRACE("(%p %x)\n", _this, flags);
402 if (flags & 2)
404 /* we have an array, with the number of elements stored before the first object */
405 int i, *ptr = (int *)_this - 1;
407 for (i = *ptr - 1; i >= 0; i--) MSVCRT___non_rtti_object_dtor(_this + i);
408 MSVCRT_operator_delete(ptr);
410 else
412 MSVCRT___non_rtti_object_dtor(_this);
413 if (flags & 1) MSVCRT_operator_delete(_this);
415 return _this;
418 /******************************************************************
419 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
421 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor)
422 void * __stdcall MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object * _this, unsigned int flags)
424 TRACE("(%p %x)\n", _this, flags);
425 MSVCRT___non_rtti_object_dtor(_this);
426 if (flags & 1) MSVCRT_operator_delete(_this);
427 return _this;
430 /******************************************************************
431 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
433 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor)
434 bad_cast * __stdcall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
436 TRACE("(%p %s)\n", _this, *name);
437 EXCEPTION_ctor(_this, name);
438 _this->vtable = &MSVCRT_bad_cast_vtable;
439 return _this;
442 /******************************************************************
443 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
445 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor)
446 bad_cast * __stdcall MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
448 TRACE("(%p %p)\n", _this, rhs);
449 MSVCRT_exception_copy_ctor(_this, rhs);
450 _this->vtable = &MSVCRT_bad_cast_vtable;
451 return _this;
454 /******************************************************************
455 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
457 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor)
458 void __stdcall MSVCRT_bad_cast_dtor(bad_cast * _this)
460 TRACE("(%p)\n", _this);
461 MSVCRT_exception_dtor(_this);
464 /******************************************************************
465 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
467 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals)
468 bad_cast * __stdcall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
470 TRACE("(%p %p)\n", _this, rhs);
471 MSVCRT_exception_opequals(_this, rhs);
472 return _this;
475 /******************************************************************
476 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
478 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor)
479 void * __stdcall MSVCRT_bad_cast_vector_dtor(bad_cast * _this, unsigned int flags)
481 TRACE("(%p %x)\n", _this, flags);
482 if (flags & 2)
484 /* we have an array, with the number of elements stored before the first object */
485 int i, *ptr = (int *)_this - 1;
487 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_cast_dtor(_this + i);
488 MSVCRT_operator_delete(ptr);
490 else
492 MSVCRT_bad_cast_dtor(_this);
493 if (flags & 1) MSVCRT_operator_delete(_this);
495 return _this;
498 /******************************************************************
499 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
501 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor)
502 void * __stdcall MSVCRT_bad_cast_scalar_dtor(bad_cast * _this, unsigned int flags)
504 TRACE("(%p %x)\n", _this, flags);
505 MSVCRT_bad_cast_dtor(_this);
506 if (flags & 1) MSVCRT_operator_delete(_this);
507 return _this;
510 /******************************************************************
511 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
513 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals)
514 int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
516 int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
517 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
518 return ret;
521 /******************************************************************
522 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
524 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals)
525 int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
527 int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
528 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
529 return ret;
532 /******************************************************************
533 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
535 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before)
536 int __stdcall MSVCRT_type_info_before(type_info * _this, const type_info * rhs)
538 int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
539 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
540 return ret;
543 /******************************************************************
544 * ??1type_info@@UAE@XZ (MSVCRT.@)
546 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor)
547 void __stdcall MSVCRT_type_info_dtor(type_info * _this)
549 TRACE("(%p)\n", _this);
550 MSVCRT_free(_this->name);
553 /******************************************************************
554 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
556 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name)
557 const char * __stdcall MSVCRT_type_info_name(type_info * _this)
559 if (!_this->name)
561 /* Create and set the demangled name */
562 /* Nota: mangled name in type_info struct always start with a '.', while
563 * it isn't valid for mangled name.
564 * Is this '.' really part of the mangled name, or has it some other meaning ?
566 char* name = __unDName(0, _this->mangled + 1, 0,
567 MSVCRT_malloc, MSVCRT_free, 0x2800);
569 if (name)
571 unsigned int len = strlen(name);
573 /* It seems _unDName may leave blanks at the end of the demangled name */
574 while (len && name[--len] == ' ')
575 name[len] = '\0';
577 _mlock(_EXIT_LOCK2);
579 if (_this->name)
581 /* Another thread set this member since we checked above - use it */
582 MSVCRT_free(name);
584 else
585 _this->name = name;
587 _munlock(_EXIT_LOCK2);
590 TRACE("(%p) returning %s\n", _this, _this->name);
591 return _this->name;
594 /******************************************************************
595 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
597 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name)
598 const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
600 TRACE("(%p) returning %s\n", _this, _this->mangled);
601 return _this->mangled;
604 /* Unexported */
605 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor)
606 void * __stdcall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
608 TRACE("(%p %x)\n", _this, flags);
609 if (flags & 2)
611 /* we have an array, with the number of elements stored before the first object */
612 int i, *ptr = (int *)_this - 1;
614 for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
615 MSVCRT_operator_delete(ptr);
617 else
619 MSVCRT_type_info_dtor(_this);
620 if (flags & 1) MSVCRT_operator_delete(_this);
622 return _this;
625 /* vtables */
627 #define __ASM_VTABLE(name,funcs) \
628 __asm__(".data\n" \
629 "\t.align 4\n" \
630 "\t.long " __ASM_NAME(#name "_rtti") "\n" \
631 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
632 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
633 "\t.long " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
634 funcs "\n\t.text");
636 #define __ASM_EXCEPTION_VTABLE(name) \
637 __ASM_VTABLE(name, "\t.long " THISCALL_NAME(MSVCRT_what_exception) )
639 #ifndef __GNUC__
640 void __asm_dummy_vtables(void) {
641 #endif
643 __ASM_VTABLE(type_info,"")
644 __ASM_EXCEPTION_VTABLE(exception)
645 __ASM_EXCEPTION_VTABLE(bad_typeid)
646 __ASM_EXCEPTION_VTABLE(bad_cast)
647 __ASM_EXCEPTION_VTABLE(__non_rtti_object)
649 #ifndef __GNUC__
651 #endif
653 /* Static RTTI for exported objects */
655 static const type_info exception_type_info =
657 &MSVCRT_type_info_vtable,
658 NULL,
659 ".?AVexception@@"
662 static const rtti_base_descriptor exception_rtti_base_descriptor =
664 &exception_type_info,
666 { 0, -1, 0 },
670 static const rtti_base_array exception_rtti_base_array =
673 &exception_rtti_base_descriptor,
674 NULL,
675 NULL
679 static const rtti_object_hierarchy exception_type_hierarchy =
684 &exception_rtti_base_array
687 const rtti_object_locator exception_rtti =
692 &exception_type_info,
693 &exception_type_hierarchy
696 static const cxx_type_info exception_cxx_type_info =
699 &exception_type_info,
700 { 0, -1, 0 },
701 sizeof(exception),
702 (cxx_copy_ctor)THISCALL(MSVCRT_exception_copy_ctor)
705 static const type_info bad_typeid_type_info =
707 &MSVCRT_type_info_vtable,
708 NULL,
709 ".?AVbad_typeid@@"
712 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
714 &bad_typeid_type_info,
716 { 0, -1, 0 },
720 static const rtti_base_array bad_typeid_rtti_base_array =
723 &bad_typeid_rtti_base_descriptor,
724 &exception_rtti_base_descriptor,
725 NULL
729 static const rtti_object_hierarchy bad_typeid_type_hierarchy =
734 &bad_typeid_rtti_base_array
737 const rtti_object_locator bad_typeid_rtti =
742 &bad_typeid_type_info,
743 &bad_typeid_type_hierarchy
746 static const cxx_type_info bad_typeid_cxx_type_info =
749 &bad_typeid_type_info,
750 { 0, -1, 0 },
751 sizeof(exception),
752 (cxx_copy_ctor)THISCALL(MSVCRT_bad_typeid_copy_ctor)
755 static const type_info bad_cast_type_info =
757 &MSVCRT_type_info_vtable,
758 NULL,
759 ".?AVbad_cast@@"
762 static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
764 &bad_cast_type_info,
766 { 0, -1, 0 },
770 static const rtti_base_array bad_cast_rtti_base_array =
773 &bad_cast_rtti_base_descriptor,
774 &exception_rtti_base_descriptor,
775 NULL
779 static const rtti_object_hierarchy bad_cast_type_hierarchy =
784 &bad_cast_rtti_base_array
787 const rtti_object_locator bad_cast_rtti =
792 &bad_cast_type_info,
793 &bad_cast_type_hierarchy
796 static const cxx_type_info bad_cast_cxx_type_info =
799 &bad_cast_type_info,
800 { 0, -1, 0 },
801 sizeof(exception),
802 (cxx_copy_ctor)THISCALL(MSVCRT_bad_cast_copy_ctor)
805 static const type_info __non_rtti_object_type_info =
807 &MSVCRT_type_info_vtable,
808 NULL,
809 ".?AV__non_rtti_object@@"
812 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
814 &__non_rtti_object_type_info,
816 { 0, -1, 0 },
820 static const rtti_base_array __non_rtti_object_rtti_base_array =
823 &__non_rtti_object_rtti_base_descriptor,
824 &bad_typeid_rtti_base_descriptor,
825 &exception_rtti_base_descriptor
829 static const rtti_object_hierarchy __non_rtti_object_type_hierarchy =
834 &__non_rtti_object_rtti_base_array
837 const rtti_object_locator __non_rtti_object_rtti =
842 &__non_rtti_object_type_info,
843 &__non_rtti_object_type_hierarchy
846 static const cxx_type_info __non_rtti_object_cxx_type_info =
849 &__non_rtti_object_type_info,
850 { 0, -1, 0 },
851 sizeof(exception),
852 (cxx_copy_ctor)THISCALL(MSVCRT___non_rtti_object_copy_ctor)
855 static const type_info type_info_type_info =
857 &MSVCRT_type_info_vtable,
858 NULL,
859 ".?AVtype_info@@"
862 static const rtti_base_descriptor type_info_rtti_base_descriptor =
864 &type_info_type_info,
866 { 0, -1, 0 },
870 static const rtti_base_array type_info_rtti_base_array =
873 &type_info_rtti_base_descriptor,
874 NULL,
875 NULL
879 static const rtti_object_hierarchy type_info_type_hierarchy =
884 &type_info_rtti_base_array
887 const rtti_object_locator type_info_rtti =
892 &type_info_type_info,
893 &type_info_type_hierarchy
897 * Exception RTTI for cpp objects
899 static const cxx_type_info_table bad_cast_type_info_table =
903 &__non_rtti_object_cxx_type_info,
904 &bad_typeid_cxx_type_info,
905 &exception_cxx_type_info
909 static const cxx_exception_type bad_cast_exception_type =
912 (void*)THISCALL(MSVCRT_bad_cast_dtor),
913 NULL,
914 &bad_cast_type_info_table
917 static const cxx_type_info_table bad_typeid_type_info_table =
921 &bad_cast_cxx_type_info,
922 &exception_cxx_type_info,
923 NULL
927 static const cxx_exception_type bad_typeid_exception_type =
930 (void*)THISCALL(MSVCRT_bad_typeid_dtor),
931 NULL,
932 &bad_cast_type_info_table
935 static const cxx_exception_type __non_rtti_object_exception_type =
938 (void*)THISCALL(MSVCRT___non_rtti_object_dtor),
939 NULL,
940 &bad_typeid_type_info_table
944 /******************************************************************
945 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
947 * Install a handler to be called when terminate() is called.
949 * PARAMS
950 * func [I] Handler function to install
952 * RETURNS
953 * The previously installed handler function, if any.
955 MSVCRT_terminate_function CDECL MSVCRT_set_terminate(MSVCRT_terminate_function func)
957 thread_data_t *data = msvcrt_get_thread_data();
958 MSVCRT_terminate_function previous = data->terminate_handler;
959 TRACE("(%p) returning %p\n",func,previous);
960 data->terminate_handler = func;
961 return previous;
964 /******************************************************************
965 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
967 * Install a handler to be called when unexpected() is called.
969 * PARAMS
970 * func [I] Handler function to install
972 * RETURNS
973 * The previously installed handler function, if any.
975 MSVCRT_unexpected_function CDECL MSVCRT_set_unexpected(MSVCRT_unexpected_function func)
977 thread_data_t *data = msvcrt_get_thread_data();
978 MSVCRT_unexpected_function previous = data->unexpected_handler;
979 TRACE("(%p) returning %p\n",func,previous);
980 data->unexpected_handler = func;
981 return previous;
984 /******************************************************************
985 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
987 MSVCRT__se_translator_function CDECL MSVCRT__set_se_translator(MSVCRT__se_translator_function func)
989 thread_data_t *data = msvcrt_get_thread_data();
990 MSVCRT__se_translator_function previous = data->se_translator;
991 TRACE("(%p) returning %p\n",func,previous);
992 data->se_translator = func;
993 return previous;
996 /******************************************************************
997 * ?terminate@@YAXXZ (MSVCRT.@)
999 * Default handler for an unhandled exception.
1001 * PARAMS
1002 * None.
1004 * RETURNS
1005 * This function does not return. Either control resumes from any
1006 * handler installed by calling set_terminate(), or (by default) abort()
1007 * is called.
1009 void CDECL MSVCRT_terminate(void)
1011 thread_data_t *data = msvcrt_get_thread_data();
1012 if (data->terminate_handler) data->terminate_handler();
1013 MSVCRT_abort();
1016 /******************************************************************
1017 * ?unexpected@@YAXXZ (MSVCRT.@)
1019 void CDECL MSVCRT_unexpected(void)
1021 thread_data_t *data = msvcrt_get_thread_data();
1022 if (data->unexpected_handler) data->unexpected_handler();
1023 MSVCRT_terminate();
1027 /******************************************************************
1028 * __RTtypeid (MSVCRT.@)
1030 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1032 * PARAMS
1033 * cppobj [I] C++ object to get type information for.
1035 * RETURNS
1036 * Success: A type_info object describing cppobj.
1037 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
1038 * exception is thrown. If cppobj is NULL, a bad_typeid exception
1039 * is thrown. In either case, this function does not return.
1041 * NOTES
1042 * This function is usually called by compiler generated code as a result
1043 * of using one of the C++ dynamic cast statements.
1045 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
1047 const type_info *ret;
1049 if (!cppobj)
1051 bad_typeid e;
1052 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
1053 _CxxThrowException( &e, &bad_typeid_exception_type );
1054 return NULL;
1057 __TRY
1059 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1060 ret = obj_locator->type_descriptor;
1062 __EXCEPT_PAGE_FAULT
1064 __non_rtti_object e;
1065 MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
1066 _CxxThrowException( &e, &bad_typeid_exception_type );
1067 return NULL;
1069 __ENDTRY
1070 return ret;
1073 /******************************************************************
1074 * __RTDynamicCast (MSVCRT.@)
1076 * Dynamically cast a C++ object to one of its base classes.
1078 * PARAMS
1079 * cppobj [I] Any C++ object to cast
1080 * unknown [I] Reserved, set to 0
1081 * src [I] type_info object describing cppobj
1082 * dst [I] type_info object describing the base class to cast to
1083 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
1085 * RETURNS
1086 * Success: The address of cppobj, cast to the object described by dst.
1087 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
1088 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
1089 * is thrown and this function does not return.
1091 * NOTES
1092 * This function is usually called by compiler generated code as a result
1093 * of using one of the C++ dynamic cast statements.
1095 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
1096 type_info *src, type_info *dst,
1097 int do_throw)
1099 void *ret;
1101 if (!cppobj) return NULL;
1103 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
1104 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
1106 if (unknown) FIXME("Unknown parameter is non-zero: please report\n");
1108 /* To cast an object at runtime:
1109 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
1110 * 2.Search for the destination type in the class hierarchy
1111 * 3.If destination type is found, return base object address + dest offset
1112 * Otherwise, fail the cast
1114 __TRY
1116 int i;
1117 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1118 const rtti_object_hierarchy *obj_bases = obj_locator->type_hierarchy;
1119 const rtti_base_descriptor * const* base_desc = obj_bases->base_classes->bases;
1121 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
1123 ret = NULL;
1124 for (i = 0; i < obj_bases->array_len; i++)
1126 const type_info *typ = base_desc[i]->type_descriptor;
1128 if (!strcmp(typ->mangled, dst->mangled))
1130 /* compute the correct this pointer for that base class */
1131 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
1132 ret = get_this_pointer( &base_desc[i]->offsets, this_ptr );
1133 break;
1136 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
1137 * to a reference, since references cannot be NULL.
1139 if (!ret && do_throw)
1141 const char *msg = "Bad dynamic_cast!";
1142 bad_cast e;
1143 MSVCRT_bad_cast_ctor( &e, &msg );
1144 _CxxThrowException( &e, &bad_cast_exception_type );
1147 __EXCEPT_PAGE_FAULT
1149 __non_rtti_object e;
1150 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1151 _CxxThrowException( &e, &bad_typeid_exception_type );
1152 return NULL;
1154 __ENDTRY
1155 return ret;
1159 /******************************************************************
1160 * __RTCastToVoid (MSVCRT.@)
1162 * Dynamically cast a C++ object to a void*.
1164 * PARAMS
1165 * cppobj [I] The C++ object to cast
1167 * RETURNS
1168 * Success: The base address of the object as a void*.
1169 * Failure: NULL, if cppobj is NULL or has no RTTI.
1171 * NOTES
1172 * This function is usually called by compiler generated code as a result
1173 * of using one of the C++ dynamic cast statements.
1175 void* CDECL MSVCRT___RTCastToVoid(void *cppobj)
1177 void *ret;
1179 if (!cppobj) return NULL;
1181 __TRY
1183 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1184 ret = (char *)cppobj - obj_locator->base_class_offset;
1186 __EXCEPT_PAGE_FAULT
1188 __non_rtti_object e;
1189 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1190 _CxxThrowException( &e, &bad_typeid_exception_type );
1191 return NULL;
1193 __ENDTRY
1194 return ret;