From b424442cf8a524365884c277484dde989f9a157e Mon Sep 17 00:00:00 2001 From: "Ondrej Zajicek (work)" Date: Thu, 3 Mar 2022 03:38:12 +0100 Subject: [PATCH] Filter: Implement type checks for function calls Keep list of function parameters in f_line and use it to verify types of arguments for function calls. Only static type checks are implemented. --- conf/confbase.Y | 1 + filter/config.Y | 36 ++++++++++++++++++++++++++---------- filter/f-inst.c | 13 +++++++++++++ filter/f-inst.h | 6 ++++++ 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/conf/confbase.Y b/conf/confbase.Y index 6985783b..aa35f882 100644 --- a/conf/confbase.Y +++ b/conf/confbase.Y @@ -75,6 +75,7 @@ CF_DECLS struct f_static_attr fsa; struct f_lval flv; struct f_line *fl; + struct f_arg *fa; const struct filter *f; struct f_tree *e; struct f_trie *trie; diff --git a/filter/config.Y b/filter/config.Y index 0ab32a57..a4b4d3d3 100644 --- a/filter/config.Y +++ b/filter/config.Y @@ -302,9 +302,10 @@ CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN, %type filter where_filter %type filter_body function_body %type lvalue -%type type function_args function_vars +%type type function_vars +%type function_argsn function_args %type ec_kind -%type break_command +%type break_command %type cnum %type pair_item ec_item lc_item set_item switch_item set_items switch_items switch_body %type fprefix_set @@ -403,18 +404,21 @@ type: ; function_argsn: - /* EMPTY */ + /* EMPTY */ { $$ = NULL; } | function_argsn type symbol ';' { if ($3->scope->slots >= 0xfe) cf_error("Too many declarations, at most 255 allowed"); - cf_define_symbol($3, SYM_VARIABLE | $2, offset, $3->scope->slots++); + $$ = cfg_alloc(sizeof(struct f_arg)); + $$->arg = cf_define_symbol($3, SYM_VARIABLE | $2, offset, $3->scope->slots++); + $$->next = $1; } ; function_args: - '(' ')' { $$ = 0; } + '(' ')' { $$ = NULL; } | '(' function_argsn type symbol ')' { - cf_define_symbol($4, SYM_VARIABLE | $3, offset, $4->scope->slots++); - $$ = $4->scope->slots; + $$ = cfg_alloc(sizeof(struct f_arg)); + $$->arg = cf_define_symbol($4, SYM_VARIABLE | $3, offset, $4->scope->slots++); + $$->next = $2; } ; @@ -459,9 +463,21 @@ function_def: FUNCTION symbol { DBG( "Beginning of function %s\n", $2->name ); $2 = cf_define_symbol($2, SYM_FUNCTION, function, NULL); cf_push_scope($2); - } function_args function_body { - DBG("Definition of function %s with %u args and %u local vars.\n", $2->name, $4, $5->vars); - $5->args = $4; + } function_args function_body + { + $5->arg_list = NULL; + $5->args = 0; + + /* Revert the args */ + while ($4) { + struct f_arg *tmp = $4; + $4 = $4->next; + + tmp->next = $5->arg_list; + $5->arg_list = tmp; + $5->args++; + } + $2->function = $5; cf_pop_scope(); } diff --git a/filter/f-inst.c b/filter/f-inst.c index 0d935859..e9bae684 100644 --- a/filter/f-inst.c +++ b/filter/f-inst.c @@ -1089,6 +1089,19 @@ cf_error("Function '%s' expects %u arguments, got %u arguments", sym->name, sym->function->args, whati->varcount); + /* Typecheck individual arguments */ + struct f_inst *a = fvar; + struct f_arg *b = sym->function->arg_list; + for (uint i = 1; a && b; a = a->next, b = b->next, i++) + { + enum f_type b_type = b->arg->class & 0xff; + + if (a->type && (a->type != b_type) && !f_const_promotion(a, b_type)) + cf_error("Argument %u of '%s' must be %s, got %s", + i, sym->name, f_type_name(b_type), f_type_name(a->type)); + } + ASSERT(!a && !b); + /* Add implicit void slot for the return value */ struct f_inst *tmp = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_VOID }); tmp->next = whati->fvar; diff --git a/filter/f-inst.h b/filter/f-inst.h index df45f88e..12df40f6 100644 --- a/filter/f-inst.h +++ b/filter/f-inst.h @@ -35,12 +35,18 @@ const char *f_instruction_name_(enum f_instruction_code fi); static inline const char *f_instruction_name(enum f_instruction_code fi) { return f_instruction_name_(fi) + 3; } +struct f_arg { + struct symbol *arg; + struct f_arg *next; +}; + /* Filter structures for execution */ /* Line of instructions to be unconditionally executed one after another */ struct f_line { uint len; /* Line length */ u8 args; /* Function: Args required */ u8 vars; + struct f_arg *arg_list; struct f_line_item items[0]; /* The items themselves */ };