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
*);
64 extern const vtable_ptr MSVCP_exception_vtable
;
65 extern const vtable_ptr MSVCP_bad_alloc_vtable
;
66 extern const vtable_ptr MSVCP_logic_error_vtable
;
67 extern const vtable_ptr MSVCP_length_error_vtable
;
68 extern const vtable_ptr MSVCP_out_of_range_vtable
;
69 extern const vtable_ptr MSVCP_invalid_argument_vtable
;
70 extern const vtable_ptr MSVCP_runtime_error_vtable
;
72 DEFINE_THISCALL_WRAPPER(MSVCP_exception_ctor
, 8)
73 exception
* __thiscall
MSVCP_exception_ctor(exception
*this, const char **name
)
75 TRACE("(%p %s)\n", this, *name
);
77 this->vtable
= &MSVCP_exception_vtable
;
79 unsigned int name_len
= strlen(*name
) + 1;
80 this->name
= malloc(name_len
);
81 memcpy(this->name
, *name
, name_len
);
85 this->do_free
= FALSE
;
90 DEFINE_THISCALL_WRAPPER(MSVCP_exception_copy_ctor
,8)
91 exception
* __thiscall
MSVCP_exception_copy_ctor(exception
*this, const exception
*rhs
)
93 TRACE("(%p,%p)\n", this, rhs
);
96 this->vtable
= &MSVCP_exception_vtable
;
97 this->name
= rhs
->name
;
98 this->do_free
= FALSE
;
100 MSVCP_exception_ctor(this, (const char**)&rhs
->name
);
101 TRACE("name = %s\n", this->name
);
105 DEFINE_THISCALL_WRAPPER(MSVCP_exception_dtor
,4)
106 void __thiscall
MSVCP_exception_dtor(exception
*this)
108 TRACE("(%p)\n", this);
109 this->vtable
= &MSVCP_exception_vtable
;
114 DEFINE_THISCALL_WRAPPER(MSVCP_exception_vector_dtor
, 8)
115 void * __thiscall
MSVCP_exception_vector_dtor(exception
*this, unsigned int flags
)
117 TRACE("%p %x\n", this, flags
);
119 /* we have an array, with the number of elements stored before the first object */
120 int i
, *ptr
= (int *)this-1;
122 for(i
=*ptr
-1; i
>=0; i
--)
123 MSVCP_exception_dtor(this+i
);
124 MSVCRT_operator_delete(ptr
);
126 MSVCP_exception_dtor(this);
128 MSVCRT_operator_delete(this);
134 DEFINE_RTTI_DATA(exception
, 0, 0, NULL
, NULL
, NULL
, ".?AVexception@std@@");
136 static const cxx_type_info exception_cxx_type_info
= {
138 &exception_type_info
,
141 (cxx_copy_ctor
)THISCALL(MSVCP_exception_dtor
)
144 static const cxx_type_info_table exception_cxx_type_table
= {
147 &exception_cxx_type_info
,
153 static const cxx_exception_type exception_cxx_type
= {
155 (cxx_copy_ctor
)THISCALL(MSVCP_exception_copy_ctor
),
157 &exception_cxx_type_table
160 /* bad_alloc class data */
161 typedef exception bad_alloc
;
163 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_ctor
, 8)
164 bad_alloc
* __thiscall
MSVCP_bad_alloc_ctor(bad_alloc
*this, const char **name
)
166 TRACE("%p %s\n", this, *name
);
167 MSVCP_exception_ctor(this, name
);
168 this->vtable
= &MSVCP_bad_alloc_vtable
;
172 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_copy_ctor
, 8)
173 bad_alloc
* __thiscall
MSVCP_bad_alloc_copy_ctor(bad_alloc
*this, const bad_alloc
*rhs
)
175 TRACE("%p %p\n", this, rhs
);
176 MSVCP_exception_copy_ctor(this, rhs
);
177 this->vtable
= &MSVCP_bad_alloc_vtable
;
181 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_dtor
, 4)
182 void __thiscall
MSVCP_bad_alloc_dtor(bad_alloc
*this)
185 MSVCP_exception_dtor(this);
188 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_vector_dtor
, 8)
189 void * __thiscall
MSVCP_bad_alloc_vector_dtor(bad_alloc
*this, unsigned int flags
)
191 TRACE("%p %x\n", this, flags
);
193 /* we have an array, with the number of elements stored before the first object */
194 int i
, *ptr
= (int *)this-1;
196 for(i
=*ptr
-1; i
>=0; i
--)
197 MSVCP_bad_alloc_dtor(this+i
);
198 MSVCRT_operator_delete(ptr
);
200 MSVCP_bad_alloc_dtor(this);
202 MSVCRT_operator_delete(this);
208 DEFINE_THISCALL_WRAPPER(MSVCP_what_exception
,4)
209 const char* __thiscall
MSVCP_what_exception(exception
* this)
211 TRACE("(%p) returning %s\n", this, this->name
);
212 return this->name
? this->name
: "Unknown exception";
215 DEFINE_RTTI_DATA(bad_alloc
, 0, 1, &exception_rtti_base_descriptor
, NULL
, NULL
, ".?AVbad_alloc@std@@");
217 static const cxx_type_info bad_alloc_cxx_type_info
= {
219 &bad_alloc_type_info
,
222 (cxx_copy_ctor
)THISCALL(MSVCP_bad_alloc_copy_ctor
)
225 static const cxx_type_info_table bad_alloc_cxx_type_table
= {
228 &bad_alloc_cxx_type_info
,
229 &exception_cxx_type_info
,
234 static const cxx_exception_type bad_alloc_cxx_type
= {
236 (cxx_copy_ctor
)THISCALL(MSVCP_bad_alloc_dtor
),
238 &bad_alloc_cxx_type_table
241 /* logic_error class data */
242 typedef struct _logic_error
{
244 basic_string_char str
;
247 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_ctor
, 8)
248 logic_error
* __thiscall
MSVCP_logic_error_ctor(
249 logic_error
*this, const char **name
)
251 TRACE("%p %s\n", this, *name
);
252 this->e
.vtable
= &MSVCP_logic_error_vtable
;
254 this->e
.do_free
= FALSE
;
255 MSVCP_basic_string_char_ctor_cstr(&this->str
, *name
);
259 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_copy_ctor
, 8)
260 logic_error
* __thiscall
MSVCP_logic_error_copy_ctor(
261 logic_error
*this, logic_error
*rhs
)
263 TRACE("%p %p\n", this, rhs
);
264 MSVCP_exception_copy_ctor(&this->e
, &rhs
->e
);
265 MSVCP_basic_string_char_copy_ctor(&this->str
, &rhs
->str
);
266 this->e
.vtable
= &MSVCP_logic_error_vtable
;
270 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_dtor
, 4)
271 void __thiscall
MSVCP_logic_error_dtor(logic_error
*this)
274 MSVCP_exception_dtor(&this->e
);
275 MSVCP_basic_string_char_dtor(&this->str
);
278 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_vector_dtor
, 8)
279 void* __thiscall
MSVCP_logic_error_vector_dtor(
280 logic_error
*this, unsigned int flags
)
282 TRACE("%p %x\n", this, flags
);
284 /* we have an array, with the number of elements stored before the first object */
285 int i
, *ptr
= (int *)this-1;
287 for(i
=*ptr
-1; i
>=0; i
--)
288 MSVCP_logic_error_dtor(this+i
);
289 MSVCRT_operator_delete(ptr
);
291 MSVCP_logic_error_dtor(this);
293 MSVCRT_operator_delete(this);
299 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_what
, 4)
300 const char* __thiscall
MSVCP_logic_error_what(logic_error
*this)
303 return MSVCP_basic_string_char_c_str(&this->str
);
306 DEFINE_RTTI_DATA(logic_error
, 0, 1, &exception_rtti_base_descriptor
, NULL
, NULL
, ".?AVlogic_error@std@@");
308 static const cxx_type_info logic_error_cxx_type_info
= {
310 &logic_error_type_info
,
313 (cxx_copy_ctor
)THISCALL(MSVCP_logic_error_copy_ctor
)
316 static const cxx_type_info_table logic_error_cxx_type_table
= {
319 &logic_error_cxx_type_info
,
320 &exception_cxx_type_info
,
325 static const cxx_exception_type logic_error_cxx_type
= {
327 (cxx_copy_ctor
)THISCALL(MSVCP_logic_error_dtor
),
329 &logic_error_cxx_type_table
332 /* length_error class data */
333 typedef logic_error length_error
;
335 DEFINE_THISCALL_WRAPPER(MSVCP_length_error_ctor
, 8)
336 length_error
* __thiscall
MSVCP_length_error_ctor(
337 length_error
*this, const char **name
)
339 TRACE("%p %s\n", this, *name
);
340 MSVCP_logic_error_ctor(this, name
);
341 this->e
.vtable
= &MSVCP_length_error_vtable
;
345 DEFINE_THISCALL_WRAPPER(MSVCP_length_error_copy_ctor
, 8)
346 length_error
* __thiscall
MSVCP_length_error_copy_ctor(
347 length_error
*this, length_error
*rhs
)
349 TRACE("%p %p\n", this, rhs
);
350 MSVCP_logic_error_copy_ctor(this, rhs
);
351 this->e
.vtable
= &MSVCP_length_error_vtable
;
355 DEFINE_THISCALL_WRAPPER(MSVCP_length_error_vector_dtor
, 8)
356 void* __thiscall
MSVCP_length_error_vector_dtor(
357 length_error
*this, unsigned int flags
)
359 TRACE("%p %x\n", this, flags
);
360 return MSVCP_logic_error_vector_dtor(this, flags
);
363 DEFINE_RTTI_DATA(length_error
, 0, 2, &logic_error_rtti_base_descriptor
, &exception_rtti_base_descriptor
, NULL
, ".?AVlength_error@std@@");
365 static const cxx_type_info length_error_cxx_type_info
= {
367 &length_error_type_info
,
369 sizeof(length_error
),
370 (cxx_copy_ctor
)THISCALL(MSVCP_length_error_copy_ctor
)
373 static const cxx_type_info_table length_error_cxx_type_table
= {
376 &length_error_cxx_type_info
,
377 &logic_error_cxx_type_info
,
378 &exception_cxx_type_info
382 static const cxx_exception_type length_error_cxx_type
= {
384 (cxx_copy_ctor
)THISCALL(MSVCP_logic_error_dtor
),
386 &length_error_cxx_type_table
389 /* out_of_range class data */
390 typedef logic_error out_of_range
;
392 DEFINE_THISCALL_WRAPPER(MSVCP_out_of_range_ctor
, 8)
393 out_of_range
* __thiscall
MSVCP_out_of_range_ctor(
394 out_of_range
*this, const char **name
)
396 TRACE("%p %s\n", this, *name
);
397 MSVCP_logic_error_ctor(this, name
);
398 this->e
.vtable
= &MSVCP_out_of_range_vtable
;
402 DEFINE_THISCALL_WRAPPER(MSVCP_out_of_range_copy_ctor
, 8)
403 out_of_range
* __thiscall
MSVCP_out_of_range_copy_ctor(
404 out_of_range
*this, out_of_range
*rhs
)
406 TRACE("%p %p\n", this, rhs
);
407 MSVCP_logic_error_copy_ctor(this, rhs
);
408 this->e
.vtable
= &MSVCP_out_of_range_vtable
;
412 DEFINE_THISCALL_WRAPPER(MSVCP_out_of_range_vector_dtor
, 8)
413 void* __thiscall
MSVCP_out_of_range_vector_dtor(
414 out_of_range
*this, unsigned int flags
)
416 TRACE("%p %x\n", this, flags
);
417 return MSVCP_logic_error_vector_dtor(this, flags
);
420 DEFINE_RTTI_DATA(out_of_range
, 0, 2, &logic_error_rtti_base_descriptor
, &exception_rtti_base_descriptor
, NULL
, ".?AVout_of_range@std@@");
422 static const cxx_type_info out_of_range_cxx_type_info
= {
424 &out_of_range_type_info
,
426 sizeof(out_of_range
),
427 (cxx_copy_ctor
)THISCALL(MSVCP_out_of_range_copy_ctor
)
430 static const cxx_type_info_table out_of_range_cxx_type_table
= {
433 &out_of_range_cxx_type_info
,
434 &logic_error_cxx_type_info
,
435 &exception_cxx_type_info
439 static const cxx_exception_type out_of_range_cxx_type
= {
441 (cxx_copy_ctor
)THISCALL(MSVCP_logic_error_dtor
),
443 &out_of_range_cxx_type_table
446 /* invalid_argument class data */
447 typedef logic_error invalid_argument
;
449 DEFINE_THISCALL_WRAPPER(MSVCP_invalid_argument_ctor
, 8)
450 invalid_argument
* __thiscall
MSVCP_invalid_argument_ctor(
451 invalid_argument
*this, const char **name
)
453 TRACE("%p %s\n", this, *name
);
454 MSVCP_logic_error_ctor(this, name
);
455 this->e
.vtable
= &MSVCP_invalid_argument_vtable
;
459 DEFINE_THISCALL_WRAPPER(MSVCP_invalid_argument_copy_ctor
, 8)
460 invalid_argument
* __thiscall
MSVCP_invalid_argument_copy_ctor(
461 invalid_argument
*this, invalid_argument
*rhs
)
463 TRACE("%p %p\n", this, rhs
);
464 MSVCP_logic_error_copy_ctor(this, rhs
);
465 this->e
.vtable
= &MSVCP_invalid_argument_vtable
;
469 DEFINE_THISCALL_WRAPPER(MSVCP_invalid_argument_vector_dtor
, 8)
470 void* __thiscall
MSVCP_invalid_argument_vector_dtor(
471 invalid_argument
*this, unsigned int flags
)
473 TRACE("%p %x\n", this, flags
);
474 return MSVCP_logic_error_vector_dtor(this, flags
);
477 DEFINE_RTTI_DATA(invalid_argument
, 0, 2, &logic_error_rtti_base_descriptor
, &exception_rtti_base_descriptor
, NULL
, ".?AVinvalid_argument@std@@");
479 static const cxx_type_info invalid_argument_cxx_type_info
= {
481 &invalid_argument_type_info
,
483 sizeof(invalid_argument
),
484 (cxx_copy_ctor
)THISCALL(MSVCP_invalid_argument_copy_ctor
)
487 static const cxx_type_info_table invalid_argument_cxx_type_table
= {
490 &invalid_argument_cxx_type_info
,
491 &logic_error_cxx_type_info
,
492 &exception_cxx_type_info
496 static const cxx_exception_type invalid_argument_cxx_type
= {
498 (cxx_copy_ctor
)THISCALL(MSVCP_logic_error_dtor
),
500 &invalid_argument_cxx_type_table
503 /* runtime_error class data */
506 basic_string_char str
;
509 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_ctor
, 8)
510 runtime_error
* __thiscall
MSVCP_runtime_error_ctor(
511 runtime_error
*this, const char **name
)
513 TRACE("%p %s\n", this, *name
);
514 this->e
.vtable
= &MSVCP_runtime_error_vtable
;
516 this->e
.do_free
= FALSE
;
517 MSVCP_basic_string_char_ctor_cstr(&this->str
, *name
);
521 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_copy_ctor
, 8)
522 runtime_error
* __thiscall
MSVCP_runtime_error_copy_ctor(
523 runtime_error
*this, runtime_error
*rhs
)
525 TRACE("%p %p\n", this, rhs
);
526 MSVCP_exception_copy_ctor(&this->e
, &rhs
->e
);
527 MSVCP_basic_string_char_copy_ctor(&this->str
, &rhs
->str
);
528 this->e
.vtable
= &MSVCP_runtime_error_vtable
;
532 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_dtor
, 4)
533 void __thiscall
MSVCP_runtime_error_dtor(runtime_error
*this)
536 MSVCP_exception_dtor(&this->e
);
537 MSVCP_basic_string_char_dtor(&this->str
);
540 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_vector_dtor
, 8)
541 void* __thiscall
MSVCP_runtime_error_vector_dtor(
542 runtime_error
*this, unsigned int flags
)
544 TRACE("%p %x\n", this, flags
);
546 /* we have an array, with the number of elements stored before the first object */
547 int i
, *ptr
= (int *)this-1;
549 for(i
=*ptr
-1; i
>=0; i
--)
550 MSVCP_runtime_error_dtor(this+i
);
551 MSVCRT_operator_delete(ptr
);
553 MSVCP_runtime_error_dtor(this);
555 MSVCRT_operator_delete(this);
561 DEFINE_RTTI_DATA(runtime_error
, 0, 1, &exception_rtti_base_descriptor
, NULL
, NULL
, ".?AVruntime_error@std@@");
563 static const cxx_type_info runtime_error_cxx_type_info
= {
565 &runtime_error_type_info
,
567 sizeof(runtime_error
),
568 (cxx_copy_ctor
)THISCALL(MSVCP_runtime_error_copy_ctor
)
571 static const cxx_type_info_table runtime_error_cxx_type_table
= {
574 &runtime_error_cxx_type_info
,
575 &exception_cxx_type_info
,
580 static const cxx_exception_type runtime_error_cxx_type
= {
582 (cxx_copy_ctor
)THISCALL(MSVCP_runtime_error_dtor
),
584 &runtime_error_cxx_type_table
587 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_what
, 4)
588 const char* __thiscall
MSVCP_runtime_error_what(runtime_error
*this)
591 return MSVCP_basic_string_char_c_str(&this->str
);
595 void __asm_dummy_vtables(void) {
597 __ASM_VTABLE(exception
, VTABLE_ADD_FUNC(MSVCP_what_exception
));
598 __ASM_VTABLE(bad_alloc
, VTABLE_ADD_FUNC(MSVCP_what_exception
));
599 __ASM_VTABLE(logic_error
, VTABLE_ADD_FUNC(MSVCP_logic_error_what
));
600 __ASM_VTABLE(length_error
, VTABLE_ADD_FUNC(MSVCP_logic_error_what
));
601 __ASM_VTABLE(out_of_range
, VTABLE_ADD_FUNC(MSVCP_logic_error_what
));
602 __ASM_VTABLE(invalid_argument
, VTABLE_ADD_FUNC(MSVCP_logic_error_what
));
603 __ASM_VTABLE(runtime_error
, VTABLE_ADD_FUNC(MSVCP_runtime_error_what
));
608 /* Internal: throws selected exception */
609 void throw_exception(exception_type et
, const char *str
)
611 const char *addr
= str
;
616 MSVCP_exception_ctor(&e
, &addr
);
617 _CxxThrowException(&e
, &exception_cxx_type
);
619 case EXCEPTION_BAD_ALLOC
: {
621 MSVCP_bad_alloc_ctor(&e
, &addr
);
622 _CxxThrowException(&e
, &bad_alloc_cxx_type
);
624 case EXCEPTION_LOGIC_ERROR
: {
626 MSVCP_logic_error_ctor(&e
, &addr
);
627 _CxxThrowException((exception
*)&e
, &logic_error_cxx_type
);
629 case EXCEPTION_LENGTH_ERROR
: {
631 MSVCP_length_error_ctor(&e
, &addr
);
632 _CxxThrowException((exception
*)&e
, &length_error_cxx_type
);
634 case EXCEPTION_OUT_OF_RANGE
: {
636 MSVCP_out_of_range_ctor(&e
, &addr
);
637 _CxxThrowException((exception
*)&e
, &out_of_range_cxx_type
);
639 case EXCEPTION_INVALID_ARGUMENT
: {
641 MSVCP_invalid_argument_ctor(&e
, &addr
);
642 _CxxThrowException((exception
*)&e
, &invalid_argument_cxx_type
);
644 case EXCEPTION_RUNTIME_ERROR
: {
646 MSVCP_runtime_error_ctor(&e
, &addr
);
647 _CxxThrowException((exception
*)&e
, &runtime_error_cxx_type
);