]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/generate.cpp
Use default member initializers for simple types
[libs/gl.git] / source / glsl / generate.cpp
index f3c57a2f0f9190352860bc5f7bc22ff634433123..ec51c19cdd9cc90472a0b2471eff915823fae0d7 100644 (file)
@@ -1,5 +1,6 @@
+#include <msp/core/algorithm.h>
+#include <msp/core/hash.h>
 #include <msp/core/raii.h>
-#include "builtin.h"
 #include "generate.h"
 
 using namespace std;
@@ -8,240 +9,40 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
-DeclarationCombiner::DeclarationCombiner():
-       toplevel(true)
-{ }
-
-void DeclarationCombiner::visit(Block &block)
+void ConstantIdAssigner::apply(Module &module, const Features &features)
 {
-       if(!toplevel)
-               return;
-
-       SetForScope<bool> set(toplevel, false);
-       BlockModifier::visit(block);
-}
+       for(Stage &s: module.stages)
+               s.content.visit(*this);
 
-void DeclarationCombiner::visit(FunctionDeclaration &func)
-{
-       vector<FunctionDeclaration *> &decls = functions[func.name];
-       if(func.definition)
+       for(VariableDeclaration *v: auto_constants)
        {
-               for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
-               {
-                       (*i)->definition = func.definition;
-                       (*i)->body.body.clear();
-               }
-       }
-       decls.push_back(&func);
-}
+               unsigned id = hash32(v->name)%features.constant_id_range;
+               while(used_ids.count(id))
+                       id = (id+1)%features.constant_id_range;
 
