2 * Copyright 2010 Piotr Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(msvcp90);
30 /* dlls/msvcrt/cppexcept.h */
31 typedef void (*cxx_copy_ctor)(void);
33 /* complete information about a C++ type */
34 typedef struct __cxx_type_info
36 UINT flags; /* flags (see CLASS_* flags below) */
37 const type_info *type_info; /* C++ type info */
38 this_ptr_offsets offsets; /* offsets for computing the this pointer */
39 unsigned int size; /* object size */
40 cxx_copy_ctor copy_ctor; /* copy constructor */
42 #define CLASS_IS_SIMPLE_TYPE 1
43 #define CLASS_HAS_VIRTUAL_BASE_CLASS 4
45 /* table of C++ types that apply for a given object */
46 typedef struct __cxx_type_info_table
48 UINT count; /* number of types */
49 const cxx_type_info *info[3]; /* variable length, we declare it large enough for static RTTI */
50 } cxx_type_info_table;
52 /* type information for an exception object */
53 typedef struct __cxx_exception_type
55 UINT flags; /* TYPE_FLAG flags */
56 void (*destructor)(void);/* exception object destructor */
57 void* /*cxx_exc_custom_handler*/ custom_handler; /* custom handler for this exception */
58 const cxx_type_info_table *type_info_table; /* list of types for this exception object */
61 void WINAPI _CxxThrowException(exception*,const cxx_exception_type*);
67 #define __ASM_EXCEPTION_VTABLE(name) \
68 __ASM_VTABLE(name, "\t.quad " THISCALL_NAME(MSVCP_what_exception) )
70 #define __ASM_EXCEPTION_STRING_VTABLE(name) \
71 __ASM_VTABLE(name, "\t.quad " THISCALL_NAME(MSVCP_logic_error_what) )
75 #define __ASM_EXCEPTION_VTABLE(name) \
76 __ASM_VTABLE(name, "\t.long " THISCALL_NAME(MSVCP_what_exception) )
78 #define __ASM_EXCEPTION_STRING_VTABLE(name) \
79 __ASM_VTABLE(name, "\t.long " THISCALL_NAME(MSVCP_logic_error_what) )
83 extern const vtable_ptr MSVCP_bad_alloc_vtable;
84 extern const vtable_ptr MSVCP_logic_error_vtable;
85 extern const vtable_ptr MSVCP_length_error_vtable;
86 extern const vtable_ptr MSVCP_out_of_range_vtable;
87 extern const vtable_ptr MSVCP_invalid_argument_vtable;
89 /* exception class data */
90 static type_info exception_type_info = {
91 NULL, /* set by set_exception_vtable */
96 DEFINE_THISCALL_WRAPPER(MSVCP_exception_ctor, 8)
97 exception* __thiscall MSVCP_exception_ctor(exception *this, const char **name)
99 TRACE("(%p %s)\n", this, *name);
101 this->vtable = exception_type_info.vtable;
103 unsigned int name_len = strlen(*name) + 1;
104 this->name = malloc(name_len);
105 memcpy(this->name, *name, name_len);
106 this->do_free = TRUE;
109 this->do_free = FALSE;
114 DEFINE_THISCALL_WRAPPER(MSVCP_exception_copy_ctor,8)
115 exception* __thiscall MSVCP_exception_copy_ctor(exception *this, const exception *rhs)
117 TRACE("(%p,%p)\n", this, rhs);
120 this->vtable = exception_type_info.vtable;
121 this->name = rhs->name;
122 this->do_free = FALSE;
124 MSVCP_exception_ctor(this, (const char**)&rhs->name);
125 TRACE("name = %s\n", this->name);
129 DEFINE_THISCALL_WRAPPER(MSVCP_exception_dtor,4)
130 void __thiscall MSVCP_exception_dtor(exception *this)
132 TRACE("(%p)\n", this);
133 this->vtable = exception_type_info.vtable;
138 static const rtti_base_descriptor exception_rtti_base_descriptor = {
139 &exception_type_info,
145 static const cxx_type_info exception_cxx_type_info = {
147 &exception_type_info,
150 (cxx_copy_ctor)THISCALL(MSVCP_exception_dtor)
153 static const cxx_type_info_table exception_cxx_type_table = {
156 &exception_cxx_type_info,
162 static const cxx_exception_type exception_cxx_type = {
164 (cxx_copy_ctor)THISCALL(MSVCP_exception_copy_ctor),
166 &exception_cxx_type_table
169 void set_exception_vtable(void)
171 HMODULE hmod = GetModuleHandleA("msvcrt.dll");
172 exception_type_info.vtable = (void*)GetProcAddress(hmod, "??_7exception@@6B@");
175 /* bad_alloc class data */
176 typedef exception bad_alloc;
178 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_ctor, 8)
179 bad_alloc* __thiscall MSVCP_bad_alloc_ctor(bad_alloc *this, const char **name)
181 TRACE("%p %s\n", this, *name);
182 MSVCP_exception_ctor(this, name);
183 this->vtable = &MSVCP_bad_alloc_vtable;
187 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_copy_ctor, 8)
188 bad_alloc* __thiscall MSVCP_bad_alloc_copy_ctor(bad_alloc *this, const bad_alloc *rhs)
190 TRACE("%p %p\n", this, rhs);
191 MSVCP_exception_copy_ctor(this, rhs);
192 this->vtable = &MSVCP_bad_alloc_vtable;
196 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_dtor, 4)
197 void __thiscall MSVCP_bad_alloc_dtor(bad_alloc *this)
200 MSVCP_exception_dtor(this);
203 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_vector_dtor, 8)
204 void * __thiscall MSVCP_bad_alloc_vector_dtor(bad_alloc *this, unsigned int flags)
206 TRACE("%p %x\n", this, flags);
208 /* we have an array, with the number of elements stored before the first object */
209 int i, *ptr = (int *)this-1;
211 for(i=*ptr-1; i>=0; i--)
212 MSVCP_bad_alloc_dtor(this+i);
213 MSVCRT_operator_delete(ptr);
215 MSVCP_bad_alloc_dtor(this);
217 MSVCRT_operator_delete(this);
223 DEFINE_THISCALL_WRAPPER(MSVCP_what_exception,4)
224 const char* __thiscall MSVCP_what_exception(exception * this)
226 TRACE("(%p) returning %s\n", this, this->name);
227 return this->name ? this->name : "Unknown exception";
230 static const type_info bad_alloc_type_info = {
231 &MSVCP_bad_alloc_vtable,
233 ".?AVbad_alloc@std@@"
236 static const rtti_base_descriptor bad_alloc_rtti_base_descriptor = {
237 &bad_alloc_type_info,
243 static const rtti_base_array bad_alloc_rtti_base_array = {
245 &bad_alloc_rtti_base_descriptor,
246 &exception_rtti_base_descriptor,
251 static const rtti_object_hierarchy bad_alloc_type_hierarchy = {
255 &bad_alloc_rtti_base_array
258 const rtti_object_locator bad_alloc_rtti = {
262 &bad_alloc_type_info,
263 &bad_alloc_type_hierarchy
266 static const cxx_type_info bad_alloc_cxx_type_info = {
268 &bad_alloc_type_info,
271 (cxx_copy_ctor)THISCALL(MSVCP_bad_alloc_copy_ctor)
274 static const cxx_type_info_table bad_alloc_cxx_type_table = {
277 &bad_alloc_cxx_type_info,
278 &exception_cxx_type_info,
283 static const cxx_exception_type bad_alloc_cxx_type = {
285 (cxx_copy_ctor)THISCALL(MSVCP_bad_alloc_dtor),
287 &bad_alloc_cxx_type_table
290 /* logic_error class data */
291 typedef struct _logic_error {
293 basic_string_char str;
296 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_ctor, 8)
297 logic_error* __thiscall MSVCP_logic_error_ctor(
298 logic_error *this, const char **name)
300 TRACE("%p %s\n", this, *name);
301 this->e.vtable = &MSVCP_logic_error_vtable;
303 this->e.do_free = FALSE;
304 MSVCP_basic_string_char_ctor_cstr(&this->str, *name);
308 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_copy_ctor, 8)
309 logic_error* __thiscall MSVCP_logic_error_copy_ctor(
310 logic_error *this, logic_error *rhs)
312 TRACE("%p %p\n", this, rhs);
313 MSVCP_exception_copy_ctor(&this->e, &rhs->e);
314 MSVCP_basic_string_char_copy_ctor(&this->str, &rhs->str);
315 this->e.vtable = &MSVCP_logic_error_vtable;
319 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_dtor, 4)
320 void __thiscall MSVCP_logic_error_dtor(logic_error *this)
323 MSVCP_exception_dtor(&this->e);
324 MSVCP_basic_string_char_dtor(&this->str);
327 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_vector_dtor, 8)
328 void* __thiscall MSVCP_logic_error_vector_dtor(
329 logic_error *this, unsigned int flags)
331 TRACE("%p %x\n", this, flags);
333 /* we have an array, with the number of elements stored before the first object */
334 int i, *ptr = (int *)this-1;
336 for(i=*ptr-1; i>=0; i--)
337 MSVCP_logic_error_dtor(this+i);
338 MSVCRT_operator_delete(ptr);
340 MSVCP_logic_error_dtor(this);
342 MSVCRT_operator_delete(this);
348 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_what, 4)
349 const char* __thiscall MSVCP_logic_error_what(logic_error *this)
352 return MSVCP_basic_string_char_c_str(&this->str);
355 static const type_info logic_error_type_info = {
356 &MSVCP_logic_error_vtable,
358 ".?AVlogic_error@std@@"
361 static const rtti_base_descriptor logic_error_rtti_base_descriptor = {
362 &logic_error_type_info,
368 static const rtti_base_array logic_error_rtti_base_array = {
370 &logic_error_rtti_base_descriptor,
371 &exception_rtti_base_descriptor,
376 static const rtti_object_hierarchy logic_error_type_hierarchy = {
380 &logic_error_rtti_base_array
383 const rtti_object_locator logic_error_rtti = {
387 &logic_error_type_info,
388 &logic_error_type_hierarchy
391 static const cxx_type_info logic_error_cxx_type_info = {
393 &logic_error_type_info,
396 (cxx_copy_ctor)THISCALL(MSVCP_logic_error_copy_ctor)
399 static const cxx_type_info_table logic_error_cxx_type_table = {
402 &logic_error_cxx_type_info,
403 &exception_cxx_type_info,
408 static const cxx_exception_type logic_error_cxx_type = {
410 (cxx_copy_ctor)THISCALL(MSVCP_logic_error_dtor),
412 &logic_error_cxx_type_table
415 /* length_error class data */
416 typedef logic_error length_error;
418 DEFINE_THISCALL_WRAPPER(MSVCP_length_error_ctor, 8)
419 length_error* __thiscall MSVCP_length_error_ctor(
420 length_error *this, const char **name)
422 TRACE("%p %s\n", this, *name);
423 MSVCP_logic_error_ctor(this, name);
424 this->e.vtable = &MSVCP_length_error_vtable;
428 DEFINE_THISCALL_WRAPPER(MSVCP_length_error_copy_ctor, 8)
429 length_error* __thiscall MSVCP_length_error_copy_ctor(
430 length_error *this, length_error *rhs)
432 TRACE("%p %p\n", this, rhs);
433 MSVCP_logic_error_copy_ctor(this, rhs);
434 this->e.vtable = &MSVCP_length_error_vtable;
438 DEFINE_THISCALL_WRAPPER(MSVCP_length_error_vector_dtor, 8)
439 void* __thiscall MSVCP_length_error_vector_dtor(
440 length_error *this, unsigned int flags)
442 TRACE("%p %x\n", this, flags);
443 return MSVCP_logic_error_vector_dtor(this, flags);
446 static const type_info length_error_type_info = {
447 &MSVCP_length_error_vtable,
449 ".?AVlength_error@std@@"
452 static const rtti_base_descriptor length_error_rtti_base_descriptor = {
453 &length_error_type_info,
459 static const rtti_base_array length_error_rtti_base_array = {
461 &length_error_rtti_base_descriptor,
462 &logic_error_rtti_base_descriptor,
463 &exception_rtti_base_descriptor
467 static const rtti_object_hierarchy length_error_type_hierarchy = {
471 &length_error_rtti_base_array
474 const rtti_object_locator length_error_rtti = {
478 &length_error_type_info,
479 &length_error_type_hierarchy
482 static const cxx_type_info length_error_cxx_type_info = {
484 &length_error_type_info,
486 sizeof(length_error),
487 (cxx_copy_ctor)THISCALL(MSVCP_length_error_copy_ctor)
490 static const cxx_type_info_table length_error_cxx_type_table = {
493 &length_error_cxx_type_info,
494 &logic_error_cxx_type_info,
495 &exception_cxx_type_info
499 static const cxx_exception_type length_error_cxx_type = {
501 (cxx_copy_ctor)THISCALL(MSVCP_logic_error_dtor),
503 &length_error_cxx_type_table
506 /* out_of_range class data */
507 typedef logic_error out_of_range;
509 DEFINE_THISCALL_WRAPPER(MSVCP_out_of_range_ctor, 8)
510 out_of_range* __thiscall MSVCP_out_of_range_ctor(
511 out_of_range *this, const char **name)
513 TRACE("%p %s\n", this, *name);
514 MSVCP_logic_error_ctor(this, name);
515 this->e.vtable = &MSVCP_out_of_range_vtable;
519 DEFINE_THISCALL_WRAPPER(MSVCP_out_of_range_copy_ctor, 8)
520 out_of_range* __thiscall MSVCP_out_of_range_copy_ctor(
521 out_of_range *this, out_of_range *rhs)
523 TRACE("%p %p\n", this, rhs);
524 MSVCP_logic_error_copy_ctor(this, rhs);
525 this->e.vtable = &MSVCP_out_of_range_vtable;
529 DEFINE_THISCALL_WRAPPER(MSVCP_out_of_range_vector_dtor, 8)
530 void* __thiscall MSVCP_out_of_range_vector_dtor(
531 out_of_range *this, unsigned int flags)
533 TRACE("%p %x\n", this, flags);
534 return MSVCP_logic_error_vector_dtor(this, flags);
537 static const type_info out_of_range_type_info = {
538 &MSVCP_out_of_range_vtable,
540 ".?AVout_of_range@std@@"
543 static const rtti_base_descriptor out_of_range_rtti_base_descriptor = {
544 &out_of_range_type_info,
550 static const rtti_base_array out_of_range_rtti_base_array = {
552 &out_of_range_rtti_base_descriptor,
553 &logic_error_rtti_base_descriptor,
554 &exception_rtti_base_descriptor
558 static const rtti_object_hierarchy out_of_range_type_hierarchy = {
562 &out_of_range_rtti_base_array
565 const rtti_object_locator out_of_range_rtti = {
569 &out_of_range_type_info,
570 &out_of_range_type_hierarchy
573 static const cxx_type_info out_of_range_cxx_type_info = {
575 &out_of_range_type_info,
577 sizeof(out_of_range),
578 (cxx_copy_ctor)THISCALL(MSVCP_out_of_range_copy_ctor)
581 static const cxx_type_info_table out_of_range_cxx_type_table = {
584 &out_of_range_cxx_type_info,
585 &logic_error_cxx_type_info,
586 &exception_cxx_type_info
590 static const cxx_exception_type out_of_range_cxx_type = {
592 (cxx_copy_ctor)THISCALL(MSVCP_logic_error_dtor),
594 &out_of_range_cxx_type_table
597 /* invalid_argument class data */
598 typedef logic_error invalid_argument;
600 DEFINE_THISCALL_WRAPPER(MSVCP_invalid_argument_ctor, 8)
601 invalid_argument* __thiscall MSVCP_invalid_argument_ctor(
602 invalid_argument *this, const char **name)
604 TRACE("%p %s\n", this, *name);
605 MSVCP_logic_error_ctor(this, name);
606 this->e.vtable = &MSVCP_invalid_argument_vtable;
610 DEFINE_THISCALL_WRAPPER(MSVCP_invalid_argument_copy_ctor, 8)
611 invalid_argument* __thiscall MSVCP_invalid_argument_copy_ctor(
612 invalid_argument *this, invalid_argument *rhs)
614 TRACE("%p %p\n", this, rhs);
615 MSVCP_logic_error_copy_ctor(this, rhs);
616 this->e.vtable = &MSVCP_invalid_argument_vtable;
620 DEFINE_THISCALL_WRAPPER(MSVCP_invalid_argument_vector_dtor, 8)
621 void* __thiscall MSVCP_invalid_argument_vector_dtor(
622 invalid_argument *this, unsigned int flags)
624 TRACE("%p %x\n", this, flags);
625 return MSVCP_logic_error_vector_dtor(this, flags);
628 static const type_info invalid_argument_type_info = {
629 &MSVCP_invalid_argument_vtable,
631 ".?AVinvalid_argument@std@@"
634 static const rtti_base_descriptor invalid_argument_rtti_base_descriptor = {
635 &invalid_argument_type_info,
641 static const rtti_base_array invalid_argument_rtti_base_array = {
643 &invalid_argument_rtti_base_descriptor,
644 &logic_error_rtti_base_descriptor,
645 &exception_rtti_base_descriptor
649 static const rtti_object_hierarchy invalid_argument_type_hierarchy = {
653 &invalid_argument_rtti_base_array
656 const rtti_object_locator invalid_argument_rtti = {
660 &invalid_argument_type_info,
661 &invalid_argument_type_hierarchy
664 static const cxx_type_info invalid_argument_cxx_type_info = {
666 &invalid_argument_type_info,
668 sizeof(invalid_argument),
669 (cxx_copy_ctor)THISCALL(MSVCP_invalid_argument_copy_ctor)
672 static const cxx_type_info_table invalid_argument_cxx_type_table = {
675 &invalid_argument_cxx_type_info,
676 &logic_error_cxx_type_info,
677 &exception_cxx_type_info
681 static const cxx_exception_type invalid_argument_cxx_type = {
683 (cxx_copy_ctor)THISCALL(MSVCP_logic_error_dtor),
685 &invalid_argument_cxx_type_table
689 void __asm_dummy_vtables(void) {
691 __ASM_EXCEPTION_VTABLE(bad_alloc)
692 __ASM_EXCEPTION_STRING_VTABLE(logic_error)
693 __ASM_EXCEPTION_STRING_VTABLE(length_error)
694 __ASM_EXCEPTION_STRING_VTABLE(out_of_range)
695 __ASM_EXCEPTION_STRING_VTABLE(invalid_argument)
700 /* Internal: throws selected exception */
701 void throw_exception(exception_type et, const char *str)
703 const char *addr = str;
708 MSVCP_exception_ctor(&e, &addr);
709 _CxxThrowException(&e, &exception_cxx_type);
712 case EXCEPTION_BAD_ALLOC: {
714 MSVCP_bad_alloc_ctor(&e, &addr);
715 _CxxThrowException(&e, &bad_alloc_cxx_type);
718 case EXCEPTION_LOGIC_ERROR: {
720 MSVCP_logic_error_ctor(&e, &addr);
721 _CxxThrowException((exception*)&e, &logic_error_cxx_type);
724 case EXCEPTION_LENGTH_ERROR: {
726 MSVCP_length_error_ctor(&e, &addr);
727 _CxxThrowException((exception*)&e, &length_error_cxx_type);
730 case EXCEPTION_OUT_OF_RANGE: {
732 MSVCP_out_of_range_ctor(&e, &addr);
733 _CxxThrowException((exception*)&e, &out_of_range_cxx_type);
735 case EXCEPTION_INVALID_ARGUMENT: {
737 MSVCP_invalid_argument_ctor(&e, &addr);
738 _CxxThrowException((exception*)&e, &invalid_argument_cxx_type);