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