]> git.tdb.fi Git - libs/gl.git/commitdiff
Handle expression replacement through TraversingVisitor
authorMikko Rasa <tdb@tdb.fi>
Tue, 9 Mar 2021 10:05:29 +0000 (12:05 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 9 Mar 2021 10:05:29 +0000 (12:05 +0200)
There were several cases missing, like VariableResolver not handling
replacement of swizzles in return statements.  This way each of the
visitors can just concentrate on what they're actually doing.

source/glsl/generate.cpp
source/glsl/generate.h
source/glsl/optimize.cpp
source/glsl/optimize.h
source/glsl/visitor.cpp
source/glsl/visitor.h

index 8547152e45df9edb714f06ae8c2c83cccae40350..1f229991f7b2427e45b7fe511099dd2a894774c5 100644 (file)
@@ -274,7 +274,7 @@ void VariableResolver::enter(Block &block)
        block.variables.clear();
 }
 
-void VariableResolver::visit_and_replace(RefPtr<Expression> &expr)
+void VariableResolver::visit(RefPtr<Expression> &expr)
 {
        r_replacement_expr = 0;
        expr->visit(*this);
@@ -376,7 +376,7 @@ void VariableResolver::add_to_chain(Assignment::Target::ChainType type, unsigned
 
 void VariableResolver::visit(MemberAccess &memacc)
 {
-       visit_and_replace(memacc.left);
+       TraversingVisitor::visit(memacc);
 
        VariableDeclaration *declaration = 0;
        if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
@@ -430,7 +430,7 @@ void VariableResolver::visit(MemberAccess &memacc)
 
 void VariableResolver::visit(Swizzle &swizzle)
 {
-       visit_and_replace(swizzle.left);
+       TraversingVisitor::visit(swizzle);
 
        if(record_target)
        {
@@ -441,11 +441,6 @@ void VariableResolver::visit(Swizzle &swizzle)
        }
 }
 
-void VariableResolver::visit(UnaryExpression &unary)
-{
-       visit_and_replace(unary.expression);
-}
-
 void VariableResolver::visit(BinaryExpression &binary)
 {
        if(binary.oper->token[0]=='[')
@@ -454,9 +449,9 @@ void VariableResolver::visit(BinaryExpression &binary)
                        /* The subscript expression is not a part of the primary assignment
                        target. */
                        SetFlag set(record_target, false);
-                       visit_and_replace(binary.right);
+                       visit(binary.right);
                }
-               visit_and_replace(binary.left);
+               visit(binary.left);
 
                if(record_target)
                {
@@ -468,10 +463,7 @@ void VariableResolver::visit(BinaryExpression &binary)
                }
        }
        else
-       {
-               visit_and_replace(binary.left);
-               visit_and_replace(binary.right);
-       }
+               TraversingVisitor::visit(binary);
 }
 
 void VariableResolver::visit(Assignment &assign)
@@ -479,30 +471,19 @@ void VariableResolver::visit(Assignment &assign)
        {
                SetFlag set(record_target);
                r_assignment_target = Assignment::Target();
-               visit_and_replace(assign.left);
+               visit(assign.left);
                r_any_resolved |= (r_assignment_target<assign.target || assign.target<r_assignment_target);
                assign.target = r_assignment_target;
        }
 
        r_self_referencing = false;
-       visit_and_replace(assign.right);
+       visit(assign.right);
        assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
 }
 
-void VariableResolver::visit(FunctionCall &call)
-{
-       for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
-               visit_and_replace(*i);
-}
-
 void VariableResolver::visit(VariableDeclaration &var)
 {
-       if(var.layout)
-               var.layout->visit(*this);
-       if(var.array_size)
-               visit_and_replace(var.array_size);
-       if(var.init_expression)
-               visit_and_replace(var.init_expression);
+       TraversingVisitor::visit(var);
        current_block->variables.insert(make_pair(var.name, &var));
 }
 
index c04e2f45fa38a6a419feff3cbf981653c130c104..4e66ef68ed6c75fbda2e360dfe813a1cdda1838e 100644 (file)
@@ -107,17 +107,15 @@ public:
 
 private:
        virtual void enter(Block &);
-       void visit_and_replace(RefPtr<Expression> &);
+       virtual void visit(RefPtr<Expression> &);
        void check_assignment_target(Statement *);
        virtual void visit(VariableReference &);
        virtual void visit(InterfaceBlockReference &);
        void add_to_chain(Assignment::Target::ChainType, unsigned);
        virtual void visit(MemberAccess &);
        virtual void visit(Swizzle &);
-       virtual void visit(UnaryExpression &);
        virtual void visit(BinaryExpression &);
        virtual void visit(Assignment &);
-       virtual void visit(FunctionCall &);
        virtual void visit(VariableDeclaration &);
        virtual void visit(InterfaceBlock &);
 };
index 87410d5a78c46d3e8eb2e38c9d3a2fd24ffafcef..be6cf7b6e456257bb8b46c6fc27f3781189eb1b2 100644 (file)
@@ -193,7 +193,7 @@ bool FunctionInliner::apply(Stage &s)
        return r_any_inlined;
 }
 
