From cb8ee1f5ea24a5f7b561fc88d4ff5af3ae364038 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 6 Mar 2021 14:27:16 +0200 Subject: [PATCH] Rearrange expression node replacement Rather than every expression visiting function clearing the replacement result, clear it in the replacement function both before and after. This is more compact and less prone to forgetting it from somewhere. --- source/glsl/generate.cpp | 53 ++++++++++++++++++++-------------------- source/glsl/generate.h | 3 ++- source/glsl/optimize.cpp | 13 ++-------- 3 files changed, 31 insertions(+), 38 deletions(-) diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 610aa390..b7ab9d3f 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -238,6 +238,15 @@ void VariableResolver::enter(Block &block) block.variables.clear(); } +void VariableResolver::visit_and_replace(RefPtr &expr) +{ + r_replacement_expr = 0; + expr->visit(*this); + if(r_replacement_expr) + expr = r_replacement_expr; + r_replacement_expr = 0; +} + void VariableResolver::visit(VariableReference &var) { VariableDeclaration *declaration = 0; @@ -260,11 +269,12 @@ void VariableResolver::visit(VariableReference &var) { /* The name refers to an interface block with an instance name rather than a variable. Prepare a new syntax tree node accordingly. */ - r_iface_ref = new InterfaceBlockReference; - r_iface_ref->source = var.source; - r_iface_ref->line = var.line; - r_iface_ref->name = var.name; - r_iface_ref->declaration = i->second; + InterfaceBlockReference *iface_ref = new InterfaceBlockReference; + iface_ref->source = var.source; + iface_ref->line = var.line; + iface_ref->name = var.name; + iface_ref->declaration = i->second; + r_replacement_expr = iface_ref; } else { @@ -308,11 +318,7 @@ void VariableResolver::visit(InterfaceBlockReference &iface) void VariableResolver::visit(MemberAccess &memacc) { - r_iface_ref = 0; - memacc.left->visit(*this); - if(r_iface_ref) - memacc.left = r_iface_ref; - r_iface_ref = 0; + visit_and_replace(memacc.left); map *members = 0; if(StructDeclaration *strct = dynamic_cast(memacc.left->type)) @@ -337,8 +343,7 @@ void VariableResolver::visit(MemberAccess &memacc) void VariableResolver::visit(UnaryExpression &unary) { - TraversingVisitor::visit(unary); - r_iface_ref = 0; + visit_and_replace(unary.expression); } void VariableResolver::visit(BinaryExpression &binary) @@ -349,17 +354,15 @@ void VariableResolver::visit(BinaryExpression &binary) /* The subscript expression is not a part of the primary assignment target. */ SetFlag set(record_target, false); - binary.right->visit(*this); + visit_and_replace(binary.right); } - r_iface_ref = 0; - binary.left->visit(*this); - if(r_iface_ref) - binary.left = r_iface_ref; + visit_and_replace(binary.left); } else - TraversingVisitor::visit(binary); - - r_iface_ref = 0; + { + visit_and_replace(binary.left); + visit_and_replace(binary.right); + } } void VariableResolver::visit(Assignment &assign) @@ -367,22 +370,20 @@ void VariableResolver::visit(Assignment &assign) { SetFlag set(record_target); r_assignment_target = 0; - assign.left->visit(*this); + visit_and_replace(assign.left); r_any_resolved |= (r_assignment_target!=assign.target_declaration); assign.target_declaration = r_assignment_target; } r_self_referencing = false; - assign.right->visit(*this); + visit_and_replace(assign.right); assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='='); - - r_iface_ref = 0; } void VariableResolver::visit(FunctionCall &call) { - TraversingVisitor::visit(call); - r_iface_ref = 0; + for(NodeArray::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i) + visit_and_replace(*i); } void VariableResolver::visit(VariableDeclaration &var) diff --git a/source/glsl/generate.h b/source/glsl/generate.h index 3d175b42..9365c6c3 100644 --- a/source/glsl/generate.h +++ b/source/glsl/generate.h @@ -85,7 +85,7 @@ class VariableResolver: private TraversingVisitor { private: Stage *stage; - RefPtr r_iface_ref; + RefPtr r_replacement_expr; std::string block_interface; bool r_any_resolved; bool record_target; @@ -99,6 +99,7 @@ public: private: virtual void enter(Block &); + void visit_and_replace(RefPtr &); virtual void visit(VariableReference &); virtual void visit(InterfaceBlockReference &); virtual void visit(MemberAccess &); diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 1aad8d1b..f9383148 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -202,6 +202,7 @@ void FunctionInliner::visit_and_inline(RefPtr &ptr) ptr = r_inline_result; r_any_inlined = true; } + r_inline_result = 0; } void FunctionInliner::visit(Block &block) @@ -218,20 +219,17 @@ void FunctionInliner::visit(Block &block) void FunctionInliner::visit(UnaryExpression &unary) { visit_and_inline(unary.expression); - r_inline_result = 0; } void FunctionInliner::visit(BinaryExpression &binary) { visit_and_inline(binary.left); visit_and_inline(binary.right); - r_inline_result = 0; } void FunctionInliner::visit(MemberAccess &memacc) { visit_and_inline(memacc.left); - r_inline_result = 0; } void FunctionInliner::visit(FunctionCall &call) @@ -259,8 +257,6 @@ void FunctionInliner::visit(FunctionCall &call) inlined further. */ inlineable.erase(current_function); } - else - r_inline_result = 0; } void FunctionInliner::visit(ExpressionStatement &expr) @@ -272,7 +268,6 @@ void FunctionInliner::visit(VariableDeclaration &var) { if(var.init_expression) visit_and_inline(var.init_expression); - r_inline_result = 0; } void FunctionInliner::visit(FunctionDeclaration &func) @@ -364,6 +359,7 @@ void ExpressionInliner::visit_and_record(RefPtr &ptr, const Operator r_ref_info->inline_on_rhs = on_rhs; } } + r_ref_info = 0; } void ExpressionInliner::inline_expression(Expression &expr, RefPtr &ptr, const Operator *outer_oper, const Operator *inner_oper, bool on_rhs) @@ -439,7 +435,6 @@ void ExpressionInliner::visit(VariableReference &var) void ExpressionInliner::visit(MemberAccess &memacc) { visit_and_record(memacc.left, memacc.oper, false); - r_ref_info = 0; r_oper = memacc.oper; r_trivial = false; } @@ -448,7 +443,6 @@ void ExpressionInliner::visit(UnaryExpression &unary) { SetFlag set_target(mutating, mutating || unary.oper->token[1]=='+' || unary.oper->token[1]=='-'); visit_and_record(unary.expression, unary.oper, false); - r_ref_info = 0; r_oper = unary.oper; r_trivial = false; } @@ -460,7 +454,6 @@ void ExpressionInliner::visit(BinaryExpression &binary) SetFlag clear_target(mutating, false); visit_and_record(binary.right, binary.oper, true); } - r_ref_info = 0; r_oper = binary.oper; r_trivial = false; } @@ -489,7 +482,6 @@ void ExpressionInliner::visit(Assignment &assign) } } - r_ref_info = 0; r_oper = assign.oper; r_trivial = false; } @@ -498,7 +490,6 @@ void ExpressionInliner::visit(FunctionCall &call) { for(NodeArray::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i) visit_and_record(*i, 0, false); - r_ref_info = 0; r_oper = 0; r_trivial = false; } -- 2.45.2