xmllite: Fix reported node type for attributes.
[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_RTTI_DATA0( type_info, 0, ".?AVtype_info@@" )
670 DEFINE_RTTI_DATA0( exception, 0, ".?AVexception@@" )
671 DEFINE_RTTI_DATA1( bad_typeid, 0, &exception_rtti_base_descriptor, ".?AVbad_typeid@@" )
672 DEFINE_RTTI_DATA1( bad_cast, 0, &exception_rtti_base_descriptor, ".?AVbad_cast@@" )
673 DEFINE_RTTI_DATA2( __non_rtti_object, 0, &bad_typeid_rtti_base_descriptor, &exception_rtti_base_descriptor, ".?AV__non_rtti_object@@" )
674
675 DEFINE_EXCEPTION_TYPE_INFO( exception, 0, NULL, NULL )
676 DEFINE_EXCEPTION_TYPE_INFO( bad_typeid, 1, &exception_cxx_type_info, NULL )
677 DEFINE_EXCEPTION_TYPE_INFO( bad_cast, 1, &exception_cxx_type_info, NULL )
678 DEFINE_EXCEPTION_TYPE_INFO( __non_rtti_object, 2, &bad_typeid_cxx_type_info, &exception_cxx_type_info )
679
680 void msvcrt_init_exception(void *base)
681 {
682 #ifdef __x86_64__
683     init_type_info_rtti(base);
684     init_exception_rtti(base);
685     init_bad_typeid_rtti(base);
686     init_bad_cast_rtti(base);
687     init___non_rtti_object_rtti(base);
688
689     init_exception_cxx(base);
690     init_bad_typeid_cxx(base);
691     init_bad_cast_cxx(base);
692     init___non_rtti_object_cxx(base);
693 #endif
694 }
695
696
697 /******************************************************************
698  *              ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
699  *
700  * Install a handler to be called when terminate() is called.
701  *
702  * PARAMS
703  *  func [I] Handler function to install
704  *
705  * RETURNS
706  *  The previously installed handler function, if any.
707  */
708 MSVCRT_terminate_function CDECL MSVCRT_set_terminate(MSVCRT_terminate_function func)
709 {
710     thread_data_t *data = msvcrt_get_thread_data();
711     MSVCRT_terminate_function previous = data->terminate_handler;
712     TRACE("(%p) returning %p\n",func,previous);
713     data->terminate_handler = func;
714     return previous;
715 }
716
717 /******************************************************************
718  *              _get_terminate (MSVCRT.@)
719  */
720 MSVCRT_terminate_function CDECL MSVCRT__get_terminate(void)
721 {
722     thread_data_t *data = msvcrt_get_thread_data();
723     TRACE("returning %p\n", data->terminate_handler);
724     return data->terminate_handler;
725 }
726
727 /******************************************************************
728  *              ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
729  *
730  * Install a handler to be called when unexpected() is called.
731  *
732  * PARAMS
733  *  func [I] Handler function to install
734  *
735  * RETURNS
736  *  The previously installed handler function, if any.
737  */
738 MSVCRT_unexpected_function CDECL MSVCRT_set_unexpected(MSVCRT_unexpected_function func)
739 {
740     thread_data_t *data = msvcrt_get_thread_data();
741     MSVCRT_unexpected_function previous = data->unexpected_handler;
742     TRACE("(%p) returning %p\n",func,previous);
743     data->unexpected_handler = func;
744     return previous;
745 }
746
747 /******************************************************************
748  *              _get_unexpected (MSVCRT.@)
749  */
750 MSVCRT_unexpected_function CDECL MSVCRT__get_unexpected(void)
751 {
752     thread_data_t *data = msvcrt_get_thread_data();
753     TRACE("returning %p\n", data->unexpected_handler);
754     return data->unexpected_handler;
755 }
756
757 /******************************************************************
758  *              ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z  (MSVCRT.@)
759  */
760 MSVCRT__se_translator_function CDECL MSVCRT__set_se_translator(MSVCRT__se_translator_function func)
761 {
762     thread_data_t *data = msvcrt_get_thread_data();
763     MSVCRT__se_translator_function previous = data->se_translator;
764     TRACE("(%p) returning %p\n",func,previous);
765     data->se_translator = func;
766     return previous;
767 }
768
769 /******************************************************************
770  *              ?terminate@@YAXXZ (MSVCRT.@)
771  *
772  * Default handler for an unhandled exception.
773  *
774  * PARAMS
775  *  None.
776  *
777  * RETURNS
778  *  This function does not return. Either control resumes from any
779  *  handler installed by calling set_terminate(), or (by default) abort()
780  *  is called.
781  */
782 void CDECL MSVCRT_terminate(void)
783 {
784     thread_data_t *data = msvcrt_get_thread_data();
785     if (data->terminate_handler) data->terminate_handler();
786     MSVCRT_abort();
787 }
788
789 /******************************************************************
790  *              ?unexpected@@YAXXZ (MSVCRT.@)
791  */
792 void CDECL MSVCRT_unexpected(void)
793 {
794     thread_data_t *data = msvcrt_get_thread_data();
795     if (data->unexpected_handler) data->unexpected_handler();
796     MSVCRT_terminate();
797 }
798
799
800 /******************************************************************
801  *              __RTtypeid (MSVCRT.@)
802  *
803  * Retrieve the Run Time Type Information (RTTI) for a C++ object.
804  *
805  * PARAMS
806  *  cppobj [I] C++ object to get type information for.
807  *
808  * RETURNS
809  *  Success: A type_info object describing cppobj.
810  *  Failure: If the object to be cast has no RTTI, a __non_rtti_object
811  *           exception is thrown. If cppobj is NULL, a bad_typeid exception
812  *           is thrown. In either case, this function does not return.
813  *
814  * NOTES
815  *  This function is usually called by compiler generated code as a result
816  *  of using one of the C++ dynamic cast statements.
817  */
818 #ifndef __x86_64__
819 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
820 {
821     const type_info *ret;
822
823     if (!cppobj)
824     {
825         bad_typeid e;
826         MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
827         _CxxThrowException( &e, &bad_typeid_exception_type );
828         return NULL;
829     }
830
831     __TRY
832     {
833         const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
834         ret = obj_locator->type_descriptor;
835     }
836     __EXCEPT_PAGE_FAULT
837     {
838         __non_rtti_object e;
839         MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
840         _CxxThrowException( &e, &bad_typeid_exception_type );
841         return NULL;
842     }
843     __ENDTRY
844     return ret;
845 }
846
847 #else
848
849 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
850 {
851     const type_info *ret;
852
853     if (!cppobj)
854     {
855         bad_typeid e;
856         MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
857         _CxxThrowException( &e, &bad_typeid_exception_type );
858         return NULL;
859     }
860
861     __TRY
862     {
863         const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
864         char *base;
865
866         if(obj_locator->signature == 0)
867             base = (char*)RtlPcToFileHeader((void*)obj_locator, (void**)&base);
868         else
869             base = (char*)obj_locator - obj_locator->object_locator;
870
871         ret = (type_info*)(base + obj_locator->type_descriptor);
872     }
873     __EXCEPT_PAGE_FAULT
874     {
875         __non_rtti_object e;
876         MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
877         _CxxThrowException( &e, &bad_typeid_exception_type );
878         return NULL;
879     }
880     __ENDTRY
881     return ret;
882 }
883 #endif
884
885 /******************************************************************
886  *              __RTDynamicCast (MSVCRT.@)
887  *
888  * Dynamically cast a C++ object to one of its base classes.
889  *
890  * PARAMS
891  *  cppobj   [I] Any C++ object to cast
892  *  unknown  [I] Reserved, set to 0
893  *  src      [I] type_info object describing cppobj
894  *  dst      [I] type_info object describing the base class to cast to
895  *  do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
896  *
897  * RETURNS
898  *  Success: The address of cppobj, cast to the object described by dst.
899  *  Failure: NULL, If the object to be cast has no RTTI, or dst is not a
900  *           valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
901  *           is thrown and this function does not return.
902  *
903  * NOTES
904  *  This function is usually called by compiler generated code as a result
905  *  of using one of the C++ dynamic cast statements.
906  */
907 #ifndef __x86_64__
908 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
909                                    type_info *src, type_info *dst,
910                                    int do_throw)
911 {
912     void *ret;
913
914     if (!cppobj) return NULL;
915
916     TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
917           cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
918
919     /* To cast an object at runtime:
920      * 1.Find out the true type of the object from the typeinfo at vtable[-1]
921      * 2.Search for the destination type in the class hierarchy
922      * 3.If destination type is found, return base object address + dest offset
923      *   Otherwise, fail the cast
924      *
925      * FIXME: the unknown parameter doesn't seem to be used for anything
926      */
927     __TRY
928     {
929         int i;
930         const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
931         const rtti_object_hierarchy *obj_bases = obj_locator->type_hierarchy;
932         const rtti_base_descriptor * const* base_desc = obj_bases->base_classes->bases;
933
934         if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
935
936         ret = NULL;
937         for (i = 0; i < obj_bases->array_len; i++)
938         {
939             const type_info *typ = base_desc[i]->type_descriptor;
940
941             if (!strcmp(typ->mangled, dst->mangled))
942             {
943                 /* compute the correct this pointer for that base class */
944                 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
945                 ret = get_this_pointer( &base_desc[i]->offsets, this_ptr );
946                 break;
947             }
948         }
949         /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
950          * to a reference, since references cannot be NULL.
951          */
952         if (!ret && do_throw)
953         {
954             const char *msg = "Bad dynamic_cast!";
955             bad_cast e;
956             MSVCRT_bad_cast_ctor( &e, &msg );
957             _CxxThrowException( &e, &bad_cast_exception_type );
958         }
959     }
960     __EXCEPT_PAGE_FAULT
961     {
962         __non_rtti_object e;
963         MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
964         _CxxThrowException( &e, &bad_typeid_exception_type );
965         return NULL;
966     }
967     __ENDTRY
968     return ret;
969 }
970
971 #else
972
973 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
974         type_info *src, type_info *dst,
975         int do_throw)
976 {
977     void *ret;
978
979     if (!cppobj) return NULL;
980
981     TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
982             cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
983
984     __TRY
985     {
986         int i;
987         const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
988         const rtti_object_hierarchy *obj_bases;
989         const rtti_base_array *base_array;
990         char *base;
991
992         if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
993
994         if(obj_locator->signature == 0)
995             base = (char*)RtlPcToFileHeader((void*)obj_locator, (void**)&base);
996         else
997             base = (char*)obj_locator - obj_locator->object_locator;
998
999         obj_bases = (const rtti_object_hierarchy*)(base + obj_locator->type_hierarchy);
1000         base_array = (const rtti_base_array*)(base + obj_bases->base_classes);
1001
1002         ret = NULL;
1003         for (i = 0; i < obj_bases->array_len; i++)
1004         {
1005             const rtti_base_descriptor *base_desc = (const rtti_base_descriptor*)(base + base_array->bases[i]);
1006             const type_info *typ = (const type_info*)(base + base_desc->type_descriptor);
1007
1008             if (!strcmp(typ->mangled, dst->mangled))
1009             {
1010                 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
1011                 ret = get_this_pointer( &base_desc->offsets, this_ptr );
1012                 break;
1013             }
1014         }
1015         if (!ret && do_throw)
1016         {
1017             const char *msg = "Bad dynamic_cast!";
1018             bad_cast e;
1019             MSVCRT_bad_cast_ctor( &e, &msg );
1020             _CxxThrowException( &e, &bad_cast_exception_type );
1021         }
1022     }
1023     __EXCEPT_PAGE_FAULT
1024     {
1025         __non_rtti_object e;
1026         MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1027         _CxxThrowException( &e, &bad_typeid_exception_type );
1028         return NULL;
1029     }
1030     __ENDTRY
1031     return ret;
1032 }
1033 #endif
1034
1035
1036 /******************************************************************
1037  *              __RTCastToVoid (MSVCRT.@)
1038  *
1039  * Dynamically cast a C++ object to a void*.
1040  *
1041  * PARAMS
1042  *  cppobj [I] The C++ object to cast
1043  *
1044  * RETURNS
1045  *  Success: The base address of the object as a void*.
1046  *  Failure: NULL, if cppobj is NULL or has no RTTI.
1047  *
1048  * NOTES
1049  *  This function is usually called by compiler generated code as a result
1050  *  of using one of the C++ dynamic cast statements.
1051  */
1052 void* CDECL MSVCRT___RTCastToVoid(void *cppobj)
1053 {
1054     void *ret;
1055
1056     if (!cppobj) return NULL;
1057
1058     __TRY
1059     {
1060         const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1061         ret = (char *)cppobj - obj_locator->base_class_offset;
1062     }
1063     __EXCEPT_PAGE_FAULT
1064     {
1065         __non_rtti_object e;
1066         MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1067         _CxxThrowException( &e, &bad_typeid_exception_type );
1068         return NULL;
1069     }
1070     __ENDTRY
1071     return ret;
1072 }
1073
1074
1075 /*********************************************************************
1076  *              _CxxThrowException (MSVCRT.@)
1077  */
1078 void WINAPI _CxxThrowException( exception *object, const cxx_exception_type *type )
1079 {
1080     ULONG_PTR args[3];
1081
1082     args[0] = CXX_FRAME_MAGIC_VC6;
1083     args[1] = (ULONG_PTR)object;
1084     args[2] = (ULONG_PTR)type;
1085     RaiseException( CXX_EXCEPTION, EH_NONCONTINUABLE, 3, args );
1086 }