ReactOS 0.4.16-dev-456-ga97fcf1
cppexcept.h
Go to the documentation of this file.
1/*
2 * msvcrt C++ exception handling
3 *
4 * Copyright 2002 Alexandre Julliard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21#ifndef __MSVCRT_CPPEXCEPT_H
22#define __MSVCRT_CPPEXCEPT_H
23
24#include "wine/asm.h"
25
26#define CXX_FRAME_MAGIC_VC6 0x19930520
27#define CXX_FRAME_MAGIC_VC7 0x19930521
28#define CXX_FRAME_MAGIC_VC8 0x19930522
29#define CXX_EXCEPTION 0xe06d7363
30
31#define FUNC_DESCR_SYNCHRONOUS 1 /* synchronous exceptions only (built with /EHs and /EHsc) */
32#define FUNC_DESCR_NOEXCEPT 4 /* noexcept function */
33
34typedef void (*vtable_ptr)(void);
35
36/* type_info object, see cpp.c for implementation */
37typedef struct __type_info
38{
39 const vtable_ptr *vtable;
40 char *name; /* Unmangled name, allocated lazily */
41 char mangled[64]; /* Variable length, but we declare it large enough for static RTTI */
43
44/* exception object */
45typedef struct __exception
46{
47 const vtable_ptr *vtable;
48 char *name; /* Name of this exception, always a new copy for each object */
49 BOOL do_free; /* Whether to free 'name' in our dtor */
51
53
54/* offsets for computing the this pointer */
55typedef struct
56{
57 int this_offset; /* offset of base class this pointer from start of object */
58 int vbase_descr; /* offset of virtual base class descriptor */
59 int vbase_offset; /* offset of this pointer offset in virtual base class descriptor */
61
62/* complete information about a C++ type */
63#ifndef __x86_64__
64typedef struct __cxx_type_info
65{
66 UINT flags; /* flags (see CLASS_* flags below) */
67 const type_info *type_info; /* C++ type info */
68 this_ptr_offsets offsets; /* offsets for computing the this pointer */
69 unsigned int size; /* object size */
70 cxx_copy_ctor copy_ctor; /* copy constructor */
72#else
73typedef struct __cxx_type_info
74{
75 UINT flags;
76 unsigned int type_info;
78 unsigned int size;
79 unsigned int copy_ctor;
81#endif
82
83#define CLASS_IS_SIMPLE_TYPE 1
84#define CLASS_HAS_VIRTUAL_BASE_CLASS 4
85
86/* table of C++ types that apply for a given object */
87#ifndef __x86_64__
88typedef struct __cxx_type_info_table
89{
90 UINT count; /* number of types */
91 const cxx_type_info *info[3]; /* variable length, we declare it large enough for static RTTI */
93#else
94typedef struct __cxx_type_info_table
95{
96 UINT count;
97 unsigned int info[3];
99#endif
100
103
106 const struct __cxx_function_descr*, int nested_trylevel,
107 EXCEPTION_REGISTRATION_RECORD *nested_frame, DWORD unknown3 );
108
109/* type information for an exception object */
110#ifndef __x86_64__
111typedef struct __cxx_exception_type
112{
113 UINT flags; /* TYPE_FLAG flags */
114 void (*destructor)(void);/* exception object destructor */
115 cxx_exc_custom_handler custom_handler; /* custom handler for this exception */
116 const cxx_type_info_table *type_info_table; /* list of types for this exception object */
118#else
119typedef struct
120{
121 UINT flags;
122 unsigned int destructor;
123 unsigned int custom_handler;
124 unsigned int type_info_table;
126#endif
127
130
131static inline const char *dbgstr_type_info( const type_info *info )
132{
133 if (!info) return "{}";
134 return wine_dbg_sprintf( "{vtable=%p name=%s (%s)}",
135 info->vtable, info->mangled, info->name ? info->name : "" );
136}
137
138/* compute the this pointer for a base class of a given type */
139static inline void *get_this_pointer( const this_ptr_offsets *off, void *object )
140{
141 if (!object) return NULL;
142
143 if (off->vbase_descr >= 0)
144 {
145 int *offset_ptr;
146
147 /* move this ptr to vbase descriptor */
148 object = (char *)object + off->vbase_descr;
149 /* and fetch additional offset from vbase descriptor */
150 offset_ptr = (int *)(*(char **)object + off->vbase_offset);
151 object = (char *)object + *offset_ptr;
152 }
153
154 object = (char *)object + off->this_offset;
155 return object;
156}
157
158#ifndef __x86_64__
159#define DEFINE_CXX_TYPE_INFO(type) \
160static const cxx_type_info type ## _cxx_type_info = { \
161 0, \
162 & type ##_type_info, \
163 { 0, -1, 0 }, \
164 sizeof(type), \
165 (cxx_copy_ctor)THISCALL(type ##_copy_ctor) \
166};
167
168#define DEFINE_CXX_EXCEPTION(type, base_no, cl1, cl2, dtor) \
169static const cxx_type_info_table type ## _cxx_type_table = { \
170 base_no+1, \
171 { \
172 & type ## _cxx_type_info, \
173 cl1, \
174 cl2, \
175 } \
176}; \
177\
178static const cxx_exception_type type ## _exception_type = { \
179 0, \
180 (cxx_copy_ctor)THISCALL(dtor), \
181 NULL, \
182 & type ## _cxx_type_table \
183};
184
185#else
186
187#define DEFINE_CXX_TYPE_INFO(type) \
188static cxx_type_info type ## _cxx_type_info = { \
189 0, \
190 0xdeadbeef, \
191 { 0, -1, 0 }, \
192 sizeof(type), \
193 0xdeadbeef \
194}; \
195\
196static void init_ ## type ## _cxx_type_info(char *base) \
197{ \
198 type ## _cxx_type_info.type_info = (char *)&type ## _type_info - base; \
199 type ## _cxx_type_info.copy_ctor = (char *)type ## _copy_ctor - base; \
200}
201
202#define DEFINE_CXX_EXCEPTION(type, base_no, cl1, cl2, dtor) \
203static cxx_type_info_table type ## _cxx_type_table = { \
204 base_no+1, \
205 { \
206 0xdeadbeef, \
207 0xdeadbeef, \
208 0xdeadbeef, \
209 } \
210}; \
211\
212static cxx_exception_type type ##_exception_type = { \
213 0, \
214 0xdeadbeef, \
215 0, \
216 0xdeadbeef \
217}; \
218\
219static void init_ ## type ## _cxx(char *base) \
220{ \
221 init_ ## type ## _cxx_type_info(base); \
222 type ## _cxx_type_table.info[0] = (char *)&type ## _cxx_type_info - base; \
223 type ## _cxx_type_table.info[1] = (char *)cl1 - base; \
224 type ## _cxx_type_table.info[2] = (char *)cl2 - base; \
225 type ## _exception_type.destructor = (char *)dtor - base; \
226 type ## _exception_type.type_info_table = (char *)&type ## _cxx_type_table - base; \
227}
228
229#endif
230
231#define DEFINE_CXX_DATA(type, base_no, cl1, cl2, dtor) \
232DEFINE_CXX_TYPE_INFO(type) \
233DEFINE_CXX_EXCEPTION(type, base_no, cl1, cl2, dtor)
234
235#define DEFINE_CXX_EXCEPTION0(name, dtor) \
236 DEFINE_CXX_EXCEPTION(name, 0, NULL, NULL, dtor)
237
238#define DEFINE_CXX_DATA0(name, dtor) \
239 DEFINE_CXX_DATA(name, 0, NULL, NULL, dtor)
240#define DEFINE_CXX_DATA1(name, cl1, dtor) \
241 DEFINE_CXX_DATA(name, 1, cl1, NULL, dtor)
242#define DEFINE_CXX_DATA2(name, cl1, cl2, dtor) \
243 DEFINE_CXX_DATA(name, 2, cl1, cl2, dtor)
244
245#if _MSVCR_VER >= 80
246#define EXCEPTION_MANGLED_NAME ".?AVexception@std@@"
247#else
248#define EXCEPTION_MANGLED_NAME ".?AVexception@@"
249#endif
250
251#define CREATE_EXCEPTION_OBJECT(exception_name) \
252static exception* __exception_ctor(exception *this, const char *str, const vtable_ptr *vtbl) \
253{ \
254 if (str) \
255 { \
256 unsigned int len = strlen(str) + 1; \
257 this->name = malloc(len); \
258 memcpy(this->name, str, len); \
259 this->do_free = TRUE; \
260 } \
261 else \
262 { \
263 this->name = NULL; \
264 this->do_free = FALSE; \
265 } \
266 this->vtable = vtbl; \
267 return this; \
268} \
269\
270static exception* __exception_copy_ctor(exception *this, const exception *rhs, const vtable_ptr *vtbl) \
271{ \
272 if (rhs->do_free) \
273 { \
274 __exception_ctor(this, rhs->name, vtbl); \
275 } \
276 else \
277 { \
278 *this = *rhs; \
279 this->vtable = vtbl; \
280 } \
281 return this; \
282} \
283extern const vtable_ptr exception_name ## _vtable; \
284exception* __thiscall exception_name ## _copy_ctor(exception *this, const exception *rhs); \
285DEFINE_THISCALL_WRAPPER(exception_name ## _copy_ctor,8) \
286exception* __thiscall exception_name ## _copy_ctor(exception *this, const exception *rhs) \
287{ \
288 return __exception_copy_ctor(this, rhs, & exception_name ## _vtable); \
289} \
290\
291void __thiscall exception_name ## _dtor(exception *this); \
292DEFINE_THISCALL_WRAPPER(exception_name ## _dtor,4) \
293void __thiscall exception_name ## _dtor(exception *this) \
294{ \
295 if (this->do_free) free(this->name); \
296} \
297\
298void* __thiscall exception_name ## _vector_dtor(exception *this, unsigned int flags); \
299DEFINE_THISCALL_WRAPPER(exception_name ## _vector_dtor,8) \
300void* __thiscall exception_name ## _vector_dtor(exception *this, unsigned int flags) \
301{ \
302 if (flags & 2) \
303 { \
304 INT_PTR i, *ptr = (INT_PTR *)this - 1; \
305\
306 for (i = *ptr - 1; i >= 0; i--) exception_name ## _dtor(this + i); \
307 operator_delete(ptr); \
308 } \
309 else \
310 { \
311 exception_name ## _dtor(this); \
312 if (flags & 1) operator_delete(this); \
313 } \
314 return this; \
315} \
316\
317const char* __thiscall exception_name ## _what(exception *this); \
318DEFINE_THISCALL_WRAPPER(exception_name ## _what,4) \
319const char* __thiscall exception_name ## _what(exception *this) \
320{ \
321 return this->name ? this->name : "Unknown exception"; \
322} \
323\
324__ASM_BLOCK_BEGIN(exception_name ## _vtables) \
325__ASM_VTABLE(exception_name, \
326 VTABLE_ADD_FUNC(exception_name ## _vector_dtor) \
327 VTABLE_ADD_FUNC(exception_name ## _what)); \
328__ASM_BLOCK_END \
329\
330DEFINE_RTTI_DATA0(exception_name, 0, EXCEPTION_MANGLED_NAME) \
331DEFINE_CXX_TYPE_INFO(exception_name)
332
333#endif /* __MSVCRT_CPPEXCEPT_H */
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
const char * wine_dbg_sprintf(const char *format,...)
Definition: compat.c:296
#define CDECL
Definition: compat.h:29
CONTEXT * PCONTEXT
Definition: compat.h:699
struct _EXCEPTION_RECORD * PEXCEPTION_RECORD
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLbitfield flags
Definition: glext.h:7161
int CDECL _XcptFilter(NTSTATUS, PEXCEPTION_POINTERS)
Definition: except.c:278
struct __type_info type_info
static const char * dbgstr_type_info(const type_info *info)
Definition: cppexcept.h:155
void(* cxx_copy_ctor)(void)
Definition: cppexcept.h:108
struct __exception exception
struct __cxx_type_info_table cxx_type_info_table
struct __cxx_exception_type cxx_exception_type
DWORD(* cxx_exc_custom_handler)(PEXCEPTION_RECORD, cxx_exception_frame *, PCONTEXT, EXCEPTION_REGISTRATION_RECORD **, const cxx_function_descr *, int nested_trylevel, EXCEPTION_REGISTRATION_RECORD *nested_frame, DWORD unknown3)
Definition: cppexcept.h:137
struct __cxx_type_info cxx_type_info
void WINAPI _CxxThrowException(exception *, const cxx_exception_type *)
void(* vtable_ptr)()
Definition: cppexcept.h:37
static void * get_this_pointer(const this_ptr_offsets *off, void *object)
Definition: cppexcept.h:163
unsigned int UINT
Definition: ndis.h:50
#define DWORD
Definition: nt_native.h:44
cxx_exc_custom_handler custom_handler
Definition: cppexcept.h:147
const cxx_type_info_table * type_info_table
Definition: cppexcept.h:148
void(* destructor)(void)
Definition: cppexcept.h:146
cxx_copy_ctor copy_ctor
Definition: cppexcept.h:125
const type_info * type_info
Definition: cppexcept.h:122
this_ptr_offsets offsets
Definition: cppexcept.h:123
unsigned int size
Definition: cppexcept.h:124
BOOL do_free
Definition: cppexcept.h:49
char * name
Definition: cpp.c:28
vtable_ptr * vtable
Definition: cpp.c:27
vtable_ptr * vtable
Definition: cpp.c:34
char mangled[16]
Definition: cpp.c:36
char * name
Definition: cpp.c:35
#define WINAPI
Definition: msvc.h:6
void(* cxx_copy_ctor)(void)
Definition: cppexcept.h:52
DWORD(* cxx_exc_custom_handler)(PEXCEPTION_RECORD, struct __cxx_exception_frame *, PCONTEXT, EXCEPTION_REGISTRATION_RECORD **, const struct __cxx_function_descr *, int nested_trylevel, EXCEPTION_REGISTRATION_RECORD *nested_frame, DWORD unknown3)
Definition: cppexcept.h:104
void(* vtable_ptr)(void)
Definition: cppexcept.h:34