#pragma lib
[m6w6/ext-psi] / src / types / cpp_exp.c
1 /*******************************************************************************
2 Copyright (c) 2017, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #else
29 # include "php_config.h"
30 #endif
31
32 #include <assert.h>
33
34 #include "data.h"
35 #include "cpp.h"
36 #include "debug.h"
37
38 struct psi_cpp_exp *psi_cpp_exp_init(token_t type, void *data)
39 {
40 struct psi_cpp_exp *exp = pecalloc(1, sizeof(*exp), 1);
41
42 switch ((exp->type = type)) {
43 case PSI_T_WARNING:
44 case PSI_T_ERROR:
45 case PSI_T_UNDEF:
46 case PSI_T_IFDEF:
47 case PSI_T_IFNDEF:
48 case PSI_T_IMPORT:
49 case PSI_T_INCLUDE:
50 case PSI_T_INCLUDE_NEXT:
51 exp->data.tok = data;
52 break;
53 case PSI_T_DEFINE:
54 case PSI_T_PRAGMA:
55 exp->data.decl = data;
56 break;
57 case PSI_T_IF:
58 case PSI_T_ELIF:
59 exp->data.num = data;
60 break;
61 case PSI_T_ENDIF:
62 case PSI_T_ELSE:
63 break;
64 default:
65 assert(0);
66 break;
67 }
68
69 return exp;
70 }
71
72 void psi_cpp_exp_free(struct psi_cpp_exp **exp_ptr)
73 {
74 if (*exp_ptr) {
75 struct psi_cpp_exp *exp = *exp_ptr;
76
77 *exp_ptr = NULL;
78 switch (exp->type) {
79 case PSI_T_WARNING:
80 case PSI_T_ERROR:
81 case PSI_T_UNDEF:
82 case PSI_T_IFDEF:
83 case PSI_T_IFNDEF:
84 case PSI_T_IMPORT:
85 case PSI_T_INCLUDE:
86 case PSI_T_INCLUDE_NEXT:
87 psi_token_free(&exp->data.tok);
88 break;
89 case PSI_T_DEFINE:
90 case PSI_T_PRAGMA:
91 psi_cpp_macro_decl_free(&exp->data.decl);
92 break;
93 case PSI_T_IF:
94 case PSI_T_ELIF:
95 psi_num_exp_free(&exp->data.num);
96 break;
97 case PSI_T_ENDIF:
98 case PSI_T_ELSE:
99 break;
100 default:
101 assert(0);
102 break;
103 }
104 psi_token_free(&exp->token);
105 free(exp);
106 }
107 }
108
109 void psi_cpp_exp_dump(struct psi_dump *dump, struct psi_cpp_exp *exp)
110 {
111 PSI_DUMP(dump, "#%s ", exp->token->text->val);
112 switch (exp->type) {
113 case PSI_T_WARNING:
114 case PSI_T_ERROR:
115 if (!exp->data.tok) {
116 break;
117 }
118 /* no break */
119 case PSI_T_UNDEF:
120 case PSI_T_IFDEF:
121 case PSI_T_IFNDEF:
122 PSI_DUMP(dump, "%s", exp->data.tok->text->val);
123 break;
124 case PSI_T_IMPORT:
125 case PSI_T_INCLUDE:
126 case PSI_T_INCLUDE_NEXT:
127 if (exp->data.tok->type == PSI_T_CPP_HEADER) {
128 PSI_DUMP(dump, "<%s>", exp->data.tok->text->val);
129 } else {
130 PSI_DUMP(dump, "\"%s\"", exp->data.tok->text->val);
131 }
132 break;
133 case PSI_T_DEFINE:
134 case PSI_T_PRAGMA:
135 psi_cpp_macro_decl_dump(dump, exp->data.decl);
136 break;
137 case PSI_T_IF:
138 case PSI_T_ELIF:
139 psi_num_exp_dump(dump, exp->data.num);
140 break;
141 case PSI_T_ENDIF:
142 case PSI_T_ELSE:
143 break;
144 default:
145 assert(0);
146 break;
147 }
148 PSI_DUMP(dump, "\n");
149 }
150
151
152 static inline bool psi_cpp_level_skipped(struct psi_cpp *cpp)
153 {
154 return cpp->skip == cpp->level;
155 }
156
157 static inline void psi_cpp_level_skip(struct psi_cpp *cpp)
158 {
159 assert(!cpp->skip);
160 cpp->skip = cpp->level;
161 }
162
163 static inline void psi_cpp_level_unskip(struct psi_cpp *cpp)
164 {
165 if (psi_cpp_level_skipped(cpp)) {
166 cpp->skip = 0;
167 }
168 }
169
170 static inline bool psi_cpp_level_masked(struct psi_cpp *cpp)
171 {
172 return cpp->seen & (1 << cpp->level);
173 }
174
175 static inline void psi_cpp_level_mask(struct psi_cpp *cpp)
176 {
177 assert(!psi_cpp_level_masked(cpp));
178 cpp->seen |= (1 << cpp->level);
179 }
180
181 static inline void psi_cpp_level_unmask(struct psi_cpp *cpp)
182 {
183 cpp->seen &= ~(1 << cpp->level);
184 }
185
186 void psi_cpp_exp_exec(struct psi_cpp_exp *exp, struct psi_cpp *cpp, struct psi_data *D)
187 {
188 PSI_DEBUG_PRINT(D, "PSI: CPP EVAL < %s (level=%u, skip=%u)\n",
189 exp->token->text->val, cpp->level, cpp->skip);
190
191 #if PSI_CPP_DEBUG
192 PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP exec -> ");
193 PSI_DEBUG_DUMP(cpp->parser, psi_cpp_exp_dump, exp);
194 #endif
195
196 switch (exp->type) {
197 case PSI_T_ERROR:
198 if (!cpp->skip) {
199 D->error(D, exp->token, PSI_ERROR, "%s",
200 exp->data.tok ? exp->data.tok->text->val : "");
201 }
202 break;
203 case PSI_T_WARNING:
204 if (!cpp->skip) {
205 D->error(D, exp->token, PSI_WARNING, "%s",
206 exp->data.tok ? exp->data.tok->text->val : "");
207 }
208 break;
209 case PSI_T_UNDEF:
210 if (!cpp->skip) {
211 psi_cpp_undef(cpp, exp->data.tok);
212 }
213 break;
214 case PSI_T_DEFINE:
215 if (!cpp->skip) {
216 psi_cpp_define(cpp, exp->data.decl);
217 /* FIXME: copy */
218 exp->data.decl = NULL;
219 }
220 break;
221 case PSI_T_PRAGMA:
222 if (!cpp->skip) {
223 psi_cpp_pragma(cpp, exp->data.decl);
224 }
225 break;
226 case PSI_T_IFDEF:
227 ++cpp->level;
228 if (!cpp->skip) {
229 if (psi_cpp_defined(cpp, exp->data.tok)) {
230 psi_cpp_level_mask(cpp);
231 } else {
232 psi_cpp_level_skip(cpp);
233 }
234 }
235 break;
236 case PSI_T_IFNDEF:
237 ++cpp->level;
238 if (!cpp->skip) {
239 if (psi_cpp_defined(cpp, exp->data.tok)) {
240 psi_cpp_level_skip(cpp);
241 } else {
242 psi_cpp_level_mask(cpp);
243 }
244 }
245 break;
246 case PSI_T_IF:
247 ++cpp->level;
248 if (!cpp->skip) {
249 if (psi_cpp_if(cpp, exp)) {
250 psi_cpp_level_mask(cpp);
251 } else {
252 psi_cpp_level_skip(cpp);
253 }
254 }
255 break;
256 case PSI_T_ENDIF:
257 if (!cpp->level) {
258 D->error(D, exp->token, PSI_WARNING, "Ingoring lone #endif");
259 } else {
260 psi_cpp_level_unskip(cpp);
261 psi_cpp_level_unmask(cpp);
262 --cpp->level;
263 }
264 break;
265 case PSI_T_ELSE:
266 /* FIXME: catch "else" after "else" */
267 if (!cpp->level) {
268 D->error(D, exp->token, PSI_WARNING, "Ingoring lone #else");
269 } else if (psi_cpp_level_skipped(cpp) && !psi_cpp_level_masked(cpp)) {
270 /*
271 * if skip is set on this level and the level has
272 * not been masked yet, then unskip and mask this level
273 */
274 psi_cpp_level_unskip(cpp);
275 psi_cpp_level_mask(cpp);
276 } else if (!cpp->skip && psi_cpp_level_masked(cpp)) {
277 /*
278 * previous block masked this level
279 */
280 psi_cpp_level_skip(cpp);
281 } else {
282 assert(cpp->skip <= cpp->level);
283 }
284 break;
285 case PSI_T_ELIF:
286 if (!cpp->level) {
287 D->error(D, exp->token, PSI_WARNING, "Ingoring lone #elif");
288 } else if (psi_cpp_level_skipped(cpp) && !psi_cpp_level_masked(cpp)) {
289 /*
290 * if skip is set on this level and the level has
291 * not been masked yet, then unskip and mask this
292 * level, if the condition evals truthy
293 */
294 if (psi_cpp_if(cpp, exp)) {
295 psi_cpp_level_unskip(cpp);
296 psi_cpp_level_mask(cpp);
297 }
298 } else if (!cpp->skip && psi_cpp_level_masked(cpp)) {
299 /*
300 * previous block masked this level
301 */
302 psi_cpp_level_skip(cpp);
303 } else {
304 assert(cpp->skip <= cpp->level);
305 }
306 break;
307 case PSI_T_INCLUDE:
308 if (!cpp->skip) {
309 if (!psi_cpp_include(cpp, exp->data.tok, PSI_CPP_INCLUDE)) {
310 D->error(D, exp->token, PSI_WARNING, "Failed to include %s: %s",
311 exp->data.tok->text->val, strerror(errno));
312 }
313 }
314 break;
315 case PSI_T_INCLUDE_NEXT:
316 if (!cpp->skip) {
317 if (!psi_cpp_include(cpp, exp->data.tok, PSI_CPP_INCLUDE_NEXT)) {
318 D->error(D, exp->token, PSI_WARNING, "Failed to include next %s: %s",
319 exp->data.tok->text->val, strerror(errno));
320 }
321 }
322 break;
323 case PSI_T_IMPORT:
324 if (!cpp->skip) {
325 if (!psi_cpp_include(cpp, exp->data.tok, PSI_CPP_INCLUDE_ONCE)) {
326 D->error(D, exp->token, PSI_WARNING, "Failed to include once %s: %s",
327 exp->data.tok->text->val, strerror(errno));
328 }
329 }
330 break;
331 default:
332 assert(0);
333 break;
334 }
335
336 PSI_DEBUG_PRINT(D, "PSI: CPP EVAL > %s (level=%u, skip=%u)\n",
337 exp->token->text->val, cpp->level, cpp->skip);
338 }