basic support for builtins
[m6w6/ext-psi] / src / cpp.c
index 8075cf6b6ea646a21eddb32ba86a321953870ab6..4982c7842337b8a4d8bcc26780b8af95b73e9cbd 100644 (file)
--- a/src/cpp.c
+++ b/src/cpp.c
@@ -25,6 +25,8 @@
 
 #include "php_psi_stdinc.h"
 
+#include <libgen.h>
+
 #include "cpp.h"
 #include "parser.h"
 
 
 #include "php_psi.h"
 
+HashTable psi_cpp_defaults;
+
+PHP_MINIT_FUNCTION(psi_cpp)
+{
+       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;
+}
+
+PHP_MSHUTDOWN_FUNCTION(psi_cpp)
+{
+       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 void free_cpp_def(zval *p)
 {
        if (Z_TYPE_P(p) == IS_PTR) {
-               psi_cpp_macro_decl_free((void *) &Z_PTR_P(p));
+               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);
+               }
        }
 }
 
 struct psi_cpp *psi_cpp_init(struct psi_parser *P)
 {
-       struct psi_cpp *cpp = calloc(1, sizeof(*cpp));
+       struct psi_cpp *cpp = pecalloc(1, sizeof(*cpp), 1);
 
        cpp->parser = P;
-       zend_hash_init(&cpp->defs, 0, NULL, free_cpp_def, 1);
        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);
 
        return cpp;
 }
 
-bool psi_cpp_load_defaults(struct psi_cpp *cpp)
-{
-       struct psi_parser_input *predef;
-
-       if ((predef = psi_parser_open_string(cpp->parser, psi_cpp_predef, sizeof(psi_cpp_predef) - 1))) {
-               bool parsed = psi_parser_parse(cpp->parser, predef);
-               free(predef);
-               return parsed;
-       }
-
-       return false;
-}
+static char *include_flavor[] = {
+               "include",
+               "include next",
+               "include once"
+};
 
+#if PSI_CPP_DEBUG > 1
 static int dump_def(zval *p)
 {
        struct psi_cpp_macro_decl *decl = Z_PTR_P(p);
 
        if (decl) {
-               dprintf(2, "#define ");
+               dprintf(2, "PSI: CPP decl -> #define ");
                psi_cpp_macro_decl_dump(2, decl);
                dprintf(2, "\n");
        }
        return ZEND_HASH_APPLY_KEEP;
 }
+#endif
 
 void psi_cpp_free(struct psi_cpp **cpp_ptr)
 {
        if (*cpp_ptr) {
                struct psi_cpp *cpp = *cpp_ptr;
 
+#if PSI_CPP_DEBUG > 1
+               zend_hash_apply(&cpp->defs, dump_def);
+#endif
                *cpp_ptr = NULL;
-               if (cpp->parser->flags & PSI_DEBUG) {
-                       fprintf(stderr, "PSI: CPP decls:\n");
-                       zend_hash_apply(&cpp->defs, dump_def);
-               }
                zend_hash_destroy(&cpp->defs);
                zend_hash_destroy(&cpp->once);
                free(cpp);
@@ -113,8 +163,8 @@ static bool psi_cpp_stage1(struct psi_cpp *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;
                        }
@@ -168,8 +218,9 @@ static bool psi_cpp_stage1(struct psi_cpp *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 = zend_string_init_interned("\xA0", 1, 1);
+                                       psi_cpp_tokiter_add(cpp, no_ws);
                                        continue;
                                }
                        }
@@ -180,6 +231,7 @@ static bool psi_cpp_stage1(struct psi_cpp *cpp)
                }
 
                ws = false;
+               psi_cpp_tokiter_add_cur(cpp);
                psi_cpp_tokiter_next(cpp);
        }
 
@@ -204,7 +256,7 @@ static bool psi_cpp_stage2(struct psi_cpp *cpp)
                        }
                } 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;
@@ -220,7 +272,7 @@ static bool psi_cpp_stage2(struct psi_cpp *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;
@@ -232,7 +284,7 @@ static bool psi_cpp_stage2(struct psi_cpp *cpp)
                                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;
@@ -244,7 +296,7 @@ static bool psi_cpp_stage2(struct psi_cpp *cpp)
                                                } 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
                                                }
                                        }
