X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Fcontext.c;h=fd315eefcbc413558bdc10a6e91ff1ec296a040a;hp=a75368eb108dc240741937bfc90b0b912c83bc8c;hb=b1720af16e5d7765e2effcd16e23f351c2f82e54;hpb=19f0eb5807fb8d0f3aa305f2476f094e66a02709 diff --git a/src/context.c b/src/context.c index a75368e..fd315ee 100644 --- a/src/context.c +++ b/src/context.c @@ -129,6 +129,15 @@ static struct psi_std_type { {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 struct psi_predef_type { token_t type_tag; const char *type_name; @@ -151,6 +160,12 @@ static struct psi_predef_const { PSI_MACROS +struct utsname *uname2() { + struct utsname *u = calloc(1, sizeof(*u)); + uname(u); + return u; +} + int psi_glob(const char *pattern, int flags, int (*errfunc) (const char *epath, int eerrno), glob_t *pglob) { @@ -169,6 +184,7 @@ static struct psi_func_redir { void (*func)(void); } psi_func_redirs[] = { {"glob", (void (*)(void)) psi_glob}, + {"uname2", (void (*)(void)) uname2}, PSI_REDIRS {0} }; @@ -200,7 +216,7 @@ static struct psi_predef_struct { PSI_STRUCTS {0} }; - +*/ static int validate_lib(PSI_Data *data, void **dlopened) { char lib[MAXPATHLEN]; const char *ptr = data->psi.file.ln; @@ -356,6 +372,7 @@ static const char * const abi_ccs[] = { "default", /* \ */ "extern", /* > - all the same */ "cdecl", /* / */ + "mscdecl", "stdcall", "fastcall", }; @@ -523,6 +540,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; } @@ -547,14 +572,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(set->func->token, 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(svar->token, PSI_WARNING, "Unknown variable '%s'", svar->name); + data->error(svar->token, PSI_WARNING, "Unknown variable '%s' in `set` statement", svar->name); return 0; } } @@ -582,14 +607,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; + } } } } @@ -598,7 +624,6 @@ 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(sub_var->token, E_WARNING, "Inner `set` statement casts on pointers must reference the same variable"); @@ -881,25 +906,26 @@ static inline int validate_impl_free_stmts(PSI_Data *data, impl *impl) { free_call->func, impl->func->name); return 0; } - if (!impl->decl->args) { - data->error(free_call->token, 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; + } } } @@ -940,6 +966,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; @@ -1136,7 +1189,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]); } } @@ -1311,6 +1364,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; @@ -1318,7 +1372,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]; @@ -1327,6 +1386,10 @@ 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);