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