urlmon: Fixed a race in tests causing unexpected Switch call failures.
[wine/wine-gecko.git] / dlls / msvcrt / cpp.c
blob4d0f5d51d68f7fd639ed722f4b347346b5320f63
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 __thiscall __stdcall
77 #define DEFINE_THISCALL_WRAPPER(func,args) \
78 extern void THISCALL(func)(void); \
79 __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
80 "popl %eax\n\t" \
81 "pushl %ecx\n\t" \
82 "pushl %eax\n\t" \
83 "jmp " __ASM_NAME(#func) __ASM_STDCALL(args) )
84 #else /* __i386__ */
86 #define THISCALL(func) func
87 #define THISCALL_NAME(func) __ASM_NAME(#func)
88 #define __thiscall __cdecl
89 #define DEFINE_THISCALL_WRAPPER(func,args) /* nothing */
91 #endif /* __i386__ */
93 extern const vtable_ptr MSVCRT_exception_vtable;
94 extern const vtable_ptr MSVCRT_bad_typeid_vtable;
95 extern const vtable_ptr MSVCRT_bad_cast_vtable;
96 extern const vtable_ptr MSVCRT___non_rtti_object_vtable;
97 extern const vtable_ptr MSVCRT_type_info_vtable;
99 /* get the vtable pointer for a C++ object */
100 static inline const vtable_ptr *get_vtable( void *obj )
102 return *(const vtable_ptr **)obj;
105 static inline const rtti_object_locator *get_obj_locator( void *cppobj )
107 const vtable_ptr *vtable = get_vtable( cppobj );
108 return (const rtti_object_locator *)vtable[-1];
111 static void dump_obj_locator( const rtti_object_locator *ptr )
113 int i;
114 const rtti_object_hierarchy *h = ptr->type_hierarchy;
116 TRACE( "%p: sig=%08x base_offset=%08x flags=%08x type=%p %s hierarchy=%p\n",
117 ptr, ptr->signature, ptr->base_class_offset, ptr->flags,
118 ptr->type_descriptor, dbgstr_type_info(ptr->type_descriptor), ptr->type_hierarchy );
119 TRACE( " hierarchy: sig=%08x attr=%08x len=%d base classes=%p\n",
120 h->signature, h->attributes, h->array_len, h->base_classes );
121 for (i = 0; i < h->array_len; i++)
123 TRACE( " base class %p: num %d off %d,%d,%d attr %08x type %p %s\n",
124 h->base_classes->bases[i],
125 h->base_classes->bases[i]->num_base_classes,
126 h->base_classes->bases[i]->offsets.this_offset,
127 h->base_classes->bases[i]->offsets.vbase_descr,
128 h->base_classes->bases[i]->offsets.vbase_offset,
129 h->base_classes->bases[i]->attributes,
130 h->base_classes->bases[i]->type_descriptor,
131 dbgstr_type_info(h->base_classes->bases[i]->type_descriptor) );
135 /* Internal common ctor for exception */
136 static void EXCEPTION_ctor(exception *_this, const char** name)
138 _this->vtable = &MSVCRT_exception_vtable;
139 if (*name)
141 unsigned int name_len = strlen(*name) + 1;
142 _this->name = MSVCRT_malloc(name_len);
143 memcpy(_this->name, *name, name_len);
144 _this->do_free = TRUE;
146 else
148 _this->name = NULL;
149 _this->do_free = FALSE;
153 /******************************************************************
154 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
156 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor,8)
157 exception * __thiscall MSVCRT_exception_ctor(exception * _this, const char ** name)
159 TRACE("(%p,%s)\n", _this, *name);
160 EXCEPTION_ctor(_this, name);
161 return _this;
164 /******************************************************************
165 * ??0exception@@QAE@ABQBDH@Z (MSVCRT.@)
167 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor_noalloc,12)
168 exception * __thiscall MSVCRT_exception_ctor_noalloc(exception * _this, char ** name, int noalloc)
170 TRACE("(%p,%s)\n", _this, *name);
171 _this->vtable = &MSVCRT_exception_vtable;
172 _this->name = *name;
173 _this->do_free = FALSE;
174 return _this;
177 /******************************************************************
178 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
180 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor,8)
181 exception * __thiscall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
183 TRACE("(%p,%p)\n", _this, rhs);
185 if (!rhs->do_free)
187 _this->vtable = &MSVCRT_exception_vtable;
188 _this->name = rhs->name;
189 _this->do_free = FALSE;
191 else
192 EXCEPTION_ctor(_this, (const char**)&rhs->name);
193 TRACE("name = %s\n", _this->name);
194 return _this;
197 /******************************************************************
198 * ??0exception@@QAE@XZ (MSVCRT.@)
200 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor,4)
201 exception * __thiscall MSVCRT_exception_default_ctor(exception * _this)
203 static const char* empty = NULL;
205 TRACE("(%p)\n", _this);
206 EXCEPTION_ctor(_this, &empty);
207 return _this;
210 /******************************************************************
211 * ??1exception@@UAE@XZ (MSVCRT.@)
213 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor,4)
214 void __thiscall MSVCRT_exception_dtor(exception * _this)
216 TRACE("(%p)\n", _this);
217 _this->vtable = &MSVCRT_exception_vtable;
218 if (_this->do_free) MSVCRT_free(_this->name);
221 /******************************************************************
222 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
224 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals,8)
225 exception * __thiscall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
227 TRACE("(%p %p)\n", _this, rhs);
228 if (_this != rhs)
230 MSVCRT_exception_dtor(_this);
231 MSVCRT_exception_copy_ctor(_this, rhs);
233 TRACE("name = %s\n", _this->name);
234 return _this;
237 /******************************************************************
238 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
240 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor,8)
241 void * __thiscall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
243 TRACE("(%p %x)\n", _this, flags);
244 if (flags & 2)
246 /* we have an array, with the number of elements stored before the first object */
247 int i, *ptr = (int *)_this - 1;
249 for (i = *ptr - 1; i >= 0; i--) MSVCRT_exception_dtor(_this + i);
250 MSVCRT_operator_delete(ptr);
252 else
254 MSVCRT_exception_dtor(_this);
255 if (flags & 1) MSVCRT_operator_delete(_this);
257 return _this;
260 /******************************************************************
261 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
263 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor,8)
264 void * __thiscall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
266 TRACE("(%p %x)\n", _this, flags);
267 MSVCRT_exception_dtor(_this);
268 if (flags & 1) MSVCRT_operator_delete(_this);
269 return _this;
272 /******************************************************************
273 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
275 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception,4)
276 const char * __thiscall MSVCRT_what_exception(exception * _this)
278 TRACE("(%p) returning %s\n", _this, _this->name);
279 return _this->name ? _this->name : "Unknown exception";
282 /******************************************************************
283 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
285 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor,8)
286 bad_typeid * __thiscall MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
288 TRACE("(%p %p)\n", _this, rhs);
289 MSVCRT_exception_copy_ctor(_this, rhs);
290 _this->vtable = &MSVCRT_bad_typeid_vtable;
291 return _this;
294 /******************************************************************
295 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
297 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor,8)
298 bad_typeid * __thiscall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
300 TRACE("(%p %s)\n", _this, name);
301 EXCEPTION_ctor(_this, &name);
302 _this->vtable = &MSVCRT_bad_typeid_vtable;
303 return _this;
306 /******************************************************************
307 * ??_Fbad_typeid@@QAEXXZ (MSVCRT.@)
309 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_default_ctor,4)
310 bad_typeid * __thiscall MSVCRT_bad_typeid_default_ctor(bad_typeid * _this)
312 return MSVCRT_bad_typeid_ctor( _this, "bad typeid" );
315 /******************************************************************
316 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
318 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor,4)
319 void __thiscall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
321 TRACE("(%p)\n", _this);
322 MSVCRT_exception_dtor(_this);
325 /******************************************************************
326 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
328 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals,8)
329 bad_typeid * __thiscall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
331 TRACE("(%p %p)\n", _this, rhs);
332 MSVCRT_exception_opequals(_this, rhs);
333 return _this;
336 /******************************************************************
337 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
339 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor,8)
340 void * __thiscall MSVCRT_bad_typeid_vector_dtor(bad_typeid * _this, unsigned int flags)
342 TRACE("(%p %x)\n", _this, flags);
343 if (flags & 2)
345 /* we have an array, with the number of elements stored before the first object */
346 int i, *ptr = (int *)_this - 1;
348 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_typeid_dtor(_this + i);
349 MSVCRT_operator_delete(ptr);
351 else
353 MSVCRT_bad_typeid_dtor(_this);
354 if (flags & 1) MSVCRT_operator_delete(_this);
356 return _this;
359 /******************************************************************
360 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
362 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor,8)
363 void * __thiscall MSVCRT_bad_typeid_scalar_dtor(bad_typeid * _this, unsigned int flags)
365 TRACE("(%p %x)\n", _this, flags);
366 MSVCRT_bad_typeid_dtor(_this);
367 if (flags & 1) MSVCRT_operator_delete(_this);
368 return _this;
371 /******************************************************************
372 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
374 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor,8)
375 __non_rtti_object * __thiscall MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
376 const __non_rtti_object * rhs)
378 TRACE("(%p %p)\n", _this, rhs);
379 MSVCRT_bad_typeid_copy_ctor(_this, rhs);
380 _this->vtable = &MSVCRT___non_rtti_object_vtable;
381 return _this;
384 /******************************************************************
385 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
387 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor,8)
388 __non_rtti_object * __thiscall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
389 const char * name)
391 TRACE("(%p %s)\n", _this, name);
392 EXCEPTION_ctor(_this, &name);
393 _this->vtable = &MSVCRT___non_rtti_object_vtable;
394 return _this;
397 /******************************************************************
398 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
400 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor,4)
401 void __thiscall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
403 TRACE("(%p)\n", _this);
404 MSVCRT_bad_typeid_dtor(_this);
407 /******************************************************************
408 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
410 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals,8)
411 __non_rtti_object * __thiscall MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
412 const __non_rtti_object *rhs)
414 TRACE("(%p %p)\n", _this, rhs);
415 MSVCRT_bad_typeid_opequals(_this, rhs);
416 return _this;
419 /******************************************************************
420 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
422 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor,8)
423 void * __thiscall MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object * _this, unsigned int flags)
425 TRACE("(%p %x)\n", _this, flags);
426 if (flags & 2)
428 /* we have an array, with the number of elements stored before the first object */
429 int i, *ptr = (int *)_this - 1;
431 for (i = *ptr - 1; i >= 0; i--) MSVCRT___non_rtti_object_dtor(_this + i);
432 MSVCRT_operator_delete(ptr);
434 else
436 MSVCRT___non_rtti_object_dtor(_this);
437 if (flags & 1) MSVCRT_operator_delete(_this);
439 return _this;
442 /******************************************************************
443 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
445 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor,8)
446 void * __thiscall MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object * _this, unsigned int flags)
448 TRACE("(%p %x)\n", _this, flags);
449 MSVCRT___non_rtti_object_dtor(_this);
450 if (flags & 1) MSVCRT_operator_delete(_this);
451 return _this;
454 /******************************************************************
455 * ??0bad_cast@@AAE@PBQBD@Z (MSVCRT.@)
456 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
458 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor,8)
459 bad_cast * __thiscall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
461 TRACE("(%p %s)\n", _this, *name);
462 EXCEPTION_ctor(_this, name);
463 _this->vtable = &MSVCRT_bad_cast_vtable;
464 return _this;
467 /******************************************************************
468 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
470 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor,8)
471 bad_cast * __thiscall MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
473 TRACE("(%p %p)\n", _this, rhs);
474 MSVCRT_exception_copy_ctor(_this, rhs);
475 _this->vtable = &MSVCRT_bad_cast_vtable;
476 return _this;
479 /******************************************************************
480 * ??0bad_cast@@QAE@PBD@Z (MSVCRT.@)
482 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor_charptr,8)
483 bad_cast * __thiscall MSVCRT_bad_cast_ctor_charptr(bad_cast * _this, const char * name)
485 TRACE("(%p %s)\n", _this, name);
486 EXCEPTION_ctor(_this, &name);
487 _this->vtable = &MSVCRT_bad_cast_vtable;
488 return _this;
491 /******************************************************************
492 * ??_Fbad_cast@@QAEXXZ (MSVCRT.@)
494 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_default_ctor,4)
495 bad_cast * __thiscall MSVCRT_bad_cast_default_ctor(bad_cast * _this)
497 return MSVCRT_bad_cast_ctor_charptr( _this, "bad cast" );
500 /******************************************************************
501 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
503 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor,4)
504 void __thiscall MSVCRT_bad_cast_dtor(bad_cast * _this)
506 TRACE("(%p)\n", _this);
507 MSVCRT_exception_dtor(_this);
510 /******************************************************************
511 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
513 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals,8)
514 bad_cast * __thiscall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
516 TRACE("(%p %p)\n", _this, rhs);
517 MSVCRT_exception_opequals(_this, rhs);
518 return _this;
521 /******************************************************************
522 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
524 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor,8)
525 void * __thiscall MSVCRT_bad_cast_vector_dtor(bad_cast * _this, unsigned int flags)
527 TRACE("(%p %x)\n", _this, flags);
528 if (flags & 2)
530 /* we have an array, with the number of elements stored before the first object */
531 int i, *ptr = (int *)_this - 1;
533 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_cast_dtor(_this + i);
534 MSVCRT_operator_delete(ptr);
536 else
538 MSVCRT_bad_cast_dtor(_this);
539 if (flags & 1) MSVCRT_operator_delete(_this);
541 return _this;
544 /******************************************************************
545 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
547 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor,8)
548 void * __thiscall MSVCRT_bad_cast_scalar_dtor(bad_cast * _this, unsigned int flags)
550 TRACE("(%p %x)\n", _this, flags);
551 MSVCRT_bad_cast_dtor(_this);
552 if (flags & 1) MSVCRT_operator_delete(_this);
553 return _this;
556 /******************************************************************
557 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
559 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals,8)
560 int __thiscall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
562 int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
563 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
564 return ret;
567 /******************************************************************
568 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
570 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals,8)
571 int __thiscall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
573 int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
574 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
575 return ret;
578 /******************************************************************
579 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
581 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before,8)
582 int __thiscall MSVCRT_type_info_before(type_info * _this, const type_info * rhs)
584 int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
585 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
586 return ret;
589 /******************************************************************
590 * ??1type_info@@UAE@XZ (MSVCRT.@)
592 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor,4)
593 void __thiscall MSVCRT_type_info_dtor(type_info * _this)
595 TRACE("(%p)\n", _this);
596 MSVCRT_free(_this->name);
599 /******************************************************************
600 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
602 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name,4)
603 const char * __thiscall MSVCRT_type_info_name(type_info * _this)
605 if (!_this->name)
607 /* Create and set the demangled name */
608 /* Nota: mangled name in type_info struct always start with a '.', while
609 * it isn't valid for mangled name.
610 * Is this '.' really part of the mangled name, or has it some other meaning ?
612 char* name = __unDName(0, _this->mangled + 1, 0,
613 MSVCRT_malloc, MSVCRT_free, 0x2800);
615 if (name)
617 unsigned int len = strlen(name);
619 /* It seems _unDName may leave blanks at the end of the demangled name */
620 while (len && name[--len] == ' ')
621 name[len] = '\0';
623 _mlock(_EXIT_LOCK2);
625 if (_this->name)
627 /* Another thread set this member since we checked above - use it */
628 MSVCRT_free(name);
630 else
631 _this->name = name;
633 _munlock(_EXIT_LOCK2);
636 TRACE("(%p) returning %s\n", _this, _this->name);
637 return _this->name;
640 /******************************************************************
641 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
643 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name,4)
644 const char * __thiscall MSVCRT_type_info_raw_name(type_info * _this)
646 TRACE("(%p) returning %s\n", _this, _this->mangled);
647 return _this->mangled;
650 /* Unexported */
651 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor,8)
652 void * __thiscall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
654 TRACE("(%p %x)\n", _this, flags);
655 if (flags & 2)
657 /* we have an array, with the number of elements stored before the first object */
658 int i, *ptr = (int *)_this - 1;
660 for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
661 MSVCRT_operator_delete(ptr);
663 else
665 MSVCRT_type_info_dtor(_this);
666 if (flags & 1) MSVCRT_operator_delete(_this);
668 return _this;
671 /* vtables */
673 #ifdef _WIN64
675 #define __ASM_VTABLE(name,funcs) \
676 __asm__(".data\n" \
677 "\t.align 8\n" \
678 "\t.quad " __ASM_NAME(#name "_rtti") "\n" \
679 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
680 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
681 "\t.quad " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
682 funcs "\n\t.text");
684 #define __ASM_EXCEPTION_VTABLE(name) \
685 __ASM_VTABLE(name, "\t.quad " THISCALL_NAME(MSVCRT_what_exception) )
687 #else
689 #define __ASM_VTABLE(name,funcs) \
690 __asm__(".data\n" \
691 "\t.align 4\n" \
692 "\t.long " __ASM_NAME(#name "_rtti") "\n" \
693 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
694 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
695 "\t.long " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
696 funcs "\n\t.text");
698 #define __ASM_EXCEPTION_VTABLE(name) \
699 __ASM_VTABLE(name, "\t.long " THISCALL_NAME(MSVCRT_what_exception) )
701 #endif /* _WIN64 */
703 #ifndef __GNUC__
704 void __asm_dummy_vtables(void) {
705 #endif
707 __ASM_VTABLE(type_info,"")
708 __ASM_EXCEPTION_VTABLE(exception)
709 __ASM_EXCEPTION_VTABLE(bad_typeid)
710 __ASM_EXCEPTION_VTABLE(bad_cast)
711 __ASM_EXCEPTION_VTABLE(__non_rtti_object)
713 #ifndef __GNUC__
715 #endif
717 /* Static RTTI for exported objects */
719 static const type_info exception_type_info =
721 &MSVCRT_type_info_vtable,
722 NULL,
723 ".?AVexception@@"
726 static const rtti_base_descriptor exception_rtti_base_descriptor =
728 &exception_type_info,
730 { 0, -1, 0 },
734 static const rtti_base_array exception_rtti_base_array =
737 &exception_rtti_base_descriptor,
738 NULL,
739 NULL
743 static const rtti_object_hierarchy exception_type_hierarchy =
748 &exception_rtti_base_array
751 const rtti_object_locator exception_rtti =
756 &exception_type_info,
757 &exception_type_hierarchy
760 static const cxx_type_info exception_cxx_type_info =
763 &exception_type_info,
764 { 0, -1, 0 },
765 sizeof(exception),
766 (cxx_copy_ctor)THISCALL(MSVCRT_exception_copy_ctor)
769 static const type_info bad_typeid_type_info =
771 &MSVCRT_type_info_vtable,
772 NULL,
773 ".?AVbad_typeid@@"
776 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
778 &bad_typeid_type_info,
780 { 0, -1, 0 },
784 static const rtti_base_array bad_typeid_rtti_base_array =
787 &bad_typeid_rtti_base_descriptor,
788 &exception_rtti_base_descriptor,
789 NULL
793 static const rtti_object_hierarchy bad_typeid_type_hierarchy =
798 &bad_typeid_rtti_base_array
801 const rtti_object_locator bad_typeid_rtti =
806 &bad_typeid_type_info,
807 &bad_typeid_type_hierarchy
810 static const cxx_type_info bad_typeid_cxx_type_info =
813 &bad_typeid_type_info,
814 { 0, -1, 0 },
815 sizeof(exception),
816 (cxx_copy_ctor)THISCALL(MSVCRT_bad_typeid_copy_ctor)
819 static const type_info bad_cast_type_info =
821 &MSVCRT_type_info_vtable,
822 NULL,
823 ".?AVbad_cast@@"
826 static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
828 &bad_cast_type_info,
830 { 0, -1, 0 },
834 static const rtti_base_array bad_cast_rtti_base_array =
837 &bad_cast_rtti_base_descriptor,
838 &exception_rtti_base_descriptor,
839 NULL
843 static const rtti_object_hierarchy bad_cast_type_hierarchy =
848 &bad_cast_rtti_base_array
851 const rtti_object_locator bad_cast_rtti =
856 &bad_cast_type_info,
857 &bad_cast_type_hierarchy
860 static const cxx_type_info bad_cast_cxx_type_info =
863 &bad_cast_type_info,
864 { 0, -1, 0 },
865 sizeof(exception),
866 (cxx_copy_ctor)THISCALL(MSVCRT_bad_cast_copy_ctor)
869 static const type_info __non_rtti_object_type_info =
871 &MSVCRT_type_info_vtable,
872 NULL,
873 ".?AV__non_rtti_object@@"
876 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
878 &__non_rtti_object_type_info,
880 { 0, -1, 0 },
884 static const rtti_base_array __non_rtti_object_rtti_base_array =
887 &__non_rtti_object_rtti_base_descriptor,
888 &bad_typeid_rtti_base_descriptor,
889 &exception_rtti_base_descriptor
893 static const rtti_object_hierarchy __non_rtti_object_type_hierarchy =
898 &__non_rtti_object_rtti_base_array
901 const rtti_object_locator __non_rtti_object_rtti =
906 &__non_rtti_object_type_info,
907 &__non_rtti_object_type_hierarchy
910 static const cxx_type_info __non_rtti_object_cxx_type_info =
913 &__non_rtti_object_type_info,
914 { 0, -1, 0 },
915 sizeof(exception),
916 (cxx_copy_ctor)THISCALL(MSVCRT___non_rtti_object_copy_ctor)
919 static const type_info type_info_type_info =
921 &MSVCRT_type_info_vtable,
922 NULL,
923 ".?AVtype_info@@"
926 static const rtti_base_descriptor type_info_rtti_base_descriptor =
928 &type_info_type_info,
930 { 0, -1, 0 },
934 static const rtti_base_array type_info_rtti_base_array =
937 &type_info_rtti_base_descriptor,
938 NULL,
939 NULL
943 static const rtti_object_hierarchy type_info_type_hierarchy =
948 &type_info_rtti_base_array
951 const rtti_object_locator type_info_rtti =
956 &type_info_type_info,
957 &type_info_type_hierarchy
961 * Exception RTTI for cpp objects
963 static const cxx_type_info_table bad_cast_type_info_table =
967 &__non_rtti_object_cxx_type_info,
968 &bad_typeid_cxx_type_info,
969 &exception_cxx_type_info
973 static const cxx_exception_type bad_cast_exception_type =
976 (void*)THISCALL(MSVCRT_bad_cast_dtor),
977 NULL,
978 &bad_cast_type_info_table
981 static const cxx_type_info_table bad_typeid_type_info_table =
985 &bad_cast_cxx_type_info,
986 &exception_cxx_type_info,
987 NULL
991 static const cxx_exception_type bad_typeid_exception_type =
994 (void*)THISCALL(MSVCRT_bad_typeid_dtor),
995 NULL,
996 &bad_cast_type_info_table
999 static const cxx_exception_type __non_rtti_object_exception_type =
1002 (void*)THISCALL(MSVCRT___non_rtti_object_dtor),
1003 NULL,
1004 &bad_typeid_type_info_table
1008 /******************************************************************
1009 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
1011 * Install a handler to be called when terminate() is called.
1013 * PARAMS
1014 * func [I] Handler function to install
1016 * RETURNS
1017 * The previously installed handler function, if any.
1019 MSVCRT_terminate_function CDECL MSVCRT_set_terminate(MSVCRT_terminate_function func)
1021 thread_data_t *data = msvcrt_get_thread_data();
1022 MSVCRT_terminate_function previous = data->terminate_handler;
1023 TRACE("(%p) returning %p\n",func,previous);
1024 data->terminate_handler = func;
1025 return previous;
1028 /******************************************************************
1029 * _get_terminate (MSVCRT.@)
1031 MSVCRT_terminate_function CDECL MSVCRT__get_terminate(void)
1033 thread_data_t *data = msvcrt_get_thread_data();
1034 TRACE("returning %p\n", data->terminate_handler);
1035 return data->terminate_handler;
1038 /******************************************************************
1039 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
1041 * Install a handler to be called when unexpected() is called.
1043 * PARAMS
1044 * func [I] Handler function to install
1046 * RETURNS
1047 * The previously installed handler function, if any.
1049 MSVCRT_unexpected_function CDECL MSVCRT_set_unexpected(MSVCRT_unexpected_function func)
1051 thread_data_t *data = msvcrt_get_thread_data();
1052 MSVCRT_unexpected_function previous = data->unexpected_handler;
1053 TRACE("(%p) returning %p\n",func,previous);
1054 data->unexpected_handler = func;
1055 return previous;
1058 /******************************************************************
1059 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
1061 MSVCRT__se_translator_function CDECL MSVCRT__set_se_translator(MSVCRT__se_translator_function func)
1063 thread_data_t *data = msvcrt_get_thread_data();
1064 MSVCRT__se_translator_function previous = data->se_translator;
1065 TRACE("(%p) returning %p\n",func,previous);
1066 data->se_translator = func;
1067 return previous;
1070 /******************************************************************
1071 * ?terminate@@YAXXZ (MSVCRT.@)
1073 * Default handler for an unhandled exception.
1075 * PARAMS
1076 * None.
1078 * RETURNS
1079 * This function does not return. Either control resumes from any
1080 * handler installed by calling set_terminate(), or (by default) abort()
1081 * is called.
1083 void CDECL MSVCRT_terminate(void)
1085 thread_data_t *data = msvcrt_get_thread_data();
1086 if (data->terminate_handler) data->terminate_handler();
1087 MSVCRT_abort();
1090 /******************************************************************
1091 * ?unexpected@@YAXXZ (MSVCRT.@)
1093 void CDECL MSVCRT_unexpected(void)
1095 thread_data_t *data = msvcrt_get_thread_data();
1096 if (data->unexpected_handler) data->unexpected_handler();
1097 MSVCRT_terminate();
1101 /******************************************************************
1102 * __RTtypeid (MSVCRT.@)
1104 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1106 * PARAMS
1107 * cppobj [I] C++ object to get type information for.
1109 * RETURNS
1110 * Success: A type_info object describing cppobj.
1111 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
1112 * exception is thrown. If cppobj is NULL, a bad_typeid exception
1113 * is thrown. In either case, this function does not return.
1115 * NOTES
1116 * This function is usually called by compiler generated code as a result
1117 * of using one of the C++ dynamic cast statements.
1119 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
1121 const type_info *ret;
1123 if (!cppobj)
1125 bad_typeid e;
1126 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
1127 _CxxThrowException( &e, &bad_typeid_exception_type );
1128 return NULL;
1131 __TRY
1133 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1134 ret = obj_locator->type_descriptor;
1136 __EXCEPT_PAGE_FAULT
1138 __non_rtti_object e;
1139 MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
1140 _CxxThrowException( &e, &bad_typeid_exception_type );
1141 return NULL;
1143 __ENDTRY
1144 return ret;
1147 /******************************************************************
1148 * __RTDynamicCast (MSVCRT.@)
1150 * Dynamically cast a C++ object to one of its base classes.
1152 * PARAMS
1153 * cppobj [I] Any C++ object to cast
1154 * unknown [I] Reserved, set to 0
1155 * src [I] type_info object describing cppobj
1156 * dst [I] type_info object describing the base class to cast to
1157 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
1159 * RETURNS
1160 * Success: The address of cppobj, cast to the object described by dst.
1161 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
1162 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
1163 * is thrown and this function does not return.
1165 * NOTES
1166 * This function is usually called by compiler generated code as a result
1167 * of using one of the C++ dynamic cast statements.
1169 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
1170 type_info *src, type_info *dst,
1171 int do_throw)
1173 void *ret;
1175 if (!cppobj) return NULL;
1177 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
1178 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
1180 /* To cast an object at runtime:
1181 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
1182 * 2.Search for the destination type in the class hierarchy
1183 * 3.If destination type is found, return base object address + dest offset
1184 * Otherwise, fail the cast
1186 * FIXME: the unknown parameter doesn't seem to be used for anything
1188 __TRY
1190 int i;
1191 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1192 const rtti_object_hierarchy *obj_bases = obj_locator->type_hierarchy;
1193 const rtti_base_descriptor * const* base_desc = obj_bases->base_classes->bases;
1195 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
1197 ret = NULL;
1198 for (i = 0; i < obj_bases->array_len; i++)
1200 const type_info *typ = base_desc[i]->type_descriptor;
1202 if (!strcmp(typ->mangled, dst->mangled))
1204 /* compute the correct this pointer for that base class */
1205 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
1206 ret = get_this_pointer( &base_desc[i]->offsets, this_ptr );
1207 break;
1210 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
1211 * to a reference, since references cannot be NULL.
1213 if (!ret && do_throw)
1215 const char *msg = "Bad dynamic_cast!";
1216 bad_cast e;
1217 MSVCRT_bad_cast_ctor( &e, &msg );
1218 _CxxThrowException( &e, &bad_cast_exception_type );
1221 __EXCEPT_PAGE_FAULT
1223 __non_rtti_object e;
1224 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1225 _CxxThrowException( &e, &bad_typeid_exception_type );
1226 return NULL;
1228 __ENDTRY
1229 return ret;
1233 /******************************************************************
1234 * __RTCastToVoid (MSVCRT.@)
1236 * Dynamically cast a C++ object to a void*.
1238 * PARAMS
1239 * cppobj [I] The C++ object to cast
1241 * RETURNS
1242 * Success: The base address of the object as a void*.
1243 * Failure: NULL, if cppobj is NULL or has no RTTI.
1245 * NOTES
1246 * This function is usually called by compiler generated code as a result
1247 * of using one of the C++ dynamic cast statements.
1249 void* CDECL MSVCRT___RTCastToVoid(void *cppobj)
1251 void *ret;
1253 if (!cppobj) return NULL;
1255 __TRY
1257 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1258 ret = (char *)cppobj - obj_locator->base_class_offset;
1260 __EXCEPT_PAGE_FAULT
1262 __non_rtti_object e;
1263 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1264 _CxxThrowException( &e, &bad_typeid_exception_type );
1265 return NULL;
1267 __ENDTRY
1268 return ret;