msdasql: Implement IColumnsRowset GetAvailableColumns.
[wine.git] / include / wine / exception.h
blob51446ad74037688c87d32d2a65e00d21d805dd42
1 /*
2 * Wine exception handling
4 * Copyright (c) 1999 Alexandre Julliard
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.
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.
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
21 #if 0
22 #pragma makedep install
23 #endif
25 #ifndef __WINE_WINE_EXCEPTION_H
26 #define __WINE_WINE_EXCEPTION_H
28 #include <windef.h>
29 #include <winternl.h>
30 #include <excpt.h>
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
36 /* The following definitions allow using exceptions in Wine and Winelib code
38 * They should be used like this:
40 * __TRY
41 * {
42 * do some stuff that can raise an exception
43 * }
44 * __EXCEPT(filter_func)
45 * {
46 * handle the exception here
47 * }
48 * __ENDTRY
50 * or
52 * __TRY
53 * {
54 * do some stuff that can raise an exception
55 * }
56 * __FINALLY(finally_func)
58 * The filter_func and finally_func functions must be defined like this:
60 * LONG CALLBACK filter_func( PEXCEPTION_POINTERS __eptr ) { ... }
62 * void CALLBACK finally_func( BOOL __normal ) { ... }
64 * The filter function must return one of the EXCEPTION_* code; it can
65 * use GetExceptionInformation() and GetExceptionCode() to retrieve the
66 * exception info.
68 * Warning: Inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
69 * break out of the current block, but avoid using them because they
70 * won't work when compiling with native exceptions. You cannot use
71 * 'return', 'goto', or 'longjmp' to leave a __TRY block either, as
72 * this will surely crash. You can use 'return', 'goto', or 'longjmp'
73 * to leave an __EXCEPT block though.
75 * -- AJ
78 #if !defined(__GNUC__) && !defined(__clang__)
79 #define __attribute__(x) /* nothing */
80 #endif
82 /* Define this if you want to use your compiler built-in __try/__except support.
83 * This is only useful when compiling to a native Windows binary, as the built-in
84 * compiler exceptions will most certainly not work under Winelib.
86 #ifdef USE_COMPILER_EXCEPTIONS
88 #define __TRY __try
89 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
90 #define __EXCEPT_CTX(func, ctx) __except((func)(GetExceptionInformation(), ctx))
91 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
92 #define __FINALLY_CTX(func, ctx) __finally { (func)(!AbnormalTermination(), ctx); }
93 #define __ENDTRY /*nothing*/
94 #define __EXCEPT_PAGE_FAULT __except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
95 #define __EXCEPT_ALL __except(EXCEPTION_EXECUTE_HANDLER)
97 #else /* USE_COMPILER_EXCEPTIONS */
99 #ifdef __i386__
100 typedef struct { int reg[16]; } __wine_jmp_buf;
101 #elif defined(__x86_64__)
102 typedef struct { DECLSPEC_ALIGN(16) struct { unsigned __int64 Part[2]; } reg[16]; } __wine_jmp_buf;
103 #elif defined(__arm__)
104 typedef struct { int reg[28]; } __wine_jmp_buf;
105 #elif defined(__aarch64__)
106 typedef struct { __int64 reg[24]; } __wine_jmp_buf;
107 #else
108 typedef struct { int reg; } __wine_jmp_buf;
109 #endif
111 extern int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) __wine_setjmpex( __wine_jmp_buf *buf,
112 EXCEPTION_REGISTRATION_RECORD *frame ) DECLSPEC_HIDDEN;
113 extern void DECLSPEC_NORETURN __cdecl __wine_longjmp( __wine_jmp_buf *buf, int retval ) DECLSPEC_HIDDEN;
114 extern void DECLSPEC_NORETURN __cdecl __wine_rtl_unwind( EXCEPTION_REGISTRATION_RECORD* frame, EXCEPTION_RECORD *record,
115 void (*target)(void) ) DECLSPEC_HIDDEN;
116 extern DWORD __cdecl __wine_exception_handler( EXCEPTION_RECORD *record,
117 EXCEPTION_REGISTRATION_RECORD *frame,
118 CONTEXT *context,
119 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
120 extern DWORD __cdecl __wine_exception_ctx_handler( EXCEPTION_RECORD *record,
121 EXCEPTION_REGISTRATION_RECORD *frame,
122 CONTEXT *context,
123 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
124 extern DWORD __cdecl __wine_exception_handler_page_fault( EXCEPTION_RECORD *record,
125 EXCEPTION_REGISTRATION_RECORD *frame,
126 CONTEXT *context,
127 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
128 extern DWORD __cdecl __wine_exception_handler_all( EXCEPTION_RECORD *record,
129 EXCEPTION_REGISTRATION_RECORD *frame,
130 CONTEXT *context,
131 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
132 extern DWORD __cdecl __wine_finally_handler( EXCEPTION_RECORD *record,
133 EXCEPTION_REGISTRATION_RECORD *frame,
134 CONTEXT *context,
135 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
136 extern DWORD __cdecl __wine_finally_ctx_handler( EXCEPTION_RECORD *record,
137 EXCEPTION_REGISTRATION_RECORD *frame,
138 CONTEXT *context,
139 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
141 #define __TRY \
142 do { __WINE_FRAME __f; \
143 int __first = 1; \
144 for (;;) if (!__first) \
146 do {
148 #define __EXCEPT(func) \
149 } while(0); \
150 __wine_pop_frame( &__f.frame ); \
151 break; \
152 } else { \
153 __f.frame.Handler = __wine_exception_handler; \
154 __f.u.filter = (func); \
155 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
156 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
157 do {
159 #define __EXCEPT_CTX(func, context) \
160 } while(0); \
161 __wine_pop_frame( &__f.frame ); \
162 break; \
163 } else { \
164 __f.frame.Handler = __wine_exception_ctx_handler; \
165 __f.u.filter_ctx = (func); \
166 __f.ctx = context; \
167 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
168 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
169 do {
171 #define __EXCEPT_HANDLER(handler) \
172 } while(0); \
173 __wine_pop_frame( &__f.frame ); \
174 break; \
175 } else { \
176 __f.frame.Handler = (handler); \
177 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
178 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
179 do {
181 #define __EXCEPT_PAGE_FAULT __EXCEPT_HANDLER(__wine_exception_handler_page_fault)
182 #define __EXCEPT_ALL __EXCEPT_HANDLER(__wine_exception_handler_all)
184 #define __ENDTRY \
185 } while (0); \
186 break; \
188 __wine_push_frame( &__f.frame ); \
189 __first = 0; \
191 } while (0);
193 #define __FINALLY(func) \
194 } while(0); \
195 __wine_pop_frame( &__f.frame ); \
196 (func)(1); \
197 break; \
198 } else { \
199 __f.frame.Handler = __wine_finally_handler; \
200 __f.u.finally_func = (func); \
201 __wine_push_frame( &__f.frame ); \
202 __first = 0; \
204 } while (0);
206 #define __FINALLY_CTX(func, context) \
207 } while(0); \
208 __wine_pop_frame( &__f.frame ); \
209 (func)(1, context); \
210 break; \
211 } else { \
212 __f.frame.Handler = __wine_finally_ctx_handler; \
213 __f.u.finally_func_ctx = (func); \
214 __f.ctx = context; \
215 __wine_push_frame( &__f.frame ); \
216 __first = 0; \
218 } while (0);
221 typedef LONG (CALLBACK *__WINE_FILTER)(PEXCEPTION_POINTERS);
222 typedef LONG (CALLBACK *__WINE_FILTER_CTX)(PEXCEPTION_POINTERS, void*);
223 typedef void (CALLBACK *__WINE_FINALLY)(BOOL);
224 typedef void (CALLBACK *__WINE_FINALLY_CTX)(BOOL, void*);
226 #define GetExceptionInformation() (__eptr)
227 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
228 #define AbnormalTermination() (!__normal)
230 typedef struct __tagWINE_FRAME
232 EXCEPTION_REGISTRATION_RECORD frame;
233 union
235 /* exception data */
236 __WINE_FILTER filter;
237 __WINE_FILTER_CTX filter_ctx;
238 /* finally data */
239 __WINE_FINALLY finally_func;
240 __WINE_FINALLY_CTX finally_func_ctx;
241 } u;
242 void *ctx;
243 __wine_jmp_buf jmp;
244 /* hack to make GetExceptionCode() work in handler */
245 DWORD ExceptionCode;
246 const struct __tagWINE_FRAME *ExceptionRecord;
247 } __WINE_FRAME;
249 #endif /* USE_COMPILER_EXCEPTIONS */
251 static inline EXCEPTION_REGISTRATION_RECORD *__wine_push_frame( EXCEPTION_REGISTRATION_RECORD *frame )
253 #if defined(__GNUC__) && defined(__i386__)
254 EXCEPTION_REGISTRATION_RECORD *prev;
255 __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
256 "\n\tmovl %0,(%1)"
257 "\n\t.byte 0x64\n\tmovl %1,(0)"
258 : "=&r" (prev) : "r" (frame) : "memory" );
259 return prev;
260 #else
261 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
262 frame->Prev = teb->ExceptionList;
263 teb->ExceptionList = frame;
264 return frame->Prev;
265 #endif
268 static inline EXCEPTION_REGISTRATION_RECORD *__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD *frame )
270 #if defined(__GNUC__) && defined(__i386__)
271 __asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
272 : : "r" (frame->Prev) : "memory" );
273 return frame->Prev;
275 #else
276 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
277 teb->ExceptionList = frame->Prev;
278 return frame->Prev;
279 #endif
282 static inline EXCEPTION_REGISTRATION_RECORD *__wine_get_frame(void)
284 #if defined(__GNUC__) && defined(__i386__)
285 EXCEPTION_REGISTRATION_RECORD *ret;
286 __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0" : "=r" (ret) );
287 return ret;
288 #else
289 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
290 return teb->ExceptionList;
291 #endif
294 #ifdef __cplusplus
296 #endif
298 #endif /* __WINE_WINE_EXCEPTION_H */