]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/generate.cpp
Further refactor the resolving process in SL::Compiler
[libs/gl.git] / source / glsl / generate.cpp
index 69a8dfb16d1eb62cccfbbba342418c0868bff69a..4e225ae520569dab6188666ba1bc7a2c026a5bb2 100644 (file)
@@ -1,4 +1,6 @@
+#include <msp/core/hash.h>
 #include <msp/core/raii.h>
+#include <msp/strings/lexicalcast.h>
 #include "builtin.h"
 #include "generate.h"
 
@@ -8,31 +10,18 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
-DeclarationCombiner::DeclarationCombiner():
-       toplevel(true)
-{ }
+void DeclarationCombiner::apply(Stage &stage)
+{
+       stage.content.visit(*this);
+       NodeRemover().apply(stage, nodes_to_remove);
+}
 
 void DeclarationCombiner::visit(Block &block)
 {
-       if(!toplevel)
+       if(current_block)
                return;
 
-       SetForScope<bool> set(toplevel, false);
-       BlockModifier::visit(block);
-}
-
-void DeclarationCombiner::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;
-                       (*i)->body.body.clear();
-               }
-       }
-       decls.push_back(&func);
+       TraversingVisitor::visit(block);
 }
 
 void DeclarationCombiner::visit(VariableDeclaration &var)
@@ -51,8 +40,9 @@ void DeclarationCombiner::visit(VariableDeclaration &var)
                                {
                                        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)
+                                               if(j->name==i->name)
                                                {
+                                                       j->has_value = i->value;
                                                        j->value = i->value;
                                                        found = true;
                                                }
@@ -64,102 +54,321 @@ void DeclarationCombiner::visit(VariableDeclaration &var)
                        else
                                ptr->layout = var.layout;
                }
-               remove_node = true;
+               nodes_to_remove.insert(&var);
        }
        else
                ptr = &var;
 }
 
 
