}
-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)
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);
}
}
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);
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);
}
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();
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();