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(msvcp
);
30 #define CLASS_IS_SIMPLE_TYPE 1
31 #define CLASS_HAS_VIRTUAL_BASE_CLASS 4
33 void WINAPI
_CxxThrowException(exception
*,const cxx_exception_type
*);
36 extern const vtable_ptr MSVCP_exception_vtable
;
37 extern const vtable_ptr MSVCP_bad_alloc_vtable
;
38 extern const vtable_ptr MSVCP_logic_error_vtable
;
39 extern const vtable_ptr MSVCP_length_error_vtable
;
40 extern const vtable_ptr MSVCP_out_of_range_vtable
;
41 extern const vtable_ptr MSVCP_invalid_argument_vtable
;
42 extern const vtable_ptr MSVCP_runtime_error_vtable
;
43 extern const vtable_ptr MSVCP_failure_vtable
;
45 static void MSVCP_type_info_dtor(type_info
* _this
)
51 DEFINE_THISCALL_WRAPPER(MSVCP_type_info_vector_dtor
,8)
52 void * __thiscall
MSVCP_type_info_vector_dtor(type_info
* _this
, unsigned int flags
)
54 TRACE("(%p %x)\n", _this
, flags
);
57 /* we have an array, with the number of elements stored before the first object */
58 INT_PTR i
, *ptr
= (INT_PTR
*)_this
- 1;
60 for (i
= *ptr
- 1; i
>= 0; i
--) MSVCP_type_info_dtor(_this
+ i
);
61 MSVCRT_operator_delete(ptr
);
65 MSVCP_type_info_dtor(_this
);
66 if (flags
& 1) MSVCRT_operator_delete(_this
);
71 DEFINE_RTTI_DATA0( type_info
, 0, ".?AVtype_info@@" )
73 static exception
* 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_PTR i
, *ptr
= (INT_PTR
*)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_DATA0(exception
, 0, ".?AVexception@std@@")
135 DEFINE_CXX_DATA0(exception
, MSVCP_exception_dtor
)
137 /* bad_alloc class data */
138 typedef exception bad_alloc
;
140 static bad_alloc
* MSVCP_bad_alloc_ctor(bad_alloc
*this, const char **name
)
142 TRACE("%p %s\n", this, *name
);
143 MSVCP_exception_ctor(this, name
);
144 this->vtable
= &MSVCP_bad_alloc_vtable
;
148 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_copy_ctor
, 8)
149 bad_alloc
* __thiscall
MSVCP_bad_alloc_copy_ctor(bad_alloc
*this, const bad_alloc
*rhs
)
151 TRACE("%p %p\n", this, rhs
);
152 MSVCP_exception_copy_ctor(this, rhs
);
153 this->vtable
= &MSVCP_bad_alloc_vtable
;
157 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_dtor
, 4)
158 void __thiscall
MSVCP_bad_alloc_dtor(bad_alloc
*this)
161 MSVCP_exception_dtor(this);
164 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_vector_dtor
, 8)
165 void * __thiscall
MSVCP_bad_alloc_vector_dtor(bad_alloc
*this, unsigned int flags
)
167 TRACE("%p %x\n", this, flags
);
169 /* we have an array, with the number of elements stored before the first object */
170 INT_PTR i
, *ptr
= (INT_PTR
*)this-1;
172 for(i
=*ptr
-1; i
>=0; i
--)
173 MSVCP_bad_alloc_dtor(this+i
);
174 MSVCRT_operator_delete(ptr
);
176 MSVCP_bad_alloc_dtor(this);
178 MSVCRT_operator_delete(this);
184 DEFINE_THISCALL_WRAPPER(MSVCP_what_exception
,4)
185 const char* __thiscall
MSVCP_what_exception(exception
* this)
187 TRACE("(%p) returning %s\n", this, this->name
);
188 return this->name
? this->name
: "Unknown exception";
191 DEFINE_RTTI_DATA1(bad_alloc
, 0, &exception_rtti_base_descriptor
, ".?AVbad_alloc@std@@")
192 DEFINE_CXX_DATA1(bad_alloc
, &exception_cxx_type_info
, MSVCP_bad_alloc_dtor
)
194 /* logic_error class data */
195 typedef struct _logic_error
{
197 basic_string_char str
;
200 static logic_error
* MSVCP_logic_error_ctor(
201 logic_error
*this, const char **name
)
203 TRACE("%p %s\n", this, *name
);
204 this->e
.vtable
= &MSVCP_logic_error_vtable
;
206 this->e
.do_free
= FALSE
;
207 MSVCP_basic_string_char_ctor_cstr(&this->str
, *name
);
211 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_copy_ctor
, 8)
212 logic_error
* __thiscall
MSVCP_logic_error_copy_ctor(
213 logic_error
*this, logic_error
*rhs
)
215 TRACE("%p %p\n", this, rhs
);
216 MSVCP_exception_copy_ctor(&this->e
, &rhs
->e
);
217 MSVCP_basic_string_char_copy_ctor(&this->str
, &rhs
->str
);
218 this->e
.vtable
= &MSVCP_logic_error_vtable
;
222 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_dtor
, 4)
223 void __thiscall
MSVCP_logic_error_dtor(logic_error
*this)
226 MSVCP_exception_dtor(&this->e
);
227 MSVCP_basic_string_char_dtor(&this->str
);
230 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_vector_dtor
, 8)
231 void* __thiscall
MSVCP_logic_error_vector_dtor(
232 logic_error
*this, unsigned int flags
)
234 TRACE("%p %x\n", this, flags
);
236 /* we have an array, with the number of elements stored before the first object */
237 INT_PTR i
, *ptr
= (INT_PTR
*)this-1;
239 for(i
=*ptr
-1; i
>=0; i
--)
240 MSVCP_logic_error_dtor(this+i
);
241 MSVCRT_operator_delete(ptr
);
243 MSVCP_logic_error_dtor(this);
245 MSVCRT_operator_delete(this);
251 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_what
, 4)
252 const char* __thiscall
MSVCP_logic_error_what(logic_error
*this)
255 return MSVCP_basic_string_char_c_str(&this->str
);
258 DEFINE_RTTI_DATA1(logic_error
, 0, &exception_rtti_base_descriptor
, ".?AVlogic_error@std@@")
259 DEFINE_CXX_DATA1(logic_error
, &exception_cxx_type_info
, MSVCP_logic_error_dtor
)
261 /* length_error class data */
262 typedef logic_error length_error
;
264 static length_error
* MSVCP_length_error_ctor(
265 length_error
*this, const char **name
)
267 TRACE("%p %s\n", this, *name
);
268 MSVCP_logic_error_ctor(this, name
);
269 this->e
.vtable
= &MSVCP_length_error_vtable
;
273 DEFINE_THISCALL_WRAPPER(MSVCP_length_error_copy_ctor
, 8)
274 length_error
* __thiscall
MSVCP_length_error_copy_ctor(
275 length_error
*this, length_error
*rhs
)
277 TRACE("%p %p\n", this, rhs
);
278 MSVCP_logic_error_copy_ctor(this, rhs
);
279 this->e
.vtable
= &MSVCP_length_error_vtable
;
283 DEFINE_RTTI_DATA2(length_error
, 0, &logic_error_rtti_base_descriptor
, &exception_rtti_base_descriptor
, ".?AVlength_error@std@@")
284 DEFINE_CXX_DATA2(length_error
, &logic_error_cxx_type_info
, &exception_cxx_type_info
, MSVCP_logic_error_dtor
)
286 /* out_of_range class data */
287 typedef logic_error out_of_range
;
289 static out_of_range
* MSVCP_out_of_range_ctor(
290 out_of_range
*this, const char **name
)
292 TRACE("%p %s\n", this, *name
);
293 MSVCP_logic_error_ctor(this, name
);
294 this->e
.vtable
= &MSVCP_out_of_range_vtable
;
298 DEFINE_THISCALL_WRAPPER(MSVCP_out_of_range_copy_ctor
, 8)
299 out_of_range
* __thiscall
MSVCP_out_of_range_copy_ctor(
300 out_of_range
*this, out_of_range
*rhs
)
302 TRACE("%p %p\n", this, rhs
);
303 MSVCP_logic_error_copy_ctor(this, rhs
);
304 this->e
.vtable
= &MSVCP_out_of_range_vtable
;
308 DEFINE_RTTI_DATA2(out_of_range
, 0, &logic_error_rtti_base_descriptor
, &exception_rtti_base_descriptor
, ".?AVout_of_range@std@@")
309 DEFINE_CXX_DATA2(out_of_range
, &logic_error_cxx_type_info
, &exception_cxx_type_info
, MSVCP_logic_error_dtor
)
311 /* invalid_argument class data */
312 typedef logic_error invalid_argument
;
314 static invalid_argument
* MSVCP_invalid_argument_ctor(
315 invalid_argument
*this, const char **name
)
317 TRACE("%p %s\n", this, *name
);
318 MSVCP_logic_error_ctor(this, name
);
319 this->e
.vtable
= &MSVCP_invalid_argument_vtable
;
323 DEFINE_THISCALL_WRAPPER(MSVCP_invalid_argument_copy_ctor
, 8)
324 invalid_argument
* __thiscall
MSVCP_invalid_argument_copy_ctor(
325 invalid_argument
*this, invalid_argument
*rhs
)
327 TRACE("%p %p\n", this, rhs
);
328 MSVCP_logic_error_copy_ctor(this, rhs
);
329 this->e
.vtable
= &MSVCP_invalid_argument_vtable
;
333 DEFINE_RTTI_DATA2(invalid_argument
, 0, &logic_error_rtti_base_descriptor
, &exception_rtti_base_descriptor
, ".?AVinvalid_argument@std@@")
334 DEFINE_CXX_DATA2(invalid_argument
, &logic_error_cxx_type_info
, &exception_cxx_type_info
, MSVCP_logic_error_dtor
)
336 /* runtime_error class data */
339 basic_string_char str
;
342 static runtime_error
* MSVCP_runtime_error_ctor(
343 runtime_error
*this, const char **name
)
345 TRACE("%p %s\n", this, *name
);
346 this->e
.vtable
= &MSVCP_runtime_error_vtable
;
348 this->e
.do_free
= FALSE
;
349 MSVCP_basic_string_char_ctor_cstr(&this->str
, *name
);
353 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_copy_ctor
, 8)
354 runtime_error
* __thiscall
MSVCP_runtime_error_copy_ctor(
355 runtime_error
*this, runtime_error
*rhs
)
357 TRACE("%p %p\n", this, rhs
);
358 MSVCP_exception_copy_ctor(&this->e
, &rhs
->e
);
359 MSVCP_basic_string_char_copy_ctor(&this->str
, &rhs
->str
);
360 this->e
.vtable
= &MSVCP_runtime_error_vtable
;
364 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_dtor
, 4)
365 void __thiscall
MSVCP_runtime_error_dtor(runtime_error
*this)
368 MSVCP_exception_dtor(&this->e
);
369 MSVCP_basic_string_char_dtor(&this->str
);
372 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_vector_dtor
, 8)
373 void* __thiscall
MSVCP_runtime_error_vector_dtor(
374 runtime_error
*this, unsigned int flags
)
376 TRACE("%p %x\n", this, flags
);
378 /* we have an array, with the number of elements stored before the first object */
379 INT_PTR i
, *ptr
= (INT_PTR
*)this-1;
381 for(i
=*ptr
-1; i
>=0; i
--)
382 MSVCP_runtime_error_dtor(this+i
);
383 MSVCRT_operator_delete(ptr
);
385 MSVCP_runtime_error_dtor(this);
387 MSVCRT_operator_delete(this);
393 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_what
, 4)
394 const char* __thiscall
MSVCP_runtime_error_what(runtime_error
*this)
397 return MSVCP_basic_string_char_c_str(&this->str
);
400 DEFINE_RTTI_DATA1(runtime_error
, 0, &exception_rtti_base_descriptor
, ".?AVruntime_error@std@@")
401 DEFINE_CXX_DATA1(runtime_error
, &exception_cxx_type_info
, MSVCP_runtime_error_dtor
)
403 /* failure class data */
404 typedef runtime_error failure
;
406 static failure
* MSVCP_failure_ctor(
407 failure
*this, const char **name
)
409 TRACE("%p %s\n", this, *name
);
410 MSVCP_runtime_error_ctor(this, name
);
411 this->e
.vtable
= &MSVCP_failure_vtable
;
415 DEFINE_THISCALL_WRAPPER(MSVCP_failure_copy_ctor
, 8)
416 failure
* __thiscall
MSVCP_failure_copy_ctor(
417 failure
*this, failure
*rhs
)
419 TRACE("%p %p\n", this, rhs
);
420 MSVCP_runtime_error_copy_ctor(this, rhs
);
421 this->e
.vtable
= &MSVCP_failure_vtable
;
425 DEFINE_THISCALL_WRAPPER(MSVCP_failure_dtor
, 4)
426 void __thiscall
MSVCP_failure_dtor(failure
*this)
429 MSVCP_runtime_error_dtor(this);
432 DEFINE_THISCALL_WRAPPER(MSVCP_failure_vector_dtor
, 8)
433 void* __thiscall
MSVCP_failure_vector_dtor(
434 failure
*this, unsigned int flags
)
436 TRACE("%p %x\n", this, flags
);
437 return MSVCP_runtime_error_vector_dtor(this, flags
);
440 DEFINE_THISCALL_WRAPPER(MSVCP_failure_what
, 4)
441 const char* __thiscall
MSVCP_failure_what(failure
*this)
444 return MSVCP_runtime_error_what(this);
447 DEFINE_RTTI_DATA2(failure
, 0, &runtime_error_rtti_base_descriptor
, &exception_rtti_base_descriptor
, ".?AVfailure@std@@")
448 DEFINE_CXX_DATA2(failure
, &runtime_error_cxx_type_info
, &exception_cxx_type_info
, MSVCP_runtime_error_dtor
)
450 /* ?_Nomemory@std@@YAXXZ */
451 void __cdecl
_Nomemory(void)
454 throw_exception(EXCEPTION_BAD_ALLOC
, NULL
);
457 /* ?_Xmem@tr1@std@@YAXXZ */
458 void __cdecl
_Xmem(void)
461 throw_exception(EXCEPTION_BAD_ALLOC
, NULL
);
464 /* ?_Xinvalid_argument@std@@YAXPBD@Z */
465 /* ?_Xinvalid_argument@std@@YAXPEBD@Z */
466 void __cdecl
_Xinvalid_argument(const char *str
)
468 TRACE("(%s)\n", debugstr_a(str
));
469 throw_exception(EXCEPTION_INVALID_ARGUMENT
, str
);
472 /* ?_Xlength_error@std@@YAXPBD@Z */
473 /* ?_Xlength_error@std@@YAXPEBD@Z */
474 void __cdecl
_Xlength_error(const char *str
)
476 TRACE("(%s)\n", debugstr_a(str
));
477 throw_exception(EXCEPTION_LENGTH_ERROR
, str
);
480 /* ?_Xout_of_range@std@@YAXPBD@Z */
481 /* ?_Xout_of_range@std@@YAXPEBD@Z */
482 void __cdecl
_Xout_of_range(const char *str
)
484 TRACE("(%s)\n", debugstr_a(str
));
485 throw_exception(EXCEPTION_OUT_OF_RANGE
, str
);
488 /* ?_Xruntime_error@std@@YAXPBD@Z */
489 /* ?_Xruntime_error@std@@YAXPEBD@Z */
490 void __cdecl
_Xruntime_error(const char *str
)
492 TRACE("(%s)\n", debugstr_a(str
));
493 throw_exception(EXCEPTION_RUNTIME_ERROR
, str
);
497 void __asm_dummy_vtables(void) {
499 __ASM_VTABLE(type_info
,
500 VTABLE_ADD_FUNC(MSVCP_type_info_vector_dtor
));
501 __ASM_VTABLE(exception
,
502 VTABLE_ADD_FUNC(MSVCP_exception_vector_dtor
)
503 VTABLE_ADD_FUNC(MSVCP_what_exception
));
504 __ASM_VTABLE(bad_alloc
,
505 VTABLE_ADD_FUNC(MSVCP_bad_alloc_vector_dtor
)
506 VTABLE_ADD_FUNC(MSVCP_what_exception
));
507 __ASM_VTABLE(logic_error
,
508 VTABLE_ADD_FUNC(MSVCP_logic_error_vector_dtor
)
509 VTABLE_ADD_FUNC(MSVCP_logic_error_what
));
510 __ASM_VTABLE(length_error
,
511 VTABLE_ADD_FUNC(MSVCP_logic_error_vector_dtor
)
512 VTABLE_ADD_FUNC(MSVCP_logic_error_what
));
513 __ASM_VTABLE(out_of_range
,
514 VTABLE_ADD_FUNC(MSVCP_logic_error_vector_dtor
)
515 VTABLE_ADD_FUNC(MSVCP_logic_error_what
));
516 __ASM_VTABLE(invalid_argument
,
517 VTABLE_ADD_FUNC(MSVCP_logic_error_vector_dtor
)
518 VTABLE_ADD_FUNC(MSVCP_logic_error_what
));
519 __ASM_VTABLE(runtime_error
,
520 VTABLE_ADD_FUNC(MSVCP_runtime_error_vector_dtor
)
521 VTABLE_ADD_FUNC(MSVCP_runtime_error_what
));
522 __ASM_VTABLE(failure
,
523 VTABLE_ADD_FUNC(MSVCP_failure_vector_dtor
)
524 VTABLE_ADD_FUNC(MSVCP_failure_what
));
529 /* Internal: throws selected exception */
530 void throw_exception(exception_type et
, const char *str
)
532 const char *addr
= str
;
535 case EXCEPTION_RERAISE
:
536 _CxxThrowException(NULL
, NULL
);
539 MSVCP_exception_ctor(&e
, &addr
);
540 _CxxThrowException(&e
, &exception_cxx_type
);
542 case EXCEPTION_BAD_ALLOC
: {
544 MSVCP_bad_alloc_ctor(&e
, &addr
);
545 _CxxThrowException(&e
, &bad_alloc_cxx_type
);
547 case EXCEPTION_LOGIC_ERROR
: {
549 MSVCP_logic_error_ctor(&e
, &addr
);
550 _CxxThrowException((exception
*)&e
, &logic_error_cxx_type
);
552 case EXCEPTION_LENGTH_ERROR
: {
554 MSVCP_length_error_ctor(&e
, &addr
);
555 _CxxThrowException((exception
*)&e
, &length_error_cxx_type
);
557 case EXCEPTION_OUT_OF_RANGE
: {
559 MSVCP_out_of_range_ctor(&e
, &addr
);
560 _CxxThrowException((exception
*)&e
, &out_of_range_cxx_type
);
562 case EXCEPTION_INVALID_ARGUMENT
: {
564 MSVCP_invalid_argument_ctor(&e
, &addr
);
565 _CxxThrowException((exception
*)&e
, &invalid_argument_cxx_type
);
567 case EXCEPTION_RUNTIME_ERROR
: {
569 MSVCP_runtime_error_ctor(&e
, &addr
);
570 _CxxThrowException((exception
*)&e
, &runtime_error_cxx_type
);
572 case EXCEPTION_FAILURE
: {
574 MSVCP_failure_ctor(&e
, &addr
);
575 _CxxThrowException((exception
*)&e
, &failure_cxx_type
);
580 void init_exception(void *base
)
583 init_type_info_rtti(base
);
584 init_exception_rtti(base
);
585 init_bad_alloc_rtti(base
);
586 init_logic_error_rtti(base
);
587 init_length_error_rtti(base
);
588 init_out_of_range_rtti(base
);
589 init_invalid_argument_rtti(base
);
590 init_runtime_error_rtti(base
);
591 init_failure_rtti(base
);
593 init_exception_cxx(base
);
594 init_bad_alloc_cxx(base
);
595 init_logic_error_cxx(base
);
596 init_length_error_cxx(base
);
597 init_out_of_range_cxx(base
);
598 init_invalid_argument_cxx(base
);
599 init_runtime_error_cxx(base
);
600 init_failure_cxx(base
);