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