+ConstantSpecializer::ConstantSpecializer():
+       values(0)
+{ }
+
+void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
+{
+       values = v;
+       stage.content.visit(*this);
+}
+
+void ConstantSpecializer::visit(VariableDeclaration &var)
+{
+       bool specializable = false;
+       if(var.layout)
+       {
+               vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
+               for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
+                       if(i->name=="constant_id")
+                       {
+                               specializable = true;
+                               if(values)
+                                       qualifiers.erase(i);
+                               else if(i->value==-1)
+                                       i->value = hash32(var.name)&0x7FFFFFFF;
+                               break;
+                       }
+
+               if(qualifiers.empty())
+                       var.layout = 0;
+       }
+
+       if(specializable && values)
+       {
+               map<string, int>::const_iterator i = values->find(var.name);
+               if(i!=values->end())
+               {
+                       RefPtr<Literal> literal = new Literal;
+                       if(var.type=="bool")
+                       {
+                               literal->token = (i->second ? "true" : "false");
+                               literal->value = static_cast<bool>(i->second);
+                       }
+                       else if(var.type=="int")
+                       {
+                               literal->token = lexical_cast<string>(i->second);
+                               literal->value = i->second;
+                       }
+                       var.init_expression = literal;
+               }
+       }
+}
+
+
+void BlockHierarchyResolver::enter(Block &block)
+{
+       r_any_resolved |= (current_block!=block.parent);
+       block.parent = current_block;
+}
+
+
+TypeResolver::TypeResolver():
+       stage(0),
+       r_any_resolved(false)
+{ }
+
+bool TypeResolver::apply(Stage &s)
+{
+       stage = &s;
+       s.types.clear();
+       r_any_resolved = false;
+       s.content.visit(*this);
+       return r_any_resolved;
+}
+
+TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
+{
+       map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(&type);
+       if(i!=array_types.end())
+               return i->second;
+
+       BasicTypeDeclaration *array = new BasicTypeDeclaration;
+       array->source = BUILTIN_SOURCE;
+       array->name = type.name+"[]";
+       array->kind = BasicTypeDeclaration::ARRAY;
+       array->base = type.name;
+       array->base_type = &type;
+       stage->content.body.insert(type_insert_point, array);
+       array_types[&type] = array;
+       return array;
+}
+
+void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array)
+{
+       TypeDeclaration *resolved = 0;
+       map<string, TypeDeclaration *>::iterator i = stage->types.find(name);
+       if(i!=stage->types.end())
+       {
+               map<TypeDeclaration *, TypeDeclaration *>::iterator j = alias_map.find(i->second);
+               resolved = (j!=alias_map.end() ? j->second : i->second);
+       }
+
+       if(resolved && array)
+               resolved = get_or_create_array_type(*resolved);
+
+       r_any_resolved |= (resolved!=type);
+       type=resolved;
+}
+
+void TypeResolver::visit(Block &block)
+{
+       for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+       {
+               if(!block.parent)
+                       type_insert_point = i;
+               (*i)->visit(*this);
+       }
+}
+
+void TypeResolver::visit(BasicTypeDeclaration &type)
+{
+       resolve_type(type.base_type, type.base, false);
+
+       if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type)
+               if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
+                       if(basic_base->kind==BasicTypeDeclaration::VECTOR)
+                       {
+                               type.kind = BasicTypeDeclaration::MATRIX;
+                               type.size |= basic_base->size<<16;
+                       }
+
+       if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
+               alias_map[&type] = type.base_type;
+       else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type)
+               array_types[type.base_type] = &type;
+
+       stage->types.insert(make_pair(type.name, &type));
+}
+
+void TypeResolver::visit(ImageTypeDeclaration &type)
+{
+       resolve_type(type.base_type, type.base, false);
+       stage->types.insert(make_pair(type.name, &type));
+}
+
+void TypeResolver::visit(StructDeclaration &strct)
+{
+       stage->types.insert(make_pair(strct.name, &strct));
+       TraversingVisitor::visit(strct);
+}
+
+void TypeResolver::visit(VariableDeclaration &var)
+{
+       resolve_type(var.type_declaration, var.type, var.array);
+}
+
+void TypeResolver::visit(FunctionDeclaration &func)
+{
+       resolve_type(func.return_type_declaration, func.return_type, false);
+       TraversingVisitor::visit(func);
+}
+
+
 VariableResolver::VariableResolver():
-       anonymous(false),
+       stage(0),
+       r_any_resolved(false),
        record_target(false),
-       assignment_target(0),
-       self_referencing(false)
+       r_self_referencing(false),
+       r_assignment_target(0)
 { }
 
-void VariableResolver::apply(Stage &s)
+bool VariableResolver::apply(Stage &s)
 {
-       SetForScope<Stage *> set(stage, &s);
-       Stage *builtins = get_builtins(stage->type);
-       if(builtins)
-               blocks.push_back(&builtins->content);
-       stage->content.visit(*this);
-       if(builtins)
-               blocks.pop_back();
+       stage = &s;
+       s.interface_blocks.clear();
+       r_any_resolved = false;
+       s.content.visit(*this);
+       return r_any_resolved;
 }
 
