]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/visitor.cpp
Fix another case of future features leaking
[libs/gl.git] / source / glsl / visitor.cpp
index c27de11892607ec627e1a6bf7022e2aa267e0023..f4b2a8d01c25294ca153e1a629d86de0260b1cc8 100644 (file)
@@ -134,10 +134,11 @@ void NodeRemover::apply(Stage &s, const set<Node *> &tr)
        s.content.visit(*this);
 }
 
-void NodeRemover::remove_variable(map<string, VariableDeclaration *> &vars, VariableDeclaration &decl)
+template<typename T>
+void NodeRemover::remove_from_map(map<string, T *> &vars, const string &key, T &node)
 {
-       map<string, VariableDeclaration *>::iterator i = vars.find(decl.name);
-       if(i!=vars.end() && i->second==&decl)
+       typename map<string, T *>::iterator i = vars.find(key);
+       if(i!=vars.end() && i->second==&node)
                vars.erase(i);
 }
 
@@ -157,14 +158,14 @@ void NodeRemover::visit(Block &block)
 void NodeRemover::visit(StructDeclaration &strct)
 {
        if(to_remove->count(&strct))
-               current_block->types.erase(strct.name);
+               remove_from_map(stage->types, strct.name, strct);
 }
 
 void NodeRemover::visit(VariableDeclaration &var)
 {
        if(recursive_remove || to_remove->count(&var))
        {
-               remove_variable(current_block->variables, var);
+               remove_from_map(current_block->variables, var.name, var);
                stage->locations.erase(var.name);
                if(var.linked_declaration)
                        var.linked_declaration->linked_declaration = 0;
@@ -176,11 +177,22 @@ void NodeRemover::visit(VariableDeclaration &var)
 void NodeRemover::visit(InterfaceBlock &iface)
 {
        if(to_remove->count(&iface))
-               current_block->interfaces.erase(&iface);
+       {
+               remove_from_map(stage->interface_blocks, iface.name, iface);
+               if(!iface.instance_name.empty())
+                       remove_from_map(stage->interface_blocks, iface.instance_name, iface);
+       }
        SetFlag set_recursive(recursive_remove, recursive_remove || to_remove->count(&iface));
        TraversingVisitor::visit(iface);
 }
 
+void NodeRemover::visit(FunctionDeclaration &func)
+{
+       if(to_remove->count(&func))
+               remove_from_map(stage->functions, func.name, func);
+       TraversingVisitor::visit(func);
+}
+
 void NodeRemover::visit(Iteration &iter)
 {
        if(to_remove->count(iter.init_statement.get()))
@@ -188,6 +200,39 @@ void NodeRemover::visit(Iteration &iter)
        TraversingVisitor::visit(iter);
 }
 
+
+NodeReorderer::NodeReorderer():
+       reorder_before(0),
+       to_reorder(0)
+{ }
+
+void NodeReorderer::apply(Stage &stage, Node &before, const set<Node *> &tr)
+{
+       reorder_before = &before;
+       to_reorder = &tr;
+       stage.content.visit(*this);
+}
+
+void NodeReorderer::visit(Block &block)
+{
+       NodeList<Statement>::iterator insert_point = block.body.end();
+       for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
+       {
+               (*i)->visit(*this);
+               if(insert_point!=block.body.end() && to_reorder->count(i->get()))
+               {
+                       NodeList<Statement>::iterator j = i++;
+                       block.body.splice(insert_point, block.body, j);
+               }
+               else
+               {
+                       if(i->get()==reorder_before)
+                               insert_point = i;
+                       ++i;
+               }
+       }
+}
+
 } // namespace SL
 } // namespace GL
 } // namespace Msp