@@ -254,18 +306,17 @@ static bool psi_cpp_stage2(struct psi_cpp *cpp)
                                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 PSI_CPP_DEBUG
-                               fprintf(stderr, "PSI: CPP skip ");
-                               psi_token_dump(2, current);
+                               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;
@@ -306,12 +357,13 @@ static bool psi_cpp_stage2(struct psi_cpp *cpp)
                        }
 
 #if PSI_CPP_DEBUG > 1
-                       psi_cpp_tokiter_dump(2, cpp);
+                       PSI_DEBUG_DUMP(cpp->parser, psi_cpp_tokiter_dump, cpp);
 #endif
 
                        continue;
                }
 
+               psi_cpp_tokiter_add_cur(cpp);
                psi_cpp_tokiter_next(cpp);
        }
 
@@ -323,18 +375,31 @@ static bool psi_cpp_stage2(struct psi_cpp *cpp)
 bool psi_cpp_process(struct psi_cpp *cpp, struct psi_plist **tokens)
 {
        bool parsed = false;
-       struct psi_cpp temp = *cpp;
+       struct psi_cpp temp = *cpp;     cpp->level = temp.level;
+
+       cpp->tokens.iter = *tokens;
+       cpp->tokens.next = NULL;
 
-       cpp->tokens = *tokens;
        if (psi_cpp_stage1(cpp) && psi_cpp_stage2(cpp)) {
                parsed = true;
        }
-       *tokens = cpp->tokens;
 
-       if (temp.tokens) {
-               cpp->tokens = temp.tokens;
-               cpp->index = temp.index;
+       if (cpp->tokens.next) {
+               free(cpp->tokens.iter);
+               cpp->tokens.iter = cpp->tokens.next;
+               cpp->tokens.next = NULL;
+       }
+
+       *tokens = cpp->tokens.iter;
+
+       if (temp.tokens.iter) {
+               cpp->tokens.iter = temp.tokens.iter;
+               cpp->tokens.next = temp.tokens.next;
        }
+       cpp->index = temp.index;
+       cpp->skip = temp.skip;
+       cpp->level = temp.level;
+       cpp->seen = temp.seen;
 
        return parsed;
 }
@@ -344,18 +409,21 @@ bool psi_cpp_defined(struct psi_cpp *cpp, struct psi_token *tok)
        bool defined;
 
        if (tok->type == PSI_T_NAME) {
-               defined = zend_hash_str_exists(&cpp->defs, tok->text, tok->size);
+               defined = zend_hash_exists(&cpp->defs, tok->text)
+                               || psi_builtin_exists(tok->text);
        } else {
                defined = false;
        }
 
 #if PSI_CPP_DEBUG
-       fprintf(stderr, "PSI: CPP defined -> %s ", defined ? "true" : "false");
+       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP defined -> %s ", defined ? "true" : "false");
        if (defined) {
-               struct psi_cpp_macro_decl *macro = zend_hash_str_find_ptr(&cpp->defs, tok->text, tok->size);
-               fprintf(stderr, " @ %s:%u ", macro->token->file, macro->token->line);
+               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);
+               }
        }
-       psi_token_dump(2, tok);
+       PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, tok);
 #endif
 
        return defined;
@@ -363,89 +431,134 @@ bool psi_cpp_defined(struct psi_cpp *cpp, struct psi_token *tok)
 
 void psi_cpp_define(struct psi_cpp *cpp, struct psi_cpp_macro_decl *decl)
 {
-       struct psi_cpp_macro_decl *old = zend_hash_str_find_ptr(&cpp->defs, decl->token->text, decl->token->size);
+       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);
+                               "'%s' redefined", decl->token->text->val);
                cpp->parser->error(PSI_DATA(cpp->parser), old->token, PSI_WARNING,
-                               "'%s' previously defined", old->token->text);
+                               "'%s' previously defined", old->token->text->val);
+       }
+#if PSI_CPP_DEBUG
+       if (decl->exp) {
+               PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO num_exp -> ");
+       } else {
+               PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO decl    -> ");
        }