-void VariableResolver::visit(Block &block)
+void VariableResolver::enter(Block &block)
 {
-       blocks.push_back(&block);
        block.variables.clear();
-       TraversingVisitor::visit(block);
-       blocks.pop_back();
+}
+
+void VariableResolver::visit_and_replace(RefPtr<Expression> &expr)
+{
+       r_replacement_expr = 0;
+       expr->visit(*this);
+       if(r_replacement_expr)
+               expr = r_replacement_expr;
+       r_replacement_expr = 0;
 }
 
 void VariableResolver::visit(VariableReference &var)
 {
-       var.declaration = 0;
-       type = 0;
-       for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
+       VariableDeclaration *declaration = 0;
+
+       /* Look for variable declarations in the block hierarchy first.  Interface
+       blocks are always defined in the top level so we can't accidentally skip
+       one. */
+       for(Block *block=current_block; (!declaration && block); block=block->parent)
+       {
+               map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
+               if(i!=block->variables.end())
+                       declaration = i->second;
+       }
+
+       if(!declaration)
        {
-               --i;
-               map<string, VariableDeclaration *>::iterator j = (*i)->variables.find(var.name);
-               if(j!=(*i)->variables.end())
+               const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
+               map<string, InterfaceBlock *>::const_iterator i = blocks.find("_"+var.name);
+               if(i!=blocks.end())
                {
-                       var.declaration = j->second;
-                       type = j->second->type_declaration;
-                       break;
+                       /* The name refers to an interface block with an instance name rather
+                       than a variable.  Prepare a new syntax tree node accordingly. */
+                       InterfaceBlockReference *iface_ref = new InterfaceBlockReference;
+                       iface_ref->source = var.source;
+                       iface_ref->line = var.line;
+                       iface_ref->name = var.name;
+                       iface_ref->declaration = i->second;
+                       r_replacement_expr = iface_ref;
+               }
+               else
+               {
+                       // Look for the variable in anonymous interface blocks.
+                       for(i=blocks.begin(); (!declaration && i!=blocks.end()); ++i)
+                               if(i->second->instance_name.empty())
+                               {
+                                       map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
+                                       if(j!=i->second->members.variables.end())
+                                               declaration = j->second;
+                               }
                }
        }
 
+       r_any_resolved |= (declaration!=var.declaration);
+       var.declaration = declaration;
+
        if(record_target)
        {
-               if(assignment_target)
+               if(r_assignment_target)
                {
+                       /* More than one variable reference found in assignment target.
+                       Unable to determine what the primary target is. */
                        record_target = false;
-                       assignment_target = 0;
+                       r_assignment_target = 0;
                }
                else
-                       assignment_target = var.declaration;
+                       r_assignment_target = var.declaration;
        }
-       else if(var.declaration && var.declaration==assignment_target)
-               self_referencing = true;
+       else if(var.declaration && var.declaration==r_assignment_target)
+               r_self_referencing = true;
+}
+
+void VariableResolver::visit(InterfaceBlockReference &iface)
+{
+       map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find("_"+iface.name);
+       InterfaceBlock *declaration = (i!=stage->interface_blocks.end() ? i->second : 0);
+       r_any_resolved |= (declaration!=iface.declaration);
+       iface.declaration = declaration;
 }
 
 void VariableResolver::visit(MemberAccess &memacc)
 {
-       type = 0;
-       TraversingVisitor::visit(memacc);
-       memacc.declaration = 0;
-       if(type)
+       visit_and_replace(memacc.left);
+
+       map<string, VariableDeclaration *> *members = 0;
+       if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
+               members = &strct->members.variables;
+       else if(InterfaceBlockReference *iface_ref = dynamic_cast<InterfaceBlockReference *>(memacc.left.get()))
        {
-               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;
+               if(iface_ref->declaration)
+                       members = &iface_ref->declaration->members.variables;
+       }
+
+       VariableDeclaration *declaration = 0;
+       if(members)
+       {
+               map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
+               if(i!=members->end())
+                       declaration = i->second;
        }
+
+       r_any_resolved |= (declaration!=memacc.declaration);
+       memacc.declaration = declaration;
+}
+
+void VariableResolver::visit(UnaryExpression &unary)
+{
+       visit_and_replace(unary.expression);
 }
 
 void VariableResolver::visit(BinaryExpression &binary)
 {
-       if(binary.oper=="[")
+       if(binary.oper->token[0]=='[')
        {
                {
-                       SetForScope<bool> set(record_target, false);
-                       binary.right->visit(*this);
+                       /* The subscript expression is not a part of the primary assignment
+                       target. */
+                       SetFlag set(record_target, false);
+                       visit_and_replace(binary.right);
                }
-               type = 0;
-               binary.left->visit(*this);
+               visit_and_replace(binary.left);
        }
        else
        {
-               TraversingVisitor::visit(binary);
-               type = 0;
+               visit_and_replace(binary.left);
+               visit_and_replace(binary.right);
        }
 }
 
