8073281ea7c80f12043e2d0644edbd6844dee53b
1 /*******************************************************************************
2 Copyright (c) 2017, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
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.
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 *******************************************************************************/
26 #include "php_psi_stdinc.h"
32 void psi_cpp_tokiter_dump(int fd
, struct psi_cpp
*cpp
)
34 size_t i
= cpp
->index
;
42 while (psi_plist_get(cpp
->tokens
, i
, &T
)) {
43 dprintf(fd
, "PSI: CPP tokens %5zu %c ", i
, cpp
->index
== i
? '*' : ' ');
44 psi_token_dump(fd
, T
);
45 if (i
>= cpp
->index
+ 10) {
46 dprintf(fd
, "PSI: CPP tokens .....\n");
54 void psi_cpp_tokiter_reset(struct psi_cpp
*cpp
)
57 fprintf(stderr
, "PSI: CPP reset (%zu tokens)\n", psi_plist_count(cpp
->tokens
));
58 # if PSI_CPP_DEBUG > 1
59 psi_cpp_tokiter_dump(2, cpp
);
66 bool psi_cpp_tokiter_seek(struct psi_cpp
*cpp
, size_t index
)
68 if (index
< psi_plist_count(cpp
->tokens
)) {
75 struct psi_token
*psi_cpp_tokiter_current(struct psi_cpp
*cpp
)
77 struct psi_token
*current
= NULL
;
78 bool found
= psi_plist_get(cpp
->tokens
, cpp
->index
, ¤t
);
85 size_t psi_cpp_tokiter_index(struct psi_cpp
*cpp
)
90 void psi_cpp_tokiter_next(struct psi_cpp
*cpp
)
92 #if 0 && PSI_CPP_DEBUG
93 fprintf(stderr
, "PSI: CPP next -> index=%zu -> index=%zu\n", cpp
->index
, cpp
->index
+1);
98 void psi_cpp_tokiter_prev(struct psi_cpp
*cpp
)
100 #if 0 && PSI_CPP_DEBUG
101 fprintf(stderr
, "PSI: CPP prev -> index=%zu -> index=%zu\n", cpp
->index
, cpp
->index
-1);
108 bool psi_cpp_tokiter_valid(struct psi_cpp
*cpp
)
110 #if 0 && PSI_CPP_DEBUG
111 fprintf(stderr
, "PSI: CPP valid -> index=%zu -> %d\n", cpp
->index
, cpp
->index
< psi_plist_count(cpp
->tokens
));
113 return cpp
->index
< psi_plist_count(cpp
->tokens
);
116 bool psi_cpp_tokiter_del_cur(struct psi_cpp
*cpp
, bool free_token
)
118 struct psi_token
*cur
= NULL
;
119 bool deleted
= psi_plist_del(cpp
->tokens
, cpp
->index
, &cur
);
123 fprintf(stderr
, "PSI: CPP del_cur -> index=%zu, del=%d, free=%d, count=%zu ",
124 cpp
->index
, (int) deleted
, (int) free_token
, psi_plist_count(cpp
->tokens
));
126 psi_token_dump(2, cur
);
128 fprintf(stderr
, "NULL\n");
132 psi_token_free(&cur
);
134 count
= psi_plist_count(cpp
->tokens
);
135 if (deleted
&& cpp
->index
>= count
) {
137 cpp
->index
= count
- 1;
145 bool psi_cpp_tokiter_del_range(struct psi_cpp
*cpp
, size_t offset
, size_t num_eles
, bool free_tokens
)
147 struct psi_token
**ptr
;
151 ptr
= calloc(num_eles
, sizeof(*ptr
));
157 fprintf(stderr
, "PSI: CPP del_range -> index=%zu, offset=%zu, num_eles=%zu, count=%zu\n",
158 cpp
->index
, offset
, num_eles
, psi_plist_count(cpp
->tokens
));
161 deleted
= psi_plist_del_r(cpp
->tokens
, offset
, num_eles
, (void *) ptr
);
164 size_t count
= psi_plist_count(cpp
->tokens
);
166 if (cpp
->index
>= count
) {
168 cpp
->index
= count
- 1;
176 psi_token_free(&ptr
[num_eles
]);
184 bool psi_cpp_tokiter_ins_cur(struct psi_cpp
*cpp
, struct psi_token
*tok
)
186 struct psi_plist
*tokens
= psi_plist_ins(cpp
->tokens
, cpp
->index
, &tok
);
189 fprintf(stderr
, "PSI: CPP ins_cur -> index=%zu ", cpp
->index
);
190 psi_token_dump(2, tok
);
196 cpp
->tokens
= tokens
;
200 bool psi_cpp_tokiter_ins_range(struct psi_cpp
*cpp
, size_t offset
,
201 size_t num_eles
, void **eles
)
203 struct psi_plist
*tokens
;
209 tokens
= psi_plist_ins_r(cpp
->tokens
, offset
, num_eles
, eles
);
212 fprintf(stderr
, "PSI: CPP ins_range -> index=%zu, offset=%zu, num_eles=%zu, count=%zu\n",
213 cpp
->index
, offset
, num_eles
, psi_plist_count(tokens
));
219 cpp
->tokens
= tokens
;
223 bool psi_cpp_tokiter_defined(struct psi_cpp
*cpp
)
225 if (psi_cpp_tokiter_valid(cpp
)) {
226 struct psi_token
*current
= psi_cpp_tokiter_current(cpp
);
228 return psi_cpp_defined(cpp
, current
);
234 static size_t psi_cpp_tokiter_expand_tokens(struct psi_cpp
*cpp
,
235 struct psi_token
*target
, struct psi_plist
*tokens
)
237 if (tokens
&& psi_plist_count(tokens
)) {
239 bool stringify
= false, paste
= false;
240 struct psi_token
*tok
, **exp_tokens
= calloc(psi_plist_count(tokens
), sizeof(*exp_tokens
));
242 while (psi_plist_get(tokens
, i
++, &tok
)) {
243 struct psi_token
*new_tok
;
245 if (tok
->type
== PSI_T_EOL
) {
248 if (tok
->type
== PSI_T_HASH
) {
252 if (tok
->type
== PSI_T_CPP_PASTE
) {
257 if (paste
&& n
> 0 && exp_tokens
[n
- 1]) {
258 struct psi_token
*tmp_tok
, *old_tok
= exp_tokens
[n
- 1];
260 tmp_tok
= psi_token_init(old_tok
->type
, "", 0,
261 target
->col
, target
->line
,
262 target
->file
? target
->file
: zend_empty_string
);
264 new_tok
= psi_token_cat(NULL
, 3, tmp_tok
, old_tok
, tok
);
265 psi_token_free(&old_tok
);
266 psi_token_free(&tmp_tok
);
268 exp_tokens
[n
- 1] = new_tok
;
270 new_tok
= psi_token_init(stringify
? PSI_T_QUOTED_STRING
: tok
->type
,
271 tok
->text
->val
, tok
->text
->len
, target
->col
, target
->line
,
272 target
->file
?: zend_empty_string
);
274 exp_tokens
[n
++] = new_tok
;
278 fprintf(stderr
, "PSI: CPP expand > ");
279 psi_token_dump(2, tok
);
285 psi_cpp_tokiter_ins_range(cpp
, psi_cpp_tokiter_index(cpp
), n
, (void *) exp_tokens
);
294 static void psi_cpp_tokiter_free_call_tokens(struct psi_plist
**arg_tokens_list
, size_t arg_count
, bool free_tokens
)
298 for (i
= 0; i
< arg_count
; ++i
) {
299 if (arg_tokens_list
[i
]) {
301 struct psi_token
*tok
;
303 while (psi_plist_pop(arg_tokens_list
[i
], &tok
)) {
304 psi_token_free(&tok
);
307 psi_plist_free(arg_tokens_list
[i
]);
310 free(arg_tokens_list
);
313 static struct psi_plist
**psi_cpp_tokiter_read_call_tokens(
314 struct psi_cpp
*cpp
, size_t arg_count
)
316 size_t arg_index
= 0, lparens
= 1, rparens
= 0;
317 struct psi_plist
**arg_tokens
= calloc(arg_count
, sizeof(*arg_tokens
));
318 struct psi_plist
*free_tokens
= psi_plist_init((psi_plist_dtor
) psi_token_free
);
319 struct psi_token
*tok
;
321 arg_tokens
[0] = psi_plist_init(NULL
);
323 /* next token must be a LPAREN for a macro call */
324 psi_cpp_tokiter_next(cpp
);
325 tok
= psi_cpp_tokiter_current(cpp
);
326 if (!psi_cpp_tokiter_valid(cpp
) || tok
->type
!= PSI_T_LPAREN
) {
330 /* free LPAREN on success */
331 free_tokens
= psi_plist_add(free_tokens
, &tok
);
333 while (lparens
> rparens
) {
334 psi_cpp_tokiter_next(cpp
);
335 if (!psi_cpp_tokiter_valid(cpp
)) {
338 tok
= psi_cpp_tokiter_current(cpp
);
343 arg_tokens
[arg_index
] = psi_plist_add(arg_tokens
[arg_index
], &tok
);
346 if (++rparens
== lparens
) {
348 if (arg_index
+ 1 < arg_count
) {
351 free_tokens
= psi_plist_add(free_tokens
, &tok
);
353 arg_tokens
[arg_index
] = psi_plist_add(arg_tokens
[arg_index
], &tok
);
357 if (1 == (lparens
- rparens
)) {
358 /* too many commas? */
359 if (++arg_index
>= arg_count
) {
362 free_tokens
= psi_plist_add(free_tokens
, &tok
);
364 arg_tokens
[arg_index
] = psi_plist_init(NULL
);
366 arg_tokens
[arg_index
] = psi_plist_add(arg_tokens
[arg_index
], &tok
);
370 arg_tokens
[arg_index
] = psi_plist_add(arg_tokens
[arg_index
], &tok
);
374 psi_plist_free(free_tokens
);
378 psi_cpp_tokiter_free_call_tokens(arg_tokens
, arg_count
, false);
382 static void psi_cpp_tokiter_expand_call_tokens(struct psi_cpp
*cpp
,
383 struct psi_token
*target
, struct psi_cpp_macro_decl
*macro
,
384 struct psi_plist
**arg_tokens_list
)
387 struct psi_token
*tok
;
388 struct psi_plist
*tokens
= psi_plist_init(NULL
);
390 for (i
= 0; psi_plist_get(macro
->tokens
, i
, &tok
); ++i
) {
391 struct psi_plist
*arg_tokens
= NULL
;
393 if (tok
->type
== PSI_T_NAME
) {
395 struct psi_token
*arg_name
;
397 for (s
= 0; psi_plist_get(macro
->sig
, s
, &arg_name
); ++s
) {
398 if (zend_string_equals(arg_name
->text
, tok
->text
)) {
399 arg_tokens
= arg_tokens_list
[s
];
406 tokens
= psi_plist_add_r(tokens
, psi_plist_count(arg_tokens
), psi_plist_eles(arg_tokens
));
408 tokens
= psi_plist_add(tokens
, &tok
);
412 psi_cpp_tokiter_expand_tokens(cpp
, target
, tokens
);
413 psi_plist_free(tokens
);
416 static bool psi_cpp_tokiter_expand_call(struct psi_cpp
*cpp
,
417 struct psi_token
*target
, struct psi_cpp_macro_decl
*macro
)
419 /* function-like macro
420 * #define FOO(a,b) a>b // macro->sig == {a, b}, macro->tokens = {a, >, b}
421 * # if FOO(1,2) // expands to if 1 > 2
423 size_t start
= psi_cpp_tokiter_index(cpp
);
424 struct psi_plist
**arg_tokens_list
;
426 /* read in tokens, until we have balanced parens */
427 arg_tokens_list
= psi_cpp_tokiter_read_call_tokens(cpp
, psi_plist_count(macro
->sig
));
428 if (!arg_tokens_list
) {
429 psi_cpp_tokiter_seek(cpp
, start
);
433 /* ditch arg tokens */
434 psi_cpp_tokiter_del_range(cpp
, start
, psi_cpp_tokiter_index(cpp
) - start
+ 1, false);
435 psi_cpp_tokiter_seek(cpp
, start
);
437 /* insert and expand macro tokens */
438 psi_cpp_tokiter_expand_call_tokens(cpp
, target
, macro
, arg_tokens_list
);
439 psi_cpp_tokiter_free_call_tokens(arg_tokens_list
, psi_plist_count(macro
->sig
), true);
441 /* back to where we took off */
442 psi_cpp_tokiter_seek(cpp
, start
);
444 psi_token_free(&target
);
449 static bool psi_cpp_tokiter_expand_def(struct psi_cpp
*cpp
,
450 struct psi_token
*target
, struct psi_cpp_macro_decl
*macro
)
452 size_t index
= psi_cpp_tokiter_index(cpp
);
454 /* delete current token from stream */
455 psi_cpp_tokiter_del_cur(cpp
, false);
457 if (index
!= psi_cpp_tokiter_index(cpp
)) {
458 /* might have been last token */
459 psi_cpp_tokiter_next(cpp
);
461 /* replace with tokens from macro */
462 psi_cpp_tokiter_expand_tokens(cpp
, target
, macro
->tokens
);
464 psi_token_free(&target
);
469 static inline int psi_cpp_tokiter_expand_cmp(struct psi_token
*t
,
470 struct psi_cpp_macro_decl
*m
)
472 if (psi_plist_count(m
->tokens
) == 1) {
475 psi_plist_get(m
->tokens
, 0, &r
);
477 return !zend_string_equals(r
->text
, t
->text
);
482 bool psi_cpp_tokiter_expand(struct psi_cpp
*cpp
)
484 if (psi_cpp_tokiter_valid(cpp
)) {
485 struct psi_token
*current
= psi_cpp_tokiter_current(cpp
);
488 struct psi_cpp_macro_decl
*macro
= zend_hash_find_ptr(
489 &cpp
->defs
, current
->text
);
491 /* don't expand itself */
492 if (macro
&& macro
->token
!= current
) {
494 fprintf(stderr
, "PSI: CPP expand < ");
495 psi_token_dump(2, current
);
498 return psi_cpp_tokiter_expand_call(cpp
, current
, macro
);
499 } else if (psi_cpp_tokiter_expand_cmp(current
, macro
)) {
500 return psi_cpp_tokiter_expand_def(cpp
, current
, macro
);