-void DeclarationCombiner::visit(VariableDeclaration &var)
-{
-       VariableDeclaration *&ptr = variables[var.name];
-       if(ptr)
-       {
-               ptr->type = var.type;
-               if(var.init_expression)
-                       ptr->init_expression = var.init_expression;
-               if(var.layout)
-               {
-                       if(ptr->layout)
-                       {
-                               for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
-                               {
-                                       bool found = false;
-                                       for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
-                                               if(j->identifier==i->identifier)
-                                               {
-                                                       j->value = i->value;
-                                                       found = true;
-                                               }
-
-                                       if(!found)
-                                               ptr->layout->qualifiers.push_back(*i);
-                               }
-                       }
-                       else
-                               ptr->layout = var.layout;
-               }
-               remove_node = true;
-       }
-       else
-               ptr = &var;
-}
-
-
-VariableResolver::VariableResolver():
-       anonymous(false),
-       record_target(false),
-       assignment_target(0),
-       self_referencing(false)
-{ }
+               auto i = find_member(v->layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
+               if(i!=v->layout->qualifiers.end())
+                       i->value = id;
 
-void VariableResolver::apply(Stage &stage)
-{
-       Stage *builtins = get_builtins(stage.type);
-       if(builtins)
-               blocks.push_back(&builtins->content);
-       visit(stage.content);
-       if(builtins)
-               blocks.pop_back();
-}
-
-void VariableResolver::visit(Block &block)
-{
-       blocks.push_back(&block);
-       block.variables.clear();
-       TraversingVisitor::visit(block);
-       blocks.pop_back();
-}
-
-void VariableResolver::visit(VariableReference &var)
-{
-       var.declaration = 0;
-       type = 0;
-       for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
-       {
-               --i;
-               map<string, VariableDeclaration *>::iterator j = (*i)->variables.find(var.name);
-               if(j!=(*i)->variables.end())
-               {
-                       var.declaration = j->second;
-                       type = j->second->type_declaration;
-                       break;
-               }
-       }
-
-       if(record_target)
-       {
-               if(assignment_target)
-               {
-                       record_target = false;
-                       assignment_target = 0;
-               }
-               else
-                       assignment_target = var.declaration;
-       }
-       else if(var.declaration && var.declaration==assignment_target)
-               self_referencing = true;
-}
-
-void VariableResolver::visit(MemberAccess &memacc)
-{
-       type = 0;
-       TraversingVisitor::visit(memacc);
-       memacc.declaration = 0;
-       if(type)
-       {
-               map<string, VariableDeclaration *>::iterator i = type->members.variables.find(memacc.member);
-               if(i!=type->members.variables.end())
-               {
-                       memacc.declaration = i->second;
-                       type = i->second->type_declaration;
-               }
-               else
-                       type = 0;
+               used_ids.insert(id);
        }
 }
 
-void VariableResolver::visit(BinaryExpression &binary)
+void ConstantIdAssigner::visit(VariableDeclaration &var)
 {
-       if(binary.oper=="[")
+       if(var.layout)
        {
+               auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
+               if(i!=var.layout->qualifiers.end() && i->has_value)
                {
-                       SetForScope<bool> set(record_target, false);
-                       binary.right->visit(*this);
+                       if(i->value==-1)
+                               auto_constants.push_back(&var);
+                       else
+                               used_ids.insert(i->value);
                }
-               type = 0;
-               binary.left->visit(*this);
-       }
-       else
-       {
-               TraversingVisitor::visit(binary);
-               type = 0;
        }
 }
 
-void VariableResolver::visit(Assignment &assign)
-{
-       {
-               SetFlag set(record_target);
-               assignment_target = 0;
-               assign.left->visit(*this);
-       }
-
-       self_referencing = false;
-       assign.right->visit(*this);
-
-       assign.self_referencing = (self_referencing || assign.oper!="=");
-       assign.target_declaration = assignment_target;
-}
-
-void VariableResolver::visit(StructDeclaration &strct)
-{
-       TraversingVisitor::visit(strct);
-       blocks.back()->types[strct.name] = &strct;
-}
-
-void VariableResolver::visit(VariableDeclaration &var)
-{
-       for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
-       {
-               --i;
-               map<string, StructDeclaration *>::iterator j = (*i)->types.find(var.type);
-               if(j!=(*i)->types.end())
-                       var.type_declaration = j->second;
-       }
-
-       if(!block_interface.empty() && var.interface.empty())
-               var.interface = block_interface;
-
-       TraversingVisitor::visit(var);
-       blocks.back()->variables[var.name] = &var;
-       if(anonymous && blocks.size()>1)
-               blocks[blocks.size()-2]->variables[var.name] = &var;
-}
-
-void VariableResolver::visit(InterfaceBlock &iface)
-{
-       SetFlag set(anonymous);
-       SetForScope<string> set2(block_interface, iface.interface);
-       TraversingVisitor::visit(iface);
-}
-
-
-void FunctionResolver::visit(FunctionCall &call)
-{
-       map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
-       if(i!=functions.end())
-               call.declaration = i->second.back();
-
-       TraversingVisitor::visit(call);
-}
-
-void FunctionResolver::visit(FunctionDeclaration &func)
-{
-       vector<FunctionDeclaration *> &decls = functions[func.name];
-       if(func.definition)
-       {
-               for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
-                       (*i)->definition = func.definition;
-               decls.clear();
-               decls.push_back(&func);
-       }
-       else if(!decls.empty() && decls.back()->definition)
-               func.definition = decls.back()->definition;
-       else
-               decls.push_back(&func);
-
-       TraversingVisitor::visit(func);
-}
-
-
-InterfaceGenerator::InterfaceGenerator():
-       stage(0),
-       scope_level(0)
-{ }
 
 string InterfaceGenerator::get_out_prefix(Stage::Type type)
 {
@@ -255,31 +56,25 @@ string InterfaceGenerator::get_out_prefix(Stage::Type type)
 
 void InterfaceGenerator::apply(Stage &s)
 {
-       SetForScope<Stage *> set(stage, &s);
+       stage = &s;
+       iface_target_block = &stage->content;
        if(stage->previous)
                in_prefix = get_out_prefix(stage->previous->type);
        out_prefix = get_out_prefix(stage->type);
-       visit(s.content);
+       s.content.visit(*this);
+       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(auto i=block.body.begin(); i!=block.body.end(); ++i)
        {
-               (*i)->visit(*this);
-
-               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();
-               }
+               assignment_insert_point = i;
+               if(&block==&stage->content)
+                       iface_insert_point = i;
 
-               apply_and_increment(block, i);
+               (*i)->visit(*this);
        }
 }
 
@@ -289,29 +84,79 @@ string InterfaceGenerator::change_prefix(const string &name, const string &prefi
        return prefix+name.substr(offset);
 }
 
-bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
+VariableDeclaration *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))
-               return false;
+       if(stage->content.variables.count(name))
+               return 0;
+
+       if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array)
+               return 0;
 
        VariableDeclaration* iface_var = new VariableDeclaration;
        iface_var->sampling = var.sampling;
        iface_var->interface = iface;
        iface_var->type = var.type;
-       iface_var->type_declaration = var.type_declaration;
        iface_var->name = name;
