2288990fad8a04ecc6d18b00f6983c3728bf99e9
[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
37 struct psi_cpp_exp *psi_cpp_exp_init(token_t type, void *data)
38 {
39 struct psi_cpp_exp *exp = pecalloc(1, sizeof(*exp), 1);
40
41 switch ((exp->type = type)) {
42 case PSI_T_WARNING:
43 case PSI_T_ERROR:
44 case PSI_T_UNDEF:
45 case PSI_T_IFDEF:
46 case PSI_T_IFNDEF:
47 case PSI_T_IMPORT:
48 case PSI_T_INCLUDE:
49 case PSI_T_INCLUDE_NEXT:
50 exp->data.tok = data;
51 break;
52 case PSI_T_DEFINE:
53 exp->data.decl = data;
54 break;
55 case PSI_T_IF:
56 case PSI_T_ELIF:
57 exp->data.num = data;
58 break;
59 case PSI_T_ENDIF:
60 case PSI_T_ELSE:
61 case PSI_T_PRAGMA_ONCE:
62 break;
63 default:
64 assert(0);
65 break;
66 }
67
68 return exp;
69 }
70
71 void psi_cpp_exp_free(struct psi_cpp_exp **exp_ptr)
72 {
73 if (*exp_ptr) {
74 struct psi_cpp_exp *exp = *exp_ptr;
75
76 *exp_ptr = NULL;
77 switch (exp->type) {
78 case PSI_T_WARNING:
79 case PSI_T_ERROR:
80 case PSI_T_UNDEF:
81 case PSI_T_IFDEF:
82 case PSI_T_IFNDEF:
83 case PSI_T_IMPORT:
84 case PSI_T_INCLUDE:
85 case PSI_T_INCLUDE_NEXT:
86 psi_token_free(&exp->data.tok);
87 break;
88 case PSI_T_DEFINE:
89 psi_cpp_macro_decl_free(&exp->data.decl);
90 break;
91 case PSI_T_IF:
92 case PSI_T_ELIF:
93 psi_num_exp_free(&exp->data.num);
94 break;
95 case PSI_T_ENDIF:
96 case PSI_T_ELSE:
97 case PSI_T_PRAGMA_ONCE:
98 break;
99 default:
100 assert(0);
101 break;
102 }
103 psi_token_free(&exp->token);
104 free(exp);
105 }
106 }
107
108 void psi_cpp_exp_dump(struct psi_dump *dump, struct psi_cpp_exp *exp)
109 {
110 PSI_DUMP(dump, "#%s ", exp->token->text->val);
111 switch (exp->type) {
112 case PSI_T_WARNING:
113 case PSI_T_ERROR:
114 if (!exp->data.tok) {
115 break;
116 }
117 /* no break */
118 case PSI_T_UNDEF:
119 case PSI_T_IFDEF:
120 case PSI_T_IFNDEF:
121 PSI_DUMP(dump, "%s", exp->data.tok->text->val);
122 break;
123 case PSI_T_IMPORT:
124 case PSI_T_INCLUDE:
125 case PSI_T_INCLUDE_NEXT:
126 if (exp->data.tok->type == PSI_T_CPP_HEADER) {
127 PSI_DUMP(dump, "<%s>", exp->data.tok->text->val);
128 } else {
129 PSI_DUMP(dump, "\"%s\"", exp->data.tok->text->val);
130 }
131 break;
132 case PSI_T_DEFINE:
133 psi_cpp_macro_decl_dump(dump, exp->data.decl);
134 break;
135 case PSI_T_IF:
136 case PSI_T_ELIF:
137 psi_num_exp_dump(dump, exp->data.num);
138 break;
139 case PSI_T_ENDIF:
140 case PSI_T_ELSE:
141 case PSI_T_PRAGMA_ONCE:
142 break;
143 default:
144 assert(0);
145 break;
146 }
147 PSI_DUMP(dump, "\n");
148 }
149
150
151 static inline bool psi_cpp_level_skipped(struct psi_cpp *cpp)
152 {
153 return cpp->skip == cpp->level;
154 }
155
156 static inline void psi_cpp_level_skip(struct psi_cpp *cpp)
157 {
158 assert(!cpp->skip);
159 cpp->skip = cpp->level;
160 }
161
162 static inline void psi_cpp_level_unskip(struct psi_cpp *cpp)
163 {
164 if (psi_cpp_level_skipped(cpp)) {
165 cpp->skip = 0;
166 }
167 }
168
169 static inline bool psi_cpp_level_masked(struct psi_cpp *cpp)
170 {
171 return cpp->seen & (1 << cpp->level);
172 }
173
174 static inline void psi_cpp_level_mask(struct psi_cpp *cpp)
175 {
176 assert(!psi_cpp_level_masked(cpp));
177 cpp->seen |= (1 << cpp->level);
178 }
179
180 static inline void psi_cpp_level_unmask(struct psi_cpp *cpp)
181 {
182 cpp->seen &= ~(1 << cpp->level);
183 }
184
185 void psi_cpp_exp_exec(struct psi_cpp_exp *exp, struct psi_cpp *cpp, struct psi_data *D)
186 {
187 PSI_DEBUG_PRINT(D, "PSI: CPP EVAL < %s (level=%u, skip=%u)\n",
188 exp->token->text->val, cpp->level, cpp->skip);
189
190 #if PSI_CPP_DEBUG
191 PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP exec -> ");
192 PSI_DEBUG_DUMP(cpp->parser, psi_cpp_exp_dump, exp);
193 #endif
194
195 switch (exp->type) {
196 case PSI_T_ERROR:
197 if (!cpp->skip) {
198 D->error(D, exp->token, PSI_ERROR, "%s",
199 exp->data.tok ? exp->data.tok->text->val : "");
200 }
201 break;
202 case PSI_T_WARNING:
203 if (!cpp->skip) {
204 D->error(D, exp->token, PSI_WARNING, "%s",
205 exp->data.tok ? exp->data.tok->text->val : "");
206 }
207 break;
208 case PSI_T_UNDEF:
209 if (!cpp->skip) {
210 psi_cpp_undef(cpp, exp->data.tok);
211 }
212 break;
213 case PSI_T_DEFINE:
214 if (!cpp->skip) {
215 psi_cpp_define(cpp, exp->data.decl);
216 /* FIXME: copy */
217 exp->data.decl = NULL;
218 }
219 break;
220 case PSI_T_IFDEF:
221 ++cpp->level;
222 if (!cpp->skip) {
223 if (psi_cpp_defined(cpp, exp->data.tok)) {
224 psi_cpp_level_mask(cpp);
225 } else {
226 psi_cpp_level_skip(cpp);
227 }
228 }
229 break;
230 case PSI_T_IFNDEF:
231 ++cpp->level;
232 if (!cpp->skip) {
233 if (psi_cpp_defined(cpp, exp->data.tok)) {
234 psi_cpp_level_skip(cpp);
235 } else {
236 psi_cpp_level_mask(cpp);
237 }
238 }
239 break;
240 case PSI_T_IF:
241 ++cpp->level;
242 if (!cpp->skip) {
243 if (psi_cpp_if(cpp, exp)) {
244 psi_cpp_level_mask(cpp);
245 } else {
246 psi_cpp_level_skip(cpp);
247 }
248 }
249 break;
250 case PSI_T_ENDIF:
251 if (!cpp->level) {
252 D->error(D, exp->token, PSI_WARNING, "Ingoring lone #endif");
253 } else {
254 psi_cpp_level_unskip(cpp);
255 psi_cpp_level_unmask(cpp);
256 --cpp->level;
257 }
258 break;
259 case PSI_T_ELSE:
260 /* FIXME: catch "else" after "else" */
261 if (!cpp->level) {
262 D->error(D, exp->token, PSI_WARNING, "Ingoring lone #else");
263 } else if (psi_cpp_level_skipped(cpp) && !psi_cpp_level_masked(cpp)) {
264 /*
265 * if skip is set on this level and the level has
266 * not been masked yet, then unskip and mask this level
267 */
268 psi_cpp_level_unskip(cpp);
269 psi_cpp_level_mask(cpp);
270 } else if (!cpp->skip && psi_cpp_level_masked(cpp)) {
271 /*
272 * previous block masked this level
273 */
274 psi_cpp_level_skip(cpp);
275 } else {
276 assert(cpp->skip <= cpp->level);
277 }
278 break;
279 case PSI_T_ELIF:
280 if (!cpp->level) {
281 D->error(D, exp->token, PSI_WARNING, "Ingoring lone #elif");
282 } else if (psi_cpp_level_skipped(cpp) && !psi_cpp_level_masked(cpp)) {
283 /*
284 * if skip is set on this level and the level has
285 * not been masked yet, then unskip and mask this
286 * level, if the condition evals truthy
287 */
288 if (psi_cpp_if(cpp, exp)) {
289 psi_cpp_level_unskip(cpp);
290 psi_cpp_level_mask(cpp);
291 }
292 } else if (!cpp->skip && psi_cpp_level_masked(cpp)) {
293 /*
294 * previous block masked this level
295 */
296 psi_cpp_level_skip(cpp);
297 } else {
298 assert(cpp->skip <= cpp->level);
299 }
300 break;
301 case PSI_T_INCLUDE:
302 if (!cpp->skip) {
303 if (!psi_cpp_include(cpp, exp->data.tok, PSI_CPP_INCLUDE)) {
304 D->error(D, exp->token, PSI_WARNING, "Failed to include %s: %s",
305 exp->data.tok->text->val, strerror(errno));
306 }
307 }
308 break;
309 case PSI_T_INCLUDE_NEXT:
310 if (!cpp->skip) {
311 if (!psi_cpp_include(cpp, exp->data.tok, PSI_CPP_INCLUDE_NEXT)) {
312 D->error(D, exp->token, PSI_WARNING, "Failed to include next %s: %s",
313 exp->data.tok->text->val, strerror(errno));
314 }
315 }
316 break;
317 case PSI_T_IMPORT:
318 if (!cpp->skip) {
319 if (!psi_cpp_include(cpp, exp->data.tok, PSI_CPP_INCLUDE_ONCE)) {
320 D->error(D, exp->token, PSI_WARNING, "Failed to include once %s: %s",
321 exp->data.tok->text->val, strerror(errno));
322 }
323 }
324 break;
325 case PSI_T_PRAGMA_ONCE:
326 if (!cpp->skip) {
327 zend_hash_add_empty_element(&cpp->once, exp->token->file);
328 }
329 break;
330 default:
331 assert(0);
332 break;
333 }
334
335 PSI_DEBUG_PRINT(D, "PSI: CPP EVAL > %s (level=%u, skip=%u)\n",
336 exp->token->text->val, cpp->level, cpp->skip);
337 }