From 2ef72f745dad8a79e617c66ee9c7cee6ba2211c4 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sat, 16 Nov 2024 20:14:51 +0900 Subject: [PATCH] [qfcc] Improve glsl scope handling While storing the scope in a block was the right idea, the implementation wasn't quite right as it resulted in the current scope getting set at the wrong times resulting in incorrect symbol access when shadowing was in effect. --- tools/qfcc/source/expr_process.c | 11 ++++------- tools/qfcc/source/glsl-parse.y | 7 ++++--- tools/qfcc/source/target_spirv.c | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/tools/qfcc/source/expr_process.c b/tools/qfcc/source/expr_process.c index a4b42c66a..994fcce5f 100644 --- a/tools/qfcc/source/expr_process.c +++ b/tools/qfcc/source/expr_process.c @@ -165,10 +165,8 @@ proc_label (const expr_t *expr) static const expr_t * proc_block (const expr_t *expr) { - if (expr->block.scope) { - expr->block.scope->parent = current_symtab; - current_symtab = expr->block.scope; - } + auto old_scope = current_symtab; + current_symtab = expr->block.scope; int count = list_count (&expr->block.list); int num_out = 0; const expr_t *result = nullptr; @@ -191,9 +189,8 @@ proc_block (const expr_t *expr) block->block.scope = expr->block.scope; block->block.result = result; block->block.is_call = expr->block.is_call; - if (expr->block.scope) { - current_symtab = current_symtab->parent; - } + + current_symtab = old_scope; return block; } diff --git a/tools/qfcc/source/glsl-parse.y b/tools/qfcc/source/glsl-parse.y index 644532fc5..e493b0831 100644 --- a/tools/qfcc/source/glsl-parse.y +++ b/tools/qfcc/source/glsl-parse.y @@ -186,7 +186,7 @@ int yylex (YYSTYPE *yylval, YYLTYPE *yylloc); %type condition %type compound_statement_no_new_scope compound_statement %type statement statement_no_new_scope statement_list -%type new_block new_scope +%type new_block new_scope %type simple_statement expression_statement %type declaration_statement selection_statement %type switch_statement switch_statement_list case_label @@ -1160,6 +1160,7 @@ new_block : /* empty */ { auto block = new_block_expr (nullptr); + block->block.scope = current_symtab; $$ = block; } ; @@ -1266,7 +1267,7 @@ case_label iteration_statement : WHILE new_scope break_label continue_label '(' condition ')' - statement_no_new_scope + { $$ = $new_scope; } statement_no_new_scope { $$ = new_loop_expr (false, false, $condition, $statement_no_new_scope, @@ -1286,7 +1287,7 @@ iteration_statement | FOR new_scope break_label continue_label // '(' for_init_statement for_rest_statement ')' '(' for_init_statement conditionopt ';' expressionopt ')' - statement_no_new_scope + { $$ = $new_scope; } statement_no_new_scope { auto loop = new_loop_expr (false, false, $conditionopt, $statement_no_new_scope, diff --git a/tools/qfcc/source/target_spirv.c b/tools/qfcc/source/target_spirv.c index 35decbdb6..9b72b3e88 100644 --- a/tools/qfcc/source/target_spirv.c +++ b/tools/qfcc/source/target_spirv.c @@ -1586,7 +1586,7 @@ spirv_declare_sym (specifier_t spec, const expr_t *init, symtab_t *symtab, symtab_addsymbol (symtab, sym); if (symtab->type == stab_local) { if (init) { - if (is_constexpr (init)) { + if (!block && is_constexpr (init)) { } else if (block) { auto r = pointer_deref (new_symbol_expr (sym)); auto e = assign_expr (r, init);