toplevel(true)
{ }
+void DeclarationCombiner::apply(Stage &stage)
+{
+ visit(stage.content);
+ NodeRemover().apply(stage, nodes_to_remove);
+}
+
void DeclarationCombiner::visit(Block &block)
{
if(!toplevel)
return;
SetForScope<bool> set(toplevel, false);
- BlockModifier::visit(block);
+ TraversingVisitor::visit(block);
}
void DeclarationCombiner::visit(FunctionDeclaration &func)
else
ptr->layout = var.layout;
}
- remove_node = true;
+ nodes_to_remove.insert(&var);
}
else
ptr = &var;
self_referencing(false)
{ }
-void VariableResolver::apply(Stage &s)
+void VariableResolver::apply(Stage &stage)
{
- SetForScope<Stage *> set(stage, &s);
- Stage *builtins = get_builtins(stage->type);
+ Stage *builtins = get_builtins(stage.type);
if(builtins)
blocks.push_back(&builtins->content);
- stage->content.visit(*this);
+ visit(stage.content);
if(builtins)
blocks.pop_back();
}
InterfaceGenerator::InterfaceGenerator():
- scope_level(0)
+ stage(0),
+ scope_level(0),
+ current_block(0)
{ }
-string InterfaceGenerator::get_out_prefix(StageType type)
+string InterfaceGenerator::get_out_prefix(Stage::Type type)
{
- if(type==VERTEX)
+ if(type==Stage::VERTEX)
return "_vs_out_";
- else if(type==GEOMETRY)
+ else if(type==Stage::GEOMETRY)
return "_gs_out_";
else
return string();
void InterfaceGenerator::apply(Stage &s)
{
- SetForScope<Stage *> set(stage, &s);
+ stage = &s;
if(stage->previous)
in_prefix = get_out_prefix(stage->previous->type);
out_prefix = get_out_prefix(stage->type);
- stage->content.visit(*this);
+ visit(s.content);
+ NodeRemover().apply(s, nodes_to_remove);
}
void InterfaceGenerator::visit(Block &block)
{
SetForScope<unsigned> set(scope_level, scope_level+1);
- for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
+ SetForScope<Block *> set_block(current_block, &block);
+ for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
{
- (*i)->visit(*this);
-
+ assignment_insert_point = i;
if(scope_level==1)
- {
- for(map<string, RefPtr<VariableDeclaration> >::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j)
- {
- NodeList<Statement>::iterator k = block.body.insert(i, j->second);
- (*k)->visit(*this);
- }
- iface_declarations.clear();
- }
+ iface_insert_point = i;
- apply_and_increment(block, i);
+ (*i)->visit(*this);
}
}
bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
{
const map<string, VariableDeclaration *> &stage_vars = (iface=="in" ? stage->in_variables : stage->out_variables);
- if(stage_vars.count(name) || iface_declarations.count(name))
+ if(stage_vars.count(name))
return false;
VariableDeclaration* iface_var = new VariableDeclaration;
iface_var->type = var.type;
iface_var->type_declaration = var.type_declaration;
iface_var->name = name;
- if(stage->type==GEOMETRY)
+ if(stage->type==Stage::GEOMETRY)
iface_var->array = ((var.array && var.interface!="in") || iface=="in");
else
iface_var->array = var.array;
iface_var->array_size = var.array_size;
if(iface=="in")
iface_var->linked_declaration = &var;
- iface_declarations[name] = iface_var;
+ stage->content.body.insert(iface_insert_point, iface_var);
+ {
+ SetForScope<unsigned> set_level(scope_level, 1);
+ SetForScope<Block *> set_block(current_block, &stage->content);
+ iface_var->visit(*this);
+ }
return true;
}
ExpressionStatement *stmt = new ExpressionStatement;
stmt->expression = assign;
+ current_block->body.insert(assignment_insert_point, stmt);
stmt->visit(*this);
- insert_nodes.push_back(stmt);
return *stmt;
}
{
if(var.declaration || !stage->previous)
return;
- if(iface_declarations.count(var.name))
- return;
const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
stage->out_variables[var.name] = &var;
else if(generate_interface(var, "out", change_prefix(var.name, string())))
{
- remove_node = true;
+ nodes_to_remove.insert(&var);
if(var.init_expression)
{
ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
for(map<string, VariableDeclaration *>::const_iterator i=stage->in_variables.begin(); i!=stage->in_variables.end(); ++i)
pass_vars.push_back(i->second);
- for(map<string, RefPtr<VariableDeclaration> >::const_iterator i=iface_declarations.begin(); i!=iface_declarations.end(); ++i)
- if(i->second->interface=="in")
- pass_vars.push_back(i->second.get());
if(stage->previous)
{
}
}
- if(stage->type==GEOMETRY)
+ if(stage->type==Stage::GEOMETRY)
{
VariableReference *ref = new VariableReference;
ref->name = "gl_in";
insert_assignment(out_name, ref);
}
- remove_node = true;
+ nodes_to_remove.insert(&pass);
}
{
SetForScope<unsigned> set(scope_level, scope_level+1);
if(scope_level>1)
- return StageVisitor::visit(block);
+ return TraversingVisitor::visit(block);
NodeList<Statement>::iterator struct_insert_point = block.body.end();
NodeList<Statement>::iterator variable_insert_point = block.body.end();
void DeclarationReorderer::visit(VariableDeclaration &var)
{
- StageVisitor::visit(var);
+ TraversingVisitor::visit(var);
kind = VARIABLE;
}