X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Fcontext.c;h=3c86f992323bd821dd11a1411992d1c72e859d8a;hp=44f82992e6e0ee1f739ea2aaa11aec137fc5f4ca;hb=974c9580535a1668dd6eed9992d582ebcc29b27a;hpb=4b5ceb304b924c50eaf8263ad394493ff806fed4 diff --git a/src/context.c b/src/context.c index 44f8299..3c86f99 100644 --- a/src/context.c +++ b/src/context.c @@ -150,10 +150,24 @@ static struct psi_predef_const { 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} }; @@ -397,7 +411,7 @@ static inline int validate_decl(PSI_Data *data, void *dl, decl *decl) { } return 1; } -static inline decl_arg *locate_decl_var_arg(decl_var *var, decl_args *args) { +static inline decl_arg *locate_decl_var_arg(decl_var *var, decl_args *args, decl_arg *func) { size_t i; for (i = 0; i < args->count; ++i) { @@ -408,21 +422,76 @@ static inline decl_arg *locate_decl_var_arg(decl_var *var, decl_args *args) { return var->arg = arg; } } + if (func && !strcmp(var->name, func->var->name)) { + return var->arg = func; + } return NULL; } static inline decl_arg *locate_struct_member(decl_struct *s, decl_var *var) { if (s->args) { - return locate_decl_var_arg(var, s->args); + return locate_decl_var_arg(var, s->args, NULL); } return NULL; } -static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *ref, decl_args *ref_list) { +static inline constant *locate_num_exp_constant(num_exp *exp, constants *consts) { size_t i; - decl_type *ref_type = real_decl_type(ref->type); - decl_var *set_var = set->vars->vars[0]; + for (i = 0; i < consts->count; ++i) { + constant *cnst = consts->list[i]; + + if (!strcmp(cnst->name, exp->u.numb)) { + free(exp->u.numb); + return exp->u.cnst = cnst; + } + } + + return NULL; +} +static inline int validate_num_exp(PSI_Data *data, decl_args *dargs, decl_arg *func, num_exp *exp) { + if (exp->operand) { + switch (exp->operator) { + case PSI_T_PLUS: + exp->calculator = psi_calc_add; + break; + case PSI_T_MINUS: + exp->calculator = psi_calc_sub; + break; + case PSI_T_ASTERISK: + exp->calculator = psi_calc_mul; + break; + case PSI_T_SLASH: + exp->calculator = psi_calc_div; + break; + EMPTY_SWITCH_DEFAULT_CASE(); + } + if (!validate_num_exp(data, dargs, func, exp->operand)) { + return 0; + } + } + 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", + 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", + exp->u.numb); + return 0; + } + return 1; + case PSI_T_NUMBER: + return 1; + default: + return 0; + } +} +static inline int validate_set_value_handler(set_value *set) { switch (set->func->type) { case PSI_T_TO_BOOL: set->func->handler = psi_to_bool; @@ -439,25 +508,67 @@ static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *r case PSI_T_TO_ARRAY: set->func->handler = psi_to_array; break; - EMPTY_SWITCH_DEFAULT_CASE(); + case PSI_T_TO_OBJECT: + set->func->handler = psi_to_object; + break; + case PSI_T_VOID: + set->func->handler = psi_to_void; + break; + default: + return 0; } + return 1; +} +static inline void decl_var_arg_v(decl_args *args, va_list argp) { + int argc; + decl_arg **argv; - for (i = 1; i < set->vars->count; ++i) { - if (!locate_decl_var_arg(set->vars->vars[i], ref_list)) { + memset(args, 0, sizeof(*args)); + + while ((argc = va_arg(argp, int))) { + argv = va_arg(argp, decl_arg **); + while (argc--) { + add_decl_arg(args, *argv++); + } + } +} +static inline int validate_set_value_ex(PSI_Data *data, set_value *set, decl_arg *ref, decl_args *ref_list) { + size_t i; + decl_type *ref_type; + decl_var *set_var = set->vars->vars[0]; + + if (!validate_set_value_handler(set)) { + data->error(PSI_WARNING, "Invalid cast '%s'", set->func->name); + return 0; + } + + for (i = 0; i < set->vars->count; ++i) { + if (!locate_decl_var_arg(set->vars->vars[i], ref_list, NULL)) { + data->error(PSI_WARNING, "Unknown variable '%s'", set->vars->vars[i]->name); return 0; } } - set_var->arg = ref; + + if (!ref) { + ref = set_var->arg; + } + ref_type = real_decl_type(ref->type); if (set->count) { int is_to_array = (set->func->type == PSI_T_TO_ARRAY); int is_pointer_to_struct = (ref_type->type == PSI_T_STRUCT && ref->var->pointer_level); - if (is_to_array && !is_pointer_to_struct) { + + if (!is_to_array && !is_pointer_to_struct) { data->error(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; } } + if (set->num) { + if (!validate_num_exp(data, ref_list, ref, set->num)) { + return 0; + } + } if (ref_type->type == PSI_T_STRUCT) { /* to_array(struct, to_...) */ @@ -467,7 +578,7 @@ static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *r set->inner[i]->outer.set = set; if (sub_ref) { - if (!validate_set_value(data, set->inner[i], sub_ref, ref_type->strct->args)) { + if (!validate_set_value_ex(data, set->inner[i], sub_ref, ref_type->strct->args)) { return 0; } } @@ -475,7 +586,7 @@ static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *r } else if (set->count == 1) { /* to_array(ptr, to_string(*ptr)) */ decl_var *sub_var = set->inner[0]->vars->vars[0]; - decl_arg *sub_ref = locate_decl_var_arg(sub_var, ref_list); + decl_arg *sub_ref = locate_decl_var_arg(sub_var, ref_list, ref); set->inner[0]->outer.set = set; if (sub_ref) { @@ -483,7 +594,7 @@ static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *r data->error(E_WARNING, "Inner `set` statement casts on pointers must reference the same variable"); return 0; } - if (!validate_set_value(data, set->inner[0], sub_ref, ref_list)) { + if (!validate_set_value_ex(data, set->inner[0], sub_ref, ref_list)) { return 0; } } @@ -494,6 +605,21 @@ static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *r return 1; } +static inline int validate_set_value(PSI_Data *data, set_value *set, ...) { + va_list argp; + decl_args args = {0}; + int check; + + va_start(argp, set); + decl_var_arg_v(&args, argp); + va_end(argp); + + check = validate_set_value_ex(data, set, NULL, &args); + if (args.args) { + free(args.args); + } + return check; +} static inline decl *locate_impl_decl(decls *decls, return_stmt *ret) { if (decls) { size_t i; @@ -533,7 +659,7 @@ static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) { return 0; } - if (!validate_set_value(data, ret->set, ret->decl, impl->decl->args)) { + if (!validate_set_value(data, ret->set, 1, &ret->decl, impl->decl->args ? (int) impl->decl->args->count : 0, impl->decl->args ? impl->decl->args->args : NULL, 0)) { return 0; } @@ -541,9 +667,21 @@ static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) { return 1; } + static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) { size_t i, j; /* we can have multiple let stmts */ + /* check that we have a decl arg for every let stmt */ + for (i = 0; i < impl->stmts->let.count; ++i) { + let_stmt *let = impl->stmts->let.list[i]; + + if (!locate_decl_var_arg(let->var, impl->decl->args, impl->decl->func)) { + data->error(PSI_WARNING, "Unknown variable '%s' in `let` statement" + " of implementation '%s'", let->var->name, impl->func->name); + return 0; + } + } + /* check that we have a let stmt for every decl arg */ if (impl->decl->args) for (i = 0; i < impl->decl->args->count; ++i) { decl_arg *darg = impl->decl->args->args[i]; @@ -572,9 +710,10 @@ static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) { int check = 0; if (let->val && let->val->func && let->val->func->alloc) { - if (!validate_decl_type(data, let->val->func->alloc->type)) { - data->error(PSI_WARNING, "Cannot use '%s' as type for calloc in `let` statement", - let->val->func->alloc->type->name); + if (!validate_num_exp(data, impl->decl->args, impl->decl->func, let->val->func->alloc->nmemb)) { + return 0; + } + if (!validate_num_exp(data, impl->decl->args, impl->decl->func, let->val->func->alloc->size)) { return 0; } } @@ -632,7 +771,7 @@ static inline int validate_impl_set_stmts(PSI_Data *data, impl *impl) { if (!strcmp(set_var->name, set_arg->var->name)) { check = 1; set_var->arg = set_arg; - if (!validate_set_value(data, set->val, set_arg, impl->decl->args)) { + if (!validate_set_value(data, set->val, 1, &set_arg, 1, &impl->decl->func, impl->decl->args->count, impl->decl->args->args, 0)) { return 0; } break; @@ -959,9 +1098,13 @@ void PSI_ContextBuild(PSI_Context *C, const char *paths) continue; } - while (-1 != PSI_ParserScan(&P)) { + while (0 < PSI_ParserScan(&P)) { PSI_ParserParse(&P, PSI_TokenAlloc(&P)); - }; + if (P.num == PSI_T_EOF) { + break; + } + } + PSI_ParserParse(&P, NULL); PSI_ContextValidate(C, &P); PSI_ParserDtor(&P); @@ -1053,6 +1196,35 @@ static inline void dump_decl_arg(int fd, decl_arg *a) { static inline void dump_level(int fd, unsigned level) { dprintf(fd, "%.*s", level, "\t\t\t\t\t\t\t\t\t"); } +static inline void dump_num_exp(int fd, num_exp *exp) { + while (exp) { + switch (exp->t) { + case PSI_T_NUMBER: + dprintf(fd, "%s", exp->u.numb); + break; + case PSI_T_NSNAME: + dprintf(fd, "%s", exp->u.cnst->name); + break; + case PSI_T_NAME: + dump_decl_var(fd, exp->u.dvar); + break; + EMPTY_SWITCH_DEFAULT_CASE(); + } + if (exp->operand) { + char op; + + switch (exp->operator) { + case PSI_T_PLUS: op = '+'; break; + case PSI_T_MINUS: op = '-'; break; + case PSI_T_ASTERISK:op = '*'; break; + case PSI_T_SLASH: op = '/'; break; + EMPTY_SWITCH_DEFAULT_CASE(); + } + dprintf(fd, " %c ", op); + } + exp = exp->operand; + } +} static inline void dump_impl_set_value(int fd, set_value *set, unsigned level) { size_t i; @@ -1069,6 +1241,10 @@ static inline void dump_impl_set_value(int fd, set_value *set, unsigned level) { } dump_decl_var(fd, svar); } + if (set->num) { + dprintf(fd, ", "); + dump_num_exp(fd, set->num); + } if (set->inner) { dprintf(fd, ",\n"); for (i = 0; i < set->count; ++i) { @@ -1186,8 +1362,9 @@ void PSI_ContextDump(PSI_Context *C, int fd) if (let->val->func) { dprintf(fd, "%s(", let->val->func->name); if (let->val->func->alloc) { - dprintf(fd, "%zu, ", let->val->func->alloc->n); - dump_decl_type(fd, let->val->func->alloc->type); + dump_num_exp(fd, let->val->func->alloc->nmemb); + dprintf(fd, ", "); + dump_num_exp(fd, let->val->func->alloc->size); } else { dprintf(fd, "$%s", let->val->var->name); }