]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programcompiler.cpp
Throw an exception if Texture*::allocate is called before storage
[libs/gl.git] / source / programcompiler.cpp
index fbcc6598d3bcd5e7e74bf50bdf3762e51de9e343..5bb84269f30abe4fcd38198cef0b6a2ae416b2f8 100644 (file)
@@ -208,6 +208,7 @@ void ProgramCompiler::generate(Stage &stage)
        apply<InterfaceGenerator>(stage);
        apply<VariableResolver>(stage);
        apply<DeclarationReorderer>(stage);
+       apply<FunctionResolver>(stage);
        apply<LegacyConverter>(stage);
 }
 
@@ -987,13 +988,30 @@ void ProgramCompiler::InterfaceGenerator::visit(Passthrough &pass)
 
 
 ProgramCompiler::DeclarationReorderer::DeclarationReorderer():
+       scope_level(0),
        kind(NO_DECLARATION)
 { }
 
+void ProgramCompiler::DeclarationReorderer::visit(FunctionCall &call)
+{
+       FunctionDeclaration *def = call.declaration;
+       if(def)
+               def = def->definition;
+       if(def && !ordered_funcs.count(def))
+               needed_funcs.insert(def);
+}
+
 void ProgramCompiler::DeclarationReorderer::visit(Block &block)
 {
+       SetForScope<unsigned> set(scope_level, scope_level+1);
+       if(scope_level>1)
+               return Visitor::visit(block);
+
        list<RefPtr<Node> >::iterator struct_insert_point = block.body.end();
        list<RefPtr<Node> >::iterator variable_insert_point = block.body.end();
+       list<RefPtr<Node> >::iterator function_insert_point = block.body.end();
+       unsigned unordered_func_count = 0;
+       bool ordered_any_funcs = false;
 
        for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
        {
@@ -1017,13 +1035,64 @@ void ProgramCompiler::DeclarationReorderer::visit(Block &block)
                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 ProgramCompiler::DeclarationReorderer::visit(ProgramSyntax::VariableDeclaration &var)
+{
+       Visitor::visit(var);
+       kind = VARIABLE;
+}
+
+void ProgramCompiler::DeclarationReorderer::visit(FunctionDeclaration &func)
+{
+       needed_funcs.clear();
+       func.body.visit(*this);
+       needed_funcs.erase(&func);
+       kind = FUNCTION;
+}
+
 
 ProgramCompiler::InlineableFunctionLocator::InlineableFunctionLocator():
        in_function(0)