From 14b46ca054f563d3d23073633feafc4a6fcc4e05 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 24 Feb 2021 00:11:43 +0200 Subject: [PATCH] Refactor scope management 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 | 24 +++--------------------- source/glsl/generate.h | 6 ++---- source/glsl/visitor.cpp | 6 ++++++ source/glsl/visitor.h | 1 + 4 files changed, 12 insertions(+), 25 deletions(-) diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 952f1f0b..0662f989 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -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 set_block(current_block, &func.body); - func.body.variables.clear(); - TraversingVisitor::visit(func); -} - -void VariableResolver::visit(Iteration &iter) -{ - SetForScope set_block(current_block, &iter.body); - iter.body.variables.clear(); - TraversingVisitor::visit(iter); -} - void FunctionResolver::visit(FunctionCall &call) { diff --git a/source/glsl/generate.h b/source/glsl/generate.h index 6740f38e..c54860fc 100644 --- a/source/glsl/generate.h +++ b/source/glsl/generate.h @@ -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 diff --git a/source/glsl/visitor.cpp b/source/glsl/visitor.cpp index 1a7adcc5..fe0175c2 100644 --- a/source/glsl/visitor.cpp +++ b/source/glsl/visitor.cpp @@ -15,6 +15,8 @@ void NodeVisitor::visit(Assignment &assign) void TraversingVisitor::visit(Block &block) { + if(&block!=current_block) + enter(block); SetForScope set_block(current_block, &block); for(NodeList::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 set_block(current_block, &func.body); for(NodeArray::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 set_block(current_block, &iter.body); if(iter.init_statement) iter.init_statement->visit(*this); if(iter.condition) diff --git a/source/glsl/visitor.h b/source/glsl/visitor.h index ba345c45..86dd79dc 100644 --- a/source/glsl/visitor.h +++ b/source/glsl/visitor.h @@ -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 &); -- 2.45.2