-       zend_hash_str_update_ptr(&cpp->defs, decl->token->text, decl->token->size, 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 *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 *cpp, struct psi_cpp_exp *exp)
 {
-       if (!psi_num_exp_validate(PSI_DATA(cpp->parser), 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, &cpp->defs)) {
+       if (!psi_num_exp_get_long(exp->data.num, NULL, cpp)) {
                return false;
        }
        return true;
 }
 
-static inline bool try_include(struct psi_cpp *cpp, const char *path, bool *parsed)
+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;
 
-       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP include trying %s\n", path);
+       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) {
-               struct psi_plist *tokens;
+       if (!include) {
+               return false;
+       }
 
-               PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP include scanning %s\n", path);
+       zend_hash_str_add_empty_element(&cpp->once, path, strlen(path));
 
-               tokens = psi_parser_scan(cpp->parser, include);
-               if (tokens) {
-                       *parsed = psi_cpp_process(cpp, &tokens);
+       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP include scanning %s\n", path);
 
-                       if (*parsed) {
-                               size_t num_tokens = psi_plist_count(tokens);
+       tokens = psi_parser_scan(cpp->parser, include);
+       psi_parser_input_free(&include);
 
-                               ++cpp->expanded;
-                               psi_cpp_tokiter_ins_range(cpp, cpp->index,
-                                               num_tokens, psi_plist_eles(tokens));
-                               /* skip already processed tokens */
-                               cpp->index += num_tokens;
-                               free(tokens);
-                       } else {
-                               psi_plist_free(tokens);
-                       }
-               }
-               free(include);
+       if (!tokens) {
+               return false;
+       }
 
-               zend_hash_str_add_empty_element(&cpp->once, path, strlen(path));
-               return true;
+       parsed = psi_cpp_process(cpp, &tokens);
+       if (!parsed) {
+               psi_plist_free(tokens);
+               return false;
        }
-       return false;
+
+       psi_cpp_tokiter_add_range(cpp, psi_plist_count(tokens), psi_plist_eles(tokens));
+       free(tokens);
+
+       ++cpp->expanded;
+       return true;
 }
 
-bool psi_cpp_include(struct psi_cpp *cpp, const char *file, unsigned flags)
+#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)
 {
-       bool parsed = false;
-       int f_len = strlen(file);
+       char temp[PATH_MAX];
 
-       if (!(flags & PSI_CPP_INCLUDE_NEXT) || *file == '/') {
+       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 ((flags & PSI_CPP_INCLUDE_ONCE) && zend_hash_str_exists(&cpp->once, file, f_len)) {
-                       return true;
+               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);
                }
-               if (try_include(cpp, file, &parsed)) {
-                       /* found */
-                       return parsed;
+
+               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 != '/') {
-               char path[PATH_MAX];
+       if (file->text->val[0] != '/') {
                const char *sep;
                int p_len;
 
@@ -458,7 +571,7 @@ bool psi_cpp_include(struct psi_cpp *cpp, const char *file, unsigned flags)
                        }
                }
 
-               if (!(flags & PSI_CPP_INCLUDE_NEXT) || !cpp->search) {
+               if (!(flags & PSI_CPP_INCLUDE_NEXT)) {
                        cpp->search = PSI_G(search_path);
                }
 
@@ -468,13 +581,12 @@ bool psi_cpp_include(struct psi_cpp *cpp, const char *file, unsigned flags)
                        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, f_len, file))) {
-                               if ((flags & PSI_CPP_INCLUDE_ONCE) && zend_hash_str_exists(&cpp->once, path, p_len)) {
-                                       return true;
-                               }
-                               if (try_include(cpp, path, &parsed)) {
-                                       break;
-                               }
+                       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) {
@@ -483,5 +595,5 @@ bool psi_cpp_include(struct psi_cpp *cpp, const char *file, unsigned flags)
                } while (sep);
        }
 
-       return parsed;
+       return false;
 }