]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/visitor.cpp
Rewrite syntax tree modifications
[libs/gl.git] / source / glsl / visitor.cpp
index 98777ae9b0541f97af5b4156a7725235d9cb24d6..cf0cca9612e92132e9a89f16471f788c8f7d00a1 100644 (file)
@@ -114,41 +114,11 @@ void TraversingVisitor::visit(Return &ret)
 }
 
 
-BlockModifier::BlockModifier():
-       remove_node(false)
-{ }
-
-void BlockModifier::flatten_block(Block &block)
-{
-       insert_nodes.insert(insert_nodes.end(), block.body.begin(), block.body.end());
-       remove_node = true;
-}
-
-void BlockModifier::apply_and_increment(Block &block, NodeList<Statement>::iterator &i)
-{
-       block.body.insert(i, insert_nodes.begin(), insert_nodes.end());
-       insert_nodes.clear();
-
-       if(remove_node)
-               block.body.erase(i++);
-       else
-               ++i;
-       remove_node = false;
-}
-
-void BlockModifier::visit(Block &block)
-{
-       for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
-       {
-               (*i)->visit(*this);
-               apply_and_increment(block, i);
-       }
-}
-
-
 NodeRemover::NodeRemover():
        stage(0),
-       to_remove(0)
+       to_remove(0),
+       anonymous(false),
+       recursive_remove(false)
 { }
 
 void NodeRemover::apply(Stage &s, const set<Node *> &tr)
@@ -158,8 +128,16 @@ void NodeRemover::apply(Stage &s, const set<Node *> &tr)
        visit(s.content);
 }
 
+void NodeRemover::remove_variable(map<string, VariableDeclaration *> &vars, VariableDeclaration &decl)
+{
+       map<string, VariableDeclaration *>::iterator i = vars.find(decl.name);
+       if(i!=vars.end() && i->second==&decl)
+               vars.erase(i);
+}
+
 void NodeRemover::visit(Block &block)
 {
+       blocks.push_back(&block);
        for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
        {
                (*i)->visit(*this);
@@ -168,14 +146,24 @@ void NodeRemover::visit(Block &block)
                else
                        ++i;
        }
+       blocks.pop_back();
+}
+
+void NodeRemover::visit(StructDeclaration &strct)
+{
+       if(to_remove->count(&strct))
+               blocks.back()->types.erase(strct.name);
 }
 
 void NodeRemover::visit(VariableDeclaration &var)
 {
-       if(to_remove->count(&var))
+       if(recursive_remove || to_remove->count(&var))
        {
-               stage->in_variables.erase(var.name);
-               stage->out_variables.erase(var.name);
+               remove_variable(blocks.back()->variables, var);
+               if(anonymous && blocks.size()>1)
+                       remove_variable(blocks[blocks.size()-2]->variables, var);
+               remove_variable(stage->in_variables, var);
+               remove_variable(stage->out_variables, var);
                stage->locations.erase(var.name);
                if(var.linked_declaration)
                        var.linked_declaration->linked_declaration = 0;
@@ -184,6 +172,13 @@ void NodeRemover::visit(VariableDeclaration &var)
                var.init_expression = 0;
 }
 
+void NodeRemover::visit(InterfaceBlock &iface)
+{
+       SetFlag set_anon(anonymous);
+       SetFlag set_recursive(recursive_remove, recursive_remove || to_remove->count(&iface));
+       TraversingVisitor::visit(iface);
+}
+
 void NodeRemover::visit(Iteration &iter)
 {
        if(to_remove->count(iter.init_statement.get()))