Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / msvcrt / cpp.c
blob2ac9b8f11dbe89a46e44f67416a01c88a2f58f78
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,args) \
77 extern void THISCALL(func)(void); \
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) __ASM_STDCALL(args) )
83 #else /* __i386__ */
85 #define THISCALL(func) func
86 #define THISCALL_NAME(func) __ASM_NAME(#func)
87 #define DEFINE_THISCALL_WRAPPER(func,args) /* 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 EXCEPTION_ctor(exception *_this, const char** name)
136 _this->vtable = &MSVCRT_exception_vtable;
137 if (*name)
139 unsigned int 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,8)
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,8)
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,4)
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,4)
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,8)
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,8)
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,8)
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,4)
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,8)
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,8)
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,4)
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,8)
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,8)
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,8)
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,8)
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,8)
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,4)
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,8)
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,8)
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,8)
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,8)
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,8)
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,4)
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,8)
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,8)
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,8)
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,8)
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,8)
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,8)
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,4)
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,4)
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,4)
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,8)
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 #ifdef _WIN64
629 #define __ASM_VTABLE(name,funcs) \
630 __asm__(".data\n" \
631 "\t.align 8\n" \
632 "\t.quad " __ASM_NAME(#name "_rtti") "\n" \
633 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
634 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
635 "\t.quad " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
636 funcs "\n\t.text");
638 #define __ASM_EXCEPTION_VTABLE(name) \
639 __ASM_VTABLE(name, "\t.quad " THISCALL_NAME(MSVCRT_what_exception) )
641 #else
643 #define __ASM_VTABLE(name,funcs) \
644 __asm__(".data\n" \
645 "\t.align 4\n" \
646 "\t.long " __ASM_NAME(#name "_rtti") "\n" \
647 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
648 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
649 "\t.long " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
650 funcs "\n\t.text");
652 #define __ASM_EXCEPTION_VTABLE(name) \
653 __ASM_VTABLE(name, "\t.long " THISCALL_NAME(MSVCRT_what_exception) )
655 #endif /* _WIN64 */
657 #ifndef __GNUC__
658 void __asm_dummy_vtables(void) {
659 #endif
661 __ASM_VTABLE(type_info,"")
662 __ASM_EXCEPTION_VTABLE(exception)
663 __ASM_EXCEPTION_VTABLE(bad_typeid)
664 __ASM_EXCEPTION_VTABLE(bad_cast)
665 __ASM_EXCEPTION_VTABLE(__non_rtti_object)
667 #ifndef __GNUC__
669 #endif
671 /* Static RTTI for exported objects */
673 static const type_info exception_type_info =
675 &MSVCRT_type_info_vtable,
676 NULL,
677 ".?AVexception@@"
680 static const rtti_base_descriptor exception_rtti_base_descriptor =
682 &exception_type_info,
684 { 0, -1, 0 },
688 static const rtti_base_array exception_rtti_base_array =
691 &exception_rtti_base_descriptor,
692 NULL,
693 NULL
697 static const rtti_object_hierarchy exception_type_hierarchy =
702 &exception_rtti_base_array
705 const rtti_object_locator exception_rtti =
710 &exception_type_info,
711 &exception_type_hierarchy
714 static const cxx_type_info exception_cxx_type_info =
717 &exception_type_info,
718 { 0, -1, 0 },
719 sizeof(exception),
720 (cxx_copy_ctor)THISCALL(MSVCRT_exception_copy_ctor)
723 static const type_info bad_typeid_type_info =
725 &MSVCRT_type_info_vtable,
726 NULL,
727 ".?AVbad_typeid@@"
730 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
732 &bad_typeid_type_info,
734 { 0, -1, 0 },
738 static const rtti_base_array bad_typeid_rtti_base_array =
741 &bad_typeid_rtti_base_descriptor,
742 &exception_rtti_base_descriptor,
743 NULL
747 static const rtti_object_hierarchy bad_typeid_type_hierarchy =
752 &bad_typeid_rtti_base_array
755 const rtti_object_locator bad_typeid_rtti =
760 &bad_typeid_type_info,
761 &bad_typeid_type_hierarchy
764 static const cxx_type_info bad_typeid_cxx_type_info =
767 &bad_typeid_type_info,
768 { 0, -1, 0 },
769 sizeof(exception),
770 (cxx_copy_ctor)THISCALL(MSVCRT_bad_typeid_copy_ctor)
773 static const type_info bad_cast_type_info =
775 &MSVCRT_type_info_vtable,
776 NULL,
777 ".?AVbad_cast@@"
780 static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
782 &bad_cast_type_info,
784 { 0, -1, 0 },
788 static const rtti_base_array bad_cast_rtti_base_array =
791 &bad_cast_rtti_base_descriptor,
792 &exception_rtti_base_descriptor,
793 NULL
797 static const rtti_object_hierarchy bad_cast_type_hierarchy =
802 &bad_cast_rtti_base_array
805 const rtti_object_locator bad_cast_rtti =
810 &bad_cast_type_info,
811 &bad_cast_type_hierarchy
814 static const cxx_type_info bad_cast_cxx_type_info =
817 &bad_cast_type_info,
818 { 0, -1, 0 },
819 sizeof(exception),
820 (cxx_copy_ctor)THISCALL(MSVCRT_bad_cast_copy_ctor)
823 static const type_info __non_rtti_object_type_info =
825 &MSVCRT_type_info_vtable,
826 NULL,
827 ".?AV__non_rtti_object@@"
830 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
832 &__non_rtti_object_type_info,
834 { 0, -1, 0 },
838 static const rtti_base_array __non_rtti_object_rtti_base_array =
841 &__non_rtti_object_rtti_base_descriptor,
842 &bad_typeid_rtti_base_descriptor,
843 &exception_rtti_base_descriptor
847 static const rtti_object_hierarchy __non_rtti_object_type_hierarchy =
852 &__non_rtti_object_rtti_base_array
855 const rtti_object_locator __non_rtti_object_rtti =
860 &__non_rtti_object_type_info,
861 &__non_rtti_object_type_hierarchy
864 static const cxx_type_info __non_rtti_object_cxx_type_info =
867 &__non_rtti_object_type_info,
868 { 0, -1, 0 },
869 sizeof(exception),
870 (cxx_copy_ctor)THISCALL(MSVCRT___non_rtti_object_copy_ctor)
873 static const type_info type_info_type_info =
875 &MSVCRT_type_info_vtable,
876 NULL,
877 ".?AVtype_info@@"
880 static const rtti_base_descriptor type_info_rtti_base_descriptor =
882 &type_info_type_info,
884 { 0, -1, 0 },
888 static const rtti_base_array type_info_rtti_base_array =
891 &type_info_rtti_base_descriptor,
892 NULL,
893 NULL
897 static const rtti_object_hierarchy type_info_type_hierarchy =
902 &type_info_rtti_base_array
905 const rtti_object_locator type_info_rtti =
910 &type_info_type_info,
911 &type_info_type_hierarchy
915 * Exception RTTI for cpp objects
917 static const cxx_type_info_table bad_cast_type_info_table =
921 &__non_rtti_object_cxx_type_info,
922 &bad_typeid_cxx_type_info,
923 &exception_cxx_type_info
927 static const cxx_exception_type bad_cast_exception_type =
930 (void*)THISCALL(MSVCRT_bad_cast_dtor),
931 NULL,
932 &bad_cast_type_info_table
935 static const cxx_type_info_table bad_typeid_type_info_table =
939 &bad_cast_cxx_type_info,
940 &exception_cxx_type_info,
941 NULL
945 static const cxx_exception_type bad_typeid_exception_type =
948 (void*)THISCALL(MSVCRT_bad_typeid_dtor),
949 NULL,
950 &bad_cast_type_info_table
953 static const cxx_exception_type __non_rtti_object_exception_type =
956 (void*)THISCALL(MSVCRT___non_rtti_object_dtor),
957 NULL,
958 &bad_typeid_type_info_table
962 /******************************************************************
963 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
965 * Install a handler to be called when terminate() is called.
967 * PARAMS
968 * func [I] Handler function to install
970 * RETURNS
971 * The previously installed handler function, if any.
973 MSVCRT_terminate_function CDECL MSVCRT_set_terminate(MSVCRT_terminate_function func)
975 thread_data_t *data = msvcrt_get_thread_data();
976 MSVCRT_terminate_function previous = data->terminate_handler;
977 TRACE("(%p) returning %p\n",func,previous);
978 data->terminate_handler = func;
979 return previous;
982 /******************************************************************
983 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
985 * Install a handler to be called when unexpected() is called.
987 * PARAMS
988 * func [I] Handler function to install
990 * RETURNS
991 * The previously installed handler function, if any.
993 MSVCRT_unexpected_function CDECL MSVCRT_set_unexpected(MSVCRT_unexpected_function func)
995 thread_data_t *data = msvcrt_get_thread_data();
996 MSVCRT_unexpected_function previous = data->unexpected_handler;
997 TRACE("(%p) returning %p\n",func,previous);
998 data->unexpected_handler = func;
999 return previous;
1002 /******************************************************************
1003 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
1005 MSVCRT__se_translator_function CDECL MSVCRT__set_se_translator(MSVCRT__se_translator_function func)
1007 thread_data_t *data = msvcrt_get_thread_data();
1008 MSVCRT__se_translator_function previous = data->se_translator;
1009 TRACE("(%p) returning %p\n",func,previous);
1010 data->se_translator = func;
1011 return previous;
1014 /******************************************************************
1015 * ?terminate@@YAXXZ (MSVCRT.@)
1017 * Default handler for an unhandled exception.
1019 * PARAMS
1020 * None.
1022 * RETURNS
1023 * This function does not return. Either control resumes from any
1024 * handler installed by calling set_terminate(), or (by default) abort()
1025 * is called.
1027 void CDECL MSVCRT_terminate(void)
1029 thread_data_t *data = msvcrt_get_thread_data();
1030 if (data->terminate_handler) data->terminate_handler();
1031 MSVCRT_abort();
1034 /******************************************************************
1035 * ?unexpected@@YAXXZ (MSVCRT.@)
1037 void CDECL MSVCRT_unexpected(void)
1039 thread_data_t *data = msvcrt_get_thread_data();
1040 if (data->unexpected_handler) data->unexpected_handler();
1041 MSVCRT_terminate();
1045 /******************************************************************
1046 * __RTtypeid (MSVCRT.@)
1048 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1050 * PARAMS
1051 * cppobj [I] C++ object to get type information for.
1053 * RETURNS
1054 * Success: A type_info object describing cppobj.
1055 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
1056 * exception is thrown. If cppobj is NULL, a bad_typeid exception
1057 * is thrown. In either case, this function does not return.
1059 * NOTES
1060 * This function is usually called by compiler generated code as a result
1061 * of using one of the C++ dynamic cast statements.
1063 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
1065 const type_info *ret;
1067 if (!cppobj)
1069 bad_typeid e;
1070 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
1071 _CxxThrowException( &e, &bad_typeid_exception_type );
1072 return NULL;
1075 __TRY
1077 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1078 ret = obj_locator->type_descriptor;
1080 __EXCEPT_PAGE_FAULT
1082 __non_rtti_object e;
1083 MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
1084 _CxxThrowException( &e, &bad_typeid_exception_type );
1085 return NULL;
1087 __ENDTRY
1088 return ret;
1091 /******************************************************************
1092 * __RTDynamicCast (MSVCRT.@)
1094 * Dynamically cast a C++ object to one of its base classes.
1096 * PARAMS
1097 * cppobj [I] Any C++ object to cast
1098 * unknown [I] Reserved, set to 0
1099 * src [I] type_info object describing cppobj
1100 * dst [I] type_info object describing the base class to cast to
1101 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
1103 * RETURNS
1104 * Success: The address of cppobj, cast to the object described by dst.
1105 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
1106 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
1107 * is thrown and this function does not return.
1109 * NOTES
1110 * This function is usually called by compiler generated code as a result
1111 * of using one of the C++ dynamic cast statements.
1113 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
1114 type_info *src, type_info *dst,
1115 int do_throw)
1117 void *ret;
1119 if (!cppobj) return NULL;
1121 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
1122 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
1124 /* To cast an object at runtime:
1125 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
1126 * 2.Search for the destination type in the class hierarchy
1127 * 3.If destination type is found, return base object address + dest offset
1128 * Otherwise, fail the cast
1130 * FIXME: the unknown parameter doesn't seem to be used for anything
1132 __TRY
1134 int i;
1135 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1136 const rtti_object_hierarchy *obj_bases = obj_locator->type_hierarchy;
1137 const rtti_base_descriptor * const* base_desc = obj_bases->base_classes->bases;
1139 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
1141 ret = NULL;
1142 for (i = 0; i < obj_bases->array_len; i++)
1144 const type_info *typ = base_desc[i]->type_descriptor;
1146 if (!strcmp(typ->mangled, dst->mangled))
1148 /* compute the correct this pointer for that base class */
1149 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
1150 ret = get_this_pointer( &base_desc[i]->offsets, this_ptr );
1151 break;
1154 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
1155 * to a reference, since references cannot be NULL.
1157 if (!ret && do_throw)
1159 const char *msg = "Bad dynamic_cast!";
1160 bad_cast e;
1161 MSVCRT_bad_cast_ctor( &e, &msg );
1162 _CxxThrowException( &e, &bad_cast_exception_type );
1165 __EXCEPT_PAGE_FAULT
1167 __non_rtti_object e;
1168 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1169 _CxxThrowException( &e, &bad_typeid_exception_type );
1170 return NULL;
1172 __ENDTRY
1173 return ret;
1177 /******************************************************************
1178 * __RTCastToVoid (MSVCRT.@)
1180 * Dynamically cast a C++ object to a void*.
1182 * PARAMS
1183 * cppobj [I] The C++ object to cast
1185 * RETURNS
1186 * Success: The base address of the object as a void*.
1187 * Failure: NULL, if cppobj is NULL or has no RTTI.
1189 * NOTES
1190 * This function is usually called by compiler generated code as a result
1191 * of using one of the C++ dynamic cast statements.
1193 void* CDECL MSVCRT___RTCastToVoid(void *cppobj)
1195 void *ret;
1197 if (!cppobj) return NULL;
1199 __TRY
1201 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1202 ret = (char *)cppobj - obj_locator->base_class_offset;
1204 __EXCEPT_PAGE_FAULT
1206 __non_rtti_object e;
1207 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1208 _CxxThrowException( &e, &bad_typeid_exception_type );
1209 return NULL;
1211 __ENDTRY
1212 return ret;