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