@@ -167,80 +376,489 @@ void VariableResolver::visit(Assignment &assign)
 {
        {
                SetFlag set(record_target);
-               assignment_target = 0;
-               assign.left->visit(*this);
+               r_assignment_target = 0;
+               visit_and_replace(assign.left);
+               r_any_resolved |= (r_assignment_target!=assign.target_declaration);
+               assign.target_declaration = r_assignment_target;
        }
 
-       self_referencing = false;
-       assign.right->visit(*this);
-
-       assign.self_referencing = (self_referencing || assign.oper!="=");
-       assign.target_declaration = assignment_target;
+       r_self_referencing = false;
+       visit_and_replace(assign.right);
+       assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
 }
 
-void VariableResolver::visit(StructDeclaration &strct)
+void VariableResolver::visit(FunctionCall &call)
 {
-       TraversingVisitor::visit(strct);
-       blocks.back()->types[strct.name] = &strct;
+       for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
+               visit_and_replace(*i);
 }
 
 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;
+       current_block->variables.insert(make_pair(var.name, &var));
 }
 
 void VariableResolver::visit(InterfaceBlock &iface)
 {
-       SetFlag set(anonymous);
-       SetForScope<string> set2(block_interface, iface.interface);
+       /* Block names can be reused in different interfaces.  Prefix the name with
+       the first character of the interface to avoid conflicts. */
+       stage->interface_blocks.insert(make_pair(iface.interface+iface.name, &iface));
+       if(!iface.instance_name.empty())
+               stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
+
+       SetForScope<string> set_iface(block_interface, iface.interface);
        TraversingVisitor::visit(iface);
 }
 
 
