/home/andy/git/oilshell/oil/cpp/core_error.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Replacement for core/error |
2 | | |
3 | | #ifndef CORE_ERROR_H |
4 | | #define CORE_ERROR_H |
5 | | |
6 | | #include "_gen/frontend/syntax.asdl.h" |
7 | | #include "mycpp/runtime.h" |
8 | | |
9 | | namespace runtime { |
10 | | extern int NO_SPID; |
11 | | } |
12 | | |
13 | | namespace error { |
14 | | |
15 | | using syntax_asdl::Token; |
16 | | using syntax_asdl::word_part_t; |
17 | | using syntax_asdl::word_t; |
18 | | |
19 | | class Usage { |
20 | | public: |
21 | | Usage(Str* msg, int span_id) |
22 | | : GC_CLASS_FIXED(header_, field_mask(), sizeof(Usage)), |
23 | | msg(msg), |
24 | 1 | span_id(span_id) { |
25 | 1 | } |
26 | | |
27 | | Usage(Str* msg) |
28 | | : GC_CLASS_FIXED(header_, field_mask(), sizeof(Usage)), |
29 | | msg(msg), |
30 | 0 | span_id(runtime::NO_SPID) { |
31 | 0 | } |
32 | | |
33 | | GC_OBJ(header_); |
34 | | Str* msg; |
35 | | int span_id; |
36 | | |
37 | 1 | static constexpr uint16_t field_mask() { |
38 | 1 | return maskbit(offsetof(Usage, msg)); |
39 | 1 | } |
40 | | }; |
41 | | |
42 | | // This definition is different in Python than C++. Not worth auto-translating. |
43 | | class _ErrorWithLocation { |
44 | | public: |
45 | | _ErrorWithLocation(Str* user_str, int span_id) |
46 | | : GC_CLASS_FIXED(header_, field_mask(), sizeof(_ErrorWithLocation)), |
47 | | status(1), |
48 | | user_str_(user_str), |
49 | | span_id(span_id), |
50 | | token(nullptr), |
51 | | part(nullptr), |
52 | 1 | word(nullptr) { |
53 | 1 | } |
54 | | _ErrorWithLocation(Str* user_str, Token* token) |
55 | | : GC_CLASS_FIXED(header_, field_mask(), sizeof(_ErrorWithLocation)), |
56 | | status(1), |
57 | | user_str_(user_str), |
58 | | span_id(runtime::NO_SPID), |
59 | | token(token), |
60 | | part(nullptr), |
61 | 0 | word(nullptr) { |
62 | 0 | } |
63 | | _ErrorWithLocation(Str* user_str, word_part_t* part) |
64 | | : GC_CLASS_FIXED(header_, field_mask(), sizeof(_ErrorWithLocation)), |
65 | | status(1), |
66 | | user_str_(user_str), |
67 | | span_id(runtime::NO_SPID), |
68 | | token(nullptr), |
69 | | part(part), |
70 | 0 | word(nullptr) { |
71 | 0 | } |
72 | | _ErrorWithLocation(Str* user_str, word_t* word) |
73 | | : GC_CLASS_FIXED(header_, field_mask(), sizeof(_ErrorWithLocation)), |
74 | | status(1), |
75 | | user_str_(user_str), |
76 | | span_id(runtime::NO_SPID), |
77 | | token(nullptr), |
78 | | part(nullptr), |
79 | 0 | word(word) { |
80 | 0 | } |
81 | | _ErrorWithLocation(int status, Str* user_str, int span_id, bool show_code) |
82 | | : GC_CLASS_FIXED(header_, field_mask(), sizeof(_ErrorWithLocation)), |
83 | | status(status), |
84 | | user_str_(user_str), |
85 | | span_id(span_id), |
86 | | token(nullptr), |
87 | | part(nullptr), |
88 | | word(nullptr), |
89 | 0 | show_code(show_code) { |
90 | 0 | } |
91 | | |
92 | 0 | Str* UserErrorString() { |
93 | 0 | return user_str_; |
94 | 0 | } |
95 | | |
96 | 0 | bool HasLocation() { |
97 | 0 | return false; // TODO: fix this |
98 | 0 | assert(0); |
99 | 0 | } |
100 | | |
101 | 0 | int ExitStatus() { |
102 | 0 | return status; |
103 | 0 | } |
104 | | |
105 | | GC_OBJ(header_); |
106 | | |
107 | | int status; |
108 | | Str* user_str_; |
109 | | int span_id; |
110 | | syntax_asdl::Token* token; |
111 | | syntax_asdl::word_part_t* part; |
112 | | syntax_asdl::word_t* word; |
113 | | |
114 | | bool show_code; |
115 | | |
116 | 1 | static constexpr uint16_t field_mask() { |
117 | 1 | return maskbit(offsetof(_ErrorWithLocation, user_str_)); |
118 | 1 | } |
119 | | }; |
120 | | |
121 | | class Parse : public _ErrorWithLocation { |
122 | | public: |
123 | 0 | Parse(Str* user_str, int span_id) : _ErrorWithLocation(user_str, span_id) { |
124 | 0 | } |
125 | 0 | Parse(Str* user_str, Token* token) : _ErrorWithLocation(user_str, token) { |
126 | 0 | } |
127 | 0 | Parse(Str* user_str, word_part_t* part) : _ErrorWithLocation(user_str, part) { |
128 | 0 | } |
129 | 0 | Parse(Str* user_str, word_t* word) : _ErrorWithLocation(user_str, word) { |
130 | 0 | } |
131 | | }; |
132 | | |
133 | | class RedirectEval : public _ErrorWithLocation { |
134 | | public: |
135 | | // code only uses this variant |
136 | | RedirectEval(Str* user_str, word_t* word) |
137 | 0 | : _ErrorWithLocation(user_str, word) { |
138 | 0 | } |
139 | | }; |
140 | | |
141 | | class FailGlob : public _ErrorWithLocation { |
142 | | public: |
143 | | // code only uses this variant |
144 | 0 | FailGlob(Str* user_str, int span_id) : _ErrorWithLocation(user_str, span_id) { |
145 | 0 | } |
146 | | }; |
147 | | |
148 | | class FatalRuntime : public _ErrorWithLocation { |
149 | | public: |
150 | 1 | explicit FatalRuntime(Str* user_str) : _ErrorWithLocation(user_str, -1) { |
151 | 1 | } |
152 | | FatalRuntime(int status, Str* user_str) |
153 | 0 | : _ErrorWithLocation(status, user_str, -1, false) { |
154 | 0 | } |
155 | | }; |
156 | | |
157 | | class Strict : public FatalRuntime { |
158 | | public: |
159 | 0 | explicit Strict(Str* user_str) : FatalRuntime(user_str) { |
160 | 0 | } |
161 | | }; |
162 | | |
163 | | // Stub |
164 | | class ErrExit : public _ErrorWithLocation { |
165 | | public: |
166 | | ErrExit(Str* user_str, int span_id, int status) |
167 | 0 | : _ErrorWithLocation(status, user_str, span_id, false) { |
168 | 0 | } |
169 | | ErrExit(Str* user_str, int span_id, int status, bool show_code) |
170 | 0 | : _ErrorWithLocation(status, user_str, span_id, show_code) { |
171 | 0 | } |
172 | | }; |
173 | | |
174 | | // Stub: the parts that raise aren't translated |
175 | | class Expr : public _ErrorWithLocation { |
176 | | public: |
177 | 0 | Expr(Str* user_str, int span_id) : _ErrorWithLocation(user_str, span_id) { |
178 | 0 | } |
179 | | #if 0 |
180 | | Expr(Str* user_str, Token* token) |
181 | | : _ErrorWithLocation(user_str, token) { |
182 | | } |
183 | | #endif |
184 | | }; |
185 | | |
186 | | // Stub |
187 | | class Runtime : public _ErrorWithLocation { |
188 | | public: |
189 | 0 | explicit Runtime(Str* user_str) : _ErrorWithLocation(user_str, -1) { |
190 | 0 | } |
191 | | }; |
192 | | |
193 | | } // namespace error |
194 | | |
195 | | #endif // CORE_ERROR_H |