2df4854b47e4dfa8c598c48c2d137713577266d4
[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 #include "php_psi_stdinc.h"
27
28 #include <assert.h>
29
30 #include "data.h"
31 #include "cpp.h"
32
33 struct psi_cpp_exp *psi_cpp_exp_init(token_t type, void *data)
34 {
35 struct psi_cpp_exp *exp = calloc(1, sizeof(*exp));
36
37 switch ((exp->type = type)) {
38 case PSI_T_WARNING:
39 case PSI_T_ERROR:
40 case PSI_T_UNDEF:
41 case PSI_T_IFDEF:
42 case PSI_T_IFNDEF:
43 case PSI_T_IMPORT:
44 case PSI_T_INCLUDE:
45 case PSI_T_INCLUDE_NEXT:
46 exp->data.tok = data;
47 break;
48 case PSI_T_DEFINE:
49 exp->data.decl = data;
50 break;
51 case PSI_T_IF:
52 case PSI_T_ELIF:
53 exp->data.num = data;
54 break;
55 case PSI_T_ENDIF:
56 case PSI_T_ELSE:
57 case PSI_T_PRAGMA_ONCE:
58 break;
59 default:
60 assert(0);
61 break;
62 }
63
64 return exp;
65 }
66
67 void psi_cpp_exp_free(struct psi_cpp_exp **exp_ptr)
68 {
69 if (*exp_ptr) {
70 struct psi_cpp_exp *exp = *exp_ptr;
71
72 *exp_ptr = NULL;
73 switch (exp->type) {
74 case PSI_T_WARNING:
75 case PSI_T_ERROR:
76 if (!exp->data.tok) {
77 break;
78 }
79 /* no break */
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(int fd, struct psi_cpp_exp *exp)
109 {
110 dprintf(fd, "#%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 dprintf(fd, "%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 dprintf(fd, "<%s>", exp->data.tok->text->val);
128 } else {
129 dprintf(fd, "\"%s\"", exp->data.tok->text->val);
130 }
131 break;
132 case PSI_T_DEFINE:
133 psi_cpp_macro_decl_dump(fd, exp->data.decl);
134 break;
135 case PSI_T_IF:
136 case PSI_T_ELIF:
137 psi_num_exp_dump(fd, 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 dprintf(fd, "\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_cpp_exp_dump(2, exp);
192 #endif
193
194 switch (exp->type) {
195 case PSI_T_ERROR:
196 if (!cpp->skip) {
197 D->error(D, exp->token, PSI_ERROR, "%s",
198 exp->data.tok ? exp->data.tok->text->val : "");
199 }
200 break;
201 case PSI_T_WARNING:
202 if (!cpp->skip) {
203 D->error(D, exp->token, PSI_WARNING, "%s",
204 exp->data.tok ? exp->data.tok->text->val : "");
205 }
206 break;
207 case PSI_T_UNDEF:
208 if (!cpp->skip) {
209 psi_cpp_undef(cpp, exp->data.tok);
210 }
211 break;
212 case PSI_T_DEFINE:
213 if (!cpp->skip) {
214 psi_cpp_define(cpp, exp->data.decl);
215 /* FIXME: copy */
216 exp->data.decl = NULL;
217 }
218 break;
219 case PSI_T_IFDEF:
220 ++cpp->level;
221 if (!cpp->skip) {
222 if (psi_cpp_defined(cpp, exp->data.tok)) {
223 psi_cpp_level_mask(cpp);
224 } else {
225 psi_cpp_level_skip(cpp);
226 }
227 }
228 break;
229 case PSI_T_IFNDEF:
230 ++cpp->level;
231 if (!cpp->skip) {
232 if (psi_cpp_defined(cpp, exp->data.tok)) {
233 psi_cpp_level_skip(cpp);
234 } else {
235 psi_cpp_level_mask(cpp);
236 }
237 }
238 break;
239 case PSI_T_IF:
240 ++cpp->level;
241 if (!cpp->skip) {
242 if (psi_cpp_if(cpp, exp)) {
243 psi_cpp_level_mask(cpp);
244 } else {
245 psi_cpp_level_skip(cpp);
246 }
247 }
248 break;
249 case PSI_T_ENDIF:
250 if (!cpp->level) {
251 D->error(D, exp->token, PSI_WARNING, "Ingoring lone #endif");
252 } else {
253 psi_cpp_level_unskip(cpp);
254 psi_cpp_level_unmask(cpp);
255 --cpp->level;
256 }
257 break;
258 case PSI_T_ELSE:
259 /* FIXME: catch "else" after "else" */
260 if (!cpp->level) {
261 D->error(D, exp->token, PSI_WARNING, "Ingoring lone #else");
262 } else if (psi_cpp_level_skipped(cpp) && !psi_cpp_level_masked(cpp)) {
263 /*
264 * if skip is set on this level and the level has
265 * not been masked yet, then unskip and mask this level
266 */
267 psi_cpp_level_unskip(cpp);
268 psi_cpp_level_mask(cpp);
269 } else if (!cpp->skip && psi_cpp_level_masked(cpp)) {
270 /*
271 * previous block masked this level
272 */
273 psi_cpp_level_skip(cpp);
274 } else {
275 assert(cpp->skip <= cpp->level);
276 }
277 break;
278 case PSI_T_ELIF:
279 if (!cpp->level) {
280 D->error(D, exp->token, PSI_WARNING, "Ingoring lone #elif");
281 } else if (psi_cpp_level_skipped(cpp) && !psi_cpp_level_masked(cpp)) {
282 /*
283 * if skip is set on this level and the level has
284 * not been masked yet, then unskip and mask this
285 * level, if the condition evals truthy
286 */
287 if (psi_cpp_if(cpp, exp)) {
288 psi_cpp_level_unskip(cpp);
289 psi_cpp_level_mask(cpp);
290 }
291 } else if (!cpp->skip && psi_cpp_level_masked(cpp)) {
292 /*
293 * previous block masked this level
294 */
295 psi_cpp_level_skip(cpp);
296 } else {
297 assert(cpp->skip <= cpp->level);
298 }
299 break;
300 case PSI_T_INCLUDE:
301 if (!cpp->skip) {
302 if (!psi_cpp_include(cpp, exp->data.tok, PSI_CPP_INCLUDE)) {
303 D->error(D, exp->token, PSI_WARNING, "Failed to include %s", exp->data.tok->text->val);
304 }
305 }
306 break;
307 case PSI_T_INCLUDE_NEXT:
308 if (!cpp->skip) {
309 if (!psi_cpp_include(cpp, exp->data.tok, PSI_CPP_INCLUDE_NEXT)) {
310 D->error(D, exp->token, PSI_WARNING, "Failed to include %s", exp->data.tok->text->val);
311 }
312 }
313 break;
314 case PSI_T_IMPORT:
315 if (!cpp->skip) {
316 if (!psi_cpp_include(cpp, exp->data.tok, PSI_CPP_INCLUDE_ONCE)) {
317 D->error(D, exp->token, PSI_WARNING, "Failed to include %s", exp->data.tok->text->val);
318 }
319 }
320 break;
321 case PSI_T_PRAGMA_ONCE:
322 if (!cpp->skip) {
323 zend_hash_add_empty_element(&cpp->once, exp->token->file);
324 }
325 break;
326 default:
327 assert(0);
328 break;
329 }
330
331 PSI_DEBUG_PRINT(D, "PSI: CPP EVAL > %s (level=%u, skip=%u)\n",
332 exp->token->text->val, cpp->level, cpp->skip);
333 }