fix macro expansion
[m6w6/ext-psi] / src / cpp.c
index 8fcd5cf986fc3e0f9bf4d9c2e07c1d4fdd1fd4ab..034f8a230d0f99e2adabc1d1b73075349b200bf8 100644 (file)
--- a/src/cpp.c
+++ b/src/cpp.c
  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 "php_psi.h"
+
+#include <libgen.h>
 
 #include "cpp.h"
 #include "parser.h"
+#include "debug.h"
 
-static inline bool psi_cpp_level_skipped(struct psi_cpp_data *cpp)
-{
-       return cpp->skip == cpp->level;
-}
-static inline void psi_cpp_level_skip(struct psi_cpp_data *cpp)
-{
-       assert(!cpp->skip);
-       cpp->skip = cpp->level;
-}
-static inline void psi_cpp_level_unskip(struct psi_cpp_data *cpp)
+#define PSI_CPP_SEARCH
+#define PSI_CPP_PREDEF
+#include "php_psi_predef.h"
+
+HashTable psi_cpp_defaults;
+
+PHP_MINIT_FUNCTION(psi_cpp);
+PHP_MINIT_FUNCTION(psi_cpp)
 {
-       if (psi_cpp_level_skipped(cpp)) {
-               cpp->skip = 0;
+       struct psi_parser parser;
+       struct psi_parser_input *predef;
+
+       PSI_G(search_path) = pemalloc(strlen(PSI_G(directory)) + strlen(psi_cpp_search) + 1 + 1, 1);
+       sprintf(PSI_G(search_path), "%s:%s", PSI_G(directory), psi_cpp_search);
+
+       if (!psi_parser_init(&parser, psi_error_wrapper, PSI_SILENT)) {
+               return FAILURE;
        }
+
+       if (!(predef = psi_parser_open_string(&parser, psi_cpp_predef, sizeof(psi_cpp_predef) - 1))) {
+               psi_parser_dtor(&parser);
+               return FAILURE;
+       }
+
+       if (!psi_parser_parse(&parser, predef)) {
+               psi_parser_input_free(&predef);
+               psi_parser_dtor(&parser);
+               return FAILURE;
+       }
+       psi_parser_input_free(&predef);
+
+       zend_hash_init(&psi_cpp_defaults, 0, NULL, NULL, 1);
+       zend_hash_copy(&psi_cpp_defaults, &parser.preproc->defs, NULL);
+
+       psi_parser_dtor(&parser);
+
+       return SUCCESS;
 }
-static inline bool psi_cpp_level_masked(struct psi_cpp_data *cpp)
-{
-       return cpp->seen & (1 << cpp->level);
-}
-static inline void psi_cpp_level_mask(struct psi_cpp_data *cpp)
+
+PHP_MSHUTDOWN_FUNCTION(psi_cpp);
+PHP_MSHUTDOWN_FUNCTION(psi_cpp)
 {
-       assert(!psi_cpp_level_masked(cpp));
-       cpp->seen |= (1 << cpp->level);
+       struct psi_cpp_macro_decl *macro;
+
+       ZEND_HASH_FOREACH_PTR(&psi_cpp_defaults, macro)
+       {
+               psi_cpp_macro_decl_free(&macro);
+       }
+       ZEND_HASH_FOREACH_END();
+
+       zend_hash_destroy(&psi_cpp_defaults);
+
+       return SUCCESS;
 }
-static inline void psi_cpp_level_unmask(struct psi_cpp_data *cpp)
+
+static void free_cpp_def(zval *p)
 {
-       cpp->seen &= ~(1 << cpp->level);
+       if (Z_TYPE_P(p) == IS_PTR) {
+               struct psi_cpp_macro_decl *macro = Z_PTR_P(p);
+
+               if (!zend_hash_exists(&psi_cpp_defaults, macro->token->text)) {
+                       psi_cpp_macro_decl_free(&macro);
+               }
+       }
 }
 
-static void psi_cpp_eval(struct psi_data *D, struct psi_cpp_data *cpp)
+struct psi_cpp *psi_cpp_init(struct psi_parser *P)
 {
-       assert(cpp->exp);
+       struct psi_cpp *cpp = pecalloc(1, sizeof(*cpp), 1);
 
-       PSI_DEBUG_PRINT(D, "PSI: CPP EVAL < %s (level=%u, skip=%u)\n",
-                       cpp->exp->token->text, cpp->level, cpp->skip);
+       cpp->parser = P;
+       zend_hash_init(&cpp->once, 0, NULL, NULL, 1);
+       zend_hash_init(&cpp->defs, 0, NULL, free_cpp_def, 1);
+       zend_hash_copy(&cpp->defs, &psi_cpp_defaults, NULL);
+       zend_hash_init(&cpp->expanding, 0, NULL, NULL, 1);
 
-#if PSI_CPP_DEBUG
-       psi_cpp_exp_dump(2, cpp->exp);
-#endif
-
-       switch (cpp->exp->type) {
-       case PSI_T_ERROR:
-               if (!cpp->skip) {
-                       D->error(D, cpp->exp->token, PSI_ERROR, "%s",
-                                       cpp->exp->data.tok->text);
-               }
-               break;
-       case PSI_T_WARNING:
-               if (!cpp->skip) {
-                       D->error(D, cpp->exp->token, PSI_WARNING, "%s",
-                                       cpp->exp->data.tok->text);
-               }
-               break;
-       case PSI_T_UNDEF:
-               if (!cpp->skip) {
-                       psi_cpp_undef(cpp, cpp->exp->data.tok);
-               }
-               break;
-       case PSI_T_DEFINE:
-               if (!cpp->skip) {
-                       psi_cpp_define(cpp, cpp->exp->data.decl);
-                       /* FIXME: copy */
-                       cpp->exp->data.decl = NULL;
-               }
-               break;
-       case PSI_T_IFDEF:
-               ++cpp->level;
-               if (!cpp->skip) {
-                       if (psi_cpp_defined(cpp, cpp->exp->data.tok)) {
-                               psi_cpp_level_mask(cpp);
-                       } else {
-                               psi_cpp_level_skip(cpp);
-                       }
-               }
-               break;
-       case PSI_T_IFNDEF:
-               ++cpp->level;
-               if (!cpp->skip) {
-                       if (psi_cpp_defined(cpp, cpp->exp->data.tok)) {
-                               psi_cpp_level_skip(cpp);
-                       } else {
-                               psi_cpp_level_mask(cpp);
-                       }
-               }
-               break;
-       case PSI_T_IF:
-               ++cpp->level;
-               if (!cpp->skip) {
-                       if (psi_cpp_if(cpp->exp, &cpp->defs, D)) {
-                               psi_cpp_level_mask(cpp);
-                       } else {
-                               psi_cpp_level_skip(cpp);
-                       }
-               }
-               break;
-       case PSI_T_ENDIF:
-               if (!cpp->level) {
-                       D->error(D, cpp->exp->token, PSI_WARNING, "Ingoring lone #endif");
-               } else {
-                       psi_cpp_level_unskip(cpp);
-                       psi_cpp_level_unmask(cpp);
-                       --cpp->level;
-               }
-               break;
-       case PSI_T_ELSE:
-               /* FIXME: catch "else" after "else" */
-               if (!cpp->level) {
-                       D->error(D, cpp->exp->token, PSI_WARNING, "Ingoring lone #else");
-               } else if (psi_cpp_level_skipped(cpp) && !psi_cpp_level_masked(cpp)) {
-                       /*
-                        * if skip is set on this level and the level has
-                        * not been masked yet, then unskip and mask this level
-                        */
-                       psi_cpp_level_unskip(cpp);
-                       psi_cpp_level_mask(cpp);
-               } else if (!cpp->skip && psi_cpp_level_masked(cpp)) {
-                       /*
-                        * previous block masked this level
-                        */
-                       psi_cpp_level_skip(cpp);
-               } else {
-                       assert(cpp->skip < cpp->level);
-               }
-               break;
-       case PSI_T_ELIF:
-               if (!cpp->level) {
-                       D->error(D, cpp->exp->token, PSI_WARNING, "Ingoring lone #elif");
-               } else if (psi_cpp_level_skipped(cpp) && !psi_cpp_level_masked(cpp)) {
-                       /*
-                        * if skip is set on this level and the level has
-                        * not been masked yet, then unskip and mask this
-                        * level, if the condition evals truthy
-                        */
-                       if (psi_cpp_if(cpp->exp, &cpp->defs, D)) {
-                               psi_cpp_level_unskip(cpp);
-                               psi_cpp_level_mask(cpp);
-                       }
-               } else if (!cpp->skip && psi_cpp_level_masked(cpp)) {
-                       /*
-                        * previous block masked this level
-                        */
-                       psi_cpp_level_skip(cpp);
-               } else {
-                       assert(cpp->skip < cpp->level);
-               }
-               break;
-       default:
-               assert(0);
-               break;
-       }
+       return cpp;
+}
 
-       PSI_DEBUG_PRINT(D, "PSI: CPP EVAL > %s (level=%u, skip=%u)\n",
-                       cpp->exp->token->text, cpp->level, cpp->skip);
+static char *include_flavor[] = {
+               "include",
+               "include next",
+               "include once"
+};
 
-       psi_cpp_exp_free(&cpp->exp);
+void psi_cpp_free(struct psi_cpp **cpp_ptr)
+{
+       if (*cpp_ptr) {
+               struct psi_cpp *cpp = *cpp_ptr;
+
+               *cpp_ptr = NULL;
+               zend_hash_destroy(&cpp->defs);
+               zend_hash_destroy(&cpp->once);
+               zend_hash_destroy(&cpp->expanding);
+               free(cpp);
+       }
 }
 
-static bool psi_cpp_stage1(struct psi_parser *P, struct psi_cpp_data *cpp)
+static bool psi_cpp_stage1(struct psi_cpp *cpp)
 {
        bool name = false, define = false, hash = false, eol = true, esc = false, ws = false;
 
+       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP %s\n", "stage1");
+
        psi_cpp_tokiter_reset(cpp);
        while (psi_cpp_tokiter_valid(cpp)) {
                struct psi_token *token = psi_cpp_tokiter_current(cpp);
 
-               /* strip comments */
-               if (token->type == PSI_T_COMMENT) {
+               /* strip comments and attributes */
+               if (token->type == PSI_T_COMMENT
+                               || token->type == PSI_T_CPP_ATTRIBUTE) {
                        psi_cpp_tokiter_del_cur(cpp, true);
                        continue;
                }
@@ -202,8 +155,8 @@ static bool psi_cpp_stage1(struct psi_parser *P, struct psi_cpp_data *cpp)
                /* line continuations */
                if (token->type == PSI_T_EOL) {
                        if (esc) {
-                               psi_cpp_tokiter_del_range(cpp, psi_cpp_tokiter_index(cpp) - 1, 2, true);
-                               psi_cpp_tokiter_prev(cpp);
+                               psi_cpp_tokiter_del_prev(cpp, true);
+                               psi_cpp_tokiter_del_cur(cpp, true);
                                esc = false;
                                continue;
                        }
@@ -219,6 +172,9 @@ static bool psi_cpp_stage1(struct psi_parser *P, struct psi_cpp_data *cpp)
                 */
 
                if (token->type == PSI_T_WHITESPACE) {
+                       if (name) {
+                               name = false;
+                       }
                        ws = true;
                        psi_cpp_tokiter_del_cur(cpp, true);
                        continue;
@@ -254,8 +210,9 @@ static bool psi_cpp_stage1(struct psi_parser *P, struct psi_cpp_data *cpp)
                                        struct psi_token *no_ws = psi_token_copy(token);
 
                                        no_ws->type = PSI_T_NO_WHITESPACE;
-                                       no_ws->text[0] = '\xA0';
-                                       psi_cpp_tokiter_ins_cur(cpp, no_ws);
+                                       zend_string_release(no_ws->text);
+                                       no_ws->text = psi_string_init_interned("\xA0", 1, 1);
+                                       psi_cpp_tokiter_add(cpp, no_ws);
                                        continue;
                                }
                        }
@@ -266,188 +223,375 @@ static bool psi_cpp_stage1(struct psi_parser *P, struct psi_cpp_data *cpp)
                }
 
                ws = false;
+               psi_cpp_tokiter_add_cur(cpp);
                psi_cpp_tokiter_next(cpp);
        }
 
        return true;
 }
 
-static bool psi_cpp_stage2(struct psi_parser *P, struct psi_cpp_data *cpp)
+static bool psi_cpp_stage2(struct psi_cpp *cpp)
 {
-       do {
-               bool is_eol = true, do_cpp = false, do_expansion = true, skip_paren = false, skip_all = false;
+       bool is_eol = true, do_expansion = true, skip_paren = false, skip_all = false;
 
-               psi_cpp_tokiter_reset(cpp);
+       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP %s\n", "stage2");
 
-               while (psi_cpp_tokiter_valid(cpp)) {
-                       struct psi_token *current = psi_cpp_tokiter_current(cpp);
+       psi_cpp_tokiter_reset(cpp);
+       while (psi_cpp_tokiter_valid(cpp)) {
+               struct psi_token *current = psi_cpp_tokiter_current(cpp);
 
-                       if (current->type == PSI_T_HASH) {
-                               if (is_eol) {
-                                       do_cpp = true;
-                                       is_eol = false;
-                               }
-                       } else if (current->type == PSI_T_EOL) {
+               if (current->type == PSI_T_HASH) {
+                       if (is_eol) {
+                               cpp->do_cpp = true;
+                               is_eol = false;
+                       }
+               } else if (current->type == PSI_T_EOL) {
 #if PSI_CPP_DEBUG
-                               fprintf(stderr, "PSI: CPP do_expansion=true, PSI_T_EOL\n");
+                       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP do_expansion=true, PSI_T_EOL\n");
 #endif
-                               is_eol = true;
-                               skip_all = false;
-                               do_expansion = true;
-                               if (!do_cpp) {
-                                       psi_cpp_tokiter_del_cur(cpp, true);
-                                       continue;
-                               }
-                       } else {
-                               is_eol = false;
+                       is_eol = true;
+                       skip_all = false;
+                       do_expansion = true;
+                       if (!cpp->do_cpp) {
+                               psi_cpp_tokiter_del_cur(cpp, true);
+                               continue;
+                       }
+               } else {
+                       is_eol = false;
 
-                               if (do_cpp) {
-                                       switch (current->type) {
-                                       case PSI_T_DEFINE:
+                       if (cpp->do_cpp) {
+                               switch (current->type) {
+                               case PSI_T_DEFINE:
 #if PSI_CPP_DEBUG
-                                               fprintf(stderr, "PSI: CPP do_expansion=false, PSI_T_DEFINE, skip_all\n");
+                                       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP do_expansion=false, PSI_T_DEFINE, skip_all\n");
 #endif
-                                               do_expansion = false;
-                                               skip_all = true;
-                                               break;
-                                       case PSI_T_DEFINED:
-                                               skip_paren = true;
-                                               /* no break */
-                                       case PSI_T_IFDEF:
-                                       case PSI_T_IFNDEF:
-                                       case PSI_T_UNDEF:
+                                       do_expansion = false;
+                                       skip_all = true;
+                                       break;
+                               case PSI_T_DEFINED:
+                                       skip_paren = true;
+                                       /* no break */
+                               case PSI_T_IFDEF:
+                               case PSI_T_IFNDEF:
+                               case PSI_T_UNDEF:
 #if PSI_CPP_DEBUG
-                                               fprintf(stderr, "PSI: CPP do_expansion=false, PSI_T_{IF{,N},UN}DEF\n");
+                                       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP do_expansion=false, PSI_T_{IF{,N},UN}DEF\n");
 #endif
-                                               do_expansion = false;
-                                               break;
-                                       case PSI_T_LPAREN:
-                                               if (!skip_all) {
-                                                       if (skip_paren) {
-                                                               skip_paren = false;
-                                                       } else {
-                                                               do_expansion = true;
+                                       do_expansion = false;
+                                       break;
+                               case PSI_T_LPAREN:
+
+                                       if (!skip_all) {
+                                               if (skip_paren) {
+                                                       skip_paren = false;
+                                               } else {
+                                                       do_expansion = true;
 #if PSI_CPP_DEBUG
-                                                               fprintf(stderr, "PSI: CPP do_expansion=true, PSI_T_LPAREN, !skip_all, !skip_paren\n");
+                                                       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP do_expansion=true, PSI_T_LPAREN, !skip_all, !skip_paren\n");
 #endif
-                                                       }
                                                }
-                                               break;
-                                       case PSI_T_NAME:
-                                               break;
-                                       default:
-                                               do_expansion = !skip_all;
+                                       }
+                                       break;
+                               case PSI_T_NAME:
+                                       break;
+                               default:
+                                       do_expansion = !skip_all;
 #if PSI_CPP_DEBUG
-                                               fprintf(stderr, "PSI CPP do_expansion=%s, <- !skip_all\n", do_expansion?"true":"false");
+                                       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP do_expansion=%s, <- !skip_all\n", do_expansion?"true":"false");
 #endif
-                                       }
                                }
                        }
+               }
 
-                       if (cpp->skip) {
-                               /* FIXME: del_range */
-                               if (!do_cpp) {
+               if (cpp->skip) {
+                       if (!cpp->do_cpp) {
 #if PSI_CPP_DEBUG
-                                       fprintf(stderr, "PSI: CPP skip ");
-                                       psi_token_dump(2, current);
+                               PSI_DEBUG_LOCK(cpp->parser,
+                                       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP skip ");
+                                       PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, current);
+                               );
 #endif
-                                       psi_cpp_tokiter_del_cur(cpp, true);
-                                       continue;
-                               }
+                               psi_cpp_tokiter_del_cur(cpp, true);
+                               continue;
                        }
+               }
 
-                       if (do_expansion && current->type == PSI_T_NAME && psi_cpp_tokiter_defined(cpp)) {
-                               bool expanded = false;
-
-                               while (psi_cpp_tokiter_expand(cpp)) {
-                                       expanded = true;
-                               }
-                               if (expanded) {
-                                       continue;
-                               }
+               if (do_expansion && psi_cpp_defined(cpp, current)) {
+                       if (psi_cpp_tokiter_expand(cpp)) {
+                               continue;
                        }
+               }
 
-                       if (do_cpp) {
-                               if (is_eol) {
-                                       do_cpp = false;
-                                       skip_all = false;
-                               }
+               psi_cpp_tokiter_add_cur(cpp);
 
-                               if (P->flags & PSI_DEBUG) {
-                                       fprintf(stderr, "PSI> Parse (%zu) ", psi_cpp_tokiter_index(cpp));
-                                       psi_token_dump(2, current);
-                               }
+               if (cpp->do_cpp && is_eol) {
+                       size_t processed = 0;
+                       bool parsed;
 
-                               psi_parser_proc_parse(P->proc, current->type, current, P);
-                               psi_cpp_tokiter_del_cur(cpp, false);
+                       cpp->do_cpp = false;
+                       parsed = psi_parser_process(cpp->parser, cpp->tokens.exec, &processed);
+                       psi_plist_clean(cpp->tokens.exec);
 
-                               if (is_eol) {
-                                       psi_parser_proc_parse(P->proc, 0, NULL, P);
-                                       psi_cpp_eval(PSI_DATA(P), cpp);
-                               }
+                       if (!parsed) {
+                               psi_plist_free(cpp->tokens.exec);
+                               return false;
+                       }
 
-#if PSI_CPP_DEBUG
-                               psi_cpp_tokiter_dump(2, cpp);
+#if PSI_CPP_DEBUG > 1
+                       PSI_DEBUG_DUMP(cpp->parser, psi_cpp_tokiter_dump, cpp);
 #endif
+               }
 
-                               continue;
-                       }
+               psi_cpp_tokiter_next(cpp);
+       }
 
-                       psi_cpp_tokiter_next(cpp);
-               }
-       } while (cpp->expanded);
+       psi_plist_free(cpp->tokens.exec);
+       cpp->tokens.exec = NULL;
 
        return true;
 }
 
-bool psi_cpp_preprocess(struct psi_parser *P, struct psi_cpp_data *cpp)
+bool psi_cpp_process(struct psi_cpp *cpp, struct psi_plist **tokens,
+               struct psi_token *expanding)
 {
-       if (!psi_cpp_stage1(P, cpp)) {
-               return false;
+       bool parsed = false;
+       struct psi_cpp temp = *cpp;
+
+       cpp->tokens.iter = *tokens;
+       cpp->tokens.next = NULL;
+       cpp->tokens.exec = NULL;
+
+       if (expanding) {
+               zend_hash_add_empty_element(&cpp->expanding, expanding->text);
+       }
+       if (psi_cpp_stage1(cpp) && psi_cpp_stage2(cpp)) {
+               parsed = true;
+       }
+       if (expanding) {
+               zend_hash_del(&cpp->expanding, expanding->text);
        }
 
-       if (!psi_cpp_stage2(P, cpp)) {
-               return false;
+       *tokens = cpp->tokens.next;
+       psi_plist_free(cpp->tokens.iter);
+       if (cpp->tokens.exec) {
+               assert(!psi_plist_count(cpp->tokens.exec));
+               psi_plist_free(cpp->tokens.exec);
        }
 
-       return true;
+       cpp->tokens = temp.tokens;
+       cpp->index = temp.index;
+       cpp->skip = temp.skip;
+       cpp->level = temp.level;
+       cpp->seen = temp.seen;
+       cpp->do_cpp = temp.do_cpp;
+
+       return parsed;
 }
 
-bool psi_cpp_defined(struct psi_cpp_data *cpp, struct psi_token *tok)
+bool psi_cpp_defined(struct psi_cpp *cpp, struct psi_token *tok)
 {
-       bool defined;
+       bool defined = false;
 
        if (tok->type == PSI_T_NAME) {
-               defined = zend_hash_str_exists(&cpp->defs, tok->text, tok->size);
-       } else {
-               defined = false;
-       }
-
+               if (psi_builtin_exists(tok->text)) {
+                       defined = true;
+               } else if (!zend_hash_exists(&cpp->expanding, tok->text)) {
+                       defined = zend_hash_exists(&cpp->defs, tok->text);
+               }
 #if PSI_CPP_DEBUG
-       fprintf(stderr, "PSI: CPP defined -> %s ", defined ? "true" : "false");
-       psi_token_dump(2, tok);
+               PSI_DEBUG_LOCK(cpp->parser,
+                               PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP defined -> %s ", defined ? "true" : "false");
+                               if (defined) {
+                                       struct psi_cpp_macro_decl *macro = zend_hash_find_ptr(&cpp->defs, tok->text);
+                                       if (macro) {
+                                               PSI_DEBUG_PRINT(cpp->parser, " @ %s:%u ", macro->token->file->val, macro->token->line);
+                                       }
+                               } else {
+                                       zend_string *key;
+
+                                       PSI_DEBUG_PRINT(cpp->parser, " expanding=");
+                                       ZEND_HASH_FOREACH_STR_KEY(&cpp->expanding, key)
+                                       {
+                                               PSI_DEBUG_PRINT(cpp->parser, "%s,", key->val);
+                                       }
+                                       ZEND_HASH_FOREACH_END();
+                                       PSI_DEBUG_PRINT(cpp->parser, "\t");
+                               }
+                               PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, tok);
+               );
 #endif
+       }
 
        return defined;
 }
 
-void psi_cpp_define(struct psi_cpp_data *cpp, struct psi_cpp_macro_decl *decl)
+void psi_cpp_define(struct psi_cpp *cpp, struct psi_cpp_macro_decl *decl)
 {
-       zend_hash_str_add_ptr(&cpp->defs, decl->token->text, decl->token->size, decl);
+       struct psi_cpp_macro_decl *old = zend_hash_find_ptr(&cpp->defs, decl->token->text);
+
+       if (old && !psi_cpp_macro_decl_equal(old, decl)) {
+               cpp->parser->error(PSI_DATA(cpp->parser), decl->token, PSI_WARNING,
+                               "'%s' redefined", decl->token->text->val);
+               cpp->parser->error(PSI_DATA(cpp->parser), old->token, PSI_WARNING,
+                               "'%s' previously defined", old->token->text->val);
+       }
+#if PSI_CPP_DEBUG
+       PSI_DEBUG_LOCK(cpp->parser,
+                       if (decl->exp) {
+                               PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO num_exp -> ");
+                       } else {
+                               PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO decl    -> ");
+                       }
+                       PSI_DEBUG_DUMP(cpp->parser, psi_cpp_macro_decl_dump, decl);
+                       PSI_DEBUG_PRINT(cpp->parser, "\n");
+       );
+#endif
+       zend_hash_update_ptr(&cpp->defs, decl->token->text, decl);
 }
 
-bool psi_cpp_undef(struct psi_cpp_data *cpp, struct psi_token *tok)
+bool psi_cpp_undef(struct psi_cpp *cpp, struct psi_token *tok)
 {
-       return SUCCESS == zend_hash_str_del(&cpp->defs, tok->text, tok->size);
+       return SUCCESS == zend_hash_del(&cpp->defs, tok->text);
 }
 
-bool psi_cpp_if(struct psi_cpp_exp *exp, HashTable *defs, struct psi_data *D)
+bool psi_cpp_if(struct psi_cpp *cpp, struct psi_cpp_exp *exp)
 {
-       if (!psi_num_exp_validate(D, exp->data.num, NULL, NULL, NULL, NULL, NULL)) {
+       struct psi_validate_scope scope = {0};
+
+       scope.cpp = cpp;
+       if (!psi_num_exp_validate(PSI_DATA(cpp->parser), exp->data.num, &scope)) {
                return false;
        }
-       if (!psi_long_num_exp(exp->data.num, NULL, defs)) {
+       if (!psi_num_exp_get_long(exp->data.num, NULL, cpp)) {
                return false;
        }
        return true;
 }
+
+bool psi_cpp_include(struct psi_cpp *cpp, const struct psi_token *file, unsigned flags)
+{
+       bool parsed = false;
+       char path[PATH_MAX];
+       struct psi_plist *tokens;
+       struct psi_parser_input *include;
+
+       if (!psi_cpp_has_include(cpp, file, flags, path)) {
+               return false;
+       }
+
+       if (flags & PSI_CPP_INCLUDE_ONCE) {
+               if (zend_hash_str_exists(&cpp->once, path, strlen(path))) {
+                       return true;
+               }
+       }
+
+       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP %s opening %s\n",
+                       include_flavor[flags], path);
+
+       include = psi_parser_open_file(cpp->parser, path, false);
+       if (!include) {
+               return false;
+       }
+
+       zend_hash_str_add_empty_element(&cpp->once, path, strlen(path));
+
+       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP include scanning %s\n", path);
+
+       tokens = psi_parser_scan(cpp->parser, include);
+       psi_parser_input_free(&include);
+
+       if (!tokens) {
+               return false;
+       }
+
+       parsed = psi_cpp_process(cpp, &tokens, NULL);
+       if (!parsed) {
+               psi_plist_free(tokens);
+               return false;
+       }
+
+       psi_cpp_tokiter_add_range(cpp, psi_plist_count(tokens), psi_plist_eles(tokens));
+       free(tokens);
+
+       ++cpp->expanded;
+       return true;
+}
+
+#ifndef HAVE_EACCESS
+#      define eaccess access
+#endif
+bool psi_cpp_has_include(struct psi_cpp *cpp, const struct psi_token *file, unsigned flags, char *path)
+{
+       char temp[PATH_MAX];
+
+       if (!path) {
+               path = temp;
+       }
+
+       if (file->type == PSI_T_QUOTED_STRING && (!(flags & PSI_CPP_INCLUDE_NEXT) || file->text->val[0] == '/')) {
+               /* first try as is, full or relative path */
+               if (file->text->val[0] == '/') {
+                       path = file->text->val;
+               } else {
+                       char *dir;
+                       size_t len;
+
+                       strncpy(path, file->file->val, PATH_MAX);
+
+                       dir = dirname(path);
+                       len = strlen(dir);
+
+                       assert(len + file->text->len + 1 < PATH_MAX);
+
+                       memmove(path, dir, len);
+                       path[len] = '/';
+                       memcpy(&(path)[len + 1], file->text->val, file->text->len + 1);
+               }
+
+               PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP %s trying %s\n",
+                               include_flavor[flags], path);
+               if (0 == eaccess(path, R_OK)) {
+                       return true;
+               }
+       }
+
+       /* look through search paths */
+       if (file->text->val[0] != '/') {
+               const char *sep;
+               int p_len;
+
+               if ((flags & PSI_CPP_INCLUDE_NEXT) && cpp->search) {
+                       if ((sep = strchr(cpp->search, ':'))) {
+                               cpp->search = sep + 1;
+                       } else {
+                               /* point to end of string */
+                               cpp->search += strlen(cpp->search);
+                       }
+               }
+
+               if (!(flags & PSI_CPP_INCLUDE_NEXT)) {
+                       cpp->search = PSI_G(search_path);
+               }
+
+               do {
+                       int d_len;
+
+                       sep = strchr(cpp->search, ':');
+                       d_len = sep ? sep - cpp->search : strlen(cpp->search);
+
+                       if (PATH_MAX > (p_len = snprintf(path, PATH_MAX, "%.*s/%.*s", d_len, cpp->search, (int) file->text->len, file->text->val))) {
+                               PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP %s trying %s\n",
+                                               include_flavor[flags], path);
+                               if (0 == eaccess(path, R_OK)) {
+                                        return true;
+                                }
+                       }
+
+                       if (sep) {
+                               cpp->search = sep + 1;
+                       }
+               } while (sep);
+       }
+
+       return false;
+}