msvcp: Sync fpos usage and istream<>::tellg.
[wine] / dlls / msvcp100 / exception.c
1 /*
2  * Copyright 2010 Piotr Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #include "msvcp.h"
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(msvcp);
29
30 #define CLASS_IS_SIMPLE_TYPE          1
31 #define CLASS_HAS_VIRTUAL_BASE_CLASS  4
32
33 void WINAPI _CxxThrowException(exception*,const cxx_exception_type*);
34
35 /* vtables */
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;
44
45 static void MSVCP_type_info_dtor(type_info * _this)
46 {
47     free(_this->name);
48 }
49
50 /* Unexported */
51 DEFINE_THISCALL_WRAPPER(MSVCP_type_info_vector_dtor,8)
52 void * __thiscall MSVCP_type_info_vector_dtor(type_info * _this, unsigned int flags)
53 {
54     TRACE("(%p %x)\n", _this, flags);
55     if (flags & 2)
56     {
57         /* we have an array, with the number of elements stored before the first object */
58         INT_PTR i, *ptr = (INT_PTR *)_this - 1;
59
60         for (i = *ptr - 1; i >= 0; i--) MSVCP_type_info_dtor(_this + i);
61         MSVCRT_operator_delete(ptr);
62     }
63     else
64     {
65         MSVCP_type_info_dtor(_this);
66         if (flags & 1) MSVCRT_operator_delete(_this);
67     }
68     return _this;
69 }
70
71 DEFINE_RTTI_DATA0( type_info, 0, ".?AVtype_info@@" );
72
73 static exception* MSVCP_exception_ctor(exception *this, const char **name)
74 {
75     TRACE("(%p %s)\n", this, *name);
76
77     this->vtable = &MSVCP_exception_vtable;
78     if(*name) {
79         unsigned int name_len = strlen(*name) + 1;
80         this->name = malloc(name_len);
81         memcpy(this->name, *name, name_len);
82         this->do_free = TRUE;
83     } else {
84         this->name = NULL;
85         this->do_free = FALSE;
86     }
87     return this;
88 }
89
90 DEFINE_THISCALL_WRAPPER(MSVCP_exception_copy_ctor,8)
91 exception* __thiscall MSVCP_exception_copy_ctor(exception *this, const exception *rhs)
92 {
93     TRACE("(%p,%p)\n", this, rhs);
94
95     if(!rhs->do_free) {
96         this->vtable = &MSVCP_exception_vtable;
97         this->name = rhs->name;
98         this->do_free = FALSE;
99     } else
100         MSVCP_exception_ctor(this, (const char**)&rhs->name);
101     TRACE("name = %s\n", this->name);
102     return this;
103 }
104
105 DEFINE_THISCALL_WRAPPER(MSVCP_exception_dtor,4)
106 void __thiscall MSVCP_exception_dtor(exception *this)
107 {
108     TRACE("(%p)\n", this);
109     this->vtable = &MSVCP_exception_vtable;
110     if(this->do_free)
111         free(this->name);
112 }
113
114 DEFINE_THISCALL_WRAPPER(MSVCP_exception_vector_dtor, 8)
115 void * __thiscall MSVCP_exception_vector_dtor(exception *this, unsigned int flags)
116 {
117     TRACE("%p %x\n", this, flags);
118     if(flags & 2) {
119         /* we have an array, with the number of elements stored before the first object */
120         INT_PTR i, *ptr = (INT_PTR *)this-1;
121
122         for(i=*ptr-1; i>=0; i--)
123             MSVCP_exception_dtor(this+i);
124         MSVCRT_operator_delete(ptr);
125     } else {
126         MSVCP_exception_dtor(this);
127         if(flags & 1)
128             MSVCRT_operator_delete(this);
129     }
130
131     return this;
132 }
133
134 DEFINE_RTTI_DATA0(exception, 0, ".?AVexception@std@@");
135 DEFINE_CXX_DATA0(exception, MSVCP_exception_dtor);
136
137 /* bad_alloc class data */
138 typedef exception bad_alloc;
139
140 static bad_alloc* MSVCP_bad_alloc_ctor(bad_alloc *this, const char **name)
141 {
142     TRACE("%p %s\n", this, *name);
143     MSVCP_exception_ctor(this, name);
144     this->vtable = &MSVCP_bad_alloc_vtable;
145     return this;
146 }
147
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)
150 {
151     TRACE("%p %p\n", this, rhs);
152     MSVCP_exception_copy_ctor(this, rhs);
153     this->vtable = &MSVCP_bad_alloc_vtable;
154     return this;
155 }
156
157 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_dtor, 4)
158 void __thiscall MSVCP_bad_alloc_dtor(bad_alloc *this)
159 {
160     TRACE("%p\n", this);
161     MSVCP_exception_dtor(this);
162 }
163
164 DEFINE_THISCALL_WRAPPER(MSVCP_bad_alloc_vector_dtor, 8)
165 void * __thiscall MSVCP_bad_alloc_vector_dtor(bad_alloc *this, unsigned int flags)
166 {
167     TRACE("%p %x\n", this, flags);
168     if(flags & 2) {
169         /* we have an array, with the number of elements stored before the first object */
170         INT_PTR i, *ptr = (INT_PTR *)this-1;
171
172         for(i=*ptr-1; i>=0; i--)
173             MSVCP_bad_alloc_dtor(this+i);
174         MSVCRT_operator_delete(ptr);
175     } else {
176         MSVCP_bad_alloc_dtor(this);
177         if(flags & 1)
178             MSVCRT_operator_delete(this);
179     }
180
181     return this;
182 }
183
184 DEFINE_THISCALL_WRAPPER(MSVCP_what_exception,4)
185 const char* __thiscall MSVCP_what_exception(exception * this)
186 {
187     TRACE("(%p) returning %s\n", this, this->name);
188     return this->name ? this->name : "Unknown exception";
189 }
190
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);
193
194 /* logic_error class data */
195 typedef struct _logic_error {
196     exception e;
197     basic_string_char str;
198 } logic_error;
199
200 static logic_error* MSVCP_logic_error_ctor(
201         logic_error *this, const char **name)
202 {
203     TRACE("%p %s\n", this, *name);
204     this->e.vtable = &MSVCP_logic_error_vtable;
205     this->e.name = NULL;
206     this->e.do_free = FALSE;
207     MSVCP_basic_string_char_ctor_cstr(&this->str, *name);
208     return this;
209 }
210
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)
214 {
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;
219     return this;
220 }
221
222 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_dtor, 4)
223 void __thiscall MSVCP_logic_error_dtor(logic_error *this)
224 {
225     TRACE("%p\n", this);
226     MSVCP_exception_dtor(&this->e);
227     MSVCP_basic_string_char_dtor(&this->str);
228 }
229
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)
233 {
234     TRACE("%p %x\n", this, flags);
235     if(flags & 2) {
236         /* we have an array, with the number of elements stored before the first object */
237         INT_PTR i, *ptr = (INT_PTR *)this-1;
238
239         for(i=*ptr-1; i>=0; i--)
240             MSVCP_logic_error_dtor(this+i);
241         MSVCRT_operator_delete(ptr);
242     } else {
243         MSVCP_logic_error_dtor(this);
244         if(flags & 1)
245             MSVCRT_operator_delete(this);
246     }
247
248     return this;
249 }
250
251 DEFINE_THISCALL_WRAPPER(MSVCP_logic_error_what, 4)
252 const char* __thiscall MSVCP_logic_error_what(logic_error *this)
253 {
254     TRACE("%p\n", this);
255     return MSVCP_basic_string_char_c_str(&this->str);
256 }
257
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);
260
261 /* length_error class data */
262 typedef logic_error length_error;
263
264 static length_error* MSVCP_length_error_ctor(
265         length_error *this, const char **name)
266 {
267     TRACE("%p %s\n", this, *name);
268     MSVCP_logic_error_ctor(this, name);
269     this->e.vtable = &MSVCP_length_error_vtable;
270     return this;
271 }
272
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)
276 {
277     TRACE("%p %p\n", this, rhs);
278     MSVCP_logic_error_copy_ctor(this, rhs);
279     this->e.vtable = &MSVCP_length_error_vtable;
280     return this;
281 }
282
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);
285
286 /* out_of_range class data */
287 typedef logic_error out_of_range;
288
289 static out_of_range* MSVCP_out_of_range_ctor(
290         out_of_range *this, const char **name)
291 {
292     TRACE("%p %s\n", this, *name);
293     MSVCP_logic_error_ctor(this, name);
294     this->e.vtable = &MSVCP_out_of_range_vtable;
295     return this;
296 }
297
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)
301 {
302     TRACE("%p %p\n", this, rhs);
303     MSVCP_logic_error_copy_ctor(this, rhs);
304     this->e.vtable = &MSVCP_out_of_range_vtable;
305     return this;
306 }
307
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);
310
311 /* invalid_argument class data */
312 typedef logic_error invalid_argument;
313
314 static invalid_argument* MSVCP_invalid_argument_ctor(
315         invalid_argument *this, const char **name)
316 {
317     TRACE("%p %s\n", this, *name);
318     MSVCP_logic_error_ctor(this, name);
319     this->e.vtable = &MSVCP_invalid_argument_vtable;
320     return this;
321 }
322
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)
326 {
327     TRACE("%p %p\n", this, rhs);
328     MSVCP_logic_error_copy_ctor(this, rhs);
329     this->e.vtable = &MSVCP_invalid_argument_vtable;
330     return this;
331 }
332
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);
335
336 /* runtime_error class data */
337 typedef struct {
338     exception e;
339     basic_string_char str;
340 } runtime_error;
341
342 static runtime_error* MSVCP_runtime_error_ctor(
343         runtime_error *this, const char **name)
344 {
345     TRACE("%p %s\n", this, *name);
346     this->e.vtable = &MSVCP_runtime_error_vtable;
347     this->e.name = NULL;
348     this->e.do_free = FALSE;
349     MSVCP_basic_string_char_ctor_cstr(&this->str, *name);
350     return this;
351 }
352
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)
356 {
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;
361     return this;
362 }
363
364 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_dtor, 4)
365 void __thiscall MSVCP_runtime_error_dtor(runtime_error *this)
366 {
367     TRACE("%p\n", this);
368     MSVCP_exception_dtor(&this->e);
369     MSVCP_basic_string_char_dtor(&this->str);
370 }
371
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)
375 {
376     TRACE("%p %x\n", this, flags);
377     if(flags & 2) {
378         /* we have an array, with the number of elements stored before the first object */
379         INT_PTR i, *ptr = (INT_PTR *)this-1;
380
381         for(i=*ptr-1; i>=0; i--)
382             MSVCP_runtime_error_dtor(this+i);
383         MSVCRT_operator_delete(ptr);
384     } else {
385         MSVCP_runtime_error_dtor(this);
386         if(flags & 1)
387             MSVCRT_operator_delete(this);
388     }
389
390     return this;
391 }
392
393 DEFINE_THISCALL_WRAPPER(MSVCP_runtime_error_what, 4)
394 const char* __thiscall MSVCP_runtime_error_what(runtime_error *this)
395 {
396     TRACE("%p\n", this);
397     return MSVCP_basic_string_char_c_str(&this->str);
398 }
399
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);
402
403 /* failure class data */
404 typedef runtime_error failure;
405
406 static failure* MSVCP_failure_ctor(
407         failure *this, const char **name)
408 {
409     TRACE("%p %s\n", this, *name);
410     MSVCP_runtime_error_ctor(this, name);
411     this->e.vtable = &MSVCP_failure_vtable;
412     return this;
413 }
414
415 DEFINE_THISCALL_WRAPPER(MSVCP_failure_copy_ctor, 8)
416 failure* __thiscall MSVCP_failure_copy_ctor(
417         failure *this, failure *rhs)
418 {
419     TRACE("%p %p\n", this, rhs);
420     MSVCP_runtime_error_copy_ctor(this, rhs);
421     this->e.vtable = &MSVCP_failure_vtable;
422     return this;
423 }
424
425 DEFINE_THISCALL_WRAPPER(MSVCP_failure_dtor, 4)
426 void __thiscall MSVCP_failure_dtor(failure *this)
427 {
428     TRACE("%p\n", this);
429     MSVCP_runtime_error_dtor(this);
430 }
431
432 DEFINE_THISCALL_WRAPPER(MSVCP_failure_vector_dtor, 8)
433 void* __thiscall MSVCP_failure_vector_dtor(
434         failure *this, unsigned int flags)
435 {
436     TRACE("%p %x\n", this, flags);
437     return MSVCP_runtime_error_vector_dtor(this, flags);
438 }
439
440 DEFINE_THISCALL_WRAPPER(MSVCP_failure_what, 4)
441 const char* __thiscall MSVCP_failure_what(failure *this)
442 {
443     TRACE("%p\n", this);
444     return MSVCP_runtime_error_what(this);
445 }
446
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);
449
450 /* ?_Nomemory@std@@YAXXZ */
451 void __cdecl _Nomemory(void)
452 {
453     TRACE("()\n");
454     throw_exception(EXCEPTION_BAD_ALLOC, NULL);
455 }
456
457 #ifndef __GNUC__
458 void __asm_dummy_vtables(void) {
459 #endif
460     __ASM_VTABLE(type_info,
461             VTABLE_ADD_FUNC(MSVCP_type_info_vector_dtor));
462     __ASM_VTABLE(exception,
463             VTABLE_ADD_FUNC(MSVCP_exception_vector_dtor)
464             VTABLE_ADD_FUNC(MSVCP_what_exception));
465     __ASM_VTABLE(bad_alloc,
466             VTABLE_ADD_FUNC(MSVCP_bad_alloc_vector_dtor)
467             VTABLE_ADD_FUNC(MSVCP_what_exception));
468     __ASM_VTABLE(logic_error,
469             VTABLE_ADD_FUNC(MSVCP_logic_error_vector_dtor)
470             VTABLE_ADD_FUNC(MSVCP_logic_error_what));
471     __ASM_VTABLE(length_error,
472             VTABLE_ADD_FUNC(MSVCP_logic_error_vector_dtor)
473             VTABLE_ADD_FUNC(MSVCP_logic_error_what));
474     __ASM_VTABLE(out_of_range,
475             VTABLE_ADD_FUNC(MSVCP_logic_error_vector_dtor)
476             VTABLE_ADD_FUNC(MSVCP_logic_error_what));
477     __ASM_VTABLE(invalid_argument,
478             VTABLE_ADD_FUNC(MSVCP_logic_error_vector_dtor)
479             VTABLE_ADD_FUNC(MSVCP_logic_error_what));
480     __ASM_VTABLE(runtime_error,
481             VTABLE_ADD_FUNC(MSVCP_runtime_error_vector_dtor)
482             VTABLE_ADD_FUNC(MSVCP_runtime_error_what));
483     __ASM_VTABLE(failure,
484             VTABLE_ADD_FUNC(MSVCP_failure_vector_dtor)
485             VTABLE_ADD_FUNC(MSVCP_failure_what));
486 #ifndef __GNUC__
487 }
488 #endif
489
490 /* Internal: throws selected exception */
491 void throw_exception(exception_type et, const char *str)
492 {
493     const char *addr = str;
494
495     switch(et) {
496     case EXCEPTION_RERAISE:
497         _CxxThrowException(NULL, NULL);
498     case EXCEPTION: {
499         exception e;
500         MSVCP_exception_ctor(&e, &addr);
501         _CxxThrowException(&e, &exception_cxx_type);
502     }
503     case EXCEPTION_BAD_ALLOC: {
504         bad_alloc e;
505         MSVCP_bad_alloc_ctor(&e, &addr);
506         _CxxThrowException(&e, &bad_alloc_cxx_type);
507     }
508     case EXCEPTION_LOGIC_ERROR: {
509         logic_error e;
510         MSVCP_logic_error_ctor(&e, &addr);
511         _CxxThrowException((exception*)&e, &logic_error_cxx_type);
512     }
513     case EXCEPTION_LENGTH_ERROR: {
514         length_error e;
515         MSVCP_length_error_ctor(&e, &addr);
516         _CxxThrowException((exception*)&e, &length_error_cxx_type);
517     }
518     case EXCEPTION_OUT_OF_RANGE: {
519         out_of_range e;
520         MSVCP_out_of_range_ctor(&e, &addr);
521         _CxxThrowException((exception*)&e, &out_of_range_cxx_type);
522     }
523     case EXCEPTION_INVALID_ARGUMENT: {
524         invalid_argument e;
525         MSVCP_invalid_argument_ctor(&e, &addr);
526         _CxxThrowException((exception*)&e, &invalid_argument_cxx_type);
527     }
528     case EXCEPTION_RUNTIME_ERROR: {
529         runtime_error e;
530         MSVCP_runtime_error_ctor(&e, &addr);
531         _CxxThrowException((exception*)&e, &runtime_error_cxx_type);
532     }
533     case EXCEPTION_FAILURE: {
534         failure e;
535         MSVCP_failure_ctor(&e, &addr);
536         _CxxThrowException((exception*)&e, &failure_cxx_type);
537     }
538     }
539 }
540
541 void init_exception(void *base)
542 {
543 #ifdef __x86_64__
544     init_type_info_rtti(base);
545     init_exception_rtti(base);
546     init_bad_alloc_rtti(base);
547     init_logic_error_rtti(base);
548     init_length_error_rtti(base);
549     init_out_of_range_rtti(base);
550     init_invalid_argument_rtti(base);
551     init_runtime_error_rtti(base);
552     init_failure_rtti(base);
553
554     init_exception_cxx(base);
555     init_bad_alloc_cxx(base);
556     init_logic_error_cxx(base);
557     init_length_error_cxx(base);
558     init_out_of_range_cxx(base);
559     init_invalid_argument_cxx(base);
560     init_runtime_error_cxx(base);
561     init_failure_cxx(base);
562 #endif
563 }