forked from asyncvlsi/act
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcept.h
145 lines (105 loc) · 2.6 KB
/
except.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*************************************************************************
*
* Exception handling support in C
*
* Copyright (c) 2009, 2019 Rajit Manohar
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
**************************************************************************
*/
#ifndef __EXCEPT_H__
#define __EXCEPT_H__
/*
Faking exceptions in C
*/
#include <stdio.h>
#include <setjmp.h>
#ifdef __cplusplus
extern "C" {
#endif
#if defined (__STDC__)
#if (__STDC_VERSION__ >= 201112L)
#define NORETURN_SPECIFIER _Noreturn
#else
#define NORETURN_SPECIFIER
#endif
#else
#define NORETURN_SPECIFIER
#endif
typedef struct _except_ except_t;
void except_init (void);
/* initialize exceptions package */
void except_done (void);
int except_type (void);
char *except_arg (void);
NORETURN_SPECIFIER void except_throw (int type, char *arg);
void except_error (void);
void _except_setup (void);
except_t *_except_env (void);
/*
try {
...
}
catch {
case ...
}
Turns into:
if (except_try ()) {
except_done ();
}
else {
except_error ();
switch (except_type ()) {
case T1:
case T2:
...
}
}
throw exception
turns into:
except_throw (type, arg);
========================================================================
With macros, this is:
TRY {
...
} CATCH {
EXCEPT_SWITCH {
case T1:
...
}
}
*/
struct _except_ {
int type;
char *msg;
jmp_buf env;
struct _except_ *next;
};
#define TRY \
_except_setup (); \
if (setjmp (_except_env()->env) == 0) {
#define CATCH except_done(); } else
#define EXCEPT_SWITCH except_error(); switch (except_type())
#define THROW(x) except_throw ((x), NULL)
#define THROW2(x,y) except_throw ((x), (y))
#define DEFAULT_CASE default: except_throw (except_type (), except_arg ()); break
/* common exceptions */
#define EXC_NULL_EXCEPTION 1
#ifdef __cplusplus
}
#endif
#endif /* __EXCEPT_H__ */