X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Fcontext_validate.c;h=56d7b2ea43eab6caad086c2cdd1dea03be010b09;hp=12befca821bf12da20baf7319a618622206961de;hb=8b652242a4ef920f2ab82bfb822596de25bc5a63;hpb=ef5079cd02c9d8666f6b9336853d2ab393e33467 diff --git a/src/context_validate.c b/src/context_validate.c index 12befca..56d7b2e 100644 --- a/src/context_validate.c +++ b/src/context_validate.c @@ -1,4 +1,4 @@ - #ifdef HAVE_CONFIG_H +#ifdef HAVE_CONFIG_H # include "config.h" #endif @@ -154,6 +154,9 @@ static inline int validate_decl_typedef(PSI_Data *data, decl_arg *def) { def->var->name); return 0; } + if (def->type->type == PSI_T_VOID && def->var->pointer_level) { + def->type->type = PSI_T_POINTER; + } return 1; } @@ -201,22 +204,31 @@ static inline void psi_sort_struct_args(void **args, size_t count) { psi_sort_struct_arg_cmp, psi_sort_struct_arg_swp); } -static inline int validate_decl_struct_darg(PSI_Data *data, decl_arg *darg) { +static inline int validate_decl_struct_darg(PSI_Data *data, decl_arg *darg, void *current) { decl_type *real = real_decl_type(darg->type); /* pre-validate any structs/unions/enums */ switch (real->type) { case PSI_T_STRUCT: + if (current && current == real->strct) { + return 1; + } if (!validate_decl_struct(data, real->strct)) { return 0; } break; case PSI_T_UNION: + if (current && current == real->unn) { + return 1; + } if (!validate_decl_union(data, real->unn)) { return 0; } break; case PSI_T_ENUM: + if (current && current == real->enm) { + return 1; + } if (!validate_decl_enum(data, real->enm)) { return 0; } @@ -253,30 +265,81 @@ static inline size_t sizeof_decl_arg(decl_arg *darg) { } } + ZEND_ASSERT(size); + return size; } -static inline size_t align_decl_arg(decl_arg *darg, size_t *pos, size_t *len) { +static inline size_t alignof_decl_type(decl_type *t); +static inline size_t alignof_decl_arg(decl_arg *darg); +static inline size_t alignof_decl_union(decl_union *u); +static inline size_t alignof_decl_struct(decl_struct *s); + +static inline size_t alignof_decl_args(decl_args *args) { + size_t i, maxalign = 0; + + for (i = 0; i < args->count; ++i) { + decl_arg *darg = args->args[i]; + size_t align = alignof_decl_arg(darg); + + if (align > maxalign) { + maxalign = align; + } + } + + return maxalign; +} + +static inline size_t alignof_decl_struct(decl_struct *s) { + if (!s->align) { + s->align = alignof_decl_args(s->args); + } + return s->align; +} + +static inline size_t alignof_decl_union(decl_union *u) { + if (!u->align) { + u->align = alignof_decl_args(u->args); + } + return u->align; +} + +static inline size_t alignof_decl_type(decl_type *t) { + decl_type *real = real_decl_type(t); + size_t align; + + switch (real->type) { + case PSI_T_STRUCT: + align = alignof_decl_struct(real->strct); + break; + case PSI_T_UNION: + align = alignof_decl_union(real->unn); + break; + case PSI_T_ENUM: + default: + align = psi_t_alignment(real->type); + } + + return align; +} + +static inline size_t alignof_decl_arg(decl_arg *darg) { size_t align; if (darg->var->pointer_level && (!darg->var->array_size || darg->var->pointer_level > 2)) { align = psi_t_alignment(PSI_T_POINTER); } else { - decl_type *real = real_decl_type(darg->type); - - switch (real->type) { - case PSI_T_STRUCT: - align = real->strct->align; - break; - case PSI_T_UNION: - align = real->unn->align; - break; - default: - align = psi_t_alignment(real->type); - break; - } + align = alignof_decl_type(darg->type); } + return align; +} + +static inline size_t align_decl_arg(decl_arg *darg, size_t *pos, size_t *len) { + size_t align = alignof_decl_arg(darg); + + ZEND_ASSERT(align); + *len = sizeof_decl_arg(darg); *pos = psi_align(align, *pos); @@ -303,7 +366,9 @@ static inline int validate_decl_struct(PSI_Data *data, decl_struct *s) { ZEND_ASSERT(!darg->var->arg || darg->var->arg == darg); darg->var->arg = darg; - if (darg->layout) { + if (!validate_decl_struct_darg(data, darg, s)) { + return 0; + } else if (darg->layout) { pos = darg->layout->pos; align = align_decl_arg(darg, &pos, &len); @@ -312,20 +377,16 @@ static inline int validate_decl_struct(PSI_Data *data, 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'", - darg->layout->len, s->name, darg->var->name, len, + len, s->name, darg->var->name, darg->layout->len, darg->type->name); - return 0; } 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", - darg->layout->len, s->name, darg->var->name, len); - return 0; + pos, s->name, darg->var->name, darg->layout->pos); } - } else if (!validate_decl_struct_darg(data, darg)) { - return 0; - } else { + } else { if (i) { pos = s->args->args[i-1]->layout->pos + s->args->args[i-1]->layout->len; @@ -357,7 +418,7 @@ static inline int validate_decl_struct(PSI_Data *data, decl_struct *s) { } static inline int validate_decl_union(PSI_Data *data, decl_union *u) { - size_t i, pos, len, size, align; + size_t i, pos, len, size = 0, align; if (!u->size && !u->args->count) { data->error(data, u->token, PSI_WARNING, @@ -376,27 +437,26 @@ static inline int validate_decl_union(PSI_Data *data, decl_union *u) { ZEND_ASSERT(!darg->var->arg || darg->var->arg == darg); darg->var->arg = darg; - if (darg->layout) { + if (!validate_decl_struct_darg(data, darg, u)) { + return 0; + } else if (darg->layout) { pos = darg->layout->pos; align = align_decl_arg(darg, &pos, &len); if (darg->layout->pos != 0) { data->error(data, darg->token, PSI_WARNING, - "Offset of %s.%s must be 0", + "Offset of %s.%s should be 0", u->name, darg->var->name); - return 0; + 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'", - darg->layout->len, u->name, darg->var->name, size, + len, u->name, darg->var->name, darg->layout->len, darg->type->name); - return 0; } - } else if (!validate_decl_struct_darg(data, darg)) { - return 0; } else { pos = 0; @@ -661,7 +721,6 @@ static inline int validate_set_value_handler(set_value *set) { 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 */ @@ -706,7 +765,7 @@ static inline int validate_set_value_ex(PSI_Data *data, set_value *set, decl_arg } ref_type = real_decl_type(ref->type); - if (set->count) { + if (set->inner && set->inner->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); @@ -724,21 +783,21 @@ 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_...) */ - 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]; + if (!set->outer.set || set->outer.set->inner->vals != set->inner->vals) { + for (i = 0; i < set->inner->count; ++i) { + decl_var *sub_var = set->inner->vals[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)) { + if (!validate_set_value_ex(data, set->inner->vals[i], sub_ref, ref_type->strct->args)) { return 0; } } } } - } else if (set->count == 1) { + } else if (set->inner && set->inner->count == 1) { /* to_array(ptr, to_string(*ptr)) */ - decl_var *sub_var = set->inner[0]->vars->vars[0]; + decl_var *sub_var = set->inner->vals[0]->vars->vars[0]; decl_arg *sub_ref = locate_decl_var_arg(sub_var, ref_list, ref); if (sub_ref) { @@ -746,11 +805,11 @@ static inline int validate_set_value_ex(PSI_Data *data, set_value *set, decl_arg data->error(data, 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)) { + if (!validate_set_value_ex(data, set->inner->vals[0], sub_ref, ref_list)) { return 0; } } - } else if (set->count > 1) { + } else if (set->inner && set->inner->count > 1) { data->error(data, set->func->token, E_WARNING, "Inner `set` statement casts on pointers may only occur once"); return 0; } @@ -823,6 +882,52 @@ static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) { return 1; } +static inline int validate_let_func(PSI_Data *data, let_func *func, impl *impl) { + int check = 0; + size_t j; + + if (impl->func->args) { + for (j = 0; j < impl->func->args->count; ++j) { + impl_arg *iarg = impl->func->args->args[j]; + + if (!strcmp(func->var->name, iarg->var->name)) { + func->arg = iarg; + check = 1; + break; + } + } + } + if (!check) { + data->error(data, func->var->token, PSI_WARNING, "Unknown value '$%s' of `let` statement" + " for cast '%s' of implementation '%s'", + func->var->name, func->name, impl->func->name); + return 0; + } + return 1; +} + +static inline int validate_let_callback(PSI_Data *data, decl_var *let_var, let_callback *cb, impl *impl) { + decl_type *cb_type; + decl *cb_func; + size_t i; + + if (!validate_let_func(data, cb->func, impl)) { + return 0; + } + + cb_type = real_decl_type(let_var->arg->type); + if (cb_type->type != PSI_T_FUNCTION) { + data->error(data, let_var->token, PSI_WARNING, "Not a function: %s", let_var->name); + return 0; + } + cb_func = cb_type->func; + for (i = 0; i < cb->args->count; ++i) { + if (!validate_set_value(data, cb->args->vals[i], cb_func->args->count, cb_func->args->args, 0)) { + return 0; + } + } +} + static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) { size_t i, j; /* we can have multiple let stmts */ @@ -874,22 +979,13 @@ static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) { return 0; } break; - case PSI_LET_FUNC: - if (impl->func->args) { - for (j = 0; j < impl->func->args->count; ++j) { - impl_arg *iarg = impl->func->args->args[j]; - - if (!strcmp(let->val->data.func->var->name, iarg->var->name)) { - let->val->data.func->arg = iarg; - check = 1; - break; - } - } + case PSI_LET_CALLBACK: + if (!validate_let_callback(data, let->var, let->val->data.callback, impl)) { + return 0; } - if (!check) { - data->error(data, 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); + break; + case PSI_LET_FUNC: + if (!validate_let_func(data, let->val->data.func, impl)) { return 0; } break; @@ -1119,6 +1215,7 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P) size_t i, count = C->count++, check_round, check_count; decl_typedefs *check_defs = P->defs; decl_structs *check_structs = P->structs; + decl_unions *check_unions = P->unions; decl_enums *check_enums = P->enums; unsigned silent = C->flags & PSI_PARSER_SILENT; @@ -1142,6 +1239,7 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P) for (check_round = 0, check_count = 0; CHECK_TOTAL && check_count != CHECK_TOTAL; ++check_round) { decl_typedefs *recheck_defs = NULL; decl_structs *recheck_structs = NULL; + decl_unions *recheck_unions = NULL; decl_enums *recheck_enums = NULL; check_count = CHECK_TOTAL; @@ -1160,6 +1258,13 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P) recheck_structs = add_decl_struct(recheck_structs, check_structs->list[i]); } } + for (i = 0; i < CHECK_COUNT(unions); ++i) { + if (validate_decl_union(PSI_DATA(C), check_unions->list[i])) { + C->unions = add_decl_union(C->unions, check_unions->list[i]); + } else { + recheck_unions = add_decl_union(recheck_unions, check_unions->list[i]); + } + } for (i = 0; i < CHECK_COUNT(enums); ++i) { if (validate_decl_enum(PSI_DATA(C), check_enums->list[i])) { C->enums = add_decl_enum(C->enums, check_enums->list[i]); @@ -1170,6 +1275,7 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P) REVALIDATE(defs); REVALIDATE(structs); + REVALIDATE(unions); REVALIDATE(enums); if (check_round == 0 && !silent) {