X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Fcontext.c;h=3ab1d218797685340360eda838aad56a9f610c52;hp=3b5c734e106da577a7aa90a3cf4bea0d876eecab;hb=ac133e1eb5215f4da26898eb191a24fad848db0f;hpb=0e6d6d0bdaf5cca59d04a8f4e9fcca8a65f341d8 diff --git a/src/context.c b/src/context.c index 3b5c734..3ab1d21 100644 --- a/src/context.c +++ b/src/context.c @@ -4,82 +4,7 @@ #include "php.h" -#include -#ifdef HAVE_SYS_TYPES_H -# include -#endif -#ifdef HAVE_SYS_STAT_H -# include -#endif -#ifdef STDC_HEADERS -# include -# include -#else -# ifdef HAVE_STDLIB_H -# include -# endif -#endif -#ifdef HAVE_STRING_H -# if !defined STDC_HEADERS && defined HAVE_MEMORY_H -# include -# endif -# include -#endif -#ifdef HAVE_STRINGS_H -# include -#endif -#ifdef HAVE_INTTYPES_H -# include -#endif -#ifdef HAVE_STDINT_H -# include -#endif -#ifdef HAVE_UNISTD_H -# include -#endif - -#ifdef HAVE_ERRNO_H -# include -#endif -#ifdef HAVE_GLOB_H -# include -#endif -#ifdef HAVE_NETINET_IN_H -# include -#endif -#ifdef HAVE_ARPA_NAMESER_H -# include -#endif -#ifdef HAVE_NETDB_H -# include -#endif -#ifdef HAVE_RESOLV_H -# include -#endif -#ifdef HAVE_SYS_SELECT_H -# include -#endif -#ifdef HAVE_SYS_SOCKET_H -# include -#endif -#ifdef HAVE_SYS_TIME_H -# include -#endif -#ifdef HAVE_SYS_TIMES_H -# include -#endif -#ifdef HAVE_SYS_UIO_H -# include -#endif -#ifdef HAVE_SYS_UTSNAME_H -# include -#endif -#ifdef HAVE_TIME_H -# include -#endif -#ifdef HAVE_WCHAR_H -# include -#endif +#include "php_psi_stdinc.h" #ifdef HAVE_DIRENT_H # include @@ -115,6 +40,9 @@ static struct psi_std_type { } psi_std_types[] = { {PSI_T_FLOAT, "float"}, {PSI_T_DOUBLE, "double"}, +#ifdef HAVE_LONG_DOUBLE + {PSI_T_LONG_DOUBLE, "long double"}, +#endif {PSI_T_INT8, "int8_t"}, {PSI_T_INT16, "int16_t"}, {PSI_T_INT32, "int32_t"}, @@ -126,77 +54,13 @@ static struct psi_std_type { {0} }; -static struct psi_predef_type { - token_t type_tag; - const char *type_name; - const char *alias; -} psi_predef_types[] = { - PSI_TYPES - {0} -}; - -static struct psi_predef_const { - token_t type_tag; - const char *type_name; - const char *var_name; - const char *val_text; - token_t val_type_tag; -} psi_predef_consts[] = { - PSI_CONSTS - {0} -}; - -PSI_MACROS - -int psi_glob(const char *pattern, int flags, - int (*errfunc) (const char *epath, int eerrno), - glob_t *pglob) { - size_t offs = flags & GLOB_DOOFFS ? pglob->gl_offs : 0; - int rv = glob(pattern, flags, errfunc, pglob); - if (pglob->gl_pathv) { - while (offs--) { - pglob->gl_pathv[offs] = NULL; - } - } - return rv; -} - -static struct psi_func_redir { - const char *name; - void (*func)(void); -} psi_func_redirs[] = { - {"glob", (void (*)(void)) psi_glob}, - PSI_REDIRS - {0} -}; - -static struct psi_predef_decl { - token_t type_tag; - const char *type_name; - const char *var_name; - size_t pointer_level; - size_t array_size; -} psi_predef_decls[] = { - PSI_DECLS - {0} -}; -static struct psi_predef_decl psi_predef_vararg_decls[] = { - PSI_VA_DECLS - {0} -}; - -static struct psi_predef_struct { - token_t type_tag; - const char *type_name; - const char *var_name; - size_t offset; - size_t size; - size_t pointer_level; - size_t array_size; -} psi_predef_structs[] = { - PSI_STRUCTS - {0} -}; +#include "php_psi_types.h" +#include "php_psi_consts.h" +#include "php_psi_macros.h" +#include "php_psi_redirs.h" +#include "php_psi_decls.h" +#include "php_psi_va_decls.h" +#include "php_psi_structs.h" static int validate_lib(PSI_Data *data, void **dlopened) { char lib[MAXPATHLEN]; @@ -209,13 +73,13 @@ static int validate_lib(PSI_Data *data, void **dlopened) { } else if (!strchr(ptr, '/')) { len = snprintf(lib, MAXPATHLEN, "lib%s.%s", ptr, PHP_PSI_SHLIB_SUFFIX); if (MAXPATHLEN == len) { - data->error(PSI_WARNING, "Library name too long: '%s'", ptr); + data->error(NULL, PSI_WARNING, "Library name too long: '%s'", ptr); } lib[len] = 0; ptr = lib; } if (!(*dlopened = dlopen(ptr, RTLD_LAZY|RTLD_LOCAL))) { - data->error(PSI_WARNING, "Could not open library '%s': %s.", + data->error(NULL, PSI_WARNING, "Could not open library '%s': %s.", data->psi.file.ln, dlerror()); return 0; } @@ -263,6 +127,10 @@ static inline int locate_decl_type_struct(decl_structs *structs, decl_type *type static inline int validate_decl_type(PSI_Data *data, decl_type *type) { switch (type->type) { + case PSI_T_CHAR: + case PSI_T_SHORT: + case PSI_T_INT: + case PSI_T_LONG: case PSI_T_NAME: if (!data->defs || !locate_decl_type_alias(data->defs, type)) { return 0; @@ -281,7 +149,8 @@ static inline int validate_decl_type(PSI_Data *data, decl_type *type) { } static inline int validate_decl_typedef(PSI_Data *data, decl_typedef *def) { if (!validate_decl_type(data, def->type)) { - data->error(PSI_WARNING, "Type '%s' cannot be aliased to %s'%s'", + data->error(def->token, PSI_WARNING, + "Type '%s' cannot be aliased to %s'%s'", def->type->name, def->type->type == PSI_T_STRUCT?"struct ":"",def->alias); return 0; } @@ -296,15 +165,43 @@ static inline int validate_constant(PSI_Data *data, constant *c) { static inline int validate_decl_arg(PSI_Data *data, decl_arg *arg) { if (!validate_decl_type(data, arg->type)) { - data->error(PSI_WARNING, "Cannot use '%s'(%d) as type for decl var '%s'" - " in %s on line %zu at col %zu", - arg->type->name, arg->type->type, arg->var->name, - arg->type->token->file, arg->type->token->line, arg->type->token->col); + data->error(arg->type->token, PSI_WARNING, + "Cannot use '%s'(%d) as type for decl var '%s'", + arg->type->name, arg->type->type, arg->var->name); return 0; } return 1; } +static int psi_sort_struct_arg_cmp(const void *_a, const void *_b) { + decl_arg *a = *(decl_arg **)_a, *b = *(decl_arg **)_b; + + if (a->layout->pos == b->layout->pos) { + if (a->layout->len == b->layout->len) { + return 0; + } else if (a->layout->len > b->layout->len) { + return -1; + } else { + return 1; + } + } else if (a->layout->pos > b->layout->pos) { + return 1; + } else { + return -1; + } +} +static void psi_sort_struct_arg_swp(void *a, void *b) { + decl_arg **_a = a, **_b = b, *_c; + + _c = *_b; + *_b = *_a; + *_a = _c; +} +static inline void psi_sort_struct_args(decl_struct *s) { + zend_insert_sort(s->args->args, s->args->count, sizeof(*s->args->args), + psi_sort_struct_arg_cmp, psi_sort_struct_arg_swp); +} + static inline int validate_decl_struct(PSI_Data *data, decl_struct *s) { size_t i; @@ -346,6 +243,9 @@ static inline int validate_decl_struct(PSI_Data *data, decl_struct *s) { s->size = darg->layout->pos + darg->layout->len; } } + + psi_sort_struct_args(s); + return 1; } @@ -353,6 +253,7 @@ static const char * const abi_ccs[] = { "default", /* \ */ "extern", /* > - all the same */ "cdecl", /* / */ + "mscdecl", "stdcall", "fastcall", }; @@ -364,7 +265,6 @@ static inline int validate_decl_abi(PSI_Data *data, decl_abi *abi) { return 1; } } - data->error(PSI_WARNING, "Invalid calling convention: '%s'", abi->convention); return 0; } static inline int validate_decl_func(PSI_Data *data, void *dl, decl *decl, decl_arg *func) @@ -372,7 +272,7 @@ static inline int validate_decl_func(PSI_Data *data, void *dl, decl *decl, decl_ struct psi_func_redir *redir; if (!strcmp(func->var->name, "dlsym")) { - data->error(PSI_WARNING, "Cannot dlsym dlsym (sic!)"); + data->error(func->token, PSI_WARNING, "Cannot dlsym dlsym (sic!)"); return 0; } @@ -390,7 +290,8 @@ static inline int validate_decl_func(PSI_Data *data, void *dl, decl *decl, decl_ #endif decl->call.sym = dlsym(dl ?: RTLD_NEXT, func->var->name); if (!decl->call.sym) { - data->error(PSI_WARNING, "Failed to locate symbol '%s': %s", + data->error(func->token, PSI_WARNING, + "Failed to locate symbol '%s': %s", func->var->name, dlerror()); } } @@ -399,6 +300,8 @@ static inline int validate_decl_func(PSI_Data *data, void *dl, decl *decl, decl_ static inline int validate_decl(PSI_Data *data, void *dl, decl *decl) { if (!validate_decl_abi(data, decl->abi)) { + data->error(decl->abi->token, PSI_WARNING, + "Invalid calling convention: '%s'", decl->abi->token->text); return 0; } if (!validate_decl_func(data, dl, decl, decl->func)) { @@ -477,14 +380,14 @@ static inline int validate_num_exp(PSI_Data *data, decl_args *dargs, decl_arg *f switch (exp->t) { case PSI_T_NAME: if (!locate_decl_var_arg(exp->u.dvar, dargs, func)) { - data->error(PSI_WARNING, "Unknown variable '%s' in numeric expression", + data->error(exp->token, PSI_WARNING, "Unknown variable '%s' in numeric expression", exp->u.dvar->name); return 0; } return 1; case PSI_T_NSNAME: if (!locate_num_exp_constant(exp, data->consts)) { - data->error(PSI_WARNING, "Unknown constant '%s' in numeric expression", + data->error(exp->token, PSI_WARNING, "Unknown constant '%s' in numeric expression", exp->u.numb); return 0; } @@ -518,6 +421,14 @@ static inline int validate_set_value_handler(set_value *set) { case PSI_T_VOID: set->func->handler = psi_to_void; break; + case PSI_T_ELLIPSIS: + if (set->outer.set && set->outer.set->func->type == PSI_T_TO_ARRAY) { + set->func->handler = psi_to_recursive; + set->inner = set->outer.set->inner; + set->count = set->outer.set->count; + break; + } + /* no break */ default: return 0; } @@ -542,14 +453,14 @@ static inline int validate_set_value_ex(PSI_Data *data, set_value *set, decl_arg decl_var *set_var = set->vars->vars[0]; if (!validate_set_value_handler(set)) { - data->error(PSI_WARNING, "Invalid cast '%s'", set->func->name); + data->error(set->func->token, PSI_WARNING, "Invalid cast '%s' in `set` statement", set->func->name); return 0; } for (i = 0; i < set->vars->count; ++i) { decl_var *svar = set->vars->vars[i]; if (!svar->arg && !locate_decl_var_arg(svar, ref_list, NULL)) { - data->error(PSI_WARNING, "Unknown variable '%s'", svar->name); + data->error(svar->token, PSI_WARNING, "Unknown variable '%s' in `set` statement", svar->name); return 0; } } @@ -564,7 +475,7 @@ static inline int validate_set_value_ex(PSI_Data *data, set_value *set, decl_arg int is_pointer_to_struct = (ref_type->type == PSI_T_STRUCT && ref->var->pointer_level); if (!is_to_array && !is_pointer_to_struct) { - data->error(E_WARNING, "Inner `set` statement casts only work with " + data->error(set->func->token, E_WARNING, "Inner `set` statement casts only work with " "to_array() casts on structs or pointers: %s(%s...", set->func->name, set->vars->vars[0]->name); return 0; } @@ -577,14 +488,15 @@ static inline int validate_set_value_ex(PSI_Data *data, set_value *set, decl_arg if (ref_type->type == PSI_T_STRUCT) { /* to_array(struct, to_...) */ - for (i = 0; i < set->count; ++i) { - decl_var *sub_var = set->inner[i]->vars->vars[0]; - decl_arg *sub_ref = locate_struct_member(ref_type->strct, sub_var); - - set->inner[i]->outer.set = set; - if (sub_ref) { - if (!validate_set_value_ex(data, set->inner[i], sub_ref, ref_type->strct->args)) { - return 0; + if (!set->outer.set || set->outer.set->inner != set->inner) { + for (i = 0; i < set->count; ++i) { + decl_var *sub_var = set->inner[i]->vars->vars[0]; + decl_arg *sub_ref = locate_struct_member(ref_type->strct, sub_var); + + if (sub_ref) { + if (!validate_set_value_ex(data, set->inner[i], sub_ref, ref_type->strct->args)) { + return 0; + } } } } @@ -593,10 +505,9 @@ static inline int validate_set_value_ex(PSI_Data *data, set_value *set, decl_arg decl_var *sub_var = set->inner[0]->vars->vars[0]; decl_arg *sub_ref = locate_decl_var_arg(sub_var, ref_list, ref); - set->inner[0]->outer.set = set; if (sub_ref) { if (strcmp(sub_var->name, set_var->name)) { - data->error(E_WARNING, "Inner `set` statement casts on pointers must reference the same variable"); + data->error(sub_var->token, E_WARNING, "Inner `set` statement casts on pointers must reference the same variable"); return 0; } if (!validate_set_value_ex(data, set->inner[0], sub_ref, ref_list)) { @@ -604,7 +515,7 @@ static inline int validate_set_value_ex(PSI_Data *data, set_value *set, decl_arg } } } else if (set->count > 1) { - data->error(E_WARNING, "Inner `set` statement casts on pointers may only occur once"); + data->error(set->func->token, E_WARNING, "Inner `set` statement casts on pointers may only occur once"); return 0; } @@ -646,11 +557,13 @@ static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) { /* and which type cast to apply */ if (impl->stmts->ret.count != 1) { if (impl->stmts->ret.count > 1) { - data->error(PSI_WARNING, "Too many `return` statements for implmentation %s;" + data->error(impl->stmts->ret.list[1]->token, PSI_WARNING, + "Too many `return` statements for implmentation %s;" " found %zu, exactly one is needed", impl->func->name, impl->stmts->ret.count); } else { - data->error(PSI_WARNING, "Missing `return` statement for implementation %s", + data->error(impl->func->token, PSI_WARNING, + "Missing `return` statement for implementation %s", impl->func->name); } return 0; @@ -659,8 +572,9 @@ static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) { ret = impl->stmts->ret.list[0]; if (!(impl->decl = locate_impl_decl(data->decls, ret))) { - data->error(PSI_WARNING, "Missing declaration for implementation %s", - impl->func->name); + data->error(ret->token, PSI_WARNING, + "Missing declaration '%s' for `return` statment for implementation %s", + ret->set->vars->vars[0]->name, impl->func->name); return 0; } @@ -690,7 +604,7 @@ static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) { } if (!locate_decl_var_arg(let_var, impl->decl->args, impl->decl->func)) { - data->error(PSI_WARNING, "Unknown variable '%s' in `let` statement" + data->error(let_var->token, PSI_WARNING, "Unknown variable '%s' in `let` statement" " of implementation '%s'", let_var->name, impl->func->name); return 0; } @@ -737,7 +651,7 @@ static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) { } } if (!check) { - data->error(PSI_WARNING, "Unknown value '$%s' of `let` statement" + data->error(let->var->token, PSI_WARNING, "Unknown value '$%s' of `let` statement" " for variable '%s' of implementation '%s'", let->val->data.func->var->name, let->var->name, impl->func->name); return 0; @@ -761,7 +675,8 @@ static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) { } } if (!check) { - data->error(PSI_WARNING, "Missing `let` statement for arg '%s %.*s%s'" + data->error(impl->func->token, PSI_WARNING, + "Missing `let` statement for arg '%s %.*s%s'" " of declaration '%s' for implementation '%s'", darg->type->name, (int) darg->var->pointer_level, "*****", darg->var->name, impl->decl->func->var->name, impl->func->name); @@ -789,7 +704,7 @@ static inline int validate_impl_set_stmts(PSI_Data *data, impl *impl) { } } if (!check) { - data->error(PSI_WARNING, "Unknown variable '$%s' of `set` statement" + data->error(set->var->token, PSI_WARNING, "Unknown variable '$%s' of `set` statement" " of implementation '%s'", set->var->name, impl->func->name); return 0; @@ -832,7 +747,7 @@ static inline int validate_impl_set_stmts(PSI_Data *data, impl *impl) { } if (!check) { - data->error(PSI_WARNING, "Unknown value '%s' of `set` statement" + data->error(set_var->token, PSI_WARNING, "Unknown value '%s' of `set` statement" " for variable '$%s' of implementation '%s'", set_var->name, set->arg->var->name, impl->func->name); return 0; @@ -866,33 +781,38 @@ static inline int validate_impl_free_stmts(PSI_Data *data, impl *impl) { /* first find the decl of the free func */ if (!locate_free_decl(data->decls, free_call)) { - data->error(PSI_WARNING, "Unknown function '%s' in `free` statement" - " of implementation '%s'", free_call->func, impl->func->name); + data->error(free_call->token, PSI_WARNING, + "Missing declaration '%s' in `free` statement" + " of implementation '%s'", + free_call->func, impl->func->name); return 0; } - if (!impl->decl->args) { - data->error(PSI_WARNING, "Declaration '%s' of implementation '%s'" - " does not have any arguments to free", - impl->decl->func->var->name, impl->func->name); - } + + /* now check for known vars */ for (l = 0; l < free_call->vars->count; ++l) { int check = 0; decl_var *free_var = free_call->vars->vars[l]; - for (k = 0; k < impl->decl->args->count; ++k) { - decl_arg *free_arg = impl->decl->args->args[k]; + if (!strcmp(free_var->name, impl->decl->func->var->name)) { + check = 1; + free_var->arg = impl->decl->func; + } else if (impl->decl->args) { + for (k = 0; k < impl->decl->args->count; ++k) { + decl_arg *free_arg = impl->decl->args->args[k]; - if (!strcmp(free_var->name, free_arg->var->name)) { - check = 1; - free_var->arg = free_arg; - break; + if (!strcmp(free_var->name, free_arg->var->name)) { + check = 1; + free_var->arg = free_arg; + break; + } } } if (!check) { - data->error(PSI_WARNING, "Unknown variable '%s' of `free` statement" + data->error(free_var->token, PSI_WARNING, + "Unknown variable '%s' of `free` statement" " of implementation '%s'", free_var->name, impl->func->name); return 0; @@ -904,7 +824,8 @@ static inline int validate_impl_free_stmts(PSI_Data *data, impl *impl) { } static inline int validate_impl_stmts(PSI_Data *data, impl *impl) { if (!impl->stmts) { - data->error(PSI_WARNING, "Missing body for implementation %s!", + data->error(impl->func->token, PSI_WARNING, + "Missing body for implementation %s!", impl->func->name); return 0; } @@ -926,6 +847,33 @@ static inline int validate_impl_stmts(PSI_Data *data, impl *impl) { return 1; } +static inline int validate_impl_args(PSI_Data *data, impl *impl) { + int def = 0; + size_t i; + + for (i = 0; i < impl->func->args->count; ++i) { + impl_arg *iarg = impl->func->args->args[i]; + + if (iarg->def) { + def = 1; + } else if (def) { + data->error(impl->func->token, PSI_WARNING, + "Non-optional argument %zu '$%s' of implementation '%s'" + " follows optional argument", + i+1, iarg->var->name, impl->func->name); + return 0; + } + } + + return 1; +} +static inline int validate_impl(PSI_Data *data, impl *impl) { + if (!validate_impl_args(data, impl)) { + return 0; + } + return validate_impl_stmts(data, impl); +} + PSI_Context *PSI_ContextInit(PSI_Context *C, PSI_ContextOps *ops, PSI_ContextErrorFunc error) { size_t i; @@ -1122,7 +1070,7 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P) size_t i; for (i = 0; i < D->impls->count; ++i) { - if (validate_impl_stmts(PSI_DATA(C), D->impls->list[i])) { + if (validate_impl(PSI_DATA(C), D->impls->list[i])) { C->impls = add_impl(C->impls, D->impls->list[i]); } } @@ -1161,11 +1109,11 @@ void PSI_ContextBuild(PSI_Context *C, const char *paths) PSI_Parser P; if (MAXPATHLEN <= slprintf(psi, MAXPATHLEN, "%s/%s", ptr, entries[i]->d_name)) { - C->error(PSI_WARNING, "Path to PSI file too long: %s/%s", + C->error(NULL, PSI_WARNING, "Path to PSI file too long: %s/%s", ptr, entries[i]->d_name); } if (!PSI_ParserInit(&P, psi, C->error, flags)) { - C->error(PSI_WARNING, "Failed to init PSI parser (%s): %s", + C->error(NULL, PSI_WARNING, "Failed to init PSI parser (%s): %s", psi, strerror(errno)); continue; } @@ -1195,7 +1143,7 @@ void PSI_ContextBuild(PSI_Context *C, const char *paths) if (PSI_ContextCompile(C) && SUCCESS != zend_register_functions(NULL, C->closures, NULL, MODULE_PERSISTENT)) { - C->error(PSI_WARNING, "Failed to register functions!"); + C->error(NULL, PSI_WARNING, "Failed to register functions!"); } free(cpy); @@ -1297,6 +1245,7 @@ static inline void dump_num_exp(int fd, num_exp *exp) { exp = exp->operand; } } + static inline void dump_impl_set_value(int fd, set_value *set, unsigned level) { size_t i; @@ -1304,7 +1253,12 @@ static inline void dump_impl_set_value(int fd, set_value *set, unsigned level) { /* only if not directly after `set ...` */ dump_level(fd, level); } - dprintf(fd, "%s(", set->func->name); + + if (set->func->type == PSI_T_ELLIPSIS) { + dprintf(fd, "%s(", set->outer.set->func->name); + } else { + dprintf(fd, "%s(", set->func->name); + } for (i = 0; i < set->vars->count; ++i) { decl_var *svar = set->vars->vars[i]; @@ -1313,11 +1267,15 @@ static inline void dump_impl_set_value(int fd, set_value *set, unsigned level) { } dump_decl_var(fd, svar); } + + if (set->func->type == PSI_T_ELLIPSIS) { + dprintf(fd, ", ..."); + } if (set->num) { dprintf(fd, ", "); dump_num_exp(fd, set->num); } - if (set->inner) { + if (set->inner && set->func->type != PSI_T_ELLIPSIS) { dprintf(fd, ",\n"); for (i = 0; i < set->count; ++i) { dump_impl_set_value(fd, set->inner[i], level+1); @@ -1353,7 +1311,8 @@ void PSI_ContextDump(PSI_Context *C, int fd) dprintf(fd, "typedef "); dump_decl_type(fd, tdef->type); - dprintf(fd, " %s;\n", tdef->alias); + dprintf(fd, " %s%s;\n", tdef->type->type == PSI_T_POINTER ? "*":"", + tdef->alias); } dprintf(fd, "\n"); } @@ -1464,6 +1423,9 @@ void PSI_ContextDump(PSI_Context *C, int fd) dprintf(fd, "%s($%s)", let->val->data.func->name, let->val->data.func->var->name); break; + case PSI_LET_NUMEXP: + dump_num_exp(fd, let->val->data.num); + break; EMPTY_SWITCH_DEFAULT_CASE(); }