-void FunctionInliner::visit_and_inline(RefPtr<Expression> &ptr)
+void FunctionInliner::visit(RefPtr<Expression> &ptr)
 {
        r_inline_result = 0;
        ptr->visit(*this);
@@ -216,31 +216,10 @@ void FunctionInliner::visit(Block &block)
        }
 }
 
-void FunctionInliner::visit(UnaryExpression &unary)
-{
-       visit_and_inline(unary.expression);
-}
-
-void FunctionInliner::visit(BinaryExpression &binary)
-{
-       visit_and_inline(binary.left);
-       visit_and_inline(binary.right);
-}
-
-void FunctionInliner::visit(MemberAccess &memacc)
-{
-       visit_and_inline(memacc.left);
-}
-
-void FunctionInliner::visit(Swizzle &swizzle)
-{
-       visit_and_inline(swizzle.left);
-}
-
 void FunctionInliner::visit(FunctionCall &call)
 {
        for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
-               visit_and_inline(*i);
+               visit(*i);
 
        FunctionDeclaration *def = call.declaration;
        if(def)
@@ -264,29 +243,12 @@ void FunctionInliner::visit(FunctionCall &call)
        }
 }
 
-void FunctionInliner::visit(ExpressionStatement &expr)
-{
-       visit_and_inline(expr.expression);
-}
-
-void FunctionInliner::visit(VariableDeclaration &var)
-{
-       if(var.init_expression)
-               visit_and_inline(var.init_expression);
-}
-
 void FunctionInliner::visit(FunctionDeclaration &func)
 {
        SetForScope<FunctionDeclaration *> set_func(current_function, &func);
        TraversingVisitor::visit(func);
 }
 
