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