-       if(stage->type==Stage::GEOMETRY)
+       /* Geometry shader inputs are always arrays.  But if we're bringing in an
+       entire block, the array is on the block and not individual variables. */
+       if(stage->type==Stage::GEOMETRY && !copy_block)
                iface_var->array = ((var.array && var.interface!="in") || iface=="in");
        else
                iface_var->array = var.array;
        if(iface_var->array)
                iface_var->array_size = var.array_size;
        if(iface=="in")
+       {
+               iface_var->layout = var.layout;
                iface_var->linked_declaration = &var;
-       iface_declarations[name] = iface_var;
+               var.linked_declaration = iface_var;
+       }
+
+       iface_target_block->body.insert(iface_insert_point, iface_var);
+       iface_target_block->variables.insert(make_pair(name, iface_var));
+       if(iface_target_block==&stage->content && iface=="in")
+               declared_inputs.push_back(iface_var);
 
-       return true;
+       return iface_var;
+}
+
+InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
+{
+       if(stage->interface_blocks.count("in "+out_block.block_name))
+               return 0;
+
+       InterfaceBlock *in_block = new InterfaceBlock;
+       in_block->interface = "in";
+       in_block->block_name = out_block.block_name;
+       in_block->members = new Block;
+       in_block->instance_name = out_block.instance_name;
+       if(stage->type==Stage::GEOMETRY)
+               in_block->array = true;
+       else
+               in_block->array = out_block.array;
+       in_block->linked_block = &out_block;
+       out_block.linked_block = in_block;
+
+       {
+               SetFlag set_copy(copy_block, true);
+               SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
+               SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
+               if(out_block.struct_declaration)
+                       out_block.struct_declaration->members.visit(*this);
+               else if(out_block.members)
+                       out_block.members->visit(*this);
+       }
+
+       iface_target_block->body.insert(iface_insert_point, in_block);
+       stage->interface_blocks.insert(make_pair("in "+in_block->block_name, in_block));
+       if(!in_block->instance_name.empty())
+               stage->interface_blocks.insert(make_pair(in_block->instance_name, in_block));
+
+       SetFlag set_scope(function_scope, false);
+       SetForScope<Block *> set_block(current_block, &stage->content);
+       in_block->visit(*this);
+
+       return in_block;
 }
 
 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
@@ -320,13 +165,13 @@ ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, E
        VariableReference *ref = new VariableReference;
        ref->name = left;
        assign->left = ref;
-       assign->oper = "=";
+       assign->oper = &Operator::get_operator("=", Operator::BINARY);
        assign->right = right;
 
        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;
 }
@@ -335,29 +180,65 @@ void InterfaceGenerator::visit(VariableReference &var)
 {
        if(var.declaration || !stage->previous)
                return;
-       if(iface_declarations.count(var.name))
+       /* Don't pull a variable from previous stage if we just generated an output
+       interface in this stage */
+       if(stage->content.variables.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);
-       if(i==prev_out.end())
-               i = prev_out.find(in_prefix+var.name);
-       if(i!=prev_out.end())
+       const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
+       auto i = prev_vars.find(var.name);
+       if(i==prev_vars.end() || i->second->interface!="out")
+               i = prev_vars.find(in_prefix+var.name);
+       if(i!=prev_vars.end() && i->second->interface=="out")
        {
-               generate_interface(*i->second, "in", i->second->name);
-               var.name = i->second->name;
+               if(stage->type==Stage::GEOMETRY && i->second->array)
+                       stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
+                               format("Can't access '%s' through automatic interface because it's an array", var.name)));
+               else
+               {
+                       generate_interface(*i->second, "in", i->second->name);
+                       var.name = i->second->name;
+               }
+               return;
        }
