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 #include <internal/wine_msc.h>
70 #define DEFINE_THISCALL_WRAPPER(func,args) \
71 extern void THISCALL(func)(void); \
72 __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
76 "jmp " __ASM_NAME(#func) __ASM_STDCALL(args) )
81 #define THISCALL(func) func
82 #define THISCALL_NAME(func) __ASM_NAME(#func)
83 #define DEFINE_THISCALL_WRAPPER(func,args) /* nothing */
87 extern const vtable_ptr MSVCRT_exception_vtable
;
88 extern const vtable_ptr MSVCRT_bad_typeid_vtable
;
89 extern const vtable_ptr MSVCRT_bad_cast_vtable
;
90 extern const vtable_ptr MSVCRT___non_rtti_object_vtable
;
91 extern const vtable_ptr MSVCRT_type_info_vtable
;
93 /* get the vtable pointer for a C++ object */
94 static inline const vtable_ptr
*get_vtable( void *obj
)
96 return *(const vtable_ptr
**)obj
;
99 static inline const rtti_object_locator
*get_obj_locator( void *cppobj
)
101 const vtable_ptr
*vtable
= get_vtable( cppobj
);
102 return (const rtti_object_locator
*)vtable
[-1];
105 static void dump_obj_locator( const rtti_object_locator
*ptr
)
108 const rtti_object_hierarchy
*h
= ptr
->type_hierarchy
;
110 TRACE( "%p: sig=%08x base_offset=%08x flags=%08x type=%p %s hierarchy=%p\n",
111 ptr
, ptr
->signature
, ptr
->base_class_offset
, ptr
->flags
,
112 ptr
->type_descriptor
, dbgstr_type_info(ptr
->type_descriptor
), ptr
->type_hierarchy
);
113 TRACE( " hierarchy: sig=%08x attr=%08x len=%d base classes=%p\n",
114 h
->signature
, h
->attributes
, h
->array_len
, h
->base_classes
);
115 for (i
= 0; i
< h
->array_len
; i
++)
117 TRACE( " base class %p: num %d off %d,%d,%d attr %08x type %p %s\n",
118 h
->base_classes
->bases
[i
],
119 h
->base_classes
->bases
[i
]->num_base_classes
,
120 h
->base_classes
->bases
[i
]->offsets
.this_offset
,
121 h
->base_classes
->bases
[i
]->offsets
.vbase_descr
,
122 h
->base_classes
->bases
[i
]->offsets
.vbase_offset
,
123 h
->base_classes
->bases
[i
]->attributes
,
124 h
->base_classes
->bases
[i
]->type_descriptor
,
125 dbgstr_type_info(h
->base_classes
->bases
[i
]->type_descriptor
) );
129 /* Internal common ctor for exception */
130 static void EXCEPTION_ctor(exception
*_this
, const char** name
)
132 _this
->vtable
= &MSVCRT_exception_vtable
;
135 size_t name_len
= strlen(*name
) + 1;
136 _this
->name
= MSVCRT_malloc(name_len
);
137 memcpy(_this
->name
, *name
, name_len
);
138 _this
->do_free
= TRUE
;
143 _this
->do_free
= FALSE
;
147 /******************************************************************
148 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
150 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor
,8)
151 exception
* __stdcall
MSVCRT_exception_ctor(exception
* _this
, const char ** name
)
153 TRACE("(%p,%s)\n", _this
, *name
);
154 EXCEPTION_ctor(_this
, name
);
158 /******************************************************************
159 * ??0exception@@QAE@ABQBDH@Z (MSVCRT.@)
161 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor_noalloc
,12)
162 exception
* __stdcall
MSVCRT_exception_ctor_noalloc(exception
* _this
, char ** name
, int noalloc
)
164 TRACE("(%p,%s)\n", _this
, *name
);
165 _this
->vtable
= &MSVCRT_exception_vtable
;
167 _this
->do_free
= FALSE
;
171 /******************************************************************
172 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
174 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor
,8)
175 exception
* __stdcall
MSVCRT_exception_copy_ctor(exception
* _this
, const exception
* rhs
)
177 TRACE("(%p,%p)\n", _this
, rhs
);
181 _this
->vtable
= &MSVCRT_exception_vtable
;
182 _this
->name
= rhs
->name
;
183 _this
->do_free
= FALSE
;
186 EXCEPTION_ctor(_this
, (const char**)&rhs
->name
);
187 TRACE("name = %s\n", _this
->name
);
191 /******************************************************************
192 * ??0exception@@QAE@XZ (MSVCRT.@)
194 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor
,4)
195 exception
* __stdcall
MSVCRT_exception_default_ctor(exception
* _this
)
197 static const char* empty
= NULL
;
199 TRACE("(%p)\n", _this
);
200 EXCEPTION_ctor(_this
, &empty
);
204 /******************************************************************
205 * ??1exception@@UAE@XZ (MSVCRT.@)
207 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor
,4)
208 void __stdcall
MSVCRT_exception_dtor(exception
* _this
)
210 TRACE("(%p)\n", _this
);
211 _this
->vtable
= &MSVCRT_exception_vtable
;
212 if (_this
->do_free
) MSVCRT_free(_this
->name
);
215 /******************************************************************
216 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
218 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals
,8)
219 exception
* __stdcall
MSVCRT_exception_opequals(exception
* _this
, const exception
* rhs
)
221 TRACE("(%p %p)\n", _this
, rhs
);
224 MSVCRT_exception_dtor(_this
);
225 MSVCRT_exception_copy_ctor(_this
, rhs
);
227 TRACE("name = %s\n", _this
->name
);
231 /******************************************************************
232 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
234 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor
,8)
235 void * __stdcall
MSVCRT_exception_vector_dtor(exception
* _this
, unsigned int flags
)
237 TRACE("(%p %x)\n", _this
, flags
);
240 /* we have an array, with the number of elements stored before the first object */
241 int i
, *ptr
= (int *)_this
- 1;
243 for (i
= *ptr
- 1; i
>= 0; i
--) MSVCRT_exception_dtor(_this
+ i
);
244 MSVCRT_operator_delete(ptr
);
248 MSVCRT_exception_dtor(_this
);
249 if (flags
& 1) MSVCRT_operator_delete(_this
);
254 /******************************************************************
255 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
257 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor
,8)
258 void * __stdcall
MSVCRT_exception_scalar_dtor(exception
* _this
, unsigned int flags
)
260 TRACE("(%p %x)\n", _this
, flags
);
261 MSVCRT_exception_dtor(_this
);
262 if (flags
& 1) MSVCRT_operator_delete(_this
);
266 /******************************************************************
267 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
269 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception
,4)
270 const char * __stdcall
MSVCRT_what_exception(exception
* _this
)
272 TRACE("(%p) returning %s\n", _this
, _this
->name
);
273 return _this
->name
? _this
->name
: "Unknown exception";
276 /******************************************************************
277 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
279 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor
,8)
280 bad_typeid
* __stdcall
MSVCRT_bad_typeid_copy_ctor(bad_typeid
* _this
, const bad_typeid
* rhs
)
282 TRACE("(%p %p)\n", _this
, rhs
);
283 MSVCRT_exception_copy_ctor(_this
, rhs
);
284 _this
->vtable
= &MSVCRT_bad_typeid_vtable
;
288 /******************************************************************
289 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
291 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor
,8)
292 bad_typeid
* __stdcall
MSVCRT_bad_typeid_ctor(bad_typeid
* _this
, const char * name
)
294 TRACE("(%p %s)\n", _this
, name
);
295 EXCEPTION_ctor(_this
, &name
);
296 _this
->vtable
= &MSVCRT_bad_typeid_vtable
;
300 /******************************************************************
301 * ??_Fbad_typeid@@QAEXXZ (MSVCRT.@)
303 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_default_ctor
,4)
304 bad_typeid
* __stdcall
MSVCRT_bad_typeid_default_ctor(bad_typeid
* _this
)
306 return MSVCRT_bad_typeid_ctor( _this
, "bad typeid" );
309 /******************************************************************
310 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
312 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor
,4)
313 void __stdcall
MSVCRT_bad_typeid_dtor(bad_typeid
* _this
)
315 TRACE("(%p)\n", _this
);
316 MSVCRT_exception_dtor(_this
);
319 /******************************************************************
320 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
322 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals
,8)
323 bad_typeid
* __stdcall
MSVCRT_bad_typeid_opequals(bad_typeid
* _this
, const bad_typeid
* rhs
)
325 TRACE("(%p %p)\n", _this
, rhs
);
326 MSVCRT_exception_opequals(_this
, rhs
);
330 /******************************************************************
331 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
333 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor
,8)
334 void * __stdcall
MSVCRT_bad_typeid_vector_dtor(bad_typeid
* _this
, unsigned int flags
)
336 TRACE("(%p %x)\n", _this
, flags
);
339 /* we have an array, with the number of elements stored before the first object */
340 int i
, *ptr
= (int *)_this
- 1;
342 for (i
= *ptr
- 1; i
>= 0; i
--) MSVCRT_bad_typeid_dtor(_this
+ i
);
343 MSVCRT_operator_delete(ptr
);
347 MSVCRT_bad_typeid_dtor(_this
);
348 if (flags
& 1) MSVCRT_operator_delete(_this
);
353 /******************************************************************
354 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
356 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor
,8)
357 void * __stdcall
MSVCRT_bad_typeid_scalar_dtor(bad_typeid
* _this
, unsigned int flags
)
359 TRACE("(%p %x)\n", _this
, flags
);
360 MSVCRT_bad_typeid_dtor(_this
);
361 if (flags
& 1) MSVCRT_operator_delete(_this
);
365 /******************************************************************
366 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
368 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor
,8)
369 __non_rtti_object
* __stdcall
MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object
* _this
,
370 const __non_rtti_object
* rhs
)
372 TRACE("(%p %p)\n", _this
, rhs
);
373 MSVCRT_bad_typeid_copy_ctor(_this
, rhs
);
374 _this
->vtable
= &MSVCRT___non_rtti_object_vtable
;
378 /******************************************************************
379 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
381 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor
,8)
382 __non_rtti_object
* __stdcall
MSVCRT___non_rtti_object_ctor(__non_rtti_object
* _this
,
385 TRACE("(%p %s)\n", _this
, name
);
386 EXCEPTION_ctor(_this
, &name
);
387 _this
->vtable
= &MSVCRT___non_rtti_object_vtable
;
391 /******************************************************************
392 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
394 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor
,4)
395 void __stdcall
MSVCRT___non_rtti_object_dtor(__non_rtti_object
* _this
)
397 TRACE("(%p)\n", _this
);
398 MSVCRT_bad_typeid_dtor(_this
);
401 /******************************************************************
402 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
404 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals
,8)
405 __non_rtti_object
* __stdcall
MSVCRT___non_rtti_object_opequals(__non_rtti_object
* _this
,
406 const __non_rtti_object
*rhs
)
408 TRACE("(%p %p)\n", _this
, rhs
);
409 MSVCRT_bad_typeid_opequals(_this
, rhs
);
413 /******************************************************************
414 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
416 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor
,8)
417 void * __stdcall
MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object
* _this
, unsigned int flags
)
419 TRACE("(%p %x)\n", _this
, flags
);
422 /* we have an array, with the number of elements stored before the first object */
423 int i
, *ptr
= (int *)_this
- 1;
425 for (i
= *ptr
- 1; i
>= 0; i
--) MSVCRT___non_rtti_object_dtor(_this
+ i
);
426 MSVCRT_operator_delete(ptr
);
430 MSVCRT___non_rtti_object_dtor(_this
);
431 if (flags
& 1) MSVCRT_operator_delete(_this
);
436 /******************************************************************
437 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
439 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor
,8)
440 void * __stdcall
MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object
* _this
, unsigned int flags
)
442 TRACE("(%p %x)\n", _this
, flags
);
443 MSVCRT___non_rtti_object_dtor(_this
);
444 if (flags
& 1) MSVCRT_operator_delete(_this
);
448 /******************************************************************
449 * ??0bad_cast@@AAE@PBQBD@Z (MSVCRT.@)
450 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
452 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor
,8)
453 bad_cast
* __stdcall
MSVCRT_bad_cast_ctor(bad_cast
* _this
, const char ** name
)
455 TRACE("(%p %s)\n", _this
, *name
);
456 EXCEPTION_ctor(_this
, name
);
457 _this
->vtable
= &MSVCRT_bad_cast_vtable
;
461 /******************************************************************
462 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
464 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor
,8)
465 bad_cast
* __stdcall
MSVCRT_bad_cast_copy_ctor(bad_cast
* _this
, const bad_cast
* rhs
)
467 TRACE("(%p %p)\n", _this
, rhs
);
468 MSVCRT_exception_copy_ctor(_this
, rhs
);
469 _this
->vtable
= &MSVCRT_bad_cast_vtable
;
473 /******************************************************************
474 * ??0bad_cast@@QAE@PBD@Z (MSVCRT.@)
476 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor_charptr
,8)
477 bad_cast
* __stdcall
MSVCRT_bad_cast_ctor_charptr(bad_cast
* _this
, const char * name
)
479 TRACE("(%p %s)\n", _this
, name
);
480 EXCEPTION_ctor(_this
, &name
);
481 _this
->vtable
= &MSVCRT_bad_cast_vtable
;
485 /******************************************************************
486 * ??_Fbad_cast@@QAEXXZ (MSVCRT.@)
488 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_default_ctor
,4)
489 bad_cast
* __stdcall
MSVCRT_bad_cast_default_ctor(bad_cast
* _this
)
491 return MSVCRT_bad_cast_ctor_charptr( _this
, "bad cast" );
494 /******************************************************************
495 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
497 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor
,4)
498 void __stdcall
MSVCRT_bad_cast_dtor(bad_cast
* _this
)
500 TRACE("(%p)\n", _this
);
501 MSVCRT_exception_dtor(_this
);
504 /******************************************************************
505 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
507 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals
,8)
508 bad_cast
* __stdcall
MSVCRT_bad_cast_opequals(bad_cast
* _this
, const bad_cast
* rhs
)
510 TRACE("(%p %p)\n", _this
, rhs
);
511 MSVCRT_exception_opequals(_this
, rhs
);
515 /******************************************************************
516 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
518 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor
,8)
519 void * __stdcall
MSVCRT_bad_cast_vector_dtor(bad_cast
* _this
, unsigned int flags
)
521 TRACE("(%p %x)\n", _this
, flags
);
524 /* we have an array, with the number of elements stored before the first object */
525 int i
, *ptr
= (int *)_this
- 1;
527 for (i
= *ptr
- 1; i
>= 0; i
--) MSVCRT_bad_cast_dtor(_this
+ i
);
528 MSVCRT_operator_delete(ptr
);
532 MSVCRT_bad_cast_dtor(_this
);
533 if (flags
& 1) MSVCRT_operator_delete(_this
);
538 /******************************************************************
539 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
541 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor
,8)
542 void * __stdcall
MSVCRT_bad_cast_scalar_dtor(bad_cast
* _this
, unsigned int flags
)
544 TRACE("(%p %x)\n", _this
, flags
);
545 MSVCRT_bad_cast_dtor(_this
);
546 if (flags
& 1) MSVCRT_operator_delete(_this
);
550 /******************************************************************
551 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
553 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals
,8)
554 int __stdcall
MSVCRT_type_info_opequals_equals(type_info
* _this
, const type_info
* rhs
)
556 int ret
= !strcmp(_this
->mangled
+ 1, rhs
->mangled
+ 1);
557 TRACE("(%p %p) returning %d\n", _this
, rhs
, ret
);
561 /******************************************************************
562 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
564 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals
,8)
565 int __stdcall
MSVCRT_type_info_opnot_equals(type_info
* _this
, const type_info
* rhs
)
567 int ret
= !!strcmp(_this
->mangled
+ 1, rhs
->mangled
+ 1);
568 TRACE("(%p %p) returning %d\n", _this
, rhs
, ret
);
572 /******************************************************************
573 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
575 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before
,8)
576 int __stdcall
MSVCRT_type_info_before(type_info
* _this
, const type_info
* rhs
)
578 int ret
= strcmp(_this
->mangled
+ 1, rhs
->mangled
+ 1) < 0;
579 TRACE("(%p %p) returning %d\n", _this
, rhs
, ret
);
583 /******************************************************************
584 * ??1type_info@@UAE@XZ (MSVCRT.@)
586 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor
,4)
587 void __stdcall
MSVCRT_type_info_dtor(type_info
* _this
)
589 TRACE("(%p)\n", _this
);
590 MSVCRT_free(_this
->name
);
593 /******************************************************************
594 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
596 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name
,4)
597 const char * __stdcall
MSVCRT_type_info_name(type_info
* _this
)
601 /* Create and set the demangled name */
602 /* Note: mangled name in type_info struct always starts with a '.', while
603 * it isn't valid for mangled name.
604 * Is this '.' really part of the mangled name, or has it some other meaning ?
606 char* name
= __unDName(0, _this
->mangled
+ 1, 0,
607 MSVCRT_malloc
, MSVCRT_free
, UNDNAME_NO_ARGUMENTS
| UNDNAME_32_BIT_DECODE
);
610 size_t len
= strlen(name
);
612 /* It seems _unDName may leave blanks at the end of the demangled name */
613 while (len
&& name
[--len
] == ' ')
616 if (InterlockedCompareExchangePointer((void**)&_this
->name
, name
, NULL
))
618 /* Another thread set this member since we checked above - use it */
623 TRACE("(%p) returning %s\n", _this
, _this
->name
);
627 /******************************************************************
628 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
630 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name
,4)
631 const char * __stdcall
MSVCRT_type_info_raw_name(type_info
* _this
)
633 TRACE("(%p) returning %s\n", _this
, _this
->mangled
);
634 return _this
->mangled
;
638 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor
,8)
639 void * __stdcall
MSVCRT_type_info_vector_dtor(type_info
* _this
, unsigned int flags
)
641 TRACE("(%p %x)\n", _this
, flags
);
644 /* we have an array, with the number of elements stored before the first object */
645 int i
, *ptr
= (int *)_this
- 1;
647 for (i
= *ptr
- 1; i
>= 0; i
--) MSVCRT_type_info_dtor(_this
+ i
);
648 MSVCRT_operator_delete(ptr
);
652 MSVCRT_type_info_dtor(_this
);
653 if (flags
& 1) MSVCRT_operator_delete(_this
);
663 #define __ASM_VTABLE(name,funcs) \
666 "\t.quad " __ASM_NAME(#name "_rtti") "\n" \
667 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
668 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
669 "\t.quad " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
672 #define __ASM_EXCEPTION_VTABLE(name) \
673 __ASM_VTABLE(name, "\t.quad " THISCALL_NAME(MSVCRT_what_exception) )
677 #define __ASM_VTABLE(name,funcs) \
680 "\t.long " __ASM_NAME(#name "_rtti") "\n" \
681 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
682 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
683 "\t.long " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
686 #define __ASM_EXCEPTION_VTABLE(name) \
687 __ASM_VTABLE(name, "\t.long " THISCALL_NAME(MSVCRT_what_exception) )
692 void __asm_dummy_vtables(void) {
695 __ASM_VTABLE(type_info
,"")
696 __ASM_EXCEPTION_VTABLE(exception
)
697 __ASM_EXCEPTION_VTABLE(bad_typeid
)
698 __ASM_EXCEPTION_VTABLE(bad_cast
)
699 __ASM_EXCEPTION_VTABLE(__non_rtti_object
)
706 /* Static RTTI for exported objects */
708 static const type_info exception_type_info
=
710 &MSVCRT_type_info_vtable
,
715 static const rtti_base_descriptor exception_rtti_base_descriptor
=
717 &exception_type_info
,
723 static const rtti_base_array exception_rtti_base_array
=
726 &exception_rtti_base_descriptor
,
732 static const rtti_object_hierarchy exception_type_hierarchy
=
737 &exception_rtti_base_array
740 const rtti_object_locator exception_rtti
=
745 &exception_type_info
,
746 &exception_type_hierarchy
749 static const cxx_type_info exception_cxx_type_info
=
752 &exception_type_info
,
755 (cxx_copy_ctor
)THISCALL(MSVCRT_exception_copy_ctor
)
758 static const type_info bad_typeid_type_info
=
760 &MSVCRT_type_info_vtable
,
765 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor
=
767 &bad_typeid_type_info
,
773 static const rtti_base_array bad_typeid_rtti_base_array
=
776 &bad_typeid_rtti_base_descriptor
,
777 &exception_rtti_base_descriptor
,
782 static const rtti_object_hierarchy bad_typeid_type_hierarchy
=
787 &bad_typeid_rtti_base_array
790 const rtti_object_locator bad_typeid_rtti
=
795 &bad_typeid_type_info
,
796 &bad_typeid_type_hierarchy
799 static const cxx_type_info bad_typeid_cxx_type_info
=
802 &bad_typeid_type_info
,
805 (cxx_copy_ctor
)THISCALL(MSVCRT_bad_typeid_copy_ctor
)
808 static const type_info bad_cast_type_info
=
810 &MSVCRT_type_info_vtable
,
815 static const rtti_base_descriptor bad_cast_rtti_base_descriptor
=
823 static const rtti_base_array bad_cast_rtti_base_array
=
826 &bad_cast_rtti_base_descriptor
,
827 &exception_rtti_base_descriptor
,
832 static const rtti_object_hierarchy bad_cast_type_hierarchy
=
837 &bad_cast_rtti_base_array
840 const rtti_object_locator bad_cast_rtti
=
846 &bad_cast_type_hierarchy
849 static const cxx_type_info bad_cast_cxx_type_info
=
855 (cxx_copy_ctor
)THISCALL(MSVCRT_bad_cast_copy_ctor
)
858 static const type_info __non_rtti_object_type_info
=
860 &MSVCRT_type_info_vtable
,
862 ".?AV__non_rtti_object@@"
865 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor
=
867 &__non_rtti_object_type_info
,
873 static const rtti_base_array __non_rtti_object_rtti_base_array
=
876 &__non_rtti_object_rtti_base_descriptor
,
877 &bad_typeid_rtti_base_descriptor
,
878 &exception_rtti_base_descriptor
882 static const rtti_object_hierarchy __non_rtti_object_type_hierarchy
=
887 &__non_rtti_object_rtti_base_array
890 const rtti_object_locator __non_rtti_object_rtti
=
895 &__non_rtti_object_type_info
,
896 &__non_rtti_object_type_hierarchy
899 static const cxx_type_info __non_rtti_object_cxx_type_info
=
902 &__non_rtti_object_type_info
,
905 (cxx_copy_ctor
)THISCALL(MSVCRT___non_rtti_object_copy_ctor
)
908 static const type_info type_info_type_info
=
910 &MSVCRT_type_info_vtable
,
915 static const rtti_base_descriptor type_info_rtti_base_descriptor
=
917 &type_info_type_info
,
923 static const rtti_base_array type_info_rtti_base_array
=
926 &type_info_rtti_base_descriptor
,
932 static const rtti_object_hierarchy type_info_type_hierarchy
=
937 &type_info_rtti_base_array
940 const rtti_object_locator type_info_rtti
=
945 &type_info_type_info
,
946 &type_info_type_hierarchy
950 * Exception RTTI for cpp objects
952 static const cxx_type_info_table bad_cast_type_info_table
=
956 &__non_rtti_object_cxx_type_info
,
957 &bad_typeid_cxx_type_info
,
958 &exception_cxx_type_info
962 static const cxx_exception_type bad_cast_exception_type
=
965 (void*)THISCALL(MSVCRT_bad_cast_dtor
),
967 &bad_cast_type_info_table
970 static const cxx_type_info_table bad_typeid_type_info_table
=
974 &bad_cast_cxx_type_info
,
975 &exception_cxx_type_info
,
980 static const cxx_exception_type bad_typeid_exception_type
=
983 (void*)THISCALL(MSVCRT_bad_typeid_dtor
),
985 &bad_cast_type_info_table
988 static const cxx_exception_type __non_rtti_object_exception_type
=
991 (void*)THISCALL(MSVCRT___non_rtti_object_dtor
),
993 &bad_typeid_type_info_table
997 /******************************************************************
998 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
1000 * Install a handler to be called when terminate() is called.
1003 * func [I] Handler function to install
1006 * The previously installed handler function, if any.
1008 terminate_function CDECL
MSVCRT_set_terminate(terminate_function func
)
1010 thread_data_t
*data
= msvcrt_get_thread_data();
1011 terminate_function previous
= data
->terminate_handler
;
1012 TRACE("(%p) returning %p\n",func
,previous
);
1013 data
->terminate_handler
= func
;
1017 /******************************************************************
1018 * _get_terminate (MSVCRT.@)
1020 terminate_function CDECL
_get_terminate(void)
1022 thread_data_t
*data
= msvcrt_get_thread_data();
1023 return data
->terminate_handler
;
1026 /******************************************************************
1027 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
1029 * Install a handler to be called when unexpected() is called.
1032 * func [I] Handler function to install
1035 * The previously installed handler function, if any.
1037 unexpected_function CDECL
MSVCRT_set_unexpected(unexpected_function func
)
1039 thread_data_t
*data
= msvcrt_get_thread_data();
1040 unexpected_function previous
= data
->unexpected_handler
;
1041 TRACE("(%p) returning %p\n",func
,previous
);
1042 data
->unexpected_handler
= func
;
1046 /******************************************************************
1047 * _get_unexpected (MSVCRT.@)
1049 unexpected_function CDECL
_get_unexpected(void)
1051 thread_data_t
*data
= msvcrt_get_thread_data();
1052 return data
->unexpected_handler
;
1055 /******************************************************************
1056 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
1058 _se_translator_function CDECL
MSVCRT__set_se_translator(_se_translator_function func
)
1060 thread_data_t
*data
= msvcrt_get_thread_data();
1061 _se_translator_function previous
= data
->se_translator
;
1062 TRACE("(%p) returning %p\n",func
,previous
);
1063 data
->se_translator
= func
;
1067 /******************************************************************
1068 * ?terminate@@YAXXZ (MSVCRT.@)
1070 * Default handler for an unhandled exception.
1076 * This function does not return. Either control resumes from any
1077 * handler installed by calling set_terminate(), or (by default) abort()
1080 void CDECL
MSVCRT_terminate(void)
1082 thread_data_t
*data
= msvcrt_get_thread_data();
1083 if (data
->terminate_handler
) data
->terminate_handler();
1087 /******************************************************************
1088 * ?unexpected@@YAXXZ (MSVCRT.@)
1090 void CDECL
MSVCRT_unexpected(void)
1092 thread_data_t
*data
= msvcrt_get_thread_data();
1093 if (data
->unexpected_handler
) data
->unexpected_handler();
1098 /******************************************************************
1099 * __RTtypeid (MSVCRT.@)
1101 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1104 * cppobj [I] C++ object to get type information for.
1107 * Success: A type_info object describing cppobj.
1108 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
1109 * exception is thrown. If cppobj is NULL, a bad_typeid exception
1110 * is thrown. In either case, this function does not return.
1113 * This function is usually called by compiler generated code as a result
1114 * of using one of the C++ dynamic cast statements.
1116 const type_info
* CDECL
MSVCRT___RTtypeid(void *cppobj
)
1118 const type_info
*ret
;
1123 MSVCRT_bad_typeid_ctor( &e
, "Attempted a typeid of NULL pointer!" );
1124 _CxxThrowException( &e
, &bad_typeid_exception_type
);
1130 const rtti_object_locator
*obj_locator
= get_obj_locator( cppobj
);
1131 ret
= obj_locator
->type_descriptor
;
1135 __non_rtti_object e
;
1136 MSVCRT___non_rtti_object_ctor( &e
, "Bad read pointer - no RTTI data!" );
1137 _CxxThrowException( &e
, &bad_typeid_exception_type
);
1144 /******************************************************************
1145 * __RTDynamicCast (MSVCRT.@)
1147 * Dynamically cast a C++ object to one of its base classes.
1150 * cppobj [I] Any C++ object to cast
1151 * unknown [I] Reserved, set to 0
1152 * src [I] type_info object describing cppobj
1153 * dst [I] type_info object describing the base class to cast to
1154 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
1157 * Success: The address of cppobj, cast to the object described by dst.
1158 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
1159 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
1160 * is thrown and this function does not return.
1163 * This function is usually called by compiler generated code as a result
1164 * of using one of the C++ dynamic cast statements.
1166 void* CDECL
MSVCRT___RTDynamicCast(void *cppobj
, int unknown
,
1167 type_info
*src
, type_info
*dst
,
1172 if (!cppobj
) return NULL
;
1174 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
1175 cppobj
, unknown
, src
, dbgstr_type_info(src
), dst
, dbgstr_type_info(dst
), do_throw
);
1177 /* To cast an object at runtime:
1178 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
1179 * 2.Search for the destination type in the class hierarchy
1180 * 3.If destination type is found, return base object address + dest offset
1181 * Otherwise, fail the cast
1183 * FIXME: the unknown parameter doesn't seem to be used for anything
1188 const rtti_object_locator
*obj_locator
= get_obj_locator( cppobj
);
1189 const rtti_object_hierarchy
*obj_bases
= obj_locator
->type_hierarchy
;
1190 const rtti_base_descriptor
* const* base_desc
= obj_bases
->base_classes
->bases
;
1192 if (TRACE_ON(msvcrt
)) dump_obj_locator(obj_locator
);
1195 for (i
= 0; i
< obj_bases
->array_len
; i
++)
1197 const type_info
*typ
= base_desc
[i
]->type_descriptor
;
1199 if (!strcmp(typ
->mangled
, dst
->mangled
))
1201 /* compute the correct this pointer for that base class */
1202 void *this_ptr
= (char *)cppobj
- obj_locator
->base_class_offset
;
1203 ret
= get_this_pointer( &base_desc
[i
]->offsets
, this_ptr
);
1207 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
1208 * to a reference, since references cannot be NULL.
1210 if (!ret
&& do_throw
)
1212 const char *msg
= "Bad dynamic_cast!";
1214 MSVCRT_bad_cast_ctor( &e
, &msg
);
1215 _CxxThrowException( &e
, &bad_cast_exception_type
);
1220 __non_rtti_object e
;
1221 MSVCRT___non_rtti_object_ctor( &e
, "Access violation - no RTTI data!" );
1222 _CxxThrowException( &e
, &bad_typeid_exception_type
);
1230 /******************************************************************
1231 * __RTCastToVoid (MSVCRT.@)
1233 * Dynamically cast a C++ object to a void*.
1236 * cppobj [I] The C++ object to cast
1239 * Success: The base address of the object as a void*.
1240 * Failure: NULL, if cppobj is NULL or has no RTTI.
1243 * This function is usually called by compiler generated code as a result
1244 * of using one of the C++ dynamic cast statements.
1246 void* CDECL
MSVCRT___RTCastToVoid(void *cppobj
)
1250 if (!cppobj
) return NULL
;
1254 const rtti_object_locator
*obj_locator
= get_obj_locator( cppobj
);
1255 ret
= (char *)cppobj
- obj_locator
->base_class_offset
;
1259 __non_rtti_object e
;
1260 MSVCRT___non_rtti_object_ctor( &e
, "Access violation - no RTTI data!" );
1261 _CxxThrowException( &e
, &bad_typeid_exception_type
);