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