]> git.tdb.fi Git - libs/gl.git/commitdiff
Rearrange expression node replacement
authorMikko Rasa <tdb@tdb.fi>
Sat, 6 Mar 2021 12:27:16 +0000 (14:27 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 6 Mar 2021 12:27:16 +0000 (14:27 +0200)
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
source/glsl/generate.h
source/glsl/optimize.cpp

index 610aa390964faa9ea91dc5fdc06695a0ea318d0c..b7ab9d3f7b9fc956c37752f18e7e5bb36df0e0d3 100644 (file)
@@ -238,6 +238,15 @@ void VariableResolver::enter(Block &block)
        block.variables.clear();
 }
 
+void VariableResolver::visit_and_replace(RefPtr<Expression> &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<string, VariableDeclaration *> *members = 0;
        if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(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<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
+               visit_and_replace(*i);
 }
 
 void VariableResolver::visit(VariableDeclaration &var)
index 3d175b42dceda80c71c5052bfbdba0862ea1a4cd..9365c6c3490fec2fb6290769d2083d1bec22bf8f 100644 (file)
@@ -85,7 +85,7 @@ class VariableResolver: private TraversingVisitor
 {
 private:
        Stage *stage;
-       RefPtr<InterfaceBlockReference> r_iface_ref;
+       RefPtr<Expression> 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<Expression> &);
        virtual void visit(VariableReference &);
        virtual void visit(InterfaceBlockReference &);
        virtual void visit(MemberAccess &);
index 1aad8d1b311adcecf27904b8e29efb49892d98a6..f938314814ea9cd4211279d5e94ff817c2a92fcc 100644 (file)
@@ -202,6 +202,7 @@ void FunctionInliner::visit_and_inline(RefPtr<Expression> &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<Expression> &ptr, const Operator
                        r_ref_info->inline_on_rhs = on_rhs;
                }
        }
+       r_ref_info = 0;
 }
 
 void ExpressionInliner::inline_expression(Expression &expr, RefPtr<Expression> &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<Expression>::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;
 }