]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor block modification operations into an intermediate base class
authorMikko Rasa <tdb@tdb.fi>
Fri, 18 Nov 2016 17:38:10 +0000 (19:38 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 18 Nov 2016 17:38:10 +0000 (19:38 +0200)
source/programcompiler.cpp
source/programcompiler.h

index 937639a49157bf6f73cda381d306585e1c05b482..3cb8611c0192be3d08d67798aa9c1c0d6407f09a 100644 (file)
@@ -647,11 +647,44 @@ void ProgramCompiler::FunctionResolver::visit(FunctionDeclaration &func)
 }
 
 
-ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
-       scope_level(0),
+ProgramCompiler::BlockModifier::BlockModifier():
        remove_node(false)
 { }
 
+void ProgramCompiler::BlockModifier::flatten_block(Block &block)
+{
+       for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+               insert_nodes.push_back((*i)->clone());
+       remove_node = true;
+}
+
+void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, list<NodePtr<Node> >::iterator &i)
+{
+       for(list<Node *>::iterator j=insert_nodes.begin(); j!=insert_nodes.end(); ++j)
+               block.body.insert(i, *j);
+       insert_nodes.clear();
+
+       if(remove_node)
+               block.body.erase(i++);
+       else
+               ++i;
+       remove_node = false;
+}
+
+void ProgramCompiler::BlockModifier::visit(Block &block)
+{
+       for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
+       {
+               (*i)->visit(*this);
+               apply_and_increment(block, i);
+       }
+}
+
+
+ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
+       scope_level(0)
+{ }
+
 string ProgramCompiler::InterfaceGenerator::get_out_prefix(StageType type)
 {
        if(type==VERTEX)
@@ -688,15 +721,7 @@ void ProgramCompiler::InterfaceGenerator::visit(Block &block)
                        iface_declarations.clear();
                }
 
-               for(list<Node *>::iterator j=insert_nodes.begin(); j!=insert_nodes.end(); ++j)
-                       block.body.insert(i, *j);
-               insert_nodes.clear();
-
-               if(remove_node)
-                       block.body.erase(i++);
-               else
-                       ++i;
-               remove_node = false;
+               apply_and_increment(block, i);
        }
 }
 
@@ -959,30 +984,13 @@ void ProgramCompiler::ExpressionEvaluator::visit(BinaryExpression &binary)
 
 
 ProgramCompiler::ConstantConditionEliminator::ConstantConditionEliminator():
-       scope_level(0),
-       remove_node(false),
-       replacement_block(0)
+       scope_level(0)
 { }
 
 void ProgramCompiler::ConstantConditionEliminator::visit(Block &block)
 {
        SetForScope<unsigned> set(scope_level, scope_level+1);
-       for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
-       {
-               (*i)->visit(*this);
-               if(replacement_block)
-               {
-                       for(list<NodePtr<Node> >::iterator j=replacement_block->body.begin(); j!=replacement_block->body.end(); ++j)
-                               block.body.insert(i, *j);
-                       replacement_block = 0;
-               }
-
-               if(remove_node)
-                       block.body.erase(i++);
-               else
-                       ++i;
-               remove_node = false;
-       }
+       BlockModifier::visit(block);
 
        for(map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
                variable_values.erase(i->second);
@@ -1004,10 +1012,7 @@ void ProgramCompiler::ConstantConditionEliminator::visit(Conditional &cond)
        ExpressionEvaluator eval(variable_values);
        cond.condition->visit(eval);
        if(eval.result_valid)
-       {
-               remove_node = true;
-               replacement_block = (eval.result ? &cond.body : &cond.else_body);
-       }
+               flatten_block(eval.result ? cond.body : cond.else_body);
        else
                TraversingVisitor::visit(cond);
 }
index 8681d3638a032cabd389879922fe7378c4a45a76..96fee471cc7e5351a59e7d51f9b3772862bcc195 100644 (file)
@@ -116,14 +116,24 @@ private:
                virtual void visit(ProgramSyntax::FunctionDeclaration &);
        };
 
-       struct InterfaceGenerator: Visitor
+       struct BlockModifier: Visitor
+       {
+               bool remove_node;
+               std::list<ProgramSyntax::Node *> insert_nodes;
+
+               BlockModifier();
+
+               void flatten_block(ProgramSyntax::Block &);
+               void apply_and_increment(ProgramSyntax::Block &, std::list<ProgramSyntax::NodePtr<ProgramSyntax::Node> >::iterator &);
+               virtual void visit(ProgramSyntax::Block &);
+       };
+
+       struct InterfaceGenerator: BlockModifier
        {
                std::string in_prefix;
                std::string out_prefix;
                unsigned scope_level;
                std::map<std::string, ProgramSyntax::VariableDeclaration *> iface_declarations;
-               bool remove_node;
-               std::list<ProgramSyntax::Node *> insert_nodes;
 
                InterfaceGenerator();
 
@@ -161,12 +171,10 @@ private:
                virtual void visit(ProgramSyntax::BinaryExpression &);
        };
 
-       struct ConstantConditionEliminator: Visitor
+       struct ConstantConditionEliminator: BlockModifier
        {
                unsigned scope_level;
                ExpressionEvaluator::ValueMap variable_values;
-               bool remove_node;
-               ProgramSyntax::Block *replacement_block;
 
                ConstantConditionEliminator();