]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor scope management
authorMikko Rasa <tdb@tdb.fi>
Tue, 23 Feb 2021 22:11:43 +0000 (00:11 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 23 Feb 2021 22:11:43 +0000 (00:11 +0200)
TraversingVisitor now takes care of maintaining current_block correctly
for functions and loops.  This fixes an issue where InterfaceGenerator
was treating function in/out parameters as global interface variables.
Now there's a different bug where it generates global interface variables
for them.

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

index 952f1f0bfa4f1bddb2e1bc7585f3829fda9ce78b..0662f9891f8adec9cbd6bf32da8f47f3b75bed1b 100644 (file)
@@ -73,10 +73,9 @@ void DeclarationCombiner::visit(VariableDeclaration &var)
 }
 
 
-void BlockResolver::visit(Block &block)
+void BlockResolver::enter(Block &block)
 {
        block.parent = current_block;
-       TraversingVisitor::visit(block);
 }
 
 void BlockResolver::visit(InterfaceBlock &iface)
@@ -104,12 +103,9 @@ Block *VariableResolver::next_block(Block &block)
        return block.parent ? block.parent : &block!=builtins ? builtins : 0;
 }
 
-void VariableResolver::visit(Block &block)
+void VariableResolver::enter(Block &block)
 {
-       if(current_block!=&block)
-               block.variables.clear();
-
-       TraversingVisitor::visit(block);
+       block.variables.clear();
 }
 
 void VariableResolver::visit(VariableReference &var)
@@ -222,20 +218,6 @@ void VariableResolver::visit(InterfaceBlock &iface)
        TraversingVisitor::visit(iface);
 }
 
-void VariableResolver::visit(FunctionDeclaration &func)
-{
-       SetForScope<Block *> set_block(current_block, &func.body);
-       func.body.variables.clear();
-       TraversingVisitor::visit(func);
-}
-
-void VariableResolver::visit(Iteration &iter)
-{
-       SetForScope<Block *> set_block(current_block, &iter.body);
-       iter.body.variables.clear();
-       TraversingVisitor::visit(iter);
-}
-
 
 void FunctionResolver::visit(FunctionCall &call)
 {
index 6740f38ea5c49e531b9fbbabf9b285e08ea7f24b..c54860fc8c09d7e4a97b93edf721dfcf30d2792a 100644 (file)
@@ -33,7 +33,7 @@ public:
        void apply(Stage &s) { s.content.visit(*this); }
 
 private:
-       virtual void visit(Block &);
+       virtual void enter(Block &);
        virtual void visit(InterfaceBlock &);
 };
 
@@ -55,7 +55,7 @@ public:
 private:
        Block *next_block(Block &);
 
-       virtual void visit(Block &);
+       virtual void enter(Block &);
        virtual void visit(VariableReference &);
        virtual void visit(MemberAccess &);
        virtual void visit(BinaryExpression &);
@@ -63,8 +63,6 @@ private:
        virtual void visit(StructDeclaration &);
        virtual void visit(VariableDeclaration &);
        virtual void visit(InterfaceBlock &);
-       virtual void visit(FunctionDeclaration &);
-       virtual void visit(Iteration &);
 };
 
 class FunctionResolver: private TraversingVisitor
index 1a7adcc5287a40894a20fc8a9a3926a84db01140..fe0175c2214f51946fb7805fa866606964e335fc 100644 (file)
@@ -15,6 +15,8 @@ void NodeVisitor::visit(Assignment &assign)
 
 void TraversingVisitor::visit(Block &block)
 {
+       if(&block!=current_block)
+               enter(block);
        SetForScope<Block *> set_block(current_block, &block);
        for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
                (*i)->visit(*this);
@@ -79,6 +81,8 @@ void TraversingVisitor::visit(InterfaceBlock &iface)
 
 void TraversingVisitor::visit(FunctionDeclaration &func)
 {
+       enter(func.body);
+       SetForScope<Block *> set_block(current_block, &func.body);
        for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
                (*i)->visit(*this);
        func.body.visit(*this);
@@ -93,6 +97,8 @@ void TraversingVisitor::visit(Conditional &cond)
 
 void TraversingVisitor::visit(Iteration &iter)
 {
+       enter(iter.body);
+       SetForScope<Block *> set_block(current_block, &iter.body);
        if(iter.init_statement)
                iter.init_statement->visit(*this);
        if(iter.condition)
index ba345c450bee95953e2ecb45d0322fd7f127004e..86dd79dc10e2432913cf4e89b3201687c797dd54 100644 (file)
@@ -49,6 +49,7 @@ protected:
        TraversingVisitor(): current_block(0) { }
 
 public:
+       virtual void enter(Block &) { }
        virtual void visit(Block &);
        virtual void visit(ParenthesizedExpression &);
        virtual void visit(MemberAccess &);