OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
-#include "php_psi_stdinc.h"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#else
+# include "php_config.h"
+#endif
#include "cpp.h"
#include "parser.h"
-
-void psi_cpp_tokiter_dump(int fd, struct psi_cpp *cpp)
+void psi_cpp_tokiter_dump(struct psi_dump *dump, struct psi_cpp *cpp);
+void psi_cpp_tokiter_dump(struct psi_dump *dump, struct psi_cpp *cpp)
{
- size_t i;
+ size_t i = cpp->index;
struct psi_token *T;
- for (i = 0; psi_plist_get(cpp->tokens, i, &T); ++i) {
- dprintf(fd, "PSI: CPP tokens %5zu %c ", i, cpp->index == i ? '*' : ' ');
- psi_token_dump(fd, T);
+ if (i > 40) {
+ i -= 40;
+ } else {
+ i = 0;
+ }
+ while (psi_plist_get(cpp->tokens.iter, i, &T)) {
+ PSI_DUMP(dump, "PSI: CPP tokens %5zu %c ", i, cpp->index == i ? '*' : ' ');
+ if (T) {
+ psi_token_dump(dump, T);
+ } else {
+ PSI_DUMP(dump, "TOKEN deleted\n");
+ }
+ if (i >= cpp->index + 40) {
+ PSI_DUMP(dump, "PSI: CPP tokens .....\n");
+ break;
+ }
+ ++i;
}
}
void psi_cpp_tokiter_reset(struct psi_cpp *cpp)
{
#if PSI_CPP_DEBUG
- fprintf(stderr, "PSI: CPP reset (%zu tokens)\n", psi_plist_count(cpp->tokens));
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP reset -> iter.count=%zu, next.count=%zu\n",
+ psi_plist_count(cpp->tokens.iter),
+ psi_plist_count(cpp->tokens.next));
# if PSI_CPP_DEBUG > 1
- psi_cpp_tokiter_dump(2, cpp);
+ PSI_DEBUG_DUMP(cpp->parser, psi_cpp_tokiter_dump, cpp);
# endif
#endif
cpp->index = 0;
cpp->expanded = 0;
+ cpp->skip = 0;
+ cpp->seen = 0;
+ cpp->level = 0;
+
+ if (cpp->tokens.next) {
+ free(cpp->tokens.iter);
+ cpp->tokens.iter = cpp->tokens.next;
+ }
+ cpp->tokens.next = psi_plist_init((psi_plist_dtor) psi_token_free);
}
bool psi_cpp_tokiter_seek(struct psi_cpp *cpp, size_t index)
{
- if (index < psi_plist_count(cpp->tokens)) {
+ if (index < psi_plist_count(cpp->tokens.iter)) {
cpp->index = index;
return true;
}
struct psi_token *psi_cpp_tokiter_current(struct psi_cpp *cpp)
{
struct psi_token *current = NULL;
- bool found = psi_plist_get(cpp->tokens, cpp->index, ¤t);
+ bool found = psi_plist_get(cpp->tokens.iter, cpp->index, ¤t);
assert(found);
return cpp->index;
}
-void psi_cpp_tokiter_next(struct psi_cpp *cpp)
+bool psi_cpp_tokiter_add_cur(struct psi_cpp *cpp)
{
-#if 0 && PSI_CPP_DEBUG
- fprintf(stderr, "PSI: CPP next -> index=%zu -> index=%zu\n", cpp->index, cpp->index+1);
+ struct psi_token *cur = NULL;
+
+ if (psi_plist_get(cpp->tokens.iter, cpp->index, &cur)) {
+ struct psi_plist *tokens = psi_plist_add(cpp->tokens.next, &cur);
+
+ if (tokens) {
+ cpp->tokens.next = tokens;
+
+#if PSI_CPP_DEBUG
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP add_cur -> index=%zu, iter.count=%zu, next.count=%zu ",
+ cpp->index, psi_plist_count(cpp->tokens.iter), psi_plist_count(cpp->tokens.next));
+ PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, cur);
#endif
- ++cpp->index;
+
+ return true;
+ }
+ }
+
+ return false;
}
-void psi_cpp_tokiter_prev(struct psi_cpp *cpp)
+bool psi_cpp_tokiter_add(struct psi_cpp *cpp, struct psi_token *tok)
{
-#if 0 && PSI_CPP_DEBUG
- fprintf(stderr, "PSI: CPP prev -> index=%zu -> index=%zu\n", cpp->index, cpp->index-1);
+ struct psi_plist *tokens = psi_plist_add(cpp->tokens.next, &tok);
+
+ if (!tokens) {
+ return false;
+ }
+ cpp->tokens.next = tokens;
+
+#if PSI_CPP_DEBUG
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP add -> index=%zu, iter.count=%zu, next.count=%zu ",
+ cpp->index, psi_plist_count(cpp->tokens.iter), psi_plist_count(cpp->tokens.next));
+ PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, tok);
#endif
- if (cpp->index) {
- --cpp->index;
+
+ return true;
+}
+
+
+bool psi_cpp_tokiter_add_range(struct psi_cpp *cpp, size_t num_eles, void **eles)
+{
+ struct psi_plist *tokens;
+
+ if (!num_eles) {
+ return true;
+ }
+
+ tokens = psi_plist_add_r(cpp->tokens.next, num_eles, eles);
+ if (!tokens) {
+ return false;
}
+ cpp->tokens.next = tokens;
+
+#if PSI_CPP_DEBUG
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP add_range -> index=%zu, num_eles=%zu, iter.count=%zu, next.count=%zu\n",
+ cpp->index, num_eles, psi_plist_count(cpp->tokens.iter), psi_plist_count(cpp->tokens.next));
+#endif
+
+ return true;
+}
+
+
+void psi_cpp_tokiter_next(struct psi_cpp *cpp)
+{
+#if 0 && PSI_CPP_DEBUG
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP next -> index=%zu -> index=%zu\n",
+ cpp->index, cpp->index+1);
+#endif
+ ++cpp->index;
}
bool psi_cpp_tokiter_valid(struct psi_cpp *cpp)
{
#if 0 && PSI_CPP_DEBUG
- fprintf(stderr, "PSI: CPP valid -> index=%zu -> %d\n", cpp->index, cpp->index < psi_plist_count(cpp->tokens));
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP valid -> index=%zu -> %d\n",
+ cpp->index, cpp->index < psi_plist_count(cpp->tokens.iter));
#endif
- return cpp->index < psi_plist_count(cpp->tokens);
+ return cpp->index < psi_plist_count(cpp->tokens.iter);
}
-bool psi_cpp_tokiter_del_cur(struct psi_cpp *cpp, bool free_token)
+bool psi_cpp_tokiter_del_prev(struct psi_cpp *cpp, bool free_token)
{
struct psi_token *cur = NULL;
- bool deleted = psi_plist_del(cpp->tokens, cpp->index, &cur);
- size_t count;
#if PSI_CPP_DEBUG
- fprintf(stderr, "PSI: CPP del_cur -> index=%zu, del=%d, free=%d, count=%zu ",
- cpp->index, (int) deleted, (int) free_token, psi_plist_count(cpp->tokens));
- if (cur) {
- psi_token_dump(2, cur);
- } else {
- fprintf(stderr, "NULL\n");
- }
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP del_prev -> index=%zu, iter.count=%zu, next.count=%zu\n",
+ cpp->index, psi_plist_count(cpp->tokens.iter), psi_plist_count(cpp->tokens.next));
#endif
- if (cur && free_token) {
- free(cur);
- }
- count = psi_plist_count(cpp->tokens);
- if (deleted && cpp->index >= count) {
- if (count > 0) {
- cpp->index = count - 1;
- } else {
- cpp->index = 0;
+
+ if (psi_plist_pop(cpp->tokens.next, NULL) && psi_plist_get(cpp->tokens.iter, cpp->index - 1, &cur)) {
+ psi_plist_unset(cpp->tokens.iter, cpp->index - 1);
+ if (free_token && cur) {
+ psi_token_free(&cur);
}
+ return true;
}
- return deleted;
-}
-bool psi_cpp_tokiter_del_range(struct psi_cpp *cpp, size_t offset, size_t num_eles, bool free_tokens)
+ return false;
+}
+bool psi_cpp_tokiter_del_cur(struct psi_cpp *cpp, bool free_token)
{
- struct psi_token **ptr;
- bool deleted;
-
- if (free_tokens) {
- ptr = calloc(num_eles, sizeof(*ptr));
- } else {
- ptr = NULL;
- }
+ struct psi_token *cur = NULL;
#if PSI_CPP_DEBUG
- fprintf(stderr, "PSI: CPP del_range -> index=%zu, offset=%zu, num_eles=%zu, count=%zu\n",
- cpp->index, offset, num_eles, psi_plist_count(cpp->tokens));
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP del_cur -> index=%zu, iter.count=%zu, next.count=%zu ",
+ cpp->index, psi_plist_count(cpp->tokens.iter), psi_plist_count(cpp->tokens.next));
#endif
- deleted = psi_plist_del_r(cpp->tokens, offset, num_eles, (void *) ptr);
-
- if (deleted) {
- if (cpp->index >= psi_plist_count(cpp->tokens)) {
- cpp->index = MAX(0, psi_plist_count(cpp->tokens)-1);
- }
- if (free_tokens) {
- while (num_eles--) {
- if (ptr[num_eles]) {
- free(ptr[num_eles]);
- }
- }
- free(ptr);
+ if (psi_plist_get(cpp->tokens.iter, cpp->index, &cur)) {
+#if PSI_CPP_DEBUG
+ PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, cur);
+#endif
+ psi_plist_unset(cpp->tokens.iter, cpp->index);
+ if (free_token && cur) {
+ psi_token_free(&cur);
}
+ ++cpp->index;
+ return true;
}
- return deleted;
+
+ return false;
}
-bool psi_cpp_tokiter_ins_cur(struct psi_cpp *cpp, struct psi_token *tok)
+bool psi_cpp_tokiter_del_range(struct psi_cpp *cpp, size_t offset, size_t num_eles, bool free_tokens)
{
- struct psi_plist *tokens = psi_plist_ins(cpp->tokens, cpp->index, &tok);
+ struct psi_token *ptr;
+ size_t i;
#if PSI_CPP_DEBUG
- fprintf(stderr, "PSI: CPP ins_cur -> index=%zu ", cpp->index);
- psi_token_dump(2, tok);
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP del_range -> index=%zu, offset=%zu, num_eles=%zu, iter.count=%zu, next.count=%zu\n",
+ cpp->index, offset, num_eles, psi_plist_count(cpp->tokens.iter), psi_plist_count(cpp->tokens.next));
#endif
- if (!tokens) {
- return false;
+ for (i = offset; i < offset + num_eles; ++i) {
+ if (!psi_plist_get(cpp->tokens.iter, i, &ptr)) {
+ return false;
+ }
+#if PSI_CPP_DEBUG
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP del_range -> ");
+ PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, ptr);
+#endif
+ psi_plist_unset(cpp->tokens.iter, i);
+ if (free_tokens && ptr) {
+ psi_token_free(&ptr);
+ }
}
- cpp->tokens = tokens;
+
+ cpp->index = i;
return true;
}
-bool psi_cpp_tokiter_ins_range(struct psi_cpp *cpp, size_t offset,
- size_t num_eles, void **eles)
+bool psi_cpp_tokiter_ins_range(struct psi_cpp *cpp, size_t num_eles, void **eles)
{
struct psi_plist *tokens;
return true;
}
- tokens = psi_plist_ins_r(cpp->tokens, offset, num_eles, eles);
+ tokens = psi_plist_ins_r(cpp->tokens.iter, cpp->index, num_eles, eles);
+ if (!tokens) {
+ return false;
+ }
+ cpp->tokens.iter = tokens;
#if PSI_CPP_DEBUG
- fprintf(stderr, "PSI: CPP ins_range -> index=%zu, offset=%zu, num_eles=%zu, count=%zu\n",
- cpp->index, offset, num_eles, psi_plist_count(tokens));
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP ins_range -> index=%zu, num_eles=%zu, iter.count=%zu, next.count=%zu\n",
+ cpp->index, num_eles, psi_plist_count(cpp->tokens.iter), psi_plist_count(cpp->tokens.next));
#endif
- if (!tokens) {
- return false;
- }
- cpp->tokens = tokens;
return true;
}
return false;
}
-void psi_cpp_tokiter_expand_tokens(struct psi_cpp *cpp, struct psi_plist *tokens)
+static inline size_t psi_cpp_tokiter_expand_tokens(struct psi_cpp *cpp,
+ struct psi_token *target, struct psi_plist *tokens)
{
- if (tokens && psi_plist_count(tokens)) {
- size_t i = 0, n = 0;
- bool stringify = false, paste = false;
- struct psi_token *tok, **exp_tokens = calloc(psi_plist_count(tokens), sizeof(*exp_tokens));
-
- while (psi_plist_get(tokens, i++, &tok)) {
- struct psi_token *new_tok;
-
- if (tok->type == PSI_T_HASH) {
- if (stringify) {
- stringify = false;
- paste = true;
+ size_t i = 0, n = 0;
+ bool stringify = false, paste = false;
+ struct psi_token *tok;
+ struct psi_plist *exp;
+
+ if (!psi_plist_count(tokens)) {
+ return 0;
+ }
+
+ exp = psi_plist_init(NULL);
+ while (psi_plist_get(tokens, i++, &tok)) {
+ struct psi_token *new_tok;
+
+ if (tok->type == PSI_T_EOL) {
+ continue;
+ }
+ if (tok->type == PSI_T_HASH) {
+ stringify = true;
+ continue;
+ }
+ if (tok->type == PSI_T_CPP_PASTE) {
+ paste = true;
+ continue;
+ }
+
+ if (paste && psi_plist_count(exp)) {
+ struct psi_token *old_tok;
+ struct psi_parser_input *toscan;
+
+ psi_plist_pop(exp, &old_tok);
+ new_tok = psi_token_cat(NULL, 2, old_tok, tok);
+
+ /* reclassify token(s) */
+ if ((toscan = psi_parser_open_string(cpp->parser, new_tok->text->val, new_tok->text->len))) {
+ struct psi_plist *scanned;
+
+ scanned = psi_parser_scan(cpp->parser, toscan);
+ if (psi_plist_count(scanned)) {
+ size_t i = 0;
+ struct psi_token *tmp_tok;
+
+ exp = psi_plist_add_r(exp, psi_plist_count(scanned), psi_plist_eles(scanned));
+
+ /* fix meta info */
+ while (psi_plist_get(scanned, i++, &tmp_tok)) {
+ zend_string_release(tmp_tok->file);
+ tmp_tok->file = zend_string_copy(new_tok->file);
+ tmp_tok->line = new_tok->line;
+ tmp_tok->col = new_tok->col;
+ }
+
+ psi_token_free(&new_tok);
+ psi_plist_top(scanned, &new_tok);
} else {
- stringify = true;
+ exp = psi_plist_add(exp, &new_tok);
+ }
+ if (scanned) {
+ free(scanned);
}
- continue;
+ psi_parser_input_free(&toscan);
+ } else {
+ exp = psi_plist_add(exp, &new_tok);
}
- if (paste && n > 0 && exp_tokens[n - 1] &&
- (new_tok = psi_token_cat(NULL, 2, exp_tokens[n - 1], tok))) {
- free(exp_tokens[n - 1]);
- exp_tokens[n - 1] = new_tok;
- } else {
- struct psi_token *cpy = psi_token_copy(tok);
+ psi_token_free(&old_tok);
+ } else {
+ new_tok = psi_token_init(stringify ? PSI_T_QUOTED_STRING : tok->type,
+ tok->text->val, tok->text->len, target->col, target->line,
+ target->file ?: zend_empty_string);
- if (stringify) {
- cpy = psi_token_append(NULL,
- psi_token_prepend(NULL, cpy, 1, "\""), 1, "\"");
- cpy->type = PSI_T_QUOTED_STRING;
- }
- exp_tokens[n++] = cpy;
- }
+ exp = psi_plist_add(exp, &new_tok);
+ }
#if PSI_CPP_DEBUG
- fprintf(stderr, "PSI: CPP expand > ");
- psi_token_dump(2, tok);
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP expand > ");
+ PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, new_tok);
#endif
- paste = false;
- stringify = false;
- }
- psi_cpp_tokiter_ins_range(cpp, psi_cpp_tokiter_index(cpp), n, (void *) exp_tokens);
- free(exp_tokens);
+
+ paste = false;
+ stringify = false;
}
+
+ n = psi_plist_count(exp);
+ psi_cpp_tokiter_ins_range(cpp, n, psi_plist_eles(exp));
+ free(exp);
+
+ return n;
}
-static void psi_cpp_tokiter_free_call_tokens(struct psi_plist **arg_tokens_list, size_t arg_count, bool free_tokens)
+static inline void psi_cpp_tokiter_free_call_tokens(
+ struct psi_plist **arg_tokens_list, size_t arg_count, bool free_tokens)
{
size_t i;
struct psi_token *tok;
while (psi_plist_pop(arg_tokens_list[i], &tok)) {
- free(tok);
+ psi_token_free(&tok);
}
}
psi_plist_free(arg_tokens_list[i]);
free(arg_tokens_list);
}
-static struct psi_plist **psi_cpp_tokiter_read_call_tokens(
+static inline struct psi_plist **psi_cpp_tokiter_read_call_tokens(
struct psi_cpp *cpp, size_t arg_count)
{
- size_t arg_index = 0, lparens = 1, rparens = 0;
- struct psi_plist **arg_tokens = calloc(arg_count, sizeof(*arg_tokens));
+ size_t arg_index = 0, lparens = 1, rparens = 0, start = psi_cpp_tokiter_index(cpp);
+ struct psi_plist **arg_tokens = pecalloc(arg_count, sizeof(*arg_tokens), 1);
struct psi_plist *free_tokens = psi_plist_init((psi_plist_dtor) psi_token_free);
struct psi_token *tok;
arg_tokens[0] = psi_plist_init(NULL);
- /* free macro name token on success */
- tok = psi_cpp_tokiter_current(cpp);
- free_tokens = psi_plist_add(free_tokens, &tok);
-
/* next token must be a LPAREN for a macro call */
psi_cpp_tokiter_next(cpp);
tok = psi_cpp_tokiter_current(cpp);
}
}
+ /* ditch arg tokens */
+ psi_cpp_tokiter_del_range(cpp, start, psi_cpp_tokiter_index(cpp) - start + 1, false);
psi_plist_free(free_tokens);
+
return arg_tokens;
fail:
return NULL;
}
-static void psi_cpp_tokiter_expand_call_tokens(struct psi_cpp *cpp,
- struct psi_cpp_macro_decl *macro, struct psi_plist **arg_tokens_list)
+static inline void psi_cpp_tokiter_expand_call_tokens(struct psi_cpp *cpp,
+ struct psi_token *target, struct psi_cpp_macro_decl *macro,
+ struct psi_plist **arg_tokens_list)
{
size_t i;
struct psi_token *tok;
struct psi_plist *tokens = psi_plist_init(NULL);
+ bool prescan = true;
for (i = 0; psi_plist_get(macro->tokens, i, &tok); ++i) {
struct psi_plist *arg_tokens = NULL;
- if (tok->type == PSI_T_NAME) {
+ if (tok->type == PSI_T_HASH || tok->type == PSI_T_CPP_PASTE) {
+ prescan = false;
+ } else if (tok->type == PSI_T_NAME) {
size_t s;
struct psi_token *arg_name;
for (s = 0; psi_plist_get(macro->sig, s, &arg_name); ++s) {
- if (arg_name->size == tok->size) {
- if (!memcmp(arg_name->text, tok->text, tok->size)) {
- arg_tokens = arg_tokens_list[s];
- break;
+ if (zend_string_equals(arg_name->text, tok->text)) {
+ if (prescan) {
+ bool processed = psi_cpp_process(cpp, &arg_tokens_list[s]);
+
+ assert(processed);
}
+ arg_tokens = arg_tokens_list[s];
+ break;
}
}
}
}
}
- psi_cpp_tokiter_expand_tokens(cpp, tokens);
+ psi_cpp_tokiter_expand_tokens(cpp, target, tokens);
psi_plist_free(tokens);
}
-static bool psi_cpp_tokiter_expand_call(struct psi_cpp *cpp,
- struct psi_cpp_macro_decl *macro)
+static inline bool psi_cpp_tokiter_expand_call(struct psi_cpp *cpp,
+ struct psi_token *target, struct psi_cpp_macro_decl *macro)
{
/* function-like macro
* #define FOO(a,b) a>b // macro->sig == {a, b}, macro->tokens = {a, >, b}
* # if FOO(1,2) // expands to if 1 > 2
*/
- size_t start = psi_cpp_tokiter_index(cpp);
+ size_t start = psi_cpp_tokiter_index(cpp), argc = psi_plist_count(macro->sig);
struct psi_plist **arg_tokens_list;
+
/* read in tokens, until we have balanced parens */
- arg_tokens_list = psi_cpp_tokiter_read_call_tokens(cpp, psi_plist_count(macro->sig));
+ arg_tokens_list = psi_cpp_tokiter_read_call_tokens(cpp, argc);
if (!arg_tokens_list) {
psi_cpp_tokiter_seek(cpp, start);
return false;
}
- /* ditch arg tokens */
- psi_cpp_tokiter_del_range(cpp, start, psi_cpp_tokiter_index(cpp) - start + 1, false);
- psi_cpp_tokiter_seek(cpp, start);
+ /* insert and expand macro tokens */
+ psi_cpp_tokiter_expand_call_tokens(cpp, target, macro, arg_tokens_list);
+ psi_cpp_tokiter_free_call_tokens(arg_tokens_list, argc, true);
+
+ psi_token_free(&target);
+ ++cpp->expanded;
+ return true;
+}
+
+static inline void psi_cpp_tokiter_expand_builtin_tokens(struct psi_cpp *cpp,
+ struct psi_token *target, struct psi_builtin *builtin,
+ struct psi_plist **arg_tokens_list)
+{
+ size_t s;
+ struct psi_plist *res = NULL;
+ size_t argc = psi_plist_count(builtin->decl->sig);
+
+ /* prescan */
+ for (s = 0; s < argc; ++s) {
+ bool processed = psi_cpp_process(cpp, &arg_tokens_list[s]);
+ assert(processed);
+ }
/* insert and expand macro tokens */
- psi_cpp_tokiter_expand_call_tokens(cpp, macro, arg_tokens_list);
- psi_cpp_tokiter_free_call_tokens(arg_tokens_list, psi_plist_count(macro->sig), true);
+ if (!builtin->func(cpp, target, arg_tokens_list, &res)) {
+ struct psi_token *zero = psi_token_init(PSI_T_NUMBER, "0", 1,
+ target->col, target->line, target->file);
+ psi_cpp_tokiter_ins_range(cpp, 1, (void *) &zero);
+ } else if (!res) {
+ struct psi_token *one = psi_token_init(PSI_T_NUMBER, "1", 1,
+ target->col, target->line, target->file);
+ psi_cpp_tokiter_ins_range(cpp, 1, (void *) &one);
+ } else {
+ psi_cpp_tokiter_expand_tokens(cpp, target, res);
+ psi_plist_free(res);
+ }
+}
- /* back to where we took off */
- psi_cpp_tokiter_seek(cpp, start);
+static inline bool psi_cpp_tokiter_expand_builtin(struct psi_cpp *cpp,
+ struct psi_token *target, struct psi_builtin *builtin)
+{
+ size_t start = psi_cpp_tokiter_index(cpp), argc = 0;
+ struct psi_plist **arg_tokens_list = NULL;
+
+ if (builtin->decl->sig) {
+ argc = psi_plist_count(builtin->decl->sig);
+ /* read in tokens, until we have balanced parens */
+ arg_tokens_list = psi_cpp_tokiter_read_call_tokens(cpp, argc);
+ if (!arg_tokens_list) {
+ psi_cpp_tokiter_seek(cpp, start);
+ return false;
+ }
+ } else {
+ psi_cpp_tokiter_next(cpp);
+ }
+
+ psi_cpp_tokiter_expand_builtin_tokens(cpp, target, builtin, arg_tokens_list);
+ if (arg_tokens_list) {
+ psi_cpp_tokiter_free_call_tokens(arg_tokens_list, argc, true);
+ }
+
+ psi_token_free(&target);
+ ++cpp->expanded;
+ return true;
+}
+
+
+static inline bool psi_cpp_tokiter_expand_def(struct psi_cpp *cpp,
+ struct psi_token *target, struct psi_cpp_macro_decl *macro)
+{
+ /* delete current token from stream */
+ psi_cpp_tokiter_del_cur(cpp, false);
+ /* replace with tokens from macro */
+ psi_cpp_tokiter_expand_tokens(cpp, target, macro->tokens);
+
+ psi_token_free(&target);
++cpp->expanded;
return true;
}
+static inline int psi_cpp_tokiter_expand_cmp(struct psi_token *t,
+ struct psi_cpp_macro_decl *m)
+{
+ if (psi_plist_count(m->tokens) == 1) {
+ struct psi_token *r;
+
+ if (psi_plist_get(m->tokens, 0, &r) && r->text) {
+ return !zend_string_equals(r->text, t->text);
+ }
+ }
+ return -1;
+}
+
+static inline bool psi_cpp_tokiter_expand_decl(struct psi_cpp *cpp,
+ struct psi_token *target, struct psi_cpp_macro_decl *macro)
+{
+ if (!macro) {
+ return false;
+ }
+
+ /* don't expand itself */
+ if (macro->token == target) {
+ return false;
+ }
+
+ if (macro->sig) {
+ return psi_cpp_tokiter_expand_call(cpp, target, macro);
+ } else if (psi_cpp_tokiter_expand_cmp(target, macro)) {
+ return psi_cpp_tokiter_expand_def(cpp, target, macro);
+ } else {
+ return false;
+ }
+}
+
bool psi_cpp_tokiter_expand(struct psi_cpp *cpp)
{
- if (psi_cpp_tokiter_valid(cpp)) {
- struct psi_token *current = psi_cpp_tokiter_current(cpp);
+ struct psi_token *current;
- if (current) {
- struct psi_cpp_macro_decl *macro = zend_hash_str_find_ptr(
- cpp->defs, current->text, current->size);
+ if (!psi_cpp_tokiter_valid(cpp)) {
+ return false;
+ }
- /* don't expand itself */
- if (macro && macro->token != current) {
+ current = psi_cpp_tokiter_current(cpp);
+ if (!current) {
+ return false;
+ }
#if PSI_CPP_DEBUG
- fprintf(stderr, "PSI: CPP expand < ");
- psi_token_dump(2, current);
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP expand < ");
+ PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, current);
#endif
- if (macro->sig) {
- return psi_cpp_tokiter_expand_call(cpp, macro);
- } else {
- size_t index = psi_cpp_tokiter_index(cpp);
-
- /* delete current token from stream */
- psi_cpp_tokiter_del_cur(cpp, true);
-
- if (index != psi_cpp_tokiter_index(cpp)) {
- /* might have been last token */
- psi_cpp_tokiter_next(cpp);
- }
- /* replace with tokens from macro */
- psi_cpp_tokiter_expand_tokens(cpp, macro->tokens);
- ++cpp->expanded;
- return true;
- }
- }
- }
+ if (psi_builtin_exists(current->text)) {
+ return psi_cpp_tokiter_expand_builtin(cpp, current,
+ psi_builtin_get(current->text));
+ } else {
+ return psi_cpp_tokiter_expand_decl(cpp, current,
+ zend_hash_find_ptr(&cpp->defs, current->text));
}
- return false;
}