From 2fa436074ca9a5e87f39b696de832fa2188fcfc6 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 30 Oct 2018 13:50:44 +0100 Subject: [PATCH] zend_string'ify --- TODO | 1 + src/call.c | 26 +- src/call.h | 4 +- src/context.c | 8 +- src/cpp.c | 38 +- src/cpp_tokiter.c | 37 +- src/data.c | 2 +- src/error.c | 2 +- src/libffi.c | 8 +- src/libjit.c | 8 +- src/marshal.c | 6 +- src/parser.c | 741 +++++++++++++++++-------------------- src/parser.h | 2 +- src/parser.re | 99 ++--- src/parser_proc.c | 46 +-- src/parser_proc_grammar.y | 46 +-- src/plist.c | 5 +- src/token.c | 130 +++---- src/token.h | 12 +- src/types.h | 6 +- src/types/assert_stmt.c | 6 +- src/types/const.c | 17 +- src/types/const.h | 4 +- src/types/cpp_exp.c | 31 +- src/types/cpp_macro_call.c | 14 +- src/types/cpp_macro_call.h | 4 +- src/types/cpp_macro_decl.c | 23 +- src/types/decl.c | 30 +- src/types/decl.h | 2 +- src/types/decl_abi.c | 14 +- src/types/decl_abi.h | 4 +- src/types/decl_arg.c | 14 +- src/types/decl_arg.h | 2 +- src/types/decl_enum.c | 14 +- src/types/decl_enum.h | 4 +- src/types/decl_enum_item.c | 12 +- src/types/decl_enum_item.h | 4 +- src/types/decl_extvar.c | 32 +- src/types/decl_file.c | 4 +- src/types/decl_file.h | 2 +- src/types/decl_struct.c | 28 +- src/types/decl_struct.h | 4 +- src/types/decl_type.c | 36 +- src/types/decl_type.h | 6 +- src/types/decl_union.c | 26 +- src/types/decl_union.h | 4 +- src/types/decl_var.c | 41 +- src/types/decl_var.h | 19 +- src/types/free_exp.c | 19 +- src/types/free_exp.h | 4 +- src/types/free_stmt.c | 4 +- src/types/impl.c | 6 +- src/types/impl_def_val.c | 14 +- src/types/impl_func.c | 16 +- src/types/impl_func.h | 5 +- src/types/impl_type.c | 12 +- src/types/impl_type.h | 4 +- src/types/impl_var.c | 39 +- src/types/impl_var.h | 28 +- src/types/let_callback.c | 12 +- src/types/let_calloc.c | 4 +- src/types/let_exp.c | 26 +- src/types/let_exp.h | 2 +- src/types/let_func.c | 27 +- src/types/let_func.h | 4 +- src/types/let_stmt.c | 17 +- src/types/num_exp.c | 10 +- src/types/number.c | 105 +++--- src/types/number.h | 2 +- src/types/return_exp.c | 15 +- src/types/return_exp.h | 2 +- src/types/return_stmt.c | 8 +- src/types/set_exp.c | 17 +- src/types/set_func.c | 31 +- src/types/set_func.h | 4 +- src/types/set_stmt.c | 10 +- src/validate.c | 16 +- src/validate.h | 24 +- 78 files changed, 989 insertions(+), 1126 deletions(-) diff --git a/TODO b/TODO index 77b6420..1e14626 100644 --- a/TODO +++ b/TODO @@ -8,3 +8,4 @@ * fix right recursion of let_exp and set_exp * improve varargs support, currently it's impossible to map e.g. curl_easy_setopt * cpp: asm aliasing/redirects +* impl_var.name: remove leading '$' diff --git a/src/call.c b/src/call.c index 015b626..2c18e9b 100644 --- a/src/call.c +++ b/src/call.c @@ -159,9 +159,9 @@ struct psi_call_frame_symbol *psi_call_frame_fetch_symbol( struct psi_call_frame *frame, struct psi_decl_var *dvar) { struct psi_call_frame_symbol *frame_sym; - frame_sym = zend_hash_str_find_ptr(&frame->symbols, dvar->fqn, strlen(dvar->fqn)); + frame_sym = zend_hash_find_ptr(&frame->symbols, dvar->fqn); if (!frame_sym) { - frame_sym = zend_hash_str_add_ptr(&frame->symbols, dvar->fqn, strlen(dvar->fqn), + frame_sym = zend_hash_add_ptr(&frame->symbols, dvar->fqn, psi_call_frame_symbol_init(dvar)); } return frame_sym; @@ -173,17 +173,16 @@ zval *psi_call_frame_new_argument(struct psi_call_frame *frame, /* varargs are just appended with numeric indices */ return zend_hash_next_index_insert_ptr(&frame->arguments, frame_arg); } else { - return zend_hash_str_add_ptr(&frame->arguments, - frame_arg->spec->var->name, strlen(frame_arg->spec->var->name), - frame_arg); + return zend_hash_add_ptr(&frame->arguments, + frame_arg->spec->var->name, frame_arg); } } zval *psi_call_frame_sub_argument(struct psi_call_frame *frame, - struct psi_impl_var *inner_var, zval *outer_zval, const char *name) { + struct psi_impl_var *inner_var, zval *outer_zval, zend_string *name) { struct psi_call_frame_argument *iarg; zval *inner_zval = zend_symtable_str_find(Z_ARRVAL_P(outer_zval), - &inner_var->name[1], strlen(&inner_var->name[1])); + &inner_var->name->val[1], inner_var->name->len - 1); if (!inner_zval) { zval empty_zval; @@ -191,7 +190,7 @@ zval *psi_call_frame_sub_argument(struct psi_call_frame *frame, SEPARATE_ZVAL(outer_zval); ZVAL_NULL(&empty_zval); inner_zval = zend_symtable_str_update(Z_ARRVAL_P(outer_zval), - &inner_var->name[1], strlen(&inner_var->name[1]), + &inner_var->name->val[1], inner_var->name->len - 1, &empty_zval); } @@ -201,20 +200,20 @@ zval *psi_call_frame_sub_argument(struct psi_call_frame *frame, struct psi_call_frame_argument *frame_arg; impl_val empty_val = {0}; struct psi_impl_arg *carg_spec = psi_impl_arg_init( - psi_impl_type_init(PSI_T_MIXED, "mixed"), + psi_impl_type_init(PSI_T_MIXED, zend_string_init(ZEND_STRL("mixed"), 1)), psi_impl_var_copy(inner_var), NULL); psi_call_frame_push_auto_ex(frame, carg_spec, (void(*)(void*)) psi_impl_arg_free); frame_arg = psi_call_frame_argument_init(carg_spec, &empty_val, inner_zval, 0); - zend_hash_str_add_ptr(&frame->arguments, name, strlen(name), frame_arg); + zend_hash_add_ptr(&frame->arguments, name, frame_arg); } return inner_zval; } struct psi_call_frame_argument *psi_call_frame_get_argument( - struct psi_call_frame *frame, const char *name) { - return zend_hash_str_find_ptr(&frame->arguments, name, strlen(name)); + struct psi_call_frame *frame, zend_string *name) { + return zend_hash_find_ptr(&frame->arguments, name); } size_t psi_call_frame_num_var_args(struct psi_call_frame *frame) { @@ -546,8 +545,7 @@ void psi_call_frame_free(struct psi_call_frame *frame) { memcpy(temp, &frame->temp, sizeof(*temp)); ZVAL_OBJ(&zlocal, psi_object_init_ex(NULL, temp, psi_call_frame_local_auto_dtor)); - zend_set_local_var_str(frame->impl->func->name, - strlen(frame->impl->func->name), &zlocal, /* force */ 1); + zend_set_local_var(frame->impl->func->name, &zlocal, /* force */ 1); } else { zend_llist_destroy(&frame->temp); } diff --git a/src/call.h b/src/call.h index 2144b4c..a6ad873 100644 --- a/src/call.h +++ b/src/call.h @@ -77,9 +77,9 @@ size_t psi_call_frame_num_var_args(struct psi_call_frame *frame); size_t psi_call_frame_num_fixed_args(struct psi_call_frame *frame); zval *psi_call_frame_new_argument(struct psi_call_frame *frame, struct psi_call_frame_argument *frame_arg); -zval *psi_call_frame_sub_argument(struct psi_call_frame *frame, struct psi_impl_var *inner_var, zval *outer_zval, const char *name); +zval *psi_call_frame_sub_argument(struct psi_call_frame *frame, struct psi_impl_var *inner_var, zval *outer_zval, zend_string *name); -struct psi_call_frame_argument *psi_call_frame_get_argument(struct psi_call_frame *frame, const char *name); +struct psi_call_frame_argument *psi_call_frame_get_argument(struct psi_call_frame *frame, zend_string *name); struct psi_call_frame_argument *psi_call_frame_get_var_argument(struct psi_call_frame *frame, zend_long index); struct psi_call_frame_symbol *psi_call_frame_fetch_symbol(struct psi_call_frame *frame, struct psi_decl_var *dvar); diff --git a/src/context.c b/src/context.c index abe1ff6..995a001 100644 --- a/src/context.c +++ b/src/context.c @@ -176,11 +176,11 @@ zend_function_entry *psi_context_compile(struct psi_context *C) while (psi_plist_get(C->consts, i++, &c)) { - if (zend_get_constant_str(c->name, strlen(c->name))) { + if (zend_get_constant(c->name)) { continue; } - zc.name = zend_string_init(c->name, strlen(c->name), 1); + zc.name = zend_string_copy(c->name); switch (c->type->type) { case PSI_T_BOOL: @@ -218,9 +218,9 @@ zend_function_entry *psi_context_compile(struct psi_context *C) zend_string *name; if (psi_decl_type_is_anon(e->name, "enum")) { - name = strpprintf(0, "psi\\%s", item->name); + name = strpprintf(0, "psi\\%s", item->name->val); } else { - name = strpprintf(0, "psi\\%s\\%s", e->name, item->name); + name = strpprintf(0, "psi\\%s\\%s", e->name->val, item->name->val); } if (zend_get_constant(name)) { diff --git a/src/cpp.c b/src/cpp.c index 908c21f..5a1130c 100644 --- a/src/cpp.c +++ b/src/cpp.c @@ -172,7 +172,8 @@ 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'; + zend_string_release(no_ws->text); + no_ws->text = zend_string_init("\xA0", 1, 1); psi_cpp_tokiter_ins_cur(cpp, no_ws); continue; } @@ -348,7 +349,7 @@ 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); } else { defined = false; } @@ -356,8 +357,8 @@ bool psi_cpp_defined(struct psi_cpp *cpp, struct psi_token *tok) #if PSI_CPP_DEBUG fprintf(stderr, "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); + fprintf(stderr, " @ %s:%u ", macro->token->file->val, macro->token->line); } psi_token_dump(2, tok); #endif @@ -367,13 +368,13 @@ 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) { @@ -384,12 +385,12 @@ void psi_cpp_define(struct psi_cpp *cpp, struct psi_cpp_macro_decl *decl) psi_cpp_macro_decl_dump(2, decl); fprintf(stderr, "\n"); #endif - zend_hash_str_update_ptr(&cpp->defs, decl->token->text, decl->token->size, decl); + 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) @@ -445,37 +446,36 @@ static inline bool try_include(struct psi_cpp *cpp, const char *path, bool *pars static inline void include_path(const struct psi_token *file, char **path) { - if (*file->text == '/') { - *path = file->text; + if (file->text->val[0] == '/') { + *path = file->text->val; } else { char *dir; size_t len; - strncpy(*path, file->file, PATH_MAX); + strncpy(*path, file->file->val, PATH_MAX); dir = dirname(*path); len = strlen(dir); - assert(len + file->size + 1 < PATH_MAX); + assert(len + file->text->len + 1 < PATH_MAX); memmove(*path, dir, len); (*path)[len] = '/'; - memcpy(&(*path)[len + 1], file->text, file->size + 1); + memcpy(&(*path)[len + 1], file->text->val, file->text->len + 1); } } bool psi_cpp_include(struct psi_cpp *cpp, const struct psi_token *file, unsigned flags) { bool parsed = false; - int f_len = strlen(file->text); - if (file->type == PSI_T_QUOTED_STRING && (!(flags & PSI_CPP_INCLUDE_NEXT) || *file->text == '/')) { + if (file->type == PSI_T_QUOTED_STRING && (!(flags & PSI_CPP_INCLUDE_NEXT) || file->text->val[0] == '/')) { /* first try as is, full or relative path */ char temp[PATH_MAX], *path = temp; include_path(file, &path); - if ((flags & PSI_CPP_INCLUDE_ONCE) && zend_hash_str_exists(&cpp->once, path, f_len)) { + if ((flags & PSI_CPP_INCLUDE_ONCE) && zend_hash_str_exists(&cpp->once, path, strlen(path))) { return true; } if (try_include(cpp, path, &parsed)) { @@ -485,7 +485,7 @@ bool psi_cpp_include(struct psi_cpp *cpp, const struct psi_token *file, unsigned } /* look through search paths */ - if (*file->text != '/') { + if (file->text->val[0] != '/') { char path[PATH_MAX]; const char *sep; int p_len; @@ -509,7 +509,7 @@ bool psi_cpp_include(struct psi_cpp *cpp, const struct psi_token *file, unsigned 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->text))) { + if (PATH_MAX > (p_len = snprintf(path, PATH_MAX, "%.*s/%.*s", d_len, cpp->search, (int) file->text->len, file->text->val))) { if ((flags & PSI_CPP_INCLUDE_ONCE) && zend_hash_str_exists(&cpp->once, path, p_len)) { return true; } diff --git a/src/cpp_tokiter.c b/src/cpp_tokiter.c index 57645ec..9ebe191 100644 --- a/src/cpp_tokiter.c +++ b/src/cpp_tokiter.c @@ -128,8 +128,8 @@ bool psi_cpp_tokiter_del_cur(struct psi_cpp *cpp, bool free_token) fprintf(stderr, "NULL\n"); } #endif - if (cur && free_token) { - free(cur); + if (free_token) { + psi_token_free(&cur); } count = psi_plist_count(cpp->tokens); if (deleted && cpp->index >= count) { @@ -173,9 +173,7 @@ bool psi_cpp_tokiter_del_range(struct psi_cpp *cpp, size_t offset, size_t num_el if (free_tokens) { while (num_eles--) { - if (ptr[num_eles]) { - free(ptr[num_eles]); - } + psi_token_free(&ptr[num_eles]); } free(ptr); } @@ -260,16 +258,17 @@ static size_t psi_cpp_tokiter_expand_tokens(struct psi_cpp *cpp, struct psi_token *tmp_tok, *old_tok = exp_tokens[n - 1]; tmp_tok = psi_token_init(old_tok->type, "", 0, - target->col, target->line, target->file?:""); + target->col, target->line, + target->file ? target->file->val : ""); new_tok = psi_token_cat(NULL, 3, tmp_tok, old_tok, tok); - free(old_tok); - free(tmp_tok); + psi_token_free(&old_tok); + psi_token_free(&tmp_tok); exp_tokens[n - 1] = new_tok; } else { new_tok = psi_token_init(stringify ? PSI_T_QUOTED_STRING : tok->type, - tok->text, tok->size, target->col, target->line, target->file?:""); + tok->text->val, tok->text->len, target->col, target->line, target->file?:""); exp_tokens[n++] = new_tok; } @@ -301,7 +300,7 @@ static void psi_cpp_tokiter_free_call_tokens(struct psi_plist **arg_tokens_list, 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]); @@ -395,11 +394,9 @@ static void psi_cpp_tokiter_expand_call_tokens(struct psi_cpp *cpp, 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)) { + arg_tokens = arg_tokens_list[s]; + break; } } } @@ -443,7 +440,7 @@ static bool psi_cpp_tokiter_expand_call(struct psi_cpp *cpp, /* back to where we took off */ psi_cpp_tokiter_seek(cpp, start); - free(target); + psi_token_free(&target); ++cpp->expanded; return true; } @@ -463,7 +460,7 @@ static bool psi_cpp_tokiter_expand_def(struct psi_cpp *cpp, /* replace with tokens from macro */ psi_cpp_tokiter_expand_tokens(cpp, target, macro->tokens); - free(target); + psi_token_free(&target); ++cpp->expanded; return true; } @@ -476,7 +473,7 @@ static inline int psi_cpp_tokiter_expand_cmp(struct psi_token *t, psi_plist_get(m->tokens, 0, &r); - return strcmp(r->text, t->text); + return !zend_string_equals(r->text, t->text); } return -1; } @@ -487,8 +484,8 @@ bool psi_cpp_tokiter_expand(struct psi_cpp *cpp) struct psi_token *current = psi_cpp_tokiter_current(cpp); if (current) { - struct psi_cpp_macro_decl *macro = zend_hash_str_find_ptr( - &cpp->defs, current->text, current->size); + struct psi_cpp_macro_decl *macro = zend_hash_find_ptr( + &cpp->defs, current->text); /* don't expand itself */ if (macro && macro->token != current) { diff --git a/src/data.c b/src/data.c index d5c9765..f9e0e5f 100644 --- a/src/data.c +++ b/src/data.c @@ -164,7 +164,7 @@ void psi_data_dump(int fd, struct psi_data *D) char *libname; if (D->file.filename) { - dprintf(fd, "// filename=%s (%u errors)\n", D->file.filename, D->errors); + dprintf(fd, "// filename=%s (%u errors)\n", D->file.filename->val, D->errors); } while (psi_plist_get(D->file.libnames, i++, &libname)) { dprintf(fd, "lib \"%s\";\n", libname); diff --git a/src/error.c b/src/error.c index c92b847..486867b 100644 --- a/src/error.c +++ b/src/error.c @@ -57,7 +57,7 @@ void psi_error_wrapper(struct psi_data *context, struct psi_token *t, int type, } if (t) { - fn = t->file; + fn = t->file->val; ln = t->line; } else if (zend_is_executing()) { fn = zend_get_executed_filename(); diff --git a/src/libffi.c b/src/libffi.c index f14a9cd..fbd79b0 100644 --- a/src/libffi.c +++ b/src/libffi.c @@ -413,15 +413,15 @@ static inline ffi_type *psi_ffi_decl_func_type(struct psi_decl *fn) { } } -static inline ffi_abi psi_ffi_abi(const char *convention) { +static inline ffi_abi psi_ffi_abi(zend_string *convention) { if (FFI_LAST_ABI - 2 != FFI_FIRST_ABI) { #ifdef HAVE_FFI_STDCALL - if (!strcasecmp(convention, "stdcall")) { + if (zend_string_equals_literal(convention, "stdcall")) { return FFI_STDCALL; } #endif #ifdef HAVE_FFI_FASTCALL - if (!strcasecmp(convention, "fastcall")) { + if (zend_string_equals_literal(convention, "fastcall")) { return FFI_FASTCALL; } #endif @@ -785,7 +785,7 @@ static zend_function_entry *psi_ffi_compile(struct psi_context *C) continue; } - zf->fname = impl->func->name + (impl->func->name[0] == '\\'); + zf->fname = impl->func->name->val + (impl->func->name->val[0] == '\\'); zf->handler = ((struct psi_ffi_impl_info *) impl->info)->code; zf->num_args = psi_plist_count(impl->func->args); zf->arg_info = psi_internal_arginfo(impl); diff --git a/src/libjit.c b/src/libjit.c index eaf3b16..22266ba 100644 --- a/src/libjit.c +++ b/src/libjit.c @@ -231,12 +231,12 @@ static inline jit_type_t psi_jit_decl_arg_type(struct psi_decl_arg *darg) } } -static inline jit_abi_t psi_jit_abi(const char *convention) +static inline jit_abi_t psi_jit_abi(zend_string *convention) { - if (!strcasecmp(convention, "stdcall")) { + if (zend_string_equals_literal(convention, "stdcall")) { return jit_abi_stdcall; } - if (!strcasecmp(convention, "fastcall")) { + if (zend_string_equals_literal(convention, "fastcall")) { return jit_abi_fastcall; } return jit_abi_cdecl; @@ -550,7 +550,7 @@ static zend_function_entry *psi_jit_compile(struct psi_context *C) continue; } - zf->fname = impl->func->name + (impl->func->name[0] == '\\'); + zf->fname = impl->func->name->val + (impl->func->name->val[0] == '\\'); zf->handler = ((struct psi_jit_impl_info *) impl->info)->closure; zf->num_args = psi_plist_count(impl->func->args); zf->arg_info = psi_internal_arginfo(impl); diff --git a/src/marshal.c b/src/marshal.c index c7651f6..9804fe0 100644 --- a/src/marshal.c +++ b/src/marshal.c @@ -112,7 +112,7 @@ zend_internal_arg_info *psi_internal_arginfo(struct psi_impl *impl) struct psi_impl_arg *vararg = impl->func->vararg; zend_internal_arg_info *ai = &aip[argc]; - ai->name = vararg->var->name; + ai->name = vararg->var->name->val; #ifdef ZEND_TYPE_ENCODE ai->type = ZEND_TYPE_ENCODE(psi_internal_type(vararg->type), 1); #else @@ -128,7 +128,7 @@ zend_internal_arg_info *psi_internal_arginfo(struct psi_impl *impl) while (psi_plist_get(impl->func->args, i++, &iarg)) { zend_internal_arg_info *ai = &aip[i]; - ai->name = iarg->var->name; + ai->name = iarg->var->name->val; #ifdef ZEND_TYPE_ENCODE ai->type = ZEND_TYPE_ENCODE(psi_internal_type(iarg->type), 1); #else @@ -732,7 +732,7 @@ void psi_set_to_array(zval *return_value, struct psi_set_exp *set, impl_val *r_v ZVAL_NULL(&ele); psi_set_exp_exec_ex(sub_exp, &ele, sym->ptr, frame); - add_assoc_zval(return_value, ivar->name + 1, &ele); + add_assoc_zval(return_value, ivar->name->val + 1, &ele); } } diff --git a/src/parser.c b/src/parser.c index 07c5cbe..70e4ba2 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,4 +1,4 @@ -/* Generated by re2c 1.1.1 on Mon Oct 29 07:42:47 2018 */ +/* Generated by re2c 1.1.1 on Tue Oct 30 13:23:48 2018 */ #line 1 "src/parser.re" /******************************************************************************* Copyright (c) 2016, Michael Wallner . @@ -31,6 +31,8 @@ #include #include +#include + #include "parser.h" #define YYMAXFILL 15 @@ -70,7 +72,7 @@ struct psi_parser_input *psi_parser_open_file(struct psi_parser *P, const char * return NULL; } - if (!(fb = malloc(sizeof(*fb) + strlen(filename) + 1 + sb.st_size + YYMAXFILL))) { + if (!(fb = malloc(sizeof(*fb) + sb.st_size + YYMAXFILL))) { if (report_errors) { P->error(PSI_DATA(P), NULL, PSI_WARNING, "Could not allocate %zu bytes for reading '%s': %s", @@ -100,10 +102,8 @@ struct psi_parser_input *psi_parser_open_file(struct psi_parser *P, const char * return NULL; } - memset(fb->buffer + sb.st_size, 0, YYMAXFILL); fb->length = sb.st_size; - fb->file = &fb->buffer[sb.st_size + YYMAXFILL]; - memcpy(fb->file, filename, strlen(filename) + 1); + fb->file = zend_string_init(filename, strlen(filename), 1); return fb; } @@ -112,7 +112,7 @@ struct psi_parser_input *psi_parser_open_string(struct psi_parser *P, const char { struct psi_parser_input *sb; - if (!(sb = malloc(sizeof(*sb) + sizeof("") + length + YYMAXFILL))) { + if (!(sb = malloc(sizeof(*sb) + length + YYMAXFILL))) { P->error(PSI_DATA(P), NULL, PSI_WARNING, "Could not allocate %zu bytes: %s", length + YYMAXFILL, strerror(errno)); @@ -123,68 +123,11 @@ struct psi_parser_input *psi_parser_open_string(struct psi_parser *P, const char memset(sb->buffer + length, 0, YYMAXFILL); sb->length = length; - sb->file = &sb->buffer[length + YYMAXFILL]; - memcpy(sb->file, "", sizeof("")); + sb->file = zend_string_init("", strlen(""), 1); return sb; } -#if 0 -static void psi_parser_register_constants(struct psi_parser *P) -{ - zend_string *key; - zval *val; - - ZEND_HASH_FOREACH_STR_KEY_VAL(&P->cpp.defs, key, val) - { - struct psi_impl_def_val *iv; - struct psi_const_type *ct; - struct psi_const *c; - const char *ctn; - token_t ctt; - impl_val tmp; - zend_string *str; - - ZVAL_DEREF(val); - switch (Z_TYPE_P(val)) { - case IS_TRUE: - case IS_FALSE: - ctt = PSI_T_BOOL; - ctn = "bool"; - tmp.zend.bval = Z_TYPE_P(val) == IS_TRUE; - break; - case IS_LONG: - ctt = PSI_T_INT; - ctn = "int"; - tmp.zend.lval = Z_LVAL_P(val); - break; - case IS_DOUBLE: - ctt = PSI_T_FLOAT; - ctn = "float"; - tmp.dval = Z_DVAL_P(val); - break; - default: - ctt = PSI_T_STRING; - ctn = "string"; - str = zval_get_string(val); - tmp.zend.str = zend_string_dup(str, 1); - zend_string_release(str); - break; - } - - iv = psi_impl_def_val_init(ctt, NULL); - iv->ival = tmp; - ct = psi_const_type_init(ctt, ctn); - c = psi_const_init(ct, key->val, iv); - if (!P->consts) { - P->consts = psi_plist_init((psi_plist_dtor) psi_const_free); - } - P->consts = psi_plist_add(P->consts, &c); - } - ZEND_HASH_FOREACH_END(); -} -#endif - struct psi_plist *psi_parser_preprocess(struct psi_parser *P, struct psi_plist **tokens) { if (psi_cpp_process(P->preproc, tokens)) { @@ -223,15 +166,20 @@ void psi_parser_postprocess(struct psi_parser *P) struct psi_impl_def_val *def; struct psi_const *cnst; struct psi_num_exp *num; - char *name_str = malloc(name->len + sizeof("psi\\")); + smart_str ns_name = {0}; + zend_string *name_str; + + smart_str_appendl_ex(&ns_name, ZEND_STRL("psi\\"), 1); + smart_str_append_ex(&ns_name, name, 1); + name_str = smart_str_extract(&ns_name); - strcat(strcpy(name_str, "psi\\"), name->val); num = psi_num_exp_copy(scope.macro->exp); def = psi_impl_def_val_init(PSI_T_NUMBER, num); - type = psi_impl_type_init(PSI_T_NUMBER, ""); + type = psi_impl_type_init(PSI_T_NUMBER, + zend_string_init(ZEND_STRL(""), 1)); cnst = psi_const_init(type, name_str, def); P->consts = psi_plist_add(P->consts, &cnst); - free(name_str); + zend_string_release(name_str); } } else { if (psi_plist_count(scope.macro->tokens) == 1) { @@ -242,14 +190,19 @@ void psi_parser_postprocess(struct psi_parser *P) struct psi_impl_type *type; struct psi_impl_def_val *def; struct psi_const *cnst; - char *name_str = malloc(name->len + sizeof("psi\\")); + smart_str ns_name = {0}; + zend_string *name_str; + + smart_str_appendl_ex(&ns_name, ZEND_STRL("psi\\"), 1); + smart_str_append_ex(&ns_name, name, 1); + name_str = smart_str_extract(&ns_name); - strcat(strcpy(name_str, "psi\\"), name->val); - type = psi_impl_type_init(PSI_T_STRING, "string"); + type = psi_impl_type_init(PSI_T_STRING, + zend_string_init(ZEND_STRL("string"), 1)); def = psi_impl_def_val_init(PSI_T_QUOTED_STRING, t->text); cnst = psi_const_init(type, name_str, def); P->consts = psi_plist_add(P->consts, &cnst); - free(name_str); + zend_string_release(name_str); } } } @@ -330,7 +283,7 @@ struct psi_plist *psi_parser_scan(struct psi_parser *P, struct psi_parser_input bool escaped; token_t char_width; - PSI_DEBUG_PRINT(P, "PSI: scanning %s\n", I->file); + PSI_DEBUG_PRINT(P, "PSI: scanning %s\n", I->file->val); tok = mrk = eol = cur = I->buffer; lim = I->buffer + I->length; @@ -345,7 +298,7 @@ struct psi_plist *psi_parser_scan(struct psi_parser *P, struct psi_parser_input (void) ctxmrk; -#line 349 "src/parser.c" +#line 302 "src/parser.c" { unsigned char yych; unsigned int yyaccept = 0; @@ -470,9 +423,9 @@ struct psi_plist *psi_parser_scan(struct psi_parser *P, struct psi_parser_input yy2: ++cur; yy3: -#line 514 "src/parser.re" +#line 467 "src/parser.re" { NEWTOKEN(-2); goto error; } -#line 476 "src/parser.c" +#line 429 "src/parser.c" yy4: ++cur; if (lim <= cur) if (cur >= lim) goto done;; @@ -484,14 +437,14 @@ yy4: default: goto yy6; } yy6: -#line 513 "src/parser.re" +#line 466 "src/parser.re" { NEWTOKEN(PSI_T_WHITESPACE); goto start; } -#line 490 "src/parser.c" +#line 443 "src/parser.c" yy7: ++cur; -#line 512 "src/parser.re" +#line 465 "src/parser.re" { NEWTOKEN(PSI_T_EOL); NEWLINE(); goto start; } -#line 495 "src/parser.c" +#line 448 "src/parser.c" yy9: yych = *++cur; switch (yych) { @@ -499,14 +452,14 @@ yy9: default: goto yy10; } yy10: -#line 415 "src/parser.re" +#line 368 "src/parser.re" { NEWTOKEN(PSI_T_NOT); goto start; } -#line 505 "src/parser.c" +#line 458 "src/parser.c" yy11: ++cur; -#line 388 "src/parser.re" +#line 341 "src/parser.re" { escaped = false; tok += 1; goto string; } -#line 510 "src/parser.c" +#line 463 "src/parser.c" yy13: yych = *++cur; switch (yych) { @@ -514,9 +467,9 @@ yy13: default: goto yy14; } yy14: -#line 398 "src/parser.re" +#line 351 "src/parser.re" { NEWTOKEN(PSI_T_HASH); goto start; } -#line 520 "src/parser.c" +#line 473 "src/parser.c" yy15: yych = *++cur; switch (yych) { @@ -589,9 +542,9 @@ yy15: } yy16: ++cur; -#line 416 "src/parser.re" +#line 369 "src/parser.re" { NEWTOKEN(PSI_T_MODULO); goto start; } -#line 595 "src/parser.c" +#line 548 "src/parser.c" yy18: yych = *++cur; switch (yych) { @@ -599,29 +552,29 @@ yy18: default: goto yy19; } yy19: -#line 417 "src/parser.re" +#line 370 "src/parser.re" { NEWTOKEN(PSI_T_AMPERSAND); goto start; } -#line 605 "src/parser.c" +#line 558 "src/parser.c" yy20: ++cur; -#line 387 "src/parser.re" +#line 340 "src/parser.re" { escaped = false; tok += 1; goto character; } -#line 610 "src/parser.c" +#line 563 "src/parser.c" yy22: ++cur; -#line 399 "src/parser.re" +#line 352 "src/parser.re" { NEWTOKEN(PSI_T_LPAREN); goto start; } -#line 615 "src/parser.c" +#line 568 "src/parser.c" yy24: ++cur; -#line 400 "src/parser.re" +#line 353 "src/parser.re" { NEWTOKEN(PSI_T_RPAREN); goto start; } -#line 620 "src/parser.c" +#line 573 "src/parser.c" yy26: ++cur; -#line 413 "src/parser.re" +#line 366 "src/parser.re" { NEWTOKEN(PSI_T_ASTERISK); goto start; } -#line 625 "src/parser.c" +#line 578 "src/parser.c" yy28: yyaccept = 0; yych = *(mrk = ++cur); @@ -640,14 +593,14 @@ yy28: default: goto yy29; } yy29: -#line 418 "src/parser.re" +#line 371 "src/parser.re" { NEWTOKEN(PSI_T_PLUS); goto start; } -#line 646 "src/parser.c" +#line 599 "src/parser.c" yy30: ++cur; -#line 402 "src/parser.re" +#line 355 "src/parser.re" { NEWTOKEN(PSI_T_COMMA); goto start; } -#line 651 "src/parser.c" +#line 604 "src/parser.c" yy32: yyaccept = 1; yych = *(mrk = ++cur); @@ -666,9 +619,9 @@ yy32: default: goto yy33; } yy33: -#line 419 "src/parser.re" +#line 372 "src/parser.re" { NEWTOKEN(PSI_T_MINUS); goto start; } -#line 672 "src/parser.c" +#line 625 "src/parser.c" yy34: yyaccept = 2; yych = *(mrk = ++cur); @@ -687,9 +640,9 @@ yy34: default: goto yy35; } yy35: -#line 430 "src/parser.re" +#line 383 "src/parser.re" { NEWTOKEN(PSI_T_PERIOD); goto start; } -#line 693 "src/parser.c" +#line 646 "src/parser.c" yy36: yych = *++cur; switch (yych) { @@ -698,9 +651,9 @@ yy36: default: goto yy37; } yy37: -#line 420 "src/parser.re" +#line 373 "src/parser.re" { NEWTOKEN(PSI_T_SLASH); goto start; } -#line 704 "src/parser.c" +#line 657 "src/parser.c" yy38: yyaccept = 3; yych = *(mrk = ++cur); @@ -713,9 +666,9 @@ yy38: default: goto yy126; } yy39: -#line 374 "src/parser.re" +#line 327 "src/parser.re" { NEWTOKEN(PSI_T_NUMBER); token->flags = PSI_NUMBER_INT; goto start; } -#line 719 "src/parser.c" +#line 672 "src/parser.c" yy40: yyaccept = 3; mrk = ++cur; @@ -743,14 +696,14 @@ yy40: } yy42: ++cur; -#line 403 "src/parser.re" +#line 356 "src/parser.re" { NEWTOKEN(PSI_T_COLON); goto start; } -#line 749 "src/parser.c" +#line 702 "src/parser.c" yy44: ++cur; -#line 401 "src/parser.re" +#line 354 "src/parser.re" { NEWTOKEN(PSI_T_EOS); goto start; } -#line 754 "src/parser.c" +#line 707 "src/parser.c" yy46: yyaccept = 4; yych = *(mrk = ++cur); @@ -826,9 +779,9 @@ yy46: default: goto yy47; } yy47: -#line 428 "src/parser.re" +#line 381 "src/parser.re" { NEWTOKEN(PSI_T_LCHEVR); goto start; } -#line 832 "src/parser.c" +#line 785 "src/parser.c" yy48: yych = *++cur; switch (yych) { @@ -836,9 +789,9 @@ yy48: default: goto yy49; } yy49: -#line 412 "src/parser.re" +#line 365 "src/parser.re" { NEWTOKEN(PSI_T_EQUALS); goto start; } -#line 842 "src/parser.c" +#line 795 "src/parser.c" yy50: yych = *++cur; switch (yych) { @@ -847,14 +800,14 @@ yy50: default: goto yy51; } yy51: -#line 429 "src/parser.re" +#line 382 "src/parser.re" { NEWTOKEN(PSI_T_RCHEVR); goto start; } -#line 853 "src/parser.c" +#line 806 "src/parser.c" yy52: ++cur; -#line 432 "src/parser.re" +#line 385 "src/parser.re" { NEWTOKEN(PSI_T_IIF); goto start; } -#line 858 "src/parser.c" +#line 811 "src/parser.c" yy54: yyaccept = 5; yych = *(mrk = ++cur); @@ -866,9 +819,9 @@ yy54: default: goto yy62; } yy55: -#line 507 "src/parser.re" +#line 460 "src/parser.re" { NEWTOKEN(PSI_T_NAME); goto start; } -#line 872 "src/parser.c" +#line 825 "src/parser.c" yy56: yyaccept = 5; yych = *(mrk = ++cur); @@ -1111,9 +1064,9 @@ yy74: } yy75: ++cur; -#line 406 "src/parser.re" +#line 359 "src/parser.re" { NEWTOKEN(PSI_T_LBRACKET); goto start; } -#line 1117 "src/parser.c" +#line 1070 "src/parser.c" yy77: yych = *++cur; switch (yych) { @@ -1195,19 +1148,19 @@ yy77: default: goto yy184; } yy78: -#line 421 "src/parser.re" +#line 374 "src/parser.re" { NEWTOKEN(PSI_T_BSLASH); goto start; } -#line 1201 "src/parser.c" +#line 1154 "src/parser.c" yy79: ++cur; -#line 407 "src/parser.re" +#line 360 "src/parser.re" { NEWTOKEN(PSI_T_RBRACKET); goto start; } -#line 1206 "src/parser.c" +#line 1159 "src/parser.c" yy81: ++cur; -#line 423 "src/parser.re" +#line 376 "src/parser.re" { NEWTOKEN(PSI_T_CARET); goto start; } -#line 1211 "src/parser.c" +#line 1164 "src/parser.c" yy83: yyaccept = 5; yych = *(mrk = ++cur); @@ -1347,9 +1300,9 @@ yy95: } yy96: ++cur; -#line 404 "src/parser.re" +#line 357 "src/parser.re" { NEWTOKEN(PSI_T_LBRACE); goto start; } -#line 1353 "src/parser.c" +#line 1306 "src/parser.c" yy98: yych = *++cur; switch (yych) { @@ -1357,29 +1310,29 @@ yy98: default: goto yy99; } yy99: -#line 422 "src/parser.re" +#line 375 "src/parser.re" { NEWTOKEN(PSI_T_PIPE); goto start; } -#line 1363 "src/parser.c" +#line 1316 "src/parser.c" yy100: ++cur; -#line 405 "src/parser.re" +#line 358 "src/parser.re" { NEWTOKEN(PSI_T_RBRACE); goto start; } -#line 1368 "src/parser.c" +#line 1321 "src/parser.c" yy102: ++cur; -#line 414 "src/parser.re" +#line 367 "src/parser.re" { NEWTOKEN(PSI_T_TILDE); goto start; } -#line 1373 "src/parser.c" +#line 1326 "src/parser.c" yy104: ++cur; -#line 408 "src/parser.re" +#line 361 "src/parser.re" { NEWTOKEN(PSI_T_CMP_NE); goto start; } -#line 1378 "src/parser.c" +#line 1331 "src/parser.c" yy106: ++cur; -#line 397 "src/parser.re" +#line 350 "src/parser.re" { NEWTOKEN(PSI_T_CPP_PASTE); goto start; } -#line 1383 "src/parser.c" +#line 1336 "src/parser.c" yy108: ++cur; if (lim <= cur) if (cur >= lim) goto done;; @@ -1453,14 +1406,14 @@ yy108: default: goto yy108; } yy110: -#line 509 "src/parser.re" +#line 462 "src/parser.re" { NEWTOKEN(PSI_T_DOLLAR_NAME); goto start; } -#line 1459 "src/parser.c" +#line 1412 "src/parser.c" yy111: ++cur; -#line 410 "src/parser.re" +#line 363 "src/parser.re" { NEWTOKEN(PSI_T_AND); goto start; } -#line 1464 "src/parser.c" +#line 1417 "src/parser.c" yy113: yych = *++cur; switch (yych) { @@ -1594,19 +1547,19 @@ yy116: default: goto yy118; } yy118: -#line 380 "src/parser.re" +#line 333 "src/parser.re" { NEWTOKEN(PSI_T_NUMBER); token->flags = PSI_NUMBER_FLT; goto start; } -#line 1600 "src/parser.c" +#line 1553 "src/parser.c" yy119: ++cur; -#line 394 "src/parser.re" +#line 347 "src/parser.re" { goto comment; } -#line 1605 "src/parser.c" +#line 1558 "src/parser.c" yy121: ++cur; -#line 395 "src/parser.re" +#line 348 "src/parser.re" { goto comment_sl; } -#line 1610 "src/parser.c" +#line 1563 "src/parser.c" yy123: yyaccept = 6; mrk = ++cur; @@ -1682,9 +1635,9 @@ yy128: } yy129: cur -= 1; -#line 376 "src/parser.re" +#line 329 "src/parser.re" { NEWTOKEN(PSI_T_NUMBER); token->flags = PSI_NUMBER_INT | PSI_NUMBER_L; cur += 1; goto start; } -#line 1688 "src/parser.c" +#line 1641 "src/parser.c" yy130: yych = *++cur; switch (yych) { @@ -1694,9 +1647,9 @@ yy130: } yy131: cur -= 1; -#line 375 "src/parser.re" +#line 328 "src/parser.re" { NEWTOKEN(PSI_T_NUMBER); token->flags = PSI_NUMBER_INT | PSI_NUMBER_U; cur += 1; goto start; } -#line 1700 "src/parser.c" +#line 1653 "src/parser.c" yy132: yych = *++cur; switch (yych) { @@ -1800,29 +1753,29 @@ yy133: } yy135: ++cur; -#line 424 "src/parser.re" +#line 377 "src/parser.re" { NEWTOKEN(PSI_T_LSHIFT); goto start; } -#line 1806 "src/parser.c" +#line 1759 "src/parser.c" yy137: ++cur; -#line 426 "src/parser.re" +#line 379 "src/parser.re" { NEWTOKEN(PSI_T_CMP_LE); goto start; } -#line 1811 "src/parser.c" +#line 1764 "src/parser.c" yy139: ++cur; -#line 409 "src/parser.re" +#line 362 "src/parser.re" { NEWTOKEN(PSI_T_CMP_EQ); goto start; } -#line 1816 "src/parser.c" +#line 1769 "src/parser.c" yy141: ++cur; -#line 427 "src/parser.re" +#line 380 "src/parser.re" { NEWTOKEN(PSI_T_CMP_GE); goto start; } -#line 1821 "src/parser.c" +#line 1774 "src/parser.c" yy143: ++cur; -#line 425 "src/parser.re" +#line 378 "src/parser.re" { NEWTOKEN(PSI_T_RSHIFT); goto start; } -#line 1826 "src/parser.c" +#line 1779 "src/parser.c" yy145: yyaccept = 5; yych = *(mrk = ++cur); @@ -1903,9 +1856,9 @@ yy146: default: goto yy61; } yy147: -#line 487 "src/parser.re" +#line 440 "src/parser.re" { NEWTOKEN(PSI_T_AS); goto start; } -#line 1909 "src/parser.c" +#line 1862 "src/parser.c" yy148: ++cur; if (lim <= cur) if (cur >= lim) goto done;; @@ -2154,9 +2107,9 @@ yy160: default: goto yy61; } yy161: -#line 457 "src/parser.re" +#line 410 "src/parser.re" { NEWTOKEN(PSI_T_IF); goto start; } -#line 2160 "src/parser.c" +#line 2113 "src/parser.c" yy162: yyaccept = 5; yych = *(mrk = ++cur); @@ -2170,9 +2123,9 @@ yy162: yy163: ++cur; cur -= 1; -#line 392 "src/parser.re" +#line 345 "src/parser.re" { char_width = sizeof(wchar_t)/8; } -#line 2176 "src/parser.c" +#line 2129 "src/parser.c" yy165: yyaccept = 5; yych = *(mrk = ++cur); @@ -2289,9 +2242,9 @@ yy178: yy179: ++cur; cur -= 1; -#line 391 "src/parser.re" +#line 344 "src/parser.re" { char_width = 4; } -#line 2295 "src/parser.c" +#line 2248 "src/parser.c" yy181: yyaccept = 5; yych = *(mrk = ++cur); @@ -2390,9 +2343,9 @@ yy184: default: goto yy184; } yy186: -#line 508 "src/parser.re" +#line 461 "src/parser.re" { NEWTOKEN(PSI_T_NSNAME); goto start; } -#line 2396 "src/parser.c" +#line 2349 "src/parser.c" yy187: yyaccept = 5; yych = *(mrk = ++cur); @@ -2521,9 +2474,9 @@ yy201: yy202: ++cur; cur -= 1; -#line 390 "src/parser.re" +#line 343 "src/parser.re" { char_width = 2; } -#line 2527 "src/parser.c" +#line 2480 "src/parser.c" yy204: yyaccept = 5; yych = *(mrk = ++cur); @@ -2551,14 +2504,14 @@ yy206: } yy207: ++cur; -#line 411 "src/parser.re" +#line 364 "src/parser.re" { NEWTOKEN(PSI_T_OR); goto start; } -#line 2557 "src/parser.c" +#line 2510 "src/parser.c" yy209: ++cur; -#line 431 "src/parser.re" +#line 384 "src/parser.re" { NEWTOKEN(PSI_T_ELLIPSIS); goto start; } -#line 2562 "src/parser.c" +#line 2515 "src/parser.c" yy211: yych = *++cur; switch (yych) { @@ -2573,15 +2526,15 @@ yy211: yy212: ++cur; cur -= 1; -#line 381 "src/parser.re" +#line 334 "src/parser.re" { NEWTOKEN(PSI_T_NUMBER); token->flags = PSI_NUMBER_FLT | PSI_NUMBER_F; cur += 1; goto start; } -#line 2579 "src/parser.c" +#line 2532 "src/parser.c" yy214: ++cur; cur -= 1; -#line 382 "src/parser.re" +#line 335 "src/parser.re" { NEWTOKEN(PSI_T_NUMBER); token->flags = PSI_NUMBER_FLT | PSI_NUMBER_L; cur += 1; goto start; } -#line 2585 "src/parser.c" +#line 2538 "src/parser.c" yy216: yych = *++cur; switch (yych) { @@ -2593,9 +2546,9 @@ yy217: ++cur; yy218: cur -= 2; -#line 377 "src/parser.re" +#line 330 "src/parser.re" { NEWTOKEN(PSI_T_NUMBER); token->flags = PSI_NUMBER_INT | PSI_NUMBER_UL; cur += 2; goto start; } -#line 2599 "src/parser.c" +#line 2552 "src/parser.c" yy219: yych = *++cur; switch (yych) { @@ -2642,9 +2595,9 @@ yy220: } yy222: ++cur; -#line 510 "src/parser.re" +#line 463 "src/parser.re" { tok += 1; cur -= 1; NEWTOKEN(PSI_T_CPP_HEADER); cur += 1; goto start; } -#line 2648 "src/parser.c" +#line 2601 "src/parser.c" yy224: yyaccept = 5; yych = *(mrk = ++cur); @@ -2855,9 +2808,9 @@ yy241: default: goto yy61; } yy242: -#line 482 "src/parser.re" +#line 435 "src/parser.re" { NEWTOKEN(PSI_T_LET); goto start; } -#line 2861 "src/parser.c" +#line 2814 "src/parser.c" yy243: yyaccept = 12; yych = *(mrk = ++cur); @@ -2930,9 +2883,9 @@ yy243: default: goto yy61; } yy244: -#line 481 "src/parser.re" +#line 434 "src/parser.re" { NEWTOKEN(PSI_T_LIB); goto start; } -#line 2936 "src/parser.c" +#line 2889 "src/parser.c" yy245: yyaccept = 5; yych = *(mrk = ++cur); @@ -3062,9 +3015,9 @@ yy252: default: goto yy61; } yy253: -#line 483 "src/parser.re" +#line 436 "src/parser.re" { NEWTOKEN(PSI_T_SET); goto start; } -#line 3068 "src/parser.c" +#line 3021 "src/parser.c" yy254: yyaccept = 5; yych = *(mrk = ++cur); @@ -3290,9 +3243,9 @@ yy272: default: goto yy61; } yy273: -#line 451 "src/parser.re" +#line 404 "src/parser.re" { NEWTOKEN(PSI_T_INT); goto start; } -#line 3296 "src/parser.c" +#line 3249 "src/parser.c" yy274: yyaccept = 5; yych = *(mrk = ++cur); @@ -3358,9 +3311,9 @@ yy281: yy282: ++cur; cur -= 1; -#line 389 "src/parser.re" +#line 342 "src/parser.re" { char_width = 1; } -#line 3364 "src/parser.c" +#line 3317 "src/parser.c" yy284: yyaccept = 5; yych = *(mrk = ++cur); @@ -3392,27 +3345,27 @@ yy287: yy288: ++cur; cur -= 2; -#line 384 "src/parser.re" +#line 337 "src/parser.re" { NEWTOKEN(PSI_T_NUMBER); token->flags = PSI_NUMBER_FLT | PSI_NUMBER_DD; cur += 2; goto start; } -#line 3398 "src/parser.c" +#line 3351 "src/parser.c" yy290: ++cur; cur -= 2; -#line 383 "src/parser.re" +#line 336 "src/parser.re" { NEWTOKEN(PSI_T_NUMBER); token->flags = PSI_NUMBER_FLT | PSI_NUMBER_DF; cur += 2; goto start; } -#line 3404 "src/parser.c" +#line 3357 "src/parser.c" yy292: ++cur; cur -= 2; -#line 385 "src/parser.re" +#line 338 "src/parser.re" { NEWTOKEN(PSI_T_NUMBER); token->flags = PSI_NUMBER_FLT | PSI_NUMBER_DL; cur += 2; goto start; } -#line 3410 "src/parser.c" +#line 3363 "src/parser.c" yy294: ++cur; cur -= 3; -#line 378 "src/parser.re" +#line 331 "src/parser.re" { NEWTOKEN(PSI_T_NUMBER); token->flags = PSI_NUMBER_INT | PSI_NUMBER_ULL; cur += 3; goto start; } -#line 3416 "src/parser.c" +#line 3369 "src/parser.c" yy296: ++cur; if ((lim - cur) < 3) if (cur >= lim) goto done;; @@ -3568,9 +3521,9 @@ yy304: default: goto yy61; } yy305: -#line 461 "src/parser.re" +#line 414 "src/parser.re" { NEWTOKEN(PSI_T_ELIF); goto start; } -#line 3574 "src/parser.c" +#line 3527 "src/parser.c" yy306: yyaccept = 16; yych = *(mrk = ++cur); @@ -3643,9 +3596,9 @@ yy306: default: goto yy61; } yy307: -#line 460 "src/parser.re" +#line 413 "src/parser.re" { NEWTOKEN(PSI_T_ELSE); goto start; } -#line 3649 "src/parser.c" +#line 3602 "src/parser.c" yy308: yyaccept = 5; yych = *(mrk = ++cur); @@ -3750,9 +3703,9 @@ yy312: default: goto yy61; } yy313: -#line 488 "src/parser.re" +#line 441 "src/parser.re" { NEWTOKEN(PSI_T_FREE); goto start; } -#line 3756 "src/parser.c" +#line 3709 "src/parser.c" yy314: yyaccept = 5; yych = *(mrk = ++cur); @@ -3873,9 +3826,9 @@ yy320: default: goto yy61; } yy321: -#line 472 "src/parser.re" +#line 425 "src/parser.re" { NEWTOKEN(PSI_T_NULL); goto start; } -#line 3879 "src/parser.c" +#line 3832 "src/parser.c" yy322: yyaccept = 5; yych = *(mrk = ++cur); @@ -4027,9 +3980,9 @@ yy332: default: goto yy61; } yy333: -#line 489 "src/parser.re" +#line 442 "src/parser.re" { NEWTOKEN(PSI_T_TEMP); goto start; } -#line 4033 "src/parser.c" +#line 3986 "src/parser.c" yy334: yyaccept = 5; yych = *(mrk = ++cur); @@ -4150,9 +4103,9 @@ yy340: default: goto yy61; } yy341: -#line 470 "src/parser.re" +#line 423 "src/parser.re" { NEWTOKEN(PSI_T_TRUE); goto start; } -#line 4156 "src/parser.c" +#line 4109 "src/parser.c" yy342: yyaccept = 5; yych = *(mrk = ++cur); @@ -4241,9 +4194,9 @@ yy344: default: goto yy61; } yy345: -#line 498 "src/parser.re" +#line 451 "src/parser.re" { NEWTOKEN(PSI_T_ZVAL); goto start; } -#line 4247 "src/parser.c" +#line 4200 "src/parser.c" yy346: yyaccept = 5; yych = *(mrk = ++cur); @@ -4353,9 +4306,9 @@ yy351: default: goto yy61; } yy352: -#line 448 "src/parser.re" +#line 401 "src/parser.re" { NEWTOKEN(PSI_T_BOOL); goto start; } -#line 4359 "src/parser.c" +#line 4312 "src/parser.c" yy353: yyaccept = 23; yych = *(mrk = ++cur); @@ -4428,9 +4381,9 @@ yy353: default: goto yy61; } yy354: -#line 449 "src/parser.re" +#line 402 "src/parser.re" { NEWTOKEN(PSI_T_CHAR); goto start; } -#line 4434 "src/parser.c" +#line 4387 "src/parser.c" yy355: yyaccept = 5; yych = *(mrk = ++cur); @@ -4517,9 +4470,9 @@ yy357: default: goto yy61; } yy358: -#line 445 "src/parser.re" +#line 398 "src/parser.re" { NEWTOKEN(PSI_T_ENUM); goto start; } -#line 4523 "src/parser.c" +#line 4476 "src/parser.c" yy359: yyaccept = 5; yych = *(mrk = ++cur); @@ -4600,9 +4553,9 @@ yy360: default: goto yy61; } yy361: -#line 441 "src/parser.re" +#line 394 "src/parser.re" { NEWTOKEN(PSI_T_LINE); goto start; } -#line 4606 "src/parser.c" +#line 4559 "src/parser.c" yy362: yyaccept = 26; yych = *(mrk = ++cur); @@ -4675,9 +4628,9 @@ yy362: default: goto yy61; } yy363: -#line 452 "src/parser.re" +#line 405 "src/parser.re" { NEWTOKEN(PSI_T_LONG); goto start; } -#line 4681 "src/parser.c" +#line 4634 "src/parser.c" yy364: yyaccept = 5; yych = *(mrk = ++cur); @@ -4806,9 +4759,9 @@ yy372: default: goto yy61; } yy373: -#line 447 "src/parser.re" +#line 400 "src/parser.re" { NEWTOKEN(PSI_T_VOID); goto start; } -#line 4812 "src/parser.c" +#line 4765 "src/parser.c" yy374: yyaccept = 5; yych = *(mrk = ++cur); @@ -4888,9 +4841,9 @@ yy375: default: goto yy61; } yy376: -#line 476 "src/parser.re" +#line 429 "src/parser.re" { NEWTOKEN(PSI_T_ARRAY); goto start; } -#line 4894 "src/parser.c" +#line 4847 "src/parser.c" yy377: yyaccept = 5; yych = *(mrk = ++cur); @@ -5003,9 +4956,9 @@ yy382: default: goto yy61; } yy383: -#line 499 "src/parser.re" +#line 452 "src/parser.re" { NEWTOKEN(PSI_T_COUNT); goto start; } -#line 5009 "src/parser.c" +#line 4962 "src/parser.c" yy384: yyaccept = 5; yych = *(mrk = ++cur); @@ -5086,9 +5039,9 @@ yy385: default: goto yy61; } yy386: -#line 462 "src/parser.re" +#line 415 "src/parser.re" { NEWTOKEN(PSI_T_ENDIF); goto start; } -#line 5092 "src/parser.c" +#line 5045 "src/parser.c" yy387: yyaccept = 31; yych = *(mrk = ++cur); @@ -5161,9 +5114,9 @@ yy387: default: goto yy61; } yy388: -#line 467 "src/parser.re" +#line 420 "src/parser.re" { NEWTOKEN(PSI_T_ERROR); goto start; } -#line 5167 "src/parser.c" +#line 5120 "src/parser.c" yy389: yyaccept = 32; yych = *(mrk = ++cur); @@ -5236,9 +5189,9 @@ yy389: default: goto yy61; } yy390: -#line 471 "src/parser.re" +#line 424 "src/parser.re" { NEWTOKEN(PSI_T_FALSE); goto start; } -#line 5242 "src/parser.c" +#line 5195 "src/parser.c" yy391: yyaccept = 5; yych = *(mrk = ++cur); @@ -5327,9 +5280,9 @@ yy393: default: goto yy61; } yy394: -#line 458 "src/parser.re" +#line 411 "src/parser.re" { NEWTOKEN(PSI_T_IFDEF); goto start; } -#line 5333 "src/parser.c" +#line 5286 "src/parser.c" yy395: yyaccept = 5; yych = *(mrk = ++cur); @@ -5426,9 +5379,9 @@ yy398: default: goto yy61; } yy399: -#line 473 "src/parser.re" +#line 426 "src/parser.re" { NEWTOKEN(PSI_T_MIXED); goto start; } -#line 5432 "src/parser.c" +#line 5385 "src/parser.c" yy400: yyaccept = 5; yych = *(mrk = ++cur); @@ -5629,9 +5582,9 @@ yy416: default: goto yy61; } yy417: -#line 465 "src/parser.re" +#line 418 "src/parser.re" { NEWTOKEN(PSI_T_UNDEF); goto start; } -#line 5635 "src/parser.c" +#line 5588 "src/parser.c" yy418: yyaccept = 5; yych = *(mrk = ++cur); @@ -5747,9 +5700,9 @@ yy424: default: goto yy61; } yy425: -#line 446 "src/parser.re" +#line 399 "src/parser.re" { NEWTOKEN(PSI_T_CONST); goto start; } -#line 5753 "src/parser.c" +#line 5706 "src/parser.c" yy426: yyaccept = 5; yych = *(mrk = ++cur); @@ -5831,9 +5784,9 @@ yy427: default: goto yy61; } yy428: -#line 453 "src/parser.re" +#line 406 "src/parser.re" { NEWTOKEN(PSI_T_FLOAT); goto start; } -#line 5837 "src/parser.c" +#line 5790 "src/parser.c" yy429: yyaccept = 5; yych = *(mrk = ++cur); @@ -5913,9 +5866,9 @@ yy430: default: goto yy61; } yy431: -#line 450 "src/parser.re" +#line 403 "src/parser.re" { NEWTOKEN(PSI_T_SHORT); goto start; } -#line 5919 "src/parser.c" +#line 5872 "src/parser.c" yy432: yyaccept = 5; yych = *(mrk = ++cur); @@ -6016,9 +5969,9 @@ yy436: default: goto yy61; } yy437: -#line 444 "src/parser.re" +#line 397 "src/parser.re" { NEWTOKEN(PSI_T_UNION); goto start; } -#line 6022 "src/parser.c" +#line 5975 "src/parser.c" yy438: yyaccept = 5; yych = *(mrk = ++cur); @@ -6105,9 +6058,9 @@ yy440: default: goto yy61; } yy441: -#line 496 "src/parser.re" +#line 449 "src/parser.re" { NEWTOKEN(PSI_T_ARRVAL); goto start; } -#line 6111 "src/parser.c" +#line 6064 "src/parser.c" yy442: yyaccept = 5; yych = *(mrk = ++cur); @@ -6204,9 +6157,9 @@ yy445: default: goto yy61; } yy446: -#line 500 "src/parser.re" +#line 453 "src/parser.re" { NEWTOKEN(PSI_T_CALLOC); goto start; } -#line 6210 "src/parser.c" +#line 6163 "src/parser.c" yy447: yyaccept = 42; yych = *(mrk = ++cur); @@ -6281,9 +6234,9 @@ yy447: default: goto yy61; } yy448: -#line 463 "src/parser.re" +#line 416 "src/parser.re" { NEWTOKEN(PSI_T_DEFINE); goto start; } -#line 6287 "src/parser.c" +#line 6240 "src/parser.c" yy449: yyaccept = 5; yych = *(mrk = ++cur); @@ -6372,9 +6325,9 @@ yy451: default: goto yy61; } yy452: -#line 459 "src/parser.re" +#line 412 "src/parser.re" { NEWTOKEN(PSI_T_IFNDEF); goto start; } -#line 6378 "src/parser.c" +#line 6331 "src/parser.c" yy453: yyaccept = 5; yych = *(mrk = ++cur); @@ -6455,9 +6408,9 @@ yy454: default: goto yy61; } yy455: -#line 493 "src/parser.re" +#line 446 "src/parser.re" { NEWTOKEN(PSI_T_INTVAL); goto start; } -#line 6461 "src/parser.c" +#line 6414 "src/parser.c" yy456: yyaccept = 45; yych = *(mrk = ++cur); @@ -6530,9 +6483,9 @@ yy456: default: goto yy61; } yy457: -#line 477 "src/parser.re" +#line 430 "src/parser.re" { NEWTOKEN(PSI_T_OBJECT); goto start; } -#line 6536 "src/parser.c" +#line 6489 "src/parser.c" yy458: yyaccept = 46; yych = *(mrk = ++cur); @@ -6605,9 +6558,9 @@ yy458: default: goto yy61; } yy459: -#line 497 "src/parser.re" +#line 450 "src/parser.re" { NEWTOKEN(PSI_T_OBJVAL); goto start; } -#line 6611 "src/parser.c" +#line 6564 "src/parser.c" yy460: yyaccept = 5; yych = *(mrk = ++cur); @@ -6704,9 +6657,9 @@ yy463: default: goto yy61; } yy464: -#line 486 "src/parser.re" +#line 439 "src/parser.re" { NEWTOKEN(PSI_T_RETURN); goto start; } -#line 6710 "src/parser.c" +#line 6663 "src/parser.c" yy465: yyaccept = 48; yych = *(mrk = ++cur); @@ -6779,9 +6732,9 @@ yy465: default: goto yy61; } yy466: -#line 479 "src/parser.re" +#line 432 "src/parser.re" { NEWTOKEN(PSI_T_STATIC); goto start; } -#line 6785 "src/parser.c" +#line 6738 "src/parser.c" yy467: yyaccept = 49; yych = *(mrk = ++cur); @@ -6854,9 +6807,9 @@ yy467: default: goto yy61; } yy468: -#line 475 "src/parser.re" +#line 428 "src/parser.re" { NEWTOKEN(PSI_T_STRING); goto start; } -#line 6860 "src/parser.c" +#line 6813 "src/parser.c" yy469: yyaccept = 50; yych = *(mrk = ++cur); @@ -6929,9 +6882,9 @@ yy469: default: goto yy61; } yy470: -#line 490 "src/parser.re" +#line 443 "src/parser.re" { NEWTOKEN(PSI_T_STRLEN); goto start; } -#line 6935 "src/parser.c" +#line 6888 "src/parser.c" yy471: yyaccept = 51; yych = *(mrk = ++cur); @@ -7004,9 +6957,9 @@ yy471: default: goto yy61; } yy472: -#line 491 "src/parser.re" +#line 444 "src/parser.re" { NEWTOKEN(PSI_T_STRVAL); goto start; } -#line 7010 "src/parser.c" +#line 6963 "src/parser.c" yy473: yyaccept = 5; yych = *(mrk = ++cur); @@ -7103,9 +7056,9 @@ yy476: default: goto yy61; } yy477: -#line 504 "src/parser.re" +#line 457 "src/parser.re" { NEWTOKEN(PSI_T_TO_INT); goto start; } -#line 7109 "src/parser.c" +#line 7062 "src/parser.c" yy478: yyaccept = 5; yych = *(mrk = ++cur); @@ -7237,9 +7190,9 @@ yy486: default: goto yy61; } yy487: -#line 454 "src/parser.re" +#line 407 "src/parser.re" { NEWTOKEN(PSI_T_DOUBLE); goto start; } -#line 7243 "src/parser.c" +#line 7196 "src/parser.c" yy488: yyaccept = 54; yych = *(mrk = ++cur); @@ -7312,9 +7265,9 @@ yy488: default: goto yy527; } yy489: -#line 433 "src/parser.re" +#line 386 "src/parser.re" { NEWTOKEN(PSI_T_PRAGMA); goto start; } -#line 7318 "src/parser.c" +#line 7271 "src/parser.c" yy490: yyaccept = 55; yych = *(mrk = ++cur); @@ -7387,9 +7340,9 @@ yy490: default: goto yy61; } yy491: -#line 456 "src/parser.re" +#line 409 "src/parser.re" { NEWTOKEN(PSI_T_SIGNED); goto start; } -#line 7393 "src/parser.c" +#line 7346 "src/parser.c" yy492: yyaccept = 56; yych = *(mrk = ++cur); @@ -7462,9 +7415,9 @@ yy492: default: goto yy61; } yy493: -#line 440 "src/parser.re" +#line 393 "src/parser.re" { NEWTOKEN(PSI_T_SIZEOF); goto start; } -#line 7468 "src/parser.c" +#line 7421 "src/parser.c" yy494: yyaccept = 57; yych = *(mrk = ++cur); @@ -7537,9 +7490,9 @@ yy494: default: goto yy61; } yy495: -#line 443 "src/parser.re" +#line 396 "src/parser.re" { NEWTOKEN(PSI_T_STRUCT); goto start; } -#line 7543 "src/parser.c" +#line 7496 "src/parser.c" yy496: yyaccept = 5; yych = *(mrk = ++cur); @@ -7633,9 +7586,9 @@ yy499: default: goto yy61; } yy500: -#line 495 "src/parser.re" +#line 448 "src/parser.re" { NEWTOKEN(PSI_T_BOOLVAL); goto start; } -#line 7639 "src/parser.c" +#line 7592 "src/parser.c" yy501: yyaccept = 5; yych = *(mrk = ++cur); @@ -7724,9 +7677,9 @@ yy503: default: goto yy61; } yy504: -#line 464 "src/parser.re" +#line 417 "src/parser.re" { NEWTOKEN(PSI_T_DEFINED); goto start; } -#line 7730 "src/parser.c" +#line 7683 "src/parser.c" yy505: yyaccept = 5; yych = *(mrk = ++cur); @@ -7816,9 +7769,9 @@ yy507: default: goto yy61; } yy508: -#line 468 "src/parser.re" +#line 421 "src/parser.re" { NEWTOKEN(PSI_T_INCLUDE); goto start; } -#line 7822 "src/parser.c" +#line 7775 "src/parser.c" yy509: yyaccept = 61; yych = *(mrk = ++cur); @@ -7891,9 +7844,9 @@ yy509: default: goto yy61; } yy510: -#line 492 "src/parser.re" +#line 445 "src/parser.re" { NEWTOKEN(PSI_T_PATHVAL); goto start; } -#line 7897 "src/parser.c" +#line 7850 "src/parser.c" yy511: yyaccept = 5; yych = *(mrk = ++cur); @@ -7990,9 +7943,9 @@ yy514: default: goto yy61; } yy515: -#line 506 "src/parser.re" +#line 459 "src/parser.re" { NEWTOKEN(PSI_T_TO_BOOL); goto start; } -#line 7996 "src/parser.c" +#line 7949 "src/parser.c" yy516: yyaccept = 5; yych = *(mrk = ++cur); @@ -8089,9 +8042,9 @@ yy519: default: goto yy61; } yy520: -#line 466 "src/parser.re" +#line 419 "src/parser.re" { NEWTOKEN(PSI_T_WARNING); goto start; } -#line 8095 "src/parser.c" +#line 8048 "src/parser.c" yy521: yyaccept = 64; yych = *(mrk = ++cur); @@ -8164,9 +8117,9 @@ yy521: default: goto yy61; } yy522: -#line 438 "src/parser.re" +#line 391 "src/parser.re" { NEWTOKEN(PSI_T_CPP_ASM); goto start; } -#line 8170 "src/parser.c" +#line 8123 "src/parser.c" yy523: yyaccept = 5; yych = *(mrk = ++cur); @@ -8341,9 +8294,9 @@ yy529: default: goto yy61; } yy530: -#line 442 "src/parser.re" +#line 395 "src/parser.re" { NEWTOKEN(PSI_T_TYPEDEF); goto start; } -#line 8347 "src/parser.c" +#line 8300 "src/parser.c" yy531: yyaccept = 5; yych = *(mrk = ++cur); @@ -8430,9 +8383,9 @@ yy533: default: goto yy61; } yy534: -#line 474 "src/parser.re" +#line 427 "src/parser.re" { NEWTOKEN(PSI_T_CALLABLE); goto start; } -#line 8436 "src/parser.c" +#line 8389 "src/parser.c" yy535: yyaccept = 67; yych = *(mrk = ++cur); @@ -8505,9 +8458,9 @@ yy535: default: goto yy61; } yy536: -#line 478 "src/parser.re" +#line 431 "src/parser.re" { NEWTOKEN(PSI_T_CALLBACK); goto start; } -#line 8511 "src/parser.c" +#line 8464 "src/parser.c" yy537: yyaccept = 68; yych = *(mrk = ++cur); @@ -8580,9 +8533,9 @@ yy537: default: goto yy61; } yy538: -#line 494 "src/parser.re" +#line 447 "src/parser.re" { NEWTOKEN(PSI_T_FLOATVAL); goto start; } -#line 8586 "src/parser.c" +#line 8539 "src/parser.c" yy539: yyaccept = 69; yych = *(mrk = ++cur); @@ -8655,9 +8608,9 @@ yy539: default: goto yy61; } yy540: -#line 480 "src/parser.re" +#line 433 "src/parser.re" { NEWTOKEN(PSI_T_FUNCTION); goto start; } -#line 8661 "src/parser.c" +#line 8614 "src/parser.c" yy541: yyaccept = 5; yych = *(mrk = ++cur); @@ -8754,9 +8707,9 @@ yy544: default: goto yy61; } yy545: -#line 502 "src/parser.re" +#line 455 "src/parser.re" { NEWTOKEN(PSI_T_TO_ARRAY); goto start; } -#line 8760 "src/parser.c" +#line 8713 "src/parser.c" yy546: yyaccept = 71; yych = *(mrk = ++cur); @@ -8829,9 +8782,9 @@ yy546: default: goto yy61; } yy547: -#line 505 "src/parser.re" +#line 458 "src/parser.re" { NEWTOKEN(PSI_T_TO_FLOAT); goto start; } -#line 8835 "src/parser.c" +#line 8788 "src/parser.c" yy548: yyaccept = 5; yych = *(mrk = ++cur); @@ -8934,9 +8887,9 @@ yy552: default: goto yy61; } yy553: -#line 435 "src/parser.re" +#line 388 "src/parser.re" { NEWTOKEN(PSI_T_CPP_INLINE); goto start; } -#line 8940 "src/parser.c" +#line 8893 "src/parser.c" yy554: yyaccept = 5; yych = *(mrk = ++cur); @@ -9091,9 +9044,9 @@ yy557: default: goto yy61; } yy558: -#line 455 "src/parser.re" +#line 408 "src/parser.re" { NEWTOKEN(PSI_T_UNSIGNED); goto start; } -#line 9097 "src/parser.c" +#line 9050 "src/parser.c" yy559: yyaccept = 74; yych = *(mrk = ++cur); @@ -9166,9 +9119,9 @@ yy559: default: goto yy61; } yy560: -#line 439 "src/parser.re" +#line 392 "src/parser.re" { NEWTOKEN(PSI_T_VOLATILE); goto start; } -#line 9172 "src/parser.c" +#line 9125 "src/parser.c" yy561: yyaccept = 5; yych = *(mrk = ++cur); @@ -9265,9 +9218,9 @@ yy564: default: goto yy61; } yy565: -#line 501 "src/parser.re" +#line 454 "src/parser.re" { NEWTOKEN(PSI_T_TO_OBJECT); goto start; } -#line 9271 "src/parser.c" +#line 9224 "src/parser.c" yy566: yyaccept = 76; yych = *(mrk = ++cur); @@ -9340,9 +9293,9 @@ yy566: default: goto yy61; } yy567: -#line 503 "src/parser.re" +#line 456 "src/parser.re" { NEWTOKEN(PSI_T_TO_STRING); goto start; } -#line 9346 "src/parser.c" +#line 9299 "src/parser.c" yy568: yyaccept = 5; yych = *(mrk = ++cur); @@ -9527,9 +9480,9 @@ yy574: default: goto yy61; } yy575: -#line 484 "src/parser.re" +#line 437 "src/parser.re" { NEWTOKEN(PSI_T_PRE_ASSERT); goto start; } -#line 9533 "src/parser.c" +#line 9486 "src/parser.c" yy576: yyaccept = 5; yych = *(mrk = ++cur); @@ -9616,9 +9569,9 @@ yy578: default: goto yy61; } yy579: -#line 436 "src/parser.re" +#line 389 "src/parser.re" { NEWTOKEN(PSI_T_CPP_RESTRICT); goto start; } -#line 9622 "src/parser.c" +#line 9575 "src/parser.c" yy580: yyaccept = 5; mrk = ++cur; @@ -9774,9 +9727,9 @@ yy582: default: goto yy61; } yy583: -#line 485 "src/parser.re" +#line 438 "src/parser.re" { NEWTOKEN(PSI_T_POST_ASSERT); goto start; } -#line 9780 "src/parser.c" +#line 9733 "src/parser.c" yy584: yyaccept = 5; yych = *(mrk = ++cur); @@ -9866,9 +9819,9 @@ yy586: default: goto yy527; } yy587: -#line 434 "src/parser.re" +#line 387 "src/parser.re" { NEWTOKEN(PSI_T_PRAGMA_ONCE); goto start; } -#line 9872 "src/parser.c" +#line 9825 "src/parser.c" yy588: yyaccept = 81; yych = *(mrk = ++cur); @@ -9941,9 +9894,9 @@ yy588: default: goto yy61; } yy589: -#line 469 "src/parser.re" +#line 422 "src/parser.re" { NEWTOKEN(PSI_T_INCLUDE_NEXT); goto start; } -#line 9947 "src/parser.c" +#line 9900 "src/parser.c" yy590: yyaccept = 5; yych = *(mrk = ++cur); @@ -10040,9 +9993,9 @@ yy593: default: goto yy61; } yy594: -#line 437 "src/parser.re" +#line 390 "src/parser.re" { NEWTOKEN(PSI_T_CPP_EXTENSION); goto start; } -#line 10046 "src/parser.c" +#line 9999 "src/parser.c" yy595: ++cur; if ((lim - cur) < 2) if (cur >= lim) goto done;; @@ -10062,16 +10015,16 @@ yy597: } yy598: ++cur; -#line 511 "src/parser.re" +#line 464 "src/parser.re" { parens = 2; goto cpp_attribute; } -#line 10068 "src/parser.c" +#line 10021 "src/parser.c" } -#line 517 "src/parser.re" +#line 470 "src/parser.re" character: ; -#line 10075 "src/parser.c" +#line 10028 "src/parser.c" { unsigned char yych; if (lim <= cur) if (cur >= lim) goto done;; @@ -10085,17 +10038,17 @@ yy598: } yy602: ++cur; -#line 535 "src/parser.re" +#line 488 "src/parser.re" { escaped = false; goto character; } -#line 10091 "src/parser.c" +#line 10044 "src/parser.c" yy604: ++cur; -#line 522 "src/parser.re" +#line 475 "src/parser.re" { NEWLINE(); goto character; } -#line 10096 "src/parser.c" +#line 10049 "src/parser.c" yy606: ++cur; -#line 524 "src/parser.re" +#line 477 "src/parser.re" { if (escaped) { escaped = false; @@ -10107,19 +10060,19 @@ yy606: token->flags = char_width; goto start; } -#line 10111 "src/parser.c" +#line 10064 "src/parser.c" yy608: ++cur; -#line 523 "src/parser.re" +#line 476 "src/parser.re" { escaped = !escaped; goto character; } -#line 10116 "src/parser.c" +#line 10069 "src/parser.c" } -#line 537 "src/parser.re" +#line 490 "src/parser.re" string: ; -#line 10123 "src/parser.c" +#line 10076 "src/parser.c" { unsigned char yych; if (lim <= cur) if (cur >= lim) goto done;; @@ -10133,17 +10086,17 @@ yy608: } yy612: ++cur; -#line 555 "src/parser.re" +#line 508 "src/parser.re" { escaped = false; goto string; } -#line 10139 "src/parser.c" +#line 10092 "src/parser.c" yy614: ++cur; -#line 542 "src/parser.re" +#line 495 "src/parser.re" { NEWLINE(); goto string; } -#line 10144 "src/parser.c" +#line 10097 "src/parser.c" yy616: ++cur; -#line 544 "src/parser.re" +#line 497 "src/parser.re" { if (escaped) { escaped = false; @@ -10155,19 +10108,19 @@ yy616: token->flags = char_width; goto start; } -#line 10159 "src/parser.c" +#line 10112 "src/parser.c" yy618: ++cur; -#line 543 "src/parser.re" +#line 496 "src/parser.re" { escaped = !escaped; goto string; } -#line 10164 "src/parser.c" +#line 10117 "src/parser.c" } -#line 557 "src/parser.re" +#line 510 "src/parser.re" comment: ; -#line 10171 "src/parser.c" +#line 10124 "src/parser.c" { unsigned char yych; if ((lim - cur) < 2) if (cur >= lim) goto done;; @@ -10181,14 +10134,14 @@ yy618: yy622: ++cur; yy623: -#line 564 "src/parser.re" +#line 517 "src/parser.re" { goto comment; } -#line 10187 "src/parser.c" +#line 10140 "src/parser.c" yy624: ++cur; -#line 562 "src/parser.re" +#line 515 "src/parser.re" { NEWLINE(); goto comment; } -#line 10192 "src/parser.c" +#line 10145 "src/parser.c" yy626: yych = *++cur; switch (yych) { @@ -10197,16 +10150,16 @@ yy626: } yy627: ++cur; -#line 563 "src/parser.re" +#line 516 "src/parser.re" { NEWTOKEN(PSI_T_COMMENT); goto start; } -#line 10203 "src/parser.c" +#line 10156 "src/parser.c" } -#line 566 "src/parser.re" +#line 519 "src/parser.re" comment_sl: ; -#line 10210 "src/parser.c" +#line 10163 "src/parser.c" { unsigned char yych; if (lim <= cur) if (cur >= lim) goto done;; @@ -10218,22 +10171,22 @@ yy627: } yy631: ++cur; -#line 572 "src/parser.re" +#line 525 "src/parser.re" { goto comment_sl; } -#line 10224 "src/parser.c" +#line 10177 "src/parser.c" yy633: ++cur; -#line 571 "src/parser.re" +#line 524 "src/parser.re" { NEWTOKEN(PSI_T_COMMENT); NEWLINE(); goto start; } -#line 10229 "src/parser.c" +#line 10182 "src/parser.c" } -#line 574 "src/parser.re" +#line 527 "src/parser.re" cpp_attribute: ; -#line 10237 "src/parser.c" +#line 10190 "src/parser.c" { unsigned char yych; if (lim <= cur) if (cur >= lim) goto done;; @@ -10247,31 +10200,31 @@ yy633: } yy637: ++cur; -#line 583 "src/parser.re" +#line 536 "src/parser.re" { goto cpp_attribute; } -#line 10253 "src/parser.c" +#line 10206 "src/parser.c" yy639: ++cur; -#line 582 "src/parser.re" +#line 535 "src/parser.re" { NEWLINE(); goto cpp_attribute; } -#line 10258 "src/parser.c" +#line 10211 "src/parser.c" yy641: ++cur; -#line 580 "src/parser.re" +#line 533 "src/parser.re" { ++parens; goto cpp_attribute; } -#line 10263 "src/parser.c" +#line 10216 "src/parser.c" yy643: ++cur; -#line 581 "src/parser.re" +#line 534 "src/parser.re" { if (parens == 1) { NEWTOKEN(PSI_T_CPP_ATTRIBUTE); goto start; } else { --parens; goto cpp_attribute; } } -#line 10268 "src/parser.c" +#line 10221 "src/parser.c" } -#line 585 "src/parser.re" +#line 538 "src/parser.re" error: ; P->error(PSI_DATA(P), token, PSI_WARNING, "PSI syntax error: unexpected input (%d) '%.*s' at col %tu", - token->type, token->size, token->text, tok - eol + 1); + token->type, token->text->len, token->text->val, tok - eol + 1); psi_plist_free(tokens); return NULL; diff --git a/src/parser.h b/src/parser.h index d4cfb22..261b2b9 100644 --- a/src/parser.h +++ b/src/parser.h @@ -46,7 +46,7 @@ struct psi_parser { struct psi_parser_input { size_t length; - char *file; + zend_string *file; unsigned lines; char buffer[1]; }; diff --git a/src/parser.re b/src/parser.re index 7892b14..2085a12 100644 --- a/src/parser.re +++ b/src/parser.re @@ -29,6 +29,8 @@ #include #include +#include + #include "parser.h" /*!max:re2c*/ @@ -67,7 +69,7 @@ struct psi_parser_input *psi_parser_open_file(struct psi_parser *P, const char * return NULL; } - if (!(fb = malloc(sizeof(*fb) + strlen(filename) + 1 + sb.st_size + YYMAXFILL))) { + if (!(fb = malloc(sizeof(*fb) + sb.st_size + YYMAXFILL))) { if (report_errors) { P->error(PSI_DATA(P), NULL, PSI_WARNING, "Could not allocate %zu bytes for reading '%s': %s", @@ -97,10 +99,8 @@ struct psi_parser_input *psi_parser_open_file(struct psi_parser *P, const char * return NULL; } - memset(fb->buffer + sb.st_size, 0, YYMAXFILL); fb->length = sb.st_size; - fb->file = &fb->buffer[sb.st_size + YYMAXFILL]; - memcpy(fb->file, filename, strlen(filename) + 1); + fb->file = zend_string_init(filename, strlen(filename), 1); return fb; } @@ -109,7 +109,7 @@ struct psi_parser_input *psi_parser_open_string(struct psi_parser *P, const char { struct psi_parser_input *sb; - if (!(sb = malloc(sizeof(*sb) + sizeof("") + length + YYMAXFILL))) { + if (!(sb = malloc(sizeof(*sb) + length + YYMAXFILL))) { P->error(PSI_DATA(P), NULL, PSI_WARNING, "Could not allocate %zu bytes: %s", length + YYMAXFILL, strerror(errno)); @@ -120,68 +120,11 @@ struct psi_parser_input *psi_parser_open_string(struct psi_parser *P, const char memset(sb->buffer + length, 0, YYMAXFILL); sb->length = length; - sb->file = &sb->buffer[length + YYMAXFILL]; - memcpy(sb->file, "", sizeof("")); + sb->file = zend_string_init("", strlen(""), 1); return sb; } -#if 0 -static void psi_parser_register_constants(struct psi_parser *P) -{ - zend_string *key; - zval *val; - - ZEND_HASH_FOREACH_STR_KEY_VAL(&P->cpp.defs, key, val) - { - struct psi_impl_def_val *iv; - struct psi_const_type *ct; - struct psi_const *c; - const char *ctn; - token_t ctt; - impl_val tmp; - zend_string *str; - - ZVAL_DEREF(val); - switch (Z_TYPE_P(val)) { - case IS_TRUE: - case IS_FALSE: - ctt = PSI_T_BOOL; - ctn = "bool"; - tmp.zend.bval = Z_TYPE_P(val) == IS_TRUE; - break; - case IS_LONG: - ctt = PSI_T_INT; - ctn = "int"; - tmp.zend.lval = Z_LVAL_P(val); - break; - case IS_DOUBLE: - ctt = PSI_T_FLOAT; - ctn = "float"; - tmp.dval = Z_DVAL_P(val); - break; - default: - ctt = PSI_T_STRING; - ctn = "string"; - str = zval_get_string(val); - tmp.zend.str = zend_string_dup(str, 1); - zend_string_release(str); - break; - } - - iv = psi_impl_def_val_init(ctt, NULL); - iv->ival = tmp; - ct = psi_const_type_init(ctt, ctn); - c = psi_const_init(ct, key->val, iv); - if (!P->consts) { - P->consts = psi_plist_init((psi_plist_dtor) psi_const_free); - } - P->consts = psi_plist_add(P->consts, &c); - } - ZEND_HASH_FOREACH_END(); -} -#endif - struct psi_plist *psi_parser_preprocess(struct psi_parser *P, struct psi_plist **tokens) { if (psi_cpp_process(P->preproc, tokens)) { @@ -220,15 +163,20 @@ void psi_parser_postprocess(struct psi_parser *P) struct psi_impl_def_val *def; struct psi_const *cnst; struct psi_num_exp *num; - char *name_str = malloc(name->len + sizeof("psi\\")); + smart_str ns_name = {0}; + zend_string *name_str; + + smart_str_appendl_ex(&ns_name, ZEND_STRL("psi\\"), 1); + smart_str_append_ex(&ns_name, name, 1); + name_str = smart_str_extract(&ns_name); - strcat(strcpy(name_str, "psi\\"), name->val); num = psi_num_exp_copy(scope.macro->exp); def = psi_impl_def_val_init(PSI_T_NUMBER, num); - type = psi_impl_type_init(PSI_T_NUMBER, ""); + type = psi_impl_type_init(PSI_T_NUMBER, + zend_string_init(ZEND_STRL(""), 1)); cnst = psi_const_init(type, name_str, def); P->consts = psi_plist_add(P->consts, &cnst); - free(name_str); + zend_string_release(name_str); } } else { if (psi_plist_count(scope.macro->tokens) == 1) { @@ -239,14 +187,19 @@ void psi_parser_postprocess(struct psi_parser *P) struct psi_impl_type *type; struct psi_impl_def_val *def; struct psi_const *cnst; - char *name_str = malloc(name->len + sizeof("psi\\")); + smart_str ns_name = {0}; + zend_string *name_str; + + smart_str_appendl_ex(&ns_name, ZEND_STRL("psi\\"), 1); + smart_str_append_ex(&ns_name, name, 1); + name_str = smart_str_extract(&ns_name); - strcat(strcpy(name_str, "psi\\"), name->val); - type = psi_impl_type_init(PSI_T_STRING, "string"); + type = psi_impl_type_init(PSI_T_STRING, + zend_string_init(ZEND_STRL("string"), 1)); def = psi_impl_def_val_init(PSI_T_QUOTED_STRING, t->text); cnst = psi_const_init(type, name_str, def); P->consts = psi_plist_add(P->consts, &cnst); - free(name_str); + zend_string_release(name_str); } } } @@ -327,7 +280,7 @@ struct psi_plist *psi_parser_scan(struct psi_parser *P, struct psi_parser_input bool escaped; token_t char_width; - PSI_DEBUG_PRINT(P, "PSI: scanning %s\n", I->file); + PSI_DEBUG_PRINT(P, "PSI: scanning %s\n", I->file->val); tok = mrk = eol = cur = I->buffer; lim = I->buffer + I->length; @@ -586,7 +539,7 @@ struct psi_plist *psi_parser_scan(struct psi_parser *P, struct psi_parser_input error: ; P->error(PSI_DATA(P), token, PSI_WARNING, "PSI syntax error: unexpected input (%d) '%.*s' at col %tu", - token->type, token->size, token->text, tok - eol + 1); + token->type, token->text->len, token->text->val, tok - eol + 1); psi_plist_free(tokens); return NULL; diff --git a/src/parser_proc.c b/src/parser_proc.c index c9bb1e9..ff70d2b 100644 --- a/src/parser_proc.c +++ b/src/parser_proc.c @@ -152,7 +152,7 @@ static inline void psi_parser_proc_deanon_typedef(struct psi_decl_arg *def) return; } free(def->type->name); - def->type->name = strdup(def->var->name); + def->type->name = zend_string_copy(def->var->name); } static inline void psi_parser_proc_add_typedef(struct psi_parser *P, struct psi_decl_arg *def) { @@ -174,7 +174,7 @@ static inline void psi_parser_proc_add_const(struct psi_parser *P, struct psi_co static inline void psi_parser_proc_add_decl(struct psi_parser *P, struct psi_decl *decl) { assert(decl); - if (psi_decl_is_blacklisted(decl->func->var->name)) { + if (psi_decl_is_blacklisted(decl->func->var->name->val)) { psi_decl_free(&decl); return; } @@ -4301,7 +4301,7 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, case 285: #line 458 "src/parser_proc_grammar.y" /* glr.c:817 */ { - char *libname = strdup((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))->text); + char *libname = strdup((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))->text->val); P->file.libnames = psi_plist_add(P->file.libnames, &libname); } #line 4308 "src/parser_proc.c" /* glr.c:817 */ @@ -4335,7 +4335,7 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, #line 471 "src/parser_proc_grammar.y" /* glr.c:817 */ { if (P->flags & PSI_DEBUG) { - P->error(PSI_DATA(P), (*(struct psi_decl **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))->func->token, PSI_NOTICE, "Ignored decl: %s", (*(struct psi_decl **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))->func->var->name); + P->error(PSI_DATA(P), (*(struct psi_decl **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))->func->token, PSI_NOTICE, "Ignored decl: %s", (*(struct psi_decl **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))->func->var->name->val); } psi_decl_free(&(*(struct psi_decl **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))); } @@ -4420,7 +4420,7 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, while (psi_plist_get((*(struct psi_plist **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval)), index++, &next)) { struct psi_token *old = msg; msg = psi_token_cat(" ", 2, msg, next); - free(old); + psi_token_free(&old); } } psi_plist_free((*(struct psi_plist **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))); @@ -5087,7 +5087,7 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, { if ((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))) { (*(struct psi_token **)(&(*yyvalp))) = psi_token_cat(" ", 2, (*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval)), (*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))); - free((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))); + psi_token_free(&(*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))); } else { (*(struct psi_token **)(&(*yyvalp))) = psi_token_copy((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))); } @@ -5100,8 +5100,8 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, { if ((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))) { (*(struct psi_token **)(&(*yyvalp))) = psi_token_cat(" ", 2, (*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval)), (*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))); - free((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))); - free((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))); + psi_token_free(&(*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))); + psi_token_free(&(*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))); } else { (*(struct psi_token **)(&(*yyvalp))) = (*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval)); } @@ -5202,7 +5202,7 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, { if ((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))) { (*(struct psi_token **)(&(*yyvalp))) = psi_token_cat(" ", 2, (*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval)), (*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))); - free((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))); + psi_token_free(&(*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))); } else { (*(struct psi_token **)(&(*yyvalp))) = psi_token_copy((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))); } @@ -5215,8 +5215,8 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, { (*(struct psi_decl **)(&(*yyvalp))) = (*(struct psi_decl **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-2)].yystate.yysemantics.yysval)); if ((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))) { - (*(struct psi_decl **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-2)].yystate.yysemantics.yysval))->redir = strdup((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))->text); - free((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))); + (*(struct psi_decl **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-2)].yystate.yysemantics.yysval))->redir = zend_string_copy((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))->text); + psi_token_free(&(*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))); } } #line 5223 "src/parser_proc.c" /* glr.c:817 */ @@ -5227,8 +5227,8 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, { (*(struct psi_decl **)(&(*yyvalp))) = (*(struct psi_decl **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-2)].yystate.yysemantics.yysval)); if ((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))) { - (*(struct psi_decl **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-2)].yystate.yysemantics.yysval))->redir = strdup((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))->text); - free((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))); + (*(struct psi_decl **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-2)].yystate.yysemantics.yysval))->redir = zend_string_copy((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))->text); + psi_token_free(&(*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))); } } #line 5235 "src/parser_proc.c" /* glr.c:817 */ @@ -5262,7 +5262,7 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, #line 1096 "src/parser_proc_grammar.y" /* glr.c:817 */ { (*(struct psi_token **)(&(*yyvalp))) = psi_token_cat("", 2, (*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval)), (*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))); - free((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))); + psi_token_free(&(*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))); } #line 5268 "src/parser_proc.c" /* glr.c:817 */ break; @@ -5277,7 +5277,7 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, struct psi_decl_var *var; while (psi_plist_get((*(struct psi_plist **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval)), i++, &var)) { - if (psi_decl_extvar_is_blacklisted(var->name)) { + if (psi_decl_extvar_is_blacklisted(var->name->val)) { psi_decl_var_free(&var); } else { struct psi_decl_extvar *evar = psi_decl_extvar_init( @@ -5288,7 +5288,7 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, free((*(struct psi_plist **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))); } - if (psi_decl_extvar_is_blacklisted((*(struct psi_decl_arg **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-2)].yystate.yysemantics.yysval))->var->name)) { + if (psi_decl_extvar_is_blacklisted((*(struct psi_decl_arg **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-2)].yystate.yysemantics.yysval))->var->name->val)) { psi_decl_arg_free(&(*(struct psi_decl_arg **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-2)].yystate.yysemantics.yysval))); } else { struct psi_decl_extvar *evar = psi_decl_extvar_init((*(struct psi_decl_arg **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-2)].yystate.yysemantics.yysval))); @@ -5553,7 +5553,7 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, char digest[17]; struct psi_token *name; - psi_token_hash((*(struct psi_decl **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))->func ->token, digest); + psi_token_hash((*(struct psi_decl **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))->func->token, digest); name = psi_token_append("@", psi_token_copy((*(struct psi_decl **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))->func->token), 2, "funct", digest); (*(struct psi_decl_arg **)(&(*yyvalp))) = psi_decl_arg_init( @@ -5958,7 +5958,7 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, case 499: #line 1723 "src/parser_proc_grammar.y" /* glr.c:817 */ { - uint64_t len = (*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))->size + 1; + uint64_t len = (*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))->text->len + 1; (*(struct psi_number **)(&(*yyvalp))) = psi_number_init(PSI_T_UINT64, &len, 0); } #line 5965 "src/parser_proc.c" /* glr.c:817 */ @@ -6037,7 +6037,7 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, case 508: #line 1787 "src/parser_proc_grammar.y" /* glr.c:817 */ { - (*(struct psi_layout **)(&(*yyvalp))) = psi_layout_init(0, 0, psi_layout_init(0, atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))->text), NULL)); + (*(struct psi_layout **)(&(*yyvalp))) = psi_layout_init(0, 0, psi_layout_init(0, atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (0)].yystate.yysemantics.yysval))->text->val), NULL)); } #line 6043 "src/parser_proc.c" /* glr.c:817 */ break; @@ -6045,7 +6045,7 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, case 509: #line 1790 "src/parser_proc_grammar.y" /* glr.c:817 */ { - (*(struct psi_layout **)(&(*yyvalp))) = psi_layout_init(atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-3)].yystate.yysemantics.yysval))->text), atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))->text), NULL); + (*(struct psi_layout **)(&(*yyvalp))) = psi_layout_init(atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-3)].yystate.yysemantics.yysval))->text->val), atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))->text->val), NULL); } #line 6051 "src/parser_proc.c" /* glr.c:817 */ break; @@ -6053,7 +6053,7 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, case 510: #line 1793 "src/parser_proc_grammar.y" /* glr.c:817 */ { - (*(struct psi_layout **)(&(*yyvalp))) = psi_layout_init(atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-3)].yystate.yysemantics.yysval))->text), atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))->text), psi_layout_init(0, atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-7)].yystate.yysemantics.yysval))->text), NULL)); + (*(struct psi_layout **)(&(*yyvalp))) = psi_layout_init(atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-3)].yystate.yysemantics.yysval))->text->val), atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))->text->val), psi_layout_init(0, atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-7)].yystate.yysemantics.yysval))->text->val), NULL)); } #line 6059 "src/parser_proc.c" /* glr.c:817 */ break; @@ -6070,8 +6070,8 @@ yyuserAction (yyRuleNum yyn, size_t yyrhslen, yyGLRStackItem* yyvsp, case 512: #line 1803 "src/parser_proc_grammar.y" /* glr.c:817 */ { - (*(struct psi_layout*)(&(*yyvalp))).pos = atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-3)].yystate.yysemantics.yysval))->text); - (*(struct psi_layout*)(&(*yyvalp))).len = atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))->text); + (*(struct psi_layout*)(&(*yyvalp))).pos = atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-3)].yystate.yysemantics.yysval))->text->val); + (*(struct psi_layout*)(&(*yyvalp))).len = atol((*(struct psi_token **)(&((yyGLRStackItem const *)yyvsp)[YYFILL (-1)].yystate.yysemantics.yysval))->text->val); } #line 6077 "src/parser_proc.c" /* glr.c:817 */ break; diff --git a/src/parser_proc_grammar.y b/src/parser_proc_grammar.y index 6e07b5f..7101d0e 100644 --- a/src/parser_proc_grammar.y +++ b/src/parser_proc_grammar.y @@ -61,7 +61,7 @@ static inline void psi_parser_proc_deanon_typedef(struct psi_decl_arg *def) return; } free(def->type->name); - def->type->name = strdup(def->var->name); + def->type->name = zend_string_copy(def->var->name); } static inline void psi_parser_proc_add_typedef(struct psi_parser *P, struct psi_decl_arg *def) { @@ -83,7 +83,7 @@ static inline void psi_parser_proc_add_const(struct psi_parser *P, struct psi_co static inline void psi_parser_proc_add_decl(struct psi_parser *P, struct psi_decl *decl) { assert(decl); - if (psi_decl_is_blacklisted(decl->func->var->name)) { + if (psi_decl_is_blacklisted(decl->func->var->name->val)) { psi_decl_free(&decl); return; } @@ -456,7 +456,7 @@ block: } } | lib { - char *libname = strdup($lib->text); + char *libname = strdup($lib->text->val); P->file.libnames = psi_plist_add(P->file.libnames, &libname); } | constant { @@ -470,7 +470,7 @@ block: } | ignored_decl { if (P->flags & PSI_DEBUG) { - P->error(PSI_DATA(P), $ignored_decl->func->token, PSI_NOTICE, "Ignored decl: %s", $ignored_decl->func->var->name); + P->error(PSI_DATA(P), $ignored_decl->func->token, PSI_NOTICE, "Ignored decl: %s", $ignored_decl->func->var->name->val); } psi_decl_free(&$ignored_decl); } @@ -519,7 +519,7 @@ cpp_exp[exp]: while (psi_plist_get($tokens, index++, &next)) { struct psi_token *old = msg; msg = psi_token_cat(" ", 2, msg, next); - free(old); + psi_token_free(&old); } } psi_plist_free($tokens); @@ -986,7 +986,7 @@ decl_int_type[type]: | int_signed int_signed_types { if ($2) { $type = psi_token_cat(" ", 2, $1, $2); - free($2); + psi_token_free(&$2); } else { $type = psi_token_copy($1); } @@ -994,8 +994,8 @@ decl_int_type[type]: | int_width int_width_types { if ($2) { $type = psi_token_cat(" ", 2, $1, $2); - free($1); - free($2); + psi_token_free(&$1); + psi_token_free(&$2); } else { $type = $1; } @@ -1056,7 +1056,7 @@ int_width_types[type]: | int_signed int_signed_types { if ($2) { $type = psi_token_cat(" ", 2, $1, $2); - free($2); + psi_token_free(&$2); } else { $type = psi_token_copy($1); } @@ -1067,15 +1067,15 @@ decl_stmt: decl decl_asm EOS { $decl_stmt = $decl; if ($decl_asm) { - $decl->redir = strdup($decl_asm->text); - free($decl_asm); + $decl->redir = zend_string_copy($decl_asm->text); + psi_token_free(&$decl_asm); } } | CPP_EXTENSION decl decl_asm EOS { $decl_stmt = $decl; if ($decl_asm) { - $decl->redir = strdup($decl_asm->text); - free($decl_asm); + $decl->redir = zend_string_copy($decl_asm->text); + psi_token_free(&$decl_asm); } } ; @@ -1095,7 +1095,7 @@ quoted_strings[strings]: } | quoted_strings[strings_] QUOTED_STRING { $strings = psi_token_cat("", 2, $strings_, $QUOTED_STRING); - free($strings_); + psi_token_free(&$strings_); } ; @@ -1108,7 +1108,7 @@ decl_extvar_stmt[list]: struct psi_decl_var *var; while (psi_plist_get($vars, i++, &var)) { - if (psi_decl_extvar_is_blacklisted(var->name)) { + if (psi_decl_extvar_is_blacklisted(var->name->val)) { psi_decl_var_free(&var); } else { struct psi_decl_extvar *evar = psi_decl_extvar_init( @@ -1119,7 +1119,7 @@ decl_extvar_stmt[list]: free($vars); } - if (psi_decl_extvar_is_blacklisted($decl_arg->var->name)) { + if (psi_decl_extvar_is_blacklisted($decl_arg->var->name->val)) { psi_decl_arg_free(&$decl_arg); } else { struct psi_decl_extvar *evar = psi_decl_extvar_init($decl_arg); @@ -1422,7 +1422,7 @@ decl_anon_arg[arg]: char digest[17]; struct psi_token *name; - psi_token_hash($decl->func ->token, digest); + psi_token_hash($decl->func->token, digest); name = psi_token_append("@", psi_token_copy($decl->func->token), 2, "funct", digest); $arg = psi_decl_arg_init( @@ -1721,7 +1721,7 @@ sizeof_body_notypes[sizeof]: $sizeof = psi_number_init(PSI_T_INT8, &sizeof_a, 0); } | QUOTED_STRING { - uint64_t len = $QUOTED_STRING->size + 1; + uint64_t len = $QUOTED_STRING->text->len + 1; $sizeof = psi_number_init(PSI_T_UINT64, &len, 0); } ; @@ -1785,13 +1785,13 @@ decl_layout[l]: $l = NULL; } | COLON NUMBER[width] { - $l = psi_layout_init(0, 0, psi_layout_init(0, atol($width->text), NULL)); + $l = psi_layout_init(0, 0, psi_layout_init(0, atol($width->text->val), NULL)); } | COLON COLON LPAREN NUMBER[align] COMMA NUMBER[size] RPAREN { - $l = psi_layout_init(atol($align->text), atol($size->text), NULL); + $l = psi_layout_init(atol($align->text->val), atol($size->text->val), NULL); } | COLON NUMBER[width] COLON COLON LPAREN NUMBER[align] COMMA NUMBER[size] RPAREN { - $l = psi_layout_init(atol($align->text), atol($size->text), psi_layout_init(0, atol($width->text), NULL)); + $l = psi_layout_init(atol($align->text->val), atol($size->text->val), psi_layout_init(0, atol($width->text->val), NULL)); } ; @@ -1801,8 +1801,8 @@ align_and_size[as]: $as.len = 0; } | COLON COLON LPAREN NUMBER[align] COMMA NUMBER[size] RPAREN { - $as.pos = atol($align->text); - $as.len = atol($size->text); + $as.pos = atol($align->text->val); + $as.len = atol($size->text->val); } ; diff --git a/src/plist.c b/src/plist.c index 38cde5b..58f7898 100644 --- a/src/plist.c +++ b/src/plist.c @@ -42,6 +42,7 @@ struct psi_plist { memcpy(dest, src, list->size); \ } \ } while (0) +#define PLIST_CPY_R(list, dest, src, num) memcpy((dest), (src), (num) * (list)->size) /* !!! adjust list->count prior reduction */ #define PLIST_MOV_REDUCE(l, i) PLIST_MOV_REDUCE_EX(l, i, 1) #define PLIST_MOV_REDUCE_EX(l, i, n) memmove(PLIST_ELE(l, i), PLIST_ELE(l, (i) + (n)), (l)->size * ((l)->count - (i))) @@ -204,9 +205,7 @@ struct psi_plist *psi_plist_ins_r(struct psi_plist *list, size_t offset_start, s size_t e; PLIST_MOV_EXPAND_EX(list, offset_start, num_eles); - for (e = 0; e < num_eles; ++e) { - PLIST_CPY(list, PLIST_ELE(list, offset_start + e), &eles[e]); - } + PLIST_CPY_R(list, PLIST_ELE(list, offset_start), eles, num_eles); list->count = new_count; } } diff --git a/src/token.c b/src/token.c index 08a6802..95eaf7a 100644 --- a/src/token.c +++ b/src/token.c @@ -27,6 +27,8 @@ #include +#include + #include "token.h" #include "parser.h" @@ -35,21 +37,16 @@ size_t psi_token_alloc_size(size_t token_len, size_t fname_len) { } struct psi_token *psi_token_init(token_t token_typ, const char *token_txt, - size_t token_len, unsigned col, unsigned line, const char *file) + size_t token_len, unsigned col, unsigned line, zend_string *file) { struct psi_token *T; - size_t file_len = strlen(file); - T = calloc(1, psi_token_alloc_size(token_len, file_len)); + T = calloc(1, sizeof(*T)); T->type = token_typ; - T->size = token_len; - T->text = &T->buf[0]; - T->file = &T->buf[token_len + 1]; - T->line = line; T->col = col; - - memcpy(T->text, token_txt, token_len); - memcpy(T->file, file, file_len); + T->line = line; + T->file = zend_string_copy(file); + T->text = zend_string_init(token_txt, token_len, 1); return T; } @@ -59,18 +56,19 @@ void psi_token_free(struct psi_token **token_ptr) { struct psi_token *token = *token_ptr; *token_ptr = NULL; + zend_string_release(token->text); + zend_string_release(token->file); free(token); } } struct psi_token *psi_token_copy(struct psi_token *src) { - size_t strct_len = psi_token_alloc_size(src->size, strlen(src->file)); - struct psi_token *ptr = malloc(strct_len); + struct psi_token *ptr = malloc(sizeof(*ptr)); - memcpy(ptr, src, strct_len); + *ptr = *src; - ptr->text = &ptr->buf[0]; - ptr->file = &ptr->buf[ptr->size + 1]; + ptr->text = zend_string_copy(ptr->text); + ptr->file = zend_string_copy(ptr->file); return ptr; } @@ -79,90 +77,74 @@ void psi_token_copy_ctor(struct psi_token **tok) { *tok = psi_token_copy(*tok); } +/* concatenate `argc` number of tokens separated by `sep` into a newly allocated token */ struct psi_token *psi_token_cat(const char *sep, unsigned argc, ...) { va_list argv; unsigned i; size_t sep_len = sep ? strlen(sep) : 0; - struct psi_token *T = NULL; + struct psi_token *T = malloc(sizeof(*T)); + smart_str text = {0}; va_start(argv, argc); - for (i = 0; i < argc; ++i) { - struct psi_token *arg = va_arg(argv, struct psi_token *); - if (T) { - size_t token_len = T->size, fname_len = strlen(T->file); - struct psi_token *tmp = realloc(T, psi_token_alloc_size(T->size += arg->size + sep_len, fname_len)); + *T = *(struct psi_token *) va_arg(argv, struct psi_token *); + T->type = PSI_T_NAME; + T->file = zend_string_copy(T->file); - if (tmp) { - T = tmp; - } else { - free(T); - va_end(argv); - return NULL; - } + for (i = 0; i < argc; ++i) { + struct psi_token *arg = va_arg(argv, struct psi_token *); - T->text = &T->buf[0]; - T->file = &T->buf[T->size + 1]; - memmove(&T->buf[T->size + 1], &T->buf[token_len + 1], fname_len + 1); - memcpy(&T->buf[token_len], sep, sep_len); - memcpy(&T->buf[token_len + sep_len], arg->text, arg->size + 1); - } else { - T = psi_token_copy(arg); - T->type = PSI_T_NAME; + if (sep_len && text.a) { + smart_str_appendl_ex(&text, sep, sep_len, 1); } + + smart_str_append_ex(&text, arg->text, 1); } va_end(argv); + T->text = smart_str_extract(&text); + return T; } -struct psi_token *psi_token_prepend(const char *sep, struct psi_token *T, unsigned argc, ...) { +/* append `argc` number of C strings separated by `sep` to token `T` */ +struct psi_token *psi_token_append(const char *sep, struct psi_token *T, unsigned argc, ...) { va_list argv; unsigned i; size_t sep_len = sep ? strlen(sep) : 0; + smart_str text = {0}; + + smart_str_append_ex(&text, T->text, 1); va_start(argv, argc); for (i = 0; i < argc; ++i) { char *str = va_arg(argv, char *); - size_t str_len = strlen(str), token_len = T->size, fname_len = strlen(T->file); - - T = realloc(T, psi_token_alloc_size(T->size += str_len + sep_len, fname_len)); - T->text = &T->buf[0]; - T->file = &T->buf[T->size + 1]; - memmove(&T->buf[str_len + sep_len], &T->buf[0], token_len + 1 + fname_len + 1); - memcpy(&T->buf[0], str, str_len); - memcpy(&T->buf[str_len], sep, sep_len); - T->buf[T->size] = '\0'; - } - va_end(argv); + size_t str_len = strlen(str); - return T; -} -struct psi_token *psi_token_append(const char *sep, struct psi_token *T, unsigned argc, ...) { - va_list argv; - unsigned i; - size_t sep_len = sep ? strlen(sep) : 0; + if (sep_len && text.a) { + smart_str_appendl_ex(&text, sep, sep_len, 1); + } - va_start(argv, argc); - for (i = 0; i < argc; ++i) { - char *str = va_arg(argv, char *); - size_t str_len = strlen(str), token_len = T->size, fname_len = strlen(T->file); - - T = realloc(T, psi_token_alloc_size(T->size += str_len + sep_len, fname_len)); - T->text = &T->buf[0]; - T->file = &T->buf[T->size + 1]; - memmove(&T->buf[T->size + 1], &T->buf[token_len + 1], fname_len + 1); - memcpy(&T->buf[token_len], sep, sep_len); - memcpy(&T->buf[token_len + sep_len], str, str_len + 1); + smart_str_appendl_ex(&text, str, str_len, 1); } va_end(argv); + zend_string_release(T->text); + T->text = smart_str_extract(&text); + return T; } char *php_strtr(char *str, size_t len, char *str_from, char *str_to, size_t trlen); struct psi_token *psi_token_translit(struct psi_token *T, char *from, char *to) { - php_strtr(T->text, T->size, from, to, MIN(strlen(from), strlen(to))); + zend_string *tmp = zend_string_init(T->text->val, T->text->len, 1); + + zend_string_release(T->text); + T->text = tmp; + + php_strtr(T->text->val, T->text->len, from, to, MIN(strlen(from), strlen(to))); + zend_string_forget_hash_val(T->text); + return T; } @@ -191,8 +173,8 @@ static inline uint64_t psi_hash(char *digest_buf, ...) uint64_t psi_token_hash(struct psi_token *t, char *digest_buf) { char loc_buf[48]; - sprintf(loc_buf, "%u%u", t->line, t->col); - return psi_hash(digest_buf, t->file, loc_buf, (char *) NULL); + sprintf(digest_buf, "%u%u", t->line, t->col); + return psi_hash(digest_buf, t->file->val, loc_buf, (char *) NULL); } void psi_token_dump(int fd, struct psi_token *t) @@ -204,8 +186,8 @@ void psi_token_dump(int fd, struct psi_token *t) dprintf(fd, "EOF"); } else { dprintf(fd, "\""); - for (i = 0; i < t->size; ++i) { - switch (t->text[i]) { + for (i = 0; i < t->text->len; ++i) { + switch (t->text->val[i]) { case '\0': dprintf(fd, "\\0"); break; @@ -234,15 +216,15 @@ void psi_token_dump(int fd, struct psi_token *t) dprintf(fd, "\\\""); break; default: - if (isprint(t->text[i])) { - dprintf(fd, "%c", t->text[i]); + if (isprint(t->text->val[i])) { + dprintf(fd, "%c", t->text->val[i]); } else { - dprintf(fd, "\\x%02hhX", t->text[i]); + dprintf(fd, "\\x%02hhX", t->text->val[i]); } break; } } dprintf(fd, "\""); } - dprintf(fd, " at col %u in %s on line %u\n", t->col, t->file, t->line); + dprintf(fd, " at col %u in %s on line %u\n", t->col, t->file->val, t->line); } diff --git a/src/token.h b/src/token.h index 5f6c7c7..368fba9 100644 --- a/src/token.h +++ b/src/token.h @@ -176,20 +176,20 @@ static inline const char *psi_t_indirection(unsigned pointer_level) { struct psi_token { token_t type; - unsigned size, line, col, flags; - char *text, *file; - char buf[1]; + unsigned line; + unsigned col; + unsigned flags; + zend_string *file; + zend_string *text; }; struct psi_parser; struct psi_token *psi_token_init(token_t token_typ, const char *token_txt, - size_t token_len, unsigned col, unsigned line, const char *file); -size_t psi_token_alloc_size(size_t token_len, size_t fname_len); + size_t token_len, unsigned col, unsigned line, zend_string *file); struct psi_token *psi_token_copy(struct psi_token *src); void psi_token_copy_ctor(struct psi_token **src); struct psi_token *psi_token_cat(const char *sep, unsigned argc, ...); -struct psi_token *psi_token_prepend(const char *sep, struct psi_token *T, unsigned argc, ...); struct psi_token *psi_token_append(const char *sep, struct psi_token *T, unsigned argc, ...); struct psi_token *psi_token_translit(struct psi_token *T, char *from, char *to); uint64_t psi_token_hash(struct psi_token *t, char *digest_buf); diff --git a/src/types.h b/src/types.h index 220793c..df267a0 100644 --- a/src/types.h +++ b/src/types.h @@ -73,7 +73,7 @@ static inline impl_val *deref_impl_val(impl_val *ret_val, struct psi_decl_var *v ZEND_ASSERT(!var->arg || var->arg->var != var); #if 0 fprintf(stderr, "deref: %s pl=%u:%u as=%u:%u %p\n", - var->name, var->pointer_level, var->arg->var->pointer_level, + var->name->val, var->pointer_level, var->arg->var->pointer_level, var->array_size, var->arg->var->array_size, ret_val); #endif for (i = 0; i < var->pointer_level; ++i) { @@ -92,7 +92,7 @@ static inline impl_val *enref_impl_val(void *ptr, struct psi_decl_var *var) { ZEND_ASSERT(var->arg->var == var); #if 0 fprintf(stderr, "enref: %s pl=%u:%u as=%u:%u\n", - var->name, var->pointer_level, var->arg->var->pointer_level, + var->name->val, var->pointer_level, var->arg->var->pointer_level, var->array_size, var->arg->var->array_size); #endif if (!var->pointer_level ){//&& real_decl_type(var->arg->type)->type != PSI_T_STRUCT) { @@ -115,7 +115,7 @@ static inline impl_val *enref_impl_val(void *ptr, struct psi_decl_var *var) { static inline impl_val *struct_member_ref(struct psi_decl_arg *set_arg, impl_val *struct_ptr, impl_val **to_free) { void *ptr = (char *) struct_ptr + set_arg->layout->pos; #if 0 - fprintf(stderr, "struct member %s: %p\n", set_arg->var->name, ptr); + fprintf(stderr, "struct member %s: %p\n", set_arg->var->name->val, ptr); #endif return ptr; } diff --git a/src/types/assert_stmt.c b/src/types/assert_stmt.c index 7b860d9..9ded0ee 100644 --- a/src/types/assert_stmt.c +++ b/src/types/assert_stmt.c @@ -43,12 +43,10 @@ void psi_assert_stmt_free(struct psi_assert_stmt **stmt_ptr) if (*stmt_ptr) { struct psi_assert_stmt *stmt = *stmt_ptr; + *stmt_ptr = NULL; psi_num_exp_free(&stmt->exp); - if (stmt->token) { - free(stmt->token); - } + psi_token_free(&stmt->token); free(stmt); - *stmt_ptr = NULL; } } diff --git a/src/types/const.c b/src/types/const.c index 18e9da6..1a06e43 100644 --- a/src/types/const.c +++ b/src/types/const.c @@ -26,12 +26,17 @@ #include "php_psi_stdinc.h" #include "data.h" -struct psi_const *psi_const_init(struct psi_impl_type *type, const char *name, +struct psi_const *psi_const_init(struct psi_impl_type *type, zend_string *name, struct psi_impl_def_val *val) { struct psi_const *c = calloc(1, sizeof(*c)); + + if (name->val[0] == '\\') { + c->name = zend_string_init(&name->val[1], name->len-1, 1); + } else { + c->name = zend_string_copy(name); + } c->type = type; - c->name = strdup(name[0] == '\\' ? &name[1] : name); c->val = val; return c; } @@ -42,11 +47,9 @@ void psi_const_free(struct psi_const **constant_ptr) struct psi_const *constant = *constant_ptr; *constant_ptr = NULL; - if (constant->token) { - free(constant->token); - } + psi_token_free(&constant->token); psi_impl_type_free(&constant->type); - free(constant->name); + zend_string_release(constant->name); psi_impl_def_val_free(&constant->val); free(constant); } @@ -56,7 +59,7 @@ void psi_const_dump(int fd, struct psi_const *cnst) { dprintf(fd, "const "); psi_impl_type_dump(fd, cnst->type); - dprintf(fd, " %s = ", cnst->name); + dprintf(fd, " %s = ", cnst->name->val); psi_impl_def_val_dump(fd, cnst->val); dprintf(fd, ";"); } diff --git a/src/types/const.h b/src/types/const.h index a75caf2..4dbb171 100644 --- a/src/types/const.h +++ b/src/types/const.h @@ -34,11 +34,11 @@ struct psi_impl_def_val; struct psi_const { struct psi_token *token; struct psi_impl_type *type; - char *name; + zend_string *name; struct psi_impl_def_val *val; }; -struct psi_const *psi_const_init(struct psi_impl_type *type, const char *name, +struct psi_const *psi_const_init(struct psi_impl_type *type, zend_string *name, struct psi_impl_def_val *val); void psi_const_free(struct psi_const **constant_ptr); void psi_const_dump(int fd, struct psi_const *cnst); diff --git a/src/types/cpp_exp.c b/src/types/cpp_exp.c index 6e01843..2df4854 100644 --- a/src/types/cpp_exp.c +++ b/src/types/cpp_exp.c @@ -83,8 +83,7 @@ void psi_cpp_exp_free(struct psi_cpp_exp **exp_ptr) case PSI_T_IMPORT: case PSI_T_INCLUDE: case PSI_T_INCLUDE_NEXT: - free(exp->data.tok); - exp->data.tok = NULL; + psi_token_free(&exp->data.tok); break; case PSI_T_DEFINE: psi_cpp_macro_decl_free(&exp->data.decl); @@ -101,16 +100,14 @@ void psi_cpp_exp_free(struct psi_cpp_exp **exp_ptr) assert(0); break; } - if (exp->token) { - free(exp->token); - } + psi_token_free(&exp->token); free(exp); } } void psi_cpp_exp_dump(int fd, struct psi_cpp_exp *exp) { - dprintf(fd, "#%s ", exp->token->text); + dprintf(fd, "#%s ", exp->token->text->val); switch (exp->type) { case PSI_T_WARNING: case PSI_T_ERROR: @@ -121,15 +118,15 @@ void psi_cpp_exp_dump(int fd, struct psi_cpp_exp *exp) case PSI_T_UNDEF: case PSI_T_IFDEF: case PSI_T_IFNDEF: - dprintf(fd, "%s", exp->data.tok->text); + dprintf(fd, "%s", exp->data.tok->text->val); break; case PSI_T_IMPORT: case PSI_T_INCLUDE: case PSI_T_INCLUDE_NEXT: if (exp->data.tok->type == PSI_T_CPP_HEADER) { - dprintf(fd, "<%s>", exp->data.tok->text); + dprintf(fd, "<%s>", exp->data.tok->text->val); } else { - dprintf(fd, "\"%s\"", exp->data.tok->text); + dprintf(fd, "\"%s\"", exp->data.tok->text->val); } break; case PSI_T_DEFINE: @@ -188,7 +185,7 @@ static inline void psi_cpp_level_unmask(struct psi_cpp *cpp) void psi_cpp_exp_exec(struct psi_cpp_exp *exp, struct psi_cpp *cpp, struct psi_data *D) { PSI_DEBUG_PRINT(D, "PSI: CPP EVAL < %s (level=%u, skip=%u)\n", - exp->token->text, cpp->level, cpp->skip); + exp->token->text->val, cpp->level, cpp->skip); #if PSI_CPP_DEBUG psi_cpp_exp_dump(2, exp); @@ -198,13 +195,13 @@ void psi_cpp_exp_exec(struct psi_cpp_exp *exp, struct psi_cpp *cpp, struct psi_d case PSI_T_ERROR: if (!cpp->skip) { D->error(D, exp->token, PSI_ERROR, "%s", - exp->data.tok ? exp->data.tok->text : ""); + exp->data.tok ? exp->data.tok->text->val : ""); } break; case PSI_T_WARNING: if (!cpp->skip) { D->error(D, exp->token, PSI_WARNING, "%s", - exp->data.tok ? exp->data.tok->text : ""); + exp->data.tok ? exp->data.tok->text->val : ""); } break; case PSI_T_UNDEF: @@ -303,27 +300,27 @@ void psi_cpp_exp_exec(struct psi_cpp_exp *exp, struct psi_cpp *cpp, struct psi_d case PSI_T_INCLUDE: if (!cpp->skip) { if (!psi_cpp_include(cpp, exp->data.tok, PSI_CPP_INCLUDE)) { - D->error(D, exp->token, PSI_WARNING, "Failed to include %s", exp->data.tok->text); + D->error(D, exp->token, PSI_WARNING, "Failed to include %s", exp->data.tok->text->val); } } break; case PSI_T_INCLUDE_NEXT: if (!cpp->skip) { if (!psi_cpp_include(cpp, exp->data.tok, PSI_CPP_INCLUDE_NEXT)) { - D->error(D, exp->token, PSI_WARNING, "Failed to include %s", exp->data.tok->text); + D->error(D, exp->token, PSI_WARNING, "Failed to include %s", exp->data.tok->text->val); } } break; case PSI_T_IMPORT: if (!cpp->skip) { if (!psi_cpp_include(cpp, exp->data.tok, PSI_CPP_INCLUDE_ONCE)) { - D->error(D, exp->token, PSI_WARNING, "Failed to include %s", exp->data.tok->text); + D->error(D, exp->token, PSI_WARNING, "Failed to include %s", exp->data.tok->text->val); } } break; case PSI_T_PRAGMA_ONCE: if (!cpp->skip) { - zend_hash_str_add_empty_element(&cpp->once, exp->token->file, strlen(exp->token->file)); + zend_hash_add_empty_element(&cpp->once, exp->token->file); } break; default: @@ -332,5 +329,5 @@ void psi_cpp_exp_exec(struct psi_cpp_exp *exp, struct psi_cpp *cpp, struct psi_d } PSI_DEBUG_PRINT(D, "PSI: CPP EVAL > %s (level=%u, skip=%u)\n", - exp->token->text, cpp->level, cpp->skip); + exp->token->text->val, cpp->level, cpp->skip); } diff --git a/src/types/cpp_macro_call.c b/src/types/cpp_macro_call.c index 4bd3932..8d7dc23 100644 --- a/src/types/cpp_macro_call.c +++ b/src/types/cpp_macro_call.c @@ -28,11 +28,11 @@ #include "cpp.h" #include "data.h" -struct psi_cpp_macro_call *psi_cpp_macro_call_init(const char *name, +struct psi_cpp_macro_call *psi_cpp_macro_call_init(zend_string *name, struct psi_plist *args) { struct psi_cpp_macro_call *call = calloc(1, sizeof(*call)); - call->name = strdup(name); + call->name = zend_string_copy(name); call->args = args; return call; } @@ -41,7 +41,7 @@ struct psi_cpp_macro_call *psi_cpp_macro_call_copy( struct psi_cpp_macro_call *call) { struct psi_cpp_macro_call *copy = calloc(1, sizeof(*copy)); - copy->name = strdup(call->name); + copy->name = zend_string_copy(call->name); if (call->token) { copy->token = psi_token_copy(call->token); } @@ -59,13 +59,11 @@ void psi_cpp_macro_call_free(struct psi_cpp_macro_call **call_ptr) *call_ptr = NULL; - free(call->name); + zend_string_release(call->name); if (call->args) { psi_plist_free(call->args); } - if (call->token) { - free(call->token); - } + psi_token_free(&call->token); free(call); } } @@ -75,7 +73,7 @@ void psi_cpp_macro_call_dump(int fd, struct psi_cpp_macro_call *call) size_t i = 0; struct psi_num_exp *num; - dprintf(fd, "%s(", call->name); + dprintf(fd, "%s(", call->name->val); while (psi_plist_get(call->args, i++, &num)) { if (i > 1) { dprintf(fd, ", "); diff --git a/src/types/cpp_macro_call.h b/src/types/cpp_macro_call.h index 7ca283d..63e6414 100644 --- a/src/types/cpp_macro_call.h +++ b/src/types/cpp_macro_call.h @@ -31,11 +31,11 @@ struct psi_plist; struct psi_cpp_macro_call { struct psi_token *token; - char *name; + zend_string *name; struct psi_plist *args; }; -struct psi_cpp_macro_call *psi_cpp_macro_call_init(const char *name, +struct psi_cpp_macro_call *psi_cpp_macro_call_init(zend_string *name, struct psi_plist *args); struct psi_cpp_macro_call *psi_cpp_macro_call_copy( struct psi_cpp_macro_call *call); diff --git a/src/types/cpp_macro_decl.c b/src/types/cpp_macro_decl.c index 6d3747e..c26b708 100644 --- a/src/types/cpp_macro_decl.c +++ b/src/types/cpp_macro_decl.c @@ -44,9 +44,7 @@ void psi_cpp_macro_decl_free(struct psi_cpp_macro_decl **macro_ptr) struct psi_cpp_macro_decl *macro = *macro_ptr; *macro_ptr = NULL; - if (macro->token) { - free(macro->token); - } + psi_token_free(¯o->token); if (macro->exp) { psi_num_exp_free(¯o->exp); } @@ -62,7 +60,7 @@ void psi_cpp_macro_decl_free(struct psi_cpp_macro_decl **macro_ptr) void psi_cpp_macro_decl_dump(int fd, struct psi_cpp_macro_decl *macro) { - dprintf(fd, "%s", macro->token->text); + dprintf(fd, "%s", macro->token->text->val); if (macro->sig) { size_t i = 0; @@ -70,7 +68,7 @@ void psi_cpp_macro_decl_dump(int fd, struct psi_cpp_macro_decl *macro) dprintf(fd, "("); while (psi_plist_get(macro->sig, i++, &tok)) { - dprintf(fd, "%s%s", i>1?",":"", tok->text); + dprintf(fd, "%s%s", i>1?",":"", tok->text->val); } dprintf(fd, ")"); } @@ -78,22 +76,23 @@ void psi_cpp_macro_decl_dump(int fd, struct psi_cpp_macro_decl *macro) if (macro->exp) { dprintf(fd, " "); psi_num_exp_dump(fd, macro->exp); - if (!macro->tokens) abort(); - } else - if (macro->tokens) { + + assert(macro->tokens); + + } else if (macro->tokens) { size_t i = 0; struct psi_token *tok; while (psi_plist_get(macro->tokens, i++, &tok)) { switch (tok->type) { case PSI_T_QUOTED_STRING: - dprintf(fd, " \"%s\"", tok->text); + dprintf(fd, " \"%s\"", tok->text->val); break; case PSI_T_QUOTED_CHAR: - dprintf(fd, " '%s'", tok->text); + dprintf(fd, " '%s'", tok->text->val); break; default: - dprintf(fd, " %s", tok->text); + dprintf(fd, " %s", tok->text->val); } } } @@ -113,7 +112,7 @@ static inline bool cmp_token_list(struct psi_plist *l1, struct psi_plist *l2) psi_plist_get(l1, i, &t1); psi_plist_get(l2, i, &t2); - if (strcmp(t1->text, t2->text)) { + if (!zend_string_equals(t1->text, t2->text)) { return false; } } diff --git a/src/types/decl.c b/src/types/decl.c index ed66d40..56ba81d 100644 --- a/src/types/decl.c +++ b/src/types/decl.c @@ -24,12 +24,13 @@ *******************************************************************************/ #include "php_psi_stdinc.h" - #include "php_psi.h" #include #include +#include + #include "data.h" #define PSI_FUNC_REDIRS @@ -58,7 +59,7 @@ void psi_decl_free(struct psi_decl **d_ptr) psi_plist_free(d->args); } if (d->redir) { - free(d->redir); + zend_string_release(d->redir); } free(d); } @@ -91,7 +92,7 @@ void psi_decl_dump(int fd, struct psi_decl *decl) dprintf(fd, ")[%u]", decl->func->var->array_size); } if (decl->redir) { - dprintf(fd, ") __asm__ (\"%s\");", decl->redir); + dprintf(fd, ") __asm__ (\"%s\");", decl->redir->val); } else { dprintf(fd, ");"); } @@ -108,7 +109,7 @@ static inline bool psi_decl_validate_func(struct psi_data *data, } for (redir = &psi_func_redirs[0]; redir->name; ++redir) { - if (!strcmp(func->var->name, redir->name)) { + if (!strcmp(func->var->name->val, redir->name)) { decl->sym = redir->func; } } @@ -117,7 +118,7 @@ static inline bool psi_decl_validate_func(struct psi_data *data, void *dl; while (!decl->sym && psi_plist_get(data->file.dlopened, i++, &dl)) { - decl->sym = dlsym(dl, decl->redir ?: func->var->name); + decl->sym = dlsym(dl, decl->redir ? decl->redir->val : func->var->name->val); } } if (!decl->sym) { @@ -127,11 +128,12 @@ static inline bool psi_decl_validate_func(struct psi_data *data, #ifndef RTLD_DEFAULT # define RTLD_DEFAULT ((void *) 0) #endif - decl->sym = dlsym(RTLD_DEFAULT, decl->redir ?: func->var->name); + decl->sym = dlsym(RTLD_DEFAULT, decl->redir ? decl->redir->val : func->var->name->val); if (!decl->sym) { data->error(data, func->token, PSI_WARNING, "Failed to locate symbol '%s(%s)': %s", - func->var->name, decl->redir ?: "", + func->var->name->val, + decl->redir ? decl->redir->val : "", dlerror() ?: "not found"); return false; } @@ -156,10 +158,10 @@ bool psi_decl_validate_nodl(struct psi_data *data, struct psi_decl *decl, struct psi_validate_scope *scope) { if (!decl->abi) { - decl->abi = psi_decl_abi_init("default"); + decl->abi = psi_decl_abi_init(zend_string_init(ZEND_STRL("default"), 1)); } else if (!psi_decl_abi_validate(data, decl->abi)) { data->error(data, decl->abi->token, PSI_WARNING, - "Invalid calling convention: '%s'", decl->abi->token->text); + "Invalid calling convention: '%s'", decl->abi->token->text->val); return false; } if (!psi_decl_arg_validate(data, decl->func, scope)) { @@ -171,9 +173,13 @@ bool psi_decl_validate_nodl(struct psi_data *data, struct psi_decl *decl, while (psi_plist_get(decl->args, i++, &arg)) { if (!arg->var->name) { - arg->var->name = malloc(7); - snprintf(arg->var->name, 6, "arg%zu", i); - arg->var->fqn = strdup(arg->var->name); + smart_str name = {0}; + + smart_str_appendl_ex(&name, ZEND_STRL("arg"), 1); + smart_str_append_unsigned_ex(&name, i, 1); + + arg->var->name = smart_str_extract(&name); + arg->var->fqn = zend_string_copy(arg->var->name); } if (!psi_decl_arg_validate(data, arg, scope)) { return false; diff --git a/src/types/decl.h b/src/types/decl.h index eb80802..11e72a8 100644 --- a/src/types/decl.h +++ b/src/types/decl.h @@ -35,7 +35,7 @@ struct psi_decl { struct psi_decl_abi *abi; struct psi_decl_arg *func; struct psi_plist *args; - char *redir; + zend_string *redir; void *sym; void *info; unsigned varargs:1; diff --git a/src/types/decl_abi.c b/src/types/decl_abi.c index 60ad161..75de9da 100644 --- a/src/types/decl_abi.c +++ b/src/types/decl_abi.c @@ -26,10 +26,10 @@ #include "php_psi_stdinc.h" #include "data.h" -struct psi_decl_abi *psi_decl_abi_init(const char *convention) +struct psi_decl_abi *psi_decl_abi_init(zend_string *convention) { struct psi_decl_abi *abi = calloc(1, sizeof(*abi)); - abi->convention = strdup(convention); + abi->convention = zend_string_copy(convention); return abi; } @@ -39,17 +39,15 @@ void psi_decl_abi_free(struct psi_decl_abi **abi_ptr) struct psi_decl_abi *abi = *abi_ptr; *abi_ptr = NULL; - if (abi->token) { - free(abi->token); - } - free(abi->convention); + psi_token_free(&abi->token); + zend_string_release(abi->convention); free(abi); } } void psi_decl_abi_dump(int fd, struct psi_decl_abi *abi) { - dprintf(fd, "%s", abi->convention); + dprintf(fd, "%s", abi->convention->val); } static const char * const abi_ccs[] = { @@ -65,7 +63,7 @@ bool psi_decl_abi_validate(struct psi_data *data, struct psi_decl_abi *abi) size_t i; for (i = 0; i < sizeof(abi_ccs) / sizeof(char *); ++i) { - if (strcasecmp(abi->convention, abi_ccs[i])) { + if (!strcasecmp(abi->convention->val, abi_ccs[i])) { return true; } } diff --git a/src/types/decl_abi.h b/src/types/decl_abi.h index d601792..331ee92 100644 --- a/src/types/decl_abi.h +++ b/src/types/decl_abi.h @@ -31,10 +31,10 @@ struct psi_data; struct psi_decl_abi { struct psi_token *token; - char *convention; + zend_string *convention; }; -struct psi_decl_abi *psi_decl_abi_init(const char *convention); +struct psi_decl_abi *psi_decl_abi_init(zend_string *convention); void psi_decl_abi_free(struct psi_decl_abi **abi_ptr); void psi_decl_abi_dump(int fd, struct psi_decl_abi *abi); bool psi_decl_abi_validate(struct psi_data *data, struct psi_decl_abi *abi); diff --git a/src/types/decl_arg.c b/src/types/decl_arg.c index 1d143ee..f9ed347 100644 --- a/src/types/decl_arg.c +++ b/src/types/decl_arg.c @@ -46,7 +46,7 @@ void psi_decl_arg_free(struct psi_decl_arg **arg_ptr) *arg_ptr = NULL; if (arg->token && arg->token != arg->var->token) { - free(arg->token); + psi_token_free(&arg->token); } psi_decl_type_free(&arg->type); psi_decl_var_free(&arg->var); @@ -66,7 +66,7 @@ void psi_decl_arg_dump(int fd, struct psi_decl_arg *arg, unsigned level) } dprintf(fd, " %s(*%s)", psi_t_indirection(arg->var->pointer_level - !! arg->var->array_size), - arg->var->name); + arg->var->name->val); dprintf(fd, "("); if (arg->type->real.func->args) { size_t j = 0; @@ -139,7 +139,7 @@ bool psi_decl_arg_validate_typedef(struct psi_data *data, def->type->type = PSI_T_POINTER; } else { data->error(data, def->token, PSI_WARNING, - "Type '%s' cannot be aliased to 'void'", def->type->name); + "Type '%s' cannot be aliased to 'void'", def->type->name->val); return false; } } else if (!psi_decl_type_validate(data, def->type, def, scope)) { @@ -161,7 +161,7 @@ bool psi_decl_arg_validate_typedef(struct psi_data *data, } data->error(data, def->token, PSI_WARNING, "Type '%s' cannot be aliased to '%s%s'%s%s", def->var->name, pre, - def->type->name, *data->last_error ? ": " : "", data->last_error); + def->type->name->val, *data->last_error ? ": " : "", data->last_error); return false; } @@ -238,14 +238,14 @@ size_t psi_decl_arg_get_size(struct psi_decl_arg *darg) } struct psi_decl_arg *psi_decl_arg_get_by_name(struct psi_plist *args, - const char *name) + zend_string *name) { size_t i = 0; struct psi_decl_arg *arg; if (args) while (psi_plist_get(args, i++, &arg)) { - if (!strcmp(name, arg->var->name)) { + if (zend_string_equals(name, arg->var->name)) { return arg; } } @@ -264,7 +264,7 @@ struct psi_decl_arg *psi_decl_arg_get_by_var(struct psi_decl_var *var, return var->arg = arg; } - if (func && !strcmp(var->name, func->var->name)) { + if (func && !zend_string_equals(var->name, func->var->name)) { return var->arg = func; } diff --git a/src/types/decl_arg.h b/src/types/decl_arg.h index b915893..325d7c7 100644 --- a/src/types/decl_arg.h +++ b/src/types/decl_arg.h @@ -54,7 +54,7 @@ size_t psi_decl_arg_align(struct psi_decl_arg *darg, size_t *pos, size_t *len); size_t psi_decl_arg_get_align(struct psi_decl_arg *darg); size_t psi_decl_arg_get_size(struct psi_decl_arg *darg); -struct psi_decl_arg *psi_decl_arg_get_by_name(struct psi_plist *args, const char *name); +struct psi_decl_arg *psi_decl_arg_get_by_name(struct psi_plist *args, zend_string *name); struct psi_decl_arg *psi_decl_arg_get_by_var(struct psi_decl_var *var, struct psi_plist *args, struct psi_decl_arg *func); diff --git a/src/types/decl_enum.c b/src/types/decl_enum.c index 026c375..f060bf3 100644 --- a/src/types/decl_enum.c +++ b/src/types/decl_enum.c @@ -26,10 +26,10 @@ #include "php_psi_stdinc.h" #include "data.h" -struct psi_decl_enum *psi_decl_enum_init(const char *name, struct psi_plist *l) +struct psi_decl_enum *psi_decl_enum_init(zend_string *name, struct psi_plist *l) { struct psi_decl_enum *e = calloc(1, sizeof(*e)); - e->name = strdup(name); + e->name = zend_string_copy(name); e->items = l; return e; } @@ -40,20 +40,18 @@ void psi_decl_enum_free(struct psi_decl_enum **e_ptr) struct psi_decl_enum *e = *e_ptr; *e_ptr = NULL; - if (e->token) { - free(e->token); - } + psi_token_free(&e->token); if (e->items) { psi_plist_free(e->items); } - free(e->name); + zend_string_release(e->name); free(e); } } void psi_decl_enum_dump(int fd, struct psi_decl_enum *e, unsigned level) { - dprintf(fd, "enum %s {\n", e->name); + dprintf(fd, "enum %s {\n", e->name->val); if (e->items) { size_t i = 0; struct psi_decl_enum_item *item; @@ -75,7 +73,7 @@ bool psi_decl_enum_validate(struct psi_data *data, struct psi_decl_enum *e) struct psi_decl_enum_item *i, *p = NULL; if (!psi_plist_count(e->items)) { - data->error(data, e->token, PSI_WARNING, "Empty enum '%s'", e->name); + data->error(data, e->token, PSI_WARNING, "Empty enum '%s'", e->name->val); return false; } diff --git a/src/types/decl_enum.h b/src/types/decl_enum.h index 8082f3c..fd51d71 100644 --- a/src/types/decl_enum.h +++ b/src/types/decl_enum.h @@ -32,11 +32,11 @@ struct psi_plist; struct psi_decl_enum { struct psi_token *token; - char *name; + zend_string *name; struct psi_plist *items; }; -struct psi_decl_enum *psi_decl_enum_init(const char *name, struct psi_plist *l); +struct psi_decl_enum *psi_decl_enum_init(zend_string *name, struct psi_plist *l); void psi_decl_enum_free(struct psi_decl_enum **e_ptr); void psi_decl_enum_dump(int fd, struct psi_decl_enum *e, unsigned level); bool psi_decl_enum_validate(struct psi_data *data, struct psi_decl_enum *e); diff --git a/src/types/decl_enum_item.c b/src/types/decl_enum_item.c index 7410e01..7c49142 100644 --- a/src/types/decl_enum_item.c +++ b/src/types/decl_enum_item.c @@ -26,11 +26,11 @@ #include "php_psi_stdinc.h" #include "data.h" -struct psi_decl_enum_item *psi_decl_enum_item_init(const char *name, +struct psi_decl_enum_item *psi_decl_enum_item_init(zend_string *name, struct psi_num_exp *num) { struct psi_decl_enum_item *i = calloc(1, sizeof(*i)); - i->name = strdup(name); + i->name = zend_string_copy(name); i->num = num; return i; } @@ -41,9 +41,7 @@ void psi_decl_enum_item_free(struct psi_decl_enum_item **i_ptr) struct psi_decl_enum_item *i = *i_ptr; *i_ptr = NULL; - if (i->token) { - free(i->token); - } + psi_token_free(&i->token); if (i->num) { if (i->num == &i->inc) { switch (i->inc.op) { @@ -61,14 +59,14 @@ void psi_decl_enum_item_free(struct psi_decl_enum_item **i_ptr) psi_num_exp_free(&i->num); } } - free(i->name); + zend_string_release(i->name); free(i); } } void psi_decl_enum_item_dump(int fd, struct psi_decl_enum_item *item) { - dprintf(fd, "%s", item->name); + dprintf(fd, "%s", item->name->val); if (item->num && item->num != &item->inc) { dprintf(fd, " = "); psi_num_exp_dump(fd, item->num); diff --git a/src/types/decl_enum_item.h b/src/types/decl_enum_item.h index 4a3e9a7..c465ed1 100644 --- a/src/types/decl_enum_item.h +++ b/src/types/decl_enum_item.h @@ -33,14 +33,14 @@ struct psi_decl_enum; struct psi_decl_enum_item { struct psi_token *token; - char *name; + zend_string *name; int64_t val; struct psi_num_exp inc; struct psi_num_exp *num; struct psi_decl_enum_item *prev; }; -struct psi_decl_enum_item *psi_decl_enum_item_init(const char *name, struct psi_num_exp *num); +struct psi_decl_enum_item *psi_decl_enum_item_init(zend_string *name, struct psi_num_exp *num); void psi_decl_enum_item_free(struct psi_decl_enum_item **i_ptr); void psi_decl_enum_item_dump(int fd, struct psi_decl_enum_item *item); bool psi_decl_enum_item_validate(struct psi_data *data, struct psi_decl_enum *enm, struct psi_decl_enum_item *item, size_t seq); diff --git a/src/types/decl_extvar.c b/src/types/decl_extvar.c index efbec20..c21226e 100644 --- a/src/types/decl_extvar.c +++ b/src/types/decl_extvar.c @@ -30,6 +30,8 @@ #include #include +#include + #include "data.h" struct psi_decl_extvar *psi_decl_extvar_init(struct psi_decl_arg *arg) @@ -46,9 +48,7 @@ void psi_decl_extvar_free(struct psi_decl_extvar **evar_ptr) struct psi_decl_extvar *evar = *evar_ptr; *evar_ptr = NULL; - if (evar->token) { - free(evar->token); - } + psi_token_free(&evar->token); psi_decl_arg_free(&evar->arg); psi_decl_free(&evar->getter); psi_decl_free(&evar->setter); @@ -66,7 +66,7 @@ bool psi_decl_extvar_validate(struct psi_data *data, evar->size = psi_decl_arg_get_size(evar->arg); if (!evar->size) { data->error(data, evar->arg->var->token, PSI_WARNING, - "Failed to calculate size of symbol '%s'", evar->arg->var->name); + "Failed to calculate size of symbol '%s'", evar->arg->var->name->val); return false; } @@ -75,7 +75,7 @@ bool psi_decl_extvar_validate(struct psi_data *data, void *dl; while (!evar->sym && psi_plist_get(data->file.dlopened, i++, &dl)) { - evar->sym = dlsym(dl, evar->arg->var->name); + evar->sym = dlsym(dl, evar->arg->var->name->val); } } if (!evar->sym) { @@ -85,10 +85,10 @@ bool psi_decl_extvar_validate(struct psi_data *data, #ifndef RTLD_DEFAULT # define RTLD_DEFAULT ((void *) 0) #endif - evar->sym = dlsym(RTLD_DEFAULT, evar->arg->var->name); + evar->sym = dlsym(RTLD_DEFAULT, evar->arg->var->name->val); if (!evar->sym) { data->error(data, evar->arg->var->token, PSI_WARNING, - "Failed to locate symbol '%s': %s", evar->arg->var->name, + "Failed to locate symbol '%s': %s", evar->arg->var->name->val, dlerror() ?: "not found"); return false; } @@ -115,7 +115,8 @@ void psi_decl_extvar_dump(int fd, struct psi_decl_extvar *evar) struct psi_decl *psi_decl_extvar_setter(struct psi_decl_extvar *evar) { - struct psi_decl_type *func_type = psi_decl_type_init(PSI_T_VOID, "void"); + struct psi_decl_type *func_type = psi_decl_type_init(PSI_T_VOID, + zend_string_init(ZEND_STRS("void"), 1)); struct psi_decl_var *func_var = psi_decl_var_copy(evar->arg->var); struct psi_decl_arg *func = psi_decl_arg_init(func_type, func_var); struct psi_decl_type *arg_type = psi_decl_type_copy(evar->arg->type); @@ -123,11 +124,15 @@ struct psi_decl *psi_decl_extvar_setter(struct psi_decl_extvar *evar) struct psi_decl_arg *arg = psi_decl_arg_init(arg_type, arg_var); struct psi_plist *args = psi_plist_init((psi_plist_dtor) psi_decl_arg_free); struct psi_decl *decl = psi_decl_init(func, psi_plist_add(args, &arg)); + smart_str name = {0}; func_var->pointer_level = 0; func_var->array_size = 0; - func_var->name = realloc(func_var->name, strlen(evar->arg->var->name) + sizeof("_set")); - strcat(func_var->name, "_set"); + + smart_str_append_ex(&name, func_var->name, 1); + smart_str_appendl_ex(&name, ZEND_STRL("_set"), 1); + zend_string_release(func_var->name); + func_var->name = smart_str_extract(&name); decl->extvar = 1; @@ -145,9 +150,12 @@ struct psi_decl *psi_decl_extvar_getter(struct psi_decl_extvar *evar) struct psi_decl_var *func_var = psi_decl_var_copy(evar->arg->var); struct psi_decl_arg *func = psi_decl_arg_init(func_type, func_var); struct psi_decl *decl = psi_decl_init(func, NULL); + smart_str name = {0}; - func_var->name = realloc(func_var->name, strlen(evar->arg->var->name) + sizeof("_get")); - strcat(func_var->name, "_get"); + smart_str_append_ex(&name, func_var->name, 1); + smart_str_appendl_ex(&name, ZEND_STRL("_set"), 1); + zend_string_release(func_var->name); + func_var->name = smart_str_extract(&name); decl->extvar = 1; diff --git a/src/types/decl_file.c b/src/types/decl_file.c index df07579..89c2323 100644 --- a/src/types/decl_file.c +++ b/src/types/decl_file.c @@ -37,7 +37,7 @@ void psi_decl_file_dtor(struct psi_decl_file *file) psi_plist_free(file->dlopened); } if (file->filename) { - free(file->filename); + zend_string_release(file->filename); } memset(file, 0, sizeof(*file)); } @@ -84,7 +84,7 @@ bool psi_decl_file_validate(struct psi_data *dst, struct psi_data *src) } if (src->file.filename) { - dst->file.filename = strdup(src->file.filename); + dst->file.filename = zend_string_copy(src->file.filename); } return true; } diff --git a/src/types/decl_file.h b/src/types/decl_file.h index 0b62b18..b34daba 100644 --- a/src/types/decl_file.h +++ b/src/types/decl_file.h @@ -29,7 +29,7 @@ struct psi_data; struct psi_decl_file { - char *filename; + zend_string *filename; struct psi_plist *libnames; struct psi_plist *dlopened; }; diff --git a/src/types/decl_struct.c b/src/types/decl_struct.c index 35818b2..cfe46c9 100644 --- a/src/types/decl_struct.c +++ b/src/types/decl_struct.c @@ -28,11 +28,11 @@ #include -struct psi_decl_struct* psi_decl_struct_init(const char *name, +struct psi_decl_struct* psi_decl_struct_init(zend_string *name, struct psi_plist *args) { struct psi_decl_struct *s = calloc(1, sizeof(*s)); - s->name = strdup(name); + s->name = zend_string_copy(name); s->args = args; return s; } @@ -43,23 +43,21 @@ void psi_decl_struct_free(struct psi_decl_struct **s_ptr) struct psi_decl_struct *s = *s_ptr; *s_ptr = NULL; - if (s->token) { - free(s->token); - } + psi_token_free(&s->token); if (s->args) { psi_plist_free(s->args); } if (s->engine.type && s->engine.dtor) { s->engine.dtor(s->engine.type); } - free(s->name); + zend_string_release(s->name); free(s); } } void psi_decl_struct_dump(int fd, struct psi_decl_struct *strct) { - dprintf(fd, "struct %s::(%zu, %zu)", strct->name, strct->align, + dprintf(fd, "struct %s::(%zu, %zu)", strct->name->val, strct->align, strct->size); if (psi_plist_count(strct->args)) { psi_decl_type_dump_args_with_layout(fd, strct->args, 0); @@ -119,7 +117,8 @@ bool psi_decl_struct_validate(struct psi_data *data, struct psi_decl_struct *s, if (!align) { data->error(data, darg->token, PSI_WARNING, "Computed zero alignment of %s.%s of type '%s'", - len, s->name, darg->var->name, darg->type->name); + len, s->name->val, darg->var->name->val, + darg->type->name->val); psi_validate_scope_del_struct(scope, s->name); return false; } @@ -128,14 +127,15 @@ bool psi_decl_struct_validate(struct psi_data *data, struct psi_decl_struct *s, data->error(data, darg->token, PSI_WARNING, "Computed size %zu of %s.%s does not match" " pre-defined size %zu of type '%s'", - len, s->name, darg->var->name, darg->layout->len, - darg->type->name); + len, s->name->val, darg->var->name->val, + darg->layout->len, darg->type->name->val); } if (darg->layout->pos != pos) { data->error(data, darg->token, PSI_WARNING, "Computed offset %zu of %s.%s does not match" " pre-defined offset %zu", - pos, s->name, darg->var->name, darg->layout->pos); + pos, s->name->val, darg->var->name->val, + darg->layout->pos); } } else { if (i) { @@ -155,7 +155,8 @@ bool psi_decl_struct_validate(struct psi_data *data, struct psi_decl_struct *s, break; default: data->error(data, darg->token, PSI_WARNING, - "Unsupported type for bit field: %s", real->name); + "Unsupported type for bit field: %s", + real->name->val); psi_validate_scope_del_struct(scope, s->name); return false; } @@ -180,7 +181,8 @@ bool psi_decl_struct_validate(struct psi_data *data, struct psi_decl_struct *s, data->error(data, darg->token, PSI_WARNING, "Computed offset %zu of %s.%s does not match" " pre-defined offset %zu", - pos, s->name, darg->var->name, darg->layout->pos); + pos, s->name->val, darg->var->name->val, + darg->layout->pos); } darg->layout->pos = pos; darg->layout->len = len; diff --git a/src/types/decl_struct.h b/src/types/decl_struct.h index 5a7e45a..51cdbe2 100644 --- a/src/types/decl_struct.h +++ b/src/types/decl_struct.h @@ -34,7 +34,7 @@ struct psi_validate_scope; struct psi_decl_struct { struct psi_token *token; - char *name; + zend_string *name; struct psi_plist *args; size_t size; size_t align; @@ -44,7 +44,7 @@ struct psi_decl_struct { } engine; }; -struct psi_decl_struct *psi_decl_struct_init(const char *name, struct psi_plist *args); +struct psi_decl_struct *psi_decl_struct_init(zend_string *name, struct psi_plist *args); void psi_decl_struct_free(struct psi_decl_struct **s_ptr); void psi_decl_struct_dump(int fd, struct psi_decl_struct *strct); diff --git a/src/types/decl_type.c b/src/types/decl_type.c index 00112b3..31c49b4 100644 --- a/src/types/decl_type.c +++ b/src/types/decl_type.c @@ -30,11 +30,11 @@ #define PSI_STD_TYPES #include "php_psi_posix.h" -struct psi_decl_type *psi_decl_type_init(token_t type, const char *name) +struct psi_decl_type *psi_decl_type_init(token_t type, zend_string *name) { struct psi_decl_type *t = calloc(1, sizeof(*t)); t->type = type; - t->name = strdup(name); + t->name = zend_string_copy(name); return t; } @@ -44,13 +44,11 @@ void psi_decl_type_free(struct psi_decl_type **type_ptr) struct psi_decl_type *type = *type_ptr; *type_ptr = NULL; - if (type->token) { - free(type->token); - } + psi_token_free(&type->token); if (type->type == PSI_T_FUNCTION) { psi_decl_free(&type->real.func); } - free(type->name); + zend_string_release(type->name); free(type); } } @@ -61,7 +59,7 @@ struct psi_decl_type *psi_decl_type_copy(struct psi_decl_type *src) dst->type = src->type; if (src->name) { - dst->name = strdup(src->name); + dst->name = zend_string_copy(src->name); } if (src->token) { dst->token = psi_token_copy(src->token); @@ -120,14 +118,14 @@ bool psi_decl_type_get_alias(struct psi_decl_type *type, struct psi_plist *defs) } if (defs) { while (psi_plist_get(defs, i++, &def)) { - if (!strcmp(def->var->name, type->name)) { + if (zend_string_equals(def->var->name, type->name)) { type->real.def = def; return true; } } } for (stdtyp = &psi_std_types[0]; stdtyp->type_tag; ++stdtyp) { - if (!strcmp(type->name, stdtyp->alias ?: stdtyp->type_name)) { + if (!strcmp(type->name->val, stdtyp->alias ?: stdtyp->type_name)) { type->type = stdtyp->type_tag; return true; } @@ -146,7 +144,7 @@ bool psi_decl_type_get_struct(struct psi_decl_type *type, struct psi_plist *stru } if (structs) { while (psi_plist_get(structs, i++, &s)) { - if (!strcmp(s->name, type->name)) { + if (zend_string_equals(s->name, type->name)) { type->real.strct = s; return true; } @@ -165,7 +163,7 @@ bool psi_decl_type_get_union(struct psi_decl_type *type, struct psi_plist *union } if (unions) { while (psi_plist_get(unions, i++, &u)) { - if (!strcmp(u->name, type->name)) { + if (zend_string_equals(u->name, type->name)) { type->real.unn = u; return true; } @@ -184,7 +182,7 @@ bool psi_decl_type_get_enum(struct psi_decl_type *type, struct psi_plist *enums) } if (enums) { while (psi_plist_get(enums, i++, &e)) { - if (!strcmp(e->name, type->name)) { + if (zend_string_equals(e->name, type->name)) { type->real.enm = e; return true; } @@ -203,7 +201,7 @@ bool psi_decl_type_get_decl(struct psi_decl_type *type, struct psi_plist *decls) } if (decls) { while (psi_plist_get(decls, i++, &decl)) { - if (!strcmp(decl->func->var->name, type->name)) { + if (zend_string_equals(decl->func->var->name, type->name)) { type->real.func = decl; return true; } @@ -243,7 +241,7 @@ bool psi_decl_type_validate(struct psi_data *data, struct psi_decl_type *type, return true; } else { data->error(data, type->token, PSI_WARNING, - "Unknown struct '%s'", type->name); + "Unknown struct '%s'", type->name->val); return false; } } @@ -259,7 +257,7 @@ bool psi_decl_type_validate(struct psi_data *data, struct psi_decl_type *type, return true; } else { data->error(data, type->token, PSI_WARNING, - "Unknown union '%s'", type->name); + "Unknown union '%s'", type->name->val); return false; } } @@ -270,14 +268,14 @@ bool psi_decl_type_validate(struct psi_data *data, struct psi_decl_type *type, case PSI_T_ENUM: if (!psi_decl_type_get_enum(type, data->enums)) { data->error(data, type->token, PSI_WARNING, - "Unknown enum '%s'", type->name); + "Unknown enum '%s'", type->name->val); return false; } break; case PSI_T_FUNCTION: if (!psi_decl_type_get_decl(type, data->decls)) { data->error(data, type->token, PSI_WARNING, - "Unknown decl '%s'", type->name); + "Unknown decl '%s'", type->name->val); return false; } if (!psi_decl_validate_nodl(data, type->real.func, scope)) { @@ -324,7 +322,7 @@ void psi_decl_type_dump(int fd, struct psi_decl_type *t, unsigned level) { switch (t->type) { case PSI_T_POINTER: - dprintf(fd, "%s *", t->name); + dprintf(fd, "%s *", t->name->val); return; case PSI_T_ENUM: @@ -372,7 +370,7 @@ void psi_decl_type_dump(int fd, struct psi_decl_type *t, unsigned level) break; } - dprintf(fd, "%s", t->name); + dprintf(fd, "%s", t->name->val); } int psi_decl_type_is_weak(struct psi_decl_type *type) diff --git a/src/types/decl_type.h b/src/types/decl_type.h index 8e80b5e..da95e64 100644 --- a/src/types/decl_type.h +++ b/src/types/decl_type.h @@ -40,7 +40,7 @@ struct psi_validate_scope; struct psi_decl_type { struct psi_token *token; - char *name; + zend_string *name; token_t type; union { struct psi_decl_arg *def; @@ -52,9 +52,9 @@ struct psi_decl_type { }; #include -#define psi_decl_type_is_anon(name, type) !strncmp(name, type "@", sizeof(type)) +#define psi_decl_type_is_anon(name, type) !strncmp(name->val, type "@", sizeof(type)) -struct psi_decl_type *psi_decl_type_init(token_t type, const char *name); +struct psi_decl_type *psi_decl_type_init(token_t type, zend_string *name); struct psi_decl_type *psi_decl_type_copy(struct psi_decl_type *src); void psi_decl_type_free(struct psi_decl_type **type_ptr); void psi_decl_type_dump(int fd, struct psi_decl_type *t, unsigned level); diff --git a/src/types/decl_union.c b/src/types/decl_union.c index 5d0f163..826dc48 100644 --- a/src/types/decl_union.c +++ b/src/types/decl_union.c @@ -28,11 +28,11 @@ #include -struct psi_decl_union* psi_decl_union_init(const char *name, +struct psi_decl_union* psi_decl_union_init(zend_string *name, struct psi_plist *args) { struct psi_decl_union *u = calloc(1, sizeof(*u)); - u->name = strdup(name); + u->name = zend_string_copy(name); u->args = args; return u; } @@ -43,20 +43,18 @@ void psi_decl_union_free(struct psi_decl_union **u_ptr) struct psi_decl_union *u = *u_ptr; *u_ptr = NULL; - if (u->token) { - free(u->token); - } + psi_token_free(&u->token); if (u->args) { psi_plist_free(u->args); } - free(u->name); + zend_string_release(u->name); free(u); } } void psi_decl_union_dump(int fd, struct psi_decl_union *unn) { - dprintf(fd, "union %s::(%zu, %zu)", unn->name, unn->align, unn->size); + dprintf(fd, "union %s::(%zu, %zu)", unn->name->val, unn->align, unn->size); if (psi_plist_count(unn->args)) { psi_decl_type_dump_args_with_layout(fd, unn->args, 0); } else { @@ -86,7 +84,7 @@ bool psi_decl_union_validate(struct psi_data *data, struct psi_decl_union *u, if (!u->size && !psi_plist_count(u->args)) { data->error(data, u->token, PSI_WARNING, - "Cannot compute size of empty union %s", u->name); + "Cannot compute size of empty union %s", u->name->val); return false; } @@ -107,16 +105,16 @@ bool psi_decl_union_validate(struct psi_data *data, struct psi_decl_union *u, if (darg->layout->pos != 0) { data->error(data, darg->token, PSI_WARNING, - "Offset of %s.%s should be 0", u->name, - darg->var->name); + "Offset of %s.%s should be 0", u->name->val, + darg->var->name->val); darg->layout->pos = 0; } if (darg->layout->len != len) { data->error(data, darg->token, PSI_WARNING, "Computed size %zu of %s.%s does not match" " pre-defined size %zu of type '%s'", - len, u->name, darg->var->name, darg->layout->len, - darg->type->name); + len, u->name->val, darg->var->name->val, + darg->layout->len, darg->type->name->val); } } else { pos = 0; @@ -126,8 +124,8 @@ bool psi_decl_union_validate(struct psi_data *data, struct psi_decl_union *u, if (darg->layout) { if (darg->layout->pos != 0) { data->error(data, darg->token, PSI_WARNING, - "Offset of %s.%s should be 0", u->name, - darg->var->name); + "Offset of %s.%s should be 0", u->name->val, + darg->var->name->val); darg->layout->pos = 0; } darg->layout->len = len; diff --git a/src/types/decl_union.h b/src/types/decl_union.h index be059ba..1227ed1 100644 --- a/src/types/decl_union.h +++ b/src/types/decl_union.h @@ -33,13 +33,13 @@ struct psi_validate_scope; struct psi_decl_union { struct psi_token *token; - char *name; + zend_string *name; struct psi_plist *args; size_t size; size_t align; }; -struct psi_decl_union *psi_decl_union_init(const char *name, struct psi_plist *args); +struct psi_decl_union *psi_decl_union_init(zend_string *name, struct psi_plist *args); void psi_decl_union_free(struct psi_decl_union **u_ptr); void psi_decl_union_dump(int fd, struct psi_decl_union *unn); diff --git a/src/types/decl_var.c b/src/types/decl_var.c index 990b9ae..56c525c 100644 --- a/src/types/decl_var.c +++ b/src/types/decl_var.c @@ -28,13 +28,15 @@ #include -struct psi_decl_var *psi_decl_var_init(const char *name, unsigned pl, +#include + +struct psi_decl_var *psi_decl_var_init(zend_string *name, unsigned pl, unsigned as) { struct psi_decl_var *v = calloc(1, sizeof(*v)); if (name) { - v->name = strdup(name); - v->fqn = strdup(name); + v->name = zend_string_copy(name); + v->fqn = zend_string_copy(name); } v->pointer_level = pl; v->array_size = as; @@ -48,8 +50,8 @@ struct psi_decl_var *psi_decl_var_copy(struct psi_decl_var *src) *dest = *src; if (dest->name) { - dest->name = strdup(dest->name); - dest->fqn = strdup(dest->fqn); + dest->name = zend_string_copy(dest->name); + dest->fqn = zend_string_copy(dest->fqn); } if (dest->token) { @@ -64,12 +66,10 @@ void psi_decl_var_free(struct psi_decl_var **var_ptr) struct psi_decl_var *var = *var_ptr; *var_ptr = NULL; - if (var->token) { - free(var->token); - } + psi_token_free(&var->token); if (var->name) { - free(var->name); - free(var->fqn); + zend_string_release(var->name); + zend_string_release(var->fqn); } free(var); } @@ -79,12 +79,23 @@ void psi_decl_var_dump(int fd, struct psi_decl_var *var) { dprintf(fd, "%s%s", psi_t_indirection(var->pointer_level - !!var->array_size), - var->name ? var->name : "/**/"); + var->name ? var->name->val : "/**/"); if (var->array_size && var->arg->type->type != PSI_T_FUNCTION) { dprintf(fd, "[%u]", var->array_size); } } +static inline zend_string *psi_decl_var_name_prepend(zend_string *current, zend_string *prepend) { + smart_str name = {0}; + + smart_str_alloc(&name, prepend->len + 1 + current->len, 1); + smart_str_append_ex(&name, prepend, 1); + smart_str_appendc_ex(&name, '.', 1); + smart_str_append_ex(&name, current, 1); + + return smart_str_extract(&name); +} + bool psi_decl_var_validate(struct psi_data *data, struct psi_decl_var *dvar, struct psi_validate_scope *scope) { @@ -104,13 +115,15 @@ bool psi_decl_var_validate(struct psi_data *data, struct psi_decl_var *dvar, if (args && psi_decl_arg_get_by_var(dvar, args, NULL)) { okay = true; - } else if (!strcmp(svar->name, dvar->name)) { + } else if (zend_string_equals(svar->name, dvar->name)) { dvar->arg = svar->arg; okay = true; } } if (okay) { + zend_string *tmp = dvar->fqn; dvar->fqn = psi_decl_var_name_prepend(dvar->fqn, svar->name); + zend_string_release(tmp); } } } else if (scope && scope->current_set) { @@ -127,13 +140,15 @@ bool psi_decl_var_validate(struct psi_data *data, struct psi_decl_var *dvar, if (args && psi_decl_arg_get_by_var(dvar, args, NULL)) { okay = true; - } else if (!strcmp(svar->name, dvar->name)) { + } else if (zend_string_equals(svar->name, dvar->name)) { dvar->arg = svar->arg; okay = true; } } if (okay) { + zend_string *tmp = dvar->fqn; dvar->fqn = psi_decl_var_name_prepend(dvar->fqn, svar->name); + zend_string_release(tmp); } } } diff --git a/src/types/decl_var.h b/src/types/decl_var.h index 69f9adc..ca9a5fd 100644 --- a/src/types/decl_var.h +++ b/src/types/decl_var.h @@ -35,32 +35,17 @@ struct psi_validate_scope; struct psi_decl_var { struct psi_token *token; - char *name, *fqn; + zend_string *name, *fqn; unsigned pointer_level; unsigned array_size; struct psi_decl_arg *arg; }; -struct psi_decl_var *psi_decl_var_init(const char *name, unsigned pl, unsigned as); +struct psi_decl_var *psi_decl_var_init(zend_string *name, unsigned pl, unsigned as); struct psi_decl_var *psi_decl_var_copy(struct psi_decl_var *src); void psi_decl_var_free(struct psi_decl_var **var_ptr); void psi_decl_var_dump(int fd, struct psi_decl_var *var); -#include - -static inline char *psi_decl_var_name_prepend(char *current, const char *prepend) { - size_t c_len = strlen(current); - size_t p_len = strlen(prepend); - - current = realloc(current, p_len + 1 + c_len + 1); - if (current) { - memmove(current + p_len + 1, current, c_len + 1); - current[p_len] = '.'; - memcpy(current, prepend, p_len); - } - return current; -} - bool psi_decl_var_validate(struct psi_data *data, struct psi_decl_var *dvar, struct psi_validate_scope *scope); diff --git a/src/types/free_exp.c b/src/types/free_exp.c index 9f53611..115da91 100644 --- a/src/types/free_exp.c +++ b/src/types/free_exp.c @@ -27,10 +27,10 @@ #include "data.h" #include "call.h" -struct psi_free_exp *psi_free_exp_init(const char *func, struct psi_plist *vars) +struct psi_free_exp *psi_free_exp_init(zend_string *func, struct psi_plist *vars) { struct psi_free_exp *f = calloc(1, sizeof(*f)); - f->func = strdup(func); + f->func = zend_string_copy(func); f->vars = vars; return f; } @@ -41,10 +41,8 @@ void psi_free_exp_free(struct psi_free_exp **f_ptr) struct psi_free_exp *f = *f_ptr; *f_ptr = NULL; - if (f->token) { - free(f->token); - } - free(f->func); + psi_token_free(&f->token); + zend_string_release(f->func); psi_plist_free(f->vars); if (f->let) { free(f->let); @@ -58,7 +56,7 @@ void psi_free_exp_dump(int fd, struct psi_free_exp *call) size_t l = 0, c = psi_plist_count(call->vars); struct psi_decl_var *fvar; - dprintf(fd, "%s(", call->func); + dprintf(fd, "%s(", call->func->val); while (psi_plist_get(call->vars, l++, &fvar)) { psi_decl_var_dump(fd, fvar); if (l < c) { @@ -76,7 +74,7 @@ static inline struct psi_decl *locate_free_decl(struct psi_plist *decls, struct psi_decl *decl; while (psi_plist_get(decls, i++, &decl)) { - if (!strcmp(decl->func->var->name, f->func)) { + if (zend_string_equals(decl->func->var->name, f->func)) { return f->decl = decl; } } @@ -95,7 +93,8 @@ bool psi_free_exp_validate(struct psi_data *data, struct psi_free_exp *exp, if (!locate_free_decl(data->decls, exp)) { data->error(data, exp->token, PSI_WARNING, "Missing declaration '%s' in `free` statement" - " of implementation '%s'", exp->func, scope->impl->func->name); + " of implementation '%s'", exp->func->val, + scope->impl->func->name->val); return false; } @@ -106,7 +105,7 @@ bool psi_free_exp_validate(struct psi_data *data, struct psi_free_exp *exp, data->error(data, free_var->token, PSI_WARNING, "Unknown variable '%s' of `free` statement" " of implementation '%s'", - free_var->name, scope->impl->func->name); + free_var->name->val, scope->impl->func->name->val); return false; } diff --git a/src/types/free_exp.h b/src/types/free_exp.h index 41abc02..a615868 100644 --- a/src/types/free_exp.h +++ b/src/types/free_exp.h @@ -36,13 +36,13 @@ struct psi_call_frame; struct psi_free_exp { struct psi_token *token; - char *func; + zend_string *func; struct psi_plist *vars; struct psi_decl *decl; struct psi_let_stmt **let; }; -struct psi_free_exp *psi_free_exp_init(const char *func, struct psi_plist *vars); +struct psi_free_exp *psi_free_exp_init(zend_string *func, struct psi_plist *vars); void psi_free_exp_free(struct psi_free_exp **f_ptr); void psi_free_exp_dump(int fd, struct psi_free_exp *call); diff --git a/src/types/free_stmt.c b/src/types/free_stmt.c index 4e5a10c..18677af 100644 --- a/src/types/free_stmt.c +++ b/src/types/free_stmt.c @@ -39,9 +39,7 @@ void psi_free_stmt_free(struct psi_free_stmt **f_ptr) struct psi_free_stmt *f = *f_ptr; *f_ptr = NULL; - if (f->token) { - free(f->token); - } + psi_token_free(&f->token); psi_plist_free(f->exps); free(f); } diff --git a/src/types/impl.c b/src/types/impl.c index e987147..d2c87b9 100644 --- a/src/types/impl.c +++ b/src/types/impl.c @@ -190,7 +190,7 @@ struct psi_decl_arg *psi_impl_get_decl_arg(struct psi_impl *impl, struct psi_decl_var *arg; while (psi_plist_get(ret->exp->args, i++, &arg)) { - if (!strcmp(var->name, arg->name)) { + if (zend_string_equals(var->name, arg->name)) { return var->arg = arg->arg; } } @@ -221,7 +221,7 @@ struct psi_impl_arg *psi_impl_get_arg(struct psi_impl *impl, struct psi_impl_arg *iarg; while (psi_plist_get(impl->func->args, i++, &iarg)) { - if (!strcmp(var->name, iarg->var->name)) { + if (zend_string_equals(var->name, iarg->var->name)) { return var->arg = iarg; } } @@ -238,7 +238,7 @@ struct psi_decl_arg *psi_impl_get_temp_let_arg(struct psi_impl *impl, if (let->exp->kind != PSI_LET_TMP) { continue; } - if (strcmp(let->exp->var->name, var->name)) { + if (!zend_string_equals(let->exp->var->name, var->name)) { continue; } return var->arg = let->exp->var->arg; diff --git a/src/types/impl_def_val.c b/src/types/impl_def_val.c index fe11b26..311b39d 100644 --- a/src/types/impl_def_val.c +++ b/src/types/impl_def_val.c @@ -44,7 +44,7 @@ struct psi_impl_def_val *psi_impl_def_val_init(token_t t, void *data) /* no break */ case PSI_T_STRING: if (data) { - def->ival.zend.str = zend_string_init(data, strlen(data), 1); + def->ival.zend.str = zend_string_copy(data); } break; @@ -65,9 +65,7 @@ void psi_impl_def_val_free(struct psi_impl_def_val **def_ptr) struct psi_impl_def_val *def = *def_ptr; *def_ptr = NULL; - if (def->token) { - free(def->token); - } + psi_token_free(&def->token); switch (def->type) { case PSI_T_NUMBER: psi_num_exp_free(&def->data.num); @@ -114,12 +112,14 @@ bool psi_impl_def_val_validate(struct psi_data *data, case PSI_T_DOUBLE: val->type = PSI_T_FLOAT; type->type = PSI_T_FLOAT; - strcpy(type->name, "float"); + zend_string_release(type->name); + type->name = zend_string_init(ZEND_STRL("float"), 1); break; default: val->type = PSI_T_INT; type->type = PSI_T_INT; - strcpy(type->name, "int"); + zend_string_release(type->name); + type->name = zend_string_init(ZEND_STRL("int"), 1); break; } psi_num_exp_free(&val->data.num); @@ -160,7 +160,7 @@ bool psi_impl_def_val_validate(struct psi_data *data, data->error(data, val->token, PSI_WARNING, "Invalid default value type '%s', " "expected one of bool, int, float, string.", - type->name); + type->name->val); } return false; diff --git a/src/types/impl_func.c b/src/types/impl_func.c index 5893b4f..d160448 100644 --- a/src/types/impl_func.c +++ b/src/types/impl_func.c @@ -26,12 +26,12 @@ #include "php_psi_stdinc.h" #include "data.h" -struct psi_impl_func *psi_impl_func_init(const char *name, +struct psi_impl_func *psi_impl_func_init(zend_string *name, struct psi_plist *args, struct psi_impl_type *type) { struct psi_impl_func *func = calloc(1, sizeof(*func)); - func->name = strdup(name); + func->name = zend_string_copy(name); func->args = args ? : psi_plist_init((psi_plist_dtor) psi_impl_arg_free); func->return_type = type; @@ -45,9 +45,7 @@ void psi_impl_func_free(struct psi_impl_func **f_ptr) struct psi_impl_func *f = *f_ptr; *f_ptr = NULL; - if (f->token) { - free(f->token); - } + psi_token_free(&f->token); psi_impl_type_free(&f->return_type); psi_plist_free(f->args); @@ -56,7 +54,7 @@ void psi_impl_func_free(struct psi_impl_func **f_ptr) psi_impl_arg_free(&f->vararg); } - free(f->name); + zend_string_release(f->name); free(f); } } @@ -79,7 +77,7 @@ bool psi_impl_func_validate(struct psi_data *data, struct psi_impl_func *func, "Non-optional argument %zu '$%s' of implementation '%s'" " follows optional argument", i + 1, - iarg->var->name, func->name); + iarg->var->name->val, func->name->val); return false; } } @@ -89,7 +87,7 @@ bool psi_impl_func_validate(struct psi_data *data, struct psi_impl_func *func, void psi_impl_func_dump(int fd, struct psi_impl_func *func) { - dprintf(fd, "function %s(", func->name); + dprintf(fd, "function %s(", func->name->val); if (func->args) { size_t i = 0; struct psi_impl_arg *iarg; @@ -107,5 +105,5 @@ void psi_impl_func_dump(int fd, struct psi_impl_func *func) } } dprintf(fd, ") : %s%s", func->return_reference ? "&" : "", - func->return_type->name); + func->return_type->name->val); } diff --git a/src/types/impl_func.h b/src/types/impl_func.h index cf87e1b..38ebf68 100644 --- a/src/types/impl_func.h +++ b/src/types/impl_func.h @@ -35,7 +35,7 @@ struct psi_impl_type; struct psi_impl_func { struct psi_token *token; - char *name; + zend_string *name; struct psi_plist *args; struct psi_impl_arg *vararg; struct psi_impl_type *return_type; @@ -43,7 +43,8 @@ struct psi_impl_func { unsigned static_memory:1; }; -struct psi_impl_func *psi_impl_func_init(const char *name, struct psi_plist *args, struct psi_impl_type *return_type); +struct psi_impl_func *psi_impl_func_init(zend_string *name, + struct psi_plist *args, struct psi_impl_type *return_type); void psi_impl_func_free(struct psi_impl_func **f_ptr); void psi_impl_func_dump(int fd, struct psi_impl_func *func); bool psi_impl_func_validate(struct psi_data *data, struct psi_impl_func *func, diff --git a/src/types/impl_type.c b/src/types/impl_type.c index 25b1f1f..3517727 100644 --- a/src/types/impl_type.c +++ b/src/types/impl_type.c @@ -26,19 +26,19 @@ #include "php_psi_stdinc.h" #include "data.h" -struct psi_impl_type *psi_impl_type_init(token_t type, const char *name) +struct psi_impl_type *psi_impl_type_init(token_t type, zend_string *name) { struct psi_impl_type *t = calloc(1, sizeof(*t)); t->type = type; - t->name = strdup(name); + t->name = zend_string_copy(name); return t; } void psi_impl_type_dump(int fd, struct psi_impl_type *type) { - dprintf(fd, "%s", type->name); + dprintf(fd, "%s", type->name->val); } void psi_impl_type_free(struct psi_impl_type **type_ptr) @@ -47,10 +47,8 @@ void psi_impl_type_free(struct psi_impl_type **type_ptr) struct psi_impl_type *type = *type_ptr; *type_ptr = NULL; - if (type->token) { - free(type->token); - } - free(type->name); + psi_token_free(&type->token); + zend_string_release(type->name); free(type); } } diff --git a/src/types/impl_type.h b/src/types/impl_type.h index 330a978..7c5a6fe 100644 --- a/src/types/impl_type.h +++ b/src/types/impl_type.h @@ -30,11 +30,11 @@ struct psi_impl_type { struct psi_token *token; - char *name; + zend_string *name; token_t type; }; -struct psi_impl_type *psi_impl_type_init(token_t type, const char *name); +struct psi_impl_type *psi_impl_type_init(token_t type, zend_string *name); void psi_impl_type_free(struct psi_impl_type **type_ptr); void psi_impl_type_dump(int fd, struct psi_impl_type *type); diff --git a/src/types/impl_var.c b/src/types/impl_var.c index 7b690bb..1fa4173 100644 --- a/src/types/impl_var.c +++ b/src/types/impl_var.c @@ -26,12 +26,14 @@ #include "php_psi_stdinc.h" #include "data.h" -struct psi_impl_var *psi_impl_var_init(const char *name, bool is_reference) +#include + +struct psi_impl_var *psi_impl_var_init(zend_string *name, bool is_reference) { struct psi_impl_var *var = calloc(1, sizeof(*var)); - var->name = strdup(name); - var->fqn = strdup(name); + var->name = zend_string_copy(name); + var->fqn = zend_string_copy(name); var->reference = is_reference; return var; @@ -43,8 +45,8 @@ struct psi_impl_var *psi_impl_var_copy(struct psi_impl_var *var) *cpy = *var; - cpy->name = strdup(cpy->name); - cpy->fqn = strdup(cpy->fqn); + cpy->name = zend_string_copy(cpy->name); + cpy->fqn = zend_string_copy(cpy->fqn); if (cpy->token) { cpy->token = psi_token_copy(cpy->token); @@ -58,11 +60,9 @@ void psi_impl_var_free(struct psi_impl_var **var_ptr) struct psi_impl_var *var = *var_ptr; *var_ptr = NULL; - if (var->token) { - free(var->token); - } - free(var->name); - free(var->fqn); + psi_token_free(&var->token); + zend_string_release(var->name); + zend_string_release(var->fqn); free(var); } } @@ -72,7 +72,16 @@ void psi_impl_var_dump(int fd, struct psi_impl_var *var, bool vararg) dprintf(fd, "%s%s%s", var->reference ? "&" : "", vararg ? "..." : "", - var->name); + var->name->val); +} + +static inline zend_string *psi_impl_var_name_prepend(zend_string *current, zend_string *prepend) { + smart_str name = {0}; + + smart_str_append_ex(&name, prepend, 1); + smart_str_appendc_ex(&name, '.', 1); + smart_str_appendl_ex(&name, ZSTR_VAL(current) + 1, ZSTR_LEN(current) - 1, 1); + return smart_str_extract(&name); } bool psi_impl_var_validate(struct psi_data *data, struct psi_impl_var *ivar, @@ -83,15 +92,19 @@ bool psi_impl_var_validate(struct psi_data *data, struct psi_impl_var *ivar, while ((current_let_exp = current_let_exp->outer)) { struct psi_impl_var *svar = psi_let_exp_get_impl_var(current_let_exp); + zend_string *tmp = ivar->fqn; - ivar->fqn = psi_impl_var_name_prepend(ivar->fqn, svar->name + 1); + ivar->fqn = psi_impl_var_name_prepend(ivar->fqn, svar->name); + zend_string_release(tmp); } } else if (scope && scope->current_set) { struct psi_set_exp *current_set_exp = scope->current_set; while ((current_set_exp = current_set_exp->outer)) { struct psi_impl_var *svar = psi_set_exp_get_impl_var(current_set_exp); + zend_string *tmp = ivar->fqn; - ivar->fqn = psi_impl_var_name_prepend(ivar->fqn, svar->name + 1); + ivar->fqn = psi_impl_var_name_prepend(ivar->fqn, svar->name); + zend_string_release(tmp); } } diff --git a/src/types/impl_var.h b/src/types/impl_var.h index 762f421..2a179ad 100644 --- a/src/types/impl_var.h +++ b/src/types/impl_var.h @@ -33,40 +33,16 @@ struct psi_validate_scope; struct psi_impl_var { struct psi_token *token; - char *name, *fqn; + zend_string *name, *fqn; struct psi_impl_arg *arg; unsigned reference:1; }; -struct psi_impl_var *psi_impl_var_init(const char *name, bool is_reference); +struct psi_impl_var *psi_impl_var_init(zend_string *name, bool is_reference); struct psi_impl_var *psi_impl_var_copy(struct psi_impl_var *var); void psi_impl_var_free(struct psi_impl_var **var_ptr); void psi_impl_var_dump(int fd, struct psi_impl_var *var, bool vararg); -#include - -static inline char *psi_impl_var_name_prepend(char *current, const char *prepend) { - size_t c_len = strlen(current); - size_t p_len = strlen(prepend); - - current = realloc(current, p_len - + c_len // includes '$' - + 1 // connecting dot - + 1 // terminating 0 - ); - if (current) { - if (c_len > 1) { - memmove(current + p_len + 1 + 1, current + 1, c_len - 1 + 1); - current[p_len + 1] = '.'; - } else { - /* just '$' */ - current[p_len + 1] = '\0'; - } - memcpy(current + 1, prepend, p_len); - } - return current; -} - bool psi_impl_var_validate(struct psi_data *data, struct psi_impl_var *ivar, struct psi_validate_scope *scope); diff --git a/src/types/let_callback.c b/src/types/let_callback.c index 45639f8..7126b12 100644 --- a/src/types/let_callback.c +++ b/src/types/let_callback.c @@ -47,9 +47,7 @@ void psi_let_callback_free(struct psi_let_callback **cb_ptr) if (cb->cb_args) { psi_plist_free(cb->cb_args); } - if (cb->token) { - free(cb->token); - } + psi_token_free(&cb->token); free(cb); } } @@ -67,7 +65,7 @@ static inline bool psi_let_callback_validate_decl_args(struct psi_data *data, data->error(data, cb->token, PSI_WARNING, "Argument count of callback statement of implementation '%s'" "does not match argument count of callback declaration '%s'", - impl->func->name, cb_decl->func->var->name); + impl->func->name->val, cb_decl->func->var->name->val); return false; } @@ -91,7 +89,7 @@ bool psi_let_callback_validate(struct psi_data *data, struct psi_let_callback *c if (cb_type->type != PSI_T_FUNCTION) { data->error(data, cb_var->token, PSI_WARNING, "Expected a function: %s", - cb_var->name); + cb_var->name->val); return false; } cb->decl = cb_type->real.func; @@ -143,8 +141,8 @@ void psi_let_callback_dump(int fd, struct psi_let_callback *callback, } } dprintf(fd, ") as %s(%s(", - callback->func->name, - callback->func->var->name); + callback->func->name->val, + callback->func->var->name->val); if (callback->args) { size_t i = 0, last = psi_plist_count(callback->args); diff --git a/src/types/let_calloc.c b/src/types/let_calloc.c index e14ef69..8b26431 100644 --- a/src/types/let_calloc.c +++ b/src/types/let_calloc.c @@ -45,9 +45,7 @@ void psi_let_calloc_free(struct psi_let_calloc **alloc_ptr) *alloc_ptr = NULL; psi_num_exp_free(&alloc->nmemb); psi_num_exp_free(&alloc->size); - if (alloc->token) { - free(alloc->token); - } + psi_token_free(&alloc->token); free(alloc); } } diff --git a/src/types/let_exp.c b/src/types/let_exp.c index 3175060..fa74b16 100644 --- a/src/types/let_exp.c +++ b/src/types/let_exp.c @@ -30,6 +30,8 @@ #include +#include + struct psi_let_exp *psi_let_exp_init_ex(struct psi_decl_var *var, enum psi_let_exp_kind kind, void *data) { @@ -123,7 +125,7 @@ void psi_let_exp_dump(int fd, struct psi_let_exp *val, unsigned level, int last) break; case PSI_LET_TMP: psi_decl_var_dump(fd, val->data.var); - dprintf(fd, "\t/* fqn=%s */", val->data.var->fqn); + dprintf(fd, "\t/* fqn=%s */", val->data.var->fqn->val); break; case PSI_LET_CALLOC: psi_let_calloc_dump(fd, val->data.alloc); @@ -143,7 +145,7 @@ void psi_let_exp_dump(int fd, struct psi_let_exp *val, unsigned level, int last) } if (val->var) { - dprintf(fd, "\t/* fqn=%s */", val->var->fqn); + dprintf(fd, "\t/* fqn=%s */", val->var->fqn->val); } if (level > 1) { @@ -165,7 +167,7 @@ bool psi_let_exp_validate(struct psi_data *data, struct psi_let_exp *val, if (!psi_decl_var_validate(data, val->data.var, scope)) { data->error(data, dvar->token ? : **(struct psi_token ***) &val->data, PSI_WARNING, "Unknown variable '%s' in temp let statment of implementation '%s'", - dvar->name, scope->impl->func->name); + dvar->name->val, scope->impl->func->name->val); return false; } break; @@ -174,7 +176,7 @@ bool psi_let_exp_validate(struct psi_data *data, struct psi_let_exp *val, if (!psi_decl_var_validate(data, dvar, scope)) { data->error(data, dvar->token ? : **(struct psi_token ***) &val->data, PSI_WARNING, "Unknown variable '%s' in let statement of implementation '%s'", - dvar->name, scope->impl->func->name); + dvar->name->val, scope->impl->func->name->val); return false; } break; @@ -322,10 +324,11 @@ struct psi_let_func* psi_let_exp_get_func(struct psi_let_exp* exp) struct psi_decl_var *psi_let_exp_get_decl_var(struct psi_let_exp *val) { if (!val->var) { - const char *name = psi_let_exp_get_decl_var_name(val); + zend_string *name = psi_let_exp_get_decl_var_name(val); if (name) { val->var = psi_decl_var_init(name, 0, 0); + zend_string_release(name); } } return val->var; @@ -337,15 +340,22 @@ struct psi_impl_var *psi_let_exp_get_impl_var(struct psi_let_exp *val) return fn ? fn->var : NULL; } -const char* psi_let_exp_get_decl_var_name(struct psi_let_exp* val) +zend_string *psi_let_exp_get_decl_var_name(struct psi_let_exp* val) { struct psi_impl_var* var; + if (val->var) { - return val->var->name; + return zend_string_copy(val->var->name); } + var = psi_let_exp_get_impl_var(val); if (var) { - return &var->name[1]; + smart_str name = {0}; + + smart_str_appendl_ex(&name, &var->name->val[1], var->name->len - 1, 1); + + return smart_str_extract(&name); } + return NULL; } diff --git a/src/types/let_exp.h b/src/types/let_exp.h index 3e6eddc..91cb1ef 100644 --- a/src/types/let_exp.h +++ b/src/types/let_exp.h @@ -70,6 +70,6 @@ bool psi_let_exp_validate(struct psi_data *data, struct psi_let_exp *exp, struct struct psi_let_func *psi_let_exp_get_func(struct psi_let_exp *exp); struct psi_impl_var *psi_let_exp_get_impl_var(struct psi_let_exp *exp); struct psi_decl_var *psi_let_exp_get_decl_var(struct psi_let_exp *val); -const char *psi_let_exp_get_decl_var_name(struct psi_let_exp *exp); +zend_string *psi_let_exp_get_decl_var_name(struct psi_let_exp *exp); #endif diff --git a/src/types/let_func.c b/src/types/let_func.c index fdb89a0..468d215 100644 --- a/src/types/let_func.c +++ b/src/types/let_func.c @@ -32,12 +32,12 @@ #include -struct psi_let_func *psi_let_func_init(token_t type, const char *name, +struct psi_let_func *psi_let_func_init(token_t type, zend_string *name, struct psi_impl_var *var) { struct psi_let_func *func = calloc(1, sizeof(*func)); func->type = type; - func->name = strdup(name); + func->name = zend_string_copy(name); func->var = var; return func; } @@ -48,11 +48,9 @@ void psi_let_func_free(struct psi_let_func **func_ptr) struct psi_let_func *func = *func_ptr; *func_ptr = NULL; - if (func->token) { - free(func->token); - } + psi_token_free(&func->token); psi_impl_var_free(&func->var); - free(func->name); + zend_string_release(func->name); if (func->inner) { psi_plist_free(func->inner); } @@ -62,7 +60,8 @@ void psi_let_func_free(struct psi_let_func **func_ptr) void psi_let_func_dump(int fd, struct psi_let_func *func, unsigned level) { - dprintf(fd, "%s(%s\t/* fqn=%s */", func->name, func->var->name, func->var->fqn); + dprintf(fd, "%s(%s\t/* fqn=%s */", func->name->val, func->var->name->val, + func->var->fqn->val); if (func->inner) { size_t i = 0, count = psi_plist_count(func->inner); @@ -108,7 +107,7 @@ static inline int validate_let_func_type(struct psi_data *data, default: data->error(data, func->var->token, PSI_WARNING, "Unknown `let` cast function '%s' of implementation '%s'", - func->name, scope->impl->func->name); + func->name->val, scope->impl->func->name->val); return false; } } @@ -133,7 +132,7 @@ static inline bool validate_let_func_inner(struct psi_data *data, struct psi_let_exp *inner; while (psi_plist_get(func->inner, i++, &inner)) { - const char *name = psi_let_exp_get_decl_var_name(inner); + zend_string *name = psi_let_exp_get_decl_var_name(inner); struct psi_decl_arg *sub_arg; inner->outer = exp; @@ -169,7 +168,7 @@ static inline bool validate_let_func_inner(struct psi_data *data, sub_var = psi_let_exp_get_impl_var(exp); sub_ref = psi_let_exp_get_impl_var(inner); - if (strcmp(sub_var->name, sub_ref->name)) { + if (!zend_string_equals(sub_var->name, sub_ref->name)) { data->error(data, sub_var->token, E_WARNING, "Inner `set` statement casts on pointers must" " reference the same variable"); @@ -186,7 +185,7 @@ static inline bool validate_let_func_inner(struct psi_data *data, "Inner let statement's values must refer to a structure or" " array type, got '%s%s' for '%s'", var_typ->name, psi_t_indirection(let_var->arg->var->pointer_level), - let_var->name); + let_var->name->val); return false; } @@ -235,7 +234,7 @@ void exec_let_func_arrval_inner(struct psi_let_func *func, * we only want to set supplied data on unions */ if (!zend_symtable_str_exists(Z_ARRVAL_P(frame_arg->zval_ptr), - &inner_var->name[1], strlen(&inner_var->name[1]))) { + &inner_var->name->val[1], inner_var->name->len - 1)) { return; } } @@ -244,8 +243,8 @@ void exec_let_func_arrval_inner(struct psi_let_func *func, /* example from dm_store/dbm_fetch with arrval($string) conversion: let key = arrval($key, - dptr = strval($0), - dsize = strlen($0) + dptr = strval($0), + dsize = strlen($0) ); # --- darg = key diff --git a/src/types/let_func.h b/src/types/let_func.h index 60fdefd..5313a93 100644 --- a/src/types/let_func.h +++ b/src/types/let_func.h @@ -38,12 +38,12 @@ struct psi_call_frame; struct psi_let_func { struct psi_token *token; token_t type; - char *name; + zend_string *name; struct psi_impl_var *var; struct psi_plist *inner; }; -struct psi_let_func *psi_let_func_init(token_t type, const char *name, struct psi_impl_var *var); +struct psi_let_func *psi_let_func_init(token_t type, zend_string *name, struct psi_impl_var *var); void psi_let_func_free(struct psi_let_func **func_ptr); void psi_let_func_dump(int fd, struct psi_let_func *func, unsigned level); diff --git a/src/types/let_stmt.c b/src/types/let_stmt.c index 9ada463..b85f0ce 100644 --- a/src/types/let_stmt.c +++ b/src/types/let_stmt.c @@ -43,9 +43,7 @@ void psi_let_stmt_free(struct psi_let_stmt **stmt_ptr) if (stmt->exp) { psi_let_exp_free(&stmt->exp); } - if (stmt->token) { - free(stmt->token); - } + psi_token_free(&stmt->token); free(stmt); } } @@ -77,14 +75,14 @@ bool psi_let_stmts_validate(struct psi_data *data, struct psi_validate_scope *sc if (!let->exp->var) { data->error(data, let->token, PSI_WARNING, "Missing variable in `let` statement for implementation %s", - scope->impl->func->name); + scope->impl->func->name->val); return false; } if (!psi_impl_get_decl_arg(scope->impl, let_var)) { data->error(data, let_var->token, PSI_WARNING, "Unknown variable '%s' in `let` statement of implementation '%s'", - let_var->name, scope->impl->func->name); + let_var->name->val, scope->impl->func->name->val); return false; } switch (let->exp->kind) { @@ -100,7 +98,7 @@ bool psi_let_stmts_validate(struct psi_data *data, struct psi_validate_scope *sc if (let_ivar && !psi_impl_get_arg(scope->impl, let_ivar)) { data->error(data, let_var->token, PSI_WARNING, "Unknown variable '%s' in `let` statement of implementation '%s'", - let_ivar->name, scope->impl->func->name); + let_ivar->name->val, scope->impl->func->name->val); return false; } @@ -120,10 +118,11 @@ bool psi_let_stmts_validate(struct psi_data *data, struct psi_validate_scope *sc data->error(data, scope->impl->func->token, PSI_WARNING, "Missing `let` statement for arg '%s %s%s'" " of declaration '%s' for implementation '%s'", - darg->type->name, + darg->type->name->val, psi_t_indirection(darg->var->pointer_level), - darg->var->name, scope->impl->decl->func->var->name, - scope->impl->func->name); + darg->var->name->val, + scope->impl->decl->func->var->name->val, + scope->impl->func->name->val); return false; } } diff --git a/src/types/num_exp.c b/src/types/num_exp.c index 4b8846a..72f374e 100644 --- a/src/types/num_exp.c +++ b/src/types/num_exp.c @@ -216,9 +216,7 @@ void psi_num_exp_free(struct psi_num_exp **c_ptr) assert(0); } - if (c->token) { - free(c->token); - } + psi_token_free(&c->token); free(c); } @@ -306,7 +304,7 @@ struct psi_plist *psi_num_exp_tokens(struct psi_num_exp *exp, list = psi_plist_add(list, &ntoken); ntoken = psi_token_copy(exp->data.c.typ->token); list = psi_plist_add(list, &ntoken); - ntoken = psi_token_init(PSI_T_RPAREN, ")", 1, ntoken->col+ntoken->size, ntoken->line, ntoken->file); + ntoken = psi_token_init(PSI_T_RPAREN, ")", 1, ntoken->col+ntoken->text->len, ntoken->line, ntoken->file); list = psi_plist_add(list, &ntoken); break; @@ -323,7 +321,7 @@ struct psi_plist *psi_num_exp_tokens(struct psi_num_exp *exp, list = psi_plist_add(list, &ntoken); list = psi_num_exp_tokens(exp->data.u, list); psi_plist_top(list, &ntoken); - ntoken = psi_token_init(PSI_T_RPAREN, ")", 1, ntoken->col+ntoken->size, ntoken->line, ntoken->file); + ntoken = psi_token_init(PSI_T_RPAREN, ")", 1, ntoken->col+ntoken->text->len, ntoken->line, ntoken->file); list = psi_plist_add(list, &ntoken); break; @@ -363,7 +361,7 @@ struct psi_plist *psi_num_exp_tokens(struct psi_num_exp *exp, list = psi_plist_add(list, &ntoken); list = psi_num_exp_tokens(exp->data.t.truthy, list); psi_plist_top(list, &ntoken); - ntoken = psi_token_init(PSI_T_COLON, ":", 1, ntoken->col+ntoken->size, ntoken->line, ntoken->file); + ntoken = psi_token_init(PSI_T_COLON, ":", 1, ntoken->col+ntoken->text->len, ntoken->line, ntoken->file); list = psi_plist_add(list, &ntoken); list = psi_plist_add(list, &ntoken); list = psi_num_exp_tokens(exp->data.t.falsy, list); diff --git a/src/types/number.c b/src/types/number.c index bc9d0a0..e877e40 100644 --- a/src/types/number.c +++ b/src/types/number.c @@ -84,7 +84,7 @@ struct psi_number *psi_number_init(token_t t, void *num, unsigned flags) case PSI_T_NUMBER: case PSI_T_NSNAME: case PSI_T_DEFINE: - exp->data.numb = strdup(num); + exp->data.numb = zend_string_copy(num); break; case PSI_T_NAME: exp->data.dvar = num; @@ -133,7 +133,7 @@ struct psi_number *psi_number_copy(struct psi_number *exp) case PSI_T_NSNAME: case PSI_T_DEFINE: case PSI_T_QUOTED_CHAR: - num->data.numb = strdup(num->data.numb); + num->data.numb = zend_string_copy(num->data.numb); break; case PSI_T_NAME: num->data.dvar = psi_decl_var_copy(num->data.dvar); @@ -184,7 +184,7 @@ void psi_number_free(struct psi_number **exp_ptr) case PSI_T_NUMBER: case PSI_T_DEFINE: case PSI_T_QUOTED_CHAR: - free(exp->data.numb); + zend_string_release(exp->data.numb); break; case PSI_T_NAME: psi_decl_var_free(&exp->data.dvar); @@ -217,8 +217,7 @@ struct psi_plist *psi_number_tokens(struct psi_number *exp, unsigned pl = exp->data.dvar->pointer_level - !!exp->data.dvar->array_size; while (pl--) { - - ntoken = psi_token_init(PSI_T_POINTER, "*", 1, ntoken->col+ntoken->size, ntoken->line, ntoken->file); + ntoken = psi_token_init(PSI_T_POINTER, "*", 1, ntoken->col+ntoken->text->len, ntoken->line, ntoken->file); list = psi_plist_add(list, &ntoken); } ntoken = temp; @@ -229,11 +228,11 @@ struct psi_plist *psi_number_tokens(struct psi_number *exp, if (exp->data.dvar->array_size) { char buf[0x20], *ptr; - ntoken = psi_token_init(PSI_T_LBRACKET, "[", 1, ntoken->col+ntoken->size, ntoken->line, ntoken->file); + ntoken = psi_token_init(PSI_T_LBRACKET, "[", 1, ntoken->col+ntoken->text->len, ntoken->line, ntoken->file); list = psi_plist_add(list, &ntoken); ptr = zend_print_ulong_to_buf(&buf[sizeof(buf) - 1], exp->data.dvar->array_size); - ntoken = psi_token_init(PSI_T_NUMBER, ptr, strlen(ptr), ntoken->col+ntoken->size, ntoken->line, ntoken->file); + ntoken = psi_token_init(PSI_T_NUMBER, ptr, strlen(ptr), ntoken->col+ntoken->text->len, ntoken->line, ntoken->file); list = psi_plist_add(list, &ntoken); } break; @@ -242,11 +241,11 @@ struct psi_plist *psi_number_tokens(struct psi_number *exp, /* decl_type */ ntoken = psi_token_copy(exp->token); list = psi_plist_add(list, &ntoken); - ntoken = psi_token_init(PSI_T_LPAREN, "(", 1, ntoken->col+ntoken->size, ntoken->line, ntoken->file); + ntoken = psi_token_init(PSI_T_LPAREN, "(", 1, ntoken->col+ntoken->text->len, ntoken->line, ntoken->file); list = psi_plist_add(list, &ntoken); ntoken = psi_token_copy(exp->data.dtyp->token); list = psi_plist_add(list, &ntoken); - ntoken = psi_token_init(PSI_T_RPAREN, ")", 1, ntoken->col+ntoken->size, ntoken->line, ntoken->file); + ntoken = psi_token_init(PSI_T_RPAREN, ")", 1, ntoken->col+ntoken->text->len, ntoken->line, ntoken->file); list = psi_plist_add(list, &ntoken); break; @@ -322,16 +321,16 @@ void psi_number_dump(int fd, struct psi_number *exp) case PSI_T_NSNAME: case PSI_T_DEFINE: case PSI_T_QUOTED_CHAR: - dprintf(fd, "%s", exp->data.numb); + dprintf(fd, "%s", exp->data.numb->val); break; case PSI_T_FUNCTION: psi_cpp_macro_call_dump(fd, exp->data.call); break; case PSI_T_CONST: - dprintf(fd, "%s", exp->data.cnst->name); + dprintf(fd, "%s", exp->data.cnst->name->val); break; case PSI_T_ENUM: - dprintf(fd, "%s", exp->data.enm->name); + dprintf(fd, "%s", exp->data.enm->name->val); break; case PSI_T_NAME: psi_decl_var_dump(fd, exp->data.dvar); @@ -359,7 +358,7 @@ static inline bool psi_number_validate_enum(struct psi_data *data, switch (exp->type) { case PSI_T_NAME: while (psi_plist_get(enm->items, i++, &itm)) { - if (!strcmp(itm->name, exp->data.dvar->name)) { + if (zend_string_equals(itm->name, exp->data.dvar->name)) { psi_decl_var_free(&exp->data.dvar); exp->type = PSI_T_ENUM; exp->data.enm = itm; @@ -370,8 +369,8 @@ static inline bool psi_number_validate_enum(struct psi_data *data, case PSI_T_DEFINE: while (psi_plist_get(enm->items, i++, &itm)) { - if (!strcmp(itm->name, exp->data.numb)) { - free(exp->data.numb); + if (zend_string_equals(itm->name, exp->data.numb)) { + zend_string_release(exp->data.numb); exp->type = PSI_T_ENUM; exp->data.enm = itm; return psi_number_validate(data, exp, scope); @@ -458,13 +457,13 @@ static inline bool psi_number_validate_char(struct psi_data *data, struct psi_nu { impl_val val = {0}; unsigned lvl = 1; - token_t typ = validate_char(exp->data.numb, &val, &lvl); + token_t typ = validate_char(exp->data.numb->val, &val, &lvl); if (!typ) { return false; } - free(exp->data.numb); + zend_string_release(exp->data.numb); exp->type = typ; exp->data.ival = val; return true; @@ -480,27 +479,27 @@ static inline bool psi_number_validate_number(struct psi_data *data, struct psi_ switch (exp->flags & 0x0f00) { case PSI_NUMBER_L: default: - tmp.i64 = strtol(exp->data.numb, NULL, 0); - free(exp->data.numb); + tmp.i64 = strtol(exp->data.numb->val, NULL, 0); + zend_string_release(exp->data.numb); exp->type = PSI_T_INT64; exp->data.ival.i64 = tmp.i64; break; case PSI_NUMBER_LL: - tmp.i64 = strtoll(exp->data.numb, NULL, 0); - free(exp->data.numb); + tmp.i64 = strtoll(exp->data.numb->val, NULL, 0); + zend_string_release(exp->data.numb); exp->type = PSI_T_INT64; exp->data.ival.i64 = tmp.i64; break; case PSI_NUMBER_U: case PSI_NUMBER_UL: - tmp.u64 = strtoul(exp->data.numb, NULL, 0); - free(exp->data.numb); + tmp.u64 = strtoul(exp->data.numb->val, NULL, 0); + zend_string_release(exp->data.numb); exp->type = PSI_T_UINT64; exp->data.ival.u64 = tmp.u64; break; case PSI_NUMBER_ULL: - tmp.u64 = strtoull(exp->data.numb, NULL, 0); - free(exp->data.numb); + tmp.u64 = strtoull(exp->data.numb->val, NULL, 0); + zend_string_release(exp->data.numb); exp->type = PSI_T_UINT64; exp->data.ival.u64 = tmp.u64; break; @@ -511,24 +510,24 @@ static inline bool psi_number_validate_number(struct psi_data *data, struct psi_ switch (exp->flags & 0x0ff00) { case PSI_NUMBER_F: case PSI_NUMBER_DF: - tmp.fval = strtof(exp->data.numb, NULL); - free(exp->data.numb); + tmp.fval = strtof(exp->data.numb->val, NULL); + zend_string_release(exp->data.numb); exp->type = PSI_T_FLOAT; exp->data.ival.fval = tmp.fval; break; case PSI_NUMBER_L: case PSI_NUMBER_DL: #if HAVE_LONG_DOUBLE - tmp.ldval = strtold(exp->data.numb, NULL); - free(exp->data.numb); + tmp.ldval = strtold(exp->data.numb->val, NULL); + zend_string_release(exp->data.numb); exp->type = PSI_T_LONG_DOUBLE; exp->data.ival.ldval = tmp.ldval; break; #endif case PSI_NUMBER_DD: default: - tmp.dval = strtod(exp->data.numb, NULL); - free(exp->data.numb); + tmp.dval = strtod(exp->data.numb->val, NULL); + zend_string_release(exp->data.numb); exp->type = PSI_T_DOUBLE; exp->data.ival.dval = tmp.dval; break; @@ -539,15 +538,15 @@ static inline bool psi_number_validate_number(struct psi_data *data, struct psi_ break; } } else { - switch (is_numeric_string(exp->data.numb, strlen(exp->data.numb), (zend_long *) &tmp, (double *) &tmp, 1)) { + switch (is_numeric_str_function(exp->data.numb, (zend_long *) &tmp, (double *) &tmp)) { case IS_LONG: - free(exp->data.numb); + zend_string_release(exp->data.numb); exp->type = PSI_T_INT64; exp->data.ival.i64 = tmp.zend.lval; return true; case IS_DOUBLE: - free(exp->data.numb); + zend_string_release(exp->data.numb); exp->type = PSI_T_DOUBLE; exp->data.ival.dval = tmp.dval; return true; @@ -586,7 +585,7 @@ bool psi_number_validate(struct psi_data *data, struct psi_number *exp, return true; case PSI_T_NAME: - if (scope && scope->defs && zend_hash_str_exists(scope->defs, exp->data.dvar->name, strlen(exp->data.dvar->name))) { + if (scope && scope->defs && zend_hash_exists(scope->defs, exp->data.dvar->name)) { return true; } if (scope && scope->current_enum && psi_number_validate_enum(data, exp, scope)) { @@ -607,18 +606,18 @@ bool psi_number_validate(struct psi_data *data, struct psi_number *exp, } data->error(data, exp->token, PSI_WARNING, "Unknown variable '%s' in numeric expression", - exp->data.dvar->name); + exp->data.dvar->name->val); return false; case PSI_T_FUNCTION: - if (scope && scope->defs && zend_hash_str_exists(scope->defs, exp->data.numb, strlen(exp->data.numb))) { + if (scope && scope->defs && zend_hash_exists(scope->defs, exp->data.numb)) { return true; } return false; case PSI_T_DEFINE: - if (scope && scope->defs && zend_hash_str_exists(scope->defs, exp->data.numb, strlen(exp->data.numb))) { - if (!scope->macro || strcmp(scope->macro->token->text, exp->data.numb)) { + if (scope && scope->defs && zend_hash_exists(scope->defs, exp->data.numb)) { + if (!scope->macro || !zend_string_equals(scope->macro->token->text, exp->data.numb)) { return true; } /* #define foo foo */ @@ -644,7 +643,7 @@ bool psi_number_validate(struct psi_data *data, struct psi_number *exp, struct psi_decl_type *dtyp = exp->data.dtyp; data->error(data, exp->token, PSI_WARNING, - "Cannot compute sizeof(%s) (%u)", dtyp->name, dtyp->type); + "Cannot compute sizeof(%s) (%u)", dtyp->name->val, dtyp->type); exp->type = PSI_T_UINT8; exp->data.ival.u8 = 0; @@ -656,22 +655,22 @@ bool psi_number_validate(struct psi_data *data, struct psi_number *exp, { zval *zc; - if (*exp->data.numb == '\\') { - zc = zend_get_constant_str(exp->data.numb + 1, strlen(exp->data.numb) - 1); + if (exp->data.numb->val[0] == '\\') { + zc = zend_get_constant_str(&exp->data.numb->val[1], exp->data.numb->len - 1); } else { - zc = zend_get_constant_str(exp->data.numb, strlen(exp->data.numb)); + zc = zend_get_constant(exp->data.numb); } if (zc) { switch (Z_TYPE_P(zc)) { case IS_LONG: - free(exp->data.numb); + zend_string_release(exp->data.numb); exp->type = PSI_T_INT64; exp->data.ival.i64 = Z_LVAL_P(zc); return true; case IS_DOUBLE: - free(exp->data.numb); + zend_string_release(exp->data.numb); exp->type = PSI_T_DOUBLE; exp->data.ival.dval = Z_DVAL_P(zc); return true; @@ -682,8 +681,8 @@ bool psi_number_validate(struct psi_data *data, struct psi_number *exp, } } while (psi_plist_get(data->consts, i++, &cnst)) { - if (!strcmp(cnst->name, exp->data.numb)) { - free(exp->data.numb); + if (zend_string_equals(cnst->name, exp->data.numb)) { + zend_string_release(exp->data.numb); exp->type = PSI_T_CONST; exp->data.cnst = cnst; return true; @@ -691,7 +690,7 @@ bool psi_number_validate(struct psi_data *data, struct psi_number *exp, } data->error(data, exp->token, PSI_WARNING, "Unknown constant '%s' in numeric expression", - exp->data.numb); + exp->data.numb->val); return false; case PSI_T_NUMBER: @@ -712,13 +711,11 @@ static inline token_t psi_number_eval_constant(struct psi_number *exp, { switch (exp->data.cnst->type->type) { case PSI_T_INT: - res->i64 = zend_get_constant_str(exp->data.cnst->name, - strlen(exp->data.cnst->name))->value.lval; + res->i64 = zend_get_constant(exp->data.cnst->name)->value.lval; if (frame) PSI_DEBUG_PRINT(frame->context, " %" PRIi64, res->i64); return PSI_T_INT64; case PSI_T_FLOAT: - res->dval = zend_get_constant_str(exp->data.cnst->name, - strlen(exp->data.cnst->name))->value.dval; + res->dval = zend_get_constant(exp->data.cnst->name)->value.dval; if (frame) PSI_DEBUG_PRINT(frame->context, " %" PRIdval, res->dval); return PSI_T_DOUBLE; default: @@ -764,7 +761,7 @@ static inline token_t psi_number_eval_define(struct psi_number *exp, if (defs) { struct psi_cpp_macro_decl *macro; - macro = zend_hash_str_find_ptr(defs, exp->data.numb, strlen(exp->data.numb)); + macro = zend_hash_find_ptr(defs, exp->data.numb); if (macro && macro->exp && macro->exp != rec_guard) { return psi_num_exp_exec(macro->exp, res, NULL, defs); } @@ -833,7 +830,7 @@ token_t psi_number_eval(struct psi_number *exp, impl_val *res, return PSI_T_INT64; case PSI_T_NUMBER: - res->i64 = atol(exp->data.numb); + res->i64 = atol(exp->data.numb->val); return PSI_T_INT64; case PSI_T_CONST: diff --git a/src/types/number.h b/src/types/number.h index a2dc792..9f3363c 100644 --- a/src/types/number.h +++ b/src/types/number.h @@ -63,7 +63,7 @@ struct psi_number { token_t type; unsigned flags; union { - char *numb; + zend_string *numb; impl_val ival; struct psi_const *cnst; struct psi_decl_var *dvar; diff --git a/src/types/return_exp.c b/src/types/return_exp.c index c5ee0a3..78e1aa5 100644 --- a/src/types/return_exp.c +++ b/src/types/return_exp.c @@ -45,9 +45,7 @@ void psi_return_exp_free(struct psi_return_exp **exp_ptr) struct psi_return_exp *exp = *exp_ptr; *exp_ptr = NULL; - if (exp->token) { - psi_token_free(&exp->token); - } + psi_token_free(&exp->token); if (exp->func) { psi_decl_var_free(&exp->func); } @@ -110,7 +108,7 @@ static inline bool psi_return_exp_validate_decl_args(struct psi_data *data, data->error(data, exp->token, PSI_WARNING, "Argument count of return statement of implementation '%s'" "does not match argument count of declaration '%s'", - impl->func->name, impl->decl->func->var->name); + impl->func->name->val, impl->decl->func->var->name->val); return false; } @@ -126,12 +124,11 @@ bool psi_return_exp_validate(struct psi_data *data, struct psi_return_exp *exp, { size_t i = 0; struct psi_decl *decl; - const char *name = psi_return_exp_get_decl_name(exp); - + zend_string *name = psi_return_exp_get_decl_name(exp); while (psi_plist_get(data->decls, i++, &decl)) { - if (!strcmp(decl->func->var->name, name)) { + if (zend_string_equals(decl->func->var->name, name)) { scope->impl->decl = decl; if (psi_return_exp_validate_decl_args(data, exp, scope->impl)) { scope->current_set = exp->set; @@ -147,11 +144,11 @@ bool psi_return_exp_validate(struct psi_data *data, struct psi_return_exp *exp, data->error(data, exp->token, PSI_WARNING, "Missing declaration '%s' for `return` statement of implementation %s", - name, scope->impl->func->name); + name->val, scope->impl->func->name->val); return false; } -const char *psi_return_exp_get_decl_name(struct psi_return_exp *exp) +zend_string *psi_return_exp_get_decl_name(struct psi_return_exp *exp) { return exp->func ? exp->func->name : exp->set->data.func->var->name; } diff --git a/src/types/return_exp.h b/src/types/return_exp.h index 864a798..b76313a 100644 --- a/src/types/return_exp.h +++ b/src/types/return_exp.h @@ -41,6 +41,6 @@ void psi_return_exp_exec(struct psi_return_exp *exp, zval *return_value, struct psi_call_frame *frame); bool psi_return_exp_validate(struct psi_data *data, struct psi_return_exp *exp, struct psi_validate_scope *scope); -const char *psi_return_exp_get_decl_name(struct psi_return_exp *exp); +zend_string *psi_return_exp_get_decl_name(struct psi_return_exp *exp); #endif /* RETURN_EXP_H */ diff --git a/src/types/return_stmt.c b/src/types/return_stmt.c index b4bcb52..8ff646d 100644 --- a/src/types/return_stmt.c +++ b/src/types/return_stmt.c @@ -46,9 +46,7 @@ void psi_return_stmt_free(struct psi_return_stmt **ret_ptr) struct psi_return_stmt *ret = *ret_ptr; *ret_ptr = NULL; - if (ret->token) { - free(ret->token); - } + psi_token_free(&ret->token); psi_return_exp_free(&ret->exp); free(ret); } @@ -78,12 +76,12 @@ bool psi_return_stmt_validate(struct psi_data *data, data->error(data, ret->token, PSI_WARNING, "Too many `return` statements for implementation %s;" " found %zu, exactly one is required", - scope->impl->func->name, count); + scope->impl->func->name->val, count); return false; case 0: data->error(data, scope->impl->func->token, PSI_WARNING, "Missing `return` statement for implementation %s", - scope->impl->func->name); + scope->impl->func->name->val); return false; case 1: break; diff --git a/src/types/set_exp.c b/src/types/set_exp.c index e19e79d..ef4173a 100644 --- a/src/types/set_exp.c +++ b/src/types/set_exp.c @@ -29,6 +29,8 @@ #include "calc.h" #include "marshal.h" +#include + struct psi_set_exp *psi_set_exp_init(enum psi_set_exp_kind kind, void *data) { struct psi_set_exp *val = calloc(1, sizeof(*val)); @@ -131,7 +133,7 @@ void psi_set_exp_dump(int fd, struct psi_set_exp *set, unsigned level, int last) if (set->var) { /* parsed, or generated */ if (set->var->token) { - dprintf(fd, "%s = ", set->var->name); + dprintf(fd, "%s = ", set->var->name->val); } } @@ -151,7 +153,7 @@ void psi_set_exp_dump(int fd, struct psi_set_exp *set, unsigned level, int last) } if (set->var) { - dprintf(fd, "\t/* fqn=%s */", set->var->fqn); + dprintf(fd, "\t/* fqn=%s */", set->var->fqn->val); } } @@ -169,15 +171,18 @@ struct psi_impl_var *psi_set_exp_get_impl_var(struct psi_set_exp *exp) { if (!exp->var) { struct psi_decl_var *dvar = psi_set_exp_get_decl_var(exp); - char *dollar_name; + zend_string *dollar_name; + smart_str name = {0}; if (!dvar) { return NULL; } - dollar_name = psi_impl_var_name_prepend(strdup("$"), dvar->name); + smart_str_appendc_ex(&name, '$', 1); + smart_str_append_ex(&name, dvar->name, 1); + dollar_name = smart_str_extract(&name); exp->var = psi_impl_var_init(dollar_name, 0); - free(dollar_name); + zend_string_release(dollar_name); } return exp->var; @@ -190,7 +195,7 @@ bool psi_set_exp_validate(struct psi_data *data, struct psi_set_exp *set, if (ivar && !psi_impl_var_validate(data, ivar, scope)) { data->error(data, ivar->token ? : **(struct psi_token ***) &set->data, - PSI_WARNING, "Unknown variable '%s'", ivar->name); + PSI_WARNING, "Unknown variable '%s'", ivar->name->val); return false; } diff --git a/src/types/set_func.c b/src/types/set_func.c index 2b59686..7ac2ed5 100644 --- a/src/types/set_func.c +++ b/src/types/set_func.c @@ -26,13 +26,13 @@ #include "php_psi_stdinc.h" #include "data.h" -struct psi_set_func *psi_set_func_init(token_t type, const char *name, +struct psi_set_func *psi_set_func_init(token_t type, zend_string *name, struct psi_decl_var *var) { struct psi_set_func *func = calloc(1, sizeof(*func)); func->type = type; - func->name = strdup(name); + func->name = zend_string_copy(name); func->var = var; return func; @@ -44,20 +44,18 @@ void psi_set_func_free(struct psi_set_func **func_ptr) struct psi_set_func *func = *func_ptr; *func_ptr = NULL; - if (func->token) { - free(func->token); - } + psi_token_free(&func->token); psi_decl_var_free(&func->var); - free(func->name); + zend_string_release(func->name); free(func); } } void psi_set_func_dump(int fd, struct psi_set_func *func, unsigned level) { - dprintf(fd, "%s(", func->name); + dprintf(fd, "%s(", func->name->val); psi_decl_var_dump(fd, func->var); - dprintf(fd, "\t/* fqn=%s */", func->var->fqn); + dprintf(fd, "\t/* fqn=%s */", func->var->fqn->val); if (func->inner && !func->recursive) { size_t i = 0, count = psi_plist_count(func->inner); struct psi_set_exp *inner; @@ -133,7 +131,7 @@ static inline bool psi_set_func_validate_to_array(struct psi_data *data, PSI_WARNING, "Expected to_type() cast expression"); return false; } - if (strcmp(set_var->name, psi_set_exp_get_decl_var(sub_exp)->name)) { + if (!zend_string_equals(set_var->name, psi_set_exp_get_decl_var(sub_exp)->name)) { /* no warning, because of ambiguity with a one-field-struct monstrosity */ goto complex; } @@ -151,13 +149,14 @@ static inline bool psi_set_func_validate_to_array(struct psi_data *data, PSI_WARNING, "Expected to_type() cast expression"); return false; } - if (strcmp(set_var->name, psi_set_exp_get_decl_var(sub_exp)->name)) { + if (!zend_string_equals(set_var->name, psi_set_exp_get_decl_var(sub_exp)->name)) { data->error(data, **(struct psi_token ***) &sub_exp->data, PSI_WARNING, "Expected %s(%s) cast expression to reference the same" " variable like the outer `set` statement, '%s'", - sub_exp->data.func->name, - psi_set_exp_get_decl_var(sub_exp)->name, set_var); + sub_exp->data.func->name->val, + psi_set_exp_get_decl_var(sub_exp)->name->val, + set_var->name->val); return false; } func->handler = psi_set_to_array_counted; @@ -173,7 +172,7 @@ static inline bool psi_set_func_validate_to_array(struct psi_data *data, data->error(data, func->token, PSI_WARNING, "Expected struct or union type for complex to_array()" " cast expression, got '%s'", - set_var->arg->type->name); + set_var->arg->type->name->val); return false; } func->handler = psi_set_to_array; @@ -191,7 +190,7 @@ static inline bool psi_set_func_validate_to_recursive(struct psi_data *data, data->error(data, func->token, PSI_WARNING, "Expected to_array() as parent to recursion in `set` statement" " of implementation '%s'", - impl->func->name); + impl->func->name->val); return false; } @@ -210,7 +209,7 @@ bool psi_set_func_validate(struct psi_data *data, struct psi_set_func *func, && !psi_impl_get_temp_let_arg(scope->impl, func->var)) { data->error(data, func->var->token, PSI_WARNING, "Unknown variable '%s' in implementation %s", - func->var->name, scope->impl->func->name); + func->var->name->val, scope->impl->func->name->val); return false; } @@ -286,7 +285,7 @@ bool psi_set_func_validate(struct psi_data *data, struct psi_set_func *func, default: data->error(data, func->token, PSI_WARNING, "Unknown cast '%s' in `set` statement of implementation '%s'", - func->name, scope->impl->func->name); + func->name->val, scope->impl->func->name->val); return false; } diff --git a/src/types/set_func.h b/src/types/set_func.h index 3c35069..3f6ef25 100644 --- a/src/types/set_func.h +++ b/src/types/set_func.h @@ -36,14 +36,14 @@ struct psi_validate_scope; struct psi_set_func { struct psi_token *token; token_t type; - char *name; + zend_string *name; struct psi_decl_var *var; psi_marshal_set handler; struct psi_plist *inner; unsigned recursive:1; }; -struct psi_set_func *psi_set_func_init(token_t type, const char *name, struct psi_decl_var *var); +struct psi_set_func *psi_set_func_init(token_t type, zend_string *name, struct psi_decl_var *var); void psi_set_func_free(struct psi_set_func **func_ptr); void psi_set_func_dump(int fd, struct psi_set_func *func, unsigned level); bool psi_set_func_validate(struct psi_data *data, struct psi_set_func *func, diff --git a/src/types/set_stmt.c b/src/types/set_stmt.c index 2139dc4..5d31aca 100644 --- a/src/types/set_stmt.c +++ b/src/types/set_stmt.c @@ -46,9 +46,7 @@ void psi_set_stmt_free(struct psi_set_stmt **set_ptr) *set_ptr = NULL; psi_set_exp_free(&set->exp); - if (set->token) { - free(set->token); - } + psi_token_free(&set->token); free(set); } } @@ -72,13 +70,13 @@ bool psi_set_stmts_validate(struct psi_data *data, struct psi_validate_scope *sc if (!set->exp->var) { data->error(data, set->token, PSI_WARNING, "Missing variable of `set` statement of implementation '%s'", - scope->impl->func->name); + scope->impl->func->name->val); return false; } if (!psi_impl_get_arg(scope->impl, set->exp->var)) { data->error(data, set->token, PSI_WARNING, "Unknown variable '%s' of `set` statement of implementation '%s'", - set->exp->var->name, scope->impl->func->name); + set->exp->var->name->val, scope->impl->func->name->val); return false; } @@ -92,7 +90,7 @@ bool psi_set_stmts_validate(struct psi_data *data, struct psi_validate_scope *sc if (!psi_impl_get_temp_let_arg(scope->impl, set->exp->data.func->var)) { data->error(data, set->token, PSI_WARNING, "Unknown variable '%s' of `set` statement of implementation '%s'", - set->exp->data.func->var->name, scope->impl->func->name); + set->exp->data.func->var->name->val, scope->impl->func->name->val); scope->current_set = NULL; return false; } diff --git a/src/validate.c b/src/validate.c index 2dfc3d4..6481527 100644 --- a/src/validate.c +++ b/src/validate.c @@ -76,7 +76,7 @@ static bool psi_validate_type(struct psi_validate_scope *scope, { struct psi_decl_arg *def = ptr; - PSI_DEBUG_PRINT(dst, "typedef %s", def->var->name); + PSI_DEBUG_PRINT(dst, "typedef %s", def->var->name->val); return psi_decl_arg_validate_typedef(dst, def, scope); } @@ -85,7 +85,7 @@ static bool psi_validate_struct(struct psi_validate_scope *scope, { struct psi_decl_struct *str = ptr; - PSI_DEBUG_PRINT(dst, "struct %s", str->name); + PSI_DEBUG_PRINT(dst, "struct %s", str->name->val); if (psi_decl_struct_validate(dst, str, scope)) { PSI_DEBUG_PRINT(dst, "::(%zu, %zu)", str->align, str->size); return true; @@ -98,7 +98,7 @@ static bool psi_validate_union(struct psi_validate_scope *scope, { struct psi_decl_union *unn = ptr; - PSI_DEBUG_PRINT(dst, "union %s", unn->name); + PSI_DEBUG_PRINT(dst, "union %s", unn->name->val); if (psi_decl_union_validate(dst, unn, scope)) { PSI_DEBUG_PRINT(dst, "::(%zu, %zu)", unn->align, unn->size); return true; @@ -111,7 +111,7 @@ static bool psi_validate_enum(struct psi_validate_scope *scope, { struct psi_decl_enum *enm = ptr; - PSI_DEBUG_PRINT(dst, "enum %s", enm->name); + PSI_DEBUG_PRINT(dst, "enum %s", enm->name->val); return psi_decl_enum_validate(dst, enm); } @@ -120,7 +120,7 @@ static bool psi_validate_extvar(struct psi_validate_scope *scope, { struct psi_decl_extvar *evar = ptr; - PSI_DEBUG_PRINT(dst, "extvar %s", evar->arg->var->name); + PSI_DEBUG_PRINT(dst, "extvar %s", evar->arg->var->name->val); if (psi_decl_extvar_validate(dst, evar, scope)) { dst->decls = psi_plist_add(dst->decls, &evar->getter); dst->decls = psi_plist_add(dst->decls, &evar->setter); @@ -134,7 +134,7 @@ static bool psi_validate_decl(struct psi_validate_scope *scope, { struct psi_decl *decl = ptr; - PSI_DEBUG_PRINT(dst, "decl %s", decl->func->var->name); + PSI_DEBUG_PRINT(dst, "decl %s", decl->func->var->name->val); return psi_decl_validate(dst, decl, scope); } @@ -143,7 +143,7 @@ static bool psi_validate_const(struct psi_validate_scope *scope, { struct psi_const *cnst = ptr; - PSI_DEBUG_PRINT(dst, "constant %s", cnst->name); + PSI_DEBUG_PRINT(dst, "constant %s", cnst->name->val); return psi_const_validate(dst, cnst, scope); } @@ -152,7 +152,7 @@ static bool psi_validate_impl(struct psi_validate_scope *scope, { struct psi_impl *impl = ptr; - PSI_DEBUG_PRINT(dst, "impl %s", impl->func->name); + PSI_DEBUG_PRINT(dst, "impl %s", impl->func->name->val); return psi_impl_validate(dst, impl, scope); } diff --git a/src/validate.h b/src/validate.h index 09a0e09..3e4dda6 100644 --- a/src/validate.h +++ b/src/validate.h @@ -59,32 +59,32 @@ static inline void psi_validate_scope_dtor(struct psi_validate_scope *scope) } #define psi_validate_scope_has_type(s, t) \ - ((s) ? zend_hash_str_exists(&(s)->types, (t), strlen(t)) : false) + ((s) ? zend_hash_exists(&(s)->types, (t)) : false) #define psi_validate_scope_has_struct(s, t) \ - ((s) ? zend_hash_str_exists(&(s)->structs, (t), strlen(t)) : false) + ((s) ? zend_hash_exists(&(s)->structs, (t)) : false) #define psi_validate_scope_has_union(s, t) \ - ((s) ? zend_hash_str_exists(&(s)->unions, (t), strlen(t)) : false) + ((s) ? zend_hash_exists(&(s)->unions, (t)) : false) #define psi_validate_scope_add_type(s, t, p) \ - do { if (s) zend_hash_str_add_ptr(&(s)->types, (t), strlen(t), (p)); } while(0) + do { if (s) zend_hash_add_ptr(&(s)->types, (t), (p)); } while(0) #define psi_validate_scope_add_struct(s, t, p) \ - do { if (s) zend_hash_str_add_ptr(&(s)->structs, (t), strlen(t), (p)); } while(0) + do { if (s) zend_hash_add_ptr(&(s)->structs, (t), (p)); } while(0) #define psi_validate_scope_add_union(s, t, p) \ - do { if (s) zend_hash_str_add_ptr(&(s)->unions, (t), strlen(t), (p)); } while(0) + do { if (s) zend_hash_add_ptr(&(s)->unions, (t), (p)); } while(0) #define psi_validate_scope_get_type(s, t) \ - ((s) ? zend_hash_str_find_ptr(&(s)->types, (t), strlen(t)) : NULL) + ((s) ? zend_hash_find_ptr(&(s)->types, (t)) : NULL) #define psi_validate_scope_get_struct(s, t) \ - ((s) ? zend_hash_str_find_ptr(&(s)->structs, (t), strlen(t)) : NULL) + ((s) ? zend_hash_find_ptr(&(s)->structs, (t)) : NULL) #define psi_validate_scope_get_union(s, t) \ - ((s) ? zend_hash_str_find_ptr(&(s)->unions, (t), strlen(t)) : NULL) + ((s) ? zend_hash_find_ptr(&(s)->unions, (t)) : NULL) #define psi_validate_scope_del_type(s, t) \ - do { if (s) zend_hash_str_del(&(s)->types, (t), strlen(t)); } while(0) + do { if (s) zend_hash_del(&(s)->types, (t)); } while(0) #define psi_validate_scope_del_struct(s, t) \ - do { if (s) zend_hash_str_del(&(s)->structs, (t), strlen(t)); } while(0) + do { if (s) zend_hash_del(&(s)->structs, (t)); } while(0) #define psi_validate_scope_del_union(s, t) \ - do { if (s) zend_hash_str_del(&(s)->unions, (t), strlen(t)); } while(0) + do { if (s) zend_hash_del(&(s)->unions, (t)); } while(0) #endif /* PSI_VALIDATE_H */ -- 2.30.2