From: Mikko Rasa Date: Fri, 18 Nov 2016 17:38:10 +0000 (+0200) Subject: Refactor block modification operations into an intermediate base class X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=acec85413f86c58ff262fbc26c2c1aa8726b5c58;p=libs%2Fgl.git Refactor block modification operations into an intermediate base class --- diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index 937639a4..3cb8611c 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -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 >::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 >::iterator &i) +{ + for(list::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 >::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::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 set(scope_level, scope_level+1); - for(list >::iterator i=block.body.begin(); i!=block.body.end(); ) - { - (*i)->visit(*this); - if(replacement_block) - { - for(list >::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::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); } diff --git a/source/programcompiler.h b/source/programcompiler.h index 8681d363..96fee471 100644 --- a/source/programcompiler.h +++ b/source/programcompiler.h @@ -116,14 +116,24 @@ private: virtual void visit(ProgramSyntax::FunctionDeclaration &); }; - struct InterfaceGenerator: Visitor + struct BlockModifier: Visitor + { + bool remove_node; + std::list insert_nodes; + + BlockModifier(); + + void flatten_block(ProgramSyntax::Block &); + void apply_and_increment(ProgramSyntax::Block &, std::list >::iterator &); + virtual void visit(ProgramSyntax::Block &); + }; + + struct InterfaceGenerator: BlockModifier { std::string in_prefix; std::string out_prefix; unsigned scope_level; std::map iface_declarations; - bool remove_node; - std::list 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();