Sync with trunk revision 63128.
[reactos.git] / lib / sdk / crt / except / cpp.c
1 /*
2 * msvcrt.dll C++ objects
3 *
4 * Copyright 2000 Jon Griffiths
5 * Copyright 2003, 2004 Alexandre Julliard
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <precomp.h>
23
24 #include <wine/exception.h>
25 #include <internal/wine/msvcrt.h>
26 #include <internal/wine/cppexcept.h>
27
28 typedef exception bad_cast;
29 typedef exception bad_typeid;
30 typedef exception __non_rtti_object;
31
32 typedef struct _rtti_base_descriptor
33 {
34 const type_info *type_descriptor;
35 int num_base_classes;
36 this_ptr_offsets offsets; /* offsets for computing the this pointer */
37 unsigned int attributes;
38 } rtti_base_descriptor;
39
40 typedef struct _rtti_base_array
41 {
42 const rtti_base_descriptor *bases[3]; /* First element is the class itself */
43 } rtti_base_array;
44
45 typedef struct _rtti_object_hierarchy
46 {
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;
52
53 typedef struct _rtti_object_locator
54 {
55 unsigned int signature;
56 int base_class_offset;
57 unsigned int flags;
58 const type_info *type_descriptor;
59 const rtti_object_hierarchy *type_hierarchy;
60 } rtti_object_locator;
61
62 #ifdef __i386__ /* thiscall functions are i386-specific */
63
64 #define THISCALL(func) __thiscall_ ## func
65 #define THISCALL_NAME(func) __ASM_NAME("__thiscall_" #func)
66
67 #ifdef _MSC_VER
68 #include <internal/wine_msc.h>
69 #else
70 #define DEFINE_THISCALL_WRAPPER(func,args) \
71 extern void THISCALL(func)(void); \
72 __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
73 "popl %eax\n\t" \
74 "pushl %ecx\n\t" \
75 "pushl %eax\n\t" \
76 "jmp " __ASM_NAME(#func) __ASM_STDCALL(args) )
77 #endif /* _MSC_VER */
78
79 #else /* __i386__ */
80
81 #define THISCALL(func) func
82 #define THISCALL_NAME(func) __ASM_NAME(#func)
83 #define DEFINE_THISCALL_WRAPPER(func,args) /* nothing */
84
85 #endif /* __i386__ */
86
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;
92
93 /* get the vtable pointer for a C++ object */
94 static inline const vtable_ptr *get_vtable( void *obj )
95 {
96 return *(const vtable_ptr **)obj;
97 }
98
99 static inline const rtti_object_locator *get_obj_locator( void *cppobj )
100 {
101 const vtable_ptr *vtable = get_vtable( cppobj );
102 return (const rtti_object_locator *)vtable[-1];
103 }
104
105 static void dump_obj_locator( const rtti_object_locator *ptr )
106 {
107 int i;
108 const rtti_object_hierarchy *h = ptr->type_hierarchy;
109
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++)
116 {
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) );
126 }
127 }
128
129 /* Internal common ctor for exception */
130 static void EXCEPTION_ctor(exception *_this, const char** name)
131 {
132 _this->vtable = &MSVCRT_exception_vtable;
133 if (*name)
134 {
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;
139 }
140 else
141 {
142 _this->name = NULL;
143 _this->do_free = FALSE;
144 }
145 }
146
147 /******************************************************************
148 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
149 */
150 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor,8)
151 exception * __stdcall MSVCRT_exception_ctor(exception * _this, const char ** name)
152 {
153 TRACE("(%p,%s)\n", _this, *name);
154 EXCEPTION_ctor(_this, name);
155 return _this;
156 }
157
158 /******************************************************************
159 * ??0exception@@QAE@ABQBDH@Z (MSVCRT.@)
160 */
161 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor_noalloc,12)
162 exception * __stdcall MSVCRT_exception_ctor_noalloc(exception * _this, char ** name, int noalloc)
163 {
164 TRACE("(%p,%s)\n", _this, *name);
165 _this->vtable = &MSVCRT_exception_vtable;
166 _this->name = *name;
167 _this->do_free = FALSE;
168 return _this;
169 }
170
171 /******************************************************************
172 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
173 */
174 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor,8)
175 exception * __stdcall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
176 {
177 TRACE("(%p,%p)\n", _this, rhs);
178
179 if (!rhs->do_free)
180 {
181 _this->vtable = &MSVCRT_exception_vtable;
182 _this->name = rhs->name;
183 _this->do_free = FALSE;
184 }
185 else
186 EXCEPTION_ctor(_this, (const char**)&rhs->name);
187 TRACE("name = %s\n", _this->name);
188 return _this;
189 }
190
191 /******************************************************************
192 * ??0exception@@QAE@XZ (MSVCRT.@)
193 */
194 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor,4)
195 exception * __stdcall MSVCRT_exception_default_ctor(exception * _this)
196 {
197 static const char* empty = NULL;
198
199 TRACE("(%p)\n", _this);
200 EXCEPTION_ctor(_this, &empty);
201 return _this;
202 }
203
204 /******************************************************************
205 * ??1exception@@UAE@XZ (MSVCRT.@)
206 */
207 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor,4)
208 void __stdcall MSVCRT_exception_dtor(exception * _this)
209 {
210 TRACE("(%p)\n", _this);
211 _this->vtable = &MSVCRT_exception_vtable;
212 if (_this->do_free) MSVCRT_free(_this->name);
213 }
214
215 /******************************************************************
216 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
217 */
218 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals,8)
219 exception * __stdcall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
220 {
221 TRACE("(%p %p)\n", _this, rhs);
222 if (_this != rhs)
223 {
224 MSVCRT_exception_dtor(_this);
225 MSVCRT_exception_copy_ctor(_this, rhs);
226 }
227 TRACE("name = %s\n", _this->name);
228 return _this;
229 }
230
231 /******************************************************************
232 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
233 */
234 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor,8)
235 void * __stdcall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
236 {
237 TRACE("(%p %x)\n", _this, flags);
238 if (flags & 2)
239 {
240 /* we have an array, with the number of elements stored before the first object */
241 int i, *ptr = (int *)_this - 1;
242
243 for (i = *ptr - 1; i >= 0; i--) MSVCRT_exception_dtor(_this + i);
244 MSVCRT_operator_delete(ptr);
245 }
246 else
247 {
248 MSVCRT_exception_dtor(_this);
249 if (flags & 1) MSVCRT_operator_delete(_this);
250 }
251 return _this;
252 }
253
254 /******************************************************************
255 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
256 */
257 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor,8)
258 void * __stdcall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
259 {
260 TRACE("(%p %x)\n", _this, flags);
261 MSVCRT_exception_dtor(_this);
262 if (flags & 1) MSVCRT_operator_delete(_this);
263 return _this;
264 }
265
266 /******************************************************************
267 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
268 */
269 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception,4)
270 const char * __stdcall MSVCRT_what_exception(exception * _this)
271 {
272 TRACE("(%p) returning %s\n", _this, _this->name);
273 return _this->name ? _this->name : "Unknown exception";
274 }
275
276 /******************************************************************
277 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
278 */
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)
281 {
282 TRACE("(%p %p)\n", _this, rhs);
283 MSVCRT_exception_copy_ctor(_this, rhs);
284 _this->vtable = &MSVCRT_bad_typeid_vtable;
285 return _this;
286 }
287
288 /******************************************************************
289 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
290 */
291 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor,8)
292 bad_typeid * __stdcall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
293 {
294 TRACE("(%p %s)\n", _this, name);
295 EXCEPTION_ctor(_this, &name);
296 _this->vtable = &MSVCRT_bad_typeid_vtable;
297 return _this;
298 }
299
300 /******************************************************************
301 * ??_Fbad_typeid@@QAEXXZ (MSVCRT.@)
302 */
303 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_default_ctor,4)
304 bad_typeid * __stdcall MSVCRT_bad_typeid_default_ctor(bad_typeid * _this)
305 {
306 return MSVCRT_bad_typeid_ctor( _this, "bad typeid" );
307 }
308
309 /******************************************************************
310 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
311 */
312 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor,4)
313 void __stdcall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
314 {
315 TRACE("(%p)\n", _this);
316 MSVCRT_exception_dtor(_this);
317 }
318
319 /******************************************************************
320 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
321 */
322 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals,8)
323 bad_typeid * __stdcall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
324 {
325 TRACE("(%p %p)\n", _this, rhs);
326 MSVCRT_exception_opequals(_this, rhs);
327 return _this;
328 }
329
330 /******************************************************************
331 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
332 */
333 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor,8)
334 void * __stdcall MSVCRT_bad_typeid_vector_dtor(bad_typeid * _this, unsigned int flags)
335 {
336 TRACE("(%p %x)\n", _this, flags);
337 if (flags & 2)
338 {
339 /* we have an array, with the number of elements stored before the first object */
340 int i, *ptr = (int *)_this - 1;
341
342 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_typeid_dtor(_this + i);
343 MSVCRT_operator_delete(ptr);
344 }
345 else
346 {
347 MSVCRT_bad_typeid_dtor(_this);
348 if (flags & 1) MSVCRT_operator_delete(_this);
349 }
350 return _this;
351 }
352
353 /******************************************************************
354 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
355 */
356 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor,8)
357 void * __stdcall MSVCRT_bad_typeid_scalar_dtor(bad_typeid * _this, unsigned int flags)
358 {
359 TRACE("(%p %x)\n", _this, flags);
360 MSVCRT_bad_typeid_dtor(_this);
361 if (flags & 1) MSVCRT_operator_delete(_this);
362 return _this;
363 }
364
365 /******************************************************************
366 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
367 */
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)
371 {
372 TRACE("(%p %p)\n", _this, rhs);
373 MSVCRT_bad_typeid_copy_ctor(_this, rhs);
374 _this->vtable = &MSVCRT___non_rtti_object_vtable;
375 return _this;
376 }
377
378 /******************************************************************
379 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
380 */
381 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor,8)
382 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
383 const char * name)
384 {
385 TRACE("(%p %s)\n", _this, name);
386 EXCEPTION_ctor(_this, &name);
387 _this->vtable = &MSVCRT___non_rtti_object_vtable;
388 return _this;
389 }
390
391 /******************************************************************
392 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
393 */
394 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor,4)
395 void __stdcall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
396 {
397 TRACE("(%p)\n", _this);
398 MSVCRT_bad_typeid_dtor(_this);
399 }
400
401 /******************************************************************
402 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
403 */
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)
407 {
408 TRACE("(%p %p)\n", _this, rhs);
409 MSVCRT_bad_typeid_opequals(_this, rhs);
410 return _this;
411 }
412
413 /******************************************************************
414 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
415 */
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)
418 {
419 TRACE("(%p %x)\n", _this, flags);
420 if (flags & 2)
421 {
422 /* we have an array, with the number of elements stored before the first object */
423 int i, *ptr = (int *)_this - 1;
424
425 for (i = *ptr - 1; i >= 0; i--) MSVCRT___non_rtti_object_dtor(_this + i);
426 MSVCRT_operator_delete(ptr);
427 }
428 else
429 {
430 MSVCRT___non_rtti_object_dtor(_this);
431 if (flags & 1) MSVCRT_operator_delete(_this);
432 }
433 return _this;
434 }
435
436 /******************************************************************
437 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
438 */
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)
441 {
442 TRACE("(%p %x)\n", _this, flags);
443 MSVCRT___non_rtti_object_dtor(_this);
444 if (flags & 1) MSVCRT_operator_delete(_this);
445 return _this;
446 }
447
448 /******************************************************************
449 * ??0bad_cast@@AAE@PBQBD@Z (MSVCRT.@)
450 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
451 */
452 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor,8)
453 bad_cast * __stdcall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
454 {
455 TRACE("(%p %s)\n", _this, *name);
456 EXCEPTION_ctor(_this, name);
457 _this->vtable = &MSVCRT_bad_cast_vtable;
458 return _this;
459 }
460
461 /******************************************************************
462 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
463 */
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)
466 {
467 TRACE("(%p %p)\n", _this, rhs);
468 MSVCRT_exception_copy_ctor(_this, rhs);
469 _this->vtable = &MSVCRT_bad_cast_vtable;
470 return _this;
471 }
472
473 /******************************************************************
474 * ??0bad_cast@@QAE@PBD@Z (MSVCRT.@)
475 */
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)
478 {
479 TRACE("(%p %s)\n", _this, name);
480 EXCEPTION_ctor(_this, &name);
481 _this->vtable = &MSVCRT_bad_cast_vtable;
482 return _this;
483 }
484
485 /******************************************************************
486 * ??_Fbad_cast@@QAEXXZ (MSVCRT.@)
487 */
488 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_default_ctor,4)
489 bad_cast * __stdcall MSVCRT_bad_cast_default_ctor(bad_cast * _this)
490 {
491 return MSVCRT_bad_cast_ctor_charptr( _this, "bad cast" );
492 }
493
494 /******************************************************************
495 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
496 */
497 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor,4)
498 void __stdcall MSVCRT_bad_cast_dtor(bad_cast * _this)
499 {
500 TRACE("(%p)\n", _this);
501 MSVCRT_exception_dtor(_this);
502 }
503
504 /******************************************************************
505 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
506 */
507 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals,8)
508 bad_cast * __stdcall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
509 {
510 TRACE("(%p %p)\n", _this, rhs);
511 MSVCRT_exception_opequals(_this, rhs);
512 return _this;
513 }
514
515 /******************************************************************
516 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
517 */
518 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor,8)
519 void * __stdcall MSVCRT_bad_cast_vector_dtor(bad_cast * _this, unsigned int flags)
520 {
521 TRACE("(%p %x)\n", _this, flags);
522 if (flags & 2)
523 {
524 /* we have an array, with the number of elements stored before the first object */
525 int i, *ptr = (int *)_this - 1;
526
527 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_cast_dtor(_this + i);
528 MSVCRT_operator_delete(ptr);
529 }
530 else
531 {
532 MSVCRT_bad_cast_dtor(_this);
533 if (flags & 1) MSVCRT_operator_delete(_this);
534 }
535 return _this;
536 }
537
538 /******************************************************************
539 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
540 */
541 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor,8)
542 void * __stdcall MSVCRT_bad_cast_scalar_dtor(bad_cast * _this, unsigned int flags)
543 {
544 TRACE("(%p %x)\n", _this, flags);
545 MSVCRT_bad_cast_dtor(_this);
546 if (flags & 1) MSVCRT_operator_delete(_this);
547 return _this;
548 }
549
550 /******************************************************************
551 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
552 */
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)
555 {
556 int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
557 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
558 return ret;
559 }
560
561 /******************************************************************
562 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
563 */
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)
566 {
567 int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
568 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
569 return ret;
570 }
571
572 /******************************************************************
573 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
574 */
575 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before,8)
576 int __stdcall MSVCRT_type_info_before(type_info * _this, const type_info * rhs)
577 {
578 int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
579 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
580 return ret;
581 }
582
583 /******************************************************************
584 * ??1type_info@@UAE@XZ (MSVCRT.@)
585 */
586 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor,4)
587 void __stdcall MSVCRT_type_info_dtor(type_info * _this)
588 {
589 TRACE("(%p)\n", _this);
590 MSVCRT_free(_this->name);
591 }
592
593 /******************************************************************
594 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
595 */
596 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name,4)
597 const char * __stdcall MSVCRT_type_info_name(type_info * _this)
598 {
599 if (!_this->name)
600 {
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 ?
605 */
606 char* name = __unDName(0, _this->mangled + 1, 0,
607 MSVCRT_malloc, MSVCRT_free, UNDNAME_NO_ARGUMENTS | UNDNAME_32_BIT_DECODE);
608 if (name)
609 {
610 size_t len = strlen(name);
611
612 /* It seems _unDName may leave blanks at the end of the demangled name */
613 while (len && name[--len] == ' ')
614 name[len] = '\0';
615
616 if (InterlockedCompareExchangePointer((void**)&_this->name, name, NULL))
617 {
618 /* Another thread set this member since we checked above - use it */
619 MSVCRT_free(name);
620 }
621 }
622 }
623 TRACE("(%p) returning %s\n", _this, _this->name);
624 return _this->name;
625 }
626
627 /******************************************************************
628 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
629 */
630 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name,4)
631 const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
632 {
633 TRACE("(%p) returning %s\n", _this, _this->mangled);
634 return _this->mangled;
635 }
636
637 /* Unexported */
638 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor,8)
639 void * __stdcall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
640 {
641 TRACE("(%p %x)\n", _this, flags);
642 if (flags & 2)
643 {
644 /* we have an array, with the number of elements stored before the first object */
645 int i, *ptr = (int *)_this - 1;
646
647 for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
648 MSVCRT_operator_delete(ptr);
649 }
650 else
651 {
652 MSVCRT_type_info_dtor(_this);
653 if (flags & 1) MSVCRT_operator_delete(_this);
654 }
655 return _this;
656 }
657
658 /* vtables */
659
660 #ifdef __GNUC__
661 #ifdef _WIN64
662
663 #define __ASM_VTABLE(name,funcs) \
664 __asm__(".data\n" \
665 "\t.align 8\n" \
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" \
670 funcs "\n\t.text");
671
672 #define __ASM_EXCEPTION_VTABLE(name) \
673 __ASM_VTABLE(name, "\t.quad " THISCALL_NAME(MSVCRT_what_exception) )
674
675 #else
676
677 #define __ASM_VTABLE(name,funcs) \
678 __asm__(".data\n" \
679 "\t.align 4\n" \
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" \
684 funcs "\n\t.text");
685
686 #define __ASM_EXCEPTION_VTABLE(name) \
687 __ASM_VTABLE(name, "\t.long " THISCALL_NAME(MSVCRT_what_exception) )
688
689 #endif /* _WIN64 */
690
691 #ifndef __GNUC__
692 void __asm_dummy_vtables(void) {
693 #endif
694
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)
700
701 #ifndef __GNUC__
702 }
703 #endif
704 #endif
705
706 /* Static RTTI for exported objects */
707
708 static const type_info exception_type_info =
709 {
710 &MSVCRT_type_info_vtable,
711 NULL,
712 ".?AVexception@@"
713 };
714
715 static const rtti_base_descriptor exception_rtti_base_descriptor =
716 {
717 &exception_type_info,
718 0,
719 { 0, -1, 0 },
720 0
721 };
722
723 static const rtti_base_array exception_rtti_base_array =
724 {
725 {
726 &exception_rtti_base_descriptor,
727 NULL,
728 NULL
729 }
730 };
731
732 static const rtti_object_hierarchy exception_type_hierarchy =
733 {
734 0,
735 0,
736 1,
737 &exception_rtti_base_array
738 };
739
740 const rtti_object_locator exception_rtti =
741 {
742 0,
743 0,
744 0,
745 &exception_type_info,
746 &exception_type_hierarchy
747 };
748
749 static const cxx_type_info exception_cxx_type_info =
750 {
751 0,
752 &exception_type_info,
753 { 0, -1, 0 },
754 sizeof(exception),
755 (cxx_copy_ctor)THISCALL(MSVCRT_exception_copy_ctor)
756 };
757
758 static const type_info bad_typeid_type_info =
759 {
760 &MSVCRT_type_info_vtable,
761 NULL,
762 ".?AVbad_typeid@@"
763 };
764
765 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
766 {
767 &bad_typeid_type_info,
768 1,
769 { 0, -1, 0 },
770 0
771 };
772
773 static const rtti_base_array bad_typeid_rtti_base_array =
774 {
775 {
776 &bad_typeid_rtti_base_descriptor,
777 &exception_rtti_base_descriptor,
778 NULL
779 }
780 };
781
782 static const rtti_object_hierarchy bad_typeid_type_hierarchy =
783 {
784 0,
785 0,
786 2,
787 &bad_typeid_rtti_base_array
788 };
789
790 const rtti_object_locator bad_typeid_rtti =
791 {
792 0,
793 0,
794 0,
795 &bad_typeid_type_info,
796 &bad_typeid_type_hierarchy
797 };
798
799 static const cxx_type_info bad_typeid_cxx_type_info =
800 {
801 0,
802 &bad_typeid_type_info,
803 { 0, -1, 0 },
804 sizeof(exception),
805 (cxx_copy_ctor)THISCALL(MSVCRT_bad_typeid_copy_ctor)
806 };
807
808 static const type_info bad_cast_type_info =
809 {
810 &MSVCRT_type_info_vtable,
811 NULL,
812 ".?AVbad_cast@@"
813 };
814
815 static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
816 {
817 &bad_cast_type_info,
818 1,
819 { 0, -1, 0 },
820 0
821 };
822
823 static const rtti_base_array bad_cast_rtti_base_array =
824 {
825 {
826 &bad_cast_rtti_base_descriptor,
827 &exception_rtti_base_descriptor,
828 NULL
829 }
830 };
831
832 static const rtti_object_hierarchy bad_cast_type_hierarchy =
833 {
834 0,
835 0,
836 2,
837 &bad_cast_rtti_base_array
838 };
839
840 const rtti_object_locator bad_cast_rtti =
841 {
842 0,
843 0,
844 0,
845 &bad_cast_type_info,
846 &bad_cast_type_hierarchy
847 };
848
849 static const cxx_type_info bad_cast_cxx_type_info =
850 {
851 0,
852 &bad_cast_type_info,
853 { 0, -1, 0 },
854 sizeof(exception),
855 (cxx_copy_ctor)THISCALL(MSVCRT_bad_cast_copy_ctor)
856 };
857
858 static const type_info __non_rtti_object_type_info =
859 {
860 &MSVCRT_type_info_vtable,
861 NULL,
862 ".?AV__non_rtti_object@@"
863 };
864
865 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
866 {
867 &__non_rtti_object_type_info,
868 2,
869 { 0, -1, 0 },
870 0
871 };
872
873 static const rtti_base_array __non_rtti_object_rtti_base_array =
874 {
875 {
876 &__non_rtti_object_rtti_base_descriptor,
877 &bad_typeid_rtti_base_descriptor,
878 &exception_rtti_base_descriptor
879 }
880 };
881
882 static const rtti_object_hierarchy __non_rtti_object_type_hierarchy =
883 {
884 0,
885 0,
886 3,
887 &__non_rtti_object_rtti_base_array
888 };
889
890 const rtti_object_locator __non_rtti_object_rtti =
891 {
892 0,
893 0,
894 0,
895 &__non_rtti_object_type_info,
896 &__non_rtti_object_type_hierarchy
897 };
898
899 static const cxx_type_info __non_rtti_object_cxx_type_info =
900 {
901 0,
902 &__non_rtti_object_type_info,
903 { 0, -1, 0 },
904 sizeof(exception),
905 (cxx_copy_ctor)THISCALL(MSVCRT___non_rtti_object_copy_ctor)
906 };
907
908 static const type_info type_info_type_info =
909 {
910 &MSVCRT_type_info_vtable,
911 NULL,
912 ".?AVtype_info@@"
913 };
914
915 static const rtti_base_descriptor type_info_rtti_base_descriptor =
916 {
917 &type_info_type_info,
918 0,
919 { 0, -1, 0 },
920 0
921 };
922
923 static const rtti_base_array type_info_rtti_base_array =
924 {
925 {
926 &type_info_rtti_base_descriptor,
927 NULL,
928 NULL
929 }
930 };
931
932 static const rtti_object_hierarchy type_info_type_hierarchy =
933 {
934 0,
935 0,
936 1,
937 &type_info_rtti_base_array
938 };
939
940 const rtti_object_locator type_info_rtti =
941 {
942 0,
943 0,
944 0,
945 &type_info_type_info,
946 &type_info_type_hierarchy
947 };
948
949 /*
950 * Exception RTTI for cpp objects
951 */
952 static const cxx_type_info_table bad_cast_type_info_table =
953 {
954 3,
955 {
956 &__non_rtti_object_cxx_type_info,
957 &bad_typeid_cxx_type_info,
958 &exception_cxx_type_info
959 }
960 };
961
962 static const cxx_exception_type bad_cast_exception_type =
963 {
964 0,
965 (void*)THISCALL(MSVCRT_bad_cast_dtor),
966 NULL,
967 &bad_cast_type_info_table
968 };
969
970 static const cxx_type_info_table bad_typeid_type_info_table =
971 {
972 2,
973 {
974 &bad_cast_cxx_type_info,
975 &exception_cxx_type_info,
976 NULL
977 }
978 };
979
980 static const cxx_exception_type bad_typeid_exception_type =
981 {
982 0,
983 (void*)THISCALL(MSVCRT_bad_typeid_dtor),
984 NULL,
985 &bad_cast_type_info_table
986 };
987
988 static const cxx_exception_type __non_rtti_object_exception_type =
989 {
990 0,
991 (void*)THISCALL(MSVCRT___non_rtti_object_dtor),
992 NULL,
993 &bad_typeid_type_info_table
994 };
995
996
997 /******************************************************************
998 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
999 *
1000 * Install a handler to be called when terminate() is called.
1001 *
1002 * PARAMS
1003 * func [I] Handler function to install
1004 *
1005 * RETURNS
1006 * The previously installed handler function, if any.
1007 */
1008 terminate_function CDECL MSVCRT_set_terminate(terminate_function func)
1009 {
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;
1014 return previous;
1015 }
1016
1017 /******************************************************************
1018 * _get_terminate (MSVCRT.@)
1019 */
1020 terminate_function CDECL _get_terminate(void)
1021 {
1022 thread_data_t *data = msvcrt_get_thread_data();
1023 return data->terminate_handler;
1024 }
1025
1026 /******************************************************************
1027 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
1028 *
1029 * Install a handler to be called when unexpected() is called.
1030 *
1031 * PARAMS
1032 * func [I] Handler function to install
1033 *
1034 * RETURNS
1035 * The previously installed handler function, if any.
1036 */
1037 unexpected_function CDECL MSVCRT_set_unexpected(unexpected_function func)
1038 {
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;
1043 return previous;
1044 }
1045
1046 /******************************************************************
1047 * _get_unexpected (MSVCRT.@)
1048 */
1049 unexpected_function CDECL _get_unexpected(void)
1050 {
1051 thread_data_t *data = msvcrt_get_thread_data();
1052 return data->unexpected_handler;
1053 }
1054
1055 /******************************************************************
1056 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
1057 */
1058 _se_translator_function CDECL MSVCRT__set_se_translator(_se_translator_function func)
1059 {
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;
1064 return previous;
1065 }
1066
1067 /******************************************************************
1068 * ?terminate@@YAXXZ (MSVCRT.@)
1069 *
1070 * Default handler for an unhandled exception.
1071 *
1072 * PARAMS
1073 * None.
1074 *
1075 * RETURNS
1076 * This function does not return. Either control resumes from any
1077 * handler installed by calling set_terminate(), or (by default) abort()
1078 * is called.
1079 */
1080 void CDECL MSVCRT_terminate(void)
1081 {
1082 thread_data_t *data = msvcrt_get_thread_data();
1083 if (data->terminate_handler) data->terminate_handler();
1084 abort();
1085 }
1086
1087 /******************************************************************
1088 * ?unexpected@@YAXXZ (MSVCRT.@)
1089 */
1090 void CDECL MSVCRT_unexpected(void)
1091 {
1092 thread_data_t *data = msvcrt_get_thread_data();
1093 if (data->unexpected_handler) data->unexpected_handler();
1094 MSVCRT_terminate();
1095 }
1096
1097
1098 /******************************************************************
1099 * __RTtypeid (MSVCRT.@)
1100 *
1101 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1102 *
1103 * PARAMS
1104 * cppobj [I] C++ object to get type information for.
1105 *
1106 * RETURNS
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.
1111 *
1112 * NOTES
1113 * This function is usually called by compiler generated code as a result
1114 * of using one of the C++ dynamic cast statements.
1115 */
1116 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
1117 {
1118 const type_info *ret;
1119
1120 if (!cppobj)
1121 {
1122 bad_typeid e;
1123 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
1124 _CxxThrowException( &e, &bad_typeid_exception_type );
1125 return NULL;
1126 }
1127
1128 __TRY
1129 {
1130 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1131 ret = obj_locator->type_descriptor;
1132 }
1133 __EXCEPT_PAGE_FAULT
1134 {
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 );
1138 return NULL;
1139 }
1140 __ENDTRY
1141 return ret;
1142 }
1143
1144 /******************************************************************
1145 * __RTDynamicCast (MSVCRT.@)
1146 *
1147 * Dynamically cast a C++ object to one of its base classes.
1148 *
1149 * PARAMS
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
1155 *
1156 * RETURNS
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.
1161 *
1162 * NOTES
1163 * This function is usually called by compiler generated code as a result
1164 * of using one of the C++ dynamic cast statements.
1165 */
1166 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
1167 type_info *src, type_info *dst,
1168 int do_throw)
1169 {
1170 void *ret;
1171
1172 if (!cppobj) return NULL;
1173
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);
1176
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
1182 *
1183 * FIXME: the unknown parameter doesn't seem to be used for anything
1184 */
1185 __TRY
1186 {
1187 int i;
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;
1191
1192 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
1193
1194 ret = NULL;
1195 for (i = 0; i < obj_bases->array_len; i++)
1196 {
1197 const type_info *typ = base_desc[i]->type_descriptor;
1198
1199 if (!strcmp(typ->mangled, dst->mangled))
1200 {
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 );
1204 break;
1205 }
1206 }
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.
1209 */
1210 if (!ret && do_throw)
1211 {
1212 const char *msg = "Bad dynamic_cast!";
1213 bad_cast e;
1214 MSVCRT_bad_cast_ctor( &e, &msg );
1215 _CxxThrowException( &e, &bad_cast_exception_type );
1216 }
1217 }
1218 __EXCEPT_PAGE_FAULT
1219 {
1220 __non_rtti_object e;
1221 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1222 _CxxThrowException( &e, &bad_typeid_exception_type );
1223 return NULL;
1224 }
1225 __ENDTRY
1226 return ret;
1227 }
1228
1229
1230 /******************************************************************
1231 * __RTCastToVoid (MSVCRT.@)
1232 *
1233 * Dynamically cast a C++ object to a void*.
1234 *
1235 * PARAMS
1236 * cppobj [I] The C++ object to cast
1237 *
1238 * RETURNS
1239 * Success: The base address of the object as a void*.
1240 * Failure: NULL, if cppobj is NULL or has no RTTI.
1241 *
1242 * NOTES
1243 * This function is usually called by compiler generated code as a result
1244 * of using one of the C++ dynamic cast statements.
1245 */
1246 void* CDECL MSVCRT___RTCastToVoid(void *cppobj)
1247 {
1248 void *ret;
1249
1250 if (!cppobj) return NULL;
1251
1252 __TRY
1253 {
1254 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1255 ret = (char *)cppobj - obj_locator->base_class_offset;
1256 }
1257 __EXCEPT_PAGE_FAULT
1258 {
1259 __non_rtti_object e;
1260 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1261 _CxxThrowException( &e, &bad_typeid_exception_type );
1262 return NULL;
1263 }
1264 __ENDTRY
1265 return ret;
1266 }