From a36992487d018d8801ead6980b362b00a2f5f5c5 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 9 Nov 2016 15:59:17 +0200 Subject: [PATCH] Remove unused variable and struct declarations from the syntax tree --- source/programcompiler.cpp | 175 ++++++++++++++++++++++++++++++++++++- source/programcompiler.h | 41 +++++++++ source/programparser.cpp | 4 +- source/programparser.h | 2 + source/programsyntax.cpp | 92 +++++++++++++++++++ source/programsyntax.h | 29 +++++- 6 files changed, 336 insertions(+), 7 deletions(-) diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp index 1da80bab..92dec9c7 100644 --- a/source/programcompiler.cpp +++ b/source/programcompiler.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "error.h" @@ -60,6 +61,22 @@ void ProgramCompiler::process() void ProgramCompiler::process(Context &context) { inject_block(context.content, module->global_context.content); + + VariableResolver resolver; + context.content.visit(resolver); + + while(1) + { + UnusedVariableLocator unused_locator; + context.content.visit(unused_locator); + + NodeRemover remover; + remover.to_remove.insert(unused_locator.unused_variables.begin(), unused_locator.unused_variables.end()); + context.content.visit(remover); + + if(!remover.n_removed) + break; + } } void ProgramCompiler::inject_block(Block &target, const Block &source) @@ -229,14 +246,13 @@ void ProgramCompiler::Formatter::visit(InterfaceBlock &iface) void ProgramCompiler::Formatter::visit(FunctionDeclaration &func) { formatted += format("%s %s(", func.return_type, func.name); - parameter_list = true; for(vector >::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) { if(i!=func.parameters.begin()) formatted += ", "; + SetFlag set(parameter_list); (*i)->visit(*this); } - parameter_list = false; formatted += ')'; if(func.definition) { @@ -263,9 +279,8 @@ void ProgramCompiler::Formatter::visit(Conditional &cond) if(!cond.else_body.body.empty()) { formatted += format("\n%selse", string(indent*2, ' ')); - else_if = true; + SetFlag set(else_if); cond.else_body.visit(*this); - else_if = false; } } @@ -288,5 +303,157 @@ void ProgramCompiler::Formatter::visit(Return &ret) formatted += ';'; } + +ProgramCompiler::VariableResolver::VariableResolver(): + anonymous(false) +{ } + +void ProgramCompiler::VariableResolver::visit(Block &block) +{ + blocks.push_back(&block); + block.variables.clear(); + TraversingVisitor::visit(block); + blocks.pop_back(); +} + +void ProgramCompiler::VariableResolver::visit(VariableReference &var) +{ + var.declaration = 0; + type = 0; + for(vector::iterator i=blocks.end(); i!=blocks.begin(); ) + { + --i; + map::iterator j = (*i)->variables.find(var.name); + if(j!=(*i)->variables.end()) + { + var.declaration = j->second; + type = j->second->type_declaration; + break; + } + } +} + +void ProgramCompiler::VariableResolver::visit(MemberAccess &memacc) +{ + type = 0; + TraversingVisitor::visit(memacc); + memacc.declaration = 0; + if(type) + { + map::iterator i = type->members.variables.find(memacc.member); + if(i!=type->members.variables.end()) + { + memacc.declaration = i->second; + type = i->second->type_declaration; + } + else + type = 0; + } +} + +void ProgramCompiler::VariableResolver::visit(BinaryExpression &binary) +{ + if(binary.oper=="[") + { + binary.right->visit(*this); + type = 0; + binary.left->visit(*this); + } + else + { + TraversingVisitor::visit(binary); + type = 0; + } +} + +void ProgramCompiler::VariableResolver::visit(StructDeclaration &strct) +{ + TraversingVisitor::visit(strct); + blocks.back()->types[strct.name] = &strct; +} + +void ProgramCompiler::VariableResolver::visit(VariableDeclaration &var) +{ + for(vector::iterator i=blocks.end(); i!=blocks.begin(); ) + { + --i; + map::iterator j = (*i)->types.find(var.type); + if(j!=(*i)->types.end()) + var.type_declaration = j->second; + } + + TraversingVisitor::visit(var); + blocks.back()->variables[var.name] = &var; + if(anonymous && blocks.size()>1) + blocks[blocks.size()-2]->variables[var.name] = &var; +} + +void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface) +{ + SetFlag set(anonymous); + TraversingVisitor::visit(iface); +} + + +void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var) +{ + unused_variables.erase(var.declaration); +} + +void ProgramCompiler::UnusedVariableLocator::visit(MemberAccess &memacc) +{ + TraversingVisitor::visit(memacc); + unused_variables.erase(memacc.declaration); +} + +void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var) +{ + unused_variables.insert(&var); + TraversingVisitor::visit(var); +} + + +ProgramCompiler::NodeRemover::NodeRemover(): + n_removed(0), + immutable_block(false), + remove_block(false) +{ } + +void ProgramCompiler::NodeRemover::visit(Block &block) +{ + remove_block = immutable_block; + for(list >::iterator i=block.body.begin(); i!=block.body.end(); ) + { + bool remove = false; + if(to_remove.count(&**i)) + remove = !immutable_block; + else + { + remove_block = false; + (*i)->visit(*this); + remove = remove_block; + } + + if(remove) + block.body.erase(i++); + else + ++i; + + n_removed += remove; + } +} + +void ProgramCompiler::NodeRemover::visit(StructDeclaration &strct) +{ + SetFlag set(immutable_block); + TraversingVisitor::visit(strct); +} + +void ProgramCompiler::NodeRemover::visit(InterfaceBlock &iface) +{ + SetFlag set(immutable_block); + TraversingVisitor::visit(iface); +} + } // namespace GL } // namespace Msp diff --git a/source/programcompiler.h b/source/programcompiler.h index 63ead7a4..0ea485a9 100644 --- a/source/programcompiler.h +++ b/source/programcompiler.h @@ -1,6 +1,7 @@ #ifndef MSP_GL_PROGRAMCOMPILER_H_ #define MSP_GL_PROGRAMCOMPILER_H_ +#include #include "programparser.h" #include "programsyntax.h" @@ -41,6 +42,46 @@ public: }; private: + struct VariableResolver: ProgramSyntax::TraversingVisitor + { + std::vector blocks; + ProgramSyntax::StructDeclaration *type; + bool anonymous; + + VariableResolver(); + + virtual void visit(ProgramSyntax::Block &); + virtual void visit(ProgramSyntax::VariableReference &); + virtual void visit(ProgramSyntax::MemberAccess &); + virtual void visit(ProgramSyntax::BinaryExpression &); + virtual void visit(ProgramSyntax::StructDeclaration &); + virtual void visit(ProgramSyntax::VariableDeclaration &); + virtual void visit(ProgramSyntax::InterfaceBlock &); + }; + + struct UnusedVariableLocator: ProgramSyntax::TraversingVisitor + { + std::set unused_variables; + + virtual void visit(ProgramSyntax::VariableReference &); + virtual void visit(ProgramSyntax::MemberAccess &); + virtual void visit(ProgramSyntax::VariableDeclaration &); + }; + + struct NodeRemover: ProgramSyntax::TraversingVisitor + { + std::set to_remove; + unsigned n_removed; + bool immutable_block; + bool remove_block; + + NodeRemover(); + + virtual void visit(ProgramSyntax::Block &); + virtual void visit(ProgramSyntax::StructDeclaration &); + virtual void visit(ProgramSyntax::InterfaceBlock &); + }; + ProgramParser parser; ProgramSyntax::Module *module; diff --git a/source/programparser.cpp b/source/programparser.cpp index bf2d42da..c6cdfd06 100644 --- a/source/programparser.cpp +++ b/source/programparser.cpp @@ -310,7 +310,7 @@ bool ProgramParser::is_builtin_type(const string &token) bool ProgramParser::is_type(const string &token) { - return is_builtin_type(token) || cur_module->structs.count(token); + return is_builtin_type(token) || declared_types.count(token); } bool ProgramParser::is_identifier(const string &token) @@ -550,7 +550,7 @@ StructDeclaration *ProgramParser::parse_struct_declaration() parse_block(strct->members, true); expect(";"); - cur_module->structs[strct->name] = strct.get(); + declared_types.insert(strct->name); return strct.release(); } diff --git a/source/programparser.h b/source/programparser.h index 4bd6b70e..390f19cb 100644 --- a/source/programparser.h +++ b/source/programparser.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include "programsyntax.h" @@ -40,6 +41,7 @@ private: std::deque next_tokens; ProgramSyntax::Module main_module; ProgramSyntax::Module *cur_module; + std::set declared_types; static Operator operators[]; diff --git a/source/programsyntax.cpp b/source/programsyntax.cpp index 5b39d7f3..f64493ee 100644 --- a/source/programsyntax.cpp +++ b/source/programsyntax.cpp @@ -28,6 +28,10 @@ void ParenthesizedExpression::visit(NodeVisitor &visitor) } +VariableReference::VariableReference(): + declaration(0) +{ } + void VariableReference::visit(NodeVisitor &visitor) { visitor.visit(*this); @@ -95,6 +99,7 @@ void StructDeclaration::visit(NodeVisitor &visitor) VariableDeclaration::VariableDeclaration(): constant(false), + type_declaration(0), array(false) { } @@ -143,6 +148,93 @@ void Iteration::visit(NodeVisitor &visitor) } +void TraversingVisitor::visit(Block &block) +{ + for(list >::iterator i=block.body.begin(); i!=block.body.end(); ++i) + (*i)->visit(*this); +} + +void TraversingVisitor::visit(ParenthesizedExpression &parexpr) +{ + parexpr.expression->visit(*this); +} + +void TraversingVisitor::visit(MemberAccess &memacc) +{ + memacc.left->visit(*this); +} + +void TraversingVisitor::visit(UnaryExpression &unary) +{ + unary.expression->visit(*this); +} + +void TraversingVisitor::visit(BinaryExpression &binary) +{ + binary.left->visit(*this); + binary.right->visit(*this); +} + +void TraversingVisitor::visit(FunctionCall &call) +{ + for(vector >::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i) + (*i)->visit(*this); +} + +void TraversingVisitor::visit(ExpressionStatement &expr) +{ + expr.expression->visit(*this); +} + +void TraversingVisitor::visit(StructDeclaration &strct) +{ + strct.members.visit(*this); +} + +void TraversingVisitor::visit(VariableDeclaration &var) +{ + if(var.init_expression) + var.init_expression->visit(*this); + if(var.array_size) + var.array_size->visit(*this); +} + +void TraversingVisitor::visit(InterfaceBlock &iface) +{ + iface.members.visit(*this); +} + +void TraversingVisitor::visit(FunctionDeclaration &func) +{ + for(vector >::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) + (*i)->visit(*this); + func.body.visit(*this); +} + +void TraversingVisitor::visit(Conditional &cond) +{ + cond.condition->visit(*this); + cond.body.visit(*this); + cond.else_body.visit(*this); +} + +void TraversingVisitor::visit(Iteration &iter) +{ + iter.init_statement->visit(*this); + if(iter.condition) + iter.condition->visit(*this); + if(iter.loop_expression) + iter.loop_expression->visit(*this); + iter.body.visit(*this); +} + +void TraversingVisitor::visit(Return &ret) +{ + if(ret.expression) + ret.expression->visit(*this); +} + + Context::Context(ContextType t): type(t), present(false) diff --git a/source/programsyntax.h b/source/programsyntax.h index 8f72a0bd..11a67430 100644 --- a/source/programsyntax.h +++ b/source/programsyntax.h @@ -41,15 +41,20 @@ private: public: T *operator->() { return node; } const T *operator->() const { return node; } + T &operator*() { return *node; } + const T &operator*() const { return *node; } operator void *() const { return node; } }; +struct StructDeclaration; struct VariableDeclaration; struct Block: Node { std::list > body; bool use_braces; + std::map types; + std::map variables; Block(); @@ -81,6 +86,9 @@ struct ParenthesizedExpression: Expression struct VariableReference: Expression { std::string name; + VariableDeclaration *declaration; + + VariableReference(); virtual VariableReference *clone() const { return new VariableReference(*this); } virtual void visit(NodeVisitor &); @@ -90,6 +98,7 @@ struct MemberAccess: Expression { NodePtr left; std::string member; + VariableDeclaration *declaration; virtual MemberAccess *clone() const { return new MemberAccess(*this); } virtual void visit(NodeVisitor &); @@ -173,6 +182,7 @@ struct VariableDeclaration: Node std::string sampling; std::string interface; std::string type; + StructDeclaration *type_declaration; std::string name; bool array; NodePtr array_size; @@ -262,6 +272,24 @@ struct NodeVisitor virtual void visit(Return &) { } }; +struct TraversingVisitor: NodeVisitor +{ + virtual void visit(Block &); + virtual void visit(ParenthesizedExpression &); + virtual void visit(MemberAccess &); + virtual void visit(UnaryExpression &); + virtual void visit(BinaryExpression &); + virtual void visit(FunctionCall &); + virtual void visit(ExpressionStatement &); + virtual void visit(StructDeclaration &); + virtual void visit(VariableDeclaration &); + virtual void visit(InterfaceBlock &); + virtual void visit(FunctionDeclaration &); + virtual void visit(Conditional &); + virtual void visit(Iteration &); + virtual void visit(Return &); +}; + enum ContextType { GLOBAL, @@ -285,7 +313,6 @@ struct Module Context vertex_context; Context geometry_context; Context fragment_context; - std::map structs; Module(); }; -- 2.45.2