]> git.tdb.fi Git - libs/gl.git/commitdiff
Remove unused variable and struct declarations from the syntax tree
authorMikko Rasa <tdb@tdb.fi>
Wed, 9 Nov 2016 13:59:17 +0000 (15:59 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 9 Nov 2016 14:00:07 +0000 (16:00 +0200)
source/programcompiler.cpp
source/programcompiler.h
source/programparser.cpp
source/programparser.h
source/programsyntax.cpp
source/programsyntax.h

index 1da80bab07871f02befd3a2cc960b22ab1ebd486..92dec9c778913388620f2bcd0dde7e6903a4a276 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/raii.h>
 #include <msp/strings/format.h>
 #include <msp/strings/utils.h>
 #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<NodePtr<VariableDeclaration> >::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<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
+       {
+               --i;
+               map<string, VariableDeclaration *>::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<string, VariableDeclaration *>::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<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
+       {
+               --i;
+               map<string, StructDeclaration *>::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<NodePtr<Node> >::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
index 63ead7a4c8997f0a6f3bd4d76f8bee10da2a9958..0ea485a9765d2faa7cafca338b622c5955b936da 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GL_PROGRAMCOMPILER_H_
 #define MSP_GL_PROGRAMCOMPILER_H_
 
+#include <set>
 #include "programparser.h"
 #include "programsyntax.h"
 
@@ -41,6 +42,46 @@ public:
        };
 
 private:
+       struct VariableResolver: ProgramSyntax::TraversingVisitor
+       {
+               std::vector<ProgramSyntax::Block *> 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<ProgramSyntax::VariableDeclaration *> unused_variables;
+
+               virtual void visit(ProgramSyntax::VariableReference &);
+               virtual void visit(ProgramSyntax::MemberAccess &);
+               virtual void visit(ProgramSyntax::VariableDeclaration &);
+       };
+
+       struct NodeRemover: ProgramSyntax::TraversingVisitor
+       {
+               std::set<ProgramSyntax::Node *> 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;
 
index bf2d42daedbfd232dcc0dd194deb7c60ddf76625..c6cdfd06246f1c14683919fcfec637b8adf85058 100644 (file)
@@ -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();
 }
 
index 4bd6b70e99e1b9e2064eb9de9416a253ae5286bc..390f19cbdf921a24ca6c2a6f00adcb76d7c9cce9 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <deque>
 #include <map>
+#include <set>
 #include <string>
 #include <msp/io/base.h>
 #include "programsyntax.h"
@@ -40,6 +41,7 @@ private:
        std::deque<std::string> next_tokens;
        ProgramSyntax::Module main_module;
        ProgramSyntax::Module *cur_module;
+       std::set<std::string> declared_types;
 
        static Operator operators[];
 
index 5b39d7f3036a4f90e9f495f9ff2f172e02cc59e2..f64493eed2a116fae42a88791a6183a084c06e0a 100644 (file)
@@ -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<NodePtr<Node> >::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<NodePtr<Expression> >::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<NodePtr<VariableDeclaration> >::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)
index 8f72a0bdfc7258a4af9bdb048e0aad114b4a83a2..11a67430926849adee88a32cb636bcde544d1736 100644 (file)
@@ -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<NodePtr<Node> > body;
        bool use_braces;
+       std::map<std::string, StructDeclaration *> types;
+       std::map<std::string, VariableDeclaration *> 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<Expression> 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<Expression> 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<std::string, StructDeclaration *> structs;
 
        Module();
 };