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