-void FunctionInliner::visit(Conditional &cond)
-{
-       visit_and_inline(cond.condition);
-       cond.body.visit(*this);
-}
-
 void FunctionInliner::visit(Iteration &iter)
 {
        /* Visit the initialization statement before entering the loop body so the
@@ -301,12 +263,6 @@ void FunctionInliner::visit(Iteration &iter)
        iter.body.visit(*this);
 }
 
-void FunctionInliner::visit(Return &ret)
-{
-       if(ret.expression)
-               visit_and_inline(ret.expression);
-}
-
 
 ExpressionInliner::ExpressionInfo::ExpressionInfo():
        expression(0),
@@ -413,6 +369,11 @@ void ExpressionInliner::visit(Block &block)
                        i->second.available = false;
 }
 
+void ExpressionInliner::visit(RefPtr<Expression> &expr)
+{
+       visit_and_record(expr, 0, false);
+}
+
 void ExpressionInliner::visit(VariableReference &var)
 {
        if(var.declaration)
@@ -493,8 +454,7 @@ void ExpressionInliner::visit(Assignment &assign)
 
 void ExpressionInliner::visit(FunctionCall &call)
 {
-       for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
-               visit_and_record(*i, 0, false);
+       TraversingVisitor::visit(call);
        r_oper = 0;
        r_trivial = false;
 }
@@ -503,8 +463,7 @@ void ExpressionInliner::visit(VariableDeclaration &var)
 {
        r_oper = 0;
        r_trivial = true;
-       if(var.init_expression)
-               visit_and_record(var.init_expression, 0, false);
+       TraversingVisitor::visit(var);
 
        bool constant = var.constant;
        if(constant && var.layout)
@@ -528,12 +487,6 @@ void ExpressionInliner::visit(VariableDeclaration &var)
        }
 }
 
-void ExpressionInliner::visit(Conditional &cond)
-{
-       visit_and_record(cond.condition, 0, false);
-       cond.body.visit(*this);
-}
-
 void ExpressionInliner::visit(Iteration &iter)
 {
        SetForScope<Block *> set_block(current_block, &iter.body);
@@ -545,16 +498,10 @@ void ExpressionInliner::visit(Iteration &iter)
 
        SetForScope<Block *> set_body(iteration_body, &iter.body);
        if(iter.condition)
-               iter.condition->visit(*this);
+               visit(iter.condition);
        iter.body.visit(*this);
        if(iter.loop_expression)
-               iter.loop_expression->visit(*this);
-}
-
-void ExpressionInliner::visit(Return &ret)
-{
-       if(ret.expression)
-               visit_and_record(ret.expression, 0, false);
+               visit(iter.loop_expression);
 }
 
 
index 4663c12ec12e6ffcaf4fe946fff5bbc36cc5ed90..26076452efe58776ac092a8ce6ad840f63345b14 100644 (file)
@@ -83,21 +83,11 @@ public:
        bool apply(Stage &);
 
 private:
-       void visit_and_inline(RefPtr<Expression> &);
-
+       virtual void visit(RefPtr<Expression> &);
        virtual void visit(Block &);
-       virtual void visit(UnaryExpression &);
-       virtual void visit(BinaryExpression &);
-       virtual void visit(Assignment &a) { visit(static_cast<BinaryExpression &>(a)); }
-       virtual void visit(MemberAccess &);
-       virtual void visit(Swizzle &);
        virtual void visit(FunctionCall &);
-       virtual void visit(ExpressionStatement &);
-       virtual void visit(VariableDeclaration &);
        virtual void visit(FunctionDeclaration &);
-       virtual void visit(Conditional &);
        virtual void visit(Iteration &);
-       virtual void visit(Return &);
 };
 
 /** Inlines variables into expressions.  Variables with trivial values (those
@@ -138,6 +128,7 @@ private:
        void visit_and_record(RefPtr<Expression> &, const Operator *, bool);
        void inline_expression(Expression &, RefPtr<Expression> &, const Operator *, const Operator *, bool);
        virtual void visit(Block &);
+       virtual void visit(RefPtr<Expression> &);
        virtual void visit(VariableReference &);
        virtual void visit(MemberAccess &);
        virtual void visit(Swizzle &);
@@ -146,9 +137,7 @@ private:
        virtual void visit(Assignment &);
        virtual void visit(FunctionCall &);
        virtual void visit(VariableDeclaration &);
-       virtual void visit(Conditional &);
        virtual void visit(Iteration &);
-       virtual void visit(Return &);
 };
 
 /** Removes conditional statements and loops where the condition can be
index dfe2d8d46523a00d83b024f1e13dc261db36703d..8c8056c56352c99a56ca3a7e5b21e298026e5fbe 100644 (file)
@@ -16,47 +16,52 @@ void TraversingVisitor::visit(Block &block)
                (*i)->visit(*this);
 }
 
+void TraversingVisitor::visit(RefPtr<Expression> &expr)
+{
+       expr->visit(*this);
+}
+
 void TraversingVisitor::visit(ParenthesizedExpression &parexpr)
 {
-       parexpr.expression->visit(*this);
+       visit(parexpr.expression);
 }
 
 void TraversingVisitor::visit(MemberAccess &memacc)
 {
-       memacc.left->visit(*this);
+       visit(memacc.left);
 }
 
 void TraversingVisitor::visit(Swizzle &swizzle)
 {
-       swizzle.left->visit(*this);
+       visit(swizzle.left);
 }
 
 void TraversingVisitor::visit(UnaryExpression &unary)
 {
-       unary.expression->visit(*this);
+       visit(unary.expression);
 }
 
 void TraversingVisitor::visit(BinaryExpression &binary)
 {
-       binary.left->visit(*this);
-       binary.right->visit(*this);
+       visit(binary.left);
+       visit(binary.right);
 }
 
 void TraversingVisitor::visit(Assignment &assign)
 {
-       assign.left->visit(*this);
-       assign.right->visit(*this);
+       visit(assign.left);
+       visit(assign.right);
 }
 
 void TraversingVisitor::visit(FunctionCall &call)
 {
        for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
-               (*i)->visit(*this);
+               visit(*i);
 }
 
 void TraversingVisitor::visit(ExpressionStatement &expr)
 {
-       expr.expression->visit(*this);
+       visit(expr.expression);
 }
 
 void TraversingVisitor::visit(InterfaceLayout &layout)
@@ -74,9 +79,9 @@ void TraversingVisitor::visit(VariableDeclaration &var)
        if(var.layout)
                var.layout->visit(*this);
        if(var.init_expression)
-               var.init_expression->visit(*this);
+               visit(var.init_expression);
        if(var.array_size)
-               var.array_size->visit(*this);
+               visit(var.array_size);
 }
 
 void TraversingVisitor::visit(InterfaceBlock &iface)
@@ -96,7 +101,7 @@ void TraversingVisitor::visit(FunctionDeclaration &func)
 
 void TraversingVisitor::visit(Conditional &cond)
 {
-       cond.condition->visit(*this);
+       visit(cond.condition);
        cond.body.visit(*this);
        cond.else_body.visit(*this);
 }
@@ -108,22 +113,22 @@ void TraversingVisitor::visit(Iteration &iter)
        if(iter.init_statement)
                iter.init_statement->visit(*this);
        if(iter.condition)
-               iter.condition->visit(*this);
+               visit(iter.condition);
        iter.body.visit(*this);
        if(iter.loop_expression)
-               iter.loop_expression->visit(*this);
+               visit(iter.loop_expression);
 }
 
 void TraversingVisitor::visit(Passthrough &pass)
 {
        if(pass.subscript)
-               pass.subscript->visit(*this);
+               visit(pass.subscript);
 }
 
 void TraversingVisitor::visit(Return &ret)
 {
        if(ret.expression)
-               ret.expression->visit(*this);
+               visit(ret.expression);
 }
 
 
index 30ab25444f2aabe89636309b29d8a9dc56ed519d..85f9e848c39b9b45b822c63408ceb6e44a0e5bb9 100644 (file)
@@ -57,6 +57,7 @@ protected:
 public:
        virtual void enter(Block &) { }
        virtual void visit(Block &);
+       virtual void visit(RefPtr<Expression> &);
        virtual void visit(ParenthesizedExpression &);
        virtual void visit(MemberAccess &);
        virtual void visit(Swizzle &);