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