X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Fcontext_validate.c;h=56d7b2ea43eab6caad086c2cdd1dea03be010b09;hp=826a6b3cc1bc947408bf77c64702d47c49b50066;hb=8b652242a4ef920f2ab82bfb822596de25bc5a63;hpb=69fb17bcfa5ed9c32754fa143b59f4a4a2dd4bd8 diff --git a/src/context_validate.c b/src/context_validate.c index 826a6b3..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; } @@ -207,17 +210,26 @@ static inline int validate_decl_struct_darg(PSI_Data *data, decl_arg *darg, void /* pre-validate any structs/unions/enums */ switch (real->type) { case PSI_T_STRUCT: - if ((current && current == real->strct) || !validate_decl_struct(data, real->strct)) { + 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) || !validate_decl_union(data, real->unn)) { + 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) || !validate_decl_enum(data, real->enm)) { + if (current && current == real->enm) { + return 1; + } + if (!validate_decl_enum(data, real->enm)) { return 0; } break; @@ -709,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 */ @@ -754,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); @@ -772,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) { @@ -794,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; } @@ -871,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 */ @@ -922,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; @@ -1167,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; @@ -1190,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; @@ -1208,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]); @@ -1218,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) {