+ExpressionResolver::ExpressionResolver():
+       stage(0),
+       r_any_resolved(false)
+{ }
+
+bool ExpressionResolver::apply(Stage &s)
+{
+       stage = &s;
+       r_any_resolved = false;
+       s.content.visit(*this);
+       return r_any_resolved;
+}
+
+bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type)
+{
+       return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
+}
+
+bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type)
+{
+       return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
+}
+
+BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type)
+{
+       if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY)
+       {
+               BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
+               return (basic_base ? get_element_type(*basic_base) : 0);
+       }
+       else
+               return &type;
+}
+
+bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to)
+{
+       if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT)
+               return from.size<=to.size;
+       else if(from.kind!=to.kind)
+               return false;
+       else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size)
+       {
+               BasicTypeDeclaration *from_base = dynamic_cast<BasicTypeDeclaration *>(from.base_type);
+               BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(to.base_type);
+               return (from_base && to_base && can_convert(*from_base, *to_base));
+       }
+       else
+               return false;
+}
+
+ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right)
+{
+       if(&left==&right)
+               return SAME_TYPE;
+       else if(can_convert(left, right))
+               return LEFT_CONVERTIBLE;
+       else if(can_convert(right, left))
+               return RIGHT_CONVERTIBLE;
+       else
+               return NOT_COMPATIBLE;
+}
+
+BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size)
+{
+       for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
+               if((*i)->kind==kind && (*i)->size==size)
+                       return *i;
+       return 0;
+}
+
+BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size)
+{
+       for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
+               if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size)
+                       return *i;
+       return 0;
+}
+
+void ExpressionResolver::convert_to(RefPtr<Expression> &expr, BasicTypeDeclaration &type)
+{
+       RefPtr<FunctionCall> call = new FunctionCall;
+       call->name = type.name;
+       call->constructor = true;
+       call->arguments.push_back(0);
+       call->arguments.back() = expr;
+       call->type = &type;
+       expr = call;
+}
+
+bool ExpressionResolver::convert_to_element(RefPtr<Expression> &expr, BasicTypeDeclaration &elem_type)
+{
+       if(BasicTypeDeclaration *expr_type = dynamic_cast<BasicTypeDeclaration *>(expr->type))
+       {
+               BasicTypeDeclaration *to_type = &elem_type;
+               if(is_vector_or_matrix(*expr_type))
+                       to_type = find_type(elem_type, expr_type->kind, expr_type->size);
+               if(to_type)
+               {
+                       convert_to(expr, *to_type);
+                       return true;
+               }
+       }
+
+       return false;
+}
+
+void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool lvalue)
+{
+       r_any_resolved |= (type!=expr.type || lvalue!=expr.lvalue);
+       expr.type = type;
+       expr.lvalue = lvalue;
+}
+
+void ExpressionResolver::visit(Literal &literal)
+{
+       if(literal.value.check_type<bool>())
+               resolve(literal, find_type(BasicTypeDeclaration::BOOL, 1), false);
+       else if(literal.value.check_type<int>())
+               resolve(literal, find_type(BasicTypeDeclaration::INT, 32), false);
+       else if(literal.value.check_type<float>())
+               resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false);
+}
+
+void ExpressionResolver::visit(ParenthesizedExpression &parexpr)
+{
+       TraversingVisitor::visit(parexpr);
+       resolve(parexpr, parexpr.expression->type, parexpr.expression->lvalue);
+}
+
+void ExpressionResolver::visit(VariableReference &var)
+{
+       if(var.declaration)
+               resolve(var, var.declaration->type_declaration, true);
+}
+
+void ExpressionResolver::visit(InterfaceBlockReference &iface)
+{
+       resolve(iface, 0, true);
+}
+
+void ExpressionResolver::visit(MemberAccess &memacc)
+{
+       TraversingVisitor::visit(memacc);
+
+       if(memacc.declaration)
+               resolve(memacc, memacc.declaration->type_declaration, memacc.left->lvalue);
+}
+
+void ExpressionResolver::visit(UnaryExpression &unary)
+{
+       TraversingVisitor::visit(unary);
+
+       BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(unary.expression->type);
+       if(!basic)
+               return;
+
+       char oper = unary.oper->token[0];
+       if(oper=='!')
+       {
+               if(basic->kind!=BasicTypeDeclaration::BOOL)
+                       return;
+       }
+       else if(oper=='~')
+       {
+               if(basic->kind!=BasicTypeDeclaration::INT)
+                       return;
+       }
+       else if(oper=='+' || oper=='-')
+       {
+               BasicTypeDeclaration *elem = get_element_type(*basic);
+               if(!elem || !is_scalar(*elem))
+                       return;
+       }
+       resolve(unary, basic, unary.expression->lvalue);
+}
+
+void ExpressionResolver::visit(BinaryExpression &binary, bool assign)
+{
+       /* Binary operators are only defined for basic types (not for image or
+       structure types). */
+       BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(binary.left->type);
+       BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(binary.right->type);
+       if(!basic_left || !basic_right)
+               return;
+
+       char oper = binary.oper->token[0];
+       if(oper=='[')
+       {
+               /* Subscripting operates on vectors, matrices and arrays, and the right
+               operand must be an integer. */
+               if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT)
+                       return;
+
+               resolve(binary, basic_left->base_type, binary.left->lvalue);
+               return;
+       }
+       else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY)
+               // No other binary operator can be used with arrays.
+               return;
+
+       BasicTypeDeclaration *elem_left = get_element_type(*basic_left);
+       BasicTypeDeclaration *elem_right = get_element_type(*basic_right);
+       if(!elem_left || !elem_right)
+               return;
+
+       Compatibility compat = get_compatibility(*basic_left, *basic_right);
+       Compatibility elem_compat = get_compatibility(*elem_left, *elem_right);
+       if(elem_compat==NOT_COMPATIBLE)
+               return;
+       if(assign && (compat==LEFT_CONVERTIBLE || elem_compat==LEFT_CONVERTIBLE))
+               return;
+
+       TypeDeclaration *type = 0;
+       char oper2 = binary.oper->token[1];
+       if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>'))
+       {
+               /* Relational operators compare two scalar integer or floating-point
+               values. */
+               if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE)
+                       return;
+
+               type = find_type(BasicTypeDeclaration::BOOL, 1);
+       }
+       else if((oper=='=' || oper=='!') && oper2=='=')
+       {
+               // Equality comparison can be done on any compatible types.
+               if(compat==NOT_COMPATIBLE)
+                       return;
+
+               type = find_type(BasicTypeDeclaration::BOOL, 1);
+       }
+       else if(oper2=='&' || oper2=='|' || oper2=='^')
+       {
+               // Logical operators can only be applied to booleans.
+               if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL)
+                       return;
+
+               type = basic_left;
+       }
+       else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2)
+       {
+               // Bitwise operators and modulo can only be applied to integers.
+               if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
+                       return;
+
+               type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left);
+       }
+       else if((oper=='<' || oper=='>') && oper2==oper)
+       {
+               // Shifts apply to integer scalars and vectors, with some restrictions.
+               if(elem_left->kind!=BasicTypeDeclaration::INT || elem_right->kind!=BasicTypeDeclaration::INT)
+                       return;
+               unsigned left_size = (basic_left->kind==BasicTypeDeclaration::INT ? 1 : basic_left->kind==BasicTypeDeclaration::VECTOR ? basic_left->size : 0);
+               unsigned right_size = (basic_right->kind==BasicTypeDeclaration::INT ? 1 : basic_right->kind==BasicTypeDeclaration::VECTOR ? basic_right->size : 0);
+               if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size))
+                       return;
+
+               type = basic_left;
+               // Don't perform conversion even if the operands are of different sizes.
+               compat = SAME_TYPE;
+       }
+       else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
+       {
+               // Arithmetic operators require scalar elements.
+               if(!is_scalar(*elem_left) || !is_scalar(*elem_right))
+                       return;
+
+               if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) &&
+                       (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX))
+               {
+                       /* Multiplication has special rules when at least one operand is a
+                       matrix and the other is a vector or a matrix. */
+                       unsigned left_columns = basic_left->size&0xFFFF;
+                       unsigned right_rows = basic_right->size;
+                       if(basic_right->kind==BasicTypeDeclaration::MATRIX)
+                               right_rows >>= 16;
+                       if(left_columns!=right_rows)
+                               return;
+
+                       BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left);
+
+                       if(basic_left->kind==BasicTypeDeclaration::VECTOR)
+                               type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF);
+                       else if(basic_right->kind==BasicTypeDeclaration::VECTOR)
+                               type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16);
+                       else
+                               type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF));
+               }
+               else if(compat==NOT_COMPATIBLE)
+               {
+                       // Arithmetic between scalars and matrices or vectors is supported.
+                       if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right))
+                               type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right);
+                       else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right))
+                               type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left);
+                       else
+                               return;
+               }
+               else if(compat==LEFT_CONVERTIBLE)
+                       type = basic_right;
+               else
+                       type = basic_left;
+       }
+       else
+               return;
+
+       if(assign && type!=basic_left)
+               return;
+
+       bool converted = true;
+       if(compat==LEFT_CONVERTIBLE)
+               convert_to(binary.left, *basic_right);
+       else if(compat==RIGHT_CONVERTIBLE)
+               convert_to(binary.right, *basic_left);
+       else if(elem_compat==LEFT_CONVERTIBLE)
+               converted = convert_to_element(binary.left, *elem_right);
+       else if(elem_compat==RIGHT_CONVERTIBLE)
+               converted = convert_to_element(binary.right, *elem_left);
+
+       if(!converted)
+               type = 0;
+
+       resolve(binary, type, assign);
+}
+
+void ExpressionResolver::visit(BinaryExpression &binary)
+{
+       TraversingVisitor::visit(binary);
+       visit(binary, false);
+}
+
+void ExpressionResolver::visit(Assignment &assign)
+{
+       TraversingVisitor::visit(assign);
+
+       if(assign.oper->token[0]!='=')
+               return visit(assign, true);
+       else if(assign.left->type!=assign.right->type)
+       {
+               BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(assign.left->type);
+               BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(assign.right->type);
+               if(!basic_left || !basic_right)
+                       return;
+
+               Compatibility compat = get_compatibility(*basic_left, *basic_right);
+               if(compat==RIGHT_CONVERTIBLE)
+                       convert_to(assign.right, *basic_left);
+               else if(compat!=SAME_TYPE)
+                       return;
+       }
+
+       resolve(assign, assign.left->type, true);
+}
+
+void ExpressionResolver::visit(FunctionCall &call)
+{
+       TraversingVisitor::visit(call);
+
+       TypeDeclaration *type = 0;
+       if(call.declaration)
+               type = call.declaration->return_type_declaration;
+       else if(call.constructor)
+       {
+               map<string, TypeDeclaration *>::const_iterator i=stage->types.find(call.name);
+               type = (i!=stage->types.end() ? i->second : 0);
+       }
+       resolve(call, type, false);
+}
+
+void ExpressionResolver::visit(BasicTypeDeclaration &type)
+{
+       basic_types.push_back(&type);
+}
+
+void ExpressionResolver::visit(VariableDeclaration &var)
+{
+       TraversingVisitor::visit(var);
+       if(!var.init_expression)
+               return;
+
+       BasicTypeDeclaration *var_basic = dynamic_cast<BasicTypeDeclaration *>(var.type_declaration);
+       BasicTypeDeclaration *init_basic = dynamic_cast<BasicTypeDeclaration *>(var.init_expression->type);
+       if(!var_basic || !init_basic)
+               return;
+
+       Compatibility compat = get_compatibility(*var_basic, *init_basic);
+       if(compat==RIGHT_CONVERTIBLE)
+               convert_to(var.init_expression, *var_basic);
+}
+
+
+bool FunctionResolver::apply(Stage &s)
+{
+       stage = &s;
+       s.functions.clear();
+       r_any_resolved = false;
+       s.content.visit(*this);
+       return r_any_resolved;
+}
+
 void FunctionResolver::visit(FunctionCall &call)
 {
-       map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
-       if(i!=functions.end())
-               call.declaration = i->second.back();
+       map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
+       if(i!=stage->functions.end())
+               call.declaration = i->second;
 
        TraversingVisitor::visit(call);
 }
 
 void FunctionResolver::visit(FunctionDeclaration &func)
 {
-       vector<FunctionDeclaration *> &decls = functions[func.name];
-       if(func.definition)
+       FunctionDeclaration *&stage_decl = stage->functions[func.name];
+       vector<FunctionDeclaration *> &decls = declarations[func.name];
+       if(func.definition==&func)
        {
+               stage_decl = &func;
+
+               // Set all previous declarations to use this definition.
                for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
+               {
                        (*i)->definition = func.definition;
-               decls.clear();
-               decls.push_back(&func);
+                       (*i)->body.body.clear();
+               }
        }
-       else if(!decls.empty() && decls.back()->definition)
-               func.definition = decls.back()->definition;
        else
-               decls.push_back(&func);
+       {
+               func.definition = 0;
+               if(!stage_decl)
+                       stage_decl = &func;
+               else
+                       func.definition = stage_decl->definition;
+       }
+       decls.push_back(&func);
 
        TraversingVisitor::visit(func);
 }
 
 
 InterfaceGenerator::InterfaceGenerator():
