]> git.tdb.fi Git - libs/gl.git/commitdiff
Store types and interface blocks in Stage
authorMikko Rasa <tdb@tdb.fi>
Thu, 25 Feb 2021 23:03:36 +0000 (01:03 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 25 Feb 2021 23:21:10 +0000 (01:21 +0200)
They're only allowed at top level so it seems pointless for every block
to have the maps.

source/glsl/debug.cpp
source/glsl/generate.cpp
source/glsl/generate.h
source/glsl/syntax.h
source/glsl/visitor.cpp

index 62cf7a94701ec04169c39748fbad6c84b46d58d2..c29a60ceab724c6b19069538d38aa3ad144b7d29 100644 (file)
@@ -13,6 +13,20 @@ const std::string &DumpTree::apply(Stage &stage)
        formatted = format("Stage: %s\n", Stage::get_stage_name(stage.type));
        tree.push_back(BRANCH);
        append(format("Version: %d.%02d", stage.required_features.glsl_version.major, stage.required_features.glsl_version.minor));
+
+       for(std::map<string, StructDeclaration *>::const_iterator i=stage.types.begin(); i!=stage.types.end(); ++i)
+               append(format("Type: %%%d %s", get_label(*i->second), i->first));
+
+       set<InterfaceBlock *> seen_interfaces;
+       for(std::map<string, InterfaceBlock *>::const_iterator i=stage.interface_blocks.begin(); i!=stage.interface_blocks.end(); ++i)
+               if(seen_interfaces.insert(i->second).second)
+               {
+                       string text = format("Interface block: %%%d %s %s", get_label(*i->second), i->second->interface, i->second->name);
+                       if(!i->second->instance_name.empty())
+                               text += format(" %s", i->second->instance_name);
+                       append(text);
+               }
+
        last_branch();
        stage.content.visit(*this);
        return formatted;
@@ -79,23 +93,10 @@ void DumpTree::visit(Block &block)
        append(format("Block %s", (block.use_braces ? "{}" : "(inline)")));
        begin_sub();
 
-       for(std::map<string, StructDeclaration *>::const_iterator i=block.types.begin(); i!=block.types.end(); ++i)
-               append(format("Type: %%%d %s", get_label(*i->second), i->first));
-
        for(std::map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
                append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
 
-       set<InterfaceBlock *> seen_interfaces;
-       for(std::map<string, InterfaceBlock *>::const_iterator i=block.interfaces.begin(); i!=block.interfaces.end(); ++i)
-               if(seen_interfaces.insert(i->second).second)
-               {
-                       string text = format("Interface: %%%d %s %s", get_label(*i->second), i->second->interface, i->second->name);
-                       if(!i->second->instance_name.empty())
-                               text += format(" %s", i->second->instance_name);
-                       append(text);
-               }
-
-       bool labeled_body = (!block.types.empty() || !block.variables.empty());
+       bool labeled_body = !block.variables.empty();
        if(labeled_body)
        {
                last_branch();
index 751043d9231a64d4c72af4894ec29c39b8143d48..c073d870ac4d779591fa2c896724737a5de278ca 100644 (file)
@@ -80,6 +80,7 @@ void BlockResolver::enter(Block &block)
 
 
 VariableResolver::VariableResolver():
+       stage(0),
        builtins(0),
        members(0),
        record_target(false),
@@ -87,11 +88,12 @@ VariableResolver::VariableResolver():
        self_referencing(false)
 { }
 
-void VariableResolver::apply(Stage &stage)
+void VariableResolver::apply(Stage &s)
 {
-       Stage *builtin_stage = get_builtins(stage.type);
+       stage = &s;
+       Stage *builtin_stage = get_builtins(s.type);
        builtins = (builtin_stage ? &builtin_stage->content : 0);
-       stage.content.visit(*this);
+       s.content.visit(*this);
 }
 
 Block *VariableResolver::next_block(Block &block)
@@ -108,38 +110,38 @@ void VariableResolver::visit(VariableReference &var)
 {
        var.declaration = 0;
        members = 0;
-       for(Block *block=current_block; block; block=next_block(*block))
+       for(Block *block=current_block; (!var.declaration && block); block=next_block(*block))
        {
                map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
                if(i!=block->variables.end())
                        var.declaration = i->second;
-               else
-               {
-                       const map<string, InterfaceBlock *> &ifaces = block->interfaces;
-                       for(map<string, InterfaceBlock *>::const_iterator j=ifaces.begin(); (!var.declaration && j!=ifaces.end()); ++j)
-                               if(j->second->instance_name.empty())
-                               {
-                                       i = j->second->members.variables.find(var.name);
-                                       if(i!=j->second->members.variables.end())
-                                               var.declaration = i->second;
-                               }
-               }
-
-               if(var.declaration)
-               {
-                       if(var.declaration->type_declaration)
-                               members = &var.declaration->type_declaration->members.variables;
-                       break;
-               }
+       }
 
-               map<string, InterfaceBlock *>::iterator j = block->interfaces.find(var.name);
-               if(j!=block->interfaces.end() && j->second->instance_name==var.name)
+       if(var.declaration)
+       {
+               if(var.declaration->type_declaration)
+                       members = &var.declaration->type_declaration->members.variables;
+       }
+       else
+       {
+               const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
+               map<string, InterfaceBlock *>::const_iterator i = blocks.find(var.name);
+               if(i!=blocks.end() && i->second->instance_name==var.name)
                {
                        iface_ref = new InterfaceBlockReference;
                        iface_ref->name = var.name;
-                       iface_ref->declaration = j->second;
-                       members = &j->second->members.variables;
-                       break;
+                       iface_ref->declaration = i->second;
+                       members = &i->second->members.variables;
+               }
+               else
+               {
+                       for(i=blocks.begin(); (!var.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())
+                                               var.declaration = j->second;
+                               }
                }
        }
 
@@ -162,11 +164,11 @@ void VariableResolver::visit(InterfaceBlockReference &iface)
        iface.declaration = 0;
        for(Block *block=current_block; block; block=next_block(*block))
        {
-               map<string, InterfaceBlock *>::iterator j = block->interfaces.find(iface.name);
-               if(j!=block->interfaces.end())
+               map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
+               if(i!=stage->interface_blocks.end())
                {
-                       iface.declaration = j->second;
-                       members = &j->second->members.variables;
+                       iface.declaration = i->second;
+                       members = &i->second->members.variables;
                        break;
                }
        }
@@ -237,17 +239,14 @@ void VariableResolver::visit(Assignment &assign)
 void VariableResolver::visit(StructDeclaration &strct)
 {
        TraversingVisitor::visit(strct);
-       current_block->types[strct.name] = &strct;
+       stage->types[strct.name] = &strct;
 }
 
 void VariableResolver::visit(VariableDeclaration &var)
 {
-       for(Block *block=current_block; block; block=next_block(*block))
-       {
-               map<string, StructDeclaration *>::iterator j = block->types.find(var.type);
-               if(j!=block->types.end())
-                       var.type_declaration = j->second;
-       }
+       map<string, StructDeclaration *>::iterator i = stage->types.find(var.type);
+       if(i!=stage->types.end())
+               var.type_declaration = i->second;
 
        if(!block_interface.empty() && var.interface.empty())
                var.interface = block_interface;
@@ -260,9 +259,9 @@ void VariableResolver::visit(InterfaceBlock &iface)
 {
        /* Block names can't be used for any other identifiers so we can put them
        in the same map with instance names. */
-       current_block->interfaces[iface.name] = &iface;
+       stage->interface_blocks[iface.name] = &iface;
        if(!iface.instance_name.empty())
-               current_block->interfaces[iface.instance_name] = &iface;
+               stage->interface_blocks[iface.instance_name] = &iface;
 
        SetForScope<string> set_iface(block_interface, iface.interface);
        TraversingVisitor::visit(iface);
@@ -376,7 +375,7 @@ bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const stri
 
 bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
 {
-       if(stage->content.interfaces.count(out_block.name))
+       if(stage->interface_blocks.count(out_block.name))
                return false;
 
        InterfaceBlock *in_block = new InterfaceBlock;
@@ -398,9 +397,9 @@ bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
        }
 
        iface_target_block->body.insert(iface_insert_point, in_block);
-       iface_target_block->interfaces[in_block->name] = in_block;
+       stage->interface_blocks[in_block->name] = in_block;
        if(!in_block->instance_name.empty())
-               stage->content.interfaces[in_block->instance_name] = in_block;
+               stage->interface_blocks[in_block->instance_name] = in_block;
 
        SetFlag set_scope(function_scope, false);
        SetForScope<Block *> set_block(current_block, &stage->content);
@@ -446,7 +445,7 @@ void InterfaceGenerator::visit(VariableReference &var)
                return;
        }
 
-       const map<string, InterfaceBlock *> &prev_blocks = stage->previous->content.interfaces;
+       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" && j->second->instance_name==var.name)
        {
@@ -527,7 +526,7 @@ void InterfaceGenerator::visit(InterfaceBlock &iface)
        {
                if(!iface.linked_block && stage->previous)
                {
-                       const map<string, InterfaceBlock *> &prev_blocks = stage->previous->content.interfaces;
+                       const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
                        map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
                        if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
                        {
index d0cbc08f279c4b8c12837abf2e0f040b784f6f2b..f1f10b897c281e9ec4a39ff032a167963b936b8a 100644 (file)
@@ -39,6 +39,7 @@ private:
 class VariableResolver: private TraversingVisitor
 {
 private:
+       Stage *stage;
        Block *builtins;
        std::map<std::string, VariableDeclaration *> *members;
        RefPtr<InterfaceBlockReference> iface_ref;
index 7729611c2a5e2a3d013b2e8a5112391af52ae668..49699f2ed3811880e5ecbfb0495549379ff41784 100644 (file)
@@ -108,9 +108,7 @@ struct Block: Node
 {
        NodeList<Statement> body;
        bool use_braces;
-       std::map<std::string, StructDeclaration *> types;
        std::map<std::string, VariableDeclaration *> variables;
-       std::map<std::string, InterfaceBlock *> interfaces;
        Block *parent;
 
        Block();
@@ -401,6 +399,8 @@ struct Stage
        Type type;
        Stage *previous;
        Block content;
+       std::map<std::string, StructDeclaration *> types;
+       std::map<std::string, InterfaceBlock *> interface_blocks;
        std::map<std::string, unsigned> locations;
        Features required_features;
 
index 051d92f6adfc1e48f9117a32797d3004f9759e10..fd25bf342a7ea588525be88079166453474c4e8e 100644 (file)
@@ -158,7 +158,7 @@ void NodeRemover::visit(Block &block)
 void NodeRemover::visit(StructDeclaration &strct)
 {
        if(to_remove->count(&strct))
-               remove_from_map(current_block->types, strct.name, strct);
+               remove_from_map(stage->types, strct.name, strct);
 }
 
 void NodeRemover::visit(VariableDeclaration &var)
@@ -178,9 +178,9 @@ void NodeRemover::visit(InterfaceBlock &iface)
 {
        if(to_remove->count(&iface))
        {
-               remove_from_map(current_block->interfaces, iface.name, iface);
+               remove_from_map(stage->interface_blocks, iface.name, iface);
                if(!iface.instance_name.empty())
-                       remove_from_map(current_block->interfaces, iface.instance_name, iface);
+                       remove_from_map(stage->interface_blocks, iface.instance_name, iface);
        }
        SetFlag set_recursive(recursive_remove, recursive_remove || to_remove->count(&iface));
        TraversingVisitor::visit(iface);