msvcrt: Fix MSVC build in exit.c and msvcrt.h.
[wine] / dlls / msvcrt / cppexcept.h
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 #define CXX_FRAME_MAGIC_VC6 0x19930520
25 #define CXX_FRAME_MAGIC_VC7 0x19930521
26 #define CXX_FRAME_MAGIC_VC8 0x19930522
27 #define CXX_EXCEPTION       0xe06d7363
28
29 typedef void (*vtable_ptr)(void);
30
31 /* type_info object, see cpp.c for implementation */
32 typedef struct __type_info
33 {
34   const vtable_ptr *vtable;
35   char              *name;        /* Unmangled name, allocated lazily */
36   char               mangled[32]; /* Variable length, but we declare it large enough for static RTTI */
37 } type_info;
38
39 /* exception object */
40 typedef struct __exception
41 {
42   const vtable_ptr *vtable;
43   char             *name;    /* Name of this exception, always a new copy for each object */
44   int               do_free; /* Whether to free 'name' in our dtor */
45 } exception;
46
47 /* the exception frame used by CxxFrameHandler */
48 typedef struct __cxx_exception_frame
49 {
50     EXCEPTION_REGISTRATION_RECORD  frame;    /* the standard exception frame */
51     int                            trylevel;
52     DWORD                          ebp;
53 } cxx_exception_frame;
54
55 /* info about a single catch {} block */
56 typedef struct __catchblock_info
57 {
58     UINT             flags;         /* flags (see below) */
59     const type_info *type_info;     /* C++ type caught by this block */
60     int              offset;        /* stack offset to copy exception object to */
61     void           (*handler)(void);/* catch block handler code */
62 } catchblock_info;
63 #define TYPE_FLAG_CONST      1
64 #define TYPE_FLAG_VOLATILE   2
65 #define TYPE_FLAG_REFERENCE  8
66
67 /* info about a single try {} block */
68 typedef struct __tryblock_info
69 {
70     int                    start_level;      /* start trylevel of that block */
71     int                    end_level;        /* end trylevel of that block */
72     int                    catch_level;      /* initial trylevel of the catch block */
73     int                    catchblock_count; /* count of catch blocks in array */
74     const catchblock_info *catchblock;       /* array of catch blocks */
75 } tryblock_info;
76
77 /* info about the unwind handler for a given trylevel */
78 typedef struct __unwind_info
79 {
80     int    prev;          /* prev trylevel unwind handler, to run after this one */
81     void (*handler)(void);/* unwind handler */
82 } unwind_info;
83
84 /* descriptor of all try blocks of a given function */
85 typedef struct __cxx_function_descr
86 {
87     UINT                 magic;          /* must be CXX_FRAME_MAGIC */
88     UINT                 unwind_count;   /* number of unwind handlers */
89     const unwind_info   *unwind_table;   /* array of unwind handlers */
90     UINT                 tryblock_count; /* number of try blocks */
91     const tryblock_info *tryblock;       /* array of try blocks */
92     UINT                 ipmap_count;
93     const void          *ipmap;
94     const void          *expect_list;    /* expected exceptions list when magic >= VC7 */
95     UINT                 flags;          /* flags when magic >= VC8 */
96 } cxx_function_descr;
97
98 #define FUNC_DESCR_SYNCHRONOUS  1        /* synchronous exceptions only (built with /EHs) */
99
100 typedef void (*cxx_copy_ctor)(void);
101
102 /* offsets for computing the this pointer */
103 typedef struct
104 {
105     int         this_offset;   /* offset of base class this pointer from start of object */
106     int         vbase_descr;   /* offset of virtual base class descriptor */
107     int         vbase_offset;  /* offset of this pointer offset in virtual base class descriptor */
108 } this_ptr_offsets;
109
110 /* complete information about a C++ type */
111 typedef struct __cxx_type_info
112 {
113     UINT             flags;        /* flags (see CLASS_* flags below) */
114     const type_info *type_info;    /* C++ type info */
115     this_ptr_offsets offsets;      /* offsets for computing the this pointer */
116     unsigned int     size;         /* object size */
117     cxx_copy_ctor    copy_ctor;    /* copy constructor */
118 } cxx_type_info;
119 #define CLASS_IS_SIMPLE_TYPE          1
120 #define CLASS_HAS_VIRTUAL_BASE_CLASS  4
121
122 /* table of C++ types that apply for a given object */
123 typedef struct __cxx_type_info_table
124 {
125     UINT                 count;     /* number of types */
126     const cxx_type_info *info[3];   /* variable length, we declare it large enough for static RTTI */
127 } cxx_type_info_table;
128
129 typedef DWORD (*cxx_exc_custom_handler)( PEXCEPTION_RECORD, cxx_exception_frame*,
130                                          PCONTEXT, EXCEPTION_REGISTRATION_RECORD**,
131                                          const cxx_function_descr*, int nested_trylevel,
132                                          EXCEPTION_REGISTRATION_RECORD *nested_frame, DWORD unknown3 );
133
134 /* type information for an exception object */
135 typedef struct __cxx_exception_type
136 {
137     UINT                       flags;            /* TYPE_FLAG flags */
138     void                     (*destructor)(void);/* exception object destructor */
139     cxx_exc_custom_handler     custom_handler;   /* custom handler for this exception */
140     const cxx_type_info_table *type_info_table;  /* list of types for this exception object */
141 } cxx_exception_type;
142
143 void WINAPI _CxxThrowException(exception*,const cxx_exception_type*);
144 int CDECL _XcptFilter(NTSTATUS, PEXCEPTION_POINTERS);
145 int CDECL __CppXcptFilter(NTSTATUS, PEXCEPTION_POINTERS);
146
147 static inline const char *dbgstr_type_info( const type_info *info )
148 {
149     if (!info) return "{}";
150     return wine_dbg_sprintf( "{vtable=%p name=%s (%s)}",
151                              info->vtable, info->mangled, info->name ? info->name : "" );
152 }
153
154 /* compute the this pointer for a base class of a given type */
155 static inline void *get_this_pointer( const this_ptr_offsets *off, void *object )
156 {
157     void *this_ptr;
158     int *offset_ptr;
159
160     if (!object) return NULL;
161     this_ptr = (char *)object + off->this_offset;
162     if (off->vbase_descr >= 0)
163     {
164         /* move this ptr to vbase descriptor */
165         this_ptr = (char *)this_ptr + off->vbase_descr;
166         /* and fetch additional offset from vbase descriptor */
167         offset_ptr = (int *)(*(char **)this_ptr + off->vbase_offset);
168         this_ptr = (char *)this_ptr + *offset_ptr;
169     }
170     return this_ptr;
171 }
172
173 #endif /* __MSVCRT_CPPEXCEPT_H */