]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/generate.cpp
Store a pointer to operator info rather than the token in expressions
[libs/gl.git] / source / glsl / generate.cpp
index f93446b90a7d05dcbf50482a42b3aefe83710d46..6b336f3179de690b2967cd85fe5da0cafff130ab 100644 (file)
@@ -116,7 +116,6 @@ void BlockHierarchyResolver::enter(Block &block)
 
 VariableResolver::VariableResolver():
        stage(0),
-       builtins(0),
        members(0),
        record_target(false),
        assignment_target(0),
@@ -126,16 +125,11 @@ VariableResolver::VariableResolver():
 void VariableResolver::apply(Stage &s)
 {
        stage = &s;
-       Stage *builtin_stage = get_builtins(s.type);
-       builtins = (builtin_stage ? &builtin_stage->content : 0);
+       s.types.clear();
+       s.interface_blocks.clear();
        s.content.visit(*this);
 }
 
-Block *VariableResolver::next_block(Block &block)
-{
-       return block.parent ? block.parent : &block!=builtins ? builtins : 0;
-}
-
 void VariableResolver::enter(Block &block)
 {
        block.variables.clear();
@@ -145,7 +139,7 @@ void VariableResolver::visit(VariableReference &var)
 {
        var.declaration = 0;
        members = 0;
-       for(Block *block=current_block; (!var.declaration && block); block=next_block(*block))
+       for(Block *block=current_block; (!var.declaration && block); block=block->parent)
        {
                map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
                if(i!=block->variables.end())
@@ -197,7 +191,7 @@ void VariableResolver::visit(VariableReference &var)
 void VariableResolver::visit(InterfaceBlockReference &iface)
 {
        iface.declaration = 0;
-       for(Block *block=current_block; block; block=next_block(*block))
+       for(Block *block=current_block; block; block=block->parent)
        {
                map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
                if(i!=stage->interface_blocks.end())
@@ -236,7 +230,7 @@ void VariableResolver::visit(MemberAccess &memacc)
 
 void VariableResolver::visit(BinaryExpression &binary)
 {
-       if(binary.oper=="[")
+       if(binary.oper->token[0]=='[')
        {
                {
                        SetForScope<bool> set(record_target, false);
@@ -267,7 +261,7 @@ void VariableResolver::visit(Assignment &assign)
        self_referencing = false;
        assign.right->visit(*this);
 
-       assign.self_referencing = (self_referencing || assign.oper!="=");
+       assign.self_referencing = (self_referencing || assign.oper->token[0]!='=');
        assign.target_declaration = assignment_target;
 }
 
@@ -306,6 +300,7 @@ void VariableResolver::visit(InterfaceBlock &iface)
 void FunctionResolver::apply(Stage &s)
 {
        stage = &s;
+       s.functions.clear();
        s.content.visit(*this);
 }
 
@@ -334,10 +329,11 @@ void FunctionResolver::visit(FunctionDeclaration &func)
        }
        else
        {
+               func.definition = 0;
                if(!stage_decl)
                        stage_decl = &func;
-
-               func.definition = stage_decl->definition;
+               else
+                       func.definition = stage_decl->definition;
        }
        decls.push_back(&func);
 
@@ -464,7 +460,7 @@ 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;
@@ -626,9 +622,8 @@ 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 = "]";
 
                MemberAccess *memacc = new MemberAccess;
                memacc->left = subscript;
@@ -648,9 +643,8 @@ 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
@@ -660,111 +654,6 @@ void InterfaceGenerator::visit(Passthrough &pass)
        nodes_to_remove.insert(&pass);
 }
 
-
-DeclarationReorderer::DeclarationReorderer():
-       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)
-{
-       if(block.parent)
-               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;
-}
-
 } // namespace SL
 } // namespace GL
 } // namespace Msp