From 89dac4a1b8e063e832857a876015f664b55b3697 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sat, 16 Nov 2024 19:00:08 +0900 Subject: [PATCH] [qfcc] Create scopes for while and for loops in glsl I guess that was a mental FIXME for later, but later came sooner. The declaration doesn't seem to be working properly, but I'll worry about that later when I can get some automated tests going. --- tools/qfcc/include/expr.h | 3 ++- tools/qfcc/source/expr.c | 3 ++- tools/qfcc/source/expr_process.c | 4 ++-- tools/qfcc/source/glsl-parse.y | 27 +++++++++++++++------------ 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/tools/qfcc/include/expr.h b/tools/qfcc/include/expr.h index 59eee7d5f..62ea224db 100644 --- a/tools/qfcc/include/expr.h +++ b/tools/qfcc/include/expr.h @@ -346,6 +346,7 @@ typedef struct { typedef struct { specifier_t spec; ex_list_t list; + symtab_t *symtab; } ex_decl_t; typedef struct expr_s { @@ -923,7 +924,7 @@ expr_t *new_cond_expr (const expr_t *test, const expr_t *true_expr, const expr_t *false_expr); expr_t *new_field_expr (const expr_t *object, const expr_t *member); expr_t *new_array_expr (const expr_t *base, const expr_t *index); -expr_t *new_decl_expr (specifier_t spec); +expr_t *new_decl_expr (specifier_t spec, symtab_t *symtab); expr_t *append_decl (expr_t *decl, symbol_t *sym, const expr_t *init); expr_t *append_decl_list (expr_t *decl, const expr_t *list); diff --git a/tools/qfcc/source/expr.c b/tools/qfcc/source/expr.c index 6c5a3611f..77b203113 100644 --- a/tools/qfcc/source/expr.c +++ b/tools/qfcc/source/expr.c @@ -2204,12 +2204,13 @@ new_array_expr (const expr_t *base, const expr_t *index) } expr_t * -new_decl_expr (specifier_t spec) +new_decl_expr (specifier_t spec, symtab_t *symtab) { auto decl = new_expr (); decl->type = ex_decl; decl->decl = (ex_decl_t) { .spec = spec, + .symtab = symtab, }; return decl; } diff --git a/tools/qfcc/source/expr_process.c b/tools/qfcc/source/expr_process.c index 46a3abd97..36d52425b 100644 --- a/tools/qfcc/source/expr_process.c +++ b/tools/qfcc/source/expr_process.c @@ -351,8 +351,8 @@ proc_decl (const expr_t *expr) spec.type = find_type (spec.type); sym->type = nullptr; } - current_language.parse_declaration (spec, sym, init, current_symtab, - block); + auto symtab = expr->decl.symtab; + current_language.parse_declaration (spec, sym, init, symtab, block); } return block; } diff --git a/tools/qfcc/source/glsl-parse.y b/tools/qfcc/source/glsl-parse.y index eb00310ca..c3050202b 100644 --- a/tools/qfcc/source/glsl-parse.y +++ b/tools/qfcc/source/glsl-parse.y @@ -675,12 +675,12 @@ declaration } | type_qualifier new_identifier ';' { - auto decl = new_decl_expr ($type_qualifier); + auto decl = new_decl_expr ($type_qualifier, current_symtab); $$ = append_decl (decl, $new_identifier, nullptr); } | type_qualifier new_identifier identifier_list ';' { - auto decl = new_decl_expr ($type_qualifier); + auto decl = new_decl_expr ($type_qualifier, current_symtab); append_decl (decl, $new_identifier, nullptr); $$ = append_decl_list (decl, $identifier_list); } @@ -832,11 +832,11 @@ init_declarator_list single_declaration : fully_specified_type { - $$ = new_decl_expr ($fully_specified_type); + $$ = new_decl_expr ($fully_specified_type, current_symtab); } | fully_specified_type new_identifier { - auto decl = new_decl_expr ($fully_specified_type); + auto decl = new_decl_expr ($fully_specified_type, current_symtab); $$ = append_decl (decl, $new_identifier, nullptr); } | fully_specified_type new_identifier array_specifier @@ -844,7 +844,7 @@ single_declaration auto spec = $fully_specified_type; spec.type = append_type ($array_specifier, spec.type); spec.type = find_type (spec.type); - auto decl = new_decl_expr (spec); + auto decl = new_decl_expr (spec, current_symtab); $$ = append_decl (decl, $new_identifier, nullptr); } | fully_specified_type new_identifier array_specifier '=' initializer @@ -852,12 +852,12 @@ single_declaration auto spec = $fully_specified_type; spec.type = append_type ($array_specifier, spec.type); spec.type = find_type (spec.type); - auto decl = new_decl_expr (spec); + auto decl = new_decl_expr (spec, current_symtab); $$ = append_decl (decl, $new_identifier, $initializer); } | fully_specified_type new_identifier '=' initializer { - auto decl = new_decl_expr ($fully_specified_type); + auto decl = new_decl_expr ($fully_specified_type, current_symtab); $$ = append_decl (decl, $new_identifier, $initializer); } ; @@ -1132,7 +1132,7 @@ compound_statement | '{' new_scope statement_list '}' { $$ = $3; - current_symtab = $2->block.scope->parent; + current_symtab = $new_scope->block.scope->parent; } ; @@ -1150,7 +1150,7 @@ new_scope : /* empty */ { auto block = new_block_expr (nullptr); - block->block.scope = new_symtab (nullptr, stab_local); + block->block.scope = new_symtab (current_symtab, stab_local); current_symtab = block->block.scope; $$ = block; } @@ -1215,7 +1215,7 @@ condition : expression | fully_specified_type new_identifier '=' initializer { - auto decl = new_decl_expr ($fully_specified_type); + auto decl = new_decl_expr ($fully_specified_type, current_symtab); $$ = append_decl (decl, $new_identifier, $initializer); } ; @@ -1267,13 +1267,15 @@ case_label ; iteration_statement - : WHILE break_label continue_label '(' condition ')' statement_no_new_scope + : WHILE new_scope break_label continue_label '(' condition ')' + statement_no_new_scope { $$ = build_while_statement (false, $continue_label, $statement_no_new_scope, break_label, continue_label); break_label = $break_label; continue_label = $continue_label; + current_symtab = $new_scope->block.scope->parent; } | DO break_label continue_label statement WHILE '(' expression ')' ';' { @@ -1282,7 +1284,7 @@ iteration_statement break_label = $break_label; continue_label = $continue_label; } - | FOR break_label continue_label + | FOR new_scope break_label continue_label // '(' for_init_statement for_rest_statement ')' '(' for_init_statement conditionopt ';' expressionopt ')' statement_no_new_scope @@ -1296,6 +1298,7 @@ iteration_statement break_label, continue_label); break_label = $break_label; continue_label = $continue_label; + current_symtab = $new_scope->block.scope->parent; } ;