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_bad_alloc_vtable
;
65 extern const vtable_ptr MSVCP_logic_error_vtable
;
66 extern const vtable_ptr MSVCP_length_error_vtable
;
67 extern const vtable_ptr MSVCP_out_of_range_vtable
;
68 extern const vtable_ptr MSVCP_invalid_argument_vtable
;
69 extern const vtable_ptr MSVCP_runtime_error_vtable
;
71 /* exception class data */
72 static type_info exception_type_info
= {
73 NULL
, /* set by set_exception_vtable */
78 DEFINE_THISCALL_WRAPPER(MSVCP_exception_ctor
, 8)
79 exception
* __thiscall
MSVCP_exception_ctor(exception
*this, const char **name
)
81 TRACE("(%p %s)\n", this, *name
);
83 this->vtable
= exception_type_info
.vtable
;
85 unsigned int name_len
= strlen(*name
) + 1;
86 this->name
= malloc(name_len
);
87 memcpy(this->name
, *name
, name_len
);
91 this->do_free
= FALSE
;
96 DEFINE_THISCALL_WRAPPER(MSVCP_exception_copy_ctor
,8)
97 exception
* __thiscall
MSVCP_exception_copy_ctor(exception
*this, const exception
*rhs
)
99 TRACE("(%p,%p)\n", this, rhs
);
102 this->vtable
= exception_type_info
.vtable
;
103 this->name
= rhs
->name
;
104 this->do_free
= FALSE
;
106 MSVCP_exception_ctor(this, (const char**)&rhs
->name
);
107 TRACE("name = %s\n", this->name
);
111 DEFINE_THISCALL_WRAPPER(MSVCP_exception_dtor
,4)
112 void __thiscall
MSVCP_exception_dtor(exception
*this)
114 TRACE("(%p)\n", this);
115 this->vtable
= exception_type_info
.vtable
;
120 static const rtti_base_descriptor exception_rtti_base_descriptor
= {
121 &exception_type_info
,
127 static const cxx_type_info exception_cxx_type_info
= {
129 &exception_type_info
,
132 (cxx_copy_ctor
)THISCALL(MSVCP_exception_dtor
)
135 static const cxx_type_info_table exception_cxx_type_table
= {
138 &exception_cxx_type_info
,
144 static const cxx_exception_type exception_cxx_type
= {
146 (cxx_copy_ctor
)THISCALL(MSVCP_exception_copy_ctor
),
148 &exception_cxx_type_table
151 void set_exception_vtable(void)
153 HMODULE hmod
= GetModuleHandleA("msvcrt.dll");
154 exception_type_info
.vtable
= (void*)GetProcAddress(hmod
, "??_7exception@@6B@");
157 /* bad_alloc class data */
158 typedef exception bad_alloc
;
160 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_ctor
, 8)
161 bad_alloc
* __thiscall
MSVCP_bad_alloc_ctor(bad_alloc
*this, const char **name
)
163 TRACE("%p %s\n", this, *name
);
164 MSVCP_exception_ctor(this, name
);
165 this->vtable
= &MSVCP_bad_alloc_vtable
;
169 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_copy_ctor
, 8)
170 bad_alloc
* __thiscall
MSVCP_bad_alloc_copy_ctor(bad_alloc
*this, const bad_alloc
*rhs
)
172 TRACE("%p %p\n", this, rhs
);
173 MSVCP_exception_copy_ctor(this, rhs
);
174 this->vtable
= &MSVCP_bad_alloc_vtable
;
178 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_dtor
, 4)
179 void __thiscall
MSVCP_bad_alloc_dtor(bad_alloc
*this)
182 MSVCP_exception_dtor(this);
185 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_vector_dtor
, 8)
186 void * __thiscall
MSVCP_bad_alloc_vector_dtor(bad_alloc
*this, unsigned int flags
)
188 TRACE("%p %x\n", this, flags
);
190 /* we have an array, with the number of elements stored before the first object */
191 int i
, *ptr
= (int *)this-1;
193 for(i
=*ptr
-1; i
>=0; i
--)
194 MSVCP_bad_alloc_dtor(this+i
);
195 MSVCRT_operator_delete(ptr
);
197 MSVCP_bad_alloc_dtor(this);
199 MSVCRT_operator_delete(this);
205 DEFINE_THISCALL_WRAPPER(MSVCP_what_exception
,4)
206 const char* __thiscall
MSVCP_what_exception(exception
* this)
208 TRACE("(%p) returning %s\n", this, this->name
);
209 return this->name
? this->name
: "Unknown exception";
212 DEFINE_RTTI_DATA(bad_alloc
, 0, 1, &exception_rtti_base_descriptor
, NULL
, NULL
, ".?AVbad_alloc@std@@");
214 static const cxx_type_info bad_alloc_cxx_type_info
= {
216 &bad_alloc_type_info
,
219 (cxx_copy_ctor
)THISCALL(MSVCP_bad_alloc_copy_ctor
)
222 static const cxx_type_info_table bad_alloc_cxx_type_table
= {
225 &bad_alloc_cxx_type_info
,
226 &exception_cxx_type_info
,
231 static const cxx_exception_type bad_alloc_cxx_type
= {
233 (cxx_copy_ctor
)THISCALL(MSVCP_bad_alloc_dtor
),
235 &bad_alloc_cxx_type_table
238 /* logic_error class data */
239 typedef struct _logic_error
{
241 basic_string_char str
;
244 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_ctor
, 8)
245 logic_error
* __thiscall
MSVCP_logic_error_ctor(
246 logic_error
*this, const char **name
)
248 TRACE("%p %s\n", this, *name
);
249 this->e
.vtable
= &MSVCP_logic_error_vtable
;
251 this->e
.do_free
= FALSE
;
252 MSVCP_basic_string_char_ctor_cstr(&this->str
, *name
);
256 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_copy_ctor
, 8)
257 logic_error
* __thiscall
MSVCP_logic_error_copy_ctor(
258 logic_error
*this, logic_error
*rhs
)
260 TRACE("%p %p\n", this, rhs
);
261 MSVCP_exception_copy_ctor(&this->e
, &rhs
->e
);
262 MSVCP_basic_string_char_copy_ctor(&this->str
, &rhs
->str
);
263 this->e
.vtable
= &MSVCP_logic_error_vtable
;
267 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_dtor
, 4)
268 void __thiscall
MSVCP_logic_error_dtor(logic_error
*this)
271 MSVCP_exception_dtor(&this->e
);
272 MSVCP_basic_string_char_dtor(&this->str
);
275 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_vector_dtor
, 8)
276 void* __thiscall
MSVCP_logic_error_vector_dtor(
277 logic_error
*this, unsigned int flags
)
279 TRACE("%p %x\n", this, flags
);
281 /* we have an array, with the number of elements stored before the first object */
282 int i
, *ptr
= (int *)this-1;
284 for(i
=*ptr
-1; i
>=0; i
--)
285 MSVCP_logic_error_dtor(this+i
);
286 MSVCRT_operator_delete(ptr
);
288 MSVCP_logic_error_dtor(this);
290 MSVCRT_operator_delete(this);
296 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_what
, 4)
297 const char* __thiscall
MSVCP_logic_error_what(logic_error
*this)
300 return MSVCP_basic_string_char_c_str(&this->str
);
303 DEFINE_RTTI_DATA(logic_error
, 0, 1, &exception_rtti_base_descriptor
, NULL
, NULL
, ".?AVlogic_error@std@@");
305 static const cxx_type_info logic_error_cxx_type_info
= {
307 &logic_error_type_info
,
310 (cxx_copy_ctor
)THISCALL(MSVCP_logic_error_copy_ctor
)
313 static const cxx_type_info_table logic_error_cxx_type_table
= {
316 &logic_error_cxx_type_info
,
317 &exception_cxx_type_info
,
322 static const cxx_exception_type logic_error_cxx_type
= {
324 (cxx_copy_ctor
)THISCALL(MSVCP_logic_error_dtor
),
326 &logic_error_cxx_type_table
329 /* length_error class data */
330 typedef logic_error length_error
;
332 DEFINE_THISCALL_WRAPPER(MSVCP_length_error_ctor
, 8)
333 length_error
* __thiscall
MSVCP_length_error_ctor(
334 length_error
*this, const char **name
)
336 TRACE("%p %s\n", this, *name
);
337 MSVCP_logic_error_ctor(this, name
);
338 this->e
.vtable
= &MSVCP_length_error_vtable
;
342 DEFINE_THISCALL_WRAPPER(MSVCP_length_error_copy_ctor
, 8)
343 length_error
* __thiscall
MSVCP_length_error_copy_ctor(
344 length_error
*this, length_error
*rhs
)
346 TRACE("%p %p\n", this, rhs
);
347 MSVCP_logic_error_copy_ctor(this, rhs
);
348 this->e
.vtable
= &MSVCP_length_error_vtable
;
352 DEFINE_THISCALL_WRAPPER(MSVCP_length_error_vector_dtor
, 8)
353 void* __thiscall
MSVCP_length_error_vector_dtor(
354 length_error
*this, unsigned int flags
)
356 TRACE("%p %x\n", this, flags
);
357 return MSVCP_logic_error_vector_dtor(this, flags
);
360 DEFINE_RTTI_DATA(length_error
, 0, 2, &logic_error_rtti_base_descriptor
, &exception_rtti_base_descriptor
, NULL
, ".?AVlength_error@std@@");
362 static const cxx_type_info length_error_cxx_type_info
= {
364 &length_error_type_info
,
366 sizeof(length_error
),
367 (cxx_copy_ctor
)THISCALL(MSVCP_length_error_copy_ctor
)
370 static const cxx_type_info_table length_error_cxx_type_table
= {
373 &length_error_cxx_type_info
,
374 &logic_error_cxx_type_info
,
375 &exception_cxx_type_info
379 static const cxx_exception_type length_error_cxx_type
= {
381 (cxx_copy_ctor
)THISCALL(MSVCP_logic_error_dtor
),
383 &length_error_cxx_type_table
386 /* out_of_range class data */
387 typedef logic_error out_of_range
;
389 DEFINE_THISCALL_WRAPPER(MSVCP_out_of_range_ctor
, 8)
390 out_of_range
* __thiscall
MSVCP_out_of_range_ctor(
391 out_of_range
*this, const char **name
)
393 TRACE("%p %s\n", this, *name
);
394 MSVCP_logic_error_ctor(this, name
);
395 this->e
.vtable
= &MSVCP_out_of_range_vtable
;
399 DEFINE_THISCALL_WRAPPER(MSVCP_out_of_range_copy_ctor
, 8)
400 out_of_range
* __thiscall
MSVCP_out_of_range_copy_ctor(
401 out_of_range
*this, out_of_range
*rhs
)
403 TRACE("%p %p\n", this, rhs
);
404 MSVCP_logic_error_copy_ctor(this, rhs
);
405 this->e
.vtable
= &MSVCP_out_of_range_vtable
;
409 DEFINE_THISCALL_WRAPPER(MSVCP_out_of_range_vector_dtor
, 8)
410 void* __thiscall
MSVCP_out_of_range_vector_dtor(
411 out_of_range
*this, unsigned int flags
)
413 TRACE("%p %x\n", this, flags
);
414 return MSVCP_logic_error_vector_dtor(this, flags
);
417 DEFINE_RTTI_DATA(out_of_range
, 0, 2, &logic_error_rtti_base_descriptor
, &exception_rtti_base_descriptor
, NULL
, ".?AVout_of_range@std@@");
419 static const cxx_type_info out_of_range_cxx_type_info
= {
421 &out_of_range_type_info
,
423 sizeof(out_of_range
),
424 (cxx_copy_ctor
)THISCALL(MSVCP_out_of_range_copy_ctor
)
427 static const cxx_type_info_table out_of_range_cxx_type_table
= {
430 &out_of_range_cxx_type_info
,
431 &logic_error_cxx_type_info
,
432 &exception_cxx_type_info
436 static const cxx_exception_type out_of_range_cxx_type
= {
438 (cxx_copy_ctor
)THISCALL(MSVCP_logic_error_dtor
),
440 &out_of_range_cxx_type_table
443 /* invalid_argument class data */
444 typedef logic_error invalid_argument
;
446 DEFINE_THISCALL_WRAPPER(MSVCP_invalid_argument_ctor
, 8)
447 invalid_argument
* __thiscall
MSVCP_invalid_argument_ctor(
448 invalid_argument
*this, const char **name
)
450 TRACE("%p %s\n", this, *name
);
451 MSVCP_logic_error_ctor(this, name
);
452 this->e
.vtable
= &MSVCP_invalid_argument_vtable
;
456 DEFINE_THISCALL_WRAPPER(MSVCP_invalid_argument_copy_ctor
, 8)
457 invalid_argument
* __thiscall
MSVCP_invalid_argument_copy_ctor(
458 invalid_argument
*this, invalid_argument
*rhs
)
460 TRACE("%p %p\n", this, rhs
);
461 MSVCP_logic_error_copy_ctor(this, rhs
);
462 this->e
.vtable
= &MSVCP_invalid_argument_vtable
;
466 DEFINE_THISCALL_WRAPPER(MSVCP_invalid_argument_vector_dtor
, 8)
467 void* __thiscall
MSVCP_invalid_argument_vector_dtor(
468 invalid_argument
*this, unsigned int flags
)
470 TRACE("%p %x\n", this, flags
);
471 return MSVCP_logic_error_vector_dtor(this, flags
);
474 DEFINE_RTTI_DATA(invalid_argument
, 0, 2, &logic_error_rtti_base_descriptor
, &exception_rtti_base_descriptor
, NULL
, ".?AVinvalid_argument@std@@");
476 static const cxx_type_info invalid_argument_cxx_type_info
= {
478 &invalid_argument_type_info
,
480 sizeof(invalid_argument
),
481 (cxx_copy_ctor
)THISCALL(MSVCP_invalid_argument_copy_ctor
)
484 static const cxx_type_info_table invalid_argument_cxx_type_table
= {
487 &invalid_argument_cxx_type_info
,
488 &logic_error_cxx_type_info
,
489 &exception_cxx_type_info
493 static const cxx_exception_type invalid_argument_cxx_type
= {
495 (cxx_copy_ctor
)THISCALL(MSVCP_logic_error_dtor
),
497 &invalid_argument_cxx_type_table
500 /* runtime_error class data */
503 basic_string_char str
;
506 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_ctor
, 8)
507 runtime_error
* __thiscall
MSVCP_runtime_error_ctor(
508 runtime_error
*this, const char **name
)
510 TRACE("%p %s\n", this, *name
);
511 this->e
.vtable
= &MSVCP_runtime_error_vtable
;
513 this->e
.do_free
= FALSE
;
514 MSVCP_basic_string_char_ctor_cstr(&this->str
, *name
);
518 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_copy_ctor
, 8)
519 runtime_error
* __thiscall
MSVCP_runtime_error_copy_ctor(
520 runtime_error
*this, runtime_error
*rhs
)
522 TRACE("%p %p\n", this, rhs
);
523 MSVCP_exception_copy_ctor(&this->e
, &rhs
->e
);
524 MSVCP_basic_string_char_copy_ctor(&this->str
, &rhs
->str
);
525 this->e
.vtable
= &MSVCP_runtime_error_vtable
;
529 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_dtor
, 4)
530 void __thiscall
MSVCP_runtime_error_dtor(runtime_error
*this)
533 MSVCP_exception_dtor(&this->e
);
534 MSVCP_basic_string_char_dtor(&this->str
);
537 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_vector_dtor
, 8)
538 void* __thiscall
MSVCP_runtime_error_vector_dtor(
539 runtime_error
*this, unsigned int flags
)
541 TRACE("%p %x\n", this, flags
);
543 /* we have an array, with the number of elements stored before the first object */
544 int i
, *ptr
= (int *)this-1;
546 for(i
=*ptr
-1; i
>=0; i
--)
547 MSVCP_runtime_error_dtor(this+i
);
548 MSVCRT_operator_delete(ptr
);
550 MSVCP_runtime_error_dtor(this);
552 MSVCRT_operator_delete(this);
558 DEFINE_RTTI_DATA(runtime_error
, 0, 1, &exception_rtti_base_descriptor
, NULL
, NULL
, ".?AVruntime_error@std@@");
560 static const cxx_type_info runtime_error_cxx_type_info
= {
562 &runtime_error_type_info
,
564 sizeof(runtime_error
),
565 (cxx_copy_ctor
)THISCALL(MSVCP_runtime_error_copy_ctor
)
568 static const cxx_type_info_table runtime_error_cxx_type_table
= {
571 &runtime_error_cxx_type_info
,
572 &exception_cxx_type_info
,
577 static const cxx_exception_type runtime_error_cxx_type
= {
579 (cxx_copy_ctor
)THISCALL(MSVCP_runtime_error_dtor
),
581 &runtime_error_cxx_type_table
584 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_what
, 4)
585 const char* __thiscall
MSVCP_runtime_error_what(runtime_error
*this)
588 return MSVCP_basic_string_char_c_str(&this->str
);
592 void __asm_dummy_vtables(void) {
594 __ASM_VTABLE(bad_alloc
, VTABLE_ADD_FUNC(MSVCP_what_exception
));
595 __ASM_VTABLE(logic_error
, VTABLE_ADD_FUNC(MSVCP_logic_error_what
));
596 __ASM_VTABLE(length_error
, VTABLE_ADD_FUNC(MSVCP_logic_error_what
));
597 __ASM_VTABLE(out_of_range
, VTABLE_ADD_FUNC(MSVCP_logic_error_what
));
598 __ASM_VTABLE(invalid_argument
, VTABLE_ADD_FUNC(MSVCP_logic_error_what
));
599 __ASM_VTABLE(runtime_error
, VTABLE_ADD_FUNC(MSVCP_runtime_error_what
));
604 /* Internal: throws selected exception */
605 void throw_exception(exception_type et
, const char *str
)
607 const char *addr
= str
;
612 MSVCP_exception_ctor(&e
, &addr
);
613 _CxxThrowException(&e
, &exception_cxx_type
);
615 case EXCEPTION_BAD_ALLOC
: {
617 MSVCP_bad_alloc_ctor(&e
, &addr
);
618 _CxxThrowException(&e
, &bad_alloc_cxx_type
);
620 case EXCEPTION_LOGIC_ERROR
: {
622 MSVCP_logic_error_ctor(&e
, &addr
);
623 _CxxThrowException((exception
*)&e
, &logic_error_cxx_type
);
625 case EXCEPTION_LENGTH_ERROR
: {
627 MSVCP_length_error_ctor(&e
, &addr
);
628 _CxxThrowException((exception
*)&e
, &length_error_cxx_type
);
630 case EXCEPTION_OUT_OF_RANGE
: {
632 MSVCP_out_of_range_ctor(&e
, &addr
);
633 _CxxThrowException((exception
*)&e
, &out_of_range_cxx_type
);
635 case EXCEPTION_INVALID_ARGUMENT
: {
637 MSVCP_invalid_argument_ctor(&e
, &addr
);
638 _CxxThrowException((exception
*)&e
, &invalid_argument_cxx_type
);
640 case EXCEPTION_RUNTIME_ERROR
: {
642 MSVCP_runtime_error_ctor(&e
, &addr
);
643 _CxxThrowException((exception
*)&e
, &runtime_error_cxx_type
);