-       scope_level(0)
+       stage(0),
+       function_scope(false),
+       iface_block(0),
+       copy_block(false),
+       iface_target_block(0)
 { }
 
 string InterfaceGenerator::get_out_prefix(Stage::Type type)
@@ -255,31 +873,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);
-       stage->content.visit(*this);
+       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(NodeList<Statement>::iterator 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 +901,70 @@ 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;
 
        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;
+       }
 
-       return true;
+       iface_target_block->body.insert(iface_insert_point, iface_var);
+       iface_target_block->variables.insert(make_pair(name, iface_var));
+
+       return iface_var;
+}
+
+InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
+{
+       if(stage->interface_blocks.count("in"+out_block.name))
+               return 0;
+
+       InterfaceBlock *in_block = new InterfaceBlock;
+       in_block->interface = "in";
+       in_block->name = out_block.name;
+       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);
+               SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
+               out_block.members.visit(*this);
+       }
+
+       iface_target_block->body.insert(iface_insert_point, in_block);
+       stage->interface_blocks.insert(make_pair("in"+in_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 +973,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 +988,78 @@ 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;
+       map<string, VariableDeclaration *>::const_iterator 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;
+               return;
+       }
+
+       const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
+       map<string, InterfaceBlock *>::const_iterator 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(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
+               if(j->second->instance_name.empty())
+               {
+                       i = j->second->members.variables.find(var.name);
+                       if(i!=j->second->members.variables.end())
+                       {
+                               generate_interface(*j->second);
+                               return;
+                       }
+               }
 }
 
 void InterfaceGenerator::visit(VariableDeclaration &var)
 {
+       if(copy_block)
+       {
+               generate_interface(var, "in", var.name);
+               return;
+       }
+
+       if(iface_block)
+       {
+               if(iface_block->linked_block)
+               {
+                       // Link all variables to their counterparts in the linked block.
+                       const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
+                       map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
+                       if(i!=linked_vars.end())
+                       {
+                               var.linked_declaration = i->second;
+                               var.linked_declaration->linked_declaration = &var;
+                       }
+               }
+               return;
+       }
+
        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());
@@ -369,14 +1071,13 @@ void InterfaceGenerator::visit(VariableDeclaration &var)
        }
        else if(var.interface=="in")
        {
-               stage->in_variables[var.name] = &var;
-               if(var.linked_declaration)
-                       var.linked_declaration->linked_declaration = &var;
-               else if(stage->previous)
+               /* Try to link input variables in global scope with output variables from
+               previous stage. */
+               if(current_block==&stage->content && !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;
+                       map<string, VariableDeclaration *>::const_iterator 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 +1088,70 @@ void InterfaceGenerator::visit(VariableDeclaration &var)
        TraversingVisitor::visit(var);
 }
 
+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;
+                       map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.name);
+                       if(i!=prev_blocks.end())
+                       {
+                               iface.linked_block = i->second;
+                               i->second->linked_block = &iface;
+                       }
+               }
+       }
+
+       SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
+       TraversingVisitor::visit(iface);
+}
+
+void InterfaceGenerator::visit(FunctionDeclaration &func)
+{
+       SetFlag set_scope(function_scope, true);
+       // Skip parameters because they're not useful here
+       func.body.visit(*this);
+}
+
 void InterfaceGenerator::visit(Passthrough &pass)
 {
        vector<VariableDeclaration *> pass_vars;
 
-       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)
+       // Pass through all input variables of this stage.
+       for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
                if(i->second->interface=="in")
-                       pass_vars.push_back(i->second.get());
+                       pass_vars.push_back(i->second);
 
        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)
+               const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
+               for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
                {
-                       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(i->second->interface!="out")
+                               continue;
 
-                       if(!linked && generate_interface(*i->second, "in", i->second->name))
+                       /* Pass through output variables from the previous stage, but only
+                       those which are not already linked to an input here. */
+                       if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
                                pass_vars.push_back(i->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;
@@ -440,123 +1171,15 @@ void InterfaceGenerator::visit(Passthrough &pass)
                {
                        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 StageVisitor::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)
-{
-       StageVisitor::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