]> 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 09c5c37d3fa2c21fbf6afde8a594e04d1ecfa5f7..6b336f3179de690b2967cd85fe5da0cafff130ab 100644 (file)
@@ -24,20 +24,6 @@ void DeclarationCombiner::visit(Block &block)
        TraversingVisitor::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);
-}
-
 void DeclarationCombiner::visit(VariableDeclaration &var)
 {
        VariableDeclaration *&ptr = variables[var.name];
@@ -113,15 +99,9 @@ void ConstantSpecializer::visit(VariableDeclaration &var)
                {
                        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;
                }
        }
@@ -136,7 +116,6 @@ void BlockHierarchyResolver::enter(Block &block)
 
 VariableResolver::VariableResolver():
        stage(0),
-       builtins(0),
        members(0),
        record_target(false),
        assignment_target(0),
@@ -146,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();
@@ -165,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())
@@ -217,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())
@@ -256,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);
@@ -287,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;
 }
 
@@ -323,29 +297,45 @@ void VariableResolver::visit(InterfaceBlock &iface)
 }
 
 
+void FunctionResolver::apply(Stage &s)
+{
+       stage = &s;
+       s.functions.clear();
+       s.content.visit(*this);
+}
+
 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;
+
                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);
 }
@@ -470,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;
@@ -632,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;
@@ -654,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
@@ -666,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