+
+       const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
+       auto j = prev_blocks.find(var.name);
+       if(j!=prev_blocks.end() && j->second->interface=="out")
+       {
+               generate_interface(*j->second);
+               /* Let VariableResolver convert the variable reference into an interface
+               block reference. */
+               return;
+       }
+
+       for(const auto &kvp: prev_blocks)
+               if(kvp.second->instance_name.empty() && kvp.second->struct_declaration)
+               {
+                       const map<string, VariableDeclaration *> &iface_vars = kvp.second->struct_declaration->members.variables;
+                       i = iface_vars.find(var.name);
+                       if(i!=iface_vars.end())
+                       {
+                               generate_interface(*kvp.second);
+                               return;
+                       }
+               }
 }
 
 void InterfaceGenerator::visit(VariableDeclaration &var)
 {
-       if(var.interface=="out")
+       if(copy_block)
+               generate_interface(var, "in", var.name);
+       else if(var.interface=="out")
        {
-               if(scope_level==1)
-                       stage->out_variables[var.name] = &var;
-               else if(generate_interface(var, "out", change_prefix(var.name, string())))
+               /* For output variables in function scope, generate a global interface
+               and replace the local declaration with an assignment. */
+               VariableDeclaration *out_var = 0;
+               if(function_scope && (out_var=generate_interface(var, "out", var.name)))
                {
-                       remove_node = true;
+                       out_var->source = var.source;
+                       out_var->line = var.line;
+                       nodes_to_remove.insert(&var);
                        if(var.init_expression)
                        {
                                ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
@@ -367,16 +248,18 @@ void InterfaceGenerator::visit(VariableDeclaration &var)
                        }
                }
        }
-       else if(var.interface=="in")
+       else if(var.interface=="in" && current_block==&stage->content)
        {
-               stage->in_variables[var.name] = &var;
-               if(var.linked_declaration)
-                       var.linked_declaration->linked_declaration = &var;
-               else if(stage->previous)
+               if(var.name.compare(0, 3, "gl_"))
+                       declared_inputs.push_back(&var);
+
+               /* Try to link input variables in global scope with output variables from
+               previous stage. */
+               if(!var.linked_declaration && stage->previous)
                {
-                       const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
-                       map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
-                       if(i!=prev_out.end())
+                       const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
+                       auto i = prev_vars.find(var.name);
+                       if(i!=prev_vars.end() && i->second->interface=="out")
                        {
                                var.linked_declaration = i->second;
                                i->second->linked_declaration = &var;
@@ -387,40 +270,64 @@ void InterfaceGenerator::visit(VariableDeclaration &var)
        TraversingVisitor::visit(var);
 }
 
-void InterfaceGenerator::visit(Passthrough &pass)
+void InterfaceGenerator::visit(InterfaceBlock &iface)
+{
+       if(iface.interface=="in")
+       {
+               /* Try to link input blocks with output blocks sharing the same block
+               name from previous stage. */
+               if(!iface.linked_block && stage->previous)
+               {
+                       const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
+                       auto i = prev_blocks.find("out "+iface.block_name);
+                       if(i!=prev_blocks.end())
+                       {
+                               iface.linked_block = i->second;
+                               i->second->linked_block = &iface;
+                       }
+               }
+       }
+
+       TraversingVisitor::visit(iface);
+}
+
+void InterfaceGenerator::visit(FunctionDeclaration &func)
 {
-       vector<VariableDeclaration *> pass_vars;
+       SetFlag set_scope(function_scope, true);
+       // Skip parameters because they're not useful here
+       func.body.visit(*this);
+}
 
-       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());
+void InterfaceGenerator::visit(Passthrough &pass)
+{
+       // Pass through all input variables declared so far.
+       vector<VariableDeclaration *> pass_vars = declared_inputs;
 
        if(stage->previous)
        {
-               const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
-               for(map<string, VariableDeclaration *>::const_iterator i=prev_out.begin(); i!=prev_out.end(); ++i)
+               for(const auto &kvp: stage->previous->content.variables)
                {
-                       bool linked = false;
-                       for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
-                               linked = ((*j)->linked_declaration==i->second);
+                       if(kvp.second->interface!="out")
+                               continue;
 
-                       if(!linked && generate_interface(*i->second, "in", i->second->name))
-                               pass_vars.push_back(i->second);
+                       /* Pass through output variables from the previous stage, but only
+                       those which are not already linked to an input here. */
+                       if(!kvp.second->linked_declaration && generate_interface(*kvp.second, "in", kvp.second->name))
+                               pass_vars.push_back(kvp.second);
                }
        }
 
        if(stage->type==Stage::GEOMETRY)
        {
-               VariableReference *ref = new VariableReference;
+               /* Special case for geometry shader: copy gl_Position from input to
+               output. */
+               InterfaceBlockReference *ref = new InterfaceBlockReference;
                ref->name = "gl_in";
 
                BinaryExpression *subscript = new BinaryExpression;
                subscript->left = ref;
-               subscript->oper = "[";
+               subscript->oper = &Operator::get_operator("[", Operator::BINARY);
                subscript->right = pass.subscript;
-               subscript->after = "]";
 
                MemberAccess *memacc = new MemberAccess;
                memacc->left = subscript;
@@ -429,134 +336,26 @@ void InterfaceGenerator::visit(Passthrough &pass)
                insert_assignment("gl_Position", memacc);
        }
 
-       for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
+       for(VariableDeclaration *v: pass_vars)
        {
-               string out_name = change_prefix((*i)->name, out_prefix);
-               generate_interface(**i, "out", out_name);
+               string out_name = change_prefix(v->name, out_prefix);
+               generate_interface(*v, "out", out_name);
 
                VariableReference *ref = new VariableReference;
-               ref->name = (*i)->name;
+               ref->name = v->name;
                if(pass.subscript)
                {
                        BinaryExpression *subscript = new BinaryExpression;
                        subscript->left = ref;
-                       subscript->oper = "[";
+                       subscript->oper = &Operator::get_operator("[", Operator::BINARY);
                        subscript->right = pass.subscript;
-                       subscript->after = "]";
                        insert_assignment(out_name, subscript);
                }
                else
                        insert_assignment(out_name, ref);
        }
 
-       remove_node = true;
-}
-
-
-DeclarationReorderer::DeclarationReorderer():
-       scope_level(0),
-       kind(NO_DECLARATION)
-{ }
-
-void DeclarationReorderer::visit(FunctionCall &call)
-{
-       FunctionDeclaration *def = call.declaration;
-       if(def)
-               def = def->definition;
-       if(def && !ordered_funcs.count(def))
-               needed_funcs.insert(def);
-}
-
-void DeclarationReorderer::visit(Block &block)
-{
-       SetForScope<unsigned> set(scope_level, scope_level+1);
-       if(scope_level>1)
-               return TraversingVisitor::visit(block);
-
-       NodeList<Statement>::iterator struct_insert_point = block.body.end();
-       NodeList<Statement>::iterator variable_insert_point = block.body.end();
-       NodeList<Statement>::iterator function_insert_point = block.body.end();
-       unsigned unordered_func_count = 0;
-       bool ordered_any_funcs = false;
-
-       for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
-       {
-               kind = NO_DECLARATION;
-               (*i)->visit(*this);
-
-               bool moved = false;
-               if(kind==STRUCT && struct_insert_point!=block.body.end())
-               {
-                       block.body.insert(struct_insert_point, *i);
-                       moved = true;
-               }
-               else if(kind>STRUCT && struct_insert_point==block.body.end())
-                       struct_insert_point = i;
-
-               if(kind==VARIABLE && variable_insert_point!=block.body.end())
-               {
-                       block.body.insert(variable_insert_point, *i);
-                       moved = true;
-               }
-               else if(kind>VARIABLE && variable_insert_point==block.body.end())
-                       variable_insert_point = i;
-
-               if(kind==FUNCTION)
-               {
-                       if(function_insert_point==block.body.end())
-                               function_insert_point = i;
-
-                       if(needed_funcs.empty())
-                       {
-                               ordered_funcs.insert(i->get());
-                               if(i!=function_insert_point)
-                               {
-                                       block.body.insert(function_insert_point, *i);
-                                       moved = true;
-                               }
-                               else
-                                       ++function_insert_point;
-                               ordered_any_funcs = true;
-                       }
-                       else
-                               ++unordered_func_count;
-               }
-
-               if(moved)
-               {
-                       if(function_insert_point==i)
-                               ++function_insert_point;
-                       block.body.erase(i++);
-               }
-               else
-                       ++i;
-
-               if(i==block.body.end() && unordered_func_count)
-               {
-                       if(!ordered_any_funcs)
-                               // A subset of the remaining functions forms a recursive loop
-                               /* TODO pick a function and move it up, adding any necessary
-                               declarations */
-                               break;
-
-                       i = function_insert_point;
-                       unordered_func_count = 0;
-               }
-       }
-}
-
-void DeclarationReorderer::visit(VariableDeclaration &var)
-{
-       TraversingVisitor::visit(var);
-       kind = VARIABLE;
-}
-
-void DeclarationReorderer::visit(FunctionDeclaration &func)
-{
-       needed_funcs.clear();
-       func.body.visit(*this);
-       needed_funcs.erase(&func);
-       kind = FUNCTION;
+       nodes_to_remove.insert(&pass);
 }
 
 } // namespace SL