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.
This commit is contained in:
parent
4c0c507b1f
commit
93d6096c87
4 changed files with 46 additions and 10 deletions
|
@ -75,6 +75,7 @@ CF_DECLS
|
||||||
struct f_static_attr fsa;
|
struct f_static_attr fsa;
|
||||||
struct f_lval flv;
|
struct f_lval flv;
|
||||||
struct f_line *fl;
|
struct f_line *fl;
|
||||||
|
struct f_arg *fa;
|
||||||
const struct filter *f;
|
const struct filter *f;
|
||||||
struct f_tree *e;
|
struct f_tree *e;
|
||||||
struct f_trie *trie;
|
struct f_trie *trie;
|
||||||
|
|
|
@ -302,9 +302,10 @@ CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN,
|
||||||
%type <f> filter where_filter
|
%type <f> filter where_filter
|
||||||
%type <fl> filter_body function_body
|
%type <fl> filter_body function_body
|
||||||
%type <flv> lvalue
|
%type <flv> lvalue
|
||||||
%type <i> type function_args function_vars
|
%type <i> type function_vars
|
||||||
|
%type <fa> function_argsn function_args
|
||||||
%type <ecs> ec_kind
|
%type <ecs> ec_kind
|
||||||
%type <fret> break_command
|
%type <fret> break_command
|
||||||
%type <i32> cnum
|
%type <i32> cnum
|
||||||
%type <e> pair_item ec_item lc_item set_item switch_item set_items switch_items switch_body
|
%type <e> pair_item ec_item lc_item set_item switch_item set_items switch_items switch_body
|
||||||
%type <trie> fprefix_set
|
%type <trie> fprefix_set
|
||||||
|
@ -403,18 +404,21 @@ type:
|
||||||
;
|
;
|
||||||
|
|
||||||
function_argsn:
|
function_argsn:
|
||||||
/* EMPTY */
|
/* EMPTY */ { $$ = NULL; }
|
||||||
| function_argsn type symbol ';' {
|
| function_argsn type symbol ';' {
|
||||||
if ($3->scope->slots >= 0xfe) cf_error("Too many declarations, at most 255 allowed");
|
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:
|
function_args:
|
||||||
'(' ')' { $$ = 0; }
|
'(' ')' { $$ = NULL; }
|
||||||
| '(' function_argsn type symbol ')' {
|
| '(' function_argsn type symbol ')' {
|
||||||
cf_define_symbol($4, SYM_VARIABLE | $3, offset, $4->scope->slots++);
|
$$ = cfg_alloc(sizeof(struct f_arg));
|
||||||
$$ = $4->scope->slots;
|
$$->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 );
|
FUNCTION symbol { DBG( "Beginning of function %s\n", $2->name );
|
||||||
$2 = cf_define_symbol($2, SYM_FUNCTION, function, NULL);
|
$2 = cf_define_symbol($2, SYM_FUNCTION, function, NULL);
|
||||||
cf_push_scope($2);
|
cf_push_scope($2);
|
||||||
} function_args function_body {
|
} 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;
|
$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;
|
$2->function = $5;
|
||||||
cf_pop_scope();
|
cf_pop_scope();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1089,6 +1089,19 @@
|
||||||
cf_error("Function '%s' expects %u arguments, got %u arguments",
|
cf_error("Function '%s' expects %u arguments, got %u arguments",
|
||||||
sym->name, sym->function->args, whati->varcount);
|
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 */
|
/* Add implicit void slot for the return value */
|
||||||
struct f_inst *tmp = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_VOID });
|
struct f_inst *tmp = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_VOID });
|
||||||
tmp->next = whati->fvar;
|
tmp->next = whati->fvar;
|
||||||
|
|
|
@ -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)
|
static inline const char *f_instruction_name(enum f_instruction_code fi)
|
||||||
{ return f_instruction_name_(fi) + 3; }
|
{ return f_instruction_name_(fi) + 3; }
|
||||||
|
|
||||||
|
struct f_arg {
|
||||||
|
struct symbol *arg;
|
||||||
|
struct f_arg *next;
|
||||||
|
};
|
||||||
|
|
||||||
/* Filter structures for execution */
|
/* Filter structures for execution */
|
||||||
/* Line of instructions to be unconditionally executed one after another */
|
/* Line of instructions to be unconditionally executed one after another */
|
||||||
struct f_line {
|
struct f_line {
|
||||||
uint len; /* Line length */
|
uint len; /* Line length */
|
||||||
u8 args; /* Function: Args required */
|
u8 args; /* Function: Args required */
|
||||||
u8 vars;
|
u8 vars;
|
||||||
|
struct f_arg *arg_list;
|
||||||
struct f_line_item items[0]; /* The items themselves */
|
struct f_line_item items[0]; /* The items themselves */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue