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
24 #include <wine/exception.h>
25 #include <internal/wine/msvcrt.h>
26 #include <internal/wine/cppexcept.h>
28 typedef exception bad_cast
;
29 typedef exception bad_typeid
;
30 typedef exception __non_rtti_object
;
32 typedef struct _rtti_base_descriptor
34 const type_info
*type_descriptor
;
36 this_ptr_offsets offsets
; /* offsets for computing the this pointer */
37 unsigned int attributes
;
38 } rtti_base_descriptor
;
40 typedef struct _rtti_base_array
42 const rtti_base_descriptor
*bases
[3]; /* First element is the class itself */
45 typedef struct _rtti_object_hierarchy
47 unsigned int signature
;
48 unsigned int attributes
;
49 int array_len
; /* Size of the array pointed to by 'base_classes' */
50 const rtti_base_array
*base_classes
;
51 } rtti_object_hierarchy
;
53 typedef struct _rtti_object_locator
55 unsigned int signature
;
56 int base_class_offset
;
58 const type_info
*type_descriptor
;
59 const rtti_object_hierarchy
*type_hierarchy
;
60 } rtti_object_locator
;
62 #ifdef __i386__ /* thiscall functions are i386-specific */
64 #define THISCALL(func) __thiscall_ ## func
65 #define THISCALL_NAME(func) __ASM_NAME("__thiscall_" #func)
68 #pragma message ("DEFINE_THISCALL_WRAPPER broken")
69 #define DEFINE_THISCALL_WRAPPER(func,args) \
70 extern void THISCALL(func)(void);
71 // __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
75 // jmp __ASM_NAME(#func) __ASM_STDCALL(args) )
77 #define DEFINE_THISCALL_WRAPPER(func,args) \
78 extern void THISCALL(func)(void); \
79 __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
83 "jmp " __ASM_NAME(#func) __ASM_STDCALL(args) )
88 #define THISCALL(func) func
89 #define THISCALL_NAME(func) __ASM_NAME(#func)
90 #define DEFINE_THISCALL_WRAPPER(func,args) /* nothing */
94 extern const vtable_ptr MSVCRT_exception_vtable
;
95 extern const vtable_ptr MSVCRT_bad_typeid_vtable
;
96 extern const vtable_ptr MSVCRT_bad_cast_vtable
;
97 extern const vtable_ptr MSVCRT___non_rtti_object_vtable
;
98 extern const vtable_ptr MSVCRT_type_info_vtable
;
100 /* get the vtable pointer for a C++ object */
101 static inline const vtable_ptr
*get_vtable( void *obj
)
103 return *(const vtable_ptr
**)obj
;
106 static inline const rtti_object_locator
*get_obj_locator( void *cppobj
)
108 const vtable_ptr
*vtable
= get_vtable( cppobj
);
109 return (const rtti_object_locator
*)vtable
[-1];
112 static void dump_obj_locator( const rtti_object_locator
*ptr
)
115 const rtti_object_hierarchy
*h
= ptr
->type_hierarchy
;
117 TRACE( "%p: sig=%08x base_offset=%08x flags=%08x type=%p %s hierarchy=%p\n",
118 ptr
, ptr
->signature
, ptr
->base_class_offset
, ptr
->flags
,
119 ptr
->type_descriptor
, dbgstr_type_info(ptr
->type_descriptor
), ptr
->type_hierarchy
);
120 TRACE( " hierarchy: sig=%08x attr=%08x len=%d base classes=%p\n",
121 h
->signature
, h
->attributes
, h
->array_len
, h
->base_classes
);
122 for (i
= 0; i
< h
->array_len
; i
++)
124 TRACE( " base class %p: num %d off %d,%d,%d attr %08x type %p %s\n",
125 h
->base_classes
->bases
[i
],
126 h
->base_classes
->bases
[i
]->num_base_classes
,
127 h
->base_classes
->bases
[i
]->offsets
.this_offset
,
128 h
->base_classes
->bases
[i
]->offsets
.vbase_descr
,
129 h
->base_classes
->bases
[i
]->offsets
.vbase_offset
,
130 h
->base_classes
->bases
[i
]->attributes
,
131 h
->base_classes
->bases
[i
]->type_descriptor
,
132 dbgstr_type_info(h
->base_classes
->bases
[i
]->type_descriptor
) );
136 /* Internal common ctor for exception */
137 static void WINAPI
EXCEPTION_ctor(exception
*_this
, const char** name
)
139 _this
->vtable
= &MSVCRT_exception_vtable
;
142 unsigned int name_len
= strlen(*name
) + 1;
143 _this
->name
= MSVCRT_malloc(name_len
);
144 memcpy(_this
->name
, *name
, name_len
);
145 _this
->do_free
= TRUE
;
150 _this
->do_free
= FALSE
;
154 /******************************************************************
155 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
157 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor
,8)
158 exception
* __stdcall
MSVCRT_exception_ctor(exception
* _this
, const char ** name
)
160 TRACE("(%p,%s)\n", _this
, *name
);
161 EXCEPTION_ctor(_this
, name
);
165 /******************************************************************
166 * ??0exception@@QAE@ABQBDH@Z (MSVCRT.@)
168 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor_noalloc
,12)
169 exception
* __stdcall
MSVCRT_exception_ctor_noalloc(exception
* _this
, char ** name
, int noalloc
)
171 TRACE("(%p,%s)\n", _this
, *name
);
172 _this
->vtable
= &MSVCRT_exception_vtable
;
174 _this
->do_free
= FALSE
;
178 /******************************************************************
179 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
181 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor
,8)
182 exception
* __stdcall
MSVCRT_exception_copy_ctor(exception
* _this
, const exception
* rhs
)
184 TRACE("(%p,%p)\n", _this
, rhs
);
188 _this
->vtable
= &MSVCRT_exception_vtable
;
189 _this
->name
= rhs
->name
;
190 _this
->do_free
= FALSE
;
193 EXCEPTION_ctor(_this
, (const char**)&rhs
->name
);
194 TRACE("name = %s\n", _this
->name
);
198 /******************************************************************
199 * ??0exception@@QAE@XZ (MSVCRT.@)
201 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor
,4)
202 exception
* __stdcall
MSVCRT_exception_default_ctor(exception
* _this
)
204 static const char* empty
= NULL
;
206 TRACE("(%p)\n", _this
);
207 EXCEPTION_ctor(_this
, &empty
);
211 /******************************************************************
212 * ??1exception@@UAE@XZ (MSVCRT.@)
214 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor
,4)
215 void __stdcall
MSVCRT_exception_dtor(exception
* _this
)
217 TRACE("(%p)\n", _this
);
218 _this
->vtable
= &MSVCRT_exception_vtable
;
219 if (_this
->do_free
) MSVCRT_free(_this
->name
);
222 /******************************************************************
223 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
225 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals
,8)
226 exception
* __stdcall
MSVCRT_exception_opequals(exception
* _this
, const exception
* rhs
)
228 TRACE("(%p %p)\n", _this
, rhs
);
231 MSVCRT_exception_dtor(_this
);
232 MSVCRT_exception_copy_ctor(_this
, rhs
);
234 TRACE("name = %s\n", _this
->name
);
238 /******************************************************************
239 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
241 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor
,8)
242 void * __stdcall
MSVCRT_exception_vector_dtor(exception
* _this
, unsigned int flags
)
244 TRACE("(%p %x)\n", _this
, flags
);
247 /* we have an array, with the number of elements stored before the first object */
248 int i
, *ptr
= (int *)_this
- 1;
250 for (i
= *ptr
- 1; i
>= 0; i
--) MSVCRT_exception_dtor(_this
+ i
);
251 MSVCRT_operator_delete(ptr
);
255 MSVCRT_exception_dtor(_this
);
256 if (flags
& 1) MSVCRT_operator_delete(_this
);
261 /******************************************************************
262 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
264 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor
,8)
265 void * __stdcall
MSVCRT_exception_scalar_dtor(exception
* _this
, unsigned int flags
)
267 TRACE("(%p %x)\n", _this
, flags
);
268 MSVCRT_exception_dtor(_this
);
269 if (flags
& 1) MSVCRT_operator_delete(_this
);
273 /******************************************************************
274 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
276 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception
,4)
277 const char * __stdcall
MSVCRT_what_exception(exception
* _this
)
279 TRACE("(%p) returning %s\n", _this
, _this
->name
);
280 return _this
->name
? _this
->name
: "Unknown exception";
283 /******************************************************************
284 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
286 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor
,8)
287 bad_typeid
* __stdcall
MSVCRT_bad_typeid_copy_ctor(bad_typeid
* _this
, const bad_typeid
* rhs
)
289 TRACE("(%p %p)\n", _this
, rhs
);
290 MSVCRT_exception_copy_ctor(_this
, rhs
);
291 _this
->vtable
= &MSVCRT_bad_typeid_vtable
;
295 /******************************************************************
296 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
298 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor
,8)
299 bad_typeid
* __stdcall
MSVCRT_bad_typeid_ctor(bad_typeid
* _this
, const char * name
)
301 TRACE("(%p %s)\n", _this
, name
);
302 EXCEPTION_ctor(_this
, &name
);
303 _this
->vtable
= &MSVCRT_bad_typeid_vtable
;
307 /******************************************************************
308 * ??_Fbad_typeid@@QAEXXZ (MSVCRT.@)
310 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_default_ctor
,4)
311 bad_typeid
* __stdcall
MSVCRT_bad_typeid_default_ctor(bad_typeid
* _this
)
313 return MSVCRT_bad_typeid_ctor( _this
, "bad typeid" );
316 /******************************************************************
317 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
319 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor
,4)
320 void __stdcall
MSVCRT_bad_typeid_dtor(bad_typeid
* _this
)
322 TRACE("(%p)\n", _this
);
323 MSVCRT_exception_dtor(_this
);
326 /******************************************************************
327 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
329 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals
,8)
330 bad_typeid
* __stdcall
MSVCRT_bad_typeid_opequals(bad_typeid
* _this
, const bad_typeid
* rhs
)
332 TRACE("(%p %p)\n", _this
, rhs
);
333 MSVCRT_exception_opequals(_this
, rhs
);
337 /******************************************************************
338 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
340 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor
,8)
341 void * __stdcall
MSVCRT_bad_typeid_vector_dtor(bad_typeid
* _this
, unsigned int flags
)
343 TRACE("(%p %x)\n", _this
, flags
);
346 /* we have an array, with the number of elements stored before the first object */
347 int i
, *ptr
= (int *)_this
- 1;
349 for (i
= *ptr
- 1; i
>= 0; i
--) MSVCRT_bad_typeid_dtor(_this
+ i
);
350 MSVCRT_operator_delete(ptr
);
354 MSVCRT_bad_typeid_dtor(_this
);
355 if (flags
& 1) MSVCRT_operator_delete(_this
);
360 /******************************************************************
361 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
363 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor
,8)
364 void * __stdcall
MSVCRT_bad_typeid_scalar_dtor(bad_typeid
* _this
, unsigned int flags
)
366 TRACE("(%p %x)\n", _this
, flags
);
367 MSVCRT_bad_typeid_dtor(_this
);
368 if (flags
& 1) MSVCRT_operator_delete(_this
);
372 /******************************************************************
373 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
375 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor
,8)
376 __non_rtti_object
* __stdcall
MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object
* _this
,
377 const __non_rtti_object
* rhs
)
379 TRACE("(%p %p)\n", _this
, rhs
);
380 MSVCRT_bad_typeid_copy_ctor(_this
, rhs
);
381 _this
->vtable
= &MSVCRT___non_rtti_object_vtable
;
385 /******************************************************************
386 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
388 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor
,8)
389 __non_rtti_object
* __stdcall
MSVCRT___non_rtti_object_ctor(__non_rtti_object
* _this
,
392 TRACE("(%p %s)\n", _this
, name
);
393 EXCEPTION_ctor(_this
, &name
);
394 _this
->vtable
= &MSVCRT___non_rtti_object_vtable
;
398 /******************************************************************
399 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
401 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor
,4)
402 void __stdcall
MSVCRT___non_rtti_object_dtor(__non_rtti_object
* _this
)
404 TRACE("(%p)\n", _this
);
405 MSVCRT_bad_typeid_dtor(_this
);
408 /******************************************************************
409 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
411 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals
,8)
412 __non_rtti_object
* __stdcall
MSVCRT___non_rtti_object_opequals(__non_rtti_object
* _this
,
413 const __non_rtti_object
*rhs
)
415 TRACE("(%p %p)\n", _this
, rhs
);
416 MSVCRT_bad_typeid_opequals(_this
, rhs
);
420 /******************************************************************
421 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
423 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor
,8)
424 void * __stdcall
MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object
* _this
, unsigned int flags
)
426 TRACE("(%p %x)\n", _this
, flags
);
429 /* we have an array, with the number of elements stored before the first object */
430 int i
, *ptr
= (int *)_this
- 1;
432 for (i
= *ptr
- 1; i
>= 0; i
--) MSVCRT___non_rtti_object_dtor(_this
+ i
);
433 MSVCRT_operator_delete(ptr
);
437 MSVCRT___non_rtti_object_dtor(_this
);
438 if (flags
& 1) MSVCRT_operator_delete(_this
);
443 /******************************************************************
444 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
446 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor
,8)
447 void * __stdcall
MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object
* _this
, unsigned int flags
)
449 TRACE("(%p %x)\n", _this
, flags
);
450 MSVCRT___non_rtti_object_dtor(_this
);
451 if (flags
& 1) MSVCRT_operator_delete(_this
);
455 /******************************************************************
456 * ??0bad_cast@@AAE@PBQBD@Z (MSVCRT.@)
457 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
459 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor
,8)
460 bad_cast
* __stdcall
MSVCRT_bad_cast_ctor(bad_cast
* _this
, const char ** name
)
462 TRACE("(%p %s)\n", _this
, *name
);
463 EXCEPTION_ctor(_this
, name
);
464 _this
->vtable
= &MSVCRT_bad_cast_vtable
;
468 /******************************************************************
469 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
471 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor
,8)
472 bad_cast
* __stdcall
MSVCRT_bad_cast_copy_ctor(bad_cast
* _this
, const bad_cast
* rhs
)
474 TRACE("(%p %p)\n", _this
, rhs
);
475 MSVCRT_exception_copy_ctor(_this
, rhs
);
476 _this
->vtable
= &MSVCRT_bad_cast_vtable
;
480 /******************************************************************
481 * ??0bad_cast@@QAE@PBD@Z (MSVCRT.@)
483 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor_charptr
,8)
484 bad_cast
* __stdcall
MSVCRT_bad_cast_ctor_charptr(bad_cast
* _this
, const char * name
)
486 TRACE("(%p %s)\n", _this
, name
);
487 EXCEPTION_ctor(_this
, &name
);
488 _this
->vtable
= &MSVCRT_bad_cast_vtable
;
492 /******************************************************************
493 * ??_Fbad_cast@@QAEXXZ (MSVCRT.@)
495 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_default_ctor
,4)
496 bad_cast
* __stdcall
MSVCRT_bad_cast_default_ctor(bad_cast
* _this
)
498 return MSVCRT_bad_cast_ctor_charptr( _this
, "bad cast" );
501 /******************************************************************
502 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
504 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor
,4)
505 void __stdcall
MSVCRT_bad_cast_dtor(bad_cast
* _this
)
507 TRACE("(%p)\n", _this
);
508 MSVCRT_exception_dtor(_this
);
511 /******************************************************************
512 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
514 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals
,8)
515 bad_cast
* __stdcall
MSVCRT_bad_cast_opequals(bad_cast
* _this
, const bad_cast
* rhs
)
517 TRACE("(%p %p)\n", _this
, rhs
);
518 MSVCRT_exception_opequals(_this
, rhs
);
522 /******************************************************************
523 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
525 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor
,8)
526 void * __stdcall
MSVCRT_bad_cast_vector_dtor(bad_cast
* _this
, unsigned int flags
)
528 TRACE("(%p %x)\n", _this
, flags
);
531 /* we have an array, with the number of elements stored before the first object */
532 int i
, *ptr
= (int *)_this
- 1;
534 for (i
= *ptr
- 1; i
>= 0; i
--) MSVCRT_bad_cast_dtor(_this
+ i
);
535 MSVCRT_operator_delete(ptr
);
539 MSVCRT_bad_cast_dtor(_this
);
540 if (flags
& 1) MSVCRT_operator_delete(_this
);
545 /******************************************************************
546 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
548 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor
,8)
549 void * __stdcall
MSVCRT_bad_cast_scalar_dtor(bad_cast
* _this
, unsigned int flags
)
551 TRACE("(%p %x)\n", _this
, flags
);
552 MSVCRT_bad_cast_dtor(_this
);
553 if (flags
& 1) MSVCRT_operator_delete(_this
);
557 /******************************************************************
558 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
560 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals
,8)
561 int __stdcall
MSVCRT_type_info_opequals_equals(type_info
* _this
, const type_info
* rhs
)
563 int ret
= !strcmp(_this
->mangled
+ 1, rhs
->mangled
+ 1);
564 TRACE("(%p %p) returning %d\n", _this
, rhs
, ret
);
568 /******************************************************************
569 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
571 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals
,8)
572 int __stdcall
MSVCRT_type_info_opnot_equals(type_info
* _this
, const type_info
* rhs
)
574 int ret
= !!strcmp(_this
->mangled
+ 1, rhs
->mangled
+ 1);
575 TRACE("(%p %p) returning %d\n", _this
, rhs
, ret
);
579 /******************************************************************
580 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
582 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before
,8)
583 int __stdcall
MSVCRT_type_info_before(type_info
* _this
, const type_info
* rhs
)
585 int ret
= strcmp(_this
->mangled
+ 1, rhs
->mangled
+ 1) < 0;
586 TRACE("(%p %p) returning %d\n", _this
, rhs
, ret
);
590 /******************************************************************
591 * ??1type_info@@UAE@XZ (MSVCRT.@)
593 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor
,4)
594 void __stdcall
MSVCRT_type_info_dtor(type_info
* _this
)
596 TRACE("(%p)\n", _this
);
597 MSVCRT_free(_this
->name
);
600 /******************************************************************
601 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
603 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name
,4)
604 const char * __stdcall
MSVCRT_type_info_name(type_info
* _this
)
608 /* Create and set the demangled name */
609 /* Nota: mangled name in type_info struct always start with a '.', while
610 * it isn't valid for mangled name.
611 * Is this '.' really part of the mangled name, or has it some other meaning ?
613 char* name
= __unDName(0, _this
->mangled
+ 1, 0,
614 MSVCRT_malloc
, MSVCRT_free
, 0x2800);
618 unsigned int len
= strlen(name
);
620 /* It seems _unDName may leave blanks at the end of the demangled name */
621 while (len
&& name
[--len
] == ' ')
628 /* Another thread set this member since we checked above - use it */
634 _munlock(_EXIT_LOCK2
);
637 TRACE("(%p) returning %s\n", _this
, _this
->name
);
641 /******************************************************************
642 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
644 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name
,4)
645 const char * __stdcall
MSVCRT_type_info_raw_name(type_info
* _this
)
647 TRACE("(%p) returning %s\n", _this
, _this
->mangled
);
648 return _this
->mangled
;
652 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor
,8)
653 void * __stdcall
MSVCRT_type_info_vector_dtor(type_info
* _this
, unsigned int flags
)
655 TRACE("(%p %x)\n", _this
, flags
);
658 /* we have an array, with the number of elements stored before the first object */
659 int i
, *ptr
= (int *)_this
- 1;
661 for (i
= *ptr
- 1; i
>= 0; i
--) MSVCRT_type_info_dtor(_this
+ i
);
662 MSVCRT_operator_delete(ptr
);
666 MSVCRT_type_info_dtor(_this
);
667 if (flags
& 1) MSVCRT_operator_delete(_this
);
676 #define __ASM_VTABLE(name,funcs) \
679 "\t.quad " __ASM_NAME(#name "_rtti") "\n" \
680 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
681 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
682 "\t.quad " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
685 #define __ASM_EXCEPTION_VTABLE(name) \
686 __ASM_VTABLE(name, "\t.quad " THISCALL_NAME(MSVCRT_what_exception) )
690 #define __ASM_VTABLE(name,funcs) \
693 "\t.long " __ASM_NAME(#name "_rtti") "\n" \
694 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
695 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
696 "\t.long " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
699 #define __ASM_EXCEPTION_VTABLE(name) \
700 __ASM_VTABLE(name, "\t.long " THISCALL_NAME(MSVCRT_what_exception) )
705 void __asm_dummy_vtables(void) {
708 __ASM_VTABLE(type_info
,"")
709 __ASM_EXCEPTION_VTABLE(exception
)
710 __ASM_EXCEPTION_VTABLE(bad_typeid
)
711 __ASM_EXCEPTION_VTABLE(bad_cast
)
712 __ASM_EXCEPTION_VTABLE(__non_rtti_object
)
718 /* Static RTTI for exported objects */
720 static const type_info exception_type_info
=
722 &MSVCRT_type_info_vtable
,
727 static const rtti_base_descriptor exception_rtti_base_descriptor
=
729 &exception_type_info
,
735 static const rtti_base_array exception_rtti_base_array
=
738 &exception_rtti_base_descriptor
,
744 static const rtti_object_hierarchy exception_type_hierarchy
=
749 &exception_rtti_base_array
752 const rtti_object_locator exception_rtti
=
757 &exception_type_info
,
758 &exception_type_hierarchy
761 static const cxx_type_info exception_cxx_type_info
=
764 &exception_type_info
,
767 (cxx_copy_ctor
)THISCALL(MSVCRT_exception_copy_ctor
)
770 static const type_info bad_typeid_type_info
=
772 &MSVCRT_type_info_vtable
,
777 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor
=
779 &bad_typeid_type_info
,
785 static const rtti_base_array bad_typeid_rtti_base_array
=
788 &bad_typeid_rtti_base_descriptor
,
789 &exception_rtti_base_descriptor
,
794 static const rtti_object_hierarchy bad_typeid_type_hierarchy
=
799 &bad_typeid_rtti_base_array
802 const rtti_object_locator bad_typeid_rtti
=
807 &bad_typeid_type_info
,
808 &bad_typeid_type_hierarchy
811 static const cxx_type_info bad_typeid_cxx_type_info
=
814 &bad_typeid_type_info
,
817 (cxx_copy_ctor
)THISCALL(MSVCRT_bad_typeid_copy_ctor
)
820 static const type_info bad_cast_type_info
=
822 &MSVCRT_type_info_vtable
,
827 static const rtti_base_descriptor bad_cast_rtti_base_descriptor
=
835 static const rtti_base_array bad_cast_rtti_base_array
=
838 &bad_cast_rtti_base_descriptor
,
839 &exception_rtti_base_descriptor
,
844 static const rtti_object_hierarchy bad_cast_type_hierarchy
=
849 &bad_cast_rtti_base_array
852 const rtti_object_locator bad_cast_rtti
=
858 &bad_cast_type_hierarchy
861 static const cxx_type_info bad_cast_cxx_type_info
=
867 (cxx_copy_ctor
)THISCALL(MSVCRT_bad_cast_copy_ctor
)
870 static const type_info __non_rtti_object_type_info
=
872 &MSVCRT_type_info_vtable
,
874 ".?AV__non_rtti_object@@"
877 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor
=
879 &__non_rtti_object_type_info
,
885 static const rtti_base_array __non_rtti_object_rtti_base_array
=
888 &__non_rtti_object_rtti_base_descriptor
,
889 &bad_typeid_rtti_base_descriptor
,
890 &exception_rtti_base_descriptor
894 static const rtti_object_hierarchy __non_rtti_object_type_hierarchy
=
899 &__non_rtti_object_rtti_base_array
902 const rtti_object_locator __non_rtti_object_rtti
=
907 &__non_rtti_object_type_info
,
908 &__non_rtti_object_type_hierarchy
911 static const cxx_type_info __non_rtti_object_cxx_type_info
=
914 &__non_rtti_object_type_info
,
917 (cxx_copy_ctor
)THISCALL(MSVCRT___non_rtti_object_copy_ctor
)
920 static const type_info type_info_type_info
=
922 &MSVCRT_type_info_vtable
,
927 static const rtti_base_descriptor type_info_rtti_base_descriptor
=
929 &type_info_type_info
,
935 static const rtti_base_array type_info_rtti_base_array
=
938 &type_info_rtti_base_descriptor
,
944 static const rtti_object_hierarchy type_info_type_hierarchy
=
949 &type_info_rtti_base_array
952 const rtti_object_locator type_info_rtti
=
957 &type_info_type_info
,
958 &type_info_type_hierarchy
962 * Exception RTTI for cpp objects
964 static const cxx_type_info_table bad_cast_type_info_table
=
968 &__non_rtti_object_cxx_type_info
,
969 &bad_typeid_cxx_type_info
,
970 &exception_cxx_type_info
974 static const cxx_exception_type bad_cast_exception_type
=
977 (void*)THISCALL(MSVCRT_bad_cast_dtor
),
979 &bad_cast_type_info_table
982 static const cxx_type_info_table bad_typeid_type_info_table
=
986 &bad_cast_cxx_type_info
,
987 &exception_cxx_type_info
,
992 static const cxx_exception_type bad_typeid_exception_type
=
995 (void*)THISCALL(MSVCRT_bad_typeid_dtor
),
997 &bad_cast_type_info_table
1000 static const cxx_exception_type __non_rtti_object_exception_type
=
1003 (void*)THISCALL(MSVCRT___non_rtti_object_dtor
),
1005 &bad_typeid_type_info_table
1009 /******************************************************************
1010 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
1012 * Install a handler to be called when terminate() is called.
1015 * func [I] Handler function to install
1018 * The previously installed handler function, if any.
1020 terminate_function CDECL
MSVCRT_set_terminate(terminate_function func
)
1022 MSVCRT_thread_data
*data
= msvcrt_get_thread_data();
1023 terminate_function previous
= data
->terminate_handler
;
1024 TRACE("(%p) returning %p\n",func
,previous
);
1025 data
->terminate_handler
= func
;
1029 /******************************************************************
1030 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
1032 * Install a handler to be called when unexpected() is called.
1035 * func [I] Handler function to install
1038 * The previously installed handler function, if any.
1040 unexpected_function CDECL
MSVCRT_set_unexpected(unexpected_function func
)
1042 MSVCRT_thread_data
*data
= msvcrt_get_thread_data();
1043 unexpected_function previous
= data
->unexpected_handler
;
1044 TRACE("(%p) returning %p\n",func
,previous
);
1045 data
->unexpected_handler
= func
;
1049 /******************************************************************
1050 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
1052 _se_translator_function CDECL
MSVCRT__set_se_translator(_se_translator_function func
)
1054 MSVCRT_thread_data
*data
= msvcrt_get_thread_data();
1055 _se_translator_function previous
= data
->se_translator
;
1056 TRACE("(%p) returning %p\n",func
,previous
);
1057 data
->se_translator
= func
;
1061 /******************************************************************
1062 * ?terminate@@YAXXZ (MSVCRT.@)
1064 * Default handler for an unhandled exception.
1070 * This function does not return. Either control resumes from any
1071 * handler installed by calling set_terminate(), or (by default) abort()
1074 void CDECL
MSVCRT_terminate(void)
1076 MSVCRT_thread_data
*data
= msvcrt_get_thread_data();
1077 if (data
->terminate_handler
) data
->terminate_handler();
1081 /******************************************************************
1082 * ?unexpected@@YAXXZ (MSVCRT.@)
1084 void CDECL
MSVCRT_unexpected(void)
1086 MSVCRT_thread_data
*data
= msvcrt_get_thread_data();
1087 if (data
->unexpected_handler
) data
->unexpected_handler();
1092 /******************************************************************
1093 * __RTtypeid (MSVCRT.@)
1095 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1098 * cppobj [I] C++ object to get type information for.
1101 * Success: A type_info object describing cppobj.
1102 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
1103 * exception is thrown. If cppobj is NULL, a bad_typeid exception
1104 * is thrown. In either case, this function does not return.
1107 * This function is usually called by compiler generated code as a result
1108 * of using one of the C++ dynamic cast statements.
1110 const type_info
* CDECL
MSVCRT___RTtypeid(void *cppobj
)
1112 const type_info
*ret
;
1117 MSVCRT_bad_typeid_ctor( &e
, "Attempted a typeid of NULL pointer!" );
1118 _CxxThrowException( &e
, &bad_typeid_exception_type
);
1124 const rtti_object_locator
*obj_locator
= get_obj_locator( cppobj
);
1125 ret
= obj_locator
->type_descriptor
;
1129 __non_rtti_object e
;
1130 MSVCRT___non_rtti_object_ctor( &e
, "Bad read pointer - no RTTI data!" );
1131 _CxxThrowException( &e
, &bad_typeid_exception_type
);
1138 /******************************************************************
1139 * __RTDynamicCast (MSVCRT.@)
1141 * Dynamically cast a C++ object to one of its base classes.
1144 * cppobj [I] Any C++ object to cast
1145 * unknown [I] Reserved, set to 0
1146 * src [I] type_info object describing cppobj
1147 * dst [I] type_info object describing the base class to cast to
1148 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
1151 * Success: The address of cppobj, cast to the object described by dst.
1152 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
1153 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
1154 * is thrown and this function does not return.
1157 * This function is usually called by compiler generated code as a result
1158 * of using one of the C++ dynamic cast statements.
1160 void* CDECL
MSVCRT___RTDynamicCast(void *cppobj
, int unknown
,
1161 type_info
*src
, type_info
*dst
,
1166 if (!cppobj
) return NULL
;
1168 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
1169 cppobj
, unknown
, src
, dbgstr_type_info(src
), dst
, dbgstr_type_info(dst
), do_throw
);
1171 /* To cast an object at runtime:
1172 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
1173 * 2.Search for the destination type in the class hierarchy
1174 * 3.If destination type is found, return base object address + dest offset
1175 * Otherwise, fail the cast
1177 * FIXME: the unknown parameter doesn't seem to be used for anything
1182 const rtti_object_locator
*obj_locator
= get_obj_locator( cppobj
);
1183 const rtti_object_hierarchy
*obj_bases
= obj_locator
->type_hierarchy
;
1184 const rtti_base_descriptor
* const* base_desc
= obj_bases
->base_classes
->bases
;
1186 if (TRACE_ON(msvcrt
)) dump_obj_locator(obj_locator
);
1189 for (i
= 0; i
< obj_bases
->array_len
; i
++)
1191 const type_info
*typ
= base_desc
[i
]->type_descriptor
;
1193 if (!strcmp(typ
->mangled
, dst
->mangled
))
1195 /* compute the correct this pointer for that base class */
1196 void *this_ptr
= (char *)cppobj
- obj_locator
->base_class_offset
;
1197 ret
= get_this_pointer( &base_desc
[i
]->offsets
, this_ptr
);
1201 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
1202 * to a reference, since references cannot be NULL.
1204 if (!ret
&& do_throw
)
1206 const char *msg
= "Bad dynamic_cast!";
1208 MSVCRT_bad_cast_ctor( &e
, &msg
);
1209 _CxxThrowException( &e
, &bad_cast_exception_type
);
1214 __non_rtti_object e
;
1215 MSVCRT___non_rtti_object_ctor( &e
, "Access violation - no RTTI data!" );
1216 _CxxThrowException( &e
, &bad_typeid_exception_type
);
1224 /******************************************************************
1225 * __RTCastToVoid (MSVCRT.@)
1227 * Dynamically cast a C++ object to a void*.
1230 * cppobj [I] The C++ object to cast
1233 * Success: The base address of the object as a void*.
1234 * Failure: NULL, if cppobj is NULL or has no RTTI.
1237 * This function is usually called by compiler generated code as a result
1238 * of using one of the C++ dynamic cast statements.
1240 void* CDECL
MSVCRT___RTCastToVoid(void *cppobj
)
1244 if (!cppobj
) return NULL
;
1248 const rtti_object_locator
*obj_locator
= get_obj_locator( cppobj
);
1249 ret
= (char *)cppobj
- obj_locator
->base_class_offset
;
1253 __non_rtti_object e
;
1254 MSVCRT___non_rtti_object_ctor( &e
, "Access violation - no RTTI data!" );
1255 _CxxThrowException( &e
, &bad_typeid_exception_type
);